[Pkg-nagios-changes] [SCM] UNNAMED PROJECT branch, debian/master, updated. 810edbdd3feedbfe37f4a65bee50b57b2f60fa2a

Sebastien Coavoux s.coavoux at free.fr
Tue Feb 28 22:05:49 UTC 2012


The following commit has been merged in the debian/master branch:
commit cf249564eef5b5123b9d811bcfbc96def90841f3
Author: Sebastien Coavoux <s.coavoux at free.fr>
Date:   Mon Nov 14 10:52:32 2011 +0100

    Started : Handle properly several instance in the same DB

diff --git a/shinken/modules/ndodb_mysql_broker/__init__.py b/shinken/modules/ndodb_mysql_broker/__init__.py
index 6167154..67e9bf8 100644
--- a/shinken/modules/ndodb_mysql_broker/__init__.py
+++ b/shinken/modules/ndodb_mysql_broker/__init__.py
@@ -20,7 +20,7 @@
 import sys
 
 from ndodb_mysql_broker import Ndodb_Mysql_broker, properties
-
+from ndodb_mysql_broker_sync import Ndodb_Mysql_broker_sync, properties
 
 #called by the plugin manager to get a instance
 def get_instance(mod_conf):
@@ -37,7 +37,12 @@ def get_instance(mod_conf):
     if not hasattr( mod_conf, 'character_set'):
         mod_conf.character_set = 'utf8'
     if not hasattr(mod_conf, 'synchronise_database_id'):
-        #TODO : Use a Boolean?
+        #TODO : Use a Boolean  ?
         mod_conf.synchronise_database_id = '0'
-    instance = Ndodb_Mysql_broker(mod_conf)
+        instance = Ndodb_Mysql_broker(mod_conf)
+    elif mod_conf.synchronise_database_id == 'O' :
+        instance = Ndodb_Mysql_broker(mod_conf)
+    else :
+        instance = Ndodb_Mysql_broker_sync(mod_conf)
+        print("Running synchro module")
     return instance
diff --git a/shinken/modules/ndodb_mysql_broker/ndodb_mysql_broker.py b/shinken/modules/ndodb_mysql_broker/ndodb_mysql_broker_sync.py
similarity index 88%
copy from shinken/modules/ndodb_mysql_broker/ndodb_mysql_broker.py
copy to shinken/modules/ndodb_mysql_broker/ndodb_mysql_broker_sync.py
index f758676..619353a 100644
--- a/shinken/modules/ndodb_mysql_broker/ndodb_mysql_broker.py
+++ b/shinken/modules/ndodb_mysql_broker/ndodb_mysql_broker_sync.py
@@ -47,7 +47,7 @@ def de_unixify(t):
 
 #Class for the Merlindb Broker
 #Get broks and puts them in merlin database
-class Ndodb_Mysql_broker(BaseModule):
+class Ndodb_Mysql_broker_sync(BaseModule):
     def __init__(self, conf):
         BaseModule.__init__(self, conf)
         #Mapping for name of dataand transform function
@@ -75,16 +75,16 @@ class Ndodb_Mysql_broker(BaseModule):
         self.db = DBMysql(self.host, self.user, self.password, self.database, self.character_set, table_prefix='nagios_')
         self.connect_database()
 
-        #Cache for hosts and services
-        #will be flushed when we got a net instance id
-        #or something like that
-        self.services_cache = {}
-        self.hosts_cache = {}
+        #Cache for hosts and services when sync is active
+        self.services_cache_sync = {}
+        self.hosts_cache_sync = {}
 
         #Cache for database id
         #In order not to query the database every time
         self.database_id_cache={}
+        
 
+        
 
         #Todo list to manage brok
         self.todo=[]
@@ -97,13 +97,9 @@ class Ndodb_Mysql_broker(BaseModule):
     
         if 'instance_id' in new_b.data:
             
-            if self.synchronise_database_id != 1:
-                # We've got problem with instance_id == 0 so we add 1 every where
-                new_b.data['instance_id'] = new_b.data['instance_id'] + 1
-            
             #We have to synchronise database id
             #so we wait for the instance name
-            elif 'instance_name' not in new_b.data :
+            if 'instance_name' not in new_b.data :
                 self.todo.append(new_b)
                 #print("No instance name for %s : " % new_b.data)
                 return  
@@ -189,24 +185,32 @@ class Ndodb_Mysql_broker(BaseModule):
             return data_id
 
 
-    def get_host_object_id_by_name(self, host_name):
+    def get_host_object_id_by_name_sync(self, host_name, instance_id):
+        
+
+        
         #First look in cache.
-        if host_name in self.hosts_cache:
-            return self.hosts_cache[host_name]
+        if instance_id in self.hosts_cache_sync:
+            if host_name in self.hosts_cache_sync[instance_id]:
+                return self.hosts_cache_sync[instance_id][host_name]
 
         #Not in cache, not good
-        query = u"SELECT object_id from nagios_objects where name1='%s' and objecttype_id='1'" % host_name
+        query = u"SELECT object_id from nagios_objects where name1='%s' and objecttype_id='1' and instance_id='%s'" % (host_name,instance_id)
         self.db.execute_query(query)
         row = self.db.fetchone ()
         if row is None or len(row) < 1:
             return 0
         else:
-            self.hosts_cache[host_name] = row[0]
+            if instance_id not in self.hosts_cache_sync:
+                self.hosts_cache_sync[instance_id] = {}
+            self.hosts_cache_sync[instance_id][host_name] = row[0]
             return row[0]
 
-    def get_contact_object_id_by_name(self, contact_name):
-        #Not in cache, not good
-        query = u"SELECT object_id from nagios_objects where name1='%s' and objecttype_id='10'" % contact_name
+    def get_contact_object_id_by_name_sync(self, contact_name,instance_id):
+        
+        
+             
+        query = u"SELECT object_id from nagios_objects where name1='%s' and objecttype_id='10' and instance_id='%s'" % (contact_name,instance_id)
         self.db.execute_query(query)
         row = self.db.fetchone ()
         if row is None or len(row) < 1:
@@ -216,8 +220,9 @@ class Ndodb_Mysql_broker(BaseModule):
 
 
 
-    def get_hostgroup_object_id_by_name(self, hostgroup_name):
-        query = u"SELECT object_id from nagios_objects where name1='%s' and objecttype_id='3'" % hostgroup_name
+    def get_hostgroup_object_id_by_name_sync(self, hostgroup_name, instance_id):
+
+        query = u"SELECT object_id from nagios_objects where name1='%s' and objecttype_id='3' and instance_id='%s'" % (hostgroup_name,instance_id)
         self.db.execute_query(query)
         row = self.db.fetchone ()
         if row is None or len(row) < 1:
@@ -226,24 +231,29 @@ class Ndodb_Mysql_broker(BaseModule):
             return row[0]
 
 
-    def get_service_object_id_by_name(self, host_name, service_description):
-        #first look in cache
-        if (host_name, service_description) in self.services_cache:
-            return self.services_cache[(host_name, service_description)]
+    def get_service_object_id_by_name_sync(self, host_name, service_description, instance_id):
+
+        
+        if instance_id in self.services_cache_sync:
+            if (host_name, service_description) in self.services_cache_sync[instance_id]:
+                return self.services_cache_sync[(host_name, service_description)]
 
         #else; not in cache :(
-        query = u"SELECT object_id from nagios_objects where name1='%s' and name2='%s' and objecttype_id='2'" % (host_name, service_description)
+        query = u"SELECT object_id from nagios_objects where name1='%s' and name2='%s' and objecttype_id='2' and instance_id='%s'" % (host_name, service_description,instance_id)
         self.db.execute_query(query)
         row = self.db.fetchone ()
         if row is None or len(row) < 1:
             return 0
         else:
-            self.services_cache[(host_name, service_description)] = row[0]
+            if instance_id not in self.services_cache_sync:
+                self.services_cache_sync[instance_id] = {}
+            self.services_cache_sync[(host_name, service_description)] = row[0]
             return row[0]
 
 
-    def get_servicegroup_object_id_by_name(self, servicegroup_name):
-        query = u"SELECT object_id from nagios_objects where name1='%s' and objecttype_id='4'" % servicegroup_name
+    def get_servicegroup_object_id_by_name_sync(self, servicegroup_name, instance_id):
+ 
+        query = u"SELECT object_id from nagios_objects where name1='%s' and objecttype_id='4' and instance_id='%s'" % (servicegroup_name,instance_id)
         self.db.execute_query(query)
         row = self.db.fetchone ()
         if row is None or len(row) < 1:
@@ -252,8 +262,10 @@ class Ndodb_Mysql_broker(BaseModule):
             return row[0]
 
         
-    def get_contactgroup_object_id_by_name(self, contactgroup_name):
-        query = u"SELECT object_id from nagios_objects where name1='%s' and objecttype_id='11'" % contactgroup_name
+    def get_contactgroup_object_id_by_name_sync(self, contactgroup_name, instance_id):
+
+              
+        query = u"SELECT object_id from nagios_objects where name1='%s' and objecttype_id='11'and instance_id='%s'" % (contactgroup_name,instance_id)
         self.db.execute_query(query)
         row = self.db.fetchone ()
         if row is None or len(row) < 1:
@@ -263,6 +275,7 @@ class Ndodb_Mysql_broker(BaseModule):
 
 
 
+
     # Ok, we are at launch and a scheduler want him only, OK...
     # So ca create several queries with all tables we need to delete with
     # our instance_id
@@ -283,8 +296,8 @@ class Ndodb_Mysql_broker(BaseModule):
 
         #We also clean cache, because we are not sure about this data now
         print "[MySQL/NDO] Flushing caches (clean from instance %d)" % instance_id
-        self.services_cache = {}
-        self.hosts_cache = {}
+        self.services_cache_sync = {}
+        self.hosts_cache_sync = {}
 
         return res
 
@@ -368,10 +381,10 @@ class Ndodb_Mysql_broker(BaseModule):
         object_query = self.db.create_insert_query('objects', objects_data)
         self.db.execute_query(object_query)
 
-        host_id = self.get_host_object_id_by_name(data['host_name'])
+        host_id = self.get_host_object_id_by_name_sync(data['host_name'],data['instance_id'])
 
         #print "DATA:", data
-        hosts_data = {'host_id' : data['id'], 'instance_id' : data['instance_id'],
+        hosts_data = { 'instance_id' : data['instance_id'],
                       'host_object_id' : host_id, 'alias' : data['alias'],
                       'display_name' : data['display_name'], 'address' : data['address'],
                       'failure_prediction_options' : '0', 'check_interval' : data['check_interval'],
@@ -426,13 +439,13 @@ class Ndodb_Mysql_broker(BaseModule):
         object_query = self.db.create_insert_query('objects', objects_data)
         self.db.execute_query(object_query)
 
-        host_id = self.get_host_object_id_by_name(data['host_name'])
-        service_id = self.get_service_object_id_by_name(data['host_name'], data['service_description'])
+        host_id = self.get_host_object_id_by_name_sync(data['host_name'],data['instance_id'])
+        service_id = self.get_service_object_id_by_name_sync(data['host_name'], data['service_description'],data['instance_id'])
 
         #print "DATA:", data
         #print "HOST ID:", host_id
         #print "SERVICE ID:", service_id
-        services_data = {'service_id' : data['id'], 'instance_id' : data['instance_id'],
+        services_data = { 'instance_id' : data['instance_id'],
                       'service_object_id' : service_id, 'host_object_id' : host_id,
                       'display_name' : data['display_name'],
                       'failure_prediction_options' : '0', 'check_interval' : data['check_interval'],
@@ -489,10 +502,13 @@ class Ndodb_Mysql_broker(BaseModule):
                         }
         object_query = self.db.create_insert_query('objects', objects_data)
         self.db.execute_query(object_query)
+        if self.synchronise_database_id != 1:
+            hostgroup_id = self.get_hostgroup_object_id_by_name(data['hostgroup_name'])
+        else :
+            hostgroup_id = self.get_hostgroup_object_id_by_name_sync(data['hostgroup_name'],data['instance_id'])
+        
 
-        hostgroup_id = self.get_hostgroup_object_id_by_name(data['hostgroup_name'])
-
-        hostgroups_data = {'hostgroup_id' : data['id'], 'instance_id' :  data['instance_id'],
+        hostgroups_data = { 'instance_id' :  data['instance_id'],
                            'config_type' : 0, 'hostgroup_object_id' : hostgroup_id,
                            'alias' : data['alias']
             }
@@ -503,8 +519,9 @@ class Ndodb_Mysql_broker(BaseModule):
         #Ok, the hostgroups table is uptodate, now we add relations
         #between hosts and hostgroups
         for (h_id, h_name) in b.data['members']:
-            host_id = self.get_host_object_id_by_name(h_name)
-            hostgroup_members_data = {'instance_id' : data['instance_id'], 'hostgroup_id' : data['id'],
+            host_id = self.get_host_object_id_by_name_sync(h_name,data['instance_id'])
+
+            hostgroup_members_data = {'instance_id' : data['instance_id'],
                                       'host_object_id' : host_id}
             q = self.db.create_insert_query('hostgroup_members', hostgroup_members_data)
             res.append(q)
@@ -525,10 +542,12 @@ class Ndodb_Mysql_broker(BaseModule):
         object_query = self.db.create_insert_query('objects', objects_data)
         self.db.execute_query(object_query)
 
-        servicegroup_id = self.get_servicegroup_object_id_by_name(data['servicegroup_name'])
+        servicegroup_id = self.get_servicegroup_object_id_by_name_sync(data['servicegroup_name'],data['instance_id'])
+
+        
 
 
-        servicegroups_data = {'servicegroup_id' : data['id'], 'instance_id' :  data['instance_id'],
+        servicegroups_data = {'instance_id' :  data['instance_id'],
                            'config_type' : 0, 'servicegroup_object_id' : servicegroup_id,
                            'alias' : data['alias']
             }
@@ -539,7 +558,7 @@ class Ndodb_Mysql_broker(BaseModule):
         #Ok, the hostgroups table is uptodate, now we add relations
         #between hosts and hostgroups
         for (s_id, s_name) in b.data['members']:
-            servicegroup_members_data = {'instance_id' : data['instance_id'], 'servicegroup_id' : data['id'],
+            servicegroup_members_data = {'instance_id' : data['instance_id'],
                                          'service_object_id' : s_id}
             q = self.db.create_insert_query('servicegroup_members', servicegroup_members_data)
             res.append(q)
@@ -550,7 +569,8 @@ class Ndodb_Mysql_broker(BaseModule):
     def manage_host_check_result_brok(self, b):
         data = b.data
         #print "DATA", data
-        host_id = self.get_host_object_id_by_name(data['host_name'])
+        host_id = self.get_host_object_id_by_name_sync(data['host_name'],data['instance_id'])
+
         #Only the host is impacted
         where_clause = {'host_object_id' : host_id}
         host_check_data = {'instance_id' : data['instance_id'],
@@ -579,7 +599,9 @@ class Ndodb_Mysql_broker(BaseModule):
     #next_check with it
     def manage_host_next_schedule_brok(self, b):
         data = b.data
-        host_id = self.get_host_object_id_by_name(data['host_name'])
+        
+        host_id = self.get_host_object_id_by_name_sync(data['host_name'],data['instance_id'])
+
         #Only the host is impacted
         where_clause = {'host_object_id' : host_id}
 
@@ -594,7 +616,7 @@ class Ndodb_Mysql_broker(BaseModule):
     def manage_service_check_result_brok(self, b):
         data = b.data
         #print "DATA", data
-        service_id = self.get_service_object_id_by_name(data['host_name'], data['service_description'])
+        service_id = self.get_service_object_id_by_name_sync(data['host_name'], data['service_description'],data['instance_id'])
 
         #Only the service is impacted
         where_clause = {'service_object_id' : service_id}
@@ -627,7 +649,7 @@ class Ndodb_Mysql_broker(BaseModule):
     def manage_service_next_schedule_brok(self, b):
         data = b.data
         #print "DATA", data
-        service_id = self.get_service_object_id_by_name(data['host_name'], data['service_description'])
+        service_id = self.get_service_object_id_by_name_sync(data['host_name'], data['service_description'],data['instance_id'])
 
         #Only the service is impacted
         where_clause = {'service_object_id' : service_id}
@@ -643,7 +665,9 @@ class Ndodb_Mysql_broker(BaseModule):
     #Ok the host is updated
     def manage_update_host_status_brok(self, b):
         data = b.data
-        host_id = self.get_host_object_id_by_name(data['host_name'])
+        
+        host_id = self.get_host_object_id_by_name_sync(data['host_name'],data['instance_id'])
+            
 
         hosts_data = {'instance_id' : data['instance_id'],
                       'failure_prediction_options' : '0', 'check_interval' : data['check_interval'],
@@ -691,7 +715,7 @@ class Ndodb_Mysql_broker(BaseModule):
     def manage_update_service_status_brok(self, b):
         data = b.data
 
-        service_id = self.get_service_object_id_by_name(data['host_name'], data['service_description'])
+        service_id = self.get_service_object_id_by_name_sync(data['host_name'], data['service_description'],data['instance_id'])
 
 
 
@@ -709,7 +733,7 @@ class Ndodb_Mysql_broker(BaseModule):
             }
 
         #Only the service is impacted
-        where_clause = {'service_object_id' : service_id, 'service_id' : data['id']}
+        where_clause = {'service_object_id' : service_id, 'instance_id' : data['instance_id']}
         #where_clause = {'host_name' : data['host_name']}
         query = self.db.create_update_query('services', services_data, where_clause)
 
@@ -756,9 +780,10 @@ class Ndodb_Mysql_broker(BaseModule):
         object_query = self.db.create_insert_query('objects', objects_data)
         self.db.execute_query(object_query)
 
-        contact_obj_id = self.get_contact_object_id_by_name(data['contact_name'])
+        contact_obj_id = self.get_contact_object_id_by_name_sync(data['contact_name'],data['instance_id'])
+        #contact_id = self.get_contact_id(data['id'])
 
-        contacts_data = {'contact_id' : data['id'], 'instance_id' : data['instance_id'],
+        contacts_data = {'instance_id' : data['instance_id'],
                       'contact_object_id' : contact_obj_id,
                       'alias' : data['alias'],
                       'email_address' : data['email'], 'pager_address' : data['pager'],
@@ -785,9 +810,9 @@ class Ndodb_Mysql_broker(BaseModule):
         object_query = self.db.create_insert_query('objects', objects_data)
         self.db.execute_query(object_query)
 
-        contactgroup_id = self.get_contactgroup_object_id_by_name(data['contactgroup_name'])
+        contactgroup_id = self.get_contactgroup_object_id_by_name_sync(data['contactgroup_name'],data['instance_id'])
 
-        contactgroups_data = {'contactgroup_id' : data['id'], 'instance_id' :  data['instance_id'],
+        contactgroups_data = { 'instance_id' :  data['instance_id'],
                            'config_type' : 0, 'contactgroup_object_id' : contactgroup_id,
                            'alias' : data['alias']
             }
@@ -799,25 +824,27 @@ class Ndodb_Mysql_broker(BaseModule):
         #between hosts and hostgroups
         for (c_id, c_name) in b.data['members']:
             #print c_name
-            contact_obj_id = self.get_contact_object_id_by_name(c_name)
-            contactgroup_members_data = {'instance_id' : data['instance_id'], 'contactgroup_id' : data['id'],
+            contact_obj_id = self.get_contact_object_id_by_name_sync(c_name,data['instance_id'])
+            
+            contactgroup_members_data = {'instance_id' : data['instance_id'],
                                          'contact_object_id' : contact_obj_id}
             q = self.db.create_insert_query('contactgroup_members', contactgroup_members_data)
             res.append(q)
         return res
 
 
+
     #A notification have just be created, we INSERT it
     def manage_notification_raise_brok(self, b):
 
         data = b.data
         print "CREATING A NOTIFICATION", data
         if data['service_description'] != '':
-            object_id = self.get_service_object_id_by_name(data['host_name'], data['service_description'])
+             service_id = self.get_service_object_id_by_name_sync(data['host_name'], data['service_description'],data['instance_id'])
         else:
-            object_id = self.get_host_object_id_by_name(data['host_name'])
+             host_id = self.get_host_object_id_by_name_sync(data['host_name'],data['instance_id'])
 
-        notification_data = {'notification_id' : data['id'], 'instance_id' :  data['instance_id'],
+        notification_data = {'instance_id' :  data['instance_id'],
                              'start_time' : de_unixify(data['start_time']),
                              'end_time' : de_unixify(data['end_time']),
                              'state' : data['state']                             

-- 
UNNAMED PROJECT



More information about the Pkg-nagios-changes mailing list