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.6 by tdb, Tue May 21 16:47:16 2002 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 >
102   # Read the contents of the base directory
103   # and pull out the list of subdirectories (except . and .. :)
104   opendir(DIR, $rrddir);
# Line 92 | Line 125 | foreach my $machine (@rrddirlist) {
125              if($deleterrds) {
126                  # if so, delete it
127                  unlink("$rrddir/$machine/$rrd");
128 +                &log("deleted old rrd $rrddir/$machine/$rrd\n");
129              }
130 +            else {
131 +                &log("ignored old rrd $rrddir/$machine/$rrd\n");
132 +            }
133              # no more processing required for this rrd
134              next;
135          }
# Line 200 | Line 237 | foreach my $machine (@rrddirlist) {
237          closedir DIR;
238          if($#dirlist == -1) {
239              rmdir "$rrddir/$machine";
240 +            &log("deleting empty rrd directory $rrddir/$machine\n");
241          }
242      }
243   }
# Line 229 | Line 267 | if($deleteimgs) {
267              if((time - $mtime) > $maximgage) {
268                  # if so, delete it
269                  unlink("$imgdir/$machine/$img");
270 +                &log("deleted old image $imgdir/$machine/$img\n");
271              }
272          }
273          # have a last check, maybe we can remove the directory now?
# Line 238 | Line 277 | if($deleteimgs) {
277          closedir DIR;
278          if($#dirlist == -1) {
279              rmdir "$imgdir/$machine";
280 +            &log("deleted empty image directory $imgdir/$machine\n");
281          }
282      }
283   }
# Line 270 | Line 310 | sub makegraph() {
310      if(! -d "$imgdir/$machine") {
311          # not sure on this umask, but it seems to work?
312          mkdir "$imgdir/$machine", 0777;
313 +        &log("created directory $imgdir/$machine\n");
314      }
315      my(@rrdcmd);
316      foreach my $dataitem (@data) {
# Line 291 | Line 332 | sub makegraph() {
332      push @rrdcmd, @rawcmd;
333      RRDs::graph ("$imgdir/$machine/$type-3h.png", "--start=-10800", @rrdcmd);
334      my($err_3h) = RRDs::error;
335 <    print STDERR "Error generating 3h graph for $machine/$type: $err_3h\n" if $err_3h;
335 >    &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      RRDs::graph ("$imgdir/$machine/$type-1d.png", "--start=-86400", @rrdcmd);
338      my($err_1d) = RRDs::error;
339 <    print STDERR "Error generating 1d graph for $machine/$type: $err_1d\n" if $err_1d;
339 >    &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      RRDs::graph ("$imgdir/$machine/$type-1w.png", "--start=-604800", @rrdcmd);
342      my($err_1w) = RRDs::error;
343 <    print STDERR "Error generating 1w graph for $machine/$type: $err_1w\n" if $err_1w;
343 >    &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      RRDs::graph ("$imgdir/$machine/$type-1m.png", "--start=-2678400", @rrdcmd);
346      my($err_1m) = RRDs::error;
347 <    print STDERR "Error generating 1m graph for $machine/$type: $err_1m\n" if $err_1m;
347 >    &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      RRDs::graph ("$imgdir/$machine/$type-1y.png", "--start=-31536000", @rrdcmd);
350      my($err_1y) = RRDs::error;
351 <    print STDERR "Error generating 1y graph for $machine/$type: $err_1y\n" if $err_1y;
351 >    &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      return;
354   }
355  
# Line 329 | Line 375 | sub get_colour {
375      else {
376          return "#000066";
377      }
378 + }
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   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines