[tt]
================================================================================
#!/usr/contrib/bin/perl
#
# p_check.pl
#
# Checks that a list of processes are running, prints an message on STDOUT
# for processes that are not.
#
# This script is designed to be run by a HP/OVO logfile processor; it should
# run pretty much as is on any unix platform with perl install, the ps
# command may have to be modified - but not much.
#
# Run the script using a template and direct its output to a file, monitor
# that file using the conditions searching for lines in the log starting
# with ERROR:
#
# usage: p_check.pl proc1 count proc2 count proc3 count
# eg: p_check.pl sendmail 1 vhand 1 user_proc 3
# The example above would check for a sendmail process, a vhand process and
# three copies of user_proc.
#
use strict;
my $ps_cmd = 'ps -e'; # get all running processes
my %ps_list;
my ($proc, $count);
print "OK: Checking command line.\n";
while(@ARGV){
$proc = shift or die; # get stuff from command line
$count = shift or die;
print "DBG: $proc, $count\n";
$ps_list{$proc} = $count; # build a hash of things to do
}
# ok, now get a process list and check for the processes we care about
print "OK: Getting process list.\n";
open(PS,"$ps_cmd |"

|| die;
while(<PS>){
(undef, undef, undef, $proc) = split;
chomp($proc);
if(defined($ps_list{$proc})){ # if it's in the list
$ps_list{$proc} --; # knock one off its count
}
}
#
# If there's anything in %ps_list with a positive value, not enough instances
# of that process are running.
#
print "OK: Reporting any errors.\n";
foreach $proc (keys %ps_list){
if($ps_list{$proc}){ # a non-zero count?
# yes, so print an error message
print "ERROR: Not enough instances of $proc are running\n"
if $ps_list{$proc} > 0;
print "ERROR: Too many instances of $proc are running\n"
if $ps_list{$proc} < 0;
} else {
print "OK: Enough instances of $proc are running\n";
}
}
print "OK: Finished\n";
exit 0;
[/tt]
Hope this is useful to you, and others. the first line needs to be changed to match where your copy of perl is stored. It requires Perl v5 as it stands, if only Perl 4 is available: delete 'use strict' line, remove the my from the 'my $ps_cmd = 'ps -e'; # get all running processes' line and delete the two other 'my' lines. Mike
"Experience is the comb that Nature gives us after we are bald."
Is that a haiku?
I never could get the hang
of writing those things.