2019-06-18 01:22:30 +02:00
|
|
|
package prometheus
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
libHTTP "dev.sum7.eu/genofire/golang-lib/http"
|
|
|
|
"github.com/bdlm/log"
|
2019-06-20 07:51:12 +02:00
|
|
|
"github.com/prometheus/alertmanager/notify/webhook"
|
2019-07-15 23:57:26 +02:00
|
|
|
"gosrc.io/xmpp"
|
2019-06-18 01:22:30 +02:00
|
|
|
|
2019-08-08 15:49:25 +02:00
|
|
|
"dev.sum7.eu/sum7/hook2xmpp/runtime"
|
2019-06-18 01:22:30 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
const hookType = "prometheus"
|
|
|
|
|
|
|
|
func init() {
|
2019-07-15 23:57:26 +02:00
|
|
|
runtime.HookRegister[hookType] = func(client xmpp.Sender, hooks []runtime.Hook) func(w http.ResponseWriter, r *http.Request) {
|
2019-06-18 01:22:30 +02:00
|
|
|
log.WithField("type", hookType).Info("loaded")
|
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
logger := log.WithField("type", hookType)
|
|
|
|
|
2019-06-20 07:51:12 +02:00
|
|
|
var request webhook.Message
|
2019-06-18 01:22:30 +02:00
|
|
|
if err := libHTTP.Read(r, &request); err != nil {
|
|
|
|
logger.Errorf("no readable payload: %s", err)
|
|
|
|
http.Error(w, fmt.Sprintf("no readable payload"), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-07-16 00:53:20 +02:00
|
|
|
// title
|
2019-06-18 01:22:30 +02:00
|
|
|
content := strings.Join(request.GroupLabels.Values(), " ")
|
2019-07-16 00:53:20 +02:00
|
|
|
html := fmt.Sprintf(`<span style="font-weight: bold;">%s</span>`, content)
|
|
|
|
|
|
|
|
statusColor := "#ffff00"
|
|
|
|
|
|
|
|
switch request.Status {
|
|
|
|
case "resolved":
|
|
|
|
statusColor = "#00ff00"
|
|
|
|
case "firing":
|
2019-07-16 08:54:00 +02:00
|
|
|
statusColor = "#ff0000"
|
2019-07-16 00:53:20 +02:00
|
|
|
}
|
|
|
|
|
2019-06-18 01:22:30 +02:00
|
|
|
firingAlerts := request.Alerts.Firing()
|
|
|
|
if len(firingAlerts) > 0 {
|
|
|
|
for _, a := range firingAlerts {
|
2019-06-20 07:13:22 +02:00
|
|
|
if description, ok := a.Annotations["message"]; ok {
|
2019-06-18 01:22:30 +02:00
|
|
|
content = fmt.Sprintf("%s\n%s", content, description)
|
2019-07-16 00:53:20 +02:00
|
|
|
html = fmt.Sprintf("%s<br/>%s", html, description)
|
2019-06-18 01:22:30 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
content = fmt.Sprintf("[%s:%d] %s", request.Status, len(firingAlerts), content)
|
2019-07-16 00:53:20 +02:00
|
|
|
html = fmt.Sprintf(`<span style="color:%s">%s:%d</span> %s`, statusColor, request.Status, len(firingAlerts), html)
|
2019-06-18 01:22:30 +02:00
|
|
|
} else {
|
2019-07-16 00:53:20 +02:00
|
|
|
html = fmt.Sprintf(`<span style="color:%s">%s</span> %s`, statusColor, request.Status, content)
|
2019-06-18 01:22:30 +02:00
|
|
|
content = fmt.Sprintf("[%s] %s", request.Status, content)
|
|
|
|
}
|
|
|
|
|
|
|
|
logger = logger.WithField("body", content)
|
|
|
|
|
|
|
|
ok := false
|
|
|
|
token := strings.TrimSpace(strings.TrimPrefix(r.Header.Get("Authorization"), "Bearer "))
|
|
|
|
for _, hook := range hooks {
|
|
|
|
if token != hook.Secret {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
logger.Infof("run hook")
|
2019-07-16 00:53:20 +02:00
|
|
|
runtime.Notify(client, hook, content, html)
|
2019-06-18 01:22:30 +02:00
|
|
|
ok = true
|
|
|
|
}
|
|
|
|
if !ok {
|
|
|
|
logger.Warnf("no hook found")
|
|
|
|
http.Error(w, fmt.Sprintf("no configuration for %s for url: %s", hookType, request.ExternalURL), http.StatusNotFound)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|