From 487c6fd11c2746a71fee1a6b562e41fbb2486382 Mon Sep 17 00:00:00 2001 From: Jeff Carr Date: Sat, 12 Oct 2024 11:54:01 -0500 Subject: [PATCH] polling duration times work Signed-off-by: Jeff Carr --- argv.go | 17 +++++++++++++++++ http.go | 7 +++++-- main.go | 10 ++++------ poll.go | 14 ++++++++------ structs.go | 2 +- watchdog.go | 4 ++-- 6 files changed, 37 insertions(+), 17 deletions(-) diff --git a/argv.go b/argv.go index 4bed667..6584e1a 100644 --- a/argv.go +++ b/argv.go @@ -1,5 +1,7 @@ package main +import "go.wit.com/log" + /* this parses the command line arguements @@ -25,3 +27,18 @@ go install go.wit.com/apps/virtigo@latest func (args) Version() string { return "virtigo " + Version } + +var INFO *log.LogFlag +var POLL *log.LogFlag +var WARN *log.LogFlag +var EVENT *log.LogFlag + +func init() { + full := "go.wit.com/apps/virtigo" + short := "virtigo" + + INFO = log.NewFlag("INFO", false, full, short, "general virtigo") + POLL = log.NewFlag("POLL", false, full, short, "virtigo polling") + WARN = log.NewFlag("WARN", true, full, short, "bad things") + EVENT = log.NewFlag("EVENT", true, full, short, "hypeprvisor/droplet events") +} diff --git a/http.go b/http.go index 2188ed5..e4c9401 100644 --- a/http.go +++ b/http.go @@ -4,8 +4,10 @@ import ( "fmt" "net/http" "strings" + "time" "go.wit.com/log" + "go.wit.com/lib/gui/shell" ) // remove '?' part and trailing '/' @@ -18,7 +20,7 @@ func okHandler(w http.ResponseWriter, r *http.Request) { var tmp string tmp = cleanURL(r.URL.Path) - log.Info("Got URL:", tmp) + log.Info("Handling URL:", tmp) if tmp == "/" { fmt.Fprintln(w, "OK") return @@ -30,7 +32,8 @@ func okHandler(w http.ResponseWriter, r *http.Request) { if tmp == "/vms" { for _, d := range me.droplets { - fmt.Fprintln(w, d.Hostname, d.hname, d.lastpoll) + dur := time.Since(d.lastpoll) // Calculate the elapsed time + fmt.Fprintln(w, d.Hostname, d.hname, shell.FormatDuration(dur)) } return } diff --git a/main.go b/main.go index cf6456d..d04d9eb 100644 --- a/main.go +++ b/main.go @@ -24,26 +24,24 @@ func main() { os.Exit(0) } - log.Info("connect to cluser here", argv.Hosts) + log.Info("create cluser for", argv.Hosts) for _, s := range argv.Hosts { me.names = append(me.names, s) - log.Info("Make a hypervisor struct for", s) + log.Info("Making a hypervisor struct for", s) var h HyperT h.Hostname = s h.Autoscan = true h.Delay = 5 * time.Second h.Scan = func() { - log.Info("scanned farm03?") h.pollHypervisor() } me.hypers = append(me.hypers, h) } - log.Info("me.names =", me.names) + // start the watchdog polling for each hypervisor for _, h := range me.hypers { - log.Info("me hostname =", h.Hostname) - log.Info("should start watchdog here for hostname =", h.Hostname) + log.Info("starting watchdog here for hostname =", h.Hostname) go h.NewWatchdog() } diff --git a/poll.go b/poll.go index b7daeb9..5e9fdb0 100644 --- a/poll.go +++ b/poll.go @@ -10,7 +10,7 @@ import ( func (h HyperT) pollHypervisor() { url := "http://" + h.Hostname + ":2520/vms" - log.Info("wget url =", url) + log.Log(INFO, "wget url =", url) s := shell.Wget(url) if s == nil { return @@ -29,15 +29,16 @@ func (h HyperT) pollHypervisor() { state := fields[0] name := fields[1] if state == "ON" { - log.Info("POLL", h.Hostname, "STATE:", state, "HOST:", name, "rest:", fields[2:]) + log.Log(POLL, h.Hostname, "STATE:", state, "HOST:", name, "rest:", fields[2:]) var found = false for _, d := range me.droplets { if d.Hostname == name { - log.Info("ALREADY RECORDED", d.Hostname) + log.Log(INFO, "ALREADY RECORDED", d.Hostname) found = true d.lastpoll = time.Now() + // log.Info("ALREADY RECORDED", d.Hostname, d.lastpoll) if d.hname != h.Hostname { - log.Info("DROPLET", d.Hostname, "MOVED FROM", d.hname, "TO", d.Hostname) + log.Log(EVENT, "DROPLET", d.Hostname, "MOVED FROM", d.hname, "TO", d.Hostname) } d.hname = h.Hostname @@ -46,11 +47,12 @@ func (h HyperT) pollHypervisor() { if found { continue } - var d DropletT + var d = new(DropletT) d.Hostname = name d.hname = h.Hostname + d.lastpoll = time.Now() me.droplets = append(me.droplets, d) - log.Info(name, "IS NEW. ADDED ON", h.Hostname) + log.Log(EVENT, name, "IS NEW. ADDED ON", h.Hostname) } } // log.Info("i, s =", hostname, i, s) diff --git a/structs.go b/structs.go index 7a0f0ee..c747661 100644 --- a/structs.go +++ b/structs.go @@ -18,7 +18,7 @@ func (b *virtigoT) Enable() { type virtigoT struct { names []string hypers []HyperT - droplets []DropletT + droplets []*DropletT } // the stuff that is needed for a hypervisor diff --git a/watchdog.go b/watchdog.go index 155c79d..41bc698 100644 --- a/watchdog.go +++ b/watchdog.go @@ -12,7 +12,7 @@ import ( func (h *HyperT) NewWatchdog() { var delay int = 99 var i int = delay - h.MyTicker(h.Delay, "new Watchdog() "+h.Hostname, func() { + h.MyTicker(h.Delay, h.Hostname, func() { i += 1 // check if the env var is set to autoscan if os.Getenv("WATCHDOG_AUTO_SCAN") != "true" { @@ -56,7 +56,7 @@ func (h *HyperT) MyTicker(t time.Duration, name string, f func()) { fmt.Println("Done!") return case t := <-h.Dog.C: - log.Info(name, "Current time: ", t) + log.Log(POLL, "Watchdog() ticked", name, "Current time: ", t) h.Scan() // f() }