如何在Logstash中添加Elastic Search主机名和端口作为参数?

时间:2017-01-08 15:47:01

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

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

  

由于错误LogStash :: ConfigurationError导致管道中止:主机'%{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

1 个答案:

答案 0 :(得分:0)

为什么不使用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