Here is the full code we'll be working with:
#!/usr/bin/perlPlace this script on a cron job that runs every minute on the system you want to monitor. It's very simple - given a list of processes, the script will make sure that they are currently active. If any of them come back as inactive, it sends out a one line warning email to the configured $alert_email letting you know what process is dead on which host. Not exactly enterprise-level, but it works great in a pinch! :)
use strict;
my @services = ( 'mongrel_rails', 'apache2', 'mysql' );
my $alert_email = 'your-email-here\@your-domain-name';
my $host = `/bin/hostname`;
chomp $host;
foreach my $service (@services) {
my $status = `/bin/ps cax | /bin/grep $service`;
if (!$status) {
my $alert = `/usr/bin/mailx -s "ALERT! $host: $service stopped" $alert_email < /dev/null > /dev/null`;
}
}
Let's take a closer look at the configuration:
my @services = ( 'mongrel_rails', 'apache2', 'mysql' );Don't worry about the hostname, it should pull that automatically based on your system configuration. The first thing to configure is the list of processes that you want to monitor. These should be based on the actual process names - if you're unsure of that, run /bin/ps cax on your system while the services you want to monitor are active and you can see exactly what you need to use. In the example above, I'm watching 3 processes - mongrel, apache, and mysql (it's a Ruby on Rails server).
my $alert_email = 'your-email-here\@your-domain-name';
$alert_email should obviously contain your email address, or the email address you want to get flooded with ALERT messages when a process dies. Once you have the script all configured, give it a run and see if it works. Of course, if you run it and all the services are active, it will do nothing, so kill one of the services you're monitoring and run it again to make sure you're getting the emails.
The meat of the script is just a shell command that lists the currently running processes, and greps for each monitored service in turn. If it finds it, the $status has a value and returns true. If the process doesn't exist, $status will return false and the email should go out.
my $status = `/bin/ps cax | /bin/grep $service`;And that's it! Happy service monitoring!
if (!$status) {
my $alert = `/usr/bin/mailx -s "ALERT! $host: $service stopped" $alert_email < /dev/null > /dev/null`;
}
