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 main
|
|
|
|
|
|
|
|
import (
|
2021-11-09 05:31:25 +00:00
|
|
|
"fmt"
|
2021-05-19 21:27:29 +00:00
|
|
|
"math"
|
|
|
|
"strconv"
|
|
|
|
|
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
|
|
|
|
2021-09-21 10:18:34 +00:00
|
|
|
"github.com/prometheus-community/ipmi_exporter/freeipmi"
|
2021-05-19 21:27:29 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
IPMICollectorName CollectorName = "ipmi"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
sensorStateDesc = prometheus.NewDesc(
|
|
|
|
prometheus.BuildFQName(namespace, "sensor", "state"),
|
|
|
|
"Indicates the severity of the state reported by an IPMI sensor (0=nominal, 1=warning, 2=critical).",
|
|
|
|
[]string{"id", "name", "type"},
|
|
|
|
nil,
|
|
|
|
)
|
|
|
|
|
|
|
|
sensorValueDesc = prometheus.NewDesc(
|
|
|
|
prometheus.BuildFQName(namespace, "sensor", "value"),
|
|
|
|
"Generic data read from an IPMI sensor of unknown type, relying on labels for context.",
|
|
|
|
[]string{"id", "name", "type"},
|
|
|
|
nil,
|
|
|
|
)
|
|
|
|
|
2021-11-14 10:32:52 +00:00
|
|
|
fanSpeedRPMDesc = prometheus.NewDesc(
|
2021-05-19 21:27:29 +00:00
|
|
|
prometheus.BuildFQName(namespace, "fan_speed", "rpm"),
|
|
|
|
"Fan speed in rotations per minute.",
|
|
|
|
[]string{"id", "name"},
|
|
|
|
nil,
|
|
|
|
)
|
|
|
|
|
2021-11-14 10:32:52 +00:00
|
|
|
fanSpeedRatioDesc = prometheus.NewDesc(
|
|
|
|
prometheus.BuildFQName(namespace, "fan_speed", "ratio"),
|
|
|
|
"Fan speed as a proportion of the maximum speed.",
|
|
|
|
[]string{"id", "name"},
|
|
|
|
nil,
|
|
|
|
)
|
|
|
|
|
2021-05-19 21:27:29 +00:00
|
|
|
fanSpeedStateDesc = prometheus.NewDesc(
|
|
|
|
prometheus.BuildFQName(namespace, "fan_speed", "state"),
|
|
|
|
"Reported state of a fan speed sensor (0=nominal, 1=warning, 2=critical).",
|
|
|
|
[]string{"id", "name"},
|
|
|
|
nil,
|
|
|
|
)
|
|
|
|
|
|
|
|
temperatureDesc = prometheus.NewDesc(
|
|
|
|
prometheus.BuildFQName(namespace, "temperature", "celsius"),
|
|
|
|
"Temperature reading in degree Celsius.",
|
|
|
|
[]string{"id", "name"},
|
|
|
|
nil,
|
|
|
|
)
|
|
|
|
|
|
|
|
temperatureStateDesc = prometheus.NewDesc(
|
|
|
|
prometheus.BuildFQName(namespace, "temperature", "state"),
|
|
|
|
"Reported state of a temperature sensor (0=nominal, 1=warning, 2=critical).",
|
|
|
|
[]string{"id", "name"},
|
|
|
|
nil,
|
|
|
|
)
|
|
|
|
|
|
|
|
voltageDesc = prometheus.NewDesc(
|
|
|
|
prometheus.BuildFQName(namespace, "voltage", "volts"),
|
|
|
|
"Voltage reading in Volts.",
|
|
|
|
[]string{"id", "name"},
|
|
|
|
nil,
|
|
|
|
)
|
|
|
|
|
|
|
|
voltageStateDesc = prometheus.NewDesc(
|
|
|
|
prometheus.BuildFQName(namespace, "voltage", "state"),
|
|
|
|
"Reported state of a voltage sensor (0=nominal, 1=warning, 2=critical).",
|
|
|
|
[]string{"id", "name"},
|
|
|
|
nil,
|
|
|
|
)
|
|
|
|
|
|
|
|
currentDesc = prometheus.NewDesc(
|
|
|
|
prometheus.BuildFQName(namespace, "current", "amperes"),
|
|
|
|
"Current reading in Amperes.",
|
|
|
|
[]string{"id", "name"},
|
|
|
|
nil,
|
|
|
|
)
|
|
|
|
|
|
|
|
currentStateDesc = prometheus.NewDesc(
|
|
|
|
prometheus.BuildFQName(namespace, "current", "state"),
|
|
|
|
"Reported state of a current sensor (0=nominal, 1=warning, 2=critical).",
|
|
|
|
[]string{"id", "name"},
|
|
|
|
nil,
|
|
|
|
)
|
|
|
|
|
|
|
|
powerDesc = prometheus.NewDesc(
|
|
|
|
prometheus.BuildFQName(namespace, "power", "watts"),
|
|
|
|
"Power reading in Watts.",
|
|
|
|
[]string{"id", "name"},
|
|
|
|
nil,
|
|
|
|
)
|
|
|
|
|
|
|
|
powerStateDesc = prometheus.NewDesc(
|
|
|
|
prometheus.BuildFQName(namespace, "power", "state"),
|
|
|
|
"Reported state of a power sensor (0=nominal, 1=warning, 2=critical).",
|
|
|
|
[]string{"id", "name"},
|
|
|
|
nil,
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
type IPMICollector struct{}
|
|
|
|
|
|
|
|
func (c IPMICollector) Name() CollectorName {
|
|
|
|
return IPMICollectorName
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c IPMICollector) Cmd() string {
|
|
|
|
return "ipmimonitoring"
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c IPMICollector) Args() []string {
|
|
|
|
return []string{
|
|
|
|
"-Q",
|
|
|
|
"--ignore-unrecognized-events",
|
|
|
|
"--comma-separated-output",
|
|
|
|
"--no-header-output",
|
|
|
|
"--sdr-cache-recreate",
|
|
|
|
"--output-event-bitmask",
|
2022-06-08 14:29:21 +00:00
|
|
|
"--output-sensor-state",
|
2021-05-19 21:27:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c IPMICollector) Collect(result freeipmi.Result, ch chan<- prometheus.Metric, target ipmiTarget) (int, error) {
|
|
|
|
excludeIds := target.config.ExcludeSensorIDs
|
2022-01-21 11:39:15 +00:00
|
|
|
targetHost := targetName(target.host)
|
2021-05-19 21:27:29 +00:00
|
|
|
results, err := freeipmi.GetSensorData(result, excludeIds)
|
|
|
|
if err != nil {
|
2024-10-18 00:09:00 +00:00
|
|
|
logger.Error("Failed to collect sensor data", "target", targetHost, "error", err)
|
2021-05-19 21:27:29 +00:00
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
for _, data := range results {
|
|
|
|
var state float64
|
|
|
|
|
|
|
|
switch data.State {
|
|
|
|
case "Nominal":
|
|
|
|
state = 0
|
|
|
|
case "Warning":
|
|
|
|
state = 1
|
|
|
|
case "Critical":
|
|
|
|
state = 2
|
|
|
|
case "N/A":
|
|
|
|
state = math.NaN()
|
|
|
|
default:
|
2024-10-18 00:09:00 +00:00
|
|
|
logger.Error("Unknown sensor state", "target", targetHost, "state", data.State)
|
2021-05-19 21:27:29 +00:00
|
|
|
state = math.NaN()
|
|
|
|
}
|
|
|
|
|
2024-10-18 00:09:00 +00:00
|
|
|
logger.Debug("Got values", "target", targetHost, "data", fmt.Sprintf("%+v", data))
|
2021-05-19 21:27:29 +00:00
|
|
|
|
|
|
|
switch data.Unit {
|
|
|
|
case "RPM":
|
2021-11-14 10:32:52 +00:00
|
|
|
collectTypedSensor(ch, fanSpeedRPMDesc, fanSpeedStateDesc, state, data, 1.0)
|
2021-05-19 21:27:29 +00:00
|
|
|
case "C":
|
2021-11-14 10:32:52 +00:00
|
|
|
collectTypedSensor(ch, temperatureDesc, temperatureStateDesc, state, data, 1.0)
|
2021-05-19 21:27:29 +00:00
|
|
|
case "A":
|
2021-11-14 10:32:52 +00:00
|
|
|
collectTypedSensor(ch, currentDesc, currentStateDesc, state, data, 1.0)
|
2021-05-19 21:27:29 +00:00
|
|
|
case "V":
|
2021-11-14 10:32:52 +00:00
|
|
|
collectTypedSensor(ch, voltageDesc, voltageStateDesc, state, data, 1.0)
|
2021-05-19 21:27:29 +00:00
|
|
|
case "W":
|
2021-11-14 10:32:52 +00:00
|
|
|
collectTypedSensor(ch, powerDesc, powerStateDesc, state, data, 1.0)
|
|
|
|
case "%":
|
|
|
|
switch data.Type {
|
|
|
|
case "Fan":
|
|
|
|
collectTypedSensor(ch, fanSpeedRatioDesc, fanSpeedStateDesc, state, data, 0.01)
|
|
|
|
default:
|
|
|
|
collectGenericSensor(ch, state, data)
|
|
|
|
}
|
2021-05-19 21:27:29 +00:00
|
|
|
default:
|
|
|
|
collectGenericSensor(ch, state, data)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 1, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c IPMICollector) Describe(ch chan<- *prometheus.Desc) {
|
|
|
|
ch <- sensorStateDesc
|
|
|
|
ch <- sensorValueDesc
|
2021-11-14 10:32:52 +00:00
|
|
|
ch <- fanSpeedRPMDesc
|
|
|
|
ch <- fanSpeedRatioDesc
|
2021-05-19 21:27:29 +00:00
|
|
|
ch <- fanSpeedStateDesc
|
|
|
|
ch <- temperatureDesc
|
|
|
|
ch <- temperatureStateDesc
|
|
|
|
ch <- voltageDesc
|
|
|
|
ch <- voltageStateDesc
|
|
|
|
ch <- currentDesc
|
|
|
|
ch <- currentStateDesc
|
|
|
|
ch <- powerDesc
|
|
|
|
ch <- powerStateDesc
|
|
|
|
}
|
|
|
|
|
2021-11-14 10:32:52 +00:00
|
|
|
func collectTypedSensor(ch chan<- prometheus.Metric, desc, stateDesc *prometheus.Desc, state float64, data freeipmi.SensorData, scale float64) {
|
2021-05-19 21:27:29 +00:00
|
|
|
ch <- prometheus.MustNewConstMetric(
|
|
|
|
desc,
|
|
|
|
prometheus.GaugeValue,
|
2021-11-14 10:32:52 +00:00
|
|
|
data.Value*scale,
|
2021-05-19 21:27:29 +00:00
|
|
|
strconv.FormatInt(data.ID, 10),
|
|
|
|
data.Name,
|
|
|
|
)
|
|
|
|
ch <- prometheus.MustNewConstMetric(
|
|
|
|
stateDesc,
|
|
|
|
prometheus.GaugeValue,
|
|
|
|
state,
|
|
|
|
strconv.FormatInt(data.ID, 10),
|
|
|
|
data.Name,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
func collectGenericSensor(ch chan<- prometheus.Metric, state float64, data freeipmi.SensorData) {
|
|
|
|
ch <- prometheus.MustNewConstMetric(
|
|
|
|
sensorValueDesc,
|
|
|
|
prometheus.GaugeValue,
|
|
|
|
data.Value,
|
|
|
|
strconv.FormatInt(data.ID, 10),
|
|
|
|
data.Name,
|
|
|
|
data.Type,
|
|
|
|
)
|
|
|
|
ch <- prometheus.MustNewConstMetric(
|
|
|
|
sensorStateDesc,
|
|
|
|
prometheus.GaugeValue,
|
|
|
|
state,
|
|
|
|
strconv.FormatInt(data.ID, 10),
|
|
|
|
data.Name,
|
|
|
|
data.Type,
|
|
|
|
)
|
|
|
|
}
|