forked from cloudradar/frontman
-
Notifications
You must be signed in to change notification settings - Fork 0
/
healthcheck.go
68 lines (60 loc) · 1.54 KB
/
healthcheck.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
68
package frontman
import (
"fmt"
"strings"
"sync"
"time"
"github.com/go-ping/ping"
"github.com/sirupsen/logrus"
)
// HealthCheck runs before any other check to ensure that the host itself and its network are healthly.
// This is useful to confirm a stable internet connection to avoid false alerts due to network outages.
func (fm *Frontman) HealthCheck() error {
hcfg := fm.Config.HealthChecks
if len(hcfg.ReferencePingHosts) == 0 {
return nil
}
if hcfg.ReferencePingCount == 0 {
return nil
}
timeout := secToDuration(hcfg.ReferencePingTimeout)
if timeout == 0 {
// use the default timeout
timeout = 500 * time.Millisecond
}
failC := make(chan string, len(hcfg.ReferencePingHosts))
wg := new(sync.WaitGroup)
for _, addr := range hcfg.ReferencePingHosts {
pinger, err := ping.NewPinger(addr)
if err != nil {
logrus.WithError(err).Warningln("failed to parse host for ICMP ping")
continue
}
pinger.Timeout = timeout
pinger.Count = hcfg.ReferencePingCount
wg.Add(1)
go func(addr string) {
defer wg.Done()
pinger.Run()
if pinger.Statistics().PacketLoss > 0 {
failC <- addr
}
}(addr)
}
go func() {
wg.Wait()
close(failC)
}()
failedHosts := []string{}
for host := range failC {
failedHosts = append(failedHosts, host)
}
fm.statsLock.Lock()
fm.stats.HealthChecksPerformed++
fm.stats.HealthChecksLastTimestamp = uint64(time.Now().Unix())
fm.statsLock.Unlock()
if len(failedHosts) > 0 {
return fmt.Errorf("host(s) failed to respond to ICMP ping: %s", strings.Join(failedHosts, ", "))
}
return nil
}