2021-11-25 18:15:56 +01:00
|
|
|
package collectors
|
|
|
|
|
|
|
|
import (
|
2021-11-29 15:32:58 +01:00
|
|
|
"encoding/json"
|
2021-11-25 18:15:56 +01:00
|
|
|
"errors"
|
|
|
|
"log"
|
2021-11-29 15:32:58 +01:00
|
|
|
"os"
|
|
|
|
"os/exec"
|
2021-11-25 18:15:56 +01:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"time"
|
2022-02-07 15:44:29 +01:00
|
|
|
|
2022-01-25 15:37:43 +01:00
|
|
|
lp "github.com/ClusterCockpit/cc-metric-collector/internal/ccMetric"
|
2021-11-25 18:15:56 +01:00
|
|
|
)
|
|
|
|
|
2022-02-07 15:44:29 +01:00
|
|
|
const IPMITOOL_PATH = `ipmitool`
|
|
|
|
const IPMISENSORS_PATH = `ipmi-sensors`
|
2021-11-25 18:15:56 +01:00
|
|
|
|
|
|
|
type IpmiCollectorConfig struct {
|
2021-11-29 15:32:58 +01:00
|
|
|
ExcludeDevices []string `json:"exclude_devices"`
|
|
|
|
IpmitoolPath string `json:"ipmitool_path"`
|
|
|
|
IpmisensorsPath string `json:"ipmisensors_path"`
|
2021-11-25 18:15:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
type IpmiCollector struct {
|
2022-01-25 15:37:43 +01:00
|
|
|
metricCollector
|
2022-02-07 15:44:29 +01:00
|
|
|
//tags map[string]string
|
|
|
|
//matches map[string]string
|
|
|
|
config IpmiCollectorConfig
|
|
|
|
ipmitool string
|
|
|
|
ipmisensors string
|
2021-11-25 18:15:56 +01:00
|
|
|
}
|
|
|
|
|
2022-01-25 15:37:43 +01:00
|
|
|
func (m *IpmiCollector) Init(config json.RawMessage) error {
|
2021-11-25 18:15:56 +01:00
|
|
|
m.name = "IpmiCollector"
|
|
|
|
m.setup()
|
2022-01-25 15:37:43 +01:00
|
|
|
m.meta = map[string]string{"source": m.name, "group": "IPMI"}
|
2022-02-07 15:44:29 +01:00
|
|
|
m.config.IpmitoolPath = string(IPMITOOL_PATH)
|
|
|
|
m.config.IpmisensorsPath = string(IPMISENSORS_PATH)
|
|
|
|
m.ipmitool = ""
|
|
|
|
m.ipmisensors = ""
|
2021-11-25 18:15:56 +01:00
|
|
|
if len(config) > 0 {
|
|
|
|
err := json.Unmarshal(config, &m.config)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2022-02-07 15:44:29 +01:00
|
|
|
p, err := exec.LookPath(m.config.IpmitoolPath)
|
|
|
|
if err == nil {
|
|
|
|
m.ipmitool = p
|
2021-11-25 18:15:56 +01:00
|
|
|
}
|
2022-02-07 15:44:29 +01:00
|
|
|
p, err = exec.LookPath(m.config.IpmisensorsPath)
|
|
|
|
if err == nil {
|
|
|
|
m.ipmisensors = p
|
2021-11-25 18:15:56 +01:00
|
|
|
}
|
2022-02-07 15:44:29 +01:00
|
|
|
if len(m.ipmitool) == 0 && len(m.ipmisensors) == 0 {
|
2021-11-29 15:32:58 +01:00
|
|
|
return errors.New("No IPMI reader found")
|
2021-11-25 18:15:56 +01:00
|
|
|
}
|
|
|
|
m.init = true
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-01-25 15:37:43 +01:00
|
|
|
func (m *IpmiCollector) readIpmiTool(cmd string, output chan lp.CCMetric) {
|
2021-11-25 18:15:56 +01:00
|
|
|
command := exec.Command(cmd, "sensor")
|
|
|
|
command.Wait()
|
|
|
|
stdout, err := command.Output()
|
|
|
|
if err != nil {
|
|
|
|
log.Print(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ll := strings.Split(string(stdout), "\n")
|
|
|
|
|
|
|
|
for _, line := range ll {
|
|
|
|
lv := strings.Split(line, "|")
|
|
|
|
if len(lv) < 3 {
|
2021-11-29 15:32:58 +01:00
|
|
|
continue
|
|
|
|
}
|
2021-11-25 18:15:56 +01:00
|
|
|
v, err := strconv.ParseFloat(strings.Trim(lv[1], " "), 64)
|
|
|
|
if err == nil {
|
|
|
|
name := strings.ToLower(strings.Replace(strings.Trim(lv[0], " "), " ", "_", -1))
|
|
|
|
unit := strings.Trim(lv[2], " ")
|
|
|
|
if unit == "Volts" {
|
2022-01-25 15:37:43 +01:00
|
|
|
unit = "Volts"
|
2021-11-25 18:15:56 +01:00
|
|
|
} else if unit == "degrees C" {
|
2022-01-25 15:37:43 +01:00
|
|
|
unit = "degC"
|
2021-11-25 18:15:56 +01:00
|
|
|
} else if unit == "degrees F" {
|
2022-01-25 15:37:43 +01:00
|
|
|
unit = "degF"
|
2021-11-25 18:15:56 +01:00
|
|
|
} else if unit == "Watts" {
|
2022-01-25 15:37:43 +01:00
|
|
|
unit = "Watts"
|
2021-11-25 18:15:56 +01:00
|
|
|
}
|
2021-11-29 15:32:58 +01:00
|
|
|
|
2022-01-25 15:37:43 +01:00
|
|
|
y, err := lp.New(name, map[string]string{"type": "node"}, m.meta, map[string]interface{}{"value": v}, time.Now())
|
2021-11-25 18:15:56 +01:00
|
|
|
if err == nil {
|
2022-01-25 15:37:43 +01:00
|
|
|
y.AddMeta("unit", unit)
|
|
|
|
output <- y
|
2021-11-25 18:15:56 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-25 15:37:43 +01:00
|
|
|
func (m *IpmiCollector) readIpmiSensors(cmd string, output chan lp.CCMetric) {
|
2021-11-25 18:15:56 +01:00
|
|
|
|
|
|
|
command := exec.Command(cmd, "--comma-separated-output", "--sdr-cache-recreate")
|
|
|
|
command.Wait()
|
|
|
|
stdout, err := command.Output()
|
|
|
|
if err != nil {
|
|
|
|
log.Print(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ll := strings.Split(string(stdout), "\n")
|
|
|
|
|
|
|
|
for _, line := range ll {
|
|
|
|
lv := strings.Split(line, ",")
|
|
|
|
if len(lv) > 3 {
|
2021-11-29 15:32:58 +01:00
|
|
|
v, err := strconv.ParseFloat(lv[3], 64)
|
|
|
|
if err == nil {
|
|
|
|
name := strings.ToLower(strings.Replace(lv[1], " ", "_", -1))
|
2022-01-25 15:37:43 +01:00
|
|
|
y, err := lp.New(name, map[string]string{"type": "node"}, m.meta, map[string]interface{}{"value": v}, time.Now())
|
2021-11-29 15:32:58 +01:00
|
|
|
if err == nil {
|
2022-01-25 15:37:43 +01:00
|
|
|
if len(lv) > 4 {
|
|
|
|
y.AddMeta("unit", lv[4])
|
|
|
|
}
|
|
|
|
output <- y
|
2021-11-29 15:32:58 +01:00
|
|
|
}
|
|
|
|
}
|
2021-11-25 18:15:56 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-25 15:37:43 +01:00
|
|
|
func (m *IpmiCollector) Read(interval time.Duration, output chan lp.CCMetric) {
|
2021-11-29 15:32:58 +01:00
|
|
|
if len(m.config.IpmitoolPath) > 0 {
|
|
|
|
_, err := os.Stat(m.config.IpmitoolPath)
|
|
|
|
if err == nil {
|
2022-01-25 15:37:43 +01:00
|
|
|
m.readIpmiTool(m.config.IpmitoolPath, output)
|
2021-11-25 18:15:56 +01:00
|
|
|
}
|
|
|
|
} else if len(m.config.IpmisensorsPath) > 0 {
|
2021-11-29 15:32:58 +01:00
|
|
|
_, err := os.Stat(m.config.IpmisensorsPath)
|
2021-11-25 18:15:56 +01:00
|
|
|
if err == nil {
|
2022-01-25 15:37:43 +01:00
|
|
|
m.readIpmiSensors(m.config.IpmisensorsPath, output)
|
2021-11-25 18:15:56 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *IpmiCollector) Close() {
|
|
|
|
m.init = false
|
|
|
|
}
|