/
psutil.go
102 lines (98 loc) · 2.03 KB
/
psutil.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
91
92
93
94
95
96
97
98
99
100
101
102
package monitor
import (
"encoding/json"
"github.com/shirou/gopsutil/cpu"
"github.com/shirou/gopsutil/disk"
"github.com/shirou/gopsutil/host"
"github.com/shirou/gopsutil/load"
"github.com/shirou/gopsutil/mem"
"github.com/shirou/gopsutil/net"
"time"
)
type StatusServer struct {
Percent StatusPercent
CPU []CPUInfo
Mem MemInfo
Swap SwapInfo
Load *load.AvgStat
Network map[string]InterfaceInfo
BootTime uint64
Uptime uint64
}
type StatusPercent struct {
CPU float64
Disk float64
Mem float64
Swap float64
}
type CPUInfo struct {
ModelName string
Cores int32
}
type MemInfo struct {
Total uint64
Used uint64
Available uint64
}
type SwapInfo struct {
Total uint64
Used uint64
Available uint64
}
type InterfaceInfo struct {
Addrs []string
ByteSent uint64
ByteRecv uint64
}
func InfoMiniJSON() string {
v, _ := mem.VirtualMemory()
s, _ := mem.SwapMemory()
c, _ := cpu.Info()
cc, _ := cpu.Percent(time.Second, false)
d, _ := disk.Usage("/")
n, _ := host.Info()
nv, _ := net.IOCounters(true)
l, _ := load.Avg()
i, _ := net.Interfaces()
ss := new(StatusServer)
ss.Load = l
ss.Uptime = n.Uptime
ss.BootTime = n.BootTime
ss.Percent.Mem = v.UsedPercent
ss.Percent.CPU = cc[0]
ss.Percent.Swap = s.UsedPercent
ss.Percent.Disk = d.UsedPercent
ss.CPU = make([]CPUInfo, len(c))
for i, ci := range c {
ss.CPU[i].ModelName = ci.ModelName
ss.CPU[i].Cores = ci.Cores
}
ss.Mem.Total = v.Total
ss.Mem.Available = v.Available
ss.Mem.Used = v.Used
ss.Swap.Total = s.Total
ss.Swap.Available = s.Free
ss.Swap.Used = s.Used
ss.Network = make(map[string]InterfaceInfo)
for _, v := range nv {
var ii InterfaceInfo
ii.ByteSent = v.BytesSent
ii.ByteRecv = v.BytesRecv
ss.Network[v.Name] = ii
}
for _, v := range i {
if ii, ok := ss.Network[v.Name]; ok {
ii.Addrs = make([]string, len(v.Addrs))
for i, vv := range v.Addrs {
ii.Addrs[i] = vv.Addr
}
ss.Network[v.Name] = ii
}
}
b, err := json.Marshal(ss)
if err != nil {
return ""
} else {
return string(b)
}
}