Add possibility to specify measurement per register (#7231)
This commit is contained in:
parent
9a1c26d6cc
commit
3650d74de2
|
@ -52,6 +52,7 @@ The Modbus plugin collects Discrete Inputs, Coils, Input Registers and Holding R
|
||||||
]
|
]
|
||||||
|
|
||||||
## Analog Variables, Input Registers and Holding Registers
|
## Analog Variables, Input Registers and Holding Registers
|
||||||
|
## measurement - the (optional) measurement name, defaults to "modbus"
|
||||||
## name - the variable name
|
## name - the variable name
|
||||||
## byte_order - the ordering of bytes
|
## byte_order - the ordering of bytes
|
||||||
## |---AB, ABCD - Big Endian
|
## |---AB, ABCD - Big Endian
|
||||||
|
|
|
@ -7,10 +7,12 @@ import (
|
||||||
"net"
|
"net"
|
||||||
"net/url"
|
"net/url"
|
||||||
"sort"
|
"sort"
|
||||||
|
"time"
|
||||||
|
|
||||||
mb "github.com/goburrow/modbus"
|
mb "github.com/goburrow/modbus"
|
||||||
"github.com/influxdata/telegraf"
|
"github.com/influxdata/telegraf"
|
||||||
"github.com/influxdata/telegraf/internal"
|
"github.com/influxdata/telegraf/internal"
|
||||||
|
"github.com/influxdata/telegraf/metric"
|
||||||
"github.com/influxdata/telegraf/plugins/inputs"
|
"github.com/influxdata/telegraf/plugins/inputs"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -44,6 +46,7 @@ type register struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
type fieldContainer struct {
|
type fieldContainer struct {
|
||||||
|
Measurement string `toml:"measurement"`
|
||||||
Name string `toml:"name"`
|
Name string `toml:"name"`
|
||||||
ByteOrder string `toml:"byte_order"`
|
ByteOrder string `toml:"byte_order"`
|
||||||
DataType string `toml:"data_type"`
|
DataType string `toml:"data_type"`
|
||||||
|
@ -97,6 +100,7 @@ const sampleConfig = `
|
||||||
##
|
##
|
||||||
|
|
||||||
## Digital Variables, Discrete Inputs and Coils
|
## Digital Variables, Discrete Inputs and Coils
|
||||||
|
## measurement - the (optional) measurement name, defaults to "modbus"
|
||||||
## name - the variable name
|
## name - the variable name
|
||||||
## address - variable address
|
## address - variable address
|
||||||
|
|
||||||
|
@ -113,6 +117,7 @@ const sampleConfig = `
|
||||||
]
|
]
|
||||||
|
|
||||||
## Analog Variables, Input Registers and Holding Registers
|
## Analog Variables, Input Registers and Holding Registers
|
||||||
|
## measurement - the (optional) measurement name, defaults to "modbus"
|
||||||
## name - the variable name
|
## name - the variable name
|
||||||
## byte_order - the ordering of bytes
|
## byte_order - the ordering of bytes
|
||||||
## |---AB, ABCD - Big Endian
|
## |---AB, ABCD - Big Endian
|
||||||
|
@ -319,10 +324,11 @@ func validateFieldContainers(t []fieldContainer, n string) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
//search name duplicate
|
//search name duplicate
|
||||||
if nameEncountered[item.Name] {
|
canonical_name := item.Measurement + "." + item.Name
|
||||||
return fmt.Errorf("name '%s' is duplicated in '%s' - '%s'", item.Name, n, item.Name)
|
if nameEncountered[canonical_name] {
|
||||||
|
return fmt.Errorf("name '%s' is duplicated in measurement '%s' '%s' - '%s'", item.Name, item.Measurement, n, item.Name)
|
||||||
} else {
|
} else {
|
||||||
nameEncountered[item.Name] = true
|
nameEncountered[canonical_name] = true
|
||||||
}
|
}
|
||||||
|
|
||||||
if n == cInputRegisters || n == cHoldingRegisters {
|
if n == cInputRegisters || n == cHoldingRegisters {
|
||||||
|
@ -635,6 +641,7 @@ func (m *Modbus) Gather(acc telegraf.Accumulator) error {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
timestamp := time.Now()
|
||||||
err := m.getFields()
|
err := m.getFields()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
disconnect(m)
|
disconnect(m)
|
||||||
|
@ -642,18 +649,28 @@ func (m *Modbus) Gather(acc telegraf.Accumulator) error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
grouper := metric.NewSeriesGrouper()
|
||||||
for _, reg := range m.registers {
|
for _, reg := range m.registers {
|
||||||
fields := make(map[string]interface{})
|
|
||||||
tags := map[string]string{
|
tags := map[string]string{
|
||||||
"name": m.Name,
|
"name": m.Name,
|
||||||
"type": reg.Type,
|
"type": reg.Type,
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, field := range reg.Fields {
|
for _, field := range reg.Fields {
|
||||||
fields[field.Name] = field.value
|
// In case no measurement was specified we use "modbus" as default
|
||||||
|
measurement := "modbus"
|
||||||
|
if field.Measurement != "" {
|
||||||
|
measurement = field.Measurement
|
||||||
}
|
}
|
||||||
|
|
||||||
acc.AddFields("modbus", fields, tags)
|
// Group the data by series
|
||||||
|
grouper.Add(measurement, tags, timestamp, field.Name, field.value)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Add the metrics grouped by series to the accumulator
|
||||||
|
for _, metric := range grouper.Metrics() {
|
||||||
|
acc.AddMetric(metric)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
|
|
Loading…
Reference in New Issue