forked from SumoLogic/telegraf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
port_name.go
234 lines (202 loc) · 5.21 KB
/
port_name.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
package portname
import (
"bufio"
"io"
"os"
"strconv"
"strings"
"github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/plugins/processors"
)
var sampleConfig = `
[[processors.port_name]]
## Name of tag holding the port number
# tag = "port"
## Or name of the field holding the port number
# field = "port"
## Name of output tag or field (depending on the source) where service name will be added
# dest = "service"
## Default tcp or udp
# default_protocol = "tcp"
## Tag containing the protocol (tcp or udp, case-insensitive)
# protocol_tag = "proto"
## Field containing the protocol (tcp or udp, case-insensitive)
# protocol_field = "proto"
`
type sMap map[string]map[int]string // "https" == services["tcp"][443]
var services sMap
type PortName struct {
SourceTag string `toml:"tag"`
SourceField string `toml:"field"`
Dest string `toml:"dest"`
DefaultProtocol string `toml:"default_protocol"`
ProtocolTag string `toml:"protocol_tag"`
ProtocolField string `toml:"protocol_field"`
Log telegraf.Logger `toml:"-"`
}
func (d *PortName) SampleConfig() string {
return sampleConfig
}
func (d *PortName) Description() string {
return "Given a tag/field of a TCP or UDP port number, add a tag/field of the service name looked up in the system services file"
}
func readServicesFile() {
file, err := os.Open(servicesPath())
if err != nil {
return
}
defer file.Close()
services = readServices(file)
}
// Read the services file into a map.
//
// This function takes a similar approach to parsing as the go
// standard library (see src/net/port_unix.go in golang source) but
// maps protocol and port number to service name, not protocol and
// service to port number.
func readServices(r io.Reader) sMap {
services = make(sMap)
scanner := bufio.NewScanner(r)
for scanner.Scan() {
line := scanner.Text()
// "http 80/tcp www www-http # World Wide Web HTTP"
if i := strings.Index(line, "#"); i >= 0 {
line = line[:i]
}
f := strings.Fields(line)
if len(f) < 2 {
continue
}
service := f[0] // "http"
portProto := f[1] // "80/tcp"
portProtoSlice := strings.SplitN(portProto, "/", 2)
if len(portProtoSlice) < 2 {
continue
}
port, err := strconv.Atoi(portProtoSlice[0]) // "80"
if err != nil || port <= 0 {
continue
}
proto := portProtoSlice[1] // "tcp"
proto = strings.ToLower(proto)
protoMap, ok := services[proto]
if !ok {
protoMap = make(map[int]string)
services[proto] = protoMap
}
protoMap[port] = service
}
return services
}
func (d *PortName) Apply(metrics ...telegraf.Metric) []telegraf.Metric {
for _, m := range metrics {
var portProto string
var fromField bool
if len(d.SourceTag) > 0 {
if tag, ok := m.GetTag(d.SourceTag); ok {
portProto = string([]byte(tag))
}
}
if len(d.SourceField) > 0 {
if field, ok := m.GetField(d.SourceField); ok {
switch v := field.(type) {
default:
d.Log.Errorf("Unexpected type %t in source field; must be string or int", v)
continue
case int64:
portProto = strconv.FormatInt(v, 10)
case uint64:
portProto = strconv.FormatUint(v, 10)
case string:
portProto = v
}
fromField = true
}
}
if len(portProto) == 0 {
continue
}
portProtoSlice := strings.SplitN(portProto, "/", 2)
l := len(portProtoSlice)
if l == 0 {
// Empty tag
d.Log.Errorf("empty port tag: %v", d.SourceTag)
continue
}
var port int
if l > 0 {
var err error
val := portProtoSlice[0]
port, err = strconv.Atoi(val)
if err != nil {
// Can't convert port to string
d.Log.Errorf("error converting port to integer: %v", val)
continue
}
}
proto := d.DefaultProtocol
if l > 1 && len(portProtoSlice[1]) > 0 {
proto = portProtoSlice[1]
}
if len(d.ProtocolTag) > 0 {
if tag, ok := m.GetTag(d.ProtocolTag); ok {
proto = tag
}
}
if len(d.ProtocolField) > 0 {
if field, ok := m.GetField(d.ProtocolField); ok {
switch v := field.(type) {
default:
d.Log.Errorf("Unexpected type %t in protocol field; must be string", v)
continue
case string:
proto = v
}
}
}
proto = strings.ToLower(proto)
protoMap, ok := services[proto]
if !ok {
// Unknown protocol
//
// Protocol is normally tcp or udp. The services file
// normally has entries for both, so our map does too. If
// not, it's very likely the source tag or the services
// file doesn't make sense.
d.Log.Errorf("protocol not found in services map: %v", proto)
continue
}
service, ok := protoMap[port]
if !ok {
// Unknown port
//
// Not all ports are named so this isn't an error, but
// it's helpful to know when debugging.
d.Log.Debugf("port not found in services map: %v", port)
continue
}
if fromField {
m.AddField(d.Dest, service)
} else {
m.AddTag(d.Dest, service)
}
}
return metrics
}
func (h *PortName) Init() error {
services = make(sMap)
readServicesFile()
return nil
}
func init() {
processors.Add("port_name", func() telegraf.Processor {
return &PortName{
SourceTag: "port",
SourceField: "port",
Dest: "service",
DefaultProtocol: "tcp",
ProtocolTag: "proto",
ProtocolField: "proto",
}
})
}