ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/i-scream/projects/cms/source/reports/rrdgraphing/graph.pl
Revision: 1.8
Committed: Sun Oct 13 02:04:03 2002 UTC (21 years, 11 months ago) by tdb
Content type: text/plain
Branch: MAIN
Changes since 1.7: +23 -24 lines
Log Message:
Modified the layout of the text information on the graphs. Looks much
neater now I've figure out how to do number formating ;)

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