2018-07-03 22:32:52 +00:00
|
|
|
package enum
|
|
|
|
|
|
|
|
import (
|
2019-05-16 22:59:19 +00:00
|
|
|
"fmt"
|
2018-07-03 22:32:52 +00:00
|
|
|
"strconv"
|
|
|
|
|
|
|
|
"github.com/influxdata/telegraf"
|
|
|
|
"github.com/influxdata/telegraf/plugins/processors"
|
|
|
|
)
|
|
|
|
|
|
|
|
var sampleConfig = `
|
2018-09-11 23:03:47 +00:00
|
|
|
[[processors.enum.mapping]]
|
2018-07-03 23:10:55 +00:00
|
|
|
## Name of the field to map
|
2018-09-11 23:03:47 +00:00
|
|
|
field = "status"
|
2018-07-03 23:10:55 +00:00
|
|
|
|
2019-05-16 22:59:19 +00:00
|
|
|
## Name of the tag to map
|
|
|
|
# tag = "status"
|
|
|
|
|
|
|
|
## Destination tag or field to be used for the mapped value. By default the
|
|
|
|
## source tag or field is used, overwriting the original value.
|
|
|
|
dest = "status_code"
|
2018-07-03 23:10:55 +00:00
|
|
|
|
|
|
|
## Default value to be used for all values not contained in the mapping
|
|
|
|
## table. When unset, the unmodified value for the field will be used if no
|
|
|
|
## match is found.
|
|
|
|
# default = 0
|
|
|
|
|
|
|
|
## Table of mappings
|
2018-09-11 23:03:47 +00:00
|
|
|
[processors.enum.mapping.value_mappings]
|
|
|
|
green = 1
|
2019-05-16 22:59:19 +00:00
|
|
|
amber = 2
|
2018-09-11 23:03:47 +00:00
|
|
|
red = 3
|
2018-07-03 22:32:52 +00:00
|
|
|
`
|
|
|
|
|
|
|
|
type EnumMapper struct {
|
2018-09-11 23:03:47 +00:00
|
|
|
Mappings []Mapping `toml:"mapping"`
|
2018-07-03 22:32:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type Mapping struct {
|
2019-05-16 22:59:19 +00:00
|
|
|
Tag string
|
2018-09-11 23:03:47 +00:00
|
|
|
Field string
|
|
|
|
Dest string
|
2018-07-03 22:32:52 +00:00
|
|
|
Default interface{}
|
|
|
|
ValueMappings map[string]interface{}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (mapper *EnumMapper) SampleConfig() string {
|
|
|
|
return sampleConfig
|
|
|
|
}
|
|
|
|
|
|
|
|
func (mapper *EnumMapper) Description() string {
|
|
|
|
return "Map enum values according to given table."
|
|
|
|
}
|
|
|
|
|
|
|
|
func (mapper *EnumMapper) Apply(in ...telegraf.Metric) []telegraf.Metric {
|
|
|
|
for i := 0; i < len(in); i++ {
|
|
|
|
in[i] = mapper.applyMappings(in[i])
|
|
|
|
}
|
|
|
|
return in
|
|
|
|
}
|
|
|
|
|
|
|
|
func (mapper *EnumMapper) applyMappings(metric telegraf.Metric) telegraf.Metric {
|
2018-09-11 23:03:47 +00:00
|
|
|
for _, mapping := range mapper.Mappings {
|
2019-05-16 22:59:19 +00:00
|
|
|
if mapping.Field != "" {
|
|
|
|
if originalValue, isPresent := metric.GetField(mapping.Field); isPresent {
|
2020-05-11 18:36:21 +00:00
|
|
|
if adjustedValue, isString := adjustValue(originalValue).(string); isString {
|
2019-05-16 22:59:19 +00:00
|
|
|
if mappedValue, isMappedValuePresent := mapping.mapValue(adjustedValue); isMappedValuePresent {
|
|
|
|
writeField(metric, mapping.getDestination(), mappedValue)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if mapping.Tag != "" {
|
|
|
|
if originalValue, isPresent := metric.GetTag(mapping.Tag); isPresent {
|
|
|
|
if mappedValue, isMappedValuePresent := mapping.mapValue(originalValue); isMappedValuePresent {
|
|
|
|
switch val := mappedValue.(type) {
|
|
|
|
case string:
|
|
|
|
writeTag(metric, mapping.getDestinationTag(), val)
|
|
|
|
default:
|
|
|
|
writeTag(metric, mapping.getDestinationTag(), fmt.Sprintf("%v", val))
|
|
|
|
}
|
2018-07-03 22:32:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return metric
|
|
|
|
}
|
|
|
|
|
2020-05-11 18:36:21 +00:00
|
|
|
func adjustValue(in interface{}) interface{} {
|
|
|
|
switch val := in.(type) {
|
|
|
|
case bool:
|
|
|
|
return strconv.FormatBool(val)
|
|
|
|
case int64:
|
|
|
|
return strconv.FormatInt(val, 10)
|
|
|
|
case uint64:
|
|
|
|
return strconv.FormatUint(val, 10)
|
|
|
|
default:
|
|
|
|
return in
|
2018-07-03 22:32:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (mapping *Mapping) mapValue(original string) (interface{}, bool) {
|
|
|
|
if mapped, found := mapping.ValueMappings[original]; found == true {
|
|
|
|
return mapped, true
|
|
|
|
}
|
|
|
|
if mapping.Default != nil {
|
|
|
|
return mapping.Default, true
|
|
|
|
}
|
|
|
|
return original, false
|
|
|
|
}
|
|
|
|
|
|
|
|
func (mapping *Mapping) getDestination() string {
|
2018-09-11 23:03:47 +00:00
|
|
|
if mapping.Dest != "" {
|
|
|
|
return mapping.Dest
|
2018-07-03 22:32:52 +00:00
|
|
|
}
|
2018-09-11 23:03:47 +00:00
|
|
|
return mapping.Field
|
2018-07-03 22:32:52 +00:00
|
|
|
}
|
|
|
|
|
2019-05-16 22:59:19 +00:00
|
|
|
func (mapping *Mapping) getDestinationTag() string {
|
|
|
|
if mapping.Dest != "" {
|
|
|
|
return mapping.Dest
|
|
|
|
}
|
|
|
|
return mapping.Tag
|
|
|
|
}
|
|
|
|
|
2018-07-03 22:32:52 +00:00
|
|
|
func writeField(metric telegraf.Metric, name string, value interface{}) {
|
2018-09-11 23:03:47 +00:00
|
|
|
metric.RemoveField(name)
|
2018-07-03 22:32:52 +00:00
|
|
|
metric.AddField(name, value)
|
|
|
|
}
|
|
|
|
|
2019-05-16 22:59:19 +00:00
|
|
|
func writeTag(metric telegraf.Metric, name string, value string) {
|
|
|
|
metric.RemoveTag(name)
|
|
|
|
metric.AddTag(name, value)
|
|
|
|
}
|
|
|
|
|
2018-07-03 22:32:52 +00:00
|
|
|
func init() {
|
|
|
|
processors.Add("enum", func() telegraf.Processor {
|
|
|
|
return &EnumMapper{}
|
|
|
|
})
|
|
|
|
}
|