2021-01-01 09:26:49 -09:00
|
|
|
package check
|
2020-08-20 08:44:56 -08:00
|
|
|
|
2021-01-01 15:43:28 -09:00
|
|
|
import (
|
2021-01-02 06:29:12 -09:00
|
|
|
"alrm/check/ping"
|
2021-01-01 15:43:28 -09:00
|
|
|
"fmt"
|
2021-01-02 13:21:57 -09:00
|
|
|
"strconv"
|
2021-01-03 08:17:02 -09:00
|
|
|
"strings"
|
|
|
|
"time"
|
2021-01-02 13:21:57 -09:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
TK_NONE = iota
|
|
|
|
TK_COUNT
|
|
|
|
TK_TIMEOUT
|
|
|
|
TK_INTERVAL
|
2021-01-03 08:17:02 -09:00
|
|
|
TK_MAXPACKETLOSS
|
2021-01-01 15:43:28 -09:00
|
|
|
)
|
|
|
|
|
2020-08-20 08:44:56 -08:00
|
|
|
type CheckPing struct {
|
2021-01-03 08:17:02 -09:00
|
|
|
Type string
|
|
|
|
Address string
|
|
|
|
Count int
|
|
|
|
Timeout time.Duration
|
|
|
|
Interval time.Duration
|
|
|
|
MaxPacketLoss float64
|
|
|
|
state int
|
2021-01-02 13:21:57 -09:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewCheckPing(addr string) *CheckPing {
|
|
|
|
return &CheckPing{
|
|
|
|
Type: "ping", Address: addr,
|
|
|
|
Count: 1, Timeout: time.Second * 5,
|
2021-01-03 08:17:02 -09:00
|
|
|
Interval: time.Second, MaxPacketLoss: 0,
|
2021-01-02 13:21:57 -09:00
|
|
|
}
|
2020-08-20 08:44:56 -08:00
|
|
|
}
|
|
|
|
|
2021-01-02 06:29:12 -09:00
|
|
|
func (c *CheckPing) Check(debuglvl int) error {
|
|
|
|
if debuglvl > 0 {
|
|
|
|
fmt.Printf("Pinging %s .. \n", c.Address)
|
|
|
|
}
|
|
|
|
|
|
|
|
p, err := ping.NewPinger(c.Address)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-01-02 13:21:57 -09:00
|
|
|
p.Count = c.Count
|
|
|
|
p.Timeout = c.Timeout
|
|
|
|
p.Interval = c.Interval
|
|
|
|
|
2021-01-02 06:29:12 -09:00
|
|
|
err = p.Run()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
stats := p.Statistics()
|
|
|
|
|
2021-01-03 08:17:02 -09:00
|
|
|
if debuglvl > 1 {
|
2021-01-02 13:21:57 -09:00
|
|
|
for i, r := range stats.Rtts {
|
|
|
|
fmt.Printf("Ping %d: %s\n", i+1, r)
|
|
|
|
}
|
2021-01-02 06:29:12 -09:00
|
|
|
}
|
2021-01-03 08:17:02 -09:00
|
|
|
|
|
|
|
if debuglvl > 0 {
|
|
|
|
fmt.Printf("Packet loss: %.f%%\n", stats.PacketLoss)
|
|
|
|
}
|
|
|
|
|
|
|
|
if stats.PacketLoss > c.MaxPacketLoss {
|
|
|
|
return fmt.Errorf("ping packet loss exceeds max")
|
|
|
|
}
|
2020-08-20 08:44:56 -08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *CheckPing) Parse(tk string) (bool, error) {
|
2021-01-02 13:21:57 -09:00
|
|
|
var err error
|
|
|
|
switch c.state {
|
2021-01-03 08:17:02 -09:00
|
|
|
case TK_NONE:
|
|
|
|
switch strings.ToLower(tk) {
|
|
|
|
case "count":
|
|
|
|
c.state = TK_COUNT
|
|
|
|
case "timeout":
|
|
|
|
c.state = TK_TIMEOUT
|
|
|
|
case "interval":
|
|
|
|
c.state = TK_INTERVAL
|
|
|
|
case "maxpacketloss":
|
|
|
|
c.state = TK_MAXPACKETLOSS
|
2021-01-02 13:21:57 -09:00
|
|
|
default:
|
2021-01-03 08:17:02 -09:00
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
case TK_COUNT:
|
|
|
|
c.Count, err = strconv.Atoi(tk)
|
|
|
|
if err != nil {
|
|
|
|
return false, fmt.Errorf("invalid count \"%s\"", tk)
|
|
|
|
}
|
|
|
|
c.state = TK_NONE
|
|
|
|
|
|
|
|
case TK_TIMEOUT:
|
|
|
|
c.Timeout, err = time.ParseDuration(tk)
|
|
|
|
if err != nil {
|
|
|
|
return false, fmt.Errorf("invalid timeout \"%s\"", tk)
|
|
|
|
}
|
|
|
|
c.state = TK_NONE
|
|
|
|
|
|
|
|
case TK_INTERVAL:
|
|
|
|
c.Interval, err = time.ParseDuration(tk)
|
|
|
|
if err != nil {
|
|
|
|
return false, fmt.Errorf("invalid interval \"%s\"", tk)
|
|
|
|
}
|
|
|
|
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")
|
2021-01-02 13:21:57 -09:00
|
|
|
}
|
|
|
|
return true, nil
|
2020-08-20 08:44:56 -08:00
|
|
|
}
|