Skip to content
Permalink
Browse files

data_sources/ipmi.py: factor out parsing of sensor reading

and add a test.

Change-Id: Icdfec8ad2db9903693d71d6c63f9589426fe525e
  • Loading branch information...
mo-ki committed Jul 4, 2019
1 parent b1c1654 commit 833fa78f772fb8fec4bea3c8c3697541f4a126e4
Showing with 51 additions and 28 deletions.
  1. +28 −28 cmk_base/data_sources/ipmi.py
  2. +23 −0 tests/unit/cmk_base/data_sources/test_ipmi.py
@@ -140,40 +140,40 @@ def _fetch_ipmi_sensors_section(self, connection):
continue

reading = sdr.sensors[number].decode_sensor_reading(rsp['data'])
if reading is None:
continue

# {'states': [], 'health': 0, 'name': 'CPU1 Temp', 'imprecision': 0.5,
# 'units': '\xc2\xb0C', 'state_ids': [], 'type': 'Temperature',
# 'value': 25.0, 'unavailable': 0}]]
health_txt = "N/A"
if reading.health >= ipmi_const.Health.Failed:
health_txt = "FAILED"
elif reading.health >= ipmi_const.Health.Critical:
health_txt = "CRITICAL"
elif reading.health >= ipmi_const.Health.Warning:
health_txt = "WARNING"
# workaround for pyghmi bug: https://bugs.launchpad.net/pyghmi/+bug/1790120
health_txt = _handle_false_positive_warnings(reading)
elif reading.health == ipmi_const.Health.Ok:
health_txt = "OK"

parts = [
"%d" % number,
reading.name,
reading.type,
("%0.2f" % reading.value) if reading.value else "N/A",
reading.units if reading.units != "\xc2\xb0C" else "C",
health_txt,
]

sensors.append(parts)
if reading is not None:
sensors.append(self._parse_sensor_reading(number, reading))

output = "<<<mgmt_ipmi_sensors:sep(124)>>>\n" \
+ "".join([ "|".join(sensor) + "\n" for sensor in sensors ])

return output

@staticmethod
def _parse_sensor_reading(number, reading):
# {'states': [], 'health': 0, 'name': 'CPU1 Temp', 'imprecision': 0.5,
# 'units': '\xc2\xb0C', 'state_ids': [], 'type': 'Temperature',
# 'value': 25.0, 'unavailable': 0}]]
health_txt = "N/A"
if reading.health >= ipmi_const.Health.Failed:
health_txt = "FAILED"
elif reading.health >= ipmi_const.Health.Critical:
health_txt = "CRITICAL"
elif reading.health >= ipmi_const.Health.Warning:
health_txt = "WARNING"
# workaround for pyghmi bug: https://bugs.launchpad.net/pyghmi/+bug/1790120
health_txt = _handle_false_positive_warnings(reading)
elif reading.health == ipmi_const.Health.Ok:
health_txt = "OK"

return [
"%d" % number,
reading.name,
reading.type,
("%0.2f" % reading.value) if reading.value else "N/A",
reading.units if reading.units != "\xc2\xb0C" else "C",
health_txt,
]

def _fetch_ipmi_firmware_section(self, connection):
self._logger.debug("Fetching firmware information via UDP from %s:623" % (self._ipaddress))
try:
@@ -0,0 +1,23 @@
from collections import namedtuple

import pytest # type: ignore

from cmk_base.data_sources.ipmi import IPMIManagementBoardDataSource

SensorReading = namedtuple(
"SensorReading", "states health name imprecision units"
" state_ids type value unavailable")


@pytest.mark.parametrize(
"reading, parsed",
[
# standard case
(SensorReading(['lower non-critical threshold'], 1, "Hugo", None, "", [42], "hugo-type",
None, 0), ["0", "Hugo", "hugo-type", "N/A", "", "WARNING"]),
# false positive (no non-critical state): let state come through
(SensorReading(['Present'], 1, "Dingeling", 0.2, "\xc2\xb0C", [], "FancyDevice", 3.14159265,
1), ["0", "Dingeling", "FancyDevice", "3.14", "C", "Present"]),
])
def test_ipmi_parse_sensor_reading(reading, parsed):
assert IPMIManagementBoardDataSource._parse_sensor_reading(0, reading) == parsed

0 comments on commit 833fa78

Please sign in to comment.
You can’t perform that action at this time.