logstash grok 多项匹配

业务场景:新版本日志需要添加字段,需要兼容新旧日志匹配

版本:logstash-2.3

filter {
    grok {
         match => [
            "message" , "%{DATA:hostname}\|%{DATA:tag}\|%{DATA:types}\|%{DATA:uid}\|%{GREEDYDATA:msg}",
            "message" , "%{DATA:hostname}\|%{DATA:tag}\|%{GREEDYDATA:msg}"
         ]
        remove_field => ['type','_id','input_type','tags','message','beat','offset']
    }
}
filter {
    grok {
	match => {
	    "message"=>[
		"%{DATA:hostname}\|%{DATA:tag}\|%{DATA:types}\|%{DATA:uid}\|%{GREEDYDATA:msg}",
		"%{DATA:hostname}\|%{DATA:tag}\|%{GREEDYDATA:msg}"]
	    }
    }
}
太多使用DATA和GREEDYDAYA会导致性能cpu负载严重。建议多使用正则匹配,或者ruby代码块
filter {
     grok {
        match => [
               "message", "(?[a-zA-Z0-9._-]+)\|%{DATA:tag}\|%{NUMBER:types}\|(?[0-9]+)\|%{GREEDYDATA:msg}",
               "message", "(?[a-zA-Z0-9._-]+)\|%{DATA:tag}\|%{GREEDYDATA:msg}",
        ]
       remove_field => ['type','_id','input_type','tags','message','beat','offset']
    }
}
filter {
    ruby {
        code =>'
        arr = event["message"].split("|")
        if arr.length == 5
            event["hostname"] = arr[0]
            event["tag"] = arr[1]
            event["types"] = arr[2]
            event["uid"] = arr[3]
            event["msg"] = arr[4]
        elsif arr.length == 3
            event["hostname"] = arr[0]
            event["tag"] = arr[1]
            event["msg"] = arr[2]
        end'
       remove_field => ['type','_id','input_type','tags','message','beat','offset']
    }
}

参考文档:https://github.com/chenryn/logstash-best-practice-cn/blob/master/filter/grok.md

你可能感兴趣的:(elk)