ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/i-scream/projects/cms/source/reports/rrdgraphing/graph.pl
(Generate patch)

Comparing projects/cms/source/reports/rrdgraphing/graph.pl (file contents):
Revision 1.4 by tdb, Tue May 21 11:37:56 2002 UTC vs.
Revision 1.12 by tdb, Mon Mar 10 11:34:43 2003 UTC

# Line 2 | Line 2
2  
3   #
4   # i-scream central monitoring system
5 + # http://www.i-scream.org.uk
6   # Copyright (C) 2000-2002 i-scream
7   #
8   # This program is free software; you can redistribute it and/or
# Line 33 | Line 34
34   # possibly make more configurable?
35   #  -- allow configurable periods of graphs
36   #  -- comments, types, etc
36 #  -- move all to external config file
37  
38 + my($version) = '$Id$';
39 +
40   $| = 1;
41 +
42   use strict;
43 + use Getopt::Std;
44   use RRDs;
45  
46 < # Base directory for images
47 < # (a directory will be constructed for each host under this)
48 < my($imgdir) = "/home/pkg/iscream/public_html/graphs";
46 > # 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 < # Location of RRD databases
57 < my($rrddir) = "/u1/i-scream/databases";
56 > # default locate of the config file
57 > my($configfile) = "rrdgraphing.conf";
58  
59 < # / converted to a decimal then hex'd
60 < my($hex_slash) = "_2f";
61 < # _ converted to a decimal then hex'd
52 < my($hex_underscore) = "_5f";
59 > # check for command line arguments
60 > my(%opts);
61 > my($ret) = getopts('hvqVc:', \%opts);
62  
63 < # maximum age (last modified) before an rrd or graph get cleaned up
64 < # (in seconds)
56 < my($maxrrdage) = 3600; # 1 hour
57 < my($maximgage) = 3600; # 1 hour
63 > # if invalid argument given, $ret will not be 1
64 > &usage() if $ret != 1;
65  
66 < # delete rrd's when they get cleaned up?
60 < # if unset, will just ignore the rrd's
61 < #  - usually best to leave this off, we don't want to delete useful rrds :)
62 < my($deleterrds) = 0;
66 > # first process the arguments which might mean we exit now
67  
68 < # delete graphs when they get cleaned up?
69 < # if unset, won't bother checking at all
70 < #  - usually best to leave this on
71 < my($deleteimgs) = 1;
72 <    
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   # Read the contents of the base directory
102   # and pull out the list of subdirectories (except . and .. :)
103   opendir(DIR, $rrddir);
# Line 92 | Line 124 | foreach my $machine (@rrddirlist) {
124              if($deleterrds) {
125                  # if so, delete it
126                  unlink("$rrddir/$machine/$rrd");
127 +                &log("deleted old rrd $rrddir/$machine/$rrd\n");
128              }
129 +            else {
130 +                &log("ignored old rrd $rrddir/$machine/$rrd\n");
131 +            }
132              # no more processing required for this rrd
133              next;
134          }
135          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";
138 >            push @data, "LINE2:$1:idle:idle#00FF00:OK:idle cpu  ";
139 >            push @data, "LINE2:$1:user:user#0000FF:OK:user cpu  ";
140 >            push @data, "LINE2:$1:kernel:kernel#00FFFF:OK:kernel cpu";
141 >            push @data, "LINE2:$1:swap:swap#FF00FF:OK:swap cpu  ";
142 >            push @data, "LINE2:$1:iowait:iowait#FF0000:OK:iowait cpu";
143              push @rawdata, "--upper-limit=100";
144 <            &makegraph($machine, $1, "CPU Usage for $machine", \@data, \@rawdata);
144 >            &makegraph($machine, $1, "CPU Usage for $machine", "% cpu time", \@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,-";
149 >            # we don't actually want to display free or total memory,
150 >            # although we need it to work out peruse...
151 >            push @data, "NONE:$1:free:free#CCCCFF:NONE:free memory";
152 >            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              # and add it to the graph
157 <            push @rawdata, "AREA:inuse#CCCCFF:memory in use";
157 >            push @rawdata, "AREA:peruse#CCCCFF:% memory in use";
158 >            push @rawdata, "--upper-limit=100";
159              push @rawdata, "--base=1024";
160 <            &makegraph($machine, $1, "Memory Usage for $machine", \@data, \@rawdata);
160 >            # add some nice values to the legend
161 >            &addlegend(\@rawdata, "peruse");
162 >            # put the total memory on the graph so we can map percentages to real values
163 >            push @rawdata, "GPRINT:total:LAST:Current total memory\\: \%.2lf %sb\\c";
164 >            &makegraph($machine, $1, "Memory Usage for $machine", "% memory in use", \@data, \@rawdata);
165          }
166          if($rrd =~ /^(load)\.rrd$/) {
167              my(@data);
168 <            push @data, "LINE2:$1:load1:load1#CCCCFF:1 minute load average";
169 <            push @data, "LINE2:$1:load5:load5#7777FF:5 minute load average";
170 <            push @data, "LINE2:$1:load15:load15#0000FF:15 minute load average";
171 <            &makegraph($machine, $1, "Loads for $machine", \@data);
168 >            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 >            &makegraph($machine, $1, "Loads for $machine", "load average", \@data);
172          }
173          if($rrd =~ /^(proc)\.rrd$/) {
174              my(@data);
175 <            push @data, "LINE2:$1:cpu:cpu#00FF00:cpu processes";
176 <            push @data, "LINE2:$1:sleeping:sleeping#0000FF:sleeping processes";
177 <            push @data, "LINE2:$1:stopped:stopped#00FFFF:stopped processes";
178 <            push @data, "LINE2:$1:total:total#FF00FF:total processes";
179 <            push @data, "LINE2:$1:zombie:zombie#FF0000:zombie processes";
180 <            &makegraph($machine, $1, "Processes on $machine", \@data);
175 >            push @data, "LINE2:$1:cpu:cpu#00FF00:OK:cpu processes     ";
176 >            push @data, "LINE2:$1:sleeping:sleeping#0000FF:OK:sleeping processes";
177 >            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 >            &makegraph($machine, $1, "Processes on $machine", "no. of processes", \@data);
181          }
182          if($rrd =~ /^(swap)\.rrd$/) {
183              my(@data);
184              my(@rawdata);
185 <            # we don't actually want to display free swap,
186 <            # although we need it to do inuse...
187 <            push @data, "NONE:$1:free:free#CCCCFF:free swap";
188 <            push @data, "LINE2:$1:total:total#0000FF:total swap";
189 <            # calculate inuse
190 <            push @rawdata, "CDEF:inuse=total,free,-";
185 >            # we don't actually want to display free or total swap,
186 >            # although we need it to work out peruse...
187 >            push @data, "NONE:$1:free:free#CCCCFF:NONE:free swap";
188 >            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              # and add it to the graph
193 <            push @rawdata, "AREA:inuse#CCCCFF:swap in use";
193 >            push @rawdata, "AREA:peruse#CCCCFF:% swap in use";
194 >            push @rawdata, "--upper-limit=100";
195              push @rawdata, "--base=1024";
196 <            &makegraph($machine, $1, "Swap Usage for $machine", \@data, \@rawdata);
196 >            # add some nice values to the legend
197 >            &addlegend(\@rawdata, "peruse");
198 >            # put the total swap on the graph so we can map percentages to real values
199 >            push @rawdata, "GPRINT:total:LAST:Current total swap\\: \%.2lf %sb\\c";
200 >            &makegraph($machine, $1, "Swap Usage for $machine", "% swap in use", \@data, \@rawdata);
201          }
202          if($rrd =~ /^(users)\.rrd$/) {
203              my(@data);
204 <            push @data, "AREA:$1:count:count#CCCCFF:user count";
205 <            &makegraph($machine, $1, "User Count for $machine", \@data);
204 >            push @data, "AREA:$1:count:count#CCCCFF:OK:user count";
205 >            &makegraph($machine, $1, "User Count for $machine", "no. of users", \@data);
206          }
207 <        if($rrd =~ /^(disk)-(\S+).rrd$/) {
207 >        if($rrd =~ /^(paging)\.rrd$/) {
208              my(@data);
209 +            push @data, "LINE2:$1:pageins:pageins#00FF00:OK:pages paged in ";
210 +            push @data, "LINE2:$1:pageouts:pageouts#0000FF:OK:pages paged out";
211 +            &makegraph($machine, $1, "Paging on $machine", "pages per second", \@data);
212 +        }
213 +        if($rrd =~ /^(disk)-(\S+)\.rrd$/) {
214 +            my(@data);
215              my(@rawdata);
216 <            push @data, "LINE2:$1-$2:kbytes:kbytes#0000FF:total size";
217 <            push @data, "AREA:$1-$2:used:used#CCCCFF:used";
216 >            # we need this lot for our calculations, but we'll never show them
217 >            push @data, "NONE:$1-$2:total:total#0000FF:NONE:total size\\n";
218 >            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 >            push @rawdata, "CDEF:peruse=used,total,/,100,*";
223 >            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 >            push @rawdata, "GPRINT:total:LAST:Current total space\\: \%.2lf %sb\\c";
227 >            # 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              push @rawdata, "--base=1024";
232 +            &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              my($type) = $1;
237              my($name) = $2;
238              my($nicename) = $2;
239              $nicename =~ s/$hex_slash/\//g;
240              $nicename =~ s/$hex_underscore/_/g;
241 <            &makegraph($machine, "$type-$name", "Disk Usage for $machine on $nicename", \@data, \@rawdata);
241 >            &makegraph($machine, "$type-$name", "Disk Usage for $machine on $nicename", "% usage", \@data, \@rawdata);
242          }
243 +        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          # probably a queue with a name like this :)
260          if($rrd =~ /^(\d+)_0\.rrd$/) {
261              my(@data);
# Line 176 | Line 263 | foreach my $machine (@rrddirlist) {
263              my($baserrd) = $1;
264              my($i) = 0;
265              while( -f "$rrddir/$machine/$baserrd\_$i.rrd" ) {
266 <                push @data, "LINE2:$baserrd\_$i:size:size$i" . &get_colour($i) . ":queue$i size ";
266 >                push @data, "LINE2:$baserrd\_$i:size:size$i" . &get_colour($i) . ":OK:queue$i size ";
267                  ++$i;
268              }
269 <            push @data, "LINE2:$baserrd\_0:total:total#FF0000:packets/sec - currently";
183 <            push @rawdata, "GPRINT:total:LAST:%lf %spackets/sec";
269 >            push @data, "LINE2:$baserrd\_0:total:total#FF0000:OK:packets/sec ";
270              my($comment);
271              if(-f "$rrddir/$machine/$baserrd.def") {
272                  open(DEF, "$rrddir/$machine/$baserrd.def");
# Line 188 | Line 274 | foreach my $machine (@rrddirlist) {
274                  chomp $comment if defined $comment;
275              }
276              $comment = "unknown queue" if not defined $comment;
277 <            &makegraph($machine, $baserrd, $comment, \@data, \@rawdata);
277 >            &makegraph($machine, $baserrd, $comment, "", \@data, \@rawdata);
278          }
279      }
280      # have a last check, maybe we can remove the directory now?
# Line 200 | Line 286 | foreach my $machine (@rrddirlist) {
286          closedir DIR;
287          if($#dirlist == -1) {
288              rmdir "$rrddir/$machine";
289 +            &log("deleting empty rrd directory $rrddir/$machine\n");
290          }
291      }
292   }
# Line 229 | Line 316 | if($deleteimgs) {
316              if((time - $mtime) > $maximgage) {
317                  # if so, delete it
318                  unlink("$imgdir/$machine/$img");
319 +                &log("deleted old image $imgdir/$machine/$img\n");
320              }
321          }
322          # have a last check, maybe we can remove the directory now?
# Line 238 | Line 326 | if($deleteimgs) {
326          closedir DIR;
327          if($#dirlist == -1) {
328              rmdir "$imgdir/$machine";
329 +            &log("deleted empty image directory $imgdir/$machine\n");
330          }
331      }
332   }
# Line 254 | Line 343 | exit(0);
343   #              (eg. cpu)
344   # $title     = the title for the graph
345   #              (eg. kernow CPU usage)
346 + # $vlabel    = the vertical label to apply to the left side of the graph
347 + #              (eg. kb/s)
348   # $dataref   = a reference to an array containing information for the graph
349 < #              elements of format: "gtype:rrdname:dsname:name#colour:comment with spaces"
349 > #              elements of format: "gtype:rrdname:dsname:name#colour:legend:comment with spaces"
350   #              (if gtype is "NONE" only a DEF of 'name' will be defined, no line will be plotted)
351 + #              (if legend is "NONE" the latest/average/max/min legend won't be printed)
352   # $rawcmdref = a reference to an array containing raw rrd commands
353   #              elements a single command each, no spaces
354   #
355  
356   sub makegraph() {
357 <    my($machine, $type, $title, $dataref, $rawcmdref) = @_;
357 >    my($machine, $type, $title, $vlabel, $dataref, $rawcmdref) = @_;
358      # pass in these arrays by reference
359      my(@data) = @$dataref if defined $dataref;
360      my(@rawcmd) = @$rawcmdref if defined $rawcmdref;
# Line 270 | Line 362 | sub makegraph() {
362      if(! -d "$imgdir/$machine") {
363          # not sure on this umask, but it seems to work?
364          mkdir "$imgdir/$machine", 0777;
365 +        &log("created directory $imgdir/$machine\n");
366      }
367      my(@rrdcmd);
368      foreach my $dataitem (@data) {
369 <        # dataitem should be: "gtype:rrdname:dsname:name#colour:comment with spaces"
369 >        # dataitem should be: "gtype:rrdname:dsname:name#colour:legend:comment with spaces"
370          # (if gtype is "NONE" only a DEF of 'name' will be defined, no line will be plotted)
371 <        if($dataitem =~ /^(\S+):(\S+):(\S+):(\S+)#(.{6}):(.*)$/) {
371 >        # (if legend is "NONE" the latest/average/max/min legend won't be printed)
372 >        if($dataitem =~ /^(\S+):(\S+):(\S+):(\S+)#(.{6}):(\S+):(.*)$/) {
373              push @rrdcmd, "DEF:$4=$rrddir/$machine/$2.rrd:$3:AVERAGE";
374              if($1 ne "NONE") {
375 <                push @rrdcmd, "$1:$4#$5:$6";
375 >                push @rrdcmd, "$1:$4#$5:$7";
376 >                if($6 ne "NONE") {
377 >                    # add some nice values to the legend
378 >                    &addlegend(\@rrdcmd, $4);
379 >                }
380              }
381          }
382      }
383      push @rrdcmd, "--title=$title";
384      push @rrdcmd, "--imgformat=PNG";
385      push @rrdcmd, "--lower-limit=0";
386 +    push @rrdcmd, "--vertical-label=$vlabel";
387      # 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 <    print STDERR "Error generating 3h graph for $machine/$type: $err_3h\n" if $err_3h;
393 >    &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      RRDs::graph ("$imgdir/$machine/$type-1d.png", "--start=-86400", @rrdcmd);
396      my($err_1d) = RRDs::error;
397 <    print STDERR "Error generating 1d graph for $machine/$type: $err_1d\n" if $err_1d;
397 >    &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      RRDs::graph ("$imgdir/$machine/$type-1w.png", "--start=-604800", @rrdcmd);
400      my($err_1w) = RRDs::error;
401 <    print STDERR "Error generating 1w graph for $machine/$type: $err_1w\n" if $err_1w;
401 >    &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      RRDs::graph ("$imgdir/$machine/$type-1m.png", "--start=-2678400", @rrdcmd);
404      my($err_1m) = RRDs::error;
405 <    print STDERR "Error generating 1m graph for $machine/$type: $err_1m\n" if $err_1m;
405 >    &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      RRDs::graph ("$imgdir/$machine/$type-1y.png", "--start=-31536000", @rrdcmd);
408      my($err_1y) = RRDs::error;
409 <    print STDERR "Error generating 1y graph for $machine/$type: $err_1y\n" if $err_1y;
409 >    &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      return;
412   }
413  
414 + # subroutine to add a legend
415 + # accepts reference to an array and a name
416 + sub addlegend() {
417 +    my($dataref, $name) = @_;
418 +    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 + }
422 +
423   # hacky subroutine to return a colour
424   # could be done much better somehow :/
425   sub get_colour {
# Line 329 | Line 442 | sub get_colour {
442      else {
443          return "#000066";
444      }
445 + }
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   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines