From 75462bf4fea23f0e27a46c35ae3a28666749e482 Mon Sep 17 00:00:00 2001 From: Richard Kojedzinszky Date: Sun, 4 Aug 2019 12:56:36 +0200 Subject: [PATCH] Scrape thermal_zone temperatures (#1425) * Scrape thermal_zone temperatures Signed-off-by: Richard Kojedzinszky --- CHANGELOG.md | 1 + collector/fixtures/e2e-64k-page-output.txt | 4 ++ collector/fixtures/e2e-output.txt | 4 ++ collector/fixtures/sys.ttar | 30 ++++++++++ collector/thermal_zone_linux.go | 68 ++++++++++++++++++++++ end-to-end-test.sh | 1 + 6 files changed, 108 insertions(+) create mode 100644 collector/thermal_zone_linux.go diff --git a/CHANGELOG.md b/CHANGELOG.md index 0c1893f6..a1312669 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -22,6 +22,7 @@ * [BUGFIX] Renamed label `state` to `name` on `node_systemd_service_restart_total`. #1393 * [BUGFIX] Fix netdev nil reference on Darwin #1414 * [BUGFIX] Strip path.rootfs from mountpoint labels #1421 +* [FEATURE] Add new thermal_zone collector #1425 ## 0.18.1 / 2019-06-04 diff --git a/collector/fixtures/e2e-64k-page-output.txt b/collector/fixtures/e2e-64k-page-output.txt index 5e83c60a..b7e56edd 100644 --- a/collector/fixtures/e2e-64k-page-output.txt +++ b/collector/fixtures/e2e-64k-page-output.txt @@ -2488,6 +2488,7 @@ node_scrape_collector_success{collector="schedstat"} 1 node_scrape_collector_success{collector="sockstat"} 1 node_scrape_collector_success{collector="stat"} 1 node_scrape_collector_success{collector="textfile"} 1 +node_scrape_collector_success{collector="thermal_zone"} 1 node_scrape_collector_success{collector="vmstat"} 1 node_scrape_collector_success{collector="wifi"} 1 node_scrape_collector_success{collector="xfs"} 1 @@ -2539,6 +2540,9 @@ node_sockstat_sockets_used 229 # HELP node_textfile_scrape_error 1 if there was an error opening or reading a file, 0 otherwise # TYPE node_textfile_scrape_error gauge node_textfile_scrape_error 0 +# HELP node_thermal_zone_temp Zone temperature in Celsius +# TYPE node_thermal_zone_temp gauge +node_thermal_zone_temp{type="cpu-thermal",zone="0"} 12.376 # HELP node_vmstat_oom_kill /proc/vmstat information field oom_kill. # TYPE node_vmstat_oom_kill untyped node_vmstat_oom_kill 0 diff --git a/collector/fixtures/e2e-output.txt b/collector/fixtures/e2e-output.txt index 258e7652..1d392d89 100644 --- a/collector/fixtures/e2e-output.txt +++ b/collector/fixtures/e2e-output.txt @@ -2488,6 +2488,7 @@ node_scrape_collector_success{collector="schedstat"} 1 node_scrape_collector_success{collector="sockstat"} 1 node_scrape_collector_success{collector="stat"} 1 node_scrape_collector_success{collector="textfile"} 1 +node_scrape_collector_success{collector="thermal_zone"} 1 node_scrape_collector_success{collector="vmstat"} 1 node_scrape_collector_success{collector="wifi"} 1 node_scrape_collector_success{collector="xfs"} 1 @@ -2539,6 +2540,9 @@ node_sockstat_sockets_used 229 # HELP node_textfile_scrape_error 1 if there was an error opening or reading a file, 0 otherwise # TYPE node_textfile_scrape_error gauge node_textfile_scrape_error 0 +# HELP node_thermal_zone_temp Zone temperature in Celsius +# TYPE node_thermal_zone_temp gauge +node_thermal_zone_temp{type="cpu-thermal",zone="0"} 12.376 # HELP node_vmstat_oom_kill /proc/vmstat information field oom_kill. # TYPE node_vmstat_oom_kill untyped node_vmstat_oom_kill 0 diff --git a/collector/fixtures/sys.ttar b/collector/fixtures/sys.ttar index ee30c814..2880390e 100644 --- a/collector/fixtures/sys.ttar +++ b/collector/fixtures/sys.ttar @@ -920,6 +920,12 @@ Lines: 1 1 Mode: 644 # ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +Directory: sys/class/thermal +Mode: 755 +# ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +Path: sys/class/thermal/thermal_zone0 +SymlinkTo: ../../devices/virtual/thermal/thermal_zone0 +# ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Directory: sys/devices Mode: 755 # ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - @@ -2460,6 +2466,30 @@ local_node 26719046550 other_node 9860526920 Mode: 644 # ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +Directory: sys/devices/virtual +Mode: 755 +# ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +Directory: sys/devices/virtual/thermal +Mode: 755 +# ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +Directory: sys/devices/virtual/thermal/thermal_zone0 +Mode: 755 +# ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +Path: sys/devices/virtual/thermal/thermal_zone0/policy +Lines: 1 +step_wise +Mode: 644 +# ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +Path: sys/devices/virtual/thermal/thermal_zone0/temp +Lines: 1 +12376 +Mode: 644 +# ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +Path: sys/devices/virtual/thermal/thermal_zone0/type +Lines: 1 +cpu-thermal +Mode: 644 +# ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Directory: sys/fs Mode: 755 # ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/collector/thermal_zone_linux.go b/collector/thermal_zone_linux.go new file mode 100644 index 00000000..16fe61c6 --- /dev/null +++ b/collector/thermal_zone_linux.go @@ -0,0 +1,68 @@ +// Copyright 2019 The Prometheus Authors +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +// +build !nothermalzone + +package collector + +import ( + "fmt" + + "github.com/prometheus/client_golang/prometheus" + "github.com/prometheus/procfs/sysfs" +) + +type thermalZoneCollector struct { + fs sysfs.FS + zoneTemp *prometheus.Desc +} + +func init() { + registerCollector("thermal_zone", defaultEnabled, NewThermalZoneCollector) +} + +// NewThermalZoneCollector returns a new Collector exposing kernel/system statistics. +func NewThermalZoneCollector() (Collector, error) { + fs, err := sysfs.NewFS(*sysPath) + if err != nil { + return nil, fmt.Errorf("failed to open sysfs: %v", err) + } + + return &thermalZoneCollector{ + fs: fs, + zoneTemp: prometheus.NewDesc( + prometheus.BuildFQName(namespace, "thermal_zone", "temp"), + "Zone temperature in Celsius", + []string{"zone", "type"}, nil, + ), + }, nil +} + +func (c *thermalZoneCollector) Update(ch chan<- prometheus.Metric) error { + thermalZones, err := c.fs.ClassThermalZoneStats() + if err != nil { + return err + } + + for _, stats := range thermalZones { + ch <- prometheus.MustNewConstMetric( + c.zoneTemp, + prometheus.GaugeValue, + float64(stats.Temp)/1000.0, + stats.Name, + stats.Type, + ) + } + + return nil +} diff --git a/end-to-end-test.sh b/end-to-end-test.sh index 56ed04c8..469b8114 100755 --- a/end-to-end-test.sh +++ b/end-to-end-test.sh @@ -33,6 +33,7 @@ enabled_collectors=$(cat << COLLECTORS schedstat sockstat stat + thermal_zone textfile bonding vmstat