forked from mperham/inspeqtor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
global_parser.go
111 lines (91 loc) · 2.48 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
109
110
111
package inspeqtor
import (
"fmt"
"io/ioutil"
"strconv"
"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"
)
/*
Parses the global inspeqtor configuration in /etc/inspeqtor/inspeqtor.conf.
*/
type GlobalConfig struct {
CycleTime uint
ExposePort uint
DeployLength uint
Variables map[string]string
}
var Defaults = GlobalConfig{15, 4677, 300, map[string]string{}}
/*
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 {
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(data)
p := parser.NewParser()
obj, err := p.Parse(s)
if err != nil {
return nil, err
}
ast := obj.(ast.Config)
config := ConfigFile{Defaults, map[string]*AlertRoute{}}
config.Variables = ast.Variables
if val, has := ast.Variables["log_level"]; has {
util.SetLogLevel(val)
}
parseValue(ast, &config.CycleTime, "cycle_time", 15)
parseValue(ast, &config.DeployLength, "deploy_length", 300)
parseValue(ast, &config.ExposePort, "expose_port", 4677)
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, fmt.Errorf("Duplicate alert config for '%s'", v.Name)
}
config.AlertRoutes[v.Name] = ar
}
return &config, nil
}
util.Info("No configuration file found at " + rootDir + "/inspeqtor.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)
}
}