zabbix配置监控脚本
1.修改agent配置文件
[root@chang ~]# vim /etc/zabbix/zabbix_agentd.conf
333行 原# UnsafeUserParameters=0 修改成 UnsafeUserParameters=1
2.创建脚本与脚本存放目录
[root@chang ~]# mkdir /etc/zabbix/zabbix_scripts
[root@chang zabbix_scripts]# vim check_mysql.sh
#!/bin/bash#定义Mariadb用户名和密码以及连接地址
DB_USER="root"
DB_PASSWORD="123qqq.A"
DB_HOST="localhost"#检查MariaDB是否正在运行
if pgrep mariadb > /dev/null; thenecho "MariaDB is running."exit 1elseecho "MariaDB is not running."
fi#检查连接数是否超过限制
MAX_CONNECTIONS=100
CURRENT_CONNECTIONS=$(mysql -u root -p123qqq.A -h localhost -e "SHOW STATUS LIKE 'Threads_connected';" |awk '{print $2}'|sed -n '2p')
if [ "$CURRENT_CONNECTIONS" -ge "$MAX_CONNECTIONS" ]; thenecho "Maximum number of connections exceeded ($MAX_CONNECTIONS)."elseecho "The number of connections has not exceeded the limit"
fi#检查磁盘空间是否超过限制
MAX_DISK_USAGE=80
CURRENT_DISK_USAGE=$(df -h /var/lib/mysql/ | tail -1 | awk '{print $5}' | cut -d'%' -f1)
if [ "$CURRENT_DISK_USAGE" -ge "$MAX_DISK_USAGE" ]; thenecho "Disk usage exceeded ($MAX_DISK_USAGE%)."elseecho "The disk space has not exceeded the limit"
fi
3.添加监控的key
[root@chang zabbix]# vim zabbix_agentd.conf
#末尾添加
UserParameter=mysql_check,sh /etc/zabbix/zabbix_scripts/check_mysql.sh
4.重启agent服务
[root@chang zabbix]# systemctl restart zabbix-agent.service
5.web界面配置
点击添加,完成。
等待一会,数据传输过来了。