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