Debug the grok expression on the terminal command line in ruby
Anyone who uses logstash knows to debug the grok regular expression on the http://grokdebug.herokuapp.com. Now the question is: Which of the following is the best wall flip technology? Js files from google domain names are used in the page, so frequent access failures. Therefore, quick debugging through command line on the terminal has become a necessity.
In fact, when logstash was still in the 1.1 s, there was a batch of official wiki dedicated to teach you how to test the grok expression through irb interactive. But I don't know why the wiki page is missing ...... Fortunately, the Code itself is not complex. A few lines of script can be written to achieve the goal:
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 [: dirs] = % w (patterns) Options [: named] = false Opts. on ('-d DIR1, DIR2', '-- dirs DIR1, DIR2', Array, 'set grok patterns directories. default :". /patterns "') do | value | Options [: dirs] = value End Opts. on ('-m message',' -- msg message', 'Your raw MESSAGE to be matched') do | value | Options [: message] = value End Opts. on ('-p PATTERN', '-- pattern PATTERN', 'Your grok pattern to be computed') do | value | Options [: pattern] = value End Opts. on ('-n',' -- named', 'named captures only') do Options [: named] = true End End. parse! Grok = Grok. new Options [: dirs]. 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 [: pattern], options [: named]) Ap grok. match (options [: message]). captures () |
Test:
The 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 have more type conversion functions than the grokdebug website. It uses jls-grok 0.10.10, and I use the latest version 0.11.0.