def host_labels(section: NodeInfo) -> HostLabelGenerator: """Host label function Labels: cmk/kubernetes/object: This label is set to the Kubernetes object type. cmk/kubernetes/cluster: This label is set to the given Kubernetes cluster name. cmk/kubernetes/node: This label contains the name of the Kubernetes Node this checkmk host is associated with. Checkmk hosts of the type Pod and Node will be assigned this label. cmk/kubernetes/annotation/{key}:{value} : These labels are yielded for each Kubernetes annotation that is a valid Kubernetes label. This can be configured via the rule 'Kubernetes'. cmk/os_family: This label is set to the operating system as reported by the agent as "AgentOS" (such as "windows" or "linux"). """ yield HostLabel("cmk/kubernetes/object", "node") yield HostLabel("cmk/kubernetes/cluster", section.cluster) yield HostLabel("cmk/kubernetes/node", section.name) yield HostLabel("cmk/os_family", section.operating_system) yield from kube_labels_to_cmk_labels(section.labels) yield from kube_annotations_to_cmk_labels(section.annotations)
def host_labels(section: NamespaceInfo) -> HostLabelGenerator: """Host label function Labels: cmk/kubernetes/object: This label is set to the Kubernetes object type. cmk/kubernetes/cluster: This label is set to the given Kubernetes cluster name. cmk/kubernetes/namespace: This label contains the name of the Kubernetes Namespace this checkmk host is associated with. cmk/kubernetes/annotation/{key}:{value} : These labels are yielded for each Kubernetes annotation that is a valid Kubernetes label. This can be configured via the rule 'Kubernetes'. """ yield HostLabel("cmk/kubernetes/object", "namespace") yield HostLabel("cmk/kubernetes/cluster", section.cluster) yield HostLabel("cmk/kubernetes/namespace", section.name) yield from kube_labels_to_cmk_labels(section.labels) yield from kube_annotations_to_cmk_labels(section.annotations)
def _host_labels(section: Info) -> HostLabelGenerator: """Host label function. Labels: cmk/kubernetes/object: This label is set to the Kubernetes object type. cmk/kubernetes/cluster: This label is set to the given Kubernetes Cluster name. cmk/kubernetes/namespace: This label contains the name of the Kubernetes Namespace this checkmk host is associated with. cmk/kubernetes/deployment: This label is set to the name of the Deployment. cmk/kubernetes/daemonset: This label is set to the name of the DaemonSet. cmk/kubernetes/statefulset: This label is set to the name of the StatefulSet. """ yield HostLabel("cmk/kubernetes/object", object_type) yield HostLabel("cmk/kubernetes/cluster", section.cluster) yield HostLabel("cmk/kubernetes/namespace", section.namespace) yield HostLabel(f"cmk/kubernetes/{object_type}", section.name)
def host_labels(section: DeploymentInfo) -> HostLabelGenerator: """Host label function Labels: cmk/kubernetes/object: This label is set to the Kubernetes object type. cmk/kubernetes/namespace: This label is set to the namespace of the deployment. cmk/kubernetes/deployment: This label is set to the name of the deployment. cmk/container_name: This label is set to the name of the container cmk/container_image: This label is set to the image of the container. """ if not section: return yield HostLabel("cmk/kubernetes/object", "deployment") yield HostLabel("cmk/kubernetes/namespace", section.namespace) yield HostLabel("cmk/kubernetes/deployment", section.name) for container in section.containers: yield HostLabel("cmk/container_name", container) for image in section.images: yield HostLabel("cmk/container_image", image)
def _host_labels(section: Info) -> HostLabelGenerator: """Host label function. Labels: cmk/kubernetes/object: This label is set to the Kubernetes object type. cmk/kubernetes/cluster: This label is set to the given Kubernetes Cluster name. cmk/kubernetes/namespace: This label contains the name of the Kubernetes Namespace this checkmk host is associated with. cmk/kubernetes/annotation/{key}:{value} : These labels are yielded for each Kubernetes annotation that is a valid Kubernetes label. This can be configured via the rule 'Kubernetes'. cmk/kubernetes/deployment: This label is set to the name of the Deployment. cmk/kubernetes/daemonset: This label is set to the name of the DaemonSet. cmk/kubernetes/statefulset: This label is set to the name of the StatefulSet. """ yield HostLabel("cmk/kubernetes/object", object_type) yield HostLabel("cmk/kubernetes/cluster", section.cluster) yield HostLabel("cmk/kubernetes/namespace", section.namespace) yield HostLabel(f"cmk/kubernetes/{object_type}", section.name) yield from kube_annotations_to_cmk_labels(section.annotations)
def host_labels(section: NodeInfo) -> HostLabelGenerator: """Host label function Labels: cmk/kubernetes/object: This label is set to the Kubernetes object type. cmk/kubernetes/cluster: This label is set to the given Kubernetes cluster name. cmk/kubernetes/node: This label contains the name of the Kubernetes Node this checkmk host is associated with. Checkmk hosts of the type Pod and Node will be assigned this label. cmk/os_family: This label is set to the operating system as reported by the agent as "AgentOS" (such as "windows" or "linux"). """ yield HostLabel("cmk/kubernetes/object", "node") yield HostLabel("cmk/kubernetes/cluster", section.cluster) yield HostLabel("cmk/kubernetes/node", section.name) yield HostLabel("cmk/os_family", section.operating_system) yield from kube_labels_to_cmk_labels(section.labels)
def host_labels(section: PodInfo) -> HostLabelGenerator: """Host label function Labels: cmk/kubernetes/object: This label is set to the Kubernetes object type. cmk/kubernetes/cluster: This label is set to the given Kubernetes cluster name. cmk/kubernetes/namespace: This label contains the name of the Kubernetes Namespace this checkmk host is associated with. cmk/kubernetes/node: This label contains the name of the Kubernetes Node this checkmk host is associated with. Checkmk hosts of the type Pod and Node will be assigned this label. """ yield HostLabel("cmk/kubernetes/object", "pod") yield HostLabel("cmk/kubernetes/cluster", section.cluster) if section.node is not None: yield HostLabel("cmk/kubernetes/node", section.node) if section.namespace: yield HostLabel("cmk/kubernetes/namespace", section.namespace) for controller in section.controllers: yield HostLabel(f"cmk/kubernetes/{controller.type_.value}", controller.name) yield from kube_labels_to_cmk_labels(section.labels)
def test_kube_labels_to_cmk_labels(): labels: Labels = { LabelName("asd"): Label(name="asd", value="bsd"), LabelName("empty"): Label(name="empty", value=""), } result = list(kube_labels_to_cmk_labels(labels)) assert result == [ HostLabel("cmk/kubernetes/label/asd", "bsd"), HostLabel("cmk/kubernetes/label/empty", "true"), ]
def host_labels(section: ClusterInfo) -> HostLabelGenerator: """Host label function Labels: cmk/kubernetes/object: This label is set to the Kubernetes object type. cmk/kubernetes/cluster: This label is set to the given Kubernetes cluster name. """ yield HostLabel("cmk/kubernetes/object", "cluster") yield HostLabel("cmk/kubernetes/cluster", section.name)
def kube_annotations_to_cmk_labels( annotations: FilteredAnnotations) -> HostLabelGenerator: """Convert Kubernetes Annotations to HostLabels. Kubernetes annotations are not valid Checkmk labels, but agent_kube makes sure that annotations only arrive here, if we want to yield it as a HostLabel, e.g. a restricted set of characters. Directly yielding `HostLabel(annotation.name, annotation.value)` is problematic. This is because a user can add annotations to their Kubernetes objects, which overwrite existing Checkmk labels. For instance, the annotation `cmk/os_name=` would overwrite the cmk label `cmk/os_name:linux`. To circumvent this problem, we prepend every annotation key with 'cmk/kubernetes/annotation/'. >>> annotations = { ... 'k8s.io/app': 'nginx', ... 'infra': 'yes', ... 'empty': '', ... } >>> list(kube_annotations_to_cmk_labels(annotations)) [HostLabel('cmk/kubernetes/annotation/k8s.io/app', 'nginx'), HostLabel('cmk/kubernetes/annotation/infra', 'yes'), HostLabel('cmk/kubernetes/annotation/empty', 'true')] """ for name, value in annotations.items(): yield HostLabel(f"cmk/kubernetes/annotation/{name}", value or "true")
def test_host_labels_ps_match(): section = ( 1, [ ( ps.ps_info("(root,4056,1512,0.0/52-04:56:05,5689)".split() ), # type: ignore[call-arg] ["/usr/lib/ssh/sshd"], ), ]) params = [ { "default_params": {}, "descr": "SSH", "match": "~.*ssh?", "label": { 'marco': 'polo' }, }, {}, ] assert list(ps.host_labels_ps(params, section)) == [ # type: ignore[arg-type] HostLabel('marco', 'polo') ]
def test_host_labels_ps_match(): section = ( 1, [ ( ps.PsInfo.from_raw("(root,4056,1512,0.0/52-04:56:05,5689)"), ["/usr/lib/ssh/sshd"], ), ], ) params = [ { "default_params": {}, "descr": "SSH", "match": "~.*ssh?", "label": { "marco": "polo" }, }, {}, ] assert list(ps.host_labels_ps(params, section)) == [ # type: ignore[arg-type] HostLabel("marco", "polo") ]
def host_labels(section: NodeInfo) -> HostLabelGenerator: """Host label function Labels: cmk/kubernetes/object: This label is set to the Kubernetes object type. cmk/os_family: This label is set to the operating system as reported by the agent as "AgentOS" (such as "windows" or "linux"). """ yield HostLabel("cmk/kubernetes/object", "node") yield HostLabel("cmk/os_family", section.operating_system) for label in section.labels.values(): yield HostLabel(label.name, label.value)
def host_labels(section: NamespaceInfo) -> HostLabelGenerator: """Host label function Labels: cmk/kubernetes/object: This label is set to the Kubernetes object type. cmk/kubernetes/cluster: This label is set to the given Kubernetes cluster name. cmk/kubernetes/namespace: This label contains the name of the Kubernetes Namespace this checkmk host is associated with. """ yield HostLabel("cmk/kubernetes/object", "namespace") yield HostLabel("cmk/kubernetes/cluster", section.cluster) yield HostLabel("cmk/kubernetes/namespace", section.name) yield from kube_labels_to_cmk_labels(section.labels)
def test_host_labels(): section = parse_snmp_extended_info( [ ["_", "fibrechannel switch", "_", "_", "_", "_", "_", "_", "_"], ["_", "_", "_", "_", "_", "_", "_", "_", "_"], ] ) assert list(get_device_type_label(section)) == [ HostLabel("cmk/device_type", "fcswitch"), ]
def host_labels(section: PodContainers) -> HostLabelGenerator: """Host label function Labels: cmk/container_image: This label is set to the image of the container """ for image in { container.image for container in section.containers.values() }: yield HostLabel("cmk/container_image", image)
def host_labels(section: PodInfo) -> HostLabelGenerator: """Host label function Labels: cmk/kubernetes/object: This label is set to the Kubernetes object type. cmk/kubernetes/cluster: This label is set to the given Kubernetes cluster name. cmk/kubernetes/namespace: This label contains the name of the Kubernetes Namespace this checkmk host is associated with. cmk/kubernetes/annotation/{key}:{value} : These labels are yielded for each Kubernetes annotation that is a valid Kubernetes label. This can be configured via the rule 'Kubernetes'. cmk/kubernetes/node: This label contains the name of the Kubernetes Node this checkmk host is associated with. Checkmk hosts of the type Pod and Node will be assigned this label. """ yield HostLabel("cmk/kubernetes/object", "pod") yield HostLabel("cmk/kubernetes/cluster", section.cluster) if section.node is not None: yield HostLabel("cmk/kubernetes/node", section.node) if section.namespace: yield HostLabel("cmk/kubernetes/namespace", section.namespace) for controller in section.controllers: yield HostLabel(f"cmk/kubernetes/{controller.type_.value}", controller.name) yield from kube_labels_to_cmk_labels(section.labels) yield from kube_annotations_to_cmk_labels(section.annotations)
def host_labels(section: PodInfo) -> HostLabelGenerator: """Host label function Labels: cmk/kubernetes/object: This label is set to the Kubernetes object type. cmk/kubernetes/namespace: This label is set to the namespace of the deployment. cmk/kubernetes/node: This label is set to the node of the pod. """ yield HostLabel("cmk/kubernetes/object", "pod") if section.node is not None: yield HostLabel("cmk/kubernetes/node", section.node) yield HostLabel("cmk/kubernetes/namespace", section.namespace) for controller in section.controllers: yield HostLabel(f"cmk/kubernetes/{controller.type_.value}", controller.name) for label in section.labels.values(): yield HostLabel(label.name, label.value)
def kube_labels_to_cmk_labels(labels: Labels) -> HostLabelGenerator: """Convert Kubernetes Labels to HostLabels. Key-value pairs of Kubernetes labels are valid checkmk labels (see `LabelName` and `LabelValue`). However, directly yielding `HostLabel(label.name, label.value)` is problematic. This is because a user can add labels to their Kubernetes objects, which overwrite existing Checkmk labels. For instance, the label `cmk/os_name=` would overwrite the cmk label `cmk/os_name:linux`. To circumvent this problem, we prepend every label key with 'cmk/kubernetes/label/'. >>> list(kube_labels_to_cmk_labels({ ... 'k8s.io/app': Label(name='k8s.io/app', value='nginx'), ... 'infra': Label(name='infra', value='yes'), ... })) [HostLabel('cmk/kubernetes/label/k8s.io/app', 'nginx'), HostLabel('cmk/kubernetes/label/infra', 'yes')] """ for label in labels.values(): if (value := label.value) == "": value = LabelValue("true") yield HostLabel(f"cmk/kubernetes/label/{label.name}", value)
# -*- coding: utf-8 -*- # Copyright (C) 2019 tribe29 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. import pytest from cmk.base.plugins.agent_based.check_mk import ( parse_checkmk_labels, host_label_function_labels, ) from cmk.base.plugins.agent_based.agent_based_api.v1 import HostLabel @pytest.mark.parametrize("string_table,expected_parsed_data", [ ( [['Version:', '1.7.0i1'], ['AgentOS:', 'linux'], ['Hostname:', 'klappclub'], ['AgentDirectory:', '/etc/check_mk'], ['DataDirectory:', '/var/lib/check_mk_agent'], ['SpoolDirectory:', '/var/lib/check_mk_agent/spool'], ['PluginsDirectory:', '/usr/lib/check_mk_agent/plugins'], ['LocalDirectory:', '/usr/lib/check_mk_agent/local']], [HostLabel('cmk/os_family', 'linux')], ), ]) def test_checkmk_labels(string_table, expected_parsed_data): result = list( host_label_function_labels(parse_checkmk_labels(string_table))) assert isinstance(result[0], HostLabel) assert expected_parsed_data == result
inventory_snmp_extended_info, parse_snmp_extended_info, ) from .utils_inventory import sort_inventory_result @pytest.mark.parametrize( "string_table, expected_result", [ ( [ ["_", "fibrechannel switch", "_", "_", "_", "_", "_", "_", "_"], ["_", "_", "_", "_", "_", "_", "_", "_", "_"], ], [HostLabel("cmk/device_type", "fcswitch")], ), ( [ ["_", "switch (fc1)", "_", "_", "_", "_", "_", "_", "_"], ], [HostLabel("cmk/device_type", "switch")], ), ( [ ["_", "fc switch", "_", "_", "_", "_", "_", "_", "_"], ], [HostLabel("cmk/device_type", "fcswitch")], ), ], )
def test_get_discovery_specs(): assert ps.get_discovery_specs([ { "default_params": {}, "descr": "smss", "match": "~smss.exe" }, { "default_params": { "cpulevels": (90.0, 98.0), "handle_count": (1000, 2000), "levels": (1, 1, 99999, 99999), "max_age": (3600, 7200), "resident_levels": (104857600, 209715200), "resident_levels_perc": (25.0, 50.0), "single_cpulevels": (90.0, 98.0), "virtual_levels": (1073741824000, 2147483648000), }, "descr": "svchost", "match": "svchost.exe" }, { "default_params": { "process_info": "text" }, "match": "~.*(fire)fox", "descr": "firefox is on %s", "user": None, }, { "default_params": { "process_info": "text" }, "match": "~.*(fire)fox", "descr": "firefox is on %s", "user": None, "label": DiscoveredHostLabels(HostLabel(u'marco', u'polo'), HostLabel(u'peter', u'pan')), }, { "default_params": { "cpu_rescale_max": True, "cpu_average": 15, "process_info": "html", "resident_levels_perc": (25.0, 50.0), "virtual_levels": (1024**3, 2 * 1024**3), "resident_levels": (1024**3, 2 * 1024**3), "icon": "emacs.png", }, "descr": "emacs %u", "match": "emacs", "user": False }, { "default_params": { "max_age": (3600, 7200), "resident_levels_perc": (25.0, 50.0), "single_cpulevels": (90.0, 98.0), "resident_levels": (104857600, 209715200), }, "match": "~.*cron", "descr": "cron", "user": "******" }, { "default_params": {}, "descr": "sshd", "match": "~.*sshd" }, { 'default_params': {}, 'descr': 'PS counter', 'user': '******', }, { "default_params": { "process_info": "text" }, "match": r"~/omd/sites/(\w+)/lib/cmc/checkhelper", "descr": "Checkhelpers %s", "user": None, }, { "default_params": { "process_info": "text" }, "match": r"~/omd/sites/\w+/lib/cmc/checkhelper", "descr": "Checkhelpers Overall", "user": None, }, {}, ]) == [ ("smss", "~smss.exe", None, (None, False), DiscoveredHostLabels(), { 'cpu_rescale_max': None }), ("svchost", "svchost.exe", None, (None, False), {}, { "cpulevels": (90.0, 98.0), 'cpu_rescale_max': None, "handle_count": (1000, 2000), "levels": (1, 1, 99999, 99999), "max_age": (3600, 7200), "resident_levels": (104857600, 209715200), "resident_levels_perc": (25.0, 50.0), "single_cpulevels": (90.0, 98.0), "virtual_levels": (1073741824000, 2147483648000), }), ("firefox is on %s", "~.*(fire)fox", None, (None, False), {}, { "process_info": "text", 'cpu_rescale_max': None, }), ("firefox is on %s", "~.*(fire)fox", None, (None, False), DiscoveredHostLabels(HostLabel(u'marco', u'polo'), HostLabel(u'peter', u'pan')), { "process_info": "text", 'cpu_rescale_max': None, }), ("emacs %u", "emacs", False, (None, False), {}, { "cpu_average": 15, 'cpu_rescale_max': True, "process_info": "html", "resident_levels_perc": (25.0, 50.0), "virtual_levels": (1024**3, 2 * 1024**3), "resident_levels": (1024**3, 2 * 1024**3), "icon": "emacs.png", }), ("cron", "~.*cron", "root", (None, False), {}, { "max_age": (3600, 7200), 'cpu_rescale_max': None, "resident_levels_perc": (25.0, 50.0), "single_cpulevels": (90.0, 98.0), "resident_levels": (104857600, 209715200) }), ("sshd", "~.*sshd", None, (None, False), {}, { 'cpu_rescale_max': None }), ('PS counter', None, 'zombie', (None, False), {}, { 'cpu_rescale_max': None }), ("Checkhelpers %s", r"~/omd/sites/(\w+)/lib/cmc/checkhelper", None, (None, False), {}, { "process_info": "text", 'cpu_rescale_max': None, }), ("Checkhelpers Overall", r"~/omd/sites/\w+/lib/cmc/checkhelper", None, (None, False), {}, { "process_info": "text", 'cpu_rescale_max': None, }), ]
def kube_labels_to_cmk_labels(labels: Labels) -> HostLabelGenerator: for label in labels.values(): if (value := label.value) == "": value = LabelValue("true") yield HostLabel(label.name, value)
import pytest from cmk.base.plugins.agent_based.agent_based_api.v1 import HostLabel from cmk.base.plugins.agent_based.check_mk import host_label_function_labels, parse_checkmk_labels @pytest.mark.parametrize( "string_table,expected_parsed_data", [ ( [ ["Version:", "1.7.0i1"], ["AgentOS:", "linux"], ["Hostname:", "klappclub"], ["AgentDirectory:", "/etc/check_mk"], ["DataDirectory:", "/var/lib/check_mk_agent"], ["SpoolDirectory:", "/var/lib/check_mk_agent/spool"], ["PluginsDirectory:", "/usr/lib/check_mk_agent/plugins"], ["LocalDirectory:", "/usr/lib/check_mk_agent/local"], ], [HostLabel("cmk/os_family", "linux")], ), ], ) def test_checkmk_labels(string_table, expected_parsed_data): result = list( host_label_function_labels(parse_checkmk_labels(string_table))) assert isinstance(result[0], HostLabel) assert expected_parsed_data == result