Created
October 30, 2014 20:51
-
-
Save jveldboom/b91607ae8ad721ca6529 to your computer and use it in GitHub Desktop.
Combine sar system stats
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* sar documentation http://linux.die.net/man/1/sar | |
*/ | |
class SystemStats | |
{ | |
function __construct($sar) | |
{ | |
$this->sar = $sar; | |
} | |
function run() | |
{ | |
$return = array(); | |
foreach($this->sar as $type => $opt) | |
{ | |
$results = self::runCommand($opt['cmd']); | |
$return = self::formatValues($return,$results,$type,$opt['map']); | |
} | |
return json_encode($return); | |
} | |
function runCommand($cmd) | |
{ | |
return shell_exec($cmd); | |
} | |
function formatValues($return,$results,$type,$map) | |
{ | |
$bits = explode(" ",$results); | |
foreach($bits as $k => $v) | |
{ | |
if(!isset($map[$k]) || !$map[$k]) continue; | |
$return[$type.'.'.$map[$k]] = trim($v); | |
} | |
return $return; | |
} | |
} | |
$sar = array( | |
'cpu' => array( | |
'cmd' => 'LC_TIME="en_US.UTF-8" sar -u | tail -n 2 | head -n 1 | awk \'{$1=$1}{print}\'', | |
'map' => array(false,false,'num','user','nice','system','iowait','steal','idle'), | |
), | |
'load' => array( | |
'cmd' => 'cat /proc/loadavg', | |
'map' => array('1min','5min','15min',false,false), | |
), | |
'mem' => array( | |
'cmd' => 'LC_TIME="en_US.UTF-8" sar -r | tail -n 2 | head -n 1 | awk \'{$1=$1}{print}\'', | |
'map' => array(false,false,'kbmemfree','kbmemused','memused','kbbuffers','kbcached','kbcommit','commit','kbactive','kbinact','kbdirty'), | |
), | |
'disk' => array( | |
'cmd' => 'LC_TIME="en_US.UTF-8" sar -b | tail -n 2 | head -n 1 | awk \'{$1=$1}{print}\'', | |
'map' => array(false,false,'tps','rtps','wtps','breads','bwrtns'), | |
), | |
'net' => array( | |
'cmd' => 'LC_TIME="en_US.UTF-8" sar -n DEV | grep eth0 | tail -n 2 | head -n 1 | awk \'{$1=$1}{print}\'', | |
'map' => array(false,false,'iface','rxpcks','txpcks','rxkbs','txkbs','rxcmps','txcmps','rxmcsts','ifutil'), | |
), | |
); | |
$n = new SystemStats($sar); | |
echo $n->run(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment