2018-12-14 11:24:34 +08:00
|
|
|
package widgets
|
|
|
|
|
|
|
|
import (
|
2018-12-14 13:59:45 +08:00
|
|
|
"fmt"
|
2018-12-14 11:24:34 +08:00
|
|
|
"log"
|
|
|
|
"os/exec"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2019-03-01 08:29:52 +08:00
|
|
|
func getProcs() ([]Proc, error) {
|
2018-12-14 11:24:34 +08:00
|
|
|
output, err := exec.Command("ps", "-axo", "pid:10,comm:50,pcpu:5,pmem:5,args").Output()
|
|
|
|
if err != nil {
|
2020-06-19 08:51:01 +08:00
|
|
|
return nil, fmt.Errorf(tr.Value("widget.proc.err.ps", err.Error()))
|
2018-12-14 11:24:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// converts to []string, removing trailing newline and header
|
2019-03-01 08:29:52 +08:00
|
|
|
linesOfProcStrings := strings.Split(strings.TrimSuffix(string(output), "\n"), "\n")[1:]
|
2018-12-14 11:24:34 +08:00
|
|
|
|
2019-03-01 08:29:52 +08:00
|
|
|
procs := []Proc{}
|
|
|
|
for _, line := range linesOfProcStrings {
|
2018-12-14 11:24:34 +08:00
|
|
|
pid, err := strconv.Atoi(strings.TrimSpace(line[0:10]))
|
|
|
|
if err != nil {
|
2020-06-19 08:51:01 +08:00
|
|
|
log.Println(tr.Value("widget.proc.err.pidconv", err.Error(), line))
|
2018-12-14 11:24:34 +08:00
|
|
|
}
|
|
|
|
cpu, err := strconv.ParseFloat(strings.TrimSpace(line[63:68]), 64)
|
|
|
|
if err != nil {
|
2020-06-19 08:51:01 +08:00
|
|
|
log.Println(tr.Value("widget.proc.err.cpuconv", err.Error(), line))
|
2018-12-14 11:24:34 +08:00
|
|
|
}
|
|
|
|
mem, err := strconv.ParseFloat(strings.TrimSpace(line[69:74]), 64)
|
|
|
|
if err != nil {
|
2020-06-19 08:51:01 +08:00
|
|
|
log.Println(tr.Value("widget.proc.err.memconv", err.Error(), line))
|
2018-12-14 11:24:34 +08:00
|
|
|
}
|
2019-03-01 08:29:52 +08:00
|
|
|
proc := Proc{
|
|
|
|
Pid: pid,
|
|
|
|
CommandName: strings.TrimSpace(line[11:61]),
|
|
|
|
FullCommand: line[74:],
|
2020-04-28 09:33:41 +08:00
|
|
|
CPU: cpu,
|
2019-03-01 08:29:52 +08:00
|
|
|
Mem: mem,
|
2018-12-14 11:24:34 +08:00
|
|
|
}
|
2019-03-01 08:29:52 +08:00
|
|
|
procs = append(procs, proc)
|
2018-12-14 11:24:34 +08:00
|
|
|
}
|
2019-03-01 08:29:52 +08:00
|
|
|
|
|
|
|
return procs, nil
|
2018-12-14 11:24:34 +08:00
|
|
|
}
|