-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.php
More file actions
130 lines (94 loc) · 2.72 KB
/
Copy pathscript.php
File metadata and controls
130 lines (94 loc) · 2.72 KB
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
<?php
if(http_response_code() !== FALSE) {
die('CLI ONLY');
}
echo "Path to binlog?:";
$binlogPath = stream_get_line(STDIN, 1024, PHP_EOL);
if($binlogPath === false) {
die();
}
if(!file_exists($binlogPath)) {
die("File does not exist or is not readable");
}
$output = [];
exec('/usr/bin/mysqlbinlog --base64-output=decode-rows -vv ' . $binlogPath, $output);
$requests = [];
$table = null;
$query = null;
$linesParsed = 0;
foreach($output as $line) {
$matches = [];
if(preg_match('/^#[0-9]{6} .*Table_map: (.*) mapped/', $line, $matches)) {
$table = $matches[1];
$query = null;
$linesParsed++;
continue;
}
// Only recognize lines, when a table is found
if($table === null) {
continue;
}
// This a request line
if(substr($line, 0, 4) == '### ') {
if($query === null) {
$query = "";
}
$query .= $line . PHP_EOL;
$linesParsed++;
} else if($query !== null) {
// End of request found:
$requests[] = [
'table' => $table,
'query' => $query
];
$table = null;
$query = null;
}
}
echo $linesParsed . ' of ' . count($output) . ' lines parsed' . PHP_EOL . PHP_EOL;
$stat = [];
$countRequests = 0;
$strlenQueries = 0; // = String Length - Gives some information about the size of a query
foreach($requests as $request) {
$table = $request['table'];
$query = $request['query'];
if(!isset($stat[$table])) {
$stat[$table] = [
'countRequests' => 0,
'strlenQueries' => 0,
'requests' => []
];
}
$stat[$table]['countRequests']++;
$stat[$table]['strlenQueries']+= mb_strlen($query);
$stat[$table]['requests'][] = $request;
$countRequests++;
$strlenQueries += mb_strlen($query);
}
//ksort($stat, SORT_STRING);
$sortArray = [];
foreach($stat as $table => $data) {
$sortArray[] = $stat[$table]['strlenQueries'];
}
array_multisort($sortArray, SORT_NUMERIC, SORT_DESC, $stat);
$mask = "|%80s |%-15s |%-15s \n";
printf($mask, 'Table', 'count requests', 'strlen queries');
foreach($stat as $table => $data) {
printf(
$mask,
$table,
$data['countRequests'] . ' (' . ($countRequests == 0 ? 0 : round($data['countRequests']/$countRequests*100, 2)) . '%)',
$data['strlenQueries'] . ' (' . ($strlenQueries == 0 ? 0 : round($data['strlenQueries']/$strlenQueries*100, 2)) . '%)'
);
}
while(true) {
echo "Print specific database.table?:";
$table = stream_get_line(STDIN, 1024, PHP_EOL);
if($table === "")
break;
if(isset($stat[$table])) {
print_r($stat[$table]);
} else {
echo "Not found" . PHP_EOL;
}
}