#!/bin/perl 
# 
# name     : monitor_processes.pl
#
# History :
#            <1> V1.1 Sean Boran
#		 FCS
#
# FUNCTION: 	Check to see if a list of processes are running.
#		If not, send a message to syslog (if syslog is not
#		running, send a mail).
#		[syslogd is monitored even if not listed]
#		The list is given on the command line, but also
#		has defaults set below.
#		Runs on solaris 1 &2.
#
 

$user = 'root';

# --- security precautions ---
$ENV{'PATH'} = '/usr/bin';
$ENV{'SHELL'} = '/bin/sh' if $ENV{'SHELL'} ne '';
$ENV{'IFS'} = '' if $ENV{'IFS'} ne '';
umask(077);				# -rw-------

# ----------------- variable setup  ---------------
$debug = '';				# '1' for debug, '' for no debug info
$host = `uname -n`;
$subject = "Processes dead on $host";
chop($host);
$syslog_priority = 'warning|daemon' ;

@proc_list = @ARGV;
if (@proc_list == 0) {			# are there any args ?
	@proc_list = ( 			# no, set defaults
	'inetd','sendmail',
	'monitor_socket.pl');	
}
push(@proc_list, 'syslogd');		# ALWAYS check for syslog - we use it!

print "Searching for: @proc_list\n" if $debug;
foreach $process (@proc_list) {
	$event_count{$process} = 0;
}

chop($os=`uname -r`);
if ($os =~ /4\.1\.\d/) {
    $mail='/usr/ucb/mail';		
    $ps_options = '-ax';
    print "OS= 4.1.x\n" if $debug;
}
elsif ($os =~ /5\.\d/) {			# assume Solaris 2 (SVR4)
    $mail='/usr/bin/mailx';
    $ps_options = '-ef';
    print "OS= Solaris 2.x\n" if $debug;
}
else {
    print "OS= $os is not supported!\n";
    exit -1;
}


# ---------- call "ps" & analyse output  -------------
open(PS, "/usr/bin/ps $ps_options |") 
    || die "can't run ps: __FILE__ $!\n";

$/="\n";					# record seperator
while ($_ = <PS>) {

	$pattern = '(.+ +\d+:\d\d )(.*)'; 	
	# $1 = anything spaces manydigits : digit digit onespace <2>
	# $2 = rest of line

    	if (/$pattern/) {		 
#	    print "Process: '$2'\n" if $debug;
	    foreach $process (@proc_list) {
	    	if ($2 =~ /$process/) {
		    $event_count{$process}++;	# count occurrences
		}
	    }
	}
}
close(PS);

	# now inform about any process not running.
	# If syslog is running use 'logger' else
	# send email to root

$tmp_var="";
foreach $process (@proc_list) {
  if ($event_count{$process} == 0) {		
	$tmp_var = $tmp_var . 
	    "WARNING: Process '$process' is NOT running!\n";
  }
}

if ($event_count{'syslogd'} != 0){			# syslog OK!
    # syslog is last in @proc_list
    # &syslog() doesn't work on solaris 2..

    foreach $process (@proc_list) {
	if ($event_count{$process} == 0) {
	    if ($debug) {
		print "WARNING: Process '$process' is NOT running!\n" if $debug;
	    }
	    else {					
		system("/usr/ucb/logger -p daemon.err ".
		    "Process: '$process' NOT running!\n");
                ## send an email too:
	        system "echo '$tmp_var' | $mail -s '$subject' $user";
	    }
	}
	elsif ($debug) {
	    print "Process '$process' occurred $event_count{$process} times\n";
	}
    }
}
else {							# syslog is dead!
    system "echo '$tmp_var' | $mail -s Processes_dead $user";
}


exit 0;
#EOF
