2 /* vim: set expandtab sw=4 ts=4 sts=4: */
5 * functions for displaying server status sub item: monitor
7 * @usedby server_status_monitor.php
11 if (! defined('PHPMYADMIN')) {
16 * Prints html with monitor
18 * @param PMA_ServerStatusData $ServerStatusData Server status data
22 function PMA_getHtmlForMonitor($ServerStatusData)
24 $retval = PMA_getHtmlForTabLinks();
26 $retval .= PMA_getHtmlForSettingsDialog();
28 $retval .= PMA_getHtmlForInstructionsDialog();
30 $retval .= PMA_getHtmlForAddChartDialog();
33 $retval .= PMA_getHtmlForAnalyseDialog();
36 $retval .= '<table class="clearfloat" id="chartGrid"></table>';
37 $retval .= '<div id="logTable">';
41 $retval .= '<script type="text/javascript">';
42 $retval .= 'variableNames = [ ';
44 foreach ($ServerStatusData->status
as $name=>$value) {
45 if (is_numeric($value)) {
49 $retval .= "'" . $name . "'";
53 $retval .= '</script>';
59 * Returns html for Analyse Dialog
63 function PMA_getHtmlForAnalyseDialog()
65 $retval = '<div id="logAnalyseDialog" title="';
66 $retval .= __('Log statistics') . '" style="display:none;">';
67 $retval .= '<p>' . __('Selected time range:');
68 $retval .= '<input type="text" name="dateStart"'
69 . ' class="datetimefield" value="" /> - ';
70 $retval .= '<input type="text" name="dateEnd" class="datetimefield" value="" />';
72 $retval .= '<input type="checkbox" id="limitTypes"'
73 . ' value="1" checked="checked" />';
74 $retval .= '<label for="limitTypes">';
75 $retval .= __('Only retrieve SELECT,INSERT,UPDATE and DELETE Statements');
76 $retval .= '</label>';
78 $retval .= '<input type="checkbox" id="removeVariables"'
79 . ' value="1" checked="checked" />';
80 $retval .= '<label for="removeVariables">';
81 $retval .= __('Remove variable data in INSERT statements for better grouping');
82 $retval .= '</label>';
85 'Choose from which log you want the statistics to be generated from.'
89 $retval .= __('Results are grouped by query text.');
92 $retval .= '<div id="queryAnalyzerDialog" title="';
93 $retval .= __('Query analyzer') . '" style="display:none;">';
94 $retval .= '<textarea id="sqlquery"> </textarea>';
96 $retval .= '<div class="placeHolder"></div>';
103 * Returns html for Instructions Dialog
107 function PMA_getHtmlForInstructionsDialog()
109 $retval = '<div id="monitorInstructionsDialog" title="';
110 $retval .= __('Monitor Instructions') . '" style="display:none;">';
112 'The phpMyAdmin Monitor can assist you in optimizing the server'
113 . ' configuration and track down time intensive queries. For the latter you'
114 . ' will need to set log_output to \'TABLE\' and have either the'
115 . ' slow_query_log or general_log enabled. Note however, that the'
116 . ' general_log produces a lot of data and increases server load'
120 $retval .= '<p></p>';
121 $retval .= '<img class="ajaxIcon" src="';
122 $retval .= $GLOBALS['pmaThemeImage'] . 'ajax_clock_small.gif"';
123 $retval .= ' alt="' . __('Loading…') . '" />';
124 $retval .= '<div class="ajaxContent"></div>';
125 $retval .= '<div class="monitorUse" style="display:none;">';
126 $retval .= '<p></p>';
127 $retval .= '<strong>';
128 $retval .= __('Using the monitor:');
129 $retval .= '</strong><p>';
131 'Your browser will refresh all displayed charts in a regular interval.'
132 . ' You may add charts and change the refresh rate under \'Settings\','
133 . ' or remove any chart using the cog icon on each respective chart.'
135 $retval .= '</p><p>';
137 'To display queries from the logs, select the relevant time span on any'
138 . ' chart by holding down the left mouse button and panning over the'
139 . ' chart. Once confirmed, this will load a table of grouped queries,'
140 . ' there you may click on any occurring SELECT statements to further'
145 $retval .= PMA_Util
::getImage('s_attention.png');
146 $retval .= '<strong>';
147 $retval .= __('Please note:');
148 $retval .= '</strong><br />';
150 'Enabling the general_log may increase the server load by'
151 . ' 5-15%. Also be aware that generating statistics from the logs is a'
152 . ' load intensive task, so it is advisable to select only a small time'
153 . ' span and to disable the general_log and empty its table once'
154 . ' monitoring is not required any more.'
164 * Returns html for addChartDialog
168 function PMA_getHtmlForAddChartDialog()
170 $retval = '<div id="addChartDialog" title="'
171 . __('Add chart') . '" style="display:none;">';
172 $retval .= '<div id="tabGridVariables">';
173 $retval .= '<p><input type="text" name="chartTitle" value="'
174 . __('Chart Title') . '" /></p>';
175 $retval .= '<input type="radio" name="chartType"'
176 . ' value="preset" id="chartPreset" />';
177 $retval .= '<label for="chartPreset">' . __('Preset chart') . '</label>';
178 $retval .= '<select name="presetCharts"></select><br/>';
179 $retval .= '<input type="radio" name="chartType" value="variable" '
180 . 'id="chartStatusVar" checked="checked" />';
181 $retval .= '<label for="chartStatusVar">';
182 $retval .= __('Status variable(s)');
183 $retval .= '</label><br/>';
184 $retval .= '<div id="chartVariableSettings">';
185 $retval .= '<label for="chartSeries">' . __('Select series:') . '</label><br />';
186 $retval .= '<select id="chartSeries" name="varChartList" size="1">';
187 $retval .= '<option>' . __('Commonly monitored') . '</option>';
188 $retval .= '<option>Processes</option>';
189 $retval .= '<option>Questions</option>';
190 $retval .= '<option>Connections</option>';
191 $retval .= '<option>Bytes_sent</option>';
192 $retval .= '<option>Bytes_received</option>';
193 $retval .= '<option>Threads_connected</option>';
194 $retval .= '<option>Created_tmp_disk_tables</option>';
195 $retval .= '<option>Handler_read_first</option>';
196 $retval .= '<option>Innodb_buffer_pool_wait_free</option>';
197 $retval .= '<option>Key_reads</option>';
198 $retval .= '<option>Open_tables</option>';
199 $retval .= '<option>Select_full_join</option>';
200 $retval .= '<option>Slow_queries</option>';
201 $retval .= '</select><br />';
202 $retval .= '<label for="variableInput">';
203 $retval .= __('or type variable name:');
204 $retval .= ' </label>';
205 $retval .= '<input type="text" name="variableInput" id="variableInput" />';
206 $retval .= '<p></p>';
207 $retval .= '<input type="checkbox" name="differentialValue"'
208 . ' id="differentialValue" value="differential" checked="checked" />';
209 $retval .= '<label for="differentialValue">';
210 $retval .= __('Display as differential value');
211 $retval .= '</label><br />';
212 $retval .= '<input type="checkbox" id="useDivisor"'
213 . ' name="useDivisor" value="1" />';
214 $retval .= '<label for="useDivisor">' . __('Apply a divisor') . '</label>';
215 $retval .= '<span class="divisorInput" style="display:none;">';
216 $retval .= '<input type="text" name="valueDivisor" size="4" value="1" />';
217 $retval .= '(<a href="#kibDivisor">' . __('KiB') . '</a>, ';
218 $retval .= '<a href="#mibDivisor">' . __('MiB') . '</a>)';
219 $retval .= '</span><br />';
220 $retval .= '<input type="checkbox" id="useUnit" name="useUnit" value="1" />';
221 $retval .= '<label for="useUnit">';
222 $retval .= __('Append unit to data values');
223 $retval .= '</label>';
224 $retval .= '<span class="unitInput" style="display:none;">';
225 $retval .= '<input type="text" name="valueUnit" size="4" value="" />';
226 $retval .= '</span>';
228 $retval .= '<a href="#submitAddSeries"><b>' . __('Add this series') . '</b></a>';
229 $retval .= '<span id="clearSeriesLink" style="display:none;">';
230 $retval .= ' | <a href="#submitClearSeries">' . __('Clear series') . '</a>';
231 $retval .= '</span>';
233 $retval .= __('Series in Chart:');
235 $retval .= '<span id="seriesPreview">';
236 $retval .= '<i>' . __('None') . '</i>';
237 $retval .= '</span>';
246 * Returns html with Tab Links
250 function PMA_getHtmlForTabLinks()
252 $retval = '<div class="tabLinks">';
253 $retval .= '<a href="#pauseCharts">';
254 $retval .= PMA_Util
::getImage('play.png') . __('Start Monitor');
256 $retval .= '<a href="#settingsPopup" class="popupLink">';
257 $retval .= PMA_Util
::getImage('s_cog.png') . __('Settings');
260 $retval .= '<a href="#monitorInstructionsDialog">';
261 $retval .= PMA_Util
::getImage('b_help.png') . __('Instructions/Setup');
263 $retval .= '<a href="#endChartEditMode" style="display:none;">';
264 $retval .= PMA_Util
::getImage('s_okay.png');
265 $retval .= __('Done dragging (rearranging) charts');
273 * Returns html with Settings dialog
277 function PMA_getHtmlForSettingsDialog()
279 $retval = '<div class="popupContent settingsPopup">';
280 $retval .= '<a href="#addNewChart">';
281 $retval .= PMA_Util
::getImage('b_chart.png') . __('Add chart');
283 $retval .= '<a href="#rearrangeCharts">';
284 $retval .= PMA_Util
::getImage('b_tblops.png') . __('Enable charts dragging');
286 $retval .= '<div class="clearfloat paddingtop"></div>';
287 $retval .= '<div class="floatleft">';
288 $retval .= __('Refresh rate') . '<br />';
289 $retval .= PMA_ServerStatusData
::getHtmlForRefreshList(
292 Array(2, 3, 4, 5, 10, 20, 40, 60, 120, 300, 600, 1200)
296 $retval .= '<div class="floatleft">';
297 $retval .= __('Chart columns');
299 $retval .= '<select name="chartColumns">';
300 $retval .= '<option>1</option>';
301 $retval .= '<option>2</option>';
302 $retval .= '<option>3</option>';
303 $retval .= '<option>4</option>';
304 $retval .= '<option>5</option>';
305 $retval .= '<option>6</option>';
306 $retval .= '</select>';
308 $retval .= '<div class="clearfloat paddingtop">';
309 $retval .= '<b>' . __('Chart arrangement') . '</b> ';
310 $retval .= PMA_Util
::showHint(
312 'The arrangement of the charts is stored to the browsers local storage. '
313 . 'You may want to export it if you have a complicated set up.'
317 $retval .= '<a class="ajax" href="#importMonitorConfig">';
318 $retval .= __('Import');
320 $retval .= ' ';
321 $retval .= '<a class="disableAjax" href="#exportMonitorConfig">';
322 $retval .= __('Export');
324 $retval .= ' ';
325 $retval .= '<a href="#clearMonitorConfig">';
326 $retval .= __('Reset to default');
336 * Define some data and links needed on the client side
338 * @param PMA_ServerStatusData $ServerStatusData Server status data
342 function PMA_getHtmlForClientSideDataAndLinks($ServerStatusData)
345 * Define some data needed on the client side
347 $input = '<input type="hidden" name="%s" value="%s" />';
348 $form = '<form id="js_data" class="hide">';
349 $form .= sprintf($input, 'server_time', microtime(true) * 1000);
350 $form .= sprintf($input, 'server_os', PHP_OS
);
351 $form .= sprintf($input, 'is_superuser', $GLOBALS['dbi']->isSuperuser());
352 $form .= sprintf($input, 'server_db_isLocal', $ServerStatusData->db_isLocal
);
355 * Define some links used on client side
357 $links = '<div id="profiling_docu" class="hide">';
358 $links .= PMA_Util
::showMySQLDocu('general-thread-states');
360 $links .= '<div id="explain_docu" class="hide">';
361 $links .= PMA_Util
::showMySQLDocu('explain-output');
364 return $form . $links;
367 /***************************Ajax request function***********************************/
370 * Returns JSon for real-time charting data
374 function PMA_getJsonForChartingData()
376 $ret = json_decode($_REQUEST['requiredData'], true);
377 $statusVars = array();
378 $serverVars = array();
379 $sysinfo = $cpuload = $memory = 0;
381 /* Accumulate all required variables and data */
382 list($serverVars, $statusVars, $ret) = PMA_getJsonForChartingDataGet(
383 $ret, $serverVars, $statusVars, $sysinfo, $cpuload, $memory
386 // Retrieve all required status variables
387 if (count($statusVars)) {
388 $statusVarValues = $GLOBALS['dbi']->fetchResult(
389 "SHOW GLOBAL STATUS WHERE Variable_name='"
390 . implode("' OR Variable_name='", $statusVars) . "'",
395 $statusVarValues = array();
398 // Retrieve all required server variables
399 if (count($serverVars)) {
400 $serverVarValues = $GLOBALS['dbi']->fetchResult(
401 "SHOW GLOBAL VARIABLES WHERE Variable_name='"
402 . implode("' OR Variable_name='", $serverVars) . "'",
407 $serverVarValues = array();
410 // ...and now assign them
411 $ret = PMA_getJsonForChartingDataSet($ret, $statusVarValues, $serverVarValues);
413 $ret['x'] = microtime(true) * 1000;
418 * Assign the variables for real-time charting data
420 * @param array $ret Real-time charting data
421 * @param array $statusVarValues Status variable values
422 * @param array $serverVarValues Server variable values
426 function PMA_getJsonForChartingDataSet($ret, $statusVarValues, $serverVarValues)
428 foreach ($ret as $chart_id => $chartNodes) {
429 foreach ($chartNodes as $node_id => $nodeDataPoints) {
430 foreach ($nodeDataPoints as $point_id => $dataPoint) {
431 switch ($dataPoint['type']) {
433 $ret[$chart_id][$node_id][$point_id]['value']
434 = $statusVarValues[$dataPoint['name']];
437 $ret[$chart_id][$node_id][$point_id]['value']
438 = $serverVarValues[$dataPoint['name']];
448 * Get called to get JSON for charting data
450 * @param array $ret Real-time charting data
451 * @param array $serverVars Server variable values
452 * @param array $statusVars Status variable values
453 * @param mixed $sysinfo System info
454 * @param mixed $cpuload CPU load
455 * @param mixed $memory Memory
459 function PMA_getJsonForChartingDataGet(
460 $ret, $serverVars, $statusVars, $sysinfo, $cpuload, $memory
463 foreach ($ret as $chart_id => $chartNodes) {
464 // For each data series
465 foreach ($chartNodes as $node_id => $nodeDataPoints) {
466 // For each data point in the series (usually just 1)
467 foreach ($nodeDataPoints as $point_id => $dataPoint) {
468 list($serverVars, $statusVars, $ret[$chart_id][$node_id][$point_id])
469 = PMA_getJsonForChartingDataSwitch(
470 $dataPoint['type'], $dataPoint['name'], $serverVars,
471 $statusVars, $ret[$chart_id][$node_id][$point_id],
472 $sysinfo, $cpuload, $memory
477 return array($serverVars, $statusVars, $ret);
481 * Switch called to get JSON for charting data
483 * @param string $type Type
484 * @param string $pName Name
485 * @param array $serverVars Server variable values
486 * @param array $statusVars Status variable values
487 * @param array $ret Real-time charting data
488 * @param mixed $sysinfo System info
489 * @param mixed $cpuload CPU load
490 * @param mixed $memory Memory
494 function PMA_getJsonForChartingDataSwitch(
495 $type, $pName, $serverVars, $statusVars, $ret,
496 $sysinfo, $cpuload, $memory
499 /* We only collect the status and server variables here to
500 * read them all in one query,
501 * and only afterwards assign them.
502 * Also do some white list filtering on the names
505 if (!preg_match('/[^a-zA-Z_]+/', $pName)) {
506 $serverVars[] = $pName;
511 if (!preg_match('/[^a-zA-Z_]+/', $pName)) {
512 $statusVars[] = $pName;
517 $result = $GLOBALS['dbi']->query('SHOW PROCESSLIST');
518 $ret['value'] = $GLOBALS['dbi']->numRows($result);
523 include_once 'libraries/sysinfo.lib.php';
524 $sysinfo = PMA_getSysInfo();
527 $cpuload = $sysinfo->loadavg();
530 if (PMA_getSysInfoOs() == 'Linux') {
531 $ret['idle'] = $cpuload['idle'];
532 $ret['busy'] = $cpuload['busy'];
534 $ret['value'] = $cpuload['loadavg'];
541 include_once 'libraries/sysinfo.lib.php';
542 $sysinfo = PMA_getSysInfo();
545 $memory = $sysinfo->memory();
548 $ret['value'] = isset($memory[$pName]) ?
$memory[$pName] : 0;
552 return array($serverVars, $statusVars, $ret);
556 * Returns JSon for log data with type: slow
558 * @param int $start Unix Time: Start time for query
559 * @param int $end Unix Time: End time for query
563 function PMA_getJsonForLogDataTypeSlow($start, $end)
565 $query = 'SELECT start_time, user_host, ';
566 $query .= 'Sec_to_Time(Sum(Time_to_Sec(query_time))) as query_time, ';
567 $query .= 'Sec_to_Time(Sum(Time_to_Sec(lock_time))) as lock_time, ';
568 $query .= 'SUM(rows_sent) AS rows_sent, ';
569 $query .= 'SUM(rows_examined) AS rows_examined, db, sql_text, ';
570 $query .= 'COUNT(sql_text) AS \'#\' ';
571 $query .= 'FROM `mysql`.`slow_log` ';
572 $query .= 'WHERE start_time > FROM_UNIXTIME(' . $start . ') ';
573 $query .= 'AND start_time < FROM_UNIXTIME(' . $end . ') GROUP BY sql_text';
575 $result = $GLOBALS['dbi']->tryQuery($query);
577 $return = array('rows' => array(), 'sum' => array());
579 while ($row = $GLOBALS['dbi']->fetchAssoc($result)) {
580 $type = /*overload*/mb_strtolower(
581 /*overload*/mb_substr(
584 /*overload*/mb_strpos($row['sql_text'], ' ')
591 //Cut off big inserts and updates, but append byte count instead
592 if (/*overload*/mb_strlen($row['sql_text']) > 220) {
593 $implode_sql_text = implode(
595 PMA_Util
::formatByteDown(
596 /*overload*/mb_strlen($row['sql_text']), 2, 2
599 $row['sql_text'] = /*overload*/mb_substr($row['sql_text'], 0, 200)
600 . '... [' . $implode_sql_text . ']';
607 if (! isset($return['sum'][$type])) {
608 $return['sum'][$type] = 0;
610 $return['sum'][$type] +
= $row['#'];
611 $return['rows'][] = $row;
614 $return['sum']['TOTAL'] = array_sum($return['sum']);
615 $return['numRows'] = count($return['rows']);
617 $GLOBALS['dbi']->freeResult($result);
622 * Returns JSon for log data with type: general
624 * @param int $start Unix Time: Start time for query
625 * @param int $end Unix Time: End time for query
629 function PMA_getJsonForLogDataTypeGeneral($start, $end)
632 if (isset($_REQUEST['limitTypes']) && $_REQUEST['limitTypes']) {
634 = 'AND argument REGEXP \'^(INSERT|SELECT|UPDATE|DELETE)\' ';
637 $query = 'SELECT TIME(event_time) as event_time, user_host, thread_id, ';
638 $query .= 'server_id, argument, count(argument) as \'#\' ';
639 $query .= 'FROM `mysql`.`general_log` ';
640 $query .= 'WHERE command_type=\'Query\' ';
641 $query .= 'AND event_time > FROM_UNIXTIME(' . $start . ') ';
642 $query .= 'AND event_time < FROM_UNIXTIME(' . $end . ') ';
643 $query .= $limitTypes . 'GROUP by argument'; // HAVING count > 1';
645 $result = $GLOBALS['dbi']->tryQuery($query);
647 $return = array('rows' => array(), 'sum' => array());
648 $insertTables = array();
649 $insertTablesFirst = -1;
651 $removeVars = isset($_REQUEST['removeVariables'])
652 && $_REQUEST['removeVariables'];
654 while ($row = $GLOBALS['dbi']->fetchAssoc($result)) {
655 preg_match('/^(\w+)\s/', $row['argument'], $match);
656 $type = /*overload*/mb_strtolower($match[1]);
658 if (! isset($return['sum'][$type])) {
659 $return['sum'][$type] = 0;
661 $return['sum'][$type] +
= $row['#'];
664 /** @noinspection PhpMissingBreakStatementInspection */
666 // Group inserts if selected
669 '/^INSERT INTO (`|\'|"|)([^\s\\1]+)\\1/i',
670 $row['argument'], $matches
673 $insertTables[$matches[2]]++
;
674 if ($insertTables[$matches[2]] > 1) {
675 $return['rows'][$insertTablesFirst]['#']
676 = $insertTables[$matches[2]];
678 // Add a ... to the end of this query to indicate that
679 // there's been other queries
680 $temp = $return['rows'][$insertTablesFirst]['argument'];
681 $return['rows'][$insertTablesFirst]['argument']
682 .= PMA_getSuspensionPoints(
683 $temp[/*overload*/mb_strlen($temp) - 1]
686 // Group this value, thus do not add to the result list
689 $insertTablesFirst = $i;
690 $insertTables[$matches[2]] +
= $row['#'] - 1;
696 // Cut off big inserts and updates,
697 // but append byte count therefor
698 if (/*overload*/mb_strlen($row['argument']) > 220) {
699 $row['argument'] = /*overload*/mb_substr($row['argument'], 0, 200)
703 PMA_Util
::formatByteDown(
704 /*overload*/mb_strlen($row['argument']),
717 $return['rows'][] = $row;
721 $return['sum']['TOTAL'] = array_sum($return['sum']);
722 $return['numRows'] = count($return['rows']);
724 $GLOBALS['dbi']->freeResult($result);
730 * Return suspension points if needed
732 * @param string $lastChar Last char
734 * @return null|string Return suspension points if needed
736 function PMA_getSuspensionPoints($lastChar)
738 if ($lastChar != '.') {
745 * Returns JSon for logging vars
749 function PMA_getJsonForLoggingVars()
751 if (isset($_REQUEST['varName']) && isset($_REQUEST['varValue'])) {
752 $value = PMA_Util
::sqlAddSlashes($_REQUEST['varValue']);
753 if (! is_numeric($value)) {
754 $value="'" . $value . "'";
757 if (! preg_match("/[^a-zA-Z0-9_]+/", $_REQUEST['varName'])) {
758 $GLOBALS['dbi']->query(
759 'SET GLOBAL ' . $_REQUEST['varName'] . ' = ' . $value
765 $loggingVars = $GLOBALS['dbi']->fetchResult(
766 'SHOW GLOBAL VARIABLES WHERE Variable_name IN'
767 . ' ("general_log","slow_query_log","long_query_time","log_output")',
775 * Returns JSon for query_analyzer
779 function PMA_getJsonForQueryAnalyzer()
783 if (/*overload*/mb_strlen($_REQUEST['database'])) {
784 $GLOBALS['dbi']->selectDb($_REQUEST['database']);
787 if ($profiling = PMA_Util
::profilingSupported()) {
788 $GLOBALS['dbi']->query('SET PROFILING=1;');
791 // Do not cache query
792 $query = preg_replace(
798 $GLOBALS['dbi']->tryQuery($query);
799 $return['affectedRows'] = $GLOBALS['cached_affected_rows'];
801 $result = $GLOBALS['dbi']->tryQuery('EXPLAIN ' . $query);
802 while ($row = $GLOBALS['dbi']->fetchAssoc($result)) {
803 $return['explain'][] = $row;
806 // In case an error happened
807 $return['error'] = $GLOBALS['dbi']->getError();
809 $GLOBALS['dbi']->freeResult($result);
812 $return['profiling'] = array();
813 $result = $GLOBALS['dbi']->tryQuery(
814 'SELECT seq,state,duration FROM INFORMATION_SCHEMA.PROFILING'
815 . ' WHERE QUERY_ID=1 ORDER BY seq'
817 while ($row = $GLOBALS['dbi']->fetchAssoc($result)) {
818 $return['profiling'][]= $row;
820 $GLOBALS['dbi']->freeResult($result);