source: MondoRescue/branches/stable/sar2rrd/sar2rrd.pl@ 1030

Last change on this file since 1030 was 1030, checked in by Bruno Cornec, 19 years ago

Creation of a pet project sar2rrd (put under subversion for ease of dev)
Not related to mondorescue

  • Property svn:executable set to *
File size: 3.4 KB
Line 
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#
14use strict; # Always :-)
15use English; # for $UID
16use Data::Dumper; # for debug
17use Time::Local; # for timestamp
18use Date::Manip; # for timestamp
19use File::Basename; # for file manageemnt
20use Storable; # for data exchange
21use Config::Auto; # for config file
22use 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#
31my $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
38my ($l,$p,$uid,$gid,$q,$c,$gc,$apache,$empty) = getpwnam("apache") or die "apache not in passwd file";
39my $base = "$apache/html/$dir";
40my $config = Config::Auto::parse("$base/$dir.conf");
41my @machines = @{$config->{machines}};
42my $shot = $config->{shot};
43
44my $debug = 0;
45foreach my $a (@ARGV) {
46 $debug++ if ($a =~ /-v/);
47 $| = 1; # Flush STDOUT
48}
49
50#
51my @day;
52my %t;
53my %attr;
54
55print Dumper($config) if ($debug > 1);
56mkdir $base,0755 if (not (-d $base));
57chown $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
63foreach 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}
104store (\%attr,"$base/$config->{svgsar}") || die "Unable to store attr structure: $!";
105print "Please launch sar2html now to get graphs in HTML pages\n" if ($debug > 0);
Note: See TracBrowser for help on using the repository browser.