@@ -17,9 +17,9 @@ import (
17
17
18
18
var (
19
19
queryCmd = silenceCmd .Command ("query" , "Query Alertmanager silences." ).Default ()
20
- queryExpired = queryCmd .Flag ("expired" , "Show expired silences as well as active" ).Bool ()
20
+ queryExpired = queryCmd .Flag ("expired" , "Show expired silences instead of active" ).Bool ()
21
21
silenceQuery = queryCmd .Arg ("matcher-groups" , "Query filter" ).Strings ()
22
- queryWithin = queryCmd .Flag ("within" , "Show silences that will expire within a duration" ).Duration ()
22
+ queryWithin = queryCmd .Flag ("within" , "Show silences that will expire or have expired within a duration" ).Duration ()
23
23
)
24
24
25
25
func init () {
@@ -106,12 +106,20 @@ func query(element *kingpin.ParseElement, ctx *kingpin.ParseContext) error {
106
106
107
107
displaySilences := []types.Silence {}
108
108
for _ , silence := range fetchedSilences {
109
- // If we are only returning current silences and this one has already expired skip it
109
+ // skip expired silences if -- expired is not set
110
110
if ! * queryExpired && silence .EndsAt .Before (time .Now ()) {
111
111
continue
112
112
}
113
-
114
- if int64 (* queryWithin ) > 0 && silence .EndsAt .After (time .Now ().UTC ().Add (* queryWithin )) {
113
+ // skip active silences if --expired is set
114
+ if * queryExpired && silence .EndsAt .After (time .Now ()) {
115
+ continue
116
+ }
117
+ // skip active silences expiring after "--within"
118
+ if ! * queryExpired && int64 (* queryWithin ) > 0 && silence .EndsAt .After (time .Now ().UTC ().Add (* queryWithin )) {
119
+ continue
120
+ }
121
+ // skip silences that expired before "--within"
122
+ if * queryExpired && int64 (* queryWithin ) > 0 && silence .EndsAt .Before (time .Now ().UTC ().Add (- * queryWithin )) {
115
123
continue
116
124
}
117
125
0 commit comments