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

Comparing projects/cms/source/reports/rrdgraphing/watch.pl (file contents):
Revision 1.2 by tdb, Sat May 18 18:15:59 2002 UTC vs.
Revision 1.3 by tdb, Tue May 21 15:01:43 2002 UTC

# Line 30 | Line 30
30   # $Id$
31   #------------------------------------------------------------
32  
33 < ## TODO
34 < # ought to think about cleaning up when we restart?
35 < #  -- old queue data etc
33 > my($version) = '$Id$';
34  
35   $| = 1;
36  
37   use strict;
38 < use iscream::XMLParser;
38 > use Getopt::Std;
39   use IO::Socket;
40 + use iscream::XMLParser;
41   use RRDs;
42  
43 < # Base directory for images
44 < # (a directory will be constructed for each host under this)
45 < my($imgdir) = "/home/pkg/iscream/public_html/graphs";
43 > # define variables that will be read from the config
44 > # nb. keep this insync with the config file!
45 > use vars qw{
46 >    $imgdir $rrddir                    
47 >    $maxrrdage $maximgage $deleterrds $deleteimgs
48 >    $hex_slash $hex_underscore  
49 >    $rrdstep $retry_wait
50 >    $verbose $quiet
51 > };
52  
53 < # Location of RRD databases
54 < my($rrddir) = "/u1/i-scream/databases";
53 > # default locate of the config file
54 > my($configfile) = "rrdgraphing.conf";
55  
56 < # for reference:
57 < # ch -> hex: $hex = sprintf("%02x", ord($ch));
58 < # hex -> ch: $ch = chr(hex($hex));
56 > # check for command line arguments
57 > my(%opts);
58 > my($ret) = getopts('hvqVc:', \%opts);
59  
60 < # / converted to a decimal then hex'd
61 < my($hex_slash) = "_2f";
57 < # _ converted to a decimal then hex'd
58 < my($hex_underscore) = "_5f";
60 > # if invalid argument given, $ret will not be 1
61 > &usage() if $ret != 1;
62  
63 < # step interval in the rrd databases
61 < my($rrdstep) = 15;
63 > # first process the arguments which might mean we exit now
64  
65 < # time to wait (in seconds) before retrying a connection
66 < my($retry_wait) = 10;
65 > # -h is usage
66 > if($opts{h}) {
67 >    &usage();
68 > }
69 > # -V is version
70 > if($opts{V}) {
71 >    print "watch.pl version: $version\n";
72 >    exit(1);
73 > }
74  
75 + # Then try getting the config
76 +
77 + # -c specifies the config file location
78 + if($opts{c}) {
79 +    $configfile = $opts{c};
80 + }
81 + # suck in the config
82 + &log("reading config from $configfile\n");
83 + do $configfile;
84 +
85 + # Then any options we might want to override the config with
86 +
87 + # -v is verbose
88 + if($opts{v}) {
89 +    $verbose = $opts{v};
90 + }
91 + # -q is verbose
92 + if($opts{q}) {
93 +    $quiet = $opts{q};
94 +    # if we're meant to be quiet, we can hardly be verbose!
95 + #    $verbose = 0;
96 + }
97 +
98 + # Finally check for required arguments
99 +
100 + # check we still have two arguments left
101   if (@ARGV != 2) {
102 <    die "Usage: watch.pl [i-scream client interface] [TCP port]\n";
102 >    &usage();
103   }
104  
105   # user supplied client interface server and port
106   my($addr) = $ARGV[0];
107   my($cport) = $ARGV[1];
108  
109 +
110 + # Main program loop
111   while(1) {
112      
113 <    print "Connecting control channel to port $cport on $addr...\n";
113 >    &log("Connecting control channel to port $cport on $addr...\n");
114      
115      # attempt to connect the control channel
116      my($csock) = new IO::Socket::INET(
# Line 84 | Line 121 | while(1) {
121      
122      # if socket isn't defined connection failed
123      if (!defined $csock) {
124 <        print STDERR "ERROR: Could not connect control channel to $addr:$cport.\n";
125 <        print STDERR "Please check that there is an i-scream server at this address.\n";
124 >        &error("ERROR: Could not connect control channel to $addr:$cport.\n");
125 >        &error("Please check that there is an i-scream server at this address.\n");
126          &wait_then_retry();
127          next;
128      }
# Line 95 | Line 132 | while(1) {
132      # client interface should send it's protocol ID
133      # we know about "PROTOCOL 1.1", and will only accept the same
134      $response = <$csock>;
135 +    &log("CLI sent: $response");
136      if ($response && $response ne "PROTOCOL 1.1\n") {
137 <        print STDERR "The i-scream server sent an unexpected protocol ID: $response\n";
137 >        &error("The i-scream server sent an unexpected protocol ID: $response\n");
138          close($csock);
139          &wait_then_retry();
140          next;
# Line 104 | Line 142 | while(1) {
142      
143      # send our identifier to the client interface
144      print $csock "rrdgraphing\n";
145 +    &log("we sent: rrdgraphing\n");
146      $response = <$csock>;
147 +    &log("CLI sent: $response");
148      if ($response && $response ne "OK\n") {
149 <        print STDERR "Received unexpected response: $response\n";
149 >        &error("Received unexpected response: $response\n");
150          close($csock);
151          &wait_then_retry();
152          next;
# Line 114 | Line 154 | while(1) {
154      
155      # tell the client interface we'd like to start the data channel
156      print $csock "STARTDATA\n";
157 <    
157 >    &log("we sent: STARTDATA\n");
158 >
159      # the response should be the socket to connect the data channel to
160      $response = <$csock>;
161 +    &log("CLI sent: $response");
162      chomp $response;
163      
164      my($dport) = $response;
165 <    print "Connecting data channel to port $dport on $addr...\n";
165 >    &log("Connecting data channel to port $dport on $addr...\n");
166      
167      # attempt to connect the data channel
168      my($dsock) = new IO::Socket::INET(
# Line 131 | Line 173 | while(1) {
173      
174      # if socket isn't defined connection failed
175      if (!defined $dsock) {
176 <        print STDERR "ERROR: Could not connect data channel to $addr:$dport.\n";
177 <        print STDERR "Failure in communications.\n";
176 >        &error("ERROR: Could not connect data channel to $addr:$dport.\n");
177 >        &error("Failure in communications.\n");
178          close($csock);
179          &wait_then_retry();
180          next;
# Line 159 | Line 201 | exit 0;
201   # wait for a while before retrying
202   #
203   sub wait_then_retry() {
204 <    print STDERR "Will retry connection to i-scream server in $retry_wait seconds.\n\n";
204 >    &error("Will retry connection to i-scream server in $retry_wait seconds.\n\n");
205      sleep $retry_wait;
206   }
207  
# Line 186 | Line 228 | sub processdata() {
228          # attempt to parse the data
229          my($err, %xmlhash) = &iscream::XMLParser::parse($xml);
230          if($err) {
231 <            print STDERR "Skipped, XML did not parse: $xml";
231 >            &error("Skipped, XML did not parse: $xml");
232              next;
233          }
234          
# Line 199 | Line 241 | sub processdata() {
241              if(! -d "$rrddir/$machine") {
242                  # not sure on this umask, but it seems to work?
243                  mkdir "$rrddir/$machine", 0777;
244 +                &log("created directory $rrddir/$machine\n");
245              }
246              
247              my(@data);
# Line 267 | Line 310 | sub processdata() {
310              if(! -d "$rrddir/$machine") {
311                  # not sure on this umask, but it seems to work?
312                  mkdir "$rrddir/$machine", 0777;
313 +                &log("created directory $rrddir/$machine\n");
314              }
315              my($hash) = $xmlhash{"packet.attributes.hashCode"};
316              my($date) = $xmlhash{"packet.attributes.date"};
# Line 274 | Line 318 | sub processdata() {
318              # take a look to see if we have a shutdown packet...
319              if($xmlhash{"packet.attributes.shutdown"} && $xmlhash{"packet.attributes.shutdown"} eq "true") {
320                  unlink <$rrddir/$machine/$hash\_*.rrd>, "$rrddir/$machine/$hash.def", <$imgdir/$machine/$hash*.png>;
321 +                &log("deleted $rrddir/$machine/$hash\_*.rrd $rrddir/$machine/$hash.def $imgdir/$machine/$hash*.png\n");
322                  next;
323              }
324              # look through to see how many internal queues we have
# Line 283 | Line 328 | sub processdata() {
328                  if($xmlhash{"packet.queue.attributes.queue$i"} eq "[deleted]") {
329                      # delete the queues rrd
330                      unlink "$rrddir/$machine/$hash\_$i.rrd";
331 +                    &log("deleted $rrddir/$machine/$hash\_$i.rrd\n");
332                      # are there any other rrd's left on this queue? if not, cleanup.
333                      # get a list of any that may be still there..
334                      opendir(DIR, "$rrddir/$machine");
# Line 293 | Line 339 | sub processdata() {
339                      if($rrdcount == 0) {
340                          # clean up the def file and any images
341                          unlink "$rrddir/$machine/$hash.def", <$imgdir/$machine/$hash*.png>;
342 +                        &log("deleted $rrddir/$machine/$hash.def $imgdir/$machine/$hash*.png\n");
343                      }
344                      ++$i;
345                      next;
# Line 303 | Line 350 | sub processdata() {
350                      open(DEF, ">$rrddir/$machine/$hash.def");
351                      print DEF $name;
352                      close DEF;
353 +                    &log("created $rrddir/$machine/$hash.def\n");
354                  }
355                  my(@data) = ( "packet.queue.attributes.queue$i:size:GAUGE",
356                                "packet.queue.attributes.total:total:COUNTER",
# Line 312 | Line 360 | sub processdata() {
360              }
361          }
362          else {
363 <            #print STDERR "SKIPPED: valid xml, but not a data or statistics packet\n";
363 >            #&error("SKIPPED: valid xml, but not a data or statistics packet\n");
364          }
365      }
366      
367      # we'll now return from this sub and reconnect
368 <    print STDERR "Data channel socket gave no data, bailing out...\n";
368 >    &error("Data channel socket gave no data, bailing out...\n");
369   }
370  
371   #
# Line 349 | Line 397 | sub updaterrd() {
397              }
398          }
399          # call the &makerrd to actually create the rrd
400 <        print "making new rrd for $rrddir/$machine/$type.rrd\n";
400 >        &log("making new rrd for $rrddir/$machine/$type.rrd\n");
401          &makerrd($machine, $type, $date, $step, @createdata);
402      }
403      # get the details out of the data we've been given
# Line 366 | Line 414 | sub updaterrd() {
414      }
415      # perform the update
416      RRDs::update ("$rrddir/$machine/$type.rrd", $updateparams);
417 +    &log("updating $rrddir/$machine/$type.rrd\n");
418      my($err) = RRDs::error;
419 <    print STDERR "Error updating $rrddir/$machine/$type.rrd: $err\n" if $err;
419 >    &error("Error updating $rrddir/$machine/$type.rrd: $err\n") if $err;
420   }
421  
422   #
# Line 390 | Line 439 | sub makerrd() {
439      if(! -d "$rrddir/$machine") {
440          # not sure on this umask, but it seems to work?
441          mkdir "$rrddir/$machine", 0777;
442 +        &log("created directory $rrddir/$machine\n");
443      }
444      my(@rrdcmd);
445      # we'll want to add our first data item at $start,
# Line 423 | Line 473 | sub makerrd() {
473      );
474      RRDs::create (@rrdcmd);
475      my($err) = RRDs::error;
476 <    print STDERR "Error creating rrd for $rrddir/$machine/$type: $err\n" if $err;
476 >    &error("Error creating rrd for $rrddir/$machine/$type: $err\n") if $err;
477 > }
478 >
479 > # prints out usage information then exits
480 > sub usage() {
481 >    print "Usage: watch.pl [options] i-scream_client_interface port\n";
482 >    print "Options\n";
483 >    print "  -c config        Specifies the configuration file\n";
484 >    print "                    default: rrdgraphing.conf\n";
485 >    print "  -v               Be verbose about what's happening\n";
486 >    print "  -q               Be quiet, even supress errors\n";
487 >    print "  -V               Print version number\n";
488 >    print "  -h               Prints this help page\n";
489 >    exit(1);
490 > }
491 >
492 > # prints a log message if verbose is turned on
493 > sub log() {
494 >    my($msg) = @_;
495 >    print $msg if $verbose;
496 > }
497 >
498 > # prints an error message unless quiet is turned on
499 > sub error() {
500 >    my($msg) = @_;
501 >    print STDERR $msg unless $quiet;
502   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines