Skip to content

Commit fd062f0

Browse files
committed
fix: show comment for host notification results in log views
PainterLogComment.render() used a hardcoded threshold of 6 ";"-separated fields to decide whether a notification log line carries a trailing comment. That threshold only matches the layout of SERVICE notification log lines, which have an extra ";<service>" segment (7 fields with a comment). HOST notification log lines lack that segment, so even with a comment present they only ever reach 6 fields and never cross the "> 6" bar -- the comment column silently stays empty for every host notification, regardless of what the notification plugin wrote to stdout. Extract the threshold into _log_comment_min_fields(log_type), which returns 6 for service log types and 5 for host log types, and have PainterLogComment request the log_type column (already used by the neighboring PainterLogPluginOutput) to pick the right one.
1 parent cd9a44a commit fd062f0

2 files changed

Lines changed: 60 additions & 3 deletions

File tree

cmk/gui/painter/v0/painters.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5835,6 +5835,19 @@ def render(self, row: Row, cell: Cell, user: LoggedInUser) -> CellSpec:
58355835
return ("", row["log_options"])
58365836

58375837

5838+
def _log_comment_min_fields(log_type: str) -> int:
5839+
"""Minimum number of ";"-separated fields in a notification log line
5840+
before a trailing comment field is present.
5841+
5842+
Host notification log lines have one field fewer than service
5843+
notification log lines (they lack the ";<service>" segment), so the
5844+
threshold below which no comment field can be present differs between
5845+
the two. See cmk.events.log_to_history._format_notification_message,
5846+
which is the counterpart producing these log lines.
5847+
"""
5848+
return 6 if "SERVICE" in log_type else 5
5849+
5850+
58385851
class PainterLogComment(Painter):
58395852
@property
58405853
@override
@@ -5852,14 +5865,14 @@ def short_title(self, cell: Cell) -> str:
58525865
@property
58535866
@override
58545867
def columns(self) -> Sequence[ColumnName]:
5855-
return ["log_options"]
5868+
return ["log_options", "log_type"]
58565869

58575870
@override
58585871
def render(self, row: Row, cell: Cell, user: LoggedInUser) -> CellSpec:
58595872
msg = row["log_options"]
58605873
if ";" in msg:
58615874
parts = msg.split(";")
5862-
if len(parts) > 6:
5875+
if len(parts) > _log_comment_min_fields(row.get("log_type", "")):
58635876
return ("", parts[-1])
58645877
return ("", "")
58655878

tests/unit/cmk/gui/plugins/views/test_painters.py

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
from cmk.gui.http import request
2424
from cmk.gui.logged_in import user
2525
from cmk.gui.painter.v0 import all_painters
26-
from cmk.gui.painter.v0.painters import _paint_custom_notes
26+
from cmk.gui.painter.v0.painters import _log_comment_min_fields, _paint_custom_notes
2727
from cmk.gui.type_defs import ColumnSpec, DynamicIconName, Row
2828
from cmk.gui.utils.html import HTML
2929
from cmk.gui.utils.roles import UserPermissions
@@ -1655,3 +1655,47 @@ def test_paint_custom_notes_file_inclusion_and_html_tags(
16551655
expected_string = str(HTML.without_escaping("<hr>".join(expected_notes)))
16561656

16571657
assert expected_string == notes_as_string
1658+
1659+
1660+
@pytest.mark.parametrize(
1661+
"log_type, log_options, expected_comment",
1662+
[
1663+
# Host notification log lines have no ";<service>" segment, so with
1664+
# a comment present they only ever reach 6 fields, not 7.
1665+
pytest.param(
1666+
"HOST NOTIFICATION RESULT",
1667+
"cmkadmin;myhost;CRITICAL;my_plugin;some output;the comment",
1668+
"the comment",
1669+
id="host-with-comment",
1670+
),
1671+
pytest.param(
1672+
"HOST NOTIFICATION RESULT",
1673+
"cmkadmin;myhost;CRITICAL;my_plugin;some output",
1674+
"",
1675+
id="host-without-comment",
1676+
),
1677+
pytest.param(
1678+
"SERVICE NOTIFICATION RESULT",
1679+
"cmkadmin;myhost;My Service;CRITICAL;my_plugin;some output;the comment",
1680+
"the comment",
1681+
id="service-with-comment",
1682+
),
1683+
pytest.param(
1684+
"SERVICE NOTIFICATION RESULT",
1685+
"cmkadmin;myhost;My Service;CRITICAL;my_plugin;some output",
1686+
"",
1687+
id="service-without-comment",
1688+
),
1689+
],
1690+
)
1691+
def test_log_comment_field_count(
1692+
log_type: str, log_options: str, expected_comment: str
1693+
) -> None:
1694+
# Regression test for the comment field being silently dropped for host
1695+
# notifications: PainterLogComment.render used a hardcoded threshold of
1696+
# 6 fields that only matches the (one field longer) service notification
1697+
# log line layout.
1698+
parts = log_options.split(";")
1699+
min_fields = _log_comment_min_fields(log_type)
1700+
actual_comment = parts[-1] if len(parts) > min_fields else ""
1701+
assert actual_comment == expected_comment

0 commit comments

Comments
 (0)