2016-07-28 11:31:11 +00:00
|
|
|
package models
|
2016-01-22 18:54:12 +00:00
|
|
|
|
|
|
|
import (
|
2016-09-08 14:22:10 +00:00
|
|
|
"fmt"
|
2016-01-22 18:54:12 +00:00
|
|
|
"time"
|
|
|
|
|
2016-01-27 21:21:36 +00:00
|
|
|
"github.com/influxdata/telegraf"
|
2018-03-28 00:30:51 +00:00
|
|
|
"github.com/influxdata/telegraf/plugins/serializers/influx"
|
2016-11-07 08:34:46 +00:00
|
|
|
"github.com/influxdata/telegraf/selfstat"
|
2016-01-22 18:54:12 +00:00
|
|
|
)
|
|
|
|
|
2016-11-07 08:34:46 +00:00
|
|
|
var GlobalMetricsGathered = selfstat.Register("agent", "metrics_gathered", map[string]string{})
|
|
|
|
|
2016-01-22 18:54:12 +00:00
|
|
|
type RunningInput struct {
|
2016-01-27 21:21:36 +00:00
|
|
|
Input telegraf.Input
|
2016-01-22 18:54:12 +00:00
|
|
|
Config *InputConfig
|
2016-09-08 14:22:10 +00:00
|
|
|
|
|
|
|
trace bool
|
|
|
|
defaultTags map[string]string
|
2016-11-07 08:34:46 +00:00
|
|
|
|
|
|
|
MetricsGathered selfstat.Stat
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewRunningInput(
|
|
|
|
input telegraf.Input,
|
|
|
|
config *InputConfig,
|
|
|
|
) *RunningInput {
|
|
|
|
return &RunningInput{
|
|
|
|
Input: input,
|
|
|
|
Config: config,
|
|
|
|
MetricsGathered: selfstat.Register(
|
|
|
|
"gather",
|
|
|
|
"metrics_gathered",
|
|
|
|
map[string]string{"input": config.Name},
|
|
|
|
),
|
|
|
|
}
|
2016-01-22 18:54:12 +00:00
|
|
|
}
|
|
|
|
|
2018-09-28 21:48:20 +00:00
|
|
|
// InputConfig is the common config for all inputs.
|
2016-01-22 18:54:12 +00:00
|
|
|
type InputConfig struct {
|
2018-09-28 21:48:20 +00:00
|
|
|
Name string
|
|
|
|
Interval time.Duration
|
|
|
|
|
2016-01-22 18:54:12 +00:00
|
|
|
NameOverride string
|
|
|
|
MeasurementPrefix string
|
|
|
|
MeasurementSuffix string
|
|
|
|
Tags map[string]string
|
|
|
|
Filter Filter
|
|
|
|
}
|
2016-09-08 14:22:10 +00:00
|
|
|
|
|
|
|
func (r *RunningInput) Name() string {
|
|
|
|
return "inputs." + r.Config.Name
|
|
|
|
}
|
|
|
|
|
2018-09-28 21:48:20 +00:00
|
|
|
func (r *RunningInput) MakeMetric(metric telegraf.Metric) telegraf.Metric {
|
|
|
|
if ok := r.Config.Filter.Select(metric); !ok {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
r.Config.Filter.Modify(metric)
|
|
|
|
if len(metric.FieldList()) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-09-08 14:22:10 +00:00
|
|
|
m := makemetric(
|
2018-09-28 21:48:20 +00:00
|
|
|
metric,
|
2016-09-08 14:22:10 +00:00
|
|
|
r.Config.NameOverride,
|
|
|
|
r.Config.MeasurementPrefix,
|
|
|
|
r.Config.MeasurementSuffix,
|
|
|
|
r.Config.Tags,
|
2018-09-28 21:48:20 +00:00
|
|
|
r.defaultTags)
|
2016-09-08 14:22:10 +00:00
|
|
|
|
|
|
|
if r.trace && m != nil {
|
2018-03-28 00:30:51 +00:00
|
|
|
s := influx.NewSerializer()
|
2018-04-03 23:56:44 +00:00
|
|
|
s.SetFieldSortOrder(influx.SortFields)
|
2018-03-28 00:30:51 +00:00
|
|
|
octets, err := s.Serialize(m)
|
|
|
|
if err == nil {
|
|
|
|
fmt.Print("> " + string(octets))
|
|
|
|
}
|
2016-09-08 14:22:10 +00:00
|
|
|
}
|
|
|
|
|
2016-11-07 08:34:46 +00:00
|
|
|
r.MetricsGathered.Incr(1)
|
|
|
|
GlobalMetricsGathered.Incr(1)
|
2016-09-08 14:22:10 +00:00
|
|
|
return m
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *RunningInput) Trace() bool {
|
|
|
|
return r.trace
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *RunningInput) SetTrace(trace bool) {
|
|
|
|
r.trace = trace
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *RunningInput) SetDefaultTags(tags map[string]string) {
|
|
|
|
r.defaultTags = tags
|
|
|
|
}
|