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 = "--log.level " + cfg.logLevel;
11
12 pluginsPath = "--path.plugins ${pluginPath}";
13
14 logstashConf = pkgs.writeText "logstash.conf" ''
15 input {
16 ${cfg.inputConfig}
17 }
18
19 filter {
20 ${cfg.filterConfig}
21 }
22
23 output {
24 ${cfg.outputConfig}
25 }
26 '';
27
28 logstashSettingsYml = pkgs.writeText "logstash.yml" cfg.extraSettings;
29
30 logstashSettingsDir = pkgs.runCommand "logstash-settings" {inherit logstashSettingsYml;} ''
31 mkdir -p $out
32 ln -s $logstashSettingsYml $out/logstash.yml
33 '';
34in
35
36{
37 ###### interface
38
39 options = {
40
41 services.logstash = {
42
43 enable = mkOption {
44 type = types.bool;
45 default = false;
46 description = "Enable logstash.";
47 };
48
49 package = mkOption {
50 type = types.package;
51 default = pkgs.logstash;
52 defaultText = "pkgs.logstash";
53 example = literalExample "pkgs.logstash5";
54 description = "Logstash package to use.";
55 };
56
57 plugins = mkOption {
58 type = types.listOf types.path;
59 default = [ ];
60 example = literalExample "[ pkgs.logstash-contrib ]";
61 description = "The paths to find other logstash plugins in.";
62 };
63
64 dataDir = mkOption {
65 type = types.str;
66 default = "/var/lib/logstash";
67 description = ''
68 A path to directory writable by logstash that it uses to store data.
69 Plugins will also have access to this path.
70 '';
71 };
72
73 logLevel = mkOption {
74 type = types.enum [ "debug" "info" "warn" "error" "fatal" ];
75 default = "warn";
76 description = "Logging verbosity level.";
77 };
78
79 filterWorkers = mkOption {
80 type = types.int;
81 default = 1;
82 description = "The quantity of filter workers to run.";
83 };
84
85 listenAddress = mkOption {
86 type = types.str;
87 default = "127.0.0.1";
88 description = "Address on which to start webserver.";
89 };
90
91 port = mkOption {
92 type = types.str;
93 default = "9292";
94 description = "Port on which to start webserver.";
95 };
96
97 inputConfig = mkOption {
98 type = types.lines;
99 default = ''generator { }'';
100 description = "Logstash input configuration.";
101 example = ''
102 # Read from journal
103 pipe {
104 command => "''${pkgs.systemd}/bin/journalctl -f -o json"
105 type => "syslog" codec => json {}
106 }
107 '';
108 };
109
110 filterConfig = mkOption {
111 type = types.lines;
112 default = "";
113 description = "logstash filter configuration.";
114 example = ''
115 if [type] == "syslog" {
116 # Keep only relevant systemd fields
117 # http://www.freedesktop.org/software/systemd/man/systemd.journal-fields.html
118 prune {
119 whitelist_names => [
120 "type", "@timestamp", "@version",
121 "MESSAGE", "PRIORITY", "SYSLOG_FACILITY"
122 ]
123 }
124 }
125 '';
126 };
127
128 outputConfig = mkOption {
129 type = types.lines;
130 default = ''stdout { codec => rubydebug }'';
131 description = "Logstash output configuration.";
132 example = ''
133 redis { host => ["localhost"] data_type => "list" key => "logstash" codec => json }
134 elasticsearch { }
135 '';
136 };
137
138 extraSettings = mkOption {
139 type = types.lines;
140 default = "";
141 description = "Extra Logstash settings in YAML format.";
142 example = ''
143 pipeline:
144 batch:
145 size: 125
146 delay: 5
147 '';
148 };
149
150
151 };
152 };
153
154
155 ###### implementation
156
157 config = mkIf cfg.enable {
158 systemd.services.logstash = with pkgs; {
159 description = "Logstash Daemon";
160 wantedBy = [ "multi-user.target" ];
161 environment = { JAVA_HOME = jre; };
162 path = [ pkgs.bash ];
163 serviceConfig = {
164 ExecStartPre = ''${pkgs.coreutils}/bin/mkdir -p "${cfg.dataDir}" ; ${pkgs.coreutils}/bin/chmod 700 "${cfg.dataDir}"'';
165 ExecStart = concatStringsSep " " (filter (s: stringLength s != 0) [
166 "${cfg.package}/bin/logstash"
167 "-w ${toString cfg.filterWorkers}"
168 (ops havePluginPath pluginsPath)
169 "${verbosityFlag}"
170 "-f ${logstashConf}"
171 "--path.settings ${logstashSettingsDir}"
172 "--path.data ${cfg.dataDir}"
173 ]);
174 };
175 };
176 };
177}