feta/process/handlers.go

111 lines
2.6 KiB
Go
Raw Normal View History

2019-12-19 14:24:26 +00:00
package process
2019-11-05 23:32:09 +00:00
2020-03-27 23:02:36 +00:00
import (
"fmt"
"net/http"
"runtime"
"strings"
"time"
2019-11-05 23:32:09 +00:00
u "git.eeqj.de/sneak/goutil"
"github.com/flosch/pongo2"
2020-03-27 23:02:36 +00:00
"github.com/gin-gonic/gin"
"github.com/labstack/echo"
2020-03-27 23:02:36 +00:00
)
2019-11-05 23:32:09 +00:00
type hash map[string]interface{}
2019-12-19 14:24:26 +00:00
func (a *Server) instances() []hash {
2019-11-05 23:32:09 +00:00
resp := make([]hash, 0)
now := time.Now()
2019-12-19 14:24:26 +00:00
for _, v := range a.feta.manager.ListInstances() {
2019-11-05 23:32:09 +00:00
i := make(hash)
// FIXME figure out why a very short lock here deadlocks
v.Lock()
2019-12-19 14:24:26 +00:00
i["hostname"] = v.Hostname
i["nextCheck"] = v.NextFetch.UTC().Format(time.RFC3339)
i["nextCheckAfter"] = (-1 * now.Sub(v.NextFetch)).String()
i["successCount"] = v.SuccessCount
i["errorCount"] = v.ErrorCount
i["identified"] = v.Identified
i["status"] = v.Status() //FIXME maybe this is why
2019-11-05 23:32:09 +00:00
i["software"] = "unknown"
i["version"] = "unknown"
2019-12-19 14:24:26 +00:00
if v.Identified {
i["software"] = v.ServerImplementationString
i["version"] = v.ServerVersionString
2019-11-05 23:32:09 +00:00
}
v.Unlock()
resp = append(resp, i)
}
return resp
}
2019-12-19 14:24:26 +00:00
func (a *Server) instanceSummary() map[string]int {
2019-11-06 07:03:42 +00:00
resp := make(map[string]int)
2019-12-19 14:24:26 +00:00
for _, v := range a.feta.manager.ListInstances() {
2019-11-06 07:03:42 +00:00
v.Lock()
resp[fmt.Sprintf("STATUS_%s", v.Status())]++
2019-12-19 14:24:26 +00:00
if v.ServerImplementationString != "" {
impl := strings.ToUpper(u.FilterToAlnum(v.ServerImplementationString))
resp[fmt.Sprintf("SOFTWARE_%s", impl)]++
2019-11-06 07:03:42 +00:00
}
v.Unlock()
}
return resp
}
/*
2019-12-19 14:24:26 +00:00
func (a *Server) getInstanceListHandler() http.HandlerFunc {
2019-11-06 07:03:42 +00:00
return func(w http.ResponseWriter, r *http.Request) {
result := &gin.H{
"instances": a.instances(),
}
json, err := json.Marshal(result)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
w.Write(json)
}
}
*/
2019-11-06 07:03:42 +00:00
func (a *Server) indexHandler(c echo.Context) error {
tc := pongo2.Context{
"time": time.Now().UTC().Format(time.RFC3339Nano),
"gitrev": a.feta.version,
2019-11-05 23:32:09 +00:00
}
return c.Render(http.StatusOK, "index.html", tc)
2019-11-05 23:32:09 +00:00
}
func (a *Server) statsHandler(c echo.Context) error {
index := &gin.H{
"server": &gin.H{
"now": time.Now().UTC().Format(time.RFC3339),
"uptime": a.feta.uptime().String(),
"goroutines": runtime.NumGoroutine(),
"goversion": runtime.Version(),
"version": a.feta.version,
"buildarch": a.feta.buildarch,
},
"instanceSummary": a.instanceSummary(),
}
2019-11-05 23:32:09 +00:00
return c.JSONPretty(http.StatusOK, index, " ")
}
func (a *Server) healthCheckHandler(c echo.Context) error {
resp := &gin.H{
"status": "ok",
"now": time.Now().UTC().Format(time.RFC3339),
"uptime": a.feta.uptime().String(),
2019-11-05 23:32:09 +00:00
}
return c.JSONPretty(http.StatusOK, resp, " ")
2019-11-05 23:32:09 +00:00
}