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:
The code is as follows |
Copy Code |
#!/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 dir1,dir2 ', Array, ' Set grok patterns. 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 ', ' Your grok the pattern of to being 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].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[:p Attern], options[:named]) AP Grok.match (Options[:message]). Captures () |
Test:
The code is as follows |
Copy Code |
$ sudo gem install Jls-grok awesome_print $ Ruby Grokdebug.rb Run Grokdebug at your terminal. -D,--dirs dir1,dir2 Set grok patterns directories. Default: "./patterns" -M,--msg message Your Raw message to is matched -P,--pattern pattern Your grok pattern to being 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 Grokdebug Web site. The function of type conversion. The Jls-grok is 0.10.10 version, and I use the latest version 0.11.0.