diff options
author | Caleb Bassi <calebjbassi@gmail.com> | 2019-05-15 16:44:11 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-05-15 16:44:11 -0700 |
commit | d734634a5d364715f773ec0aa9c0198c4a760f22 (patch) | |
tree | d42e75458215ce70ddf48b6300bcb6b0ec8b2f24 | |
parent | 21cd6424249842c94b0abfaaa07080f6bad069c9 (diff) | |
parent | ea5e097d52c5d1ffa832248e0eb04f6aa649164e (diff) |
Merge pull request #143 from mgamsjager/freebsd-proc
FreeBSD process support
-rw-r--r-- | src/widgets/proc_freebsd.go | 66 | ||||
-rw-r--r-- | src/widgets/proc_other.go | 2 |
2 files changed, 67 insertions, 1 deletions
diff --git a/src/widgets/proc_freebsd.go b/src/widgets/proc_freebsd.go new file mode 100644 index 0000000..96c8833 --- /dev/null +++ b/src/widgets/proc_freebsd.go @@ -0,0 +1,66 @@ +// +build freebsd + +package widgets + +import ( + "encoding/json" + "fmt" + "log" + "os/exec" + "strconv" + "strings" +) + +type processList struct { + ProcessInformation struct { + Process []struct { + Pid string `json:"pid"` + Comm string `json:"command"` + Cpu string `json:"percent-cpu" ` + Mem string `json:"percent-memory" ` + Args string `json:"arguments" ` + } `json:"process"` + } `json:"process-information"` +} + +func getProcs() ([]Proc, error) { + output, err := exec.Command("ps", "-axo pid,comm,%cpu,%mem,args", "--libxo", "json").Output() + if err != nil { + return nil, fmt.Errorf("failed to execute 'ps' command: %v", err) + } + + list := processList{} + err = json.Unmarshal(output, &list) + if err != nil { + return nil, fmt.Errorf("failed to unmarshal json. %s", err) + } + procs := []Proc{} + + for _, process := range list.ProcessInformation.Process { + if process.Comm == "idle" { + continue + } + pid, err := strconv.Atoi(strings.TrimSpace(process.Pid)) + if err != nil { + log.Printf("failed to convert first field to int: %v. split: %v", err, process) + } + cpu, err := strconv.ParseFloat(process.Cpu, 64) + if err != nil { + log.Printf("failed to convert third field to float: %v. split: %v", err, process) + } + mem, err := strconv.ParseFloat(process.Mem, 64) + if err != nil { + log.Printf("failed to convert fourth field to float: %v. split: %v", err, process) + } + proc := Proc{ + Pid: pid, + CommandName: process.Comm, + Cpu: cpu, + Mem: mem, + FullCommand: process.Args, + } + procs = append(procs, proc) + } + + return procs, nil +} diff --git a/src/widgets/proc_other.go b/src/widgets/proc_other.go index 73e12a2..a4aedf0 100644 --- a/src/widgets/proc_other.go +++ b/src/widgets/proc_other.go @@ -1,4 +1,4 @@ -// +build freebsd darwin openbsd +// +build darwin openbsd package widgets |