Bladeren bron

Add more system information

Florian Hoss 2 jaren geleden
bovenliggende
commit
eb744b976a
6 gewijzigde bestanden met toevoegingen van 58 en 33 verwijderingen
  1. 5 1
      static/js/app.js
  2. 3 2
      system/cpu.go
  3. 8 3
      system/disk.go
  4. 10 3
      system/ram.go
  5. 26 20
      system/types.go
  6. 6 4
      templates/index.gohtml

+ 5 - 1
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;
 }

+ 3 - 2
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])
 }

+ 8 - 3
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() {

+ 10 - 3
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() {

+ 26 - 20
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"`
 }

+ 6 - 4
templates/index.gohtml

@@ -67,12 +67,12 @@
         </svg>
         <div class="w-full truncate">
           <div class="truncate">{{ .System.Static.CPU.Name }}</div>
-          <div class="text-slate-700 dark:text-slate-300 truncate">{{ .System.Static.CPU.Threads }} Threads</div>
+          <div class="text-xs text-slate-700 dark:text-slate-300 truncate">{{ .System.Static.CPU.Threads }}</div>
           <div class="bg-slate-300 dark:bg-slate-700 h-px mt-1">
             <div
               id="systemCpuPercentage"
               class="transition-[width] duration-700 bg-slate-800 dark:bg-slate-200 h-px"
-              style="width: {{ .System.Live.CPU.Percentage }}%"
+              style="width: {{ .System.Live.CPU }}%"
             ></div>
           </div>
         </div>
@@ -82,7 +82,8 @@
           <use xlink:href="#ram"></use>
         </svg>
         <div class="w-full truncate">
-          <div class="truncate">{{ .System.Live.Ram.Value }} / {{ .System.Static.Ram }}</div>
+          <div class="truncate"><span id="systemRamValue">{{ .System.Live.Ram.Value }}</span> / {{ .System.Static.Ram.Total }}</div>
+          <div class="text-xs text-slate-700 dark:text-slate-300 truncate">{{ .System.Static.Ram.Swap }}</div>
           <div class="bg-slate-300 dark:bg-slate-700 h-px mt-1">
             <div
               id="systemRamPercentage"
@@ -97,7 +98,8 @@
           <use xlink:href="#disk"></use>
         </svg>
         <div class="w-full truncate">
-          <div class="truncate">{{ .System.Live.Disk.Value }} / {{ .System.Static.Disk }}</div>
+          <div class="truncate"><span id="systemDiskValue">{{ .System.Live.Disk.Value }}</span> / {{ .System.Static.Disk.Total }}</div>
+          <div class="text-xs text-slate-700 dark:text-slate-300 truncate">{{ .System.Static.Disk.Partitions }}</div>
           <div class="bg-slate-300 dark:bg-slate-700 h-px mt-1">
             <div
               id="systemDiskPercentage"