Change InnerConfig to config and modified exec.go to back-compatible with old command configuration.

This commit is contained in:
Henry Hu 2016-02-03 23:54:31 +08:00
parent 9fdf75e1f7
commit 51622d0bbf
4 changed files with 20 additions and 11 deletions

View File

@ -14,14 +14,14 @@ const (
) )
// Config represents the configuration for Graphite endpoints. // Config represents the configuration for Graphite endpoints.
type InnerConfig struct { type Config struct {
Separator string Separator string
Tags []string Tags []string
Templates []string Templates []string
} }
// DefaultTags returns the config's tags. // DefaultTags returns the config's tags.
func (c *InnerConfig) DefaultTags() models.Tags { func (c *Config) DefaultTags() models.Tags {
tags := models.Tags{} tags := models.Tags{}
for _, t := range c.Tags { for _, t := range c.Tags {
parts := strings.Split(t, "=") parts := strings.Split(t, "=")
@ -31,7 +31,7 @@ func (c *InnerConfig) DefaultTags() models.Tags {
} }
// Validate validates the config's templates and 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 { if err := c.validateTemplates(); err != nil {
return err return err
} }
@ -43,7 +43,7 @@ func (c *InnerConfig) Validate() error {
return nil return nil
} }
func (c *InnerConfig) validateTemplates() error { func (c *Config) validateTemplates() error {
// map to keep track of filters we see // map to keep track of filters we see
filters := map[string]struct{}{} filters := map[string]struct{}{}
@ -110,7 +110,7 @@ func (c *InnerConfig) validateTemplates() error {
return nil return nil
} }
func (c *InnerConfig) validateTags() error { func (c *Config) validateTags() error {
for _, t := range c.Tags { for _, t := range c.Tags {
if err := c.validateTag(t); err != nil { if err := c.validateTag(t); err != nil {
return err return err
@ -119,7 +119,7 @@ func (c *InnerConfig) validateTags() error {
return nil return nil
} }
func (c *InnerConfig) validateTemplate(template string) error { func (c *Config) validateTemplate(template string) error {
hasMeasurement := false hasMeasurement := false
for _, p := range strings.Split(template, ".") { for _, p := range strings.Split(template, ".") {
if p == "measurement" || p == "measurement*" { if p == "measurement" || p == "measurement*" {
@ -134,7 +134,7 @@ func (c *InnerConfig) validateTemplate(template string) error {
return nil return nil
} }
func (c *InnerConfig) validateFilter(filter string) error { func (c *Config) validateFilter(filter string) error {
for _, p := range strings.Split(filter, ".") { for _, p := range strings.Split(filter, ".") {
if p == "" { if p == "" {
return fmt.Errorf("filter contains blank section: %s", filter) return fmt.Errorf("filter contains blank section: %s", filter)
@ -147,7 +147,7 @@ func (c *InnerConfig) validateFilter(filter string) error {
return nil return nil
} }
func (c *InnerConfig) validateTag(keyValue string) error { func (c *Config) validateTag(keyValue string) error {
parts := strings.Split(keyValue, "=") parts := strings.Split(keyValue, "=")
if len(parts) != 2 { if len(parts) != 2 {
return fmt.Errorf("invalid template tags: '%s'", keyValue) return fmt.Errorf("invalid template tags: '%s'", keyValue)

View File

@ -28,6 +28,9 @@ and strings will be ignored.
# Read flattened metrics from one or more commands that output JSON to stdout # Read flattened metrics from one or more commands that output JSON to stdout
[[inputs.exec]] [[inputs.exec]]
# Shell/commands array # 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"] commands = ["/tmp/test.sh","/tmp/test2.sh"]
# Data format to consume. This can be "json", "influx" or "graphite" (line-protocol) # Data format to consume. This can be "json", "influx" or "graphite" (line-protocol)

View File

@ -13,7 +13,7 @@ const (
// Config represents the configuration for Graphite endpoints. // Config represents the configuration for Graphite endpoints.
type Config struct { type Config struct {
Commands []string Commands []string
graphite.InnerConfig graphite.Config
} }
// New Config instance. // New Config instance.

View File

@ -16,6 +16,9 @@ import (
const sampleConfig = ` const sampleConfig = `
# Shell/commands array # 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"] commands = ["/tmp/test.sh","/tmp/test2.sh"]
# Data format to consume. This can be "json", "influx" or "graphite" (line-protocol) # Data format to consume. This can be "json", "influx" or "graphite" (line-protocol)
@ -52,6 +55,7 @@ const sampleConfig = `
type Exec struct { type Exec struct {
Commands []string Commands []string
Command string
DataFormat string DataFormat string
Separator 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:]...) 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 var out bytes.Buffer
cmd.Stdout = &out cmd.Stdout = &out
@ -119,6 +121,10 @@ func (e *Exec) initConfig() error {
e.Lock() e.Lock()
defer e.Unlock() 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 := NewConfig(e.Commands, e.Tags, e.Templates, e.Separator)
c.WithDefaults() c.WithDefaults()
if err := c.Validate(); err != nil { if err := c.Validate(); err != nil {