Shell整理--检查WEB是否正常脚本(附PHP脚本)

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;
}
?>



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值