Rainbond/grctl/cmd/monitor.go

112 lines
2.4 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 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
}
2018-08-01 18:49:31 +08:00
_, err := clients.RegionClient.Monitor().DelRule(name)
2018-08-01 15:40:08 +08:00
handleErr(err)
2018-08-01 18:49:31 +08:00
fmt.Println("Delete rule succeeded")
2018-08-01 15:40:08 +08:00
return nil
},
},
{
Name: "add",
2018-08-02 12:08:15 +08:00
Usage: "add FilePath",
2018-08-01 15:40:08 +08:00
Action: func(c *cli.Context) error {
Common(c)
2018-08-02 12:08:15 +08:00
filePath := c.Args().First()
if filePath == "" {
logrus.Errorf("need args")
2018-08-01 15:40:08 +08:00
return nil
}
2018-08-02 12:08:15 +08:00
_, err := clients.RegionClient.Monitor().AddRule(filePath)
handleErr(err)
fmt.Println("Add rule successfully")
return nil
2018-08-01 16:22:47 +08:00
},
},
{
Name: "modify",
Usage: "modify 修改规则",
Flags: []cli.Flag{
cli.StringFlag{
Name: "RulesName,rn",
Value: "",
Usage: "RulesName",
},
cli.StringFlag{
2018-08-02 12:08:15 +08:00
Name: "RulesPath,rp",
2018-08-01 16:22:47 +08:00
Value: "",
2018-08-02 12:08:15 +08:00
Usage: "RulesPath",
2018-08-01 16:22:47 +08:00
},
},
Action: func(c *cli.Context) error {
Common(c)
2018-08-02 12:08:15 +08:00
if c.IsSet("RulesName") && c.IsSet("RulesPath") {
path := c.String("RulesPath")
2018-08-01 16:22:47 +08:00
ruleName := c.String("RulesName")
2018-08-02 12:08:15 +08:00
_, err := clients.RegionClient.Monitor().RegRule(ruleName, path)
2018-08-01 16:22:47 +08:00
handleErr(err)
2018-08-01 18:49:31 +08:00
fmt.Println("Modify rule successfully")
2018-08-01 16:22:47 +08:00
return nil
}
return errors.New("rule name or rules not null")
2018-08-01 15:40:08 +08:00
},
},
},
}
return c
}