diff --git a/collector/meminfo_bsd.go b/collector/meminfo_bsd.go index 9e70fd1a..bcdc3879 100644 --- a/collector/meminfo_bsd.go +++ b/collector/meminfo_bsd.go @@ -17,30 +17,12 @@ package collector import ( - "errors" + "fmt" "github.com/prometheus/client_golang/prometheus" + "golang.org/x/sys/unix" ) -/* -#include -#include - -int _sysctl(const char* name) { - int val; - size_t size = sizeof(val); - int res = sysctlbyname(name, &val, &size, NULL, 0); - if (res == -1) { - return -1; - } - if (size != sizeof(val)) { - return -2; - } - return val; -} -*/ -import "C" - const ( memInfoSubsystem = "memory" ) @@ -58,34 +40,20 @@ func NewMeminfoCollector() (Collector, error) { } func (c *meminfoCollector) Update(ch chan<- prometheus.Metric) (err error) { - var pages map[string]C.int - pages = make(map[string]C.int) - - size := C._sysctl(C.CString("vm.stats.vm.v_page_size")) - if size == -1 { - return errors.New("sysctl(vm.stats.vm.v_page_size) failed") - } - if size == -2 { - return errors.New("sysctl(vm.stats.vm.v_page_size) failed, wrong buffer size") - } - - pages["active"] = C._sysctl(C.CString("vm.stats.vm.v_active_count")) - pages["inactive"] = C._sysctl(C.CString("vm.stats.vm.v_inactive_count")) - pages["wire"] = C._sysctl(C.CString("vm.stats.vm.v_wire_count")) - pages["cache"] = C._sysctl(C.CString("vm.stats.vm.v_cache_count")) - pages["free"] = C._sysctl(C.CString("vm.stats.vm.v_free_count")) - pages["swappgsin"] = C._sysctl(C.CString("vm.stats.vm.v_swappgsin")) - pages["swappgsout"] = C._sysctl(C.CString("vm.stats.vm.v_swappgsout")) - pages["total"] = C._sysctl(C.CString("vm.stats.vm.v_page_count")) + pages := make(map[string]uint32) - for key := range pages { - if pages[key] == -1 { - return errors.New("sysctl() failed for " + key) - } - if pages[key] == -2 { - return errors.New("sysctl() failed for " + key + ", wrong buffer size") - } + size, err := unix.SysctlUint32("vm.stats.vm.v_page_size") + if err != nil { + return fmt.Errorf("sysctl(vm.stats.vm.v_page_size) failed: %s", err) } + pages["active"], _ = unix.SysctlUint32("vm.stats.vm.v_active_count") + pages["inactive"], _ = unix.SysctlUint32("vm.stats.vm.v_inactive_count") + pages["wire"], _ = unix.SysctlUint32("vm.stats.vm.v_wire_count") + pages["cache"], _ = unix.SysctlUint32("vm.stats.vm.v_cache_count") + pages["free"], _ = unix.SysctlUint32("vm.stats.vm.v_free_count") + pages["swappgsin"], _ = unix.SysctlUint32("vm.stats.vm.v_swappgsin") + pages["swappgsout"], _ = unix.SysctlUint32("vm.stats.vm.v_swappgsout") + pages["total"], _ = unix.SysctlUint32("vm.stats.vm.v_page_count") for k, v := range pages { ch <- prometheus.MustNewConstMetric(