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"
|
|
|
|
|
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"
|
|
|
|
)
|
|
|
|
|
2018-04-13 11:34:16 +00:00
|
|
|
type alertQueryCmd struct {
|
|
|
|
expired, silenced bool
|
|
|
|
matcherGroups []string
|
|
|
|
}
|
2017-04-20 09:04:17 +00:00
|
|
|
|
2018-04-13 11:34:16 +00:00
|
|
|
func configureAlertCmd(app *kingpin.Application, longHelpText map[string]string) {
|
|
|
|
var (
|
|
|
|
a = &alertQueryCmd{}
|
|
|
|
alertCmd = app.Command("alert", "View and search through current alerts")
|
|
|
|
queryCmd = alertCmd.Command("query", "View and search through current alerts").Default()
|
|
|
|
)
|
|
|
|
queryCmd.Flag("expired", "Show expired alerts as well as active").BoolVar(&a.expired)
|
|
|
|
queryCmd.Flag("silenced", "Show silenced alerts").Short('s').BoolVar(&a.silenced)
|
|
|
|
queryCmd.Arg("matcher-groups", "Query filter").StringsVar(&a.matcherGroups)
|
|
|
|
queryCmd.Action(a.queryAlerts)
|
2017-12-22 10:17:13 +00:00
|
|
|
longHelpText["alert"] = `View and search through current alerts.
|
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 alert 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 alerts 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 alert query foo node=bar
|
2017-04-20 09:04:17 +00:00
|
|
|
|
2017-12-22 10:17:13 +00:00
|
|
|
If alertname is omitted and the first argument does not contain a '=' or a
|
|
|
|
'=~' 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 alert query 'alertname=~foo.*'
|
2017-04-20 09:04:17 +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.`
|
|
|
|
longHelpText["alert query"] = longHelpText["alert"]
|
2017-04-20 09:04:17 +00:00
|
|
|
}
|
|
|
|
|
2018-04-13 11:34:16 +00:00
|
|
|
func (a *alertQueryCmd) queryAlerts(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(a.matcherGroups) == 1 {
|
2018-04-11 09:17:41 +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(a.matcherGroups[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}", a.matcherGroups[0])
|
2017-04-20 09:04:17 +00:00
|
|
|
} else {
|
2018-04-13 11:34:16 +00:00
|
|
|
filterString = fmt.Sprintf("{%s}", strings.Join(a.matcherGroups, ","))
|
2017-04-20 09:04:17 +00:00
|
|
|
}
|
2018-04-13 11:34:16 +00:00
|
|
|
} else if len(a.matcherGroups) > 1 {
|
|
|
|
filterString = fmt.Sprintf("{%s}", strings.Join(a.matcherGroups, ","))
|
2017-04-20 09:04:17 +00:00
|
|
|
}
|
|
|
|
|
2018-04-13 11:34:16 +00:00
|
|
|
c, err := api.NewClient(api.Config{Address: alertmanagerURL.String()})
|
2017-04-20 09:04:17 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-04-11 09:17:41 +00:00
|
|
|
alertAPI := client.NewAlertAPI(c)
|
2018-04-13 11:34:16 +00:00
|
|
|
fetchedAlerts, err := alertAPI.List(context.Background(), filterString, a.expired, a.silenced)
|
2018-04-11 09:17:41 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2017-04-20 09:04:17 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
2018-04-11 09:17:41 +00:00
|
|
|
return formatter.FormatAlerts(fetchedAlerts)
|
2017-04-20 09:04:17 +00:00
|
|
|
}
|