use MySQLdb or fallback to PyMySQL
paulfantom committed
Jun 15, 2016 at 11:34 UTC
3ebfed1cd06860b46c1e27f77c7888e178251383
1 file changed
+19
-13
python.d/mysql.chart.py
+19
-13
@@ -3,16 +3,22 @@
3
NAME = "mysql.chart.py"
4
from sys import stderr
5
try:
6
- import pymysql.cursors
7
- # https://github.com/PyMySQL/PyMySQL
6
+ import MySQLdb
7
+ stderr.write(NAME + ": using MySQLdb")
8
+ # https://github.com/PyMySQL/mysqlclient-python
9
except ImportError:
9
- stderr.write(NAME + ": You need to install PyMySQL module to use mysql.chart.py plugin\n")
10
+ try:
11
+ import pymysql as MySQLdb
12
+ # https://github.com/PyMySQL/PyMySQL
13
+ stderr.write(NAME + ": using pymysql")
14
+ except ImportError:
15
+ stderr.write(NAME + ": You need to install PyMySQL module to use mysql.chart.py plugin\n")
16
17
config = [
18
{
19
'name' : 'local',
20
'user' : 'root',
15
- 'password' : None,
21
+ 'password' : '',
22
'socket' : '/var/run/mysqld/mysqld.sock'
23
}
24
]
@@ -311,8 +317,8 @@ def get_data(config):
317
cnx = connections[config['name']]
318
except KeyError as e:
319
stderr.write(NAME + ": reconnecting\n")
314
- cnx = pymysql.connect(user=config['user'],
315
- password=config['password'],
320
+ cnx = MySQLdb.connect(user=config['user'],
321
+ passwd=config['password'],
322
read_default_file=config['my.cnf'],
323
unix_socket=config['socket'],
324
host=config['host'],
@@ -348,16 +354,16 @@ def check():
354
if 'password' not in config[i]:
355
config[i]['password'] = ''
356
if 'my.cnf' in config[i]:
351
- config[i]['socket'] = None
352
- config[i]['host'] = None
357
+ config[i]['socket'] = ''
358
+ config[i]['host'] = ''
359
config[i]['port'] = 0
360
elif 'socket' in config[i]:
355
- config[i]['my.cnf'] = None
356
- config[i]['host'] = None
361
+ config[i]['my.cnf'] = ''
362
+ config[i]['host'] = ''
363
config[i]['port'] = 0
364
elif 'host' in config[i]:
359
- config[i]['my.cnf'] = None
360
- config[i]['socket'] = None
365
+ config[i]['my.cnf'] = ''
366
+ config[i]['socket'] = ''
367
if 'port' in config[i]:
368
config[i]['port'] = int(config[i]['port'])
369
else:
@@ -365,7 +371,7 @@ def check():
371
372
for srv in config:
373
try:
368
- cnx = pymysql.connect(user=srv['user'],
374
+ cnx = MySQLdb.connect(user=srv['user'],
375
password=srv['password'],
376
read_default_file=srv['my.cnf'],
377
unix_socket=srv['socket'],