Skip to content

Commit e6edc22

Browse files
authored
Merge pull request benjojo#3 from funkypenguin/master
Fix error checking, add Dockerfile
2 parents 0b06423 + 34535fc commit e6edc22

File tree

3 files changed

+38
-4
lines changed

3 files changed

+38
-4
lines changed

Dockerfile

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# Built following https://medium.com/@chemidy/create-the-smallest-and-secured-golang-docker-image-based-on-scratch-4752223b7324
2+
3+
# STEP 1 build executable binary
4+
FROM golang:alpine as builder
5+
# Install SSL ca certificates
6+
RUN apk update && apk add git && apk add ca-certificates
7+
# Create appuser
8+
RUN adduser -D -g '' appuser
9+
COPY . $GOPATH/src/mypackage/myapp/
10+
WORKDIR $GOPATH/src/mypackage/myapp/
11+
#get dependancies
12+
RUN go get -d -v
13+
#build the binary
14+
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -a -installsuffix cgo -ldflags="-w -s" -o /go/bin/alertmanager-discord
15+
16+
17+
# STEP 2 build a small image
18+
# start from scratch
19+
FROM scratch
20+
COPY --from=builder /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/
21+
COPY --from=builder /etc/passwd /etc/passwd
22+
# Copy our static executable
23+
COPY --from=builder /go/bin/alertmanager-discord /go/bin/alertmanager-discord
24+
25+
EXPOSE 9094
26+
USER appuser
27+
ENTRYPOINT ["/go/bin/alertmanager-discord"]
28+

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
alertmanager-discord
22
===
33

4-
Give this a webhook and point it as a webhook on alertmanager, and it will post your alerts into a discord channel for you as they trigger:
4+
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:
55

66
![](/.github/demo.png)
77

@@ -31,4 +31,4 @@ receivers:
3131
- name: 'discord_webhook'
3232
webhook_configs:
3333
- url: 'http://localhost:9094'
34-
```
34+
```

main.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"encoding/json"
66
"flag"
77
"fmt"
8+
"os"
89
"io/ioutil"
910
"net/http"
1011
"strings"
@@ -44,9 +45,14 @@ type discordOut struct {
4445
}
4546

4647
func main() {
47-
webhookUrl := os.Getenv("webhook")
48+
webhookUrl := os.Getenv("DISCORD_WEBHOOK")
49+
if webhookUrl == "" {
50+
fmt.Fprintf(os.Stderr, "error: environment variable DISCORD_WEBHOOK not found\n")
51+
os.Exit(1)
52+
}
4853
whURL := flag.String("webhook.url", webhookUrl, "")
4954
flag.Parse()
55+
fmt.Fprintf(os.Stdout, "info: Listening on 0.0.0.0:9094\n")
5056
http.ListenAndServe(":9094", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
5157
b, err := ioutil.ReadAll(r.Body)
5258
if err != nil {
@@ -73,7 +79,7 @@ func main() {
7379
if strings.Contains(realname, "localhost") && alert.Labels["exported_instance"] != "" {
7480
realname = alert.Labels["exported_instance"]
7581
}
76-
Content += fmt.Sprintf("[%s]: %s on %s\n%s\n", strings.ToUpper(amo.Status), alert.Labels["alertname"], realname, alert.Annotations.Description)
82+
Content += fmt.Sprintf("[%s]: %s on %s\n%s\n\n", strings.ToUpper(amo.Status), alert.Labels["alertname"], realname, alert.Annotations.Description)
7783
}
7884

7985
DO.Content = Content + "```"

0 commit comments

Comments
 (0)