Skip to content

Instantly share code, notes, and snippets.

@davidsword
Last active February 3, 2018 03:02
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save davidsword/b01a38ea335471c23615e0330eeeb286 to your computer and use it in GitHub Desktop.
<?
/* =====================================================================================================
/* !SERVER TOP */
/*
/* Read a server's TOP output (crontab: `top -d 99 -n 1 -b > /var/www/server_top.txt`)
/* returns serialized array of $cpu, $memory, $load, $time, $hdd
/* Your TOP output may be different. See $refex_*
/*
/* By davidsword.ca
/*
/* ---------------------------------------------------------------------------------------------------*/
/* =====================================================================================================
/* !SETTINGS */
/* ---------------------------------------------------------------------------------------------------*/
# error_reporting(E_ALL);
# ini_set('display_errors', 1);
# ini_set('display_startup_errors',1);
date_default_timezone_set('America/Vancouver');
$serverTopFile = '/var/www/server_top.txt';
$regex_cpu = '/(\d+?.?\d) us,/';
$regex_mem = '/Mem:[ \t]+(\d+)[ \t]+total,[ \t]+(\d+)[ \t]+used,[ \t]+(\d+)[ \t]+free,/';
$regex_load = '/load average: (\d+.\d{1,2}), (\d+.\d{1,2}), (\d+.\d{1,2})/';
$disk = '/';
// Alright, lets go
$top = file_get_contents($serverTopFile);
/* =====================================================================================================
/* !CPU STATS */
/* ---------------------------------------------------------------------------------------------------*/
$cpuAdder = 0;
@preg_match_all($regex_cpu, $top, $cpus);
foreach ($cpus[1] as $foundCPU)
$cpuAdder += floatval($foundCPU);
$cpu = round($cpuAdder / count($cpus[1]),2);
/* =====================================================================================================
/* !LOAD TIME STATS */
/* ---------------------------------------------------------------------------------------------------*/
preg_match($regex_load, $top, $load_times);
$load = ($load_times[1]+$load_times[2]+$load_times[3])/3;
$load = round($load,2);
/* =====================================================================================================
/* !MEMORY */
/* ---------------------------------------------------------------------------------------------------*/
@preg_match($regex_mem, $top, $memory_stats);
$memory_total = $memory_stats[1];
$memory_used = $memory_stats[2];
$memory_free = $memory_stats[3];
$memory = @floor($memory_used/$memory_total*100);
/* =====================================================================================================
/* !TOP TIME */
/* ---------------------------------------------------------------------------------------------------*/
@preg_match('/[0-9]{1,2}:[0-9]{1,2}:[0-9]{1,2}/', $top, $time); //
/* =====================================================================================================
/* !HDD */
/* ---------------------------------------------------------------------------------------------------*/
$hdd = 100 - round((toGB(disk_free_space($disk)) / toGB(disk_total_space($disk))) * 100);
/* =====================================================================================================
/* !SPIT OUT STATS */ // No key's as we're being a bit cryptic
/* ---------------------------------------------------------------------------------------------------*/
echo serialize(array(
$cpu,
$memory,
$load,
$time[0],
$hdd
));
/* =====================================================================================================
/* !convert B to G */
/* ---------------------------------------------------------------------------------------------------*/
function toGB($size) {
return round($size / 1024 / 1024 / 1024,2);
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment