monitoring the server load from web

From a Linux console, we can choose to show the server load using the ‘top” or “uptime” command. To get a simple reflection of the same information, we can also use the “/proc/loadavg”.

The first three columns of “/proc/loadavg” measure CPU and IO utilization of the last one, five, and 10 minute periods. The fourth column shows the number of currently running processes and the total number of processes. The last column displays the last process ID used.

Using a simple PHP script and Ajax, we can display the load status using “/proc/loadavg” on a timer using web interface without having to login to a console.

Here is the html page that we can use to show the status in every 5 seconds. It calls a PHP script which returns the content of /proc/loadavg. And by using Ajax, the page doesn’t need to be refreshed.

<html>
<body>

<script type=”text/javascript”>
<!– //
// initialize the ajax stuff
var x;
try {
// Try to create object for Firefox, Safari, IE7, etc.
x = new XMLHttpRequest();
} catch (e) {
try {
// Try to create object for later versions of IE.
x = new ActiveXObject(‘MSXML2.XMLHTTP’);
} catch (e) {
try {
// Try to create object for early versions of IE.
x = new ActiveXObject(‘Microsoft.XMLHTTP’);
} catch (e) {
// Could not create an XMLHttpRequest object.
alert(‘Not able to init ajax’);
}
}
}

setInterval(‘showLoad()’, 5000);

function showLoad() {
el = document.getElementById(‘LoadStatus’);
x.onreadystatechange = function() {
if (x.readyState == 4) {
if (x.status >= 200 && x.status <= 299) {
el.innerHTML = x.responseText;
}
}
}
x.open(“GET”, ‘/path/to/serverstatus.php?Load’, true);
x.send(null);
}
// –>
</script>
<div id=”LoadStatus” style=”width:400px; height:30px; background-color:#eee;”>
</div>

</body>
</html>

The simple PHP script that reads the loadavg file:

<?php
$info = date(“H:i:s”) . ‘ ‘;

if (isset($_GET['Load'])) {
$info .= file_get_contents(‘/proc/loadavg’);
echo $info;
}
?>

This solution is especially useful when telnet/ssh to a server is hard to do. For example, in a restricted network, or there is no computer around. You might ask without a computer, how to access web in the first place, well smart phone is the answer :). As you can see it has more potential to do a lot more stuff if utilizing other Linux commands.

This entry was posted in ajax gadgets. Bookmark the permalink.

One Response to monitoring the server load from web

  1. Slawa says:

    There is an open-source script phploadavg (http://phploadavg.sourceforge.net/) which shows real-time graphs of the server load (and performance). Requires zero installation and configuration. It helped me a lot when my server was getting slow to understand why was it slow. It uses the same idea of reading /etc/loadavg, but allows better overview of the load over time.

Comments are closed.