微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何使用logstash管道检查EKL中是否存在特定索引?

如何解决如何使用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 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?