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