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 <stuartnelson3@gmail.com>

* Update comment and client_test

Signed-off-by: stuart nelson <stuartnelson3@gmail.com>
This commit is contained in:
stuart nelson 2018-05-09 10:57:01 +02:00 committed by GitHub
parent 02f10f204f
commit 942be9d993
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 19 additions and 6 deletions

View File

@ -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
}

View File

@ -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)

View File

@ -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}