zabbix4.0監控Mysql數據庫

#wget http://repo.zabbix.com/zabbix/3.4/rhel/7/x86_64/zabbix-agent-3.4.11-1.el7.x86_64.rpm

#rpm -ivh zabbix-agent-3.4.11-1.el7.x86_64.rpm

[root@lamp02-salve ~]# egrep -v "#|^$" /etc/zabbix/zabbix_agentd.conf
PidFile=/var/run/zabbix/zabbix_agentd.pid
LogFile=/var/log/zabbix/zabbix_agentd.log
LogFileSize=0
Server=192.168.2.159
ServerActive=127.0.0.1
Hostname=Zabbix server
Include=/etc/zabbix/zabbix_agentd.d/*.conf

agent端檢查mysql腳本

 #vim /etc/zabbix/zabbix_agentd.d/check_mysql 

#!/bin/bash
# 主機地址/IP
MYSQL_HOST='192.168.2.158'
# 端口
MYSQL_PORT='3306'
# 數據連接
MYSQL_CONN="/usr/local/mysql/bin/mysqladmin  -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|C
om_begin)" 
        ;; 
esac

通過mysqladmin獲取mysql運行狀態參數,需要登陸驗證密碼,這裏可以將密碼寫在/etc/my.conf配置文件中如

[client]
host=localhost
user= 'root'

password='123.com'

[root@lamp02-salve zabbix_agentd.d]# chmod o+x /etc/zabbix/zabbix_agentd.d/check_mysql -R

[root@lamp02-salve zabbix_agentd.d]# chown zabbix.zabbix /etc/zabbix/zabbix_agentd.d/check_mysql

[root@lamp02-salve ~]# vim /etc/zabbix/zabbix_agentd.d/userparameter_mysql.conf    #這裏定義一個userparameneter子文件,將監控項key值以及腳本添加進去

UserParamenter=mysql.status[*],/etc/zabbix/zabbix_agentd.d/check_mysql $1
UserParamenter=mysql.ping,HOME=/usr/bin/mysqladmin ping 2>/dev/null | grep -c alive
UserParamenter=mysql.version,/usr/bin/mysql -V

 zabbix web端引入模板

zabbix自帶了mysql監控模板,因此只需要鏈接即可

上圖便是我們mysql監控項,可以剛發現都對應着鍵值,都獲取agent數據,說明監控成功

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