-
-
Notifications
You must be signed in to change notification settings - Fork 28
/
check_ping.go
112 lines (91 loc) · 3.04 KB
/
check_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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package services
import (
"fmt"
"strconv"
"strings"
"time"
"github.com/go-ping/ping"
)
// Custom errors.
var (
ErrPingExpect = fmt.Errorf("ping/icmp expect must contain three integers separated by two colons. ex: 3:2:500")
ErrNoPingVal = fmt.Errorf("ping or icmp 'check' must not be empty")
ErrPingCountZero = fmt.Errorf("none of the ping expect values may be set to 0")
)
/*
* These all run once at startup to fill our check data.
* The service Lock is acquired before running any of this code.
*/
// pingExpect is setup for each 'ping'/'icmp' service from input data on initialization.
type pingExpect struct {
count int
min int
interval int
icmp bool
}
func (s *Service) checkPingValues(icmp bool) error {
if s.Value == "" {
return ErrNoPingVal
}
if err := s.fillPingExpect(icmp); err != nil {
return fmt.Errorf("ping expect format is count:min:interval where interval is in milliseconds "+
" and min is the minimum packets that must be returned: %w", err)
}
if s.svc.ping.count == 0 || s.svc.ping.min > s.svc.ping.count || s.svc.ping.interval <= 0 {
return fmt.Errorf("%w, ensure minimum(%d) is less than or equal to count(%d) and interval(%d) > 0",
ErrPingExpect, s.svc.ping.min, s.svc.ping.count, s.svc.ping.interval)
}
return nil
}
func (s *Service) fillPingExpect(icmp bool) (err error) {
s.svc.ping = &pingExpect{
icmp: icmp,
}
splitStr := strings.Split(s.Expect, ":")
if len(splitStr) != 3 { //nolint:gomnd
return ErrPingExpect
}
if s.svc.ping.count, err = strconv.Atoi(splitStr[0]); err != nil {
return fmt.Errorf("invalid packet send count: %s: %w", splitStr[0], err)
}
if s.svc.ping.min, err = strconv.Atoi(splitStr[1]); err != nil {
return fmt.Errorf("invalid minimum packet receive count: %s: %w", splitStr[1], err)
}
if s.svc.ping.interval, err = strconv.Atoi(splitStr[2]); err != nil {
return fmt.Errorf("invalid packet send interval: %s: %w", splitStr[2], err)
}
return nil
}
func (s *Service) checkPING() *result {
pinger, err := ping.NewPinger(s.Value)
if err != nil {
return &result{
state: StateUnknown,
output: "invalid ping value: " + err.Error(),
}
}
pinger.SetPrivileged(s.svc.ping.icmp)
pinger.Timeout = s.Timeout.Duration
pinger.Count = s.svc.ping.count
pinger.Interval = time.Duration(s.svc.ping.interval) * time.Millisecond
if err = pinger.Run(); err != nil { // blocks.
return &result{
state: StateCritical,
output: "error pinging service: " + err.Error(),
}
}
stats := pinger.Statistics()
state := StateOK
msg := fmt.Sprintf("rcvd(%d) >= min(%d)", stats.PacketsRecv, s.svc.ping.min)
// Check if we received our minimum packet count responses.
if stats.PacketsRecv < s.svc.ping.min {
state = StateCritical
msg = fmt.Sprintf("rcvd(%d) < min(%d)", stats.PacketsRecv, s.svc.ping.min)
}
return &result{
state: state,
output: fmt.Sprintf("(%s) pkts sent:%d, rcvd:%d, loss:%.01f, max:%s, avg:%s",
msg, stats.PacketsSent, stats.PacketsRecv, stats.PacketLoss,
stats.MaxRtt.Round(time.Millisecond), stats.AvgRtt.Round(time.Millisecond)),
}
}