Unifi Syslog with Alloy and Loki

Unifi network devices generate valuable logs that can help you troubleshoot network issues and monitor your devices. By sending these syslog messages to Loki using Grafana Alloy, you can centralize your network logs alongside your application logs for unified observability.

This guide we will only focus on device logs. Securtiy and firewall logs are out of scope.

Prerequisites

Why Grafana Alloy?

Grafana Alloy is the next-generation telemetry collector that replaces Promtail. It supports multiple data formats including logs, metrics, traces, and profiles. For syslog collection, Alloy provides:

Setup Grafana Alloy

First, create a folder to hold the Docker Compose file and Alloy configuration:

BASH
1mkdir alloy
Click to expand and view more

Create the Docker Compose file:

BASH
1nano alloy/docker-compose.yml
Click to expand and view more

Paste the following content:

docker-compose.yml
 1services:
 2  alloy:
 3    image: grafana/alloy:latest
 4    container_name: alloy
 5    restart: unless-stopped
 6    environment:
 7      - TZ=Europe/Amsterdam
 8    ports:
 9      - "12345:12345"
10      - "514:514/udp"  # Syslog UDP
11    volumes:
12      - ./config.alloy:/etc/alloy/config.alloy:ro
13      - alloy-data:/var/lib/alloy/data
14    command:
15      - run
16      - --server.http.listen-addr=0.0.0.0:12345
17      - --storage.path=/var/lib/alloy/data
18      - /etc/alloy/config.alloy
19    networks:
20      - backend
21
22networks:
23  backend:
24    name: backend
25
26volumes:
27  alloy-data:
28    name: alloy-data
Click to expand and view more

Configure Alloy for Syslog

Create the Alloy configuration file:

BASH
1nano alloy/config.alloy
Click to expand and view more

Paste the following configuration:

config.alloy
 1/* UniFi Syslog (RFC3164) - Relabel rules to capture syslog metadata */
 2loki.relabel "unifi_syslog" {
 3  forward_to = []
 4
 5  // Copy severity first (for error, debug, or unknown values)
 6  rule {
 7    source_labels = ["__syslog_message_severity"]
 8    target_label  = "detected_level"
 9  }
10  // Then normalize specific values (these overwrite the above)
11  rule {
12    source_labels = ["__syslog_message_severity"]
13    regex         = "(?i)(emergency|alert|critical)"
14    target_label  = "detected_level"
15    replacement   = "critical"
16  }
17  rule {
18    source_labels = ["__syslog_message_severity"]
19    regex         = "(?i)(warning)"
20    target_label  = "detected_level"
21    replacement   = "warn"
22  }
23  rule {
24    source_labels = ["__syslog_message_severity"]
25    regex         = "(?i)(notice|informational)"
26    target_label  = "detected_level"
27    replacement   = "info"
28  }
29
30  // Map hostname from syslog header
31  rule {
32    source_labels = ["__syslog_message_hostname"]
33    target_label  = "host"
34  }
35
36  // Map app name from syslog header
37  rule {
38    source_labels = ["__syslog_message_app_name"]
39    target_label  = "app"
40  }
41}
42
43loki.source.syslog "unifi" {
44  listener {
45    address       = "0.0.0.0:514"
46    protocol      = "udp"
47    syslog_format = "rfc3164"
48    use_incoming_timestamp = false
49    labels        = {
50      job      = "unifi",
51    }
52  }
53
54  relabel_rules = loki.relabel.unifi_syslog.rules
55  forward_to    = [loki.process.unifi.receiver]
56}
57
58loki.process "unifi" {
59  // Extract app and message from UniFi syslog content
60  // Format 1 (AP/Switch): "mac,device-firmware: process[pid]: message"
61  //   Example: "6c63f8863465,U7-Pro-Wall-8.3.2+18064: hostapd[5343]: wifi1ap6: STA ..."
62  // Format 2 (Gateway): "hostname process[pid]: message"
63  //   Example: "UCG-Fiber bash[2616997]: HISTORY: ..."
64  stage.regex {
65    expression = "^(?:[\\w,\\-\\.\\+]+:\\s+|[\\w\\-]+\\s+)?(?P<app>[\\w\\-]+)(?:\\[\\d+\\])?:\\s*(?P<message>.*)$"
66  }
67
68  // Set app label from regex
69  stage.labels {
70    values = {
71      app = "",
72    }
73  }
74
75  // Output just the message content
76  stage.output {
77    source = "message"
78  }
79
80  forward_to = [loki.write.default.receiver]
81}
82
83// Loki write endpoint
84loki.write "default" {
85  endpoint {
86    url = "http://loki:3100/loki/api/v1/push"
87  }
88}
Click to expand and view more

Configuration Breakdown

This configuration includes three main components that work together to collect, process, and forward Unifi syslog messages to Loki.

1. Relabel Rules (loki.relabel "unifi_syslog")

The relabel component extracts and normalizes metadata from the syslog headers:

Severity Normalization:

Metadata Extraction:

These labels make it easy to filter logs by device or severity without parsing the log content.

2. Syslog Listener (loki.source.syslog "unifi")

The syslog source receives incoming log messages:

Listener Configuration:

Static Labels:

Pipeline:

3. Log Processing (loki.process "unifi")

This component parses and cleans Unifi-specific log formats:

Regex Extraction: Unifi devices use different log formats depending on the device type:

The regex pattern captures:

Output Stage:

4. Loki Write (loki.write "default")

The final component sends processed logs to Loki:

Start Alloy

Deploy the Alloy container:

BASH
1docker compose -f alloy/docker-compose.yml up -d
Click to expand and view more

Verify Alloy is running and check the logs:

BASH
1docker logs alloy
Click to expand and view more

You should see messages indicating that Alloy has started and the syslog listener is active.

Configure Unifi Devices

Now configure your Unifi Controller to send syslog messages to Alloy.

Using Unifi Controller

  1. Open your Unifi Controller
  2. Navigate to Settings > Cyber Secure
  3. Go to to Traffic Logging
  4. Set Activity Logging (Syslog) to SIEM Server
  5. Set the IP Address to your Alloy server IP (e.g., 192.168.1.100)
  6. Set the Port to 514
  7. Click Apply Changes

Verify Log Collection

After configuring your Unifi devices, logs should start flowing to Loki within seconds.

Check in Grafana

  1. Open Grafana and navigate to Explore
  2. Select Loki as the datasource
  3. Run a query to see Unifi logs:
PLAINTEXT
{job="unifi"}
Click to expand and view more

You should see syslog messages from your Unifi devices, including authentication events, DHCP assignments, wireless connections, and security events.

Example Queries

With the labels and processing configured above, you can create powerful queries to analyze your network logs.

Basic Queries

All Unifi logs:

PLAINTEXT
{job="unifi"}
Click to expand and view more

Logs from a specific device:

PLAINTEXT
{job="unifi", host="UCG-Fiber"}
Click to expand and view more

Logs from a specific application:

PLAINTEXT
{job="unifi", app="hostapd"}
Click to expand and view more

Summary

With Grafana Alloy configured to receive Unifi syslog messages, you now have centralized network logging alongside your application logs in Loki. This unified observability platform makes it easier to correlate network events with application behavior, troubleshoot issues, and maintain security.

The modern Alloy architecture provides better performance and more flexible log processing compared to traditional syslog daemons, while integrating seamlessly with the Grafana ecosystem.

Copyright Notice

Author: Sven van Ginkel

Link: https://svenvg.com/posts/unifi-syslog-with-alloy-and-loki/

License: CC BY-NC-SA 4.0

This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License. Please attribute the source, use non-commercially, and maintain the same license.

Start searching

Enter keywords to search articles

↑↓
ESC
⌘K Shortcut