Rumah > Artikel > pangkalan data > zabbix企业应用之Mysql主从监控_MySQL
现在我在给大家介绍一下如何监控mysql主从状态的。
下面是监控效果图:
1、Mysql Slave Status of 3306 Port
这个也是使用low level discovery进行监控,并且已经跟之前的lld监控mysql的模板合成了一个模板(所以我本文就只介绍mysql主从状态,其他的性能,这样使用一个模板既能监控mysql性能,又能自动的监控mysql主从状态,如果io或者sql任何一个状态不为yew,就会报警。
如何实现:
一、客户端
1、监控内容的json化
在客户端里,需要把监控的内容json化展示,然后服务端可以通过正则来过滤出结果
下面在是我的json监控mysql的json展示
脚本内容如下:
<ol class="dp-rb"> <li class="alt"><span><span class="comment">#!/bin/bash </span><span> </span></span></li> <li> <span class="comment">#Fucation:mysql low-level discovery </span><span> </span> </li> <li class="alt"> <span class="comment">#Script_name mysql_low_discovery.sh </span><span> </span> </li> <li><span>mysql() { </span></li> <li class="alt"> <span> port=($(sudo netstat -tpln | awk -F </span><span class="string">"[ :]+"</span><span> </span><span class="string">'/[m]ysql/ && /0.0.0.0/ {print $5}'</span><span>)) </span> </li> <li> <span> printf </span><span class="string">'{\n'</span><span> </span> </li> <li class="alt"> <span> printf </span><span class="string">'\t"data":[\n'</span><span> </span> </li> <li> <span> </span><span class="keyword">for</span><span> key </span><span class="keyword">in</span><span> ${!port[@]} </span> </li> <li class="alt"> <span> </span><span class="keyword">do</span><span> </span> </li> <li> <span> </span><span class="keyword">if</span><span> [[ </span><span class="string">"${#port[@]}"</span><span> -gt 1 && </span><span class="string">"${key}"</span><span> -ne </span><span class="string">"$((${#port[@]}-1))"</span><span> ]];</span><span class="keyword">then</span><span> </span> </li> <li class="alt"> <span> socket=`ps aux|grep ${port[${key}]}|grep -v grep|awk -F </span><span class="string">'='</span><span> </span><span class="string">'{print $10}'</span><span>|cut -d </span><span class="string">' '</span><span> -f 1` </span> </li> <li> <span> printf </span><span class="string">'\t {\n'</span><span> </span> </li> <li class="alt"> <span> printf </span><span class="string">"\t\t\t\"{#MYSQLPORT}\":\"${port[${key}]}\"},\n"</span><span> </span> </li> <li> <span> </span><span class="keyword">else</span><span> [[ </span><span class="string">"${key}"</span><span> -eq </span><span class="string">"((${#port[@]}-1))"</span><span> ]] </span> </li> <li class="alt"> <span> socket=`ps aux|grep ${port[${key}]}|grep -v grep|awk -F </span><span class="string">'='</span><span> </span><span class="string">'{print $10}'</span><span>|cut -d </span><span class="string">' '</span><span> -f 1` </span> </li> <li> <span> printf </span><span class="string">'\t {\n'</span><span> </span> </li> <li class="alt"> <span> printf </span><span class="string">"\t\t\t\"{#MYSQLPORT}\":\"${port[${key}]}\"}\n"</span><span> </span> </li> <li><span> fi </span></li> <li class="alt"><span> done </span></li> <li> <span> printf </span><span class="string">'\t ]\n'</span><span> </span> </li> <li class="alt"> <span> printf </span><span class="string">'}\n'</span><span> </span> </li> <li><span>} </span></li> <li class="alt"> <span class="variable">$1</span><span> </span> </li> </ol>
把此文件存放到/usr/local/zabbix/bin里,然后给与755权限,并修改用户与组为zabbix,同时允许zabbix用户无密码运行netstat
<ol class="dp-rb"><li class="alt"><span><span>echo </span><span class="string">"zabbix ALL=(root) NOPASSWD:/bin/netstat"</span><span>>>/etc/sudoers </span></span></li></ol>
#Disable requiretty
<ol class="dp-rb"><li class="alt"><span><span>sed -i </span><span class="string">'s/^Defaults.*.requiretty/#Defaults requiretty/'</span><span> /etc/sudoers </span></span></li></ol>
不关闭的话,会无法获取数据,并且zabbix日志里报
2、修改zabbix_agentd.conf文件
在zabbix_agentd.conf最后添加以下内容
<ol class="dp-rb"> <li class="alt"><span><span>UserParameter=zabbix_low_discovery[*],/bin/bash /usr/local/zabbix/bin/mysql_low_discovery.sh </span><span class="variable">$1</span><span> </span></span></li> <li> <span>UserParameter=mysql_stats[*],mysql -h localhost -P </span><span class="variable">$1</span><span> -uzabbix -pzabbix -e </span><span class="string">"show global status"</span><span>|grep </span><span class="string">"\"</span><span>|cut -f2 </span> </li> <li class="alt"> <span>UserParameter=mysql_stats_slave[*],mysql -h localhost -P </span><span class="variable">$1</span><span> -uzabbix -pzabbix -e </span><span class="string">"show slave status\G"</span><span>|grep </span><span class="string">"\"</span><span>|awk </span><span class="string">'{if($NF=="Yes") {print 1} else {print 0}}'</span><span> </span> </li> </ol>
3、测试
如果你能通过/usr/local/zabbix/bin/zabbix_get -s 127.0.0.1 -k zabbix_low_discovery[mysql]与/usr/local/zabbix/bin/zabbix_get -s 127.0.0.1 -k mysql_stats_slave[3306,Slave_IO_Running]、/usr/local/zabbix/bin/zabbix_get -s 127.0.0.1 -k mysql_stats_slave[3306,Slave_SQL_Running]命令获取数据就代表没问题,下面是我的测试结果
4、mysql授权
<ol class="dp-rb"> <li class="alt"><span><span>GRANT PROCESS,REPLICATION CLIENT ON *.* TO </span><span class="string">'zabbix'</span><span>@</span><span class="string">'localhost'</span><span> identified BY </span><span class="string">'zabbix'</span><span>; </span></span></li> <li><span>Flush privileges; </span></li> </ol>
5、重启zabbix服务
<ol class="dp-rb"> <li class="alt"><span><span>ps -ef|grep zabbix|grep -v grep|awk </span><span class="string">'{print $2}'</span><span>|xargs kill -9 </span></span></li> <li><span>/usr/local/zabbix/sbin/zabbix_agentd -c /usr/local/zabbix/conf/zabbix_agentd.conf </span></li> </ol>
二、服务端
1、模板导入
把Template Mysql Auto Discovery导入到zabbix里(模板在附件),具体操作不介绍。
2、设置正则
在“管理”=》“一般”=》“正则表达式”里,选择“新的正则表达式”
然后设置类似如下
我这是运行3301到3309这9个mysql实例
3、主机关联模板
把需要监控mysql实例的主机管理模板即可监控,默认是3600秒后自动更新,如下图
为什么设置3600秒?原因是如果设置时间过短,比如你设置60s,一个是服务器的压力大,一个是如果你检测的端口突然当掉了,还没有来的急报警,主机通过json来获取不到这个信息,就认为没有这个端口,模板里会自动关闭这个监控项的内容(默认保留30天,然后自动删除),这样报警功能就基本没有了,所以还是发现的间隔时间长一些比较好。