Commit 114a7171 authored by Markus Frosch's avatar Markus Frosch

Added LDAP listeners to reload Icinga on Nagios object changes

Only when Nagios objects are used.
parent 1eb5a58a
# -*- coding: utf-8 -*-
#
# Univention Icinga
# listener module: listen for Nagios config changes
#
# Copyright 2014 Icinga Development Team <info@icinga.org>
# 2014 Markus Frosch <markus@lazyfrosch.de>
#
# http://www.icinga.org/
#
# based on nagios-server.py from univention-nagios package
# Copyright 2004-2012 Univention GmbH
#
# http://www.univention.de/
#
# All rights reserved.
#
# The source code of this program is made available
# under the terms of the GNU Affero General Public License version 3
# (GNU AGPL V3) as published by the Free Software Foundation.
#
# Binary versions of this program provided by Univention to you as
# well as other copyrighted, protected or trademarked materials like
# Logos, graphics, fonts, specific documentations and configurations,
# cryptographic keys etc. are subject to a license agreement between
# you and Univention and not subject to the GNU AGPL V3.
#
# In the case you use this program under the terms of the GNU AGPL V3,
# the program is provided in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public
# License with the Debian GNU/Linux or Univention distribution in file
# /usr/share/common-licenses/AGPL-3; if not, see
# <http://www.gnu.org/licenses/>.
__package__='' # workaround for PEP 366
import listener
import os, re, string, stat
import univention.debug
import univention_baseconfig
import subprocess
name='ZZicinga_nagios_objectchanges'
description='Handle Nagios objects changes for Icinga'
filter='(|(objectClass=univentionNagiosServiceClass)(objectClass=univentionNagiosTimeperiodClass)(objectClass=univentionHost))'
__initscript='/etc/init.d/icinga'
__reload = False
def handler(dn, new, old):
global __reload
# univention.debug.debug(univention.debug.LISTENER, univention.debug.INFO, 'NAGIOS-SERVER: IN dn=%s' % str(dn))
# univention.debug.debug(univention.debug.LISTENER, univention.debug.INFO, 'NAGIOS-SERVER: IN old=%s' % str(old))
# univention.debug.debug(univention.debug.LISTENER, univention.debug.INFO, 'NAGIOS-SERVER: IN new=%s' % str(new))
if ((old and old.has_key('objectClass') and 'univentionNagiosServiceClass' in old['objectClass']) or
(new and new.has_key('objectClass') and 'univentionNagiosServiceClass' in new['objectClass'])):
__reload = True
elif ((old and old.has_key('objectClass') and 'univentionNagiosHostClass' in old['objectClass']) or
(new and new.has_key('objectClass') and 'univentionNagiosHostClass' in new['objectClass'])):
# check if the nagios related attributes were changed
for attr in ['aRecord', 'associatedDomain', 'uid', 'cn', 'description', 'univentionNagiosParent', 'univentionNagiosEnabled', 'univentionNagiosEmail']:
if not (new.get(attr, None) == old.get(attr, None)):
__reload = True
break
elif ((old and old.has_key('objectClass') and 'univentionNagiosTimeperiodClass' in old['objectClass']) or
(new and new.has_key('objectClass') and 'univentionNagiosTimeperiodClass' in new['objectClass'])):
__reload = True
def postrun():
global __reload
if __reload:
global __initscript
initscript = __initscript
if listener.baseConfig.is_true("icinga/server/usenagiosconfig", False):
# usenagiosconfig is disabled - nothing todo
__reload = False
return
# restart icinga if not running and icinga/server/autostart is set to yes/true/1
# otherwise if icinga is running, ask icinga to reload config
p = subprocess.Popen(('pidof', '/usr/sbin/icinga'), stdout=subprocess.PIPE)
pidlist, stderr = p.communicate()
listener.setuid(0)
null = open(os.path.devnull, 'w')
try:
retcode = subprocess.call(('icinga', '-v', '/etc/icinga/icinga.cfg'), stdout=null, stderr=null)
finally:
null.close()
listener.unsetuid()
if not pidlist.strip():
if retcode == 0:
if listener.baseConfig.is_true("icinga/server/autostart", False):
univention.debug.debug(univention.debug.LISTENER, univention.debug.INFO, 'NAGIOS-SERVER: icinga not running - restarting server')
listener.setuid(0)
try:
listener.run(initscript, ['icinga', 'restart'], uid=0)
finally:
listener.unsetuid()
else:
univention.debug.debug(univention.debug.LISTENER, univention.debug.ERROR, 'NAGIOS-SERVER: icinga reported an error in configfile /etc/icinga/icinga.cfg. Please restart icinga manually: "%s restart".' % initscript)
listener.unsetuid()
else:
if retcode == 0:
univention.debug.debug(univention.debug.LISTENER, univention.debug.INFO, 'NAGIOS-SERVER: reloading server')
listener.setuid(0)
try:
listener.run(initscript, ['icinga', 'reload'], uid=0)
finally:
listener.unsetuid()
else:
univention.debug.debug(univention.debug.LISTENER, univention.debug.ERROR, 'NAGIOS-SERVER: icinga reported an error in configfile /etc/icinga/icinga.cfg. Please restart icinga manually: "%s restart".' % initscript)
listener.unsetuid()
__reload = False
# vi: noexpandtab :
ZZicinga_nagios_objectchanges.py usr/lib/univention-directory-listener/system/
28univention-icinga.inst usr/lib/univention-install/
......@@ -57,6 +57,11 @@ if [ ! -d /etc/apache2/conf.d ]; then
fi
if [ "$1" = "configure" ]; then
if test -f /etc/init.d/univention-directory-listener
then
/etc/init.d/univention-directory-listener crestart
fi
if test -f /etc/init.d/apache2
then
/etc/init.d/apache2 restart
......
Markdown is supported
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