]> Frank Brehm's Git Trees - my-stuff/nagios.git/commitdiff
register_templates() dazu
authorFrank Brehm <frank@brehm-online.com>
Mon, 13 Dec 2010 22:00:27 +0000 (22:00 +0000)
committerFrank Brehm <frank@brehm-online.com>
Mon, 13 Dec 2010 22:00:27 +0000 (22:00 +0000)
git-svn-id: http://svn.brehm-online.com/svn/my-stuff/nagios/trunk@153 ec8d2aa5-1599-4edb-8739-2b3a1bc399aa

bin/nagios/config.py

index f52fc754bf2813fc379c7d7ac83d0cc361223477..79abfb7d47aa8877fe9850bd90a3e751d5a67e73 100644 (file)
@@ -56,7 +56,7 @@ class NagiosConfig(LoggingObject):
 
     #------------------------------------------------------
     def __repr__( self ):
-        "Dumper seiner selbst"
+        "Dumper of itself"
 
         dump = {}
         dump['conf_dir'] = self.conf_dir
@@ -102,24 +102,36 @@ class NagiosConfig(LoggingObject):
         self.objects = {}
         self.config_objects = {}
         self.objects_read = {}
+        self.templates = {}
         self.ignore_empty_object_props = ignore_empty_object_props
 
     #------------------------------------------------------
     def __del__( self ):
-        "Destruktor"
+        "Destructor"
 
-        self.logger.debug( "Mayday - someone destroys me!!" )
+        if not getattr( self, 'logger', None ) is None:
+            self.logger.debug( "Mayday - someone destroys me!!" )
 
     #------------------------------------------------------
     def canonize_file( self, f ):
-        """Kanonisiert den übergebenen Dateinamen, indem er nicht-absoluten Dateinamen verabsolutiert"""
+        """Canonize the given filename by absolutize the relative filename.
+        @param f: The filename to canonize
+        @type f: str
+        @return: the canonized filename
+        @rtype: str
+        """
         f = os.path.abspath(f);
         f = self.search_conf(f)
         return f
 
     #------------------------------------------------------
     def file( self, new_file = None ):
-        "Gibt den aktuellen Konfigurationsdateiname zururück bzw. setzt ihn."
+        '''Gives the current main configuration filename or sets it.
+        @param new_file: The new main configuration filename or None
+        @type new_file: str or None
+        @return: the old filename
+        @rtype: str
+        '''
         old_name = self.file_name
         if new_file is not None:
             self.file_name = os.path.abspath(new_file)
@@ -127,7 +139,13 @@ class NagiosConfig(LoggingObject):
 
     #------------------------------------------------------
     def search_conf( self, f  = None ):
-        "Sucht die Nagios-Konfigurationsdatei und speichert diese in self.file_name bzw. stirbt, wenn nicht gefunden."
+        """Searches for the main configuration file and saves it in self.file_name.
+        Raises an exception, if not found.
+        @param f: The filename to search
+        @type f: str
+        @return: None
+        @rtype: None
+        """
 
         self.logger.debug( "Searching for main configuration file ..." )
 
@@ -138,7 +156,7 @@ class NagiosConfig(LoggingObject):
                 self.file_name = f
                 return f
             else:
-                raise NagiosConfigError( "Pfad '" + f + "' ist keine normale Datei." )
+                raise NagiosConfigError( "Path {0!r} isn't a regular file.".format( f ) )
 
         search_dirs = ( os.path.dirname(self.file_name), self.conf_dir, \
                         os.sep + os.path.join( 'etc', 'nagios3'), \
@@ -154,7 +172,7 @@ class NagiosConfig(LoggingObject):
                 continue
             if path in used_dirs: continue
             used_dirs[path] = 1
-            print "Suche Konfig in Verzeichnis '" + path + "' ..."
+            self.logger.debug( "Searching for configuration in directory {0!r} ...".format( path ) )
             my_file = os.path.join( path, 'nagios.cfg' )
             if os.path.exists(my_file) and os.path.isfile(my_file):
                 my_file = os.path.realpath(my_file)
@@ -298,12 +316,66 @@ class NagiosConfig(LoggingObject):
             self.read_objectfile(file_name)
 
         pp = pprint.PrettyPrinter( indent = 4, depth = 6, width = 120 )
-        self.logger.debug( "Read objects:\n{0}".format( pp.pformat( self.objects_read ) ) )
+        if self.verbose >= 3:
+            self.logger.debug( "Read objects:\n{0}".format( pp.pformat( self.objects_read ) ) )
 
         del self.struct_verifier
 
+        self.register_templates()
+
         return
 
+    #------------------------------------------------------
+    def register_templates( self ):
+        '''Searches the self.objects_read structure, searches for templates and registers them in self.templates
+        @return: None
+        @rtype: None
+        '''
+
+        self.logger.info( "Registering templates ..." )
+        self.templates = {
+            'host':              {},
+            'hostgroup':         {},
+            'service':           {},
+            'servicegroup':      {},
+            'contact':           {},
+            'contactgroup':      {},
+            'timeperiod':        {},
+            'command':           {},
+            'servicedependency': {},
+            'serviceescalation': {},
+            'hostdependency':    {},
+            'hostescalation':    {},
+            'hostextinfo':       {},
+            'serviceextinfo':    {},
+        }
+
+        pp = pprint.PrettyPrinter( indent = 4, depth = 6, width = 120 )
+        for object_type in self.templates:
+            if object_type in self.objects_read:
+                index = 0
+                for object in self.objects_read[object_type]:
+                    if self.verbose >= 4:
+                        self.logger.debug( "Object {0}({1}):\n{2}".format( object_type, index, pp.pformat( object ) ) )
+                    if 'name' in object:
+                        template_name = object['name'][0]
+                        self.logger.debug( "Template {0!r} ({1}) found for {2} in {3!r}({4}).".format(
+                                template_name,
+                                index,
+                                object_type,
+                                object['__object_definition__'][0],
+                                object['__object_definition__'][1]
+                        ) )
+                        if template_name in self.templates[object_type]:
+                            raise NagiosConfigError( "Double definition of a {0} template named {1!r} in {2}({3}).".format(
+                                        object_type, template_name, object['__object_definition__'][0], object['__object_definition__'][1] ) )
+                        else:
+                            self.templates[object_type][template_name] = index
+                    index += 1
+
+        if self.verbose >= 3:
+            self.logger.debug( "Found templates:\n{0}".format( pp.pformat( self.templates ) ) )
+
     #------------------------------------------------------
     def read_object_cache_file( self ):
         '''Reads objects from object cache file.