You Are Here Home > A little Program For Monitoring Your Websites, With Alarm

A little Program For Monitoring Your Websites, With Alarm

I hope someone will find this useful:

###
 #    Hamid Alipour
###
 
###
 # Config
#####################################################
urls = ['http://www.P U T  Y O U R  U R L S  H E R E.com/']
url_timeout = 5
sleep_time = 5 * 60
failed_count_before_alarm = 10
sleep_time_on_failure = 15
#####################################################
 
import urllib2
import winsound
import time
 
class SiteMonitor:
 
    def __init__(self):
        pass
 
    def start(self):
        while True:
            for url in urls:
                if not self.check_url(url):
                    if not self.is_it_live(url):
                        self.alarm()
            self.sleep(sleep_time)
 
    def is_it_live(self, url):
        failed_count = 0
        while failed_count < failed_count_before_alarm:
            if not self.check_url(url):
                failed_count += 1
            else:
                return True
            self.sleep(sleep_time_on_failure)
        return False
 
    def check_url(self, url):
        print 'Checking...' + url + '...',
        try:
            urllib2.urlopen(url, timeout=url_timeout)
        except:
            print 'Failed!'
            return False
        print 'Done it\'s up!'
        return True
 
    def alarm(self):
        print 'Too many failures...'
        while True:
            print 'Alarm...'
            winsound.PlaySound("SystemExit", winsound.SND_ALIAS)
 
    def sleep(self, seconds):
        print 'Sleeping for ' + str(seconds) + ' second(s)...'
        time.sleep(seconds)
        print 'Waking up...'
 
def main():
    SiteMonitor().start()
 
if __name__ == "__main__":
    main()

Save it as sitemonitor.py and run it like:
python sitemonitor.py

Or just double click on the file.

Please note: in order to run this, you will have to install Python:
http://www.python.org/download/

A little Program For Monitoring Your Websites, With Alarm
Filed under: Python, Server   Posted by: Codehead on February 20, 2009

Disclaimer
1 - Use the information provided here at your own risk.
2 - You may not republish this content without prior written consent.




1 Comment »

  1. bernard:
     

    the beast!!

    Comment — August 12, 2009 @ 2:37 am

RSS feed for comments on this post. TrackBack URL

Leave a comment