mirror of
https://github.com/prometheus/alertmanager
synced 2025-02-02 19:52:08 +00:00
cli: style improvement and fix filter handling
Signed-off-by: Paul Gier <pgier@redhat.com>
This commit is contained in:
parent
6561774f9e
commit
ab4784b112
@ -74,7 +74,6 @@ func configureQueryAlertsCmd(cc *kingpin.CmdClause) {
|
||||
}
|
||||
|
||||
func (a *alertQueryCmd) queryAlerts(ctx context.Context, _ *kingpin.ParseContext) error {
|
||||
filter := []string{}
|
||||
if len(a.matcherGroups) > 0 {
|
||||
// Attempt to parse the first argument. If the parser fails
|
||||
// then we likely don't have a (=|=~|!=|!~) so lets assume that
|
||||
@ -85,7 +84,6 @@ func (a *alertQueryCmd) queryAlerts(ctx context.Context, _ *kingpin.ParseContext
|
||||
if err != nil {
|
||||
a.matcherGroups[0] = fmt.Sprintf("alertname=%s", m)
|
||||
}
|
||||
filter = append(filter, a.matcherGroups[0])
|
||||
}
|
||||
|
||||
// If no selector was passed, default to showing active alerts.
|
||||
@ -93,12 +91,12 @@ func (a *alertQueryCmd) queryAlerts(ctx context.Context, _ *kingpin.ParseContext
|
||||
a.active = true
|
||||
}
|
||||
|
||||
alertParams := alert.NewGetAlertsParams().WithContext(ctx)
|
||||
alertParams.Active = &a.active
|
||||
alertParams.Inhibited = &a.inhibited
|
||||
alertParams.Silenced = &a.silenced
|
||||
alertParams.Unprocessed = &a.unprocessed
|
||||
alertParams.Filter = filter
|
||||
alertParams := alert.NewGetAlertsParams().WithContext(ctx).
|
||||
WithActive(&a.active).
|
||||
WithInhibited(&a.inhibited).
|
||||
WithSilenced(&a.silenced).
|
||||
WithUnprocessed(&a.unprocessed).
|
||||
WithFilter(a.matcherGroups)
|
||||
|
||||
amclient := NewAlertmanagerClient(alertmanagerURL)
|
||||
|
||||
|
@ -44,10 +44,6 @@ type Formatter interface {
|
||||
// Formatters is a map of cli argument names to formatter interface object.
|
||||
var Formatters = map[string]Formatter{}
|
||||
|
||||
func FormatDateTime(input strfmt.DateTime) string {
|
||||
return FormatDate(time.Time(input))
|
||||
}
|
||||
|
||||
func FormatDate(input time.Time) string {
|
||||
return input.Format(*dateFormat)
|
||||
func FormatDate(input strfmt.DateTime) string {
|
||||
return time.Time(input).Format(*dateFormat)
|
||||
}
|
||||
|
@ -20,7 +20,6 @@ import (
|
||||
"sort"
|
||||
"strings"
|
||||
"text/tabwriter"
|
||||
"time"
|
||||
|
||||
"github.com/prometheus/alertmanager/api/v2/models"
|
||||
)
|
||||
@ -48,9 +47,9 @@ func (formatter *ExtendedFormatter) FormatSilences(silences []models.GettableSil
|
||||
"%s\t%s\t%s\t%s\t%s\t%s\t%s\t\n",
|
||||
*silence.ID,
|
||||
extendedFormatMatchers(silence.Matchers),
|
||||
FormatDateTime(*silence.Silence.StartsAt),
|
||||
FormatDateTime(*silence.Silence.EndsAt),
|
||||
FormatDateTime(*silence.UpdatedAt),
|
||||
FormatDate(*silence.Silence.StartsAt),
|
||||
FormatDate(*silence.Silence.EndsAt),
|
||||
FormatDate(*silence.UpdatedAt),
|
||||
*silence.CreatedBy,
|
||||
*silence.Comment,
|
||||
)
|
||||
@ -69,8 +68,8 @@ func (formatter *ExtendedFormatter) FormatAlerts(alerts []*models.GettableAlert)
|
||||
"%s\t%s\t%s\t%s\t%s\t\n",
|
||||
extendedFormatLabels(alert.Labels),
|
||||
extendedFormatAnnotations(alert.Annotations),
|
||||
FormatDate(time.Time(*alert.StartsAt)),
|
||||
FormatDate(time.Time(*alert.EndsAt)),
|
||||
FormatDate(*alert.StartsAt),
|
||||
FormatDate(*alert.EndsAt),
|
||||
alert.GeneratorURL,
|
||||
)
|
||||
}
|
||||
|
@ -20,7 +20,6 @@ import (
|
||||
"sort"
|
||||
"strings"
|
||||
"text/tabwriter"
|
||||
"time"
|
||||
|
||||
"github.com/prometheus/alertmanager/api/v2/models"
|
||||
)
|
||||
@ -47,7 +46,7 @@ func (formatter *SimpleFormatter) FormatSilences(silences []models.GettableSilen
|
||||
"%s\t%s\t%s\t%s\t%s\t\n",
|
||||
*silence.ID,
|
||||
simpleFormatMatchers(silence.Matchers),
|
||||
FormatDateTime(*silence.EndsAt),
|
||||
FormatDate(*silence.EndsAt),
|
||||
*silence.CreatedBy,
|
||||
*silence.Comment,
|
||||
)
|
||||
@ -64,7 +63,7 @@ func (formatter *SimpleFormatter) FormatAlerts(alerts []*models.GettableAlert) e
|
||||
w,
|
||||
"%s\t%s\t%s\t\n",
|
||||
alert.Labels["alertname"],
|
||||
FormatDate(time.Time(*alert.StartsAt)),
|
||||
FormatDate(*alert.StartsAt),
|
||||
alert.Annotations["summary"],
|
||||
)
|
||||
}
|
||||
|
@ -60,17 +60,17 @@ func configureSilenceImportCmd(cc *kingpin.CmdClause) {
|
||||
|
||||
func addSilenceWorker(ctx context.Context, sclient *silence.Client, silencec <-chan *models.PostableSilence, errc chan<- error) {
|
||||
for s := range silencec {
|
||||
params := silence.NewPostSilencesParams().WithContext(ctx)
|
||||
params.Silence = s
|
||||
sid := s.ID
|
||||
params := silence.NewPostSilencesParams().WithContext(ctx).WithSilence(s)
|
||||
postOk, err := sclient.PostSilences(params)
|
||||
if err != nil && strings.Contains(err.Error(), "not found") {
|
||||
// silence doesn't exists yet, retry to create as a new one
|
||||
params.Silence.ID = ""
|
||||
postOk, _ = sclient.PostSilences(params)
|
||||
postOk, err = sclient.PostSilences(params)
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "Error adding silence id='%v': %v\n", s.ID, err)
|
||||
fmt.Fprintf(os.Stderr, "Error adding silence id='%v': %v\n", sid, err)
|
||||
} else {
|
||||
fmt.Println(postOk.Payload.SilenceID)
|
||||
}
|
||||
|
@ -90,7 +90,6 @@ func configureSilenceQueryCmd(cc *kingpin.CmdClause) {
|
||||
}
|
||||
|
||||
func (c *silenceQueryCmd) query(ctx context.Context, _ *kingpin.ParseContext) error {
|
||||
filter := []string{}
|
||||
if len(c.matchers) > 0 {
|
||||
// If the parser fails then we likely don't have a (=|=~|!=|!~) so lets
|
||||
// assume that the user wants alertname=<arg> and prepend `alertname=`
|
||||
@ -99,11 +98,9 @@ func (c *silenceQueryCmd) query(ctx context.Context, _ *kingpin.ParseContext) er
|
||||
if err != nil {
|
||||
c.matchers[0] = fmt.Sprintf("alertname=%s", c.matchers[0])
|
||||
}
|
||||
filter = append(filter, c.matchers[0])
|
||||
}
|
||||
|
||||
silenceParams := silence.NewGetSilencesParams().WithContext(ctx)
|
||||
silenceParams.Filter = filter
|
||||
silenceParams := silence.NewGetSilencesParams().WithContext(ctx).WithFilter(c.matchers)
|
||||
|
||||
amclient := NewAlertmanagerClient(alertmanagerURL)
|
||||
|
||||
|
@ -109,11 +109,9 @@ func (c *silenceUpdateCmd) update(ctx context.Context, _ *kingpin.ParseContext)
|
||||
Silence: sil.Silence,
|
||||
}
|
||||
|
||||
silenceParams := silence.NewPostSilencesParams().WithContext(ctx)
|
||||
silenceParams.Silence = ps
|
||||
|
||||
amclient := NewAlertmanagerClient(alertmanagerURL)
|
||||
|
||||
silenceParams := silence.NewPostSilencesParams().WithContext(ctx).WithSilence(ps)
|
||||
postOk, err := amclient.Silence.PostSilences(silenceParams)
|
||||
if err != nil {
|
||||
return err
|
||||
|
Loading…
Reference in New Issue
Block a user