1. 程式人生 > >ZABBIX最全MYSQL自定義監控多實例mysql與主從復制狀態沒有之一

ZABBIX最全MYSQL自定義監控多實例mysql與主從復制狀態沒有之一

等待 獲取 oca 含義 cli ges type con sha

我們首先要提取你服務器上有多少mysql實例提取方法如下:

#!/usr/bin/env python
import os
import json
t=os.popen("""sudo netstat -nltp|grep -w "mysqld"|grep -w "LISTEN"|grep -v grep|grep -v ‘^$‘|awk -F: ‘{print $4}‘""")
s=os.popen("""sudo netstat -nltp|grep -w "mysqld"|grep -w "LISTEN"|grep -v grep|grep -v ‘^$‘|awk -F: ‘{print $2}‘|awk ‘{print $1}‘""")

port_info = []
ports = []

for port in t.readlines():
r = os.path.basename(port.strip())
if r:
port_info.append(r)

for port in s.readlines():
r = os.path.basename(port.strip())
if r:
port_info.append(r)

port_info = list(set(port_info))

for port in port_info:
ports += [{‘{#MYSQLPORT}‘:port}]
print(json.dumps({‘data‘:ports},sort_keys=True,indent=4,separators=(‘,‘,‘:‘)))

執行結果:
技術分享圖片
先看一下監控的第一個腳本和各個參數的含義如下:

#!/bin/bash
#-------------------------------------------------------------------------------
#FileName: check_mysql.sh
#-------------------------------------------------------------------------------

#用戶名
MYSQL_USER=‘zabbix‘

#密碼
MYSQL_PWD=‘zabbix‘

#主機地址/IP
MYSQL_HOST=‘127.0.0.1‘

#端口
MYSQL_PORT=$2

#數據連接
MYSQL_CONN="/usr/bin/mysqladmin -u${MYSQL_USER} -p${MYSQL_PWD} -h${MYSQL_HOST} -P${MYSQL_PORT}"

##help函數
help() {
echo "Usage:$0 [ping|Uptime|Com_update|Slow_queries|Com_select|Com_rollback|Questions|Com_insert|Com_delete|Com_commit|Bytes_sent|Bytes_received|Com_begin|Aborted_clients|Aborted_connects|Binlog_cache_disk_use|Binlog_cache_use|Uptime|Threads_running|Threads_connected|Threads_cached|Table_locks_immediate|Table_locks_waited|Slow_queries|Slave_running|Select_scan|Qcache_hits|Qcache_free_memory|Innodb_buffer_pool_pages_data|Innodb_buffer_pool_pages_dirty|Innodb_buffer_pool_pages_flushed|Innodb_buffer_pool_pages_free|Innodb_buffer_pool_pages_misc|Innodb_buffer_pool_pages_total|Innodb_buffer_pool_read_ahead_rnd|Innodb_buffer_pool_read_requests|Innodb_buffer_pool_reads|Innodb_buffer_pool_wait_free|Innodb_buffer_pool_write_requests|Innodb_data_fsyncs|Innodb_data_read|Innodb_data_reads|Innodb_data_writes|Innodb_data_written|Innodb_dblwr_pages_written|Innodb_dblwr_writes|Innodb_log_write_requests|Innodb_log_writes|Innodb_os_log_fsyncs|Innodb_os_log_written|Innodb_page_size|Innodb_pages_created|Innodb_pages_read|Innodb_pages_written|Innodb_rows_deleted|Innodb_rows_inserted|Innodb_rows_read|Innodb_rows_updated|Key_blocks_unused|Key_blocks_used|Key_read_requests|Key_reads|Open_tables|Opened_tables|Open_files|Qcache_free_memory|Qcache_lowmem_prunes|Queries|Select_full_join|Select_range|Select_range_check|Table_locks_immediate|Table_locks_waited|Table_open_cache_hits|Table_open_cache_misses] port"
}

#參數是否正確
if [ $# -lt "2" ];then
echo "參數缺失!"
help
exit 2
fi

#獲取數據
case $1 in
ping)
result=${MYSQL_CONN} ping | grep -c alive
echo $result
;;
Uptime)
result=${MYSQL_CONN} status|cut -f2 -d":"|cut -f1 -d"T"
echo $result
;;
Com_update)
result=${MYSQL_CONN} extended-status |grep -w "Com_update"|cut -d"|" -f3
echo $result
;;
Slow_queries)
result=${MYSQL_CONN} status |cut -f5 -d":"|cut -f1 -d"O"
echo $result
;;
Com_select)
result=${MYSQL_CONN} extended-status |grep -w "Com_select"|cut -d"|" -f3
echo $result
;;
Com_rollback)
result=${MYSQL_CONN} extended-status |grep -w "Com_rollback"|cut -d"|" -f3
echo $result
;;
Questions)
result=${MYSQL_CONN} status|cut -f4 -d":"|cut -f1 -d"S"
echo $result
;;
Com_insert)
result=${MYSQL_CONN} extended-status |grep -w "Com_insert"|cut -d"|" -f3
echo $result
;;
Com_delete)
result=${MYSQL_CONN} extended-status |grep -w "Com_delete"|cut -d"|" -f3
echo $result
;;
Com_commit)
result=${MYSQL_CONN} extended-status |grep -w "Com_commit"|cut -d"|" -f3
echo $result
;;
Bytes_sent)
result=${MYSQL_CONN} extended-status |grep -w "Bytes_sent" |cut -d"|" -f3
echo $result
;;
Bytes_received)
result=${MYSQL_CONN} extended-status |grep -w "Bytes_received" |cut -d"|" -f3
echo $result
;;
Com_begin)
result=${MYSQL_CONN} extended-status |grep -w "Com_begin"|cut -d"|" -f3
echo $result
;;
#為正確關閉連接終止的連接數
Aborted_clients)
result=${MYSQL_CONN} extended-status |grep -w "Aborted_clients"|cut -d"|" -f3
echo $result
;;
#連接到Mysql服務器失敗次數
Aborted_connects)
result=${MYSQL_CONN} extended-status |grep -w "Aborted_connects"|cut -d"|" -f3
echo $result
;;
#二進制日誌緩存的已經存在的硬盤的條數
Binlog_cache_disk_use)
result=${MYSQL_CONN} extended-status |grep -w "Binlog_cache_disk_use"|cut -d"|" -f3
echo $result
;;
#二進制日誌已緩存的條數(內存中)
result=${MYSQL_CONN} extended-status |grep -w "Binlog_cache_use"|cut -d"|" -f3
echo $result
;;
#服務器已運行時間
Uptime)
result=${MYSQL_CONN} extended-status |grep -w "Uptime"|cut -d"|" -f3
echo $result
;;
#激活的線程數
Threads_running)
result=${MYSQL_CONN} extended-status |grep -w "Threads_running"|cut -d"|" -f3
echo $result
;;
#創建用來處理連接的線程數
Threads_created)
result=${MYSQL_CONN} extended-status |grep -w "Threads_created"|cut -d"|" -f3
echo $result
;;
#當前打開的連接數量
Threads_connected)
result=${MYSQL_CONN} extended-status |grep -w "Threads_connected"|cut -d"|" -f3
echo $result
;;
#線程緩存內的線程數量
Threads_cached)
result=${MYSQL_CONN} extended-status |grep -w "Threads_cached"|cut -d"|" -f3
echo $result
;;
#立即釋放表鎖數
Table_locks_immediate)
result=${MYSQL_CONN} extended-status |grep -w "Table_locks_immediate"|cut -d"|" -f3
echo $result
;;
#等待表鎖數
Table_locks_waited)
result=${MYSQL_CONN} extended-status |grep -w "Table_locks_waited"|cut -d"|" -f3
echo $result
;;
#是否開啟了slave線程
Slave_running)
result=${MYSQL_CONN} extended-status |grep -w "Slave_running"|cut -d"|" -f3
echo $result
;;
#執行全表搜索查詢數量
Select_scan)
result=${MYSQL_CONN} extended-status |grep -w "Select_scan"|cut -d"|" -f3
echo $result
;;
#命中
Qcache_hits)
result=${MYSQL_CONN} extended-status |grep -w "Qcache_hits"|cut -d"|" -f3
echo $result
;;
Qcache_free_memory)
result=${MYSQL_CONN} extended-status |grep -w "Qcache_free_memory"|cut -d"|" -f3
echo $result
;;
#包含數據的頁數
Innodb_buffer_pool_pages_data)
result=${MYSQL_CONN} extended-status |grep -w "Innodb_buffer_pool_pages_data"|cut -d"|" -f3
echo $result
;;
#當前臟頁數
Innodb_buffer_pool_pages_dirty)
result=${MYSQL_CONN} extended-status |grep -w "Innodb_buffer_pool_pages_dirty"|cut -d"|" -f3
echo $result
;;
#已經flush的頁面數
Innodb_buffer_pool_pages_flushed)
result=${MYSQL_CONN} extended-status |grep -w "Innodb_buffer_pool_pages_flushed"|cut -d"|" -f3
echo $result
;;
#空頁數
Innodb_buffer_pool_pages_free)
result=${MYSQL_CONN} extended-status |grep -w "Innodb_buffer_pool_pages_free"|cut -d"|" -f3
echo $result
;;
#優先用作管理的頁數
Innodb_buffer_pool_pages_misc)
result=${MYSQL_CONN} extended-status |grep -w "Innodb_buffer_pool_pages_misc"|cut -d"|" -f3
echo $result
;;
#總頁數
Innodb_buffer_pool_pages_total)
result=${MYSQL_CONN} extended-status |grep -w "Innodb_buffer_pool_pages_total"|cut -d"|" -f3
echo $result
;;
#隨機讀取的次數
Innodb_buffer_pool_read_ahead_rnd)
result=${MYSQL_CONN} extended-status |grep -w "Innodb_buffer_pool_read_ahead_rnd"|cut -d"|" -f3
echo $result
;;
#InnoDB已完成的邏輯讀請求
Innodb_buffer_pool_read_requests)
result=${MYSQL_CONN} extended-status |grep -w "Innodb_buffer_pool_read_requests"|cut -d"|" -f3
echo $result
;;
#從磁盤上讀取的頁數
Innodb_buffer_pool_reads)
result=${MYSQL_CONN} extended-status |grep -w "Innodb_buffer_pool_reads"|cut -d"|" -f3
echo $result
;;
#緩沖池等待空閑的次數
Innodb_buffer_pool_wait_free)
result=${MYSQL_CONN} extended-status |grep -w "Innodb_buffer_pool_wait_free"|cut -d"|" -f3
echo $result
;;
#緩沖池總發出的請求次數
Innodb_buffer_pool_write_requests)
result=${MYSQL_CONN} extended-status |grep -w "Innodb_buffer_pool_wait_free"|cut -d"|" -f3
echo $result
;;
#fysncs()操作數
Innodb_data_fsyncs)
result=${MYSQL_CONN} extended-status |grep -w "Innodb_data_fsyncs"|cut -d"|" -f3
echo $result
;;
#總共讀取的字節數
Innodb_data_read)
result=${MYSQL_CONN} extended-status |grep -w "Innodb_data_read"|cut -d"|" -f3
echo $result
;;
#Innodb完成的讀的次數
Innodb_data_reads)
result=${MYSQL_CONN} extended-status |grep -w "Innodb_data_reads"|cut -d"|" -f3
echo $result
;;
#Innodb完成的寫的次數
Innodb_data_writes)
result=${MYSQL_CONN} extended-status |grep -w "Innodb_data_writes"|cut -d"|" -f3
echo $result
;;
#InnoDB總寫出的次數
Innodb_data_written)
result=${MYSQL_CONN} extended-status |grep -w "Innodb_data_written"|cut -d"|" -f3
echo $result
;;
#雙寫已經寫好的頁數
Innodb_dblwr_pages_written)
result=${MYSQL_CONN} extended-status |grep -w "Innodb_dblwr_pages_written"|cut -d"|" -f3
echo $result
;;
#已執行的雙寫操作數量
Innodb_dblwr_writes)
result=${MYSQL_CONN} extended-status |grep -w "Innodb_dblwr_writes"|cut -d"|" -f3
echo $result
;;
#日誌寫請求數
Innodb_log_write_requests)
result=${MYSQL_CONN} extended-status |grep -w "Innodb_log_write_requests"|cut -d"|" -f3
echo $result
;;
#向日誌文件的物理寫數量
Innodb_log_writes)
result=${MYSQL_CONN} extended-status |grep -w "Innodb_log_writes"|cut -d"|" -f3
echo $result
;;
#向日誌文件完成的fsync()寫入量
Innodb_os_log_fsyncs)
result=${MYSQL_CONN} extended-status |grep -w "Innodb_os_log_fsyncs"|cut -d"|" -f3
echo $result
;;
#寫入文件的字節數
Innodb_os_log_written)
result=${MYSQL_CONN} extended-status |grep -w "Innodb_os_log_written"|cut -d"|" -f3
echo $result
;;
#編譯的InnoDB頁大小
Innodb_page_size)
result=${MYSQL_CONN} extended-status |grep -w "Innodb_page_size"|cut -d"|" -f3
echo $result
;;
#創建的頁數
Innodb_pages_created)
result=${MYSQL_CONN} extended-status |grep -w "Innodb_pages_created"|cut -d"|" -f3
echo $result
;;
#從buffer_pool中讀取的頁數
Innodb_pages_read)
result=${MYSQL_CONN} extended-status |grep -w "Innodb_pages_read"|cut -d"|" -f3
echo $result
;;
#寫入的頁數
Innodb_pages_written)
result=${MYSQL_CONN} extended-status |grep -w "Innodb_pages_written"|cut -d"|" -f3
echo $result
;;
#刪除
Innodb_rows_deleted)
result=${MYSQL_CONN} extended-status |grep -w "Innodb_rows_deleted"|cut -d"|" -f3
echo $result
;;
#插入
Innodb_rows_inserted)
result=${MYSQL_CONN} extended-status |grep -w "Innodb_rows_inserted"|cut -d"|" -f3
echo $result
;;
#從InnoDB表讀取的行數
Innodb_rows_read)
result=${MYSQL_CONN} extended-status |grep -w "Innodb_rows_read"|cut -d"|" -f3
echo $result
;;
#更新
Innodb_rows_updated)
result=${MYSQL_CONN} extended-status |grep -w "Innodb_rows_updated"|cut -d"|" -f3
echo $result
;;
#未使用的緩存簇(blocks)數
Key_blocks_unused)
result=${MYSQL_CONN} extended-status |grep -w "Key_blocks_unused"|cut -d"|" -f3
echo $result
;;
#曾經用到的最大的blocks數
Key_blocks_used)
result=${MYSQL_CONN} extended-status |grep -w "Key_blocks_used"|cut -d"|" -f3
echo $result
;;
#一共有多少個索引請求
Key_read_requests)
result=${MYSQL_CONN} extended-status |grep -w "Key_read_requests"|cut -d"|" -f3
echo $result
;;
#一共發生了多少次物理IO
Key_reads)
result=${MYSQL_CONN} extended-status |grep -w "Key_reads"|cut -d"|" -f3
echo $result
;;
#當前打開的表數
Open_tables)
result=${MYSQL_CONN} extended-status |grep -w "Open_tables"|cut -d"|" -f3
echo $result
;;
#當前已打開的表數量
Opened_tables)
result=${MYSQL_CONN} extended-status |grep -w "Opened_tables"|cut -d"|" -f3
echo $result
;;
#當前打開的文件數
Open_files)
result=${MYSQL_CONN} extended-status |grep -w "Open_files"|cut -d"|" -f3
echo $result
;;
#當前已打開的文件數
Opened_files)
result=${MYSQL_CONN} extended-status |grep -w "Opened_files"|cut -d"|" -f3
echo $result
;;
Qcache_free_memory)
result=${MYSQL_CONN} extended-status |grep -w "Qcache_free_memory"|cut -d"|" -f3
echo $result
;;
#當Qcache_free_memory還有很多的時候Qcache_lowmem_prunes增高報警
Qcache_lowmem_prunes)
result=${MYSQL_CONN} extended-status |grep -w "Qcache_lowmem_prunes"|cut -d"|" -f3
echo $result
;;
#mysql系統接收的查詢的次數(包括存儲過程內部的查詢)
Queries)
result=${MYSQL_CONN} extended-status |grep -w "Queries"|cut -d"|" -f3
echo $result
;;
#應用到其他表,沒有使用索引的聯接的數量
Select_full_join)
result=${MYSQL_CONN} extended-status |grep -w "Select_full_join"|cut -d"|" -f3
echo $result
;;
#僅應用到第一個表,在第一個表中使用範圍的聯接的數量
Select_range)
result=${MYSQL_CONN} extended-status |grep -w "Select_range"|cut -d"|" -f3
echo $result
;;
#應用到其他表
Select_range_check)
result=${MYSQL_CONN} extended-status |grep -w "Select_range_check"|cut -d"|" -f3
echo $result
;;
#立即釋放表鎖數
Table_locks_immediate)
result=${MYSQL_CONN} extended-status |grep -w "Table_locks_immediate"|cut -d"|" -f3
echo $result
;;
#需要等待的表鎖數
Table_locks_waited)
result=${MYSQL_CONN} extended-status |grep -w "Table_locks_waited"|cut -d"|" -f3
echo $result
;;
#表緩存查找的命中數
Table_open_cache_hits)
result=${MYSQL_CONN} extended-status |grep -w "Table_open_cache_hits"|cut -d"|" -f3
echo $result
;;
#表緩存查找的未命中數
Table_open_cache_misses)
result=${MYSQL_CONN} extended-status |grep -w "Table_open_cache_misses"|cut -d"|" -f3
echo $result
;;

*)
help
;;
esac

第一個腳本是所有參數的一個提取過程,然後我們可以在系統上直接提取參數了,接下來我們開始做key,怎麽做了如下:

UserParameter=mysql_discovery[],/bin/bash /data/zabbix/scripts/mysql_discovery.py
UserParameter=mysql.status[
],/data/zabbix/scripts/check_mysql.sh $1 $2
UserParameter=mysql.ping[*],/data/zabbix/scripts/check_mysql.sh ping $1

然後我們就可以去zabbix_server服務器端去get值去了,這是上邊比較簡單的監控方法。其中的那個值說白了只要我命令能獲取到值我都能監控起來,空氣中的二氧化碳也不例外。
第二種方法就是分步自己寫少量對自己有用的參數就行監控就好了。話不多說打字挺累的,直接上腳本。
1、獲取mysql狀態(mysql性能數據,通過mysqldump)
#!/bin/bash

one=ps xua|grep mysqld|grep -w "port=3307"|wc -l
if [ $one -lt 1 ];then

socket=ps xua|grep -w "socket"|grep -v "mysqld_safe"|awk ‘BEGIN {FS="--socket="} {print $2}‘|awk ‘{print $1}‘|grep sock|tail -n 1
mysqladmin -r ext -uzabbix -pzabbix -P 3307 -S $socket 2>/dev/null|grep -w "$2"|awk ‘{print $4}‘

else

socket=ps xua|grep -w "socket"|grep -v "mysqld_safe"|grep -w "port=$1"|awk ‘BEGIN {FS="--socket="} {print $2}‘|awk ‘{print $1}‘
mysqladmin -r ext -uzabbix -pzabbix -P $1 -S $socket 2>/dev/null|grep -w "$2"|awk ‘{print $4}‘
fi

2、獲取mysql主從復制狀態

#!/bin/bash

one=ps xua|grep mysqld|grep -w "port=3307"|wc -l

if [ $one -lt 1 ];then

socket=`ps xua|grep -w "socket"|grep -v "mysqld_safe"|awk ‘BEGIN {FS="--socket="} {print $2}‘|awk ‘{print $1}‘|grep  sock|tail -n 1`
 mysql -uzabbix -pzabbix  -P 3307 -S $socket -e "show slave status\G" 2>/dev/null|grep -w "$2"|awk ‘{print $2}‘

else
socket=ps xua|grep -w "socket"|grep -v "mysqld_safe"|grep -w "port=$1"|awk ‘BEGIN {FS="--socket="} {print $2}‘|awk ‘{print $1}‘
mysql -uzabbix -pzabbix -P $1 -S $socket -e "show slave status\G" 2>/dev/null|grep -w "$2"|awk ‘{print $2}‘
fi
技術分享圖片

3、mysql存活狀態

#!/bin/bash

one=ps xua|grep mysqld|grep -w "port=3307"|wc -l

if [ $one -lt 1 ];then

socket=`ps xua|grep -w "socket"|grep -v "mysqld_safe"|awk ‘BEGIN {FS="--socket="} {print $2}‘|awk ‘{print $1}‘|grep sock|tail -n 1`
 mysqladmin -h localhost -P 3307 -S $socket -uzabbix -p‘zabbix‘ ping 2>/dev/null| grep -c alive

else
socket=ps xua|grep -w "socket"|grep -v "mysqld_safe"|grep -w "port=$1"|awk ‘BEGIN {FS="--socket="} {print $2}‘|awk ‘{print $1}‘
mysqladmin -h localhost -P $1 -S $socket -uzabbix -p‘zabbix‘ ping 2>/dev/null| grep -c alive
fi

4、自定義key
這一部分不用教大家了,自己根據自己實際路徑腳本名稱想定義成什麽key去定義就好了。

最後給大家看一下監控完成後的樣子就好了:

技術分享圖片

技術分享圖片

技術分享圖片
OK完成了。不懂得請留言謝謝,不一定及時回信息,但是看到一定回信息。加油向小米飛貓看齊。

ZABBIX最全MYSQL自定義監控多實例mysql與主從復制狀態沒有之一