Created
April 22, 2016 20:29
-
-
Save kravietz/c4a86f674a555107a77c665c8fdcd651 to your computer and use it in GitHub Desktop.
This file contains hidden or 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/python3 | |
# monitor a group of websites and email alerts | |
# cron task: | |
# | |
# */5 * * * * t=$(mktemp); if ! python3 /home/user/server-checks.py >$t; then mail [email protected] -s "Web check $(date)" <$t; fi; rm $t | |
# https://ipsec.pl/ True 0.7856874465942383 | |
# sample output (only if errors detected, otherwise stays silent) | |
# | |
# https://echelon.pl/ True 0.7726583480834961 | |
# https://kabardians.com/ True 0.7841081619262695 | |
# https://webcookies.org/ True 2.0685818195343018 | |
# https://passphrase.today/ True 0.6283636093139648 | |
# [0.6228809356689453, 0.6283636093139648, 8.63703966140747] | |
# https://cspbuilder.info/static/#/main/ True 0.5091941356658936 | |
# http://evilsource.org/ True 0.3158442974090576 | |
# http://carmichael.industries True 0.3121650218963623 | |
# http://krawczyk.industries True 0.3126814365386963 | |
# http://volkoff.industries True 0.31519412994384766 | |
# http://webcookies.info True 0.317718505859375 | |
import requests | |
import time | |
import statistics | |
import sys | |
from requests.exceptions import Timeout, ConnectionError | |
SERVERS = [ | |
{ "url": "https://ipsec.pl/", }, | |
{ "url": "https://echelon.pl/", }, | |
{ "url": "https://kabardians.com/", }, | |
{ "url": "https://webcookies.org/", }, | |
{ "url": "https://passphrase.today/", }, | |
{ "url": "https://cspbuilder.info/static/#/main/", }, | |
{ "url": "http://evilsource.org/", }, | |
{ "url": "http://carmichael.industries", }, | |
{ "url": "http://krawczyk.industries", "expect": 301 }, | |
{ "url": "http://volkoff.industries", }, | |
{ "url": "http://webcookies.info", "expect": 307 }, | |
] | |
ret = 0 | |
ALERT_TIMEOUT = 5.0 # seconds | |
GOOD_RESPONSE_TIME = 3.0 # seconds | |
for server in SERVERS: | |
result_set = [] | |
time_set = [] | |
url = server["url"] | |
for attempt in range(3): | |
t0 = time.time() | |
try: | |
r = requests.get(url, timeout=ALERT_TIMEOUT, allow_redirects=False) | |
except (ConnectionError, Timeout) as e: | |
print(url, e) | |
break | |
t1 = time.time() | |
result_set.append(r) | |
time_set.append(t1-t0) | |
if len(result_set): | |
ok_codes = [ requests.codes.ok ] | |
if "expect" in server: | |
ok_codes.append(server["expect"]) | |
all_ok = all(list(map(lambda x: x.status_code in ok_codes, result_set))) | |
all_fast = all(list(map(lambda x: x < GOOD_RESPONSE_TIME, time_set))) | |
print(url, all_ok, statistics.median(time_set)) | |
if not all_ok: | |
print('\t', result_set) | |
ret = 1 | |
if not all_fast: | |
print('\t', time_set) | |
ret = 1 | |
sys.exit(ret) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment