]> arthur.barton.de Git - netdata.git/blobdiff - python.d/mysql.chart.py
fail2ban_plugin: parse both conf_path and conf_dir/; regex update
[netdata.git] / python.d / mysql.chart.py
index f741d91b03bf67fcc70667f90038c0e722264eb2..0e3a032998c2535f10a0d46a08408f0519254f09 100644 (file)
@@ -22,24 +22,25 @@ except ImportError:
         raise ImportError
 
 # default module values (can be overridden per job in `config`)
-update_every = 3
+update_every = 3
 priority = 90000
-retries = 7
+retries = 60
 
 # default configuration (overridden by python.d.plugin)
-config = {
-    'local': {
-        'user': 'root',
-        'password': '',
-        'socket': '/var/run/mysqld/mysqld.sock',
-        'update_every': update_every,
-        'retries': retries,
-        'priority': priority
-    }
-}
+config = {
+    'local': {
+        'user': 'root',
+#         'pass': '',
+        'socket': '/var/run/mysqld/mysqld.sock',
+        'update_every': update_every,
+        'retries': retries,
+        'priority': priority
+    }
+#}
 
 # query executed on MySQL server
-QUERY = "SHOW GLOBAL STATUS"
+QUERY = "SHOW GLOBAL STATUS;"
+QUERY_SLAVE = "SHOW SLAVE STATUS;"
 
 ORDER = ['net',
          'queries',
@@ -55,7 +56,7 @@ ORDER = ['net',
          'innodb_buffer_pool_read_ahead', 'innodb_buffer_pool_reqs', 'innodb_buffer_pool_ops',
          'qcache_ops', 'qcache', 'qcache_freemem', 'qcache_memblocks',
          'key_blocks', 'key_requests', 'key_disk_ops',
-         'files', 'files_rate']
+         'files', 'files_rate', 'slave_behind', 'slave_status']
 
 CHARTS = {
     'net': {
@@ -105,7 +106,7 @@ CHARTS = {
             ["Select_scan", "scan", "incremental"]
         ]},
     'sort_issues': {
-        'options': [None, 'mysql Sort Issues', 'issues/s', 'issues', 'mysql.sort.issues', 'line'],
+        'options': [None, 'mysql Sort Issues', 'issues/s', 'issues', 'mysql.sort_issues', 'line'],
         'lines': [
             ["Sort_merge_passes", "merge_passes", "incremental"],
             ["Sort_range", "range", "incremental"],
@@ -141,7 +142,7 @@ CHARTS = {
     'thread_cache_misses': {
         'options': [None, 'mysql Threads Cache Misses', 'misses', 'threads', 'mysql.thread_cache_misses', 'area'],
         'lines': [
-            ["Thread_cache_misses", "misses", "misses", "absolute", 1, 100]
+            ["Thread_cache_misses", "misses", "absolute", 1, 100]
         ]},
     'innodb_io': {
         'options': [None, 'mysql InnoDB I/O Bandwidth', 'kilobytes/s', 'innodb', 'mysql.innodb_io', 'area'],
@@ -168,7 +169,7 @@ CHARTS = {
         'lines': [
             ["Innodb_log_waits", "waits", "incremental"],
             ["Innodb_log_write_requests", "write_requests", "incremental", -1, 1],
-            ["Innodb_log_writes", "incremental", -1, 1],
+            ["Innodb_log_writes", "writes", "incremental", -1, 1],
         ]},
     'innodb_os_log': {
         'options': [None, 'mysql InnoDB OS Log Operations', 'operations', 'innodb', 'mysql.innodb_os_log', 'line'],
@@ -208,8 +209,8 @@ CHARTS = {
     'innodb_buffer_pool_bytes': {
         'options': [None, 'mysql InnoDB Buffer Pool Bytes', 'MB', 'innodb', 'mysql.innodb_buffer_pool_bytes', 'area'],
         'lines': [
-            ["Innodb_buffer_pool_bytes_data", "data", "absolute"],
-            ["Innodb_buffer_pool_bytes_dirty", "dirty", "absolute", -1, 1]
+            ["Innodb_buffer_pool_bytes_data", "data", "absolute", 1, 1024 * 1024],
+            ["Innodb_buffer_pool_bytes_dirty", "dirty", "absolute", -1, 1024 * 1024]
         ]},
     'innodb_buffer_pool_read_ahead': {
         'options': [None, 'mysql InnoDB Buffer Pool Read Ahead', 'operations/s', 'innodb', 'mysql.innodb_buffer_pool_read_ahead', 'area'],
@@ -246,7 +247,7 @@ CHARTS = {
     'qcache_freemem': {
         'options': [None, 'mysql QCache Free Memory', 'MB', 'qcache', 'mysql.qcache_freemem', 'area'],
         'lines': [
-            ["Qcache_free_memory", "free", "absolute"]
+            ["Qcache_free_memory", "free", "absolute", 1, 1024 * 1024]
         ]},
     'qcache_memblocks': {
         'options': [None, 'mysql QCache Memory Blocks', 'blocks', 'qcache', 'mysql.qcache_memblocks', 'line'],
@@ -298,8 +299,18 @@ CHARTS = {
             ["Connection_errors_peer_address", "peer_addr", "incremental"],
             ["Connection_errors_select", "select", "incremental"],
             ["Connection_errors_tcpwrap", "tcpwrap", "incremental"]
+        ]},
+    'slave_behind': {
+        'options': [None, 'Slave Behind Seconds', 'seconds', 'slave', 'mysql.slave_behind', 'line'],
+        'lines': [
+            ["slave_behind", "seconds", "absolute"]
+        ]},
+    'slave_status': {
+        'options': [None, 'Slave Status', 'status', 'slave', 'mysql.slave_status', 'line'],
+        'lines': [
+            ["slave_sql", "sql_running", "absolute"],
+            ["slave_io", "io_running", "absolute"]
         ]}
-
 }
 
 
@@ -310,6 +321,7 @@ class Service(SimpleService):
         self.order = ORDER
         self.definitions = CHARTS
         self.connection = None
+        self.do_slave = -1
 
     def _parse_config(self, configuration):
         """
@@ -317,44 +329,98 @@ class Service(SimpleService):
         :param configuration: dict
         :return: dict
         """
+        parameters = {}
         if self.name is None:
             self.name = 'local'
-        if 'user' not in configuration:
-            self.configuration['user'] = 'root'
-        if 'password' not in configuration:
-            self.configuration['password'] = ''
+        if 'user' in configuration:
+            parameters['user'] = self.configuration['user']
+        if 'pass' in configuration:
+            parameters['passwd'] = self.configuration['pass']
         if 'my.cnf' in configuration:
-            self.configuration['socket'] = ''
-            self.configuration['host'] = ''
-            self.configuration['port'] = 0
+            parameters['read_default_file'] = self.configuration['my.cnf']
         elif 'socket' in configuration:
-            self.configuration['my.cnf'] = ''
-            self.configuration['host'] = ''
-            self.configuration['port'] = 0
+            parameters['unix_socket'] = self.configuration['socket']
         elif 'host' in configuration:
-            self.configuration['my.cnf'] = ''
-            self.configuration['socket'] = ''
+            parameters['host'] = self.configuration['host']
             if 'port' in configuration:
-                self.configuration['port'] = int(configuration['port'])
-            else:
-                self.configuration['port'] = 3306
+                parameters['port'] = int(self.configuration['port'])
+        self.connection_parameters = parameters
 
     def _connect(self):
         """
         Try to connect to MySQL server
         """
         try:
-            self.connection = MySQLdb.connect(user=self.configuration['user'],
-                                              passwd=self.configuration['password'],
-                                              read_default_file=self.configuration['my.cnf'],
-                                              unix_socket=self.configuration['socket'],
-                                              host=self.configuration['host'],
-                                              port=self.configuration['port'],
-                                              connect_timeout=self.update_every)
+            self.connection = MySQLdb.connect(connect_timeout=self.update_every, **self.connection_parameters)
+        except MySQLdb.OperationalError as e:
+            self.error("Cannot establish connection to MySQL.")
+            self.debug(str(e))
+            raise RuntimeError
         except Exception as e:
             self.error("problem connecting to server:", e)
             raise RuntimeError
 
+    def _get_data_slave(self):
+        """
+        Get slave raw data from MySQL server
+        :return: dict
+        """
+        if self.connection is None:
+            try:
+                self._connect()
+            except RuntimeError:
+                return None
+
+        slave_data = None
+        slave_raw_data = None
+        try:
+            cursor = self.connection.cursor()
+            if cursor.execute(QUERY_SLAVE):
+                slave_raw_data = dict(list(zip([elem[0] for elem in cursor.description], cursor.fetchone())))
+
+        except MySQLdb.OperationalError as e:
+            self.debug("Reconnecting for query", QUERY_SLAVE, ":", str(e))
+            try:
+                self._connect()
+                cursor = self.connection.cursor()
+                if cursor.execute(QUERY_SLAVE):
+                    slave_raw_data = dict(list(zip([elem[0] for elem in cursor.description], cursor.fetchone())))
+            except Exception as e:
+                self.error("retried, but cannot execute query", QUERY_SLAVE, ":", str(e))
+                self.connection.close()
+                self.connection = None
+                return None
+
+        except Exception as e:
+            self.error("cannot execute query", QUERY_SLAVE, ":", str(e))
+            self.connection.close()
+            self.connection = None
+            return None
+
+        if slave_raw_data is not None:
+            slave_data = {
+                'slave_behind': None,
+                'slave_sql': None,
+                'slave_io': None
+            }
+
+            try:
+                slave_data['slave_behind'] = int(slave_raw_data.setdefault('Seconds_Behind_Master', -1))
+            except:
+                slave_data['slave_behind'] = None
+
+            try:
+                slave_data['slave_sql'] = 1 if slave_raw_data.get('Slave_SQL_Running') == 'Yes' else -1
+            except:
+                slave_data['slave_sql'] = None
+
+            try:
+                slave_data['slave_io'] = 1 if slave_raw_data.get('Slave_IO_Running') == 'Yes' else -1
+            except:
+                slave_data['slave_io'] = None
+
+        return slave_data
+
     def _get_data(self):
         """
         Get raw data from MySQL server
@@ -369,16 +435,47 @@ class Service(SimpleService):
             cursor = self.connection.cursor()
             cursor.execute(QUERY)
             raw_data = cursor.fetchall()
+
+        except MySQLdb.OperationalError as e:
+            self.debug("Reconnecting for query", QUERY, ":", str(e))
+            try:
+                self._connect()
+                cursor = self.connection.cursor()
+                cursor.execute(QUERY)
+                raw_data = cursor.fetchall()
+            except Exception as e:
+                self.error("retried, but cannot execute query", QUERY, ":", str(e))
+                self.connection.close()
+                self.connection = None
+                return None
+
         except Exception as e:
-            self.error("cannot execute query.", e)
+            self.error("cannot execute query", QUERY, ":", str(e))
             self.connection.close()
             self.connection = None
             return None
+
         data = dict(raw_data)
+
+        # check for slave data
+        # the first time is -1 (so we do it)
+        # then it is set to 1 or 0 and we keep it like that
+        if self.do_slave != 0:
+            slave_data = self._get_data_slave()
+            if slave_data is not None:
+                data.update(slave_data)
+                if self.do_slave == -1:
+                    self.do_slave = 1
+            else:
+                if self.do_slave == -1:
+                    self.error("replication metrics will be disabled - please allow netdata to collect them.")
+                    self.do_slave = 0
+
+        # do calculations
         try:
-            data["Thread_cache_misses"] = int(data["Threads_created"] * 10000 / float(data["Connections"]))
+            data["Thread_cache_misses"] = round(float(data["Threads_created"]) / float(data["Connections"]) * 10000)
         except:
-            data["Thread_cache_misses"] = 0
+            data["Thread_cache_misses"] = None
 
         return data