2018-05-14 12:36:24 +00:00
|
|
|
// Copyright 2018 Prometheus Team
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
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"
|
|
|
|
|
2018-04-11 09:17:41 +00:00
|
|
|
"github.com/prometheus/client_golang/api"
|
2018-04-24 07:35:15 +00:00
|
|
|
"gopkg.in/alecthomas/kingpin.v2"
|
2018-04-11 09:17:41 +00:00
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2018-04-24 07:35:15 +00:00
|
|
|
func configureSilenceUpdateCmd(cc *kingpin.CmdClause) {
|
2018-04-13 11:34:16 +00:00
|
|
|
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-11 13:46:59 +00:00
|
|
|
}
|
|
|
|
|
2018-04-24 07:35:15 +00:00
|
|
|
func (c *silenceUpdateCmd) update(ctx *kingpin.ParseContext) error {
|
2018-04-13 11:34:16 +00:00
|
|
|
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
|
|
|
|
}
|