Looking around on Google for a webpage test script returns a lot of results. Some of them are useful, some are not. In particular, for Python, the scripts on the first page of results are minimal and lacking a useful copy and paste / ready to go script that will answer the question “is my webpage available?”. So I decided to write a quick one that will give you the return code and email you as an alert if the page does not return with a 200 code (successful). You can find the script here.
If you are familiar with Python scripting, this script could easily be modified to post to a form so that you can test a MySQL transaction (or other transactional DB) to ensure your stack is running as needed.
#!/usr/bin/python
###########################################################
## Matt Reid / themattreid at gmail d o t com
## Site: http://themattreid.com
## Date: 2010-02-24
## Purpose: Checks URL for valid page and emails if failed.
###########################################################
###########################################################
## EDIT THE FOLLOWING AS NEEDED
email = "email@gmail.com" ##Email address to send alert to
host = "google.com" ##Hostname base URL without http://
port = "80" ##Port - 80 for http, 443 for https
url = "/index.html" ##Page to look for on host
SENDMAIL = "/usr/sbin/sendmail" ##Binary location for sendmail
## END OF EDITABLE OPTIONS
###########################################################
import sys
import os
from httplib import HTTP
def get(host,port,url):
concaturl = host+url
print "Checking Host:",concaturl,"\n"
h = HTTP(host, port)
h.putrequest('GET', url)
h.putheader('Host', host)
h.putheader('User-agent', 'python-httplib')
h.endheaders()
(returncode, returnmsg, headers) = h.getreply()
if returncode != 200:
print returncode,returnmsg
return returncode
else:
f = h.getfile()
# return f.read() #returns content of page for further parsing
print returncode, returnmsg
return returncode
if __name__ == '__main__':
concaturl = "http://"+host+url
state = get(host,port,url)
if(state != 200):
msg = "Link (%s) is broken or unavailable. Please reference return code: %s" % (concaturl,state)
to = "To: "+email+"\n"
print msg,"\n"
p = os.popen("%s -t" % SENDMAIL, "w")
p.write(to)
p.write("Subject: Demo Server Status\n")
p.write("\n")
p.write(msg)
sts = p.close()
if sts != 0:
print "Sendmail exit status", sts
else:
print "Link is good.\n"
PlanetMySQL Voting: Vote UP / Vote DOWN