Skip to content

Commit ce53a6a

Browse files
printer_supply: use supply toner color metric
The following improvements were made to the plugin: - drop generic "pages" metric as it's not relevant to the printer supply level data coming from the SNMP address. - use a supply toner metric based on the color code provided by the data extracted from the service description. - improve the summary in the check levels metric and only show unit-based summary when the unit is not a percentage. Note: this change will drop the legacy "pages" metric and the generic "supply_toner" metric. It's not possible to migrate "supply_toner" to the new `supply_toner_{color}` metric. Since we'd also need the ability to translate the metric based on color which isn't possible. CMK-22267 Change-Id: I4cfc6371471e8765e952b987886faa07564c366a
1 parent 1a5cd1c commit ce53a6a

File tree

3 files changed

+103
-49
lines changed

3 files changed

+103
-49
lines changed

.werks/17835.md

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
[//]: # (werk v2)
2+
# Printer Supply: switch to color-based toner metrics
3+
4+
key | value
5+
---------- | ---
6+
date | 2025-03-24T14:58:01+00:00
7+
version | 2.4.0b3
8+
class | feature
9+
edition | cre
10+
component | checks
11+
level | 1
12+
compatible | yes
13+
14+
Starting in 2.4, the toner color was not shown in `supply_toner` metric. This is now supported by
15+
migrating these metrics to `supply_toner_{black|cyan|magenta|yellow}` if a color is found.
16+
Otherwise, the `supply_toner_other` metric will be used.
17+
18+
**Note:** this means that history of the previous `supply_toner` metric will not be connected to the
19+
new color metric history. However, you will still see its history in the respective service's
20+
detailed view.
21+
22+
In addition, this change drops the `pages` metric as it's not related to the data derived from the
23+
SNMP data. The readability of labels for the existing results and metrics were also improved,
24+
removing any redundant information.

cmk/plugins/collection/agent_based/printer_supply.py

+13-16
Original file line numberDiff line numberDiff line change
@@ -241,36 +241,33 @@ def check_printer_supply(item: str, params: CheckParams, section: Section) -> Ch
241241
return
242242

243243
color_info = _get_supply_color_info(item, supply.color)
244-
245-
warn, crit = params["levels"]
244+
metric_name = f"supply_toner_{supply.color or 'other'}"
246245

247246
if supply.has_partial_data: # no percentage possible
248-
yield from _get_partial_data_results(supply, params, color_info)
247+
yield from _get_partial_data_results(supply, params, color_info, metric_name)
249248
return
250249

251250
yield from check_levels_v1(
252251
_get_fill_level_percentage(supply, params["upturn_toner"]),
253-
levels_lower=(warn, crit),
254-
label=f"{color_info}Remaining",
252+
metric_name=metric_name,
253+
levels_lower=params["levels"],
254+
label="Supply level remaining",
255255
render_func=render.percent,
256256
)
257257

258-
summary = f"Supply: {supply.level} of max. {supply.max_capacity}{supply.unit}"
259-
yield Result(state=State.OK, summary=summary)
260-
yield Metric(
261-
"pages",
262-
supply.level,
263-
levels=(0.01 * warn * supply.max_capacity, 0.01 * crit * supply.max_capacity),
264-
boundaries=(0, supply.max_capacity),
265-
)
258+
if supply.unit not in {"", "%"}:
259+
yield Result(
260+
state=State.OK,
261+
summary=f"Supply: {supply.level} of max. {supply.max_capacity}{supply.unit}",
262+
)
266263

267264

268265
def _get_supply_color_info(item: str, color: Color | None) -> str:
269266
return f"[{color}] " if color and color not in item.lower() else ""
270267

271268

272269
def _get_partial_data_results(
273-
supply: PrinterSupply, params: CheckParams, color_info: str
270+
supply: PrinterSupply, params: CheckParams, color_info: str, metric_name: str
274271
) -> CheckResult:
275272
if supply.level_unrestricted or supply.capacity_unrestricted:
276273
summary = "%sThere are no restrictions on this supply" % color_info
@@ -293,8 +290,8 @@ def _get_partial_data_results(
293290
yield Result(state=State.UNKNOWN, summary="%s Unknown level" % color_info)
294291

295292
elif supply.capacity_unknown:
296-
yield Result(state=State.OK, summary="%sLevel: %d" % (color_info, supply.level))
297-
yield Metric("pages", supply.level)
293+
yield Result(state=State.OK, summary="Supply: %d%s" % (supply.level, supply.unit))
294+
yield Metric(metric_name, supply.level)
298295

299296

300297
def _get_fill_level_percentage(supply: PrinterSupply, upturn_toner: bool) -> float:

tests/unit/cmk/plugins/collection/agent_based/test_printer_supply.py

+66-33
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,10 @@ def test_inventory_printer_supply(
143143
["Magenta Ink Cartridge", "15", "-2", "5", "3", "1"],
144144
],
145145
],
146-
[Result(state=State.OK, summary="Level: 5"), Metric("pages", 5)],
146+
[
147+
Result(state=State.OK, summary="Supply: 5 tenths of milliliters"),
148+
Metric("supply_toner_magenta", 5),
149+
],
147150
),
148151
(
149152
"Magenta Ink Cartridge",
@@ -156,10 +159,11 @@ def test_inventory_printer_supply(
156159
],
157160
[
158161
Result(
159-
state=State.CRIT, summary="Remaining: 5.00% (warn/crit below 20.00%/10.00%)"
162+
state=State.CRIT,
163+
summary="Supply level remaining: 5.00% (warn/crit below 20.00%/10.00%)",
160164
),
165+
Metric("supply_toner_magenta", 5.0),
161166
Result(state=State.OK, summary="Supply: 5 of max. 100 tenths of milliliters"),
162-
Metric("pages", 5, levels=(20.0, 10.0), boundaries=(0, 100)),
163167
],
164168
),
165169
(
@@ -173,10 +177,11 @@ def test_inventory_printer_supply(
173177
],
174178
[
175179
Result(
176-
state=State.WARN, summary="Remaining: 15.00% (warn/crit below 20.00%/10.00%)"
180+
state=State.WARN,
181+
summary="Supply level remaining: 15.00% (warn/crit below 20.00%/10.00%)",
177182
),
183+
Metric("supply_toner_magenta", 15.0),
178184
Result(state=State.OK, summary="Supply: 15 of max. 100 tenths of milliliters"),
179-
Metric("pages", 15, levels=(20.0, 10.0), boundaries=(0, 100)),
180185
],
181186
),
182187
(
@@ -189,9 +194,9 @@ def test_inventory_printer_supply(
189194
],
190195
],
191196
[
192-
Result(state=State.OK, summary="Remaining: 25.00%"),
197+
Result(state=State.OK, summary="Supply level remaining: 25.00%"),
198+
Metric("supply_toner_magenta", 25.0),
193199
Result(state=State.OK, summary="Supply: 25 of max. 100 tenths of milliliters"),
194-
Metric("pages", 25, levels=(20.0, 10.0), boundaries=(0, 100)),
195200
],
196201
),
197202
(
@@ -204,9 +209,9 @@ def test_inventory_printer_supply(
204209
],
205210
],
206211
[
207-
Result(state=State.OK, summary="Remaining: 25.00%"),
212+
Result(state=State.OK, summary="Supply level remaining: 25.00%"),
213+
Metric("supply_toner_magenta", 25.0),
208214
Result(state=State.OK, summary="Supply: 25 of max. 100 micrometers"),
209-
Metric("pages", 25, levels=(20.0, 10.0), boundaries=(0, 100)),
210215
],
211216
),
212217
(
@@ -219,9 +224,8 @@ def test_inventory_printer_supply(
219224
],
220225
],
221226
[
222-
Result(state=State.OK, summary="Remaining: 25.00%"),
223-
Result(state=State.OK, summary="Supply: 25 of max. 100"),
224-
Metric("pages", 25, levels=(20.0, 10.0), boundaries=(0, 100)),
227+
Result(state=State.OK, summary="Supply level remaining: 25.00%"),
228+
Metric("supply_toner_magenta", 25.0),
225229
],
226230
),
227231
(
@@ -234,9 +238,8 @@ def test_inventory_printer_supply(
234238
],
235239
],
236240
[
237-
Result(state=State.OK, summary="Remaining: 25.00%"),
238-
Result(state=State.OK, summary="Supply: 25 of max. 100%"),
239-
Metric("pages", 25, levels=(20.0, 10.0), boundaries=(0, 100)),
241+
Result(state=State.OK, summary="Supply level remaining: 25.00%"),
242+
Metric("supply_toner_magenta", 25.0),
240243
],
241244
),
242245
(
@@ -271,9 +274,8 @@ def test_inventory_printer_supply(
271274
],
272275
],
273276
[
274-
Result(state=State.OK, summary="Remaining: 25.00%"),
275-
Result(state=State.OK, summary="Supply: 25 of max. 100%"),
276-
Metric("pages", 25, levels=(20.0, 10.0), boundaries=(0, 100)),
277+
Result(state=State.OK, summary="Supply level remaining: 25.00%"),
278+
Metric("supply_toner_magenta", 25.0),
277279
],
278280
),
279281
(
@@ -287,9 +289,8 @@ def test_inventory_printer_supply(
287289
],
288290
],
289291
[
290-
Result(state=State.OK, summary="Remaining: 25.00%"),
291-
Result(state=State.OK, summary="Supply: 25 of max. 100%"),
292-
Metric("pages", 25, levels=(20.0, 10.0), boundaries=(0, 100)),
292+
Result(state=State.OK, summary="Supply level remaining: 25.00%"),
293+
Metric("supply_toner_other", 25.0),
293294
],
294295
),
295296
(
@@ -303,9 +304,8 @@ def test_inventory_printer_supply(
303304
],
304305
],
305306
[
306-
Result(state=State.OK, summary="Remaining: 25.00%"),
307-
Result(state=State.OK, summary="Supply: 25 of max. 100%"),
308-
Metric("pages", 25, levels=(20.0, 10.0), boundaries=(0, 100)),
307+
Result(state=State.OK, summary="Supply level remaining: 25.00%"),
308+
Metric("supply_toner_magenta", 25.0),
309309
],
310310
),
311311
(
@@ -318,9 +318,8 @@ def test_inventory_printer_supply(
318318
],
319319
],
320320
[
321-
Result(state=State.OK, summary="Remaining: 75.00%"),
322-
Result(state=State.OK, summary="Supply: 25 of max. 100%"),
323-
Metric("pages", 25, levels=(20.0, 10.0), boundaries=(0, 100)),
321+
Result(state=State.OK, summary="Supply level remaining: 75.00%"),
322+
Metric("supply_toner_magenta", 75.0),
324323
],
325324
),
326325
(
@@ -333,9 +332,8 @@ def test_inventory_printer_supply(
333332
],
334333
],
335334
[
336-
Result(state=State.OK, summary="Remaining: 75.00%"),
337-
Result(state=State.OK, summary="Supply: 25 of max. 100%"),
338-
Metric("pages", 25, levels=(20.0, 10.0), boundaries=(0, 100)),
335+
Result(state=State.OK, summary="Supply level remaining: 75.00%"),
336+
Metric("supply_toner_magenta", 75.0),
339337
],
340338
),
341339
(
@@ -348,9 +346,8 @@ def test_inventory_printer_supply(
348346
],
349347
],
350348
[
351-
Result(state=State.OK, summary="[magenta] Remaining: 75.00%"),
352-
Result(state=State.OK, summary="Supply: 25 of max. 100%"),
353-
Metric("pages", 25, levels=(20.0, 10.0), boundaries=(0, 100)),
349+
Result(state=State.OK, summary="Supply level remaining: 75.00%"),
350+
Metric("supply_toner_magenta", 75.0),
354351
],
355352
),
356353
],
@@ -364,3 +361,39 @@ def test_check_printer_supply(
364361
section = parse_printer_supply(info)
365362
result = check_printer_supply(item, params, section)
366363
assert list(result) == expected_result
364+
365+
366+
def _get_section_with_color_in_description(item: str) -> Section:
367+
max_capacity, supply_level, container_class = "0", "25", str(SupplyClass.CONTAINER)
368+
info = [[["1.1", ""]], [[item, "", max_capacity, supply_level, container_class, ""]]]
369+
return parse_printer_supply(info)
370+
371+
372+
@pytest.mark.parametrize("item", ["Black", "Schwarz", "Noir", "Negra"])
373+
def test_check_printer_supply_black_translations(item: str) -> None:
374+
section = _get_section_with_color_in_description(item)
375+
376+
_, value = list(check_printer_supply(item, DEFAULT_PARAMETERS, section))
377+
expected = Metric("supply_toner_black", 25.0)
378+
379+
assert value == expected
380+
381+
382+
@pytest.mark.parametrize("item", ["Cyan", "Zyan", "Cian"])
383+
def test_check_printer_supply_cyan_translations(item: str) -> None:
384+
section = _get_section_with_color_in_description(item)
385+
386+
_, value = list(check_printer_supply(item, DEFAULT_PARAMETERS, section))
387+
expected = Metric("supply_toner_cyan", 25.0)
388+
389+
assert value == expected
390+
391+
392+
@pytest.mark.parametrize("item", ["Yellow", "Gelb", "Jaune", "Amarilla"])
393+
def test_check_printer_supply_yellow_translations(item: str) -> None:
394+
section = _get_section_with_color_in_description(item)
395+
396+
_, value = list(check_printer_supply(item, DEFAULT_PARAMETERS, section))
397+
expected = Metric("supply_toner_yellow", 25.0)
398+
399+
assert value == expected

0 commit comments

Comments
 (0)