From 6ae95f2ad0e73c0c6e86c1845fe061bc142be509 Mon Sep 17 00:00:00 2001 From: rhartmann <2409662+rhartmann@users.noreply.github.com> Date: Wed, 13 Nov 2024 14:43:36 +0100 Subject: [PATCH 1/2] vSphere: Ignore VM templates Skip agent output for VM templates. This removes VM templates from discovery and avoids their piggyback data. --- cmk/special_agents/agent_vsphere.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/cmk/special_agents/agent_vsphere.py b/cmk/special_agents/agent_vsphere.py index 48ec6a1a591..8b84f8bb3a2 100644 --- a/cmk/special_agents/agent_vsphere.py +++ b/cmk/special_agents/agent_vsphere.py @@ -1880,6 +1880,9 @@ def fetch_virtual_machines( if vm_data.get("summary.config.ftInfo.role") == "2": continue # This response coming from the passive fault-tolerance node + if vm_data.get("config.template") == "true": + continue + if "runtime.host" in vm_data: vm_data["runtime.host"] = hostsystems.get( vm_data["runtime.host"], vm_data["runtime.host"] From 075c2130184ab512efc44e49dfd32da9880a390c Mon Sep 17 00:00:00 2001 From: Ringo Hartmann <2409662+rhartmann@users.noreply.github.com> Date: Thu, 14 Nov 2024 11:27:50 +0100 Subject: [PATCH 2/2] vSphere: Add agent option to ignore templates Make ignoring templates optional (command-line argument). This adds backwards compatibilty: templates do not disappear unless opted-in. --- cmk/plugins/vsphere/rulesets/special_agent.py | 13 +++++++++++++ .../vsphere/server_side_calls/special_agent.py | 4 ++++ cmk/special_agents/agent_vsphere.py | 11 ++++++++++- .../vsphere/server_side_calls/test_special_agent.py | 4 ++++ tests/unit/cmk/special_agents/test_sa_vsphere.py | 2 ++ 5 files changed, 33 insertions(+), 1 deletion(-) diff --git a/cmk/plugins/vsphere/rulesets/special_agent.py b/cmk/plugins/vsphere/rulesets/special_agent.py index fcc0fbd05a7..63b58682bc9 100644 --- a/cmk/plugins/vsphere/rulesets/special_agent.py +++ b/cmk/plugins/vsphere/rulesets/special_agent.py @@ -147,6 +147,19 @@ def parameter_form() -> Dictionary: ), ), ), + "ignore_templates": DictElement( + required=True, + parameter_form=BooleanChoice( + title=Title("Templates"), + label=Label("Do not monitor VM templates"), + prefill=DefaultValue(False), + help_text=Help( + "A template is created by converting a stopped VM. It cannot be started or modified " + "without converting or cloning it back to a VM. This option tells the vSphere agent " + "to exclude template VMs in its output." + ), + ), + ), "host_pwr_display": DictElement( required=False, parameter_form=SingleChoice( diff --git a/cmk/plugins/vsphere/server_side_calls/special_agent.py b/cmk/plugins/vsphere/server_side_calls/special_agent.py index c9cc49f46f7..b78a8f3966c 100644 --- a/cmk/plugins/vsphere/server_side_calls/special_agent.py +++ b/cmk/plugins/vsphere/server_side_calls/special_agent.py @@ -30,6 +30,7 @@ class Params(BaseModel): timeout: int | None = None infos: Sequence[str] skip_placeholder_vms: bool + ignore_templates: bool host_pwr_display: str | None = None vm_pwr_display: str | None = None snapshots_on_host: bool @@ -55,6 +56,9 @@ def commands_function( # pylint: disable=too-many-branches if params.skip_placeholder_vms: command_arguments.append("-P") + + if params.ignore_templates: + command_arguments.append("-T") if params.spaces: command_arguments += ["--spaces", params.spaces] diff --git a/cmk/special_agents/agent_vsphere.py b/cmk/special_agents/agent_vsphere.py index 8b84f8bb3a2..71a4afa6925 100644 --- a/cmk/special_agents/agent_vsphere.py +++ b/cmk/special_agents/agent_vsphere.py @@ -937,6 +937,15 @@ def parse_arguments(argv: Sequence[str]) -> argparse.Namespace: help="""Skip placeholder virtualmachines. These backup vms are created by the Site Recovery Manager (SRM) and are identified by not having any assigned virtual disks.""", ) + parser.add_argument( + "-T", + "--ignore-templates", + action="store_true", + help="""Ignore Template VMs. A Template is created by converting a stopped VM. + It cannot be started or modified without converting or cloning it back to a VM. + + This option suppresses all agent output of Template VMs.""", + ) # optional arguments parser.add_argument( @@ -1880,7 +1889,7 @@ def fetch_virtual_machines( if vm_data.get("summary.config.ftInfo.role") == "2": continue # This response coming from the passive fault-tolerance node - if vm_data.get("config.template") == "true": + if opt.ignore_templates and vm_data.get("config.template") == "true": continue if "runtime.host" in vm_data: diff --git a/tests/unit/cmk/plugins/vsphere/server_side_calls/test_special_agent.py b/tests/unit/cmk/plugins/vsphere/server_side_calls/test_special_agent.py index bbd5feb25df..1b9cc584715 100644 --- a/tests/unit/cmk/plugins/vsphere/server_side_calls/test_special_agent.py +++ b/tests/unit/cmk/plugins/vsphere/server_side_calls/test_special_agent.py @@ -25,6 +25,7 @@ "tcp_port": 443, "direct": "host_system", "skip_placeholder_vms": True, + "ignore_templates": True, "ssl": False, "secret": Secret(23), "spaces": "cut", @@ -45,6 +46,7 @@ "--hostname", "host", "-P", + "-T", "--spaces", "cut", "--no-cert-check", @@ -61,6 +63,7 @@ "direct": "host_system", "vm_piggyname": "alias", "skip_placeholder_vms": True, + "ignore_templates": True, "ssl": False, "secret": Secret(id=1, pass_safely=True), "spaces": "cut", @@ -81,6 +84,7 @@ "--hostname", "host", "-P", + "-T", "--spaces", "cut", "--vm_piggyname", diff --git a/tests/unit/cmk/special_agents/test_sa_vsphere.py b/tests/unit/cmk/special_agents/test_sa_vsphere.py index c863a1776ba..3e870001bc8 100644 --- a/tests/unit/cmk/special_agents/test_sa_vsphere.py +++ b/tests/unit/cmk/special_agents/test_sa_vsphere.py @@ -18,6 +18,7 @@ "port": 443, "hostname": None, "skip_placeholder_vm": False, + "ignore_templates": False, "host_pwr_display": None, "vm_pwr_display": None, "snapshots_on_host": False, @@ -45,6 +46,7 @@ (["--hostname", "myHost"], {"hostname": "myHost"}), (["-H", "myHost"], {"hostname": "myHost"}), (["-P"], {"skip_placeholder_vm": True}), + (["-T"], {"ignore_templates": True}), (["--host_pwr_display", "vm"], {"host_pwr_display": "vm"}), (["--vm_pwr_display", "esxhost"], {"vm_pwr_display": "esxhost"}), (["--snapshots-on-host"], {"snapshots_on_host": True}),