1 | #!/usr/bin/perl -w #-d
|
---|
2 | #
|
---|
3 | # iostat graphing tool based on rrdtool
|
---|
4 | # (c) 2004 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 | # So the script has been developped first with that context in mind
|
---|
10 | # trying to be generic enough to be expandable later on
|
---|
11 | # More work needs to be done, some code factorized, ...
|
---|
12 | #
|
---|
13 | use strict; # Always :-)
|
---|
14 | use English; # for $UID
|
---|
15 | use Data::Dumper; # for debug
|
---|
16 | use Time::Local; # for timestamp
|
---|
17 | use Date::Manip; # for timestamp
|
---|
18 | use File::Basename; # for file manageemnt
|
---|
19 | use Storable; # for data exchange
|
---|
20 | use Config::Auto; # for config file
|
---|
21 | use sario2rrd qw(create_attr create_gauge update_gauge);
|
---|
22 | # local common functions
|
---|
23 |
|
---|
24 | # Avoid localisation problems in sar
|
---|
25 | $ENV{LANG}="C";
|
---|
26 |
|
---|
27 | #
|
---|
28 | # Begin of configuration options
|
---|
29 | #
|
---|
30 | my $dir = "sar2rrd"; # Place under the html dir of user apache
|
---|
31 | # Under that directory you should have one
|
---|
32 | # directory per machine and in it all the
|
---|
33 | # files resulting form the sar and iostat
|
---|
34 | # commands (saxx and ioxx). Look at the
|
---|
35 | # cron file and related script
|
---|
36 |
|
---|
37 | my ($l,$p,$uid,$gid,$q,$c,$gc,$apache,$empty) = getpwnam("apache") or die "apache not in passwd file";
|
---|
38 | my $base = "$apache/html/$dir";
|
---|
39 | my $config = Config::Auto::parse("$base/$dir.conf");
|
---|
40 | my @machines = @{$config->{machines}};
|
---|
41 | my $shot = $config->{shot};
|
---|
42 |
|
---|
43 | #
|
---|
44 | # End of configuration options
|
---|
45 | #
|
---|
46 | my $debug = 0;
|
---|
47 | foreach my $a (@ARGV) {
|
---|
48 | $debug++ if ($a =~ /-v/);
|
---|
49 | $| = 1; # Flush STDOUT
|
---|
50 | }
|
---|
51 | #
|
---|
52 | my @day;
|
---|
53 | my %t;
|
---|
54 | my %attr;
|
---|
55 |
|
---|
56 | print Dumper($config) if ($debug > 1);
|
---|
57 | mkdir $base,0755 if (not (-d $base));
|
---|
58 | chown $uid, $gid, $base || warn "Unable to chown $base to $uid" if ($UID == 0);
|
---|
59 | #
|
---|
60 | # data struct: hash per machine and per timestamp pointing to sar
|
---|
61 | # and iostat values and a hash containing the attributes
|
---|
62 | #
|
---|
63 |
|
---|
64 | foreach my $m (@machines) {
|
---|
65 | # Clean the table
|
---|
66 | foreach my $ts (keys %t) {
|
---|
67 | delete $t{$ts};
|
---|
68 | }
|
---|
69 | $attr{$m}{nbfields} = 0;
|
---|
70 | #
|
---|
71 | # The iostat file has to be the result of iostat -x -d
|
---|
72 | # This stores the iostat file content in the %t datastructure
|
---|
73 | #
|
---|
74 | print "Getting iostat info for $m ( " if ($debug > 0);
|
---|
75 | foreach my $d (<$base/$m/io[0-9][0-9]>) {
|
---|
76 | print basename($d)." " if ($debug > 0);
|
---|
77 | my $tmst = "";
|
---|
78 | open(IOSTAT,"$d") || die "Unable to open $d";
|
---|
79 | while (<IOSTAT>) {
|
---|
80 | s/ +/ /g;
|
---|
81 | my ($dev,$rrqm,$wrqm,$r,$w,$rsec,$wsec,$rkb,$wkb,$rqsz,$qusz,$await,$svctm,$util) = split / /;
|
---|
82 | chomp($util) if defined($util);
|
---|
83 | #
|
---|
84 | # This line gives the day
|
---|
85 | #
|
---|
86 | if ($dev =~ /^Linux/) {
|
---|
87 | chomp($r);
|
---|
88 | $r =~ s/\t//g;
|
---|
89 | @day = split(/\//,$r);
|
---|
90 | print "Day:".Dumper(@day) if ($debug > 3);
|
---|
91 | next;
|
---|
92 | }
|
---|
93 | next if ($dev =~ /^Device:/);
|
---|
94 | next if ($dev =~ /^$/);
|
---|
95 | #
|
---|
96 | # This line gives the timestamp
|
---|
97 | #
|
---|
98 | if ($dev =~ /^Time:/) {
|
---|
99 | $tmst = timelocal(reverse(split(/:/,$rrqm)),$day[1],$day[0]-1,$day[2]+2000);
|
---|
100 | print "tmst: $tmst\n" if ($debug > 3);
|
---|
101 | next;
|
---|
102 | }
|
---|
103 | $dev =~ s|^/dev/||;
|
---|
104 | next if (($dev =~ /[0-9]$/) && ($dev !~ /cciss/));
|
---|
105 | next if (($dev =~ /p[0-9]$/) && ($dev =~ /cciss/));
|
---|
106 | $t{$tmst}{$dev."-in"} = $r;
|
---|
107 | $t{$tmst}{$dev."-out"} = $w;
|
---|
108 | }
|
---|
109 | close(IOSTAT);
|
---|
110 | }
|
---|
111 | print ")\n" if ($debug > 0);
|
---|
112 |
|
---|
113 | #
|
---|
114 | # Then create hash attributes + rrd base + update it
|
---|
115 | #
|
---|
116 | create_attr(\%t,\%attr,$m,$shot,$debug);
|
---|
117 | create_gauge(\%t,\%attr,$config->{step},"$base/$m/$config->{rrdio}",$uid,$gid,$m,$debug);
|
---|
118 | update_gauge(\%t,\%attr,"$base/$m/$config->{rrdio}",$m,$debug);
|
---|
119 | }
|
---|
120 | store (\%attr,"$base/$config->{svgio}") || die "Unable to store attr structure: $!";
|
---|
121 | print "Please launch sar2html now to get graphs in HTML pages\n" if ($debug > 0);
|
---|