Require plugin declaration in config to use any plugin

This also makes it so `-test` without a config file tests all available
plugins with the default configuration. This mode is very useful for
getting some initial data out of tivan.
This commit is contained in:
Evan Phoenix 2015-05-18 14:10:12 -07:00
parent f1e1204374
commit b2c5d95737
3 changed files with 54 additions and 10 deletions

View File

@ -1,6 +1,7 @@
package tivan package tivan
import ( import (
"fmt"
"log" "log"
"net/url" "net/url"
"os" "os"
@ -82,16 +83,13 @@ func (agent *Agent) Connect() error {
func (a *Agent) LoadPlugins() ([]string, error) { func (a *Agent) LoadPlugins() ([]string, error) {
var names []string var names []string
var pluginNames []string for _, name := range a.Config.PluginsDeclared() {
creator, ok := plugins.Plugins[name]
if !ok {
return nil, fmt.Errorf("Undefined but requested plugin: %s", name)
}
for name, _ := range plugins.Plugins { plugin := creator()
pluginNames = append(pluginNames, name)
}
sort.Strings(pluginNames)
for _, name := range pluginNames {
plugin := plugins.Plugins[name]()
err := a.Config.Apply(name, plugin) err := a.Config.Apply(name, plugin)
if err != nil { if err != nil {
@ -128,6 +126,35 @@ func (a *Agent) crank() error {
return err return err
} }
func (a *Agent) TestAllPlugins() error {
var names []string
for name, _ := range plugins.Plugins {
names = append(names, name)
}
sort.Strings(names)
var acc BatchPoints
acc.Debug = true
fmt.Printf("* Testing all plugins with default configuration\n")
for _, name := range names {
plugin := plugins.Plugins[name]()
fmt.Printf("* Plugin: %s\n", name)
acc.Prefix = name + "_"
err := plugin.Gather(&acc)
if err != nil {
return err
}
}
return nil
}
func (a *Agent) Test() error { func (a *Agent) Test() error {
var acc BatchPoints var acc BatchPoints

View File

@ -56,7 +56,12 @@ func main() {
} }
if *fTest { if *fTest {
err = ag.Test() if *fConfig != "" {
err = ag.Test()
} else {
err = ag.TestAllPlugins()
}
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }

View File

@ -50,6 +50,18 @@ func (c *Config) Apply(name string, v interface{}) error {
return nil return nil
} }
func (c *Config) PluginsDeclared() []string {
var plugins []string
for name, _ := range c.plugins {
plugins = append(plugins, name)
}
sort.Strings(plugins)
return plugins
}
func DefaultConfig() *Config { func DefaultConfig() *Config {
return &Config{} return &Config{}
} }