我试图让grok使用logstash,但却努力摆脱起跑线。我已经尝试将事情简化为简洁的测试,其中包括:
require "test_utils"
describe "basic grokking" do
extend LogStash::RSpec
config <<-CONFIG
filter {
grok {
match => [ "message", "%{IP:client} %{WORD:method} %{URIPATHPARAM:request} %{NUMBER:bytes} %{NUMBER:duration}" ]
}
}
CONFIG
sample ({'@message' => '55.3.244.1 GET /index.html 15824 0.043'}) do
puts subject.inspect # view http://www.codeshare.io/OhDC0
insist { subject["client"] } == "55.3.244.1"
end
end
我收到以下错误:
1) basic grokking "{"@message":"55.3.244.1 GET /index.html 15824 0.043..." when processed
Failure/Error: Unable to find matching line from backtrace
Insist::Failure:
Expected "55.3.244.1", but got nil
没有多少语法调整得到结果,我也无法弄清楚如何检查subject
以找出那里有什么。
最终目标是使用grok提取以下HttpRequestId
:
[HttpRequestId = e29041b2-a4a0-4bf3-ba05-2de5e7bcf444] 2015/04/10 08:12:51:632 [DEBUG] ... log message ...
使用类似的东西:
grok {
match => [ "Message", "\[HttpRequestId = %{UUID:HttpRequestId}" ]
}
注意我已针对https://grokdebug.herokuapp.com/检查了我的模式并且它们有效。这与我测试的方式有关。
答案 0 :(得分:2)
经过广泛调查,这里是logstash 1.4.2的grok表达式的最小rspec测试:
# encoding: utf-8
require "test_utils"
describe "simple test" do
extend LogStash::RSpec
config <<-CONFIG
filter {
grok {
match => { "message" => "am%{GREEDYDATA:foo}" }
}
}
CONFIG
sample 'amessage' do
insist { subject["foo"] } == "essage"
insist { subject["message"] } == "amessage"
end
end
请注意,您必须拥有# encoding ...
行,否则您将收到Expected "essage", but got nil
消息。
此邮件列表消息有助于:https://groups.google.com/d/msg/logstash-users/rs6jlAxv36M/1ylU-GJ-DVQJ