Rainbond/monitor/api/controller/rules.go

63 lines
1.5 KiB
Go
Raw Normal View History

2018-07-31 14:26:47 +08:00
package controller
import (
"net/http"
"io/ioutil"
httputil "github.com/goodrain/rainbond/util/http"
"github.com/Sirupsen/logrus"
"gopkg.in/yaml.v2"
"github.com/goodrain/rainbond/monitor/prometheus"
2018-07-31 16:09:46 +08:00
"github.com/go-chi/chi"
2018-07-31 14:26:47 +08:00
)
2018-07-31 16:09:46 +08:00
type ControllerManager struct {
Rules *prometheus.AlertingRulesManager
Manager *prometheus.Manager
}
func NewControllerManager(a *prometheus.AlertingRulesManager, p *prometheus.Manager) *ControllerManager {
c := &ControllerManager{
Rules: a,
Manager: p,
}
return c
}
func (c *ControllerManager) AddRules(w http.ResponseWriter, r *http.Request) {
2018-07-31 14:26:47 +08:00
in, err := ioutil.ReadAll(r.Body)
if err != nil {
httputil.ReturnError(r, w, 400, err.Error())
return
}
var RulesConfig prometheus.AlertingNameConfig
if err := yaml.Unmarshal(in, &RulesConfig); err != nil {
logrus.Error("Unmarshal prometheus alerting rules config string to object error.", err.Error())
httputil.ReturnError(r, w, 400, err.Error())
return
}
2018-07-31 16:09:46 +08:00
c.Rules.RulesConfig.LoadAlertingRulesConfig()
c.Rules.RulesConfig.AddRules(RulesConfig)
c.Rules.RulesConfig.SaveAlertingRulesConfig()
c.Manager.RestartDaemon()
2018-07-31 14:26:47 +08:00
}
2018-07-31 16:09:46 +08:00
func (c *ControllerManager) GetRules(w http.ResponseWriter, r *http.Request) {
rulesName := chi.URLParam(r, "rules_name")
c.Rules.RulesConfig.LoadAlertingRulesConfig()
for _, v := range c.Rules.RulesConfig.Groups {
if v.Name == rulesName {
res := v.Rules
httputil.ReturnSuccess(r, w, res)
}
}
httputil.ReturnSuccess(r, w, "Did not find the rule")
}