2012-07-11 17:22:16 +02:00
|
|
|
{ config, pkgs, ... }:
|
|
|
|
|
|
|
|
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))) +
|
|
|
|
" ]";
|
|
|
|
|
2012-07-11 17:59:00 +02:00
|
|
|
valueToConfig = nvpair: let name = nvpair.name; value = nvpair.value; in
|
2012-07-11 17:22:16 +02:00
|
|
|
if (isAttrs value) && ((!(value ? __type)) || value.__type == "repeated")
|
|
|
|
then ''
|
|
|
|
${name} {
|
|
|
|
${exprToConfig value}
|
|
|
|
}
|
|
|
|
''
|
|
|
|
else "${name} => ${exprToConfig value}";
|
|
|
|
|
2012-07-11 17:59:00 +02:00
|
|
|
repeatedAttrsToConfig = values:
|
|
|
|
concatStringsSep "\n" (map valueToConfig values);
|
2012-07-11 17:22:16 +02:00
|
|
|
|
|
|
|
attrsToConfig = attrs:
|
|
|
|
let
|
2012-07-11 17:59:00 +02:00
|
|
|
attrToConfig = name: valueToConfig {
|
|
|
|
inherit name;
|
|
|
|
value = (getAttr name attrs);
|
|
|
|
};
|
2012-07-11 17:22:16 +02:00
|
|
|
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
|
2012-07-11 17:59:00 +02:00
|
|
|
if isRepeatedAttrs expr then repeatedAttrsToConfig expr.values
|
2012-07-11 17:22:16 +02:00
|
|
|
else attrsToConfig expr;
|
|
|
|
|
|
|
|
mergeConfigs = configs:
|
|
|
|
let
|
|
|
|
op = attrs: newAttrs:
|
|
|
|
let
|
|
|
|
isRepeated = newAttrs ? __type && newAttrs.__type == "repeated";
|
|
|
|
in {
|
2012-07-11 17:59:00 +02:00
|
|
|
values = attrs.values ++ (if isRepeated then newAttrs.values else
|
|
|
|
map (name: { inherit name; value = getAttr name newAttrs; })
|
|
|
|
(attrNames newAttrs));
|
2012-07-11 17:22:16 +02:00
|
|
|
};
|
2012-07-11 17:59:00 +02:00
|
|
|
in (foldl op { values = []; } configs) // { __type = "repeated"; };
|
2012-07-11 17:22:16 +02:00
|
|
|
|
|
|
|
in
|
|
|
|
|
|
|
|
{
|
|
|
|
###### interface
|
|
|
|
|
|
|
|
options = {
|
|
|
|
services.logstash = {
|
|
|
|
enable = mkOption {
|
|
|
|
default = false;
|
|
|
|
description = ''
|
|
|
|
Enable logstash.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
inputConfig = mkOption {
|
|
|
|
default = {};
|
|
|
|
description = ''
|
2012-07-12 20:35:06 +02:00
|
|
|
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,
|
2012-07-11 17:22:16 +02:00
|
|
|
strings, numbers, arrays, hashes, or other name-value pairs,
|
2012-07-12 20:35:06 +02:00
|
|
|
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
|
2012-07-12 20:15:43 +02:00
|
|
|
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.
|
2012-07-11 17:22:16 +02:00
|
|
|
'';
|
2013-10-28 16:14:15 +01:00
|
|
|
apply = mergeConfigs;
|
2012-07-11 17:22:16 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
filterConfig = mkOption {
|
|
|
|
default = {};
|
|
|
|
description = ''
|
2012-07-12 20:35:06 +02:00
|
|
|
An attribute set (or an expression generated by mkNameValuePairs)
|
|
|
|
representing a logstash configuration's filter section.
|
2012-07-11 17:22:16 +02:00
|
|
|
See inputConfig description for details.
|
|
|
|
'';
|
2013-10-28 16:14:15 +01:00
|
|
|
apply = mergeConfigs;
|
2012-07-11 17:22:16 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
outputConfig = mkOption {
|
|
|
|
default = {};
|
|
|
|
description = ''
|
2012-07-12 20:35:06 +02:00
|
|
|
An attribute set (or an expression generated by mkNameValuePairs)
|
|
|
|
representing a logstash configuration's output section.
|
2012-07-11 17:22:16 +02:00
|
|
|
See inputConfig description for details.
|
|
|
|
'';
|
2013-10-28 16:14:15 +01:00
|
|
|
apply = mergeConfigs;
|
2012-07-11 17:22:16 +02:00
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
###### implementation
|
|
|
|
|
2012-07-12 20:15:43 +02:00
|
|
|
config = mkMerge [ {
|
|
|
|
lib.logstash = {
|
|
|
|
mkFloat = stringRep: { __type = "float"; value = stringRep; };
|
|
|
|
|
|
|
|
mkHash = attrs: { __type = "hash"; value = attrs; };
|
|
|
|
|
|
|
|
mkNameValuePairs = mergeConfigs;
|
|
|
|
};
|
|
|
|
} ( mkIf cfg.enable {
|
2013-01-16 12:33:18 +01:00
|
|
|
systemd.services.logstash = with pkgs; {
|
2012-07-11 17:22:16 +02:00
|
|
|
description = "Logstash daemon";
|
2012-10-30 14:18:51 +01:00
|
|
|
|
|
|
|
wantedBy = [ "multi-user.target" ];
|
2012-07-11 17:22:16 +02:00
|
|
|
|
|
|
|
path = [ jre ];
|
|
|
|
|
2012-10-30 14:22:14 +01:00
|
|
|
script = "cd /tmp && exec java -jar ${logstash} agent -f ${writeText "logstash.conf" ''
|
2012-07-11 17:22:16 +02:00
|
|
|
input {
|
|
|
|
${exprToConfig cfg.inputConfig}
|
|
|
|
}
|
|
|
|
|
|
|
|
filter {
|
|
|
|
${exprToConfig cfg.filterConfig}
|
|
|
|
}
|
|
|
|
|
|
|
|
output {
|
|
|
|
${exprToConfig cfg.outputConfig}
|
|
|
|
}
|
2012-10-30 14:22:14 +01:00
|
|
|
''} &> /var/log/logstash.log";
|
2012-07-11 17:22:16 +02:00
|
|
|
};
|
2012-07-12 20:15:43 +02:00
|
|
|
})];
|
2012-07-11 17:22:16 +02:00
|
|
|
}
|