No module named 'cmk.agent_based'

CMK version: 2.2.0p34 CEE
OS version: Ubuntu 22.04.5

Hello anyone,

at the work, we need to read a special info from a networkdevice. The info is deliver from the device with snmp - CheckMK dont know what to do with it.

To get the info in the monitoring, I writed a snmp plugin by my self. The problem is, i cant test it, because CheckMK doent know his own module?

When executing the script, I made sure that it is CheckMK’s Python that executes the module. However, this did not help.

Script:

#!/usr/bin/env python3

from cmk.agent_based.v2 import (
    CheckPlugin,
    CheckResult,
    startswith,
    DiscoveryResult,
    Result,
    Service,
    SimpleSNMPSection,
    SNMPTree,
    State,
    StringTable,
)

def parse_vpnsumup(data) -> str:
    return data

def discover_vpnsumup(data) -> str:
    yield Service()

def check_vpnsumup(data) -> str:
    if data == "":
        yield Result(state=State.CRIT, summary=f"Crit.")
    elif data == 0:
        yield Result(state=State.INFO, summary=f"Info.")
    else:
        yield Result(state=State.OK, summary=f"Ok.")

snmp_section_flintstone_setup = SimpleSNMPSection(
    name = "vpnsumup_snmp_config",
    parse_function = parse_vpnsumup,
    detect = all_of(                                    
        startswith(".0.0.0", "text"),   
        startswith(".0.0.0", "text"),           
        exists(".0.0.0"),            
    ),
    fetch = SNMPTree( 
        base = ".0.0.0"  
    ),
)

check_plugin__vpnsumup = CheckPlugin(
    name = "vpnsumup_snmp_scan",
    sections = [ "vpnsumup" ],
    service_name = "vpnsumup",
    discovery_function = discover_vpnsumup,
    check_function = check_vpnsumup,
)

As template, I have take the offical docu from CheckMk, see for more infos the link below.
SNMP-basierte Check-Plugins schreiben

Path:

OMD[prod]:~/local/lib/python3/cmk_addons/plugins/genua/agent_based$ ll
total 4
-rwx------ 1 prod prod 2331 Sep 27 09:59 vpnsumup.py*

Execution:

OMD[prod]:~/local/lib/python3/cmk_addons/plugins/genua/agent_based$ ./vpnsumup.py

Error message:

Traceback (most recent call last):
  File "/opt/omd/sites/prod/local/lib/python3/cmk_addons/plugins/genua/agent_based/./vpnsumup.py", line 21, in <module>
    from cmk.agent_based.v2 import (
ModuleNotFoundError: No module named 'cmk.agent_based'

Does anyone have an idea?

With best Regards
Alphabeit

Hi Alphabeit and welcome to the forum.
The paths you are refering to are valid for checkmk 2.3.
For 2.2 you need to use other paths (and other check API).
Check out SNMP-basierte Check-Plugins schreiben (different URL) and make sure to select the documentation version that matches your checkmk version:

image

1 Like

Hey Dirk,

thank you, your right. The only problem, the problem dosent change.

At first, I have change the path and the import command, based on the docu for the v2.2. It didnt help.

Import Statement:

from .agent_based_api.v1 import register, Result, Service, startswith, SNMPTree, State

Error Message:

OMD[prod]:~/local/lib/check_mk/base/plugins/agent_based$ ./vpnsumup.py
Traceback (most recent call last):
  File "/opt/omd/sites/prod/local/lib/python3/cmk/base/plugins/agent_based/./vpnsumup.py", line 21, in <module>
    from .agent_based_api.v1 import register, Result, Service, startswith, SNMPTree, State
ImportError: attempted relative import with no known parent package

After that, I also to tryd tell python the parent module. Have use “cmk”, idk what the real name is.

Import Statement:

from cmk.agent_based_api.v1 import register, Result, Service, startswith, SNMPTree, State

Error Message:

OMD[prod]:~/local/lib/check_mk/base/plugins/agent_based$ ./vpnsumup.py
Traceback (most recent call last):
  File "/opt/omd/sites/prod/local/lib/python3/cmk/base/plugins/agent_based/./vpnsumup.py", line 21, in <module>
    from cmk.agent_based_api.v1 import register, Result, Service, startswith, SNMPTree, State
ModuleNotFoundError: No module named 'cmk.agent_based_api'

Hi Alphabeit!

This is an example of what I use as an import statement for one of my SNMP plugins:

from cmk.base.plugins.agent_based.agent_based_api.v1 import (
    register,
    all_of,
    matches,
    Result,
    SNMPTree,
    State,
    render,
)

You don’t need all of them and probably different ones, but this is the basic statement.


Many plugins use

from .agent_based_api.v1 import …

but I had not the best experience with these relative imports and prefer the explicit one instead:

from cmk.base.plugins.agent_based.agent_based_api.v1 import …
2 Likes

Ah ja… a simple one. How could I not have thought of that myself. :crazy_face:

Thank you.

2 Likes