"Fossies" - the Fresh Open Source Software Archive

Member "sawdog-2.4/sawdog.pl" (22 Sep 2006, 9512 Bytes) of package /linux/privat/old/sawdog-2.4.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Perl source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "sawdog.pl" see the Fossies "Dox" file reference documentation.

    1 #!/usr/bin/perl
    2 # 
    3 # Sawdog - A collection of simple scripts, which informs in case of server outages
    4 # Copyright 1999-2000 by Christian Gloor
    5 # 
    6 # This piece of code is distributed under the terms of the GNU General Public License (GPL)
    7 # You should have received a copy of the GPL (file COPYING) along with those scripts; 
    8 # if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambrisge, MA 02139, USA
    9 #
   10 # Current maintainer: chgloor@digicomp.ch, don't hesitate to contact him if you have any questions. 
   11 #
   12 # Fall 1999: 1.0.0 initial        - chgloor@digicomp.ch: initial release for internal use only 
   13 # May  2000: 1.2.0                - michi@digicomp.ch: changed for sms use only 
   14 # May  2000: 2.0.0 phoenix        - chgloor: additional features (state unknown, logfile, locking)
   15 # June 2000: 2.1.0 generic        - chgloor: compact notification, aliases, generic notification
   16 # June 2000: 2.2.0 speedup        - chgloor: expect scritps speedup, 'required'
   17 # Nov  2000: 2.3.0 multiple rec.  - chgloor: multiple recipients
   18 # Sept 2006: 2.4.0 cleanup        - chgloor/max: cleanup/webinterface
   19 
   20 $version       = '2.4.00 multiple recipients - chgloor 14.09.06';
   21 
   22 $varpath       = '/var/sawdog';
   23 $confpath      = '/etc/sawdog';
   24 $configfile    = "$confpath/sawdog.conf";
   25 $statusfile    = "$varpath/sawdog.status";
   26 $lockfile      = "$varpath/sawdog.lock";
   27 $logfile       = "/var/log/sawdog.log";
   28 $servicedir    = './services/';
   29 
   30 # don't change anything below this line, unless you really know what you do, this is the end of the config section
   31 # ----------------------------------------------------------------------------------------------------------------
   32 
   33 $notify=0;  $notify        = 1 if ($ARGV[0] eq 'notify');
   34 $verbose=1; $verbose       = 0 if ($notify == 1);
   35 
   36 
   37 # check for another copy running - if there is a lockfile, exit, create a new lockflie otherwise.
   38 open (LCK, "<$lockfile") and die ("There's another copy of sawdog running right now.\nRemove $lockfile if you want to continue anyway. \n");
   39 `touch $lockfile`;
   40 
   41 # print release number and stuff if not invoked with argument 'notify'
   42 print "This is sawdog Release $version\nPlease use the argument 'notify' to enable notification\n" if ($notify == 0);
   43 $start = time;
   44 
   45 # try to open the existing statusfile, and read all the stuff into memory
   46 if (open (ST, "<$statusfile")) { 
   47     @status = <ST>; 
   48     close (ST); 
   49 } else {
   50     print "Can't open the statusfile: $statusfile\nI'll create a new one\n";
   51 }
   52 
   53 # try to read the configuration file. if this fails, we cannot do anything, so exit then.
   54 open (CF, "<$configfile") or print "cannot open the configfile: $configfile - check the permissions and the filename\n";
   55 @config = <CF>;
   56 close (CF);
   57 
   58 # create a new, empty statusfile. if one exists already, overwrite it. we have the content in memory.
   59 # if the script crashes here, don't worry, you will just get some 'we are up' messages. future enhancement possible here.
   60 open (ST, ">$statusfile") or print "cannot reopen the statusfile: $statusfile - check the permissions\n";
   61 
   62 # open the logfile in append mode. 
   63 open (LOG, ">>$logfile") or print "cannot append the logfile: $logfile - check the permissions\n";
   64 
   65 foreach (@config) {
   66     $status = '';
   67     $required = 0;
   68     # if the line is a comment '#' or empty ' '
   69     next if ((/^\#/)||(/^\s/));
   70     # fill the notification hash with notification methods. if the line starts with a dot only
   71     if (/^\.(.+?)\s+\((.+?)\)/) {
   72     $notification{$1} = $2;
   73     next;
   74     }
   75 
   76     if (s/^\!//) {
   77     $required = 1;
   78     }
   79     # the first word, until a whitespace occurs
   80     ($server) = /^(.+?)\s/;
   81     # if an alias is set, else servername. the word before the first open bracket '('
   82     ($alias)  = /(\S+?)\s+\(/;
   83     # the ports. words between brackets. brute force.
   84     ($port[0]) = /\((.+?)\s*[,|\)]/;
   85     ($port[1]) = /\(.+?,\s*(.+?)\s*[,|\)]/;
   86     ($port[2]) = /\(.+?,.+?,\s*(.+?)\s*[,|\)]/;
   87     ($port[3]) = /\(.+?,.+?,.+?,\s*(.+?)\s*[,|\)]/;
   88     ($port[4]) = /\(.+?,.+?,.+?,.+?,\s*(.+?)\s*[,|\)]/;
   89     ($port[5]) = /\(.+?,.+?,.+?,.+?,.+?,\s*(.+?)\s*[,|\)]/;
   90     ($port[6]) = /\(.+?,.+?,.+?,.+?,.+?,.+?,\s*(.+?)\s*[,|\)]/;
   91     # the person responsible for this server, between last close bracket ')' and line end.
   92     ($sms) = /\)\s+(.+)$/;
   93     
   94     print "\nprocessing $server ($alias): " if $verbose;      
   95     $startstring = localtime(time);
   96     
   97     foreach $i (0..6) {
   98     $temp = $port[$i];
   99     if ($temp) {
  100             print "$temp " if $verbose;
  101         if (system("$servicedir$temp $server  &> /dev/null") == 0) {
  102         $unknown = 1;
  103         foreach (@status) {
  104             if (/$server\s$temp\sdead/) {
  105             print "up! " if $verbose;
  106                         $hashup{$sms} = "UP: $alias ->" if ($hashup{$sms} eq '');
  107                         $hashup{$sms} = "$hashup{$sms} $temp";
  108                 print ST "$server $temp alive\n";
  109             print LOG "[$startstring] $server $temp alive\n";
  110             $unknown = 0;
  111             last;
  112             } elsif (/$server\s$temp\sunknown/) {
  113             print "up? " if $verbose;
  114                 print ST "$server $temp alive\n";
  115                 $unknown = 0;
  116             last;
  117             } elsif (/$server\s$temp\salive/) {
  118                 print "up. " if $verbose;
  119             print ST "$server $temp alive\n";
  120             $unknown = 0;
  121                 last;
  122             }
  123         }
  124         print "new " if ($verbose and $unknown);
  125         print ST "$server $temp unknown\n" if $unknown;
  126             print LOG "[$startstring] $server $temp new\n" if $unknown;
  127         $status = 'up';
  128         } else {
  129             $unknown = 1;
  130         foreach (@status) {
  131             if (/$server\s$temp\salive/) {
  132             print "unknown " if $verbose;
  133                 print ST "$server $temp unknown\n";
  134             print LOG "[$startstring] $server $temp unknown\n";
  135                     $unknown = 0;
  136             last;
  137             } elsif (/$server\s$temp\sunknown/) {
  138             print "down! " if $verbose;
  139                         $hashdown{$sms} = "DOWN: $alias ->" if ($hashdown{$sms} eq '');
  140                         $hashdown{$sms} = "$hashdown{$sms} $temp";
  141                 print ST "$server $temp dead\n";
  142             print LOG "[$startstring] $server $temp dead\n";
  143                     $unknown = 0;
  144             last;
  145             } elsif (/$server\s$temp\sdead/) {
  146                 print "down. " if $verbose;
  147             print ST "$server $temp dead\n";
  148             $unknown = 0;
  149                 last;
  150             }
  151         }
  152         print "new " if ($verbose and $unknown);
  153         print ST "$server $temp unknown\n" if $unknown; 
  154         print LOG "[$startstring] $server $temp new\n" if $unknown;
  155         $status = 'down';
  156         }
  157     }
  158     }
  159     # process the entire hash 'hashup', if there's something in, at least one server is up again
  160     while (($methoduser, $message) = each %hashup) {
  161     # trigger notification for each name and each server, but all ports at once
  162         
  163     # *****************************************************************************
  164     # loop over recipients list
  165     foreach $singlemethoduser (split /\s+/, $methoduser) {
  166         
  167         # check if there is a -> in the username, if so, split it into method and username
  168         if ($singlemethoduser =~ /->/) {
  169         ($method, $user) = $singlemethoduser =~ /^(.+?)->(.+)/;
  170         } else {
  171         # if not, set the default
  172         $method = 'default';
  173         $user = $singlemethoduser;
  174         }
  175         print "\n$user via $method --> $message" if $verbose;
  176         print LOG "$user via $method --> $message\n";
  177         # substitute the %xx% tokens by the proper values
  178         $toexecute = $notification{$method};
  179         $toexecute =~ s/\%user\%/$user/gi;
  180         $toexecute =~ s/\%message\%/$message/gi;
  181         # execute the string finally 
  182         eval '`$toexecute`' if ($ARGV[0] eq 'notify');
  183     }
  184     # *****************************************************************************
  185     # end loop over recipients
  186     
  187     # remove the key from the hash, cleanup
  188     delete $hashup{$methoduser};
  189     }    
  190     # process the entire hash 'hashdown', if there's something in, at least one server is down
  191     while (($methoduser, $message) = each %hashdown) {
  192     # trigger notification for each name and each server, but all ports at once
  193     
  194     # *****************************************************************************
  195     # loop over recipients list
  196     foreach $singlemethoduser (split /\s+/, $methoduser) {
  197         # check if there is a -> in the username, if so, split it into method and username
  198         if ($singlemethoduser =~ /->/) {
  199         ($method, $user) = $singlemethoduser =~ /^(.+?)->(.+)/;
  200         } else {
  201         # if not, set the default
  202         $method = 'default';
  203         $user = $singlemethoduser;
  204         }
  205         if ($required==1) {
  206         $message = "$message! further processing aborted!";
  207         for (@status) {
  208             print ST $_ if (/[^!]/);
  209         }
  210         }
  211         print "\n$user via $method--> $message" if $verbose;
  212         print LOG "$user via $method --> $message\n";
  213         # substitute the %xx% tokens by the proper values
  214         $toexecute = $notification{$method};
  215         $toexecute =~ s/\%user\%/$user/gi;
  216         $toexecute =~ s/\%message\%/$message/gi;
  217         # execute the string finally
  218         eval '`$toexecute`' if ($ARGV[0] eq 'notify');
  219     }
  220     # *****************************************************************************
  221     # end loop over recipients
  222     
  223     # remove the key from the hash, cleanup
  224     delete $hashdown{$methoduser};
  225     }
  226     last if (($required==1) & ($status eq 'down'));
  227 }
  228 
  229 # compute the time elapsed by processing the whole thing
  230 # nice to know if you're planning to run this as a frequent cron job
  231 $duration = time - $start;
  232 print "\n\nProcessing took $duration seconds.\n" if $verbose;
  233 
  234 # close the statusfile (and flush the buffer, i.e. write to the disk)
  235 close(ST);
  236 
  237 print "done.\n" if $verbose;
  238 
  239 # remove the lockfile. quick and fucking dirty hack.
  240 `rm -rf $lockfile`;
  241 
  242 
  243 
  244 
  245 
  246 
  247 
  248 
  249 
  250 
  251 
  252 
  253