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/dispatch"
|
|
|
|
"github.com/prometheus/alertmanager/pkg/parse"
|
2017-04-27 12:18:52 +00:00
|
|
|
"github.com/prometheus/alertmanager/types"
|
2017-04-20 09:04:17 +00:00
|
|
|
"github.com/prometheus/common/model"
|
|
|
|
)
|
|
|
|
|
|
|
|
type alertmanagerAlertResponse struct {
|
|
|
|
Status string `json:"status"`
|
|
|
|
Data []*alertGroup `json:"data,omitempty"`
|
|
|
|
ErrorType string `json:"errorType,omitempty"`
|
|
|
|
Error string `json:"error,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type alertGroup struct {
|
|
|
|
Labels model.LabelSet `json:"labels"`
|
2017-05-08 10:40:39 +00:00
|
|
|
GroupKey string `json:"groupKey"`
|
2017-04-20 09:04:17 +00:00
|
|
|
Blocks []*alertBlock `json:"blocks"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type alertBlock struct {
|
|
|
|
RouteOpts interface{} `json:"routeOpts"`
|
|
|
|
Alerts []*dispatch.APIAlert `json:"alerts"`
|
|
|
|
}
|
|
|
|
|
2017-12-22 10:17:13 +00:00
|
|
|
var (
|
|
|
|
alertCmd = app.Command("alert", "View and search through current alerts")
|
|
|
|
alertQueryCmd = alertCmd.Command("query", "View and search through current alerts").Default()
|
|
|
|
expired = alertQueryCmd.Flag("expired", "Show expired alerts as well as active").Bool()
|
|
|
|
showSilenced = alertQueryCmd.Flag("silenced", "Show silenced alerts").Short('s').Bool()
|
|
|
|
alertQuery = alertQueryCmd.Arg("matcher-groups", "Query filter").Strings()
|
|
|
|
)
|
2017-04-20 09:04:17 +00:00
|
|
|
|
2017-12-22 10:17:13 +00:00
|
|
|
func init() {
|
|
|
|
alertQueryCmd.Action(queryAlerts)
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
func fetchAlerts(filter string) ([]*dispatch.APIAlert, error) {
|
|
|
|
alertResponse := alertmanagerAlertResponse{}
|
|
|
|
|
2017-12-22 10:17:13 +00:00
|
|
|
u := GetAlertmanagerURL("/api/v1/alerts/groups")
|
2017-04-20 09:04:17 +00:00
|
|
|
u.RawQuery = "filter=" + url.QueryEscape(filter)
|
|
|
|
|
|
|
|
res, err := http.Get(u.String())
|
|
|
|
if err != nil {
|
|
|
|
return []*dispatch.APIAlert{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
defer res.Body.Close()
|
|
|
|
|
|
|
|
err = json.NewDecoder(res.Body).Decode(&alertResponse)
|
|
|
|
if err != nil {
|
2017-11-12 16:43:48 +00:00
|
|
|
return []*dispatch.APIAlert{}, fmt.Errorf("unable to decode json response: %s", err)
|
2017-04-20 09:04:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if alertResponse.Status != "success" {
|
|
|
|
return []*dispatch.APIAlert{}, fmt.Errorf("[%s] %s", alertResponse.ErrorType, alertResponse.Error)
|
|
|
|
}
|
|
|
|
|
|
|
|
return flattenAlertOverview(alertResponse.Data), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func flattenAlertOverview(overview []*alertGroup) []*dispatch.APIAlert {
|
|
|
|
alerts := []*dispatch.APIAlert{}
|
|
|
|
for _, group := range overview {
|
|
|
|
for _, block := range group.Blocks {
|
|
|
|
alerts = append(alerts, block.Alerts...)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return alerts
|
|
|
|
}
|
|
|
|
|
2017-12-22 10:17:13 +00:00
|
|
|
func queryAlerts(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(*alertQuery) == 1 {
|
2017-04-20 09:04:17 +00:00
|
|
|
// If we only have one argument then it's possible that the user wants me to assume alertname=<arg>
|
|
|
|
// Attempt to use the parser to pare the argument
|
|
|
|
// If the parser fails then we likely don't have a (=|=~|!=|!~) so lets prepend `alertname=` to the front
|
2017-12-22 10:17:13 +00:00
|
|
|
_, err := parse.Matcher((*alertQuery)[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}", (*alertQuery)[0])
|
2017-04-20 09:04:17 +00:00
|
|
|
} else {
|
2017-12-22 10:17:13 +00:00
|
|
|
filterString = fmt.Sprintf("{%s}", strings.Join(*alertQuery, ","))
|
2017-04-20 09:04:17 +00:00
|
|
|
}
|
2017-12-22 10:17:13 +00:00
|
|
|
} else if len(*alertQuery) > 1 {
|
|
|
|
filterString = fmt.Sprintf("{%s}", strings.Join(*alertQuery, ","))
|
2017-04-20 09:04:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fetchedAlerts, err := fetchAlerts(filterString)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
displayAlerts := []*dispatch.APIAlert{}
|
|
|
|
for _, alert := range fetchedAlerts {
|
|
|
|
// If we are only returning current alerts and this one has already expired skip it
|
2017-12-22 10:17:13 +00:00
|
|
|
if !*expired {
|
2017-04-20 09:04:17 +00:00
|
|
|
if !alert.EndsAt.IsZero() && alert.EndsAt.Before(time.Now()) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-22 10:17:13 +00:00
|
|
|
if !*showSilenced {
|
2017-04-20 09:04:17 +00:00
|
|
|
// If any silence mutes this alert don't show it
|
2017-05-08 10:40:39 +00:00
|
|
|
if alert.Status.State == types.AlertStateSuppressed && len(alert.Status.SilencedBy) > 0 {
|
2017-04-20 09:04:17 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
displayAlerts = append(displayAlerts, alert)
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
return formatter.FormatAlerts(displayAlerts)
|
|
|
|
}
|