Simple Host Monitoring with SSH
There are many complex host and service monitoring solutions available for Unix systems. But sometimes you just need something very simple to monitor a server or an application on a temporary basis. A basic ping monitor is fine, but it will only tell you if a server is responding on the network. It will not tell you if there is some other problem on the system. The script below relies on passwordless SSH setup to periodically log into the monitored nodes and check on their health by executing a local or remote script.
There are two pre-requisites to make this method work. First, you need to configure passwordless SSH from the monitoring host to the monitored client. Instructions are available here. Second, for notification this script uses “mailx”, so you need sendmail configured on the monitoring host to enable it to send you emails. Test both passwordless SSH and email (example: echo “text” | mailx -s “test email from hostname” your.email@domain.com
Save the script below as hostmon.ksh; chmod 755; and run as “nohup /full/path/to/hostmon.ksh /dev/null 2>&1 &”. The script will loop through a list of monitored clients. If a problem is detected, it will send you an email. At this point, the script will stop monitoring this particular client, so you will not receive duplicate emails.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
#!/bin/ksh # # Simple remote host monitor using SSH. Requires passwordless SSH setup. # configure() { remotehosts="client1 client2 client3" interval=300 email="your.email@domain.com" } sshmon() { for i in {remotehosts} do status=1 ssh ${i} "uptime" ; status=$(echo $?) if [ ${status} -ne 0 ] then echo "`date` - Can't communicate with ${i}!" echo "`date` - Can't communicate with ${i}!" | mailx -s "hostmon error on ${i}" "${email}" remotehosts=$(echo "{remotehosts}" | sed "s/${i}//g" | sed 's/ //g') if [ `echo ${remotehosts} | wc -m | awk '{print $1}'` -lt 3 ] then exit fi fi done } configure while [ 1 ] do sshmon sleep ${interval} done |
-
Sergio
-
davemc74656
-
Heath
-
Con Orpe
-
Joe M
-
Brian
