nixpkgs/nixos/modules/services/monitoring/collectd.nix
volth b3c5e9ac1e
nixos/collectd: restart on failure
`collectd' might fail because of a failure in any of numerous plugins.
For example `virt' plugin sometimes fails if `collectd' is started before `libvirtd'
2019-01-26 00:31:32 +00:00

107 lines
2.1 KiB
Nix

{ config, pkgs, lib, ... }:
with lib;
let
cfg = config.services.collectd;
conf = pkgs.writeText "collectd.conf" ''
BaseDir "${cfg.dataDir}"
AutoLoadPlugin ${boolToString cfg.autoLoadPlugin}
Hostname "${config.networking.hostName}"
LoadPlugin syslog
<Plugin "syslog">
LogLevel "info"
NotifyLevel "OKAY"
</Plugin>
${concatMapStrings (f: ''
Include "${f}"
'') cfg.include}
${cfg.extraConfig}
'';
in {
options.services.collectd = with types; {
enable = mkEnableOption "collectd agent";
package = mkOption {
default = pkgs.collectd;
defaultText = "pkgs.collectd";
description = ''
Which collectd package to use.
'';
type = package;
};
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;
};
extraConfig = mkOption {
default = "";
description = ''
Extra configuration for collectd.
'';
type = lines;
};
};
config = mkIf cfg.enable {
systemd.services.collectd = {
description = "Collectd Monitoring Agent";
after = [ "network.target" ];
wantedBy = [ "multi-user.target" ];
serviceConfig = {
ExecStart = "${cfg.package}/sbin/collectd -C ${conf} -f";
User = cfg.user;
PermissionsStartOnly = true;
Restart = "on-failure";
RestartSec = 3;
};
preStart = ''
mkdir -p "${cfg.dataDir}"
chmod 755 "${cfg.dataDir}"
chown -R ${cfg.user} "${cfg.dataDir}"
'';
};
users.users = optional (cfg.user == "collectd") {
name = "collectd";
};
};
}