SNMP cpu_utilization on switch

Hello, i wrote a plugin for getting the cpu_utilization of a switch, but sadly im getting back errors trying to test it on my checkmk-instance. Maybe you can help me fixing this.

Best regards,

Lukas

CMK version:
2.1.0p30

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

huawei_cpu_default_levels = (80.0, 95.0)

def inventory_huawei_cpu(info):
    if info and info[0] != [None, None]:
        print(info)
        import pprint
        pprint.pprint(info)
        return [(None, 'huawei_cpu_default_levels')]

def check_huawei_cpu(item, params, info):
    if info[0] == [None, None]:
        return 3, 'No information about the CPU utilization available'

    if info[0][1]:
        util = float(info[0][1])
    else:
        util = float(info[0][0])

    infotext = "%2.1f%% utilization in the last 5 minutes" % util
    warn, crit = params
    perfdata = [("util", util, warn, crit, 0, 100)]

    if util >= crit:
        return (2, infotext + " (critical at %d%%)" % crit, perfdata)
    elif util >= warn:
        return (1, infotext + " (warning at %d%%)" % warn, perfdata)
    else:
        return (0, infotext, perfdata)

check_info["huawei_cpu"] = {
    "check_function": check_huawei_cpu,
    "inventory_function": inventory_huawei_cpu,
    "service_description": "CPU utilization",
    "has_perfdata": True,
    "group": "cpu_utilization",
    "snmp_info": (".1.3.6.1.4.1.2011.6.3.33", ["1"]),
    "snmp_scan_function": lambda oid: True,
}
**Output of “cmk -vp --debug huawei_switch_cpu_utilization”:** 
huawei_cpu: failed to convert scan function: <lambda>
Traceback (most recent call last):
  File "/omd/sites/monitor254/lib/python3/cmk/base/api/agent_based/register/section_plugins_legacy/convert_scan_functions.py", line 392, in _compute_detect_spec
    return _ast_convert_dispatcher(expression_ast)
  File "/omd/sites/monitor254/lib/python3/cmk/base/api/agent_based/register/section_plugins_legacy/convert_scan_functions.py", line 347, in _ast_convert_dispatcher
    raise ValueError(ast.dump(arg))
ValueError: Constant(value=True)

 

The above exception was the direct cause of the following exception:

 

Traceback (most recent call last):
  File "/omd/sites/monitor254/lib/python3/cmk/base/config.py", line 2122, in _extract_agent_and_snmp_sections
    create_snmp_section_plugin_from_legacy(
  File "/omd/sites/monitor254/lib/python3/cmk/base/api/agent_based/register/section_plugins_legacy/__init__.py", line 246, in create_snmp_section_plugin_from_legacy
    detect_spec = create_detect_spec(
  File "/omd/sites/monitor254/lib/python3/cmk/base/api/agent_based/register/section_plugins_legacy/convert_scan_functions.py", line 416, in create_detect_spec
    _compute_detect_spec(
  File "/omd/sites/monitor254/lib/python3/cmk/base/api/agent_based/register/section_plugins_legacy/convert_scan_functions.py", line 395, in _compute_detect_spec
    raise NotImplementedError(msg) from exc
NotImplementedError: huawei_cpu: failed to convert scan function: <lambda>

 

The above exception was the direct cause of the following exception:

 

Traceback (most recent call last):
  File "/omd/sites/monitor254/bin/cmk", line 79, in <module>
    errors = config.load_all_agent_based_plugins(check_api.get_check_api_context)
  File "/omd/sites/monitor254/lib/python3/cmk/base/config.py", line 1530, in load_all_agent_based_plugins
    errors.extend(load_checks(get_check_api_context, filelist))
  File "/omd/sites/monitor254/lib/python3/cmk/base/config.py", line 1682, in load_checks
    return _extract_agent_and_snmp_sections(
  File "/omd/sites/monitor254/lib/python3/cmk/base/config.py", line 2143, in _extract_agent_and_snmp_sections
    raise MKGeneralException(exc) from exc
cmk.utils.exceptions.MKGeneralException: huawei_cpu: failed to convert scan function: <lambda>

@BumBleBee-95 i think your scan function should look more like this

 'snmp_scan_function': lambda oid: oid('.1.3.6.1.2.1.1.2.0').startswith('.1.3.6.1.4.1.2011'),

Btw. for CMK2.1.x you sould write your check plugin with the “new” check API not in the old pre CMK 2.x style.

Have a look at the documentation, on how to do this.

@thl-cmk

thanks for your help man! As I noticed the check I actually want already exists in /share/check_mk/checks

Sadly it has the wrong oid. Is it possible to just make a copy of that, change the oid and also put it in the folder /share/check_mk/checks ? Or will that not work?

Basically, you can copy the check in question to the local site structure and modify it to your liking. In case of this check from /share/check_mk/checks to /local/share/check_mk/checks. If you do so, this will replace the original check completely.