Where a network anomaly detector should live

A script on the router or logic in a flow collector that is already running - how the plan shrank by half and what a run over the history showed.

After the story with the set-top box that held 5051 connections, I needed alerts about anomalous device behaviour in my home network. The first plan for the detector turned out to be twice as complex as it needed to be.

A plan built around a RouterOS script

The task in the tracker carried a requirement: the detector is written as a RouterOS script. The argument sounded convincing: the detector should run inside the thing being watched, not inside the watcher.

The agent accepted this requirement and built a plan around it:

  • a new firewall rule;
  • a script of about 300 lines that parses conntrack on its own;
  • state between runs kept in the comments of address-list entries;
  • a day of dry run to calibrate the thresholds.

The plan would have worked, but it redid something I already had.

A flow collector instead of a script

By that time both sites had a flow collector running. It keeps a permanent session with the router over the binary API, refreshes the "who talks to whom" map every 10 seconds, and stores the history in SQLite.

I asked the agent what would happen if we gave this to the collector. Parsing conntrack with a script would have been a second, worse copy of the same work, and it would have loaded the router exactly at the moment of an incident. I gave the detectors to the collector.

What moving it to the collector gave me

The plan shrank by about half, but the savings were not the main result:

  • the thresholds are picked from history. The collector had 48 hours of flows, and I ran the same detector code over them in -backtest mode. I could see what would have fired and on whom before turning anything on;
  • the alert text now has the country, the ASN and the domain of the target. A script on the router would not have had this data at all;
  • no changes to the production firewall were needed at all. Rollout is docker compose, rollback is the previous image;
  • one of the planned detectors turned out to be pointless. On the history, the "wide scan" could not tell scanning from an ordinary P2P client, and the threshold from the task could not be reproduced.

I consider the last point the most useful one. On the router this would have surfaced only after a week of false alerts. Now, if the history is already there, a new detector is first run over it, and the report gets numbers: over 48 hours it would have fired N times, and here is on whom.

The price of the move

  • the detector sees the network with a delay of up to 10 seconds;
  • the collector became a critical component, before that its failure cost only an empty map;
  • 48 hours of history do not cover weekly cycles, so the thresholds will have to be rechecked.

If the collector goes down, the network is left unattended. But covering that is cheaper than moving the logic to the router: an Uptime Kuma monitor watches the collectors themselves. If a collector goes silent, a separate alert arrives.

What I now ask before adding a new watchdog

  1. Who already collects the data I need?
  2. What will the watched system pay for doing the computation itself, especially at the moment of an incident?
  3. How do I cover the failure of the watcher?

This time the answers were these. The flow collector was already gathering the data. Doing the computation itself would have cost the router load exactly at the moment of an incident. And a separate monitor watches the collector.

Tags: Diagnostics, MikroTik, Monitoring, Networking

LinkedIn X Telegram

Read next

Comments