-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathdiskspace
executable file
·121 lines (105 loc) · 2.58 KB
/
diskspace
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
#!/usr/bin/env perl
# print information about file systems over a certain percentage full
use warnings;
use Getopt::Long;
my $all; # report all file systems
my $minimum; # report file systems with less than this many bytes free
my $threshold; # report file systems over this percent used
my $help;
my $default = 95; # default threshold if no options specified
GetOptions ("help|h" => \$help,
"all|a" => \$all,
"minimum|m:s" => \$minimum,
"threshold|t:s" => \$threshold)
or usage();
if ($help) { usage(); }
# symbols for multiples of 1024 bytes
my @suffixes = ("KB", "MB", "GB", "TB", "PB", "EB");
# output format for file systems at or above threshold
format STDOUT =
@<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< @>>>> @<
$mount, $free,$suffixes[$power]
.
my $full = 0; # number of file systems at or above threshold
@output = `df -Pk`; # retrieve disk space information
shift @output; # strip header
foreach my $line (@output)
{
$\ = "\n";
$, = "\t";
# parse this output line
our ($fs, $blocks, $used, $avail, $percent, $mount) = split (/\s+/, $line);
# strip percent sign
$percent =~ s/%$//;
# convert to three figures plus binary suffix
$free = $avail;
$power = 0;
while ($free >= 1000)
{
$free /= 1024;
$power++;
}
$free = sprintf ("%.1f", $free);
if (length ($free) > 3)
{
$free =~ s/\..*//;
}
# -a specified
if (defined($all))
{
$full++;
write;
}
# both -t and -m specified
if (defined($threshold) && defined($minimum))
{
if ($percent >= $threshold &&
$avail < $minimum)
{
$full++;
write;
}
}
# only -t specified
elsif (defined($threshold))
{
if ($percent >= $threshold)
{
$full++;
write;
}
}
# only -m specified
elsif (defined($minimum))
{
if ($avail < $minimum)
{
$full++;
write;
}
}
# neither -t nor -m specified
else
{
if ($percent >= $default)
{
$full++;
write;
}
}
}
exit $full;
# print a usage message
sub usage
{
print "
Usage:
diskspace [options]
Options:
-h Print a message describing how to run this program
-m bytes Show file systems that have less than bytes bytes free
-t percent Show file systems that are at least percent full
";
exit 2;
}
# vi: set sw=4 ts=33: