2019-05-14 17:08:46 +08:00
|
|
|
// +build darwin openbsd
|
2018-12-14 11:13:28 +08:00
|
|
|
|
2018-05-16 04:13:22 +08:00
|
|
|
package widgets
|
2018-05-16 02:10:30 +08:00
|
|
|
|
|
|
|
import (
|
2018-12-14 13:59:45 +08:00
|
|
|
"fmt"
|
2018-12-05 13:44:25 +08:00
|
|
|
"log"
|
2018-05-16 02:10:30 +08:00
|
|
|
"os/exec"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
2019-05-29 01:32:45 +08:00
|
|
|
|
2020-02-29 04:48:35 +08:00
|
|
|
"github.com/xxxserxxx/gotop/v3/utils"
|
2018-05-16 02:10:30 +08:00
|
|
|
)
|
|
|
|
|
2019-02-10 06:43:09 +08:00
|
|
|
const (
|
2019-03-01 08:29:52 +08:00
|
|
|
// Define column widths for ps output used in Procs()
|
2019-02-10 06:43:09 +08:00
|
|
|
five = "12345"
|
|
|
|
ten = five + five
|
|
|
|
fifty = ten + ten + ten + ten + ten
|
|
|
|
)
|
|
|
|
|
2019-03-12 04:35:13 +08:00
|
|
|
func getProcs() ([]Proc, error) {
|
2019-02-10 06:43:09 +08:00
|
|
|
keywords := fmt.Sprintf("pid=%s,comm=%s,pcpu=%s,pmem=%s,args", ten, fifty, five, five)
|
2019-02-10 22:16:12 +08:00
|
|
|
output, err := exec.Command("ps", "-caxo", keywords).Output()
|
2018-12-05 13:44:25 +08:00
|
|
|
if err != nil {
|
2018-12-14 13:59:45 +08:00
|
|
|
return nil, fmt.Errorf("failed to execute 'ps' command: %v", err)
|
2018-12-05 13:44:25 +08:00
|
|
|
}
|
2019-02-10 06:43:09 +08:00
|
|
|
|
2018-12-14 11:24:34 +08:00
|
|
|
// converts to []string and removes the header
|
2019-03-01 08:29:52 +08:00
|
|
|
linesOfProcStrings := strings.Split(strings.TrimSpace(string(output)), "\n")[1:]
|
2019-02-10 06:43:09 +08:00
|
|
|
|
2019-03-01 08:29:52 +08:00
|
|
|
procs := []Proc{}
|
|
|
|
for _, line := range linesOfProcStrings {
|
2019-02-10 06:43:09 +08:00
|
|
|
pid, err := strconv.Atoi(strings.TrimSpace(line[0:10]))
|
2018-12-05 13:44:25 +08:00
|
|
|
if err != nil {
|
2019-02-10 06:43:09 +08:00
|
|
|
log.Printf("failed to convert first field to int: %v. split: %v", err, line)
|
2018-12-05 13:44:25 +08:00
|
|
|
}
|
2019-05-29 01:32:45 +08:00
|
|
|
cpu, err := strconv.ParseFloat(utils.ConvertLocalizedString(strings.TrimSpace(line[63:68])), 64)
|
2018-12-05 13:44:25 +08:00
|
|
|
if err != nil {
|
2019-02-10 06:43:09 +08:00
|
|
|
log.Printf("failed to convert third field to float: %v. split: %v", err, line)
|
2018-12-05 13:44:25 +08:00
|
|
|
}
|
2019-05-29 01:32:45 +08:00
|
|
|
mem, err := strconv.ParseFloat(utils.ConvertLocalizedString(strings.TrimSpace(line[69:74])), 64)
|
2018-12-05 13:44:25 +08:00
|
|
|
if err != nil {
|
2019-02-10 06:43:09 +08:00
|
|
|
log.Printf("failed to convert fourth field to float: %v. split: %v", err, line)
|
2018-12-05 13:44:25 +08:00
|
|
|
}
|
2019-03-01 08:29:52 +08:00
|
|
|
proc := Proc{
|
2019-03-12 04:35:13 +08:00
|
|
|
Pid: pid,
|
|
|
|
CommandName: strings.TrimSpace(line[11:61]),
|
|
|
|
Cpu: cpu,
|
|
|
|
Mem: mem,
|
|
|
|
FullCommand: line[74:],
|
2018-05-16 02:10:30 +08:00
|
|
|
}
|
2019-03-01 08:29:52 +08:00
|
|
|
procs = append(procs, proc)
|
2018-05-16 02:10:30 +08:00
|
|
|
}
|
2019-03-01 08:29:52 +08:00
|
|
|
|
|
|
|
return procs, nil
|
2018-05-16 02:10:30 +08:00
|
|
|
}
|