1 # -*- coding: utf-8 -*-
2 # Description: MySQL netdata python.d module
3 # Author: Pawel Krupa (paulfantom)
5 from base import SimpleService
8 # import 3rd party library to handle MySQL communication
12 # https://github.com/PyMySQL/mysqlclient-python
13 msg.info("using MySQLdb")
16 import pymysql as MySQLdb
18 # https://github.com/PyMySQL/PyMySQL
19 msg.info("using pymysql")
21 msg.error("MySQLdb or PyMySQL module is needed to use mysql.chart.py plugin")
24 # default module values (can be overridden per job in `config`)
29 # default configuration (overridden by python.d.plugin)
34 # 'socket': '/var/run/mysqld/mysqld.sock',
35 # 'update_every': update_every,
37 # 'priority': priority
41 # query executed on MySQL server
42 QUERY = "SHOW GLOBAL STATUS;"
48 'join_issues', 'sort_issues',
50 'connections', 'connection_errors',
51 'binlog_cache', 'binlog_stmt_cache',
52 'threads', 'thread_cache_misses',
53 'innodb_io', 'innodb_io_ops', 'innodb_io_pending_ops', 'innodb_log', 'innodb_os_log', 'innodb_os_log_io',
54 'innodb_cur_row_lock', 'innodb_rows', 'innodb_buffer_pool_pages', 'innodb_buffer_pool_bytes',
55 'innodb_buffer_pool_read_ahead', 'innodb_buffer_pool_reqs', 'innodb_buffer_pool_ops',
56 'qcache_ops', 'qcache', 'qcache_freemem', 'qcache_memblocks',
57 'key_blocks', 'key_requests', 'key_disk_ops',
58 'files', 'files_rate']
62 'options': [None, 'mysql Bandwidth', 'kilobits/s', 'bandwidth', 'mysql.net', 'area'],
64 ["Bytes_received", "in", "incremental", 8, 1024],
65 ["Bytes_sent", "out", "incremental", -8, 1024]
68 'options': [None, 'mysql Queries', 'queries/s', 'queries', 'mysql.queries', 'line'],
70 ["Queries", "queries", "incremental"],
71 ["Questions", "questions", "incremental"],
72 ["Slow_queries", "slow_queries", "incremental"]
75 'options': [None, 'mysql Handlers', 'handlers/s', 'handlers', 'mysql.handlers', 'line'],
77 ["Handler_commit", "commit", "incremental"],
78 ["Handler_delete", "delete", "incremental"],
79 ["Handler_prepare", "prepare", "incremental"],
80 ["Handler_read_first", "read_first", "incremental"],
81 ["Handler_read_key", "read_key", "incremental"],
82 ["Handler_read_next", "read_next", "incremental"],
83 ["Handler_read_prev", "read_prev", "incremental"],
84 ["Handler_read_rnd", "read_rnd", "incremental"],
85 ["Handler_read_rnd_next", "read_rnd_next", "incremental"],
86 ["Handler_rollback", "rollback", "incremental"],
87 ["Handler_savepoint", "savepoint", "incremental"],
88 ["Handler_savepoint_rollback", "savepoint_rollback", "incremental"],
89 ["Handler_update", "update", "incremental"],
90 ["Handler_write", "write", "incremental"]
93 'options': [None, 'mysql Tables Locks', 'locks/s', 'locks', 'mysql.table_locks', 'line'],
95 ["Table_locks_immediate", "immediate", "incremental"],
96 ["Table_locks_waited", "waited", "incremental", -1, 1]
99 'options': [None, 'mysql Select Join Issues', 'joins/s', 'issues', 'mysql.join_issues', 'line'],
101 ["Select_full_join", "full_join", "incremental"],
102 ["Select_full_range_join", "full_range_join", "incremental"],
103 ["Select_range", "range", "incremental"],
104 ["Select_range_check", "range_check", "incremental"],
105 ["Select_scan", "scan", "incremental"]
108 'options': [None, 'mysql Sort Issues', 'issues/s', 'issues', 'mysql.sort_issues', 'line'],
110 ["Sort_merge_passes", "merge_passes", "incremental"],
111 ["Sort_range", "range", "incremental"],
112 ["Sort_scan", "scan", "incremental"]
115 'options': [None, 'mysql Tmp Operations', 'counter', 'temporaries', 'mysql.tmp', 'line'],
117 ["Created_tmp_disk_tables", "disk_tables", "incremental"],
118 ["Created_tmp_files", "files", "incremental"],
119 ["Created_tmp_tables", "tables", "incremental"]
122 'options': [None, 'mysql Connections', 'connections/s', 'connections', 'mysql.connections', 'line'],
124 ["Connections", "all", "incremental"],
125 ["Aborted_connects", "aborted", "incremental"]
128 'options': [None, 'mysql Binlog Cache', 'transactions/s', 'binlog', 'mysql.binlog_cache', 'line'],
130 ["Binlog_cache_disk_use", "disk", "incremental"],
131 ["Binlog_cache_use", "all", "incremental"]
134 'options': [None, 'mysql Threads', 'threads', 'threads', 'mysql.threads', 'line'],
136 ["Threads_connected", "connected", "absolute"],
137 ["Threads_created", "created", "incremental"],
138 ["Threads_cached", "cached", "absolute", -1, 1],
139 ["Threads_running", "running", "absolute"],
141 'thread_cache_misses': {
142 'options': [None, 'mysql Threads Cache Misses', 'misses', 'threads', 'mysql.thread_cache_misses', 'area'],
144 ["Thread_cache_misses", "misses", "absolute", 1, 100]
147 'options': [None, 'mysql InnoDB I/O Bandwidth', 'kilobytes/s', 'innodb', 'mysql.innodb_io', 'area'],
149 ["Innodb_data_read", "read", "incremental", 1, 1024],
150 ["Innodb_data_written", "write", "incremental", -1, 1024]
153 'options': [None, 'mysql InnoDB I/O Operations', 'operations/s', 'innodb', 'mysql.innodb_io_ops', 'line'],
155 ["Innodb_data_reads", "reads", "incremental"],
156 ["Innodb_data_writes", "writes", "incremental", -1, 1],
157 ["Innodb_data_fsyncs", "fsyncs", "incremental"]
159 'innodb_io_pending_ops': {
160 'options': [None, 'mysql InnoDB Pending I/O Operations', 'operations', 'innodb', 'mysql.innodb_io_pending_ops', 'line'],
162 ["Innodb_data_pending_reads", "reads", "absolute"],
163 ["Innodb_data_pending_writes", "writes", "absolute", -1, 1],
164 ["Innodb_data_pending_fsyncs", "fsyncs", "absolute"]
167 'options': [None, 'mysql InnoDB Log Operations', 'operations/s', 'innodb', 'mysql.innodb_log', 'line'],
169 ["Innodb_log_waits", "waits", "incremental"],
170 ["Innodb_log_write_requests", "write_requests", "incremental", -1, 1],
171 ["Innodb_log_writes", "writes", "incremental", -1, 1],
174 'options': [None, 'mysql InnoDB OS Log Operations', 'operations', 'innodb', 'mysql.innodb_os_log', 'line'],
176 ["Innodb_os_log_fsyncs", "fsyncs", "incremental"],
177 ["Innodb_os_log_pending_fsyncs", "pending_fsyncs", "absolute"],
178 ["Innodb_os_log_pending_writes", "pending_writes", "absolute", -1, 1],
180 'innodb_os_log_io': {
181 'options': [None, 'mysql InnoDB OS Log Bandwidth', 'kilobytes/s', 'innodb', 'mysql.innodb_os_log_io', 'area'],
183 ["Innodb_os_log_written", "write", "incremental", -1, 1024],
185 'innodb_cur_row_lock': {
186 'options': [None, 'mysql InnoDB Current Row Locks', 'operations', 'innodb', 'mysql.innodb_cur_row_lock', 'area'],
188 ["Innodb_row_lock_current_waits", "current_waits", "absolute"]
191 'options': [None, 'mysql InnoDB Row Operations', 'operations/s', 'innodb', 'mysql.innodb_rows', 'area'],
193 ["Innodb_rows_inserted", "read", "incremental"],
194 ["Innodb_rows_read", "deleted", "incremental", -1, 1],
195 ["Innodb_rows_updated", "inserted", "incremental", 1, 1],
196 ["Innodb_rows_deleted", "updated", "incremental", -1, 1],
198 'innodb_buffer_pool_pages': {
199 'options': [None, 'mysql InnoDB Buffer Pool Pages', 'pages', 'innodb', 'mysql.innodb_buffer_pool_pages', 'line'],
201 ["Innodb_buffer_pool_pages_data", "data", "absolute"],
202 ["Innodb_buffer_pool_pages_dirty", "dirty", "absolute", -1, 1],
203 ["Innodb_buffer_pool_pages_free", "free", "absolute"],
204 ["Innodb_buffer_pool_pages_flushed", "flushed", "incremental", -1, 1],
205 ["Innodb_buffer_pool_pages_misc", "misc", "absolute", -1, 1],
206 ["Innodb_buffer_pool_pages_total", "total", "absolute"]
208 'innodb_buffer_pool_bytes': {
209 'options': [None, 'mysql InnoDB Buffer Pool Bytes', 'MB', 'innodb', 'mysql.innodb_buffer_pool_bytes', 'area'],
211 ["Innodb_buffer_pool_bytes_data", "data", "absolute", 1, 1024 * 1024],
212 ["Innodb_buffer_pool_bytes_dirty", "dirty", "absolute", -1, 1024 * 1024]
214 'innodb_buffer_pool_read_ahead': {
215 'options': [None, 'mysql InnoDB Buffer Pool Read Ahead', 'operations/s', 'innodb', 'mysql.innodb_buffer_pool_read_ahead', 'area'],
217 ["Innodb_buffer_pool_read_ahead", "all", "incremental"],
218 ["Innodb_buffer_pool_read_ahead_evicted", "evicted", "incremental", -1, 1],
219 ["Innodb_buffer_pool_read_ahead_rnd", "random", "incremental"]
221 'innodb_buffer_pool_reqs': {
222 'options': [None, 'mysql InnoDB Buffer Pool Requests', 'requests/s', 'innodb', 'mysql.innodb_buffer_pool_reqs', 'area'],
224 ["Innodb_buffer_pool_read_requests", "reads", "incremental"],
225 ["Innodb_buffer_pool_write_requests", "writes", "incremental", -1, 1]
227 'innodb_buffer_pool_ops': {
228 'options': [None, 'mysql InnoDB Buffer Pool Operations', 'operations/s', 'innodb', 'mysql.innodb_buffer_pool_ops', 'area'],
230 ["Innodb_buffer_pool_reads", "disk reads", "incremental"],
231 ["Innodb_buffer_pool_wait_free", "wait free", "incremental", -1, 1]
234 'options': [None, 'mysql QCache Operations', 'queries/s', 'qcache', 'mysql.qcache_ops', 'line'],
236 ["Qcache_hits", "hits", "incremental"],
237 ["Qcache_lowmem_prunes", "lowmem prunes", "incremental", -1, 1],
238 ["Qcache_inserts", "inserts", "incremental"],
239 ["Qcache_not_cached", "not cached", "incremental", -1, 1]
242 'options': [None, 'mysql QCache Queries in Cache', 'queries', 'qcache', 'mysql.qcache', 'line'],
244 ["Qcache_queries_in_cache", "queries", "absolute"]
247 'options': [None, 'mysql QCache Free Memory', 'MB', 'qcache', 'mysql.qcache_freemem', 'area'],
249 ["Qcache_free_memory", "free", "absolute", 1, 1024 * 1024]
251 'qcache_memblocks': {
252 'options': [None, 'mysql QCache Memory Blocks', 'blocks', 'qcache', 'mysql.qcache_memblocks', 'line'],
254 ["Qcache_free_blocks", "free", "absolute"],
255 ["Qcache_total_blocks", "total", "absolute"]
258 'options': [None, 'mysql MyISAM Key Cache Blocks', 'blocks', 'myisam', 'mysql.key_blocks', 'line'],
260 ["Key_blocks_unused", "unused", "absolute"],
261 ["Key_blocks_used", "used", "absolute", -1, 1],
262 ["Key_blocks_not_flushed", "not flushed", "absolute"]
265 'options': [None, 'mysql MyISAM Key Cache Requests', 'requests/s', 'myisam', 'mysql.key_requests', 'area'],
267 ["Key_read_requests", "reads", "incremental"],
268 ["Key_write_requests", "writes", "incremental", -1, 1]
271 'options': [None, 'mysql MyISAM Key Cache Disk Operations', 'operations/s', 'myisam', 'mysql.key_disk_ops', 'area'],
273 ["Key_reads", "reads", "incremental"],
274 ["Key_writes", "writes", "incremental", -1, 1]
277 'options': [None, 'mysql Open Files', 'files', 'files', 'mysql.files', 'line'],
279 ["Open_files", "files", "absolute"]
282 'options': [None, 'mysql Opened Files Rate', 'files/s', 'files', 'mysql.files_rate', 'line'],
284 ["Opened_files", "files", "incremental"]
286 'binlog_stmt_cache': {
287 'options': [None, 'mysql Binlog Statement Cache', 'statements/s', 'binlog', 'mysql.binlog_stmt_cache', 'line'],
289 ["Binlog_stmt_cache_disk_use", "disk", "incremental"],
290 ["Binlog_stmt_cache_use", "all", "incremental"]
292 'connection_errors': {
293 'options': [None, 'mysql Connection Errors', 'connections/s', 'connections', 'mysql.connection_errors', 'line'],
295 ["Connection_errors_accept", "accept", "incremental"],
296 ["Connection_errors_internal", "internal", "incremental"],
297 ["Connection_errors_max_connections", "max", "incremental"],
298 ["Connection_errors_peer_address", "peer_addr", "incremental"],
299 ["Connection_errors_select", "select", "incremental"],
300 ["Connection_errors_tcpwrap", "tcpwrap", "incremental"]
306 class Service(SimpleService):
307 def __init__(self, configuration=None, name=None):
308 SimpleService.__init__(self, configuration=configuration, name=name)
309 self._parse_config(configuration)
311 self.definitions = CHARTS
312 self.connection = None
314 def _parse_config(self, configuration):
316 Parse configuration to collect data from MySQL server
317 :param configuration: dict
321 if self.name is None:
323 if 'user' in configuration:
324 parameters['user'] = self.configuration['user']
325 if 'pass' in configuration:
326 parameters['passwd'] = self.configuration['pass']
327 if 'my.cnf' in configuration:
328 parameters['read_default_file'] = self.configuration['my.cnf']
329 elif 'socket' in configuration:
330 parameters['unix_socket'] = self.configuration['socket']
331 elif 'host' in configuration:
332 parameters['host'] = self.configuration['host']
333 if 'port' in configuration:
334 parameters['port'] = self.configuration['port']
335 self.connection_parameters = parameters
339 Try to connect to MySQL server
342 self.connection = MySQLdb.connect(connect_timeout=self.update_every, **self.connection_parameters)
343 except MySQLdb.OperationalError as e:
344 self.error("Cannot establish connection to MySQL.")
347 except Exception as e:
348 self.error("problem connecting to server:", e)
353 Get raw data from MySQL server
356 if self.connection is None:
362 cursor = self.connection.cursor()
363 cursor.execute(QUERY)
364 raw_data = cursor.fetchall()
365 except MySQLdb.OperationalError as e:
366 self.debug("Reconnecting due to", str(e))
368 cursor = self.connection.cursor()
369 cursor.execute(QUERY)
370 raw_data = cursor.fetchall()
371 except Exception as e:
372 self.error("cannot execute query.", e)
373 self.connection.close()
374 self.connection = None
377 data = dict(raw_data)
379 data["Thread_cache_misses"] = int(data["Threads_created"] * 10000 / float(data["Connections"]))
381 data["Thread_cache_misses"] = 0
387 Check if service is able to connect to server
391 self.connection = self._connect()
394 self.connection = None