Browse Source

Implement meminfo on BSD without cgo

This removes some error handling, which should be fine. If the calls
fail, we will get the zeroes, which is a safe enough fallback.
Additionally, if the first sysctl (page_size) succeeded it is unlikely
that other ones will fail.
pull/385/head
Dominik Honnef 8 years ago
parent
commit
d2a43f7d05
  1. 60
      collector/meminfo_bsd.go

60
collector/meminfo_bsd.go

@ -17,30 +17,12 @@
package collector package collector
import ( import (
"errors" "fmt"
"github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus"
"golang.org/x/sys/unix"
) )
/*
#include <stddef.h>
#include <sys/sysctl.h>
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 ( const (
memInfoSubsystem = "memory" memInfoSubsystem = "memory"
) )
@ -58,34 +40,20 @@ func NewMeminfoCollector() (Collector, error) {
} }
func (c *meminfoCollector) Update(ch chan<- prometheus.Metric) (err error) { func (c *meminfoCollector) Update(ch chan<- prometheus.Metric) (err error) {
var pages map[string]C.int pages := make(map[string]uint32)
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"))
for key := range pages { size, err := unix.SysctlUint32("vm.stats.vm.v_page_size")
if pages[key] == -1 { if err != nil {
return errors.New("sysctl() failed for " + key) return fmt.Errorf("sysctl(vm.stats.vm.v_page_size) failed: %s", err)
}
if pages[key] == -2 {
return errors.New("sysctl() failed for " + key + ", wrong buffer size")
}
} }
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 { for k, v := range pages {
ch <- prometheus.MustNewConstMetric( ch <- prometheus.MustNewConstMetric(

Loading…
Cancel
Save