59 lines
1.3 KiB
Go
59 lines
1.3 KiB
Go
package controllers
|
|
|
|
import (
|
|
"github.com/shirou/gopsutil/v3/cpu"
|
|
"github.com/shirou/gopsutil/v3/disk"
|
|
"github.com/shirou/gopsutil/v3/host"
|
|
"github.com/shirou/gopsutil/v3/mem"
|
|
|
|
"github.com/gofiber/fiber/v2"
|
|
)
|
|
|
|
func GetServerInfo(c *fiber.Ctx) error {
|
|
|
|
var diskPercent float64 = 0.0
|
|
var memoryPercent float64 = 0.0
|
|
var cpuPercent float64 = 0.0
|
|
|
|
// Get info from the host
|
|
hostStat, _ := host.Info()
|
|
|
|
// Get info from the host disks
|
|
if hostStat.OS == "windows" {
|
|
var total, free uint64 = 0.0, 0.0
|
|
partitions, _ := disk.Partitions(false)
|
|
for _, partition := range partitions {
|
|
v3, _ := disk.Usage(partition.Device)
|
|
total = total + v3.Total
|
|
free = free + v3.Free
|
|
}
|
|
diskPercent = (float64(total-free) / float64(total)) * 100
|
|
} else {
|
|
diskstat, _ := disk.Usage("/")
|
|
diskPercent = diskstat.UsedPercent
|
|
}
|
|
|
|
// Get info from the host memory
|
|
v, _ := mem.VirtualMemory()
|
|
memoryPercent = v.UsedPercent
|
|
|
|
// Get info from the host CPUs
|
|
cpus := 0
|
|
v4, _ := cpu.Percent(0, true)
|
|
for _, cpu := range v4 {
|
|
cpus++
|
|
cpuPercent = cpuPercent + cpu
|
|
}
|
|
cpuPercent = cpuPercent / float64(cpus)
|
|
|
|
c.Set("Content-Type", "application/json; charset=utf-8")
|
|
|
|
type result struct {
|
|
Memory float64 `json:"memory"`
|
|
CPU float64 `json:"cpu"`
|
|
Disk float64 `json:"disk"`
|
|
}
|
|
|
|
return c.JSON(result{Memory: memoryPercent, CPU: cpuPercent, Disk: diskPercent})
|
|
}
|