Refactoring: Changed all check parameters starting with an 'o' to the new rulespec...
[check_mk.git] / checks / j4p_performance
blobb923f359f4d1923266caec4bb4ba7d302594fc70
1 #!/usr/bin/python
2 # -*- encoding: utf-8; py-indent-offset: 4 -*-
3 # +------------------------------------------------------------------+
4 # | ____ _ _ __ __ _ __ |
5 # | / ___| |__ ___ ___| | __ | \/ | |/ / |
6 # | | | | '_ \ / _ \/ __| |/ / | |\/| | ' / |
7 # | | |___| | | | __/ (__| < | | | | . \ |
8 # | \____|_| |_|\___|\___|_|\_\___|_| |_|_|\_\ |
9 # | |
10 # | Copyright Mathias Kettner 2014 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.
27 # WARNING: These checks are deprecated and will be removed soon.
28 # Please use jolokia_* instead
30 # MB warn, crit
31 j4p_performance_mem_default_levels = (1000, 2000)
32 # Number of threads warn, crit
33 j4p_performance_threads_default_levels = (80, 100)
34 # Number of sessions low crit, low warn, high warn, high crit
35 j4p_performance_app_sess_default_levels = (-1, -1, 800, 1000)
36 # Number of requests low crit, low warn, high warn, high crit
37 j4p_performance_serv_req_default_levels = (-1, -1, 5000, 6000)
40 def j4p_performance_parse(info):
41 parsed = {}
42 for inst, var, value in info:
43 app, servlet = None, None
44 if ',' in inst:
45 parts = inst.split(',')
46 if len(parts) == 3:
47 inst, app, servlet = parts
48 else:
49 inst, app = parts
51 parsed.setdefault(inst, {})
52 if servlet:
53 parsed[inst].setdefault('apps', {})
54 parsed[inst]['apps'][app].setdefault('servlets', {})
55 parsed[inst]['apps'][app]['servlets'].setdefault(servlet, {})
56 parsed[inst]['apps'][app]['servlets'][servlet][var] = value
57 elif app:
58 parsed[inst].setdefault('apps', {})
59 parsed[inst]['apps'].setdefault(app, {})
60 parsed[inst]['apps'][app][var] = value
61 else:
62 parsed[inst][var] = value
63 return parsed
66 def j4p_performance_app(info, split_item):
67 inst, app = split_item
68 parsed = j4p_performance_parse(info)
69 if not inst in parsed \
70 or not app in parsed[inst].get('apps', {}):
71 return None
72 return parsed[inst]['apps'][app]
75 def j4p_performance_serv(info, split_item):
76 inst, app, serv = split_item
77 app = j4p_performance_app(info, (inst, app))
78 if not app or not serv in app.get('servlets', {}):
79 return None
80 return app['servlets'][serv]
83 def inventory_j4p_performance(info, what):
84 parsed = j4p_performance_parse(info)
85 levels = None
86 if what == 'mem':
87 levels = 'j4p_performance_mem_default_levels'
88 elif what == 'threads':
89 levels = 'j4p_performance_threads_default_levels'
90 return [(k, levels) for k in parsed]
93 def inventory_j4p_performance_apps(info, what):
94 inv = []
95 parsed = j4p_performance_parse(info)
96 levels = None
97 if what == 'app_sess':
98 levels = 'j4p_performance_app_sess_default_levels'
99 for inst, vals in parsed.iteritems():
100 for app in vals.get('apps', {}).keys():
101 inv.append(('%s %s' % (inst, app), levels))
102 return inv
105 def inventory_j4p_performance_serv(info, what):
106 inv = []
107 parsed = j4p_performance_parse(info)
108 levels = None
109 if what == 'serv_req':
110 levels = 'j4p_performance_serv_req_default_levels'
111 for inst, vals in parsed.iteritems():
112 for app, val in vals.get('apps', {}).iteritems():
113 for serv in val.get('servlets', {}).keys():
114 inv.append(('%s %s %s' % (inst, app, serv), levels))
115 return inv
118 def check_j4p_performance_mem(item, params, info):
119 warn, crit = params
120 parsed = j4p_performance_parse(info)
121 if item not in parsed:
122 return (3, "data not found in agent output")
123 d = parsed[item]
124 mb = 1024 * 1024.0
125 heap = saveint(d["HeapMemoryUsage"]) / mb
126 non_heap = saveint(d["NonHeapMemoryUsage"]) / mb
127 total = heap + non_heap
128 perfdata = [
129 ("heap", heap, warn, crit),
130 ("nonheap", non_heap, warn, crit),
132 infotext = "%.0f MB total (%.0f MB heap, %.0f MB non-heap) (warn/crit at %.0f/%.0f)" % (
133 total, heap, non_heap, warn, crit)
134 if total >= crit:
135 return (2, infotext, perfdata)
136 elif total >= warn:
137 return (1, infotext, perfdata)
138 return (0, infotext, perfdata)
141 check_info["j4p_performance.mem"] = {
142 'check_function': check_j4p_performance_mem,
143 'default_levels_variable': None,
144 'group': 'j4p_performance.mem',
145 'has_perfdata': True,
146 'inventory_function': lambda i: inventory_j4p_performance(i, "mem"),
147 'node_info': False,
148 'service_description': 'JMX %s Memory',
149 'snmp_info': None,
150 'snmp_scan_function': None
154 def check_j4p_performance_threads(item, params, info):
155 warn, crit = params
156 parsed = j4p_performance_parse(info)
157 if item not in parsed:
158 return (3, "data not found in agent output")
159 d = parsed[item]
161 this_time = time.time()
162 perfdata = []
163 output = []
164 status = 0
165 for key in ['ThreadCount', 'DeamonThreadCount', 'PeakThreadCount', 'TotalStartedThreadCount']:
166 val = saveint(d[key])
167 if key == 'ThreadCount':
168 # Thread count might lead to a warn/crit state
169 if val >= crit:
170 status = 2
171 elif val >= warn:
172 status = 1
174 # Calculate the thread increase rate
175 rate = get_rate("j4p_performance.threads.%s" % item, this_time, val)
176 output.append('ThreadRate: %0.2f' % rate)
177 perfdata.append(('ThreadRate', rate))
179 perfdata.append((key, val))
180 output.append('%s: %d' % (key, val))
182 return (status, ', '.join(output), perfdata)
185 check_info["j4p_performance.threads"] = {
186 'check_function': check_j4p_performance_threads,
187 'default_levels_variable': None,
188 'group': 'j4p_performance.threads',
189 'has_perfdata': True,
190 'inventory_function': lambda i: inventory_j4p_performance(i, "threads"),
191 'node_info': False,
192 'service_description': 'JMX %s Threads',
193 'snmp_info': None,
194 'snmp_scan_function': None
198 def check_j4p_performance_uptime(item, _unused, info):
199 parsed = j4p_performance_parse(info)
200 if item not in parsed:
201 return (3, "data not found in agent output")
202 uptime = saveint(parsed[item]['Uptime']) / 1000
204 seconds = uptime % 60
205 rem = uptime / 60
206 minutes = rem % 60
207 hours = (rem % 1440) / 60
208 days = rem / 1440
209 now = int(time.time())
210 since = time.strftime("%c", time.localtime(now - uptime))
211 return (0, "up since %s (%dd %02d:%02d:%02d)" % (since, days, hours, minutes, seconds),
212 [("uptime", uptime)])
215 check_info["j4p_performance.uptime"] = {
216 'check_function': check_j4p_performance_uptime,
217 'default_levels_variable': None,
218 'group': 'j4p_performance.uptime',
219 'has_perfdata': True,
220 'inventory_function': lambda i: inventory_j4p_performance(i, "uptime"),
221 'node_info': False,
222 'service_description': 'JMX %s Uptime',
223 'snmp_info': None,
224 'snmp_scan_function': None
228 def check_j4p_performance_app_state(item, _unused, info):
229 app = j4p_performance_app(info, item.split())
230 if not app or not 'Running' in app:
231 return (3, "data not found in agent output")
233 if app['Running'] == '1':
234 return (0, 'application is running')
235 return (2, 'application is not running (Running: %s)')
238 check_info["j4p_performance.app_state"] = {
239 'check_function': check_j4p_performance_app_state,
240 'default_levels_variable': None,
241 'group': 'j4p_performance.app_state',
242 'inventory_function': lambda i: inventory_j4p_performance_apps(i, "app_state"),
243 'node_info': False,
244 'service_description': 'JMX %s State',
245 'snmp_info': None,
246 'snmp_scan_function': None
250 def check_j4p_performance_app_sess(item, params, info):
251 lo_crit, lo_warn, hi_warn, hi_crit = params
252 app = j4p_performance_app(info, item.split())
253 if not app or not 'Sessions' in app:
254 return (3, "data not found in agent output")
255 sess = saveint(app['Sessions'])
257 status = 0
258 status_txt = ''
259 if lo_crit is not None and sess <= lo_crit:
260 status = 2
261 status_txt = ' (Below or equal %d)' % lo_crit
262 elif lo_warn is not None and sess <= lo_warn:
263 status = 1
264 status_txt = ' (Below or equal %d)' % lo_warn
265 elif hi_crit is not None and sess >= hi_crit:
266 status = 2
267 status_txt = ' (Above or equal %d)' % lo_warn
268 elif hi_warn is not None and sess >= hi_warn:
269 status = 1
270 status_txt = ' (Above or equal %d)' % lo_crit
272 return (
273 status,
274 '%d Sessions%s' % (sess, status_txt),
275 [('sessions', sess, hi_warn, hi_crit)],
279 check_info["j4p_performance.app_sess"] = {
280 'check_function': check_j4p_performance_app_sess,
281 'default_levels_variable': None,
282 'group': 'j4p_performance.app_sess',
283 'has_perfdata': True,
284 'inventory_function': lambda i: inventory_j4p_performance_apps(i, "app_sess"),
285 'node_info': False,
286 'service_description': 'JMX %s Sessions',
287 'snmp_info': None,
288 'snmp_scan_function': None
292 def check_j4p_performance_serv_req(item, params, info):
293 lo_crit, lo_warn, hi_warn, hi_crit = params
294 serv = j4p_performance_serv(info, item.split())
295 if not serv or not 'Requests' in serv:
296 return (3, "data not found in agent output")
297 req = saveint(serv['Requests'])
299 status = 0
300 status_txt = ''
301 if lo_crit is not None and req <= lo_crit:
302 status = 2
303 status_txt = ' (Below or equal %d)' % lo_crit
304 elif lo_warn is not None and req <= lo_warn:
305 status = 1
306 status_txt = ' (Below or equal %d)' % lo_warn
307 elif hi_crit is not None and req >= hi_crit:
308 status = 2
309 status_txt = ' (Above or equal %d)' % lo_warn
310 elif hi_warn is not None and req >= hi_warn:
311 status = 1
312 status_txt = ' (Above or equal %d)' % lo_crit
314 output = ['Requests: %d%s' % (req, status_txt)]
315 perfdata = [('Requests', req, hi_warn, hi_crit)]
316 this_time = time.time()
317 rate = get_rate("j4p_performance.serv_req.%s" % item, this_time, req)
318 output.append('RequestRate: %0.2f' % rate)
319 perfdata.append(('RequestRate', rate))
320 return (status, ', '.join(output), perfdata)
323 check_info["j4p_performance.serv_req"] = {
324 'check_function': check_j4p_performance_serv_req,
325 'default_levels_variable': None,
326 'group': 'j4p_performance.serv_req',
327 'has_perfdata': True,
328 'inventory_function': lambda i: inventory_j4p_performance_serv(i, "serv_req"),
329 'node_info': False,
330 'service_description': 'JMX %s Requests',
331 'snmp_info': None,
332 'snmp_scan_function': None