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