forked from influxdata/telegraf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser_test.go
163 lines (148 loc) · 4.2 KB
/
parser_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
package nagios
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
const validOutput1 = `PING OK - Packet loss = 0%, RTA = 0.30 ms|rta=0.298000ms;4000.000000;6000.000000;0.000000 pl=0%;80;90;0;100
This is a long output
with three lines
`
const validOutput2 = "TCP OK - 0.008 second response time on port 80|time=0.008457s;;;0.000000;10.000000"
const validOutput3 = "TCP OK - 0.008 second response time on port 80|time=0.008457"
const validOutput4 = "OK: Load average: 0.00, 0.01, 0.05 | 'load1'=0.00;~:4;@0:6;0; 'load5'=0.01;3;0:5;0; 'load15'=0.05;0:2;0:4;0;"
const invalidOutput3 = "PING OK - Packet loss = 0%, RTA = 0.30 ms"
const invalidOutput4 = "PING OK - Packet loss = 0%, RTA = 0.30 ms| =3;;;; dgasdg =;;;; sff=;;;;"
func TestParseValidOutput(t *testing.T) {
parser := NagiosParser{
MetricName: "nagios_test",
}
// Output1
metrics, err := parser.Parse([]byte(validOutput1))
require.NoError(t, err)
require.Len(t, metrics, 2)
// rta
assert.Equal(t, "rta", metrics[0].Tags()["perfdata"])
assert.Equal(t, map[string]interface{}{
"value": float64(0.298),
"warning_lt": float64(0),
"warning_gt": float64(4000),
"critical_lt": float64(0),
"critical_gt": float64(6000),
"min": float64(0),
}, metrics[0].Fields())
assert.Equal(t, map[string]string{"unit": "ms", "perfdata": "rta"}, metrics[0].Tags())
// pl
assert.Equal(t, "pl", metrics[1].Tags()["perfdata"])
assert.Equal(t, map[string]interface{}{
"value": float64(0),
"warning_lt": float64(0),
"warning_gt": float64(80),
"critical_lt": float64(0),
"critical_gt": float64(90),
"min": float64(0),
"max": float64(100),
}, metrics[1].Fields())
assert.Equal(t, map[string]string{"unit": "%", "perfdata": "pl"}, metrics[1].Tags())
// Output2
metrics, err = parser.Parse([]byte(validOutput2))
require.NoError(t, err)
require.Len(t, metrics, 1)
// time
assert.Equal(t, "time", metrics[0].Tags()["perfdata"])
assert.Equal(t, map[string]interface{}{
"value": float64(0.008457),
"min": float64(0),
"max": float64(10),
}, metrics[0].Fields())
assert.Equal(t, map[string]string{"unit": "s", "perfdata": "time"}, metrics[0].Tags())
// Output3
metrics, err = parser.Parse([]byte(validOutput3))
require.NoError(t, err)
require.Len(t, metrics, 1)
// time
assert.Equal(t, "time", metrics[0].Tags()["perfdata"])
assert.Equal(t, map[string]interface{}{
"value": float64(0.008457),
}, metrics[0].Fields())
assert.Equal(t, map[string]string{"perfdata": "time"}, metrics[0].Tags())
// Output4
metrics, err = parser.Parse([]byte(validOutput4))
require.NoError(t, err)
require.Len(t, metrics, 3)
// load
// const validOutput4 = "OK: Load average: 0.00, 0.01, 0.05 | 'load1'=0.00;0:4;0:6;0; 'load5'=0.01;0:3;0:5;0; 'load15'=0.05;0:2;0:4;0;"
assert.Equal(t, map[string]interface{}{
"value": float64(0.00),
"warning_lt": MinFloat64,
"warning_gt": float64(4),
"critical_le": float64(0),
"critical_ge": float64(6),
"min": float64(0),
}, metrics[0].Fields())
assert.Equal(t, map[string]string{"perfdata": "load1"}, metrics[0].Tags())
}
func TestParseInvalidOutput(t *testing.T) {
parser := NagiosParser{
MetricName: "nagios_test",
}
// invalidOutput3
metrics, err := parser.Parse([]byte(invalidOutput3))
require.NoError(t, err)
require.Len(t, metrics, 0)
// invalidOutput4
metrics, err = parser.Parse([]byte(invalidOutput4))
require.NoError(t, err)
require.Len(t, metrics, 0)
}
func TestParseThreshold(t *testing.T) {
tests := []struct {
input string
eMin float64
eMax float64
eErr error
}{
{
input: "10",
eMin: 0,
eMax: 10,
eErr: nil,
},
{
input: "10:",
eMin: 10,
eMax: MaxFloat64,
eErr: nil,
},
{
input: "~:10",
eMin: MinFloat64,
eMax: 10,
eErr: nil,
},
{
input: "10:20",
eMin: 10,
eMax: 20,
eErr: nil,
},
{
input: "10:20",
eMin: 10,
eMax: 20,
eErr: nil,
},
{
input: "10:20:30",
eMin: 0,
eMax: 0,
eErr: ErrBadThresholdFormat,
},
}
for i := range tests {
min, max, err := parseThreshold(tests[i].input)
require.Equal(t, tests[i].eMin, min)
require.Equal(t, tests[i].eMax, max)
require.Equal(t, tests[i].eErr, err)
}
}