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
|
// SPDX-License-Identifier: GPL-3.0-or-later
package match
import (
"fmt"
"strings"
rs "github.com/netdata/netdata/go/go.d.plugin/modules/vsphere/resources"
"github.com/netdata/netdata/go/go.d.plugin/pkg/matcher"
)
type HostMatcher interface {
Match(*rs.Host) bool
}
type VMMatcher interface {
Match(*rs.VM) bool
}
type (
hostDCMatcher struct{ m matcher.Matcher }
hostClusterMatcher struct{ m matcher.Matcher }
hostHostMatcher struct{ m matcher.Matcher }
vmDCMatcher struct{ m matcher.Matcher }
vmClusterMatcher struct{ m matcher.Matcher }
vmHostMatcher struct{ m matcher.Matcher }
vmVMMatcher struct{ m matcher.Matcher }
orHostMatcher struct{ lhs, rhs HostMatcher }
orVMMatcher struct{ lhs, rhs VMMatcher }
andHostMatcher struct{ lhs, rhs HostMatcher }
andVMMatcher struct{ lhs, rhs VMMatcher }
)
func (m hostDCMatcher) Match(host *rs.Host) bool { return m.m.MatchString(host.Hier.DC.Name) }
func (m hostClusterMatcher) Match(host *rs.Host) bool { return m.m.MatchString(host.Hier.Cluster.Name) }
func (m hostHostMatcher) Match(host *rs.Host) bool { return m.m.MatchString(host.Name) }
func (m vmDCMatcher) Match(vm *rs.VM) bool { return m.m.MatchString(vm.Hier.DC.Name) }
func (m vmClusterMatcher) Match(vm *rs.VM) bool { return m.m.MatchString(vm.Hier.Cluster.Name) }
func (m vmHostMatcher) Match(vm *rs.VM) bool { return m.m.MatchString(vm.Hier.Host.Name) }
func (m vmVMMatcher) Match(vm *rs.VM) bool { return m.m.MatchString(vm.Name) }
func (m orHostMatcher) Match(host *rs.Host) bool { return m.lhs.Match(host) || m.rhs.Match(host) }
func (m orVMMatcher) Match(vm *rs.VM) bool { return m.lhs.Match(vm) || m.rhs.Match(vm) }
func (m andHostMatcher) Match(host *rs.Host) bool { return m.lhs.Match(host) && m.rhs.Match(host) }
func (m andVMMatcher) Match(vm *rs.VM) bool { return m.lhs.Match(vm) && m.rhs.Match(vm) }
func newAndHostMatcher(lhs, rhs HostMatcher, others ...HostMatcher) andHostMatcher {
m := andHostMatcher{lhs: lhs, rhs: rhs}
switch len(others) {
case 0:
return m
default:
return newAndHostMatcher(m, others[0], others[1:]...)
}
}
func newAndVMMatcher(lhs, rhs VMMatcher, others ...VMMatcher) andVMMatcher {
m := andVMMatcher{lhs: lhs, rhs: rhs}
switch len(others) {
case 0:
return m
default:
return newAndVMMatcher(m, others[0], others[1:]...)
}
}
func newOrHostMatcher(lhs, rhs HostMatcher, others ...HostMatcher) orHostMatcher {
m := orHostMatcher{lhs: lhs, rhs: rhs}
switch len(others) {
case 0:
return m
default:
return newOrHostMatcher(m, others[0], others[1:]...)
}
}
func newOrVMMatcher(lhs, rhs VMMatcher, others ...VMMatcher) orVMMatcher {
m := orVMMatcher{lhs: lhs, rhs: rhs}
switch len(others) {
case 0:
return m
default:
return newOrVMMatcher(m, others[0], others[1:]...)
}
}
type (
VMIncludes []string
HostIncludes []string
)
func (vi VMIncludes) Parse() (VMMatcher, error) {
var ms []VMMatcher
for _, v := range vi {
m, err := parseVMInclude(v)
if err != nil {
return nil, err
}
if m == nil {
continue
}
ms = append(ms, m)
}
switch len(ms) {
case 0:
return nil, nil
case 1:
return ms[0], nil
default:
return newOrVMMatcher(ms[0], ms[1], ms[2:]...), nil
}
}
func (hi HostIncludes) Parse() (HostMatcher, error) {
var ms []HostMatcher
for _, v := range hi {
m, err := parseHostInclude(v)
if err != nil {
return nil, err
}
if m == nil {
continue
}
ms = append(ms, m)
}
switch len(ms) {
case 0:
return nil, nil
case 1:
return ms[0], nil
default:
return newOrHostMatcher(ms[0], ms[1], ms[2:]...), nil
}
}
const (
datacenterIdx = iota
clusterIdx
hostIdx
vmIdx
)
func cleanInclude(include string) string {
return strings.Trim(include, "/")
}
func parseHostInclude(include string) (HostMatcher, error) {
if !isIncludeFormatValid(include) {
return nil, fmt.Errorf("bad include format: %s", include)
}
include = cleanInclude(include)
parts := strings.Split(include, "/") // /dc/clusterIdx/hostIdx
var ms []HostMatcher
for i, v := range parts {
m, err := parseSubInclude(v)
if err != nil {
return nil, err
}
switch i {
case datacenterIdx:
ms = append(ms, hostDCMatcher{m})
case clusterIdx:
ms = append(ms, hostClusterMatcher{m})
case hostIdx:
ms = append(ms, hostHostMatcher{m})
}
}
switch len(ms) {
case 0:
return nil, nil
case 1:
return ms[0], nil
default:
return newAndHostMatcher(ms[0], ms[1], ms[2:]...), nil
}
}
func parseVMInclude(include string) (VMMatcher, error) {
if !isIncludeFormatValid(include) {
return nil, fmt.Errorf("bad include format: %s", include)
}
include = cleanInclude(include)
parts := strings.Split(include, "/") // /dc/clusterIdx/hostIdx/vmIdx
var ms []VMMatcher
for i, v := range parts {
m, err := parseSubInclude(v)
if err != nil {
return nil, err
}
switch i {
case datacenterIdx:
ms = append(ms, vmDCMatcher{m})
case clusterIdx:
ms = append(ms, vmClusterMatcher{m})
case hostIdx:
ms = append(ms, vmHostMatcher{m})
case vmIdx:
ms = append(ms, vmVMMatcher{m})
}
}
switch len(ms) {
case 0:
return nil, nil
case 1:
return ms[0], nil
default:
return newAndVMMatcher(ms[0], ms[1], ms[2:]...), nil
}
}
func parseSubInclude(sub string) (matcher.Matcher, error) {
sub = strings.TrimSpace(sub)
if sub == "" || sub == "!*" {
return matcher.FALSE(), nil
}
if sub == "*" {
return matcher.TRUE(), nil
}
return matcher.NewSimplePatternsMatcher(sub)
}
func isIncludeFormatValid(line string) bool {
return strings.HasPrefix(line, "/")
}
|