-
Notifications
You must be signed in to change notification settings - Fork 0
/
ping.go
67 lines (59 loc) · 1.72 KB
/
ping.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
package main
import "time"
import "github.com/prometheus-community/pro-bing"
// PingTracker represents the Ping tracking service
type PingTracker struct {
Config *Config
}
// NewPingTracker returns a new PingTracker with given Config
func NewPingTracker(config *Config) *PingTracker {
return &PingTracker{Config: config}
}
// TrackPingTargets tracks ping targets. Meant to be executed periodically. Starts async pings for each config.
func (p *PingTracker) TrackPingTargets(noExec, debug bool) {
for _, config := range p.Config.PingTrackingConfigs {
go p.ping(noExec, debug, &config)
}
}
// ping executes the ping and decides for executions calls. Meant to be executed async.
func (p *PingTracker) ping(noExec, debug bool, pingConfig *PingConfig) {
pinger, err := probing.NewPinger(pingConfig.Target)
if err != nil {
p.Config.log(err.Error())
p.Config.exec(CalleePing, noExec)
return
}
pinger.Count = 1
pinger.Timeout = pingConfig.PingTimeout
for i := 0; i < pingConfig.RetryCount; i++ {
if debug && i > 0 {
p.Config.log("Retrying: " + pingConfig.Target)
}
err = pinger.Run()
if err != nil {
p.Config.log(err.Error())
p.Config.exec(CalleePing, noExec)
}
success := pinger.Statistics().PacketsRecv > 0
// Check for Config
if pingConfig.OnSuccess {
// If ping returns -> execute. Else return
if success {
p.Config.log("Ping successful. Execution started due to OnSuccess for " + pingConfig.Target)
p.Config.exec(CalleePing, noExec)
}
return
} else {
// If ping fails -> retry. Else return
if success {
return
}
if debug {
p.Config.log("Pinging failed for: " + pingConfig.Target)
}
}
// Wait
time.Sleep(pingConfig.RetryDelay)
}
p.Config.exec(CalleePing, noExec)
}