Ping check actually pings

This commit is contained in:
2021-01-02 06:29:12 -09:00
parent ad3d9fb4d4
commit 4e67a31552
7 changed files with 56 additions and 17 deletions

View File

@ -6,7 +6,7 @@ import (
type AlrmCheck interface {
Parse(string) (bool, error)
Check() error
Check(int) error
}
func NewCheck(name string, addr string) (AlrmCheck, error) {

View File

@ -1,16 +1,43 @@
package check
import (
"alrm/check/ping"
"fmt"
"time"
)
type CheckPing struct {
Type string
Address string
Type string
Address string
Count int
Timeout time.Duration
}
func (c *CheckPing) Check() error {
fmt.Printf("Pinging %s .. \n", c.Address)
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])
}
return nil
}