/[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.7 - (hide annotations)
Thu Jul 18 10:23:24 2002 UTC (21 years, 8 months ago) by dpavlin
Branch: MAIN
Changes since 1.6: +22 -7 lines
File MIME type: text/plain
use Cache::FileCache if available (that way, we will connect to each host
only once and cache result)

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 dpavlin 1.7 # 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 dpavlin 1.1
33     sub parse_df {
34     my $ssh = shift @_;
35 dpavlin 1.7 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 dpavlin 1.1 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 dpavlin 1.3 # print STDERR "$_\n";
47 dpavlin 1.7 # $df->{$mount}=$use_pcnt;
48     $df->{$mount}=$used * 100 / $total if ($total != 0);
49     # print $df->{$mount}," == $use_pcnt\n";
50 dpavlin 1.1 }
51     }
52     close(DF);
53 dpavlin 1.7 $cache->set( $ssh, $df, "1 min" ) if ($USE_CACHE);
54 dpavlin 1.1 }
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 dpavlin 1.4 my @targets;
66    
67 dpavlin 1.7 foreach my $mnt (keys %{$df}) {
68 dpavlin 1.1 my $target = $mnt;
69     if ($mnt eq "/") {
70     $target = "root";
71     } else {
72     $target =~ s,^/,,;
73 dpavlin 1.3 $target =~ s,/,_,g;
74 dpavlin 1.1 }
75 dpavlin 1.4 push @targets,$target;
76 dpavlin 1.1 print <<"EOF";
77     target $target
78     target-type = usage
79     display-name = "$mnt"
80 dpavlin 1.5 skip-overview = 1
81 dpavlin 1.1
82     EOF
83     }
84 dpavlin 1.4 print "target overview
85     mtargets = \"",join("; ",@targets),"\"
86 dpavlin 1.6 target-type = usage
87     y-min = 0
88     y-max = 100\n";
89 dpavlin 1.1 exit 1;
90     }
91    
92     # dump free %
93     #
94 dpavlin 1.7 print $df->{$mount} || "unknown";
95 dpavlin 1.1 print "\n";

  ViewVC Help
Powered by ViewVC 1.1.26