Replacement of cmk modules in 2.3

Hello,
I’ve written in the past a plug-in that uses the module ‘cmk.base.item_state’.
With 2.3 that module seem to be gone after I did an test update.

How can I find what module I should use for replacement of this? I’m not sure what is the right way to approach this.

It’s regarding this module: cmk.base.item_state.
I got that option recommended in the past in this post

But overall is there a document that helps us find modules that replace the old ones? Or how does that work?

Best Regards,
Erik

The mechanics to store and retreive data between check intervals have changed. Until 1.6 it was done with the checkmk functions get_item_state() and set_item_state(). With 2.0/2.1 these functions still existed but became deprecated and apparently they have completely gone with 2.3. (I don’t know about 2.2.).

The new way to store data between check intervals is the value store:

from .agent_based_api.v1 import get_value_store

def check_function(...):

    # get a reference to the value_store:
    value_store = get_value_store()
    
    # get previous data:
    previous_data = value_store['my_key'] # or value_store.get('my_key', 0)
    
    # store current data:
    value_store['my_key'] = current_data

    ...  

The value store is a MutableMapping and can be used like a regular dictionary. It gets automatically persisted, i.e. there is no function like put_value_store() or something like that.

2 Likes

For reference changed code below, thanks to @Dirk for his help.

value_store = get_value_store()

if item in section:
    now = time()
    data = section[item]
    item_key = "%s" % data['writername']
    previous_data = value_store.get(item_key, {})  # Default to an empty dictionary if not previously set

    #################################
    # Check
    #################################
    if data['state'] == "Stable":
        yield Result(state=State.OK, summary="%s has a %s state, Last error is: %s. Writer ID: %s. Writer Instance ID: %s." % (data['writername'], data['state'], data['lasterror'], data['id'], data['instanceid']))
        value_store[item_key] = {"last_state": data['state'], "timestamp": int(now)}

    elif data['state'] == "In-Progress":
        if data['state'] != previous_data.get('last_state'):
            value_store[item_key] = {"last_state": data['state'], "timestamp": int(now)}
            yield Result(state=State.OK, summary="%s has a %s state, Last error is: %s. Writer ID: %s. Writer Instance ID: %s." % (data['writername'], data['state'], data['lasterror'], data['id'], data['instanceid']))
        elif previous_data.get('last_state') == data['state'] and now - previous_data.get('timestamp', 0) < warninprogress:
            yield Result(state=State.OK, summary="%s has a %s state, Last error is: %s. Writer ID: %s. Writer Instance ID: %s." % (data['writername'], data['state'], data['lasterror'], data['id'], data['instanceid']))
        elif now - previous_data.get('timestamp', 0) >= warninprogress and now - previous_data.get('timestamp', 0) < critinprogress and previous_data.get('last_state') == data['state']:
            yield Result(state=State.WARN, summary="%s has a %s state, Last error is: %s. Writer ID: %s. Writer Instance ID: %s. This Writer is in this state since %s" % (data['writername'], data['state'], data['lasterror'], data['id'], data['instanceid'], render.datetime(int(previous_data['timestamp']))))
        elif now - previous_data.get('timestamp', 0) >= critinprogress and previous_data.get('last_state') == data['state']:
            yield Result(state=State.CRIT, summary="%s has a %s state, Last error is: %s. Writer ID: %s. Writer Instance ID: %s. This Writer is in this state since %s" % (data['writername'], data['state'], data['lasterror'], data['id'], data['instanceid'], render.datetime(int(previous_data['timestamp']))))

    elif data['state'] == "Waiting_for_Completion":
        if data['state'] != previous_data.get('last_state'):
            value_store[item_key] = {"last_state": data['state'], "timestamp": int(now)}
            yield Result(state=State.OK, summary="%s has a %s state, Last error is: %s. Writer ID: %s. Writer Instance ID: %s." % (data['writername'], data['state'], data['lasterror'], data['id'], data['instanceid']))
        elif previous_data.get('last_state') == data['state'] and now - previous_data.get('timestamp', 0) < warnwaiting:
            yield Result(state=State.OK, summary="%s has a %s state, Last error is: %s. Writer ID: %s. Writer Instance ID: %s." % (data['writername'], data['state'], data['lasterror'], data['id'], data['instanceid']))
        elif now - previous_data.get('timestamp', 0) >= warnwaiting and now - previous_data.get('timestamp', 0) < critwaiting and previous_data.get('last_state') == data['state']:
            yield Result(state=State.WARN, summary="%s has a %s state, Last error is: %s. Writer ID: %s. Writer Instance ID: %s. This Writer is in this state since %s" % (data['writername'], data['state'], data['lasterror'], data['id'], data['instanceid'], render.datetime(int(previous_data['timestamp']))))
        elif now - previous_data.get('timestamp', 0) >= critwaiting and previous_data.get('last_state') == data['state']:
            yield Result(state=State.CRIT, summary="%s has a %s state, Last error is: %s. Writer ID: %s. Writer Instance ID: %s. This Writer is in this state since %s" % (data['writername'], data['state'], data['lasterror'], data['id'], data['instanceid'], render.datetime(int(previous_data['timestamp']))))
2 Likes