-
Notifications
You must be signed in to change notification settings - Fork 1
/
client_used_ratio.go
90 lines (80 loc) · 1.81 KB
/
client_used_ratio.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/**
* Author: haoshuaiwei
* Date: 2019-05-23 15:36
*/
package fast_crawl_client
import (
"fast-work/fast-driver"
"github.com/hsw409328/gofunc"
"github.com/shirou/gopsutil/disk"
"github.com/shirou/gopsutil/host"
"github.com/shirou/gopsutil/mem"
"runtime"
"time"
)
type CpuData struct {
CpuNumber int `cpu核数`
UsedNumber int `cpu使用数量`
UsableNumber int `cpu可用数量`
}
func mClient() *host.InfoStat {
hostInfo, _ := host.Info()
return hostInfo
}
func mCpu() int {
return runtime.NumCPU()
}
type MemData struct {
TotalMem int
UsableMem float64
UsedPercent float64
}
func mMemory() *MemData {
memValue, _ := mem.VirtualMemory()
baseByte := float64(1024 * 1024 * 1024)
return &MemData{
TotalMem: int(float64(memValue.Total) / baseByte),
UsableMem: float64(float64(memValue.Available) / baseByte),
UsedPercent: memValue.UsedPercent,
}
}
type DiskData struct {
Total int
Used int
Usable int
UsedPercent float64
}
func mDisk() *DiskData {
d, _ := disk.Usage("/")
baseByte := uint64(1024 * 1024 * 1024)
return &DiskData{
Total: int(d.Total / baseByte),
Used: int(d.Used / baseByte),
Usable: int(d.Free / baseByte),
UsedPercent: d.UsedPercent,
}
}
func MonitorClient() {
go func() {
for {
//每10秒汇报一下状态
hostInfo := mClient()
memInfo := mMemory()
diskInfo := mDisk()
saveResult, err := gofunc.MapOrSliceToJsonString(map[string]interface{}{
"HostInfo": hostInfo,
"MemInfo": memInfo,
"DiskInfo": diskInfo,
"CreateTime": gofunc.CurrentTime(),
})
if err != nil {
clientLog.Error(err)
}
result := fast_driver.RedisDriver.HSet("ClientInfo", hostInfo.Hostname, saveResult)
if result.Err() != nil {
clientLog.Error(err)
}
time.Sleep(time.Second * 10)
}
}()
}