This code sample is a Daemon Watcher first developed by Tim Baker when we worked at Cox Interactive Media. I liked it so much I made some improvements here and there, and now use it as standard tool for all system installs. This version came straight from a Solaris 8 machine. Generally the cron looks like
#Daemon checker 15,30,45,59 * * * * /admin/global/bin/check_daemon.pl >> /var/adm/log/check_daemon.log
Don't forget to rotate the logs this generates.
#!/bin/perl
# ========================================================================
# File : check_daemons.pl
# Purpose : Runs from cron.
# Author: Tim Baker & Mike Tremaine mgt-@@-stellarcore.net
# ========================================================================
# This is cron'd to run once every 15mins (0,15,30,45)
use strict;
&init_global();
#Check Daemons everytime we run
foreach my $daemon( keys %{ $global::daemons } ) {
&check_daemon( $daemon );
}
exit;
# ------------------------------------------------------------------------
# init_global() - setup the Lumberjack stream
#
# in : none
# out : none
# glob : Lots
# err : none
# notes : none
# ------------------------------------------------------------------------
sub init_global {
#Logs
$global::log_file = "check_daemon.log";
$global::log_dir = "/var/adm/log/check_daemon.log";
$global::run_dir = "/var/run";
#Commands
$global::p_rm = "/bin/rm";
$global::p_xargs = "/bin/xargs";
$global::p_find = "/bin/find";
$global::ps_cmd = '/bin/ps';
$global::ps_flags = '-f -p';
$global::grep_cmd = '/bin/grep';
$global::grep_flags = '';
#Daemon Config
# Info about the daemons to watch
$global::daemons = {
prngd => {
greptest => 'prngd',
pidfile => "$global::run_dir/prngd.pid",
command => "/etc/init.d/prngd start"
},
sshd => {
greptest => 'sshd',
pidfile => "$global::run_dir/sshd.pid",
command => "/etc/init.d/sshd start"
},
sendmail => {
greptest => 'sendmail',
pidfile => "$global::run_dir/sendmail.pid",
command => "/etc/init.d/sendmail start"
}
};
#Time values for functions
my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
$year = $year + 1900;
$mon = $mon + 1;
if ($min < 10) { $min = "0" . $min; };
if ($mon < 10) { $mon = "0" . $mon; };
if ($mday < 10) { $mday = "0" . $mday; };
if ($hour < 10) { $hour = "0" . $hour; };
$global::timestamp = "[" . $mon . "/" . $mday . "/" . $year . " " . $hour . ":" . $min . " ]";
} # sub init_global
#######################################
#Check Daemons
sub check_daemon {
my $daemon = shift;
my $grep_test = $global::daemons->{$daemon}->{greptest};
my $pid_file = $global::daemons->{$daemon}->{pidfile};
# if there's a PID file , see if that process is running
if( open( PID, "< $pid_file" ) ) {
chomp( my $pid =
|
||||