/
global_parser.go
108 lines (91 loc) · 2.41 KB
/
global_parser.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
package inspeqtor
import (
"errors"
"fmt"
"github.com/mperham/inspeqtor/conf/global/ast"
"github.com/mperham/inspeqtor/conf/global/lexer"
"github.com/mperham/inspeqtor/conf/global/parser"
"github.com/mperham/inspeqtor/util"
"io/ioutil"
"strconv"
)
/*
Parses the global inspeqtor configuration in /etc/inspeqtor/inspeqtor.conf.
*/
type GlobalConfig struct {
CycleTime uint
DeployLength uint
}
var Defaults = GlobalConfig{15, 300}
/*
An alert route is a way to send an alert to a recipient.
Channel is the notification mechanism: email, campfire, etc.
Config is an undefined set of kv pairs for configuring the channel.
The configuration looks like this:
send alerts
via CHANNEL with K V, K V, K V
You'd then write a rule like:
if foo > 10 then alert
*/
type AlertRoute struct {
Name string
Channel string
Config map[string]string
}
type ConfigFile struct {
Top GlobalConfig
AlertRoutes map[string]*AlertRoute
}
func ParseGlobal(rootDir string) (*ConfigFile, error) {
path := rootDir + "/inspeqtor.conf"
exists, err := util.FileExists(path)
if err != nil {
return nil, err
}
if exists {
util.Debug("Parsing " + path)
data, err := ioutil.ReadFile(path)
if err != nil {
return nil, err
}
s := lexer.NewLexer([]byte(data))
p := parser.NewParser()
obj, err := p.Parse(s)
if err != nil {
return nil, err
}
ast := obj.(ast.Config)
var config ConfigFile
config.Top = Defaults
if val, has := ast.Variables["log_level"]; has {
util.SetLogLevel(val)
}
parseValue(ast, &config.Top.CycleTime, "cycle_time", 15)
parseValue(ast, &config.Top.DeployLength, "deploy_length", 300)
config.AlertRoutes = map[string]*AlertRoute{}
for _, v := range ast.Routes {
ar, err := ValidateChannel(v.Name, v.Channel, v.Config)
if err != nil {
return nil, err
}
if _, ok := config.AlertRoutes[v.Name]; ok {
return nil, errors.New(fmt.Sprintf("Duplicate alert config for '%s'", v.Name))
}
config.AlertRoutes[v.Name] = ar
}
return &config, nil
} else {
util.Info("No configuration file found at " + rootDir + "/inspector.conf")
return &ConfigFile{Defaults, nil}, nil
}
}
func parseValue(ast ast.Config, store *uint, name string, def uint) {
if val, has := ast.Variables[name]; has {
ival, err := strconv.ParseUint(val, 10, 32)
if err != nil {
util.Warn("Invalid %s: %d", name, val)
ival = uint64(def)
}
*store = uint(ival)
}
}