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-04-20 09:04:17 +00:00
|
|
|
|
2023-10-19 11:00:01 +00:00
|
|
|
"github.com/alecthomas/kingpin/v2"
|
2019-09-16 08:56:29 +00:00
|
|
|
"github.com/prometheus/common/model"
|
|
|
|
|
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"
|
2019-09-16 08:56:29 +00:00
|
|
|
"github.com/prometheus/alertmanager/pkg/labels"
|
2017-04-20 09:04:17 +00:00
|
|
|
)
|
|
|
|
|
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.")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-02 14:58:01 +00:00
|
|
|
func loadAlertmanagerConfig(ctx context.Context, alertmanagerURL *url.URL, configFile string) (*config.Config, error) {
|
2018-08-22 14:41:09 +00:00
|
|
|
checkRoutingConfigInputFlags(alertmanagerURL, configFile)
|
|
|
|
if configFile != "" {
|
2019-08-26 12:01:40 +00:00
|
|
|
cfg, err := config.LoadFile(configFile)
|
2018-08-22 14:41:09 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2019-03-18 00:31:22 +00:00
|
|
|
// TypeMatchers only valid for when you are going to add a silence
|
2021-01-29 21:52:13 +00:00
|
|
|
func TypeMatchers(matchers []labels.Matcher) models.Matchers {
|
|
|
|
typeMatchers := make(models.Matchers, len(matchers))
|
|
|
|
for i, matcher := range matchers {
|
|
|
|
typeMatchers[i] = TypeMatcher(matcher)
|
2017-04-20 09:04:17 +00:00
|
|
|
}
|
2021-01-29 21:52:13 +00:00
|
|
|
return typeMatchers
|
2017-04-20 09:04:17 +00:00
|
|
|
}
|
|
|
|
|
2019-03-18 00:31:22 +00:00
|
|
|
// TypeMatcher only valid for when you are going to add a silence
|
2021-01-29 21:52:13 +00:00
|
|
|
func TypeMatcher(matcher labels.Matcher) *models.Matcher {
|
2019-03-13 16:01:08 +00:00
|
|
|
name := matcher.Name
|
|
|
|
value := matcher.Value
|
|
|
|
typeMatcher := models.Matcher{
|
|
|
|
Name: &name,
|
|
|
|
Value: &value,
|
|
|
|
}
|
2017-04-20 09:04:17 +00:00
|
|
|
|
2021-01-29 21:52:13 +00:00
|
|
|
isEqual := (matcher.Type == labels.MatchEqual) || (matcher.Type == labels.MatchRegexp)
|
|
|
|
isRegex := (matcher.Type == labels.MatchRegexp) || (matcher.Type == labels.MatchNotRegexp)
|
|
|
|
typeMatcher.IsEqual = &isEqual
|
2019-03-13 16:01:08 +00:00
|
|
|
typeMatcher.IsRegex = &isRegex
|
2021-01-29 21:52:13 +00:00
|
|
|
return &typeMatcher
|
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)
|
|
|
|
}
|
|
|
|
}
|