def test_cpu_loads_predictive(mocker): # make sure cpu_load check can handle predictive values mocker.patch("cmk.base.check_api._prediction.get_levels", return_value=(None, (2.2, 4.2, None, None))) # TODO: don't mock this. Use the context managers. mocker.patch("cmk.base.plugin_contexts._hostname", value="unittest") mocker.patch("cmk.base.plugin_contexts._service_description", value="unittest-sd") params = { 'levels': { 'period': 'minute', 'horizon': 1, 'levels_upper': ('absolute', (2.0, 4.0)) } } section = Section(load=Load(0.5, 1.0, 1.5), num_cpus=4, num_threads=123) result = set(check_cpu_load(params, section)) assert result == set(( Result(state=State.OK, summary='15 min load: 1.50 (no reference for prediction yet)'), Result(state=State.OK, summary='15 min load per core: 0.38 (4 cores)'), Metric('load1', 0.5, boundaries=(0, 4.0)), Metric('load5', 1.0, boundaries=(0, 4.0)), Metric('load15', 1.5, levels=(2.2, 4.2)), # those are the predicted values ))
def test_cpu_loads_predictive(mocker: Mock) -> None: # make sure cpu_load check can handle predictive values mocker.patch( "cmk.base.check_api._prediction.get_levels", return_value=(None, (2.2, 4.2, None, None)), ) with current_host("unittest"), current_service( CheckPluginName("cpu_loads"), "item"): assert list( check_cpu_load( { "levels": { "period": "minute", "horizon": 1, "levels_upper": ("absolute", (2.0, 4.0)), } }, Section( load=Load(0.5, 1.0, 1.5), num_cpus=4, threads=Threads(count=123), ), ) ) == [ Result( state=State.OK, summary="15 min load: 1.50 (no reference for prediction yet)"), Metric("load15", 1.5, levels=(2.2, 4.2)), # those are the predicted values Result(state=State.OK, summary="15 min load per core: 0.38 (4 cores)"), Metric("load1", 0.5, boundaries=(0, 4.0)), Metric("load5", 1.0, boundaries=(0, 4.0)), ]
def test_cpu_threads(): section = Section(load=Load(0.1, 0.1, 0.1), num_cpus=4, num_threads=1234) params: Dict[str, Any] = {} result = set(check_cpu_threads(params, section)) assert result == { Metric("threads", 1234.0), Result(state=State.OK, summary="1234"), }
def test_cpu_loads_fixed_levels(mocker): section = Section(load=Load(0.5, 1.0, 1.5), num_cpus=4, num_threads=123) params = {'levels': (2.0, 4.0)} result = set(check_cpu_load(params, section)) assert result == set(( Result(state=State.OK, summary='15 min load: 1.50'), Result(state=State.OK, summary='15 min load per core: 0.38 (4 cores)'), Metric('load1', 0.5, boundaries=(0, 4.0)), Metric('load5', 1.0, boundaries=(0, 4.0)), Metric('load15', 1.5, levels=(8.0, 16.0)), # levels multiplied by num_cpus ))
def test_cpu_threads_max_threads(): section = Section(load=Load(0.1, 0.1, 0.1), num_cpus=4, num_threads=1234, max_threads=2468) params: Dict[str, Any] = {} result = set(check_cpu_threads(params, section)) assert result == { Metric('thread_usage', 50.0), Metric('threads', 1234.0), Result(state=State.OK, summary='1234'), Result(state=State.OK, summary='Usage: 50.00%') }
def test_parse_statgrab_load() -> None: assert parse_statgrab_load([ ["min1", "2.500000"], ["min15", "5.898438"], ["min5", "4.191406"], ]) == Section( load=Load( load1=2.500000, load5=4.191406, load15=5.898438, ), num_cpus=1, )
def test_cpu_threads_max_threads(): section = Section( load=Load(0.1, 0.1, 0.1), num_cpus=4, threads=Threads(count=1234, max=2468), ) params: Dict[str, Any] = {} result = set(check_cpu_threads(params, section)) assert result == { Metric("thread_usage", 50.0), Metric("threads", 1234.0), Result(state=State.OK, summary="1234"), Result(state=State.OK, summary="Usage: 50.00%"), }
def test_cpu_loads_fixed_levels() -> None: assert list( check_cpu_load( {"levels": (2.0, 4.0)}, Section( load=Load(0.5, 1.0, 1.5), num_cpus=4, num_threads=123, type=ProcessorType.physical, ), )) == [ Result(state=State.OK, summary="15 min load: 1.50"), Metric("load15", 1.5, levels=(8.0, 16.0)), # levels multiplied by num_cpus Result(state=State.OK, summary="15 min load per core: 0.38 (4 physical cores)"), Metric("load1", 0.5, boundaries=(0, 4.0)), Metric("load5", 1.0, boundaries=(0, 4.0)), ]
"average:", "0.08,", "0.07,", "0.06", ], [ "32", "logical", "processors", "(4", "per", "socket)", ], ], Section( load=Load(load1=0.08, load5=0.07, load15=0.06), num_cpus=32, ), id="with number of cpus", ), pytest.param( [[ "10:44am", "up", "239", "days,", "19:28,", "6", "users,", "load", "average:",
# conditions defined in the file COPYING, which is part of this source code package. import pytest from cmk.base.plugins.agent_based.agent_based_api.v1.type_defs import StringTable from cmk.base.plugins.agent_based.ucd_cpu_load import parse_ucd_cpu_load from cmk.base.plugins.agent_based.utils.cpu import Load, Section @pytest.mark.parametrize( ["string_table", "expected_section"], [ pytest.param( [["312", "3.123213"], ["280", "2.78897"], ["145", "1.34563546"]], Section( load=Load(load1=3.123213, load5=2.78897, load15=1.34563546), num_cpus=1, ), id="complete dataset", ), pytest.param( [["", "5,234"], ["234", ""], ["", ""]], Section( load=Load(load1=5.234, load5=2.34, load15=0), num_cpus=1, ), id="data missing", ), ], ) def test_parse_ucd_cpu_load(string_table: StringTable,