Hi,
I have a json file with below multiline json format:
[
{
"status": "fail",
"executiontime": 1117,
"errormsg": "dummy error1",
"testname": "test1",
"errorcode": 0,
"signalcode": 0
},
{
"status": "pass",
"executiontime": 1111,
"errormsg": "Dummy error2",
"testname": "test2",
"errorcode": 0,
"signalcode": 0
},
{
"status": "fail",
"executiontime": 1155,
"errormsg": "Dummy error3",
"testname": "test3",
"errorcode": 0,
"signalcode": 0
}
]
I am using grok pattern to fetch the fields and index them to elasticsearch.
My conf file looks something like below:
#An input plugin enables a specific source of events to be read by Logstash.
input
{
file
{
codec => multiline {
pattern => "^\s\s\s\s}"
negate => true
what => previous
max_lines => 20000
}
path => [path/to//abc.json"]
start_position => "beginning"
sincedb_path => "/dev/null"
type => "test"
ignore_older => 0
}
}
filter
{
if [type] == "test"
{
grok
{
match => [
'message' , '%{GREEDYDATA}"status": "%{GREEDYDATA:status}", \r\n\s+"executiontime": %{GREEDYDATA:exectime}, \r\n\s+"errormsg": "%{GREEDYDATA:error}", \r\n\s+"testname": "%{GREEDYDATA:testname}", \r\n\s+"errorcode": %{GREEDYDATA:errorcode}, \r\n\s+"signalcode": %{GREEDYDATA:signalcode}\r%{GREEDYDATA}'
]
}
if "_jsonparsefailure" in [tags]
{
drop{}
}
if "_grokparsefailure" in [tags]
{
drop {}
}
else
{
mutate
{
gsub => ["message", "\r\n", ""]
remove_field => [ "message", "@version", "path", "host", tags]
}
}
ruby{
code => "
event['exectime'] = event['exectime'].to_i;
event['signalcode'] = event['signalcode'].to_i;
event['errorcode'] = event['errorcode'].to_i;
"
}
}
}
output
{
if [type] == "test"
{
stdout
{
codec => rubydebug
}
}
}
This works fine with the above pattern.
But the fields in the json may not be in the same order when generated.
For example: "errorcode", "signalcode" can appear at the top, testname can appear at the 3rd place as below:
{
"errorcode": 0,
"signalcode": 0,
"testname": "test1",
"status": "pass",
"executiontime": 1111,
"errormsg": "StaleElementReferenceException"
}
I this case the grok pattern which I am using in my config file above will not work.
Is there any way that I can handle the above condition?
Looking for help ASAP.