object NotificationCommand "slack-notifications-command" { import "plugin-notification-command" command = {{ log(LogDebug, "slack-notifications", "Sending notification...reading user configuration") var plugin_output_max_length_from_config = macro("$slack_notifications_plugin_output_max_length$") var plugin_output_max_length = Math.round(plugin_output_max_length_from_config) var slack_webhook_url = macro("$slack_notifications_webhook_url$") var slack_channel = macro("$slack_notifications_channel$") var slack_botname = macro("$slack_notifications_botname$") var icinga2_base_url = macro("$slack_notifications_icinga2_base_url$") var slack_icon_dictionary = macro("$slack_notifications_icon_dictionary$") var configuration = { "vars.slack_notifications_plugin_output_max_length" = plugin_output_max_length, "vars.slack_notifications_webhook_url" = slack_webhook_url, "vars.slack_notifications_channel" = slack_channel, "vars.slack_notifications_botname" = slack_botname, "vars.slack_notifications_icinga2_base_url" = icinga2_base_url "vars.slack_notifications_icon_dictionary" = slack_icon_dictionary } log(LogDebug, "slack-notifications", "Sending notification...read user configuration successfully: " + Json.encode(configuration)) log(LogDebug, "slack-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_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_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.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.output" = host_output, } log(LogDebug, "slack-notifications", "Sending notification...read notification data successfully: " + Json.encode(notification_data)) log(LogDebug, "slack-notifications", "Sending notification...choosing icon") var notification_type_custom_text = "" if(notification_type == "CUSTOM"){ // notification_type_custom_text = "(Author: " + notification_author + ", Message: " + notification_comment + ")" notification_type_custom_text = "(Machine: " + host_name + ", Author: " + notification_author + ", Message: " + notification_comment + ")" } var icon = slack_icon_dictionary.get(notification_type) log(LogDebug, "slack-notifications", "Sending notification...chose icon successfully: " + icon) log(LogDebug, "slack-notifications", "Sending notification...generating notification text") var short_host_output = host_output.substr(0, plugin_output_max_length) var host_name_with_link = "<" + icinga2_base_url + "/monitoring/host/show?host=" + host_name + "|" + host_display_name + ">" var text = "error crafting payload" if(service_name != null) { var service_name_with_link = "<" + icinga2_base_url + "/monitoring/service/show?host=" + host_name + "&service=" + service_name + "|" + service_display_name + ">" var short_service_output = service_output.substr(0, plugin_output_max_length) var service_state_text = " transitioned from state " + service_last_state + " to state " + service_state if(service_last_state == service_state) { service_state_text = " is still in state " + service_state } text = ":" + icon + ": " + notification_type + notification_type_custom_text + ": Service " + service_name_with_link + service_state_text + " on host " + host_name_with_link + ". Plugin output: ```" + short_service_output + "```" } else { var host_state_text = " transitioned from state " + host_last_state + " to state " + host_state if(host_last_state == host_state) { host_state_text = " is still in state " + host_state } text = ":" + icon + ": " + notification_type + notification_type_custom_text + ": Host " + host_name_with_link + host_state_text + ". Plugin output: ```" + short_host_output + "```" } log(LogDebug, "slack-notifications", "Sending notification...generated notification text successfully: " + text) log(LogDebug, "slack-notifications", "Generating notification command") var payload_dictionary = { "channel" = slack_channel, "username" = slack_botname, "text" = text } var payload_json = Json.encode(payload_dictionary) var slack_payload = "payload=" + payload_json var payload_argument = escape_shell_arg(slack_payload) var slack_webhook_url_argument = escape_shell_arg(slack_webhook_url) var cmd = [ "curl", "--fail", "--connect-timeout", "30", "--max-time", "60", "--silent", "--show-error", "-X", "POST", "--data-urlencode", payload_argument, slack_webhook_url_argument ] var notification_command = cmd.join(" ") log(LogDebug, "slack-notifications", "Generated notification command successfully: " + notification_command) return notification_command }} }