diff --git a/README.md b/README.md index e29756f..776afbe 100644 --- a/README.md +++ b/README.md @@ -84,7 +84,9 @@ $ sensu-wrapper -d -n "name" -d -T 25 ping 8.8.8.8 #### Extra JSON -Many people who use Sensu will add arbitrary JSON fields to their checks which are then used in handlers. `sensu-wrapper` supports this by specifying the path to a file to read in which will then be sent in the check. For example you might have a file like so: +Many people who use Sensu will add arbitrary JSON fields to their checks which are then used in handlers. `sensu-wrapper` supports this in two ways. + +First, you can add JSON by specifying the path to a file to read in which will then be sent in the check. For example you might have a file like so: ```json # /tmp/mycheck.json @@ -102,6 +104,13 @@ $ sensu-wrapper -n "testing" -f /tmp/json -H default -d --ttl 30 -source "mychec {"command":"/bin/echo hello","environment":"production","handlers":["default"],"message":"there is a problem","name":"testing","output":"hello\n","runbook":"http://url","source":"mycheck","status":0,"ttl":30} ``` +Alternatively, you can specify JSON directly as a string: + +```shell +$ sensu-wrapper -d -n "testing" -j '{"test_field": "hello"}' /bin/echo 'hello' +{"command":"/bin/echo hello","name":"testing","output":"hello\n","status":0,"test_field":"hello"} +``` + ## Building Make sure your `$GOPATH` is set: https://github.com/golang/go/wiki/GOPATH diff --git a/sensu-wrapper.go b/sensu-wrapper.go index d02855e..e4f53dc 100644 --- a/sensu-wrapper.go +++ b/sensu-wrapper.go @@ -86,6 +86,7 @@ func main() { cli.StringFlag{Name: "source, S, s", Usage: "The source of the check"}, cli.StringSliceFlag{Name: "handlers, H", Usage: "The handlers to use for the check"}, cli.StringFlag{Name: "json-file, f", Usage: "JSON file to read and add to output"}, + cli.StringFlag{Name: "json, j", Usage: "JSON string to add to output"}, } app.Name = "Sensu Wrapper" @@ -133,18 +134,45 @@ func main() { var output_json []byte if c.IsSet("json-file") { - json_file, err := ioutil.ReadFile(c.String("json-file")) + additional_json, err := ioutil.ReadFile(c.String("json-file")) // check for file errors if err != nil { panic(err) } // create to unmarshal JSON values := map[string]interface{}{} - if err := json.Unmarshal([]byte(json_file), &values); err != nil { + if err := json.Unmarshal([]byte(additional_json), &values); err != nil { return cli.NewExitError("Invalid JSON in"+c.String("json-file"), -1) } - // appened the values from sensu_values struct + // append the values from sensu_values struct + values["name"] = sensu_values.Name + values["command"] = sensu_values.Command + values["status"] = sensu_values.Status + values["output"] = sensu_values.Output + if sensu_values.Ttl != 0 { + values["ttl"] = sensu_values.Ttl + } + if sensu_values.Source != "" { + values["source"] = sensu_values.Source + } + if len(sensu_values.Handlers) != 0 { + values["handlers"] = sensu_values.Handlers + } + // marshal final values into JSON + output_json, _ = json.Marshal(values) + + } else if c.IsSet("json") { + additional_json := c.String("json") + + //FIXME: Remove duplicate code here + + // create to unmarshal JSON + values := map[string]interface{}{} + if err := json.Unmarshal([]byte(additional_json), &values); err != nil { + return cli.NewExitError("Invalid JSON in"+c.String("json"), -1) + } + // append the values from sensu_values struct values["name"] = sensu_values.Name values["command"] = sensu_values.Command values["status"] = sensu_values.Status