1 Star 0 Fork 0

zhangjungang/beats

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
task.go 1.21 KB
一键复制 编辑 原始数据 按行查看 历史
package tcp
import (
"time"
"github.com/elastic/beats/libbeat/common"
"github.com/elastic/beats/libbeat/outputs/transport"
"github.com/elastic/beats/heartbeat/look"
"github.com/elastic/beats/heartbeat/reason"
)
func pingHost(
dialer transport.Dialer,
host string,
timeout time.Duration,
validator ConnCheck,
) (common.MapStr, error) {
start := time.Now()
deadline := start.Add(timeout)
conn, err := dialer.Dial("tcp", host)
if err != nil {
debugf("dial failed with: %v", err)
return nil, reason.IOFailed(err)
}
defer conn.Close()
if validator == nil {
// no additional validation step => ping success
return common.MapStr{}, nil
}
if err := conn.SetDeadline(deadline); err != nil {
debugf("setting connection deadline failed with: %v", err)
return nil, reason.IOFailed(err)
}
validateStart := time.Now()
err = validator.Validate(conn)
if err != nil && err != errRecvMismatch {
debugf("check failed with: %v", err)
return nil, reason.IOFailed(err)
}
end := time.Now()
event := common.MapStr{
"tcp": common.MapStr{
"rtt": common.MapStr{
"validate": look.RTT(end.Sub(validateStart)),
},
},
}
if err != nil {
event["error"] = reason.FailValidate(err)
}
return event, nil
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/zhangjungang/beats.git
git@gitee.com:zhangjungang/beats.git
zhangjungang
beats
beats
v6.2.3

搜索帮助

0d507c66 1850385 C8b1a773 1850385