2019-09-21 22:03:26 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2019-10-04 01:16:03 +00:00
|
|
|
"errors"
|
2019-09-21 22:03:26 +00:00
|
|
|
"io/ioutil"
|
|
|
|
"log"
|
2019-11-21 23:30:19 +00:00
|
|
|
|
|
|
|
"gopkg.in/yaml.v2"
|
2019-09-21 22:03:26 +00:00
|
|
|
)
|
|
|
|
|
2019-10-02 23:09:11 +00:00
|
|
|
// Config type is contains all provided user configuration
|
2019-09-21 22:03:26 +00:00
|
|
|
type Config struct {
|
|
|
|
CheckInterval int64 `yaml:"check_interval"`
|
2019-10-02 16:37:29 +00:00
|
|
|
Monitors []*Monitor
|
|
|
|
Alerts map[string]*Alert
|
2019-09-21 22:03:26 +00:00
|
|
|
}
|
|
|
|
|
2019-10-02 23:09:11 +00:00
|
|
|
// IsValid checks config validity and returns true if valid
|
|
|
|
func (config Config) IsValid() (isValid bool) {
|
|
|
|
isValid = true
|
2019-10-04 01:16:03 +00:00
|
|
|
|
|
|
|
// Validate monitors
|
|
|
|
if config.Monitors == nil || len(config.Monitors) == 0 {
|
|
|
|
log.Printf("ERROR: Invalid monitor configuration: Must provide at least one monitor")
|
|
|
|
isValid = false
|
|
|
|
}
|
2019-10-02 23:09:11 +00:00
|
|
|
for _, monitor := range config.Monitors {
|
|
|
|
if !monitor.IsValid() {
|
|
|
|
log.Printf("ERROR: Invalid monitor configuration: %s", monitor.Name)
|
|
|
|
isValid = false
|
|
|
|
}
|
2019-10-07 17:48:19 +00:00
|
|
|
// Check that all Monitor alerts actually exist
|
|
|
|
for _, isUp := range []bool{true, false} {
|
|
|
|
for _, alertName := range monitor.GetAlertNames(isUp) {
|
|
|
|
if _, ok := config.Alerts[alertName]; !ok {
|
|
|
|
log.Printf(
|
|
|
|
"ERROR: Invalid monitor configuration: %s. Unknown alert %s",
|
|
|
|
monitor.Name, alertName,
|
|
|
|
)
|
|
|
|
isValid = false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-10-02 23:09:11 +00:00
|
|
|
}
|
|
|
|
|
2019-10-04 01:16:03 +00:00
|
|
|
// Validate alerts
|
|
|
|
if config.Alerts == nil || len(config.Alerts) == 0 {
|
|
|
|
log.Printf("ERROR: Invalid alert configuration: Must provide at least one alert")
|
|
|
|
isValid = false
|
|
|
|
}
|
2019-10-02 23:09:11 +00:00
|
|
|
for _, alert := range config.Alerts {
|
|
|
|
if !alert.IsValid() {
|
|
|
|
log.Printf("ERROR: Invalid alert configuration: %s", alert.Name)
|
|
|
|
isValid = false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Init performs extra initialization on top of loading the config from file
|
2019-10-04 01:16:03 +00:00
|
|
|
func (config *Config) Init() (err error) {
|
2019-10-02 23:09:11 +00:00
|
|
|
for name, alert := range config.Alerts {
|
|
|
|
alert.Name = name
|
2019-10-04 01:16:03 +00:00
|
|
|
if err = alert.BuildTemplates(); err != nil {
|
|
|
|
return
|
2019-10-03 23:30:49 +00:00
|
|
|
}
|
2019-10-02 23:09:11 +00:00
|
|
|
}
|
2019-10-04 01:16:03 +00:00
|
|
|
|
|
|
|
return
|
2019-10-02 23:09:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// LoadConfig will read config from the given path and parse it
|
2019-10-04 01:16:03 +00:00
|
|
|
func LoadConfig(filePath string) (config Config, err error) {
|
2019-09-21 22:03:26 +00:00
|
|
|
data, err := ioutil.ReadFile(filePath)
|
|
|
|
if err != nil {
|
2019-10-04 01:16:03 +00:00
|
|
|
return
|
2019-09-21 22:03:26 +00:00
|
|
|
}
|
2019-10-01 15:26:07 +00:00
|
|
|
|
2020-01-07 18:28:14 +00:00
|
|
|
err = yaml.Unmarshal(data, &config)
|
2019-09-21 22:03:26 +00:00
|
|
|
if err != nil {
|
2019-10-04 01:16:03 +00:00
|
|
|
return
|
2019-09-21 22:03:26 +00:00
|
|
|
}
|
|
|
|
|
2019-11-15 19:25:21 +00:00
|
|
|
if LogDebug {
|
|
|
|
log.Printf("DEBUG: Config values:\n%v\n", config)
|
|
|
|
}
|
2019-09-21 22:03:26 +00:00
|
|
|
|
2019-10-02 23:09:11 +00:00
|
|
|
if !config.IsValid() {
|
2019-10-04 01:16:03 +00:00
|
|
|
err = errors.New("Invalid configuration")
|
|
|
|
return
|
2019-10-02 23:09:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Finish initializing configuration
|
2019-10-04 01:16:03 +00:00
|
|
|
err = config.Init()
|
2019-10-02 23:09:11 +00:00
|
|
|
|
2019-10-04 01:16:03 +00:00
|
|
|
return
|
2019-09-21 22:03:26 +00:00
|
|
|
}
|