【问题标题】:How to add Elastic Search host name and port as parameter in Logstash?如何在 Logstash 中添加 Elastic Search 主机名和端口作为参数?
【发布时间】:2017-05-22 21:59:38
【问题描述】:

我尝试使用 Elastic Search 的主机名和端口作为 Logstash 输出中的参数,但出现以下错误。

管道由于错误 LogStash::ConfigurationError: Host '%{my_es}' 而中止 已指定,但无效!使用完整的 URL 或主机名:端口字符串

执行以下命令后出现错误:

output { 
    stdout {
       codec => rubydebug { metadata => true }
    } 
    elasticsearch { 
       hosts => "%{my_es}" 
       index => "%{my_ind}" 
       document_id => "%{rwrDate}"
    }
}

我尝试使用 %{my_es} 作为主机名的参数。

如果我使用硬编码值 hosts => ["100.100.100.100:9200"] 一切正常。

output { 
     stdout {
        codec => rubydebug { metadata => true }
     }
     elasticsearch { 
        hosts => ["elasticsearch:9200"] 
        index => "%{my_ind}" 
        document_id => "%{rwrDate}"
     }
}

下面是代码示例

while read line
do
echo $line | ESADDR=echo ${ESHOST} IND_NAME=echo ${PRODUCT}_${KPI} | cut -d" " -f1 | tr 'A-Z' 'a-z' /opt/logstash/bin/logstash -e 'input { stdin {} }   
filter {
    grok { match => {"message" => "%{DATE:rwrDate},%{NUMBER:rwrRate:float}"}} 
    date {match => ["rwrDate","d/M/yyyy"] locale=> "en" target => "rwrDate"} 
    environment { add_metadata_from_env => { "myes" => "ESADDR" } add_field => {"my_es" => "%{[@metadata][myes]}"} } 
    environment { add_metadata_from_env => { "ind" => "IND_NAME" } add_field => {"my_ind" => "%{[@metadata][ind]}"} } 
    mutate {remove_field => ["timestamp"]}
} 
output { 
    stdout {codec => rubydebug { metadata => true }} 
    elasticsearch { hosts => "%{my_es}" index => "%{my_ind}" document_id => "%{rwrDate}"}
}' 
done < $file

【问题讨论】:

  • 试试ruby 过滤器。请参阅:过滤器stackoverflow.com/questions/27954942/…
  • 您的“hosts”参数在未设置该变量时使用值“%{my_es}”。尝试添加一个 stdout{} 输出节,包括元数据,这可能会向您显示 logstash 看到的内容。快速浏览一下,我猜您需要导出变量。
  • 您的“while”循环和 unix 预处理可能是不必要的。为什么不让 logstash 完成所有这些工作?
  • 你好 Yuri,你能告诉我如何在 Ruby 过滤器中初始化数组吗? hosts => ["100.100.100.100:9200"] 是数组类型。所以如果我想动态使用主机,我也需要添加数组。

标签: elasticsearch logstash elasticsearch-plugin logstash-grok logstash-configuration


【解决方案1】:

为什么不使用 shell 中的环境变量替换? 像这样:

export ES_HOST1=10.192.210.100:9200

bin/logstash --path.settings=/etc/logstash -e 'output { elasticsearch { hosts => ["'$ES_HOST1'"] }}'

~ 可能重复: Environment Variable replacement in Logstash when running as a service in Ubuntu

【讨论】:

    猜你喜欢
    • 2020-10-06
    • 1970-01-01
    • 1970-01-01
    • 2016-04-12
    • 2020-07-07
    • 1970-01-01
    • 2022-09-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多