Ai
1 Star 0 Fork 0

yonglinux/rpcx-sun-examples

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
hostinfo.go 1.14 KB
一键复制 编辑 原始数据 按行查看 历史
yonglinux 提交于 2022-02-18 15:47 +08:00 . add hostinfo.go.
package example
import (
"github.com/shirou/gopsutil/v3/load"
"github.com/shirou/gopsutil/v3/mem"
"net"
"strconv"
"strings"
)
func GetMemInfo() (free float64) {
memInfo, err := mem.VirtualMemory()
if err != nil {
panic(err)
}
free = memInfo.UsedPercent
return free
}
func GetCpuLoad() (lod int) {
lo, err := load.Avg()
if err != nil {
panic(err)
}
lod = int(lo.Load1)
return lod
}
func GetHostIp() (ips string) {
addrSlice, _ := net.InterfaceAddrs()
for _, addr := range addrSlice {
if ipnet, ok := addr.(*net.IPNet); ok && !ipnet.IP.IsLoopback() {
if nil != ipnet.IP.To4() {
ip := ipnet.IP.String()
if !strings.HasPrefix(ip, "169") {
ips = ip
}
}
}
}
return ips
}
// func GetHostAll() (info map[string]int) {
// info = map[string]int{
// // "hostip": GetHostIp(),
// "load": GetCpuLoad(),
// "free": int(GetMemInfo()),
// }
// return info
// }
func GetHostAll() (info map[string]string) {
info = map[string]string{
"ip": GetHostIp(),
"load": strconv.Itoa(GetCpuLoad()),
"free": strconv.Itoa(int(GetMemInfo())),
}
return info
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/yonglinux/rpcx-sun-examples.git
git@gitee.com:yonglinux/rpcx-sun-examples.git
yonglinux
rpcx-sun-examples
rpcx-sun-examples
v1.1.8

搜索帮助