Text Alert for Website Downtime

I’m going to set up my MacBook (via command line) to send me an email and text message when a website is down.

I’ll be using gmail for SMTP so first let’s configure postfix:

Configure Postfix for Gmail SMTP

Edit file /etc/postfix/main.cf

1
sudo vim /etc/postfix/main.cf

and add in the following below the commented out relayhosts :-

1
2
3
4
5
relayhost = [smtp.gmail.com]:587
smtp_sasl_auth_enable = yes
smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd
smtp_sasl_security_options = noanonymous
smtp_use_tls = yes

Generate sasl_password if not already exists

1
sudo vim /etc/postfix/sasl_passwd

and enter in the following:-

1
[smtp.gmail.com]:587 username@gmail.com:password

Run the following commands

1
2
3
4
sudo chmod 600 /etc/postfix/sasl_passwd
sudo postmap /etc/postfix/sasl_passwd
sudo launchctl stop org.postfix.master
sudo launchctl start org.postfix.master

Now, you should be able to send emails from within the command line:

1
mail -s "test" your@yourdomain.com

 

 

Now I create a bash script to monitor the website:


#!/bin/bash
NOTIFYEMAIL=[email address]
SMSEMAIL=[phonenumber]@[carrier] # see list below
SENDEREMAIL=[username]@gmail.com
SERVER=http://[the_website_address]/
PAUSE=60
FAILED=0
DEBUG=0

while true 
do
/usr/bin/curl -sSf $SERVER > /dev/null 2>&1
CS=$?
# For debugging purposes
if [ $DEBUG -eq 1 ]
then
    echo "STATUS = $CS"
    echo "FAILED = $FAILED"
    if [ $CS -ne 0 ]
    then
        echo "$SERVER is down"

    elif [ $CS -eq 0 ]
    then
        echo "$SERVER is up"
    fi
fi

# If the server is down and no alert is sent - alert
if [ $CS -ne 0 ] && [ $FAILED -eq 0 ]
then
    FAILED=1
    if [ $DEBUG -eq 1 ]
    then
        echo "$SERVER failed"
    fi
    if [ $DEBUG = 0 ]
    then
        echo "$SERVER went down $(date)" | /usr/bin/mail -s "$SERVER went down" "$SENDEREMAIL" "$SMSEMAIL" 
        echo "$SERVER went down $(date)" | /usr/bin/mail -s "$SERVER went down" "$SENDEREMAIL" "$NOTIFYEMAIL" 
    fi

# If the server is back up and no alert is sent - alert
elif [ $CS -eq 0 ] && [ $FAILED -eq 1 ]
then
    FAILED=0
    if [ $DEBUG -eq 1 ]
    then
        echo "$SERVER is back up"
    fi
    if [ $DEBUG = 0 ]
    then
        echo "$SERVER is back up $(date)" | /usr/bin/mail -s "$SERVER is back up again" "$SENDEREMAIL" "$SMSEMAIL"
        echo "$SERVER is back up $(date)" | /usr/bin/mail -s "$SERVER is back up again" "$SENDEREMAIL" "$NOTIFYEMAIL"
    fi
fi
sleep $PAUSE
done

Save this as an `.sh` file (like `outage.sh`) and run it from the command line:

./outage.sh

These are the carrier urls for sending a text message via email:

Carrier Email to SMS Gateway
Alltel [10-digit phone number]@message.alltel.comExample: 1234567890@message.alltel.com
AT&T (formerly Cingular) [10-digit phone number]@txt.att.net[10-digit phone number]@mms.att.net (MMS)[10-digit phone number]@cingularme.comExample: 1234567890@txt.att.net
Boost Mobile [10-digit phone number]@myboostmobile.comExample: 1234567890@myboostmobile.com
Nextel (now Sprint Nextel) [10-digit telephone number]@messaging.nextel.comExample: 1234567890@messaging.nextel.com
Sprint PCS (now Sprint Nextel) [10-digit phone number]@messaging.sprintpcs.com[10-digit phone number]@pm.sprint.com (MMS)Example: 1234567890@messaging.sprintpcs.com
T-Mobile [10-digit phone number]@tmomail.netExample: 1234567890@tmomail.net
US Cellular [10-digit phone number]email.uscc.net (SMS)[10-digit phone number]@mms.uscc.net (MMS)Example: 1234567890@email.uscc.net
Verizon [10-digit phone number]@vtext.com[10-digit phone number]@vzwpix.com (MMS)Example: 1234567890@vtext.com
Virgin Mobile USA [10-digit phone number]@vmobl.comExample: 1234567890@vmobl.com

Hope it’s helpful.