Status.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. <?php
  2. /* vim: set expandtab sw=4 ts=4 sts=4: */
  3. /**
  4. * functions for displaying server status
  5. *
  6. * @usedby server_status.php
  7. *
  8. * @package PhpMyAdmin
  9. */
  10. namespace PhpMyAdmin\Server;
  11. use PhpMyAdmin\ReplicationGui;
  12. use PhpMyAdmin\Server\Status\Data;
  13. use PhpMyAdmin\Util;
  14. /**
  15. * PhpMyAdmin\Server\Status
  16. *
  17. * @package PhpMyAdmin
  18. */
  19. class Status
  20. {
  21. /**
  22. * Prints server status information: processes, connections and traffic
  23. *
  24. * @param Data $serverStatusData Server status data
  25. *
  26. * @return string
  27. */
  28. public static function getHtml(Data $serverStatusData)
  29. {
  30. //display the server state General Information
  31. $retval = self::getHtmlForServerStateGeneralInfo($serverStatusData);
  32. //display the server state traffic information
  33. $retval .= self::getHtmlForServerStateTraffic($serverStatusData);
  34. //display the server state connection information
  35. $retval .= self::getHtmlForServerStateConnections($serverStatusData);
  36. // display replication information
  37. if ($GLOBALS['replication_info']['master']['status']
  38. || $GLOBALS['replication_info']['slave']['status']
  39. ) {
  40. $retval .= self::getHtmlForReplicationInfo();
  41. }
  42. return $retval;
  43. }
  44. /**
  45. * Prints server state General information
  46. *
  47. * @param Data $serverStatusData Server status data
  48. *
  49. * @return string
  50. */
  51. public static function getHtmlForServerStateGeneralInfo(Data $serverStatusData)
  52. {
  53. $start_time = $GLOBALS['dbi']->fetchValue(
  54. 'SELECT UNIX_TIMESTAMP() - ' . $serverStatusData->status['Uptime']
  55. );
  56. $retval = '<h3>';
  57. $bytes_received = $serverStatusData->status['Bytes_received'];
  58. $bytes_sent = $serverStatusData->status['Bytes_sent'];
  59. $retval .= sprintf(
  60. __('Network traffic since startup: %s'),
  61. implode(
  62. ' ',
  63. Util::formatByteDown(
  64. $bytes_received + $bytes_sent,
  65. 3,
  66. 1
  67. )
  68. )
  69. );
  70. $retval .= '</h3>';
  71. $retval .= '<p>';
  72. $retval .= sprintf(
  73. __('This MySQL server has been running for %1$s. It started up on %2$s.'),
  74. Util::timespanFormat($serverStatusData->status['Uptime']),
  75. Util::localisedDate($start_time)
  76. ) . "\n";
  77. $retval .= '</p>';
  78. return $retval;
  79. }
  80. /**
  81. * Returns HTML to display replication information
  82. *
  83. * @return string HTML on replication
  84. */
  85. public static function getHtmlForReplicationInfo()
  86. {
  87. $retval = '<p class="notice clearfloat">';
  88. if ($GLOBALS['replication_info']['master']['status']
  89. && $GLOBALS['replication_info']['slave']['status']
  90. ) {
  91. $retval .= __(
  92. 'This MySQL server works as <b>master</b> and '
  93. . '<b>slave</b> in <b>replication</b> process.'
  94. );
  95. } elseif ($GLOBALS['replication_info']['master']['status']) {
  96. $retval .= __(
  97. 'This MySQL server works as <b>master</b> '
  98. . 'in <b>replication</b> process.'
  99. );
  100. } elseif ($GLOBALS['replication_info']['slave']['status']) {
  101. $retval .= __(
  102. 'This MySQL server works as <b>slave</b> '
  103. . 'in <b>replication</b> process.'
  104. );
  105. }
  106. $retval .= '</p>';
  107. /*
  108. * if the server works as master or slave in replication process,
  109. * display useful information
  110. */
  111. $retval .= '<hr class="clearfloat" />';
  112. $retval .= '<h3><a name="replication">';
  113. $retval .= __('Replication status');
  114. $retval .= '</a></h3>';
  115. foreach ($GLOBALS['replication_types'] as $type) {
  116. if (isset($GLOBALS['replication_info'][$type]['status'])
  117. && $GLOBALS['replication_info'][$type]['status']
  118. ) {
  119. $retval .= ReplicationGui::getHtmlForReplicationStatusTable($type);
  120. }
  121. }
  122. return $retval;
  123. }
  124. /**
  125. * Prints server state traffic information
  126. *
  127. * @param Data $serverStatusData Server status data
  128. *
  129. * @return string
  130. */
  131. public static function getHtmlForServerStateTraffic(Data $serverStatusData)
  132. {
  133. $hour_factor = 3600 / $serverStatusData->status['Uptime'];
  134. $retval = '<table id="serverstatustraffic" class="width100 data noclick">';
  135. $retval .= '<thead>';
  136. $retval .= '<tr>';
  137. $retval .= '<th>';
  138. $retval .= __('Traffic') . '&nbsp;';
  139. $retval .= Util::showHint(
  140. __(
  141. 'On a busy server, the byte counters may overrun, so those statistics '
  142. . 'as reported by the MySQL server may be incorrect.'
  143. )
  144. );
  145. $retval .= '</th>';
  146. $retval .= '<th>#</th>';
  147. $retval .= '<th>&oslash; ' . __('per hour') . '</th>';
  148. $retval .= '</tr>';
  149. $retval .= '</thead>';
  150. $retval .= '<tbody>';
  151. $retval .= '<tr>';
  152. $retval .= '<th class="name">' . __('Received') . '</th>';
  153. $retval .= '<td class="value">';
  154. $retval .= implode(
  155. ' ',
  156. Util::formatByteDown(
  157. $serverStatusData->status['Bytes_received'], 3, 1
  158. )
  159. );
  160. $retval .= '</td>';
  161. $retval .= '<td class="value">';
  162. $retval .= implode(
  163. ' ',
  164. Util::formatByteDown(
  165. $serverStatusData->status['Bytes_received'] * $hour_factor, 3, 1
  166. )
  167. );
  168. $retval .= '</td>';
  169. $retval .= '</tr>';
  170. $retval .= '<tr>';
  171. $retval .= '<th class="name">' . __('Sent') . '</th>';
  172. $retval .= '<td class="value">';
  173. $retval .= implode(
  174. ' ',
  175. Util::formatByteDown(
  176. $serverStatusData->status['Bytes_sent'], 3, 1
  177. )
  178. );
  179. $retval .= '</td>';
  180. $retval .= '<td class="value">';
  181. $retval .= implode(
  182. ' ',
  183. Util::formatByteDown(
  184. $serverStatusData->status['Bytes_sent'] * $hour_factor, 3, 1
  185. )
  186. );
  187. $retval .= '</td>';
  188. $retval .= '</tr>';
  189. $retval .= '<tr>';
  190. $retval .= '<th class="name">' . __('Total') . '</th>';
  191. $retval .= '<td class="value">';
  192. $bytes_received = $serverStatusData->status['Bytes_received'];
  193. $bytes_sent = $serverStatusData->status['Bytes_sent'];
  194. $retval .= implode(
  195. ' ',
  196. Util::formatByteDown(
  197. $bytes_received + $bytes_sent, 3, 1
  198. )
  199. );
  200. $retval .= '</td>';
  201. $retval .= '<td class="value">';
  202. $bytes_received = $serverStatusData->status['Bytes_received'];
  203. $bytes_sent = $serverStatusData->status['Bytes_sent'];
  204. $retval .= implode(
  205. ' ',
  206. Util::formatByteDown(
  207. ($bytes_received + $bytes_sent) * $hour_factor, 3, 1
  208. )
  209. );
  210. $retval .= '</td>';
  211. $retval .= '</tr>';
  212. $retval .= '</tbody>';
  213. $retval .= '</table>';
  214. return $retval;
  215. }
  216. /**
  217. * Prints server state connections information
  218. *
  219. * @param Data $serverStatusData Server status data
  220. *
  221. * @return string
  222. */
  223. public static function getHtmlForServerStateConnections(Data $serverStatusData)
  224. {
  225. $hour_factor = 3600 / $serverStatusData->status['Uptime'];
  226. $retval = '<table id="serverstatusconnections" class="width100 data noclick">';
  227. $retval .= '<thead>';
  228. $retval .= '<tr>';
  229. $retval .= '<th>' . __('Connections') . '</th>';
  230. $retval .= '<th>#</th>';
  231. $retval .= '<th>&oslash; ' . __('per hour') . '</th>';
  232. $retval .= '<th>%</th>';
  233. $retval .= '</tr>';
  234. $retval .= '</thead>';
  235. $retval .= '<tbody>';
  236. $retval .= '<tr>';
  237. $retval .= '<th class="name">' . __('Max. concurrent connections') . '</th>';
  238. $retval .= '<td class="value">';
  239. $retval .= Util::formatNumber(
  240. $serverStatusData->status['Max_used_connections'], 0
  241. );
  242. $retval .= '</td>';
  243. $retval .= '<td class="value">--- </td>';
  244. $retval .= '<td class="value">--- </td>';
  245. $retval .= '</tr>';
  246. $retval .= '<tr>';
  247. $retval .= '<th class="name">' . __('Failed attempts') . '</th>';
  248. $retval .= '<td class="value">';
  249. $retval .= Util::formatNumber(
  250. $serverStatusData->status['Aborted_connects'], 4, 1, true
  251. );
  252. $retval .= '</td>';
  253. $retval .= '<td class="value">';
  254. $retval .= Util::formatNumber(
  255. $serverStatusData->status['Aborted_connects'] * $hour_factor, 4, 2, true
  256. );
  257. $retval .= '</td>';
  258. $retval .= '<td class="value">';
  259. if ($serverStatusData->status['Connections'] > 0) {
  260. $abortNum = $serverStatusData->status['Aborted_connects'];
  261. $connectNum = $serverStatusData->status['Connections'];
  262. $retval .= Util::formatNumber(
  263. $abortNum * 100 / $connectNum,
  264. 0, 2, true
  265. );
  266. $retval .= '%';
  267. } else {
  268. $retval .= '--- ';
  269. }
  270. $retval .= '</td>';
  271. $retval .= '</tr>';
  272. $retval .= '<tr>';
  273. $retval .= '<th class="name">' . __('Aborted') . '</th>';
  274. $retval .= '<td class="value">';
  275. $retval .= Util::formatNumber(
  276. $serverStatusData->status['Aborted_clients'], 4, 1, true
  277. );
  278. $retval .= '</td>';
  279. $retval .= '<td class="value">';
  280. $retval .= Util::formatNumber(
  281. $serverStatusData->status['Aborted_clients'] * $hour_factor, 4, 2, true
  282. );
  283. $retval .= '</td>';
  284. $retval .= '<td class="value">';
  285. if ($serverStatusData->status['Connections'] > 0) {
  286. $abortNum = $serverStatusData->status['Aborted_clients'];
  287. $connectNum = $serverStatusData->status['Connections'];
  288. $retval .= Util::formatNumber(
  289. $abortNum * 100 / $connectNum,
  290. 0, 2, true
  291. );
  292. $retval .= '%';
  293. } else {
  294. $retval .= '--- ';
  295. }
  296. $retval .= '</td>';
  297. $retval .= '</tr>';
  298. $retval .= '<tr>';
  299. $retval .= '<th class="name">' . __('Total') . '</th>';
  300. $retval .= '<td class="value">';
  301. $retval .= Util::formatNumber(
  302. $serverStatusData->status['Connections'], 4, 0
  303. );
  304. $retval .= '</td>';
  305. $retval .= '<td class="value">';
  306. $retval .= Util::formatNumber(
  307. $serverStatusData->status['Connections'] * $hour_factor, 4, 2
  308. );
  309. $retval .= '</td>';
  310. $retval .= '<td class="value">';
  311. $retval .= Util::formatNumber(100, 0, 2);
  312. $retval .= '%</td>';
  313. $retval .= '</tr>';
  314. $retval .= '</tbody>';
  315. $retval .= '</table>';
  316. return $retval;
  317. }
  318. }