server_status.php 16 KB

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