]> arthur.barton.de Git - netdata.git/blob - python.d/mysql.chart.py
Merge branch 'master' into ab-debian
[netdata.git] / python.d / mysql.chart.py
1 # -*- coding: utf-8 -*-
2 # Description: MySQL netdata python.d module
3 # Author: Pawel Krupa (paulfantom)
4
5 from base import MySQLService
6
7 # default module values (can be overridden per job in `config`)
8 # update_every = 3
9 priority = 90000
10 retries = 60
11
12 # query executed on MySQL server
13 QUERY_GLOBAL = 'SHOW GLOBAL STATUS;'
14 QUERY_SLAVE = 'SHOW SLAVE STATUS;'
15
16 GLOBAL_STATS = [
17  'Bytes_received',
18  'Bytes_sent',
19  'Queries',
20  'Questions',
21  'Slow_queries',
22  'Handler_commit',
23  'Handler_delete',
24  'Handler_prepare',
25  'Handler_read_first',
26  'Handler_read_key',
27  'Handler_read_next',
28  'Handler_read_prev',
29  'Handler_read_rnd',
30  'Handler_read_rnd_next',
31  'Handler_rollback',
32  'Handler_savepoint',
33  'Handler_savepoint_rollback',
34  'Handler_update',
35  'Handler_write',
36  'Table_locks_immediate',
37  'Table_locks_waited',
38  'Select_full_join',
39  'Select_full_range_join',
40  'Select_range',
41  'Select_range_check',
42  'Select_scan',
43  'Sort_merge_passes',
44  'Sort_range',
45  'Sort_scan',
46  'Created_tmp_disk_tables',
47  'Created_tmp_files',
48  'Created_tmp_tables',
49  'Connections',
50  'Aborted_connects',
51  'Binlog_cache_disk_use',
52  'Binlog_cache_use',
53  'Threads_connected',
54  'Threads_created',
55  'Threads_cached',
56  'Threads_running',
57  'Thread_cache_misses',
58  'Innodb_data_read',
59  'Innodb_data_written',
60  'Innodb_data_reads',
61  'Innodb_data_writes',
62  'Innodb_data_fsyncs',
63  'Innodb_data_pending_reads',
64  'Innodb_data_pending_writes',
65  'Innodb_data_pending_fsyncs',
66  'Innodb_log_waits',
67  'Innodb_log_write_requests',
68  'Innodb_log_writes',
69  'Innodb_os_log_fsyncs',
70  'Innodb_os_log_pending_fsyncs',
71  'Innodb_os_log_pending_writes',
72  'Innodb_os_log_written',
73  'Innodb_row_lock_current_waits',
74  'Innodb_rows_inserted',
75  'Innodb_rows_read',
76  'Innodb_rows_updated',
77  'Innodb_rows_deleted',
78  'Innodb_buffer_pool_pages_data',
79  'Innodb_buffer_pool_pages_dirty',
80  'Innodb_buffer_pool_pages_free',
81  'Innodb_buffer_pool_pages_flushed',
82  'Innodb_buffer_pool_pages_misc',
83  'Innodb_buffer_pool_pages_total',
84  'Innodb_buffer_pool_bytes_data',
85  'Innodb_buffer_pool_bytes_dirty',
86  'Innodb_buffer_pool_read_ahead',
87  'Innodb_buffer_pool_read_ahead_evicted',
88  'Innodb_buffer_pool_read_ahead_rnd',
89  'Innodb_buffer_pool_read_requests',
90  'Innodb_buffer_pool_write_requests',
91  'Innodb_buffer_pool_reads',
92  'Innodb_buffer_pool_wait_free',
93  'Qcache_hits',
94  'Qcache_lowmem_prunes',
95  'Qcache_inserts',
96  'Qcache_not_cached',
97  'Qcache_queries_in_cache',
98  'Qcache_free_memory',
99  'Qcache_free_blocks',
100  'Qcache_total_blocks',
101  'Key_blocks_unused',
102  'Key_blocks_used',
103  'Key_blocks_not_flushed',
104  'Key_read_requests',
105  'Key_write_requests',
106  'Key_reads',
107  'Key_writes',
108  'Open_files',
109  'Opened_files',
110  'Binlog_stmt_cache_disk_use',
111  'Binlog_stmt_cache_use',
112  'Connection_errors_accept',
113  'Connection_errors_internal',
114  'Connection_errors_max_connections',
115  'Connection_errors_peer_address',
116  'Connection_errors_select',
117  'Connection_errors_tcpwrap']
118
119 def slave_seconds(value):
120     return value if value else -1
121
122 def slave_running(value):
123     return 1 if value == 'Yes' else -1
124
125
126 SLAVE_STATS = [
127     ('Seconds_Behind_Master', slave_seconds),
128     ('Slave_SQL_Running', slave_running),
129     ('Slave_IO_Running', slave_running)
130 ]
131
132 ORDER = ['net',
133          'queries',
134          'handlers',
135          'table_locks',
136          'join_issues', 'sort_issues',
137          'tmp',
138          'connections', 'connection_errors',
139          'binlog_cache', 'binlog_stmt_cache',
140          'threads', 'thread_cache_misses',
141          'innodb_io', 'innodb_io_ops', 'innodb_io_pending_ops', 'innodb_log', 'innodb_os_log', 'innodb_os_log_io',
142          'innodb_cur_row_lock', 'innodb_rows', 'innodb_buffer_pool_pages', 'innodb_buffer_pool_bytes',
143          'innodb_buffer_pool_read_ahead', 'innodb_buffer_pool_reqs', 'innodb_buffer_pool_ops',
144          'qcache_ops', 'qcache', 'qcache_freemem', 'qcache_memblocks',
145          'key_blocks', 'key_requests', 'key_disk_ops',
146          'files', 'files_rate', 'slave_behind', 'slave_status']
147
148 CHARTS = {
149     'net': {
150         'options': [None, 'mysql Bandwidth', 'kilobits/s', 'bandwidth', 'mysql.net', 'area'],
151         'lines': [
152             ['Bytes_received', 'in', 'incremental', 8, 1024],
153             ['Bytes_sent', 'out', 'incremental', -8, 1024]
154         ]},
155     'queries': {
156         'options': [None, 'mysql Queries', 'queries/s', 'queries', 'mysql.queries', 'line'],
157         'lines': [
158             ['Queries', 'queries', 'incremental'],
159             ['Questions', 'questions', 'incremental'],
160             ['Slow_queries', 'slow_queries', 'incremental']
161         ]},
162     'handlers': {
163         'options': [None, 'mysql Handlers', 'handlers/s', 'handlers', 'mysql.handlers', 'line'],
164         'lines': [
165             ['Handler_commit', 'commit', 'incremental'],
166             ['Handler_delete', 'delete', 'incremental'],
167             ['Handler_prepare', 'prepare', 'incremental'],
168             ['Handler_read_first', 'read_first', 'incremental'],
169             ['Handler_read_key', 'read_key', 'incremental'],
170             ['Handler_read_next', 'read_next', 'incremental'],
171             ['Handler_read_prev', 'read_prev', 'incremental'],
172             ['Handler_read_rnd', 'read_rnd', 'incremental'],
173             ['Handler_read_rnd_next', 'read_rnd_next', 'incremental'],
174             ['Handler_rollback', 'rollback', 'incremental'],
175             ['Handler_savepoint', 'savepoint', 'incremental'],
176             ['Handler_savepoint_rollback', 'savepoint_rollback', 'incremental'],
177             ['Handler_update', 'update', 'incremental'],
178             ['Handler_write', 'write', 'incremental']
179         ]},
180     'table_locks': {
181         'options': [None, 'mysql Tables Locks', 'locks/s', 'locks', 'mysql.table_locks', 'line'],
182         'lines': [
183             ['Table_locks_immediate', 'immediate', 'incremental'],
184             ['Table_locks_waited', 'waited', 'incremental', -1, 1]
185         ]},
186     'join_issues': {
187         'options': [None, 'mysql Select Join Issues', 'joins/s', 'issues', 'mysql.join_issues', 'line'],
188         'lines': [
189             ['Select_full_join', 'full_join', 'incremental'],
190             ['Select_full_range_join', 'full_range_join', 'incremental'],
191             ['Select_range', 'range', 'incremental'],
192             ['Select_range_check', 'range_check', 'incremental'],
193             ['Select_scan', 'scan', 'incremental']
194         ]},
195     'sort_issues': {
196         'options': [None, 'mysql Sort Issues', 'issues/s', 'issues', 'mysql.sort_issues', 'line'],
197         'lines': [
198             ['Sort_merge_passes', 'merge_passes', 'incremental'],
199             ['Sort_range', 'range', 'incremental'],
200             ['Sort_scan', 'scan', 'incremental']
201         ]},
202     'tmp': {
203         'options': [None, 'mysql Tmp Operations', 'counter', 'temporaries', 'mysql.tmp', 'line'],
204         'lines': [
205             ['Created_tmp_disk_tables', 'disk_tables', 'incremental'],
206             ['Created_tmp_files', 'files', 'incremental'],
207             ['Created_tmp_tables', 'tables', 'incremental']
208         ]},
209     'connections': {
210         'options': [None, 'mysql Connections', 'connections/s', 'connections', 'mysql.connections', 'line'],
211         'lines': [
212             ['Connections', 'all', 'incremental'],
213             ['Aborted_connects', 'aborted', 'incremental']
214         ]},
215     'binlog_cache': {
216         'options': [None, 'mysql Binlog Cache', 'transactions/s', 'binlog', 'mysql.binlog_cache', 'line'],
217         'lines': [
218             ['Binlog_cache_disk_use', 'disk', 'incremental'],
219             ['Binlog_cache_use', 'all', 'incremental']
220         ]},
221     'threads': {
222         'options': [None, 'mysql Threads', 'threads', 'threads', 'mysql.threads', 'line'],
223         'lines': [
224             ['Threads_connected', 'connected', 'absolute'],
225             ['Threads_created', 'created', 'incremental'],
226             ['Threads_cached', 'cached', 'absolute', -1, 1],
227             ['Threads_running', 'running', 'absolute'],
228         ]},
229     'thread_cache_misses': {
230         'options': [None, 'mysql Threads Cache Misses', 'misses', 'threads', 'mysql.thread_cache_misses', 'area'],
231         'lines': [
232             ['Thread_cache_misses', 'misses', 'absolute', 1, 100]
233         ]},
234     'innodb_io': {
235         'options': [None, 'mysql InnoDB I/O Bandwidth', 'kilobytes/s', 'innodb', 'mysql.innodb_io', 'area'],
236         'lines': [
237             ['Innodb_data_read', 'read', 'incremental', 1, 1024],
238             ['Innodb_data_written', 'write', 'incremental', -1, 1024]
239         ]},
240     'innodb_io_ops': {
241         'options': [None, 'mysql InnoDB I/O Operations', 'operations/s', 'innodb', 'mysql.innodb_io_ops', 'line'],
242         'lines': [
243             ['Innodb_data_reads', 'reads', 'incremental'],
244             ['Innodb_data_writes', 'writes', 'incremental', -1, 1],
245             ['Innodb_data_fsyncs', 'fsyncs', 'incremental']
246         ]},
247     'innodb_io_pending_ops': {
248         'options': [None, 'mysql InnoDB Pending I/O Operations', 'operations', 'innodb', 'mysql.innodb_io_pending_ops', 'line'],
249         'lines': [
250             ['Innodb_data_pending_reads', 'reads', 'absolute'],
251             ['Innodb_data_pending_writes', 'writes', 'absolute', -1, 1],
252             ['Innodb_data_pending_fsyncs', 'fsyncs', 'absolute']
253         ]},
254     'innodb_log': {
255         'options': [None, 'mysql InnoDB Log Operations', 'operations/s', 'innodb', 'mysql.innodb_log', 'line'],
256         'lines': [
257             ['Innodb_log_waits', 'waits', 'incremental'],
258             ['Innodb_log_write_requests', 'write_requests', 'incremental', -1, 1],
259             ['Innodb_log_writes', 'writes', 'incremental', -1, 1],
260         ]},
261     'innodb_os_log': {
262         'options': [None, 'mysql InnoDB OS Log Operations', 'operations', 'innodb', 'mysql.innodb_os_log', 'line'],
263         'lines': [
264             ['Innodb_os_log_fsyncs', 'fsyncs', 'incremental'],
265             ['Innodb_os_log_pending_fsyncs', 'pending_fsyncs', 'absolute'],
266             ['Innodb_os_log_pending_writes', 'pending_writes', 'absolute', -1, 1],
267         ]},
268     'innodb_os_log_io': {
269         'options': [None, 'mysql InnoDB OS Log Bandwidth', 'kilobytes/s', 'innodb', 'mysql.innodb_os_log_io', 'area'],
270         'lines': [
271             ['Innodb_os_log_written', 'write', 'incremental', -1, 1024],
272         ]},
273     'innodb_cur_row_lock': {
274         'options': [None, 'mysql InnoDB Current Row Locks', 'operations', 'innodb', 'mysql.innodb_cur_row_lock', 'area'],
275         'lines': [
276             ['Innodb_row_lock_current_waits', 'current_waits', 'absolute']
277         ]},
278     'innodb_rows': {
279         'options': [None, 'mysql InnoDB Row Operations', 'operations/s', 'innodb', 'mysql.innodb_rows', 'area'],
280         'lines': [
281             ['Innodb_rows_inserted', 'read', 'incremental'],
282             ['Innodb_rows_read', 'deleted', 'incremental', -1, 1],
283             ['Innodb_rows_updated', 'inserted', 'incremental', 1, 1],
284             ['Innodb_rows_deleted', 'updated', 'incremental', -1, 1],
285         ]},
286     'innodb_buffer_pool_pages': {
287         'options': [None, 'mysql InnoDB Buffer Pool Pages', 'pages', 'innodb', 'mysql.innodb_buffer_pool_pages', 'line'],
288         'lines': [
289             ['Innodb_buffer_pool_pages_data', 'data', 'absolute'],
290             ['Innodb_buffer_pool_pages_dirty', 'dirty', 'absolute', -1, 1],
291             ['Innodb_buffer_pool_pages_free', 'free', 'absolute'],
292             ['Innodb_buffer_pool_pages_flushed', 'flushed', 'incremental', -1, 1],
293             ['Innodb_buffer_pool_pages_misc', 'misc', 'absolute', -1, 1],
294             ['Innodb_buffer_pool_pages_total', 'total', 'absolute']
295         ]},
296     'innodb_buffer_pool_bytes': {
297         'options': [None, 'mysql InnoDB Buffer Pool Bytes', 'MB', 'innodb', 'mysql.innodb_buffer_pool_bytes', 'area'],
298         'lines': [
299             ['Innodb_buffer_pool_bytes_data', 'data', 'absolute', 1, 1024 * 1024],
300             ['Innodb_buffer_pool_bytes_dirty', 'dirty', 'absolute', -1, 1024 * 1024]
301         ]},
302     'innodb_buffer_pool_read_ahead': {
303         'options': [None, 'mysql InnoDB Buffer Pool Read Ahead', 'operations/s', 'innodb', 'mysql.innodb_buffer_pool_read_ahead', 'area'],
304         'lines': [
305             ['Innodb_buffer_pool_read_ahead', 'all', 'incremental'],
306             ['Innodb_buffer_pool_read_ahead_evicted', 'evicted', 'incremental', -1, 1],
307             ['Innodb_buffer_pool_read_ahead_rnd', 'random', 'incremental']
308         ]},
309     'innodb_buffer_pool_reqs': {
310         'options': [None, 'mysql InnoDB Buffer Pool Requests', 'requests/s', 'innodb', 'mysql.innodb_buffer_pool_reqs', 'area'],
311         'lines': [
312             ['Innodb_buffer_pool_read_requests', 'reads', 'incremental'],
313             ['Innodb_buffer_pool_write_requests', 'writes', 'incremental', -1, 1]
314         ]},
315     'innodb_buffer_pool_ops': {
316         'options': [None, 'mysql InnoDB Buffer Pool Operations', 'operations/s', 'innodb', 'mysql.innodb_buffer_pool_ops', 'area'],
317         'lines': [
318             ['Innodb_buffer_pool_reads', 'disk reads', 'incremental'],
319             ['Innodb_buffer_pool_wait_free', 'wait free', 'incremental', -1, 1]
320         ]},
321     'qcache_ops': {
322         'options': [None, 'mysql QCache Operations', 'queries/s', 'qcache', 'mysql.qcache_ops', 'line'],
323         'lines': [
324             ['Qcache_hits', 'hits', 'incremental'],
325             ['Qcache_lowmem_prunes', 'lowmem prunes', 'incremental', -1, 1],
326             ['Qcache_inserts', 'inserts', 'incremental'],
327             ['Qcache_not_cached', 'not cached', 'incremental', -1, 1]
328         ]},
329     'qcache': {
330         'options': [None, 'mysql QCache Queries in Cache', 'queries', 'qcache', 'mysql.qcache', 'line'],
331         'lines': [
332             ['Qcache_queries_in_cache', 'queries', 'absolute']
333         ]},
334     'qcache_freemem': {
335         'options': [None, 'mysql QCache Free Memory', 'MB', 'qcache', 'mysql.qcache_freemem', 'area'],
336         'lines': [
337             ['Qcache_free_memory', 'free', 'absolute', 1, 1024 * 1024]
338         ]},
339     'qcache_memblocks': {
340         'options': [None, 'mysql QCache Memory Blocks', 'blocks', 'qcache', 'mysql.qcache_memblocks', 'line'],
341         'lines': [
342             ['Qcache_free_blocks', 'free', 'absolute'],
343             ['Qcache_total_blocks', 'total', 'absolute']
344         ]},
345     'key_blocks': {
346         'options': [None, 'mysql MyISAM Key Cache Blocks', 'blocks', 'myisam', 'mysql.key_blocks', 'line'],
347         'lines': [
348             ['Key_blocks_unused', 'unused', 'absolute'],
349             ['Key_blocks_used', 'used', 'absolute', -1, 1],
350             ['Key_blocks_not_flushed', 'not flushed', 'absolute']
351         ]},
352     'key_requests': {
353         'options': [None, 'mysql MyISAM Key Cache Requests', 'requests/s', 'myisam', 'mysql.key_requests', 'area'],
354         'lines': [
355             ['Key_read_requests', 'reads', 'incremental'],
356             ['Key_write_requests', 'writes', 'incremental', -1, 1]
357         ]},
358     'key_disk_ops': {
359         'options': [None, 'mysql MyISAM Key Cache Disk Operations', 'operations/s', 'myisam', 'mysql.key_disk_ops', 'area'],
360         'lines': [
361             ['Key_reads', 'reads', 'incremental'],
362             ['Key_writes', 'writes', 'incremental', -1, 1]
363         ]},
364     'files': {
365         'options': [None, 'mysql Open Files', 'files', 'files', 'mysql.files', 'line'],
366         'lines': [
367             ['Open_files', 'files', 'absolute']
368         ]},
369     'files_rate': {
370         'options': [None, 'mysql Opened Files Rate', 'files/s', 'files', 'mysql.files_rate', 'line'],
371         'lines': [
372             ['Opened_files', 'files', 'incremental']
373         ]},
374     'binlog_stmt_cache': {
375         'options': [None, 'mysql Binlog Statement Cache', 'statements/s', 'binlog', 'mysql.binlog_stmt_cache', 'line'],
376         'lines': [
377             ['Binlog_stmt_cache_disk_use', 'disk', 'incremental'],
378             ['Binlog_stmt_cache_use', 'all', 'incremental']
379         ]},
380     'connection_errors': {
381         'options': [None, 'mysql Connection Errors', 'connections/s', 'connections', 'mysql.connection_errors', 'line'],
382         'lines': [
383             ['Connection_errors_accept', 'accept', 'incremental'],
384             ['Connection_errors_internal', 'internal', 'incremental'],
385             ['Connection_errors_max_connections', 'max', 'incremental'],
386             ['Connection_errors_peer_address', 'peer_addr', 'incremental'],
387             ['Connection_errors_select', 'select', 'incremental'],
388             ['Connection_errors_tcpwrap', 'tcpwrap', 'incremental']
389         ]},
390     'slave_behind': {
391         'options': [None, 'Slave Behind Seconds', 'seconds', 'slave', 'mysql.slave_behind', 'line'],
392         'lines': [
393             ['Seconds_Behind_Master', 'seconds', 'absolute']
394         ]},
395     'slave_status': {
396         'options': [None, 'Slave Status', 'status', 'slave', 'mysql.slave_status', 'line'],
397         'lines': [
398             ['Slave_SQL_Running', 'sql_running', 'absolute'],
399             ['Slave_IO_Running', 'io_running', 'absolute']
400         ]}
401 }
402
403
404 class Service(MySQLService):
405     def __init__(self, configuration=None, name=None):
406         MySQLService.__init__(self, configuration=configuration, name=name)
407         self.order = ORDER
408         self.definitions = CHARTS
409         self.queries = dict(global_status=QUERY_GLOBAL, slave_status=QUERY_SLAVE)
410
411     def _get_data(self):
412
413         raw_data = self._get_raw_data(description=True)
414
415         if not raw_data:
416            return None
417
418         to_netdata = dict()
419
420         if 'global_status' in raw_data:
421             global_status = dict(raw_data['global_status'][0])
422             for key in GLOBAL_STATS:
423                 if key in global_status:
424                     to_netdata[key] = global_status[key]
425             if 'Threads_created' in to_netdata and 'Connections' in to_netdata:
426                 to_netdata['Thread_cache_misses'] = round(int(to_netdata['Threads_created']) / float(to_netdata['Connections']) * 10000)
427
428         if 'slave_status' in raw_data:
429             if raw_data['slave_status'][0]:
430                 slave_raw_data = dict(zip([e[0] for e in raw_data['slave_status'][1]], raw_data['slave_status'][0][0]))
431                 for key, function in SLAVE_STATS:
432                     if key in slave_raw_data:
433                        to_netdata[key] = function(slave_raw_data[key])
434             else:
435                 self.queries.pop('slave_status')
436
437         return to_netdata or None
438