Logstash和postfix的Grok模式问题

时间:2016-01-08 17:18:35

标签: logstash postfix-mta grok subject

我在解析弹性搜索服务器的某一行数据时遇到问题,以便可以搜索,等等。

我在这里尝试做的是将后缀记录到通过系统的所有消息的主题行。我知道这对于数据记录来说有点灰色,但似乎有效。

为了达到这个目的,我编辑了postfix文件中的main.cf来执行主题行的headercheck并将其记录在INFO级别并将其写入maillog。

因此,消息的主题行来自后缀清理过程,在kibana中看起来像这样,onced parsed out:

Table
JSON
@timestamp      January 8th 2016, 11:51:10.951
t@version       1
t_id            AVIiJeGaAHt2sxJKgJgY
t_index         logstash-2016.01.08
#_score         [empty]
t_type          log
#count          1
tfields.type        postfix
tfrom           [Incoming server]
thelo           [Test computer]
tinput_type     [empty]
#line           715
tmessage        Jan  8 11:51:10 testserver postfix/cleanup[19150]: CFEBE81B5877: info: header Subject: Test Messages from unknown[10.21.2.166]; from=<Test@testserver.com> to=<example@example.com> proto=ESMTP helo=<testcomputer>
#offset         226,216
tproto          ESMTP
tshipper        Testserver
tsource         /var/log/maillog
ttags           _grokparsefailure
tto             example@example.com
ttype           log

这是我正在使用的grok模式文件:

# Postfix stuff based on https://gist.github.com/jbrownsc/4694374:
# ORIGINAL POSTFIX PATTERNS #
QUEUEID (?:[A-F0-9]+|NOQUEUE)
EMAILADDRESSPART [a-zA-Z0-9_.+-=:]+
EMAILADDRESS %{EMAILADDRESSPART:local}@%{EMAILADDRESSPART:remote}
RELAY (?:%{HOSTNAME:relayhost}(?:\[%{IP:relayip}\](?::[0-9]+(.[0-9]+)?)?)?)
POSREAL [0-9]+(.[0-9]+)?
DELAYS (%{POSREAL}[/]*)+
DSN %{NONNEGINT}.%{NONNEGINT}.%{NONNEGINT}
STATUS sent|deferred|bounced|expired
PERMERROR 5[0-9]{2}
MESSAGELEVEL reject|warning|error|fatal|panic
POSTFIXACTION discard|dunno|filter|hold|ignore|info|prepend|redirect|replace|reject|warn

# postfix/smtp and postfix/lmtp and postfix/local
POSTFIXSMTPRELAY %{QUEUEID:qid}: to=<%{EMAILADDRESS:to}>,(?:\sorig_to=<%{EMAILADDRESS:orig_to}>,)? relay=%{RELAY}, delay=%{POSREAL:delay}, delays=%{DELAYS:delays}, dsn$
POSTFIXSMTPCONNECT connect to %{RELAY}: %{GREEDYDATA:reason}
POSTFIXSMTP4XX %{QUEUEID:qid}: host %{RELAY} said: %{GREEDYDATA:reason}
POSTFIXSMTP5XX %{QUEUEID:qid}: to=<%{EMAILADDRESS:to}>,(?:\sorig_to=<%{EMAILADDRESS:orig_to}>,)? relay=%{RELAY}, delay=%{POSREAL:delay}, delays=%{DELAYS:delays}, dsn=%$
POSTFIXSMTPREFUSAL %{QUEUEID:qid}: host %{RELAY} refused to talk to me: %{GREEDYDATA:reason}
POSTFIXSMTPLOSTCONNECTION %{QUEUEID:qid}: lost connection with %{RELAY} while %{GREEDYDATA:reason}
POSTFIXSMTPTIMEOUT %{QUEUEID:qid}: conversation with %{RELAY} timed out while %{GREEDYDATA:reason}

# postfix/smtpd
POSTFIXSMTPDCONNECTS (?:dis)?connect from %{RELAY}
POSTFIXSMTPDACTIONS %{QUEUEID:qid}: %{POSTFIXACTION}: %{DATA:command} from %{RELAY}: %{DATA:smtp_response}: %{DATA:reason}; from=<%{EMAILADDRESS:from}> to=<%{EMAILADDR$
POSTFIXSMTPDTIMEOUTS timeout after %{DATA:command} from %{RELAY}
POSTFIXSMTPDLOGIN %{QUEUEID:qid}: client=%{DATA:client}, sasl_method=%{DATA:saslmethod}, sasl_username=%{EMAILADDRESS:saslusername}
POSTFIXSMTPDCLIENT %{QUEUEID:qid}: client=%{DATA:client}

# postfix/cleanup
POSTFIXCLEANUP %{QUEUEID:qid}: %{DATA:type_alert}: %{GREEDYDATA:subject} from %{RELAY}; message-id=<%{EMAILADDRESS:messageid}>

# postfix/bounce
POSTFIXBOUNCE %{QUEUEID:qid}: sender non-delivery notification: %{QUEUEID:bouncequeueid}

# postfix/qmgr and postfix/pickup
POSTFIXQMGR %{QUEUEID:qid}: (?:removed|from=<(?:%{EMAILADDRESS:from})?>(?:, size=%{POSINT:size}, nrcpt=%{POSINT:nrcpt} \(%{GREEDYDATA:queuestatus}\))?)

# postfix/warm
POSTFIXINFO %{QUEUEID:qid}

如您所见,它不想为我解析主题行。我试图在postfix / cleanup模式下做出正确的更改,但它似乎没有工作。我是grok模式构建的新手,我们将不胜感激。

logastash的Grok语句:

input {
    file {
        type => "postfix"
        path => "/var/log/maillog"
    }
}

    filter {
        grok {
            patterns_dir => [ "/etc/logstash/patterns.d" ]
            pattern => [
                "%{SYSLOGBASE} %{POSTFIXSMTPDCONNECTS}",
                "%{SYSLOGBASE} %{POSTFIXSMTPDACTIONS}",
                "%{SYSLOGBASE} %{POSTFIXSMTPDTIMEOUTS}",
                "%{SYSLOGBASE} %{POSTFIXSMTPDLOGIN}",
                "%{SYSLOGBASE} %{POSTFIXSMTPDCLIENT}",
                "%{SYSLOGBASE} %{POSTFIXSMTPRELAY}",
                "%{SYSLOGBASE} %{POSTFIXSMTPCONNECT}",
                "%{SYSLOGBASE} %{POSTFIXSMTP4XX}",
                "%{SYSLOGBASE} %{POSTFIXSMTP5XX}",
                "%{SYSLOGBASE} %{POSTFIXSMTPREFUSAL}",
                "%{SYSLOGBASE} %{POSTFIXSMTPLOSTCONNECTION}",
                "%{SYSLOGBASE} %{POSTFIXSMTPTIMEOUT}",
                "%{SYSLOGBASE} %{POSTFIXBOUNCE}",
                "%{SYSLOGBASE} %{POSTFIXQMGR}",
                "%{SYSLOGBASE} %{POSTFIXCLEANUP}",
                "%{SYSLOGBASE} %{POSTFIXINFO}"
            ]
            named_captures_only => true
        }
    }

1 个答案:

答案 0 :(得分:0)

您的模式必须与您的输入相匹配,如之前的评论中所述。如果查看输入,它在SYSLOGBASE之后有几个部分。 (为了讨论而添加了行号):

1. CFEBE81B5877:
2. info:
3. header Subject: Test Messages from unknown[10.21.2.166];
4. from=<Test@testserver.com>
5. to=<example@example.com>
6. proto=ESMTP
7. helo=<testcomputer>

因此,您的模式必须考虑所有(或在某些情况下,某些情况下)此数据。

即使您的新模式也不会这样做(再次将数字与会话值分开):

1. %{QUEUEID:qid}:
2. %{DATA:type_alert}:
3. %{GREEDYDATA:subject} from %{RELAY};
to=<%{EMAILADDRESS:to}>
%{DATA:proto)
<%{IPORHOST}>

输入的行号1-3与您的模式的1-3匹配,您可以在grok调试器中测试。

但请看输入的第4行。您的模式中没有任何匹配它的内容。因此,整个模式不匹配,并且没有字段。

使用grok的通用建议是使用调试器,开始减速,从左侧一次移动一个字段。这将使您远离缺少的字段,并确保您在移动时对解析感到满意。