-
Notifications
You must be signed in to change notification settings - Fork 554
Expand file tree
/
Copy pathmsteams.py
More file actions
200 lines (180 loc) · 6.95 KB
/
Copy pathmsteams.py
File metadata and controls
200 lines (180 loc) · 6.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
#!/usr/bin/env python3
# Copyright (C) 2022 Checkmk GmbH - License: GNU General Public License v2
# This file is part of Checkmk (https://checkmk.com). It is subject to the terms and
# conditions defined in the file COPYING, which is part of this source code package.
from collections.abc import Iterable
from cmk.notification_plugins.utils import (
host_url_from_context,
post_request,
process_by_status_code,
service_url_from_context,
substitute_context,
)
from cmk.utils.ms_teams_constants import (
ms_teams_tmpl_host_details,
ms_teams_tmpl_host_summary,
ms_teams_tmpl_host_title,
ms_teams_tmpl_svc_details,
ms_teams_tmpl_svc_summary,
ms_teams_tmpl_svc_title,
)
from cmk.utils.notify_types import PluginNotificationContext
MAP_TYPES: dict[str, str] = {
"PROBLEM": "Problem notification",
"RECOVERY": "Recovery notification",
"DOWNTIMESTART": "Start of downtime",
"DOWNTIMEEND": "End of downtime",
"DOWNTIMECANCELLED": "Downtime cancelled",
"ACKNOWLEDGEMENT": "Problem acknowledged",
}
EMOJIS: dict[str, str] = {
"OK": "🟢",
"UP": "🟢",
"WARNING": "🟡",
"CRITICAL": "🔴",
"DOWN": "🔴",
"UNKNOWN": "⚪",
"ACKNOWLEDGEMENT": "✅",
"DOWNTIMESTART": "🕒",
"DOWNTIMEEND": "🕒",
"DOWNTIMECANCELLED": "🕒",
}
def _msteams_msg(
context: PluginNotificationContext,
) -> dict[str, object]:
title, summary, details, subtitle = _get_text_fields(context, notify_what := context["WHAT"])
state = context.get("SERVICESTATE", context.get("HOSTSTATE", ""))
emoji = EMOJIS.get(state, "")
title_with_emoji = f"{emoji} {substitute_context(title, context)}"
actions = []
if info_url := (
service_url_from_context(context)
if notify_what == "SERVICE"
else host_url_from_context(context)
):
actions.append(
{
"type": "Action.OpenUrl",
"title": f"View {notify_what.lower()} details in Checkmk",
"url": info_url,
"role": "Button",
}
)
return {
"type": "message",
"attachments": [
{
"contentType": "application/vnd.microsoft.card.adaptive",
"contentUrl": "null",
"content": {
"$schema": "http://adaptivecards.io/schemas/adaptive-card.json",
"type": "AdaptiveCard",
"version": "1.3",
"body": [
{
"type": "TextBlock",
"text": title_with_emoji,
"weight": "bolder",
"size": "large",
"style": "heading",
"wrap": True,
},
{
"type": "TextBlock",
"text": substitute_context(subtitle, context),
"weight": "bolder",
"wrap": True,
},
{
"type": "TextBlock",
"text": substitute_context(summary, context),
"wrap": True,
},
{
"type": "ColumnSet",
"separator": True,
"columns": [
{
"type": "Column",
"width": "auto",
"items": [
{
"type": "TextBlock",
"text": "Details",
"wrap": True,
"weight": "bolder",
}
],
},
{
"type": "Column",
"width": "stretch",
"items": list(_get_details(context, details)),
},
],
},
*_get_section_facts(context),
],
"actions": actions,
"msteams": {
"width": "Full",
},
},
},
],
}
def _get_text_fields(
context: PluginNotificationContext,
notify_what: str,
) -> tuple[str, str, str, str]:
subtitle: str = MAP_TYPES.get(context["NOTIFICATIONTYPE"], "")
if notify_what == "SERVICE":
return (
context.get("PARAMETER_SERVICE_TITLE", ms_teams_tmpl_svc_title()),
context.get("PARAMETER_SERVICE_SUMMARY", ms_teams_tmpl_svc_summary()),
context.get("PARAMETER_SERVICE_DETAILS", ms_teams_tmpl_svc_details()),
subtitle,
)
return (
context.get("PARAMETER_HOST_TITLE", ms_teams_tmpl_host_title()),
context.get("PARAMETER_HOST_SUMMARY", ms_teams_tmpl_host_summary()),
context.get("PARAMETER_HOST_DETAILS", ms_teams_tmpl_host_details()),
subtitle,
)
def _get_details(context: PluginNotificationContext, details: str) -> Iterable[dict[str, object]]:
full_details = substitute_context(details, context).replace("\\n", "\n\n")
add_separator = False
for segment in full_details.split("\n\n"):
if not segment.strip():
add_separator = True
continue
if add_separator:
yield {"type": "TextBlock", "text": segment, "wrap": True, "separator": True}
add_separator = False
else:
yield {"type": "TextBlock", "text": segment, "wrap": True, "spacing": "none"}
def _get_section_facts(context: PluginNotificationContext) -> Iterable[dict[str, object]]:
section_facts = []
if "PARAMETER_AFFECTED_HOST_GROUPS" in context and (groups := context.get("HOSTGROUPNAMES")):
section_facts.append({"title": "Affected host groups", "value": groups})
if author := context.get("NOTIFICATIONAUTHOR"):
section_facts.append({"title": "Author", "value": author})
if comment := context.get("NOTIFICATIONCOMMENT"):
section_facts.append({"title": "Comment", "value": comment})
if section_facts:
yield {
"type": "FactSet",
"facts": section_facts,
"separator": True,
}
def main() -> int:
try:
response = post_request(_msteams_msg)
if response.status_code in (200, 201, 202):
return 0
else:
print(f"Error: Status {response.status_code} - Response: {response.text}")
return 1
except Exception as e:
print(f"Exception while sending notification: {e}")
return 2