From 50dc2698bfee5ce06d75881ddf7cb6af11137549 Mon Sep 17 00:00:00 2001 From: Henry Hu Date: Wed, 3 Feb 2016 23:51:28 +0800 Subject: [PATCH] Change InnerConfig to config and modified exec.go to back-compatible with old command configuration. --- internal/config/testdata/telegraf-agent.toml | 2 +- .../graphite/{innerconfig.go => config.go} | 16 ++++++++-------- plugins/inputs/exec/README.md | 3 +++ plugins/inputs/exec/config.go | 2 +- plugins/inputs/exec/exec.go | 10 ++++++++-- plugins/inputs/socket/config.go | 2 +- 6 files changed, 22 insertions(+), 13 deletions(-) rename internal/encoding/graphite/{innerconfig.go => config.go} (88%) diff --git a/internal/config/testdata/telegraf-agent.toml b/internal/config/testdata/telegraf-agent.toml index 8bde486dd..5ede47016 100644 --- a/internal/config/testdata/telegraf-agent.toml +++ b/internal/config/testdata/telegraf-agent.toml @@ -107,7 +107,7 @@ # Read flattened metrics from one or more commands that output JSON to stdout [[inputs.exec]] # the command to run - commands = ["/usr/bin/mycollector --foo=bar"] + command = "/usr/bin/mycollector --foo=bar" name_suffix = "_mycollector" # Read metrics of haproxy, via socket or csv stats page diff --git a/internal/encoding/graphite/innerconfig.go b/internal/encoding/graphite/config.go similarity index 88% rename from internal/encoding/graphite/innerconfig.go rename to internal/encoding/graphite/config.go index 2fdab3687..283448e86 100644 --- a/internal/encoding/graphite/innerconfig.go +++ b/internal/encoding/graphite/config.go @@ -14,14 +14,14 @@ const ( ) // Config represents the configuration for Graphite endpoints. -type InnerConfig struct { +type Config struct { Separator string Tags []string Templates []string } // DefaultTags returns the config's tags. -func (c *InnerConfig) DefaultTags() models.Tags { +func (c *Config) DefaultTags() models.Tags { tags := models.Tags{} for _, t := range c.Tags { parts := strings.Split(t, "=") @@ -31,7 +31,7 @@ func (c *InnerConfig) DefaultTags() models.Tags { } // Validate validates the config's templates and tags. -func (c *InnerConfig) Validate() error { +func (c *Config) Validate() error { if err := c.validateTemplates(); err != nil { return err } @@ -43,7 +43,7 @@ func (c *InnerConfig) Validate() error { return nil } -func (c *InnerConfig) validateTemplates() error { +func (c *Config) validateTemplates() error { // map to keep track of filters we see filters := map[string]struct{}{} @@ -110,7 +110,7 @@ func (c *InnerConfig) validateTemplates() error { return nil } -func (c *InnerConfig) validateTags() error { +func (c *Config) validateTags() error { for _, t := range c.Tags { if err := c.validateTag(t); err != nil { return err @@ -119,7 +119,7 @@ func (c *InnerConfig) validateTags() error { return nil } -func (c *InnerConfig) validateTemplate(template string) error { +func (c *Config) validateTemplate(template string) error { hasMeasurement := false for _, p := range strings.Split(template, ".") { if p == "measurement" || p == "measurement*" { @@ -134,7 +134,7 @@ func (c *InnerConfig) validateTemplate(template string) error { return nil } -func (c *InnerConfig) validateFilter(filter string) error { +func (c *Config) validateFilter(filter string) error { for _, p := range strings.Split(filter, ".") { if p == "" { return fmt.Errorf("filter contains blank section: %s", filter) @@ -147,7 +147,7 @@ func (c *InnerConfig) validateFilter(filter string) error { return nil } -func (c *InnerConfig) validateTag(keyValue string) error { +func (c *Config) validateTag(keyValue string) error { parts := strings.Split(keyValue, "=") if len(parts) != 2 { return fmt.Errorf("invalid template tags: '%s'", keyValue) diff --git a/plugins/inputs/exec/README.md b/plugins/inputs/exec/README.md index 5dabada6b..8f67a480a 100644 --- a/plugins/inputs/exec/README.md +++ b/plugins/inputs/exec/README.md @@ -28,6 +28,9 @@ and strings will be ignored. # Read flattened metrics from one or more commands that output JSON to stdout [[inputs.exec]] # Shell/commands array + # compatible with old version + # we can still use the old command configuration + # command = "/usr/bin/mycollector --foo=bar" commands = ["/tmp/test.sh","/tmp/test2.sh"] # Data format to consume. This can be "json", "influx" or "graphite" (line-protocol) diff --git a/plugins/inputs/exec/config.go b/plugins/inputs/exec/config.go index 13fe05c71..d2239b0c8 100644 --- a/plugins/inputs/exec/config.go +++ b/plugins/inputs/exec/config.go @@ -13,7 +13,7 @@ const ( // Config represents the configuration for Graphite endpoints. type Config struct { Commands []string - graphite.InnerConfig + graphite.Config } // New Config instance. diff --git a/plugins/inputs/exec/exec.go b/plugins/inputs/exec/exec.go index 1697cb936..6f17da932 100644 --- a/plugins/inputs/exec/exec.go +++ b/plugins/inputs/exec/exec.go @@ -16,6 +16,9 @@ import ( const sampleConfig = ` # Shell/commands array + # compatible with old version + # we can still use the old command configuration + # command = "/usr/bin/mycollector --foo=bar" commands = ["/tmp/test.sh","/tmp/test2.sh"] # Data format to consume. This can be "json", "influx" or "graphite" (line-protocol) @@ -52,6 +55,7 @@ const sampleConfig = ` type Exec struct { Commands []string + Command string DataFormat string Separator string @@ -84,8 +88,6 @@ func (c CommandRunner) Run(e *Exec, command string) ([]byte, error) { } cmd := exec.Command(split_cmd[0], split_cmd[1:]...) - //name := strings.Replace(filepath.Base(cmd.Path), "/", "_", -1) - //name = strings.Replace(name, ".", "_", -1) var out bytes.Buffer cmd.Stdout = &out @@ -119,6 +121,10 @@ func (e *Exec) initConfig() error { e.Lock() defer e.Unlock() + if e.Command != "" && len(e.Commands) < 1 { + e.Commands = []string{e.Command} + } + c := NewConfig(e.Commands, e.Tags, e.Templates, e.Separator) c.WithDefaults() if err := c.Validate(); err != nil { diff --git a/plugins/inputs/socket/config.go b/plugins/inputs/socket/config.go index 422248aba..a4419cf45 100644 --- a/plugins/inputs/socket/config.go +++ b/plugins/inputs/socket/config.go @@ -33,7 +33,7 @@ type Config struct { Protocol string UdpReadBuffer int - graphite.InnerConfig + graphite.Config } // WithDefaults takes the given config and returns a new config with any required