3
0
Fork 0
forked from mirrors/nixpkgs
nixpkgs/nixos/modules/services/logging/logstash.nix

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

195 lines
5.2 KiB
Nix
Raw Normal View History

{ config, lib, pkgs, ... }:
with lib;
let
cfg = config.services.logstash;
ops = lib.optionalString;
2018-08-03 11:23:56 +01:00
verbosityFlag = "--log.level " + cfg.logLevel;
logstashConf = pkgs.writeText "logstash.conf" ''
input {
${cfg.inputConfig}
}
filter {
${cfg.filterConfig}
}
output {
${cfg.outputConfig}
}
'';
logstashSettingsYml = pkgs.writeText "logstash.yml" cfg.extraSettings;
logstashJvmOptionsFile = pkgs.writeText "jvm.options" cfg.extraJvmOptions;
logstashSettingsDir = pkgs.runCommand "logstash-settings" {
inherit logstashJvmOptionsFile;
inherit logstashSettingsYml;
preferLocalBuild = true;
} ''
mkdir -p $out
ln -s $logstashSettingsYml $out/logstash.yml
ln -s $logstashJvmOptionsFile $out/jvm.options
'';
in
{
imports = [
(mkRenamedOptionModule [ "services" "logstash" "address" ] [ "services" "logstash" "listenAddress" ])
(mkRemovedOptionModule [ "services" "logstash" "enableWeb" ] "The web interface was removed from logstash")
];
###### interface
options = {
2014-09-05 14:28:24 +01:00
services.logstash = {
2014-09-05 14:28:24 +01:00
enable = mkOption {
2014-09-05 14:28:24 +01:00
type = types.bool;
default = false;
description = lib.mdDoc "Enable logstash.";
};
package = mkOption {
type = types.package;
default = pkgs.logstash;
defaultText = literalExpression "pkgs.logstash";
description = lib.mdDoc "Logstash package to use.";
};
plugins = mkOption {
type = types.listOf types.path;
default = [ ];
example = literalExpression "[ pkgs.logstash-contrib ]";
description = lib.mdDoc "The paths to find other logstash plugins in.";
};
dataDir = mkOption {
type = types.str;
default = "/var/lib/logstash";
description = lib.mdDoc ''
A path to directory writable by logstash that it uses to store data.
Plugins will also have access to this path.
'';
};
logLevel = mkOption {
type = types.enum [ "debug" "info" "warn" "error" "fatal" ];
default = "warn";
description = lib.mdDoc "Logging verbosity level.";
};
filterWorkers = mkOption {
type = types.int;
default = 1;
description = lib.mdDoc "The quantity of filter workers to run.";
};
listenAddress = mkOption {
type = types.str;
default = "127.0.0.1";
description = lib.mdDoc "Address on which to start webserver.";
};
port = mkOption {
type = types.str;
default = "9292";
description = lib.mdDoc "Port on which to start webserver.";
};
inputConfig = mkOption {
2014-09-05 14:28:24 +01:00
type = types.lines;
default = "generator { }";
description = lib.mdDoc "Logstash input configuration.";
example = literalExpression ''
'''
# Read from journal
pipe {
command => "''${config.systemd.package}/bin/journalctl -f -o json"
type => "syslog" codec => json {}
}
'''
2013-10-31 14:59:59 +00:00
'';
};
filterConfig = mkOption {
2014-09-05 14:28:24 +01:00
type = types.lines;
default = "";
description = lib.mdDoc "logstash filter configuration.";
example = ''
2013-10-31 14:59:59 +00:00
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",
2016-06-21 02:21:55 +01:00
"MESSAGE", "PRIORITY", "SYSLOG_FACILITY"
2013-10-31 14:59:59 +00:00
]
}
}
'';
};
outputConfig = mkOption {
2014-09-05 14:28:24 +01:00
type = types.lines;
default = "stdout { codec => rubydebug }";
description = lib.mdDoc "Logstash output configuration.";
2013-10-31 14:59:59 +00:00
example = ''
redis { host => ["localhost"] data_type => "list" key => "logstash" codec => json }
elasticsearch { }
2013-10-31 14:59:59 +00:00
'';
};
2014-09-05 14:28:24 +01:00
extraSettings = mkOption {
type = types.lines;
default = "";
description = lib.mdDoc "Extra Logstash settings in YAML format.";
example = ''
pipeline:
batch:
size: 125
delay: 5
'';
};
extraJvmOptions = mkOption {
type = types.lines;
default = "";
description = lib.mdDoc "Extra JVM options, one per line (jvm.options format).";
example = ''
-Xms2g
-Xmx2g
'';
};
};
};
###### implementation
config = mkIf cfg.enable {
systemd.services.logstash = {
description = "Logstash Daemon";
wantedBy = [ "multi-user.target" ];
2015-09-06 13:52:36 +01:00
path = [ pkgs.bash ];
serviceConfig = {
ExecStartPre = ''${pkgs.coreutils}/bin/mkdir -p "${cfg.dataDir}" ; ${pkgs.coreutils}/bin/chmod 700 "${cfg.dataDir}"'';
ExecStart = concatStringsSep " " (filter (s: stringLength s != 0) [
"${cfg.package}/bin/logstash"
"-w ${toString cfg.filterWorkers}"
(concatMapStringsSep " " (x: "--path.plugins ${x}") cfg.plugins)
"${verbosityFlag}"
"-f ${logstashConf}"
2018-08-03 11:23:56 +01:00
"--path.settings ${logstashSettingsDir}"
"--path.data ${cfg.dataDir}"
]);
};
};
};
}