X-Git-Url: https://arthur.barton.de/gitweb/?a=blobdiff_plain;f=python.d%2Fmysql.chart.py;h=abf6bf7153fecf5f755574842620e166397a9907;hb=8679670bdbe3c5928ec2e266d9c72e1a758fdf37;hp=c68cf4c16a6c781eb1de49fe7031c48c4b516053;hpb=dd1b39cafcdbfafd1b720fca7a5b01bf764047cc;p=netdata.git diff --git a/python.d/mysql.chart.py b/python.d/mysql.chart.py index c68cf4c1..abf6bf71 100644 --- a/python.d/mysql.chart.py +++ b/python.d/mysql.chart.py @@ -1,484 +1,438 @@ -# Description: MySQL netdata python.d plugin +# -*- coding: utf-8 -*- +# Description: MySQL netdata python.d module # Author: Pawel Krupa (paulfantom) -import os -import sys +from base import MySQLService -NAME = os.path.basename(__file__).replace(".chart.py", "") +# default module values (can be overridden per job in `config`) +# update_every = 3 +priority = 90000 +retries = 60 -# import 3rd party library to handle MySQL communication -try: - import MySQLdb +# query executed on MySQL server +QUERY_GLOBAL = 'SHOW GLOBAL STATUS;' +QUERY_SLAVE = 'SHOW SLAVE STATUS;' - # https://github.com/PyMySQL/mysqlclient-python - sys.stderr.write(NAME + ": using MySQLdb\n") -except ImportError: - try: - import pymysql as MySQLdb +GLOBAL_STATS = [ + 'Bytes_received', + 'Bytes_sent', + 'Queries', + 'Questions', + 'Slow_queries', + 'Handler_commit', + 'Handler_delete', + 'Handler_prepare', + 'Handler_read_first', + 'Handler_read_key', + 'Handler_read_next', + 'Handler_read_prev', + 'Handler_read_rnd', + 'Handler_read_rnd_next', + 'Handler_rollback', + 'Handler_savepoint', + 'Handler_savepoint_rollback', + 'Handler_update', + 'Handler_write', + 'Table_locks_immediate', + 'Table_locks_waited', + 'Select_full_join', + 'Select_full_range_join', + 'Select_range', + 'Select_range_check', + 'Select_scan', + 'Sort_merge_passes', + 'Sort_range', + 'Sort_scan', + 'Created_tmp_disk_tables', + 'Created_tmp_files', + 'Created_tmp_tables', + 'Connections', + 'Aborted_connects', + 'Binlog_cache_disk_use', + 'Binlog_cache_use', + 'Threads_connected', + 'Threads_created', + 'Threads_cached', + 'Threads_running', + 'Thread_cache_misses', + 'Innodb_data_read', + 'Innodb_data_written', + 'Innodb_data_reads', + 'Innodb_data_writes', + 'Innodb_data_fsyncs', + 'Innodb_data_pending_reads', + 'Innodb_data_pending_writes', + 'Innodb_data_pending_fsyncs', + 'Innodb_log_waits', + 'Innodb_log_write_requests', + 'Innodb_log_writes', + 'Innodb_os_log_fsyncs', + 'Innodb_os_log_pending_fsyncs', + 'Innodb_os_log_pending_writes', + 'Innodb_os_log_written', + 'Innodb_row_lock_current_waits', + 'Innodb_rows_inserted', + 'Innodb_rows_read', + 'Innodb_rows_updated', + 'Innodb_rows_deleted', + 'Innodb_buffer_pool_pages_data', + 'Innodb_buffer_pool_pages_dirty', + 'Innodb_buffer_pool_pages_free', + 'Innodb_buffer_pool_pages_flushed', + 'Innodb_buffer_pool_pages_misc', + 'Innodb_buffer_pool_pages_total', + 'Innodb_buffer_pool_bytes_data', + 'Innodb_buffer_pool_bytes_dirty', + 'Innodb_buffer_pool_read_ahead', + 'Innodb_buffer_pool_read_ahead_evicted', + 'Innodb_buffer_pool_read_ahead_rnd', + 'Innodb_buffer_pool_read_requests', + 'Innodb_buffer_pool_write_requests', + 'Innodb_buffer_pool_reads', + 'Innodb_buffer_pool_wait_free', + 'Qcache_hits', + 'Qcache_lowmem_prunes', + 'Qcache_inserts', + 'Qcache_not_cached', + 'Qcache_queries_in_cache', + 'Qcache_free_memory', + 'Qcache_free_blocks', + 'Qcache_total_blocks', + 'Key_blocks_unused', + 'Key_blocks_used', + 'Key_blocks_not_flushed', + 'Key_read_requests', + 'Key_write_requests', + 'Key_reads', + 'Key_writes', + 'Open_files', + 'Opened_files', + 'Binlog_stmt_cache_disk_use', + 'Binlog_stmt_cache_use', + 'Connection_errors_accept', + 'Connection_errors_internal', + 'Connection_errors_max_connections', + 'Connection_errors_peer_address', + 'Connection_errors_select', + 'Connection_errors_tcpwrap'] - # https://github.com/PyMySQL/PyMySQL - sys.stderr.write(NAME + ": using pymysql\n") - except ImportError: - sys.stderr.write(NAME + ": You need to install MySQLdb or PyMySQL module to use mysql.chart.py plugin\n") - raise ImportError +def slave_seconds(value): + return value if value else -1 -from base import BaseService +def slave_running(value): + return 1 if value == 'Yes' else -1 -# default module values (can be overridden per job in `config`) -update_every = 3 -priority = 90000 -retries = 7 - -# 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 - } -} -# query executed on MySQL server -QUERY = "SHOW GLOBAL STATUS" +SLAVE_STATS = [ + ('Seconds_Behind_Master', slave_seconds), + ('Slave_SQL_Running', slave_running), + ('Slave_IO_Running', slave_running) +] -# charts order (can be overridden if you want less charts, or different order) ORDER = ['net', 'queries', 'handlers', 'table_locks', - 'join_issues', - 'sort_issues', + 'join_issues', 'sort_issues', 'tmp', - 'connections', - 'binlog_cache', - 'threads', - 'thread_cache_misses', - 'innodb_io', - 'innodb_io_ops', - 'innodb_io_pending_ops', - 'innodb_log', - 'innodb_os_log', - 'innodb_os_log_io', - 'innodb_cur_row_lock', - 'innodb_rows', - 'innodb_buffer_pool_pages', - 'innodb_buffer_pool_bytes', - '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', - 'binlog_stmt_cache', - 'connection_errors'] - -# charts definitions in format: -# CHARTS = { -# 'chart_name_in_netdata': ( -# "parameters defining chart (passed to CHART statement)", -# [ # dimensions (lines) definitions -# ("dimension_name", "dimension parameters (passed to DIMENSION statement)") -# ]) -# } + 'connections', 'connection_errors', + 'binlog_cache', 'binlog_stmt_cache', + 'threads', 'thread_cache_misses', + 'innodb_io', 'innodb_io_ops', 'innodb_io_pending_ops', 'innodb_log', 'innodb_os_log', 'innodb_os_log_io', + 'innodb_cur_row_lock', 'innodb_rows', 'innodb_buffer_pool_pages', 'innodb_buffer_pool_bytes', + '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', 'slave_behind', 'slave_status'] CHARTS = { - 'net': ( - "'' 'mysql Bandwidth' 'kilobits/s' bandwidth mysql.net area", - [ - ("Bytes_received", "in incremental 8 1024"), - ("Bytes_sent", "out incremental -8 1024") - ]), - 'queries': ( - "'' 'mysql Queries' 'queries/s' queries mysql.queries line", - [ - ("Queries", "queries incremental 1 1"), - ("Questions", "questions incremental 1 1"), - ("Slow_queries", "slow_queries incremental -1 1") - ]), - 'handlers': ( - "'' 'mysql Handlers' 'handlers/s' handlers mysql.handlers line", - [ - ("Handler_commit", "commit incremental 1 1"), - ("Handler_delete", "delete incremental 1 1"), - ("Handler_prepare", "prepare incremental 1 1"), - ("Handler_read_first", "read_first incremental 1 1"), - ("Handler_read_key", "read_key incremental 1 1"), - ("Handler_read_next", "read_next incremental 1 1"), - ("Handler_read_prev", "read_prev incremental 1 1"), - ("Handler_read_rnd", "read_rnd incremental 1 1"), - ("Handler_read_rnd_next", "read_rnd_next incremental 1 1"), - ("Handler_rollback", "rollback incremental 1 1"), - ("Handler_savepoint", "savepoint incremental 1 1"), - ("Handler_savepoint_rollback", "savepoint_rollback incremental 1 1"), - ("Handler_update", "update incremental 1 1"), - ("Handler_write", "write incremental 1 1") - ]), - 'table_locks': ( - "'' 'mysql Tables Locks' 'locks/s' locks mysql.table_locks line", - [ - ("Table_locks_immediate", "immediate incremental 1 1"), - ("Table_locks_waited", "waited incremental -1 1") - ]), - 'join_issues': ( - "'' 'mysql Select Join Issues' 'joins/s' issues mysql.join_issues line", - [ - ("Select_full_join", "full_join incremental 1 1"), - ("Select_full_range_join", "full_range_join incremental 1 1"), - ("Select_range", "range incremental 1 1"), - ("Select_range_check", "range_check incremental 1 1"), - ("Select_scan", "scan incremental 1 1"), - ]), - 'sort_issues': ( - "'' 'mysql Sort Issues' 'issues/s' issues mysql.sort.issues line", - [ - ("Sort_merge_passes", "merge_passes incremental 1 1"), - ("Sort_range", "range incremental 1 1"), - ("Sort_scan", "scan incremental 1 1"), - ]), - 'tmp': ( - "'' 'mysql Tmp Operations' 'counter' temporaries mysql.tmp line", - [ - ("Created_tmp_disk_tables", "disk_tables incremental 1 1"), - ("Created_tmp_files", "files incremental 1 1"), - ("Created_tmp_tables", "tables incremental 1 1"), - ]), - 'connections': ( - "'' 'mysql Connections' 'connections/s' connections mysql.connections line", - [ - ("Connections", "all incremental 1 1"), - ("Aborted_connects", "aborted incremental 1 1"), - ]), - 'binlog_cache': ( - "'' 'mysql Binlog Cache' 'transactions/s' binlog mysql.binlog_cache line", - [ - ("Binlog_cache_disk_use", "disk incremental 1 1"), - ("Binlog_cache_use", "all incremental 1 1"), - ]), - 'threads': ( - "'' 'mysql Threads' 'threads' threads mysql.threads line", - [ - ("Threads_connected", "connected absolute 1 1"), - ("Threads_created", "created incremental 1 1"), - ("Threads_cached", "cached absolute -1 1"), - ("Threads_running", "running absolute 1 1"), - ]), - 'thread_cache_misses': ( - "'' 'mysql Threads Cache Misses' 'misses' threads mysql.thread_cache_misses area", - [ - ("Thread_cache_misses", "misses misses absolute 1 100"), - ]), - 'innodb_io': ( - "'' 'mysql InnoDB I/O Bandwidth' 'kilobytes/s' innodb mysql.innodb_io area", - [ - ("Innodb_data_read", "read incremental 1 1024"), - ("Innodb_data_written", "write incremental -1 1024"), - ]), - 'innodb_io_ops': ( - "'' 'mysql InnoDB I/O Operations' 'operations/s' innodb mysql.innodb_io_ops line", - [ - ("Innodb_data_reads", "reads incremental 1 1"), - ("Innodb_data_writes", "writes incremental -1 1"), - ("Innodb_data_fsyncs", "fsyncs incremental 1 1"), - ]), - 'innodb_io_pending_ops': ( - "'' 'mysql InnoDB Pending I/O Operations' 'operations' innodb mysql.innodb_io_pending_ops line", - [ - ("Innodb_data_pending_reads", "reads absolute 1 1"), - ("Innodb_data_pending_writes", "writes absolute -1 1"), - ("Innodb_data_pending_fsyncs", "fsyncs absolute 1 1"), - ]), - 'innodb_log': ( - "'' 'mysql InnoDB Log Operations' 'operations/s' innodb mysql.innodb_log line", - [ - ("Innodb_log_waits", "waits incremental 1 1"), - ("Innodb_log_write_requests", "write_requests incremental -1 1"), - ("Innodb_log_writes", "incremental -1 1"), - ]), - 'innodb_os_log': ( - "'' 'mysql InnoDB OS Log Operations' 'operations' innodb mysql.innodb_os_log line", - [ - ("Innodb_os_log_fsyncs", "fsyncs incremental 1 1"), - ("Innodb_os_log_pending_fsyncs", "pending_fsyncs absolute 1 1"), - ("Innodb_os_log_pending_writes", "pending_writes absolute -1 1"), - ]), - 'innodb_os_log_io': ( - "'' 'mysql InnoDB OS Log Bandwidth' 'kilobytes/s' innodb mysql.innodb_os_log_io area", - [ - ("Innodb_os_log_written", "write incremental -1 1024"), - ]), - 'innodb_cur_row_lock': ( - "'' 'mysql InnoDB Current Row Locks' 'operations' innodb mysql.innodb_cur_row_lock area", - [ - ("Innodb_row_lock_current_waits", "current_waits absolute 1 1"), - ]), - 'innodb_rows': ( - "'' 'mysql InnoDB Row Operations' 'operations/s' innodb mysql.innodb_rows area", - [ - ("Innodb_rows_inserted", "read incremental 1 1"), - ("Innodb_rows_read", "deleted incremental -1 1"), - ("Innodb_rows_updated", "inserted incremental 1 1"), - ("Innodb_rows_deleted", "updated incremental -1 1"), - ]), - 'innodb_buffer_pool_pages': ( - "'' 'mysql InnoDB Buffer Pool Pages' 'pages' innodb mysql.innodb_buffer_pool_pages line", - [ - ("Innodb_buffer_pool_pages_data", "data absolute 1 1"), - ("Innodb_buffer_pool_pages_dirty", "dirty absolute -1 1"), - ("Innodb_buffer_pool_pages_free", "free absolute 1 1"), - ("Innodb_buffer_pool_pages_flushed", "flushed incremental -1 1"), - ("Innodb_buffer_pool_pages_misc", "misc absolute -1 1"), - ("Innodb_buffer_pool_pages_total", "total absolute 1 1"), - ]), - 'innodb_buffer_pool_bytes': ( - "'' 'mysql InnoDB Buffer Pool Bytes' 'MB' innodb mysql.innodb_buffer_pool_bytes area", - [ - ("Innodb_buffer_pool_bytes_data", "data absolute 1"), - ("Innodb_buffer_pool_bytes_dirty", "dirty absolute -1"), - ]), - 'innodb_buffer_pool_read_ahead': ( - "'' 'mysql InnoDB Buffer Pool Read Ahead' 'operations/s' innodb mysql.innodb_buffer_pool_read_ahead area", - [ - ("Innodb_buffer_pool_read_ahead", "all incremental 1 1"), - ("Innodb_buffer_pool_read_ahead_evicted", "evicted incremental -1 1"), - ("Innodb_buffer_pool_read_ahead_rnd", "random incremental 1 1"), - ]), - 'innodb_buffer_pool_reqs': ( - "'' 'mysql InnoDB Buffer Pool Requests' 'requests/s' innodb mysql.innodb_buffer_pool_reqs area", - [ - ("Innodb_buffer_pool_read_requests", "reads incremental 1 1"), - ("Innodb_buffer_pool_write_requests", "writes incremental -1 1"), - ]), - 'innodb_buffer_pool_ops': ( - "'' 'mysql InnoDB Buffer Pool Operations' 'operations/s' innodb mysql.innodb_buffer_pool_ops area", - [ - ("Innodb_buffer_pool_reads", "'disk reads' incremental 1 1"), - ("Innodb_buffer_pool_wait_free", "'wait free' incremental -1 1"), - ]), - 'qcache_ops': ( - "'' 'mysql QCache Operations' 'queries/s' qcache mysql.qcache_ops line", - [ - ("Qcache_hits", "hits incremental 1 1"), - ("Qcache_lowmem_prunes", "'lowmem prunes' incremental -1 1"), - ("Qcache_inserts", "inserts incremental 1 1"), - ("Qcache_not_cached", "'not cached' incremental -1 1"), - ]), - 'qcache': ( - "'' 'mysql QCache Queries in Cache' 'queries' qcache mysql.qcache line", - [ - ("Qcache_queries_in_cache", "queries absolute 1 1"), - ]), - 'qcache_freemem': ( - "'' 'mysql QCache Free Memory' 'MB' qcache mysql.qcache_freemem area", - [ - ("Qcache_free_memory", "free absolute 1"), - ]), - 'qcache_memblocks': ( - "'' 'mysql QCache Memory Blocks' 'blocks' qcache mysql.qcache_memblocks line", - [ - ("Qcache_free_blocks", "free absolute 1"), - ("Qcache_total_blocks", "total absolute 1 1"), - ]), - 'key_blocks': ( - "'' 'mysql MyISAM Key Cache Blocks' 'blocks' myisam mysql.key_blocks line", - [ - ("Key_blocks_unused", "unused absolute 1 1"), - ("Key_blocks_used", "used absolute -1 1"), - ("Key_blocks_not_flushed", "'not flushed' absolute 1 1"), - ]), - 'key_requests': ( - "'' 'mysql MyISAM Key Cache Requests' 'requests/s' myisam mysql.key_requests area", - [ - ("Key_read_requests", "reads incremental 1 1"), - ("Key_write_requests", "writes incremental -1 1"), - ]), - 'key_disk_ops': ( - "'' 'mysql MyISAM Key Cache Disk Operations' 'operations/s' myisam mysql.key_disk_ops area", - [ - ("Key_reads", "reads incremental 1 1"), - ("Key_writes", "writes incremental -1 1"), - ]), - 'files': ( - "'' 'mysql Open Files' 'files' files mysql.files line", - [ - ("Open_files", "files absolute 1 1"), - ]), - 'files_rate': ( - "'' 'mysql Opened Files Rate' 'files/s' files mysql.files_rate line", - [ - ("Opened_files", "files incremental 1 1"), - ]), - 'binlog_stmt_cache': ( - "'' 'mysql Binlog Statement Cache' 'statements/s' binlog mysql.binlog_stmt_cache line", - [ - ("Binlog_stmt_cache_disk_use", "disk incremental 1 1"), - ("Binlog_stmt_cache_use", "all incremental 1 1"), - ]), - 'connection_errors': ( - "'' 'mysql Connection Errors' 'connections/s' connections mysql.connection_errors line", - [ - ("Connection_errors_accept", "accept incremental 1 1"), - ("Connection_errors_internal", "internal incremental 1 1"), - ("Connection_errors_max_connections", "max incremental 1 1"), - ("Connection_errors_peer_address", "peer_addr incremental 1 1"), - ("Connection_errors_select", "select incremental 1 1"), - ("Connection_errors_tcpwrap", "tcpwrap incremental 1 1") - ]) + 'net': { + 'options': [None, 'mysql Bandwidth', 'kilobits/s', 'bandwidth', 'mysql.net', 'area'], + 'lines': [ + ['Bytes_received', 'in', 'incremental', 8, 1024], + ['Bytes_sent', 'out', 'incremental', -8, 1024] + ]}, + 'queries': { + 'options': [None, 'mysql Queries', 'queries/s', 'queries', 'mysql.queries', 'line'], + 'lines': [ + ['Queries', 'queries', 'incremental'], + ['Questions', 'questions', 'incremental'], + ['Slow_queries', 'slow_queries', 'incremental'] + ]}, + 'handlers': { + 'options': [None, 'mysql Handlers', 'handlers/s', 'handlers', 'mysql.handlers', 'line'], + 'lines': [ + ['Handler_commit', 'commit', 'incremental'], + ['Handler_delete', 'delete', 'incremental'], + ['Handler_prepare', 'prepare', 'incremental'], + ['Handler_read_first', 'read_first', 'incremental'], + ['Handler_read_key', 'read_key', 'incremental'], + ['Handler_read_next', 'read_next', 'incremental'], + ['Handler_read_prev', 'read_prev', 'incremental'], + ['Handler_read_rnd', 'read_rnd', 'incremental'], + ['Handler_read_rnd_next', 'read_rnd_next', 'incremental'], + ['Handler_rollback', 'rollback', 'incremental'], + ['Handler_savepoint', 'savepoint', 'incremental'], + ['Handler_savepoint_rollback', 'savepoint_rollback', 'incremental'], + ['Handler_update', 'update', 'incremental'], + ['Handler_write', 'write', 'incremental'] + ]}, + 'table_locks': { + 'options': [None, 'mysql Tables Locks', 'locks/s', 'locks', 'mysql.table_locks', 'line'], + 'lines': [ + ['Table_locks_immediate', 'immediate', 'incremental'], + ['Table_locks_waited', 'waited', 'incremental', -1, 1] + ]}, + 'join_issues': { + 'options': [None, 'mysql Select Join Issues', 'joins/s', 'issues', 'mysql.join_issues', 'line'], + 'lines': [ + ['Select_full_join', 'full_join', 'incremental'], + ['Select_full_range_join', 'full_range_join', 'incremental'], + ['Select_range', 'range', 'incremental'], + ['Select_range_check', 'range_check', 'incremental'], + ['Select_scan', 'scan', 'incremental'] + ]}, + 'sort_issues': { + 'options': [None, 'mysql Sort Issues', 'issues/s', 'issues', 'mysql.sort_issues', 'line'], + 'lines': [ + ['Sort_merge_passes', 'merge_passes', 'incremental'], + ['Sort_range', 'range', 'incremental'], + ['Sort_scan', 'scan', 'incremental'] + ]}, + 'tmp': { + 'options': [None, 'mysql Tmp Operations', 'counter', 'temporaries', 'mysql.tmp', 'line'], + 'lines': [ + ['Created_tmp_disk_tables', 'disk_tables', 'incremental'], + ['Created_tmp_files', 'files', 'incremental'], + ['Created_tmp_tables', 'tables', 'incremental'] + ]}, + 'connections': { + 'options': [None, 'mysql Connections', 'connections/s', 'connections', 'mysql.connections', 'line'], + 'lines': [ + ['Connections', 'all', 'incremental'], + ['Aborted_connects', 'aborted', 'incremental'] + ]}, + 'binlog_cache': { + 'options': [None, 'mysql Binlog Cache', 'transactions/s', 'binlog', 'mysql.binlog_cache', 'line'], + 'lines': [ + ['Binlog_cache_disk_use', 'disk', 'incremental'], + ['Binlog_cache_use', 'all', 'incremental'] + ]}, + 'threads': { + 'options': [None, 'mysql Threads', 'threads', 'threads', 'mysql.threads', 'line'], + 'lines': [ + ['Threads_connected', 'connected', 'absolute'], + ['Threads_created', 'created', 'incremental'], + ['Threads_cached', 'cached', 'absolute', -1, 1], + ['Threads_running', 'running', 'absolute'], + ]}, + 'thread_cache_misses': { + 'options': [None, 'mysql Threads Cache Misses', 'misses', 'threads', 'mysql.thread_cache_misses', 'area'], + 'lines': [ + ['Thread_cache_misses', 'misses', 'absolute', 1, 100] + ]}, + 'innodb_io': { + 'options': [None, 'mysql InnoDB I/O Bandwidth', 'kilobytes/s', 'innodb', 'mysql.innodb_io', 'area'], + 'lines': [ + ['Innodb_data_read', 'read', 'incremental', 1, 1024], + ['Innodb_data_written', 'write', 'incremental', -1, 1024] + ]}, + 'innodb_io_ops': { + 'options': [None, 'mysql InnoDB I/O Operations', 'operations/s', 'innodb', 'mysql.innodb_io_ops', 'line'], + 'lines': [ + ['Innodb_data_reads', 'reads', 'incremental'], + ['Innodb_data_writes', 'writes', 'incremental', -1, 1], + ['Innodb_data_fsyncs', 'fsyncs', 'incremental'] + ]}, + 'innodb_io_pending_ops': { + 'options': [None, 'mysql InnoDB Pending I/O Operations', 'operations', 'innodb', 'mysql.innodb_io_pending_ops', 'line'], + 'lines': [ + ['Innodb_data_pending_reads', 'reads', 'absolute'], + ['Innodb_data_pending_writes', 'writes', 'absolute', -1, 1], + ['Innodb_data_pending_fsyncs', 'fsyncs', 'absolute'] + ]}, + 'innodb_log': { + 'options': [None, 'mysql InnoDB Log Operations', 'operations/s', 'innodb', 'mysql.innodb_log', 'line'], + 'lines': [ + ['Innodb_log_waits', 'waits', 'incremental'], + ['Innodb_log_write_requests', 'write_requests', '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'], + 'lines': [ + ['Innodb_os_log_fsyncs', 'fsyncs', 'incremental'], + ['Innodb_os_log_pending_fsyncs', 'pending_fsyncs', 'absolute'], + ['Innodb_os_log_pending_writes', 'pending_writes', 'absolute', -1, 1], + ]}, + 'innodb_os_log_io': { + 'options': [None, 'mysql InnoDB OS Log Bandwidth', 'kilobytes/s', 'innodb', 'mysql.innodb_os_log_io', 'area'], + 'lines': [ + ['Innodb_os_log_written', 'write', 'incremental', -1, 1024], + ]}, + 'innodb_cur_row_lock': { + 'options': [None, 'mysql InnoDB Current Row Locks', 'operations', 'innodb', 'mysql.innodb_cur_row_lock', 'area'], + 'lines': [ + ['Innodb_row_lock_current_waits', 'current_waits', 'absolute'] + ]}, + 'innodb_rows': { + 'options': [None, 'mysql InnoDB Row Operations', 'operations/s', 'innodb', 'mysql.innodb_rows', 'area'], + 'lines': [ + ['Innodb_rows_inserted', 'read', 'incremental'], + ['Innodb_rows_read', 'deleted', 'incremental', -1, 1], + ['Innodb_rows_updated', 'inserted', 'incremental', 1, 1], + ['Innodb_rows_deleted', 'updated', 'incremental', -1, 1], + ]}, + 'innodb_buffer_pool_pages': { + 'options': [None, 'mysql InnoDB Buffer Pool Pages', 'pages', 'innodb', 'mysql.innodb_buffer_pool_pages', 'line'], + 'lines': [ + ['Innodb_buffer_pool_pages_data', 'data', 'absolute'], + ['Innodb_buffer_pool_pages_dirty', 'dirty', 'absolute', -1, 1], + ['Innodb_buffer_pool_pages_free', 'free', 'absolute'], + ['Innodb_buffer_pool_pages_flushed', 'flushed', 'incremental', -1, 1], + ['Innodb_buffer_pool_pages_misc', 'misc', 'absolute', -1, 1], + ['Innodb_buffer_pool_pages_total', 'total', 'absolute'] + ]}, + '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', 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'], + 'lines': [ + ['Innodb_buffer_pool_read_ahead', 'all', 'incremental'], + ['Innodb_buffer_pool_read_ahead_evicted', 'evicted', 'incremental', -1, 1], + ['Innodb_buffer_pool_read_ahead_rnd', 'random', 'incremental'] + ]}, + 'innodb_buffer_pool_reqs': { + 'options': [None, 'mysql InnoDB Buffer Pool Requests', 'requests/s', 'innodb', 'mysql.innodb_buffer_pool_reqs', 'area'], + 'lines': [ + ['Innodb_buffer_pool_read_requests', 'reads', 'incremental'], + ['Innodb_buffer_pool_write_requests', 'writes', 'incremental', -1, 1] + ]}, + 'innodb_buffer_pool_ops': { + 'options': [None, 'mysql InnoDB Buffer Pool Operations', 'operations/s', 'innodb', 'mysql.innodb_buffer_pool_ops', 'area'], + 'lines': [ + ['Innodb_buffer_pool_reads', 'disk reads', 'incremental'], + ['Innodb_buffer_pool_wait_free', 'wait free', 'incremental', -1, 1] + ]}, + 'qcache_ops': { + 'options': [None, 'mysql QCache Operations', 'queries/s', 'qcache', 'mysql.qcache_ops', 'line'], + 'lines': [ + ['Qcache_hits', 'hits', 'incremental'], + ['Qcache_lowmem_prunes', 'lowmem prunes', 'incremental', -1, 1], + ['Qcache_inserts', 'inserts', 'incremental'], + ['Qcache_not_cached', 'not cached', 'incremental', -1, 1] + ]}, + 'qcache': { + 'options': [None, 'mysql QCache Queries in Cache', 'queries', 'qcache', 'mysql.qcache', 'line'], + 'lines': [ + ['Qcache_queries_in_cache', 'queries', 'absolute'] + ]}, + 'qcache_freemem': { + 'options': [None, 'mysql QCache Free Memory', 'MB', 'qcache', 'mysql.qcache_freemem', 'area'], + 'lines': [ + ['Qcache_free_memory', 'free', 'absolute', 1, 1024 * 1024] + ]}, + 'qcache_memblocks': { + 'options': [None, 'mysql QCache Memory Blocks', 'blocks', 'qcache', 'mysql.qcache_memblocks', 'line'], + 'lines': [ + ['Qcache_free_blocks', 'free', 'absolute'], + ['Qcache_total_blocks', 'total', 'absolute'] + ]}, + 'key_blocks': { + 'options': [None, 'mysql MyISAM Key Cache Blocks', 'blocks', 'myisam', 'mysql.key_blocks', 'line'], + 'lines': [ + ['Key_blocks_unused', 'unused', 'absolute'], + ['Key_blocks_used', 'used', 'absolute', -1, 1], + ['Key_blocks_not_flushed', 'not flushed', 'absolute'] + ]}, + 'key_requests': { + 'options': [None, 'mysql MyISAM Key Cache Requests', 'requests/s', 'myisam', 'mysql.key_requests', 'area'], + 'lines': [ + ['Key_read_requests', 'reads', 'incremental'], + ['Key_write_requests', 'writes', 'incremental', -1, 1] + ]}, + 'key_disk_ops': { + 'options': [None, 'mysql MyISAM Key Cache Disk Operations', 'operations/s', 'myisam', 'mysql.key_disk_ops', 'area'], + 'lines': [ + ['Key_reads', 'reads', 'incremental'], + ['Key_writes', 'writes', 'incremental', -1, 1] + ]}, + 'files': { + 'options': [None, 'mysql Open Files', 'files', 'files', 'mysql.files', 'line'], + 'lines': [ + ['Open_files', 'files', 'absolute'] + ]}, + 'files_rate': { + 'options': [None, 'mysql Opened Files Rate', 'files/s', 'files', 'mysql.files_rate', 'line'], + 'lines': [ + ['Opened_files', 'files', 'incremental'] + ]}, + 'binlog_stmt_cache': { + 'options': [None, 'mysql Binlog Statement Cache', 'statements/s', 'binlog', 'mysql.binlog_stmt_cache', 'line'], + 'lines': [ + ['Binlog_stmt_cache_disk_use', 'disk', 'incremental'], + ['Binlog_stmt_cache_use', 'all', 'incremental'] + ]}, + 'connection_errors': { + 'options': [None, 'mysql Connection Errors', 'connections/s', 'connections', 'mysql.connection_errors', 'line'], + 'lines': [ + ['Connection_errors_accept', 'accept', 'incremental'], + ['Connection_errors_internal', 'internal', 'incremental'], + ['Connection_errors_max_connections', 'max', 'incremental'], + ['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': [ + ['Seconds_Behind_Master', 'seconds', 'absolute'] + ]}, + 'slave_status': { + 'options': [None, 'Slave Status', 'status', 'slave', 'mysql.slave_status', 'line'], + 'lines': [ + ['Slave_SQL_Running', 'sql_running', 'absolute'], + ['Slave_IO_Running', 'io_running', 'absolute'] + ]} } -class Service(BaseService): +class Service(MySQLService): def __init__(self, configuration=None, name=None): - super(self.__class__, self).__init__(configuration=configuration, name=name) - self.configuration = self._parse_config(configuration) - self.connection = None - self.defs = {} - - def _parse_config(self, configuration): - """ - Parse configuration to collect data from MySQL server - :param configuration: dict - :return: dict - """ - if self.name is None: - self.name = 'local' - if 'user' not in configuration: - configuration['user'] = 'root' - if 'password' not in configuration: - configuration['password'] = '' - if 'my.cnf' in configuration: - configuration['socket'] = '' - configuration['host'] = '' - configuration['port'] = 0 - elif 'socket' in configuration: - configuration['my.cnf'] = '' - configuration['host'] = '' - configuration['port'] = 0 - elif 'host' in configuration: - configuration['my.cnf'] = '' - configuration['socket'] = '' - if 'port' in configuration: - configuration['port'] = int(configuration['port']) - else: - configuration['port'] = 3306 - - return configuration - - 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.configuration['update_every']) - except Exception as e: - self.error(NAME + " has problem connecting to server:", e) - raise RuntimeError + MySQLService.__init__(self, configuration=configuration, name=name) + self.order = ORDER + self.definitions = CHARTS + self.queries = dict(global_status=QUERY_GLOBAL, slave_status=QUERY_SLAVE) def _get_data(self): - """ - Get raw data from MySQL server - :return: dict - """ - if self.connection is None: - try: - self._connect() - except RuntimeError: - return None - try: - cursor = self.connection.cursor() - cursor.execute(QUERY) - raw_data = cursor.fetchall() - except Exception as e: - self.error(NAME + ": cannot execute query.", e) - self.connection.close() - self.connection = None - return None - return dict(raw_data) + raw_data = self._get_raw_data(description=True) - def check(self): - """ - Check if service is able to connect to server - :return: boolean - """ - try: - self.connection = self._connect() - return True - except RuntimeError: - self.connection = None - return False + if not raw_data: + return None - def create(self): - """ - Create graphs - :return: boolean - """ - for name in ORDER: - self.defs[name] = [] - for line in CHARTS[name][1]: - self.defs[name].append(line[0]) + to_netdata = dict() - idx = 0 - data = self._get_data() - if data is None: - return False - for name in ORDER: - header = "CHART mysql_" + \ - str(self.name) + "." + \ - name + " " + \ - CHARTS[name][0] + " " + \ - str(self.priority + idx) + " " + \ - str(self.update_every) - content = "" - # check if server has this data point - for line in CHARTS[name][1]: - if line[0] in data: - content += "DIMENSION " + line[0] + " " + line[1] + "\n" - if len(content) > 0: - print(header) - print(content) - idx += 1 + if 'global_status' in raw_data: + global_status = dict(raw_data['global_status'][0]) + for key in GLOBAL_STATS: + if key in global_status: + to_netdata[key] = global_status[key] + if 'Threads_created' in to_netdata and 'Connections' in to_netdata: + to_netdata['Thread_cache_misses'] = round(int(to_netdata['Threads_created']) / float(to_netdata['Connections']) * 10000) - if idx == 0: - return False - return True + if 'slave_status' in raw_data: + if raw_data['slave_status'][0]: + slave_raw_data = dict(zip([e[0] for e in raw_data['slave_status'][1]], raw_data['slave_status'][0][0])) + for key, function in SLAVE_STATS: + if key in slave_raw_data: + to_netdata[key] = function(slave_raw_data[key]) + else: + self.queries.pop('slave_status') - def update(self, interval): - """ - Update data on graphs - :param interval: int - :return: boolean - """ - data = self._get_data() - if data is None: - return False - try: - data['Thread cache misses'] = int(int(data['Threads_created']) * 10000 / int(data['Connections'])) - except Exception: - pass - for chart, dimensions in self.defs.items(): - header = "BEGIN mysql_" + str(self.name) + "." + chart + " " + str(interval) + '\n' - lines = "" - for d in dimensions: - try: - lines += "SET " + d + " = " + data[d] + '\n' - except KeyError: - pass - if len(lines) > 0: - print(header + lines + "END") + return to_netdata or None - return True