From 0112e59b1f40097fb1fe4dcc239ed082cc1ac0e7 Mon Sep 17 00:00:00 2001
From: lassulus <lass@lassul.us>
Date: Sat, 28 Jan 2017 18:36:53 +0100
Subject: [PATCH] l 3 kapacitor: add alert definition

---
 lass/3modules/kapacitor.nix | 31 +++++++++++++++++++++++++++++++
 1 file changed, 31 insertions(+)

diff --git a/lass/3modules/kapacitor.nix b/lass/3modules/kapacitor.nix
index 8524c8198..a2ee71732 100644
--- a/lass/3modules/kapacitor.nix
+++ b/lass/3modules/kapacitor.nix
@@ -21,6 +21,14 @@ let
       type = types.str;
       default = "kapacitor";
     };
+    alarms = mkOption {
+      type = with types; attrsOf str;
+      default = {};
+    };
+    check_db = mkOption {
+      type = types.str;
+      default = "kapacitor_example";
+    };
     config = mkOption {
       type = types.str;
       #TODO: find a good default
@@ -216,6 +224,29 @@ let
         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