Hello. In message there is json, but in some messages there is no json. I want set message to dash in first case (where there is no json). How to do it? Please, help me.
cat /etc/logstash/mytests/test1
input {
generator { count => 1 message => '2018-06-13 20:18:08 > 95.153.131.227 > RESOURCE#24973 > OPENED' }
generator { count => 1 message => '2018-06-20 12:58:52 > 95.153.222.121 > RESOURCE#51059 > DRIVER #1976 > ACTION QUERY: {"action":"orderSum","parameters":{"trip_id":12507}}' }
}
output { stdout { codec => rubydebug { metadata => true } } }
filter {
grok {
match => { "message" => "%{DATESTAMP:timestamp}%{SPACE}>%{SPACE}%{IP:remote_ip}%{SPACE}>%{SPACE}RESOURCE#%{NONNEGINT:resource}%{SPACE}>%{SPACE}((?\w+$)|DRIVER%{SPACE}#%{NONNEGINT:driver_id}%{SPACE}>%{SPACE}(?([\s\w]+[^:{]+$|\w+(\s+[^\s:]+))?):?(%{SPACE}(?{.*}))?)" }
overwrite => [ "message" ]
}
date {
match => ["timestamp", "yy-MM-dd HH:mm:ss"]
target => "@timestamp"
timezone => "Europe/Moscow"
}
mutate {
remove_field => [ "timestamp" ]
}
}
xen ~ # /opt/logstash/bin/logstash -f /etc/logstash/mytests/test1
{
"sequence" => 0,
"driver_id" => "1976",
"remote_ip" => "95.153.222.121",
"@timestamp" => 2018-06-20T09:58:52.000Z,
"resource" => "51059",
"@version" => "1",
"host" => "xen",
"message" => "{"action":"orderSum","parameters":{"trip_id":12507}}",
"category" => "ACTION QUERY"
}
{
"sequence" => 0,
"remote_ip" => "95.153.131.227",
"@timestamp" => 2018-06-13T17:18:08.000Z,
"resource" => "24973",
"@version" => "1",
"host" => "xen",
"message" => "2018-06-13 20:18:08 > 95.153.131.227 > RESOURCE#24973 > OPENED",
"category" => "OPENED"
}
I want in second case in output (first generator in input) set message to "-", but not original message, because original message have no json. Grok matches in this two case. In first case non-obligatory field "message" does not match.