diff --git a/CHANGELOG.md b/CHANGELOG.md index 1c6b0c4..d6b4323 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,8 @@ +## next + +* Now, `ipmi_dcmi_power_consumption_watts` metric is not present if Power +Measurement feature is not present. Before this change - the value was zero + ## 1.6.1 / 2022-06-17 * Another "I screwed up the release" release diff --git a/collector_dcmi.go b/collector_dcmi.go index 0e95f8b..7684eef 100644 --- a/collector_dcmi.go +++ b/collector_dcmi.go @@ -53,10 +53,13 @@ func (c DCMICollector) Collect(result freeipmi.Result, ch chan<- prometheus.Metr level.Error(logger).Log("msg", "Failed to collect DCMI data", "target", targetName(target.host), "error", err) return 0, err } - ch <- prometheus.MustNewConstMetric( - powerConsumptionDesc, - prometheus.GaugeValue, - currentPowerConsumption, - ) + // Returned value negative == Power Measurement is not avail + if currentPowerConsumption > -1 { + ch <- prometheus.MustNewConstMetric( + powerConsumptionDesc, + prometheus.GaugeValue, + currentPowerConsumption, + ) + } return 1, nil } diff --git a/docs/metrics.md b/docs/metrics.md index 8fcef3f..5df88ea 100644 --- a/docs/metrics.md +++ b/docs/metrics.md @@ -41,7 +41,7 @@ case it will be exported as `"N/A"`. This metric is only provided if the `chassis` collector is enabled. The metric `ipmi_chassis_power_state` shows the current chassis power state of -the machine. The value is 1 for power on, and 0 otherwise. +the machine. The value is 1 for power on, and 0 otherwise. ## Power consumption @@ -159,7 +159,7 @@ explicit [power consumption metrics](#power_consumption) for this. ### Generic sensors For all sensors that can not be classified, two generic metrics are exported, -the state and the value. However, to provide a little more context, the sensor +the state and the value. However, to provide a little more context, the sensor type is added as label (in addition to name and ID). Example: ipmi_sensor_state{id="139",name="Power Cable",type="Cable/Interconnect"} 0 diff --git a/freeipmi/freeipmi.go b/freeipmi/freeipmi.go index 0231aee..1591c58 100644 --- a/freeipmi/freeipmi.go +++ b/freeipmi/freeipmi.go @@ -33,6 +33,7 @@ import ( ) var ( + ipmiDCMIPowerMeasurementRegex = regexp.MustCompile(`^Power Measurement\s*:\s*(?PActive|Not\sAvailable).*`) ipmiDCMICurrentPowerRegex = regexp.MustCompile(`^Current Power\s*:\s*(?P[0-9.]*)\s*Watts.*`) ipmiChassisPowerRegex = regexp.MustCompile(`^System Power\s*:\s(?P.*)`) ipmiChassisDriveFaultRegex = regexp.MustCompile(`^Drive Fault\s*:\s(?P.*)`) @@ -200,11 +201,20 @@ 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) + // Check for Power Measurement are avail + value, err := getValue(ipmiOutput.output, ipmiDCMIPowerMeasurementRegex) if err != nil { return -1, err } - return strconv.ParseFloat(value, 64) + // When Power Measurement in 'Active' state - we can get watts + if value == "Active" { + value, err := getValue(ipmiOutput.output, ipmiDCMICurrentPowerRegex) + if err != nil { + return -1, err + } + return strconv.ParseFloat(value, 64) + } + return -1, nil } func GetChassisPowerState(ipmiOutput Result) (float64, error) {