From 613ada987d2da5d1bcbbd7d4fd40ef0ab69132c2 Mon Sep 17 00:00:00 2001 From: Sean Chittenden Date: Thu, 11 Aug 2016 00:48:24 -0700 Subject: [PATCH] Add DoSysctrl() to Linux's common utilities. --- internal/common/common_linux.go | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/internal/common/common_linux.go b/internal/common/common_linux.go index 06fe4cb..9568106 100644 --- a/internal/common/common_linux.go +++ b/internal/common/common_linux.go @@ -2,7 +2,31 @@ package common -import "os" +import ( + "os" + "os/exec" + "strings" +) + +func DoSysctrl(mib string) ([]string, error) { + err := os.Setenv("LC_ALL", "C") + if err != nil { + return []string{}, err + } + sysctl, err := exec.LookPath("/sbin/sysctl") + if err != nil { + return []string{}, err + } + out, err := exec.Command(sysctl, "-n", mib).Output() + if err != nil { + return []string{}, err + } + v := strings.Replace(string(out), "{ ", "", 1) + v = strings.Replace(string(v), " }", "", 1) + values := strings.Fields(string(v)) + + return values, nil +} func NumProcs() (uint64, error) { f, err := os.Open(HostProc())