2018-05-21 16:36:45 +02:00
|
|
|
// Copyright 2018 Google LLC
|
|
|
|
//
|
|
|
|
// 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
|
|
|
|
//
|
|
|
|
// https://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 main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"gopkg.in/yaml.v2"
|
|
|
|
"io/ioutil"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2020-01-25 17:42:59 +01:00
|
|
|
defaultMsgOnceTemplate = "Alert {{ .GroupLabels.alertname }} for {{ .GroupLabels.job }} is {{ .Status }}"
|
|
|
|
defaultMsgTemplate = "Alert {{ .Labels.alertname }} on {{ .Labels.instance }} is {{ .Status }}"
|
2018-05-21 16:36:45 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
type IRCChannel struct {
|
|
|
|
Name string `yaml:"name"`
|
|
|
|
Password string `yaml:"password"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type Config struct {
|
2020-03-05 09:49:31 +01:00
|
|
|
HTTPHost string `yaml:"http_host"`
|
|
|
|
HTTPPort int `yaml:"http_port"`
|
|
|
|
IRCNick string `yaml:"irc_nickname"`
|
|
|
|
IRCNickPass string `yaml:"irc_nickname_password"`
|
|
|
|
IRCRealName string `yaml:"irc_realname"`
|
|
|
|
IRCHost string `yaml:"irc_host"`
|
|
|
|
IRCPort int `yaml:"irc_port"`
|
|
|
|
IRCUseSSL bool `yaml:"irc_use_ssl"`
|
|
|
|
IRCVerifySSL bool `yaml:"irc_verify_ssl"`
|
|
|
|
IRCChannels []IRCChannel `yaml:"irc_channels"`
|
|
|
|
MsgTemplate string `yaml:"msg_template"`
|
|
|
|
MsgOnce bool `yaml:"msg_once_per_alert_group"`
|
|
|
|
UsePrivmsg bool `yaml:"use_privmsg"`
|
2018-05-21 16:36:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func LoadConfig(configFile string) (*Config, error) {
|
|
|
|
config := &Config{
|
2020-03-05 09:49:31 +01:00
|
|
|
HTTPHost: "localhost",
|
|
|
|
HTTPPort: 8000,
|
|
|
|
IRCNick: "alertmanager-irc-relay",
|
|
|
|
IRCNickPass: "",
|
|
|
|
IRCRealName: "Alertmanager IRC Relay",
|
2020-03-05 10:12:02 +01:00
|
|
|
IRCHost: "example.com",
|
2020-03-05 09:49:31 +01:00
|
|
|
IRCPort: 7000,
|
|
|
|
IRCUseSSL: true,
|
|
|
|
IRCVerifySSL: true,
|
2020-03-05 10:12:02 +01:00
|
|
|
IRCChannels: []IRCChannel{},
|
2020-03-05 09:49:31 +01:00
|
|
|
MsgOnce: false,
|
|
|
|
UsePrivmsg: false,
|
2018-05-21 16:36:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if configFile != "" {
|
|
|
|
data, err := ioutil.ReadFile(configFile)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if err := yaml.Unmarshal(data, config); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set default template if config does not have one.
|
2020-01-25 17:42:59 +01:00
|
|
|
if config.MsgTemplate == "" {
|
|
|
|
if config.MsgOnce {
|
|
|
|
config.MsgTemplate = defaultMsgOnceTemplate
|
2018-05-21 16:36:45 +02:00
|
|
|
} else {
|
2020-01-25 17:42:59 +01:00
|
|
|
config.MsgTemplate = defaultMsgTemplate
|
2018-05-21 16:36:45 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return config, nil
|
|
|
|
}
|