Logstash - reading only appended data in file - elasticsearch

I'm learning how to use logstash and I'm facing some problems in reading a file with logstash which is constantly updated. Here is my test:
logstash.conf
input {
file {
path => ["/usr/share/logs_data/first_log_test.log"]
start_position => "beginning"
}
}
filter {
grok {
match => ["message", "(?<execution_date>\d{4}-\d{2}-\d{2}) (?<execution_time>\d{2}:\d{2}:\d{2})%{GREEDYDATA}ParaBrutos/configs/pipelines/(?<crawler_category>([^/])+)/(?<crawler_subcategory>([^-])+)-(?<crawler_name>([^.])+).json"]
}
}
output {
elasticsearch {
hosts => "elasticsearch:9200"
user => "elastic"
password => "changeme"
ecs_compatibility => disabled
index => "logs_second"
}
}
First, I use this repo's code, for installing ELK stack in a dockerized way.
I started with this test log file empty, and then added few lines that maches the pattern, one by one, in a text editor, simultaneously seeing the index patterns in kibana updating. However, each new row I add in this test log is not added alone, and I see hits of old entries in kibana index patterns.
What happens?
Could it be anything related to not selecting time filters in index pattern creation?
Or something related to since_db settings? But what? Because by default, isn't it supposed to save the last read position of the file?
Or something related to start_position? Even though, by the plugin documentation, it is supposed to make effect only when the first read of the file is done?
I'm a bit lost, tried lots of things and still am not understanding well what's happening. Could you help me?

If you are using a text editor then you are probably creating a new file each time you exit it.
That could be an inode reuse issue. There are links to various issues in the META issue 211. Especially see 251.
Tracking which files have been read when those files can get rotated is an extremely hard problem. Way harder than most folks would initially think. A good option to get it right is to checksum the file contents (although this is not foolproof). The file input does not do that, because it can get ridiculously expensive. Instead it implements a very cheap technique that almost always gets it right (but in a few cases it decides it has already read a file that it has not read).
There are other cases where it gets it wrong by duplicating data (which is what you are hitting). As I said, it is a really hard problem.

Related

How to store a part of log into a file using LogStash

I'm processing a log file with the help of logstash aggregate filter with grok having multiple patterns.
Now while processing the logs I want to extract a part of the log with some regex and store it into a file.
For example, let's say my log is :
id:0422 time:[2013-11-19 02:34:58] level:INFO text:(Lorem Ipsum is simply dummy text of the printing and typesetting industry)
In this log the text will be different at every time.
I have a regex with help of it I can match a part of text that can occure in logstash
So if I find something in that text with help of that regex while logstash indexing into elastic I want to store it into some file or something
Is it possible to achieve this?
There are different solutions for this:
create a filter using ruby code that will be triggered to write in a specific format when you have all the event data together
create a separate output which will be triggered based on an if statement to a file, this will be the preferred way of working as it is clear that it is an output.
Depending on the fact if you want to send all data or not, or have it look different or not you might need to use the clone function in order to clone the event into two different ones which can be manipulated apart from each other using tags.

Kibana (elasticsearch visualization) - how add plot based on sub-string of field?

I have a field in my logs called json_path containing data like /nfs/abc/123/subdir/blah.json and I want to create count plot on part of the string abc here, so the third chunk using the token /. I have tried all sorts of online answers, but they're all partial answers (nothing I can easily understand how to use or integrate). I've tried running POST/GET queries in the Console, which all failed due to syntax errors I couldn't manage to debug (they were complaining about newline control chars, when there were none that I could obviously see or see in a text editor explicitly showing control-characters). I also tried Management -> Index Patterns -> Scripted Field but after adding my code there, basically the whole Kibana crashed (stopped working temporarily) until I removed that Scripted Field.
All this elasticsearch and kibana stuff is annoyingly difficult, all the docs expect you to be an expert in their tool, rather than just an engineer needing to visualize some data.
I don't really want to add a new data field in my log-generation code, because then all my old logs will be unsupported (which have the relevant data, it just needs that bit of string processing before data viz). I know I could probably back-annotate the old logs, but the whole Kibana/elasticsearch experience is just frustrating and I don't use it enough to justify learning such detailed procedures (I actually learned a bunch of this stuff a year ago, and then promptly forgot it due to lack of use).
You cannot plot on a sub string of a field unless you extract that sub string into a new field. I can understand the frustration in learning a new product but to be able to achieve what you want you need to have that sub string value in a new field. Scripted fields are generally used to modify a field. To be able to extract sub string from a field I’d recommend using Ingest Node processor like grok processor. This will add a new field which you can use to plot in Kibana visualizations..

Simple ruby filter in Logstash

I kept running into a wall trying to get a ruby file to work for a filter and I finally simplified my config file to simply this:
filter {
ruby {
code => "event.cancel"
}
}
But even with this simple example, running logstash.bat -t gives me the following:
The given configuration is invalid. Reason: uninitialized constant LogStashCompilerLSCLGrammar::LogStash::Compiler::LSCL::AST::Hash::ConfigurationError
Can anyone point me in the right direction to get a ruby filter to work at all? Same thing happening with fresh install of 6.2.2.
Well I should have gone further and simplified even more. I still received the error even after removing then entire ruby { } section which meant my issue was elsewhere. Sorry to waste viewing time. I found my issue with another filters add_field not working correctly. Lesson learned: Make one change at a time and test...

Logstash drop lines in file

I am wondering if it is possible for logstash to ignore first 2 lines of a file? I have looked in many places and the only solution seems to be using an if to check if the line is certain text, and if so drop .. but this seems extremely inefficient as I know for a fact I only need to drop first 2 lines and dont need to "if..then" check millions or even thousands of lines that follow.
Thanks.
The simple answer is no, not with the existing versions. The logstash file input only has an option for start_postition => beginning.
If you feel strongly about it, you could always fork the file input, update it with a start_position => skip_lines, add another parameter to specify how many lines, and then submit a pull request back to Elastic and it might get implemented.

How to read data in logs using logstash?

I have just started log stash, i have log files in that log file whole object is printed in the logs, Since my object is huge i cant write the grok patterns to the whole object and also i expecting only two values out of those object. Can you please let us know how can i get that?
my logs files looks like below
2015-06-10 13:02:57,903 your done OBJ[name:test;loc:blr;country:india,acc:test#abe.com]
This is just an example my object has lot attributes in int , in those object i need to get only name and acc.
Regards
Mohan.
You can use the following pattern for the same
%{GREEDYDATA}\[name:%{WORD:name};%{GREEDYDATA},acc:%{NOTSPACE:account}\]
GREEDYDATA us defined as follows -
GREEDYDATA .*
The key lie in understanding greedydata macro.
It eats up all possible characters as possible.
Logstash patterns don't have to match the entire line. You could also pull the leading information off (date, time, etc) in one grok{} and then use a different grok{} to pull off just the two fields that you want.

Resources