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 package = mkOption {
38 default = pkgs.collectd;
39 defaultText = "pkgs.collectd";
40 description = ''
41 Which collectd package to use.
42 '';
43 type = package;
44 };
45
46 user = mkOption {
47 default = "collectd";
48 description = ''
49 User under which to run collectd.
50 '';
51 type = nullOr str;
52 };
53
54 dataDir = mkOption {
55 default = "/var/lib/collectd";
56 description = ''
57 Data directory for collectd agent.
58 '';
59 type = path;
60 };
61
62 pidFile = mkOption {
63 default = "/var/run/collectd.pid";
64 description = ''
65 Location of collectd pid file.
66 '';
67 type = path;
68 };
69
70 autoLoadPlugin = mkOption {
71 default = false;
72 description = ''
73 Enable plugin autoloading.
74 '';
75 type = bool;
76 };
77
78 include = mkOption {
79 default = [];
80 description = ''
81 Additional paths to load config from.
82 '';
83 type = listOf str;
84 };
85
86 extraConfig = mkOption {
87 default = "";
88 description = ''
89 Extra configuration for collectd.
90 '';
91 type = lines;
92 };
93
94 };
95
96 config = mkIf cfg.enable {
97 systemd.services.collectd = {
98 description = "Collectd Monitoring Agent";
99 after = [ "network.target" ];
100 wantedBy = [ "multi-user.target" ];
101
102 serviceConfig = {
103 ExecStart = "${pkgs.collectd}/sbin/collectd -C ${conf} -P ${cfg.pidFile}";
104 Type = "forking";
105 PIDFile = cfg.pidFile;
106 User = optional (cfg.user!="root") cfg.user;
107 PermissionsStartOnly = true;
108 };
109
110 preStart = ''
111 mkdir -m 0700 -p ${cfg.dataDir}
112 install -D /dev/null ${cfg.pidFile}
113 if [ "$(id -u)" = 0 ]; then
114 chown -R ${cfg.user} ${cfg.dataDir};
115 chown ${cfg.user} ${cfg.pidFile}
116 fi
117 '';
118 };
119
120 users.extraUsers = optional (cfg.user == "collectd") {
121 name = "collectd";
122 uid = config.ids.uids.collectd;
123 };
124 };
125}