1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
|
#!/usr/bin/env perl
## Copyright © 2010 by Daniel Friesel <derf@finalrewind.org>
## License: WTFPL <http://sam.zoy.org/wtfpl>
## 0. You just DO WHAT THE FUCK YOU WANT TO.
use strict;
use warnings;
use 5.010;
use autodie;
use Filesys::Df;
use GD;
use Getopt::Std;
use Term::ANSIColor;
my @mounts;
my %opts;
my $alpha;
sub format_size {
my ($bytes) = @_;
my @postfix = (' ', 'k', 'M', 'G', 'T');
while ($bytes > 1000) {
$bytes /= 1000;
shift @postfix;
}
return sprintf('%3.1f%s', $bytes, $postfix[0]);
}
sub bar_console {
my ($percent, $width) = @_;
my $vlen = sprintf("%d", $width * $percent / 100);
return sprintf(
'[%s%s]',
'=' x ($vlen),
' ' x ($width - $vlen),
);
}
sub bar_png {
my ($percent, $w, $h) = @_;
my $bar = GD::Image->new($w, $h);
my $l_int = 6;
my ($w_o, $w_w) = ($w * 0.8, $w * 0.9);
my $black = $bar->colorAllocateAlpha( 0, 0, 0, $alpha);
my $gray = $bar->colorAllocateAlpha(127, 127, 127, $alpha);
my $lgray = $bar->colorAllocateAlpha(191, 191, 191, $alpha);
my $ok_b = $bar->colorAllocateAlpha(220, 240, 220, $alpha);
my $ok_f = $bar->colorAllocateAlpha( 59, 191, 50, $alpha);
my $wa_b = $bar->colorAllocateAlpha(240, 240, 220, $alpha);
my $wa_f = $bar->colorAllocateAlpha(191, 191, 50, $alpha);
my $cr_b = $bar->colorAllocateAlpha(240, 220, 220, $alpha);
my $cr_f = $bar->colorAllocateAlpha(191, 0, 0, $alpha);
my $white = $bar->colorAllocateAlpha(255, 255, 255, $alpha);
my $vwidth = sprintf("%d", ($w- 2) * $percent / 100);
$bar->rectangle(0, 0, $w - 1, $h - 1, $gray);
$bar->filledRectangle(1, 1, $w - 2, $h - 2, $cr_b);
$bar->filledRectangle(1, 1, $w_w - 1, $h - 2, $wa_b);
$bar->filledRectangle(1, 1, $w_o - 1, $h - 2, $ok_b);
$bar->filledRectangle(1, 1, $vwidth , $h - 2, $ok_f);
if ($vwidth > $w_o) {
$bar->filledRectangle($w_o, 1, $vwidth, $h - 2, $wa_f);
}
if ($vwidth > $w_w) {
$bar->filledRectangle($w_w, 1, $vwidth, $h - 2, $cr_f);
}
for my $i (1 .. $w / $l_int) {
$bar->line($i * $l_int, 1, $i * $l_int, $h - 2, $white);
}
return $bar;
}
sub show_df_console {
my $mp_width = 10;
foreach my $mount (@mounts) {
my $this_width = length($mount->[1]);
if ($this_width > $mp_width) {
$mp_width = $this_width;
}
}
my $h_format = "%-${mp_width}s %6s %6s %6s %4s%% %20s %s\n";
my $l_format = "%-${mp_width}s %6s %6s %6s %04.1f%% %20s %s\n";
print color('yellow');
printf(
$h_format,
qw{Mountpoint Size Used Avail Use},
q{},
q{Filesystem},
);
print color('reset');
foreach my $mount (@mounts) {
# $mount->[4]->{'per'} (provided by Filesys::Df) is inaccurate
my $percent = $mount->[4]->{'used'} * 100 / $mount->[4]->{'blocks'};
printf(
$l_format,
$mount->[1],
format_size($mount->[4]->{'blocks'}),
format_size($mount->[4]->{'used'}),
format_size($mount->[4]->{'bavail'}),
$percent,
bar_console($percent, 18),
$mount->[0],
);
}
return;
}
sub show_df_png {
my $spacing = 32;
my $h = @mounts * $spacing;
my $w = 200;
my $offset_left = 5;
my $offset_top = 3;
my $offset_right = 5;
my $offset_free = $w - $offset_right - (6 * 6);
my $bar_width = $w - $offset_left - $offset_right;
my $bar_height = 10;
my $bar_top = $offset_top + 13;
my $im = GD::Image->new($w, $h);
my $black = $im->colorAllocateAlpha( 0, 0, 0, $alpha);
my $gray = $im->colorAllocateAlpha(127, 127, 127, $alpha);
my $lgray = $im->colorAllocateAlpha(191, 191, 191, $alpha);
my $white = $im->colorAllocateAlpha(255, 255, 255, $alpha);
$im->rectangle(0, 0, $w - 1, $h - 1, $gray);
$im->filledRectangle(1, 1, $w - 2, $h - 2, $white);
for my $i (0 .. $#mounts) {
my $mount = $mounts[$i];
my $percent = $mount->[4]->{'used'} * 100 / $mount->[4]->{'blocks'};
$im->string(gdSmallFont, $offset_left, $spacing * $i + $offset_top,
$mount->[1], $black);
$im->string(gdSmallFont, $offset_free, $spacing * $i + $offset_top,
sprintf("%6s", format_size($mount->[4]->{'bavail'})), $black);
$im->copy(bar_png($percent, $bar_width, $bar_height), $offset_left,
$spacing * $i + $bar_top, 0, 0, $bar_width, $bar_height);
}
open(my $out_fh, '>', '/tmp/vdf.png');
binmode $out_fh;
print $out_fh $im->png();
close($out_fh);
return;
}
getopts('Aa:cfg', \%opts);
$alpha = $opts{'a'} // 0;
open(my $mounts_fh, '<', '/proc/mounts');
while (my $line = <$mounts_fh>) {
my ($device, $mountpoint, $type, $flags) = split(qr{ }, $line);
my $df_ref = df($mountpoint, 1);
if (not defined $df_ref) {
next;
}
push(@mounts, [
$device,
$mountpoint,
$type,
$flags,
$df_ref
]);
}
close($mounts_fh);
if (not $opts{'A'}) {
@mounts = grep { $_->[0] !~ qr{ ^ ( (root|tmp)fs | none ) $ }x } @mounts;
}
if ($opts{'c'}) {
show_df_console();
}
elsif ($opts{'g'}) {
show_df_png();
}
if ($opts{'f'}) {
exec('feh', '/tmp/vdf.png');
}
__END__
=head1 NAME
=head1 SYNOPSIS
=head1 DESCRIPTION
=head1 OPTIONS
=head1 EXIT STATUS
=head1 CONFIGURATION
=head1 DEPENDENCIES
=head1 BUGS AND LIMITATIONS
=head1 AUTHOR
Copyright (C) 2010 by Daniel Friesel E<lt>derf@finalrewind.orgE<gt>
=head1 LICENSE
0. You just DO WHAT THE FUCK YOU WANT TO.
|