forked from kumina/postfix_exporter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
postfix_exporter_test.go
243 lines (239 loc) · 10.5 KB
/
postfix_exporter_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
package main
import (
"testing"
"github.com/prometheus/client_golang/prometheus"
io_prometheus_client "github.com/prometheus/client_model/go"
"github.com/stretchr/testify/assert"
)
func TestPostfixExporter_CollectFromLogline(t *testing.T) {
type fields struct {
showqPath string
logSrc LogSource
cleanupProcesses prometheus.Counter
cleanupRejects prometheus.Counter
cleanupNotAccepted prometheus.Counter
lmtpDelays *prometheus.HistogramVec
pipeDelays *prometheus.HistogramVec
qmgrInsertsNrcpt prometheus.Histogram
qmgrInsertsSize prometheus.Histogram
qmgrRemoves prometheus.Counter
smtpDelays *prometheus.HistogramVec
smtpTLSConnects *prometheus.CounterVec
smtpDeferreds prometheus.Counter
smtpdConnects prometheus.Counter
smtpdDisconnects prometheus.Counter
smtpdFCrDNSErrors prometheus.Counter
smtpdLostConnections *prometheus.CounterVec
smtpdProcesses *prometheus.CounterVec
smtpdRejects *prometheus.CounterVec
smtpdSASLAuthenticationFailures prometheus.Counter
smtpdTLSConnects *prometheus.CounterVec
unsupportedLogEntries *prometheus.CounterVec
}
type args struct {
line []string
removedCount int
saslFailedCount int
outgoingTLS int
smtpdMessagesProcessed int
}
tests := []struct {
name string
fields fields
args args
}{
{
name: "Single line",
args: args{
line: []string{
"Feb 11 16:49:24 letterman postfix/qmgr[8204]: AAB4D259B1: removed",
},
removedCount: 1,
saslFailedCount: 0,
},
fields: fields{
qmgrRemoves: prometheus.NewCounter(prometheus.CounterOpts{}),
unsupportedLogEntries: prometheus.NewCounterVec(prometheus.CounterOpts{}, []string{"process"}),
},
},
{
name: "Multiple lines",
args: args{
line: []string{
"Feb 11 16:49:24 letterman postfix/qmgr[8204]: AAB4D259B1: removed",
"Feb 11 16:49:24 letterman postfix/qmgr[8204]: C2032259E6: removed",
"Feb 11 16:49:24 letterman postfix/qmgr[8204]: B83C4257DC: removed",
"Feb 11 16:49:24 letterman postfix/qmgr[8204]: 721BE256EA: removed",
"Feb 11 16:49:25 letterman postfix/qmgr[8204]: CA94A259EB: removed",
"Feb 11 16:49:25 letterman postfix/qmgr[8204]: AC1E3259E1: removed",
"Feb 11 16:49:25 letterman postfix/qmgr[8204]: D114D221E3: removed",
"Feb 11 16:49:25 letterman postfix/qmgr[8204]: A55F82104D: removed",
"Feb 11 16:49:25 letterman postfix/qmgr[8204]: D6DAA259BC: removed",
"Feb 11 16:49:25 letterman postfix/qmgr[8204]: E3908259F0: removed",
"Feb 11 16:49:25 letterman postfix/qmgr[8204]: 0CBB8259BF: removed",
"Feb 11 16:49:25 letterman postfix/qmgr[8204]: EA3AD259F2: removed",
"Feb 11 16:49:25 letterman postfix/qmgr[8204]: DDEF824B48: removed",
"Feb 11 16:49:26 letterman postfix/qmgr[8204]: 289AF21DB9: removed",
"Feb 11 16:49:26 letterman postfix/qmgr[8204]: 6192B260E8: removed",
"Feb 11 16:49:26 letterman postfix/qmgr[8204]: F2831259F4: removed",
"Feb 11 16:49:26 letterman postfix/qmgr[8204]: 09D60259F8: removed",
"Feb 11 16:49:26 letterman postfix/qmgr[8204]: 13A19259FA: removed",
"Feb 11 16:49:26 letterman postfix/qmgr[8204]: 2D42722065: removed",
"Feb 11 16:49:26 letterman postfix/qmgr[8204]: 746E325A0E: removed",
"Feb 11 16:49:26 letterman postfix/qmgr[8204]: 4D2F125A02: removed",
"Feb 11 16:49:26 letterman postfix/qmgr[8204]: E30BC259EF: removed",
"Feb 11 16:49:26 letterman postfix/qmgr[8204]: DC88924DA1: removed",
"Feb 11 16:49:26 letterman postfix/qmgr[8204]: 2164B259FD: removed",
"Feb 11 16:49:26 letterman postfix/qmgr[8204]: 8C30525A14: removed",
"Feb 11 16:49:26 letterman postfix/qmgr[8204]: 8DCCE25A15: removed",
"Feb 11 16:49:26 letterman postfix/qmgr[8204]: C5217255D5: removed",
"Feb 11 16:49:27 letterman postfix/qmgr[8204]: D8EE625A28: removed",
"Feb 11 16:49:27 letterman postfix/qmgr[8204]: 9AD7C25A19: removed",
"Feb 11 16:49:27 letterman postfix/qmgr[8204]: D0EEE2596C: removed",
"Feb 11 16:49:27 letterman postfix/qmgr[8204]: DFE732172E: removed",
},
removedCount: 31,
saslFailedCount: 0,
},
fields: fields{
qmgrRemoves: prometheus.NewCounter(prometheus.CounterOpts{}),
unsupportedLogEntries: prometheus.NewCounterVec(prometheus.CounterOpts{}, []string{"process"}),
},
},
{
name: "SASL Failed",
args: args{
line: []string{
"Apr 26 10:55:19 tcc1 postfix/smtpd[21126]: warning: SASL authentication failure: cannot connect to saslauthd server: Permission denied",
"Apr 26 10:55:19 tcc1 postfix/smtpd[21126]: warning: SASL authentication failure: Password verification failed",
"Apr 26 10:55:19 tcc1 postfix/smtpd[21126]: warning: laptop.local[192.168.1.2]: SASL PLAIN authentication failed: generic failure",
},
saslFailedCount: 1,
removedCount: 0,
},
fields: fields{
smtpdSASLAuthenticationFailures: prometheus.NewCounter(prometheus.CounterOpts{}),
unsupportedLogEntries: prometheus.NewCounterVec(prometheus.CounterOpts{}, []string{"process"}),
},
},
{
name: "SASL login",
args: args{
line: []string{
"Oct 30 13:19:26 mailgw-out1 postfix/smtpd[27530]: EB4B2C19E2: client=xxx[1.2.3.4], sasl_method=PLAIN, sasl_username=user@domain",
"Feb 24 16:42:00 letterman postfix/smtpd[24906]: 1CF582025C: client=xxx[2.3.4.5]",
},
removedCount: 0,
saslFailedCount: 0,
outgoingTLS: 0,
smtpdMessagesProcessed: 2,
},
fields: fields{
unsupportedLogEntries: prometheus.NewCounterVec(prometheus.CounterOpts{}, []string{"process"}),
smtpdProcesses: prometheus.NewCounterVec(prometheus.CounterOpts{}, []string{"sasl_method"}),
},
},
{
name: "Issue #35",
args: args{
line: []string{
"Jul 24 04:38:17 mail postfix/smtp[30582]: Verified TLS connection established to gmail-smtp-in.l.google.com[108.177.14.26]:25: TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256",
"Jul 24 03:28:15 mail postfix/smtp[24052]: Verified TLS connection established to mx2.comcast.net[2001:558:fe21:2a::6]:25: TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)",
},
removedCount: 0,
saslFailedCount: 0,
outgoingTLS: 2,
},
fields: fields{
unsupportedLogEntries: prometheus.NewCounterVec(prometheus.CounterOpts{}, []string{"process"}),
smtpTLSConnects: prometheus.NewCounterVec(prometheus.CounterOpts{}, []string{"Verified", "TLSv1.2", "ECDHE-RSA-AES256-GCM-SHA384", "256", "256"}),
},
},
{
name: "Testing delays",
args: args{
line: []string{
"Feb 24 16:18:40 letterman postfix/smtp[59649]: 5270320179: to=<[email protected]>, relay=mail.telia.com[81.236.60.210]:25, delay=2017, delays=0.1/2017/0.03/0.05, dsn=2.0.0, status=sent (250 2.0.0 6FVIjIMwUJwU66FVIjAEB0 mail accepted for delivery)",
},
removedCount: 0,
saslFailedCount: 0,
outgoingTLS: 0,
smtpdMessagesProcessed: 0,
},
fields: fields{
smtpDelays: prometheus.NewHistogramVec(prometheus.HistogramOpts{}, []string{"stage"}),
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
e := &PostfixExporter{
showqPath: tt.fields.showqPath,
logSrc: tt.fields.logSrc,
cleanupProcesses: tt.fields.cleanupProcesses,
cleanupRejects: tt.fields.cleanupRejects,
cleanupNotAccepted: tt.fields.cleanupNotAccepted,
lmtpDelays: tt.fields.lmtpDelays,
pipeDelays: tt.fields.pipeDelays,
qmgrInsertsNrcpt: tt.fields.qmgrInsertsNrcpt,
qmgrInsertsSize: tt.fields.qmgrInsertsSize,
qmgrRemoves: tt.fields.qmgrRemoves,
smtpDelays: tt.fields.smtpDelays,
smtpTLSConnects: tt.fields.smtpTLSConnects,
smtpDeferreds: tt.fields.smtpDeferreds,
smtpdConnects: tt.fields.smtpdConnects,
smtpdDisconnects: tt.fields.smtpdDisconnects,
smtpdFCrDNSErrors: tt.fields.smtpdFCrDNSErrors,
smtpdLostConnections: tt.fields.smtpdLostConnections,
smtpdProcesses: tt.fields.smtpdProcesses,
smtpdRejects: tt.fields.smtpdRejects,
smtpdSASLAuthenticationFailures: tt.fields.smtpdSASLAuthenticationFailures,
smtpdTLSConnects: tt.fields.smtpdTLSConnects,
unsupportedLogEntries: tt.fields.unsupportedLogEntries,
logUnsupportedLines: true,
}
for _, line := range tt.args.line {
e.CollectFromLogLine(line)
}
assertCounterEquals(t, e.qmgrRemoves, tt.args.removedCount, "Wrong number of 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")
})
}
}
func assertCounterEquals(t *testing.T, counter prometheus.Collector, expected int, message string) {
if counter != nil && expected > 0 {
switch counter.(type) {
case *prometheus.CounterVec:
counter := counter.(*prometheus.CounterVec)
metricsChan := make(chan prometheus.Metric)
go func() {
counter.Collect(metricsChan)
close(metricsChan)
}()
var count int = 0
for metric := range metricsChan {
metricDto := io_prometheus_client.Metric{}
metric.Write(&metricDto)
count += int(*metricDto.Counter.Value)
}
assert.Equal(t, expected, count, message)
case prometheus.Counter:
metricsChan := make(chan prometheus.Metric)
go func() {
counter.Collect(metricsChan)
close(metricsChan)
}()
var count int = 0
for metric := range metricsChan {
metricDto := io_prometheus_client.Metric{}
metric.Write(&metricDto)
count += int(*metricDto.Counter.Value)
}
assert.Equal(t, expected, count, message)
default:
t.Fatal("Type not implemented")
}
}
}