zabbix监控mysql

zabbix 2.2版本及以上,server中自带mysql模板, Template App MySQL

一:web端配置

在页面上添加监控机器时,关联上模板 Template App MySQL 即可。

二:客户端

在做以下的工作前提是:zabbix agent已配置好并可正常工作。

1:zabbix agent的配置文件中添加以下三行

UserParameter=mysql.status[*],/etc/zabbix/check_mysql.sh $1

UserParameter=mysql.version,mysql -V

UserParameter=mysql.ping,HOME=/var/lib/zabbix mysqladmin ping | grep -c alive

2:其中/etc/zabbix/check_mysql.sh 脚本的内容如下

#!/bin/bash

# 数据连接

MYSQL_CONN="mysqladmin"

# 参数是否正确

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

3:mysql配置

可以看到上面执行mysqladmin命令时,没有用户名,密码,机器以及端口。因为mysql 5.6及以上版本如果在命令行中直接输入密码,会提示不安全。所以这些信息需要写在mysql配置文件中。

[client]

host=xx.xx.xx.xx

port=3306

user=zabbix

password=zabbix

然后重启mysql服务即可

service mysql restart


三:重启客户端

service zabbix-agent restart


页面上过一会就会有数据了。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容