#!/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 # --------------------------------------------------- # Last updated: Jun - 15 - 2009. ports="22 80 25" # service names as per above ports service="SSH WEB MAIL" #Email id to send alert ADMINEMAIL="admin@myispname.com" #Bin paths, set them according to your Linux distro NETSTAT=/bin/netstat MAIL=/bin/mail LOGGER=/bin/logger #Counters, set defaults c=1 status="" sendmail=0 # set the following to 1, if you want message in /var/log/messages via a SYSLOG logtosyslog=0 # Log file used to send an email LOG="/tmp/services.log.$$" # log message to screen and a log file log(){ echo "$@" echo "$@" >> $LOG } # log message and stop script die(){ echo "$@" exit 999 } # Look out for all bins and create a log file init_script(){ [ ! -x $MAIL ] && die "$MAIL command not found." [ ! -x $NETSTAT ] && die "$MAIL command not found." [ ! -x $LOGGER ] && die "$LOGGER command not found." >$LOG } # check for all running services and shoot an email if service is not running chk_services(){ log "-------------------------------------------------------------" log "Running services status @ $(hostname) [ $(date) ]" log "-------------------------------------------------------------" # get open ports RPORTS=$($NETSTAT -tulpn -A inet | grep -vE '^Active|Proto' | awk '{ print $4}' | cut -d: -f2 | sed '/^$/d' | sort -u) # okay let us compare them for t in $ports do sname=$(echo $service | cut -d' ' -f$c) echo -en " $sname\t\t\t : " echo -en " $sname\t\t\t : " >> $LOG for r in $RPORTS do if [ "$r" == "$t" ] then status="YES" sendmail=1 break fi done echo -n "$status" echo "" echo -n "$status" >>$LOG echo "" >>$LOG # Log to a syslog /var/log/messages? # This is useful if you have a dedicated syslog server [ $logtosyslog -eq 1 ] && $LOGGER "$sname service running : $status" # Update counters for next round c=$( expr $c + 1 ) status="NO" done log "-------------------------------------------------------------" log "This is an automatically generated $(uname) service status notification by $0 script." if [ $sendmail -eq 1 ]; then $MAIL -s "Service Down @ $(hostname)" $ADMINEMAIL < $LOG fi } ### main ### init_script chk_services ### remove a log file ### [ -f $LOG ] && /bin/rm -f $LOG
Want to read Linux tips and tricks, but don't have time to check our blog everyday? Subscribe to our email newsletter to make sure you don't miss a single tip/tricks.
- Download Script
- Email this to a friend
- Rss Feed
- Last Updated: 06/15/09
{ 10 comments… read them below or add one }
Hey, this script not workin under CentOS 4.7, script messaged all service is down:
-bash-3.00# sh test_script.sh
Running services status:
SSH: No
WEB: No
MAIL: No
-bash-3.00#
But all services works fine, can you help me?
Try updated script. Let me know if you need any other help.
Hey,
Thanks, now it work, but I have issue with only ssh. I run sshd service on non-standart port, and replace 22 port with my settings. After thant when I run this script I got blank value.
————————————————————-
Running services status @ server.****.com [ Thu Jun 18 19:07:24 EEST 2009 ]
————————————————————-
SSH :
WEB : YES
MAIL : YES
————————————————————-
This is an automatically generated Linux service status notification by checkstatus script.
And got fake alert :( Can you help me?
Replace port 22 with your actual port such as #3922 in $ports i.e. from
To:
Yeah, I do that at once, but still get nothing about ssh
-bash-3.00# sh /srv/chkserv
————————————————————-
Running services status @ server.*****.com [ Fri Jun 19 12:08:57 EEST 2009 ]
————————————————————-
SSH :
WEB : YES
MAIL : YES
————————————————————-
This is an automatically generated Linux service status notification by /srv/chkserv script.
-bash-3.00# grep Port /etc/ssh/sshd_config
Port 22
#GatewayPorts no
-bash-3.00# ps aux|grep ssh
root 16151 0.0 0.0 7004 2284 ? Ss 12:07 0:00 sshd: root@ttyp0
root 23653 0.0 0.0 4624 632 ttyp0 S+ 12:09 0:00 grep ssh
root 28047 0.0 0.0 4140 1044 ? Ss Jun18 0:00 /usr/sbin/sshd
Correct way to run script is either:
chmod +x /srv/chkserv/srv/chkserv
OR
bash /srv/chkservTo find current sshd port enter:
netstat -tulpn-bash-3.00# chmod +x /srv/chkserv
-bash-3.00# /srv/chkserv
————————————————————-
Running services status @ server.*****.com [ Fri Jun 19 13:53:11 EEST 2009 ]
————————————————————-
SSH :
WEB : YES
MAIL : YES
————————————————————-
This is an automatically generated Linux service status notification by /srv/chkserv script.
-bash-3.00# bash /srv/chkserv
————————————————————-
Running services status @ server.*****.com [ Fri Jun 19 13:53:23 EEST 2009 ]
————————————————————-
SSH :
WEB : YES
MAIL : YES
————————————————————-
This is an automatically generated Linux service status notification by /srv/chkserv script.
-bash-3.00# netstat -tulpn |grep ssh
tcp 0 0 :::22 :::* LISTEN 9262/sshd
-bash-3.00# grep port /srv/chkserv
ports=”22 80 25″
Any idea?
So now, I replace Ports in script to:
ports=”80 25 22″
And Services to:
service=”WEB MAIL SSH”
And re-run script:
-bash-3.00# /srv/chkserv
————————————————————-
Running services status @ server.*****.com [ Fri Jun 19 14:27:53 EEST 2009 ]
————————————————————-
WEB : YES
MAIL : YES
SSH : NO
————————————————————-
This is an automatically generated Linux service status notification by /srv/chkserv script.
I don’t know why :(
Ok, I will look at the script this week end and see if there is any bug that is shooting an email even if service is running.