Shell script to monitor services such as web/http, ssh, mail server
Posted in Monitoring
#!/bin/bash # Shell script to monitor running services such as web/http, ssh, mail etc. # If service fails it will send an Email to ADMIN user # ------------------------------------------------------------------------- # Copyright (c) 2006 nixCraft project <http://www.cyberciti.biz/fb/> # This script is licensed under GNU GPL version 2.0 or above # ------------------------------------------------------------------------- # This script is part of nixCraft shell script collection (NSSC) # Visit http://bash.cyberciti.biz/ for more information. # ---------------------------------------------------------------------- # See URL for more info # http://www.cyberciti.biz/tips/processing-the-delimited-files-using-cut-and-awk.html # --------------------------------------------------- # service port ports="22 80 25" # service names as per above ports service="SSH WEB MAIL" # No of services to monitor as per (above ports+1) SCOUNTER=4 #Email id to send alert ADMINEMAIL="admin@myispname.com" # counter c=1 echo "Running services status:" # use sudo if you want i.e. sudo /bin/netstat /bin/netstat -tulpn | grep -vE '^Active|Proto' | while read LINE do sendMail=0 # get active port name and use : as delimiter t=$(echo $LINE | awk '{ print $4}' | cut -d: -f2) [ "$t" == "" ] && t=-1 || : # get service name from $services and : as delimiter sname=$(echo $service | cut -d' ' -f$c) sstatus="$sname: No" # now compare port for i in $ports do if [ $i -eq $t ]; then sstatus="$sname: Ok" sendMail=1 fi done # display service status as OK or NO echo "$sstatus" #next service please c=$( expr $c + 1 ) [ "$sendMail" == "0" ] && echo $sstatus | mail -s "service down $sstatus" $ADMINEMAIL || : # break afer 3 services [ $c -ge $SCOUNTER ] && break || : done
Download - Email this to a friend - Printable version
Is your site working? Monitor Your Web Site 24/7. Get SMS alerts on server downtime! Free 30-day trial including 20 SMS!
Related Other Helpful Shell Scripts:
- Shell Script To Auto Restart Apache HTTPD When it Goes Down / Dead
- Shell Script To Bridge Multiple Network Interfaces on Xen host to Guests VPS
- Monitor UNIX / Linux Server Disk Space with Shell Script
- NAS Backup Server Disk Monitoring Shell Script
- Shell script to General FreeBSD and Linux System information
Leave a Reply
We encourage your comments, and suggestions. But please stay on topic, be polite, and avoid spam. Thank you very much for stopping by our site!
Tags: adminemail, delimited, delimiter, echo line, echo service, expr, for loop, grep, loop method, mail service, mail services, monitor mail server, monitore web server, script collection, sendmail, service names, service status, shell loops, sudo, web mail, while loop ~ Last updated on: April 10, 2008

