summary refs log tree commit diff
path: root/nixos/modules/services/monitoring/collectd.nix
blob: 8d81737a3ef0d85ac301d4c960cd3c271d0f192c (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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
{ config, pkgs, lib, ... }:

with lib;

let
  cfg = config.services.collectd;

  unvalidated_conf = pkgs.writeText "collectd-unvalidated.conf" ''
    BaseDir "${cfg.dataDir}"
    AutoLoadPlugin ${boolToString cfg.autoLoadPlugin}
    Hostname "${config.networking.hostName}"

    LoadPlugin syslog
    <Plugin "syslog">
      LogLevel "info"
      NotifyLevel "OKAY"
    </Plugin>

    ${concatStrings (mapAttrsToList (plugin: pluginConfig: ''
      LoadPlugin ${plugin}
      <Plugin "${plugin}">
      ${pluginConfig}
      </Plugin>
    '') cfg.plugins)}

    ${concatMapStrings (f: ''
      Include "${f}"
    '') cfg.include}

    ${cfg.extraConfig}
  '';

  conf = if cfg.validateConfig then
    pkgs.runCommand "collectd.conf" {} ''
      echo testing ${unvalidated_conf}
      # collectd -t fails if BaseDir does not exist.
      sed '1s/^BaseDir.*$/BaseDir "."/' ${unvalidated_conf} > collectd.conf
      ${package}/bin/collectd -t -C collectd.conf
      cp ${unvalidated_conf} $out
    '' else unvalidated_conf;

  package =
    if cfg.buildMinimalPackage
    then minimalPackage
    else cfg.package;

  minimalPackage = cfg.package.override {
    enabledPlugins = [ "syslog" ] ++ builtins.attrNames cfg.plugins;
  };

in {
  options.services.collectd = with types; {
    enable = mkEnableOption "collectd agent";

    validateConfig = mkOption {
      default = true;
      description = ''
        Validate the syntax of collectd configuration file at build time.
        Disable this if you use the Include directive on files unavailable in
        the build sandbox, or when cross-compiling.
      '';
      type = types.bool;
    };

    package = mkOption {
      default = pkgs.collectd;
      defaultText = literalExpression "pkgs.collectd";
      description = ''
        Which collectd package to use.
      '';
      type = types.package;
    };

    buildMinimalPackage = mkOption {
      default = false;
      description = ''
        Build a minimal collectd package with only the configured `services.collectd.plugins`
      '';
      type = bool;
    };

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

    autoLoadPlugin = mkOption {
      default = false;
      description = ''
        Enable plugin autoloading.
      '';
      type = bool;
    };

    include = mkOption {
      default = [];
      description = ''
        Additional paths to load config from.
      '';
      type = listOf str;
    };

    plugins = mkOption {
      default = {};
      example = { cpu = ""; memory = ""; network = "Server 192.168.1.1 25826"; };
      description = ''
        Attribute set of plugin names to plugin config segments
      '';
      type = attrsOf lines;
    };

    extraConfig = mkOption {
      default = "";
      description = ''
        Extra configuration for collectd.
      '';
      type = lines;
    };

  };

  config = mkIf cfg.enable {
    systemd.tmpfiles.rules = [
      "d '${cfg.dataDir}' - ${cfg.user} - - -"
    ];

    systemd.services.collectd = {
      description = "Collectd Monitoring Agent";
      after = [ "network.target" ];
      wantedBy = [ "multi-user.target" ];

      serviceConfig = {
        ExecStart = "${package}/sbin/collectd -C ${conf} -f";
        User = cfg.user;
        Restart = "on-failure";
        RestartSec = 3;
      };
    };

    users.users = optionalAttrs (cfg.user == "collectd") {
      collectd = {
        isSystemUser = true;
        group = "collectd";
      };
    };

    users.groups = optionalAttrs (cfg.user == "collectd") {
      collectd = {};
    };
  };
}