virtigo/poll.go

66 lines
1.5 KiB
Go
Raw Normal View History

package main
import (
"strings"
"time"
"go.wit.com/lib/gui/shell"
"go.wit.com/log"
)
func (h HyperT) pollHypervisor() {
url := "http://" + h.Hostname + ":2520/vms"
log.Log(POLL, "wget url =", url)
s := shell.Wget(url)
if s == nil {
return
}
var bytesSplice []byte
bytesSplice = s.Bytes()
// fmt.Fprintln(w, string(bytesSplice))
for _, line := range strings.Split(string(bytesSplice), "\n") {
if line == "" {
continue
}
fields := strings.Fields(line)
if len(fields) < 2 {
continue
}
state := fields[0]
name := fields[1]
if state == "ON" {
log.Log(POLL, h.Hostname, "STATE:", state, "HOST:", name, "rest:", fields[2:])
d := findDroplet(name)
if d != nil {
log.Log(INFO, "ALREADY RECORDED", d.Hostname)
d.lastpoll = time.Now()
d.CurrentState = "ON"
// log.Info("ALREADY RECORDED", d.Hostname, d.lastpoll)
if d.hname != h.Hostname {
log.Log(EVENT, "DROPLET", d.Hostname, "MOVED FROM", d.hname, "TO", d.Hostname)
d.hname = h.Hostname
}
continue
}
// this is a new unknown droplet (not in the config file)
d = new(DropletT)
d.Hostname = name
d.hname = h.Hostname
d.lastpoll = time.Now()
d.CurrentState = "ON"
me.droplets = append(me.droplets, d)
log.Log(EVENT, name, "IS NEW. ADDED ON", h.Hostname)
}
}
// 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
}