2021-11-25 18:15:56 +01:00
|
|
|
package collectors
|
|
|
|
|
|
|
|
import (
|
2022-11-11 16:16:14 +01:00
|
|
|
"bufio"
|
|
|
|
"bytes"
|
2021-11-29 15:32:58 +01:00
|
|
|
"encoding/json"
|
2021-11-25 18:15:56 +01:00
|
|
|
"errors"
|
2022-11-11 16:16:14 +01:00
|
|
|
"fmt"
|
|
|
|
"io"
|
2021-11-25 18:15:56 +01:00
|
|
|
"log"
|
2021-11-29 15:32:58 +01:00
|
|
|
"os/exec"
|
2021-11-25 18:15:56 +01:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"time"
|
2022-02-07 15:44:29 +01:00
|
|
|
|
2022-11-11 16:16:14 +01:00
|
|
|
cclog "github.com/ClusterCockpit/cc-metric-collector/pkg/ccLogger"
|
2022-10-10 11:53:11 +02:00
|
|
|
lp "github.com/ClusterCockpit/cc-metric-collector/pkg/ccMetric"
|
2021-11-25 18:15:56 +01:00
|
|
|
)
|
|
|
|
|
2022-02-07 15:44:29 +01:00
|
|
|
const IPMISENSORS_PATH = `ipmi-sensors`
|
2021-11-25 18:15:56 +01:00
|
|
|
|
|
|
|
type IpmiCollector struct {
|
2022-01-25 15:37:43 +01:00
|
|
|
metricCollector
|
2022-11-11 16:16:14 +01:00
|
|
|
config struct {
|
|
|
|
ExcludeDevices []string `json:"exclude_devices"`
|
|
|
|
IpmitoolPath string `json:"ipmitool_path"`
|
|
|
|
IpmisensorsPath string `json:"ipmisensors_path"`
|
|
|
|
}
|
2022-02-07 15:44:29 +01:00
|
|
|
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 {
|
2022-11-11 16:16:14 +01:00
|
|
|
// Check if already initialized
|
|
|
|
if m.init {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-11-25 18:15:56 +01:00
|
|
|
m.name = "IpmiCollector"
|
|
|
|
m.setup()
|
2022-05-13 14:10:39 +02:00
|
|
|
m.parallel = true
|
2022-11-11 16:16:14 +01:00
|
|
|
m.meta = map[string]string{
|
|
|
|
"source": m.name,
|
|
|
|
"group": "IPMI",
|
|
|
|
}
|
|
|
|
// default path to IPMI tools
|
|
|
|
m.config.IpmitoolPath = "ipmitool"
|
|
|
|
m.config.IpmisensorsPath = "ipmi-sensors"
|
2021-11-25 18:15:56 +01:00
|
|
|
if len(config) > 0 {
|
|
|
|
err := json.Unmarshal(config, &m.config)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2022-11-11 16:16:14 +01:00
|
|
|
// Check if executables ipmitool or ipmisensors are found
|
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 {
|
2022-03-15 16:38:20 +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) {
|
2022-11-11 16:16:14 +01:00
|
|
|
|
|
|
|
// Setup ipmitool command
|
2021-11-25 18:15:56 +01:00
|
|
|
command := exec.Command(cmd, "sensor")
|
2022-11-11 16:16:14 +01:00
|
|
|
stdout, _ := command.StdoutPipe()
|
|
|
|
errBuf := new(bytes.Buffer)
|
|
|
|
command.Stderr = errBuf
|
|
|
|
|
|
|
|
// start command
|
|
|
|
if err := command.Start(); err != nil {
|
|
|
|
cclog.ComponentError(
|
|
|
|
m.name,
|
|
|
|
fmt.Sprintf("readIpmiTool(): Failed to start command \"%s\": %v", command.String(), err),
|
|
|
|
)
|
2021-11-25 18:15:56 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-11-11 16:16:14 +01:00
|
|
|
// Read command output
|
|
|
|
scanner := bufio.NewScanner(stdout)
|
|
|
|
for scanner.Scan() {
|
|
|
|
lv := strings.Split(scanner.Text(), "|")
|
2021-11-25 18:15:56 +01:00
|
|
|
if len(lv) < 3 {
|
2021-11-29 15:32:58 +01:00
|
|
|
continue
|
|
|
|
}
|
2022-11-11 16:16:14 +01:00
|
|
|
v, err := strconv.ParseFloat(strings.TrimSpace(lv[1]), 64)
|
2021-11-25 18:15:56 +01:00
|
|
|
if err == nil {
|
2022-11-11 16:16:14 +01:00
|
|
|
name := strings.ToLower(strings.Replace(strings.TrimSpace(lv[0]), " ", "_", -1))
|
|
|
|
unit := strings.TrimSpace(lv[2])
|
2021-11-25 18:15:56 +01:00
|
|
|
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-11-11 16:16:14 +01:00
|
|
|
|
|
|
|
// Wait for command end
|
|
|
|
if err := command.Wait(); err != nil {
|
|
|
|
errMsg, _ := io.ReadAll(errBuf)
|
|
|
|
cclog.ComponentError(
|
|
|
|
m.name,
|
|
|
|
fmt.Sprintf("readIpmiTool(): Failed to wait for the end of command \"%s\": %v\n", command.String(), err),
|
|
|
|
fmt.Sprintf("readIpmiTool(): command stderr: \"%s\"\n", string(errMsg)),
|
|
|
|
)
|
|
|
|
return
|
|
|
|
}
|
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) {
|
2022-11-11 16:16:14 +01:00
|
|
|
|
|
|
|
// Check if already initialized
|
|
|
|
if !m.init {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-11-29 15:32:58 +01:00
|
|
|
if len(m.config.IpmitoolPath) > 0 {
|
2022-11-11 16:16:14 +01:00
|
|
|
m.readIpmiTool(m.config.IpmitoolPath, output)
|
2021-11-25 18:15:56 +01:00
|
|
|
} else if len(m.config.IpmisensorsPath) > 0 {
|
2022-11-11 16:16:14 +01:00
|
|
|
m.readIpmiSensors(m.config.IpmisensorsPath, output)
|
2021-11-25 18:15:56 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *IpmiCollector) Close() {
|
|
|
|
m.init = false
|
|
|
|
}
|