mirror of
https://github.com/prometheus/node_exporter.git
synced 2024-11-23 12:30:46 +01:00
Use SysctlTimeval for boottime collector on BSD
Use SysctlTimeval from the golang.org/x/sys/unix package to simplify the implementation of the boottime collector for the BSDs and allows to build it without cgo. Tested on macOS 11.6, FreeBSD 13 and OpenBSD 7. Signed-off-by: Tobias Klauser <tklauser@distanz.ch>
This commit is contained in:
parent
85e20238e7
commit
58ab0144af
@ -11,8 +11,8 @@
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
//go:build (freebsd || dragonfly || (openbsd && !amd64) || netbsd || darwin) && !noboottime
|
||||
// +build freebsd dragonfly openbsd,!amd64 netbsd darwin
|
||||
//go:build (freebsd || dragonfly || openbsd || netbsd || darwin) && !noboottime
|
||||
// +build freebsd dragonfly openbsd netbsd darwin
|
||||
// +build !noboottime
|
||||
|
||||
package collector
|
||||
@ -20,11 +20,11 @@ package collector
|
||||
import (
|
||||
"github.com/go-kit/log"
|
||||
"github.com/prometheus/client_golang/prometheus"
|
||||
"golang.org/x/sys/unix"
|
||||
)
|
||||
|
||||
type bootTimeCollector struct {
|
||||
boottime bsdSysctl
|
||||
logger log.Logger
|
||||
logger log.Logger
|
||||
}
|
||||
|
||||
func init() {
|
||||
@ -34,27 +34,25 @@ func init() {
|
||||
// newBootTimeCollector returns a new Collector exposing system boot time on BSD systems.
|
||||
func newBootTimeCollector(logger log.Logger) (Collector, error) {
|
||||
return &bootTimeCollector{
|
||||
boottime: bsdSysctl{
|
||||
name: "boot_time_seconds",
|
||||
description: "Unix time of last boot, including microseconds.",
|
||||
mib: "kern.boottime",
|
||||
dataType: bsdSysctlTypeStructTimeval,
|
||||
},
|
||||
logger: logger,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// Update pushes boot time onto ch
|
||||
func (c *bootTimeCollector) Update(ch chan<- prometheus.Metric) error {
|
||||
v, err := c.boottime.Value()
|
||||
tv, err := unix.SysctlTimeval("kern.boottime")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// This conversion maintains the usec precision. Using the time
|
||||
// package did not.
|
||||
v := float64(tv.Sec) + (float64(tv.Usec) / float64(1000*1000))
|
||||
|
||||
ch <- prometheus.MustNewConstMetric(
|
||||
prometheus.NewDesc(
|
||||
prometheus.BuildFQName(namespace, "", c.boottime.name),
|
||||
c.boottime.description,
|
||||
prometheus.BuildFQName(namespace, "", "boot_time_seconds"),
|
||||
"Unix time of last boot, including microseconds.",
|
||||
nil, nil,
|
||||
), prometheus.GaugeValue, v)
|
||||
|
||||
|
@ -1,62 +0,0 @@
|
||||
// Copyright 2020 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.
|
||||
|
||||
//go:build !noboottime
|
||||
// +build !noboottime
|
||||
|
||||
package collector
|
||||
|
||||
import (
|
||||
"github.com/go-kit/log"
|
||||
"github.com/prometheus/client_golang/prometheus"
|
||||
"golang.org/x/sys/unix"
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
type bootTimeCollector struct {
|
||||
name, description string
|
||||
logger log.Logger
|
||||
}
|
||||
|
||||
func init() {
|
||||
registerCollector("boottime", defaultEnabled, newBootTimeCollector)
|
||||
}
|
||||
|
||||
// newBootTimeCollector returns a new Collector exposing system boot time on BSD systems.
|
||||
func newBootTimeCollector(logger log.Logger) (Collector, error) {
|
||||
return &bootTimeCollector{
|
||||
name: "boot_time_seconds",
|
||||
description: "Unix time of last boot, including microseconds.",
|
||||
logger: logger,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// Update pushes boot time onto ch
|
||||
func (c *bootTimeCollector) Update(ch chan<- prometheus.Metric) error {
|
||||
raw, err := unix.SysctlRaw("kern.boottime")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
tv := *(*unix.Timeval)(unsafe.Pointer(&raw[0]))
|
||||
v := (float64(tv.Sec) + (float64(tv.Usec) / float64(1000*1000)))
|
||||
|
||||
ch <- prometheus.MustNewConstMetric(
|
||||
prometheus.NewDesc(
|
||||
prometheus.BuildFQName(namespace, "", c.name),
|
||||
c.description,
|
||||
nil, nil,
|
||||
), prometheus.GaugeValue, v)
|
||||
|
||||
return nil
|
||||
}
|
@ -36,7 +36,6 @@ const (
|
||||
// Default to uint32.
|
||||
bsdSysctlTypeUint32 bsdSysctlType = iota
|
||||
bsdSysctlTypeUint64
|
||||
bsdSysctlTypeStructTimeval
|
||||
bsdSysctlTypeCLong
|
||||
)
|
||||
|
||||
@ -75,8 +74,6 @@ func (b bsdSysctl) Value() (float64, error) {
|
||||
case bsdSysctlTypeUint64:
|
||||
tmp64, err = unix.SysctlUint64(b.mib)
|
||||
tmpf64 = float64(tmp64)
|
||||
case bsdSysctlTypeStructTimeval:
|
||||
tmpf64, err = b.getStructTimeval()
|
||||
case bsdSysctlTypeCLong:
|
||||
tmpf64, err = b.getCLong()
|
||||
}
|
||||
@ -92,28 +89,6 @@ func (b bsdSysctl) Value() (float64, error) {
|
||||
return tmpf64, nil
|
||||
}
|
||||
|
||||
func (b bsdSysctl) getStructTimeval() (float64, error) {
|
||||
raw, err := unix.SysctlRaw(b.mib)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
if len(raw) != int(unsafe.Sizeof(unix.Timeval{})) {
|
||||
// Shouldn't get here.
|
||||
return 0, fmt.Errorf(
|
||||
"length of bytes received from sysctl (%d) does not match expected bytes (%d)",
|
||||
len(raw),
|
||||
unsafe.Sizeof(unix.Timeval{}),
|
||||
)
|
||||
}
|
||||
|
||||
tv := *(*unix.Timeval)(unsafe.Pointer(&raw[0]))
|
||||
|
||||
// This conversion maintains the usec precision. Using the time
|
||||
// package did not.
|
||||
return (float64(tv.Sec) + (float64(tv.Usec) / float64(1000*1000))), nil
|
||||
}
|
||||
|
||||
func (b bsdSysctl) getCLong() (float64, error) {
|
||||
raw, err := unix.SysctlRaw(b.mib)
|
||||
if err != nil {
|
||||
|
Loading…
Reference in New Issue
Block a user