def test_cluster(): section: Dict[str, local.LocalSection] = { "node0": local.LocalSection(errors=[], data={ "item": local.LocalResult( cached=None, item="Clustered service", state=State.OK, apply_levels=False, text="Service is OK", perfdata=[], ) }), "node1": local.LocalSection(errors=[], data={ "item": local.LocalResult( cached=None, item="Clustered service", state=State.WARN, apply_levels=False, text="Service is WARN", perfdata=[], ) }), "node2": local.LocalSection(errors=[], data={ "item": local.LocalResult( cached=None, item="Clustered service", state=State.CRIT, apply_levels=False, text="Service is CRIT", perfdata=[], ) }), } worst = local.cluster_check_local("item", {}, section) best = local.cluster_check_local("item", {"outcome_on_cluster": "best"}, section) assert list(worst) == [ Result(state=State.CRIT, notice="[node2]: Service is CRIT"), Result(state=State.OK, notice="[node0]: Service is OK"), Result(state=State.WARN, notice="[node1]: Service is WARN"), ] assert list(best) == [ Result(state=State.OK, summary="[node0]: Service is OK"), Result(state=State.OK, notice="[node1]: Service is WARN(!)"), Result(state=State.OK, notice="[node2]: Service is CRIT(!!)"), ]
def test_cluster(): section: Dict[str, Dict[str, local.LocalResult]] = { "node0": { "item": local.LocalResult( cached=None, item="Clustered service", state=0, text="Service is OK", perfdata=[], ) }, "node1": { "item": local.LocalResult( cached=None, item="Clustered service", state=1, text="Service is WARN", perfdata=[], ) }, "node2": { "item": local.LocalResult( cached=None, item="Clustered service", state=2, text="Service is CRIT", perfdata=[], ) }, } worst = local.cluster_check_local("item", Parameters({}), section) best = local.cluster_check_local( "item", Parameters({"outcome_on_cluster": "best"}), section) assert list(worst) == [ Result(state=state.CRIT, notice="[node2]: Service is CRIT"), Result(state=state.OK, notice="[node0]: Service is OK"), Result(state=state.WARN, notice="[node1]: Service is WARN(!)"), ] assert list(best) == [ Result(state=state.OK, summary="[node0]: Service is OK"), Result(state=state.OK, notice="[node1]: Service is WARN(!)"), Result(state=state.OK, notice="[node2]: Service is CRIT(!!)"), ]
def test_compute_state(): local_result = local.LocalResult( cached=None, item="Some_other_Service", state='P', text="Result is computed from two values", perfdata=[ local.Perfdata( name="value1", value=10, levels=(30, 50, None, None), tuple=("value1", 10, 30, 50, None, None), ), local.Perfdata( name="value2", value=20, levels=(20, 50, 10, 0), tuple=("value2", 20, 20, 50, 0, 100), ) ], ) assert list(local.check_local("", {}, {"": local_result})) == [ Result(state=state.OK, summary="Result is computed from two values"), Result(state=state.OK, summary="value1: 10.00"), Metric("value1", 10, levels=(30, 50)), Result(state=state.WARN, summary="value2: 20.00 (warn/crit at 20.00/50.00)"), Metric("value2", 20, levels=(20, 50), boundaries=(0, 100)), ]
def test_fix_state(): local_result = local.LocalResult( cached=None, item="NotGood", state=State.CRIT, apply_levels=False, text="A critical check", perfdata=[ local.Perfdata( name="V", value=120, levels_upper=(50, 100), levels_lower=None, boundaries=(0, 1000), ) ], ) assert list( local.check_local( "NotGood", {}, local.LocalSection(errors=[], data={"NotGood": local_result}))) == [ Result(state=State.CRIT, summary="A critical check"), Result(state=State.OK, summary='V: 120.00'), Metric("V", 120, boundaries=(0, 1000)), ]
def test_compute_state(): local_result = local.LocalResult( cache_info=None, item="Some_other_Service", state=State.OK, apply_levels=True, text="Result is computed from two values", perfdata=[ local.Perfdata( name="value1", value=10, levels_upper=(30, 50), levels_lower=None, boundaries=(None, None), ), local.Perfdata( name="value2", value=20, levels_upper=(20, 50), levels_lower=(10, 0), boundaries=(0, 100), ), ], ) assert list( local.check_local("", {}, local.LocalSection(errors=[], data={"": local_result})) ) == [ Result(state=State.OK, summary="Result is computed from two values"), Result(state=State.OK, summary="Value 1: 10.00"), Metric("value1", 10, levels=(30.0, 50.0)), Result(state=State.WARN, summary="Value 2: 20.00 (warn/crit at 20.00/50.00)"), Metric("value2", 20, levels=(20, 50), boundaries=(0, 100)), ]
def test_cluster_missing_item(): section: Dict[str, Dict[str, local.LocalResult]] = { "node0": { "item": local.LocalResult( cached=None, item="Clustered service", state=0, text="Service is OK", perfdata=[], ) }, "node1": {}, } worst = local.cluster_check_local("item", Parameters({}), section) best = local.cluster_check_local( "item", Parameters({"outcome_on_cluster": "best"}), section) assert list(worst) == [ Result(state=state.OK, summary="[node0]: Service is OK"), ] assert list(best) == [ Result(state=state.OK, summary="[node0]: Service is OK"), ]
def test_fix_state(): local_result = local.LocalResult( cached=None, item="NotGood", state=2, text="A critical check", perfdata=[ local.Perfdata( name="V", value=120, levels=(50, 100, None, None), tuple=("V", 120, 50, 100, 0, 1000), ) ], ) assert list(local.check_local("NotGood", {}, {"NotGood": local_result})) == [ Result(state=state.CRIT, summary="A critical check"), Metric("V", 120, levels=(50, 100), boundaries=(0, 1000)), ]
def test_cluster_missing_item(): section: Dict[str, local.LocalSection] = { "node0": local.LocalSection(errors=[], data={ "item": local.LocalResult( cache_info=None, item="Clustered service", state=State.OK, apply_levels=False, text="Service is OK", perfdata=[], ) }), "node1": local.LocalSection(errors=[], data={}), } worst = local.cluster_check_local("item", {}, section) best = local.cluster_check_local("item", {"outcome_on_cluster": "best"}, section) assert list(worst) == [ Result(state=State.OK, summary="[node0]: Service is OK"), ] assert list(best) == [ Result(state=State.OK, summary="[node0]: Service is OK"), ]
def test_cached(): local_result = local.LocalResult( cached=(361, 314, 120), item="Cached", state=0, text="A cached data service", perfdata=[], ) assert list(local.check_local("", {}, {"": local_result})) == [ Result(state=state.OK, summary="A cached data service"), Result( state=state.OK, summary=("Cache generated 6 minutes 1 second ago, " "Cache interval: 2 minutes 0 seconds, " "Elapsed cache lifespan: 314%"), ), ]
def test_cached(): local_result = local.LocalResult( cached=(361, 314, 120), item="Cached", state=State.OK, apply_levels=False, text="A cached data service", perfdata=[], ) assert list( local.check_local( "", {}, local.LocalSection(errors=[], data={"": local_result}))) == [ Result(state=State.OK, summary="A cached data service"), Result( state=State.OK, summary=("Cache generated 6 minutes 1 second ago, " "Cache interval: 2 minutes 0 seconds, " "Elapsed cache lifespan: 314.00%"), ), ]
def test_cached(age, expected): local_result = local.LocalResult( cache_info=CacheInfo( age=age, cache_interval=120, ), item="Cached", state=State.OK, apply_levels=False, text="A cached data service", perfdata=[], ) assert list( local.check_local("", {}, local.LocalSection(errors=[], data={"": local_result})) ) == [ Result(state=State.OK, summary="A cached data service"), Result( state=State.OK, summary=f"{expected}", ), ]
from cmk.base.plugins.agent_based.agent_based_api.v0 import Result, state, Metric @pytest.mark.parametrize( "string_table_row,expected_parsed_data", [ ( ['0', 'Service_FOO', 'V=1', 'This', 'Check', 'is', 'OK'], { "Service_FOO": local.LocalResult( cached=None, item="Service_FOO", state=0, text="This Check is OK", perfdata=[ local.Perfdata( name="V", value=1.0, levels=(None, None, None, None), tuple=("V", 1.0, None, None, None, None), ) ], ) }, ), ( ['0 "Service FOO" V=1 This Check is OK'], # 1.7: sep(0) + shlex { "Service FOO": local.LocalResult( cached=None, item="Service FOO", state=0,
@pytest.mark.parametrize( "string_table_row,expected_parsed_data", [ ( ['0', 'Service_FOO', 'V=1', 'This', 'Check', 'is', 'OK'], local.LocalSection(errors=[], data={ "Service_FOO": local.LocalResult( cached=None, item="Service_FOO", state=State.OK, apply_levels=False, text="This Check is OK", perfdata=[ local.Perfdata( name="V", value=1.0, levels_upper=None, levels_lower=None, boundaries=(None, None), ) ], ) }), ), ( ['0 "Service FOO" V=1 This Check is OK'], # 1.7: sep(0) + shlex local.LocalSection(errors=[], data={ "Service FOO": local.LocalResult(