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 739f8d9
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 41 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ 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
* Run `telegraf -sample-config -filter cpu:mem -outputfilter influxdb`
to create a config file with only CPU and memory plugins, 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.
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
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 739f8d9

Please sign in to comment.