From: Frank Brehm Date: Tue, 14 Dec 2010 22:57:36 +0000 (+0000) Subject: Template-Chain abgeklappert X-Git-Url: https://git.uhu-banane.org/?a=commitdiff_plain;h=df74e3408f77f736a1bc198ffae30e5fa2898e2a;p=my-stuff%2Fnagios.git Template-Chain abgeklappert git-svn-id: http://svn.brehm-online.com/svn/my-stuff/nagios/trunk@157 ec8d2aa5-1599-4edb-8739-2b3a1bc399aa --- diff --git a/bin/nagios/config.py b/bin/nagios/config.py index 1fc6eaf..934dbc6 100644 --- a/bin/nagios/config.py +++ b/bin/nagios/config.py @@ -336,8 +336,47 @@ class NagiosConfig(LoggingObject): pp = pprint.PrettyPrinter( indent = 4, depth = 6, width = 120 ) for object_type in self.objects_read: for object in self.objects_read[object_type]: - object['templates'] = {} - object['templates_applied'] = set([]) + self._retrieve_templates( object_type, object ) + + #------------------------------------------------------ + def _retrieve_templates( self, object_type, object ): + '''Recursive function to collect the templates of an object. + @param object_type: the type of the given object structure ('host', 'service', 'timeperiod' a.s.o.) + @type object_type: str + @param object: the nagios object + @type object: dict + @return: Ordered list of templates of this object + @rtype: list + ''' + + if not 'use' in object: + return [] + + file = object['use'][1] + row_num = object['use'][2] + + if 'templates' in object: + return object['templates'] + + object['templates'] = [] + object['templates_applied'] = set([]) + + for template_name in object['use'][0]: + if not template_name in self.templates[object_type]: + raise NagiosConfigError( "Template {0!r} for {1!r} in {2!r}({3}) not found.".format( template_name, object_type, file, row_num ) ) + template_id = self.templates[object_type][template_name] + template_object = self.objects_read[object_type][template_id] + names = [ template_name ] + for name in self._retrieve_templates( object_type, template_name ): + names.append( name ) + for name in names: + if name in object['templates_applied']: + self.logger.debug( "Template {0!r} for {1!r} allready applied.".format( name, object_type ) ) + else: + object['templates'].append( name ) + object['templates_applied'].add( name ) + + return object['templates'] #------------------------------------------------------ def register_templates( self ):