/
config.go
308 lines (251 loc) · 7.97 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
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
// Copyright 2019 Richard Hartmann
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package config
import (
"fmt"
multierror "github.com/hashicorp/go-multierror"
)
// Config represents the configuration of the modbus exporter.
type Config struct {
Modules []Module `yaml:"modules"`
}
// validate semantically validates the given config.
func (c *Config) validate() error {
for _, t := range c.Modules {
if err := t.validate(); err != nil {
return err
}
}
return nil
}
// HasModule returns whether the given config has a module with the given name.
func (c *Config) HasModule(n string) bool {
return c.GetModule(n) != nil
}
// GetModule returns the module matching the given string or nil if none was
// found.
func (c *Config) GetModule(n string) *Module {
for _, m := range c.Modules {
m := m
if m.Name == n {
return &m
}
}
return nil
}
// ListTargets is the list of configurations of the targets from the configuration
// file.
type ListTargets map[string]*Module
// Module defines the configuration parameters of a modbus module.
type Module struct {
Name string `yaml:"name"`
Protocol ModbusProtocol `yaml:"protocol"`
Timeout int `yaml:"timeout"`
Baudrate int `yaml:"baudrate"`
Databits int `yaml:"databits"`
Stopbits int `yaml:"stopbits"`
Parity string `yaml:"parity"`
Metrics []MetricDef `yaml:"metrics"`
}
// RegisterAddr specifies the register in the possible output of _digital
// output_, _digital input, _ananlog input, _analog output_.
type RegisterAddr uint32
// ModbusDataType is an Enum, representing the possible data types a register
// value can be interpreted as.
type ModbusDataType string
func (t *ModbusDataType) validate() error {
possibleModbusDataTypes := []ModbusDataType{
ModbusBool,
ModbusInt16,
ModbusUInt16,
ModbusFloat16,
ModbusInt32,
ModbusUInt32,
ModbusFloat32,
ModbusInt64,
ModbusUInt64,
ModbusFloat64,
}
if t == nil {
return fmt.Errorf("expected data type not to be nil")
}
for _, possibleType := range possibleModbusDataTypes {
if *t == possibleType {
return nil
}
}
return fmt.Errorf("expected one of the following data types %v but got '%v'",
possibleModbusDataTypes,
*t)
}
const (
ModbusBool ModbusDataType = "bool"
ModbusFloat16 ModbusDataType = "float16"
ModbusInt16 ModbusDataType = "int16"
ModbusUInt16 ModbusDataType = "uint16"
ModbusInt32 ModbusDataType = "int32"
ModbusUInt32 ModbusDataType = "uint32"
ModbusFloat32 ModbusDataType = "float32"
ModbusInt64 ModbusDataType = "int64"
ModbusUInt64 ModbusDataType = "uint64"
ModbusFloat64 ModbusDataType = "float64"
)
// EndiannessType is an Enum, representing the possible endianness types a register
// value can have.
type EndiannessType string
func (endianness *EndiannessType) validate() error {
possibleEndiannessTypes := []EndiannessType{
EndiannessBigEndian,
EndiannessLittleEndian,
EndiannessMixedEndian,
EndiannessYolo,
}
for _, possibleEndianness := range possibleEndiannessTypes {
if *endianness == possibleEndianness {
return nil
}
}
return fmt.Errorf("expected one of the following endianness types %v but got '%v'",
possibleEndiannessTypes,
*endianness)
}
const (
// EndiannessBigEndian (1 2 3 4)
EndiannessBigEndian EndiannessType = "big"
// EndiannessLittleEndian (4 3 2 1)
EndiannessLittleEndian EndiannessType = "little"
// EndiannessMixedEndian (2 1 4 3)
EndiannessMixedEndian EndiannessType = "mixed"
// EndiannessYolo (3 4 1 2)
EndiannessYolo EndiannessType = "yolo"
)
// MetricType specifies the Prometheus metric type, see
// https://prometheus.io/docs/concepts/metric_types/ for details.
type MetricType string
func (t *MetricType) validate() error {
possibleMetricTypes := []MetricType{
MetricTypeGauge,
MetricTypeCounter,
}
if t == nil {
return fmt.Errorf("expected metric type not to be nil")
}
for _, possibleType := range possibleMetricTypes {
if *t == possibleType {
return nil
}
}
return fmt.Errorf("expected one of the following metric types %v but got '%v'",
possibleMetricTypes,
*t)
}
const (
MetricTypeGauge MetricType = "gauge"
MetricTypeCounter MetricType = "counter"
)
// MetricDef defines how to construct Prometheus metrics based on one or more
// Modbus registers.
type MetricDef struct {
// Name of the metric in the Prometheus output format.
Name string `yaml:"name"`
// Help text of the metric in the Prometheus output format.
Help string `yaml:"help"`
// Labels to be applied to the metric in the Prometheus output format.
Labels map[string]string `yaml:"labels"`
Address RegisterAddr `yaml:"address"`
DataType ModbusDataType `yaml:"dataType"`
Endianness EndiannessType `yaml:"endianness,omitempty"`
// Bit offset within the input register to parse. Only valid for boolean data
// type. The two bytes of a register are interpreted in network order (big
// endianness). Boolean is determined via `register&(1<<offset)>0`.
BitOffset *int `yaml:"bitOffset,omitempty"`
MetricType MetricType `yaml:"metricType"`
// Scaling factor
Factor *float64 `yaml:"factor,omitempty"`
}
// Validate semantically validates the given metric definition.
func (d *MetricDef) validate() error {
if err := d.DataType.validate(); err != nil {
return fmt.Errorf("invalid metric definition %v: %v", d.Name, err)
}
if err := d.MetricType.validate(); err != nil {
return fmt.Errorf("invalid metric definition %v: %v", d.Name, err)
}
// TODO: Does it have to be used with bools though? Or should there be a default?
if d.BitOffset != nil && d.DataType != ModbusBool {
return fmt.Errorf("bitPosition can only be used with boolean data type")
}
if d.Endianness != "" {
if err := d.Endianness.validate(); err != nil {
return fmt.Errorf("invalid endianness definition %v: %v", d.Name, err)
}
} else {
d.Endianness = EndiannessBigEndian
}
if d.Factor != nil && d.DataType == ModbusBool {
return fmt.Errorf("factor cannot be used with boolean data type")
}
if d.Factor != nil && *d.Factor == 0.0 {
return fmt.Errorf("factor cannot be 0")
}
return nil
}
// ModbusProtocol specifies the protocol used to retrieve modbus data.
type ModbusProtocol string
const (
// ModbusProtocolTCPIP represents modbus via TCP/IP.
ModbusProtocolTCPIP = "tcp/ip"
)
// ModbusProtocolValidationError is returned on invalid or unsupported modbus
// protocol specifications.
type ModbusProtocolValidationError struct {
e string
}
// Error implements the Golang error interface.
func (e *ModbusProtocolValidationError) Error() string {
return e.e
}
func (t *ModbusProtocol) validate() error {
possibleProtocols := []ModbusProtocol{
ModbusProtocolTCPIP,
}
if t == nil {
return fmt.Errorf("expected protocol not to be nil")
}
for _, possibleProtocol := range possibleProtocols {
if *t == possibleProtocol {
return nil
}
}
return fmt.Errorf("expected one of the following protocols %v but got '%v'",
possibleProtocols,
*t)
}
// Validate tries to find inconsistencies in the parameters of a module.
func (s *Module) validate() error {
var err error
if protocolErr := s.Protocol.validate(); protocolErr != nil {
err = multierror.Append(err, protocolErr)
}
// track that error if we have no register definitions
if len(s.Metrics) == 0 {
noRegErr := fmt.Errorf("no metric definitions found in module %s", s.Name)
err = multierror.Append(err, noRegErr)
}
for _, def := range s.Metrics {
if err := def.validate(); err != nil {
return fmt.Errorf("failed to validate module %v: %v", s.Name, err)
}
}
return err
}