def float_or_int(value): from cmk.gui.utils import saveint, savefloat if isinstance(value, int): return saveint(value) if isinstance(value, float): return savefloat(value) if isinstance(value, basestring): value_int = saveint(value) value_float = savefloat(value) if str(value_int) == str(value): return value_int if str(value_float) == str(value): return value_float
def filter_table(self, rows: Rows) -> Rows: from_value = utils.saveint(html.request.var(self.ident + "_from")) to_value = utils.saveint(html.request.var(self.ident + "_to")) if not from_value and not to_value: return rows newrows = [] for row in rows: value = row.get(self.ident, None) if value is not None: if from_value and value < from_value: continue if to_value and value > to_value: continue newrows.append(row) return newrows
def _from_vars(self): self._start = bool(html.request.var("_start")) # 'all' not set -> only scan checked hosts in current folder, no recursion # otherwise: all host in this folder, maybe recursively self._all = bool(html.request.var("all")) self._complete_folder = self._all # Ignored during initial form display # TODO: Make dedicated class or class members self._settings = { "where": html.request.var("where"), "alias": html.get_unicode_input("alias", "").strip() or None, "recurse": html.get_checkbox("recurse"), "select": html.request.var("select"), "timeout": utils.saveint(html.request.var("timeout")) or 8, "probes": utils.saveint(html.request.var("probes")) or 2, "max_ttl": utils.saveint(html.request.var("max_ttl")) or 10, "force_explicit": html.get_checkbox("force_explicit"), "ping_probes": utils.saveint(html.request.var("ping_probes")) or 0, } self._job = ParentScanBackgroundJob()
def action(self, cmdtag, spec, row, row_index, num_rows): if html.request.var("_resched_checks"): spread = utils.saveint(html.request.var("_resched_spread")) text = "<b>" + _("reschedule an immediate check") if spread: text += _(" spread over %d minutes ") % spread text += "</b>" + _("of") t = time.time() if spread: t += spread * 60.0 * row_index / num_rows command = "SCHEDULE_FORCED_" + cmdtag + "_CHECK;%s;%d" % (spec, int(t)) return command, text
def filter_table(self, context: VisualContext, rows: Rows) -> Rows: values = context.get(self.ident, {}) assert not isinstance(values, str) from_value, to_value = (utils.saveint(values.get(v, 0)) for v in self.htmlvars) if not from_value and not to_value: return rows newrows = [] for row in rows: value = row.get(self.ident, None) if value is not None: if from_value and value < from_value: continue if to_value and value > to_value: continue newrows.append(row) return newrows
def _render_group(self, rows_with_ids, header, view, group_cells, cells, num_columns, show_checkboxes): repeat_heading_every = 20 # in case column_headers is "repeat" if group_cells: self._show_group_header_table(group_cells, rows_with_ids[0][1]) html.open_table(class_="data") odd = "odd" column_headers = view.get("column_headers") if column_headers != "off": self._show_header_line(cells, show_checkboxes) groups, rows_with_ids = calculate_view_grouping_of_services( rows_with_ids, row_group_cells=None) visible_row_number = 0 group_hidden, num_grouped_rows = None, 0 for index, row in rows_with_ids: if view.get("column_headers") == "repeat": if visible_row_number > 0 and visible_row_number % repeat_heading_every == 0: self._show_header_line(cells, show_checkboxes) visible_row_number += 1 odd = "even" if odd == "odd" else "odd" # state = row.get("service_state", row.get("aggr_state")) state = utils.saveint(row.get("service_state")) if state is None: state = utils.saveint(row.get("host_state", 0)) if state > 0: state += 1 # 1 is critical for hosts num_cells = len(cells) if index in groups: group_spec, num_grouped_rows = groups[index] group_hidden = grouped_row_title(index, group_spec, num_grouped_rows, odd, num_cells) odd = "even" if odd == "odd" else "odd" css_classes = [] if is_stale(row): css_classes.append("stale") hide = "" if num_grouped_rows > 0: num_grouped_rows -= 1 if group_hidden: hide = "display:none" if group_hidden is not None and num_grouped_rows == 0: # last row in group css_classes.append("group_end") group_hidden = None css_classes.append("%s%d" % (odd, state)) html.open_tr(class_=["data"] + css_classes, style=hide) if show_checkboxes: render_checkbox_td(view, row, num_cells) for cell in cells: cell.paint(row) html.close_tr() html.close_table() # Don't make rows selectable when no commands can be fired # Ignore "C" display option here. Otherwise the rows will not be selectable # after view reload. if not user.may("general.act"): return init_rowselect(_get_view_name(view))
def load_users(lock: bool = False) -> Users: filename = _root_dir() + "contacts.mk" if lock: # Note: the lock will be released on next save_users() call or at # end of page request automatically. store.aquire_lock(filename) if 'users' in g: return g.users # First load monitoring contacts from Check_MK's world. If this is # the first time, then the file will be empty, which is no problem. # Execfile will the simply leave contacts = {} unchanged. contacts = store.load_from_mk_file(filename, "contacts", {}) # Now load information about users from the GUI config world filename = _multisite_dir() + "users.mk" users = store.load_from_mk_file(_multisite_dir() + "users.mk", "multisite_users", {}) # Merge them together. Monitoring users not known to Multisite # will be added later as normal users. result = {} for uid, user in users.items(): # Transform user IDs which were stored with a wrong type uid = ensure_str(uid) profile = contacts.get(uid, {}) profile.update(user) result[uid] = profile # Convert non unicode mail addresses if "email" in profile: profile["email"] = ensure_str(profile["email"]) # This loop is only neccessary if someone has edited # contacts.mk manually. But we want to support that as # far as possible. for uid, contact in contacts.items(): # Transform user IDs which were stored with a wrong type uid = ensure_str(uid) if uid not in result: result[uid] = contact result[uid]["roles"] = ["user"] result[uid]["locked"] = True result[uid]["password"] = "" # Passwords are read directly from the apache htpasswd-file. # That way heroes of the command line will still be able to # change passwords with htpasswd. Users *only* appearing # in htpasswd will also be loaded and assigned to the role # they are getting according to the multisite old-style # configuration variables. def readlines(f): try: return Path(f).open(encoding="utf-8") except IOError: return [] # FIXME TODO: Consolidate with htpasswd user connector filename = cmk.utils.paths.htpasswd_file for line in readlines(filename): line = line.strip() if ':' in line: uid, password = line.strip().split(":")[:2] uid = ensure_str(uid) if password.startswith("!"): locked = True password = password[1:] else: locked = False if uid in result: result[uid]["password"] = password result[uid]["locked"] = locked else: # Create entry if this is an admin user new_user = { "roles": config.roles_of_user(uid), "password": password, "locked": False, } result[uid] = new_user # Make sure that the user has an alias result[uid].setdefault("alias", uid) # Other unknown entries will silently be dropped. Sorry... # Now read the serials, only process for existing users serials_file = '%s/auth.serials' % os.path.dirname( cmk.utils.paths.htpasswd_file) for line in readlines(serials_file): line = line.strip() if ':' in line: user_id, serial = line.split(':')[:2] user_id = ensure_str(user_id) if user_id in result: result[user_id]['serial'] = utils.saveint(serial) # Now read the user specific files directory = cmk.utils.paths.var_dir + "/web/" for d in os.listdir(directory): if d[0] != '.': uid = ensure_str(d) # read special values from own files if uid in result: for attr, conv_func in [ ('num_failed_logins', utils.saveint), ('last_pw_change', utils.saveint), ('last_seen', utils.savefloat), ('enforce_pw_change', lambda x: bool(utils.saveint(x))), ('idle_timeout', _convert_idle_timeout), ('session_id', _convert_session_info), ]: val = load_custom_attr(uid, attr, conv_func) if val is not None: result[uid][attr] = val # read automation secrets and add them to existing # users or create new users automatically try: user_secret_path = Path(directory) / d / "automation.secret" with user_secret_path.open(encoding="utf-8") as f: secret: Optional[str] = ensure_str(f.read().strip()) except IOError: secret = None if secret: if uid in result: result[uid]["automation_secret"] = secret else: result[uid] = { "roles": ["guest"], "automation_secret": secret, } # populate the users cache g.users = result return result
def _render_group(self, rows_with_ids, header, view, group_cells, cells, num_columns, show_checkboxes): repeat_heading_every = 20 # in case column_headers is "repeat" html.open_table(class_="groupheader", cellspacing=0, cellpadding=0, border=0) html.open_tr(class_="groupheader") painted = False for cell in group_cells: if painted: html.td(", ") painted = cell.paint(rows_with_ids[0][1]) html.close_tr() html.close_table() html.open_table(class_="data") odd = "odd" column_headers = view.get("column_headers") if column_headers != "off": self._show_header_line(cells, show_checkboxes) groups, rows_with_ids = calculate_view_grouping_of_services( rows_with_ids, row_group_cells=None) visible_row_number = 0 group_hidden, num_grouped_rows = None, 0 for index, row in rows_with_ids: if view.get("column_headers") == "repeat": if visible_row_number > 0 and visible_row_number % repeat_heading_every == 0: self._show_header_line(cells, show_checkboxes) visible_row_number += 1 odd = "even" if odd == "odd" else "odd" # state = row.get("service_state", row.get("aggr_state")) state = utils.saveint(row.get("service_state")) if state is None: state = utils.saveint(row.get("host_state", 0)) if state > 0: state += 1 # 1 is critical for hosts num_cells = len(cells) if index in groups: group_spec, num_grouped_rows = groups[index] group_hidden = grouped_row_title(index, group_spec, num_grouped_rows, odd, num_cells) odd = "even" if odd == "odd" else "odd" css_classes = [] if is_stale(row): css_classes.append("stale") hide = "" if num_grouped_rows > 0: num_grouped_rows -= 1 if group_hidden: hide = "display:none" if group_hidden is not None and num_grouped_rows == 0: # last row in group css_classes.append("group_end") group_hidden = None css_classes.append("%s%d" % (odd, state)) html.open_tr(class_=["data"] + css_classes, style=hide) if show_checkboxes: render_checkbox_td(view, row, num_cells) for cell in cells: cell.paint(row) html.close_tr() html.close_table() init_rowselect(view)