Skip to content

Commit

Permalink
add test and plugin-filters options to view the collected plugin …
Browse files Browse the repository at this point in the history
…metrics
  • Loading branch information
startover committed Mar 14, 2017
1 parent 5155a2a commit ce1f98b
Show file tree
Hide file tree
Showing 4 changed files with 117 additions and 10 deletions.
53 changes: 53 additions & 0 deletions agent/agent.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package agent

import (
"fmt"
"runtime"
"sort"
"sync"
"time"

Expand Down Expand Up @@ -109,6 +111,57 @@ func collectWithTimeout(
// Test verifies that we can 'collect' from all Plugins with their configured
// Config struct
func (a *Agent) Test() error {
shutdown := make(chan struct{})
metricC := make(chan metric.Metric)
var metrics []string

// dummy receiver for the metric channel
var wg sync.WaitGroup
wg.Add(1)
go func() {
defer wg.Done()

for {
select {
case m := <-metricC:
metrics = append(metrics, m.String())
case <-shutdown:
return
}
}
}()

agg := NewAggregator(metricC, a.conf)
for _, rp := range a.conf.Plugins {
fmt.Println("------------------------------------")
for i, plug := range rp.Plugins {
fmt.Printf("* Plugin: %s\n", rp.Name)
if err := plug.Check(agg); err != nil {
return err
}

time.Sleep(time.Second)
if err := plug.Check(agg); err != nil {
return err
}
agg.Flush()

// Waiting for the metrics filled up
time.Sleep(time.Millisecond)

fmt.Printf("* Instance #%d, Collected %d metrics\n", i, len(metrics))
sort.Strings(metrics)
for _, m := range metrics {
fmt.Println("> " + m)
}
metrics = []string{}
}
}

close(shutdown)
wg.Wait()

fmt.Println("Done!")
return nil
}

Expand Down
7 changes: 6 additions & 1 deletion common/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@ import (
const VERSION = "0.4.0"

// NewConfig creates a new instance of Config.
func NewConfig(confPath string) (*Config, error) {
func NewConfig(confPath string, pluginFilters []string) (*Config, error) {
c := &Config{}
c.pluginFilters = pluginFilters

err := c.LoadConfig(confPath)
if err != nil {
Expand All @@ -38,6 +39,7 @@ type Config struct {
GlobalConfig GlobalConfig `toml:"global"`
LoggingConfig LoggingConfig `toml:"logging"`
Plugins []*plugin.RunningPlugin
pluginFilters []string
}

// GlobalConfig XXX
Expand Down Expand Up @@ -137,6 +139,9 @@ func (c *Config) LoadConfig(confPath string) error {
}

func (c *Config) addPlugin(name string, pluginConfig *plugin.Config) error {
if len(c.pluginFilters) > 0 && !util.StringInSlice(name, c.pluginFilters) {
return nil
}
checker, ok := collector.Plugins[name]
if !ok {
return fmt.Errorf("Undefined plugin: %s", name)
Expand Down
5 changes: 5 additions & 0 deletions common/metric/metric.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,11 @@ func (m *Metric) IsExpired(timestamp, expirySeconds int64) bool {
return false
}

// String XXX
func (m *Metric) String() string {
return fmt.Sprintf("%s %f %v", m.Name, m.Value, m.Tags)
}

// Format XXX
func (m Metric) Format() interface{} {
if m.Formatter != nil {
Expand Down
62 changes: 53 additions & 9 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,36 +19,75 @@ import (
)

var fConfig = flag.String("config", "", "configuration file to load")
var fTest = flag.Bool("test", false, "collect metrics, print them out, and exit")
var fPluginFilters = flag.String("plugin-filter", "",
"filter the plugins to enable, separator is :")

func startAgent(shutdown chan struct{}, conf *config.Config) {
const usage = `Cloudinsight Agent, a system tool that monitors system processes and services.
Usage:
cloudinsight-agent [commands|flags]
The commands & flags are:
--config <file> configuration file to load
--test collect metrics once, print them to stdout, and exit
--plugin-filter filter the plugins to enable, separator is :
Examples:
# run cloudinsight-agent with all plugins defined in config file
cloudinsight-agent --config cloudinsight-agent.conf
# run a single collection, outputing metrics to stdout
cloudinsight-agent --config cloudinsight-agent.conf -test
# run cloudinsight-agent, enabling the system & disk plugins
cloudinsight-agent --config cloudinsight-agent.conf --plugin-filter system:disk
`

func startAgent(shutdown chan struct{}, conf *config.Config, test bool) {
ag := agent.NewAgent(conf)
err := ag.Run(shutdown)
if err != nil {
if test {
log.SetLevel("error")
log.SetOutput(os.Stderr)
if err := ag.Test(); err != nil {
log.Fatal(err)
}
os.Exit(0)
}

if err := ag.Run(shutdown); err != nil {
log.Fatal(err)
}
}

func startForwarder(shutdown chan struct{}, conf *config.Config) {
f := forwarder.NewForwarder(conf)
err := f.Run(shutdown)
if err != nil {
if err := f.Run(shutdown); err != nil {
log.Fatal(err)
}
}

func startStatsd(shutdown chan struct{}, conf *config.Config) {
s := statsd.NewStatsd(conf)
err := s.Run(shutdown)
if err != nil {
if err := s.Run(shutdown); err != nil {
log.Fatal(err)
}
}

func usageExit(rc int) {
fmt.Println(usage)
os.Exit(rc)
}

func main() {
reload := make(chan bool, 1)
reload <- true
for <-reload {
reload <- false
flag.Usage = func() { usageExit(0) }
flag.Parse()

shutdown := make(chan struct{})
Expand All @@ -69,7 +108,12 @@ func main() {
}
}()

conf, err := config.NewConfig(*fConfig)
pluginFilters := []string{}
if *fPluginFilters != "" {
pluginFilters = strings.Split(":"+strings.TrimSpace(*fPluginFilters)+":", ":")
}

conf, err := config.NewConfig(*fConfig, pluginFilters)
if err != nil {
log.Fatalf("failed to load config: %s", err)
}
Expand All @@ -91,7 +135,7 @@ func main() {
go func() {
defer wg.Done()

startAgent(shutdown, conf)
startAgent(shutdown, conf, *fTest)
}()

go func() {
Expand Down

0 comments on commit ce1f98b

Please sign in to comment.