1{
2 config,
3 lib,
4 pkgs,
5 options,
6 ...
7}:
8
9let
10 cfg = config.services.prometheus.exporters.mikrotik;
11 inherit (lib)
12 mkOption
13 types
14 literalExpression
15 concatStringsSep
16 escapeShellArg
17 ;
18in
19{
20 port = 9436;
21 extraOpts = {
22 configFile = mkOption {
23 type = types.nullOr types.path;
24 default = null;
25 description = ''
26 Path to a mikrotik exporter configuration file. Mutually exclusive with
27 {option}`configuration` option.
28 '';
29 example = literalExpression "./mikrotik.yml";
30 };
31
32 configuration = mkOption {
33 type = types.nullOr types.attrs;
34 default = null;
35 description = ''
36 Mikrotik exporter configuration as nix attribute set. Mutually exclusive with
37 {option}`configFile` option.
38
39 See <https://github.com/nshttpd/mikrotik-exporter/blob/master/README.md>
40 for the description of the configuration file format.
41 '';
42 example = literalExpression ''
43 {
44 devices = [
45 {
46 name = "my_router";
47 address = "10.10.0.1";
48 user = "prometheus";
49 password = "changeme";
50 }
51 ];
52 features = {
53 bgp = true;
54 dhcp = true;
55 routes = true;
56 optics = true;
57 };
58 }
59 '';
60 };
61 };
62 serviceOpts =
63 let
64 configFile =
65 if cfg.configFile != null then
66 cfg.configFile
67 else
68 "${pkgs.writeText "mikrotik-exporter.yml" (builtins.toJSON cfg.configuration)}";
69 in
70 {
71 serviceConfig = {
72 # -port is misleading name, it actually accepts address too
73 ExecStart = ''
74 ${pkgs.prometheus-mikrotik-exporter}/bin/mikrotik-exporter \
75 -config-file=${escapeShellArg configFile} \
76 -port=${cfg.listenAddress}:${toString cfg.port} \
77 ${concatStringsSep " \\\n " cfg.extraFlags}
78 '';
79 };
80 };
81}