20

I'm a lone web developer with my own Centos VPS hosting a few small web sites for my clients. Today I discovered my httpd service had stopped (for no apparent reason - but that's another thread). I restarted it but now I need to find a way that I can be notified by email and/or SMS if it happens again - I don't like it when my client rings me to tell me their web site doesn't work!

I know there are probably many different possibilities, including server monitoring software. I think all I really need is a script that I can run as a cron job from my dev host (which is permanently running in my office) that attempts to load a page from my production server and if it doesn't load within say 30 seconds then it sends me an email or SMS. I'm pretty rubbish at shell scripting, hence this question.

Any suggestions would be gratefully appreciated.

user9517
  • 114,104
  • 20
  • 206
  • 289
Xoundboy
  • 593
  • 1
  • 9
  • 20
  • 3
    Have you looked into Nagios or Pingdom? They have that functionality built in (Well, Pingdom has SMS built in, with Nagios it requires a bit of tweeking but it is possible) – Smudge Jun 20 '11 at 14:48
  • No, I'm going to take a look now, thanks for the advice. – Xoundboy Jun 20 '11 at 14:55

9 Answers9

18

Well... The most simple script, I cam write:

/usr/bin/wget "www.example.com" --timeout 30 -O - 2>/dev/null | grep "Normal operation string" || echo "The site is down" | /usr/bin/mail -v -s "Site is down" your@e-mail.address

Add it to cron as:

* * * * * /usr/bin/wget "www.example.com" --timeout 30 -O - 2>/dev/null  | grep "Normal operation string" || echo "The site is down" | /usr/bin/mail -v -s "Site is down" your@e-mail.address

But it is too simple to tell you what the problem is if it exists.

UPD: Now this one-liner checks for a specific string on the page ("Normal operation string"), which should appear only on normal operation.

UPD2: A simple way to send the error page in the e-mail:

/usr/bin/wget "www.example.com" --timeout 30 -O - 2>/dev/null | grep "Normal operation string" || /usr/bin/wget "www.example.com" --timeout 30 -O - 2>/dev/null | /usr/bin/mail -v -s "Site is down" your@e-mail.address

It's minus is that the page is re-requested in case of first test failure. This time the request may be successful and you won't see the error. Of course, it is possible to store the output and send it as an attachment, but it will make the script more complex.

HUB
  • 6,322
  • 3
  • 22
  • 22
  • Looking interesting....so if the page doesn't load within 30 seconds an email is sent right? What if the page loads but an error message it displayed - how could this be adapted to test for a specific output? – Xoundboy Jun 20 '11 at 15:07
  • Updated the answer. But the script still doesn't send you the error. – HUB Jun 20 '11 at 15:36
  • - hi, thanks for the update - I'm struggling to get this to work on my machine - I don't think the mail part of the command is doing its thing properly - haven't got any time now to troubleshoot but will try again tomorrow or asap. – Xoundboy Jun 20 '11 at 18:12
  • Well... Try to run mail program manually and check the output. Also see "/var/log/mail" for results Check the firewall settings (access to remote port 25 should be permitted). – HUB Jun 20 '11 at 19:01
  • If you just whant to break the script, e.g. in a deployment chain, you can do "wget "www.example.com" --timeout 30 -O - 2>/dev/null | grep "Normal operation string" || echo "The site is down" | exit 1". With this you can also make as many tests after each other as you like. – Tobias Gaertner Jan 17 '20 at 09:01
8

Take a look at this script:

curl is a command-line utility to fetch a URL. The script checks the exit code ($? refers to the exit code of the most recent command in a shell script) and if it was anything other than 0, reports an error (an exit code of 0 generally refers to success). As mentioned in HUB's answer, you can also just || on the command-line to run a second command when the first one fails.

Once you have the status figured out, you just have to send yourself some mail. Here is an example that uses the mail command to send mail from a shell script, assuming the box you're testing from has SMTP setup:

BTW: if you're not good at shell scripting, don't limit yourself to a shell script. You could use a ruby script, a php script, any kind of script your server can run! Just add the #!/path/to/executable line at the beginning of the script - for instance:

#!/usr/bin/php

mltsy
  • 292
  • 2
  • 9
  • Thanks for your input - I'm going to check out this solution as soon as I get a moment. Will report back soon. – Xoundboy Jun 20 '11 at 18:14
7

Check this script. it's checking against a list of websites and sends email (to list of emails) whenever something wrong (http response different from 200). The script creates a .temp file to "remember" which website(s) failed at last check so you won't get multiple emails. the .temp file is deleted when the website is working again.

#!/bin/bash
# list of websites. each website in new line. leave an empty line in the end.
LISTFILE=/scripts/isOnline/websites.lst
# Send mail in case of failure to. leave an empty line in the end.
EMAILLISTFILE=/scripts/isOnline/emails.lst

# `Quiet` is true when in crontab; show output when it's run manually from shell.
# Set THIS_IS_CRON=1 in the beginning of your crontab -e.
# else you will get the output to your email every time
if [ -n "$THIS_IS_CRON" ]; then QUIET=true; else QUIET=false; fi

function test {
  response=$(curl --write-out %{http_code} --silent --output /dev/null $1)
  filename=$( echo $1 | cut -f1 -d"/" )
  if [ "$QUIET" = false ] ; then echo -n "$p "; fi

  if [ $response -eq 200 ] ; then
    # website working
    if [ "$QUIET" = false ] ; then
      echo -n "$response "; echo -e "\e[32m[ok]\e[0m"
    fi
    # remove .temp file if exist.
    if [ -f cache/$filename ]; then rm -f cache/$filename; fi
  else
    # website down
    if [ "$QUIET" = false ] ; then echo -n "$response "; echo -e "\e[31m[DOWN]\e[0m"; fi
    if [ ! -f cache/$filename ]; then
        while read e; do
            # using mailx command
            echo "$p WEBSITE DOWN" | mailx -s "$1 WEBSITE DOWN" $e
            # using mail command
            #mail -s "$p WEBSITE DOWN" "$EMAIL"
        done < $EMAILLISTFILE
        echo > cache/$filename
    fi
  fi
}

# main loop
while read p; do
  test $p
done < $LISTFILE

Add the following lines to crontab config ($ crontab -e)

THIS_IS_CRON=1
*/30 * * * * /path/to/isOnline/checker.sh

Available on Github

ET-CS
  • 196
  • 1
  • 4
3

I know that all the above scripts are exactly what you've asked, but I would suggest looking at monit because it will send you an email if apache is down but it will also restart it (if it's down).

Jure1873
  • 3,692
  • 1
  • 21
  • 28
  • This also looks interesting - I'll try to find time to check it out soon and p[ost back my findings. – Xoundboy Jun 20 '11 at 18:13
1

I would recommend pingdom for this. Their free service allows you to check 1 site, but that's all you need to check 1 server. If you have an iPhone they push-message you for free, so no need to buy SMS credits from them, and they have multiple settings you can use. Mine is set to notify me after 2 retries (10min) and every 10min downtime after that. It's awesome, since it also checks for HTTP 500 messages indicating a site is down. If it fails, it immediately checks your site again from a different server in a different location. If that one fails, well, that triggers your preference in how/when you'd like to get notified.

AcidRaZor
  • 151
  • 3
  • I really like this Pingdom service - just set up the free account and tested the check and the SMS to my Czech mobile number - both work - will try this out for the time being and see how it goes. – Xoundboy Jun 20 '11 at 15:35
  • Sadly, Pingdom no longer appears to offer the Free tier. The least expensive plan is $14.95/month. – Ben Johnson Aug 08 '18 at 19:36
  • You can use https://cronitor.io - they offer one free site for checking and can push notifications to email, slack or any other webhook. – Riz Feb 12 '19 at 13:22
  • Another alternative to pingdom would be [uptimerobot](https://uptimerobot.com/), 5 minutes monitoring intervals and 50 monitored website in the free plan. It integrates easily with slack and telegram too (besides push, SMS or email notification, ...). – trolologuy Aug 29 '19 at 18:41
1

Slight variation of the above.

A script to check if a website is available every 10 seconds. Log failed attempts into a siteuptime.txt file so it can be looked at (or graphed in excel) later.

#!/bin/bash
# Check site every 10 seconds, log failed connection attempts in siteuptime.txt
while true; do
echo "Checking site...";
/usr/bin/wget "http://www.mysite" --timeout 6 -O - 2>/dev/null | grep "My String On page" || echo "The site is down" | date --iso-8601=seconds >> siteuptime.txt;
sleep 10;
done;
dtbaker
  • 183
  • 1
  • 8
1
#!/bin/bash

################Files to be created before starting exicution####################
# sudo apt-get install alsa alsa-utils                                                  #
# mkdir -p $HOME/scripts                                                                    #
# touch $HOME/scripts/URL_File                                                              #
# touch $HOME/scripts/alert_Data                                                            #
# touch /tmp/http                                                                               #
# touch /tmp/http_file                                                                        #
# Download alert.wav file and copy it into $HOME/scripts directory                #
#################################################################################

####### checking existing process and creating temp files for URLs###############
Proc=$(ps -ef | grep http_alerts.sh | wc -l)
number=$(ps -ef | grep http_alerts.sh)
if [ $Proc -gt 3 ]
then
    echo "Script Already Running. Please kill PID($number) and restart"
else
FILE="$HOME/scripts/URL_File"
myfileval=1
while read -r line_read; do
    echo $line_read > /tmp/http_file
    File_name=$(cat /tmp/http_file | awk -v "val=$myfileval" 'NR==val {print $2}')
    File_name_val=$(ls /tmp/$File_name 2>/dev/null | wc -l)
    File_name_val0=0
    if [ $File_name_val -eq $File_name_val0 ]
    then 
        touch /tmp/$File_name
    fi
done < "$FILE"
####### checking existing process and finding temp files for URLs###############
echo "############ SCRIPT STARTED WORKING ################"
echo "############ SCRIPT STARTED WORKING ################" >> $HOME/scripts/alert_Data
echo " " >> $HOME/scripts/alert_Data
####### Continues Loop to check the URLs without break           ###############
while true
do
#######  Reading file URLs                                       ###############
### URL formate- http or https URL; 
### remarks; if domain name 0 else 1; 
### domain without proto(http/https); 
### Public_1; Public_2; ########
filename="$HOME/scripts/URL_File" ### file path
while read -r line; do
        echo $line > /tmp/http ### inserting each line data to temparary file
### Checking Internet Connection #######
        while true
        do
            if ping -q -c 1 -W 1 8.8.8.8 >/dev/null; 
        then   
            break
        else 
            echo "You are not connected to internet. Please wait"
            sleep 5  
        fi
        done 
### Checking Internet Connection #######
    myval=1
    i=$((i+1))
    j=7
    k=$(shuf -i 1-${j} -n 1)
    l=30
    i=$(($l+$k)) ##### Color code 31 to 37
    echo ""
    echo ""
    URL=$(cat /tmp/http |  awk -v "val=$myval" 'NR==val {print $1}')  ### 1st paramater from file. example: http://myabcd.com
    Server_State=$(cat /tmp/http |  awk -v "val=$myval" 'NR==val {print $2}') ### 2nd paramater from file. example: this_is_myabcd_site
    val3=$(cat /tmp/http | awk -v "val=$myfileval" 'NR==val {print $3}') ### 3rd paramater from file. 0 or 1
    val4=$(cat /tmp/http | awk -v "val=$myfileval" 'NR==val {print $4}') ### 4rd paramater from file. example: myabcd.com
    val5=$(cat /tmp/http | awk -v "val=$myfileval" 'NR==val {print $5}') ### 5th paramater from file. example: 123.123.123.111
    val6=$(cat /tmp/http | awk -v "val=$myfileval" 'NR==val {print $5}') ### 6th paramater from file. example: 123.123.123.222
echo "\e[1;${i}m-------------------------------------------------------------------\e[0m"
echo "\e[1;${i}m| Cheking URL :   $URL                             \e[0m"
echo "\e[1;${i}m-------------------------------------------------------------------\e[0m" >> $HOME/scripts/alert_Data
echo "\e[1;${i}m| Cheking URL :   $URL                             \e[0m" >> $HOME/scripts/alert_Data
DATA=$(date) ### time stamp 
code=$(curl -s -o /dev/null -w "%{http_code}" $URL) ### getting URL response code
if [ $code -eq 200 -o $code -eq 301 -o $code -eq 302 ] ### checking with sucessful response codes
then 
    echo "\e[1;${i}m-------------------------------------------------------------------\e[0m"
    echo "\e[1;${i}m| UP TIME     :   $DATA                            \e[0m"
    echo "\e[1;${i}m-------------------------------------------------------------------\e[0m"
    echo "\e[1;${i}m| Server State:   $Server_State                    \e[0m" 
    echo "\e[1;${i}m-------------------------------------------------------------------\e[0m" 
    echo "\e[1;${i}m-------------------------------------------------------------------\e[0m" >> $HOME/scripts/alert_Data
    echo "\e[1;${i}m| UP TIME     :   $DATA                            \e[0m" >> $HOME/scripts/alert_Data
    echo "\e[1;${i}m-------------------------------------------------------------------\e[0m" >> $HOME/scripts/alert_Data
    echo "\e[1;${i}m| Server State:   $Server_State                    \e[0m" >> $HOME/scripts/alert_Data
    echo "\e[1;${i}m-------------------------------------------------------------------\e[0m" >> $HOME/scripts/alert_Data
elif [ $code -eq 404 -o $code -eq 500 ] ### checking with error response codes
then 
    echo "\e[1;${i}m-------------------------------------------------------------------\e[0m"
    echo "\e[1;${i}m| URL IS DOWN :   $URL                             \e[0m"
    echo "\e[1;${i}m-------------------------------------------------------------------\e[0m"
    echo "\e[1;${i}m| DOWN TIME   :   $DATA                            \e[0m"
    echo "\e[1;${i}m-------------------------------------------------------------------\e[0m"
    echo "\e[1;${i}m| HTTP TIME   :   $code                            \e[0m"
    echo "\e[1;${i}m-------------------------------------------------------------------\e[0m"
    echo "\e[1;${i}m| Server State:   $Server_State                    \e[0m"
    echo "\e[1;${i}m-------------------------------------------------------------------\e[0m" >> $HOME/scripts/alert_Data
    echo "\e[1;${i}m| URL IS DOWN :   $URL                             \e[0m" >> $HOME/scripts/alert_Data
    echo "\e[1;${i}m-------------------------------------------------------------------\e[0m" >> $HOME/scripts/alert_Data
    echo "\e[1;${i}m| DOWN TIME   :   $DATA                            \e[0m" >> $HOME/scripts/alert_Data
    echo "\e[1;${i}m-------------------------------------------------------------------\e[0m" >> $HOME/scripts/alert_Data
    echo "\e[1;${i}m| HTTP CODE   :   $code                            \e[0m" >> $HOME/scripts/alert_Data
    echo "\e[1;${i}m-------------------------------------------------------------------\e[0m" >> $HOME/scripts/alert_Data
    echo "\e[1;${i}m| Server State:   $Server_State                    \e[0m" >> $HOME/scripts/alert_Data
    echo "\e[1;${i}m-------------------------------------------------------------------\e[0m" >> $HOME/scripts/alert_Data
    aplay $HOME/scripts/alert.wav 2> /dev/null ### On failure buzzer will sound
    /usr/bin/truncate -s 0 /tmp/$Server_State  ### truncate the file with server failure count data
    echo " Dear Admin Team \n The $URL is DOWN for the State $Server_State. The HTTP response code is $code " | mail -s "$Server_State is down" -a "From: from@mail.com" yourmail1@mail.com,yourmail2@mail.com >> /dev/null   ### On failure sending mail
elif [ $code -eq 000 ]
then
   LNUM=$(cat /tmp/$Server_State | wc -l)
   LNUM0=0
   oval=0
    if [ $val3 -eq $oval ] ### checking Domain or Public IP
    then
    dname=$(nslookup $val4 | awk '/^Address: /{print $2}') ### getting domain name Public IPs
        for dname_i in $dname
        do
            dname_url="http://$dname_i/" ### Making Public IP as http URL
            dname_code=$(curl -s -o /dev/null -w "%{http_code}" $dname_url)  ### getting public IP response
            if [ $dname_code -eq 200 -o $dname_code -eq 301 -o $dname_code -eq 302 ]  ### If success response
            then
            echo "\e[1;${i}m---------------------------------------------------\e[0m"
            echo "\e[1;${i}m| UP TIME     :   $DATA                            \e[0m"
            echo "\e[1;${i}m---------------------------------------------------\e[0m"
            echo "\e[1;${i}m| Server State:   $Server_State                    \e[0m" 
            echo "\e[1;${i}m---------------------------------------------------\e[0m" 
            echo "\e[1;${i}m---------------------------------------------------\e[0m" >> $HOME/scripts/alert_Data
            echo "\e[1;${i}m| UP TIME     :   $DATA                            \e[0m" >> $HOME/scripts/alert_Data
            echo "\e[1;${i}m---------------------------------------------------\e[0m" >> $HOME/scripts/alert_Data
            echo "\e[1;${i}m| Server State:   $Server_State                    \e[0m" >> $HOME/scripts/alert_Data
            echo "\e[1;${i}m---------------------------------------------------\e[0m" >> $HOME/scripts/alert_Data
            else #### if did not success response 
                    if [ $LNUM -eq $LNUM0 ] ### If no failure count, then add the failure count from 1
                        then
                        echo "$Server_State 0" > /tmp/$Server_State
                        else 
                        ALT=$(cat /tmp/$Server_State |  awk -v "val=$myval" 'NR==val {print $2}')  ### server failure count
                        ALT5=5
                        if [ $ALT -eq $ALT5 ] ### If failure count is 5 then alert with sound and send mail
                        then
                        echo "\e[0;${i}m---------------------------------------------------\e[0m"
                        echo "\e[0;${i}m| URL IS DOWN :   $URL                             \e[0m"
                        echo "\e[0;${i}m---------------------------------------------------\e[0m"
                        echo "\e[0;${i}m| DOWN TIME   :   $DATA                            \e[0m"
                        echo "\e[0;${i}m---------------------------------------------------\e[0m"
                        echo "\e[0;${i}m| HTTP CODE   :   $code                            \e[0m"
                        echo "\e[0;${i}m---------------------------------------------------\e[0m"
                        echo "\e[0;${i}m| Server State:   $Server_State                    \e[0m"
                        echo "\e[0;${i}m---------------------------------------------------\e[0m" >> $HOME/scripts/alert_Data
                        echo "\e[0;${i}m| URL IS DOWN :   $URL                             \e[0m" >> $HOME/scripts/alert_Data
                        echo "\e[0;${i}m---------------------------------------------------\e[0m" >> $HOME/scripts/alert_Data
                        echo "\e[0;${i}m| DOWN TIME   :   $DATA                            \e[0m" >> $HOME/scripts/alert_Data
                        echo "\e[0;${i}m---------------------------------------------------\e[0m" >> $HOME/scripts/alert_Data
                        echo "\e[0;${i}m| HTTP CODE   :   $code                            \e[0m" >> $HOME/scripts/alert_Data
                        echo "\e[0;${i}m---------------------------------------------------\e[0m" >> $HOME/scripts/alert_Data
                        echo "\e[0;${i}m| Server State:   $Server_State                    \e[0m" >> $HOME/scripts/alert_Data
                        echo "\e[0;${i}m---------------------------------------------------\e[0m" >> $HOME/scripts/alert_Data
                        aplay $HOME/scripts/alert.wav 2> /dev/null  ### On failure buzzer will sound
                        /usr/bin/truncate -s 0 /tmp/$Server_State   ### truncate the file with server failure count data
                        echo " Dear Admin Team \n The $URL is DOWN for the State $Server_State. The HTTP response code is $code " | mail -s "$Server_State is down" -a "From: from@mail.com" yourmail1@mail.com,yourmail2@mail.com >> /dev/null   ### On failure sending mail 
                        else
                        ALT=$((ALT+1)) ### increase server failure count
                        echo "$Server_State $ALT" > /tmp/$Server_State
                        fi
                        fi
                    fi
                    done
                    oval1=1
                    elif [ $val3 -eq $oval1 ]   ### No domain name backup public IPs are there
                    then
                        if [ "$val5" != "" ]  ### first Public IP of diffrent ISP
                        then 
                        dname_url="http://$val5/" ### making URL with public IP
                        dname_code=$(curl -s -o /dev/null -w "%{http_code}" $dname_url)   ### getting response code
                        if [ $dname_code -eq 200 -o $dname_code -eq 301 -o $dname_code -eq 302 ] ### validating response code
                        then
                        echo "\e[1;${i}m---------------------------------------------------\e[0m"
                        echo "\e[1;${i}m| UP TIME     :   $DATA                            \e[0m"
                        echo "\e[1;${i}m---------------------------------------------------\e[0m"
                        echo "\e[1;${i}m| Server State:   $Server_State                    \e[0m" 
                        echo "\e[1;${i}m---------------------------------------------------\e[0m" 
                        echo "\e[1;${i}m---------------------------------------------------\e[0m" >> $HOME/scripts/alert_Data
                        echo "\e[1;${i}m| UP TIME     :   $DATA                            \e[0m" >> $HOME/scripts/alert_Data
                        echo "\e[1;${i}m---------------------------------------------------\e[0m" >> $HOME/scripts/alert_Data
                        echo "\e[1;${i}m| Server State:   $Server_State                    \e[0m" >> $HOME/scripts/alert_Data
                        echo "\e[1;${i}m---------------------------------------------------\e[0m" >> $HOME/scripts/alert_Data
                        elif [ "$val6" != "" ]  ### second Public IP of diffrent ISP
                        then 
                        dname_url="http://$val6/" ### making URL with public IP
                        dname_code=$(curl -s -o /dev/null -w "%{http_code}" $dname_url)   ### getting response code
                        if [ $dname_code -eq 200 -o $dname_code -eq 301 -o $dname_code -eq 302 ] ### validating response code
                        then
                        echo "\e[1;${i}m---------------------------------------------------\e[0m"
                        echo "\e[1;${i}m| UP TIME     :   $DATA                            \e[0m"
                        echo "\e[1;${i}m---------------------------------------------------\e[0m"
                        echo "\e[1;${i}m| Server State:   $Server_State                    \e[0m" 
                        echo "\e[1;${i}m---------------------------------------------------\e[0m" 
                        echo "\e[1;${i}m---------------------------------------------------\e[0m" >> $HOME/scripts/alert_Data
                        echo "\e[1;${i}m| UP TIME     :   $DATA                            \e[0m" >> $HOME/scripts/alert_Data
                        echo "\e[1;${i}m---------------------------------------------------\e[0m" >> $HOME/scripts/alert_Data
                        echo "\e[1;${i}m| Server State:   $Server_State                    \e[0m" >> $HOME/scripts/alert_Data
                        echo "\e[1;${i}m---------------------------------------------------\e[0m" >> $HOME/scripts/alert_Data
                        else
                        if [ $LNUM -eq $LNUM0 ]
                        then
                        echo "$Server_State 0" > /tmp/$Server_State
                        else
                        ALT=$(cat /tmp/$Server_State |  awk -v "val=$myval" 'NR==val {print $2}')  ### server failure count
                        ALT5=5
                        if [ $ALT -eq $ALT5 ]
                        then
                        echo "\e[0;${i}m---------------------------------------------------\e[0m"
                        echo "\e[0;${i}m| URL IS DOWN :   $URL                             \e[0m"
                        echo "\e[0;${i}m---------------------------------------------------\e[0m"
                        echo "\e[0;${i}m| DOWN TIME   :   $DATA                            \e[0m"
                        echo "\e[0;${i}m---------------------------------------------------\e[0m"
                        echo "\e[0;${i}m| HTTP CODE   :   $code                            \e[0m"
                        echo "\e[0;${i}m---------------------------------------------------\e[0m"
                        echo "\e[0;${i}m| Server State:   $Server_State                    \e[0m"
                        echo "\e[0;${i}m---------------------------------------------------\e[0m" >> $HOME/scripts/alert_Data
                        echo "\e[0;${i}m| URL IS DOWN :   $URL                             \e[0m" >> $HOME/scripts/alert_Data
                        echo "\e[0;${i}m---------------------------------------------------\e[0m" >> $HOME/scripts/alert_Data
                        echo "\e[0;${i}m| DOWN TIME   :   $DATA                            \e[0m" >> $HOME/scripts/alert_Data
                        echo "\e[0;${i}m---------------------------------------------------\e[0m" >> $HOME/scripts/alert_Data
                        echo "\e[0;${i}m| HTTP CODE   :   $code                            \e[0m" >> $HOME/scripts/alert_Data
                        echo "\e[0;${i}m---------------------------------------------------\e[0m" >> $HOME/scripts/alert_Data
                        echo "\e[0;${i}m| Server State:   $Server_State                    \e[0m" >> $HOME/scripts/alert_Data
                        echo "\e[0;${i}m---------------------------------------------------\e[0m" >> $HOME/scripts/alert_Data
                        aplay $HOME/scripts/alert.wav 2> /dev/null
                        /usr/bin/truncate -s 0 /tmp/$Server_State
                        echo " Dear Admin Team \n The $URL is DOWN for the State $Server_State. The HTTP response code is $code " | mail -s "$Server_State is down" -a "From: from@mail.com" yourmail1@mail.com,yourmail2@mail.com >> /dev/null   ### On failure sending mail
                        else
                        ALT=$((ALT+1))
                        echo "$Server_State $ALT" > /tmp/$Server_State
                        fi
                    fi
                fi
            fi
        fi
    fi
fi
sleep 4
vl=1
pdate=$(ls -l $HOME/scripts/alert_Data | awk -v "val=$vl" 'NR==val {print $7}')   ### getting file created day
ddate=$(date | awk -v "val=$vl" 'NR==val {print $3}') ### current day count
if [ $pdate -gt $ddate ]  ### validating file created day and current
then 
d=`date +%m-%d-%Y`
mv $HOME/scripts/alert_Data $HOME/scripts/alert_Data$d  ### taking backup of existing file with time stamp
touch $HOME/scripts/alert_Data ### creating new file
fi
done < "$filename"
done
fi
#######################################################################################################################################################
#The content of $HOME/scripts/URL_File is as below
#Please remove "#" and Headline titles. Each parameter will be read by the difrence of spaces.
#URLoftheSite           Remarks         Domain(0)/IPstatus(1)   Domain_name(without protocol)   PublicIP_1      PublicIP_2  
#http://myexamplesite.com   this_is_myexamplesite   1           myexamplesite.com       123.123.123.111     123.123.123.222
0

As you have many sites on your VPS, I would recommend You can open account with website monitoring site such as host-tracker.com. Apart from alerting you if site is down or not they also provide you weekly,monthly and yearly uptime of ur sites. Whish is very helpful for management and performance.

Alok
  • 1
  • 1
0

How about this:

#!/bin/bash
/etc/init.d/httpd status
if [[ $? == 3 ]]; then
   echo "Httpd is down `date`" | mail support@example.com
   exit 1
fi
exit 0
frogstarr78
  • 475
  • 7
  • 17
  • 1
    I like the simplicity but that won't test if the web site is available over the Internet. The service could be running but not actually serving clients. I've fallen for that one before. – John Gardeniers Jun 21 '11 at 08:18
  • 2
    Agreed, although, if you want to ensure that it's accessible from the Internet, you'd have to test it over the Internet. Even running wget on the server to it's external network interface wouldn't be an accurate test that the Internet can access it. – frogstarr78 Jun 23 '11 at 16:32