PHP监控服务器运维状况并短信告警

admin2012-06-21linux51
"192.168.254.11","port"=>"23","protocol"=>"tcp"),
                array("IP"=>"192.168.254.12","port"=>"23","protocol"=>"tcp"),
                array("IP"=>"192.168.254.13","port"=>"443","protocol"=>"tcp"),
                array("IP"=>"192.168.254.101","port"=>"3389","protocol"=>"tcp"),
                array("IP"=>"192.168.254.101","port"=>"1521","protocol"=>"tcp"),
                array("IP"=>"192.168.254.102","port"=>"3389","protocol"=>"tcp"),
                array("IP"=>"192.168.254.102","port"=>"8443","protocol"=>"tcp"),
                array("IP"=>"192.168.254.103","port"=>"2008","protocol"=>"tcp"),
                array("IP"=>"192.168.254.156","port"=>"3389","protocol"=>"tcp"),
                array("IP"=>"192.168.254.156","port"=>"514","protocol"=>"udp")
                //array("IP"=>"127.0.0.1","port"=>"9999","protocol"=>"tcp")
            ) ;
$PingCheckList    = array(//Ping检测需要提供的服务器IP
                        "192.168.254.11",
                        "192.168.254.12",
                        "192.168.254.13",
                        "192.168.254.101",
                        "192.168.254.102",
                        "192.168.254.103",
                        "192.168.254.156"
                        );

/**
 * 服务器端口检测:超时30秒,检测10次
 *
 * @param unknown_type $targetIP
 * @param unknown_type $targetPort
 * @param unknown_type $Protocol
 * @param unknown_type $echoChar
 * @param unknown_type $timeout
 * @param unknown_type $number
 * @return unknown
 */
function PortCheck($targetIP,$targetPort,$Protocol="tcp",$echoChar=NULL,$timeout=30,$number=4){
    $i     = 0;
    $j    = 0;
    while ($i++<$number) {
        $fp = @fsockopen("$Protocol://$targetIP",$targetPort, &$errno, &$errstr,$timeout); 
        if (!$fp) {
            $j++;
        }
        else{
            fclose($fp);
            return 1;
        }
    }
    //echo $j;
    if ($j > $number/2) {
        $str = "{$targetIP}端口{$targetPort}/{$Protocol}连接失败{$j}次!"."/".date_format(date_create('Asia/Shanghai'),'Y-m-d/H:i:s');
        SMS($str);
        return $j;
    }
    
}

/**
 * 服务器Ping状态检测,要求丢包率<60%
 *
 * @param unknown_type $targetIP
 * @param unknown_type $number
 */
function PingCheck($targetIP,$number=10){
    exec("ping  {$targetIP} -n {$number}",$result);
    $ping_result = '';
    foreach ($result as $value) {
        $ping_result .= $value;
    }
    if (preg_match('/\((\d+)% loss\),/',$ping_result,$matches)>0){
        $loss    =    $matches[1];
        if($loss > 60){//丢包率大于60%
            $str = "{$targetIP}当前丢包率:{$loss}%/".date_format(date_create('Asia/Shanghai'),'Y-m-d/H:i:s');
            SMS($str);
        }
    }
    return    "{$loss}%";
}

/**
 * 短信发送模块
 *
 * @param unknown_type $msg
 */
function SMS($msg){
    global $fetionPath;
    global $sender;
    global $receiver;
    global $password;
    $smsString = "\"$fetionPath\" --mobile=$sender --pwd=$password --to=$receiver --msg-gb=$msg";
    //echo $smsString;
    $result    = exec($smsString);
    return 0;
}

/**
 * 服务器状态检测函数
 *
 * @param unknown_type $PingCheckList
 * @param unknown_type $PortCheckList
 * @return unknown
 */
function CheckServerStatus($PingCheckList,$PortCheckList){
    foreach ($PingCheckList as $value) {
        echo "\n-----Ping检测{$value}:   ";
        echo PingCheck($value);
    }
    foreach ($PortCheckList as $server) {
        //var_dump($server);
        echo "\n-----端口检测".$server["IP"]."/".$server["port"]."/".$server["protocol"].":   ";
        echo PortCheck($server["IP"],$server["port"],$server["protocol"]);
    }
    return 0;
}

$i = 1;
while (true) {
    CheckServerStatus($PingCheckList,$PortCheckList);
    //echo $x = '"D:\\fetionrobot\\fetion.exe" --mobile=13811111111 --pwd=123456 --to=13822222222 --msg-gb=127.0.0.1端口9999/tcp连接失败10次!/2009-03-27 02:08:45';
    //$result    = exec($x,$result);
    //var_dump($result);
    echo "\n当前是第".$i++."轮检测|".date_format(date_create('Asia/Shanghai'),'Y-m-d H:i:s')."\n";
    sleep(1800);
}

?>