polling duration times work

Signed-off-by: Jeff Carr <jcarr@wit.com>
This commit is contained in:
Jeff Carr 2024-10-12 11:54:01 -05:00
parent a6b385e216
commit 487c6fd11c
6 changed files with 37 additions and 17 deletions

17
argv.go
View File

@ -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")
}

View File

@ -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
}

10
main.go
View File

@ -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()
}

14
poll.go
View File

@ -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)

View File

@ -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

View File

@ -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()
}