Data.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502
  1. <?php
  2. /* vim: set expandtab sw=4 ts=4 sts=4: */
  3. /**
  4. * PhpMyAdmin\Server\Status\Data class
  5. * Used by server_status_*.php pages
  6. *
  7. * @package PhpMyAdmin
  8. */
  9. namespace PhpMyAdmin\Server\Status;
  10. use PhpMyAdmin\Url;
  11. /**
  12. * This class provides data about the server status
  13. *
  14. * All properties of the class are read-only
  15. *
  16. * TODO: Use lazy initialisation for some of the properties
  17. * since not all of the server_status_*.php pages need
  18. * all the data that this class provides.
  19. *
  20. * @package PhpMyAdmin
  21. */
  22. class Data
  23. {
  24. public $status;
  25. public $sections;
  26. public $variables;
  27. public $used_queries;
  28. public $allocationMap;
  29. public $links;
  30. public $db_isLocal;
  31. public $section;
  32. public $sectionUsed;
  33. public $selfUrl;
  34. public $dataLoaded;
  35. /**
  36. * An empty setter makes the above properties read-only
  37. *
  38. * @param string $a key
  39. * @param mixed $b value
  40. *
  41. * @return void
  42. */
  43. public function __set($a, $b)
  44. {
  45. // Discard everything
  46. }
  47. /**
  48. * Gets the allocations for constructor
  49. *
  50. * @return array
  51. */
  52. private function _getAllocations()
  53. {
  54. return array(
  55. // variable name => section
  56. // variable names match when they begin with the given string
  57. 'Com_' => 'com',
  58. 'Innodb_' => 'innodb',
  59. 'Ndb_' => 'ndb',
  60. 'Handler_' => 'handler',
  61. 'Qcache_' => 'qcache',
  62. 'Threads_' => 'threads',
  63. 'Slow_launch_threads' => 'threads',
  64. 'Binlog_cache_' => 'binlog_cache',
  65. 'Created_tmp_' => 'created_tmp',
  66. 'Key_' => 'key',
  67. 'Delayed_' => 'delayed',
  68. 'Not_flushed_delayed_rows' => 'delayed',
  69. 'Flush_commands' => 'query',
  70. 'Last_query_cost' => 'query',
  71. 'Slow_queries' => 'query',
  72. 'Queries' => 'query',
  73. 'Prepared_stmt_count' => 'query',
  74. 'Select_' => 'select',
  75. 'Sort_' => 'sort',
  76. 'Open_tables' => 'table',
  77. 'Opened_tables' => 'table',
  78. 'Open_table_definitions' => 'table',
  79. 'Opened_table_definitions' => 'table',
  80. 'Table_locks_' => 'table',
  81. 'Rpl_status' => 'repl',
  82. 'Slave_' => 'repl',
  83. 'Tc_' => 'tc',
  84. 'Ssl_' => 'ssl',
  85. 'Open_files' => 'files',
  86. 'Open_streams' => 'files',
  87. 'Opened_files' => 'files',
  88. );
  89. }
  90. /**
  91. * Gets the sections for constructor
  92. *
  93. * @return array
  94. */
  95. private function _getSections()
  96. {
  97. return array(
  98. // section => section name (description)
  99. 'com' => 'Com',
  100. 'query' => __('SQL query'),
  101. 'innodb' => 'InnoDB',
  102. 'ndb' => 'NDB',
  103. 'handler' => __('Handler'),
  104. 'qcache' => __('Query cache'),
  105. 'threads' => __('Threads'),
  106. 'binlog_cache' => __('Binary log'),
  107. 'created_tmp' => __('Temporary data'),
  108. 'delayed' => __('Delayed inserts'),
  109. 'key' => __('Key cache'),
  110. 'select' => __('Joins'),
  111. 'repl' => __('Replication'),
  112. 'sort' => __('Sorting'),
  113. 'table' => __('Tables'),
  114. 'tc' => __('Transaction coordinator'),
  115. 'files' => __('Files'),
  116. 'ssl' => 'SSL',
  117. 'other' => __('Other')
  118. );
  119. }
  120. /**
  121. * Gets the links for constructor
  122. *
  123. * @return array
  124. */
  125. private function _getLinks()
  126. {
  127. $links = array();
  128. // variable or section name => (name => url)
  129. $links['table'][__('Flush (close) all tables')] = $this->selfUrl
  130. . Url::getCommon(
  131. array(
  132. 'flush' => 'TABLES'
  133. )
  134. );
  135. $links['table'][__('Show open tables')]
  136. = 'sql.php' . Url::getCommon(
  137. array(
  138. 'sql_query' => 'SHOW OPEN TABLES',
  139. 'goto' => $this->selfUrl,
  140. )
  141. );
  142. if ($GLOBALS['replication_info']['master']['status']) {
  143. $links['repl'][__('Show slave hosts')]
  144. = 'sql.php' . Url::getCommon(
  145. array(
  146. 'sql_query' => 'SHOW SLAVE HOSTS',
  147. 'goto' => $this->selfUrl,
  148. )
  149. );
  150. $links['repl'][__('Show master status')] = '#replication_master';
  151. }
  152. if ($GLOBALS['replication_info']['slave']['status']) {
  153. $links['repl'][__('Show slave status')] = '#replication_slave';
  154. }
  155. $links['repl']['doc'] = 'replication';
  156. $links['qcache'][__('Flush query cache')]
  157. = $this->selfUrl
  158. . Url::getCommon(
  159. array(
  160. 'flush' => 'QUERY CACHE'
  161. )
  162. );
  163. $links['qcache']['doc'] = 'query_cache';
  164. $links['threads']['doc'] = 'mysql_threads';
  165. $links['key']['doc'] = 'myisam_key_cache';
  166. $links['binlog_cache']['doc'] = 'binary_log';
  167. $links['Slow_queries']['doc'] = 'slow_query_log';
  168. $links['innodb'][__('Variables')]
  169. = 'server_engines.php?' . Url::getCommon(array('engine' => 'InnoDB'));
  170. $links['innodb'][__('InnoDB Status')]
  171. = 'server_engines.php'
  172. . Url::getCommon(
  173. array(
  174. 'engine' => 'InnoDB',
  175. 'page' => 'Status'
  176. )
  177. );
  178. $links['innodb']['doc'] = 'innodb';
  179. return($links);
  180. }
  181. /**
  182. * Calculate some values
  183. *
  184. * @param array $server_status contains results of SHOW GLOBAL STATUS
  185. * @param array $server_variables contains results of SHOW GLOBAL VARIABLES
  186. *
  187. * @return array $server_status
  188. */
  189. private function _calculateValues(array $server_status, array $server_variables)
  190. {
  191. // Key_buffer_fraction
  192. if (isset($server_status['Key_blocks_unused'])
  193. && isset($server_variables['key_cache_block_size'])
  194. && isset($server_variables['key_buffer_size'])
  195. && $server_variables['key_buffer_size'] != 0
  196. ) {
  197. $server_status['Key_buffer_fraction_%']
  198. = 100
  199. - $server_status['Key_blocks_unused']
  200. * $server_variables['key_cache_block_size']
  201. / $server_variables['key_buffer_size']
  202. * 100;
  203. } elseif (isset($server_status['Key_blocks_used'])
  204. && isset($server_variables['key_buffer_size'])
  205. && $server_variables['key_buffer_size'] != 0
  206. ) {
  207. $server_status['Key_buffer_fraction_%']
  208. = $server_status['Key_blocks_used']
  209. * 1024
  210. / $server_variables['key_buffer_size'];
  211. }
  212. // Ratio for key read/write
  213. if (isset($server_status['Key_writes'])
  214. && isset($server_status['Key_write_requests'])
  215. && $server_status['Key_write_requests'] > 0
  216. ) {
  217. $key_writes = $server_status['Key_writes'];
  218. $key_write_requests = $server_status['Key_write_requests'];
  219. $server_status['Key_write_ratio_%']
  220. = 100 * $key_writes / $key_write_requests;
  221. }
  222. if (isset($server_status['Key_reads'])
  223. && isset($server_status['Key_read_requests'])
  224. && $server_status['Key_read_requests'] > 0
  225. ) {
  226. $key_reads = $server_status['Key_reads'];
  227. $key_read_requests = $server_status['Key_read_requests'];
  228. $server_status['Key_read_ratio_%']
  229. = 100 * $key_reads / $key_read_requests;
  230. }
  231. // Threads_cache_hitrate
  232. if (isset($server_status['Threads_created'])
  233. && isset($server_status['Connections'])
  234. && $server_status['Connections'] > 0
  235. ) {
  236. $server_status['Threads_cache_hitrate_%']
  237. = 100 - $server_status['Threads_created']
  238. / $server_status['Connections'] * 100;
  239. }
  240. return $server_status;
  241. }
  242. /**
  243. * Sort variables into arrays
  244. *
  245. * @param array $server_status contains results of SHOW GLOBAL STATUS
  246. * @param array $allocations allocations for sections
  247. * @param array $allocationMap map variables to their section
  248. * @param array $sectionUsed is a section used?
  249. * @param array $used_queries used queries
  250. *
  251. * @return array ($allocationMap, $sectionUsed, $used_queries)
  252. */
  253. private function _sortVariables(
  254. array $server_status, array $allocations, array $allocationMap, array $sectionUsed,
  255. array $used_queries
  256. ) {
  257. foreach ($server_status as $name => $value) {
  258. $section_found = false;
  259. foreach ($allocations as $filter => $section) {
  260. if (mb_strpos($name, $filter) !== false) {
  261. $allocationMap[$name] = $section;
  262. $sectionUsed[$section] = true;
  263. $section_found = true;
  264. if ($section == 'com' && $value > 0) {
  265. $used_queries[$name] = $value;
  266. }
  267. break; // Only exits inner loop
  268. }
  269. }
  270. if (! $section_found) {
  271. $allocationMap[$name] = 'other';
  272. $sectionUsed['other'] = true;
  273. }
  274. }
  275. return array($allocationMap, $sectionUsed, $used_queries);
  276. }
  277. /**
  278. * Constructor
  279. */
  280. public function __construct()
  281. {
  282. $this->selfUrl = basename($GLOBALS['PMA_PHP_SELF']);
  283. // get status from server
  284. $server_status_result = $GLOBALS['dbi']->tryQuery('SHOW GLOBAL STATUS');
  285. $server_status = array();
  286. if ($server_status_result === false) {
  287. $this->dataLoaded = false;
  288. } else {
  289. $this->dataLoaded = true;
  290. while ($arr = $GLOBALS['dbi']->fetchRow($server_status_result)) {
  291. $server_status[$arr[0]] = $arr[1];
  292. }
  293. $GLOBALS['dbi']->freeResult($server_status_result);
  294. }
  295. // for some calculations we require also some server settings
  296. $server_variables = $GLOBALS['dbi']->fetchResult(
  297. 'SHOW GLOBAL VARIABLES', 0, 1
  298. );
  299. // cleanup of some deprecated values
  300. $server_status = self::cleanDeprecated($server_status);
  301. // calculate some values
  302. $server_status = $this->_calculateValues(
  303. $server_status, $server_variables
  304. );
  305. // split variables in sections
  306. $allocations = $this->_getAllocations();
  307. $sections = $this->_getSections();
  308. // define some needful links/commands
  309. $links = $this->_getLinks();
  310. // Variable to contain all com_ variables (query statistics)
  311. $used_queries = array();
  312. // Variable to map variable names to their respective section name
  313. // (used for js category filtering)
  314. $allocationMap = array();
  315. // Variable to mark used sections
  316. $sectionUsed = array();
  317. // sort vars into arrays
  318. list(
  319. $allocationMap, $sectionUsed, $used_queries
  320. ) = $this->_sortVariables(
  321. $server_status, $allocations, $allocationMap, $sectionUsed,
  322. $used_queries
  323. );
  324. // admin commands are not queries (e.g. they include COM_PING,
  325. // which is excluded from $server_status['Questions'])
  326. unset($used_queries['Com_admin_commands']);
  327. // Set all class properties
  328. $this->db_isLocal = false;
  329. $serverHostToLower = mb_strtolower(
  330. $GLOBALS['cfg']['Server']['host']
  331. );
  332. if ($serverHostToLower === 'localhost'
  333. || $GLOBALS['cfg']['Server']['host'] === '127.0.0.1'
  334. || $GLOBALS['cfg']['Server']['host'] === '::1'
  335. ) {
  336. $this->db_isLocal = true;
  337. }
  338. $this->status = $server_status;
  339. $this->sections = $sections;
  340. $this->variables = $server_variables;
  341. $this->used_queries = $used_queries;
  342. $this->allocationMap = $allocationMap;
  343. $this->links = $links;
  344. $this->sectionUsed = $sectionUsed;
  345. }
  346. /**
  347. * cleanup of some deprecated values
  348. *
  349. * @param array $server_status status array to process
  350. *
  351. * @return array
  352. */
  353. public static function cleanDeprecated(array $server_status)
  354. {
  355. $deprecated = array(
  356. 'Com_prepare_sql' => 'Com_stmt_prepare',
  357. 'Com_execute_sql' => 'Com_stmt_execute',
  358. 'Com_dealloc_sql' => 'Com_stmt_close',
  359. );
  360. foreach ($deprecated as $old => $new) {
  361. if (isset($server_status[$old]) && isset($server_status[$new])) {
  362. unset($server_status[$old]);
  363. }
  364. }
  365. return $server_status;
  366. }
  367. /**
  368. * Generates menu HTML
  369. *
  370. * @return string
  371. */
  372. public function getMenuHtml()
  373. {
  374. $url_params = Url::getCommon();
  375. $items = array(
  376. array(
  377. 'name' => __('Server'),
  378. 'url' => 'server_status.php'
  379. ),
  380. array(
  381. 'name' => __('Processes'),
  382. 'url' => 'server_status_processes.php'
  383. ),
  384. array(
  385. 'name' => __('Query statistics'),
  386. 'url' => 'server_status_queries.php'
  387. ),
  388. array(
  389. 'name' => __('All status variables'),
  390. 'url' => 'server_status_variables.php'
  391. ),
  392. array(
  393. 'name' => __('Monitor'),
  394. 'url' => 'server_status_monitor.php'
  395. ),
  396. array(
  397. 'name' => __('Advisor'),
  398. 'url' => 'server_status_advisor.php'
  399. )
  400. );
  401. $retval = '<ul id="topmenu2">';
  402. foreach ($items as $item) {
  403. $class = '';
  404. if ($item['url'] === $this->selfUrl) {
  405. $class = ' class="tabactive"';
  406. }
  407. $retval .= '<li>';
  408. $retval .= '<a' . $class;
  409. $retval .= ' href="' . $item['url'] . $url_params . '">';
  410. $retval .= $item['name'];
  411. $retval .= '</a>';
  412. $retval .= '</li>';
  413. }
  414. $retval .= '</ul>';
  415. $retval .= '<div class="clearfloat"></div>';
  416. return $retval;
  417. }
  418. /**
  419. * Builds a <select> list for refresh rates
  420. *
  421. * @param string $name Name of select
  422. * @param int $defaultRate Currently chosen rate
  423. * @param array $refreshRates List of refresh rates
  424. *
  425. * @return string
  426. */
  427. public static function getHtmlForRefreshList($name,
  428. $defaultRate = 5,
  429. array $refreshRates = array(1, 2, 5, 10, 20, 40, 60, 120, 300, 600)
  430. ) {
  431. $return = '<select name="' . $name . '" id="id_' . $name
  432. . '" class="refreshRate">';
  433. foreach ($refreshRates as $rate) {
  434. $selected = ($rate == $defaultRate)?' selected="selected"':'';
  435. $return .= '<option value="' . $rate . '"' . $selected . '>';
  436. if ($rate < 60) {
  437. $return .= sprintf(
  438. _ngettext('%d second', '%d seconds', $rate), $rate
  439. );
  440. } else {
  441. $rate = $rate / 60;
  442. $return .= sprintf(
  443. _ngettext('%d minute', '%d minutes', $rate), $rate
  444. );
  445. }
  446. $return .= '</option>';
  447. }
  448. $return .= '</select>';
  449. return $return;
  450. }
  451. }