Exemple #1
0
 def blocking():
     web_session = CMKWebSession(site)
     # disable content parsing on each request for performance reasons
     web_session._handle_http_response = lambda *args, **kwargs: None  # type: ignore
     web_session.login()
     web_session.enforce_non_localized_gui()
     return web_session
Exemple #2
0
    def init_wato(self):
        if not self._missing_but_required_wato_files():
            logger.info(
                "WATO is already initialized -> Skipping initializiation")
            return

        logger.debug("Initializing WATO...")

        web = CMKWebSession(self)
        web.login()

        # Call WATO once for creating the default WATO configuration
        logger.debug(
            "Requesting wato.py (which creates the WATO factory settings)...")
        response = web.get("wato.py?mode=sites").text
        # logger.debug("Debug: %r" % response)
        assert "site=%s" % web.site.id in response

        logger.debug("Waiting for WATO files to be created...")
        wait_time = 20.0
        while self._missing_but_required_wato_files() and wait_time >= 0:
            time.sleep(0.5)
            wait_time -= 0.5

        missing_files = self._missing_but_required_wato_files()
        assert not missing_files, ("Failed to initialize WATO data structures "
                                   "(Still missing: %s)" % missing_files)

        web.enforce_non_localized_gui()

        self._add_wato_test_config(web)
Exemple #3
0
 def _add_wato_test_config(self, web: CMKWebSession) -> None:
     # This entry is interesting because it is a check specific setting. These
     # settings are only registered during check loading. In case one tries to
     # load the config without loading the checks in advance, this leads into an
     # exception.
     # We set this config option here trying to catch this kind of issue.
     web.set_ruleset(
         "fileinfo_groups",
         {
             "ruleset": {
                 "": [  # "" -> folder
                     {
                         "condition": {},
                         "options": {},
                         "value": {"group_patterns": [("TESTGROUP", ("*gwia*", ""))]},
                     },
                 ],
             }
         },
     )
Exemple #4
0
    def prepare_for_tests(self) -> None:
        self.verify_cmk()

        web = CMKWebSession(self)
        web.login()
        web.enforce_non_localized_gui()
        self._add_wato_test_config(web)
Exemple #5
0
    def _new_site(self, name: str) -> Site:
        site = self._site_obj(name)

        site.create()
        self._sites[site.id] = site

        site.open_livestatus_tcp(encrypted=False)
        site.start()
        site.prepare_for_tests()
        # There seem to be still some changes that want to be activated
        CMKWebSession(site).activate_changes()
        logger.debug("Created site %s", site.id)
        return site
Exemple #6
0
def web(site: Site):
    web = CMKWebSession(site)
    web.login()
    web.enforce_non_localized_gui()
    return web
Exemple #7
0
 def __init__(self, site: Site):
     super().__init__()
     self.site = site
     self.status = CMKEventConsoleStatus("%s/tmp/run/mkeventd/status" % site.root)
     self.web_session = CMKWebSession(site)
Exemple #8
0
class CMKEventConsole:
    def __init__(self, site: Site):
        super().__init__()
        self.site = site
        self.status = CMKEventConsoleStatus("%s/tmp/run/mkeventd/status" % site.root)
        self.web_session = CMKWebSession(site)

    def _config(self):
        cfg: Dict[str, Any] = {}
        content = self.site.read_file("etc/check_mk/mkeventd.d/wato/global.mk")
        exec(content, {}, cfg)
        return cfg

    def _gather_status_port(self):
        config = self._config()

        if self.site.reuse and self.site.exists() and "remote_status" in config:
            port = config["remote_status"][0]
        else:
            port = self.site.get_free_port_from(self.site.livestatus_port + 1)

        self.status_port = port

    def enable_remote_status_port(self, web):
        html = web.get("wato.py?mode=mkeventd_config").text
        assert "mode=mkeventd_edit_configvar&site=&varname=remote_status" in html

        html = web.get(
            "wato.py?folder=&mode=mkeventd_edit_configvar&site=&varname=remote_status"
        ).text
        assert "Save" in html

        html = web.post(
            "wato.py",
            data={
                "filled_in": "value_editor",
                "ve_use": "on",
                "ve_value_0": self.status_port,
                "ve_value_2_use": "on",
                "ve_value_2_value_0": "127.0.0.1",
                "save": "Save",
                "varname": "remote_status",
                "mode": "mkeventd_edit_configvar",
            },
            add_transid=True,
        ).text
        assert "%d, no commands, 127.0.0.1" % self.status_port in html

    def activate_changes(self, web):
        old_t = web.site.live.query_value(
            "GET eventconsolestatus\nColumns: status_config_load_time\n"
        )
        assert old_t > time.time() - 86400

        self.web_session.activate_changes(allow_foreign_changes=True)

        def config_reloaded():
            new_t = web.site.live.query_value(
                "GET eventconsolestatus\nColumns: status_config_load_time\n"
            )
            return new_t > old_t

        reload_time, timeout = time.time(), 10
        while not config_reloaded():
            if time.time() > reload_time + timeout:
                raise Exception("Config did not update within %d seconds" % timeout)
            time.sleep(0.2)

        assert config_reloaded()

    @classmethod
    def new_event(cls, attrs):
        now = time.time()
        default_event = {
            "rule_id": 815,
            "text": "",
            "phase": "open",
            "count": 1,
            "time": now,
            "first": now,
            "last": now,
            "comment": "",
            "host": "test-host",
            "ipaddress": "127.0.0.1",
            "application": "",
            "pid": 0,
            "priority": 3,
            "facility": 1,  # user
            "match_groups": (),
        }

        event = default_event.copy()
        event.update(attrs)
        return event