Merge pull request #431 from xmujay/0609_monitor
[phpmyadmin/aamir.git] / server_status.php
blob690432e74e16418f4d4f5f82b87566b9b4400b7b
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * object the server status page: processes, connections and traffic
6 * @package PhpMyAdmin
7 */
9 require_once 'libraries/common.inc.php';
10 require_once 'libraries/server_common.inc.php';
11 require_once 'libraries/ServerStatusData.class.php';
13 /**
14 * Replication library
16 if (PMA_DRIZZLE) {
17 $server_master_status = false;
18 $server_slave_status = false;
19 } else {
20 include_once 'libraries/replication.inc.php';
21 include_once 'libraries/replication_gui.lib.php';
24 $ServerStatusData = new PMA_ServerStatusData();
26 /**
27 * Kills a selected process
29 if (! empty($_REQUEST['kill'])) {
30 if ($GLOBALS['dbi']->tryQuery('KILL ' . $_REQUEST['kill'] . ';')) {
31 $message = PMA_Message::success(__('Thread %s was successfully killed.'));
32 } else {
33 $message = PMA_Message::error(
34 __(
35 'phpMyAdmin was unable to kill thread %s.'
36 . ' It probably has already been closed.'
40 $message->addParam($_REQUEST['kill']);
43 /**
44 * start output
46 $response = PMA_Response::getInstance();
47 $response->addHTML('<div>');
48 $response->addHTML($ServerStatusData->getMenuHtml());
49 $response->addHTML(PMA_getServerStatusHtml($ServerStatusData));
50 $response->addHTML('</div>');
52 exit;
54 /**
55 * Prints server status information: processes, connections and traffic
57 * @param Object $ServerStatusData An instance of the PMA_ServerStatusData class
59 * @return string
61 function PMA_getServerStatusHtml($ServerStatusData)
63 //display the server state General Information
64 $retval = PMA_getServerStateGeneralInfoHtml($ServerStatusData);
66 //display the server state traffic information
67 $retval .= PMA_getServerStateTrafficHtml($ServerStatusData);
69 //display the server state connection information
70 $retval .= PMA_getServerStateConnectionsHtml($ServerStatusData);
72 //display the server Process List information
73 $retval .= PMA_getServerProcesslistHtml($ServerStatusData);
75 return $retval;
78 /**
79 * Prints server state General information
81 * @param Object $ServerStatusData An instance of the PMA_ServerStatusData class
83 * @return string
85 function PMA_getServerStateGeneralInfoHtml($ServerStatusData)
87 $start_time = $GLOBALS['dbi']->fetchValue(
88 'SELECT UNIX_TIMESTAMP() - ' . $ServerStatusData->status['Uptime']
91 $retval = '<h3>';
92 $retval .= sprintf(
93 __('Network traffic since startup: %s'),
94 implode(
95 ' ',
96 PMA_Util::formatByteDown(
97 $ServerStatusData->status['Bytes_received'] + $ServerStatusData->status['Bytes_sent'],
103 $retval .= '</h3>';
104 $retval .= '<p>';
105 $retval .= sprintf(
106 __('This MySQL server has been running for %1$s. It started up on %2$s.'),
107 PMA_Util::timespanFormat($ServerStatusData->status['Uptime']),
108 PMA_Util::localisedDate($start_time)
109 ) . "\n";
110 $retval .= '</p>';
112 if ($GLOBALS['server_master_status'] || $GLOBALS['server_slave_status']) {
113 $retval .= '<p class="notice">';
114 if ($GLOBALS['server_master_status'] && $GLOBALS['server_slave_status']) {
115 $retval .= __(
116 'This MySQL server works as <b>master</b> and '
117 . '<b>slave</b> in <b>replication</b> process.'
119 } elseif ($GLOBALS['server_master_status']) {
120 $retval .= __(
121 'This MySQL server works as <b>master</b> '
122 . 'in <b>replication</b> process.'
124 } elseif ($GLOBALS['server_slave_status']) {
125 $retval .= __(
126 'This MySQL server works as <b>slave</b> '
127 . 'in <b>replication</b> process.'
130 $retval .= '</p>';
134 * if the server works as master or slave in replication process,
135 * display useful information
137 if ($GLOBALS['server_master_status'] || $GLOBALS['server_slave_status']) {
138 $retval .= '<hr class="clearfloat" />';
139 $retval .= '<h3><a name="replication">';
140 $retval .= __('Replication status');
141 $retval .= '</a></h3>';
142 foreach ($GLOBALS['replication_types'] as $type) {
143 if (isset(${"server_{$type}_status"}) && ${"server_{$type}_status"}) {
144 PMA_replication_print_status_table($type);
149 return $retval;
153 * Prints server state traffic information
155 * @param Object $ServerStatusData An instance of the PMA_ServerStatusData class
157 * @return string
159 function PMA_getServerStateTrafficHtml($ServerStatusData)
161 $hour_factor = 3600 / $ServerStatusData->status['Uptime'];
162 $retval = '<table id="serverstatustraffic" class="data noclick">';
163 $retval .= '<thead>';
164 $retval .= '<tr>';
165 $retval .= '<th colspan="2">';
166 $retval .= __('Traffic') . '&nbsp;';
167 $retval .= PMA_Util::showHint(
169 'On a busy server, the byte counters may overrun, so those statistics '
170 . 'as reported by the MySQL server may be incorrect.'
173 $retval .= '</th>';
174 $retval .= '<th>&oslash; ' . __('per hour') . '</th>';
175 $retval .= '</tr>';
176 $retval .= '</thead>';
177 $retval .= '<tbody>';
178 $retval .= '<tr class="odd">';
179 $retval .= '<th class="name">' . __('Received') . '</th>';
180 $retval .= '<td class="value">';
181 $retval .= implode(
182 ' ',
183 PMA_Util::formatByteDown(
184 $ServerStatusData->status['Bytes_received'], 3, 1
187 $retval .= '</td>';
188 $retval .= '<td class="value">';
189 $retval .= implode(
190 ' ',
191 PMA_Util::formatByteDown(
192 $ServerStatusData->status['Bytes_received'] * $hour_factor, 3, 1
195 $retval .= '</td>';
196 $retval .= '</tr>';
197 $retval .= '<tr class="even">';
198 $retval .= '<th class="name">' . __('Sent') . '</th>';
199 $retval .= '<td class="value">';
200 $retval .= implode(
201 ' ',
202 PMA_Util::formatByteDown(
203 $ServerStatusData->status['Bytes_sent'], 3, 1
206 $retval .= '</td>';
207 $retval .= '<td class="value"><?php echo';
208 $retval .= implode(
209 ' ',
210 PMA_Util::formatByteDown(
211 $ServerStatusData->status['Bytes_sent'] * $hour_factor, 3, 1
214 $retval .= '</td>';
215 $retval .= '</tr>';
216 $retval .= '<tr class="odd">';
217 $retval .= '<th class="name">' . __('Total') . '</th>';
218 $retval .= '<td class="value">';
219 $retval .= implode(
220 ' ',
221 PMA_Util::formatByteDown(
222 $ServerStatusData->status['Bytes_received'] + $ServerStatusData->status['Bytes_sent'], 3, 1
225 $retval .= '</td>';
226 $retval .= '<td class="value">';
227 $retval .= implode(
228 ' ',
229 PMA_Util::formatByteDown(
230 ($ServerStatusData->status['Bytes_received'] + $ServerStatusData->status['Bytes_sent'])
231 * $hour_factor, 3, 1
234 $retval .= '</td>';
235 $retval .= '</tr>';
236 $retval .= '</tbody>';
237 $retval .= '</table>';
238 return $retval;
242 * Prints server state connections information
244 * @param Object $ServerStatusData An instance of the PMA_ServerStatusData class
246 * @return string
248 function PMA_getServerStateConnectionsHtml($ServerStatusData)
250 $hour_factor = 3600 / $ServerStatusData->status['Uptime'];
251 $retval = '<table id="serverstatusconnections" class="data noclick">';
252 $retval .= '<thead>';
253 $retval .= '<tr>';
254 $retval .= '<th colspan="2">' . __('Connections') . '</th>';
255 $retval .= '<th>&oslash; ' . __('per hour') . '</th>';
256 $retval .= '<th>%</th>';
257 $retval .= '</tr>';
258 $retval .= '</thead>';
259 $retval .= '<tbody>';
260 $retval .= '<tr class="odd">';
261 $retval .= '<th class="name">' . __('max. concurrent connections') . '</th>';
262 $retval .= '<td class="value">';
263 $retval .= PMA_Util::formatNumber(
264 $ServerStatusData->status['Max_used_connections'], 0
266 $retval .= '</td>';
267 $retval .= '<td class="value">--- </td>';
268 $retval .= '<td class="value">--- </td>';
269 $retval .= '</tr>';
270 $retval .= '<tr class="even">';
271 $retval .= '<th class="name">' . __('Failed attempts') . '</th>';
272 $retval .= '<td class="value">';
273 $retval .= PMA_Util::formatNumber(
274 $ServerStatusData->status['Aborted_connects'], 4, 1, true
276 $retval .= '</td>';
277 $retval .= '<td class="value">';
278 $retval .= PMA_Util::formatNumber(
279 $ServerStatusData->status['Aborted_connects'] * $hour_factor, 4, 2, true
281 $retval .= '</td>';
282 $retval .= '<td class="value">';
283 if ($ServerStatusData->status['Connections'] > 0) {
284 $retval .= PMA_Util::formatNumber(
285 $ServerStatusData->status['Aborted_connects'] * 100 / $ServerStatusData->status['Connections'],
286 0, 2, true
288 $retval .= '%';
289 } else {
290 $retval .= '--- ';
292 $retval .= '</td>';
293 $retval .= '</tr>';
294 $retval .= '<tr class="odd">';
295 $retval .= '<th class="name">' . __('Aborted') . '</th>';
296 $retval .= '<td class="value">';
297 $retval .= PMA_Util::formatNumber(
298 $ServerStatusData->status['Aborted_clients'], 4, 1, true
300 $retval .= '</td>';
301 $retval .= '<td class="value">';
302 $retval .= PMA_Util::formatNumber(
303 $ServerStatusData->status['Aborted_clients'] * $hour_factor, 4, 2, true
305 $retval .= '</td>';
306 $retval .= '<td class="value">';
307 if ($ServerStatusData->status['Connections'] > 0) {
308 $retval .= PMA_Util::formatNumber(
309 $ServerStatusData->status['Aborted_clients'] * 100 / $ServerStatusData->status['Connections'],
310 0, 2, true
312 $retval .= '%';
313 } else {
314 $retval .= '--- ';
316 $retval .= '</td>';
317 $retval .= '</tr>';
318 $retval .= '<tr class="even">';
319 $retval .= '<th class="name">' . __('Total') . '</th>';
320 $retval .= '<td class="value">';
321 $retval .= PMA_Util::formatNumber(
322 $ServerStatusData->status['Connections'], 4, 0
324 $retval .= '</td>';
325 $retval .= '<td class="value">';
326 $retval .= PMA_Util::formatNumber(
327 $ServerStatusData->status['Connections'] * $hour_factor, 4, 2
329 $retval .= '</td>';
330 $retval .= '<td class="value">';
331 $retval .= PMA_Util::formatNumber(100, 0, 2);
332 $retval .= '%</td>';
333 $retval .= '</tr>';
334 $retval .= '</tbody>';
335 $retval .= '</table>';
337 return $retval;
341 * Prints Server Process list
343 * @param Object $ServerStatusData An instance of the PMA_ServerStatusData class
345 * @return string
347 function PMA_getServerProcesslistHtml($ServerStatusData)
349 $url_params = array();
351 $show_full_sql = ! empty($_REQUEST['full']);
352 if ($show_full_sql) {
353 $url_params['full'] = 1;
354 $full_text_link = 'server_status.php' . PMA_generate_common_url(
355 array(), 'html', '?'
357 } else {
358 $full_text_link = 'server_status.php' . PMA_generate_common_url(
359 array('full' => 1)
363 // This array contains display name and real column name of each
364 // sortable column in the table
365 $sortable_columns = array(
366 array(
367 'column_name' => __('ID'),
368 'order_by_field' => 'Id'
370 array(
371 'column_name' => __('User'),
372 'order_by_field' => 'User'
374 array(
375 'column_name' => __('Host'),
376 'order_by_field' => 'Host'
378 array(
379 'column_name' => __('Database'),
380 'order_by_field' => 'db'
382 array(
383 'column_name' => __('Command'),
384 'order_by_field' => 'Command'
386 array(
387 'column_name' => __('Time'),
388 'order_by_field' => 'Time'
390 array(
391 'column_name' => __('Status'),
392 'order_by_field' => 'State'
394 array(
395 'column_name' => __('SQL query'),
396 'order_by_field' => 'Info'
399 $sortable_columns_count = count($sortable_columns);
401 if (PMA_DRIZZLE) {
402 $left_str = 'left(p.info, '
403 . (int)$GLOBALS['cfg']['MaxCharactersInDisplayedSQL'] . ')';
404 $sql_query = "SELECT
405 p.id AS Id,
406 p.username AS User,
407 p.host AS Host,
408 p.db AS db,
409 p.command AS Command,
410 p.time AS Time,
411 p.state AS State,"
412 . ($show_full_sql ? 's.query' : $left_str )
413 . " AS Info FROM data_dictionary.PROCESSLIST p "
414 . ($show_full_sql
415 ? 'LEFT JOIN data_dictionary.SESSIONS s ON s.session_id = p.id'
416 : '');
417 if (! empty($_REQUEST['order_by_field'])
418 && ! empty($_REQUEST['sort_order'])
420 $sql_query .= ' ORDER BY p.' . $_REQUEST['order_by_field'] . ' '
421 . $_REQUEST['sort_order'];
423 } else {
424 $sql_query = $show_full_sql
425 ? 'SHOW FULL PROCESSLIST'
426 : 'SHOW PROCESSLIST';
427 if (! empty($_REQUEST['order_by_field'])
428 && ! empty($_REQUEST['sort_order'])
430 $sql_query = 'SELECT * FROM `INFORMATION_SCHEMA`.`PROCESSLIST` ORDER BY `'
431 . $_REQUEST['order_by_field'] . '` ' . $_REQUEST['sort_order'];
435 $result = $GLOBALS['dbi']->query($sql_query);
437 $retval = '<table id="tableprocesslist" class="data clearfloat noclick sortable">';
438 $retval .= '<thead>';
439 $retval .= '<tr>';
440 $retval .= '<th>' . __('Processes') . '</th>';
441 foreach ($sortable_columns as $column) {
443 $is_sorted = ! empty($_REQUEST['order_by_field'])
444 && ! empty($_REQUEST['sort_order'])
445 && ($_REQUEST['order_by_field'] == $column['order_by_field']);
447 $column['sort_order'] = 'ASC';
448 if ($is_sorted && $_REQUEST['sort_order'] === 'ASC') {
449 $column['sort_order'] = 'DESC';
452 if ($is_sorted) {
453 if ($_REQUEST['sort_order'] == 'ASC') {
454 $asc_display_style = 'inline';
455 $desc_display_style = 'none';
456 } elseif ($_REQUEST['sort_order'] == 'DESC') {
457 $desc_display_style = 'inline';
458 $asc_display_style = 'none';
462 $retval .= '<th>';
463 $retval .= '<a href="server_status.php' . PMA_generate_common_url($column) . '" ';
464 if ($is_sorted) {
465 $retval .= 'onmouseout="$(\'.soimg\').toggle()" '
466 . 'onmouseover="$(\'.soimg\').toggle()"';
468 $retval .= '>';
470 $retval .= $column['column_name'];
472 if ($is_sorted) {
473 $retval .= '<img class="icon ic_s_desc soimg" alt="'
474 . __('Descending') . '" title="" src="themes/dot.gif" '
475 . 'style="display: ' . $desc_display_style . '" />';
476 $retval .= '<img class="icon ic_s_asc soimg hide" alt="'
477 . __('Ascending') . '" title="" src="themes/dot.gif" '
478 . 'style="display: ' . $asc_display_style . '" />';
481 $retval .= '</a>';
483 if (! PMA_DRIZZLE && (0 === --$sortable_columns_count)) {
484 $retval .= '<a href="' . $full_text_link . '">';
485 if ($show_full_sql) {
486 $retval .= PMA_Util::getImage(
487 's_partialtext.png',
488 __('Truncate Shown Queries')
490 } else {
491 $retval .= PMA_Util::getImage(
492 's_fulltext.png',
493 __('Show Full Queries')
496 $retval .= '</a>';
498 $retval .= '</th>';
501 $retval .= '</tr>';
502 $retval .= '</thead>';
503 $retval .= '<tbody>';
505 $odd_row = true;
506 while ($process = $GLOBALS['dbi']->fetchAssoc($result)) {
508 // Array keys need to modify due to the way it has used
509 // to display column values
510 if (! empty($_REQUEST['order_by_field'])
511 && ! empty($_REQUEST['sort_order'])
513 foreach (array_keys($process) as $key) {
514 $new_key = ucfirst(strtolower($key));
515 $process[$new_key] = $process[$key];
516 unset($process[$key]);
520 $url_params['kill'] = $process['Id'];
521 $kill_process = 'server_status.php' . PMA_generate_common_url($url_params);
523 $retval .= '<tr class="' . ($odd_row ? 'odd' : 'even') . '">';
524 $retval .= '<td><a href="' . $kill_process . '">' . __('Kill') . '</a></td>';
525 $retval .= '<td class="value">' . $process['Id'] . '</td>';
526 $retval .= '<td>' . $process['User'] . '</td>';
527 $retval .= '<td>' . $process['Host'] . '</td>';
528 $retval .= '<td>' . ((! isset($process['db']) || ! strlen($process['db']))
529 ? '<i>' . __('None') . '</i>'
530 : $process['db']) . '</td>';
531 $retval .= '<td>' . $process['Command'] . '</td>';
532 $retval .= '<td class="value">' . $process['Time'] . '</td>';
533 $retval .= '<td>' . (empty($process['State']) ? '---' : $process['State']) . '</td>';
534 $retval .= '<td>';
536 if (empty($process['Info'])) {
537 $retval .= '---';
538 } else {
539 if (! $show_full_sql && strlen($process['Info']) > $GLOBALS['cfg']['MaxCharactersInDisplayedSQL']) {
540 $retval .= htmlspecialchars(substr($process['Info'], 0, $GLOBALS['cfg']['MaxCharactersInDisplayedSQL']))
541 . '[...]';
542 } else {
543 $retval .= '<code class="sql"><pre>'
544 . $process['Info']
545 . '</pre></code>';
548 $retval .= '</td>';
549 $retval .= '</tr>';
550 $odd_row = ! $odd_row;
552 $retval .= '</tbody>';
553 $retval .= '</table>';
555 return $retval;