├── README.md ├── mm.conf ├── mm.items ├── mm.sh └── mm.xml /README.md: -------------------------------------------------------------------------------- 1 | # Mysql/Mariadb monitoring 2 | 3 | ## Description 4 | 5 | Yet another module for mysql monitoring 6 | Supports multiple instances and multi-master replication with low-level discovery 7 | Please, ensure that you are using mysqld that supports multi-source replication, e.g. mysql 5.7 or mariadb 10+ 8 | 9 | ## Requirements 10 | 11 | - Credentials in /etc/zabbix/.my.cnf 12 | 13 | ## Installing 14 | - Create user for monitoring, e.g. 15 | ``` 16 | GRANT PROCESS, REPLICATION CLIENT ON *.* TO `zabbix`@`localhost` 17 | ``` 18 | - Put mm.sh into /etc/zabbix folder 19 | - Run 20 | ``` 21 | chmod +x /etc/zabbix/mm.sh 22 | ``` 23 | - Put mm.items into /etc/zabbix/ 24 | - Put mm.conf into /etc/zabbix/zabbix_agentd.conf.d/ 25 | - Import template to zabbix - mm.xml 26 | - Apply “Template App Mysql Monitoring” template to host 27 | -------------------------------------------------------------------------------- /mm.conf: -------------------------------------------------------------------------------- 1 | # 2 | # Discovery mysql 3 | # 4 | UserParameter=mm.instances.discovery,/etc/zabbix/mm.sh discover_instances 5 | UserParameter=mm.replicas.discovery,/etc/zabbix/mm.sh discover_replicas 6 | UserParameter=mm.alive.instances,/etc/zabbix/mm.sh update_alive_instances 7 | UserParameter=mm.replication_status,/etc/zabbix/mm.sh update_replication_status 8 | UserParameter=mm.extended_status,/etc/zabbix/mm.sh update_extended_status 9 | -------------------------------------------------------------------------------- /mm.items: -------------------------------------------------------------------------------- 1 | Aria_pagecache_blocks_not_flushed 2 | Aria_pagecache_blocks_unused 3 | Aria_pagecache_blocks_used 4 | Aria_pagecache_read_requests 5 | Aria_pagecache_reads 6 | Aria_pagecache_write_requests 7 | Aria_pagecache_writes 8 | Aria_transaction_log_syncs 9 | Maria_pagecache_blocks_not_flushed 10 | Maria_pagecache_blocks_unused 11 | Maria_pagecache_blocks_used 12 | Maria_pagecache_read_requests 13 | Maria_pagecache_reads 14 | Maria_pagecache_write_requests 15 | Maria_pagecache_writes 16 | Maria_transaction_log_syncs 17 | aria_block_size 18 | aria_pagecache_blocks 19 | aria_pagecache_buffer_size 20 | maria_block_size 21 | maria_pagecache_blocks 22 | maria_pagecache_buffer_size 23 | Innodb_adaptive_hash_cells 24 | Innodb_adaptive_hash_hash_searches 25 | Innodb_adaptive_hash_heap_buffers 26 | Innodb_adaptive_hash_non_hash_searches 27 | Innodb_buffer_pool_hit_ratio 28 | Innodb_buffer_pool_max_dirty_pages 29 | Innodb_buffer_pool_max_dirty_pages_b 30 | Innodb_buffer_pool_pages_data 31 | Innodb_buffer_pool_pages_data_b 32 | Innodb_buffer_pool_pages_dirty 33 | Innodb_buffer_pool_pages_dirty_b 34 | Innodb_buffer_pool_pages_free 35 | Innodb_buffer_pool_pages_free_b 36 | Innodb_buffer_pool_pages_misc 37 | Innodb_buffer_pool_pages_misc_b 38 | Innodb_buffer_pool_pages_total 39 | Innodb_buffer_pool_pages_total_b 40 | Innodb_buffer_pool_read_requests 41 | Innodb_buffer_pool_reads 42 | Innodb_data_fsyncs 43 | Innodb_data_pending_fsyncs 44 | Innodb_data_pending_reads 45 | Innodb_data_pending_writes 46 | Innodb_data_read 47 | Innodb_data_reads 48 | Innodb_data_writes 49 | Innodb_data_written 50 | Innodb_log_waits 51 | Innodb_lsn_current 52 | Innodb_lsn_flushed 53 | Innodb_lsn_last_checkpoint 54 | Innodb_mutex_os_waits 55 | Innodb_mutex_spin_rounds 56 | Innodb_mutex_spin_waits 57 | Innodb_os_log_fsyncs 58 | Innodb_os_log_pending_fsyncs 59 | Innodb_os_log_pending_writes 60 | Innodb_row_lock_current_waits 61 | Innodb_row_lock_time 62 | Innodb_row_lock_time_avg 63 | Innodb_row_lock_time_max 64 | Innodb_row_lock_waits 65 | Innodb_rows_deleted 66 | Innodb_rows_inserted 67 | Innodb_rows_read 68 | Innodb_rows_updated 69 | ignore_builtin_innodb 70 | innodb_buffer_pool_instances 71 | innodb_buffer_pool_size 72 | innodb_file_format 73 | innodb_file_per_table 74 | innodb_flush_log_at_trx_commit 75 | innodb_force_recovery 76 | innodb_io_capacity 77 | innodb_log_buffer_size 78 | innodb_log_file_size 79 | innodb_page_size 80 | Binlog_APPEND_BLOCK_EVENT_count 81 | Binlog_BEGIN_LOAD_QUERY_EVENT_count 82 | Binlog_CREATE_FILE_EVENT_count 83 | Binlog_DELETE_FILE_EVENT_count 84 | Binlog_DELETE_ROWS_EVENT_count 85 | Binlog_EXECUTE_LOAD_QUERY_EVENT_count 86 | Binlog_EXEC_LOAD_EVENT_count 87 | Binlog_FORMAT_DESCRIPTION_EVENT_count 88 | Binlog_INCIDENT_EVENT_count 89 | Binlog_INTVAR_EVENT_count 90 | Binlog_LOAD_EVENT_count 91 | Binlog_QUERY_EVENT_count 92 | Binlog_RAND_EVENT_count 93 | Binlog_ROTATE_EVENT_count 94 | Binlog_SLAVE_EVENT_count 95 | Binlog_START_EVENT_count 96 | Binlog_STOP_EVENT_count 97 | Binlog_TABLE_MAP_EVENT_count 98 | Binlog_UPDATE_ROWS_EVENT_count 99 | Binlog_USER_VAR_EVENT_count 100 | Binlog_WRITE_ROWS_EVENT_count 101 | Binlog_XID_EVENT_count 102 | Binlog_avg_event_size 103 | Binlog_count 104 | Binlog_event_count 105 | Binlog_file 106 | Binlog_number 107 | Binlog_position 108 | Binlog_total_size 109 | Slave_count 110 | Key_blocks_not_flushed 111 | Key_blocks_unused 112 | Key_blocks_used 113 | Key_blocks_warm 114 | Key_read_requests 115 | Key_reads 116 | Key_write_requests 117 | Key_writes 118 | key_buffer_blocks 119 | key_buffer_size 120 | key_cache_block_size 121 | Aborted_clients 122 | Aborted_connects 123 | Access_denied_errors 124 | Binlog_bytes_written 125 | Binlog_cache_disk_use 126 | Binlog_cache_use 127 | Bytes_received 128 | Bytes_sent 129 | Com_call_proc 130 | Com_delete 131 | Com_delete_multi 132 | Com_insert 133 | Com_insert_select 134 | Com_replace 135 | Com_replace_select 136 | Com_select 137 | Com_show_create_table 138 | Com_show_fields 139 | Com_update 140 | Com_update_multi 141 | Connections 142 | Created_tmp_disk_tables 143 | Created_tmp_files 144 | Created_tmp_tables 145 | Handler_delete 146 | Handler_read_first 147 | Handler_read_key 148 | Handler_read_last 149 | Handler_read_next 150 | Handler_read_prev 151 | Handler_read_rnd 152 | Handler_read_rnd_next 153 | Handler_update 154 | Handler_write 155 | Max_used_connections 156 | Open_table_definitions 157 | Open_tables 158 | Opened_files 159 | Opened_table_definitions 160 | Opened_tables 161 | Qcache_free_blocks 162 | Qcache_free_memory 163 | Qcache_hits 164 | Qcache_inserts 165 | Qcache_lowmem_prunes 166 | Qcache_not_cached 167 | Qcache_queries_in_cache 168 | Qcache_total_blocks 169 | Queries 170 | Questions 171 | Select_full_join 172 | Select_full_range_join 173 | Select_range_check 174 | Select_scan 175 | Slow_queries 176 | Sort_merge_passes 177 | Sort_range 178 | Sort_rows 179 | Sort_scan 180 | Table_locks_immediate 181 | Table_locks_waited 182 | Threads_cached 183 | Threads_connected 184 | Threads_created 185 | Threads_running 186 | Uptime 187 | alive 188 | max_connections 189 | max_user_connections 190 | query_cache_size 191 | table_cache 192 | table_definition_cache 193 | table_open_cache 194 | version 195 | Exec_Master_Log_Pos 196 | Read_Master_Log_Pos 197 | Relay_Log_Pos 198 | Seconds_Behind_Master 199 | Slave_IO_Running 200 | Slave_SQL_Running 201 | Com_select 202 | Com_insert 203 | Com_update 204 | Com_delete 205 | Com_replace 206 | Com_call_procedure 207 | Com_show_authors 208 | Com_show_binlog_events 209 | Com_show_binlogs 210 | Com_show_charsets 211 | Com_show_client_statistics 212 | Com_show_collations 213 | Com_show_contributors 214 | Com_show_create_db 215 | Com_show_create_event 216 | Com_show_create_func 217 | Com_show_create_proc 218 | Com_show_create_table 219 | Com_show_create_trigger 220 | Com_show_databases 221 | Com_show_engine_logs 222 | Com_show_engine_mutex 223 | Com_show_engine_status 224 | Com_show_errors 225 | Com_show_events 226 | Com_show_explain 227 | Com_show_fields 228 | Com_show_function_status 229 | Com_show_grants 230 | Com_show_index_statistics 231 | Com_show_keys 232 | Com_show_master_status 233 | Com_show_open_tables 234 | Com_show_plugins 235 | Com_show_privileges 236 | Com_show_procedure_status 237 | Com_show_processlist 238 | Com_show_profile 239 | Com_show_profiles 240 | Com_show_relaylog_events 241 | Com_show_slave_hosts 242 | Com_show_slave_status 243 | Com_show_status 244 | Com_show_storage_engines 245 | Com_show_table_statistics 246 | Com_show_table_status 247 | Com_show_tables 248 | Com_show_triggers 249 | Com_show_user_statistics 250 | Com_show_variables 251 | Com_show_warnings 252 | -------------------------------------------------------------------------------- /mm.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | hostname=$(grep -i "Hostname=" /etc/zabbix/zabbix_agentd.conf | sed 's/Hostname=//g') 4 | 5 | get_mysqld_sockets() { 6 | ss -xl 2>/dev/null | awk '/mysql(.*)?\.sock/ && !/handler-socket/ { print $5 }' 7 | } 8 | 9 | check_alive() { 10 | mysqladmin --defaults-extra-file=/etc/zabbix/.my.cnf -S ${1} ping >/dev/null 2>&1 11 | socket_alive=$? 12 | } 13 | 14 | send_to_zabbix() { 15 | zabbix_sender -T -c /etc/zabbix/zabbix_agentd.conf -i "${1}" > /tmp/mm.zabbix_sender.log 2>&1 16 | } 17 | 18 | format_data_out() { 19 | local _name=$1 20 | cat | awk 'BEGIN { check=0; count=0; array[0]=0; } { 21 | array[count]=$1; 22 | count=count+1; 23 | } 24 | END { 25 | printf( "{\n\t\"data\":[\n" ); 26 | for(i=0;i> /tmp/mm.alive_instances.dat~ 55 | done 56 | 57 | mv /tmp/mm.alive_instances.dat~ /tmp/mm.alive_instances.dat 58 | send_to_zabbix /tmp/mm.alive_instances.dat && echo 1 || echo 0 59 | } 60 | 61 | 62 | discover_instances() { 63 | get_mysqld_sockets \ 64 | | rev | cut -d'/' -f1 | rev | grep -Eo 'mysql([-a-z]+)?' \ 65 | | format_data_out '#INSTANCE' 66 | } 67 | 68 | discover_replicas() { 69 | for socket in $(get_mysqld_sockets) ; do 70 | instance=$(echo $socket | rev | cut -d'/' -f1 | rev | grep -Eo 'mysql([-a-z]+)?') 71 | replicas=$(get_slave_data ${socket} Connection_name) 72 | for replica in ${replicas:-main}; do 73 | echo "${instance}"_"${replica}"; 74 | done 75 | done | format_data_out '#REPLICA' 76 | } 77 | 78 | update_extended_status() { 79 | timestamp=$(date +'%s') 80 | rm -f /tmp/mm.extended_status.dat 81 | 82 | for socket in $(get_mysqld_sockets) 83 | do 84 | check_alive ${socket} 85 | instance=$(echo $socket | rev | cut -d'/' -f1 | rev | grep -Eo 'mysql[-a-z]+') 86 | mysql --defaults-extra-file=/etc/zabbix/.my.cnf -S ${socket} -e 'show GLOBAL status' \ 87 | | tail -n+2 \ 88 | | awk "{if (\$2 != \"\") print \"${hostname} mm.mysql.\" \$1 \"[${instance}]\"\" ${timestamp} \" \$2}" \ 89 | | grep -i -f /etc/zabbix/mm.items >> /tmp/mm.extended_status.dat~ 90 | done 91 | 92 | mv /tmp/mm.extended_status.dat~ /tmp/mm.extended_status.dat 93 | send_to_zabbix /tmp/mm.extended_status.dat 94 | echo 1 95 | } 96 | 97 | update_replication_status() { 98 | rm -f /tmp/mm.replication_status.dat 99 | timestamp=$(date +'%s') 100 | 101 | for socket in $(get_mysqld_sockets) 102 | do 103 | instance=$(echo $socket | rev | cut -d'/' -f1 | rev | grep -Eo 'mysql[-a-z]+') 104 | replicas=$(get_slave_data ${socket} Connection_name) 105 | for replica in ${replicas:-main} 106 | do 107 | SBM=$(get_slave_data ${socket} "Seconds_Behind_Master" ${replica}) 108 | SIR=$(get_slave_data ${socket} "Slave_IO_Running" ${replica}) 109 | SSR=$(get_slave_data ${socket} "Slave_SQL_Running" ${replica}) 110 | echo "${hostname} mm.Seconds_Behind_Master[${instance}_${replica}] ${timestamp} ${SBM/NULL/0}" >> /tmp/mm.replication_status.dat~ 111 | echo "${hostname} mm.Slave_IO_Running[${instance}_${replica}] ${timestamp} ${SIR}" >> /tmp/mm.replication_status.dat~ 112 | echo "${hostname} mm.Slave_SQL_Running[${instance}_${replica}] ${timestamp} ${SSR}" >> /tmp/mm.replication_status.dat~ 113 | done 114 | done 115 | 116 | mv /tmp/mm.replication_status.dat~ /tmp/mm.replication_status.dat 117 | send_to_zabbix /tmp/mm.replication_status.dat && echo 1 || echo 0 118 | } 119 | 120 | parse_args() 121 | { 122 | case "$1" in 123 | discover_instances \ 124 | | discover_replicas \ 125 | | update_alive_instances \ 126 | | update_replication_status \ 127 | | update_extended_status ) 128 | $1 129 | ;; 130 | *) 131 | echo "usage: ${0} [-h] [discover_instances] [discover_replicas] [update_alive_instances] [update_replication_status] [update_extended_status] 132 | 133 | optional arguments: 134 | -h, --help --usage show this help message and exit 135 | discover_instances return a list of mysqld instances in zabbix discovery format 136 | discover_replicas return a list of all mysqld instances replications in zabbix discovery format 137 | update_alive_instances send list of mysqld instances statuses to zabbix 138 | update_replication_status send list of mysqld instances replicas statuses to zabbix 139 | update_extended_status send output of 'show GLOBAL status' of each mysqld instance to zabbix" 140 | ;; 141 | esac 142 | } 143 | 144 | parse_args "$@" 145 | 146 | # vim: noet ts=4 147 | --------------------------------------------------------------------------------