2017-12-11 13:46:59 +00:00
|
|
|
package cli
|
|
|
|
|
|
|
|
import (
|
2018-04-11 09:17:41 +00:00
|
|
|
"context"
|
2018-03-29 10:11:31 +00:00
|
|
|
"errors"
|
2017-12-11 13:46:59 +00:00
|
|
|
"fmt"
|
|
|
|
"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-12-11 13:46:59 +00:00
|
|
|
"github.com/prometheus/alertmanager/cli/format"
|
2018-04-11 09:17:41 +00:00
|
|
|
"github.com/prometheus/alertmanager/client"
|
2017-12-11 13:46:59 +00:00
|
|
|
"github.com/prometheus/alertmanager/types"
|
2018-01-15 18:45:46 +00:00
|
|
|
"github.com/prometheus/common/model"
|
2017-12-11 13:46:59 +00:00
|
|
|
)
|
|
|
|
|
2018-04-13 11:34:16 +00:00
|
|
|
type silenceUpdateCmd struct {
|
|
|
|
quiet bool
|
|
|
|
duration string
|
|
|
|
start string
|
|
|
|
end string
|
|
|
|
comment string
|
|
|
|
ids []string
|
|
|
|
}
|
|
|
|
|
|
|
|
func configureSilenceUpdateCmd(cc *kingpin.CmdClause, longHelpText map[string]string) {
|
|
|
|
var (
|
|
|
|
c = &silenceUpdateCmd{}
|
|
|
|
updateCmd = cc.Command("update", "Update silences")
|
|
|
|
)
|
|
|
|
updateCmd.Flag("quiet", "Only show silence ids").Short('q').BoolVar(&c.quiet)
|
|
|
|
updateCmd.Flag("duration", "Duration of silence").Short('d').StringVar(&c.duration)
|
|
|
|
updateCmd.Flag("start", "Set when the silence should start. RFC3339 format 2006-01-02T15:04:05Z07:00").StringVar(&c.start)
|
|
|
|
updateCmd.Flag("end", "Set when the silence should end (overwrites duration). RFC3339 format 2006-01-02T15:04:05Z07:00").StringVar(&c.end)
|
|
|
|
updateCmd.Flag("comment", "A comment to help describe the silence").Short('c').StringVar(&c.comment)
|
|
|
|
updateCmd.Arg("update-ids", "Silence IDs to update").StringsVar(&c.ids)
|
2017-12-11 13:46:59 +00:00
|
|
|
|
2018-04-13 11:34:16 +00:00
|
|
|
updateCmd.Action(c.update)
|
2017-12-22 10:17:13 +00:00
|
|
|
longHelpText["silence update"] = `Extend or update existing silence in Alertmanager.`
|
2017-12-11 13:46:59 +00:00
|
|
|
}
|
|
|
|
|
2018-04-13 11:34:16 +00:00
|
|
|
func (c *silenceUpdateCmd) update(element *kingpin.ParseElement, ctx *kingpin.ParseContext) error {
|
|
|
|
if len(c.ids) < 1 {
|
2017-12-11 13:46:59 +00:00
|
|
|
return fmt.Errorf("no silence IDs specified")
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2017-12-11 13:46:59 +00:00
|
|
|
var updatedSilences []types.Silence
|
2018-04-13 11:34:16 +00:00
|
|
|
for _, silenceID := range c.ids {
|
2018-04-11 09:17:41 +00:00
|
|
|
silence, err := silenceAPI.Get(context.Background(), silenceID)
|
2017-12-11 13:46:59 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-04-13 11:34:16 +00:00
|
|
|
if c.start != "" {
|
|
|
|
silence.StartsAt, err = time.Parse(time.RFC3339, c.start)
|
2018-04-11 09:17:41 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-13 11:34:16 +00:00
|
|
|
if c.end != "" {
|
|
|
|
silence.EndsAt, err = time.Parse(time.RFC3339, c.end)
|
2018-04-11 09:17:41 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-04-13 11:34:16 +00:00
|
|
|
} else if c.duration != "" {
|
|
|
|
d, err := model.ParseDuration(c.duration)
|
2018-04-11 09:17:41 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if d == 0 {
|
|
|
|
return fmt.Errorf("silence duration must be greater than 0")
|
|
|
|
}
|
|
|
|
silence.EndsAt = silence.StartsAt.UTC().Add(time.Duration(d))
|
|
|
|
}
|
|
|
|
|
|
|
|
if silence.StartsAt.After(silence.EndsAt) {
|
|
|
|
return errors.New("silence cannot start after it ends")
|
|
|
|
}
|
|
|
|
|
2018-04-13 11:34:16 +00:00
|
|
|
if c.comment != "" {
|
|
|
|
silence.Comment = c.comment
|
2018-04-11 09:17:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
newID, err := silenceAPI.Set(context.Background(), *silence)
|
2017-12-11 13:46:59 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-04-11 09:17:41 +00:00
|
|
|
silence.ID = newID
|
|
|
|
|
2017-12-11 13:46:59 +00:00
|
|
|
updatedSilences = append(updatedSilences, *silence)
|
|
|
|
}
|
|
|
|
|
2018-04-13 11:34:16 +00:00
|
|
|
if c.quiet {
|
2017-12-11 13:46:59 +00:00
|
|
|
for _, silence := range updatedSilences {
|
|
|
|
fmt.Println(silence.ID)
|
|
|
|
}
|
|
|
|
} else {
|
2018-04-13 11:34:16 +00:00
|
|
|
formatter, found := format.Formatters[output]
|
2017-12-11 13:46:59 +00:00
|
|
|
if !found {
|
|
|
|
return fmt.Errorf("unknown output formatter")
|
|
|
|
}
|
|
|
|
formatter.FormatSilences(updatedSilences)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|