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.9 by tdb, Thu Feb 10 17:35:58 2005 UTC

# Line 2 | Line 2
2  
3   #
4   # i-scream central monitoring system
5 + # http://www.i-scream.org
6   # Copyright (C) 2000-2002 i-scream
7   #
8   # This program is free software; you can redistribute it and/or
# Line 21 | Line 22
22  
23   # -----------------------------------------------------------
24   # i-scream graph generation scripts
25 < # http://www.i-scream.org.uk
25 > # http://www.i-scream.org
26   #
27   # Generates rrd databases for i-scream data by connecting to
28   # the i-scream server and collecting data.
# Line 30 | Line 31
31   # $Id$
32   #------------------------------------------------------------
33  
34 < ## TODO
34 < # ought to think about cleaning up when we restart?
35 < #  -- old queue data etc
34 > my($version) = '$Id$';
35  
36   $| = 1;
37  
38   use strict;
39 < use iscream::XMLParser;
39 > use Getopt::Std;
40   use IO::Socket;
41 + use iscream::XMLParser;
42   use RRDs;
43  
44 < # Base directory for images
45 < # (a directory will be constructed for each host under this)
46 < my($imgdir) = "/home/pkg/iscream/public_html/graphs";
44 > # define variables that will be read from the config
45 > # nb. keep this insync with the config file!
46 > use vars qw{
47 >    $imgdir $rrddir
48 >    $maxrrdage $maximgage $deleterrds $deleteimgs
49 >    $hex_slash $hex_underscore $hex_space $hex_colon $hex_bslash
50 >    $rrdstep $retry_wait
51 >    $verbose $quiet
52 > };
53  
54 < # Location of RRD databases
55 < my($rrddir) = "/u1/i-scream/databases";
54 > # default locate of the config file
55 > my($configfile) = "rrdgraphing.conf";
56  
57 < # for reference:
58 < # ch -> hex: $hex = sprintf("%02x", ord($ch));
59 < # hex -> ch: $ch = chr(hex($hex));
57 > # check for command line arguments
58 > my(%opts);
59 > my($ret) = getopts('hvqVc:', \%opts);
60  
61 < # / converted to a decimal then hex'd
62 < my($hex_slash) = "_2f";
57 < # _ converted to a decimal then hex'd
58 < my($hex_underscore) = "_5f";
61 > # if invalid argument given, $ret will not be 1
62 > &usage() if $ret != 1;
63  
64 < # step interval in the rrd databases
61 < my($rrdstep) = 15;
64 > # first process the arguments which might mean we exit now
65  
66 < # time to wait (in seconds) before retrying a connection
67 < my($retry_wait) = 10;
66 > # -h is usage
67 > if($opts{h}) {
68 >    &usage();
69 > }
70 > # -V is version
71 > if($opts{V}) {
72 >    print "watch.pl version: $version\n";
73 >    exit(1);
74 > }
75  
76 + # Then try getting the config
77 +
78 + # -c specifies the config file location
79 + if($opts{c}) {
80 +    $configfile = $opts{c};
81 + }
82 + # suck in the config
83 + &log("reading config from $configfile\n");
84 + do $configfile;
85 +
86 + # Then any options we might want to override the config with
87 +
88 + # -v is verbose
89 + if($opts{v}) {
90 +    $verbose = $opts{v};
91 + }
92 + # -q is verbose
93 + if($opts{q}) {
94 +    $quiet = $opts{q};
95 +    # if we're meant to be quiet, we can hardly be verbose!
96 + #    $verbose = 0;
97 + }
98 +
99 + # Finally check for required arguments
100 +
101 + # check we still have two arguments left
102   if (@ARGV != 2) {
103 <    die "Usage: watch.pl [i-scream client interface] [TCP port]\n";
103 >    &usage();
104   }
105  
106   # user supplied client interface server and port
107   my($addr) = $ARGV[0];
108   my($cport) = $ARGV[1];
109  
110 +
111 + # Main program loop
112   while(1) {
113      
114 <    print "Connecting control channel to port $cport on $addr...\n";
114 >    &log("Connecting control channel to port $cport on $addr...\n");
115      
116      # attempt to connect the control channel
117      my($csock) = new IO::Socket::INET(
# Line 84 | Line 122 | while(1) {
122      
123      # if socket isn't defined connection failed
124      if (!defined $csock) {
125 <        print STDERR "ERROR: Could not connect control channel to $addr:$cport.\n";
126 <        print STDERR "Please check that there is an i-scream server at this address.\n";
125 >        &error("ERROR: Could not connect control channel to $addr:$cport.\n");
126 >        &error("Please check that there is an i-scream server at this address.\n");
127          &wait_then_retry();
128          next;
129      }
# Line 95 | Line 133 | while(1) {
133      # client interface should send it's protocol ID
134      # we know about "PROTOCOL 1.1", and will only accept the same
135      $response = <$csock>;
136 +    &log("CLI sent: $response");
137      if ($response && $response ne "PROTOCOL 1.1\n") {
138 <        print STDERR "The i-scream server sent an unexpected protocol ID: $response\n";
138 >        &error("The i-scream server sent an unexpected protocol ID: $response\n");
139          close($csock);
140          &wait_then_retry();
141          next;
# Line 104 | Line 143 | while(1) {
143      
144      # send our identifier to the client interface
145      print $csock "rrdgraphing\n";
146 +    &log("we sent: rrdgraphing\n");
147      $response = <$csock>;
148 +    &log("CLI sent: $response");
149      if ($response && $response ne "OK\n") {
150 <        print STDERR "Received unexpected response: $response\n";
150 >        &error("Received unexpected response: $response\n");
151          close($csock);
152          &wait_then_retry();
153          next;
# Line 114 | Line 155 | while(1) {
155      
156      # tell the client interface we'd like to start the data channel
157      print $csock "STARTDATA\n";
158 <    
158 >    &log("we sent: STARTDATA\n");
159 >
160      # the response should be the socket to connect the data channel to
161      $response = <$csock>;
162 +    &log("CLI sent: $response");
163      chomp $response;
164      
165      my($dport) = $response;
166 <    print "Connecting data channel to port $dport on $addr...\n";
166 >    &log("Connecting data channel to port $dport on $addr...\n");
167      
168      # attempt to connect the data channel
169      my($dsock) = new IO::Socket::INET(
# Line 131 | Line 174 | while(1) {
174      
175      # if socket isn't defined connection failed
176      if (!defined $dsock) {
177 <        print STDERR "ERROR: Could not connect data channel to $addr:$dport.\n";
178 <        print STDERR "Failure in communications.\n";
177 >        &error("ERROR: Could not connect data channel to $addr:$dport.\n");
178 >        &error("Failure in communications.\n");
179          close($csock);
180          &wait_then_retry();
181          next;
# Line 159 | Line 202 | exit 0;
202   # wait for a while before retrying
203   #
204   sub wait_then_retry() {
205 <    print STDERR "Will retry connection to i-scream server in $retry_wait seconds.\n\n";
205 >    &error("Will retry connection to i-scream server in $retry_wait seconds.\n\n");
206      sleep $retry_wait;
207   }
208  
# Line 186 | Line 229 | sub processdata() {
229          # attempt to parse the data
230          my($err, %xmlhash) = &iscream::XMLParser::parse($xml);
231          if($err) {
232 <            print STDERR "Skipped, XML did not parse: $xml";
232 >            &error("Skipped, XML did not parse: $xml");
233              next;
234          }
235          
# Line 199 | Line 242 | sub processdata() {
242              if(! -d "$rrddir/$machine") {
243                  # not sure on this umask, but it seems to work?
244                  mkdir "$rrddir/$machine", 0777;
245 +                &log("created directory $rrddir/$machine\n");
246              }
247              
248              my(@data);
# Line 212 | Line 256 | sub processdata() {
256                       );
257              &updaterrd($machine, "cpu", $date, $rrdstep, \%xmlhash, @data);
258                    
259 +            # uptime
260 +            @data = ( "packet.os.uptime:uptime:GAUGE" );
261 +            &updaterrd($machine, "uptime", $date, $rrdstep, \%xmlhash, @data);
262 +                  
263              # mem
264              @data = ( "packet.memory.free:free:GAUGE",
265                        "packet.memory.total:total:GAUGE",
266 +                      "packet.memory.cache:cache:GAUGE",
267                       );
268              &updaterrd($machine, "mem", $date, $rrdstep, \%xmlhash, @data);
269                      
# Line 244 | Line 293 | sub processdata() {
293              @data = ( "packet.users.count:count:GAUGE",
294                       );
295              &updaterrd($machine, "users", $date, $rrdstep, \%xmlhash, @data);
296 <            
296 >
297 >            # paging
298 >            @data = ( "packet.pages.pageins:pageins:GAUGE",
299 >                      "packet.pages.pageouts:pageouts:GAUGE",
300 >                     );
301 >            &updaterrd($machine, "paging", $date, $rrdstep, \%xmlhash, @data);
302 >
303              # disk
304              my($i) = 0;
305              while(defined $xmlhash{"packet.disk.p$i.attributes.mount"}) {
306                  my($mount) = $xmlhash{"packet.disk.p$i.attributes.mount"};
307                  $mount =~ s/_/$hex_underscore/g;
308                  $mount =~ s/\//$hex_slash/g;
309 <                @data = ( "packet.disk.p$i.attributes.kbytes:kbytes:GAUGE",
309 >                $mount =~ s/\\/$hex_bslash/g;
310 >                $mount =~ s/ /$hex_space/g;
311 >                $mount =~ s/:/$hex_colon/g;
312 >                @data = ( "packet.disk.p$i.attributes.total:total:GAUGE",
313                            "packet.disk.p$i.attributes.used:used:GAUGE",
314 +                          "packet.disk.p$i.attributes.totalinodes:totalinodes:GAUGE",
315 +                          "packet.disk.p$i.attributes.freeinodes:freeinodes:GAUGE",
316                           );
317                  &updaterrd($machine, "disk-$mount", $date, $rrdstep, \%xmlhash, @data);
318                  ++$i;
319              }
320 +
321 +            # diskio
322 +            $i = 0;
323 +            while(defined $xmlhash{"packet.diskio.p$i.attributes.name"}) {
324 +                my($name) = $xmlhash{"packet.diskio.p$i.attributes.name"};
325 +                $name =~ s/_/$hex_underscore/g;
326 +                $name =~ s/\//$hex_slash/g;
327 +                $name =~ s/\\/$hex_bslash/g;
328 +                $name =~ s/ /$hex_space/g;
329 +                $name =~ s/:/$hex_colon/g;
330 +                @data = ( "packet.diskio.p$i.attributes.rbytes:rbytes:GAUGE",
331 +                          "packet.diskio.p$i.attributes.wbytes:wbytes:GAUGE",
332 +                         );
333 +                &updaterrd($machine, "diskio-$name", $date, $rrdstep, \%xmlhash, @data);
334 +                ++$i
335 +            }
336 +
337 +            # net
338 +            $i = 0;
339 +            while(defined $xmlhash{"packet.net.p$i.attributes.name"}) {
340 +                my($name) = $xmlhash{"packet.net.p$i.attributes.name"};
341 +                $name =~ s/_/$hex_underscore/g;
342 +                $name =~ s/\//$hex_slash/g;
343 +                $name =~ s/\\/$hex_bslash/g;
344 +                $name =~ s/ /$hex_space/g;
345 +                $name =~ s/:/$hex_colon/g;
346 +                @data = ( "packet.net.p$i.attributes.rx:rx:GAUGE",
347 +                          "packet.net.p$i.attributes.tx:tx:GAUGE",
348 +                         );
349 +                &updaterrd($machine, "net-$name", $date, $rrdstep, \%xmlhash, @data);
350 +                ++$i
351 +            }
352 +
353 +            # mailq
354 +            $i = 0;
355 +            while(defined $xmlhash{"packet.mailq.p$i.attributes.name"}) {
356 +                my($name) = $xmlhash{"packet.mailq.p$i.attributes.name"};
357 +                $name =~ s/\s+//g;
358 +                @data = ( "packet.mailq.p$i.attributes.size:size:GAUGE",
359 +                         );
360 +                &updaterrd($machine, "mailq-$name", $date, $rrdstep, \%xmlhash, @data);
361 +                ++$i
362 +            }
363          }
364          
365          # queue statistics packet
# Line 267 | Line 370 | sub processdata() {
370              if(! -d "$rrddir/$machine") {
371                  # not sure on this umask, but it seems to work?
372                  mkdir "$rrddir/$machine", 0777;
373 +                &log("created directory $rrddir/$machine\n");
374              }
375              my($hash) = $xmlhash{"packet.attributes.hashCode"};
376              my($date) = $xmlhash{"packet.attributes.date"};
# Line 274 | Line 378 | sub processdata() {
378              # take a look to see if we have a shutdown packet...
379              if($xmlhash{"packet.attributes.shutdown"} && $xmlhash{"packet.attributes.shutdown"} eq "true") {
380                  unlink <$rrddir/$machine/$hash\_*.rrd>, "$rrddir/$machine/$hash.def", <$imgdir/$machine/$hash*.png>;
381 +                &log("deleted $rrddir/$machine/$hash\_*.rrd $rrddir/$machine/$hash.def $imgdir/$machine/$hash*.png\n");
382                  next;
383              }
384              # look through to see how many internal queues we have
# Line 283 | Line 388 | sub processdata() {
388                  if($xmlhash{"packet.queue.attributes.queue$i"} eq "[deleted]") {
389                      # delete the queues rrd
390                      unlink "$rrddir/$machine/$hash\_$i.rrd";
391 +                    &log("deleted $rrddir/$machine/$hash\_$i.rrd\n");
392                      # are there any other rrd's left on this queue? if not, cleanup.
393                      # get a list of any that may be still there..
394                      opendir(DIR, "$rrddir/$machine");
# Line 293 | Line 399 | sub processdata() {
399                      if($rrdcount == 0) {
400                          # clean up the def file and any images
401                          unlink "$rrddir/$machine/$hash.def", <$imgdir/$machine/$hash*.png>;
402 +                        &log("deleted $rrddir/$machine/$hash.def $imgdir/$machine/$hash*.png\n");
403                      }
404                      ++$i;
405                      next;
# Line 303 | Line 410 | sub processdata() {
410                      open(DEF, ">$rrddir/$machine/$hash.def");
411                      print DEF $name;
412                      close DEF;
413 +                    &log("created $rrddir/$machine/$hash.def\n");
414                  }
415                  my(@data) = ( "packet.queue.attributes.queue$i:size:GAUGE",
416                                "packet.queue.attributes.total:total:COUNTER",
# Line 312 | Line 420 | sub processdata() {
420              }
421          }
422          else {
423 <            #print STDERR "SKIPPED: valid xml, but not a data or statistics packet\n";
423 >            #&error("SKIPPED: valid xml, but not a data or statistics packet\n");
424          }
425      }
426      
427      # we'll now return from this sub and reconnect
428 <    print STDERR "Data channel socket gave no data, bailing out...\n";
428 >    &error("Data channel socket gave no data, bailing out...\n");
429   }
430  
431   #
# Line 349 | Line 457 | sub updaterrd() {
457              }
458          }
459          # call the &makerrd to actually create the rrd
460 <        print "making new rrd for $rrddir/$machine/$type.rrd\n";
460 >        &log("making new rrd for $rrddir/$machine/$type.rrd\n");
461          &makerrd($machine, $type, $date, $step, @createdata);
462      }
463      # get the details out of the data we've been given
# Line 366 | Line 474 | sub updaterrd() {
474      }
475      # perform the update
476      RRDs::update ("$rrddir/$machine/$type.rrd", $updateparams);
477 +    &log("updating $rrddir/$machine/$type.rrd\n");
478      my($err) = RRDs::error;
479 <    print STDERR "Error updating $rrddir/$machine/$type.rrd: $err\n" if $err;
479 >    &error("Error updating $rrddir/$machine/$type.rrd: $err\n") if $err;
480   }
481  
482   #
# Line 390 | Line 499 | sub makerrd() {
499      if(! -d "$rrddir/$machine") {
500          # not sure on this umask, but it seems to work?
501          mkdir "$rrddir/$machine", 0777;
502 +        &log("created directory $rrddir/$machine\n");
503      }
504      my(@rrdcmd);
505      # we'll want to add our first data item at $start,
# Line 423 | Line 533 | sub makerrd() {
533      );
534      RRDs::create (@rrdcmd);
535      my($err) = RRDs::error;
536 <    print STDERR "Error creating rrd for $rrddir/$machine/$type: $err\n" if $err;
536 >    &error("Error creating rrd for $rrddir/$machine/$type: $err\n") if $err;
537 > }
538 >
539 > # prints out usage information then exits
540 > sub usage() {
541 >    print "Usage: watch.pl [options] i-scream_client_interface port\n";
542 >    print "Options\n";
543 >    print "  -c config        Specifies the configuration file\n";
544 >    print "                    default: rrdgraphing.conf\n";
545 >    print "  -v               Be verbose about what's happening\n";
546 >    print "  -q               Be quiet, even supress errors\n";
547 >    print "  -V               Print version number\n";
548 >    print "  -h               Prints this help page\n";
549 >    exit(1);
550 > }
551 >
552 > # prints a log message if verbose is turned on
553 > sub log() {
554 >    my($msg) = @_;
555 >    print $msg if $verbose;
556 > }
557 >
558 > # prints an error message unless quiet is turned on
559 > sub error() {
560 >    my($msg) = @_;
561 >    print STDERR $msg unless $quiet;
562   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines