summary refs log tree commit diff
path: root/nixos/modules/services/monitoring/prometheus/unifi-exporter.nix
blob: 0a56d6ae95a59b917cf83465b75ec65ebdfc823f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
{ config, pkgs, lib, ... }:

with lib;

let
  cfg = config.services.prometheus.unifiExporter;
in {
  options = {
    services.prometheus.unifiExporter = {
      enable = mkEnableOption "prometheus unifi exporter";

      port = mkOption {
        type = types.int;
        default = 9130;
        description = ''
          Port to listen on.
        '';
      };

      unifiAddress = mkOption {
        type = types.str;
        example = "https://10.0.0.1:8443";
        description = ''
          URL of the UniFi Controller API.
        '';
      };

      unifiInsecure = mkOption {
        type = types.bool;
        default = false;
        description = ''
          If enabled skip the verification of the TLS certificate of the UniFi Controller API.
          Use with caution.
        '';
      };
      
      unifiUsername = mkOption {
        type = types.str;
        example = "ReadOnlyUser";
        description = ''
          username for authentication against UniFi Controller API.
        '';
      };
      
      unifiPassword = mkOption {
        type = types.str;
        description = ''
          Password for authentication against UniFi Controller API.
        '';
      };
      
      unifiTimeout = mkOption {
        type = types.str;
        default = "5s";
        example = "2m";
        description = ''
          Timeout including unit for UniFi Controller API requests.
        '';
      };

      extraFlags = mkOption {
        type = types.listOf types.str;
        default = [];
        description = ''
          Extra commandline options when launching the unifi exporter.
        '';
      };

      openFirewall = mkOption {
        type = types.bool;
        default = false;
        description = ''
          Open port in firewall for incoming connections.
        '';
      };
    };
  };

  config = mkIf cfg.enable {
    networking.firewall.allowedTCPPorts = optional cfg.openFirewall cfg.port;

    systemd.services.prometheus-unifi-exporter = {
      description = "Prometheus exporter for UniFi Controller metrics";
      unitConfig.Documentation = "https://github.com/mdlayher/unifi_exporter";
      wantedBy = [ "multi-user.target" ];
      after = optional config.services.unifi.enable "unifi.service";
      serviceConfig = {
        User = "nobody";
        Restart = "always";
        PrivateTmp = true;
        WorkingDirectory = /tmp;
        ExecStart = ''
          ${pkgs.prometheus-unifi-exporter}/bin/unifi_exporter \
            -telemetry.addr :${toString cfg.port} \
            -unifi.addr ${cfg.unifiAddress} \
            -unifi.username ${cfg.unifiUsername} \
            -unifi.password ${cfg.unifiPassword} \
            -unifi.timeout ${cfg.unifiTimeout} \
            ${optionalString cfg.unifiInsecure "-unifi.insecure" } \
            ${concatStringsSep " \\\n  " cfg.extraFlags}
        '';
      };
    };
  };
}