2011-08-09 120 views
2

这是脚本检查,如果服务器是开/关迭代MySQL的结果进行比较

$ip = "xxxxxxxx"; 
$port = "xxx"; 
$fp = @fsockopen("udp://$ip", $port, $errno, $errstr, 1); 
socket_set_timeout($fp, 000002); 
if (!$fp) 
{ 
    echo "OFFLINE"; 
} 
else 
{ 
    echo "ONLINE"; 
} 
@fclose($fp); 

MySQL表

id | ip | port  | online_status | 
1  | ip1 | port1  |    | 
2  | ip2 | port2  |    | 
3  | ip3 | port3  |    | 
4  | ip4 | port4  |    | 
5  | ip5 | port5  |    | 

我需要检查每一个服务器,如果它的开/关在同一时间并写在表中。
我该怎么做?

$servers = $db->getArray("SELECT * FROM servers"); 

回答

4

尝试通过阵列循环并尝试连接到每个服务器。这会告诉你,如果你可以连接到服务器。然后,更新您的表以包含状态。

/* Loop through the servers */ 
foreach($servers as $server) 
{ 
    $id = $server['id']; 
    $ip = $server['ip']; 
    $port = $server['port']; 
    /* Try to connect to this particular server */ 
    $fp = @fsocketopen("udp://$ip", $port, $errno, $errstr, 1); 

    socket_set_timeout($fp, 000002); 

    $status = ''; 
    /* Determine the status of the server */ 
    if(!$fp) 
    { 
     $status = 'OFFLINE'; 
    } 
    else 
    { 
     $status = 'ONLINE'; 
    } 
    /* Close our connection */ 
    @fclose($fp); 

    /* Update the servers table to show the status of the server */ 
    $db->Update("UPDATE servers SET online_status='$status' WHERE id='$id'"); 
}