2017-02-28 21:23:10 +00:00
|
|
|
// Copyright 2017 The Prometheus Authors
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2018-04-15 04:42:02 +00:00
|
|
|
// +build freebsd dragonfly openbsd netbsd darwin
|
2017-02-28 21:23:10 +00:00
|
|
|
// +build !nomeminfo
|
|
|
|
|
|
|
|
package collector
|
|
|
|
|
|
|
|
import (
|
2017-04-11 10:04:14 +00:00
|
|
|
"fmt"
|
2018-02-01 17:43:40 +00:00
|
|
|
"syscall"
|
|
|
|
"unsafe"
|
|
|
|
|
2017-02-28 21:23:10 +00:00
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
|
|
|
"golang.org/x/sys/unix"
|
|
|
|
)
|
|
|
|
|
2017-04-11 10:04:14 +00:00
|
|
|
// #include <sys/types.h>
|
|
|
|
import "C"
|
|
|
|
|
2017-02-28 21:23:10 +00:00
|
|
|
type bsdSysctlType uint8
|
|
|
|
|
|
|
|
// BSD-specific sysctl value types. There is an impedience mismatch between
|
|
|
|
// native C types, e.g. int vs long, and the golang unix.Sysctl variables
|
|
|
|
const (
|
|
|
|
// Default to uint32.
|
|
|
|
bsdSysctlTypeUint32 bsdSysctlType = iota
|
|
|
|
bsdSysctlTypeUint64
|
2017-04-11 10:04:14 +00:00
|
|
|
bsdSysctlTypeStructTimeval
|
2017-10-25 18:55:22 +00:00
|
|
|
bsdSysctlTypeCLong
|
2017-02-28 21:23:10 +00:00
|
|
|
)
|
|
|
|
|
2017-10-25 18:55:22 +00:00
|
|
|
// Contains all the info needed to map a single bsd-sysctl to a prometheus
|
|
|
|
// value.
|
2017-02-28 21:23:10 +00:00
|
|
|
type bsdSysctl struct {
|
|
|
|
// Prometheus name
|
|
|
|
name string
|
|
|
|
|
|
|
|
// Simple prometheus description
|
|
|
|
description string
|
|
|
|
|
|
|
|
// Prometheus type
|
|
|
|
valueType prometheus.ValueType
|
|
|
|
|
|
|
|
// Sysctl name
|
|
|
|
mib string
|
|
|
|
|
|
|
|
// Sysctl data-type
|
|
|
|
dataType bsdSysctlType
|
2017-03-08 01:54:28 +00:00
|
|
|
|
|
|
|
// Post-retrieval conversion hooks
|
2017-04-11 10:04:14 +00:00
|
|
|
conversion func(float64) float64
|
2017-02-28 21:23:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (b bsdSysctl) Value() (float64, error) {
|
|
|
|
var tmp32 uint32
|
|
|
|
var tmp64 uint64
|
2017-04-11 10:04:14 +00:00
|
|
|
var tmpf64 float64
|
2017-02-28 21:23:10 +00:00
|
|
|
var err error
|
|
|
|
|
|
|
|
switch b.dataType {
|
|
|
|
case bsdSysctlTypeUint32:
|
|
|
|
tmp32, err = unix.SysctlUint32(b.mib)
|
2017-04-11 10:04:14 +00:00
|
|
|
tmpf64 = float64(tmp32)
|
2017-02-28 21:23:10 +00:00
|
|
|
case bsdSysctlTypeUint64:
|
|
|
|
tmp64, err = unix.SysctlUint64(b.mib)
|
2017-04-11 10:04:14 +00:00
|
|
|
tmpf64 = float64(tmp64)
|
|
|
|
case bsdSysctlTypeStructTimeval:
|
2017-10-25 18:55:22 +00:00
|
|
|
tmpf64, err = b.getStructTimeval()
|
|
|
|
case bsdSysctlTypeCLong:
|
|
|
|
tmpf64, err = b.getCLong()
|
2017-02-28 21:23:10 +00:00
|
|
|
}
|
2017-04-11 10:04:14 +00:00
|
|
|
|
2017-02-28 21:23:10 +00:00
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
2017-03-08 01:54:28 +00:00
|
|
|
if b.conversion != nil {
|
2017-04-11 10:04:14 +00:00
|
|
|
return b.conversion(tmpf64), nil
|
2017-03-08 01:54:28 +00:00
|
|
|
}
|
|
|
|
|
2017-04-11 10:04:14 +00:00
|
|
|
return tmpf64, nil
|
2017-02-28 21:23:10 +00:00
|
|
|
}
|
2017-10-25 18:55:22 +00:00
|
|
|
|
|
|
|
func (b bsdSysctl) getStructTimeval() (float64, error) {
|
|
|
|
raw, err := unix.SysctlRaw(b.mib)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
2018-02-01 17:43:40 +00:00
|
|
|
if len(raw) != int(unsafe.Sizeof(syscall.Timeval{})) {
|
|
|
|
// Shouldn't get here.
|
2017-10-25 18:55:22 +00:00
|
|
|
return 0, fmt.Errorf(
|
|
|
|
"length of bytes received from sysctl (%d) does not match expected bytes (%d)",
|
|
|
|
len(raw),
|
2018-02-01 17:43:40 +00:00
|
|
|
unsafe.Sizeof(syscall.Timeval{}),
|
2017-10-25 18:55:22 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2018-02-01 17:43:40 +00:00
|
|
|
tv := *(*syscall.Timeval)(unsafe.Pointer(&raw[0]))
|
2017-10-25 18:55:22 +00:00
|
|
|
|
|
|
|
// This conversion maintains the usec precision. Using the time
|
|
|
|
// package did not.
|
2018-02-01 17:43:40 +00:00
|
|
|
return (float64(tv.Sec) + (float64(tv.Usec) / float64(1000*1000))), nil
|
2017-10-25 18:55:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (b bsdSysctl) getCLong() (float64, error) {
|
|
|
|
raw, err := unix.SysctlRaw(b.mib)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(raw) == C.sizeof_long {
|
|
|
|
return float64(*(*C.long)(unsafe.Pointer(&raw[0]))), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(raw) == C.sizeof_int {
|
|
|
|
// This is valid for at least vfs.bufspace, and the default
|
|
|
|
// long handler - which can clamp longs to 32-bits:
|
|
|
|
// https://github.com/freebsd/freebsd/blob/releng/10.3/sys/kern/vfs_bio.c#L338
|
|
|
|
// https://github.com/freebsd/freebsd/blob/releng/10.3/sys/kern/kern_sysctl.c#L1062
|
|
|
|
return float64(*(*C.int)(unsafe.Pointer(&raw[0]))), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0, fmt.Errorf(
|
|
|
|
"length of bytes received from sysctl (%d) does not match expected bytes (long: %d), (int: %d)",
|
|
|
|
len(raw),
|
|
|
|
C.sizeof_long,
|
|
|
|
C.sizeof_int,
|
|
|
|
)
|
|
|
|
|
|
|
|
}
|