func findSensorsInPath()

in metric/system/hwmon/hwmon.go [171:216]


func findSensorsInPath(path string, sensorRegex *regexp.Regexp) ([]Sensor, error) {
	sensorList := []Sensor{}

	//This is just to track what sensors we've found, as hwmon just dumps everything into one directory
	foundMap := map[string]bool{}

	//iterate over the files in the hwmon path
	files, err := os.ReadDir(path)
	if err != nil {
		return nil, fmt.Errorf("error reading from hwmon path %s: %w", path, err)
	}
	for _, file := range files {
		if file.IsDir() {
			continue
		}
		// The actual hwmon sensor files are formatted as typeNUM_filetype
		if !strings.Contains(file.Name(), "_") {
			continue
		}
		prefixes := sensorRegex.FindStringSubmatch(file.Name())
		//There should be three values here: the total match, and the two submatches for type and number
		//These directories have a lot of stuff in them, so this isn't an error.
		if len(prefixes) < 3 {
			continue
		}
		_, found := foundMap[prefixes[0]]
		if found {
			continue
		}

		st, found := getSensorType(prefixes[1])
		// Skip sensor types that we currently don't support
		if !found {
			continue
		}
		sensorNum, err := strconv.ParseInt(prefixes[2], 10, 64)
		if err != nil {
			return nil, fmt.Errorf("error parsing int %s: %w", prefixes[2], err)
		}
		foundMap[prefixes[0]] = true
		sensorList = append(sensorList, Sensor{DevType: st, SensorNum: sensorNum})

	}

	return sensorList, nil
}