Skip to content

Commit

Permalink
Merge pull request shirou#485 from cshunger/linux-temperature
Browse files Browse the repository at this point in the history
Linux temperature
  • Loading branch information
shirou authored Jan 30, 2018
2 parents 4ac6b5b + 29f8dfa commit c432be2
Showing 1 changed file with 29 additions and 5 deletions.
34 changes: 29 additions & 5 deletions host/host_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -591,22 +591,46 @@ func SensorsTemperaturesWithContext(ctx context.Context) ([]TemperatureStat, err
}
}

for _, match := range files {
match = strings.Split(match, "_")[0]
name, err := ioutil.ReadFile(filepath.Join(filepath.Dir(match), "name"))
// example directory
// device/ temp1_crit_alarm temp2_crit_alarm temp3_crit_alarm temp4_crit_alarm temp5_crit_alarm temp6_crit_alarm temp7_crit_alarm
// name temp1_input temp2_input temp3_input temp4_input temp5_input temp6_input temp7_input
// power/ temp1_label temp2_label temp3_label temp4_label temp5_label temp6_label temp7_label
// subsystem/ temp1_max temp2_max temp3_max temp4_max temp5_max temp6_max temp7_max
// temp1_crit temp2_crit temp3_crit temp4_crit temp5_crit temp6_crit temp7_crit uevent
for _, file := range files {
filename := strings.Split(filepath.Base(file), "_")
if filename[1] == "label" {
// Do not try to read the temperature of the label file
continue
}

// Get the label of the temperature you are reading
var label string
c, _ := ioutil.ReadFile(filepath.Join(filepath.Dir(file), filename[0]+"_label"))
if c != nil {
//format the label from "Core 0" to "core0_"
label = fmt.Sprintf("%s_", strings.Join(strings.Split(strings.TrimSpace(strings.ToLower(string(c))), " "), ""))
}

// Get the name of the tempearture you are reading
name, err := ioutil.ReadFile(filepath.Join(filepath.Dir(file), "name"))
if err != nil {
return temperatures, err
}
current, err := ioutil.ReadFile(match + "_input")

// Get the temperature reading
current, err := ioutil.ReadFile(file)
if err != nil {
return temperatures, err
}
temperature, err := strconv.ParseFloat(strings.TrimSpace(string(current)), 64)
if err != nil {
continue
}

tempName := strings.TrimSpace(strings.ToLower(string(strings.Join(filename[1:], ""))))
temperatures = append(temperatures, TemperatureStat{
SensorKey: strings.TrimSpace(string(name)),
SensorKey: fmt.Sprintf("%s_%s%s", strings.TrimSpace(string(name)), label, tempName),
Temperature: temperature / 1000.0,
})
}
Expand Down

0 comments on commit c432be2

Please sign in to comment.