Having Logstash reading JSON - json

I am trying to use logstash for analyzing a file containing JSON objects as follows:
{"Query":{"project_id":"a7565b911f324a9199a91854ea18de7e","timestamp":1392076800,"tx_id":"2e20a255448742cebdd2ccf5c207cd4e","token":"3F23A788D06DD5FE9745D140C264C2A4D7A8C0E6acf4a4e01ba39c66c7c9cbd6a123588b22dc3a24"}}
{"Response":{"result_code":"Success","project_id":"a7565b911f324a9199a91854ea18de7e","timestamp":1392076801,"http_status_code":200,"tx_id":"2e20a255448742cebdd2ccf5c207cd4e","token":"3F23A788D06DD5FE9745D140C264C2A4D7A8C0E6acf4a4e01ba39c66c7c9cbd6a123588b22dc3a24","targets":[]}}
{"Query":{"project_id":"a7565b911f324a9199a91854ea18de7e","timestamp":1392076801,"tx_id":"f7f68c7fb14f4959a1db1a206c88a5b7","token":"3F23A788D06DD5FE9745D140C264C2A4D7A8C0E6acf4a4e01ba39c66c7c9cbd6a123588b22dc3a24"}}
Ideally i'd expect Logstash to understand the JSON.
I used the following config:
input {
file {
type => "recolog"
format => json_event
# Wildcards work, here :)
path => [ "/root/isaac/DailyLog/reco.log" ]
}
}
output {
stdout { debug => true }
elasticsearch { embedded => true }
}
I built this file based on this Apache recipe
When running logstash with debug = true, it reads the objects like this:
How could i see stats in the kibana GUI based on my JSON file, for example number of Query objects and even queries based on timestamp.
For now it looks like it understand a very basic version of the data not the structure of it.
Thx in advance

I found out that logstash will automatically detect JSON byt using the codec field within the file input as follows:
input {
stdin {
type => "stdin-type"
}
file {
type => "prodlog"
# Wildcards work, here :)
path => [ "/root/isaac/Mylogs/testlog.log"]
codec => json
}
}
output {
stdout { debug => true }
elasticsearch { embedded => true }
}
Then Kibana showed the fields of the JSON perfectly.

Related

How to send json events to ES with Logstash Plugin for Amazon ES?

I am new to ES. Trying to send json events to ES with https://github.com/awslabs/logstash-output-amazon_es
However, when I give below configuration it does not recognize any events?
input {
file {
path => "C:/Program Files/logstash-2.3.1/transactions.log"
start_position => beginning
codec => "json_lines"
}
}
filter {
json {
source => "message"
}
}
output {
amazon_es {
hosts => ["endpoint"]
region => "us-east-1"
codec => json
index => "production-logs-%{+YYYY.MM.dd}"
}
}
I am running it in debug mode but there is nothing in the log
Also do I need to create the index before I start sending the events from Logstash?
The below config works somehow, however it does not recognize any json fields
input {
file {
path => "C:/Program Files/logstash-2.3.1/transactions.log"
start_position => beginning
}
}
output {
amazon_es {
hosts => ["Endpoint"]
region => "us-east-1"
index => "production-logs-%{+YYYY.MM.dd}"
}
}
There may be several things at play here, including:
Logstash thinks your file has already been processed. start_position is only for files that haven't been seen before. If you're testing, set sincedb_path to /dev/null, or manually manage your registry files.
You're having mapping problems. Elasticsearch will drop documents when the field mapping isn't correct (trying to insert a string into a numeric field, etc). This should be shown in the elasticsearch logs, if you can get to them on AWS.
debug is very verbose. If you're really getting nothing, then you're not receiving any input. See the first bullet item.
adding a stdout{} output is a good idea until you get things working. This will show you what logstash is sending to elasticsearch.

Logstash - convert JSON to readable format - during logging to a file

I have a Logstash configuration as given below:
input {
udp {
port => 5043
codec => json
}
}
output {
file {
path => "/logfile.log"
}
}
I am trying to log messages in the "logfile.log" which are more readable.
So if my input data is like {"attr1":"val1","attr2":"val2"}
I want to write it in the log as:
attr1_val1 | attr2_val2
Basically converting data from JSON to a readable format.
What should I be modifying in my Logstash configuration to do that?
The message_format option of the file output allows you to specify how each message should be formatted. If the keys of your messages are fixed and known you can simply do this:
output {
file {
message_format => "attr1_%{attr1} | attr2_%{attr2}"
...
}
}
To handle arbitrary fields you'll probably have to write some custom Ruby code using the ruby filter. The following filter, for example, produces the same results as above but doesn't require you to hardcode the names of the fields:
filter {
ruby {
code => '
values = []
event.to_hash.each { |k, v|
next if k.start_with? "#"
values << "#{k}_#{v.to_s}"
}
event["myfield"] = values.join(" | ")
'
}
}
output {
file {
message_format => "%{myfield}"
...
}
}

Logstash is not pushing json format files

I am new in Elasticsearch,Kibana et Logstash. I am trying to load a json file like this one:
{"timestamp":"2014-05-19T00:00:00.430Z","memoryUsage":42.0,"totalMemory":85.74,"usedMemory":78.77,"cpuUsage":26.99,"monitoringType":"jvmHealth"}
{"timestamp":"2014-05-19T00:09:10.431Z","memoryUsage":43.0,"totalMemory":85.74,"usedMemory":78.77,"cpuUsage":26.99,"monitoringType":"jvmHealth"}
{"timestamp":"2014-05-19T00:09:10.441Z","transactionTime":1,"nbAddedObjects":0,"nbRemovedObjects":0,"monitoringType":"transactions"}
{"timestamp":"2014-05-19T00:09:10.513Z","transactionTime":6,"nbAddedObjects":4,"nbRemovedObjects":0,"monitoringType":"transactions"}
No index is created and I just get this message :
Using milestone 2 input plugin 'file'. This plugin should be stable,
but if you see strange behavior, please let us know! For more
information on plugin milestones, see
http://logstash.net/docs/1.4.1/plugin-milestones {:level=>:warn}
What can be the problem? I can use directly bulk but I must go with logstash.
Do you have any suggested code that can help?
EDIT (to move the config from a comment into the question):
input {
file {
path => "/home/ndoye/Elasticsearch/great_log.json"
type => json
codec => json
}
}
filter {
date {
match => ["timestamp","yyyy-MM-dd HH:mm:ss.SSS"]
}
}
output {
stdout{
#codec => rubydebug
}
elasticsearch {
embedded => true
}
}

Using JSON with LogStash

I'm going out of my mind here. I have an app that writes logs to a file. Each log entry is a JSON object. An example of my .json file looks like the following:
{"Property 1":"value A","Property 2":"value B"}
{"Property 1":"value x","Property 2":"value y"}
I'm trying desperately to get the log entries into LogStash. In an attempt to do this, I've created the following LogStash configuration file:
input {
file {
type => "json"
path => "/logs/mylogs.log"
codec => "json"
}
}
output {
file {
path => "/logs/out.log"
}
}
Right now, I'm manually adding records to mylogs.log to try and get it working. However, they appear oddly in the stdout. When I look open out.log, I see something like the following:
{"message":"\"Property 1\":\"value A\", \"Property 2\":\"value B\"}","#version":"1","#timestamp":"2014-04-08T15:33:07.519Z","type":"json","host":"ip-[myAddress]","path":"/logs/mylogs.log"}
Because of this, if I send the message to ElasticSearch, I don't get the fields. Instead I get a jumbled mess. I need my properties to still be properties. I do not want them crammed into the message portion or the output. I have a hunch this has something to do with Codecs. Yet, I'm not sure. I'm not sure if I should change the codec on the logstash input configuration. Or, if I should change the input on the output configuration.
Try removing the json codec and adding a json filter:
input {
file {
type => "json"
path => "/logs/mylogs.log"
}
}
filter{
json{
source => "message"
}
}
output {
file {
path => "/logs/out.log"
}
}
You do not need the json codec because you do not want decode the source JSON but you want filter the input to get the JSON data in the #message field only.
By default tcp put everything to message field if json codec not specified.
An workaround to _jsonparsefailure of the message field after we specify the json codec also can be rectified by doing the following:
input {
tcp {
port => '9563'
}
}
filter{
json{
source => "message"
target => "myroot"
}
json{
source => "myroot"
}
}
output {
elasticsearch {
hosts => [ "localhost:9200" ]
}
}
It will parse message field to proper json string to field myroot
and then myroot is parsed to yield the json.
We can remove the redundant field like message as
filter {
json {
source => "message"
remove_field => ["message"]
}
}
Try with this one:
filter {
json {
source => "message"
target => "jsoncontent" # with multiple layers structure
}
}

Logstash Json filter behaving unexpectedly for large nested JSONs

This is my logstash config:
input {
redis { host => "127.0.0.1" data_type => "list" key => "logstash" }
}
filter {
json {
source => 'appData'
}
}
output {
elasticsearch { host => '127.0.0.1' }
}
Things work fine is appData is a simple JSON, but the elasticsearch starts giving parsing errors when large nested JSONs are passed into appData.
Is there an alternative to the JSON filter ? or is there somewhere I can see the debug logs to pin point the problem.