3

I would like to share with you a way to send notifications from icinga2 to cachet via the API.

Icinga2 version : 2.4.10-1
Cachet version : 2.3.9

First of all, you have to know which component ID you want to use (in my case, because you can update component by name)

To get the component ID, you can use the curl command :

curl --insecure --request GET --url https://URL/api/v1/components -H "X-Cachet-Token: TOKEN"

URL : The URL of your cachet installation
TOKEN : The Token of the member in Cachet

Create command in /etc/icinga2/conf.d/commands.conf

object NotificationCommand "cachet-incident-notification-v2" {
    import "plugin-notification-command"

    command = [ PluginDir + "/cachet-notification-v2.sh" ]

    env = {
      "SERVICESTATE" = "$service.state$"
    }
}

Create notification template in /etc/icinga2/conf.d/templates.conf

template Notification "cachet-incident-notification-v2" {
  command = "cachet-incident-notification-v2"

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

 /*
 period = "24x7"
*/
  interval = 0
}

Create notification in /etc/icinga2/conf.d/notifications.conf

apply Notification "cachet-incident-notification-v2" to Service {
  import "cachet-incident-notification-v2"

  user_groups = host.vars.notification.pager.groups

  assign where service.vars.cachetv2 == "1" && host.vars.cachetv2 == "1"

  interval = 0 # Disable Re-notification
}

Add variable in your check service in /etc/icinga2/conf.d/service/your/service.conf

[...]    
vars.cachetv2 = "1"
[...]

Add variable in your host config file in /etc/icinga2/conf.d/hosts/your/host

[...]    
vars.cachetv2 = "1"
[...]

Create the script in /usr/lib/nagios/plugins/cachet-notification-v2.sh

#!/bin/bash

# Some Constants
NOW="$(date +'%d/%m/%Y')"
CACHETAPI_URL="https://URL/api/v1/components/<ID DU COMPOSANT>"
CACHETAPI_TOKEN="TOKEN><"
# Map Notification states for icinga2
# OK - 1 operational
# Warning - 3 Partial outage
# Critical - 4 Major outage
# Unknown - 2 Performance issues
case "$SERVICESTATE" in
        'OK')
                COMPONENT_STATUS=1
        ;;
        'WARNING')
                COMPONENT_STATUS=3
        ;;
        'CRITICAL')
                COMPONENT_STATUS=4
        ;;
        'UNKNOWN')
                COMPONENT_STATUS=2
        ;;
esac

curl -X PUT -H "Content-Type: application/json;" -H "X-Cachet-Token: ${CACHETAPI_TOKEN}" -d '{"status": "'"${COMPONENT_STATUS}"'"}' ${CACHETAPI_URL} -k

PS : Give the execution permission to the script

Check the syntax and reload

/etc/init.d/icinga2 checkconfig && /etc/init.d/icinga2 reload

The result : When your check results in "CRITICAL", the status in Cachet will be MAJOR ISSUE
When your check results in "WARNING", the status in Cachet will be PARTIAL ISSUE
When your check results in "OK", the status in Cachet will be OPERATIONAL
When your check results in "UNKNOWN", the status in Cachet will be PERFORMANCE DELAY

I hope it will help.

Nicolas B.

0 Answers0