150 lines
3.5 KiB
Go
150 lines
3.5 KiB
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"net/http"
|
|
"strings"
|
|
"time"
|
|
|
|
"go.wit.com/lib/gui/shell"
|
|
"go.wit.com/log"
|
|
)
|
|
|
|
// remove '?' part and trailing '/'
|
|
func cleanURL(url string) string {
|
|
url = "/" + strings.Trim(url, "/")
|
|
return url
|
|
}
|
|
|
|
func okHandler(w http.ResponseWriter, r *http.Request) {
|
|
var tmp string
|
|
tmp = cleanURL(r.URL.Path)
|
|
|
|
// is the cluster running what it should?
|
|
if tmp == "/droplets" {
|
|
for _, d := range me.droplets {
|
|
if d.pb.StartState != "ON" {
|
|
continue
|
|
}
|
|
dur := time.Since(d.lastpoll) // Calculate the elapsed time
|
|
var hname string
|
|
if d.h == nil {
|
|
hname = ""
|
|
} else {
|
|
hname = d.h.pb.Hostname
|
|
}
|
|
if d.CurrentState != "ON" {
|
|
fmt.Fprintln(w, "BAD STATE ", d.pb.Hostname, hname, "(", d.pb.StartState, "vs", d.CurrentState, ")", shell.FormatDuration(dur))
|
|
} else {
|
|
dur := time.Since(d.lastpoll) // Calculate the elapsed time
|
|
fmt.Fprintln(w, "GOOD STATE ON", d.pb.Hostname, hname, shell.FormatDuration(dur))
|
|
}
|
|
}
|
|
return
|
|
}
|
|
|
|
if tmp == "/favicon.ico" {
|
|
// w.Header().Set("Content-Type", "image/svg+xml")
|
|
w.Header().Set("Content-Type", "image/png")
|
|
writeFile(w, "ipv6.png")
|
|
return
|
|
}
|
|
|
|
if tmp == "/goReference.svg" {
|
|
w.Header().Set("Content-Type", "image/svg+xml")
|
|
writeFile(w, "goReference.svg")
|
|
return
|
|
}
|
|
|
|
if tmp == "/writeconfig" {
|
|
writeConfigFile()
|
|
writeConfigFileDroplets()
|
|
fmt.Fprintln(w, "OK")
|
|
return
|
|
}
|
|
|
|
if tmp == "/dumplibvirtxml" {
|
|
dumpLibvirtxmlDomainNames()
|
|
return
|
|
}
|
|
|
|
if tmp == "/uptime" {
|
|
b, s := clusterHealthy()
|
|
if b {
|
|
log.Info("Handling URL:", tmp, "cluster is ok", s)
|
|
fmt.Fprintln(w, s)
|
|
} else {
|
|
log.Info("Handling URL:", tmp, "cluster is not right yet", s)
|
|
fmt.Fprintln(w, s)
|
|
}
|
|
for _, h := range me.hypers {
|
|
url := "http://" + h.pb.Hostname + ":2520/kill"
|
|
dur := time.Since(h.lastpoll) // Calculate the elapsed time
|
|
if dur > 90*time.Second {
|
|
h.RestartDaemon()
|
|
continue
|
|
}
|
|
if h.killcount != 0 {
|
|
log.Info("KILL count =", h.killcount, "FOR", h.pb.Hostname, dur, "curl", url)
|
|
}
|
|
if h.killcount > 10 {
|
|
log.Info("KILL count is greater than 10 for host", h.pb.Hostname, dur, "curl", url)
|
|
}
|
|
// l := shell.FormatDuration(dur)
|
|
// log.Warn("HOST =", h.pb.Hostname, "Last poll =", l)
|
|
//if d.pb.StartState != "ON" {
|
|
// continue
|
|
//}
|
|
// dur := time.Since(d.lastpoll) // Calculate the elapsed time
|
|
}
|
|
return
|
|
}
|
|
|
|
if tmp == "/start" {
|
|
start := r.URL.Query().Get("start")
|
|
// log.Warn("Handling URL:", tmp, "start droplet", start)
|
|
b, result := Start(start)
|
|
log.Warn("Start returned =", b, "result =", result)
|
|
fmt.Fprintln(w, "Start() returned", b)
|
|
fmt.Fprintln(w, "result:", result)
|
|
return
|
|
}
|
|
|
|
log.Warn("BAD URL =", tmp)
|
|
fmt.Fprintln(w, "BAD URL", tmp)
|
|
// badurl(w, r.URL.String())
|
|
// fmt.Fprintln(w, "BAD", tmp)
|
|
}
|
|
|
|
func writeFile(w http.ResponseWriter, filename string) {
|
|
// fmt.Fprintln(w, "GOT TEST?")
|
|
fullname := "resources/" + filename
|
|
pfile, err := resources.ReadFile(fullname)
|
|
if err != nil {
|
|
log.Println("ERROR:", err)
|
|
// w.Write(pfile)
|
|
return
|
|
}
|
|
|
|
var repohtml string
|
|
repohtml = string(pfile)
|
|
if filename == "goReference.svg" {
|
|
w.Header().Set("Content-Type", "image/svg+xml")
|
|
}
|
|
fmt.Fprintln(w, repohtml)
|
|
log.Println("writeFile() found internal file:", filename)
|
|
}
|
|
|
|
// starts and sits waiting for HTTP requests
|
|
func startHTTP() {
|
|
http.HandleFunc("/", okHandler)
|
|
|
|
p := fmt.Sprintf(":%d", argv.Port)
|
|
log.Println("Running on port", p)
|
|
|
|
err := http.ListenAndServe(p, nil)
|
|
if err != nil {
|
|
log.Println("Error starting server:", err)
|
|
}
|
|
}
|