forked from elastic/beats
-
Notifications
You must be signed in to change notification settings - Fork 0
/
icmp.go
100 lines (83 loc) · 2.24 KB
/
icmp.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
package icmp
import (
"fmt"
"net"
"github.com/elastic/beats/libbeat/common"
"github.com/elastic/beats/libbeat/logp"
"github.com/elastic/beats/heartbeat/look"
"github.com/elastic/beats/heartbeat/monitors"
)
func init() {
monitors.RegisterActive("icmp", create)
}
var debugf = logp.MakeDebug("icmp")
func create(
info monitors.Info,
cfg *common.Config,
) ([]monitors.Job, error) {
config := DefaultConfig
if err := cfg.Unpack(&config); err != nil {
return nil, err
}
// TODO: check icmp is support by OS + check we've
// got required credentials (implementation uses RAW socket, requires root +
// not supported on all OSes)
// TODO: replace icmp package base reader/sender using raw sockets with
// OS specific solution
var jobs []monitors.Job
addJob := func(t monitors.Job, err error) error {
if err != nil {
return err
}
jobs = append(jobs, t)
return nil
}
ipVersion := config.Mode.Network()
if len(config.Hosts) > 0 && ipVersion == "" {
err := fmt.Errorf("pinging hosts requires ipv4 or ipv6 mode enabled")
return nil, err
}
var loopErr error
loopInit.Do(func() {
debugf("initialize icmp handler")
loop, loopErr = newICMPLoop()
})
if loopErr != nil {
debugf("Failed to initialize ICMP loop %v", loopErr)
return nil, loopErr
}
if err := loop.checkNetworkMode(ipVersion); err != nil {
return nil, err
}
typ := config.Name
network := config.Mode.Network()
pingFactory := monitors.MakePingIPFactory(nil, createPingIPFactory(&config))
for _, host := range config.Hosts {
ip := net.ParseIP(host)
if ip != nil {
name := fmt.Sprintf("icmp-ip@%v", ip.String())
err := addJob(monitors.MakeByIPJob(name, typ, ip, pingFactory))
if err != nil {
return nil, err
}
continue
}
name := fmt.Sprintf("%v-host-%v@%v", config.Name, network, host)
err := addJob(monitors.MakeByHostJob(name, typ, host, config.Mode, pingFactory))
if err != nil {
return nil, err
}
}
return jobs, nil
}
func createPingIPFactory(config *Config) func(*net.IPAddr) (common.MapStr, error) {
return func(ip *net.IPAddr) (common.MapStr, error) {
rtt, _, err := loop.ping(ip, config.Timeout, config.Wait)
if err != nil {
return nil, err
}
return common.MapStr{
"icmp_rtt": look.RTT(rtt),
}, nil
}
}