Debugging Grok expressions on the terminal command line in Ruby _php tutorial

Source: Internet
Author: User
Tags logstash

Debugging Grok expressions on the terminal command line in Ruby


People with Logstash know to debug Grok regular expressions on http://grokdebug.herokuapp.com. Now the question is: which is the best technology to turn the wall? The page uses the JS file from the Google domain name, so the access fails frequently. Therefore, it is necessary to quickly debug on the terminal by means of command line.

In fact, when Logstash was still in his 1.1, the official wiki was a group of people who taught us how to grok expressions through the IRB interactive test. But I don't know why the wiki page is gone ... Fortunately, the code itself is not complex, just a few lines of script, you can achieve the purpose:

The code is as follows
#!/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 directories. Default: "./patterns" ') do |value|
options[:d IRS] = value
End
Opts.on ('-M message ', '--msg message ', ' Your raw message to being matched ') do |value|
Options[:message] = value
End
Opts.on ('-P pattern ', '--pattern pattern ', ' Your grok pattern 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 it:

code is as follows
$ 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 be matched
-P,--pattern pattern Your grok pattern to be 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've got more than grokdebug. type conversion functionality. It uses the Jls-grok version of 0.10.10, and I'm using the latest version of 0.11.0.

http://www.bkjia.com/PHPjc/898888.html www.bkjia.com true http://www.bkjia.com/PHPjc/898888.html techarticle debug Grok expression on terminal command line in Ruby Anyone with Logstash knows to debug grok regular expression above http://grokdebug.herokuapp.com. Now the question is: turn over the wall technology which ...

  • 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.