forked from influxdata/telegraf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.go
183 lines (160 loc) · 4 KB
/
config.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
package binary
import (
"encoding/binary"
"encoding/hex"
"errors"
"fmt"
"strings"
"time"
"github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/internal"
"github.com/influxdata/telegraf/metric"
)
type BinaryPart struct {
Offset uint64 `toml:"offset"`
Bits uint64 `toml:"bits"`
Match string `toml:"match"`
val []byte
}
type Filter struct {
Selection []BinaryPart `toml:"selection"`
LengthMin uint64 `toml:"length_min"`
Length uint64 `toml:"length"`
}
type Config struct {
MetricName string `toml:"metric_name"`
Filter *Filter `toml:"filter"`
Entries []Entry `toml:"entries"`
}
func (c *Config) preprocess(defaultName string) error {
// Preprocess filter part
if c.Filter != nil {
if c.Filter.Length != 0 && c.Filter.LengthMin != 0 {
return errors.New("length and length_min cannot be used together")
}
var length uint64
for i, s := range c.Filter.Selection {
end := (s.Offset + s.Bits) / 8
if (s.Offset+s.Bits)%8 != 0 {
end++
}
if end > length {
length = end
}
var err error
s.val, err = hex.DecodeString(strings.TrimPrefix(s.Match, "0x"))
if err != nil {
return fmt.Errorf("decoding match %d failed: %w", i, err)
}
c.Filter.Selection[i] = s
}
if c.Filter.Length != 0 && length > c.Filter.Length {
return fmt.Errorf("filter length (%d) larger than constraint (%d)", length, c.Filter.Length)
}
if c.Filter.Length == 0 && length > c.Filter.LengthMin {
c.Filter.LengthMin = length
}
}
// Preprocess entries part
var hasField, hasMeasurement bool
defined := make(map[string]bool)
for i, e := range c.Entries {
if err := e.check(); err != nil {
return fmt.Errorf("entry %q (%d): %w", e.Name, i, err)
}
// Store the normalized entry
c.Entries[i] = e
if e.Omit {
continue
}
// Check for duplicate entries
key := e.Assignment + "_" + e.Name
if defined[key] {
return fmt.Errorf("multiple definitions of %q", e.Name)
}
defined[key] = true
hasMeasurement = hasMeasurement || e.Assignment == "measurement"
hasField = hasField || e.Assignment == "field"
}
if !hasMeasurement && c.MetricName == "" {
if defaultName == "" {
return errors.New("no metric name given")
}
c.MetricName = defaultName
}
if !hasField {
return errors.New("no field defined")
}
return nil
}
func (c *Config) matches(in []byte) bool {
// If no filter is given, just match everything
if c.Filter == nil {
return true
}
// Checking length constraints
length := uint64(len(in))
if c.Filter.Length != 0 && length != c.Filter.Length {
return false
}
if c.Filter.LengthMin != 0 && length < c.Filter.LengthMin {
return false
}
// Matching elements
for _, s := range c.Filter.Selection {
data, err := extractPart(in, s.Offset, s.Bits)
if err != nil {
return false
}
if len(data) != len(s.val) {
return false
}
for i, v := range data {
if v != s.val[i] {
return false
}
}
}
return true
}
func (c *Config) collect(in []byte, order binary.ByteOrder, defaultTime time.Time) (telegraf.Metric, error) {
t := defaultTime
name := c.MetricName
tags := make(map[string]string)
fields := make(map[string]interface{})
var offset uint64
for _, e := range c.Entries {
data, n, err := e.extract(in, offset)
if err != nil {
return nil, err
}
offset += n
switch e.Assignment {
case "measurement":
name = convertStringType(data)
case "field":
v, err := e.convertType(data, order)
if err != nil {
return nil, fmt.Errorf("field %q failed: %w", e.Name, err)
}
fields[e.Name] = v
case "tag":
raw, err := e.convertType(data, order)
if err != nil {
return nil, fmt.Errorf("tag %q failed: %w", e.Name, err)
}
v, err := internal.ToString(raw)
if err != nil {
return nil, fmt.Errorf("tag %q failed: %w", e.Name, err)
}
tags[e.Name] = v
case "time":
var err error
t, err = e.convertTimeType(data, order)
if err != nil {
return nil, fmt.Errorf("time failed: %w", err)
}
}
}
return metric.New(name, tags, fields, t), nil
}