Skip to content

Instantly share code, notes, and snippets.

@davidsword
Created September 14, 2016 21:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save davidsword/54120468172d52640e62ff32a591d61e to your computer and use it in GitHub Desktop.
Save davidsword/54120468172d52640e62ff32a591d61e to your computer and use it in GitHub Desktop.
<div id="dynamic_stats-wrapper">
<!-- Everything below reloads every 60 seconds -->
<div id="dynamic_stats">
<?
// get the file from the server
$stats = file_get_contents('/var/www/.../top.txt');
$cores = 16;
// checky amature way to remove extra spacing
$stats = str_replace(' ', ' ', $stats);
$stats = str_replace(' ', ' ', $stats);
$stats = str_replace(' ', ' ', $stats);
$stat_line = explode('<br />', nl2br($stats));
// == GET CPU == //
$cpu = 0;
for ($i = 2; $i != 18; $i++ ) {
preg_match('/: (\d+?.?\d)%us,/', $stat_line[$i], $cpu_id);
$cpu += $cpu_id[1];
}
$cpu = ceil($cpu / $cores);// % used each core /
// == GET LOAD TIME == //
preg_match('/load average: (\d+.\d{1,2}), (\d+.\d{1,2}), (\d+.\d{1,2})/', $stat_line[0], $load_times); //
$load = "{$load_times[1]}";
$load = floor($load/5*100);
// == GET MEMORY == //
preg_match('/Mem: (\d+)k total, (\d+)k used, (\d+)k free,/', $stat_line[18], $memory_stats); //
$memory_total = formatBytes($memory_stats[1],2);
$memory_used = formatBytes($memory_stats[2],2);
$memory_free = formatBytes($memory_stats[3],2);
$memory = floor($memory_used/$memory_total*100);
// output to hold the changing values for JS to pull into the gauges
// "REAL" are from the server_top_output.txt every 60 seconds
$time = substr_replace($stat_line[0], '', 0, 6 );
echo "
<pre style='display:none'>From: ".substr_replace($time, '', 9, strlen($time)) ."
CPU: <span id='source_cpu'>{$cpu}</span>%
LOAD: <span id='source_load'>{$load}</span>%
MEM: <span id='source_mem'>{$memory}</span>";
?>
</div>
<!-- Everything above reloads every 30 seconds -->
</div><!-- /dynamic_stats-wrapper -->
<?
// convert KB to G
function formatBytes($size, $precision = 2) {
$base = log($size) / log(1024);
$suffixes = array('k', 'M', 'G', 'T');
return round(pow(1024, $base - floor($base)), $precision) . $suffixes[floor($base)];
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment