Monitoring Website for Downtime

Sometimes our in house server go down, But at production level we cannot take risk of 1min downtime, so we were supposed to monitor our server for downtime.
Although there are many cheap services available but i decided to do it my own way, I can use this script in my RPI to monitor even my internal services.

Below is python code, Which sends email if a server goes down and when it comes back online. Obviously you can add code to get more statistics like Logging to file, average response time, Monitoring page changes, Downtime etc.

import httplib
import time
from smtplib import SMTP

def send_email(to,subject,body,SERVER="",PORT=587,USERNAME="[email protected]",PASSWORD="YourPassword"):
	headers = "\r\n".join(["from: " + USERNAME,
                       "subject: " + subject,
                       "to: " + to,
                       "mime-version: 1.0",
                       "content-type: text/html"])
	content = headers + "\r\n\r\n" + body
	smtp.sendmail(USERNAME, to, content)

def get_status_code(host,path="/"):
		conn = httplib.HTTPConnection(host,timeout=10)
		return conn.getresponse().status
	except StandardError:
		return 0

def get_site_status(host):
	start = time.time()
	status = False
	code = get_status_code(host)
	#if found, temp redirect, permanent redirect.
	if code in (200,302,301):
		status = True
	return status, time.time()-start,code

def check_internet():
	return get_site_status("")

def check_status():
	toMonitor = [""]
	down = []
	email = ""
	while True: 
		for value in toMonitor:
			st = "Checking %s..." %value

			#give enough space so that status code can be idented.
				st += " "
			print st,

			status,rtime,code = get_site_status(value)
			print "[%s]" %code,

			if status==False :
				if check_internet()[0] == True :
					print "[Server down]"
					#if not present in Server down list. Add for email and add to down list.
					if value not in down:
						email += st + "<b>[SERVER_DOWN]</b><br/>"
					print "[Internet Fail]"
				# if in down list compose mail that server is up.
				if value in down:
					email += st + "<b>[%fs]</b>" %rtime
				print "[%fs]" %rtime


			print "Sending alert email..."
			send_email("[email protected]","ServerDown!","<pre>%s</pre>" %email)
			email = ""

		time.sleep(60) #sleep for 60s

if __name__ == '__main__':

After saving code to server use cron job to start run script on every boot.

Edit crontab using `sudo crontab -e` and then add following line to file.

`@reboot screen -dmS monitor python /root/ &> /dev/null`

Above command starts a `screen` session named monitor and forwards error of screen to null.
Incase you want to see output of use `screen -r monitor`.
You can also add it simply without screen like
`@reboot python /root/ &`

If you have some suggestions comment.

Leave a Reply

Your email address will not be published.