-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathconfig.go
209 lines (187 loc) · 5.39 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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
package patrol
import (
"fmt"
"io/ioutil"
"strings"
"time"
"github.com/karimsa/patrol/internal/checker"
"github.com/karimsa/patrol/internal/history"
"github.com/karimsa/patrol/internal/logger"
"gopkg.in/yaml.v2"
)
type checkCmd string
func (cmd *checkCmd) String() string {
return string(*cmd)
}
func (cmd *checkCmd) isZero() bool {
return string(*cmd) == ""
}
func (cmd *checkCmd) UnmarshalYAML(unmarshal func(interface{}) error) error {
var slice []string
if err := unmarshal(&slice); err == nil {
*cmd = checkCmd(strings.Join(slice, ";"))
return nil
}
var str string
if err := unmarshal(&str); err != nil {
return err
}
*cmd = checkCmd(str)
return nil
}
type configRaw struct {
Name string
Port int
HTTPS PatrolHttpsOptions `yaml:"https"`
DB string `yaml:"db"`
LogLevel string `yaml:"logLevel"`
Compact history.CompactOptions
Services map[string]struct {
Checks []struct {
Name string
Interval duration
Timeout duration
Cmd checkCmd
Type string
MetricUnit string `yaml:"unit"`
MaxRetries *int `yaml:"maxRetries"`
RetryInterval time.Duration `yaml:"retryInterval"`
}
OnFailure []*singleNotificationConfig `yaml:"on_failure"`
OnRecovered []*singleNotificationConfig `yaml:"on_recovered"`
OnSuccess []*singleNotificationConfig `yaml:"on_success"`
}
OnFailure []*singleNotificationConfig `yaml:"on_failure"`
OnRecovered []*singleNotificationConfig `yaml:"on_recovered"`
OnSuccess []*singleNotificationConfig `yaml:"on_success"`
}
func FromConfigFile(filePath string, historyOptions *history.NewOptions) (*Patrol, configRaw, error) {
buffer, err := ioutil.ReadFile(filePath)
if err != nil {
return nil, configRaw{}, err
}
return FromConfig(buffer, historyOptions)
}
func FromConfig(data []byte, historyOptions *history.NewOptions) (patrol *Patrol, raw configRaw, err error) {
err = yaml.UnmarshalStrict(data, &raw)
if err != nil {
return
}
if raw.Name == "" {
raw.Name = "Statuspage"
}
if raw.Port <= 0 {
raw.Port = 8080
}
if raw.DB == "" {
err = fmt.Errorf("'db' propery must be specified in config file")
return
}
if raw.LogLevel == "" {
raw.LogLevel = "info"
}
logLevel, err := getLogLevel(raw.LogLevel)
if err != nil {
return
}
patrolOpts := CreatePatrolOptions{
Name: raw.Name,
Port: uint32(raw.Port),
LogLevel: logLevel,
GroupEventHandlers: make(map[string]EventHandlers),
GlobalEventHandlers: EventHandlers{
"healthy": raw.OnSuccess,
"recovered": raw.OnRecovered,
"unhealthy": raw.OnFailure,
},
}
if historyOptions == nil {
patrolOpts.History.File = raw.DB
} else {
patrolOpts.History = *historyOptions
}
patrolOpts.History.Compact = raw.Compact
patrolOpts.History.LogLevel = logLevel
if raw.HTTPS.Cert != "" && raw.HTTPS.Key != "" {
patrolOpts.HTTPS = &raw.HTTPS
}
// Just a random guess for size, estimating about 5 checks for
// each defined service
patrolOpts.Checkers = make([]*checker.Checker, 0, len(raw.Services)*5)
historyFile, err := history.New(patrolOpts.History)
if err != nil {
return
}
if len(raw.Services) == 0 {
err = fmt.Errorf("Config file contains no services")
return
}
for group, groupConfig := range raw.Services {
if groupConfig.Checks == nil || len(groupConfig.Checks) == 0 {
err = fmt.Errorf("Empty group '%s' defined in config", group)
return
}
for idx, checkConfig := range groupConfig.Checks {
if checkConfig.Type == "" {
checkConfig.Type = "boolean"
}
if checkConfig.Name == "" {
err = fmt.Errorf("%d-th check missing name in %s", idx, group)
return
}
if checkConfig.Cmd.isZero() {
err = fmt.Errorf("%d-th check missing cmd in %s", idx, group)
return
}
if checkConfig.Type == "metric" && checkConfig.MetricUnit == "" {
err = fmt.Errorf("%d-th check is of type metric but is missing unit in %s", idx, group)
return
}
if checkConfig.Interval.isZero() {
checkConfig.Interval = duration(60 * time.Second)
}
if checkConfig.Timeout.isZero() {
checkConfig.Timeout = duration(3 * time.Minute)
}
maxRetries := 3
if checkConfig.MaxRetries != nil {
maxRetries = *checkConfig.MaxRetries
}
if checkConfig.RetryInterval <= 0*time.Second {
checkConfig.RetryInterval = 1 * time.Minute
}
groupConfig.Checks[idx] = checkConfig
patrolOpts.Checkers = append(patrolOpts.Checkers, checker.New(&checker.Checker{
Group: group,
Name: checkConfig.Name,
Type: checkConfig.Type,
Cmd: checkConfig.Cmd.String(),
MetricUnit: checkConfig.MetricUnit,
MaxRetries: maxRetries,
RetryInterval: checkConfig.RetryInterval,
Interval: checkConfig.Interval.duration(),
CmdTimeout: checkConfig.Timeout.duration(),
History: historyFile,
}))
}
patrolOpts.GroupEventHandlers[group] = EventHandlers{
"healthy": groupConfig.OnSuccess,
"recovered": groupConfig.OnRecovered,
"unhealthy": groupConfig.OnFailure,
}
}
patrol, err = New(patrolOpts, historyFile)
return
}
func getLogLevel(level string) (logger.LogLevel, error) {
switch level {
case "none":
return logger.LevelNone, nil
case "info":
return logger.LevelInfo, nil
case "debug":
return logger.LevelDebug, nil
default:
return logger.LogLevel(-1), fmt.Errorf("Unrecognized log level: '%s'", level)
}
}