diff --git a/README.md b/README.md index 82041d1..2fb1d18 100644 --- a/README.md +++ b/README.md @@ -3,9 +3,10 @@ [![Go Test](https://github.com/windynik/sensu-go-zenduty-handler/workflows/Go%20Test/badge.svg)](https://github.com/windynik/sensu-go-zenduty-handler/actions?query=workflow%3A%22Go+Test%22) [![goreleaser](https://github.com/windynik/sensu-go-zenduty-handler/workflows/goreleaser/badge.svg)](https://github.com/windynik/sensu-go-zenduty-handler/actions?query=workflow%3Agoreleaser) -# Handler Plugin Template + -# sensu-go-zenduty-handler +# Sensu Go Zenduty Plugin ## Table of Contents + - [Overview](#overview) - [Files](#files) - [Usage examples](#usage-examples) @@ -46,12 +48,23 @@ the plugin with goreleaser. Register the asset with [Bonsai][8] to share it with ## Overview -The sensu-go-zenduty-handler is a [Sensu Handler][6] that ... - -## Files +The Sensu Zenduty Handler is a [Sensu Handler][6] that can trigger and resolve incident in Zenduty for alerting Operators and entities. ## Usage examples +### Help + +``` +Usage: + sensu-go-zenduty-handler [flags] + +Flags: + -d, --debug Enable debug mode, which prints JSON object which would be POSTed to the Zenduty webhook instead of actually POSTing it + -h, --help help for sensu-go-zenduty-handler + -w, --webhook string The Zenduty Webhook URL, use default from ZENDUTY_WEBHOOK env var + -a, --withAnnotations string The Zenduty handler will parse check and entity annotations with these values. Use ZENDUTY_ANNOTATIONS env var with commas, like: documentation,playbook +``` + ## Configuration ### Asset registration @@ -70,41 +83,22 @@ If you're using an earlier version of sensuctl, you can find the asset on the [B ```yml --- -type: Handler api_version: core/v2 +type: Handler metadata: name: sensu-go-zenduty-handler namespace: default spec: - command: sensu-go-zenduty-handler --example example_arg type: pipe + command: 'sensu-go-zenduty-handler -w "${ZENDUTY_WEBHOOK}"' + timeout: 0 + filters: + - is_incident + - not_silenced + env_vars: + - ZENDUTY_WEBHOOK={{YOUR_ZENDUTY_WEBHOOK}} runtime_assets: - - windynik/sensu-go-zenduty-handler -``` - -#### Proxy Support - -This handler supports the use of the environment variables HTTP_PROXY, -HTTPS_PROXY, and NO_PROXY (or the lowercase versions thereof). HTTPS_PROXY takes -precedence over HTTP_PROXY for https requests. The environment values may be -either a complete URL or a "host[:port]", in which case the "http" scheme is assumed. - -### Annotations - -All arguments for this handler are tunable on a per entity or check basis based on annotations. The -annotations keyspace for this handler is `sensu.io/plugins/sensu-go-zenduty-handler/config`. - -#### Examples - -To change the example argument for a particular check, for that checks's metadata add the following: - -```yml -type: CheckConfig -api_version: core/v2 -metadata: - annotations: - sensu.io/plugins/sensu-go-zenduty-handler/config/example-argument: "Example change" -[...] + - sensu-go-zenduty-handler ``` ## Installation from source @@ -119,13 +113,6 @@ From the local path of the sensu-go-zenduty-handler repository: go build ``` -## Additional notes - -## Contributing - -For more information about contributing to this plugin, see [Contributing][1]. - -[1]: https://github.com/sensu/sensu-go/blob/master/CONTRIBUTING.md [2]: https://github.com/sensu-community/sensu-plugin-sdk [3]: https://github.com/sensu-plugins/community/blob/master/PLUGIN_STYLEGUIDE.md [4]: https://github.com/sensu-community/handler-plugin-template/blob/master/.github/workflows/release.yml diff --git a/example-event.json b/example-event.json index f9216a3..7de14df 100644 --- a/example-event.json +++ b/example-event.json @@ -117,5 +117,6 @@ "labels": null, "annotations": null } - } + }, + "id": "3c3e68f6-6db7-40d3-9b84-4d61817ae559" } diff --git a/main b/main deleted file mode 100755 index d7ee9c3..0000000 Binary files a/main and /dev/null differ diff --git a/main.go b/main.go index 7c0ebc7..20b1be0 100644 --- a/main.go +++ b/main.go @@ -26,6 +26,7 @@ type Properties struct { Subject string `json:"subject"` Message string `json:"message"` Status string `json:"status"` + Id string `json:"id"` } // Payload struct includes Properties struct for post to Zenduty webhook URL @@ -117,14 +118,25 @@ func parseAnnotations(event *types.Event) string { return output } +// Gets ID of the event to set as Zenduty Entity Id, if ID doesn't exist(?) check name is used. +func getID(event *types.Event) string { + var output string + // if len(event.ID)>0{ + // output = string(event.ID) + // } else{ + output = event.Check.Name + // } + return output +} + // eventSubject func returns a one-line short summary func eventSubject(event *types.Event) string { - return fmt.Sprintf("%s %s on host %s", event.Check.Name, formattedEventAction(event), event.Entity.Name) + return fmt.Sprintf("The %s check has changed to %s on host %s", event.Check.Name, formattedEventAction(event), event.Entity.Name) } // eventDescription func returns a formatted message func eventDescription(event *types.Event) string { - return fmt.Sprintf("Server: %s, \nCheck: %s, \nStatus: %s, \nCheck Output: %s, \nAnnotation Information:\n%s", event.Entity.Name, event.Check.Name, formattedEventAction(event), event.Check.Output, parseAnnotations(event)) + return fmt.Sprintf("Server: %s, \nCheck: %s, \nStatus: %s, \nCheck Output: %s, \nAnnotation Information: %s\n", event.Entity.Name, event.Check.Name, formattedEventAction(event), event.Check.Output, parseAnnotations(event)) } func run(cmd *cobra.Command, args []string) error { @@ -170,7 +182,7 @@ func run(cmd *cobra.Command, args []string) error { Subject: eventSubject(event), Message: eventDescription(event), Status: formattedEventAction(event), - + Id : getID(event), }, } bodymarshal, err := json.Marshal(formPost) diff --git a/main.old.go b/main.old.go deleted file mode 100644 index 12362ba..0000000 --- a/main.old.go +++ /dev/null @@ -1,54 +0,0 @@ -package main - -import ( - "fmt" - "log" - - "github.com/sensu-community/sensu-plugin-sdk/sensu" - "github.com/sensu/sensu-go/types" -) - -// Config represents the handler plugin config. -type Config struct { - sensu.PluginConfig - Example string -} - -var ( - plugin = Config{ - PluginConfig: sensu.PluginConfig{ - Name: "sensu-go-zenduty-handler", - Short: "A handler that creates alerts and resolves alerts in Zenduty", - Keyspace: "sensu.io/plugins/sensu-go-zenduty-handler/config", - }, - } - - options = []*sensu.PluginConfigOption{ - &sensu.PluginConfigOption{ - Path: "example", - Env: "HANDLER_EXAMPLE", - Argument: "example", - Shorthand: "e", - Default: "", - Usage: "An example string configuration option", - Value: &plugin.Example, - }, - } -) - -func main() { - handler := sensu.NewGoHandler(&plugin.PluginConfig, options, checkArgs, executeHandler) - handler.Execute() -} - -func checkArgs(_ *types.Event) error { - if len(plugin.Example) == 0 { - return fmt.Errorf("--example or HANDLER_EXAMPLE environment variable is required") - } - return nil -} - -func executeHandler(event *types.Event) error { - log.Println("executing handler with --example", plugin.Example) - return nil -}