mirror of
https://github.com/prometheus/alertmanager
synced 2024-12-25 15:42:18 +00:00
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:
parent
02f10f204f
commit
942be9d993
17
cli/alert.go
17
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
|
||||
}
|
||||
|
@ -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)
|
||||
|
@ -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}
|
||||
|
Loading…
Reference in New Issue
Block a user