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-07 12:12:00 +00:00
|
|
|
package cli
|
|
|
|
|
|
|
|
import (
|
2018-04-11 09:17:41 +00:00
|
|
|
"context"
|
2017-12-07 12:12:00 +00:00
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
2018-04-11 09:17:41 +00:00
|
|
|
"strings"
|
2017-12-22 10:17:13 +00:00
|
|
|
"sync"
|
2017-12-07 12:12:00 +00:00
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
2019-03-13 16:01:08 +00:00
|
|
|
kingpin "gopkg.in/alecthomas/kingpin.v2"
|
2018-04-11 09:17:41 +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"
|
2017-12-07 12:12:00 +00:00
|
|
|
)
|
|
|
|
|
2018-04-13 11:34:16 +00:00
|
|
|
type silenceImportCmd struct {
|
|
|
|
force bool
|
|
|
|
workers int
|
|
|
|
file string
|
|
|
|
}
|
|
|
|
|
2018-04-24 07:35:15 +00:00
|
|
|
const silenceImportHelp = `Import alertmanager silences from JSON file or stdin
|
|
|
|
|
|
|
|
This command can be used to bulk import silences from a JSON file
|
|
|
|
created by query command. For example:
|
|
|
|
|
|
|
|
amtool silence query -o json foo > foo.json
|
2018-04-24 09:46:24 +00:00
|
|
|
|
2018-04-24 07:35:15 +00:00
|
|
|
amtool silence import foo.json
|
|
|
|
|
|
|
|
JSON data can also come from stdin if no param is specified.
|
|
|
|
`
|
|
|
|
|
|
|
|
func configureSilenceImportCmd(cc *kingpin.CmdClause) {
|
2018-04-13 11:34:16 +00:00
|
|
|
var (
|
|
|
|
c = &silenceImportCmd{}
|
2018-04-24 07:35:15 +00:00
|
|
|
importCmd = cc.Command("import", silenceImportHelp)
|
2018-04-13 11:34:16 +00:00
|
|
|
)
|
2017-12-07 12:12:00 +00:00
|
|
|
|
2018-04-13 11:34:16 +00:00
|
|
|
importCmd.Flag("force", "Force adding new silences even if it already exists").Short('f').BoolVar(&c.force)
|
|
|
|
importCmd.Flag("worker", "Number of concurrent workers to use for import").Short('w').Default("8").IntVar(&c.workers)
|
|
|
|
importCmd.Arg("input-file", "JSON file with silences").ExistingFileVar(&c.file)
|
2018-07-17 07:50:48 +00:00
|
|
|
importCmd.Action(execWithTimeout(c.bulkImport))
|
2017-12-07 12:12:00 +00:00
|
|
|
}
|
|
|
|
|
2019-03-13 16:01:08 +00:00
|
|
|
func addSilenceWorker(ctx context.Context, sclient *silence.Client, silencec <-chan *models.PostableSilence, errc chan<- error) {
|
2017-12-07 12:12:00 +00:00
|
|
|
for s := range silencec {
|
2019-03-13 16:01:08 +00:00
|
|
|
params := silence.NewPostSilencesParams().WithContext(ctx)
|
|
|
|
params.Silence = s
|
|
|
|
postOk, err := sclient.PostSilences(params)
|
2018-04-11 09:17:41 +00:00
|
|
|
if err != nil && strings.Contains(err.Error(), "not found") {
|
2017-12-07 12:12:00 +00:00
|
|
|
// silence doesn't exists yet, retry to create as a new one
|
2019-03-13 16:01:08 +00:00
|
|
|
params.Silence.ID = ""
|
|
|
|
postOk, _ = sclient.PostSilences(params)
|
2017-12-07 12:12:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
2019-03-13 16:01:08 +00:00
|
|
|
fmt.Fprintf(os.Stderr, "Error adding silence id='%v': %v\n", s.ID, err)
|
2017-12-07 12:12:00 +00:00
|
|
|
} else {
|
2019-03-13 16:01:08 +00:00
|
|
|
fmt.Println(postOk.Payload.SilenceID)
|
2017-12-07 12:12:00 +00:00
|
|
|
}
|
|
|
|
errc <- err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-17 07:50:48 +00:00
|
|
|
func (c *silenceImportCmd) bulkImport(ctx context.Context, _ *kingpin.ParseContext) error {
|
2017-12-07 12:12:00 +00:00
|
|
|
input := os.Stdin
|
2017-12-22 10:17:13 +00:00
|
|
|
var err error
|
2018-04-13 11:34:16 +00:00
|
|
|
if c.file != "" {
|
|
|
|
input, err = os.Open(c.file)
|
2017-12-07 12:12:00 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer input.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
dec := json.NewDecoder(input)
|
|
|
|
// read open square bracket
|
|
|
|
_, err = dec.Token()
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "couldn't unmarshal input data, is it JSON?")
|
|
|
|
}
|
|
|
|
|
2019-03-13 16:01:08 +00:00
|
|
|
amclient := NewAlertmanagerClient(alertmanagerURL)
|
|
|
|
silencec := make(chan *models.PostableSilence, 100)
|
2017-12-07 12:12:00 +00:00
|
|
|
errc := make(chan error, 100)
|
|
|
|
var wg sync.WaitGroup
|
2018-04-13 11:34:16 +00:00
|
|
|
for w := 0; w < c.workers; w++ {
|
2018-02-28 16:42:32 +00:00
|
|
|
wg.Add(1)
|
2017-12-07 12:12:00 +00:00
|
|
|
go func() {
|
2019-03-13 16:01:08 +00:00
|
|
|
addSilenceWorker(ctx, amclient.Silence, silencec, errc)
|
2017-12-07 12:12:00 +00:00
|
|
|
wg.Done()
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
|
|
|
errCount := 0
|
|
|
|
go func() {
|
|
|
|
for err := range errc {
|
|
|
|
if err != nil {
|
|
|
|
errCount++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
count := 0
|
|
|
|
for dec.More() {
|
2019-03-13 16:01:08 +00:00
|
|
|
var s models.PostableSilence
|
2017-12-07 12:12:00 +00:00
|
|
|
err := dec.Decode(&s)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "couldn't unmarshal input data, is it JSON?")
|
|
|
|
}
|
|
|
|
|
2018-04-13 11:34:16 +00:00
|
|
|
if c.force {
|
2017-12-07 12:12:00 +00:00
|
|
|
// reset the silence ID so Alertmanager will always create new silence
|
|
|
|
s.ID = ""
|
|
|
|
}
|
|
|
|
|
|
|
|
silencec <- &s
|
|
|
|
count++
|
|
|
|
}
|
|
|
|
|
|
|
|
close(silencec)
|
|
|
|
wg.Wait()
|
|
|
|
close(errc)
|
|
|
|
|
|
|
|
if errCount > 0 {
|
|
|
|
return fmt.Errorf("couldn't import %v out of %v silences", errCount, count)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|