From eb744b976a46f79eae6d2975af94bffd75ebeb73 Mon Sep 17 00:00:00 2001 From: Florian Hoss Date: Sun, 23 Oct 2022 19:24:11 +0200 Subject: [PATCH] Add more system information --- static/js/app.js | 6 +++++- system/cpu.go | 5 +++-- system/disk.go | 11 +++++++--- system/ram.go | 13 +++++++++--- system/types.go | 46 ++++++++++++++++++++++++------------------ templates/index.gohtml | 10 +++++---- 6 files changed, 58 insertions(+), 33 deletions(-) diff --git a/static/js/app.js b/static/js/app.js index c165631fed7641a1beb3c126a96d5c46fc350a12..5d4452a661b70f9d6790ea52e398f459398c814c 100644 --- a/static/js/app.js +++ b/static/js/app.js @@ -15,7 +15,9 @@ const weatherSunset = document.getElementById("weatherSunset"); // system elements const systemCpuPercentage = document.getElementById("systemCpuPercentage"); const systemRamPercentage = document.getElementById("systemRamPercentage"); +const systemRamValue = document.getElementById("systemRamValue"); const systemDiskPercentage = document.getElementById("systemDiskPercentage"); +const systemDiskValue = document.getElementById("systemDiskValue"); function connect() { let ws = new WebSocket(apiBase.replace("http", "ws") + "/system/ws"); @@ -47,7 +49,9 @@ function replaceWeather(parsed) { } function replaceSystem(parsed) { - systemCpuPercentage.style = "width:" + parsed.cpu.percentage + "%"; + systemCpuPercentage.style = "width:" + parsed.cpu + "%"; systemRamPercentage.style = "width:" + parsed.ram.percentage + "%"; + systemRamValue.innerText = parsed.ram.value; systemDiskPercentage.style = "width:" + parsed.disk.percentage + "%"; + systemDiskValue.innerText = parsed.disk.value; } diff --git a/system/cpu.go b/system/cpu.go index 3f8b1bb88095f3a239aca15c0ef68dafa71c1c29..63df49fc9a7fd4d426255409b090b437c4b69afe 100644 --- a/system/cpu.go +++ b/system/cpu.go @@ -4,11 +4,12 @@ import ( "github.com/shirou/gopsutil/v3/cpu" "math" "runtime" + "strconv" ) func staticCpu() CPU { var p CPU - p.Threads = runtime.NumCPU() + p.Threads = strconv.Itoa(runtime.NumCPU()) + " threads" p.Architecture = runtime.GOARCH c, err := cpu.Info() if err == nil { @@ -24,5 +25,5 @@ func (s *System) liveCpu() { if err != nil { return } - s.Live.CPU.Percentage = math.RoundToEven(p[0]) + s.Live.CPU = math.RoundToEven(p[0]) } diff --git a/system/disk.go b/system/disk.go index 714e831b76b335e2594c0f6dadb0616fc0eb716a..39057c554709603f540f79e310dade628776b293 100644 --- a/system/disk.go +++ b/system/disk.go @@ -5,14 +5,19 @@ import ( "github.com/dustin/go-humanize" "github.com/shirou/gopsutil/v3/disk" "math" + "strconv" ) -func staticDisk() string { +func staticDisk() Disk { + var result = Disk{} d, err := disk.Usage("/") if err != nil { - return "" + return result } - return humanize.IBytes(d.Total) + p, err := disk.Partitions(false) + result.Total = humanize.IBytes(d.Total) + result.Partitions = strconv.Itoa(len(p)) + " partitions" + return result } func (s *System) liveDisk() { diff --git a/system/ram.go b/system/ram.go index 29e6368707ca25b43fe69e90d94c8965beaac0bc..08bb191bb9106526a3e8b03eee5f8c5e131b0597 100644 --- a/system/ram.go +++ b/system/ram.go @@ -7,12 +7,19 @@ import ( "math" ) -func staticRam() string { +func staticRam() Ram { + var result = Ram{} r, err := mem.VirtualMemory() if err != nil { - return "" + return result } - return humanize.IBytes(r.Total) + result.Total = humanize.IBytes(r.Total) + if r.SwapTotal > 0 { + result.Swap = humanize.IBytes(r.SwapTotal) + " swap" + } else { + result.Swap = "No swap" + } + return result } func (s *System) liveRam() { diff --git a/system/types.go b/system/types.go index 17d1f4f80aefa31566ce3835f5260da16abe3c14..bc5658dcacdddc00ca29dbbddcdf8fd9c73b3d5e 100644 --- a/system/types.go +++ b/system/types.go @@ -4,35 +4,41 @@ type SystemConfig struct { LiveSystem bool `mapstructure:"LIVE_SYSTEM"` } -type BasicSystemInformation struct { - Value string `json:"value" validate:"required"` - Percentage float64 `json:"percentage" validate:"required"` +type LiveStorageInformation struct { + Value string `json:"value"` + Percentage float64 `json:"percentage"` } type LiveInformation struct { - CPU CpuSystemInformation `json:"cpu" validate:"required"` - Ram BasicSystemInformation `json:"ram" validate:"required"` - Disk BasicSystemInformation `json:"disk" validate:"required"` - ServerUptime uint64 `json:"server_uptime" validate:"required"` + CPU float64 `json:"cpu"` + Ram LiveStorageInformation `json:"ram"` + Disk LiveStorageInformation `json:"disk"` + ServerUptime uint64 `json:"server_uptime"` } -type StaticInformation struct { - CPU CPU `json:"cpu" validate:"required"` - Ram string `json:"ram" validate:"required"` - Disk string `json:"disk" validate:"required"` +type CPU struct { + Name string `json:"name"` + Threads string `json:"threads"` + Architecture string `json:"architecture"` } -type System struct { - Live LiveInformation `json:"live" validate:"required"` - Static StaticInformation `json:"static" validate:"required"` +type Ram struct { + Total string `json:"total"` + Swap string `json:"swap"` } -type CPU struct { - Name string `json:"name" validate:"required"` - Threads int `json:"threads" validate:"required"` - Architecture string `json:"architecture" validate:"required"` +type Disk struct { + Total string `json:"total"` + Partitions string `json:"partitions"` } -type CpuSystemInformation struct { - Percentage float64 `json:"percentage" validate:"required"` +type StaticInformation struct { + CPU CPU `json:"cpu"` + Ram Ram `json:"ram"` + Disk Disk `json:"disk"` +} + +type System struct { + Live LiveInformation `json:"live"` + Static StaticInformation `json:"static"` } diff --git a/templates/index.gohtml b/templates/index.gohtml index 626cc0226a65c7832df3198aeba456e4e67949c5..d5c31b06ec112e200740720b9ff244333d4c2a26 100644 --- a/templates/index.gohtml +++ b/templates/index.gohtml @@ -67,12 +67,12 @@
{{ .System.Static.CPU.Name }}
-
{{ .System.Static.CPU.Threads }} Threads
+
{{ .System.Static.CPU.Threads }}
@@ -82,7 +82,8 @@
-
{{ .System.Live.Ram.Value }} / {{ .System.Static.Ram }}
+
{{ .System.Live.Ram.Value }} / {{ .System.Static.Ram.Total }}
+
{{ .System.Static.Ram.Swap }}
-
{{ .System.Live.Disk.Value }} / {{ .System.Static.Disk }}
+
{{ .System.Live.Disk.Value }} / {{ .System.Static.Disk.Total }}
+
{{ .System.Static.Disk.Partitions }}