Translated using Weblate (Finnish)
[phpmyadmin.git] / libraries / ServerStatusData.class.php
blobd0def03985fee9bc316b3caef02baa1a4b0570ff
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 $categoryUsed;
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 * Constructor
54 public function __construct()
56 $this->selfUrl = basename($GLOBALS['PMA_PHP_SELF']);
57 /**
58 * get status from server
60 $server_status = $GLOBALS['dbi']->fetchResult('SHOW GLOBAL STATUS', 0, 1);
61 if (PMA_DRIZZLE) {
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 = $GLOBALS['dbi']->fetchResult($sql, 0, 1);
66 $server_status = array_merge($server_status, $statements);
69 /**
70 * for some calculations we require also some server settings
72 $server_variables = $GLOBALS['dbi']->fetchResult(
73 'SHOW GLOBAL VARIABLES', 0, 1
76 /**
77 * cleanup of some deprecated values
79 $server_status = self::cleanDeprecated($server_status);
81 /**
82 * calculate some values
84 // Key_buffer_fraction
85 if (isset($server_status['Key_blocks_unused'])
86 && isset($server_variables['key_cache_block_size'])
87 && isset($server_variables['key_buffer_size'])
88 ) {
89 $server_status['Key_buffer_fraction_%']
90 = 100
91 - $server_status['Key_blocks_unused']
92 * $server_variables['key_cache_block_size']
93 / $server_variables['key_buffer_size']
94 * 100;
95 } elseif (isset($server_status['Key_blocks_used'])
96 && isset($server_variables['key_buffer_size'])
97 ) {
98 $server_status['Key_buffer_fraction_%']
99 = $server_status['Key_blocks_used']
100 * 1024
101 / $server_variables['key_buffer_size'];
104 // Ratio for key read/write
105 if (isset($server_status['Key_writes'])
106 && isset($server_status['Key_write_requests'])
107 && $server_status['Key_write_requests'] > 0
109 $key_writes = $server_status['Key_writes'];
110 $key_write_requests = $server_status['Key_write_requests'];
111 $server_status['Key_write_ratio_%']
112 = 100 * $key_writes / $key_write_requests;
115 if (isset($server_status['Key_reads'])
116 && isset($server_status['Key_read_requests'])
117 && $server_status['Key_read_requests'] > 0
119 $key_reads = $server_status['Key_reads'];
120 $key_read_requests = $server_status['Key_read_requests'];
121 $server_status['Key_read_ratio_%']
122 = 100 * $key_reads / $key_read_requests;
125 // Threads_cache_hitrate
126 if (isset($server_status['Threads_created'])
127 && isset($server_status['Connections'])
128 && $server_status['Connections'] > 0
131 $server_status['Threads_cache_hitrate_%']
132 = 100 - $server_status['Threads_created']
133 / $server_status['Connections'] * 100;
137 * split variables in sections
139 $allocations = array(
140 // variable name => section
141 // variable names match when they begin with the given string
143 'Com_' => 'com',
144 'Innodb_' => 'innodb',
145 'Ndb_' => 'ndb',
146 'Handler_' => 'handler',
147 'Qcache_' => 'qcache',
148 'Threads_' => 'threads',
149 'Slow_launch_threads' => 'threads',
151 'Binlog_cache_' => 'binlog_cache',
152 'Created_tmp_' => 'created_tmp',
153 'Key_' => 'key',
155 'Delayed_' => 'delayed',
156 'Not_flushed_delayed_rows' => 'delayed',
158 'Flush_commands' => 'query',
159 'Last_query_cost' => 'query',
160 'Slow_queries' => 'query',
161 'Queries' => 'query',
162 'Prepared_stmt_count' => 'query',
164 'Select_' => 'select',
165 'Sort_' => 'sort',
167 'Open_tables' => 'table',
168 'Opened_tables' => 'table',
169 'Open_table_definitions' => 'table',
170 'Opened_table_definitions' => 'table',
171 'Table_locks_' => 'table',
173 'Rpl_status' => 'repl',
174 'Slave_' => 'repl',
176 'Tc_' => 'tc',
178 'Ssl_' => 'ssl',
180 'Open_files' => 'files',
181 'Open_streams' => 'files',
182 'Opened_files' => 'files',
185 $sections = array(
186 // section => section name (description)
187 'com' => 'Com',
188 'query' => __('SQL query'),
189 'innodb' => 'InnoDB',
190 'ndb' => 'NDB',
191 'handler' => __('Handler'),
192 'qcache' => __('Query cache'),
193 'threads' => __('Threads'),
194 'binlog_cache' => __('Binary log'),
195 'created_tmp' => __('Temporary data'),
196 'delayed' => __('Delayed inserts'),
197 'key' => __('Key cache'),
198 'select' => __('Joins'),
199 'repl' => __('Replication'),
200 'sort' => __('Sorting'),
201 'table' => __('Tables'),
202 'tc' => __('Transaction coordinator'),
203 'files' => __('Files'),
204 'ssl' => 'SSL',
205 'other' => __('Other')
209 * define some needful links/commands
211 // variable or section name => (name => url)
212 $links = array();
214 $links['table'][__('Flush (close) all tables')] = $this->selfUrl
215 . PMA_URL_getCommon(
216 array(
217 'flush' => 'TABLES'
220 $links['table'][__('Show open tables')]
221 = 'sql.php' . PMA_URL_getCommon(
222 array(
223 'sql_query' => 'SHOW OPEN TABLES',
224 'goto' => $this->selfUrl,
228 if ($GLOBALS['replication_info']['master']['status']) {
229 $links['repl'][__('Show slave hosts')]
230 = 'sql.php' . PMA_URL_getCommon(
231 array(
232 'sql_query' => 'SHOW SLAVE HOSTS',
233 'goto' => $this->selfUrl,
236 $links['repl'][__('Show master status')] = '#replication_master';
238 if ($GLOBALS['replication_info']['slave']['status']) {
239 $links['repl'][__('Show slave status')] = '#replication_slave';
242 $links['repl']['doc'] = 'replication';
244 $links['qcache'][__('Flush query cache')]
245 = $this->selfUrl
246 . PMA_URL_getCommon(
247 array(
248 'flush' => 'QUERY CACHE'
251 $links['qcache']['doc'] = 'query_cache';
253 $links['threads']['doc'] = 'mysql_threads';
255 $links['key']['doc'] = 'myisam_key_cache';
257 $links['binlog_cache']['doc'] = 'binary_log';
259 $links['Slow_queries']['doc'] = 'slow_query_log';
261 $links['innodb'][__('Variables')]
262 = 'server_engines.php?engine=InnoDB&amp;'
263 . PMA_URL_getCommon(array(), 'html', '');
264 $links['innodb'][__('InnoDB Status')]
265 = 'server_engines.php'
266 . PMA_URL_getCommon(
267 array(
268 'engine' => 'InnoDB',
269 'page' => 'Status'
272 $links['innodb']['doc'] = 'innodb';
274 // Variable to contain all com_ variables (query statistics)
275 $used_queries = array();
277 // Variable to map variable names to their respective section name
278 // (used for js category filtering)
279 $allocationMap = array();
281 // Variable to mark used sections
282 $categoryUsed = array();
284 // sort vars into arrays
285 foreach ($server_status as $name => $value) {
286 $section_found = false;
287 foreach ($allocations as $filter => $section) {
288 if (/*overload*/mb_strpos($name, $filter) !== false) {
289 $allocationMap[$name] = $section;
290 $categoryUsed[$section] = true;
291 $section_found = true;
292 if ($section == 'com' && $value > 0) {
293 $used_queries[$name] = $value;
295 break; // Only exits inner loop
298 if (!$section_found) {
299 $allocationMap[$name] = 'other';
300 $categoryUsed['other'] = true;
304 if (PMA_DRIZZLE) {
305 $used_queries = $GLOBALS['dbi']->fetchResult(
306 'SELECT * FROM data_dictionary.global_statements',
310 unset($used_queries['admin_commands']);
311 } else {
312 // admin commands are not queries (e.g. they include COM_PING,
313 // which is excluded from $server_status['Questions'])
314 unset($used_queries['Com_admin_commands']);
317 // Set all class properties
318 $this->db_isLocal = false;
319 $serverHostToLower = /*overload*/mb_strtolower(
320 $GLOBALS['cfg']['Server']['host']
322 if ($serverHostToLower === 'localhost'
323 || $GLOBALS['cfg']['Server']['host'] === '127.0.0.1'
324 || $GLOBALS['cfg']['Server']['host'] === '::1'
326 $this->db_isLocal = true;
328 $this->status = $server_status;
329 $this->sections = $sections;
330 $this->variables = $server_variables;
331 $this->used_queries = $used_queries;
332 $this->allocationMap = $allocationMap;
333 $this->links = $links;
334 $this->categoryUsed = $categoryUsed;
338 * cleanup of some deprecated values
340 * @param array $server_status status array to process
342 * @return array
344 public static function cleanDeprecated($server_status)
346 $deprecated = array(
347 'Com_prepare_sql' => 'Com_stmt_prepare',
348 'Com_execute_sql' => 'Com_stmt_execute',
349 'Com_dealloc_sql' => 'Com_stmt_close',
351 foreach ($deprecated as $old => $new) {
352 if (isset($server_status[$old]) && isset($server_status[$new])) {
353 unset($server_status[$old]);
356 return $server_status;
360 * Generates menu HTML
362 * @return string
364 public function getMenuHtml()
366 $url_params = PMA_URL_getCommon();
367 $items = array(
368 array(
369 'name' => __('Server'),
370 'url' => 'server_status.php'
372 array(
373 'name' => __('Processes'),
374 'url' => 'server_status_processes.php'
376 array(
377 'name' => __('Query statistics'),
378 'url' => 'server_status_queries.php'
380 array(
381 'name' => __('All status variables'),
382 'url' => 'server_status_variables.php'
384 array(
385 'name' => __('Monitor'),
386 'url' => 'server_status_monitor.php'
388 array(
389 'name' => __('Advisor'),
390 'url' => 'server_status_advisor.php'
394 $retval = '<ul id="topmenu2">';
395 foreach ($items as $item) {
396 $class = '';
397 if ($item['url'] === $this->selfUrl) {
398 $class = ' class="tabactive"';
400 $retval .= '<li>';
401 $retval .= '<a' . $class;
402 $retval .= ' href="' . $item['url'] . $url_params . '">';
403 $retval .= $item['name'];
404 $retval .= '</a>';
405 $retval .= '</li>';
407 $retval .= '</ul>';
408 $retval .= '<div class="clearfloat"></div>';
410 return $retval;
414 * Builds a <select> list for refresh rates
416 * @param string $name Name of select
417 * @param int $defaultRate Currently chosen rate
418 * @param array $refreshRates List of refresh rates
420 * @return string
422 public static function getHtmlForRefreshList($name,
423 $defaultRate = 5,
424 $refreshRates = Array(1, 2, 5, 10, 20, 40, 60, 120, 300, 600)
426 $return = '<select name="' . $name . '" id="id_' . $name
427 . '" class="refreshRate">';
428 foreach ($refreshRates as $rate) {
429 $selected = ($rate == $defaultRate)?' selected="selected"':'';
430 $return .= '<option value="' . $rate . '"' . $selected . '>';
431 if ($rate < 60) {
432 $return .= sprintf(
433 _ngettext('%d second', '%d seconds', $rate), $rate
435 } else {
436 $rate = $rate / 60;
437 $return .= sprintf(
438 _ngettext('%d minute', '%d minutes', $rate), $rate
441 $return .= '</option>';
443 $return .= '</select>';
444 return $return;