Module: check_mk
Branch: master
Commit: fac03e9aa9ccb4aca1c49662bc4db839553db3f0
URL:
http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=fac03e9aa9ccb4…
Author: Lars Michelsen <lm(a)mathias-kettner.de>
Date: Wed Feb 3 10:45:04 2016 +0100
Generalized dashlet graph rendering code
---
web/htdocs/metrics.py | 40 ++++++++++++++++++-------------------
web/plugins/dashboard/dashlets.py | 36 ++++++++++++++++++++++-----------
web/plugins/pages/shipped.py | 2 +-
3 files changed, 45 insertions(+), 33 deletions(-)
diff --git a/web/htdocs/metrics.py b/web/htdocs/metrics.py
index 4b9132e..d755f45 100644
--- a/web/htdocs/metrics.py
+++ b/web/htdocs/metrics.py
@@ -1351,22 +1351,25 @@ def host_service_graph_popup_pnp(site, host_name,
service_description):
# | This page handler is called by graphs embedded in a dashboard. |
# '----------------------------------------------------------------------'
-def page_host_service_graph_dashlet():
+def page_graph_dashlet():
+ spec = html.var("spec")
+ if not spec:
+ raise MKUserError("spec", _("Missing spec parameter"))
+
+ graph_specification = json.loads(html.var("spec"))
+
if cmk_graphs_possible():
- func = host_service_graph_dashlet_cmk
+ host_service_graph_dashlet_cmk(graph_specification)
+ elif graph_specification[0] == "template":
+ host_service_graph_dashlet_pnp(graph_specification)
else:
- func = host_service_graph_dashlet_pnp
-
- site = html.var('site')
- host_name = html.var('host_name')
- service_description = html.var('service')
- source = int(html.var("source"))
- return func(site, host_name, service_description, source)
+ html.write(_("This graph can not be rendered."))
-def host_service_graph_dashlet_cmk(site, host_name, service_description, source):
+def host_service_graph_dashlet_cmk(graph_specification):
size = (int(((float(html.var("width")) - 49 - 5)/html_size_per_ex)),
int((float(html.var("height")) - 23)/html_size_per_ex))
+
graph_render_options = {
"size" : size,
"font_size" : 8,
@@ -1375,26 +1378,23 @@ def host_service_graph_dashlet_cmk(site, host_name,
service_description, source)
"resizable" : False,
}
+ # FIXME: html.var("timerange") ???
end_time = time.time()
start_time = end_time - 8 * 3600
graph_data_range = {
"time_range" : (start_time, end_time),
}
- graph_specification = (
- "template", {
- "site" : site,
- "host_name" : host_name,
- "service_description" : service_description,
- })
-
html_code = render_graphs_from_specification_html(graph_specification,
graph_data_range, graph_render_options)
html.write(html_code)
-def host_service_graph_dashlet_pnp(site, host_name, service_description, source):
- pnp_host = pnp_cleanup(host_name)
- pnp_svc = pnp_cleanup(service_description)
+def host_service_graph_dashlet_pnp(graph_specification):
+ site = graph_specification[1]["site"]
+ source = int(graph_specification[1]["graph_index"])
+
+ pnp_host = pnp_cleanup(graph_specification[1]["host_name"])
+ pnp_svc = pnp_cleanup(graph_specification[1]["service_description"])
url_prefix = html.site_status[site]["site"]["url_prefix"]
html.write(url_prefix +
"pnp4nagios/index.php/image?host=%s&srv=%s&source=%d&view=%s&theme=multisite"
% \
diff --git a/web/plugins/dashboard/dashlets.py b/web/plugins/dashboard/dashlets.py
index 2331d77..0b32df7 100644
--- a/web/plugins/dashboard/dashlets.py
+++ b/web/plugins/dashboard/dashlets.py
@@ -24,6 +24,11 @@
# to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
# Boston, MA 02110-1301 USA.
+try:
+ import simplejson as json
+except ImportError:
+ import json
+
# .--Overview------------------------------------------------------------.
# | ___ _ |
# | / _ \__ _____ _ ____ _(_) _____ __ |
@@ -361,8 +366,15 @@ def dashlet_graph_reload_js(nr, dashlet):
# configured. So we assume the default timerange here by default.
timerange = dashlet.get('timerange', '1')
- return 'dashboard_render_graph(%d, \'%s\', \'%s\',
\'%s\', \'%s\', \'%s\')' % \
- (nr, site, host, service, dashlet["source"], timerange)
+ graph_specification = ("template", {
+ "site" : site,
+ "host_name" : host,
+ "service_description" : service,
+ "graph_index" : dashlet["source"] -1,
+ })
+
+ return "dashboard_render_graph(%d, %s, '%s')" % \
+ (nr, json.dumps(graph_specification), timerange)
dashlet_types["pnpgraph"] = {
@@ -410,22 +422,22 @@ dashlet_types["pnpgraph"] = {
"on_refresh" : dashlet_graph_reload_js,
"script": """
var dashlet_offsets = {};
-function dashboard_render_graph(nr, site, host_name, service, source, timerange)
+function dashboard_render_graph(nr, graph_specification, timerange)
{
// Get the target size for the graph from the inner dashlet container
var inner = document.getElementById('dashlet_inner_' + nr);
var c_w = inner.clientWidth;
var c_h = inner.clientHeight;
- call_ajax("host_service_graph_dashlet.py?site="+encodeURIComponent(site)
- +"&host_name="+encodeURIComponent(host_name)
- +"&service="+encodeURIComponent(service)
- +"&source="+encodeURIComponent(source)
- +"&timerange="+encodeURIComponent(timerange)
- +"&width="+c_w
- +"&height="+c_h
- +"&id="+nr
- +"&what=dashlet", {
+ var post_data = "spec=" +
encodeURIComponent(JSON.stringify(graph_specification))
+ + "&timerange=" + encodeURIComponent(timerange)
+ + "&width=" + c_w
+ + "&height=" + c_h
+ + "&id=" + nr;
+
+ call_ajax("graph_dashlet.py", {
+ post_data : post_data,
+ method : "POST",
response_handler : handle_dashboard_render_graph_response,
handler_data : nr,
});
diff --git a/web/plugins/pages/shipped.py b/web/plugins/pages/shipped.py
index 1795ab0..477fcb1 100644
--- a/web/plugins/pages/shipped.py
+++ b/web/plugins/pages/shipped.py
@@ -64,7 +64,7 @@ pagehandlers.update({
"view" : views.page_view,
"ajax_inv_render_tree" : views.ajax_inv_render_tree,
"host_service_graph_popup" : metrics.page_host_service_graph_popup,
- "host_service_graph_dashlet" : metrics.page_host_service_graph_dashlet,
+ "graph_dashlet" : metrics.page_graph_dashlet,
"ajax_set_rowselection" : weblib.ajax_set_rowselection,
"prediction_graph" : prediction.page_graph,
"logwatch" : logwatch.page_show,