-
Notifications
You must be signed in to change notification settings - Fork 61
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Checking for valid CPU average values #97
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -44,6 +44,7 @@ public function __construct(IConfig $config) { | |
} | ||
|
||
public function getSystemStatistics() { | ||
$processorUsage = $this->getProcessorUsage(); | ||
$memoryUsage = $this->getMemoryUsage(); | ||
return [ | ||
'version' => $this->config->getSystemValue('version'), | ||
|
@@ -56,7 +57,7 @@ public function getSystemStatistics() { | |
'memcache.locking' => $this->config->getSystemValue('memcache.locking', 'none'), | ||
'debug' => $this->config->getSystemValue('debug', false) ? 'yes' : 'no', | ||
'freespace' => $this->view->free_space(), | ||
'cpuload' => sys_getloadavg(), | ||
'cpuload' => $processorUsage['loadavg'], | ||
'mem_total' => $memoryUsage['mem_total'], | ||
'mem_free' => $memoryUsage['mem_free'] | ||
]; | ||
|
@@ -94,4 +95,25 @@ protected function getMemoryUsage() { | |
]; | ||
} | ||
|
||
/** | ||
* Get current CPU load average | ||
* | ||
* @return array load average with three values, 1/5/15 minutes average. | ||
*/ | ||
protected function getProcessorUsage() { | ||
// get current system load average. | ||
$loadavg = sys_getloadavg(); | ||
|
||
// check if we got any values back. | ||
if (!(is_array($loadavg) && count($loadavg) === 3)) { | ||
// either no array or too few array keys. | ||
// returning back zeroes to prevent any errors on JS side. | ||
$loadavg = [0, 0, 0]; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. wonder if it would make more sense to return a error code (maybe 'N/A' like we use for other statistics as well) and then hide the CPU Load in the web interface or display something like "not available". Otherwise it looks like everything is OK and the system load is just super low, something which makes probably every admin happy. 😉 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
} | ||
|
||
return [ | ||
'loadavg' => $loadavg | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why do you put this in another array and don't return There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To allow easier extending the information without breaking something on the JS side. To be more extensible for the future. when extending stats with more CPU graph lines someday (e.g. splitting up CPU usage in user, system, kernel or something like that). But I can remove array if you want. |
||
]; | ||
} | ||
|
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Think this would be a nice method to write a small unit test.