Converting date to UNIX time in Logstash - logstash

Is it possible to convert date from "2016-08-22T09:09:55.487Z" format to UNIX time in Logstash? I have seen the opposite operation, but nothing about it.

First, you'll have to convert "2016-08-22T09:09:55.487Z" to a date object, with the date filter:
(supposing that the field date contains a string representing a valid ISO8601 timestamp)
date {
match => ["date", "ISO8601"]
target => "date_object"
}
At this point you'll have a field date_object containing a logstash timestamp.
This timestamp can be converted to its epoch equivalent with the to_i method.
To do this we'll have to use the ruby filter, which allow to execute ruby code as a filter.
ruby {
code => set.event('date_epoch', event.get('date_object').to_i)"
}
Then you'll have a field date_epoch, which will be a number representing the UNIX time.

I came across a similar issue today. Unfortunately the peace of config above has a limitation that it loses milliseconds from the timestamp during the integer conversion:
ruby {
code => "event['date_epoch'] = event['date_object'].to_i"
}
I’ve tried several options including converting the date object to float, multiplying it by 1000 and then back to string. The bottom line is that the precision was not exactly the same.
Finally I came up with this a bit hacky sample below. It worked with logstash version 2.4.1.
So first I create a field tmpTimestamp in order to convert the parsed timestamp into a plain String:
mutate{
add_field => ["tmpTimestamp","%{#timestamp}"]
}
A peace of ruby code to cast the string into a standard ruby DateTime format, convert it to the epoch format (including ms) and then back to String:
ruby { code => "require 'date';event['epoch'] = DateTime.parse(event['tmpTimestamp']).strftime('%Q').to_s" }
Remove unused tmp variable:
mutate{
remove_field => ["tmpTimestamp"]
}

Related

How can I store String type DateTime string to ISO DateTime Format in MongoDB in NodeJS?

I am receiving a string format DateTime Strings data from the android app.
according to the previous developer's note, it's RFC 3339 format string.
I am stuck in converting this string into MongoDB's Date format in Node.js
in detail edit documents, formats are like below.
[ current string what I receive -> Date format in MongoDB, which I wanna store]
[ "2020-09-14 08:18:56", -> ISODate("2020-09-14T08:42:41.000Z") ]
Is there any way to save those strings into MongoDB on NodeJS programmatically?
RFC 3339 is a profile (a set of standards derived) of ISO 6081 (see these SO posts: [1], [2]), so you can just pass the string to Date's constructor and get a valid js Date:
var raw = "2020-09-14 08:18:56";
var date = new Date(raw);
console.log(date);

grok pattern for Automation Anywhere timestamp

(1/15/2018 3:00:32 AM)
Hi I have the above format for which I was trying to write grok pattern to seperate date, time, and AM/PM , Please help. I was using below pattern but still don't see the proper out put when create the index.
grok {
match => {
"message" => "%{MONTHDAY}/%{MONTHNUM}/%{YEAR}[T ]%{HOUR}:?%{MINUTE}(?::?%{SECOND})?%{ISO8601_TIMEZONE}?"
}
}
The first number is a month and the second is the day, since it's above 12. So you'll have to switch %{MONTHDAY} & %{MONTHNUM} like this:
"%{MONTHNUM}/%{MONTHDAY}/%{YEAR}[T ]%{HOUR}:?%{MINUTE}(?::?%{SECOND})?%{ISO8601_TIMEZONE}?"

Logstash: How to save an entry from earlier in a log for use across multiple lines later in the log?

So the format of my logs looks somethings like this
02:00:30> First line of log for date of 2014-08-13
...
04:03:30> Every other line of log
My question is: how can I save the date from the first line to create the timestamp for the other lines in the files?
Is there a way to set some kind of "global" field that I can reuse for other lines?
I'm looking at historical logs so the current time isn't much use.
I posted a memorize filter that you could use to do that. It was posted here.
You'd use it like this:
filter {
if [message] =~ /date of/ {
grok {
match => [ "message", "date of (?<date>\d\d\d\d-\d\d-\d\d)" ]
}
} else {
// parse your log with grok or some other method that doesn't capture date
}
memorize {
field => date
}
}
So on the first line, because you extract a date, it'll memorize it... since it's not on the remaining lines, it'll add the memorized date to the events.

Converting date MMM dd HH:mm:ss for logstash

I have a logfile with a custom format, the date field looks like this:
Dec 4 23:59:21
Nov 21 23:59:21
in my logstash config I have this for the filter:
date {
type => "custom"
# tell it the format
custom_timestamp => ["MMM d HH:mm:ss", "MMM dd HH:mm:ss"]
# locale didn't help
locale => "en"
}
mutate{
type => "custom"
# replace the timestamp
replace => ["#timestamp", "%{custom_timestamp}"]
}
which supposedly replaces the logstash timestamp with the custom one from the logs (I am backfilling it from old logs at the moment to test).
If I turn on the debug flag and output to stdout, it shows me that #timestamp has been replaced with custom_timestamp but I get an error message telling me that it cannot be imported:
:exception=>java.lang.IllegalArgumentException: Invalid format: "Dec 4 23:59:21"
what do I have to do to convert the date format?
Turns out that the sample I was working from is wrong. You do not need the mutate replacement, the config is this now:
date {
type => "custom"
# tell it the format
custom_timestamp => ["MMM d HH:mm:ss", "MMM dd HH:mm:ss"]
# date format is english, computer might not be
locale => "en"
}
mutate{
type => "custom"
#other mutations go here
}
Two misconceptions in this post:
The java exception is generated because there is no YEAR in your format, therefore it cannot parse the date safely.
You need to run a mutate if you want other applications to see your old imported logs as a coherent timeline. Otherwise, when you import all your old logs, you'll only see a few minutes of events concentrated (during the import).
Other than that, good question/answer, it helped me get back on track on my particular problem ;)

Converting UTC Date to GMT in Groovy / Java

I am working with SoupUI a i need to adjust a Date/time (UTC) that i get back in a response to a GMT Date/time. The date that i get back in the respone looks as followes:
2012-11-09T00:00:00+01:00
I would like to convert this to
2012-11-08T23:00:00Z
Unfortunatly i lack Java skils and therefore also Groovy skils to be able to do this on my own. i did a lot o searches on date convertions but until now i was still unable to find what i was looking for. i will keep searching. if i do manage to get the solution then i will post it here.
Assuming there isn't a colon in the timezone portion, I believe this should work:
// Your input String (with no colons in the timezone portion)
String original = '2012-11-09T00:00:00+0100'
// The format to read this input String
def inFormat = new java.text.SimpleDateFormat( "yyyy-MM-dd'T'HH:mm:ssZ" )
// The format we want to output
def outFormat = new java.text.SimpleDateFormat( "yyyy-MM-dd'T'HH:mm:ss'Z'" )
// Set the timezone for the output
outFormat.timeZone = java.util.TimeZone.getTimeZone( 'GMT' )
// Then parse the original String, and format the resultant
// Date back into a new String
String result = outFormat.format( inFormat.parse( original ) )
// Check it's what we wanted
assert result == '2012-11-08T23:00:00Z'
If there is a colon in the TimeZone, you'll need Java 7 for this task (or maybe a date handling framework like JodaTime), and you can change the first two lines to:
// Your input String
String original = '2012-11-09T00:00:00+01:00'
// The format to read this input String (using the X
// placeholder for ISO time difference)
def inFormat = new java.text.SimpleDateFormat( "yyyy-MM-dd'T'HH:mm:ssX" )

Resources