From cdb44955cf0606dc59a34985f3eb9cf9db4cf4e2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thom=C3=A1s=20S=2E=20Bregolin?= <17909188+tbregolin@users.noreply.github.com> Date: Fri, 12 Jan 2018 09:35:06 +0000 Subject: [PATCH] Make --expired list only expired silences (#1176) (#1190) This means there's no longer a way to list both active and expired silences at the same time. This is the desired behaviour according to consensus at https://github.com/prometheus/alertmanager/pull/1175 --- cli/silence_query.go | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/cli/silence_query.go b/cli/silence_query.go index a7f57aca..b40eae39 100644 --- a/cli/silence_query.go +++ b/cli/silence_query.go @@ -17,9 +17,9 @@ import ( var ( queryCmd = silenceCmd.Command("query", "Query Alertmanager silences.").Default() - queryExpired = queryCmd.Flag("expired", "Show expired silences as well as active").Bool() + queryExpired = queryCmd.Flag("expired", "Show expired silences instead of active").Bool() silenceQuery = queryCmd.Arg("matcher-groups", "Query filter").Strings() - queryWithin = queryCmd.Flag("within", "Show silences that will expire within a duration").Duration() + queryWithin = queryCmd.Flag("within", "Show silences that will expire or have expired within a duration").Duration() ) func init() { @@ -106,12 +106,20 @@ func query(element *kingpin.ParseElement, ctx *kingpin.ParseContext) error { displaySilences := []types.Silence{} for _, silence := range fetchedSilences { - // If we are only returning current silences and this one has already expired skip it + // skip expired silences if --expired is not set if !*queryExpired && silence.EndsAt.Before(time.Now()) { continue } - - if int64(*queryWithin) > 0 && silence.EndsAt.After(time.Now().UTC().Add(*queryWithin)) { + // skip active silences if --expired is set + if *queryExpired && silence.EndsAt.After(time.Now()) { + continue + } + // skip active silences expiring after "--within" + if !*queryExpired && int64(*queryWithin) > 0 && silence.EndsAt.After(time.Now().UTC().Add(*queryWithin)) { + continue + } + // skip silences that expired before "--within" + if *queryExpired && int64(*queryWithin) > 0 && silence.EndsAt.Before(time.Now().UTC().Add(-*queryWithin)) { continue }