logstash过滤器之translate
官方手册:https://www.elastic.co/guide/en/logstash/current/plugins-filters-translate.html#plugins-filters-translate-target
功能描述
translate过滤器插件用于根据字典或查找文件过滤传入数据中的特定字段,如果输入字段与字典查找文件中的条目匹配,则它将执行操作。
常用于对日志事件,根据特定的字段打上标签,比如对日志中的hostip字段,打上对应的应用系统名的标签
使用方法
可以通过以下任意一种方式指定字典,不可同时使用以下两种方式配置
- 通过 dictionary 配置项配置
- 可以在dictionary_path配置项中指定外部文件
如原始日志事件如下:
{"hostip":"10.17.2.101","logid":"1001"}
{"hostip":"10.17.2.102","logid":"1002"}
{"hostip":"10.17.2.103","logid":"1003"}
根据事件中的hostip字段值,将日志打上应用系统标签
通过 dictionary 配置
input {
stdin{
codec => json
}
}
filter {
translate {
source => "hostip"
target => "resource_name"
dictionary => {
"10.17.2.101" => "应用1"
"10.17.2.102" => "应用2"
}
fallback => "未知" //默认匹配结果
}
}
translate 处理之后如下:
通过 dictionary_path 配置
dictionary_path 文件路径为 “/data/resource_lookup.yaml”,内容如下:
"10.17.2.101": "应用1" # 注意,冒号后面要有个空格
"10.17.2.102": "应用2"
logstash中配置如下:
input {
stdin{
codec => json
}
}
filter {
translate {
source => "hostip"
target => "resource_name"
dictionary_path => "/data/resource_lookup.yaml"
fallback => "未知"
}
}