Merge remote-tracking branch 'origin/master'
[phpmyadmin.git] / libraries / ServerStatusData.class.php
blobce08fa28782ea0d0db8c9e9c884b91e326fd9148
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * PMA_ServerStatusData class
5 * Used by server_status_*.php pages
7 * @package PhpMyAdmin
8 */
10 if (! defined('PHPMYADMIN')) {
11 exit;
14 /**
15 * This class provides data about the server status
17 * All properties of the class are read-only
19 * TODO: Use lazy initialisation for some of the properties
20 * since not all of the server_status_*.php pages need
21 * all the data that this class provides.
23 * @package PhpMyAdmin
25 class PMA_ServerStatusData
27 public $status;
28 public $sections;
29 public $variables;
30 public $used_queries;
31 public $allocationMap;
32 public $links;
33 public $db_isLocal;
34 public $section;
35 public $sectionUsed;
36 public $selfUrl;
38 /**
39 * An empty setter makes the above properties read-only
41 * @param string $a key
42 * @param mixed $b value
44 * @return void
46 public function __set($a, $b)
48 // Discard everything
51 /**
52 * Gets the allocations for constructor
54 * @return array
56 private function _getAllocations()
58 return array(
59 // variable name => section
60 // variable names match when they begin with the given string
62 'Com_' => 'com',
63 'Innodb_' => 'innodb',
64 'Ndb_' => 'ndb',
65 'Handler_' => 'handler',
66 'Qcache_' => 'qcache',
67 'Threads_' => 'threads',
68 'Slow_launch_threads' => 'threads',
70 'Binlog_cache_' => 'binlog_cache',
71 'Created_tmp_' => 'created_tmp',
72 'Key_' => 'key',
74 'Delayed_' => 'delayed',
75 'Not_flushed_delayed_rows' => 'delayed',
77 'Flush_commands' => 'query',
78 'Last_query_cost' => 'query',
79 'Slow_queries' => 'query',
80 'Queries' => 'query',
81 'Prepared_stmt_count' => 'query',
83 'Select_' => 'select',
84 'Sort_' => 'sort',
86 'Open_tables' => 'table',
87 'Opened_tables' => 'table',
88 'Open_table_definitions' => 'table',
89 'Opened_table_definitions' => 'table',
90 'Table_locks_' => 'table',
92 'Rpl_status' => 'repl',
93 'Slave_' => 'repl',
95 'Tc_' => 'tc',
97 'Ssl_' => 'ssl',
99 'Open_files' => 'files',
100 'Open_streams' => 'files',
101 'Opened_files' => 'files',
106 * Gets the sections for constructor
108 * @return array
110 private function _getSections()
112 return array(
113 // section => section name (description)
114 'com' => 'Com',
115 'query' => __('SQL query'),
116 'innodb' => 'InnoDB',
117 'ndb' => 'NDB',
118 'handler' => __('Handler'),
119 'qcache' => __('Query cache'),
120 'threads' => __('Threads'),
121 'binlog_cache' => __('Binary log'),
122 'created_tmp' => __('Temporary data'),
123 'delayed' => __('Delayed inserts'),
124 'key' => __('Key cache'),
125 'select' => __('Joins'),
126 'repl' => __('Replication'),
127 'sort' => __('Sorting'),
128 'table' => __('Tables'),
129 'tc' => __('Transaction coordinator'),
130 'files' => __('Files'),
131 'ssl' => 'SSL',
132 'other' => __('Other')
137 * Gets the links for constructor
139 * @return array
141 private function _getLinks()
143 $links = array();
144 // variable or section name => (name => url)
146 $links['table'][__('Flush (close) all tables')] = $this->selfUrl
147 . PMA_URL_getCommon(
148 array(
149 'flush' => 'TABLES'
152 $links['table'][__('Show open tables')]
153 = 'sql.php' . PMA_URL_getCommon(
154 array(
155 'sql_query' => 'SHOW OPEN TABLES',
156 'goto' => $this->selfUrl,
160 if ($GLOBALS['replication_info']['master']['status']) {
161 $links['repl'][__('Show slave hosts')]
162 = 'sql.php' . PMA_URL_getCommon(
163 array(
164 'sql_query' => 'SHOW SLAVE HOSTS',
165 'goto' => $this->selfUrl,
168 $links['repl'][__('Show master status')] = '#replication_master';
170 if ($GLOBALS['replication_info']['slave']['status']) {
171 $links['repl'][__('Show slave status')] = '#replication_slave';
174 $links['repl']['doc'] = 'replication';
176 $links['qcache'][__('Flush query cache')]
177 = $this->selfUrl
178 . PMA_URL_getCommon(
179 array(
180 'flush' => 'QUERY CACHE'
183 $links['qcache']['doc'] = 'query_cache';
185 $links['threads']['doc'] = 'mysql_threads';
187 $links['key']['doc'] = 'myisam_key_cache';
189 $links['binlog_cache']['doc'] = 'binary_log';
191 $links['Slow_queries']['doc'] = 'slow_query_log';
193 $links['innodb'][__('Variables')]
194 = 'server_engines.php?engine=InnoDB&amp;'
195 . PMA_URL_getCommon(array(), 'html', '');
196 $links['innodb'][__('InnoDB Status')]
197 = 'server_engines.php'
198 . PMA_URL_getCommon(
199 array(
200 'engine' => 'InnoDB',
201 'page' => 'Status'
204 $links['innodb']['doc'] = 'innodb';
206 return($links);
210 * Calculate some values
212 * @param array $server_status contains results of SHOW GLOBAL STATUS
213 * @param array $server_variables contains results of SHOW GLOBAL VARIABLES
215 * @return array $server_status
217 private function _calculateValues($server_status, $server_variables)
219 // Key_buffer_fraction
220 if (isset($server_status['Key_blocks_unused'])
221 && isset($server_variables['key_cache_block_size'])
222 && isset($server_variables['key_buffer_size'])
224 $server_status['Key_buffer_fraction_%']
225 = 100
226 - $server_status['Key_blocks_unused']
227 * $server_variables['key_cache_block_size']
228 / $server_variables['key_buffer_size']
229 * 100;
230 } elseif (isset($server_status['Key_blocks_used'])
231 && isset($server_variables['key_buffer_size'])
233 $server_status['Key_buffer_fraction_%']
234 = $server_status['Key_blocks_used']
235 * 1024
236 / $server_variables['key_buffer_size'];
239 // Ratio for key read/write
240 if (isset($server_status['Key_writes'])
241 && isset($server_status['Key_write_requests'])
242 && $server_status['Key_write_requests'] > 0
244 $key_writes = $server_status['Key_writes'];
245 $key_write_requests = $server_status['Key_write_requests'];
246 $server_status['Key_write_ratio_%']
247 = 100 * $key_writes / $key_write_requests;
250 if (isset($server_status['Key_reads'])
251 && isset($server_status['Key_read_requests'])
252 && $server_status['Key_read_requests'] > 0
254 $key_reads = $server_status['Key_reads'];
255 $key_read_requests = $server_status['Key_read_requests'];
256 $server_status['Key_read_ratio_%']
257 = 100 * $key_reads / $key_read_requests;
260 // Threads_cache_hitrate
261 if (isset($server_status['Threads_created'])
262 && isset($server_status['Connections'])
263 && $server_status['Connections'] > 0
266 $server_status['Threads_cache_hitrate_%']
267 = 100 - $server_status['Threads_created']
268 / $server_status['Connections'] * 100;
270 return $server_status;
274 * Sort variables into arrays
276 * @param array $server_status contains results of SHOW GLOBAL STATUS
277 * @param array $allocations allocations for sections
278 * @param array $allocationMap map variables to their section
279 * @param array $sectionUsed is a section used?
280 * @param array $used_queries used queries
282 * @return array ($allocationMap, $sectionUsed, $used_queries)
284 private function _sortVariables(
285 $server_status, $allocations, $allocationMap, $sectionUsed,
286 $used_queries
288 foreach ($server_status as $name => $value) {
289 $section_found = false;
290 foreach ($allocations as $filter => $section) {
291 if (/*overload*/mb_strpos($name, $filter) !== false) {
292 $allocationMap[$name] = $section;
293 $sectionUsed[$section] = true;
294 $section_found = true;
295 if ($section == 'com' && $value > 0) {
296 $used_queries[$name] = $value;
298 break; // Only exits inner loop
301 if (! $section_found) {
302 $allocationMap[$name] = 'other';
303 $sectionUsed['other'] = true;
306 return array($allocationMap, $sectionUsed, $used_queries);
310 * Constructor
312 public function __construct()
314 $this->selfUrl = basename($GLOBALS['PMA_PHP_SELF']);
316 // get status from server
317 $server_status = $GLOBALS['dbi']->fetchResult('SHOW GLOBAL STATUS', 0, 1);
318 if (PMA_DRIZZLE) {
319 // Drizzle doesn't put query statistics into variables, add it
320 $sql = "SELECT concat('Com_', variable_name), variable_value "
321 . "FROM data_dictionary.GLOBAL_STATEMENTS";
322 $statements = $GLOBALS['dbi']->fetchResult($sql, 0, 1);
323 $server_status = array_merge($server_status, $statements);
326 // for some calculations we require also some server settings
327 $server_variables = $GLOBALS['dbi']->fetchResult(
328 'SHOW GLOBAL VARIABLES', 0, 1
331 // cleanup of some deprecated values
332 $server_status = self::cleanDeprecated($server_status);
334 // calculate some values
335 $server_status = $this->_calculateValues(
336 $server_status, $server_variables
339 // split variables in sections
340 $allocations = $this->_getAllocations();
342 $sections = $this->_getSections();
344 // define some needful links/commands
345 $links = $this->_getLinks();
347 // Variable to contain all com_ variables (query statistics)
348 $used_queries = array();
350 // Variable to map variable names to their respective section name
351 // (used for js category filtering)
352 $allocationMap = array();
354 // Variable to mark used sections
355 $sectionUsed = array();
357 // sort vars into arrays
358 list(
359 $allocationMap, $sectionUsed, $used_queries
360 ) = $this->_sortVariables(
361 $server_status, $allocations, $allocationMap, $sectionUsed,
362 $used_queries
365 if (PMA_DRIZZLE) {
366 $used_queries = $GLOBALS['dbi']->fetchResult(
367 'SELECT * FROM data_dictionary.global_statements',
371 unset($used_queries['admin_commands']);
372 } else {
373 // admin commands are not queries (e.g. they include COM_PING,
374 // which is excluded from $server_status['Questions'])
375 unset($used_queries['Com_admin_commands']);
378 // Set all class properties
379 $this->db_isLocal = false;
380 $serverHostToLower = /*overload*/mb_strtolower(
381 $GLOBALS['cfg']['Server']['host']
383 if ($serverHostToLower === 'localhost'
384 || $GLOBALS['cfg']['Server']['host'] === '127.0.0.1'
385 || $GLOBALS['cfg']['Server']['host'] === '::1'
387 $this->db_isLocal = true;
389 $this->status = $server_status;
390 $this->sections = $sections;
391 $this->variables = $server_variables;
392 $this->used_queries = $used_queries;
393 $this->allocationMap = $allocationMap;
394 $this->links = $links;
395 $this->sectionUsed = $sectionUsed;
399 * cleanup of some deprecated values
401 * @param array $server_status status array to process
403 * @return array
405 public static function cleanDeprecated($server_status)
407 $deprecated = array(
408 'Com_prepare_sql' => 'Com_stmt_prepare',
409 'Com_execute_sql' => 'Com_stmt_execute',
410 'Com_dealloc_sql' => 'Com_stmt_close',
412 foreach ($deprecated as $old => $new) {
413 if (isset($server_status[$old]) && isset($server_status[$new])) {
414 unset($server_status[$old]);
417 return $server_status;
421 * Generates menu HTML
423 * @return string
425 public function getMenuHtml()
427 $url_params = PMA_URL_getCommon();
428 $items = array(
429 array(
430 'name' => __('Server'),
431 'url' => 'server_status.php'
433 array(
434 'name' => __('Processes'),
435 'url' => 'server_status_processes.php'
437 array(
438 'name' => __('Query statistics'),
439 'url' => 'server_status_queries.php'
441 array(
442 'name' => __('All status variables'),
443 'url' => 'server_status_variables.php'
445 array(
446 'name' => __('Monitor'),
447 'url' => 'server_status_monitor.php'
449 array(
450 'name' => __('Advisor'),
451 'url' => 'server_status_advisor.php'
455 $retval = '<ul id="topmenu2">';
456 foreach ($items as $item) {
457 $class = '';
458 if ($item['url'] === $this->selfUrl) {
459 $class = ' class="tabactive"';
461 $retval .= '<li>';
462 $retval .= '<a' . $class;
463 $retval .= ' href="' . $item['url'] . $url_params . '">';
464 $retval .= $item['name'];
465 $retval .= '</a>';
466 $retval .= '</li>';
468 $retval .= '</ul>';
469 $retval .= '<div class="clearfloat"></div>';
471 return $retval;
475 * Builds a <select> list for refresh rates
477 * @param string $name Name of select
478 * @param int $defaultRate Currently chosen rate
479 * @param array $refreshRates List of refresh rates
481 * @return string
483 public static function getHtmlForRefreshList($name,
484 $defaultRate = 5,
485 $refreshRates = Array(1, 2, 5, 10, 20, 40, 60, 120, 300, 600)
487 $return = '<select name="' . $name . '" id="id_' . $name
488 . '" class="refreshRate">';
489 foreach ($refreshRates as $rate) {
490 $selected = ($rate == $defaultRate)?' selected="selected"':'';
491 $return .= '<option value="' . $rate . '"' . $selected . '>';
492 if ($rate < 60) {
493 $return .= sprintf(
494 _ngettext('%d second', '%d seconds', $rate), $rate
496 } else {
497 $rate = $rate / 60;
498 $return .= sprintf(
499 _ngettext('%d minute', '%d minutes', $rate), $rate
502 $return .= '</option>';
504 $return .= '</select>';
505 return $return;