ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/i-scream/projects/cms/source/reports/rrdgraphing/graph.pl
Revision: 1.6
Committed: Tue May 21 16:47:16 2002 UTC (22 years, 7 months ago) by tdb
Content type: text/plain
Branch: MAIN
Changes since 1.5: +3 -2 lines
Log Message:
Added URL to GPL headers.

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