-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsysteminfo.go
155 lines (130 loc) · 3.3 KB
/
systeminfo.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
package main
import (
"fmt"
"sort"
"time"
"github.com/shirou/gopsutil/v4/cpu"
"github.com/shirou/gopsutil/v4/mem"
"github.com/shirou/gopsutil/v4/process"
)
func GetCPUStats() (cpu.TimesStat, error) {
stats, err := cpu.Times(false)
if err != nil {
return cpu.TimesStat{}, err
}
if len(stats) == 0 {
return cpu.TimesStat{}, nil
}
currStats := stats[0]
total := currStats.User + currStats.System + currStats.Idle + currStats.Nice +
currStats.Iowait + currStats.Irq + currStats.Softirq + currStats.Steal +
currStats.Guest
if total == 0 {
return cpu.TimesStat{}, nil
}
// Overwrite TimesStat fields with percentage values
currStats.User = (currStats.User / total) * 100
currStats.System = (currStats.System / total) * 100
currStats.Idle = (currStats.Idle / total) * 100
currStats.Nice = (currStats.Nice / total) * 100
currStats.Iowait = (currStats.Iowait / total) * 100
currStats.Irq = (currStats.Irq / total) * 100
currStats.Softirq = (currStats.Softirq / total) * 100
currStats.Steal = (currStats.Steal / total) * 100
currStats.Guest = (currStats.Guest / total) * 100
return currStats, nil
}
func GetMEMStats() (mem.VirtualMemoryStat, error) {
v, err := mem.VirtualMemory()
if err != nil {
return mem.VirtualMemoryStat{}, err
}
return mem.VirtualMemoryStat{
Total: v.Total,
Used: v.Used,
Free: v.Free,
UsedPercent: v.UsedPercent,
Available: v.Available,
}, nil
}
type ProcessInfo struct {
PID int32
Name string
Username string
Memory uint64
CPUPercent float64 // CPU usage percentage
RunningTime string
}
func GetProcesses(n int) ([]ProcessInfo, error) {
procs, err := process.Processes()
if err != nil {
return nil, err
}
var processInfos []ProcessInfo
for _, p := range procs {
pid := p.Pid
name, err := p.Name()
if err != nil {
name = "Unknown"
}
createTime, err := p.CreateTime()
if err != nil {
createTime = 0
}
startTime := time.Unix(createTime/1000, 0)
runningTime := time.Since(startTime).Truncate(time.Second)
username, err := p.Username()
if err != nil {
name = "Unknown"
}
memoryInfo, err := p.MemoryInfo()
if err != nil {
processInfos = append(processInfos, ProcessInfo{
PID: pid,
Name: name,
RunningTime: runningTime.String(),
Username: username,
Memory: 0,
CPUPercent: 0,
})
continue
}
memory := memoryInfo.RSS
cpuPercent, err := p.CPUPercent()
if err != nil {
cpuPercent = 0
}
processInfos = append(processInfos, ProcessInfo{
PID: pid,
Name: name,
RunningTime: runningTime.String(),
Username: username,
Memory: memory,
CPUPercent: cpuPercent,
})
}
sort.Slice(processInfos, func(i, j int) bool {
return processInfos[i].CPUPercent > processInfos[j].CPUPercent
})
if len(processInfos) > n {
processInfos = processInfos[:n]
}
return processInfos, nil
}
func convertBytes(bytes uint64) (string, string) {
const (
KB = 1024
MB = KB * 1024
GB = MB * 1024
)
switch {
case bytes >= GB:
return fmt.Sprintf("%.2f", float64(bytes)/float64(GB)), "GB"
case bytes >= MB:
return fmt.Sprintf("%.2f", float64(bytes)/float64(MB)), "MB"
case bytes >= KB:
return fmt.Sprintf("%.2f", float64(bytes)/float64(KB)), "KB"
default:
return fmt.Sprintf("%d", bytes), "B"
}
}