2016-01-27 21:21:36 +00:00
|
|
|
package agent
|
2015-04-01 16:34:32 +00:00
|
|
|
|
|
|
|
import (
|
2015-05-18 21:10:12 +00:00
|
|
|
"fmt"
|
2015-04-01 16:34:32 +00:00
|
|
|
"log"
|
2015-04-07 16:56:40 +00:00
|
|
|
"os"
|
2016-01-26 08:19:34 +00:00
|
|
|
"runtime"
|
2015-05-20 05:19:32 +00:00
|
|
|
"sync"
|
2015-04-07 16:23:35 +00:00
|
|
|
"time"
|
2015-04-01 16:34:32 +00:00
|
|
|
|
2016-01-27 21:21:36 +00:00
|
|
|
"github.com/influxdata/telegraf"
|
2016-05-30 22:24:42 +00:00
|
|
|
"github.com/influxdata/telegraf/internal"
|
2016-01-20 18:57:35 +00:00
|
|
|
"github.com/influxdata/telegraf/internal/config"
|
2016-01-22 18:54:12 +00:00
|
|
|
"github.com/influxdata/telegraf/internal/models"
|
2016-11-07 08:34:46 +00:00
|
|
|
"github.com/influxdata/telegraf/selfstat"
|
2015-04-01 16:34:32 +00:00
|
|
|
)
|
|
|
|
|
2015-08-04 14:58:32 +00:00
|
|
|
// Agent runs telegraf and collects data based on the given config
|
2015-04-01 16:34:32 +00:00
|
|
|
type Agent struct {
|
2015-11-24 21:22:11 +00:00
|
|
|
Config *config.Config
|
2015-04-01 16:34:32 +00:00
|
|
|
}
|
|
|
|
|
2015-08-04 14:58:32 +00:00
|
|
|
// NewAgent returns an Agent struct based off the given Config
|
2015-11-24 21:22:11 +00:00
|
|
|
func NewAgent(config *config.Config) (*Agent, error) {
|
2015-11-26 01:42:07 +00:00
|
|
|
a := &Agent{
|
|
|
|
Config: config,
|
2015-09-02 16:30:44 +00:00
|
|
|
}
|
2015-04-01 16:34:32 +00:00
|
|
|
|
2016-03-21 21:33:19 +00:00
|
|
|
if !a.Config.Agent.OmitHostname {
|
|
|
|
if a.Config.Agent.Hostname == "" {
|
|
|
|
hostname, err := os.Hostname()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
a.Config.Agent.Hostname = hostname
|
2015-04-07 16:56:40 +00:00
|
|
|
}
|
|
|
|
|
2016-03-21 21:33:19 +00:00
|
|
|
config.Tags["host"] = a.Config.Agent.Hostname
|
2015-11-24 01:00:54 +00:00
|
|
|
}
|
|
|
|
|
2015-11-26 01:42:07 +00:00
|
|
|
return a, nil
|
2015-04-07 00:24:24 +00:00
|
|
|
}
|
|
|
|
|
2015-08-12 17:04:25 +00:00
|
|
|
// Connect connects to all configured outputs
|
2015-08-04 14:58:32 +00:00
|
|
|
func (a *Agent) Connect() error {
|
2015-11-24 21:22:11 +00:00
|
|
|
for _, o := range a.Config.Outputs {
|
|
|
|
switch ot := o.Output.(type) {
|
2016-01-27 21:21:36 +00:00
|
|
|
case telegraf.ServiceOutput:
|
2015-10-22 16:17:57 +00:00
|
|
|
if err := ot.Start(); err != nil {
|
2016-09-30 21:37:56 +00:00
|
|
|
log.Printf("E! Service for output %s failed to start, exiting\n%s\n",
|
2015-11-24 21:22:11 +00:00
|
|
|
o.Name, err.Error())
|
2015-10-22 16:17:57 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-30 21:37:56 +00:00
|
|
|
log.Printf("D! Attempting connection to output: %s\n", o.Name)
|
2015-11-24 21:22:11 +00:00
|
|
|
err := o.Output.Connect()
|
2015-08-07 20:31:25 +00:00
|
|
|
if err != nil {
|
2016-09-30 21:37:56 +00:00
|
|
|
log.Printf("E! Failed to connect to output %s, retrying in 15s, "+
|
2016-02-16 00:21:38 +00:00
|
|
|
"error was '%s' \n", o.Name, err)
|
2015-09-19 01:02:16 +00:00
|
|
|
time.Sleep(15 * time.Second)
|
2015-11-24 21:22:11 +00:00
|
|
|
err = o.Output.Connect()
|
2015-09-19 01:02:16 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-08-07 20:31:25 +00:00
|
|
|
}
|
2016-09-30 21:37:56 +00:00
|
|
|
log.Printf("D! Successfully connected to output: %s\n", o.Name)
|
2015-04-01 16:34:32 +00:00
|
|
|
}
|
2015-08-07 20:31:25 +00:00
|
|
|
return nil
|
|
|
|
}
|
2015-04-01 16:34:32 +00:00
|
|
|
|
2015-08-12 17:04:25 +00:00
|
|
|
// Close closes the connection to all configured outputs
|
|
|
|
func (a *Agent) Close() error {
|
|
|
|
var err error
|
2015-11-24 21:22:11 +00:00
|
|
|
for _, o := range a.Config.Outputs {
|
|
|
|
err = o.Output.Close()
|
|
|
|
switch ot := o.Output.(type) {
|
2016-01-27 21:21:36 +00:00
|
|
|
case telegraf.ServiceOutput:
|
2015-10-22 16:17:57 +00:00
|
|
|
ot.Stop()
|
|
|
|
}
|
2015-08-12 17:04:25 +00:00
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-07-28 11:31:11 +00:00
|
|
|
func panicRecover(input *models.RunningInput) {
|
2016-01-26 08:19:34 +00:00
|
|
|
if err := recover(); err != nil {
|
|
|
|
trace := make([]byte, 2048)
|
|
|
|
runtime.Stack(trace, true)
|
2016-09-30 21:37:56 +00:00
|
|
|
log.Printf("E! FATAL: Input [%s] panicked: %s, Stack:\n%s\n",
|
2016-09-08 14:22:10 +00:00
|
|
|
input.Name(), err, trace)
|
2016-09-30 21:37:56 +00:00
|
|
|
log.Println("E! PLEASE REPORT THIS PANIC ON GITHUB with " +
|
2016-01-26 08:19:34 +00:00
|
|
|
"stack trace, configuration, and OS information: " +
|
|
|
|
"https://github.com/influxdata/telegraf/issues/new")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-19 15:36:58 +00:00
|
|
|
// gatherer runs the inputs that have been configured with their own
|
2015-08-26 17:02:10 +00:00
|
|
|
// reporting interval.
|
2016-05-19 15:36:58 +00:00
|
|
|
func (a *Agent) gatherer(
|
2015-10-16 22:13:32 +00:00
|
|
|
shutdown chan struct{},
|
2016-07-28 11:31:11 +00:00
|
|
|
input *models.RunningInput,
|
2016-05-19 15:36:58 +00:00
|
|
|
interval time.Duration,
|
2016-01-27 23:15:14 +00:00
|
|
|
metricC chan telegraf.Metric,
|
2016-09-08 14:22:10 +00:00
|
|
|
) {
|
2016-01-26 08:19:34 +00:00
|
|
|
defer panicRecover(input)
|
|
|
|
|
2016-11-07 08:34:46 +00:00
|
|
|
GatherTime := selfstat.RegisterTiming("gather",
|
|
|
|
"gather_time_ns",
|
|
|
|
map[string]string{"input": input.Config.Name},
|
|
|
|
)
|
|
|
|
|
|
|
|
acc := NewAccumulator(input, metricC)
|
|
|
|
acc.SetPrecision(a.Config.Agent.Precision.Duration,
|
|
|
|
a.Config.Agent.Interval.Duration)
|
|
|
|
|
2016-05-19 15:36:58 +00:00
|
|
|
ticker := time.NewTicker(interval)
|
|
|
|
defer ticker.Stop()
|
|
|
|
|
2015-05-20 05:19:32 +00:00
|
|
|
for {
|
2016-05-30 22:24:42 +00:00
|
|
|
internal.RandomSleep(a.Config.Agent.CollectionJitter.Duration, shutdown)
|
2015-05-20 05:19:32 +00:00
|
|
|
|
2016-05-30 22:24:42 +00:00
|
|
|
start := time.Now()
|
2016-05-19 15:36:58 +00:00
|
|
|
gatherWithTimeout(shutdown, input, acc, interval)
|
2015-09-28 20:08:28 +00:00
|
|
|
elapsed := time.Since(start)
|
2015-08-26 23:43:09 +00:00
|
|
|
|
2016-11-07 08:34:46 +00:00
|
|
|
GatherTime.Incr(elapsed.Nanoseconds())
|
2015-05-20 05:19:32 +00:00
|
|
|
|
|
|
|
select {
|
|
|
|
case <-shutdown:
|
2016-09-08 14:22:10 +00:00
|
|
|
return
|
2015-05-20 05:19:32 +00:00
|
|
|
case <-ticker.C:
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-19 15:36:58 +00:00
|
|
|
// gatherWithTimeout gathers from the given input, with the given timeout.
|
|
|
|
// when the given timeout is reached, gatherWithTimeout logs an error message
|
|
|
|
// but continues waiting for it to return. This is to avoid leaving behind
|
|
|
|
// hung processes, and to prevent re-calling the same hung process over and
|
|
|
|
// over.
|
|
|
|
func gatherWithTimeout(
|
|
|
|
shutdown chan struct{},
|
2016-07-28 11:31:11 +00:00
|
|
|
input *models.RunningInput,
|
2018-01-26 19:39:58 +00:00
|
|
|
acc telegraf.Accumulator,
|
2016-05-19 15:36:58 +00:00
|
|
|
timeout time.Duration,
|
|
|
|
) {
|
|
|
|
ticker := time.NewTicker(timeout)
|
|
|
|
defer ticker.Stop()
|
|
|
|
done := make(chan error)
|
|
|
|
go func() {
|
|
|
|
done <- input.Input.Gather(acc)
|
|
|
|
}()
|
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case err := <-done:
|
|
|
|
if err != nil {
|
2017-01-29 22:28:52 +00:00
|
|
|
acc.AddError(err)
|
2016-05-19 15:36:58 +00:00
|
|
|
}
|
|
|
|
return
|
|
|
|
case <-ticker.C:
|
2017-01-29 22:28:52 +00:00
|
|
|
err := fmt.Errorf("took longer to collect than collection interval (%s)",
|
|
|
|
timeout)
|
|
|
|
acc.AddError(err)
|
2016-05-19 15:36:58 +00:00
|
|
|
continue
|
|
|
|
case <-shutdown:
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-07 20:39:43 +00:00
|
|
|
// Test verifies that we can 'Gather' from all inputs with their configured
|
2015-08-04 14:58:32 +00:00
|
|
|
// Config struct
|
2015-04-07 00:24:24 +00:00
|
|
|
func (a *Agent) Test() error {
|
2015-10-16 22:13:32 +00:00
|
|
|
shutdown := make(chan struct{})
|
|
|
|
defer close(shutdown)
|
2016-01-27 23:15:14 +00:00
|
|
|
metricC := make(chan telegraf.Metric)
|
2015-04-07 00:24:24 +00:00
|
|
|
|
2015-10-22 00:32:43 +00:00
|
|
|
// dummy receiver for the point channel
|
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
select {
|
2016-01-27 23:15:14 +00:00
|
|
|
case <-metricC:
|
2015-10-22 00:32:43 +00:00
|
|
|
// do nothing
|
|
|
|
case <-shutdown:
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
2015-04-07 00:24:24 +00:00
|
|
|
|
2016-01-07 20:39:43 +00:00
|
|
|
for _, input := range a.Config.Inputs {
|
2017-02-13 10:40:38 +00:00
|
|
|
if _, ok := input.Input.(telegraf.ServiceInput); ok {
|
|
|
|
fmt.Printf("\nWARNING: skipping plugin [[%s]]: service inputs not supported in --test mode\n",
|
|
|
|
input.Name())
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2016-09-08 14:22:10 +00:00
|
|
|
acc := NewAccumulator(input, metricC)
|
2016-06-13 14:21:11 +00:00
|
|
|
acc.SetPrecision(a.Config.Agent.Precision.Duration,
|
|
|
|
a.Config.Agent.Interval.Duration)
|
2016-09-08 14:22:10 +00:00
|
|
|
input.SetTrace(true)
|
|
|
|
input.SetDefaultTags(a.Config.Tags)
|
2015-05-20 05:19:32 +00:00
|
|
|
|
2016-01-07 20:39:43 +00:00
|
|
|
if err := input.Input.Gather(acc); err != nil {
|
2015-04-07 00:24:24 +00:00
|
|
|
return err
|
|
|
|
}
|
2015-09-21 17:05:58 +00:00
|
|
|
|
2016-01-07 20:39:43 +00:00
|
|
|
// Special instructions for some inputs. cpu, for example, needs to be
|
2015-09-23 20:54:22 +00:00
|
|
|
// run twice in order to return cpu usage percentages.
|
2016-09-08 14:22:10 +00:00
|
|
|
switch input.Name() {
|
2017-02-13 10:33:51 +00:00
|
|
|
case "inputs.cpu", "inputs.mongodb", "inputs.procstat":
|
2015-09-23 20:54:22 +00:00
|
|
|
time.Sleep(500 * time.Millisecond)
|
2016-01-07 20:39:43 +00:00
|
|
|
if err := input.Input.Gather(acc); err != nil {
|
2015-09-23 20:54:22 +00:00
|
|
|
return err
|
|
|
|
}
|
2015-09-21 17:05:58 +00:00
|
|
|
}
|
2015-04-07 00:24:24 +00:00
|
|
|
|
2015-09-23 20:54:22 +00:00
|
|
|
}
|
2015-04-07 00:24:24 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-02-16 00:21:38 +00:00
|
|
|
// flush writes a list of metrics to all configured outputs
|
2016-01-22 18:54:12 +00:00
|
|
|
func (a *Agent) flush() {
|
2015-10-21 20:05:27 +00:00
|
|
|
var wg sync.WaitGroup
|
2016-01-22 18:54:12 +00:00
|
|
|
|
|
|
|
wg.Add(len(a.Config.Outputs))
|
2015-11-24 21:22:11 +00:00
|
|
|
for _, o := range a.Config.Outputs {
|
2016-07-28 11:31:11 +00:00
|
|
|
go func(output *models.RunningOutput) {
|
2016-01-22 18:54:12 +00:00
|
|
|
defer wg.Done()
|
|
|
|
err := output.Write()
|
|
|
|
if err != nil {
|
2016-09-30 21:37:56 +00:00
|
|
|
log.Printf("E! Error writing to output [%s]: %s\n",
|
2016-01-22 18:54:12 +00:00
|
|
|
output.Name, err.Error())
|
|
|
|
}
|
|
|
|
}(o)
|
2015-10-21 16:57:51 +00:00
|
|
|
}
|
2016-01-22 18:54:12 +00:00
|
|
|
|
|
|
|
wg.Wait()
|
2015-10-16 22:13:32 +00:00
|
|
|
}
|
|
|
|
|
2016-02-16 00:21:38 +00:00
|
|
|
// flusher monitors the metrics input channel and flushes on the minimum interval
|
2018-07-12 00:33:27 +00:00
|
|
|
func (a *Agent) flusher(
|
|
|
|
shutdown chan struct{},
|
|
|
|
metricC chan telegraf.Metric,
|
|
|
|
aggMetricC chan telegraf.Metric,
|
|
|
|
outMetricC chan telegraf.Metric,
|
|
|
|
) error {
|
2016-09-08 14:22:10 +00:00
|
|
|
var wg sync.WaitGroup
|
|
|
|
wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-shutdown:
|
|
|
|
if len(outMetricC) > 0 {
|
2018-07-12 00:33:27 +00:00
|
|
|
// keep going until channel is empty
|
2016-09-08 14:22:10 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
return
|
2018-07-12 00:33:27 +00:00
|
|
|
case metric := <-outMetricC:
|
|
|
|
for i, o := range a.Config.Outputs {
|
|
|
|
if i == len(a.Config.Outputs)-1 {
|
|
|
|
o.AddMetric(metric)
|
|
|
|
} else {
|
|
|
|
o.AddMetric(metric.Copy())
|
2016-09-08 14:22:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
2015-10-23 17:23:08 +00:00
|
|
|
|
2018-07-12 00:33:27 +00:00
|
|
|
wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
|
|
|
for metric := range aggMetricC {
|
|
|
|
// Apply Processors
|
|
|
|
metrics := []telegraf.Metric{metric}
|
|
|
|
for _, processor := range a.Config.Processors {
|
|
|
|
metrics = processor.Apply(metrics...)
|
|
|
|
}
|
|
|
|
outMetricC <- metric
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2017-07-13 22:34:21 +00:00
|
|
|
wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-shutdown:
|
2018-07-12 00:33:27 +00:00
|
|
|
if len(metricC) > 0 {
|
|
|
|
// keep going until channel is empty
|
2017-07-13 22:34:21 +00:00
|
|
|
continue
|
|
|
|
}
|
2018-07-12 00:33:27 +00:00
|
|
|
close(aggMetricC)
|
2017-07-13 22:34:21 +00:00
|
|
|
return
|
2018-07-12 00:33:27 +00:00
|
|
|
case metric := <-metricC:
|
|
|
|
// Apply Processors
|
2017-07-13 22:34:21 +00:00
|
|
|
metrics := []telegraf.Metric{metric}
|
|
|
|
for _, processor := range a.Config.Processors {
|
|
|
|
metrics = processor.Apply(metrics...)
|
|
|
|
}
|
2018-07-12 00:33:27 +00:00
|
|
|
|
|
|
|
for _, metric := range metrics {
|
|
|
|
// Apply Aggregators
|
|
|
|
var dropOriginal bool
|
|
|
|
for _, agg := range a.Config.Aggregators {
|
|
|
|
if ok := agg.Add(metric.Copy()); ok {
|
|
|
|
dropOriginal = true
|
2018-01-22 19:16:07 +00:00
|
|
|
}
|
|
|
|
}
|
2018-07-12 00:33:27 +00:00
|
|
|
|
|
|
|
// Forward metric to Outputs
|
|
|
|
if !dropOriginal {
|
|
|
|
outMetricC <- metric
|
|
|
|
}
|
2017-07-13 22:34:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2016-09-08 14:22:10 +00:00
|
|
|
ticker := time.NewTicker(a.Config.Agent.FlushInterval.Duration)
|
2017-01-17 23:01:12 +00:00
|
|
|
semaphore := make(chan struct{}, 1)
|
2015-10-16 22:13:32 +00:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-shutdown:
|
2016-09-30 21:37:56 +00:00
|
|
|
log.Println("I! Hang on, flushing any cached metrics before shutdown")
|
2016-09-08 14:22:10 +00:00
|
|
|
// wait for outMetricC to get flushed before flushing outputs
|
|
|
|
wg.Wait()
|
2016-01-22 18:54:12 +00:00
|
|
|
a.flush()
|
2015-10-16 22:13:32 +00:00
|
|
|
return nil
|
|
|
|
case <-ticker.C:
|
2017-01-17 23:01:12 +00:00
|
|
|
go func() {
|
|
|
|
select {
|
|
|
|
case semaphore <- struct{}{}:
|
|
|
|
internal.RandomSleep(a.Config.Agent.FlushJitter.Duration, shutdown)
|
|
|
|
a.flush()
|
|
|
|
<-semaphore
|
|
|
|
default:
|
|
|
|
// skipping this flush because one is already happening
|
|
|
|
log.Println("W! Skipping a scheduled flush because there is" +
|
|
|
|
" already a flush ongoing.")
|
|
|
|
}
|
|
|
|
}()
|
2015-10-16 22:13:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-04 14:58:32 +00:00
|
|
|
// Run runs the agent daemon, gathering every Interval
|
2015-04-07 16:23:58 +00:00
|
|
|
func (a *Agent) Run(shutdown chan struct{}) error {
|
2015-05-20 05:19:32 +00:00
|
|
|
var wg sync.WaitGroup
|
|
|
|
|
2016-09-30 21:37:56 +00:00
|
|
|
log.Printf("I! Agent Config: Interval:%s, Quiet:%#v, Hostname:%#v, "+
|
2016-01-15 19:25:56 +00:00
|
|
|
"Flush Interval:%s \n",
|
2016-09-30 21:37:56 +00:00
|
|
|
a.Config.Agent.Interval.Duration, a.Config.Agent.Quiet,
|
2016-01-07 20:39:43 +00:00
|
|
|
a.Config.Agent.Hostname, a.Config.Agent.FlushInterval.Duration)
|
2015-10-23 17:23:08 +00:00
|
|
|
|
2018-07-12 00:33:27 +00:00
|
|
|
// Channel shared between all input threads for accumulating metrics
|
2016-10-07 15:43:44 +00:00
|
|
|
metricC := make(chan telegraf.Metric, 100)
|
2018-07-12 00:33:27 +00:00
|
|
|
|
|
|
|
// Channel for metrics ready to be output
|
|
|
|
outMetricC := make(chan telegraf.Metric, 100)
|
|
|
|
|
|
|
|
// Channel for aggregated metrics
|
|
|
|
aggMetricC := make(chan telegraf.Metric, 100)
|
2015-10-16 22:13:32 +00:00
|
|
|
|
2015-10-21 20:05:27 +00:00
|
|
|
// Round collection to nearest interval by sleeping
|
2015-11-26 01:42:07 +00:00
|
|
|
if a.Config.Agent.RoundInterval {
|
|
|
|
i := int64(a.Config.Agent.Interval.Duration)
|
2015-10-21 20:05:27 +00:00
|
|
|
time.Sleep(time.Duration(i - (time.Now().UnixNano() % i)))
|
|
|
|
}
|
|
|
|
|
2015-10-16 22:13:32 +00:00
|
|
|
wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
2018-07-12 00:33:27 +00:00
|
|
|
if err := a.flusher(shutdown, metricC, aggMetricC, outMetricC); err != nil {
|
2016-09-30 21:37:56 +00:00
|
|
|
log.Printf("E! Flusher routine failed, exiting: %s\n", err.Error())
|
2015-10-16 22:13:32 +00:00
|
|
|
close(shutdown)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2016-10-10 12:43:47 +00:00
|
|
|
wg.Add(len(a.Config.Aggregators))
|
|
|
|
for _, aggregator := range a.Config.Aggregators {
|
|
|
|
go func(agg *models.RunningAggregator) {
|
|
|
|
defer wg.Done()
|
2018-07-12 00:33:27 +00:00
|
|
|
acc := NewAccumulator(agg, aggMetricC)
|
2016-10-10 12:43:47 +00:00
|
|
|
acc.SetPrecision(a.Config.Agent.Precision.Duration,
|
|
|
|
a.Config.Agent.Interval.Duration)
|
2018-01-19 01:37:53 +00:00
|
|
|
agg.Run(acc, shutdown)
|
2016-10-10 12:43:47 +00:00
|
|
|
}(aggregator)
|
|
|
|
}
|
|
|
|
|
2018-07-07 07:54:21 +00:00
|
|
|
// Service inputs may immediately add metrics, if metrics are added before
|
|
|
|
// the aggregator starts they will be dropped. Generally this occurs
|
|
|
|
// only during testing but it is an outstanding issue.
|
|
|
|
//
|
|
|
|
// https://github.com/influxdata/telegraf/issues/4394
|
2018-06-05 23:30:53 +00:00
|
|
|
for _, input := range a.Config.Inputs {
|
|
|
|
input.SetDefaultTags(a.Config.Tags)
|
|
|
|
switch p := input.Input.(type) {
|
|
|
|
case telegraf.ServiceInput:
|
|
|
|
acc := NewAccumulator(input, metricC)
|
|
|
|
// Service input plugins should set their own precision of their
|
|
|
|
// metrics.
|
|
|
|
acc.SetPrecision(time.Nanosecond, 0)
|
|
|
|
if err := p.Start(acc); err != nil {
|
|
|
|
log.Printf("E! Service for input %s failed to start, exiting\n%s\n",
|
|
|
|
input.Name(), err.Error())
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer p.Stop()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-07 07:54:21 +00:00
|
|
|
wg.Add(len(a.Config.Inputs))
|
|
|
|
for _, input := range a.Config.Inputs {
|
|
|
|
interval := a.Config.Agent.Interval.Duration
|
|
|
|
// overwrite global interval if this plugin has it's own.
|
|
|
|
if input.Config.Interval != 0 {
|
|
|
|
interval = input.Config.Interval
|
|
|
|
}
|
|
|
|
go func(in *models.RunningInput, interv time.Duration) {
|
|
|
|
defer wg.Done()
|
|
|
|
a.gatherer(shutdown, in, interv, metricC)
|
|
|
|
}(input, interval)
|
|
|
|
}
|
|
|
|
|
2016-05-19 15:36:58 +00:00
|
|
|
wg.Wait()
|
2017-01-21 23:37:53 +00:00
|
|
|
a.Close()
|
2016-05-19 15:36:58 +00:00
|
|
|
return nil
|
2015-04-01 16:34:32 +00:00
|
|
|
}
|