zabbix监控MySQL
来源:原创
时间:2019-03-30
作者:脚本小站
分类:Linux
在mysql上安装agent:下面是配置文件
PidFile=/var/run/zabbix/zabbix_agentd.pid LogFile=/var/log/zabbix/zabbix_agentd.log LogFileSize=0 Server=192.168.1.119 ServerActive=192.168.1.119 Hostname=mysql01 Include=/etc/zabbix/zabbix_agentd.d/*.conf UnsafeUserParameters=1 # 开启脚本监控 HostMetadataItem=system.uname
在配置文件中添加参数:
vim /etc/zabbix/zabbix_agentd.d/userparameter_mysql.conf UserParameter=mysql.check[*],/var/opt/check_mysql.sh $1 2> /dev/null
监控脚本:
/var/opt/check_mysql.sh
check_mysql.sh
# 用户名
MYSQL_USER='root'
# 密码
MYSQL_PWD='123456'
# 主机地址/IP
MYSQL_HOST='192.168.1.111'
# 端口
MYSQL_PORT='3306'
# 数据连接
MYSQL_CONN="/usr/bin/mysqladmin -u${MYSQL_USER} -p${MYSQL_PWD} -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"`
;;
Com_update)
result=`${MYSQL_CONN} extended-status |grep -w "Com_update"|cut -d"|" -f3`
;;
Slow_queries)
result=`${MYSQL_CONN} status |cut -f5 -d":"|cut -f1 -d"O"`
;;
Com_select)
result=`${MYSQL_CONN} extended-status |grep -w "Com_select"|cut -d"|" -f3`
;;
Com_rollback)
result=`${MYSQL_CONN} extended-status |grep -w "Com_rollback"|cut -d"|" -f3`
;;
Questions)
result=`${MYSQL_CONN} status|cut -f4 -d":"|cut -f1 -d"S"`
;;
Com_insert)
result=`${MYSQL_CONN} extended-status |grep -w "Com_insert"|cut -d"|" -f3`
;;
Com_delete)
result=`${MYSQL_CONN} extended-status |grep -w "Com_delete"|cut -d"|" -f3`
;;
Com_commit)
result=`${MYSQL_CONN} extended-status |grep -w "Com_commit"|cut -d"|" -f3`
;;
Bytes_sent)
result=`${MYSQL_CONN} extended-status |grep -w "Bytes_sent" |cut -d"|" -f3`
;;
Bytes_received)
result=`${MYSQL_CONN} extended-status |grep -w "Bytes_received" |cut -d"|" -f3`
;;
Com_begin)
result=`${MYSQL_CONN} extended-status |grep -w "Com_begin"|cut -d"|" -f3`
;;
*)
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
echo $result添加权限:
chmod +x check_mysql.sh
重启zabbix-agent:
systemctl restart zabbix-agent.service
在zabbix server上测试:
zabbix_get -s 192.168.1.111 -k mysql.check[Com_select]
然后在web页面添加action来自动注册或手动添加到监控。
