xmtop/widgets/proc_windows.go

55 lines
1.3 KiB
Go
Raw Normal View History

package widgets
import (
2019-03-01 08:29:52 +08:00
"fmt"
"log"
2020-06-22 00:49:04 +08:00
"strconv"
2020-06-22 00:49:04 +08:00
"github.com/shirou/gopsutil/process"
)
2019-03-01 08:29:52 +08:00
func getProcs() ([]Proc, error) {
2020-06-22 00:49:04 +08:00
psProcs, err := process.Processes()
if err != nil {
2020-06-19 08:51:01 +08:00
return nil, fmt.Errorf(tr.Value("widget.proc.err.gopsutil", err.Error()))
}
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()
if err != nil {
2020-06-22 00:49:04 +08:00
sps := fmt.Sprintf("%v", psProc)
si := strconv.Itoa(i)
spid := fmt.Sprintf("%d", pid)
log.Println(tr.Value("widget.proc.err.getcmd", err.Error(), sps, si, spid))
}
2019-03-01 08:29:52 +08:00
cpu, err := psProc.CPUPercent()
if err != nil {
2020-06-22 00:49:04 +08:00
sps := fmt.Sprintf("%v", psProc)
si := strconv.Itoa(i)
spid := fmt.Sprintf("%d", pid)
log.Println(tr.Value("widget.proc.err.cpupercent", err.Error(), sps, si, spid))
}
2019-03-01 08:29:52 +08:00
mem, err := psProc.MemoryPercent()
if err != nil {
2020-06-22 00:49:04 +08:00
sps := fmt.Sprintf("%v", psProc)
si := strconv.Itoa(i)
spid := fmt.Sprintf("%d", pid)
log.Println(tr.Value("widget.proc.err.mempercent", err.Error(), sps, si, spid))
}
2019-03-01 08:29:52 +08:00
procs[i] = Proc{
2019-03-12 13:52:37 +08:00
Pid: int(pid),
CommandName: command,
CPU: cpu,
2019-03-12 13:52:37 +08:00
Mem: float64(mem),
// 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,
}
}
2019-03-01 08:29:52 +08:00
return procs, nil
}