SNMP Check - doesn't working and doesn't execute any print for debug

Hi all,
I’m trying to write my first check snmp fo check_mk_raw-2.0.0p3, see code belowe

cat local/lib/check_mk/base/plugins/agent_based/damocles_in.py    
#!/usr/bin/env python3
# -*- encoding: utf-8; py-indent-offset: 4 -*-

# Damocles
# '.1.3.6.1.4.1.21796.3.4.1.1.3' => Check Name
# '.1.3.6.1.4.1.21796.3.4.1.1.2' => Check Value
# '.1.3.6.1.4.1.21796.3.4.1.1.4' => Alarm ON/OFF
# '.1.3.6.1.4.1.21796.3.4.1.1.6' => Pulse Counter

from .agent_based_api.v1 import *
from pprint import pprint

def parse_damocles_in(string_table):
    print(string_table)
    return string_table


register.snmp_section(
    name = "damocles_in",
    detect = startswith(".1.3.6.1.2.1.1.1.0", "Damocles"),
    parse_function=parse_damocles_in,
    fetch = SNMPTree(
        base = '.1.3.6.1.4.1.21796.3.4.1.1',
        oids = [
            OIDEnd(),
            '3', # Sensor Name
            '2', # Sensor Value
            '4', # Sensor alarm State (ON/OFF)
            '6', # Sensor Pulse Counter
        ],
    ),
)

doing

cmk --detect-plugins=damocles_in -vnII MY_HOST

the output don’t print “string_table” informations
OUTPUT:

Discovering services and host labels on: MY_HOST
MY_HOST:
+ FETCHING DATA
[SNMPFetcher] Execute data source
No piggyback files for 'MY_HOST'. Skip processing.
No piggyback files for '192.168.70.17'. Skip processing.
[PiggybackFetcher] Execute data source
+ PARSE FETCHER RESULTS
Received no piggyback data
+ EXECUTING HOST LABEL DISCOVERY
+ PERFORM HOST LABEL DISCOVERY
+ EXECUTING DISCOVERY PLUGINS (0)
SUCCESS - Found no services, no host labels

Please can someone get me in the right direction?
Many thanks

There are no “register.check_plugin” in your code.
Without any registered check plugin the rest of the code will do nothing.
The “kentix_devices” check from @r.sander is a good example for a complex SNMP check but also it is not too complex to read :slight_smile:
In this check you can see all the sections what are needed.

1 Like

Thanks Andreas! I finished to write the plugin with all sections needed (I hope).

from .agent_based_api.v1 import *
from pprint import pprint


def parse_damocles_in(string_table):
    section = []
    for line in string_table:
        alarm = int(line[3])
        if alarm != 0:
            section.append(line)

    return section

def discover_damocles_in(section):
    for line in section:
        yield Service(item=line[1])

def check_damocles_in(item, section):
    for id, check, state, enable, pulseCount in section:
        if check == item:
            perfdata = [("pulseCount", pulseCount, None, None, None)]
            if state == "0":
                yield Result(state=State.OK)
                yield Metric(perfdata)
            else:
                yield Result(state=State.CRIT)
                yield Metric(perfdata)


register.snmp_section(
    name = "damocles_in",
    detect = startswith(".1.3.6.1.2.1.1.1.0", "Damocles"),
    parse_function=parse_damocles_in,
    fetch = SNMPTree(
        base = '.1.3.6.1.4.1.21796.3.4.1.1',
        oids = [
            OIDEnd(),
            '3', # Sensor Name
            '2', # Sensor Value
            '4', # Sensor alarm State (ON/OFF)
            '6', # Sensor Pulse Counter
        ],
    ),
)

register.check_plugin(
    name="damocles_in",
    service_name="%s",
    discovery_function=discover_damocles_in,
    check_function=check_damocles_in,
)

Now the cmk command work as expected:

cmk --detect-plugins=damocles_in -vnII IT1-Damocles-Ingresso   
Discovering services and host labels on: IT1-Damocles-Ingresso
IT1-Damocles-Ingresso:
+ FETCHING DATA
[SNMPFetcher] Execute data source
No piggyback files for 'IT1-Damocles-Ingresso'. Skip processing.
No piggyback files for '192.168.70.17'. Skip processing.
[PiggybackFetcher] Execute data source
+ PARSE FETCHER RESULTS
Received no piggyback data
+ EXECUTING HOST LABEL DISCOVERY
+ PERFORM HOST LABEL DISCOVERY
+ EXECUTING DISCOVERY PLUGINS (1)
  7 damocles_in
SUCCESS - Found 7 services, no host labels

But without “–detect-plugins” it doesn’t:

Discovering services and host labels on: IT1-Damocles-Ingresso
IT1-Damocles-Ingresso:
+ FETCHING DATA
[SNMPFetcher] Execute data source
ERROR: SNMP error
Error in packet
Reason: (noSuchName) There is no such variable name in this MIB.
Failed object: .1.3.6.1.4.1.232.2.2.4.2.0


ERROR: SNMP error
Error in packet
Reason: (noSuchName) There is no such variable name in this MIB.
Failed object: .1.3.6.1.2.1.25.1.1.0


ERROR: SNMP error
Error in packet
Reason: (noSuchName) There is no such variable name in this MIB.
Failed object: .1.3.6.1.4.1.2036.2.1.1.4.0


ERROR: SNMP error
Error in packet
Reason: (noSuchName) There is no such variable name in this MIB.
Failed object: .1.3.6.1.4.1.6302.2.1.1.1.0


ERROR: SNMP error
Error in packet
Reason: (noSuchName) There is no such variable name in this MIB.
Failed object: .1.3.6.1.4.1.14848.2.1.1.1.0


ERROR: SNMP error
Error in packet
Reason: (noSuchName) There is no such variable name in this MIB.
Failed object: .1.3.6.1.4.1.30155.2.1.1.0


No piggyback files for 'IT1-Damocles-Ingresso'. Skip processing.
No piggyback files for '192.168.70.17'. Skip processing.
[PiggybackFetcher] Execute data source
+ PARSE FETCHER RESULTS
Received no piggyback data
+ EXECUTING HOST LABEL DISCOVERY
+ PERFORM HOST LABEL DISCOVERY
+ EXECUTING DISCOVERY PLUGINS (3)
  1 snmp_info
  1 uptime
SUCCESS - Found 2 services, no host labels

I fix some other errors but the problem is still present, belowe the code:

from .agent_based_api.v1 import *
from pprint import pprint


def parse_damocles_in(string_table):
    section = []
    for line in string_table:
        alarm = int(line[3])
        if alarm != 0:
            section.append(line)

    return section


def discover_damocles_in(section):
    for line in section:
        yield Service(item=line[1])


def check_damocles_in(item, section):
    for id, check, state, enable, pulseCount in section:
        if check == item:
            yield Metric('pulseCount', int(pulseCount))
            if state == "0":
                yield Result(state=State.OK, summary=f"{check} Ok!")
            else:
                yield Result(state=State.CRIT, summary=f"{check} allarmato!")


register.snmp_section(
    name = "damocles_in",
    detect = startswith(".1.3.6.1.2.1.1.1.0", "Damocles"),
    parse_function=parse_damocles_in,
    fetch = SNMPTree(
        base = '.1.3.6.1.4.1.21796.3.4.1.1',
        oids = [
            OIDEnd(),
            '3', # Sensor Name
            '2', # Sensor Value
            '4', # Sensor alarm State (ON/OFF)
            '6', # Sensor Pulse Counter
        ],
    ),
)

register.check_plugin(
    name="damocles_in",
    service_name="%s",
    discovery_function=discover_damocles_in,
    check_function=check_damocles_in,
)

with “–detect-plugins” even discovery and check work as expected, without nothing appens.

I think one problem is the parse function. This should return a dictionary.
But @moritz is also replying and from the master of check API you can get more information :slight_smile:

That strongly indicates that the decive might not be detected. To validate that assumption, try a broader detection (just for debugging!):
detect = exists(".1.3.6.1.2.1.1.1.0")

With respect to what @andreas-doehler said: A dictionary would be nicer, IMHO. You could replace the above by section[line[1]] = line , discover yield from *(Service(item=k) for k in section) and in the check use the pattern

data = section.get(item)
if data is None:
    return
id_, _, state, enable, pulse_count = data

But: A list should be fine. If you return None, processing stops. As if no data were fetched.

I tryed what suggested but nothing is changed

Just for report the snmpwalk is:

.1.3.6.1.2.1.1.1.0 Damocles 1208 SNMP Supervisor v1.0.2

What can I do for debugging why the check is not recognized from the API?

It’s hard to tell from a distance. Are there any other local changes that might be interfering? If so remove them temporarily. What’s the output of cmk --detect-plugins=damocles_in --debug -vvnII IT1-Damocles-Ingresso (note the --debug and the two -vv)?

Thanks moritz!

Below the output of the command:

Discovering services and host labels on: IT1-Damocles-Ingresso
IT1-Damocles-Ingresso:
+ FETCHING DATA
  Source: SourceType.HOST/FetcherType.SNMP
[cpu_tracking] Start [7fc3bf97fe20]
[SNMPFetcher] Fetch with cache settings: SNMPFileCache(path=PosixPath('/omd/sites/infra01_2/tmp/check_mk/data_source_cache/snmp/IT1-Damocles-Ingresso'), max_age=0, disabled=False, use_outdated=False, simulation=False), Cache enabled: False
[SNMPFetcher] Execute data source
damocles_in: Fetching data (SNMP walk cache is disabled)
Running 'snmpwalk -v1 -c public -m "" -M "" -t 60.00 -r 2 -Cc -OQ -OU -On -Ot 192.168.70.17 .1.3.6.1.4.1.21796.3.4.1.1.3'
Running 'snmpwalk -v1 -c public -m "" -M "" -t 60.00 -r 2 -Cc -OQ -OU -On -Ot 192.168.70.17 .1.3.6.1.4.1.21796.3.4.1.1.2'
Running 'snmpwalk -v1 -c public -m "" -M "" -t 60.00 -r 2 -Cc -OQ -OU -On -Ot 192.168.70.17 .1.3.6.1.4.1.21796.3.4.1.1.4'
Running 'snmpwalk -v1 -c public -m "" -M "" -t 60.00 -r 2 -Cc -OQ -OU -On -Ot 192.168.70.17 .1.3.6.1.4.1.21796.3.4.1.1.6'
[cpu_tracking] Stop [7fc3bf97fe20 - Snapshot(process=posix.times_result(user=0.020000000000000018, system=0.010000000000000009, children_user=0.0, children_system=0.01, elapsed=0.22999999672174454))]
  Source: SourceType.HOST/FetcherType.PIGGYBACK
[cpu_tracking] Start [7fc3be39b520]
No piggyback files for 'IT1-Damocles-Ingresso'. Skip processing.
No piggyback files for '192.168.70.17'. Skip processing.
[PiggybackFetcher] Fetch with cache settings: NoCache(path=PosixPath('/omd/sites/infra01_2/tmp/check_mk/data_source_cache/piggyback/IT1-Damocles-Ingresso'), max_age=0, disabled=False, use_outdated=False, simulation=False), Cache enabled: False
[PiggybackFetcher] Execute data source
[cpu_tracking] Stop [7fc3be39b520 - Snapshot(process=posix.times_result(user=0.0, system=0.0, children_user=0.0, children_system=0.0, elapsed=0.0))]
+ PARSE FETCHER RESULTS
  Source: SourceType.HOST/FetcherType.SNMP
No persisted sections loaded
  -> Add sections: ['damocles_in']
  Source: SourceType.HOST/FetcherType.PIGGYBACK
Loading autochecks from /omd/sites/infra01_2/var/check_mk/autochecks/IT1-Damocles-Ingresso.mk
No persisted sections loaded
  -> Add sections: []
Received no piggyback data
+ EXECUTING HOST LABEL DISCOVERY
Trying host label discovery with: damocles_in
+ PERFORM HOST LABEL DISCOVERY
+ EXECUTING DISCOVERY PLUGINS (1)
  Trying discovery with: damocles_in
Loading autochecks from /omd/sites/infra01_2/var/check_mk/autochecks/IT1-Damocles-Ingresso.mk
Try aquire lock on /omd/sites/infra01_2/var/check_mk/autochecks/IT1-Damocles-Ingresso.mk
Got lock on /omd/sites/infra01_2/var/check_mk/autochecks/IT1-Damocles-Ingresso.mk
Releasing lock on /omd/sites/infra01_2/var/check_mk/autochecks/IT1-Damocles-Ingresso.mk
Released lock on /omd/sites/infra01_2/var/check_mk/autochecks/IT1-Damocles-Ingresso.mk
  7 damocles_in
SUCCESS - Found 7 services, no host labels

As you can see it work as expected but only forcing with “–detect-plugins”

I think there is something strange with scan function…
Doing “cmk --debug -vvnII IT1-Damocles-Ingresso”

Using cached OID .1.3.6.1.2.1.1.2.0: ‘.1.3.6.1.4.1.21796.3.4’
Using cached OID .1.3.6.1.2.1.1.1.0: ‘Damocles 1208 SNMP Supervisor v1.0.2’
Using cached OID .1.3.6.1.2.1.1.2.0: ‘.1.3.6.1.4.1.21796.3.4’
Using cached OID .1.3.6.1.2.1.1.2.0: ‘.1.3.6.1.4.1.21796.3.4’
SNMP scan found hr_mem poseidon_inputs poseidon_temp snmp_info snmp_os snmp_uptime

PS.: I’m monitoring this device with a custom plugin written for cmk 1.6 which I’m using lambda function:

'snmp_scan_function': \
        lambda oid: oid(".1.3.6.1.2.1.1.2.0").startswith(".1.3.6.1.4.1.21796.") or \
                    oid(".1.3.6.1.2.1.1.1.0").startswith("Damocles"),

Hi! Good and bad news…
I found a rule that during upgrade from 1.6 was created automaticly


I deleted it and now the debug give me un’exeception

cmk --detect-plugins=damocles_in --debug -vvnII IT1-Damocles-Ingresso

Traceback (most recent call last):
  File "/omd/sites/infra01_2/lib/python3/cmk/base/config.py", line 1986, in _extract_agent_and_snmp_sections
    create_snmp_section_plugin_from_legacy(
  File "/omd/sites/infra01_2/lib/python3/cmk/base/api/agent_based/register/section_plugins_legacy/__init__.py", line 240, in create_snmp_section_plugin_from_legacy
    detect_spec = create_detect_spec(
  File "/omd/sites/infra01_2/lib/python3/cmk/base/api/agent_based/register/section_plugins_legacy/convert_scan_functions.py", line 397, in create_detect_spec
    _compute_detect_spec(
  File "/omd/sites/infra01_2/lib/python3/cmk/base/api/agent_based/register/section_plugins_legacy/convert_scan_functions.py", line 373, in _compute_detect_spec
    return _ast_convert_dispatcher(expression_ast)
  File "/omd/sites/infra01_2/lib/python3/cmk/base/api/agent_based/register/section_plugins_legacy/convert_scan_functions.py", line 320, in _ast_convert_dispatcher
    return _ast_convert_bool(arg)
  File "/omd/sites/infra01_2/lib/python3/cmk/base/api/agent_based/register/section_plugins_legacy/convert_scan_functions.py", line 247, in _ast_convert_bool
    return all_of(*(_ast_convert_dispatcher(v) for v in bool_ast.values))
  File "/omd/sites/infra01_2/lib/python3/cmk/base/api/agent_based/register/section_plugins_legacy/convert_scan_functions.py", line 247, in <genexpr>
    return all_of(*(_ast_convert_dispatcher(v) for v in bool_ast.values))
  File "/omd/sites/infra01_2/lib/python3/cmk/base/api/agent_based/register/section_plugins_legacy/convert_scan_functions.py", line 323, in _ast_convert_dispatcher
    return _ast_convert_compare(arg)
  File "/omd/sites/infra01_2/lib/python3/cmk/base/api/agent_based/register/section_plugins_legacy/convert_scan_functions.py", line 222, in _ast_convert_compare
    assert isinstance(comp_ast.comparators[0], ast.Str)
AssertionError

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

Traceback (most recent call last):
  File "/omd/sites/infra01_2/bin/cmk", line 79, in <module>
    errors = config.load_all_agent_based_plugins(check_api.get_check_api_context)
  File "/omd/sites/infra01_2/lib/python3/cmk/base/config.py", line 1415, in load_all_agent_based_plugins
    errors.extend(load_checks(get_check_api_context, filelist))
  File "/omd/sites/infra01_2/lib/python3/cmk/base/config.py", line 1563, in load_checks
    errors = (_extract_agent_and_snmp_sections(validate_creation_kwargs=did_compile) +
  File "/omd/sites/infra01_2/lib/python3/cmk/base/config.py", line 2005, in _extract_agent_and_snmp_sections
    raise MKGeneralException(exc) from exc
cmk.utils.exceptions.MKGeneralException

Yeah! Now the error abow was generated by another custom plugin which I deleted and now all is working like a sharme!

So, hoping this may help some other guys, in my case I have two problems:

  1. During upgrade from 1.6 the system created a rule that disable section of my custom plugin named “damocles_in”. The solution was delete this rule.
  2. I had another old custom plugin which generated un’exeption. The solution was delete this plugin

Thanks to all for the support and suggestions! :wink: