part 1
paulfantom committed
Jun 17, 2016 at 00:15 UTC
07a267ea2054d2088ca1fbefc66e70adccc9cd81
1 file changed
+78
-97
python.d/mysql.chart.py
+78
-97
@@ -1,4 +1,3 @@
1
-#!/usr/bin/python3 -u
1
2
NAME = "mysql.chart.py"
3
from sys import stderr
@@ -307,97 +306,85 @@ CHARTS = {
306
("Connection_errors_tcpwrap", "tcpwrap incremental 1 1")
307
))
308
}
310
-mysql_def = {}
311
-valid = []
312
-connections = {}
309
314
-def get_data(config):
315
- global connections
316
- try:
317
- cnx = connections[config['name']]
318
- except KeyError as e:
319
- stderr.write(NAME + ": reconnecting\n")
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'],
325
- port=config['port'],
326
- connect_timeout=int(update_every))
327
- connections[config['name']] = cnx
310
329
- try:
330
- with cnx.cursor() as cursor:
331
- cursor.execute(QUERY)
332
- raw_data = cursor.fetchall()
333
- except Exception as e:
334
- stderr.write(NAME + ": cannot execute query." + str(e) + "\n")
335
- cnx.close()
336
- del connections[config['name']]
337
- return None
338
-
339
- return dict(raw_data)
311
+def Service(object):
312
+ def __init__(self,config=None):
313
+ if config is None:
314
+ pass # TODO use defaults
315
+ if 'name' not in config:
316
+ from random import randint
317
+ config['name'] = "srv_"+str(randint(0,99)
318
+ if 'user' not in config:
319
+ config['user'] = 'root'
320
+ if 'password' not in config:
321
+ config['password'] = ''
322
+ if 'my.cnf' in config:
323
+ config['socket'] = ''
324
+ config['host'] = ''
325
+ config['port'] = 0
326
+ elif 'socket' in config:
327
+ config['my.cnf'] = ''
328
+ config['host'] = ''
329
+ config['port'] = 0
330
+ elif 'host' in config:
331
+ config['my.cnf'] = ''
332
+ config['socket'] = ''
333
+ if 'port' in config:
334
+ config['port'] = int(config['port'])
335
+ else:
336
+ config['port'] = 3306
337
338
+ self.config = config
339
+ self.connection = None
340
+ self.mysql_def = {}
341
342
-def check():
343
- # TODO what are the default credentials
344
- global valid, config
345
- if type(config) is str:
346
- from json import loads
347
- cfg = loads(config.replace("'",'"').replace('\n',' '))
348
- config = cfg
349
- for i in range(len(config)):
350
- if 'name' not in config[i]:
351
- config[i]['name'] = "srv_"+str(i)
352
- if 'user' not in config[i]:
353
- config[i]['user'] = 'root'
354
- if 'password' not in config[i]:
355
- config[i]['password'] = ''
356
- if 'my.cnf' in config[i]:
357
- config[i]['socket'] = ''
358
- config[i]['host'] = ''
359
- config[i]['port'] = 0
360
- elif 'socket' in config[i]:
361
- config[i]['my.cnf'] = ''
362
- config[i]['host'] = ''
363
- config[i]['port'] = 0
364
- elif 'host' in config[i]:
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:
370
- config[i]['port'] = 3306
342
+ def check(self):
343
+ try:
344
+ self.connection = self._connect()
345
+ return True
346
+ except RuntimeError:
347
+ self.connection = None
348
+ return False
349
+
350
+
351
372
- for srv in config:
352
+ def _connect(self):
353
try:
374
- cnx = MySQLdb.connect(user=srv['user'],
375
- passwd=srv['password'],
376
- read_default_file=srv['my.cnf'],
377
- unix_socket=srv['socket'],
378
- host=srv['host'],
379
- port=srv['port'],
380
- connect_timeout=int(update_every))
381
- cnx.close()
354
+ self.connection = MySQLdb.connect(user=self.config['user'],
355
+ passwd=self.config['password'],
356
+ read_default_file=self.config['my.cnf'],
357
+ unix_socket=self.config['socket'],
358
+ host=self.config['host'],
359
+ port=self.config['port'],
360
+ connect_timeout=int(update_every))
361
except Exception as e:
362
stderr.write(NAME + " has problem connecting to server: "+str(e).replace("\n"," ")+"\n")
384
- config.remove(srv)
363
+ raise RuntimeError #stop creating module, need to catch it in supervisor
364
386
- if len(config) == 0:
387
- return False
388
- return True
365
+
366
+ def _get_data(self):
367
+ try:
368
+ with self.connection.cursor() as cursor:
369
+ cursor.execute(QUERY)
370
+ raw_data = cursor.fetchall()
371
+ except Exception as e:
372
+ stderr.write(NAME + ": cannot execute query." + str(e) + "\n")
373
+ self.connection.close()
374
+ return None
375
+
376
+ return dict(raw_data)
377
378
391
-def create():
392
- global config, mysql_def
393
- for name in ORDER:
394
- mysql_def[name] = []
395
- for line in CHARTS[name][1]:
396
- mysql_def[name].append(line[0])
379
398
- idx = 0
399
- for srv in config:
400
- data = get_data(srv)
380
+ def create(self):
381
+ for name in ORDER:
382
+ self.mysql_def[name] = []
383
+ for line in CHARTS[name][1]:
384
+ self.mysql_def[name].append(line[0])
385
+
386
+ idx = 0
387
+ data = self._get_data(self.config)
388
for name in ORDER:
389
header = "CHART mysql_" + \
390
str(srv['name']) + "." + \
@@ -414,26 +401,22 @@ def create():
401
print(header)
402
print(content)
403
idx += 1
417
-
418
- if idx == 0:
419
- return False
420
- return True
404
+
405
+ if idx == 0:
406
+ return False
407
+ return True
408
409
423
-def update(interval):
424
- global config
425
- for srv in config:
426
- data = get_data(srv)
410
+ def update(self,interval):
411
+ data = self._get_data(self.config)
412
if data is None:
428
- config.remove(srv)
429
- # TODO notify user about problems with server
430
- continue
413
+ return False
414
try:
415
data['Thread cache misses'] = int( int(data['Threads_created']) * 10000 / int(data['Connections']))
416
except Exception:
417
pass
435
- for chart, dimensions in mysql_def.items():
436
- header = "BEGIN mysql_" + str(srv['name']) + "." + chart + " " + str(interval) + '\n'
418
+ for chart, dimensions in self._mysql_def.items():
419
+ header = "BEGIN mysql_" + str(self.config['name']) + "." + chart + " " + str(interval) + '\n'
420
lines = ""
421
for d in dimensions:
422
try:
@@ -442,7 +425,5 @@ def update(interval):
425
pass
426
if len(lines) > 0:
427
print(header + lines + "END")
445
-
446
- if len(config) == 0:
447
- return False
448
- return True
428
+
429
+ return True