代码拉取完成,页面将自动刷新
package gsysinfo
import (
"fmt"
"gitee.com/micro-tools/wf/util/gconv"
"github.com/shirou/gopsutil/cpu"
"github.com/shirou/gopsutil/disk"
"github.com/shirou/gopsutil/host"
"github.com/shirou/gopsutil/mem"
"github.com/shirou/gopsutil/net"
"runtime"
"time"
)
type Server struct {
Os Os `json:"os"`
Cpu Cpu `json:"cpu"`
Rrm Rrm `json:"ram"`
Disk Disk `json:"disk"`
}
type Os struct {
GOOS string `json:"goos"`
NumCPU int `json:"numCpu"`
Compiler string `json:"compiler"`
GoVersion string `json:"goVersion"`
NumGoroutine int `json:"numGoroutine"`
}
type Cpu struct {
Cpus []float64 `json:"cpus"`
Cores int `json:"cores"`
Percent int `json:"percent"`
}
type Rrm struct {
Used int `json:"used"`
Total int `json:"total"`
UsedPercent int `json:"usedPercent"`
}
type Disk struct {
Used int `json:"used"`
Total int `json:"total"`
UsedPercent int `json:"usedPercent"`
}
type InfoStat struct {
Hostname string `json:"hostname"`
Uptime uint64 `json:"uptime"`
BootTime uint64 `json:"bootTime"`
Procs uint64 `json:"procs"` // number of processes
OS string `json:"os"` // ex: freebsd, linux
Platform string `json:"platform"` // ex: ubuntu, linuxmint
PlatformFamily string `json:"platformFamily"` // ex: debian, rhel
PlatformVersion string `json:"platformVersion"` // version of the complete OS
KernelVersion string `json:"kernelVersion"` // version of the OS kernel (if available)
KernelArch string `json:"kernelArch"` // native cpu architecture queried at runtime, as returned by `uname -m` or empty string in case of error
VirtualizationSystem string `json:"virtualizationSystem"`
VirtualizationRole string `json:"virtualizationRole"` // guest or host
HostID string `json:"hostid"` // ex: uuid
}
func InitHost() (info InfoStat, err error) {
tmp, err := host.Info()
if err == nil {
_ = gconv.Struct(tmp, &info)
}
return
}
//@function: InitCPU
//@description: OS信息
//@return: o Os, err error
func InitOS() (o Os) {
o.GOOS = runtime.GOOS
o.NumCPU = runtime.NumCPU()
o.Compiler = runtime.Compiler
o.GoVersion = runtime.Version()
o.NumGoroutine = runtime.NumGoroutine()
return o
}
//@function: InitCPU
//@description: CPU信息
//@return: c Cpu, err error
func InitCPU(percpu bool) (c Cpu, err error) {
if cores, err := cpu.Counts(false); err != nil {
return c, err
} else {
c.Cores = cores
}
if !percpu {
if percent, err := cpu.Percent(time.Second, false); err != nil {
return c, err
} else {
c.Percent = int(percent[0])
}
} else {
if cpus, err := cpu.Percent(time.Second, true); err != nil {
return c, err
} else {
c.Cpus = cpus
}
}
return c, nil
}
//@function: InitRAM
//@description: ARM信息
//@return: r Rrm, err error
func InitRAM() (r Rrm, err error) {
if u, err := mem.VirtualMemory(); err != nil {
return r, err
} else {
r.Used = int(u.Used)
r.Total = int(u.Total)
r.UsedPercent = int(u.UsedPercent)
}
return r, nil
}
//@function: InitDisk
//@description: 硬盘信息
//@return: d Disk, err error
func InitDisk() (d Disk, err error) {
if u, err := disk.Usage("/"); err != nil {
return d, err
} else {
d.Used = int(u.Used)
d.Total = int(u.Total)
d.UsedPercent = int(u.UsedPercent)
}
return d, nil
}
func NetworkIO(pernic bool) (io []net.IOCountersStat, err error) {
return net.IOCounters(pernic)
}
//FormatSize 字节的单位转换 保留两位小数
func FormatSize(fileSize int64) (size string) {
if fileSize < 1024 {
//return strconv.FormatInt(fileSize, 10) + "B"
return fmt.Sprintf("%.2f B", float64(fileSize)/float64(1))
} else if fileSize < (1024 * 1024) {
return fmt.Sprintf("%.2f KB", float64(fileSize)/float64(1024))
} else if fileSize < (1024 * 1024 * 1024) {
return fmt.Sprintf("%.2f MB", float64(fileSize)/float64(1024*1024))
} else if fileSize < (1024 * 1024 * 1024 * 1024) {
return fmt.Sprintf("%.2f GB", float64(fileSize)/float64(1024*1024*1024))
} else if fileSize < (1024 * 1024 * 1024 * 1024 * 1024) {
return fmt.Sprintf("%.2f TB", float64(fileSize)/float64(1024*1024*1024*1024))
} else { //if fileSize < (1024 * 1024 * 1024 * 1024 * 1024 * 1024)
return fmt.Sprintf("%.2f EB", float64(fileSize)/float64(1024*1024*1024*1024*1024))
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。