Module: check_mk
Branch: master
Commit: f5df5cf581c945e44707d52d63b2fda0c38b0c1d
URL:
http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=f5df5cf581c945…
Author: Sven Panne <sp(a)mathias-kettner.de>
Date: Wed May 2 16:12:10 2018 +0200
Reduce copy-n-paste a bit by centralizing rendering of ages.
Change-Id: I3d8de99449bc5e33b3376b91deee6fe8a0ed6b42
---
active_checks/check_mailboxes | 25 ------------------------
cmk/ec/main.py | 21 +--------------------
cmk/render.py | 44 ++++++++++++++++++++++---------------------
cmk_base/check_api.py | 2 +-
4 files changed, 25 insertions(+), 67 deletions(-)
diff --git a/active_checks/check_mailboxes b/active_checks/check_mailboxes
index f460bc9..942510e 100755
--- a/active_checks/check_mailboxes
+++ b/active_checks/check_mailboxes
@@ -132,31 +132,6 @@ class ImapWrapper(object):
return MailList(self.__conn, ids[1][0].split())
-class Age(object):
- def __init__(self, secs):
- super(Age, self).__init__()
- self.__secs = secs
-
- def __str__(self):
- if self.__secs < 240:
- return "%d sec" % self.__secs
- mins = self.__secs / 60
- if mins < 120:
- return "%d min" % mins
- hours, mins = divmod(mins, 60)
- if hours < 12 and mins > 0:
- return "%d hours %d min" % (hours, mins)
- elif hours < 48:
- return "%d hours" % hours
- days, hours = divmod(hours, 24)
- if days < 7 and hours > 0:
- return "%d days %d hours" % (days, hours)
- return "%d days" % days
-
- def __float__(self):
- return float(self.__secs)
-
-
def output(message):
sys.stdout.write(message+"\n")
diff --git a/cmk/ec/main.py b/cmk/ec/main.py
index 68dacf5..082f7ca 100644
--- a/cmk/ec/main.py
+++ b/cmk/ec/main.py
@@ -601,7 +601,7 @@ class SNMPTrapTranslator(object):
if value.__class__.__name__ in ['ObjectIdentifier',
'IpAddress']:
val = value.prettyPrint()
elif value.__class__.__name__ == 'TimeTicks':
- val = self._fmt_timeticks(value._value)
+ val = str(cmk.render.Age(float(value._value) / 100))
else:
val = value._value
@@ -613,25 +613,6 @@ class SNMPTrapTranslator(object):
return var_binds
- # Format time difference seconds into approximated human readable value
- @staticmethod
- def _fmt_timeticks(ticks):
- secs = float(ticks) / 100
- if secs < 240:
- return "%d sec" % secs
- mins = secs / 60
-
- if mins < 120:
- return "%d min" % mins
-
- hours, mins = divmod(mins, 60)
- if hours < 48:
- return "%d hours, %d min" % (hours, mins)
-
- days, hours = divmod(hours, 24)
- return "%d days, %d hours, %d min" % (days, hours, mins)
-
-
# Convert pysnmp datatypes to simply handable ones
def _translate_via_mibs(self, ipaddress, var_bind_list):
var_binds = []
diff --git a/cmk/render.py b/cmk/render.py
index 92be084..29d7df8 100644
--- a/cmk/render.py
+++ b/cmk/render.py
@@ -64,27 +64,29 @@ def time_since(timestamp):
return timespan(time.time() - timestamp)
-def approx_age(secs):
- """Format time difference seconds into approximated human readable
text"""
-
- if secs < 240:
- return "%d sec" % secs
-
- mins = secs / 60
- if mins < 120:
- return "%d min" % mins
-
- hours, mins = divmod(mins, 60)
- if hours < 12 and mins > 0:
- return "%d hours %d min" % (hours, mins)
- elif hours < 48:
- return "%d hours" % hours
-
- days, hours = divmod(hours, 24)
- if days < 7 and hours > 0:
- return "%d days %d hours" % (days, hours)
-
- return "%d days" % days
+class Age(object):
+ def __init__(self, secs):
+ super(Age, self).__init__()
+ self.__secs = secs
+
+ def __str__(self):
+ if self.__secs < 240:
+ return "%d sec" % self.__secs
+ mins = self.__secs / 60
+ if mins < 120:
+ return "%d min" % mins
+ hours, mins = divmod(mins, 60)
+ if hours < 12 and mins > 0:
+ return "%d hours %d min" % (hours, mins)
+ elif hours < 48:
+ return "%d hours" % hours
+ days, hours = divmod(hours, 24)
+ if days < 7 and hours > 0:
+ return "%d days %d hours" % (days, hours)
+ return "%d days" % days
+
+ def __float__(self):
+ return float(self.__secs)
def bytes(b, base=1024.0, bytefrac=True, unit="B"):
diff --git a/cmk_base/check_api.py b/cmk_base/check_api.py
index bb70f15..6c41c3b 100644
--- a/cmk_base/check_api.py
+++ b/cmk_base/check_api.py
@@ -231,7 +231,7 @@ nagios_illegal_chars = _config.nagios_illegal_chars
is_ipv6_primary = _config.is_ipv6_primary
is_cmc = _config.is_cmc
-get_age_human_readable = render.approx_age
+get_age_human_readable = lambda secs: str(render.Age(secs))
get_bytes_human_readable = render.bytes
quote_shell_string = _utils.quote_shell_string