Example #1
0
def create_linux_test_host(request, web_fixture, site, hostname):
    def finalizer():
        web_fixture.delete_host(hostname)
        web_fixture.activate_changes()

        for path in [
                "var/check_mk/agent_output/%s" % hostname,
                "etc/check_mk/conf.d/linux_test_host_%s.mk" % hostname,
                "tmp/check_mk/status_data/%s" % hostname,
                "tmp/check_mk/status_data/%s.gz" % hostname,
                "var/check_mk/inventory/%s" % hostname,
                "var/check_mk/inventory/%s.gz" % hostname,
        ]:
            if os.path.exists(path):
                site.delete_file(path)

    request.addfinalizer(finalizer)

    web_fixture.add_host(hostname, attributes={"ipaddress": "127.0.0.1"})

    site.write_file(
        "etc/check_mk/conf.d/linux_test_host_%s.mk" % hostname,
        "datasource_programs.append(('cat ~/var/check_mk/agent_output/<HOST>', [], ['%s']))\n" %
        hostname)

    site.makedirs("var/check_mk/agent_output/")
    site.write_file("var/check_mk/agent_output/%s" % hostname, get_standard_linux_agent_output())
Example #2
0
def fixture_local_test_hosts(web, site):  # noqa: F811 # pylint: disable=redefined-outer-name
    site.makedirs("var/check_mk/agent_output/")

    web.add_hosts([
        ("test-host", "", {
            "ipaddress": "127.0.0.1",
        }),
        ("test-host2", "xy/zzz", {
            "ipaddress": "127.0.0.1",
        }),
    ])

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

    for hostname in ["test-host", "test-host2"]:
        site.write_file("var/check_mk/agent_output/%s" % hostname,
                        get_standard_linux_agent_output())

    yield

    for hostname in ["test-host", "test-host2"]:
        web.delete_host(hostname)
        site.delete_file("var/check_mk/agent_output/%s" % hostname)
    site.delete_file("etc/check_mk/conf.d/local-test-hosts.mk")
Example #3
0
def scenario_fixture(monkeypatch):
    test_hosts = ["ds-test-host1", "ds-test-host2", "ds-test-node1", "ds-test-node2"]

    ts = Scenario()

    for h in test_hosts:
        ts.add_host(h)

    ts.set_option("ipaddresses", dict((h, "127.0.0.1") for h in test_hosts))
    ts.add_cluster("ds-test-cluster1", nodes=["ds-test-node1", "ds-test-node2"])

    ts.set_ruleset(
        "datasource_programs",
        [
            ("cat %s/<HOST>" % cmk.utils.paths.tcp_cache_dir, [], test_hosts, {}),
        ],
    )

    linux_agent_output = get_standard_linux_agent_output()

    for h in test_hosts:
        cache_path = Path(cmk.utils.paths.tcp_cache_dir, h)
        cache_path.parent.mkdir(parents=True, exist_ok=True)

        with cache_path.open("w", encoding="utf-8") as f:
            f.write(linux_agent_output)

    return ts.apply(monkeypatch)
Example #4
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",
                        get_standard_linux_agent_output())

        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 range(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,
                                    encoding="utf-8")
            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()
Example #5
0
    def fake_standard_linux_agent_output(self, *test_hosts):
        self.set_ruleset(
            "datasource_programs",
            [
                ("cat %s/<HOST>" % cmk.utils.paths.tcp_cache_dir, [],
                 test_hosts, {}),
            ],
        )
        linux_agent_output = get_standard_linux_agent_output()

        for h in test_hosts:
            cache_path = Path(cmk.utils.paths.tcp_cache_dir, h)
            cache_path.parent.mkdir(parents=True, exist_ok=True)
            with cache_path.open("w", encoding="utf-8") as f:
                f.write(linux_agent_output)
Example #6
0
def test_dump_agent_test(execute):
    for opt in ["--dump-agent", "-d"]:
        p = execute(["cmk", opt, "modes-test-host"])
        assert p.returncode == 0
        assert p.stderr == ''
        assert p.stdout == get_standard_linux_agent_output()