Skip to content

Commit

Permalink
Add -outputfilter flag, and refactor the filter flag to work for -sam…
Browse files Browse the repository at this point in the history
…ple-config

Closes #211
Issue #199
  • Loading branch information
sparrc committed Sep 22, 2015
1 parent 1cd2db9 commit ab9f9b0
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 51 deletions.
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
44 changes: 13 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,7 +112,7 @@ 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() {
Expand All @@ -122,15 +121,17 @@ func (a *Agent) LoadOutputs() ([]string, error) {
return nil, fmt.Errorf("Undefined but requested output: %s", name)
}

output := creator()
if sliceContains(name, filters) || len(filters) == 0 {
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 +140,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
18 changes: 9 additions & 9 deletions agent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,31 +14,31 @@ 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("")
pluginsEnabled, _ = a.LoadPlugins([]string{""})
assert.Equal(t, 23, len(pluginsEnabled))

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

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

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

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

0 comments on commit ab9f9b0

Please sign in to comment.