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-07-17 07:50:48 +00:00
|
|
|
"context"
|
2018-07-16 14:29:04 +00:00
|
|
|
"errors"
|
2017-04-20 09:04:17 +00:00
|
|
|
"fmt"
|
|
|
|
"net/url"
|
2018-08-22 14:41:09 +00:00
|
|
|
"os"
|
2017-12-22 10:17:13 +00:00
|
|
|
"path"
|
2017-04-20 09:04:17 +00:00
|
|
|
|
2019-03-13 16:01:08 +00:00
|
|
|
"github.com/prometheus/alertmanager/api/v2/client/general"
|
|
|
|
"github.com/prometheus/alertmanager/api/v2/models"
|
|
|
|
"github.com/prometheus/alertmanager/config"
|
2018-08-22 14:41:09 +00:00
|
|
|
amconfig "github.com/prometheus/alertmanager/config"
|
2018-07-17 07:50:48 +00:00
|
|
|
kingpin "gopkg.in/alecthomas/kingpin.v2"
|
|
|
|
|
2017-04-20 09:04:17 +00:00
|
|
|
"github.com/prometheus/alertmanager/pkg/parse"
|
|
|
|
"github.com/prometheus/common/model"
|
|
|
|
"github.com/prometheus/prometheus/pkg/labels"
|
|
|
|
)
|
|
|
|
|
|
|
|
type ByAlphabetical []labels.Matcher
|
|
|
|
|
|
|
|
func (s ByAlphabetical) Len() int { return len(s) }
|
|
|
|
func (s ByAlphabetical) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
|
|
|
|
func (s ByAlphabetical) Less(i, j int) bool {
|
|
|
|
if s[i].Name != s[j].Name {
|
|
|
|
return s[i].Name < s[j].Name
|
|
|
|
} else if s[i].Type != s[j].Type {
|
|
|
|
return s[i].Type < s[j].Type
|
|
|
|
} else if s[i].Value != s[j].Value {
|
|
|
|
return s[i].Value < s[j].Value
|
|
|
|
}
|
2017-11-01 22:08:34 +00:00
|
|
|
return false
|
2017-04-20 09:04:17 +00:00
|
|
|
}
|
|
|
|
|
2019-03-18 00:31:22 +00:00
|
|
|
// GetAlertmanagerURL appends the given path to the alertmanager base URL
|
2017-12-22 10:17:13 +00:00
|
|
|
func GetAlertmanagerURL(p string) url.URL {
|
2018-04-13 11:34:16 +00:00
|
|
|
amURL := *alertmanagerURL
|
|
|
|
amURL.Path = path.Join(alertmanagerURL.Path, p)
|
2018-03-13 13:56:20 +00:00
|
|
|
return amURL
|
2017-04-20 09:04:17 +00:00
|
|
|
}
|
|
|
|
|
2018-07-16 14:29:04 +00:00
|
|
|
// Parse a list of matchers (cli arguments)
|
|
|
|
func parseMatchers(inputMatchers []string) ([]labels.Matcher, error) {
|
2017-04-20 09:04:17 +00:00
|
|
|
matchers := make([]labels.Matcher, 0)
|
|
|
|
|
2018-07-16 14:29:04 +00:00
|
|
|
for _, v := range inputMatchers {
|
2017-04-20 09:04:17 +00:00
|
|
|
name, value, matchType, err := parse.Input(v)
|
|
|
|
if err != nil {
|
|
|
|
return []labels.Matcher{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
matchers = append(matchers, labels.Matcher{
|
|
|
|
Type: matchType,
|
|
|
|
Name: name,
|
|
|
|
Value: value,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return matchers, nil
|
|
|
|
}
|
|
|
|
|
2018-08-22 14:41:09 +00:00
|
|
|
// getRemoteAlertmanagerConfigStatus returns status responsecontaining configuration from remote Alertmanager
|
2019-03-13 16:01:08 +00:00
|
|
|
func getRemoteAlertmanagerConfigStatus(ctx context.Context, alertmanagerURL *url.URL) (*models.AlertmanagerStatus, error) {
|
|
|
|
amclient := NewAlertmanagerClient(alertmanagerURL)
|
|
|
|
params := general.NewGetStatusParams().WithContext(ctx)
|
|
|
|
getOk, err := amclient.General.GetStatus(params)
|
2018-08-22 14:41:09 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-03-13 16:01:08 +00:00
|
|
|
|
|
|
|
return getOk.Payload, nil
|
2018-08-22 14:41:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func checkRoutingConfigInputFlags(alertmanagerURL *url.URL, configFile string) {
|
|
|
|
if alertmanagerURL != nil && configFile != "" {
|
|
|
|
fmt.Fprintln(os.Stderr, "Warning: --config.file flag overrides the --alertmanager.url.")
|
|
|
|
}
|
|
|
|
if alertmanagerURL == nil && configFile == "" {
|
|
|
|
kingpin.Fatalf("You have to specify one of --config.file or --alertmanager.url flags.")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func loadAlertmanagerConfig(ctx context.Context, alertmanagerURL *url.URL, configFile string) (*amconfig.Config, error) {
|
|
|
|
checkRoutingConfigInputFlags(alertmanagerURL, configFile)
|
|
|
|
if configFile != "" {
|
|
|
|
cfg, _, err := amconfig.LoadFile(configFile)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return cfg, nil
|
|
|
|
}
|
2019-04-01 15:50:38 +00:00
|
|
|
if alertmanagerURL == nil {
|
|
|
|
return nil, errors.New("failed to get Alertmanager configuration")
|
|
|
|
}
|
|
|
|
configStatus, err := getRemoteAlertmanagerConfigStatus(ctx, alertmanagerURL)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2018-08-22 14:41:09 +00:00
|
|
|
}
|
2019-04-01 15:50:38 +00:00
|
|
|
return config.Load(*configStatus.Config.Original)
|
2018-08-22 14:41:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// convertClientToCommonLabelSet converts client.LabelSet to model.Labelset
|
2019-03-13 16:01:08 +00:00
|
|
|
func convertClientToCommonLabelSet(cls models.LabelSet) model.LabelSet {
|
2018-08-22 14:41:09 +00:00
|
|
|
mls := make(model.LabelSet, len(cls))
|
|
|
|
for ln, lv := range cls {
|
|
|
|
mls[model.LabelName(ln)] = model.LabelValue(lv)
|
|
|
|
}
|
|
|
|
return mls
|
|
|
|
}
|
|
|
|
|
2018-07-16 14:29:04 +00:00
|
|
|
// Parse a list of labels (cli arguments)
|
2019-03-13 16:01:08 +00:00
|
|
|
func parseLabels(inputLabels []string) (models.LabelSet, error) {
|
|
|
|
labelSet := make(models.LabelSet, len(inputLabels))
|
2018-07-16 14:29:04 +00:00
|
|
|
|
|
|
|
for _, l := range inputLabels {
|
|
|
|
name, value, matchType, err := parse.Input(l)
|
|
|
|
if err != nil {
|
2019-03-13 16:01:08 +00:00
|
|
|
return models.LabelSet{}, err
|
2018-07-16 14:29:04 +00:00
|
|
|
}
|
|
|
|
if matchType != labels.MatchEqual {
|
2019-03-13 16:01:08 +00:00
|
|
|
return models.LabelSet{}, errors.New("labels must be specified as key=value pairs")
|
2018-07-16 14:29:04 +00:00
|
|
|
}
|
|
|
|
|
2019-03-13 16:01:08 +00:00
|
|
|
labelSet[name] = value
|
2018-07-16 14:29:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return labelSet, nil
|
|
|
|
}
|
|
|
|
|
2019-03-18 00:31:22 +00:00
|
|
|
// TypeMatchers only valid for when you are going to add a silence
|
2019-03-13 16:01:08 +00:00
|
|
|
func TypeMatchers(matchers []labels.Matcher) (models.Matchers, error) {
|
|
|
|
typeMatchers := models.Matchers{}
|
2017-04-20 09:04:17 +00:00
|
|
|
for _, matcher := range matchers {
|
|
|
|
typeMatcher, err := TypeMatcher(matcher)
|
|
|
|
if err != nil {
|
2019-03-13 16:01:08 +00:00
|
|
|
return models.Matchers{}, err
|
2017-04-20 09:04:17 +00:00
|
|
|
}
|
|
|
|
typeMatchers = append(typeMatchers, &typeMatcher)
|
|
|
|
}
|
|
|
|
return typeMatchers, nil
|
|
|
|
}
|
|
|
|
|
2019-03-18 00:31:22 +00:00
|
|
|
// TypeMatcher only valid for when you are going to add a silence
|
2017-04-20 09:04:17 +00:00
|
|
|
// Doesn't allow negative operators
|
2019-03-13 16:01:08 +00:00
|
|
|
func TypeMatcher(matcher labels.Matcher) (models.Matcher, error) {
|
|
|
|
name := matcher.Name
|
|
|
|
value := matcher.Value
|
|
|
|
typeMatcher := models.Matcher{
|
|
|
|
Name: &name,
|
|
|
|
Value: &value,
|
|
|
|
}
|
2017-04-20 09:04:17 +00:00
|
|
|
|
2019-03-13 16:01:08 +00:00
|
|
|
isRegex := false
|
2017-04-20 09:04:17 +00:00
|
|
|
switch matcher.Type {
|
|
|
|
case labels.MatchEqual:
|
2019-03-13 16:01:08 +00:00
|
|
|
isRegex = false
|
2017-04-20 09:04:17 +00:00
|
|
|
case labels.MatchRegexp:
|
2019-03-13 16:01:08 +00:00
|
|
|
isRegex = true
|
2017-04-20 09:04:17 +00:00
|
|
|
default:
|
2019-03-13 16:01:08 +00:00
|
|
|
return models.Matcher{}, fmt.Errorf("invalid match type for creation operation: %s", matcher.Type)
|
2017-04-20 09:04:17 +00:00
|
|
|
}
|
2019-03-13 16:01:08 +00:00
|
|
|
typeMatcher.IsRegex = &isRegex
|
|
|
|
return typeMatcher, nil
|
2017-04-20 09:04:17 +00:00
|
|
|
}
|
2018-07-17 07:50:48 +00:00
|
|
|
|
|
|
|
// Helper function for adding the ctx with timeout into an action.
|
|
|
|
func execWithTimeout(fn func(context.Context, *kingpin.ParseContext) error) func(*kingpin.ParseContext) error {
|
|
|
|
return func(x *kingpin.ParseContext) error {
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), timeout)
|
|
|
|
defer cancel()
|
|
|
|
return fn(ctx, x)
|
|
|
|
}
|
|
|
|
}
|