2021-01-01 09:26:49 -09:00
|
|
|
package config
|
|
|
|
|
|
|
|
import (
|
2021-11-14 07:04:25 -09:00
|
|
|
yaml "gopkg.in/yaml.v2"
|
|
|
|
"io"
|
2021-02-27 19:14:39 -09:00
|
|
|
"os"
|
2021-01-16 09:30:18 -09:00
|
|
|
"time"
|
2021-01-01 09:26:49 -09:00
|
|
|
)
|
|
|
|
|
2021-01-17 09:04:08 -09:00
|
|
|
type Config struct {
|
2021-11-14 07:04:25 -09:00
|
|
|
Interval time.Duration `yaml:"interval"`
|
|
|
|
Listen string `yaml:"listen"`
|
|
|
|
DebugLevel int `yaml:"debug_level"`
|
|
|
|
APIKey string `yaml:"api_key"`
|
|
|
|
APIKeyFile string `yaml:"api_key_file"`
|
|
|
|
WebRoot string `yaml:"web_root"`
|
|
|
|
Hosts []Host `yaml:"hosts"`
|
|
|
|
Alarms []Alarm `yaml:"alarms"`
|
|
|
|
Groups map[string][]Host `yaml:"groups"`
|
2021-01-17 05:24:13 -09:00
|
|
|
}
|
|
|
|
|
2021-11-14 07:04:25 -09:00
|
|
|
func New() *Config {
|
|
|
|
return &Config{
|
|
|
|
Interval: 30 * time.Second,
|
|
|
|
Listen: "127.0.0.1:8282",
|
|
|
|
DebugLevel: 0,
|
|
|
|
WebRoot: "/",
|
2021-01-17 09:04:08 -09:00
|
|
|
}
|
2021-11-14 07:04:25 -09:00
|
|
|
}
|
2021-01-17 09:04:08 -09:00
|
|
|
|
2021-11-14 07:04:25 -09:00
|
|
|
func ReadConfig(fn string, debuglvl int) (*Config, error) {
|
|
|
|
f, err := os.Open(fn)
|
2021-01-17 09:04:08 -09:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-01-01 09:26:49 -09:00
|
|
|
|
2021-11-14 07:04:25 -09:00
|
|
|
h, err := io.ReadAll(f)
|
2021-01-01 09:26:49 -09:00
|
|
|
if err != nil {
|
2021-11-14 07:04:25 -09:00
|
|
|
return nil, err
|
2021-02-27 19:14:39 -09:00
|
|
|
}
|
|
|
|
|
2021-11-14 07:04:25 -09:00
|
|
|
cfg := New()
|
|
|
|
err = yaml.UnmarshalStrict(h, &cfg)
|
|
|
|
if err != nil {
|
2021-01-01 09:26:49 -09:00
|
|
|
return nil, err
|
|
|
|
}
|
2021-11-14 07:04:25 -09:00
|
|
|
err = cfg.Validate()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2021-02-20 17:08:27 -09:00
|
|
|
}
|
2021-02-27 19:14:39 -09:00
|
|
|
return cfg, nil
|
2021-01-01 09:26:49 -09:00
|
|
|
}
|