2021-12-05 12:10:25 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2021-12-05 15:55:16 +00:00
|
|
|
"bytes"
|
2021-12-05 12:10:25 +00:00
|
|
|
"fmt"
|
2021-12-05 15:55:16 +00:00
|
|
|
tTemplate "text/template"
|
2021-12-05 12:10:25 +00:00
|
|
|
|
|
|
|
"github.com/gofiber/fiber/v2"
|
|
|
|
"github.com/prometheus/alertmanager/template"
|
|
|
|
"maunium.net/go/mautrix"
|
|
|
|
"maunium.net/go/mautrix/id"
|
|
|
|
)
|
|
|
|
|
|
|
|
func HandleAlertmanagerPayloadPost(c *fiber.Ctx) error {
|
|
|
|
payload := template.Data{}
|
|
|
|
if err := c.BodyParser(&payload); err != nil {
|
2021-12-05 14:02:29 +00:00
|
|
|
fmt.Println("Failed to parse payload", err)
|
2021-12-05 12:10:25 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-12-05 14:02:29 +00:00
|
|
|
fmt.Println("Got alertmanager payload")
|
|
|
|
|
2021-12-05 12:10:25 +00:00
|
|
|
for _, alert := range payload.Alerts {
|
|
|
|
message := ""
|
2021-12-05 15:55:16 +00:00
|
|
|
|
|
|
|
var rendered bytes.Buffer
|
|
|
|
at, _ := tTemplate.New("AlertMessage").Parse(alert.Annotations["description"])
|
|
|
|
at.Execute(&rendered, alert)
|
|
|
|
|
2021-12-05 12:10:25 +00:00
|
|
|
if alert.Status == "firing" {
|
|
|
|
switch alert.Labels["severity"] {
|
|
|
|
case "warning":
|
2021-12-05 15:55:16 +00:00
|
|
|
message = fmt.Sprintf("⚠️ %s", rendered.String())
|
2021-12-05 12:10:25 +00:00
|
|
|
case "notify":
|
2021-12-05 15:55:16 +00:00
|
|
|
message = fmt.Sprintf("@room - %s", rendered.String())
|
2021-12-05 12:10:25 +00:00
|
|
|
}
|
|
|
|
} else {
|
2021-12-05 15:55:16 +00:00
|
|
|
message = fmt.Sprintf("☑️ %s", rendered.String())
|
2021-12-05 12:10:25 +00:00
|
|
|
}
|
|
|
|
|
2021-12-05 15:30:58 +00:00
|
|
|
_, err := matrixClient.SendText(id.RoomID(getRoom(*defaultRoom)), message)
|
2021-12-05 12:10:25 +00:00
|
|
|
if err != nil {
|
2021-12-05 14:02:29 +00:00
|
|
|
fmt.Println("Failed sending to Matrix", err)
|
2021-12-05 12:10:25 +00:00
|
|
|
if httpErr, ok := err.(mautrix.HTTPError); ok {
|
|
|
|
return c.Status(httpErr.Response.StatusCode).SendString(httpErr.RespError.Err)
|
|
|
|
}
|
|
|
|
return fiber.ErrInternalServerError
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.SendStatus(fiber.StatusOK)
|
|
|
|
}
|