Skip to content

Commit

Permalink
Fixes and refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
Roger Rumao committed Mar 23, 2021
1 parent e69b257 commit ab20e14
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 25 deletions.
22 changes: 17 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,25 @@
Alertmanager Discord Webhook
===
========

[![Docker Image Version (latest semver)](https://img.shields.io/docker/v/rogerrum/alertmanager-discord)](https://hub.docker.com/r/rogerrum/alertmanager-discord/tags)

A minimal docker image with golang application, which listens for Prometheus Alertmanager's notifications and pushes them to Discord channel.

Repository name in Docker Hub: **[rogerrum/alertmanager-discord](https://hub.docker.com/r/rogerrum/alertmanager-discord/)**
Published via **automated build** mechanism


Give this a webhook (with the DISCORD_WEBHOOK environment variable) and point it as a webhook on alertmanager, and it will post your alerts into a discord channel for you as they trigger:

![img.png](./.github/demo-img.png)
![img.png](https://raw.githubusercontent.com/rogerrum/alertmanager-discord/main/.github/demo-img.png)

## Environment configuration variables
```properties
DISCORD_WEBHOOK=<webhook, where to post alerts. For more details see: https://support.discordapp.com/hc/en-us/articles/228383668-Intro-to-Webhooks>
DISCORD_USERNAME=<override bot name at Discord (optional)>
DISCORD_AVATAR_URL=<override avatar url at Discord (optional)>
VERBOSE=ON <(Optional - logs request and response)>
```

## Warning

Expand Down Expand Up @@ -56,10 +70,8 @@ receivers:
- url: 'http://localhost:9094'
```

## Docker
For more details see: https://prometheus.io/docs/alerting/configuration/

If you run a fancy docker/k8s infra, you can find the docker hub repo here: https://hub.docker.com/r/rogerrum/alertmanager-discord/

## Inspired by

* https://github.com/benjojo/alertmanager-discord
52 changes: 32 additions & 20 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ var (
listenAddress = flag.String("listen.address", os.Getenv("LISTEN_ADDRESS"), "Address:Port to listen on.")
username = flag.String("username", os.Getenv("DISCORD_USERNAME"), "Overrides the predefined username of the webhook.")
avatarURL = flag.String("avatar.url", os.Getenv("DISCORD_AVATAR_URL"), "Overrides the predefined avatar of the webhook.")
verboseMode = flag.String("verbose", os.Getenv("VERBOSE"), "Verbose mode")
)

func checkWebhookURL(webhookURL string) {
Expand Down Expand Up @@ -191,6 +192,11 @@ func sendWebhook(alertManagerData *AlertManagerData) {
}

discordMessageBytes, _ := json.Marshal(discordMessage)

if *verboseMode == "ON" {
log.Printf("Sending weebhok message to Discord: %s", string(discordMessageBytes))
}

http.Post(*webhookURL, "application/json", bytes.NewReader(discordMessageBytes))
}
}
Expand Down Expand Up @@ -285,29 +291,35 @@ func main() {
}

log.Printf("Listening on: %s", *listenAddress)
http.ListenAndServe(*listenAddress, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
log.Printf("%s - [%s] %s", r.Host, r.Method, r.URL.RawPath)
log.Fatal(http.ListenAndServe(*listenAddress, http.HandlerFunc(handleWebHook)))
}

body, err := ioutil.ReadAll(r.Body)
if err != nil {
panic(err)
}
func handleWebHook(w http.ResponseWriter, r *http.Request) {
log.Printf("%s - [%s] %s", r.Host, r.Method, r.URL.RawPath)

alertManagerData := AlertManagerData{}
err = json.Unmarshal(body, &alertManagerData)
if err != nil {
if isRawPromAlert(body) {
sendRawPromAlertWarn()
return
}
if len(body) > 1024 {
log.Printf("Failed to unpack inbound alert request - %s...", string(body[:1023]))
body, err := ioutil.ReadAll(r.Body)
if err != nil {
panic(err)
}

} else {
log.Printf("Failed to unpack inbound alert request - %s", string(body))
}
if *verboseMode == "ON" {
log.Printf("request payload: %s", string(body))
}

alertManagerData := AlertManagerData{}
err = json.Unmarshal(body, &alertManagerData)
if err != nil {
if isRawPromAlert(body) {
sendRawPromAlertWarn()
return
}
sendWebhook(&alertManagerData)
}))
if len(body) > 1024 {
log.Printf("Failed to unpack inbound alert request - %s...", string(body[:1023]))

} else {
log.Printf("Failed to unpack inbound alert request - %s", string(body))
}
return
}
sendWebhook(&alertManagerData)
}

0 comments on commit ab20e14

Please sign in to comment.