diff options
author | WilliButz <willibutz@posteo.de> | 2024-01-12 18:42:37 +0100 |
---|---|---|
committer | WilliButz <willibutz@posteo.de> | 2024-01-17 20:34:30 +0100 |
commit | bb9c7762bcdab1e9d697a0e521ae8078c7e10d7f (patch) | |
tree | 12fdc20f0367e79de39916bc52db836060ca0225 /nixos/modules/services/monitoring | |
parent | a8ea9fe49250d2d8e3d41a10a0f21e326c87fbad (diff) | |
download | nixlib-bb9c7762bcdab1e9d697a0e521ae8078c7e10d7f.tar nixlib-bb9c7762bcdab1e9d697a0e521ae8078c7e10d7f.tar.gz nixlib-bb9c7762bcdab1e9d697a0e521ae8078c7e10d7f.tar.bz2 nixlib-bb9c7762bcdab1e9d697a0e521ae8078c7e10d7f.tar.lz nixlib-bb9c7762bcdab1e9d697a0e521ae8078c7e10d7f.tar.xz nixlib-bb9c7762bcdab1e9d697a0e521ae8078c7e10d7f.tar.zst nixlib-bb9c7762bcdab1e9d697a0e521ae8078c7e10d7f.zip |
nixos/prometheus-snmp-exporter: add config check
This is introduced and enabled by default because the config syntax for the exporter changed with release 0.23.0. This should make the breaking config change obvious before services are deployed with an incompatible old config. The check is based on the check present in the blackbox-exporter module.
Diffstat (limited to 'nixos/modules/services/monitoring')
-rw-r--r-- | nixos/modules/services/monitoring/prometheus/exporters/snmp.nix | 39 |
1 files changed, 36 insertions, 3 deletions
diff --git a/nixos/modules/services/monitoring/prometheus/exporters/snmp.nix b/nixos/modules/services/monitoring/prometheus/exporters/snmp.nix index ad4723d28405..840ce493ee81 100644 --- a/nixos/modules/services/monitoring/prometheus/exporters/snmp.nix +++ b/nixos/modules/services/monitoring/prometheus/exporters/snmp.nix @@ -4,6 +4,25 @@ with lib; let cfg = config.services.prometheus.exporters.snmp; + + # This ensures that we can deal with string paths, path types and + # store-path strings with context. + coerceConfigFile = file: + if (builtins.isPath file) || (lib.isStorePath file) then + file + else + (lib.warn '' + ${logPrefix}: configuration file "${file}" is being copied to the nix-store. + If you would like to avoid that, please set enableConfigCheck to false. + '' /. + file); + + checkConfig = file: + pkgs.runCommandLocal "checked-snmp-exporter-config.yml" { + nativeBuildInputs = [ pkgs.buildPackages.prometheus-snmp-exporter ]; + } '' + ln -s ${coerceConfigFile file} $out + snmp_exporter --dry-run --config.file $out + ''; in { port = 9116; @@ -31,6 +50,16 @@ in }; }; + enableConfigCheck = mkOption { + type = types.bool; + default = true; + description = lib.mdDoc '' + Whether to run a correctness check for the configuration file. This depends + on the configuration file residing in the nix-store. Paths passed as string will + be copied to the store. + ''; + }; + logFormat = mkOption { type = types.enum ["logfmt" "json"]; default = "logfmt"; @@ -48,9 +77,13 @@ in }; }; serviceOpts = let - configFile = if cfg.configurationPath != null - then cfg.configurationPath - else "${pkgs.writeText "snmp-exporter-conf.yml" (builtins.toJSON cfg.configuration)}"; + uncheckedConfigFile = if cfg.configurationPath != null + then cfg.configurationPath + else "${pkgs.writeText "snmp-exporter-conf.yml" (builtins.toJSON cfg.configuration)}"; + configFile = if cfg.enableConfigCheck then + checkConfig uncheckedConfigFile + else + uncheckedConfigFile; in { serviceConfig = { ExecStart = '' |