Friday, August 13, 2010

Perl Ping

Hai
After a lot of frustration i have build this code to check which of the server is down and which is up.
These are the requirements needed to run this code.
1. Linux machine,
2. Perl >5.8
3. Sql server >5.1
4. XML::LibXML module in perl
5. DBI module
6. sendmail need to installed and service need to be running.

What is the use of this Application:
So what this will do is what ever servers you need to check for ping add them to the servers.xml one by one.If a server is down it will send a mail to the user stating that the server is down. Once that server comes online it will send you a mail stating that the server is up and online. It will make a log in the table when the server went OFFLINE and ONLINE for future reference.
INSTALLATION:
I am giving a sample xml file along with this one.
Create a database in the localhost server. The db structure is also there along with the code.
Now copy the ping.pl code to your local server and run it . U can run it as a cron or can be run as a service in linux as well.
CODE::
ping.pl::

#for Linux Machines
use DBI;
use XML::LibXML;
$to_email = "dr.virus.india\@gmail.com"; # \ should be given before the @ symbol
$from_email = "chaitu\@server.com"; # \ should be given before the @ symbol
my $app_path = substr($0,0,rindex($0,'/'));
$serverxml = $app_path."/servers.xml";
print "XML NAME=> ".$serverxml."\n";
my $dsn = "dbi:mysql:ping:localhost:3306";
my $dbh = DBI->connect($dsn,"root","") or die ("Cannot connect to database");
my @filearray;
my $parser = XML::LibXML->new();
eval{
my $doc= $parser->parse_file($serverxml);
my $root = $doc->getDocumentElement;
foreach my $file ($root->findnodes('/serverdetails/server')){
#print $file->findvalue('.')."\n";
push(@filearray,$file->findvalue('.'));
}
};
if($@)
{
print "Cannot read XML\n";
}
else
{
foreach(@filearray){
$variable = "$_";
my @split = split(',',$variable);
$server_name = $split[0];
$server_ip = $split[1];
$q = "select sno from check_table where client_nme='".$server_name."' and client_ip='".$server_ip."'";
$xt = $dbh->prepare($q);
$xt->execute();
$sno = $xt->fetchrow_array();
if($sno eq ''){
$qu = "insert into check_table(client_nme,client_ip,status_) values('".$server_name."','".$server_ip."','0')";
$dbh->do($qu);
}
print "Server Name=> ".$server_name."\nServer Ip =>".$server_ip."\n";
$pingstats = `ping -c 4 $server_ip |grep received`;
#print "Grep Details=> ".$pingstats."\n";
my @pingvar = split(',',$pingstats);
$Received = $pingvar[1];
$Lost = $pingvar[2];
#print "Received=> ".$Received."\nLost=> ".$Lost."\n";
@receive = split(' ',$Received);
$re = $receive[0];
#print "\t\t".$re."\n";
@lo = split('%',$Lost);
$los = trim($lo[0]);
#print "\t\thh".$los."\n";
if($los >= '70'){
$r = "select status_ from check_table where client_nme='".$server_name."' and client_ip='".$server_ip."'";
#print "R==> ".$r."\n";
$re = $dbh->prepare($r);
$re->execute();
$stat_vale = $re->fetchrow_array();
if($stat_vale eq '0'){
$dbh->do("update check_table set status_ = '1',stime=now() where client_nme='".$server_name."' and client_ip='".$server_ip."'" );
my $message = "Server => ".$server_name." is Down. Check it out!!!";
sendEmail($to_email, $from_email, "SERVER DOWN.", $message);
}
else{
}
}
else{
$w = "select status_ from check_table where client_nme='".$server_name."' and client_ip='".$server_ip."'";
#print "W==> ".$w."\n";
$we = $dbh->prepare($w);
$we->execute();
$st_val = $we->fetchrow_array();
#print "St_Value==> ".$st_val."\n";
if($st_val eq '1'){
$ty = "update check_table set status_ = '0',etime=now() where client_nme='".$server_name."' and client_ip='".$server_ip."'";
#print "TY==> ".$ty."\n";
$dbh->do($ty);
$dbh->do("INSERT INTO log_table(Check_id,client_nme,client_ip,stime,etime,status_)  (SELECT sno,client_nme,client_ip,stime,etime,status_ FROM check_table WHERE client_nme='".$server_name."' and client_ip='".$server_ip."')");
my $message = "Server => ".$server_name." is Online Now. Check it out!!!";
sendEmail($to_email, $from_email, "SERVER ONLINE.", $message);
}
}
}
}
sub sendEmail
{
my ($to, $from, $subject, $message) = @_;
my $sendmail = '/usr/lib/sendmail';
open(MAIL, "|$sendmail -oi -t");
print MAIL "From: $from\n";
print MAIL "To: $to\n";
print MAIL "Subject: $subject\n\n";
print MAIL "$message\n";
close(MAIL);
}
sub trim()
{
my $string = shift;
$string =~ s/^\s+//;
$string =~ s/\s+$//;
return $string;
}
servers.xml File::




DB Structure::
USE `ping`;
/*Table structure for table `check_table` */
CREATE TABLE `check_table` (
`sno` int(7) NOT NULL AUTO_INCREMENT,
`client_nme` varchar(100) NOT NULL,
`client_ip` varchar(40) NOT NULL,
`stime` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
`etime` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
`status_` enum('0','1') NOT NULL,
PRIMARY KEY (`sno`)
) ;
/*Table structure for table `log_table` */
CREATE TABLE `log_table` (
`sno` int(7) NOT NULL AUTO_INCREMENT,
`Check_id` int(7) DEFAULT NULL,
`client_nme` varchar(100) NOT NULL,
`client_ip` varchar(40) NOT NULL,
`stime` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
`etime` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
`status_` enum('0','1') NOT NULL,
PRIMARY KEY (`sno`)
)