时间:2021-07-01 10:21:17 帮助过:7人阅读
zabbix默认会带有对MySQL的监控模板(Template App MySQL),只是不能直接使用,需要在被监控机进行一些设置;
一、需要写一个脚本放在被监控机上,这个也就是与自定义监控差不多,只是模板用zabbix自带的;
cat zabbix_mysql_status.sh
#!/bin/sh #Create by feixiangdada 2015.12.17 MYSQL_SOCK="/tmp/mysql.sock" ARGS=1 if [ $# -ne "$ARGS" ];then echo "Please input one arguement:" fi case $1 in Uptime) result=`/usr/local/mysql/bin/mysqladmin -S $MYSQL_SOCK status|cut -f2 -d":"|cut -f1 -d"T"` echo $result ;; Com_update) result=`/usr/local/mysql/bin/mysqladmin -S $MYSQL_SOCK extended-status |grep -w "Com_update"|cut -d"|" -f3` echo $result ;; Slow_queries) result=`/usr/local/mysql/bin/mysqladmin -S $MYSQL_SOCK status |cut -f5 -d":"|cut -f1 -d"O"` echo $result ;; Com_select) result=`/usr/local/mysql/bin/mysqladmin -S $MYSQL_SOCK extended-status |grep -w "Com_select"|cut -d"|" -f3` echo $result ;; Com_rollback) result=`/usr/local/mysql/bin/mysqladmin -S $MYSQL_SOCK extended-status |grep -w "Com_rollback"|cut -d"|" -f3` echo $result ;; Questions) result=`/usr/local/mysql/bin/mysqladmin -S $MYSQL_SOCK status|cut -f4 -d":"|cut -f1 -d"S"` echo $result ;; Com_insert) result=`/usr/local/mysql/bin/mysqladmin -S $MYSQL_SOCK extended-status |grep -w "Com_insert"|cut -d"|" -f3` echo $result ;; Com_delete) result=`/usr/local/mysql/bin/mysqladmin -S $MYSQL_SOCK extended-status |grep -w "Com_delete"|cut -d"|" -f3` echo $result ;; Com_commit) result=`/usr/local/mysql/bin/mysqladmin -S $MYSQL_SOCK extended-status |grep -w "Com_commit"|cut -d"|" -f3` echo $result ;; Bytes_sent) result=`/usr/local/mysql/bin/mysqladmin -S $MYSQL_SOCK extended-status |grep -w "Bytes_sent" |cut -d"|" -f3` echo $result ;; Bytes_received) result=`/usr/local/mysql/bin/mysqladmin -S $MYSQL_SOCK extended-status |grep -w "Bytes_received" |cut -d"|" -f3` echo $result ;; Com_begin) result=`/usr/local/mysql/bin/mysqladmin -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
此脚本中需要注意的是:
1. mysql.sock的路径,根据自己的路径进行修改;
2. 对my.cnf进行配置:
在 my.cnf 中加入下面的即可:
[mysqladmin] host=localhost user=zabbix #用户修改成自己的 password=zabbix #密码需要对上用户
修改后需要重启MySQL(若是5.5以下可以忽略这步)
二、 对被控制机上的 zabbix_agentd.conf 添加如下内容:
UserParameter=mysql.version,mysql -V UserParameter=mysql.ping,mysqladmin -uzabbix -pzabbix -S /tmp/mysql.sock ping | grep -c alive UserParameter=mysql.status[*],/usr/local/zabbix/share/zabbix/alertscripts/mysql_status.sh $1 $2
重启 zabbix_agentd服务:
service zabbix_agentd restart
三、 在zabbix的监控上添加MySQL的模板(Template App MySQL)即可;
效果图如下:
本文出自 “浅浅的淡淡” 博客,请务必保留此出处http://cuixiang.blog.51cto.com/8204722/1725906
zabbix监控MySQL
标签:zabbix mysql