grok字段在線測試(需要翻牆):http://grokdebug.herokuapp.com/
grok自帶的正則比配倉庫:https://github.com/elastic/logstash/blob/v1.4.2/patterns/grok-patterns
前提:
filebeat從5.6的版本以上都有自己解析字段的功能,也具有grok字段,其匹配規則與logstash中的grok正則是一樣的
#filebeat的debug模式:
/usr/local/bin/filebeat -c /etc/filebeat/config.yml -e -d "*"
#nginx日誌格式
www.baidu.com 10.10.10.11 - - [23/Apr/2019:14:29:43 +0800] "GET /my/dex.php?is_ajax=1&moblie=139258467&rand_num=&emailmobil=0 HTTP/1.1" 200 9585 0.016 "https://www.baidu.com/reg/reg_new.php" "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2)" "10.10.10.131, 10.10.10.11" "-" "-" "-" "10.10.10.28:80" "-" "no-cache"
自定義的nginx日誌格式中$http_x_forwarded_for有以下三種情況:
"10.10.10.131, 10.10.10.11"、"www.baidu.com"、"-"
#grok正則解析
%{USERNAME:domain} %{IPV4:client_ip} (.*) \[%{HTTPDATE:timestamp}\] (.*) %{URIPATH:path}(.*)\" (?:%{INT:response_status}|-) (?:%{INT:response_bytes}|-) (?:%{NUMBER:response_time}|-) \"(.*?)\" \"(.*?)\" \"(%(IPV4:agent_ip)[,]|(%{USERNAME:agent_ip})|-)
#(?:%{INT:response_bytes}|-)
某些字段爲“-”,可能導致grokfailure,此時我們可以通過(?:%{XX:XX}|-)的方式進行匹配,即爲空時顯示“-”
#模糊匹配
(.*)
#簡單匹配
(.*?) ?表示只匹配0或1次
參考鏈接:
正則表達式全部符號解釋及示例:https://blog.csdn.net/smartsmile2012/article/details/70141484
grok正則:https://blog.csdn.net/backKeith/article/details/78480125?utm_source=blogxgwz5
grok解析自定義nginx日誌:https://www.cnblogs.com/zhangxun1/articles/6823329.html