Update SQL parser to 4.2.3
[phpmyadmin.git] / libraries / ServerStatusData.php
blob52ddf1928393f96c27074f52d3e3332926651f65
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * ServerStatusData class
5 * Used by server_status_*.php pages
7 * @package PhpMyAdmin
8 */
9 namespace PMA\libraries;
11 use PMA\libraries\URL;
13 /**
14 * This class provides data about the server status
16 * All properties of the class are read-only
18 * TODO: Use lazy initialisation for some of the properties
19 * since not all of the server_status_*.php pages need
20 * all the data that this class provides.
22 * @package PhpMyAdmin
24 class ServerStatusData
26 public $status;
27 public $sections;
28 public $variables;
29 public $used_queries;
30 public $allocationMap;
31 public $links;
32 public $db_isLocal;
33 public $section;
34 public $sectionUsed;
35 public $selfUrl;
36 public $dataLoaded;
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 . URL::getCommon(
148 array(
149 'flush' => 'TABLES'
152 $links['table'][__('Show open tables')]
153 = 'sql.php' . 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' . 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 . 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?' . URL::getCommon(array('engine' => 'InnoDB'));
195 $links['innodb'][__('InnoDB Status')]
196 = 'server_engines.php'
197 . URL::getCommon(
198 array(
199 'engine' => 'InnoDB',
200 'page' => 'Status'
203 $links['innodb']['doc'] = 'innodb';
205 return($links);
209 * Calculate some values
211 * @param array $server_status contains results of SHOW GLOBAL STATUS
212 * @param array $server_variables contains results of SHOW GLOBAL VARIABLES
214 * @return array $server_status
216 private function _calculateValues($server_status, $server_variables)
218 // Key_buffer_fraction
219 if (isset($server_status['Key_blocks_unused'])
220 && isset($server_variables['key_cache_block_size'])
221 && isset($server_variables['key_buffer_size'])
222 && $server_variables['key_buffer_size'] != 0
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'])
232 && $server_variables['key_buffer_size'] != 0
234 $server_status['Key_buffer_fraction_%']
235 = $server_status['Key_blocks_used']
236 * 1024
237 / $server_variables['key_buffer_size'];
240 // Ratio for key read/write
241 if (isset($server_status['Key_writes'])
242 && isset($server_status['Key_write_requests'])
243 && $server_status['Key_write_requests'] > 0
245 $key_writes = $server_status['Key_writes'];
246 $key_write_requests = $server_status['Key_write_requests'];
247 $server_status['Key_write_ratio_%']
248 = 100 * $key_writes / $key_write_requests;
251 if (isset($server_status['Key_reads'])
252 && isset($server_status['Key_read_requests'])
253 && $server_status['Key_read_requests'] > 0
255 $key_reads = $server_status['Key_reads'];
256 $key_read_requests = $server_status['Key_read_requests'];
257 $server_status['Key_read_ratio_%']
258 = 100 * $key_reads / $key_read_requests;
261 // Threads_cache_hitrate
262 if (isset($server_status['Threads_created'])
263 && isset($server_status['Connections'])
264 && $server_status['Connections'] > 0
267 $server_status['Threads_cache_hitrate_%']
268 = 100 - $server_status['Threads_created']
269 / $server_status['Connections'] * 100;
271 return $server_status;
275 * Sort variables into arrays
277 * @param array $server_status contains results of SHOW GLOBAL STATUS
278 * @param array $allocations allocations for sections
279 * @param array $allocationMap map variables to their section
280 * @param array $sectionUsed is a section used?
281 * @param array $used_queries used queries
283 * @return array ($allocationMap, $sectionUsed, $used_queries)
285 private function _sortVariables(
286 $server_status, $allocations, $allocationMap, $sectionUsed,
287 $used_queries
289 foreach ($server_status as $name => $value) {
290 $section_found = false;
291 foreach ($allocations as $filter => $section) {
292 if (mb_strpos($name, $filter) !== false) {
293 $allocationMap[$name] = $section;
294 $sectionUsed[$section] = true;
295 $section_found = true;
296 if ($section == 'com' && $value > 0) {
297 $used_queries[$name] = $value;
299 break; // Only exits inner loop
302 if (! $section_found) {
303 $allocationMap[$name] = 'other';
304 $sectionUsed['other'] = true;
307 return array($allocationMap, $sectionUsed, $used_queries);
311 * Constructor
313 public function __construct()
315 $this->selfUrl = basename($GLOBALS['PMA_PHP_SELF']);
317 // get status from server
318 $server_status_result = $GLOBALS['dbi']->tryQuery('SHOW GLOBAL STATUS');
319 $server_status = array();
320 if ($server_status_result === false) {
321 $this->dataLoaded = false;
322 } else {
323 $this->dataLoaded = true;
324 while ($arr = $GLOBALS['dbi']->fetchRow($server_status_result)) {
325 $server_status[$arr[0]] = $arr[1];
327 $GLOBALS['dbi']->freeResult($server_status_result);
330 // for some calculations we require also some server settings
331 $server_variables = $GLOBALS['dbi']->fetchResult(
332 'SHOW GLOBAL VARIABLES', 0, 1
335 // cleanup of some deprecated values
336 $server_status = self::cleanDeprecated($server_status);
338 // calculate some values
339 $server_status = $this->_calculateValues(
340 $server_status, $server_variables
343 // split variables in sections
344 $allocations = $this->_getAllocations();
346 $sections = $this->_getSections();
348 // define some needful links/commands
349 $links = $this->_getLinks();
351 // Variable to contain all com_ variables (query statistics)
352 $used_queries = array();
354 // Variable to map variable names to their respective section name
355 // (used for js category filtering)
356 $allocationMap = array();
358 // Variable to mark used sections
359 $sectionUsed = array();
361 // sort vars into arrays
362 list(
363 $allocationMap, $sectionUsed, $used_queries
364 ) = $this->_sortVariables(
365 $server_status, $allocations, $allocationMap, $sectionUsed,
366 $used_queries
369 // admin commands are not queries (e.g. they include COM_PING,
370 // which is excluded from $server_status['Questions'])
371 unset($used_queries['Com_admin_commands']);
373 // Set all class properties
374 $this->db_isLocal = false;
375 $serverHostToLower = mb_strtolower(
376 $GLOBALS['cfg']['Server']['host']
378 if ($serverHostToLower === 'localhost'
379 || $GLOBALS['cfg']['Server']['host'] === '127.0.0.1'
380 || $GLOBALS['cfg']['Server']['host'] === '::1'
382 $this->db_isLocal = true;
384 $this->status = $server_status;
385 $this->sections = $sections;
386 $this->variables = $server_variables;
387 $this->used_queries = $used_queries;
388 $this->allocationMap = $allocationMap;
389 $this->links = $links;
390 $this->sectionUsed = $sectionUsed;
394 * cleanup of some deprecated values
396 * @param array $server_status status array to process
398 * @return array
400 public static function cleanDeprecated($server_status)
402 $deprecated = array(
403 'Com_prepare_sql' => 'Com_stmt_prepare',
404 'Com_execute_sql' => 'Com_stmt_execute',
405 'Com_dealloc_sql' => 'Com_stmt_close',
407 foreach ($deprecated as $old => $new) {
408 if (isset($server_status[$old]) && isset($server_status[$new])) {
409 unset($server_status[$old]);
412 return $server_status;
416 * Generates menu HTML
418 * @return string
420 public function getMenuHtml()
422 $url_params = URL::getCommon();
423 $items = array(
424 array(
425 'name' => __('Server'),
426 'url' => 'server_status.php'
428 array(
429 'name' => __('Processes'),
430 'url' => 'server_status_processes.php'
432 array(
433 'name' => __('Query statistics'),
434 'url' => 'server_status_queries.php'
436 array(
437 'name' => __('All status variables'),
438 'url' => 'server_status_variables.php'
440 array(
441 'name' => __('Monitor'),
442 'url' => 'server_status_monitor.php'
444 array(
445 'name' => __('Advisor'),
446 'url' => 'server_status_advisor.php'
450 $retval = '<ul id="topmenu2">';
451 foreach ($items as $item) {
452 $class = '';
453 if ($item['url'] === $this->selfUrl) {
454 $class = ' class="tabactive"';
456 $retval .= '<li>';
457 $retval .= '<a' . $class;
458 $retval .= ' href="' . $item['url'] . $url_params . '">';
459 $retval .= $item['name'];
460 $retval .= '</a>';
461 $retval .= '</li>';
463 $retval .= '</ul>';
464 $retval .= '<div class="clearfloat"></div>';
466 return $retval;
470 * Builds a <select> list for refresh rates
472 * @param string $name Name of select
473 * @param int $defaultRate Currently chosen rate
474 * @param array $refreshRates List of refresh rates
476 * @return string
478 public static function getHtmlForRefreshList($name,
479 $defaultRate = 5,
480 $refreshRates = Array(1, 2, 5, 10, 20, 40, 60, 120, 300, 600)
482 $return = '<select name="' . $name . '" id="id_' . $name
483 . '" class="refreshRate">';
484 foreach ($refreshRates as $rate) {
485 $selected = ($rate == $defaultRate)?' selected="selected"':'';
486 $return .= '<option value="' . $rate . '"' . $selected . '>';
487 if ($rate < 60) {
488 $return .= sprintf(
489 _ngettext('%d second', '%d seconds', $rate), $rate
491 } else {
492 $rate = $rate / 60;
493 $return .= sprintf(
494 _ngettext('%d minute', '%d minutes', $rate), $rate
497 $return .= '</option>';
499 $return .= '</select>';
500 return $return;