package main import ( "errors" "fmt" "time" "status/check" "status/db" ) type checkTarget struct { CheckType string `json:"type"` ServiceName string `json:"name"` Target string `json:"target"` } func checkLoop(targets []checkTarget) { lastTime := time.Now() for { if time.Since(lastTime) < time.Minute { secondsUntilNextMinute := 60 - time.Now().Second() time.Sleep(time.Duration(secondsUntilNextMinute) * time.Second) lastTime = time.Now() } for _, t := range targets { currentTime := time.Now() var duration time.Duration var result check.Result var err error switch t.CheckType { case "http": duration, result, err = check.HTTPPing(t.Target) case "icmp": duration, result, err = check.ICMPPing(t.Target) default: result = check.Offline err = errors.New("unknown check type") } if err != nil { fmt.Printf("[ERROR] Failed checking `%s` with `%s` with error: `%s`", t.Target, t.CheckType, err) } entry := db.PingEntry{ ServiceName: t.ServiceName, Ping: duration.Milliseconds(), Status: result, Time: currentTime, } entry.AddToDB() } } }