ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/i-scream/projects/cms/source/reports/rrdgraphing/graph.pl
Revision: 1.5
Committed: Tue May 21 15:01:43 2002 UTC (22 years, 4 months ago) by tdb
Content type: text/plain
Branch: MAIN
Changes since 1.4: +104 -34 lines
Log Message:
Changed to have a shared config file. Also parses command line options to
make it more verbose or quiet.

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.5 # $Id: graph.pl,v 1.4 2002/05/21 11:37:56 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     my($version) = '$Id$';
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    
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     push @data, "LINE2:$1:idle:idle#00FF00:idle cpu";
139     push @data, "LINE2:$1:user:user#0000FF:user cpu";
140     push @data, "LINE2:$1:kernel:kernel#00FFFF:kernel cpu";
141     push @data, "LINE2:$1:swap:swap#FF00FF:swap cpu";
142     push @data, "LINE2:$1:iowait:iowait#FF0000:iowait cpu";
143     push @rawdata, "--upper-limit=100";
144     &makegraph($machine, $1, "CPU Usage for $machine", \@data, \@rawdata);
145     }
146     if($rrd =~ /^(mem)\.rrd$/) {
147     my(@data);
148     my(@rawdata);
149     # we don't actually want to display free memory,
150     # although we need it to do inuse...
151     push @data, "NONE:$1:free:free#CCCCFF:free memory";
152     push @data, "LINE2:$1:total:total#0000FF:total memory";
153     # calculate inuse
154     push @rawdata, "CDEF:inuse=total,free,-";
155     # and add it to the graph
156     push @rawdata, "AREA:inuse#CCCCFF:memory in use";
157     push @rawdata, "--base=1024";
158     &makegraph($machine, $1, "Memory Usage for $machine", \@data, \@rawdata);
159     }
160     if($rrd =~ /^(load)\.rrd$/) {
161     my(@data);
162     push @data, "LINE2:$1:load1:load1#CCCCFF:1 minute load average";
163     push @data, "LINE2:$1:load5:load5#7777FF:5 minute load average";
164     push @data, "LINE2:$1:load15:load15#0000FF:15 minute load average";
165     &makegraph($machine, $1, "Loads for $machine", \@data);
166     }
167     if($rrd =~ /^(proc)\.rrd$/) {
168     my(@data);
169     push @data, "LINE2:$1:cpu:cpu#00FF00:cpu processes";
170     push @data, "LINE2:$1:sleeping:sleeping#0000FF:sleeping processes";
171     push @data, "LINE2:$1:stopped:stopped#00FFFF:stopped processes";
172     push @data, "LINE2:$1:total:total#FF00FF:total processes";
173     push @data, "LINE2:$1:zombie:zombie#FF0000:zombie processes";
174     &makegraph($machine, $1, "Processes on $machine", \@data);
175     }
176     if($rrd =~ /^(swap)\.rrd$/) {
177     my(@data);
178     my(@rawdata);
179     # we don't actually want to display free swap,
180     # although we need it to do inuse...
181     push @data, "NONE:$1:free:free#CCCCFF:free swap";
182     push @data, "LINE2:$1:total:total#0000FF:total swap";
183     # calculate inuse
184     push @rawdata, "CDEF:inuse=total,free,-";
185     # and add it to the graph
186     push @rawdata, "AREA:inuse#CCCCFF:swap in use";
187     push @rawdata, "--base=1024";
188     &makegraph($machine, $1, "Swap Usage for $machine", \@data, \@rawdata);
189     }
190     if($rrd =~ /^(users)\.rrd$/) {
191     my(@data);
192     push @data, "AREA:$1:count:count#CCCCFF:user count";
193     &makegraph($machine, $1, "User Count for $machine", \@data);
194     }
195     if($rrd =~ /^(disk)-(\S+).rrd$/) {
196     my(@data);
197     my(@rawdata);
198     push @data, "LINE2:$1-$2:kbytes:kbytes#0000FF:total size";
199     push @data, "AREA:$1-$2:used:used#CCCCFF:used";
200     push @rawdata, "--base=1024";
201     my($type) = $1;
202     my($name) = $2;
203     my($nicename) = $2;
204     $nicename =~ s/$hex_slash/\//g;
205     $nicename =~ s/$hex_underscore/_/g;
206     &makegraph($machine, "$type-$name", "Disk Usage for $machine on $nicename", \@data, \@rawdata);
207     }
208     # probably a queue with a name like this :)
209     if($rrd =~ /^(\d+)_0\.rrd$/) {
210     my(@data);
211     my(@rawdata);
212     my($baserrd) = $1;
213     my($i) = 0;
214     while( -f "$rrddir/$machine/$baserrd\_$i.rrd" ) {
215     push @data, "LINE2:$baserrd\_$i:size:size$i" . &get_colour($i) . ":queue$i size ";
216     ++$i;
217     }
218     push @data, "LINE2:$baserrd\_0:total:total#FF0000:packets/sec - currently";
219     push @rawdata, "GPRINT:total:LAST:%lf %spackets/sec";
220     my($comment);
221     if(-f "$rrddir/$machine/$baserrd.def") {
222     open(DEF, "$rrddir/$machine/$baserrd.def");
223     $comment = <DEF>;
224     chomp $comment if defined $comment;
225     }
226     $comment = "unknown queue" if not defined $comment;
227     &makegraph($machine, $baserrd, $comment, \@data, \@rawdata);
228     }
229 tdb 1.3 }
230     # have a last check, maybe we can remove the directory now?
231 tdb 1.4 # (only if we're deleting stuff)
232     if($deleterrds) {
233     # Read the contents of the directory
234     opendir(DIR, "$rrddir/$machine");
235     my(@dirlist) = grep { !/^\.$/ && !/^\.\.$/ } readdir(DIR);
236     closedir DIR;
237     if($#dirlist == -1) {
238     rmdir "$rrddir/$machine";
239 tdb 1.5 &log("deleting empty rrd directory $rrddir/$machine\n");
240 tdb 1.4 }
241 tdb 1.3 }
242     }
243    
244 tdb 1.4 if($deleteimgs) {
245     # Read the contents of the graphs directory
246     # and pull out the list of subdirectories (except . and .. :)
247     opendir(DIR, $imgdir);
248     my(@imgdirlist) = grep { -d "$imgdir/$_" && !/^\.$/ && !/^\.\.$/ } readdir(DIR);
249 tdb 1.3 closedir DIR;
250    
251 tdb 1.4 # look through each directoty, as they might
252     # contain images for a particular machine
253     foreach my $machine (@imgdirlist) {
254     # Read the contents of the directory
255     opendir(DIR, "$imgdir/$machine");
256     my(@imglist) = grep { /\.png$/ && -f "$imgdir/$machine/$_" } readdir(DIR);
257     closedir DIR;
258    
259     # See what rrd we have, and generate the graphs accordingly
260     foreach my $img (@imglist) {
261     chomp $img;
262     # stat the img
263     my($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,
264     $ctime,$blksize,$blocks) = stat("$imgdir/$machine/$img");
265     # check if it's old enough to be deleted
266     if((time - $mtime) > $maximgage) {
267     # if so, delete it
268     unlink("$imgdir/$machine/$img");
269 tdb 1.5 &log("deleted old image $imgdir/$machine/$img\n");
270 tdb 1.4 }
271     }
272     # have a last check, maybe we can remove the directory now?
273     # Read the contents of the directory
274     opendir(DIR, "$imgdir/$machine");
275     my(@dirlist) = grep { !/^\.$/ && !/^\.\.$/ } readdir(DIR);
276     closedir DIR;
277     if($#dirlist == -1) {
278     rmdir "$imgdir/$machine";
279 tdb 1.5 &log("deleted empty image directory $imgdir/$machine\n");
280 tdb 1.3 }
281     }
282 tdb 1.1 }
283 tdb 1.4
284     exit(0);
285    
286 tdb 1.1
287     #
288     # subroutine to make some graphs
289     #
290     # $machine = name of the machine
291     # (eg. kernow.ukc.ac.uk)
292     # $type = the type of graph for the machine
293     # (eg. cpu)
294     # $title = the title for the graph
295     # (eg. kernow CPU usage)
296     # $dataref = a reference to an array containing information for the graph
297     # elements of format: "gtype:rrdname:dsname:name#colour:comment with spaces"
298     # (if gtype is "NONE" only a DEF of 'name' will be defined, no line will be plotted)
299     # $rawcmdref = a reference to an array containing raw rrd commands
300     # elements a single command each, no spaces
301     #
302    
303     sub makegraph() {
304     my($machine, $type, $title, $dataref, $rawcmdref) = @_;
305     # pass in these arrays by reference
306     my(@data) = @$dataref if defined $dataref;
307     my(@rawcmd) = @$rawcmdref if defined $rawcmdref;
308     # check if directory exists for images
309     if(! -d "$imgdir/$machine") {
310     # not sure on this umask, but it seems to work?
311     mkdir "$imgdir/$machine", 0777;
312 tdb 1.5 &log("created directory $imgdir/$machine\n");
313 tdb 1.1 }
314     my(@rrdcmd);
315     foreach my $dataitem (@data) {
316     # dataitem should be: "gtype:rrdname:dsname:name#colour:comment with spaces"
317     # (if gtype is "NONE" only a DEF of 'name' will be defined, no line will be plotted)
318     if($dataitem =~ /^(\S+):(\S+):(\S+):(\S+)#(.{6}):(.*)$/) {
319     push @rrdcmd, "DEF:$4=$rrddir/$machine/$2.rrd:$3:AVERAGE";
320     if($1 ne "NONE") {
321     push @rrdcmd, "$1:$4#$5:$6";
322     }
323     }
324     }
325     push @rrdcmd, "--title=$title";
326     push @rrdcmd, "--imgformat=PNG";
327     push @rrdcmd, "--lower-limit=0";
328     # not entirely convinced this is good...
329     push @rrdcmd, "--alt-autoscale-max";
330     # add any further raw commands
331     push @rrdcmd, @rawcmd;
332     RRDs::graph ("$imgdir/$machine/$type-3h.png", "--start=-10800", @rrdcmd);
333     my($err_3h) = RRDs::error;
334 tdb 1.5 &log("created $imgdir/$machine/$type-3h.png\n") unless $err_3h;
335     &error("Error generating 3h graph for $machine/$type: $err_3h\n") if $err_3h;
336 tdb 1.1 RRDs::graph ("$imgdir/$machine/$type-1d.png", "--start=-86400", @rrdcmd);
337     my($err_1d) = RRDs::error;
338 tdb 1.5 &log("created $imgdir/$machine/$type-1d.png\n") unless $err_1d;
339     &error("Error generating 1d graph for $machine/$type: $err_1d\n") if $err_1d;
340 tdb 1.1 RRDs::graph ("$imgdir/$machine/$type-1w.png", "--start=-604800", @rrdcmd);
341     my($err_1w) = RRDs::error;
342 tdb 1.5 &log("created $imgdir/$machine/$type-1w.png\n") unless $err_1w;
343     &error("Error generating 1w graph for $machine/$type: $err_1w\n") if $err_1w;
344 tdb 1.1 RRDs::graph ("$imgdir/$machine/$type-1m.png", "--start=-2678400", @rrdcmd);
345     my($err_1m) = RRDs::error;
346 tdb 1.5 &log("created $imgdir/$machine/$type-1m.png\n") unless $err_1m;
347     &error("Error generating 1m graph for $machine/$type: $err_1m\n") if $err_1m;
348 tdb 1.1 RRDs::graph ("$imgdir/$machine/$type-1y.png", "--start=-31536000", @rrdcmd);
349     my($err_1y) = RRDs::error;
350 tdb 1.5 &log("created $imgdir/$machine/$type-1y.png\n") unless $err_1y;
351     &error("Error generating 1y graph for $machine/$type: $err_1y\n") if $err_1y;
352 tdb 1.1 return;
353     }
354    
355     # hacky subroutine to return a colour
356     # could be done much better somehow :/
357     sub get_colour {
358     my($col) = @_;
359     if($col == 0) {
360     return "#0000FF";
361     }
362     elsif($col == 1) {
363     return "#00FF00";
364     }
365     elsif($col == 2) {
366     return "#FF00FF";
367     }
368     elsif($col == 3) {
369     return "#FFFF00";
370     }
371     elsif($col == 4) {
372     return "#00FFFF";
373     }
374     else {
375     return "#000066";
376     }
377 tdb 1.5 }
378    
379     # prints out usage information then exits
380     sub usage() {
381     print "Usage: graph.pl [options]\n";
382     print "Options\n";
383     print " -c config Specifies the configuration file\n";
384     print " default: rrdgraphing.conf\n";
385     print " -v Be verbose about what's happening\n";
386     print " -q Be quiet, even supress errors\n";
387     print " -V Print version number\n";
388     print " -h Prints this help page\n";
389     exit(1);
390     }
391    
392     # prints a log message if verbose is turned on
393     sub log() {
394     my($msg) = @_;
395     print $msg if $verbose;
396     }
397    
398     # prints an error message unless quiet is turned on
399     sub error() {
400     my($msg) = @_;
401     print STDERR $msg unless $quiet;
402 tdb 1.1 }