-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add metrics ipmi_dcmi_power_consumption_rate (#45)
* feat: Add metrics ipmi_dcmi_power_consumption_rate Add new ipmi-dcmi metrics Rate = power consumption / maximum power consumption * fix: Replace lshw with dmidecode * feat: Add cache for dmidecode get_power_capacities Change wording and add cache for dmidecode get_power_capacities * test: Add unit tests * test: Make test cases more comprehensive * docs: Update doc-string * docs: Update the command for max power capacity formula
- Loading branch information
Showing
8 changed files
with
261 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
"""Dmidecode metrics collector.""" | ||
import re | ||
from functools import lru_cache | ||
from logging import getLogger | ||
from typing import List | ||
|
||
from ..utils import Command | ||
|
||
logger = getLogger(__name__) | ||
|
||
|
||
MAX_POWER_CAPACITY_REGEX = r"(Max Power Capacity: )(\d+)( W)" | ||
|
||
|
||
class Dmidecode(Command): | ||
"""Command line tool for dmidecode.""" | ||
|
||
prefix = "" | ||
command = "dmidecode" | ||
|
||
@lru_cache # PSU ratings won't change over the lifetime of a server | ||
def get_power_capacities(self) -> List[int]: | ||
"""Get list of power capacities.""" | ||
result = self("-t 39") | ||
if result.error: | ||
logger.error(result.error) | ||
return [] | ||
|
||
lines = re.findall(MAX_POWER_CAPACITY_REGEX, result.data) | ||
powers = [] | ||
for line in lines: | ||
powers.append(int(line[1])) | ||
return powers |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import unittest | ||
from unittest.mock import patch | ||
|
||
from prometheus_hardware_exporter.collectors.dmidecode import Dmidecode | ||
from prometheus_hardware_exporter.utils import Command, Result | ||
|
||
TYPE_39_OUTPUT = "tests/unit/test_resources/dmidecode/dmidecode_type_39_output.txt" | ||
|
||
|
||
class TestDmidecode(unittest.TestCase): | ||
"""Test the Dmidecode class.""" | ||
|
||
@patch.object(Command, "__call__") | ||
def test_00_get_power_capacities_success(self, mock_call): | ||
with open(TYPE_39_OUTPUT, "r") as content: | ||
mock_call.return_value = Result(content.read(), None) | ||
dmidecode = Dmidecode() | ||
power_capacities = dmidecode.get_power_capacities() | ||
self.assertEqual(power_capacities, [1400, 1400]) | ||
|
||
@patch.object(Command, "__call__") | ||
def test_01_get_power_capacities_error(self, mock_call): | ||
mock_call.return_value = Result("", True) | ||
|
||
dmidecode = Dmidecode() | ||
power_capacities = dmidecode.get_power_capacities() | ||
self.assertEqual(power_capacities, []) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
tests/unit/test_resources/dmidecode/dmidecode_type_39_output.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
# dmidecode 3.3 | ||
Getting SMBIOS data from sysfs. | ||
SMBIOS 3.3.0 present. | ||
|
||
Handle 0x2700, DMI type 39, 22 bytes | ||
System Power Supply | ||
Location: Not Specified | ||
Name: PWR SPLY,1400W,RDNT,LTON | ||
Manufacturer: DELL | ||
Serial Number: CNLOD0019M3A6D | ||
Asset Tag: Not Specified | ||
Model Part Number: 01CW9GA04 | ||
Revision: Not Specified | ||
Max Power Capacity: 1400 W | ||
Status: Present, Unknown | ||
Type: Unknown | ||
Input Voltage Range Switching: Unknown | ||
Plugged: Yes | ||
Hot Replaceable: Yes | ||
|
||
Handle 0x2701, DMI type 39, 22 bytes | ||
System Power Supply | ||
Location: Not Specified | ||
Name: PWR SPLY,1400W,RDNT,LTON | ||
Manufacturer: DELL | ||
Serial Number: CNLOD0019M369D | ||
Asset Tag: Not Specified | ||
Model Part Number: 01CW9GA04 | ||
Revision: Not Specified | ||
Max Power Capacity: 1400 W | ||
Status: Present, Unknown | ||
Type: Unknown | ||
Input Voltage Range Switching: Unknown | ||
Plugged: Yes | ||
Hot Replaceable: Yes |
3 changes: 3 additions & 0 deletions
3
tests/unit/test_resources/ipmi/ipmitool_sdr_ps_sample_output.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
PS Redundancy,77h,ok,7.1,Fully Redundant | ||
Status,85h,ok,10.1,Presence detected | ||
Status,86h,ok,10.2,Presence detected |