PDA

View Full Version : Perl Script Help



codontek
16th August 2004, 12:34 PM
In my office we are running httpd and mysqld deamons. I want a PERL script that I can put in CRON. I want the script in a way that it will check if "httpd or mysqld" service are running. If not it should send a mail to the root. The cron will check for this every 10 minutes.

Can anyone help me very urgently?

This is my new job and my boss has asked me that now.

Thanks.
Codontek

rolandcl
16th August 2004, 07:19 PM
Try this,

You have to install perl-Mail-Sendmail

#!/usr/bin/perl -w

use Mail::Sendmail;

if ( `/etc/init.d/mysqld status` =~ /^mysqld.*is running/ ) {
`rm /tmp/mysqld_lock` if ( -e '/tmp/mysqld_lock' );
}
elsif ( not -e '/tmp/mysqld_lock' ) {
%mail = ( To => 'you@there',
From => 'me@here',
Subject => "mysqld crash on $ENV{HOSTNAME} !",
Message => "It seems that mysqld has unexpectedly stopped on $ENV{HOSTNAME}" );
sendmail(%mail);
print "Send mail\n";
`touch /tmp/mysqld_lock`;
}


if ( `/etc/init.d/httpd status` =~ /^httpd.*is running/ ) {
`rm /tmp/httpd_lock` if ( -e '/tmp/httpd_lock' );
}
elsif ( not -e '/tmp/httpd_lock' ) {
%mail = ( To => 'you@there',
From => 'me@here',
Subject => "httpd crash on $ENV{HOSTNAME} !",
Message => "It seems that httpd has unexpectedly stopped on $ENV{HOSTNAME}" );
sendmail(%mail);
print "Send mail\n";
`touch /tmp/httpd_lock`;
}

Jman
17th August 2004, 04:28 AM
Moved to Programming forum.

ghenry
27th August 2004, 12:09 PM
rolandcl:

Do you mind if I steal your script? I have cleaned it up a bit too (lexical variables). I hope you don't mind?

One, question, if the servers are running, why would you want to remove the lock file?



#!/usr/bin/perl -w
# Script as posted in Fedoraforum.org by ronalcl and modified by GH
#
# <ghenry[AT]suretecsystems.com
#
# http://www.suretecsytems.com
#
# 27.08.04 - GH
# Changed lock file locations to work with Fedora
# and made script strict
#

use warnings;
use strict;
use Mail::Sendmail;

if ( `/etc/init.d/mysqld status` =~ /^mysqld.*is running/ ) {
# Not sure the point of this?
#`rm /var/lock/subsys/mysqld` if ( -e '/var/lock/subsys/mysqld' );
print "Everything looks fine for mysqld on $ENV{HOSTNAME}, e-mail sent for confirmation\n";
my %mail = ( To => 'user@user.com',
From => 'user@user.com',
Subject => "mysqld is OK on $ENV{HOSTNAME} !",
Message => "It seems that mysqld is still running on $ENV{HOSTNAME}\n");
sendmail(%mail);
}
elsif ( not -e '/var/lock/subsys/mysqld' ) {
my %mail = ( To => 'user@user.com',
From => 'user@user.com',
Subject => "mysqld crash on $ENV{HOSTNAME} !",
Message => "It seems that mysqld has unexpectedly stopped on $ENV{HOSTNAME}\nRestarting..." );
sendmail(%mail);
print "An e-mail has been sent as problems were found with mysqld.\nI will try to restart mysqld anyway.\n";
`/etc/init.d/mysqld restart`;
}


if ( `/etc/init.d/httpd status` =~ /^httpd.*is running/ ) {
# Not sure teh point of this?
#`rm /var/lock/subsys/httpd_lock` if ( -e '/var/lock/subsys/httpd' );
print "Everything looks fine for httpd on $ENV{HOSTNAME}, e-mail sent for confirmation\n";
my %mail = ( To => 'user@user.com',
From => 'user@user.com',
Subject => "httpd is OK on $ENV{HOSTNAME} !",
Message => "It seems that httpd is still running on $ENV{HOSTNAME}\n");
sendmail(%mail);
}
elsif ( not -e '/var/lock/subsys/httpd' ) {
my %mail = ( To => 'user@user.com',
From => 'user@user.com',
Subject => "httpd crash on $ENV{HOSTNAME} !",
Message => "It seems that httpd has unexpectedly stopped on $ENV{HOSTNAME}\nRestarting..." );
sendmail(%mail);
print "An e-mail has been sent as problems have been found with httpd.\nI will try to restart httpd anyway.\n";
`/etc/init.d/httpd restart`;
}