zabbix监控mysql

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了zabbix监控mysql相关的知识,希望对你有一定的参考价值。

zabbix添加mysql监控:

1、建立hosts、关联mysql模板




技术分享

技术分享







2、配置zabbix_agent
vim /etc/zabbix/zabbix_agentd.conf
    UserParameter=mysql.version,mysql -V
    UserParameter=mysql.status[*],/usr/local/zabbix/scripts/chk_mysql.sh $1
    UserParameter=mysql.ping,mysqladmin -uzabbix -p123456 -P3306 -h127.0.0.1  ping | grep -c alive   


3、数据库添加账号:
mysql -uroot -p
    GRANT PROCESS,SUPER,REPLICATION CLIENT ON *.* TO [email protected]‘127.0.0.1‘ IDENTIFIED BY ‘123456‘;

4、写check脚本
mkdir -p /usr/local/zabbix/scripts
vim /usr/local/zabbix/scripts/chk_mysql
   

#!/bin/sh


MYSQL_SOCK="/usr/local/mysql/mysql.sock" 

MYSQL_USER=‘zabbix‘

MYSQL_PWD=‘123456‘

MYSQL_HOST=‘127.0.0.1‘

MYSQL_PORT=‘3306‘

ARGS=1 

if [ $# -ne "$ARGS" ];then 

    echo "Please input one arguement:" 

fi 

case $1 in 

    Uptime) 

        result=`/usr/local/mysql/bin/mysqladmin -u$MYSQL_USER -h$MYSQL_HOST -p${MYSQL_PWD} -S $MYSQL_SOCK status|cut -f2 -d":"|cut -f1 -d"T"` 

            echo $result 

            ;; 

        Com_update) 

            result=`/usr/local/mysql/bin/mysqladmin -u$MYSQL_USER -h$MYSQL_HOST -p${MYSQL_PWD} -S $MYSQL_SOCK extended-status |grep -w "Com_update"|cut -d"|" -f3` 

            echo $result 

            ;; 

        Slow_queries) 

        result=`/usr/local/mysql/bin/mysqladmin -u$MYSQL_USER -h$MYSQL_HOST -p${MYSQL_PWD} -S $MYSQL_SOCK status |cut -f5 -d":"|cut -f1 -d"O"` 

                echo $result 

                ;; 

    Com_select) 

        result=`/usr/local/mysql/bin/mysqladmin -u$MYSQL_USER -h$MYSQL_HOST -p${MYSQL_PWD} -S $MYSQL_SOCK extended-status |grep -w "Com_select"|cut -d"|" -f3` 

                echo $result 

                ;; 

    Com_rollback) 

        result=`/usr/local/mysql/bin/mysqladmin -u$MYSQL_USER -h$MYSQL_HOST -p${MYSQL_PWD} -S $MYSQL_SOCK extended-status |grep -w "Com_rollback"|cut -d"|" -f3` 

                echo $result 

                ;; 

    Questions) 

        result=`/usr/local/mysql/bin/mysqladmin -u$MYSQL_USER -h$MYSQL_HOST -p${MYSQL_PWD} -S $MYSQL_SOCK status|cut -f4 -d":"|cut -f1 -d"S"` 

                echo $result 

                ;; 

    Com_insert) 

        result=`/usr/local/mysql/bin/mysqladmin -u$MYSQL_USER -h$MYSQL_HOST -p${MYSQL_PWD} -S $MYSQL_SOCK extended-status |grep -w "Com_insert"|cut -d"|" -f3` 

                echo $result 

                ;; 

    Com_delete) 

        result=`/usr/local/mysql/bin/mysqladmin -u$MYSQL_USER -h$MYSQL_HOST -p${MYSQL_PWD} -S $MYSQL_SOCK extended-status |grep -w "Com_delete"|cut -d"|" -f3` 

                echo $result 

                ;; 

    Com_commit) 

        result=`/usr/local/mysql/bin/mysqladmin -u$MYSQL_USER -h$MYSQL_HOST -p${MYSQL_PWD} -S $MYSQL_SOCK extended-status |grep -w "Com_commit"|cut -d"|" -f3` 

                echo $result 

                ;; 

    Bytes_sent) 

        result=`/usr/local/mysql/bin/mysqladmin -u$MYSQL_USER -h$MYSQL_HOST -p${MYSQL_PWD} -S $MYSQL_SOCK extended-status |grep -w "Bytes_sent" |cut -d"|" -f3` 

                echo $result 

                ;; 

    Bytes_received) 

        result=`/usr/local/mysql/bin/mysqladmin -u$MYSQL_USER -h$MYSQL_HOST -p${MYSQL_PWD} -S $MYSQL_SOCK extended-status |grep -w "Bytes_received" |cut -d"|" -f3` 

                echo $result 

                ;; 

    Com_begin) 

        result=`/usr/local/mysql/bin/mysqladmin -u$MYSQL_USER -h$MYSQL_HOST -p${MYSQL_PWD} -S $MYSQL_SOCK extended-status |grep -w "Com_begin"|cut -d"|" -f3` 

                echo $result 

                ;; 

                        

        *) 

        echo "Usage:$0(Uptime|Com_update|Slow_queries|Com_select|Com_rollback|Questions)" 

        ;; 

esac



别忘了脚本授权:
chmod u+x /usr/local/zabbix/chk_mysql.sh
chown -R zabbix.zabbix /usr/local/zabbix/chk_mysql.sh


5、脚本准备完后,重启agent

5、验证
在zabbix-server服务器上测试是否可以获取到mysql的status信息:
    zabbix_get -s 127.0.0.1 -p10050 -k mysql.status[Com_update]
有结果就是ok的!

PS:zabbix_get命令:
zabbix_get -s 10.8.95.74 -p10050 -k "system.cpu.load[all,avg15]";



本文出自 “10620401” 博客,请务必保留此出处http://10630401.blog.51cto.com/10620401/1978809

以上是关于zabbix监控mysql的主要内容,如果未能解决你的问题,请参考以下文章

Contos7 安装 Zabbix 监控 Mysql

Ubuntu下Zabbix服务器监控工具部署

zabbix实现对nginx监控并实现nginx服务自愈

zabbix - 2

Zabbix 监控服务介绍

zabbix安装后怎么添加监控