mirror of
https://github.com/fankes/beszel.git
synced 2025-10-19 01:39:34 +08:00
change types and add pending
This commit is contained in:
2
go.mod
2
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
|
||||
|
49
main.go
49
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) {
|
||||
|
@@ -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,
|
||||
|
31
types.go
31
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"`
|
||||
|
Reference in New Issue
Block a user