Zabbix監控的方式有四個大方向:
mysql
1,Zabbix Agent類型sql
2,SNMP類型shell
3,JMX類型bash
4,IMPI類型服務器
用的多的基本上就兩種,Agent類型 和 SNMP類型。網絡
Agent類型的item是使用部署在服務端的Agent來獲取數據,能夠分爲主動和被動模式。app
SNMP是監控服務器之外設備的很是好的方式,好比能夠監控網絡設備,打印機等,只要是有SNMP功能的,zabbix均可以監控。ide
今天咱們要說的MySQL監控是基於Agent類型的監控。debug
咱們來看看服務端的默認模板:3d
item 有14個,根據這14個item生成有2個圖形類型。
第一個item的信息如圖,監控類型爲zabbix agent,key爲mysql.version ,輸出類型爲字符串,更新週期默認爲 3600秒。值的保存日期爲7天。。。。
被監控端安裝好zabbix agent後,添加好MySQL監控模板,而後在Zabbix_server端查看日誌的後你會發現
因此咱們還須要修改被監控端的 zabbix_agentd.conf 手動添加對應key所執行的數據採集語句(多用SHELL語句編寫)。
客戶端添加自定義監控項的方法爲:在配置文件後面添加語句爲 UserParameter=app.key,/some/shell/output
好比:監控mysql.version的話 語句爲
mysql.version,mysqladmin -u -p version | grep 'Server version' | awk '{printf "MySQL Server Version: "$3}'
輸出的值爲 MySQL Server Version: 5.1.73
因此添加到配置文件最後就添加好一條監控項了。
根據上圖所示,mysql的值有許多,這些值通常都在 mysqladmin status | mysqladmin extended-status 裏面。因此只須要寫一個shell腳本就能夠吧剩下的所有監控起來。
我這裏提供一個shell腳本示例
#!/bin/bash PATH=${PATH}:/usr/local/mysql5.6/bin/ case $1 in Uptime) mysqladmin status | awk '{print$2}' ;; Com_update) mysqladmin extended-status | awk '/Com_update\>/ {print $4}' ;; Slow_queries) mysqladmin extended-status | awk '/Slow_queries/ {print $4}' ;; Com_select) mysqladmin extended-status | awk '/Com_select/ {print $4}' ;; Com_rollback) mysqladmin extended-status | awk '/Com_rollback\>/ {print $4}' ;; Questions) mysqladmin extended-status | awk '/Questions/ {print $4}' ;; Com_insert) mysqladmin extended-status | awk '/Com_insert\>/ {print $4}' ;; Com_delete) mysqladmin extended-status | awk '/Com_delete\>/ {print $4}' ;; Com_commit) mysqladmin extended-status | awk '/Com_commit/ {print $4}' ;; Bytes_sent) mysqladmin extended-status | awk '/Bytes_sent/ {print $4}' ;; Bytes_received) mysqladmin extended-status | awk '/Bytes_received/ {print $4}' ;; Com_begin) mysqladmin extended-status | awk '/Com_begin/ {print $4}' ;; Aborted_clients) mysqladmin extended-status | awk '/\<Aborted_clients\>/ {print $4}' ;; Aborted_connects) mysqladmin extended-status | awk '/\<Aborted_connects\>/ {print $4}' ;; Binlog_cache_disk_use) mysqladmin extended-status | awk '/\<Binlog_cache_disk_use\>/ {print $4}' ;; Binlog_cache_use) mysqladmin extended-status | awk '/\<Binlog_cache_use\>/ {print $4}' ;; Binlog_stmt_cache_disk_use) mysqladmin extended-status | awk '/\<Binlog_stmt_cache_disk_use\>/ {print $4}' ;; Binlog_stmt_cache_use) mysqladmin extended-status | awk '/\<Binlog_stmt_cache_use\>/ {print $4}' ;; Connections) mysqladmin extended-status | awk '/\<Connections\>/ {print $4}' ;; Created_tmp_disk_tables) mysqladmin extended-status | awk '/\<Created_tmp_disk_tables\>/ {print $4}' ;; Created_tmp_files) mysqladmin extended-status | awk '/\<Created_tmp_files\>/ {print $4}' ;; Created_tmp_tables) mysqladmin extended-status | awk '/\<Created_tmp_tables\>/ {print $4}' ;; Flush_commands) mysqladmin extended-status | awk '/\<Flush_commands\>/ {print $4}' ;; Handler_commit) mysqladmin extended-status | awk '/\<Handler_commit\>/{print $4}' ;; Handler_delete) mysqladmin extended-status | awk '/\<Handler_delete\>/{print $4}' ;; Handler_external_lock) mysqladmin extended-status | awk '/\<Handler_external_lock\>/{print $4}' ;; Handler_prepare) mysqladmin extended-status | awk '/\<Handler_prepare\>/{print $4}' ;; Handler_read_first) mysqladmin extended-status | awk '/\<Handler_read_first\>/{print $4}' ;; Handler_read_key) mysqladmin extended-status | awk '/\<Handler_read_key\>/{print $4}' ;; Handler_read_last) mysqladmin extended-status | awk '/\<Handler_read_last\>/{print $4}' ;; Handler_read_next) mysqladmin extended-status | awk '/\<Handler_read_next\>/{print $4}' ;; Handler_read_prev) mysqladmin extended-status | awk '/\<Handler_read_prev\>/{print $4}' ;; Handler_read_rnd) mysqladmin extended-status | awk '/\<Handler_read_rnd\>/{print $4}' ;; Handler_read_rnd_next) mysqladmin extended-status | awk '/\<Handler_read_rnd_next\>/{print $4}' ;; Handler_rollback) mysqladmin extended-status | awk '/\<Handler_rollback\>/{print $4}' ;; Handler_savepoint) mysqladmin extended-status | awk '/\<Handler_savepoint\>/{print $4}' ;; Handler_savepoint_rollback) mysqladmin extended-status | awk '/\<Handler_savepoint_rollback\>/{print $4}' ;; Handler_update) mysqladmin extended-status | awk '/\<Handler_update\>/{print $4}' ;; Handler_write) mysqladmin extended-status | awk '/\<Handler_write\>/{print $4}' ;; Key_blocks_not_flushed) mysqladmin extended-status | awk '/\<Key_blocks_not_flushed\>/{print $4}' ;; Key_blocks_unused) mysqladmin extended-status | awk '/\<Key_blocks_unused\>/{print $4}' ;; Key_blocks_used) mysqladmin extended-status | awk '/\<Key_blocks_used\>/{print $4}' ;; Key_read_requests) mysqladmin extended-status | awk '/\<Key_read_requests\>/{print $4}' ;; Key_reads) mysqladmin extended-status | awk '/\<Key_reads\>/{print $4}' ;; Key_write_requests) mysqladmin extended-status | awk '/\<Key_write_requests\>/{print $4}' ;; Key_writes) mysqladmin extended-status | awk '/\<Key_writes\>/{print $4}' ;; Max_used_connections) mysqladmin extended-status | awk '/\<Max_used_connections\>/{print $4}' ;; Open_files) mysqladmin extended-status | awk '/\<Open_files\>/{print $4}' ;; Open_table_definitions) mysqladmin extended-status | awk '/\<Open_table_definitions\>/{print $4}' ;; Open_tables) mysqladmin extended-status | awk '/\<Open_tables\>/{print $4}' ;; Opened_table_definitions) mysqladmin extended-status | awk '/\<Opened_table_definitions\>/{print $4}' ;; Opened_tables) mysqladmin extended-status | awk '/\<Opened_tables\>/{print $4}' ;; Prepared_stmt_count) mysqladmin extended-status | awk '/\<Prepared_stmt_count\>/{print $4}' ;; Questions) mysqladmin extended-status | awk '/\<Questions\>/{print $4}' ;; Select_full_join) mysqladmin extended-status | awk '/\<Select_full_join\>/{print $4}' ;; Select_full_range_join) mysqladmin extended-status | awk '/\<Select_full_range_join\>/{print $4}' ;; Select_range) mysqladmin extended-status | awk '/\<Select_range\>/{print $4}' ;; Select_scan) mysqladmin extended-status | awk '/\<Select_scan\>/{print $4}' ;; Select_scan) mysqladmin extended-status | awk '/\<Select_scan\>/{print $4}' ;; Slave_heartbeat_period) mysqladmin extended-status | awk '/\<Slave_heartbeat_period\>/{print $4}' ;; Slave_open_temp_tables) mysqladmin extended-status | awk '/\<Slave_open_temp_tables\>/{print $4}' ;; Slave_retried_transactions) mysqladmin extended-status | awk '/\<Slave_retried_transactions\>/{print $4}' ;; Slave_running) mysqladmin extended-status | awk '/\<Slave_running\>/{print $4}' ;; Slow_queries) mysqladmin extended-status | awk '/\<Slow_queries\>/{print $4}' ;; Table_locks_immediate) mysqladmin extended-status | awk '/\<Table_locks_immediate\>/{print $4}' ;; Table_locks_waited) mysqladmin extended-status | awk '/\<Table_locks_waited\>/{print $4}' ;; #表鎖數 Table_locks_immediate) mysqladmin extended-status | awk '/\<Table_locks_immediate\>/{print $4}' ;; Threads_cached) mysqladmin extended-status | awk '/\<Threads_cached\>/{print $4}' ;; Threads_connected) mysqladmin extended-status | awk '/\<Threads_connected\>/{print $4}' ;; Threads_connected) mysqladmin extended-status | awk '/\<Threads_connected\>/{print $4}' ;; Threads_created) mysqladmin extended-status | awk '/\<Threads_created\>/{print $4}' ;; Threads_running) mysqladmin extended-status | awk '/\<Threads_running\>/{print $4}' ;; Innodb_available_undo_logs) mysqladmin extended-status | awk '/\<Innodb_available_undo_logs\>/{print $4}' ;; Innodb_buffer_pool_pages_data) mysqladmin extended-status | awk '/\<Innodb_buffer_pool_pages_data\>/{print $4}' ;; Innodb_buffer_pool_pages_dirty) mysqladmin extended-status | awk '/\<Innodb_buffer_pool_pages_dirty\>/{print $4}' ;; Innodb_buffer_pool_pages_flushed) mysqladmin extended-status | awk '/\<Innodb_buffer_pool_pages_flushed\>/{print $4}' ;; Innodb_buffer_pool_pages_free) mysqladmin extended-status | awk '/\<Innodb_buffer_pool_pages_free\>/{print $4}' ;; Innodb_buffer_pool_pages_latched) mysqladmin extended-status | awk '/\<Innodb_buffer_pool_pages_latched\>/{print $4}' ;; # debug only Innodb_buffer_pool_pages_misc) mysqladmin extended-status | awk '/\<Innodb_buffer_pool_pages_misc\>/{print $4}' ;; Innodb_buffer_pool_pages_total) mysqladmin extended-status | awk '/\<Innodb_buffer_pool_pages_total\>/{print $4}' ;; Innodb_buffer_pool_read_ahead) mysqladmin extended-status | awk '/\<Innodb_buffer_pool_read_ahead\>/{print $4}' ;; Innodb_buffer_pool_read_ahead_evicted) mysqladmin extended-status | awk '/\<Innodb_buffer_pool_read_ahead_evicted\>/{print $4}' ;; Innodb_buffer_pool_read_requests) mysqladmin extended-status | awk '/\<Innodb_buffer_pool_read_requests\>/{print $4}' ;; Innodb_buffer_pool_reads) mysqladmin extended-status | awk '/\<Innodb_buffer_pool_reads\>/{print $4}' ;; Innodb_buffer_pool_wait_free) mysqladmin extended-status | awk '/\<Innodb_buffer_pool_wait_free\>/{print $4}' ;; Innodb_buffer_pool_write_requests) mysqladmin extended-status | awk '/\<Innodb_buffer_pool_write_requests\>/{print $4}' ;; Innodb_data_fsyncs) mysqladmin extended-status | awk '/\<Innodb_data_fsyncs\>/{print $4}' ;; Innodb_data_pending_fsyncs) mysqladmin extended-status | awk '/\<Innodb_data_pending_fsyncs\>/{print $4}' ;; Innodb_data_pending_reads) mysqladmin extended-status | awk '/\<Innodb_data_pending_reads\>/{print $4}' ;; Innodb_data_pending_writes) mysqladmin extended-status | awk '/\<Innodb_data_pending_writes\>/{print $4}' ;; Innodb_data_read) mysqladmin extended-status | awk '/\<Innodb_data_read\>/{print $4}' ;; Innodb_data_reads) mysqladmin extended-status | awk '/\<Innodb_data_reads\>/{print $4}' ;; Innodb_data_writes) mysqladmin extended-status | awk '/\<Innodb_data_writes\>/{print $4}' ;; Innodb_data_written) mysqladmin extended-status | awk '/\<Innodb_data_written\>/{print $4}' ;; Innodb_dblwr_pages_written) mysqladmin extended-status | awk '/\<Innodb_dblwr_pages_written\>/{print $4}' ;; Innodb_dblwr_writes) mysqladmin extended-status | awk '/\<Innodb_dblwr_writes\>/{print $4}' ;; Innodb_log_waits) mysqladmin extended-status | awk '/\<Innodb_log_waits\>/{print $4}' ;; Innodb_log_write_requests) mysqladmin extended-status | awk '/\<Innodb_log_write_requests\>/{print $4}' ;; Innodb_log_writes) mysqladmin extended-status | awk '/\<Innodb_log_writes\>/{print $4}' ;; Innodb_num_open_files) mysqladmin extended-status | awk '/\<Innodb_num_open_files\>/{print $4}' ;; Innodb_os_log_fsyncs) mysqladmin extended-status | awk '/\<Innodb_os_log_fsyncs\>/{print $4}' ;; Innodb_os_log_pending_fsyncs) mysqladmin extended-status | awk '/\<Innodb_os_log_pending_fsyncs\>/{print $4}' ;; Innodb_os_log_pending_writes) mysqladmin extended-status | awk '/\<Innodb_os_log_pending_writes\>/{print $4}' ;; Innodb_os_log_written) mysqladmin extended-status | awk '/\<Innodb_os_log_written\>/{print $4}' ;; Innodb_page_size) mysqladmin extended-status | awk '/\<Innodb_page_size\>/{print $4}' ;; Innodb_pages_created) mysqladmin extended-status | awk '/\<Innodb_pages_created\>/{print $4}' ;; Innodb_pages_read) mysqladmin extended-status | awk '/\<Innodb_pages_read\>/{print $4}' ;; Innodb_pages_written) mysqladmin extended-status | awk '/\<Innodb_pages_written\>/{print $4}' ;; Innodb_row_lock_current_waits) mysqladmin extended-status | awk '/\<Innodb_row_lock_current_waits\>/{print $4}' ;; Innodb_row_lock_waits) mysqladmin extended-status | awk '/\<Innodb_row_lock_waits\>/{print $4}' ;; Innodb_rows_deleted) mysqladmin extended-status | awk '/\<Innodb_rows_deleted\>/{print $4}' ;; Innodb_rows_inserted) mysqladmin extended-status | awk '/\<Innodb_rows_inserted\>/{print $4}' ;; Innodb_rows_read) mysqladmin extended-status | awk '/\<Innodb_rows_read\>/{print $4}' ;; Innodb_rows_updated) mysqladmin extended-status | awk '/\<Innodb_rows_updated\>/{print $4}' ;; Qcache_free_blocks) mysqladmin extended-status | awk '/\<Qcache_free_blocks\>/{print $4}' ;; Qcache_free_memory) mysqladmin extended-status | awk '/\<Qcache_free_memory\>/{print $4}' ;; Qcache_hits) mysqladmin extended-status | awk '/\<Qcache_hits\>/{print $4}' ;; Qcache_inserts) mysqladmin extended-status | awk '/\<Qcache_inserts\>/{print $4}' ;; Qcache_lowmem_prunes) mysqladmin extended-status | awk '/\<Qcache_lowmem_prunes\>/{print $4}' ;; Qcache_not_cached) mysqladmin extended-status | awk '/\<Qcache_not_cached\>/{print $4}' ;; Qcache_queries_in_cache) mysqladmin extended-status | awk '/\<Qcache_queries_in_cache\>/{print $4}' ;; Qcache_total_blocks) mysqladmin extended-status | awk '/\<Qcache_total_blocks\>/{print $4}' ;; *) echo "Please choise one optinon ( Uptime|Com_update|Slow_queries|Com_select|Com_rollback|Questions|Com_insert|Com_delete|Com_commit|Bytes_sent|Bytes_received|Com_begin ....... )" ;; esac
zabbix_agentd.conf裏面的監控項爲↓
UserParameter=mysql.version,mysqladmin -u -p version | grep 'Server version' | awk '{printf "MySQL Server Version: "$3}' UserParameter=mysql.status[*],bash /usr/local/zabbix/etc/check_mysql_status_per.sh $1 UserParameter=mysql.ping,mysqladmin -u -p status > /dev/null 2>&1 && echo 1 || echo 0
添加好後咱們能夠到zabbix上點開 Monitoring→Latest data 選擇主機
稍等就能夠完整監控了。