def list_logs(host, logfiles): rowno = 0 for log_file in logfiles: rowno += 1 if rowno == 1: html.write("<tr class=groupheader>\n") html.write("<th>"+_('Level')+"</th><th>"+_('Logfile')+"</th>") html.write("<th>"+_('Last Entry')+"</th><th>"+_('Entries')+"</th></tr>\n") file_display = form_file_to_ext(log_file) logs = parse_file(host, log_file) if logs == [] or type(logs) != list: # corrupted logfile if logs == []: logs = "empty" html.write("<tr class=%s0>\n" % (rowno % 2 == 0 and "odd" or "even")) html.write("<td>-</td><td>%s</td><td>%s</td><td>0</td></tr>\n" % (htmllib.attrencode(logs), htmllib.attrencode(file_display))) else: worst_log = get_worst_log(logs) last_log = get_last_log(logs) state = worst_log['level'] state_name = form_level(state) html.write("<tr class=%s%d>\n" % (rowno % 2 == 0 and "odd" or "even", state)) html.write("<td class=\"state%d\">%s</td>\n" % (state, state_name)) html.write("<td><a href=\"%s\">%s</a></td>\n" % (html.makeuri([('host', host), ('file', file_display)]), htmllib.attrencode(file_display))) html.write("<td>%s</td><td>%s</td></tr>\n" % \ (form_datetime(last_log['datetime']), len(logs))) if rowno == 0: html.write('<tr><td colspan=4>'+_('No logs found for this host.')+'</td></tr>\n')
def link(text, target, frame="main"): # Convert relative links into absolute links. We have three kinds # of possible links and we change only [3] # [1] protocol://hostname/url/link.py # [2] /absolute/link.py # [3] relative.py if not (":" in target[:10]) and target[0] != '/': target = defaults.url_prefix + "check_mk/" + target return '<a onfocus="if (this.blur) this.blur();" target="%s" ' \ 'class="link main_click" href="%s">%s</a>' % (htmllib.attrencode(frame), htmllib.attrencode(target), htmllib.attrencode(text))
def do_log_ack(host, filename): todo = [] if not host and not filename: # all logs on all hosts for this_host, logs in all_logs(): for int_filename in logs: file_display = form_file_to_ext(int_filename) todo.append((this_host, int_filename, file_display)) ack_msg = _('all logfiles on all hosts') elif host and not filename: # all logs on one host for int_filename in host_logs(host): file_display = form_file_to_ext(int_filename) todo.append((host, int_filename, file_display)) ack_msg = _('all logfiles of host <tt>%s</tt>') % htmllib.attrencode(host) elif host and filename: # one log on one host int_filename = form_file_to_int(filename) todo = [ (host, int_filename, form_file_to_ext(int_filename)) ] ack_msg = _('the log file <tt>%s</tt> on host <tt>%s</tt>') % \ (htmllib.attrencode(filename), htmllib.attrencode(host)) html.header(_("Acknowledge %s") % ack_msg, stylesheets = stylesheets) html.begin_context_buttons() html.context_button(_("All Logfiles"), html.makeuri([('host', ''), ('file', '')])) if host: html.context_button(_("All Logfiles of Host"), html.makeuri([('file', '')])) if host and filename: html.context_button(_("Back to Logfile"), html.makeuri([])) html.end_context_buttons() ack = html.var('_ack') if not html.confirm(_("Do you really want to acknowledge %s by <b>deleting</b> all stored messages?") % ack_msg): html.footer() return if not config.may("general.act"): html.write("<h1 class=error>"+_('Permission denied')+"</h1>\n") html.write("<div class=error>" + _('You are not allowed to acknowledge %s</div>') % ack_msg) html.footer() return # filter invalid values if ack != '1': raise MKUserError('_ack', _('Invalid value for ack parameter.')) for this_host, int_filename, display_name in todo: try: if not may_see(this_host): raise MKAuthException(_('Permission denied.')) os.remove(defaults.logwatch_dir + '/' + this_host + '/' + int_filename) except Exception, e: html.show_error(_('The log file <tt>%s</tt> of host <tt>%s</tt> could not be deleted: %s.') % \ (htmllib.attrencode(file_display), htmllib.attrencode(this_host), e))
def page_login(): title = _("Check_MK Mobile") mobile_html_head(title) jqm_page_header(title, id="login") html.write('<div id="loginhead">%s</div>' % _("Welcome to Check_MK Multisite Mobile. Please Login.")) html.begin_form("login", method='POST', add_transid=False) # Keep information about original target URL origtarget = html.var('_origtarget', '') if not origtarget and not html.req.myfile == 'login': origtarget = html.req.uri html.hidden_field('_origtarget', htmllib.attrencode(origtarget)) html.text_input("_username", label=_("Username:"******"_password", size=None, label=_("Password:"******"<br>") html.button("_login", _('Login')) html.set_focus("_username") html.end_form() html.write('<div id="loginfoot">') html.write('<img class="logomk" src="images/logo_mk.png">') html.write('<div class="copyright">%s</div>' % _("Copyright Mathias Kettner 2012")) html.write('</div>') jqm_page_footer() mobile_html_foot() return 0 # apache.OK
def normal_login_page(called_directly = True): html.set_render_headfoot(False) html.header(_("Check_MK Multisite Login"), javascripts=[], stylesheets=["pages", "login"]) origtarget = html.var('_origtarget', '') if not origtarget and not html.req.myfile in [ 'login', 'logout' ]: origtarget = html.makeuri([]) # When e.g. the password of a user is changed and the first frame that recognizes the # non matching cookies is the sidebar it redirects the user to side.py while removing # the frameset. This is not good. Instead of this redirect the user to the index page. if html.req.myfile == 'side': html.immediate_browser_redirect(0.1, 'login.py') return apache.OK # Never allow the login page to be opened in a frameset. Redirect top page to login page. # This will result in a full screen login page. html.javascript('''if(top != self) { window.top.location.href = location; }''') # When someone calls the login page directly and is already authed redirect to main page if html.req.myfile == 'login' and check_auth() != '': html.immediate_browser_redirect(0.5, origtarget and origtarget or 'index.py') return apache.OK html.write("<div id=login>") html.write("<img id=login_window src=\"images/login_window.png\">") html.write("<div id=version>%s</div>" % defaults.check_mk_version) html.begin_form("login", method = 'POST', add_transid = False) html.hidden_field('_login', '1') html.hidden_field('_origtarget', htmllib.attrencode(origtarget)) html.write("<label id=label_user class=legend for=_username>%s:</label><br />" % _('Username')) html.text_input("_username", id="input_user") html.write("<label id=label_pass class=legend for=_password>%s:</label><br />" % _('Password')) html.password_input("_password", id="input_pass", size=None) if html.has_user_errors(): html.write('<div id=login_error>') html.show_user_errors() html.write('</div>') html.write("<div id=button_text>") html.image_button("_login", _('Login')) html.write("</div>") html.write("<div id=foot>Version: %s - © " "<a href=\"http://mathias-kettner.de\">Mathias Kettner</a><br><br>" % defaults.check_mk_version) html.write(_("You can use, modify and distribute Check_MK under the terms of the <a href='%s'>" "GNU GPL Version 2</a>." % "http://mathias-kettner.de/gpl.html")) html.write("</div>") html.write("</div>") html.set_focus('_username') html.hidden_fields() html.end_form() html.footer() return apache.OK
def page_login(): title = _("Check_MK Mobile") mobile_html_head(title) jqm_page_header(title, id="login") html.write('<div id="loginhead">%s</div>' % _("Welcome to Check_MK Multisite Mobile. Please Login.")) html.begin_form("login", method = 'POST', add_transid = False) # Keep information about original target URL origtarget = html.var('_origtarget', '') if not origtarget and not html.req.myfile == 'login': origtarget = html.req.uri html.hidden_field('_origtarget', htmllib.attrencode(origtarget)) html.text_input("_username", label = _("Username:"******"_password", size=None, label = _("Password:"******"<br>") html.button("_login", _('Login')) html.set_focus("_username") html.end_form() html.write('<div id="loginfoot">') html.write('<img class="logomk" src="images/logo_mk.png">') html.write('<div class="copyright">%s</div>' % _("Copyright Mathias Kettner 2012")) html.write('</div>') jqm_page_footer() mobile_html_foot() return 0 # apache.OK
def link(text, target, frame="main"): # Convert relative links into absolute links. We have three kinds # of possible links and we change only [3] # [1] protocol://hostname/url/link.py # [2] /absolute/link.py # [3] relative.py if not (":" in target[:10]) and target[0] != '/': target = defaults.url_prefix + "check_mk/" + target return '<a onfocus="if (this.blur) this.blur();" target="%s" ' \ 'class=link href="%s">%s</a>' % (frame, target, htmllib.attrencode(text))
def format_plugin_output(output, row=None): from htmllib import attrencode output = attrencode(output).replace("(!)", warn_marker) \ .replace("(!!)", crit_marker) \ .replace("(?)", unknown_marker) if row and "[running on" in output: a = output.index("[running on") e = output.index("]", a) hosts = output[a + 12:e].replace(" ", "").split(",") css, h = paint_host_list(row["site"], hosts) output = output[:a] + "running on " + h + output[e + 1:] return output
def end(): global table finish_previous() html.unplug() if not table["rows"] and table["omit_if_empty"]: table = None return if table["title"]: html.write("<h3>%s</h3>" % table["title"]) if table.get("help"): html.help(table["help"]) if not table["rows"]: html.write("<div class=info>%s</div>" % table["empty_text"]) table = None return html.write('<table class="data') if "css" in table: html.write(" %s" % table["css"]) html.write('">\n') html.write(" <tr>") for header, help in table["headers"]: if help: header = '<span title="%s">%s</span>' % (htmllib.attrencode(help), header) html.write(" <th>%s</th>\n" % header) html.write(" </tr>\n") odd = "even" # TODO: Sorting for row, css in table["rows"]: # TODO: Filtering odd = odd == "odd" and "even" or "odd" html.write(' <tr class="data %s0' % odd) if css: html.write(' %s' % css) html.write('">\n') for cell_content, css_classes in row: html.write(" <td%s>" % (css_classes and (" class='%s'" % css_classes) or "")) html.write(cell_content) html.write("</td>\n") html.write("</tr>\n") html.write("</table>\n") table = None
def do_log_ack(host, filename): file = form_file_to_int(filename) file_display = form_file_to_ext(file) html.header(_("Acknowledge logfile %s - %s") % (htmllib.attrencode(host), file_display), stylesheets = stylesheets) html.begin_context_buttons() html.context_button(_("All logfiles of Host"), "logwatch.py?host=%s" % htmllib.urlencode(host)) html.end_context_buttons() ack = html.var('ack') if not html.confirm(_("Do you really want to acknowledge the log file <tt>%s</tt> by <b>deleting</b> all stored messages?") % filename): html.footer() return if not (config.may("general.act") and may_see(host)): html.write("<h1 class=error>"+_('Permission denied')+"</h1>\n") html.write("<div class=error>" + _('You are not allowed to acknowledge the logs of the host %s</div>') % htmllib.attrencode(host)) html.footer() return # filter invalid values if ack != '1': raise MKUserError('ack', _('Invalid value for ack parameter.')) try: os.remove(defaults.logwatch_dir + '/' + host + '/' + file) message = '<b>'+_('%s: %s Acknowledged') % (htmllib.attrencode(host), htmllib.attrencode(file_display)) +'</b><br>' message += '<p>' message += _('The log messages from host "%s" in file "%s" have been acknowledged.') % \ (htmllib.attrencode(host), htmllib.attrencode(file_display)) message += '</p>' html.message(message) except Exception, e: html.show_error(_('The log file "%s" from host "%s" could not be deleted: %s.') % \ (htmllib.attrencode(file_display), htmllib.attrencode(host), e))
def nagioscgilink(text, target): html.write("<li class=sidebar><a target=\"main\" class=link href=\"%snagios/cgi-bin/%s\">%s</a></li>" % \ (defaults.url_prefix, target, htmllib.attrencode(text)))
def heading(text): html.write("<h3>%s</h3>\n" % htmllib.attrencode(text))
def normal_login_page(called_directly=True): # Working around the problem that the auth.php file needed for multisite based # authorization of external addons might not exist when setting up a new installation # We assume: Each user must visit this login page before using the multisite based # authorization. So we can easily create the file here if it is missing. # This is a good place to replace old api based files in the future. auth_php = defaults.var_dir + "/wato/auth/auth.php" if not os.path.exists(auth_php) or os.path.getsize(auth_php) == 0: import wato wato.create_auth_file(wato.load_users()) html.set_render_headfoot(False) html.header(_("Check_MK Multisite Login"), javascripts=[], stylesheets=["pages", "login"]) origtarget = html.var("_origtarget", "") if not origtarget and not html.req.myfile == "login": origtarget = html.makeuri([]) # When e.g. the password of a user is changed and the first frame that recognizes the # non matching cookies is the sidebar it redirects the user to side.py while removing # the frameset. This is not good. Instead of this redirect the user to the index page. if html.req.myfile == "side": html.immediate_browser_redirect(0.1, "index.py") return apache.OK # Never allow the login page to be opened in a frameset. Redirect top page to login page. # This will result in a full screen login page. html.javascript( """if(top != self) { window.top.location.href = location; }""" ) # When someone calls the login page directly and is already authed redirect to main page if html.req.myfile == "login" and check_auth() != "": html.immediate_browser_redirect(0.5, origtarget and origtarget or "index.py") return apache.OK html.write("<div id=login>") html.write('<img id=login_window src="images/login_window.png">') html.write("<div id=version>%s</div>" % defaults.check_mk_version) html.begin_form("login", method="POST", add_transid=False) html.hidden_field("_login", "1") html.hidden_field("_origtarget", htmllib.attrencode(origtarget)) html.write("<label id=label_user class=legend for=_username>%s:</label><br />" % _("Username")) html.text_input("_username", id="input_user") html.write("<label id=label_pass class=legend for=_password>%s:</label><br />" % _("Password")) html.password_input("_password", id="input_pass", size=None) if html.has_user_errors(): html.write("<div id=login_error>") html.show_user_errors() html.write("</div>") html.write("<div id=button_text>") html.image_button("_login", _("Login")) html.write("</div>") html.write( "<div id=foot>Version: %s - © " '<a href="http://mathias-kettner.de">Mathias Kettner</a><br><br>' % defaults.check_mk_version ) html.write( _( "You can use, modify and distribute Check_MK under the terms of the <a href='%s'>" "GNU GPL Version 2</a>." % "http://mathias-kettner.de/gpl.html" ) ) html.write("</div>") html.write("</div>") html.set_focus("_username") html.hidden_fields() html.end_form() html.footer() return apache.OK
def page_show(): host = html.var('host') filename = html.var('file') # Acknowledging logs is supported on # a) all logs on all hosts # b) all logs on one host # c) one log on one host if html.has_var('_ack') and not html.var("_do_actions") == _("No"): html.live.set_auth_domain('action') do_log_ack(host, filename) return if not host: show_log_list() return # Check user permissions on the host if not may_see(host): raise MKAuthException(_("You are not allowed to access the logs of the host %s") % htmllib.attrencode(host)) if filename: show_file(host, filename) else: show_host_log_list(host)
def ajax_del_bookmark(): try: num = int(html.var("num")) except ValueError: raise MKGeneralException(_("Invalid bookmark id.")) bookmarks = load_bookmarks() try: del bookmarks[num] except IndexError: raise MKGeneralException(_("Unknown bookmark id: %d. This is probably a problem with reload or browser history. Please try again.") % htmllib.attrencode(num)) save_bookmarks(bookmarks) render_bookmarks()
def show_file(host, filename): master_url = html.var('master_url', '') int_filename = form_file_to_int(filename) html.header(_("Logfiles of Host %s: %s") % (host, filename), stylesheets = stylesheets) html.begin_context_buttons() html.context_button(_("Services"), "%sview.py?view_name=host&site=&host=%s" % (master_url, htmllib.urlencode(host)), 'services') html.context_button(_("All Logfiles of Host"), html.makeuri([('file', '')])) html.context_button(_("All Logfiles"), html.makeuri([('host', ''), ('file', '')])) html.context_button(_("Analyze Patterns"), "%swato.py?mode=pattern_editor&host=%s&file=%s" % (master_url, htmllib.urlencode(host), htmllib.urlencode(filename)), 'analyze') if html.var('_hidecontext', 'no') == 'yes': hide_context_label = _('Show Context') hide_context_param = 'no' hide = True else: hide_context_label = _('Hide Context') hide_context_param = 'yes' hide = False logs = parse_file(host, int_filename, hide) if type(logs) != list: html.end_context_buttons() html.show_error(_("Unable to show logfile: <b>%s</b>") % logs) html.footer() return elif logs == []: html.end_context_buttons() html.message(_("This logfile contains no unacknowledged messages.")) html.footer() return ack_button(host, int_filename) html.context_button(hide_context_label, html.makeuri([('_hidecontext', hide_context_param)])) html.end_context_buttons() html.write("<div id=logwatch>\n") for log in logs: html.write('<div class="chunk">\n'); html.write('<table class="section">\n<tr>\n'); html.write('<td class="%s">%s</td>\n' % (form_level(log['level']), form_level(log['level']))); html.write('<td class="date">%s</td>\n' % (form_datetime(log['datetime']))); html.write('</tr>\n</table>\n'); for line in log['lines']: html.write('<p class="%s">' % line['class']) edit_url = master_url + "wato.py?" + htmllib.urlencode_vars([ ('mode', 'pattern_editor'), ('host', host), ('file', filename), ('match', line['line']), ]) html.icon_button(edit_url, _("Analyze this line"), "analyze") html.write('%s</p>\n' % (htmllib.attrencode(line['line']) )) html.write('</div>\n') html.write("</div>\n") html.footer()
def page_edit_bookmark(): html.header(_("Edit Bookmark")) try: n = int(html.var("num")) except ValueError: raise MKGeneralException(_("Invalid bookmark id.")) bookmarks = load_bookmarks() if n >= len(bookmarks): raise MKGeneralException(_("Unknown bookmark id: %d. This is probably a problem with reload or browser history. Please try again.") % htmllib.attrencode(n)) if html.var("save") and html.check_transaction(): title = html.var("title") url = html.var("url") bookmarks[n] = (title, url) save_bookmarks(bookmarks) html.reload_sidebar() html.begin_form("edit_bookmark") if html.var("save"): title = html.var("title") url = html.var("url") bookmarks[n] = (title, url) save_bookmarks(bookmarks) html.reload_sidebar() else: title, url = bookmarks[n] html.set_var("title", title) html.set_var("url", url) html.write("<table class=edit_bookmarks>") html.write("<tr><td>%s</td><td>" % _('Title:')) html.text_input("title", size = 50) html.write("</td></tr><tr><td>%s:</td><td>" % _('URL')) html.text_input("url", size = 50) html.write("</td></tr><tr><td></td><td>") html.button("save", _("Save")) html.write("</td></tr></table>\n") html.hidden_field("num", str(n)) html.end_form() html.footer()
def normal_login_page(called_directly=True): html.set_render_headfoot(False) html.header(_("Check_MK Multisite Login"), javascripts=[], stylesheets=["pages", "login"]) origtarget = html.var('_origtarget', '') if not origtarget and not html.req.myfile in ['login', 'logout']: origtarget = html.makeuri([]) # When e.g. the password of a user is changed and the first frame that recognizes the # non matching cookies is the sidebar it redirects the user to side.py while removing # the frameset. This is not good. Instead of this redirect the user to the index page. if html.req.myfile == 'side': html.immediate_browser_redirect(0.1, 'login.py') return apache.OK # Never allow the login page to be opened in a frameset. Redirect top page to login page. # This will result in a full screen login page. html.javascript('''if(top != self) { window.top.location.href = location; }''') # When someone calls the login page directly and is already authed redirect to main page if html.req.myfile == 'login' and check_auth() != '': html.immediate_browser_redirect( 0.5, origtarget and origtarget or 'index.py') return apache.OK html.write("<div id=login>") html.write("<img id=login_window src=\"images/login_window.png\">") html.write("<div id=version>%s</div>" % defaults.check_mk_version) html.begin_form("login", method='POST', add_transid=False) html.hidden_field('_login', '1') html.hidden_field('_origtarget', htmllib.attrencode(origtarget)) html.write( "<label id=label_user class=legend for=_username>%s:</label><br />" % _('Username')) html.text_input("_username", id="input_user") html.write( "<label id=label_pass class=legend for=_password>%s:</label><br />" % _('Password')) html.password_input("_password", id="input_pass", size=None) if html.has_user_errors(): html.write('<div id=login_error>') html.show_user_errors() html.write('</div>') html.write("<div id=button_text>") html.image_button("_login", _('Login')) html.write("</div>") html.write( "<div id=foot>Version: %s - © " "<a href=\"http://mathias-kettner.de\">Mathias Kettner</a><br><br>" % defaults.check_mk_version) html.write( _("You can use, modify and distribute Check_MK under the terms of the <a href='%s'>" "GNU GPL Version 2</a>." % "http://mathias-kettner.de/gpl.html")) html.write("</div>") html.write("</div>") html.set_focus('_username') html.hidden_fields() html.end_form() html.footer() return apache.OK