Skip to content

Commit

Permalink
feat(linux/hwmon): ✨ expose the sysfs path for the hwmon sensor
Browse files Browse the repository at this point in the history
  • Loading branch information
joshuar committed Apr 27, 2024
1 parent ae5aa9e commit 4a198fa
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 7 deletions.
14 changes: 8 additions & 6 deletions internal/linux/system/hwmon.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,19 @@ type hwSensor struct {
hwType string
name string
id string
path string
linux.Sensor
}

func (s *hwSensor) asBool(h *hwmon.Sensor) {
if v, err := strconv.ParseBool(fmt.Sprint(int(h.Value()))); err != nil {
s.Value = false
} else {
s.Value = v
}
if s.Value.(bool) {
// we don't care if the value cannot be parsed, treat it as false
value, _ := strconv.ParseBool(fmt.Sprint(int(h.Value())))
if value {
s.IconString = "mdi:alarm-light"
} else {
s.IconString = "mdi:alarm-light-off"
}
s.Value = value
s.IsBinary = true
}

Expand Down Expand Up @@ -68,11 +67,13 @@ func (s *hwSensor) Attributes() any {
NativeUnit string `json:"native_unit_of_measurement,omitempty"`
DataSource string `json:"Data Source"`
SensorType string `json:"Sensor Type"`
HWMonPath string `json:"SysFS Path"`
}{
NativeUnit: s.UnitsString,
DataSource: linux.DataSrcSysfs,
SensorType: s.hwType,
Attributes: s.ExtraAttrs,
HWMonPath: s.path,
}
}

Expand All @@ -81,6 +82,7 @@ func newHWSensor(s *hwmon.Sensor) *hwSensor {
name: s.Name(),
id: s.ID(),
hwType: s.SensorType.String(),
path: s.SysFSPath,
ExtraAttrs: make(map[string]float64),
}
hw.IsDiagnostic = true
Expand Down
4 changes: 3 additions & 1 deletion pkg/linux/hwmon/hwmon.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ type Sensor struct {
label string
id string
units string
SysFSPath string
Attributes []Attribute
scaleFactor float64
value float64
Expand Down Expand Up @@ -160,7 +161,7 @@ func (s *Sensor) Units() string {
// String will format the sensor name and value as a pretty string.
func (s *Sensor) String() string {
var b strings.Builder
fmt.Fprintf(&b, "%s: %.3f %s [%s] (id: %s)", s.Name(), s.Value(), s.Units(), s.SensorType, s.ID())
fmt.Fprintf(&b, "%s: %.3f %s [%s] (id: %s, path: %s)", s.Name(), s.Value(), s.Units(), s.SensorType, s.ID(), s.SysFSPath)
for i, a := range s.Attributes {
if i == 0 {
fmt.Fprintf(&b, " (")
Expand Down Expand Up @@ -335,6 +336,7 @@ func getSensors(path string) ([]*Sensor, error) {
deviceModel: deviceModel,
id: sensorFile.sensorType,
SensorType: t,
SysFSPath: path,
scaleFactor: sf,
units: u,
}
Expand Down

0 comments on commit 4a198fa

Please sign in to comment.