forked from influxdata/telegraf
-
Notifications
You must be signed in to change notification settings - Fork 5
/
syslog.go
249 lines (218 loc) · 7.17 KB
/
syslog.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
package syslog
import (
"crypto/tls"
"fmt"
"log"
"net"
"strconv"
"strings"
"github.com/influxdata/go-syslog/v2/nontransparent"
"github.com/influxdata/go-syslog/v2/rfc5424"
"github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/internal"
framing "github.com/influxdata/telegraf/internal/syslog"
tlsint "github.com/influxdata/telegraf/internal/tls"
"github.com/influxdata/telegraf/plugins/outputs"
)
type Syslog struct {
Address string
KeepAlivePeriod *internal.Duration
DefaultSdid string
DefaultSeverityCode uint8
DefaultFacilityCode uint8
DefaultAppname string
Sdids []string
Separator string `toml:"sdparam_separator"`
Framing framing.Framing
Trailer nontransparent.TrailerType
net.Conn
tlsint.ClientConfig
mapper *SyslogMapper
}
var sampleConfig = `
## URL to connect to
## ex: address = "tcp://127.0.0.1:8094"
## ex: address = "tcp4://127.0.0.1:8094"
## ex: address = "tcp6://127.0.0.1:8094"
## ex: address = "tcp6://[2001:db8::1]:8094"
## ex: address = "udp://127.0.0.1:8094"
## ex: address = "udp4://127.0.0.1:8094"
## ex: address = "udp6://127.0.0.1:8094"
address = "tcp://127.0.0.1:8094"
## Optional TLS Config
# tls_ca = "/etc/telegraf/ca.pem"
# tls_cert = "/etc/telegraf/cert.pem"
# tls_key = "/etc/telegraf/key.pem"
## Use TLS but skip chain & host verification
# insecure_skip_verify = false
## Period between keep alive probes.
## Only applies to TCP sockets.
## 0 disables keep alive probes.
## Defaults to the OS configuration.
# keep_alive_period = "5m"
## The framing technique with which it is expected that messages are
## transported (default = "octet-counting"). Whether the messages come
## using the octect-counting (RFC5425#section-4.3.1, RFC6587#section-3.4.1),
## or the non-transparent framing technique (RFC6587#section-3.4.2). Must
## be one of "octet-counting", "non-transparent".
# framing = "octet-counting"
## The trailer to be expected in case of non-trasparent framing (default = "LF").
## Must be one of "LF", or "NUL".
# trailer = "LF"
## SD-PARAMs settings
## Syslog messages can contain key/value pairs within zero or more
## structured data sections. For each unrecognised metric tag/field a
## SD-PARAMS is created.
##
## Example:
## [[outputs.syslog]]
## sdparam_separator = "_"
## default_sdid = "default@32473"
## sdids = ["foo@123", "bar@456"]
##
## input => xyzzy,x=y foo@123_value=42,bar@456_value2=84,something_else=1
## output (structured data only) => [foo@123 value=42][bar@456 value2=84][default@32473 something_else=1 x=y]
## SD-PARAMs separator between the sdid and tag/field key (default = "_")
# sdparam_separator = "_"
## Default sdid used for tags/fields that don't contain a prefix defined in
## the explict sdids setting below If no default is specified, no SD-PARAMs
## will be used for unrecognised field.
# default_sdid = "default@32473"
## List of explicit prefixes to extract from tag/field keys and use as the
## SDID, if they match (see above example for more details):
# sdids = ["foo@123", "bar@456"]
## Default severity value. Severity and Facility are used to calculate the
## message PRI value (RFC5424#section-6.2.1). Used when no metric field
## with key "severity_code" is defined. If unset, 5 (notice) is the default
# default_severity_code = 5
## Default facility value. Facility and Severity are used to calculate the
## message PRI value (RFC5424#section-6.2.1). Used when no metric field with
## key "facility_code" is defined. If unset, 1 (user-level) is the default
# default_facility_code = 1
## Default APP-NAME value (RFC5424#section-6.2.5)
## Used when no metric tag with key "appname" is defined.
## If unset, "Telegraf" is the default
# default_appname = "Telegraf"
`
func (s *Syslog) Connect() error {
s.initializeSyslogMapper()
spl := strings.SplitN(s.Address, "://", 2)
if len(spl) != 2 {
return fmt.Errorf("invalid address: %s", s.Address)
}
tlsCfg, err := s.ClientConfig.TLSConfig()
if err != nil {
return err
}
var c net.Conn
if tlsCfg == nil {
c, err = net.Dial(spl[0], spl[1])
} else {
c, err = tls.Dial(spl[0], spl[1], tlsCfg)
}
if err != nil {
return err
}
if err := s.setKeepAlive(c); err != nil {
log.Printf("unable to configure keep alive (%s): %s", s.Address, err)
}
s.Conn = c
return nil
}
func (s *Syslog) setKeepAlive(c net.Conn) error {
if s.KeepAlivePeriod == nil {
return nil
}
tcpc, ok := c.(*net.TCPConn)
if !ok {
return fmt.Errorf("cannot set keep alive on a %s socket", strings.SplitN(s.Address, "://", 2)[0])
}
if s.KeepAlivePeriod.Duration == 0 {
return tcpc.SetKeepAlive(false)
}
if err := tcpc.SetKeepAlive(true); err != nil {
return err
}
return tcpc.SetKeepAlivePeriod(s.KeepAlivePeriod.Duration)
}
func (s *Syslog) Close() error {
if s.Conn == nil {
return nil
}
err := s.Conn.Close()
s.Conn = nil
return err
}
func (s *Syslog) SampleConfig() string {
return sampleConfig
}
func (s *Syslog) Description() string {
return "Configuration for Syslog server to send metrics to"
}
func (s *Syslog) Write(metrics []telegraf.Metric) (err error) {
if s.Conn == nil {
// previous write failed with permanent error and socket was closed.
if err = s.Connect(); err != nil {
return err
}
}
for _, metric := range metrics {
var msg *rfc5424.SyslogMessage
if msg, err = s.mapper.MapMetricToSyslogMessage(metric); err != nil {
log.Printf("E! [outputs.syslog] Failed to create syslog message: %v", err)
continue
}
var msgBytesWithFraming []byte
if msgBytesWithFraming, err = s.getSyslogMessageBytesWithFraming(msg); err != nil {
log.Printf("E! [outputs.syslog] Failed to convert syslog message with framing: %v", err)
continue
}
if _, err = s.Conn.Write(msgBytesWithFraming); err != nil {
if netErr, ok := err.(net.Error); !ok || !netErr.Temporary() {
s.Close()
s.Conn = nil
return fmt.Errorf("closing connection: %v", netErr)
}
return err
}
}
return nil
}
func (s *Syslog) getSyslogMessageBytesWithFraming(msg *rfc5424.SyslogMessage) ([]byte, error) {
var msgString string
var err error
if msgString, err = msg.String(); err != nil {
return nil, err
}
msgBytes := []byte(msgString)
if s.Framing == framing.OctetCounting {
return append([]byte(strconv.Itoa(len(msgBytes))+" "), msgBytes...), nil
}
// Non-transparent framing
return append(msgBytes, byte(s.Trailer)), nil
}
func (s *Syslog) initializeSyslogMapper() {
if s.mapper != nil {
return
}
s.mapper = newSyslogMapper()
s.mapper.DefaultFacilityCode = s.DefaultFacilityCode
s.mapper.DefaultSeverityCode = s.DefaultSeverityCode
s.mapper.DefaultAppname = s.DefaultAppname
s.mapper.Separator = s.Separator
s.mapper.DefaultSdid = s.DefaultSdid
s.mapper.Sdids = s.Sdids
}
func newSyslog() *Syslog {
return &Syslog{
Framing: framing.OctetCounting,
Trailer: nontransparent.LF,
Separator: "_",
DefaultSeverityCode: uint8(5), // notice
DefaultFacilityCode: uint8(1), // user-level
DefaultAppname: "Telegraf",
}
}
func init() {
outputs.Add("syslog", func() telegraf.Output { return newSyslog() })
}