2021-09-21 10:18:34 +00:00
|
|
|
// Copyright 2021 The Prometheus Authors
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2021-05-19 21:27:29 +00:00
|
|
|
package freeipmi
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"crypto/rand"
|
|
|
|
"encoding/csv"
|
|
|
|
"encoding/hex"
|
|
|
|
"fmt"
|
|
|
|
"math"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
"path/filepath"
|
|
|
|
"regexp"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"syscall"
|
2021-09-21 12:44:11 +00:00
|
|
|
|
|
|
|
"github.com/go-kit/log"
|
|
|
|
"github.com/go-kit/log/level"
|
2021-05-19 21:27:29 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
ipmiDCMICurrentPowerRegex = regexp.MustCompile(`^Current Power\s*:\s*(?P<value>[0-9.]*)\s*Watts.*`)
|
|
|
|
ipmiChassisPowerRegex = regexp.MustCompile(`^System Power\s*:\s(?P<value>.*)`)
|
2023-01-12 14:18:28 +00:00
|
|
|
ipmiChassisDriveFaultRegex = regexp.MustCompile(`^Drive Fault\s*:\s(?P<value>.*)`)
|
|
|
|
ipmiChassisCoolingFaultRegex = regexp.MustCompile(`^Cooling/fan fault\s*:\s(?P<value>.*)`)
|
2021-05-19 21:27:29 +00:00
|
|
|
ipmiSELEntriesRegex = regexp.MustCompile(`^Number of log entries\s*:\s(?P<value>[0-9.]*)`)
|
|
|
|
ipmiSELFreeSpaceRegex = regexp.MustCompile(`^Free space remaining\s*:\s(?P<value>[0-9.]*)\s*bytes.*`)
|
|
|
|
bmcInfoFirmwareRevisionRegex = regexp.MustCompile(`^Firmware Revision\s*:\s*(?P<value>[0-9.]*).*`)
|
|
|
|
bmcInfoSystemFirmwareVersionRegex = regexp.MustCompile(`^System Firmware Version\s*:\s*(?P<value>[0-9.]*).*`)
|
|
|
|
bmcInfoManufacturerIDRegex = regexp.MustCompile(`^Manufacturer ID\s*:\s*(?P<value>.*)`)
|
|
|
|
)
|
|
|
|
|
|
|
|
// Result represents the outcome of a call to one of the FreeIPMI tools.
|
|
|
|
// It can be used with other functions in this package to extract data.
|
|
|
|
type Result struct {
|
|
|
|
output []byte
|
|
|
|
err error
|
|
|
|
}
|
|
|
|
|
|
|
|
// SensorData represents the reading of a single sensor.
|
|
|
|
type SensorData struct {
|
|
|
|
ID int64
|
|
|
|
Name string
|
|
|
|
Type string
|
|
|
|
State string
|
|
|
|
Value float64
|
|
|
|
Unit string
|
|
|
|
Event string
|
|
|
|
}
|
|
|
|
|
|
|
|
// EscapePassword escapes a password so that the result is suitable for usage in a
|
|
|
|
// FreeIPMI config file.
|
|
|
|
func EscapePassword(password string) string {
|
|
|
|
return strings.Replace(password, "#", "\\#", -1)
|
|
|
|
}
|
|
|
|
|
2021-12-09 19:54:01 +00:00
|
|
|
func pipeName() (string, error) {
|
2021-05-19 21:27:29 +00:00
|
|
|
randBytes := make([]byte, 16)
|
2021-12-09 19:54:01 +00:00
|
|
|
_, err := rand.Read(randBytes)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return filepath.Join(os.TempDir(), "ipmi_exporter-"+hex.EncodeToString(randBytes)), nil
|
2021-05-19 21:27:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func contains(s []int64, elm int64) bool {
|
|
|
|
for _, a := range s {
|
|
|
|
if a == elm {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func getValue(ipmiOutput []byte, regex *regexp.Regexp) (string, error) {
|
|
|
|
for _, line := range strings.Split(string(ipmiOutput), "\n") {
|
|
|
|
match := regex.FindStringSubmatch(line)
|
|
|
|
if match == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
for i, name := range regex.SubexpNames() {
|
|
|
|
if name != "value" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
return match[i], nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return "", fmt.Errorf("could not find value in output: %s", string(ipmiOutput))
|
|
|
|
}
|
|
|
|
|
2021-09-21 12:44:11 +00:00
|
|
|
func freeipmiConfigPipe(config string, logger log.Logger) (string, error) {
|
2021-05-19 21:27:29 +00:00
|
|
|
content := []byte(config)
|
2021-12-09 19:54:01 +00:00
|
|
|
pipe, err := pipeName()
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
err = syscall.Mkfifo(pipe, 0600)
|
2021-05-19 21:27:29 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
go func(file string, data []byte) {
|
|
|
|
f, err := os.OpenFile(file, os.O_WRONLY|os.O_CREATE|os.O_APPEND, os.ModeNamedPipe)
|
|
|
|
if err != nil {
|
2022-02-22 13:55:27 +00:00
|
|
|
level.Error(logger).Log("msg", "Error opening pipe", "error", err)
|
2021-05-19 21:27:29 +00:00
|
|
|
}
|
|
|
|
if _, err := f.Write(data); err != nil {
|
2022-02-22 13:55:27 +00:00
|
|
|
level.Error(logger).Log("msg", "Error writing config to pipe", "error", err)
|
2021-05-19 21:27:29 +00:00
|
|
|
}
|
|
|
|
f.Close()
|
|
|
|
}(pipe, content)
|
|
|
|
return pipe, nil
|
|
|
|
}
|
|
|
|
|
2021-09-21 12:44:11 +00:00
|
|
|
func Execute(cmd string, args []string, config string, target string, logger log.Logger) Result {
|
2021-05-19 21:27:29 +00:00
|
|
|
pipe, err := freeipmiConfigPipe(config, logger)
|
|
|
|
if err != nil {
|
|
|
|
return Result{nil, err}
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
if err := os.Remove(pipe); err != nil {
|
2022-02-22 13:55:27 +00:00
|
|
|
level.Error(logger).Log("msg", "Error deleting named pipe", "error", err)
|
2021-05-19 21:27:29 +00:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
args = append(args, "--config-file", pipe)
|
|
|
|
if target != "" {
|
|
|
|
args = append(args, "-h", target)
|
|
|
|
}
|
|
|
|
|
2022-02-22 13:55:27 +00:00
|
|
|
level.Debug(logger).Log("msg", "Executing", "command", cmd, "args", fmt.Sprintf("%+v", args))
|
2021-05-19 21:27:29 +00:00
|
|
|
out, err := exec.Command(cmd, args...).CombinedOutput()
|
|
|
|
if err != nil {
|
|
|
|
err = fmt.Errorf("error running %s: %s", cmd, err)
|
|
|
|
}
|
|
|
|
return Result{out, err}
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetSensorData(ipmiOutput Result, excludeSensorIds []int64) ([]SensorData, error) {
|
|
|
|
var result []SensorData
|
|
|
|
|
|
|
|
if ipmiOutput.err != nil {
|
|
|
|
return result, fmt.Errorf("%s: %s", ipmiOutput.err, ipmiOutput.output)
|
|
|
|
}
|
|
|
|
|
|
|
|
r := csv.NewReader(bytes.NewReader(ipmiOutput.output))
|
|
|
|
fields, err := r.ReadAll()
|
|
|
|
if err != nil {
|
|
|
|
return result, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, line := range fields {
|
|
|
|
var data SensorData
|
|
|
|
|
|
|
|
data.ID, err = strconv.ParseInt(line[0], 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return result, err
|
|
|
|
}
|
|
|
|
if contains(excludeSensorIds, data.ID) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
data.Name = line[1]
|
|
|
|
data.Type = line[2]
|
|
|
|
data.State = line[3]
|
|
|
|
|
|
|
|
value := line[4]
|
|
|
|
if value != "N/A" {
|
|
|
|
data.Value, err = strconv.ParseFloat(value, 64)
|
|
|
|
if err != nil {
|
|
|
|
return result, err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
data.Value = math.NaN()
|
|
|
|
}
|
|
|
|
|
|
|
|
data.Unit = line[5]
|
|
|
|
data.Event = strings.Trim(line[6], "'")
|
|
|
|
|
|
|
|
result = append(result, data)
|
|
|
|
}
|
|
|
|
return result, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetCurrentPowerConsumption(ipmiOutput Result) (float64, error) {
|
|
|
|
if ipmiOutput.err != nil {
|
|
|
|
return -1, fmt.Errorf("%s: %s", ipmiOutput.err, ipmiOutput.output)
|
|
|
|
}
|
|
|
|
value, err := getValue(ipmiOutput.output, ipmiDCMICurrentPowerRegex)
|
|
|
|
if err != nil {
|
|
|
|
return -1, err
|
|
|
|
}
|
|
|
|
return strconv.ParseFloat(value, 64)
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetChassisPowerState(ipmiOutput Result) (float64, error) {
|
|
|
|
if ipmiOutput.err != nil {
|
|
|
|
return -1, fmt.Errorf("%s: %s", ipmiOutput.err, ipmiOutput.output)
|
|
|
|
}
|
|
|
|
value, err := getValue(ipmiOutput.output, ipmiChassisPowerRegex)
|
|
|
|
if err != nil {
|
|
|
|
return -1, err
|
|
|
|
}
|
|
|
|
if value == "on" {
|
|
|
|
return 1, err
|
|
|
|
}
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
2023-01-12 14:18:28 +00:00
|
|
|
func GetChassisDriveFault(ipmiOutput Result) (float64, error) {
|
|
|
|
if ipmiOutput.err != nil {
|
|
|
|
return -1, fmt.Errorf("%s: %s", ipmiOutput.err, ipmiOutput.output)
|
|
|
|
}
|
|
|
|
value, err := getValue(ipmiOutput.output, ipmiChassisDriveFaultRegex)
|
|
|
|
if err != nil {
|
|
|
|
return -1, err
|
|
|
|
}
|
|
|
|
if value == "false" {
|
|
|
|
return 1, err
|
|
|
|
}
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetChassisCoolingFault(ipmiOutput Result) (float64, error) {
|
|
|
|
if ipmiOutput.err != nil {
|
|
|
|
return -1, fmt.Errorf("%s: %s", ipmiOutput.err, ipmiOutput.output)
|
|
|
|
}
|
|
|
|
value, err := getValue(ipmiOutput.output, ipmiChassisCoolingFaultRegex)
|
|
|
|
if err != nil {
|
|
|
|
return -1, err
|
|
|
|
}
|
|
|
|
if value == "false" {
|
|
|
|
return 1, err
|
|
|
|
}
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
2021-05-19 21:27:29 +00:00
|
|
|
func GetBMCInfoFirmwareRevision(ipmiOutput Result) (string, error) {
|
2021-09-21 10:18:34 +00:00
|
|
|
// Workaround for an issue described here: https://github.com/prometheus-community/ipmi_exporter/issues/57
|
2021-05-19 21:27:29 +00:00
|
|
|
// The command may fail, but produce usable output (minus the system firmware revision).
|
|
|
|
// Try to recover gracefully from that situation by first trying to parse the output, and only
|
|
|
|
// raise the initial error if that also fails.
|
|
|
|
value, err := getValue(ipmiOutput.output, bmcInfoFirmwareRevisionRegex)
|
|
|
|
if err != nil {
|
|
|
|
if ipmiOutput.err != nil {
|
|
|
|
return "", fmt.Errorf("%s: %s", ipmiOutput.err, ipmiOutput.output)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return value, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetBMCInfoManufacturerID(ipmiOutput Result) (string, error) {
|
2021-09-21 10:18:34 +00:00
|
|
|
// Workaround for an issue described here: https://github.com/prometheus-community/ipmi_exporter/issues/57
|
2021-05-19 21:27:29 +00:00
|
|
|
// The command may fail, but produce usable output (minus the system firmware revision).
|
|
|
|
// Try to recover gracefully from that situation by first trying to parse the output, and only
|
|
|
|
// raise the initial error if that also fails.
|
|
|
|
value, err := getValue(ipmiOutput.output, bmcInfoManufacturerIDRegex)
|
|
|
|
if err != nil {
|
|
|
|
if ipmiOutput.err != nil {
|
|
|
|
return "", fmt.Errorf("%s: %s", ipmiOutput.err, ipmiOutput.output)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return value, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetBMCInfoSystemFirmwareVersion(ipmiOutput Result) (string, error) {
|
|
|
|
if ipmiOutput.err != nil {
|
|
|
|
return "", fmt.Errorf("%s: %s", ipmiOutput.err, ipmiOutput.output)
|
|
|
|
}
|
|
|
|
return getValue(ipmiOutput.output, bmcInfoSystemFirmwareVersionRegex)
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetSELInfoEntriesCount(ipmiOutput Result) (float64, error) {
|
|
|
|
if ipmiOutput.err != nil {
|
|
|
|
return -1, fmt.Errorf("%s: %s", ipmiOutput.err, ipmiOutput.output)
|
|
|
|
}
|
|
|
|
value, err := getValue(ipmiOutput.output, ipmiSELEntriesRegex)
|
|
|
|
if err != nil {
|
|
|
|
return -1, err
|
|
|
|
}
|
|
|
|
return strconv.ParseFloat(value, 64)
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetSELInfoFreeSpace(ipmiOutput Result) (float64, error) {
|
|
|
|
if ipmiOutput.err != nil {
|
|
|
|
return -1, fmt.Errorf("%s: %s", ipmiOutput.err, ipmiOutput.output)
|
|
|
|
}
|
|
|
|
value, err := getValue(ipmiOutput.output, ipmiSELFreeSpaceRegex)
|
|
|
|
if err != nil {
|
|
|
|
return -1, err
|
|
|
|
}
|
|
|
|
return strconv.ParseFloat(value, 64)
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetRawOctets(ipmiOutput Result) ([]string, error) {
|
|
|
|
if ipmiOutput.err != nil {
|
|
|
|
return nil, fmt.Errorf("%s: %s", ipmiOutput.err, ipmiOutput.output)
|
|
|
|
}
|
|
|
|
strOutput := strings.Trim(string(ipmiOutput.output), " \r\n")
|
|
|
|
if !strings.HasPrefix(strOutput, "rcvd: ") {
|
|
|
|
return nil, fmt.Errorf("unexpected raw response: %s", strOutput)
|
|
|
|
}
|
2022-10-31 08:53:34 +00:00
|
|
|
octets := strings.Split(strOutput[6:], " ")
|
|
|
|
return octets, nil
|
2021-05-19 21:27:29 +00:00
|
|
|
}
|