Track the number of expired emails form logs

This commit is contained in:
Thomas Paradis 2021-04-14 10:52:11 +02:00
parent 8966f65067
commit 4470123df6
2 changed files with 28 additions and 0 deletions

View File

@ -53,6 +53,7 @@ type PostfixExporter struct {
qmgrInsertsNrcpt prometheus.Histogram
qmgrInsertsSize prometheus.Histogram
qmgrRemoves prometheus.Counter
qmgrExpires prometheus.Counter
smtpDelays *prometheus.HistogramVec
smtpTLSConnects *prometheus.CounterVec
smtpConnectionTimedOut prometheus.Counter
@ -294,6 +295,7 @@ var (
logLine = regexp.MustCompile(` ?(postfix|opendkim)(/(\w+))?\[\d+\]: (.*)`)
lmtpPipeSMTPLine = regexp.MustCompile(`, relay=(\S+), .*, delays=([0-9\.]+)/([0-9\.]+)/([0-9\.]+)/([0-9\.]+), `)
qmgrInsertLine = regexp.MustCompile(`:.*, size=(\d+), nrcpt=(\d+) `)
qmgrExpiredLine = regexp.MustCompile(`:.*, status=(expired|force-expired), returned to sender`)
smtpStatusLine = regexp.MustCompile(`, status=(\w+) `)
smtpTLSLine = regexp.MustCompile(`^(\S+) TLS connection established to \S+: (\S+) with cipher (\S+) \((\d+)/(\d+) bits\)`)
smtpConnectionTimedOut = regexp.MustCompile(`^connect\s+to\s+(.*)\[(.*)\]:(\d+):\s+(Connection timed out)$`)
@ -356,6 +358,8 @@ func (e *PostfixExporter) CollectFromLogLine(line string) {
addToHistogram(e.qmgrInsertsNrcpt, qmgrInsertMatches[2], "QMGR nrcpt")
} else if strings.HasSuffix(remainder, ": removed") {
e.qmgrRemoves.Inc()
} else if qmgrExpired := qmgrExpiredLine.FindStringSubmatch(remainder); qmgrExpired != nil {
e.qmgrExpires.Inc()
} else {
e.addToUnsupportedLine(line, subprocess)
}
@ -505,6 +509,11 @@ func NewPostfixExporter(showqPath string, logSrc LogSource, logUnsupportedLines
Name: "qmgr_messages_removed_total",
Help: "Total number of messages removed from mail queues.",
}),
qmgrExpires: prometheus.NewCounter(prometheus.CounterOpts{
Namespace: "postfix",
Name: "qmgr_messages_expired_total",
Help: "Total number of messages expired from mail queues.",
}),
smtpDelays: prometheus.NewHistogramVec(
prometheus.HistogramOpts{
Namespace: "postfix",
@ -633,6 +642,7 @@ func (e *PostfixExporter) Describe(ch chan<- *prometheus.Desc) {
ch <- e.qmgrInsertsNrcpt.Desc()
ch <- e.qmgrInsertsSize.Desc()
ch <- e.qmgrRemoves.Desc()
ch <- e.qmgrExpires.Desc()
e.smtpDelays.Describe(ch)
e.smtpTLSConnects.Describe(ch)
ch <- e.smtpDeferreds.Desc()
@ -711,6 +721,7 @@ func (e *PostfixExporter) Collect(ch chan<- prometheus.Metric) {
ch <- e.qmgrInsertsNrcpt
ch <- e.qmgrInsertsSize
ch <- e.qmgrRemoves
ch <- e.qmgrExpires
e.smtpDelays.Collect(ch)
e.smtpTLSConnects.Collect(ch)
ch <- e.smtpDeferreds

View File

@ -20,6 +20,7 @@ func TestPostfixExporter_CollectFromLogline(t *testing.T) {
qmgrInsertsNrcpt prometheus.Histogram
qmgrInsertsSize prometheus.Histogram
qmgrRemoves prometheus.Counter
qmgrExpires prometheus.Counter
smtpDelays *prometheus.HistogramVec
smtpTLSConnects *prometheus.CounterVec
smtpDeferreds prometheus.Counter
@ -40,6 +41,7 @@ func TestPostfixExporter_CollectFromLogline(t *testing.T) {
type args struct {
line []string
removedCount int
expiredCount int
saslFailedCount int
outgoingTLS int
smtpdMessagesProcessed int
@ -110,6 +112,19 @@ func TestPostfixExporter_CollectFromLogline(t *testing.T) {
unsupportedLogEntries: prometheus.NewCounterVec(prometheus.CounterOpts{}, []string{"process"}),
},
},
{
name: "qmgr expired",
args: args{
line: []string{
"Apr 10 14:50:16 mail postfix/qmgr[3663]: BACE842E72: from=<noreply@domain.com>, status=expired, returned to sender",
"Apr 10 14:50:16 mail postfix/qmgr[3663]: BACE842E73: from=<noreply@domain.com>, status=force-expired, returned to sender",
},
expiredCount: 2,
},
fields: fields{
qmgrExpires: prometheus.NewCounter(prometheus.CounterOpts{}),
},
},
{
name: "SASL Failed",
args: args{
@ -225,6 +240,7 @@ func TestPostfixExporter_CollectFromLogline(t *testing.T) {
qmgrInsertsNrcpt: tt.fields.qmgrInsertsNrcpt,
qmgrInsertsSize: tt.fields.qmgrInsertsSize,
qmgrRemoves: tt.fields.qmgrRemoves,
qmgrExpires: tt.fields.qmgrExpires,
smtpDelays: tt.fields.smtpDelays,
smtpTLSConnects: tt.fields.smtpTLSConnects,
smtpDeferreds: tt.fields.smtpDeferreds,
@ -247,6 +263,7 @@ func TestPostfixExporter_CollectFromLogline(t *testing.T) {
e.CollectFromLogLine(line)
}
assertCounterEquals(t, e.qmgrRemoves, tt.args.removedCount, "Wrong number of lines counted")
assertCounterEquals(t, e.qmgrExpires, tt.args.expiredCount, "Wrong number of qmgr expired lines counted")
assertCounterEquals(t, e.smtpdSASLAuthenticationFailures, tt.args.saslFailedCount, "Wrong number of Sasl counter counted")
assertCounterEquals(t, e.smtpTLSConnects, tt.args.outgoingTLS, "Wrong number of TLS connections counted")
assertCounterEquals(t, e.smtpdProcesses, tt.args.smtpdMessagesProcessed, "Wrong number of smtpd messages processed")