Monday, March 10, 2014

Bash Script To Monitor Your Web App


Assume you have a web application deployed on some Linux Server(s) and you want to get an email notification if for some reason your web app went down and stopped providing service. The most straightforward and easy way is to create a cronjob on the server to poll the service status using a HTTP client (wget). Recently we run into this kind of need and created a very simple (and crude and primitive and pathetic) bash script to poll an URL on periodic intervals and based on the http response send an email to the defined mail distribution list to inform people about service status. You can find the bash script below. I need to admit it is very primitive but yet its working for us so far. (Except the flooding of the emails when the server is down)

1.) ssh to your server and open "vim" paste below script and save it with some name.

#!/bin/bash
MAIL_LIST="xx.yy@cc.com aa@bb.tt ll@bb.cc.com"
APPLICATION_URL=http://xxx.yy.zz.tt:8080/core

wget -q -o /dev/null --spider --no-check-certificate $APPLICATION_URL

if [ $? -eq 0 ]
then

MSG="`date` SUCCESS $APPLICATION_URL accessible!"
echo "$MSG" >> log_vulture_watch.log
else

MSG="`date` ERROR $APPLICATION_URL not accessible!"
echo "$MSG" >> log_vulture_watch.log
echo "Unable to access $APPLICATION_URL " | mail -s "Vulture_Watch Server Monitor" $MAIL_LIST
fi


2.) cd to the directory where the script file is located and make it executable. chmod 777 <scriptname>


3.) Type crontab -e and enter a line consisting of time period, and command and then save your settings. type crontab -l to list cronjobs you should see the task you just defined.


# Edit this file to introduce tasks to be run by cron.
#
# Each task to run has to be defined through a single line
# indicating with different fields when the task will be run
# and what command to run for the task
#
# To define the time you can provide concrete values for
# minute (m), hour (h), day of month (dom), month (mon),
# and day of week (dow) or use '*' in these fields (for 'any').#
# Notice that tasks will be started based on the cron's system
# daemon's notion of time and timezones.
#
# Output of the crontab jobs (including errors) is sent through
# email to the user the crontab file belongs to (unless redirected).
#
# For example, you can run a backup of all your user accounts
# at 5 a.m every week with:
# 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/
#
# For more information see the manual pages of crontab(5) and cron(8)
#
# m h  dom mon dow   command

*/1 * * * * /home/ososapp/rscripts/vulture_watch
ososapp@ososapp:~$


You can find plenty of resources about crontab over the internet (http://unixhelp.ed.ac.uk/CGI/man-cgi?crontab+5 ), but what above line says is run the script every minute. (Please pay attention to the "/"  character before the number 1 , if you don't put it your script wont run every single minute.). The last part of the script (/home/ososapp/rscripts/vulture_watch) is the path to your bash script file. (in my case  vulture_watch is the script file name and it is located in directory /home/ososapp/rscripts )

4.) You are done :) enjoy.



No comments:

Post a Comment