#!/usr/bin/perl 

# This piece of code is distributed under the terms of the GPL
# Fall 1999: written by chgloor@digicomp.ch
# May  2000: changed for sms use only by michi@digicomp.ch
# May  2000: additional features by chgloor

$version    = '2.0.14 phoenix - chgloor 22.05.00';

$configfile    = './watchdog.conf';
$statusfile    = './watchdog.status';
$lockfile      = './watchdog.lock';
$logfile       = './watchdog.log';

$notify=0;  $notify        = 1 if ($ARGV[0] eq 'notify');
$debug=0;   $debug         = 1 if ($ARGV[0] eq 'debug');

open (LCK, "<$lockfile") and die ("There's another copy of watchdog running right now.\nRemove $lockfile if you want to continue anyway. \n");
`touch $lockfile`;

print "This is watchdog Release $version\nPlease use the option 'notify' to enable notification\n" if ($notify == 0);
$start = time;

if (open (ST, "<$statusfile")) {
    @status = <ST>;
    close (ST);
} else {
    print "Can't open the statusfile: $statusfile\nI'll create a new one, if i am allowed to.\n";
}
open (CF, "<$configfile") or print "cannot open the configfile: $configfile\n";
@config = <CF>;
close (CF);

open (ST, ">$statusfile") or print "cannot reopen the statusfile: $statusfile\n";
open (LOG, ">>$logfile") or print "cannot open the logfile: $logfile\n";

foreach (@config) {
    next if ((/^#/)||(/^\s/));
    ($server) = /^(.+?)\s/;
    ($port[0]) = /\((.+?)\s*[,|\)]/;
    ($port[1]) = /\(.+?,\s*(.+?)\s*[,|\)]/;
    ($port[2]) = /\(.+?,.+?,\s*(.+?)\s*[,|\)]/;
    ($port[3]) = /\(.+?,.+?,.+?,\s*(.+?)\s*[,|\)]/;
    ($port[4]) = /\(.+?,.+?,.+?,.+?,\s*(.+?)\s*[,|\)]/;
    ($sms) = /\)\s+(.+)$/;
	      
    print "\nprocessing $server: " if $debug;      
    $startstring = localtime(time);
	      
    foreach $i (0..4) {
	$temp = $port[$i];
	if ($temp) {
            print "$temp " if $debug;
	    if (system("./$temp $server  &> /dev/null") == 0) {
		$unknown = 1;
		foreach (@status) {
		    if (/$server\s$temp\sdead/) {
			print "up! " if $debug;
                        `echo '$server $temp is up again' | /bin/mail -s $sms sms\@atropina.digicomp.ch`  if ($ARGV[0] eq 'notify');	
          		print ST "$server $temp alive\n";
			print LOG "[$startstring] $server $temp alive\n";
			$unknown = 0;
			last;
		    } elsif (/$server\s$temp\sunknown/) {
			print "up? " if $debug;
	        	print ST "$server $temp alive\n";
		        $unknown = 0;
			last;
		    } elsif (/$server\s$temp\salive/) {
		        print "up. " if $debug;
			print ST "$server $temp alive\n";
			$unknown = 0;
		        last;
		    }
		}
		print "new " if ($debug and $unknown);
		print ST "$server $temp unknown\n" if $unknown;
	        print LOG "[$startstring] $server $temp new\n" if $unknown;

	    } else {
	        $unknown = 1;
		foreach (@status) {
		    if (/$server\s$temp\salive/) {
			print "unknown " if $debug;
		        print ST "$server $temp unknown\n";
			print LOG "[$startstring] $server $temp unknown\n";
       	        	$unknown = 0;
			last;
		    } elsif (/$server\s$temp\sunknown/) {
			print "down! " if $debug;
			`echo '$server $temp is down' | /bin/mail sms\@atropina.digicomp.ch -s $sms`  if ($ARGV[0] eq 'notify');
		        print ST "$server $temp dead\n";
			print LOG "[$startstring] $server $temp dead\n";
       	        	$unknown = 0;
			last;
		    } elsif (/$server\s$temp\sdead/) {
		        print "down. " if $debug;
			print ST "$server $temp dead\n";
			$unknown = 0;
		        last;
		    }
		}
		print "new " if ($debug and $unknown);
		print ST "$server $temp unknown\n" if $unknown; 
		print LOG "[$startstring] $server $temp new\n" if $unknown;
	    }
	}
    }
}

$duration = time - $start;
print "\n\nProcessing took $duration seconds.\n" if $debug;
print "done.\n" if $debug;
close(ST);

`rm -rf $lockfile`;

