Added max rtt and max avg rtt to ping check

This commit is contained in:
Christopher Ramey 2021-01-10 09:06:40 -09:00
parent 5c55903e88
commit 233aec7010

View File

@ -14,6 +14,8 @@ const (
TK_TIMEOUT TK_TIMEOUT
TK_INTERVAL TK_INTERVAL
TK_MAXPACKETLOSS TK_MAXPACKETLOSS
TK_MAXRTT
TK_MAXAVGRTT
) )
type CheckPing struct { type CheckPing struct {
@ -23,6 +25,8 @@ type CheckPing struct {
Timeout time.Duration Timeout time.Duration
Interval time.Duration Interval time.Duration
MaxPacketLoss float64 MaxPacketLoss float64
MaxRTT time.Duration
MaxAvgRTT time.Duration
state int state int
} }
@ -63,11 +67,22 @@ func (c *CheckPing) Check(debuglvl int) error {
if debuglvl > 0 { if debuglvl > 0 {
fmt.Printf("Packet loss: %.f%%\n", stats.PacketLoss) fmt.Printf("Packet loss: %.f%%\n", stats.PacketLoss)
fmt.Printf("Max RTT: %s\n", stats.MaxRtt.String())
fmt.Printf("Max Configured RTT: %s\n", c.MaxRTT.String())
fmt.Printf("Average RTT: %s\n", stats.AvgRtt.String())
} }
if stats.PacketLoss > c.MaxPacketLoss { if stats.PacketLoss > c.MaxPacketLoss {
return fmt.Errorf("ping packet loss exceeds max") return fmt.Errorf("ping packet loss exceeds max")
} }
if c.MaxRTT > 0 && stats.MaxRtt > c.MaxRTT {
return fmt.Errorf("ping rtt exceeds max")
}
if c.MaxAvgRTT > 0 && stats.AvgRtt > c.MaxAvgRTT {
return fmt.Errorf("ping average rtt exceeds max")
}
return nil return nil
} }
@ -84,6 +99,10 @@ func (c *CheckPing) Parse(tk string) (bool, error) {
c.state = TK_INTERVAL c.state = TK_INTERVAL
case "maxpacketloss": case "maxpacketloss":
c.state = TK_MAXPACKETLOSS c.state = TK_MAXPACKETLOSS
case "maxrtt":
c.state = TK_MAXRTT
case "maxavgrtt":
c.state = TK_MAXAVGRTT
default: default:
return false, nil return false, nil
} }
@ -116,6 +135,20 @@ func (c *CheckPing) Parse(tk string) (bool, error) {
} }
c.state = TK_NONE c.state = TK_NONE
case TK_MAXRTT:
c.MaxRTT, err = time.ParseDuration(tk)
if err != nil {
return false, fmt.Errorf("invalid maxrtt \"%s\"", tk)
}
c.state = TK_NONE
case TK_MAXAVGRTT:
c.MaxAvgRTT, err = time.ParseDuration(tk)
if err != nil {
return false, fmt.Errorf("invalid maxavgrtt \"%s\"", tk)
}
c.state = TK_NONE
default: default:
return false, fmt.Errorf("invalid state in check_ping") return false, fmt.Errorf("invalid state in check_ping")
} }