at 22.05-pre 3.0 kB view raw
1{ config, pkgs, lib, ... }: 2 3with lib; 4 5let 6 cfg = config.services.collectd; 7 8 conf = pkgs.writeText "collectd.conf" '' 9 BaseDir "${cfg.dataDir}" 10 AutoLoadPlugin ${boolToString cfg.autoLoadPlugin} 11 Hostname "${config.networking.hostName}" 12 13 LoadPlugin syslog 14 <Plugin "syslog"> 15 LogLevel "info" 16 NotifyLevel "OKAY" 17 </Plugin> 18 19 ${concatStrings (mapAttrsToList (plugin: pluginConfig: '' 20 LoadPlugin ${plugin} 21 <Plugin "${plugin}"> 22 ${pluginConfig} 23 </Plugin> 24 '') cfg.plugins)} 25 26 ${concatMapStrings (f: '' 27 Include "${f}" 28 '') cfg.include} 29 30 ${cfg.extraConfig} 31 ''; 32 33 package = 34 if cfg.buildMinimalPackage 35 then minimalPackage 36 else cfg.package; 37 38 minimalPackage = cfg.package.override { 39 enabledPlugins = [ "syslog" ] ++ builtins.attrNames cfg.plugins; 40 }; 41 42in { 43 options.services.collectd = with types; { 44 enable = mkEnableOption "collectd agent"; 45 46 package = mkOption { 47 default = pkgs.collectd; 48 defaultText = literalExpression "pkgs.collectd"; 49 description = '' 50 Which collectd package to use. 51 ''; 52 type = types.package; 53 }; 54 55 buildMinimalPackage = mkOption { 56 default = false; 57 description = '' 58 Build a minimal collectd package with only the configured `services.collectd.plugins` 59 ''; 60 type = bool; 61 }; 62 63 user = mkOption { 64 default = "collectd"; 65 description = '' 66 User under which to run collectd. 67 ''; 68 type = nullOr str; 69 }; 70 71 dataDir = mkOption { 72 default = "/var/lib/collectd"; 73 description = '' 74 Data directory for collectd agent. 75 ''; 76 type = path; 77 }; 78 79 autoLoadPlugin = mkOption { 80 default = false; 81 description = '' 82 Enable plugin autoloading. 83 ''; 84 type = bool; 85 }; 86 87 include = mkOption { 88 default = []; 89 description = '' 90 Additional paths to load config from. 91 ''; 92 type = listOf str; 93 }; 94 95 plugins = mkOption { 96 default = {}; 97 example = { cpu = ""; memory = ""; network = "Server 192.168.1.1 25826"; }; 98 description = '' 99 Attribute set of plugin names to plugin config segments 100 ''; 101 type = attrsOf lines; 102 }; 103 104 extraConfig = mkOption { 105 default = ""; 106 description = '' 107 Extra configuration for collectd. 108 ''; 109 type = lines; 110 }; 111 112 }; 113 114 config = mkIf cfg.enable { 115 systemd.tmpfiles.rules = [ 116 "d '${cfg.dataDir}' - ${cfg.user} - - -" 117 ]; 118 119 systemd.services.collectd = { 120 description = "Collectd Monitoring Agent"; 121 after = [ "network.target" ]; 122 wantedBy = [ "multi-user.target" ]; 123 124 serviceConfig = { 125 ExecStart = "${package}/sbin/collectd -C ${conf} -f"; 126 User = cfg.user; 127 Restart = "on-failure"; 128 RestartSec = 3; 129 }; 130 }; 131 132 users.users = optionalAttrs (cfg.user == "collectd") { 133 collectd = { 134 isSystemUser = true; 135 }; 136 }; 137 }; 138}