def test_load_data_from_not_permitted_file(tmpdir):
    locked_file = tmpdir.join("test")
    locked_file.write("[1, 2]")
    os.chmod("%s" % locked_file, 0200)

    with pytest.raises(MKGeneralException) as e:
        store.load_data_from_file("%s" % locked_file)
    assert "%s" % locked_file in "%s" % e
    assert "Permission denied" in "%s" % e
def test_load_data_from_file_locking(tmpdir):
    locked_file = tmpdir.join("locked_file")
    locked_file.write("[1, 2]")

    data = store.load_data_from_file("%s" % locked_file, lock=True)
    assert data == [1, 2]
    assert store.have_lock("%s" % locked_file) == True
Exemple #3
0
 def load_guitest(self, name):
     path = cmk.paths.var_dir + "/guitests/" + name + ".mk"
     data = store.load_data_from_file(path)
     if data == None:
         raise MKGeneralException(_("GUI test file does not exist %s.") % path)
     else:
         return data
Exemple #4
0
    def load(cls):
        cls.clear_instances()

        # First load builtin pages. Set username to ''
        for name, page_dict in cls.builtin_pages().items():
            page_dict["owner"] = ''  # might have been forgotten on copy action
            page_dict["public"] = True
            page_dict["name"] = name
            new_page = cls(page_dict)
            cls.add_instance(("", name), new_page)

        # Now scan users subdirs for files "user_$type_name.mk"
        subdirs = os.listdir(config.config_dir)
        for user in subdirs:
            try:
                path = "%s/%s/user_%ss.mk" % (config.config_dir, user,
                                              cls.type_name())
                if not os.path.exists(path):
                    continue

                if not userdb.user_exists(user):
                    continue

                user_pages = store.load_data_from_file(path, {})
                for name, page_dict in user_pages.items():
                    page_dict["owner"] = user
                    page_dict["name"] = name
                    cls.add_instance((user, name), cls(page_dict))

            except SyntaxError, e:
                raise MKGeneralException(
                    _("Cannot load %s from %s: %s") %
                    (cls.type_name(), path, e))
Exemple #5
0
 def load_guitest(self, name):
     path = cmk.paths.var_dir + "/guitests/" + name + ".mk"
     data = store.load_data_from_file(path)
     if data == None:
         raise MKGeneralException(_("GUI test file does not exist %s.") % path)
     else:
         return data
Exemple #6
0
    def load(self):
        self.clear_instances()

        # First load builtin pages. Set username to ''
        for name, page_dict in self.builtin_pages().items():
            page_dict["owner"]  = '' # might have been forgotten on copy action
            page_dict["public"] = True
            page_dict["name"]   = name
            new_page = self(page_dict)
            self.add_instance(("", name), new_page)

        # Now scan users subdirs for files "user_$type_name.mk"
        subdirs = os.listdir(config.config_dir)
        for user in subdirs:
            try:
                path = "%s/%s/user_%ss.mk" % (config.config_dir, user, self.type_name())
                if not os.path.exists(path):
                    continue

                if not userdb.user_exists(user):
                    continue

                user_pages = store.load_data_from_file(path, {})
                for name, page_dict in user_pages.items():
                    page_dict["owner"] = user
                    page_dict["name"] = name
                    self.add_instance((user, name), self(page_dict))

            except SyntaxError, e:
                raise MKGeneralException(_("Cannot load %s from %s: %s") %
                                                (self.type_name(), path, e))
def test_load_data_from_file_dict(tmpdir):
    locked_file = tmpdir.join("test")
    locked_file.write(repr({"1": 2, "ä": u"ß"}))

    data = store.load_data_from_file("%s" % locked_file)
    assert type(data) == dict
    assert data["1"] == 2
    assert type(data["ä"]) == unicode
    assert data["ä"] == u"ß"
Exemple #8
0
def load_historic_host(hostname, timestamp):
    if '/' in hostname:
        return None  # just for security reasons

    path = cmk.paths.var_dir + "/inventory/" + hostname

    # Try current tree
    if int(os.stat(path).st_mtime) == timestamp:
        return host(hostname)

    path = cmk.paths.var_dir + "/inventory_archive/" + hostname + "/%d" % timestamp
    return store.load_data_from_file(path, {})
Exemple #9
0
def load_historic_host(hostname, timestamp):
    if '/' in hostname:
        return None # just for security reasons

    path = cmk.paths.var_dir + "/inventory/" + hostname

    # Try current tree
    if int(os.stat(path).st_mtime) == timestamp:
        return host(hostname)

    path = cmk.paths.var_dir + "/inventory_archive/" + hostname + "/%d" % timestamp
    return store.load_data_from_file(path, {})
Exemple #10
0
    def save_guitest_step(self, step):
        path = cmk.paths.var_dir + "/guitests/RECORD"
        test_steps = store.load_data_from_file(path, [])

        if self.guitest_repair_step != None:
            if self.guitest_repair_step > len(test_steps):
                raise MKGeneralException("Test step for repairing is %s, but test only has %d steps." %
                                          (self.guitest_repair_step, len(test_steps)))
            mod_step = test_steps[self.guitest_repair_step]
            mod_step["output"] = step["output"]
            mod_step["user"] = step["user"]
            mod_step["elapsed_time"] = step["elapsed_time"]
        else:
            test_steps.append(step)
        file(path, "w").write("%s\n" % pprint.pformat(test_steps))
Exemple #11
0
def test_save_data_to_file_not_pretty(tmpdir):
    f = tmpdir.join("test")
    path = "%s" % f

    data = {
        "asdasaaaaaaaaaaaaaaaaaaaad": "asbbbbbbbbbbbbbbbbbbd",
        "1asdasaaaaaaaaaaaaaaaaaaaad": "asbbbbbbbbbbbbbbbbbbd",
        "2asdasaaaaaaaaaaaaaaaaaaaad": "asbbbbbbbbbbbbbbbbbbd",
        "3asdasaaaaaaaaaaaaaaaaaaaad": "asbbbbbbbbbbbbbbbbbbd",
        "4asdasaaaaaaaaaaaaaaaaaaaad": "asbbbbbbbbbbbbbbbbbbd",
        "5asdasaaaaaaaaaaaaaaaaaaaad": "asbbbbbbbbbbbbbbbbbbd",
    }
    store.save_data_to_file(path, data, pretty=False)
    assert file(path).read().count("\n") == 1
    assert store.load_data_from_file(path) == data
Exemple #12
0
    def save_guitest_step(self, step):
        path = cmk.paths.var_dir + "/guitests/RECORD"
        test_steps = store.load_data_from_file(path, [])

        if self.guitest_repair_step != None:
            if self.guitest_repair_step > len(test_steps):
                raise MKGeneralException("Test step for repairing is %s, but test only has %d steps." %
                                          (self.guitest_repair_step, len(test_steps)))
            mod_step = test_steps[self.guitest_repair_step]
            mod_step["output"] = step["output"]
            mod_step["user"] = step["user"]
            mod_step["elapsed_time"] = step["elapsed_time"]
        else:
            test_steps.append(step)
        file(path, "w").write("%s\n" % pprint.pformat(test_steps))
Exemple #13
0
def load_user_config():
    path = config.user.confdir + "/sidebar.mk"
    user_config = store.load_data_from_file(path)
    if user_config == None:
        user_config = {"snapins": config.sidebar, "fold": False}

    if type(user_config) == list:
        user_config = {"snapins": user_config, "fold": False}

    # Remove entries the user is not allowed for or which have state "off" (from legacy version)
    # silently skip configured but not existant snapins
    user_config["snapins"] = [
        entry
        for entry in user_config["snapins"]
        if entry[0] in sidebar_snapins and entry[1] != "off" and config.user.may("sidesnap." + entry[0])
    ]

    return user_config
Exemple #14
0
def get_gui_messages(user_id=None):
    if user_id is None:
        user_id = config.user.id
    path = config.config_dir + "/" + user_id.encode("utf-8") + '/messages.mk'
    messages = store.load_data_from_file(path, [])

    # Delete too old messages
    updated = False
    for index, message in enumerate(messages):
        now = time.time()
        valid_till = message.get('valid_till')
        if valid_till is not None and valid_till < now:
            messages.pop(index)
            updated = True

    if updated:
        save_gui_messages(messages)

    return messages
Exemple #15
0
def get_gui_messages(user_id = None):
    if user_id is None:
        user_id = config.user.id
    path = config.config_dir + "/" + user_id.encode("utf-8") + '/messages.mk'
    messages = store.load_data_from_file(path, [])

    # Delete too old messages
    updated = False
    for index, message in enumerate(messages):
        now = time.time()
        valid_till = message.get('valid_till')
        if valid_till is not None and valid_till < now:
            messages.pop(index)
            updated = True

    if updated:
        save_gui_messages(messages)

    return messages
Exemple #16
0
def load_user_config():
    path = config.user.confdir + "/sidebar.mk"
    user_config = store.load_data_from_file(path)
    if user_config == None:
        user_config = {
            "snapins": config.sidebar,
            "fold": False,
        }

    if type(user_config) == list:
        user_config = {
            "snapins": user_config,
            "fold": False,
        }

    # Remove entries the user is not allowed for or which have state "off" (from legacy version)
    # silently skip configured but not existant snapins
    user_config["snapins"] = [
        entry for entry in user_config["snapins"]
        if entry[0] in sidebar_snapins and entry[1] != "off"
        and config.user.may("sidesnap." + entry[0])
    ]

    return user_config
Exemple #17
0
def load_host(hostname):
    if '/' in hostname:
        return None # just for security reasons
    path = cmk.paths.var_dir + "/inventory/" + hostname
    return store.load_data_from_file(path, {})
Exemple #18
0
def load_host(hostname):
    if '/' in hostname:
        return None  # just for security reasons
    path = cmk.paths.var_dir + "/inventory/" + hostname
    return store.load_data_from_file(path, {})
Exemple #19
0
def page_graph():
    host    = html.var("host")
    service = html.var("service")
    dsname  = html.var("dsname")

    html.header(_("Prediction for %s - %s - %s") %
            (host, service, dsname),
            javascripts=["prediction"],
            stylesheets=["pages", "prediction"])

    # Get current value from perf_data via Livestatus
    current_value = get_current_perfdata(host, service, dsname)

    dir = "%s/prediction/%s/%s/%s" % (
            cmk.paths.var_dir, host, pnp_cleanup(service), pnp_cleanup(dsname))

    if not os.path.exists(dir):
        raise MKGeneralException(_("There is currently no prediction information "
                                   "available for this service."))

    # Load all prediction information, sort by time of generation
    tg_name = html.var("timegroup")
    timegroup = None
    timegroups = []
    now = time.time()
    for f in os.listdir(dir):
        file_path = dir + "/" + f
        if not f.endswith(".info"):
            continue

        tg_info = store.load_data_from_file(dir + "/" + f)
        if tg_info == None:
            continue

        tg_info["name"] = f[:-5]
        timegroups.append(tg_info)
        if tg_info["name"] == tg_name or \
            (tg_name == None and now >= tg_info["range"][0] and now <= tg_info["range"][1]):
            timegroup = tg_info
            tg_name = tg_info["name"]

    timegroups.sort(cmp = lambda a,b: cmp(a["range"][0], b["range"][0]))

    choices = [ (tg_info["name"], tg_info["name"].title())
                for tg_info in timegroups ]

    if not timegroup:
        if timegroups:
            timegroup  = timegroups[0]
            tg_name = choices[0][0]
        else:
            raise MKGeneralException(_("Missing prediction information."))

    html.begin_form("prediction")
    html.write(_("Show prediction for "))
    html.select("timegroup", choices, tg_name, onchange="document.prediction.submit();")
    html.hidden_fields()
    html.end_form()

    # Get prediction data
    path = dir + "/" + timegroup["name"]
    tg_data = store.load_data_from_file(path)
    if tg_data == None:
        raise MKGeneralException(_("Missing prediction data."))

    swapped = swap_and_compute_levels(tg_data, timegroup)
    vertical_range = compute_vertical_range(swapped)
    legend = [
       ( "#000000", _("Reference") ),
       ( "#ffffff", _("OK area") ),
       ( "#ffff00", _("Warning area") ),
       ( "#ff0000", _("Critical area") ),
    ]
    if current_value != None:
        legend.append( ("#0000ff", _("Current value: %.2f") % current_value) )

    create_graph(timegroup["name"], graph_size, timegroup["range"], vertical_range, legend)

    if "levels_upper" in timegroup:
        render_dual_area(swapped["upper_warn"], swapped["upper_crit"], "#fff000", 0.4)
        render_area_reverse(swapped["upper_crit"], "#ff0000", 0.1)

    if "levels_lower" in timegroup:
        render_dual_area(swapped["lower_crit"], swapped["lower_warn"], "#fff000", 0.4)
        render_area(swapped["lower_crit"], "#ff0000", 0.1)

    vscala_low = vertical_range[0]
    vscala_high = vertical_range[1]
    vert_scala = compute_vertical_scala(vscala_low, vscala_high)
    time_scala = [ [timegroup["range"][0] + i*3600, "%02d:00" % i] for i in range(0, 25, 2) ]
    render_coordinates(vert_scala, time_scala);

    if "levels_lower" in timegroup:
        render_dual_area(swapped["average"], swapped["lower_warn"], "#ffffff", 0.5)
        render_curve(swapped["lower_warn"], "#e0e000", square=True)
        render_curve(swapped["lower_crit"], "#f0b0a0", square=True)

    if "levels_upper" in timegroup:
        render_dual_area(swapped["upper_warn"], swapped["average"], "#ffffff", 0.5)
        render_curve(swapped["upper_warn"], "#e0e000", square=True)
        render_curve(swapped["upper_crit"], "#f0b0b0", square=True)
    render_curve(swapped["average"], "#000000")
    render_curve(swapped["average"], "#000000")

    # Try to get current RRD data and render it also
    from_time, until_time = timegroup["range"]
    now = time.time()
    if now >= from_time and now <= until_time:
        if time.daylight:
            tz_offset = time.altzone
        else:
            tz_offset = time.timezone
        rrd_step, rrd_data = get_rrd_data(host, service, dsname, "MAX", from_time, until_time)
        render_curve(rrd_data, "#0000ff", 2)
        if current_value != None:
            rel_time = (now - tz_offset) % timegroup["slice"]
            render_point(timegroup["range"][0] + rel_time, current_value, "#0000ff")

    html.footer()
Exemple #20
0
def load_acknowledgements():
    return store.load_data_from_file(acknowledgement_path, [])
Exemple #21
0
def load_acknowledgements():
    return store.load_data_from_file(acknowledgement_path, [])
Exemple #22
0
def test_load_data_from_file_not_existing(tmpdir):
    data = store.load_data_from_file("%s/x" % tmpdir)
    assert data == None

    data = store.load_data_from_file("%s/x" % tmpdir, "DEFAULT")
    assert data == "DEFAULT"
Exemple #23
0
def test_load_data_from_file_empty(tmpdir):
    locked_file = tmpdir.join("test")
    locked_file.write("")
    data = store.load_data_from_file("%s/x" % tmpdir, "DEF")
    assert data == "DEF"
Exemple #24
0
def page_graph():
    host = html.var("host")
    service = html.var("service")
    dsname = html.var("dsname")

    html.header(_("Prediction for %s - %s - %s") % (host, service, dsname),
                javascripts=["prediction"],
                stylesheets=["pages", "prediction"])

    # Get current value from perf_data via Livestatus
    current_value = get_current_perfdata(host, service, dsname)

    dir = "%s/prediction/%s/%s/%s" % (
        cmk.paths.var_dir, host, pnp_cleanup(service), pnp_cleanup(dsname))

    if not os.path.exists(dir):
        raise MKGeneralException(
            _("There is currently no prediction information "
              "available for this service."))

    # Load all prediction information, sort by time of generation
    tg_name = html.var("timegroup")
    timegroup = None
    timegroups = []
    now = time.time()
    for f in os.listdir(dir):
        file_path = dir + "/" + f
        if not f.endswith(".info"):
            continue

        tg_info = store.load_data_from_file(dir + "/" + f)
        if tg_info == None:
            continue

        tg_info["name"] = f[:-5]
        timegroups.append(tg_info)
        if tg_info["name"] == tg_name or \
            (tg_name == None and now >= tg_info["range"][0] and now <= tg_info["range"][1]):
            timegroup = tg_info
            tg_name = tg_info["name"]

    timegroups.sort(cmp=lambda a, b: cmp(a["range"][0], b["range"][0]))

    choices = [(tg_info["name"], tg_info["name"].title())
               for tg_info in timegroups]

    if not timegroup:
        if timegroups:
            timegroup = timegroups[0]
            tg_name = choices[0][0]
        else:
            raise MKGeneralException(_("Missing prediction information."))

    html.begin_form("prediction")
    html.write(_("Show prediction for "))
    html.select("timegroup",
                choices,
                tg_name,
                onchange="document.prediction.submit();")
    html.hidden_fields()
    html.end_form()

    # Get prediction data
    path = dir + "/" + timegroup["name"]
    tg_data = store.load_data_from_file(path)
    if tg_data == None:
        raise MKGeneralException(_("Missing prediction data."))

    swapped = swap_and_compute_levels(tg_data, timegroup)
    vertical_range = compute_vertical_range(swapped)
    legend = [
        ("#000000", _("Reference")),
        ("#ffffff", _("OK area")),
        ("#ffff00", _("Warning area")),
        ("#ff0000", _("Critical area")),
    ]
    if current_value != None:
        legend.append(("#0000ff", _("Current value: %.2f") % current_value))

    create_graph(timegroup["name"], graph_size, timegroup["range"],
                 vertical_range, legend)

    if "levels_upper" in timegroup:
        render_dual_area(swapped["upper_warn"], swapped["upper_crit"],
                         "#fff000", 0.4)
        render_area_reverse(swapped["upper_crit"], "#ff0000", 0.1)

    if "levels_lower" in timegroup:
        render_dual_area(swapped["lower_crit"], swapped["lower_warn"],
                         "#fff000", 0.4)
        render_area(swapped["lower_crit"], "#ff0000", 0.1)

    vscala_low = vertical_range[0]
    vscala_high = vertical_range[1]
    vert_scala = compute_vertical_scala(vscala_low, vscala_high)
    time_scala = [[timegroup["range"][0] + i * 3600,
                   "%02d:00" % i] for i in range(0, 25, 2)]
    render_coordinates(vert_scala, time_scala)

    if "levels_lower" in timegroup:
        render_dual_area(swapped["average"], swapped["lower_warn"], "#ffffff",
                         0.5)
        render_curve(swapped["lower_warn"], "#e0e000", square=True)
        render_curve(swapped["lower_crit"], "#f0b0a0", square=True)

    if "levels_upper" in timegroup:
        render_dual_area(swapped["upper_warn"], swapped["average"], "#ffffff",
                         0.5)
        render_curve(swapped["upper_warn"], "#e0e000", square=True)
        render_curve(swapped["upper_crit"], "#f0b0b0", square=True)
    render_curve(swapped["average"], "#000000")
    render_curve(swapped["average"], "#000000")

    # Try to get current RRD data and render it also
    from_time, until_time = timegroup["range"]
    now = time.time()
    if now >= from_time and now <= until_time:
        if time.daylight:
            tz_offset = time.altzone
        else:
            tz_offset = time.timezone
        rrd_step, rrd_data = get_rrd_data(host, service, dsname, "MAX",
                                          from_time, until_time)
        render_curve(rrd_data, "#0000ff", 2)
        if current_value != None:
            rel_time = (now - tz_offset) % timegroup["slice"]
            render_point(timegroup["range"][0] + rel_time, current_value,
                         "#0000ff")

    html.footer()
Exemple #25
0
def test_save_data_to_file(tmpdir):
    f = tmpdir.join("test")
    path = "%s" % f

    store.save_data_to_file(path, [2, 3])
    assert store.load_data_from_file(path) == [2, 3]
Exemple #26
0
def test_load_data_not_locked(tmpdir):
    locked_file = tmpdir.join("locked_file")
    locked_file.write("[1, 2]")

    data = store.load_data_from_file("%s" % locked_file)
    assert store.have_lock("%s" % locked_file) == False