first basic check to tell if the cluster is healthy

Signed-off-by: Jeff Carr <jcarr@wit.com>
This commit is contained in:
Jeff Carr 2024-10-12 12:45:43 -05:00
parent 487c6fd11c
commit e94b4d6626
5 changed files with 102 additions and 21 deletions

46
configfiles.go Normal file
View File

@ -0,0 +1,46 @@
package main
import (
"os"
"path/filepath"
"strings"
"go.wit.com/log"
)
func readConfigFile(filename string) {
// fmt.Fprintln(w, "GOT TEST?")
homeDir, _ := os.UserHomeDir()
fullname := filepath.Join(homeDir, ".config/virtigo/", filename)
pfile, err := os.ReadFile(fullname)
if err != nil {
log.Info("No config file :", err)
// w.Write(pfile)
return
}
f := string(pfile)
for _, line := range strings.Split(f, "\n") {
fields := strings.Fields(line)
if len(fields) < 1 {
continue
}
name := fields[0]
d := findDroplet(name)
if d == nil {
// this is a new unknown droplet (not in the config file)
d = new(DropletT)
d.Hostname = name
if len(fields) < 2 || fields[1] != "ON" {
d.State = "OFF"
} else {
d.State = "ON"
}
me.droplets = append(me.droplets, d)
log.Log(EVENT, "NEW CONFIG DROPLET", d.Hostname, d.State)
} else {
log.Info("not sure what to do here. duplicate", name, "in config file")
}
}
}

29
http.go
View File

@ -6,8 +6,8 @@ import (
"strings" "strings"
"time" "time"
"go.wit.com/log"
"go.wit.com/lib/gui/shell" "go.wit.com/lib/gui/shell"
"go.wit.com/log"
) )
// remove '?' part and trailing '/' // remove '?' part and trailing '/'
@ -30,7 +30,8 @@ func okHandler(w http.ResponseWriter, r *http.Request) {
return return
} }
if tmp == "/vms" { // list all the droplets
if tmp == "/droplets" {
for _, d := range me.droplets { for _, d := range me.droplets {
dur := time.Since(d.lastpoll) // Calculate the elapsed time dur := time.Since(d.lastpoll) // Calculate the elapsed time
fmt.Fprintln(w, d.Hostname, d.hname, shell.FormatDuration(dur)) fmt.Fprintln(w, d.Hostname, d.hname, shell.FormatDuration(dur))
@ -38,6 +39,30 @@ func okHandler(w http.ResponseWriter, r *http.Request) {
return return
} }
// is the cluster running what it should?
if tmp == "/good" {
var good = true
for _, d := range me.droplets {
if d.State != "ON" {
continue
}
dur := time.Since(d.lastpoll) // Calculate the elapsed time
if d.CurrentState != "ON" {
fmt.Fprintln(w, "BAD STATE ", d.Hostname, "State =", d.State, "CurrentState =", d.CurrentState, shell.FormatDuration(dur))
good = false
} else {
dur := time.Since(d.lastpoll) // Calculate the elapsed time
fmt.Fprintln(w, "GOOD STATE ON", d.Hostname, d.hname, shell.FormatDuration(dur))
}
}
if good {
fmt.Fprintln(w, "GOOD=true")
} else {
fmt.Fprintln(w, "GOOD=false")
}
return
}
if tmp == "/favicon.ico" { if tmp == "/favicon.ico" {
writeFile(w, "ipv6.png") writeFile(w, "ipv6.png")
return return

View File

@ -24,6 +24,8 @@ func main() {
os.Exit(0) os.Exit(0)
} }
readConfigFile("droplets")
log.Info("create cluser for", argv.Hosts) log.Info("create cluser for", argv.Hosts)
for _, s := range argv.Hosts { for _, s := range argv.Hosts {
me.names = append(me.names, s) me.names = append(me.names, s)

36
poll.go
View File

@ -10,7 +10,7 @@ import (
func (h HyperT) pollHypervisor() { func (h HyperT) pollHypervisor() {
url := "http://" + h.Hostname + ":2520/vms" url := "http://" + h.Hostname + ":2520/vms"
log.Log(INFO, "wget url =", url) log.Log(POLL, "wget url =", url)
s := shell.Wget(url) s := shell.Wget(url)
if s == nil { if s == nil {
return return
@ -30,30 +30,36 @@ func (h HyperT) pollHypervisor() {
name := fields[1] name := fields[1]
if state == "ON" { if state == "ON" {
log.Log(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 d := findDroplet(name)
for _, d := range me.droplets { if d != nil {
if d.Hostname == name { log.Log(INFO, "ALREADY RECORDED", d.Hostname)
log.Log(INFO, "ALREADY RECORDED", d.Hostname) d.lastpoll = time.Now()
found = true d.CurrentState = "ON"
d.lastpoll = time.Now() // log.Info("ALREADY RECORDED", d.Hostname, d.lastpoll)
// log.Info("ALREADY RECORDED", d.Hostname, d.lastpoll) if d.hname != h.Hostname {
if d.hname != h.Hostname { log.Log(EVENT, "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 d.hname = h.Hostname
} }
}
if found {
continue continue
} }
var d = new(DropletT) // this is a new unknown droplet (not in the config file)
d = new(DropletT)
d.Hostname = name d.Hostname = name
d.hname = h.Hostname d.hname = h.Hostname
d.lastpoll = time.Now() d.lastpoll = time.Now()
d.CurrentState = "ON"
me.droplets = append(me.droplets, d) me.droplets = append(me.droplets, d)
log.Log(EVENT, name, "IS NEW. ADDED ON", h.Hostname) log.Log(EVENT, name, "IS NEW. ADDED ON", h.Hostname)
} }
} }
// log.Info("i, s =", hostname, i, s) // log.Info("i, s =", hostname, i, s)
} }
func findDroplet(name string) *DropletT {
for _, d := range me.droplets {
if d.Hostname == name {
return d
}
}
return nil
}

View File

@ -32,8 +32,10 @@ type HyperT struct {
// the stuff that is needed for a hypervisor // the stuff that is needed for a hypervisor
type DropletT struct { type DropletT struct {
Hostname string // the name of the virtual machine. should be unique (probably enforce this forever) Hostname string // the name of the virtual machine. should be unique (probably enforce this forever)
hname string // the hypervisor it's currently running on State string // what the state of the droplet is SUPPOSED TO BE
h *HyperT // the hypervisor it's currently running on CurrentState string // what the state of the droplet is ACTUALLY IS
lastpoll time.Time // the last time the droplet was seen running hname string // the hypervisor it's currently running on
h *HyperT // the hypervisor it's currently running on
lastpoll time.Time // the last time the droplet was seen running
} }