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 06:29:12 -09:00
|
|
|
"time"
|
2021-01-01 15:43:28 -09:00
|
|
|
)
|
|
|
|
|
2020-08-20 08:44:56 -08:00
|
|
|
type CheckPing struct {
|
2021-01-02 06:29:12 -09:00
|
|
|
Type string
|
|
|
|
Address string
|
|
|
|
Count int
|
|
|
|
Timeout time.Duration
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
p.Count = 1
|
|
|
|
p.Timeout = time.Second * 5
|
|
|
|
err = p.Run()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
stats := p.Statistics()
|
|
|
|
if len(stats.Rtts) < 1 {
|
|
|
|
return fmt.Errorf("ping failure")
|
|
|
|
}
|
|
|
|
|
|
|
|
if debuglvl > 0 {
|
|
|
|
fmt.Printf("Ping RTT: %s\n", stats.Rtts[0])
|
|
|
|
}
|
2020-08-20 08:44:56 -08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *CheckPing) Parse(tk string) (bool, error) {
|
|
|
|
return false, nil
|
|
|
|
}
|