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