From 635a75c64bb430d242f09f6648de223867f3cf32 Mon Sep 17 00:00:00 2001 From: Holger Obermaier <40787752+ho-ob@users.noreply.github.com> Date: Fri, 18 Feb 2022 16:56:41 +0100 Subject: [PATCH] Report maximum and critical temperature --- collectors.json | 2 ++ collectors/tempMetric.go | 75 +++++++++++++++++++++++++++++++++++----- 2 files changed, 69 insertions(+), 8 deletions(-) diff --git a/collectors.json b/collectors.json index 669355c..27ef822 100644 --- a/collectors.json +++ b/collectors.json @@ -15,6 +15,8 @@ "numastats": {}, "nvidia": {}, "tempstat": { + "report_max_temperature": true, + "report_critical_temperature": true, "tag_override": { "hwmon0": { "type": "socket", diff --git a/collectors/tempMetric.go b/collectors/tempMetric.go index bd26584..bbc5100 100644 --- a/collectors/tempMetric.go +++ b/collectors/tempMetric.go @@ -21,18 +21,24 @@ import ( // /sys/class/hwmon/hwmon*/temp*_crit -> 100000 = 100.0°C type TempCollectorSensor struct { - name string - label string - metricName string // Default: name_label - file string - tags map[string]string + name string + label string + metricName string // Default: name_label + file string + maxTempName string + maxTemp int64 + critTempName string + critTemp int64 + tags map[string]string } type TempCollector struct { metricCollector config struct { - ExcludeMetrics []string `json:"exclude_metrics"` - TagOverride map[string]map[string]string `json:"tag_override"` + ExcludeMetrics []string `json:"exclude_metrics"` + TagOverride map[string]map[string]string `json:"tag_override"` + ReportMaxTemp bool `json:"report_max_temperature"` + ReportCriticalTemp bool `json:"report_critical_temperature"` } sensors []*TempCollectorSensor } @@ -92,6 +98,9 @@ func (m *TempCollector) Init(config json.RawMessage) error { switch { case len(sensor.name) == 0 && len(sensor.label) == 0: continue + case sensor.name == "coretemp" && strings.HasPrefix(sensor.label, "Core ") || + sensor.name == "coretemp" && strings.HasPrefix(sensor.label, "Package id "): + sensor.metricName = "temp_" + sensor.label case len(sensor.name) != 0 && len(sensor.label) != 0: sensor.metricName = sensor.name + "_" + sensor.label case len(sensor.name) != 0: @@ -99,12 +108,12 @@ func (m *TempCollector) Init(config json.RawMessage) error { case len(sensor.label) != 0: sensor.metricName = sensor.label } + sensor.metricName = strings.ToLower(sensor.metricName) sensor.metricName = strings.Replace(sensor.metricName, " ", "_", -1) // Add temperature prefix, if required if !strings.Contains(sensor.metricName, "temp") { sensor.metricName = "temp_" + sensor.metricName } - sensor.metricName = strings.ToLower(sensor.metricName) // Sensor file sensor.file = file @@ -122,6 +131,28 @@ func (m *TempCollector) Init(config json.RawMessage) error { } } + // max temperature + if m.config.ReportMaxTemp { + maxTempFile := strings.TrimSuffix(file, "_input") + "_max" + if buffer, err := ioutil.ReadFile(maxTempFile); err == nil { + if x, err := strconv.ParseInt(strings.TrimSpace(string(buffer)), 10, 64); err == nil { + sensor.maxTempName = strings.Replace(sensor.metricName, "temp", "max_temp", 1) + sensor.maxTemp = x / 1000 + } + } + } + + // critical temperature + if m.config.ReportCriticalTemp { + criticalTempFile := strings.TrimSuffix(file, "_input") + "_crit" + if buffer, err := ioutil.ReadFile(criticalTempFile); err == nil { + if x, err := strconv.ParseInt(strings.TrimSpace(string(buffer)), 10, 64); err == nil { + sensor.critTempName = strings.Replace(sensor.metricName, "temp", "crit_temp", 1) + sensor.critTemp = x / 1000 + } + } + } + m.sensors = append(m.sensors, sensor) } @@ -164,6 +195,34 @@ func (m *TempCollector) Read(interval time.Duration, output chan lp.CCMetric) { if err == nil { output <- y } + + // max temperature + if m.config.ReportMaxTemp && sensor.maxTemp != 0 { + y, err := lp.New( + sensor.maxTempName, + sensor.tags, + m.meta, + map[string]interface{}{"value": sensor.maxTemp}, + time.Now(), + ) + if err == nil { + output <- y + } + } + + // critical temperature + if m.config.ReportCriticalTemp && sensor.critTemp != 0 { + y, err := lp.New( + sensor.critTempName, + sensor.tags, + m.meta, + map[string]interface{}{"value": sensor.critTemp}, + time.Now(), + ) + if err == nil { + output <- y + } + } } }