forked from zoni/nagios-check-runner
-
Notifications
You must be signed in to change notification settings - Fork 1
/
config.go
105 lines (92 loc) · 2.1 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package nca
import (
"fmt"
"github.com/kballard/go-shellquote"
"gopkg.in/yaml.v2"
"io"
"io/ioutil"
"strings"
)
// Config describes the full agent configuration.
type Config struct {
Publishers map[string]map[string]interface{}
Hostname string
Checks map[string]Check
}
// ReadConfig loads configuration from the given source and returns a
// fully initialized Configuration struct from it.
func ReadConfig(src io.Reader) (*Config, error) {
data, err := ioutil.ReadAll(src)
if err != nil {
return nil, err
}
c := &Config{}
if err = yaml.Unmarshal(data, c); err != nil {
return nil, Error{
Code: ErrInvalidConfig,
Message: err.Error(),
}
}
if err = parseChecks(c); err != nil {
return nil, err
}
if err = parsePublishers(c); err != nil {
return nil, err
}
return c, nil
}
// parseChecks is a helper function to ReadConfig.
func parseChecks(cfg *Config) error {
for name, check := range cfg.Checks {
if check.Name == "" {
check.Name = name
}
if check.Interval < 1 {
check.Interval = 60
}
if check.Retry < 1 {
check.Retry = 60
}
if check.Timeout < 1 {
check.Timeout = 10
}
splitArgs, err := shellquote.Split(check.Command)
if err != nil {
return err
}
if len(splitArgs) < 1 {
return Error{
Code: ErrInvalidConfig,
Message: fmt.Sprintf("Check '%s' is missing a command to execute", name),
}
}
check.Args = splitArgs
cfg.Checks[name] = check
}
return nil
}
// parsePublishers is a helper function to ReadConfig.
func parsePublishers(cfg *Config) error {
for label, publisher := range cfg.Publishers {
_, found := publisher["type"]
if !found {
publisher["type"] = label + "publisher"
}
t, ok := publisher["type"].(string)
if !ok {
return Error{
Code: ErrInvalidConfig,
Message: fmt.Sprintf("Type field of publisher '%s' should be a string", label),
}
}
ptype := strings.ToLower(t)
publisher["type"] = ptype
if _, found := publisherFactories[ptype]; !found {
return Error{
Code: ErrInvalidConfig,
Message: fmt.Sprintf("No publisher named %q available", ptype),
}
}
}
return nil
}