<?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=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/,它有一个整体绿色div告诉访问者所有内容都已启动并运行但如果其中一个服务器发生故障,则会变为橙色,如果所有服务器都关闭,则会变为红色。
答案 0 :(得分:1)
在数组中收集服务器状态,然后检查in_array
<?php
$status = array();
foreach($server as $host => $port){
$status[] = servercheck($host,$port);
}
if (in_array('0', $status)) {
echo $offline;
} else {
echo $online;
}
&GT;
答案 1 :(得分:1)
在你的情况下,我更喜欢另一种方法,然后直接询问页面。在你的位置,我将建立一个cronjob,它检查你的服务的每一分钟,并将状态保存在数据库或其他东西。然后,您可以使用该数据构建图表。
但是对于你的情况。为了实现这一点,我宁愿通过服务器循环运行,然后保存数组中的每个失败声明。之后,您可以使用该阵列。所以数组有一个条目是橙色的。如果您有多个条目,则表示您处于红色状态。或者更容易使用计数器变量。
$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>
有些想法。我不知道它是否正确但你可以测试它。