From d827db8e1728c80ce295bb9a4c25b76c33e77264 Mon Sep 17 00:00:00 2001 From: Dominik Honnef Date: Thu, 5 Jan 2017 06:47:13 +0100 Subject: [PATCH] Better error handling when collecting CPU temps Log why we couldn't collect the temperature, and set metric to NaN if the CPU should support temperature collection but had an error. --- collector/cpu_freebsd.go | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/collector/cpu_freebsd.go b/collector/cpu_freebsd.go index 0ec0a1d1..adbdfcf7 100644 --- a/collector/cpu_freebsd.go +++ b/collector/cpu_freebsd.go @@ -17,10 +17,12 @@ package collector import ( "fmt" + "math" "strconv" "unsafe" "github.com/prometheus/client_golang/prometheus" + "github.com/prometheus/common/log" "golang.org/x/sys/unix" ) @@ -130,10 +132,18 @@ func (c *statCollector) Update(ch chan<- prometheus.Metric) (err error) { ch <- c.cpu.mustNewConstMetric(float64(t.idle), lcpu, "idle") temp, err := unix.SysctlUint32(fmt.Sprintf("dev.cpu.%d.temperature", cpu)) - if err == nil { - ftemp := float64(temp-2732) / 10 - ch <- c.temp.mustNewConstMetric(ftemp, lcpu) + 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 }