<?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>
I would like to add an overall message which changes depending on the servers being online. Similar to https://demo.cachethq.io/ which has an overall green div telling the visitor that everything is up and running but if 1 of the servers go down it changes to orange and if all the servers go down, it changes to red.
collect your servers status in an array , then check if in_array
<?php
$status = array();
foreach($server as $host => $port){
$status[] = servercheck($host,$port);
}
if (in_array('0', $status)) {
echo $offline;
} else {
echo $online;
}
?>
In your case i would prefer another approach then asking the page directly. In your position i would build a cronjob which checks every minute your services and save the state in a database or something else. Then you can build a Graph with that data.
But for your case. To achieve this i would prefer to run with a loop through your servers then save every failed stated in an array. After that you can work with that array. So of the array has one entry it's orange. If you have more then one entries you have a red state. Or much easier use an counter variable.
$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!";
}
Your complete code:
<?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 like that. I don't know if it's correct but you can test it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With