api.dbmon.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. <?php
  2. /**
  3. * Simple database query performance monitor
  4. */
  5. class DBmon {
  6. /**
  7. * Contains full query rendering modifier
  8. *
  9. * @var bool
  10. */
  11. protected $fullModifier = false;
  12. /**
  13. * System messages helper placeholder
  14. *
  15. * @var object
  16. */
  17. protected $messages = '';
  18. /**
  19. * Deafault container refresh rate in ms.
  20. *
  21. * @var int
  22. */
  23. protected $timeout = 2000;
  24. /**
  25. * Some predefined URLs/routes etc
  26. */
  27. const URL_ME = '?module=dbmon';
  28. const ROUTE_FULL = 'renderfullqueries';
  29. const ROUTE_ZEN = 'dbmonzenmode';
  30. /**
  31. * creates new DBmon instance
  32. */
  33. public function __construct() {
  34. $this->initMessages();
  35. $this->setOptions();
  36. }
  37. /**
  38. * Inits system message helper for further usage
  39. */
  40. protected function initMessages() {
  41. $this->messages = new UbillingMessageHelper();
  42. }
  43. /**
  44. * Sets some current instance properties
  45. *
  46. * @return void
  47. */
  48. protected function setOptions() {
  49. if (ubRouting::checkGet(self::ROUTE_FULL)) {
  50. $this->fullModifier = true;
  51. }
  52. }
  53. /**
  54. * Returns current instance refresh timeout
  55. *
  56. * @return int
  57. */
  58. public function getTimeout() {
  59. return($this->timeout);
  60. }
  61. /**
  62. * Returns currently running MySQL processes
  63. *
  64. * @return array
  65. */
  66. protected function getProcessList() {
  67. $result = array();
  68. if ($this->fullModifier) {
  69. $query = "SHOW FULL PROCESSLIST";
  70. } else {
  71. $query = "SHOW PROCESSLIST";
  72. }
  73. $result = simple_queryall($query);
  74. return($result);
  75. }
  76. /**
  77. * Renders module interface controls
  78. *
  79. * @return string
  80. */
  81. public function renderControls() {
  82. $result = '';
  83. $result .= wf_BackLink('?module=report_sysload') . ' ';
  84. if ($this->fullModifier) {
  85. $result .= wf_Link(self::URL_ME, wf_img('skins/icon_restoredb.png') . ' ' . __('Current database processes'), false, 'ubButton');
  86. } else {
  87. $result .= wf_Link(self::URL_ME . '&' . self::ROUTE_FULL . '=true', wf_img('skins/icon_restoredb.png') . ' ' . __('Render full queries'), false, 'ubButton');
  88. }
  89. $result .= wf_Link(self::URL_ME . '&' . self::ROUTE_ZEN . '=true', wf_img('skins/zen.png') . ' ' . __('Zen'), false, 'ubButton');
  90. return($result);
  91. }
  92. /**
  93. * Renders basic report
  94. *
  95. * @return string
  96. */
  97. public function renderReport() {
  98. $result = '';
  99. $all = $this->getProcessList();
  100. if (!empty($all)) {
  101. $count = 0;
  102. $cells = wf_TableCell(__('ID'));
  103. $cells .= wf_TableCell(__('User'));
  104. $cells .= wf_TableCell(__('Host'));
  105. $cells .= wf_TableCell(__('DB'));
  106. $cells .= wf_TableCell(__('Command'));
  107. $cells .= wf_TableCell(__('Time'));
  108. $cells .= wf_TableCell(__('Status'));
  109. $cells .= wf_TableCell(__('Info'));
  110. $rows = wf_TableRow($cells, 'row1');
  111. foreach ($all as $io => $each) {
  112. if (!ispos($each['Info'], 'PROCESSLIST')) {
  113. $cells = wf_TableCell($each['Id']);
  114. $cells .= wf_TableCell($each['User']);
  115. $cells .= wf_TableCell($each['Host']);
  116. $cells .= wf_TableCell($each['db']);
  117. $cells .= wf_TableCell($each['Command']);
  118. $cells .= wf_TableCell($each['Time']);
  119. $cells .= wf_TableCell($each['State']);
  120. $cells .= wf_TableCell($each['Info']);
  121. $rows .= wf_TableRow($cells, 'row5');
  122. $count++;
  123. }
  124. }
  125. $result .= wf_TableBody($rows, '100%', 0, 'sortable');
  126. $result .= __('Total') . ': ' . $count;
  127. } else {
  128. $result .= $this->messages->getStyledMessage(__('Nothing to show') . ': ' . __('Collecting data'), 'warning');
  129. }
  130. return($result);
  131. }
  132. }