Skip to content

通过命令行指定 Logstash 配置

🏷️ ELK

在官方 Release 文档 Stashing Your First Event 看到可以通过 -e 参数在命令行中指定配置。

bash
logstash.bat -e 'input { stdin { } } output { stdout {} }'

官方对于 -e 参数的描述:

-e, --config.string CONFIG_STRING
Use the given string as the configuration data. Same syntax as the config file. If no input is specified, then the following is used as the default input: input { stdin { type => stdin } } and if no output is specified, then the following is used as the default output: output { stdout { codec => rubydebug } }. If you wish to use both defaults, please use the empty string for the -e flag. The default is nil.

从上可以看出 CONFIG_STRING 的语法是同配置文件一致的。但在 Windows 下执行时报了 Expected one of # 的错误。

bash
logstash.bat -e 'input { rabbitmq { type => "oct-mid" durable => true exchange => "logstash" exchange_type => "topic" key => "service.*" host => "192.168.0.69" port => 5672 user => "username" password => "password" queue => "OCT_MID_Log" auto_delete => false tags => [ "service" ] } } output { elasticsearch { hosts => [ "http://192.168.0.95:9200" ] index => "logstash-%{+YYYY.MM}" } }'

[ERROR][logstash.agent ] Cannot create pipeline {:reason=>"Expected one of #, => at line 1, column 25 (byte 25) after input { rabbitmq { type "}

网上查到很多人是在使用配置文件时发生的这个错误,是由于文件编码格式导致的。


最后尝试将单引号和双引号替换后,可以正常启动了。问了运维说是 Windows 下本来就不支持单引号的参数导致的。

bash
logstash.bat -e "input { rabbitmq { type => 'oct-mid' durable => true exchange => 'logstash' exchange_type => 'topic' key => 'service.#' host => '192.168.0.69' port => 5672 user => 'username' password => 'password' queue => 'OCT_MID_Log' auto_delete => false tags => [ 'service' ] } } output { elasticsearch { hosts => [ 'http://192.168.0.95:9200' ] index => 'logstash-%{+YYYY.MM}' } }"

参考

  1. Stashing Your First Event
  2. Running Logstash from the Command Line