Logstash—Filter模块-grok
一种将非机构化数据转换为结构化数据
1.grok格式
%{SYNTAX:SEMANTIC} # SYNTAX:匹配数据的模式(正则)名称 # SEMANTIC:标识或字段名称
2.相关参数
filter { grok { add_field:增加数据 add_tag:增加tag break_on_match:true 第一个匹配成功结束 keep_empty_captures:true 保存空的事件字段 match:匹配有值的字段 named_captures_only:true 使用grok模式定义的字段 overwrite:覆盖字段的值 pattern_dir:目录 pattern_files_glob:如果设置了目录,则用来选择目录中所有的文件 periodic_flush:规定时间内周期调用flush方法 remove_field:移除字段 remove_tag:移除tag tag_on_failure:无法匹配则弹出tag出错 tag_on_timeout:超时则产生新的tag timeout_millis:超时中断正则表达式 } } ################################################################### filter { grok { add_field=>{"current_time"=>""%{@timestamp}} match=>{"message"=>"%{CISCOTIMESTAMP:timestmap}%{HOST:host} %{WORD:program}:\[%{NUMBER:duration}]%{GREEDYDATA:log}"} remove_field=>["host"] remove_tag=>["grok","test"] } }
3.正则表达式库Oniguruma
https://github.com/logstash-plugins/logstash-patterns-core/blob/main/patterns/ecs-v1/grok-patterns
USERNAME [a-zA-Z0-9._-]+ USER %{USERNAME} EMAILLOCALPART [a-zA-Z0-9!#$%&'*+\-/=?^_`{|}~]{1,64}(?:\.[a-zA-Z0-9!#$%&'*+\-/=?^_`{|}~]{1,62}){0,63} EMAILADDRESS %{EMAILLOCALPART}@%{HOSTNAME} INT (?:[+-]?(?:[0-9]+)) BASE10NUM (?<![0-9.+-])(?>[+-]?(?:(?:[0-9]+(?:\.[0-9]+)?)|(?:\.[0-9]+))) NUMBER (?:%{BASE10NUM}) BASE16NUM (?<![0-9A-Fa-f])(?:[+-]?(?:0x)?(?:[0-9A-Fa-f]+)) BASE16FLOAT \b(?<![0-9A-Fa-f.])(?:[+-]?(?:0x)?(?:(?:[0-9A-Fa-f]+(?:\.[0-9A-Fa-f]*)?)|(?:\.[0-9A-Fa-f]+)))\b POSINT \b(?:[1-9][0-9]*)\b NONNEGINT \b(?:[0-9]+)\b WORD \b\w+\b NOTSPACE \S+ SPACE \s* DATA .*? GREEDYDATA .* QUOTEDSTRING (?>(?<!\\)(?>"(?>\\.|[^\\"]+)+"|""|(?>'(?>\\.|[^\\']+)+')|''|(?>`(?>\\.|[^\\`]+)+`)|``)) UUID [A-Fa-f0-9]{8}-(?:[A-Fa-f0-9]{4}-){3}[A-Fa-f0-9]{12} # URN, allowing use of RFC 2141 section 2.3 reserved characters URN urn:[0-9A-Za-z][0-9A-Za-z-]{0,31}:(?:%[0-9a-fA-F]{2}|[0-9A-Za-z()+,.:=@;$_!*'/?#-])+ # Networking MAC (?:%{CISCOMAC}|%{WINDOWSMAC}|%{COMMONMAC}) CISCOMAC (?:(?:[A-Fa-f0-9]{4}\.){2}[A-Fa-f0-9]{4}) WINDOWSMAC (?:(?:[A-Fa-f0-9]{2}-){5}[A-Fa-f0-9]{2}) COMMONMAC (?:(?:[A-Fa-f0-9]{2}:){5}[A-Fa-f0-9]{2}) IPV6 ((([0-9A-Fa-f]{1,4}:){7}([0-9A-Fa-f]{1,4}|:))|(([0-9A-Fa-f]{1,4}:){6}(:[0-9A-Fa-f]{1,4}|((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){5}(((:[0-9A-Fa-f]{1,4}){1,2})|:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){4}(((:[0-9A-Fa-f]{1,4}){1,3})|((:[0-9A-Fa-f]{1,4})?:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){3}(((:[0-9A-Fa-f]{1,4}){1,4})|((:[0-9A-Fa-f]{1,4}){0,2}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){2}(((:[0-9A-Fa-f]{1,4}){1,5})|((:[0-9A-Fa-f]{1,4}){0,3}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){1}(((:[0-9A-Fa-f]{1,4}){1,6})|((:[0-9A-Fa-f]{1,4}){0,4}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(:(((:[0-9A-Fa-f]{1,4}){1,7})|((:[0-9A-Fa-f]{1,4}){0,5}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:)))(%.+)? IPV4 (?<![0-9])(?:(?:[0-1]?[0-9]{1,2}|2[0-4][0-9]|25[0-5])[.](?:[0-1]?[0-9]{1,2}|2[0-4][0-9]|25[0-5])[.](?:[0-1]?[0-9]{1,2}|2[0-4][0-9]|25[0-5])[.](?:[0-1]?[0-9]{1,2}|2[0-4][0-9]|25[0-5]))(?![0-9]) IP (?:%{IPV6}|%{IPV4}) HOSTNAME \b(?:[0-9A-Za-z][0-9A-Za-z-]{0,62})(?:\.(?:[0-9A-Za-z][0-9A-Za-z-]{0,62}))*(\.?|\b) IPORHOST (?:%{IP}|%{HOSTNAME}) HOSTPORT %{IPORHOST}:%{POSINT} # paths (only absolute paths are matched) PATH (?:%{UNIXPATH}|%{WINPATH}) UNIXPATH (/[[[:alnum:]]_%!$@:.,+~-]*)+ TTY (?:/dev/(pts|tty([pq])?)(\w+)?/?(?:[0-9]+)) WINPATH (?>[A-Za-z]+:|\\)(?:\\[^\\?*]*)+ URIPROTO [A-Za-z]([A-Za-z0-9+\-.]+)+ URIHOST %{IPORHOST}(?::%{POSINT})? # uripath comes loosely from RFC1738, but mostly from what Firefox doesn't turn into %XX URIPATH (?:/[A-Za-z0-9$.+!*'(){},~:;=@#%&_\-]*)+ URIQUERY [A-Za-z0-9$.+!*'|(){},~@#%&/=:;_?\-\[\]<>]* # deprecated (kept due compatibility): URIPARAM \?%{URIQUERY} URIPATHPARAM %{URIPATH}(?:\?%{URIQUERY})? URI %{URIPROTO}://(?:%{USER}(?::[^@]*)?@)?(?:%{URIHOST})?(?:%{URIPATH}(?:\?%{URIQUERY})?)? # Months: January, Feb, 3, 03, 12, December MONTH \b(?:[Jj]an(?:uary|uar)?|[Ff]eb(?:ruary|ruar)?|[Mm](?:a|ä)?r(?:ch|z)?|[Aa]pr(?:il)?|[Mm]a(?:y|i)?|[Jj]un(?:e|i)?|[Jj]ul(?:y|i)?|[Aa]ug(?:ust)?|[Ss]ep(?:tember)?|[Oo](?:c|k)?t(?:ober)?|[Nn]ov(?:ember)?|[Dd]e(?:c|z)(?:ember)?)\b MONTHNUM (?:0?[1-9]|1[0-2]) MONTHNUM2 (?:0[1-9]|1[0-2]) MONTHDAY (?:(?:0[1-9])|(?:[12][0-9])|(?:3[01])|[1-9]) # Days: Monday, Tue, Thu, etc... DAY (?:Mon(?:day)?|Tue(?:sday)?|Wed(?:nesday)?|Thu(?:rsday)?|Fri(?:day)?|Sat(?:urday)?|Sun(?:day)?) # Years? YEAR (?>\d\d){1,2} HOUR (?:2[0123]|[01]?[0-9]) MINUTE (?:[0-5][0-9]) # '60' is a leap second in most time standards and thus is valid. SECOND (?:(?:[0-5]?[0-9]|60)(?:[:.,][0-9]+)?) TIME (?!<[0-9])%{HOUR}:%{MINUTE}(?::%{SECOND})(?![0-9]) # datestamp is YYYY/MM/DD-HH:MM:SS.UUUU (or something like it) DATE_US %{MONTHNUM}[/-]%{MONTHDAY}[/-]%{YEAR} DATE_EU %{MONTHDAY}[./-]%{MONTHNUM}[./-]%{YEAR} ISO8601_TIMEZONE (?:Z|[+-]%{HOUR}(?::?%{MINUTE})) ISO8601_SECOND %{SECOND} TIMESTAMP_ISO8601 %{YEAR}-%{MONTHNUM}-%{MONTHDAY}[T ]%{HOUR}:?%{MINUTE}(?::?%{SECOND})?%{ISO8601_TIMEZONE}? DATE %{DATE_US}|%{DATE_EU} DATESTAMP %{DATE}[- ]%{TIME} TZ (?:[APMCE][SD]T|UTC) DATESTAMP_RFC822 %{DAY} %{MONTH} %{MONTHDAY} %{YEAR} %{TIME} %{TZ} DATESTAMP_RFC2822 %{DAY}, %{MONTHDAY} %{MONTH} %{YEAR} %{TIME} %{ISO8601_TIMEZONE} DATESTAMP_OTHER %{DAY} %{MONTH} %{MONTHDAY} %{TIME} %{TZ} %{YEAR} DATESTAMP_EVENTLOG %{YEAR}%{MONTHNUM2}%{MONTHDAY}%{HOUR}%{MINUTE}%{SECOND} # Syslog Dates: Month Day HH:MM:SS SYSLOGTIMESTAMP %{MONTH} +%{MONTHDAY} %{TIME} PROG [\x21-\x5a\x5c\x5e-\x7e]+ SYSLOGPROG %{PROG:[process][name]}(?:\[%{POSINT:[process][pid]:int}\])? SYSLOGHOST %{IPORHOST} SYSLOGFACILITY <%{NONNEGINT:[log][syslog][facility][code]:int}.%{NONNEGINT:[log][syslog][priority]:int}> HTTPDATE %{MONTHDAY}/%{MONTH}/%{YEAR}:%{TIME} %{INT} # Shortcuts QS %{QUOTEDSTRING} # Log formats SYSLOGBASE %{SYSLOGTIMESTAMP:timestamp} (?:%{SYSLOGFACILITY} )?%{SYSLOGHOST:[host][hostname]} %{SYSLOGPROG}: # Log Levels LOGLEVEL ([Aa]lert|ALERT|[Tt]race|TRACE|[Dd]ebug|DEBUG|[Nn]otice|NOTICE|[Ii]nfo?(?:rmation)?|INFO?(?:RMATION)?|[Ww]arn?(?:ing)?|WARN?(?:ING)?|[Ee]rr?(?:or)?|ERR?(?:OR)?|[Cc]rit?(?:ical)?|CRIT?(?:ICAL)?|[Ff]atal|FATAL|[Ss]evere|SEVERE|EMERG(?:ENCY)?|[Ee]merg(?:ency)?)
4.在线校验
http://grokdebug.herokuapp.com/
http://www.5axxw.com/tools/v2/grok.html
5.例子
日志行
Log flie:Jun 19 02:11:30 This is a sample log.
grok
%{CISCOTIMESTAMP:timestmap} %{GREEDYDATA:log}
结果:
{ "timestmap": [ [ "Jun 19 02:11:30" ] ], "MONTH": [ [ "Jun" ] ], "MONTHDAY": [ [ "19" ] ], "YEAR": [ [ null ] ], "TIME": [ [ "02:11:30" ] ], "HOUR": [ [ "02" ] ], "MINUTE": [ [ "11" ] ], "SECOND": [ [ "30" ] ], "log": [ [ "This is a sample log." ] ] }
6.常用 表达式
主机、ip、路径
HOSTNAME 主机名称 IPORHOST IP或者主机名称 HOSTPORT 主机名(IP)+端口,如: 127.0.0.1:3306、api.stozen.NET:8000 MAC MAC地址 IP IP地址,IPv4或IPv6地址,比如:127.0.0.1、FE80:0000:0000:0000:AAAA:0000:00C2:0002等 PATH 路径,Unix系统或者Windows系统里的路径格式,比如:/usr/xxx、c:\windows\xxx等
字符串、数字
USERNAME 或 USER \w+,比如:1234、Bob、Alex.Wong等 WORD \b\w+\b,比如:String、3529345、ILoveYou等 EMAILLOCALPART [a-zA-Z][a-zA-Z0-9_.+-=:]+ EMAILADDRESS 电子邮件,%{EMAILLOCALPART}@%{HOSTNAME},比如:stone@abc.com、等 BASE10NUM 十进制数字,包括整数和小数,比如:0、18、5.23等 NUMBER 十进制数字,包括整数和小数,比如:0、18、5.23等 BASE16NUM 十六进制数字,整数,比如:0x0045fa2d、-0x3F8709等 BASE16FLOAT 十六进制数字,整数和小数 WORD 字符串,包括数字和大小写字母,比如:String、3529345、ILoveYou等 NOTSPACE 不带任何空格的字符串 SPACE 空格字符串 QUOTEDSTRING 或 QS 带引号的字符串,比如:"This is an apple"、'What is your name?'等 UUID 标准UUID,比如:550E8400-E29B-11D4-A716-446655440000
时间日期
MONTH 月份名称,比如:Jan、January等 MONTHNUM 月份数字,比如:03、9、12等 MONTHDAY 日期数字,比如:03、9、31等 DAY 星期几名称,比如:Mon、Monday等 YEAR 年份数字 HOUR 小时数字 MINUTE 分钟数字 SECOND 秒数字 TIME 时间,比如:00:01:23 DATE_US 美国日期格式,比如:10-15-1982、10/15/1982等 DATE_EU 欧洲日期格式,比如:15-10-1982、15/10/1982、15.10.1982等 DATE 日期,美国日期%{DATE_US}或者欧洲日期%{DATE_EU} ISO8601_TIMEZONE ISO8601时间格式,比如:+10:23、-1023等 TIMESTAMP_ISO8601 ISO8601时间戳格式,比如:2016-07-03T00:34:06+08:00 DATESTAMP 完整日期+时间,比如:07-03-2016 00:34:06 HTTPDATE http默认日期格式,比如:03/Jul/2016:00:36:53 +0800 LOGLEVEL 日志等级,比如:Alert、alert、ALERT、Error等
HTTP
URIPROTO URI协议,比如:http、ftp等 URIHOST URI主机,比如:www.stozen.net、10.0.0.1:22等 URIPATH URI路径,比如://www.stozen.Net/abc/、/api.PHP等 URI 完整的URI,比如:http://www.stozen.net/abc/api.php?a=1&b=2&c=3 URIPARAM URI里的GET参数,比如:?a=1&b=2&c=3 URIPATHPARAM URI路径+GET参数,比如://www.stozen.net/abc/api.php?a=1&b=2&c=3
天道酬勤 循序渐进 技压群雄