Exemplo n.º 1
0
def test_discovered_host_labels_repr():
    labels = DiscoveredHostLabels()
    labels.add_label(HostLabel(u"äbc", u"123", "plugin_1"))
    labels.add_label(HostLabel(u"ccc", u"ddd", "plugin_2"))
    assert repr(
        labels
    ) == "DiscoveredHostLabels(HostLabel('ccc', 'ddd', plugin_name='plugin_2'), HostLabel('äbc', '123', plugin_name='plugin_1'))"
Exemplo n.º 2
0
def test_discovered_host_labels_add():
    labels_1 = DiscoveredHostLabels()
    labels_1.add_label(HostLabel(u"äbc", u"123", "plugin_1"))

    labels_2 = DiscoveredHostLabels()
    labels_2.add_label(HostLabel(u"xyz", u"blä", "plugin_2"))

    new_labels = labels_1 + labels_2
    assert new_labels.to_dict() == {
        u"äbc": {
            "value": u"123",
            "plugin_name": "plugin_1",
        },
        u"xyz": {
            "value": u"blä",
            "plugin_name": "plugin_2",
        },
    }

    labels_1 += labels_2
    assert labels_1.to_dict() == {
        u"äbc": {
            "value": u"123",
            "plugin_name": "plugin_1",
        },
        u"xyz": {
            "value": u"blä",
            "plugin_name": "plugin_2",
        },
    }
Exemplo n.º 3
0
def test_discovered_host_labels_repr():
    labels = DiscoveredHostLabels()
    labels.add_label(HostLabel(u"äbc", u"123", SectionName("plugin_1")))
    labels.add_label(HostLabel(u"ccc", u"ddd", SectionName("plugin_2")))
    assert repr(labels) == (
        "DiscoveredHostLabels("  #
        "HostLabel('ccc', 'ddd', plugin_name=SectionName('plugin_2')), "  #
        "HostLabel('äbc', '123', plugin_name=SectionName('plugin_1')))")
Exemplo n.º 4
0
def test_discovered_host_labels_to_list():
    labels = DiscoveredHostLabels()
    assert labels.to_list() == []

    labels.add_label(HostLabel(u"äbc", u"123", "plugin_1"))
    labels.add_label(HostLabel(u"xyz", u"blä", "plugin_2"))

    assert labels.to_list() == [
        HostLabel(u"xyz", u"blä", "plugin_2"),
        HostLabel(u"äbc", u"123", "plugin_1")
    ]
Exemplo n.º 5
0
def test_discovered_host_labels_to_list():
    labels = DiscoveredHostLabels()
    assert labels.to_list() == []

    labels.add_label(HostLabel("äbc", "123", SectionName("plugin_1")))
    labels.add_label(HostLabel("xyz", "blä", SectionName("plugin_2")))

    assert labels.to_list() == [
        HostLabel("xyz", "blä", SectionName("plugin_2")),
        HostLabel("äbc", "123", SectionName("plugin_1")),
    ]
Exemplo n.º 6
0
def test_discovered_host_labels_sub():
    labels_1 = DiscoveredHostLabels()
    labels_1.add_label(HostLabel("foo", "bär", SectionName("plugin_1")))
    labels_1.add_label(HostLabel("foo2", "bär2", SectionName("plugin_2")))

    labels_2 = DiscoveredHostLabels()
    labels_2.add_label(HostLabel("foo", "bär", SectionName("plugin_1")))

    assert (labels_1 - labels_2).to_dict() == {
        "foo2": {
            "value": "bär2",
            "plugin_name": "plugin_2",
        },
    }

    assert (labels_2 - labels_1).to_dict() == {}
Exemplo n.º 7
0
def test_discovered_host_labels_path(discovered_host_labels_dir):
    hostname = "test.host.de"
    config.get_config_cache().initialize()
    assert not (discovered_host_labels_dir / hostname).exists()
    DiscoveredHostLabelsStore(hostname).save(
        DiscoveredHostLabels(HostLabel("foo", "1.5")).to_dict())
    assert (discovered_host_labels_dir / (hostname + ".mk")).exists()
Exemplo n.º 8
0
def test_host_labels_from_dict() -> None:
    label_dict: HostLabelValueDict = {
        "value": "123",
        "plugin_name": "plugin_1",
    }
    labels = HostLabel.from_dict("äbc", label_dict)
    assert labels.to_dict() == label_dict
Exemplo n.º 9
0
def test_discovered_host_labels_to_dict():
    labels = DiscoveredHostLabels()
    assert labels.to_dict() == {}

    labels.add_label(HostLabel("äbc", "123", SectionName("plugin_1")))
    labels.add_label(HostLabel("xyz", "blä", SectionName("plugin_2")))

    assert labels.to_dict() == {
        "äbc": {
            "value": "123",
            "plugin_name": "plugin_1",
        },
        "xyz": {
            "value": "blä",
            "plugin_name": "plugin_2",
        },
    }
Exemplo n.º 10
0
 def __repr__(self) -> str:
     entries: List[object] = [
         o for o in self.entries if isinstance(o, object)
     ]
     host_labels: List[object] = [
         HostLabel(str(k), str(self.labels[k])) for k in self.labels
     ]
     return "DiscoveryResult(%r)" % (entries + host_labels, )
Exemplo n.º 11
0
def test_discovered_host_labels_to_dict():
    labels = DiscoveredHostLabels()
    assert labels.to_dict() == {}

    labels.add_label(HostLabel(u"äbc", u"123", "plugin_1"))
    labels.add_label(HostLabel(u"xyz", u"blä", "plugin_2"))

    assert labels.to_dict() == {
        u"äbc": {
            "value": u"123",
            "plugin_name": "plugin_1",
        },
        u"xyz": {
            "value": u"blä",
            "plugin_name": "plugin_2",
        },
    }
Exemplo n.º 12
0
 def __repr__(self):
     # type: () -> str
     entries = [o for o in self.entries
                if isinstance(o, object)]  # type: List[object]
     host_labels = [
         HostLabel(six.text_type(k), six.text_type(self.labels[k]))
         for k in self.labels
     ]  # type: List[object]
     return "DiscoveryResult(%r)" % (entries + host_labels, )
Exemplo n.º 13
0
def test_discovered_host_labels_store_save(discovered_host_labels_dir):
    store = DiscoveredHostLabelsStore("host")

    labels = DiscoveredHostLabels(HostLabel(u"xyz", u"äbc"))
    label_dict = labels.to_dict()

    assert not store.file_path.exists()  # pylint: disable=no-member

    store.save(label_dict)
    assert store.file_path.exists()  # pylint: disable=no-member
    assert store.load() == label_dict
Exemplo n.º 14
0
def test_discovered_host_labels_store_save(discovered_host_labels_dir):
    store = DiscoveredHostLabelsStore("host")

    labels = DiscoveredHostLabels(
        HostLabel(u"xyz", u"äbc", SectionName("sectionname")))
    label_dict = labels.to_dict()

    assert not store.file_path.exists()

    store.save(label_dict)
    assert store.file_path.exists()
    assert store.load() == label_dict
Exemplo n.º 15
0
def test_perform_host_label_discovery(discovered_host_labels_dir,
                                      existing_labels, new_labels,
                                      expected_labels, load_labels):
    hostname = "testhost"
    config.get_config_cache().initialize()
    store = DiscoveredHostLabelsStore(hostname)
    store.save(
        DiscoveredHostLabels(*[HostLabel(*x)
                               for x in existing_labels]).to_dict())

    discovery_parameters = DiscoveryParameters(
        on_error="raise",
        load_labels=load_labels,
        save_labels=False,
    )

    new_host_labels, _host_labels_per_plugin = _perform_host_label_discovery(
        hostname, DiscoveredHostLabels(*[HostLabel(*x) for x in new_labels]),
        discovery_parameters)

    labels_expected = DiscoveredHostLabels(
        *[HostLabel(*x) for x in expected_labels])
    assert new_host_labels.to_dict() == labels_expected.to_dict()
Exemplo n.º 16
0
def _load_existing_host_labels(
    *,
    host_name: HostName,
    discovery_parameters: DiscoveryParameters,
) -> Sequence[HostLabel]:
    # Take over old items if -I is selected
    if not discovery_parameters.load_labels:
        return []

    raw_label_dict = DiscoveredHostLabelsStore(host_name).load()
    return [
        HostLabel.from_dict(name, value)
        for name, value in raw_label_dict.items()
    ]
    def insane_discovery(info):
        """Completely crazy discovery function:

            * wrong arg name
            * is not a generator
            * returns all kinds of stuff
        """
        assert info == ["info"]
        return [
            ("foo", {}),
            "some string",
            HostLabel("whoop", "deedoo"),
            OldService("bar", {"P": "O"}),
        ]
Exemplo n.º 18
0
def _discover_host_labels_for_source_type(
    *,
    host_key: HostKey,
    parsed_sections_broker: ParsedSectionsBroker,
    on_error: OnError,
) -> Mapping[str, HostLabel]:

    host_labels = {}
    try:
        parsed_results = parsed_sections_broker.all_parsing_results(host_key)

        console.vverbose(
            "Trying host label discovery with: %s\n" %
            ", ".join(str(r.section.name) for r in parsed_results))
        for (section_data, _cache_info), section_plugin in parsed_results:

            kwargs = {'section': section_data}

            host_label_params = config.get_host_label_parameters(
                host_key.hostname, section_plugin)
            if host_label_params is not None:
                kwargs["params"] = host_label_params

            try:
                for label in section_plugin.host_label_function(**kwargs):
                    console.vverbose(
                        f"  {label.name}: {label.value} ({section_plugin.name})\n"
                    )
                    host_labels[label.name] = HostLabel(
                        label.name,
                        label.value,
                        section_plugin.name,
                    )
            except (KeyboardInterrupt, MKTimeout):
                raise
            except Exception as exc:
                if on_error is OnError.RAISE:
                    raise
                if on_error is OnError.WARN:
                    console.error(
                        f"Host label discovery of '{section_plugin.name}' failed: {exc}\n"
                    )

    except KeyboardInterrupt:
        raise MKGeneralException("Interrupted by Ctrl-C.")

    return host_labels
Exemplo n.º 19
0
         "cpu_rescale_max": "cpu_rescale_max_unspecified",
         "process_info": "text"
     },
     "match": "~.*(fire)fox",
     "descr": "firefox is on %s",
     "user": None,
 },
 {
     "default_params": {
         "cpu_rescale_max": "cpu_rescale_max_unspecified",
         "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
 },
import cmk.base.api.agent_based.register.section_plugins as section_plugins
from cmk.base.api.agent_based.section_types import SNMPTree
from cmk.base.check_api_utils import Service
from cmk.base.discovered_labels import DiscoveredHostLabels, HostLabel


def old_school_scan_function(oid):
    return oid(".1.2.3.4.5").startswith("norris")


def old_school_parse_function(_info):
    return {"what": "ever"}


HOST_LABELS = [
    HostLabel("foo", "bar"),
    HostLabel("gee", "boo"),
    HostLabel("heinz", "hirn"),
]


def old_school_discover_function(parsed_extra):
    _parsed, _extra_section = parsed_extra
    yield "item1", {"discoverd_param": 42}
    yield HOST_LABELS[0]
    yield Service(
        "item2",
        {},
        host_labels=DiscoveredHostLabels(*HOST_LABELS[1:]),
    )
    yield "item3", "{'how_bad_is_this': 100}"
Exemplo n.º 21
0
parsed = {
    u'collision_count': None,
    u'conditions': None,
    u'current_number_scheduled': 1,
    u'desired_number_scheduled': 1,
    u'number_available': 1,
    u'number_misscheduled': 0,
    u'number_ready': 1,
    u'number_unavailable': None,
    u'observed_generation': 1,
    u'updated_number_scheduled': 1
}

discovery = {
    '': [(None, {}), HostLabel(u'cmk/kubernetes_object', u'daemonset')]
}

checks = {
    '': [(
        None,
        {},
        [(0, 'Ready: 1', [('k8s_daemon_pods_ready', 1, None, None, None, None)]),
         (0, 'Scheduled: 1/1', [('k8s_daemon_pods_scheduled_desired', 1, None, None, None, None),
                                ('k8s_daemon_pods_scheduled_current', 1, None, None, None, None)]),
         (0, 'Up to date: 1', [('k8s_daemon_pods_scheduled_updated', 1, None, None, None, None)]),
         (0, 'Available: 1/1', [('k8s_daemon_pods_available', 1, None, None, None, None),
                                ('k8s_daemon_pods_unavailable', 0, None, None, None, None)])],
    )],
}
Exemplo n.º 22
0
def _discover_host_labels_for_source_type(
    *,
    host_key: HostKey,
    parsed_sections_broker: ParsedSectionsBroker,
    discovery_parameters: DiscoveryParameters,
) -> Mapping[str, HostLabel]:

    try:
        host_data = parsed_sections_broker[host_key]
    except KeyError:
        return {}

    host_labels = {}
    try:
        # We do *not* process all available raw sections. Instead we see which *parsed*
        # sections would result from them, and then process those.
        parse_sections = {
            agent_based_register.get_section_plugin(rs).parsed_section_name
            for rs in host_data.sections
        }
        applicable_sections = parsed_sections_broker.determine_applicable_sections(
            parse_sections,
            host_key.source_type,
        )

        console.vverbose("Trying host label discovery with: %s\n" %
                         ", ".join(str(s.name) for s in applicable_sections))
        for section_plugin in _sort_sections_by_label_priority(
                applicable_sections):

            kwargs = {
                'section':
                parsed_sections_broker.get_parsed_section(
                    host_key, section_plugin.parsed_section_name),
            }

            host_label_params = config.get_host_label_parameters(
                host_key.hostname, section_plugin)
            if host_label_params is not None:
                kwargs["params"] = host_label_params

            try:
                for label in section_plugin.host_label_function(**kwargs):
                    console.vverbose(
                        f"  {label.name}: {label.value} ({section_plugin.name})\n"
                    )
                    host_labels[label.name] = HostLabel(
                        label.name,
                        label.value,
                        section_plugin.name,
                    )
            except (KeyboardInterrupt, MKTimeout):
                raise
            except Exception as exc:
                if cmk.utils.debug.enabled(
                ) or discovery_parameters.on_error == "raise":
                    raise
                if discovery_parameters.on_error == "warn":
                    console.error("Host label discovery of '%s' failed: %s\n" %
                                  (section_plugin.name, exc))

    except KeyboardInterrupt:
        raise MKGeneralException("Interrupted by Ctrl-C.")

    return host_labels
Exemplo n.º 23
0
def _load_existing_host_labels(host_name: HostName) -> Sequence[HostLabel]:
    raw_label_dict = DiscoveredHostLabelsStore(host_name).load()
    return [HostLabel.from_dict(name, value) for name, value in raw_label_dict.items()]
Exemplo n.º 24
0
# -*- encoding: utf-8
# yapf: disable


from cmk.base.discovered_labels import HostLabel


checkname = 'k8s_job_info'


parsed = {u'active': 1, u'failed': 1, u'succeeded': 1}


discovery = {
    '': [(None, {}), HostLabel(u'cmk/kubernetes_object', u'job')]
}


checks = {'': [(None, {}, [(2, 'Running: 1/3, Failed: 1, Succeeded: 1', [])])]}
Exemplo n.º 25
0
# -*- 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  # type: ignore[import]

from cmk.base.plugins.agent_based.check_mk import (
    parse_checkmk_labels,
    host_label_function_labels,
)
from cmk.base.discovered_labels 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', plugin_name=None)],
    ),
])
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
Exemplo n.º 26
0
 # do discovery: only_new == True
 # discover on host: mode != "remove"
 DiscoveryTestCase(
     parameters=discovery.DiscoveryParameters(
         on_error="raise",
         load_labels=True,
         save_labels=True,
         only_host_labels=False,
     ),
     expected_services={
         (CheckPluginName('df'), '/boot/test-efi'),
         (CheckPluginName('df'), '/opt/omd/sites/test-heute/tmp'),
     },
     on_realhost=ExpectedDiscoveryResultRealHost(
         expected_vanished_host_labels=[
             HostLabel('existing_label', 'bar', SectionName('foo')),
             HostLabel('another_label', 'true', SectionName('labels')),
         ],
         expected_old_host_labels=[],
         expected_new_host_labels=[
             HostLabel('cmk/check_mk_server', 'yes', SectionName('labels')),
         ],
         expected_stored_labels={
             'another_label': {
                 'plugin_name': 'labels',
                 'value': 'true',
             },
             'existing_label': {
                 'plugin_name': 'foo',
                 'value': 'bar',
             },
Exemplo n.º 27
0
         "process_info": "text"
     },
     "match": "~.*(fire)fox",
     "descr": "firefox is on %s",
     "user": None,
 }, [], ["@all"], {
     "description": u"Firefox"
 }),
 ({
     "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')),
 }, [], ["@all"], {
     "description": u"Firefox with host labels"
 }),
 ({
     "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",
    [u'| Metadata loop file', u' /data/docker/devicemapper/devicemapper/metadata'],
    [u'| Library Version', u' 1.02.117-Koechelverzeichnis/ (2024-12-12)'],
    [u'|Execution Driver', u' killmenow-0.23'],
    [u'|Logging Driver', u' json-file'],
    [u'|Kernel Version', u' 3.14.15-926.5.3.el5.x86_64'],
    [u'|Operating System', u' <unknown>'],
    [u'|CPUs', u' 1024'],
    [u'|Total Memory', u' -23 GiB'],
    [u'|Name', u' voms01'],
    [
        u'|ID', u' XXXX', u'XXXX', u'XXXX', u'XXXX', u'XXXX', u'XXXX', u'BLOB', u'BOBO', u'0COV',
        u'FEFE', u'WHOO', u'0TEH'
    ],
]

discovery = {'': [(None, {}), HostLabel(u'cmk/docker_object', u'node')],
            'containers': [(None, {})]}

checks = {
    '': [
        (None, {}, [
            (0, u'Daemon running on host voms01', []),
            DEPRECATION_WARNING,
        ]),
    ],
    'containers': [
        (None, {}, [
            (0, 'Containers: 42', [('containers', 42, None, None, None, None)]),
            (3, 'Running: count not present in agent output', []),
            (3, 'Paused: count not present in agent output', []),
            (3, 'Stopped: count not present in agent output', []),
checkname = 'docker_container_status'

freeze_time = '2019-06-05T09:40:06.893459004Z'

info = [
    [
        u'@docker_version_info',
        u'{"PluginVersion": "0.1", "DockerPyVersion": "4.0.2", "ApiVersion": "1.39"}'
    ],
    [
        u'{"Status": "running", "Healthcheck": {"Test": ["CMD-SHELL", "/healthcheck.sh"]}, "Pid": 0, "OOMKilled": false, "Dead": false, "RestartPolicy": {"MaximumRetryCount": 0, "Name": "no"}, "Paused": false, "Running": false, "FinishedAt": "2019-06-05T13:52:46.75115293Z", "Health": {"Status": "unhealthy", "Log": [{"Start": "2019-06-05T15:50:23.329542773+02:00", "Output": "mysqld is alive\\n", "End": "2019-06-05T15:50:23.703382311+02:00", "ExitCode": 0}, {"Start": "2019-06-05T15:50:53.724749309+02:00", "Output": "mysqld is alive\\n", "End": "2019-06-05T15:50:54.082847699+02:00", "ExitCode": 0}, {"Start": "2019-06-05T15:51:24.10105535+02:00", "Output": "mysqld is alive\\n", "End": "2019-06-05T15:51:24.479921663+02:00", "ExitCode": 0}, {"Start": "2019-06-05T15:51:54.531087549+02:00", "Output": "mysqld is alive\\n", "End": "2019-06-05T15:51:54.891176872+02:00", "ExitCode": 0}, {"Start": "2019-06-05T15:52:24.911587947+02:00", "Output": "mysqld is alive\\n", "End": "2019-06-05T15:52:25.256847222+02:00", "ExitCode": 0}], "FailingStreak": 0}, "Restarting": false, "Error": "", "StartedAt": "2019-06-05T08:58:06.893459004Z", "ExitCode": 0}'
    ]
]

discovery = {
    '': [HostLabel(u'cmk/docker_object', u'container', plugin_name=None), (None, {})],
    'uptime': [(None, {})],
    'health': [(None, {})]
}

checks = {
    '': [(None, {}, [(0, u'Container running', [])])],
    'uptime': [(None, {}, [(0, 'Up since Wed Jun  5 10:58:06 2019, uptime: 0:42:00',
                            [('uptime', 2520.0, None, None, None, None)])])],
    'health': [(None, {}, [(2, u'Health status: Unhealthy', []),
                           (0, u'Last health report: mysqld is alive', []),
                           (2, 'Failing streak: 0', []),
                           (0, u"Health test: CMD-SHELL /healthcheck.sh", [])])]
}

extra_sections = {'': [[]], 'uptime': [[]], 'health': [[]]}
Exemplo n.º 30
0
def test_discovered_host_label_equal():
    assert HostLabel(u"äbc", u"123") != HostLabel(u"xyz", u"blä")
    assert HostLabel(u"äbc", u"123") == HostLabel(u"äbc", u"123")