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-13 11:34:16 +00:00
|
|
|
"net/url"
|
2017-04-20 09:04:17 +00:00
|
|
|
"os"
|
2019-06-25 12:22:29 +00:00
|
|
|
"path"
|
2018-07-17 07:50:48 +00:00
|
|
|
"time"
|
2017-04-20 09:04:17 +00:00
|
|
|
|
2019-03-13 16:01:08 +00:00
|
|
|
"github.com/go-openapi/strfmt"
|
2017-12-22 10:17:13 +00:00
|
|
|
"github.com/prometheus/common/version"
|
2019-03-13 16:01:08 +00:00
|
|
|
kingpin "gopkg.in/alecthomas/kingpin.v2"
|
2018-04-24 07:35:15 +00:00
|
|
|
|
2019-03-13 16:01:08 +00:00
|
|
|
"github.com/prometheus/alertmanager/api/v2/client"
|
2018-04-24 07:35:15 +00:00
|
|
|
"github.com/prometheus/alertmanager/cli/config"
|
|
|
|
"github.com/prometheus/alertmanager/cli/format"
|
2019-05-16 03:23:00 +00:00
|
|
|
|
|
|
|
clientruntime "github.com/go-openapi/runtime/client"
|
2017-04-20 09:04:17 +00:00
|
|
|
)
|
|
|
|
|
2017-12-22 10:17:13 +00:00
|
|
|
var (
|
2018-04-13 11:34:16 +00:00
|
|
|
verbose bool
|
|
|
|
alertmanagerURL *url.URL
|
|
|
|
output string
|
2018-07-17 07:50:48 +00:00
|
|
|
timeout time.Duration
|
2018-04-24 07:35:15 +00:00
|
|
|
|
|
|
|
configFiles = []string{os.ExpandEnv("$HOME/.config/amtool/config.yml"), "/etc/amtool/config.yml"}
|
|
|
|
legacyFlags = map[string]string{"comment_required": "require-comment"}
|
2017-12-22 10:17:13 +00:00
|
|
|
)
|
2017-04-20 09:04:17 +00:00
|
|
|
|
2018-04-24 07:35:15 +00:00
|
|
|
func requireAlertManagerURL(pc *kingpin.ParseContext) error {
|
|
|
|
// Return without error if any help flag is set.
|
|
|
|
for _, elem := range pc.Elements {
|
|
|
|
f, ok := elem.Clause.(*kingpin.FlagClause)
|
|
|
|
if !ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
name := f.Model().Name
|
|
|
|
if name == "help" || name == "help-long" || name == "help-man" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if alertmanagerURL == nil {
|
|
|
|
kingpin.Fatalf("required flag --alertmanager.url not provided")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-03-13 16:01:08 +00:00
|
|
|
const (
|
|
|
|
defaultAmHost = "localhost"
|
|
|
|
defaultAmPort = "9093"
|
|
|
|
defaultAmApiv2path = "/api/v2"
|
|
|
|
)
|
|
|
|
|
|
|
|
// NewAlertmanagerClient initializes an alertmanager client with the given URL
|
|
|
|
func NewAlertmanagerClient(amURL *url.URL) *client.Alertmanager {
|
2019-05-16 03:23:00 +00:00
|
|
|
address := defaultAmHost + ":" + defaultAmPort
|
|
|
|
schemes := []string{"http"}
|
|
|
|
|
|
|
|
if amURL.Host != "" {
|
|
|
|
address = amURL.Host // URL documents host as host or host:port
|
|
|
|
}
|
|
|
|
if amURL.Scheme != "" {
|
|
|
|
schemes = []string{amURL.Scheme}
|
|
|
|
}
|
|
|
|
|
2019-06-25 12:22:29 +00:00
|
|
|
cr := clientruntime.New(address, path.Join(amURL.Path, defaultAmApiv2path), schemes)
|
2019-03-13 16:01:08 +00:00
|
|
|
|
2019-05-16 03:23:00 +00:00
|
|
|
if amURL.User != nil {
|
|
|
|
password, _ := amURL.User.Password()
|
|
|
|
cr.DefaultAuthentication = clientruntime.BasicAuth(amURL.User.Username(), password)
|
2019-03-13 16:01:08 +00:00
|
|
|
}
|
2019-05-16 03:23:00 +00:00
|
|
|
|
|
|
|
return client.New(cr, strfmt.Default)
|
2019-03-13 16:01:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Execute is the main function for the amtool command
|
2018-04-13 11:34:16 +00:00
|
|
|
func Execute() {
|
|
|
|
var (
|
2020-01-22 13:53:19 +00:00
|
|
|
app = kingpin.New("amtool", helpRoot)
|
2018-04-13 11:34:16 +00:00
|
|
|
)
|
2017-06-02 20:51:44 +00:00
|
|
|
|
2018-04-13 11:34:16 +00:00
|
|
|
format.InitFormatFlags(app)
|
2017-12-22 10:17:13 +00:00
|
|
|
|
2018-04-13 11:34:16 +00:00
|
|
|
app.Flag("verbose", "Verbose running information").Short('v').BoolVar(&verbose)
|
2018-04-24 07:35:15 +00:00
|
|
|
app.Flag("alertmanager.url", "Alertmanager to talk to").URLVar(&alertmanagerURL)
|
2018-04-13 11:34:16 +00:00
|
|
|
app.Flag("output", "Output formatter (simple, extended, json)").Short('o').Default("simple").EnumVar(&output, "simple", "extended", "json")
|
2018-07-17 07:50:48 +00:00
|
|
|
app.Flag("timeout", "Timeout for the executed command").Default("30s").DurationVar(&timeout)
|
|
|
|
|
2018-04-13 11:34:16 +00:00
|
|
|
app.Version(version.Print("amtool"))
|
|
|
|
app.GetFlag("help").Short('h')
|
|
|
|
app.UsageTemplate(kingpin.CompactUsageTemplate)
|
2017-12-22 10:17:13 +00:00
|
|
|
|
2018-04-24 07:35:15 +00:00
|
|
|
resolver, err := config.NewResolver(configFiles, legacyFlags)
|
2018-04-13 11:34:16 +00:00
|
|
|
if err != nil {
|
|
|
|
kingpin.Fatalf("could not load config file: %v\n", err)
|
2017-12-22 10:17:13 +00:00
|
|
|
}
|
|
|
|
|
2018-04-24 07:35:15 +00:00
|
|
|
configureAlertCmd(app)
|
|
|
|
configureSilenceCmd(app)
|
|
|
|
configureCheckConfigCmd(app)
|
2020-05-18 13:25:15 +00:00
|
|
|
configureClusterCmd(app)
|
2018-04-24 07:35:15 +00:00
|
|
|
configureConfigCmd(app)
|
2018-04-13 11:34:16 +00:00
|
|
|
|
2018-04-24 07:35:15 +00:00
|
|
|
err = resolver.Bind(app, os.Args[1:])
|
2018-04-13 11:34:16 +00:00
|
|
|
if err != nil {
|
|
|
|
kingpin.Fatalf("%v\n", err)
|
2017-12-22 10:17:13 +00:00
|
|
|
}
|
2018-04-13 11:34:16 +00:00
|
|
|
|
2018-04-24 07:35:15 +00:00
|
|
|
_, err = app.Parse(os.Args[1:])
|
|
|
|
if err != nil {
|
|
|
|
kingpin.Fatalf("%v\n", err)
|
|
|
|
}
|
2017-12-22 10:17:13 +00:00
|
|
|
}
|
|
|
|
|
2018-04-13 11:34:16 +00:00
|
|
|
const (
|
2018-04-24 07:35:15 +00:00
|
|
|
helpRoot = `View and modify the current Alertmanager state.
|
2017-12-22 10:17:13 +00:00
|
|
|
|
|
|
|
Config File:
|
|
|
|
The alertmanager tool will read a config file in YAML format from one of two
|
|
|
|
default config locations: $HOME/.config/amtool/config.yml or
|
|
|
|
/etc/amtool/config.yml
|
|
|
|
|
|
|
|
All flags can be given in the config file, but the following are the suited for
|
|
|
|
static configuration:
|
2017-04-20 09:04:17 +00:00
|
|
|
|
|
|
|
alertmanager.url
|
|
|
|
Set a default alertmanager url for each request
|
|
|
|
|
|
|
|
author
|
2017-12-22 10:17:13 +00:00
|
|
|
Set a default author value for new silences. If this argument is not
|
|
|
|
specified then the username will be used
|
2017-04-20 09:04:17 +00:00
|
|
|
|
2017-12-22 10:17:13 +00:00
|
|
|
require-comment
|
|
|
|
Bool, whether to require a comment on silence creation. Defaults to true
|
2017-04-20 09:04:17 +00:00
|
|
|
|
|
|
|
output
|
|
|
|
Set a default output type. Options are (simple, extended, json)
|
|
|
|
|
2017-12-22 10:17:13 +00:00
|
|
|
date.format
|
|
|
|
Sets the output format for dates. Defaults to "2006-01-02 15:04:05 MST"
|
|
|
|
`
|
2018-04-13 11:34:16 +00:00
|
|
|
)
|