1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555
|
<?php
/**
* functions for displaying server status sub item: monitor
*/
declare(strict_types=1);
namespace PhpMyAdmin\Server\Status;
use PhpMyAdmin\DatabaseInterface;
use PhpMyAdmin\Profiling;
use PhpMyAdmin\Server\SysInfo\SysInfo;
use PhpMyAdmin\Util;
use function array_sum;
use function count;
use function implode;
use function is_numeric;
use function json_decode;
use function mb_strlen;
use function mb_strpos;
use function mb_strtolower;
use function mb_substr;
use function microtime;
use function preg_match;
use function preg_replace;
use function strlen;
/**
* functions for displaying server status sub item: monitor
*/
class Monitor
{
/** @var DatabaseInterface */
private $dbi;
/**
* @param DatabaseInterface $dbi DatabaseInterface instance
*/
public function __construct($dbi)
{
$this->dbi = $dbi;
}
/**
* Returns JSON for real-time charting data
*
* @param string $requiredData Required data
*
* @return array JSON
*/
public function getJsonForChartingData(string $requiredData): array
{
$ret = json_decode($requiredData, true);
$statusVars = [];
$serverVars = [];
$sysinfo = $cpuload = $memory = 0;
/* Accumulate all required variables and data */
[$serverVars, $statusVars, $ret] = $this->getJsonForChartingDataGet(
$ret,
$serverVars,
$statusVars,
$sysinfo,
$cpuload,
$memory
);
// Retrieve all required status variables
$statusVarValues = [];
if (count($statusVars)) {
$statusVarValues = $this->dbi->fetchResult(
"SHOW GLOBAL STATUS WHERE Variable_name='"
. implode("' OR Variable_name='", $statusVars) . "'",
0,
1
);
}
// Retrieve all required server variables
$serverVarValues = [];
if (count($serverVars)) {
$serverVarValues = $this->dbi->fetchResult(
"SHOW GLOBAL VARIABLES WHERE Variable_name='"
. implode("' OR Variable_name='", $serverVars) . "'",
0,
1
);
}
// ...and now assign them
$ret = $this->getJsonForChartingDataSet($ret, $statusVarValues, $serverVarValues);
$ret['x'] = (int) (microtime(true) * 1000);
return $ret;
}
/**
* Assign the variables for real-time charting data
*
* @param array $ret Real-time charting data
* @param array $statusVarValues Status variable values
* @param array $serverVarValues Server variable values
*
* @return array
*/
private function getJsonForChartingDataSet(
array $ret,
array $statusVarValues,
array $serverVarValues
): array {
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;
}
}
}
}
return $ret;
}
/**
* Get called to get JSON for charting data
*
* @param array $ret Real-time charting data
* @param array $serverVars Server variable values
* @param array $statusVars Status variable values
* @param mixed $sysinfo System info
* @param mixed $cpuload CPU load
* @param mixed $memory Memory
*
* @return array
*/
private function getJsonForChartingDataGet(
array $ret,
array $serverVars,
array $statusVars,
$sysinfo,
$cpuload,
$memory
) {
// For each chart
foreach ($ret as $chartId => $chartNodes) {
// For each data series
foreach ($chartNodes as $nodeId => $nodeDataPoints) {
// For each data point in the series (usually just 1)
foreach ($nodeDataPoints as $pointId => $dataPoint) {
[$serverVars, $statusVars, $ret[$chartId][$nodeId][$pointId]] = $this->getJsonForChartingDataSwitch(
$dataPoint['type'],
$dataPoint['name'],
$serverVars,
$statusVars,
$ret[$chartId][$nodeId][$pointId],
$sysinfo,
$cpuload,
$memory
);
} /* foreach */
} /* foreach */
}
return [
$serverVars,
$statusVars,
$ret,
];
}
/**
* Switch called to get JSON for charting data
*
* @param string $type Type
* @param string $pName Name
* @param array $serverVars Server variable values
* @param array $statusVars Status variable values
* @param array $ret Real-time charting data
* @param mixed $sysinfo System info
* @param mixed $cpuload CPU load
* @param mixed $memory Memory
*
* @return array
*/
private function getJsonForChartingDataSwitch(
$type,
$pName,
array $serverVars,
array $statusVars,
array $ret,
$sysinfo,
$cpuload,
$memory
) {
/**
* We only collect the status and server variables here to read them all in one query,
* and only afterwards assign them. Also do some allow list filtering on the names
*/
switch ($type) {
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 = $this->dbi->query('SHOW PROCESSLIST');
$ret['value'] = $result->numRows();
break;
case 'cpu':
if (! $sysinfo) {
$sysinfo = SysInfo::get();
}
if (! $cpuload) {
$cpuload = $sysinfo->loadavg();
}
if (SysInfo::getOs() === 'Linux') {
$ret['idle'] = $cpuload['idle'];
$ret['busy'] = $cpuload['busy'];
} else {
$ret['value'] = $cpuload['loadavg'];
}
break;
case 'memory':
if (! $sysinfo) {
$sysinfo = SysInfo::get();
}
if (! $memory) {
$memory = $sysinfo->memory();
}
$ret['value'] = $memory[$pName] ?? 0;
break;
}
return [
$serverVars,
$statusVars,
$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
*/
public function getJsonForLogDataTypeSlow(int $start, int $end): ?array
{
$query = 'SELECT start_time, user_host, ';
$query .= 'Sec_to_Time(Sum(Time_to_Sec(query_time))) as query_time, ';
$query .= 'Sec_to_Time(Sum(Time_to_Sec(lock_time))) as lock_time, ';
$query .= 'SUM(rows_sent) AS rows_sent, ';
$query .= 'SUM(rows_examined) AS rows_examined, db, sql_text, ';
$query .= 'COUNT(sql_text) AS \'#\' ';
$query .= 'FROM `mysql`.`slow_log` ';
$query .= 'WHERE start_time > FROM_UNIXTIME(' . $start . ') ';
// See: mode = ONLY_FULL_GROUP_BY
$query .= 'AND start_time < FROM_UNIXTIME(' . $end . ') GROUP BY start_time, user_host, db, sql_text';
$result = $this->dbi->tryQuery($query);
if ($result === false) {
return null;
}
$return = [
'rows' => [],
'sum' => [],
];
while ($row = $result->fetchAssoc()) {
$type = mb_strtolower(
mb_substr(
$row['sql_text'],
0,
(int) mb_strpos($row['sql_text'], ' ')
)
);
switch ($type) {
case 'insert':
case 'update':
//Cut off big inserts and updates, but append byte count instead
if (mb_strlen($row['sql_text']) > 220) {
$implodeSqlText = implode(
' ',
(array) Util::formatByteDown(
mb_strlen($row['sql_text']),
2,
2
)
);
$row['sql_text'] = mb_substr($row['sql_text'], 0, 200)
. '... [' . $implodeSqlText . ']';
}
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']);
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
* @param bool $isTypesLimited Whether to limit types or not
* @param bool $removeVariables Whether to remove variables or not
*/
public function getJsonForLogDataTypeGeneral(
int $start,
int $end,
bool $isTypesLimited,
bool $removeVariables
): ?array {
$limitTypes = '';
if ($isTypesLimited) {
$limitTypes = 'AND argument REGEXP \'^(INSERT|SELECT|UPDATE|DELETE)\' ';
}
$query = 'SELECT TIME(event_time) as event_time, user_host, thread_id, ';
$query .= 'server_id, argument, count(argument) as \'#\' ';
$query .= 'FROM `mysql`.`general_log` ';
$query .= 'WHERE command_type=\'Query\' ';
$query .= 'AND event_time > FROM_UNIXTIME(' . $start . ') ';
$query .= 'AND event_time < FROM_UNIXTIME(' . $end . ') ';
// See: mode = ONLY_FULL_GROUP_BY
$query .= $limitTypes . 'GROUP by event_time, user_host, thread_id, server_id, argument'; // HAVING count > 1';
$result = $this->dbi->tryQuery($query);
if ($result === false) {
return null;
}
$return = [
'rows' => [],
'sum' => [],
];
$insertTables = [];
$insertTablesFirst = -1;
$i = 0;
while ($row = $result->fetchAssoc()) {
preg_match('/^(\w+)\s/', $row['argument'], $match);
$type = mb_strtolower($match[1]);
if (! isset($return['sum'][$type])) {
$return['sum'][$type] = 0;
}
$return['sum'][$type] += $row['#'];
switch ($type) {
/** @noinspection PhpMissingBreakStatementInspection */
case 'insert':
// Group inserts if selected
if (
$removeVariables && preg_match(
'/^INSERT INTO (`|\'|"|)([^\s\\1]+)\\1/i',
$row['argument'],
$matches
)
) {
if (! isset($insertTables[$matches[2]])) {
$insertTables[$matches[2]] = 0;
}
$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'];
$return['rows'][$insertTablesFirst]['argument'] .= $this->getSuspensionPoints(
$temp[strlen($temp) - 1]
);
// Group this value, thus do not add to the result list
continue 2;
}
$insertTablesFirst = $i;
$insertTables[$matches[2]] += $row['#'] - 1;
}
// No break here
case 'update':
// Cut off big inserts and updates,
// but append byte count therefor
if (mb_strlen($row['argument']) > 220) {
$row['argument'] = mb_substr($row['argument'], 0, 200)
. '... ['
. implode(
' ',
(array) Util::formatByteDown(
mb_strlen($row['argument']),
2,
2
)
)
. ']';
}
break;
default:
break;
}
$return['rows'][] = $row;
$i++;
}
$return['sum']['TOTAL'] = array_sum($return['sum']);
$return['numRows'] = count($return['rows']);
return $return;
}
/**
* Return suspension points if needed
*
* @param string $lastChar Last char
*
* @return string Return suspension points if needed
*/
private function getSuspensionPoints(string $lastChar): string
{
if ($lastChar !== '.') {
return '<br>...';
}
return '';
}
/**
* Returns JSON for logging vars
*
* @param string|null $name Variable name
* @param string|null $value Variable value
*
* @return array JSON
*/
public function getJsonForLoggingVars(?string $name, ?string $value): array
{
if (isset($name, $value)) {
$escapedValue = $this->dbi->escapeString($value);
if (! is_numeric($escapedValue)) {
$escapedValue = "'" . $escapedValue . "'";
}
if (! preg_match('/[^a-zA-Z0-9_]+/', $name)) {
$this->dbi->query('SET GLOBAL ' . $name . ' = ' . $escapedValue);
}
}
return $this->dbi->fetchResult(
'SHOW GLOBAL VARIABLES WHERE Variable_name IN'
. ' ("general_log","slow_query_log","long_query_time","log_output")',
0,
1
);
}
/**
* Returns JSON for query_analyzer
*
* @param string $database Database name
* @param string $query SQL query
*
* @return array JSON
*/
public function getJsonForQueryAnalyzer(
string $database,
string $query
): array {
global $cached_affected_rows;
$return = [];
if (strlen($database) > 0) {
$this->dbi->selectDb($database);
}
$profiling = Profiling::isSupported($this->dbi);
if ($profiling) {
$this->dbi->query('SET PROFILING=1;');
}
// Do not cache query
$sqlQuery = preg_replace('/^(\s*SELECT)/i', '\\1 SQL_NO_CACHE', $query);
$this->dbi->tryQuery($sqlQuery);
$return['affectedRows'] = $cached_affected_rows;
$result = $this->dbi->tryQuery('EXPLAIN ' . $sqlQuery);
if ($result !== false) {
$return['explain'] = $result->fetchAllAssoc();
}
// In case an error happened
$return['error'] = $this->dbi->getError();
if ($profiling) {
$return['profiling'] = [];
$result = $this->dbi->tryQuery(
'SELECT seq,state,duration FROM INFORMATION_SCHEMA.PROFILING WHERE QUERY_ID=1 ORDER BY seq'
);
if ($result !== false) {
$return['profiling'] = $result->fetchAllAssoc();
}
}
return $return;
}
}
|