Listens for syslog from two devices with on syslog, counts the events and prints the serious ones.
This program listens for syslog messages from two devices. An on syslog
block in the body of the class counts each event by device and severity, and
prints the events of severity 3 (error) or worse. At the end the program
prints what the listener received and the counts.
Run it from this folder, on the saved events in saved:
bogo run --replay saved syslog_listener.bogo
bogo test syslog_listener.bogo
With --replay saved no port is opened. The events are read from
saved/listen/syslog.txt, and listen() returns when all of them are
handled. One saved event comes from a sender that is not in devices, so it
is counted as "from other senders" and not handled. The events were written by
hand, and the names and addresses are invented.
To use it on your own network, put your devices in devices and point their
syslog at this computer, UDP port 5514. Without --replay the program really
opens UDP port 5514 on every address of the computer and listens for one
minute (listen(1min)). The firewall of the computer must let the packets in.
Port 514, the usual syslog port, needs an administrator on most systems.
syslog_listener.bogo
edition2027"""Listen for syslog from two devices, count the events and print the serious ones."""classSyslogListener:
"""Counts the syslog events of each device by severity.
The `on syslog` block stands in the class body. Its listener opens when the class
is read, before main runs. Packets from a sender not in `devices` are not handled.
"""
devices = [
Device(name="core-sw1", ip=192.0.2.1, platform="iosxe"),
Device(name="edge-rt2", ip=192.0.2.2, platform="iosxe"),
]
serious = 3# severity 0 to 3: emergency, alert, critical, error
counts: dict[str, int] = {}
on syslog port 5514from devices as e:
SyslogListener.tally(SyslogListener.counts, e.device, e.severity)
ifSyslogListener.is_serious(e.severity):
print(f"{e.received} {e.device:9} {e.code}: {e.message}")
@staticmethoddef is_serious(severity: int) -> bool:
return severity <= SyslogListener.serious
@staticmethoddef tally(counts: dict[str, int], device: str, severity: int) -> None:
key = f"{device} severity {severity}"
counts[key] = counts.get(key, 0) + 1 @staticmethoddef main(args: list[str]) -> None:
print("Serious events as they arrive:")
for c in listen(1min):
print()
print(
f"{c.received} received, {c.handled} handled, {c.not_from} from other senders, "f"{c.unreadable} unreadable"
)
print()
for key, n in sorted(SyslogListener.counts.items()):
print(f"{n:4} {key}")
classTestSyslogListener:
def test_serious_is_error_or_worse(self) -> None:
assertSyslogListener.is_serious(0)
assertSyslogListener.is_serious(3)
assertnotSyslogListener.is_serious(5)
def test_tally_counts_per_device_and_severity(self) -> None:
counts: dict[str, int] = {}
SyslogListener.tally(counts, "core-sw1", 3)
SyslogListener.tally(counts, "core-sw1", 3)
SyslogListener.tally(counts, "edge-rt2", 5)
assert counts == {"core-sw1 severity 3": 2, "edge-rt2 severity 5": 1}