Skip to content

Commit b5acfa2

Browse files
committed
refactor(base): get_worst() accepts variadic states
Make `get_worst()` variadic (`*states`) so plugins that need to combine three or more states in one call can do so without nesting: state = lib.base.get_worst(state, used_state, committed_state) instead of state = lib.base.get_worst(state, lib.base.get_worst(used_state, committed_state)) Existing two-argument callers keep working unchanged. Calling with no arguments returns STATE_OK. Needed for the `state +=` fixes in the monitoring-plugins audit backlog (#120 sibling issue on monitoring-plugins), where several wildfly / starface plugins currently combine three states via the broken `state += get_worst(...)` pattern.
1 parent b530b1a commit b5acfa2

2 files changed

Lines changed: 19 additions & 13 deletions

File tree

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
88

99
## [Unreleased]
1010

11+
### Changed
12+
13+
* base.py: `get_worst()` now accepts any number of state arguments (`*states`). Existing two-argument callers keep working unchanged, but plugins that need to combine three or more states in one call no longer have to nest the call - e.g. `get_worst(state, used_state, committed_state)` instead of `get_worst(state, get_worst(used_state, committed_state))`
14+
1115
### Fixed
1216

1317
* Fix `--require-hashes` pip installs in CI workflows by using pinned versions instead

base.py

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
"""Provides very common every-day functions."""
1212

1313
__author__ = 'Linuxfabrik GmbH, Zurich/Switzerland'
14-
__version__ = '2026040801'
14+
__version__ = '2026041201'
1515

1616
import numbers
1717
import operator
@@ -292,17 +292,17 @@ def get_table(
292292
return '\n'.join(lines) + '\n'
293293

294294

295-
def get_worst(state1, state2):
295+
def get_worst(*states):
296296
"""
297-
Compares `state1` to `state2` and returns the worse state based on the following priority:
298-
STATE_OK < STATE_UNKNOWN < STATE_WARNING < STATE_CRITICAL
299-
It will prioritize any non-OK state.
297+
Returns the worst state among any number of input states, using the
298+
following priority: STATE_OK < STATE_UNKNOWN < STATE_WARNING < STATE_CRITICAL.
299+
Any non-OK state is prioritized over STATE_OK.
300300
301-
Note that numerically the priority order does not match their integer values.
301+
Note that numerically the priority order does not match their integer
302+
values. Calling with no arguments returns `STATE_OK`.
302303
303304
### Parameters
304-
- **state1** (`int`): The first state to compare.
305-
- **state2** (`int`): The second state to compare.
305+
- ***states** (`int`): One or more states to compare.
306306
307307
### Returns
308308
- **int**: The worse state according to the priority order.
@@ -313,14 +313,16 @@ def get_worst(state1, state2):
313313
314314
>>> get_worst(STATE_UNKNOWN, STATE_CRITICAL)
315315
STATE_CRITICAL
316+
317+
>>> get_worst(STATE_OK, STATE_WARNING, STATE_CRITICAL)
318+
STATE_CRITICAL
316319
"""
317-
state1 = int(state1)
318-
state2 = int(state2)
319-
if STATE_CRIT in (state1, state2):
320+
states = [int(s) for s in states]
321+
if STATE_CRIT in states:
320322
return STATE_CRIT
321-
if STATE_WARN in (state1, state2):
323+
if STATE_WARN in states:
322324
return STATE_WARN
323-
if STATE_UNKNOWN in (state1, state2):
325+
if STATE_UNKNOWN in states:
324326
return STATE_UNKNOWN
325327
return STATE_OK
326328

0 commit comments

Comments
 (0)