2024-10-12 00:17:26 -05:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"strings"
|
2024-10-12 11:54:01 -05:00
|
|
|
"time"
|
2024-10-12 00:17:26 -05:00
|
|
|
|
2024-10-12 11:54:01 -05:00
|
|
|
"go.wit.com/lib/gui/shell"
|
2024-10-12 12:45:43 -05:00
|
|
|
"go.wit.com/log"
|
2024-10-12 00:17:26 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
// 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)
|
|
|
|
|
2024-10-12 12:45:43 -05:00
|
|
|
// is the cluster running what it should?
|
2024-10-13 03:04:46 -05:00
|
|
|
if tmp == "/droplets" {
|
2024-10-12 12:45:43 -05:00
|
|
|
for _, d := range me.droplets {
|
2024-10-22 17:59:27 -05:00
|
|
|
if d.pb.StartState != "ON" {
|
2024-10-12 12:45:43 -05:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
dur := time.Since(d.lastpoll) // Calculate the elapsed time
|
2024-10-22 18:19:21 -05:00
|
|
|
var hname string
|
|
|
|
if d.h == nil {
|
|
|
|
hname = ""
|
|
|
|
} else {
|
|
|
|
hname = d.h.pb.Hostname
|
|
|
|
}
|
2024-10-12 12:45:43 -05:00
|
|
|
if d.CurrentState != "ON" {
|
2024-10-22 18:19:21 -05:00
|
|
|
fmt.Fprintln(w, "BAD STATE ", d.pb.Hostname, hname, "(", d.pb.StartState, "vs", d.CurrentState, ")", shell.FormatDuration(dur))
|
2024-10-12 12:45:43 -05:00
|
|
|
} else {
|
|
|
|
dur := time.Since(d.lastpoll) // Calculate the elapsed time
|
2024-10-22 18:19:21 -05:00
|
|
|
fmt.Fprintln(w, "GOOD STATE ON", d.pb.Hostname, hname, shell.FormatDuration(dur))
|
2024-10-12 12:45:43 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-10-12 00:17:26 -05:00
|
|
|
if tmp == "/favicon.ico" {
|
2024-10-22 19:51:01 -05:00
|
|
|
// w.Header().Set("Content-Type", "image/svg+xml")
|
|
|
|
w.Header().Set("Content-Type", "image/png")
|
2024-10-12 00:17:26 -05:00
|
|
|
writeFile(w, "ipv6.png")
|
|
|
|
return
|
|
|
|
}
|
2024-10-22 19:51:01 -05:00
|
|
|
|
|
|
|
if tmp == "/goReference.svg" {
|
|
|
|
w.Header().Set("Content-Type", "image/svg+xml")
|
|
|
|
writeFile(w, "goReference.svg")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if tmp == "/writeconfig" {
|
|
|
|
writeConfigFile()
|
2024-10-23 00:48:35 -05:00
|
|
|
writeConfigFileDroplets()
|
2024-10-22 19:55:32 -05:00
|
|
|
fmt.Fprintln(w, "OK")
|
2024-10-22 19:51:01 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-10-12 00:17:26 -05:00
|
|
|
if tmp == "/uptime" {
|
2024-10-13 01:13:19 -05:00
|
|
|
b, s := clusterHealthy()
|
|
|
|
if b {
|
|
|
|
log.Info("Handling URL:", tmp, "cluster is ok", s)
|
|
|
|
fmt.Fprintln(w, s)
|
2024-10-12 13:01:31 -05:00
|
|
|
} else {
|
2024-10-13 01:13:19 -05:00
|
|
|
log.Info("Handling URL:", tmp, "cluster is not right yet", s)
|
|
|
|
fmt.Fprintln(w, s)
|
2024-10-12 13:01:31 -05:00
|
|
|
}
|
2024-10-13 00:40:22 -05:00
|
|
|
for _, h := range me.hypers {
|
2024-10-22 17:27:24 -05:00
|
|
|
url := "http://" + h.pb.Hostname + ":2520/kill"
|
2024-10-13 00:40:22 -05:00
|
|
|
dur := time.Since(h.lastpoll) // Calculate the elapsed time
|
2024-10-13 01:13:19 -05:00
|
|
|
if dur > 90*time.Second {
|
2024-10-13 02:23:30 -05:00
|
|
|
h.RestartDaemon()
|
|
|
|
continue
|
2024-10-13 00:57:29 -05:00
|
|
|
}
|
|
|
|
if h.killcount != 0 {
|
2024-10-22 17:27:24 -05:00
|
|
|
log.Info("KILL count =", h.killcount, "FOR", h.pb.Hostname, dur, "curl", url)
|
2024-10-13 00:57:29 -05:00
|
|
|
}
|
|
|
|
if h.killcount > 10 {
|
2024-10-22 17:27:24 -05:00
|
|
|
log.Info("KILL count is greater than 10 for host", h.pb.Hostname, dur, "curl", url)
|
2024-10-13 00:40:22 -05:00
|
|
|
}
|
|
|
|
// l := shell.FormatDuration(dur)
|
2024-10-22 17:27:24 -05:00
|
|
|
// log.Warn("HOST =", h.pb.Hostname, "Last poll =", l)
|
2024-10-22 17:59:27 -05:00
|
|
|
//if d.pb.StartState != "ON" {
|
2024-10-13 00:40:22 -05:00
|
|
|
// continue
|
|
|
|
//}
|
|
|
|
// dur := time.Since(d.lastpoll) // Calculate the elapsed time
|
|
|
|
}
|
2024-10-12 00:17:26 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-10-13 03:49:54 -05:00
|
|
|
if tmp == "/start" {
|
|
|
|
start := r.URL.Query().Get("start")
|
2024-10-16 20:43:01 -05:00
|
|
|
// 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)
|
2024-10-13 03:49:54 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-10-12 00:17:26 -05:00
|
|
|
log.Warn("BAD URL =", tmp)
|
2024-10-16 21:47:38 -05:00
|
|
|
fmt.Fprintln(w, "BAD URL", tmp)
|
2024-10-12 00:17:26 -05:00
|
|
|
// 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)
|
|
|
|
}
|
|
|
|
}
|