1{ config, pkgs, lib, ... }:
2
3with lib;
4
5let
6 cfg = config.services.prometheus.snmpExporter;
7 mkConfigFile = pkgs.writeText "snmp.yml" (if cfg.configurationPath == null then builtins.toJSON cfg.configuration else builtins.readFile cfg.configurationPath);
8in {
9 options = {
10 services.prometheus.snmpExporter = {
11 enable = mkEnableOption "Prometheus snmp exporter";
12
13 user = mkOption {
14 type = types.str;
15 default = "nobody";
16 description = ''
17 User name under which snmp exporter shall be run.
18 '';
19 };
20
21 group = mkOption {
22 type = types.str;
23 default = "nogroup";
24 description = ''
25 Group under which snmp exporter shall be run.
26 '';
27 };
28
29 port = mkOption {
30 type = types.int;
31 default = 9116;
32 description = ''
33 Port to listen on.
34 '';
35 };
36
37 listenAddress = mkOption {
38 type = types.nullOr types.str;
39 default = null;
40 description = ''
41 Address to listen on for web interface and telemetry.
42 '';
43 };
44
45 configurationPath = mkOption {
46 type = types.nullOr types.path;
47 default = null;
48 description = ''
49 Path to a snmp exporter configuration file. Mutually exclusive with 'configuration' option.
50 '';
51 example = "./snmp.yml";
52 };
53
54 configuration = mkOption {
55 type = types.nullOr types.attrs;
56 default = {};
57 description = ''
58 Snmp exporter configuration as nix attribute set. Mutually exclusive with 'configurationPath' option.
59 '';
60 example = ''
61 {
62 "default" = {
63 "version" = 2;
64 "auth" = {
65 "community" = "public";
66 };
67 };
68 };
69 '';
70 };
71
72 logFormat = mkOption {
73 type = types.str;
74 default = "logger:stderr";
75 description = ''
76 Set the log target and format.
77 '';
78 };
79
80 logLevel = mkOption {
81 type = types.enum ["debug" "info" "warn" "error" "fatal"];
82 default = "info";
83 description = ''
84 Only log messages with the given severity or above.
85 '';
86 };
87
88 openFirewall = mkOption {
89 type = types.bool;
90 default = false;
91 description = ''
92 Open port in firewall for incoming connections.
93 '';
94 };
95 };
96 };
97
98 config = mkIf cfg.enable {
99 networking.firewall.allowedTCPPorts = optional cfg.openFirewall cfg.port;
100
101 assertions = singleton
102 {
103 assertion = (cfg.configurationPath == null) != (cfg.configuration == null);
104 message = "Please ensure you have either 'configuration' or 'configurationPath' set!";
105 };
106
107 systemd.services.prometheus-snmp-exporter = {
108 wantedBy = [ "multi-user.target" ];
109 after = [ "network.target" ];
110 script = ''
111 ${pkgs.prometheus-snmp-exporter.bin}/bin/snmp_exporter \
112 -config.file ${mkConfigFile} \
113 -log.format ${cfg.logFormat} \
114 -log.level ${cfg.logLevel} \
115 -web.listen-address ${optionalString (cfg.listenAddress != null) cfg.listenAddress}:${toString cfg.port}
116 '';
117
118 serviceConfig = {
119 User = cfg.user;
120 Group = cfg.group;
121 Restart = "always";
122 PrivateTmp = true;
123 WorkingDirectory = "/tmp";
124 };
125 };
126 };
127}