logstash apache2 not getting converted to json - json

A simple logstash example is not working for me. I want to read my apache access.log and dump it out. I use the following configuration file
input {
file {
path => "/var/log/apache2/access.log"
start_position => beginning
}
}
filter {
grok {
match => { "message" => "%{COMBINEDAPACHELOG}"}
}
}
output {
stdout {}
}
and I cannot get json output from logstash as shown here.
root#rick-VirtualBox:/opt/logstash# ./bin/logstash -f /home/rick/log_conf/first-pipeline.conf
Logstash startup completed
2015-08-10T18:58:07.660Z rick-VirtualBox 192.168.56.1 - - [10/Aug/2015:12:46:21 -0400] "GET / HTTP/1.1" 200 427 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_3) AppleWebKit/600.6.3 (KHTML, like Gecko) Version/8.0.6 Safari/600.6.3"
When i use the grokdebugger on the line above it says its a COMBINEDAPACHELOG pattern, which i believe is what i am asking for.
Isn't logStash suppose to create json?

Actually json is not the default codec. According to the docs Logstash's default output codec for stdout is line.
Try:
output {
stdout { codec => json }
}
You might also consider using codec => rubydebug which also produces a very clean output.

Related

How to get all contents in a json file sent to elasticsearch using logstash

I have test results that are being stored in json files. I then have logstash locate the file and attempt to send all of the lines to elasticsearch. Only about half of the lines are being sent and can't figure out why certain lines are being left out. For example, there will be 34 lines, but only 14 are sent.
input {
file {
path => "/data/*.json"
start_position => "beginning"
}
}
# ----------------------------------------------------------------------
filter {
# Parse fields out of JSON message, then remove the raw JSON.
json {
source => "message"
}
}
# ----------------------------------------------------------------------
output {
elasticsearch {
hosts => ["host:9200", "localhost:9200"]
index => "ct-%{+YYYY.MM.dd}"
}
stdout { codec => rubydebug }
I'm not sure if there is something within the json itself that causes logstash to just skip over it, or if there's something wrong with my logstash.conf file that I posted above.
Logstash computes files from different types to send it to elasticsearch in Json format. In your case, a Filebeat agent with an elasticsearch output would be enough to send a json file to ES and to index it.
It would look like this using Filebeat 6.x :
#=========================== Filebeat inputs =============================
filebeat.inputs:
- type: log
# Paths to the logs
paths:
- "/your/path/to/your/logs/file.json"
# tags to identify the logs source, .gz files excluded from the prospector
tags: ["beats","yourtag"]
exclude_files: ['\.gz$']
#================================ Outputs =====================================
#----------------------------- Elasticsearch output --------------------------------
output.elasticsearch:
# The ES host & index name
hosts: ["yourEShost:9200"]
index: "ct-%{+YYYY.MM.dd}"

Parsing error "_grokparsefailure" in LogStash

At first I displayed the logs in Kibana from the syslog and it worked fine. I set it up according to the documentation.
Now I've changed the source of the logs, now it retrieves logs from my web application and although Kibana still displays them kind of correctly, now there're the Tags "_grokparsefailure" which means that there's an error in parsing the logs.
The current filter I have:
filter {
if [type] == "syslog" {
grok {
match => { "message" => "%{SYSLOGTIMESTAMP:syslog_timestamp} %{SYSLOGHOST:syslog_hostname} %{DATA:syslog_program}(?:\[%{POSINT:syslog_pid}\])?: %{GREEDYDATA:syslog_message}" }
add_field => [ "received_at", "%{#timestamp}" ]
add_field => [ "received_from", "%{host}" ]
}
syslog_pri { }
date {
match => [ "syslog_timestamp", "MMM d HH:mm:ss", "MMM dd HH:mm:ss" ]
}
}
}
How can I find out where the parsing error is? Is there an online service which would help me create a grok pattern for my new logs? Any other advice?
UPDATE: the logs are in json.
In response to OP's:
Is there an online service which would help me create a grok pattern for my new logs?
My favorite tool for testing grok patterns is:
http://grokconstructor.appspot.com/do/match
But I know some prefer: https://grokdebug.herokuapp.com/
Your logs probably aren't parsing properly because you're using the syslog pattern on logs that aren't in the syslog format.
EDIT: For json log parsing you may want to look at either the json filter or the json codec
You can debug your logs in Kibana
GoTo: Kibana -> Managenement -> Dev Tools -> Grok Debugger

logstash : http input takes only first line (with csv filter)

i'm newbie to elk stack and trying to monitor logs send through http. I have below logstash configuration. But it only read and send first line to elastic search although I send multiple lines in my http POST request body (Im using chromes DHC plugin to send http request to logstash). Please help me to read full data and send them to elastic search.
input {
http {
host => "127.0.0.1" # default: 0.0.0.0
port => 8081 # default: 8080
threads => 10
}
}
filter {
csv {
separator => ","
columns => ["posTimestamp","posCode","logLevel","location","errCode","errDesc","detail"]
}
date {
match => ["posTimestamp", "ISO8601"]
}
mutate {
strip => ["posCode", "logLevel", "location", "errCode", "errDesc" ]
remove_field => [ "path", "message", "headers" ]
}
}
output {
elasticsearch {
protocol => "http"
host => "localhost"
index => "temp"
}
stdout {
codec => rubydebug
}
}
Sample data:
2015-08-24T05:21:40.468,352701060205140,ERROR,Colombo,ERR_01,INVALID_CARD,Testing POS errors
2015-08-24T05:21:41.468,352701060205140,ERROR,Colombo,ERR_01,INVALID_CARD,Testing POS errors
2015-08-24T05:23:40.468,81021320,ERROR,Colombo,ERR_01,INVALID_CARD,Testing POS errors
2015-08-25T05:23:50.468,352701060205140,ERROR,Colombo,ERR_02,TIME_OUT,Testing POS errors
Managed to solve this by adding split filter.
split {
}

Import JSON Files into Logstash + Elasticsearch + Kibana

So, I have a web platform that prints a JSON file per request containing some log data about that request. I can configure several rules about when should it log stuff, only at certain levels, etc...
Now, I've been toying with the Logstash + Elasticsearch + Kibana3 stack, and I'd love to find a way to see those logs in Kibana. My question is, is there a way to make Logstash import these kind of files, or would I have to write a custom input plugin for it? I've searched around and for what I've seen, plugins are written in Ruby, a language I don't have experience with.
Logstash is a very good tool for processing dynamic files.
Here is the way to import your json file into elasticsearch using logstash:
configuration file:
input
{
file
{
path => ["/path/to/json/file"]
start_position => "beginning"
sincedb_path => "/dev/null"
exclude => "*.gz"
}
}
filter
{
mutate
{
replace => [ "message", "%{message}" ]
gsub => [ 'message','\n','']
}
if [message] =~ /^{.*}$/
{
json { source => message }
}
}
output
{
elasticsearch {
protocol => "http"
codec => json
host => "localhost"
index => "json"
embedded => true
}
stdout { codec => rubydebug }
}
example of json file:
{"foo":"bar", "bar": "foo"}
{"hello":"world", "goodnight": "moon"}
Note the json need to be in one line. if you want to parse a multiline json file, replace relevant fields in your configuration file:
input
{
file
{
codec => multiline
{
pattern => '^\{'
negate => true
what => previous
}
path => ["/opt/mount/ELK/json/*.json"]
start_position => "beginning"
sincedb_path => "/dev/null"
exclude => "*.gz"
}
}
filter
{
mutate
{
replace => [ "message", "%{message}}" ]
gsub => [ 'message','\n','']
}
if [message] =~ /^{.*}$/
{
json { source => message }
}
}
Logstash is just a tool for converting various kinds of syslog files into JSON and loading them into elasticsearch (or graphite, or... ).
Since your files are already in JSON, you don't need logstash. You can upload them directly into elasticsearch using curl.
See Import/Index a JSON file into Elasticsearch
However, in order to work well with Kibana, your JSON files need to be at a minimum.
Flat - Kibana does not grok nested JSON structs. You need a simple hash of key/value pairs.
Have a identifiable timestamp.
What I would suggest is looking the JSON files logstash outputs and seeing if you can massage your JSON files to match that structure. You can do this in any language you
like that supports JSON. The program jq is very handy for filtering json from one format to another.
Logstash format - https://gist.github.com/jordansissel/2996677
jq - http://stedolan.github.io/jq/
Logstash can import different formats and sources as it provides a lot of plugins. There are also other log collector and forwarder tools that can send logs to logstash such as nxlog, rsyslog, syslog-ng, flume, kafka, fluentd, etc. From what I've heard most people use nxlog on windows (though it works on linux equally well) in combination with the ELK stack because of its low resource footprint. (Disclaimer: I'm affiliated with the project)

json filter fails with >#<NoMethodError: undefined method `[]' for nil:NilClass>

I'm trying to process entries from a logfile that contains both plain messages and json formatted messages. My initial idea was to grep for messages enclosed in curly braces and have them processed by another chained filter. Grep works fine (as does plain message processing), but the subsequent json filter reports an exception. I attached the logstash configuration, input and error message below.
Do you have any ideas what the problem might be? Any alternative suggestions for processing plain and json formatted entries from the same file?
Thanks a lot,
Johannes
Error message:
Trouble parsing json {:key=>"#message", :raw=>"{\"time\":\"14.08.2013 10:16:31:799\",\"level\":\"DEBUG\",\"thread\":\"main\",\"clazz\":\"org.springframework.beans.factory.support.DefaultListableBeanFactory\",\"line\":\"214\",\"msg\":\"Returning cached instance of singleton bean 'org.apache.activemq.xbean.XBeanBrokerService#0'\"}", :exception=>#<NoMethodError: undefined method `[]' for nil:NilClass>, :level=>:warn}
logstash conf:
file {
path => [ "plain.log" ]
type => "plainlog"
format => "plain"
}
}
filter {
# Grep json formatted messages and send them to following json filter
grep {
type => "plainlog"
add_tag => [ "grepped_json" ]
match => [ "#message", "^{.*}" ]
}
json {
tags => [ "grepped_json" ]
source => "#message"
}
}
output {
stdout { debug => true debug_format => "json"}
elasticsearch { embedded => true }
}
Input from logfile (just one line):
{"time":"14.08.2013 10:16:31:799","level":"DEBUG","thread":"main","clazz":"org.springframework.beans.factory.support.DefaultListableBeanFactory","line":"214","msg":"Returning cached instance of singleton bean 'org.apache.activemq.xbean.XBeanBrokerService#0'"}
I had the same problem and solved it by adding a target to the json filter.
The documentation does say the target is optional but apparently it isn't.
Changing your example you should have:
json {
tags => [ "grepped_json" ]
source => "#message"
target => "data"
}