Created
January 30, 2020 09:09
-
-
Save aluminiumgeek/81309e99a89c345db06e837cba50dc86 to your computer and use it in GitHub Desktop.
[Python] Simple website downtime monitor (+ Pushover)
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
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