tested mysql.chart.py
paulfantom committed
Jun 17, 2016 at 14:49 UTC
526092446e3b2fb972c612e79302996cb13d9edb
1 file changed
+18
-14
python.d/mysql.chart.py
+18
-14
@@ -8,12 +8,12 @@ import sys
8
try:
9
import MySQLdb
10
# https://github.com/PyMySQL/mysqlclient-python
11
- sys.stderr.write(NAME + ": using MySQLdb")
11
+ sys.stderr.write(NAME + ": using MySQLdb\n")
12
except ImportError:
13
try:
14
import pymysql as MySQLdb
15
# https://github.com/PyMySQL/PyMySQL
16
- sys.stderr.write(NAME + ": using pymysql")
16
+ sys.stderr.write(NAME + ": using pymysql\n")
17
except ImportError:
18
sys.stderr.write(NAME + ": You need to install PyMySQL module to use mysql.chart.py plugin\n")
19
@@ -22,7 +22,7 @@ config = [
22
{
23
'name' : 'local',
24
'user' : 'root',
25
- 'password' : '',
25
+ 'password' : 'a',
26
'socket' : '/var/run/mysqld/mysqld.sock',
27
'update_every' : 3,
28
'retries' : 4
@@ -330,7 +330,7 @@ class BaseService(object):
330
configuration = config
331
self.error(NAME+": no configuration supplied. using defaults.")
332
333
- self._parse_base_config(configuration)
333
+ self._parse_base_config(configuration,update_every,priority,retries)
334
335
def _parse_base_config(self,config,update_every,priority,retries):
336
# parse configuration options to run this Service
@@ -372,12 +372,12 @@ class BaseService(object):
372
373
class Service(BaseService):
374
def __init__(self,configuration=None,update_every=3,priority=90000,retries=2):
375
- super().__init__(*args,**kwargs)
375
+ super().__init__(configuration,update_every,priority,retries)
376
self.configuration = self._parse_config(configuration)
377
self.connection = None
378
- self.defs = None
378
+ self.defs = {}
379
380
- def _parse_config(configuration):
380
+ def _parse_config(self,configuration):
381
# parse configuration to collect data from mysql server
382
if 'name' not in configuration:
383
configuration['name'] = 'local'
@@ -417,6 +417,8 @@ class Service(BaseService):
417
raise RuntimeError #stop creating module, need to catch it in supervisor
418
419
def _get_data(self):
420
+ if self.connection is None:
421
+ self._connect()
422
try:
423
with self.connection.cursor() as cursor:
424
cursor.execute(QUERY)
@@ -424,6 +426,7 @@ class Service(BaseService):
426
except Exception as e:
427
self.error(NAME + ": cannot execute query.", e)
428
self.connection.close()
429
+ self.connection = None
430
return None
431
432
return dict(raw_data)
@@ -438,19 +441,20 @@ class Service(BaseService):
441
442
def create(self):
443
for name in ORDER:
441
- self.mysql_def[name] = []
444
+ print(name)
445
+ self.defs[name] = []
446
for line in CHARTS[name][1]:
447
self.defs[name].append(line[0])
448
449
idx = 0
446
- data = self._get_data(self.configuration)
450
+ data = self._get_data()
451
for name in ORDER:
452
header = "CHART mysql_" + \
449
- str(srv['name']) + "." + \
453
+ str(self.configuration['name']) + "." + \
454
name + " " + \
455
CHARTS[name][0] + " " + \
452
- str(priority + idx) + " " + \
453
- str(update_every)
456
+ str(self.priority + idx) + " " + \
457
+ str(self.update_every)
458
content = ""
459
# check if server has this datapoint
460
for line in CHARTS[name][1]:
@@ -466,7 +470,7 @@ class Service(BaseService):
470
return True
471
472
def update(self,interval):
469
- data = self._get_data(self.configuration)
473
+ data = self._get_data()
474
if data is None:
475
return False
476
try:
@@ -487,7 +491,7 @@ class Service(BaseService):
491
return True
492
493
if __name__ == "__main__":
490
- my = Service(config)
494
+ my = Service(config[0])
495
my.check()
496
my.create()
497
my.update(1)