Skip to content

Commit

Permalink
17045 FIX gude_relayport: Fix section parsing
Browse files Browse the repository at this point in the history
Parsing of the section for the 'gude_relayport' used to crash, because of wrong OIDs.
This has now been fixed.

SUP-22135

Change-Id: I6b35122ec3061e1b8fce0ea0d51d396139efa317
(cherry picked from commit 022643a882c36b5afb82ead0e7f95936965392a1)
  • Loading branch information
racicLuka committed Feb 12, 2025
1 parent b5afd88 commit 3d7b1e2
Show file tree
Hide file tree
Showing 6 changed files with 406 additions and 82 deletions.
16 changes: 16 additions & 0 deletions .werks/17045.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
[//]: # (werk v2)
# gude_relayport: Fix section parsing

key | value
---------- | ---
date | 2025-02-06T23:28:25+00:00
version | 2.3.0p27
class | fix
edition | cre
component | checks
level | 1
compatible | yes

Parsing of the section for the 'gude_relayport' used to crash when the device was off or did not deliver values.
This has now been fixed.

81 changes: 0 additions & 81 deletions cmk/base/legacy_checks/gude_relayport.py

This file was deleted.

107 changes: 107 additions & 0 deletions cmk/plugins/gude/agent_based/gude_relayport.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
#!/usr/bin/env python3
# Copyright (C) 2025 Checkmk GmbH - License: GNU General Public License v2
# This file is part of Checkmk (https://checkmk.com). It is subject to the terms and
# conditions defined in the file COPYING, which is part of this source code package.


from collections.abc import Mapping
from dataclasses import asdict, dataclass

from cmk.agent_based.v2 import (
CheckPlugin,
CheckResult,
DiscoveryResult,
Service,
SimpleSNMPSection,
SNMPTree,
startswith,
StringTable,
)
from cmk.plugins.lib.elphase import check_elphase, CheckParams

# .1.3.6.1.4.1.28507.38.1.3.1.2.1.2.1 TWTA 2 --> GUDEADS-EPC822X-MIB::epc822XPortName.1
# .1.3.6.1.4.1.28507.38.1.3.1.2.1.3.1 0 --> GUDEADS-EPC822X-MIB::epc822XPortState.1
# .1.3.6.1.4.1.28507.38.1.5.1.2.1.4.1 0 --> GUDEADS-EPC822X-MIB::epc822XspPowerActive.1
# .1.3.6.1.4.1.28507.38.1.5.1.2.1.5.1 0 --> GUDEADS-EPC822X-MIB::epc822XspCurrent.1
# .1.3.6.1.4.1.28507.38.1.5.1.2.1.6.1 228 --> GUDEADS-EPC822X-MIB::epc822XspVoltage.1
# .1.3.6.1.4.1.28507.38.1.5.1.2.1.7.1 4995 --> GUDEADS-EPC822X-MIB::epc822XspFrequency.1
# .1.3.6.1.4.1.28507.38.1.5.1.2.1.10.1 0 --> GUDEADS-EPC822X-MIB::epc822XspPowerApparent.1

_STATE_MAP = {
"0": (2, "off"),
"1": (0, "on"),
}


@dataclass(frozen=True, kw_only=True)
class RelayPort:
device_state: tuple[int, str]
power: float | None
current: float | None
voltage: float | None
frequency: float | None
appower: float | None


Section = Mapping[str, RelayPort]


def parse_gude_relayport(string_table: StringTable) -> Section:
return {
portname: RelayPort(
device_state=_STATE_MAP[state],
power=float(power) if power else None,
current=float(current) * 0.001 if current else None,
voltage=float(voltage) if voltage else None,
frequency=float(freq) * 0.01 if freq else None,
appower=float(appower) if appower else None,
)
for portname, state, power, current, voltage, freq, appower in string_table
}


snmp_section_gude_relayport = SimpleSNMPSection(
name="gude_relayport",
detect=startswith(".1.3.6.1.2.1.1.2.0", ".1.3.6.1.4.1.28507.38"),
fetch=SNMPTree(
base=".1.3.6.1.4.1.28507.38.1",
oids=[
"3.1.2.1.2", # GUDEADS-EPC822X-MIB::epc822XPortName.1
"3.1.2.1.3", # GUDEADS-EPC822X-MIB::epc822XPortState.1
"5.5.2.1.4", # GUDEADS-EPC822X-MIB::epc822XspPowerActive.1
"5.5.2.1.5", # GUDEADS-EPC822X-MIB::epc822XspCurrent.1
"5.5.2.1.6", # GUDEADS-EPC822X-MIB::epc822XspVoltage.1
"5.5.2.1.7", # GUDEADS-EPC822X-MIB::epc822XspFrequency.1
"5.5.2.1.10", # GUDEADS-EPC822X-MIB::epc822XspPowerApparent.1
],
),
parse_function=parse_gude_relayport,
)


def discover_gude_relayport(
section: Section,
) -> DiscoveryResult:
yield from (Service(item=relayport) for relayport in section)


def check_gude_relayport(item: str, params: CheckParams, section: Section) -> CheckResult:
if (relayport := section.get(item)) is None:
return

yield from check_elphase(
item, params, {item: {k: v for k, v in asdict(relayport).items() if v is not None}}
)


check_plugin_gude_relayport = CheckPlugin(
name="gude_relayport",
service_name="Relay port %s",
discovery_function=discover_gude_relayport,
check_function=check_gude_relayport,
check_ruleset_name="el_inphase",
check_default_parameters={
"voltage": (220, 210),
"current": (15, 16),
},
)
File renamed without changes.
1 change: 0 additions & 1 deletion tests/unit/checks/test_generic_legacy_conversion.py
Original file line number Diff line number Diff line change
Expand Up @@ -714,7 +714,6 @@ def test_no_new_or_vanished_legacy_checks(fix_plugin_legacy: FixPluginLegacy) ->
"graylog_sources",
"gude_humidity",
"gude_powerbanks",
"gude_relayport",
"gude_temp",
"h3c_lanswitch_cpu",
"h3c_lanswitch_sensors",
Expand Down
Loading

0 comments on commit 3d7b1e2

Please sign in to comment.