ELK - Filtering data with Logstash - elasticsearch

I am experimenting with ELK stack, and so far so good. I have small issue that I am trying to resolve.
I have a field named 'message' coming from filebeat. Inside that field is a string with data for logging.
Sometimes that message field might contain this line:
successfully saved with IP address: [142.93.111.8] user: [testuser#some.com]
I would like to apply a filter, so the logstash send this to the Elastic Search:
successfully saved with IP address: [] user: [testuser#some.com]
This is what I currently have in Logstash configuration:
input {
beats {
port => "5043"
codec => json
}
}
filter {
if [message] =~ /IP address:/{
mutate { add_tag => "whats happening" }
}
}
output {
elasticsearch {
hosts => [ "localhost:9200" ]
}
}
Something else cought my attention. ELK is able to do text filtering on Filebeat level and also on Logstash level. Which one is the most usual scenario? Is Filebeat filtering more suitable?

I have found the correct solution in my case:
mutate {
gsub => ["message", "address: \[(.*?)]", "address:[not indexable]"]
}
Hopefully someone will find it usefull.

Related

How to extract service name from document field in Logstash

I am stuck in middle of ELK- Stack configuration, any lead will be highly appreciated.
Case Study:
I am able to see the logs(parsed through logstash without any filter) but I want to apply filter's while parsing the logs.
For ex:
system.process.cmdline: "C:\example1\example.exe" -displayname "example.run" -servicename "example.run"
I can see the above logs in kibana dashboard but I want only the -servicename keys, value.
Expected output in Kibana, where servicename is an index and example.run will be associate value.
servicename "example.run"
I am newbie in ELK.So, Please help me out...
My environment:
Elasticsearch- 6.6
Kibana- 6.6
Logstash- 6.6
Filebeat- 6.6
Metricbeat- 6.6
Logs coming from- Windows server 2016
input {
beats {
port => "5044"
}
}
filter {
grok{
match =>{"message" => "%{NOSPACE:hostname} "}
}
}
output {
file {
path => "/var/log/logstash/out.log"
}
}
I have tried with the above logstash pipeline. But i am not successfull in getting the required result. Assuming i have to add more lines in filter but don't know what exactly.
use this in you filter:
grok{
match => { "message" => "%{GREEDYDATA:ignore}-servicename \"%{DATA:serviceName}\"" }
}
your service name should be now in serviceName key

Duplicate field values for grok-parsed data

I have a filebeat that captures logs from uwsgi application running in docker. The data is sent to the logstash which parses it and forwards to elasticsearch.
Here is the logstash conf file:
input {
beats {
port => 5044
}
}
filter {
grok {
match => { "log" => "\[pid: %{NUMBER:worker.pid}\] %{IP:request.ip} \{%{NUMBER:request.vars} vars in %{NUMBER:request.size} bytes} \[%{HTTPDATE:timestamp}] %{URIPROTO:request.method} %{URIPATH:request.endpoint}%{URIPARAM:request.params}? => generated %{NUMBER:response.size} bytes in %{NUMBER:response.time} msecs(?: via sendfile\(\))? \(HTTP/%{NUMBER:request.http_version} %{NUMBER:response.code}\) %{NUMBER:headers} headers in %{NUMBER:response.size} bytes \(%{NUMBER:worker.switches} switches on core %{NUMBER:worker.core}\)" }
}
date {
# 29/Oct/2018:06:50:38 +0700
match => [ "timestamp" , "dd/MMM/yyyy:HH:mm:ss Z"]
}
kv {
source => "request.params"
field_split => "&?"
target => "request.query"
}
}
output {
elasticsearch {
hosts => ["http://localhost:9200"]
index => "test-index"
}
}
Everything was fine, but I've noticed that all values captured by the grok pattern is duplicated. Here is how it looks in kibana:
Note that the raw data like log which wasn't grok output is fine. I've seen that kv filter has allow_duplicate_values parameter, but it doesn't apply to grok.
What is wrong with my configuration? Also, is it possible to rerun grok patterns on existing data in elasticsearch?
Maybe your filebeat is already doing the job and creating these fields
Did you try to add this parameter to your grok ?
overwrite => [ "request.ip", "request.endpoint", ... ]
In order to rerun grok on already indexed data you need to use elasticsearch input plugin in order to read data from ES and re-index it after grok.

How to generate reports on existing dump of logs using ELK?

Using ELK stack, is it possible to generate reports on existing dump of logs?
For example:
I have some 2 GB of Apache access logs and I want to have the dashboard reports showing:
All requests, with status code 400
All requests, with pattern like "GET http://example.com/abc/.*"
Appreciate, any example links.
Yes, it is possible. You should:
Install and setup the ELK stack.
Install filebeat, configure it to harvest your logs, and to forward the data to logstash.
In logstash, listen to filebeat input, use the grok to process/break up your data, and forward it to elastichsearch something like:
input {
beats {
port => 5044
}
}
filter {
grok {
match => { "message" => "%{COMMONAPACHELOG}" }
}
}
output {
elasticsearch {
hosts => ["localhost:9200"]
index => "filebeat-logstash-%{+YYYY.MM.dd}"
}
}
In kibana, setup your indices, and query for data, e.g.
response: 400
verb: GET AND message: "http://example.com/abc/"

How to show a count in world map using IP address in Kibana, Logstash and ElasticSearch?

I want to display the number of users accessing the app in a World Map using ElasicSearch, Kibana and Logstash. I am new this stuff so having a difficult time.
Here is my sample log:
2014-07-16 21:41:04,254 [main] [] [INFO ] [o.a.c.s.f.ReflectionServiceFactoryBean] - Creating Service {http://com/test/matrix/expense}ExpenseService from class com.test.matrix.expense.ExpenseService
And here is my config file:
input {
file{
#log.dir is provided from the application
path => "D:/installDir/log/**/*.log"
start_position=>"beginning"
}
}
filter {
multiline {
pattern => "^%{TIMESTAMP_ISO8601} "
negate => true
what => previous
}
grok {
match => ["message", "%{TIMESTAMP_ISO8601:timestamp} \[%{DATA:module}-%{DATA:instance}-%{GREEDYDATA:thread}\] \[%{DATA:user}\] \[%{DATA:severity}\] \[%{JAVACLASS:javaClassName}\] - %{GREEDYDATA:shortmessage}"]
}
date {
match => ["timestamp", "ISO8601"]
}
}
output {
elasticsearch_http {
host => "SAKHAN6440.corp.out.com"
port => 9201
}
}
first it seems that you have no user identifier in your sample logs ! In order to display the number of users accessing the app on a wordl map you need to have the client IP.
Once you got it, simply add this to your logstash conf :
geoip {
source => "client_ip"
target => "geoip"
fields => ["country_code2"]
database => "your/path/to/db/GeoIP.dat"
}
Where the client_ip is the field containing the IP and GeoIP.dat is the free db downloaded from here.
This will add a geoip.country_code2 that you will be able to add in your Kibana map.
Then you should be able to see the trafic on your app regarding different countries of the world !
Bye

Tagging the Logs by Logstash - Grok - ElasticSearch

Summary:
I am using Logstash - Grok and elastic search and my main aim is to First accept the logs by logstash, parse them by grok and associate tags with the messages depending on the type of the log, and then finally feed it to the Elastic server to query with Kibana.
I have already written this code but am not able to get the tags in Elastic Search.
This is my logstash confif file.
input {
stdin {
type => "stdin-type"
}
}
filter {
grok {
tags => "mytags"
pattern => "I am a %{USERNAME}"
add_tag => "mytag"
named_captures_only => true
}
}
output {
stdout { debug => true debug_format => "json"}
elasticsearch {}
}
Where am I going wrong?
1) I would first start with editing your values to match the data type they represent. For example
add_tag => "mytag"
actually should have an array as it's value, not a simple string. Change that to
add_tag => ["mytag"]
as a good start. Double check all your values and verify they are of the correct type for logstash.
2) You are limiting your grok filters to messages that are already tagged with "mytags" based on the config line
tags => "mytags"
I don't see anywhere where you have added that tag ahead of time. Therefore, none of your messages will even go through your grok filter.
3) Please read the logstash docs carefully. I am rather new to the Logstash/Grok/ES/Kibana etc. world as well, but I have had very similar problems to what you have had, and all of them were solved by paying attention to what the documentation says.
You can run LogStash by hand (You may already be doing this) with /opt/logstash/bin/logstash -f $CONFIG_FILE and can check that your config file is valid with /opt/logstash/bin/logstash -f $CONFIG_FILE --configtest I bet you're already doing that though.
You may need to put your add_tag stanza into an array
grok {
...
add_tag => [ "mytag" ]
}
It could also be that what you're piping into STDIN isn't being matched in the grok pattern. If grok doesn't match is should result in _grokparsefailure being added to your tags. If you see those, it means your grok pattern isn't firing.
A better way to do this may be...
input {
stdin {
type => 'stdin'
}
}
filter {
if [type] = 'stdin' {
mutate {
add_tag => [ "mytag" ]
}
}
}
output {
stdout {
codec => 'rubydebug'
}
}
This will add a "mytag" tag to all things coming from standard in, wether they're groked or not.

Resources