Skip to content

Commit

Permalink
Hopefully fixed pinger logic
Browse files Browse the repository at this point in the history
  • Loading branch information
tobias- committed Nov 18, 2024
1 parent 937288f commit 044db10
Showing 1 changed file with 19 additions and 16 deletions.
35 changes: 19 additions & 16 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,35 +78,38 @@ func main() {
if err != nil {
log.Logger.Fatal().Err(err).Msgf("Failed to create pinger for %s", opts.IP)
}
pinger.Count = 1
pinger.Timeout = time.Duration(opts.Timeout) * time.Millisecond
pinger.SetPrivileged(!opts.Unprivileged)

if opts.MqttUri != "" {
setupMqtt()
}

failureCount := 0
for {
err = pingIP(pinger)
acceptedDiff := 0
pinger.OnRecv = func(packet *ping.Packet) {
log.Debug().Msgf("Successfully pinged %s", opts.IP)
setStatus(true)
statistics := pinger.Statistics()
acceptedDiff = statistics.PacketsSent - statistics.PacketsRecv
}

go func() {
err := pinger.Run()
if err != nil {
failureCount++
log.Warn().Err(err).Msgf("Failed to ping %s. Failure count: %d", opts.IP, failureCount)
setStatus(false)
} else {
log.Debug().Msgf("Successfully pinged %s", opts.IP)
failureCount = 0
setStatus(true)
log.Logger.Error().Err(err).Msgf("Failed to ping %s", pinger.Addr())
}
}()
defer pinger.Stop()

if failureCount >= opts.MaxFailures {
for {
time.Sleep(time.Duration(opts.Timeout) * time.Millisecond)
stats := pinger.Statistics()
if acceptedDiff+stats.PacketsRecv+opts.MaxFailures < stats.PacketsSent {
log.Error().Msgf("Ping to %s failed %d times consecutively. Running command: %s", opts.IP, opts.MaxFailures, strings.Join(opts.Command.Args, " "))
setStatus(false)
runCommand(opts.Command.Args)
failureCount = 0
time.Sleep(time.Duration(opts.Cooldown) * time.Millisecond)
}

time.Sleep(time.Duration(opts.Interval) * time.Millisecond)
log.Debug().Msgf("Sent %d packets and received %d packets and accepted diff: %d", stats.PacketsSent, stats.PacketsRecv, acceptedDiff)
}
}

Expand Down

0 comments on commit 044db10

Please sign in to comment.