月小升在mysql的配置上卡了3天,緣由一方面不熟悉,一方面我昨天18:00配置完畢,半天沒有數據,就絕望的關閉了,我昨天晚上還在糾結要不要繼續研究,今天打開一看,數據圖表都有了。原來mysql的監控,須要一點時間來生效。html
當知道了正確的辦法,我在另外一臺服務器配置,其實只須要3分鐘java
步驟:配置 > 主機 > 點擊主機的主機 > 模版mysql
見圖sql
轉存失敗
從新上傳
取消
點擊那個模版進去看看模版的描述緩存
Requirements for template operation:
1.Install Zabbix agent and MySQL client.
2.Copy Template_DB_MySQL.conf into folder with Zabbix agent configuration (/etc/zabbix/zabbix_agentd.d/ by default). Don't forget restart zabbix-agent.
3.Create MySQL user for monitoring. For example:
CREATE USER 'zbx_monitor'@'%' IDENTIFIED BY '<password>';
GRANT USAGE,REPLICATION CLIENT,PROCESS,SHOW DATABASES,SHOW VIEW ON . TO 'zbx_monitor'@'%';
For more information read the MYSQL documentation https://dev.mysql.com/doc/refman/8.0/en/grant.html , please.
4.Create .my.cnf in home directory of Zabbix agent for Linux (/var/lib/zabbix by default ) or my.cnf in c:\ for Windows. For example:
[client]
user=zbx_monitor
password=<password>
You can discuss this template or leave feedback on our forum https://www.zabbix.com/forum/zabbix-suggestions-and-feedback/384189-discussion-thread-for-official-zabbix-template-db-mysql
Template tooling version used: 0.35bash
1)查看zabbix中的mysql監控模板,find / -name userparameter_mysql.conf服務器
[root@ ~]# find / -name userparameter_mysql.conf
/backup/soft/zabbix-4.4.6/conf/zabbix_agentd/userparameter_mysql.conf
這個文件的第一句話ide
#template_db_mysql.conf created by Zabbix for "Template DB MySQL" and Zabbix 4.2
2)拷貝到zabbix_agentd.conf.d/目錄下性能
cp –r /backup/soft/zabbix-4.4.6/conf/zabbix_agentd/userparameter_mysql.conf /usr/local/etc/zabbix_agentd.conf.d/
3)測試模版是否有效測試
mysql.ping [t|mysqladmin: connect to server at '-P' failed
error: 'Unknown MySQL server host '-P' (2)'
4) 模版修改
UserParameter=mysql.ping[*], mysqladmin -h"$1" -P"$2" ping
說-P這個參數不知道 $1 $2 應該從其餘地方傳入的參數。
測試糾纏的過程下面寫,寫最終成型的模版
UserParameter=mysql.ping[], HOME=/etc/zabbix mysqladmin ping
UserParameter=mysql.get_status_variables[],HOME=/etc/zabbix mysql -sNX -e "show global status"
UserParameter=mysql.version[],HOME=/etc/zabbix mysqladmin version
UserParameter=mysql.uptime,HOME=/etc/zabbix mysqladmin status | cut -f2 -d ":" | cut -f1 -d "T" | tr -d " "
UserParameter=mysql.db.discovery[],HOME=/etc/zabbix mysql -sN -e "show databases"
UserParameter=mysql.dbsize[], HOME=/etc/zabbix mysql -sN -e "SELECT SUM(DATA_LENGTH + INDEX_LENGTH) FROM INFORMATION_SCHEMA.TABL
ES WHERE TABLE_SCHEMA='$3'"
UserParameter=mysql.replication.discovery[], HOME=/etc/zabbix mysql -sNX -e "show slave status"
UserParameter=mysql.slave_status[*], HOME=/etc/zabbix mysql -sNX -e "show slave status"
個人這個模版和拷貝過來的,其實只多了一個 HOME=/etc/zabbix (要相信原生的模版寫法)
5)搞一下那個 .my.cnf
[mysql]
user=zbx_monitor
password=Javaer123456
[mysqladmin]
user=zbx_monitor
password=Javaer123456
路徑/etc/zabbix/.my.cnf 和上面的HOME=/etc/zabbix 是否是一致,對,那個HOME的意思就是「大哥來這裏找我」
6)重啓動zabbix_agentd
這一步結束後,要等,等5分鐘,10分鐘的,不要着急。
7)看數據在監測 > 最新數據
8)看圖標在監測 > 圖形裏
轉存失敗
從新上傳
取消
我以爲反覆糾結userparameter_mysql.conf的過程是由於很多東西沒弄明白了。
END
=====華麗分割線======
記錄以下,下面的記錄含有很多能繼續測試zabbix底層讀取配置文件的邏輯。
zabbix_agentd -t mysql.ping[*] -c /usr/local/etc/zabbix_agentd.conf.d/userparameter_mysql.conf
mysqladmin -uzbx_monitor -p'Javaer123456' -hlocalhost ping
zabbix_get -slocalhost -p 10050 -k mysql.ping
zabbix_agentd -t mysql.ping[*] -c /usr/local/etc/zabbix_agentd.conf.d/userparameter_mysql.conf
強制改爲寫死的,我就測試mysql.ping 一個參數
UserParameter=mysql.ping[*], mysqladmin -uzbx_monitor -p'Javaer123456' -hlocalhost ping
zabbix_agentd -t mysql.ping -c /usr/local/etc/zabbix_agentd.conf.d/userparameter_mysql.conf
mysql.ping [t|mysqladmin: [Warning] Using a password on the command line interface can be insecure.
mysqld is alive]
查看mysqladmin是否正常
#mysqladmin -uzbx_monitor -p'Javaer123456' -hlocalhost ping
mysqladmin: [Warning] Using a password on the command line interface can be insecure.
mysqld is alive
命令行改進,返回1或者0
我在/etc/zabbix下創建一個.my.cnf的測試
UserParameter=mysql.ping,HOME=/etc/zabbix/ mysqladmin ping | grep -c alive
這個命令中」UserParameter」表示這是一個用戶自定義的腳本;「=」號後是腳本的內容;「mysql.ping」是Key,「,」號後的命令會在Zabbix Server向Agent發起獲取「mysql.ping」這個key的請求時被調用,並將返回值返回給Server。
註釋掉其餘參數
#UserParameter=mysql.ping[], mysqladmin -h"$1" -P"$2" ping
UserParameter=mysql.ping[], mysqladmin -uzbx_monitor -p'Javaer123456' -hlocalhost ping
#UserParameter=mysql.get_status_variables[], mysql -h"$1" -P"$2" -sNX -e "show global status"
#UserParameter=mysql.version[], mysqladmin -s -h"$1" -P"$2" version
#UserParameter=mysql.db.discovery[], mysql -h"$1" -P"$2" -sN -e "show databases"
#UserParameter=mysql.dbsize[], mysql -h"$1" -P"$2" -sN -e "SELECT SUM(DATA_LENGTH + INDEX_LENGTH) FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA='$3'"
#UserParameter=mysql.replication.discovery[], mysql -h"$1" -P"$2" -sNX -e "show slave status"
#UserParameter=mysql.slave_status[], mysql -h"$1" -P"$2" -sNX -e "show slave status"
重啓動
測試server端讀取狀況
zabbix_get -slocalhost -p 10050 -k mysql.ping
mysqld is alive
mysqladmin -uzbx_monitor -p'Javaer123456' -hlocalhost extended-status
出來一堆東西
mysqladmin -uzbx_monitor -p'Javaer123456' -hlocalhost extended-status | grep -w "Threads_connected" | cut -d "|" -f 3
mysqladmin: [Warning] Using a password on the command line interface can be insecure.
71
測試這個玩意
UserParameter=mysql.status[*],mysqladmin -uzbx_monitor -p'Javaer123456' -hlocalhost extended-status | grep -w "Threads_connected" | cut -d "|" -f 3
mysql.status [t|mysqladmin: [Warning] Using a password on the command line interface can be insecure.
71]
zabbix_get -slocalhost -p 10050 -k mysql.status
mysqladmin -uzbx_monitor -p'Javaer123456' -hlocalhost extended-status | grep -w "Connections" | cut -d "|" -f 3
抄寫個腳本給配置文件用
#!/bin/bash
#Filename:chk_mysql.sh
MYSQL_SOCK="/var/lib/mysql/mysql.sock"
MYSQL_USER='zbx_monitor'
MYSQL_PASSWORD='Javaer123456'
MYSQL_HOST='localhost'
MYSQL_PORT='3306'
MYSQL_Connect="/usr/bin/mysqladmin -u$MYSQL_USER -p$MYSQL_PASSWORD -h$MYSQL_HOST -P$MYSQL_PORT -S$MYSQL_SOCK"
if [ $# -ne 1 ];then
echo "please input one arguement"
fi
case $1 in
Uptime) #查詢當前MySQL本次啓動後的運行統計時間
result=${MYSQL_Connect} status 2>/dev/null | cut -d ":" -f 2 | cut -d " " -f 2
echo $result
;;
Slow_queries) #查看當前慢查詢語句的個數
result=${MYSQL_Connect} extended-status 2>/dev/null | grep -w "Slow_queries" | cut -d "|" -f 3
echo $result
;;
Com_rollback) #執行回滾的個數
result=${MYSQL_Connect} extended-status 2>/dev/null | grep -w "Com_rollback" | cut -d "|" -f 3
echo $result
;;
Questions)
result=${MYSQL_Connect} extended-status 2>/dev/null | grep -w "Questions" | cut -d "|" -f 3
echo $result
;;
Com_commit)
result=${MYSQL_Connect} extended-status 2>/dev/null | grep -w "Com_commit" | cut -d "|" -f 3
echo $result
;;
Bytes_sent) #發送的字節數
result=${MYSQL_Connect} extended-status 2>/dev/null | grep -w "Bytes_sent" | cut -d "|" -f 3
echo $result
;;
Bytes_received) #接受的字節數
result=${MYSQL_Connect} extended-status 2>/dev/null | grep -w "Bytes_received" | cut -d "|" -f 3
echo $result
;;
Com_begin)
result=${MYSQL_Connect} extended-status 2>/dev/null | grep -w "Com_begin" | cut -d "|" -f 3
echo $result
;;
Open_tables) #查看當前打開的表數量
result=${MYSQL_Connect} status 2>/dev/null | cut -d ":" -f 5 | cut -d " " -f 2
echo $result
;;
Threads_connected) #查看當前打開的鏈接數量
result=${MYSQL_Connect} extended-status 2>/dev/null | grep -w "Threads_connected" | cut -d "|" -f 3
echo $result
;;
Threads_cached) #查看線程緩存內的線程數量
result=${MYSQL_Connect} extended-status 2>/dev/null | grep -w "Threads_cached" | cut -d "|" -f 3
echo $result
;;
Threads_created) #查看建立用來處理鏈接的線程數。若是Threads_created較大,可能要增長thread_cache_size值。
result=${MYSQL_Connect} extended-status 2>/dev/null | grep -w "Threads_created" | cut -d "|" -f 3
echo $result
;;
Threads_running) #查看激活的(非睡眠狀態)線程數
result=${MYSQL_Connect} extended-status 2>/dev/null | grep -w "Threads_running" | cut -d "|" -f 3
echo $result
;;
Slow_launch_threads) #查看建立時間超過slow_launch_time秒的線程數
result=${MYSQL_Connect} extended-status 2>/dev/null | grep -w "Slow_launch_threads" | cut -d "|" -f 3
echo $result
;;
Com_select) #查看select語句的執行數
result=${MYSQL_Connect} extended-status 2>/dev/null | grep -w "Com_select" |cut -d "|" -f 3
echo $result
;;
Com_insert) #查看insert語句的執行數
result=${MYSQL_Connect} extended-status 2>/dev/null | grep -w "Com_insert" |cut -d "|" -f 3
echo $result
;;
Com_update) #查看update語句的執行數
result=${MYSQL_Connect} extended-status 2>/dev/null | grep -w "Com_update" | cut -d "|" -f 3
echo $result
;;
Com_delete) #查看delete語句的執行數
result=${MYSQL_Connect} extended-status 2>/dev/null | grep -w "Com_delete" | cut -d "|" -f 3
echo $result
;;
Connections) #查看試圖鏈接到MySQL(無論是否鏈接成功)的鏈接數
result=${MYSQL_Connect} extended-status 2>/dev/null | grep -w "Connections" | cut -d "|" -f 3
echo $result
;;
Table_locks_immediate) #查看當即得到的表的鎖的次數
result=${MYSQL_Connect} extended-status 2>/dev/null | grep -w "Table_locks_immediate" | cut -d "|" -f 3
echo $result
;;
Table_locks_waited) #查看不能當即得到的表的鎖的次數。若是該值較高,而且有性能問題,你應首先優化查詢,而後拆分表或使用複製
result=${MYSQL_Connect} extended-status 2>/dev/null | grep -w "Table_locks_waited" | cut -d "|" -f 3
echo $result
;;
*)
echo "Usage:$0(Uptime|Com_update|Slow_queries|Com_select|Com_rollback|Questions|Com_insert|Com_delete|Com_commit|Bytes_sent|Bytes_received|Com_begin)"
;;
esac
Usage:./chk.sh(Uptime|Com_update|Slow_queries|Com_select|Com_rollback|Questions|Com_insert|Com_delete|Com_commit|Bytes_sent|Bytes_received|Com_begin)
文件路徑
/usr/local/etc/zabbix_agentd.conf.d/chk.sh
腳本在配置文件引入
UserParameter=mysql.status[*],/usr/local/etc/zabbix_agentd.conf.d/chk.sh $1
測試chk.sh 是否有用
zabbix_get -slocalhost -p 10050 -k "mysql.status[Com_update]"
zabbix_get -slocalhost -p 10050 -k "mysql.threads_connected"
新版本zabbix對應的key的形式。
mysql.connections.rate
mysql.threads_connected
result=${MYSQL_Connect} extended-status 2>/dev/null | grep -w 「Threads_connected」 | cut -d 「|」 -f 3
echo $result
單獨配置一個參數mysql.threads_connected
UserParameter=mysql.threads_connected, mysqladmin -uzbx_monitor -p’Javaer123456′ -hlocalhost extended-status | grep -w 「Connections」 | cut -d 「|」 -f 3
zabbix 日誌
cat /tmp/zabbix_server.log