Rainbond/grctl/cmd/monitor.go
2019-02-21 21:55:56 +08:00

113 lines
2.4 KiB
Go

package cmd
import (
"errors"
"fmt"
"github.com/Sirupsen/logrus"
"github.com/ghodss/yaml"
"github.com/goodrain/rainbond/grctl/clients"
"github.com/urfave/cli"
)
//NewCmdAlerting new altering rule manage cmd
func NewCmdAlerting() cli.Command {
c := cli.Command{
Name: "alerting",
Usage: "alerting rule manage",
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)
fmt.Println(string(rule))
return nil
},
},
{
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
}
_, err := clients.RegionClient.Monitor().DelRule(name)
handleErr(err)
fmt.Println("Delete rule succeeded")
return nil
},
},
{
Name: "add",
Usage: "add FilePath",
Action: func(c *cli.Context) error {
Common(c)
filePath := c.Args().First()
if filePath == "" {
logrus.Errorf("need args")
return nil
}
_, err := clients.RegionClient.Monitor().AddRule(filePath)
handleErr(err)
fmt.Println("Add rule successfully")
return nil
},
},
{
Name: "modify",
Usage: "modify 修改规则",
Flags: []cli.Flag{
cli.StringFlag{
Name: "RulesName,rn",
Value: "",
Usage: "RulesName",
},
cli.StringFlag{
Name: "RulesPath,rp",
Value: "",
Usage: "RulesPath",
},
},
Action: func(c *cli.Context) error {
Common(c)
if c.IsSet("RulesName") && c.IsSet("RulesPath") {
path := c.String("RulesPath")
ruleName := c.String("RulesName")
_, err := clients.RegionClient.Monitor().RegRule(ruleName, path)
handleErr(err)
fmt.Println("Modify rule successfully")
return nil
}
return errors.New("rule name or rules not null")
},
},
},
}
return c
}