Using get_value_store()

Hello, I’m trying to develop some .py that need to compare or calculate different values in a certain period of time. And I’m having some difficulties using the API more precisely the get_value_store().

One of the formulas I want to develop is the growth rate in percentage and then in integers like the file growth, for example: (Current value - Previous value) / Previous value * 100

To collect the Previous Value I was trying to use get_value_store() but I didn’t understand the concept, do I have to define a function in another file and then call it with the parameter get_value_store() to collect the current value and do the calculation?
When I save the value, how will I get it? How is the data saved?

Thanks

1 Like

from the Plugin API coc

get_value_store()
Get the value store for the current service from Checkmk

The returned value store object can be used to persist values between different check executions. It is a MutableMapping, so it can be used just like a dictionary.

Return type
MutableMapping[str, Any]

so you can use it like this:

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


def check_your_check_function(section):
    current_value = section.current_value  # or where ever your current value comes from

    persistent_values = get_value_store()  # load value store for this service
    
    if not persistent_values.get('previous_value'):   # check if this the first run
        persistent_values['previous_value'] = current_value  # save value for next run
        previous_value = None
    else:
        previous_value = persistent_values['previous_value']   # load previos value from store
        persistent_values['previous_value'] = current_value     # svae current value for next run

   # do your calucaltions ....

2 Likes

I would write this block as

previous_value = persistent_values.get('previous_value')
persistent_values['previous_value'] = current_value

There is no if needed as get() returns None when the key is not in the dict. And the second assignment is in both branches of the if.

3 Likes