/[cricket]/parse_df.pl
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Annotation of /parse_df.pl

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.11 - (hide annotations)
Tue May 13 16:00:53 2003 UTC (20 years, 11 months ago) by dpavlin
Branch: MAIN
CVS Tags: HEAD
Changes since 1.10: +1 -1 lines
File MIME type: text/plain
moved alarm off to avoid hanging on df command (if nfs shares are involved
it's quote often!)

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 dpavlin 1.8 # http://www.rot13.org/~dpavlin/sysadm.html
9 dpavlin 1.1 #
10     # Usage:
11     #
12 dpavlin 1.9 # parse_df.pl "ssh -i ~cricket/.ssh/df target.host" /mount_point
13 dpavlin 1.1 # ssh into remote host and get data for /mount_point
14     #
15 dpavlin 1.9 # parse_df.pl "ssh -i ~cricket/.ssh/df target.host" --config
16 dpavlin 1.1 # dump configuration file to stdout
17     #
18    
19     use strict;
20    
21     my $ssh = shift @ARGV || "";
22     my $mount = shift @ARGV || "/";
23    
24 dpavlin 1.7 # 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 dpavlin 1.1
34     sub parse_df {
35     my $ssh = shift @_;
36 dpavlin 1.7 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 dpavlin 1.10 eval {
43     local $SIG{ALRM} = sub { die "ssh timeout\n"; };
44     alarm 10; # wait for ssh to connect and return first line
45     open(DF,"$ssh df -klP |") || die "$ssh df: $!";
46     };
47     print STDERR " read DF eof=",eof(DF);
48 dpavlin 1.1 while(<DF>) {
49     chomp;
50     my ($node,$total,$used,$aval,$use_pcnt,$mount) = split(/\s+/,$_,6);
51     if ($use_pcnt && $use_pcnt =~ s/(\d+)%/$1/) {
52 dpavlin 1.3 # print STDERR "$_\n";
53 dpavlin 1.7 # $df->{$mount}=$use_pcnt;
54     $df->{$mount}=$used * 100 / $total if ($total != 0);
55     # print $df->{$mount}," == $use_pcnt\n";
56 dpavlin 1.1 }
57     }
58     close(DF);
59 dpavlin 1.11 alarm 0; # turn alarm off
60 dpavlin 1.7 $cache->set( $ssh, $df, "1 min" ) if ($USE_CACHE);
61 dpavlin 1.1 }
62    
63     parse_df($ssh);
64    
65     if ($mount eq "--config") {
66     print <<"EOF";
67     target --default--
68     ssh = "$ssh"
69     long-desc = "edit name of your host here"
70    
71     EOF
72 dpavlin 1.4 my @targets;
73    
74 dpavlin 1.7 foreach my $mnt (keys %{$df}) {
75 dpavlin 1.1 my $target = $mnt;
76     if ($mnt eq "/") {
77     $target = "root";
78     } else {
79     $target =~ s,^/,,;
80 dpavlin 1.3 $target =~ s,/,_,g;
81 dpavlin 1.1 }
82 dpavlin 1.4 push @targets,$target;
83 dpavlin 1.1 print <<"EOF";
84     target $target
85     target-type = usage
86     display-name = "$mnt"
87 dpavlin 1.5 skip-overview = 1
88 dpavlin 1.1
89     EOF
90     }
91 dpavlin 1.4 print "target overview
92     mtargets = \"",join("; ",@targets),"\"
93 dpavlin 1.6 target-type = usage
94     y-min = 0
95     y-max = 100\n";
96 dpavlin 1.1 exit 1;
97     }
98    
99     # dump free %
100     #
101 dpavlin 1.7 print $df->{$mount} || "unknown";
102 dpavlin 1.1 print "\n";

  ViewVC Help
Powered by ViewVC 1.1.26