3

メールでログ メッセージを転送するために使用している Logstash 構成があります。jsonおよびを使用して、 json_encodeJSON ログ メッセージを解析および再エンコードします。

json_encodeJSON をきれいに印刷するために使用され、非常に見栄えの良いメールが作成されました。残念ながら、最近の Logstash のアップグレードにより、きれいに印刷されなくなりました。

電子メールの本文に使用できるフィールドに、きれいな形式のイベントを取得する方法はありますか? JSON、Ruby デバッグ、またはその他の人間が読めるほとんどの形式で問題ありません。

filter {
    if [type] == "bunyan" {
        # Save a copy of the message, in case we need to pretty-print later
        mutate {
            add_field => { "@orig_message" => "%{message}" }
        }

        json {
            source => "message"
            add_tag => "json"
        }
    }

    // other filters that might add an "email" tag

    if "email" in [tags] {
        # pretty-print JSON for the email
        if "json" in [tags] {
            # re-parse the message into a field we can encode
            json {
                source => "@orig_message"
                target => "body"
            }

            # encode the message, but pretty this time
            json_encode {
                source => "body"
                target => "body"
            }
        }

        # escape the body for HTML output
        mutate {
            add_field => { htmlbody => "%{body}" }
        }
        mutate {
            gsub => [
                'htmlbody', '&', '&',
                'htmlbody', '<', '&lt;'
            ]
        }
    }
}

output {
    if "email" in [tags] and "throttled" not in [tags] {
        email {
            options => {
                # config stuff...
            }
            body => "%{body}"
            htmlbody => "
<table>
  <tr><td>host:</td><td>%{host}</td></tr>
  <tr><td>when:</td><td>%{@timestamp}</td></tr>
</table>
<pre>%{htmlbody}</pre>
"
        }
    }
}
4

1 に答える 1

1

approxiblue が述べたように、この問題は、logstash の新しい JSON パーサー(JrJackson) が原因です。プリティプリントのサポートが再び追加されるまで、古いパーサーを回避策として使用できます。方法は次のとおりです。

プラグインの ruby​​ ファイルの 2 行を変更する必要があります。パスは次のようになります。

LS_HOME/vendor/bundle/jruby/1.9/gems/logstash-filter-json_encode-0.1.5/lib/logstash/filters/json_encode.rb

5行目を変更

require "logstash/json" 

の中へ

require "json" 

44行目を変更

event[@target] = LogStash::Json.dump(event[@source])

の中へ

event[@target] = JSON.pretty_generate(event[@source])

それで全部です。再起動後、logstash を再度整形する必要があります。

補足:

Ruby ソースを変更したくない場合は、json_encode の代わりに Ruby フィルターを使用することもできます。

# encode the message, but pretty this time
ruby  {
    init => "require 'json'"
    code => "event['body'] = JSON.pretty_generate(event['body'])"
}
于 2015-08-25T16:12:07.143 に答える