summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMatthias Gamsjager <matthias.gamsjager@philips.com>2019-05-14 11:08:46 +0200
committerMatthias Gamsjager <matthias.gamsjager@philips.com>2019-05-14 12:57:05 +0200
commit68d38a61d7ce0a854c604a698f917a3b80b2c9b4 (patch)
tree8ca6abfcc38a9dd5564402c4a4a3876087a3fa89
parent21cd6424249842c94b0abfaaa07080f6bad069c9 (diff)
Create separate file for FreeBSD
-rw-r--r--src/widgets/proc_freebsd.go66
-rw-r--r--src/widgets/proc_other.go2
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..0122496
--- /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 Keywords 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)
+ }
+
+ processList := Keywords{}
+ err = json.Unmarshal(output, &processList)
+ if err != nil {
+ return nil, fmt.Errorf("failed to unmarshal json. %s", err)
+ } else {
+ return nil, fmt.Errorf("Success to unmarshal json. %s", output)
+ }
+
+ procs := []Proc{}
+
+ for _, process := range processList.ProcessInformation.Process {
+ 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