mirror of
https://gitee.com/rainbond/Rainbond.git
synced 2024-12-02 11:47:36 +08:00
[REV] Optimize how grctl adds rules
This commit is contained in:
parent
30713eb483
commit
db83fdebab
@ -25,6 +25,11 @@ import (
|
||||
"fmt"
|
||||
"encoding/json"
|
||||
"bytes"
|
||||
"os"
|
||||
"errors"
|
||||
"io/ioutil"
|
||||
"github.com/Sirupsen/logrus"
|
||||
"gopkg.in/yaml.v2"
|
||||
)
|
||||
|
||||
//ClusterInterface cluster api
|
||||
@ -32,8 +37,8 @@ type MonitorInterface interface {
|
||||
GetRule(name string) (*model.AlertingNameConfig, *util.APIHandleError)
|
||||
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)
|
||||
AddRule(path string) (*utilhttp.ResponseBody, *util.APIHandleError)
|
||||
RegRule(ruleName string, path string) (*utilhttp.ResponseBody, *util.APIHandleError)
|
||||
}
|
||||
|
||||
func (r *regionImpl) Monitor() MonitorInterface {
|
||||
@ -54,6 +59,7 @@ func (m *monitor) GetRule(name string) (*model.AlertingNameConfig, *util.APIHand
|
||||
return nil, handleErrAndCode(err, code)
|
||||
}
|
||||
if code != 200 {
|
||||
logrus.Error("Return failure message ", decode.Bean)
|
||||
return nil, util.CreateAPIHandleError(code, fmt.Errorf("get alerting rules error code %d", code))
|
||||
}
|
||||
return &ac, nil
|
||||
@ -68,6 +74,7 @@ func (m *monitor) GetAllRule() (*model.AlertingRulesConfig, *util.APIHandleError
|
||||
return nil, handleErrAndCode(err, code)
|
||||
}
|
||||
if code != 200 {
|
||||
logrus.Error("Return failure message ", decode.Bean)
|
||||
return nil, util.CreateAPIHandleError(code, fmt.Errorf("get alerting rules error code %d", code))
|
||||
}
|
||||
return &ac, nil
|
||||
@ -80,40 +87,75 @@ func (m *monitor) DelRule(name string) (*utilhttp.ResponseBody, *util.APIHandleE
|
||||
return nil, handleErrAndCode(err, code)
|
||||
}
|
||||
if code != 200 {
|
||||
logrus.Error("Return failure message ", decode.Bean)
|
||||
return nil, util.CreateAPIHandleError(code, fmt.Errorf("del alerting rules error code %d", code))
|
||||
}
|
||||
return &decode, nil
|
||||
}
|
||||
|
||||
func (m *monitor) AddRule(rules *model.AlertingNameConfig) (*utilhttp.ResponseBody, *util.APIHandleError) {
|
||||
func (m *monitor) AddRule(path string) (*utilhttp.ResponseBody, *util.APIHandleError) {
|
||||
_, err := os.Stat(path)
|
||||
if err!= nil || !os.IsExist(err){
|
||||
return nil, util.CreateAPIHandleError(400, errors.New("file does not exist"))
|
||||
}
|
||||
|
||||
content, err := ioutil.ReadFile(path)
|
||||
if err != nil {
|
||||
logrus.Error("Failed to read AlertingRules config file: ", err.Error())
|
||||
return nil, util.CreateAPIHandleError(400, err)
|
||||
}
|
||||
var rulesConfig model.AlertingNameConfig
|
||||
if err := yaml.Unmarshal(content, &rulesConfig); err != nil {
|
||||
logrus.Error("Unmarshal AlertingRulesConfig config string to object error.", err.Error())
|
||||
return nil, util.CreateAPIHandleError(400, err)
|
||||
|
||||
}
|
||||
var decode utilhttp.ResponseBody
|
||||
body, err := json.Marshal(rules)
|
||||
body, err := json.Marshal(rulesConfig)
|
||||
if err != nil {
|
||||
return nil, util.CreateAPIHandleError(400, err)
|
||||
}
|
||||
code, err := m.DoRequest(m.prefix, "POST", bytes.NewBuffer(body), nil)
|
||||
code, err := m.DoRequest(m.prefix, "POST", bytes.NewBuffer(body), &decode)
|
||||
if err != nil {
|
||||
println("====err>",code,err)
|
||||
return nil, handleErrAndCode(err, code)
|
||||
}
|
||||
if code != 200 {
|
||||
logrus.Error("Return failure message ", decode.Bean)
|
||||
return nil, util.CreateAPIHandleError(code, fmt.Errorf("add alerting rules error code %d", code))
|
||||
}
|
||||
return &decode, nil
|
||||
}
|
||||
|
||||
func (m *monitor) RegRule(ruleName string, rules *model.AlertingNameConfig) (*utilhttp.ResponseBody, *util.APIHandleError) {
|
||||
func (m *monitor) RegRule(ruleName string, path string) (*utilhttp.ResponseBody, *util.APIHandleError) {
|
||||
_, err := os.Stat(path)
|
||||
if err!= nil || !os.IsExist(err){
|
||||
return nil, util.CreateAPIHandleError(400, errors.New("file does not exist"))
|
||||
}
|
||||
|
||||
content, err := ioutil.ReadFile(path)
|
||||
if err != nil {
|
||||
logrus.Error("Failed to read AlertingRules config file: ", err.Error())
|
||||
return nil, util.CreateAPIHandleError(400, err)
|
||||
}
|
||||
var rulesConfig model.AlertingNameConfig
|
||||
if err := yaml.Unmarshal(content, &rulesConfig); err != nil {
|
||||
logrus.Error("Unmarshal AlertingRulesConfig config string to object error.", err.Error())
|
||||
return nil, util.CreateAPIHandleError(400, err)
|
||||
|
||||
}
|
||||
var decode utilhttp.ResponseBody
|
||||
body, err := json.Marshal(rules)
|
||||
body, err := json.Marshal(rulesConfig)
|
||||
if err != nil {
|
||||
return nil, util.CreateAPIHandleError(400, err)
|
||||
}
|
||||
code, err := m.DoRequest(m.prefix+"/"+ruleName, "PUT", bytes.NewBuffer(body), nil)
|
||||
code, err := m.DoRequest(m.prefix+"/"+ruleName, "PUT", bytes.NewBuffer(body), &decode)
|
||||
if err != nil {
|
||||
println("====err>",code,err)
|
||||
return nil, handleErrAndCode(err, code)
|
||||
}
|
||||
if code != 200 {
|
||||
logrus.Error("Return failure message ", decode.Bean)
|
||||
return nil, util.CreateAPIHandleError(code, fmt.Errorf("add alerting rules error code %d", code))
|
||||
}
|
||||
return &decode, nil
|
||||
|
@ -39,6 +39,7 @@ type Config struct {
|
||||
StartArgs []string
|
||||
ConfigFile string
|
||||
AlertingRulesFile string
|
||||
AlertManagerUrl string
|
||||
LocalStoragePath string
|
||||
Web Web
|
||||
Tsdb Tsdb
|
||||
@ -98,6 +99,7 @@ func NewConfig() *Config {
|
||||
|
||||
ConfigFile: "/etc/prometheus/prometheus.yml",
|
||||
AlertingRulesFile: "/etc/prometheus/rules.yml",
|
||||
AlertManagerUrl: "",
|
||||
LocalStoragePath: "/prometheusdata",
|
||||
WebTimeout: "5m",
|
||||
RemoteFlushDeadline: "1m",
|
||||
@ -130,6 +132,8 @@ func (c *Config) AddFlag(cmd *pflag.FlagSet) {
|
||||
func (c *Config) AddPrometheusFlag(cmd *pflag.FlagSet) {
|
||||
cmd.StringVar(&c.ConfigFile, "config.file", c.ConfigFile, "Prometheus configuration file path.")
|
||||
|
||||
cmd.StringVar(&c.AlertManagerUrl, "alertmanager.url", c.AlertManagerUrl, "AlertManager url.")
|
||||
|
||||
cmd.StringVar(&c.AlertingRulesFile, "rules-config.file", c.AlertingRulesFile, "Prometheus alerting rules config file path.")
|
||||
|
||||
cmd.StringVar(&c.Web.ListenAddress, "web.listen-address", c.Web.ListenAddress, "Address to listen on for UI, API, and telemetry.")
|
||||
@ -209,6 +213,9 @@ func (c *Config) CompleteConfig() {
|
||||
if c.Web.EnableLifecycle {
|
||||
defaultOptions += " --web.enable-lifecycle"
|
||||
}
|
||||
if c.AlertManagerUrl != "" {
|
||||
defaultOptions += " --alertmanager.url="+c.AlertManagerUrl
|
||||
}
|
||||
|
||||
args := strings.Split(defaultOptions, " ")
|
||||
c.StartArgs = append(c.StartArgs, os.Args[0])
|
||||
|
@ -6,7 +6,6 @@ import (
|
||||
"github.com/goodrain/rainbond/grctl/clients"
|
||||
"fmt"
|
||||
"github.com/ghodss/yaml"
|
||||
"github.com/goodrain/rainbond/node/api/model"
|
||||
"errors"
|
||||
)
|
||||
|
||||
@ -63,27 +62,19 @@ func NewCmdAlerting() cli.Command {
|
||||
},
|
||||
{
|
||||
Name: "add",
|
||||
Usage: "add 添加规则",
|
||||
Flags: []cli.Flag{
|
||||
cli.StringFlag{
|
||||
Name: "Rules,r",
|
||||
Value: "",
|
||||
Usage: "Rules",
|
||||
},
|
||||
},
|
||||
Usage: "add FilePath",
|
||||
Action: func(c *cli.Context) error {
|
||||
Common(c)
|
||||
if c.IsSet("Rules") {
|
||||
rules := c.String("Rules")
|
||||
|
||||
var rulesConfig model.AlertingNameConfig
|
||||
yaml.Unmarshal([]byte(rules), &rulesConfig)
|
||||
_, err := clients.RegionClient.Monitor().AddRule(&rulesConfig)
|
||||
handleErr(err)
|
||||
fmt.Println("Add rule successfully")
|
||||
filePath := c.Args().First()
|
||||
if filePath == "" {
|
||||
logrus.Errorf("need args")
|
||||
return nil
|
||||
}
|
||||
return errors.New("rules not null")
|
||||
_, err := clients.RegionClient.Monitor().AddRule(filePath)
|
||||
handleErr(err)
|
||||
fmt.Println("Add rule successfully")
|
||||
return nil
|
||||
|
||||
},
|
||||
},
|
||||
{
|
||||
@ -96,19 +87,17 @@ func NewCmdAlerting() cli.Command {
|
||||
Usage: "RulesName",
|
||||
},
|
||||
cli.StringFlag{
|
||||
Name: "Rules,r",
|
||||
Name: "RulesPath,rp",
|
||||
Value: "",
|
||||
Usage: "Rules",
|
||||
Usage: "RulesPath",
|
||||
},
|
||||
},
|
||||
Action: func(c *cli.Context) error {
|
||||
Common(c)
|
||||
if c.IsSet("RulesName") && c.IsSet("Rules") {
|
||||
rules := c.String("Rules")
|
||||
if c.IsSet("RulesName") && c.IsSet("RulesPath") {
|
||||
path := c.String("RulesPath")
|
||||
ruleName := c.String("RulesName")
|
||||
var rulesConfig model.AlertingNameConfig
|
||||
yaml.Unmarshal([]byte(rules), &rulesConfig)
|
||||
_, err := clients.RegionClient.Monitor().RegRule(ruleName, &rulesConfig)
|
||||
_, err := clients.RegionClient.Monitor().RegRule(ruleName, path)
|
||||
handleErr(err)
|
||||
fmt.Println("Modify rule successfully")
|
||||
return nil
|
||||
|
Loading…
Reference in New Issue
Block a user