Skip to content

Commit 3858a80

Browse files
committed
MySQLService: extra connection parameters can be defined in a module
1 parent f1e4c84 commit 3858a80

1 file changed

Lines changed: 10 additions & 7 deletions

File tree

python.d/python_modules/base.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -946,20 +946,21 @@ class MySQLService(SimpleService):
946946
def __init__(self, configuration=None, name=None):
947947
SimpleService.__init__(self, configuration=configuration, name=name)
948948
self.__connection = None
949-
self.conn_properties = dict()
949+
self.__conn_properties = dict()
950+
self.extra_conn_properties = dict()
950951
self.__queries = self.configuration.get('queries', dict())
951952
self.queries = dict()
952953

953954
def __connect(self):
954955
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:
957958
return None, str(error)
958959
else:
959960
return connection, None
960961

961962
def check(self):
962-
def get_connection_properties(conf):
963+
def get_connection_properties(conf, extra_conf):
963964
properties = dict()
964965
if 'user' in conf and conf['user']:
965966
properties['user'] = conf['user']
@@ -969,9 +970,11 @@ def get_connection_properties(conf):
969970
properties['unix_socket'] = conf['socket']
970971
elif 'host' in conf and conf['host']:
971972
properties['host'] = conf['host']
972-
properties['port'] = int(conf['port']) if conf.get('port') else 3306
973+
properties['port'] = int(conf.get('port', 3306))
973974
elif 'my.cnf' in conf and conf['my.cnf']:
974975
properties['read_default_file'] = conf['my.cnf']
976+
if isinstance(extra_conf, dict) and extra_conf:
977+
properties.update(extra_conf)
975978

976979
return properties or None
977980

@@ -1010,8 +1013,8 @@ def is_valid_query(query):
10101013
return None
10111014

10121015
# 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:
10151018
self.error('Connection properties are missing')
10161019
return False
10171020

0 commit comments

Comments
 (0)