1 Star 0 Fork 0

zhangjungang/beats

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
icmp.go 2.22 KB
一键复制 编辑 原始数据 按行查看 历史
Steffen Siering 提交于 2017-04-28 09:08 . Heartbeat event format (#4091)
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
}
network := config.Mode.Network()
pingFactory := monitors.MakePingIPFactory(createPingIPFactory(&config))
for _, host := range config.Hosts {
jobName := fmt.Sprintf("icmp-%v-host-%v@%v", config.Name, network, host)
if ip := net.ParseIP(host); ip != nil {
jobName = fmt.Sprintf("icmp-%v-ip@%v", config.Name, ip.String())
}
settings := monitors.MakeHostJobSettings(jobName, host, config.Mode)
err := addJob(monitors.MakeByHostJob(settings, 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, n, err := loop.ping(ip, config.Timeout, config.Wait)
fields := common.MapStr{"requests": n}
if err == nil {
fields["rtt"] = look.RTT(rtt)
}
event := common.MapStr{"icmp": fields}
return event, err
}
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/zhangjungang/beats.git
git@gitee.com:zhangjungang/beats.git
zhangjungang
beats
beats
v6.0.0-alpha2

搜索帮助