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
|
/*
* Copyright (c) 2000,2003 Silicon Graphics, Inc. All Rights Reserved.
*
* 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 2 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.
*/
/*
* Uses the same regular expression logic as pmdaweblog, but extracted
* here so new patterns and access logs can be tested
*
* Usage: check_match configfile pat_name [input]
* configfile regex spec file as used by pmdaweblog
* pat_name use only this names regex from configfile
* input test input to try and match, defaults to stdin
*/
#include <ctype.h>
#include <pmapi.h>
#if defined(HAVE_REGEX_H)
#include <regex.h>
#endif
#include <sys/types.h>
int
main(int argc, char *argv[])
{
FILE *fc;
char buf[1024];
char *p;
char *q;
#ifdef HAVE_REGEX
char *comp = NULL;
char sub0[1024];
char sub1[1024];
char sub2[1024];
char sub3[1024];
#endif
int lno = 0;
int regex_posix = 0;
int cern_format = 0;
int common_extended_format = 0;
int squid_format = 0;
int methodpos = 1, c_statuspos = 2, sizepos = 2, s_statuspos = 2;
long client_cache_hits, proxy_cache_hits, remote_fetches;
double proxy_bytes, remote_bytes;
#if (defined HAVE_REGEXEC) && (defined HAVE_REGCOMP)
regex_t re = {0};
regmatch_t pmatch[5];
size_t nmatch = 5;
#endif
if (argc < 3 || argc > 4) {
fprintf(stderr, "Usage: check_match configfile pat_name [input]\n");
exit(1);
}
if ((fc = fopen(argv[1], "r")) == NULL) {
fprintf(stderr, "check_match: cannot open configfile \"%s\": %s\n", argv[1], osstrerror());
exit(1);
}
if (argc == 4) {
if (freopen(argv[3], "r", stdin) == NULL) {
fprintf(stderr, "check_match: cannot open input \"%s\": %s\n", argv[3], osstrerror());
exit(1);
}
}
while (fgets(buf, sizeof(buf), fc) != NULL) {
lno++;
if (strncmp(buf, "regex", 5) != 0) continue;
if (strncmp(buf, "regex_posix", 11) == 0) {
regex_posix = 1;
p = &buf[11];
}
else {
regex_posix = 0;
p = &buf[5];
}
while (*p && isspace((int)*p)) p++;
if (*p == '\0') continue;
q = p++;
while (*p && !isspace((int)*p)) p++;
if (*p == '\0') continue;
*p = '\0';
cern_format = squid_format = common_extended_format = 0;
if (strcmp(q, argv[2]) == 0) {
if(regex_posix) {
q = ++p;
while (*p && !isspace((int)*p)) p++;
if (*p == '\0') continue;
*p = '\0';
fprintf(stderr, "args are (%s)\n", q);
if(strncmp(q, "method,size", 11) == 0) {
cern_format = 1;
methodpos = 1;
sizepos = 2;
}
else if(strncmp(q, "size,method", 11) == 0) {
methodpos = 2;
sizepos = 1;
}
else {
char *str;
int pos;
pos = 1;
str=q;
do {
switch(str[0]) {
case '1':
methodpos = pos++;
break;
case '2':
sizepos = pos++;
break;
case '3':
c_statuspos = pos++;
break;
case '4':
s_statuspos = pos++;
break;
case '-':
methodpos = 1;
sizepos = 2;
str[0] = '\0';
break;
case ',':
case '\0':
break;
default:
fprintf(stderr,
"could figure out arg order params (%s)\n",
str);
exit(1);
}
} while ( *str++ );
if(c_statuspos > 0 && s_statuspos > 0) {
if(strcmp(argv[2], "SQUID") == 0)
squid_format = 1;
else
common_extended_format = 1;
} else
cern_format = 1;
}
fprintf(stderr, "cern: %d, cef: %d, squid: %d, MP: %d, SP: %d, CSP: %d, SSP: %d\n",
cern_format, common_extended_format, squid_format,
methodpos, sizepos, c_statuspos, s_statuspos);
}
q = ++p;
while (*p && *p != '\n') p++;
while (p >= q && isspace((int)*p)) p--;
p[1] = '\0';
if(regex_posix) {
#ifdef HAVE_REGCOMP
fprintf(stderr, "%s[%d]: regex_posix: %s\n", argv[1], lno, q);
fclose(fc);
if(regcomp(&re, q, REG_EXTENDED) != 0 ) {
fprintf(stderr, "Error: bad regular expression\n");
exit(1);
}
#else
fprintf(stderr, "%s[%d]: no support for POSIX regexp\n",
argv[1], lno);
#endif
}
else {
#ifdef HAVE_REGCMP
if(strcmp(argv[2], "CERN") == 0)
cern_format = 1;
else if (strcmp(argv[2], "NS_PROXY") == 0)
common_extended_format = 1;
else if (strcmp(argv[2], "SQUID") == 0)
squid_format = 1;
fprintf(stderr, "%s[%d]: regex: %s\n", argv[1], lno, q);
fclose(fc);
comp = regcmp(q, NULL);
if (comp == NULL) {
fprintf(stderr, "Error: bad regular expression\n");
exit(1);
}
#else
fprintf(stderr, "%s[%d]: regcmp is not available\n",
argv[1], lno);
#endif
}
break;
}
}
lno = 0;
remote_fetches = proxy_cache_hits = client_cache_hits = 0;
remote_bytes = proxy_bytes = 0.0;
while (fgets(buf, sizeof(buf), stdin) != NULL) {
lno++;
if(regex_posix) {
#ifdef HAVE_REGEXEC
if(regexec(&re, buf, nmatch, pmatch, 0) == 0) {
buf[pmatch[methodpos].rm_eo] = '\0';
buf[pmatch[sizepos].rm_eo] = '\0';
if(common_extended_format || squid_format) {
buf[pmatch[c_statuspos].rm_eo] = '\0';
buf[pmatch[s_statuspos].rm_eo] = '\0';
}
if(common_extended_format) {
fprintf(stderr,"[%d] M: %s, S: %s, CS: %s, SS: %s\n",
lno,
&buf[pmatch[methodpos].rm_so],
&buf[pmatch[sizepos].rm_so],
&buf[pmatch[c_statuspos].rm_so],
&buf[pmatch[s_statuspos].rm_so]);
if(strcmp(&buf[pmatch[c_statuspos].rm_so], "200") == 0 &&
strcmp(&buf[pmatch[s_statuspos].rm_so], "200") == 0) {
fprintf(stderr,"\tREMOTE fetch of %.0f bytes\n",
atof(&buf[pmatch[sizepos].rm_so]));
remote_fetches++;
remote_bytes += atof(&buf[pmatch[sizepos].rm_so]);
}
if(strcmp(&buf[pmatch[c_statuspos].rm_so], "200") == 0 &&
(strcmp(&buf[pmatch[s_statuspos].rm_so], "304") == 0 ||
strcmp(&buf[pmatch[s_statuspos].rm_so], "-") == 0)) {
fprintf(stderr,"\tCACHE return of %.0f bytes\n",
atof(&buf[pmatch[sizepos].rm_so]));
proxy_cache_hits++;
proxy_bytes += atof(&buf[pmatch[sizepos].rm_so]);
}
if(strcmp(&buf[pmatch[c_statuspos].rm_so], "304") == 0 &&
(strcmp(&buf[pmatch[s_statuspos].rm_so], "304") == 0 ||
strcmp(&buf[pmatch[s_statuspos].rm_so], "-") == 0)) {
fprintf(stderr,"\tCLIENT hit of %.0f bytes\n",
atof(&buf[pmatch[sizepos].rm_so]));
client_cache_hits++;
}
} else if(squid_format) {
fprintf(stderr,"[%d] M: %s, S: %s, CS: %s, SS: %s\n",
lno,
&buf[pmatch[methodpos].rm_so],
&buf[pmatch[sizepos].rm_so],
&buf[pmatch[c_statuspos].rm_so],
&buf[pmatch[s_statuspos].rm_so]);
if(strcmp(&buf[pmatch[c_statuspos].rm_so], "200") == 0 &&
(strstr(&buf[pmatch[s_statuspos].rm_so],
"_MISS")!=NULL ||
strstr(&buf[pmatch[s_statuspos].rm_so],
"_CLIENT_REFRESH")!=NULL ||
strstr(&buf[pmatch[s_statuspos].rm_so],
"_SWAPFAIL")!=NULL)){
fprintf(stderr,"\tREMOTE fetch of %.0f bytes (code: %s, Squid result code: %s)\n",
atof(&buf[pmatch[sizepos].rm_so]),
&buf[pmatch[c_statuspos].rm_so], &buf[pmatch[s_statuspos].rm_so]);
remote_fetches++;
remote_bytes += atof(&buf[pmatch[sizepos].rm_so]);
}
if(strcmp(&buf[pmatch[c_statuspos].rm_so], "200") == 0 &&
strstr(&buf[pmatch[s_statuspos].rm_so], "_HIT") != NULL) {
fprintf(stderr,"\tCACHE return of %.0f bytes (code: %s, Squid result code: %s)\n",
atof(&buf[pmatch[sizepos].rm_so]),
&buf[pmatch[c_statuspos].rm_so], &buf[pmatch[s_statuspos].rm_so]);
proxy_cache_hits++;
proxy_bytes += atof(&buf[pmatch[sizepos].rm_so]);
}
if(strcmp(&buf[pmatch[c_statuspos].rm_so], "304") == 0 &&
strstr(&buf[pmatch[s_statuspos].rm_so], "_HIT") != NULL) {
fprintf(stderr,"\tCLIENT hit of %.0f bytes (code: %s, Squid result code: %s)\n",
atof(&buf[pmatch[sizepos].rm_so]),
&buf[pmatch[c_statuspos].rm_so], &buf[pmatch[s_statuspos].rm_so]);
client_cache_hits++;
}
} else {
fprintf(stderr, "[%d] match: method=\"%s\" size=\"%s\"\n", lno,
&buf[pmatch[methodpos].rm_so], &buf[pmatch[sizepos].rm_so]);
}
}
else
fprintf(stderr, "[%d] no match: %s\n", lno, buf);
#else
fprintf(stderr, "[%d] - no regexec()\n", lno);
#endif
}
else {
#ifdef HAVE_REGEX
if (regex(comp, buf, sub0, sub1, sub2, sub3) != NULL) {
if(common_extended_format) {
fprintf(stderr,"[%d] M: %s, S: %s, CS: %s, SS: %s\n",
lno, sub0, sub1, sub2, sub3);
if(strcmp(sub2, "200") == 0 &&
strcmp(sub3, "200") == 0 ) {
fprintf(stderr,"\tREMOTE fetch of %s bytes\n", sub1);
remote_fetches++;
remote_bytes += atof(sub1);
}
if(strcmp(sub2, "200") == 0 &&
(strcmp(sub3, "304") == 0 || strcmp(sub3, "-") == 0)) {
fprintf(stderr,"\tCACHE return of %s bytes\n", sub1);
proxy_cache_hits++;
proxy_bytes += atof(sub1);
}
if(strcmp(sub2, "304") == 0 &&
(strcmp(sub3, "304") == 0 || strcmp(sub3, "-") == 0)) {
fprintf(stderr,"\tCLIENT hit of %s bytes\n", sub1);
client_cache_hits++;
}
} else if(squid_format) {
fprintf(stderr,"[%d] M: %s, S: %s, CS: %s, SS: %s\n",
lno, sub0, sub1, sub2, sub3);
if(strcmp(sub2, "200") == 0 &&
(strstr(sub3, "_MISS") != NULL ||
strstr(sub3, "_CLIENT_REFRESH")!= NULL ||
strstr(sub3, "_SWAPFAIL") != NULL)){
fprintf(stderr,"\tREMOTE fetch of %.0f bytes (code: %s, Squid result code: %s)\n",
atof(sub1),
sub2, sub3);
remote_fetches++;
remote_bytes += atof(sub1);
}
if(strcmp(sub2, "200") == 0 &&
strstr(sub3, "_HIT") != NULL) {
fprintf(stderr,"\tCACHE return of %.0f bytes (code: %s, Squid result code: %s)\n",
atof(sub1),
sub2, sub3);
proxy_cache_hits++;
proxy_bytes += atof(sub1);
}
if(strcmp(sub2, "304") == 0 &&
strstr(sub3, "_HIT") != NULL) {
fprintf(stderr,"\tCLIENT hit of %.0f bytes (code: %s, Squid result code: %s)\n",
atof(sub3),
sub2, sub3);
client_cache_hits++;
}
} else {
fprintf(stderr, "[%d] match: method=\"%s\" size=\"%s\"\n", lno,
sub0, sub1);
}
}
else
fprintf(stderr, "[%d] no match: %s\n", lno, buf);
#else
fprintf(stderr, "[%d] - no regex()\n", lno);
#endif
}
}
if(common_extended_format || squid_format) {
fprintf(stderr,"Proxy Cache Summary Report\n\n");
fprintf(stderr,
"# requests %ld\n# client cache hits %ld\n# cache hits %ld\n# remote fetches %ld\n",
(client_cache_hits + proxy_cache_hits + remote_fetches),
client_cache_hits, proxy_cache_hits, remote_fetches);
fprintf(stderr,
"\nTotal Mbytes %f bytes\nFrom proxy cache %f Mbytes\nFrom remote sites %f Mbytes\n\n",
(proxy_bytes + remote_bytes)/1000000.0,
proxy_bytes/1000000.0, remote_bytes/1000000.0);
fprintf(stderr,
"Client Cache %% hit rate: %.2f\n",
100.0*(float)client_cache_hits/(float)(client_cache_hits + proxy_cache_hits + remote_fetches));
fprintf(stderr,
"Proxy Cache %% hit rate: %.2f\n",
100.0*(float)proxy_cache_hits/(float)(client_cache_hits + proxy_cache_hits + remote_fetches));
fprintf(stderr,
"Local Cache %% hit rate: %.2f\n",
100.0*(float)(client_cache_hits + proxy_cache_hits)/
(float)(client_cache_hits + proxy_cache_hits + remote_fetches));
fprintf(stderr,
"\nAverage fetch size: Proxy -> Client: %.2f Kb\n",
proxy_bytes/proxy_cache_hits/1000.0);
fprintf(stderr,
"Average fetch size: Remote -> Client : %.2f Kb\n",
remote_bytes/remote_fetches/1000.0);
fprintf(stderr,"\nClient Cache bandwidth reduction effectiveness: UNKNOWN\n");
fprintf(stderr,
"Proxy Cache bandwidth reduction effectiveness: %f%%\n",
100.0*proxy_bytes/(proxy_bytes + remote_bytes));
}
exit(0);
}
|