master
go 59 lines 1.64 KB
Raw
1 // SPDX-License-Identifier: GPL-3.0-or-later
2
3 package powervault
4
5 import (
6 "strconv"
7 "strings"
8 )
9
10 // collectSensorMetrics reports per-sensor readings by type.
11 // Uses cached discovery data — no API calls.
12 //
13 // Sensor types and value formats:
14 // - Temperature: "32 C" → parse integer before " C"
15 // - Voltage: "3.30 V" → parse float before " V", report in millivolts
16 // - Current: "1.25 A" → parse float before " A", report in milliamps
17 // - Charge Capacity: "95%" → parse integer before "%"
18 func (c *Collector) collectSensorMetrics() {
19 for _, s := range c.discovered.sensors {
20 id := s.DurableID
21
22 switch s.SensorType {
23 case "Temperature":
24 if v, ok := parseIntBefore(s.Value, " C"); ok {
25 c.mx.sensor.temperature.WithLabelValues(id).Observe(float64(v))
26 }
27 case "Voltage":
28 if v, ok := parseFloatBefore(s.Value, " V"); ok {
29 c.mx.sensor.voltage.WithLabelValues(id).Observe(v * 1000) // millivolts
30 }
31 case "Current Sensor":
32 if v, ok := parseFloatBefore(s.Value, " A"); ok {
33 c.mx.sensor.current.WithLabelValues(id).Observe(v * 1000) // milliamps
34 }
35 case "Charge Capacity":
36 if v, ok := parseIntBefore(s.Value, "%"); ok {
37 c.mx.sensor.chargeCapacity.WithLabelValues(id).Observe(float64(v))
38 }
39 }
40 }
41 }
42
43 func parseIntBefore(s, suffix string) (int64, bool) {
44 idx := strings.Index(s, suffix)
45 if idx < 0 {
46 idx = len(s)
47 }
48 v, err := strconv.ParseInt(strings.TrimSpace(s[:idx]), 10, 64)
49 return v, err == nil
50 }
51
52 func parseFloatBefore(s, suffix string) (float64, bool) {
53 idx := strings.Index(s, suffix)
54 if idx < 0 {
55 idx = len(s)
56 }
57 v, err := strconv.ParseFloat(strings.TrimSpace(s[:idx]), 64)
58 return v, err == nil
59 }