diff --git a/api/api_common.go b/api/api_common.go index 1a83dc25e..48d167b59 100644 --- a/api/api_common.go +++ b/api/api_common.go @@ -36,7 +36,7 @@ type Common interface { // LogAlerts returns list of all, active and inactive alerts tracked by the // node - LogAlerts(ctx context.Context) ([]alerting.Alert, error) + LogAlerts(ctx context.Context) ([]alerting.Alert, error) //perm:admin // MethodGroup: Common diff --git a/api/mocks/mock_full.go b/api/mocks/mock_full.go index a6e0e9e91..7e3818401 100644 --- a/api/mocks/mock_full.go +++ b/api/mocks/mock_full.go @@ -24,6 +24,7 @@ import ( apitypes "github.com/filecoin-project/lotus/api/types" miner "github.com/filecoin-project/lotus/chain/actors/builtin/miner" types "github.com/filecoin-project/lotus/chain/types" + alerting "github.com/filecoin-project/lotus/journal/alerting" marketevents "github.com/filecoin-project/lotus/markets/loggers" dtypes "github.com/filecoin-project/lotus/node/modules/dtypes" imports "github.com/filecoin-project/lotus/node/repo/imports" @@ -995,6 +996,21 @@ func (mr *MockFullNodeMockRecorder) ID(arg0 interface{}) *gomock.Call { return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "ID", reflect.TypeOf((*MockFullNode)(nil).ID), arg0) } +// LogAlerts mocks base method. +func (m *MockFullNode) LogAlerts(arg0 context.Context) ([]alerting.Alert, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "LogAlerts", arg0) + ret0, _ := ret[0].([]alerting.Alert) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// LogAlerts indicates an expected call of LogAlerts. +func (mr *MockFullNodeMockRecorder) LogAlerts(arg0 interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "LogAlerts", reflect.TypeOf((*MockFullNode)(nil).LogAlerts), arg0) +} + // LogList mocks base method. func (m *MockFullNode) LogList(arg0 context.Context) ([]string, error) { m.ctrl.T.Helper() diff --git a/api/proxy_gen.go b/api/proxy_gen.go index db3e668a2..f2dc7c560 100644 --- a/api/proxy_gen.go +++ b/api/proxy_gen.go @@ -64,7 +64,7 @@ type CommonStruct struct { Discover func(p0 context.Context) (apitypes.OpenRPCDocument, error) `perm:"read"` - LogAlerts func(p0 context.Context) ([]alerting.Alert, error) `` + LogAlerts func(p0 context.Context) ([]alerting.Alert, error) `perm:"admin"` LogList func(p0 context.Context) ([]string, error) `perm:"write"` diff --git a/api/v0api/v0mocks/mock_full.go b/api/v0api/v0mocks/mock_full.go index ae717e1ec..97ca4e7fe 100644 --- a/api/v0api/v0mocks/mock_full.go +++ b/api/v0api/v0mocks/mock_full.go @@ -23,6 +23,7 @@ import ( apitypes "github.com/filecoin-project/lotus/api/types" miner "github.com/filecoin-project/lotus/chain/actors/builtin/miner" types "github.com/filecoin-project/lotus/chain/types" + alerting "github.com/filecoin-project/lotus/journal/alerting" marketevents "github.com/filecoin-project/lotus/markets/loggers" dtypes "github.com/filecoin-project/lotus/node/modules/dtypes" imports "github.com/filecoin-project/lotus/node/repo/imports" @@ -950,6 +951,21 @@ func (mr *MockFullNodeMockRecorder) ID(arg0 interface{}) *gomock.Call { return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "ID", reflect.TypeOf((*MockFullNode)(nil).ID), arg0) } +// LogAlerts mocks base method. +func (m *MockFullNode) LogAlerts(arg0 context.Context) ([]alerting.Alert, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "LogAlerts", arg0) + ret0, _ := ret[0].([]alerting.Alert) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// LogAlerts indicates an expected call of LogAlerts. +func (mr *MockFullNodeMockRecorder) LogAlerts(arg0 interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "LogAlerts", reflect.TypeOf((*MockFullNode)(nil).LogAlerts), arg0) +} + // LogList mocks base method. func (m *MockFullNode) LogList(arg0 context.Context) ([]string, error) { m.ctrl.T.Helper() diff --git a/cli/log.go b/cli/log.go index 4ab6aa748..7b223aa17 100644 --- a/cli/log.go +++ b/cli/log.go @@ -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 + }, +} diff --git a/documentation/en/api-v0-methods-miner.md b/documentation/en/api-v0-methods-miner.md index b02ac6c0d..1c6891329 100644 --- a/documentation/en/api-v0-methods-miner.md +++ b/documentation/en/api-v0-methods-miner.md @@ -44,6 +44,7 @@ * [I](#I) * [ID](#ID) * [Log](#Log) + * [LogAlerts](#LogAlerts) * [LogList](#LogList) * [LogSetLevel](#LogSetLevel) * [Market](#Market) @@ -664,6 +665,15 @@ Response: `"12D3KooWGzxzKZYveHXtpG6AsrUJBcWxHBFS2HsEoGTxrMLvKXtf"` ## Log +### LogAlerts + + +Perms: admin + +Inputs: `null` + +Response: `null` + ### LogList diff --git a/documentation/en/api-v0-methods.md b/documentation/en/api-v0-methods.md index 6f030e979..f5907f494 100644 --- a/documentation/en/api-v0-methods.md +++ b/documentation/en/api-v0-methods.md @@ -71,6 +71,7 @@ * [I](#I) * [ID](#ID) * [Log](#Log) + * [LogAlerts](#LogAlerts) * [LogList](#LogList) * [LogSetLevel](#LogSetLevel) * [Market](#Market) @@ -1816,6 +1817,15 @@ Response: `"12D3KooWGzxzKZYveHXtpG6AsrUJBcWxHBFS2HsEoGTxrMLvKXtf"` ## Log +### LogAlerts + + +Perms: admin + +Inputs: `null` + +Response: `null` + ### LogList diff --git a/documentation/en/api-v1-unstable-methods.md b/documentation/en/api-v1-unstable-methods.md index 72a9becba..e77e0c7bf 100644 --- a/documentation/en/api-v1-unstable-methods.md +++ b/documentation/en/api-v1-unstable-methods.md @@ -74,6 +74,7 @@ * [I](#I) * [ID](#ID) * [Log](#Log) + * [LogAlerts](#LogAlerts) * [LogList](#LogList) * [LogSetLevel](#LogSetLevel) * [Market](#Market) @@ -1880,6 +1881,15 @@ Response: `"12D3KooWGzxzKZYveHXtpG6AsrUJBcWxHBFS2HsEoGTxrMLvKXtf"` ## Log +### LogAlerts + + +Perms: admin + +Inputs: `null` + +Response: `null` + ### LogList diff --git a/documentation/en/cli-lotus-miner.md b/documentation/en/cli-lotus-miner.md index d93efa903..f69c8b144 100644 --- a/documentation/en/cli-lotus-miner.md +++ b/documentation/en/cli-lotus-miner.md @@ -506,6 +506,7 @@ USAGE: COMMANDS: list List log systems set-level Set log level + alerts Get alert states help, h Shows a list of commands or help for one command OPTIONS: @@ -561,6 +562,20 @@ OPTIONS: ``` +### lotus-miner log alerts +``` +NAME: + lotus-miner log alerts - Get alert states + +USAGE: + lotus-miner log alerts [command options] [arguments...] + +OPTIONS: + --all get all (active and inactive) alerts (default: false) + --help, -h show help (default: false) + +``` + ## lotus-miner wait-api ``` NAME: diff --git a/documentation/en/cli-lotus.md b/documentation/en/cli-lotus.md index 855745348..0b354777e 100644 --- a/documentation/en/cli-lotus.md +++ b/documentation/en/cli-lotus.md @@ -2366,6 +2366,7 @@ USAGE: COMMANDS: list List log systems set-level Set log level + alerts Get alert states help, h Shows a list of commands or help for one command OPTIONS: @@ -2421,6 +2422,20 @@ OPTIONS: ``` +### lotus log alerts +``` +NAME: + lotus log alerts - Get alert states + +USAGE: + lotus log alerts [command options] [arguments...] + +OPTIONS: + --all get all (active and inactive) alerts (default: false) + --help, -h show help (default: false) + +``` + ## lotus wait-api ``` NAME: