A simple monitoring solution written in Go (work in progress)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

89 lines
1.6 KiB

  1. package config
  2. import (
  3. "fmt"
  4. "git.binarythought.com/cdramey/alrm/alarm"
  5. "os"
  6. "time"
  7. )
  8. type Config struct {
  9. Groups map[string]*Group
  10. Alarms map[string]alarm.Alarm
  11. Interval time.Duration
  12. DebugLevel int
  13. Listen string
  14. Path string
  15. APIKey []byte
  16. APIKeyFile string
  17. }
  18. func (c *Config) NewAlarm(name string, typename string) (alarm.Alarm, error) {
  19. if c.Alarms == nil {
  20. c.Alarms = make(map[string]alarm.Alarm)
  21. }
  22. if _, exists := c.Alarms[name]; exists {
  23. return nil, fmt.Errorf("alarm %s already exists", name)
  24. }
  25. a, err := alarm.NewAlarm(name, typename)
  26. if err != nil {
  27. return nil, err
  28. }
  29. c.Alarms[name] = a
  30. return a, nil
  31. }
  32. func (c *Config) NewGroup(name string) (*Group, error) {
  33. if c.Groups == nil {
  34. c.Groups = make(map[string]*Group)
  35. }
  36. if _, exists := c.Groups[name]; exists {
  37. return nil, fmt.Errorf("group %s already exists", name)
  38. }
  39. group := &Group{Name: name}
  40. c.Groups[name] = group
  41. return group, nil
  42. }
  43. func (c *Config) SetInterval(val string) error {
  44. interval, err := time.ParseDuration(val)
  45. if err != nil {
  46. return err
  47. }
  48. c.Interval = interval
  49. return nil
  50. }
  51. func ReadConfig(fn string, debuglvl int) (*Config, error) {
  52. cfg := &Config{
  53. // Default check interval, 30 seconds
  54. Interval: time.Second * 30,
  55. // Default listen address
  56. Listen: "127.0.0.1:8282",
  57. DebugLevel: debuglvl,
  58. Path: fn,
  59. // API keyfile defaults to alrmrc.key
  60. APIKeyFile: fn + ".key",
  61. }
  62. pr := &parser{config: cfg}
  63. if err := pr.parse(); err != nil {
  64. return nil, err
  65. }
  66. if len(cfg.APIKey) == 0 {
  67. b, err := os.ReadFile(cfg.APIKeyFile)
  68. if err != nil {
  69. return nil, err
  70. }
  71. cfg.APIKey = b
  72. }
  73. return cfg, nil
  74. }