2 /* vim: set expandtab sw=4 ts=4 sts=4: */
4 * PMA_ServerStatusData class
5 * Used by server_status_*.php pages
10 if (! defined('PHPMYADMIN')) {
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.
25 class PMA_ServerStatusData
31 public $allocationMap;
39 * An empty setter makes the above properties read-only
41 * @param string $a key
42 * @param mixed $b value
46 public function __set($a, $b)
52 * Gets the allocations for constructor
56 private function _getAllocations()
59 // variable name => section
60 // variable names match when they begin with the given string
63 'Innodb_' => 'innodb',
65 'Handler_' => 'handler',
66 'Qcache_' => 'qcache',
67 'Threads_' => 'threads',
68 'Slow_launch_threads' => 'threads',
70 'Binlog_cache_' => 'binlog_cache',
71 'Created_tmp_' => 'created_tmp',
74 'Delayed_' => 'delayed',
75 'Not_flushed_delayed_rows' => 'delayed',
77 'Flush_commands' => 'query',
78 'Last_query_cost' => 'query',
79 'Slow_queries' => 'query',
81 'Prepared_stmt_count' => 'query',
83 'Select_' => 'select',
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',
99 'Open_files' => 'files',
100 'Open_streams' => 'files',
101 'Opened_files' => 'files',
106 * Gets the sections for constructor
110 private function _getSections()
113 // section => section name (description)
115 'query' => __('SQL query'),
116 'innodb' => 'InnoDB',
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'),
132 'other' => __('Other')
137 * Gets the links for constructor
141 private function _getLinks()
144 // variable or section name => (name => url)
146 $links['table'][__('Flush (close) all tables')] = $this->selfUrl
152 $links['table'][__('Show open tables')]
153 = 'sql.php' . PMA_URL_getCommon(
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(
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')]
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&'
195 . PMA_URL_getCommon(array(), 'html', '');
196 $links['innodb'][__('InnoDB Status')]
197 = 'server_engines.php'
200 'engine' => 'InnoDB',
204 $links['innodb']['doc'] = 'innodb';
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_%']
226 - $server_status['Key_blocks_unused']
227 * $server_variables['key_cache_block_size']
228 / $server_variables['key_buffer_size']
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']
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,
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);
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);
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
359 $allocationMap, $sectionUsed, $used_queries
360 ) = $this->_sortVariables(
361 $server_status, $allocations, $allocationMap, $sectionUsed,
366 $used_queries = $GLOBALS['dbi']->fetchResult(
367 'SELECT * FROM data_dictionary.global_statements',
371 unset($used_queries['admin_commands']);
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
405 public static function cleanDeprecated($server_status)
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
425 public function getMenuHtml()
427 $url_params = PMA_URL_getCommon();
430 'name' => __('Server'),
431 'url' => 'server_status.php'
434 'name' => __('Processes'),
435 'url' => 'server_status_processes.php'
438 'name' => __('Query statistics'),
439 'url' => 'server_status_queries.php'
442 'name' => __('All status variables'),
443 'url' => 'server_status_variables.php'
446 'name' => __('Monitor'),
447 'url' => 'server_status_monitor.php'
450 'name' => __('Advisor'),
451 'url' => 'server_status_advisor.php'
455 $retval = '<ul id="topmenu2">';
456 foreach ($items as $item) {
458 if ($item['url'] === $this->selfUrl
) {
459 $class = ' class="tabactive"';
462 $retval .= '<a' . $class;
463 $retval .= ' href="' . $item['url'] . $url_params . '">';
464 $retval .= $item['name'];
469 $retval .= '<div class="clearfloat"></div>';
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
483 public static function getHtmlForRefreshList($name,
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 . '>';
494 _ngettext('%d second', '%d seconds', $rate), $rate
499 _ngettext('%d minute', '%d minutes', $rate), $rate
502 $return .= '</option>';
504 $return .= '</select>';