Seite wählen

Jabber-Notifications mit Icinga 2

von | 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.

1 Kommentar

  1. HB

    Ich hatte da ein kleines Problem, die Messages wurden nicht zugestellt…
    Habe ich aber in einer python-shell das alles „von Hand“ gemacht, so kamen die Messages an…
    Abhilfe:
    from time import sleep
    und nach connect und auth ein sleep(0.05)
    Hat man beide Systeme, icinga und jabber, in einem Netz/VLAN am gleichen Switch, könnte es da zu timing probs kommen…
    Gruss,
    HB
    PS: hier der Diff

    --- jabber-notification.py_orig 2015-08-02 03:12:11.840522711 +0200
    +++ jabber-notification.py 2015-08-02 02:52:10.304479594 +0200
    @@ -1,5 +1,6 @@
    #!/usr/bin/env python
    import xmpp, os, sys
    +from time import sleep
    if len(sys.argv) < 3:
    print "Syntax:", sys.argv[0], " "
    @@ -9,7 +10,9 @@
    cl = xmpp.Client(jid.getDomain(), debug = [])
    con = cl.connect()
    cl.auth(jid.getNode(), os.environ["XMPP_PASSWORD"])
    +sleep(0.05)
    cl.sendInitPresence()
    +sleep(0.05)
    msg = xmpp.Message(sys.argv[1], sys.argv[2])
    msg.setAttr('type', 'chat')

    Antworten

Trackbacks/Pingbacks

  1. Schwarze Magie für GNU/Linux-Nerds › NETWAYS Blog - […] Bernd recht hat, hat er recht – nicht umsonst nimmt z. B. Gunnars Jabber-Notification-Skript Nutzername und Passwort via Umgebungsvariablen…

Einen Kommentar abschicken

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert

Mehr Beiträge zum Thema Icinga | Technology

Kibana Sicherheits-Updates: CVSS:Critical

Und täglich grüßt das Murmeltier. Nein nicht ganz. Heute ist es  aus der Elastic Stack Werkzeugkiste Kibana, für das es ein wichtiges Sicherheits-Update gibt. Es besteht auf jeden Fall Handlungsbedarf! IMHO auch wenn ihr die "Reporting" Funktion deaktiviert habt. Der...