make last state persistent, fix exceptions
This commit is contained in:
parent
b92cf90a0b
commit
b66b63e6bd
1 changed files with 11 additions and 2 deletions
13
monitor.py
13
monitor.py
|
@ -4,6 +4,8 @@ import time
|
||||||
import logging
|
import logging
|
||||||
import datetime
|
import datetime
|
||||||
import socket
|
import socket
|
||||||
|
import json
|
||||||
|
import os
|
||||||
|
|
||||||
import requests
|
import requests
|
||||||
import pydig
|
import pydig
|
||||||
|
@ -62,7 +64,7 @@ def self_check():
|
||||||
try:
|
try:
|
||||||
if requests.get("https://google.com/").status_code != 200:
|
if requests.get("https://google.com/").status_code != 200:
|
||||||
return False
|
return False
|
||||||
except ConnectionError:
|
except requests.exceptions.ConnectionError:
|
||||||
return False
|
return False
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
@ -72,7 +74,7 @@ def http_requirement(url: str, code: int) -> bool:
|
||||||
for i in range(2):
|
for i in range(2):
|
||||||
try:
|
try:
|
||||||
resp = requests.get(url)
|
resp = requests.get(url)
|
||||||
except ConnectionError:
|
except requests.exceptions.ConnectionError:
|
||||||
passed = False
|
passed = False
|
||||||
else:
|
else:
|
||||||
passed = resp.status_code == code
|
passed = resp.status_code == code
|
||||||
|
@ -140,4 +142,11 @@ monitors = {
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
# we assume this is gonna be run in a cron job as the gitpython
|
# we assume this is gonna be run in a cron job as the gitpython
|
||||||
# library is slowly leaking memory apparently
|
# library is slowly leaking memory apparently
|
||||||
|
if os.path.exists("last-state"):
|
||||||
|
with open("last-state", 'r') as f:
|
||||||
|
last_states = json.load(f)
|
||||||
|
|
||||||
check(monitors)
|
check(monitors)
|
||||||
|
|
||||||
|
with open("last-state", 'w+') as f:
|
||||||
|
json.dump(last_states, f)
|
||||||
|
|
Loading…
Reference in a new issue