forked from corelight/suricata_exporter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_test.go
264 lines (220 loc) · 6.93 KB
/
main_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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"regexp"
"sort"
"strings"
"testing"
"github.com/prometheus/client_golang/prometheus"
dto "github.com/prometheus/client_model/go"
)
var (
descStringRe = regexp.MustCompile("fqName: \"([^\"]+)\"")
sampleCounters = map[string]interface{}{
"message": map[string]interface{}{
"uptime": 123.0,
"threads": map[string]interface{}{},
"detect": map[string]interface{}{
"engines": []interface{}{
map[string]interface{}{
"id": 0.0,
"last_reload": "2021-12-08T11:28:38.980499+0100",
"rules_loaded": 42.0,
"rules_failed": 18.0,
},
},
},
}}
)
type testMetric struct {
fqName string
type_ string
value float64
labels map[string]string
}
// Aggregate metrics by fqName and return them as testMetric instances
func aggregateMetrics(metrics []prometheus.Metric) map[string][]testMetric {
result := make(map[string][]testMetric)
for _, m := range metrics {
dm := &dto.Metric{}
m.Write(dm)
tm := testMetricFromMetric(m)
// fmt.Printf("%+v\n", tm)
result[tm.fqName] = append(result[tm.fqName], tm)
}
return result
}
// Helper converting *prometheus.Metric to something easier usable for testing.
func testMetricFromMetric(m prometheus.Metric) testMetric {
desc := m.Desc()
dm := &dto.Metric{}
m.Write(dm)
var type_ string
var value float64
if dm.Counter != nil {
type_ = "counter"
value = dm.Counter.GetValue()
} else if dm.Gauge != nil {
type_ = "gauge"
value = dm.Gauge.GetValue()
} else {
panic(fmt.Sprintf("unknown type: %v", desc.String()))
}
labels := make(map[string]string)
// Iterate over LabelPairs
for _, lp := range dm.GetLabel() {
labels[*lp.Name] = *lp.Value
}
matches := descStringRe.FindStringSubmatch(desc.String())
return testMetric{
fqName: matches[1],
type_: type_,
value: value,
labels: labels,
}
}
// Call produceMetrics with the given data and collect all produced metrics.
func produceMetricsHelper(data map[string]interface{}) []prometheus.Metric {
ch := make(chan prometheus.Metric)
finished := make(chan bool)
go func() {
produceMetrics(ch, data)
finished <- true
}()
metrics := []prometheus.Metric{}
done := false
for !done {
select {
case m := <-ch:
metrics = append(metrics, m)
case <-finished:
done = true
}
}
return metrics
}
func TestProduceMetricsRules(t *testing.T) {
metrics := produceMetricsHelper(sampleCounters)
found_rules_loaded := false
found_rules_failed := false
for _, m := range metrics {
if strings.Contains(m.Desc().String(), "suricata_detect_engine_rules_loaded") {
found_rules_loaded = true
dm := &dto.Metric{}
m.Write(dm)
expected := `label:<name:"id" value:"0" > gauge:<value:42 > `
if dm.String() != expected {
t.Errorf("Unexpected rules_loaded metric: expected=%q have=%q", expected, dm.String())
}
} else if strings.Contains(m.Desc().String(), "suricata_detect_engine_rules_failed") {
dm := &dto.Metric{}
m.Write(dm)
found_rules_failed = true
expected := `label:<name:"id" value:"0" > gauge:<value:18 > `
if dm.String() != expected {
t.Errorf("Unexpected rules_loaded metric: expected=%q have=%q", expected, dm.String())
}
}
}
if !found_rules_loaded {
t.Errorf("Failed to find suricata_detect_engine_rules_loaded metric")
}
if !found_rules_failed {
t.Errorf("Failed to find suricata_detect_engine_rules_loaded metric")
}
}
func TestProduceMetricsLastReload(t *testing.T) {
metrics := produceMetricsHelper(sampleCounters)
found_last_reload := false
for _, m := range metrics {
if strings.Contains(m.Desc().String(), "suricata_detect_engine_last_reload") {
found_last_reload = true
dm := &dto.Metric{}
m.Write(dm)
expected := `label:<name:"id" value:"0" > gauge:<value:1.638959318e+09 > `
if dm.String() != expected {
t.Errorf("Unexpected rules_loaded metric: expected=%q have=%q", expected, dm.String())
}
}
}
if !found_last_reload {
t.Errorf("Failed to find suricata_detect_engine_last_reload_timestamp_seconds metric")
}
}
func TestDump604AFPacket(t *testing.T) {
data, err := ioutil.ReadFile("./testdata/dump-counters-6.0.4-afpacket.json")
if err != nil {
log.Panicf("Unable to open file: %s", err)
}
var counters map[string]interface{}
json.Unmarshal(data, &counters)
metrics := produceMetricsHelper(counters)
agged := aggregateMetrics(metrics)
tms, ok := agged["suricata_capture_kernel_packets_total"] // test metrics
if !ok {
t.Errorf("Failed to find suricata_capture_kernel_packets metrics")
}
if len(tms) != 8 {
t.Errorf("Unexpected number of suricata_kernel_packets metrics: %v", len(tms))
}
tns := make([]string, len(tms)) // thread names
for i, tm := range tms {
tns[i] = tm.labels["thread"]
}
sort.Strings(tns)
threadNames := fmt.Sprintf("%v", tns)
if threadNames != "[W#01-wlp0s20f3 W#02-wlp0s20f3 W#03-wlp0s20f3 W#04-wlp0s20f3 W#05-wlp0s20f3 W#06-wlp0s20f3 W#07-wlp0s20f3 W#08-wlp0s20f3]" {
t.Errorf("Unexpected threadNames: %v", threadNames)
}
}
func TestDump604Netmap(t *testing.T) {
data, err := ioutil.ReadFile("./testdata/dump-counters-6.0.4-netmap.json")
if err != nil {
log.Panicf("Unable to open file: %s", err)
}
var counters map[string]interface{}
json.Unmarshal(data, &counters)
metrics := produceMetricsHelper(counters)
// This is a bit dumb because once more metrics are added this isn't
// useful, but testing individual metrics is a bit annoying.
if len(metrics) != 231 {
t.Errorf("Expected 231 metrics, got %d", len(metrics))
}
}
func TestDump604Napatech(t *testing.T) {
data, err := ioutil.ReadFile("./testdata/dump-counters-6.0.4-napatech.json")
if err != nil {
log.Panicf("Unable to open file: %s", err)
}
var counters map[string]interface{}
json.Unmarshal(data, &counters)
metrics := produceMetricsHelper(counters)
agged := aggregateMetrics(metrics)
if _, ok := agged["suricata_napatech_packets_total"]; !ok {
t.Errorf("Missing suricata_napatech_packets_total metric")
}
if _, ok := agged["suricata_napatech_bytes_total"]; !ok {
t.Errorf("Missing suricata_napatech_bytes_total metric")
}
if _, ok := agged["suricata_napatech_overflow_drop_bytes_total"]; !ok {
t.Errorf("Missing suricata_napatech_overflow_drop_bytes_total metric")
}
if _, ok := agged["suricata_napatech_overflow_drop_packets_total"]; !ok {
t.Errorf("Missing suricata_napatech_overflow_drop_packets_total metric")
}
if _, ok := agged["suricata_napatech_dispatch_host_packets_total"]; !ok {
t.Errorf("Missing suricata_napatech_dispatch_host_packets_total metric")
}
if _, ok := agged["suricata_napatech_dispatch_host_bytes_total"]; !ok {
t.Errorf("Missing suricata_napatech_dispatch_host_packets_total metric")
}
if _, ok := agged["suricata_napatech_dispatch_drop_packets_total"]; !ok {
t.Errorf("Missing suricata_napatech_dispatch_drop_packets_total metric")
}
if _, ok := agged["suricata_napatech_dispatch_drop_bytes_total"]; !ok {
t.Errorf("Missing suricata_napatech_dispatch_drop_packets_total metric")
}
}