Rainbond/grctl/cmd/monitor.go

129 lines
2.9 KiB
Go
Raw Normal View History

package cmd
import (
"github.com/urfave/cli"
"github.com/Sirupsen/logrus"
"github.com/goodrain/rainbond/grctl/clients"
"fmt"
"github.com/ghodss/yaml"
2018-08-01 15:40:08 +08:00
"encoding/json"
"github.com/goodrain/rainbond/node/api/model"
2018-08-01 16:22:47 +08:00
"errors"
)
//NewCmdNode NewCmdNode
func NewCmdAlerting() cli.Command {
c := cli.Command{
Name: "alerting",
Usage: "监控报警。grctl alerting",
Subcommands: []cli.Command{
{
Name: "get",
Usage: "get rule_name",
Action: func(c *cli.Context) error {
Common(c)
name := c.Args().First()
if name == "" {
logrus.Errorf("need args")
return nil
}
v, err := clients.RegionClient.Monitor().GetRule(name)
handleErr(err)
rule, _ := yaml.Marshal(v)
2018-08-01 13:17:42 +08:00
fmt.Println(string(rule))
return nil
},
},
2018-08-01 15:40:08 +08:00
{
Name: "list",
Usage: "list",
Action: func(c *cli.Context) error {
Common(c)
list, err := clients.RegionClient.Monitor().GetAllRule()
handleErr(err)
ruleList, _ := yaml.Marshal(list)
fmt.Println(string(ruleList))
return nil
},
},
{
Name: "del",
Usage: "del rule_name",
Action: func(c *cli.Context) error {
Common(c)
name := c.Args().First()
if name == "" {
logrus.Errorf("need args")
return nil
}
v, err := clients.RegionClient.Monitor().DelRule(name)
handleErr(err)
result, _ := json.Marshal(v.Bean)
fmt.Println(string(result))
return nil
},
},
{
Name: "add",
2018-08-01 16:22:47 +08:00
Usage: "add 添加规则",
Flags: []cli.Flag{
cli.StringFlag{
Name: "Rules,r",
Value: "",
Usage: "Rules",
},
},
2018-08-01 15:40:08 +08:00
Action: func(c *cli.Context) error {
Common(c)
2018-08-01 16:22:47 +08:00
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))
2018-08-01 15:40:08 +08:00
return nil
}
2018-08-01 16:22:47 +08:00
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")
2018-08-01 15:40:08 +08:00
},
},
},
}
return c
}