match logstash correct date format - date

I have some logs files with the following timestamp format :
2014-04-22 16:08:22,455
I would like to know which is the correct config filter to parse it.
I have the following pattern:
DATE (\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2},\d{3})
This is my grok filter:
grok {
patterns_dir => "./patterns"
match => ["message", "%{DATE:date}"]
}
But then I don't know what to put in the filter date, I know that it's not
date {
match => ["date","YYYY-MM-dd HH:mm:ss"]
}
Thanks in advance for your help.

If your grok works correctly (e.g. you get the "date" field with the contents of your log date correctly groked (parsed) in the output, then this should work:
date {
match => [ "date" , "yyyy-MM-dd HH:mm:ss,SSS" ]
}

Related

How to grok catalina log file

I'm trying to find a pattern for this line of log (extracted from catalina.log) of an apache tomcat 8 installation.
30-Apr-2019 15:40:40.044 INFOS [main] org.apache.catalina.startup.VersionLoggerListener.log message
No one of the date pattern include in logstash matches with this date format.
Do you have idea how can I parse this date 30-Apr-2019 15:40:40.044 to a timestamp in my logstash filter ?
Thanks
As stated by #baudsp, you may add the date pattern for catalina using a custom pattern file, or use it embedded in the grok, as shown here
(?<date>%{MONTHDAY}-%{MONTH}-%{YEAR} %{HOUR}:?%{MINUTE}(?::?%{SECOND}))
If you use the pattern often, put it in a file would probably be better, and provide more readability
Finally, there is a solution :
I put a new pattern in a file custom.txt
MY_DATE_PATTERN %{MONTHDAY}-%{MONTH}-%{YEAR} %{HOUR}:?%{MINUTE}(?::?%{SECOND})
Then in my logstash.conf I put this filter :
grok {
patterns_dir => ["./patterns"]
match => {
"message" => "%{MY_DATE_PATTERN:timestamp}%{SPACE}%{GREEDYDATA:loglevel}%{SPACE}\[%{GREEDYDATA:thread}\]%{SPACE}%{JAVACLASS:classname}%{SPACE}%{GREEDYDATA:logmessage}"
}
}
date {
match => [ "timestamp" , "dd-MMM-yyyy HH:mm:ss.SSS" ]
}
Thanks for your help

Elastic(search): Query result date format differs from stored format

got a problem with the elastic date format conversion when I parse the results from a query. So i have a default mapping on a date field as following:
"timestamp" : {
"type" : "date",
"format" : "dateOptionalTime"
}
and it is stored as "timestamp":"2015-05-06T08:52:56.387Z"
if I execute a max aggregation on that field I get a long value:
"timestamp_max": {
"value": 1430902071110
}
however I want the value be the same as it is stored. I read that one can specify the format in the aggregation but its not working. I tried:
"aggregations":{
"timestamp_max":{
"max":{
"field":"timestamp",
"format" : "dateOptionalTime"
}
}
}
but this gives a SearchParseException ... SearchParseException[[logstash-2015.05.07][0]: query[ConstantScore(BooleanFilter(+no_cache(timestamp:[1429357190515 TO 1431949190515])))],from[-1],size[-1]: Parse Failure [Unexpected token VALUE_STRING in [timestamp_max].]]; ...
What am I doing wrong?
Best regards,
Jan
You're almost there. You just need to specify the date format using the correct formatting pattern like this:
"aggregations":{
"timestamp_max":{
"max":{
"field":"timestamp",
"format" : "yyyy-MM-dd'T'HH:mm:ss.SSSZ"
}
}
}
Please note that this is only working from ES 1.5.0 onwards. See the related issue on the ES github.

How to Insert Date (as Date DataType) In Mongo from 'Mongo Command Prompt' & From 'mongoimport'

MongoDB shell version: 2.0.4
Want to insert date as 'date datatype' from mongo command line.
db.my_date_collection.insert(
{"user_id" : 91,
"event_timestamp" :new Date("09-AUG-12 05.30.28.402000 AM")
});
Above query add record but change date from "09-AUG-12 05.30.28.402000 AM" to "1970-01-01T00:00:00Z"
> db.my_date_collection.findOne()
{
"_id" : ObjectId("54168ddc289a635d725d86fb"),
"user_id" : 91,
"event_timestamp" : ISODate("1970-01-01T00:00:00Z")
}
Plus it also didn't save 'date data' as date 'datatype'
typeof db.my_date_collection.findOne().event_timestamp;
object
Can someone help me insert data as date type from mongo prompt?
Additionally can some one tell me how to insert the 'date as date datatype type' from a tsv file? as i got the same issue in loading tsv from mongoimport.
Thanks
The date is not in a valid format to be parsed by the date constructor. Try the equivalent value in a valid format (I'm assuming UTC time since you didn't include a timezone):
"09-AUG-12 05.30.28.402000 AM" => "2012-08-09T05:30:28.402"
> var my_date = ISODate("2012-08-09T05:30:28.402")
> db.my_dates.insert({ "x" : my_date })
> var doc = db.my_dates.findOne()
> doc.x instanceof Date
true
The return value is of Date type, but Javascript isn't object oriented so the meaning of that and how to determine if something is of Date type are different than you are expecting, apparently. Use instanceof to check is something is of a certain type or "inherits" from that type.
Your code is good. Take a look at typeof doc. It can return more generic type. Use instanceof to check a type:
db.my_date_collection.findOne().event_timestamp instanceof Date

logstash mongodb output and ISODate type

I have some troubles trying to convert a date type field into mongoDB format (ISODate).
I have a RabbitMQ queue with JSON messages inside. These messages have a Date property like this :
Date : "2014-05-01T14:53:34.25677Z"
My logstash service read the RabbitMQ queue and inject messages into mongoDB.
Here is my logstash config file :
input {
rabbitmq {
...
codec => json
}
}
output {
mongodb {
codec => json
collection => "log"
isodate => true
database => "Test"
uri => "mongodb://localhost:27017"
}
}
My problem is that my Date property is insterted as string instead as Date. How can I do to tell Logstash to insert my Date field as an ISODate field into mongoDB?
Thank you
You should use a logstash Date filter to convert the string into a Date prior to inserting it into MongoDB: http://logstash.net/docs/1.4.2/filters/date
Don't know your full schema but it should looking something like this:
filter {
date {
match => [ "Date", "ISO8601" ]
}
}
Note the use of "ISO8601" - that appears to match the format you are receiving but you may need to play around a bit with it. As you test this I'd strongly suggest using the stdout output option for test runs to easily see what's getting done prior to insertion into MongoDB:
output {
stdout { codec => rubydebug }
}

How do I make Perl Mongo find() return the records where ts is less than 5 days old?

I have a MongoDB collection called Clickstream and want to find all documents matching a specific pubCode with a timestamp in the last 5 days.
The timestamp is in a field called ts, with a Mongo data type of Date.
From the Mongo shell I can execute the following:-
db.Clickstream.find({
pubCode : "w_abc123",
ts: {$gte: ISODate("2014-02-28T00:00:00Z")}
})
However, I can't seem to do the same thing in Perl (using today's date, rather than a hardcoded date & time). If I do this:-
my $now = DateTime->now;
my $n_days_ago = $now->add( days => -5 );
... etc ...
my $ptr = $ptrClickstream->find(
{
pubCode => "$pubCode",
ts => { "\$gte" => ISODate("$n_days_ago") }
},
{ hitType => 1 }
);
I get the following error message:-
Undefined subroutine &main::ISODate called at recalcPubPop.pm line 25.
And if I remove the reference to ISOData() like this
my $ptr = $ptrClickstream->find({
pubCode => "$pubCode", ts => { "\$gte" => "$n_days_ago" }
});
I get no documents returned.
Any suggestions on how to make this find() return the records where ts is less than 5 days old?
Thanks!
Use DateTime naturally. Just explaining for entirety.
So where you see that ISODate form from within the mongo shell, that is actually just a native representation for the shell's JavaScript environment of the underlying BSON Date that is really being stored.
As such, the Perl driver "inflates" these values as DateTime objects when you retrieve them, and in a similar manner handles the conversion to the BSON Date when you pass in the values.
So you already did the DateTime math part in your code:
my $now = DateTime->now;
my $n_days_ago = $now->add( days => -5 );
For reference ->add works "in-place" and you seem to want the "start" of the day so we can truncate.
Try and force "UTC" dates, which is what they will be in the collection That is just for completeness as they generally should be that way. You may find this form of coding your date a bit cleaner:
my $then = DateTime->now( time_zone => 'UTC' )
->truncate( to => 'day' )->add( days => -5 );
And your second query form was generally right, but your real problem was you we're coercing to string by interpolating as in "$n_days_ago", so you didn't have the object but the string. See the form as I have amended and don't try to interpolate unless you really mean to:
my $ptr = $ptrClickstream->find({
pubCode => $pubCode, ts => { '$gte' => $then }
});
So all of this works for me. If that does not return a result check your vars that their values are definitely what you expect.
All of the driver implementations handle native "Date Objects" for their language in this way.
For more information, see the driver section on Dates.