<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0">
<channel>
<title><![CDATA[静怡家园]]></title> 
<link>http://www.zhanghaijun.com/index.php</link> 
<description><![CDATA[书山有路勤为径，学海无涯苦作舟！]]></description> 
<language>zh-cn</language> 
<copyright><![CDATA[静怡家园]]></copyright>
<item>
<link>http://www.zhanghaijun.com/post//</link>
<title><![CDATA[shell脚本监控mysql服务器状态]]></title> 
<author>碟舞飞扬 &lt;webmaster@zhanghaijun.com&gt;</author>
<category><![CDATA[数据库类]]></category>
<pubDate>Mon, 27 Feb 2012 02:35:51 +0000</pubDate> 
<guid>http://www.zhanghaijun.com/post//</guid> 
<description>
<![CDATA[ 
	目的：<br/>1.监控mysql服务器的状态<br/>2.当发现mysql down机就自动重启mysql服务<br/>3.重启mysql不成功，发邮件给管理员警告mysql down机<br/><br/>#vi /usr/local/sbin/check_mysql.sh<br/><div class="code">#!/bin/sh<br/>cat &lt;&lt; EOF<br/>+--------------------------------------------------------------------------+<br/>&#124;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=== Welcome to LinuxTone===&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#124;<br/>&#124;-------------------------http://www.linuxtone.org------------------------ &#124;<br/>+--------------------------------------------------------------------------+<br/>EOF<br/><br/>PORT=`netstat -na&#124;grep &quot;LISTEN&quot;&#124;grep &quot;3306&quot;&#124;awk -F&#91;:&quot; &quot;&#93;+ &#039;&#123;print $5&#125;&#039;`<br/>MYSQLIP=`ifconfig eth0&#124;awk &#039;/inet/&#123;print $2&#125;&#039;&#124;cut -c 6-`<br/>while &#91; `whoami` == &quot;root&quot; &#93;<br/>do<br/>&nbsp;&nbsp;if &#91; &quot;$PORT&quot; == &quot;3306&quot; &#93;;then<br/>&nbsp;&nbsp;&nbsp;&nbsp;echo &quot;mysql is running......&quot;<br/>&nbsp;&nbsp;else<br/>&nbsp;&nbsp;&nbsp;&nbsp;echo &quot;restart mysql&quot;<br/>&nbsp;&nbsp;&nbsp;&nbsp;/etc/init.d/mysqld restart<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if &#91; &quot;$PORT&quot; == &quot;3306&quot; &#93;;then<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; echo &quot;mysql restart successful......&quot; <br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; else<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; echo &quot;mysql restart failure......&quot;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; echo &quot;server: $MYSQLIP mysql is down,please try to restart mysql!&quot; &gt; /var/log/mymsg<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; mail -s &quot;warn!server: $MYSQLIP&nbsp;&nbsp;mysql is down&quot; hamgua&#92;@gmail.com &lt; /var/log/mymsg<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; fi<br/>&nbsp;&nbsp;fi<br/>break<br/>done</div><br/>赋予check_mysql.sh可执行的权限<br/>#chmod u+x /usr/local/sbin/check_mysql.sh<br/><br/>加入crontab，让系统五分钟检测一次mysql状态<br/>#crontab -l<br/>*/5 * * * * /usr/local/sbin/check_mysql.sh > /dev/null 2>&1<br/>Tags - <a href="http://www.zhanghaijun.com/tags/mysql/" rel="tag">mysql</a> , <a href="http://www.zhanghaijun.com/tags/shell/" rel="tag">shell</a>
]]>
</description>
</item><item>
<link>http://www.zhanghaijun.com/post//#blogcomment</link>
<title><![CDATA[[评论] shell脚本监控mysql服务器状态]]></title> 
<author> &lt;user@domain.com&gt;</author>
<category><![CDATA[评论]]></category>
<pubDate>Thu, 01 Jan 1970 00:00:00 +0000</pubDate> 
<guid>http://www.zhanghaijun.com/post//#blogcomment</guid> 
<description>
<![CDATA[ 
	
]]>
</description>
</item>
</channel>
</rss>