2015-09-25 14:14:30 +00:00
|
|
|
// Copyright 2015 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.
|
|
|
|
|
|
|
|
package config
|
|
|
|
|
|
|
|
import (
|
2017-07-12 07:25:47 +00:00
|
|
|
"encoding/json"
|
2015-09-25 14:14:30 +00:00
|
|
|
"fmt"
|
2019-08-28 13:04:40 +00:00
|
|
|
"net"
|
2018-07-26 10:39:33 +00:00
|
|
|
"net/url"
|
2022-07-18 12:25:32 +00:00
|
|
|
"os"
|
2015-10-09 09:06:04 +00:00
|
|
|
"path/filepath"
|
2015-09-25 14:14:30 +00:00
|
|
|
"regexp"
|
2021-01-15 20:11:39 +00:00
|
|
|
"sort"
|
2015-09-25 14:14:30 +00:00
|
|
|
"strings"
|
2015-11-27 14:41:22 +00:00
|
|
|
"time"
|
2015-09-25 14:14:30 +00:00
|
|
|
|
2019-08-28 13:04:40 +00:00
|
|
|
"github.com/pkg/errors"
|
2017-07-27 08:48:24 +00:00
|
|
|
commoncfg "github.com/prometheus/common/config"
|
2015-09-25 14:14:30 +00:00
|
|
|
"github.com/prometheus/common/model"
|
|
|
|
"gopkg.in/yaml.v2"
|
2021-01-15 20:11:39 +00:00
|
|
|
|
|
|
|
"github.com/prometheus/alertmanager/pkg/labels"
|
2021-02-16 10:45:21 +00:00
|
|
|
"github.com/prometheus/alertmanager/timeinterval"
|
2015-09-25 14:14:30 +00:00
|
|
|
)
|
|
|
|
|
2018-12-13 10:09:03 +00:00
|
|
|
const secretToken = "<secret>"
|
|
|
|
|
|
|
|
var secretTokenJSON string
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
b, err := json.Marshal(secretToken)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
secretTokenJSON = string(b)
|
|
|
|
}
|
|
|
|
|
2015-12-03 11:40:50 +00:00
|
|
|
// Secret is a string that must not be revealed on marshaling.
|
|
|
|
type Secret string
|
|
|
|
|
2019-01-15 17:03:45 +00:00
|
|
|
// MarshalYAML implements the yaml.Marshaler interface for Secret.
|
2015-12-03 11:40:50 +00:00
|
|
|
func (s Secret) MarshalYAML() (interface{}, error) {
|
2017-06-06 11:02:49 +00:00
|
|
|
if s != "" {
|
2018-12-13 10:09:03 +00:00
|
|
|
return secretToken, nil
|
2017-06-06 11:02:49 +00:00
|
|
|
}
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2018-07-26 10:39:33 +00:00
|
|
|
// UnmarshalYAML implements the yaml.Unmarshaler interface for Secret.
|
2017-06-06 11:02:49 +00:00
|
|
|
func (s *Secret) UnmarshalYAML(unmarshal func(interface{}) error) error {
|
|
|
|
type plain Secret
|
|
|
|
return unmarshal((*plain)(s))
|
2015-12-03 11:40:50 +00:00
|
|
|
}
|
|
|
|
|
2019-01-15 17:03:45 +00:00
|
|
|
// MarshalJSON implements the json.Marshaler interface for Secret.
|
2017-06-08 11:14:37 +00:00
|
|
|
func (s Secret) MarshalJSON() ([]byte, error) {
|
2018-12-13 10:09:03 +00:00
|
|
|
return json.Marshal(secretToken)
|
2017-06-08 11:14:37 +00:00
|
|
|
}
|
|
|
|
|
2018-10-05 17:29:52 +00:00
|
|
|
// URL is a custom type that represents an HTTP or HTTPS URL and allows validation at configuration load time.
|
2018-07-26 10:39:33 +00:00
|
|
|
type URL struct {
|
|
|
|
*url.URL
|
|
|
|
}
|
|
|
|
|
|
|
|
// Copy makes a deep-copy of the struct.
|
|
|
|
func (u *URL) Copy() *URL {
|
|
|
|
v := *u.URL
|
|
|
|
return &URL{&v}
|
|
|
|
}
|
|
|
|
|
|
|
|
// MarshalYAML implements the yaml.Marshaler interface for URL.
|
|
|
|
func (u URL) MarshalYAML() (interface{}, error) {
|
|
|
|
if u.URL != nil {
|
|
|
|
return u.URL.String(), nil
|
|
|
|
}
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// UnmarshalYAML implements the yaml.Unmarshaler interface for URL.
|
|
|
|
func (u *URL) UnmarshalYAML(unmarshal func(interface{}) error) error {
|
|
|
|
var s string
|
|
|
|
if err := unmarshal(&s); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-10-05 17:29:52 +00:00
|
|
|
urlp, err := parseURL(s)
|
2018-07-26 10:39:33 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-10-05 17:29:52 +00:00
|
|
|
u.URL = urlp.URL
|
2018-07-26 10:39:33 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// MarshalJSON implements the json.Marshaler interface for URL.
|
|
|
|
func (u URL) MarshalJSON() ([]byte, error) {
|
|
|
|
if u.URL != nil {
|
|
|
|
return json.Marshal(u.URL.String())
|
|
|
|
}
|
2021-06-02 07:11:15 +00:00
|
|
|
return []byte("null"), nil
|
2018-07-26 10:39:33 +00:00
|
|
|
}
|
|
|
|
|
2019-12-12 15:35:19 +00:00
|
|
|
// UnmarshalJSON implements the json.Marshaler interface for URL.
|
|
|
|
func (u *URL) UnmarshalJSON(data []byte) error {
|
|
|
|
var s string
|
|
|
|
if err := json.Unmarshal(data, &s); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
urlp, err := parseURL(s)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
u.URL = urlp.URL
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-07-26 10:39:33 +00:00
|
|
|
// SecretURL is a URL that must not be revealed on marshaling.
|
|
|
|
type SecretURL URL
|
|
|
|
|
|
|
|
// MarshalYAML implements the yaml.Marshaler interface for SecretURL.
|
|
|
|
func (s SecretURL) MarshalYAML() (interface{}, error) {
|
|
|
|
if s.URL != nil {
|
2018-12-13 10:09:03 +00:00
|
|
|
return secretToken, nil
|
2018-07-26 10:39:33 +00:00
|
|
|
}
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// UnmarshalYAML implements the yaml.Unmarshaler interface for SecretURL.
|
|
|
|
func (s *SecretURL) UnmarshalYAML(unmarshal func(interface{}) error) error {
|
2018-12-13 10:09:03 +00:00
|
|
|
var str string
|
|
|
|
if err := unmarshal(&str); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// In order to deserialize a previously serialized configuration (eg from
|
|
|
|
// the Alertmanager API with amtool), `<secret>` needs to be treated
|
|
|
|
// specially, as it isn't a valid URL.
|
|
|
|
if str == secretToken {
|
|
|
|
s.URL = &url.URL{}
|
|
|
|
return nil
|
|
|
|
}
|
2018-07-26 10:39:33 +00:00
|
|
|
return unmarshal((*URL)(s))
|
|
|
|
}
|
|
|
|
|
|
|
|
// MarshalJSON implements the json.Marshaler interface for SecretURL.
|
|
|
|
func (s SecretURL) MarshalJSON() ([]byte, error) {
|
2018-12-13 10:09:03 +00:00
|
|
|
return json.Marshal(secretToken)
|
2018-07-26 10:39:33 +00:00
|
|
|
}
|
|
|
|
|
2019-12-12 15:35:19 +00:00
|
|
|
// UnmarshalJSON implements the json.Marshaler interface for SecretURL.
|
|
|
|
func (s *SecretURL) UnmarshalJSON(data []byte) error {
|
|
|
|
// In order to deserialize a previously serialized configuration (eg from
|
|
|
|
// the Alertmanager API with amtool), `<secret>` needs to be treated
|
|
|
|
// specially, as it isn't a valid URL.
|
|
|
|
if string(data) == secretToken || string(data) == secretTokenJSON {
|
|
|
|
s.URL = &url.URL{}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return json.Unmarshal(data, (*URL)(s))
|
|
|
|
}
|
|
|
|
|
2015-09-25 14:14:30 +00:00
|
|
|
// Load parses the YAML input s into a Config.
|
|
|
|
func Load(s string) (*Config, error) {
|
|
|
|
cfg := &Config{}
|
2018-04-17 14:22:46 +00:00
|
|
|
err := yaml.UnmarshalStrict([]byte(s), cfg)
|
2015-09-25 14:14:30 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-11-20 14:10:38 +00:00
|
|
|
// Check if we have a root route. We cannot check for it in the
|
|
|
|
// UnmarshalYAML method because it won't be called if the input is empty
|
|
|
|
// (e.g. the config file is empty or only contains whitespace).
|
|
|
|
if cfg.Route == nil {
|
|
|
|
return nil, errors.New("no route provided in config")
|
|
|
|
}
|
|
|
|
|
2017-05-09 08:11:40 +00:00
|
|
|
// Check if continue in root route.
|
2017-05-03 12:40:54 +00:00
|
|
|
if cfg.Route.Continue {
|
|
|
|
return nil, errors.New("cannot have continue in root route")
|
|
|
|
}
|
|
|
|
|
2015-09-25 14:14:30 +00:00
|
|
|
cfg.original = s
|
|
|
|
return cfg, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// LoadFile parses the given YAML file into a Config.
|
2019-08-26 12:01:40 +00:00
|
|
|
func LoadFile(filename string) (*Config, error) {
|
2022-07-18 12:25:32 +00:00
|
|
|
content, err := os.ReadFile(filename)
|
2015-09-25 14:14:30 +00:00
|
|
|
if err != nil {
|
2019-08-26 12:01:40 +00:00
|
|
|
return nil, err
|
2015-09-25 14:14:30 +00:00
|
|
|
}
|
2015-10-09 09:06:04 +00:00
|
|
|
cfg, err := Load(string(content))
|
|
|
|
if err != nil {
|
2019-08-26 12:01:40 +00:00
|
|
|
return nil, err
|
2015-10-09 09:06:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
resolveFilepaths(filepath.Dir(filename), cfg)
|
2019-08-26 12:01:40 +00:00
|
|
|
return cfg, nil
|
2015-10-09 09:06:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// resolveFilepaths joins all relative paths in a configuration
|
|
|
|
// with a given base directory.
|
|
|
|
func resolveFilepaths(baseDir string, cfg *Config) {
|
|
|
|
join := func(fp string) string {
|
|
|
|
if len(fp) > 0 && !filepath.IsAbs(fp) {
|
|
|
|
fp = filepath.Join(baseDir, fp)
|
|
|
|
}
|
|
|
|
return fp
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, tf := range cfg.Templates {
|
|
|
|
cfg.Templates[i] = join(tf)
|
|
|
|
}
|
2021-03-02 21:56:06 +00:00
|
|
|
|
|
|
|
cfg.Global.HTTPConfig.SetDirectory(baseDir)
|
|
|
|
for _, receiver := range cfg.Receivers {
|
|
|
|
for _, cfg := range receiver.OpsGenieConfigs {
|
|
|
|
cfg.HTTPConfig.SetDirectory(baseDir)
|
|
|
|
}
|
|
|
|
for _, cfg := range receiver.PagerdutyConfigs {
|
|
|
|
cfg.HTTPConfig.SetDirectory(baseDir)
|
|
|
|
}
|
|
|
|
for _, cfg := range receiver.PushoverConfigs {
|
|
|
|
cfg.HTTPConfig.SetDirectory(baseDir)
|
|
|
|
}
|
|
|
|
for _, cfg := range receiver.SlackConfigs {
|
|
|
|
cfg.HTTPConfig.SetDirectory(baseDir)
|
|
|
|
}
|
|
|
|
for _, cfg := range receiver.VictorOpsConfigs {
|
|
|
|
cfg.HTTPConfig.SetDirectory(baseDir)
|
|
|
|
}
|
|
|
|
for _, cfg := range receiver.WebhookConfigs {
|
|
|
|
cfg.HTTPConfig.SetDirectory(baseDir)
|
|
|
|
}
|
|
|
|
for _, cfg := range receiver.WechatConfigs {
|
|
|
|
cfg.HTTPConfig.SetDirectory(baseDir)
|
|
|
|
}
|
2021-06-10 02:13:26 +00:00
|
|
|
for _, cfg := range receiver.SNSConfigs {
|
|
|
|
cfg.HTTPConfig.SetDirectory(baseDir)
|
|
|
|
}
|
2022-02-22 16:51:02 +00:00
|
|
|
for _, cfg := range receiver.TelegramConfigs {
|
|
|
|
cfg.HTTPConfig.SetDirectory(baseDir)
|
|
|
|
}
|
2022-06-10 14:17:14 +00:00
|
|
|
for _, cfg := range receiver.DiscordConfigs {
|
|
|
|
cfg.HTTPConfig.SetDirectory(baseDir)
|
|
|
|
}
|
2022-11-08 12:54:38 +00:00
|
|
|
for _, cfg := range receiver.WebexConfigs {
|
|
|
|
cfg.HTTPConfig.SetDirectory(baseDir)
|
|
|
|
}
|
2021-03-02 21:56:06 +00:00
|
|
|
}
|
2015-09-25 14:14:30 +00:00
|
|
|
}
|
|
|
|
|
2020-11-24 01:42:27 +00:00
|
|
|
// MuteTimeInterval represents a named set of time intervals for which a route should be muted.
|
2020-09-22 07:13:27 +00:00
|
|
|
type MuteTimeInterval struct {
|
2021-11-15 10:16:52 +00:00
|
|
|
Name string `yaml:"name" json:"name"`
|
|
|
|
TimeIntervals []timeinterval.TimeInterval `yaml:"time_intervals" json:"time_intervals"`
|
2020-09-22 07:13:27 +00:00
|
|
|
}
|
|
|
|
|
2020-11-24 04:34:13 +00:00
|
|
|
// UnmarshalYAML implements the yaml.Unmarshaler interface for MuteTimeInterval.
|
2020-10-14 10:26:16 +00:00
|
|
|
func (mt *MuteTimeInterval) UnmarshalYAML(unmarshal func(interface{}) error) error {
|
|
|
|
type plain MuteTimeInterval
|
|
|
|
if err := unmarshal((*plain)(mt)); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if mt.Name == "" {
|
|
|
|
return fmt.Errorf("missing name in mute time interval")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-03-04 14:24:29 +00:00
|
|
|
// TimeInterval represents a named set of time intervals for which a route should be muted.
|
|
|
|
type TimeInterval struct {
|
|
|
|
Name string `yaml:"name" json:"name"`
|
|
|
|
TimeIntervals []timeinterval.TimeInterval `yaml:"time_intervals" json:"time_intervals"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// UnmarshalYAML implements the yaml.Unmarshaler interface for MuteTimeInterval.
|
|
|
|
func (ti *TimeInterval) UnmarshalYAML(unmarshal func(interface{}) error) error {
|
|
|
|
type plain TimeInterval
|
|
|
|
if err := unmarshal((*plain)(ti)); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if ti.Name == "" {
|
|
|
|
return fmt.Errorf("missing name in time interval")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-09-25 14:14:30 +00:00
|
|
|
// Config is the top-level configuration for Alertmanager's config files.
|
|
|
|
type Config struct {
|
2023-01-09 21:12:12 +00:00
|
|
|
Global *GlobalConfig `yaml:"global,omitempty" json:"global,omitempty"`
|
|
|
|
Route *Route `yaml:"route,omitempty" json:"route,omitempty"`
|
|
|
|
InhibitRules []InhibitRule `yaml:"inhibit_rules,omitempty" json:"inhibit_rules,omitempty"`
|
|
|
|
Receivers []Receiver `yaml:"receivers,omitempty" json:"receivers,omitempty"`
|
|
|
|
Templates []string `yaml:"templates" json:"templates"`
|
2022-03-04 14:24:29 +00:00
|
|
|
// Deprecated. Remove before v1.0 release.
|
2020-10-06 03:48:16 +00:00
|
|
|
MuteTimeIntervals []MuteTimeInterval `yaml:"mute_time_intervals,omitempty" json:"mute_time_intervals,omitempty"`
|
2022-03-04 14:24:29 +00:00
|
|
|
TimeIntervals []TimeInterval `yaml:"time_intervals,omitempty" json:"time_intervals,omitempty"`
|
2015-09-25 14:14:30 +00:00
|
|
|
|
|
|
|
// original is the input from which the config was parsed.
|
|
|
|
original string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c Config) String() string {
|
2017-06-06 11:02:49 +00:00
|
|
|
b, err := yaml.Marshal(c)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Sprintf("<error creating config string: %s>", err)
|
2015-09-25 14:14:30 +00:00
|
|
|
}
|
2017-06-06 11:02:49 +00:00
|
|
|
return string(b)
|
2015-09-25 14:14:30 +00:00
|
|
|
}
|
|
|
|
|
2019-01-15 17:03:45 +00:00
|
|
|
// UnmarshalYAML implements the yaml.Unmarshaler interface for Config.
|
2015-09-25 14:14:30 +00:00
|
|
|
func (c *Config) UnmarshalYAML(unmarshal func(interface{}) error) error {
|
|
|
|
// We want to set c to the defaults and then overwrite it with the input.
|
|
|
|
// To make unmarshal fill the plain data struct rather than calling UnmarshalYAML
|
|
|
|
// again, we have to hide it using a type indirection.
|
|
|
|
type plain Config
|
|
|
|
if err := unmarshal((*plain)(c)); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-10-17 08:02:32 +00:00
|
|
|
// If a global block was open but empty the default global config is overwritten.
|
|
|
|
// We have to restore it here.
|
|
|
|
if c.Global == nil {
|
|
|
|
c.Global = &GlobalConfig{}
|
2019-01-15 17:03:45 +00:00
|
|
|
*c.Global = DefaultGlobalConfig()
|
2015-10-17 08:02:32 +00:00
|
|
|
}
|
|
|
|
|
2021-04-02 01:57:55 +00:00
|
|
|
if c.Global.SlackAPIURL != nil && len(c.Global.SlackAPIURLFile) > 0 {
|
|
|
|
return fmt.Errorf("at most one of slack_api_url & slack_api_url_file must be configured")
|
|
|
|
}
|
|
|
|
|
2021-10-21 07:29:03 +00:00
|
|
|
if c.Global.OpsGenieAPIKey != "" && len(c.Global.OpsGenieAPIKeyFile) > 0 {
|
|
|
|
return fmt.Errorf("at most one of opsgenie_api_key & opsgenie_api_key_file must be configured")
|
|
|
|
}
|
|
|
|
|
2022-10-25 13:40:17 +00:00
|
|
|
if c.Global.VictorOpsAPIKey != "" && len(c.Global.VictorOpsAPIKeyFile) > 0 {
|
|
|
|
return fmt.Errorf("at most one of victorops_api_key & victorops_api_key_file must be configured")
|
|
|
|
}
|
|
|
|
|
2022-09-16 12:36:57 +00:00
|
|
|
if len(c.Global.SMTPAuthPassword) > 0 && len(c.Global.SMTPAuthPasswordFile) > 0 {
|
|
|
|
return fmt.Errorf("at most one of smtp_auth_password & smtp_auth_password_file must be configured")
|
|
|
|
}
|
|
|
|
|
2015-09-25 14:14:30 +00:00
|
|
|
names := map[string]struct{}{}
|
|
|
|
|
2015-11-10 13:08:20 +00:00
|
|
|
for _, rcv := range c.Receivers {
|
|
|
|
if _, ok := names[rcv.Name]; ok {
|
|
|
|
return fmt.Errorf("notification config name %q is not unique", rcv.Name)
|
2015-09-25 14:14:30 +00:00
|
|
|
}
|
2017-07-27 08:48:24 +00:00
|
|
|
for _, wh := range rcv.WebhookConfigs {
|
|
|
|
if wh.HTTPConfig == nil {
|
|
|
|
wh.HTTPConfig = c.Global.HTTPConfig
|
|
|
|
}
|
|
|
|
}
|
2015-11-10 13:08:20 +00:00
|
|
|
for _, ec := range rcv.EmailConfigs {
|
2019-08-28 13:04:40 +00:00
|
|
|
if ec.Smarthost.String() == "" {
|
|
|
|
if c.Global.SMTPSmarthost.String() == "" {
|
2015-10-29 13:34:24 +00:00
|
|
|
return fmt.Errorf("no global SMTP smarthost set")
|
2015-10-17 08:02:32 +00:00
|
|
|
}
|
2015-10-29 13:34:24 +00:00
|
|
|
ec.Smarthost = c.Global.SMTPSmarthost
|
2015-10-17 08:02:32 +00:00
|
|
|
}
|
2015-10-29 13:34:24 +00:00
|
|
|
if ec.From == "" {
|
|
|
|
if c.Global.SMTPFrom == "" {
|
2015-11-20 14:10:38 +00:00
|
|
|
return fmt.Errorf("no global SMTP from set")
|
2015-10-19 14:52:54 +00:00
|
|
|
}
|
2015-10-29 13:34:24 +00:00
|
|
|
ec.From = c.Global.SMTPFrom
|
2015-10-19 14:52:54 +00:00
|
|
|
}
|
2017-07-18 07:32:57 +00:00
|
|
|
if ec.Hello == "" {
|
|
|
|
ec.Hello = c.Global.SMTPHello
|
|
|
|
}
|
2016-04-15 03:12:47 +00:00
|
|
|
if ec.AuthUsername == "" {
|
|
|
|
ec.AuthUsername = c.Global.SMTPAuthUsername
|
|
|
|
}
|
2022-09-16 12:36:57 +00:00
|
|
|
if ec.AuthPassword == "" && ec.AuthPasswordFile == "" {
|
2016-04-15 03:12:47 +00:00
|
|
|
ec.AuthPassword = c.Global.SMTPAuthPassword
|
2022-09-16 12:36:57 +00:00
|
|
|
ec.AuthPasswordFile = c.Global.SMTPAuthPasswordFile
|
2016-04-15 03:12:47 +00:00
|
|
|
}
|
|
|
|
if ec.AuthSecret == "" {
|
|
|
|
ec.AuthSecret = c.Global.SMTPAuthSecret
|
|
|
|
}
|
|
|
|
if ec.AuthIdentity == "" {
|
|
|
|
ec.AuthIdentity = c.Global.SMTPAuthIdentity
|
|
|
|
}
|
2016-09-27 09:00:21 +00:00
|
|
|
if ec.RequireTLS == nil {
|
|
|
|
ec.RequireTLS = new(bool)
|
|
|
|
*ec.RequireTLS = c.Global.SMTPRequireTLS
|
|
|
|
}
|
2015-10-17 08:02:32 +00:00
|
|
|
}
|
2015-11-10 13:08:20 +00:00
|
|
|
for _, sc := range rcv.SlackConfigs {
|
2017-07-27 08:48:24 +00:00
|
|
|
if sc.HTTPConfig == nil {
|
|
|
|
sc.HTTPConfig = c.Global.HTTPConfig
|
|
|
|
}
|
2021-04-02 01:57:55 +00:00
|
|
|
if sc.APIURL == nil && len(sc.APIURLFile) == 0 {
|
|
|
|
if c.Global.SlackAPIURL == nil && len(c.Global.SlackAPIURLFile) == 0 {
|
|
|
|
return fmt.Errorf("no global Slack API URL set either inline or in a file")
|
2015-10-17 08:02:32 +00:00
|
|
|
}
|
2015-12-03 11:40:50 +00:00
|
|
|
sc.APIURL = c.Global.SlackAPIURL
|
2021-04-02 01:57:55 +00:00
|
|
|
sc.APIURLFile = c.Global.SlackAPIURLFile
|
2015-10-17 08:02:32 +00:00
|
|
|
}
|
|
|
|
}
|
2017-07-27 08:48:24 +00:00
|
|
|
for _, poc := range rcv.PushoverConfigs {
|
|
|
|
if poc.HTTPConfig == nil {
|
|
|
|
poc.HTTPConfig = c.Global.HTTPConfig
|
|
|
|
}
|
|
|
|
}
|
2015-11-10 13:08:20 +00:00
|
|
|
for _, pdc := range rcv.PagerdutyConfigs {
|
2017-07-27 08:48:24 +00:00
|
|
|
if pdc.HTTPConfig == nil {
|
|
|
|
pdc.HTTPConfig = c.Global.HTTPConfig
|
|
|
|
}
|
2018-07-26 10:39:33 +00:00
|
|
|
if pdc.URL == nil {
|
|
|
|
if c.Global.PagerdutyURL == nil {
|
2015-10-19 09:44:46 +00:00
|
|
|
return fmt.Errorf("no global PagerDuty URL set")
|
|
|
|
}
|
2015-10-19 09:45:52 +00:00
|
|
|
pdc.URL = c.Global.PagerdutyURL
|
2015-10-19 09:44:46 +00:00
|
|
|
}
|
|
|
|
}
|
2015-11-24 22:29:25 +00:00
|
|
|
for _, ogc := range rcv.OpsGenieConfigs {
|
2017-07-27 08:48:24 +00:00
|
|
|
if ogc.HTTPConfig == nil {
|
|
|
|
ogc.HTTPConfig = c.Global.HTTPConfig
|
|
|
|
}
|
2018-07-26 10:39:33 +00:00
|
|
|
if ogc.APIURL == nil {
|
|
|
|
if c.Global.OpsGenieAPIURL == nil {
|
2015-11-24 22:29:25 +00:00
|
|
|
return fmt.Errorf("no global OpsGenie URL set")
|
|
|
|
}
|
2017-11-11 14:01:21 +00:00
|
|
|
ogc.APIURL = c.Global.OpsGenieAPIURL
|
2015-11-24 22:29:25 +00:00
|
|
|
}
|
2018-07-26 10:39:33 +00:00
|
|
|
if !strings.HasSuffix(ogc.APIURL.Path, "/") {
|
|
|
|
ogc.APIURL.Path += "/"
|
2015-11-24 22:29:25 +00:00
|
|
|
}
|
2021-10-21 07:29:03 +00:00
|
|
|
if ogc.APIKey == "" && len(ogc.APIKeyFile) == 0 {
|
|
|
|
if c.Global.OpsGenieAPIKey == "" && len(c.Global.OpsGenieAPIKeyFile) == 0 {
|
|
|
|
return fmt.Errorf("no global OpsGenie API Key set either inline or in a file")
|
2018-01-29 15:05:17 +00:00
|
|
|
}
|
|
|
|
ogc.APIKey = c.Global.OpsGenieAPIKey
|
2021-10-21 07:29:03 +00:00
|
|
|
ogc.APIKeyFile = c.Global.OpsGenieAPIKeyFile
|
2018-01-29 15:05:17 +00:00
|
|
|
}
|
2015-11-24 22:29:25 +00:00
|
|
|
}
|
2017-12-09 15:20:22 +00:00
|
|
|
for _, wcc := range rcv.WechatConfigs {
|
2018-03-20 11:21:19 +00:00
|
|
|
if wcc.HTTPConfig == nil {
|
|
|
|
wcc.HTTPConfig = c.Global.HTTPConfig
|
|
|
|
}
|
|
|
|
|
2018-07-26 10:39:33 +00:00
|
|
|
if wcc.APIURL == nil {
|
|
|
|
if c.Global.WeChatAPIURL == nil {
|
2017-12-09 15:20:22 +00:00
|
|
|
return fmt.Errorf("no global Wechat URL set")
|
|
|
|
}
|
2018-02-11 19:09:47 +00:00
|
|
|
wcc.APIURL = c.Global.WeChatAPIURL
|
2017-12-09 15:20:22 +00:00
|
|
|
}
|
2018-02-11 19:09:47 +00:00
|
|
|
|
2017-12-09 15:20:22 +00:00
|
|
|
if wcc.APISecret == "" {
|
|
|
|
if c.Global.WeChatAPISecret == "" {
|
|
|
|
return fmt.Errorf("no global Wechat ApiSecret set")
|
|
|
|
}
|
2018-02-11 19:09:47 +00:00
|
|
|
wcc.APISecret = c.Global.WeChatAPISecret
|
2017-12-09 15:20:22 +00:00
|
|
|
}
|
2018-02-11 19:09:47 +00:00
|
|
|
|
2017-12-09 15:20:22 +00:00
|
|
|
if wcc.CorpID == "" {
|
|
|
|
if c.Global.WeChatAPICorpID == "" {
|
|
|
|
return fmt.Errorf("no global Wechat CorpID set")
|
|
|
|
}
|
|
|
|
wcc.CorpID = c.Global.WeChatAPICorpID
|
|
|
|
}
|
2018-02-11 19:09:47 +00:00
|
|
|
|
2018-07-26 10:39:33 +00:00
|
|
|
if !strings.HasSuffix(wcc.APIURL.Path, "/") {
|
|
|
|
wcc.APIURL.Path += "/"
|
2017-12-09 15:20:22 +00:00
|
|
|
}
|
|
|
|
}
|
2016-07-03 15:33:44 +00:00
|
|
|
for _, voc := range rcv.VictorOpsConfigs {
|
2017-07-27 08:48:24 +00:00
|
|
|
if voc.HTTPConfig == nil {
|
|
|
|
voc.HTTPConfig = c.Global.HTTPConfig
|
|
|
|
}
|
2018-07-26 10:39:33 +00:00
|
|
|
if voc.APIURL == nil {
|
|
|
|
if c.Global.VictorOpsAPIURL == nil {
|
2016-07-03 15:33:44 +00:00
|
|
|
return fmt.Errorf("no global VictorOps URL set")
|
|
|
|
}
|
|
|
|
voc.APIURL = c.Global.VictorOpsAPIURL
|
|
|
|
}
|
2018-07-26 10:39:33 +00:00
|
|
|
if !strings.HasSuffix(voc.APIURL.Path, "/") {
|
|
|
|
voc.APIURL.Path += "/"
|
2016-07-03 15:33:44 +00:00
|
|
|
}
|
2022-10-25 13:40:17 +00:00
|
|
|
if voc.APIKey == "" && len(voc.APIKeyFile) == 0 {
|
|
|
|
if c.Global.VictorOpsAPIKey == "" && len(c.Global.VictorOpsAPIKeyFile) == 0 {
|
2017-07-12 07:25:47 +00:00
|
|
|
return fmt.Errorf("no global VictorOps API Key set")
|
|
|
|
}
|
|
|
|
voc.APIKey = c.Global.VictorOpsAPIKey
|
2022-10-25 13:40:17 +00:00
|
|
|
voc.APIKeyFile = c.Global.VictorOpsAPIKeyFile
|
2017-07-12 07:25:47 +00:00
|
|
|
}
|
2016-07-03 15:33:44 +00:00
|
|
|
}
|
2021-06-10 02:13:26 +00:00
|
|
|
for _, sns := range rcv.SNSConfigs {
|
|
|
|
if sns.HTTPConfig == nil {
|
|
|
|
sns.HTTPConfig = c.Global.HTTPConfig
|
|
|
|
}
|
|
|
|
}
|
2022-02-22 16:51:02 +00:00
|
|
|
|
|
|
|
for _, telegram := range rcv.TelegramConfigs {
|
|
|
|
if telegram.HTTPConfig == nil {
|
|
|
|
telegram.HTTPConfig = c.Global.HTTPConfig
|
|
|
|
}
|
|
|
|
if telegram.APIUrl == nil {
|
|
|
|
telegram.APIUrl = c.Global.TelegramAPIUrl
|
|
|
|
}
|
|
|
|
}
|
2022-06-10 14:17:14 +00:00
|
|
|
for _, discord := range rcv.DiscordConfigs {
|
|
|
|
if discord.HTTPConfig == nil {
|
|
|
|
discord.HTTPConfig = c.Global.HTTPConfig
|
|
|
|
}
|
|
|
|
if discord.WebhookURL == nil {
|
|
|
|
return fmt.Errorf("no discord webhook URL provided")
|
|
|
|
}
|
|
|
|
}
|
2022-11-08 12:54:38 +00:00
|
|
|
for _, webex := range rcv.WebexConfigs {
|
|
|
|
if webex.HTTPConfig == nil {
|
|
|
|
webex.HTTPConfig = c.Global.HTTPConfig
|
|
|
|
}
|
2022-11-09 14:16:00 +00:00
|
|
|
if webex.APIURL == nil {
|
|
|
|
if c.Global.WebexAPIURL == nil {
|
|
|
|
return fmt.Errorf("no global Webex URL set")
|
|
|
|
}
|
|
|
|
|
|
|
|
webex.APIURL = c.Global.WebexAPIURL
|
2022-11-08 12:54:38 +00:00
|
|
|
}
|
|
|
|
}
|
2022-02-22 16:51:02 +00:00
|
|
|
|
2015-11-10 13:08:20 +00:00
|
|
|
names[rcv.Name] = struct{}{}
|
2015-09-25 14:14:30 +00:00
|
|
|
}
|
2016-01-26 11:52:43 +00:00
|
|
|
|
|
|
|
// The root route must not have any matchers as it is the fallback node
|
|
|
|
// for all alerts.
|
2016-01-28 12:44:21 +00:00
|
|
|
if c.Route == nil {
|
2017-10-19 19:00:35 +00:00
|
|
|
return fmt.Errorf("no routes provided")
|
2016-01-28 12:44:21 +00:00
|
|
|
}
|
2016-06-04 08:05:54 +00:00
|
|
|
if len(c.Route.Receiver) == 0 {
|
2017-10-19 19:00:35 +00:00
|
|
|
return fmt.Errorf("root route must specify a default receiver")
|
2016-06-04 08:05:54 +00:00
|
|
|
}
|
2021-12-02 12:08:35 +00:00
|
|
|
if len(c.Route.Match) > 0 || len(c.Route.MatchRE) > 0 || len(c.Route.Matchers) > 0 {
|
2017-10-19 19:00:35 +00:00
|
|
|
return fmt.Errorf("root route must not have any matchers")
|
2016-01-26 11:52:43 +00:00
|
|
|
}
|
2020-11-24 04:02:07 +00:00
|
|
|
if len(c.Route.MuteTimeIntervals) > 0 {
|
2020-12-16 10:33:37 +00:00
|
|
|
return fmt.Errorf("root route must not have any mute time intervals")
|
2020-09-22 07:13:27 +00:00
|
|
|
}
|
2016-01-26 11:52:43 +00:00
|
|
|
|
2022-03-04 14:24:29 +00:00
|
|
|
if len(c.Route.ActiveTimeIntervals) > 0 {
|
|
|
|
return fmt.Errorf("root route must not have any active time intervals")
|
|
|
|
}
|
|
|
|
|
2016-01-26 11:52:43 +00:00
|
|
|
// Validate that all receivers used in the routing tree are defined.
|
2020-10-06 03:48:16 +00:00
|
|
|
if err := checkReceiver(c.Route, names); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
tiNames := make(map[string]struct{})
|
2022-03-04 14:24:29 +00:00
|
|
|
|
|
|
|
// read mute time intervals until deprecated
|
2020-10-06 03:48:16 +00:00
|
|
|
for _, mt := range c.MuteTimeIntervals {
|
2020-11-24 03:38:32 +00:00
|
|
|
if _, ok := tiNames[mt.Name]; ok {
|
|
|
|
return fmt.Errorf("mute time interval %q is not unique", mt.Name)
|
|
|
|
}
|
2020-10-06 03:48:16 +00:00
|
|
|
tiNames[mt.Name] = struct{}{}
|
|
|
|
}
|
2022-03-04 14:24:29 +00:00
|
|
|
|
|
|
|
for _, mt := range c.TimeIntervals {
|
|
|
|
if _, ok := tiNames[mt.Name]; ok {
|
|
|
|
return fmt.Errorf("time interval %q is not unique", mt.Name)
|
|
|
|
}
|
|
|
|
tiNames[mt.Name] = struct{}{}
|
|
|
|
}
|
|
|
|
|
2020-10-06 03:48:16 +00:00
|
|
|
return checkTimeInterval(c.Route, tiNames)
|
2015-09-25 14:14:30 +00:00
|
|
|
}
|
|
|
|
|
2016-01-26 11:52:43 +00:00
|
|
|
// checkReceiver returns an error if a node in the routing tree
|
|
|
|
// references a receiver not in the given map.
|
|
|
|
func checkReceiver(r *Route, receivers map[string]struct{}) error {
|
2019-03-02 11:33:40 +00:00
|
|
|
for _, sr := range r.Routes {
|
|
|
|
if err := checkReceiver(sr, receivers); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2016-06-04 08:05:54 +00:00
|
|
|
if r.Receiver == "" {
|
|
|
|
return nil
|
|
|
|
}
|
2016-01-26 11:52:43 +00:00
|
|
|
if _, ok := receivers[r.Receiver]; !ok {
|
2017-10-19 19:00:35 +00:00
|
|
|
return fmt.Errorf("undefined receiver %q used in route", r.Receiver)
|
2016-01-26 11:52:43 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-10-06 03:48:16 +00:00
|
|
|
func checkTimeInterval(r *Route, timeIntervals map[string]struct{}) error {
|
|
|
|
for _, sr := range r.Routes {
|
|
|
|
if err := checkTimeInterval(sr, timeIntervals); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2022-03-04 14:24:29 +00:00
|
|
|
|
|
|
|
for _, ti := range r.ActiveTimeIntervals {
|
|
|
|
if _, ok := timeIntervals[ti]; !ok {
|
|
|
|
return fmt.Errorf("undefined time interval %q used in route", ti)
|
|
|
|
}
|
2020-10-06 03:48:16 +00:00
|
|
|
}
|
2022-03-04 14:24:29 +00:00
|
|
|
|
|
|
|
for _, tm := range r.MuteTimeIntervals {
|
|
|
|
if _, ok := timeIntervals[tm]; !ok {
|
|
|
|
return fmt.Errorf("undefined time interval %q used in route", tm)
|
2020-10-06 03:48:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-01-15 17:03:45 +00:00
|
|
|
// DefaultGlobalConfig returns GlobalConfig with default values.
|
|
|
|
func DefaultGlobalConfig() GlobalConfig {
|
2022-03-25 16:59:51 +00:00
|
|
|
defaultHTTPConfig := commoncfg.DefaultHTTPClientConfig
|
2019-01-15 17:03:45 +00:00
|
|
|
return GlobalConfig{
|
2020-12-16 10:48:08 +00:00
|
|
|
ResolveTimeout: model.Duration(5 * time.Minute),
|
2021-03-01 22:19:38 +00:00
|
|
|
HTTPConfig: &defaultHTTPConfig,
|
2020-12-16 10:48:08 +00:00
|
|
|
|
2020-10-06 03:48:16 +00:00
|
|
|
SMTPHello: "localhost",
|
|
|
|
SMTPRequireTLS: true,
|
|
|
|
PagerdutyURL: mustParseURL("https://events.pagerduty.com/v2/enqueue"),
|
|
|
|
OpsGenieAPIURL: mustParseURL("https://api.opsgenie.com/"),
|
|
|
|
WeChatAPIURL: mustParseURL("https://qyapi.weixin.qq.com/cgi-bin/"),
|
|
|
|
VictorOpsAPIURL: mustParseURL("https://alert.victorops.com/integrations/generic/20131114/alert/"),
|
2022-02-22 16:51:02 +00:00
|
|
|
TelegramAPIUrl: mustParseURL("https://api.telegram.org"),
|
2022-11-09 14:16:00 +00:00
|
|
|
WebexAPIURL: mustParseURL("https://webexapis.com/v1/messages"),
|
2019-01-15 17:03:45 +00:00
|
|
|
}
|
2018-07-26 10:39:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func mustParseURL(s string) *URL {
|
2018-10-05 17:29:52 +00:00
|
|
|
u, err := parseURL(s)
|
2018-07-26 10:39:33 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2018-10-05 17:29:52 +00:00
|
|
|
return u
|
|
|
|
}
|
|
|
|
|
|
|
|
func parseURL(s string) (*URL, error) {
|
|
|
|
u, err := url.Parse(s)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if u.Scheme != "http" && u.Scheme != "https" {
|
|
|
|
return nil, fmt.Errorf("unsupported scheme %q for URL", u.Scheme)
|
|
|
|
}
|
|
|
|
if u.Host == "" {
|
|
|
|
return nil, fmt.Errorf("missing host for URL")
|
|
|
|
}
|
|
|
|
return &URL{u}, nil
|
2015-10-19 09:44:46 +00:00
|
|
|
}
|
2015-10-17 08:02:32 +00:00
|
|
|
|
2019-08-28 13:04:40 +00:00
|
|
|
// HostPort represents a "host:port" network address.
|
|
|
|
type HostPort struct {
|
|
|
|
Host string
|
|
|
|
Port string
|
|
|
|
}
|
|
|
|
|
|
|
|
// UnmarshalYAML implements the yaml.Unmarshaler interface for HostPort.
|
|
|
|
func (hp *HostPort) UnmarshalYAML(unmarshal func(interface{}) error) error {
|
|
|
|
var (
|
|
|
|
s string
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
if err = unmarshal(&s); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if s == "" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
hp.Host, hp.Port, err = net.SplitHostPort(s)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if hp.Port == "" {
|
|
|
|
return errors.Errorf("address %q: port cannot be empty", s)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-12-12 15:35:19 +00:00
|
|
|
// UnmarshalJSON implements the json.Unmarshaler interface for HostPort.
|
|
|
|
func (hp *HostPort) UnmarshalJSON(data []byte) error {
|
|
|
|
var (
|
|
|
|
s string
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
if err = json.Unmarshal(data, &s); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if s == "" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
hp.Host, hp.Port, err = net.SplitHostPort(s)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if hp.Port == "" {
|
|
|
|
return errors.Errorf("address %q: port cannot be empty", s)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-08-28 13:04:40 +00:00
|
|
|
// MarshalYAML implements the yaml.Marshaler interface for HostPort.
|
|
|
|
func (hp HostPort) MarshalYAML() (interface{}, error) {
|
|
|
|
return hp.String(), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// MarshalJSON implements the json.Marshaler interface for HostPort.
|
|
|
|
func (hp HostPort) MarshalJSON() ([]byte, error) {
|
|
|
|
return json.Marshal(hp.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
func (hp HostPort) String() string {
|
|
|
|
if hp.Host == "" && hp.Port == "" {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
return fmt.Sprintf("%s:%s", hp.Host, hp.Port)
|
|
|
|
}
|
|
|
|
|
2015-11-12 11:57:20 +00:00
|
|
|
// GlobalConfig defines configuration parameters that are valid globally
|
|
|
|
// unless overwritten.
|
2015-10-17 08:02:32 +00:00
|
|
|
type GlobalConfig struct {
|
2015-11-27 14:41:22 +00:00
|
|
|
// ResolveTimeout is the time after which an alert is declared resolved
|
|
|
|
// if it has not been updated.
|
2016-11-22 14:05:14 +00:00
|
|
|
ResolveTimeout model.Duration `yaml:"resolve_timeout" json:"resolve_timeout"`
|
|
|
|
|
2017-07-27 08:48:24 +00:00
|
|
|
HTTPConfig *commoncfg.HTTPClientConfig `yaml:"http_config,omitempty" json:"http_config,omitempty"`
|
|
|
|
|
2022-09-16 12:36:57 +00:00
|
|
|
SMTPFrom string `yaml:"smtp_from,omitempty" json:"smtp_from,omitempty"`
|
|
|
|
SMTPHello string `yaml:"smtp_hello,omitempty" json:"smtp_hello,omitempty"`
|
|
|
|
SMTPSmarthost HostPort `yaml:"smtp_smarthost,omitempty" json:"smtp_smarthost,omitempty"`
|
|
|
|
SMTPAuthUsername string `yaml:"smtp_auth_username,omitempty" json:"smtp_auth_username,omitempty"`
|
|
|
|
SMTPAuthPassword Secret `yaml:"smtp_auth_password,omitempty" json:"smtp_auth_password,omitempty"`
|
|
|
|
SMTPAuthPasswordFile string `yaml:"smtp_auth_password_file,omitempty" json:"smtp_auth_password_file,omitempty"`
|
|
|
|
SMTPAuthSecret Secret `yaml:"smtp_auth_secret,omitempty" json:"smtp_auth_secret,omitempty"`
|
|
|
|
SMTPAuthIdentity string `yaml:"smtp_auth_identity,omitempty" json:"smtp_auth_identity,omitempty"`
|
|
|
|
SMTPRequireTLS bool `yaml:"smtp_require_tls" json:"smtp_require_tls,omitempty"`
|
|
|
|
SlackAPIURL *SecretURL `yaml:"slack_api_url,omitempty" json:"slack_api_url,omitempty"`
|
|
|
|
SlackAPIURLFile string `yaml:"slack_api_url_file,omitempty" json:"slack_api_url_file,omitempty"`
|
|
|
|
PagerdutyURL *URL `yaml:"pagerduty_url,omitempty" json:"pagerduty_url,omitempty"`
|
|
|
|
OpsGenieAPIURL *URL `yaml:"opsgenie_api_url,omitempty" json:"opsgenie_api_url,omitempty"`
|
|
|
|
OpsGenieAPIKey Secret `yaml:"opsgenie_api_key,omitempty" json:"opsgenie_api_key,omitempty"`
|
|
|
|
OpsGenieAPIKeyFile string `yaml:"opsgenie_api_key_file,omitempty" json:"opsgenie_api_key_file,omitempty"`
|
|
|
|
WeChatAPIURL *URL `yaml:"wechat_api_url,omitempty" json:"wechat_api_url,omitempty"`
|
|
|
|
WeChatAPISecret Secret `yaml:"wechat_api_secret,omitempty" json:"wechat_api_secret,omitempty"`
|
|
|
|
WeChatAPICorpID string `yaml:"wechat_api_corp_id,omitempty" json:"wechat_api_corp_id,omitempty"`
|
|
|
|
VictorOpsAPIURL *URL `yaml:"victorops_api_url,omitempty" json:"victorops_api_url,omitempty"`
|
|
|
|
VictorOpsAPIKey Secret `yaml:"victorops_api_key,omitempty" json:"victorops_api_key,omitempty"`
|
2022-10-25 13:40:17 +00:00
|
|
|
VictorOpsAPIKeyFile string `yaml:"victorops_api_key_file,omitempty" json:"victorops_api_key_file,omitempty"`
|
2022-09-16 12:36:57 +00:00
|
|
|
TelegramAPIUrl *URL `yaml:"telegram_api_url,omitempty" json:"telegram_api_url,omitempty"`
|
2022-11-09 14:16:00 +00:00
|
|
|
WebexAPIURL *URL `yaml:"webex_api_url,omitempty" json:"webex_api_url,omitempty"`
|
2015-10-17 08:02:32 +00:00
|
|
|
}
|
|
|
|
|
2019-01-15 17:03:45 +00:00
|
|
|
// UnmarshalYAML implements the yaml.Unmarshaler interface for GlobalConfig.
|
2015-10-17 08:02:32 +00:00
|
|
|
func (c *GlobalConfig) UnmarshalYAML(unmarshal func(interface{}) error) error {
|
2019-01-15 17:03:45 +00:00
|
|
|
*c = DefaultGlobalConfig()
|
2015-10-17 08:02:32 +00:00
|
|
|
type plain GlobalConfig
|
2018-04-17 14:22:46 +00:00
|
|
|
return unmarshal((*plain)(c))
|
2015-10-17 08:02:32 +00:00
|
|
|
}
|
|
|
|
|
2015-09-25 14:14:30 +00:00
|
|
|
// A Route is a node that contains definitions of how to handle alerts.
|
|
|
|
type Route struct {
|
2018-11-29 11:31:14 +00:00
|
|
|
Receiver string `yaml:"receiver,omitempty" json:"receiver,omitempty"`
|
|
|
|
|
2018-12-13 10:51:49 +00:00
|
|
|
GroupByStr []string `yaml:"group_by,omitempty" json:"group_by,omitempty"`
|
|
|
|
GroupBy []model.LabelName `yaml:"-" json:"-"`
|
|
|
|
GroupByAll bool `yaml:"-" json:"-"`
|
2021-01-24 09:40:01 +00:00
|
|
|
// Deprecated. Remove before v1.0 release.
|
|
|
|
Match map[string]string `yaml:"match,omitempty" json:"match,omitempty"`
|
|
|
|
// Deprecated. Remove before v1.0 release.
|
2022-03-04 14:24:29 +00:00
|
|
|
MatchRE MatchRegexps `yaml:"match_re,omitempty" json:"match_re,omitempty"`
|
|
|
|
Matchers Matchers `yaml:"matchers,omitempty" json:"matchers,omitempty"`
|
|
|
|
MuteTimeIntervals []string `yaml:"mute_time_intervals,omitempty" json:"mute_time_intervals,omitempty"`
|
|
|
|
ActiveTimeIntervals []string `yaml:"active_time_intervals,omitempty" json:"active_time_intervals,omitempty"`
|
|
|
|
Continue bool `yaml:"continue" json:"continue,omitempty"`
|
|
|
|
Routes []*Route `yaml:"routes,omitempty" json:"routes,omitempty"`
|
2015-09-25 14:14:30 +00:00
|
|
|
|
2016-11-22 14:05:14 +00:00
|
|
|
GroupWait *model.Duration `yaml:"group_wait,omitempty" json:"group_wait,omitempty"`
|
|
|
|
GroupInterval *model.Duration `yaml:"group_interval,omitempty" json:"group_interval,omitempty"`
|
|
|
|
RepeatInterval *model.Duration `yaml:"repeat_interval,omitempty" json:"repeat_interval,omitempty"`
|
2015-09-25 14:14:30 +00:00
|
|
|
}
|
|
|
|
|
2019-01-15 17:03:45 +00:00
|
|
|
// UnmarshalYAML implements the yaml.Unmarshaler interface for Route.
|
2015-09-25 14:14:30 +00:00
|
|
|
func (r *Route) UnmarshalYAML(unmarshal func(interface{}) error) error {
|
|
|
|
type plain Route
|
|
|
|
if err := unmarshal((*plain)(r)); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for k := range r.Match {
|
|
|
|
if !model.LabelNameRE.MatchString(k) {
|
|
|
|
return fmt.Errorf("invalid label name %q", k)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-29 11:31:14 +00:00
|
|
|
for _, l := range r.GroupByStr {
|
|
|
|
if l == "..." {
|
|
|
|
r.GroupByAll = true
|
|
|
|
} else {
|
|
|
|
labelName := model.LabelName(l)
|
|
|
|
if !labelName.IsValid() {
|
|
|
|
return fmt.Errorf("invalid label name %q in group_by list", l)
|
|
|
|
}
|
|
|
|
r.GroupBy = append(r.GroupBy, labelName)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(r.GroupBy) > 0 && r.GroupByAll {
|
|
|
|
return fmt.Errorf("cannot have wildcard group_by (`...`) and other other labels at the same time")
|
|
|
|
}
|
2015-09-25 14:14:30 +00:00
|
|
|
|
|
|
|
groupBy := map[model.LabelName]struct{}{}
|
|
|
|
|
|
|
|
for _, ln := range r.GroupBy {
|
|
|
|
if _, ok := groupBy[ln]; ok {
|
|
|
|
return fmt.Errorf("duplicated label %q in group_by", ln)
|
|
|
|
}
|
|
|
|
groupBy[ln] = struct{}{}
|
|
|
|
}
|
|
|
|
|
2018-02-09 09:53:46 +00:00
|
|
|
if r.GroupInterval != nil && time.Duration(*r.GroupInterval) == time.Duration(0) {
|
|
|
|
return fmt.Errorf("group_interval cannot be zero")
|
|
|
|
}
|
|
|
|
if r.RepeatInterval != nil && time.Duration(*r.RepeatInterval) == time.Duration(0) {
|
|
|
|
return fmt.Errorf("repeat_interval cannot be zero")
|
|
|
|
}
|
|
|
|
|
2018-04-17 14:22:46 +00:00
|
|
|
return nil
|
2015-09-25 14:14:30 +00:00
|
|
|
}
|
|
|
|
|
2015-11-12 11:57:20 +00:00
|
|
|
// InhibitRule defines an inhibition rule that mutes alerts that match the
|
|
|
|
// target labels if an alert matching the source labels exists.
|
|
|
|
// Both alerts have to have a set of labels being equal.
|
2015-09-25 14:14:30 +00:00
|
|
|
type InhibitRule struct {
|
2015-11-12 11:57:20 +00:00
|
|
|
// SourceMatch defines a set of labels that have to equal the given
|
2021-01-22 14:54:11 +00:00
|
|
|
// value for source alerts. Deprecated. Remove before v1.0 release.
|
2017-06-20 17:09:14 +00:00
|
|
|
SourceMatch map[string]string `yaml:"source_match,omitempty" json:"source_match,omitempty"`
|
2015-11-12 11:57:20 +00:00
|
|
|
// SourceMatchRE defines pairs like SourceMatch but does regular expression
|
2021-01-22 14:54:11 +00:00
|
|
|
// matching. Deprecated. Remove before v1.0 release.
|
2019-10-25 09:44:45 +00:00
|
|
|
SourceMatchRE MatchRegexps `yaml:"source_match_re,omitempty" json:"source_match_re,omitempty"`
|
2021-01-22 14:54:11 +00:00
|
|
|
// SourceMatchers defines a set of label matchers that have to be fulfilled for source alerts.
|
|
|
|
SourceMatchers Matchers `yaml:"source_matchers,omitempty" json:"source_matchers,omitempty"`
|
2015-11-12 11:57:20 +00:00
|
|
|
// TargetMatch defines a set of labels that have to equal the given
|
2021-01-22 14:54:11 +00:00
|
|
|
// value for target alerts. Deprecated. Remove before v1.0 release.
|
2017-06-20 17:09:14 +00:00
|
|
|
TargetMatch map[string]string `yaml:"target_match,omitempty" json:"target_match,omitempty"`
|
2015-11-12 11:57:20 +00:00
|
|
|
// TargetMatchRE defines pairs like TargetMatch but does regular expression
|
2021-01-22 14:54:11 +00:00
|
|
|
// matching. Deprecated. Remove before v1.0 release.
|
2019-10-25 09:44:45 +00:00
|
|
|
TargetMatchRE MatchRegexps `yaml:"target_match_re,omitempty" json:"target_match_re,omitempty"`
|
2021-01-22 14:54:11 +00:00
|
|
|
// TargetMatchers defines a set of label matchers that have to be fulfilled for target alerts.
|
|
|
|
TargetMatchers Matchers `yaml:"target_matchers,omitempty" json:"target_matchers,omitempty"`
|
2015-11-12 11:57:20 +00:00
|
|
|
// A set of labels that must be equal between the source and target alert
|
|
|
|
// for them to be a match.
|
2017-06-20 17:09:14 +00:00
|
|
|
Equal model.LabelNames `yaml:"equal,omitempty" json:"equal,omitempty"`
|
2015-09-25 14:14:30 +00:00
|
|
|
}
|
|
|
|
|
2019-01-15 17:03:45 +00:00
|
|
|
// UnmarshalYAML implements the yaml.Unmarshaler interface for InhibitRule.
|
2015-09-25 14:14:30 +00:00
|
|
|
func (r *InhibitRule) UnmarshalYAML(unmarshal func(interface{}) error) error {
|
|
|
|
type plain InhibitRule
|
|
|
|
if err := unmarshal((*plain)(r)); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for k := range r.SourceMatch {
|
|
|
|
if !model.LabelNameRE.MatchString(k) {
|
|
|
|
return fmt.Errorf("invalid label name %q", k)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for k := range r.TargetMatch {
|
|
|
|
if !model.LabelNameRE.MatchString(k) {
|
|
|
|
return fmt.Errorf("invalid label name %q", k)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-17 14:22:46 +00:00
|
|
|
return nil
|
2015-09-25 14:14:30 +00:00
|
|
|
}
|
|
|
|
|
2015-11-12 11:57:20 +00:00
|
|
|
// Receiver configuration provides configuration on how to contact a receiver.
|
2015-11-10 13:08:20 +00:00
|
|
|
type Receiver struct {
|
2015-11-12 11:57:20 +00:00
|
|
|
// A unique identifier for this receiver.
|
2016-11-22 14:05:14 +00:00
|
|
|
Name string `yaml:"name" json:"name"`
|
2015-09-25 14:14:30 +00:00
|
|
|
|
2022-06-10 14:17:14 +00:00
|
|
|
DiscordConfigs []*DiscordConfig `yaml:"discord_configs,omitempty" json:"discord_configs,omitempty"`
|
2016-11-22 14:05:14 +00:00
|
|
|
EmailConfigs []*EmailConfig `yaml:"email_configs,omitempty" json:"email_configs,omitempty"`
|
|
|
|
PagerdutyConfigs []*PagerdutyConfig `yaml:"pagerduty_configs,omitempty" json:"pagerduty_configs,omitempty"`
|
|
|
|
SlackConfigs []*SlackConfig `yaml:"slack_configs,omitempty" json:"slack_configs,omitempty"`
|
|
|
|
WebhookConfigs []*WebhookConfig `yaml:"webhook_configs,omitempty" json:"webhook_configs,omitempty"`
|
|
|
|
OpsGenieConfigs []*OpsGenieConfig `yaml:"opsgenie_configs,omitempty" json:"opsgenie_configs,omitempty"`
|
2017-12-09 15:20:22 +00:00
|
|
|
WechatConfigs []*WechatConfig `yaml:"wechat_configs,omitempty" json:"wechat_configs,omitempty"`
|
2016-11-22 14:05:14 +00:00
|
|
|
PushoverConfigs []*PushoverConfig `yaml:"pushover_configs,omitempty" json:"pushover_configs,omitempty"`
|
|
|
|
VictorOpsConfigs []*VictorOpsConfig `yaml:"victorops_configs,omitempty" json:"victorops_configs,omitempty"`
|
2021-06-10 02:13:26 +00:00
|
|
|
SNSConfigs []*SNSConfig `yaml:"sns_configs,omitempty" json:"sns_configs,omitempty"`
|
2022-02-22 16:51:02 +00:00
|
|
|
TelegramConfigs []*TelegramConfig `yaml:"telegram_configs,omitempty" json:"telegram_configs,omitempty"`
|
2022-11-08 12:54:38 +00:00
|
|
|
WebexConfigs []*WebexConfig `yaml:"webex_configs,omitempty" json:"webex_configs,omitempty"`
|
2015-09-25 14:14:30 +00:00
|
|
|
}
|
|
|
|
|
2019-01-15 17:03:45 +00:00
|
|
|
// UnmarshalYAML implements the yaml.Unmarshaler interface for Receiver.
|
2015-11-10 13:08:20 +00:00
|
|
|
func (c *Receiver) UnmarshalYAML(unmarshal func(interface{}) error) error {
|
|
|
|
type plain Receiver
|
2015-09-25 14:14:30 +00:00
|
|
|
if err := unmarshal((*plain)(c)); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if c.Name == "" {
|
2015-11-10 13:08:20 +00:00
|
|
|
return fmt.Errorf("missing name in receiver")
|
2015-09-25 14:14:30 +00:00
|
|
|
}
|
2018-04-17 14:22:46 +00:00
|
|
|
return nil
|
2015-09-25 14:14:30 +00:00
|
|
|
}
|
|
|
|
|
2019-10-25 09:44:45 +00:00
|
|
|
// MatchRegexps represents a map of Regexp.
|
|
|
|
type MatchRegexps map[string]Regexp
|
|
|
|
|
|
|
|
// UnmarshalYAML implements the yaml.Unmarshaler interface for MatchRegexps.
|
|
|
|
func (m *MatchRegexps) UnmarshalYAML(unmarshal func(interface{}) error) error {
|
|
|
|
type plain MatchRegexps
|
|
|
|
if err := unmarshal((*plain)(m)); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for k, v := range *m {
|
|
|
|
if !model.LabelNameRE.MatchString(k) {
|
|
|
|
return fmt.Errorf("invalid label name %q", k)
|
|
|
|
}
|
|
|
|
if v.Regexp == nil {
|
|
|
|
return fmt.Errorf("invalid regexp value for %q", k)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-11-26 17:19:46 +00:00
|
|
|
// Regexp encapsulates a regexp.Regexp and makes it YAML marshalable.
|
2015-09-25 14:14:30 +00:00
|
|
|
type Regexp struct {
|
2015-11-05 09:25:25 +00:00
|
|
|
*regexp.Regexp
|
2019-02-19 10:08:35 +00:00
|
|
|
original string
|
2015-09-25 14:14:30 +00:00
|
|
|
}
|
|
|
|
|
2019-01-15 17:03:45 +00:00
|
|
|
// UnmarshalYAML implements the yaml.Unmarshaler interface for Regexp.
|
2015-09-25 14:14:30 +00:00
|
|
|
func (re *Regexp) UnmarshalYAML(unmarshal func(interface{}) error) error {
|
|
|
|
var s string
|
|
|
|
if err := unmarshal(&s); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-11-19 07:51:52 +00:00
|
|
|
regex, err := regexp.Compile("^(?:" + s + ")$")
|
2015-09-25 14:14:30 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-11-05 09:25:25 +00:00
|
|
|
re.Regexp = regex
|
2019-02-19 10:08:35 +00:00
|
|
|
re.original = s
|
2015-09-25 14:14:30 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-01-15 17:03:45 +00:00
|
|
|
// MarshalYAML implements the yaml.Marshaler interface for Regexp.
|
2017-06-20 17:09:14 +00:00
|
|
|
func (re Regexp) MarshalYAML() (interface{}, error) {
|
2019-02-19 10:08:35 +00:00
|
|
|
if re.original != "" {
|
|
|
|
return re.original, nil
|
2015-09-25 14:14:30 +00:00
|
|
|
}
|
|
|
|
return nil, nil
|
|
|
|
}
|
2019-12-12 15:35:19 +00:00
|
|
|
|
2021-01-22 15:44:23 +00:00
|
|
|
// UnmarshalJSON implements the json.Unmarshaler interface for Regexp
|
2019-12-12 15:35:19 +00:00
|
|
|
func (re *Regexp) UnmarshalJSON(data []byte) error {
|
|
|
|
var s string
|
|
|
|
if err := json.Unmarshal(data, &s); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
regex, err := regexp.Compile("^(?:" + s + ")$")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
re.Regexp = regex
|
|
|
|
re.original = s
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// MarshalJSON implements the json.Marshaler interface for Regexp.
|
|
|
|
func (re Regexp) MarshalJSON() ([]byte, error) {
|
|
|
|
if re.original != "" {
|
|
|
|
return json.Marshal(re.original)
|
|
|
|
}
|
2021-06-02 07:11:15 +00:00
|
|
|
return []byte("null"), nil
|
2019-12-12 15:35:19 +00:00
|
|
|
}
|
2021-01-15 20:11:39 +00:00
|
|
|
|
|
|
|
// Matchers is label.Matchers with an added UnmarshalYAML method to implement the yaml.Unmarshaler interface
|
|
|
|
// and MarshalYAML to implement the yaml.Marshaler interface.
|
|
|
|
type Matchers labels.Matchers
|
|
|
|
|
|
|
|
// UnmarshalYAML implements the yaml.Unmarshaler interface for Matchers.
|
|
|
|
func (m *Matchers) UnmarshalYAML(unmarshal func(interface{}) error) error {
|
|
|
|
var lines []string
|
|
|
|
if err := unmarshal(&lines); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for _, line := range lines {
|
|
|
|
pm, err := labels.ParseMatchers(line)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
*m = append(*m, pm...)
|
|
|
|
}
|
|
|
|
sort.Sort(labels.Matchers(*m))
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// MarshalYAML implements the yaml.Marshaler interface for Matchers.
|
|
|
|
func (m Matchers) MarshalYAML() (interface{}, error) {
|
|
|
|
result := make([]string, len(m))
|
|
|
|
for i, matcher := range m {
|
|
|
|
result[i] = matcher.String()
|
|
|
|
}
|
|
|
|
return result, nil
|
|
|
|
}
|
2021-01-22 15:44:23 +00:00
|
|
|
|
|
|
|
// UnmarshalJSON implements the json.Unmarshaler interface for Matchers.
|
|
|
|
func (m *Matchers) UnmarshalJSON(data []byte) error {
|
|
|
|
var lines []string
|
|
|
|
if err := json.Unmarshal(data, &lines); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for _, line := range lines {
|
|
|
|
pm, err := labels.ParseMatchers(line)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
*m = append(*m, pm...)
|
|
|
|
}
|
|
|
|
sort.Sort(labels.Matchers(*m))
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// MarshalJSON implements the json.Marshaler interface for Matchers.
|
|
|
|
func (m Matchers) MarshalJSON() ([]byte, error) {
|
|
|
|
if len(m) == 0 {
|
2021-06-02 07:11:15 +00:00
|
|
|
return []byte("[]"), nil
|
2021-01-22 15:44:23 +00:00
|
|
|
}
|
|
|
|
result := make([]string, len(m))
|
|
|
|
for i, matcher := range m {
|
|
|
|
result[i] = matcher.String()
|
|
|
|
}
|
|
|
|
return json.Marshal(result)
|
|
|
|
}
|