Watches the loss to two hosts, then raises and clears an alert with alert when.
This program shows watch and alert when. First it watches the packet loss
to two hosts for 3 seconds. Then it pings them each second in an every
block, and raises an alert for a host whose loss stays above 5 percent for 2
seconds. A raised alert clears once when the loss comes back down.
Run it from this folder, on the saved answers in saved:
bogo run --replay saved watch_alert.bogo
bogo test watch_alert.bogo
With --replay saved no ping is sent. A replay gives the same answer to every
ping of a host, so the second host stays at 50 percent loss: its alert is
raised and never cleared. The saved answers were written by hand, and the
addresses are documentation addresses.
To use it on your own network, put your hosts in hosts, and set limit and
hold (a real alert usually waits minutes, not seconds). Remove turns and
its break to run until Ctrl+C. Without --replay the program really sends
pings. In place of print, an alert can post to a chat tool with webhook.
watch_alert.bogo
edition2027"""Watch the loss to two hosts, then raise an alert when the loss stays high."""classWatchAlert:
"""Pings two hosts each second. A host whose loss stays above the limit for the
time given raises an alert once, and clears it once when the loss comes back down.
"""
hosts = [192.0.2.10, 198.51.100.20]
limit = 5pct# loss above this is bad
hold = 2s# for this long before the alert is raised
turns = 4# the program stops after this many turns; remove it to run until Ctrl+C @staticmethoddef losses(hosts: list[IP]) -> dict[str, Percent]:
return {str(h): ping(h, count=4).loss for h in hosts}
@staticmethoddef bad(r: PingResult, limit: Percent) -> bool:
return r.loss > limit
@staticmethoddef message(host: IP, r: PingResult, raised: bool, lasted: Duration) -> str:
if raised:
returnf"ALERT {host}: loss {r.loss} for {lasted}"returnf"CLEAR {host}: back to normal after {lasted}" @staticmethoddef main(args: list[str]) -> None:
w = WatchAlert
print("Watching the loss for 3 seconds:")
watch w.losses(w.hosts) every1sfor3s
print(f"Alert when the loss stays above {w.limit} for {w.hold}:")
turn = 0every1s:
turn += 1for h in w.hosts:
r = ping(h, count=4)
alertwhen w.bad(r, w.limit) for2s per h as a:
print(w.message(h, r, a.raised, a.lasted))
if turn >= w.turns:
break
print(f"Done after {turn} turns.")
classTestWatchAlert:
def test_bad_is_above_the_limit_only(self) -> None:
half = PingResult(4, 2, 50pct, None, None, None)
none = PingResult(4, 4, 0pct, 20ms, 21ms, 23ms)
assertWatchAlert.bad(half, 5pct)
assertnotWatchAlert.bad(none, 5pct)
def test_messages(self) -> None:
r = PingResult(4, 2, 50pct, None, None, None)
assert (
WatchAlert.message(192.0.2.10, r, True, 3s)
== "ALERT 192.0.2.10: loss 50pct for 3s"
)
assertWatchAlert.message(192.0.2.10, r, False, 1min).startswith("CLEAR 192.0.2.10")