Exemplo n.º 1
0
def page_run_cron():
    # type: () -> None
    # Prevent cron jobs from being run too often, also we need
    # locking in order to prevent overlapping runs
    if os.path.exists(lock_file):
        last_run = os.stat(lock_file).st_mtime
        if time.time() - last_run < 59:
            raise MKGeneralException("Cron called too early. Skipping.")
    open(lock_file, "w")  # touches the file
    store.aquire_lock(lock_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.
    config.set_super_user()

    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:
            html.write("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")
    html.write("OK\n")
Exemplo n.º 2
0
    def __init__(self):
        super(ModeAutomation, self).__init__()

        # 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.
        config.set_super_user()
Exemplo n.º 3
0
def ajax_graph_images_for_notifications():
    graphs = []

    if html.request.remote_ip not in ["127.0.0.1", "::1"]:
        raise MKUnauthenticatedException(
            _("You are not allowed to access this page (%s).") %
            html.request.remote_ip)

    config.set_super_user()

    try:
        host_name = html.request.var("host")
        if not host_name:
            raise MKGeneralException(_("Missing mandatory \"host\" parameter"))

        service_description = html.request.var("service", "_HOST_")

        site = html.request.var("site")
        # FIXME: We should really enforce site here. But it seems that the notification context
        # has no idea about the site of the host. This could be optimized later.
        #if not site:
        #    raise MKGeneralException("Missing mandatory \"site\" parameter")
        try:
            row = get_graph_data_from_livestatus(site, host_name,
                                                 service_description)
        except livestatus.MKLivestatusNotFoundError:
            if config.debug:
                raise
            raise Exception(
                _("Cannot render graph: host %s, service %s not found.") %
                (host_name, service_description))

        site = row["site"]

        # Always use 25h graph in notifications
        end_time = time.time()
        start_time = end_time - (25 * 3600)

        graph_render_options = graph_image_render_options()

        graph_identification = (
            "template",
            {
                "site": site,
                "host_name": host_name,
                "service_description": service_description,
                "graph_index": None,  # all graphs
            })

        graph_data_range = graph_image_data_range(graph_render_options,
                                                  start_time, end_time)
        graph_recipes = graph_identification_types.create_graph_recipes(
            graph_identification,
            destination=html_render.GraphDestinations.notification)
        num_graphs = html.request.get_integer_input("num_graphs") or len(
            graph_recipes)

        for graph_recipe in graph_recipes[:num_graphs]:
            graph_artwork = artwork.compute_graph_artwork(
                graph_recipe, graph_data_range, graph_render_options)
            graph_png = render_graph_image(graph_artwork, graph_data_range,
                                           graph_render_options)

            graphs.append(base64.b64encode(graph_png).decode("ascii"))

        html.write(json.dumps(graphs))

    except Exception as e:
        logger.error("Call to ajax_graph_images.py failed: %s\n%s", e,
                     traceback.format_exc())
        if config.debug:
            raise
Exemplo n.º 4
0
def initialize_gui_environment() -> None:
    load_all_plugins()
    load_config()
    set_super_user()