From ef3a24325cef662af9cb1f307e137e92a810052b Mon Sep 17 00:00:00 2001 From: Henry Dollman Date: Sun, 14 Jul 2024 18:42:36 -0400 Subject: [PATCH] change types and add pending --- go.mod | 2 +- main.go | 49 ++++++++++++++----- migrations/1720568457_collections_snapshot.go | 11 +++-- types.go | 31 +++++++++--- 4 files changed, 66 insertions(+), 27 deletions(-) diff --git a/go.mod b/go.mod index f86c18e..2c746d9 100644 --- a/go.mod +++ b/go.mod @@ -6,6 +6,7 @@ require ( github.com/labstack/echo/v5 v5.0.0-20230722203903-ec5b858dab61 github.com/pocketbase/dbx v1.10.1 github.com/pocketbase/pocketbase v0.22.16 + golang.org/x/crypto v0.24.0 ) require ( @@ -59,7 +60,6 @@ require ( github.com/valyala/fasttemplate v1.2.2 // indirect go.opencensus.io v0.24.0 // indirect gocloud.dev v0.37.0 // indirect - golang.org/x/crypto v0.24.0 // indirect golang.org/x/image v0.18.0 // indirect golang.org/x/net v0.26.0 // indirect golang.org/x/oauth2 v0.21.0 // indirect diff --git a/main.go b/main.go index a2eb004..0645bdb 100644 --- a/main.go +++ b/main.go @@ -139,13 +139,30 @@ func main() { return nil }) + // do things after a systems record is updated + app.OnRecordAfterUpdateRequest("systems").Add(func(e *core.RecordUpdateEvent) error { + status := e.Record.Get("status") + // if server connection exists, close it + if status == "down" || status == "paused" { + deleteServerConnection(e.Record) + } + return nil + }) + + // do things after a systems record is deleted + app.OnRecordAfterDeleteRequest("systems").Add(func(e *core.RecordDeleteEvent) error { + // if server connection exists, close it + deleteServerConnection(e.Record) + return nil + }) + if err := app.Start(); err != nil { log.Fatal(err) } } func serverUpdateTicker() { - ticker := time.NewTicker(60 * time.Second) + ticker := time.NewTicker(10 * time.Second) for range ticker.C { updateServers() } @@ -185,7 +202,7 @@ func updateServer(record *models.Record) { client, err := getServerConnection(&server) if err != nil { app.Logger().Error("Failed to connect:", "err", err.Error(), "server", server.Host, "port", server.Port) - setInactive(record) + updateServerStatus(record, "down") return } server.Client = client @@ -195,12 +212,12 @@ func updateServer(record *models.Record) { systemData, err := requestJson(&server) if err != nil { app.Logger().Error("Failed to get server stats: ", "err", err.Error()) - setInactive(record) + updateServerStatus(record, "down") return } // update system record record.Set("status", "up") - record.Set("stats", systemData.System) + record.Set("info", systemData.Info) if err := app.Dao().SaveRecord(record); err != nil { app.Logger().Error("Failed to update record: ", "err", err.Error()) } @@ -208,7 +225,7 @@ func updateServer(record *models.Record) { system_stats, _ := app.Dao().FindCollectionByNameOrId("system_stats") system_stats_record := models.NewRecord(system_stats) system_stats_record.Set("system", record.Id) - system_stats_record.Set("stats", systemData.System) + system_stats_record.Set("stats", systemData.Stats) if err := app.Dao().SaveRecord(system_stats_record); err != nil { app.Logger().Error("Failed to save record: ", "err", err.Error()) } @@ -225,21 +242,27 @@ func updateServer(record *models.Record) { } // set server to status down and close connection -func setInactive(record *models.Record) { +func updateServerStatus(record *models.Record, status string) { // if in map, close connection and remove from map + // this is now down automatically in an after update hook + // if status == "down" || status == "paused" { + // deleteServerConnection(record) + // } + if record.Get("status") != status { + record.Set("status", status) + if err := app.Dao().SaveRecord(record); err != nil { + app.Logger().Error("Failed to update record: ", "err", err.Error()) + } + } +} + +func deleteServerConnection(record *models.Record) { if _, ok := serverConnections[record.Id]; ok { if serverConnections[record.Id].Client != nil { serverConnections[record.Id].Client.Close() } delete(serverConnections, record.Id) } - // set inactive - if record.Get("status") != "down" { - record.Set("status", "down") - if err := app.Dao().SaveRecord(record); err != nil { - app.Logger().Error("Failed to update record: ", "err", err.Error()) - } - } } func getServerConnection(server *Server) (*ssh.Client, error) { diff --git a/migrations/1720568457_collections_snapshot.go b/migrations/1720568457_collections_snapshot.go index ecb2433..3e8dbf6 100644 --- a/migrations/1720568457_collections_snapshot.go +++ b/migrations/1720568457_collections_snapshot.go @@ -15,7 +15,7 @@ func init() { { "id": "2hz5ncl8tizk5nx", "created": "2024-07-07 16:08:20.979Z", - "updated": "2024-07-14 03:36:23.090Z", + "updated": "2024-07-14 19:51:52.377Z", "name": "systems", "type": "base", "system": false, @@ -47,7 +47,8 @@ func init() { "values": [ "up", "down", - "paused" + "paused", + "pending" ] } }, @@ -82,7 +83,7 @@ func init() { { "system": false, "id": "qoq64ntl", - "name": "stats", + "name": "info", "type": "json", "required": true, "presentable": false, @@ -194,8 +195,8 @@ func init() { }, { "id": "_pb_users_auth_", - "created": "2024-07-14 03:36:23.076Z", - "updated": "2024-07-14 03:36:23.087Z", + "created": "2024-07-14 16:25:18.226Z", + "updated": "2024-07-14 16:25:18.235Z", "name": "users", "type": "auth", "system": false, diff --git a/types.go b/types.go index a8d8954..66e7e38 100644 --- a/types.go +++ b/types.go @@ -7,25 +7,40 @@ import ( type Server struct { Host string Port string + Status string Client *ssh.Client } type SystemData struct { - System SystemStats `json:"stats"` + Stats SystemStats `json:"stats"` + Info SystemInfo `json:"info"` Containers []ContainerStats `json:"container"` } -type SystemStats struct { - Cpu float64 `json:"c"` - Mem float64 `json:"m"` - MemUsed float64 `json:"mu"` +type SystemInfo struct { + Cores int `json:"c"` + Threads int `json:"t"` + CpuModel string `json:"m"` + Os string `json:"o"` + Uptime uint64 `json:"u"` + Cpu float64 `json:"cpu"` MemPct float64 `json:"mp"` - MemBuf float64 `json:"mb"` - Disk float64 `json:"d"` - DiskUsed float64 `json:"du"` DiskPct float64 `json:"dp"` } +type SystemStats struct { + Cpu float64 `json:"cpu"` + Mem float64 `json:"m"` + MemUsed float64 `json:"mu"` + MemPct float64 `json:"mp"` + MemBuffCache float64 `json:"mb"` + Disk float64 `json:"d"` + DiskUsed float64 `json:"du"` + DiskPct float64 `json:"dp"` + DiskRead float64 `json:"dr"` + DiskWrite float64 `json:"dw"` +} + type ContainerStats struct { Name string `json:"n"` Cpu float64 `json:"c"`