Skip to content

Instantly share code, notes, and snippets.

@aluminiumgeek
Created January 30, 2020 09:09
Show Gist options
  • Save aluminiumgeek/81309e99a89c345db06e837cba50dc86 to your computer and use it in GitHub Desktop.
Save aluminiumgeek/81309e99a89c345db06e837cba50dc86 to your computer and use it in GitHub Desktop.
[Python] Simple website downtime monitor (+ Pushover)
import time
from datetime import datetime
import requests
URL = 'https://example.com'
CHECK_SECONDS = 60
def monitor():
# Pushover API credentials
data = {
'token': '',
'user': '',
}
down_at = None
passes = 0
while True:
resp = requests.get(URL)
if resp.status_code == 200:
print('200 OK')
if down_at is not None:
print('Recovered')
downtime = (datetime.now() - down_at).seconds / 60.0
data.update(
title='SITE IS UP',
message='Website has recovered, total downtime %.2f minutes' % downtime
)
requests.post('https://api.pushover.net/1/messages.json', data=data)
down_at = None
passes = 0
else:
print('Down')
if down_at is None:
down_at = datetime.now()
elif passes == 5:
passes = 0
else:
# Avoid notification spam, do not send outage notification for 5 mins
print('Pass', passes)
passes += 1
time.sleep(CHECK_SECONDS)
continue
print('Send down notification')
data.update(
title='SITE IS DOWN',
message='Website is down since {}'.format(down_at.strftime('%H:%M'))
)
requests.post('https://api.pushover.net/1/messages.json', data=data)
time.sleep(CHECK_SECONDS)
if __name__ == '__main__':
monitor()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment