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
版权声明:本文为Jackson_Baekhyun原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。