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