2 /* vim: set expandtab sw=4 ts=4 sts=4: */
4 * PMA_ServerStatusData class
5 * Used by server_status_*.php pages
10 require_once 'libraries/common.inc.php';
13 * This class provides data about the server status
15 * All properties of the class are read-only
17 * TODO: Use lazy initialisation for some of the properties
18 * since not all of the server_status_*.php pages need
19 * all the data that this class provides.
23 class PMA_ServerStatusData
29 public $allocationMap;
37 * An empty setter makes the above properties read-only
39 * @param string $a key
40 * @param mixed $b value
44 public function __set($a, $b)
54 public function __construct()
56 $this->selfUrl
= basename($GLOBALS['PMA_PHP_SELF']);
58 * get status from server
60 $server_status = PMA_DBI_fetch_result('SHOW GLOBAL STATUS', 0, 1);
62 // Drizzle doesn't put query statistics into variables, add it
63 $sql = "SELECT concat('Com_', variable_name), variable_value
64 FROM data_dictionary.GLOBAL_STATEMENTS";
65 $statements = PMA_DBI_fetch_result($sql, 0, 1);
66 $server_status = array_merge($server_status, $statements);
70 * for some calculations we require also some server settings
72 $server_variables = PMA_DBI_fetch_result('SHOW GLOBAL VARIABLES', 0, 1);
75 * cleanup of some deprecated values
77 $server_status = self
::cleanDeprecated($server_status);
80 * calculate some values
82 // Key_buffer_fraction
83 if (isset($server_status['Key_blocks_unused'])
84 && isset($server_variables['key_cache_block_size'])
85 && isset($server_variables['key_buffer_size'])
87 $server_status['Key_buffer_fraction_%']
89 - $server_status['Key_blocks_unused']
90 * $server_variables['key_cache_block_size']
91 / $server_variables['key_buffer_size']
93 } elseif (isset($server_status['Key_blocks_used'])
94 && isset($server_variables['key_buffer_size'])) {
95 $server_status['Key_buffer_fraction_%']
96 = $server_status['Key_blocks_used']
98 / $server_variables['key_buffer_size'];
101 // Ratio for key read/write
102 if (isset($server_status['Key_writes'])
103 && isset($server_status['Key_write_requests'])
104 && $server_status['Key_write_requests'] > 0
106 $server_status['Key_write_ratio_%']
107 = 100 * $server_status['Key_writes'] / $server_status['Key_write_requests'];
110 if (isset($server_status['Key_reads'])
111 && isset($server_status['Key_read_requests'])
112 && $server_status['Key_read_requests'] > 0
114 $server_status['Key_read_ratio_%']
115 = 100 * $server_status['Key_reads'] / $server_status['Key_read_requests'];
118 // Threads_cache_hitrate
119 if (isset($server_status['Threads_created'])
120 && isset($server_status['Connections'])
121 && $server_status['Connections'] > 0
124 $server_status['Threads_cache_hitrate_%']
125 = 100 - $server_status['Threads_created']
126 / $server_status['Connections'] * 100;
130 * split variables in sections
132 $allocations = array(
133 // variable name => section
134 // variable names match when they begin with the given string
137 'Innodb_' => 'innodb',
139 'Handler_' => 'handler',
140 'Qcache_' => 'qcache',
141 'Threads_' => 'threads',
142 'Slow_launch_threads' => 'threads',
144 'Binlog_cache_' => 'binlog_cache',
145 'Created_tmp_' => 'created_tmp',
148 'Delayed_' => 'delayed',
149 'Not_flushed_delayed_rows' => 'delayed',
151 'Flush_commands' => 'query',
152 'Last_query_cost' => 'query',
153 'Slow_queries' => 'query',
154 'Queries' => 'query',
155 'Prepared_stmt_count' => 'query',
157 'Select_' => 'select',
160 'Open_tables' => 'table',
161 'Opened_tables' => 'table',
162 'Open_table_definitions' => 'table',
163 'Opened_table_definitions' => 'table',
164 'Table_locks_' => 'table',
166 'Rpl_status' => 'repl',
173 'Open_files' => 'files',
174 'Open_streams' => 'files',
175 'Opened_files' => 'files',
179 // section => section name (description)
181 'query' => __('SQL query'),
182 'innodb' => 'InnoDB',
184 'handler' => __('Handler'),
185 'qcache' => __('Query cache'),
186 'threads' => __('Threads'),
187 'binlog_cache' => __('Binary log'),
188 'created_tmp' => __('Temporary data'),
189 'delayed' => __('Delayed inserts'),
190 'key' => __('Key cache'),
191 'select' => __('Joins'),
192 'repl' => __('Replication'),
193 'sort' => __('Sorting'),
194 'table' => __('Tables'),
195 'tc' => __('Transaction coordinator'),
196 'files' => __('Files'),
198 'other' => __('Other')
202 * define some needfull links/commands
204 // variable or section name => (name => url)
207 $links['table'][__('Flush (close) all tables')]
208 = $this->selfUrl
. '?flush=TABLES&' . PMA_generate_common_url();
209 $links['table'][__('Show open tables')]
210 = 'sql.php?sql_query=' . urlencode('SHOW OPEN TABLES') .
211 '&goto=' . $this->selfUrl
. '&' . PMA_generate_common_url();
213 if ($GLOBALS['server_master_status']) {
214 $links['repl'][__('Show slave hosts')]
215 = 'sql.php?sql_query=' . urlencode('SHOW SLAVE HOSTS')
216 . '&goto=' . $this->selfUrl
. '&'
217 . PMA_generate_common_url();
218 $links['repl'][__('Show master status')] = '#replication_master';
220 if ($GLOBALS['server_slave_status']) {
221 $links['repl'][__('Show slave status')] = '#replication_slave';
224 $links['repl']['doc'] = 'replication';
226 $links['qcache'][__('Flush query cache')]
227 = $this->selfUrl
. '?flush=' . urlencode('QUERY CACHE') . '&' .
228 PMA_generate_common_url();
229 $links['qcache']['doc'] = 'query_cache';
231 $links['threads']['doc'] = 'mysql_threads';
233 $links['key']['doc'] = 'myisam_key_cache';
235 $links['binlog_cache']['doc'] = 'binary_log';
237 $links['Slow_queries']['doc'] = 'slow_query_log';
239 $links['innodb'][__('Variables')]
240 = 'server_engines.php?engine=InnoDB&' . PMA_generate_common_url();
241 $links['innodb'][__('InnoDB Status')]
242 = 'server_engines.php?engine=InnoDB&page=Status&' .
243 PMA_generate_common_url();
244 $links['innodb']['doc'] = 'innodb';
247 // Variable to contain all com_ variables (query statistics)
248 $used_queries = array();
250 // Variable to map variable names to their respective section name
251 // (used for js category filtering)
252 $allocationMap = array();
254 // Variable to mark used sections
255 $categoryUsed = array();
257 // sort vars into arrays
258 foreach ($server_status as $name => $value) {
259 $section_found = false;
260 foreach ($allocations as $filter => $section) {
261 if (strpos($name, $filter) !== false) {
262 $allocationMap[$name] = $section;
263 $categoryUsed[$section] = true;
264 $section_found = true;
265 if ($section == 'com' && $value > 0) {
266 $used_queries[$name] = $value;
268 break; // Only exits inner loop
271 if (!$section_found) {
272 $allocationMap[$name] = 'other';
273 $categoryUsed['other'] = true;
278 $used_queries = PMA_DBI_fetch_result(
279 'SELECT * FROM data_dictionary.global_statements',
283 unset($used_queries['admin_commands']);
285 // admin commands are not queries (e.g. they include COM_PING,
286 // which is excluded from $server_status['Questions'])
287 unset($used_queries['Com_admin_commands']);
290 // Set all class properties
291 $this->db_isLocal
= false;
292 if (strtolower($GLOBALS['cfg']['Server']['host']) === 'localhost'
293 ||
$GLOBALS['cfg']['Server']['host'] === '127.0.0.1'
294 ||
$GLOBALS['cfg']['Server']['host'] === '::1'
296 $this->db_isLocal
= true;
298 $this->status
= $server_status;
299 $this->sections
= $sections;
300 $this->variables
= $server_variables;
301 $this->used_queries
= $used_queries;
302 $this->allocationMap
= $allocationMap;
303 $this->links
= $links;
304 $this->categoryUsed
= $categoryUsed;
308 * cleanup of some deprecated values
310 * @param array $server_status status array to process
314 public static function cleanDeprecated($server_status)
317 'Com_prepare_sql' => 'Com_stmt_prepare',
318 'Com_execute_sql' => 'Com_stmt_execute',
319 'Com_dealloc_sql' => 'Com_stmt_close',
321 foreach ($deprecated as $old => $new) {
322 if (isset($server_status[$old]) && isset($server_status[$new])) {
323 unset($server_status[$old]);
326 return $server_status;
330 * cleanup of some deprecated values
334 public function getMenuHtml()
336 $url_params = PMA_generate_common_url();
339 'name' => __('Server'),
340 'url' => 'server_status.php'
343 'name' => __('Query statistics'),
344 'url' => 'server_status_queries.php'
347 'name' => __('All status variables'),
348 'url' => 'server_status_variables.php'
351 'name' => __('Monitor'),
352 'url' => 'server_status_monitor.php'
355 'name' => __('Advisor'),
356 'url' => 'server_status_advisor.php'
360 $retval = '<ul id="topmenu2">';
361 foreach ($items as $item) {
363 if ($item['url'] === $this->selfUrl
) {
364 $class = ' class="tabactive"';
367 $retval .= '<a' . $class;
368 $retval .= ' href="' . $item['url'] . '?' . $url_params . '">';
369 $retval .= $item['name'];
374 $retval .= '<div class="clearfloat"></div>';