nixos/collectd: put extraconfig before plugins

this is necessary to override the global option Interval.
If set after the plugins, it has no effect.

Changed files
+36 -27
nixos
modules
services
monitoring
tests
+30 -26
nixos/modules/services/monitoring/collectd.nix
···
let
cfg = config.services.collectd;
-
unvalidated_conf = pkgs.writeText "collectd-unvalidated.conf" ''
-
BaseDir "${cfg.dataDir}"
-
AutoLoadPlugin ${boolToString cfg.autoLoadPlugin}
-
Hostname "${config.networking.hostName}"
-
-
LoadPlugin syslog
-
<Plugin "syslog">
-
LogLevel "info"
-
NotifyLevel "OKAY"
-
</Plugin>
-
-
${concatStrings (mapAttrsToList (plugin: pluginConfig: ''
-
LoadPlugin ${plugin}
-
<Plugin "${plugin}">
-
${pluginConfig}
-
</Plugin>
-
'') cfg.plugins)}
-
-
${concatMapStrings (f: ''
-
Include "${f}"
-
'') cfg.include}
-
-
${cfg.extraConfig}
-
'';
+
baseDirLine = ''BaseDir "${cfg.dataDir}"'';
+
unvalidated_conf = pkgs.writeText "collectd-unvalidated.conf" cfg.extraConfig;
conf = if cfg.validateConfig then
pkgs.runCommand "collectd.conf" {} ''
echo testing ${unvalidated_conf}
+
cp ${unvalidated_conf} collectd.conf
# collectd -t fails if BaseDir does not exist.
-
sed '1s/^BaseDir.*$/BaseDir "."/' ${unvalidated_conf} > collectd.conf
+
substituteInPlace collectd.conf --replace ${lib.escapeShellArgs [ baseDirLine ]} 'BaseDir "."'
${package}/bin/collectd -t -C collectd.conf
cp ${unvalidated_conf} $out
'' else unvalidated_conf;
···
extraConfig = mkOption {
default = "";
description = ''
-
Extra configuration for collectd.
+
Extra configuration for collectd. Use mkBefore to add lines before the
+
default config, and mkAfter to add them below.
'';
type = lines;
};
···
};
config = mkIf cfg.enable {
+
# 1200 is after the default (1000) but before mkAfter (1500).
+
services.collectd.extraConfig = lib.mkOrder 1200 ''
+
${baseDirLine}
+
AutoLoadPlugin ${boolToString cfg.autoLoadPlugin}
+
Hostname "${config.networking.hostName}"
+
+
LoadPlugin syslog
+
<Plugin "syslog">
+
LogLevel "info"
+
NotifyLevel "OKAY"
+
</Plugin>
+
+
${concatStrings (mapAttrsToList (plugin: pluginConfig: ''
+
LoadPlugin ${plugin}
+
<Plugin "${plugin}">
+
${pluginConfig}
+
</Plugin>
+
'') cfg.plugins)}
+
+
${concatMapStrings (f: ''
+
Include "${f}"
+
'') cfg.include}
+
'';
+
systemd.tmpfiles.rules = [
"d '${cfg.dataDir}' - ${cfg.user} - - -"
];
+6 -1
nixos/tests/collectd.nix
···
meta = { };
nodes.machine =
-
{ pkgs, ... }:
+
{ pkgs, lib, ... }:
{
services.collectd = {
enable = true;
+
extraConfig = lib.mkBefore ''
+
Interval 30
+
'';
plugins = {
rrdtool = ''
DataDir "/var/lib/collectd/rrd"
···
machine.succeed(f"rrdinfo {file} | logger")
# check that this file contains a shortterm metric
machine.succeed(f"rrdinfo {file} | grep -F 'ds[shortterm].min = '")
+
# check that interval was set before the plugins
+
machine.succeed(f"rrdinfo {file} | grep -F 'step = 30'")
# check that there are frequent updates
machine.succeed(f"cp {file} before")
machine.wait_until_fails(f"cmp before {file}")