【发布时间】:2016-05-10 10:48:01
【问题描述】:
我有各种 shell 脚本,我从这些脚本中运行 syslog 的记录器行,其中包含 json 格式的消息:
printf '{"task_id": "%s", "seconds": %f, "success": %s}' ${task_id} ${num_seconds} ${success_bool}
这会在 /var/log/syslog 中获得以下输出:
Feb 1 15:12:16 my-machine logger: {"task_id": "231232xyz", "seconds": 12.453000, "success": true}
我使用常规的 logstash syslog 输入来接收这个,单个日志作为常规日志接收,消息作为字符串:
"_source": {
"message": "{\"task_id\": "231232xyz", \"seconds\": 12.453000, \"success\": true}",
"tags": [
"_jsonparsefailure",
"_grokparsefailure"
],
我显然可以只使用普通消息作为
task_id: 221232xyz, seconds: 12.453000, success: true
并使用 grok 将值提取并解析为字段(包括将秒数转换为浮点数),但我认为在使用 cee 或仅使用最适合我的普通消息之间应该有一个解决方案。显然,来自 Syslog 的其他消息将包含非 json 消息。将系统日志消息的内容解析为 JSON 是否可行?
编辑,根据评论的要求,这里是 logstash 输入:
input {
syslog {
port => 5000
host => "0.0.0.0"
type => "syslog"
codec => "json"
}
}
grok 过滤器是我(工作)尝试匹配逗号分隔的消息并开始从中提取执行时间:
filter {
grok {
match => ["message", "seconds: %{NUMBER:exec_time}"
}
mutate {
convert => {"exec_time" => "float"}
}
}
【问题讨论】:
-
jsonparsefailure 标记意味着您正在尝试在配置中使用 json(编解码器?过滤器?);请与我们分享。
-
我刚刚添加了我的 logstash 输入配置。这是编解码器,而不是过滤器。我不确定如何为此使用过滤器。
-
显然使用了 grok/mutate,正如我在帖子末尾展示的那样,但与 Logstash 可以将消息视为 JSON 相比,它似乎更少的未来证明(如果有人修改了字符串的格式) .
标签: logging elasticsearch logstash logstash-grok rsyslog