minitor-go/config.go

169 lines
3.9 KiB
Go
Raw Normal View History

2019-09-21 22:03:26 +00:00
package main
import (
"errors"
2022-01-27 00:34:31 +00:00
"fmt"
2021-05-11 04:39:52 +00:00
"time"
2021-05-11 03:12:18 +00:00
"git.iamthefij.com/iamthefij/slog"
2022-01-27 00:34:31 +00:00
/*
* "github.com/hashicorp/hcl/v2"
* "github.com/hashicorp/hcl/v2/gohcl"
*/
"github.com/hashicorp/hcl/v2/hclsimple"
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 {
2022-01-27 00:34:31 +00:00
CheckIntervalStr string `hcl:"check_interval"`
CheckInterval time.Duration
2019-09-21 22:03:26 +00:00
2022-01-27 00:34:31 +00:00
DefaultAlertAfter *int `hcl:"default_alert_after,optional"`
DefaultAlertEvery *int `hcl:"default_alert_every,optional"`
DefaultAlertDown []string `hcl:"default_alert_down,optional"`
DefaultAlertUp []string `hcl:"default_alert_up,optional"`
Monitors []*Monitor `hcl:"monitor,block"`
Alerts []*Alert `hcl:"alert,block"`
2022-01-27 00:34:31 +00:00
alertLookup map[string]*Alert
}
2022-01-27 00:34:31 +00:00
func (c Config) GetAlert(name string) (*Alert, bool) {
if c.alertLookup == nil {
c.alertLookup = map[string]*Alert{}
for _, alert := range c.Alerts {
c.alertLookup[alert.Name] = alert
}
2022-01-27 00:34:31 +00:00
}
v, ok := c.alertLookup[name]
return v, ok
}
2021-05-11 04:00:58 +00:00
2022-01-27 00:34:31 +00:00
// BuildAllTemplates builds all alert templates
func (c *Config) BuildAllTemplates() (err error) {
for _, alert := range c.Alerts {
if err = alert.BuildTemplates(); err != nil {
return
}
}
2021-05-11 04:00:58 +00:00
2022-01-27 00:34:31 +00:00
return
}
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
2022-01-27 00:34:31 +00:00
if len(config.Alerts) == 0 {
2020-02-18 00:47:43 +00:00
// 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
}
}
// Validate monitors
2022-01-27 00:34:31 +00:00
if 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) {
2022-01-27 00:34:31 +00:00
if _, ok := config.GetAlert(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) {
2022-01-27 00:34:31 +00:00
config.CheckInterval, err = time.ParseDuration(config.CheckIntervalStr)
if err != nil {
return fmt.Errorf("failed to parse top level check_interval duration: %w", err)
}
for _, monitor := range config.Monitors {
2022-01-27 00:34:31 +00:00
// Parse the check_interval string into a time.Duration
if monitor.CheckIntervalStr != nil {
monitor.CheckInterval, err = time.ParseDuration(*monitor.CheckIntervalStr)
if err != nil {
return fmt.Errorf("failed to parse check_interval duration for monitor %s: %w", monitor.Name, err)
}
}
// Set default values for monitor alerts
if monitor.AlertAfter == nil {
monitor.AlertAfter = config.DefaultAlertAfter
}
2022-01-27 00:34:31 +00:00
if monitor.AlertEvery == nil {
monitor.AlertEvery = config.DefaultAlertEvery
}
2022-01-27 00:34:31 +00:00
if monitor.AlertDown == nil {
monitor.AlertDown = config.DefaultAlertDown
}
2022-01-27 00:34:31 +00:00
if monitor.AlertUp == nil {
monitor.AlertUp = config.DefaultAlertUp
}
}
2022-01-27 00:34:31 +00:00
err = config.BuildAllTemplates()
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) {
2022-01-27 00:34:31 +00:00
err = hclsimple.DecodeFile(filePath, nil, &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
}