2017-04-20 09:04:17 +00:00
|
|
|
package cli
|
|
|
|
|
|
|
|
import (
|
2018-04-11 09:17:41 +00:00
|
|
|
"context"
|
2017-04-20 09:04:17 +00:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2017-12-22 10:17:13 +00:00
|
|
|
"github.com/alecthomas/kingpin"
|
2018-04-11 09:17:41 +00:00
|
|
|
"github.com/prometheus/client_golang/api"
|
|
|
|
|
2017-04-20 09:04:17 +00:00
|
|
|
"github.com/prometheus/alertmanager/cli/format"
|
2018-04-11 09:17:41 +00:00
|
|
|
"github.com/prometheus/alertmanager/client"
|
2017-04-20 09:04:17 +00:00
|
|
|
"github.com/prometheus/alertmanager/pkg/parse"
|
|
|
|
"github.com/prometheus/alertmanager/types"
|
|
|
|
)
|
|
|
|
|
2018-04-13 11:34:16 +00:00
|
|
|
type silenceQueryCmd struct {
|
|
|
|
expired bool
|
|
|
|
quiet bool
|
|
|
|
matchers []string
|
|
|
|
within time.Duration
|
|
|
|
}
|
2017-04-20 09:04:17 +00:00
|
|
|
|
2018-04-13 11:34:16 +00:00
|
|
|
func configureSilenceQueryCmd(cc *kingpin.CmdClause, longHelpText map[string]string) {
|
|
|
|
var (
|
|
|
|
c = &silenceQueryCmd{}
|
|
|
|
queryCmd = cc.Command("query", "Query Alertmanager silences.").Default()
|
|
|
|
)
|
|
|
|
|
|
|
|
queryCmd.Flag("expired", "Show expired silences instead of active").BoolVar(&c.expired)
|
|
|
|
queryCmd.Flag("quiet", "Only show silence ids").Short('q').BoolVar(&c.quiet)
|
|
|
|
queryCmd.Arg("matcher-groups", "Query filter").StringsVar(&c.matchers)
|
|
|
|
queryCmd.Flag("within", "Show silences that will expire or have expired within a duration").DurationVar(&c.within)
|
|
|
|
queryCmd.Action(c.query)
|
2017-12-22 10:17:13 +00:00
|
|
|
longHelpText["silence query"] = `Query Alertmanager silences.
|
2017-04-20 09:04:17 +00:00
|
|
|
|
2017-12-22 10:17:13 +00:00
|
|
|
Amtool has a simplified prometheus query syntax, but contains robust support for
|
|
|
|
bash variable expansions. The non-option section of arguments constructs a list
|
|
|
|
of "Matcher Groups" that will be used to filter your query. The following
|
|
|
|
examples will attempt to show this behaviour in action:
|
2017-04-20 09:04:17 +00:00
|
|
|
|
2017-12-22 10:17:13 +00:00
|
|
|
amtool silence query alertname=foo node=bar
|
2017-04-20 09:04:17 +00:00
|
|
|
|
2017-12-22 10:17:13 +00:00
|
|
|
This query will match all silences with the alertname=foo and node=bar label
|
|
|
|
value pairs set.
|
2017-04-20 09:04:17 +00:00
|
|
|
|
2017-12-22 10:17:13 +00:00
|
|
|
amtool silence query foo node=bar
|
2017-04-20 09:04:17 +00:00
|
|
|
|
2018-01-12 09:44:38 +00:00
|
|
|
If alertname is omitted and the first argument does not contain a '=' or a
|
2017-12-22 10:17:13 +00:00
|
|
|
'=~' then it will be assumed to be the value of the alertname pair.
|
2017-04-20 09:04:17 +00:00
|
|
|
|
2017-12-22 10:17:13 +00:00
|
|
|
amtool silence query 'alertname=~foo.*'
|
2017-12-12 14:36:05 +00:00
|
|
|
|
2017-12-22 10:17:13 +00:00
|
|
|
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.
|
2017-12-12 14:36:05 +00:00
|
|
|
|
2017-12-22 10:17:13 +00:00
|
|
|
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:
|
2017-12-12 14:36:05 +00:00
|
|
|
|
2017-12-22 10:17:13 +00:00
|
|
|
amtool silence query --within 8h
|
2017-04-20 09:04:17 +00:00
|
|
|
|
2018-01-12 09:44:38 +00:00
|
|
|
returns 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.
|
|
|
|
|
|
|
|
The "--expired" parameter returns only expired silences. Used in combination
|
|
|
|
with "--within=TIME", amtool returns the silences that expired within the
|
|
|
|
preceding duration.
|
|
|
|
|
|
|
|
amtool silence query --within 2h --expired
|
|
|
|
|
|
|
|
returns all silences that expired within the preceeding 2 hours.`
|
2017-04-20 09:04:17 +00:00
|
|
|
}
|
|
|
|
|
2018-04-13 11:34:16 +00:00
|
|
|
func (c *silenceQueryCmd) query(element *kingpin.ParseElement, ctx *kingpin.ParseContext) error {
|
2017-04-20 09:04:17 +00:00
|
|
|
var filterString = ""
|
2018-04-13 11:34:16 +00:00
|
|
|
if len(c.matchers) == 1 {
|
2018-04-04 08:37:35 +00:00
|
|
|
// If the parser fails then we likely don't have a (=|=~|!=|!~) so lets
|
|
|
|
// assume that the user wants alertname=<arg> and prepend `alertname=`
|
|
|
|
// to the front.
|
2018-04-13 11:34:16 +00:00
|
|
|
_, err := parse.Matcher(c.matchers[0])
|
2017-04-20 09:04:17 +00:00
|
|
|
if err != nil {
|
2018-04-13 11:34:16 +00:00
|
|
|
filterString = fmt.Sprintf("{alertname=%s}", c.matchers[0])
|
2017-04-20 09:04:17 +00:00
|
|
|
} else {
|
2018-04-13 11:34:16 +00:00
|
|
|
filterString = fmt.Sprintf("{%s}", strings.Join(c.matchers, ","))
|
2017-04-20 09:04:17 +00:00
|
|
|
}
|
2018-04-13 11:34:16 +00:00
|
|
|
} else if len(c.matchers) > 1 {
|
|
|
|
filterString = fmt.Sprintf("{%s}", strings.Join(c.matchers, ","))
|
2017-04-20 09:04:17 +00:00
|
|
|
}
|
|
|
|
|
2018-04-13 11:34:16 +00:00
|
|
|
apiClient, err := api.NewClient(api.Config{Address: alertmanagerURL.String()})
|
2018-04-11 09:17:41 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-04-13 11:34:16 +00:00
|
|
|
silenceAPI := client.NewSilenceAPI(apiClient)
|
2018-04-11 09:17:41 +00:00
|
|
|
fetchedSilences, err := silenceAPI.List(context.Background(), filterString)
|
2017-04-20 09:04:17 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
displaySilences := []types.Silence{}
|
|
|
|
for _, silence := range fetchedSilences {
|
2018-01-12 09:35:06 +00:00
|
|
|
// skip expired silences if --expired is not set
|
2018-04-13 11:34:16 +00:00
|
|
|
if !c.expired && silence.EndsAt.Before(time.Now()) {
|
2017-04-20 09:04:17 +00:00
|
|
|
continue
|
|
|
|
}
|
2018-01-12 09:35:06 +00:00
|
|
|
// skip active silences if --expired is set
|
2018-04-13 11:34:16 +00:00
|
|
|
if c.expired && silence.EndsAt.After(time.Now()) {
|
2018-01-12 09:35:06 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
// skip active silences expiring after "--within"
|
2018-04-13 11:34:16 +00:00
|
|
|
if !c.expired && int64(c.within) > 0 && silence.EndsAt.After(time.Now().UTC().Add(c.within)) {
|
2018-01-12 09:35:06 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
// skip silences that expired before "--within"
|
2018-04-13 11:34:16 +00:00
|
|
|
if c.expired && int64(c.within) > 0 && silence.EndsAt.Before(time.Now().UTC().Add(-c.within)) {
|
2017-12-12 14:36:05 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2018-04-11 09:17:41 +00:00
|
|
|
displaySilences = append(displaySilences, *silence)
|
2017-04-20 09:04:17 +00:00
|
|
|
}
|
|
|
|
|
2018-04-13 11:34:16 +00:00
|
|
|
if c.quiet {
|
2017-04-20 09:04:17 +00:00
|
|
|
for _, silence := range displaySilences {
|
|
|
|
fmt.Println(silence.ID)
|
|
|
|
}
|
|
|
|
} else {
|
2018-04-13 11:34:16 +00:00
|
|
|
formatter, found := format.Formatters[output]
|
2017-04-20 09:04:17 +00:00
|
|
|
if !found {
|
2017-11-12 16:43:48 +00:00
|
|
|
return errors.New("unknown output formatter")
|
2017-04-20 09:04:17 +00:00
|
|
|
}
|
|
|
|
formatter.FormatSilences(displaySilences)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|