-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsplitLog.php
More file actions
421 lines (387 loc) · 10.5 KB
/
splitLog.php
File metadata and controls
421 lines (387 loc) · 10.5 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
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
<?php
/****************************************************************************
This file is part of the slow query log filter.
Copyright (C) 2012 Thoronador
Copyright (C) 2012 Fizzban (minor modifications)
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
***************************************************************************/
// error constants that can be returned by the functions
define('SPLIT_ERROR_NONE', 0);
define('SPLIT_ERROR_NOT_EXISTS', 1);
define('SPLIT_ERROR_NOT_A_FILE', 2);
define('SPLIT_ERROR_TARGET_EXISTS', 3);
define('SPLIT_ERROR_IO', 4);
define('SPLIT_ERROR_NOT_SQL', 5);
define('SPLIT_ERROR_NO_QUERY', 6);
define('SPLIT_ERROR_EOF', 7);
/* returns a brief description of the error code's meaning
parameters:
code - (int) the error code, usually one of the constants above
*/
function errorCodeToString($code)
{
switch ((int)$code)
{
case SPLIT_ERROR_NONE:
return 'No error.';
case SPLIT_ERROR_NOT_EXISTS:
return 'Log file does not exist.';
case SPLIT_ERROR_NOT_A_FILE:
return 'Given path does not point to a file.';
case SPLIT_ERROR_TARGET_EXISTS:
return 'File with target name already exists.';
case SPLIT_ERROR_IO:
return 'I/O error.';
case SPLIT_ERROR_NOT_SQL:
return 'File does not seem to be the slow query log.';
case SPLIT_ERROR_NO_QUERY:
return 'No query listed after statistic data.';
case SPLIT_ERROR_EOF:
return 'Unexpected end of file.';
default:
return 'Unknown error code ('.intval($code).')';
}//swi
}//function errorCodeToString
//aux. function
function readQueryArray(&$file)
{
$result = array();
$nextLine = fgets($file);
if ($nextLine===false)
{
//unexpected end of file or I/O error
fclose($file);
return SPLIT_ERROR_IO;
}
//should be Time line (but does not have to)
if (substr($nextLine, 0, 7)==='# Time:')
{
$result['time'] = $nextLine;
//read user/host line
$nextLine = fgets($file);
}
if ($nextLine===false)
{
//unexpected end of file or I/O error
fclose($file);
return SPLIT_ERROR_IO;
}
//should be user/host line
if (substr($nextLine, 0, 12)!=='# User@Host:')
{
fclose($file);
return SPLIT_ERROR_NOT_SQL;
}
$result['user'] = $nextLine;
//read stats line
$nextLine = fgets($file);
if ($nextLine===false)
{
//unexpected end of file or I/O error
fclose($file);
return SPLIT_ERROR_IO;
}
//should be stats line
if (substr($nextLine, 0, 13)!=='# Query_time:')
{
fclose($file);
return SPLIT_ERROR_NOT_SQL;
}
$result['stats'] = $nextLine;
//prepare query lines
$result['query'] = array();
//read query lines
$pos = ftell($file); //save position for later fseek()
$go = true;
while($go)
{
$nextLine = fgets($file);
if ($nextLine===false)
{
//end of file or I/O error
if (feof($file))
{
return $result;
}
fclose($file);
return SPLIT_ERROR_IO;
}
if (substr($nextLine, 0, 2)!=='# ')
{
$result['query'][] = $nextLine;
$pos = ftell($file);
}
else
{
$go = false;
fseek($file, $pos); //skip back to start of current line
}
}//while
if (!empty($result['query'])) return $result;
//empty query - should not happen
return SPLIT_ERROR_NO_QUERY;
}//function readQueryArray
/* tries to get the user name from the given string, which has to be the line
starting with "# User@Host: ..." from the slow query log. Returns a string
containing the user name in case of success; returns false in case of
failure.
parameters:
userLine - (string) a line starting with "# User@Host: ..." from the log
*/
function getUserFromUserLine($userLine)
{
if (substr($userLine, 0, 12)!=='# User@Host:')
{
return false;
}
$userLine = trim(substr($userLine, 12));
$both = explode('@', $userLine);
if (count($both)===2) return trim($both[0]);
return false;
}//function getUserFromUserLine
/* "splits" a slow query log file by user, i.e. reads entries from origLog and
only saves the entries matching the user $user to the file newLog. Returns
zero in case of success or a non-zero int value in case of failure.
parameters:
origLog - (string) file name of the MySQL slow query log file
newLog - (string) name of the file where the matching log entries will
be saved
user - (string) name of the MySQL user that
returns:
Returns zero in case of success or a positive non-zero integer value
indicating the type of error in case of failure.
remarks:
If the file at newLog already exists, the function will fail and return
an error code, because we don't want to overwrite existing files.
You can use the errorCodeToString() function to get a brief description
of the error code's meaning.
*/
function splitLog($origLog, $newLog, $user)
{
$origLog = (string) $origLog;
$newLog = (string) $newLog;
$user = trim((string) $user);
//check existence
if (file_exists($origLog)===false)
{
return SPLIT_ERROR_NOT_EXISTS;
}
//only real files, no directories
if (is_file($origLog)===false)
{
return SPLIT_ERROR_NOT_A_FILE;
}
//we don't want to overwrite existing target files...
if (file_exists($newLog)===true)
{
return SPLIT_ERROR_TARGET_EXISTS;
}
//open slow query log file
$file = fopen($origLog, 'rb');
if ($file===false)
{
return SPLIT_ERROR_IO;
}
// ---- read starting lines
$nextLine = fgets($file);
if ($nextLine===false)
{
//unexpected end of file or I/O error
fclose($file);
return SPLIT_ERROR_IO;
}
//should be starting line
if (substr($nextLine, 0, 25)!=='/usr/sbin/mysqld, Version')
{
fclose($file);
return SPLIT_ERROR_NOT_SQL;
}
//read tcp line
$nextLine = fgets($file);
if ($nextLine===false)
{
//unexpected end of file or I/O error
fclose($file);
return SPLIT_ERROR_IO;
}
//should be tcp line
if (substr($nextLine, 0, 9)!=='Tcp port:')
{
fclose($file);
return SPLIT_ERROR_NOT_SQL;
}
// read Time line
$nextLine = fgets($file);
if ($nextLine===false)
{
//unexpected end of file or I/O error
fclose($file);
return SPLIT_ERROR_IO;
}
//should be Time line
if (substr($nextLine, 0, 5)!=='Time ')
{
fclose($file);
return SPLIT_ERROR_NOT_SQL;
}
//prepare output file
$target = fopen($newLog, 'wb');
if ($target===false)
{
fclose($file);
return SPLIT_ERROR_IO;
}
//now read the real log lines
do
{
$data = readQueryArray($file);
if (is_int($data))
{
fclose($target);
return $data;
}//if int
else
{
//check user
if (getUserFromUserLine($data['user'])===$user)
{
//write to target
fwrite($target, $data['time']);
fwrite($target, $data['user']);
fwrite($target, $data['stats']);
foreach($data['query'] as $ql)
{
fwrite($target, $ql);
}//foreach
}//if
}//else - it's an array
} while (!feof($file) && is_array($data));
fclose($target);
fclose($file);
if (is_int($data))
{
//error occured in do-while-loop
return $data;
}
//no error
return SPLIT_ERROR_NONE;
}//function splitLog
/* returns user distribution in given query log
parameters:
slowLog - (string) path to the slow query log file
returns:
If successful, the function returns an associative array. The keys of it
are the strings containing the user names, and the associated values are
integers that indicate the number of log entries for that user. It is
possible that the returned array is empty, but that should usually not
happen, unless the slow query log itself is empty, except for the three
leading lines.
If an error occured, the function returns an integer error code.
You can use the errorCodeToString() function to get a brief description
of the error code's meaning.
*/
function getLogUserStatistics($slowLog)
{
$slowLog = (string) $slowLog;
//check existence
if (file_exists($slowLog)===false)
{
return SPLIT_ERROR_NOT_EXISTS;
}
//only real files, no directories
if (is_file($slowLog)===false)
{
return SPLIT_ERROR_NOT_A_FILE;
}
//open slow query log file for reading
$file = fopen($slowLog, 'rb');
if ($file===false)
{
return SPLIT_ERROR_IO;
}
// ---- read starting lines
$nextLine = fgets($file);
if ($nextLine===false)
{
//unexpected end of file or I/O error
fclose($file);
return SPLIT_ERROR_IO;
}
//should be starting line
if (substr($nextLine, 0, 25)!=='/usr/sbin/mysqld, Version')
{
fclose($file);
return SPLIT_ERROR_NOT_SQL;
}
//read tcp line
$nextLine = fgets($file);
if ($nextLine===false)
{
//unexpected end of file or I/O error
fclose($file);
return SPLIT_ERROR_IO;
}
//should be tcp line
if (substr($nextLine, 0, 9)!=='Tcp port:')
{
fclose($file);
return SPLIT_ERROR_NOT_SQL;
}
// read Time line
$nextLine = fgets($file);
if ($nextLine===false)
{
//unexpected end of file or I/O error
fclose($file);
return SPLIT_ERROR_IO;
}
//should be Time line
if (substr($nextLine, 0, 5)!=='Time ')
{
fclose($file);
return SPLIT_ERROR_NOT_SQL;
}
//prepare statistics array
$stats = array();
//now read the real log lines
do
{
$data = readQueryArray($file);
if (is_int($data))
{
unset($stats);
return $data;
}//if int
else
{
//check user
$user = getUserFromUserLine($data['user']);
if ($user!==false)
{
if(isset($stats[$user]))
{
++$stats[$user];
}
else $stats[$user] = 1;
}//if
}//else - it's an array
} while (!feof($file) && is_array($data));
fclose($file);
if (is_int($data))
{
//error occured in do-while-loop
return $data;
}
//no error
return $stats;
}//function getLogUserStatistics
?>