Module: check_mk
Branch: master
Commit: 055e0db575631bb01edec2ec7ec2e2661ae9976e
URL:
http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=055e0db575631b…
Author: Tom Baerwinkel <tb(a)mathias-kettner.de>
Date: Wed Dec 5 17:45:22 2018 +0100
k8s_resources: unify the output of the special agent and the check_functions
CMK-511
Change-Id: Iff8457626cccd4efcba699f1af98b1b3b2df42ff
---
agents/special/agent_kubernetes | 13 ++-
checks/k8s_resources | 110 ++++++---------------
cmk/gui/plugins/metrics/check_mk.py | 4 +-
.../datasets/k8s_resources_regression.py | 2 +-
4 files changed, 45 insertions(+), 84 deletions(-)
diff --git a/agents/special/agent_kubernetes b/agents/special/agent_kubernetes
index 919710d..9d129c4 100755
--- a/agents/special/agent_kubernetes
+++ b/agents/special/agent_kubernetes
@@ -402,10 +402,10 @@ class PodList(ListLike[Pod]):
# type: () -> Dict[str, Dict[str, Dict[str, int]]]
pods_sorted = sorted(self, key=lambda pod: pod.node)
by_node = itertools.groupby(pods_sorted, lambda pod: pod.node)
- return {node: {'allocations': {'pods': len(list(pods))}} for
node, pods in by_node}
+ return {node: {'requests': {'pods': len(list(pods))}} for node,
pods in by_node}
def pods_in_cluster(self):
- return {'allocations': {'pods': len(self)}}
+ return {'requests': {'pods': len(self)}}
def resources_per_node(self):
# type: () -> Dict[str, Dict[str, Dict[str, float]]]
@@ -561,7 +561,14 @@ class Section(object):
def insert(self, data):
# type: (Dict[str, Any]) -> None
- self.content.update(data)
+ for key, value in data.iteritems():
+ if key not in self.content:
+ self.content[key] = value
+ else:
+ if isinstance(value, dict):
+ self.content[key].update(value)
+ else:
+ raise ValueError('Key %s is already present and cannot be
merged' % key)
def output(self):
# type: () -> str
diff --git a/checks/k8s_resources b/checks/k8s_resources
index d673380..47c482a 100644
--- a/checks/k8s_resources
+++ b/checks/k8s_resources
@@ -33,107 +33,61 @@ def get_k8s_resources_inventory_function(name):
return inventory_function
-check_info['k8s_resources'] = {
- 'parse_function': parse_k8s,
- 'includes': ['k8s.include'],
-}
+def get_k8s_resources_check_function(name, default, readable):
+ def check_resources(_no_item, params, parsed):
+ request = parsed.get('requests', {}).get(name, default)
+ yield 0, 'Request: %s' % readable(request), [('k8s_%s_request' %
name, request)]
+
+ limit = parsed.get('limits', {}).get(name)
+ if limit:
+ if math.isinf(limit):
+ yield 0, 'Limit: n.a.'
+ elif limit:
+ yield 0, 'Limit: %s' % readable(limit), [('k8s_%s_limit'
% name, limit)]
+ allocatable = parsed.get('allocatable', {}).get(name, default)
+ yield 0, 'Allocatable: %s' % readable(allocatable),
[('k8s_%s_allocatable' % name,
+ allocatable)]
-def check_k8s_pod_resources(_no_item, params, parsed):
- pods = parsed.get('allocations', {}).get('pods', 0)
- yield 0, 'Pods: %d' % pods, [('k8s_pods', pods)]
+ capacity = parsed.get('capacity', {}).get(name, default)
+ yield 0, 'Capacity: %s' % readable(capacity), [('k8s_%s_capacity'
% name, capacity)]
- allocatable = parsed.get('allocatable', {}).get('pods', 0)
- yield 0, 'Allocatable: %d' % allocatable, [('k8s_pods_allocatable',
allocatable)]
+ if allocatable:
+ usage = 100.0 * request / allocatable
+ yield check_levels(
+ usage,
+ 'k8s_%s_usage' % name,
+ params.get(name),
+ infoname='Usage',
+ human_readable_func=get_percent_human_readable)
- capacity = parsed.get('capacity', {}).get('pods', 0)
- yield 0, 'Capacity: %d' % capacity, [('k8s_pods_capacity',
capacity)]
+ return check_resources
- if allocatable:
- usage = 100.0 * pods / allocatable
- yield check_levels(
- usage,
- 'k8s_pods_usage',
- params.get('pods'),
- infoname='Usage',
- human_readable_func=get_percent_human_readable)
+check_info['k8s_resources'] = {
+ 'parse_function': parse_k8s,
+ 'includes': ['k8s.include'],
+}
check_info['k8s_resources.pods'] = {
'inventory_function': get_k8s_resources_inventory_function('pods'),
- 'check_function': check_k8s_pod_resources,
+ 'check_function': get_k8s_resources_check_function('pods', 0, str),
'service_description': 'Pod resources',
'has_perfdata': True,
'group': 'k8s_resources',
}
-
-def check_k8s_cpu_resources(_no_item, params, parsed):
- request = parsed.get('requests', {}).get('cpu', 0.0)
- yield 0, 'Request: %.3f' % request, [('k8s_cpu_request', request)]
-
- limit = parsed.get('limits', {}).get('cpu')
- if math.isinf(limit):
- yield 0, 'Limit: n.a.'
- else:
- yield 0, 'Limit: %.3f' % limit, [('k8s_cpu_limit', limit)]
-
- allocatable = parsed.get('allocatable', {}).get('cpu', 0.0)
- yield 0, 'Allocatable: %.3f' % allocatable, [('k8s_cpu_allocatable',
allocatable)]
-
- capacity = parsed.get('capacity', {}).get('cpu', 0.0)
- yield 0, 'Capacity: %.3f' % capacity, [('k8s_cpu_capacity',
capacity)]
-
- if allocatable:
- usage = 100.0 * request / allocatable
- yield check_levels(
- usage,
- 'k8s_cpu_usage',
- params.get('cpu'),
- infoname='Usage',
- human_readable_func=get_percent_human_readable)
-
-
check_info['k8s_resources.cpu'] = {
'inventory_function': get_k8s_resources_inventory_function('cpu'),
- 'check_function': check_k8s_cpu_resources,
+ 'check_function': get_k8s_resources_check_function('cpu', 0.0, lambda
x: '%.3f' % x),
'service_description': 'CPU resources',
'has_perfdata': True,
'group': 'k8s_resources',
}
-
-def check_k8s_mem_resources(_no_item, params, parsed):
- request = parsed.get('requests', {}).get('memory', 0.0)
- yield 0, 'Request: %s' % get_bytes_human_readable(request),
[('k8s_memory_request', request)]
-
- limit = parsed.get('limits', {}).get('memory')
- if math.isinf(limit):
- yield 0, 'Limit: n.a.'
- else:
- yield 0, 'Limit: %s' % get_bytes_human_readable(limit),
[('k8s_memory_limit', limit)]
-
- allocatable = parsed.get('allocatable', {}).get('memory', 0.0)
- yield 0, 'Allocatable: %s' % get_bytes_human_readable(allocatable),
[('k8s_memory_allocatable',
- allocatable)]
-
- capacity = parsed.get('capacity', {}).get('memory', 0.0)
- yield 0, 'Capacity: %s' % get_bytes_human_readable(capacity),
[('k8s_memory_capacity',
- capacity)]
-
- if allocatable:
- usage = 100.0 * request / allocatable
- yield check_levels(
- usage,
- 'k8s_memory_usage',
- params.get('memory'),
- infoname='Usage',
- human_readable_func=get_percent_human_readable)
-
-
check_info['k8s_resources.memory'] = {
'inventory_function':
get_k8s_resources_inventory_function('memory'),
- 'check_function': check_k8s_mem_resources,
+ 'check_function': get_k8s_resources_check_function('memory', 0.0,
get_bytes_human_readable),
'service_description': 'Memory resources',
'has_perfdata': True,
'group': 'k8s_resources',
diff --git a/cmk/gui/plugins/metrics/check_mk.py b/cmk/gui/plugins/metrics/check_mk.py
index ed3976f..9bd0276 100644
--- a/cmk/gui/plugins/metrics/check_mk.py
+++ b/cmk/gui/plugins/metrics/check_mk.py
@@ -4370,7 +4370,7 @@ metric_info["k8s_nodes"] = {
"color": "11/a",
}
-metric_info["k8s_pods"] = {
+metric_info["k8s_pods_request"] = {
"title": _("Pods"),
"unit": "count",
"color": "16/b",
@@ -8152,7 +8152,7 @@ graph_info["k8s_resources.pods"] = {
"metrics": [
("k8s_pods_capacity", "area"),
("k8s_pods_allocatable", "area"),
- ("k8s_pods", "area"),
+ ("k8s_pods_request", "area"),
],
}
diff --git a/tests/unit/checks/generictests/datasets/k8s_resources_regression.py
b/tests/unit/checks/generictests/datasets/k8s_resources_regression.py
index c9805f4..7b3bceb 100644
--- a/tests/unit/checks/generictests/datasets/k8s_resources_regression.py
+++ b/tests/unit/checks/generictests/datasets/k8s_resources_regression.py
@@ -26,7 +26,7 @@ checks = {
(0, 'Usage: 0.0%', [('k8s_memory_usage', 0.0)]),
])],
'pods': [(None, 'default', [
- (0, 'Pods: 0', [('k8s_pods', 0)]),
+ (0, 'Request: 0', [('k8s_pods_request', 0)]),
(0, 'Allocatable: 110', [('k8s_pods_allocatable', 110)]),
(0, 'Capacity: 110', [('k8s_pods_capacity', 110)]),
(0, 'Usage: 0.0%', [('k8s_pods_usage', 0.0)]),