2015-05-22 23:45:14 +00:00
|
|
|
package telegraf
|
2015-04-01 16:34:32 +00:00
|
|
|
|
|
|
|
import (
|
2015-08-26 23:43:09 +00:00
|
|
|
"errors"
|
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"
|
2015-04-01 16:34:32 +00:00
|
|
|
"sort"
|
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-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-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-04-01 16:34:32 +00:00
|
|
|
Interval Duration
|
2015-08-04 14:58:32 +00:00
|
|
|
|
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
|
|
|
|
|
|
|
Config *Config
|
|
|
|
|
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{
|
|
|
|
Config: config,
|
|
|
|
Interval: Duration{10 * time.Second},
|
|
|
|
UTC: true,
|
|
|
|
Precision: "s",
|
|
|
|
}
|
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-05-22 23:33:38 +00:00
|
|
|
if config.Tags == nil {
|
|
|
|
config.Tags = map[string]string{}
|
2015-04-07 16:56:40 +00:00
|
|
|
}
|
|
|
|
|
2015-05-22 23:33:38 +00:00
|
|
|
config.Tags["host"] = agent.Hostname
|
|
|
|
|
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-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()
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-08-11 20:02:04 +00:00
|
|
|
// LoadOutputs loads the agent's outputs
|
2015-09-22 01:38:57 +00:00
|
|
|
func (a *Agent) LoadOutputs(filters []string) ([]string, error) {
|
2015-08-07 20:31:25 +00:00
|
|
|
var names []string
|
2015-04-01 16:34:32 +00:00
|
|
|
|
2015-08-07 20:31:25 +00:00
|
|
|
for _, name := range a.Config.OutputsDeclared() {
|
|
|
|
creator, ok := outputs.Outputs[name]
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("Undefined but requested output: %s", name)
|
|
|
|
}
|
|
|
|
|
2015-09-22 01:38:57 +00:00
|
|
|
if sliceContains(name, filters) || len(filters) == 0 {
|
2015-09-23 20:54:22 +00:00
|
|
|
if a.Debug {
|
|
|
|
log.Println("Output Enabled: ", name)
|
|
|
|
}
|
2015-09-22 01:38:57 +00:00
|
|
|
output := creator()
|
2015-08-07 20:31:25 +00:00
|
|
|
|
2015-09-22 01:38:57 +00:00
|
|
|
err := a.Config.ApplyOutput(name, output)
|
|
|
|
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-09-22 01:38:57 +00:00
|
|
|
func (a *Agent) LoadPlugins(filters []string) ([]string, error) {
|
2015-04-01 16:34:32 +00:00
|
|
|
var names []string
|
|
|
|
|
2015-05-18 21:10:12 +00:00
|
|
|
for _, name := range a.Config.PluginsDeclared() {
|
|
|
|
creator, ok := plugins.Plugins[name]
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("Undefined but requested plugin: %s", name)
|
|
|
|
}
|
2015-05-18 18:54:59 +00:00
|
|
|
|
2015-09-22 01:38:57 +00:00
|
|
|
if sliceContains(name, filters) || len(filters) == 0 {
|
2015-08-11 15:50:36 +00:00
|
|
|
plugin := creator()
|
2015-09-22 01:38:57 +00:00
|
|
|
|
2015-08-11 15:50:36 +00:00
|
|
|
config, err := a.Config.ApplyPlugin(name, plugin)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
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-08-26 17:02:10 +00:00
|
|
|
// crankParallel runs the plugins that are using the same reporting interval
|
|
|
|
// as the telegraf agent.
|
2015-05-20 05:19:32 +00:00
|
|
|
func (a *Agent) crankParallel() error {
|
|
|
|
points := make(chan *BatchPoints, len(a.plugins))
|
|
|
|
|
|
|
|
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-09-10 03:27:58 +00:00
|
|
|
var bp BatchPoints
|
|
|
|
bp.Debug = a.Debug
|
|
|
|
bp.Prefix = plugin.name + "_"
|
|
|
|
bp.Config = plugin.config
|
|
|
|
bp.Precision = a.Precision
|
2015-05-20 05:19:32 +00:00
|
|
|
|
2015-09-10 03:27:58 +00:00
|
|
|
if err := plugin.plugin.Gather(&bp); 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-09-10 03:27:58 +00:00
|
|
|
points <- &bp
|
2015-05-20 05:19:32 +00:00
|
|
|
}(plugin)
|
|
|
|
}
|
|
|
|
|
|
|
|
wg.Wait()
|
|
|
|
|
|
|
|
close(points)
|
|
|
|
|
2015-08-11 20:02:04 +00:00
|
|
|
var bp BatchPoints
|
|
|
|
bp.Time = time.Now()
|
2015-09-02 16:30:44 +00:00
|
|
|
if a.UTC {
|
|
|
|
bp.Time = bp.Time.UTC()
|
|
|
|
}
|
2015-08-11 20:02:04 +00:00
|
|
|
bp.Tags = a.Config.Tags
|
2015-09-02 16:30:44 +00:00
|
|
|
bp.Precision = a.Precision
|
2015-05-20 05:19:32 +00:00
|
|
|
|
|
|
|
for sub := range points {
|
2015-08-11 20:02:04 +00:00
|
|
|
bp.Points = append(bp.Points, sub.Points...)
|
2015-05-20 05:19:32 +00:00
|
|
|
}
|
|
|
|
|
2015-09-28 20:08:28 +00:00
|
|
|
elapsed := time.Since(start)
|
|
|
|
log.Printf("Cranking default interval, gathered %d metrics from %d plugins in %s\n",
|
|
|
|
len(bp.Points), counter, elapsed)
|
2015-08-11 20:02:04 +00:00
|
|
|
return a.flush(&bp)
|
2015-05-20 05:19:32 +00:00
|
|
|
}
|
|
|
|
|
2015-08-26 17:02:10 +00:00
|
|
|
// crank is mostly for test purposes.
|
2015-04-01 16:34:32 +00:00
|
|
|
func (a *Agent) crank() error {
|
2015-08-11 20:02:04 +00:00
|
|
|
var bp BatchPoints
|
2015-04-06 16:32:10 +00:00
|
|
|
|
2015-08-11 20:02:04 +00:00
|
|
|
bp.Debug = a.Debug
|
2015-09-10 03:27:58 +00:00
|
|
|
bp.Precision = a.Precision
|
2015-04-07 00:24:24 +00:00
|
|
|
|
2015-04-01 16:34:32 +00:00
|
|
|
for _, plugin := range a.plugins {
|
2015-08-11 20:02:04 +00:00
|
|
|
bp.Prefix = plugin.name + "_"
|
|
|
|
bp.Config = plugin.config
|
|
|
|
err := plugin.plugin.Gather(&bp)
|
2015-04-01 16:34:32 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-11 20:02:04 +00:00
|
|
|
bp.Tags = a.Config.Tags
|
2015-09-02 16:30:44 +00:00
|
|
|
bp.Time = time.Now()
|
|
|
|
if a.UTC {
|
|
|
|
bp.Time = bp.Time.UTC()
|
|
|
|
}
|
2015-04-06 16:32:10 +00:00
|
|
|
|
2015-08-11 20:02:04 +00:00
|
|
|
return a.flush(&bp)
|
2015-04-01 16:34:32 +00:00
|
|
|
}
|
|
|
|
|
2015-08-26 17:02:10 +00:00
|
|
|
// crankSeparate runs the plugins that have been configured with their own
|
|
|
|
// reporting interval.
|
2015-05-20 05:19:32 +00:00
|
|
|
func (a *Agent) crankSeparate(shutdown chan struct{}, plugin *runningPlugin) error {
|
|
|
|
ticker := time.NewTicker(plugin.config.Interval)
|
|
|
|
|
|
|
|
for {
|
2015-08-11 20:02:04 +00:00
|
|
|
var bp BatchPoints
|
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-08-11 20:02:04 +00:00
|
|
|
bp.Debug = a.Debug
|
2015-05-20 05:19:32 +00:00
|
|
|
|
2015-08-11 20:02:04 +00:00
|
|
|
bp.Prefix = plugin.name + "_"
|
|
|
|
bp.Config = plugin.config
|
2015-09-10 03:27:58 +00:00
|
|
|
bp.Precision = a.Precision
|
2015-08-26 23:43:09 +00:00
|
|
|
|
|
|
|
if err := plugin.plugin.Gather(&bp); err != nil {
|
|
|
|
log.Printf("Error in plugin [%s]: %s", plugin.name, err)
|
|
|
|
outerr = errors.New("Error encountered processing plugins & outputs")
|
2015-05-20 05:19:32 +00:00
|
|
|
}
|
|
|
|
|
2015-08-11 20:02:04 +00:00
|
|
|
bp.Tags = a.Config.Tags
|
|
|
|
bp.Time = time.Now()
|
2015-09-02 16:30:44 +00:00
|
|
|
if a.UTC {
|
|
|
|
bp.Time = bp.Time.UTC()
|
|
|
|
}
|
2015-05-20 05:19:32 +00:00
|
|
|
|
2015-09-28 20:08:28 +00:00
|
|
|
elapsed := time.Since(start)
|
|
|
|
log.Printf("Cranking separate interval, gathered %d metrics from %s in %s\n",
|
|
|
|
len(bp.Points), plugin.name, elapsed)
|
2015-08-26 23:43:09 +00:00
|
|
|
if err := a.flush(&bp); err != nil {
|
|
|
|
outerr = errors.New("Error encountered processing plugins & outputs")
|
|
|
|
}
|
|
|
|
|
|
|
|
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-07 20:31:25 +00:00
|
|
|
func (a *Agent) flush(bp *BatchPoints) error {
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
var outerr error
|
2015-08-26 23:43:09 +00:00
|
|
|
|
2015-08-07 20:31:25 +00:00
|
|
|
for _, o := range a.outputs {
|
|
|
|
wg.Add(1)
|
|
|
|
go func(ro *runningOutput) {
|
|
|
|
defer wg.Done()
|
2015-08-26 23:43:09 +00:00
|
|
|
// Log all output errors:
|
|
|
|
if err := ro.output.Write(bp.BatchPoints); err != nil {
|
|
|
|
log.Printf("Error in output [%s]: %s", ro.name, err)
|
|
|
|
outerr = errors.New("Error encountered flushing outputs")
|
|
|
|
}
|
2015-08-07 20:31:25 +00:00
|
|
|
}(o)
|
|
|
|
}
|
|
|
|
|
|
|
|
wg.Wait()
|
|
|
|
return outerr
|
|
|
|
}
|
|
|
|
|
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 {
|
|
|
|
var acc BatchPoints
|
|
|
|
|
|
|
|
acc.Debug = true
|
|
|
|
|
|
|
|
for _, plugin := range a.plugins {
|
2015-05-18 19:15:15 +00:00
|
|
|
acc.Prefix = plugin.name + "_"
|
2015-05-20 05:19:32 +00:00
|
|
|
acc.Config = plugin.config
|
|
|
|
|
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-08-26 23:43:09 +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 {
|
|
|
|
case "cpu":
|
|
|
|
time.Sleep(500 * time.Millisecond)
|
|
|
|
fmt.Printf("* Plugin: %s, Collection 2\n", plugin.name)
|
|
|
|
if err := plugin.plugin.Gather(&acc); err != nil {
|
|
|
|
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-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
|
|
|
|
|
|
|
|
for _, plugin := range a.plugins {
|
|
|
|
if plugin.config.Interval != 0 {
|
|
|
|
wg.Add(1)
|
|
|
|
go func(plugin *runningPlugin) {
|
|
|
|
defer wg.Done()
|
2015-08-26 23:43:09 +00:00
|
|
|
if err := a.crankSeparate(shutdown, plugin); err != nil {
|
|
|
|
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
|
|
|
ticker := time.NewTicker(a.Interval.Duration)
|
|
|
|
|
|
|
|
for {
|
2015-08-26 23:43:09 +00:00
|
|
|
if err := a.crankParallel(); err != nil {
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|