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 logstashSettingsDir = pkgs.runCommand "logstash-settings" {
27 inherit logstashSettingsYml;
28 preferLocalBuild = true;
29 } ''
30 mkdir -p $out
31 ln -s $logstashSettingsYml $out/logstash.yml
32 '';
33in
34
35{
36 imports = [
37 (mkRenamedOptionModule [ "services" "logstash" "address" ] [ "services" "logstash" "listenAddress" ])
38 (mkRemovedOptionModule [ "services" "logstash" "enableWeb" ] "The web interface was removed from logstash")
39 ];
40
41 ###### interface
42
43 options = {
44
45 services.logstash = {
46
47 enable = mkOption {
48 type = types.bool;
49 default = false;
50 description = "Enable logstash.";
51 };
52
53 package = mkOption {
54 type = types.package;
55 default = pkgs.logstash;
56 defaultText = "pkgs.logstash";
57 example = literalExample "pkgs.logstash";
58 description = "Logstash package to use.";
59 };
60
61 plugins = mkOption {
62 type = types.listOf types.path;
63 default = [ ];
64 example = literalExample "[ pkgs.logstash-contrib ]";
65 description = "The paths to find other logstash plugins in.";
66 };
67
68 dataDir = mkOption {
69 type = types.str;
70 default = "/var/lib/logstash";
71 description = ''
72 A path to directory writable by logstash that it uses to store data.
73 Plugins will also have access to this path.
74 '';
75 };
76
77 logLevel = mkOption {
78 type = types.enum [ "debug" "info" "warn" "error" "fatal" ];
79 default = "warn";
80 description = "Logging verbosity level.";
81 };
82
83 filterWorkers = mkOption {
84 type = types.int;
85 default = 1;
86 description = "The quantity of filter workers to run.";
87 };
88
89 listenAddress = mkOption {
90 type = types.str;
91 default = "127.0.0.1";
92 description = "Address on which to start webserver.";
93 };
94
95 port = mkOption {
96 type = types.str;
97 default = "9292";
98 description = "Port on which to start webserver.";
99 };
100
101 inputConfig = mkOption {
102 type = types.lines;
103 default = "generator { }";
104 description = "Logstash input configuration.";
105 example = ''
106 # Read from journal
107 pipe {
108 command => "''${pkgs.systemd}/bin/journalctl -f -o json"
109 type => "syslog" codec => json {}
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 # http://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
155 };
156 };
157
158
159 ###### implementation
160
161 config = mkIf cfg.enable {
162 systemd.services.logstash = {
163 description = "Logstash Daemon";
164 wantedBy = [ "multi-user.target" ];
165 path = [ pkgs.bash ];
166 serviceConfig = {
167 ExecStartPre = ''${pkgs.coreutils}/bin/mkdir -p "${cfg.dataDir}" ; ${pkgs.coreutils}/bin/chmod 700 "${cfg.dataDir}"'';
168 ExecStart = concatStringsSep " " (filter (s: stringLength s != 0) [
169 "${cfg.package}/bin/logstash"
170 "-w ${toString cfg.filterWorkers}"
171 (concatMapStringsSep " " (x: "--path.plugins ${x}") cfg.plugins)
172 "${verbosityFlag}"
173 "-f ${logstashConf}"
174 "--path.settings ${logstashSettingsDir}"
175 "--path.data ${cfg.dataDir}"
176 ]);
177 };
178 };
179 };
180}