2017-04-20 09:04:17 +00:00
|
|
|
package cli
|
|
|
|
|
|
|
|
import (
|
2018-04-11 09:17:41 +00:00
|
|
|
"context"
|
2017-04-20 09:04:17 +00:00
|
|
|
"errors"
|
|
|
|
|
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"
|
|
|
|
|
|
|
|
"github.com/prometheus/alertmanager/client"
|
2017-04-20 09:04:17 +00:00
|
|
|
)
|
|
|
|
|
2018-04-13 11:34:16 +00:00
|
|
|
type silenceExpireCmd struct {
|
|
|
|
ids []string
|
|
|
|
}
|
|
|
|
|
|
|
|
func configureSilenceExpireCmd(cc *kingpin.CmdClause, longHelpText map[string]string) {
|
|
|
|
var (
|
|
|
|
c = &silenceExpireCmd{}
|
|
|
|
expireCmd = cc.Command("expire", "expire an alertmanager silence")
|
|
|
|
)
|
|
|
|
expireCmd.Arg("silence-ids", "Ids of silences to expire").StringsVar(&c.ids)
|
2017-04-20 09:04:17 +00:00
|
|
|
|
2018-04-13 11:34:16 +00:00
|
|
|
expireCmd.Action(c.expire)
|
2017-12-22 10:17:13 +00:00
|
|
|
longHelpText["silence expire"] = `Expire an alertmanager silence`
|
|
|
|
}
|
2017-04-20 09:04:17 +00:00
|
|
|
|
2018-04-13 11:34:16 +00:00
|
|
|
func (c *silenceExpireCmd) expire(element *kingpin.ParseElement, ctx *kingpin.ParseContext) error {
|
|
|
|
if len(c.ids) < 1 {
|
2017-11-12 16:43:48 +00:00
|
|
|
return errors.New("no silence IDs specified")
|
2017-04-20 09:04:17 +00:00
|
|
|
}
|
|
|
|
|
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)
|
2017-04-20 09:04:17 +00:00
|
|
|
|
2018-04-13 11:34:16 +00:00
|
|
|
for _, id := range c.ids {
|
2018-04-11 10:30:18 +00:00
|
|
|
err := silenceAPI.Expire(context.Background(), id)
|
2017-04-20 09:04:17 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2018-04-11 09:17:41 +00:00
|
|
|
|
2017-04-20 09:04:17 +00:00
|
|
|
return nil
|
|
|
|
}
|