http://www.ohlinux.com/archives/273/
前言:自从我们的网站用了nginx+fastcgi后,出了不少的问题,尤其在那一段时间内。出现的问题头疼之一的就是502,nginx并没有死,但fastcgi却死掉了,所以单单查看80端口是不够的,
另外我们的系统都是集群的,某一个WEB出现问题也是不好发现的(在没有加监控之前),所以当时好几个同事都写了相同功能的程序,我对SHELL熟悉,写了shell脚本,有同事是写PHP的 利用curl写了PHP的程序,还有利用shell结合curl写的,而我觉得curl要必须安装才有,所以我写了一个wget 的 利用最基本的系统命令就可以完成。
#!/bin/sh
# -------------------------------------------------------------------------------
# Filename: check_web.sh
# Revision: 1.2
# Date: 2008/12/15
# Author: Ajian
# Email: ajian521#gmail.com
# Website: www.ohlinux.com
# Description: check the web is right or wrong ,eg: nginx+fastcgi
# Notes: can check one or many sites
# -------------------------------------------------------------------------------
# Copyright: 2009 (c) Ajian
# License: GPL
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty
# of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# -------------------------------------------------------------------------------
#Version 1.1
#Can check the web is working right or wrong
#Version 1.2
#Add the color of the dscription and print more information
HOSTLIST=hosts.list
OUTFILE=/tmp/check_log.txt
#function: shellUsage()
#shellUsage: How to use the shell script.
#return: 0
shellUsage()
{
#shellUsage
echo "Usage :`basename $0` [-f HostFile]||[-h domain_or_IP]"
echo "try `basename $0` -help for more info"
exit 1
}
while getopts f:h:o: OPTION ; do
case $OPTION in
f)HOSTLIST="$OPTARG"
if [ ! -f $HOSTLIST ]; then
echo "$HOSTLIST can not find out!"
shellUsage
fi
;;
o)OUTFILE="$OPTARG"
;;
h)Host="$OPTARG"
;;
*)shellUsage
;;
esac
done
#Function checkHttp()
checkHttp()
{
wget --server-response --spider http://$Host -o $OUTFILE
echo "\033[44;37;5m Host: \033[0m \033[32;49;1m $Host \033[39;49;0m"
echo "==================================="
grep '^[ ]' $OUTFILE
echo "==================================="
}
#Function checkMain()
checkMain()
{
for Host in `cat "$HOSTLIST"`
do
checkHttp
done
}
if [ "$Host" = "" ]; then
checkMain
else
checkHttp
fi
附上小刘的PHP脚本,后来他还加入了gtalk的消息发送
<?php
$cmd = "cat /etc/hosts | grep -v 'pma.' | grep -v 'svn.' | grep 'imobile.com.cn'";
$out = shell_exec($cmd);
$hosts = explode("\n", $out);
$cnt = count($hosts) - 1;
for($i = 0; $i < $cnt; $i++)
{
list($ip, $host) = explode(" ", $hosts[$i]);
if (test_server_status($host))
{
echo "OK -- {$ip} {$host}";
}
else
{
echo "ERROR -- {$ip} {$host}";
}
echo "\n";
}
function test_server_status($host)
{
$ptn0 = 'HTTP/1.0 200 OK';
$ptn1 = 'HTTP/1.1 200 OK';
$cmd = '/usr/bin/curl -I ' . $host;
$out = shell_exec($cmd);
if (strpos($out, $ptn0) !== false)
{
return true;
}
if (strpos($out, $ptn1) !== false)
{
return true;
}
return false;
}
?>