From 8966f650671051c15877a9129d38f86102574d4c Mon Sep 17 00:00:00 2001 From: Thomas Paradis Date: Wed, 7 Apr 2021 15:41:26 +0200 Subject: [PATCH] Add another metric for mails delivered locally --- postfix_exporter.go | 14 ++++++++++++++ postfix_exporter_test.go | 16 ++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/postfix_exporter.go b/postfix_exporter.go index ccc818c..f74dabe 100644 --- a/postfix_exporter.go +++ b/postfix_exporter.go @@ -72,6 +72,7 @@ type PostfixExporter struct { smtpStatusDeferred prometheus.Counter opendkimSignatureAdded *prometheus.CounterVec bounceNonDelivery prometheus.Counter + virtualDelivered prometheus.Counter } // A LogSource is an interface to read log lines. @@ -405,6 +406,12 @@ func (e *PostfixExporter) CollectFromLogLine(line string) { } else { e.addToUnsupportedLine(line, process) } + case "virtual": + if strings.HasSuffix(remainder, ", status=sent (delivered to maildir)") { + e.virtualDelivered.Inc() + } else { + e.addToUnsupportedLine(line, process) + } default: e.addToUnsupportedLine(line, subprocess) } @@ -603,6 +610,11 @@ func NewPostfixExporter(showqPath string, logSrc LogSource, logUnsupportedLines Name: "bounce_non_delivery_notification_total", Help: "Total number of non delivery notification sent by bounce.", }), + virtualDelivered: prometheus.NewCounter(prometheus.CounterOpts{ + Namespace: "postfix", + Name: "virtual_delivered_total", + Help: "Total number of mail delivered to a virtual mailbox.", + }), }, nil } @@ -638,6 +650,7 @@ func (e *PostfixExporter) Describe(ch chan<- *prometheus.Desc) { e.smtpConnectionTimedOut.Describe(ch) e.opendkimSignatureAdded.Describe(ch) ch <- e.bounceNonDelivery.Desc() + ch <- e.virtualDelivered.Desc() } func (e *PostfixExporter) StartMetricCollection(ctx context.Context) { @@ -715,4 +728,5 @@ func (e *PostfixExporter) Collect(ch chan<- prometheus.Metric) { ch <- e.smtpConnectionTimedOut e.opendkimSignatureAdded.Collect(ch) ch <- e.bounceNonDelivery + ch <- e.virtualDelivered } diff --git a/postfix_exporter_test.go b/postfix_exporter_test.go index 378b67a..cb646b1 100644 --- a/postfix_exporter_test.go +++ b/postfix_exporter_test.go @@ -34,6 +34,7 @@ func TestPostfixExporter_CollectFromLogline(t *testing.T) { smtpdSASLAuthenticationFailures prometheus.Counter smtpdTLSConnects *prometheus.CounterVec bounceNonDelivery prometheus.Counter + virtualDelivered prometheus.Counter unsupportedLogEntries *prometheus.CounterVec } type args struct { @@ -44,6 +45,7 @@ func TestPostfixExporter_CollectFromLogline(t *testing.T) { smtpdMessagesProcessed int smtpMessagesProcessed int bounceNonDelivery int + virtualDelivered int } tests := []struct { name string @@ -197,6 +199,18 @@ func TestPostfixExporter_CollectFromLogline(t *testing.T) { bounceNonDelivery: prometheus.NewCounter(prometheus.CounterOpts{}), }, }, + { + name: "Testing virtual delivered", + args: args{ + line: []string{ + "Apr 7 15:35:20 123-mail postfix/virtual[20235]: 199041033BE: to=, relay=virtual, delay=0.08, delays=0.08/0/0/0, dsn=2.0.0, status=sent (delivered to maildir)", + }, + virtualDelivered: 1, + }, + fields: fields{ + virtualDelivered: prometheus.NewCounter(prometheus.CounterOpts{}), + }, + }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { @@ -225,6 +239,7 @@ func TestPostfixExporter_CollectFromLogline(t *testing.T) { smtpdSASLAuthenticationFailures: tt.fields.smtpdSASLAuthenticationFailures, smtpdTLSConnects: tt.fields.smtpdTLSConnects, bounceNonDelivery: tt.fields.bounceNonDelivery, + virtualDelivered: tt.fields.virtualDelivered, unsupportedLogEntries: tt.fields.unsupportedLogEntries, logUnsupportedLines: true, } @@ -237,6 +252,7 @@ func TestPostfixExporter_CollectFromLogline(t *testing.T) { assertCounterEquals(t, e.smtpdProcesses, tt.args.smtpdMessagesProcessed, "Wrong number of smtpd messages processed") assertCounterEquals(t, e.smtpProcesses, tt.args.smtpMessagesProcessed, "Wrong number of smtp messages processed") assertCounterEquals(t, e.bounceNonDelivery, tt.args.bounceNonDelivery, "Wrong number of non delivery notifications") + assertCounterEquals(t, e.virtualDelivered, tt.args.virtualDelivered, "Wrong number of delivered mails") }) } }