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

New option to filter the plugins to run at startup #106

Merged
merged 4 commits into from
Aug 11, 2015
Merged
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
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,21 @@ brew install telegraf
* Edit the configuration to match your needs
* Run `telegraf -config telegraf.toml -test` to output one full measurement sample to STDOUT
* Run `telegraf -config telegraf.toml` to gather and send metrics to InfluxDB
* Run `telegraf -config telegraf.toml -filter system:swap` to enable only two plugins described into config file

### Telegraf Usage

```telegraf --help```

* -config="": configuration file to load
* -debug=false: show metrics as they're generated to stdout
* -filter="": filter the plugins to enable, separator is :
* -httptest.serve="": if non-empty, httptest.NewServer serves on this address and blocks
* -pidfile="": file to write our pid to
* -sample-config=false: print out full sample configuration
* -test=false: gather metrics, print them out, and exit
* -version=false: display the version

## Telegraf Options

Telegraf has a few options you can configure under the `agent` section of the
Expand Down
37 changes: 29 additions & 8 deletions agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,24 +99,45 @@ func (a *Agent) Connect() error {
}

// LoadPlugins loads the agent's plugins
func (a *Agent) LoadPlugins() ([]string, error) {
func (a *Agent) LoadPlugins(pluginsFilter 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)
}

plugin := creator()

config, err := a.Config.ApplyPlugin(name, plugin)
if err != nil {
return nil, err
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
}

a.plugins = append(a.plugins, &runningPlugin{name, plugin, config})
names = append(names, name)
if isPluginEnabled {
plugin := creator()
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)
}
}

sort.Strings(names)
Expand Down
42 changes: 42 additions & 0 deletions agent_test.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,47 @@
package telegraf

import (
"github.com/stretchr/testify/assert"
"testing"

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

func TestAgent_LoadPlugin(t *testing.T) {

// load a dedicated configuration file
config, _ := LoadConfig("./testdata/telegraf-agent.toml")
a, _ := NewAgent(config)

pluginsEnabled, _ := a.LoadPlugins("mysql")
assert.Equal(t, 1, len(pluginsEnabled))

pluginsEnabled, _ = a.LoadPlugins("foo")
assert.Equal(t, 0, len(pluginsEnabled))

pluginsEnabled, _ = a.LoadPlugins("mysql:foo")
assert.Equal(t, 1, len(pluginsEnabled))

pluginsEnabled, _ = a.LoadPlugins("mysql:redis")
assert.Equal(t, 2, len(pluginsEnabled))

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

pluginsEnabled, _ = a.LoadPlugins("")
assert.Equal(t, 24, len(pluginsEnabled))

pluginsEnabled, _ = a.LoadPlugins(" ")
assert.Equal(t, 24, len(pluginsEnabled))

pluginsEnabled, _ = a.LoadPlugins(" ")
assert.Equal(t, 24, len(pluginsEnabled))

pluginsEnabled, _ = a.LoadPlugins("\n\t")
assert.Equal(t, 24, len(pluginsEnabled))
}

/*
func TestAgent_DrivesMetrics(t *testing.T) {
var (
Expand Down
3 changes: 2 additions & 1 deletion cmd/telegraf/telegraf.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ var fConfig = flag.String("config", "", "configuration file to load")
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", "", "filter the plugins to enable, separator is :")

// Telegraf version
var Version = "unreleased"
Expand Down Expand Up @@ -61,7 +62,7 @@ func main() {
ag.Debug = true
}

plugins, err := ag.LoadPlugins()
plugins, err := ag.LoadPlugins(*fPLuginsFilter)
if err != nil {
log.Fatal(err)
}
Expand Down
Loading