How to format digits in logstash? - ruby

How can I format digit in logstash?
I am using the '' % format expression in ruby code in filter plugin but I get nil as format result. I tried sprintf and format function but same result.
Below is my code snippet.
ruby {
code => "
event.set( 'positioning', event.get('branch_lat') + ',' + event.get('branch_lon') )
event.set( 'report_datetime', event.get('report_date') + '%04d' % event.get('report_time') )
"
}
As a format result, I get below error in the log.
[2016-10-28T12:31:43,217][ERROR][logstash.filters.ruby ] Ruby exception occurred: undefined method `+' for nil:NilClass
My platform information is below.
[root#elk-analytic logstash]# rpm -qi logstash
Name : logstash
Epoch : 1
Version : 5.0.0
Release : 1
Architecture: noarch
Install Date: Thu 27 Oct 2016 01:26:03 PM JST
Group : default
Size : 198320729
License : ASL 2.0
Signature : RSA/SHA512, Wed 26 Oct 2016 01:57:59 PM JST, Key ID d27d666cd88e42b4
Source RPM : logstash-5.0.0-1.src.rpm
Build Date : Wed 26 Oct 2016 01:10:26 PM JST
Build Host : packer-virtualbox-iso-1474648640
Relocations : /
Packager : <vagrant#packer-virtualbox-iso-1474648640>
Vendor : Elasticsearch
URL : http://www.elasticsearch.org/overview/logstash/
Summary : An extensible logging pipeline
Description :
An extensible logging pipeline
Added on 2016.10.28 14:32
My Goal is to parse below csv columns into timestamp field in elasticsearch.
Please notice that hour of time has mixed patterns of 1 and 2 digits.
date,time
20160204,1000
20160204,935
I tried using date function in filter plugin but it did not work properly by logging error.
[2016-10-28T11:00:10,233][WARN ][logstash.filters.date ] Failed parsing date from field {:field=>"report_datetime",
:value=>"20160204 935", :exception=>"Cannot parse \"20160204 935\": Value 93 for hourOfDay must be in the range [0,23]", :config_parsers=>"YYYYMMdd Hmm", :config_locale=>"default=en_US"}
Below is the code snippet when above error appeared.
ruby {
code => "
event.set( 'positioning', event.get('branch_lat') + ',' + event.get('branch_lon') )
event.set( 'report_datetime', event.get('report_date') + ' ' + event.get('report_time') )
"
}
# Set the #timestamp according to report_date and time
date {
"match" => ["report_datetime", "YYYYMMdd Hmm"]
}
I did some modification and ended up with the code I first posted.

I suggest to do it like this without any ruby filter:
filter {
# your other filters...
# if 3-digit hours, pad the time with one zero
if [time] =~ /^\d{3}$/ {
mutate {
add_field => { "report_datetime" => "%{date} 0%{time}" }
}
# otherwise just concat the fields
} else {
mutate {
add_field => { "report_datetime" => "%{date} %{time}" }
}
}
# match date and time
date {
"match" => ["report_datetime", "yyyyMMdd HHmm"]
"target" => "report_datetime"
}
}

Related

Unable to convert string into date in tMap component Talend

I have gone through some similar questions but those solutions didn't worked for me I am having a date field which is String of timestamp "1631898440" I tried converting this string into date using tMap but got this error - java.lang.RuntimeException: java.text.ParseException: Unparseable date: "1631898440".
The function I am using -
row5.mydatecolumn!=null && !"".equalsIgnoreCase(row5.mydatecolumn)? TalendDate.parseDateLocale("EEE MMM dd HH:mm:ss zzz yyyy", row5.mydatecolumn, "EN") :null
Also tried -
TalendDate.parseDate("ddMMyyyy",row5.mydatecolumn)
In this I am getting this err- timestamp out of range: "898442-07-16 00:00:00+05:30"ERROR
How to resolve this issue is there anything wrong with the format of date?
In your user routine just create a fonction like this :
public static Date Convert_String_To_Date(String String_Timestamp) {
SimpleDateFormat sf = new SimpleDateFormat("ddMMyyyy");
Date date = new Date(Long.parseLong(String_Timestamp));
System.out.println("*** Date Converted to this patter ddMMyyyy : "+sf.format(date));
return TalendDate.parseDate("ddMMyyyy",sf.format(date)) ;
}
don't forget the import
import java.text.SimpleDateFormat;
import java.util.Date;
then for me i just put a tjava component where i called my fonction like below
String str = "1631898440";
System.out.println(Format_String_Date.Convert_String_To_Date(str)) ;
So , in your case you would call this fonction in your tMap like this i guess :
row5.mydatecolumn!=null && !"".equalsIgnoreCase(row5.mydatecolumn)?
Format_String_Date.Convert_String_To_Date(row5.mydatecolumn) :null
Here is the output
[statistics] connected
*** Date Converted to this patter ddMMyyyy : 19011970
Mon Jan 19 00:00:00 CET 1970
[statistics] disconnected

CaptureChangeMysql Json flow file contains "{0}" OR "{}" instead of "TRUE" or "FALSE" boolean value. How should I convert as Boolean value

[ {
"id" : 57592,
"code" : "village1023",
"created_by_id" : null,
"created_date" : "Tue Mar 31 23:08:47 IST 2020",
"l_village_name" : "village1023",
"modified_by_id" : 70806,
"modified_date" : "Tue Mar 31 23:08:47 IST 2020",
"name" : "village1023",
"status" : "{0}",
"taluk_id" : 386
} ]
Any suggestion on how could I convert the value received from JSON as "status" : "{0}" as "TRUE" for the MySQL value "1" in bit data type.
If you want to replace the JSON value content "{0}" in to true, we can update the content of the flow file using ReplaceText processor, so you can simply use the ReplaceText processor to achieve the reported scenario but still it have its own limitations. Please check and proceed further.
https://nifi.apache.org/docs/nifi-docs/components/org.apache.nifi/nifi-standard-nar/1.5.0/org.apache.nifi.processors.standard.ReplaceText/
I got the expected result, I made mistake in the expression. It should be ${field.value:equals("{0}"):ifElse("TRUE","FALSE")} in "UpdateRecord" processor.
Doumentation - Example 5 - Use Expression Language to Modify Value

Date pattern doesn't work as expected in logstash

I am trying to use following date filter to convert string to date but it doesn't seem to be working.
Sample input data(string) - Mon Jan 20 09:20:35 GMT 2020
I am first using a mutate gsub to remove GMT which renders following string output-
Mon Jan 20 09:20:35 2020
My gsub mutate filter looks like this -
mutate { gsub => [ "TimeStamp", "GMT", "" ] }
Now, I am using a date filter to convert gsub output to date format but it doesn't seem to be working-
date {
match => [ "TimeStamp", "EEE MMM dd HH:mm:ss yyyy" ]
target => "TimeStamp"
locale => "en"
}
I have also tried following with no success-
date {
match => [ "TimeStamp", "EEE\sMMM\sdd\sHH:mm:ss\s+yyyy" ]
target => "TimeStamp"
timezone => "Etc/GMT"
locale => "en"
}
The date pattern should be
MMM dd HH:mm:ss yyyy
Maybe you have to add some extra spaces before the year (looks like you have them in your logs).
Instead of EEE (name of weekday abreviated) you need to use MMM (name of month abreviated).

Overriding #timestamp via Logstash's date filter with a grok-extracted value

I am trying to mutate an string value to date time in logstash. Although the format is correct but in kibana/elastic search the field is showing string and not date.
As part of the analysis I tried to mutate the date in multiple ways but none of them are working. I tried some filters for milliseconds and half day as the date format for my log is with AM/PM.
Grok
match => { message => [
"\"%{WORD:status}\"\,\"(?<monitortime>%{MONTH:month}%{SPACE}%{MONTHDAY:day}\,%{SPACE}%{YEAR:year}%{SPACE}%{TIME:t1}%{SPACE}%{WORD:t2})\"\,\"%{WORD:monitor}\"\,%{INT:loadtime}\,%{INT:totalbytes}\,\"%{WORD:location}\"\,(?m)%{GREEDYDATA:error}"
Date Conversion
date {
locale => "en"
match => [ "monitortime", "MMM dd, yyyy kk:mm:ss.SSS aa ZZZ", "YYYY-MM-dd kk:mm:ss.SSS aa ZZZ" ]
timezone => "Etc/UCT"
}
output in kibana
message "Error","Jun 14, 2019 02:47:33 pm","xxxxxxxxxx",0,0,"stage_1","HomePage: Sign in link is not visible!"
monitortime Jun 14, 2019 02:47:33 pm
monitortime string
Timestamp recorded by elasticsearch
#timestamp Sep 10, 2019 # 20:06:48.525
The expected result will be to get monitortime as datatype date.

Logstash ignoring multiple date filters

i'm making a logstash .conf, and on my filter i need to extract the weekday of two timestamps, but Logstash act as if he only is making one match, example:
Timestamp 1: Mar 7, 2019 # 23:41:40.476 . => Thursday
Timestamp 2: Mar 1, 2019 # 15:22:47.209 . => Thu
Expected Output
Timestamp 1: Mar 7, 2019 # 23:41:40.476 . => Thursday
Timestamp 2: Mar 1, 2019 # 15:22:47.209 . => Fri
These are my filters:
date {
match => ["[system][process][cpu][start_time]", "dd-MM-YYYY HH:mm:ss", "ISO8601"]
target => "[system][process][cpu][start_time]"
add_field => {"[Weekday]" => "%{+EEEEE}"}
}
date {
match => ["[FechaPrimero]", "dd-MM-YYYY HH:mm:ss", "ISO8601"]
target => "[FechaPrimero]"
add_field => {"[WeekdayFirtsDay]" => "%{+EE}"}
}
It's because by default %{+EEEEE} and %{+EE} take into account the #timestamp field, and no a user made field (don't know it is written in the doc)
The only way of doing that, as far as I know, is using a part of ruby code, to extract day of week, as following :
ruby {
code => 'event.set("Weekday", Time.parse(event.get("[system][process][cpu][start_time]").to_s).strftime("%A"))'
}
ruby {
code => 'event.set("FechaPrimero", Time.parse(event.get("FechaPrimero").to_s).strftime("%a"))'
}

Resources