Skip to content

Commit

Permalink
HTML Email: Fix Show notification rule that triggered the notification
Browse files Browse the repository at this point in the history
CMK-21092

Change-Id: I3e0377e0821b2bef74a8a04ff4a91458424632fe
  • Loading branch information
makanakoeln committed Jan 10, 2025
1 parent 90a65b5 commit 2514a53
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 12 deletions.
37 changes: 25 additions & 12 deletions cmk/base/notify.py
Original file line number Diff line number Diff line change
Expand Up @@ -752,7 +752,9 @@ def notify_rulebased(
num_rule_matches = 0
rule_info = []

for rule in itertools.chain(rules, user_notification_rules(config_contacts=config_contacts)):
for nr, rule in enumerate(
itertools.chain(rules, user_notification_rules(config_contacts=config_contacts))
):
contact_info = _get_contact_info_text(rule)

why_not = rbn_match_rule(
Expand Down Expand Up @@ -780,6 +782,7 @@ def notify_rulebased(
host_parameters_cb,
config_contacts=config_contacts,
fallback_email=fallback_email,
rule_nr=nr,
)

plugin_info = _process_notifications(
Expand Down Expand Up @@ -817,6 +820,7 @@ def _create_notifications(
*,
config_contacts: ConfigContacts,
fallback_email: str,
rule_nr: int,
) -> tuple[Notifications, list[NotifyRuleInfo]]:
contacts = rbn_rule_contacts(
rule,
Expand Down Expand Up @@ -886,11 +890,13 @@ def _create_notifications(
else plugin_parameter_id
)

final_parameters = rbn_finalize_plugin_parameters(
HostName(enriched_context["HOSTNAME"]),
plugin_name,
host_parameters_cb,
plugin_parameters,
final_parameters: NotifyPluginParamsDict = _rbn_finalize_plugin_parameters(
hostname=HostName(enriched_context["HOSTNAME"]),
plugin_name=plugin_name,
host_parameters_cb=host_parameters_cb,
rule_parameters=plugin_parameters,
rule_matching_nr=rule_nr,
rule_matching_text=rule["description"],
)
notifications[key] = (not rule.get("allow_disable"), final_parameters, bulk)

Expand Down Expand Up @@ -930,11 +936,13 @@ def _process_notifications(
logger.info(" Sending email to %s", fallback_emails)

plugin_name, fallback_params = fallback_format
fallback_params = rbn_finalize_plugin_parameters(
HostName(enriched_context["HOSTNAME"]),
plugin_name,
host_parameters_cb,
fallback_params,
fallback_params = _rbn_finalize_plugin_parameters(
hostname=HostName(enriched_context["HOSTNAME"]),
plugin_name=plugin_name,
host_parameters_cb=host_parameters_cb,
rule_parameters=fallback_params,
rule_matching_nr=-1,
rule_matching_text="No rule matched",
)
plugin_context = create_plugin_context(
enriched_context, fallback_params, get_http_proxy
Expand Down Expand Up @@ -1038,11 +1046,13 @@ def rbn_fallback_contacts(*, config_contacts: ConfigContacts, fallback_email: st
return fallback_contacts


def rbn_finalize_plugin_parameters(
def _rbn_finalize_plugin_parameters(
hostname: HostName,
plugin_name: NotificationPluginNameStr,
host_parameters_cb: Callable[[HostName, NotificationPluginNameStr], Mapping[str, object]],
rule_parameters: NotifyPluginParamsDict,
rule_matching_nr: int,
rule_matching_text: str,
) -> NotifyPluginParamsDict:
parameters = dict(host_parameters_cb(hostname, plugin_name)).copy()
parameters.update(rule_parameters)
Expand All @@ -1052,6 +1062,9 @@ def rbn_finalize_plugin_parameters(
if plugin_name == "mail":
parameters.setdefault("graphs_per_notification", 5)
parameters.setdefault("notifications_with_graphs", 5)
# Added in 2.4.0 for HTML Mail templates
parameters.setdefault("matching_rule_nr", rule_matching_nr)
parameters.setdefault("matching_rule_text", rule_matching_text)

return parameters

Expand Down
3 changes: 3 additions & 0 deletions notifications/templates/mail/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,9 @@
{% include 'host_tags.html' %}
{% endif %}
{% if href %}{{ macros.view_issue_button(href) }}{% endif %}
{% if data.PARAMETER_NOTIFICATION_RULE %}
{% include 'matching_rule.html' %}
{% endif %}
<td>
</tr>
{{ macros.margin_tr() }}
Expand Down

0 comments on commit 2514a53

Please sign in to comment.