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"
|
|
|
|
|
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"
|
2018-04-24 07:35:15 +00:00
|
|
|
"gopkg.in/alecthomas/kingpin.v2"
|
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
|
|
|
|
}
|
|
|
|
|
2018-04-24 07:35:15 +00:00
|
|
|
const silenceAddHelp = `Add a new alertmanager silence
|
2017-04-20 09:04:17 +00:00
|
|
|
|
2018-04-24 07:35:15 +00:00
|
|
|
Amtool uses a simplified Prometheus syntax to represent silences. The
|
2017-04-20 09:04:17 +00:00
|
|
|
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
|
|
|
|
|
2018-04-24 07:35:15 +00:00
|
|
|
If alertname is omitted and the first argument does not contain a '=' or a
|
2017-04-20 09:04:17 +00:00
|
|
|
'=~' 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
|
2018-04-24 07:35:15 +00:00
|
|
|
(similar to Prometheus) is used to represent a regex match. Regex matching
|
2017-04-20 09:04:17 +00:00
|
|
|
can be used in combination with a direct match.
|
2017-12-22 10:17:13 +00:00
|
|
|
`
|
2018-04-24 07:35:15 +00:00
|
|
|
|
|
|
|
func configureSilenceAddCmd(cc *kingpin.CmdClause) {
|
|
|
|
var (
|
|
|
|
c = &silenceAddCmd{}
|
|
|
|
addCmd = cc.Command("add", silenceAddHelp)
|
|
|
|
)
|
|
|
|
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-04-20 09:04:17 +00:00
|
|
|
}
|
|
|
|
|
2018-04-24 07:35:15 +00:00
|
|
|
func (c *silenceAddCmd) add(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
|
|
|
}
|