[Application] Web Site Check V1.0

Parnassus Submission x@vex.net
Tue, 14 Aug 2001 02:06:43 -0400 (EDT)


                          Web Site Check V1.0                           
                          -------------------                           

This script monitors a web site by checking one specific page.

This script will attempt to connect to one web site and download one
page; if the script cannot connect or the page returns a status other
than OK (200), the script will log the down time and send e-mails. The
next time the script runs, if all is OK, then another e-mail will be
sent indicating that the site is now up. If the site is still down, more
e-mails will be sent indicating how long the site has been down.

####################################################################################################
# WebSiteCheck.py # 2001/08/02 # Original version: RL # copyright 2001,
no rights reserved. # # This script is meant to run periodically, such
as every ten minutes or every hour. # This script will attempt to
connect to one web site and download one page; if the script cannot #
connect or the page returns a status other than OK (200), the script
will log the down time and # send e-mails. The next time the script
runs, if all is OK, then another e-mail will be sent # indicating that
the site is now up. If the site is still down, more e-mails will be sent
# indicating how long the site has been down.
####################################################################################################
# imports import httplib import time import smtplib import string #
declare variables sModuleName = 'WebSiteCheck' # name of this module
sFromAddress = '{put the from address here}' # the from address for the
e-mails sWebSiteAddress = '{put the URL to check here' # the web site to
check sWebSitePageName = '{put the page to get here}' # this is the page
that the script will try to get sDownLogFileName = sFromAddress + '.txt'
# this file contains the starting down time in minutes sRunLogFileName =
sFromAddress + '.log' # this file contains log entries and web site
response strings # create three parallel lists to map the dest addresses
with the proper mail server; the SMTP server addresses must match the
e-mail addresses lstToAddressSeq = [0, 1, 2] lstToAddresses = ['{put
e-mail address 1 here}', '{put e-mail address 2 here}', '{put e-mail
address 3 here}'] lstToSMTPServers = ['{put SMTP server 1 here}', '{put
SMTP server 2 here}', '{put SMTP server 1 here}'] ######################
Don't change anything after this line #############################
fRunLog = '' # open file pointer fDownLog = '' # open file pointer fFile
= '' # open file pointer sSubject = '' # e-mail subject lCurrentMinute =
0 # current time in minutes from Jan 1 2001 lMinuteDiff = 0 # difference
between 'lCurrentMinute' and when the error was noticed lMinuteCount = 0
# contains the integer value of how long the site has been down
sMinuteCount = '0' # contains the string equivalent of 'lMinuteCount' #
function to convert year/month/day/hour/minute into the number of
minutes since Jan 1 2001 def ConvertDateTimeToMinutesSinceJan1(sYear,
sMonth, sDay, sHour, sMinute): n = (int(sYear) - 2001) * 535680 # 535680
= 12 month * 31 days * 24 hours * 60 minutes n = n + int(sMonth) * 44640
# 44640 = 31 days * 24 hours * 60 minutes n = n + int(sDay) * 1440 #
1440 = 24 hours * 60 minutes n = n + int(sHour) * 60 # minutes in an
hour n = n + int(sMinute) return n # function to log a message in the
Run Log def LogMessage(s): lstCurrentTime = time.localtime(time.time())
# get the current date and time into an array timCurrentTime =
time.strftime('%Y/%m/%d %H:%M:%S', lstCurrentTime) # format time string
fRunLog = open (sRunLogFileName, 'a') fRunLog.write(sModuleName + ' ' +
timCurrentTime + ' ' + s + '\n') fRunLog.close() # log the execution of
the script LogMessage('Checking ' + sWebSiteAddress) # calculate the
current time in minutes from Jan 1 2001 lstTime =
time.localtime(time.time()) lCurrentMinute =
ConvertDateTimeToMinutesSinceJan1(lstTime[0], lstTime[1], lstTime[2],
lstTime[3], lstTime[4]) # get down time start from file; 0 means the
site was not down try: fDownLog = open (sDownLogFileName, 'r') # open
file with read-only access sMinuteCount = fDownLog.readline() # get
minute count of when the site went down; '0' means that the site was up;
non-zero means that the site was down fDownLog.close() except:
sMinuteCount = '0' # file not found, set to 0 fDownLog = open
(sDownLogFileName, 'w+') # open file with write-access and truncate the
contents fDownLog.write(sMinuteCount) # write it out to the file
fDownLog.close() lMinuteCount = string.atoi(sMinuteCount) # convert to
integer lMinuteDiff = int(lCurrentMinute) - int(lMinuteCount) # diff
between start of down time and now; if down time is 0 then this is the
current time in minutes since the first of the year # http setup try: h
= httplib.HTTP(sWebSiteAddress) h.putrequest('GET', sWebSitePageName) #
try to get this page h.putheader('Accept', 'text/html')
h.putheader('Accept', 'text/plain') h.endheaders() # http call errcode,
errmsg, headers = h.getreply() except: # we can't connect at all # open
the log file LogMessage('Unable to connect to the web site' +
sWebSiteAddress) errcode = 0 # force an error by setting the error code
to 0 msg = '' # null out the message var; if it is not null at the end
of the script, send e-mail if errcode == 200: # web site up, check
downtime log: if a non-zero value exists, send e-mail that the site is
up and put '0' in downtime log to indicate that the site is up fFile =
h.getfile() h.close resp = fFile.read() # Get the raw HTML fFile.close()
if resp != '': LogMessage('Response:' + resp) else:
LogMessage('Connected but unable to get a response from the web site')
if sMinuteCount == '0': # website is up and saved downtime start = 0,
nothing to do msg = '' else: # website is up and saved downtime start >
0, tell users that site is responding sSubject = 'NOTICE: Web site ' +
sWebSiteAddress + ' is responding' msg = "From: %s\r\nTo:
%s\r\nSubject:%s\r\n\r\n" %(sFromAddress, lstToAddresses, sSubject) msg
= msg + 'Web site ' + sWebSiteAddress + ' is now responding. ' msg = msg
+ ' Site was down for ' + '%(lMinuteDiff)d' %vars() + ' minutes.'
lMinuteCount = 0 # write out the down time start set to 0 sMinuteCount =
('%(lMinuteCount)d' %vars()) # convert to string fDownLog = open
(sDownLogFileName, 'w+') # truncate and open for writing
fDownLog.write(sMinuteCount) # write out '0' for down time start
fDownLog.close() else: # site is down LogMessage(sWebSiteAddress + ' is
down') sSubject = 'CRITICAL: Web site ' + sWebSiteAddress + ' does not
respond' msg = "From: %s\r\nTo: %s\r\nSubject:%s\r\n\r\n"
%(sFromAddress, lstToAddresses, sSubject) msg = msg + 'Web site ' +
sWebSiteAddress + ' does not respond.' if errcode == 0: msg = msg + ' No
error code returned.' else: msg = msg + ' Error ' + '%(errcode)d'
%vars() + ' returned.' # first time site was noticed as down, put
downtime start in minutes after first of the year in downtime log if
sMinuteCount == '0': sCurrentMinute = ('%(lCurrentMinute)d' %vars()) #
convert to string fDownLog = open (sDownLogFileName, 'w+') # truncate
and open for writing fDownLog.write(sCurrentMinute) fDownLog.close() msg
= msg + ' Outage just detected.' else: msg = msg + ' Outage was first
detected ' + '%(lMinuteDiff)d' %vars() + ' minutes ago.' # finish
message and send if msg != '': # if not null, send message for i in
lstToAddressSeq[:]: # loop thru all the addresses
smtpIP=lstToSMTPServers[i] toaddr=lstToAddresses[i] try: server =
smtplib.SMTP(smtpIP) # server.set_debuglevel(1)
server.sendmail(sFromAddress, toaddr, msg) server.quit() time.sleep(1) #
release processor between messages except: LogMessage('Unable to send
mail to ' + toaddr + ' using ' + smtpIP) # the end


   License:  Public Domain
  Platform:  All
  Requires:  None
  Binaries:  None
       Gui:  None

  Categories:  Utility Apps

R Lind

--
<a href="">Web Site Check V1.0</a> -- This script monitors a web site by
checking one specific page.