1{ config, lib, pkgs, options }:
2
3with lib;
4
5let
6 cfg = config.services.prometheus.exporters.snmp;
7in
8{
9 port = 9116;
10 extraOpts = {
11 configurationPath = mkOption {
12 type = types.nullOr types.path;
13 default = null;
14 description = lib.mdDoc ''
15 Path to a snmp exporter configuration file. Mutually exclusive with 'configuration' option.
16 '';
17 example = literalExpression "./snmp.yml";
18 };
19
20 configuration = mkOption {
21 type = types.nullOr types.attrs;
22 default = null;
23 description = lib.mdDoc ''
24 Snmp exporter configuration as nix attribute set. Mutually exclusive with 'configurationPath' option.
25 '';
26 example = {
27 "default" = {
28 "version" = 2;
29 "auth" = {
30 "community" = "public";
31 };
32 };
33 };
34 };
35
36 logFormat = mkOption {
37 type = types.enum ["logfmt" "json"];
38 default = "logfmt";
39 description = lib.mdDoc ''
40 Output format of log messages.
41 '';
42 };
43
44 logLevel = mkOption {
45 type = types.enum ["debug" "info" "warn" "error"];
46 default = "info";
47 description = lib.mdDoc ''
48 Only log messages with the given severity or above.
49 '';
50 };
51 };
52 serviceOpts = let
53 configFile = if cfg.configurationPath != null
54 then cfg.configurationPath
55 else "${pkgs.writeText "snmp-exporter-conf.yml" (builtins.toJSON cfg.configuration)}";
56 in {
57 serviceConfig = {
58 ExecStart = ''
59 ${pkgs.prometheus-snmp-exporter}/bin/snmp_exporter \
60 --config.file=${escapeShellArg configFile} \
61 --log.format=${escapeShellArg cfg.logFormat} \
62 --log.level=${cfg.logLevel} \
63 --web.listen-address=${cfg.listenAddress}:${toString cfg.port} \
64 ${concatStringsSep " \\\n " cfg.extraFlags}
65 '';
66 };
67 };
68}