Skip to content

Instantly share code, notes, and snippets.

@epochblue
Last active August 29, 2015 14:13
Show Gist options
  • Save epochblue/d75a05b310e8fadc13ef to your computer and use it in GitHub Desktop.
Save epochblue/d75a05b310e8fadc13ef to your computer and use it in GitHub Desktop.
Personal Pingdom
"""
Notify someone if a website is unavailable.
Usage:
isitup.py [-v] <site> <from_email> <to_email> <smtp_user> <smtp_password>
isitup.py -h|--help
isitup.py --version
Options:
-h --help Print this message
-v Print verbose output
--version Display the version
"""
from __future__ import print_function
import sys
import urllib2
import smtplib
__author__ = 'Bill Israel <bill.israel@gmail.com>'
__version__ = '0.0.1'
VERBOSE = False
def log(msg):
if VERBOSE:
print(msg)
def notify(site, from_email, to_email, user, passw):
log('{} is down'.format(site))
msg = '''Subject: Ruh-roh! {0} is down.
I was unable to get a good response from {0}. Might want to look into it, boss.
- Uptime Bot
'''
try:
smtp = smtplib.SMTP('smtp.gmail.com:587')
smtp.starttls()
smtp.login(user, passw)
smtp.sendmail(from_email, to_email, msg.format(site))
smtp.quit()
except:
pass
if len(sys.argv) < 5 or sys.argv[1] in ['-h', '--help']:
print(__doc__.strip())
sys.exit(0)
if sys.argv[1] == '--version':
print('isitup.py - version {} - by {}'.format(__version__, __author__))
sys.exit(0)
if sys.argv[1] == '-v':
VERBOSE = True
site, from_email, to_email, smtp_user, smtp_pass = sys.argv[2:]
else:
site, from_email, to_email, smtp_user, smtp_pass = sys.argv[1:]
try:
response = urllib2.urlopen(site)
if response.getcode() != 200:
notify(site, from_email, to_email, smtp_user, smtp_pass)
sys.exit(1)
except urllib2.URLError as ex:
notify(site, from_email, to_email, smtp_user, smtp_pass)
sys.exit(1)
log('{} is up'.format(site))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment