Skip to content

Version 0.1.0 is a first version, for trial. See what is not proven →

bogolang
FR

← Examples

watch_alert

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
edition 2027
"""Watch the loss to two hosts, then raise an alert when the loss stays high."""


class WatchAlert:
    """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

    @staticmethod
    def losses(hosts: list[IP]) -> dict[str, Percent]:
        return {str(h): ping(h, count=4).loss for h in hosts}

    @staticmethod
    def bad(r: PingResult, limit: Percent) -> bool:
        return r.loss > limit

    @staticmethod
    def message(host: IP, r: PingResult, raised: bool, lasted: Duration) -> str:
        if raised:
            return f"ALERT {host}: loss {r.loss} for {lasted}"
        return f"CLEAR {host}: back to normal after {lasted}"

    @staticmethod
    def main(args: list[str]) -> None:
        w = WatchAlert
        print("Watching the loss for 3 seconds:")
        watch w.losses(w.hosts) every 1s for 3s

        print(f"Alert when the loss stays above {w.limit} for {w.hold}:")
        turn = 0
        every 1s:
            turn += 1
            for h in w.hosts:
                r = ping(h, count=4)
                alert when w.bad(r, w.limit) for 2s per h as a:
                    print(w.message(h, r, a.raised, a.lasted))
            if turn >= w.turns:
                break
        print(f"Done after {turn} turns.")


class TestWatchAlert:
    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)
        assert WatchAlert.bad(half, 5pct)
        assert not WatchAlert.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"
        )
        assert WatchAlert.message(192.0.2.10, r, False, 1min).startswith("CLEAR 192.0.2.10")