1 Star 0 Fork 0

zhangjungang/beats

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
icmp.go 2.24 KB
一键复制 编辑 原始数据 按行查看 历史
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
}
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/zhangjungang/beats.git
git@gitee.com:zhangjungang/beats.git
zhangjungang
beats
beats
v5.6.0

搜索帮助