shell脚本编程-例子_服务器存活监控

检测服务器存活事日常运维工作中很重要也是很基础的服务监控服务,最简单的方法是使用ping命令检测.

具体代码如下:

#!/bin/bash
TIMESTAMP=`date +%Y%m%d%H%M%S`
CURRENT_HTML=/var/www/html/${TIMESTAMP}.html
CURRENT_INDEX=/var/www/html/index.html
LN=/bin/ln
RM=/bin/rm
SERVER_LIST=server_list
cat <<EOF > $CURRENT_HTML
<html>
<head>
<title>Server Alive Monitor</title>
</head>
<body>
<table width="50%" border="1" cellpading="1" cellspaceing="0" >
<caption><h2>Server Alive Status</h2></caption>
<tr><th>Server IP</th><th>Server Status</th></tr>
EOF
while read SERVERS
do
#如果ping的结果返回0则状态ok的,同时显示颜色为blue
#如果ping的结果返回非0则状态FALSE的,同时显示字体的颜色为red
#red
ping $SERVERS -c 3
if [ $? -eq 0 ];then
	STATUS=OK
	COLOR=blue
else
	STATUS=FALSE
	COLOR=red
fi
echo "<tr><td>$SERVERS</td><td><font color=$COLOR>$STATUS</font></td></tr>" >> $CURRENT_HTML
done < $SERVER_LIST
cat <<EOF >> $CURRENT_HTML
</table>
</body>
</html>
EOF
#将web根目录中的主文件连接到新生成的页面
$LN -sf $CURRENT_HTML $CURRENT_INDEX

  最后还需要创建server_list文件:

192.168.1.1
192.168.88.1
61.135.169.125
61.135.157.156
61.135.157.154

正常执行后可显示如下结果(apache服务需要启动)

posted @ 2015-10-14 23:09  超超xc  Views(426)  Comments(0Edit  收藏  举报
I suppose,were childrenonec.