Own snmp plugin is working - but how to display counter values as values per second (like bandwidth)

I wrote a SNMP-plugin for the QueueTree of Mikrotik Routers, which discovers the services and gets values. But theses values are absolut counters which are raising all the time - like interface counters. What I want is to display these as values per seconds like the bandwidth in interfaces.
But they are diplayed as they are - raising counters.

Thanks in advance,
Bernhard

Here is my plugin:

from .agent_based_api.v1 import *
from .agent_based_api.v1.render import bytes as render_bytes
from .agent_based_api.v1.type_defs import CheckResult, DiscoveryResult
import pprint

def discover_mikrotik_queuetree(section):
    pprint.pprint(section)
    for id, QueueTreeName, QueueTreeBytes, QueueTreePackets, QueueTreeDropped in section:
        yield Service(item=QueueTreeName)

# def check_mikrotik_queuetree(item, section) -> CheckResult:
def check_mikrotik_queuetree(item, section):
    for id, QueueTreeName, QueueTreeBytes, QueueTreePackets, QueueTreeDropped in section:
        if QueueTreeName == item:
            QueueTreeBytes = int(QueueTreeBytes)      # convert string to int
            QueueTreePackets = int(QueueTreePackets)  # convert string to int
            QueueTreeDropped = int(QueueTreeDropped)  # convert string to int
            print(item, ": ", QueueTreeBytes, "Bytes, ", QueueTreePackets, "Packets, ", QueueTreeDropped, "Drops")
            yield Metric(
                "queue_bps",
                QueueTreeBytes,
                boundaries=(0, None))
            yield Metric(
                "queue_pkts",
                QueueTreePackets,
                boundaries=(0, None))
            yield Metric(
                "queue_drops",
                QueueTreeDropped,
                levels=(None, 1),
                boundaries=(0, None))
            yield Result(
                state = State.OK,
                notice = f"{QueueTreeDropped} dropped Packets"
			)
            return

register.snmp_section(
    name = "mikrotik_queuetree",
    detect = matches(".1.3.6.1.2.1.1.1.0","RouterOS RB4011iGS\+"),
    fetch = SNMPTree(
        base = '.1.3.6.1.4.1.14988.1.1.2.2.1', # mtxrQueueTreeEntry
        oids = [
            OIDEnd(),
            '2', # mtxrQueueTreeName
            '5', # mtxrQueueTreeBytes
            '6', # mtxrQueueTreePackets
            '9', # mtxrQueueTreeDropped
        ],
    ),
)

register.check_plugin(
    name="mikrotik_queuetree",
    service_name="Queue %s",
    discovery_function=discover_mikrotik_queuetree,
    check_function=check_mikrotik_queuetree,
)

And here is my metrics file:

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

from cmk.gui.i18n import _

from cmk.gui.plugins.metrics import metric_info

#.
#   .--Metrics-------------------------------------------------------------.
#   |                   __  __      _        _                             |
#   |                  |  \/  | ___| |_ _ __(_) ___ ___                    |
#   |                  | |\/| |/ _ \ __| '__| |/ __/ __|                   |
#   |                  | |  | |  __/ |_| |  | | (__\__ \                   |
#   |                  |_|  |_|\___|\__|_|  |_|\___|___/                   |
#   |                                                                      |
#   +----------------------------------------------------------------------+
#   |  Definitions of metrics                                              |
#   '----------------------------------------------------------------------'

metric_info["queue_bps"] = {
    "title": _("Queue bandwidth"),
    "unit": "bits/s",
    "color": "#00e060",
}

metric_info["queue_pkts"] = {
    "title": _("Queue Packets"),
    "unit": "1/s",
    "color": "#00e060",
}

metric_info["queue_drops"] = {
    "title": _("Queue Drops"),
    "unit": "1/s",
    "color": "#00e060",
}

You can use get_rate to convert your counters in 1/s Here is a very basic excample:

from time import time
from cmk.base.plugins.agent_based.agent_based_api.v1 import (
    get_rate,
    get_value_store,
)

now_time = time()
value_store = get_value_store()
value = get_rate(value_store, f'your_name_for_the_counter_in_value_store', now_time, your_counter, raise_overflow=True)
yield Metric(name=f'your_metric_name', value=value)
4 Likes

Thanks a lot - that helped a lot and now the plugin is working as i wanted. I also tried to find your solution in the manual afterwards, but was not lucky :frowning:

have a look at the onlie API help. You wil find it included with your checkmk installation.

https://your.checkmk.server/your_cmk_site/check_mk/plugin-api/index.html

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed. Contact an admin if you think this should be re-opened.