ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/i-scream/projects/cms/source/reports/rrdgraphing/graph.pl
Revision: 1.12
Committed: Mon Mar 10 11:34:43 2003 UTC (21 years, 2 months ago) by tdb
Content type: text/plain
Branch: MAIN
Changes since 1.11: +29 -11 lines
Log Message:
Update the graphing stuff to work with the new ihost data.

File Contents

# User Rev Content
1 tdb 1.1 #!/usr/bin/perl -w
2    
3 tdb 1.2 #
4     # i-scream central monitoring system
5 tdb 1.11 # http://www.i-scream.org.uk
6 tdb 1.2 # Copyright (C) 2000-2002 i-scream
7     #
8     # This program is free software; you can redistribute it and/or
9     # modify it under the terms of the GNU General Public License
10     # as published by the Free Software Foundation; either version 2
11     # of the License, or (at your option) any later version.
12     #
13     # This program is distributed in the hope that it will be useful,
14     # but WITHOUT ANY WARRANTY; without even the implied warranty of
15     # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16     # GNU General Public License for more details.
17     #
18     # You should have received a copy of the GNU General Public License
19     # along with this program; if not, write to the Free Software
20     # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21     #
22    
23 tdb 1.1 # -----------------------------------------------------------
24     # i-scream graph generation script
25     # http://www.i-scream.org.uk
26     #
27     # Generates graphs from rrd databases for i-scream data.
28     #
29     # $Author: tdb $
30 tdb 1.12 # $Id: graph.pl,v 1.11 2002/10/21 13:02:58 tdb Exp $
31 tdb 1.1 #------------------------------------------------------------
32    
33     ## TODO
34     # possibly make more configurable?
35     # -- allow configurable periods of graphs
36     # -- comments, types, etc
37 tdb 1.5
38 tdb 1.12 my($version) = '$Id: graph.pl,v 1.11 2002/10/21 13:02:58 tdb Exp $';
39 tdb 1.1
40     $| = 1;
41 tdb 1.5
42 tdb 1.1 use strict;
43 tdb 1.5 use Getopt::Std;
44 tdb 1.1 use RRDs;
45    
46 tdb 1.5 # define variables that will be read from the config
47     # nb. keep this insync with the config file!
48     use vars qw{
49     $imgdir $rrddir
50     $maxrrdage $maximgage $deleterrds $deleteimgs
51     $hex_slash $hex_underscore
52     $rrdstep $retry_wait
53     $verbose $quiet
54     };
55    
56     # default locate of the config file
57     my($configfile) = "rrdgraphing.conf";
58    
59     # check for command line arguments
60     my(%opts);
61     my($ret) = getopts('hvqVc:', \%opts);
62    
63     # if invalid argument given, $ret will not be 1
64     &usage() if $ret != 1;
65    
66     # first process the arguments which might mean we exit now
67    
68     # -h is usage
69     if($opts{h}) {
70     &usage();
71     }
72     # -V is version
73     if($opts{V}) {
74     print "graph.pl version: $version\n";
75     exit(1);
76     }
77    
78     # Then try getting the config
79    
80     # -c specifies the config file location
81     if($opts{c}) {
82     $configfile = $opts{c};
83     }
84     # suck in the config
85     &log("reading config from $configfile\n");
86     do $configfile;
87    
88     # Then any options we might want to override the config with
89    
90     # -v is verbose
91     if($opts{v}) {
92     $verbose = $opts{v};
93     }
94     # -q is verbose
95     if($opts{q}) {
96     $quiet = $opts{q};
97     # if we're meant to be quiet, we can hardly be verbose!
98     $verbose = 0;
99     }
100    
101 tdb 1.1 # Read the contents of the base directory
102     # and pull out the list of subdirectories (except . and .. :)
103     opendir(DIR, $rrddir);
104     my(@rrddirlist) = grep { -d "$rrddir/$_" && !/^\.$/ && !/^\.\.$/ } readdir(DIR);
105     closedir DIR;
106    
107     # look through each directoty, as they might
108     # contain rrds for a particular machine
109     foreach my $machine (@rrddirlist) {
110     # Read the contents of the directory
111     opendir(DIR, "$rrddir/$machine");
112     my(@rrdlist) = grep { /\.rrd$/ && -f "$rrddir/$machine/$_" } readdir(DIR);
113     closedir DIR;
114    
115     # See what rrd we have, and generate the graphs accordingly
116     foreach my $rrd (@rrdlist) {
117     chomp $rrd;
118 tdb 1.3 # stat the file
119     my($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,
120     $ctime,$blksize,$blocks) = stat("$rrddir/$machine/$rrd");
121     # check if it's old enough to be deleted
122     if((time - $mtime) > $maxrrdage) {
123 tdb 1.4 # do we delete the rrd, or just ignore it?
124     if($deleterrds) {
125     # if so, delete it
126     unlink("$rrddir/$machine/$rrd");
127 tdb 1.5 &log("deleted old rrd $rrddir/$machine/$rrd\n");
128     }
129     else {
130     &log("ignored old rrd $rrddir/$machine/$rrd\n");
131 tdb 1.4 }
132 tdb 1.3 # no more processing required for this rrd
133     next;
134     }
135 tdb 1.1 if($rrd =~ /^(cpu)\.rrd$/) {
136     my(@data);
137     my(@rawdata);
138 tdb 1.8 push @data, "LINE2:$1:idle:idle#00FF00:OK:idle cpu ";
139     push @data, "LINE2:$1:user:user#0000FF:OK:user cpu ";
140 tdb 1.7 push @data, "LINE2:$1:kernel:kernel#00FFFF:OK:kernel cpu";
141 tdb 1.8 push @data, "LINE2:$1:swap:swap#FF00FF:OK:swap cpu ";
142 tdb 1.7 push @data, "LINE2:$1:iowait:iowait#FF0000:OK:iowait cpu";
143 tdb 1.1 push @rawdata, "--upper-limit=100";
144 tdb 1.10 &makegraph($machine, $1, "CPU Usage for $machine", "% cpu time", \@data, \@rawdata);
145 tdb 1.1 }
146     if($rrd =~ /^(mem)\.rrd$/) {
147     my(@data);
148     my(@rawdata);
149 tdb 1.11 # we don't actually want to display free or total memory,
150     # although we need it to work out peruse...
151 tdb 1.7 push @data, "NONE:$1:free:free#CCCCFF:NONE:free memory";
152 tdb 1.11 push @data, "NONE:$1:total:total#0000FF:NONE:total memory\\n";
153     # calculate peruse - note that we only use 'free' if it's less than total
154     # (this is to avoid negative percentages :)
155     push @rawdata, "CDEF:peruse=total,free,total,LT,free,0,IF,-,total,/,100,*";
156 tdb 1.1 # and add it to the graph
157 tdb 1.11 push @rawdata, "AREA:peruse#CCCCFF:% memory in use";
158     push @rawdata, "--upper-limit=100";
159 tdb 1.12 push @rawdata, "--base=1024";
160 tdb 1.7 # add some nice values to the legend
161 tdb 1.11 &addlegend(\@rawdata, "peruse");
162     # put the total memory on the graph so we can map percentages to real values
163 tdb 1.12 push @rawdata, "GPRINT:total:LAST:Current total memory\\: \%.2lf %sb\\c";
164 tdb 1.11 &makegraph($machine, $1, "Memory Usage for $machine", "% memory in use", \@data, \@rawdata);
165 tdb 1.1 }
166     if($rrd =~ /^(load)\.rrd$/) {
167     my(@data);
168 tdb 1.8 push @data, "LINE2:$1:load1:load1#CCCCFF:OK: 1 min load average";
169     push @data, "LINE2:$1:load5:load5#7777FF:OK: 5 min load average";
170     push @data, "LINE2:$1:load15:load15#0000FF:OK:15 min load average";
171 tdb 1.10 &makegraph($machine, $1, "Loads for $machine", "load average", \@data);
172 tdb 1.1 }
173     if($rrd =~ /^(proc)\.rrd$/) {
174     my(@data);
175 tdb 1.8 push @data, "LINE2:$1:cpu:cpu#00FF00:OK:cpu processes ";
176 tdb 1.7 push @data, "LINE2:$1:sleeping:sleeping#0000FF:OK:sleeping processes";
177 tdb 1.8 push @data, "LINE2:$1:stopped:stopped#00FFFF:OK:stopped processes ";
178     push @data, "LINE2:$1:total:total#FF00FF:OK:total processes ";
179     push @data, "LINE2:$1:zombie:zombie#FF0000:OK:zombie processes ";
180 tdb 1.10 &makegraph($machine, $1, "Processes on $machine", "no. of processes", \@data);
181 tdb 1.1 }
182     if($rrd =~ /^(swap)\.rrd$/) {
183     my(@data);
184     my(@rawdata);
185 tdb 1.11 # we don't actually want to display free or total swap,
186     # although we need it to work out peruse...
187 tdb 1.7 push @data, "NONE:$1:free:free#CCCCFF:NONE:free swap";
188 tdb 1.11 push @data, "NONE:$1:total:total#0000FF:NONE:total swap\\n";
189     # calculate peruse - note that we only use 'free' if it's less than total
190     # (this is to avoid negative percentages :)
191     push @rawdata, "CDEF:peruse=total,free,total,LT,free,0,IF,-,total,/,100,*";
192 tdb 1.1 # and add it to the graph
193 tdb 1.11 push @rawdata, "AREA:peruse#CCCCFF:% swap in use";
194     push @rawdata, "--upper-limit=100";
195 tdb 1.12 push @rawdata, "--base=1024";
196 tdb 1.7 # add some nice values to the legend
197 tdb 1.11 &addlegend(\@rawdata, "peruse");
198     # put the total swap on the graph so we can map percentages to real values
199 tdb 1.12 push @rawdata, "GPRINT:total:LAST:Current total swap\\: \%.2lf %sb\\c";
200 tdb 1.11 &makegraph($machine, $1, "Swap Usage for $machine", "% swap in use", \@data, \@rawdata);
201 tdb 1.1 }
202     if($rrd =~ /^(users)\.rrd$/) {
203     my(@data);
204 tdb 1.7 push @data, "AREA:$1:count:count#CCCCFF:OK:user count";
205 tdb 1.10 &makegraph($machine, $1, "User Count for $machine", "no. of users", \@data);
206 tdb 1.1 }
207 tdb 1.11 if($rrd =~ /^(paging)\.rrd$/) {
208     my(@data);
209 tdb 1.12 push @data, "LINE2:$1:pageins:pageins#00FF00:OK:pages paged in ";
210     push @data, "LINE2:$1:pageouts:pageouts#0000FF:OK:pages paged out";
211 tdb 1.11 &makegraph($machine, $1, "Paging on $machine", "pages per second", \@data);
212     }
213 tdb 1.12 if($rrd =~ /^(disk)-(\S+)\.rrd$/) {
214 tdb 1.1 my(@data);
215     my(@rawdata);
216 tdb 1.11 # we need this lot for our calculations, but we'll never show them
217 tdb 1.12 push @data, "NONE:$1-$2:total:total#0000FF:NONE:total size\\n";
218 tdb 1.11 push @data, "NONE:$1-$2:used:used#CCCCFF:NONE:used space";
219     push @data, "NONE:$1-$2:totalinodes:totalinodes#000000:NONE:total inodes";
220     push @data, "NONE:$1-$2:freeinodes:freeinodes#000000:NONE:free inodes";
221     # calculate peruse, add it to the graph, and add a legend
222 tdb 1.12 push @rawdata, "CDEF:peruse=used,total,/,100,*";
223 tdb 1.11 push @rawdata, "AREA:peruse#CCCCFF:% disk used ";
224     &addlegend(\@rawdata, "peruse");
225     # put the total space on the graph so we can map percentages to real values
226 tdb 1.12 push @rawdata, "GPRINT:total:LAST:Current total space\\: \%.2lf %sb\\c";
227 tdb 1.11 # calculate perinodeuse, add it to the graph, and add a legend
228     push @rawdata, "CDEF:perinodeuse=totalinodes,freeinodes,totalinodes,LT,freeinodes,0,IF,-,totalinodes,/,100,*";
229     push @rawdata, "LINE2:perinodeuse#FF4444:% inodes used";
230     push @rawdata, "--upper-limit=100";
231 tdb 1.12 push @rawdata, "--base=1024";
232 tdb 1.11 &addlegend(\@rawdata, "perinodeuse");
233     # put the total inodes on the graph so we can map percentages to real values
234     push @rawdata, "GPRINT:totalinodes:LAST:Current total inodes\\: \%.2lf %s\\c";
235     # some name tidting
236 tdb 1.1 my($type) = $1;
237     my($name) = $2;
238     my($nicename) = $2;
239     $nicename =~ s/$hex_slash/\//g;
240     $nicename =~ s/$hex_underscore/_/g;
241 tdb 1.11 &makegraph($machine, "$type-$name", "Disk Usage for $machine on $nicename", "% usage", \@data, \@rawdata);
242 tdb 1.1 }
243 tdb 1.12 if($rrd =~ /^(diskio)-(\S+)\.rrd$/) {
244     my(@data);
245     my(@rawdata);
246     push @data, "LINE2:$1-$2:rbytes:rbytes#00FF00:OK:read bytes ";
247     push @data, "LINE2:$1-$2:wbytes:wbytes#0000FF:OK:write bytes";
248     push @rawdata, "--base=1024";
249     &makegraph($machine, "$1-$2", "Disk IO for $machine on $2", "bytes per second", \@data, \@rawdata);
250     }
251     if($rrd =~ /^(net)-(\S+)\.rrd$/) {
252     my(@data);
253     my(@rawdata);
254     push @data, "LINE2:$1-$2:rx:rx#00FF00:OK:received bytes ";
255     push @data, "LINE2:$1-$2:tx:tx#0000FF:OK:transfered bytes";
256     push @rawdata, "--base=1024";
257     &makegraph($machine, "$1-$2", "Network IO for $machine on $2", "bytes per second", \@data, \@rawdata);
258     }
259 tdb 1.1 # probably a queue with a name like this :)
260     if($rrd =~ /^(\d+)_0\.rrd$/) {
261     my(@data);
262     my(@rawdata);
263     my($baserrd) = $1;
264     my($i) = 0;
265     while( -f "$rrddir/$machine/$baserrd\_$i.rrd" ) {
266 tdb 1.7 push @data, "LINE2:$baserrd\_$i:size:size$i" . &get_colour($i) . ":OK:queue$i size ";
267 tdb 1.1 ++$i;
268     }
269 tdb 1.9 push @data, "LINE2:$baserrd\_0:total:total#FF0000:OK:packets/sec ";
270 tdb 1.1 my($comment);
271     if(-f "$rrddir/$machine/$baserrd.def") {
272     open(DEF, "$rrddir/$machine/$baserrd.def");
273     $comment = <DEF>;
274     chomp $comment if defined $comment;
275     }
276     $comment = "unknown queue" if not defined $comment;
277 tdb 1.10 &makegraph($machine, $baserrd, $comment, "", \@data, \@rawdata);
278 tdb 1.1 }
279 tdb 1.3 }
280     # have a last check, maybe we can remove the directory now?
281 tdb 1.4 # (only if we're deleting stuff)
282     if($deleterrds) {
283     # Read the contents of the directory
284     opendir(DIR, "$rrddir/$machine");
285     my(@dirlist) = grep { !/^\.$/ && !/^\.\.$/ } readdir(DIR);
286     closedir DIR;
287     if($#dirlist == -1) {
288     rmdir "$rrddir/$machine";
289 tdb 1.5 &log("deleting empty rrd directory $rrddir/$machine\n");
290 tdb 1.4 }
291 tdb 1.3 }
292     }
293    
294 tdb 1.4 if($deleteimgs) {
295     # Read the contents of the graphs directory
296     # and pull out the list of subdirectories (except . and .. :)
297     opendir(DIR, $imgdir);
298     my(@imgdirlist) = grep { -d "$imgdir/$_" && !/^\.$/ && !/^\.\.$/ } readdir(DIR);
299 tdb 1.3 closedir DIR;
300    
301 tdb 1.4 # look through each directoty, as they might
302     # contain images for a particular machine
303     foreach my $machine (@imgdirlist) {
304     # Read the contents of the directory
305     opendir(DIR, "$imgdir/$machine");
306     my(@imglist) = grep { /\.png$/ && -f "$imgdir/$machine/$_" } readdir(DIR);
307     closedir DIR;
308    
309     # See what rrd we have, and generate the graphs accordingly
310     foreach my $img (@imglist) {
311     chomp $img;
312     # stat the img
313     my($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,
314     $ctime,$blksize,$blocks) = stat("$imgdir/$machine/$img");
315     # check if it's old enough to be deleted
316     if((time - $mtime) > $maximgage) {
317     # if so, delete it
318     unlink("$imgdir/$machine/$img");
319 tdb 1.5 &log("deleted old image $imgdir/$machine/$img\n");
320 tdb 1.4 }
321     }
322     # have a last check, maybe we can remove the directory now?
323     # Read the contents of the directory
324     opendir(DIR, "$imgdir/$machine");
325     my(@dirlist) = grep { !/^\.$/ && !/^\.\.$/ } readdir(DIR);
326     closedir DIR;
327     if($#dirlist == -1) {
328     rmdir "$imgdir/$machine";
329 tdb 1.5 &log("deleted empty image directory $imgdir/$machine\n");
330 tdb 1.3 }
331     }
332 tdb 1.1 }
333 tdb 1.4
334     exit(0);
335    
336 tdb 1.1
337     #
338     # subroutine to make some graphs
339     #
340     # $machine = name of the machine
341     # (eg. kernow.ukc.ac.uk)
342     # $type = the type of graph for the machine
343     # (eg. cpu)
344     # $title = the title for the graph
345     # (eg. kernow CPU usage)
346 tdb 1.10 # $vlabel = the vertical label to apply to the left side of the graph
347     # (eg. kb/s)
348 tdb 1.1 # $dataref = a reference to an array containing information for the graph
349 tdb 1.7 # elements of format: "gtype:rrdname:dsname:name#colour:legend:comment with spaces"
350 tdb 1.1 # (if gtype is "NONE" only a DEF of 'name' will be defined, no line will be plotted)
351 tdb 1.7 # (if legend is "NONE" the latest/average/max/min legend won't be printed)
352 tdb 1.1 # $rawcmdref = a reference to an array containing raw rrd commands
353     # elements a single command each, no spaces
354     #
355    
356     sub makegraph() {
357 tdb 1.10 my($machine, $type, $title, $vlabel, $dataref, $rawcmdref) = @_;
358 tdb 1.1 # pass in these arrays by reference
359     my(@data) = @$dataref if defined $dataref;
360     my(@rawcmd) = @$rawcmdref if defined $rawcmdref;
361     # check if directory exists for images
362     if(! -d "$imgdir/$machine") {
363     # not sure on this umask, but it seems to work?
364     mkdir "$imgdir/$machine", 0777;
365 tdb 1.5 &log("created directory $imgdir/$machine\n");
366 tdb 1.1 }
367     my(@rrdcmd);
368     foreach my $dataitem (@data) {
369 tdb 1.7 # dataitem should be: "gtype:rrdname:dsname:name#colour:legend:comment with spaces"
370 tdb 1.1 # (if gtype is "NONE" only a DEF of 'name' will be defined, no line will be plotted)
371 tdb 1.7 # (if legend is "NONE" the latest/average/max/min legend won't be printed)
372     if($dataitem =~ /^(\S+):(\S+):(\S+):(\S+)#(.{6}):(\S+):(.*)$/) {
373 tdb 1.1 push @rrdcmd, "DEF:$4=$rrddir/$machine/$2.rrd:$3:AVERAGE";
374     if($1 ne "NONE") {
375 tdb 1.8 push @rrdcmd, "$1:$4#$5:$7";
376 tdb 1.7 if($6 ne "NONE") {
377     # add some nice values to the legend
378     &addlegend(\@rrdcmd, $4);
379     }
380 tdb 1.1 }
381     }
382     }
383     push @rrdcmd, "--title=$title";
384     push @rrdcmd, "--imgformat=PNG";
385     push @rrdcmd, "--lower-limit=0";
386 tdb 1.10 push @rrdcmd, "--vertical-label=$vlabel";
387 tdb 1.1 # not entirely convinced this is good...
388     push @rrdcmd, "--alt-autoscale-max";
389     # add any further raw commands
390     push @rrdcmd, @rawcmd;
391     RRDs::graph ("$imgdir/$machine/$type-3h.png", "--start=-10800", @rrdcmd);
392     my($err_3h) = RRDs::error;
393 tdb 1.5 &log("created $imgdir/$machine/$type-3h.png\n") unless $err_3h;
394     &error("Error generating 3h graph for $machine/$type: $err_3h\n") if $err_3h;
395 tdb 1.1 RRDs::graph ("$imgdir/$machine/$type-1d.png", "--start=-86400", @rrdcmd);
396     my($err_1d) = RRDs::error;
397 tdb 1.5 &log("created $imgdir/$machine/$type-1d.png\n") unless $err_1d;
398     &error("Error generating 1d graph for $machine/$type: $err_1d\n") if $err_1d;
399 tdb 1.1 RRDs::graph ("$imgdir/$machine/$type-1w.png", "--start=-604800", @rrdcmd);
400     my($err_1w) = RRDs::error;
401 tdb 1.5 &log("created $imgdir/$machine/$type-1w.png\n") unless $err_1w;
402     &error("Error generating 1w graph for $machine/$type: $err_1w\n") if $err_1w;
403 tdb 1.1 RRDs::graph ("$imgdir/$machine/$type-1m.png", "--start=-2678400", @rrdcmd);
404     my($err_1m) = RRDs::error;
405 tdb 1.5 &log("created $imgdir/$machine/$type-1m.png\n") unless $err_1m;
406     &error("Error generating 1m graph for $machine/$type: $err_1m\n") if $err_1m;
407 tdb 1.1 RRDs::graph ("$imgdir/$machine/$type-1y.png", "--start=-31536000", @rrdcmd);
408     my($err_1y) = RRDs::error;
409 tdb 1.5 &log("created $imgdir/$machine/$type-1y.png\n") unless $err_1y;
410     &error("Error generating 1y graph for $machine/$type: $err_1y\n") if $err_1y;
411 tdb 1.1 return;
412 tdb 1.7 }
413    
414     # subroutine to add a legend
415     # accepts reference to an array and a name
416     sub addlegend() {
417     my($dataref, $name) = @_;
418 tdb 1.8 push @$dataref, "GPRINT:$name:LAST:Current\\: \%8.2lf %s";
419     push @$dataref, "GPRINT:$name:AVERAGE:Average\\: \%8.2lf %s";
420     push @$dataref, "GPRINT:$name:MAX:Max\\: \%8.2lf %s\\n";
421 tdb 1.1 }
422    
423     # hacky subroutine to return a colour
424     # could be done much better somehow :/
425     sub get_colour {
426     my($col) = @_;
427     if($col == 0) {
428     return "#0000FF";
429     }
430     elsif($col == 1) {
431     return "#00FF00";
432     }
433     elsif($col == 2) {
434     return "#FF00FF";
435     }
436     elsif($col == 3) {
437     return "#FFFF00";
438     }
439     elsif($col == 4) {
440     return "#00FFFF";
441     }
442     else {
443     return "#000066";
444     }
445 tdb 1.5 }
446    
447     # prints out usage information then exits
448     sub usage() {
449     print "Usage: graph.pl [options]\n";
450     print "Options\n";
451     print " -c config Specifies the configuration file\n";
452     print " default: rrdgraphing.conf\n";
453     print " -v Be verbose about what's happening\n";
454     print " -q Be quiet, even supress errors\n";
455     print " -V Print version number\n";
456     print " -h Prints this help page\n";
457     exit(1);
458     }
459    
460     # prints a log message if verbose is turned on
461     sub log() {
462     my($msg) = @_;
463     print $msg if $verbose;
464     }
465    
466     # prints an error message unless quiet is turned on
467     sub error() {
468     my($msg) = @_;
469     print STDERR $msg unless $quiet;
470 tdb 1.1 }