You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

prometheus.go 1.6KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. package httplistener
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "github.com/spf13/viper"
  6. "net/http"
  7. )
  8. type HookMessage struct {
  9. Version string `json:"version"`
  10. GroupKey string `json:"groupKey"`
  11. Status string `json:"status"`
  12. Receiver string `json:"receiver"`
  13. GroupLabels map[string]string `json:"groupLabels"`
  14. CommonLabels map[string]string `json:"commonLabels"`
  15. CommonAnnotations map[string]string `json:"commonAnnotations"`
  16. ExternalURL string `json:"externalURL"`
  17. Alerts []Alert `json:"alerts"`
  18. }
  19. type Alert struct {
  20. Status string `json:"status"`
  21. Labels map[string]string `json:"labels"`
  22. Annotations map[string]string `json:"annotations"`
  23. StartsAt string `json:"startsAt,omitempty"`
  24. EndsAt string `json:"endsAt,omitempty"`
  25. GeneratorURL string `json:"generatorURL"`
  26. }
  27. func (hl *HTTPListener) prometheusHandler(w http.ResponseWriter, request *http.Request) {
  28. if request.Method != "POST" {
  29. http.NotFound(w, request)
  30. return
  31. }
  32. var message HookMessage
  33. buf := new(bytes.Buffer)
  34. buf.ReadFrom(request.Body)
  35. json.Unmarshal(buf.Bytes(), &message)
  36. log.Infof("%s [%s] Prometheus alert", request.RemoteAddr, viper.GetString("http.listeners.prometheus"))
  37. msgs, err := hl.renderTemplate("prometheus.alert", message)
  38. if err != nil {
  39. log.Errorf("Unable to render template: %s", err)
  40. return
  41. }
  42. channel := viper.GetString("http.listeners.prometheus")
  43. for _, msg := range msgs {
  44. hl.irc.Privmsg(channel, msg)
  45. }
  46. }