How to get Json in Grok Logstash - elasticsearch

So currently I'm building a log system using ELK Stack. Before building this ELK, I already have custom log format for my apps, so that it can be easily read by human. My log is formatted something like this
Method: POST
URL: https://localhost:8888/api
Body: {
"field1":"value1",
"field2":[
{
"field3":"value2",
"field4":"value3"
},
{
"field3":"value2",
"field4":"value3"
},
]
}
using grok pattern, I can get the Method and the URL, but how can I get the full body json in grok / logstash so that i can send them to elasticsearch?
Since the length of the json is not fixed and can be longer or shorter each log
Thank you

You can use the JSON Filter.
It should parse the JSON for you, and put it into a structured format so you can then send it where ever you need (e.g. Elasticsearch, another pipeline)
From the docs
It takes an existing field which contains JSON and expands it into an actual data
structure within the Logstash event.
There are also some other questions here on SO that could be helpful. An example: Using JSON with LogStash

Related

AWS AppSync - formatting hardcoded JSON data in response mapping template

I need a GraphQL query returning hardcoded informations as JSON response.
In the AppSync GraphQL schema, I added the following query:
type Query {
getHealthCheck: AWSJSON
}
My response mapping template where the values are hardcoded is the following:
$util.toJson({"version": "0.1.0"})
However, as the response of the query, I get a string instead of a proper JSON, i.e.:
{
"data": {
"getHealthCheck": "{\"version\":\"0.1.0\"}"
}
}
How can I modify the response mapping template to get a proper JSON? I tried several utils but I'm a bit lost with the data structures in VTL.
I don't think this is possible.
If you think about it, it goes against the idea of even having a GraphQL schema to then return essential arbritrary JSON.
I believe that AWSJSON will parse stringified JSON for inputs, but serialize to stringified JSON for outputs.
There's an answer to an other question that might give some ideas of how to work around this. Other than that, it seems your client will need to parse the JSON.

How to apply sub-filter in logstash

the logstash allows to extract patterns via grok filter. My question is how I can use it in sub-sequent filter? For instance, the apache log provides the URI path of the query, something like /path/api?param1=1&param2. I can extract the whole thing in grok filter and assign to attribute request. Now I want to decompose it into different parts. My question is how I can use request attribute and split it further in order to get /path, api, params? Can someone provide an example?
Thanks,
Valentin.
You can use a second grok filter on a newly created field, like this:
grok {
match => { "request" => Your pattern here }
}

access fields from log using ruby filter

What I am trying to do is pass my grok fields in some way or another to an external ruby filter-script and set based on these fields specific tags. The problem is that I can only get the whole log message with the event API.
My question is: is it possible to access fields from the already processed log message in the ruby filter or do I have to parse the whole message myself, which would not be optimal because every log message is processed twice? Alternatively I could completely dump the grok filter and do everything myself in the script.
Yes, it is possible.
You can get read-only access to any field using Event API
filter {
ruby {
code => 'event.get("foo" )'
}
}
field can also be a nested field reference such as [field][bar].
event.get("[foo][bar]")

How to format logs in filebeat

I am very new to filebeat and elasticsearch. I am doing a hobby project and I want to parse my data files. each data files contains the information's as mentioned below format,
<name>
<question>
<ans1>
<ans2>
<ans3>
..etc
I want to read this data and store in es like
{
id : <separate_id_for_each_file>,
name: <name>,
question: <question>,
ans1: <ans1>, ..etc
}
How can I do this with filebeat?
As of now, you can't do this with filebeat.
You will need to send your log to logstash, then transform it usign a plugin like grok and then send it to elastic, if you wish to add a id to the log, you can use something like the uuid plugin before sending it to grok.
Filebeat aims only to be the harvest witch will read your logs and send then forward
So your flow would be something like: filebeat > LOGSTASH[uuid,grok] > ElasticSearch
If you need examples of grok patterns, these can be usefull:
Collection of grok patterns:
https://github.com/logstash-plugins/logstash-patterns-core/blob/master/patterns/grok-patterns
Grok pattern tester:
http://grokconstructor.appspot.com/do/match

Grok pattern for Logstash using HTTP POST request as input

I'm using Logstash to process my logs and store them to Elastic Search.
I'm using http as input plugin for my logstash.
My http post request is:
$http.post(url, {type: 'reference error', message: 'y is not defined'});
I would like to store the type and message key as different fields in Elastic Search.
Currently all of the post data is stored as a single field like:
"message":"{\"type\":\"ReferenceError\",\"message\":\"y is not
defined\"}"
I think this can be done using grok filter but I have not been able to find a way to do this.
Any help is highly appreciated.
Thanks.
If you use the json codec, the information should be split out into fields for you automatically.
EDIT:
As Alain mentioned it is the best way to use the json codec which can be set directly in your http input plugin. If that is not possible for some reason you can use the grok filter.
If I understand you correctly your incoming event looks like this:
{"type": "reference error", "message": "y is not defined"}
Then a corresponding grok pattern would look like this:
{"type": %{QUOTEDSTRING:http_type}, "message": %{QUOTEDSTRING:http_message}}
In your logstash configuration:
grok {
match => [ "message", "{\"type\": %{QUOTEDSTRING:http_type}, \"message\": %{QUOTEDSTRING:http_message}}" ]
}
Then the result will have the two fields http_type and http_message.

Resources