49 lines
1.3 KiB
Python
49 lines
1.3 KiB
Python
#!/usr/bin/env python3
|
|||
|
|
"""模拟页面 pollStatus:5s 超时算失败,连续失败即离线。"""
|
||
|
|
import time
|
||
|
|
import urllib.request
|
||
|
|
import urllib.error
|
||
|
|
|
||
|
|
URL = "http://192.168.31.127/system/status"
|
||
|
|
TIMEOUT = 5.0
|
||
|
|
POLL = 1.0
|
||
|
|
WINDOW = 75
|
||
|
|
|
||
|
|
|
||
|
|
def probe():
|
||
|
|
t0 = time.time()
|
||
|
|
try:
|
||
|
|
with urllib.request.urlopen(URL, timeout=TIMEOUT) as r:
|
||
|
|
r.read(64)
|
||
|
|
return True, "HTTP %s" % r.status, time.time() - t0
|
||
|
|
except urllib.error.HTTPError as e:
|
||
|
|
return False, "HTTP %s" % e.code, time.time() - t0
|
||
|
|
except Exception as e:
|
||
|
|
return False, "%s: %s" % (type(e).__name__, str(e)[:80]), time.time() - t0
|
||
|
|
|
||
|
|
|
||
|
|
def main():
|
||
|
|
print("=== monitor start (mimic pollStatus) ===", flush=True)
|
||
|
|
prev = None
|
||
|
|
fails = 0
|
||
|
|
t_end = time.time() + WINDOW
|
||
|
|
while time.time() < t_end:
|
||
|
|
ok, msg, dt = probe()
|
||
|
|
now = time.strftime("%H:%M:%S")
|
||
|
|
if ok:
|
||
|
|
fails = 0
|
||
|
|
state = "ONLINE"
|
||
|
|
else:
|
||
|
|
fails += 1
|
||
|
|
state = "FAIL#%d" % fails + (" -> OFFLINE" if fails >= 1 else "")
|
||
|
|
mark = " *" if state != prev else ""
|
||
|
|
print("[%s] %s (%s, %.0fms)%s" % (now, state, msg, dt * 1000, mark), flush=True)
|
||
|
|
if state != prev:
|
||
|
|
prev = state
|
||
|
|
time.sleep(POLL)
|
||
|
|
print("=== monitor end ===", flush=True)
|
||
|
|
|
||
|
|
|
||
|
|
if __name__ == "__main__":
|
||
|
|
main()
|