PageRenderTime 53ms CodeModel.GetById 27ms RepoModel.GetById 1ms app.codeStats 0ms

/phpmyadmin/libraries/ServerStatusData.class.php

https://github.com/drbowen/openemr
PHP | 380 lines | 257 code | 45 blank | 78 comment | 29 complexity | b6dc26281036fbe7cba80991c4419e0e MD5 | raw file
  1. <?php
  2. /* vim: set expandtab sw=4 ts=4 sts=4: */
  3. /**
  4. * PMA_ServerStatusData class
  5. * Used by server_status_*.php pages
  6. *
  7. * @package PhpMyAdmin
  8. */
  9. require_once 'libraries/common.inc.php';
  10. /**
  11. * This class provides data about the server status
  12. *
  13. * All properties of the class are read-only
  14. *
  15. * TODO: Use lazy initialisation for some of the properties
  16. * since not all of the server_status_*.php pages need
  17. * all the data that this class provides.
  18. *
  19. * @package PhpMyAdmin
  20. */
  21. class PMA_ServerStatusData
  22. {
  23. public $status;
  24. public $sections;
  25. public $variables;
  26. public $used_queries;
  27. public $allocationMap;
  28. public $links;
  29. public $db_isLocal;
  30. public $section;
  31. public $categoryUsed;
  32. public $selfUrl;
  33. /**
  34. * An empty setter makes the above properties read-only
  35. *
  36. * @param string $a key
  37. * @param mixed $b value
  38. *
  39. * @return void
  40. */
  41. public function __set($a, $b)
  42. {
  43. // Discard everything
  44. }
  45. /**
  46. * Constructor
  47. *
  48. * @return object
  49. */
  50. public function __construct()
  51. {
  52. $this->selfUrl = basename($GLOBALS['PMA_PHP_SELF']);
  53. /**
  54. * get status from server
  55. */
  56. $server_status = PMA_DBI_fetch_result('SHOW GLOBAL STATUS', 0, 1);
  57. if (PMA_DRIZZLE) {
  58. // Drizzle doesn't put query statistics into variables, add it
  59. $sql = "SELECT concat('Com_', variable_name), variable_value
  60. FROM data_dictionary.GLOBAL_STATEMENTS";
  61. $statements = PMA_DBI_fetch_result($sql, 0, 1);
  62. $server_status = array_merge($server_status, $statements);
  63. }
  64. /**
  65. * for some calculations we require also some server settings
  66. */
  67. $server_variables = PMA_DBI_fetch_result('SHOW GLOBAL VARIABLES', 0, 1);
  68. /**
  69. * cleanup of some deprecated values
  70. */
  71. $server_status = self::cleanDeprecated($server_status);
  72. /**
  73. * calculate some values
  74. */
  75. // Key_buffer_fraction
  76. if (isset($server_status['Key_blocks_unused'])
  77. && isset($server_variables['key_cache_block_size'])
  78. && isset($server_variables['key_buffer_size'])
  79. ) {
  80. $server_status['Key_buffer_fraction_%']
  81. = 100
  82. - $server_status['Key_blocks_unused']
  83. * $server_variables['key_cache_block_size']
  84. / $server_variables['key_buffer_size']
  85. * 100;
  86. } elseif (isset($server_status['Key_blocks_used'])
  87. && isset($server_variables['key_buffer_size'])) {
  88. $server_status['Key_buffer_fraction_%']
  89. = $server_status['Key_blocks_used']
  90. * 1024
  91. / $server_variables['key_buffer_size'];
  92. }
  93. // Ratio for key read/write
  94. if (isset($server_status['Key_writes'])
  95. && isset($server_status['Key_write_requests'])
  96. && $server_status['Key_write_requests'] > 0
  97. ) {
  98. $server_status['Key_write_ratio_%']
  99. = 100 * $server_status['Key_writes'] / $server_status['Key_write_requests'];
  100. }
  101. if (isset($server_status['Key_reads'])
  102. && isset($server_status['Key_read_requests'])
  103. && $server_status['Key_read_requests'] > 0
  104. ) {
  105. $server_status['Key_read_ratio_%']
  106. = 100 * $server_status['Key_reads'] / $server_status['Key_read_requests'];
  107. }
  108. // Threads_cache_hitrate
  109. if (isset($server_status['Threads_created'])
  110. && isset($server_status['Connections'])
  111. && $server_status['Connections'] > 0
  112. ) {
  113. $server_status['Threads_cache_hitrate_%']
  114. = 100 - $server_status['Threads_created']
  115. / $server_status['Connections'] * 100;
  116. }
  117. /**
  118. * split variables in sections
  119. */
  120. $allocations = array(
  121. // variable name => section
  122. // variable names match when they begin with the given string
  123. 'Com_' => 'com',
  124. 'Innodb_' => 'innodb',
  125. 'Ndb_' => 'ndb',
  126. 'Handler_' => 'handler',
  127. 'Qcache_' => 'qcache',
  128. 'Threads_' => 'threads',
  129. 'Slow_launch_threads' => 'threads',
  130. 'Binlog_cache_' => 'binlog_cache',
  131. 'Created_tmp_' => 'created_tmp',
  132. 'Key_' => 'key',
  133. 'Delayed_' => 'delayed',
  134. 'Not_flushed_delayed_rows' => 'delayed',
  135. 'Flush_commands' => 'query',
  136. 'Last_query_cost' => 'query',
  137. 'Slow_queries' => 'query',
  138. 'Queries' => 'query',
  139. 'Prepared_stmt_count' => 'query',
  140. 'Select_' => 'select',
  141. 'Sort_' => 'sort',
  142. 'Open_tables' => 'table',
  143. 'Opened_tables' => 'table',
  144. 'Open_table_definitions' => 'table',
  145. 'Opened_table_definitions' => 'table',
  146. 'Table_locks_' => 'table',
  147. 'Rpl_status' => 'repl',
  148. 'Slave_' => 'repl',
  149. 'Tc_' => 'tc',
  150. 'Ssl_' => 'ssl',
  151. 'Open_files' => 'files',
  152. 'Open_streams' => 'files',
  153. 'Opened_files' => 'files',
  154. );
  155. $sections = array(
  156. // section => section name (description)
  157. 'com' => 'Com',
  158. 'query' => __('SQL query'),
  159. 'innodb' => 'InnoDB',
  160. 'ndb' => 'NDB',
  161. 'handler' => __('Handler'),
  162. 'qcache' => __('Query cache'),
  163. 'threads' => __('Threads'),
  164. 'binlog_cache' => __('Binary log'),
  165. 'created_tmp' => __('Temporary data'),
  166. 'delayed' => __('Delayed inserts'),
  167. 'key' => __('Key cache'),
  168. 'select' => __('Joins'),
  169. 'repl' => __('Replication'),
  170. 'sort' => __('Sorting'),
  171. 'table' => __('Tables'),
  172. 'tc' => __('Transaction coordinator'),
  173. 'files' => __('Files'),
  174. 'ssl' => 'SSL',
  175. 'other' => __('Other')
  176. );
  177. /**
  178. * define some needfull links/commands
  179. */
  180. // variable or section name => (name => url)
  181. $links = array();
  182. $links['table'][__('Flush (close) all tables')]
  183. = $this->selfUrl . '?flush=TABLES&amp;' . PMA_generate_common_url();
  184. $links['table'][__('Show open tables')]
  185. = 'sql.php?sql_query=' . urlencode('SHOW OPEN TABLES') .
  186. '&amp;goto=' . $this->selfUrl . '&amp;' . PMA_generate_common_url();
  187. if ($GLOBALS['server_master_status']) {
  188. $links['repl'][__('Show slave hosts')]
  189. = 'sql.php?sql_query=' . urlencode('SHOW SLAVE HOSTS')
  190. . '&amp;goto=' . $this->selfUrl . '&amp;'
  191. . PMA_generate_common_url();
  192. $links['repl'][__('Show master status')] = '#replication_master';
  193. }
  194. if ($GLOBALS['server_slave_status']) {
  195. $links['repl'][__('Show slave status')] = '#replication_slave';
  196. }
  197. $links['repl']['doc'] = 'replication';
  198. $links['qcache'][__('Flush query cache')]
  199. = $this->selfUrl . '?flush=' . urlencode('QUERY CACHE') . '&amp;' .
  200. PMA_generate_common_url();
  201. $links['qcache']['doc'] = 'query_cache';
  202. $links['threads']['doc'] = 'mysql_threads';
  203. $links['key']['doc'] = 'myisam_key_cache';
  204. $links['binlog_cache']['doc'] = 'binary_log';
  205. $links['Slow_queries']['doc'] = 'slow_query_log';
  206. $links['innodb'][__('Variables')]
  207. = 'server_engines.php?engine=InnoDB&amp;' . PMA_generate_common_url();
  208. $links['innodb'][__('InnoDB Status')]
  209. = 'server_engines.php?engine=InnoDB&amp;page=Status&amp;' .
  210. PMA_generate_common_url();
  211. $links['innodb']['doc'] = 'innodb';
  212. // Variable to contain all com_ variables (query statistics)
  213. $used_queries = array();
  214. // Variable to map variable names to their respective section name
  215. // (used for js category filtering)
  216. $allocationMap = array();
  217. // Variable to mark used sections
  218. $categoryUsed = array();
  219. // sort vars into arrays
  220. foreach ($server_status as $name => $value) {
  221. $section_found = false;
  222. foreach ($allocations as $filter => $section) {
  223. if (strpos($name, $filter) !== false) {
  224. $allocationMap[$name] = $section;
  225. $categoryUsed[$section] = true;
  226. $section_found = true;
  227. if ($section == 'com' && $value > 0) {
  228. $used_queries[$name] = $value;
  229. }
  230. break; // Only exits inner loop
  231. }
  232. }
  233. if (!$section_found) {
  234. $allocationMap[$name] = 'other';
  235. $categoryUsed['other'] = true;
  236. }
  237. }
  238. if (PMA_DRIZZLE) {
  239. $used_queries = PMA_DBI_fetch_result(
  240. 'SELECT * FROM data_dictionary.global_statements',
  241. 0,
  242. 1
  243. );
  244. unset($used_queries['admin_commands']);
  245. } else {
  246. // admin commands are not queries (e.g. they include COM_PING,
  247. // which is excluded from $server_status['Questions'])
  248. unset($used_queries['Com_admin_commands']);
  249. }
  250. // Set all class properties
  251. $this->db_isLocal = false;
  252. if (strtolower($GLOBALS['cfg']['Server']['host']) === 'localhost'
  253. || $GLOBALS['cfg']['Server']['host'] === '127.0.0.1'
  254. || $GLOBALS['cfg']['Server']['host'] === '::1'
  255. ) {
  256. $this->db_isLocal = true;
  257. }
  258. $this->status = $server_status;
  259. $this->sections = $sections;
  260. $this->variables = $server_variables;
  261. $this->used_queries = $used_queries;
  262. $this->allocationMap = $allocationMap;
  263. $this->links = $links;
  264. $this->categoryUsed = $categoryUsed;
  265. }
  266. /**
  267. * cleanup of some deprecated values
  268. *
  269. * @param array $server_status status array to process
  270. *
  271. * @return array
  272. */
  273. public static function cleanDeprecated($server_status)
  274. {
  275. $deprecated = array(
  276. 'Com_prepare_sql' => 'Com_stmt_prepare',
  277. 'Com_execute_sql' => 'Com_stmt_execute',
  278. 'Com_dealloc_sql' => 'Com_stmt_close',
  279. );
  280. foreach ($deprecated as $old => $new) {
  281. if (isset($server_status[$old]) && isset($server_status[$new])) {
  282. unset($server_status[$old]);
  283. }
  284. }
  285. return $server_status;
  286. }
  287. /**
  288. * cleanup of some deprecated values
  289. *
  290. * @return array
  291. */
  292. public function getMenuHtml()
  293. {
  294. $url_params = PMA_generate_common_url();
  295. $items = array(
  296. array(
  297. 'name' => __('Server'),
  298. 'url' => 'server_status.php'
  299. ),
  300. array(
  301. 'name' => __('Query statistics'),
  302. 'url' => 'server_status_queries.php'
  303. ),
  304. array(
  305. 'name' => __('All status variables'),
  306. 'url' => 'server_status_variables.php'
  307. ),
  308. array(
  309. 'name' => __('Monitor'),
  310. 'url' => 'server_status_monitor.php'
  311. ),
  312. array(
  313. 'name' => __('Advisor'),
  314. 'url' => 'server_status_advisor.php'
  315. )
  316. );
  317. $retval = '<ul id="topmenu2">';
  318. foreach ($items as $item) {
  319. $class = '';
  320. if ($item['url'] === $this->selfUrl) {
  321. $class = ' class="tabactive"';
  322. }
  323. $retval .= '<li>';
  324. $retval .= '<a' . $class;
  325. $retval .= ' href="' . $item['url'] . '?' . $url_params . '">';
  326. $retval .= $item['name'];
  327. $retval .= '</a>';
  328. $retval .= '</li>';
  329. }
  330. $retval .= '</ul>';
  331. $retval .= '<div class="clearfloat"></div>';
  332. return $retval;
  333. }
  334. }
  335. ?>