def test_host_downtime_with_services(mock_livestatus, with_request_context, dates): start_time, end_time = dates with mock_livestatus( expect_status_query=True) as live, application_and_request_context( ), SuperUserContext(): load_config() live.expect_query( "GET hosts\nColumns: name\nFilter: name = example.com") live.expect_query( "COMMAND [...] SCHEDULE_HOST_DOWNTIME;example.com;0;86400;12;0;120;;Going down", match_type="ellipsis", ) live.expect_query( "GET services\nColumns: host_name description\nFilter: host_name = example.com", ) live.expect_query( "COMMAND [...] SCHEDULE_SVC_DOWNTIME;example.com;Memory;0;86400;12;0;120;;Going down", match_type="ellipsis", ) live.expect_query( "COMMAND [...] SCHEDULE_SVC_DOWNTIME;example.com;CPU load;0;86400;12;0;120;;Going down", match_type="ellipsis", ) downtimes.schedule_host_downtime( sites.live(), "example.com", start_time, end_time, include_all_services=True, recur="weekday_start", duration=120, comment="Going down", )
def test_hostgroup_host_downtime(mock_livestatus, with_request_context, dates): start_time, end_time = dates with mock_livestatus( expect_status_query=True) as live, application_and_request_context( ), SuperUserContext(): load_config() live.expect_query([ "GET hostgroups", "Columns: members", "Filter: name = example", ]) live.expect_query( "GET hosts\nColumns: name\nFilter: name = example.com\nFilter: name = heute\nOr: 2" ) live.expect_query( "COMMAND [...] SCHEDULE_HOST_DOWNTIME;heute;0;86400;16;0;120;;Boom", match_type="ellipsis", ) live.expect_query( "COMMAND [...] SCHEDULE_HOST_DOWNTIME;example.com;0;86400;16;0;120;;Boom", match_type="ellipsis", ) downtimes.schedule_hostgroup_host_downtime( sites.live(), "example", start_time, end_time, recur="day_of_month", duration=120, comment="Boom", )
def handle_page(self): # The automation page is accessed unauthenticated. After leaving the index.py area # into the page handler we always want to have a user context initialized to keep # the code free from special cases (if no user logged in, then...). So fake the # logged in user here. with SuperUserContext(): self._handle_exc(self.page)
def page_run_cron() -> None: lock_file = _lock_file() # Prevent cron jobs from being run too often, also we need # locking in order to prevent overlapping runs if lock_file.exists(): last_run = lock_file.stat().st_mtime if time.time() - last_run < 59: raise MKGeneralException("Cron called too early. Skipping.") with lock_file.open("wb"): pass # touches the file # The cron page is accessed unauthenticated. After leaving the page_run_cron area # into the job functions we always want to have a user context initialized to keep # the code free from special cases (if no user logged in, then...). # The jobs need to be run in privileged mode in general. Some jobs, like the network # scan, switch the user context to a specific other user during execution. with store.locked(lock_file), SuperUserContext(): logger.debug("Starting cron jobs") for cron_job in multisite_cronjobs: try: job_name = cron_job.__name__ logger.debug("Starting [%s]", job_name) cron_job() logger.debug("Finished [%s]", job_name) except Exception: response.set_data("An exception occured. Take a look at the web.log.\n") logger.exception("Exception in cron job [%s]", job_name) logger.debug("Finished all cron jobs") response.set_data("OK\n")
def ajax_graph_images_for_notifications(): if request.remote_ip not in ["127.0.0.1", "::1"]: raise MKUnauthenticatedException( _("You are not allowed to access this page (%s).") % request.remote_ip) with SuperUserContext(): _answer_graph_image_request()
def delete(params): """Delete a contact group""" name = params["name"] check_modify_group_permissions("contact") with endpoint.do_not_track_permissions(), SuperUserContext(): # HACK: We need to supress this, due to lots of irrelevant dashboard permissions watolib.delete_group(name, "contact") return Response(status=204)
def bulk_delete(params): """Bulk delete contact groups""" body = params["body"] entries = body["entries"] for group_name in entries: _group = fetch_group( group_name, "contact", status=400, message=f"contact group {group_name} was not found", ) with endpoint.do_not_track_permissions(), SuperUserContext(): for group_name in entries: # We need to supress this, because a lot of dashboard permissions are checked for # various reasons. delete_group(group_name, "contact") return Response(status=204)
def run(arguments: argparse.Namespace, old_site_id: SiteId, new_site_id: SiteId) -> bool: has_errors = False logger.debug("Initializing application...") main_modules.load_plugins() with gui_context(), SuperUserContext(): logger.debug("Starting actions...") actions = sorted(rename_action_registry.values(), key=lambda a: a.sort_index) total = len(actions) for count, rename_action in enumerate(actions, start=1): logger.log(VERBOSE, " %i/%i %s...", count, total, rename_action.title) try: rename_action.run(old_site_id, new_site_id) except Exception: has_errors = True logger.error(' + "%s" failed', rename_action.title, exc_info=True) if arguments.debug: raise logger.log(VERBOSE, "Done") return has_errors
def _run_as_superuser() -> Iterator[None]: cmk.gui.config.load_config() with SuperUserContext(): yield None
def _build_index( self, match_item_generators: Iterable[ABCMatchItemGenerator], ) -> None: with SuperUserContext(): self._do_build_index(match_item_generators)
def fixture_gui_context(): with gui_context(), SuperUserContext(): yield