zabbix 监控 mysql

1.创建 mysql 账户来获取数据库数据
grant select on *.* to 'mysqlcheck'@'%' identified by 'mysqlcheck';
flush privileges;

2.在 zabbix_agentd.conf 文件添加一下参数,其中需要设置的是 -h 为 zabbix mysql 客户端的ip
UserParameter=mysql.Ping,mysqladmin -umysqlcheck -pmysqlcheck -h192.168.32.74  ping|grep alive|wc -l
UserParameter=mysql.Threads,mysqladmin -umysqlcheck -pmysqlcheck -h192.168.32.74  status|cut -f3 -d":"|cut -f1 -d"Q"
UserParameter=mysql.Questions,mysqladmin -umysqlcheck -pmysqlcheck -h192.168.32.74  status|cut -f4 -d":"|cut -f1 -d"S"
UserParameter=mysql.Slowqueries,mysqladmin -umysqlcheck -pmysqlcheck -h192.168.32.74  status|cut -f5 -d":"|cut -f1 -d"O"
UserParameter=mysql.Qps,mysqladmin -umysqlcheck -pmysqlcheck -h192.168.32.74  status|cut -f9 -d":"

UserParameter=mysql.Slave_IO_State,if [ "$(mysql -umysqlcheck -pmysqlcheck -h192.168.32.74  -e "show slave status\G"| grep Slave_IO_Running|awk '{print $2}')" == "Yes" ];then echo 1; else echo 0;fi
UserParameter=mysql.Slave_SQL_State,if [ "$(mysql -umysqlcheck -pmysqlcheck -h192.168.32.74  -e "show slave status\G"| grep Slave_SQL_Running|awk '{print $2}')" == "Yes" ];then echo 1; else echo 0;fi

UserParameter=mysql.Key_buffer_size,mysql -umysqlcheck -pmysqlcheck -h192.168.32.74  -e "show variables like 'key_buffer_size';"| grep -v Value |awk '{print $2/1024^2}'
UserParameter=mysql.Key_reads,mysql -umysqlcheck -pmysqlcheck -h192.168.32.74  -e "show status like 'key_reads';"| grep -v Value |awk '{print $2}'
UserParameter=mysql.Key_read_requests,mysql -umysqlcheck -pmysqlcheck -h192.168.32.74  -e "show status like 'key_read_requests';"| grep -v Value |awk '{print $2}'
UserParameter=mysql.Key_cache_miss_rate,echo $(mysql -umysqlcheck -pmysqlcheck -h192.168.32.74  -e "show status like 'key_reads';"| grep -v Value |awk '{print $2}') $(mysql -umysqlcheck -pmysqlcheck -h192.168.32.74  -e "show status like 'key_read_requests';"| grep -v Value |awk '{print $2}')| awk '{printf("%1.4f\n",$1/$2*100)}'


UserParameter=mysql.Key_blocks_used,mysql -umysqlcheck -pmysqlcheck -h192.168.32.74  -e "show status like 'key_blocks_used';"| grep -v Value |awk '{print $2}'
UserParameter=mysql.Key_blocks_unused,mysql -umysqlcheck -pmysqlcheck -h192.168.32.74  -e "show status like 'key_blocks_unused';"| grep -v Value |awk '{print $2}'
UserParameter=mysql.Key_blocks_used_rate,echo $(mysql -umysqlcheck -pmysqlcheck -h192.168.32.74  -e "show status like 'key_blocks_used';"| grep -v Value |awk '{print $2}') $(mysql -umysqlcheck -pmysqlcheck -h192.168.32.74  -e "show status like 'key_blocks_unused';"| grep -v Value |awk '{print $2}')| awk '{printf("%1.4f\n",$1/($1+$2)*100)}'


UserParameter=mysql.Innodb_buffer_pool_size,mysql -umysqlcheck -pmysqlcheck -h192.168.32.74  -e "show variables like 'innodb_buffer_pool_size';"| grep -v Value |awk '{print $2/1024^2}'
UserParameter=mysql.Innodb_log_file_size,mysql -umysqlcheck -pmysqlcheck -h192.168.32.74  -e "show variables like 'innodb_log_file_size';"| grep -v Value |awk '{print $2/1024^2}'
UserParameter=mysql.Innodb_log_buffer_size,mysql -umysqlcheck -pmysqlcheck -h192.168.32.74  -e "show variables like 'innodb_log_buffer_size';"| grep -v Value |awk '{print $2/1024^2}'

UserParameter=mysql.Table_open_cache,mysql -umysqlcheck -pmysqlcheck -h192.168.32.74  -e "show variables like 'table_open_cache';"| grep -v Value |awk '{print $2}'
UserParameter=mysql.Open_tables,mysql -umysqlcheck -pmysqlcheck -h192.168.32.74  -e "show status like 'open_tables';"| grep -v Value |awk '{print $2}'
UserParameter=mysql.Opened_tables,mysql -umysqlcheck -pmysqlcheck -h192.168.32.74  -e "show status like 'opened_tables';"| grep -v Value |awk '{print $2}'
UserParameter=mysql.Open_tables_rate,echo $(mysql -umysqlcheck -pmysqlcheck -h192.168.32.74  -e "show status like 'open_tables';"| grep -v Value |awk '{print $2}') $(mysql -umysqlcheck -pmysqlcheck -h192.168.32.74  -e "show status like 'opened_tables';"| grep -v Value |awk '{print $2}')| awk '{printf("%1.4f\n",$1/($1+$2)*100)}'
UserParameter=mysql.Table_open_cache_used_rate,echo $(mysql -umysqlcheck -pmysqlcheck -h192.168.32.74  -e "show status like 'open_tables';"| grep -v Value |awk '{print $2}') $(mysql -umysqlcheck -pmysqlcheck -h192.168.32.74  -e "show variables like 'table_open_cache';"| grep -v Value |awk '{print $2}')| awk '{printf("%1.4f\n",$1/($1+$2)*100)}'


UserParameter=mysql.Thread_cache_size,mysql -umysqlcheck -pmysqlcheck -h192.168.32.74  -e "show variables like 'thread_cache_size';"| grep -v Value |awk '{print $2}'
UserParameter=mysql.Threads_cached,mysql -umysqlcheck -pmysqlcheck -h192.168.32.74  -e "show status like 'Threads_cached';"| grep -v Value |awk '{print $2}'
UserParameter=mysql.Threads_connected,mysql -umysqlcheck -pmysqlcheck -h192.168.32.74  -e "show status like 'Threads_connected';"| grep -v Value |awk '{print $2}'
UserParameter=mysql.Threads_created,mysql -umysqlcheck -pmysqlcheck -h192.168.32.74  -e "show status like 'Threads_created';"| grep -v Value |awk '{print $2}'
UserParameter=mysql.Threads_running,mysql -umysqlcheck -pmysqlcheck -h192.168.32.74  -e "show status like 'Threads_running';"| grep -v Value |awk '{print $2}'

UserParameter=mysql.Qcache_free_blocks,mysql -umysqlcheck -pmysqlcheck -h192.168.32.74  -e "show status like 'Qcache_free_blocks';"| grep -v Value |awk '{print $2}'
UserParameter=mysql.Qcache_free_memory,mysql -umysqlcheck -pmysqlcheck -h192.168.32.74  -e "show status like 'Qcache_free_memory';"| grep -v Value |awk '{print $2}'
UserParameter=mysql.Qcache_hits,mysql -umysqlcheck -pmysqlcheck -h192.168.32.74  -e "show status like 'Qcache_hits';"| grep -v Value |awk '{print $2}'
UserParameter=mysql.Qcache_inserts,mysql -umysqlcheck -pmysqlcheck -h192.168.32.74  -e "show status like 'Qcache_inserts';"| grep -v Value |awk '{print $2}'
UserParameter=mysql.Qcache_lowmem_prunes,mysql -umysqlcheck -pmysqlcheck -h192.168.32.74  -e "show status like 'Qcache_lowmem_prunes';"| grep -v Value |awk '{print $2}'
UserParameter=mysql.Qcache_not_cached,mysql -umysqlcheck -pmysqlcheck -h192.168.32.74  -e "show status like 'Qcache_not_cached';"| grep -v Value |awk '{print $2}'
UserParameter=mysql.Qcache_queries_in_cache,mysql -umysqlcheck -pmysqlcheck -h192.168.32.74  -e "show status like 'Qcache_queries_in_cache';"| grep -v Value |awk '{print $2}'
UserParameter=mysql.Qcache_total_blocks,mysql -umysqlcheck -pmysqlcheck -h192.168.32.74  -e "show status like 'Qcache_total_blocks';"| grep -v Value |awk '{print $2}'
UserParameter=mysql.Qcache_fragment_rate,echo $(mysql -umysqlcheck -pmysqlcheck -h192.168.32.74  -e "show status like 'Qcache_free_blocks';"| grep -v Value |awk '{print $2}') $(mysql -umysqlcheck -pmysqlcheck -h192.168.32.74  -e "show status like 'Qcache_total_blocks';"| grep -v Value |awk '{print $2}')| awk '{printf("%1.4f\n",$1/$2*100)}'
UserParameter=mysql.Qcache_used_rate,echo $(mysql -umysqlcheck -pmysqlcheck -h192.168.32.74  -e "show variables like 'query_cache_size';"| grep -v Value |awk '{print $2}') $(mysql -umysqlcheck -pmysqlcheck -h192.168.32.74  -e "show status like 'Qcache_free_memory';"| grep -v Value |awk '{print $2}')| awk '{printf("%1.4f\n",($1-$2)/$1*100)}'
UserParameter=mysql.Qcache_hits_rate,echo $(mysql -umysqlcheck -pmysqlcheck -h192.168.32.74  -e "show status like 'Qcache_hits';"| grep -v Value |awk '{print $2}') $(mysql -umysqlcheck -pmysqlcheck -h192.168.32.74  -e "show status like 'Qcache_inserts';"| grep -v Value |awk '{print $2}')| awk '{printf("%1.4f\n",($1-$2)/$1*100)}'


UserParameter=mysql.Query_cache_limit,mysql -umysqlcheck -pmysqlcheck -h192.168.32.74  -e "show variables like 'query_cache_limit';"| grep -v Value |awk '{print $2}'
UserParameter=mysql.Query_cache_min_res_unit,mysql -umysqlcheck -pmysqlcheck -h192.168.32.74  -e "show variables like 'query_cache_min_res_unit';"| grep -v Value |awk '{print $2}'
UserParameter=mysql.Query_cache_size,mysql -umysqlcheck -pmysqlcheck -h192.168.32.74  -e "show variables like 'query_cache_size';"| grep -v Value |awk '{print $2}'

UserParameter=mysql.Sort_merge_passes,mysql -umysqlcheck -pmysqlcheck -h192.168.32.74  -e "show status like 'Sort_merge_passes';"| grep -v Value |awk '{print $2}'
UserParameter=mysql.Sort_range,mysql -umysqlcheck -pmysqlcheck -h192.168.32.74  -e "show status like 'Sort_range';"| grep -v Value |awk '{print $2}'
UserParameter=mysql.Sort_rows,mysql -umysqlcheck -pmysqlcheck -h192.168.32.74  -e "show status like 'Sort_rows';"| grep -v Value |awk '{print $2}'
UserParameter=mysql.Sort_scan,mysql -umysqlcheck -pmysqlcheck -h192.168.32.74  -e "show status like 'Sort_scan';"| grep -v Value |awk '{print $2}'

UserParameter=mysql.Handler_read_first,mysql -umysqlcheck -pmysqlcheck -h192.168.32.74  -e "show status like 'Handler_read_first';"| grep -v Value |awk '{print $2}'
UserParameter=mysql.Handler_read_key,mysql -umysqlcheck -pmysqlcheck -h192.168.32.74  -e "show status like 'Handler_read_key';"| grep -v Value |awk '{print $2}'
UserParameter=mysql.Handler_read_next,mysql -umysqlcheck -pmysqlcheck -h192.168.32.74  -e "show status like 'Handler_read_next';"| grep -v Value |awk '{print $2}'
UserParameter=mysql.Handler_read_prev,mysql -umysqlcheck -pmysqlcheck -h192.168.32.74  -e "show status like 'Handler_read_prev';"| grep -v Value |awk '{print $2}'
UserParameter=mysql.Handler_read_rnd,mysql -umysqlcheck -pmysqlcheck -h192.168.32.74  -e "show status like 'Handler_read_rnd';"| grep -v Value |awk '{print $2}'
UserParameter=mysql.Handler_read_rnd_next,mysql -umysqlcheck -pmysqlcheck -h192.168.32.74  -e "show status like 'Handler_read_rnd_next';"| grep -v Value |awk '{print $2}'

UserParameter=mysql.Com_select,mysql -umysqlcheck -pmysqlcheck -h192.168.32.74  -e "show status like 'com_select';"| grep -v Value |awk '{print $2}'
UserParameter=mysql.Com_insert,mysql -umysqlcheck -pmysqlcheck -h192.168.32.74  -e "show status like 'com_insert';"| grep -v Value |awk '{print $2}'
UserParameter=mysql.Com_insert_select,mysql -umysqlcheck -pmysqlcheck -h192.168.32.74  -e "show status like 'com_insert_select';"| grep -v Value |awk '{print $2}'
UserParameter=mysql.Com_update,mysql -umysqlcheck -pmysqlcheck -h192.168.32.74  -e "show status like 'com_update';"| grep -v Value |awk '{print $2}'
UserParameter=mysql.Com_replace,mysql -umysqlcheck -pmysqlcheck -h192.168.32.74  -e "show status like 'com_replace';"| grep -v Value |awk '{print $2}'
UserParameter=mysql.Com_replace_select,mysql -umysqlcheck -pmysqlcheck -h192.168.32.74  -e "show status like 'com_replace_select';"| grep -v Value |awk '{print $2}'
UserParameter=mysql.Table_scan_rate,echo $(mysql -umysqlcheck -pmysqlcheck -h192.168.32.74  -e "show status like 'Handler_read_rnd_next';"| grep -v Value |awk '{print $2}') $(mysql -umysqlcheck -pmysqlcheck -h192.168.32.74  -e "show status like 'com_select';"| grep -v Value |awk '{print $2}')| awk '{printf("%1.4f\n",$1/$2*100)}'


UserParameter=mysql.Open_files,mysql -umysqlcheck -pmysqlcheck -h192.168.32.74  -e "show status like 'open_files';"| grep -v Value |awk '{print $2}'
UserParameter=mysql.Open_files_limit,mysql -umysqlcheck -pmysqlcheck -h192.168.32.74  -e "show variables like 'open_files_limit';"| grep -v Value |awk '{print $2}'
UserParameter=mysql.Open_files_rate,echo $(mysql -umysqlcheck -pmysqlcheck -h192.168.32.74  -e "show status like 'open_files';"| grep -v Value |awk '{print $2}') $(mysql -umysqlcheck -pmysqlcheck -h192.168.32.74  -e "show variables like 'open_files_limit';"| grep -v Value |awk '{print $2}')| awk '{printf("%1.4f\n",$1/$2*100)}'


UserParameter=mysql.Created_tmp_disk_tables,mysql -umysqlcheck -pmysqlcheck -h192.168.32.74  -e "show status like 'created_tmp_disk_tables';"| grep -v Value |awk '{print $2}'
UserParameter=mysql.Created_tmp_tables,mysql -umysqlcheck -pmysqlcheck -h192.168.32.74  -e "show status like 'created_tmp_tables';"| grep -v Value |awk '{print $2}'
UserParameter=mysql.Created_tmp_disk_tables_rate,echo $(mysql -umysqlcheck -pmysqlcheck -h192.168.32.74  -e "show status like 'created_tmp_disk_tables';"| grep -v Value |awk '{print $2}') $(mysql -umysqlcheck -pmysqlcheck -h192.168.32.74  -e "show status like 'created_tmp_tables';"| grep -v Value |awk '{print $2}')| awk '{printf("%1.4f\n",$1/$2*100)}'


UserParameter=mysql.Max_connections,mysql -umysqlcheck -pmysqlcheck -h192.168.32.74  -e "show variables like 'max_connections';"| grep -v Value |awk '{print $2}'
UserParameter=mysql.Max_used_connections,mysql -umysqlcheck -pmysqlcheck -h192.168.32.74  -e "show status like 'Max_used_connections';"| grep -v Value |awk '{print $2}'
UserParameter=mysql.Max_connections_used_rate,echo $(mysql -umysqlcheck -pmysqlcheck -h192.168.32.74  -e "show status like 'Max_used_connections';"| grep -v Value |awk '{print $2}') $(mysql -umysqlcheck -pmysqlcheck -h192.168.32.74  -e "show variables like 'max_connections';"| grep -v Value |awk '{print $2}')| awk '{printf("%1.4f\n",$1/$2*100)}'


UserParameter=mysql.Table_locks_immediate,mysql -umysqlcheck -pmysqlcheck -h192.168.32.74  -e "show status like 'Table_locks_immediate';"| grep -v Value |awk '{print $2}'
UserParameter=mysql.Table_locks_waited,mysql -umysqlcheck -pmysqlcheck -h192.168.32.74  -e "show status like 'table_locks_waited';"| grep -v Value |awk '{print $2}'
UserParameter=mysql.Engine_select,echo $(mysql -umysqlcheck -pmysqlcheck -h192.168.32.74  -e "show status like 'Table_locks_immediate';"| grep -v Value |awk '{print $2}') $(mysql -umysqlcheck -pmysqlcheck -h192.168.32.74  -e "show status like 'table_locks_waited';"| grep -v Value |awk '{print $2}')| awk '{printf("%5.4f\n",$1/$2)}'

3.添加 zabbix_server 端的 mysql 模板
见 zabbix_mysql 模板

4.重启 zabbix_agentd 
service zabbix_agentd restart

你可能感兴趣的:(mysql,zabbix)