Skip to content

Instantly share code, notes, and snippets.

@dryan
Created July 1, 2010 18:16
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save dryan/460331 to your computer and use it in GitHub Desktop.
Save dryan/460331 to your computer and use it in GitHub Desktop.
check_sites shell script to monitor a list of websites and send notices when they are down.
#!/usr/bin/env python
sites = [
'google.com',
]
USE_BOXCAR = True
USE_GROWL = True
BOXCAR_EMAIL = ''
BOXCAR_API_KEY = ''
BOXCAR_API_SECRET = ''
import sys, os, urllib, urllib2, hashlib, datetime
now = datetime.datetime.now()
errors = []
def send_notice(site, code):
if USE_BOXCAR:
api_url = 'http://boxcar.io/devices/providers/%s/notifications' % BOXCAR_API_KEY
data = {
'email': hashlib.md5(BOXCAR_EMAIL).hexdigest(),
'secret': BOXCAR_API_SECRET,
'notification[from_screen_name]': site,
'notification[message]': code,
'notification[from_remote_service_id]': u'%s-%s' % (now.strftime('%c'), site)
}
urllib2.urlopen(api_url, urllib.urlencode(data))
if USE_GROWL:
os.system('growlnotify -n "Website Monitor" -p 2 "%s" -m "%s" -s' % (site, code))
for site in sites:
try:
urllib2.urlopen('http://%s' % site, None, 30)
except urllib2.HTTPError, code:
send_notice(site, code)
errors.append(site)
except urllib2.URLError, error:
error = str(error)
if error.find('Errno 8') > -1:
send_notice(site, u'Does not appear to exist.')
else:
send_notice(site, u'%s' % error)
errors.append(site)
except:
errors.append(site)
if len(errors):
print u'%s encountered errors.' % ','.join(errors)
else:
print u'All sites checked out.'
sys.exit()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment