Commit 58ceb8bb authored by Navid Sassan's avatar Navid Sassan
Browse files

Merge branch 'develop' into 'master'

new release

See merge request !5
parents dfeb0df6 9a430395
......@@ -12,7 +12,7 @@
"""
__author__ = 'Linuxfabrik GmbH, Zurich/Switzerland'
__version__ = '2020051901'
__version__ = '2020122301'
import collections
import datetime
......@@ -374,7 +374,7 @@ def match_range(value, spec):
return int(atom)
if spec is None or spec.lower() == 'none':
if spec is None or str(spec).lower() == 'none':
return (True, None)
if not isinstance(spec, str):
spec = str(spec)
......@@ -399,7 +399,7 @@ def match_range(value, spec):
return (True, (start, end, invert))
if spec is None or spec.lower() == 'none':
if spec is None or str(spec).lower() == 'none':
return (True, True)
success, result = parse_range(spec)
if not success:
......@@ -548,7 +548,10 @@ def seconds2human(seconds, keep_short=True, full_name=False):
'2weeks 2days 1hour 29minutes 35seconds'
"""
seconds = int(seconds)
seconds = float(seconds)
if seconds < 1:
return '{:.2f}s'.format(seconds)
if full_name:
intervals = (
('years', 60*60*24*365),
......@@ -691,7 +694,7 @@ def sort(array, reverse=True, sort_by_key=False):
if isinstance(array, dict):
if not sort_by_key:
return sorted(array.items(), key=lambda x: x[1], reverse=reverse)
return sorted(array.items(), key=lambda x: x[0].lower(), reverse=reverse)
return sorted(array.items(), key=lambda x: str(x[0]).lower(), reverse=reverse)
return array
......@@ -739,6 +742,28 @@ def sum_lod(mylist):
return total
def str2state(string):
"""Return the state based on a string.
>> lib.base.str2state('ok')
0
>>> lib.base.str2state('warn')
1
>>> lib.base.str2state('warning')
1
"""
string = str(string).lower()
if string == 'ok':
return STATE_OK
if string.startswith('warn'):
return STATE_WARN
if string.startswith('crit'):
return STATE_CRIT
if string.startswith('unk'):
return STATE_UNKNOWN
def state2str(state, empty_ok=True, prefix='', suffix=''):
"""Return the state's string representation.
The square brackets around the state cause icingaweb2 to color the state.
......
......@@ -12,7 +12,7 @@
"""
__author__ = 'Linuxfabrik GmbH, Zurich/Switzerland'
__version__ = '2020051901'
__version__ = '2020122301'
import collections
import datetime
......@@ -212,6 +212,7 @@ def get_state(value, warn, crit, operator='ge'):
if value >= float(warn):
return STATE_WARN
return STATE_OK
if operator == 'gt':
if crit is not None:
if value > float(crit):
......@@ -220,6 +221,7 @@ def get_state(value, warn, crit, operator='ge'):
if value > float(warn):
return STATE_WARN
return STATE_OK
if operator == 'le':
if crit is not None:
if value <= float(crit):
......@@ -228,6 +230,7 @@ def get_state(value, warn, crit, operator='ge'):
if value <= float(warn):
return STATE_WARN
return STATE_OK
if operator == 'lt':
if crit is not None:
if value < float(crit):
......@@ -236,6 +239,7 @@ def get_state(value, warn, crit, operator='ge'):
if value < float(warn):
return STATE_WARN
return STATE_OK
if operator == 'eq':
if crit is not None:
if value == float(crit):
......@@ -244,6 +248,7 @@ def get_state(value, warn, crit, operator='ge'):
if value == float(warn):
return STATE_WARN
return STATE_OK
if operator == 'ne':
if crit is not None:
if value != float(crit):
......@@ -252,6 +257,7 @@ def get_state(value, warn, crit, operator='ge'):
if value != float(warn):
return STATE_WARN
return STATE_OK
return STATE_UNKNOWN
......@@ -373,7 +379,7 @@ def match_range(value, spec):
return int(atom)
if spec is None or spec.lower() == 'none':
if spec is None or str(spec).lower() == 'none':
return (True, None)
if not isinstance(spec, str):
spec = str(spec)
......@@ -398,13 +404,13 @@ def match_range(value, spec):
return (True, (start, end, invert))
if spec is None or spec.lower() == 'none':
if spec is None or str(spec).lower() == 'none':
return (True, True)
success, result = parse_range(spec)
if not success:
return (success, result)
start, end, invert = result
if isinstance(value, str) or isinstance(value, unicode):
if isinstance(value, str) or isinstance(value, bytes):
value = float(value.replace('%', ''))
if value < start:
return (True, False ^ invert)
......@@ -547,7 +553,10 @@ def seconds2human(seconds, keep_short=True, full_name=False):
'2weeks 2days 1hour 29minutes 35seconds'
"""
seconds = int(seconds)
seconds = float(seconds)
if seconds < 1:
return f'{seconds:.2f}s'
if full_name:
intervals = (
('years', 60*60*24*365),
......@@ -690,7 +699,7 @@ def sort(array, reverse=True, sort_by_key=False):
if isinstance(array, dict):
if not sort_by_key:
return sorted(array.items(), key=lambda x: x[1], reverse=reverse)
return sorted(array.items(), key=lambda x: x[0].lower(), reverse=reverse)
return sorted(array.items(), key=lambda x: str(x[0]).lower(), reverse=reverse)
return array
......@@ -738,6 +747,28 @@ def sum_lod(mylist):
return total
def str2state(string):
"""Return the state based on a string.
>> lib.base.str2state('ok')
0
>>> lib.base.str2state('warn')
1
>>> lib.base.str2state('warning')
1
"""
string = str(string).lower()
if string == 'ok':
return STATE_OK
if string.startswith('warn'):
return STATE_WARN
if string.startswith('crit'):
return STATE_CRIT
if string.startswith('unk'):
return STATE_UNKNOWN
def state2str(state, empty_ok=True, prefix='', suffix=''):
"""Return the state's string representation.
The square brackets around the state cause icingaweb2 to color the state.
......
......@@ -18,6 +18,7 @@ import json
import re
import ssl
import urllib
import urllib.request
def fetch(url, insecure=False, no_proxy=False, timeout=5,
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment