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