Return NaN for invalid temperature readings

This commit is contained in:
Søren Rasmussen 2022-08-03 15:27:27 +02:00
parent 78d6841bd1
commit 15f6d277a9

View file

@ -184,7 +184,15 @@ func readSensors(hub *sentry.Hub) bool {
WithLabelValues(sensor.Name). WithLabelValues(sensor.Name).
Set(tw) Set(tw)
metrics.TemperatureSensorReadingStatus.WithLabelValues(sensor.Name, "ok").Inc() var statusStr string
if err == nil {
statusStr = "ok"
} else {
statusStr = "fail"
}
metrics.TemperatureSensorReadingStatus.
WithLabelValues(sensor.Name, statusStr).
Inc()
} }
// Throw away reading if any sensor failed // Throw away reading if any sensor failed
@ -235,28 +243,28 @@ func read(sensor string) (int64, error) {
path := filepath.Join("/sys/bus/w1/devices", sensor, "w1_slave") path := filepath.Join("/sys/bus/w1/devices", sensor, "w1_slave")
data, err := ioutil.ReadFile(path) data, err := ioutil.ReadFile(path)
if err != nil { if err != nil {
return 0.0, err return NaN, err
} }
raw := string(data) raw := string(data)
if !strings.Contains(raw, " YES") { if !strings.Contains(raw, " YES") {
return 0.0, fmt.Errorf("%v: %w: checksum failed [%v]", sensor, ErrReadSensor, raw) return NaN, fmt.Errorf("%v: %w: checksum failed [%v]", sensor, ErrReadSensor, raw)
} }
i := strings.LastIndex(raw, "t=") i := strings.LastIndex(raw, "t=")
if i == -1 { if i == -1 {
return 0.0, fmt.Errorf("%v: %w: t= not found in [%v]", sensor, ErrReadSensor, raw) return NaN, fmt.Errorf("%v: %w: t= not found in [%v]", sensor, ErrReadSensor, raw)
} }
c, err := strconv.ParseInt(raw[i+2:len(raw)-1], 10, 64) c, err := strconv.ParseInt(raw[i+2:len(raw)-1], 10, 64)
if err != nil { if err != nil {
return 0.0, err return NaN, err
} }
// Ignore magic value -128 // Ignore magic value -128
if c == -128000 { if c == -128000 {
return 0.0, fmt.Errorf("%v: magic value -128 detected", sensor) return NaN, fmt.Errorf("%v: magic value -128 detected", sensor)
} }
return c, nil return c, nil