Log in
updated 11:29 AM UTC, May 4, 2016

Script to monitor your website for free

  • Written by Madhu V Rao
  • Published in General

If you have hosted your website on Amazon Webservices (AWS) or any other cloud based hosting. You have to monitor your website for any errors , As its quite common for application server to go down from time to time due to various reasons. 

Its a good practice to keep a monitoring service which alerts you when website goes down (application server has died or instance is not accessible ,etc)

Here you can get a small script which does the job. you can set it up on your cloud instance from several locations to monitor for uptime from several locations.

Script monitors your website every minute (configurable) and on error notifies you with a mail. once recovered it will send out a mail about recovery. 

You need to have Curl installed on your ubuntu (if not below script will throw an error) and also need to have your mail details ready to send the mail on your behalf (SMPTS details)

Here it is:

#!/bin/bash
alert_sent=false
while true; do
	sleep 60 & pid=$!
	if curl -s --head  --request GET http://YOUR_WEBSITE.com | grep "200 OK" > /dev/null 
	then 
		if $alert_sent;
		then
			msg = "The HTTP server YOUR_WEBSITE.com has been recovered ";
        	        curl --url "smtps://smtp.gmail.com:465" --ssl-reqd --mail-from "FROM_YOUR_MAIL" --mail-rcpt "YOUR_MAIL_ID" --upload-file recovery.txt --user "YOUR_MAIL_ID:YOUR_PASSSSWORD" --insecure;
			alert_sent=false;
		else 
			echo "The HTTP server on YOUR_WEBSITE.com is up!" #> /dev/null;
		fi;
	else
		if $alert_sent;
		then
                        echo "The HTTP server on YOUR_WEBSITE.com is down!" #> /dev/null;
		else 
			msg="The HTTP server YOUR_WEBSITE.com is down ";
			curl --url "smtps://smtp.gmail.com:465" --ssl-reqd --mail-from "FROM_YOUR_MAIL" --mail-rcpt "YOUR_MAIL_ID" --upload-file mail.txt --user "YOUR_MAIL_ID:YOUR_PASSWORD" --insecure;
                        alert_sent=true;
		fi;
	fi;
	wait $pid;
done

It needs two more files which has  mail contents for error and recovery.

recovery.txt:

Hi,

Website YOURWEBSITE.com has been recovered.

Regards,
Self Monitoring

 

 mail.txt : this is the content for error notification:

Hi,

Website is down

Regards,
Self monitoring

 

Just copy the contents of the script to a webiste_monitor.sh and the contents of the text files to a text files.

keep all of them in a folder. change the website_monitor.sh to have executable permissions (chmod +x).

 

just run following in a terminal :

nohup ./website_monitor.sh &

 

that's it , if your website goes down, it will send out a mail.

 

 

 


Powered by Bullraider.com
Follow Us on Twitter
Find Us on Facebook
Follow Us on Google
Follow Us on Pinterest