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

with lib;

let
  cfg = config.services.kapacitor;

  kapacitorConf = pkgs.writeTextFile {
    name = "kapacitord.conf";
    text = ''
      hostname="${config.networking.hostName}"
      data_dir="${cfg.dataDir}"

      [http]
        bind-address = "${cfg.bind}:${toString cfg.port}"
        log-enabled = false
        auth-enabled = false

      [task]
        dir = "${cfg.dataDir}/tasks"
        snapshot-interval = "${cfg.taskSnapshotInterval}"

      [replay]
        dir = "${cfg.dataDir}/replay"

      [storage]
        boltdb = "${cfg.dataDir}/kapacitor.db"

      ${optionalString (cfg.loadDirectory != null) ''
        [load]
          enabled = true
          dir = "${cfg.loadDirectory}"
      ''}

      ${optionalString (cfg.defaultDatabase.enable) ''
        [[influxdb]]
          name = "default"
          enabled = true
          default = true
          urls = [ "${cfg.defaultDatabase.url}" ]
          username = "${cfg.defaultDatabase.username}"
          password = "${cfg.defaultDatabase.password}"
      ''}

      ${cfg.extraConfig}
    '';
  };
in
{
  options.services.kapacitor = {
    enable = mkEnableOption "kapacitor";

    dataDir = mkOption {
      type = types.path;
      example = "/var/lib/kapacitor";
      default = "/var/lib/kapacitor";
      description = "Location where Kapacitor stores its state";
    };

    port = mkOption {
      type = types.int;
      default = 9092;
      description = "Port of Kapacitor";
    };

    bind = mkOption {
      type = types.str;
      default = "";
      example = literalExample "0.0.0.0";
      description = "Address to bind to. The default is to bind to all addresses";
    };

    extraConfig = mkOption {
      description = "These lines go into kapacitord.conf verbatim.";
      default = "";
      type = types.lines;
    };

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

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

    taskSnapshotInterval = mkOption {
      type = types.str;
      description = "Specifies how often to snapshot the task state  (in InfluxDB time units)";
      default = "1m0s";
      example = "1m0s";
    };

    loadDirectory = mkOption {
      type = types.nullOr types.path;
      description = "Directory where to load services from, such as tasks, templates and handlers (or null to disable service loading on startup)";
      default = null;
    };

    defaultDatabase = {
      enable = mkEnableOption "kapacitor.defaultDatabase";

      url = mkOption {
        description = "The URL to an InfluxDB server that serves as the default database";
        example = "http://localhost:8086";
        type = types.string;
      };

      username = mkOption {
        description = "The username to connect to the remote InfluxDB server";
        type = types.string;
      };

      password = mkOption {
        description = "The password to connect to the remote InfluxDB server";
        type = types.string;
      };
    };
  };

  config = mkIf cfg.enable {
    environment.systemPackages = [ pkgs.kapacitor ];

    systemd.services.kapacitor = {
      description = "Kapacitor Real-Time Stream Processing Engine";
      wantedBy = [ "multi-user.target" ];
      after = [ "networking.target" ];
      serviceConfig = {
        ExecStart = "${pkgs.kapacitor}/bin/kapacitord -config ${kapacitorConf}";
        User = "kapacitor";
        Group = "kapacitor";
        PermissionsStartOnly = true;
      };
      preStart = ''
        mkdir -p ${cfg.dataDir}
        chown ${cfg.user}:${cfg.group} ${cfg.dataDir}
      '';
    };

    users.users.kapacitor = {
      uid = config.ids.uids.kapacitor;
      description = "Kapacitor user";
      home = cfg.dataDir;
    };

    users.groups.kapacitor = {
      gid = config.ids.gids.kapacitor;
    };
  };
}