mysql_plugin: rewritten to use MySQLService class
Ilya committed
Mar 9, 2017 at 15:41 UTC
1dc3d7fedb9548b9a3c465d4a0707a59f80a35d1
1 file changed
+244
-300
python.d/mysql.chart.py
+244
-300
@@ -2,45 +2,132 @@
2
# Description: MySQL netdata python.d module
3
# Author: Pawel Krupa (paulfantom)
4
5
-from base import SimpleService
6
-import msg
7
-
8
-# import 3rd party library to handle MySQL communication
9
-try:
10
- import MySQLdb
11
-
12
- # https://github.com/PyMySQL/mysqlclient-python
13
- msg.info("using MySQLdb")
14
-except ImportError:
15
- try:
16
- import pymysql as MySQLdb
17
-
18
- # https://github.com/PyMySQL/PyMySQL
19
- msg.info("using pymysql")
20
- except ImportError:
21
- msg.error("MySQLdb or PyMySQL module is needed to use mysql.chart.py plugin")
22
- raise ImportError
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
29
-# default configuration (overridden by python.d.plugin)
30
-# config = {
31
-# 'local': {
32
-# 'user': 'root',
33
-# 'pass': '',
34
-# 'socket': '/var/run/mysqld/mysqld.sock',
35
-# 'update_every': update_every,
36
-# 'retries': retries,
37
-# 'priority': priority
38
-# }
39
-#}
40
-
12
# query executed on MySQL server
42
-QUERY = "SHOW GLOBAL STATUS;"
43
-QUERY_SLAVE = "SHOW SLAVE STATUS;"
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',
@@ -62,431 +149,288 @@ CHARTS = {
149
'net': {
150
'options': [None, 'mysql Bandwidth', 'kilobits/s', 'bandwidth', 'mysql.net', 'area'],
151
'lines': [
65
- ["Bytes_received", "in", "incremental", 8, 1024],
66
- ["Bytes_sent", "out", "incremental", -8, 1024]
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': [
71
- ["Queries", "queries", "incremental"],
72
- ["Questions", "questions", "incremental"],
73
- ["Slow_queries", "slow_queries", "incremental"]
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': [
78
- ["Handler_commit", "commit", "incremental"],
79
- ["Handler_delete", "delete", "incremental"],
80
- ["Handler_prepare", "prepare", "incremental"],
81
- ["Handler_read_first", "read_first", "incremental"],
82
- ["Handler_read_key", "read_key", "incremental"],
83
- ["Handler_read_next", "read_next", "incremental"],
84
- ["Handler_read_prev", "read_prev", "incremental"],
85
- ["Handler_read_rnd", "read_rnd", "incremental"],
86
- ["Handler_read_rnd_next", "read_rnd_next", "incremental"],
87
- ["Handler_rollback", "rollback", "incremental"],
88
- ["Handler_savepoint", "savepoint", "incremental"],
89
- ["Handler_savepoint_rollback", "savepoint_rollback", "incremental"],
90
- ["Handler_update", "update", "incremental"],
91
- ["Handler_write", "write", "incremental"]
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': [
96
- ["Table_locks_immediate", "immediate", "incremental"],
97
- ["Table_locks_waited", "waited", "incremental", -1, 1]
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': [
102
- ["Select_full_join", "full_join", "incremental"],
103
- ["Select_full_range_join", "full_range_join", "incremental"],
104
- ["Select_range", "range", "incremental"],
105
- ["Select_range_check", "range_check", "incremental"],
106
- ["Select_scan", "scan", "incremental"]
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': [
111
- ["Sort_merge_passes", "merge_passes", "incremental"],
112
- ["Sort_range", "range", "incremental"],
113
- ["Sort_scan", "scan", "incremental"]
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': [
118
- ["Created_tmp_disk_tables", "disk_tables", "incremental"],
119
- ["Created_tmp_files", "files", "incremental"],
120
- ["Created_tmp_tables", "tables", "incremental"]
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': [
125
- ["Connections", "all", "incremental"],
126
- ["Aborted_connects", "aborted", "incremental"]
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': [
131
- ["Binlog_cache_disk_use", "disk", "incremental"],
132
- ["Binlog_cache_use", "all", "incremental"]
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': [
137
- ["Threads_connected", "connected", "absolute"],
138
- ["Threads_created", "created", "incremental"],
139
- ["Threads_cached", "cached", "absolute", -1, 1],
140
- ["Threads_running", "running", "absolute"],
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': [
145
- ["Thread_cache_misses", "misses", "absolute", 1, 100]
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': [
150
- ["Innodb_data_read", "read", "incremental", 1, 1024],
151
- ["Innodb_data_written", "write", "incremental", -1, 1024]
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': [
156
- ["Innodb_data_reads", "reads", "incremental"],
157
- ["Innodb_data_writes", "writes", "incremental", -1, 1],
158
- ["Innodb_data_fsyncs", "fsyncs", "incremental"]
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': [
163
- ["Innodb_data_pending_reads", "reads", "absolute"],
164
- ["Innodb_data_pending_writes", "writes", "absolute", -1, 1],
165
- ["Innodb_data_pending_fsyncs", "fsyncs", "absolute"]
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': [
170
- ["Innodb_log_waits", "waits", "incremental"],
171
- ["Innodb_log_write_requests", "write_requests", "incremental", -1, 1],
172
- ["Innodb_log_writes", "writes", "incremental", -1, 1],
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': [
177
- ["Innodb_os_log_fsyncs", "fsyncs", "incremental"],
178
- ["Innodb_os_log_pending_fsyncs", "pending_fsyncs", "absolute"],
179
- ["Innodb_os_log_pending_writes", "pending_writes", "absolute", -1, 1],
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': [
184
- ["Innodb_os_log_written", "write", "incremental", -1, 1024],
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': [
189
- ["Innodb_row_lock_current_waits", "current_waits", "absolute"]
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': [
194
- ["Innodb_rows_inserted", "read", "incremental"],
195
- ["Innodb_rows_read", "deleted", "incremental", -1, 1],
196
- ["Innodb_rows_updated", "inserted", "incremental", 1, 1],
197
- ["Innodb_rows_deleted", "updated", "incremental", -1, 1],
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': [
202
- ["Innodb_buffer_pool_pages_data", "data", "absolute"],
203
- ["Innodb_buffer_pool_pages_dirty", "dirty", "absolute", -1, 1],
204
- ["Innodb_buffer_pool_pages_free", "free", "absolute"],
205
- ["Innodb_buffer_pool_pages_flushed", "flushed", "incremental", -1, 1],
206
- ["Innodb_buffer_pool_pages_misc", "misc", "absolute", -1, 1],
207
- ["Innodb_buffer_pool_pages_total", "total", "absolute"]
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': [
212
- ["Innodb_buffer_pool_bytes_data", "data", "absolute", 1, 1024 * 1024],
213
- ["Innodb_buffer_pool_bytes_dirty", "dirty", "absolute", -1, 1024 * 1024]
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': [
218
- ["Innodb_buffer_pool_read_ahead", "all", "incremental"],
219
- ["Innodb_buffer_pool_read_ahead_evicted", "evicted", "incremental", -1, 1],
220
- ["Innodb_buffer_pool_read_ahead_rnd", "random", "incremental"]
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': [
225
- ["Innodb_buffer_pool_read_requests", "reads", "incremental"],
226
- ["Innodb_buffer_pool_write_requests", "writes", "incremental", -1, 1]
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': [
231
- ["Innodb_buffer_pool_reads", "disk reads", "incremental"],
232
- ["Innodb_buffer_pool_wait_free", "wait free", "incremental", -1, 1]
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': [
237
- ["Qcache_hits", "hits", "incremental"],
238
- ["Qcache_lowmem_prunes", "lowmem prunes", "incremental", -1, 1],
239
- ["Qcache_inserts", "inserts", "incremental"],
240
- ["Qcache_not_cached", "not cached", "incremental", -1, 1]
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': [
245
- ["Qcache_queries_in_cache", "queries", "absolute"]
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': [
250
- ["Qcache_free_memory", "free", "absolute", 1, 1024 * 1024]
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': [
255
- ["Qcache_free_blocks", "free", "absolute"],
256
- ["Qcache_total_blocks", "total", "absolute"]
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': [
261
- ["Key_blocks_unused", "unused", "absolute"],
262
- ["Key_blocks_used", "used", "absolute", -1, 1],
263
- ["Key_blocks_not_flushed", "not flushed", "absolute"]
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': [
268
- ["Key_read_requests", "reads", "incremental"],
269
- ["Key_write_requests", "writes", "incremental", -1, 1]
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': [
274
- ["Key_reads", "reads", "incremental"],
275
- ["Key_writes", "writes", "incremental", -1, 1]
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': [
280
- ["Open_files", "files", "absolute"]
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': [
285
- ["Opened_files", "files", "incremental"]
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': [
290
- ["Binlog_stmt_cache_disk_use", "disk", "incremental"],
291
- ["Binlog_stmt_cache_use", "all", "incremental"]
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': [
296
- ["Connection_errors_accept", "accept", "incremental"],
297
- ["Connection_errors_internal", "internal", "incremental"],
298
- ["Connection_errors_max_connections", "max", "incremental"],
299
- ["Connection_errors_peer_address", "peer_addr", "incremental"],
300
- ["Connection_errors_select", "select", "incremental"],
301
- ["Connection_errors_tcpwrap", "tcpwrap", "incremental"]
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': [
306
- ["slave_behind", "seconds", "absolute"]
393
+ ['Seconds_Behind_Master', 'seconds', 'absolute']
394
]},
395
'slave_status': {
396
'options': [None, 'Slave Status', 'status', 'slave', 'mysql.slave_status', 'line'],
397
'lines': [
311
- ["slave_sql", "sql_running", "absolute"],
312
- ["slave_io", "io_running", "absolute"]
398
+ ['Slave_SQL_Running', 'sql_running', 'absolute'],
399
+ ['Slave_IO_Running', 'io_running', 'absolute']
400
]}
401
}
402
403
317
-class Service(SimpleService):
404
+class Service(MySQLService):
405
def __init__(self, configuration=None, name=None):
319
- SimpleService.__init__(self, configuration=configuration, name=name)
320
- self._parse_config(configuration)
406
+ MySQLService.__init__(self, configuration=configuration, name=name)
407
self.order = ORDER
408
self.definitions = CHARTS
323
- self.connection = None
324
- self.do_slave = -1
325
-
326
- def _parse_config(self, configuration):
327
- """
328
- Parse configuration to collect data from MySQL server
329
- :param configuration: dict
330
- :return: dict
331
- """
332
- parameters = {}
333
- if self.name is None:
334
- self.name = 'local'
335
- if 'user' in configuration:
336
- parameters['user'] = self.configuration['user']
337
- if 'pass' in configuration:
338
- parameters['passwd'] = self.configuration['pass']
339
- if 'my.cnf' in configuration:
340
- parameters['read_default_file'] = self.configuration['my.cnf']
341
- elif 'socket' in configuration:
342
- parameters['unix_socket'] = self.configuration['socket']
343
- elif 'host' in configuration:
344
- parameters['host'] = self.configuration['host']
345
- if 'port' in configuration:
346
- parameters['port'] = int(self.configuration['port'])
347
- self.connection_parameters = parameters
348
-
349
- def _connect(self):
350
- """
351
- Try to connect to MySQL server
352
- """
353
- try:
354
- self.connection = MySQLdb.connect(connect_timeout=self.update_every, **self.connection_parameters)
355
- except MySQLdb.OperationalError as e:
356
- self.error("Cannot establish connection to MySQL.")
357
- self.debug(str(e))
358
- raise RuntimeError
359
- except Exception as e:
360
- self.error("problem connecting to server:", e)
361
- raise RuntimeError
362
-
363
- def _get_data_slave(self):
364
- """
365
- Get slave raw data from MySQL server
366
- :return: dict
367
- """
368
- if self.connection is None:
369
- try:
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()
378
- if cursor.execute(QUERY_SLAVE):
379
- slave_raw_data = dict(list(zip([elem[0] for elem in cursor.description], cursor.fetchone())))
380
-
381
- except MySQLdb.OperationalError as e:
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 = {
402
- 'slave_behind': None,
403
- 'slave_sql': None,
404
- 'slave_io': None
405
- }
406
-
407
- try:
408
- slave_data['slave_behind'] = int(slave_raw_data.setdefault('Seconds_Behind_Master', -1))
409
- except:
410
- slave_data['slave_behind'] = None
411
-
412
- try:
413
- slave_data['slave_sql'] = 1 if slave_raw_data.get('Slave_SQL_Running') == 'Yes' else -1
414
- except:
415
- slave_data['slave_sql'] = None
416
-
417
- try:
418
- slave_data['slave_io'] = 1 if slave_raw_data.get('Slave_IO_Running') == 'Yes' else -1
419
- except:
420
- slave_data['slave_io'] = None
421
-
422
- return slave_data
409
+ self.queries = dict(global_status=QUERY_GLOBAL, slave_status=QUERY_SLAVE)
410
411
def _get_data(self):
425
- """
426
- Get raw data from MySQL server
427
- :return: dict
428
- """
429
- if self.connection is None:
430
- try:
431
- self._connect()
432
- except RuntimeError:
433
- return None
434
- try:
435
- cursor = self.connection.cursor()
436
- cursor.execute(QUERY)
437
- raw_data = cursor.fetchall()
412
439
- except MySQLdb.OperationalError as e:
440
- self.debug("Reconnecting for query", QUERY, ":", str(e))
441
- try:
442
- self._connect()
443
- cursor = self.connection.cursor()
444
- cursor.execute(QUERY)
445
- raw_data = cursor.fetchall()
446
- except Exception as e:
447
- self.error("retried, but cannot execute query", QUERY, ":", str(e))
448
- self.connection.close()
449
- self.connection = None
450
- return None
413
+ raw_data = self._get_raw_data(description=True)
414
+
415
+ if not raw_data:
416
+ return None
417
452
- except Exception as e:
453
- self.error("cannot execute query", QUERY, ":", str(e))
454
- self.connection.close()
455
- self.connection = None
456
- return None
418
+ to_netdata = dict()
419
458
- data = dict(raw_data)
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
460
- # check for slave data
461
- # the first time is -1 (so we do it)
462
- # then it is set to 1 or 0 and we keep it like that
463
- if self.do_slave != 0:
464
- slave_data = self._get_data_slave()
465
- if slave_data is not None:
466
- data.update(slave_data)
467
- if self.do_slave == -1:
468
- self.do_slave = 1
469
- else:
470
- if self.do_slave == -1:
471
- self.error("replication metrics will be disabled - please allow netdata to collect them.")
472
- self.do_slave = 0
428
+ if 'slave_status' in raw_data and raw_data['slave_status'][0]:
429
+ slave_raw_data = dict(zip([e[0] for e in raw_data['slave_status'][1]], raw_data['slave_status'][0][0]))
430
474
- # do calculations
475
- try:
476
- data["Thread_cache_misses"] = round(float(data["Threads_created"]) / float(data["Connections"]) * 10000)
477
- except:
478
- data["Thread_cache_misses"] = None
431
+ for key, function in SLAVE_STATS:
432
+ if key in slave_raw_data:
433
+ to_netdata[key] = function(slave_raw_data[key])
434
480
- return data
435
+ return to_netdata or None
436
482
- def check(self):
483
- """
484
- Check if service is able to connect to server
485
- :return: boolean
486
- """
487
- try:
488
- self.connection = self._connect()
489
- return True
490
- except RuntimeError:
491
- self.connection = None
492
- return False