当前位置:   article > 正文

Zabbix 监控MySQL脚本(Python二次开发)_通过python zabbix数据推送 mysql

通过python zabbix数据推送 mysql

环境:

        操作系统版本:Rocky8.6或CentOS7.9

        MysqlServe版本:8.0

        Zabbix.Server版本:6.0LTS

        Zabbix.agent版本:Agent2

功能:

        监控数据库信息

        监控数据库状态

        可以检测主从状态

        监控指标如下所示:

                

  1. 'Key_read_requests' : 'gg',
  2. 'Key_reads' : 'gh',
  3. 'Key_write_requests' : 'gi',
  4. 'Key_writes' : 'gj',
  5. 'history_list' : 'gk',
  6. 'innodb_transactions' : 'gl',
  7. 'read_views' : 'gm',
  8. 'current_transactions' : 'gn',
  9. 'locked_transactions' : 'go',
  10. 'active_transactions' : 'gp',
  11. 'pool_size' : 'gq',
  12. 'free_pages' : 'gr',
  13. 'database_pages' : 'gs',
  14. 'modified_pages' : 'gt',
  15. 'pages_read' : 'gu',
  16. 'pages_created' : 'gv',
  17. 'pages_written' : 'gw',
  18. 'file_fsyncs' : 'gx',
  19. 'file_reads' : 'gy',
  20. 'file_writes' : 'gz',
  21. 'log_writes' : 'hg',
  22. 'pending_aio_log_ios' : 'hh',
  23. 'pending_aio_sync_ios' : 'hi',
  24. 'pending_buf_pool_flushes' : 'hj',
  25. 'pending_chkp_writes' : 'hk',
  26. 'pending_ibuf_aio_reads' : 'hl',
  27. 'pending_log_flushes' : 'hm',
  28. 'pending_log_writes' : 'hn',
  29. 'pending_normal_aio_reads' : 'ho',
  30. 'pending_normal_aio_writes' : 'hp',
  31. 'ibuf_inserts' : 'hq',
  32. 'ibuf_merged' : 'hr',
  33. 'ibuf_merges' : 'hs',
  34. 'spin_waits' : 'ht',
  35. 'spin_rounds' : 'hu',
  36. 'os_waits' : 'hv',
  37. 'rows_inserted' : 'hw',
  38. 'rows_updated' : 'hx',
  39. 'rows_deleted' : 'hy',
  40. 'rows_read' : 'hz',
  41. 'Table_locks_waited' : 'ig',
  42. 'Table_locks_immediate' : 'ih',
  43. 'Slow_queries' : 'ii',
  44. 'Open_files' : 'ij',
  45. 'Open_tables' : 'ik',
  46. 'Opened_tables' : 'il',
  47. 'innodb_open_files' : 'im',
  48. 'open_files_limit' : 'in',
  49. 'table_cache' : 'io',
  50. 'Aborted_clients' : 'ip',
  51. 'Aborted_connects' : 'iq',
  52. 'Max_used_connections' : 'ir',
  53. 'Slow_launch_threads' : 'is',
  54. 'Threads_cached' : 'it',
  55. 'Threads_connected' : 'iu',
  56. 'Threads_created' : 'iv',
  57. 'Threads_running' : 'iw',
  58. 'max_connections' : 'ix',
  59. 'thread_cache_size' : 'iy',
  60. 'Connections' : 'iz',
  61. 'slave_running' : 'jg',
  62. 'slave_stopped' : 'jh',
  63. 'Slave_retried_transactions' : 'ji',
  64. 'slave_lag' : 'jj',
  65. 'Slave_open_temp_tables' : 'jk',
  66. 'Qcache_free_blocks' : 'jl',
  67. 'Qcache_free_memory' : 'jm',
  68. 'Qcache_hits' : 'jn',
  69. 'Qcache_inserts' : 'jo',
  70. 'Qcache_lowmem_prunes' : 'jp',
  71. 'Qcache_not_cached' : 'jq',
  72. 'Qcache_queries_in_cache' : 'jr',
  73. 'Qcache_total_blocks' : 'js',
  74. 'query_cache_size' : 'jt',
  75. 'Questions' : 'ju',
  76. 'Com_update' : 'jv',
  77. 'Com_insert' : 'jw',
  78. 'Com_select' : 'jx',
  79. 'Com_delete' : 'jy',
  80. 'Com_replace' : 'jz',
  81. 'Com_load' : 'kg',
  82. 'Com_update_multi' : 'kh',
  83. 'Com_insert_select' : 'ki',
  84. 'Com_delete_multi' : 'kj',
  85. 'Com_replace_select' : 'kk',
  86. 'Select_full_join' : 'kl',
  87. 'Select_full_range_join' : 'km',
  88. 'Select_range' : 'kn',
  89. 'Select_range_check' : 'ko',
  90. 'Select_scan' : 'kp',
  91. 'Sort_merge_passes' : 'kq',
  92. 'Sort_range' : 'kr',
  93. 'Sort_rows' : 'ks',
  94. 'Sort_scan' : 'kt',
  95. 'Created_tmp_tables' : 'ku',
  96. 'Created_tmp_disk_tables' : 'kv',
  97. 'Created_tmp_files' : 'kw',
  98. 'Bytes_sent' : 'kx',
  99. 'Bytes_received' : 'ky',
  100. 'innodb_log_buffer_size' : 'kz',
  101. 'unflushed_log' : 'lg',
  102. 'log_bytes_flushed' : 'lh',
  103. 'log_bytes_written' : 'li',
  104. 'relay_log_space' : 'lj',
  105. 'binlog_cache_size' : 'lk',
  106. 'Binlog_cache_disk_use' : 'll',
  107. 'Binlog_cache_use' : 'lm',
  108. 'binary_log_space' : 'ln',
  109. 'innodb_locked_tables' : 'lo',
  110. 'innodb_lock_structs' : 'lp',
  111. 'State_closing_tables' : 'lq',
  112. 'State_copying_to_tmp_table' : 'lr',
  113. 'State_end' : 'ls',
  114. 'State_freeing_items' : 'lt',
  115. 'State_init' : 'lu',
  116. 'State_locked' : 'lv',
  117. 'State_login' : 'lw',
  118. 'State_preparing' : 'lx',
  119. 'State_reading_from_net' : 'ly',
  120. 'State_sending_data' : 'lz',
  121. 'State_sorting_result' : 'mg',
  122. 'State_statistics' : 'mh',
  123. 'State_updating' : 'mi',
  124. 'State_writing_to_net' : 'mj',
  125. 'State_none' : 'mk',
  126. 'State_other' : 'ml',
  127. 'Handler_commit' : 'mm',
  128. 'Handler_delete' : 'mn',
  129. 'Handler_discover' : 'mo',
  130. 'Handler_prepare' : 'mp',
  131. 'Handler_read_first' : 'mq',
  132. 'Handler_read_key' : 'mr',
  133. 'Handler_read_next' : 'ms',
  134. 'Handler_read_prev' : 'mt',
  135. 'Handler_read_rnd' : 'mu',
  136. 'Handler_read_rnd_next' : 'mv',
  137. 'Handler_rollback' : 'mw',
  138. 'Handler_savepoint' : 'mx',
  139. 'Handler_savepoint_rollback' : 'my',
  140. 'Handler_update' : 'mz',
  141. 'Handler_write' : 'ng',
  142. 'innodb_tables_in_use' : 'nh',
  143. 'innodb_lock_wait_secs' : 'ni',
  144. 'hash_index_cells_total' : 'nj',
  145. 'hash_index_cells_used' : 'nk',
  146. 'total_mem_alloc' : 'nl',
  147. 'additional_pool_alloc' : 'nm',
  148. 'uncheckpointed_bytes' : 'nn',
  149. 'ibuf_used_cells' : 'no',
  150. 'ibuf_free_cells' : 'np',
  151. 'ibuf_cell_count' : 'nq',
  152. 'adaptive_hash_memory' : 'nr',
  153. 'page_hash_memory' : 'ns',
  154. 'dictionary_cache_memory' : 'nt',
  155. 'file_system_memory' : 'nu',
  156. 'lock_system_memory' : 'nv',
  157. 'recovery_system_memory' : 'nw',
  158. 'thread_hash_memory' : 'nx',
  159. 'innodb_sem_waits' : 'ny',
  160. 'innodb_sem_wait_time_ms' : 'nz',
  161. 'Key_buf_bytes_unflushed' : 'og',
  162. 'Key_buf_bytes_used' : 'oh',
  163. 'key_buffer_size' : 'oi',
  164. 'Innodb_row_lock_time' : 'oj',
  165. 'Innodb_row_lock_waits' : 'ok',
  166. 'Query_time_count_00' : 'ol',
  167. 'Query_time_count_01' : 'om',
  168. 'Query_time_count_02' : 'on',
  169. 'Query_time_count_03' : 'oo',
  170. 'Query_time_count_04' : 'op',
  171. 'Query_time_count_05' : 'oq',
  172. 'Query_time_count_06' : 'or',
  173. 'Query_time_count_07' : 'os',
  174. 'Query_time_count_08' : 'ot',
  175. 'Query_time_count_09' : 'ou',
  176. 'Query_time_count_10' : 'ov',
  177. 'Query_time_count_11' : 'ow',
  178. 'Query_time_count_12' : 'ox',
  179. 'Query_time_count_13' : 'oy',
  180. 'Query_time_total_00' : 'oz',
  181. 'Query_time_total_01' : 'pg',
  182. 'Query_time_total_02' : 'ph',
  183. 'Query_time_total_03' : 'pi',
  184. 'Query_time_total_04' : 'pj',
  185. 'Query_time_total_05' : 'pk',
  186. 'Query_time_total_06' : 'pl',
  187. 'Query_time_total_07' : 'pm',
  188. 'Query_time_total_08' : 'pn',
  189. 'Query_time_total_09' : 'po',
  190. 'Query_time_total_10' : 'pp',
  191. 'Query_time_total_11' : 'pq',
  192. 'Query_time_total_12' : 'pr',
  193. 'Query_time_total_13' : 'ps',
  194. 'wsrep_replicated_bytes' : 'pt',
  195. 'wsrep_received_bytes' : 'pu',
  196. 'wsrep_replicated' : 'pv',
  197. 'wsrep_received' : 'pw',
  198. 'wsrep_local_cert_failures' : 'px',
  199. 'wsrep_local_bf_aborts' : 'py',
  200. 'wsrep_local_send_queue' : 'pz',
  201. 'wsrep_local_recv_queue' : 'qg',
  202. 'wsrep_cluster_size' : 'qh',
  203. 'wsrep_cert_deps_distance' : 'qi',
  204. 'wsrep_apply_window' : 'qj',
  205. 'wsrep_commit_window' : 'qk',
  206. 'wsrep_flow_control_paused' : 'ql',
  207. 'wsrep_flow_control_sent' : 'qm',
  208. 'wsrep_flow_control_recv' : 'qn',
  209. 'pool_reads' : 'qo',
  210. 'pool_read_requests' : 'qp',

安装步骤

1)安装

备注:预先装载好Zabbix-Server(我之前教程里有)

1)安装Zabbix.Agent2(参考网址)Download and install Zabbix 6.0 LTS for Rocky Linux 8, MySQL, Nginxicon-default.png?t=MBR7https://www.zabbix.com/download?zabbix=6.0&os_distribution=rocky_linux&os_version=8&components=server_frontend_agent&db=mysql&ws=nginx

  1. # rpm -Uvh https://repo.zabbix.com/zabbix/6.0/rhel/8/x86_64/zabbix-release-6.0-4.el8.noarch.rpm
  2. # dnf clean all
  3. # dnf install zabbix-server-mysql zabbix-web-mysql zabbix-nginx-conf zabbix-sql-scripts zabbix-selinux-policy zabbix-agent
  4. # mysql -uroot -p
  5. password
  6. mysql> create database zabbix character set utf8mb4 collate utf8mb4_bin;
  7. mysql> create user zabbix@localhost identified by 'password';
  8. mysql> grant all privileges on zabbix.* to zabbix@localhost;
  9. mysql> set global log_bin_trust_function_creators = 1;
  10. mysql> quit;
  11. # zcat /usr/share/zabbix-sql-scripts/mysql/server.sql.gz | mysql --default-character-set=utf8mb4 -uzabbix -p zabbix
  12. # mysql -uroot -p
  13. password
  14. mysql> set global log_bin_trust_function_creators = 0;
  15. mysql> quit;
  16. DBPassword=password
  17. # listen 8080;
  18. # server_name example.com;
  19. # systemctl restart zabbix-server zabbix-agent nginx php-fpm
  20. # systemctl enable zabbix-server zabbix-agent nginx php-fpm

2)编写配置文件(注:可以只加需要的选项没有必要都加)

  1. UserParameter=MySQL.Sort-scan,python3 /usr/local/src/MySQL_Zabbix.py kt
  2. UserParameter=MySQL.slave-stopped,python3 /usr/local/src/MySQL_Zabbix.py jh
  3. UserParameter=MySQL.Com-replace,python3 /usr/local/src/MySQL_Zabbix.py jz
  4. UserParameter=MySQL.innodb-lock-structs,python3 /usr/local/src/MySQL_Zabbix.py lp
  5. UserParameter=MySQL.Com-load,python3 /usr/local/src/MySQL_Zabbix.py kg
  6. UserParameter=MySQL.State-updating,python3 /usr/local/src/MySQL_Zabbix.py mi
  7. UserParameter=MySQL.Aborted-clients,python3 /usr/local/src/MySQL_Zabbix.py ip
  8. UserParameter=MySQL.innodb-lock-wait-secs,python3 /usr/local/src/MySQL_Zabbix.py ni
  9. UserParameter=MySQL.Handler-read-key,python3 /usr/local/src/MySQL_Zabbix.py mr
  10. UserParameter=MySQL.file-reads,python3 /usr/local/src/MySQL_Zabbix.py gy
  11. UserParameter=MySQL.Query-time-count-12,python3 /usr/local/src/MySQL_Zabbix.py ox
  12. UserParameter=MySQL.relay-log-space,python3 /usr/local/src/MySQL_Zabbix.py lj
  13. UserParameter=MySQL.Threads-connected,python3 /usr/local/src/MySQL_Zabbix.py iu
  14. UserParameter=MySQL.Qcache-lowmem-prunes,python3 /usr/local/src/MySQL_Zabbix.py jp
  15. UserParameter=MySQL.Binlog-cache-use,python3 /usr/local/src/MySQL_Zabbix.py lm
  16. UserParameter=MySQL.State-freeing-items,python3 /usr/local/src/MySQL_Zabbix.py lt
  17. UserParameter=MySQL.Query-time-count-10,python3 /usr/local/src/MySQL_Zabbix.py ov
  18. UserParameter=MySQL.read-views,python3 /usr/local/src/MySQL_Zabbix.py gm
  19. UserParameter=MySQL.Bytes-received,python3 /usr/local/src/MySQL_Zabbix.py ky
  20. UserParameter=MySQL.os-waits,python3 /usr/local/src/MySQL_Zabbix.py hv
  21. UserParameter=MySQL.Handler-commit,python3 /usr/local/src/MySQL_Zabbix.py mm
  22. UserParameter=MySQL.Com-select,python3 /usr/local/src/MySQL_Zabbix.py jx
  23. UserParameter=MySQL.Qcache-total-blocks,python3 /usr/local/src/MySQL_Zabbix.py js
  24. UserParameter=MySQL.Handler-read-prev,python3 /usr/local/src/MySQL_Zabbix.py mt
  25. UserParameter=MySQL.Sort-rows,python3 /usr/local/src/MySQL_Zabbix.py ks
  26. UserParameter=MySQL.Qcache-free-memory,python3 /usr/local/src/MySQL_Zabbix.py jm
  27. UserParameter=MySQL.pages-read,python3 /usr/local/src/MySQL_Zabbix.py gu
  28. UserParameter=MySQL.Key-read-requests,python3 /usr/local/src/MySQL_Zabbix.py gg
  29. UserParameter=MySQL.State-other,python3 /usr/local/src/MySQL_Zabbix.py ml
  30. UserParameter=MySQL.Qcache-inserts,python3 /usr/local/src/MySQL_Zabbix.py jo
  31. UserParameter=MySQL.State-none,python3 /usr/local/src/MySQL_Zabbix.py mk
  32. UserParameter=MySQL.pending-normal-aio-writes,python3 /usr/local/src/MySQL_Zabbix.py hp
  33. UserParameter=MySQL.hash-index-cells-total,python3 /usr/local/src/MySQL_Zabbix.py nj
  34. UserParameter=MySQL.pool-size,python3 /usr/local/src/MySQL_Zabbix.py gq
  35. UserParameter=MySQL.pending-ibuf-aio-reads,python3 /usr/local/src/MySQL_Zabbix.py hl
  36. UserParameter=MySQL.Handler-write,python3 /usr/local/src/MySQL_Zabbix.py ng
  37. UserParameter=MySQL.innodb-sem-waits,python3 /usr/local/src/MySQL_Zabbix.py ny
  38. UserParameter=MySQL.Handler-savepoint-rollback,python3 /usr/local/src/MySQL_Zabbix.py my
  39. UserParameter=MySQL.Query-time-total-01,python3 /usr/local/src/MySQL_Zabbix.py pg
  40. UserParameter=MySQL.Query-time-total-00,python3 /usr/local/src/MySQL_Zabbix.py oz
  41. UserParameter=MySQL.Table-locks-waited,python3 /usr/local/src/MySQL_Zabbix.py ig
  42. UserParameter=MySQL.Handler-rollback,python3 /usr/local/src/MySQL_Zabbix.py mw
  43. UserParameter=MySQL.unflushed-log,python3 /usr/local/src/MySQL_Zabbix.py lg
  44. UserParameter=MySQL.Query-time-total-04,python3 /usr/local/src/MySQL_Zabbix.py pj
  45. UserParameter=MySQL.Query-time-total-07,python3 /usr/local/src/MySQL_Zabbix.py pm
  46. UserParameter=MySQL.Handler-savepoint,python3 /usr/local/src/MySQL_Zabbix.py mx
  47. UserParameter=MySQL.Query-time-total-09,python3 /usr/local/src/MySQL_Zabbix.py po
  48. UserParameter=MySQL.Query-time-total-08,python3 /usr/local/src/MySQL_Zabbix.py pn
  49. UserParameter=MySQL.Select-range-check,python3 /usr/local/src/MySQL_Zabbix.py ko
  50. UserParameter=MySQL.Threads-running,python3 /usr/local/src/MySQL_Zabbix.py iw
  51. UserParameter=MySQL.State-init,python3 /usr/local/src/MySQL_Zabbix.py lu
  52. UserParameter=MySQL.Aborted-connects,python3 /usr/local/src/MySQL_Zabbix.py iq
  53. UserParameter=MySQL.Handler-read-first,python3 /usr/local/src/MySQL_Zabbix.py mq
  54. UserParameter=MySQL.Created-tmp-tables,python3 /usr/local/src/MySQL_Zabbix.py ku
  55. UserParameter=MySQL.Created-tmp-disk-tables,python3 /usr/local/src/MySQL_Zabbix.py kv
  56. UserParameter=MySQL.Select-full-range-join,python3 /usr/local/src/MySQL_Zabbix.py km
  57. UserParameter=MySQL.Connections,python3 /usr/local/src/MySQL_Zabbix.py iz
  58. UserParameter=MySQL.Com-insert,python3 /usr/local/src/MySQL_Zabbix.py jw
  59. UserParameter=MySQL.Query-time-total-11,python3 /usr/local/src/MySQL_Zabbix.py pq
  60. UserParameter=MySQL.innodb-transactions,python3 /usr/local/src/MySQL_Zabbix.py gl
  61. UserParameter=MySQL.State-sorting-result,python3 /usr/local/src/MySQL_Zabbix.py mg
  62. UserParameter=MySQL.State-statistics,python3 /usr/local/src/MySQL_Zabbix.py mh
  63. UserParameter=MySQL.innodb-locked-tables,python3 /usr/local/src/MySQL_Zabbix.py lo
  64. UserParameter=MySQL.log-bytes-written,python3 /usr/local/src/MySQL_Zabbix.py li
  65. UserParameter=MySQL.innodb-log-buffer-size,python3 /usr/local/src/MySQL_Zabbix.py kz
  66. UserParameter=MySQL.Select-full-join,python3 /usr/local/src/MySQL_Zabbix.py kl
  67. UserParameter=MySQL.locked-transactions,python3 /usr/local/src/MySQL_Zabbix.py go
  68. UserParameter=MySQL.Handler-read-rnd,python3 /usr/local/src/MySQL_Zabbix.py mu
  69. UserParameter=MySQL.Handler-delete,python3 /usr/local/src/MySQL_Zabbix.py mn
  70. UserParameter=MySQL.Query-time-total-13,python3 /usr/local/src/MySQL_Zabbix.py ps
  71. UserParameter=MySQL.Query-time-total-10,python3 /usr/local/src/MySQL_Zabbix.py pp
  72. UserParameter=MySQL.Key-buf-bytes-used,python3 /usr/local/src/MySQL_Zabbix.py oh
  73. UserParameter=MySQL.Com-delete-multi,python3 /usr/local/src/MySQL_Zabbix.py kj
  74. UserParameter=MySQL.Select-range,python3 /usr/local/src/MySQL_Zabbix.py kn
  75. UserParameter=MySQL.pending-aio-log-ios,python3 /usr/local/src/MySQL_Zabbix.py hh
  76. UserParameter=MySQL.ibuf-inserts,python3 /usr/local/src/MySQL_Zabbix.py hq
  77. UserParameter=MySQL.State-copying-to-tmp-table,python3 /usr/local/src/MySQL_Zabbix.py lr
  78. UserParameter=MySQL.Com-replace-select,python3 /usr/local/src/MySQL_Zabbix.py kk
  79. UserParameter=MySQL.modified-pages,python3 /usr/local/src/MySQL_Zabbix.py gt
  80. UserParameter=MySQL.Com-delete,python3 /usr/local/src/MySQL_Zabbix.py jy
  81. UserParameter=MySQL.Threads-cached,python3 /usr/local/src/MySQL_Zabbix.py it
  82. UserParameter=MySQL.hash-index-cells-used,python3 /usr/local/src/MySQL_Zabbix.py nk
  83. UserParameter=MySQL.uncheckpointed-bytes,python3 /usr/local/src/MySQL_Zabbix.py nn
  84. UserParameter=MySQL.Query-time-total-12,python3 /usr/local/src/MySQL_Zabbix.py pr
  85. UserParameter=MySQL.Qcache-hits,python3 /usr/local/src/MySQL_Zabbix.py jn
  86. UserParameter=MySQL.Questions,python3 /usr/local/src/MySQL_Zabbix.py ju
  87. UserParameter=MySQL.Qcache-queries-in-cache,python3 /usr/local/src/MySQL_Zabbix.py jr
  88. UserParameter=MySQL.key-buffer-size,python3 /usr/local/src/MySQL_Zabbix.py oi
  89. UserParameter=MySQL.total-mem-alloc,python3 /usr/local/src/MySQL_Zabbix.py nl
  90. UserParameter=MySQL.spin-rounds,python3 /usr/local/src/MySQL_Zabbix.py hu
  91. UserParameter=MySQL.ibuf-merged,python3 /usr/local/src/MySQL_Zabbix.py hr
  92. UserParameter=MySQL.rows-inserted,python3 /usr/local/src/MySQL_Zabbix.py hw
  93. UserParameter=MySQL.file-fsyncs,python3 /usr/local/src/MySQL_Zabbix.py gx
  94. UserParameter=MySQL.Bytes-sent,python3 /usr/local/src/MySQL_Zabbix.py kx
  95. UserParameter=MySQL.Query-time-total-03,python3 /usr/local/src/MySQL_Zabbix.py pi
  96. UserParameter=MySQL.ibuf-merges,python3 /usr/local/src/MySQL_Zabbix.py hs
  97. UserParameter=MySQL.Query-time-total-02,python3 /usr/local/src/MySQL_Zabbix.py ph
  98. UserParameter=MySQL.pool-reads,python3 /usr/local/src/MySQL_Zabbix.py qo
  99. UserParameter=MySQL.history-list,python3 /usr/local/src/MySQL_Zabbix.py gk
  100. UserParameter=MySQL.Query-time-total-05,python3 /usr/local/src/MySQL_Zabbix.py pk
  101. UserParameter=MySQL.rows-updated,python3 /usr/local/src/MySQL_Zabbix.py hx
  102. UserParameter=MySQL.max-connections,python3 /usr/local/src/MySQL_Zabbix.py ix
  103. UserParameter=MySQL.free-pages,python3 /usr/local/src/MySQL_Zabbix.py gr
  104. UserParameter=MySQL.Select-scan,python3 /usr/local/src/MySQL_Zabbix.py kp
  105. UserParameter=MySQL.pending-aio-sync-ios,python3 /usr/local/src/MySQL_Zabbix.py hi
  106. UserParameter=MySQL.recovery-system-memory,python3 /usr/local/src/MySQL_Zabbix.py nw
  107. UserParameter=MySQL.Query-time-total-06,python3 /usr/local/src/MySQL_Zabbix.py pl
  108. UserParameter=MySQL.innodb-sem-wait-time-ms,python3 /usr/local/src/MySQL_Zabbix.py nz
  109. UserParameter=MySQL.thread-hash-memory,python3 /usr/local/src/MySQL_Zabbix.py nx
  110. UserParameter=MySQL.dictionary-cache-memory,python3 /usr/local/src/MySQL_Zabbix.py nt
  111. UserParameter=MySQL.ibuf-used-cells,python3 /usr/local/src/MySQL_Zabbix.py no
  112. UserParameter=MySQL.State-end,python3 /usr/local/src/MySQL_Zabbix.py ls
  113. UserParameter=MySQL.slave-running,python3 /usr/local/src/MySQL_Zabbix.py jg
  114. UserParameter=MySQL.pending-normal-aio-reads,python3 /usr/local/src/MySQL_Zabbix.py ho
  115. UserParameter=MySQL.Innodb-row-lock-waits,python3 /usr/local/src/MySQL_Zabbix.py ok
  116. UserParameter=MySQL.active-transactions,python3 /usr/local/src/MySQL_Zabbix.py gp
  117. UserParameter=MySQL.Sort-range,python3 /usr/local/src/MySQL_Zabbix.py kr
  118. UserParameter=MySQL.spin-waits,python3 /usr/local/src/MySQL_Zabbix.py ht
  119. UserParameter=MySQL.Slow-queries,python3 /usr/local/src/MySQL_Zabbix.py ii
  120. UserParameter=MySQL.ibuf-cell-count,python3 /usr/local/src/MySQL_Zabbix.py nq
  121. UserParameter=MySQL.Qcache-free-blocks,python3 /usr/local/src/MySQL_Zabbix.py jl
  122. UserParameter=MySQL.Sort-merge-passes,python3 /usr/local/src/MySQL_Zabbix.py kq
  123. UserParameter=MySQL.thread-cache-size,python3 /usr/local/src/MySQL_Zabbix.py iy
  124. UserParameter=MySQL.Key-write-requests,python3 /usr/local/src/MySQL_Zabbix.py gi
  125. UserParameter=MySQL.pending-buf-pool-flushes,python3 /usr/local/src/MySQL_Zabbix.py hj
  126. UserParameter=MySQL.pending-log-writes,python3 /usr/local/src/MySQL_Zabbix.py hn
  127. UserParameter=MySQL.Com-update-multi,python3 /usr/local/src/MySQL_Zabbix.py kh
  128. UserParameter=MySQL.State-login,python3 /usr/local/src/MySQL_Zabbix.py lw
  129. UserParameter=MySQL.State-reading-from-net,python3 /usr/local/src/MySQL_Zabbix.py ly
  130. UserParameter=MySQL.State-locked,python3 /usr/local/src/MySQL_Zabbix.py lv
  131. UserParameter=MySQL.log-bytes-flushed,python3 /usr/local/src/MySQL_Zabbix.py lh
  132. UserParameter=MySQL.ibuf-free-cells,python3 /usr/local/src/MySQL_Zabbix.py np
  133. UserParameter=MySQL.Qcache-not-cached,python3 /usr/local/src/MySQL_Zabbix.py jq
  134. UserParameter=MySQL.pending-log-flushes,python3 /usr/local/src/MySQL_Zabbix.py hm
  135. UserParameter=MySQL.Max-used-connections,python3 /usr/local/src/MySQL_Zabbix.py ir
  136. UserParameter=MySQL.State-sending-data,python3 /usr/local/src/MySQL_Zabbix.py lz
  137. UserParameter=MySQL.rows-read,python3 /usr/local/src/MySQL_Zabbix.py hz
  138. UserParameter=MySQL.lock-system-memory,python3 /usr/local/src/MySQL_Zabbix.py nv
  139. UserParameter=MySQL.Handler-read-rnd-next,python3 /usr/local/src/MySQL_Zabbix.py mv
  140. UserParameter=MySQL.table-cache,python3 /usr/local/src/MySQL_Zabbix.py io
  141. UserParameter=MySQL.rows-deleted,python3 /usr/local/src/MySQL_Zabbix.py hy
  142. UserParameter=MySQL.file-system-memory,python3 /usr/local/src/MySQL_Zabbix.py nu
  143. UserParameter=MySQL.file-writes,python3 /usr/local/src/MySQL_Zabbix.py gz
  144. UserParameter=MySQL.pending-chkp-writes,python3 /usr/local/src/MySQL_Zabbix.py hk
  145. UserParameter=MySQL.additional-pool-alloc,python3 /usr/local/src/MySQL_Zabbix.py nm
  146. UserParameter=MySQL.current-transactions,python3 /usr/local/src/MySQL_Zabbix.py gn
  147. UserParameter=MySQL.Key-reads,python3 /usr/local/src/MySQL_Zabbix.py gh
  148. UserParameter=MySQL.Handler-read-next,python3 /usr/local/src/MySQL_Zabbix.py ms
  149. UserParameter=MySQL.Key-writes,python3 /usr/local/src/MySQL_Zabbix.py gj
  150. UserParameter=MySQL.Query-time-count-01,python3 /usr/local/src/MySQL_Zabbix.py om
  151. UserParameter=MySQL.pool-read-requests,python3 /usr/local/src/MySQL_Zabbix.py qp
  152. UserParameter=MySQL.Open-tables,python3 /usr/local/src/MySQL_Zabbix.py ik
  153. UserParameter=MySQL.Query-time-count-13,python3 /usr/local/src/MySQL_Zabbix.py oy
  154. UserParameter=MySQL.Com-insert-select,python3 /usr/local/src/MySQL_Zabbix.py ki
  155. UserParameter=MySQL.Query-time-count-11,python3 /usr/local/src/MySQL_Zabbix.py ow
  156. UserParameter=MySQL.Query-time-count-03,python3 /usr/local/src/MySQL_Zabbix.py oo
  157. UserParameter=MySQL.slave-lag,python3 /usr/local/src/MySQL_Zabbix.py jj
  158. UserParameter=MySQL.Handler-update,python3 /usr/local/src/MySQL_Zabbix.py mz
  159. UserParameter=MySQL.Created-tmp-files,python3 /usr/local/src/MySQL_Zabbix.py kw
  160. UserParameter=MySQL.Key-buf-bytes-unflushed,python3 /usr/local/src/MySQL_Zabbix.py og
  161. UserParameter=MySQL.State-preparing,python3 /usr/local/src/MySQL_Zabbix.py lx
  162. UserParameter=MySQL.Binlog-cache-disk-use,python3 /usr/local/src/MySQL_Zabbix.py ll
  163. UserParameter=MySQL.Slave-open-temp-tables,python3 /usr/local/src/MySQL_Zabbix.py jk
  164. UserParameter=MySQL.innodb-tables-in-use,python3 /usr/local/src/MySQL_Zabbix.py nh
  165. UserParameter=MySQL.Threads-created,python3 /usr/local/src/MySQL_Zabbix.py iv
  166. UserParameter=MySQL.Slave-retried-transactions,python3 /usr/local/src/MySQL_Zabbix.py ji
  167. UserParameter=MySQL.State-writing-to-net,python3 /usr/local/src/MySQL_Zabbix.py mj
  168. UserParameter=MySQL.pages-created,python3 /usr/local/src/MySQL_Zabbix.py gv
  169. UserParameter=MySQL.Opened-tables,python3 /usr/local/src/MySQL_Zabbix.py il
  170. UserParameter=MySQL.pages-written,python3 /usr/local/src/MySQL_Zabbix.py gw
  171. UserParameter=MySQL.database-pages,python3 /usr/local/src/MySQL_Zabbix.py gs
  172. UserParameter=MySQL.query-cache-size,python3 /usr/local/src/MySQL_Zabbix.py jt
  173. UserParameter=MySQL.page-hash-memory,python3 /usr/local/src/MySQL_Zabbix.py ns
  174. UserParameter=MySQL.Innodb-row-lock-time,python3 /usr/local/src/MySQL_Zabbix.py oj
  175. UserParameter=MySQL.Table-locks-immediate,python3 /usr/local/src/MySQL_Zabbix.py ih
  176. UserParameter=MySQL.binary-log-space,python3 /usr/local/src/MySQL_Zabbix.py ln
  177. UserParameter=MySQL.Com-update,python3 /usr/local/src/MySQL_Zabbix.py jv
  178. UserParameter=MySQL.Query-time-count-00,python3 /usr/local/src/MySQL_Zabbix.py ol
  179. UserParameter=MySQL.adaptive-hash-memory,python3 /usr/local/src/MySQL_Zabbix.py nr
  180. UserParameter=MySQL.Query-time-count-02,python3 /usr/local/src/MySQL_Zabbix.py on
  181. UserParameter=MySQL.log-writes,python3 /usr/local/src/MySQL_Zabbix.py hg
  182. UserParameter=MySQL.Query-time-count-04,python3 /usr/local/src/MySQL_Zabbix.py op
  183. UserParameter=MySQL.Query-time-count-05,python3 /usr/local/src/MySQL_Zabbix.py oq
  184. UserParameter=MySQL.Query-time-count-06,python3 /usr/local/src/MySQL_Zabbix.py or
  185. UserParameter=MySQL.Query-time-count-07,python3 /usr/local/src/MySQL_Zabbix.py os
  186. UserParameter=MySQL.Query-time-count-08,python3 /usr/local/src/MySQL_Zabbix.py ot
  187. UserParameter=MySQL.Query-time-count-09,python3 /usr/local/src/MySQL_Zabbix.py ou
  188. UserParameter=MySQL.Open-files,python3 /usr/local/src/MySQL_Zabbix.py ij
  189. UserParameter=MySQL.State-closing-tables,python3 /usr/local/src/MySQL_Zabbix.py lq
  190. UserParameter=MySQL.running-slave,python3 /usr/local/src/MySQL_Zabbix.py running-slave

3)编写Python脚本

  1. #vim MySQL_Zabbix.py
  2. #!/usr/bin/python3
  3. #
  4. #****************************************************
  5. #Author: caojidong
  6. #QQ: 1549396190
  7. #Date: 2022-2-31
  8. #FileName: MySQL_Zabbix.py
  9. #cell-phone number: 13739548267
  10. #Description: test
  11. #Copyright(C): 2022 All right
  12. #***************************************************
  13. #!/bin/bash env
  14. #_*_coding:utf8_*_
  15. import MySQLdb
  16. import re
  17. import sys
  18. import json
  19. import os
  20. import datetime
  21. import time
  22. mysql_user = 'root' #数据库的根用户
  23. mysql_pass = 'xxxxx' #用户密码
  24. mysql_host = 'xxxxxxxxx'#mysql所在服务器的ip
  25. mysql_port = 3306 #端口号
  26. mysql_socket = "/tmp.msyql.sock"
  27. mysql_flags = 0;
  28. mysql_ssl = False # Whether to use SSL to connect to MySQL.
  29. mysql_ssl_key = '/etc/pki/tls/certs/mysql/client-key.pem'
  30. mysql_ssl_cert = '/etc/pki/tls/certs/mysql/client-cert.pem'
  31. mysql_ssl_ca = '/etc/pki/tls/certs/mysql/ca-cert.pem'
  32. mysql_connection_timeout = 5
  33. heartbeat = False
  34. heartbeat_utc = False
  35. heartbeat_table = 'percona.heartbeat'
  36. heartbeat_server_id = 0
  37. cache_dir = '/tmp'
  38. poll_time = 300
  39. timezone = None
  40. chk_options = {
  41. 'innodb' : True,
  42. 'master' : True,
  43. 'slave' : True,
  44. 'procs' : True,
  45. 'get_qrt' : True,
  46. }
  47. use_ss = False
  48. debug = False
  49. debug_log = False
  50. version = '8.0' #MySQL的版本号
  51. def ss_get_mysql_stats(*args, **kwargs):
  52. global debug, mysql_user, mysql_pass, cache_dir, poll_time, chk_options
  53. global mysql_port, mysql_socket, mysql_flags
  54. global mysql_ssl, mysql_ssl_key, mysql_ssl_cert, mysql_ssl_ca
  55. global mysql_connection_timeout
  56. global heartbeat, heartbeat_table, heartbeat_server_id, heartbeat_utc
  57. user= kwargs.get('user') if kwargs.has_key('user') else mysql_user
  58. passwd = kwargs.get('passwd') if kwargs.has_key('passwd') else mysql_pass
  59. host= kwargs.get('host') if kwargs.has_key('host') else mysql_host
  60. port = kwargs.get('port') if kwargs.has_key('port') else mysql_port
  61. socket = kwargs.get('socket') if kwargs.has_key('socket') else mysql_socket
  62. flags = kwargs.get('flags') if kwargs.has_key('flags') else mysql_flags
  63. connection_timeout = kwargs.get('connection-timeout') if kwargs.has_key('connection-timeout') else mysql_connection_timeout
  64. heartbeat_server_id = kwargs.get('heartbeat_server_id') if kwargs.has_key('heartbeat_server_id') else heartbeat_server_id
  65. sanitized_host= host.replace(":","/").replace("","_")
  66. cache_file = "/tmp/mysql_zabbix_monitor_cache"
  67. fp =None
  68. if cache_dir and not kwargs.has_key('nocache'):
  69. if not os.path.exists(cache_file):
  70. fp = open(cache_file,'wb',)
  71. else:
  72. cache_file_ctime = os.stat(cache_file).st_ctime
  73. current_time = time.time()
  74. if int(current_time - cache_file_ctime) < 300:
  75. fp = open(cache_file,'rb')
  76. output_dic = json.load(fp)
  77. return output_dic
  78. else:
  79. fp = open(cache_file,'wb')
  80. else:
  81. fp = None
  82. #connect to MySQL
  83. conn = MySQLdb.Connect(host=host,port=port,user=user,passwd=passwd,unix_socket=mysql_socket)
  84. cursor = conn.cursor()
  85. """
  86. //ssl 相关设定
  87. 获取mysql版本信息
  88. """
  89. #设置变量
  90. status = {
  91. 'relay_log_space' : 0 ,
  92. 'binary_log_space' : 0,
  93. 'current_transactions' : 0,
  94. 'locked_transactions' : 0,
  95. 'active_transactions' : 0,
  96. 'innodb_locked_tables' : 0,
  97. 'innodb_tables_in_use' : 0,
  98. 'innodb_lock_structs' : 0,
  99. 'innodb_lock_wait_secs' : 0,
  100. 'innodb_sem_waits' : 0,
  101. 'innodb_sem_wait_time_ms' : 0,
  102. #
  103. 'State_closing_tables' : 0,
  104. 'State_copying_to_tmp_table' : 0,
  105. 'State_end' : 0,
  106. 'State_freeing_items' : 0,
  107. 'State_init' : 0,
  108. 'State_locked' : 0,
  109. 'State_login' : 0,
  110. 'State_preparing' : 0,
  111. 'State_reading_from_net' : 0,
  112. 'State_sending_data' : 0,
  113. 'State_sorting_result' : 0,
  114. 'State_statistics' : 0,
  115. 'State_updating' : 0,
  116. 'State_writing_to_net' : 0,
  117. 'State_none' : 0,
  118. 'State_other' : 0,
  119. }
  120. # Get SHOW STATUS and convert the name-value array into a simple
  121. # associative array.
  122. cursor.execute("SHOW /*!50002 GLOBAL */ STATUS")
  123. for item in cursor.fetchall():
  124. status[item[0]] = item[1]
  125. # Get SHOW VARIABLES and do the same thing, adding it to the $status array.
  126. cursor.execute('SHOW VARIABLES')
  127. for item in cursor.fetchall():
  128. status[item[0]] = item[1]
  129. #监控mysql主从状态
  130. if chk_options['master']:
  131. pass
  132. #返回的不是字典,根据自己实际情况调整
  133. if chk_options['procs']:
  134. cursor.execute('SHOW PROCESSLIST')
  135. for item in cursor.fetchall():
  136. state = item[6] if item[6] else None
  137. engines = {}
  138. cursor.execute("SHOW ENGINES")
  139. for item in cursor.fetchall():
  140. engines[item[0]] = item[1]
  141. if chk_options.get('innodb') and engines.has_key('InnoDB') and engines['InnoDB'] == 'DEFAULT':
  142. cursor.execute("SHOW /*!50000 ENGINE*/ INNODB STATUS")
  143. innodb_status_txt = cursor.fetchall()[0][2]
  144. result = get_innodb_array(innodb_status_txt,None)
  145. for item in result.keys():
  146. status[item] = result.get(item)
  147. if status.get('table_open_cache'):
  148. status['table_cache'] = status.get('table_open_cache')
  149. status['Key_buf_bytes_used'] = big_sub(status['key_buffer_size'],
  150. big_multiply(status['Key_blocks_unused'],status['key_cache_block_size']))
  151. status['Key_buf_bytes_unflushed'] = big_multiply(status['Key_blocks_not_flushed'],
  152. status['key_cache_block_size'])
  153. if status.has_key('unflushed_log') and status.get('unflushed_log'):
  154. status['unflushed_log'] = max(status['unflushed_log'],status['innodb_log_buffer_size'])
  155. keys = {
  156. 'Key_read_requests' : 'gg',
  157. 'Key_reads' : 'gh',
  158. 'Key_write_requests' : 'gi',
  159. 'Key_writes' : 'gj',
  160. 'history_list' : 'gk',
  161. 'innodb_transactions' : 'gl',
  162. 'read_views' : 'gm',
  163. 'current_transactions' : 'gn',
  164. 'locked_transactions' : 'go',
  165. 'active_transactions' : 'gp',
  166. 'pool_size' : 'gq',
  167. 'free_pages' : 'gr',
  168. 'database_pages' : 'gs',
  169. 'modified_pages' : 'gt',
  170. 'pages_read' : 'gu',
  171. 'pages_created' : 'gv',
  172. 'pages_written' : 'gw',
  173. 'file_fsyncs' : 'gx',
  174. 'file_reads' : 'gy',
  175. 'file_writes' : 'gz',
  176. 'log_writes' : 'hg',
  177. 'pending_aio_log_ios' : 'hh',
  178. 'pending_aio_sync_ios' : 'hi',
  179. 'pending_buf_pool_flushes' : 'hj',
  180. 'pending_chkp_writes' : 'hk',
  181. 'pending_ibuf_aio_reads' : 'hl',
  182. 'pending_log_flushes' : 'hm',
  183. 'pending_log_writes' : 'hn',
  184. 'pending_normal_aio_reads' : 'ho',
  185. 'pending_normal_aio_writes' : 'hp',
  186. 'ibuf_inserts' : 'hq',
  187. 'ibuf_merged' : 'hr',
  188. 'ibuf_merges' : 'hs',
  189. 'spin_waits' : 'ht',
  190. 'spin_rounds' : 'hu',
  191. 'os_waits' : 'hv',
  192. 'rows_inserted' : 'hw',
  193. 'rows_updated' : 'hx',
  194. 'rows_deleted' : 'hy',
  195. 'rows_read' : 'hz',
  196. 'Table_locks_waited' : 'ig',
  197. 'Table_locks_immediate' : 'ih',
  198. 'Slow_queries' : 'ii',
  199. 'Open_files' : 'ij',
  200. 'Open_tables' : 'ik',
  201. 'Opened_tables' : 'il',
  202. 'innodb_open_files' : 'im',
  203. 'open_files_limit' : 'in',
  204. 'table_cache' : 'io',
  205. 'Aborted_clients' : 'ip',
  206. 'Aborted_connects' : 'iq',
  207. 'Max_used_connections' : 'ir',
  208. 'Slow_launch_threads' : 'is',
  209. 'Threads_cached' : 'it',
  210. 'Threads_connected' : 'iu',
  211. 'Threads_created' : 'iv',
  212. 'Threads_running' : 'iw',
  213. 'max_connections' : 'ix',
  214. 'thread_cache_size' : 'iy',
  215. 'Connections' : 'iz',
  216. 'slave_running' : 'jg',
  217. 'slave_stopped' : 'jh',
  218. 'Slave_retried_transactions' : 'ji',
  219. 'slave_lag' : 'jj',
  220. 'Slave_open_temp_tables' : 'jk',
  221. 'Qcache_free_blocks' : 'jl',
  222. 'Qcache_free_memory' : 'jm',
  223. 'Qcache_hits' : 'jn',
  224. 'Qcache_inserts' : 'jo',
  225. 'Qcache_lowmem_prunes' : 'jp',
  226. 'Qcache_not_cached' : 'jq',
  227. 'Qcache_queries_in_cache' : 'jr',
  228. 'Qcache_total_blocks' : 'js',
  229. 'query_cache_size' : 'jt',
  230. 'Questions' : 'ju',
  231. 'Com_update' : 'jv',
  232. 'Com_insert' : 'jw',
  233. 'Com_select' : 'jx',
  234. 'Com_delete' : 'jy',
  235. 'Com_replace' : 'jz',
  236. 'Com_load' : 'kg',
  237. 'Com_update_multi' : 'kh',
  238. 'Com_insert_select' : 'ki',
  239. 'Com_delete_multi' : 'kj',
  240. 'Com_replace_select' : 'kk',
  241. 'Select_full_join' : 'kl',
  242. 'Select_full_range_join' : 'km',
  243. 'Select_range' : 'kn',
  244. 'Select_range_check' : 'ko',
  245. 'Select_scan' : 'kp',
  246. 'Sort_merge_passes' : 'kq',
  247. 'Sort_range' : 'kr',
  248. 'Sort_rows' : 'ks',
  249. 'Sort_scan' : 'kt',
  250. 'Created_tmp_tables' : 'ku',
  251. 'Created_tmp_disk_tables' : 'kv',
  252. 'Created_tmp_files' : 'kw',
  253. 'Bytes_sent' : 'kx',
  254. 'Bytes_received' : 'ky',
  255. 'innodb_log_buffer_size' : 'kz',
  256. 'unflushed_log' : 'lg',
  257. 'log_bytes_flushed' : 'lh',
  258. 'log_bytes_written' : 'li',
  259. 'relay_log_space' : 'lj',
  260. 'binlog_cache_size' : 'lk',
  261. 'Binlog_cache_disk_use' : 'll',
  262. 'Binlog_cache_use' : 'lm',
  263. 'binary_log_space' : 'ln',
  264. 'innodb_locked_tables' : 'lo',
  265. 'innodb_lock_structs' : 'lp',
  266. 'State_closing_tables' : 'lq',
  267. 'State_copying_to_tmp_table' : 'lr',
  268. 'State_end' : 'ls',
  269. 'State_freeing_items' : 'lt',
  270. 'State_init' : 'lu',
  271. 'State_locked' : 'lv',
  272. 'State_login' : 'lw',
  273. 'State_preparing' : 'lx',
  274. 'State_reading_from_net' : 'ly',
  275. 'State_sending_data' : 'lz',
  276. 'State_sorting_result' : 'mg',
  277. 'State_statistics' : 'mh',
  278. 'State_updating' : 'mi',
  279. 'State_writing_to_net' : 'mj',
  280. 'State_none' : 'mk',
  281. 'State_other' : 'ml',
  282. 'Handler_commit' : 'mm',
  283. 'Handler_delete' : 'mn',
  284. 'Handler_discover' : 'mo',
  285. 'Handler_prepare' : 'mp',
  286. 'Handler_read_first' : 'mq',
  287. 'Handler_read_key' : 'mr',
  288. 'Handler_read_next' : 'ms',
  289. 'Handler_read_prev' : 'mt',
  290. 'Handler_read_rnd' : 'mu',
  291. 'Handler_read_rnd_next' : 'mv',
  292. 'Handler_rollback' : 'mw',
  293. 'Handler_savepoint' : 'mx',
  294. 'Handler_savepoint_rollback' : 'my',
  295. 'Handler_update' : 'mz',
  296. 'Handler_write' : 'ng',
  297. 'innodb_tables_in_use' : 'nh',
  298. 'innodb_lock_wait_secs' : 'ni',
  299. 'hash_index_cells_total' : 'nj',
  300. 'hash_index_cells_used' : 'nk',
  301. 'total_mem_alloc' : 'nl',
  302. 'additional_pool_alloc' : 'nm',
  303. 'uncheckpointed_bytes' : 'nn',
  304. 'ibuf_used_cells' : 'no',
  305. 'ibuf_free_cells' : 'np',
  306. 'ibuf_cell_count' : 'nq',
  307. 'adaptive_hash_memory' : 'nr',
  308. 'page_hash_memory' : 'ns',
  309. 'dictionary_cache_memory' : 'nt',
  310. 'file_system_memory' : 'nu',
  311. 'lock_system_memory' : 'nv',
  312. 'recovery_system_memory' : 'nw',
  313. 'thread_hash_memory' : 'nx',
  314. 'innodb_sem_waits' : 'ny',
  315. 'innodb_sem_wait_time_ms' : 'nz',
  316. 'Key_buf_bytes_unflushed' : 'og',
  317. 'Key_buf_bytes_used' : 'oh',
  318. 'key_buffer_size' : 'oi',
  319. 'Innodb_row_lock_time' : 'oj',
  320. 'Innodb_row_lock_waits' : 'ok',
  321. 'Query_time_count_00' : 'ol',
  322. 'Query_time_count_01' : 'om',
  323. 'Query_time_count_02' : 'on',
  324. 'Query_time_count_03' : 'oo',
  325. 'Query_time_count_04' : 'op',
  326. 'Query_time_count_05' : 'oq',
  327. 'Query_time_count_06' : 'or',
  328. 'Query_time_count_07' : 'os',
  329. 'Query_time_count_08' : 'ot',
  330. 'Query_time_count_09' : 'ou',
  331. 'Query_time_count_10' : 'ov',
  332. 'Query_time_count_11' : 'ow',
  333. 'Query_time_count_12' : 'ox',
  334. 'Query_time_count_13' : 'oy',
  335. 'Query_time_total_00' : 'oz',
  336. 'Query_time_total_01' : 'pg',
  337. 'Query_time_total_02' : 'ph',
  338. 'Query_time_total_03' : 'pi',
  339. 'Query_time_total_04' : 'pj',
  340. 'Query_time_total_05' : 'pk',
  341. 'Query_time_total_06' : 'pl',
  342. 'Query_time_total_07' : 'pm',
  343. 'Query_time_total_08' : 'pn',
  344. 'Query_time_total_09' : 'po',
  345. 'Query_time_total_10' : 'pp',
  346. 'Query_time_total_11' : 'pq',
  347. 'Query_time_total_12' : 'pr',
  348. 'Query_time_total_13' : 'ps',
  349. 'wsrep_replicated_bytes' : 'pt',
  350. 'wsrep_received_bytes' : 'pu',
  351. 'wsrep_replicated' : 'pv',
  352. 'wsrep_received' : 'pw',
  353. 'wsrep_local_cert_failures' : 'px',
  354. 'wsrep_local_bf_aborts' : 'py',
  355. 'wsrep_local_send_queue' : 'pz',
  356. 'wsrep_local_recv_queue' : 'qg',
  357. 'wsrep_cluster_size' : 'qh',
  358. 'wsrep_cert_deps_distance' : 'qi',
  359. 'wsrep_apply_window' : 'qj',
  360. 'wsrep_commit_window' : 'qk',
  361. 'wsrep_flow_control_paused' : 'ql',
  362. 'wsrep_flow_control_sent' : 'qm',
  363. 'wsrep_flow_control_recv' : 'qn',
  364. 'pool_reads' : 'qo',
  365. 'pool_read_requests' : 'qp',
  366. };
  367. output_dic = {}
  368. for key in keys.keys():
  369. val = str(status[key]) if status.has_key(key) else "-1"
  370. output_dic[keys.get(key)] = val
  371. if fp is not None:
  372. json.dump(output_dic,fp)
  373. return output_dic
  374. """
  375. write to log
  376. """
  377. def big_multiply(left, right, force = None):
  378. left = float(left) if left is not None else 0
  379. right = float(right) if right is not None else 0
  380. if force == 'bc':
  381. return int(left-right)
  382. else:
  383. return int(left*right)
  384. def big_sub(left, right, force = None):
  385. left = float(left) if left is not None else 0
  386. right = float(right) if right is not None else 0
  387. return int(left-right)
  388. def big_add(left,right,force =None):
  389. left = float(left) if left is not None else 0
  390. right = float(right) if right is not None else 0
  391. if force == 'bc':
  392. return int(left+right)
  393. return int(left+right)
  394. def get_innodb_array(text, mysql_version):
  395. mysql_version = 57000
  396. results = {
  397. 'spin_waits' : [],
  398. 'spin_rounds' : [],
  399. 'os_waits' : [],
  400. 'pending_normal_aio_reads' : -1,
  401. 'pending_normal_aio_writes' : -1,
  402. 'pending_ibuf_aio_reads' : -1,
  403. 'pending_aio_log_ios' : -1,
  404. 'pending_aio_sync_ios' : -1,
  405. 'pending_log_flushes' : -1,
  406. 'pending_buf_pool_flushes' : -1,
  407. 'file_reads' : -1,
  408. 'file_writes' : -1,
  409. 'file_fsyncs' : -1,
  410. 'ibuf_inserts' : -1,
  411. 'ibuf_merged' : -1,
  412. 'ibuf_merges' : -1,
  413. 'log_bytes_written' : -1,
  414. 'unflushed_log' : -1,
  415. 'log_bytes_flushed' : -1,
  416. 'pending_log_writes' : -1,
  417. 'pending_chkp_writes' : -1,
  418. 'log_writes' : -1,
  419. 'pool_size' : -1,
  420. 'free_pages' : -1,
  421. 'database_pages' : -1,
  422. 'modified_pages' : -1,
  423. 'pages_read' : -1,
  424. 'pages_created' : -1,
  425. 'pages_written' : -1,
  426. 'queries_inside' : -1,
  427. 'queries_queued' : -1,
  428. 'read_views' : -1,
  429. 'rows_inserted' : -1,
  430. 'rows_updated' : -1,
  431. 'rows_deleted' : -1,
  432. 'rows_read' : -1,
  433. 'innodb_transactions' : -1,
  434. 'unpurged_txns' : -1,
  435. 'history_list' : -1,
  436. 'current_transactions' : -1,
  437. 'hash_index_cells_total' : -1,
  438. 'hash_index_cells_used' : -1,
  439. 'total_mem_alloc' : -1,
  440. 'additional_pool_alloc' : -1,
  441. 'last_checkpoint' : -1,
  442. 'uncheckpointed_bytes' : -1,
  443. 'ibuf_used_cells' : -1,
  444. 'ibuf_free_cells' : -1,
  445. 'ibuf_cell_count' : -1,
  446. 'adaptive_hash_memory' : -1,
  447. 'page_hash_memory' : -1,
  448. 'dictionary_cache_memory' : -1,
  449. 'file_system_memory' : -1,
  450. 'lock_system_memory' : -1,
  451. 'recovery_system_memory' : -1,
  452. 'thread_hash_memory' : -1,
  453. 'innodb_sem_waits' : -1,
  454. 'innodb_sem_wait_time_ms' : -1,
  455. };
  456. txn_seen = False
  457. for line in text.split('\n'):
  458. line = line.strip()
  459. row = re.split(' ',line)
  460. # SEMAPHORES
  461. if line.find('Mutex spin waits') == 0:
  462. #['Mutex', 'spin', 'waits', '90,', 'rounds', '2700,', 'OS', 'waits', '82']
  463. results['spin_waits'] = int(row[3].strip(','))
  464. results['spin_rounds'] = int(row[5].strip(','))
  465. results['os_waits'] = int(row[8].strip(','))
  466. elif line.find("RW-shared spins") == 0 and line.find(";") > 0:
  467. # RW-shared spins 3859028, OS waits 2100750; RW-excl spins 4641946, OS waits 1530310
  468. results['spin_waits'] = int(row[2].strip(','))
  469. results['spin_waits'] = int(row[8].strip(','))
  470. results['os_waits'] = int(row[5].strip(';'))
  471. results['os_waits'] = int(row[11].strip(';'))
  472. elif line.find("RW-shared spins") == 0:
  473. #['RW-shared', 'spins', '2826,', 'rounds', '84780,', 'OS', 'waits', '2814']
  474. results['spin_waits'] = int(row[2].strip(','))
  475. results['spin_rounds'] = int(row[4].strip(','))
  476. results['os_waits'] = int(row[7].strip(','))
  477. elif line.find("RW-shared spins") == 0 and line.find("RW-excl spins") < 0:
  478. #RW-shared spins 604733, rounds 8107431, OS waits 241268
  479. results['spin_waits'] = int(row[2].strip(','))
  480. results['spin_rounds'] = int(row[4].strip(';'))
  481. results['os_waits'] = int(row[7].strip(';'))
  482. elif line.find("RW-excl spins") == 0:
  483. #['RW-excl', 'spins', '2,', 'rounds', '2040,', 'OS', 'waits', '68']
  484. results['spin_waits'] = int(row[2].strip(','))
  485. results['spin_rounds'] = int(row[4].strip(','))
  486. results['os_waits'] = int(row[7].strip(','))
  487. elif line.find("seconds the semaphore:") > 0:
  488. # --Thread 907205 has waited at handler/ha_innodb.cc line 7156 for 1.00 seconds the semaphore:
  489. results['innodb_sem_waits'] = 1
  490. results['innodb_sem_wait_time_ms'] = int(row[9].strip(','))*100
  491. # TRANSACTIONS
  492. elif line.find("Trx id counter") == 0:
  493. if mysql_version < 50600:
  494. # For versions prior 5.6: two decimals or one hex
  495. # Trx id counter 0 1170664159
  496. # Trx id counter 861B144C
  497. results['innodb_transactions'] = max(int(row[3].strip(',')),int(row[4].strip(','))) if row[4] else int(row[3].strip(','),16)
  498. else:
  499. results['innodb_transactions'] = int(row[3].strip(','))
  500. txn_seen = True
  501. elif line.find("Purge done for trx") == 0:
  502. #['Purge', 'done', 'for', "trx's", 'n:o', '<', '214608', 'undo', 'n:o', '<', '0', 'state:', 'running', 'but','idle']
  503. if mysql_version < 506000:
  504. # For versions prior 5.6: two decimals or one hex
  505. # Purge done for trx's n:o < 0 1170663853 undo n:o < 0 0
  506. # Purge done for trx's n:o < 861B135D undo n:o < 0
  507. purged_to = int(row[6].strip(','),16) if row[7] == 'undo' else max(int(row[6].strip(','),16),int(row[6].strip(','),16))
  508. else:
  509. # For versions 5.6+ and MariaDB 10.x: one decimal
  510. # Purge done for trx's n:o < 2903354 undo n:o < 0 state: running but idle
  511. purged_to = int(row[6].strip(','),16)
  512. results['unpurged_txns'] = big_sub(results['innodb_transactions'],purged_to)
  513. elif txn_seen and line.find("---TRANSACTION") == 0:
  514. results['current_transactions'] = 1
  515. if line.find('ACTIVE') >= 0:
  516. results['active_transactions'] = 1
  517. elif txn_seen and line.find("------- TRX HAS BEEN") == 0:
  518. # ------- TRX HAS BEEN WAITING 32 SEC FOR THIS LOCK TO BE GRANTED:
  519. results['innodb_lock_wait_secs'] = int(row[5].strip((',')))
  520. elif line.find("read views open inside InnoDB") > 0:
  521. # 1 read views open inside InnoDB
  522. #['0', 'read', 'views', 'open', 'inside', 'InnoDB']
  523. results['read_views'] = int(row[0])
  524. elif line.find("mysql tables in use") == 0:
  525. # mysql tables in use 2, locked 2
  526. results['innodb_tables_in_user'] = int(row[4].strip(','))
  527. results['innodb_locked_tables'] = int(row[6].strip(','))
  528. elif txn_seen and line.find("lock struct(s)") > 0:
  529. # 23 lock struct(s), heap size 3024, undo log entries 27
  530. # LOCK WAIT 12 lock struct(s), heap size 3024, undo log entries 5
  531. # LOCK WAIT 2 lock struct(s), heap size 368
  532. if line.find("LOCK WAIT") == 0:
  533. results['innodb_lock_structs'] = int(row[2].strip(','))
  534. results['locked_transactions'] = 1
  535. else:
  536. results['innodb_lock_structs'] = int(row[0].strip(','))
  537. # FILE I/O
  538. elif line.find("OS file reads") > 0:
  539. #['1186', 'OS', 'file', 'reads,', '68341', 'OS', 'file', 'writes,', '33879', 'OS', 'fsyncs']
  540. results['file_reads'] = int(row[0])
  541. results['file_writes'] = int(row[4])
  542. results['file_fsyncs'] = int(row[8])
  543. elif line.find("Pending normal aio reads:") == 0:
  544. #['Pending', 'normal', 'aio', 'reads:', '0', '[0,', '0,', '0,', '0]', ',', 'aio', 'writes:', '0', '[0,', '0,', '0,', '0]', ',']
  545. results['pending_normal_aio_reads'] = int(row[4])
  546. results['pending_normal_aio_writes'] = int(row[12])
  547. elif line.find("ibuf aio reads") == 0:
  548. #['ibuf', 'aio', 'reads:', '0,', 'log', "i/o's:", '0,', 'sync', "i/o's:", '0']
  549. results['pending_ibuf_aio_reads'] = int(row[3].strip(','))
  550. results['pending_aio_log_ios'] = int(row[6].strip(','))
  551. results['pending_aio_sync_ios'] = int(row[9].strip(','))
  552. elif line.find("Pending flushes (fsync)") == 0:
  553. #['Pending', 'flushes', '(fsync)', 'log:', '0;', 'buffer', 'pool:', '0']
  554. results['pending_log_flushes'] = int(row[4].strip(';'))
  555. results['pending_buf_pool_flushes'] = int(row[7])
  556. elif line.find("Ibuf for space 0: size") >= 0:
  557. # Older InnoDB code seemed to be ready for an ibuf per tablespace. It
  558. # had two lines in the output. Newer has just one line, see below.
  559. # Ibuf for space 0: size 1, free list len 887, seg size 889, is not empty
  560. # Ibuf for space 0: size 1, free list len 887, seg size 889,
  561. results['ibuf_used_cells'] = int(row[5].strip(':').strip(','))
  562. results['ibuf_free_cells'] = int(row[9].strip(';').strip(','))
  563. results['ibuf_cell_count'] = int(row[12].strip(';').strip(','))
  564. elif line.find("Ibuf: size") == 0:
  565. #['Ibuf:', 'size', '1,', 'free', 'list', 'len', '0,', 'seg', 'size', '2,', '40', 'merges']
  566. results['ibuf_used_cells'] = int(row[2].strip(','))
  567. results['ibuf_free_cells'] = int(row[6].strip(','))
  568. results['ibuf_cell_count'] = int(row[9].strip(','))
  569. if line.find('merges') > 0:
  570. results['ibuf_merges'] = int(row[10].strip(','))
  571. # elif line.find("merged operations:") == 0 and line.find('delete mark') >= 0:
  572. # pass
  573. elif line.find(', delete mark') >= 0 :
  574. #['insert', '44,', 'delete', 'mark', '0,', 'delete', '0']
  575. results['ibuf_inserts'] = int(row[1].strip(','))
  576. results['ibuf_merged'] = int(row[1].strip(',')) + int(row[4].strip(',')) + int(row[6].strip(','))
  577. elif line.find(" merged recs, ") > 0:
  578. # 19817685 inserts, 19817684 merged recs, 3552620 merges
  579. results['ibuf_inserts'] = int(row[0].strip(','))
  580. results['ibuf_merged'] = int(row[2].strip(','))
  581. results['ibuf_merges'] = int(row[5].strip(','))
  582. elif line.find('Hash table size') == 0:
  583. #['Hash', 'table', 'size', '276707,', 'node', 'heap', 'has', '33', 'buffer(s)']
  584. results['hash_index_cells_total'] = int(row[3].strip(','))
  585. results['hash_index_cells_used'] = int(row[7].strip(','))
  586. elif line.find(" log i/o's done, ")> 0:
  587. #['24255', 'log', "i/o's", 'done,', '0.83', 'log', "i/o's/second"]
  588. results['log_writes'] = int(row[0])
  589. elif line.find(" pending log writes, ") > 0:
  590. #['0', 'pending', 'log', 'writes,', '0', 'pending', 'chkp', 'writes']
  591. results['pending_log_writes'] = int(row[0])
  592. results['pending_chkp_writes'] = int(row[4])
  593. elif line.find("Log sequence number") == 0:
  594. # Log sequence number 13093949495856 //plugin
  595. # Log sequence number 125 3934414864 //normal
  596. #['Log', 'sequence', 'number', '25184569']
  597. if len(row) > 4:
  598. results['log_bytes_written'] = max(int(row[3]),int(row[4]))
  599. else:
  600. results['log_bytes_written'] = int(row[3])
  601. elif line.find("Log flushed up to") >= 0:
  602. #['Log', 'flushed', 'up', 'to', '', '', '25255663']
  603. # Log flushed up to 13093948219327
  604. # Log flushed up to 125 3934414864
  605. if len(row) > 8:
  606. results['log_bytes_flushed'] = max(int(row[6]),int(row[7]))
  607. else:
  608. results['log_bytes_flushed'] = int(row[6])
  609. elif line.find("Total memory allocated") >= 0 and line.find("in additional pool allocated") > 0:
  610. #['Total', 'memory', 'allocated', '137363456;', 'in', 'additional', 'pool', 'allocated', '0']
  611. results['total_mem_alloc'] = int(row[3].strip(';'))
  612. results['additional_pool_alloc'] = int(row[8])
  613. elif line.find("Adaptive hash index ") == 0:
  614. #Adaptive hash index 1538240664 (186998824 + 1351241840)
  615. results['active_transactions'] = int(row[3])
  616. elif line.find("Page hash ") >=0 :
  617. ## Page hash 11688584
  618. #results['page_hash_memory'] = int(row[2])
  619. pass
  620. elif line.find("Dictionary cache") >= 0:
  621. # Page hash 11688584
  622. #results['page_hash_memory'] = int(row[2])
  623. pass
  624. elif line.find("File system") >= 0:
  625. # File system 313848 (82672 + 231176)
  626. #results['file_system_memory'] = to_int(row[2])
  627. pass
  628. elif line.find("Lock system") >= 0:
  629. # Lock system 29232616 (29219368 + 13248)
  630. #results['lock_system_memory'] = int(row[2])
  631. pass
  632. elif line.find("Recovery system") >= 0:
  633. # Recovery system 0 (0 + 0)
  634. #results['recovery_system_memory'] = int(row[2]);
  635. pass
  636. elif line.find("Threads ") >= 0:
  637. # Threads 409336 (406936 + 2400)
  638. #results['thread_hash_memory'] = int(row[1]);
  639. pass
  640. elif line.find("innodb_io_pattern ") >= 0:
  641. # innodb_io_pattern 0 (0 + 0)
  642. #results['innodb_io_pattern_memory'] = int(row[1]);
  643. pass
  644. elif line.find("Buffer pool size ") >= 0:
  645. #['Buffer', 'pool', 'size', '', '', '8192']
  646. # The " " after size is necessary to avoid matching the wrong line:
  647. # Buffer pool size 1769471
  648. # Buffer pool size, bytes 28991012864
  649. results['pool_size'] = int(row[5])
  650. elif line.find("Database pages") >= 0:
  651. #['Database', 'pages', '', '', '', '', '994']
  652. results['database_pages'] = int(row[6])
  653. elif line.find("Modified db pages") >= 0:
  654. #['Modified', 'db', 'pages', '', '45']
  655. results['modified_pages'] = int(row[4])
  656. elif line.find("Pages read ahead") >= 0:
  657. #['Pages', 'read', 'ahead', '0.00/s,', 'evicted', 'without', 'access', '0.00/s,', 'Random', 'read', 'ahead', '0.00/s']
  658. ## Must do this BEFORE the next test, otherwise it'll get fooled by this
  659. # line from the new plugin (see samples/innodb-015.txt):
  660. # Pages read ahead 0.00/s, evicted without access 0.06/s
  661. # TODO: No-op for now, see issue 134.
  662. pass
  663. elif line.find("Pages read") == 0:
  664. #['Pages', 'read', '845,', 'created', '149,', 'written', '53991']
  665. results['pages_read'] = int(row[2].strip(','))
  666. results['pages_created'] = int(row[4].strip(','))
  667. results['pages_written'] = int(row[6].strip(','))
  668. elif line.find("Number of rows inserted") == 0:
  669. #['Number', 'of', 'rows', 'inserted', '16243,', 'updated', '7859,', 'deleted', '10849,', 'read', '1488153']
  670. results['rows_inserted'] = int(row[4].strip(','))
  671. results['rows_updated'] = int(row[6].strip(','))
  672. results['rows_deleted'] = int(row[8].strip(','))
  673. results['rows_read'] = int(row[10].strip(','))
  674. elif line.find(" queries inside InnoDB, ") >= 0:
  675. #['0', 'queries', 'inside', 'InnoDB,', '0', 'queries', 'in', 'queue']
  676. results['queries_inside'] = int(row[0])
  677. results['queries_queued'] = int(row[4])
  678. for key in ['spin_waits','spin_rounds','os_waits']:
  679. results[key] = int(results[key])
  680. return results
  681. def main(*args,**kwargs):
  682. status_out = ss_get_mysql_stats()
  683. result = status_out.get(sys.argv[1]) if status_out.has_key(sys.argv[1]) else '-1'
  684. print result
  685. if __name__ == "__main__":
  686. main()

4)增加权限

chmod +x MySQL_Zabbix.py 

5)重启服务生效

systemctl restart zabbix-agent2

MIT License

注:一下是原创的license请务必遵守,开发不易,谢谢

MIT License

Copyright (c) 2017 

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/article/detail/49213
推荐阅读
相关标签
  

闽ICP备14008679号