Reputation:
I want to check the status of a couple of servers and display on a page if they're up or down. Currently, I'm doing this with PHP's fsockopen()
function. As part of this function, you determine a timeout time. This means that if the servers are down, it'll block for say 2 seconds before moving on. This causes my webpage to wait till the fsockopen()
s are done before it displays the page.
Is there a way to get these fsockopen()
functions to run separately from the rendering of the page, and have their results included in after they're determined? It doesn't have to be PHP, I guess.
Upvotes: 1
Views: 268
Reputation: 14173
Create a php page that accepts a variable to know which server to check
<?php
$server = isset($_GET['server']) ? $_GET['server'] : '';
$upordown = 'invalid server'; //default incase wrong variable is set
switch ($server) {
case 'server1':
//check server 1; EG: $upordown = checkserver('http://www.example.com');
break;
case 'server2':
//check server 2;
break;
case 'server3':
//check server 3;
break;
}
echo $upordown
?>
and then on your show page using jquery:
<html>
<head>
<script src='jquery.js'></script>
<script>
$(function(){
$('.serverstatus').each(function() {
var id = $(this).data('serverid');
$(this).load('/servercheck.php?server='+id);
});
});
</script>
</head>
<body>
<div class='serverstatus' data-serverid='server1'>loading</div>
<div class='serverstatus' data-serverid='server2'>loading</div>
<div class='serverstatus' data-serverid='server3'>loading</div>
</body>
</html>
Now you could add some extra safety like caching and random servernames instead of 1,2,3
Upvotes: 0
Reputation: 16333
Use AJAX. I'd recommend using jQuery's $.ajax function (http://api.jquery.com/jQuery.ajax/) to call a PHP script which will query a server. You will provide a success function to the ajax call which will be executed when the ajax call has finished.
Upvotes: 0
Reputation: 2799
Split this into two sub-problems:
1) Create a cron job (or otherwise schedule) a script that will check the server status and save that result somewhere (db, cache, file, post-it, etc). Run it as frequently as you like.
2) Query that result either while rendering the page or via an AJAX call.
Upvotes: 1