func()

in metric/system/hwmon/hwmon.go [115:163]


func (s Sensor) Fetch(path string) (SensorMetrics, error) {
	// All the different sensor types have a few common fields. Fetch those first.
	// See https://www.kernel.org/doc/Documentation/hwmon/sysfs-interface
	labelName := s.getName("label")
	label, err := stringStrip(labelName, path)

	//Some sensors don't have a label, make our own
	if os.IsNotExist(err) {
		label = fmt.Sprintf("%s_%d", s.DevType.fileKey, s.SensorNum)
	} else if err != nil {
		return SensorMetrics{}, fmt.Errorf("error fetching label for %s in %s: %w", labelName, path, err)
	}

	// Not sure if we want this to be an error, since a lot of OSes, particularly stuff running inside a VM,
	// will just have this invalid hwmon entries with labels but no values. We may want this to be a log-level error instead.
	inputName := s.getName("input")
	input, err := getValueForSensor(inputName, path, s.DevType)
	if os.IsNotExist(err) {
		return SensorMetrics{}, ErrNoMetric
	} else if err != nil {
		return SensorMetrics{}, fmt.Errorf("error fetching input for %s in %s: %w", inputName, path, err)
	}

	sensorData := SensorMetrics{
		Label:      label,
		sensorType: s.DevType,
		Value:      input,
	}

	// Other special metrics for some sensors
	// We don't want to bulk fetch these with a glob or something, we'll just end up picking up a bunch of garbage
	critName := s.getName("crit")
	critVal, _ := getValueForSensor(critName, path, s.DevType)
	sensorData.Critical = critVal

	maxName := s.getName("max")
	maxVal, _ := getValueForSensor(maxName, path, s.DevType)
	sensorData.Max = maxVal

	lowestName := s.getName("lowest")
	lowestVal, _ := getValueForSensor(lowestName, path, s.DevType)
	sensorData.Lowest = lowestVal

	avgName := s.getName("average")
	avgVal, _ := getValueForSensor(avgName, path, s.DevType)
	sensorData.Average = avgVal

	return sensorData, nil
}