Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added ability to query for silences that will expire soon #1120

Merged
merged 1 commit into from
Dec 12, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions cli/silence_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"time"

"github.com/prometheus/alertmanager/cli/format"
"github.com/prometheus/common/model"
"github.com/prometheus/alertmanager/pkg/parse"
"github.com/prometheus/alertmanager/types"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -44,12 +45,23 @@ var queryCmd = &cobra.Command{
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.

In addition to filtering by silence labels, one can also query for silences
that are due to expire soon with the "--within" parameter. In the event that
you want to preemptively act upon expiring silences by either fixing them or
extending them. For example:

amtool silence query --within 8h

gives all the silences due to expire within the next 8 hours. This syntax can
also be combined with the label based filtering above for more flexibility.
`,
Run: CommandWrapper(query),
}

func init() {
queryCmd.Flags().Bool("expired", false, "Show expired silences as well as active")
queryCmd.Flags().String("within", "", "Show silences that will expire within a duration")
queryFlags = queryCmd.Flags()
}

Expand Down Expand Up @@ -89,6 +101,19 @@ func query(cmd *cobra.Command, args []string) error {
return err
}

within, err := queryFlags.GetString("within")
if err != nil {
return err
}

var duration model.Duration
if within != "" {
duration, err = model.ParseDuration(within)
if err != nil {
return err
}
}

quiet := viper.GetBool("quiet")

var filterString = ""
Expand Down Expand Up @@ -117,6 +142,11 @@ func query(cmd *cobra.Command, args []string) error {
if !expired && silence.EndsAt.Before(time.Now()) {
continue
}

if int64(duration) > 0 && silence.EndsAt.After(time.Now().UTC().Add(time.Duration(duration))) {
continue
}

displaySilences = append(displaySilences, silence)
}

Expand Down