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 
    $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.

Achmed Zuzali
  • 883
  • 10
  • 12

2 Answers2

1

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;
}

?>

hassan
  • 7,812
  • 2
  • 25
  • 36
1

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.

René Höhle
  • 26,716
  • 22
  • 73
  • 82
  • i think that you mean `if(servercheck($host, $port) == 0)` – hassan Mar 15 '17 at 20:40
  • Oh sorry yes... :) i've changed that. – René Höhle Mar 15 '17 at 20:42
  • you are welcome, and what about using `setInterval` instead of cron ? – hassan Mar 15 '17 at 20:45
  • @hassan you can do that in that way but then you can't build Graphs and work with that data when you don't have some informations. So i would store that informations on a single place and work with that data then you have much more possibilities. – René Höhle Mar 15 '17 at 20:47
  • @Stony can you put your new code in my old code because I'm unsure where to put the code which you have given (I'm new to PHP) – Achmed Zuzali Mar 15 '17 at 20:48
  • that's great, but i think that setinterval will be more handy, it will only make sense when the user visit the page, instead of still running in the background, and within the setinterval callback , you may connect to an api which response some json [information to build your graphs] , something like that; – hassan Mar 15 '17 at 20:49
  • @hassan how do i do a cron job if its better im just experimenting with PHP to see what i can make. – Achmed Zuzali Mar 15 '17 at 20:51
  • @Stony i got this error Warning: Invalid argument supplied for foreach() in ... on line 53 Operational – Achmed Zuzali Mar 15 '17 at 20:55
  • The variable was not correct it should be `$services` in the foreach... – René Höhle Mar 15 '17 at 20:58
  • @Stony i don't get any error messages but now nothing shows up at all – Achmed Zuzali Mar 15 '17 at 21:02
  • @Stony i found the issue it was the $offline but now it just says failed – Achmed Zuzali Mar 15 '17 at 21:11
  • `$ofline` that's a typo (in the answer), which should read as `$offline` – Funk Forty Niner Mar 15 '17 at 22:08
  • @Fred-ii- i discovered that error but when i have all of the servers working it still says offline even though i want it to say online and only offline for when one or more of the servers are unavailable – Achmed Zuzali Mar 15 '17 at 23:26