如果适用,让 Logstash 将系统日志消息字符串视为 JSON

Getting Logstash to treat syslog message string as JSON if applicable

我有各种 shell 脚本,我从中 运行 系统日志的记录器行,其中包含 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 系统日志输入来接收它,个人日志作为常规日志接收,消息作为字符串:

"_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 消息。将syslog消息的内容解析为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"}
    }
}

我的 logstash conf 中的以下过滤器似乎可以转换我的 json 消息字符串并正确提取字段:

filter {
    grok {
        overwrite => ["message"]
    }
    json {
     source => "message"
    }
}

我的 JSON 中的三个 key/value 对在 JSON 条目的 _source 中似乎都是正确的类型,我现在可以使用它们作为字段:

{
    "_source: {
        "task_id": "231232xyz", 
        "seconds": 12.453000, 
        "success": true
    ...
}