Merge pull request #431 from xmujay/0609_monitor

refactor the Ajax request part of server_status_monitor.php
This commit is contained in:
Marc Delisle 2013-06-17 07:23:12 -07:00
commit 26bec29d1d
2 changed files with 392 additions and 330 deletions

View File

@ -210,7 +210,8 @@ function PMA_getHtmlForAddChartDialog()
$retval .= '<input type="radio" name="chartType" value="preset" id="chartPreset" />';
$retval .= '<label for="chartPreset">' . __('Preset chart') . '</label>';
$retval .= '<select name="presetCharts"></select><br/>';
$retval .= '<input type="radio" name="chartType" value="variable" id="chartStatusVar" checked="checked" />';
$retval .= '<input type="radio" name="chartType" value="variable" '
. 'id="chartStatusVar" checked="checked" />';
$retval .= '<label for="chartStatusVar">';
$retval .= __('Status variable(s)');
$retval .= '</label><br/>';
@ -400,4 +401,389 @@ function PMA_getHtmlForClientSideDataAndLinks($ServerStatusData)
return $form . $links;
}
/***************************Ajax request function***********************************/
/**
* Returns JSon for real-time charting data
*
* @return Array
*/
function PMA_getJsonForChartingData()
{
$ret = json_decode($_REQUEST['requiredData'], true);
$statusVars = array();
$serverVars = array();
$sysinfo = $cpuload = $memory = 0;
$pName = '';
/* Accumulate all required variables and data */
// For each chart
foreach ($ret as $chart_id => $chartNodes) {
// For each data series
foreach ($chartNodes as $node_id => $nodeDataPoints) {
// For each data point in the series (usually just 1)
foreach ($nodeDataPoints as $point_id => $dataPoint) {
$pName = $dataPoint['name'];
switch ($dataPoint['type']) {
/* We only collect the status and server variables here to
* read them all in one query,
* and only afterwards assign them.
* Also do some white list filtering on the names
*/
case 'servervar':
if (! preg_match('/[^a-zA-Z_]+/', $pName)) {
$serverVars[] = $pName;
}
break;
case 'statusvar':
if (! preg_match('/[^a-zA-Z_]+/', $pName)) {
$statusVars[] = $pName;
}
break;
case 'proc':
$result = $GLOBALS['dbi']->query('SHOW PROCESSLIST');
$ret[$chart_id][$node_id][$point_id]['value']
= $GLOBALS['dbi']->numRows($result);
break;
case 'cpu':
if (!$sysinfo) {
include_once 'libraries/sysinfo.lib.php';
$sysinfo = PMA_getSysInfo();
}
if (!$cpuload) {
$cpuload = $sysinfo->loadavg();
}
if (PMA_getSysInfoOs() == 'Linux') {
$ret[$chart_id][$node_id][$point_id]['idle']
= $cpuload['idle'];
$ret[$chart_id][$node_id][$point_id]['busy']
= $cpuload['busy'];
} else {
$ret[$chart_id][$node_id][$point_id]['value']
= $cpuload['loadavg'];
}
break;
case 'memory':
if (!$sysinfo) {
include_once 'libraries/sysinfo.lib.php';
$sysinfo = PMA_getSysInfo();
}
if (!$memory) {
$memory = $sysinfo->memory();
}
$ret[$chart_id][$node_id][$point_id]['value']
= $memory[$pName];
break;
} /* switch */
} /* foreach */
} /* foreach */
} /* foreach */
// Retrieve all required status variables
if (count($statusVars)) {
$statusVarValues = $GLOBALS['dbi']->fetchResult(
"SHOW GLOBAL STATUS WHERE Variable_name='"
. implode("' OR Variable_name='", $statusVars) . "'",
0,
1
);
} else {
$statusVarValues = array();
}
// Retrieve all required server variables
if (count($serverVars)) {
$serverVarValues = $GLOBALS['dbi']->fetchResult(
"SHOW GLOBAL VARIABLES WHERE Variable_name='"
. implode("' OR Variable_name='", $serverVars) . "'",
0,
1
);
} else {
$serverVarValues = array();
}
// ...and now assign them
foreach ($ret as $chart_id => $chartNodes) {
foreach ($chartNodes as $node_id => $nodeDataPoints) {
foreach ($nodeDataPoints as $point_id => $dataPoint) {
switch($dataPoint['type']) {
case 'statusvar':
$ret[$chart_id][$node_id][$point_id]['value']
= $statusVarValues[$dataPoint['name']];
break;
case 'servervar':
$ret[$chart_id][$node_id][$point_id]['value']
= $serverVarValues[$dataPoint['name']];
break;
}
}
}
}
$ret['x'] = microtime(true) * 1000;
return $ret;
}
/**
* Returns JSon for log data with type: slow
*
* @param int $start Unix Time: Start time for query
* @param int $end Unix Time: End time for query
*
* @return Array
*/
function PMA_getJsonForLogDataTypeSlow($start, $end)
{
$q = 'SELECT start_time, user_host, ';
$q .= 'Sec_to_Time(Sum(Time_to_Sec(query_time))) as query_time, ';
$q .= 'Sec_to_Time(Sum(Time_to_Sec(lock_time))) as lock_time, ';
$q .= 'SUM(rows_sent) AS rows_sent, ';
$q .= 'SUM(rows_examined) AS rows_examined, db, sql_text, ';
$q .= 'COUNT(sql_text) AS \'#\' ';
$q .= 'FROM `mysql`.`slow_log` ';
$q .= 'WHERE start_time > FROM_UNIXTIME(' . $start . ') ';
$q .= 'AND start_time < FROM_UNIXTIME(' . $end . ') GROUP BY sql_text';
$result = $GLOBALS['dbi']->tryQuery($q);
$return = array('rows' => array(), 'sum' => array());
$type = '';
while ($row = $GLOBALS['dbi']->fetchAssoc($result)) {
$type = strtolower(
substr($row['sql_text'], 0, strpos($row['sql_text'], ' '))
);
switch($type) {
case 'insert':
case 'update':
//Cut off big inserts and updates, but append byte count instead
if (strlen($row['sql_text']) > 220) {
$implode_sql_text = implode(
' ',
PMA_Util::formatByteDown(
strlen($row['sql_text']), 2, 2
)
);
$row['sql_text'] = substr($row['sql_text'], 0, 200)
. '... [' . $implode_sql_text . ']';
}
break;
default:
break;
}
if (! isset($return['sum'][$type])) {
$return['sum'][$type] = 0;
}
$return['sum'][$type] += $row['#'];
$return['rows'][] = $row;
}
$return['sum']['TOTAL'] = array_sum($return['sum']);
$return['numRows'] = count($return['rows']);
$GLOBALS['dbi']->freeResult($result);
return $return;
}
/**
* Returns JSon for log data with type: general
*
* @param int $start Unix Time: Start time for query
* @param int $end Unix Time: End time for query
*
* @return Array
*/
function PMA_getJsonForLogDataTypeGeneral($start, $end)
{
$limitTypes = '';
if (isset($_REQUEST['limitTypes']) && $_REQUEST['limitTypes']) {
$limitTypes
= 'AND argument REGEXP \'^(INSERT|SELECT|UPDATE|DELETE)\' ';
}
$q = 'SELECT TIME(event_time) as event_time, user_host, thread_id, ';
$q .= 'server_id, argument, count(argument) as \'#\' ';
$q .= 'FROM `mysql`.`general_log` ';
$q .= 'WHERE command_type=\'Query\' ';
$q .= 'AND event_time > FROM_UNIXTIME(' . $start . ') ';
$q .= 'AND event_time < FROM_UNIXTIME(' . $end . ') ';
$q .= $limitTypes . 'GROUP by argument'; // HAVING count > 1';
$result = $GLOBALS['dbi']->tryQuery($q);
$return = array('rows' => array(), 'sum' => array());
$type = '';
$insertTables = array();
$insertTablesFirst = -1;
$i = 0;
$removeVars = isset($_REQUEST['removeVariables'])
&& $_REQUEST['removeVariables'];
while ($row = $GLOBALS['dbi']->fetchAssoc($result)) {
preg_match('/^(\w+)\s/', $row['argument'], $match);
$type = strtolower($match[1]);
if (! isset($return['sum'][$type])) {
$return['sum'][$type] = 0;
}
$return['sum'][$type] += $row['#'];
switch($type) {
case 'insert':
// Group inserts if selected
if ($removeVars
&& preg_match(
'/^INSERT INTO (`|\'|"|)([^\s\\1]+)\\1/i',
$row['argument'], $matches
)
) {
$insertTables[$matches[2]]++;
if ($insertTables[$matches[2]] > 1) {
$return['rows'][$insertTablesFirst]['#']
= $insertTables[$matches[2]];
// Add a ... to the end of this query to indicate that
// there's been other queries
$temp = $return['rows'][$insertTablesFirst]['argument'];
if ($temp[strlen($temp) - 1] != '.') {
$return['rows'][$insertTablesFirst]['argument']
.= '<br/>...';
}
// Group this value, thus do not add to the result list
continue 2;
} else {
$insertTablesFirst = $i;
$insertTables[$matches[2]] += $row['#'] - 1;
}
}
// No break here
case 'update':
// Cut off big inserts and updates,
// but append byte count therefor
if (strlen($row['argument']) > 220) {
$row['argument'] = substr($row['argument'], 0, 200)
. '... ['
. implode(
' ',
PMA_Util::formatByteDown(
strlen($row['argument']),
2,
2
)
)
. ']';
}
break;
default:
break;
}
$return['rows'][] = $row;
$i++;
}
$return['sum']['TOTAL'] = array_sum($return['sum']);
$return['numRows'] = count($return['rows']);
$GLOBALS['dbi']->freeResult($result);
return $return;
}
/**
* Returns JSon for logging vars
*
* @return Array
*/
function PMA_getJsonForLoggingVars()
{
if (isset($_REQUEST['varName']) && isset($_REQUEST['varValue'])) {
$value = PMA_Util::sqlAddSlashes($_REQUEST['varValue']);
if (! is_numeric($value)) {
$value="'" . $value . "'";
}
if (! preg_match("/[^a-zA-Z0-9_]+/", $_REQUEST['varName'])) {
$GLOBALS['dbi']->query(
'SET GLOBAL ' . $_REQUEST['varName'] . ' = ' . $value
);
}
}
$loggingVars = $GLOBALS['dbi']->fetchResult(
'SHOW GLOBAL VARIABLES WHERE Variable_name IN'
. ' ("general_log","slow_query_log","long_query_time","log_output")',
0,
1
);
return $loggingVars;
}
/**
* Returns JSon for query_analyzer
*
* @return Array
*/
function PMA_getJsonForQueryAnalyzer()
{
$return = array();
if (strlen($_REQUEST['database'])) {
$GLOBALS['dbi']->selectDb($_REQUEST['database']);
}
if ($profiling = PMA_Util::profilingSupported()) {
$GLOBALS['dbi']->query('SET PROFILING=1;');
}
// Do not cache query
$query = preg_replace(
'/^(\s*SELECT)/i',
'\\1 SQL_NO_CACHE',
$_REQUEST['query']
);
$result = $GLOBALS['dbi']->tryQuery($query);
$return['affectedRows'] = $GLOBALS['cached_affected_rows'];
$result = $GLOBALS['dbi']->tryQuery('EXPLAIN ' . $query);
while ($row = $GLOBALS['dbi']->fetchAssoc($result)) {
$return['explain'][] = $row;
}
// In case an error happened
$return['error'] = $GLOBALS['dbi']->getError();
$GLOBALS['dbi']->freeResult($result);
if ($profiling) {
$return['profiling'] = array();
$result = $GLOBALS['dbi']->tryQuery(
'SELECT seq,state,duration FROM INFORMATION_SCHEMA.PROFILING'
. ' WHERE QUERY_ID=1 ORDER BY seq'
);
while ($row = $GLOBALS['dbi']->fetchAssoc($result)) {
$return['profiling'][]= $row;
}
$GLOBALS['dbi']->freeResult($result);
}
return $return;
}
?>

View File

@ -29,127 +29,7 @@ if (isset($_REQUEST['ajax_request']) && $_REQUEST['ajax_request'] == true) {
if (isset($_REQUEST['chart_data'])) {
switch($_REQUEST['type']) {
case 'chartgrid': // Data for the monitor
$ret = json_decode($_REQUEST['requiredData'], true);
$statusVars = array();
$serverVars = array();
$sysinfo = $cpuload = $memory = 0;
$pName = '';
/* Accumulate all required variables and data */
// For each chart
foreach ($ret as $chart_id => $chartNodes) {
// For each data series
foreach ($chartNodes as $node_id => $nodeDataPoints) {
// For each data point in the series (usually just 1)
foreach ($nodeDataPoints as $point_id => $dataPoint) {
$pName = $dataPoint['name'];
switch ($dataPoint['type']) {
/* We only collect the status and server variables here to
* read them all in one query,
* and only afterwards assign them.
* Also do some white list filtering on the names
*/
case 'servervar':
if (! preg_match('/[^a-zA-Z_]+/', $pName)) {
$serverVars[] = $pName;
}
break;
case 'statusvar':
if (! preg_match('/[^a-zA-Z_]+/', $pName)) {
$statusVars[] = $pName;
}
break;
case 'proc':
$result = $GLOBALS['dbi']->query('SHOW PROCESSLIST');
$ret[$chart_id][$node_id][$point_id]['value']
= $GLOBALS['dbi']->numRows($result);
break;
case 'cpu':
if (!$sysinfo) {
include_once 'libraries/sysinfo.lib.php';
$sysinfo = PMA_getSysInfo();
}
if (!$cpuload) {
$cpuload = $sysinfo->loadavg();
}
if (PMA_getSysInfoOs() == 'Linux') {
$ret[$chart_id][$node_id][$point_id]['idle']
= $cpuload['idle'];
$ret[$chart_id][$node_id][$point_id]['busy']
= $cpuload['busy'];
} else {
$ret[$chart_id][$node_id][$point_id]['value']
= $cpuload['loadavg'];
}
break;
case 'memory':
if (!$sysinfo) {
include_once 'libraries/sysinfo.lib.php';
$sysinfo = PMA_getSysInfo();
}
if (!$memory) {
$memory = $sysinfo->memory();
}
$ret[$chart_id][$node_id][$point_id]['value']
= $memory[$pName];
break;
} /* switch */
} /* foreach */
} /* foreach */
} /* foreach */
// Retrieve all required status variables
if (count($statusVars)) {
$statusVarValues = $GLOBALS['dbi']->fetchResult(
"SHOW GLOBAL STATUS WHERE Variable_name='"
. implode("' OR Variable_name='", $statusVars) . "'",
0,
1
);
} else {
$statusVarValues = array();
}
// Retrieve all required server variables
if (count($serverVars)) {
$serverVarValues = $GLOBALS['dbi']->fetchResult(
"SHOW GLOBAL VARIABLES WHERE Variable_name='"
. implode("' OR Variable_name='", $serverVars) . "'",
0,
1
);
} else {
$serverVarValues = array();
}
// ...and now assign them
foreach ($ret as $chart_id => $chartNodes) {
foreach ($chartNodes as $node_id => $nodeDataPoints) {
foreach ($nodeDataPoints as $point_id => $dataPoint) {
switch($dataPoint['type']) {
case 'statusvar':
$ret[$chart_id][$node_id][$point_id]['value']
= $statusVarValues[$dataPoint['name']];
break;
case 'servervar':
$ret[$chart_id][$node_id][$point_id]['value']
= $serverVarValues[$dataPoint['name']];
break;
}
}
}
}
$ret['x'] = microtime(true) * 1000;
$ret = PMA_getJsonForChartingData();
PMA_Response::getInstance()->addJSON('message', $ret);
exit;
}
@ -165,230 +45,26 @@ if (isset($_REQUEST['ajax_request']) && $_REQUEST['ajax_request'] == true) {
$end = intval($_REQUEST['time_end']);
if ($_REQUEST['type'] == 'slow') {
$q = 'SELECT start_time, user_host, ';
$q .= 'Sec_to_Time(Sum(Time_to_Sec(query_time))) as query_time, ';
$q .= 'Sec_to_Time(Sum(Time_to_Sec(lock_time))) as lock_time, ';
$q .= 'SUM(rows_sent) AS rows_sent, ';
$q .= 'SUM(rows_examined) AS rows_examined, db, sql_text, ';
$q .= 'COUNT(sql_text) AS \'#\' ';
$q .= 'FROM `mysql`.`slow_log` ';
$q .= 'WHERE start_time > FROM_UNIXTIME(' . $start . ') ';
$q .= 'AND start_time < FROM_UNIXTIME(' . $end . ') GROUP BY sql_text';
$result = $GLOBALS['dbi']->tryQuery($q);
$return = array('rows' => array(), 'sum' => array());
$type = '';
while ($row = $GLOBALS['dbi']->fetchAssoc($result)) {
$type = strtolower(
substr($row['sql_text'], 0, strpos($row['sql_text'], ' '))
);
switch($type) {
case 'insert':
case 'update':
//Cut off big inserts and updates, but append byte count instead
if (strlen($row['sql_text']) > 220) {
$implode_sql_text = implode(
' ',
PMA_Util::formatByteDown(
strlen($row['sql_text']), 2, 2
)
);
$row['sql_text'] = substr($row['sql_text'], 0, 200)
. '... [' . $implode_sql_text . ']';
}
break;
default:
break;
}
if (! isset($return['sum'][$type])) {
$return['sum'][$type] = 0;
}
$return['sum'][$type] += $row['#'];
$return['rows'][] = $row;
}
$return['sum']['TOTAL'] = array_sum($return['sum']);
$return['numRows'] = count($return['rows']);
$GLOBALS['dbi']->freeResult($result);
$return = PMA_getJsonForLogDataTypeSlow($start, $end);
PMA_Response::getInstance()->addJSON('message', $return);
exit;
}
if ($_REQUEST['type'] == 'general') {
$limitTypes = '';
if (isset($_REQUEST['limitTypes']) && $_REQUEST['limitTypes']) {
$limitTypes
= 'AND argument REGEXP \'^(INSERT|SELECT|UPDATE|DELETE)\' ';
}
$q = 'SELECT TIME(event_time) as event_time, user_host, thread_id, ';
$q .= 'server_id, argument, count(argument) as \'#\' ';
$q .= 'FROM `mysql`.`general_log` ';
$q .= 'WHERE command_type=\'Query\' ';
$q .= 'AND event_time > FROM_UNIXTIME(' . $start . ') ';
$q .= 'AND event_time < FROM_UNIXTIME(' . $end . ') ';
$q .= $limitTypes . 'GROUP by argument'; // HAVING count > 1';
$result = $GLOBALS['dbi']->tryQuery($q);
$return = array('rows' => array(), 'sum' => array());
$type = '';
$insertTables = array();
$insertTablesFirst = -1;
$i = 0;
$removeVars = isset($_REQUEST['removeVariables'])
&& $_REQUEST['removeVariables'];
while ($row = $GLOBALS['dbi']->fetchAssoc($result)) {
preg_match('/^(\w+)\s/', $row['argument'], $match);
$type = strtolower($match[1]);
if (! isset($return['sum'][$type])) {
$return['sum'][$type] = 0;
}
$return['sum'][$type] += $row['#'];
switch($type) {
case 'insert':
// Group inserts if selected
if ($removeVars
&& preg_match(
'/^INSERT INTO (`|\'|"|)([^\s\\1]+)\\1/i',
$row['argument'], $matches
)
) {
$insertTables[$matches[2]]++;
if ($insertTables[$matches[2]] > 1) {
$return['rows'][$insertTablesFirst]['#']
= $insertTables[$matches[2]];
// Add a ... to the end of this query to indicate that
// there's been other queries
$temp = $return['rows'][$insertTablesFirst]['argument'];
if ($temp[strlen($temp) - 1] != '.') {
$return['rows'][$insertTablesFirst]['argument']
.= '<br/>...';
}
// Group this value, thus do not add to the result list
continue 2;
} else {
$insertTablesFirst = $i;
$insertTables[$matches[2]] += $row['#'] - 1;
}
}
// No break here
case 'update':
// Cut off big inserts and updates,
// but append byte count therefor
if (strlen($row['argument']) > 220) {
$row['argument'] = substr($row['argument'], 0, 200)
. '... ['
. implode(
' ',
PMA_Util::formatByteDown(
strlen($row['argument']),
2,
2
)
)
. ']';
}
break;
default:
break;
}
$return['rows'][] = $row;
$i++;
}
$return['sum']['TOTAL'] = array_sum($return['sum']);
$return['numRows'] = count($return['rows']);
$GLOBALS['dbi']->freeResult($result);
$return = PMA_getJsonForLogDataTypeGeneral($start, $end);
PMA_Response::getInstance()->addJSON('message', $return);
exit;
}
}
if (isset($_REQUEST['logging_vars'])) {
if (isset($_REQUEST['varName']) && isset($_REQUEST['varValue'])) {
$value = PMA_Util::sqlAddSlashes($_REQUEST['varValue']);
if (! is_numeric($value)) {
$value="'" . $value . "'";
}
if (! preg_match("/[^a-zA-Z0-9_]+/", $_REQUEST['varName'])) {
$GLOBALS['dbi']->query(
'SET GLOBAL ' . $_REQUEST['varName'] . ' = ' . $value
);
}
}
$loggingVars = $GLOBALS['dbi']->fetchResult(
'SHOW GLOBAL VARIABLES WHERE Variable_name IN'
. ' ("general_log","slow_query_log","long_query_time","log_output")',
0,
1
);
$loggingVars = PMA_getJsonForLoggingVars();
PMA_Response::getInstance()->addJSON('message', $loggingVars);
exit;
}
if (isset($_REQUEST['query_analyzer'])) {
$return = array();
if (strlen($_REQUEST['database'])) {
$GLOBALS['dbi']->selectDb($_REQUEST['database']);
}
if ($profiling = PMA_Util::profilingSupported()) {
$GLOBALS['dbi']->query('SET PROFILING=1;');
}
// Do not cache query
$query = preg_replace(
'/^(\s*SELECT)/i',
'\\1 SQL_NO_CACHE',
$_REQUEST['query']
);
$result = $GLOBALS['dbi']->tryQuery($query);
$return['affectedRows'] = $GLOBALS['cached_affected_rows'];
$result = $GLOBALS['dbi']->tryQuery('EXPLAIN ' . $query);
while ($row = $GLOBALS['dbi']->fetchAssoc($result)) {
$return['explain'][] = $row;
}
// In case an error happened
$return['error'] = $GLOBALS['dbi']->getError();
$GLOBALS['dbi']->freeResult($result);
if ($profiling) {
$return['profiling'] = array();
$result = $GLOBALS['dbi']->tryQuery(
'SELECT seq,state,duration FROM INFORMATION_SCHEMA.PROFILING'
. ' WHERE QUERY_ID=1 ORDER BY seq'
);
while ($row = $GLOBALS['dbi']->fetchAssoc($result)) {
$return['profiling'][]= $row;
}
$GLOBALS['dbi']->freeResult($result);
}
$return = PMA_getJsonForQueryAnalyzer();
PMA_Response::getInstance()->addJSON('message', $return);
exit;
}