2018-08-07 14:28:15 +08:00
|
|
|
|
package cmd
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"fmt"
|
2018-08-07 19:12:44 +08:00
|
|
|
|
"strconv"
|
2018-09-02 23:17:56 +08:00
|
|
|
|
"time"
|
|
|
|
|
|
|
|
|
|
"github.com/apcera/termtables"
|
|
|
|
|
"github.com/goodrain/rainbond/grctl/clients"
|
|
|
|
|
"github.com/urfave/cli"
|
2018-08-07 14:28:15 +08:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
//NewCmdNode NewCmdNode
|
|
|
|
|
func NewCmdNotificationEvent() cli.Command {
|
|
|
|
|
c := cli.Command{
|
2018-09-25 19:49:40 +08:00
|
|
|
|
Name: "msg",
|
|
|
|
|
Usage: "应用异常通知事件。grctl msg",
|
2018-08-07 14:28:15 +08:00
|
|
|
|
Subcommands: []cli.Command{
|
|
|
|
|
{
|
|
|
|
|
Name: "get",
|
2018-08-30 18:43:13 +08:00
|
|
|
|
Usage: "获取未处理事件,不指定起止时间默认72小时内",
|
2018-08-07 14:28:15 +08:00
|
|
|
|
Flags: []cli.Flag{
|
|
|
|
|
cli.StringFlag{
|
|
|
|
|
Name: "StartTime,st",
|
|
|
|
|
Value: "",
|
|
|
|
|
Usage: "StartTime timestamp",
|
|
|
|
|
},
|
|
|
|
|
cli.StringFlag{
|
|
|
|
|
Name: "EndTime,et",
|
|
|
|
|
Value: "",
|
|
|
|
|
Usage: "EndTime timestamp",
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
Action: func(c *cli.Context) error {
|
|
|
|
|
Common(c)
|
2018-08-09 10:48:09 +08:00
|
|
|
|
startTime := c.String("StartTime")
|
|
|
|
|
EndTme := c.String("EndTime")
|
2018-09-02 23:17:56 +08:00
|
|
|
|
if startTime == "" && EndTme == "" {
|
2018-08-09 10:48:09 +08:00
|
|
|
|
NowTime := time.Now()
|
2018-09-02 23:17:56 +08:00
|
|
|
|
startTimeTimestamp := NowTime.AddDate(0, 0, -3).Unix()
|
2018-08-09 10:48:09 +08:00
|
|
|
|
startTime = strconv.FormatInt(startTimeTimestamp, 10)
|
|
|
|
|
EndTme = strconv.FormatInt(NowTime.Unix(), 10)
|
|
|
|
|
} else if EndTme == "" && startTime != "" {
|
|
|
|
|
NowTime := time.Now().Unix()
|
|
|
|
|
EndTme = strconv.FormatInt(NowTime, 10)
|
2018-08-07 14:28:15 +08:00
|
|
|
|
}
|
2018-08-09 10:48:09 +08:00
|
|
|
|
val, err := clients.RegionClient.Notification().GetNotification(startTime, EndTme)
|
|
|
|
|
handleErr(err)
|
|
|
|
|
serviceTable := termtables.CreateTable()
|
2018-09-25 19:49:40 +08:00
|
|
|
|
serviceTable.AddHeaders("TenantName(租户别名)/ServiceName(应用别名)", "Message(异常信息)", "Reason(异常原因)", "Count(出现次数)", "LastTime(最后一次异常时间)", "FirstTime(第一次异常时间)")
|
2018-08-09 10:48:09 +08:00
|
|
|
|
for _, v := range val {
|
2018-09-02 23:17:56 +08:00
|
|
|
|
if v.KindID == "" || v.ServiceName == "" || v.TenantName == "" {
|
2018-08-10 10:49:50 +08:00
|
|
|
|
continue
|
|
|
|
|
}
|
2018-09-25 19:49:40 +08:00
|
|
|
|
serviceTable.AddRow(v.TenantName+"/"+v.ServiceName, v.Message, v.Reason, v.Count, v.LastTime, v.FirstTime)
|
2018-08-09 10:48:09 +08:00
|
|
|
|
}
|
|
|
|
|
fmt.Println(serviceTable.Render())
|
2018-08-08 13:07:49 +08:00
|
|
|
|
return nil
|
2018-08-07 14:28:15 +08:00
|
|
|
|
},
|
|
|
|
|
},
|
2018-08-30 17:49:54 +08:00
|
|
|
|
{
|
|
|
|
|
Name: "handle",
|
|
|
|
|
Usage: "handle --help",
|
|
|
|
|
Flags: []cli.Flag{
|
|
|
|
|
cli.StringFlag{
|
|
|
|
|
Name: "ServiceName,n",
|
|
|
|
|
Value: "",
|
|
|
|
|
Usage: "ServiceName",
|
|
|
|
|
},
|
|
|
|
|
cli.StringFlag{
|
|
|
|
|
Name: "HandleMessage,m",
|
|
|
|
|
Value: "",
|
|
|
|
|
Usage: "HandleMessage",
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
Action: func(c *cli.Context) error {
|
|
|
|
|
Common(c)
|
|
|
|
|
if !c.IsSet("ServiceName") {
|
|
|
|
|
println("ServiceName must not null")
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
serviceName := c.String("ServiceName")
|
|
|
|
|
handleMessage := c.String("HandleMessage")
|
2018-09-02 23:17:56 +08:00
|
|
|
|
_, err := clients.RegionClient.Notification().HandleNotification(serviceName, handleMessage)
|
2018-08-30 17:49:54 +08:00
|
|
|
|
handleErr(err)
|
|
|
|
|
fmt.Println("Handling successfully")
|
|
|
|
|
return nil
|
|
|
|
|
},
|
|
|
|
|
},
|
2018-08-07 14:28:15 +08:00
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
return c
|
|
|
|
|
}
|