#!/usr/bin/perl
# 
# Sawdog - A collection of simple scripts, which informs in case of server outages
# Copyright 1999-2000 by Christian Gloor
# 
# This piece of code is distributed under the terms of the GNU General Public License (GPL)
# You should have received a copy of the GPL (file COPYING) along with those scripts; 
# if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambrisge, MA 02139, USA
#
# Current maintainer: chgloor@digicomp.ch, don't hesitate to contact him if you have any questions. 
#
# Fall 1999: 1.0.0 initial  - chgloor@digicomp.ch: initial release for internal use only 
# May  2000: 1.2.0          - michi@digicomp.ch: changed for sms use only 
# May  2000: 2.0.0 phoenix  - chgloor added additional features (state unknown, logfile, locking)

$version       = '2.0.17 phoenix - chgloor 23.05.00';

$configfile    = './sawdog.conf';
$statusfile    = './sawdog.status';
$lockfile      = './sawdog.lock';
$logfile       = './sawdog.log';
$servicedir    = './services/';

# don't change anything below this line, unless you really know what you do, this is the end of the config section
# ----------------------------------------------------------------------------------------------------------------

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

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

print "This is sawdog 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\n";
}
open (CF, "<$configfile") or print "cannot open the configfile: $configfile - check the permissions and the filename\n";
@config = <CF>;
close (CF);

open (ST, ">$statusfile") or print "cannot reopen the statusfile: $statusfile - check the permissions\n";
open (LOG, ">>$logfile") or print "cannot append the logfile: $logfile - check the permissions\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 $verbose;      
    $startstring = localtime(time);
    
    foreach $i (0..4) {
	$temp = $port[$i];
	if ($temp) {
            print "$temp " if $verbose;
	    if (system("$servicedir$temp $server  &> /dev/null") == 0) {
		$unknown = 1;
		foreach (@status) {
		    if (/$server\s$temp\sdead/) {
			print "up! " if $verbose;
                        `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 $verbose;
	        	print ST "$server $temp alive\n";
		        $unknown = 0;
			last;
		    } elsif (/$server\s$temp\salive/) {
		        print "up. " if $verbose;
			print ST "$server $temp alive\n";
			$unknown = 0;
		        last;
		    }
		}
		print "new " if ($verbose 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 $verbose;
		        print ST "$server $temp unknown\n";
			print LOG "[$startstring] $server $temp unknown\n";
       	        	$unknown = 0;
			last;
		    } elsif (/$server\s$temp\sunknown/) {
			print "down! " if $verbose;
			`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 $verbose;
			print ST "$server $temp dead\n";
			$unknown = 0;
		        last;
		    }
		}
		print "new " if ($verbose 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 $verbose;
print "done.\n" if $verbose;
close(ST);

`rm -rf $lockfile`;

