summary refs log tree commit diff
path: root/nixos/modules/services/monitoring/collectd.nix
blob: 641da60e9adcd1add047df9fddec718fa31471fd (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
{ config, pkgs, lib, ... }:

with lib;

let
  cfg = config.services.collectd;

  conf = pkgs.writeText "collectd.conf" ''
    BaseDir "${cfg.dataDir}"
    PIDFile "${cfg.pidFile}"
    AutoLoadPlugin ${if cfg.autoLoadPlugin then "true" else "false"}
    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 = mkOption {
      default = false;
      description = ''
        Whether to enable collectd agent.
      '';
      type = bool;
    };

    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;
    };

    pidFile = mkOption {
      default = "/var/run/collectd.pid";
      description = ''
        Location of collectd pid file.
      '';
      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} -P ${cfg.pidFile}";
        Type = "forking";
        PIDFile = cfg.pidFile;
        User = optional (cfg.user!="root") cfg.user;
        PermissionsStartOnly = true;
      };

      preStart = ''
        mkdir -p ${cfg.dataDir}
        chmod 755 ${cfg.dataDir}
        install -D /dev/null ${cfg.pidFile}
        if [ "$(id -u)" = 0 ]; then
          chown -R ${cfg.user} ${cfg.dataDir};
          chown ${cfg.user} ${cfg.pidFile}
        fi
      '';
    }; 

    users.extraUsers = optional (cfg.user == "collectd") {
      name = "collectd";
      uid = config.ids.uids.collectd;
    };
  };
}