/
config.go
78 lines (67 loc) · 1.58 KB
/
config.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package main
import (
"fmt"
"io/ioutil"
yaml "gopkg.in/yaml.v2"
)
type Check interface {
Name() string
Perform() error
}
type Notifier interface {
Perform([]string) error
}
type Config struct {
Name string `yaml:"name"`
Enabled bool `yaml:"enabled"`
Checks []Check `yaml:"-"`
Notifiers []Notifier `yaml:"-"`
}
func ReadConfig(path string) (*Config, error) {
data, err := ioutil.ReadFile(path)
if err != nil {
return nil, err
}
config := Config{}
if err = yaml.Unmarshal(data, &config); err != nil {
return nil, err
}
checks := struct {
Checks map[string][]map[string]interface{} `yaml:"checks"`
Notifiers map[string]map[string]interface{} `yaml:"notify"`
}{}
if err = yaml.Unmarshal(data, &checks); err != nil {
return nil, err
}
for t, items := range checks.Checks {
switch t {
case "web":
for _, item := range items {
config.Checks = append(config.Checks, ParseWebCheck(item))
}
case "ping":
for _, item := range items {
config.Checks = append(config.Checks, ParsePingCheck(item))
}
case "dns":
for _, item := range items {
config.Checks = append(config.Checks, ParseDnsCheck(item))
}
case "port":
for _, item := range items {
config.Checks = append(config.Checks, ParsePortCheck(item))
}
default:
return nil, fmt.Errorf("Invalid check type:", t)
}
}
for t, items := range checks.Notifiers {
switch t {
case "slack":
config.Notifiers = append(config.Notifiers, ParseSlackNotifier(items))
default:
return nil, fmt.Errorf("Invalid notifier type:", t)
}
}
return &config, nil
}