Ping check actually pings

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

2
alrmrc
View File

@ -8,7 +8,7 @@ monitor group webservers
check ping # or the end of a line check ping # or the end of a line
check ping # checks are not named, so multiple is okay check ping # checks are not named, so multiple is okay
monitor host gateway address 10.79.37.200 monitor host gateway address 10.79.37.220
check ping check ping
# Hosts without any checks will always be successful # Hosts without any checks will always be successful

View File

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

View File

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

View File

@ -23,10 +23,10 @@ func (ag *AlrmGroup) NewHost(name string) (*AlrmHost, error) {
return host, nil return host, nil
} }
func (ag *AlrmGroup) Check() error { func (ag *AlrmGroup) Check(debuglvl int) error {
for _, host := range ag.Hosts { for _, host := range ag.Hosts {
for _, chk := range host.Checks { for _, chk := range host.Checks {
err := chk.Check() err := chk.Check(debuglvl)
if err != nil { if err != nil {
return err return err
} }

5
go.mod Normal file
View File

@ -0,0 +1,5 @@
module alrm
go 1.15
require golang.org/x/net v0.0.0-20201224014010-6772e930b67b

7
go.sum Normal file
View File

@ -0,0 +1,7 @@
golang.org/x/net v0.0.0-20201224014010-6772e930b67b h1:iFwSg7t5GZmB/Q5TjiEAsdoLDrdJRC1RiF2WhuV29Qw=
golang.org/x/net v0.0.0-20201224014010-6772e930b67b/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68 h1:nxC68pudNYkKU6jWhgrqdreuFiOQWj1Fs7T3VrH4Pjw=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=

18
main.go
View File

@ -44,7 +44,7 @@ func main() {
} }
fmt.Fprintf(os.Stdout, "%s\n", string(o)) fmt.Fprintf(os.Stdout, "%s\n", string(o))
case "check", "config", "": case "config", "":
_, err := config.ReadConfig(*cfgPath, *debuglvl) _, err := config.ReadConfig(*cfgPath, *debuglvl)
if err != nil { if err != nil {
fmt.Fprintf(os.Stderr, "%s\n", err.Error()) fmt.Fprintf(os.Stderr, "%s\n", err.Error())
@ -52,26 +52,26 @@ func main() {
} }
fmt.Fprintf(os.Stdout, "Config is OK.\n") fmt.Fprintf(os.Stdout, "Config is OK.\n")
case "test": case "check":
cfg, err := config.ReadConfig(*cfgPath, *debuglvl)
if err != nil {
fmt.Fprintf(os.Stderr, "%s\n", err.Error())
os.Exit(1)
}
tn := flag.Arg(1) tn := flag.Arg(1)
if tn == "" { if tn == "" {
fmt.Fprintf(os.Stderr, "test requires a host or group\n") fmt.Fprintf(os.Stderr, "test requires a host or group\n")
os.Exit(1) os.Exit(1)
} }
cfg, err := config.ReadConfig(*cfgPath, 0)
if err != nil {
fmt.Fprintf(os.Stderr, "%s\n", err.Error())
os.Exit(1)
}
group, exists := cfg.Groups[tn] group, exists := cfg.Groups[tn]
if !exists { if !exists {
fmt.Fprintf(os.Stderr, "group or host is not defined\n") fmt.Fprintf(os.Stderr, "group or host is not defined\n")
os.Exit(1) os.Exit(1)
} }
err = group.Check() err = group.Check(*debuglvl)
if err != nil { if err != nil {
fmt.Fprintf(os.Stderr, "Check failed: %s\n", err.Error()) fmt.Fprintf(os.Stderr, "Check failed: %s\n", err.Error())
os.Exit(1) os.Exit(1)