Skip to content
This repository has been archived by the owner on Aug 29, 2020. It is now read-only.

Commit

Permalink
Close #85
Browse files Browse the repository at this point in the history
  • Loading branch information
cjbassi committed Dec 14, 2018
1 parent 23b961b commit 625b16c
Show file tree
Hide file tree
Showing 9 changed files with 38 additions and 18 deletions.
22 changes: 12 additions & 10 deletions src/widgets/cpu.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,10 @@ func (self *CPU) update() {
percent, err := psCPU.Percent(self.interval, false)
if err != nil {
log.Printf("failed to get average CPU usage percent from gopsutil: %v. self.interval: %v. percpu: %v", err, self.interval, false)
} else {
self.Data["AVRG"] = append(self.Data["AVRG"], percent[0])
self.Labels["AVRG"] = fmt.Sprintf("%3.0f%%", percent[0])
}
self.Data["AVRG"] = append(self.Data["AVRG"], percent[0])
self.Labels["AVRG"] = fmt.Sprintf("%3.0f%%", percent[0])
}()
}

Expand All @@ -87,15 +88,16 @@ func (self *CPU) update() {
percents, err := psCPU.Percent(self.interval, true)
if err != nil {
log.Printf("failed to get CPU usage percents from gopsutil: %v. self.interval: %v. percpu: %v", err, self.interval, true)
}
if len(percents) == self.Count {
for i, percent := range percents {
k := fmt.Sprintf(self.formatString, i)
self.Data[k] = append(self.Data[k], percent)
self.Labels[k] = fmt.Sprintf("%3.0f%%", percent)
}
} else {
log.Printf("error: number of CPU usage percents from gopsutil doesn't match CPU count. percents: %v. self.Count: %v", percents, self.Count)
if len(percents) != self.Count {
log.Printf("error: number of CPU usage percents from gopsutil doesn't match CPU count. percents: %v. self.Count: %v", percents, self.Count)
} else {
for i, percent := range percents {
k := fmt.Sprintf(self.formatString, i)
self.Data[k] = append(self.Data[k], percent)
self.Labels[k] = fmt.Sprintf("%3.0f%%", percent)
}
}
}
}()
}
Expand Down
1 change: 1 addition & 0 deletions src/widgets/disk.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ func (self *Disk) update() {
Partitions, err := psDisk.Partitions(false)
if err != nil {
log.Printf("failed to get disk partitions from gopsutil: %v", err)
return
}

// add partition if it's new
Expand Down
1 change: 1 addition & 0 deletions src/widgets/net.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ func (self *Net) update() {
interfaces, err := psNet.IOCounters(true)
if err != nil {
log.Printf("failed to get network activity from gopsutil: %v", err)
return
}
var curRecvTotal uint64
var curSentTotal uint64
Expand Down
14 changes: 10 additions & 4 deletions src/widgets/proc_linux.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
package widgets

import (
"fmt"
"log"
"os/exec"
"strconv"
"strings"
)

func (self *Proc) update() {
processes := Processes()
processes, err := Processes()
if err != nil {
log.Printf("failed to retrieve processes: %v", err)
return
}

// have to iterate like this in order to actually change the value
for i := range processes {
processes[i].CPU /= self.cpuCount
Expand All @@ -20,10 +26,10 @@ func (self *Proc) update() {
self.Sort()
}

func Processes() []Process {
func Processes() ([]Process, error) {
output, err := exec.Command("ps", "-axo", "pid:10,comm:50,pcpu:5,pmem:5,args").Output()
if err != nil {
log.Printf("failed to execute 'ps' command: %v", err)
return nil, fmt.Errorf("failed to execute 'ps' command: %v", err)
}

// converts to []string, removing trailing newline and header
Expand Down Expand Up @@ -52,5 +58,5 @@ func Processes() []Process {
}
processes = append(processes, process)
}
return processes
return processes, nil
}
14 changes: 10 additions & 4 deletions src/widgets/proc_other.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,20 @@
package widgets

import (
"fmt"
"log"
"os/exec"
"strconv"
"strings"
)

func (self *Proc) update() {
processes := Processes()
processes, err := Processes()
if err != nil {
log.Printf("failed to retrieve processes: %v", err)
return
}

// have to iterate like this in order to actually change the value
for i := range processes {
processes[i].CPU /= self.cpuCount
Expand All @@ -22,10 +28,10 @@ func (self *Proc) update() {
self.Sort()
}

func Processes() []Process {
func Processes() ([]Process, error) {
output, err := exec.Command("ps", "-axo", "pid,comm,pcpu,pmem,args").Output()
if err != nil {
log.Printf("failed to execute 'ps' command: %v", err)
return nil, fmt.Errorf("failed to execute 'ps' command: %v", err)
}
// converts to []string and removes the header
strOutput := strings.Split(strings.TrimSpace(string(output)), "\n")[1:]
Expand Down Expand Up @@ -53,5 +59,5 @@ func Processes() []Process {
}
processes = append(processes, process)
}
return processes
return processes, nil
}
1 change: 1 addition & 0 deletions src/widgets/proc_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ func (self *Proc) update() {
psProcesses, err := psProc.Processes()
if err != nil {
log.Printf("failed to get processes from gopsutil: %v", err)
return
}
processes := make([]Process, len(psProcesses))
for i, psProcess := range psProcesses {
Expand Down
1 change: 1 addition & 0 deletions src/widgets/temp_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ func (self *Temp) update() {
sensors, err := SensorsTemperatures()
if err != nil {
log.Printf("failed to get sensors from CGO: %v", err)
return
}
for _, sensor := range sensors {
if sensor.Temperature != 0 {
Expand Down
1 change: 1 addition & 0 deletions src/widgets/temp_other.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ func (self *Temp) update() {
sensors, err := psHost.SensorsTemperatures()
if err != nil {
log.Printf("failed to get sensors from gopsutil: %v", err)
return
}
for _, sensor := range sensors {
// only sensors with input in their name are giving us live temp info
Expand Down
1 change: 1 addition & 0 deletions src/widgets/temp_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ func (self *Temp) update() {
sensors, err := psHost.SensorsTemperatures()
if err != nil {
log.Printf("failed to get sensors from gopsutil: %v", err)
return
}
for _, sensor := range sensors {
if sensor.Temperature != 0 {
Expand Down

0 comments on commit 625b16c

Please sign in to comment.