Grok returns array instead of single string - logstash-grok

I'm new to grok and I have run into this issue that I just don't know how to solve.
Below is my grok match:
grok {
match => { "source" => "/var/log/nginx/sites/\b\w+\b/\b\w+\b/\b\w+\b/%{DATA:uuid}/" }
}
mutate {
add_field => {
"read_timestamp" => "%{#timestamp}"
"token" => "%{[fields][token]}"
"logzio_codec" => "%{[fields][logzio_codec]}"
"uuid" => "%{uuid}"
"type" => "%{[fields][type]}"
"category" => "%{[fields][category]}"
}
}
for some reason, the uuid is matched and resulted in array of 2 uuid (duplicated values). Instead of uuid_string I get [uuid_string, uuid_string]
I tried on https://grokdebug.herokuapp.com/ and got what I expected so I wonder what is wrong?

So once again I misunderstand how grok works. It seems like once the match is done, all the fields are already added to the output. The additional add_field uuid in the mutate thus causes the field to be added twice and logstash then thinks it's an array.

Related

Logstash mutate gsub not working inside "if" statement

i have an issue using logstash mutate filter gsub.
Required
Remove "ZC" characters of a field and coverting it into float
{
"field" => "12.343,40ZC",
"#timestamp" => 2020-01-06T23:00:00.000Z
}
Expected output
{
"field" => "-12343,40",
"#timestamp" => 2020-01-06T23:00:00.000Z
}
Code not working
filter{
if "ZC" in "field" {
mutate { gsub => ["field","ZC",""] }
}
}
Code working
filter{
mutate { gsub => ["field","ZC",""] }
}
I need the "if" statement because depends if the two characters exist inside the field to make a positive or negative float.
Your conditional is wrong, if you use "field" logstash understands that as a string with the value field, the correct way is to use the format [field].
Change your conditional to the following.
filter {
if "ZC" in [field] {
mutate { gsub => ["field","ZC",""] }
}
}

Grok Filter Error in Logstash

I have the following in my filter, for some reason it only prints email and not delivery_status. But when I comment out the email it then prints the delivery _status.
Is there a way to print them both without commenting either of them out?
filter {
grok {
patterns_dir => ["/etc/logstash/patterns/postfix"]
match => { "message" => "%{EMAIL}" }
match => { "message" => "%{DELIVERY_STATUS}" }
overwrite => [ "message" ]
}
}
Your help would be appreciated.
By default the grok filter finishes on the first successful match. If you want to overwrite this behaviour, add this line:
break_on_match => false
For further reference check out the grok filter docs here.

Creating new field with logstash

I have a file named "Job Code.txt"
job_id=0001,description=Ship data from server to elknode1,result=OK
job_id=0002,description=Ship data from server to elknode2,result=Error: Msg...
job_id=0003,description=Ship data from server to elknode3,result=OK
job_id=0004,description=Ship data from server to elknode4,result=OK
Here is the filter part of my .conf file but it doesn't work. How can I created new field, i.e. jobID, description, result as to be seen in kibana
filter{
grok{ match => {"message" => ["JobID: %{NOTSPACE:job_id}","description: %{NOTSPACE:description}","result: %{NOTSPACE:message}"]}
add_field => {
"JobID" => "%{job_id}"
"Description" => "%{description}"
"Message" => "%{message}"
}
}
if [job_id] == "0001" {
aggregate {
task_id => "%{job_id}"
code => "map['time_elasped']=0"
map_action => "create"
}
}
if [job_id] == "0003" {
aggregate {
task_id => "%{job_id}"
code => "map['time_elasped']=0"
map_action => "update"
}
}
if [job_id] == "0002" {
aggregate {
task_id => "%{job_id}"
code => "map['time_elasped']=0"
map_action => "update"
}
}
I know this is a couple days old, perhaps you still require an answer. Change your grok statement to:
grok {
match => { "message" => "job_id=%{DATA:job_id},description=%{DATA:description},result=%{GREEDYDATA:message}" }
}
You won't need the add_field option, grok will create them for you. The add_field option is to add arbitrary fields. Check the pattern at https://grokdebug.herokuapp.com
Also, unless there are other messages you want to match, I don't think the aggregate statements you have will do what you want.

Logstash jdbc plugin understanding

Can anyone explain what does add_field do ?
filter {
mutate {
add_field => { "%{column1}" => "column2" }
}
}
What is the difference between add_field present in mutate, clone, kv and grok plugins
With this config, add_fields adds a new field to the message, the fieldname will be equal to the value of the 'column1' field in the input, and its value will be the litteral 'column2'. If you want the value to be the value of the input field, you have to use %{column2}

How to add a new dynamic value(which is not there in input) to logstash output?

My input has timestamp in the format of Apr20 14:59:41248 Dataxyz.
Now in my output i need the timestamp in the below format:
**Day Month Monthday Hour:Minute:Second Year DataXYZ **. I was able to remove the timestamp from the input. But I am not quite sure how to add the new timestamp.
I matched the message using grok while receiving the input:
match => ["message","%{WORD:word} %{TIME:time} %{GREEDYDATA:content}"]
I tried using mutate add_field.but was not successful in adding the value of the DAY. add_field => [ "timestamp","%{DAY}"].I got the output as the word ´DAY´ and not the value of DAY. Can someone please throw some light on what is being missed.
You need to grok it out into the individual named fields, and then you can reference those fields in add_field.
So your grok would start like this:
%{MONTH:month}%{MONTHDAY:mday}
And then you can put them back together like this:
mutate {
add_field => {
"newField" => "%{mday} %{month}"
}
}
You can check with my answer, I think this very helpful to you.
grok {
match => { "message" => "%{TIMESTAMP_ISO8601:time} \[%{NUMBER:thread}\] %{LOGLEVEL:loglevel} %{JAVACLASS:class} - %{GREEDYDATA:msg}" }
}
if "Exception" in [msg] {
mutate {
add_field => { "msg_error" => "%{msg}" }
}
}
You can use custom grok patterns to extract/rename fields.
You can extract other fields similarly and rearrange/play arounnd with them in mutate filter. Refer to Custom Patterns for more information.

Resources