2021-11-25 18:19:09 +01:00
|
|
|
package collectors
|
|
|
|
|
|
|
|
import (
|
2021-11-29 15:32:58 +01:00
|
|
|
"encoding/json"
|
2021-11-25 18:19:09 +01:00
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
2022-01-25 15:37:43 +01:00
|
|
|
"log"
|
2021-11-25 18:19:09 +01:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"time"
|
2022-01-25 15:37:43 +01:00
|
|
|
lp "github.com/ClusterCockpit/cc-metric-collector/internal/ccMetric"
|
2021-11-25 18:19:09 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
const HWMON_PATH = `/sys/class/hwmon`
|
|
|
|
|
|
|
|
type TempCollectorConfig struct {
|
2021-11-29 15:32:58 +01:00
|
|
|
ExcludeMetrics []string `json:"exclude_metrics"`
|
|
|
|
TagOverride map[string]map[string]string `json:"tag_override"`
|
2021-11-25 18:19:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
type TempCollector struct {
|
2022-01-25 15:37:43 +01:00
|
|
|
metricCollector
|
2021-11-25 18:19:09 +01:00
|
|
|
config TempCollectorConfig
|
|
|
|
}
|
|
|
|
|
2022-01-25 15:37:43 +01:00
|
|
|
func (m *TempCollector) Init(config json.RawMessage) error {
|
2021-11-25 18:19:09 +01:00
|
|
|
m.name = "TempCollector"
|
|
|
|
m.setup()
|
2022-01-25 15:37:43 +01:00
|
|
|
m.meta = map[string]string{"source": m.name, "group": "IPMI", "unit": "degC"}
|
2021-11-25 18:19:09 +01:00
|
|
|
if len(config) > 0 {
|
|
|
|
err := json.Unmarshal(config, &m.config)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2022-01-25 15:37:43 +01:00
|
|
|
m.init = true
|
2021-11-25 18:19:09 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func get_hwmon_sensors() (map[string]map[string]string, error) {
|
|
|
|
var folders []string
|
|
|
|
var sensors map[string]map[string]string
|
|
|
|
sensors = make(map[string]map[string]string)
|
|
|
|
err := filepath.Walk(HWMON_PATH, func(p string, info os.FileInfo, err error) error {
|
|
|
|
if info.IsDir() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
folders = append(folders, p)
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return sensors, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, f := range folders {
|
|
|
|
sensors[f] = make(map[string]string)
|
|
|
|
myp := fmt.Sprintf("%s/", f)
|
|
|
|
err := filepath.Walk(myp, func(path string, info os.FileInfo, err error) error {
|
|
|
|
dir, fname := filepath.Split(path)
|
|
|
|
if strings.Contains(fname, "temp") && strings.Contains(fname, "_input") {
|
|
|
|
namefile := fmt.Sprintf("%s/%s", dir, strings.Replace(fname, "_input", "_label", -1))
|
|
|
|
name, ierr := ioutil.ReadFile(namefile)
|
|
|
|
if ierr == nil {
|
|
|
|
sensors[f][strings.Replace(string(name), "\n", "", -1)] = path
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return sensors, nil
|
|
|
|
}
|
|
|
|
|
2022-01-25 15:37:43 +01:00
|
|
|
func (m *TempCollector) Read(interval time.Duration, output chan lp.CCMetric) {
|
2021-11-25 18:19:09 +01:00
|
|
|
|
|
|
|
sensors, err := get_hwmon_sensors()
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
for _, files := range sensors {
|
|
|
|
for name, file := range files {
|
|
|
|
tags := map[string]string{"type": "node"}
|
|
|
|
for key, newtags := range m.config.TagOverride {
|
|
|
|
if strings.Contains(file, key) {
|
|
|
|
tags = newtags
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2022-01-25 15:37:43 +01:00
|
|
|
mname := strings.Replace(name, " ", "_", -1)
|
|
|
|
if !strings.Contains(mname, "temp") {
|
|
|
|
mname = fmt.Sprintf("temp_%s", mname)
|
|
|
|
}
|
2021-11-25 18:19:09 +01:00
|
|
|
buffer, err := ioutil.ReadFile(string(file))
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
x, err := strconv.ParseInt(strings.Replace(string(buffer), "\n", "", -1), 0, 64)
|
|
|
|
if err == nil {
|
2022-01-25 15:37:43 +01:00
|
|
|
y, err := lp.New(strings.ToLower(mname), tags, m.meta, map[string]interface{}{"value": int(float64(x) / 1000)}, time.Now())
|
2021-11-25 18:19:09 +01:00
|
|
|
if err == nil {
|
2022-01-25 15:37:43 +01:00
|
|
|
log.Print("[", m.name, "] ", y)
|
|
|
|
output <- y
|
2021-11-25 18:19:09 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *TempCollector) Close() {
|
|
|
|
m.init = false
|
|
|
|
}
|