Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add -outputfilter flag, and refactor the filter flag to work for -sam… #217

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,13 @@ if you don't have it already. You also must build with golang version 1.4+
### How to use it:

* Run `telegraf -sample-config > telegraf.conf` to create an initial configuration
* Or run `telegraf -sample-config -filter cpu:mem -outputfilter influxdb > telegraf.conf`
to create a config file with only CPU and memory plugins defined, and InfluxDB output defined
* Edit the configuration to match your needs
* Run `telegraf -config telegraf.conf -test` to output one full measurement sample to STDOUT
* Run `telegraf -config telegraf.conf` to gather and send metrics to configured outputs.
* Run `telegraf -config telegraf.conf -filter system:swap`
to enable only the system & swap plugins defined in the config.
to run telegraf with only the system & swap plugins defined in the config.

## Telegraf Options

Expand Down
46 changes: 15 additions & 31 deletions agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"log"
"os"
"sort"
"strings"
"sync"
"time"

Expand Down Expand Up @@ -113,24 +112,28 @@ func (a *Agent) Close() error {
}

// LoadOutputs loads the agent's outputs
func (a *Agent) LoadOutputs() ([]string, error) {
func (a *Agent) LoadOutputs(filters []string) ([]string, error) {
var names []string

for _, name := range a.Config.OutputsDeclared() {
fmt.Println(outputs.Outputs)
creator, ok := outputs.Outputs[name]
if !ok {
return nil, fmt.Errorf("Undefined but requested output: %s", name)
}

output := creator()
if sliceContains(name, filters) || len(filters) == 0 {
fmt.Println("OUTPUT ENABLED: ", name)
output := creator()

err := a.Config.ApplyOutput(name, output)
if err != nil {
return nil, err
}
err := a.Config.ApplyOutput(name, output)
if err != nil {
return nil, err
}

a.outputs = append(a.outputs, &runningOutput{name, output})
names = append(names, name)
a.outputs = append(a.outputs, &runningOutput{name, output})
names = append(names, name)
}
}

sort.Strings(names)
Expand All @@ -139,37 +142,18 @@ func (a *Agent) LoadOutputs() ([]string, error) {
}

// LoadPlugins loads the agent's plugins
func (a *Agent) LoadPlugins(pluginsFilter string) ([]string, error) {
func (a *Agent) LoadPlugins(filters []string) ([]string, error) {
var names []string
var filters []string

pluginsFilter = strings.TrimSpace(pluginsFilter)
if pluginsFilter != "" {
filters = strings.Split(":"+pluginsFilter+":", ":")
}

for _, name := range a.Config.PluginsDeclared() {
creator, ok := plugins.Plugins[name]
if !ok {
return nil, fmt.Errorf("Undefined but requested plugin: %s", name)
}

isPluginEnabled := false
if len(filters) > 0 {
for _, runeValue := range filters {
if runeValue != "" && strings.ToLower(runeValue) == strings.ToLower(name) {
fmt.Printf("plugin [%s] is enabled (filter options)\n", name)
isPluginEnabled = true
break
}
}
} else {
// if no filter, we ALWAYS accept the plugin
isPluginEnabled = true
}

if isPluginEnabled {
if sliceContains(name, filters) || len(filters) == 0 {
plugin := creator()

config, err := a.Config.ApplyPlugin(name, plugin)
if err != nil {
return nil, err
Expand Down
43 changes: 29 additions & 14 deletions agent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (

// needing to load the plugins
_ "github.com/influxdb/telegraf/plugins/all"
// needing to load the outputs
// _ "github.com/influxdb/telegraf/outputs/all"
)

func TestAgent_LoadPlugin(t *testing.T) {
Expand All @@ -14,33 +16,46 @@ func TestAgent_LoadPlugin(t *testing.T) {
config, _ := LoadConfig("./testdata/telegraf-agent.toml")
a, _ := NewAgent(config)

pluginsEnabled, _ := a.LoadPlugins("mysql")
pluginsEnabled, _ := a.LoadPlugins([]string{"mysql"})
assert.Equal(t, 1, len(pluginsEnabled))

pluginsEnabled, _ = a.LoadPlugins("foo")
pluginsEnabled, _ = a.LoadPlugins([]string{"foo"})
assert.Equal(t, 0, len(pluginsEnabled))

pluginsEnabled, _ = a.LoadPlugins("mysql:foo")
pluginsEnabled, _ = a.LoadPlugins([]string{"mysql", "foo"})
assert.Equal(t, 1, len(pluginsEnabled))

pluginsEnabled, _ = a.LoadPlugins("mysql:redis")
pluginsEnabled, _ = a.LoadPlugins([]string{"mysql", "redis"})
assert.Equal(t, 2, len(pluginsEnabled))

pluginsEnabled, _ = a.LoadPlugins(":mysql:foo:redis:bar")
pluginsEnabled, _ = a.LoadPlugins([]string{"mysql", "foo", "redis", "bar"})
assert.Equal(t, 2, len(pluginsEnabled))
}

pluginsEnabled, _ = a.LoadPlugins("")
assert.Equal(t, 23, len(pluginsEnabled))
// TODO enable these unit tests, currently disabled because of a circular import
// func TestAgent_LoadOutput(t *testing.T) {
// // load a dedicated configuration file
// config, _ := LoadConfig("./testdata/telegraf-agent.toml")
// a, _ := NewAgent(config)

pluginsEnabled, _ = a.LoadPlugins(" ")
assert.Equal(t, 23, len(pluginsEnabled))
// outputsEnabled, _ := a.LoadOutputs([]string{"influxdb"})
// assert.Equal(t, 1, len(outputsEnabled))

pluginsEnabled, _ = a.LoadPlugins(" ")
assert.Equal(t, 23, len(pluginsEnabled))
// outputsEnabled, _ = a.LoadOutputs([]string{})
// assert.Equal(t, 2, len(outputsEnabled))

pluginsEnabled, _ = a.LoadPlugins("\n\t")
assert.Equal(t, 23, len(pluginsEnabled))
}
// outputsEnabled, _ = a.LoadOutputs([]string{"foo"})
// assert.Equal(t, 0, len(outputsEnabled))

// outputsEnabled, _ = a.LoadOutputs([]string{"influxdb", "foo"})
// assert.Equal(t, 1, len(outputsEnabled))

// outputsEnabled, _ = a.LoadOutputs([]string{"influxdb", "kafka"})
// assert.Equal(t, 2, len(outputsEnabled))

// outputsEnabled, _ = a.LoadOutputs([]string{"influxdb", "foo", "kafka", "bar"})
// assert.Equal(t, 2, len(outputsEnabled))
// }

/*
func TestAgent_DrivesMetrics(t *testing.T) {
Expand Down
22 changes: 18 additions & 4 deletions cmd/telegraf/telegraf.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@ var fVersion = flag.Bool("version", false, "display the version")
var fSampleConfig = flag.Bool("sample-config", false,
"print out full sample configuration")
var fPidfile = flag.String("pidfile", "", "file to write our pid to")
var fPLuginsFilter = flag.String("filter", "",
var fPLuginFilters = flag.String("filter", "",
"filter the plugins to enable, separator is :")
var fOutputFilters = flag.String("outputfilter", "",
"filter the outputs to enable, separator is :")
var fUsage = flag.String("usage", "",
"print usage for a plugin, ie, 'telegraf -usage mysql'")

Expand All @@ -33,14 +35,26 @@ var Version string
func main() {
flag.Parse()

var pluginFilters []string
if *fPLuginFilters != "" {
pluginsFilter := strings.TrimSpace(*fPLuginFilters)
pluginFilters = strings.Split(":"+pluginsFilter+":", ":")
}

var outputFilters []string
if *fOutputFilters != "" {
outputFilter := strings.TrimSpace(*fOutputFilters)
outputFilters = strings.Split(":"+outputFilter+":", ":")
}

if *fVersion {
v := fmt.Sprintf("Telegraf - Version %s", Version)
fmt.Println(v)
return
}

if *fSampleConfig {
telegraf.PrintSampleConfig()
telegraf.PrintSampleConfig(pluginFilters, outputFilters)
return
}

Expand Down Expand Up @@ -76,7 +90,7 @@ func main() {
ag.Debug = true
}

outputs, err := ag.LoadOutputs()
outputs, err := ag.LoadOutputs(outputFilters)
if err != nil {
log.Fatal(err)
}
Expand All @@ -85,7 +99,7 @@ func main() {
os.Exit(1)
}

plugins, err := ag.LoadPlugins(*fPLuginsFilter)
plugins, err := ag.LoadPlugins(pluginFilters)
if err != nil {
log.Fatal(err)
}
Expand Down
23 changes: 17 additions & 6 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -384,15 +384,16 @@ var header2 = `
###############################################################################
`

// PrintSampleConfig prints the sample config!
func PrintSampleConfig() {
// PrintSampleConfig prints the sample config
func PrintSampleConfig(pluginFilters []string, outputFilters []string) {
fmt.Printf(header)

// Print Outputs
var onames []string

for oname := range outputs.Outputs {
onames = append(onames, oname)
if len(outputFilters) == 0 || sliceContains(oname, outputFilters) {
onames = append(onames, oname)
}
}
sort.Strings(onames)

Expand All @@ -414,9 +415,10 @@ func PrintSampleConfig() {

// Print Plugins
var pnames []string

for pname := range plugins.Plugins {
pnames = append(pnames, pname)
if len(pluginFilters) == 0 || sliceContains(pname, pluginFilters) {
pnames = append(pnames, pname)
}
}
sort.Strings(pnames)

Expand All @@ -435,6 +437,15 @@ func PrintSampleConfig() {
}
}

func sliceContains(name string, list []string) bool {
for _, b := range list {
if b == name {
return true
}
}
return false
}

// PrintPluginConfig prints the config usage of a single plugin.
func PrintPluginConfig(name string) error {
if creator, ok := plugins.Plugins[name]; ok {
Expand Down
Loading