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 ${concatMapStrings (f: ''
20 Include "${f}"
21 '') cfg.include}
22
23 ${cfg.extraConfig}
24 '';
25
26in {
27 options.services.collectd = with types; {
28 enable = mkEnableOption "collectd agent";
29
30 package = mkOption {
31 default = pkgs.collectd;
32 defaultText = "pkgs.collectd";
33 description = ''
34 Which collectd package to use.
35 '';
36 type = package;
37 };
38
39 user = mkOption {
40 default = "collectd";
41 description = ''
42 User under which to run collectd.
43 '';
44 type = nullOr str;
45 };
46
47 dataDir = mkOption {
48 default = "/var/lib/collectd";
49 description = ''
50 Data directory for collectd agent.
51 '';
52 type = path;
53 };
54
55 autoLoadPlugin = mkOption {
56 default = false;
57 description = ''
58 Enable plugin autoloading.
59 '';
60 type = bool;
61 };
62
63 include = mkOption {
64 default = [];
65 description = ''
66 Additional paths to load config from.
67 '';
68 type = listOf str;
69 };
70
71 extraConfig = mkOption {
72 default = "";
73 description = ''
74 Extra configuration for collectd.
75 '';
76 type = lines;
77 };
78
79 };
80
81 config = mkIf cfg.enable {
82 systemd.services.collectd = {
83 description = "Collectd Monitoring Agent";
84 after = [ "network.target" ];
85 wantedBy = [ "multi-user.target" ];
86
87 serviceConfig = {
88 ExecStart = "${cfg.package}/sbin/collectd -C ${conf} -f";
89 User = cfg.user;
90 PermissionsStartOnly = true;
91 };
92
93 preStart = ''
94 mkdir -p "${cfg.dataDir}"
95 chmod 755 "${cfg.dataDir}"
96 chown -R ${cfg.user} "${cfg.dataDir}"
97 '';
98 };
99
100 users.extraUsers = optional (cfg.user == "collectd") {
101 name = "collectd";
102 };
103 };
104}