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.

grafana.go 1012B

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. package httplistener
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "fmt"
  6. "github.com/spf13/viper"
  7. "net/http"
  8. )
  9. type grafanaMatch struct {
  10. Metric string
  11. Value float32
  12. }
  13. type grafanaAlert struct {
  14. Title string
  15. RuleName string
  16. RuleUrl string
  17. State string
  18. ImageUrl string
  19. Message string
  20. EvalMatches []grafanaMatch
  21. }
  22. func (hl *HTTPListener) grafanaAlertHandler(w http.ResponseWriter, request *http.Request) {
  23. if request.Method != "POST" {
  24. http.NotFound(w, request)
  25. return
  26. }
  27. var alert grafanaAlert
  28. buf := new(bytes.Buffer)
  29. buf.ReadFrom(request.Body)
  30. json.Unmarshal(buf.Bytes(), &alert)
  31. msg := fmt.Sprintf("[Grafana] [%s] %s: %s.", alert.State, alert.RuleName, alert.Message)
  32. for _, match := range alert.EvalMatches {
  33. msg += fmt.Sprintf(" %s:%f", match.Metric, match.Value)
  34. }
  35. msg += " " + alert.RuleUrl
  36. log.Infof("%s [%s] Grafana alert", request.RemoteAddr, viper.GetString("http.listeners.grafana"))
  37. hl.irc.Privmsgf(viper.GetString("http.listeners.grafana"), msg)
  38. }