-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #13 from opthomas-prime/slack-handler
Adds slack-compatible handler
- Loading branch information
Showing
7 changed files
with
130 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,14 @@ | ||
# xmpp-webhook | ||
- Multipurpose XMPP-Webhook (Built for Prometheus/Grafana Alerts) | ||
- Based on https://github.com/atomatt/go-xmpp | ||
- Multipurpose XMPP-Webhook (Built for DevOps Alerts) | ||
- Based on https://github.com/mellium/xmpp | ||
|
||
## Status | ||
`xmpp-webhook` ~~currently~~ only provides a hook for Grafana. ~~I will implement a `parserFunc` for Prometheus ASAP~~. Check https://github.com/opthomas-prime/xmpp-webhook/blob/master/handler.go to learn how to support more source services. | ||
`xmpp-webhook` currently support: | ||
|
||
- Grafana Webhook alerts | ||
- Slack Incoming Webhooks (Feedback appreciated) | ||
|
||
Check https://github.com/opthomas-prime/xmpp-webhook/blob/master/parser/ to learn how to support more source services. | ||
|
||
## Usage | ||
- `xmpp-webhook` is configured via environment variables: | ||
|
@@ -16,6 +21,7 @@ | |
|
||
``` | ||
curl -X POST -d @grafana-webhook-alert-example.json localhost:4321/grafana | ||
curl -X POST -d @slack-compatible-notification-example.json localhost:4321/slack | ||
``` | ||
- After parsing the body in the appropriate `parserFunc`, the notification is then distributed to the configured receivers. | ||
|
||
|
@@ -27,7 +33,9 @@ curl -X POST -d @grafana-webhook-alert-example.json localhost:4321/grafana | |
- Run: `docker run -e "[email protected]" -e "XMPP_PASS=xxx" -e "[email protected],[email protected]" -p 4321:4321 -d --name xmpp-webhook opthomasprime/xmpp-webhook:latest` | ||
|
||
## Installation | ||
IMPORTANT NOTE: For the sake of simplicity, `xmpp-webhook` is not reconnecting to the XMPP server after a connection-loss. If you use the provided `xmpp-webhook.service` - Systemd will manage the reconnect by restarting the service. | ||
~~IMPORTANT NOTE: For the sake of simplicity, `xmpp-webhook` is not reconnecting to the XMPP server after a connection-loss. If you use the provided `xmpp-webhook.service` - Systemd will manage the reconnect by restarting the service.~~. | ||
|
||
-> https://github.com/mellium/xmpp automatically reconnects after a failure. | ||
|
||
- Download and extract the latest tarball (GitHub release page) | ||
- Install the binary: `install -D -m 744 xmpp-webhook /usr/local/bin/xmpp-webhook` | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
{ | ||
"channel": "#channel", | ||
"icon_emoji": ":heart:", | ||
"username": "Flux Deployer", | ||
"attachments": [ | ||
{ | ||
"color": "#4286f4", | ||
"title": "Applied flux changes to cluster", | ||
"title_link": "https://GITURL/USERNAME/kubernetes/commit/COMMITSHA", | ||
"text": "Event: Sync: 0f34755, jabber:deployment/test\nCommits:\n\n* \u003chttps://GITURL/USERNAME/kubernetes/commit/COMMITSHA\u003e: change test to test webhook\n\nResources updated:\n\n* jabber:deployment/test" | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
package parser | ||
|
||
const readErr string = "failed to read alert body" | ||
const parseErr string = "failed to parse alert body" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package parser | ||
|
||
import ( | ||
"encoding/json" | ||
"errors" | ||
"io/ioutil" | ||
"net/http" | ||
) | ||
|
||
func GrafanaParserFunc(r *http.Request) (string, error) { | ||
// get alert data from request | ||
body, err := ioutil.ReadAll(r.Body) | ||
if err != nil { | ||
return "", errors.New(readErr) | ||
} | ||
|
||
alert := &struct { | ||
Title string `json:"title"` | ||
RuleURL string `json:"ruleUrl"` | ||
State string `json:"state"` | ||
Message string `json:"message"` | ||
}{} | ||
|
||
// parse body into the alert struct | ||
err = json.Unmarshal(body, &alert) | ||
if err != nil { | ||
return "", errors.New(parseErr) | ||
} | ||
|
||
// contruct alert message | ||
var message string | ||
switch alert.State { | ||
case "ok": | ||
message = ":) " + alert.Title | ||
default: | ||
message = ":( " + alert.Title + "\n\n" | ||
message += alert.Message + "\n\n" | ||
message += alert.RuleURL | ||
} | ||
|
||
return message, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package parser | ||
|
||
import ( | ||
"encoding/json" | ||
"errors" | ||
"io/ioutil" | ||
"net/http" | ||
) | ||
|
||
func SlackParserFunc(r *http.Request) (string, error) { | ||
// get alert data from request | ||
body, err := ioutil.ReadAll(r.Body) | ||
if err != nil { | ||
return "", errors.New(readErr) | ||
} | ||
|
||
alert := struct { | ||
Text string `json:"text"` | ||
Attachments []struct { | ||
Title string `json:"title"` | ||
TitleLink string `json:"title_link"` | ||
Text string `json:"text"` | ||
} `json:"attachments"` | ||
}{} | ||
|
||
// parse body into the alert struct | ||
err = json.Unmarshal(body, &alert) | ||
if err != nil { | ||
return "", errors.New(parseErr) | ||
} | ||
|
||
// contruct alert message | ||
message := alert.Text | ||
for _, attachment := range alert.Attachments { | ||
if len(message) > 0 { | ||
message = message + "\n" | ||
} | ||
message += attachment.Title + "\n" | ||
message += attachment.TitleLink + "\n\n" | ||
message += attachment.Text | ||
} | ||
|
||
return message, nil | ||
} |