[ADD] add grctl modify rules

This commit is contained in:
zhoujunhao 2018-08-01 16:22:47 +08:00
parent 379281e8de
commit 965ded02a8
4 changed files with 107 additions and 39 deletions

View File

@ -33,6 +33,7 @@ type MonitorInterface interface {
GetAllRule() (*model.AlertingRulesConfig, *util.APIHandleError)
DelRule(name string) (*utilhttp.ResponseBody, *util.APIHandleError)
AddRule(rules *model.AlertingNameConfig) (*utilhttp.ResponseBody, *util.APIHandleError)
RegRule(ruleName string, rules *model.AlertingNameConfig) (*utilhttp.ResponseBody, *util.APIHandleError)
}
func (r *regionImpl) Monitor() MonitorInterface {
@ -100,3 +101,20 @@ func (m *monitor) AddRule(rules *model.AlertingNameConfig) (*utilhttp.ResponseBo
}
return &decode, nil
}
func (m *monitor) RegRule(ruleName string, rules *model.AlertingNameConfig) (*utilhttp.ResponseBody, *util.APIHandleError) {
var decode utilhttp.ResponseBody
body, err := json.Marshal(rules)
if err != nil {
return nil, util.CreateAPIHandleError(400, err)
}
code, err := m.DoRequest(m.prefix+"/"+ruleName, "PUT", bytes.NewBuffer(body), nil)
if err != nil {
println("====err>",code,err)
return nil, handleErrAndCode(err, code)
}
if code != 200 {
return nil, util.CreateAPIHandleError(code, fmt.Errorf("add alerting rules error code %d", code))
}
return &decode, nil
}

View File

@ -8,6 +8,7 @@ import (
"github.com/ghodss/yaml"
"encoding/json"
"github.com/goodrain/rainbond/node/api/model"
"errors"
)
//NewCmdNode NewCmdNode
@ -64,22 +65,61 @@ func NewCmdAlerting() cli.Command {
},
{
Name: "add",
Usage: "add rules",
Usage: "add 添加规则",
Flags: []cli.Flag{
cli.StringFlag{
Name: "Rules,r",
Value: "",
Usage: "Rules",
},
},
Action: func(c *cli.Context) error {
Common(c)
rules := c.Args().First()
if rules == "" {
logrus.Errorf("need args")
if c.IsSet("Rules") {
rules := c.String("Rules")
println("====>", rules)
var rulesConfig model.AlertingNameConfig
yaml.Unmarshal([]byte(rules), &rulesConfig)
v, err := clients.RegionClient.Monitor().AddRule(&rulesConfig)
handleErr(err)
result, _ := json.Marshal(v.Bean)
fmt.Println(string(result))
return nil
}
println("====>", rules)
var rulesConfig model.AlertingNameConfig
yaml.Unmarshal([]byte(rules), &rulesConfig)
v, err := clients.RegionClient.Monitor().AddRule(&rulesConfig)
handleErr(err)
result, _ := json.Marshal(v.Bean)
fmt.Println(string(result))
return nil
return errors.New("rules not null")
},
},
{
Name: "modify",
Usage: "modify 修改规则",
Flags: []cli.Flag{
cli.StringFlag{
Name: "RulesName,rn",
Value: "",
Usage: "RulesName",
},
cli.StringFlag{
Name: "Rules,r",
Value: "",
Usage: "Rules",
},
},
Action: func(c *cli.Context) error {
Common(c)
if c.IsSet("RulesName") && c.IsSet("Rules") {
rules := c.String("Rules")
ruleName := c.String("RulesName")
println("====>", rules)
var rulesConfig model.AlertingNameConfig
yaml.Unmarshal([]byte(rules), &rulesConfig)
v, err := clients.RegionClient.Monitor().RegRule(ruleName, &rulesConfig)
handleErr(err)
result, _ := json.Marshal(v.Bean)
fmt.Println(string(result))
return nil
}
return errors.New("rule name or rules not null")
},
},
},

View File

@ -6,9 +6,9 @@ import (
httputil "github.com/goodrain/rainbond/util/http"
"github.com/Sirupsen/logrus"
"gopkg.in/yaml.v2"
"github.com/goodrain/rainbond/monitor/prometheus"
"github.com/go-chi/chi"
"encoding/json"
)
type ControllerManager struct {
@ -32,11 +32,15 @@ func (c *ControllerManager) AddRules(w http.ResponseWriter, r *http.Request) {
}
println(string(in))
var RulesConfig prometheus.AlertingNameConfig
if ok := httputil.ValidatorRequestStructAndErrorResponse(r, w, &RulesConfig, nil); !ok {
logrus.Info("参数错误")
unmarshalErr := json.Unmarshal(in, &RulesConfig)
if unmarshalErr != nil{
logrus.Info("反序列化错误",unmarshalErr)
httputil.ReturnError(r, w, 400, err.Error())
return
}
//err = ioutil.WriteFile("/etc/prometheus/cache_rule.yml", in, 0644)
//if err != nil {
// logrus.Error(err.Error())
@ -56,8 +60,8 @@ func (c *ControllerManager) AddRules(w http.ResponseWriter, r *http.Request) {
c.Rules.RulesConfig.LoadAlertingRulesConfig()
group := c.Rules.RulesConfig.Groups
for _,v := range group{
if v.Name == RulesConfig.Name{
for _, v := range group {
if v.Name == RulesConfig.Name {
httputil.ReturnError(r, w, 400, "Rule already exists")
return
}
@ -82,7 +86,7 @@ func (c *ControllerManager) GetRules(w http.ResponseWriter, r *http.Request) {
}
}
httputil.ReturnError(r, w, 400, "Rule does not exist")
httputil.ReturnError(r, w, 404, "Rule does not exist")
}
func (c *ControllerManager) DelRules(w http.ResponseWriter, r *http.Request) {
@ -92,7 +96,7 @@ func (c *ControllerManager) DelRules(w http.ResponseWriter, r *http.Request) {
groupsList := c.Rules.RulesConfig.Groups
for i, v := range groupsList {
if v.Name == rulesName {
groupsList = append(groupsList[:i],groupsList[i+1:]...)
groupsList = append(groupsList[:i], groupsList[i+1:]...)
c.Rules.RulesConfig.SaveAlertingRulesConfig()
httputil.ReturnSuccess(r, w, "successfully deleted")
return
@ -101,8 +105,8 @@ func (c *ControllerManager) DelRules(w http.ResponseWriter, r *http.Request) {
httputil.ReturnSuccess(r, w, "")
}
func (c *ControllerManager) RegRules(w http.ResponseWriter, r *http.Request) {
rulesName := chi.URLParam(r, "rules_name")
in, err := ioutil.ReadAll(r.Body)
if err != nil {
httputil.ReturnError(r, w, 400, err.Error())
@ -111,40 +115,46 @@ func (c *ControllerManager) RegRules(w http.ResponseWriter, r *http.Request) {
var RulesConfig prometheus.AlertingNameConfig
err = ioutil.WriteFile("/etc/prometheus/cache_rule.yml", in, 0644)
if err != nil {
logrus.Error(err.Error())
}
content, err := ioutil.ReadFile("/etc/prometheus/cache_rule.yml")
if err != nil {
logrus.Error( err)
}
if err := yaml.Unmarshal(content, &RulesConfig); err != nil {
logrus.Error("Unmarshal prometheus alerting rules config string to object error.", err.Error())
unmarshalErr := json.Unmarshal(in, &RulesConfig)
if unmarshalErr != nil{
logrus.Info("反序列化错误",unmarshalErr)
httputil.ReturnError(r, w, 400, err.Error())
return
}
//err = ioutil.WriteFile("/etc/prometheus/cache_rule.yml", in, 0644)
//if err != nil {
// logrus.Error(err.Error())
//}
//
//content, err := ioutil.ReadFile("/etc/prometheus/cache_rule.yml")
//if err != nil {
// logrus.Error(err)
//
//}
//
//if err := yaml.Unmarshal(content, &RulesConfig); err != nil {
// logrus.Error("Unmarshal prometheus alerting rules config string to object error.", err.Error())
// httputil.ReturnError(r, w, 400, err.Error())
// return
//}
c.Rules.RulesConfig.LoadAlertingRulesConfig()
group := c.Rules.RulesConfig.Groups
for i,v := range group{
if v.Name == RulesConfig.Name{
for i, v := range group {
if v.Name == rulesName {
group[i] = &RulesConfig
httputil.ReturnSuccess(r, w, "Update rule succeeded")
c.Rules.RulesConfig.SaveAlertingRulesConfig()
return
}
}
httputil.ReturnError(r, w, 400,"The rule to be updated does not exist")
httputil.ReturnError(r, w, 404, "The rule to be updated does not exist")
}
func (c *ControllerManager) GetAllRules(w http.ResponseWriter, r *http.Request) {
logrus.Infof("get all rule")
c.Rules.RulesConfig.LoadAlertingRulesConfig()
val := c.Rules.RulesConfig
httputil.ReturnSuccess(r, w, val)
}
}

View File

@ -38,7 +38,7 @@ func APIServer(c *controller.ControllerManager) *chi.Mux {
})
r.Route("/v2/rules", func(r chi.Router) {
r.Post("/", c.AddRules)
r.Put("/", c.RegRules)
r.Put("/{rules_name}", c.RegRules)
r.Delete("/{rules_name}", c.DelRules)
r.Get("/{rules_name}", c.GetRules)
r.Get("/all", c.GetAllRules)