2019-02-14 01:32:27 +01:00
|
|
|
package grafana
|
2019-02-13 04:32:06 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
|
2019-02-14 03:18:55 +01:00
|
|
|
libHTTP "dev.sum7.eu/genofire/golang-lib/http"
|
2019-02-13 04:32:06 +01:00
|
|
|
"github.com/bdlm/log"
|
2019-07-15 23:57:26 +02:00
|
|
|
"gosrc.io/xmpp"
|
2019-02-13 04:32:06 +01:00
|
|
|
|
2019-08-08 15:49:25 +02:00
|
|
|
"dev.sum7.eu/sum7/hook2xmpp/runtime"
|
2019-02-13 04:32:06 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
const hookType = "grafana"
|
|
|
|
|
|
|
|
type evalMatch struct {
|
|
|
|
Tags map[string]string `mapstructure:"tags,omitempty"`
|
|
|
|
Metric string `mapstructure:"metric"`
|
|
|
|
Value float64 `mapstructure:"value"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type requestBody struct {
|
|
|
|
Title string `mapstructure:"title"`
|
|
|
|
State string `mapstructure:"state"`
|
|
|
|
RuleID int64 `mapstructure:"ruleId"`
|
|
|
|
RuleName string `mapstructure:"ruleName"`
|
|
|
|
RuleURL string `mapstructure:"ruleUrl"`
|
|
|
|
EvalMatches []evalMatch `mapstructure:"evalMatches"`
|
|
|
|
ImageURL string `mapstructure:"imageUrl"`
|
|
|
|
Message string `mapstructure:"message"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r requestBody) String() string {
|
|
|
|
msg := fmt.Sprintf("%s: %s", r.Title, r.Message)
|
|
|
|
for _, e := range r.EvalMatches {
|
2019-02-13 14:03:01 +01:00
|
|
|
msg = fmt.Sprintf("%s %s=%f", msg, e.Metric, e.Value)
|
2019-02-13 04:32:06 +01:00
|
|
|
}
|
|
|
|
return msg
|
|
|
|
}
|
2019-07-16 01:02:11 +02:00
|
|
|
func (r requestBody) HTML() string {
|
|
|
|
stateColor := "#ffff00"
|
|
|
|
switch r.State {
|
|
|
|
case "alerting":
|
|
|
|
stateColor = "#ff0000"
|
|
|
|
case "ok":
|
|
|
|
stateColor = "#00ff00"
|
|
|
|
}
|
|
|
|
|
|
|
|
msg := fmt.Sprintf("<span style=\"color: %s;\">%s</span> <span style=\"font-weight: bold;\">%s</span>: %s<br/>", stateColor, r.State, r.RuleName, r.Message)
|
|
|
|
for _, e := range r.EvalMatches {
|
|
|
|
msg = fmt.Sprintf("%s %s=%f", msg, e.Metric, e.Value)
|
|
|
|
}
|
|
|
|
return msg
|
|
|
|
}
|
2019-02-13 04:32:06 +01:00
|
|
|
|
|
|
|
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-02-13 04:52:00 +01:00
|
|
|
log.WithField("type", hookType).Info("loaded")
|
2019-02-13 04:32:06 +01:00
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
logger := log.WithField("type", hookType)
|
|
|
|
|
2019-02-14 03:18:55 +01:00
|
|
|
_, secret, ok := r.BasicAuth()
|
|
|
|
|
|
|
|
if !ok {
|
|
|
|
logger.Errorf("no secret found")
|
|
|
|
http.Error(w, fmt.Sprintf("no secret found (basic-auth password)"), http.StatusUnauthorized)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-02-13 04:32:06 +01:00
|
|
|
var request requestBody
|
2019-02-14 05:05:03 +01:00
|
|
|
if err := libHTTP.Read(r, &request); err != nil {
|
2019-02-13 04:32:06 +01:00
|
|
|
logger.Errorf("no readable payload: %s", err)
|
|
|
|
http.Error(w, fmt.Sprintf("no readable payload"), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
logger = logger.WithFields(map[string]interface{}{
|
2019-02-14 01:32:27 +01:00
|
|
|
"url": request.RuleURL,
|
|
|
|
"msg": request.String(),
|
2019-02-13 19:51:23 +01:00
|
|
|
"image": request.ImageURL,
|
2019-02-13 04:32:06 +01:00
|
|
|
})
|
|
|
|
|
2019-02-14 03:18:55 +01:00
|
|
|
ok = false
|
2019-07-15 23:57:26 +02:00
|
|
|
msg := request.String()
|
2019-07-16 01:02:11 +02:00
|
|
|
html := request.HTML()
|
2019-07-15 23:57:26 +02:00
|
|
|
|
2019-02-13 04:32:06 +01:00
|
|
|
for _, hook := range hooks {
|
2019-02-14 03:18:55 +01:00
|
|
|
if secret != hook.Secret {
|
2019-02-13 04:32:06 +01:00
|
|
|
continue
|
|
|
|
}
|
2019-02-13 19:51:23 +01:00
|
|
|
|
2019-07-16 01:02:11 +02:00
|
|
|
runtime.Notify(client, hook, msg, html)
|
2019-02-13 14:05:20 +01:00
|
|
|
if request.ImageURL != "" {
|
2019-02-13 19:51:23 +01:00
|
|
|
runtime.NotifyImage(client, hook, request.ImageURL, request.String())
|
2019-02-14 01:32:27 +01:00
|
|
|
} else {
|
2019-02-13 14:05:20 +01:00
|
|
|
}
|
2019-02-13 19:24:53 +01:00
|
|
|
logger.Infof("run hook")
|
2019-02-13 04:32:06 +01:00
|
|
|
ok = true
|
|
|
|
}
|
|
|
|
if !ok {
|
|
|
|
logger.Warnf("no hook found")
|
|
|
|
http.Error(w, fmt.Sprintf("no configuration for %s for url: %s", hookType, request.RuleURL), http.StatusNotFound)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|