安装GrokDebuger环境

文:铁乐与猫

date:2019-12-16

用途

在本地测试Grok的语法,进行调试。

环境

可在windows和linux下安装,而且不需要先安装logstash也可以,但是要用到的grok-patterns可以在己安装logstash的服务器上find找到或github上直接下。

  • Ruby 2.6.3
  • Gems 3.0.3
  • Rails
  • jls-grok
  • grok-parrent 文件(或直接指定到logstsh的相关目录下)

关于安装ruby和gem

我是分开安装好的,你也可以使用RVM(Ruby Version Manager)来进行安装。

参见:http://rvm.io/

参见:https://github.com/rvm/rvm

rvm是ruby版本管理以及安装工具。

安装Rails

gem install rails

若不能访问,则可以添加淘宝镜像 https://ruby.taobao.org/ 到gem中。

安装jls-grok

gem install jls-grok

编写用于grok解析的Ruby脚本

可起名grokdebug.rb,内容如下:

require 'rubygems'
gem 'jls-grok', '=0.11.5'
require 'grok-pure'
require 'optparse'
require 'json'
options = {}
ARGV.push('-h') if ARGV.size === 0
OptionParser.new do |opts|
 opts.banner = 'Run grokdebug at your terminal.'
 options[:dirs] = %w(patterns)
 options[:named] = false
 # 设置grok-patterns文件所在目录,默认是./patterns,注意要自己去下这个文件,不然解析都会报pattern not defined.
 opts.on('-d DIR1,DIR2', '--dirs DIR1,DIR2', Array, 'Set grok patterns directories. Default: "./patterns"') do |value|
   options[:dirs] = value
 end
 opts.on('-m MESSAGE', '--msg MESSAGE', 'Your raw message to be matched') do |value|
   options[:message] = value
 end
 opts.on('-p PATTERN', '--pattern PATTERN', 'Your grok pattern to be compiled') do |value|
   options[:pattern] = value
 end
 opts.on('-n', '--named', 'Named captures only') do
 end
end.parse!
grok = Grok.new
options[:dirs].each do |dir|
 if File.directory?(dir)
   dir = File.join(dir, "*")
 end
 Dir.glob(dir).each do |file|
   grok.add_patterns_from_file(file)
 end
end
grok.compile(options[:pattern], options[:named])
puts JSON.pretty_generate(grok.match(options[:message]).captures())

grok-parrent

USERNAME [a-zA-Z0-9._-]+
USER %{USERNAME}
EMAILLOCALPART [a-zA-Z][a-zA-Z0-9_.+-=:]+
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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PV4})
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
PATH (?:%{UNIXPATH}|%{WINPATH})
UNIXPATH (/([\w_%!$@:.,+~-]+|\\.)*)+
TTY (?:/dev/(pts|tty([pq])?)(\w+)?/?(?:[0-9]+))
WINPATH (?>[A-Za-z]+:|\\)(?:\\[^\\?*]*)+
URIPROTO [A-Za-z]([A-Za-z0-9+\-.]+)+
URIHOST %{IPORHOST}(?::%{POSINT:port})?
# uripath comes loosely from RFC1738, but mostly from what Firefox
# doesn't turn into %XX
URIPATH (?:/[A-Za-z0-9$.+!*'(){},~:;=@#%&_\-]*)+
#URIPARAM \?(?:[A-Za-z0-9]+(?:=(?:[^&]*))?(?:&(?:[A-Za-z0-9]+(?:=(?:[^&]*))?)?)*)?
URIPARAM \?[A-Za-z0-9$.+!*'|(){},~@#%&/=:;_?\-\[\]<>]*
URIPATHPARAM %{URIPATH}(?:%{URIPARAM})?
URI %{URIPROTO}://(?:%{USER}(?::[^@]*)?@)?(?:%{URIHOST})?(?:%{URIPATHPARAM})?

# 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)?|[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}|60)
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:program}(?:\[%{POSINT:pid}\])?
SYSLOGHOST %{IPORHOST}
SYSLOGFACILITY <%{NONNEGINT:facility}.%{NONNEGINT:priority}>
HTTPDATE %{MONTHDAY}/%{MONTH}/%{YEAR}:%{TIME} %{INT}

# Shortcuts
QS %{QUOTEDSTRING}

# Log formats
SYSLOGBASE %{SYSLOGTIMESTAMP:timestamp} (?:%{SYSLOGFACILITY} )?%{SYSLOGHOST:logsource} %{SYSLOGPROG}:

# Log Levels
LOGLEVEL ([Aa]lert|ALERT|[Tt]race|TRACE|[Dd]ebug|DEBUG|[Nn]otice|NOTICE|[Ii]nfo|INFO|[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)?)

还可以创建新文件自定义一些DIY的解析。

实例

E:\elk>ruby grokdebug.rb -m '10.0.0.1' -p '%{IP:client}'
{
 "client": [
   "10.0.0.1"
],
 "IPV6": [
   null
],
 "IPV4": [
   "10.0.0.1"
]
}

用法很简单,-m 指定message,-p 指定pattern。

所以比较适用于一步步拆分来调试,整体看效果的话还是一些在线调试工具稍好用些。

当然你也可以写长长一串message和长长一串pattern来用。

E:\elk>ruby grokdebug.rb -m '172.16.0.5 www.tielemao.com - [15/Dec/2019:23:08:36 -0800]' -p '%{IP:remote_addr} %{HOSTNAME:hostname} %{USER:remote_user} \[%{HTTPDATE:timestamp}\]'
{
 "remote_addr": [
   "172.16.0.5"
],
 "IPV6": [
   null
],
 "IPV4": [
   "172.16.0.5"
],
 "hostname": [
   "www.tielemao.com"
],
 "remote_user": [
   "-"
],
 "USERNAME": [
   "-"
],
 "timestamp": [
   "15/Dec/2019:23:08:36 -0800"
],
 "MONTHDAY": [
   "15"
],
 "MONTH": [
   "Dec"
],
 "YEAR": [
   "2019"
],
 "TIME": [
   "23:08:36"
],
 "HOUR": [
   "23"
],
 "MINUTE": [
   "08"
],
 "SECOND": [
   "36"
],
 "INT": [
   "-0800"
]
}

本地调试的好处是有些在线工具调试对比的parrent未必是符合你使用的那个。

而在本地调试的时候指定的必然是自己要用到的且也可以自定义的。

例2

(?<type>[%{WORD}]+_%{NUMBER}+_%{NUMBER}+|%{NUMBER}+)

用于正则匹配如:

CDownloadReport_7085669_5.1

14

这两种都可以兼顾上。

再拆分CDownloadReport_7085669_5.1下去的话就是:

(?<DownloadReport>[%{WORD}]+)_(?<DownloadSize>%{NUMBER}+)_(?<DownloadTime>%{NUMBER}+)

{
 "DownloadReport": "CDownloadReport",
 "DownloadTime": "5.1",
 "DownloadSize": "7085669"
}

优化为:

 "type" => '%{WORD}_%{NUMBER:download_size:int}_%{NUMBER:download_time:float}'

发表评论

电子邮件地址不会被公开。 必填项已用*标注

3 × 3 =