Skip to content

Instantly share code, notes, and snippets.

@sanand0
Last active August 29, 2015 14:00

Revisions

  1. sanand0 revised this gist Aug 24, 2014. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion monitor.py
    Original 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:
    messages.append([url, str(e) + '. Ignore this'])
    # Ignore these messages
    pass
    except Exception, e:
    messages.append([url, str(e)])

  2. sanand0 revised this gist Jul 27, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion monitor.py
    Original 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), 'This can be ignored'])
    messages.append([url, str(e) + '. Ignore this'])
    except Exception, e:
    messages.append([url, str(e)])

  3. sanand0 revised this gist Jul 27, 2014. 1 changed file with 11 additions and 5 deletions.
    16 changes: 11 additions & 5 deletions monitor.py
    Original 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' %
    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__') and len(row) > 1:
    url, tests = row[0], row[1]
    monitor_url(url, tests)
    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))
    msg['Subject'] = config.subject + ' %d/%d' % (
    len(messages), len(config.urls))
    p = Popen([config.sendmail, '-t'], stdin=PIPE)
    p.communicate(msg.as_string())
    else:
  4. sanand0 revised this gist Apr 19, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion monitor.py
    Original 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}'.format(len(messages), len(config.urls))
    msg['Subject'] = config.subject + ' %d/%d' % (len(messages), len(config.urls))
    p = Popen([config.sendmail, '-t'], stdin=PIPE)
    p.communicate(msg.as_string())
    else:
  5. sanand0 revised this gist Apr 19, 2014. 2 changed files with 5 additions and 0 deletions.
    1 change: 1 addition & 0 deletions .gitignore
    Original file line number Diff line number Diff line change
    @@ -0,0 +1 @@
    *.pyc
    4 changes: 4 additions & 0 deletions README.md
    Original 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
  6. sanand0 created this gist Apr 19, 2014.
    9 changes: 9 additions & 0 deletions config.py
    Original 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/',
    )
    51 changes: 51 additions & 0 deletions monitor.py
    Original 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