Module: check_mk
Branch: master
Commit: 69f16d257d7b72ed3327c35d6ea7f6dfdb13fbd2
URL:
http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=69f16d257d7b72…
Author: Mathias Kettner <mk(a)mathias-kettner.de>
Date: Fri Feb 4 10:29:40 2011 +0100
First version of Pyhon web service (for NagStaMon)
---
web/htdocs/htmllib.py | 30 +++++++++++++++++++++++-------
web/htdocs/index.py | 13 +++++++++----
web/htdocs/views.py | 22 ++++++++++++++++------
web/plugins/views/webservice.py | 31 +++++++++++++++++++++++++++++++
4 files changed, 79 insertions(+), 17 deletions(-)
diff --git a/web/htdocs/htmllib.py b/web/htdocs/htmllib.py
index d19f7bb..ed32f1e 100644
--- a/web/htdocs/htmllib.py
+++ b/web/htdocs/htmllib.py
@@ -133,6 +133,10 @@ class html:
self.browser_redirect = ''
self.events = set([]) # currently used only for sounds
self.header_sent = False
+ self.output_format = "html"
+
+ def set_output_format(self, f):
+ self.output_format = f
def write(self, text):
if type(text) == unicode:
@@ -381,11 +385,12 @@ class html:
self.browser_redirect = url
def header(self, title=''):
- if not self.header_sent:
- self.html_head(title)
- self.write("<body class=main>")
- self.header_sent = True
- self.top_heading(title)
+ if self.output_format == "html":
+ if not self.header_sent:
+ self.html_head(title)
+ self.write("<body class=main>")
+ self.header_sent = True
+ self.top_heading(title)
def top_heading(self, title):
if type(self.req.user) == str:
@@ -429,15 +434,26 @@ class html:
self.write("</body></html>\n")
def footer(self):
+ if self.output_format == "html":
self.bottom_footer()
self.body_end()
def show_error(self, msg):
- self.write("<div class=error>%s</div>\n" % msg)
+ if self.output_format == "html":
+ self.write("<div class=error>%s</div>\n" % msg)
+ else:
+ self.write("ERROR: ")
+ self.write(strip_tags(msg))
+ self.write("\n")
def show_warning(self, msg):
- self.write("<div class=warning>%s</div>\n" % msg)
+ if self.output_format == "html":
+ self.write("<div class=warning>%s</div>\n" % msg)
+ else:
+ self.write("WARNING: ")
+ self.write(strip_tags(msg))
+ self.write("\n")
def message(self, msg):
self.write("<div class=success>%s</div>\n" % msg)
diff --git a/web/htdocs/index.py b/web/htdocs/index.py
index 27349bc..b29431f 100644
--- a/web/htdocs/index.py
+++ b/web/htdocs/index.py
@@ -153,6 +153,11 @@ def handler(req):
try:
read_get_vars(req)
+
+ # Prepare output format
+ output_format = html.var("output_format", "html")
+ html.set_output_format(output_format)
+
config.load_config() # load multisite.mk
if html.var("debug"): # Debug flag may be set via URL
config.debug = True
@@ -179,23 +184,23 @@ def handler(req):
except MKUserError, e:
html.header("Invalid User Input")
- html.show_error(e)
+ html.show_error(str(e))
html.footer()
except MKAuthException, e:
html.header("Permission denied")
- html.show_error(e)
+ html.show_error(str(e))
html.footer()
except MKConfigError, e:
html.header("Configuration Error")
- html.show_error(e)
+ html.show_error(str(e))
html.footer()
apache.log_error("Configuration error: %s" % (e,), apache.APLOG_ERR)
except MKGeneralException, e:
html.header("Error")
- html.show_error("Error: %s" % (e,))
+ html.show_error(str(e))
html.footer()
apache.log_error("Error: %s" % (e,), apache.APLOG_ERR)
diff --git a/web/htdocs/views.py b/web/htdocs/views.py
index 487312e..7555b7d 100644
--- a/web/htdocs/views.py
+++ b/web/htdocs/views.py
@@ -636,7 +636,7 @@ function toggle_section(nr, oImg) {
section_header(7, "Layout")
html.write("<table border=0>")
html.write("<tr><td>Basic Layout:</td><td>")
- html.sorted_select("layout", [ (k, v["title"]) for k,v in
multisite_layouts.items() ])
+ html.sorted_select("layout", [ (k, v["title"]) for k,v in
multisite_layouts.items() if not v.get("hide")])
html.write("</td></tr>\n")
html.write("<tr><td>Number of columns:</td><td>")
html.number_input("num_columns", 1)
@@ -935,13 +935,18 @@ def page_view(h):
# Display view with real data. This is *the* function everying
# is about.
def show_view(view, show_heading = False, show_buttons = True, show_footer = True):
- # Parse display options
- display_options = html.var("display_options", "")
+ all_display_options = "HTBFCEOZRSIX"
+
+ # Parse display options and
+ if html.output_format == "html":
+ display_options = html.var("display_options", "")
+ else:
+ display_options = all_display_options.lower()
# If all display_options are upper case assume all not given values default
# to lower-case. Vice versa when all display_options are lower case.
# When the display_options are mixed case assume all unset options to be enabled
- do_defaults = display_options.isupper() and "htbfceozrsix" or
"HTBFCEOZRSIX"
+ do_defaults = display_options.isupper() and all_display_options.lower() or
all_display_options
for c in do_defaults:
if c.lower() not in display_options.lower():
display_options += c
@@ -952,7 +957,12 @@ def show_view(view, show_heading = False, show_buttons = True,
show_footer = Tru
tablename = datasource["table"]
# [2] Layout
- layout = multisite_layouts[view["layout"]]
+ if html.output_format == "html":
+ layout = multisite_layouts[view["layout"]]
+ else:
+ layout = multisite_layouts.get(html.output_format)
+ if not layout:
+ layout = multisite_layouts["json"]
# User can override the layout settings via HTML variables (buttons)
# which are safed persistently. This is known as "view options"
@@ -1351,7 +1361,7 @@ def query_data(datasource, columns, add_columns, add_headers,
only_sites = [], l
html.live.set_prepend_site(True)
if limit != None:
html.live.set_limit(limit + 1) # + 1: We need to know, if limit is exceeded
- if config.debug:
+ if config.debug and html.output_format == "html":
html.write("<div
class=message><tt>%s</tt></div>\n" %
(query.replace('\n', '<br>\n')))
if only_sites:
diff --git a/web/plugins/views/webservice.py b/web/plugins/views/webservice.py
new file mode 100644
index 0000000..39621c7
--- /dev/null
+++ b/web/plugins/views/webservice.py
@@ -0,0 +1,31 @@
+def render_python_raw(data, view, group_painters, painters, num_columns):
+ html.write(repr(data))
+
+multisite_layouts["python-raw"] = {
+ "title" : "Python raw data output",
+ "render" : render_python_raw,
+ "group" : False,
+ "hide" : True,
+}
+
+def render_python(data, view, group_painters, painters, num_columns):
+ columns, rows = data
+ html.write("[\n")
+ html.write(repr([p[0]["title"] for p in painters]))
+ html.write(",\n")
+ for row in rows:
+ html.write("[")
+ for p in painters:
+ tdclass, content = prepare_paint(p, row)
+ html.write(repr(htmllib.strip_tags(content)))
+ html.write(",")
+ html.write("],")
+ html.write("\n]\n")
+
+multisite_layouts["python"] = {
+ "title" : "Python data output",
+ "render" : render_python,
+ "group" : False,
+ "hide" : True,
+}
+