forked from mperham/inspeqtor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
inq_parser.go
227 lines (197 loc) · 5.23 KB
/
inq_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
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
package inspeqtor
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"github.com/mperham/inspeqtor/conf/inq/ast"
"github.com/mperham/inspeqtor/conf/inq/lexer"
"github.com/mperham/inspeqtor/conf/inq/parser"
"github.com/mperham/inspeqtor/metrics"
"github.com/mperham/inspeqtor/services"
"github.com/mperham/inspeqtor/util"
)
/*
Parses the host-specific rules in /etc/inspeqtor/host.inq
*/
func ParseHost(global *ConfigFile, hostInq string) (*Host, error) {
var host *Host
result, err := util.FileExists(hostInq)
if err != nil {
return nil, err
}
if !result {
return nil, fmt.Errorf("Missing required file: %s", hostInq)
}
util.DebugDebug("Parsing " + hostInq)
data, err := ioutil.ReadFile(hostInq)
if err != nil {
return nil, err
}
s := lexer.NewLexer(data)
p := parser.NewParser()
obj, err := p.Parse(s)
if err != nil {
return nil, err
}
switch x := obj.(type) {
case *ast.HostCheck:
host, err = BuildHost(global, x)
if err != nil {
return nil, err
}
util.DebugDebug("Host: %+v", *host)
default:
return nil, fmt.Errorf("Invalid host.inq configuration file")
}
return host, nil
}
/*
Parses the service-specific rules in /etc/inspeqtor/services.d/*.inq
*/
func ParseServices(global *ConfigFile, confDir string) ([]Checkable, error) {
util.Debug("Parsing config in " + confDir)
files, err := filepath.Glob(confDir + "/*.inq")
if err != nil {
return nil, err
}
var checks []Checkable
for _, filename := range files {
util.DebugDebug("Parsing " + filename)
data, err := ioutil.ReadFile(filename)
if err != nil {
return nil, err
}
s := lexer.NewLexer(data)
p := parser.NewParser()
obj, err := p.Parse(s)
if err != nil {
util.Warn("Unable to parse " + filename + ": " + err.Error())
continue
}
switch x := obj.(type) {
case *ast.ProcessCheck:
svc, err := BuildService(global, x)
if err != nil {
return nil, err
}
util.DebugDebug("Service: %+v", *svc)
checks = append(checks, svc)
default:
return nil, fmt.Errorf("Invalid configuration file: %s", filename)
}
}
return checks, nil
}
var (
BuildHost = convertHost
BuildService = convertService
BuildRule = convertRule
BuildAction = convertAction
BuildExpose = convertExpose
)
// GACK, so ugly
func convertHost(global *ConfigFile, inqhost *ast.HostCheck) (*Host, error) {
hostname, err := os.Hostname()
if err != nil {
return nil, err
}
storage := metrics.NewHostStore("/proc", global.CycleTime)
h := &Host{&Entity{hostname, nil, storage, inqhost.Parameters}}
rules := make([]*Rule, len(inqhost.Rules))
for idx, rule := range inqhost.Rules {
rule, err := BuildRule(global, h, rule)
util.DebugDebug("Rule: %+v", rule)
if err != nil {
return nil, err
}
err = storage.Watch(rule.MetricFamily, rule.MetricName)
if err != nil {
return nil, err
}
rules[idx] = rule
}
h.rules = rules
return h, nil
}
func convertExpose(global *ConfigFile, check Checkable, elements []string, options map[string]string) error {
return nil
}
func convertRule(global *ConfigFile, check Checkable, inqrule ast.Rule) (*Rule, error) {
op := GT
switch inqrule.Operator {
case ">":
op = GT
case "<":
op = LT
default:
return nil, fmt.Errorf("Unknown operator: %s", inqrule.Operator)
}
var actions []Action
for _, action := range inqrule.Actions {
act, err := BuildAction(global, check, action)
if err != nil {
return nil, err
}
actions = append(actions, act)
}
return &Rule{check, inqrule.Metric.Family, inqrule.Metric.Name,
op, inqrule.Threshold.Raw, float64(inqrule.Threshold.Parsed), 0, inqrule.Threshold.PerSec, inqrule.CycleCount, 0, Ok, actions}, nil
}
func convertAction(global *ConfigFile, check Eventable, action ast.Action) (Action, error) {
switch action.Name() {
case "alert":
route := global.AlertRoutes[""]
if route == nil {
return nil, fmt.Errorf("Please configure a \"send alerts\" statement in inspeqtor.conf.")
}
return Actions["alert"](check, route)
case "reload":
return Actions["reload"](check, nil)
case "restart":
return Actions["restart"](check, nil)
default:
return nil, fmt.Errorf("Unknown action: %s", action.Name())
}
}
func convertService(global *ConfigFile, inqsvc *ast.ProcessCheck) (*Service, error) {
rules := make([]*Rule, len(inqsvc.Rules))
storage := metrics.NewProcessStore("/proc", global.CycleTime)
svc := &Service{&Entity{inqsvc.Name, nil, storage, inqsvc.Parameters}, nil, services.NewStatus(), nil}
action, err := BuildAction(global, svc, &ast.SimpleAction{ActionName: "alert"})
if err != nil {
return nil, err
}
svc.EventHandler = action
for idx, irule := range inqsvc.Rules {
rule, er := convertRule(global, svc, irule)
if er != nil {
return nil, er
}
util.DebugDebug("Rule: %+v", *rule)
rules[idx] = rule
}
svc.rules = rules
for _, r := range rules {
_, err := storage.AddSource(r.MetricFamily, svc.Parameters())
if err != nil {
return nil, err
}
err = storage.Watch(r.MetricFamily, r.MetricName)
if err != nil {
return nil, err
}
util.Debug("Watching %s:%s", r.MetricFamily, r.MetricName)
}
if len(inqsvc.Exposed) > 0 {
err = BuildExpose(global, svc, inqsvc.Exposed, inqsvc.Parameters)
if err != nil {
return nil, err
}
}
err = storage.Prepare()
if err != nil {
return nil, err
}
return svc, nil
}