Created
July 1, 2010 18:16
-
-
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.
This file contains 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 characters
#!/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