forked from Griesbacher/nagflux
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_test.go
251 lines (226 loc) · 7.21 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
package main
import (
"encoding/json"
"fmt"
"github.com/griesbacher/nagflux/target/influx"
"io/ioutil"
"net/http"
"net/url"
"os"
"reflect"
"testing"
"time"
)
const (
filename = "config.gcfg"
envInflux = "NAGFLUX_TEST_INFLUX"
envLivestatus = "NAGFLUX_TEST_LIVESTATUS"
envSave = "NAGFLUX_TEST_SAVE"
databaseName = "NAGFLUX_CI_TEST"
timeout = time.Duration(20) * 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
}
}
}
return hits == len(t)
}
var NagiosTestData = []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 1 2 none]
[]interface{}{1000.0, "usage", 3.0, "none", "h1", 5.0, nil, 4.0, `C: use`, "s1", 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 1 2 none]
[]interface{}{3000.0, "usage", 3.0, "none", "h1", 5.0, nil, 4.0, `D:\ use`, "s1", 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 2 3 none]
[]interface{}{2000.0, "ping", 4.0, "none", "h2", 6.0, nil, 5.0, "rta", "s2", 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", 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", 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}},
{`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}},
}
var TestDataName = `metrics`
var TestDataColumns = []string{"time", "command", "crit", "crit-fill", "host", "max", "message", "min", "performanceLabel", "service", "value", "warn", "warn-fill"}
var OldConfig string
var influxParam string
var livestatusParam string
var save bool
var finished chan bool
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 = true
fmt.Println("Will save the database")
}
}
func TestEverything(t *testing.T) {
go createTestData("test/nagios/", "1.txt", NagiosTestData)
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)
restoreConfig()
go checkDatabase()
select {
case <-finished:
case <-time.After(timeout):
result, err := getEverything()
t.Errorf("Expected data was not found in the influxdb within the timerange: %s\nError: %+v\nDatabase:%+v", timeout, err, result)
}
quit <- true
if !save {
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() {
nagiosResult := false
nagfluxResult1 := false
nagfluxResult2 := false
for {
time.Sleep(time.Duration(500) * time.Millisecond)
query, _ := getEverything()
if len((*query).Results) == 0 {
continue
}
result := (*query).Results[0]
if len(result.Series) != 2 || TestDataName != result.Series[1].Name || !reflect.DeepEqual(TestDataColumns, result.Series[1].Columns) {
continue
}
fmt.Println(result.Series[0].Values)
fmt.Println(result.Series[1].Values)
if !nagiosResult {
nagiosResult = testResult(NagiosTestData, result.Series[1].Values)
}
if !nagfluxResult1 {
nagfluxResult1 = testResult(NagfluxTestData1, result.Series[1].Values)
}
if !nagfluxResult2 {
nagfluxResult2 = testResult(NagfluxTestData2, result.Series[0].Values)
}
fmt.Println(nagiosResult, nagfluxResult1, nagfluxResult2)
if nagiosResult && nagfluxResult1 && nagfluxResult2 {
finished <- true
return
}
}
}
func getEverything() (*influx.ShowSeriesResult, error) {
resp, err := http.Get(influxParam + "/query?db=" + url.QueryEscape(databaseName) + "&q=select%20*%20from%20/.*/&epoch=ms")
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() {
http.Get(influxParam + "/query?q=drop%20database%20" + url.QueryEscape(databaseName))
}
func createConfig() {
old, _ := ioutil.ReadFile(filename)
OldConfig = string(old)
config := []byte(fmt.Sprintf(`
[main]
NagiosSpoolfileFolder = "test/nagios"
NagiosSpoolfileWorker = 1
InfluxWorker = 2
MaxInfluxWorker = 5
DumpFile = "nagflux.dump"
NagfluxSpoolfileFolder = "test/nagflux"
FieldSeparator = "&"
[Log]
LogFile = ""
MinSeverity = "WARN"
[Monitoring]
WebserverPort = ""
[Influx]
Enabled = true
Version = 0.9
Address = "%s"
Arguments = "precision=ms&db=%s"
CreateDatabaseIfNotExists = true
NastyString = ""
NastyStringToReplace = ""
[Livestatus]
Type = "tcp"
Address = "%s"
[Elasticsearch]
Enabled = false
Address = "http://localhost:9200"
Index = "nagflux"
Version = 2.1
`, influxParam, databaseName, livestatusParam))
if err := ioutil.WriteFile(filename, config, 0644); err != nil {
panic(err)
}
}
func restoreConfig() {
if err := ioutil.WriteFile(filename, []byte(OldConfig), 0644); err != nil {
panic(err)
}
}