]> Frank Brehm's Git Trees - my-stuff/nagios.git/commitdiff
Template-Chain abgeklappert
authorFrank Brehm <frank@brehm-online.com>
Tue, 14 Dec 2010 22:57:36 +0000 (22:57 +0000)
committerFrank Brehm <frank@brehm-online.com>
Tue, 14 Dec 2010 22:57:36 +0000 (22:57 +0000)
git-svn-id: http://svn.brehm-online.com/svn/my-stuff/nagios/trunk@157 ec8d2aa5-1599-4edb-8739-2b3a1bc399aa

bin/nagios/config.py

index 1fc6eaf4f2be588af5ee3c28cfdc5ba4ba2b7eb5..934dbc622c49a6cc52509a601d85e6e7f5201667 100644 (file)
@@ -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 ):