NAVIGATION
Home
Gallery
Java
Linux
Web
Scripts And Utilities
Mobile And Sms
Misc
Contact
pixelWIKI
Nabaz Tag




<<

Shell Script Web Site Monitor

A (very) rudimentary shell script to monitor a web site and do something in the event of failure and recovery - run it from a cron job. Create a text file on the site you want to monitor that just includes the word "UP". This script will send an email after 3 consecutive failures, and another email upon recovery.


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:
#! /bin/sh

# number of times to test before failing
FAIL_COUNT=3

#page to test
STATUS_PAGE=http://www.some_website.com/status.txt

# email address to send notification to
EMAIL_ADDRESS=my_email_address


touch status.chk
if [ "`curl $STATUS_PAGE | grep "UP" | wc -l`" == "1" ] ; then
        if [ "`grep "DOWN" status.chk | wc -l`" != "0" ] ; then
                echo "Recovered at `date`" >> status.log
                echo "Site ($STATUS_PAGE) recovered at `date`" | mail -s "Site Recovered" $EMAIL_ADDRESS
        fi
        > status.chk
else
        echo `date` >> status.chk
        if [ "`cat status.chk | wc -l`" == "$FAIL_COUNT" ] ; then
                echo "DOWN" >> status.chk
                echo "Failed at `date`" >> status.log
                echo "Site ($STATUS_PAGE) down at `date`" | mail -s "Site Down" $EMAIL_ADDRESS
        fi
fi