summary refs log tree commit diff
path: root/nixos/modules/services/monitoring/netdata.nix
blob: e1fde4fc9500246cf84b6e7f52439fb6a2c9d9f2 (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
{ config, pkgs, lib, ... }:

with lib;

let
  cfg = config.services.netdata;

  configFile = pkgs.writeText "netdata.conf" cfg.configText;

  defaultUser = "netdata";

in {
  options = {
    services.netdata = {
      enable = mkOption {
        default = false;
        type = types.bool;
        description = "Whether to enable netdata monitoring.";
      };

      user = mkOption {
        type = types.str;
        default = "netdata";
        description = "User account under which netdata runs.";
      };

      group = mkOption {
        type = types.str;
        default = "netdata";
        description = "Group under which netdata runs.";
      };

      configText = mkOption {
        type = types.lines;
        default = "";
        description = "netdata.conf configuration.";
        example = ''
          [global]
          debug log = syslog
          access log = syslog
          error log = syslog
        '';
      };

    };
  };

  config = mkIf cfg.enable {
    systemd.services.netdata = {
      description = "Real time performance monitoring";
      after = [ "network.target" ];
      wantedBy = [ "multi-user.target" ];
      preStart = concatStringsSep "\n" (map (dir: ''
        mkdir -vp ${dir}
        chmod 750 ${dir}
        chown -R ${cfg.user}:${cfg.group} ${dir}
        '') [ "/var/cache/netdata"
              "/var/log/netdata"
              "/var/lib/netdata" ]);
      serviceConfig = {
        User = cfg.user;
        Group = cfg.group;
        PermissionsStartOnly = true;
        ExecStart = "${pkgs.netdata}/bin/netdata -D -c ${configFile}";
        TimeoutStopSec = 60;
      };
    };

    users.extraUsers = optional (cfg.user == defaultUser) {
      name = defaultUser;
    };

    users.extraGroups = optional (cfg.group == defaultUser) {
      name = defaultUser;
    };

  };
}