added maxpacketloss variable for ping check
This commit is contained in:
parent
d80a5a80d7
commit
5c55903e88
@ -3,9 +3,9 @@ package check
|
||||
import (
|
||||
"alrm/check/ping"
|
||||
"fmt"
|
||||
"time"
|
||||
"strings"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
const (
|
||||
@ -13,6 +13,7 @@ const (
|
||||
TK_COUNT
|
||||
TK_TIMEOUT
|
||||
TK_INTERVAL
|
||||
TK_MAXPACKETLOSS
|
||||
)
|
||||
|
||||
type CheckPing struct {
|
||||
@ -21,6 +22,7 @@ type CheckPing struct {
|
||||
Count int
|
||||
Timeout time.Duration
|
||||
Interval time.Duration
|
||||
MaxPacketLoss float64
|
||||
state int
|
||||
}
|
||||
|
||||
@ -28,7 +30,7 @@ func NewCheckPing(addr string) *CheckPing {
|
||||
return &CheckPing{
|
||||
Type: "ping", Address: addr,
|
||||
Count: 1, Timeout: time.Second * 5,
|
||||
Interval: time.Second,
|
||||
Interval: time.Second, MaxPacketLoss: 0,
|
||||
}
|
||||
}
|
||||
|
||||
@ -52,15 +54,20 @@ func (c *CheckPing) Check(debuglvl int) error {
|
||||
}
|
||||
|
||||
stats := p.Statistics()
|
||||
if len(stats.Rtts) < 1 {
|
||||
return fmt.Errorf("ping failure")
|
||||
}
|
||||
|
||||
if debuglvl > 0 {
|
||||
if debuglvl > 1 {
|
||||
for i, r := range stats.Rtts {
|
||||
fmt.Printf("Ping %d: %s\n", i+1, r)
|
||||
}
|
||||
}
|
||||
|
||||
if debuglvl > 0 {
|
||||
fmt.Printf("Packet loss: %.f%%\n", stats.PacketLoss)
|
||||
}
|
||||
|
||||
if stats.PacketLoss > c.MaxPacketLoss {
|
||||
return fmt.Errorf("ping packet loss exceeds max")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
@ -75,6 +82,8 @@ func (c *CheckPing) Parse(tk string) (bool, error) {
|
||||
c.state = TK_TIMEOUT
|
||||
case "interval":
|
||||
c.state = TK_INTERVAL
|
||||
case "maxpacketloss":
|
||||
c.state = TK_MAXPACKETLOSS
|
||||
default:
|
||||
return false, nil
|
||||
}
|
||||
@ -100,6 +109,13 @@ func (c *CheckPing) Parse(tk string) (bool, error) {
|
||||
}
|
||||
c.state = TK_NONE
|
||||
|
||||
case TK_MAXPACKETLOSS:
|
||||
c.MaxPacketLoss, err = strconv.ParseFloat(tk, 64)
|
||||
if err != nil {
|
||||
return false, fmt.Errorf("invalid minpacketloss \"%s\"", tk)
|
||||
}
|
||||
c.state = TK_NONE
|
||||
|
||||
default:
|
||||
return false, fmt.Errorf("invalid state in check_ping")
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user