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 PIDFile "${cfg.pidFile}"
11 AutoLoadPlugin ${if cfg.autoLoadPlugin then "true" else "false"}
12 Hostname ${config.networking.hostName}
13
14 LoadPlugin syslog
15 <Plugin "syslog">
16 LogLevel "info"
17 NotifyLevel "OKAY"
18 </Plugin>
19
20 ${concatMapStrings (f: ''
21 Include "${f}"
22 '') cfg.include}
23
24 ${cfg.extraConfig}
25 '';
26
27in {
28 options.services.collectd = with types; {
29 enable = mkOption {
30 default = false;
31 description = ''
32 Whether to enable collectd agent.
33 '';
34 type = bool;
35 };
36
37 user = mkOption {
38 default = "collectd";
39 description = ''
40 User under which to run collectd.
41 '';
42 type = nullOr str;
43 };
44
45 dataDir = mkOption {
46 default = "/var/lib/collectd";
47 description = ''
48 Data directory for collectd agent.
49 '';
50 type = path;
51 };
52
53 pidFile = mkOption {
54 default = "/var/run/collectd.pid";
55 description = ''
56 Location of collectd pid file.
57 '';
58 type = path;
59 };
60
61 autoLoadPlugin = mkOption {
62 default = false;
63 description = ''
64 Enable plugin autoloading.
65 '';
66 type = bool;
67 };
68
69 include = mkOption {
70 default = [];
71 description = ''
72 Additional paths to load config from.
73 '';
74 type = listOf str;
75 };
76
77 extraConfig = mkOption {
78 default = "";
79 description = ''
80 Extra configuration for collectd.
81 '';
82 type = lines;
83 };
84
85 };
86
87 config = mkIf cfg.enable {
88 systemd.services.collectd = {
89 description = "Collectd Monitoring Agent";
90 after = [ "network.target" ];
91 wantedBy = [ "multi-user.target" ];
92
93 serviceConfig = {
94 ExecStart = "${pkgs.collectd}/sbin/collectd -C ${conf} -P ${cfg.pidFile}";
95 Type = "forking";
96 PIDFile = cfg.pidFile;
97 User = optional (cfg.user!="root") cfg.user;
98 PermissionsStartOnly = true;
99 };
100
101 preStart = ''
102 mkdir -m 0700 -p ${cfg.dataDir}
103 install -D /dev/null ${cfg.pidFile}
104 if [ "$(id -u)" = 0 ]; then
105 chown -R ${cfg.user} ${cfg.dataDir};
106 chown ${cfg.user} ${cfg.pidFile}
107 fi
108 '';
109 };
110
111 users.extraUsers = optional (cfg.user == "collectd") {
112 name = "collectd";
113 uid = config.ids.uids.collectd;
114 };
115 };
116}