only print when net fails

This commit is contained in:
Christoph J. Scherr 2023-05-28 14:56:56 +02:00
parent 32d9b4d658
commit b90a32aad5
Signed by: PlexSheep
GPG Key ID: 25B4ACF7D88186CC
1 changed files with 11 additions and 8 deletions

View File

@ -1,22 +1,24 @@
import http.client import http.client
import daemon import daemon
from time import sleep from time import sleep
from datetime import datetime
TIMEOUT: float = 1 TIMEOUT: float = 1
TESTDOMAINS: list[str] = ["cscherr.de", "debian.org", "cloudflare.com"] TESTDOMAINS: list[str] = ["cscherr.de", "debian.org", "cloudflare.com"]
SLEEPTIME: int = 600 # 10 minutes SLEEPTIME: int = 300 # 5 minutes
def check_net() -> bool: def check_net() -> tuple[bool, str]:
statuses: list[bool] = [] statuses: list[bool] = []
fmsg: str = ""
for domain in TESTDOMAINS: for domain in TESTDOMAINS:
status, msg = reachable(domain) status, msg = reachable(domain)
statuses.append(status) statuses.append(status)
print(f"{domain:60s} {msg}") fmsg += (f"{domain:60s} {msg}\n")
overall = False overall = False
for s in statuses: for s in statuses:
overall |= s overall |= s
return overall return overall, fmsg
def reachable(domain: str) -> tuple[bool, str]: def reachable(domain: str) -> tuple[bool, str]:
try: try:
@ -24,16 +26,17 @@ def reachable(domain: str) -> tuple[bool, str]:
conn.request("GET", "/") conn.request("GET", "/")
r = conn.getresponse() r = conn.getresponse()
conn.close() conn.close()
return True, r.status return True, r.status.__str__()
except Exception as e: except Exception as e:
return False, e.__str__() return False, e.__str__()
def monitor(): def monitor():
while True: while True:
try: try:
b = check_net() b, msg = check_net()
if b: print(":SUCCESS:") if not b:
else: print(":FAILURE:") print(f":FAILURE AT {datetime.now()}:")
print(msg)
sleep(SLEEPTIME) sleep(SLEEPTIME)
except Exception as e: except Exception as e:
print(e) print(e)