Thursday, March 13, 2008

Paging / monitoring service using Perl

Here I have written a small perl program which is used to read a configuration file where you can specify different types of services to be monitored and send out an email / page (email to mobile number).

This is pager.conf example


# This configuration file is used to setup paging for individual applications
# each filed is separated by one tab only.
# FIELD1 is server name
# FIELD2 is list of comma separated processes
# FIELD3 is type of process db - database proc - process mlog - message log
# FIELD4 is email address of the pager can be comma separated for multiple
# recipients.
#=============================================
# server processes type email
#=============================================
server1 db user@domail.com

Here is the actual perl program


#!/usr/bin/perl
$CONF="/path/to/pager.conf";
open(F,"<$CONF") or die "Cannot open $CONF\n";

# Setting up default email.
$to=="user\@domain.com";
$from="pager\@server.com";
$subject="Paging default";
$out = "Default message!";

$line= < F>
while($line)
{
chomp($line);
# Ignore Comments
if($line =~ m/^#/)
{
$line= < F>
next;
}
#Ignore Empty lines
if($line =~ m/^(\s)*$/)
{
$line= < F>
next;
}
# Get server proc type and email information from config file
($server,$proc,$type,$email)=split(/\t/,$line);
@proclist=split(/,/,$proc);
#print "Server=$server\nproc=$proc\ntype=$type\nemail=$email\n";
foreach $i (@proclist)
{
# if it is a database check whether its up or not send out email alerts if it is down
if($type eq "db")
{
$ret=system("Your Database related checking script/ command");
#print "return = $ret\n";
if( $ret != 0 )
{
$to=$email;
$subject="$server $i $type is down";
$out="Paging service Info:\n$server $i $type is down.\nsincerely,\nPager\n";
email_alert();
}
}
elsif($type eq "proc")
{
# if it is a process check whether its running or not send out email alerts if it is down
$ret=system("Your process related script / command");
#print "return = $ret\n";
if( $ret != 0 )
{
$to=$email;
$subject="$server $i $type is down";
$out="Paging service Info:\n$server $i $type is down.\nsincerely,\nPager\n";
email_alert();
}
}
elsif($type eq "mlog")
{
$ret=system("Your log search script / command");
#print "return = $ret\n";
if( $ret == 0 )
{
$to=$email;
$subject="$server $i has error";
$out="Paging service Info:\nOn $server $i log has error in it.\nsincerely,\nPager\n";
email_alert();
}
}

}
$line= < F>
}

#Function that sends email look at the configuration for default email at the top of the script

sub email_alert
{
#print "entered email\n";
#print "$to\n$from\n$subject\n$out\n";
# send email using UNIX/Linux sendmail
open(MAIL, "|/usr/sbin/sendmail -t");

## Mail Header
print MAIL "To: $to\n";
print MAIL "From: $from\n";
print MAIL "Subject: $subject\n";

## Mail Body
print MAIL $out;
close(MAIL);
}


This script could give you a starting point to add more services. Once it is done run it as a cron job every x amount of time where x is your interval at which your want to test for these services. On my side I test them every 5 minutes. with a cron entry .

No comments: