Customizing default summary message in plugin using check_levels

Hello guys, I’m building a plugin and I have a question. I’m only using check_levels to define partial states, and they are sufficient for me because they automatically calculate values with thresholding and metrics. So, I don’t need if statements in my code. The problem is, when everything is OK, I don’t have a message for the summary, and it defaults to something like “Everything looks OK…”. I want to customize this message. I know I could check if the values I’m testing in the check_levels are lower/higher than my thresholds with if statements, but before doing that, I’d like to know if there’s something ready to set a default message in case all of my check_levels return State.OK.

For example:

    yield from check_levels(
        time_since_last_restore,
        levels_upper = (time_since_last_restore_warn, time_since_last_restore_crit),
        metric_name = "time_since_last_restore",
        label = "Time Since Last Restore",
        render_func = lambda v: render.timespan(v),
        notice_only = True,
    )

    yield from check_levels(
        time_since_last_backup,
        levels_upper = (time_since_last_backup_warn, time_since_last_backup_crit),
        metric_name = "time_since_last_backup",
        label = "Time Since Last Log Backup",
        render_func = lambda v: render.timespan(v),
        notice_only = True,
    )

If both check_levels above are State.OK, I would like to customize the summary message to ‘Synchronized’ without needing to use ifs to compare the values of time_since_last_restore with time_since_last_restore_warn and time_since_last_restore_crit, as well as time_since_last_backup with time_since_last_backup_warn and time_since_last_backup_crit.

To me, it makes perfect sense to have some way of setting a default message for the summary if the final state is OK, since I’m already using check_levels to automatically set the state according to the thresholding.

1 Like

I’m not aware of some default OK text which can be changed. The only thing that comes to my mind is to explicitely remember the states that the calls to check_levels() returned and then finally check if all were OK:

state_list = []

# first partial check
result, metric = check_levels(...)
yield result
yield metric
state_list.append(result.state)

# second partial check
result, metric = check_levels(...)
yield result
yield metric
state_list.append(result.state)

# ...

if State.worst(*state_list) == State.OK:
    yield Result(state=State.OK, summary="Synchronized")

Maybe encapsulate this if you need it more often.

That worked really well for me! Thank you for your help @Dirk.