| Conditions | 7 |
| Paths | 10 |
| Total Lines | 29 |
| Code Lines | 16 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 1 | Features | 0 |
| 1 | <?php |
||
| 43 | public static function loadAverage() |
||
| 44 | { |
||
| 45 | $load = 0; |
||
| 46 | if (stristr(PHP_OS, 'win')) { |
||
| 47 | // its not a better solution, but no other way to do this |
||
| 48 | $cmd = "wmic cpu get loadpercentage /all"; |
||
| 49 | @exec($cmd, $output); |
||
|
|
|||
| 50 | |||
| 51 | // if output is exist |
||
| 52 | if ($output) { |
||
| 53 | // try to find line with numeric data |
||
| 54 | foreach ($output as $line) { |
||
| 55 | if ($line && preg_match("/^[0-9]+\$/", $line)) { |
||
| 56 | $load = $line; |
||
| 57 | break; |
||
| 58 | } |
||
| 59 | } |
||
| 60 | } |
||
| 61 | } else { |
||
| 62 | $sys_load = sys_getloadavg(); // get linux load average (1 = 100% of 1 CPU) |
||
| 63 | $load = $sys_load[0] * 100; // to percentage |
||
| 64 | } |
||
| 65 | |||
| 66 | if ((int)$load <= 0) { |
||
| 67 | return 'error'; |
||
| 68 | } |
||
| 69 | |||
| 70 | return (int)$load . '%'; |
||
| 71 | } |
||
| 72 | } |
If you suppress an error, we recommend checking for the error condition explicitly: