zabbix監控mysql數據庫狀態

編寫check_mysql.sh腳本
用於獲取mysql性能指標數據,你需要修改相應的數據庫信息
vim /usr/local/zabbix/share/zabbix/alertscripts/check_mysql.sh

#!/bin/bash

-------------------------------------------------------------------------------

FileName: check_mysql.sh

Revision: 1.0

Date: 2017/04/24

-------------------------------------------------------------------------------

用戶名

MYSQL_USER='root'

密碼

MYSQL_PWD='BiJX!Rrcr2G1yAO8'

主機地址/IP

MYSQL_HOST='localhost'

端口

MYSQL_PORT='3306'

數據連接

MYSQL_CONN="/usr/local/mariadb/bin/mysqladmin -u${MYSQL_USER} -p${MYSQL_PWD} -h${MYSQL_HOST} -P${MYSQL_PORT}"

參數是否正確

if [ $# -ne "1" ];then
echo "arg error!"
fi

獲取數據

case $1 in
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
;;

    *) 
    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

這裏路徑是mysqladmin命令的路徑 /usr/local/mariadb/bin/mysqladmin
whereis mysqladmin 可以搜一下命令在哪裏

● Com_update:mysql執行的更新個數
● Com_select:mysql執行的查詢個數
● Com_insert:mysql執行插入的個數
● Com_delete:執行刪除的個數
● Com_rollback:執行回滾的操作個數
● Bytes_received:接受的字節數
● Bytes_sent:發送的字節數
● Slow_queries:慢查詢語句的個數

chmod a+x !$ 添加腳本可執行權限

修改zabbix_agentd.conf 增加自定義key,在最後一行增加如下:
vim /usr/local/zabbix/etc/zabbix_agentd.conf

獲取mysql版本

UserParameter=mysql.version,mysql -V

獲取mysql性能指標,這個是上面定義好的腳本,注意填寫對腳本路徑

UserParameter=mysql.status[*],/usr/local/zabbix/share/zabbix/alertscripts/check_mysql.sh $1

獲取mysql運行狀態

UserParameter=mysql.ping,mysqladmin -uroot -pBiJX!Rrcr2G1yAO8 -P3306 -h127.0.0.1 ping | grep -c alive

修改mysql配置文件/etc/my.cnf(寫入賬號密碼,否則取值需要交互)

[client]
host = "localhost"
user = "root"
password = "BiJX!Rrcr2G1yAO8"
port = 3306

重啓客戶端
service zabbix_agentd restart

在zabbix的web界面添加監控項並更新,等待出圖

Link MySQL模板
模板是zabbix系統提供的,進入zabbix web後臺,configuration-->hosts-->點擊你的主機name-->選擇template選項卡,選擇模板“Template App MySQL”,最後點擊update即可
常見錯誤解決思路
如果發現監控沒有數據,請排查如下問題

  1. zabbix客戶端是否重啓
  2. 腳本是否有執行權限
  3. 數據庫是否有權限
  4. 環境變量是否有問題
  5. 請看zabbix item列,鼠標移至紅色叉上,有錯誤提示。
  6. 如果數據庫密碼保存在腳本中,會導致監控沒有數據會不斷的報錯爲Warning: Using a password on the command line interface can be insecure.需要將帳號密碼等配置添加到my.cnf中。

由於上面沒有加入mysql併發數的監控,現在添加併發數監控
編輯腳本文件 vim /usr/local/zabbix/share/zabbix/alertscripts/check_mysql.sh
增加取值項
Threads_connected)
result=${MYSQL_CONN} extended-status |grep -w "Threads_connected"|cut -d"|" -f3
echo $result
;;

並修改最後
*)
eche "Usage:$0(Uptime|Com_update|Slow_queries|Com_select|Com_rollback|Questions|Com_insert|Com_delete|Com_commit|Bytes_sent|Bytes_received|Com_begin|Threads_connected)"
;;

重啓客戶端,在web界面添加監控項

name隨便填
key值要跟之前格式相同,可以對照腳本辨別參數

添加完以後:

此時可以在服務端做如下驗證,如果出值正常等待一段時間即可
/usr/local/zabbix/bin/zabbix_get -s 127.0.0.1 -p10050 -k mysql.status[Threads_connected]

http://note.youdao.com/noteshare?id=09e656008bc7ef5b05dd5fd0cd7a00bc&sub=D59A4074D5F040D296B59219153CDF80 我的有道筆記

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章