1 Star 0 Fork 0

micro-tools/wf

加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
gsysinfo.go 4.50 KB
一键复制 编辑 原始数据 按行查看 历史
545403892 提交于 2023-09-27 22:16 +08:00 . 升级go-ole
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))
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/micro-tools/wf.git
git@gitee.com:micro-tools/wf.git
micro-tools
wf
wf
v1.0.2

搜索帮助