mirror of
https://github.com/prometheus/alertmanager
synced 2024-12-27 08:32:15 +00:00
[amtool] update silence add and update flags (#1298)
* Update silence add/update flags - Change --expires/-e to --duration/-d - Change --expires-on to --end - Add --start * update subcommand returns ID of new silence The silences printed before were accurate, except they had the old ID. Now the new ID is returned. * Duration is added to silence.StartsAt When a user supplies a duration to update a silence, it is applied to silence.StartsAt after any potential changes to the silence's start time.
This commit is contained in:
parent
0c086e3b12
commit
19715022a4
@ -35,8 +35,9 @@ var (
|
||||
addCmd = silenceCmd.Command("add", "Add a new alertmanager silence")
|
||||
author = addCmd.Flag("author", "Username for CreatedBy field").Short('a').Default(username()).String()
|
||||
requireComment = addCmd.Flag("require-comment", "Require comment to be set").Hidden().Default("true").Bool()
|
||||
expires = addCmd.Flag("expires", "Duration of silence").Short('e').Default("1h").String()
|
||||
expireOn = addCmd.Flag("expire-on", "Expire at a certain time (Overwrites expires) RFC3339 format 2006-01-02T15:04:05Z07:00").String()
|
||||
duration = addCmd.Flag("duration", "Duration of silence").Short('d').Default("1h").String()
|
||||
addStart = addCmd.Flag("start", "Set when the silence should start. RFC3339 format 2006-01-02T15:04:05Z07:00").String()
|
||||
addEnd = addCmd.Flag("end", "Set when the silence should end (overwrites duration). RFC3339 format 2006-01-02T15:04:05Z07:00").String()
|
||||
comment = addCmd.Flag("comment", "A comment to help describe the silence").Short('c').String()
|
||||
addArgs = addCmd.Arg("matcher-groups", "Query filter").Strings()
|
||||
)
|
||||
@ -81,26 +82,41 @@ func add(element *kingpin.ParseElement, ctx *kingpin.ParseContext) error {
|
||||
}
|
||||
|
||||
var endsAt time.Time
|
||||
if *expireOn != "" {
|
||||
endsAt, err = time.Parse(time.RFC3339, *expireOn)
|
||||
if *addEnd != "" {
|
||||
endsAt, err = time.Parse(time.RFC3339, *addEnd)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
} else {
|
||||
duration, err := model.ParseDuration(*expires)
|
||||
d, err := model.ParseDuration(*duration)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if duration == 0 {
|
||||
if d == 0 {
|
||||
return fmt.Errorf("silence duration must be greater than 0")
|
||||
}
|
||||
endsAt = time.Now().UTC().Add(time.Duration(duration))
|
||||
endsAt = time.Now().UTC().Add(time.Duration(d))
|
||||
}
|
||||
|
||||
if *requireComment && *comment == "" {
|
||||
return errors.New("comment required by config")
|
||||
}
|
||||
|
||||
var startsAt time.Time
|
||||
if *addStart != "" {
|
||||
startsAt, err = time.Parse(time.RFC3339, *addStart)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
} else {
|
||||
startsAt = time.Now().UTC()
|
||||
}
|
||||
|
||||
if startsAt.After(endsAt) {
|
||||
return errors.New("silence cannot start after it ends")
|
||||
}
|
||||
|
||||
typeMatchers, err := TypeMatchers(matchers)
|
||||
if err != nil {
|
||||
return err
|
||||
@ -108,7 +124,7 @@ func add(element *kingpin.ParseElement, ctx *kingpin.ParseContext) error {
|
||||
|
||||
silence := types.Silence{
|
||||
Matchers: typeMatchers,
|
||||
StartsAt: time.Now().UTC(),
|
||||
StartsAt: startsAt,
|
||||
EndsAt: endsAt,
|
||||
CreatedBy: *author,
|
||||
Comment: *comment,
|
||||
|
@ -2,6 +2,7 @@ package cli
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
@ -23,11 +24,12 @@ type getResponse struct {
|
||||
}
|
||||
|
||||
var (
|
||||
updateCmd = silenceCmd.Command("update", "Update silences")
|
||||
updateExpires = updateCmd.Flag("expires", "Duration of silence").Short('e').Default("1h").String()
|
||||
updateExpiresOn = updateCmd.Flag("expire-on", "Expire at a certain time (Overwrites expires) RFC3339 format 2006-01-02T15:04:05Z07:00").Time(time.RFC3339)
|
||||
updateComment = updateCmd.Flag("comment", "A comment to help describe the silence").Short('c').String()
|
||||
updateIds = updateCmd.Arg("update-ids", "Silence IDs to update").Strings()
|
||||
updateCmd = silenceCmd.Command("update", "Update silences")
|
||||
updateDuration = updateCmd.Flag("duration", "Duration of silence").Short('d').String()
|
||||
updateStart = updateCmd.Flag("start", "Set when the silence should start. RFC3339 format 2006-01-02T15:04:05Z07:00").String()
|
||||
updateEnd = updateCmd.Flag("end", "Set when the silence should end (overwrites duration). RFC3339 format 2006-01-02T15:04:05Z07:00").String()
|
||||
updateComment = updateCmd.Flag("comment", "A comment to help describe the silence").Short('c').String()
|
||||
updateIds = updateCmd.Arg("update-ids", "Silence IDs to update").Strings()
|
||||
)
|
||||
|
||||
func init() {
|
||||
@ -98,20 +100,32 @@ func getSilenceById(silenceId string, baseUrl url.URL) (*types.Silence, error) {
|
||||
}
|
||||
|
||||
func updateSilence(silence *types.Silence) (*types.Silence, error) {
|
||||
if *updateExpires != "" {
|
||||
duration, err := model.ParseDuration(*updateExpires)
|
||||
var err error
|
||||
if *updateStart != "" {
|
||||
silence.StartsAt, err = time.Parse(time.RFC3339, *updateStart)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if duration == 0 {
|
||||
return nil, fmt.Errorf("silence duration must be greater than 0")
|
||||
}
|
||||
silence.EndsAt = time.Now().UTC().Add(time.Duration(duration))
|
||||
}
|
||||
|
||||
// expire-on will override expires value if both are specified
|
||||
if !(*updateExpiresOn).IsZero() {
|
||||
silence.EndsAt = *updateExpiresOn
|
||||
if *updateEnd != "" {
|
||||
silence.EndsAt, err = time.Parse(time.RFC3339, *updateEnd)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
} else if *updateDuration != "" {
|
||||
d, err := model.ParseDuration(*updateDuration)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if d == 0 {
|
||||
return nil, 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 nil, errors.New("silence cannot start after it ends")
|
||||
}
|
||||
|
||||
if *updateComment != "" {
|
||||
@ -119,9 +133,10 @@ func updateSilence(silence *types.Silence) (*types.Silence, error) {
|
||||
}
|
||||
|
||||
// addSilence can also be used to update an existing silence
|
||||
_, err := addSilence(silence)
|
||||
newID, err := addSilence(silence)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
silence.ID = newID
|
||||
return silence, nil
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user