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"
|
2020-11-05 16:44:56 +01:00
|
|
|
"os"
|
2021-04-17 23:17:38 +02:00
|
|
|
|
|
|
|
"github.com/google/alertmanager-irc-relay/logging"
|
2018-05-21 16:36:45 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
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-11-25 11:45:40 +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"`
|
|
|
|
IRCHostPass string `yaml:"irc_host_password"`
|
|
|
|
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"`
|
|
|
|
AlertBufferSize int `yaml:"alert_buffer_size"`
|
2021-04-16 18:17:08 +02:00
|
|
|
|
2022-10-08 18:02:16 +02:00
|
|
|
NickservName string `yaml:"nickserv_name"`
|
2021-04-17 23:17:38 +02:00
|
|
|
NickservIdentifyPatterns []string `yaml:"nickserv_identify_patterns"`
|
2022-10-08 18:02:16 +02:00
|
|
|
ChanservName string `yaml:"chanserv_name"`
|
2018-05-21 16:36:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func LoadConfig(configFile string) (*Config, error) {
|
|
|
|
config := &Config{
|
2020-11-25 11:45:40 +01:00
|
|
|
HTTPHost: "localhost",
|
|
|
|
HTTPPort: 8000,
|
|
|
|
IRCNick: "alertmanager-irc-relay",
|
|
|
|
IRCNickPass: "",
|
|
|
|
IRCRealName: "Alertmanager IRC Relay",
|
|
|
|
IRCHost: "example.com",
|
|
|
|
IRCPort: 7000,
|
|
|
|
IRCHostPass: "",
|
|
|
|
IRCUseSSL: true,
|
|
|
|
IRCVerifySSL: true,
|
|
|
|
IRCChannels: []IRCChannel{},
|
|
|
|
MsgOnce: false,
|
|
|
|
UsePrivmsg: false,
|
|
|
|
AlertBufferSize: 2048,
|
2022-10-08 18:02:16 +02:00
|
|
|
NickservName: "NickServ",
|
2021-04-16 18:17:08 +02:00
|
|
|
NickservIdentifyPatterns: []string{
|
2021-06-13 23:08:01 +02:00
|
|
|
"Please choose a different nickname, or identify via",
|
2021-04-16 18:17:08 +02:00
|
|
|
"identify via /msg NickServ identify <password>",
|
|
|
|
"type /msg NickServ IDENTIFY password",
|
|
|
|
"authenticate yourself to services with the IDENTIFY command",
|
|
|
|
},
|
2022-10-08 18:02:16 +02:00
|
|
|
ChanservName: "ChanServ",
|
2018-05-21 16:36:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if configFile != "" {
|
|
|
|
data, err := ioutil.ReadFile(configFile)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-11-05 16:44:56 +01:00
|
|
|
data = []byte(os.ExpandEnv(string(data)))
|
2018-05-21 16:36:45 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-17 23:17:38 +02:00
|
|
|
loadedConfig, _ := yaml.Marshal(config)
|
|
|
|
logging.Debug("Loaded config:\n%s", loadedConfig)
|
|
|
|
|
2018-05-21 16:36:45 +02:00
|
|
|
return config, nil
|
|
|
|
}
|