Extracting fields in Logstash - logstash

I am using Logstash (with Kibana as the UI). I would like to extract some fields from my logs so that I can filter by them on the LHS of the UI.
A sample line from my log looks like this:
2013-07-04 00:27:16.341 -0700 [Comp40_db40_3720_18_25] client_login=C-316fff97-5a19-44f1-9d87-003ae0e36ac9 ip_address=192.168.4.1
In my logstash conf file, I put this:
filter {
grok {
type => "mylog"
pattern => "(?<CLIENT_NAME>Comp\d+_db\d+_\d+_\d+_\d+)"
}
}
Ideally, I would like to extract Comp40_db40_3720_18_25 (the number of digits can vary, but will always be at least 1 in each section separated by _) and client_login (can also be client_logout). Then, I can search for CLIENT_NAME=Comp40... CLIENT_NAME=Comp55, etc.
Am I missing something in my config to make this a field that I can use in Kibana?
Thanks!

If you are having any difficulty getting the pattern to match correctly, using the Grok Debugger is a great solution.
For your given problem you could just separate out your search data into another variable, and save the additional varying digits in another (trash) variable.
For example:
(?<SEARCH_FIELD>Comp\d+)%{GREEDYDATA:trash_variable}]
(Please use the Grok Debugger on the above pattern)

Related

logstash filter , unfiltered lines

I am new to Logstash filter and going through different blogs and links to understand in detail. I have few questions which are still unanswered.
. If my log file has different log pattern e.g.
2017-01-30 14:30:58 INFO ThreadName:33 - {"t":1485786658088,"h":"abcd1234", "l":"INFO", "cN":"org.logstash.demo", "mN":"getNextvalue", "m":"fetching next value"}
2017-01-30 14:30:58 INFO AnotherThread:33 -my log pattern is different
I have below filter which is successfully filtering line 1 of the log
grok
{
match => [ "message", "%{TIMESTAMP_ISO8601:LogDate} %{LOGLEVEL:loglevel} %{WORD:threadName}:%{NUMBER:ThreadID} - %{GREEDYDATA:Line}" ]
}
json
{
source => "Line"
}
what will happen with the lines which can not be filtered using filter pattern?
Is there any way to capture all the lines which were not filtered and send to elasticSearch ?
Is there any good reading material where I can read about Input, Filter, Output plugins with the examples ?
To answer your questions:
The lines which cannot be filtered using grok would end up in a
grok_parsefailure. Make sure you handle it by dropping the lines
which don't actually match the filter criteria.
As far as I know you can't capture them separately and push it to ES. Maybe for this, you can have multiple grok patterns so that you can filter it out and send it to different ES indices thereafter.
I've added the links in the comment above.
This SO could come in handy. Hope it helps!
As #darth_vader points out, you'll get a "grok_parsefailure" tag on each document that doesn't match your pattern(s) in a grok{} filter. However, how you handle this failure is up to you.
By default, all the events will fall through to your output{} section, which presumably would send them to elasticsearch. You could also have a conditional output{} section, which sent parsed logs to one output and unparsed logs to another (a file{} output, or a different index, or...).
As for examples, the official doc tends to include incomplete fragments (at best), so you're probably going to find better examples in random internet blogs.

GROK Pattern filtering

Hi I am new to logstash and grok filtering, I have a sample log like this:
1/11/2017 12:00:17 AM :
Error thrown is:
No Error
Request sent is:
webMethod:GetOSSUpdatedOrderHeader|appCode:OSS|regionCode:EMEA|orderKeyList:|lastModifedDateTime:1/10/2017 11:59:13 PM|
I want to filter out the line separator which is a line full of ** (the last line)
Also when I want to be able to capture entire line including ":" in one field. For example in the above log, webMethod:GetOSSUpdatedOrderHeader has to be captured in one field in my grok pattern. Is there a way to achieve this?? TIA. Please refer the attached image for the sample log message
A few tips:
Photos of logs are not a good way to offer someone an example, copy and paste the log
The Grok Debugger is a great way of building your own grok patterns
This should work for the sample log line you pasted in:
%{NOTSPACE:webMethod}\|%{NOTSPACE:appCode}\|%{NOTSPACE:regionCode}\|%{NOTSPACE:orderKeyList}\|%{NOTSPACE:lastModifedDateTime}
However, what you requested, probably isn't quite what you want, as you just want the field content in the result, not the name of the field as well. This should give you more sensible results:
webMethod:%{NOTSPACE:webMethod}\|appCode:%{NOTSPACE:appCode}\|regionCode:%{NOTSPACE:regionCode}\|orderKeyList:(?:%{NOTSPACE:orderKeyList}|)\|lastModifedDateTime:%{NOTSPACE:lastModifedDateTime}
You would then want to process the lastModifedDateTime field with the date filter to get the date stamp in a format logstash can save to.

Store Multiple Identical Results from Logstash

So, say I have an event coming into Logstash as a multiline object (there are many events that all basically match the pattern below):
Starting script at 2015-11-12 15:06 EST
Found result a at 127.0.0.1
Found result b at 127.0.0.1
Found result c at 0.0.0.0
Script ended at 2015-11-12 15:07 EST
How would I go about matching this in such a way as to store each of the "Found ..." lines separately?
My current config file is something like:
filter {
grok {
break_on_match => false
match => {
"message" => [
"Starting script at ${TIMESTAMP_ISO8601:run_time}",
"Found result %{GREEDYDATA:result} at ${IP:result_ip}"
]
}
}
}
As it stands, this only captures one of the "Found result..." lines. (That is, it matches them all, but only stores one of them - there's only one result variable output.) I'd like to individually capture them, and store them as an... well, anything, so long as they're all there.
Is there a way to capture multiple of the same pattern and store all of the resultant capture data distinctly, while keeping the whole multiline event together so that I can tie it to header data such as the script start time?
I think you can use the split filter to achieve what you want. It allows you to split one event into several parts. The indivdual parts are all copies of the original event as far as I remember. You have to play with the terminator parameter which controls when the message is split into parts.
Check out the docs at: https://www.elastic.co/guide/en/logstash/current/plugins-filters-split.html#plugins-filters-split-target

Handling different log formats in the same file

I have a single log file that contains differing output formats.
For example:
line 1 = 2015-01-1 12:04:56 INFO 192.168.0.1 my_user someone logged in
line 2 = 2015-01-1 12:04:56 WARN [webserver-thread] (MyClass.java:66) user authenticated
Whilst the real solution is to either split them into separate files or unify the formats is it possible to grok differing log formats with Logstash?
My first recommendation is to run one grok{} to strip off the common stuff - the datetime and log level. You can put the remaining stuff back into the [message] field:
%{TIMESTAMP_ISO8601} %{WORD:level} %{GREEDYDATA:message}
Make sure to use the 'overwrite' parameter in grok{}.
Then if you want to parse the remaining information, your (multiple) regexps will be running against a shorter string, which should make them more efficient.
You can then have multiple patterns:
grok {
match => [
"message", "PATTERN1",
"message", "PATTERN2"
]
}
By default, grok will stop processing when it hits the first match.

grok pattern for extracting info in logstash

I am using the grok pattern to extract some data from file path, but it does not seem to work right
path: /home/shard/logstash/test/12/23/abc_132.log
pattern: %{GREEDYDATA}/%{INT:group}/%{INT:id}/%{DATA:job_type}(_%{UUID:uuid})*\.log
I want to extract 132 as the uuid field and it works ok when tested in grok debugger [http://grokdebug.herokuapp.com/] but when applied in logstash indexer, it fetches all of abc_132 under job_type field.
What may be the issue here and how can I extract uuid (perhaps a different regex?).
You can try to get the uuid from the job_type by using the ruby filter
ruby {
code => "event['uuid'] = event['job_type'].split('_')[1]"
}
Hope this can help you.

Resources