Debugging Grok expressions on the terminal command line in Ruby

Source: Internet
Author: User
Tags require logstash

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.