1. Home
  2. Computing & Technology
  3. Perl

Intermediate Perl Tutorial - Monitoring Services on your Server
How to write a quick service monitoring script in Perl.

From About.com

As a web developer and system administrator, I'm responsible for running a handful of application and mail servers. There are some great enterprise-level applications out there for service monitoring (like nagios and zabbix), but they take a lot of time and configuration to get them setup and running. In a time crunch, or a situation that's not exactly 'enterprise-level', I'll toss out this quick and dirty monitoring script.

Here is the full code we'll be working with:

#!/usr/bin/perl

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`;
}
}
Place 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! :)

Let's take a closer look at the configuration:

my @services = ( 'mongrel_rails', 'apache2', 'mysql' );
my $alert_email = 'your-email-here\@your-domain-name';
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).

$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`;
if (!$status) {
my $alert = `/usr/bin/mailx -s "ALERT! $host: $service stopped" $alert_email < /dev/null > /dev/null`;
}
And that's it! Happy service monitoring!
More Perl Quick Tips
Explore Perl
About.com Special Features

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

Easy ways to connect two computers for networking purposes. More >

  1. Home
  2. Computing & Technology
  3. Perl
  4. Perl Tutorials
  5. Perl tutorial - How to write a quick service monitoring script in Perl.

©2009 About.com, a part of The New York Times Company.

All rights reserved.