1{ config, lib, pkgs, ... }:
2
3with lib;
4
5let
6 cfg = config.services.logstash;
7 pluginPath = lib.concatStringsSep ":" cfg.plugins;
8 havePluginPath = lib.length cfg.plugins > 0;
9 ops = lib.optionalString;
10 verbosityFlag = {
11 debug = "--debug";
12 info = "--verbose";
13 warn = ""; # intentionally empty
14 error = "--quiet";
15 fatal = "--silent";
16 }."${cfg.logLevel}";
17
18in
19
20{
21 ###### interface
22
23 options = {
24
25 services.logstash = {
26
27 enable = mkOption {
28 type = types.bool;
29 default = false;
30 description = "Enable logstash.";
31 };
32
33 package = mkOption {
34 type = types.package;
35 default = pkgs.logstash;
36 defaultText = "pkgs.logstash";
37 example = literalExample "pkgs.logstash";
38 description = "Logstash package to use.";
39 };
40
41 plugins = mkOption {
42 type = types.listOf types.path;
43 default = [ ];
44 example = literalExample "[ pkgs.logstash-contrib ]";
45 description = "The paths to find other logstash plugins in.";
46 };
47
48 logLevel = mkOption {
49 type = types.enum [ "debug" "info" "warn" "error" "fatal" ];
50 default = "warn";
51 description = "Logging verbosity level.";
52 };
53
54 filterWorkers = mkOption {
55 type = types.int;
56 default = 1;
57 description = "The quantity of filter workers to run.";
58 };
59
60 enableWeb = mkOption {
61 type = types.bool;
62 default = false;
63 description = "Enable the logstash web interface.";
64 };
65
66 address = mkOption {
67 type = types.str;
68 default = "0.0.0.0";
69 description = "Address on which to start webserver.";
70 };
71
72 port = mkOption {
73 type = types.str;
74 default = "9292";
75 description = "Port on which to start webserver.";
76 };
77
78 inputConfig = mkOption {
79 type = types.lines;
80 default = ''stdin { type => "example" }'';
81 description = "Logstash input configuration.";
82 example = ''
83 # Read from journal
84 pipe {
85 command => "''${pkgs.systemd}/bin/journalctl -f -o json"
86 type => "syslog" codec => json {}
87 }
88 '';
89 };
90
91 filterConfig = mkOption {
92 type = types.lines;
93 default = ''noop {}'';
94 description = "logstash filter configuration.";
95 example = ''
96 if [type] == "syslog" {
97 # Keep only relevant systemd fields
98 # http://www.freedesktop.org/software/systemd/man/systemd.journal-fields.html
99 prune {
100 whitelist_names => [
101 "type", "@timestamp", "@version",
102 "MESSAGE", "PRIORITY", "SYSLOG_FACILITY"
103 ]
104 }
105 }
106 '';
107 };
108
109 outputConfig = mkOption {
110 type = types.lines;
111 default = ''stdout { debug => true debug_format => "json"}'';
112 description = "Logstash output configuration.";
113 example = ''
114 redis { host => "localhost" data_type => "list" key => "logstash" codec => json }
115 elasticsearch { embedded => true }
116 '';
117 };
118
119 };
120 };
121
122
123 ###### implementation
124
125 config = mkIf cfg.enable {
126 systemd.services.logstash = with pkgs; {
127 description = "Logstash Daemon";
128 wantedBy = [ "multi-user.target" ];
129 environment = { JAVA_HOME = jre; };
130 path = [ pkgs.bash ];
131 serviceConfig = {
132 ExecStart =
133 "${cfg.package}/bin/logstash agent " +
134 "-w ${toString cfg.filterWorkers} " +
135 ops havePluginPath "--pluginpath ${pluginPath} " +
136 "${verbosityFlag} " +
137 "-f ${writeText "logstash.conf" ''
138 input {
139 ${cfg.inputConfig}
140 }
141
142 filter {
143 ${cfg.filterConfig}
144 }
145
146 output {
147 ${cfg.outputConfig}
148 }
149 ''} " +
150 ops cfg.enableWeb "-- web -a ${cfg.address} -p ${cfg.port}";
151 };
152 };
153 };
154}