Simple site monitoring and alerting using Python & Twilio SMS

My company has a website with mostly static contents running on an old Linux machine. We hardly need to update the contents over a long period of time and everyone forgot about it. Then we got complaints from users as the site went down for a while. We fixed the problem and got back the site up in no time but it was quite embarrassing.

So I decided to implement a simple monitoring and alert system for future breakdown since the box is like really really old. I wrote a Python script to make requests to web pages and give out alert via sms if failed. For sms alert I used Twilio API.


import urllib
from twilio.rest import TwilioRestClient

def send_sms(urlstr, msg_string):
	account_sid = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
	auth_token = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
	client = TwilioRestClient(account_sid, auth_token)

	message = client.sms.messages.create(body="Server Status (%s) : %s" % (urlstr, msg_string) , to="RECEIVER NUMBER", from_="YOUR TWILIO NUMBER" )
	print message.sid

def main():
	urlstr = "http://ourwebsite.com/"
	result = ""
	try:
		result = str(urllib.urlopen(urlstr).getcode())

	except Exception,e:
		result = "SERVER DOWN"

	if result != "200":
		send_sms(urlstr, result)
		#print "NOT OK"

	print result

if __name__ == '__main__':
	main()

Above simple script will check and send out sms if the web site is down. Go check out Twilio for awesome phone and sms system.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s