Check_ruleset_name and default parameters

Hi,

I’m starting to look at writing (snmp based) plugins. This is my code so far.
based upon Writing your own check plug-ins

#!/usr/bin/env python3
# -*- encoding: utf-8; py-indent-offset: 4 -*-

from cmk.base.plugins.agent_based.agent_based_api.v1 import *


register.snmp_section(
    name='MyTest_temp',
    detect=exists('.1.3.6.1.4.1.14988.1.1.3.10.0'),
    fetch=SNMPTree(
        base='.1.3.6.1.4.1.14988.1.1.3',
        oids=[
            '10',
        ],
    ),
)


def discovery_MyTest_temp(section):
    if section:
        yield Service()


def check_MyTest_temp(params, section):
    if section:
        temp = str(float(section[0][0])/10)
        yield Result(state=State.OK, summary=f"Internal Temp {temp}", details=str( params ) )
        return


register.check_plugin(
    name='MyTest_temp',
    service_name='MyTest Temperature',
    discovery_function=discovery_MyTest_temp,
    check_function=check_MyTest_temp,
    check_default_parameters={},
    check_ruleset_name="hw_temperature",
)

And it mostly works as expected. The point is the params in the check function is always empty.
Looking at “/lib/python3/cmk/gui/plugins/wato/check_parameters/hw_temperature_single.py” i’d expected the default values from that file to be shown.

Changing the line to

check_default_parameters={"warning at": 30, "critical at": 40 },

or

check_default_parameters={"warning_at": 30, "critical_at": 40 },

results in “Invalid parameter {‘warning at’: 30, ‘critical at’: 40}: 0” when checking the parameters in the gui.

Looks like i’m not handling that part very well. Can someone point me in the right direction?

There is a utility function check_temperature which uses the ruleset name “temperature”. You should use that one.

Have a look at $OMD_ROOT/lib/check_mk/base/plugins/agent_based/utils/temperature.py.

Import it into your check plugin with
from .utils.temperature import check_temperature

Use it in the check function with
yield from check_temperature(…)

Thanks, will look at those. (By the way, I simply took temperature metric just for learning the plugins)

I guess this is because of a not matching WATO rule set. So no real error, but “only” a incompatible WATO function. hw_temperature returns a Tuple, not a Dictionary as in your check_default_parameters definition.

When offering a tuple, it throws an error stating it wants a dict

File "/omd/sites/officelab/lib/python3/cmk/base/api/agent_based/register/utils.py", line 204, in validate_default_parameters  raise TypeError("default %s parameters must be dict" % (params_type,))

hw_temperature is an old WATO rule set. The new API expects a dictionary at this point. You should remove hw_temperature from your check, and write your own rule set (that returns a dictionary) or in case of a temparature sensor go with the check_temperature function as @r.sander said, and use temperature as rule set, with the correct parmeters of curse :wink:

1 Like