This repository has been archived by the owner on Aug 29, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 442
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #145 from mgamsjager/freebsd-fixes
Freebsd fixes
- Loading branch information
Showing
7 changed files
with
207 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package utils | ||
|
||
import ( | ||
"strings" | ||
) | ||
|
||
func ConvertLocalizedString(s string) string { | ||
if strings.ContainsAny(s, ",") { | ||
return strings.Replace(s, ",", ".", 1) | ||
} else { | ||
return s | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package widgets | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"os/exec" | ||
"strconv" | ||
"strings" | ||
|
||
"github.com/cjbassi/gotop/src/utils" | ||
) | ||
|
||
func convert(s []string) (MemoryInfo, error) { | ||
total, err := strconv.ParseUint(s[0], 10, 64) | ||
if err != nil { | ||
return MemoryInfo{}, fmt.Errorf("int converion failed %v", err) | ||
} | ||
|
||
used, err := strconv.ParseUint(s[1], 10, 64) | ||
if err != nil { | ||
return MemoryInfo{}, fmt.Errorf("int converion failed %v", err) | ||
} | ||
|
||
percentage, err := strconv.ParseFloat(strings.TrimSuffix(s[2], "%"), 64) | ||
if err != nil { | ||
return MemoryInfo{}, fmt.Errorf("float converion failed %v", err) | ||
} | ||
|
||
return MemoryInfo{ | ||
Total: total * utils.KB, | ||
Used: used * utils.KB, | ||
UsedPercent: percentage, | ||
}, nil | ||
} | ||
|
||
func gatherSwapInfo() (MemoryInfo, error) { | ||
cmd := "swapinfo -k|sed -n '1!p'|awk '{print $2,$3,$5}'" | ||
output, err := exec.Command("sh", "-c", cmd).Output() | ||
if err != nil { | ||
if err != nil { | ||
return MemoryInfo{}, fmt.Errorf("command failed %v", err) | ||
} | ||
} | ||
|
||
ss := strings.Split(strings.TrimSuffix(string(output), "\n"), " ") | ||
|
||
return convert(ss) | ||
} | ||
|
||
func (self *MemWidget) updateSwapMemory() { | ||
swapMemory, err := gatherSwapInfo() | ||
if err != nil { | ||
log.Printf("failed to get swap memory info from gopsutil: %v", err) | ||
} else { | ||
self.renderMemInfo("Swap", MemoryInfo{ | ||
Total: swapMemory.Total, | ||
Used: swapMemory.Used, | ||
UsedPercent: swapMemory.UsedPercent, | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
// +build !freebsd | ||
|
||
package widgets | ||
|
||
import ( | ||
"log" | ||
|
||
psMem "github.com/shirou/gopsutil/mem" | ||
) | ||
|
||
func (self *MemWidget) updateSwapMemory() { | ||
swapMemory, err := psMem.SwapMemory() | ||
if err != nil { | ||
log.Printf("failed to get swap memory info from gopsutil: %v", err) | ||
} else { | ||
self.renderMemInfo("Swap", MemoryInfo{ | ||
Total: swapMemory.Total, | ||
Used: swapMemory.Used, | ||
UsedPercent: swapMemory.UsedPercent, | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package widgets | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"os/exec" | ||
"strconv" | ||
"strings" | ||
|
||
"github.com/cjbassi/gotop/src/utils" | ||
) | ||
|
||
var sensorOIDS = map[string]string{ | ||
"dev.cpu.0.temperature": "CPU 0 ", | ||
"hw.acpi.thermal.tz0.temperature": "Thermal zone 0", | ||
} | ||
|
||
type sensorMeasurement struct { | ||
name string | ||
temperature float64 | ||
} | ||
|
||
func removeUnusedChars(s string) string { | ||
s1 := strings.Replace(s, "C", "", 1) | ||
s2 := strings.TrimSuffix(s1, "\n") | ||
return s2 | ||
} | ||
|
||
func refineOutput(output []byte) (float64, error) { | ||
convertedOutput := utils.ConvertLocalizedString(removeUnusedChars(string(output))) | ||
value, err := strconv.ParseFloat(convertedOutput, 64) | ||
if err != nil { | ||
return 0, err | ||
} | ||
return value, nil | ||
} | ||
|
||
func collectSensors() ([]sensorMeasurement, error) { | ||
var measurements []sensorMeasurement | ||
for k, v := range sensorOIDS { | ||
output, err := exec.Command("sysctl", "-n", k).Output() | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to execute 'sysctl' command: %v", err) | ||
} | ||
|
||
value, err := refineOutput(output) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to execute 'sysctl' command: %v", err) | ||
} | ||
|
||
measurements = append(measurements, sensorMeasurement{v, value}) | ||
|
||
} | ||
return measurements, nil | ||
|
||
} | ||
|
||
func (self *TempWidget) update() { | ||
sensors, err := collectSensors() | ||
if err != nil { | ||
log.Printf("error recieved from gopsutil: %v", err) | ||
} | ||
for _, sensor := range sensors { | ||
switch self.TempScale { | ||
case Fahrenheit: | ||
self.Data[sensor.name] = utils.CelsiusToFahrenheit(int(sensor.temperature)) | ||
case Celcius: | ||
self.Data[sensor.name] = int(sensor.temperature) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,3 @@ | ||
// +build linux freebsd | ||
|
||
package widgets | ||
|
||
import ( | ||
|