People with Logstash know to debug Grok regular expressions above http://grokdebug.herokuapp.com. Now the question is: which is the strong wall technology? The page uses the JS file from Google domain name, so the visit frequently fails. So, on the terminal through the command-line mode of rapid debugging became a necessity.
In fact, when Logstash was 1.1, there was a group of people on the official wiki that taught us how to test grok expressions interactively through IRB. But do not know why the wiki this page is gone ... Fortunately, the code itself is not complex, write a few lines of script, you can achieve the purpose:
code is as follows |
&nbs P; |
#!/usr/bin/env ruby require ' rubygems ' Gem ' jls-grok ', ' = 0.11.0 ' require ' grok-pure ' require ' optparse ' require ' ap ' options = {} Argv.push ('-h ') if argv.size = = 0 optionparser.new do |opts| Opts.banner = ' Run grokdebug at your terminal. ' options[:d IRS] =%w (patterns) options[:named] = False Opts.on ('-D dir1,dir2 ', '--dirs DI R1,dir2 ', Array, ' Set grok patterns directories. Default: "./patterns" ' Do |value| options[:d IRS] = value End Opts.on ('-M message ', '--msg message ', ' Your raw Message to is matched ') do |value| Options[:message] = value End opts.on (' P pattern ', '--pattern pattern ', ' Yo ur grok pattern to is compiled ') do |value| options[:p Attern] = value End Opts.on ('-n ', '--named ', ' named captures only ') Do options[:named] = True End end.parse! Grok = grok.new options[:d IRS].EAC H do |dir| if file.directory? (dir) dir = file.join (dir, "*") End Dir.glob (dir). Each do |file| & nbsp Grok.add_patterns_from_file (file) End End Grok.compile (options[:p Attern], options[:named]) AP Grok.match (Options[:message]). Captures () |
Test:
code is as follows |
&nbs P; |
-M,--msg message Your Raw message to is matched -p,--pattern pattern& nbsp; Your Grok compiled -N,--named Named captures only $ ruby grokdebug.rb-m ' abc123 '-P '%{number:test} ' { "test" => [ [0] "123" ], "Base10num" => [ [0] "123" ] $ ruby grokdebug.rb-m ' abc123 '-P '%{number:test:float} ' -N { "test" => [ [0] 123.0 } |
Yes, I have more than a Grokdebug site. type conversion functionality. The Jls-grok is 0.10.10 version, and I use the latest version 0.11.0.