From 942be9d99333e54cd9cce5debb7d24568b39931e Mon Sep 17 00:00:00 2001 From: stuart nelson Date: Wed, 9 May 2018 10:57:01 +0200 Subject: [PATCH] cli alert query: Expose --active and --unprocessed (#1370) * cli alert query: Expose --active and --unprocessed Support the new filter options in the alerts api endpoint introduced by https://github.com/prometheus/alertmanager/pull/1366 Signed-off-by: stuart nelson * Update comment and client_test Signed-off-by: stuart nelson --- cli/alert.go | 17 ++++++++++++++--- client/client.go | 6 ++++-- client/client_test.go | 2 +- 3 files changed, 19 insertions(+), 6 deletions(-) diff --git a/cli/alert.go b/cli/alert.go index 625220be..45483f35 100644 --- a/cli/alert.go +++ b/cli/alert.go @@ -15,8 +15,9 @@ import ( ) type alertQueryCmd struct { - inhibited, silenced bool - matcherGroups []string + inhibited, silenced, active, unprocessed bool + + matcherGroups []string } const alertHelp = `View and search through current alerts. @@ -41,6 +42,10 @@ amtool alert query 'alertname=~foo.*' As well as direct equality, regex matching is also supported. The '=~' syntax (similar to prometheus) is used to represent a regex match. Regex matching can be used in combination with a direct match. + +Amtool supports several flags for filtering the returned alerts by state +(inhibited, silenced, active, unprocessed). If none of these flags is given, +only active alerts are returned. ` func configureAlertCmd(app *kingpin.Application) { @@ -51,6 +56,8 @@ func configureAlertCmd(app *kingpin.Application) { ) queryCmd.Flag("inhibited", "Show inhibited alerts").Short('i').BoolVar(&a.inhibited) queryCmd.Flag("silenced", "Show silenced alerts").Short('s').BoolVar(&a.silenced) + queryCmd.Flag("active", "Show active alerts").Short('a').BoolVar(&a.active) + queryCmd.Flag("unprocessed", "Show unprocessed alerts").Short('u').BoolVar(&a.unprocessed) queryCmd.Arg("matcher-groups", "Query filter").StringsVar(&a.matcherGroups) queryCmd.Action(a.queryAlerts) } @@ -76,7 +83,11 @@ func (a *alertQueryCmd) queryAlerts(ctx *kingpin.ParseContext) error { return err } alertAPI := client.NewAlertAPI(c) - fetchedAlerts, err := alertAPI.List(context.Background(), filterString, a.silenced, a.inhibited) + // If no selector was passed, default to showing active alerts. + if !a.silenced && !a.inhibited && !a.active && !a.unprocessed { + a.active = true + } + fetchedAlerts, err := alertAPI.List(context.Background(), filterString, a.silenced, a.inhibited, a.active, a.unprocessed) if err != nil { return err } diff --git a/client/client.go b/client/client.go index 987d3b8d..07346032 100644 --- a/client/client.go +++ b/client/client.go @@ -154,7 +154,7 @@ func (h *httpStatusAPI) Get(ctx context.Context) (*ServerStatus, error) { // AlertAPI provides bindings for the Alertmanager's alert API. type AlertAPI interface { // List returns all the active alerts. - List(ctx context.Context, filter string, silenced, inhibited bool) ([]*ExtendedAlert, error) + List(ctx context.Context, filter string, silenced, inhibited, active, unprocessed bool) ([]*ExtendedAlert, error) // Push sends a list of alerts to the Alertmanager. Push(ctx context.Context, alerts ...Alert) error } @@ -194,7 +194,7 @@ type httpAlertAPI struct { client api.Client } -func (h *httpAlertAPI) List(ctx context.Context, filter string, silenced, inhibited bool) ([]*ExtendedAlert, error) { +func (h *httpAlertAPI) List(ctx context.Context, filter string, silenced, inhibited, active, unprocessed bool) ([]*ExtendedAlert, error) { u := h.client.URL(epAlerts, nil) params := url.Values{} if filter != "" { @@ -202,6 +202,8 @@ func (h *httpAlertAPI) List(ctx context.Context, filter string, silenced, inhibi } params.Add("silenced", fmt.Sprintf("%t", silenced)) params.Add("inhibited", fmt.Sprintf("%t", inhibited)) + params.Add("active", fmt.Sprintf("%t", active)) + params.Add("unprocessed", fmt.Sprintf("%t", unprocessed)) u.RawQuery = params.Encode() req, _ := http.NewRequest(http.MethodGet, u.String(), nil) diff --git a/client/client_test.go b/client/client_test.go index 6a6fc223..f5f3fdfd 100644 --- a/client/client_test.go +++ b/client/client_test.go @@ -118,7 +118,7 @@ func TestAPI(t *testing.T) { } doAlertList := func() (interface{}, error) { api := httpAlertAPI{client: client} - return api.List(context.Background(), "", false, false) + return api.List(context.Background(), "", false, false, false, false) } doAlertPush := func() (interface{}, error) { api := httpAlertAPI{client: client}