Refactoring: Changed all check parameters starting with an 'o' to the new rulespec...
[check_mk.git] / tests / unit / checks / test_ps.py
blobf7a1ad6b6616d40ba4c612faa752b942220091df
1 from itertools import izip_longest
2 from collections import namedtuple
3 import pytest
4 from cmk_base.check_api import MKGeneralException
5 from checktestlib import CheckResult, assertCheckResultsEqual
7 pytestmark = pytest.mark.checks
10 def splitter(text, split_symbol=None, node=None):
11 return [[node] + line.split(split_symbol) for line in text.split("\n")]
14 def generate_inputs():
15 return [
16 # CMK 1.5
17 # linux, openwrt agent(5 entry, cmk>=1.2.7)
18 splitter("""(root,225948,9684,00:00:03/05:05:29,1) /sbin/init splash
19 (root,0,0,00:00:00/05:05:29,2) [kthreadd]
20 (on,288260,7240,00:00:00/05:03:00,4480) /usr/bin/gnome-keyring-daemon --start --foreground --components=secrets
21 (on,1039012,11656,00:00:00/05:02:41,5043) /usr/bin/pulseaudio --start --log-target=syslog
22 (on,1050360,303252,00:14:59/1-03:59:39,9902) emacs
23 (on,2924232,472252,00:12:05/07:24:15,7912) /usr/lib/firefox/firefox"""),
24 # solaris (5 entry cmk>=1.5)
25 splitter(
26 """(root,4056,1512,0.0/52-04:56:05,5689) /usr/lib/ssh/sshd
27 (zombie,0,0,-/-,1952) <defunct>
28 (zombie,0,0,-/-,3952)
29 (zombie,0,0,-/-,4952) """,
30 node="solaris"),
31 # windows agent
32 splitter(
33 """(SYSTEM,0,0,0,0,0,0,0,0,1,0) System Idle Process
34 (\\NT AUTHORITY\SYSTEM,46640,10680,0,600,5212,27924179,58500375,370,11,12) svchost.exe
35 (\\NT AUTHORITY\NETWORK SERVICE,36792,10040,0,676,5588,492183155,189541215,380,8,50) svchost.exe
36 (\\NT AUTHORITY\LOCAL SERVICE,56100,18796,0,764,56632,1422261117,618855967,454,13,4300) svchost.exe
37 (\\KLAPPRECHNER\ab,29284,2948,0,3124,904,400576,901296,35,1,642)\tNOTEPAD.EXE""", "\t"),
38 # aix, bsd, hpux, macos, netbsd, openbsd agent(4 entry, cmk>=1.1.5)
39 splitter("(db2prtl,17176,17540,0.0) /usr/lib/ssh/sshd", node="bsd"),
40 # aix with zombies
41 splitter("""(oracle,9588,298788,0.0) ora_dmon_uc4prd
42 (<defunct>,,,)
43 (oracle,11448,300648,0.0) oraclemetroprd (LOCAL=NO)"""),
44 # windows agent(10 entry, cmk>1.2.5)
45 splitter(
46 """(SYSTEM,0,0,0,0,0,0,0,0,2) System Idle Process
47 (\\KLAPPRECHNER\ab,29284,2948,0,3124,904,400576,901296,35,1)\tNOTEPAD.EXE""", "\t"),
48 # windows agent(wmic_info, cmk<1.2.5)# From server-windows-mssql-2
49 splitter("""[System Process]
50 System
51 System Idle Process
52 smss.exe
53 csrss.exe
54 csrss.exe""", "\0") + splitter(
55 """[wmic process]
56 Node,HandleCount,KernelModeTime,Name,PageFileUsage,ProcessId,ThreadCount,UserModeTime,VirtualSize,WorkingSetSize
57 WSOPREKPFS01,0,388621186093750,System Idle Process,0,0,24,0,65536,24576
58 WSOPREKPFS01,1227,368895625000,System,132,4,273,0,14831616,10862592
59 WSOPREKPFS01,53,2031250,smss.exe,360,520,2,156250,4685824,323584
60 WSOPREKPFS01,679,10051718750,csrss.exe,2640,680,10,2222031250,70144000,2916352
61 WSOPREKPFS01,85,126562500,csrss.exe,1176,744,8,468750,44486656,569344
62 [wmic process end]""", ","),
63 # Second Generation
64 splitter(
65 "(root) /usr/sbin/xinetd -pidfile /var/run/xinetd.pid -stayalive -inetd_compat -inetd_ipv6"
67 # First Generation
68 splitter(
69 "/usr/sbin/xinetd -pidfile /var/run/xinetd.pid -stayalive -inetd_compat -inetd_ipv6"),
73 result_parse = [
74 (1,
75 [[None, ("root", "225948", "9684", "00:00:03/05:05:29", "1"), "/sbin/init", "splash"],
76 [None, ("root", "0", "0", "00:00:00/05:05:29", "2"), "[kthreadd]"],
78 None, ("on", "288260", "7240", "00:00:00/05:03:00", "4480"),
79 "/usr/bin/gnome-keyring-daemon", "--start", "--foreground", "--components=secrets"
82 None, ("on", "1039012", "11656", "00:00:00/05:02:41", "5043"), "/usr/bin/pulseaudio",
83 "--start", "--log-target=syslog"
85 [None, ("on", "1050360", "303252", "00:14:59/1-03:59:39", "9902"),
86 "emacs"],
87 [None, ("on", "2924232", "472252", "00:12:05/07:24:15", "7912"),
88 "/usr/lib/firefox/firefox"]]),
89 (1, [["solaris", ("root", "4056", "1512", "0.0/52-04:56:05", "5689"), "/usr/lib/ssh/sshd"],
90 ["solaris", ("zombie", "0", "0", "-/-", "1952"), "<defunct>"]]),
91 (1,
92 [[None, ("SYSTEM", "0", "0", "0", "0", "0", "0", "0", "0", "1", "0"), "System Idle Process"],
94 None,
95 ("\\NT AUTHORITY\\SYSTEM", "46640", "10680", "0", "600", "5212", "27924179", "58500375",
96 "370", "11", "12"), "svchost.exe"
99 None,
100 ("\\NT AUTHORITY\\NETWORK SERVICE", "36792", "10040", "0", "676", "5588", "492183155",
101 "189541215", "380", "8", "50"), "svchost.exe"
104 None,
105 ("\\NT AUTHORITY\\LOCAL SERVICE", "56100", "18796", "0", "764", "56632", "1422261117",
106 "618855967", "454", "13", "4300"), "svchost.exe"
109 None,
110 ("\\KLAPPRECHNER\x07b", "29284", "2948", "0", "3124", "904", "400576", "901296", "35",
111 "1", "642"), "NOTEPAD.EXE"
112 ]]),
113 (1, [["bsd", ("db2prtl", "17176", "17540", "0.0"), "/usr/lib/ssh/sshd"]]),
114 (1, [
115 [None, ("oracle", "9588", "298788", "0.0"), "ora_dmon_uc4prd"],
116 [None, ("oracle", "11448", "300648", "0.0"), "oraclemetroprd", "(LOCAL=NO)"],
118 (2, [
119 [None, ("SYSTEM", "0", "0", "0", "0", "0", "0", "0", "0", "2"), "System Idle Process"],
121 None,
122 ("\\KLAPPRECHNER\x07b", "29284", "2948", "0", "3124", "904", "400576", "901296", "35",
123 "1"), "NOTEPAD.EXE"
126 (24, [[None, (None,), u"[System Process]"],
128 None,
129 ("unknown", "14484", "10608", "0", "4", "0", "0", "368895625000", "1227", "273", ""),
130 u"System"
133 None, ("unknown", "64", "24", "0", "0", "0", "0", "388621186093750", "0", "24", ""),
134 u"System Idle Process"
137 None, ("unknown", "4576", "316", "0", "520", "0", "156250", "2031250", "53", "2", ""),
138 u"smss.exe"
141 None,
142 ("unknown", "43444", "556", "0", "744", "1", "468750", "126562500", "85", "8", ""),
143 u"csrss.exe"
146 None,
147 ("unknown", "68500", "2848", "0", "680", "2", "2222031250", "10051718750", "679",
148 "10", ""), u"csrss.exe"
149 ]]),
150 (1, [[
151 None, ("root",), "/usr/sbin/xinetd", "-pidfile", "/var/run/xinetd.pid", "-stayalive",
152 "-inetd_compat", "-inetd_ipv6"
153 ]]),
154 (1, [[
155 None, (None,), "/usr/sbin/xinetd", "-pidfile", "/var/run/xinetd.pid", "-stayalive",
156 "-inetd_compat", "-inetd_ipv6"
157 ]]),
159 input_ids = [
160 "linux, openwrt agent(5 entry, cmk>=1.2.7)",
161 "solaris (5 entry cmk>=1.5)",
162 "windows agent(11 entry, cmk>=)",
163 "aix, bsd, hpux, macos, netbsd, openbsd, agent(4 entry, cmk>=1.1.5)",
164 "aix with zombies",
165 "windows agent(10 entry, cmk>1.2.5)",
166 "windows agent(wmic_info, cmk<1.2.5)",
167 "Second Generation user info only",
168 "First Generation process only",
172 @pytest.mark.parametrize("capture, result", zip(generate_inputs(), result_parse), ids=input_ids)
173 def test_parse_ps(check_manager, capture, result):
174 check = check_manager.get_check("ps")
176 parsed = check.run_parse(capture)
177 assert parsed[0] == result[0] # cpu_cores
178 for out, ref in izip_longest(parsed[1], result[1]):
179 assert out[0] == ref[0]
180 assert out[1] == check.context["ps_info"](*ref[1])
181 assert out[2:] == ref[2:]
184 PS_DISCOVERY_WATO_RULES = [
186 "default_params": {},
187 "descr": "smss",
188 "match": "~smss.exe"
189 }, [], ["@all"], {
190 "description": u"smss"
193 "default_params": {
194 "cpulevels": (90.0, 98.0),
195 "handle_count": (1000, 2000),
196 "levels": (1, 1, 99999, 99999),
197 "max_age": (3600, 7200),
198 "resident_levels": (104857600, 209715200),
199 "resident_levels_perc": (25.0, 50.0),
200 "single_cpulevels": (90.0, 98.0),
201 "virtual_levels": (1073741824000, 2147483648000),
203 "descr": "svchost",
204 "match": "svchost.exe"
205 }, [], ["@all"], {
206 "description": u"svchost win"
209 "default_params": {
210 "process_info": "text"
212 "match": "~.*(fire)fox",
213 "descr": "firefox is on %s",
214 "user": None,
215 }, [], ["@all"], {
216 "description": u"Firefox"
219 "default_params": {
220 "cpu_rescale_max": True,
221 "cpu_average": 15,
222 "process_info": "html",
223 "resident_levels_perc": (25.0, 50.0),
224 "virtual_levels": (1024**3, 2 * 1024**3),
225 "resident_levels": (1024**3, 2 * 1024**3),
226 "icon": "emacs.png",
228 "descr": "emacs %u",
229 "match": "emacs",
230 "user": False
231 }, [], ["@all"], {
232 "description": u"emacs",
235 "default_params": {
236 "max_age": (3600, 7200),
237 "resident_levels_perc": (25.0, 50.0),
238 "single_cpulevels": (90.0, 98.0),
239 "resident_levels": (104857600, 209715200),
241 "match": "~.*cron",
242 "descr": "cron",
243 "user": "root"
244 }, [], ["@all"], {
245 "description": u"cron"
248 "default_params": {},
249 "descr": "sshd",
250 "match": "~.*sshd"
251 }, [], ["@all"], {
252 "description": u"sshd"
255 "default_params": {},
256 "descr": "Term",
257 "match": "~.*term"
258 }, [], ["@all"], {
259 "disabled": True,
260 "description": u"sshd"
263 'default_params': {},
264 'descr': 'PS counter',
265 'user': 'zombie',
266 }, [], ["@all"], {}),
269 PS_DISCOVERY_SPECS = [
270 ("smss", "~smss.exe", None, {
271 'cpu_rescale_max': None
273 ("svchost", "svchost.exe", None, {
274 "cpulevels": (90.0, 98.0),
275 'cpu_rescale_max': None,
276 "handle_count": (1000, 2000),
277 "levels": (1, 1, 99999, 99999),
278 "max_age": (3600, 7200),
279 "resident_levels": (104857600, 209715200),
280 "resident_levels_perc": (25.0, 50.0),
281 "single_cpulevels": (90.0, 98.0),
282 "virtual_levels": (1073741824000, 2147483648000),
284 ("firefox is on %s", "~.*(fire)fox", None, {
285 "process_info": "text",
286 'cpu_rescale_max': None,
288 ("emacs %u", "emacs", False, {
289 "cpu_average": 15,
290 'cpu_rescale_max': True,
291 "process_info": "html",
292 "resident_levels_perc": (25.0, 50.0),
293 "virtual_levels": (1024**3, 2 * 1024**3),
294 "resident_levels": (1024**3, 2 * 1024**3),
295 "icon": "emacs.png",
297 ("cron", "~.*cron", "root", {
298 "max_age": (3600, 7200),
299 'cpu_rescale_max': None,
300 "resident_levels_perc": (25.0, 50.0),
301 "single_cpulevels": (90.0, 98.0),
302 "resident_levels": (104857600, 209715200)
304 ("sshd", "~.*sshd", None, {
305 'cpu_rescale_max': None
307 ('PS counter', None, 'zombie', {
308 'cpu_rescale_max': None
313 def test_wato_rules(check_manager):
314 check = check_manager.get_check("ps")
315 assert check.context["ps_wato_configured_inventory_rules"](
316 PS_DISCOVERY_WATO_RULES) == PS_DISCOVERY_SPECS
319 @pytest.mark.parametrize("ps_line, ps_pattern, user_pattern, result", [
320 (["test", "ps"], "", None, True),
321 (["test", "ps"], "ps", None, True),
322 (["test", "ps"], "ps", "root", False),
323 (["test", "ps"], "ps", "~.*y$", False),
324 (["test", "ps"], "ps", "~.*t$", True),
325 (["test", "ps"], "sp", "~.*t$", False),
326 (["root", "/sbin/init", "splash"], "/sbin/init", None, True),
328 def test_process_matches(check_manager, ps_line, ps_pattern, user_pattern, result):
329 check = check_manager.get_check("ps")
330 assert check.context["process_matches"]([check.context["ps_info"](ps_line[0])] + ps_line[1:],
331 ps_pattern, user_pattern) == result
334 @pytest.mark.parametrize("ps_line, ps_pattern, user_pattern, match_groups, result", [
335 (["test", "ps"], "", None, None, True),
336 (["test", "123_foo"], "~.*/(.*)_foo", None, ['123'], False),
337 (["test", "/a/b/123_foo"], "~.*/(.*)_foo", None, ['123'], True),
338 (["test", "123_foo"], "~.*\\\\(.*)_foo", None, ['123'], False),
339 (["test", "c:\\a\\b\\123_foo"], "~.*\\\\(.*)_foo", None, ['123'], True),
341 def test_process_matches_match_groups(check_manager, ps_line, ps_pattern, user_pattern, match_groups, result):
342 check = check_manager.get_check("ps")
343 assert check.context["process_matches"]([check.context["ps_info"](ps_line[0])] + ps_line[1:],
344 ps_pattern, user_pattern, match_groups) == result
347 @pytest.mark.parametrize("text, result", [
348 ("12:17", 737),
349 ("55:12:17", 198737),
350 ("7-12:34:59", 650099),
351 ("650099", 650099),
352 ("0", 0),
354 def test_parse_ps_time(check_manager, text, result):
355 check = check_manager.get_check("ps")
356 assert check.context["parse_ps_time"](text) == result
359 @pytest.mark.parametrize("params, result", [
360 (("sshd", 1, 1, 99, 99), {
361 "process": "sshd",
362 "user": None,
363 "levels": (1, 1, 99, 99),
364 'cpu_rescale_max': None,
366 (("sshd", "root", 2, 2, 5, 5), {
367 "process": "sshd",
368 "user": "root",
369 "levels": (2, 2, 5, 5),
370 'cpu_rescale_max': None,
373 "user": "foo",
374 "process": "/usr/bin/foo",
375 "warnmin": 1,
376 "okmin": 1,
377 "okmax": 3,
378 "warnmax": 3,
379 }, {
380 "user": "foo",
381 "process": "/usr/bin/foo",
382 "levels": (1, 1, 3, 3),
383 'cpu_rescale_max': None,
386 "user": "foo",
387 "process": "/usr/bin/foo",
388 "levels": (1, 1, 3, 3),
389 'cpu_rescale_max': True,
390 }, {
391 "user": "foo",
392 "process": "/usr/bin/foo",
393 "levels": (1, 1, 3, 3),
394 'cpu_rescale_max': True,
397 def test_cleanup_params(check_manager, params, result):
398 check = check_manager.get_check("ps")
399 assert check.context["ps_cleanup_params"](params) == result
402 PS_DISCOVERED_ITEMS = [
403 ("emacs on", {
404 "cpu_average": 15,
405 'cpu_rescale_max': True,
406 "resident_levels_perc": (25.0, 50.0),
407 "process": "emacs",
408 "icon": "emacs.png",
409 "user": "on",
410 "process_info": "html",
411 "virtual_levels": (1024**3, 2 * 1024**3),
412 "resident_levels": (1024**3, 2 * 1024**3),
413 "match_groups": [],
415 ("firefox is on fire", {
416 "process": "~.*(fire)fox",
417 "process_info": "text",
418 "user": None,
419 'cpu_rescale_max': None,
420 'match_groups': ['fire'],
422 ("sshd", {
423 "process": "~.*sshd",
424 "user": None,
425 'cpu_rescale_max': None,
426 "match_groups": [],
428 ("PS counter", {
429 'cpu_rescale_max': None,
430 'process': None,
431 'user': 'zombie',
432 "match_groups": [],
434 ("svchost", {
435 "cpulevels": (90.0, 98.0),
436 "handle_count": (1000, 2000),
437 "levels": (1, 1, 99999, 99999),
438 "max_age": (3600, 7200),
439 "process": "svchost.exe",
440 "resident_levels": (104857600, 209715200),
441 "resident_levels_perc": (25.0, 50.0),
442 "single_cpulevels": (90.0, 98.0),
443 "user": None,
444 "virtual_levels": (1073741824000, 2147483648000),
445 'cpu_rescale_max': None,
446 "match_groups": [],
448 ("smss", {
449 "process": "~smss.exe",
450 "user": None,
451 'cpu_rescale_max': None,
452 "match_groups": [],
457 def test_inventory_common(check_manager):
458 check = check_manager.get_check("ps")
459 info = sum(generate_inputs(), [])
460 parsed = check.run_parse(info)[1]
461 assert check.context["inventory_ps_common"]([], PS_DISCOVERY_WATO_RULES,
462 parsed) == PS_DISCOVERED_ITEMS
465 @pytest.mark.parametrize("service_description, matches, result", [
466 ("PS %2 %1", ["service", "check"], "PS check service"),
467 ("PS %2 %1", ["service", "check", "sm"], "PS check service"),
468 ("PS %s %s", ["service", "rename"], "PS service rename"),
469 ("PS %2 %s", ["service", "rename"], "PS rename service"),
471 def test_replace_service_description(check_manager, service_description, matches, result):
472 check = check_manager.get_check("ps")
473 assert check.context["replace_service_description"](service_description, matches, "") == result
476 def test_replace_service_description_exception(check_manager):
477 check = check_manager.get_check("ps")
479 with pytest.raises(MKGeneralException, match="1 replaceable elements"):
480 check.context["replace_service_description"]("%s", [], "")
483 check_results = [
484 CheckResult([
485 (0, "1 process", [("count", 1, 100000, 100000, 0)]),
486 (1, "1.00 GB virtual: (warn/crit at 1.00 GB/2.00 GB)", [("vsz", 1050360, 1073741824,
487 2147483648, None, None)]),
488 (0, "296.14 MB physical", [("rss", 303252, 1073741824, 2147483648, None, None)]),
489 (1, "28.9% of total RAM: (warn/crit at 25.0%/50.0%)"),
490 (0, "0.0% CPU (15 min average: 0.0%)", [("pcpu", 0.0, None, None, None, None),
491 ("pcpuavg", 0.0, None, None, 0, 15)]),
492 (0, "running for 27 h", []),
494 "\n<table><tr><th>name</th><th>user</th><th>virtual size</th><th>resident size</th><th>creation time</th><th>pid</th><th>cpu usage</th></tr><tr><td>emacs</td><td>on</td><td>1050360kB</td><td>303252kB</td><td>2018-10-23 08:02:43</td><td>9902</td><td>0.0%</td></tr></table>"
497 CheckResult([
498 (0, "1 process", [("count", 1, 100000, 100000, 0,
499 None)]),
500 (0, "2.79 GB virtual", [("vsz", 2924232, None, None, None, None)]),
501 (0, "461.18 MB physical", [("rss", 472252, None, None, None, None)]),
502 (0, "0.0% CPU", [("pcpu", 0.0, None, None, None, None)]), (0, "running for 7 h", []),
504 "\nname /usr/lib/firefox/firefox, user on, virtual size 2924232kB, resident size 472252kB, creation time 2018-10-24 04:38:07, pid 7912, cpu usage 0.0%\r\n",
507 CheckResult([
508 (0, "2 processes [running on bsd, solaris]", [("count", 2, 100000, 100000, 0, None)]),
509 (0, "20.73 MB virtual", [("vsz", 21232, None, None, None, None)]),
510 (0, "18.61 MB physical", [("rss", 19052, None, None, None, None)]),
511 (0, "0.0% CPU", [("pcpu", 0.0, None, None, None, None)]),
512 (0, "running for 52 d", []),
514 CheckResult([(0, '1 process [running on solaris]', [('count', 1, 100000, 100000, 0, None)]),
515 (0, '0.0% CPU', [('pcpu', 0.0, None, None, None, None)]),
516 (0, 'running for 0.00 s', [])]),
517 CheckResult([
518 (0, "3 processes", [("count", 3, 100000, 100000, 0, None)]),
519 (0, "136.26 MB virtual", [("vsz", 139532, 1073741824000, 2147483648000, None, None)]),
520 (0, "38.59 MB physical", [("rss", 39516, 104857600, 209715200, None, None)]),
521 (3, "percentual RAM levels configured, but total RAM is unknown", []),
522 (0, "0.0% CPU", [("pcpu", 0.0, 90.0, 98.0, None, None)]),
523 (0, "0.0% CPU for svchost.exe with PID 600"),
524 (0, "0.0% CPU for svchost.exe with PID 676"),
525 (0, "0.0% CPU for svchost.exe with PID 764"),
526 (1, "1204 process handles: (warn/crit at 1000/2000)", [("process_handles", 1204, 1000, 2000,
527 None, None)]),
528 (1, "youngest running for 12.0 s, oldest running for 71 m: (warn/crit at 60 m/120 m)", []),
530 CheckResult([
531 (0, "1 process", [("count", 1, 100000, 100000, 0, None)]),
532 (0, "4.47 MB virtual", [("vsz", 4576, None, None, None, None)]),
533 (0, "316.00 kB physical", [("rss", 316, None, None, None, None)]),
534 (0, "0.0% CPU", [("pcpu", 0.0, None, None, None, None)]),
535 (0, "53 process handles", [("process_handles", 53, None, None, None, None)]),
540 @pytest.mark.parametrize(
541 "inv_item, reference",
542 zip(PS_DISCOVERED_ITEMS, check_results),
543 ids=[a[0] for a in PS_DISCOVERED_ITEMS])
544 def test_check_ps_common(check_manager, monkeypatch, inv_item, reference):
545 check = check_manager.get_check("ps")
546 parsed = sum([check.run_parse(info)[1] for info in generate_inputs()], [])
547 total_ram = 1024**3 if "emacs" in inv_item[0] else None
548 monkeypatch.setattr('time.time', lambda: 1540375342)
549 factory_defaults = {"levels": (1, 1, 99999, 99999)}
550 factory_defaults.update(inv_item[1])
551 test_result = CheckResult(check.context["check_ps_common"](
552 inv_item[0], factory_defaults, parsed, total_ram=total_ram))
553 assertCheckResultsEqual(test_result, reference)
556 cpu_config = namedtuple("CPU_config", "name agent_info cputime cpu_cores exp_load cpu_rescale_max")
557 cpu_util_data = [
558 cpu_config('linux no cpu scale conf 1 core', "(on,105,30,00:00:{:02}/03:59:39,902) test", 30, 1,
559 50, None),
560 cpu_config('linux no cpu scale conf 2 cores', "(on,105,30,00:00:{:02}/03:59:39,902) test", 30,
561 2, 50, None),
562 cpu_config('linux No_Core_division 2 cores', "(on,105,30,00:00:{:02}/03:59:39,902) test", 120,
563 2, 200, False),
564 cpu_config('linux Core_division 2 cores', "(on,105,30,00:00:{:02}/03:59:39,902) test", 30, 2,
565 25, True),
566 cpu_config("Win no cpu scale conf 2 cores",
567 "(\\KLAPPRECHNER\ab,105,30,0,3124,904,{0}0000000,{0}0000000,0,1,14340) test.exe", 54,
568 2, 90, None),
569 cpu_config("Win No_Core_division 2 cores",
570 "(\\KLAPPRECHNER\ab,105,30,0,3124,904,{0}0000000,{0}0000000,0,1,14340) test.exe", 54,
571 2, 180, False),
572 cpu_config("Win Core_division 2 cores",
573 "(\\KLAPPRECHNER\ab,105,30,0,3124,904,{0}0000000,{0}0000000,0,1,14340) test.exe", 54,
574 2, 90, True),
575 cpu_config('Solaris,BSD,etc no cpu conf 1 core', "(on,105,30,{}/03:59:39,902) test", 30.8, 1,
576 30.8, None),
577 cpu_config('Solaris,BSD,etc no cpu conf 2 cores', "(on,105,30,{}/03:59:39,902) test", 174.8, 2,
578 174.8, None),
579 cpu_config('Solaris,BSD,etc No_Core_division 2 cores', "(on,105,30,{}/03:59:39,902) test",
580 174.8, 2, 174.8, False),
581 cpu_config('Solaris,BSD,etc Core_division 2 cores', "(on,105,30,{}/03:59:39,902) test", 174.8,
582 2, 174.8 / 2, True),
586 @pytest.mark.parametrize("data", cpu_util_data, ids=[a.name for a in cpu_util_data])
587 def test_check_ps_common_cpu(check_manager, monkeypatch, data):
588 check = check_manager.get_check("ps")
590 def time_info(agent_info, check_time, cputime, cpu_cores):
591 monkeypatch.setattr('time.time', lambda: check_time)
592 parsed = check.run_parse(splitter(agent_info.format(cputime)))[1]
594 return CheckResult(check.context["check_ps_common"](
595 inv_item[0], inv_item[1], parsed, cpu_cores=cpu_cores))
597 inv_item = (
598 "test",
600 "process": "~test",
601 "user": None,
602 "levels": (1, 1, 99999, 99999) # from factory defaults
604 if data.cpu_rescale_max is not None:
605 inv_item[1].update({"cpu_rescale_max": data.cpu_rescale_max})
607 # Initialize counters
608 time_info(data.agent_info, 0, 0, data.cpu_cores)
609 # Check_cpu_utilization
610 output = time_info(data.agent_info, 60, data.cputime, data.cpu_cores)
612 reference = CheckResult([
613 (0, "1 process", [("count", 1, 100000, 100000, 0)]),
614 (0, "105.00 kB virtual", [("vsz", 105, None, None, None, None)]),
615 (0, "30.00 kB physical", [("rss", 30, None, None, None, None)]),
616 check.context["cpu_check"](data.exp_load, inv_item[0], inv_item[1]),
617 (0, "running for 239 m", []),
620 assertCheckResultsEqual(output, reference)
623 @pytest.mark.parametrize("levels, reference", [
624 ((1, 1, 99999, 99999),
625 CheckResult([
626 (2, "0 processes: (ok from 1 to 99999)", [("count", 0, 100000, 100000, 0)]),
627 ])),
628 ((0, 0, 99999, 99999),
629 CheckResult([
630 (0, "0 processes", [("count", 0, 100000, 100000, 0)]),
631 ])),
633 def test_check_ps_common_count(check_manager, levels, reference):
634 check = check_manager.get_check("ps")
636 parsed = check.run_parse(splitter("(on,105,30,00:00:{:02}/03:59:39,902) single"))[1]
638 params = {
639 "process": "~test",
640 "user": None,
641 "levels": levels,
644 output = CheckResult(check.context["check_ps_common"]('empty', params, parsed, cpu_cores=1))
645 assertCheckResultsEqual(output, reference)
648 def test_subset_patterns(check_manager):
650 check = check_manager.get_check("ps")
652 parsed = check.run_parse(
653 splitter("""(user,0,0,0.5) main
654 (user,0,0,0.4) main_dev
655 (user,0,0,0.1) main_dev
656 (user,0,0,0.5) main_test"""))[1]
658 # Boundary in match is necessary otherwise main instance accumulates all
659 wato_rule = [({
660 'default_params': {
661 'cpu_rescale_max': True,
662 'levels': (1, 1, 99999, 99999)
664 'match': '~(main.*)\\b',
665 'descr': '%s'
666 }, [], ["@all"], {})]
668 discovered = [
669 ('main', {
670 'cpu_rescale_max': True,
671 'levels': (1, 1, 99999, 99999),
672 'process': '~(main.*)\\b',
673 'match_groups': ['main'],
674 'user': None,
676 ('main_dev', {
677 'cpu_rescale_max': True,
678 'levels': (1, 1, 99999, 99999),
679 'process': '~(main.*)\\b',
680 'match_groups': ['main_dev'],
681 'user': None,
683 ('main_test', {
684 'cpu_rescale_max': True,
685 'levels': (1, 1, 99999, 99999),
686 'process': '~(main.*)\\b',
687 'match_groups': ['main_test'],
688 'user': None,
692 assert check.context["inventory_ps_common"]([], wato_rule, parsed) == discovered
694 def counted_reference(count):
695 return CheckResult([
696 (0, "%s process%s" % (count, '' if count == 1 else 'es'), [("count", count, 100000,
697 100000, 0, None)]),
698 (0, "0.5% CPU", [("pcpu", 0.5, None, None, None, None)]),
701 for (item, params), count in zip(discovered, [1, 2, 1]):
702 output = CheckResult(check.context["check_ps_common"](item, params, parsed, cpu_cores=1))
703 assertCheckResultsEqual(output, counted_reference(count))