about summary refs log tree commit diff
path: root/nixos
diff options
context:
space:
mode:
authorDomen Kožar <domen@dev.si>2013-11-08 11:44:17 -0800
committerDomen Kožar <domen@dev.si>2013-11-08 11:44:17 -0800
commita623cc96e3a3045b21300583c1c8808c4cff38bc (patch)
treeef2d85d89b474ae734788509eccc06d965d27ffe /nixos
parent3e8e635be7d3a1f93812b6692169bf9ca115be58 (diff)
parent8b1ab6d912b690329cff8a6ef0da2559488aeede (diff)
downloadnixlib-a623cc96e3a3045b21300583c1c8808c4cff38bc.tar
nixlib-a623cc96e3a3045b21300583c1c8808c4cff38bc.tar.gz
nixlib-a623cc96e3a3045b21300583c1c8808c4cff38bc.tar.bz2
nixlib-a623cc96e3a3045b21300583c1c8808c4cff38bc.tar.lz
nixlib-a623cc96e3a3045b21300583c1c8808c4cff38bc.tar.xz
nixlib-a623cc96e3a3045b21300583c1c8808c4cff38bc.tar.zst
nixlib-a623cc96e3a3045b21300583c1c8808c4cff38bc.zip
Merge pull request #1066 from offlinehacker/nixos/logstash/update
nixos/logstash: update and simplify to be fully compatible with new version
Diffstat (limited to 'nixos')
-rw-r--r--nixos/modules/services/logging/logstash.nix160
-rw-r--r--nixos/tests/default.nix1
-rw-r--r--nixos/tests/logstash.nix40
3 files changed, 83 insertions, 118 deletions
diff --git a/nixos/modules/services/logging/logstash.nix b/nixos/modules/services/logging/logstash.nix
index 79bdf4f7bbca..21128779e8f6 100644
--- a/nixos/modules/services/logging/logstash.nix
+++ b/nixos/modules/services/logging/logstash.nix
@@ -3,72 +3,8 @@
 with pkgs.lib;
 
 let
-
   cfg = config.services.logstash;
 
-  listToConfig = list: "[ " + (concatStringsSep ", " (map exprToConfig list)) + " ]";
-
-  hashToConfig = attrs:
-    let
-      attrNameToConfigList = name:
-        [ (exprToConfig name)  (exprToConfig (getAttr name attrs)) ];
-    in
-      "[ " +
-      (concatStringsSep ", " (map attrNameToConfigList (attrNames attrs))) +
-      " ]";
-
-  valueToConfig = nvpair: let name = nvpair.name; value = nvpair.value; in
-    if (isAttrs value) && ((!(value ? __type)) || value.__type == "repeated")
-      then ''
-        ${name} {
-          ${exprToConfig value}
-        }
-      ''
-      else "${name} => ${exprToConfig value}";
-
-  repeatedAttrsToConfig = values:
-      concatStringsSep "\n" (map valueToConfig values);
-
-  attrsToConfig = attrs:
-    let
-      attrToConfig = name: valueToConfig {
-        inherit name;
-        value = (getAttr name attrs);
-      };
-    in
-      concatStringsSep "\n" (map attrToConfig (attrNames attrs));
-
-  exprToConfig = expr:
-    let
-      isCustomType = expr: (isAttrs expr) && (expr ? __type);
-
-      isFloat = expr: (isCustomType expr) && (expr.__type == "float");
-
-      isHash = expr: (isCustomType expr) && (expr.__type == "hash");
-
-      isRepeatedAttrs = expr: (isCustomType expr) && (expr.__type == "repeated");
-    in
-      if builtins.isBool expr then (if expr then "true" else "false") else
-      if builtins.isString expr then ''"${expr}"'' else
-      if builtins.isInt expr then toString expr else
-      if isFloat expr then expr.value else
-      if isList expr then listToConfig expr else
-      if isHash expr then hashToConfig expr.value else
-      if isRepeatedAttrs expr then repeatedAttrsToConfig expr.values
-      else attrsToConfig expr;
-
-  mergeConfigs = configs:
-    let
-      op = attrs: newAttrs:
-        let
-          isRepeated = newAttrs ? __type && newAttrs.__type == "repeated";
-        in {
-            values = attrs.values ++ (if isRepeated then newAttrs.values else
-              map (name: { inherit name; value = getAttr name newAttrs; })
-              (attrNames newAttrs));
-          };
-    in (foldl op { values = []; } configs) // { __type = "repeated"; };
-
 in
 
 {
@@ -78,48 +14,45 @@ in
     services.logstash = {
       enable = mkOption {
         default = false;
-        description = ''
-          Enable logstash.
-        '';
+        description = "Enable logstash";
       };
 
       inputConfig = mkOption {
-        default = {};
-        description = ''
-          An attribute set (or an expression generated by mkNameValuePairs)
-          representing a logstash configuration's input section.
-          Logstash configs are name-value pairs, where values can be bools,
-          strings, numbers, arrays, hashes, or other name-value pairs,
-          and names are strings that can be repeated. Name-value pairs with no
-          repeats are represented by attr sets. Bools, strings, ints, and
-          arrays are mapped directly. Name-value pairs with repeats can be
-          generated by the config.lib.logstash.mkNameValuePairs function, which
-          takes a list of attrsets and combines them while preserving attribute
-          name duplicates if they occur. Similarly, there are the mkFloat and
-          mkHash functions, which take a string representation of a float and an
-          attrset, respectively.
+        default = ''stdin { type => "example" }'';
+        description = "Logstash input configuration";
+        example = ''
+          # Read from journal
+          pipe {
+            command => "${pkgs.systemd}/bin/journalctl -f -o json"
+            type => "syslog" codec => json {}
+          }
         '';
-        apply = mergeConfigs;
       };
 
       filterConfig = mkOption {
-        default = {};
-        description = ''
-          An attribute set (or an expression generated by mkNameValuePairs)
-          representing a logstash configuration's filter section.
-          See inputConfig description for details.
+        default = ''noop {}'';
+        description = "logstash filter configuration";
+        example = ''
+          if [type] == "syslog" {
+            # Keep only relevant systemd fields
+            # http://www.freedesktop.org/software/systemd/man/systemd.journal-fields.html
+            prune {
+              whitelist_names => [
+                "type", "@timestamp", "@version",
+                "MESSAGE", "PRIORITY", "SYSLOG_FACILITY",
+              ]
+            }
+          }
         '';
-        apply = mergeConfigs;
       };
 
       outputConfig = mkOption {
-        default = {};
-        description = ''
-          An attribute set (or an expression generated by mkNameValuePairs)
-          representing a logstash configuration's output section.
-          See inputConfig description for details.
+        default = ''stdout { debug => true debug_format => "json"}'';
+        description = "Logstash output configuration";
+        example = ''
+          redis { host => "localhost" data_type => "list" key => "logstash" codec => json }
+          elasticsearch { embedded => true }
         '';
-        apply = mergeConfigs;
       };
     };
   };
@@ -127,35 +60,26 @@ in
 
   ###### implementation
 
-  config = mkMerge [ {
-    lib.logstash = {
-      mkFloat = stringRep: { __type = "float"; value = stringRep; };
-
-      mkHash = attrs: { __type = "hash"; value = attrs; };
-
-      mkNameValuePairs = mergeConfigs;
-    };
-  } ( mkIf cfg.enable {
+  config = mkIf cfg.enable {
     systemd.services.logstash = with pkgs; {
       description = "Logstash daemon";
-
       wantedBy = [ "multi-user.target" ];
 
-      path = [ jre ];
-
-      script = "cd /tmp && exec java -jar ${logstash} agent -f ${writeText "logstash.conf" ''
-        input {
-          ${exprToConfig cfg.inputConfig}
-        }
+      serviceConfig = {
+        ExecStart = "${jre}/bin/java -jar ${logstash} agent -f ${writeText "logstash.conf" ''
+          input {
+            ${cfg.inputConfig}
+          }
 
-        filter {
-          ${exprToConfig cfg.filterConfig}
-        }
+          filter {
+            ${cfg.filterConfig}
+          }
 
-        output {
-          ${exprToConfig cfg.outputConfig}
-        }
-      ''} &> /var/log/logstash.log";
+          output {
+            ${cfg.outputConfig}
+          }
+        ''}";
+      };
     };
-  })];
+  };
 }
diff --git a/nixos/tests/default.nix b/nixos/tests/default.nix
index ce5776c8e465..574e1dd2f8b8 100644
--- a/nixos/tests/default.nix
+++ b/nixos/tests/default.nix
@@ -16,6 +16,7 @@ with import ../lib/testing.nix { inherit system minimal; };
   kde4 = makeTest (import ./kde4.nix);
   #kexec = makeTest (import ./kexec.nix);
   login = makeTest (import ./login.nix {});
+  logstash = makeTest (import ./logstash.nix);
   latestKernel.login = makeTest (import ./login.nix ({ config, pkgs, ... }: { boot.kernelPackages = pkgs.linuxPackages_latest; }));
   misc = makeTest (import ./misc.nix);
   #mpich = makeTest (import ./mpich.nix);
diff --git a/nixos/tests/logstash.nix b/nixos/tests/logstash.nix
new file mode 100644
index 000000000000..ee309d39f872
--- /dev/null
+++ b/nixos/tests/logstash.nix
@@ -0,0 +1,40 @@
+{ pkgs, ... }:
+
+# This test runs logstash and checks if messages flows and elasticsearch is
+# started
+
+{
+  nodes = {
+    one =
+      { config, pkgs, ... }:
+        {
+          services = {
+            logstash = {
+              enable = true;
+              inputConfig = ''
+                exec { command => "echo flowers" interval => 1 type => "test" }
+                exec { command => "echo dragons" interval => 1 type => "test" }
+              '';
+              filterConfig = ''
+                if [type] == "test" {
+                  grep { match => ["message", "flowers"] drop => true }
+                }
+              '';
+              outputConfig = ''
+                stdout { codec => rubydebug }
+                elasticsearch { embedded => true }
+              '';
+            };
+          };
+        };
+    };
+  
+  testScript = ''
+    startAll;
+  
+    $one->waitForUnit("logstash.service");
+    $one->waitUntilSucceeds("journalctl -n 20 _SYSTEMD_UNIT=logstash.service | grep flowers");
+    $one->fail("journalctl -n 20 _SYSTEMD_UNIT=logstash.service | grep dragons");
+    $one->waitUntilSucceeds("curl -s http://127.0.0.1:9200/_status?pretty=true | grep logstash");
+  '';
+}