Skip to content

Instantly share code, notes, and snippets.

@codebynumbers
Last active December 19, 2015 18:18
Show Gist options
  • Select an option

  • Save codebynumbers/5997321 to your computer and use it in GitHub Desktop.

Select an option

Save codebynumbers/5997321 to your computer and use it in GitHub Desktop.
Draft site pinger
import grequests
import redis
import time
BATCH_SIZE = 10
THRESHOLD = 3
SLEEP_TIME = 60
# MANIFEST -[(URL, Check type, check value), ...]
sites = [
('http://www.codebynumbers.net', 'status', 200),
('http://platform.50onred.com/health', 'content', 'OK')
]
conn = redis.Redis()
def chunks(l, n):
""" Yield successive n-sized chunks from l. """
for i in xrange(0, len(l), n):
yield l[i:i+n]
def check():
# Check sites in batches
for batch in chunks(sites, BATCH_SIZE):
# Get urls in parallel
rs = (grequests.get(url) for (url, check_type, expected) in batch)
# Process responses
for i, response in enumerate(grequests.map(rs)):
(url, check_type, expected) = batch[i]
check = response.status_code if check_type == 'status' else response.text
status = conn.get("status:%s" % url)
if expected != check:
print "Check failed for %s, got %s:%s expected %s" % (url, check_type, check, expected)
# record failure in storage
failures = conn.incr("fails:%s" % url)
# if over consecutive fail threshold, notify
if status == "up" and failures == THRESHOLD:
print "Notify user, consecutive failures reached"
# mark site as down, no more notifcations
conn.set("status:%s" % url, "down")
else:
print "Check OK for %s, got %s:%s expected %s" % (url, check_type, check, expected)
# clear failures
if status == "down":
print "Notify user, site back up"
conn.set("fails:%s" % url, 0)
conn.set("status:%s" % url, "up")
if __name__ == "__main__":
while True:
check()
time.sleep(SLEEP_TIME)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment