Repository: prometheus/procfs
Pull request #287 (2020-05-02 11:45:24)

parse temp as signed number
Signed-off-by: binjip978 <binjip978@gmail.com>


Patch:
diff --git a/fixtures.ttar b/fixtures.ttar
index 8ea9cff..e8509a4 100644
--- a/fixtures.ttar
+++ b/fixtures.ttar
@@ -3160,7 +3160,7 @@ Mode: 664
 # ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 Path: fixtures/sys/class/thermal/thermal_zone1/temp
 Lines: 1
-44000
+-44000
 Mode: 664
 # ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 Path: fixtures/sys/class/thermal/thermal_zone1/type
@@ -4166,17 +4166,6 @@ Lines: 1
 0
 Mode: 644
 # ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
-Path: fixtures/sys/fs/bcache/deaddd54-c735-46d5-868e-f331c5fd7c74/bdev0/writeback_rate_debug
-Lines: 7
-rate:           1.1M/sec
-dirty:          20.4G
-target:         20.4G
-proportional:   427.5k
-integral:       790.0k
-change:         321.5k/sec
-next io:        17ms
-Mode: 644
-# ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 Directory: fixtures/sys/fs/bcache/deaddd54-c735-46d5-868e-f331c5fd7c74/bdev0/stats_day
 Mode: 755
 # ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
@@ -4349,6 +4338,17 @@ Lines: 1
 0
 Mode: 644
 # ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
+Path: fixtures/sys/fs/bcache/deaddd54-c735-46d5-868e-f331c5fd7c74/bdev0/writeback_rate_debug
+Lines: 7
+rate:           1.1M/sec
+dirty:          20.4G
+target:         20.4G
+proportional:   427.5k
+integral:       790.0k
+change:         321.5k/sec
+next io:        17ms
+Mode: 644
+# ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 Path: fixtures/sys/fs/bcache/deaddd54-c735-46d5-868e-f331c5fd7c74/btree_cache_size
 Lines: 1
 0
diff --git a/internal/util/parse.go b/internal/util/parse.go
index 755591d..3f1b174 100644
--- a/internal/util/parse.go
+++ b/internal/util/parse.go
@@ -73,6 +73,15 @@ func ReadUintFromFile(path string) (uint64, error) {
 	return strconv.ParseUint(strings.TrimSpace(string(data)), 10, 64)
 }
 
+// ReadIntFromFile reads a file and attempts to parse a int64 from it.
+func ReadIntFromFromFile(path string) (int64, error) {
+	data, err := ioutil.ReadFile(path)
+	if err != nil {
+		return 0, err
+	}
+	return strconv.ParseInt(strings.TrimSpace(string(data)), 10, 64)
+}
+
 // ParseBool parses a string into a boolean pointer.
 func ParseBool(b string) *bool {
 	var truth bool
diff --git a/sysfs/class_thermal.go b/sysfs/class_thermal.go
index cfe11ad..c20639f 100644
--- a/sysfs/class_thermal.go
+++ b/sysfs/class_thermal.go
@@ -29,7 +29,7 @@ import (
 type ClassThermalZoneStats struct {
 	Name    string  // The name of the zone from the directory structure.
 	Type    string  // The type of thermal zone.
-	Temp    uint64  // Temperature in millidegree Celsius.
+	Temp    int64   // Temperature in millidegree Celsius.
 	Policy  string  // One of the various thermal governors used for a particular zone.
 	Mode    *bool   // Optional: One of the predefined values in [enabled, disabled].
 	Passive *uint64 // Optional: millidegrees Celsius. (0 for disabled, > 1000 for enabled+value)
@@ -67,7 +67,7 @@ func parseClassThermalZone(zone string) (ClassThermalZoneStats, error) {
 	if err != nil {
 		return ClassThermalZoneStats{}, err
 	}
-	zoneTemp, err := util.ReadUintFromFile(filepath.Join(zone, "temp"))
+	zoneTemp, err := util.ReadIntFromFromFile(filepath.Join(zone, "temp"))
 	if err != nil {
 		return ClassThermalZoneStats{}, err
 	}


Test patch:
diff --git a/sysfs/class_thermal_test.go b/sysfs/class_thermal_test.go
index 3f86967..94af8ae 100644
--- a/sysfs/class_thermal_test.go
+++ b/sysfs/class_thermal_test.go
@@ -49,7 +49,7 @@ func TestClassThermalZoneStats(t *testing.T) {
 			Name:    "1",
 			Type:    "acpitz",
 			Policy:  "step_wise",
-			Temp:    44000,
+			Temp:    -44000,
 			Mode:    enabled,
 			Passive: &passive,
 		},