2015-05-22 23:45:14 +00:00
|
|
|
package telegraf
|
2015-04-01 16:34:32 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"sort"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2015-08-25 23:59:12 +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
|
|
|
"github.com/naoina/toml"
|
|
|
|
"github.com/naoina/toml/ast"
|
|
|
|
)
|
|
|
|
|
2015-08-04 14:58:32 +00:00
|
|
|
// Duration just wraps time.Duration
|
2015-04-01 16:34:32 +00:00
|
|
|
type Duration struct {
|
|
|
|
time.Duration
|
|
|
|
}
|
|
|
|
|
2015-08-04 14:58:32 +00:00
|
|
|
// UnmarshalTOML parses the duration from the TOML config file
|
2015-04-01 16:34:32 +00:00
|
|
|
func (d *Duration) UnmarshalTOML(b []byte) error {
|
|
|
|
dur, err := time.ParseDuration(string(b[1 : len(b)-1]))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
d.Duration = dur
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-08-11 16:34:00 +00:00
|
|
|
// Config specifies the URL/user/password for the database that telegraf
|
2015-08-04 14:58:32 +00:00
|
|
|
// will be logging to, as well as all the plugins that the user has
|
|
|
|
// specified
|
2015-04-01 16:34:32 +00:00
|
|
|
type Config struct {
|
2015-08-11 20:02:04 +00:00
|
|
|
Tags map[string]string
|
|
|
|
|
2015-05-20 05:19:32 +00:00
|
|
|
agent *ast.Table
|
2015-04-01 16:34:32 +00:00
|
|
|
plugins map[string]*ast.Table
|
2015-08-07 20:31:25 +00:00
|
|
|
outputs map[string]*ast.Table
|
2015-04-01 16:34:32 +00:00
|
|
|
}
|
|
|
|
|
2015-08-04 14:58:32 +00:00
|
|
|
// Plugins returns the configured plugins as a map of name -> plugin toml
|
2015-04-01 16:34:32 +00:00
|
|
|
func (c *Config) Plugins() map[string]*ast.Table {
|
|
|
|
return c.plugins
|
|
|
|
}
|
2015-08-07 20:31:25 +00:00
|
|
|
|
|
|
|
// Outputs returns the configured outputs as a map of name -> output toml
|
|
|
|
func (c *Config) Outputs() map[string]*ast.Table {
|
|
|
|
return c.outputs
|
|
|
|
}
|
2015-04-01 16:34:32 +00:00
|
|
|
|
2015-08-12 21:22:55 +00:00
|
|
|
// TagFilter is the name of a tag, and the values on which to filter
|
2015-08-10 19:41:20 +00:00
|
|
|
type TagFilter struct {
|
|
|
|
Name string
|
|
|
|
Filter []string
|
|
|
|
}
|
|
|
|
|
2015-08-04 14:58:32 +00:00
|
|
|
// ConfiguredPlugin containing a name, interval, and drop/pass prefix lists
|
2015-08-10 19:41:20 +00:00
|
|
|
// Also lists the tags to filter
|
2015-05-20 05:19:32 +00:00
|
|
|
type ConfiguredPlugin struct {
|
|
|
|
Name string
|
|
|
|
|
|
|
|
Drop []string
|
|
|
|
Pass []string
|
|
|
|
|
2015-08-10 19:41:20 +00:00
|
|
|
TagDrop []TagFilter
|
|
|
|
TagPass []TagFilter
|
|
|
|
|
2015-05-20 05:19:32 +00:00
|
|
|
Interval time.Duration
|
|
|
|
}
|
|
|
|
|
2015-08-04 14:58:32 +00:00
|
|
|
// ShouldPass returns true if the metric should pass, false if should drop
|
2015-08-10 19:41:20 +00:00
|
|
|
func (cp *ConfiguredPlugin) ShouldPass(measurement string, tags map[string]string) bool {
|
2015-05-20 05:19:32 +00:00
|
|
|
if cp.Pass != nil {
|
|
|
|
for _, pat := range cp.Pass {
|
2015-05-29 20:25:48 +00:00
|
|
|
if strings.HasPrefix(measurement, pat) {
|
2015-05-20 05:19:32 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if cp.Drop != nil {
|
|
|
|
for _, pat := range cp.Drop {
|
2015-05-29 20:25:48 +00:00
|
|
|
if strings.HasPrefix(measurement, pat) {
|
2015-05-20 05:19:32 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2015-08-10 19:41:20 +00:00
|
|
|
if cp.TagPass != nil {
|
|
|
|
for _, pat := range cp.TagPass {
|
|
|
|
if tagval, ok := tags[pat.Name]; ok {
|
|
|
|
for _, filter := range pat.Filter {
|
|
|
|
if filter == tagval {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if cp.TagDrop != nil {
|
|
|
|
for _, pat := range cp.TagDrop {
|
|
|
|
if tagval, ok := tags[pat.Name]; ok {
|
|
|
|
for _, filter := range pat.Filter {
|
|
|
|
if filter == tagval {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2015-05-20 05:19:32 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2015-08-07 20:31:25 +00:00
|
|
|
// ApplyOutput loads the toml config into the given interface
|
|
|
|
func (c *Config) ApplyOutput(name string, v interface{}) error {
|
|
|
|
if c.outputs[name] != nil {
|
|
|
|
return toml.UnmarshalTable(c.outputs[name], v)
|
2015-08-11 20:02:04 +00:00
|
|
|
}
|
|
|
|
return nil
|
2015-08-07 20:31:25 +00:00
|
|
|
}
|
|
|
|
|
2015-08-04 14:58:32 +00:00
|
|
|
// ApplyAgent loads the toml config into the given interface
|
2015-05-20 05:19:32 +00:00
|
|
|
func (c *Config) ApplyAgent(v interface{}) error {
|
|
|
|
if c.agent != nil {
|
|
|
|
return toml.UnmarshalTable(c.agent, v)
|
2015-04-01 16:34:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-08-04 14:58:32 +00:00
|
|
|
// ApplyPlugin takes defined plugin names and applies them to the given
|
|
|
|
// interface, returning a ConfiguredPlugin object in the end that can
|
|
|
|
// be inserted into a runningPlugin by the agent.
|
2015-05-20 05:19:32 +00:00
|
|
|
func (c *Config) ApplyPlugin(name string, v interface{}) (*ConfiguredPlugin, error) {
|
|
|
|
cp := &ConfiguredPlugin{Name: name}
|
|
|
|
|
|
|
|
if tbl, ok := c.plugins[name]; ok {
|
|
|
|
|
|
|
|
if node, ok := tbl.Fields["pass"]; ok {
|
|
|
|
if kv, ok := node.(*ast.KeyValue); ok {
|
|
|
|
if ary, ok := kv.Value.(*ast.Array); ok {
|
|
|
|
for _, elem := range ary.Value {
|
|
|
|
if str, ok := elem.(*ast.String); ok {
|
|
|
|
cp.Pass = append(cp.Pass, str.Value)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if node, ok := tbl.Fields["drop"]; ok {
|
|
|
|
if kv, ok := node.(*ast.KeyValue); ok {
|
|
|
|
if ary, ok := kv.Value.(*ast.Array); ok {
|
|
|
|
for _, elem := range ary.Value {
|
|
|
|
if str, ok := elem.(*ast.String); ok {
|
|
|
|
cp.Drop = append(cp.Drop, str.Value)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if node, ok := tbl.Fields["interval"]; ok {
|
|
|
|
if kv, ok := node.(*ast.KeyValue); ok {
|
|
|
|
if str, ok := kv.Value.(*ast.String); ok {
|
|
|
|
dur, err := time.ParseDuration(str.Value)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
cp.Interval = dur
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-10 19:41:20 +00:00
|
|
|
if node, ok := tbl.Fields["tagpass"]; ok {
|
|
|
|
if subtbl, ok := node.(*ast.Table); ok {
|
|
|
|
for name, val := range subtbl.Fields {
|
|
|
|
if kv, ok := val.(*ast.KeyValue); ok {
|
|
|
|
tagfilter := &TagFilter{Name: name}
|
|
|
|
if ary, ok := kv.Value.(*ast.Array); ok {
|
|
|
|
for _, elem := range ary.Value {
|
|
|
|
if str, ok := elem.(*ast.String); ok {
|
|
|
|
tagfilter.Filter = append(tagfilter.Filter, str.Value)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
cp.TagPass = append(cp.TagPass, *tagfilter)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if node, ok := tbl.Fields["tagdrop"]; ok {
|
|
|
|
if subtbl, ok := node.(*ast.Table); ok {
|
|
|
|
for name, val := range subtbl.Fields {
|
|
|
|
if kv, ok := val.(*ast.KeyValue); ok {
|
|
|
|
tagfilter := &TagFilter{Name: name}
|
|
|
|
if ary, ok := kv.Value.(*ast.Array); ok {
|
|
|
|
for _, elem := range ary.Value {
|
|
|
|
if str, ok := elem.(*ast.String); ok {
|
|
|
|
tagfilter.Filter = append(tagfilter.Filter, str.Value)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
cp.TagDrop = append(cp.TagDrop, *tagfilter)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-20 05:19:32 +00:00
|
|
|
delete(tbl.Fields, "drop")
|
|
|
|
delete(tbl.Fields, "pass")
|
|
|
|
delete(tbl.Fields, "interval")
|
2015-08-10 19:41:20 +00:00
|
|
|
delete(tbl.Fields, "tagdrop")
|
|
|
|
delete(tbl.Fields, "tagpass")
|
2015-05-20 05:19:32 +00:00
|
|
|
return cp, toml.UnmarshalTable(tbl, v)
|
|
|
|
}
|
|
|
|
|
|
|
|
return cp, nil
|
|
|
|
}
|
|
|
|
|
2015-08-04 14:58:32 +00:00
|
|
|
// PluginsDeclared returns the name of all plugins declared in the config.
|
2015-05-18 21:10:12 +00:00
|
|
|
func (c *Config) PluginsDeclared() []string {
|
2015-08-07 20:31:25 +00:00
|
|
|
return declared(c.plugins)
|
|
|
|
}
|
|
|
|
|
|
|
|
// OutputsDeclared returns the name of all outputs declared in the config.
|
|
|
|
func (c *Config) OutputsDeclared() []string {
|
|
|
|
return declared(c.outputs)
|
|
|
|
}
|
|
|
|
|
|
|
|
func declared(endpoints map[string]*ast.Table) []string {
|
2015-08-11 20:02:04 +00:00
|
|
|
var names []string
|
2015-05-18 21:10:12 +00:00
|
|
|
|
2015-08-11 20:02:04 +00:00
|
|
|
for name := range endpoints {
|
|
|
|
names = append(names, name)
|
2015-05-18 21:10:12 +00:00
|
|
|
}
|
|
|
|
|
2015-08-11 20:02:04 +00:00
|
|
|
sort.Strings(names)
|
2015-05-18 21:10:12 +00:00
|
|
|
|
2015-08-11 20:02:04 +00:00
|
|
|
return names
|
2015-05-18 21:10:12 +00:00
|
|
|
}
|
|
|
|
|
2015-08-04 14:58:32 +00:00
|
|
|
var errInvalidConfig = errors.New("invalid configuration")
|
2015-04-01 16:34:32 +00:00
|
|
|
|
2015-08-04 14:58:32 +00:00
|
|
|
// LoadConfig loads the given config file and returns a *Config pointer
|
2015-04-01 16:34:32 +00:00
|
|
|
func LoadConfig(path string) (*Config, error) {
|
|
|
|
data, err := ioutil.ReadFile(path)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
tbl, err := toml.Parse(data)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
c := &Config{
|
2015-08-12 20:17:50 +00:00
|
|
|
Tags: make(map[string]string),
|
2015-04-01 16:34:32 +00:00
|
|
|
plugins: make(map[string]*ast.Table),
|
2015-08-07 20:31:25 +00:00
|
|
|
outputs: make(map[string]*ast.Table),
|
2015-04-01 16:34:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for name, val := range tbl.Fields {
|
|
|
|
subtbl, ok := val.(*ast.Table)
|
|
|
|
if !ok {
|
2015-08-04 14:58:32 +00:00
|
|
|
return nil, errInvalidConfig
|
2015-04-01 16:34:32 +00:00
|
|
|
}
|
|
|
|
|
2015-05-20 05:19:32 +00:00
|
|
|
switch name {
|
|
|
|
case "agent":
|
|
|
|
c.agent = subtbl
|
2015-08-12 20:17:50 +00:00
|
|
|
case "tags":
|
|
|
|
if err := toml.UnmarshalTable(subtbl, c.Tags); err != nil {
|
|
|
|
return nil, errInvalidConfig
|
|
|
|
}
|
2015-08-07 20:31:25 +00:00
|
|
|
case "outputs":
|
|
|
|
for outputName, outputVal := range subtbl.Fields {
|
|
|
|
outputSubtbl, ok := outputVal.(*ast.Table)
|
|
|
|
if !ok {
|
|
|
|
return nil, errInvalidConfig
|
|
|
|
}
|
|
|
|
c.outputs[outputName] = outputSubtbl
|
|
|
|
}
|
2015-05-20 05:19:32 +00:00
|
|
|
default:
|
2015-04-01 16:34:32 +00:00
|
|
|
c.plugins[name] = subtbl
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return c, nil
|
|
|
|
}
|
|
|
|
|
2015-08-04 14:58:32 +00:00
|
|
|
// ListTags returns a string of tags specified in the config,
|
|
|
|
// line-protocol style
|
2015-04-01 16:34:32 +00:00
|
|
|
func (c *Config) ListTags() string {
|
|
|
|
var tags []string
|
|
|
|
|
|
|
|
for k, v := range c.Tags {
|
|
|
|
tags = append(tags, fmt.Sprintf("%s=%s", k, v))
|
|
|
|
}
|
|
|
|
|
|
|
|
sort.Strings(tags)
|
|
|
|
|
|
|
|
return strings.Join(tags, " ")
|
|
|
|
}
|
2015-05-18 22:10:11 +00:00
|
|
|
|
|
|
|
type hasConfig interface {
|
|
|
|
BasicConfig() string
|
|
|
|
}
|
|
|
|
|
|
|
|
type hasDescr interface {
|
|
|
|
Description() string
|
|
|
|
}
|
|
|
|
|
2015-05-22 23:45:14 +00:00
|
|
|
var header = `# Telegraf configuration
|
2015-05-18 22:10:11 +00:00
|
|
|
|
2015-05-22 23:45:14 +00:00
|
|
|
# Telegraf is entirely plugin driven. All metrics are gathered from the
|
2015-05-18 22:10:11 +00:00
|
|
|
# declared plugins.
|
|
|
|
|
|
|
|
# Even if a plugin has no configuration, it must be declared in here
|
|
|
|
# to be active. Declaring a plugin means just specifying the name
|
2015-05-22 23:26:32 +00:00
|
|
|
# as a section with no variables. To deactivate a plugin, comment
|
|
|
|
# out the name and any variables.
|
2015-05-18 22:10:11 +00:00
|
|
|
|
2015-05-22 23:45:14 +00:00
|
|
|
# Use 'telegraf -config telegraf.toml -test' to see what metrics a config
|
2015-05-18 22:51:11 +00:00
|
|
|
# file would generate.
|
|
|
|
|
2015-05-18 23:11:45 +00:00
|
|
|
# One rule that plugins conform to is wherever a connection string
|
2015-05-18 23:08:22 +00:00
|
|
|
# can be passed, the values '' and 'localhost' are treated specially.
|
|
|
|
# They indicate to the plugin to use their own builtin configuration to
|
|
|
|
# connect to the local system.
|
|
|
|
|
2015-05-21 16:45:09 +00:00
|
|
|
# NOTE: The configuration has a few required parameters. They are marked
|
|
|
|
# with 'required'. Be sure to edit those to make this configuration work.
|
|
|
|
|
2015-08-25 23:59:12 +00:00
|
|
|
# Tags can also be specified via a normal map, but only one form at a time:
|
|
|
|
[tags]
|
|
|
|
# dc = "us-east-1"
|
2015-05-22 23:26:32 +00:00
|
|
|
|
2015-08-25 23:59:12 +00:00
|
|
|
# Configuration for telegraf itself
|
|
|
|
[agent]
|
|
|
|
# interval = "10s"
|
|
|
|
# debug = false
|
|
|
|
# hostname = "prod3241"
|
2015-05-18 22:10:11 +00:00
|
|
|
|
2015-08-25 23:59:12 +00:00
|
|
|
###############################################################################
|
|
|
|
# OUTPUTS #
|
|
|
|
###############################################################################
|
2015-05-18 22:10:11 +00:00
|
|
|
|
2015-08-25 23:59:12 +00:00
|
|
|
[outputs]
|
|
|
|
`
|
2015-05-18 22:10:11 +00:00
|
|
|
|
2015-08-25 23:59:12 +00:00
|
|
|
var header2 = `
|
2015-05-20 05:19:32 +00:00
|
|
|
|
2015-08-25 23:59:12 +00:00
|
|
|
###############################################################################
|
|
|
|
# PLUGINS #
|
|
|
|
###############################################################################
|
2015-05-18 22:10:11 +00:00
|
|
|
|
|
|
|
`
|
|
|
|
|
2015-08-04 14:58:32 +00:00
|
|
|
// PrintSampleConfig prints the sample config!
|
2015-05-18 22:10:11 +00:00
|
|
|
func PrintSampleConfig() {
|
|
|
|
fmt.Printf(header)
|
|
|
|
|
2015-08-25 23:59:12 +00:00
|
|
|
// Print Outputs
|
|
|
|
var onames []string
|
2015-05-18 22:10:11 +00:00
|
|
|
|
2015-08-25 23:59:12 +00:00
|
|
|
for oname := range outputs.Outputs {
|
|
|
|
onames = append(onames, oname)
|
2015-05-18 22:10:11 +00:00
|
|
|
}
|
2015-08-25 23:59:12 +00:00
|
|
|
sort.Strings(onames)
|
2015-05-18 22:10:11 +00:00
|
|
|
|
2015-08-25 23:59:12 +00:00
|
|
|
for _, oname := range onames {
|
|
|
|
creator := outputs.Outputs[oname]
|
|
|
|
output := creator()
|
2015-05-18 22:10:11 +00:00
|
|
|
|
2015-08-25 23:59:12 +00:00
|
|
|
fmt.Printf("\n# %s\n[outputs.%s]\n", output.Description(), oname)
|
2015-05-18 22:10:11 +00:00
|
|
|
|
2015-08-25 23:59:12 +00:00
|
|
|
config := output.SampleConfig()
|
|
|
|
if config == "" {
|
|
|
|
fmt.Printf(" # no configuration\n\n")
|
|
|
|
} else {
|
|
|
|
fmt.Printf(config)
|
|
|
|
}
|
|
|
|
}
|
2015-05-18 22:10:11 +00:00
|
|
|
|
2015-08-25 23:59:12 +00:00
|
|
|
fmt.Printf(header2)
|
2015-05-18 22:10:11 +00:00
|
|
|
|
2015-08-25 23:59:12 +00:00
|
|
|
// Print Plugins
|
|
|
|
var pnames []string
|
2015-05-18 22:10:11 +00:00
|
|
|
|
2015-08-25 23:59:12 +00:00
|
|
|
for pname := range plugins.Plugins {
|
|
|
|
pnames = append(pnames, pname)
|
|
|
|
}
|
|
|
|
sort.Strings(pnames)
|
|
|
|
|
|
|
|
for _, pname := range pnames {
|
|
|
|
creator := plugins.Plugins[pname]
|
|
|
|
plugin := creator()
|
2015-05-18 22:10:11 +00:00
|
|
|
|
2015-08-25 23:59:12 +00:00
|
|
|
fmt.Printf("# %s\n[%s]\n", plugin.Description(), pname)
|
|
|
|
|
|
|
|
config := plugin.SampleConfig()
|
2015-05-18 22:10:11 +00:00
|
|
|
if config == "" {
|
2015-08-25 23:59:12 +00:00
|
|
|
fmt.Printf(" # no configuration\n\n")
|
2015-05-18 22:10:11 +00:00
|
|
|
} else {
|
|
|
|
lines := strings.Split(config, "\n")
|
|
|
|
for _, line := range lines {
|
|
|
|
fmt.Printf("%s\n", line)
|
|
|
|
}
|
|
|
|
fmt.Printf("\n")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-08-24 20:52:46 +00:00
|
|
|
|
|
|
|
// PrintPluginConfig prints the config usage of a single plugin.
|
|
|
|
func PrintPluginConfig(name string) error {
|
|
|
|
if creator, ok := plugins.Plugins[name]; ok {
|
|
|
|
plugin := creator()
|
|
|
|
fmt.Printf("# %s\n[%s]\n", plugin.Description(), name)
|
|
|
|
fmt.Printf(strings.TrimSpace(plugin.SampleConfig()))
|
|
|
|
} else {
|
|
|
|
return errors.New(fmt.Sprintf("Plugin %s not found", name))
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|