Esempio n. 1
0
def graph_test_config(web, site):
    # No graph yet...
    with pytest.raises(APIError) as exc_info:
        web.get_regular_graph("test-host-get-graph",
                              "Check_MK",
                              0,
                              expect_error=True)
        assert "Cannot calculate graph recipes" in "%s" % exc_info

    try:
        # Now add the host
        web.add_host("test-host-get-graph",
                     attributes={
                         "ipaddress": "127.0.0.1",
                     })

        site.write_file(
            "etc/check_mk/conf.d/test-host-get-graph.mk",
            "datasource_programs.append(('cat ~/var/check_mk/agent_output/<HOST>', [], ['test-host-get-graph']))\n"
        )

        site.makedirs("var/check_mk/agent_output/")
        site.write_file(
            "var/check_mk/agent_output/test-host-get-graph",
            open(
                "%s/tests/integration/cmk/base/test-files/linux-agent-output" %
                repo_path()).read())

        web.discover_services("test-host-get-graph")
        web.activate_changes()
        site.schedule_check("test-host-get-graph", "Check_MK", 0)

        # Wait for RRD file creation. Isn't this a bug that the graph is not instantly available?
        rrd_path = site.path(
            "var/check_mk/rrd/test-host-get-graph/Check_MK.rrd")
        for attempt in xrange(50):
            time.sleep(0.1)
            proc = subprocess.Popen([
                site.path("bin/unixcat"),
                site.path("tmp/run/rrdcached.sock")
            ],
                                    stdin=subprocess.PIPE,
                                    stdout=subprocess.PIPE,
                                    stderr=subprocess.PIPE)
            out, err = proc.communicate("FLUSH %s\n" % rrd_path)
            if os.path.exists(rrd_path):
                break
            sys.stdout.write("waiting for %s (attempt %d)%s%s\n" % (
                rrd_path,
                attempt + 1,  #
                ", stdout: %s" % out if out else "",
                ", stderr: %s" % err if err else ""))
        else:
            assert False, "RRD file %s missing" % rrd_path

        yield
    finally:
        web.delete_host("test-host-get-graph")
        site.delete_file("etc/check_mk/conf.d/test-host-get-graph.mk")
    web.activate_changes()
Esempio n. 2
0
def test_discover_services(web):
    try:
        web.add_host("test-host-discovery", attributes={
            "ipaddress": "127.0.0.1",
        })

        web.discover_services("test-host-discovery")
    finally:
        web.delete_host("test-host-discovery")
Esempio n. 3
0
def test_discover_servics(web):
    try:
        web.add_host("test-host-discovery", attributes={
            "ipaddress": "127.0.0.1",
        })

        web.discover_services("test-host-discovery")
    finally:
        web.delete_host("test-host-discovery")
Esempio n. 4
0
def test_discover_services(web):  # noqa: F811 # pylint: disable=redefined-outer-name
    try:
        web.add_host("test-host-discovery", attributes={
            "ipaddress": "127.0.0.1",
        })

        web.discover_services("test-host-discovery")
    finally:
        web.delete_host("test-host-discovery")
Esempio n. 5
0
def graph_test_config(web, site):
    # No graph yet...
    with pytest.raises(APIError) as e:
        web.get_regular_graph("test-host-get-graph",
                              "Check_MK",
                              0,
                              expect_error=True)
        assert "Cannot calculate graph recipes" in "%s" % e

    try:
        # Now add the host
        web.add_host("test-host-get-graph",
                     attributes={
                         "ipaddress": "127.0.0.1",
                     })

        site.write_file(
            "etc/check_mk/conf.d/test-host-get-graph.mk",
            "datasource_programs.append(('cat ~/var/check_mk/agent_output/<HOST>', [], ['test-host-get-graph']))\n"
        )

        site.makedirs("var/check_mk/agent_output/")
        site.write_file(
            "var/check_mk/agent_output/test-host-get-graph",
            file(
                "%s/tests/integration/cmk_base/test-files/linux-agent-output" %
                repo_path()).read())

        web.discover_services("test-host-get-graph")
        web.activate_changes()
        site.schedule_check("test-host-get-graph", "Check_MK", 0)

        # Wait for RRD file creation
        # Isn't this a bug that the graph is not instantly available?
        timeout = 10
        print "Checking for graph..."
        while timeout and not site.file_exists(
                "var/check_mk/rrd/test-host-get-graph/Check_MK.rrd"):
            try:
                web.get_regular_graph("test-host-get-graph",
                                      "Check_MK",
                                      0,
                                      expect_error=True)
            except Exception:
                pass
            timeout -= 1
            time.sleep(1)
            print "Checking for graph..."
            assert site.file_exists("var/check_mk/rrd/test-host-get-graph/Check_MK.rrd"), \
                "RRD %s is still missing" % "var/check_mk/rrd/test-host-get-graph/Check_MK.rrd"

        yield
    finally:
        web.delete_host("test-host-get-graph")
        site.delete_file("etc/check_mk/conf.d/test-host-get-graph.mk")
    web.activate_changes()
Esempio n. 6
0
def default_cfg_fixture(request, site, web):  # noqa: F811 # pylint: disable=redefined-outer-name
    config = DefaultConfig(core=request.param)
    site.set_config("CORE", config.core, with_restart=True)

    print("Applying default config (%s)" % config.core)
    create_linux_test_host(request, web, site, "livestatus-test-host")
    create_linux_test_host(request, web, site, "livestatus-test-host.domain")
    web.discover_services("livestatus-test-host")
    web.activate_changes()
    return config
Esempio n. 7
0
def default_cfg(request, site, web):
    config = DefaultConfig(core=request.param)
    site.set_config("CORE", config.core, with_restart=True)

    print "Applying default config (%s)" % config.core
    create_linux_test_host(request, web, site, "livestatus-test-host")
    create_linux_test_host(request, web, site, "livestatus-test-host.domain")
    web.discover_services("livestatus-test-host")
    web.activate_changes()
    return config
Esempio n. 8
0
def test_get_graph(web, site):
    try:
        # No graph yet...
        with pytest.raises(APIError) as e:
            data = web.get_regular_graph("test-host-get-graph",
                                         "Check_MK",
                                         0,
                                         expect_error=True)
            assert "Cannot calculate graph recipes" in "%s" % e

        # Now add the host
        web.add_host("test-host-get-graph",
                     attributes={
                         "ipaddress": "127.0.0.1",
                     })
        web.discover_services("test-host-get-graph")
        web.activate_changes()

        # Issue a reschedule
        site.live.command(
            "SCHEDULE_FORCED_SERVICE_CHECK;test-host-get-graph;Check_MK;%d" %
            int(time.time()))

        # Wait for RRD file creation
        # Isn't this a bug that the graph is not instantly available?
        timeout = 10
        print "Checking for graph..."
        while timeout and not site.file_exists(
                "var/check_mk/rrd/test-host-get-graph/Check_MK.rrd"):
            try:
                data = web.get_regular_graph("test-host-get-graph",
                                             "Check_MK",
                                             0,
                                             expect_error=True)
            except Exception:
                pass
            timeout -= 1
            time.sleep(1)
            print "Checking for graph..."
        assert site.file_exists("var/check_mk/rrd/test-host-get-graph/Check_MK.rrd"), \
                        "RRD %s is still missing" % "var/check_mk/rrd/test-host-get-graph/Check_MK.rrd"

        _test_get_graph_api(web)
        _test_get_graph_image(web)
        _test_get_graph_notification_image(web)

    finally:
        web.delete_host("test-host-get-graph")
        web.activate_changes()
Esempio n. 9
0
def default_cfg(web):
    web.add_host("livestatus-test-host", attributes={
        "ipaddress": "127.0.0.1",
    })

    web.discover_services("livestatus-test-host")

    web.activate_changes()
    yield None

    #
    # Cleanup code
    #

    web.delete_host("livestatus-test-host")
Esempio n. 10
0
def default_cfg(web):
    web.add_host("livestatus-test-host",
                 attributes={
                     "ipaddress": "127.0.0.1",
                 })

    web.discover_services("livestatus-test-host")

    web.activate_changes()
    yield None

    #
    # Cleanup code
    #

    web.delete_host("livestatus-test-host")
Esempio n. 11
0
def default_cfg(web):
    print "Applying default config"
    web.add_host("livestatus-test-host",
                 attributes={
                     "ipaddress": "127.0.0.1",
                 })

    web.discover_services("livestatus-test-host")

    web.activate_changes()
    yield None

    #
    # Cleanup code
    #
    print "Cleaning up default config"

    web.delete_host("livestatus-test-host")
Esempio n. 12
0
def test_cfg(web, site):
    print("Applying default config")
    web.add_host("modes-test-host", attributes={
        "ipaddress": "127.0.0.1",
    })
    web.add_host("modes-test-host2",
                 attributes={
                     "ipaddress": "127.0.0.1",
                     "tag_criticality": "test",
                 })
    web.add_host("modes-test-host3",
                 attributes={
                     "ipaddress": "127.0.0.1",
                     "tag_criticality": "test",
                 })
    web.add_host("modes-test-host4",
                 attributes={
                     "ipaddress": "127.0.0.1",
                     "tag_criticality": "offline",
                 })

    site.write_file(
        "etc/check_mk/conf.d/modes-test-host.mk",
        "datasource_programs.append(('cat ~/var/check_mk/agent_output/<HOST>', [], ALL_HOSTS))\n"
    )

    site.makedirs("var/check_mk/agent_output/")
    site.write_file(
        "var/check_mk/agent_output/modes-test-host",
        file("%s/tests/integration/cmk_base/test-files/linux-agent-output" %
             repo_path()).read())
    site.write_file(
        "var/check_mk/agent_output/modes-test-host2",
        file("%s/tests/integration/cmk_base/test-files/linux-agent-output" %
             repo_path()).read())
    site.write_file(
        "var/check_mk/agent_output/modes-test-host3",
        file("%s/tests/integration/cmk_base/test-files/linux-agent-output" %
             repo_path()).read())

    web.discover_services("modes-test-host")
    web.discover_services("modes-test-host2")
    web.discover_services("modes-test-host3")

    web.activate_changes()
    yield None

    #
    # Cleanup code
    #
    print("Cleaning up test config")

    site.delete_dir("var/check_mk/agent_output")

    site.delete_file("etc/check_mk/conf.d/modes-test-host.mk")

    web.delete_host("modes-test-host")
    web.delete_host("modes-test-host2")
    web.delete_host("modes-test-host3")
    web.delete_host("modes-test-host4")
Esempio n. 13
0
def test_test_check_2(request, test_cfg, site, web):
    test_check_path = "local/share/check_mk/checks/test_check_2"

    def cleanup():
        if site.file_exists("etc/check_mk/conf.d/test_check_2.mk"):
            site.delete_file("etc/check_mk/conf.d/test_check_2.mk")
        if site.file_exists("var/check_mk/autochecks/modes-test-host.mk"):
            site.delete_file("var/check_mk/autochecks/modes-test-host.mk")
        site.delete_file(test_check_path)

    request.addfinalizer(cleanup)

    site.write_file(
        test_check_path, """

discover_service = False

def inventory(info):
    if discover_service:
        return [(None, {})]

def check(item, params, info):
    return 0, "OK, discovered!"

check_info["test_check_2"] = {
    "check_function"      : check,
    "inventory_function"  : inventory,
    "service_description" : "Testcheck 2",
}
""")

    site.write_file("var/check_mk/agent_output/modes-test-host", "<<<test_check_2>>>\n1 2\n")

    config.load_checks(check_api.get_check_api_context, ["%s/%s" % (site.root, test_check_path)])
    config.load(with_conf_d=False)

    # Verify that the default variable is in the check context and
    # not in the global checks module context
    assert "discover_service" not in config.__dict__
    assert "test_check_2" in config._check_contexts
    assert "discover_service" in config._check_contexts["test_check_2"]

    web.discover_services("modes-test-host")

    # Should have discovered nothing so far
    assert site.read_file("var/check_mk/autochecks/modes-test-host.mk") == "[\n]\n"

    web.discover_services("modes-test-host")

    # And now overwrite the setting in the config
    site.write_file("etc/check_mk/conf.d/test_check_2.mk", "discover_service = True\n")

    web.discover_services("modes-test-host")

    # Verify that the discovery worked as expected
    services = autochecks.parse_autochecks_file("modes-test-host")
    assert services[0].check_plugin_name == "test_check_2"
    assert services[0].item is None
    assert services[0].parameters_unresolved == "{}"
    assert services[0].service_labels.to_dict() == {}
Esempio n. 14
0
def test_cfg_fixture(web, site):  # noqa: F811 # pylint: disable=redefined-outer-name
    print("Applying default config")
    web.add_host("modes-test-host", attributes={
        "ipaddress": "127.0.0.1",
    })
    web.add_host("modes-test-host2",
                 attributes={
                     "ipaddress": "127.0.0.1",
                     "tag_criticality": "test",
                 })
    web.add_host("modes-test-host3",
                 attributes={
                     "ipaddress": "127.0.0.1",
                     "tag_criticality": "test",
                 })
    web.add_host("modes-test-host4",
                 attributes={
                     "ipaddress": "127.0.0.1",
                     "tag_criticality": "offline",
                 })

    site.write_file(
        "etc/check_mk/conf.d/modes-test-host.mk",
        "datasource_programs.append(('cat ~/var/check_mk/agent_output/<HOST>', [], ALL_HOSTS))\n"
    )

    site.makedirs("var/check_mk/agent_output/")
    site.write_file("var/check_mk/agent_output/modes-test-host",
                    get_standard_linux_agent_output())
    site.write_file("var/check_mk/agent_output/modes-test-host2",
                    get_standard_linux_agent_output())
    site.write_file("var/check_mk/agent_output/modes-test-host3",
                    get_standard_linux_agent_output())

    web.discover_services("modes-test-host")
    web.discover_services("modes-test-host2")
    web.discover_services("modes-test-host3")

    try:
        web.activate_changes()
        yield None
    finally:
        #
        # Cleanup code
        #
        print("Cleaning up test config")

        site.delete_dir("var/check_mk/agent_output")

        site.delete_file("etc/check_mk/conf.d/modes-test-host.mk")

        web.delete_host("modes-test-host")
        web.delete_host("modes-test-host2")
        web.delete_host("modes-test-host3")
        web.delete_host("modes-test-host4")

        web.activate_changes()
Esempio n. 15
0
def test_test_check_1(request, test_cfg, site, web):

    test_check_path = "local/share/check_mk/checks/test_check_1"

    def cleanup():
        if site.file_exists("etc/check_mk/conf.d/test_check_1.mk"):
            site.delete_file("etc/check_mk/conf.d/test_check_1.mk")

        if site.file_exists("var/check_mk/autochecks/modes-test-host.mk"):
            site.delete_file("var/check_mk/autochecks/modes-test-host.mk")

        site.delete_file(test_check_path)

    request.addfinalizer(cleanup)

    site.write_file(
        test_check_path, """

test_check_1_default_levels = 10.0, 20.0

def inventory(info):
    return [(None, "test_check_1_default_levels")]

def check(item, params, info):
    return 0, "OK - %r" % (test_check_1_default_levels, )

check_info["test_check_1"] = {
    "check_function"          : check,
    "inventory_function"      : inventory,
    "service_description"     : "Testcheck 1",
#    "default_levels_variable" : "test_check_1_default_levels"
}
""")

    site.write_file("var/check_mk/agent_output/modes-test-host",
                    "<<<test_check_1>>>\n1 2\n")

    config.load_checks(check_api.get_check_api_context,
                       ["%s/%s" % (site.root, test_check_path)])
    config.load(with_conf_d=False)

    # Verify that the default variable is in the check context and
    # not in the global checks module context.
    assert "test_check_1_default_levels" not in config.__dict__
    assert "test_check_1" in config._check_contexts
    assert "test_check_1_default_levels" in config._check_contexts[
        "test_check_1"]

    web.discover_services("modes-test-host")

    # Verify that the discovery worked as expected
    services = autochecks.parse_autochecks_file("modes-test-host",
                                                config.service_description)
    assert services[0].check_plugin_name == "test_check_1"
    assert services[0].item is None
    assert services[0].parameters_unresolved == "test_check_1_default_levels"
    assert services[0].service_labels.to_dict() == {}

    # Now execute the check function to verify the variable is available
    p = site.execute(["cmk", "-nv", "modes-test-host"],
                     stdout=subprocess.PIPE,
                     stderr=subprocess.PIPE)
    stdout, stderr = p.communicate()
    assert "OK - (10.0, 20.0)" in stdout
    assert stderr == ''
    assert p.returncode == 0

    # And now overwrite the setting in the config
    site.write_file("etc/check_mk/conf.d/test_check_1.mk",
                    "test_check_1_default_levels = 5.0, 30.1\n")

    p = site.execute(["cmk", "-nv", "modes-test-host"],
                     stdout=subprocess.PIPE,
                     stderr=subprocess.PIPE)
    stdout, stderr = p.communicate()
    assert "OK - (10.0, 20.0)" not in stdout
    assert "OK - (5.0, 30.1)" in stdout
    assert stderr == ''
    assert p.returncode == 0
Esempio n. 16
0
def test_discover_services(web, local_test_hosts):  # noqa: F811 # pylint: disable=redefined-outer-name
    web.discover_services("test-host")
Esempio n. 17
0
def test_check_factory_settings(request, test_cfg, site, web):
    test_check_path = "local/share/check_mk/checks/test_check_3"

    def cleanup():
        if site.file_exists("etc/check_mk/conf.d/test_check_3.mk"):
            site.delete_file("etc/check_mk/conf.d/test_check_3.mk")
        if site.file_exists("var/check_mk/autochecks/modes-test-host.mk"):
            site.delete_file("var/check_mk/autochecks/modes-test-host.mk")
        site.delete_file(test_check_path)

    request.addfinalizer(cleanup)

    site.write_file(
        test_check_path, """

factory_settings["test_check_3_default_levels"] = {
    "param1": 123,
}

def inventory(info):
    return [(None, {})]

def check(item, params, info):
    return 0, "OK - %r" % (params, )

check_info["test_check_3"] = {
    "check_function"          : check,
    "inventory_function"      : inventory,
    "service_description"     : "Testcheck 3",
    "group"                   : "asd",
    "default_levels_variable" : "test_check_3_default_levels",
}
""")

    site.write_file("var/check_mk/agent_output/modes-test-host",
                    "<<<test_check_3>>>\n1 2\n")

    config.load_checks(check_api.get_check_api_context,
                       ["%s/%s" % (site.root, test_check_path)])

    # Verify that the default variable is in the check context and
    # not in the global checks module context
    assert "test_check_3_default_levels" not in config.__dict__
    assert "test_check_3" in config._check_contexts
    assert "test_check_3_default_levels" in config._check_contexts[
        "test_check_3"]

    web.discover_services("modes-test-host")

    # Verify that the discovery worked as expected
    services = autochecks.parse_autochecks_file("modes-test-host",
                                                config.service_description)
    assert services[0].check_plugin_name == "test_check_3"
    assert services[0].item is None
    assert services[0].parameters_unresolved == "{}"
    assert services[0].service_labels.to_dict() == {}

    # Now execute the check function to verify the variable is available
    p = site.execute(["cmk", "-nv", "modes-test-host"],
                     stdout=subprocess.PIPE,
                     stderr=subprocess.PIPE)
    stdout, stderr = p.communicate()
    assert "OK - {'param1': 123}\n" in stdout
    assert stderr == ''
    assert p.returncode == 0

    # And now overwrite the setting in the config
    site.write_file(
        "etc/check_mk/conf.d/test_check_3.mk", """
checkgroup_parameters.setdefault('asd', [])

checkgroup_parameters['asd'] = [
    ( {'param2': 'xxx'}, [], ALL_HOSTS, {} ),
] + checkgroup_parameters['asd']
""")

    # And execute the check again to check for the parameters
    p = site.execute(["cmk", "-nv", "modes-test-host"],
                     stdout=subprocess.PIPE,
                     stderr=subprocess.PIPE)
    stdout, stderr = p.communicate()
    assert "'param1': 123" in stdout
    assert "'param2': 'xxx'" in stdout
    assert stderr == ''
    assert p.returncode == 0