Personnel plugin SNMP GRAPH auto generate

CMK version: 2.2.0
OS version: Debian11

Context:
I developed a simple plugin to test the possibility.

My plugin return the Used ram of a Aruba equipement.

I created the “Memory” chart but I see another chart with the OID used with the snmp query and I don’t understand how to remove this.

Script:

#!/usr/bin/env python3

from .agent_based_api.v1 import *


def discover_aruba_md_memory(section):
        yield Service()

def check_aruba_md_memory(section):
    for key in section:
        output=str(key)[2:-2]
        yield Metric("Memory", int(output), boundaries=(0, 100))
        if int(output) > 90 : yield Result(state=State.CRIT, summary=f"Used {output}%")
        elif int(output) > 75 : yield Result(state=Start.WARN, summary=f"Used {output}%")
        else : yield Result(state=State.OK, summary=f"Used {output}%")

# check snmp
register.snmp_section(
    name = "aruba_md_memory",
# vérifie que l'équipement est le bon
    detect = startswith(".1.3.6.1.2.1.1.1.0", "ArubaOS"),
# récupére la valeur snmp
    fetch = SNMPTree(
        base = '.1.3.6.1.4.1.14823.2.2.1.2.1.31',
        oids = [
            '0', #memory in pourcent
        ],
    ),
)

register.check_plugin(
    name = "aruba_md_memory",
    service_name = "Memory",
    discovery_function = discover_aruba_md_memory,
    check_function = check_aruba_md_memory,
)

Looks like that your var output are in sections. Please check with a print the result of key in the loop.
I woul like to prefer a parser to normalize the data from SNMPtree.

First: there is a typo in the Result statement → it sould be State not Start

Second: do you realy want to output a metric for each key with the same name ("Memory"), or is there only one key? And if there is only one key, why use a loop?

Last: you could use the check_levels function. instead of Metric and all this if .. then ...

        yield from check_levels(
            value=int(output),
            label='Used',
            boundaries=(0, 100),
            metric_name='Memory',
            levels_upper=(75, 90),
            render_func=render.percent,
        )

can you show the actual perfdata from your plugin. Like this

image
and

1 Like

Thank you for your replys.

This plugin is just a test to learn the functioning.

The var output is a string value was permit remove the two first and last chars. Because i didn’t use the parser, the return of memory was [‘40’].
I changed this to use a parser.
I used loop to build a structure, who can to be use of a template or exemple.

For the state=Start.WARN. I corrected ^^’

Yes, I replaced the Metric. Thank you for the example !

For the main problem.
despite the correction i have always the oid graph…

image

image

The new plugin:

#!/usr/bin/env python3
#
#   That plugin check Hardware FAN, Hardware PSU and Used memory
#   Author Mathieu Gallas/Thomas GALLOIS
#
from .agent_based_api.v1 import *
from typing import (
    List,
    Dict,
)
from cmk.base.plugins.agent_based.agent_based_api.v1.type_defs import (
    StringTable,
    DiscoveryResult,
)
## SNMP ##

def parse_aruba_md(string_table: List[StringTable]) -> Dict:

#    oid_tree = {"HWFAN": 0, "HWPSU": 1, "RAM": 2}
    oid_tree = {"HWPSU": 0, "RAM": 1}
    parsed = {}

#    parsed["HWFAN"] = {}
#    for HWFAN_1, HWFAN_2, HWFAN_3, HWFAN_4, HWFAN_5 in string_table[oid_tree["HWFAN"]]:
#        parsed["HWFAN"] = {
#            "state_fan1": int(HWFAN_1),
#            "state_fan2": int(HWFAN_2),
#            "state_fan3": int(HWFAN_3),
#            "state_fan4": int(HWFAN_4),
#            "state_fan5": int(HWFAN_5),
#        }

    parsed["HWPSU"] = {}
    for HWPSU_1 in string_table[oid_tree["HWPSU"]]:
        parsed["HWPSU"] = {
            "state_PSU1": HWPSU_1,
        }
    
    parsed["RAM"] = {}
    for memory_1 in string_table[oid_tree["RAM"]]:
        parsed["RAM"] = {
            "used_memory": memory_1,
        }

    return parsed

register.snmp_section(
    name = "aruba_md",
    parse_function = parse_aruba_md,
    parsed_section_name = "aruba_md_parsed",
    detect = startswith(".1.3.6.1.2.1.1.1.0", "ArubaOS"),
    fetch = [
 #       SNMPTree(
 #           base = '.1.3.6.1.4.1.14823.2.2.1.2.1.17.1.2',
 #           oids = [
 #               '0',    #FAN 1 STATE
 #               '1',    #FAN 2 STATE
 #               '2',    #FAN 3 STATE
 #               '3',    #FAN 4 STATE
 #               '4',    #FAN 5 STATE
 #           ],
 #       ),
        SNMPTree(
            base = '.1.3.6.1.4.1.14823.2.2.1.2.1.18.1.2',
            oids = [
                '0',    #PSU 1 STATE
            ],
        ),
        SNMPTree(
            base = '.1.3.6.1.4.1.14823.2.2.1.2.1.31',
            oids = [
                '0',    #Used memory in %
            ],
        ),
    ],
)


## Hardware FAN plugin ## HWFAN 

#def discover_aruba_md_HWFAN(section) -> DiscoveryResult:
#        yield Service()

#def check_aruba_md_HWFAN(section):
#    count=0
#    countsection=len(section["HWFAN"])
#    for key in section["HWFAN"]:
#       if key == 1:
#            count=count+1
#    if count == countsection:
#        yield Result(state=State.OK, summary=f"{countsection}/{count} FAN OK")
#    elif count > countsection:
#        yield Result(state=State.CRIT, summary=f"{countsection}/{count} FAN OK")
#    else:
#        yield Result(state=State.UNKO, summary=f"Error with value {countsection}")

#register.check_plugin(
#    name = "aruba_md_HWFAN",
#    sections = ["aruba_md_parsed"],
#    service_name = "Hardware FAN",
#    discovery_function = discover_aruba_md_HWFAN,
#    check_function = check_aruba_md_HWFAN,
#)

## Hardware PSU ##

def discover_aruba_md_HWPSU(section) -> DiscoveryResult:
    yield Service()

def check_aruba_md_HWPSU(section):
    count=0
    countsection=len(section["HWPSU"])
    for item in section["HWPSU"]:
        for key in section["HWPSU"][item]:
            if int(key) == countsection:
                count=count+1
    yield from check_levels(
        value=count,
        label='Fan ok',
        levels_lower=(1, 1),
        render_func=lambda v: "%1a / 1" % v ,
        boundaries=None,
    )


register.check_plugin(
    name = "aruba_md_HWPSU",
    sections = ["aruba_md_parsed"],
    service_name = "Hardware PSU",
    discovery_function = discover_aruba_md_HWPSU,
    check_function = check_aruba_md_HWPSU,
)

## Memory ##

def discover_aruba_md_memory(section):
    yield Service()

def check_aruba_md_memory(section):
    for item in section["RAM"]:
        for key in section["RAM"][item]:
            yield from check_levels(
                value=float(key),
                label='Used',
                boundaries=(0,100),
                metric_name='Memory',
                levels_upper=(74, 89),
                render_func=render.percent,
            )

register.check_plugin(
    name = "aruba_md_memory",
    sections = ["aruba_md_parsed"],
    service_name = "Memory",
    discovery_function = discover_aruba_md_memory,
    check_function = check_aruba_md_memory,
)


looks like the od graph metric is not from your actual plugin. Maybe it’s a leftover from a older version of the plugin?

Hello, I assumed that. I removed and recreated the host and Tadaa !
The graph oid has disappeared.

We don’t have an option to deleted old metrics?