MySQLService: extra connection parameters can be defined in a module
Ilya committed
Mar 11, 2017 at 18:46 UTC
3858a800fbafed6dd0151c6744122a7b5f596706
1 file changed
+10
-7
python.d/python_modules/base.py
+10
-7
@@ -946,20 +946,21 @@ class MySQLService(SimpleService):
946
def __init__(self, configuration=None, name=None):
947
SimpleService.__init__(self, configuration=configuration, name=name)
948
self.__connection = None
949
- self.conn_properties = dict()
949
+ self.__conn_properties = dict()
950
+ self.extra_conn_properties = dict()
951
self.__queries = self.configuration.get('queries', dict())
952
self.queries = dict()
953
954
def __connect(self):
955
try:
955
- connection = MySQLdb.connect(connect_timeout=self.update_every, **self.conn_properties)
956
- except (MySQLdb.MySQLError, TypeError) as error:
956
+ connection = MySQLdb.connect(connect_timeout=self.update_every, **self.__conn_properties)
957
+ except (MySQLdb.MySQLError, TypeError, AttributeError) as error:
958
return None, str(error)
959
else:
960
return connection, None
961
962
def check(self):
962
- def get_connection_properties(conf):
963
+ def get_connection_properties(conf, extra_conf):
964
properties = dict()
965
if 'user' in conf and conf['user']:
966
properties['user'] = conf['user']
@@ -969,9 +970,11 @@ class MySQLService(SimpleService):
970
properties['unix_socket'] = conf['socket']
971
elif 'host' in conf and conf['host']:
972
properties['host'] = conf['host']
972
- properties['port'] = int(conf['port']) if conf.get('port') else 3306
973
+ properties['port'] = int(conf.get('port', 3306))
974
elif 'my.cnf' in conf and conf['my.cnf']:
975
properties['read_default_file'] = conf['my.cnf']
976
+ if isinstance(extra_conf, dict) and extra_conf:
977
+ properties.update(extra_conf)
978
979
return properties or None
980
@@ -1010,8 +1013,8 @@ class MySQLService(SimpleService):
1013
return None
1014
1015
# Get connection properties
1013
- self.conn_properties = get_connection_properties(self.configuration)
1014
- if not self.conn_properties:
1016
+ self.__conn_properties = get_connection_properties(self.configuration, self.extra_conn_properties)
1017
+ if not self.__conn_properties:
1018
self.error('Connection properties are missing')
1019
return False
1020