Last active
August 29, 2015 14:00
Revisions
-
sanand0 revised this gist
Aug 24, 2014 . 1 changed file with 2 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -36,7 +36,8 @@ def monitor_url(url, tests): url, tests = row[0], row[1] monitor_url(url, tests) except urllib2.URLError, e: # Ignore these messages pass except Exception, e: messages.append([url, str(e)]) -
sanand0 revised this gist
Jul 27, 2014 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -36,7 +36,7 @@ def monitor_url(url, tests): url, tests = row[0], row[1] monitor_url(url, tests) except urllib2.URLError, e: messages.append([url, str(e) + '. Ignore this']) except Exception, e: messages.append([url, str(e)]) -
sanand0 revised this gist
Jul 27, 2014 . 1 changed file with 11 additions and 5 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -7,14 +7,16 @@ import re import urllib2 def monitor_url(url, tests): response = urllib2.urlopen(url) info = response.info() info['Status'] = str(response.getcode()) for header, pattern in tests: value = info.getheader(header) if not re.match(pattern, value): raise AssertionError( '%s: %s does not match %s' % (header, value, pattern)) if __name__ == '__main__': @@ -29,9 +31,12 @@ def monitor_url(url, tests): if isinstance(row, basestring): url = row monitor_url(url, []) elif hasattr(row, '__getitem__') and hasattr(row, '__len__'): if len(row) > 1: url, tests = row[0], row[1] monitor_url(url, tests) except urllib2.URLError, e: messages.append([url, str(e), 'This can be ignored']) except Exception, e: messages.append([url, str(e)]) @@ -44,7 +49,8 @@ def monitor_url(url, tests): msg = MIMEText(msg) msg['From'] = config.sender msg['To'] = config.to msg['Subject'] = config.subject + ' %d/%d' % ( len(messages), len(config.urls)) p = Popen([config.sendmail, '-t'], stdin=PIPE) p.communicate(msg.as_string()) else: -
sanand0 revised this gist
Apr 19, 2014 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -44,7 +44,7 @@ def monitor_url(url, tests): msg = MIMEText(msg) msg['From'] = config.sender msg['To'] = config.to msg['Subject'] = config.subject + ' %d/%d' % (len(messages), len(config.urls)) p = Popen([config.sendmail, '-t'], stdin=PIPE) p.communicate(msg.as_string()) else: -
sanand0 revised this gist
Apr 19, 2014 . 2 changed files with 5 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1 @@ *.pyc This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,4 @@ A simple website monitor. 1. Edit `config.py` and add a list of URLs. 2. Set up a cron job to run `python monitor.py` periodically -
sanand0 created this gist
Apr 19, 2014 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,9 @@ sender = '[email protected]' to = '[email protected]' subject = 'Site monitoring errors' sendmail = '/usr/sbin/sendmail' urls = ( 'http://gramener.com/', 'http://blog.gramener.com/', ) This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,51 @@ #!/usr/bin/python """ A simple website HTTP header monitor tool. """ import re import urllib2 def monitor_url(url, tests): response = urllib2.urlopen(url) info = response.info() info['Status'] = str(response.getcode()) for header, pattern in tests: value = info.getheader(header) if not re.match(pattern, value): raise AssertionError('%s: %s does not match %s' % (header, value, pattern)) if __name__ == '__main__': import config from os.path import exists from email.mime.text import MIMEText from subprocess import Popen, PIPE messages = [] for row in config.urls: try: if isinstance(row, basestring): url = row monitor_url(url, []) elif hasattr(row, '__getitem__') and hasattr(row, '__len__') and len(row) > 1: url, tests = row[0], row[1] monitor_url(url, tests) except Exception, e: messages.append([url, str(e)]) if len(messages) > 0: msg = '\n\n'.join( url + '\n' + msg for url, msg in messages ) if exists(config.sendmail): msg = MIMEText(msg) msg['From'] = config.sender msg['To'] = config.to msg['Subject'] = config.subject + ' {:d}/{:d}'.format(len(messages), len(config.urls)) p = Popen([config.sendmail, '-t'], stdin=PIPE) p.communicate(msg.as_string()) else: print msg