Skip to content

Instantly share code, notes, and snippets.

@cmheisel
Last active December 16, 2015 17:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cmheisel/5473418 to your computer and use it in GitHub Desktop.
Save cmheisel/5473418 to your computer and use it in GitHub Desktop.
Check for HP01, log if it appears more than it should (twice)
import time
import datetime
import random
import requests
sites_to_check = {
"http://www.daytondailynews.com/": dict(hit=0, miss=0),
"http://www.oxfordpress.com/": dict(hit=0, miss=0),
"http://www.springfieldnewssun.com/": dict(hit=0, miss=0),
"http://www.middletownjournal.com/": dict(hit=0, miss=0),
"http://www.journal-news.com/": dict(hit=0, miss=0),
"http://www.todayspulse.com/": dict(hit=0, miss=0),
"http://www.activedayton.com/": dict(hit=0, miss=0),
}
sites_to_check_uncached = {
"http://www.daytondailynews.com/": dict(hit=0, miss=0),
"http://www.oxfordpress.com/": dict(hit=0, miss=0),
"http://www.springfieldnewssun.com/": dict(hit=0, miss=0),
"http://www.middletownjournal.com/": dict(hit=0, miss=0),
"http://www.journal-news.com/": dict(hit=0, miss=0),
"http://www.todayspulse.com/": dict(hit=0, miss=0),
"http://www.activedayton.com/": dict(hit=0, miss=0),
}
def monitor_hp01(cache_bust=False):
dataset = sites_to_check
if cache_bust:
dataset = sites_to_check_uncached
for site in dataset.keys():
site_url = site
if cache_bust:
site_url = "%s?hp01_check=%s" % (site, random.randint(1, 10000))
r = requests.get(site_url)
hp01_count = r.text.count("HP01")
if hp01_count > 2:
dataset[site]['hit'] += 1
now = datetime.datetime.now()
else:
dataset[site]['miss'] +=1
hits = 0
misses = 0
for site, hit_log in dataset.items():
hits += hit_log['hit']
misses += hit_log['miss']
now = datetime.datetime.now()
if misses > 0:
ratio = hits / float(misses)
else:
ratio = 0
print "%s - Cached: %s - HP01 hit ratio: %.2f %s/%s" % (
now.strftime("%Y/%m/%d %H:%M:%S"),
not cache_bust,
ratio,
hits,
misses,
)
if __name__ == "__main__":
while True:
try:
monitor_hp01()
monitor_hp01(True)
time.sleep(60)
except KeyboardInterrupt:
raise
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment