forked from influxdata/telegraf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
syslog_mapper_test.go
200 lines (182 loc) · 6.12 KB
/
syslog_mapper_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
package syslog
import (
"os"
"testing"
"time"
"github.com/influxdata/telegraf/metric"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestSyslogMapperWithDefaults(t *testing.T) {
s := newSyslog()
s.initializeSyslogMapper()
// Init metrics
m1, _ := metric.New(
"testmetric",
map[string]string{},
map[string]interface{}{},
time.Date(2010, time.November, 10, 23, 0, 0, 0, time.UTC),
)
hostname, err := os.Hostname()
assert.NoError(t, err)
syslogMessage, err := s.mapper.MapMetricToSyslogMessage(m1)
require.NoError(t, err)
str, _ := syslogMessage.String()
assert.Equal(t, "<13>1 2010-11-10T23:00:00Z "+hostname+" Telegraf - testmetric -", str, "Wrong syslog message")
}
func TestSyslogMapperWithHostname(t *testing.T) {
s := newSyslog()
s.initializeSyslogMapper()
// Init metrics
m1, _ := metric.New(
"testmetric",
map[string]string{
"hostname": "testhost",
"source": "sourcevalue",
"host": "hostvalue",
},
map[string]interface{}{},
time.Date(2010, time.November, 10, 23, 0, 0, 0, time.UTC),
)
syslogMessage, err := s.mapper.MapMetricToSyslogMessage(m1)
require.NoError(t, err)
str, _ := syslogMessage.String()
assert.Equal(t, "<13>1 2010-11-10T23:00:00Z testhost Telegraf - testmetric -", str, "Wrong syslog message")
}
func TestSyslogMapperWithHostnameSourceFallback(t *testing.T) {
s := newSyslog()
s.initializeSyslogMapper()
// Init metrics
m1, _ := metric.New(
"testmetric",
map[string]string{
"source": "sourcevalue",
"host": "hostvalue",
},
map[string]interface{}{},
time.Date(2010, time.November, 10, 23, 0, 0, 0, time.UTC),
)
syslogMessage, err := s.mapper.MapMetricToSyslogMessage(m1)
require.NoError(t, err)
str, _ := syslogMessage.String()
assert.Equal(t, "<13>1 2010-11-10T23:00:00Z sourcevalue Telegraf - testmetric -", str, "Wrong syslog message")
}
func TestSyslogMapperWithHostnameHostFallback(t *testing.T) {
s := newSyslog()
s.initializeSyslogMapper()
// Init metrics
m1, _ := metric.New(
"testmetric",
map[string]string{
"host": "hostvalue",
},
map[string]interface{}{},
time.Date(2010, time.November, 10, 23, 0, 0, 0, time.UTC),
)
syslogMessage, err := s.mapper.MapMetricToSyslogMessage(m1)
require.NoError(t, err)
str, _ := syslogMessage.String()
assert.Equal(t, "<13>1 2010-11-10T23:00:00Z hostvalue Telegraf - testmetric -", str, "Wrong syslog message")
}
func TestSyslogMapperWithDefaultSdid(t *testing.T) {
s := newSyslog()
s.DefaultSdid = "default@32473"
s.initializeSyslogMapper()
// Init metrics
m1, _ := metric.New(
"testmetric",
map[string]string{
"appname": "testapp",
"hostname": "testhost",
"tag1": "bar",
"default@32473_tag2": "foobar",
},
map[string]interface{}{
"severity_code": uint64(3),
"facility_code": uint64(3),
"msg": "Test message",
"procid": uint64(25),
"version": uint16(2),
"msgid": int64(555),
"timestamp": time.Date(2010, time.November, 10, 23, 30, 0, 0, time.UTC).UnixNano(),
"value1": int64(2),
"default@32473_value2": "foo",
"value3": float64(1.2),
},
time.Date(2010, time.November, 10, 23, 0, 0, 0, time.UTC),
)
syslogMessage, err := s.mapper.MapMetricToSyslogMessage(m1)
require.NoError(t, err)
str, _ := syslogMessage.String()
assert.Equal(t, "<27>2 2010-11-10T23:30:00Z testhost testapp 25 555 [default@32473 tag1=\"bar\" tag2=\"foobar\" value1=\"2\" value2=\"foo\" value3=\"1.2\"] Test message", str, "Wrong syslog message")
}
func TestSyslogMapperWithDefaultSdidAndOtherSdids(t *testing.T) {
s := newSyslog()
s.DefaultSdid = "default@32473"
s.Sdids = []string{"bar@123", "foo@456"}
s.initializeSyslogMapper()
// Init metrics
m1, _ := metric.New(
"testmetric",
map[string]string{
"appname": "testapp",
"hostname": "testhost",
"tag1": "bar",
"default@32473_tag2": "foobar",
"bar@123_tag3": "barfoobar",
},
map[string]interface{}{
"severity_code": uint64(1),
"facility_code": uint64(3),
"msg": "Test message",
"procid": uint64(25),
"version": uint16(2),
"msgid": int64(555),
"timestamp": time.Date(2010, time.November, 10, 23, 30, 0, 0, time.UTC).UnixNano(),
"value1": int64(2),
"default@32473_value2": "default",
"bar@123_value3": int64(2),
"foo@456_value4": "foo",
},
time.Date(2010, time.November, 10, 23, 0, 0, 0, time.UTC),
)
syslogMessage, err := s.mapper.MapMetricToSyslogMessage(m1)
require.NoError(t, err)
str, _ := syslogMessage.String()
assert.Equal(t, "<25>2 2010-11-10T23:30:00Z testhost testapp 25 555 [bar@123 tag3=\"barfoobar\" value3=\"2\"][default@32473 tag1=\"bar\" tag2=\"foobar\" value1=\"2\" value2=\"default\"][foo@456 value4=\"foo\"] Test message", str, "Wrong syslog message")
}
func TestSyslogMapperWithNoSdids(t *testing.T) {
// Init mapper
s := newSyslog()
s.initializeSyslogMapper()
// Init metrics
m1, _ := metric.New(
"testmetric",
map[string]string{
"appname": "testapp",
"hostname": "testhost",
"tag1": "bar",
"default@32473_tag2": "foobar",
"bar@123_tag3": "barfoobar",
"foo@456_tag4": "foobarfoo",
},
map[string]interface{}{
"severity_code": uint64(2),
"facility_code": uint64(3),
"msg": "Test message",
"procid": uint64(25),
"version": uint16(2),
"msgid": int64(555),
"timestamp": time.Date(2010, time.November, 10, 23, 30, 0, 0, time.UTC).UnixNano(),
"value1": int64(2),
"default@32473_value2": "default",
"bar@123_value3": int64(2),
"foo@456_value4": "foo",
},
time.Date(2010, time.November, 10, 23, 0, 0, 0, time.UTC),
)
syslogMessage, err := s.mapper.MapMetricToSyslogMessage(m1)
require.NoError(t, err)
str, _ := syslogMessage.String()
assert.Equal(t, "<26>2 2010-11-10T23:30:00Z testhost testapp 25 555 - Test message", str, "Wrong syslog message")
}