make replication metrics optional; fixes #1472
Costa Tsaousis (ktsaou) committed
Dec 30, 2016 at 01:22 UTC
616e4dc7f9c0ba7fbbf053c1964d2cc7891256b5
1 file changed
+70
-20
python.d/mysql.chart.py
+70
-20
@@ -321,6 +321,7 @@ class Service(SimpleService):
321
self.order = ORDER
322
self.definitions = CHARTS
323
self.connection = None
324
+ self.do_slave = -1
325
326
def _parse_config(self, configuration):
327
"""
@@ -359,9 +360,9 @@ class Service(SimpleService):
360
self.error("problem connecting to server:", e)
361
raise RuntimeError
362
362
- def _get_data(self):
363
+ def _get_data_slave(self):
364
"""
364
- Get raw data from MySQL server
365
+ Get slave raw data from MySQL server
366
:return: dict
367
"""
368
if self.connection is None:
@@ -369,45 +370,94 @@ class Service(SimpleService):
370
self._connect()
371
except RuntimeError:
372
return None
373
+
374
+ slave_data = None
375
+ slave_raw_data = None
376
try:
377
cursor = self.connection.cursor()
374
- cursor.execute(QUERY)
375
- raw_data = cursor.fetchall()
376
- slave_data = {}
378
if cursor.execute(QUERY_SLAVE):
378
- slave_data = {'slave_behind': 0, 'slave_sql': 0, 'slave_io': 0 }
379
slave_raw_data = dict(list(zip([elem[0] for elem in cursor.description], cursor.fetchone())))
380
- slave_data['slave_behind'] = int(slave_raw_data.setdefault('Seconds_Behind_Master', -1))
381
- slave_data['slave_sql'] = 1 if slave_raw_data.get('Slave_SQL_Running') == 'Yes' else -1
382
- slave_data['slave_io'] = 1 if slave_raw_data.get('Slave_IO_Running') == 'Yes' else -1
380
381
except MySQLdb.OperationalError as e:
385
- self.debug("Reconnecting due to", str(e))
386
- self._connect()
382
+ self.debug("Reconnecting for query", QUERY_SLAVE, ":", str(e))
383
+ try:
384
+ self._connect()
385
+ cursor = self.connection.cursor()
386
+ if cursor.execute(QUERY_SLAVE):
387
+ slave_raw_data = dict(list(zip([elem[0] for elem in cursor.description], cursor.fetchone())))
388
+ except Exception as e:
389
+ self.error("retried, but cannot execute query", QUERY_SLAVE, ":", str(e))
390
+ self.connection.close()
391
+ self.connection = None
392
+ return None
393
+
394
+ except Exception as e:
395
+ self.error("cannot execute query", QUERY_SLAVE, ":", str(e))
396
+ self.connection.close()
397
+ self.connection = None
398
+ return None
399
+
400
+ if slave_raw_data is not None:
401
+ slave_data = {'slave_behind': 0, 'slave_sql': 0, 'slave_io': 0 }
402
+ slave_data['slave_behind'] = int(slave_raw_data.setdefault('Seconds_Behind_Master', -1))
403
+ slave_data['slave_sql'] = 1 if slave_raw_data.get('Slave_SQL_Running') == 'Yes' else -1
404
+ slave_data['slave_io'] = 1 if slave_raw_data.get('Slave_IO_Running') == 'Yes' else -1
405
+
406
+ return slave_data
407
+
408
+ def _get_data(self):
409
+ """
410
+ Get raw data from MySQL server
411
+ :return: dict
412
+ """
413
+ if self.connection is None:
414
+ try:
415
+ self._connect()
416
+ except RuntimeError:
417
+ return None
418
+ try:
419
cursor = self.connection.cursor()
420
cursor.execute(QUERY)
421
raw_data = cursor.fetchall()
390
- slave_data = {}
391
- if cursor.execute(QUERY_SLAVE):
392
- slave_data = {'slave_behind': 0, 'slave_sql': 0, 'slave_io': 0 }
393
- slave_raw_data = dict(list(zip([elem[0] for elem in cursor.description], cursor.fetchone())))
394
- slave_data['slave_behind'] = int(slave_raw_data.setdefault('Seconds_Behind_Master', -1))
395
- slave_data['slave_sql'] = 1 if slave_raw_data.get('Slave_SQL_Running') == 'Yes' else -1
396
- slave_data['slave_io'] = 1 if slave_raw_data.get('Slave_IO_Running') == 'Yes' else -1
422
+
423
+ except MySQLdb.OperationalError as e:
424
+ self.debug("Reconnecting for query", QUERY, ":", str(e))
425
+ try:
426
+ self._connect()
427
+ cursor = self.connection.cursor()
428
+ cursor.execute(QUERY)
429
+ raw_data = cursor.fetchall()
430
+ except Exception as e:
431
+ self.error("retried, but cannot execute query", QUERY, ":", str(e))
432
+ self.connection.close()
433
+ self.connection = None
434
+ return None
435
436
except Exception as e:
399
- self.error("cannot execute query.", e)
437
+ self.error("cannot execute query", QUERY, ":", str(e))
438
self.connection.close()
439
self.connection = None
440
return None
441
442
data = dict(raw_data)
443
+
444
+ # check for slave data
445
+ if self.do_slave != 0:
446
+ slave_data = self._get_data_slave()
447
+ if slave_data is not None:
448
+ data.update(slave_data)
449
+ self.do_slave = 1
450
+ else:
451
+ if self.do_slave == -1:
452
+ self.error("replication metrics will be disabled - please allow netdata to collect them.")
453
+ self.do_slave = 0
454
+
455
+ # do calculations
456
try:
457
data["Thread_cache_misses"] = int(data["Threads_created"] * 10000 / float(data["Connections"]))
458
except:
459
data["Thread_cache_misses"] = 0
460
410
- data.update(slave_data)
461
return data
462
463
def check(self):