summaryrefslogtreecommitdiffstats
path: root/lass/3modules/kapacitor.nix
blob: 299a56d921e606eac9d0757246bf056e2237a791 (plain)
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
{ config, lib, pkgs, ... }:

with builtins;
with lib;

let
  cfg = config.lass.kapacitor;

  out = {
    options.lass.kapacitor = api;
    config = mkIf cfg.enable imp;
  };

  api = {
    enable = mkEnableOption "kapacitor";
    dataDir = mkOption {
      type = types.str;
      default = "/var/lib/kapacitor";
    };
    user = mkOption {
      type = types.str;
      default = "kapacitor";
    };
    logLevel = mkOption {
      type = types.enum ["DEBUG" "INFO" "WARN" "ERROR" "OFF"];
      default = "INFO";
    };
    alarms = mkOption {
      type = with types; attrsOf str;
      default = {};
    };
    check_db = mkOption {
      type = types.str;
      default = "all_data";
    };
    config = mkOption {
      type = types.str;
      #TODO: find a good default
      default = ''
        hostname = "localhost"
        data_dir = "${cfg.dataDir}"

        [http]
          bind-address = ":9092"
          auth-enabled = false
          log-enabled = true
          write-tracing = false
          pprof-enabled = false
          https-enabled = false
          https-certificate = "/etc/ssl/kapacitor.pem"
          shutdown-timeout = "10s"
          shared-secret = ""

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

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

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

        [[influxdb]]
          enabled = true
          name = "default"
          default = false
          urls = ["http://localhost:8086"]
          username = ""
          password = ""
          ssl-ca = ""
          ssl-cert = ""
          ssl-key = ""
          insecure-skip-verify = false
          timeout = "0s"
          disable-subscriptions = false
          subscription-protocol = "http"
          udp-bind = ""
          udp-buffer = 1000
          udp-read-buffer = 0
          startup-timeout = "5m0s"
          subscriptions-sync-interval = "1m0s"
          [influxdb.subscriptions]
          [influxdb.excluded-subscriptions]
            _kapacitor = ["autogen"]

        [logging]
          file = "STDERR"
          level = "${cfg.logLevel}"

        [deadman]
          interval = "10s"
          threshold = 0.0
          id = "{{ .Group }}:NODE_NAME for task '{{ .TaskName }}'"
          message = "{{ .ID }} is {{ if eq .Level \"OK\" }}alive{{ else }}dead{{ end }}: {{ index .Fields \"emitted\" | printf \"%0.3f\" }} points/INTERVAL."
          global = false
      '';
      description = "configuration kapacitor is started with";
    };
  };

  configFile = pkgs.writeText "kapacitor.conf" cfg.config;

  imp = {

    systemd.services.kapacitor = {
      description = "kapacitor";
      after = [ "network.target" ];
      wantedBy = [ "multi-user.target" ];

      restartIfChanged = true;

      serviceConfig = {
        Restart = "always";
        ExecStart = "${pkgs.kapacitor}/bin/kapacitord -config ${configFile}";
      };
    };

    systemd.services.kapacitor-alarms = {
      description = "kapacitor-alarms";
      after = [ "kapacitor.service" ];
      wantedBy = [ "multi-user.target" ];

      restartIfChanged = true;

      serviceConfig = {
        Type = "oneshot";
        ExecStart = pkgs.writeDash "add_alarms" ''
          ${pkgs.kapacitor}/bin/kapacitor delete tasks \*
          ${concatStrings (mapAttrsToList (name: alarm: ''
            ${pkgs.kapacitor}/bin/kapacitor define ${name} \
              -type batch \
              -tick ${pkgs.writeText "${name}.tick" alarm} \
              -dbrp ${cfg.check_db}.default
            ${pkgs.kapacitor}/bin/kapacitor enable ${name}
          '') cfg.alarms)}
        '';
      };
    };

  };

in out