GUI CSS: Deployed view styles for layouts (CMK-1171)
[check_mk.git] / checks / k8s_stats
blob65a42e76c38acc1a2967935566e85f1812d7052c
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 discover_k8s_stats_network(parsed):
29 # don't use the k8s check if the check_mk_agent delivers interface data
30 parsed, lnx_info = parsed
31 if lnx_info:
32 return
34 for key, value in parsed.iteritems():
35 if key != 'network':
36 continue
38 interfaces = {i.get('name') for i in value['interfaces']}
39 for interface in interfaces:
40 yield interface, {}
43 def discover_k8s_stats_fs(parsed):
44 parsed, _ = parsed
45 ignore_fs = ['tmpfs', 'rootfs']
47 for key, value in parsed.iteritems():
48 if key != 'filesystem':
49 continue
51 devices = {disk.get('device') for disk in value}
52 for device in devices:
53 if device not in ignore_fs:
54 yield device, {}
57 def k8s_network_err_pac(device, params, now):
58 for name, mway, pway in [('Input', 'rx', 'in'), ('Output', 'tx', 'out')]:
59 pac_rate = get_rate('if.%s.if_%s_pkts' % (device['name'], pway), now,
60 device['%s_packets' % mway])
61 err_rate = get_rate('if.%s.if_%s_errors' % (device['name'], pway), now,
62 device['%s_errors' % mway])
63 warn, crit = params.get('errors', (None, None))
64 perf_data = [('if_%s_pkts' % pway, pac_rate), ('if_%s_errors' % pway, err_rate)]
65 if isinstance(warn, float):
66 try:
67 err_perc = 100.0 * err_rate / (err_rate + pac_rate)
68 except ZeroDivisionError:
69 err_perc = 0
71 status, infotext, _perf = check_levels(
72 err_perc,
73 'err_perc', (warn, crit),
74 human_readable_func=get_percent_human_readable,
75 infoname="%s errors" % name)
76 else: # absolute levels or no levels
77 status, infotext, _perf = check_levels(
78 err_rate, 'if_errors', (warn, crit), unit='1/s', infoname="%s error rate" % name)
80 yield status, infotext, perf_data
83 def check_k8s_stats_network(item, params, parsed):
84 metrics, _ = parsed
86 device = sum([
87 collections.Counter(device)
88 for device in metrics['network']['interfaces']
89 if device.get('name') == item
90 ], collections.Counter())
92 now = metrics['timestamp']
94 # Bandwidth
95 for name, met, dsname in [('In', 'rx_bytes', 'in'), ('Out', 'tx_bytes', 'out')]:
96 rate = get_rate('if.%s.%s' % (item, dsname), now, device[met])
97 yield check_levels(
98 rate,
99 dsname,
100 None,
101 unit='/s',
102 human_readable_func=get_bytes_human_readable,
103 infoname=name)
105 # Errors / Packets
106 for check_result in k8s_network_err_pac(device, params, now):
107 yield check_result
109 # Discards
110 for name, met, dsname in [
111 ('Input Discards', 'rx_dropped', 'if_in_discards'),
112 ('Output Discards', 'tx_dropped', 'if_out_discards'),
114 rate = get_rate('if.%s.%s' % (item, dsname), now, device[met])
115 yield check_levels(rate, dsname, params.get('discards'), unit='1/s', infoname=name)
118 def check_k8s_stats_fs(item, params, parsed):
119 metrics, _ = parsed
121 disk = sum([
122 collections.Counter(device)
123 for device in metrics['filesystem']
124 if device.get('device') == item
125 ], collections.Counter())
127 yield df_check_filesystem_single(
128 item,
129 disk['capacity'] / 1024.0 / 1024.0,
130 disk['available'] / 1024.0 / 1024.0,
132 disk['inodes'],
133 disk['inodes_free'],
134 params,
138 check_info['k8s_stats'] = {
139 'parse_function': parse_k8s,
140 'extra_sections': ['lnx_if'],
143 check_info['k8s_stats.network'] = {
144 'inventory_function': discover_k8s_stats_network,
145 'check_function': check_k8s_stats_network,
146 'service_description': 'Interface %s',
147 "has_perfdata": True,
148 "group": "k8s_if",
149 'includes': ['k8s.include'],
152 check_info['k8s_stats.fs'] = {
153 'inventory_function': discover_k8s_stats_fs,
154 'check_function': check_k8s_stats_fs,
155 'service_description': 'Filesystem %s',
156 "has_perfdata": True,
157 "group": "filesystem",
158 "default_levels_variable": "filesystem_default_levels",
159 'includes': ['k8s.include', 'size_trend.include', 'df.include'],