diff --git a/config_example.conf b/config_example.conf index 5bbcad4..a6a7ab1 100644 --- a/config_example.conf +++ b/config_example.conf @@ -10,6 +10,7 @@ startup_notify_muc = [] # suported hooks are, which could be declared multiple times with different `secrets` (see [[hooks.grafana]]): [[hooks.grafana]] +[[hooks.prometheus]] [[hooks.git]] [[hooks.gitlab]] [[hooks.circleci]] @@ -19,6 +20,9 @@ secret = "" notify_muc = [] notify_user = [] +# for handling webhooks from prometheus alertmanager + +[[hooks.prometheus]] # for handling webhooks from grafana # at http://localhost:8080/grafana diff --git a/main.go b/main.go index f158306..fdbcbcf 100644 --- a/main.go +++ b/main.go @@ -15,6 +15,7 @@ import ( _ "dev.sum7.eu/genofire/hook2xmpp/git" _ "dev.sum7.eu/genofire/hook2xmpp/gitlab" _ "dev.sum7.eu/genofire/hook2xmpp/grafana" + _ "dev.sum7.eu/genofire/hook2xmpp/prometheus" "dev.sum7.eu/genofire/hook2xmpp/runtime" ) diff --git a/prometheus/main.go b/prometheus/main.go new file mode 100644 index 0000000..45ab9f6 --- /dev/null +++ b/prometheus/main.go @@ -0,0 +1,55 @@ +package prometheus + +import ( + "fmt" + + "net/http" + + libHTTP "dev.sum7.eu/genofire/golang-lib/http" + "github.com/bdlm/log" + xmpp "github.com/mattn/go-xmpp" + "github.com/prometheus/alertmanager/blob/master/notify" + + "dev.sum7.eu/genofire/hook2xmpp/runtime" +) + +const hookType = "prometheus" + + +func init() { + runtime.HookRegister[hookType] = func(client *xmpp.Client, hooks []runtime.Hook) func(w http.ResponseWriter, r *http.Request) { + log.WithField("type", hookType).Info("loaded") + return func(w http.ResponseWriter, r *http.Request) { + logger := log.WithField("type", hookType) + + var request notify.WebhookMessage + 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 + } + + contentTag := request.Status + if len(request.Alerts.Firing) > 0 { + contentTag += ":" + len(request.Alerts.Firing) + } + + content := fmt.Sprintf("[%s] %s", contentTag strings.Join(request.CommonAnnotations.Values()," ")) + logger = logger.WithField("body", content) + + ok := false + for _, hook := range hooks { + if request.Payload.VCSURL != hook.Secret { + continue + } + logger.Infof("run hook") + runtime.Notify(client, hook, content) + 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) + } + } + } +}