statuspage-notifications-command.conf 3.72 KB
Newer Older
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
object NotificationCommand "statuspage-notifications-command" {
  import "plugin-notification-command"
    
  command = {{

    log(LogDebug, "statuspage-notifications", "Sending notification...reading user configuration")
    var statuspage_api_url = macro("$statuspage_notifications_api_url$")
    var statuspage_api_key = macro("$statuspage_notifications_api_key$")
    var component_map = macro("$statuspage_notifications_component_dictionary$")
    var configuration = {
        "vars.statuspage_notifications_api_url" = statuspage_api_url,
        "vars.statuspage_notifications_api_key" = statuspage_api_key,
        "vars.statuspage_notifications_component_dictionary" = component_map
    }
    log(LogDebug, "statuspage-notifications", "Sending notification...read user configuration successfully: " + Json.encode(configuration))

    log(LogDebug, "statuspage-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$")
28
    var service_component_name = macro("$service.vars.statuspage_notifications_component$")
29 30 31 32 33 34 35 36 37 38 39
    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,
40
        "service.vars.statuspage_notifications_component" = service_component_name,
41 42 43
    }
    log(LogDebug, "statuspage-notifications", "Sending notification...read notification data successfully: " + Json.encode(notification_data))

44 45
    log(LogDebug, "statuspage-notifications", "Sending notification...generating component state")
    var component_status = "operational"
Ryan Lee's avatar
Ryan Lee committed
46
    if(service_state != "OK") {
47
        component_status = "major_outage"
48
    }
49 50
    var component_id = component_map[service_component_name]
    log(LogDebug, "statuspage-notifications", "Sending notification...generated component successfully: " + component_status)
51 52

    log(LogDebug, "statuspage-notifications", "Generating notification command")
53 54 55 56
    var statuspage_payload = "component[status]=" + component_status
    var payload_argument = escape_shell_arg(statuspage_payload)
    var statuspage_api_key_argument = escape_shell_arg(statuspage_api_key)
    var statuspage_api_url_argument = escape_shell_arg(statuspage_api_url + "/components/" + component_id + ".json")
57 58 59 60 61 62
    var cmd = [
                "curl",
                "--fail",
                "--connect-timeout", "30",
                "--max-time", "60",
                "--silent", "--show-error",
63 64 65 66
                "-X", "PATCH",
                "-H", "'Authorization: OAuth " + statuspage_api_key_argument + "'",
                "-d", "component[status]=" + payload_argument,
                statuspage_api_url_agrument
67 68 69 70 71 72 73
    ]
    var notification_command = cmd.join(" ")
    log(LogDebug, "statuspage-notifications", "Generated notification command successfully: " + notification_command)

    return notification_command
  }}
}