xmtop/devices/mem_swap_freebsd.go
Sean E. Russell 7a89225d72 Abstracts CPU & mem devices.
Finishes device refactoring: temps

Refactoring to allow updating maps. Simpler, and optimizable.
2020-02-28 07:06:51 -06:00

45 lines
942 B
Go

// +build freebsd
package devices
import (
"os/exec"
"strconv"
"strings"
)
func init() {
mf := func(mems map[string]MemoryInfo) map[string]error {
cmd := "swapinfo -k|sed -n '1!p'|awk '{print $2,$3,$5}'"
output, err := exec.Command("sh", "-c", cmd).Output()
if err != nil {
return map[string]error{"swapinfo": err}
}
s := strings.TrimSuffix(string(output), "\n")
s = strings.ReplaceAll(s, "\n", " ")
ss := strings.Split(s, " ")
ss = ss[((len(ss)/3)-1)*3:]
errors := make(map[string]error)
mem := MemoryInfo{}
mem.Total, err = strconv.ParseUint(ss[0], 10, 64)
if err != nil {
errors["swap total"] = err
}
mem.Used, err = strconv.ParseUint(ss[1], 10, 64)
if err != nil {
errors["swap used"] = err
}
mem.UsedPercent, err = strconv.ParseFloat(strings.TrimSuffix(ss[2], "%"), 64)
if err != nil {
errors["swap percent"] = err
}
mems["Swap"] = mem
return errors
}
RegisterMem(mf)
}