Update API to return disk usage for all monitors

This allows us to get back an array of monitor => usage key pairs
This commit is contained in:
Kyle Johnson 2014-11-25 16:10:34 +00:00
parent e7ab18236a
commit 284b51b73d
1 changed files with 24 additions and 8 deletions

View File

@ -31,10 +31,14 @@ class HostController extends AppController {
)); ));
} }
// If $mid is set, only return disk usage for that monitor
// Else, return an array of total disk usage, and per-monitor
// usage.
function getDiskPercent($mid = null) { function getDiskPercent($mid = null) {
$this->loadModel('Config'); $this->loadModel('Config');
$this->loadModel('Monitor'); $this->loadModel('Monitor');
// If $mid is passed, see if it is valid
if ($mid) { if ($mid) {
if (!$this->Monitor->exists($mid)) { if (!$this->Monitor->exists($mid)) {
throw new NotFoundException(__('Invalid monitor')); throw new NotFoundException(__('Invalid monitor'));
@ -53,20 +57,32 @@ class HostController extends AppController {
$zm_dir_events = Configure::read('ZM_PATH_WEB') . '/' . $zm_dir_events; $zm_dir_events = Configure::read('ZM_PATH_WEB') . '/' . $zm_dir_events;
} }
$space = -1;
if ($mid) { if ($mid) {
// Get disk usage for $mid // Get disk usage for $mid
$space = shell_exec ("du -sh0 $zm_dir_events/$mid | awk '{print $1}'"); $usage = shell_exec ("du -sh0 $zm_dir_events/$mid | awk '{print $1}'");
} else { } else {
$space = shell_exec( 'df '.$zm_dir_events); $monitors = $this->Monitor->find('list');
if ( preg_match( '/\s(\d+)%/ms', $space, $matches ) ) $usage = array();
$space = $matches[1];
// Add each monitor's usage to array
foreach ($monitors as $id => $name) {
$space = shell_exec ("du -s0 $zm_dir_events/$id | awk '{print $1}'");
if ($space == null) {
$space = 0;
}
$space = $space/1024/1024;
$usage[$name] = rtrim($space);
}
// Add total usage to array
$space = shell_exec( "df $zm_dir_events |tail -n1 | awk '{print $3 }'");
$space = $space/1024/1024;
$usage['Total'] = rtrim($space);
} }
$this->set(array( $this->set(array(
'space' => $space, 'usage' => $usage,
'_serialize' => array('space') '_serialize' => array('usage')
)); ));
} }