2018-05-16 04:13:22 +08:00
|
|
|
package widgets
|
|
|
|
|
|
|
|
import (
|
2019-03-01 08:29:52 +08:00
|
|
|
"fmt"
|
2018-12-05 13:44:25 +08:00
|
|
|
"log"
|
|
|
|
|
2018-05-16 04:13:22 +08:00
|
|
|
psProc "github.com/shirou/gopsutil/process"
|
|
|
|
)
|
|
|
|
|
2019-03-01 08:29:52 +08:00
|
|
|
func getProcs() ([]Proc, error) {
|
|
|
|
psProcs, err := psProc.Processes()
|
2018-12-05 13:44:25 +08:00
|
|
|
if err != nil {
|
2019-03-01 08:29:52 +08:00
|
|
|
return nil, fmt.Errorf("failed to get processes from gopsutil: %v", err)
|
2018-12-05 13:44:25 +08:00
|
|
|
}
|
2019-03-01 08:29:52 +08:00
|
|
|
|
|
|
|
procs := make([]Proc, len(psProcs))
|
|
|
|
for i, psProc := range psProcs {
|
|
|
|
pid := psProc.Pid
|
|
|
|
command, err := psProc.Name()
|
2018-12-05 13:44:25 +08:00
|
|
|
if err != nil {
|
2019-03-01 08:29:52 +08:00
|
|
|
log.Printf("failed to get process command from gopsutil: %v. psProc: %v. i: %v. pid: %v", err, psProc, i, pid)
|
2018-12-05 13:44:25 +08:00
|
|
|
}
|
2019-03-01 08:29:52 +08:00
|
|
|
cpu, err := psProc.CPUPercent()
|
2018-12-05 13:44:25 +08:00
|
|
|
if err != nil {
|
2019-03-01 08:29:52 +08:00
|
|
|
log.Printf("failed to get process cpu usage from gopsutil: %v. psProc: %v. i: %v. pid: %v", err, psProc, i, pid)
|
2018-12-05 13:44:25 +08:00
|
|
|
}
|
2019-03-01 08:29:52 +08:00
|
|
|
mem, err := psProc.MemoryPercent()
|
2018-12-05 13:44:25 +08:00
|
|
|
if err != nil {
|
2019-03-01 08:29:52 +08:00
|
|
|
log.Printf("failed to get process memeory usage from gopsutil: %v. psProc: %v. i: %v. pid: %v", err, psProc, i, pid)
|
2018-12-05 13:44:25 +08:00
|
|
|
}
|
2018-05-16 04:13:22 +08:00
|
|
|
|
2019-03-01 08:29:52 +08:00
|
|
|
procs[i] = Proc{
|
2019-03-12 13:52:37 +08:00
|
|
|
Pid: int(pid),
|
|
|
|
CommandName: command,
|
2020-04-29 02:23:50 +08:00
|
|
|
CPU: cpu,
|
2019-03-12 13:52:37 +08:00
|
|
|
Mem: float64(mem),
|
2018-05-17 05:38:28 +08:00
|
|
|
// getting command args using gopsutil's Cmdline and CmdlineSlice wasn't
|
|
|
|
// working the last time I tried it, so we're just reusing 'command'
|
2019-03-12 13:52:37 +08:00
|
|
|
FullCommand: command,
|
2018-05-16 04:13:22 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-01 08:29:52 +08:00
|
|
|
return procs, nil
|
2018-05-16 04:13:22 +08:00
|
|
|
}
|