This commit is contained in:
Henry Dollman
2024-07-09 22:17:50 -04:00
parent 7af8671134
commit 03755ad85f

43
main.go
View File

@@ -8,6 +8,7 @@ import (
"fmt" "fmt"
"log" "log"
_ "monitor-site/migrations" _ "monitor-site/migrations"
"net/http"
"net/http/httputil" "net/http/httputil"
"net/url" "net/url"
"os" "os"
@@ -93,8 +94,31 @@ func main() {
return nil return nil
}) })
// create ssh key if it doesn't exist
app.OnBeforeServe().Add(func(e *core.ServeEvent) error { app.OnBeforeServe().Add(func(e *core.ServeEvent) error {
go serverUpdateTicker() e.Router.GET("/getkey", func(c echo.Context) error {
requestData := apis.RequestInfo(c)
if requestData.Admin == nil {
return apis.NewForbiddenError("Forbidden", nil)
}
key, err := os.ReadFile("./pb_data/id_ed25519.pub")
if err != nil {
return err
}
return c.JSON(http.StatusOK, map[string]string{"key": strings.TrimSuffix(string(key), "\n")})
})
return nil
})
// start ticker for server updates
app.OnBeforeServe().Add(func(e *core.ServeEvent) error {
// go serverUpdateTicker()
return nil
})
// immediately create connection for new servers
app.OnRecordAfterCreateRequest("systems").Add(func(e *core.RecordCreateEvent) error {
go updateServer(e.Record)
return nil return nil
}) })
@@ -104,7 +128,7 @@ func main() {
} }
func serverUpdateTicker() { func serverUpdateTicker() {
ticker := time.NewTicker(5 * time.Second) ticker := time.NewTicker(15 * time.Second)
for range ticker.C { for range ticker.C {
updateServers() updateServers()
} }
@@ -114,10 +138,9 @@ func updateServers() {
// serverCount := len(serverConnections) // serverCount := len(serverConnections)
// fmt.Println("server count: ", serverCount) // fmt.Println("server count: ", serverCount)
query := app.Dao().RecordQuery("systems"). query := app.Dao().RecordQuery("systems").
// todo check that asc is correct
OrderBy("updated ASC"). OrderBy("updated ASC").
// todo get total count of servers and divide by 4 or something // todo get total count of servers and divide by 4 or something
Limit(1) Limit(5)
records := []*models.Record{} records := []*models.Record{}
if err := query.All(&records); err != nil { if err := query.All(&records); err != nil {
@@ -126,6 +149,11 @@ func updateServers() {
} }
for _, record := range records { for _, record := range records {
updateServer(record)
}
}
func updateServer(record *models.Record) {
var server Server var server Server
// check if server connection data exists // check if server connection data exists
if _, ok := serverConnections[record.Id]; ok { if _, ok := serverConnections[record.Id]; ok {
@@ -142,12 +170,12 @@ func updateServers() {
// todo update record to not connected // todo update record to not connected
record.Set("active", false) record.Set("active", false)
delete(serverConnections, record.Id) delete(serverConnections, record.Id)
continue return
} }
server.Client = client server.Client = client
serverConnections[record.Id] = server serverConnections[record.Id] = server
} }
// get server stats // get server stats from agent
systemData, err := requestJson(&server) systemData, err := requestJson(&server)
if err != nil { if err != nil {
app.Logger().Error("Failed to get server stats: ", "err", err) app.Logger().Error("Failed to get server stats: ", "err", err)
@@ -156,7 +184,7 @@ func updateServers() {
server.Client.Close() server.Client.Close()
} }
delete(serverConnections, record.Id) delete(serverConnections, record.Id)
continue return
} }
// update system record // update system record
record.Set("active", true) record.Set("active", true)
@@ -183,7 +211,6 @@ func updateServers() {
} }
} }
} }
}
func getServerConnection(server *Server) (*ssh.Client, error) { func getServerConnection(server *Server) (*ssh.Client, error) {
// app.Logger().Debug("new ssh connection", "server", server.Ip) // app.Logger().Debug("new ssh connection", "server", server.Ip)