59 lines
859 B
Go
59 lines
859 B
Go
package models
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
ps "github.com/shirou/gopsutil/v3/process"
|
|
)
|
|
|
|
type Process struct {
|
|
PID string
|
|
User string
|
|
Cmd string
|
|
CPU float64
|
|
Memory uint64
|
|
}
|
|
|
|
func GetProcesses() ([]Process, error) {
|
|
processes, err := ps.Processes()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
var result []Process
|
|
for _, p := range processes {
|
|
pid := p.Pid
|
|
|
|
name, err := p.Name()
|
|
if err != nil {
|
|
continue
|
|
}
|
|
|
|
user, err := p.Username()
|
|
if err != nil {
|
|
user = "unknown"
|
|
}
|
|
|
|
cpu, err := p.CPUPercent()
|
|
if err != nil {
|
|
cpu = 0
|
|
}
|
|
|
|
memInfo, err := p.MemoryInfo()
|
|
|
|
if err != nil {
|
|
continue
|
|
}
|
|
|
|
result = append(result, Process{
|
|
PID: fmt.Sprintf("%d", pid),
|
|
User: user,
|
|
Cmd: name,
|
|
CPU: cpu,
|
|
Memory: memInfo.RSS,
|
|
})
|
|
}
|
|
|
|
return result, nil
|
|
}
|