Exemplo n.º 1
0
def test_host_table_host_equal_filter(site: Site) -> None:
    queries = {
        "nagios":
        "GET hosts\n"
        "Columns: host_name\n"
        "Filter: host_name = livestatus-test-host.domain\n",
        "cmc":
        "GET hosts\n"
        "Columns: host_name\n"
        "Filter: host_name = livestatus-test-host\n",
    }
    results = {
        "nagios": [
            {
                "name": "livestatus-test-host.domain",
            },
        ],
        "cmc": [
            {
                "name": "livestatus-test-host",
            },
        ],
    }

    rows = site.live.query_table_assoc(queries[site.core_name()])
    assert rows == results[site.core_name()]
def test_down_child_becomes_unreachable_then_up(unreachable_enabled, site: Site, initial_state):
    with WatchLog(site) as log:
        # - Set child down, expect DOWN notification
        site.send_host_check_result("notify-test-child", STATE_DOWN, "DOWN")
        log.check_logged("HOST ALERT: notify-test-child;DOWN;HARD;1;DOWN")

        if site.core_name() == "cmc":
            # CMC: Send a new check result for the parent to make the CMC create the host notification
            # for the child
            site.send_host_check_result("notify-test-parent", STATE_UP, "UP")

        log.check_logged(
            "HOST NOTIFICATION: check-mk-notify;notify-test-child;DOWN;check-mk-notify;"
        )

        # - Set parent down, expect DOWN notification for parent and UNREACHABLE notification for child
        site.send_host_check_result("notify-test-parent", STATE_DOWN, "DOWN")
        log.check_logged("HOST ALERT: notify-test-parent;DOWN;HARD;1;DOWN")

        # Difference beween nagios/cmc: when sending DOWN via PROCESS_HOST_CHECK_RESULT
        # the nagios core needs another child down check result to report it as unreachable.
        if site.core_name() == "cmc":
            log.check_logged("HOST ALERT: notify-test-child;UNREACHABLE;HARD;1;")

            if unreachable_enabled:
                log.check_logged(
                    "HOST NOTIFICATION: check-mk-notify;notify-test-child;UNREACHABLE;check-mk-notify;"
                )
            # TODO: Can not test this because it drains too many entries from the log. WatchLog could deal
            # with this by readding the read lines after succeeded test or similar
            # else:
            #    log.check_not_logged("HOST NOTIFICATION: check-mk-notify;notify-test-child;UNREACHABLE;check-mk-notify;")

            log.check_logged(
                "HOST NOTIFICATION: check-mk-notify;notify-test-parent;DOWN;check-mk-notify;"
            )

        elif site.core_name() == "nagios":
            log.check_logged(
                "HOST NOTIFICATION: check-mk-notify;notify-test-parent;DOWN;check-mk-notify;"
            )

            site.send_host_check_result(
                "notify-test-child", STATE_DOWN, "DOWN", expected_state=STATE_UNREACHABLE
            )
            log.check_logged("HOST ALERT: notify-test-child;UNREACHABLE;HARD;1;")

            if unreachable_enabled:
                log.check_logged(
                    "HOST NOTIFICATION: check-mk-notify;notify-test-child;UNREACHABLE;check-mk-notify;"
                )

        # - Set child up, expect:
        #   cmc: pending UP notification, and sent up notification after next parent check
        #   nagios: UP notification
        _send_child_recovery(site, log)

        # - Set parent up, expect UP notification
        _send_parent_recovery(site, log)
Exemplo n.º 3
0
def test_child_down_after_parent_recovers(unreachable_enabled, site: Site,
                                          initial_state):
    with WatchLog(site) as log:
        # - Set parent down, expect DOWN notification
        _send_parent_down(site, log)
        log.check_logged(
            "HOST NOTIFICATION: check-mk-notify;notify-test-parent;DOWN;check-mk-notify;"
        )

        # - set child down, expect UNREACHABLE notification
        _send_child_down_expect_unreachable(unreachable_enabled, site, log)

        # - Set parent up, expect UP notification
        _send_parent_recovery(site, log)

        # - Next child check DOWN, expect no notification (till next parent check confirms UP)
        site.send_host_check_result("notify-test-child", STATE_DOWN, "DOWN")
        log.check_logged("HOST ALERT: notify-test-child;DOWN;HARD;1;")

        if site.core_name() == "cmc":
            # - Set parent UP (again), expect DOWN notification for child
            site.send_host_check_result("notify-test-parent", STATE_UP, "UP")

        log.check_logged(
            "HOST NOTIFICATION: check-mk-notify;notify-test-child;DOWN;check-mk-notify;"
        )
Exemplo n.º 4
0
def test_unreachable_child_down_before_parent_down(unreachable_enabled: bool,
                                                   site: Site, initial_state):
    with WatchLog(site) as log:
        # - Set child down, expect DOWN notification
        _send_child_down(site, log)

        # - Set parent down, expect DOWN notification for parent and UNREACHABLE notification for child
        _send_parent_down(site, log)

        if site.core_name() == "cmc":
            log.check_logged(
                "HOST ALERT: notify-test-child;UNREACHABLE;HARD;1;")

            if unreachable_enabled:
                log.check_logged(
                    "HOST NOTIFICATION: check-mk-notify;notify-test-child;UNREACHABLE;check-mk-notify;"
                )
            # TODO: Can not check this at the moment
            # else:
            #    log.check_not_logged("HOST NOTIFICATION: check-mk-notify;notify-test-child;UNREACHABLE;check-mk-notify;")
            log.check_logged(
                "HOST NOTIFICATION: check-mk-notify;notify-test-parent;DOWN;check-mk-notify;"
            )

        elif site.core_name() == "nagios":
            log.check_logged(
                "HOST NOTIFICATION: check-mk-notify;notify-test-parent;DOWN;check-mk-notify;"
            )

            # Difference beween nagios/cmc: when sending DOWN via PROCESS_HOST_CHECK_RESULT
            # the nagios core needs another child down check result to report it as unreachable.
            site.send_host_check_result("notify-test-child",
                                        STATE_DOWN,
                                        "DOWN",
                                        expected_state=STATE_UNREACHABLE)
            log.check_logged(
                "HOST ALERT: notify-test-child;UNREACHABLE;HARD;1;")

            if unreachable_enabled:
                log.check_logged(
                    "HOST NOTIFICATION: check-mk-notify;notify-test-child;UNREACHABLE;check-mk-notify;"
                )
def _send_child_down(site: Site, log: WatchLog) -> None:
    # - Set child down, expect DOWN notification
    site.send_host_check_result("notify-test-child", STATE_DOWN, "DOWN")
    log.check_logged("HOST ALERT: notify-test-child;DOWN;HARD;1;DOWN")

    if site.core_name() == "cmc":
        # CMC: Send a new check result for the parent to make the CMC create the host notification
        # for the child
        site.send_host_check_result("notify-test-parent", STATE_UP, "UP")

    log.check_logged("HOST NOTIFICATION: check-mk-notify;notify-test-child;DOWN;check-mk-notify;")
Exemplo n.º 6
0
def test_tables(site: Site) -> None:
    columns_per_table: Dict[str, List[str]] = {}
    for row in site.live.query_table_assoc("GET columns\n"):
        columns_per_table.setdefault(row["table"], []).append(row["name"])
    assert len(columns_per_table) > 5

    for table, _columns in columns_per_table.items():
        print("Test table: %s" % table)

        if site.core_name() == "nagios" and table == "statehist":
            continue  # the statehist table in nagios can not be fetched without time filter

        result = site.live.query("GET %s\n" % table)
        assert isinstance(result, list)
def initial_state_fixture(site: Site) -> None:
    # Before each test: Set to initial state: Both UP
    site.send_host_check_result("notify-test-child", 0, "UP")
    site.send_host_check_result("notify-test-parent", 0, "UP")

    # Before each test: Clear logs
    if site.core_name() == "cmc":
        # The command is processed asynchronously -> Wait for completion
        inode_before = os.stat(site.path("var/check_mk/core/history")).st_ino
        site.live.command("[%d] ROTATE_LOGFILE" % time.time())

        def rotated_log():
            try:
                return inode_before != os.stat(site.path("var/check_mk/core/history")).st_ino
            except OSError as e:
                if e.errno == errno.ENOENT:
                    return False
                raise e

        wait_until(rotated_log, timeout=10)
    else:
        site.delete_file("var/nagios/nagios.log")