-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathconfig_probe.go
More file actions
244 lines (200 loc) · 6 KB
/
config_probe.go
File metadata and controls
244 lines (200 loc) · 6 KB
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
package main
import (
"errors"
"fmt"
"io/ioutil"
"os"
"path"
"reflect"
"strings"
"time"
"github.com/Knetic/govaluate"
)
// Duration hides time.Duration for TOML file reading (see UnmarshalText)
type Duration struct {
time.Duration
}
// UnmarshalText is needed to satisfy the encoding.TextUnmarshaler interface
func (d *Duration) UnmarshalText(text []byte) error {
var err error
d.Duration, err = time.ParseDuration(string(text))
return err
}
type tomlDefault struct {
Name string
Value interface{}
}
type tomlCheck struct {
Desc string
If string
Classes []string
NeededFailures int `toml:"needed_failures"`
NeededSuccesses int `toml:"needed_successes"`
}
type tomlProbe struct {
Name string
Disabled bool
Script string
Targets []string
Delay Duration
Timeout Duration
Arguments string
Default []tomlDefault
Check []tomlCheck
RunIf string `toml:"run_if"`
}
func checkTomlDefault(pDefaults map[string]interface{}, tDefaults []tomlDefault) error {
for _, tDefault := range tDefaults {
if tDefault.Name == "" {
return errors.New("[[default]] with invalid or missing 'name'")
}
if IsAllUpper(tDefault.Name) {
return fmt.Errorf("[[default]] name is invalid (all uppercase): %s", tDefault.Name)
}
valid := false
switch tDefault.Value.(type) {
case string:
valid = true
case int32:
valid = true
case int64:
valid = true
case float32:
valid = true
case float64:
valid = true
}
if valid == false {
return fmt.Errorf("[[default]] invalid value type '%s' for '%s'", reflect.TypeOf(tDefault.Value), tDefault.Name)
}
if _, exists := pDefaults[tDefault.Name]; exists == true {
return fmt.Errorf("Config error: duplicate default name '%s'", tDefault.Name)
}
pDefaults[tDefault.Name] = tDefault.Value
}
return nil
}
func tomlProbeToProbe(tProbe *tomlProbe, config *Config, filename string) (*Probe, error) {
var probe Probe
if tProbe.Disabled == true && config.loadDisabled == false {
return nil, nil
}
probe.Disabled = (tProbe.Disabled == true)
probe.Filename = filename
if tProbe.Name == "" {
return nil, errors.New("invalid or missing 'name'")
}
probe.Name = tProbe.Name
if tProbe.Script == "" {
return nil, errors.New("invalid or missing 'script'")
}
scriptPath := path.Clean(config.configPath + "/scripts/probes/" + tProbe.Script)
stat, err := os.Stat(scriptPath)
if err != nil {
return nil, fmt.Errorf("invalid 'script' file '%s': %s", scriptPath, err)
}
if !stat.Mode().IsRegular() {
return nil, fmt.Errorf("is not a regular 'script' file '%s'", scriptPath)
}
probe.Script = scriptPath
_, err = ioutil.ReadFile(scriptPath)
if err != nil {
return nil, fmt.Errorf("error reading script file '%s': %s", scriptPath, err)
}
if tProbe.Targets == nil {
return nil, errors.New("no valid 'targets' parameter found")
}
if len(tProbe.Targets) == 0 {
return nil, errors.New("empty 'targets'")
}
// explode targets on & and check IsValidTokenName
for _, targets := range tProbe.Targets {
if targets == "*" {
continue
}
tokens := strings.Split(targets, "&")
for _, token := range tokens {
ttoken := strings.TrimSpace(token)
if !IsValidTokenName(ttoken) {
return nil, fmt.Errorf("invalid 'target' class name '%s'", ttoken)
}
}
}
probe.Targets = tProbe.Targets
if tProbe.Delay.Duration == 0 {
return nil, errors.New("invalid or missing 'delay'")
}
if tProbe.Delay.Duration < (1 * time.Minute) {
return nil, errors.New("'delay' can't be less than a minute")
}
minutes := float64(tProbe.Delay.Duration) / float64(time.Minute)
if float64(int(minutes)) != minutes {
return nil, errors.New("'delay' granularity is in minutes (ex: 5m)")
}
probe.Delay = tProbe.Delay.Duration
if tProbe.Timeout.Duration == 0 {
//~ return nil, errors.New("invalid or missing 'timeout'")
tProbe.Timeout.Duration = 20 * time.Second
}
if tProbe.Timeout.Duration < (1 * time.Second) {
return nil, errors.New("'timeout' can't be less than 1 second")
}
probe.Timeout = tProbe.Timeout.Duration
// should warn about dangerous characters? (;& …)
probe.Arguments = tProbe.Arguments
if tProbe.RunIf != "" {
expr, err := govaluate.NewEvaluableExpressionWithFunctions(tProbe.RunIf, CheckFunctions)
if err != nil {
return nil, fmt.Errorf("invalid 'run_if' expression: %s (\"%s\")", err, tProbe.RunIf)
}
if vars := expr.Vars(); len(vars) > 0 {
return nil, fmt.Errorf("undefined variable(s) in 'run_if' expression: %s", strings.Join(vars, ", "))
}
probe.RunIf = expr
}
probe.Defaults = make(map[string]interface{})
if err := checkTomlDefault(probe.Defaults, tProbe.Default); err != nil {
return nil, err
}
for index, tCheck := range tProbe.Check {
var check Check
check.Index = index
if tCheck.Desc == "" {
return nil, errors.New("[[check]] with invalid or missing 'desc'")
}
check.Desc = tCheck.Desc
if tCheck.If == "" {
return nil, errors.New("[[check]] with invalid or missing 'if'")
}
expr, err := govaluate.NewEvaluableExpressionWithFunctions(tCheck.If, CheckFunctions)
if err != nil {
return nil, fmt.Errorf("[[check]] invalid 'if' expression: %s (\"%s\")", err, tCheck.If)
}
check.If = expr
if tCheck.Classes == nil {
return nil, errors.New("no valid 'classes' parameter found")
}
if len(tCheck.Classes) == 0 {
return nil, errors.New("empty classes")
}
for _, class := range tCheck.Classes {
if !IsValidTokenName(class) {
return nil, fmt.Errorf("invalid class name '%s'", class)
}
}
check.Classes = tCheck.Classes
if tCheck.NeededFailures == 0 {
tCheck.NeededFailures = 1
}
check.NeededFailures = tCheck.NeededFailures
if tCheck.NeededSuccesses == 0 {
tCheck.NeededSuccesses = check.NeededFailures
}
check.NeededSuccesses = tCheck.NeededSuccesses
probe.Checks = append(probe.Checks, &check)
}
if miss := probe.MissingDefaults(); len(miss) > 0 {
return nil, fmt.Errorf("missing defaults (used in 'if' expressions or 'arguments' parameter): %s", strings.Join(miss, ", "))
}
return &probe, nil
}