1 | #!/usr/bin/perl -w #-d
|
---|
2 | #
|
---|
3 | # sar graphing tool based on rrdtool
|
---|
4 | # (c) 2004-2007 B. Cornec HP <bruno.cornec@hp.com>
|
---|
5 | # Licensed under the GPL or Artistic License
|
---|
6 | #
|
---|
7 | # Original Charter: Monitor CPU/Load/Net_eth0|2 (io/s)/Disk (io/s)
|
---|
8 | # 1 mesure par 20 seconde
|
---|
9 | # Absolute time in minutes since Epoch
|
---|
10 | # So the script has been developped first with that context in mind
|
---|
11 | # trying to be generic enough to be expandable later on
|
---|
12 | # More work needs to be done, some code factorized, ...
|
---|
13 | #
|
---|
14 | use strict; # Always :-)
|
---|
15 | use English; # for $UID
|
---|
16 | use Data::Dumper; # for debug
|
---|
17 | use Time::Local; # for timestamp
|
---|
18 | use Date::Manip; # for timestamp
|
---|
19 | use File::Basename; # for file manageemnt
|
---|
20 | use Storable; # for data exchange
|
---|
21 | use Config::Auto; # for config file
|
---|
22 | use sario2rrd qw(create_attr create_gauge update_gauge);
|
---|
23 | # local common functions
|
---|
24 |
|
---|
25 | # Avoid localisation problems in sar
|
---|
26 | $ENV{LANG}="C";
|
---|
27 |
|
---|
28 | #
|
---|
29 | # Begin of configuration options
|
---|
30 | #
|
---|
31 | my $dir = "sar2rrd"; # Place under the html dir of user apache
|
---|
32 | # Under that directory you should have one
|
---|
33 | # directory per machine and in it all the
|
---|
34 | # files resulting form the sar and iostat
|
---|
35 | # commands (saxx and ioxx). Look at the
|
---|
36 | # cron file and related script
|
---|
37 |
|
---|
38 | my ($l,$p,$uid,$gid,$q,$c,$gc,$apache,$empty) = getpwnam("apache") or die "apache not in passwd file";
|
---|
39 | my $base = "$apache/html/$dir";
|
---|
40 | my $config = Config::Auto::parse("$base/$dir.conf");
|
---|
41 | my @machines = @{$config->{machines}};
|
---|
42 | my $shot = $config->{shot};
|
---|
43 |
|
---|
44 | my $debug = 0;
|
---|
45 | foreach my $a (@ARGV) {
|
---|
46 | $debug++ if ($a =~ /-v/);
|
---|
47 | $| = 1; # Flush STDOUT
|
---|
48 | }
|
---|
49 |
|
---|
50 | #
|
---|
51 | my @day;
|
---|
52 | my %t;
|
---|
53 | my %attr;
|
---|
54 |
|
---|
55 | print Dumper($config) if ($debug > 1);
|
---|
56 | mkdir $base,0755 if (not (-d $base));
|
---|
57 | chown $uid, $gid, $base || warn "Unable to chown $base to $uid" if ($UID == 0);
|
---|
58 | #
|
---|
59 | # data struct: hash per machine and per timestamp pointing to sar
|
---|
60 | # and iostat values and a hash containing the attributes
|
---|
61 | #
|
---|
62 |
|
---|
63 | foreach my $m (@machines) {
|
---|
64 | # Clean the table
|
---|
65 | foreach my $ts (keys %t) {
|
---|
66 | delete $t{$ts};
|
---|
67 | }
|
---|
68 | $attr{$m}{nbfields} = 0;
|
---|
69 | #
|
---|
70 | # The sar file has to be the result of sar -A -h
|
---|
71 | # This stores the sar file content in the %t datastructure
|
---|
72 | #
|
---|
73 | print "Getting sar info for $m ( " if ($debug > 0);
|
---|
74 | foreach my $d (<$base/$m/sarh[0-9][0-9]>) {
|
---|
75 | print basename($d)." " if ($debug > 0);
|
---|
76 | open(SAR,$d) || die "Unable to open $d";
|
---|
77 | while (<SAR>) {
|
---|
78 | my ($name,$int,$tmst,$f1,$f2,$f3) = split /\t/;
|
---|
79 | chomp($f3);
|
---|
80 | $t{$tmst}{memoryfree} = $f3 if ($f2 =~ /kbmemfree/);
|
---|
81 | $t{$tmst}{${f1}."-in"} = $f3 if ($f2 =~ /rxpck/);
|
---|
82 | $t{$tmst}{${f1}."-out"} = $f3 if ($f2 =~ /txpck/);
|
---|
83 | $t{$tmst}{runq} = $f3 if ($f2 =~ /runq-sz/);
|
---|
84 | $t{$tmst}{plist} = $f3 if ($f2 =~ /plist-sz/);
|
---|
85 | $t{$tmst}{load1} = $f3 if ($f2 =~ /ldavg-1/);
|
---|
86 | $t{$tmst}{load5} = $f3 if ($f2 =~ /ldavg-5/);
|
---|
87 | $t{$tmst}{${f1}."-user"} = $f3 if ($f2 =~ /user/);
|
---|
88 | $t{$tmst}{${f1}."-nice"} = $f3 if ($f2 =~ /nice/);
|
---|
89 | $t{$tmst}{${f1}."-system"} = $f3 if ($f2 =~ /system/);
|
---|
90 | $t{$tmst}{${f1}."-idle"} = $f3 if ($f2 =~ /idle/);
|
---|
91 | #$t{$tmst}{${f1}} = $f3 if (($f2 =~ /tps/) && ($f1 =~ /dev/));
|
---|
92 | }
|
---|
93 | close(SAR);
|
---|
94 | }
|
---|
95 | print ")\n" if ($debug > 0);
|
---|
96 | print Dumper(%t) if ($debug > 1);
|
---|
97 | #
|
---|
98 | # Then create hash attributes + rrd base + update it
|
---|
99 | #
|
---|
100 | create_attr(\%t,\%attr,$m,$shot,$debug);
|
---|
101 | create_gauge(\%t,\%attr,$config->{step},"$base/$m/$config->{rrdsar}",$uid,$gid,$m,$debug);
|
---|
102 | update_gauge(\%t,\%attr,"$base/$m/$config->{rrdsar}",$m,$debug);
|
---|
103 | }
|
---|
104 | store (\%attr,"$base/$config->{svgsar}") || die "Unable to store attr structure: $!";
|
---|
105 | print "Please launch sar2html now to get graphs in HTML pages\n" if ($debug > 0);
|
---|