Server load in real time

1

Using the script below I am able to visualize the load of the cpu, but I am having doubts in the part of updating the value in real time, I did a little gambiarra using the html to refresh the page after 50 seconds, but it is not a practice very cool.

<meta http-equiv="refresh" content="50" > 

PHP

<?php  
$min_warn_level = 3; 
// Set to min load average to send alert 
$email_recipient = "[email protected]"; 
 // Set to address of alert recipient
$current_reading = @exec('uptime');
preg_match("/averages?: ([0-9\.]+),[\s]+([0-9\.]+),[\s]+([0-9\.]+)/", $current_reading, $averages); 
$uptime = explode(' up ', $current_reading); 
$uptime = explode(',', $uptime[1]); $uptime = $uptime[0].', ' . $uptime[1];
$data = "Server Load Averages $averages[1], $averages[2], $averages[3]\n";
$data .= "Server Uptime $uptime"; if ($averages[3] > $min_warn_level ) {
$subject = "Alert: Load average is over $min_warn_level"; mail($email_recipient, $subject, $data); 
} 

 echo $data; ?>
    
asked by anonymous 05.06.2014 / 16:38

1 answer

2

You can use AJAX to make your requests to this PHP file, preventing the page from being reloaded every 50 seconds.

The advantage of using AJAX is that you reduce traffic between the client and the server and consequently reduce the processing load on the server.

Instead of reloading all the content, you will only receive the response from the request, which in your case will probably be an array with the CPU load data. With the response of the request, you will just have to update some elements of your HTML with the updated information.

Use with the Long Polling technique, so it will only reorder the server if there is a change in the request response.

See this link: ajax analysis . The question is about analyzing an AJAX code, it can be used as an example, you will see some analysis, it is already out of the context of your question, but it is indirectly linked.


Example with pure Javascript:

On your page where the refresh is done every 50 seconds, enter the following code:

<script type="text/javascript">
   o = "";
   try{ o = new XMLHttpRequest() }
   catch(e){ o = new ActiveXObject("Microsoft.XMLHTTP"); }

   function getAjax(){
        //Troque "seu_ficheiro.php" pelo seu ficheiro PHP que verifica a carga do CPU
        //Nesse exemplo, o ficheiro PHP está no mesmo directório que o HTML onde foi inserido o Javascript, se estiver em directórios diferentes, é necessário inserir o caminho correcto.
        o.open("GET","seu_ficheiro.php",true);
        o.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
        o.onreadystatechange = function(){
            if(o.readyState == 4){
                //Altera somente o HTML da div que vai receber a resposta da requisição
                //No exemplo utilizamos a div com o id "resultado"
                document.getElementById("resultado").innerHTML = o.responseText;
            }
        }
        o.send(null);
    }
    //A cada 10 segundos será feito uma nova requisição ao servidor
    setTimeout("getAjax()",10000);
</script>

In your PHP file, you have an "echo" of the variable "$ data", in the "result" div the content of the "$ data" variable should appear.

This is a basic example of an AJAX request, it should not be used on a large scale to avoid overloading the server.

Do not forget to remove the refresh every 50 seconds, it will no longer be necessary.

    
05.06.2014 / 16:48