Properly handle incoming hooks
This commit is contained in:
parent
b3074bee04
commit
a89b9ff694
2 changed files with 27 additions and 11 deletions
26
main.go
26
main.go
|
@ -6,13 +6,13 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"io/ioutil"
|
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"github.com/prometheus/alertmanager/template"
|
||||||
"github.com/matrix-org/gomatrix"
|
"github.com/matrix-org/gomatrix"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Alert struct {
|
func generateMatrixMessageBody(alert template.Alert) string {
|
||||||
|
return alert.Status + " // " + alert.Annotations["summary"]
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
@ -77,16 +77,20 @@ You will find more details on: http://git.sr.ht/~fnux/matrix-prometheus-alertman
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
var alert Alert
|
payload := template.Data{}
|
||||||
reqBody, _ := ioutil.ReadAll(r.Body)
|
if err := json.NewDecoder(r.Body).Decode(&payload); err != nil {
|
||||||
json.Unmarshal(reqBody, &alert)
|
w.WriteHeader(http.StatusBadRequest)
|
||||||
|
}
|
||||||
|
|
||||||
// Check validity
|
logger.Printf("Received valid hook from %v", r.RemoteAddr)
|
||||||
|
|
||||||
logger.Printf("Sending message")
|
for _, alert := range payload.Alerts {
|
||||||
_, err := matrixClient.SendText(*target, "spouik spouik spouik")
|
body := generateMatrixMessageBody(alert)
|
||||||
if err != nil {
|
logger.Printf("> %v", body)
|
||||||
logger.Fatalf("Failed to send message: %v", err)
|
_, err := matrixClient.SendText(*target, body)
|
||||||
|
if err != nil {
|
||||||
|
logger.Fatalf("Could not forward to Matrix: %v", err)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
w.WriteHeader(http.StatusOK)
|
w.WriteHeader(http.StatusOK)
|
||||||
|
|
12
mock-webhook.sh
Executable file
12
mock-webhook.sh
Executable file
|
@ -0,0 +1,12 @@
|
||||||
|
#!/bin/sh
|
||||||
|
#
|
||||||
|
# Send a dummy hook to localhost:9088 for testing purposes.
|
||||||
|
|
||||||
|
TARGET=http://localhost:9088/alert
|
||||||
|
PAYLOAD=$(cat << EOF
|
||||||
|
{"receiver":"matrix","status":"firing","alerts":[{"status":"firing","labels":{"alertname":"instance_down","instance":"example1"},"annotations":{"info":"The instance example1 is down","summary":"instance example1 is down"},"startsAt":"2020-05-03T08:30:06.275828332+02:00","endsAt":"0001-01-01T00:00:00Z","generatorURL":""}],"groupLabels":{"alertname":"instance_down"},"commonLabels":{"alertname":"instance_down","instance":"example1"},"commonAnnotations":{"info":"The instance example1 is down","summary":"instance example1 is down"},"externalURL":"http://control:9093","version":"4","groupKey":"{}:{alertname=\"instance_down\"}"}
|
||||||
|
EOF
|
||||||
|
)
|
||||||
|
|
||||||
|
curl -X POST -d "$PAYLOAD" "$TARGET"
|
||||||
|
|
Loading…
Reference in a new issue