GUI CSS: Removed snapin styles from py modules and added a _snapins.scss for the...
[check_mk.git] / checks / k8s_replicas
blob5b41f95a878300e514d8d4e5cbab85233ea81599
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 parse_k8s_surge(value, total):
29 """
30 Returns the upper level for replicas which is considered critical
31 (hence the +1 in the return value). Values may be given as an
32 absolute number or in percent.
33 """
34 if isinstance(value, int):
35 return value + total + 1
36 percentage = 1.0 + float(value.rstrip('%')) / 100.0
37 return math.ceil(percentage * total) + 1
40 def parse_k8s_unavailability(value, total):
41 """
42 Returns the lower level for replicas which is still considered ok.
43 Values may be given as an absolute number or in percent.
44 """
45 if isinstance(value, int):
46 return total - value
47 percentage = 1.0 - float(value.rstrip('%')) / 100.0
48 return math.floor(percentage * total)
51 def inventory_k8s_replicas(parsed):
52 if parsed['ready_replicas'] is not None and parsed['replicas'] is not None:
53 return [(None, {})]
56 def check_k8s_replicas(_no_item, params, parsed):
57 ready, total = parsed['ready_replicas'], parsed['replicas']
58 paused, strategy = parsed['paused'], parsed['strategy_type']
60 if paused or strategy == 'Recreate':
61 crit, crit_lower = None, None
62 elif strategy == 'RollingUpdate':
63 crit = parse_k8s_surge(parsed['max_surge'], total)
64 crit_lower = parse_k8s_unavailability(parsed['max_unavailable'], total)
65 else:
66 yield 3, "Unknown deployment strategy: %s" % strategy
67 return
69 state = 0
70 infotext = "Ready: %s/%s" % (ready, total)
71 if paused:
72 infotext += ' (paused)'
73 if crit is not None and ready >= crit:
74 state = 2
75 infotext += " (crit at %d)" % crit
76 if crit_lower is not None and ready < crit_lower:
77 state = 2
78 infotext += " (crit below %d)" % crit_lower
80 perfdata = [
81 ('ready_replicas', ready, None, crit),
82 ('total_replicas', total),
84 yield state, infotext, perfdata
86 if strategy:
87 strategy_infotext = "Strategy: %s" % parsed['strategy_type']
88 if strategy == 'RollingUpdate':
89 strategy_infotext += " (max unavailable: %s, max surge: %s)" % (
90 parsed['max_unavailable'],
91 parsed['max_surge'],
93 yield 0, strategy_infotext
96 check_info['k8s_replicas'] = {
97 'parse_function': parse_k8s,
98 'inventory_function': inventory_k8s_replicas,
99 'check_function': check_k8s_replicas,
100 'service_description': 'Replicas',
101 'has_perfdata': True,
102 'includes': ['k8s.include'],