Όταν ο χρήστης πατάει το κουμπί MONITORING SERVER, τότε αρχίζε μία setInterval που δείχνει "ζωντανά" με AJAX την κατάσταση των πόρων του server.
Η ερωτήσεις μου:
1. Οι πιο ειδικοί πως το βλέπετε το script; Δίχνει όντως σωστά αποτελέσματα;
2. Δοκίμασα να το τρέξω σε localhost (XAMPP - Windows 10) και φαίνονται σωστά τα αποτελέσματα, όμως όταν το έβαλα στο site (VPS, Linux), το CPU load παίρνει μονίμως τιμές 0,κάτι ή 0,0κάτι. Έτσι, για την περίπτωση που δεν είναι Windows το OS, έβαλα πολλαπλασιασμό επί 100. Ελπίζω να είναι όντως αυτή η σωστή τιμή που προκύπτει.
3. Στο Linux/VPS η RAM φαίνεται μονίμως στο 90% κάτι, που δεν είμαι τόσο σίγουρος ότι είναι σωστό γιατί και δεν στέκει και στο Webmin βλέπω "REAL RAM" στο 35%. Τι λάθος υπάρχει στον υπολογισμός της RAM για την περίπτωση του Linux;
4. Το να χτυπάει το serverload.php πολλές φορές, καταναλώνει από μόνο του πολλούς πορούς; Στα πόσα δευτερόλεπτα λέτε να το βάλω για να έχει νόημα; Σίγουρα η μικρότερη τιμή είναι η πιο "live", όμως εντάξει δε μπορώ να το βάλω και 500ms.

Ορίστε το script:
PHP
Κώδικας: Επιλογή όλων
function get_server_load() {
$load = '';
if (stristr(PHP_OS, 'win')) {
$cmd = 'wmic cpu get loadpercentage /all';
@exec($cmd, $output);
if ($output) {
foreach($output as $line) {
if ($line && preg_match('/^[0-9]+$/', $line)) {
$load = $line;
break;
}
}
}
} else {
$sys_load = sys_getloadavg();
$load = $sys_load[0];
}
return $load;
}
function getServerMemoryUsage($getPercentage=true)
{
$memoryTotal = null;
$memoryFree = null;
if (stristr(PHP_OS, "win")) {
// Get total physical memory (this is in bytes)
$cmd = "wmic ComputerSystem get TotalPhysicalMemory";
@exec($cmd, $outputTotalPhysicalMemory);
// Get free physical memory (this is in kibibytes!)
$cmd = "wmic OS get FreePhysicalMemory";
@exec($cmd, $outputFreePhysicalMemory);
if ($outputTotalPhysicalMemory && $outputFreePhysicalMemory) {
// Find total value
foreach ($outputTotalPhysicalMemory as $line) {
if ($line && preg_match("/^[0-9]+\$/", $line)) {
$memoryTotal = $line;
break;
}
}
// Find free value
foreach ($outputFreePhysicalMemory as $line) {
if ($line && preg_match("/^[0-9]+\$/", $line)) {
$memoryFree = $line;
$memoryFree *= 1024; // convert from kibibytes to bytes
break;
}
}
}
}
else
{
if (is_readable("/proc/meminfo"))
{
$stats = @file_get_contents("/proc/meminfo");
if ($stats !== false) {
// Separate lines
$stats = str_replace(array("\r\n", "\n\r", "\r"), "\n", $stats);
$stats = explode("\n", $stats);
// Separate values and find correct lines for total and free mem
foreach ($stats as $statLine) {
$statLineData = explode(":", trim($statLine));
//
// Extract size (TODO: It seems that (at least) the two values for total and free memory have the unit "kB" always. Is this correct?
//
// Total memory
if (count($statLineData) == 2 && trim($statLineData[0]) == "MemTotal") {
$memoryTotal = trim($statLineData[1]);
$memoryTotal = explode(" ", $memoryTotal);
$memoryTotal = $memoryTotal[0];
$memoryTotal *= 1024; // convert from kibibytes to bytes
}
// Free memory
if (count($statLineData) == 2 && trim($statLineData[0]) == "MemFree") {
$memoryFree = trim($statLineData[1]);
$memoryFree = explode(" ", $memoryFree);
$memoryFree = $memoryFree[0];
$memoryFree *= 1024; // convert from kibibytes to bytes
}
}
}
}
}
if (is_null($memoryTotal) || is_null($memoryFree)) {
return null;
} else {
if ($getPercentage) {
return (100 - ($memoryFree * 100 / $memoryTotal));
} else {
return array(
"total" => $memoryTotal,
"free" => $memoryFree,
);
}
}
}
function getNiceFileSize($bytes, $binaryPrefix=true) {
if ($binaryPrefix) {
$unit=array('B','KB','MB','GB','TB','PB');
if ($bytes==0) return '0 ' . $unit[0];
return @round($bytes/pow(1024,($i=floor(log($bytes,1024)))),2) .' '. (isset($unit[$i]) ? $unit[$i] : 'B');
} else {
$unit=array('B','KB','MB','GB','TB','PB');
if ($bytes==0) return '0 ' . $unit[0];
return @round($bytes/pow(1000,($i=floor(log($bytes,1000)))),2) .' '. (isset($unit[$i]) ? $unit[$i] : 'B');
}
}
$memUsage = getServerMemoryUsage(false);
$output = array();
$output['cpu_load'] = get_server_load();
$output['memory_total'] = $memUsage['total'];
$output['memory_free'] = $memUsage['free'];
$output['memory_usage'] = $memUsage['total'] - $memUsage['free'];
exit(json_encode($output));
Κώδικας: Επιλογή όλων
<script>
function enable_server_monitoring()
{
$('#monitor_server_info').show();
$('#monitor_server_button').hide();
setInterval(function(){ sc_show_server_load(); }, 5000);
}
function sc_show_server_load()
{
$.ajax({
url : "serverload.php",
type: "GET",
data: {
//
},
dataType: "json",
error: function(xhr, textStatus, errorThrown){
//
},
success: function(data, textStatus, jqXHR){
if (!data) { return false; }
for (b in data) {
if (b == 'cpu_load')
{
var cpu_load_percentage = data[b];
}
else if (b == 'memory_total')
{
var memory_total = data[b];
}
else if (b == 'memory_free')
{
var memory_free = data[b];
}
else if (b == 'memory_usage')
{
var memory_usage = data[b];
}
}
// do progress bars
$( "#progressbar_cpu_usage" ).width(((cpu_load_percentage*195)/100));
$('#cpu_usage_per').html(cpu_load_percentage + '%');
// memory usage
var memory_usage_percentage = (memory_usage*100/memory_total);
$('#memory_usage_per').html(Math.round(memory_usage_percentage* 100)/100 + '%');
$( "#progressbar_memory_usage" ).width((memory_usage_percentage*195)/100);
}
});
}
</script>
<input type="button" class="button" onclick="enable_server_monitoring()" value="MONITORING SERVER" id="monitor_server_button" />
<div style="display:none;" id="monitor_server_info">
<div><div id="progressbar_cpu_usage" style="background-color:red; height:20px; width:0px;"></div>CPU: <span style="display:inline-block;" id="cpu_usage_per"></span></div>
<div><div id="progressbar_memory_usage" style="background-color:green; height:20px; width:0px;"></div>MEMORY: <span style="display:inline-block;" id="memory_usage_per"></span></div>
</div>