mirror of
https://github.com/prometheus/node_exporter.git
synced 2025-02-21 03:25:57 -08:00
Refactor meminfo_bsd.go to use sysctl_bsd.go (#501)
* Refactor meminfo_bsd.go to use sysctl_bsd.go * Fixed spelling.
This commit is contained in:
parent
309b313162
commit
72d8576185
|
@ -22,19 +22,6 @@ import (
|
||||||
"golang.org/x/sys/unix"
|
"golang.org/x/sys/unix"
|
||||||
)
|
)
|
||||||
|
|
||||||
type sysctlType uint8
|
|
||||||
|
|
||||||
const (
|
|
||||||
sysctlTypeUint32 sysctlType = iota
|
|
||||||
sysctlTypeUint64
|
|
||||||
)
|
|
||||||
|
|
||||||
type meminfoSysctl struct {
|
|
||||||
name string
|
|
||||||
dataType sysctlType
|
|
||||||
conversion func(uint64) uint64
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *meminfoCollector) getMemInfo() (map[string]float64, error) {
|
func (c *meminfoCollector) getMemInfo() (map[string]float64, error) {
|
||||||
info := make(map[string]float64)
|
info := make(map[string]float64)
|
||||||
|
|
||||||
|
@ -47,37 +34,24 @@ func (c *meminfoCollector) getMemInfo() (map[string]float64, error) {
|
||||||
return v * size
|
return v * size
|
||||||
}
|
}
|
||||||
|
|
||||||
for key, v := range map[string]meminfoSysctl{
|
for _, ctl := range []bsdSysctl{
|
||||||
"active_bytes": {"vm.stats.vm.v_active_count", sysctlTypeUint32, fromPage},
|
{name: "active_bytes", mib: "vm.stats.vm.v_active_count", conversion: fromPage},
|
||||||
"inactive_bytes": {"vm.stats.vm.v_inactive_count", sysctlTypeUint32, fromPage},
|
{name: "inactive_bytes", mib: "vm.stats.vm.v_inactive_count", conversion: fromPage},
|
||||||
"wired_bytes": {"vm.stats.vm.v_wire_count", sysctlTypeUint32, fromPage},
|
{name: "wired_bytes", mib: "vm.stats.vm.v_wire_count", conversion: fromPage},
|
||||||
"cache_bytes": {"vm.stats.vm.v_cache_count", sysctlTypeUint32, fromPage},
|
{name: "cache_bytes", mib: "vm.stats.vm.v_cache_count", conversion: fromPage},
|
||||||
"buffer_bytes": {"vfs.bufspace", sysctlTypeUint32, nil},
|
{name: "buffer_bytes", mib: "vfs.bufspace"},
|
||||||
"free_bytes": {"vm.stats.vm.v_free_count", sysctlTypeUint32, fromPage},
|
{name: "free_bytes", mib: "vm.stats.vm.v_free_count", conversion: fromPage},
|
||||||
"size_bytes": {"vm.stats.vm.v_page_count", sysctlTypeUint32, fromPage},
|
{name: "size_bytes", mib: "vm.stats.vm.v_page_count", conversion: fromPage},
|
||||||
"swap_in_bytes_total": {"vm.stats.vm.v_swappgsin", sysctlTypeUint32, fromPage},
|
{name: "swap_in_bytes_total", mib: "vm.stats.vm.v_swappgsin", conversion: fromPage},
|
||||||
"swap_out_bytes_total": {"vm.stats.vm.v_swappgsout", sysctlTypeUint32, fromPage},
|
{name: "swap_out_bytes_total", mib: "vm.stats.vm.v_swappgsout", conversion: fromPage},
|
||||||
"swap_size_bytes": {"vm.swap_total", sysctlTypeUint64, nil},
|
{name: "swap_size_bytes", mib: "vm.swap_total", dataType: bsdSysctlTypeUint64},
|
||||||
} {
|
} {
|
||||||
var tmp64 uint64
|
v, err := ctl.Value()
|
||||||
switch v.dataType {
|
|
||||||
case sysctlTypeUint32:
|
|
||||||
tmp32, err = unix.SysctlUint32(v.name)
|
|
||||||
tmp64 = uint64(tmp32)
|
|
||||||
case sysctlTypeUint64:
|
|
||||||
tmp64, err = unix.SysctlUint64(v.name)
|
|
||||||
}
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
if v.conversion != nil {
|
info[ctl.name] = v
|
||||||
// Convert to bytes.
|
|
||||||
info[key] = float64(v.conversion(tmp64))
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
info[key] = float64(tmp64)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return info, nil
|
return info, nil
|
||||||
|
|
|
@ -48,6 +48,9 @@ type bsdSysctl struct {
|
||||||
|
|
||||||
// Sysctl data-type
|
// Sysctl data-type
|
||||||
dataType bsdSysctlType
|
dataType bsdSysctlType
|
||||||
|
|
||||||
|
// Post-retrieval conversion hooks
|
||||||
|
conversion func(uint64) uint64
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b bsdSysctl) Value() (float64, error) {
|
func (b bsdSysctl) Value() (float64, error) {
|
||||||
|
@ -66,5 +69,9 @@ func (b bsdSysctl) Value() (float64, error) {
|
||||||
return 0, err
|
return 0, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if b.conversion != nil {
|
||||||
|
return float64(b.conversion(tmp64)), nil
|
||||||
|
}
|
||||||
|
|
||||||
return float64(tmp64), nil
|
return float64(tmp64), nil
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue