Add system usage monitoring to admin dashboard
This commit is contained in:
@ -1,13 +1,30 @@
|
||||
package admin
|
||||
|
||||
import (
|
||||
"github.com/fossyy/filekeeper/app"
|
||||
"encoding/json"
|
||||
adminIndex "github.com/fossyy/filekeeper/view/admin/index"
|
||||
"github.com/gorilla/websocket"
|
||||
"github.com/shirou/gopsutil/v3/cpu"
|
||||
"github.com/shirou/gopsutil/v3/mem"
|
||||
"github.com/shirou/gopsutil/v3/net"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"time"
|
||||
)
|
||||
|
||||
var upgrader = websocket.Upgrader{
|
||||
ReadBufferSize: 1024,
|
||||
WriteBufferSize: 1024,
|
||||
}
|
||||
|
||||
type SystemStats struct {
|
||||
TotalMemoryGB float64 `json:"total_memory_gb"`
|
||||
MemoryUsedGB float64 `json:"memory_used_gb"`
|
||||
CpuUsagePercent float64 `json:"cpu_usage_percent"`
|
||||
UploadSpeedMbps float64 `json:"upload_speed_mbps"`
|
||||
DownloadSpeedMbps float64 `json:"download_speed_mbps"`
|
||||
}
|
||||
|
||||
func SetupRoutes() *http.ServeMux {
|
||||
handler := http.NewServeMux()
|
||||
handler.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
||||
@ -24,21 +41,59 @@ func SetupRoutes() *http.ServeMux {
|
||||
adminIndex.Main().Render(r.Context(), w)
|
||||
return
|
||||
})
|
||||
handler.HandleFunc("/public/output.css", func(w http.ResponseWriter, r *http.Request) {
|
||||
openFile, err := os.OpenFile(filepath.Join("public", "output.css"), os.O_RDONLY, 0)
|
||||
|
||||
handler.HandleFunc("/ws", func(w http.ResponseWriter, r *http.Request) {
|
||||
conn, err := upgrader.Upgrade(w, r, nil)
|
||||
if err != nil {
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
app.Server.Logger.Error(err.Error())
|
||||
log.Println(err)
|
||||
return
|
||||
}
|
||||
defer openFile.Close()
|
||||
stat, err := openFile.Stat()
|
||||
if err != nil {
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
app.Server.Logger.Error(err.Error())
|
||||
return
|
||||
for {
|
||||
handlerWS(conn)
|
||||
}
|
||||
http.ServeContent(w, r, openFile.Name(), stat.ModTime(), openFile)
|
||||
})
|
||||
|
||||
fileServer := http.FileServer(http.Dir("./public"))
|
||||
handler.Handle("/public/", http.StripPrefix("/public", fileServer))
|
||||
return handler
|
||||
}
|
||||
|
||||
func handlerWS(conn *websocket.Conn) {
|
||||
prevCounters, _ := net.IOCounters(false)
|
||||
for {
|
||||
vMem, _ := mem.VirtualMemory()
|
||||
|
||||
totalMemoryGB := float64(vMem.Total) / (1024 * 1024 * 1024)
|
||||
memoryUsedGB := float64(vMem.Used) / (1024 * 1024 * 1024)
|
||||
|
||||
cpuPercent, _ := cpu.Percent(time.Second, false)
|
||||
|
||||
currentCounters, _ := net.IOCounters(false)
|
||||
|
||||
uploadBytes := currentCounters[0].BytesSent - prevCounters[0].BytesSent
|
||||
downloadBytes := currentCounters[0].BytesRecv - prevCounters[0].BytesRecv
|
||||
uploadSpeedMbps := float64(uploadBytes) * 8 / (1024 * 1024) / 2
|
||||
downloadSpeedMbps := float64(downloadBytes) * 8 / (1024 * 1024) / 2
|
||||
|
||||
prevCounters = currentCounters
|
||||
|
||||
stats := SystemStats{
|
||||
TotalMemoryGB: totalMemoryGB,
|
||||
MemoryUsedGB: memoryUsedGB,
|
||||
CpuUsagePercent: cpuPercent[0],
|
||||
UploadSpeedMbps: uploadSpeedMbps,
|
||||
DownloadSpeedMbps: downloadSpeedMbps,
|
||||
}
|
||||
|
||||
statsJson, _ := json.Marshal(stats)
|
||||
|
||||
err := conn.WriteMessage(websocket.TextMessage, statsJson)
|
||||
if err != nil {
|
||||
conn.Close()
|
||||
return
|
||||
}
|
||||
|
||||
time.Sleep(2 * time.Second)
|
||||
}
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user