1 { config, lib, pkgs, ... }:
6 cfg = config.services.unifi-poller;
8 configFile = pkgs.writeText "unifi-poller.json" (generators.toJSON {} {
9 inherit (cfg) poller influxdb loki prometheus unifi;
13 options.services.unifi-poller = {
14 enable = mkEnableOption (lib.mdDoc "unifi-poller");
20 description = lib.mdDoc ''
21 Turns on line numbers, microsecond logging, and a per-device log.
22 This may be noisy if you have a lot of devices. It adds one line per device.
28 description = lib.mdDoc ''
29 Turns off per-interval logs. Only startup and error logs will be emitted.
33 type = with types; listOf str;
35 description = lib.mdDoc ''
36 Load additional plugins.
45 description = lib.mdDoc ''
46 Whether to disable the prometheus ouput plugin.
49 http_listen = mkOption {
51 default = "[::]:9130";
52 description = lib.mdDoc ''
53 Bind the prometheus exporter to this IP or hostname.
56 report_errors = mkOption {
59 description = lib.mdDoc ''
60 Whether to report errors.
69 description = lib.mdDoc ''
70 Whether to disable the influxdb ouput plugin.
75 default = "http://127.0.0.1:8086";
76 description = lib.mdDoc ''
77 URL of the influxdb host.
82 default = "unifipoller";
83 description = lib.mdDoc ''
84 Username for the influxdb.
89 default = pkgs.writeText "unifi-poller-influxdb-default.password" "unifipoller";
90 defaultText = literalExpression "unifi-poller-influxdb-default.password";
91 description = lib.mdDoc ''
92 Path of a file containing the password for influxdb.
93 This file needs to be readable by the unifi-poller user.
95 apply = v: "file://${v}";
100 description = lib.mdDoc ''
101 Database name. Database should exist.
104 verify_ssl = mkOption {
107 description = lib.mdDoc ''
108 Verify the influxdb's certificate.
111 interval = mkOption {
114 description = lib.mdDoc ''
115 Setting this lower than the Unifi controller's refresh
116 interval may lead to zeroes in your database.
125 description = lib.mdDoc ''
126 URL of the Loki host.
132 description = lib.mdDoc ''
138 default = pkgs.writeText "unifi-poller-loki-default.password" "";
139 defaultText = "unifi-poller-influxdb-default.password";
140 description = lib.mdDoc ''
141 Path of a file containing the password for Loki.
142 This file needs to be readable by the unifi-poller user.
144 apply = v: "file://${v}";
146 verify_ssl = mkOption {
149 description = lib.mdDoc ''
150 Verify Loki's certificate.
153 tenant_id = mkOption {
156 description = lib.mdDoc ''
157 Tenant ID to use in Loki.
160 interval = mkOption {
163 description = lib.mdDoc ''
164 How often the events are polled and pushed to Loki.
170 description = lib.mdDoc ''
171 Should be increased in case of timeout errors.
177 controllerOptions = {
181 description = lib.mdDoc ''
182 Unifi service user name.
187 default = pkgs.writeText "unifi-poller-unifi-default.password" "unifi";
188 defaultText = literalExpression "unifi-poller-unifi-default.password";
189 description = lib.mdDoc ''
190 Path of a file containing the password for the unifi service user.
191 This file needs to be readable by the unifi-poller user.
193 apply = v: "file://${v}";
197 default = "https://unifi:8443";
198 description = lib.mdDoc ''
199 URL of the Unifi controller.
203 type = with types; either (enum [ "default" "all" ]) (listOf str);
205 description = lib.mdDoc ''
206 List of site names for which statistics should be exported.
207 Or the string "default" for the default site or the string "all" for all sites.
211 save_ids = mkOption {
214 description = lib.mdDoc ''
215 Collect and save data from the intrusion detection system to influxdb and Loki.
218 save_events = mkOption {
221 description = lib.mdDoc ''
222 Collect and save data from UniFi events to influxdb and Loki.
225 save_alarms = mkOption {
228 description = lib.mdDoc ''
229 Collect and save data from UniFi alarms to influxdb and Loki.
232 save_anomalies = mkOption {
235 description = lib.mdDoc ''
236 Collect and save data from UniFi anomalies to influxdb and Loki.
239 save_dpi = mkOption {
242 description = lib.mdDoc ''
243 Collect and save data from deep packet inspection.
244 Adds around 150 data points and impacts performance.
247 save_sites = mkOption {
250 description = lib.mdDoc ''
251 Collect and save site data.
254 hash_pii = mkOption {
257 description = lib.mdDoc ''
258 Hash, with md5, client names and MAC addresses. This attempts
259 to protect personally identifiable information.
262 verify_ssl = mkOption {
265 description = lib.mdDoc ''
266 Verify the Unifi controller's certificate.
275 description = lib.mdDoc ''
276 Let prometheus select which controller to poll when scraping.
277 Use with default credentials. See unifi-poller wiki for more.
281 defaults = controllerOptions;
283 controllers = mkOption {
284 type = with types; listOf (submodule { options = controllerOptions; });
286 description = lib.mdDoc ''
287 List of Unifi controllers to poll. Use defaults if empty.
289 apply = map (flip removeAttrs [ "_module" ]);
294 config = mkIf cfg.enable {
295 users.groups.unifi-poller = { };
296 users.users.unifi-poller = {
297 description = "unifi-poller Service User";
298 group = "unifi-poller";
302 systemd.services.unifi-poller = {
303 wantedBy = [ "multi-user.target" ];
304 after = [ "network.target" ];
306 ExecStart = "${pkgs.unifi-poller}/bin/unifi-poller --config ${configFile}";
310 ProtectSystem = "full";
311 DevicePolicy = "closed";
312 NoNewPrivileges = true;
313 User = "unifi-poller";
314 WorkingDirectory = "/tmp";