Merge pull request #397 from dominikh/freebsd-cpu

Collect CPU temperatures on FreeBSD
This commit is contained in:
Johannes 'fish' Ziemke 2017-01-05 17:32:48 +01:00 committed by GitHub
commit 3e266e28b9
1 changed files with 30 additions and 6 deletions

View File

@ -16,10 +16,13 @@
package collector
import (
"fmt"
"math"
"strconv"
"unsafe"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/common/log"
"golang.org/x/sys/unix"
)
@ -78,7 +81,8 @@ func getCPUTimes() ([]cputime, error) {
}
type statCollector struct {
cpu typedDesc
cpu typedDesc
temp typedDesc
}
func init() {
@ -94,6 +98,11 @@ func NewStatCollector() (Collector, error) {
"Seconds the CPU spent in each mode.",
[]string{"cpu", "mode"}, nil,
), prometheus.CounterValue},
temp: typedDesc{prometheus.NewDesc(
prometheus.BuildFQName(Namespace, "cpu", "temperature_celsius"),
"CPU temperature",
[]string{"cpu"}, nil,
), prometheus.GaugeValue},
}, nil
}
@ -115,11 +124,26 @@ func (c *statCollector) Update(ch chan<- prometheus.Metric) (err error) {
return err
}
for cpu, t := range cpuTimes {
ch <- c.cpu.mustNewConstMetric(float64(t.user), strconv.Itoa(cpu), "user")
ch <- c.cpu.mustNewConstMetric(float64(t.nice), strconv.Itoa(cpu), "nice")
ch <- c.cpu.mustNewConstMetric(float64(t.sys), strconv.Itoa(cpu), "system")
ch <- c.cpu.mustNewConstMetric(float64(t.intr), strconv.Itoa(cpu), "interrupt")
ch <- c.cpu.mustNewConstMetric(float64(t.idle), strconv.Itoa(cpu), "idle")
lcpu := strconv.Itoa(cpu)
ch <- c.cpu.mustNewConstMetric(float64(t.user), lcpu, "user")
ch <- c.cpu.mustNewConstMetric(float64(t.nice), lcpu, "nice")
ch <- c.cpu.mustNewConstMetric(float64(t.sys), lcpu, "system")
ch <- c.cpu.mustNewConstMetric(float64(t.intr), lcpu, "interrupt")
ch <- c.cpu.mustNewConstMetric(float64(t.idle), lcpu, "idle")
temp, err := unix.SysctlUint32(fmt.Sprintf("dev.cpu.%d.temperature", cpu))
if err != nil {
if err == unix.ENOENT {
// No temperature information for this CPU
log.Debugf("no temperature information for CPU %d", cpu)
} else {
// Unexpected error
ch <- c.temp.mustNewConstMetric(math.NaN(), lcpu)
log.Errorf("failed to query CPU temperature for CPU %d: %s", cpu, err)
}
continue
}
ch <- c.temp.mustNewConstMetric(float64(temp-2732)/10, lcpu)
}
return err
}