first prometheus alert try
This commit is contained in:
		
							parent
							
								
									85ed49c032
								
							
						
					
					
						commit
						8edcc901a9
					
				| 
						 | 
				
			
			@ -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
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
							
								
								
									
										1
									
								
								main.go
								
								
								
								
							
							
						
						
									
										1
									
								
								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"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -0,0 +1,57 @@
 | 
			
		|||
package prometheus
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"fmt"
 | 
			
		||||
	"strings"
 | 
			
		||||
 | 
			
		||||
	"net/http"
 | 
			
		||||
 | 
			
		||||
	libHTTP "dev.sum7.eu/genofire/golang-lib/http"
 | 
			
		||||
	"github.com/bdlm/log"
 | 
			
		||||
	xmpp "github.com/mattn/go-xmpp"
 | 
			
		||||
	"github.com/prometheus/alertmanager/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
 | 
			
		||||
			}
 | 
			
		||||
 | 
			
		||||
			content := ""
 | 
			
		||||
			firingAlerts := len(request.Alerts.Firing())
 | 
			
		||||
			if firingAlerts > 0 {
 | 
			
		||||
				content = fmt.Sprintf("[%s:%d] %s", request.Status, firingAlerts, strings.Join(request.CommonAnnotations.Values(), " "))
 | 
			
		||||
			} else {
 | 
			
		||||
				content = fmt.Sprintf("[%s] %s", request.Status, strings.Join(request.CommonAnnotations.Values(), " "))
 | 
			
		||||
			}
 | 
			
		||||
 | 
			
		||||
			logger = logger.WithField("body", content)
 | 
			
		||||
 | 
			
		||||
			ok := false
 | 
			
		||||
			for _, hook := range hooks {
 | 
			
		||||
				if request.ExternalURL != 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)
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
		Loading…
	
		Reference in New Issue