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:

  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.

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.