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-01-21 14:35:52 +01:00
|
|
|
|
|
|
|
lp "github.com/influxdata/line-protocol"
|
2021-11-25 18:15:56 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
const IPMITOOL_PATH = `/usr/bin/ipmitool`
|
|
|
|
const IPMISENSORS_PATH = `/usr/sbin/ipmi-sensors`
|
|
|
|
|
|
|
|
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 {
|
|
|
|
MetricCollector
|
|
|
|
tags map[string]string
|
|
|
|
matches map[string]string
|
2021-11-29 15:32:58 +01:00
|
|
|
config IpmiCollectorConfig
|
2021-11-25 18:15:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *IpmiCollector) Init(config []byte) error {
|
|
|
|
m.name = "IpmiCollector"
|
|
|
|
m.setup()
|
|
|
|
if len(config) > 0 {
|
|
|
|
err := json.Unmarshal(config, &m.config)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_, err1 := os.Stat(m.config.IpmitoolPath)
|
|
|
|
_, err2 := os.Stat(m.config.IpmisensorsPath)
|
|
|
|
if err1 != nil {
|
2021-11-29 15:32:58 +01:00
|
|
|
m.config.IpmitoolPath = ""
|
2021-11-25 18:15:56 +01:00
|
|
|
}
|
|
|
|
if err2 != nil {
|
2021-11-29 15:32:58 +01:00
|
|
|
m.config.IpmisensorsPath = ""
|
2021-11-25 18:15:56 +01:00
|
|
|
}
|
|
|
|
if err1 != nil && err2 != nil {
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
func ReadIpmiTool(cmd string, out *[]lp.MutableMetric) {
|
|
|
|
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" {
|
2021-11-29 15:32:58 +01:00
|
|
|
unit = "V"
|
2021-11-25 18:15:56 +01:00
|
|
|
} else if unit == "degrees C" {
|
2021-11-29 15:32:58 +01:00
|
|
|
unit = "C"
|
2021-11-25 18:15:56 +01:00
|
|
|
} else if unit == "degrees F" {
|
2021-11-29 15:32:58 +01:00
|
|
|
unit = "F"
|
2021-11-25 18:15:56 +01:00
|
|
|
} else if unit == "Watts" {
|
2021-11-29 15:32:58 +01:00
|
|
|
unit = "W"
|
2021-11-25 18:15:56 +01:00
|
|
|
}
|
2021-11-29 15:32:58 +01:00
|
|
|
|
|
|
|
y, err := lp.New(name, map[string]string{"unit": unit, "type": "node"}, map[string]interface{}{"value": v}, time.Now())
|
2021-11-25 18:15:56 +01:00
|
|
|
if err == nil {
|
|
|
|
*out = append(*out, y)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func ReadIpmiSensors(cmd string, out *[]lp.MutableMetric) {
|
|
|
|
|
|
|
|
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))
|
|
|
|
y, err := lp.New(name, map[string]string{"unit": lv[4], "type": "node"}, map[string]interface{}{"value": v}, time.Now())
|
|
|
|
if err == nil {
|
|
|
|
*out = append(*out, y)
|
|
|
|
}
|
|
|
|
}
|
2021-11-25 18:15:56 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *IpmiCollector) Read(interval time.Duration, out *[]lp.MutableMetric) {
|
2021-11-29 15:32:58 +01:00
|
|
|
if len(m.config.IpmitoolPath) > 0 {
|
|
|
|
_, err := os.Stat(m.config.IpmitoolPath)
|
|
|
|
if err == nil {
|
|
|
|
ReadIpmiTool(m.config.IpmitoolPath, out)
|
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 {
|
|
|
|
ReadIpmiSensors(m.config.IpmisensorsPath, out)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *IpmiCollector) Close() {
|
|
|
|
m.init = false
|
|
|
|
}
|