-
Notifications
You must be signed in to change notification settings - Fork 4.9k
/
Copy pathutil.go
261 lines (227 loc) · 6.88 KB
/
util.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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
// Licensed to Elasticsearch B.V. under one or more contributor
// license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright
// ownership. Elasticsearch B.V. licenses this file to you under
// the Apache License, Version 2.0 (the "License"); you may
// not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
package monitors
import (
"errors"
"fmt"
"net"
"time"
"github.com/elastic/beats/v7/heartbeat/eventext"
"github.com/elastic/beats/v7/heartbeat/look"
"github.com/elastic/beats/v7/heartbeat/monitors/jobs"
"github.com/elastic/beats/v7/heartbeat/monitors/wrappers"
"github.com/elastic/beats/v7/libbeat/beat"
"github.com/elastic/beats/v7/libbeat/common"
)
// IPSettings provides common configuration settings for IP resolution and ping
// mode.
type IPSettings struct {
IPv4 bool `config:"ipv4"`
IPv6 bool `config:"ipv6"`
Mode PingMode `config:"mode"`
}
// PingMode enumeration for configuring `any` or `all` IPs pinging.
type PingMode uint8
const (
PingModeUndefined PingMode = iota
PingAny
PingAll
)
// DefaultIPSettings provides an instance of default IPSettings to be copied
// when unpacking settings from a common.Config object.
var DefaultIPSettings = IPSettings{
IPv4: true,
IPv6: true,
Mode: PingAny,
}
// emptyTask is a helper value for a Noop.
var emptyTask = MakeSimpleCont(func(*beat.Event) error { return nil })
// Network determines the Network type used for IP pluginName resolution, based on the
// provided settings.
func (s IPSettings) Network() string {
switch {
case s.IPv4 && !s.IPv6:
return "ip4"
case !s.IPv4 && s.IPv6:
return "ip6"
case s.IPv4 && s.IPv6:
return "ip"
}
return ""
}
// MakeSimpleCont wraps a function that produces an event and error
// into an executable Job.
func MakeSimpleCont(f func(*beat.Event) error) jobs.Job {
return func(event *beat.Event) ([]jobs.Job, error) {
err := f(event)
return nil, err
}
}
// MakePingIPFactory creates a jobFactory for building a Task from a new IP address.
func MakePingIPFactory(
f func(*beat.Event, *net.IPAddr) error,
) func(*net.IPAddr) jobs.Job {
return func(ip *net.IPAddr) jobs.Job {
return MakeSimpleCont(func(event *beat.Event) error {
return f(event, ip)
})
}
}
// MakeByIPJob builds a new Job based on already known IP. Similar to
// MakeByHostJob, the pingFactory will be used to build the tasks run by the job.
//
// A pingFactory instance is normally build with MakePingIPFactory,
// MakePingAllIPFactory or MakePingAllIPPortFactory.
func MakeByIPJob(
ip net.IP,
pingFactory func(ip *net.IPAddr) jobs.Job,
) (jobs.Job, error) {
// use ResolveIPAddr to parse the ip into net.IPAddr adding a zone info
// if ipv6 is used. We intentionally do not use a custom resolver here.
addr, err := net.ResolveIPAddr("ip", ip.String())
if err != nil {
return nil, err
}
fields := common.MapStr{
"monitor": common.MapStr{"ip": addr.String()},
}
return wrappers.WithFields(fields, pingFactory(addr)), nil
}
// MakeByHostJob creates a new Job including host lookup. The pingFactory will be used to
// build one or multiple Tasks after pluginName lookup according to settings.
//
// A pingFactory instance is normally build with MakePingIPFactory,
// MakePingAllIPFactory or MakePingAllIPPortFactory.
func MakeByHostJob(
host string,
ipSettings IPSettings,
resolver Resolver,
pingFactory func(ip *net.IPAddr) jobs.Job,
) (jobs.Job, error) {
if ip := net.ParseIP(host); ip != nil {
return MakeByIPJob(ip, pingFactory)
}
network := ipSettings.Network()
if network == "" {
return nil, errors.New("pinging hosts requires ipv4 or ipv6 mode enabled")
}
mode := ipSettings.Mode
if mode == PingAny {
return makeByHostAnyIPJob(host, ipSettings, resolver, pingFactory), nil
}
return makeByHostAllIPJob(host, ipSettings, resolver, pingFactory), nil
}
func makeByHostAnyIPJob(
host string,
ipSettings IPSettings,
resolver Resolver,
pingFactory func(ip *net.IPAddr) jobs.Job,
) jobs.Job {
network := ipSettings.Network()
return func(event *beat.Event) ([]jobs.Job, error) {
resolveStart := time.Now()
ip, err := resolver.ResolveIPAddr(network, host)
if err != nil {
return nil, err
}
resolveEnd := time.Now()
resolveRTT := resolveEnd.Sub(resolveStart)
ipFields := resolveIPEvent(ip.String(), resolveRTT)
return wrappers.WithFields(ipFields, pingFactory(ip))(event)
}
}
func makeByHostAllIPJob(
host string,
ipSettings IPSettings,
resolver Resolver,
pingFactory func(ip *net.IPAddr) jobs.Job,
) jobs.Job {
network := ipSettings.Network()
filter := makeIPFilter(network)
return func(event *beat.Event) ([]jobs.Job, error) {
// TODO: check for better DNS IP lookup support:
// - The net.LookupIP drops ipv6 zone index
//
resolveStart := time.Now()
ips, err := net.LookupIP(host)
if err != nil {
return nil, err
}
resolveEnd := time.Now()
resolveRTT := resolveEnd.Sub(resolveStart)
if filter != nil {
ips = filterIPs(ips, filter)
}
if len(ips) == 0 {
err := fmt.Errorf("no %v address resolvable for host %v", network, host)
return nil, err
}
// create ip ping tasks
cont := make([]jobs.Job, len(ips))
for i, ip := range ips {
addr := &net.IPAddr{IP: ip}
ipFields := resolveIPEvent(ip.String(), resolveRTT)
cont[i] = wrappers.WithFields(ipFields, pingFactory(addr))
}
// Ideally we would test this invocation. This function however is really hard to to test given all the extra context it takes in
// In a future refactor we could perhaps test that this in correctly invoked.
eventext.CancelEvent(event)
return cont, err
}
}
func resolveIPEvent(ip string, rtt time.Duration) common.MapStr {
return common.MapStr{
"monitor": common.MapStr{
"ip": ip,
},
"resolve": common.MapStr{
"ip": ip,
"rtt": look.RTT(rtt),
},
}
}
// Unpack sets PingMode from a constant string. Unpack will be called by common.Unpack when
// unpacking into an IPSettings type.
func (p *PingMode) Unpack(s string) error {
switch s {
case "all":
*p = PingAll
case "any":
*p = PingAny
default:
return fmt.Errorf("expecting 'any' or 'all', not '%v'", s)
}
return nil
}
func makeIPFilter(network string) func(net.IP) bool {
switch network {
case "ip4":
return func(i net.IP) bool { return i.To4() != nil }
case "ip6":
return func(i net.IP) bool { return i.To4() == nil && i.To16() != nil }
}
return nil
}
func filterIPs(ips []net.IP, filt func(net.IP) bool) []net.IP {
out := ips[:0]
for _, ip := range ips {
if filt(ip) {
out = append(out, ip)
}
}
return out
}