only print when net fails
This commit is contained in:
parent
32d9b4d658
commit
b90a32aad5
19
src/main.py
19
src/main.py
|
@ -1,22 +1,24 @@
|
|||
import http.client
|
||||
import daemon
|
||||
from time import sleep
|
||||
from datetime import datetime
|
||||
|
||||
TIMEOUT: float = 1
|
||||
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] = []
|
||||
fmsg: str = ""
|
||||
for domain in TESTDOMAINS:
|
||||
status, msg = reachable(domain)
|
||||
statuses.append(status)
|
||||
print(f"{domain:60s} {msg}")
|
||||
fmsg += (f"{domain:60s} {msg}\n")
|
||||
|
||||
overall = False
|
||||
for s in statuses:
|
||||
overall |= s
|
||||
return overall
|
||||
return overall, fmsg
|
||||
|
||||
def reachable(domain: str) -> tuple[bool, str]:
|
||||
try:
|
||||
|
@ -24,16 +26,17 @@ def reachable(domain: str) -> tuple[bool, str]:
|
|||
conn.request("GET", "/")
|
||||
r = conn.getresponse()
|
||||
conn.close()
|
||||
return True, r.status
|
||||
return True, r.status.__str__()
|
||||
except Exception as e:
|
||||
return False, e.__str__()
|
||||
|
||||
def monitor():
|
||||
while True:
|
||||
try:
|
||||
b = check_net()
|
||||
if b: print(":SUCCESS:")
|
||||
else: print(":FAILURE:")
|
||||
b, msg = check_net()
|
||||
if not b:
|
||||
print(f":FAILURE AT {datetime.now()}:")
|
||||
print(msg)
|
||||
sleep(SLEEPTIME)
|
||||
except Exception as e:
|
||||
print(e)
|
||||
|
|
Loading…
Reference in New Issue