forked from kimor79/gollectd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gollectd.go
339 lines (280 loc) · 7.24 KB
/
gollectd.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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
// Parse data from the collectd network plugin
// This is modeled after collectd's own network.c
package gollectd
import (
"bytes"
"encoding/binary"
"errors"
"fmt"
"io/ioutil"
"strings"
)
const (
TypeCounter = 0
TypeGauge = 1
TypeDerive = 2
TypeAbsolute = 3
)
const (
// Values taken from commit 633c3966f7 of
// https://github.com/collectd/collectd/commits/master/src/network.h
ParseHost = 0x0000
ParseTime = 0x0001
ParsePlugin = 0x0002
ParsePluginInstance = 0x0003
ParseType = 0x0004
ParseTypeInstance = 0x0005
ParseValues = 0x0006
ParseInterval = 0x0007
ParseTimeHR = 0x0008
ParseIntervalHR = 0x0009
// Notifications
ParseMessage = 0x0100
ParseSeverity = 0x0101
ParseSignature = 0x0200
ParseEncryption = 0x0210
)
var ErrorInvalid = errors.New("Invalid packet")
var ErrorUnsupported = errors.New("Unsupported packet")
var ErrorUnknownType = errors.New("Unknown value type")
var ErrorUnknownDataType = errors.New("Unknown data source type")
var ValueTypeNames = map[string]uint8{
"absolute": TypeAbsolute,
"counter": TypeCounter,
"derive": TypeDerive,
"gauge": TypeGauge,
}
var ValueTypeValues = map[uint8]string{
TypeAbsolute: "absolute",
TypeCounter: "counter",
TypeDerive: "derive",
TypeGauge: "gauge",
}
type Packet struct {
Hostname string
Interval uint64
IntervalHR uint64
Plugin string
PluginInstance string
Time uint64
TimeHR uint64
Type string
TypeInstance string
Values []Value
}
type Type struct {
Name string
Type uint8
Min string
Max string
}
type Types map[string][]*Type
type Value struct {
Name string
Type uint8
TypeName string
Value float64
}
func (p Packet) FormatName() string {
metricName := p.Hostname + "/" + p.Plugin
if len(p.PluginInstance) > 0 {
metricName += "-" + p.PluginInstance
}
metricName += "/" + p.Type
if len(p.TypeInstance) > 0 {
metricName += "-" + p.TypeInstance
}
return metricName
}
func Packets(b []byte, types Types) (*[]Packet, error) {
packets := make([]Packet, 0)
buf := bytes.NewBuffer(b)
var packetHeader struct {
PartType uint16
PartLength uint16
}
var err error
var packet Packet
var time uint64
var valueCount uint16
var valueTypes []uint8
for buf.Len() > 0 {
packetHeader.PartType = binary.BigEndian.Uint16(buf.Next(2))
packetHeader.PartLength = binary.BigEndian.Uint16(buf.Next(2))
if packetHeader.PartLength < 5 {
return nil, ErrorInvalid
}
nextPos := int(packetHeader.PartLength) - 4
partBytes := buf.Next(nextPos)
if len(partBytes) < nextPos {
return nil, ErrorInvalid
}
partBuffer := bytes.NewBuffer(partBytes)
switch packetHeader.PartType {
case ParseEncryption:
return nil, ErrorUnsupported
case ParseHost:
str := partBuffer.String()
packet.Hostname = str[0 : len(str)-1]
case ParseInterval:
err = binary.Read(partBuffer, binary.BigEndian, &time)
if err != nil {
return nil, err
}
packet.Interval = time
case ParseIntervalHR:
err = binary.Read(partBuffer, binary.BigEndian, &time)
if err != nil {
return nil, err
}
packet.IntervalHR = time
case ParseMessage:
// ignore (notification)
case ParsePlugin:
str := partBuffer.String()
packet.Plugin = str[0 : len(str)-1]
case ParsePluginInstance:
str := partBuffer.String()
packet.PluginInstance = str[0 : len(str)-1]
case ParseSeverity:
// ignore (notification)
case ParseSignature:
return nil, ErrorUnsupported
case ParseTime:
err = binary.Read(partBuffer, binary.BigEndian, &time)
if err != nil {
return nil, err
}
packet.Time = time
case ParseTimeHR:
err = binary.Read(partBuffer, binary.BigEndian, &time)
if err != nil {
return nil, err
}
packet.TimeHR = time
case ParseType:
str := partBuffer.String()
packet.Type = str[0 : len(str)-1]
case ParseTypeInstance:
str := partBuffer.String()
packet.TypeInstance = str[0 : len(str)-1]
case ParseValues:
err = binary.Read(partBuffer, binary.BigEndian, &valueCount)
if err != nil {
return nil, err
}
valueTypes = make([]uint8, valueCount, valueCount)
packet.Values = make([]Value, valueCount, valueCount)
var packetValue Value
for i, t := range valueTypes {
err = binary.Read(partBuffer, binary.BigEndian, &t)
if err != nil {
return nil, err
}
packetValue.Type = t
if typeName, ok := ValueTypeValues[t]; ok {
packetValue.TypeName = typeName
}
if _, ok := types[packet.Type]; ok {
packetValue.Name = types[packet.Type][i].Name
}
switch t {
case TypeAbsolute:
var value uint64
err = binary.Read(partBuffer, binary.BigEndian, &value)
if err != nil {
return nil, err
}
packetValue.Value = float64(value)
case TypeCounter:
var value uint64
err = binary.Read(partBuffer, binary.BigEndian, &value)
if err != nil {
return nil, err
}
packetValue.Value = float64(value)
case TypeDerive:
var value int64
err = binary.Read(partBuffer, binary.BigEndian, &value)
if err != nil {
return nil, err
}
packetValue.Value = float64(value)
case TypeGauge:
var value float64
err = binary.Read(partBuffer, binary.LittleEndian, &value)
if err != nil {
return nil, err
}
packetValue.Value = float64(value)
default:
return nil, ErrorUnknownType
}
packet.Values[i] = packetValue
}
packets = append(packets, packet)
default:
// Ignore unknown fields
}
}
return &packets, nil
}
func TypesDBFile(path string) (Types, error) {
// See https://collectd.org/documentation/manpages/types.db.5.shtml
b, err := ioutil.ReadFile(path)
if err != nil {
return nil, err
}
return TypesDB(b)
}
func TypesDB(b []byte) (Types, error) {
types := make(Types)
content := string(b)
lines := strings.Split(content, "\n")
for i, line := range lines {
// Skip empty & comment lines
if line == "" || line[0] == '#' {
continue
}
dataSetName, dataSetSources, err := ParseDataSet(line)
if err != nil {
return nil, fmt.Errorf("Line %d: %s", i+1, err.Error())
}
types[dataSetName] = dataSetSources
}
return types, nil
}
// ParseDataSet parses one line from a collectd types.db file and returns
// the data-set name & a Type struct
func ParseDataSet(s string) (string, []*Type, error) {
splitFn := func(c rune) bool {
return c == '\t' || c == ' ' || c == ','
}
fields := strings.FieldsFunc(s, splitFn)
if len(fields) < 2 {
return "", nil, fmt.Errorf("Minimum of 2 fields required \"%s\"", s)
}
dataSetName := fields[0]
dataSetSources := make([]*Type, len(fields[1:]))
// Parse each data source
for i, field := range fields[1:] {
// Split data source fields
dsFields := strings.Split(field, ":")
if len(dsFields) != 4 {
return "", nil, fmt.Errorf("Exactly 4 fields required \"%s\"", field)
}
// Parse data source type
dsTypeStr := strings.ToLower(dsFields[1])
dsType, ok := ValueTypeNames[dsTypeStr]
if !ok {
return "", nil, fmt.Errorf("Invalid data-source type \"%s\"", dsTypeStr)
}
dataSetSources[i] = &Type{
Name: dsFields[0],
Type: dsType,
Min: dsFields[2],
Max: dsFields[3],
}
}
return dataSetName, dataSetSources, nil
}