| 
									
										
										
										
											2019-08-04 03:56:36 -07:00
										 |  |  | // 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.
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-03 04:35:24 -07:00
										 |  |  | //go:build !nothermalzone
 | 
					
						
							| 
									
										
										
										
											2019-08-04 03:56:36 -07:00
										 |  |  | // +build !nothermalzone
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | package collector | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							| 
									
										
										
										
											2022-12-22 03:07:37 -08:00
										 |  |  | 	"errors" | 
					
						
							| 
									
										
										
										
											2019-08-04 03:56:36 -07:00
										 |  |  | 	"fmt" | 
					
						
							| 
									
										
										
										
											2024-09-11 01:51:28 -07:00
										 |  |  | 	"log/slog" | 
					
						
							| 
									
										
										
										
											2022-12-22 03:07:37 -08:00
										 |  |  | 	"os" | 
					
						
							| 
									
										
										
										
											2019-08-04 03:56:36 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	"github.com/prometheus/client_golang/prometheus" | 
					
						
							|  |  |  | 	"github.com/prometheus/procfs/sysfs" | 
					
						
							|  |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-11 20:52:16 -07:00
										 |  |  | const coolingDevice = "cooling_device" | 
					
						
							|  |  |  | const thermalZone = "thermal_zone" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-04 03:56:36 -07:00
										 |  |  | type thermalZoneCollector struct { | 
					
						
							| 
									
										
										
										
											2019-08-11 20:52:16 -07:00
										 |  |  | 	fs                    sysfs.FS | 
					
						
							|  |  |  | 	coolingDeviceCurState *prometheus.Desc | 
					
						
							|  |  |  | 	coolingDeviceMaxState *prometheus.Desc | 
					
						
							|  |  |  | 	zoneTemp              *prometheus.Desc | 
					
						
							| 
									
										
										
										
											2024-09-11 01:51:28 -07:00
										 |  |  | 	logger                *slog.Logger | 
					
						
							| 
									
										
										
										
											2019-08-04 03:56:36 -07:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func init() { | 
					
						
							|  |  |  | 	registerCollector("thermal_zone", defaultEnabled, NewThermalZoneCollector) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // NewThermalZoneCollector returns a new Collector exposing kernel/system statistics.
 | 
					
						
							| 
									
										
										
										
											2024-09-11 01:51:28 -07:00
										 |  |  | func NewThermalZoneCollector(logger *slog.Logger) (Collector, error) { | 
					
						
							| 
									
										
										
										
											2019-08-04 03:56:36 -07:00
										 |  |  | 	fs, err := sysfs.NewFS(*sysPath) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2019-11-29 05:51:31 -08:00
										 |  |  | 		return nil, fmt.Errorf("failed to open sysfs: %w", err) | 
					
						
							| 
									
										
										
										
											2019-08-04 03:56:36 -07:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return &thermalZoneCollector{ | 
					
						
							|  |  |  | 		fs: fs, | 
					
						
							|  |  |  | 		zoneTemp: prometheus.NewDesc( | 
					
						
							| 
									
										
										
										
											2019-08-11 20:52:16 -07:00
										 |  |  | 			prometheus.BuildFQName(namespace, thermalZone, "temp"), | 
					
						
							| 
									
										
										
										
											2019-08-04 03:56:36 -07:00
										 |  |  | 			"Zone temperature in Celsius", | 
					
						
							|  |  |  | 			[]string{"zone", "type"}, nil, | 
					
						
							|  |  |  | 		), | 
					
						
							| 
									
										
										
										
											2019-08-11 20:52:16 -07:00
										 |  |  | 		coolingDeviceCurState: prometheus.NewDesc( | 
					
						
							|  |  |  | 			prometheus.BuildFQName(namespace, coolingDevice, "cur_state"), | 
					
						
							|  |  |  | 			"Current throttle state of the cooling device", | 
					
						
							|  |  |  | 			[]string{"name", "type"}, nil, | 
					
						
							|  |  |  | 		), | 
					
						
							|  |  |  | 		coolingDeviceMaxState: prometheus.NewDesc( | 
					
						
							|  |  |  | 			prometheus.BuildFQName(namespace, coolingDevice, "max_state"), | 
					
						
							|  |  |  | 			"Maximum throttle state of the cooling device", | 
					
						
							|  |  |  | 			[]string{"name", "type"}, nil, | 
					
						
							|  |  |  | 		), | 
					
						
							| 
									
										
										
										
											2019-12-31 08:19:37 -08:00
										 |  |  | 		logger: logger, | 
					
						
							| 
									
										
										
										
											2019-08-04 03:56:36 -07:00
										 |  |  | 	}, nil | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func (c *thermalZoneCollector) Update(ch chan<- prometheus.Metric) error { | 
					
						
							|  |  |  | 	thermalZones, err := c.fs.ClassThermalZoneStats() | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2022-12-22 03:07:37 -08:00
										 |  |  | 		if errors.Is(err, os.ErrNotExist) || errors.Is(err, os.ErrPermission) || errors.Is(err, os.ErrInvalid) { | 
					
						
							| 
									
										
										
										
											2024-09-11 01:51:28 -07:00
										 |  |  | 			c.logger.Debug("Could not read thermal zone stats", "err", err) | 
					
						
							| 
									
										
										
										
											2022-12-22 03:07:37 -08:00
										 |  |  | 			return ErrNoData | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2019-08-04 03:56:36 -07:00
										 |  |  | 		return err | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	for _, stats := range thermalZones { | 
					
						
							|  |  |  | 		ch <- prometheus.MustNewConstMetric( | 
					
						
							|  |  |  | 			c.zoneTemp, | 
					
						
							|  |  |  | 			prometheus.GaugeValue, | 
					
						
							|  |  |  | 			float64(stats.Temp)/1000.0, | 
					
						
							|  |  |  | 			stats.Name, | 
					
						
							|  |  |  | 			stats.Type, | 
					
						
							|  |  |  | 		) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-11 20:52:16 -07:00
										 |  |  | 	coolingDevices, err := c.fs.ClassCoolingDeviceStats() | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		return err | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	for _, stats := range coolingDevices { | 
					
						
							|  |  |  | 		ch <- prometheus.MustNewConstMetric( | 
					
						
							|  |  |  | 			c.coolingDeviceCurState, | 
					
						
							|  |  |  | 			prometheus.GaugeValue, | 
					
						
							|  |  |  | 			float64(stats.CurState), | 
					
						
							|  |  |  | 			stats.Name, | 
					
						
							|  |  |  | 			stats.Type, | 
					
						
							|  |  |  | 		) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		ch <- prometheus.MustNewConstMetric( | 
					
						
							|  |  |  | 			c.coolingDeviceMaxState, | 
					
						
							|  |  |  | 			prometheus.GaugeValue, | 
					
						
							|  |  |  | 			float64(stats.MaxState), | 
					
						
							|  |  |  | 			stats.Name, | 
					
						
							|  |  |  | 			stats.Type, | 
					
						
							|  |  |  | 		) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-04 03:56:36 -07:00
										 |  |  | 	return nil | 
					
						
							|  |  |  | } |