Module: check_mk
Branch: master
Commit: 367777ee9f5847cb9cf6e21838e423fb13eb0b77
URL:
http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=367777ee9f5847…
Author: Lars Michelsen <lm(a)mathias-kettner.de>
Date: Fri Feb 16 16:33:09 2018 +0100
Moved byte value rendering functions to more specific places
Change-Id: I3c4efa541b197a084750bc74d2e83f9774783603
---
web/htdocs/lib.py | 90 --------------------------------------
web/plugins/metrics/check_mk.py | 83 +++++++++++++++++++++++++++++++++--
web/plugins/perfometer/check_mk.py | 4 +-
web/plugins/views/inventory.py | 4 +-
web/plugins/wato/mkeventd.py | 4 +-
5 files changed, 88 insertions(+), 97 deletions(-)
diff --git a/web/htdocs/lib.py b/web/htdocs/lib.py
index ac82440..5f4562d 100644
--- a/web/htdocs/lib.py
+++ b/web/htdocs/lib.py
@@ -464,93 +464,3 @@ def date_range_human_readable(start_time, end_time):
def date_month_human_readable(timestamp):
# TODO: %B will currently not be localized
return time.strftime(_("%B %Y"), time.localtime(timestamp))
-
-
-def calculate_scaled_number(v, base=1024.0, precision=1):
- base = float(base)
-
- if v >= base ** 4:
- symbol = "T"
- scale = base ** 4
-
- elif v >= base ** 3:
- symbol = "G"
- scale = base ** 3
-
- elif v >= base ** 2:
- symbol = "M"
- scale = base ** 2
-
- elif v >= base:
- symbol = "k"
- scale = base
-
- else:
- symbol = ""
- scale = 1
-
- return symbol, precision, scale
-
-
-def calculate_scaled_bytes(v, base=1024.0, bytefrac=True):
- digits = 2
- if not bytefrac:
- digits = 0
-
- return calculate_scaled_number(v, base, precision=digits)
-
-
-def bytes_human_readable(b, *args, **kwargs):
- if b < 0:
- return "-" + bytes_human_readable(-b, *args, **kwargs)
-
- if "unit" in kwargs:
- unit = kwargs.pop("unit")
- else:
- unit = "B"
-
- scale_symbol, places_after_comma, scale_factor = calculate_scaled_bytes(b, *args,
**kwargs)
-
- scaled_value = float(b) / scale_factor
- if scale_symbol == "" and unit == "B":
- return u"%.0f %s%s" % (scaled_value, scale_symbol, unit)
- else:
- return (u"%%.%df %%s%%s" % places_after_comma) % (scaled_value,
scale_symbol, unit)
-
-
-def bytes_human_readable_list(values, *args, **kwargs):
- if not values:
- reference = 0
- else:
- reference = min([ abs(v) for v in values ])
-
- if "unit" in kwargs:
- unit = kwargs.pop("unit")
- else:
- unit = "B"
-
- scale_symbol, places_after_comma, scale_factor = calculate_scaled_bytes(reference,
*args, **kwargs)
-
- units = []
- scaled_values = []
- for value in values:
- scaled_value = float(value) / scale_factor
- scaled_values.append(("%%.%df" % places_after_comma) % scaled_value)
-
- return "%s%s" % (scale_symbol, unit), scaled_values
-
-
-def metric_number_with_precision(v, *args, **kwargs):
- if v < 0:
- return "-" + metric_number_with_precision(-v, *args, **kwargs)
-
- subargs = {
- "base": 1000.0,
- "precision": kwargs.get("precision", 2),
- }
- scale_symbol, places_after_comma, scale_factor = calculate_scaled_number(v, *args,
**subargs)
- scaled_value = float(v) / scale_factor
- text = ((u"%%.%df %%s" % places_after_comma) % (scaled_value,
scale_symbol)).rstrip()
- if kwargs.get("drop_zeroes"):
- text = text.rstrip("0").rstrip(".")
- return text
diff --git a/web/plugins/metrics/check_mk.py b/web/plugins/metrics/check_mk.py
index a305404..2725392 100644
--- a/web/plugins/metrics/check_mk.py
+++ b/web/plugins/metrics/check_mk.py
@@ -24,6 +24,8 @@
# to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
# Boston, MA 02110-1301 USA.
+import cmk.render
+
# TODO Graphingsystem:
# - Default-Template: Wenn im Graph kein "range" angegeben ist, aber
# in der Unit eine "range"-Angabe ist, dann soll diese genommen werden.
@@ -68,6 +70,50 @@ unit_info[""] = {
"render" : lambda v: render_scientific(v, 2),
}
+
+# TODO: Check whether or not we can use functions from cmk.render or move it there
+def calculate_scaled_number(v, base=1024.0, precision=1):
+ base = float(base)
+
+ if v >= base ** 4:
+ symbol = "T"
+ scale = base ** 4
+
+ elif v >= base ** 3:
+ symbol = "G"
+ scale = base ** 3
+
+ elif v >= base ** 2:
+ symbol = "M"
+ scale = base ** 2
+
+ elif v >= base:
+ symbol = "k"
+ scale = base
+
+ else:
+ symbol = ""
+ scale = 1
+
+ return symbol, precision, scale
+
+
+def metric_number_with_precision(v, *args, **kwargs):
+ if v < 0:
+ return "-" + metric_number_with_precision(-v, *args, **kwargs)
+
+ subargs = {
+ "base": 1000.0,
+ "precision": kwargs.get("precision", 2),
+ }
+ scale_symbol, places_after_comma, scale_factor = calculate_scaled_number(v, *args,
**subargs)
+ scaled_value = float(v) / scale_factor
+ text = ((u"%%.%df %%s" % places_after_comma) % (scaled_value,
scale_symbol)).rstrip()
+ if kwargs.get("drop_zeroes"):
+ text = text.rstrip("0").rstrip(".")
+ return text
+
+
unit_info["count"] = {
"title" : _("Count"),
"symbol" : "",
@@ -109,14 +155,14 @@ unit_info["hz"] = {
unit_info["bytes"] = {
"title" : _("Bytes"),
"symbol" : _("B"),
- "render" : bytes_human_readable,
+ "render" : cmk.render.bytes,
"stepping" : "binary", # for vertical graph labels
}
unit_info["bytes/s"] = {
"title" : _("Bytes per second"),
"symbol" : _("B/s"),
- "render" : lambda v: bytes_human_readable(v) + _("/s"),
+ "render" : lambda v: cmk.render.bytes(v) + _("/s"),
"stepping" : "binary", # for vertical graph labels
}
@@ -127,11 +173,42 @@ unit_info["bits/s"] = {
"graph_unit" : lambda v: physical_precision_list(v, 3,
_("bit/s")),
}
+# TODO: Check whether or not we can use functions from cmk.render or move it there
+def calculate_scaled_bytes(v, base=1024.0, bytefrac=True):
+ digits = 2
+ if not bytefrac:
+ digits = 0
+
+ return calculate_scaled_number(v, base, precision=digits)
+
+
+def bytes_human_readable_list(values, *args, **kwargs):
+ if not values:
+ reference = 0
+ else:
+ reference = min([ abs(v) for v in values ])
+
+ if "unit" in kwargs:
+ unit = kwargs.pop("unit")
+ else:
+ unit = "B"
+
+ scale_symbol, places_after_comma, scale_factor = calculate_scaled_bytes(reference,
*args, **kwargs)
+
+ units = []
+ scaled_values = []
+ for value in values:
+ scaled_value = float(value) / scale_factor
+ scaled_values.append(("%%.%df" % places_after_comma) % scaled_value)
+
+ return "%s%s" % (scale_symbol, unit), scaled_values
+
+
# Output in bytes/days, value is in bytes/s
unit_info["bytes/d"] = {
"title" : _("Bytes per day"),
"symbol" : _("B/d"),
- "render" : lambda v: bytes_human_readable(v * 86400.0,
unit="B/d"),
+ "render" : lambda v: cmk.render.bytes(v * 86400.0) + "/d",
"graph_unit" : lambda values: bytes_human_readable_list(
[ v * 86400.0 for v in values ], unit=_("B/d")),
"stepping" : "binary", # for vertical graph labels
diff --git a/web/plugins/perfometer/check_mk.py b/web/plugins/perfometer/check_mk.py
index 21793ad..b89a44f 100644
--- a/web/plugins/perfometer/check_mk.py
+++ b/web/plugins/perfometer/check_mk.py
@@ -24,6 +24,8 @@
# to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
# Boston, MA 02110-1301 USA.
+import cmk.render
+
# Perf-O-Meters for Check_MK's checks
#
# They are called with:
@@ -1055,7 +1057,7 @@ def perfometer_veeam_client(row, check_command, perf_data):
duration_secs = int(graph[1])
h = perfometer_logarithmic_dual_independent(avgspeed_bytes, '#54b948',
10000000, 2, duration_secs, '#2098cb', 500, 2)
- avgspeed = bytes_human_readable(avgspeed_bytes)
+ avgspeed = cmk.render.bytes(avgspeed_bytes)
# Return Value always as minutes
duration = age_human_readable(duration_secs, True)
diff --git a/web/plugins/views/inventory.py b/web/plugins/views/inventory.py
index 5c1b5ef..c41e13f 100644
--- a/web/plugins/views/inventory.py
+++ b/web/plugins/views/inventory.py
@@ -28,7 +28,7 @@
import inventory
from cmk.regex import regex
import cmk.defines as defines
-
+import cmk.render
def paint_host_inventory_tree(row, invpath=".",
column="host_inventory"):
struct_tree = row.get(column)
@@ -220,7 +220,7 @@ def inv_paint_bytes(b):
@decorate_inv_paint
def inv_paint_size(b):
- return "number", bytes_human_readable(b)
+ return "number", cmk.render.bytes(b)
@decorate_inv_paint
diff --git a/web/plugins/wato/mkeventd.py b/web/plugins/wato/mkeventd.py
index 96da24e..ca1a949 100644
--- a/web/plugins/wato/mkeventd.py
+++ b/web/plugins/wato/mkeventd.py
@@ -31,6 +31,8 @@ import cStringIO
import cmk.paths
import cmk.ec.export as ec
import cmk.ec.defaults
+import cmk.store as store
+import cmk.render
mkeventd_enabled = config.mkeventd_enabled
@@ -2164,7 +2166,7 @@ def show_mib_table(path, title):
table.cell(_("Filename"), filename)
table.cell(_("MIB"), mib.get("name", ""))
table.cell(_("Organization"), mib.get("organization",
""))
- table.cell(_("Size"), bytes_human_readable(mib.get("size",
0)), css="number")
+ table.cell(_("Size"), cmk.render.bytes(mib.get("size", 0)),
css="number")
table.end()