feat(Go-StudyExample):2020/12/15: 新增通过gopsutil包读取服务器信息的方法
This commit is contained in:
@@ -0,0 +1,78 @@
|
|||||||
|
/*
|
||||||
|
* @Author : huangzj
|
||||||
|
* @Time : 2020/12/15 11:28
|
||||||
|
* @Description:
|
||||||
|
*/
|
||||||
|
|
||||||
|
package cupMessage
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"runtime"
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/shirou/gopsutil/cpu"
|
||||||
|
"github.com/shirou/gopsutil/disk"
|
||||||
|
"github.com/shirou/gopsutil/load"
|
||||||
|
"github.com/shirou/gopsutil/mem"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestGetCpuMessage(t *testing.T) {
|
||||||
|
DiskCheck()
|
||||||
|
OSCheck()
|
||||||
|
CPUCheck()
|
||||||
|
RAMCheck()
|
||||||
|
}
|
||||||
|
|
||||||
|
const (
|
||||||
|
B = 1
|
||||||
|
KB = 1024 * B
|
||||||
|
MB = 1024 * KB
|
||||||
|
GB = 1024 * MB
|
||||||
|
)
|
||||||
|
|
||||||
|
//服务器硬盘使用量
|
||||||
|
func DiskCheck() {
|
||||||
|
u, _ := disk.Usage("/")
|
||||||
|
usedMB := int(u.Used) / MB
|
||||||
|
usedGB := int(u.Used) / GB
|
||||||
|
totalMB := int(u.Total) / MB
|
||||||
|
totalGB := int(u.Total) / GB
|
||||||
|
usedPercent := int(u.UsedPercent)
|
||||||
|
fmt.Printf("Free space: %dMB (%dGB) / %dMB (%dGB) | Used: %d%%\n", usedMB, usedGB, totalMB, totalGB, usedPercent)
|
||||||
|
}
|
||||||
|
|
||||||
|
//OS
|
||||||
|
func OSCheck() {
|
||||||
|
fmt.Printf("goOs:%s,compiler:%s,numCpu:%d,version:%s,numGoroutine:%d\n", runtime.GOOS, runtime.Compiler, runtime.NumCPU(), runtime.Version(), runtime.NumGoroutine())
|
||||||
|
}
|
||||||
|
|
||||||
|
//CPU 使用量
|
||||||
|
func CPUCheck() {
|
||||||
|
cores, _ := cpu.Counts(false)
|
||||||
|
|
||||||
|
cpus, err := cpu.Percent(time.Duration(200)*time.Millisecond, true)
|
||||||
|
if err == nil {
|
||||||
|
for i, c := range cpus {
|
||||||
|
fmt.Printf("cpu%d : %f%%\n", i, c)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
a, _ := load.Avg()
|
||||||
|
l1 := a.Load1
|
||||||
|
l5 := a.Load5
|
||||||
|
l15 := a.Load15
|
||||||
|
fmt.Println(l1)
|
||||||
|
fmt.Println(l5)
|
||||||
|
fmt.Println(l15)
|
||||||
|
fmt.Println(cores)
|
||||||
|
}
|
||||||
|
|
||||||
|
//内存使用量
|
||||||
|
func RAMCheck() {
|
||||||
|
u, _ := mem.VirtualMemory()
|
||||||
|
usedMB := int(u.Used) / MB
|
||||||
|
totalMB := int(u.Total) / MB
|
||||||
|
usedPercent := int(u.UsedPercent)
|
||||||
|
fmt.Printf("usedMB:%d,totalMB:%d,usedPercent:%d", usedMB, totalMB, usedPercent)
|
||||||
|
}
|
||||||
Binary file not shown.
|
After Width: | Height: | Size: 73 KiB |
@@ -0,0 +1,8 @@
|
|||||||
|
这边使用到的是gopsUtil:[gopsutil](https://github.com/shirou/gopsutil)
|
||||||
|
|
||||||
|
获取对应的github代码命令:go get -u https://github.com/shirou/gopsutil
|
||||||
|
|
||||||
|
在获取包的时候会发生Sum.goland.org校验但是连接不上的问题,解决方案可以参考:[Go工程下载包 Sum.goland.org无法连通问题](https://blog.csdn.net/qq_34326321/article/details/111207006)
|
||||||
|
|
||||||
|
|
||||||
|

|
||||||
@@ -1,19 +0,0 @@
|
|||||||
module Go-StudyExample
|
|
||||||
|
|
||||||
go 1.13
|
|
||||||
|
|
||||||
require (
|
|
||||||
github.com/Bowery/prompt v0.0.0-20190916142128-fa8279994f75 // indirect
|
|
||||||
github.com/boombuler/barcode v1.0.0
|
|
||||||
github.com/dchest/safefile v0.0.0-20151022103144-855e8d98f185 // indirect
|
|
||||||
github.com/goinggo/mapstructure v0.0.0-20140717182941-194205d9b4a9
|
|
||||||
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0
|
|
||||||
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 // indirect
|
|
||||||
github.com/kardianos/govendor v1.0.9 // indirect
|
|
||||||
github.com/nfnt/resize v0.0.0-20180221191011-83c6a9932646
|
|
||||||
github.com/pkg/errors v0.9.1 // indirect
|
|
||||||
github.com/skip2/go-qrcode v0.0.0-20200617195104-da1b6568686e
|
|
||||||
golang.org/x/image v0.0.0-20200618115811-c13761719519 // indirect
|
|
||||||
golang.org/x/tools v0.0.0-20200708003708-134513de8882 // indirect
|
|
||||||
gopkg.in/yaml.v2 v2.3.0 // indirect
|
|
||||||
)
|
|
||||||
@@ -15,6 +15,8 @@ json与struct之间转换处理工具使用示例
|
|||||||
|
|
||||||
2020/12/9 :新增Gin框架使用示例
|
2020/12/9 :新增Gin框架使用示例
|
||||||
|
|
||||||
|
2020/12/15: 新增通过gopsutil包读取服务器信息的方法
|
||||||
|
|
||||||
# mod vendor模式加载包
|
# mod vendor模式加载包
|
||||||
通过go mod的方式加载的github上面的包会有报红的问题,但是包本身是可以运行的,这样就是会有一个问题,如果你想要点击去看方法的内容,没办法做到
|
通过go mod的方式加载的github上面的包会有报红的问题,但是包本身是可以运行的,这样就是会有一个问题,如果你想要点击去看方法的内容,没办法做到
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user