-
Notifications
You must be signed in to change notification settings - Fork 32
/
main_test.go
339 lines (307 loc) · 10.5 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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
package main
import (
"encoding/json"
"fmt"
"github.com/griesbacher/nagflux/target/influx"
"io/ioutil"
"net/http"
"net/url"
"os"
"reflect"
"testing"
"time"
)
const (
filename = "test.gcfg"
envInflux = "NAGFLUX_TEST_INFLUX"
envLivestatus = "NAGFLUX_TEST_LIVESTATUS"
envSave = "NAGFLUX_TEST_SAVE"
databaseName = "NAGFLUX_CI_TEST"
timeout = time.Duration(30) * time.Second
)
type testData struct {
input string
output influx.SeriesValue
}
func testResult(t []testData, result []influx.SeriesValue) bool {
hits := 0
for _, testData := range t {
for _, values := range result {
if reflect.DeepEqual(values, testData.output) {
hits++
break
}
}
}
if hits == len(t) {
return true
}
for _, testData := range t {
found := false
for _, values := range result {
if reflect.DeepEqual(values, testData.output) {
found = true
break
}
}
if !found {
fmt.Fprintf(os.Stderr, "%s", fmt.Sprintln(testData.output))
for _, values := range result {
fmt.Fprintf(os.Stderr, "-- %s", fmt.Sprintln(values))
}
}
}
return false
}
var NagiosTestData1 = []testData{
//Nasty
{`DATATYPE::SERVICEPERFDATA TIMET::1 HOSTNAME::h1 SERVICEDESC::s1 SERVICEPERFDATA::C: use=1;2;3;4;5 SERVICECHECKCOMMAND::usage
`,
// [time command crit crit-fill host max min performanceLabel service value warn warn-fill]
// [1000 usage 3 none h1 5 <nil> 4 C: use s1 <nil> 1 2 none]
[]interface{}{1000.0, "usage", 3.0, "none", "h1", 5.0, nil, 4.0, `C: use`, "s1", nil, 1.0, 2.0, "none"}},
{`DATATYPE::SERVICEPERFDATA TIMET::3 HOSTNAME::h1 SERVICEDESC::s1 SERVICEPERFDATA::D:\ use=1;2;3;4;5 SERVICECHECKCOMMAND::usage
`,
// [time command crit crit-fill host max min performanceLabel service value warn warn-fill]
// [3000 usage 3 none h1 5 <nil> 4 D:\ use s1 <nil> 1 2 none]
[]interface{}{3000.0, "usage", 3.0, "none", "h1", 5.0, nil, 4.0, `D:\ use`, "s1", nil, 1.0, 2.0, "none"}},
//Normal
{`DATATYPE::SERVICEPERFDATA TIMET::2 HOSTNAME::h2 SERVICEDESC::s2 SERVICEPERFDATA::rta=2;3;4;5;6 SERVICECHECKCOMMAND::ping
`,
//[2000 ping 4 none h2 6 <nil> 5 rta s2 <nil> 2 3 none]
[]interface{}{2000.0, "ping", 4.0, "none", "h2", 6.0, nil, 5.0, "rta", "s2", nil, 2.0, 3.0, "none"}},
{`DATATYPE::SERVICEPERFDATA TIMET::1490957788 HOSTNAME::müü SERVICEDESC::möö SERVICEPERFDATA::getItinerary_min=34385µs SERVICECHECKCOMMAND::check_perfs SERVICESTATE::0 SERVICESTATETYPE::1
`,
// [time command crit crit-fill host max min performanceLabel service value warn warn-fill]
// [1.490957788e+12 check_perfs <nil> <nil> müü <nil> <nil> <nil> getItinerary_min möö µs 34385 <nil> <nil>]
[]interface{}{1490957788000.0, "check_perfs", nil, nil, "müü", nil, nil, nil, "getItinerary_min", "möö", "µs", 34385.0, nil, nil}},
}
var NagiosTestData21 = []testData{
//Database1
{`DATATYPE::SERVICEPERFDATA TIMET::4 HOSTNAME::h3 SERVICEDESC::s1 SERVICEPERFDATA::rta=2;3;4;5;6 SERVICECHECKCOMMAND::special NAGFLUX:TARGET::` + database1 + `
`, //[2000 ping 4 none h2 6 <nil> 5 rta s2 2 3 none]
[]interface{}{4000.0, "special", 4.0, "none", "h3", 6.0, nil, 5.0, "rta", "s1", nil, 2.0, 3.0, "none"}},
}
var NagiosTestData22 = []testData{
//Database2
{`DATATYPE::SERVICEPERFDATA TIMET::4 HOSTNAME::h3 SERVICEDESC::s2 SERVICEPERFDATA::rta=2;3;4;5;6 SERVICECHECKCOMMAND::special NAGFLUX:TARGET::` + database2 + `
`, //[2000 ping 4 none h2 6 <nil> 5 rta s2 2 3 none]
[]interface{}{4000.0, "special", 4.0, "none", "h3", 6.0, nil, 5.0, "rta", "s2", nil, 2.0, 3.0, "none"}},
}
var NagfluxTestData1 = []testData{
{`table&time&t_host&t_service&t_command&t_performanceLabel&f_value
metrics&10&nagflux&service1&command1&perf&20
`,
//[10 command1 <nil> <nil> nagflux <nil> <nil> <nil> perf service1 20 <nil> <nil>]
[]interface{}{10.0, "command1", nil, nil, "nagflux", nil, nil, nil, "perf", "service1", nil, 20.0, nil, nil}},
{`metrics&20&nagflux&service\ 1&command1&perf\ 1&30
`,
//[20 command1 <nil> <nil> nagflux <nil> <nil> <nil> perf\ 1 service\ 1 30 <nil> <nil>]
[]interface{}{20.0, "command1", nil, nil, "nagflux", nil, nil, nil, "perf 1", "service 1", nil, 30.0, nil, nil}},
}
var NagfluxTestData2 = []testData{
{`table&time&t_host&t_service&f_message
messages&100&nagflux&service1&"""Hallo World"""
`,
//[100 <nil> <nil> <nil> nagflux <nil> Hallo World <nil> <nil> service1 <nil> <nil> <nil>]
[]interface{}{100.0, nil, nil, nil, "nagflux", nil, "Hallo World", nil, nil, "service1", nil, nil, nil, nil}},
{`messages&300&nagflux&service1&"""Hallo \\"""
`,
//[300 <nil> <nil> <nil> nagflux <nil> Hallo \ <nil> <nil> service1 <nil> <nil> <nil>]
[]interface{}{300.0, nil, nil, nil, "nagflux", nil, `Hallo \`, nil, nil, "service1", nil, nil, nil, nil}},
}
var TestDataName = `metrics`
var TestDataColumns = []string{"time", "command", "crit", "crit-fill", "host", "max", "message", "min", "performanceLabel", "service", "unit", "value", "warn", "warn-fill"}
var influxParam string
var livestatusParam string
var save bool
var finished chan bool
var database1 = databaseName + "_1"
var database2 = databaseName + "_2"
func init() {
finished = make(chan bool)
influxParam = os.Getenv(envInflux)
if influxParam == "" {
influxParam = "http://127.0.0.1:8086"
fmt.Printf("%s is not set, using default: %s\n", envInflux, influxParam)
}
livestatusParam = os.Getenv(envLivestatus)
if livestatusParam == "" {
livestatusParam = "127.0.0.1:6557"
fmt.Printf("%s is not set, using default: %s\n", envLivestatus, livestatusParam)
}
if os.Getenv(envSave) == "" {
save = false
fmt.Println("Will NOT save the database")
} else {
save = true
fmt.Println("Will save the database")
}
}
func TestEverything(t *testing.T) {
os.Args = append(os.Args, "-configPath="+filename)
go createTestData("test/nagios/", "1.txt", NagiosTestData1)
go createTestData("test/nagios/", "2.txt", NagiosTestData21)
go createTestData("test/nagios/", "3.txt", NagiosTestData22)
go createTestData("test/nagflux/", "1.txt", NagfluxTestData1)
go createTestData("test/nagflux/", "2.txt", NagfluxTestData2)
createConfig()
dropDatabase()
go main()
time.Sleep(time.Duration(1) * time.Second)
os.Remove(filename)
go checkDatabase()
select {
case <-finished:
case <-time.After(timeout):
result1, err1 := getEverything(database1)
result2, err2 := getEverything(database2)
dropDatabase()
t.Errorf(
"Expected data was not found in the influxdb within the timerange: %s\nError: %+v\nDatabase:%+v\nError: %+v\nDatabase:%+v",
timeout, err1, result1, err2, result2,
)
}
select {
case quit <- true:
case <-time.After(time.Duration(2) * time.Second):
fmt.Println("Killing the test due to timeout")
}
dropDatabase()
}
func createTestData(folder, file string, data []testData) {
if err := os.MkdirAll(folder, 0700); err != nil {
panic(err)
}
fileData := []byte{}
for _, data := range data {
fileData = append(fileData, []byte(data.input)...)
}
if err := ioutil.WriteFile(folder+file, fileData, 0644); err != nil {
panic(err)
}
fmt.Println(string(fileData))
}
func checkDatabase() {
result := map[string]bool{
"nagiosResult1": false,
"nagiosResult21": false,
"nagiosResult22": false,
"nagfluxResult1": false,
"nagfluxResult2": false,
}
for {
time.Sleep(time.Duration(500) * time.Millisecond)
query, _ := getEverything(database1)
if query == nil || len((*query).Results) == 0 {
continue
}
result1 := (*query).Results[0]
if len(result1.Series) != 2 || TestDataName != result1.Series[1].Name || !reflect.DeepEqual(TestDataColumns, result1.Series[1].Columns) {
continue
}
query2, _ := getEverything(database2)
if query2 == nil || len((*query2).Results) == 0 {
continue
}
result2 := (*query2).Results[0]
if len(result2.Series) != 2 || TestDataName != result2.Series[1].Name || !reflect.DeepEqual(TestDataColumns, result2.Series[1].Columns) {
continue
}
if !result["nagiosResult1"] {
result["nagiosResult1"] = testResult(NagiosTestData1, result1.Series[1].Values)
}
if !result["nagiosResult21"] {
result["nagiosResult21"] = testResult(NagiosTestData21, result1.Series[1].Values)
}
if !result["nagiosResult22"] {
result["nagiosResult22"] = testResult(NagiosTestData22, result2.Series[1].Values)
}
if !result["nagfluxResult1"] {
result["nagfluxResult1"] = testResult(NagfluxTestData1, result1.Series[1].Values)
}
if !result["nagfluxResult2"] {
result["nagfluxResult2"] = testResult(NagfluxTestData2, result1.Series[0].Values)
}
completed := true
for k, v := range result {
if !v {
fmt.Println(k + " not forfilled")
completed = false
}
}
if completed {
finished <- true
return
}
}
}
func getEverything(database string) (*influx.ShowSeriesResult, error) {
resp, err := http.Get(influxParam + "/query?db=" + url.QueryEscape(database) + "&q=select%20*%20from%20/.*/&epoch=ms&u=omdadmin&p=omd")
if err != nil {
return nil, err
}
defer resp.Body.Close()
body, _ := ioutil.ReadAll(resp.Body)
if resp.StatusCode >= 200 && resp.StatusCode < 300 {
var jsonResult influx.ShowSeriesResult
json.Unmarshal(body, &jsonResult)
return &jsonResult, nil
}
return nil, fmt.Errorf("Database query(%s) returned: %s", resp.Request.URL, resp.Status)
}
func dropDatabase() {
if !save {
http.Get(influxParam + "/query?u=omdadmin&p=omd&q=drop%20database%20" + url.QueryEscape(database1))
http.Get(influxParam + "/query?u=omdadmin&p=omd&q=drop%20database%20" + url.QueryEscape(database2))
}
}
func createConfig() {
config := []byte(fmt.Sprintf(`
[main]
NagiosSpoolfileFolder = "test/nagios"
NagiosSpoolfileWorker = 1
InfluxWorker = 2
MaxInfluxWorker = 5
DumpFile = "test/nagflux.dump"
NagfluxSpoolfileFolder = "test/nagflux"
FieldSeparator = "&"
FileBufferSize = 65536
BufferSize = 10000
[Log]
LogFile = ""
MinSeverity = "DEBUG"
[Monitoring]
PrometheusAddress = ""
[InfluxDBGlobal]
CreateDatabaseIfNotExists = true
NastyString = ""
NastyStringToReplace = ""
HostcheckAlias = "hostcheck"
[InfluxDB "%s"]
Enabled = true
Version = 1.0
Address = "%s"
Arguments = "precision=ms&db=%s&u=omdadmin&p=omd"
StopPullingDataIfDown = true
[InfluxDB "%s"]
Enabled = true
Version = 1.0
Address = "%s"
Arguments = "precision=ms&db=%s&u=omdadmin&p=omd"
StopPullingDataIfDown = true
[InfluxDB "broken"]
Enabled = true
Version = 1.0
Address = "http://127.0.0.1:1"
Arguments = "precision=ms&db=broken&u=omdadmin&p=123456"
StopPullingDataIfDown = false
[Livestatus]
Type = "tcp"
Address = "%s"
MinutesToWait = 0
`, database1, influxParam, database1, database2, influxParam, database2, livestatusParam))
if err := ioutil.WriteFile(filename, config, 0644); err != nil {
panic(err)
}
}