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.
|
|
|
|
|
|
|
|
// +build !nothermalzone
|
|
|
|
|
|
|
|
package collector
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
2019-12-31 08:19:37 -08:00
|
|
|
"github.com/go-kit/kit/log"
|
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
|
2019-12-31 08:19:37 -08:00
|
|
|
logger log.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.
|
2019-12-31 08:19:37 -08:00
|
|
|
func NewThermalZoneCollector(logger log.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 {
|
|
|
|
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
|
|
|
|
}
|