Zabbix自定义脚本监控MySQL数据库

一、MySQL数据库配置

1.1 创建Mysql数据库用户

root@mysql \~\]# mysql -uroot -p create user zabbix@'127.0.0.1' identified by '123456'; flush privileges;

1.2 添加用户密码到mysql client的配置文件中

root@mysql \~\]# vim /etc/my.cnf.d/client.cnf \[client

host='127.0.0.1'

user='zabbix'

password='123456'

二、创建自定义监控脚本

root@mysql \~\]# vim /etc/zabbix/script/check_mysql.sh

bash 复制代码
#!/bin/bash
#Mysql主机地址
MYSQL_HOST='127.0.0.1'
#Mysql端口
MYSQL_PORT='3306'
#数据连接
MYSQL_CONN="/usr/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 "Unknown options." 
        ;;
esac

三、zabbix agent添加自定义键值

root@mysql \~\]# vim /etc/zabbix/zabbix_agentd.d/userparameter_mysql.conf UserParameter=mysql.status\[\*\],bash /etc/zabbix/script/check_mysql.sh $1 UserParameter=mysql.ping,/usr/bin/mysqladmin ping 2\>/dev/null \| grep -c alive UserParameter=mysql.version,/usr/bin/mysql -V \[root@mysql \~\]# systemctl restart zabbix-agent

四、链接Mysql模板

配置-->主机-->添加模板-->选择"Template DB MySQL"

五、数据查看

相关推荐
HashData酷克数据3 分钟前
官宣:Apache Cloudberry (Incubating) 2.0.0 发布!
数据库·开源·apache·cloudberry
秋难降4 分钟前
SQL 索引突然 “罢工”?快来看看为什么
数据库·后端·sql
TDengine (老段)33 分钟前
TDengine 时间函数 TODAY() 用户手册
大数据·数据库·物联网·oracle·时序数据库·tdengine·涛思数据
码界奇点42 分钟前
KingbaseES一体化架构与多层防护体系如何保障企业级数据库的持续稳定与弹性扩展
数据库·架构·可用性测试
悟乙己1 小时前
数据科学家如何更好地展示自己的能力
大数据·数据库·数据科学家
皆过客,揽星河1 小时前
mysql进阶语法(视图)
数据库·sql·mysql·mysql基础语法·mysql进阶语法·视图创建修改删除
tuokuac2 小时前
Redis 的相关文件作用
数据库·redis·缓存
鹧鸪云光伏与储能软件开发3 小时前
投资储能项目能赚多少钱?小程序帮你测算
运维·数据库·小程序·光伏·光伏设计软件·光伏设计
2301_779503765 小时前
MySQL主从同步--主从复制进阶
数据库·mysql
beijingliushao5 小时前
58-正则表达式
数据库·python·mysql·正则表达式