2014-09-13 01:03:15 +01:00
|
|
|
{ config, pkgs, lib, ... }:
|
|
|
|
|
|
|
|
with lib;
|
|
|
|
|
|
|
|
let
|
|
|
|
cfg = config.services.collectd;
|
|
|
|
|
|
|
|
conf = pkgs.writeText "collectd.conf" ''
|
|
|
|
BaseDir "${cfg.dataDir}"
|
2017-04-11 17:08:51 +01:00
|
|
|
AutoLoadPlugin ${boolToString cfg.autoLoadPlugin}
|
2016-11-24 20:47:17 +00:00
|
|
|
Hostname "${config.networking.hostName}"
|
2014-09-13 01:03:15 +01:00
|
|
|
|
|
|
|
LoadPlugin syslog
|
|
|
|
<Plugin "syslog">
|
|
|
|
LogLevel "info"
|
|
|
|
NotifyLevel "OKAY"
|
|
|
|
</Plugin>
|
|
|
|
|
2019-10-17 20:40:57 +01:00
|
|
|
${concatStrings (mapAttrsToList (plugin: pluginConfig: ''
|
|
|
|
LoadPlugin ${plugin}
|
|
|
|
<Plugin "${plugin}">
|
|
|
|
${pluginConfig}
|
|
|
|
</Plugin>
|
|
|
|
'') cfg.plugins)}
|
|
|
|
|
2014-09-13 01:03:15 +01:00
|
|
|
${concatMapStrings (f: ''
|
2019-10-17 20:40:57 +01:00
|
|
|
Include "${f}"
|
2014-09-13 01:03:15 +01:00
|
|
|
'') cfg.include}
|
|
|
|
|
|
|
|
${cfg.extraConfig}
|
|
|
|
'';
|
|
|
|
|
2019-10-19 23:37:15 +01:00
|
|
|
package =
|
|
|
|
if cfg.buildMinimalPackage
|
|
|
|
then minimalPackage
|
|
|
|
else cfg.package;
|
|
|
|
|
|
|
|
minimalPackage = cfg.package.override {
|
|
|
|
enabledPlugins = [ "syslog" ] ++ builtins.attrNames cfg.plugins;
|
|
|
|
};
|
|
|
|
|
2014-09-13 01:03:15 +01:00
|
|
|
in {
|
|
|
|
options.services.collectd = with types; {
|
2017-06-30 01:44:03 +01:00
|
|
|
enable = mkEnableOption "collectd agent";
|
2014-09-13 01:03:15 +01:00
|
|
|
|
2016-02-15 02:34:49 +00:00
|
|
|
package = mkOption {
|
|
|
|
default = pkgs.collectd;
|
2016-02-18 19:28:08 +00:00
|
|
|
defaultText = "pkgs.collectd";
|
2016-02-15 02:34:49 +00:00
|
|
|
description = ''
|
|
|
|
Which collectd package to use.
|
|
|
|
'';
|
2019-10-19 23:37:15 +01:00
|
|
|
type = types.package;
|
|
|
|
};
|
|
|
|
|
|
|
|
buildMinimalPackage = mkOption {
|
|
|
|
default = false;
|
|
|
|
description = ''
|
|
|
|
Build a minimal collectd package with only the configured `services.collectd.plugins`
|
|
|
|
'';
|
|
|
|
type = types.bool;
|
2016-02-15 02:34:49 +00:00
|
|
|
};
|
|
|
|
|
2014-09-13 01:03:15 +01:00
|
|
|
user = mkOption {
|
|
|
|
default = "collectd";
|
|
|
|
description = ''
|
|
|
|
User under which to run collectd.
|
|
|
|
'';
|
|
|
|
type = nullOr str;
|
|
|
|
};
|
|
|
|
|
|
|
|
dataDir = mkOption {
|
|
|
|
default = "/var/lib/collectd";
|
|
|
|
description = ''
|
|
|
|
Data directory for collectd agent.
|
|
|
|
'';
|
|
|
|
type = path;
|
|
|
|
};
|
|
|
|
|
|
|
|
autoLoadPlugin = mkOption {
|
|
|
|
default = false;
|
|
|
|
description = ''
|
|
|
|
Enable plugin autoloading.
|
|
|
|
'';
|
|
|
|
type = bool;
|
|
|
|
};
|
|
|
|
|
|
|
|
include = mkOption {
|
|
|
|
default = [];
|
|
|
|
description = ''
|
|
|
|
Additional paths to load config from.
|
|
|
|
'';
|
|
|
|
type = listOf str;
|
|
|
|
};
|
|
|
|
|
2019-10-17 20:40:57 +01:00
|
|
|
plugins = mkOption {
|
|
|
|
default = {};
|
|
|
|
example = { cpu = ""; memory = ""; network = "Server 192.168.1.1 25826"; };
|
|
|
|
description = ''
|
|
|
|
Attribute set of plugin names to plugin config segments
|
|
|
|
'';
|
|
|
|
type = types.attrsOf types.str;
|
|
|
|
};
|
|
|
|
|
2014-09-13 01:03:15 +01:00
|
|
|
extraConfig = mkOption {
|
|
|
|
default = "";
|
|
|
|
description = ''
|
|
|
|
Extra configuration for collectd.
|
|
|
|
'';
|
|
|
|
type = lines;
|
|
|
|
};
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
config = mkIf cfg.enable {
|
2019-02-23 21:17:54 +00:00
|
|
|
systemd.tmpfiles.rules = [
|
|
|
|
"d '${cfg.dataDir}' - ${cfg.user} - - -"
|
|
|
|
];
|
|
|
|
|
2014-09-13 01:03:15 +01:00
|
|
|
systemd.services.collectd = {
|
|
|
|
description = "Collectd Monitoring Agent";
|
|
|
|
after = [ "network.target" ];
|
|
|
|
wantedBy = [ "multi-user.target" ];
|
|
|
|
|
|
|
|
serviceConfig = {
|
2019-10-19 23:37:15 +01:00
|
|
|
ExecStart = "${package}/sbin/collectd -C ${conf} -f";
|
2017-06-30 01:44:03 +01:00
|
|
|
User = cfg.user;
|
2019-01-26 00:31:32 +00:00
|
|
|
Restart = "on-failure";
|
|
|
|
RestartSec = 3;
|
2014-09-13 01:03:15 +01:00
|
|
|
};
|
2017-06-30 01:44:03 +01:00
|
|
|
};
|
2014-09-13 01:03:15 +01:00
|
|
|
|
2019-09-14 18:51:29 +01:00
|
|
|
users.users = optionalAttrs (cfg.user == "collectd") {
|
|
|
|
collectd = {
|
|
|
|
isSystemUser = true;
|
|
|
|
};
|
2014-09-13 01:03:15 +01:00
|
|
|
};
|
|
|
|
};
|
|
|
|
}
|