-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.rl
More file actions
472 lines (390 loc) · 11.7 KB
/
parser.rl
File metadata and controls
472 lines (390 loc) · 11.7 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
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
/*
* THIS C FILE GENERATED BY RAGEL - DO NOT EDIT!
* Ragel State Machine Compiler: http://www.complang.org/ragel/
* Source: http://www.complang.org/ragel/ragel-6.5.tar.gz
*/
%%{
machine parser;
access cmd->;
action set_year {
cmd->timestamp.tm_year = (((*(fpc - 3) - '0') * 1000) + ((*(fpc - 2) - '0') * 100) + ((*(fpc - 1) - '0') * 10) + (*(fpc - 0) - '0')) - 1900;
}
action set_month {
cmd->timestamp.tm_mon = (((*(fpc - 1) - '0') * 10) + (*(fpc - 0) - '0')) - 1;
}
action set_month_jan {
cmd->timestamp.tm_mon = 0;
}
action set_month_feb {
cmd->timestamp.tm_mon = 1;
}
action set_month_mar {
cmd->timestamp.tm_mon = 2;
}
action set_month_apr {
cmd->timestamp.tm_mon = 3;
}
action set_month_may {
cmd->timestamp.tm_mon = 4;
}
action set_month_jun {
cmd->timestamp.tm_mon = 5;
}
action set_month_jul {
cmd->timestamp.tm_mon = 6;
}
action set_month_aug {
cmd->timestamp.tm_mon = 7;
}
action set_month_sep {
cmd->timestamp.tm_mon = 8;
}
action set_month_oct {
cmd->timestamp.tm_mon = 9;
}
action set_month_nov {
cmd->timestamp.tm_mon = 10;
}
action set_month_dec {
cmd->timestamp.tm_mon = 11;
}
action set_day {
cmd->timestamp.tm_mday = ((*(fpc - 1) - '0') * 10) + (*(fpc - 0) - '0');
}
action set_hour {
cmd->timestamp.tm_hour = ((*(fpc - 1) - '0') * 10) + (*(fpc - 0) - '0');
}
action set_minute {
cmd->timestamp.tm_min = ((*(fpc - 1) - '0') * 10) + (*(fpc - 0) - '0');
}
action set_second {
cmd->timestamp.tm_sec = ((*(fpc - 1) - '0') * 10) + (*(fpc - 0) - '0');
}
action set_timezoneoffset {
int hours = ((*(fpc - 3) - '0') * 10) + (*(fpc - 2) - '0');
int minutes = ((*(fpc - 1) - '0') * 10) + (*(fpc - 0) - '0');
cmd->timestamp.tm_gmtoff = ((hours % 12) * 3600) + ((minutes % 60) * 60);
if (*(fpc - 4) == '-') {
cmd->timestamp.tm_gmtoff = -cmd->timestamp.tm_gmtoff;
}
}
action begin_timestamp {
timestamp = fpc;
cmd->timestamp.tm_gmtoff = 0;
}
action end_timestamp {
cmd->time = timegm(&cmd->timestamp);
if ((cmd->time < cmd->start_time) || ((cmd->time > cmd->stop_time))) {
cmd->time = 0;
}
}
action set_httpd_access_timestamp {
flag = 1; // match from start of timestamp to the end of text
}
action set_httpd_error_timestamp {
flag = 1; // match from start of timestamp to the end of text
}
action set_nmp2_timestamp {
flag = 0; // match from start of text to the end of text
}
action begin_text {
text = fpc;
}
action end_text {
if (cmd->time) {
int len = fpc + 1 - timestamp;
//fwrite(timestamp, 1, len, stdout);
if (match(cmd->pattern, flag ? timestamp : text)) {
if (cmd->format) {
if ((cmd->limit == 0) || ((cmd->marker >= cmd->skip) && (cmd->marker < cmd->skip + cmd->limit))) {
switch (cmd->format) {
case TEXTFORMAT:
if (fwrite(timestamp, 1, len, stdout) != len) {
fprintf(stderr, "logfile: could not write stdout\n");
result = 1;
fbreak;
}
break;
case XMLFORMAT:
{
char ch, *line = timestamp;
int i, c = 0, n = len - 1;
printf("\t<string>");
while (c < n) {
for (i = c; i < n; i++) {
if (line[i] == '<' || line[i] == '>' || line[i] == '&') {
break;
}
}
ch = line[i];
line[i] = '\0';
printf("%s", &line[c]);
if (ch == '<') {
printf("<");
}
else if (ch == '>') {
printf(">");
}
else if (ch == '&') {
printf("&");
}
line[i] = ch;
c = i + 1;
}
printf("</string>\n");
break;
}
}
}
}
else {
// if (cmd->idx_file) {
// if (fwrite(&cmd->offset, cmd->offset_size, 1, cmd->idx_file) != 1) {
// fprintf(stderr, "logfile: could not write idx file\n");
// result = 1;
// fbreak;
// }
// }
// if (cmd->log_file) {
// if (fwrite(timestamp, 1, len, cmd->log_file) != len) {
// fprintf(stderr, "logfile: could not write log file\n");
// result = 1;
// fbreak;
// }
// }
if (rec) {
cmd->index = (cmd->time - cmd->start_time) / cmd->value;
rec[cmd->index].count++;
}
}
cmd->marker++;
cmd->offset += len;
stat->totalBytes += len;
stat->totalLength++;
}
stat->totalSearchBytes += len;
stat->totalSearchLength++;
cmd->time = 0; // reset
}
}
year = ( digit digit digit digit ) @set_year;
month = ( digit digit ) @set_month;
month_jan = ( 'Jan' ) @set_month_jan;
month_feb = ( 'Feb' ) @set_month_feb;
month_mar = ( 'Mar' ) @set_month_mar;
month_apr = ( 'Apr' ) @set_month_apr;
month_may = ( 'May' ) @set_month_may;
month_jun = ( 'Jun' ) @set_month_jun;
month_jul = ( 'Jul' ) @set_month_jul;
month_aug = ( 'Aug' ) @set_month_aug;
month_sep = ( 'Sep' ) @set_month_sep;
month_oct = ( 'Oct' ) @set_month_oct;
month_nov = ( 'Nov' ) @set_month_nov;
month_dec = ( 'Dec' ) @set_month_dec;
month_all = ( month_jan | month_feb | month_mar | month_apr | month_may | month_jun |
month_jul | month_aug | month_sep | month_oct | month_nov | month_dec );
day = ( digit digit ) @set_day;
hour = ( digit digit ) @set_hour;
minute = ( digit digit ) @set_minute;
second = ( digit digit ) @set_second;
timezoneoffset = ( ( '+' | '-' ) digit digit digit digit ) @set_timezoneoffset;
ipv4 = ( digit+ '.' digit+ '.' digit+ '.' digit+ );
ip = ( ipv4 ); # Note: there is currently no support for ipv6
httpd_access_timestamp = ( ip ' - ' [^ ]+ ' [' day '/' month_all '/' year ':' hour ':' minute ':' second ' ' timezoneoffset ']' ) @set_httpd_access_timestamp;
httpd_error_timestamp = ( '[' [A-Z][a-z][a-z] ' ' month_all ' ' day ' ' hour ':' minute ':' second ' ' year ']' ) @set_httpd_error_timestamp;
nmp2_timestamp = ( year '-' month '-' day [ \t] hour ':' minute ':' second ) @set_nmp2_timestamp;
# Important Notice: log entries in httpd error_log without a timestamp - it is possible - are skipped
timestamp = ( httpd_access_timestamp | httpd_error_timestamp | nmp2_timestamp ) >begin_timestamp @end_timestamp;
line = timestamp [ \t] ( any* '\n' ) >begin_text @end_text;
main := line*;
}%%
%% write data noerror nofinal;
int parser_read(Statistics *stat, Record *rec, Command *cmd, const char *name)
{
char *text;
char *timestamp;
gzFile *file;
int flag;
int have = 0;
int result = 0;
cmd->time = 0; // reset
if ((file = gzopen(name, "rb"))) {
%% write init;
while (1) {
char *p, *pe, *data = cmd->buffer + have;
int len, space = cmd->buffer_size - have;
/* fprintf(stderr, "logfile: space: %i\n", space); */
if (space == 0) {
fprintf(stderr, "logfile: buffer out of space\n");
result = 1;
break;
}
len = gzread(file, data, space); // fread(data, 1, space, stdin);
/* fprintf(stderr, "logfile: len: %i\n", len); */
if (len == 0) {
/* fprintf(stderr, "logfile: end of file\n"); */
break;
}
/* Find the last newline by searching backwards. This is where
* we will stop processing on this iteration. */
p = cmd->buffer;
pe = cmd->buffer + have + len - 1;
while (*pe != '\n' && pe >= cmd->buffer) {
pe--;
}
pe += 1;
/* fprintf(stderr, "logfile: running on: %i\n", pe - p); */
%% write exec;
/* Have we reached or exceeded the limit. */
if (cmd->limit && (cmd->marker >= cmd->skip + cmd->limit)) {
have = 0; /* We need a controlled terminate. */
break;
}
/* How much is still in the buffer. */
have = data + len - pe;
if (have > 0) {
memmove(cmd->buffer, pe, have);
}
/* fprintf(stderr, "logfile: have: %i\n", have); */
if (len < space) {
/* fprintf(stderr, "logfile: end of file\n"); */
break;
}
}
if (have > 0) {
fprintf(stderr, "logfile: input not newline terminated\n");
result = 1;
}
gzclose(file);
}
return result;
}
int parser_test(Command *cmd, const char *name)
{
int head, tail;
time_t head_time, tail_time;
struct tm head_timestamp, tail_timestamp;
if (sscanf(name, cmd->name_format/*"%4d%2d%2d%2d%2d%2d-%4d%2d%2d%2d%2d%2d."*/,
&head_timestamp.tm_year, &head_timestamp.tm_mon, &head_timestamp.tm_mday,
&head_timestamp.tm_hour, &head_timestamp.tm_min, &head_timestamp.tm_sec,
&tail_timestamp.tm_year, &tail_timestamp.tm_mon, &tail_timestamp.tm_mday,
&tail_timestamp.tm_hour, &tail_timestamp.tm_min, &tail_timestamp.tm_sec) == 12) {
head_timestamp.tm_year -= 1900;
head_timestamp.tm_mon -= 1;
head_timestamp.tm_isdst = 0;
head_timestamp.tm_gmtoff = 0;
head_time = timegm(&head_timestamp);
tail_timestamp.tm_year -= 1900;
tail_timestamp.tm_mon -= 1;
tail_timestamp.tm_isdst = 0;
tail_timestamp.tm_gmtoff = 0;
tail_time = timegm(&tail_timestamp);
head = head_time >= cmd->start_time && head_time <= cmd->stop_time;
tail = tail_time >= cmd->start_time && tail_time <= cmd->stop_time;
return head | tail;
}
else {
int len = strlen(cmd->name);
if (len) {
if (cmd->name[len - 1] == '*') {
return strncmp(name, cmd->name, len - 1) == 0; // partial match OK in certain cases
}
else {
return strcmp(name, cmd->name) == 0; // exact match required to enforce name_format order
}
}
}
return 0;
}
int parser(Command *cmd)
{
struct dirent **dir;
int nentries, entry;
int result = 0;
if (chdir(cmd->dir) == 0) {
if (cmd->start_time && cmd->stop_time) {
if (cmd->start_time < cmd->stop_time) {
cmd->value = ((cmd->stop_time - cmd->start_time) / 86400) > 1 ? 86400 : 3600;
cmd->count = ((cmd->stop_time - cmd->start_time) / cmd->value) + 1;
Record *rec = malloc(cmd->count * sizeof(Record));
if (rec) {
for (cmd->index = 0; cmd->index < cmd->count; cmd->index++) {
reset_record(&rec[cmd->index]);
}
cmd->timestamp.tm_isdst = 0;
cmd->timestamp.tm_gmtoff = 0;
nentries = scandir(".", &dir, 0, alphasort);
if (nentries >= 0) {
Statistics stat;
stat.totalBytes = 0;
stat.totalLength = 0;
stat.totalSearchBytes = 0;
stat.totalSearchLength = 0;
if (cmd->format) {
// nothing
}
else {
print_begin_xml();
print_begin_files_xml();
}
start_clock(&stat);
for (entry = 0; entry < nentries; entry++) {
if (strcmp(dir[entry]->d_name, ".") == 0) {
goto free_dir_entry;
}
if (strcmp(dir[entry]->d_name, "..") == 0) {
goto free_dir_entry;
}
if (cmd->limit && (cmd->marker >= cmd->skip + cmd->limit)) {
goto free_dir_entry;
}
if (parser_test(cmd, dir[entry]->d_name)) {
if (cmd->format) {
// nothing
}
else {
print_file_xml(dir[entry]->d_name, cmd->marker);
}
/* fprintf(stderr, "logfile: %s\n", dir[entry]->d_name); */
if (result = parser_read(&stat, rec, cmd, dir[entry]->d_name)) {
break;
}
}
free_dir_entry:
free(dir[entry]);
}
stop_clock(&stat);
if (cmd->format) {
// nothing
}
else {
print_end_files_xml();
print_records_xml(rec, cmd);
print_statistics_xml(&stat);
print_end_xml();
}
free(dir);
}
free(rec);
}
else {
fprintf(stderr, "logfile: could not allocate record (%d)\n", cmd->count);
result = 1;
}
}
else {
fprintf(stderr, "logfile: start (%ld) is later than stop (%ld)\n", (long int)cmd->start_time, (long int)cmd->stop_time);
result = 1;
}
}
else {
fprintf(stderr, "logfile: start (%ld) stop (%ld) not specified\n", (long int)cmd->start_time, (long int)cmd->stop_time);
result = 1;
}
}
else {
fprintf(stderr, "logfile: could not change directory (%s)\n", cmd->dir);
result = 1;
}
return result;
}