Skip to content
This repository has been archived by the owner on Dec 11, 2022. It is now read-only.

Commit

Permalink
Merge pull request #24 from jaxxstorm/json_string_flag
Browse files Browse the repository at this point in the history
Add support for json strings as a flag
  • Loading branch information
jaxxstorm authored Jan 6, 2017
2 parents 34cc61e + 9ce17b1 commit 67d1fc3
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 4 deletions.
11 changes: 10 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
34 changes: 31 additions & 3 deletions sensu-wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 67d1fc3

Please sign in to comment.