Skip to content
Permalink
Browse files

Cleanup nic speed unit painter

Change-Id: I0f9ae53ed720a7e57e52df8cf182e223bb05292d
  • Loading branch information...
Titan-C committed Sep 27, 2019
1 parent 730b445 commit 8f2817b91a775a3b7728844876825d4837582cb0
@@ -361,25 +361,9 @@ def inv_paint_count(b):
return "number", str(b)


def _nic_speed_human_readable(bits_per_second):
if bits_per_second == 10000000:
return "10 Mbit/s"
elif bits_per_second == 100000000:
return "100 Mbit/s"
elif bits_per_second == 1000000000:
return "1 Gbit/s"
elif bits_per_second < 1500:
return "%d bit/s" % bits_per_second
elif bits_per_second < 1000000:
return "%s Kbit/s" % cmk.utils.render.drop_dotzero(bits_per_second / 1000.0, digits=1)
elif bits_per_second < 1000000000:
return "%s Mbit/s" % cmk.utils.render.drop_dotzero(bits_per_second / 1000000.0, digits=2)
return "%s Gbit/s" % cmk.utils.render.drop_dotzero(bits_per_second / 1000000000.0, digits=2)


@decorate_inv_paint
def inv_paint_nic_speed(bits_per_second):
return "number", _nic_speed_human_readable(int(bits_per_second))
return "number", cmk.utils.render.fmt_nic_speed(bits_per_second)


@decorate_inv_paint
@@ -146,16 +146,41 @@ def scale_factor_prefix(value, base, prefixes=('', 'k', 'M', 'G', 'T', 'P')):
return factor / base, prefix # fixed: true-division


def drop_dotzero(v, digits=2):
# type: (float, int) -> str
"""Renders a number as a floating point number and drops useless
zeroes at the end of the fraction
45.1 -> "45.1"
45.0 -> "45"
"""
t = '%.*f' % (digits, v)
if "." in t:
return t.rstrip("0").rstrip(".")
return t


def fmt_number_with_precision(v, *args, **kwargs):
factor, prefix = scale_factor_prefix(v, base=kwargs.get('base', 1000.0))
value = float(v) / factor
precision = kwargs.get("precision", 2)
if kwargs.get("drop_zeroes", False):
number = drop_dotzero(value, precision)
else:
number = '%.*f' % (precision, value)

unit = kwargs.get("unit", "")
return '%s %s' % (number, prefix + unit)


def fmt_bytes(b, base=1024.0, precision=2, unit="B"):
# type: (int, float, int, str) -> str
"""Formats byte values to be used in texts for humans.
Takes bytes as integer and returns a string which represents the bytes in a
more human readable form scaled to TB/GB/MB/KB. The unit parameter simply
changes the returned string, but does not interfere with any calculations."""
factor, prefix = scale_factor_prefix(b, base)

return '%.*f %s' % (precision, b / factor, prefix + unit) # fixed: true-division
return fmt_number_with_precision(b, base=base, precision=precision, unit=unit)


# Precise size of a file - separated decimal separator
@@ -323,23 +348,18 @@ def calculate_physical_precision(v, precision):
return scale_symbols[scale], places_after_comma, 1000**scale


def drop_dotzero(v, digits=2):
"""Renders a number as a floating point number and drops useless
zeroes at the end of the fraction
def fmt_nic_speed(speed):
"""Format network speed (bit/s) for humans."""
try:
speedi = int(speed)
except ValueError:
return speed

45.1 -> "45.1"
45.0 -> "45"
"""
t = "%%.%df" % digits % v
if "." in t:
return t.rstrip("0").rstrip(".")
return t


def fmt_number_with_precision(v, *args, **kwargs):
precision = kwargs.get("drop_zeroes", None) or kwargs.get("precision", 2)
factor, prefix = scale_factor_prefix(v, base=1000.0)
return '%.*f %s' % (precision, float(v) / factor, prefix)
return fmt_number_with_precision(speedi,
base=1000.0,
precision=2,
unit="bit/s",
drop_zeroes=True)


#.
@@ -274,6 +274,7 @@ def is_ipv6_primary(hostname):

get_age_human_readable = lambda secs: "%s" % render.Age(secs)
get_bytes_human_readable = render.fmt_bytes
get_nic_speed_human_readable = render.fmt_nic_speed
get_percent_human_readable = render.percent
get_number_with_precision = render.fmt_number_with_precision
quote_shell_string = _cmk_utils.quote_shell_string
@@ -299,30 +300,6 @@ def get_filesize_human_readable(size):
return "%.2f GB" % (float(size) / (1024 * 1024 * 1024))


# TODO: Replace by some render.* function / move to render module?
def get_nic_speed_human_readable(speed):
"""Format network speed (bit/s) for humans."""
try:
speedi = int(speed)
except ValueError:
return speed

if speedi == 10000000:
return "10 Mbit/s"
elif speedi == 100000000:
return "100 Mbit/s"
elif speedi == 1000000000:
return "1 Gbit/s"
elif speedi < 1500:
return "%d bit/s" % speedi
elif speedi < 1000000:
return "%.1f Kbit/s" % (speedi / 1000.0)
elif speedi < 1000000000:
return "%.2f Mbit/s" % (speedi / 1000000.0)

return "%.2f Gbit/s" % (speedi / 1000000000.0)


# TODO: Replace by some render.* function / move to render module?
def get_timestamp_human_readable(timestamp):
"""Format a time stamp for humans in "%Y-%m-%d %H:%M:%S" format.
@@ -42,7 +42,7 @@
checks = {'': [(u'node0',
{'inv_speed': 16000000000, 'inv_state': u'online'},
[(0, u'State: online', []),
(0, '16.00 Gbit/s', []),
(0, '16 Gbit/s', []),
(0, u'Address de:ad:be:ef', []),
(0, 'Read: 150.00 B/s', [('read_bytes', 150.0, None, None, None, None)]),
(0, 'Write: 160.00 B/s', [('write_bytes', 160.0, None, None, None, None)]),
@@ -29,10 +29,10 @@
'errors': (0.01, 0.1),
'state': ['1'],
'speed': 10000000000
}, [(0, u'[TEAM:F[o]O 123-BAR] (Connected) 10.00 Gbit/s', [])]),
}, [(0, u'[TEAM:F[o]O 123-BAR] (Connected) 10 Gbit/s', [])]),
('2', {
'errors': (0.01, 0.1),
'state': ['1'],
'speed': 10000000000
}, [(0, u'[TEAM:F[o]O 123-BAR 2] (Connected) 10.00 Gbit/s', [])])]
}, [(0, u'[TEAM:F[o]O 123-BAR 2] (Connected) 10 Gbit/s', [])])]
}
@@ -83,7 +83,7 @@
'errors': (0.01, 0.1),
'state': ['1'],
'speed': 10000000000
}, [(0, u'[A B-C] (Connected) 10.00 Gbit/s', [])]),
}, [(0, u'[A B-C] (Connected) 10 Gbit/s', [])]),
('2', {
'errors': (0.01, 0.1),
'state': ['1'],
@@ -93,7 +93,7 @@
'errors': (0.01, 0.1),
'state': ['1'],
'speed': 10000000000
}, [(0, u'[A B-C 3] (Connected) 10.00 Gbit/s', [])]),
}, [(0, u'[A B-C 3] (Connected) 10 Gbit/s', [])]),
('4', {
'errors': (0.01, 0.1),
'state': ['1'],
@@ -353,7 +353,7 @@ def test_winperf_if_parse_sections(check_manager, info):
)]),
CheckResult([(
0,
"Teaming Status (up), Members: [isatap.{F1F1F1F1-F1F1-F1F1-F1F1-F1F1F1F1F1F1} (Connected), isatap.{E1E1E1E1-E1E1-E1E1-E1E1-E1E1E1E1E1E1} (Connected), isatap.{D1D1D1D1-D1D1-D1D1-D1D1-D1D1D1D1D1D1} (Connected), isatap.{C1C1C1C1-C1C1-C1C1-C1C1-C1C1C1C1C1C1} (Connected), isatap.{B1B1B1B1-B1B1-B1B1-B1B1-B1B1B1B1B1B1} (Connected), isatap.{A1A1A1A1-A1A1-A1A1-A1A1-A1A1A1A1A1A1} (Connected)] 600.0 Kbit/s",
"Teaming Status (up), Members: [isatap.{F1F1F1F1-F1F1-F1F1-F1F1-F1F1F1F1F1F1} (Connected), isatap.{E1E1E1E1-E1E1-E1E1-E1E1-E1E1E1E1E1E1} (Connected), isatap.{D1D1D1D1-D1D1-D1D1-D1D1-D1D1D1D1D1D1} (Connected), isatap.{C1C1C1C1-C1C1-C1C1-C1C1-C1C1C1C1C1C1} (Connected), isatap.{B1B1B1B1-B1B1-B1B1-B1B1-B1B1B1B1B1B1} (Connected), isatap.{A1A1A1A1-A1A1-A1A1-A1A1-A1A1A1A1A1A1} (Connected)] 600 kbit/s",
[],
)]),
CheckResult([(
@@ -482,7 +482,7 @@ def test_winperf_if_teaming_performance_data(check_manager, monkeypatch):
assert result_3 == CheckResult([
BasicCheckResult(
0,
u'[SLOT 6 Port 1 DAG] (Connected) MAC: A0:36:9F:B0:A3:60, 10.00 Gbit/s, In: 0.00 B/s (0.0%), Out: 1.00 GB/s (85.9%)',
u'[SLOT 6 Port 1 DAG] (Connected) MAC: A0:36:9F:B0:A3:60, 10 Gbit/s, In: 0.00 B/s (0.0%), Out: 1.00 GB/s (85.9%)',
[
PerfValue('in', 0.0, None, None, 0, 1250000000.0),
PerfValue('inucast', 0.0, None, None, None, None),
@@ -500,7 +500,7 @@ def test_winperf_if_teaming_performance_data(check_manager, monkeypatch):
assert result_8 == CheckResult([
BasicCheckResult(
0,
u'[SLOT 4 Port 2 DAG] (Connected) MAC: A0:36:9F:B0:B3:66, 10.00 Gbit/s, In: 0.00 B/s (0.0%), Out: 1.00 GB/s (85.9%)',
u'[SLOT 4 Port 2 DAG] (Connected) MAC: A0:36:9F:B0:B3:66, 10 Gbit/s, In: 0.00 B/s (0.0%), Out: 1.00 GB/s (85.9%)',
[
PerfValue('in', 0.0, None, None, 0, 1250000000.0),
PerfValue('inucast', 0.0, None, None, None, None),
@@ -518,7 +518,7 @@ def test_winperf_if_teaming_performance_data(check_manager, monkeypatch):
assert result_dag_net == CheckResult([
BasicCheckResult(
0,
'Teaming Status (up), Members: [8 (Connected), 3 (Connected)] 20.00 Gbit/s, In: 0.00 B/s (0.0%), Out: 2.00 GB/s (85.9%)',
'Teaming Status (up), Members: [8 (Connected), 3 (Connected)] 20 Gbit/s, In: 0.00 B/s (0.0%), Out: 2.00 GB/s (85.9%)',
[
PerfValue('in', 0.0, None, None, 0, 2500000000.0),
PerfValue('inucast', 0.0, None, None, None, None),
@@ -135,7 +135,7 @@ def test_replace_expression():
perfdata = [(n, len(n), u'', 120, 240, 0, 25) for n in ['load1']]
translated_metrics = utils.translate_metrics(perfdata, 'check_mk-cpu.loads')
assert utils.replace_expressions("CPU Load - %(load1:max@count) CPU Cores",
translated_metrics) == 'CPU Load - 25.0 CPU Cores'
translated_metrics) == 'CPU Load - 25 CPU Cores'


@pytest.mark.parametrize("text, out", [
@@ -20,3 +20,23 @@ def test_fmt_bytes(entry, result):
@pytest.mark.parametrize("args, result", [((0.433 / 1, 10), (4.33, -1)), ((5, 10), (5, 0))])
def test_frexpb(args, result):
assert cmk.utils.render._frexpb(*args) == result


@pytest.mark.parametrize("value, kwargs, result", [
(10000486, {
'precision': 5
}, "10.00049 M"),
(100000000, {
'drop_zeroes': False
}, "100.00 M"),
])
def test_fmt_number_with_precision(value, kwargs, result):
assert cmk.utils.render.fmt_number_with_precision(value, **kwargs) == result


@pytest.mark.parametrize("entry, result", [(10000000, "10 Mbit/s"), (100000000, "100 Mbit/s"),
(1000000000, "1 Gbit/s"), (1400, "1.4 kbit/s"),
(8450, "8.45 kbit/s"), (26430, "26.43 kbit/s"),
(8583000, "8.58 Mbit/s"), (7.84e9, "7.84 Gbit/s")])
def test_fmt_nic_speed(entry, result):
assert cmk.utils.render.fmt_nic_speed(entry) == result

0 comments on commit 8f2817b

Please sign in to comment.
You can’t perform that action at this time.