Skip to content

Commit

Permalink
hyperv_vms: make params a dict
Browse files Browse the repository at this point in the history
Change-Id: I01d5ac17d16b3663480649bbb793e7c162685ee9
  • Loading branch information
mo-ki committed Nov 17, 2023
1 parent d3beab9 commit 1dc9f78
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 87 deletions.
76 changes: 42 additions & 34 deletions cmk/base/legacy_checks/hyperv_vms.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,38 +86,39 @@ def parse_hyperv_vms(string_table):


def inventory_hyperv_vms(parsed):
return [(vm_name, {"state": vm["state"]}) for (vm_name, vm) in parsed.items()]
return [(vm_name, {"discovered_state": vm["state"]}) for (vm_name, vm) in parsed.items()]


def check_hyperv_vms(item, params, parsed):
if not (vm := parsed.get(item)):
return

compare_mode, target_states = params["vm_target_state"]
# compare against discovered VM state
if params.get("compare_discovery"):
discovery_state = params.get("state")
if compare_mode == "discovery":
discovered_state = params.get("discovered_state")

# this means that the check is executed as a manual check
if discovery_state is None:
if discovered_state is None:
yield 3, "State is {} ({}), discovery state is not available".format(
vm["state"],
vm["state_msg"],
)
return

if vm["state"] == discovery_state:
if vm["state"] == discovered_state:
yield 0, "State {} ({}) matches discovery".format(vm["state"], vm["state_msg"])
return

yield 2, "State {} ({}) does not match discovery ({})".format(
vm["state"],
vm["state_msg"],
params["state"],
discovered_state,
)
return

# service state defined in rule
service_state = params.get(vm["state"])
service_state = target_states.get(vm["state"])

# as a precaution, if in the future there are new VM states we do not know about
if service_state is None:
Expand All @@ -126,37 +127,44 @@ def check_hyperv_vms(item, params, parsed):
yield service_state, "State is {} ({})".format(vm["state"], vm["state_msg"])


DEFAULT_PARAMETERS = {
"vm_target_state": (
"map",
{
"FastSaved": 0,
"FastSavedCritical": 2,
"FastSaving": 0,
"FastSavingCritical": 2,
"Off": 1,
"OffCritical": 2,
"Other": 3,
"Paused": 0,
"PausedCritical": 2,
"Pausing": 0,
"PausingCritical": 2,
"Reset": 1,
"ResetCritical": 2,
"Resuming": 0,
"ResumingCritical": 2,
"Running": 0,
"RunningCritical": 2,
"Saved": 0,
"SavedCritical": 2,
"Saving": 0,
"SavingCritical": 2,
"Starting": 0,
"StartingCritical": 2,
"Stopping": 1,
"StoppingCritical": 2,
},
),
}

check_info["hyperv_vms"] = LegacyCheckDefinition(
parse_function=parse_hyperv_vms,
service_name="VM %s",
discovery_function=inventory_hyperv_vms,
check_function=check_hyperv_vms,
check_ruleset_name="hyperv_vms",
check_default_parameters={
"FastSaved": 0,
"FastSavedCritical": 2,
"FastSaving": 0,
"FastSavingCritical": 2,
"Off": 1,
"OffCritical": 2,
"Other": 3,
"Paused": 0,
"PausedCritical": 2,
"Pausing": 0,
"PausingCritical": 2,
"Reset": 1,
"ResetCritical": 2,
"Resuming": 0,
"ResumingCritical": 2,
"Running": 0,
"RunningCritical": 2,
"Saved": 0,
"SavedCritical": 2,
"Saving": 0,
"SavingCritical": 2,
"Starting": 0,
"StartingCritical": 2,
"Stopping": 1,
"StoppingCritical": 2,
},
check_default_parameters=DEFAULT_PARAMETERS,
)
65 changes: 55 additions & 10 deletions cmk/gui/plugins/wato/check_parameters/hyperv_vms.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,14 @@
rulespec_registry,
RulespecGroupCheckParametersApplications,
)
from cmk.gui.valuespec import Alternative, Dictionary, FixedValue, MonitoringState, TextInput
from cmk.gui.valuespec import (
CascadingDropdown,
Dictionary,
FixedValue,
Migrate,
MonitoringState,
TextInput,
)

# these default values were suggested by Aldi Sued
VM_STATES_DEFVALS = [
Expand Down Expand Up @@ -51,7 +58,6 @@ def _item_spec_hyperv_vms():

def _expected_state_map() -> Dictionary:
return Dictionary(
title=_("Direct mapping of VM state to monitoring state"),
help=_(
"Define a direct translation of the possible states of the VM to monitoring "
"states, i.e. to the result of the check. This overwrites the default "
Expand All @@ -73,7 +79,7 @@ def _expected_state_map() -> Dictionary:

def _discovery_state() -> FixedValue:
return FixedValue(
value={"compare_discovery": True},
value=True,
title=_("Compare against discovered state"),
totext=_("Compare the current state of the VM against the discovered state"),
help=_(
Expand All @@ -86,16 +92,55 @@ def _discovery_state() -> FixedValue:
)


def _parameter_valuespec_hyperv_vms() -> Alternative:
return Alternative(
title=_("Translation of VM state to monitoring state"),
elements=[
_expected_state_map(),
_discovery_state(),
],
def _parameter_valuespec_hyperv_vms() -> Migrate:
return Migrate(
valuespec=Dictionary(
elements=[
(
"vm_target_state",
CascadingDropdown(
title=_("Translation of VM state to monitoring state"),
choices=[
(
"map",
_("Direct mapping of VM state to monitoring state"),
_expected_state_map(),
),
(
"discovery",
_("Compare against discovered state"),
_discovery_state(),
),
],
),
),
(
"discovered_state",
TextInput(title=_("State during discovery of the service")),
),
],
optional_keys=["discovered_state"],
hidden_keys=["discovered_state"], # not shown when editing the rule
),
migrate=_migrate_hyperv_vmstate,
)


def _migrate_hyperv_vmstate(p: dict) -> dict:
if "vm_target_state" in p or "discovered_state" in p:
return p
if set(p) == {"state"}: # properly migrate autochecks:
return {"discovered_state": p["state"]}
new = {
"vm_target_state": ("discovery", True)
if "compare_discovery" in p
else ("map", {k: v for k, v in p.items() if k != "state"})
}
if "state" in p:
new["discovered_state"] = p["state"]
return new


rulespec_registry.register(
CheckParameterRulespecWithItem(
check_group_name="hyperv_vms",
Expand Down
58 changes: 15 additions & 43 deletions tests/unit/checks/generictests/datasets/hyperv_vms.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,36 +6,8 @@
# fmt: off
# mypy: disable-error-code=var-annotated

from typing import Any

_defaults: dict[str, Any] = {
"FastSaved": 0,
"FastSavedCritical": 2,
"FastSaving": 0,
"FastSavingCritical": 2,
"Off": 1,
"OffCritical": 2,
"Other": 3,
"Paused": 0,
"PausedCritical": 2,
"Pausing": 0,
"PausingCritical": 2,
"Reset": 1,
"ResetCritical": 2,
"Resuming": 0,
"ResumingCritical": 2,
"Running": 0,
"RunningCritical": 2,
"Saved": 0,
"SavedCritical": 2,
"Saving": 0,
"SavingCritical": 2,
"Starting": 0,
"StartingCritical": 2,
"Stopping": 1,
"StoppingCritical": 2,
}

from cmk.base.legacy_checks.hyperv_vms import DEFAULT_PARAMETERS

checkname = "hyperv_vms"

Expand All @@ -56,13 +28,13 @@

discovery = {
"": [
("Q-WSUS", {"state": "Running"}),
("AUN-CAA", {"state": "Off"}),
("weg-ca-webserver", {"state": "Wrong"}),
("z4058044_snap (23.05.2014 - 09:29:29)", {"state": "Running"}),
("z230897", {"state": "Stopping"}),
("hlv2", {"state": "UnknownState"}),
("hlv3", {"state": "Running"}),
("Q-WSUS", {"discovered_state": "Running"}),
("AUN-CAA", {"discovered_state": "Off"}),
("weg-ca-webserver", {"discovered_state": "Wrong"}),
("z4058044_snap (23.05.2014 - 09:29:29)", {"discovered_state": "Running"}),
("z230897", {"discovered_state": "Stopping"}),
("hlv2", {"discovered_state": "UnknownState"}),
("hlv3", {"discovered_state": "Running"}),
]
}

Expand All @@ -71,33 +43,33 @@
"": [
(
"Q-WSUS",
{"state": "Running", **_defaults},
{"discovered_state": "Running", **DEFAULT_PARAMETERS},
[(0, "State is Running (Operating normally)")],
),
("AUN-CAA", {"state": "Off", **_defaults}, [(1, "State is Off (Operating normally)")]),
("AUN-CAA", {"state": "Off", **DEFAULT_PARAMETERS}, [(1, "State is Off (Operating normally)")]),
(
"weg-ca-webserver",
{"state": "Wrong", **_defaults},
{"discovered_state": "Wrong", **DEFAULT_PARAMETERS},
[(3, "Unknown state Wrong (Operating normally)")],
),
(
"z4058044_snap (23.05.2014 - 09:29:29)",
{"state": "Running", **_defaults, "compare_discovery": True},
{"discovered_state": "Running", **DEFAULT_PARAMETERS, "vm_target_state": ("discovery", True)},
[(0, "State Running (Operating normally) matches discovery")],
),
(
"z230897",
{"state": "Running", **_defaults, "compare_discovery": True},
{"discovered_state": "Running", **DEFAULT_PARAMETERS, "vm_target_state": ("discovery", True)},
[(2, "State Stopping (VM crashed) does not match discovery (Running)")],
),
(
"hlv2",
{"state": "UnknownState", **_defaults, "compare_discovery": True},
{"discovered_state": "UnknownState", **DEFAULT_PARAMETERS, "vm_target_state": ("discovery", True)},
[(0, "State UnknownState (Totally normal) matches discovery")],
),
(
"hlv3",
{**_defaults, "compare_discovery": True},
{**DEFAULT_PARAMETERS, "vm_target_state": ("discovery", True)},
[(3, "State is Running (Operating normally), discovery state is not available")],
),
]
Expand Down

0 comments on commit 1dc9f78

Please sign in to comment.