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"
|
|
|
|
"fmt"
|
|
|
|
"os/user"
|
|
|
|
"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"
|
2018-01-09 09:47:41 +00:00
|
|
|
"github.com/prometheus/common/model"
|
2017-04-20 09:04:17 +00:00
|
|
|
|
2018-04-11 09:17:41 +00:00
|
|
|
"github.com/prometheus/alertmanager/client"
|
|
|
|
"github.com/prometheus/alertmanager/types"
|
|
|
|
)
|
2017-04-20 09:04:17 +00:00
|
|
|
|
2017-12-22 10:17:13 +00:00
|
|
|
func username() string {
|
|
|
|
user, err := user.Current()
|
|
|
|
if err != nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
return user.Username
|
|
|
|
}
|
|
|
|
|
2018-04-13 11:34:16 +00:00
|
|
|
type silenceAddCmd struct {
|
|
|
|
author string
|
|
|
|
requireComment bool
|
|
|
|
duration string
|
|
|
|
start string
|
|
|
|
end string
|
|
|
|
comment string
|
|
|
|
matchers []string
|
|
|
|
}
|
|
|
|
|
|
|
|
func configureSilenceAddCmd(cc *kingpin.CmdClause, longHelpText map[string]string) {
|
|
|
|
var (
|
|
|
|
c = &silenceAddCmd{}
|
|
|
|
addCmd = cc.Command("add", "Add a new alertmanager silence")
|
|
|
|
)
|
|
|
|
addCmd.Flag("author", "Username for CreatedBy field").Short('a').Default(username()).StringVar(&c.author)
|
|
|
|
addCmd.Flag("require-comment", "Require comment to be set").Hidden().Default("true").BoolVar(&c.requireComment)
|
|
|
|
addCmd.Flag("duration", "Duration of silence").Short('d').Default("1h").StringVar(&c.duration)
|
|
|
|
addCmd.Flag("start", "Set when the silence should start. RFC3339 format 2006-01-02T15:04:05Z07:00").StringVar(&c.start)
|
|
|
|
addCmd.Flag("end", "Set when the silence should end (overwrites duration). RFC3339 format 2006-01-02T15:04:05Z07:00").StringVar(&c.end)
|
|
|
|
addCmd.Flag("comment", "A comment to help describe the silence").Short('c').StringVar(&c.comment)
|
|
|
|
addCmd.Arg("matcher-groups", "Query filter").StringsVar(&c.matchers)
|
|
|
|
addCmd.Action(c.add)
|
2017-12-22 10:17:13 +00:00
|
|
|
|
|
|
|
longHelpText["silence add"] = `Add a new alertmanager silence
|
2017-04-20 09:04:17 +00:00
|
|
|
|
|
|
|
Amtool uses a simplified prometheus syntax to represent silences. The
|
|
|
|
non-option section of arguments constructs a list of "Matcher Groups"
|
|
|
|
that will be used to create a number of silences. The following examples
|
|
|
|
will attempt to show this behaviour in action:
|
|
|
|
|
|
|
|
amtool silence add alertname=foo node=bar
|
|
|
|
|
|
|
|
This statement will add a silence that matches alerts with the
|
|
|
|
alertname=foo and node=bar label value pairs set.
|
|
|
|
|
|
|
|
amtool silence add foo node=bar
|
|
|
|
|
|
|
|
If alertname is ommited and the first argument does not contain a '=' or a
|
|
|
|
'=~' then it will be assumed to be the value of the alertname pair.
|
|
|
|
|
|
|
|
amtool silence add 'alertname=~foo.*'
|
|
|
|
|
|
|
|
As well as direct equality, regex matching is also supported. The '=~' syntax
|
|
|
|
(similar to prometheus) is used to represent a regex match. Regex matching
|
|
|
|
can be used in combination with a direct match.
|
2017-12-22 10:17:13 +00:00
|
|
|
`
|
2017-04-20 09:04:17 +00:00
|
|
|
}
|
|
|
|
|
2018-04-13 11:34:16 +00:00
|
|
|
func (c *silenceAddCmd) add(element *kingpin.ParseElement, ctx *kingpin.ParseContext) error {
|
2017-04-20 09:04:17 +00:00
|
|
|
var err error
|
|
|
|
|
2018-04-13 11:34:16 +00:00
|
|
|
matchers, err := parseMatchers(c.matchers)
|
2017-04-20 09:04:17 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(matchers) < 1 {
|
2017-11-12 16:43:48 +00:00
|
|
|
return fmt.Errorf("no matchers specified")
|
2017-04-20 09:04:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var endsAt time.Time
|
2018-04-13 11:34:16 +00:00
|
|
|
if c.end != "" {
|
|
|
|
endsAt, err = time.Parse(time.RFC3339, c.end)
|
2017-04-20 09:04:17 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else {
|
2018-04-13 11:34:16 +00:00
|
|
|
d, err := model.ParseDuration(c.duration)
|
2018-01-09 09:47:41 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-03-29 10:11:31 +00:00
|
|
|
if d == 0 {
|
2018-01-09 09:47:41 +00:00
|
|
|
return fmt.Errorf("silence duration must be greater than 0")
|
|
|
|
}
|
2018-03-29 10:11:31 +00:00
|
|
|
endsAt = time.Now().UTC().Add(time.Duration(d))
|
2017-04-20 09:04:17 +00:00
|
|
|
}
|
|
|
|
|
2018-04-13 11:34:16 +00:00
|
|
|
if c.requireComment && c.comment == "" {
|
2017-11-12 16:43:48 +00:00
|
|
|
return errors.New("comment required by config")
|
2017-04-20 09:04:17 +00:00
|
|
|
}
|
|
|
|
|
2018-03-29 10:11:31 +00:00
|
|
|
var startsAt time.Time
|
2018-04-13 11:34:16 +00:00
|
|
|
if c.start != "" {
|
|
|
|
startsAt, err = time.Parse(time.RFC3339, c.start)
|
2018-03-29 10:11:31 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
startsAt = time.Now().UTC()
|
|
|
|
}
|
|
|
|
|
|
|
|
if startsAt.After(endsAt) {
|
|
|
|
return errors.New("silence cannot start after it ends")
|
|
|
|
}
|
|
|
|
|
2017-04-20 09:04:17 +00:00
|
|
|
typeMatchers, err := TypeMatchers(matchers)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
silence := types.Silence{
|
|
|
|
Matchers: typeMatchers,
|
2018-03-29 10:11:31 +00:00
|
|
|
StartsAt: startsAt,
|
2017-04-20 09:04:17 +00:00
|
|
|
EndsAt: endsAt,
|
2018-04-13 11:34:16 +00:00
|
|
|
CreatedBy: c.author,
|
|
|
|
Comment: c.comment,
|
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()})
|
2017-04-20 09:04:17 +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
|
|
|
silenceID, err := silenceAPI.Set(context.Background(), silence)
|
2017-04-20 09:04:17 +00:00
|
|
|
if err != nil {
|
2018-04-11 09:17:41 +00:00
|
|
|
return err
|
2017-04-20 09:04:17 +00:00
|
|
|
}
|
2017-12-07 12:12:00 +00:00
|
|
|
|
2018-04-11 09:17:41 +00:00
|
|
|
_, err = fmt.Println(silenceID)
|
|
|
|
return err
|
2017-04-20 09:04:17 +00:00
|
|
|
}
|