2017-03-15 33 views
3
<?php 
//Page Variables 
$online='<td style="background-color:#00FF00; padding:5px;">Operational</td>'; 
$offline='<td style="background-color:#FF0000; padding:5px;">Failed</td>'; 

//Functions 
function servercheck($server,$port){ 
    //Check that the port value is not empty 
    if(empty($port)){ 
     $port=80; 
    } 
    //Check that the server value is not empty 
    if(empty($server)){ 
     $server='domain.com'; 
    } 
    //Connection 
    [email protected]($server, $port, $errno, $errstr, 1); 
     //Check if connection is present 
     if($fp){ 
      //Return Alive 
      return 1; 
     } else{ 
      //Return Dead 
      return 0; 
     } 
    //Close Connection 
    fclose($fp); 
} 
//Ports and Services to check 
$services=array( 
'Website Access' => array('domain.com' => 80), 
'Another Service' => array('domain.com' => 443), 
'Another Service' => array('domain.com' => 21), 
); 
?> 

<div class="infobox"> 
<?php 
//Check All Services 
foreach($services as $name => $server){ 
?> 
<tr> 
<td><?php echo $name; ?></td> 
<?php 
foreach($server as $host => $port){ 
    if(servercheck($host,$port)){ echo $online; }else{ echo $offline; } 
} 
?> 
</tr> 
<?php 
} 
?> 
</div> 
<div class="overallmesssage"> 
<h3> 
<!--Need this bit to show green and a message if all servers online if not  show orange for not all of them and red for none of them--> 
</div> 

我想添加一個總體消息,這些消息根據在線服務器而變化。類似於https://demo.cachethq.io/,其中有一個綠色的整體部分告訴訪問者一切正常並且正在運行,但如果其中一臺服務器停機,它將變爲橙色,如果所有服務器都停機,它將變爲紅色。PHP - 如果所有陣列都處於活動狀態,無效狀態或不可用,請顯示消息

回答

0

收集陣列中的您的服務器狀態,然後檢查是否in_array

<?php 
$status = array(); 
foreach($server as $host => $port){ 
    $status[] = servercheck($host,$port); 
} 

if (in_array('0', $status)) { 
    echo $offline; 
} else { 
    echo $online; 
} 

?>

0

你的情況,我寧願另一種方法則直接詢問頁面。在你的位置,我會建立一個cronjob,它會檢查你的服務的每一分鐘,並將狀態保存在數據庫或其他東西中。然後你可以用這些數據構建一個Graph。

但你的情況。爲了實現這一點,我寧願運行一個循環通過你的服務器,然後保存每個失敗的陳述在一個數組中。之後,您可以使用該陣列。所以陣列有一個條目是橙色的。如果你有多個條目你有一個紅色的狀態。或者更容易使用計數器變量。

$errors = 0; 
foreach($server as $host => $port) { 
    if(servercheck($host, $port) == 0) { 
     $errors++; 
    } 
} 

if ($errors == 0) { 
    echo "green!"; 
} elseif ($errors == 1) { 
    echo "orange!"; 
} else { 
    echo "red!"; 
} 

您的完整代碼:

<?php 
//Page Variables 
$online = '<td style="background-color:#00FF00; padding:5px;">Operational</td>'; 
$offline = '<td style="background-color:#FF0000; padding:5px;">Failed</td>'; 

//Functions 
function servercheck($server, $port) { 
    //Check that the port value is not empty 
    if (empty($port)) { 
     $port = 80; 
    } 
    //Check that the server value is not empty 
    if (empty($server)) { 
     $server = 'domain.com'; 
    } 
    //Connection 
    $fp = @fsockopen($server, $port, $errno, $errstr, 1); 
    //Check if connection is present 
    if ($fp) { 
     //Return Alive 
     return 1; 
    } else { 
     //Return Dead 
     return 0; 
    } 
    //Close Connection 
    fclose($fp); 
} 

//Ports and Services to check 
$services = [ 
    'Website Access' => ['domain.com' => 80], 
    'Another Service' => ['domain.com' => 443], 
    'Another Service' => ['domain.com' => 21], 
]; 

$errors = 0; 
foreach($services as $host => $port) { 
    if(servercheck($host, $port) == 0) { 
     $errors++; 
    } 
} 
?> 

<div class="infobox"> 
    <?php 
    if ($errors == 0) { 
     echo $online; 
    } elseif ($errors > 1) { 
     echo $offline; 
    } 
    ?> 
</div> 
<div class="overallmesssage"> 
    <h3></h3> 
</div> 

Somethink這樣。我不知道它是否正確,但你可以測試它。

+0

我認爲你的意思是'如果(servercheck($ host,$ port)== 0)' – hassan

+0

哦對不起是的... :)我改變了這一點。 – Stony

+0

歡迎大家使用'setInterval'而不是cron? – hassan

相關問題