From 609cba2cd5732fa193655d5ebd61d8ac51f7b633 Mon Sep 17 00:00:00 2001 From: Evan Phoenix Date: Mon, 6 Apr 2015 17:24:24 -0700 Subject: [PATCH] Provide a test mode to check plugins easily --- accumulator.go | 22 +++++++++++++++++++++- agent.go | 29 ++++++++++++++++++++++++++--- cmd/influxdb-agent/agent.go | 16 +++++++++++++++- 3 files changed, 62 insertions(+), 5 deletions(-) diff --git a/accumulator.go b/accumulator.go index 3c7f7efcf..8c5f66767 100644 --- a/accumulator.go +++ b/accumulator.go @@ -1,12 +1,32 @@ package tivan -import "github.com/influxdb/influxdb/client" +import ( + "fmt" + "sort" + "strings" + + "github.com/influxdb/influxdb/client" +) type BatchPoints struct { client.BatchPoints + + Debug bool } func (bp *BatchPoints) Add(name string, val interface{}, tags map[string]string) { + if bp.Debug { + var tg []string + + for k, v := range tags { + tg = append(tg, fmt.Sprintf("%s=\"%s\"", k, v)) + } + + sort.Strings(tg) + + fmt.Printf("> [%s] %s=%v\n", strings.Join(tg, " "), name, val) + } + bp.Points = append(bp.Points, client.Point{ Name: name, Tags: tags, diff --git a/agent.go b/agent.go index 19b1f5508..533cf7593 100644 --- a/agent.go +++ b/agent.go @@ -37,9 +37,15 @@ func NewAgent(config *Config) (*Agent, error) { return nil, err } + return agent, nil +} + +func (agent *Agent) Connect() error { + config := agent.Config + u, err := url.Parse(config.URL) if err != nil { - return nil, err + return err } c, err := client.NewClient(client.Config{ @@ -50,12 +56,12 @@ func NewAgent(config *Config) (*Agent, error) { }) if err != nil { - return nil, err + return err } agent.conn = c - return agent, nil + return nil } func (a *Agent) LoadPlugins() ([]string, error) { @@ -74,6 +80,8 @@ func (a *Agent) LoadPlugins() ([]string, error) { func (a *Agent) crank() error { var acc BatchPoints + acc.Debug = a.Debug + for _, plugin := range a.plugins { err := plugin.Gather(&acc) if err != nil { @@ -89,6 +97,21 @@ func (a *Agent) crank() error { return err } +func (a *Agent) Test() error { + var acc BatchPoints + + acc.Debug = true + + for _, plugin := range a.plugins { + err := plugin.Gather(&acc) + if err != nil { + return err + } + } + + return nil +} + func (a *Agent) Run(shutdown chan struct{}) { ticker := time.NewTicker(a.Interval.Duration) diff --git a/cmd/influxdb-agent/agent.go b/cmd/influxdb-agent/agent.go index 93350a0ef..d1ef8d94e 100644 --- a/cmd/influxdb-agent/agent.go +++ b/cmd/influxdb-agent/agent.go @@ -12,7 +12,7 @@ import ( ) var fDebug = flag.Bool("debug", false, "show metrics as they're generated to stdout") - +var fTest = flag.Bool("test", false, "gather metrics, print them out, and exit") var fConfig = flag.String("config", "", "configuration file to load") func main() { @@ -46,6 +46,20 @@ func main() { log.Fatal(err) } + if *fTest { + err = ag.Test() + if err != nil { + log.Fatal(err) + } + + return + } + + err = ag.Connect() + if err != nil { + log.Fatal(err) + } + shutdown := make(chan struct{}) signals := make(chan os.Signal)