SNMP Custom plugin error

CMK version: 2.2.0p23 MSP
OS version: Ubuntu 22.04 LTS

We’re trying to monitor our Fortiweb through SNMP. Sadly out of the box it does not recognize memory and CPU usage the way CheckMK does for Fortigates.
We’re trying to build a custom python script, but are very new when it comes to both Python & SNMP. We’ve been following the guide and looking at other python SNMP plugins to figure this out.

We keep getting the error:
Error in plugin file /omd/sites/US01/local/share/check_mk/checks/fortiweb_memory.py: No module named ‘cmk.agent_based’
Error in agent based plugin fortiweb_memory: No module named ‘cmk.agent_based’

Our custom plugin:

#!/usr/bin/env python3
# This file is explained in the Checkmk User Guide:
# https://docs.checkmk.com/master/en/devel_check_plugins_snmp.html#scaffold
#
# Store in your Checkmk site at:
# local/lib/python3/cmk_addons/plugins/flintstone_setup_check/agent_based/flintstone_setup_check.py
 
from cmk.agent_based.v2 import (
    CheckPlugin,
    CheckResult,
    startswith,
    DiscoveryResult,
    Result,
    Service,
    SimpleSNMPSection,
    SNMPTree,
    State,
    StringTable,
)
 
def parse_fortiweb(string_table):
    return {}
 
def discover_fortiweb(section):
    yield Service()
 
def check_fortiweb(section):
    yield Result(state=State.OK, summary="Everything is fine")
 
snmp_section_fortiweb_setup = SimpleSNMPSection(
    name = "fortiweb_base_config",
    parse_function = parse_fortiweb,
    detect = startswith(".1.3.6.1.2.1.1.1.0", "fortiweb-us"),
    fetch = SNMPTree(base='.1.3.6.1.4.1.12356.107.2.1', oids=['7.0']),
)
 
check_plugin__fortiweb_memory = CheckPlugin(
    name = "fortiweb_memory",
    sections = [ "fortiweb_base_config" ],
    service_name = "fortiweb setup check",
    discovery_function = discover_fortiweb,
    check_function = check_fortiweb,
)
 
#.1.3.6.1.2.1.1.1.0 fortiweb-us --> SNMPv2-MIB::sysDescr.0
#.1.3.6.1.4.1.12356.107.2.1.7.0 41 --> FORTINET-FORTIWEB-MIB::fwSysMemUsage.0
#.1.3.6.1.4.1.12356.107.2.1.8.0 8025 --> FORTINET-FORTIWEB-MIB::fwSysMemCapacity.0

Error message of --debug -vvn:

OMD[US01]:~$ cmk --debug -vvn Fortiweb-US
Trying to acquire lock on /omd/sites/US01/var/check_mk/crashes/base/f89df7aa-4437-11ef-93a5-d754f1109228/crash.info
Got lock on /omd/sites/US01/var/check_mk/crashes/base/f89df7aa-4437-11ef-93a5-d754f1109228/crash.info
Releasing lock on /omd/sites/US01/var/check_mk/crashes/base/f89df7aa-4437-11ef-93a5-d754f1109228/crash.info
Released lock on /omd/sites/US01/var/check_mk/crashes/base/f89df7aa-4437-11ef-93a5-d754f1109228/crash.info
Traceback (most recent call last):
  File "/omd/sites/US01/bin/cmk", line 97, in <module>
    errors = config.load_all_agent_based_plugins(
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/omd/sites/US01/lib/python3/cmk/base/config.py", line 1661, in load_all_agent_based_plugins
    errors = agent_based_register.load_all_plugins()
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/omd/sites/US01/lib/python3/cmk/base/api/agent_based/register/__init__.py", line 48, in load_all_plugins
    raise exception
  File "/omd/sites/US01/lib/python3/cmk/utils/plugin_loader.py", line 49, in load_plugins_with_exceptions
    importlib.import_module(full_name)
  File "/omd/sites/US01/lib/python3.11/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<frozen importlib._bootstrap>", line 1204, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1176, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1147, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 690, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 940, in exec_module
  File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
  File "/omd/sites/US01/local/lib/python3/cmk/base/plugins/agent_based/fortiweb_memory.py", line 8, in <module>
    from cmk.agent_based.v2 import (
ModuleNotFoundError: No module named 'cmk.agent_based'

Error message when testing detection of the plugin:

OMD[US01]:~/local/lib/check_mk/base/plugins/agent_based$ cmk -vI --detect-plugins=fortiweb_memory.py Fortiweb-US
Error in plugin file /omd/sites/US01/local/share/check_mk/checks/fortiweb_memory.py: No module named 'cmk.agent_based'
Error in agent based plugin fortiweb_memory: No module named 'cmk.agent_based'
Discovering services and host labels on: Fortiweb-US
Fortiweb-US:
+ FETCHING DATA
[SNMPFetcher] Execute data source
[PiggybackFetcher] Execute data source
No piggyback files for 'Fortiweb-US'. Skip processing.
No piggyback files for '10.126.0.130'. Skip processing.
+ ANALYSE DISCOVERED HOST LABELS
SUCCESS - Found no new host labels
+ ANALYSE DISCOVERED SERVICES
+ EXECUTING DISCOVERY PLUGINS (0)
SUCCESS - Found no new services

You are using CMK 2.2 but your plugin is written for CMK 2.3. So this will not work. Either update to CMK 2.3 or write the plugin for CMK 2.2.0

Thank you! We’ve just updated to 2.3.0P10.
It seems we still receive the same error.

looks like your file is in the wrong place. For the CheckAPI v2 this needs to be under

"/omd/sites/US01/local/lib/python3/cmk_addons/plugins/<youe_package_name>/agent_based/fortiweb_memory.py

You can use this package form the last CMK Conference as an example Checkmk Workshop Conf10

Thank you, we will look into this.
It seems that the official documentation is outdated in this case, as it points to the old paths (bottom page under point5)

By the way, we haven’t made a full package, just a .py file.
We don’t know how to make an MKP file.
Could this be the issue?

No, you only need this if you easily want to export/import a package.
The package from the Exchange is just an example that shows you where you need to place your files.

After placing the .py script in the location mentioned by you we get some information now. We’ll continue building this script but it seems that was our issue! The documentation sadly points to the wrong locations.

That’s good :wink: Don’t forget to mark this a solution, if it solves your issue…

@mschlenker I guess this in the making for CMK 2.3 :wink:

@mschlenker Also, the documentation shows everything v1, though the example script on Github is V2. The scripting seems to be a bit different per API version. I believe SNMP documentation needs a bit of attention.

Sorry for the inconvenience. I updated the examples just before leaving to vacation. I will check today whether a colleague already found the time to start working on the corresponding articles.

2 Likes