The LabDetection
Grafana Loki
Central log store. Log-based detection rules run here and alert on matches.
How it connects
Sends to Loki
Receives from Loki
- AlertmanagerAn alert each time a detection rule matches
- GrafanaLog search and dashboards
- PrometheusLoki's own health metrics
What it does here
Loki is the lab’s central log store. Every server sends its system log to it, and so do the firewall, the DNS filter and Falco, the runtime detection tool. With the logs in one place, a single query can follow an event across machines, and the logs remain available when the machine that wrote them is rebuilt or can no longer be trusted.
Loki also runs the lab’s log-based detection rules. A rule is a query in LogQL, Loki’s query language, that runs once a minute. When the query returns a result, Loki raises an alert and hands it to Alertmanager, which sends a notification. The rule below alerts when a player who is not on the allowlist tries to join a game server:
- alert: GameServerJoinNotAllowlisted # Count the log lines of the last 5 minutes that say "not whitelisted" # (the server's own wording). Alert when there is at least one. expr: | sum(count_over_time( {host="game-server", unit="minecraft.service"} |~ "(?i)not white-?listed" [5m] )) > 0 labels: severity: warningThe rules in use cover two areas:
- The IoT network. A device that tries to reach a private network, to use a port that is not allowed, or to use DNS over HTTPS (encrypted DNS, which would take its lookups past the DNS filter). One more rule alerts when a device looks up a domain outside its recorded baseline.
- A game server. A join attempt by a player who is not on the allowlist, a change of operator rights or permissions, and a failed account verification.
Logs are kept for 31 days and then deleted.
A detection that receives no logs stays silent, so the path into Loki is tested all the time. Every server writes a test line to its log once a minute, and a scheduled check confirms that the lines from each server still arrive. A missing line raises an alert of its own.
Why Loki
Loki indexes a few labels per log stream, such as the host and the service, and not the text of each line. It works like a filing cabinet in which the folders are labelled but the pages are not indexed: the right folder is found at once, and then its pages are read through. A full-text index, such as the one in Elasticsearch, finds any word quickly but needs far more memory and disk.
For a lab, that trade works well:
- Small footprint. Loki runs as a single process with its data on local disk.
- One alerting path. Loki rules use the same format as Prometheus rules and send their alerts to the same Alertmanager, so log alerts and metric alerts share routing, grouping and silences.
- One place to look. Grafana reads both Loki and Prometheus, so a dashboard can show a metric together with the log lines behind it.
The cost is that Loki is a log store and not a SIEM (security information and event management system). It has no built-in detection rules, no correlation across data sources and no case management, and a text search across all logs over a long period is slow. Every rule in the lab is written by hand for one specific question.
Notes from running it
Rule files need a tenant folder
Loki can keep the data of several tenants (separate users or teams) apart. With
auth_enabled: false there is only one tenant, and Loki names it fake.
The ruler, the part of Loki that evaluates alert rules, still looks for rule files in one folder per tenant. Rule files placed directly in the rules directory are ignored without an error.
ruler: storage: type: local local: # Rule files go one level down, in /etc/loki/rules/fake/ directory: /etc/loki/rulesAllow enough time for a clean stop
Loki holds recent log lines in memory before it writes them to storage. On a normal stop it can write them out first, but that takes time: about 23 seconds in this lab, while Docker waits 10 seconds by default before it kills a container.
Both settings are needed:
ingester: wal: # On a normal stop, write buffered lines to storage. flush_on_shutdown: trueservices: loki: # Docker's default is 10 seconds. stop_grace_period: 2mFlush before a backup of the data folder
POST /flush answers at once and does the work in the background, so a copy of the data
folder taken right after it can miss log lines.
The shutdown endpoint with terminate=false answers with status 204 only when
everything is written:
# Write all buffered log lines to storage and wait for the result.# After this call Loki accepts no new lines, so stop the container next.curl -X POST \ 'http://localhost:3100/ingester/shutdown?flush=true&terminate=false&delete_ring_tokens=false'After a restore, check the data itself. The /ready endpoint only shows that the
process has started. A query for a period before the backup shows that the old logs are
back.
A delete request needs disk space before it frees any
To delete log lines, Loki first writes new copies of the affected data without those
lines. The originals are removed after a delay (retention_delete_delay, two hours by
default).
A large delete therefore makes disk use rise before it falls, which matters on a small disk.
Health is checked from outside the container
The lab runs Loki from a distroless image: an image with no shell and no tools such as
curl, so a Docker health check has nothing to run.
Prometheus collects Loki’s metrics instead and alerts when it cannot reach it.