Operating System - Linux
1753288 Members
5412 Online
108792 Solutions
New Discussion юеВ

Re: Need a Shell Script to do this job. If anybody have it handy,let me know.

 
SOLVED
Go to solution
Silver_1
Regular Advisor

Need a Shell Script to do this job. If anybody have it handy,let me know.

We have 20 HPUX and 115 Linux severs and many of them are in clusters.

We would like to do a simple monitoring script which can do a ping to all these servers and the package ips, if there is no resonse, it has to pages admins. Also once the machines which was not pingable for last five minutes is back up after a reboot or so, page saying that the machine is up.

Any help is appreciated.
4 REPLIES 4
baiju_3
Esteemed Contributor
Solution

Re: Need a Shell Script to do this job. If anybody have it handy,let me know.

Hi Nair ,

Hosts.txt
----------------
host1 ip1
host2 ip2
etc ...

watch_net.sh
#!/usr/bin/sh

cat Hosts.txt |while read HOST IP;do
ping $IP -n 2 >/dev/null
if [ $? -ne 0 ]

mailx -s "$Host($IP) Not Pinging" support@domain.com
fi
done

Regards...bl.
Good things Just Got better (Plz,not stolen from advertisement -:) )
James R. Ferguson
Acclaimed Contributor

Re: Need a Shell Script to do this job. If anybody have it handy,let me know.

Hi Nair:

Here's a basic monitor script. Start it and it runs infinitly, waking up every 300-seconds. If a server is responsive to a 'ping' but was not in the previous pass, an email is generated. If a server isn't responsive but was before an email is also composed.

The list of servers can be by name and/or address, one per line in a file named '/tmp/servers'.

# cat monservers
#!/usr/bin/perl
#@(#) Simple server monitor - JRF

use strict;
use warnings;

my $hosts = "/tmp/servers";
my %servers;
my ($host, $state, $resp);

open ( FH, "<", "$hosts" ) or die "Can't open $hosts: $!";
while () {
chomp;
$servers{$_} = 1;
}

while (1) {
foreach $host (keys %servers) {
$state = $servers{$host};
$resp = system("ping $host -n 1 > /dev/null 2>&1");

if ($resp == 0 && $state == 0) {
$servers{$host} = 1; #...server now up again...
`mailx -s "$host back up" root < /dev/null`
}
if ($resp != 0 && $state == 1) {
$servers{$host} = 0; #...server has gone down...
`mailx -s "$host is down" root < /dev/null`
}
}
sleep 300
}
1;

Regards!

...JRF...
Silver_1
Regular Advisor

Re: Need a Shell Script to do this job. If anybody have it handy,let me know.

Thanks for your inputs. Scipt is working.
Stuart Browne
Honored Contributor

Re: Need a Shell Script to do this job. If anybody have it handy,let me know.

Given the sheer volume of machines, and the fact that monitoring via ping is only so good for so long, might it not be an idea to use a monitoring package?

Set nagios up once with all your machines, and you'll even get nice emails, paging, hell even WML pages for your mobile phone with status messages..

Anyway ;)

I'm kinda a nagios fan now. I admit I only use it to monitor 25 systems, but it's pretty good for keeping track of things.
One long-haired git at your service...