SMS notifications in icinga2 with sms77.de

Posted by Markus Benning on December 29, 2014

The sms77.de service could be used to send SMS notifications from icinga2.

First create a service account at sms77.de and install the sms77send command which comes with the SMS::SMS77 perl module:

cpanm SMS::SMS77

You may want to send a test message to your mobile:

$ sms77send --user <user> --password <password> --type basicplus --to <number>
test
<press CTRL-D>

To be able to call sms77send from icinga2 you need to place the following wrapper script in /etc/icinga2/scripts/sms77-service-notification.sh:

#!/usr/bin/env bash

template=$(cat <<TEMPLATE
[$NOTIFICATIONTYPE] $HOSTALIAS - $SERVICEDESC is $SERVICESTATE
$DATETIME
$SERVICEOUTPUT
TEMPLATE
)

/usr/bin/printf "%b" "$template" | /usr/bin/head -c 160 | /usr/bin/sms77send --user <user> --password <password> --type basicplus --to $USERPAGER</pre>

Place the following command definition in /etc/icinga2/conf.d/commands.conf:

object NotificationCommand "sms77-service-notification" {
  import "plugin-notification-command"

  command = [ SysconfDir + "/icinga2/scripts/sms77-service-notification.sh" ]

  env = {
    NOTIFICATIONTYPE = "$notification.type$"
    SERVICEDESC = "$service.name$"
    HOSTALIAS = "$host.display_name$"
    SERVICESTATE = "$service.state$"
    DATETIME = "$icinga.date_time$"
    SERVICEOUTPUT = "$service.output$"
    USERPAGER = "$user.pager$"
  }
}

Place a template for the notification in /etc/icinga2/conf.d/templates.conf:

template Notification "sms77-service-notification" {
  command = "sms77-service-notification"

  states = [ OK, Warning, Critical, Unknown ]
  types = [ Problem, Acknowledgement, Recovery, Custom,
            FlappingStart, FlappingEnd,
            DowntimeStart, DowntimeEnd, DowntimeRemoved ]

  period = "24x7"
  interval = 0
}

And the notification in /etc/icinga2/conf.d/notifications.conf:

apply Notification "sms77-icingaadmin" to Service {
  import "sms77-service-notification"

  user_groups = host.vars.notification.pager.groups

  assign where host.vars.notification.pager
}

To activate SMS notifications for a host add the following lines to your host definition in /etc/icings2/conf.d/hosts.conf:

vars.notification["pager"] = {
  groups = [ "icingaadmins" ]
}

And your mobile phone number to your contact in /etc/icinga2/conf.d/users.conf (eg. within object User icingaadmin):

pager = "0049<your phone number>"