From 2678d68dcc9b97a840dc84929175a18b7c9bb3ee Mon Sep 17 00:00:00 2001 From: Derek Marcotte Date: Thu, 7 Jun 2018 09:01:25 -0400 Subject: [PATCH] Fix for #945, cpu temperature is signed. (#965) * Fix for #945, cpu temperature is signed. Added a type conversion to cpu temperature sysctl. Will still collect/report -1 when the value is -1, this is because it should be up to interpretation whether this is the correct value for the system or not. Some drivers will report -1 for cpu temperature. Other sensors will report "an input into the fan control algorithm", i.e. not the actual temperature, but how much fan it wants. Some people cool their machines with liquid nitrogen. Signed-off-by: Derek Marcotte <554b8425@razorfever.net> --- collector/cpu_freebsd.go | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/collector/cpu_freebsd.go b/collector/cpu_freebsd.go index e8d2fb98..dc3d6a11 100644 --- a/collector/cpu_freebsd.go +++ b/collector/cpu_freebsd.go @@ -138,7 +138,23 @@ func (c *statCollector) Update(ch chan<- prometheus.Metric) error { } continue } - ch <- c.temp.mustNewConstMetric(float64(temp-2732)/10, lcpu) + + // Temp is a signed integer in deci-degrees Kelvin. + // Cast uint32 to int32 and convert to float64 degrees Celsius. + // + // 2732 is used as the conversion constant for deci-degrees + // Kelvin, in multiple places in the kernel that feed into this + // sysctl, so we want to maintain consistency: + // + // sys/dev/amdtemp/amdtemp.c + // #define AMDTEMP_ZERO_C_TO_K 2732 + // + // sys/dev/acpica/acpi_thermal.c + // #define TZ_ZEROC 2732 + // + // sys/dev/coretemp/coretemp.c + // #define TZ_ZEROC 2732 + ch <- c.temp.mustNewConstMetric(float64(int32(temp)-2732)/10, lcpu) } return err }