SysInfoLinux.php 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. <?php
  2. /* vim: set expandtab sw=4 ts=4 sts=4: */
  3. /**
  4. * Hold PhpMyAdmin\SysInfoLinux class
  5. *
  6. * @package PhpMyAdmin
  7. */
  8. namespace PhpMyAdmin;
  9. use PhpMyAdmin\SysInfo;
  10. use PhpMyAdmin\SysInfoBase;
  11. /**
  12. * Linux based SysInfo class
  13. *
  14. * @package PhpMyAdmin
  15. */
  16. class SysInfoLinux extends SysInfoBase
  17. {
  18. public $os = 'Linux';
  19. /**
  20. * Gets load information
  21. *
  22. * @return array with load data
  23. */
  24. function loadavg()
  25. {
  26. $buf = file_get_contents('/proc/stat');
  27. $nums = preg_split(
  28. "/\s+/",
  29. mb_substr(
  30. $buf,
  31. 0,
  32. mb_strpos($buf, "\n")
  33. )
  34. );
  35. return Array(
  36. 'busy' => $nums[1] + $nums[2] + $nums[3],
  37. 'idle' => intval($nums[4]),
  38. );
  39. }
  40. /**
  41. * Checks whether class is supported in this environment
  42. *
  43. * @return true on success
  44. */
  45. public function supported()
  46. {
  47. return @is_readable('/proc/meminfo') && @is_readable('/proc/stat');
  48. }
  49. /**
  50. * Gets information about memory usage
  51. *
  52. * @return array with memory usage data
  53. */
  54. function memory()
  55. {
  56. preg_match_all(
  57. SysInfo::MEMORY_REGEXP,
  58. file_get_contents('/proc/meminfo'),
  59. $matches
  60. );
  61. $mem = array_combine($matches[1], $matches[2]);
  62. $defaults = array(
  63. 'MemTotal' => 0,
  64. 'MemFree' => 0,
  65. 'Cached' => 0,
  66. 'Buffers' => 0,
  67. 'SwapTotal' => 0,
  68. 'SwapFree' => 0,
  69. 'SwapCached' => 0,
  70. );
  71. $mem = array_merge($defaults, $mem);
  72. foreach ($mem as $idx => $value) {
  73. $mem[$idx] = intval($value);
  74. }
  75. $mem['MemUsed'] = $mem['MemTotal']
  76. - $mem['MemFree'] - $mem['Cached'] - $mem['Buffers'];
  77. $mem['SwapUsed'] = $mem['SwapTotal']
  78. - $mem['SwapFree'] - $mem['SwapCached'];
  79. return $mem;
  80. }
  81. }