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-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"
|
2023-10-19 11:00:01 +00:00
|
|
|
"strconv"
|
2017-04-20 09:04:17 +00:00
|
|
|
"time"
|
|
|
|
|
2023-03-01 12:47:50 +00:00
|
|
|
"github.com/alecthomas/kingpin/v2"
|
2019-03-13 16:01:08 +00:00
|
|
|
"github.com/go-openapi/strfmt"
|
2018-01-09 09:47:41 +00:00
|
|
|
"github.com/prometheus/common/model"
|
2017-04-20 09:04:17 +00:00
|
|
|
|
2019-03-13 16:01:08 +00:00
|
|
|
"github.com/prometheus/alertmanager/api/v2/client/silence"
|
|
|
|
"github.com/prometheus/alertmanager/api/v2/models"
|
2023-10-19 11:00:01 +00:00
|
|
|
"github.com/prometheus/alertmanager/matchers/compat"
|
|
|
|
"github.com/prometheus/alertmanager/pkg/labels"
|
2018-04-11 09:17:41 +00:00
|
|
|
)
|
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)
|
2018-08-23 14:17:27 +00:00
|
|
|
addCmd.Flag("start", "Set when the silence should start. RFC3339 format 2006-01-02T15:04:05-07:00").StringVar(&c.start)
|
|
|
|
addCmd.Flag("end", "Set when the silence should end (overwrites duration). RFC3339 format 2006-01-02T15:04:05-07:00").StringVar(&c.end)
|
2018-04-24 07:35:15 +00:00
|
|
|
addCmd.Flag("comment", "A comment to help describe the silence").Short('c').StringVar(&c.comment)
|
|
|
|
addCmd.Arg("matcher-groups", "Query filter").StringsVar(&c.matchers)
|
2018-07-17 07:50:48 +00:00
|
|
|
addCmd.Action(execWithTimeout(c.add))
|
2017-04-20 09:04:17 +00:00
|
|
|
}
|
|
|
|
|
2018-07-17 07:50:48 +00:00
|
|
|
func (c *silenceAddCmd) add(ctx context.Context, _ *kingpin.ParseContext) error {
|
2017-04-20 09:04:17 +00:00
|
|
|
var err error
|
|
|
|
|
2019-01-07 12:49:41 +00:00
|
|
|
if len(c.matchers) > 0 {
|
|
|
|
// If the parser fails then we likely don't have a (=|=~|!=|!~) so lets
|
|
|
|
// assume that the user wants alertname=<arg> and prepend `alertname=`
|
|
|
|
// to the front.
|
2023-10-19 11:00:01 +00:00
|
|
|
_, err := compat.Matcher(c.matchers[0])
|
2019-01-07 12:49:41 +00:00
|
|
|
if err != nil {
|
2023-10-19 11:00:01 +00:00
|
|
|
c.matchers[0] = fmt.Sprintf("alertname=%s", strconv.Quote(c.matchers[0]))
|
2019-01-07 12:49:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-19 11:00:01 +00:00
|
|
|
matchers := make([]labels.Matcher, 0, len(c.matchers))
|
|
|
|
for _, s := range c.matchers {
|
|
|
|
m, err := compat.Matcher(s)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
matchers = append(matchers, *m)
|
2017-04-20 09:04:17 +00:00
|
|
|
}
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2021-10-18 13:13:42 +00:00
|
|
|
var startsAt time.Time
|
|
|
|
if c.start != "" {
|
|
|
|
startsAt, err = time.Parse(time.RFC3339, c.start)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
startsAt = time.Now().UTC()
|
|
|
|
}
|
|
|
|
|
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")
|
|
|
|
}
|
2021-10-18 13:13:42 +00:00
|
|
|
endsAt = startsAt.UTC().Add(time.Duration(d))
|
2018-03-29 10:11:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if startsAt.After(endsAt) {
|
|
|
|
return errors.New("silence cannot start after it ends")
|
|
|
|
}
|
|
|
|
|
2021-10-18 13:13:42 +00:00
|
|
|
if c.requireComment && c.comment == "" {
|
|
|
|
return errors.New("comment required by config")
|
|
|
|
}
|
|
|
|
|
2019-03-13 16:01:08 +00:00
|
|
|
start := strfmt.DateTime(startsAt)
|
|
|
|
end := strfmt.DateTime(endsAt)
|
|
|
|
ps := &models.PostableSilence{
|
|
|
|
Silence: models.Silence{
|
2021-01-29 21:52:13 +00:00
|
|
|
Matchers: TypeMatchers(matchers),
|
2019-03-13 16:01:08 +00:00
|
|
|
StartsAt: &start,
|
|
|
|
EndsAt: &end,
|
|
|
|
CreatedBy: &c.author,
|
|
|
|
Comment: &c.comment,
|
|
|
|
},
|
2017-04-20 09:04:17 +00:00
|
|
|
}
|
2019-03-13 16:01:08 +00:00
|
|
|
silenceParams := silence.NewPostSilencesParams().WithContext(ctx).
|
|
|
|
WithSilence(ps)
|
2017-04-20 09:04:17 +00:00
|
|
|
|
2019-03-13 16:01:08 +00:00
|
|
|
amclient := NewAlertmanagerClient(alertmanagerURL)
|
|
|
|
|
|
|
|
postOk, err := amclient.Silence.PostSilences(silenceParams)
|
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
|
|
|
}
|
2019-03-25 14:23:37 +00:00
|
|
|
_, err = fmt.Println(postOk.Payload.SilenceID)
|
2018-04-11 09:17:41 +00:00
|
|
|
return err
|
2017-04-20 09:04:17 +00:00
|
|
|
}
|