配置文件,logstash ruby filter event.get(";message";).match()错误
在logstash配置文件中,我试图获取要解析的XML数据 这是我的配置文件:配置文件,logstash ruby filter event.get(";message";).match()错误,ruby,
elasticsearch,logstash,config,Ruby,
elasticsearch,Logstash,Config,在logstash配置文件中,我试图获取要解析的XML数据 这是我的配置文件: input { file { path => "/home/elastic-stack/logstash-7.3.2/event-data/telmetry.log" start_position => "beginning" type => "sandbox-out" codec => multiline {
input {
file {
path => "/home/elastic-stack/logstash-7.3.2/event-data/telmetry.log"
start_position => "beginning"
type => "sandbox-out"
codec => multiline {
pattern => "^</datastore-contents-xml>"
negate => "true"
what => "next"
}
}
http {
host => "127.0.0.1"
port => 8080
type => "sandbox-out"
}
}
filter {
grok {
match => { "message" => "\[%{USER:host_name} %{IP:ip_address} %{USER:session-id} %{NUMBER:session-id-num}\]"}
}
grok {
match => { "message" => "\Subscription Id \: %{BASE16NUM:subcription-id:int}"}
}
grok {
match => { "message" => "\Event time \: %{TIMESTAMP_ISO8601:event-time}"}
}
grok {
match => {"message" => "\<%{USERNAME:Statistic}\>"}
}
mutate {
remove_field => ["headers", "host_name", "session-id","message"]
}
date {
match => ["timestamp","dd/MMM/yyyy:HH:mm:ss Z"]
}
ruby { code => 'event.set("justXml", event.get("message").match(/.+(<datastore-contents-xml.*)/m)[1])' }
xml {
#remove_namespaces => "true"
#not even the namspace option is working to access the http link
source => "justXml"
target => "xml-content"
#force_array => "false"
xpath => [
"//*[name()='datastore-contents-xml']/*[name()='memory-statistics']/*[name()='memory-statistic'][1]/*[name()='name']/text()" , "name" ,
"//*[name()='datastore-contents-xml']/*[name()='memory-statistics']/*[name()='memory-statistic'][1]/*[name()='total-memory']/text()" , "total-memory",
"//*[name()='datastore-contents-xml']/*[name()='memory-statistics']/*[name()='memory-statistic'][1]/*[name()='used-memory']/text()" , "used-memory",
"//*[name()='datastore-contents-xml']/*[name()='memory-statistics']/*[name()='memory-statistic'][1]/*[name()='free-memory']/text()" , "free-memory" ,
"//*[name()='datastore-contents-xml']/*[name()='memory-statistics']/*[name()='memory-statistic'][1]/*[name()='lowest-memory']/text()" , "lowest-memory" ,
"//*[name()='datastore-contents-xml']/*[name()='memory-statistics']/*[name()='memory-statistic'][1]/*[name()='highest-memory']/text()" , "highest-memory"
]
#logstash is not dectecting any of these xpaths in the config
}
mutate {
convert => {
"total-memory" => "integer"
"used-memory" => "integer"
"free-memory" => "integer"
"lowest-memory" => "integer"
"highest-memory" => "integer"
}
}
}
output {
stdout {
codec => rubydebug
}
file {
path => "%{type}_%{+dd_MM_yyyy}.log"
}
}
错误:
[2019-09-27T09:18:55,622][ERROR][logstash.filters.ruby ] Ruby exception occurred: undefined method `match' for nil:NilClass
/home/elastic-stack/logstash-7.3.2/vendor/bundle/jruby/2.5.0/gems/awesome_print-1.7.0/lib/awesome_print/formatters/base_formatter.rb:31: warning: constant ::Fixnum is deprecated
{
"ip_address" => "10.10.20.30",
"subcription-id" => 2147483650,
"session-id-num" => "35",
"tags" => [
[0] "_rubyexception"
],
"Statistic" => "memory-statistic",
"event-time" => "2019-09-12 13:13:30.290000+00:00",
"type" => "sandbox-out",
"@version" => "1",
"host" => "127.0.0.1",
"@timestamp" => 2019-09-27T07:18:54.868Z
通过这个错误,我已经知道问题出在ruby过滤器上,但我不知道如何解决它
此数据由Cisco Telemetry生成,我正在尝试使用弹性堆栈接收它。错误似乎是事件没有
消息
字段,因此无法对不存在的事件调用匹配
。
我看到您正在调用此ruby代码中的message
字段上的match
:
ruby { code => 'event.set("justXml", event.get("message").match(/.+(<datastore-contents-xml.*)/m)[1])' }
解决方案是仅当您不再需要消息字段时才删除它,我会将remove_字段mutate移动到过滤器
块的末尾
如果我可以补充的话,还有一个建议。在同一个消息字段上运行多个grok筛选器:
grok {
match => { "message" => "\[%{USER:host_name} %{IP:ip_address} %{USER:session-id} %{NUMBER:session-id-num}\]"}
}
grok {
match => { "message" => "\Subscription Id \: %{BASE16NUM:subcription-id:int}"}
}
grok {
match => { "message" => "\Event time \: %{TIMESTAMP_ISO8601:event-time}"}
}
grok {
match => {"message" => "\<%{USERNAME:Statistic}\>"}
}
grok{
match=>{“message”=>“\[%{USER:host\u name}%{IP:IP\u address}%{USER:session id}%{NUMBER:session id num}\]”
}
格罗克{
match=>{“message”=>“\Subscription Id\:%{BASE16NUM:subcription Id:int}”
}
格罗克{
匹配=>{“消息”=>“\Event time\:%{TIMESTAMP_ISO8601:Event time}”
}
格罗克{
匹配=>{“消息”=>“\”}
}
这可以简化为(您可以查看:
grok{
在匹配时中断匹配=>false,
匹配=>{
“消息”=>[
“\[%{USER:host\u name}%{IP:IP\u address}%{USER:session id}%{NUMBER:session id num}\]”,
“\Subscription Id\:%{BASE16NUM:Subscription Id:int}”,
“\Event time\:%{TIMESTAMP_ISO8601:Event time}”,
"\"
]
}
}
这样,您只需要grok筛选器的一个实例,因为它将遍历列表中的模式,并且由于break\u on\u match=>false
在第一次成功匹配后将不会完成,但将确保根据列表中的所有模式提取所有字段
mutate {
remove_field => ["headers", "host_name", "session-id","message"]
}
grok {
match => { "message" => "\[%{USER:host_name} %{IP:ip_address} %{USER:session-id} %{NUMBER:session-id-num}\]"}
}
grok {
match => { "message" => "\Subscription Id \: %{BASE16NUM:subcription-id:int}"}
}
grok {
match => { "message" => "\Event time \: %{TIMESTAMP_ISO8601:event-time}"}
}
grok {
match => {"message" => "\<%{USERNAME:Statistic}\>"}
}
grok {
break_on_match => false,
match => {
"message" => [
"\[%{USER:host_name} %{IP:ip_address} %{USER:session-id} %{NUMBER:session-id-num}\]",
"\Subscription Id \: %{BASE16NUM:subcription-id:int}",
"\Event time \: %{TIMESTAMP_ISO8601:event-time}",
"\<%{USERNAME:Statistic}\>"
]
}
}