From 62e9d8cfb1563ea7dbedf26a0dc593e4227cf413 Mon Sep 17 00:00:00 2001 From: Jeff Carr Date: Sun, 13 Oct 2024 00:40:22 -0500 Subject: [PATCH] cleanup STDOUT debugging Signed-off-by: Jeff Carr --- http.go | 35 ++++++++++++++++++----------------- main.go | 3 ++- poll.go | 19 +++++++++++-------- structs.go | 3 ++- 4 files changed, 33 insertions(+), 27 deletions(-) diff --git a/http.go b/http.go index 59a30be..ebcaa70 100644 --- a/http.go +++ b/http.go @@ -20,18 +20,9 @@ func okHandler(w http.ResponseWriter, r *http.Request) { var tmp string tmp = cleanURL(r.URL.Path) - log.Info("Handling URL:", tmp) - if tmp == "/" { - fmt.Fprintln(w, "OK") - return - } - if tmp == "/me" { - fmt.Fprintln(w, "OK") - return - } - // list all the droplets if tmp == "/droplets" { + log.Info("Handling URL:", tmp) for _, d := range me.droplets { dur := time.Since(d.lastpoll) // Calculate the elapsed time fmt.Fprintln(w, d.Hostname, d.hname, shell.FormatDuration(dur)) @@ -56,9 +47,9 @@ func okHandler(w http.ResponseWriter, r *http.Request) { } } if good { - fmt.Fprintln(w, "GOOD=true") + fmt.Fprintln(w, "Handling URL:", tmp, "GOOD=true") } else { - fmt.Fprintln(w, "GOOD=false") + fmt.Fprintln(w, "Handling URL:", tmp, "GOOD=false") } return } @@ -69,18 +60,28 @@ func okHandler(w http.ResponseWriter, r *http.Request) { } if tmp == "/uptime" { if clusterHealthy() { + log.Info("Handling URL:", tmp, "1 GOOD=true") fmt.Fprintln(w, "GOOD=true") } else { + log.Info("Handling URL:", tmp, "1 GOOD=false") fmt.Fprintln(w, "GOOD=false") } + for _, h := range me.hypers { + dur := time.Since(h.lastpoll) // Calculate the elapsed time + if dur > 2 * time.Minute { + url := "http://" + h.Hostname + ":2520/kill" + log.Info("KILL DAEMON ON", h.Hostname, shell.FormatDuration(dur), "curl", url) + } + // l := shell.FormatDuration(dur) + // log.Warn("HOST =", h.Hostname, "Last poll =", l) + //if d.State != "ON" { + // continue + //} + // dur := time.Since(d.lastpoll) // Calculate the elapsed time + } return } - // used for uptime monitor checking (like Kuma) - if tmp == "/uptime" { - writeFile(w, "uptime.html") - return - } log.Warn("BAD URL =", tmp) fmt.Fprintln(w, "BAD ZOOT") // badurl(w, r.URL.String()) diff --git a/main.go b/main.go index fd3884a..f7e14a6 100644 --- a/main.go +++ b/main.go @@ -35,10 +35,11 @@ func main() { me.names = append(me.names, s) log.Info("Making a hypervisor struct for", s) - var h HyperT + h := new(HyperT) h.Hostname = s h.Autoscan = true h.Delay = 5 * time.Second + h.lastpoll = time.Now() h.Scan = func() { h.pollHypervisor() } diff --git a/poll.go b/poll.go index 42b0455..6d7ebc7 100644 --- a/poll.go +++ b/poll.go @@ -8,7 +8,7 @@ import ( "go.wit.com/log" ) -func (h HyperT) pollHypervisor() { +func (h *HyperT) pollHypervisor() { url := "http://" + h.Hostname + ":2520/vms" log.Log(POLL, "wget url =", url) s := shell.Wget(url) @@ -55,7 +55,7 @@ func (h HyperT) pollHypervisor() { log.Log(EVENT, name, "IS NEW. ADDED ON", h.Hostname) } } - // log.Info("i, s =", hostname, i, s) + h.lastpoll = time.Now() } func findDroplet(name string) *DropletT { @@ -74,6 +74,10 @@ func clusterHealthy() bool { continue } dur := time.Since(d.lastpoll) // Calculate the elapsed time + if d.CurrentState == "" { + // log.Info("SKIP. hostname has not been polled yet", d.Hostname, d.hname) + continue + } if d.CurrentState != "ON" { log.Info("BAD STATE", d.State, d.Hostname, d.hname, "CurrentState =", d.CurrentState, shell.FormatDuration(dur)) good = false @@ -84,13 +88,12 @@ func clusterHealthy() bool { good = false d.CurrentState = "MISSING" } - log.Info("GOOD STATE ON", d.Hostname, d.hname, shell.FormatDuration(dur)) + l := shell.FormatDuration(dur) + if l == "" { + log.Info("DUR IS EMPTY", dur) + } + // log.Info("GOOD STATE ON", d.Hostname, d.hname, "dur =", l) } } - if good { - log.Info("GOOD=true") - } else { - log.Info("GOOD=false") - } return good } diff --git a/structs.go b/structs.go index e39b8ce..02d1b7f 100644 --- a/structs.go +++ b/structs.go @@ -17,7 +17,7 @@ func (b *virtigoT) Enable() { // this app's variables type virtigoT struct { names []string - hypers []HyperT + hypers []*HyperT droplets []*DropletT } @@ -28,6 +28,7 @@ type HyperT struct { Autoscan bool // to scan or not to scan Delay time.Duration // how often to poll the hypervisor Dog *time.Ticker // the watchdog timer itself + lastpoll time.Time // the last time the hypervisor polled } // the stuff that is needed for a hypervisor