Icinga2 - Publish your Icinga2 notification on Twitter

You can also tweet your Icinga2 notification on your Twitter account. This notification method can run additionally to your existing notification methods such as e-mail, etc.

You have to create a Twitter account if you do not have one. You can use your normal Twitter account and you do not need a developer account or any special type of an account.

Requirements

Ubuntu is used as an operating system in this tutorial. You need to have Icinga2 installed and configured. This tutorial deals only on how to install and configure an Icinga2 to post its notifications also through tweets on Twitter.

Install and configure Twidge

This program is available on the official repostitory of Ubuntu and can be installed through the "apt" package manager.

apt-get install twidge


Now you have to run the setup. This is done with this command.

twidge setup

Follow the instructions of this program. Then you have to login in your Twitter account that you want use and open the URL that is shown in this program . You have to authorize access to your twitter account, that is why you need to this authorization process.

Now you have to copy the configuration file of this program "twidge" to your icinga2 folder.

sudo cp ~/.twidgerc /etc/icinga2/twidgerc

You can delete the original configuration file "~/.twidgerc", if you do not need it anymore.
But the configuration file /etc/icinga2/twidgerc (which is used here - you can also change another path) will be used by "twidgerc" to create tweets from your Icinga2 notification.


Create notification scripts for Icinga2

Then change to the scripts directory of your Icinga2 folder and create the notification scripts for host and service notification. You have to remember that you have to keep your message in the "echo" command short, because you have a character limit of 140 characters on your tweets.

Create host notification shell script

vim /etc/icinga2/scripts/twitter-host-notification.sh
#!/bin/sh
/bin/echo "#$NOTIFICATIONTYPE - SERVER #$HOSTALIAS is $HOSTSTATE." | twidge -c /etc/icinga2/twidgerc update


Create service notification shell script

vim /etc/icinga2/scripts/twitter-service-notification.sh
#!/bin/sh
/bin/echo "#$NOTIFICATIONTYPE - SERVICE $SERVICEDISPLAYNAME on Server $HOSTALIAS is $SERVICESTATE." | twidge -c /etc/icinga2/twidgerc update


Now the created notification shell scripts have to get the correct file permission.

chmod +x /etc/icinga2/scripts/twitter-service-notification.sh
chmod +x /etc/icinga2/scripts/twitter-host-notification.sh


Configure notifications on Icinga2

Here in this tutorial we will use the admin account "icingaadmin" as an example.

1. Add a NotificationCommand for both the host and service notification.

vim /etc/icinga2/conf.d/commands.conf
object NotificationCommand "twitter-host-notification" {
  import "plugin-notification-command"

  command = [ SysconfDir + "/icinga2/scripts/twitter-host-notification.sh" ]

  env = {
    NOTIFICATIONTYPE = "$notification.type$"
    HOSTNAME = "$host.name$"
    HOSTALIAS = "$host.display_name$"
    HOSTADDRESS = "$address$"
    HOSTSTATE = "$host.state$"
    LONGDATETIME = "$icinga.long_date_time$"
    HOSTOUTPUT = "$host.output$"
    NOTIFICATIONAUTHORNAME = "$notification.author$"
    NOTIFICATIONCOMMENT = "$notification.comment$"
    HOSTDISPLAYNAME = "$host.display_name$"
    }
}

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

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

  env = {
    NOTIFICATIONTYPE = "$notification.type$"
    SERVICEDESC = "$service.name$"
    HOSTNAME = "$host.name$"
    HOSTALIAS = "$host.display_name$"
    HOSTADDRESS = "$address$"
    SERVICESTATE = "$service.state$"
    LONGDATETIME = "$icinga.long_date_time$"
    SERVICEOUTPUT = "$service.output$"
    NOTIFICATIONAUTHORNAME = "$notification.author$"
    NOTIFICATIONCOMMENT = "$notification.comment$"
    HOSTDISPLAYNAME = "$host.display_name$"
    SERVICEDISPLAYNAME = "$service.display_name$"
  }
}


2. Open the file /etc/icinga2/conf.d/templates.conf
In this file there are the templates of your hosts and services which should look similiar like this:

template Host "generic-host" {

[...]

}


Add this variable to all the templates ("Host", "Service"), that you use:

vars.notification["twitter"] = {
    users = [ "icingaadmin" ]
}

You have to add also the templates for the notifications in this same file.
Which looks like this:

template Notification "twitter-host-notification" {
  command = "twitter-host-notification"

  states = [ Up, Down ]
  types = [ Problem, Acknowledgement, Recovery, Custom ]
vars.notification_interval = 0

  period = "24x7"
}

/**
 * Provides default settings for service notifications.
 * By convention all service notifications should import
 * this template.
 */
template Notification "twitter-service-notification" {
  command = "twitter-service-notification"
  vars.notification_interval = 0

  states = [ OK, Warning, Critical, Unknown ]
  types = [ Problem, Acknowledgement, Recovery, Custom ]

  period = "24x7"
}

You need to add now the notification to your services and hosts. You can do that easily with the command "apply Notification".

3. Add this to the file /etc/icinga2/conf.d/notifications.conf

apply Notification "twitter-icingaadmin" to Host {
  import "mail-host-notification"
  command = "twitter-host-notification"

  users = [ "icingaadmin" ]
  interval = 0 //disable re-notification

  assign where host.vars.notification.twitter

}

apply Notification "twitter-icingaadmin" to Service {
  import "mail-service-notification"
  command = "twitter-service-notification"
  interval = 0 //disable re-notification

  users = [ "icingaadmin" ]

  assign where host.vars.notification.twitter

}


4. And finally, the configuration file of the program "twidgerc" should get the right file permission.

chmod 644 /etc/icinga2/twidgerc
chown nagios:nagios /etc/icinga2/twidgerc


Now restart Icinga2 and you are done.

You can also customize for example the in this tutorial created scripts in the folder "/etc/icinga2/scripts/" to run different commands according according to the service state of a service. The state of a service can be checked through the variable "$SERVICESTATE".

This was an example on how to setup Icinga2 notifications for Twitter.

 

Cookies make it easier for us to provide you with our services. With the usage of our services you permit us to use cookies.
Ok