Sending notifications to Amazon SNS

This is a prototype of sending notifications directly to Amazon SNS via the CLI utility.

You would of course need to setup IAM for the EC2 instance first, wasn’t able to document this so far.

But at least it is a good way to show how to serialize JSON directly in Icinga 2 - unfortunately this is partially hindered by a bug #8722, but it should work like this.

object NotificationCommand "aws-sns" {
    import "plugin-notification-command"
    command = [ "/usr/bin/aws", "sns", "publish" ]
    arguments += {
        "--message" = {{
            // Load the host, because we can't use macro()
            var host = get_host(notification.host_name)

            var message = {
              subject = ""
              description = ""
              level = 3
            }

            var state_id = host.state

            if (notification.service_name) {
              var stateMap = ["OK", "WARNING", "CRITICAL", "UNKNOWN"]
              var service = get_service(notification.host_name, notification.service_name)

              message.subject = "ICINGA : Service Alert " + host.name + " : " + service.name + " is " + stateMap[service.state]
              message.description = service.last_check_result.output
              state_id = service.state_raw
            } else {
              var stateMap = ["UP", "DOWN"]
              message.subject = "ICINGA : Host Alert " + host.name + " is " + stateMap[host.state]
              message.description = host.last_check_result.output
            }

            // Higher level when critical
            if (state_id == 2) {
              message.level = 2
            }
            
            return Json.encode(message)
        }}
        "--region" = "$aws_region$"
        "--topic-arn" = "arn:aws:sns:$aws_region$:$aws_account_id$:$aws_sns_topic$"
    }
}