nixpkgs/nixos/modules/services/logging/logstash.nix

162 lines
4.1 KiB
Nix
Raw Normal View History

{ config, lib, pkgs, ... }:
with lib;
let
cfg = config.services.logstash;
pluginPath = lib.concatStringsSep ":" cfg.plugins;
havePluginPath = lib.length cfg.plugins > 0;
ops = lib.optionalString;
verbosityFlag = {
debug = "--debug";
info = "--verbose";
warn = ""; # intentionally empty
error = "--quiet";
fatal = "--silent";
}."${cfg.logLevel}";
in
{
###### interface
options = {
2014-09-05 13:28:24 +00:00
services.logstash = {
2014-09-05 13:28:24 +00:00
enable = mkOption {
2014-09-05 13:28:24 +00:00
type = types.bool;
default = false;
2014-09-05 13:28:24 +00:00
description = "Enable logstash.";
};
package = mkOption {
type = types.package;
default = pkgs.logstash;
defaultText = "pkgs.logstash";
example = literalExample "pkgs.logstash";
description = "Logstash package to use.";
};
plugins = mkOption {
type = types.listOf types.path;
default = [ ];
example = literalExample "[ pkgs.logstash-contrib ]";
description = "The paths to find other logstash plugins in.";
};
logLevel = mkOption {
type = types.enum [ "debug" "info" "warn" "error" "fatal" ];
default = "warn";
description = "Logging verbosity level.";
};
watchdogTimeout = mkOption {
type = types.int;
default = 10;
description = "Set watchdog timeout value in seconds.";
};
filterWorkers = mkOption {
type = types.int;
default = 1;
description = "The quantity of filter workers to run.";
};
enableWeb = mkOption {
2014-09-05 13:28:24 +00:00
type = types.bool;
default = false;
2014-09-05 13:28:24 +00:00
description = "Enable the logstash web interface.";
};
address = mkOption {
type = types.str;
default = "0.0.0.0";
description = "Address on which to start webserver.";
};
port = mkOption {
type = types.str;
default = "9292";
description = "Port on which to start webserver.";
};
inputConfig = mkOption {
2014-09-05 13:28:24 +00:00
type = types.lines;
default = ''stdin { type => "example" }'';
2014-09-05 13:28:24 +00:00
description = "Logstash input configuration.";
example = ''
2013-10-31 14:59:59 +00:00
# Read from journal
pipe {
2015-09-24 09:42:32 +00:00
command => "''${pkgs.systemd}/bin/journalctl -f -o json"
2013-10-31 14:59:59 +00:00
type => "syslog" codec => json {}
}
'';
};
filterConfig = mkOption {
2014-09-05 13:28:24 +00:00
type = types.lines;
default = ''noop {}'';
2014-09-05 13:28:24 +00:00
description = "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 01:21:55 +00:00
"MESSAGE", "PRIORITY", "SYSLOG_FACILITY"
2013-10-31 14:59:59 +00:00
]
}
}
'';
};
outputConfig = mkOption {
2014-09-05 13:28:24 +00:00
type = types.lines;
default = ''stdout { debug => true debug_format => "json"}'';
2014-09-05 13:28:24 +00:00
description = "Logstash output configuration.";
2013-10-31 14:59:59 +00:00
example = ''
redis { host => "localhost" data_type => "list" key => "logstash" codec => json }
elasticsearch { embedded => true }
'';
};
2014-09-05 13:28:24 +00:00
};
};
###### implementation
config = mkIf cfg.enable {
systemd.services.logstash = with pkgs; {
description = "Logstash Daemon";
wantedBy = [ "multi-user.target" ];
environment = { JAVA_HOME = jre; };
2015-09-06 12:52:36 +00:00
path = [ pkgs.bash ];
serviceConfig = {
ExecStart =
"${cfg.package}/bin/logstash agent " +
"-w ${toString cfg.filterWorkers} " +
ops havePluginPath "--pluginpath ${pluginPath} " +
"${verbosityFlag} " +
"--watchdog-timeout ${toString cfg.watchdogTimeout} " +
"-f ${writeText "logstash.conf" ''
input {
${cfg.inputConfig}
}
filter {
${cfg.filterConfig}
}
output {
${cfg.outputConfig}
}
''} " +
ops cfg.enableWeb "-- web -a ${cfg.address} -p ${cfg.port}";
};
};
};
}