Add another metric for mails delivered locally

This commit is contained in:
Thomas Paradis 2021-04-07 15:41:26 +02:00
parent 6531beb3a7
commit 8966f65067
2 changed files with 30 additions and 0 deletions

View File

@ -72,6 +72,7 @@ type PostfixExporter struct {
smtpStatusDeferred prometheus.Counter smtpStatusDeferred prometheus.Counter
opendkimSignatureAdded *prometheus.CounterVec opendkimSignatureAdded *prometheus.CounterVec
bounceNonDelivery prometheus.Counter bounceNonDelivery prometheus.Counter
virtualDelivered prometheus.Counter
} }
// A LogSource is an interface to read log lines. // A LogSource is an interface to read log lines.
@ -405,6 +406,12 @@ func (e *PostfixExporter) CollectFromLogLine(line string) {
} else { } else {
e.addToUnsupportedLine(line, process) e.addToUnsupportedLine(line, process)
} }
case "virtual":
if strings.HasSuffix(remainder, ", status=sent (delivered to maildir)") {
e.virtualDelivered.Inc()
} else {
e.addToUnsupportedLine(line, process)
}
default: default:
e.addToUnsupportedLine(line, subprocess) e.addToUnsupportedLine(line, subprocess)
} }
@ -603,6 +610,11 @@ func NewPostfixExporter(showqPath string, logSrc LogSource, logUnsupportedLines
Name: "bounce_non_delivery_notification_total", Name: "bounce_non_delivery_notification_total",
Help: "Total number of non delivery notification sent by bounce.", 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 }, nil
} }
@ -638,6 +650,7 @@ func (e *PostfixExporter) Describe(ch chan<- *prometheus.Desc) {
e.smtpConnectionTimedOut.Describe(ch) e.smtpConnectionTimedOut.Describe(ch)
e.opendkimSignatureAdded.Describe(ch) e.opendkimSignatureAdded.Describe(ch)
ch <- e.bounceNonDelivery.Desc() ch <- e.bounceNonDelivery.Desc()
ch <- e.virtualDelivered.Desc()
} }
func (e *PostfixExporter) StartMetricCollection(ctx context.Context) { func (e *PostfixExporter) StartMetricCollection(ctx context.Context) {
@ -715,4 +728,5 @@ func (e *PostfixExporter) Collect(ch chan<- prometheus.Metric) {
ch <- e.smtpConnectionTimedOut ch <- e.smtpConnectionTimedOut
e.opendkimSignatureAdded.Collect(ch) e.opendkimSignatureAdded.Collect(ch)
ch <- e.bounceNonDelivery ch <- e.bounceNonDelivery
ch <- e.virtualDelivered
} }

View File

@ -34,6 +34,7 @@ func TestPostfixExporter_CollectFromLogline(t *testing.T) {
smtpdSASLAuthenticationFailures prometheus.Counter smtpdSASLAuthenticationFailures prometheus.Counter
smtpdTLSConnects *prometheus.CounterVec smtpdTLSConnects *prometheus.CounterVec
bounceNonDelivery prometheus.Counter bounceNonDelivery prometheus.Counter
virtualDelivered prometheus.Counter
unsupportedLogEntries *prometheus.CounterVec unsupportedLogEntries *prometheus.CounterVec
} }
type args struct { type args struct {
@ -44,6 +45,7 @@ func TestPostfixExporter_CollectFromLogline(t *testing.T) {
smtpdMessagesProcessed int smtpdMessagesProcessed int
smtpMessagesProcessed int smtpMessagesProcessed int
bounceNonDelivery int bounceNonDelivery int
virtualDelivered int
} }
tests := []struct { tests := []struct {
name string name string
@ -197,6 +199,18 @@ func TestPostfixExporter_CollectFromLogline(t *testing.T) {
bounceNonDelivery: prometheus.NewCounter(prometheus.CounterOpts{}), 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=<me@domain.fr>, 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 { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
@ -225,6 +239,7 @@ func TestPostfixExporter_CollectFromLogline(t *testing.T) {
smtpdSASLAuthenticationFailures: tt.fields.smtpdSASLAuthenticationFailures, smtpdSASLAuthenticationFailures: tt.fields.smtpdSASLAuthenticationFailures,
smtpdTLSConnects: tt.fields.smtpdTLSConnects, smtpdTLSConnects: tt.fields.smtpdTLSConnects,
bounceNonDelivery: tt.fields.bounceNonDelivery, bounceNonDelivery: tt.fields.bounceNonDelivery,
virtualDelivered: tt.fields.virtualDelivered,
unsupportedLogEntries: tt.fields.unsupportedLogEntries, unsupportedLogEntries: tt.fields.unsupportedLogEntries,
logUnsupportedLines: true, 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.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.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.bounceNonDelivery, tt.args.bounceNonDelivery, "Wrong number of non delivery notifications")
assertCounterEquals(t, e.virtualDelivered, tt.args.virtualDelivered, "Wrong number of delivered mails")
}) })
} }
} }