Add filtering options to select plugin at startup
This commit is contained in:
parent
ed16a84e0d
commit
8b0035b0f0
42
agent.go
42
agent.go
|
@ -8,6 +8,7 @@ import (
|
||||||
"sort"
|
"sort"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"github.com/influxdb/influxdb/client"
|
"github.com/influxdb/influxdb/client"
|
||||||
"github.com/influxdb/telegraf/plugins"
|
"github.com/influxdb/telegraf/plugins"
|
||||||
|
@ -81,24 +82,49 @@ func (agent *Agent) Connect() error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *Agent) LoadPlugins() ([]string, error) {
|
func (a *Agent) LoadPlugins(pluginsFilter string) ([]string, error) {
|
||||||
var names []string
|
var names []string
|
||||||
|
var filters []string
|
||||||
|
|
||||||
|
pluginsFilter = strings.TrimSpace(pluginsFilter)
|
||||||
|
if pluginsFilter != "" {
|
||||||
|
filters = strings.Split(":"+pluginsFilter+":", ":")
|
||||||
|
}
|
||||||
|
|
||||||
for _, name := range a.Config.PluginsDeclared() {
|
for _, name := range a.Config.PluginsDeclared() {
|
||||||
|
|
||||||
creator, ok := plugins.Plugins[name]
|
creator, ok := plugins.Plugins[name]
|
||||||
|
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil, fmt.Errorf("Undefined but requested plugin: %s", name)
|
return nil, fmt.Errorf("Undefined but requested plugin: %s", name)
|
||||||
}
|
}
|
||||||
|
|
||||||
plugin := creator()
|
// to know if plugin is enabled or not through filter flag
|
||||||
|
isPluginEnabled := false
|
||||||
config, err := a.Config.ApplyPlugin(name, plugin)
|
if len(filters)>0 {
|
||||||
if err != nil {
|
for _, runeValue := range filters {
|
||||||
return nil, err
|
if runeValue != "" && strings.ToLower(runeValue) == strings.ToLower(name) {
|
||||||
|
fmt.Printf("plugin [%s] is enabled because present is filter options\n", name)
|
||||||
|
isPluginEnabled = true
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// if no filter, we ALWAYS accept the plugin
|
||||||
|
isPluginEnabled = true
|
||||||
|
}
|
||||||
|
|
||||||
|
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)
|
||||||
}
|
}
|
||||||
|
|
||||||
a.plugins = append(a.plugins, &runningPlugin{name, plugin, config})
|
|
||||||
names = append(names, name)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
sort.Strings(names)
|
sort.Strings(names)
|
||||||
|
|
|
@ -1,5 +1,47 @@
|
||||||
package telegraf
|
package telegraf
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
|
||||||
|
// 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, 14, len(pluginsEnabled))
|
||||||
|
|
||||||
|
pluginsEnabled, _ = a.LoadPlugins(" ")
|
||||||
|
assert.Equal(t, 14, len(pluginsEnabled))
|
||||||
|
|
||||||
|
pluginsEnabled, _ = a.LoadPlugins(" ")
|
||||||
|
assert.Equal(t, 14, len(pluginsEnabled))
|
||||||
|
|
||||||
|
pluginsEnabled, _ = a.LoadPlugins("\n\t")
|
||||||
|
assert.Equal(t, 14, len(pluginsEnabled))
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
func TestAgent_DrivesMetrics(t *testing.T) {
|
func TestAgent_DrivesMetrics(t *testing.T) {
|
||||||
var (
|
var (
|
||||||
|
|
|
@ -18,6 +18,7 @@ var fConfig = flag.String("config", "", "configuration file to load")
|
||||||
var fVersion = flag.Bool("version", false, "display the version")
|
var fVersion = flag.Bool("version", false, "display the version")
|
||||||
var fSampleConfig = flag.Bool("sample-config", false, "print out full sample configuration")
|
var fSampleConfig = flag.Bool("sample-config", false, "print out full sample configuration")
|
||||||
var fPidfile = flag.String("pidfile", "", "file to write our pid to")
|
var fPidfile = flag.String("pidfile", "", "file to write our pid to")
|
||||||
|
var fPLuginsFilter = flag.String("filter", "", "filter the plugin to enable")
|
||||||
|
|
||||||
var Version = "unreleased"
|
var Version = "unreleased"
|
||||||
var Commit = ""
|
var Commit = ""
|
||||||
|
@ -58,7 +59,7 @@ func main() {
|
||||||
ag.Debug = true
|
ag.Debug = true
|
||||||
}
|
}
|
||||||
|
|
||||||
plugins, err := ag.LoadPlugins()
|
plugins, err := ag.LoadPlugins(*fPLuginsFilter)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue