I wrote this script because at some moment on my server, services such as apache2 would just go off and the reason up to now, I havent discovered.
The script can be generalized to monitor other services including postgresql, mysql …
- The Idea
The original idea was this, when a service transits from one state (on) to another state (off), email me. The difficulty I faced here is that I had to have a variable that could keep track of state transition.
Later, I just decided to check only for services that are unavailable. If unavailable, the script directly restarts my service. The idea was from here.
The most important part is to say that a service is unavailable in shell script language. This is it:
if (( $(ps -ef | grep -v grep | grep $service | wc -l) == 0 ))
This translates to English Language as
- get all currently executing processes excluding the grep process itself (ps -ef | grep -v grep)
- from this list identify ONLY $service. (grep $service)
- if this set returns nothing then our service is not on (wc -l, count the number of lines outputted. If no line outputted, then service is not on)
Another important part is how to notify the sysadmin. I used the mail utility. For test purposes, I configured my gmail account as a relay host (Mail Transfer Agent, MTA) by following this tutorial.
The mail utility is abit tricky more on it could be found here.
2. The Script
Overall, this is the script:
#!/bin/bash
#Author: Randy on 12/12/2017
#Purpose: check unavailability of services and alert sysadmin
#Prerequisite: run script as root
clear;#get the system’s host name
host=$(hostname -f)#destination mail
email=DESTINATION@DESTINATION_DOMAINfor service in postgresql apache2 mysql
do#get all currently executing processes excluding the grep process itself
#from this list identify ONLY $service.
#if this set returns nothing then our service is not on
if (( $(ps -ef | grep -v grep | grep $service | wc -l) == 0 ))
then
/etc/init.d/$service start
if (( $(ps -ef | grep -v grep | grep $service | wc -l) > 0 ))
then
#alert system admin
subject=”$service at $host has been started”
echo “$service at $host was not running and has been started!!!” | mail -s “$subject” $emailelse
subject=”$service at $host is not running!!!”
echo “$service at $host is stopped and cannot be started!!!” | mail -s “$subject” $email
fi
fi
done
the output you get in your mail is similar to this:
To get the script monitor your server every 5minutes add crontab entry:
*/5 * * * * /path/to/script
I hope that helps and as a sysadmin, be ready, to always monitor your server. If something is unclear in my explanation, don’t hesitate to drop a comment.