exec plugin doesn't crash when given null JSON values

This commit is contained in:
Josh Palay 2015-08-14 14:41:01 -07:00 committed by Cameron Sparr
parent 4d614b3088
commit 55fb249f6b
2 changed files with 5 additions and 11 deletions

View File

@ -96,26 +96,19 @@ func (e *Exec) gatherCommand(c *Command, acc plugins.Accumulator) error {
return fmt.Errorf("exec: unable to parse output of '%s' as JSON, %s", c.Command, err) return fmt.Errorf("exec: unable to parse output of '%s' as JSON, %s", c.Command, err)
} }
return processResponse(acc, c.Name, map[string]string{}, jsonOut) processResponse(acc, c.Name, map[string]string{}, jsonOut)
return nil
} }
func processResponse(acc plugins.Accumulator, prefix string, tags map[string]string, v interface{}) error { func processResponse(acc plugins.Accumulator, prefix string, tags map[string]string, v interface{}) {
switch t := v.(type) { switch t := v.(type) {
case map[string]interface{}: case map[string]interface{}:
for k, v := range t { for k, v := range t {
if err := processResponse(acc, prefix+"_"+k, tags, v); err != nil { processResponse(acc, prefix+"_"+k, tags, v)
return err
}
} }
case float64: case float64:
acc.Add(prefix, v, tags) acc.Add(prefix, v, tags)
case bool, string, []interface{}:
// ignored types
return nil
default:
return fmt.Errorf("exec: got unexpected type %T with value %v (%s)", t, v, prefix)
} }
return nil
} }
func init() { func init() {

View File

@ -14,6 +14,7 @@ const validJson = `
"num_processes": 82, "num_processes": 82,
"cpu": { "cpu": {
"status": "red", "status": "red",
"nil_status": null,
"used": 8234, "used": 8234,
"free": 32 "free": 32
}, },