Select Page

Jabber-Notifications mit Icinga 2

by | Jul 17, 2014 | Icinga, Technology

Um mit Icinga 2 einfach Notifications an Jabber-Kontakte senden zu können, habe ich mir folgendes Script geschrieben:

#!/usr/bin/env python
import xmpp, os, sys
if len(sys.argv) < 3:
    print "Syntax:", sys.argv[0], " "
    sys.exit(1)
jid = xmpp.protocol.JID(os.environ["XMPP_USER"])
cl = xmpp.Client(jid.getDomain(), debug = [])
con = cl.connect()
cl.auth(jid.getNode(), os.environ["XMPP_PASSWORD"])
cl.sendInitPresence()
msg = xmpp.Message(sys.argv[1], sys.argv[2])
msg.setAttr('type', 'chat')
cl.send(msg)

Das Script wird dabei in /etc/icinga2/scripts mit dem Dateinamen jabber-notification.py abgelegt werden. Zusätzlich muss noch die Python-XMPP-Library installiert werden, für die es z.B. in Debian das Paket python-xmpp gibt.
Um das Script mit Icinga verwenden zu können, müssen zunächst folgende allgemeine Templates definiert werden:

template NotificationCommand "jabber-template" {
  import "plugin-notification-command"
  command = [
    SysconfDir + "/icinga2/scripts/jabber-notification.py",
    "$xmpp_recipient$",
    "$xmpp_message$"
  ]
  vars.xmpp_recipient = "$jabber$"
  // Wir übergeben den Benutzernamen und das Passwort per Environment-
  // Variablen an das Script, damit diese nicht per ps(1)
  // für andere Benutzer einsehbar sind.
  env = {
    XMPP_USER = "$xmpp_user$"
    XMPP_PASSWORD = "$xmpp_password$"
  }
}
template NotificationCommand "jabber-host-notification" {
  import "jabber-template"
  vars.xmpp_message = {{{Notification Type: $notification.type$
Host: $host.display_name$
Address: $address$
State: $host.state$
Date/Time: $icinga.long_date_time$
Additional Info: $host.output$
Comment: [$notification.author$] $notification.comment$}}}
}
template NotificationCommand "jabber-service-notification" {
  import "jabber-template"
  vars.xmpp_message = {{{Notification Type: $notification.type$
Service: $service.name$
Host: $host.display_name$
Address: $address$
State: $service.state$
Date/Time: $icinga.long_date_time$
Additional Info: $service.output$
Comment: [$notification.author$] $notification.comment$}}}
}

Den beiden Templates "jabber-host-notification" und "jabber-service-notification" fehlen dabei noch die Custom Attribute für den Jabber-Benutzernamen und -Passwort. Um diese anzugeben, definieren wir zwei Commands:

object NotificationCommand "jabber-host-netways" {
  import "jabber-host-notification"
  vars.xmpp_user = "jabber-user@example.org"
  vars.xmpp_password = "passwort"
}
object NotificationCommand "jabber-service-netways" {
  import "jabber-service-notification"
  vars.xmpp_user = "jabber-user@example.org"
  vars.xmpp_password = "passwort"
}

Anschließend können wir diese Commands für Notifications verwenden:

object User "gunnar" {
  vars.jabber = "gunnar@beutner.name"
}
apply Notification "jabber-host" to Host {
  command = "jabber-host-netways"
  users = [ "gunnar" ]
  assign where true
}
apply Notification "jabber-service" to Service {
  command = "jabber-service-netways"
  users = [ "gunnar" ]
  assign where true
}

Die Command-Templates sind dabei so parametrisiert, dass in den einzelnen Notifications bei Bedarf auch die Texte für die Jabber-Messages über das Custom-Attribut xmpp_message überschrieben werden können.

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *

More posts on the topic Icinga | Technology

Open Source VMware Alternativen für Deine IT-Infrastruktur

Der 22. November 2023 war ein Mittwoch wie jeder andere, doch dann ging es wie ein Paukenschlag durch die Presse: Broadcom Inc. [...] today announced that it has completed its acquisition of VMware, Inc. Da war sie also, die Nachricht, die alle erwartet haben und noch...

Webserver? Caddy bitte! Danke!

Hin und wieder gibt es einfach Software, die Probleme erschreckend gut löst: Der Webserver Caddy – eine in Go geschriebene Plattform, die mit ihrem HTTP-Server alle Standardfälle im täglichen Betrieb abdeckt – ist ein gutes Beispiel dafür.   Install Caddy auf...