2017-04-20 09:04:17 +00:00
|
|
|
package cli
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2017-12-22 10:17:13 +00:00
|
|
|
"github.com/alecthomas/kingpin"
|
2017-04-20 09:04:17 +00:00
|
|
|
"github.com/prometheus/alertmanager/cli/format"
|
|
|
|
"github.com/prometheus/alertmanager/pkg/parse"
|
|
|
|
"github.com/prometheus/alertmanager/types"
|
|
|
|
)
|
|
|
|
|
2017-12-22 10:17:13 +00:00
|
|
|
var (
|
|
|
|
queryCmd = silenceCmd.Command("query", "Query Alertmanager silences.").Default()
|
2018-01-12 09:35:06 +00:00
|
|
|
queryExpired = queryCmd.Flag("expired", "Show expired silences instead of active").Bool()
|
2017-12-22 10:17:13 +00:00
|
|
|
silenceQuery = queryCmd.Arg("matcher-groups", "Query filter").Strings()
|
2018-01-12 09:35:06 +00:00
|
|
|
queryWithin = queryCmd.Flag("within", "Show silences that will expire or have expired within a duration").Duration()
|
2017-12-22 10:17:13 +00:00
|
|
|
)
|
2017-04-20 09:04:17 +00:00
|
|
|
|
2017-12-22 10:17:13 +00:00
|
|
|
func init() {
|
|
|
|
queryCmd.Action(query)
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
func fetchSilences(filter string) ([]types.Silence, error) {
|
|
|
|
silenceResponse := alertmanagerSilenceResponse{}
|
|
|
|
|
2017-12-22 10:17:13 +00:00
|
|
|
u := GetAlertmanagerURL("/api/v1/silences")
|
2017-04-20 09:04:17 +00:00
|
|
|
u.RawQuery = "filter=" + url.QueryEscape(filter)
|
|
|
|
|
|
|
|
res, err := http.Get(u.String())
|
|
|
|
if err != nil {
|
|
|
|
return []types.Silence{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
defer res.Body.Close()
|
|
|
|
|
|
|
|
err = json.NewDecoder(res.Body).Decode(&silenceResponse)
|
|
|
|
if err != nil {
|
|
|
|
return []types.Silence{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if silenceResponse.Status != "success" {
|
|
|
|
return []types.Silence{}, fmt.Errorf("[%s] %s", silenceResponse.ErrorType, silenceResponse.Error)
|
|
|
|
}
|
|
|
|
|
|
|
|
return silenceResponse.Data, nil
|
|
|
|
}
|
|
|
|
|
2017-12-22 10:17:13 +00:00
|
|
|
func query(element *kingpin.ParseElement, ctx *kingpin.ParseContext) error {
|
2017-04-20 09:04:17 +00:00
|
|
|
var filterString = ""
|
2017-12-22 10:17:13 +00:00
|
|
|
if len(*silenceQuery) == 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.
|
2017-12-22 10:17:13 +00:00
|
|
|
_, err := parse.Matcher((*silenceQuery)[0])
|
2017-04-20 09:04:17 +00:00
|
|
|
if err != nil {
|
2017-12-22 10:17:13 +00:00
|
|
|
filterString = fmt.Sprintf("{alertname=%s}", (*silenceQuery)[0])
|
2017-04-20 09:04:17 +00:00
|
|
|
} else {
|
2017-12-22 10:17:13 +00:00
|
|
|
filterString = fmt.Sprintf("{%s}", strings.Join(*silenceQuery, ","))
|
2017-04-20 09:04:17 +00:00
|
|
|
}
|
2017-12-22 10:17:13 +00:00
|
|
|
} else if len(*silenceQuery) > 1 {
|
|
|
|
filterString = fmt.Sprintf("{%s}", strings.Join(*silenceQuery, ","))
|
2017-04-20 09:04:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fetchedSilences, err := fetchSilences(filterString)
|
|
|
|
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
|
2017-12-22 10:17:13 +00:00
|
|
|
if !*queryExpired && 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
|
|
|
|
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)) {
|
2017-12-12 14:36:05 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2017-04-20 09:04:17 +00:00
|
|
|
displaySilences = append(displaySilences, silence)
|
|
|
|
}
|
|
|
|
|
2017-12-22 10:17:13 +00:00
|
|
|
if *silenceQuiet {
|
2017-04-20 09:04:17 +00:00
|
|
|
for _, silence := range displaySilences {
|
|
|
|
fmt.Println(silence.ID)
|
|
|
|
}
|
|
|
|
} else {
|
2017-12-22 10:17:13 +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
|
|
|
|
}
|