Refactoring: Moved check parameters from unsorted.py to dedicated modules (CMK-1393)
[check_mk.git] / checks / k8s_resources
blob47c482afcafce244e996f6c7520caf5ede144bad
1 #!/usr/bin/python
2 # -*- encoding: utf-8; py-indent-offset: 4 -*-
3 # +------------------------------------------------------------------+
4 # | ____ _ _ __ __ _ __ |
5 # | / ___| |__ ___ ___| | __ | \/ | |/ / |
6 # | | | | '_ \ / _ \/ __| |/ / | |\/| | ' / |
7 # | | |___| | | | __/ (__| < | | | | . \ |
8 # | \____|_| |_|\___|\___|_|\_\___|_| |_|_|\_\ |
9 # | |
10 # | Copyright Mathias Kettner 2018 mk@mathias-kettner.de |
11 # +------------------------------------------------------------------+
13 # This file is part of Check_MK.
14 # The official homepage is at http://mathias-kettner.de/check_mk.
16 # check_mk is free software; you can redistribute it and/or modify it
17 # under the terms of the GNU General Public License as published by
18 # the Free Software Foundation in version 2. check_mk is distributed
19 # in the hope that it will be useful, but WITHOUT ANY WARRANTY; with-
20 # out even the implied warranty of MERCHANTABILITY or FITNESS FOR A
21 # PARTICULAR PURPOSE. See the GNU General Public License for more de-
22 # tails. You should have received a copy of the GNU General Public
23 # License along with GNU Make; see the file COPYING. If not, write
24 # to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
25 # Boston, MA 02110-1301 USA.
28 def get_k8s_resources_inventory_function(name):
29 def inventory_function(parsed):
30 if parsed.get('capacity', {}).get(name):
31 return [(None, {})]
33 return inventory_function
36 def get_k8s_resources_check_function(name, default, readable):
37 def check_resources(_no_item, params, parsed):
38 request = parsed.get('requests', {}).get(name, default)
39 yield 0, 'Request: %s' % readable(request), [('k8s_%s_request' % name, request)]
41 limit = parsed.get('limits', {}).get(name)
42 if limit:
43 if math.isinf(limit):
44 yield 0, 'Limit: n.a.'
45 elif limit:
46 yield 0, 'Limit: %s' % readable(limit), [('k8s_%s_limit' % name, limit)]
48 allocatable = parsed.get('allocatable', {}).get(name, default)
49 yield 0, 'Allocatable: %s' % readable(allocatable), [('k8s_%s_allocatable' % name,
50 allocatable)]
52 capacity = parsed.get('capacity', {}).get(name, default)
53 yield 0, 'Capacity: %s' % readable(capacity), [('k8s_%s_capacity' % name, capacity)]
55 if allocatable:
56 usage = 100.0 * request / allocatable
57 yield check_levels(
58 usage,
59 'k8s_%s_usage' % name,
60 params.get(name),
61 infoname='Usage',
62 human_readable_func=get_percent_human_readable)
64 return check_resources
67 check_info['k8s_resources'] = {
68 'parse_function': parse_k8s,
69 'includes': ['k8s.include'],
72 check_info['k8s_resources.pods'] = {
73 'inventory_function': get_k8s_resources_inventory_function('pods'),
74 'check_function': get_k8s_resources_check_function('pods', 0, str),
75 'service_description': 'Pod resources',
76 'has_perfdata': True,
77 'group': 'k8s_resources',
80 check_info['k8s_resources.cpu'] = {
81 'inventory_function': get_k8s_resources_inventory_function('cpu'),
82 'check_function': get_k8s_resources_check_function('cpu', 0.0, lambda x: '%.3f' % x),
83 'service_description': 'CPU resources',
84 'has_perfdata': True,
85 'group': 'k8s_resources',
88 check_info['k8s_resources.memory'] = {
89 'inventory_function': get_k8s_resources_inventory_function('memory'),
90 'check_function': get_k8s_resources_check_function('memory', 0.0, get_bytes_human_readable),
91 'service_description': 'Memory resources',
92 'has_perfdata': True,
93 'group': 'k8s_resources',