"""Check to see if a given web site is up or not.
Usage:
python check_site.py <URL>
"""
import os, sys, urllib2, smtplib
# The following variables should be redefined to fit your system
_smtp_server = "outgoing.example.com"
# If you need to use SMTP AUTH, set this to 1.
# You will also need to set an SMTP user name and SMTP password
_authentication_required = 0
_user = ""
_password = ""
_to = "user@example.com"
_from = "webserver@mydomain.com"
_num_tries = 3
def is_site_up(url):
"""Checks to see if the given site is up"""
try:
urllib2.urlopen(url)
return True
except:
return False
def check_site(url):
"""Checks several times to see if the given site is up."""
count = 0
up = False
while not up and count < _num_tries:
if is_site_up(url):
up = True
count += 1
if not up:
print "site %s not up." % ( url )
# Construct a message
mesg = "To: " + _to + "\n"
# Note the extra blank line between Subject and body
mesg += "Subject: Server down\n\n"
mesg += "The web site: " + url + " is down.\n"
# Send email notifcation using SMTP
session = smtplib.SMTP(_smtp_server)
if _authentication_required:
session.login(_user, _password)
result = session.sendmail(_from, _to, mesg)
if result:
print "Sent mail. Status = " + str(result)
else:
print "site %s is up." % (url,)
if __name__ == '__main__':
if len(sys.argv) == 2:
check_site(sys.argv[1])
else:
print __doc__