cli for checking alerts

This commit is contained in:
Łukasz Magiera
2021-08-26 15:45:17 +02:00
parent b094e0913d
commit e2ba650a8c
10 changed files with 145 additions and 2 deletions
+51
View File
@@ -2,7 +2,9 @@ package cli
import (
"fmt"
"time"
"github.com/fatih/color"
"github.com/urfave/cli/v2"
"golang.org/x/xerrors"
)
@@ -13,6 +15,7 @@ var LogCmd = &cli.Command{
Subcommands: []*cli.Command{
LogList,
LogSetLevel,
LogAlerts,
},
}
@@ -100,3 +103,51 @@ var LogSetLevel = &cli.Command{
return nil
},
}
var LogAlerts = &cli.Command{
Name: "alerts",
Usage: "Get alert states",
Flags: []cli.Flag{
&cli.BoolFlag{
Name: "all",
Usage: "get all (active and inactive) alerts",
},
},
Action: func(cctx *cli.Context) error {
api, closer, err := GetAPI(cctx)
if err != nil {
return err
}
defer closer()
ctx := ReqContext(cctx)
alerts, err := api.LogAlerts(ctx)
if err != nil {
return xerrors.Errorf("getting alerts: %w", err)
}
all := cctx.Bool("all")
for _, alert := range alerts {
if !all && !alert.Active {
continue
}
active := color.RedString("active ")
if !alert.Active {
active = color.GreenString("inactive")
}
fmt.Printf("%s %s:%s\n", active, alert.Type.System, alert.Type.Subsystem)
if alert.LastResolved != nil {
fmt.Printf(" last resolved at %s; reason: %s\n", alert.LastResolved.Time.Truncate(time.Millisecond), alert.LastResolved.Message)
}
if alert.LastActive != nil {
fmt.Printf(" %s %s; reason: %s\n", color.YellowString("last raised at"), alert.LastActive.Time.Truncate(time.Millisecond), alert.LastActive.Message)
}
}
return nil
},
}