2019-09-21 22:03:26 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2019-10-04 01:16:03 +00:00
|
|
|
"errors"
|
2022-01-27 00:34:31 +00:00
|
|
|
"fmt"
|
2021-05-11 04:39:52 +00:00
|
|
|
"time"
|
2019-11-21 23:30:19 +00:00
|
|
|
|
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/hclsimple"
|
2019-09-21 22:03:26 +00:00
|
|
|
)
|
|
|
|
|
2024-11-16 00:17:15 +00:00
|
|
|
var (
|
|
|
|
ErrLoadingConfig = errors.New("Failed to load or parse configuration")
|
|
|
|
ErrConfigInit = errors.New("Failed to initialize configuration")
|
|
|
|
ErrInvalidConfig = errors.New("Invalid configuration")
|
|
|
|
ErrNoAlerts = errors.New("No alerts provided")
|
|
|
|
ErrInvalidAlert = errors.New("Invalid alert configuration")
|
|
|
|
ErrNoMonitors = errors.New("No monitors provided")
|
|
|
|
ErrInvalidMonitor = errors.New("Invalid monitor configuration")
|
|
|
|
ErrUnknownAlert = errors.New("Unknown alert")
|
|
|
|
)
|
2021-05-11 04:00:58 +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 {
|
2022-01-27 00:34:31 +00:00
|
|
|
CheckIntervalStr string `hcl:"check_interval"`
|
|
|
|
CheckInterval time.Duration
|
2019-09-21 22:03:26 +00:00
|
|
|
|
2024-11-16 00:17:15 +00:00
|
|
|
DefaultAlertAfter int `hcl:"default_alert_after,optional"`
|
2022-01-27 00:34:31 +00:00
|
|
|
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"`
|
2020-02-16 21:25:11 +00:00
|
|
|
|
2022-01-27 00:34:31 +00:00
|
|
|
alertLookup map[string]*Alert
|
2020-02-16 21:25:11 +00:00
|
|
|
}
|
|
|
|
|
2024-11-16 00:17:15 +00:00
|
|
|
// Init performs extra initialization on top of loading the config from file
|
|
|
|
func (config *Config) Init() (err error) {
|
|
|
|
config.CheckInterval, err = time.ParseDuration(config.CheckIntervalStr)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to parse top level check_interval duration: %w", err)
|
2022-01-27 00:34:31 +00:00
|
|
|
}
|
|
|
|
|
2024-11-16 00:17:15 +00:00
|
|
|
if config.DefaultAlertAfter == 0 {
|
|
|
|
minAlertAfter := 1
|
|
|
|
config.DefaultAlertAfter = minAlertAfter
|
|
|
|
}
|
2021-05-11 04:00:58 +00:00
|
|
|
|
2024-11-16 00:17:15 +00:00
|
|
|
for _, monitor := range config.Monitors {
|
|
|
|
if err = monitor.Init(
|
|
|
|
config.DefaultAlertAfter,
|
|
|
|
config.DefaultAlertEvery,
|
|
|
|
config.DefaultAlertDown,
|
|
|
|
config.DefaultAlertUp,
|
|
|
|
); err != nil {
|
2022-01-27 00:34:31 +00:00
|
|
|
return
|
|
|
|
}
|
2020-02-16 21:25:11 +00:00
|
|
|
}
|
2021-05-11 04:00:58 +00:00
|
|
|
|
2024-11-16 00:17:15 +00:00
|
|
|
err = config.BuildAllTemplates()
|
|
|
|
|
2022-01-27 00:34:31 +00:00
|
|
|
return
|
2020-02-16 21:25:11 +00:00
|
|
|
}
|
|
|
|
|
2019-10-02 23:09:11 +00:00
|
|
|
// IsValid checks config validity and returns true if valid
|
2024-11-16 00:17:15 +00:00
|
|
|
func (config Config) IsValid() error {
|
|
|
|
var err error
|
2019-10-04 01:16:03 +00:00
|
|
|
|
2020-02-18 00:47:43 +00:00
|
|
|
// Validate alerts
|
2022-01-27 00:34:31 +00:00
|
|
|
if len(config.Alerts) == 0 {
|
2024-11-16 00:17:15 +00:00
|
|
|
err = errors.Join(err, ErrNoAlerts)
|
2020-02-18 00:47:43 +00:00
|
|
|
}
|
2021-05-11 04:00:58 +00:00
|
|
|
|
2020-02-18 00:47:43 +00:00
|
|
|
for _, alert := range config.Alerts {
|
2024-11-16 00:36:45 +00:00
|
|
|
err = errors.Join(err, alert.Validate())
|
2020-02-18 00:47:43 +00:00
|
|
|
}
|
|
|
|
|
2019-10-04 01:16:03 +00:00
|
|
|
// Validate monitors
|
2022-01-27 00:34:31 +00:00
|
|
|
if len(config.Monitors) == 0 {
|
2024-11-16 00:17:15 +00:00
|
|
|
err = errors.Join(err, ErrNoMonitors)
|
2019-10-04 01:16:03 +00:00
|
|
|
}
|
2021-05-11 04:00:58 +00:00
|
|
|
|
2019-10-02 23:09:11 +00:00
|
|
|
for _, monitor := range config.Monitors {
|
2024-11-16 00:36:45 +00:00
|
|
|
err = errors.Join(err, monitor.Validate())
|
2024-11-16 00:17:15 +00:00
|
|
|
|
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) {
|
2022-01-27 00:34:31 +00:00
|
|
|
if _, ok := config.GetAlert(alertName); !ok {
|
2024-11-16 00:17:15 +00:00
|
|
|
err = errors.Join(
|
|
|
|
err,
|
|
|
|
fmt.Errorf("%w: %s. %w: %s", ErrInvalidMonitor, monitor.Name, ErrUnknownAlert, alertName),
|
2019-10-07 17:48:19 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-10-02 23:09:11 +00:00
|
|
|
}
|
|
|
|
|
2024-11-16 00:17:15 +00:00
|
|
|
return err
|
2019-10-02 23:09:11 +00:00
|
|
|
}
|
|
|
|
|
2024-11-16 00:17:15 +00:00
|
|
|
// GetAlert returns an alert by name
|
|
|
|
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-12-19 17:50:44 +00:00
|
|
|
}
|
2024-11-16 00:17:15 +00:00
|
|
|
}
|
2022-12-19 17:50:44 +00:00
|
|
|
|
2024-11-16 00:17:15 +00:00
|
|
|
v, ok := c.alertLookup[name]
|
2022-12-19 19:15:34 +00:00
|
|
|
|
2024-11-16 00:17:15 +00:00
|
|
|
return v, ok
|
|
|
|
}
|
2022-12-19 17:50:44 +00:00
|
|
|
|
2024-11-16 00:17:15 +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
|
2022-12-19 17:50:44 +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
|
2024-11-16 00:17:15 +00:00
|
|
|
func LoadConfig(filePath string) (Config, error) {
|
|
|
|
var config Config
|
|
|
|
|
|
|
|
if err := hclsimple.DecodeFile(filePath, nil, &config); err != nil {
|
|
|
|
slog.Debugf("Failed to load config from %s: %v", filePath, err)
|
|
|
|
return config, errors.Join(ErrLoadingConfig, err)
|
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
|
2024-11-16 00:17:15 +00:00
|
|
|
if err := config.Init(); err != nil {
|
|
|
|
return config, errors.Join(ErrConfigInit, err)
|
2019-10-02 23:09:11 +00:00
|
|
|
}
|
|
|
|
|
2024-11-16 00:17:15 +00:00
|
|
|
if err := config.IsValid(); err != nil {
|
|
|
|
return config, errors.Join(ErrInvalidConfig, err)
|
2021-05-11 04:00:58 +00:00
|
|
|
}
|
|
|
|
|
2024-11-16 00:17:15 +00:00
|
|
|
return config, nil
|
2019-09-21 22:03:26 +00:00
|
|
|
}
|