2021-01-01 09:26:49 -09:00
|
|
|
package config
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2021-01-16 09:30:18 -09:00
|
|
|
"time"
|
2021-01-17 09:04:08 -09:00
|
|
|
"alrm/alarm"
|
2021-01-01 09:26:49 -09:00
|
|
|
)
|
|
|
|
|
2021-01-17 09:04:08 -09:00
|
|
|
type Config struct {
|
|
|
|
Groups map[string]*Group
|
|
|
|
Alarms map[string]alarm.Alarm
|
2021-01-16 09:30:18 -09:00
|
|
|
Interval time.Duration
|
2021-01-01 09:26:49 -09:00
|
|
|
}
|
|
|
|
|
2021-01-17 09:04:08 -09:00
|
|
|
func NewConfig() *Config {
|
|
|
|
return &Config{
|
2021-01-17 05:24:13 -09:00
|
|
|
// Default check interval, 30 seconds
|
|
|
|
Interval: time.Second * 30,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-17 09:04:08 -09:00
|
|
|
func (c *Config) NewAlarm(name string, typename string) (alarm.Alarm, error) {
|
|
|
|
if c.Alarms == nil {
|
|
|
|
c.Alarms = make(map[string]alarm.Alarm)
|
2021-01-01 09:26:49 -09:00
|
|
|
}
|
|
|
|
|
2021-01-17 09:04:08 -09:00
|
|
|
if _, exists := c.Alarms[name]; exists {
|
|
|
|
return nil, fmt.Errorf("alarm %s already exists", name)
|
|
|
|
}
|
|
|
|
|
|
|
|
a, err := alarm.NewAlarm(name, typename)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
c.Alarms[name] = a
|
|
|
|
|
|
|
|
return a, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Config) NewGroup(name string) (*Group, error) {
|
|
|
|
if c.Groups == nil {
|
|
|
|
c.Groups = make(map[string]*Group)
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, exists := c.Groups[name]; exists {
|
2021-01-01 09:26:49 -09:00
|
|
|
return nil, fmt.Errorf("group %s already exists", name)
|
|
|
|
}
|
|
|
|
|
2021-01-17 09:04:08 -09:00
|
|
|
group := &Group{Name: name}
|
|
|
|
c.Groups[name] = group
|
2021-01-01 09:26:49 -09:00
|
|
|
return group, nil
|
|
|
|
}
|
|
|
|
|
2021-01-17 09:04:08 -09:00
|
|
|
func (c *Config) SetInterval(val string) error {
|
2021-01-16 09:30:18 -09:00
|
|
|
interval, err := time.ParseDuration(val)
|
2021-01-01 09:26:49 -09:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-01-17 09:04:08 -09:00
|
|
|
c.Interval = interval
|
2021-01-01 09:26:49 -09:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-01-17 09:04:08 -09:00
|
|
|
func ReadConfig(fn string, debuglvl int) (*Config, error) {
|
2021-01-01 09:26:49 -09:00
|
|
|
parser := &Parser{DebugLevel: debuglvl}
|
|
|
|
config, err := parser.Parse(fn)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return config, nil
|
|
|
|
}
|