英文:
ruby - Ruby exception occurred: no implicit conversion of LogStash::Timestamp into String
问题
以下是您要翻译的内容:
"I have a file stored every 5 mins in elastic DB index, and each line in the file contains a start and end date, I made the configuration to handle the dates and calculate 1 day on the end date, so when I added a day I faced this error."
"The logstash configurations"
"input {
file {
path => "/path"
start_position => "beginning"
mode => "read"
sincedb_path => "/dev/null"
}
}
filter {
grok {
match => { "message" => "%{DATA}%{NUMBER:SN}%{DATA}%{NUMBER:ID}%{DATA}%{WORD:Source}%{DATA}%{TIMESTAMP_ISO8601:Start_Time}%{DATA}%{TIMESTAMP_ISO8601:End_Time}%{DATA}%{WORD:Status}" }
}
if "FINISHED" in [message] {
mutate {add_field => { "number" => 1 } } }
if "RUNNING" in [message] {
mutate { add_field => { "number" => 3 } }}
date {
match => [ "End_Time", "yyyy-MM-dd HH:mm" ]
target => "enddate"}
date {
match => [ "@timestamp", "yyyy-MM-dd HH:mm" ]
target => "mytimestamp" }
mutate { rename => { "mytimestamp" => "@timestamp" } }
mutate { convert => { "mytimestamp" => "string" } }
ruby {
code => "
require 'time'
current_time = Date.parse(event.get('enddate'))
new_time = current_time + 86400
event.set('enddateone', new_time.iso8601(3))"
}
if "ABORTED" in [message] {
if [enddateone] >= [mytimestamp] {
mutate { add_field => { "number" => 4 } }}
else {
mutate { add_field => { "number" => 2 } }}
}
}
output {
elasticsearch {}"
英文:
I have a file stored every 5 mins in elastic DB index, and each line in the file contains a start and end date, I made the configuration to handle the dates and calculate 1 day on the end date, so when I added a day I faced this error.
[ERROR] 2023-04-13 13:52:37.082 [[main]>worker10] ruby - Ruby exception occurred: no implicit conversion of LogStash::Timestamp into String
[ERROR] 2023-04-13 13:52:37.082 [[main]>worker4] ruby - Ruby exception occurred: no implicit conversion of LogStash::Timestamp into String
[ERROR] 2023-04-13 13:52:37.082 [[main]>worker7] ruby - Ruby exception occurred: no implicit conversion of LogStash::Timestamp into String
[ERROR] 2023-04-13 13:52:37.082 [[main]>worker0] ruby - Ruby exception occurred: no implicit conversion of LogStash::Timestamp into String
[ERROR] 2023-04-13 13:52:37.082 [[main]>worker12] ruby - Ruby exception occurred: no implicit conversion of LogStash::Timestamp into String
[ERROR] 2023-04-13 13:52:37.082 [[main]>worker5] ruby - Ruby exception occurred: no implicit conversion of LogStash::Timestamp into String
[ERROR] 2023-04-13 13:52:37.082 [[main]>worker13] ruby - Ruby exception occurred: no implicit conversion of LogStash::Timestamp into String
[ERROR] 2023-04-13 13:52:37.083 [[main]>worker6] ruby - Ruby exception occurred: no implicit conversion of LogStash::Timestamp into String
[ERROR] 2023-04-13 13:52:37.084 [[main]>worker17] ruby - Ruby exception occurred: no implicit conversion of NilClass into String
[ERROR] 2023-04-13 13:52:37.084 [[main]>worker14] ruby - Ruby exception occurred: no implicit conversion of LogStash::Timestamp into String
[ERROR] 2023-04-13 13:52:37.086 [[main]>worker9] ruby - Ruby exception occurred: no implicit conversion of LogStash::Timestamp into String
[ERROR] 2023-04-13 13:52:37.086 [[main]>worker17] ruby - Ruby exception occurred: no implicit conversion of LogStash::Timestamp into String
[ERROR] 2023-04-13 13:52:37.087 [[main]>worker16] ruby - Ruby exception occurred: no implicit conversion of LogStash::Timestamp into String
[ERROR] 2023-04-13 13:52:37.087 [[main]>worker14] ruby - Ruby exception occurred: no implicit conversion of NilClass into String
[ERROR] 2023-04-13 13:52:37.087 [[main]>worker11] ruby - Ruby exception occurred: no implicit conversion of LogStash::Timestamp into String
[ERROR] 2023-04-13 13:52:37.087 [[main]>worker1] ruby - Ruby exception occurred: no implicit conversion of LogStash::Timestamp into String
[ERROR] 2023-04-13 13:52:37.087 [[main]>worker16] ruby - Ruby exception occurred: no implicit conversion of NilClass into String
[ERROR] 2023-04-13 13:52:37.087 [[main]>worker1] ruby - Ruby exception occurred: no implicit conversion of NilClass into String
The logstash configurations
input {
file {
path => "/path"
start_position => "beginning"
mode => "read"
sincedb_path => "/dev/null"
}
}
filter {
grok {
match => { "message" => "%{DATA}%{NUMBER:SN}%{DATA}%{NUMBER:ID}%{DATA}%{WORD:Source}%{DATA}%{TIMESTAMP_ISO8601:Start_Time}%{DATA}%{TIMESTAMP_ISO8601:End_Time}%{DATA}%{WORD:Status}" }
}
if "FINISHED" in [message] {
mutate {add_field => { "number" => 1 } } }
if "RUNNING" in [message] {
mutate { add_field => { "number" => 3 } }}
date {
match => [ "End_Time", "yyyy-MM-dd HH:mm" ]
target => "enddate"}
date {
match => [ "@timestamp", "yyyy-MM-dd HH:mm" ]
target => "mytimestamp" }
# mutate { rename => { "mytimestamp" => "@timestamp" } }
mutate { convert => { "mytimestamp" => "string" } }
ruby {
code => "
require 'time'
current_time = Date.parse(event.get('enddate'))
new_time = current_time + 86400
event.set('enddateone', new_time.iso8601(3))"
}
if "ABORTED" in [message] {
if [enddateone] >= [mytimestamp] {
mutate { add_field => { "number" => 4 } }}
else {
mutate { add_field => { "number" => 2 } }}
}
}
output {
elasticsearch {}
答案1
得分: 1
If the grok does not match, then the [End_Time] field will not exist. When the source field does not exist the date filter is a no-op, so the [enddate] field will not be created.
如果 grok 不匹配,那么 [End_Time] 字段将不存在。当源字段不存在时,日期过滤器不起作用,因此不会创建 [enddate] 字段。
current_time = Date.parse(event.get('enddate'))
current_time = Date.parse(event.get('enddate'))
Date.parse expects a string. If the [enddate] field does not exist then event.get will return nil, and the error message is telling you that Ruby is not willing to convert that nil to a string for you.
Date.parse 需要一个字符串。如果 [enddate] 字段不存在,那么 event.get 将返回 nil,错误消息告诉您 Ruby 不愿意为您将 nil 转换为字符串。
If the [enddate] field does exist then event.get will return a LogStash::Timestamp object, and again there is no implicit conversion of that.
如果 [enddate] 字段存在,则 event.get 将返回一个 LogStash::Timestamp 对象,同样没有隐式的转换。
You do not need to use Date.parse at all.
您根本不需要使用 Date.parse。
ruby {
code => '
enddate = event.get("enddate")
if enddate
new_time = Time.at(enddate.to_f + 86400)
event.set("enddateone", new_time.iso8601(3))
end
'
}
ruby {
code => '
enddate = event.get("enddate")
if enddate
new_time = Time.at(enddate.to_f + 86400)
event.set("enddateone", new_time.iso8601(3))
end
'
}
英文:
If the grok does not match, then the [End_Time] field will not exist. When the source field does not exist the date filter is a no-op, so the [enddate] field will not be created.
current_time = Date.parse(event.get('enddate'))
Date.parse expects a string. If the [enddate] field does not exist then event.get will return nil, and the error message is telling you that Ruby is not willing to convert that nil to a string for you.
If the [enddate] field does exist then event.get will return a LogStash::Timestamp object, and again there is no implicit conversion of that.
You do not need to use Date.parse at all.
ruby {
code => '
enddate = event.get("enddate")
if enddate
new_time = Time.at(enddate.to_f + 86400)
event.set("enddateone", new_time.iso8601(3))
end
'
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论