From 10cbd4f5f93a6cb9bb01651891bb490e381f10be Mon Sep 17 00:00:00 2001 From: Oleguer Llopart Date: Fri, 27 Jul 2018 16:13:13 +0200 Subject: [PATCH] New: queryscan.php file --- queryscan.php | 729 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 729 insertions(+) create mode 100644 queryscan.php diff --git a/queryscan.php b/queryscan.php new file mode 100644 index 0000000..ac3fc63 --- /dev/null +++ b/queryscan.php @@ -0,0 +1,729 @@ + + + + + + With this script you can record the queries made during a period of time, graphically showing the slow queries. +

+ Scan Duration: s, Scan Interval: ms, One Pixel Equals: ms, Highlight Queries Longer: ms +
+ Consultations that last less than ms have the possibility of not registering, queryDuration / scanInterval * 100 = Percentage of possibilities to register +

+ + Successful connection to the database Unable to connect to the database + +

+ + The "CGI only" mode is activated, you can not run a scan or see the last scan, you can only see the scans that you upload from the form, you can disable "asdasd" mode by editing this file or you can use CLI instead (Command line) +

+ + + Start scan +

+ Or +

+
+ Upload previous scan: + +
+ + $trimQueries) + $query .= '... Cropped query'; + + if(isset($queries[$queryId]['queries'][$md5Query])) + { + $row = $queries[$queryId]; + $row['end'] = $loopTime; + $row['duration'] = $loopTime - $row['start']; + $row['realDuration'] = $scanIntervalSeconds + $row['realDuration']; + + $rowQueries = $row['queries'][$md5Query]; + + if(isset($previousQueries[$md5Query])) + { + $row['queries'][$md5Query]['executions'][$rowQueries['index']]['end'] = $loopTime; + } + else + { + $row['queries'][$md5Query]['executions'][] = array( + 'start' => $loopTime, + 'end' => $loopTime, + ); + $row['queries'][$md5Query]['index']++; + } + + $queries[$queryId] = $row; + } + else if(isset($queries[$queryId])) + { + $row = $queries[$queryId]; + $row['end'] = $loopTime; + $row['duration'] = $loopTime - $row['start']; + $row['realDuration'] = $scanIntervalSeconds + $row['realDuration']; + + $row['queries'][$md5Query] = array( + 'query' => $query, + 'index' => 0, + 'executions' => array( + array( + 'start' => $loopTime, + 'end' => $loopTime, + ) + ), + ); + + $queries[$queryId] = $row; + } + else + { + $row = array( + 'duration' => $scanIntervalSeconds, + 'realDuration' => $scanIntervalSeconds, + 'start' => $loopTime, + 'end' => $loopTime, + 'queries' => array( + $md5Query => array( + 'query' => $query, + 'index' => 0, + 'executions' => array( + array( + 'start' => $loopTime, + 'end' => $loopTime, + ) + ) + ) + ) + ); + + $queries[$queryId] = $row; + } + } + } + mysqli_free_result($result); + + $l++; + + // Activate in cloudflare or other CDNs to avoid a 504 error + if(!$cli) + echo ' '; + + // Check server load every $onePixelEquals ms + if($loopTime - $loadLastCheck > $onePixelEqualsSeconds) + { + $load[] =array( + 'time' => $loopTime, + 'load' => sys_getloadavg()[0], + ); + $l = 0; + ob_flush(); + $loadLastCheck = $loopTime; + } + + $previousQueries = $currentQueries; + + usleep($sleepDuration); + + if($cli) + { + $percent = round($i / $numberLoops * 100); + + if($percent < 10) + $percent = '0'.$percent; + + if(!isset($cliPercents[$percent])) + { + $seconds = round((($numberLoops - $i) * $scanInterval) / 1000); + + $hours = floor($seconds / 3600); + $minutes = floor($seconds / 60) - ($hours * 60); + $seconds = $seconds - ($hours * 60 * 60) - ($minutes * 60); + + echo $percent.'% - '.($hours ? $hours.'h ' : '').($minutes ? $minutes.'m ' : '').$seconds.'s left'.PHP_EOL; + + $cliPercents[$percent] = true; + } + } + } + + if($cli) + echo PHP_EOL.'Scan is finished'.PHP_EOL; + + $endTime = microtime(true); + + // Save the data in the file + if(!empty($saveIn)) + { + $gzip = gzencode(json_encode(array( + 'queries' => $queries, + 'load' => $load, + 'startTime' => $startTime, + 'endTime' => $endTime, + )), 6); + file_put_contents($saveIn, $gzip); + + // Change file permissions + chmod($saveIn, 0640); + } +} +else if((!empty($loadFrom) && file_exists($loadFrom) && !$cliOnly) || (isset($_FILES['file']['tmp_name']) && file_exists($_FILES['file']['tmp_name']))) +{ + if(isset($_FILES['file']['tmp_name']) && file_exists($_FILES['file']['tmp_name'])) + $file = $_FILES['file']['tmp_name']; + else + $file = $loadFrom; + + $json = gzdecode(file_get_contents($file)); + $data = json_decode($json, true); + + $queries = $data['queries']; + $load = $data['load']; + $startTime = $data['startTime']; + $endTime = $data['endTime']; +} + +if(!empty($queries) && !$cli) +{ + + ?> + + + + + + + + + +
+ +
+
+ + = 60) + { + echo '
'.date('H:i', $startTimeRound + $secondsMore).'
'; + + $seconds = 0; + } + else if($onePixelEquals <= 500) + { + echo ''; + } + + $seconds++; + $secondsMore++; + } + + ?> + +
+
+
+ + Roll over to see the load: +
+ $maxLoad) + $maxLoad = $row['load']; + } + + foreach($load as $row) + { + echo '
'; + } + + ?> +
+ +
+
+ + + + Each row is a connection to the database, press a row to see the queries, Pass over to see the duration: +
+ Queries Not Highlighted: green/blue, Highlighted Queries: red/orange +
+
+ + 1 ? $width : 1).'px; background-color: '.(($highlightQueriesLongerSeconds > $duration) ? ($color ? 'green' : 'blue') : ($color ? 'red' : 'orange')).';" title="hour: '.date('H:i:s', $e['start']).', duration: '.round($duration, 3).'">
'; + } + + $con .= '
'.$lines.'
'; + + $color = !$color; + } + + echo '
'.$con.'
'; + } + + ?> + +
+ +
+
+ +
Used memory: MB
+ + \ No newline at end of file