1 |
dpavlin |
1.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 |
|
|
my %df; |
24 |
|
|
|
25 |
|
|
sub parse_df { |
26 |
|
|
my $ssh = shift @_; |
27 |
|
|
open(DF,"$ssh df -k |") || die "$ssh df: $!"; |
28 |
|
|
while(<DF>) { |
29 |
|
|
chomp; |
30 |
|
|
my ($node,$total,$used,$aval,$use_pcnt,$mount) = split(/\s+/,$_,6); |
31 |
|
|
if ($use_pcnt && $use_pcnt =~ s/(\d+)%/$1/) { |
32 |
dpavlin |
1.3 |
# print STDERR "$_\n"; |
33 |
dpavlin |
1.1 |
# $df{$mount}=$use_pcnt; |
34 |
dpavlin |
1.2 |
$df{$mount}=$used * 100 / $total if ($total != 0); |
35 |
dpavlin |
1.1 |
# print $df{$mount}," == $use_pcnt\n"; |
36 |
|
|
} |
37 |
|
|
} |
38 |
|
|
close(DF); |
39 |
|
|
} |
40 |
|
|
|
41 |
|
|
parse_df($ssh); |
42 |
|
|
|
43 |
|
|
if ($mount eq "--config") { |
44 |
|
|
print <<"EOF"; |
45 |
|
|
target --default-- |
46 |
|
|
ssh = "$ssh" |
47 |
|
|
long-desc = "edit name of your host here" |
48 |
|
|
|
49 |
|
|
EOF |
50 |
dpavlin |
1.4 |
my @targets; |
51 |
|
|
|
52 |
dpavlin |
1.1 |
foreach my $mnt (keys %df) { |
53 |
|
|
my $target = $mnt; |
54 |
|
|
if ($mnt eq "/") { |
55 |
|
|
$target = "root"; |
56 |
|
|
} else { |
57 |
|
|
$target =~ s,^/,,; |
58 |
dpavlin |
1.3 |
$target =~ s,/,_,g; |
59 |
dpavlin |
1.1 |
} |
60 |
dpavlin |
1.4 |
push @targets,$target; |
61 |
dpavlin |
1.1 |
print <<"EOF"; |
62 |
|
|
target $target |
63 |
|
|
target-type = usage |
64 |
|
|
display-name = "$mnt" |
65 |
|
|
|
66 |
|
|
EOF |
67 |
|
|
} |
68 |
dpavlin |
1.4 |
print "target overview |
69 |
|
|
mtargets = \"",join("; ",@targets),"\" |
70 |
|
|
target-type = usage\n"; |
71 |
dpavlin |
1.1 |
exit 1; |
72 |
|
|
} |
73 |
|
|
|
74 |
|
|
# dump free % |
75 |
|
|
# |
76 |
|
|
print $df{$mount} || "unknown"; |
77 |
|
|
print "\n"; |