-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathteams-notifications-command.conf
149 lines (137 loc) · 7.73 KB
/
teams-notifications-command.conf
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
object NotificationCommand "teams-notifications-command" {
import "plugin-notification-command"
command = {{
var teams_webhook_url = macro("$teams_notifications_webhook_url$")
if ( teams_webhook_url == null) {
log(LogDebug, "teams-notifications","webhook_url not set, aborting...")
return
}
log(LogDebug, "teams-notifications", "Sending notification...reading user configuration")
var plugin_output_max_length_from_config = macro("$teams_notifications_plugin_output_max_length$")
var plugin_output_max_length = Math.round(plugin_output_max_length_from_config)
var teams_channel = macro("$teams_notifications_channel$")
var teams_botname = macro("$teams_notifications_botname$")
var icinga2_base_url = macro("$teams_notifications_icinga2_base_url$")
var teams_icon_dictionary = macro("$teams_notifications_icon_dictionary$")
var teams_color = macro("$teams_notifications_color$")
var teams_footer_text = macro("$teams_footer_text$")
var configuration = {
"vars.teams_notifications_plugin_output_max_length" = plugin_output_max_length,
"vars.teams_notifications_webhook_url" = teams_webhook_url,
"vars.teams_notifications_channel" = teams_channel,
"vars.teams_notifications_botname" = teams_botname,
"vars.teams_notifications_icinga2_base_url" = icinga2_base_url
}
log(LogDebug, "teams-notifications", "Sending notification...read user configuration successfully: " + Json.encode(configuration))
log(LogDebug, "teams-notifications", "Sending notification...reading notification data")
var notification_type = macro("$notification.type$")
var notification_author = macro("$notification.author$")
var notification_comment = macro("$notification.comment$")
var icinga_long_date_time = macro("$icinga.long_date_time$")
var service_name = macro("$service.name$")
var service_display_name = macro("$service.display_name$")
var service_state = macro("$service.state$")
var service_duration_sec = macro("$service.duration_sec$")
var service_check_attempt = macro("$service.check_attempt$")
var service_last_state = macro("$service.last_state$")
var service_output = macro("$service.output$")
var host_name = macro("$host.name$")
var host_display_name = macro("$host.display_name$")
var host_state = macro("$host.state$")
var host_duration_sec = macro("$host.duration_sec$")
var host_check_attempt = macro("$host.check_attempt$")
var host_last_state = macro("$host.last_state$")
var host_output = macro("$host.output$")
var notification_data = {
"notification.type" = notification_type,
"notification.author" = notification_author,
"notification.comment" = notification_comment,
"icinga.long_date_time" = icinga_long_date_time,
"service.name" = service_name,
"service.display_name" = service_display_name,
"service.state" = service_state,
"service.check_attempt" = service_check_attempt,
"service.last_state" = service_last_state,
"service.duration_sec" = service_duration_sec,
"service.output" = service_output,
"host.name" = host_name,
"host.display_name" = host_display_name,
"host.state" = host_state,
"host.check_attempt" = host_check_attempt,
"host.last_state" = host_last_state,
"host.duration_sec" = host_duration_sec,
"host.output" = host_output,
}
log(LogDebug, "teams-notifications", "Sending notification...read notification data successfully: " + Json.encode(notification_data))
log(LogDebug, "teams-notifications", "Sending notification...choosing color")
var notification_type_custom_text = ""
if (notification_type == "CUSTOM" || notification_type == "ACKNOWLEDGEMENT") {
notification_type_custom_text = "{\"name\":\"Comment\",\"value\":\"" + notification_comment + " by " + notification_author + "\"},"
}
if(service_name != null) {
var color = teams_color.get(service_state)
} else {
var color = teams_color.get(host_state)
}
log(LogDebug, "teams-notifications", "Sending notification...chose color successfully: " + color)
function ReadableDuration(seconds){
if(seconds <= 60) {
return Math.round(seconds) + " seconds"
} else if(seconds <= 3600) {
return Math.round(seconds / 60) + " minutes"
} else if(seconds <= 86400) {
return Math.round(seconds / 3600) + " hours"
} else {
return Math.round(seconds / 86400) + " days"
}
}
log(LogDebug, "teams-notifications", "Sending notification...generating notification text")
var plugin_output = host_output.substr(0, plugin_output_max_length)
var host_name_with_link = "[" + host_display_name + "](" + icinga2_base_url + "/monitoring/host/show?host=" + host_name + ")"
var text = "error crafting payload"
var state_duration = ""
var service_details = ""
if(service_name != null) {
# Notification is for a service
var service_name_with_link = "[" + service_display_name + "](" + icinga2_base_url + "/monitoring/service/show?host=" + host_name + "&service=" + service_name.replace(" ", "%20") + ")"
var plugin_output = service_output.substr(0, plugin_output_max_length)
var state_text = "Transitioned from " + service_last_state + " to " + service_state
var fallback_text = notification_type + ": " + service_name + " is " + service_state + " on " + host_name
if(service_last_state == service_state) {
state_text = "Is still in " + service_state
var service_duration_readable = ReadableDuration(service_duration_sec)
var state_duration = "{\"name\":\"Duration\",\"value\":\"" + service_duration_readable + "\"},"
}
var service_details = "{\"name\":\"Service\",\"value\":\"" + service_name_with_link + "\"},"
} else {
# Notification is for a host
var state_text = "Transitioned from " + host_last_state + " to " + host_state
var fallback_text = notification_type + ": " + host_name + " is " + host_state
if(host_last_state == host_state) {
state_text = "Is still " + host_state
var host_duration_readable = ReadableDuration(host_duration_sec)
var state_duration = "{\"name\":\"Duration\",\"value\":\"" + host_duration_readable + "\"},"
}
}
plugin_output_escaped = plugin_output.replace("\"", "\\\"").replace("\\", "\\\\")
payload_attachments = "{\"@type\":\"MessageCard\",\"themeColor\":\"" + color + "\",\"summary\":\"" + fallback_text + "\",\"sections\":[{\"activityTitle\":\"" + fallback_text + "\",\"facts\":[{\"name\":\"Host\",\"value\":\"" + host_name_with_link + "\"}," + service_details + "{\"name\":\"State\",\"value\":\"" + state_text + "\"}," + state_duration + notification_type_custom_text + "{\"name\":\"Plugin output\",\"value\":\"```" + plugin_output_escaped + "```\"}],\"markdown\":true}]}"
log(LogDebug, "teams-notifications", "Sending notification...generated notification text successfully: " + payload_attachments)
log(LogDebug, "teams-notifications", "Generating notification command")
var payload_argument = escape_shell_arg(payload_attachments)
var teams_webhook_url_argument = escape_shell_arg(teams_webhook_url)
var cmd = [
"curl",
"--fail",
"--connect-timeout", "30",
"--max-time", "60",
"--silent", "--show-error",
"--header", "'Content-Type: application/json'",
"-X", "POST",
"--data", payload_argument,
teams_webhook_url_argument
]
var notification_command = cmd.join(" ")
log(LogDebug, "teams-notifications", "Generated notification command successfully: " + notification_command)
return notification_command
}}
}