Module: check_mk
Branch: master
Commit: dfefe49fe419a1ec65620878564301a0744147d8
URL:
http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=dfefe49fe419a1…
Author: Mathias Kettner <mk(a)mathias-kettner.de>
Date: Sat Mar 2 20:16:54 2013 +0100
Prediction: graph does now also show RRD data
---
modules/prediction.py | 24 ++++++++++++++----
web/htdocs/js/prediction.js | 21 ++++++++++++----
web/htdocs/prediction.css | 7 +++--
web/htdocs/prediction.py | 44 +++++++++++++++++++++++++++-------
web/plugins/wato/check_parameters.py | 2 +-
5 files changed, 74 insertions(+), 24 deletions(-)
diff --git a/modules/prediction.py b/modules/prediction.py
index ea08712..e7875c0 100644
--- a/modules/prediction.py
+++ b/modules/prediction.py
@@ -126,8 +126,8 @@ def group_by_wday(t):
day_of_epoch, rel_time = divmod(t - time.timezone, 86400)
return daynames[wday], rel_time
-def group_by_hour(t):
- return "%02d" % ((t % 86400) / 3600), t % 3600
+def group_by_day(t):
+ return "everyday", (t - time.timezone) % 86400
prediction_periods = {
"wday" : {
@@ -135,10 +135,10 @@ prediction_periods = {
"groupby" : group_by_wday,
"valid" : 7,
},
- "hour" : {
- "slice" : 3600,
- "groupby" : group_by_hour,
- "valid" : 24,
+ "day" : {
+ "slice" : 86400,
+ "groupby" : group_by_day,
+ "valid" : 1,
}
}
@@ -246,6 +246,18 @@ def get_predictive_levels(dsname, params, cf):
if opt_debug:
sys.stderr.write("Prediction parameters have changed.\n")
last_info = None
+ # Remove all prediction files that result from other
+ # prediction periods. This is e.g. needed if the user switches
+ # the parameter from 'wday' to 'day'.
+ for f in os.listdir(dir):
+ if f.endswith(".info"):
+ try:
+ info = eval(file(dir + "/" + f).read())
+ if info["period"] != params["period"]:
+ os.remove(dir + "/" + f)
+ os.remove(dir + "/" + f[:-5])
+ except:
+ pass
break
except Exception, e:
if opt_debug:
diff --git a/web/htdocs/js/prediction.js b/web/htdocs/js/prediction.js
index df41dbc..cdf23d3 100644
--- a/web/htdocs/js/prediction.js
+++ b/web/htdocs/js/prediction.js
@@ -37,6 +37,8 @@ var right_border = 50;
var top_border = 40;
var bottom_border = 50;
+var None = null; // make us compatible with Python repr()
+
function create_graph(cid, ft, ut, vmi, vma)
{
// Keep important data as global variables, needed by
@@ -199,21 +201,30 @@ function render_point(t, v, color)
}
-function render_curve(points, color)
+function render_curve(points, color, w)
{
var canvas = document.getElementById(canvas_id);
var c = canvas.getContext('2d');
c.beginPath();
c.strokeStyle = color;
- c.lineWidth = 1;
+ c.lineWidth = w;
- var p0 = point(points[0][0], points[0][1]);
var time_step = (until_time - from_time) / points.length;
- c.moveTo(p0[0], p0[1]);
+ var first = true;
for (i=0; i<points.length; i++) {
+ if (points[i] == null) {
+ c.stroke();
+ first = true;
+ continue;
+ }
var p = point(from_time + time_step * i, points[i]);
- c.lineTo(p[0], p[1]);
+ if (first) {
+ c.moveTo(p[0], p[1]);
+ first = false;
+ }
+ else
+ c.lineTo(p[0], p[1]);
}
c.stroke();
}
diff --git a/web/htdocs/prediction.css b/web/htdocs/prediction.css
index a07296c..68a7e90 100644
--- a/web/htdocs/prediction.css
+++ b/web/htdocs/prediction.css
@@ -30,6 +30,7 @@ table.prediction canvas {
}
table.prediction {
+ margin-top: 10px;
border: 1px solid black;
box-shadow: 0.5px 0.5px 2px #888;
border-spacing: 0px;
@@ -53,9 +54,9 @@ table.prediction td.legend div {
table.prediction td.legend div.color {
border: 1px solid black;
- height: 14px;
- width: 14px;
- vertical-align: middle;
+ height: 12px;
+ width: 12px;
+ vertical-align: bottom;
display: inline-block;
box-shadow: 0.5px 0.5px 1px #888;
}
diff --git a/web/htdocs/prediction.py b/web/htdocs/prediction.py
index e37e794..58aaedd 100644
--- a/web/htdocs/prediction.py
+++ b/web/htdocs/prediction.py
@@ -32,6 +32,25 @@ from lib import *
graph_size = 2000, 700
+# Import helper functions from check_mk module prediction.py. Maybe we should
+# find some more clean way some day for creating common Python code between
+# Check_MK CCE and Multisite.
+execfile(defaults.modules_dir + "/prediction.py")
+rrd_path = defaults.rrd_path
+rrdcached_socket = None
+omd_root = None
+try:
+ omd_root = default.omd_root
+ if omd_root:
+ rrdcached_socket = omd_root + "/tmp/run/rrdcached.sock"
+ else:
+ try:
+ rrdcached_socket = config.rrdcached_socket
+ except:
+ pass
+except:
+ pass
+
def page_graph():
host = html.var("host")
service = html.var("service")
@@ -61,6 +80,9 @@ def page_graph():
timegroup = tg_info
timegroups.sort(cmp = lambda a,b: cmp(a["range"], b["range"]))
+ if not timegroup:
+ timegroup = timegroups[0]
+
choices = [ (tg_info["name"], tg_info["name"].title())
for tg_info in timegroups ]
@@ -70,9 +92,7 @@ def page_graph():
html.hidden_fields()
html.end_form()
- title = _("Prediction for %s") % timegroup["name"]
- # time_range aus info-Datei holen
- # v_range ermitteln
+ # Get prediction data
tg_data = eval(file(dir + "/" + timegroup["name"]).read())
swapped = swap_and_compute_levels(tg_data, timegroup)
vertical_range = compute_vertical_range(swapped)
@@ -85,7 +105,7 @@ def page_graph():
if current_value != None:
legend.append( ("#0000ff", _("Current value: %.2f") %
current_value) )
- create_graph(timegroup["name"], title, graph_size,
timegroup["range"], vertical_range, legend)
+ 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)
@@ -96,13 +116,15 @@ def page_graph():
render_area(swapped["lower_crit"], "#ff0000", 0.1)
vert_scala = [ [x, "%.1f" % x] for x in range(int(vertical_range[0]),
int(vertical_range[1] + 1)) ]
+ while len(vert_scala) > 15:
+ vert_scala = vert_scala[::2]
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")
- render_curve(swapped["lower_crit"], "#j0b0a0")
+ render_curve(swapped["lower_crit"], "#f0b0a0")
if "levels_upper" in timegroup:
render_dual_area(swapped["upper_warn"], swapped["average"],
"#ffffff", 0.5)
@@ -112,6 +134,11 @@ def page_graph():
render_curve(swapped["average"], "#000000")
# render_curve(stack(swapped["average"], swapped["stdev"], -1),
"#008040")
+ # Try to get current RRD data and render it also
+ from_time, until_time = timegroup["range"]
+ 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 = time.time() % timegroup["slice"]
render_point(timegroup["range"][0] + rel_time, current_value,
"#0000ff")
@@ -178,8 +205,7 @@ def compute_vertical_range(swapped):
mmin = min(mmin, min(points))
return mmin, mmax
-def create_graph(name, title, size, range, v_range, legend):
- html.write('<h3 class=prediction>%s</h3>' % title)
+def create_graph(name, size, range, v_range, legend):
html.write('<table class=prediction><tr><td>')
html.write('<canvas class=prediction id="content_%s"
style="width: %dpx; height: %dpx;" width=%d height=%d></canvas>' %
(
name, size[0]/2, size[1]/2, size[0], size[1]))
@@ -195,8 +221,8 @@ def render_coordinates(v_scala, t_scala):
html.javascript('render_coordinates(%r, %r);' % (v_scala, t_scala))
-def render_curve(points, color):
- html.javascript('render_curve(%r, %r);' % (points, color))
+def render_curve(points, color, width=1):
+ html.javascript('render_curve(%r, %r, %d);' % (points, color, width))
def render_point(t, v, color):
html.javascript('render_point(%r, %r, %r);' % (t, v, color))
diff --git a/web/plugins/wato/check_parameters.py b/web/plugins/wato/check_parameters.py
index 17eb527..cc19f64 100644
--- a/web/plugins/wato/check_parameters.py
+++ b/web/plugins/wato/check_parameters.py
@@ -1413,7 +1413,7 @@ checkgroups.append((
title = _("Base prediction on"),
choices = [
( "wday", _("Day of the week (00:00 -
24:00 in local time)") ),
- ( "hour", _("Hour of the day")
),
+ ( "day", _("Hour of the day") ),
]
)),
( "horizon",