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"
|
2020-06-22 00:49:04 +08:00
|
|
|
"strconv"
|
2018-12-05 13:44:25 +08:00
|
|
|
|
2020-06-22 00:49:04 +08:00
|
|
|
"github.com/shirou/gopsutil/process"
|
2018-05-16 04:13:22 +08:00
|
|
|
)
|
|
|
|
|
2019-03-01 08:29:52 +08:00
|
|
|
func getProcs() ([]Proc, error) {
|
2020-06-22 00:49:04 +08:00
|
|
|
psProcs, err := process.Processes()
|
2018-12-05 13:44:25 +08:00
|
|
|
if err != nil {
|
2020-06-19 08:51:01 +08:00
|
|
|
return nil, fmt.Errorf(tr.Value("widget.proc.err.gopsutil", err.Error()))
|
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 {
|
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))
|
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 {
|
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))
|
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 {
|
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))
|
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
|
|
|
}
|