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 | 52 +++++++++++++++++++++++------------------- templates/index.gohtml | 10 ++++---- 6 files changed, 61 insertions(+), 36 deletions(-) diff --git a/static/js/app.js b/static/js/app.js index c165631..5d4452a 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 3f8b1bb..63df49f 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 714e831..39057c5 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 29e6368..08bb191 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 17d1f4f..bc5658d 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"` -} - -type StaticInformation struct { - CPU CPU `json:"cpu" validate:"required"` - Ram string `json:"ram" validate:"required"` - Disk string `json:"disk" validate:"required"` -} - -type System struct { - Live LiveInformation `json:"live" validate:"required"` - Static StaticInformation `json:"static" validate:"required"` + CPU float64 `json:"cpu"` + Ram LiveStorageInformation `json:"ram"` + Disk LiveStorageInformation `json:"disk"` + ServerUptime uint64 `json:"server_uptime"` } type CPU struct { - Name string `json:"name" validate:"required"` - Threads int `json:"threads" validate:"required"` - Architecture string `json:"architecture" validate:"required"` + Name string `json:"name"` + Threads string `json:"threads"` + Architecture string `json:"architecture"` } -type CpuSystemInformation struct { - Percentage float64 `json:"percentage" validate:"required"` +type Ram struct { + Total string `json:"total"` + Swap string `json:"swap"` +} + +type Disk struct { + Total string `json:"total"` + Partitions string `json:"partitions"` +} + +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 626cc02..d5c31b0 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 }}