server_status.lib.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543
  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. if (! defined('PHPMYADMIN')) {
  11. exit;
  12. }
  13. /**
  14. * Prints server status information: processes, connections and traffic
  15. *
  16. * @param PMA_ServerStatusData $ServerStatusData Server status data
  17. *
  18. * @return string
  19. */
  20. function PMA_getHtmlForServerStatus($ServerStatusData)
  21. {
  22. //display the server state General Information
  23. $retval = PMA_getHtmlForServerStateGeneralInfo($ServerStatusData);
  24. //display the server state traffic information
  25. $retval .= PMA_getHtmlForServerStateTraffic($ServerStatusData);
  26. //display the server state connection information
  27. $retval .= PMA_getHtmlForServerStateConnections($ServerStatusData);
  28. //display the server Process List information
  29. $retval .= PMA_getHtmlForServerProcesslist($ServerStatusData);
  30. return $retval;
  31. }
  32. /**
  33. * Prints server state General information
  34. *
  35. * @param PMA_ServerStatusData $ServerStatusData Server status data
  36. *
  37. * @return string
  38. */
  39. function PMA_getHtmlForServerStateGeneralInfo($ServerStatusData)
  40. {
  41. $start_time = $GLOBALS['dbi']->fetchValue(
  42. 'SELECT UNIX_TIMESTAMP() - ' . $ServerStatusData->status['Uptime']
  43. );
  44. $retval = '<h3>';
  45. $bytes_received = $ServerStatusData->status['Bytes_received'];
  46. $bytes_sent = $ServerStatusData->status['Bytes_sent'];
  47. $retval .= sprintf(
  48. __('Network traffic since startup: %s'),
  49. implode(
  50. ' ',
  51. PMA_Util::formatByteDown(
  52. $bytes_received + $bytes_sent,
  53. 3,
  54. 1
  55. )
  56. )
  57. );
  58. $retval .= '</h3>';
  59. $retval .= '<p>';
  60. $retval .= sprintf(
  61. __('This MySQL server has been running for %1$s. It started up on %2$s.'),
  62. PMA_Util::timespanFormat($ServerStatusData->status['Uptime']),
  63. PMA_Util::localisedDate($start_time)
  64. ) . "\n";
  65. $retval .= '</p>';
  66. if ($GLOBALS['server_master_status'] || $GLOBALS['server_slave_status']) {
  67. $retval .= '<p class="notice">';
  68. if ($GLOBALS['server_master_status'] && $GLOBALS['server_slave_status']) {
  69. $retval .= __(
  70. 'This MySQL server works as <b>master</b> and '
  71. . '<b>slave</b> in <b>replication</b> process.'
  72. );
  73. } elseif ($GLOBALS['server_master_status']) {
  74. $retval .= __(
  75. 'This MySQL server works as <b>master</b> '
  76. . 'in <b>replication</b> process.'
  77. );
  78. } elseif ($GLOBALS['server_slave_status']) {
  79. $retval .= __(
  80. 'This MySQL server works as <b>slave</b> '
  81. . 'in <b>replication</b> process.'
  82. );
  83. }
  84. $retval .= '</p>';
  85. }
  86. /*
  87. * if the server works as master or slave in replication process,
  88. * display useful information
  89. */
  90. if ($GLOBALS['server_master_status'] || $GLOBALS['server_slave_status']) {
  91. $retval .= '<hr class="clearfloat" />';
  92. $retval .= '<h3><a name="replication">';
  93. $retval .= __('Replication status');
  94. $retval .= '</a></h3>';
  95. foreach ($GLOBALS['replication_types'] as $type) {
  96. if (isset(${"server_{$type}_status"}) && ${"server_{$type}_status"}) {
  97. $retval .= PMA_getHtmlForReplicationStatusTable($type);
  98. }
  99. }
  100. }
  101. return $retval;
  102. }
  103. /**
  104. * Prints server state traffic information
  105. *
  106. * @param PMA_ServerStatusData $ServerStatusData Server status data
  107. *
  108. * @return string
  109. */
  110. function PMA_getHtmlForServerStateTraffic($ServerStatusData)
  111. {
  112. $hour_factor = 3600 / $ServerStatusData->status['Uptime'];
  113. $retval = '<table id="serverstatustraffic" class="data noclick">';
  114. $retval .= '<thead>';
  115. $retval .= '<tr>';
  116. $retval .= '<th colspan="2">';
  117. $retval .= __('Traffic') . '&nbsp;';
  118. $retval .= PMA_Util::showHint(
  119. __(
  120. 'On a busy server, the byte counters may overrun, so those statistics '
  121. . 'as reported by the MySQL server may be incorrect.'
  122. )
  123. );
  124. $retval .= '</th>';
  125. $retval .= '<th>&oslash; ' . __('per hour') . '</th>';
  126. $retval .= '</tr>';
  127. $retval .= '</thead>';
  128. $retval .= '<tbody>';
  129. $retval .= '<tr class="odd">';
  130. $retval .= '<th class="name">' . __('Received') . '</th>';
  131. $retval .= '<td class="value">';
  132. $retval .= implode(
  133. ' ',
  134. PMA_Util::formatByteDown(
  135. $ServerStatusData->status['Bytes_received'], 3, 1
  136. )
  137. );
  138. $retval .= '</td>';
  139. $retval .= '<td class="value">';
  140. $retval .= implode(
  141. ' ',
  142. PMA_Util::formatByteDown(
  143. $ServerStatusData->status['Bytes_received'] * $hour_factor, 3, 1
  144. )
  145. );
  146. $retval .= '</td>';
  147. $retval .= '</tr>';
  148. $retval .= '<tr class="even">';
  149. $retval .= '<th class="name">' . __('Sent') . '</th>';
  150. $retval .= '<td class="value">';
  151. $retval .= implode(
  152. ' ',
  153. PMA_Util::formatByteDown(
  154. $ServerStatusData->status['Bytes_sent'], 3, 1
  155. )
  156. );
  157. $retval .= '</td>';
  158. $retval .= '<td class="value">';
  159. $retval .= implode(
  160. ' ',
  161. PMA_Util::formatByteDown(
  162. $ServerStatusData->status['Bytes_sent'] * $hour_factor, 3, 1
  163. )
  164. );
  165. $retval .= '</td>';
  166. $retval .= '</tr>';
  167. $retval .= '<tr class="odd">';
  168. $retval .= '<th class="name">' . __('Total') . '</th>';
  169. $retval .= '<td class="value">';
  170. $bytes_received = $ServerStatusData->status['Bytes_received'];
  171. $bytes_sent = $ServerStatusData->status['Bytes_sent'];
  172. $retval .= implode(
  173. ' ',
  174. PMA_Util::formatByteDown(
  175. $bytes_received + $bytes_sent, 3, 1
  176. )
  177. );
  178. $retval .= '</td>';
  179. $retval .= '<td class="value">';
  180. $bytes_received = $ServerStatusData->status['Bytes_received'];
  181. $bytes_sent = $ServerStatusData->status['Bytes_sent'];
  182. $retval .= implode(
  183. ' ',
  184. PMA_Util::formatByteDown(
  185. ($bytes_received + $bytes_sent) * $hour_factor, 3, 1
  186. )
  187. );
  188. $retval .= '</td>';
  189. $retval .= '</tr>';
  190. $retval .= '</tbody>';
  191. $retval .= '</table>';
  192. return $retval;
  193. }
  194. /**
  195. * Prints server state connections information
  196. *
  197. * @param PMA_ServerStatusData $ServerStatusData Server status data
  198. *
  199. * @return string
  200. */
  201. function PMA_getHtmlForServerStateConnections($ServerStatusData)
  202. {
  203. $hour_factor = 3600 / $ServerStatusData->status['Uptime'];
  204. $retval = '<table id="serverstatusconnections" class="data noclick">';
  205. $retval .= '<thead>';
  206. $retval .= '<tr>';
  207. $retval .= '<th colspan="2">' . __('Connections') . '</th>';
  208. $retval .= '<th>&oslash; ' . __('per hour') . '</th>';
  209. $retval .= '<th>%</th>';
  210. $retval .= '</tr>';
  211. $retval .= '</thead>';
  212. $retval .= '<tbody>';
  213. $retval .= '<tr class="odd">';
  214. $retval .= '<th class="name">' . __('max. concurrent connections') . '</th>';
  215. $retval .= '<td class="value">';
  216. $retval .= PMA_Util::formatNumber(
  217. $ServerStatusData->status['Max_used_connections'], 0
  218. );
  219. $retval .= '</td>';
  220. $retval .= '<td class="value">--- </td>';
  221. $retval .= '<td class="value">--- </td>';
  222. $retval .= '</tr>';
  223. $retval .= '<tr class="even">';
  224. $retval .= '<th class="name">' . __('Failed attempts') . '</th>';
  225. $retval .= '<td class="value">';
  226. $retval .= PMA_Util::formatNumber(
  227. $ServerStatusData->status['Aborted_connects'], 4, 1, true
  228. );
  229. $retval .= '</td>';
  230. $retval .= '<td class="value">';
  231. $retval .= PMA_Util::formatNumber(
  232. $ServerStatusData->status['Aborted_connects'] * $hour_factor, 4, 2, true
  233. );
  234. $retval .= '</td>';
  235. $retval .= '<td class="value">';
  236. if ($ServerStatusData->status['Connections'] > 0) {
  237. $abortNum = $ServerStatusData->status['Aborted_connects'];
  238. $connectNum = $ServerStatusData->status['Connections'];
  239. $retval .= PMA_Util::formatNumber(
  240. $abortNum * 100 / $connectNum,
  241. 0, 2, true
  242. );
  243. $retval .= '%';
  244. } else {
  245. $retval .= '--- ';
  246. }
  247. $retval .= '</td>';
  248. $retval .= '</tr>';
  249. $retval .= '<tr class="odd">';
  250. $retval .= '<th class="name">' . __('Aborted') . '</th>';
  251. $retval .= '<td class="value">';
  252. $retval .= PMA_Util::formatNumber(
  253. $ServerStatusData->status['Aborted_clients'], 4, 1, true
  254. );
  255. $retval .= '</td>';
  256. $retval .= '<td class="value">';
  257. $retval .= PMA_Util::formatNumber(
  258. $ServerStatusData->status['Aborted_clients'] * $hour_factor, 4, 2, true
  259. );
  260. $retval .= '</td>';
  261. $retval .= '<td class="value">';
  262. if ($ServerStatusData->status['Connections'] > 0) {
  263. $abortNum = $ServerStatusData->status['Aborted_clients'];
  264. $connectNum = $ServerStatusData->status['Connections'];
  265. $retval .= PMA_Util::formatNumber(
  266. $abortNum * 100 / $connectNum,
  267. 0, 2, true
  268. );
  269. $retval .= '%';
  270. } else {
  271. $retval .= '--- ';
  272. }
  273. $retval .= '</td>';
  274. $retval .= '</tr>';
  275. $retval .= '<tr class="even">';
  276. $retval .= '<th class="name">' . __('Total') . '</th>';
  277. $retval .= '<td class="value">';
  278. $retval .= PMA_Util::formatNumber(
  279. $ServerStatusData->status['Connections'], 4, 0
  280. );
  281. $retval .= '</td>';
  282. $retval .= '<td class="value">';
  283. $retval .= PMA_Util::formatNumber(
  284. $ServerStatusData->status['Connections'] * $hour_factor, 4, 2
  285. );
  286. $retval .= '</td>';
  287. $retval .= '<td class="value">';
  288. $retval .= PMA_Util::formatNumber(100, 0, 2);
  289. $retval .= '%</td>';
  290. $retval .= '</tr>';
  291. $retval .= '</tbody>';
  292. $retval .= '</table>';
  293. return $retval;
  294. }
  295. /**
  296. * Prints Server Process list
  297. *
  298. * @return string
  299. */
  300. function PMA_getHtmlForServerProcesslist()
  301. {
  302. $url_params = array();
  303. $show_full_sql = ! empty($_REQUEST['full']);
  304. if ($show_full_sql) {
  305. $url_params['full'] = 1;
  306. $full_text_link = 'server_status.php' . PMA_URL_getCommon(
  307. array(), 'html', '?'
  308. );
  309. } else {
  310. $full_text_link = 'server_status.php' . PMA_URL_getCommon(
  311. array('full' => 1)
  312. );
  313. }
  314. // This array contains display name and real column name of each
  315. // sortable column in the table
  316. $sortable_columns = array(
  317. array(
  318. 'column_name' => __('ID'),
  319. 'order_by_field' => 'Id'
  320. ),
  321. array(
  322. 'column_name' => __('User'),
  323. 'order_by_field' => 'User'
  324. ),
  325. array(
  326. 'column_name' => __('Host'),
  327. 'order_by_field' => 'Host'
  328. ),
  329. array(
  330. 'column_name' => __('Database'),
  331. 'order_by_field' => 'db'
  332. ),
  333. array(
  334. 'column_name' => __('Command'),
  335. 'order_by_field' => 'Command'
  336. ),
  337. array(
  338. 'column_name' => __('Time'),
  339. 'order_by_field' => 'Time'
  340. ),
  341. array(
  342. 'column_name' => __('Status'),
  343. 'order_by_field' => 'State'
  344. ),
  345. array(
  346. 'column_name' => __('SQL query'),
  347. 'order_by_field' => 'Info'
  348. )
  349. );
  350. $sortableColCount = count($sortable_columns);
  351. if (PMA_DRIZZLE) {
  352. $left_str = 'left(p.info, '
  353. . (int)$GLOBALS['cfg']['MaxCharactersInDisplayedSQL'] . ')';
  354. $sql_query = "SELECT
  355. p.id AS Id,
  356. p.username AS User,
  357. p.host AS Host,
  358. p.db AS db,
  359. p.command AS Command,
  360. p.time AS Time,
  361. p.state AS State,"
  362. . ($show_full_sql ? 's.query' : $left_str )
  363. . " AS Info FROM data_dictionary.PROCESSLIST p "
  364. . ($show_full_sql
  365. ? 'LEFT JOIN data_dictionary.SESSIONS s ON s.session_id = p.id'
  366. : '');
  367. if (! empty($_REQUEST['order_by_field'])
  368. && ! empty($_REQUEST['sort_order'])
  369. ) {
  370. $sql_query .= ' ORDER BY p.' . $_REQUEST['order_by_field'] . ' '
  371. . $_REQUEST['sort_order'];
  372. }
  373. } else {
  374. $sql_query = $show_full_sql
  375. ? 'SHOW FULL PROCESSLIST'
  376. : 'SHOW PROCESSLIST';
  377. if (! empty($_REQUEST['order_by_field'])
  378. && ! empty($_REQUEST['sort_order'])
  379. ) {
  380. $sql_query = 'SELECT * FROM `INFORMATION_SCHEMA`.`PROCESSLIST` '
  381. . 'ORDER BY `'
  382. . $_REQUEST['order_by_field'] . '` ' . $_REQUEST['sort_order'];
  383. }
  384. }
  385. $result = $GLOBALS['dbi']->query($sql_query);
  386. $retval = '<table id="tableprocesslist" '
  387. . 'class="data clearfloat noclick sortable">';
  388. $retval .= '<thead>';
  389. $retval .= '<tr>';
  390. $retval .= '<th>' . __('Processes') . '</th>';
  391. foreach ($sortable_columns as $column) {
  392. $is_sorted = ! empty($_REQUEST['order_by_field'])
  393. && ! empty($_REQUEST['sort_order'])
  394. && ($_REQUEST['order_by_field'] == $column['order_by_field']);
  395. $column['sort_order'] = 'ASC';
  396. if ($is_sorted && $_REQUEST['sort_order'] === 'ASC') {
  397. $column['sort_order'] = 'DESC';
  398. }
  399. if ($is_sorted) {
  400. if ($_REQUEST['sort_order'] == 'ASC') {
  401. $asc_display_style = 'inline';
  402. $desc_display_style = 'none';
  403. } elseif ($_REQUEST['sort_order'] == 'DESC') {
  404. $desc_display_style = 'inline';
  405. $asc_display_style = 'none';
  406. }
  407. }
  408. $retval .= '<th>';
  409. $columnUrl = PMA_URL_getCommon($column);
  410. $retval .= '<a href="server_status.php' . $columnUrl . '" ';
  411. if ($is_sorted) {
  412. $retval .= 'onmouseout="$(\'.soimg\').toggle()" '
  413. . 'onmouseover="$(\'.soimg\').toggle()"';
  414. }
  415. $retval .= '>';
  416. $retval .= $column['column_name'];
  417. if ($is_sorted) {
  418. $retval .= '<img class="icon ic_s_desc soimg" alt="'
  419. . __('Descending') . '" title="" src="themes/dot.gif" '
  420. . 'style="display: ' . $desc_display_style . '" />';
  421. $retval .= '<img class="icon ic_s_asc soimg hide" alt="'
  422. . __('Ascending') . '" title="" src="themes/dot.gif" '
  423. . 'style="display: ' . $asc_display_style . '" />';
  424. }
  425. $retval .= '</a>';
  426. if (! PMA_DRIZZLE && (0 === --$sortableColCount)) {
  427. $retval .= '<a href="' . $full_text_link . '">';
  428. if ($show_full_sql) {
  429. $retval .= PMA_Util::getImage(
  430. 's_partialtext.png',
  431. __('Truncate Shown Queries')
  432. );
  433. } else {
  434. $retval .= PMA_Util::getImage(
  435. 's_fulltext.png',
  436. __('Show Full Queries')
  437. );
  438. }
  439. $retval .= '</a>';
  440. }
  441. $retval .= '</th>';
  442. }
  443. $retval .= '</tr>';
  444. $retval .= '</thead>';
  445. $retval .= '<tbody>';
  446. $odd_row = true;
  447. while ($process = $GLOBALS['dbi']->fetchAssoc($result)) {
  448. $retval .= PMA_getHtmlForServerProcessItem(
  449. $process,
  450. $odd_row,
  451. $show_full_sql
  452. );
  453. $odd_row = ! $odd_row;
  454. }
  455. $retval .= '</tbody>';
  456. $retval .= '</table>';
  457. return $retval;
  458. }
  459. /**
  460. * Prints Every Item of Server Process
  461. *
  462. * @param Array $process data of Every Item of Server Process
  463. * @param bool $odd_row display odd row or not
  464. * @param bool $show_full_sql show full sql or not
  465. *
  466. * @return string
  467. */
  468. function PMA_getHtmlForServerProcessItem($process, $odd_row, $show_full_sql)
  469. {
  470. // Array keys need to modify due to the way it has used
  471. // to display column values
  472. if (! empty($_REQUEST['order_by_field']) && ! empty($_REQUEST['sort_order']) ) {
  473. foreach (array_keys($process) as $key) {
  474. $new_key = ucfirst(strtolower($key));
  475. $process[$new_key] = $process[$key];
  476. unset($process[$key]);
  477. }
  478. }
  479. $url_params = array();
  480. $url_params['kill'] = $process['Id'];
  481. $kill_process = 'server_status.php' . PMA_URL_getCommon($url_params);
  482. $retval = '<tr class="' . ($odd_row ? 'odd' : 'even') . '">';
  483. $retval .= '<td><a href="' . $kill_process . '">' . __('Kill') . '</a></td>';
  484. $retval .= '<td class="value">' . $process['Id'] . '</td>';
  485. $retval .= '<td>' . htmlspecialchars($process['User']) . '</td>';
  486. $retval .= '<td>' . htmlspecialchars($process['Host']) . '</td>';
  487. $retval .= '<td>' . ((! isset($process['db']) || ! strlen($process['db']))
  488. ? '<i>' . __('None') . '</i>'
  489. : htmlspecialchars($process['db'])) . '</td>';
  490. $retval .= '<td>' . htmlspecialchars($process['Command']) . '</td>';
  491. $retval .= '<td class="value">' . $process['Time'] . '</td>';
  492. $processStatusStr = empty($process['State']) ? '---' : $process['State'];
  493. $retval .= '<td>' . $processStatusStr . '</td>';
  494. $retval .= '<td>';
  495. if (empty($process['Info'])) {
  496. $retval .= '---';
  497. } else {
  498. $retval .= PMA_Util::formatSql($process['Info'], ! $show_full_sql);
  499. }
  500. $retval .= '</td>';
  501. $retval .= '</tr>';
  502. return $retval;
  503. }
  504. ?>