5 |
# filesystems in same order always) |
# filesystems in same order always) |
6 |
# |
# |
7 |
# Dobrica Pavlinusic <dpavlin@rot13.org> |
# Dobrica Pavlinusic <dpavlin@rot13.org> |
8 |
|
# http://www.rot13.org/~dpavlin/sysadm.html |
9 |
# |
# |
10 |
# Usage: |
# Usage: |
11 |
# |
# |
12 |
# parse_df.pl "ssh -i ~cricket/.ssh/df host.dom" /mount_point |
# parse_df.pl "ssh -i ~cricket/.ssh/df target.host" /mount_point |
13 |
# ssh into remote host and get data for /mount_point |
# ssh into remote host and get data for /mount_point |
14 |
# |
# |
15 |
# parse_df.pl "ssh -i ~cricket/.ssh/df --config" |
# parse_df.pl "ssh -i ~cricket/.ssh/df target.host" --config |
16 |
# dump configuration file to stdout |
# dump configuration file to stdout |
17 |
# |
# |
18 |
|
|
21 |
my $ssh = shift @ARGV || ""; |
my $ssh = shift @ARGV || ""; |
22 |
my $mount = shift @ARGV || "/"; |
my $mount = shift @ARGV || "/"; |
23 |
|
|
24 |
my %df; |
# do we have Cache::FileCache installed? |
25 |
|
|
26 |
|
my $USE_CACHE = 0; |
27 |
|
eval("use Cache::FileCache;"); |
28 |
|
if (! $@) { $USE_CACHE = 1; } |
29 |
|
|
30 |
|
my $df; |
31 |
|
|
32 |
|
my $cache = new Cache::FileCache() if ($USE_CACHE); |
33 |
|
|
34 |
sub parse_df { |
sub parse_df { |
35 |
my $ssh = shift @_; |
my $ssh = shift @_; |
36 |
open(DF,"$ssh df -k |") || die "$ssh df: $!"; |
if ($USE_CACHE) { |
37 |
|
print STDERR " [using cache] "; |
38 |
|
$df = $cache->get( $ssh ); |
39 |
|
return if (defined $df); |
40 |
|
print STDERR " [cache miss] "; |
41 |
|
} |
42 |
|
open(DF,"$ssh df -klP |") || die "$ssh df: $!"; |
43 |
while(<DF>) { |
while(<DF>) { |
44 |
chomp; |
chomp; |
45 |
my ($node,$total,$used,$aval,$use_pcnt,$mount) = split(/\s+/,$_,6); |
my ($node,$total,$used,$aval,$use_pcnt,$mount) = split(/\s+/,$_,6); |
46 |
if ($use_pcnt && $use_pcnt =~ s/(\d+)%/$1/) { |
if ($use_pcnt && $use_pcnt =~ s/(\d+)%/$1/) { |
47 |
print STDERR "$_\n"; |
# print STDERR "$_\n"; |
48 |
# $df{$mount}=$use_pcnt; |
# $df->{$mount}=$use_pcnt; |
49 |
$df{$mount}=$used * 100 / $total if ($total != 0); |
$df->{$mount}=$used * 100 / $total if ($total != 0); |
50 |
# print $df{$mount}," == $use_pcnt\n"; |
# print $df->{$mount}," == $use_pcnt\n"; |
51 |
} |
} |
52 |
} |
} |
53 |
close(DF); |
close(DF); |
54 |
|
$cache->set( $ssh, $df, "1 min" ) if ($USE_CACHE); |
55 |
} |
} |
56 |
|
|
57 |
parse_df($ssh); |
parse_df($ssh); |
63 |
long-desc = "edit name of your host here" |
long-desc = "edit name of your host here" |
64 |
|
|
65 |
EOF |
EOF |
66 |
foreach my $mnt (keys %df) { |
my @targets; |
67 |
|
|
68 |
|
foreach my $mnt (keys %{$df}) { |
69 |
my $target = $mnt; |
my $target = $mnt; |
70 |
if ($mnt eq "/") { |
if ($mnt eq "/") { |
71 |
$target = "root"; |
$target = "root"; |
72 |
} else { |
} else { |
73 |
$target =~ s,^/,,; |
$target =~ s,^/,,; |
74 |
|
$target =~ s,/,_,g; |
75 |
} |
} |
76 |
|
push @targets,$target; |
77 |
print <<"EOF"; |
print <<"EOF"; |
78 |
target $target |
target $target |
79 |
target-type = usage |
target-type = usage |
80 |
display-name = "$mnt" |
display-name = "$mnt" |
81 |
|
skip-overview = 1 |
82 |
|
|
83 |
EOF |
EOF |
84 |
} |
} |
85 |
|
print "target overview |
86 |
|
mtargets = \"",join("; ",@targets),"\" |
87 |
|
target-type = usage |
88 |
|
y-min = 0 |
89 |
|
y-max = 100\n"; |
90 |
exit 1; |
exit 1; |
91 |
} |
} |
92 |
|
|
93 |
# dump free % |
# dump free % |
94 |
# |
# |
95 |
print $df{$mount} || "unknown"; |
print $df->{$mount} || "unknown"; |
96 |
print "\n"; |
print "\n"; |