forked from elastic/beats
-
Notifications
You must be signed in to change notification settings - Fork 0
/
query.go
214 lines (187 loc) · 5.76 KB
/
query.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
package wineventlog
import (
"bytes"
"fmt"
"regexp"
"strconv"
"strings"
"text/template"
"time"
"github.com/joeshaw/multierror"
)
const (
query = `<QueryList>
<Query Id="0">
<Select Path="{{.Path}}">*{{if .Select}}[System[{{join .Select " and "}}]]{{end}}</Select>{{if .Suppress}}
<Suppress Path="{{.Path}}">*[System[({{join .Suppress " or "}})]]</Suppress>{{end}}
</Query>
</QueryList>`
)
var (
templateFuncMap = template.FuncMap{"join": strings.Join}
queryTemplate = template.Must(template.New("query").Funcs(templateFuncMap).Parse(query))
incEventIDRegex = regexp.MustCompile(`^\d+$`)
incEventIDRangeRegex = regexp.MustCompile(`^(\d+)\s*-\s*(\d+)$`)
excEventIDRegex = regexp.MustCompile(`^-(\d+)$`)
)
// Query that identifies the source of the events and one or more selectors or
// suppressors.
type Query struct {
// Name of the channel or the path to the log file that contains the events
// to query.
Log string
IgnoreOlder time.Duration // Ignore records older than this time period.
// Whitelist and blacklist of event IDs. The value is a comma-separated
// list. The accepted values are single event IDs to include (e.g. 4634), a
// range of event IDs to include (e.g. 4400-4500), and single event IDs to
// exclude (e.g. -4410).
EventID string
// Level or levels to include. The value is a comma-separated list of levels
// to include. The accepted levels are verbose (5), information (4),
// warning (3), error (2), and critical (1).
Level string
// Providers (sources) to include records from.
Provider []string
}
// Build builds a query from the given parameters. The query is returned as a
// XML string and can be used with Subscribe function.
func (q Query) Build() (string, error) {
var errs multierror.Errors
if q.Log == "" {
errs = append(errs, fmt.Errorf("empty log name"))
}
qp := &queryParams{Path: q.Log}
builders := []func(Query) error{
qp.ignoreOlderSelect,
qp.eventIDSelect,
qp.levelSelect,
qp.providerSelect,
}
for _, build := range builders {
if err := build(q); err != nil {
errs = append(errs, err)
}
}
if len(errs) > 0 {
return "", errs.Err()
}
return executeTemplate(queryTemplate, qp)
}
// queryParams are the parameters that are used to create a query from a
// template.
type queryParams struct {
Path string
Select []string
Suppress []string
}
func (qp *queryParams) ignoreOlderSelect(q Query) error {
if q.IgnoreOlder <= 0 {
return nil
}
ms := q.IgnoreOlder.Nanoseconds() / int64(time.Millisecond)
qp.Select = append(qp.Select,
fmt.Sprintf("TimeCreated[timediff(@SystemTime) <= %d]", ms))
return nil
}
func (qp *queryParams) eventIDSelect(q Query) error {
if q.EventID == "" {
return nil
}
var includes []string
var excludes []string
components := strings.Split(q.EventID, ",")
for _, c := range components {
c = strings.TrimSpace(c)
switch {
case incEventIDRegex.MatchString(c):
includes = append(includes, fmt.Sprintf("EventID=%s", c))
case excEventIDRegex.MatchString(c):
m := excEventIDRegex.FindStringSubmatch(c)
excludes = append(excludes, fmt.Sprintf("EventID=%s", m[1]))
case incEventIDRangeRegex.MatchString(c):
m := incEventIDRangeRegex.FindStringSubmatch(c)
r1, _ := strconv.Atoi(m[1])
r2, _ := strconv.Atoi(m[2])
if r1 >= r2 {
return fmt.Errorf("event ID range '%s' is invalid", c)
}
includes = append(includes,
fmt.Sprintf("(EventID >= %d and EventID <= %d)", r1, r2))
default:
return fmt.Errorf("invalid event ID query component ('%s')", c)
}
}
if len(includes) == 1 {
qp.Select = append(qp.Select, includes...)
} else if len(includes) > 1 {
qp.Select = append(qp.Select, "("+strings.Join(includes, " or ")+")")
}
if len(excludes) == 1 {
qp.Suppress = append(qp.Suppress, excludes...)
} else if len(excludes) > 1 {
qp.Suppress = append(qp.Suppress, "("+strings.Join(excludes, " or ")+")")
}
return nil
}
// levelSelect returns a xpath selector for the event Level. The returned
// selector will select events with levels less than or equal to the specified
// level. Note that level 0 is used as a catch-all/unknown level.
//
// Accepted levels:
// verbose - 5
// information, info - 4 or 0
// warning, warn - 3
// error, err - 2
// critical, crit - 1
func (qp *queryParams) levelSelect(q Query) error {
if q.Level == "" {
return nil
}
l := func(level int) string { return fmt.Sprintf("Level = %d", level) }
var levelSelect []string
for _, expr := range strings.Split(q.Level, ",") {
expr = strings.TrimSpace(expr)
switch strings.ToLower(expr) {
default:
return fmt.Errorf("invalid level ('%s') for query", q.Level)
case "verbose", "5":
levelSelect = append(levelSelect, l(5))
case "information", "info", "4":
levelSelect = append(levelSelect, l(0), l(4))
case "warning", "warn", "3":
levelSelect = append(levelSelect, l(3))
case "error", "err", "2":
levelSelect = append(levelSelect, l(2))
case "critical", "crit", "1":
levelSelect = append(levelSelect, l(1))
case "0":
levelSelect = append(levelSelect, l(0))
}
}
if len(levelSelect) > 0 {
qp.Select = append(qp.Select, "("+strings.Join(levelSelect, " or ")+")")
}
return nil
}
func (qp *queryParams) providerSelect(q Query) error {
if len(q.Provider) == 0 {
return nil
}
var selects []string
for _, p := range q.Provider {
selects = append(selects, fmt.Sprintf("@Name='%s'", p))
}
qp.Select = append(qp.Select,
fmt.Sprintf("Provider[%s]", strings.Join(selects, " or ")))
return nil
}
// executeTemplate populates a template with the given data and returns the
// value as a string.
func executeTemplate(t *template.Template, data interface{}) (string, error) {
var buf bytes.Buffer
err := t.Execute(&buf, data)
if err != nil {
return "", err
}
return buf.String(), nil
}