2015-05-22 23:45:14 +00:00
|
|
|
package telegraf
|
2015-04-01 16:34:32 +00:00
|
|
|
|
|
|
|
import (
|
2015-10-23 17:23:08 +00:00
|
|
|
"crypto/rand"
|
2015-05-18 21:10:12 +00:00
|
|
|
"fmt"
|
2015-04-01 16:34:32 +00:00
|
|
|
"log"
|
2015-10-23 17:23:08 +00:00
|
|
|
"math/big"
|
2015-04-07 16:56:40 +00:00
|
|
|
"os"
|
2015-04-01 16:34:32 +00:00
|
|
|
"sort"
|
2015-11-13 23:14:07 +00:00
|
|
|
"strings"
|
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
|
|
|
|
2015-11-10 21:40:39 +00:00
|
|
|
"github.com/influxdb/telegraf/internal"
|
2015-08-07 20:31:25 +00:00
|
|
|
"github.com/influxdb/telegraf/outputs"
|
2015-05-22 23:45:14 +00:00
|
|
|
"github.com/influxdb/telegraf/plugins"
|
2015-10-16 22:13:32 +00:00
|
|
|
|
|
|
|
"github.com/influxdb/influxdb/client/v2"
|
2015-04-01 16:34:32 +00:00
|
|
|
)
|
|
|
|
|
2015-08-07 20:31:25 +00:00
|
|
|
type runningOutput struct {
|
|
|
|
name string
|
|
|
|
output outputs.Output
|
|
|
|
}
|
|
|
|
|
2015-05-18 19:15:15 +00:00
|
|
|
type runningPlugin struct {
|
|
|
|
name string
|
|
|
|
plugin plugins.Plugin
|
2015-05-20 05:19:32 +00:00
|
|
|
config *ConfiguredPlugin
|
2015-05-18 19:15:15 +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-08-04 14:58:32 +00:00
|
|
|
|
|
|
|
// Interval at which to gather information
|
2015-11-10 21:40:39 +00:00
|
|
|
Interval internal.Duration
|
2015-08-04 14:58:32 +00:00
|
|
|
|
2015-10-21 20:05:27 +00:00
|
|
|
// RoundInterval rounds collection interval to 'interval'.
|
|
|
|
// ie, if Interval=10s then always collect on :00, :10, :20, etc.
|
|
|
|
RoundInterval bool
|
|
|
|
|
2015-10-16 22:13:32 +00:00
|
|
|
// Interval at which to flush data
|
2015-11-10 21:40:39 +00:00
|
|
|
FlushInterval internal.Duration
|
2015-10-16 22:13:32 +00:00
|
|
|
|
2015-10-21 16:57:51 +00:00
|
|
|
// FlushRetries is the number of times to retry each data flush
|
|
|
|
FlushRetries int
|
|
|
|
|
2015-10-21 20:05:27 +00:00
|
|
|
// FlushJitter tells
|
2015-11-10 21:40:39 +00:00
|
|
|
FlushJitter internal.Duration
|
2015-10-21 20:05:27 +00:00
|
|
|
|
2015-10-16 22:13:32 +00:00
|
|
|
// TODO(cam): Remove UTC and Precision parameters, they are no longer
|
|
|
|
// valid for the agent config. Leaving them here for now for backwards-
|
|
|
|
// compatability
|
|
|
|
|
2015-09-02 16:30:44 +00:00
|
|
|
// Option for outputting data in UTC
|
|
|
|
UTC bool `toml:"utc"`
|
|
|
|
|
|
|
|
// Precision to write data at
|
|
|
|
// Valid values for Precision are n, u, ms, s, m, and h
|
|
|
|
Precision string
|
|
|
|
|
|
|
|
// Option for running in debug mode
|
2015-04-01 16:34:32 +00:00
|
|
|
Debug bool
|
2015-04-07 16:56:40 +00:00
|
|
|
Hostname string
|
2015-04-01 16:34:32 +00:00
|
|
|
|
2015-10-17 04:47:13 +00:00
|
|
|
Tags map[string]string
|
2015-04-01 16:34:32 +00:00
|
|
|
|
2015-08-07 20:31:25 +00:00
|
|
|
outputs []*runningOutput
|
2015-05-18 19:15:15 +00:00
|
|
|
plugins []*runningPlugin
|
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-04-06 16:32:10 +00:00
|
|
|
func NewAgent(config *Config) (*Agent, error) {
|
2015-09-02 16:30:44 +00:00
|
|
|
agent := &Agent{
|
2015-10-17 04:47:13 +00:00
|
|
|
Tags: make(map[string]string),
|
2015-11-10 21:40:39 +00:00
|
|
|
Interval: internal.Duration{10 * time.Second},
|
2015-10-21 20:05:27 +00:00
|
|
|
RoundInterval: true,
|
2015-11-10 21:40:39 +00:00
|
|
|
FlushInterval: internal.Duration{10 * time.Second},
|
2015-10-21 16:57:51 +00:00
|
|
|
FlushRetries: 2,
|
2015-11-10 21:40:39 +00:00
|
|
|
FlushJitter: internal.Duration{5 * time.Second},
|
2015-09-02 16:30:44 +00:00
|
|
|
}
|
2015-04-01 16:34:32 +00:00
|
|
|
|
2015-09-02 16:30:44 +00:00
|
|
|
// Apply the toml table to the agent config, overriding defaults
|
2015-05-20 05:19:32 +00:00
|
|
|
err := config.ApplyAgent(agent)
|
2015-04-01 16:34:32 +00:00
|
|
|
if err != nil {
|
2015-04-06 16:32:10 +00:00
|
|
|
return nil, err
|
2015-04-01 16:34:32 +00:00
|
|
|
}
|
|
|
|
|
2015-04-07 16:56:40 +00:00
|
|
|
if agent.Hostname == "" {
|
|
|
|
hostname, err := os.Hostname()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
agent.Hostname = hostname
|
2015-05-22 23:33:38 +00:00
|
|
|
}
|
2015-04-07 16:56:40 +00:00
|
|
|
|
2015-10-17 04:47:13 +00:00
|
|
|
agent.Tags["host"] = agent.Hostname
|
2015-05-22 23:33:38 +00:00
|
|
|
|
2015-04-07 00:24:24 +00:00
|
|
|
return agent, nil
|
|
|
|
}
|
|
|
|
|
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-08-07 20:31:25 +00:00
|
|
|
for _, o := range a.outputs {
|
2015-10-22 16:17:57 +00:00
|
|
|
switch ot := o.output.(type) {
|
|
|
|
case outputs.ServiceOutput:
|
|
|
|
if err := ot.Start(); err != nil {
|
|
|
|
log.Printf("Service for output %s failed to start, exiting\n%s\n",
|
|
|
|
o.name, err.Error())
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-09 21:56:10 +00:00
|
|
|
if a.Debug {
|
|
|
|
log.Printf("Attempting connection to output: %s\n", o.name)
|
|
|
|
}
|
2015-08-11 20:02:04 +00:00
|
|
|
err := o.output.Connect()
|
2015-08-07 20:31:25 +00:00
|
|
|
if err != nil {
|
2015-09-19 01:02:16 +00:00
|
|
|
log.Printf("Failed to connect to output %s, retrying in 15s\n", o.name)
|
|
|
|
time.Sleep(15 * time.Second)
|
|
|
|
err = o.output.Connect()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-08-07 20:31:25 +00:00
|
|
|
}
|
2015-08-26 17:02:10 +00:00
|
|
|
if a.Debug {
|
|
|
|
log.Printf("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
|
|
|
|
for _, o := range a.outputs {
|
|
|
|
err = o.output.Close()
|
2015-10-22 16:17:57 +00:00
|
|
|
switch ot := o.output.(type) {
|
|
|
|
case outputs.ServiceOutput:
|
|
|
|
ot.Stop()
|
|
|
|
}
|
2015-08-12 17:04:25 +00:00
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-08-11 20:02:04 +00:00
|
|
|
// LoadOutputs loads the agent's outputs
|
2015-10-17 04:47:13 +00:00
|
|
|
func (a *Agent) LoadOutputs(filters []string, config *Config) ([]string, error) {
|
2015-08-07 20:31:25 +00:00
|
|
|
var names []string
|
2015-04-01 16:34:32 +00:00
|
|
|
|
2015-11-13 23:14:07 +00:00
|
|
|
for name, output := range config.OutputsDeclared() {
|
|
|
|
// Trim the ID off the output name for filtering
|
|
|
|
filtername := strings.TrimRight(name, "-0123456789")
|
|
|
|
if sliceContains(filtername, filters) || len(filters) == 0 {
|
2015-09-23 20:54:22 +00:00
|
|
|
if a.Debug {
|
|
|
|
log.Println("Output Enabled: ", name)
|
|
|
|
}
|
2015-08-07 20:31:25 +00:00
|
|
|
|
2015-10-17 04:47:13 +00:00
|
|
|
err := config.ApplyOutput(name, output)
|
2015-09-22 01:38:57 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-08-07 20:31:25 +00:00
|
|
|
|
2015-09-22 01:38:57 +00:00
|
|
|
a.outputs = append(a.outputs, &runningOutput{name, output})
|
|
|
|
names = append(names, name)
|
|
|
|
}
|
2015-04-06 16:32:10 +00:00
|
|
|
}
|
|
|
|
|
2015-08-11 20:02:04 +00:00
|
|
|
sort.Strings(names)
|
2015-04-01 16:34:32 +00:00
|
|
|
|
2015-08-07 20:31:25 +00:00
|
|
|
return names, nil
|
2015-04-01 16:34:32 +00:00
|
|
|
}
|
|
|
|
|
2015-08-04 14:58:32 +00:00
|
|
|
// LoadPlugins loads the agent's plugins
|
2015-10-17 04:47:13 +00:00
|
|
|
func (a *Agent) LoadPlugins(filters []string, config *Config) ([]string, error) {
|
2015-04-01 16:34:32 +00:00
|
|
|
var names []string
|
|
|
|
|
2015-11-13 23:14:07 +00:00
|
|
|
for name, plugin := range config.PluginsDeclared() {
|
2015-09-22 01:38:57 +00:00
|
|
|
if sliceContains(name, filters) || len(filters) == 0 {
|
2015-11-18 00:57:29 +00:00
|
|
|
config := config.GetPluginConfig(name)
|
2015-08-11 15:50:36 +00:00
|
|
|
a.plugins = append(a.plugins, &runningPlugin{name, plugin, config})
|
|
|
|
names = append(names, name)
|
|
|
|
}
|
2015-04-01 16:34:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
sort.Strings(names)
|
|
|
|
|
|
|
|
return names, nil
|
|
|
|
}
|
|
|
|
|
2015-10-16 22:13:32 +00:00
|
|
|
// gatherParallel runs the plugins that are using the same reporting interval
|
2015-08-26 17:02:10 +00:00
|
|
|
// as the telegraf agent.
|
2015-10-16 22:13:32 +00:00
|
|
|
func (a *Agent) gatherParallel(pointChan chan *client.Point) error {
|
2015-05-20 05:19:32 +00:00
|
|
|
var wg sync.WaitGroup
|
|
|
|
|
2015-09-28 20:08:28 +00:00
|
|
|
start := time.Now()
|
|
|
|
counter := 0
|
2015-05-20 05:19:32 +00:00
|
|
|
for _, plugin := range a.plugins {
|
|
|
|
if plugin.config.Interval != 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
wg.Add(1)
|
2015-09-28 20:08:28 +00:00
|
|
|
counter++
|
2015-05-20 05:19:32 +00:00
|
|
|
go func(plugin *runningPlugin) {
|
|
|
|
defer wg.Done()
|
|
|
|
|
2015-10-16 22:13:32 +00:00
|
|
|
acc := NewAccumulator(plugin.config, pointChan)
|
|
|
|
acc.SetDebug(a.Debug)
|
|
|
|
acc.SetPrefix(plugin.name + "_")
|
2015-10-17 04:47:13 +00:00
|
|
|
acc.SetDefaultTags(a.Tags)
|
2015-05-20 05:19:32 +00:00
|
|
|
|
2015-10-16 22:13:32 +00:00
|
|
|
if err := plugin.plugin.Gather(acc); err != nil {
|
2015-08-24 17:25:15 +00:00
|
|
|
log.Printf("Error in plugin [%s]: %s", plugin.name, err)
|
2015-08-22 20:03:30 +00:00
|
|
|
}
|
|
|
|
|
2015-05-20 05:19:32 +00:00
|
|
|
}(plugin)
|
|
|
|
}
|
|
|
|
|
|
|
|
wg.Wait()
|
|
|
|
|
2015-09-28 20:08:28 +00:00
|
|
|
elapsed := time.Since(start)
|
2015-10-20 22:45:31 +00:00
|
|
|
log.Printf("Gathered metrics, (%s interval), from %d plugins in %s\n",
|
2015-10-16 22:13:32 +00:00
|
|
|
a.Interval, counter, elapsed)
|
|
|
|
return nil
|
2015-04-01 16:34:32 +00:00
|
|
|
}
|
|
|
|
|
2015-10-16 22:13:32 +00:00
|
|
|
// gatherSeparate runs the plugins that have been configured with their own
|
2015-08-26 17:02:10 +00:00
|
|
|
// reporting interval.
|
2015-10-16 22:13:32 +00:00
|
|
|
func (a *Agent) gatherSeparate(
|
|
|
|
shutdown chan struct{},
|
|
|
|
plugin *runningPlugin,
|
|
|
|
pointChan chan *client.Point,
|
|
|
|
) error {
|
2015-05-20 05:19:32 +00:00
|
|
|
ticker := time.NewTicker(plugin.config.Interval)
|
|
|
|
|
|
|
|
for {
|
2015-08-26 23:43:09 +00:00
|
|
|
var outerr error
|
2015-09-28 20:08:28 +00:00
|
|
|
start := time.Now()
|
2015-05-20 05:19:32 +00:00
|
|
|
|
2015-10-16 22:13:32 +00:00
|
|
|
acc := NewAccumulator(plugin.config, pointChan)
|
|
|
|
acc.SetDebug(a.Debug)
|
|
|
|
acc.SetPrefix(plugin.name + "_")
|
2015-10-17 04:47:13 +00:00
|
|
|
acc.SetDefaultTags(a.Tags)
|
2015-08-26 23:43:09 +00:00
|
|
|
|
2015-10-16 22:13:32 +00:00
|
|
|
if err := plugin.plugin.Gather(acc); err != nil {
|
2015-08-26 23:43:09 +00:00
|
|
|
log.Printf("Error in plugin [%s]: %s", plugin.name, err)
|
2015-09-02 16:30:44 +00:00
|
|
|
}
|
2015-05-20 05:19:32 +00:00
|
|
|
|
2015-09-28 20:08:28 +00:00
|
|
|
elapsed := time.Since(start)
|
2015-10-20 22:45:31 +00:00
|
|
|
log.Printf("Gathered metrics, (separate %s interval), from %s in %s\n",
|
2015-10-16 22:13:32 +00:00
|
|
|
plugin.config.Interval, plugin.name, elapsed)
|
2015-08-26 23:43:09 +00:00
|
|
|
|
|
|
|
if outerr != nil {
|
|
|
|
return outerr
|
2015-08-07 20:31:25 +00:00
|
|
|
}
|
2015-05-20 05:19:32 +00:00
|
|
|
|
|
|
|
select {
|
|
|
|
case <-shutdown:
|
|
|
|
return nil
|
|
|
|
case <-ticker.C:
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-04 14:58:32 +00:00
|
|
|
// Test verifies that we can 'Gather' from all plugins with their configured
|
|
|
|
// 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)
|
|
|
|
pointChan := make(chan *client.Point)
|
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 {
|
|
|
|
case <-pointChan:
|
|
|
|
// do nothing
|
|
|
|
case <-shutdown:
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
2015-04-07 00:24:24 +00:00
|
|
|
|
|
|
|
for _, plugin := range a.plugins {
|
2015-10-16 22:13:32 +00:00
|
|
|
acc := NewAccumulator(plugin.config, pointChan)
|
|
|
|
acc.SetDebug(true)
|
|
|
|
acc.SetPrefix(plugin.name + "_")
|
2015-05-20 05:19:32 +00:00
|
|
|
|
2015-09-23 20:54:22 +00:00
|
|
|
fmt.Printf("* Plugin: %s, Collection 1\n", plugin.name)
|
2015-05-20 05:19:32 +00:00
|
|
|
if plugin.config.Interval != 0 {
|
|
|
|
fmt.Printf("* Internal: %s\n", plugin.config.Interval)
|
|
|
|
}
|
|
|
|
|
2015-10-16 22:13:32 +00:00
|
|
|
if err := plugin.plugin.Gather(acc); err != nil {
|
2015-04-07 00:24:24 +00:00
|
|
|
return err
|
|
|
|
}
|
2015-09-21 17:05:58 +00:00
|
|
|
|
2015-09-23 20:54:22 +00:00
|
|
|
// Special instructions for some plugins. cpu, for example, needs to be
|
|
|
|
// run twice in order to return cpu usage percentages.
|
|
|
|
switch plugin.name {
|
2015-11-03 00:23:40 +00:00
|
|
|
case "cpu", "mongodb":
|
2015-09-23 20:54:22 +00:00
|
|
|
time.Sleep(500 * time.Millisecond)
|
|
|
|
fmt.Printf("* Plugin: %s, Collection 2\n", plugin.name)
|
2015-10-16 22:13:32 +00:00
|
|
|
if err := plugin.plugin.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
|
|
|
|
}
|
|
|
|
|
2015-10-21 20:05:27 +00:00
|
|
|
// writeOutput writes a list of points to a single output, with retries.
|
|
|
|
// Optionally takes a `done` channel to indicate that it is done writing.
|
2015-10-21 16:57:51 +00:00
|
|
|
func (a *Agent) writeOutput(
|
|
|
|
points []*client.Point,
|
|
|
|
ro *runningOutput,
|
|
|
|
shutdown chan struct{},
|
2015-10-21 20:05:27 +00:00
|
|
|
wg *sync.WaitGroup,
|
2015-10-21 16:57:51 +00:00
|
|
|
) {
|
2015-10-21 20:05:27 +00:00
|
|
|
defer wg.Done()
|
|
|
|
if len(points) == 0 {
|
|
|
|
return
|
|
|
|
}
|
2015-10-21 16:57:51 +00:00
|
|
|
retry := 0
|
|
|
|
retries := a.FlushRetries
|
2015-10-20 22:45:31 +00:00
|
|
|
start := time.Now()
|
2015-10-16 22:13:32 +00:00
|
|
|
|
2015-10-21 16:57:51 +00:00
|
|
|
for {
|
|
|
|
err := ro.output.Write(points)
|
2015-10-21 20:05:27 +00:00
|
|
|
if err == nil {
|
|
|
|
// Write successful
|
|
|
|
elapsed := time.Since(start)
|
|
|
|
log.Printf("Flushed %d metrics to output %s in %s\n",
|
|
|
|
len(points), ro.name, elapsed)
|
|
|
|
return
|
|
|
|
}
|
2015-10-21 16:57:51 +00:00
|
|
|
|
|
|
|
select {
|
|
|
|
case <-shutdown:
|
|
|
|
return
|
|
|
|
default:
|
2015-10-21 20:05:27 +00:00
|
|
|
if retry >= retries {
|
2015-10-21 16:57:51 +00:00
|
|
|
// No more retries
|
|
|
|
msg := "FATAL: Write to output [%s] failed %d times, dropping" +
|
|
|
|
" %d metrics\n"
|
|
|
|
log.Printf(msg, ro.name, retries+1, len(points))
|
|
|
|
return
|
|
|
|
} else if err != nil {
|
|
|
|
// Sleep for a retry
|
|
|
|
log.Printf("Error in output [%s]: %s, retrying in %s",
|
|
|
|
ro.name, err.Error(), a.FlushInterval.Duration)
|
|
|
|
time.Sleep(a.FlushInterval.Duration)
|
2015-10-16 22:13:32 +00:00
|
|
|
}
|
2015-10-21 16:57:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
retry++
|
2015-10-16 22:13:32 +00:00
|
|
|
}
|
2015-10-21 16:57:51 +00:00
|
|
|
}
|
2015-10-16 22:13:32 +00:00
|
|
|
|
2015-10-21 16:57:51 +00:00
|
|
|
// flush writes a list of points to all configured outputs
|
2015-10-21 20:05:27 +00:00
|
|
|
func (a *Agent) flush(
|
|
|
|
points []*client.Point,
|
|
|
|
shutdown chan struct{},
|
|
|
|
wait bool,
|
|
|
|
) {
|
|
|
|
var wg sync.WaitGroup
|
2015-10-21 16:57:51 +00:00
|
|
|
for _, o := range a.outputs {
|
2015-10-21 20:05:27 +00:00
|
|
|
wg.Add(1)
|
|
|
|
go a.writeOutput(points, o, shutdown, &wg)
|
|
|
|
}
|
|
|
|
if wait {
|
|
|
|
wg.Wait()
|
2015-10-21 16:57:51 +00:00
|
|
|
}
|
2015-10-16 22:13:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// flusher monitors the points input channel and flushes on the minimum interval
|
|
|
|
func (a *Agent) flusher(shutdown chan struct{}, pointChan chan *client.Point) error {
|
2015-10-20 22:45:31 +00:00
|
|
|
// Inelegant, but this sleep is to allow the Gather threads to run, so that
|
|
|
|
// the flusher will flush after metrics are collected.
|
|
|
|
time.Sleep(time.Millisecond * 100)
|
2015-10-23 17:23:08 +00:00
|
|
|
|
2015-10-16 22:13:32 +00:00
|
|
|
ticker := time.NewTicker(a.FlushInterval.Duration)
|
|
|
|
points := make([]*client.Point, 0)
|
2015-10-23 17:23:08 +00:00
|
|
|
|
2015-10-16 22:13:32 +00:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-shutdown:
|
2015-10-21 16:57:51 +00:00
|
|
|
log.Println("Hang on, flushing any cached points before shutdown")
|
2015-10-21 20:05:27 +00:00
|
|
|
a.flush(points, shutdown, true)
|
2015-10-16 22:13:32 +00:00
|
|
|
return nil
|
|
|
|
case <-ticker.C:
|
2015-10-23 17:23:08 +00:00
|
|
|
a.flush(points, shutdown, false)
|
2015-10-16 22:13:32 +00:00
|
|
|
points = make([]*client.Point, 0)
|
|
|
|
case pt := <-pointChan:
|
|
|
|
points = append(points, pt)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-23 17:23:08 +00:00
|
|
|
// jitterInterval applies the the interval jitter to the flush interval using
|
|
|
|
// crypto/rand number generator
|
|
|
|
func jitterInterval(ininterval, injitter time.Duration) time.Duration {
|
|
|
|
var jitter int64
|
|
|
|
outinterval := ininterval
|
|
|
|
if injitter.Nanoseconds() != 0 {
|
|
|
|
maxjitter := big.NewInt(injitter.Nanoseconds())
|
|
|
|
if j, err := rand.Int(rand.Reader, maxjitter); err == nil {
|
|
|
|
jitter = j.Int64()
|
|
|
|
}
|
|
|
|
outinterval = time.Duration(jitter + ininterval.Nanoseconds())
|
|
|
|
}
|
|
|
|
|
|
|
|
if outinterval.Nanoseconds() < time.Duration(500*time.Millisecond).Nanoseconds() {
|
|
|
|
log.Printf("Flush interval %s too low, setting to 500ms\n", outinterval)
|
|
|
|
outinterval = time.Duration(500 * time.Millisecond)
|
|
|
|
}
|
|
|
|
|
|
|
|
return outinterval
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
|
2015-10-23 17:23:08 +00:00
|
|
|
a.FlushInterval.Duration = jitterInterval(a.FlushInterval.Duration,
|
|
|
|
a.FlushJitter.Duration)
|
|
|
|
|
|
|
|
log.Printf("Agent Config: Interval:%s, Debug:%#v, Hostname:%#v, "+
|
|
|
|
"Flush Interval:%s\n",
|
|
|
|
a.Interval, a.Debug, a.Hostname, a.FlushInterval)
|
|
|
|
|
2015-10-16 22:13:32 +00:00
|
|
|
// channel shared between all plugin threads for accumulating points
|
|
|
|
pointChan := make(chan *client.Point, 1000)
|
|
|
|
|
2015-10-21 20:05:27 +00:00
|
|
|
// Round collection to nearest interval by sleeping
|
|
|
|
if a.RoundInterval {
|
|
|
|
i := int64(a.Interval.Duration)
|
|
|
|
time.Sleep(time.Duration(i - (time.Now().UnixNano() % i)))
|
|
|
|
}
|
|
|
|
ticker := time.NewTicker(a.Interval.Duration)
|
|
|
|
|
2015-10-16 22:13:32 +00:00
|
|
|
wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
|
|
|
if err := a.flusher(shutdown, pointChan); err != nil {
|
|
|
|
log.Printf("Flusher routine failed, exiting: %s\n", err.Error())
|
|
|
|
close(shutdown)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2015-05-20 05:19:32 +00:00
|
|
|
for _, plugin := range a.plugins {
|
2015-09-24 18:06:11 +00:00
|
|
|
|
|
|
|
// Start service of any ServicePlugins
|
|
|
|
switch p := plugin.plugin.(type) {
|
|
|
|
case plugins.ServicePlugin:
|
|
|
|
if err := p.Start(); err != nil {
|
|
|
|
log.Printf("Service for plugin %s failed to start, exiting\n%s\n",
|
|
|
|
plugin.name, err.Error())
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer p.Stop()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Special handling for plugins that have their own collection interval
|
2015-10-16 22:13:32 +00:00
|
|
|
// configured. Default intervals are handled below with gatherParallel
|
2015-05-20 05:19:32 +00:00
|
|
|
if plugin.config.Interval != 0 {
|
|
|
|
wg.Add(1)
|
|
|
|
go func(plugin *runningPlugin) {
|
|
|
|
defer wg.Done()
|
2015-10-16 22:13:32 +00:00
|
|
|
if err := a.gatherSeparate(shutdown, plugin, pointChan); err != nil {
|
2015-08-26 23:43:09 +00:00
|
|
|
log.Printf(err.Error())
|
2015-08-22 20:03:30 +00:00
|
|
|
}
|
2015-05-20 05:19:32 +00:00
|
|
|
}(plugin)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
defer wg.Wait()
|
|
|
|
|
2015-04-01 16:34:32 +00:00
|
|
|
for {
|
2015-10-16 22:13:32 +00:00
|
|
|
if err := a.gatherParallel(pointChan); err != nil {
|
2015-08-26 23:43:09 +00:00
|
|
|
log.Printf(err.Error())
|
2015-04-01 16:34:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-shutdown:
|
2015-04-07 16:23:58 +00:00
|
|
|
return nil
|
2015-04-01 16:34:32 +00:00
|
|
|
case <-ticker.C:
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|