minitor-go/config.go

170 lines
3.9 KiB
Go
Raw Normal View History

2019-09-21 22:03:26 +00:00
package main
import (
"errors"
2023-04-18 23:17:06 +00:00
"os"
2021-05-11 04:39:52 +00:00
"time"
2021-05-11 03:12:18 +00:00
"git.iamthefij.com/iamthefij/slog"
"gopkg.in/yaml.v2"
2019-09-21 22:03:26 +00:00
)
2021-05-11 04:00:58 +00:00
var errInvalidConfig = errors.New("Invalid configuration")
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 time.Duration `yaml:"check_interval"`
DefaultAlertAfter int16 `yaml:"default_alert_after"`
DefaultAlertEvery *int16 `yaml:"default_alert_every"`
DefaultAlertDown []string `yaml:"default_alert_down"`
DefaultAlertUp []string `yaml:"default_alert_up"`
Monitors []*Monitor
Alerts map[string]*Alert
2019-09-21 22:03:26 +00:00
}
// CommandOrShell type wraps a string or list of strings
// for executing a command directly or in a shell
type CommandOrShell struct {
ShellCommand string
Command []string
}
// Empty checks if the Command has a value
func (cos CommandOrShell) Empty() bool {
return (cos.ShellCommand == "" && cos.Command == nil)
}
// UnmarshalYAML allows unmarshalling either a string or slice of strings
// and parsing them as either a command or a shell command.
func (cos *CommandOrShell) UnmarshalYAML(unmarshal func(interface{}) error) error {
var cmd []string
err := unmarshal(&cmd)
// Error indicates this is shell command
if err != nil {
var shellCmd string
2021-05-11 04:00:58 +00:00
err := unmarshal(&shellCmd)
if err != nil {
return err
}
2021-05-11 04:00:58 +00:00
cos.ShellCommand = shellCmd
} else {
cos.Command = cmd
}
2021-05-11 04:00:58 +00:00
return nil
}
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
2020-02-18 00:47:43 +00:00
// Validate alerts
if config.Alerts == nil || len(config.Alerts) == 0 {
// This should never happen because there is a default alert named 'log' for now
2021-05-11 03:12:18 +00:00
slog.Errorf("Invalid alert configuration: Must provide at least one alert")
2020-02-18 00:47:43 +00:00
isValid = false
}
2021-05-11 04:00:58 +00:00
2020-02-18 00:47:43 +00:00
for _, alert := range config.Alerts {
if !alert.IsValid() {
2021-05-11 04:00:58 +00:00
slog.Errorf("Invalid alert configuration: %+v", alert.Name)
2021-05-11 03:12:18 +00:00
2020-02-18 00:47:43 +00:00
isValid = false
2021-05-11 04:00:58 +00:00
} else {
slog.Debugf("Loaded alert %s", alert.Name)
2020-02-18 00:47:43 +00:00
}
}
// Validate monitors
if config.Monitors == nil || len(config.Monitors) == 0 {
2021-05-11 03:12:18 +00:00
slog.Errorf("Invalid monitor configuration: Must provide at least one monitor")
isValid = false
}
2021-05-11 04:00:58 +00:00
2019-10-02 23:09:11 +00:00
for _, monitor := range config.Monitors {
if !monitor.IsValid() {
2021-05-11 03:12:18 +00:00
slog.Errorf("Invalid monitor configuration: %s", monitor.Name)
2019-10-02 23:09:11 +00:00
isValid = false
}
// 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 {
2021-05-11 03:12:18 +00:00
slog.Errorf(
"Invalid monitor configuration: %s. Unknown alert %s",
monitor.Name, alertName,
)
2021-05-11 04:00:58 +00:00
isValid = false
}
}
}
2019-10-02 23:09:11 +00:00
}
2021-05-11 04:00:58 +00:00
return isValid
2019-10-02 23:09:11 +00:00
}
// Init performs extra initialization on top of loading the config from file
func (config *Config) Init() (err error) {
for _, monitor := range config.Monitors {
if monitor.AlertAfter == 0 && config.DefaultAlertAfter > 0 {
monitor.AlertAfter = config.DefaultAlertAfter
}
if monitor.AlertEvery == nil && config.DefaultAlertEvery != nil {
monitor.AlertEvery = config.DefaultAlertEvery
}
if len(monitor.AlertDown) == 0 && len(config.DefaultAlertDown) > 0 {
monitor.AlertDown = config.DefaultAlertDown
}
if len(monitor.AlertUp) == 0 && len(config.DefaultAlertUp) > 0 {
monitor.AlertUp = config.DefaultAlertUp
}
}
2019-10-02 23:09:11 +00:00
for name, alert := range config.Alerts {
alert.Name = name
if err = alert.BuildTemplates(); err != nil {
return
2019-10-03 23:30:49 +00:00
}
2019-10-02 23:09:11 +00:00
}
return
2019-10-02 23:09:11 +00:00
}
// LoadConfig will read config from the given path and parse it
func LoadConfig(filePath string) (config Config, err error) {
2023-04-18 23:17:06 +00:00
data, err := os.ReadFile(filePath)
2019-09-21 22:03:26 +00:00
if err != nil {
return
2019-09-21 22:03:26 +00:00
}
err = yaml.Unmarshal(data, &config)
2019-09-21 22:03:26 +00:00
if err != nil {
return
2019-09-21 22:03:26 +00:00
}
2021-05-11 03:12:18 +00:00
slog.Debugf("Config values:\n%v\n", config)
2019-09-21 22:03:26 +00:00
2021-05-11 04:00:58 +00:00
// Finish initializing configuration
if err = config.Init(); err != nil {
return
2019-10-02 23:09:11 +00:00
}
2021-05-11 04:00:58 +00:00
if !config.IsValid() {
err = errInvalidConfig
2019-10-02 23:09:11 +00:00
2021-05-11 04:00:58 +00:00
return
}
return config, err
2019-09-21 22:03:26 +00:00
}