如何解决如何使用logstash管道检查EKL中是否存在特定索引?
我想编写一个logstash管道来检查ES env中是否存在特定索引;如果是,则将传入事件标记为“有效”,否则标记为“无效”。
使用 cURL 检查索引有效性:
curl -u elastic:elastic -I http://localhost:9200/sampletest1
有效输出 - HTTP/1.1 200 OK
无效输出 - HTTP/1.1 400 Not Found
我的logstash脚本:
input {
beats {
port => "5044"
}
}
filter {
#execute curl to check for index http://localhost:9200/%{process-code}
#if response has 200 then mutate with add_tags "valid". else add tag "invalid"
if "valid" in [tags] {
} else {
#delete event; prevent it from going to output section
}
}
output {
#print only valid events
stdout {
codec => rubydebug
}
}
我被困在过滤器部分提到的 2 # 行。我们不能在过滤器部分使用 exec
插件!
解决方法
按照 Badger 在评论中的建议,使用“http filter plugin”解决了这个问题。
filter {
json { source => "message"}
http {
url => "http://localhost:9200/%{process-code}"
verb => "HEAD"
body_format => "json"
user => "elastic"
password => "elastic"
}
if "_httprequestfailure" in [tags] {
#index not present. drop will prevent it from going to output section
drop {}
} else {
#index present
mutate { add_tag => [ "found" ]}
}
}
注意:上面的http插件在输出事件中添加了_jsonparsefailure
标签;为了避免它,我们可以使用 tag_on_json_failure=>[]
。
参考:https://discuss.elastic.co/t/http-filter-plugin-adds-jsonparsefailure-in-tag/277744
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。