Commit a47ebb5b authored by Finn Christiansen's avatar Finn Christiansen
Browse files

first version of XMPP notify script

parent 1673d345
XMPP Notify Plugin for Icinga 2 (should work with nagios etc.)
# Installation
* Copy xmppnotify.py to /etc/icinga2/scripts/xmppnotify.py
* Copy xmppnotify.sample.cfg to /etc/xmppnotify.cfg and enter xmpp credentials
# Icinga2 configuration
## Templates
template Notification "xmpp-host-notification" {
command = "xmpp-host-notification"
states = [ Up, Down ]
types = [ Problem, Acknowledgement, Recovery, Custom ]
period = "24x7"
interval = 8h
vars += {
notification_icingaweb2url = "https://monitoring.finnchristiansen.de/icingaweb2"
}
}
template Notification "xmpp-service-notification" {
command = "xmpp-service-notification"
states = [ OK, Warning, Critical, Unknown ]
types = [ Problem, Acknowledgement, Recovery, Custom ]
period = "24x7"
interval = 8h
vars += {
notification_icingaweb2url = "https://monitoring.finnchristiansen.de/icingaweb2"
}
}
## Commands
object NotificationCommand "xmpp-host-notification" {
command = [ SysconfDir + "/icinga2/scripts/xmppnotify.py" ]
arguments += {
"-4" = {
required = true
value = "$notification_address$"
}
"-6" = "$notification_address6$"
"-b" = "$notification_author$"
"-c" = "$notification_comment$"
"-d" = {
required = true
value = "$notification_date$"
}
"-i" = "$notification_icingaweb2url$"
"-l" = {
required = true
value = "$notification_hostname$"
}
"-n" = {
required = true
value = "$notification_hostdisplayname$"
}
"-o" = {
required = true
value = "$notification_hostoutput$"
}
"-r" = {
required = true
value = "$notification_useremail$"
}
"-s" = {
required = true
value = "$notification_hoststate$"
}
"-t" = {
required = true
value = "$notification_type$"
}
}
vars += {
notification_address = "$address$"
notification_address6 = "$address6$"
notification_author = "$notification.author$"
notification_comment = "$notification.comment$"
notification_type = "$notification.type$"
notification_date = "$icinga.long_date_time$"
notification_hostname = "$host.name$"
notification_hostdisplayname = "$host.display_name$"
notification_hostoutput = "$host.output$"
notification_hoststate = "$host.state$"
notification_useremail = "$user.vars.xmpp$"
}
}
object NotificationCommand "xmpp-service-notification" {
command = [ SysconfDir + "/icinga2/scripts/xmppnotify.py" ]
arguments += {
"-4" = "$notification_address$"
"-6" = "$notification_address6$"
"-b" = "$notification_author$"
"-c" = "$notification_comment$"
"-d" = {
required = true
value = "$notification_date$"
}
"-e" = {
required = true
value = "$notification_servicename$"
}
"-i" = "$notification_icingaweb2url$"
"-l" = {
required = true
value = "$notification_hostname$"
}
"-n" = {
required = true
value = "$notification_hostdisplayname$"
}
"-o" = {
required = true
value = "$notification_serviceoutput$"
}
"-r" = {
required = true
value = "$notification_useremail$"
}
"-s" = {
required = true
value = "$notification_servicestate$"
}
"-t" = {
required = true
value = "$notification_type$"
}
"-u" = {
required = true
value = "$notification_servicedisplayname$"
}
}
vars += {
notification_address = "$address$"
notification_address6 = "$address6$"
notification_author = "$notification.author$"
notification_comment = "$notification.comment$"
notification_type = "$notification.type$"
notification_date = "$icinga.long_date_time$"
notification_hostname = "$host.name$"
notification_hostdisplayname = "$host.display_name$"
notification_servicename = "$service.name$"
notification_serviceoutput = "$service.output$"
notification_servicestate = "$service.state$"
notification_useremail = "$user.email$"
notification_servicedisplayname = "$service.display_name$"
}
}
## Notifications
apply Notification "xmpp-icingaadmin" to Host {
import "xmpp-host-notification"
user_groups = host.vars.notification.xmpp.groups
users = host.vars.notification.xmpp.users
assign where host.vars.notification.xmpp
}
apply Notification "xmpp-icingaadmin" to Service {
import "xmpp-service-notification"
user_groups = host.vars.notification.xmpp.groups
users = host.vars.notification.xmpp.users
assign where host.vars.notification.xmpp
}
## Users
object User "icingaadmin" {
import "generic-user"
display_name = "Icinga Admin"
groups = [ "icingaadmins" ]
email = "foo@example.org"
vars.xmpp = "bar@example.org"
}
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# pip3 install dnspython
import argparse
import configparser
from slixmpp import ClientXMPP
from socket import gethostname
class XMPPNotify(ClientXMPP):
def __init__(self, jid, password, recipient, msg):
ClientXMPP.__init__(self, jid, password)
self.recipient = recipient
self.msg = msg
self.add_event_handler("session_start", self.session_start)
# self.add_event_handler("message", self.message)
def session_start(self, event):
self.send_presence()
self.get_roster()
self.send_message(mto=self.recipient, mbody=self.msg)
self.disconnect()
def message(self, msg):
pass
def build_message(args):
if args.servicename:
# service
message = """***** Service Monitoring on {monitoringhostname} *****
{servicedisplayname} on {hostdisplayname} is {servicestate}!
Info: {serviceoutput}
When: {longdatetime}
Service: {servicename}
Host: {hostname}
""".format(
monitoringhostname=gethostname(),
servicedisplayname=args.servicedisplayname,
hostdisplayname=args.hostdisplayname,
servicestate=args.state,
serviceoutput=args.output,
longdatetime=args.longdatetime,
servicename=args.servicename,
hostname=args.hostname
)
else:
message = """***** Host Monitoring on {monitoringhostname} *****
{hostdisplayname} is {hoststate}!
Info: {hostoutput}
When: {longdatetime}
Host: {hostname}
""".format(
monitoringhostname=gethostname(),
hostdisplayname=args.hostdisplayname,
hoststate=args.state,
hostoutput=args.output,
longdatetime=args.longdatetime,
hostname=args.hostname
)
if args.hostaddress:
message += "\nIPv4: {}".format(args.hostaddress)
if args.hostaddress6:
message += "\nIPv6: {}".format(args.hostaddress6)
if args.notificationcomment:
message += """
\nComment by {notificationauthorname}
{notificationcomment}
""".format(
notificationauthorname=args.notificationauthorname,
notificationcomment=args.notificationcomment
)
if args.icingaweb2url and args.servicename:
message += "\n" + args.icingaweb2url + \
"/monitoring/service/show?host={hostname}&service={servicename}" \
.format(
hostname=args.hostname,
servicename=args.servicename
)
elif args.icingaweb2url:
message += "\n" + args.icingaweb2url + \
"/monitoring/host/show?host={hostname}" \
.format(
hostname=args.hostname,
)
return message
if __name__ == '__main__':
config = configparser.RawConfigParser()
config.read('/etc/xmppnotify.cfg')
jid = config.get('Account', 'jid')
password = config.get('Account', 'password')
parser = argparse.ArgumentParser(description='XMPP Notifications')
# required
parser.add_argument('-d', '--longdatetime')
parser.add_argument('-e', '--servicename') # service only
parser.add_argument('-l', '--hostname')
parser.add_argument('-n', '--hostdisplayname')
parser.add_argument('-o', '--output') # host or service output
parser.add_argument('-r', '--jid')
parser.add_argument('-s', '--state') # host or service state
parser.add_argument('-u', '--servicedisplayname') # service only
parser.add_argument('-t', '--notificationtype')
# optional
parser.add_argument('-4', '--hostaddress')
parser.add_argument('-6', '--hostaddress6')
parser.add_argument('-b', '--notificationauthorname')
parser.add_argument('-c', '--notificationcomment')
parser.add_argument('-i', '--icingaweb2url')
args = parser.parse_args()
message = build_message(args)
xmpp = XMPPNotify(jid, password, args.jid, message)
xmpp.register_plugin('xep_0030') # Service Discovery
xmpp.register_plugin('xep_0004') # Data Forms
xmpp.register_plugin('xep_0060') # PubSub
xmpp.register_plugin('xep_0199') # XMPP Ping
# Connect to the XMPP server and start processing XMPP stanzas.
xmpp.connect()
xmpp.process(forever=False)
[Account]
jid=bar@example.org
password=password
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment