blob: 448e5c0a8300ca80436c302368bb3400130c9446 [file] [log] [blame]
Willy Tarreau72c28532009-01-22 18:56:50 +01001/*
Willy Tarreaud8fc1102010-09-12 17:56:16 +02002 * haproxy log statistics reporter
Willy Tarreau72c28532009-01-22 18:56:50 +01003 *
Willy Tarreaud2201062010-05-27 18:17:30 +02004 * Copyright 2000-2010 Willy Tarreau <w@1wt.eu>
Willy Tarreau72c28532009-01-22 18:56:50 +01005 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
Willy Tarreau72c28532009-01-22 18:56:50 +010013#include <errno.h>
14#include <fcntl.h>
15#include <stdio.h>
16#include <stdlib.h>
17#include <syslog.h>
18#include <string.h>
19#include <unistd.h>
20#include <ctype.h>
21
Willy Tarreau45cb4fb2009-10-26 21:10:04 +010022#include <eb32tree.h>
Willy Tarreauabe45b62010-10-28 20:33:46 +020023#include <eb64tree.h>
24#include <ebistree.h>
Willy Tarreaud2201062010-05-27 18:17:30 +020025#include <ebsttree.h>
Willy Tarreau72c28532009-01-22 18:56:50 +010026
Willy Tarreaud2201062010-05-27 18:17:30 +020027#define SOURCE_FIELD 5
Willy Tarreau72c28532009-01-22 18:56:50 +010028#define ACCEPT_FIELD 6
Willy Tarreaud2201062010-05-27 18:17:30 +020029#define SERVER_FIELD 8
Willy Tarreau72c28532009-01-22 18:56:50 +010030#define TIME_FIELD 9
31#define STATUS_FIELD 10
Willy Tarreaud8fc1102010-09-12 17:56:16 +020032#define TERM_CODES_FIELD 14
Willy Tarreau72c28532009-01-22 18:56:50 +010033#define CONN_FIELD 15
Willy Tarreau08911ff2011-10-13 13:28:36 +020034#define QUEUE_LEN_FIELD 16
Willy Tarreauabe45b62010-10-28 20:33:46 +020035#define METH_FIELD 17
36#define URL_FIELD 18
Willy Tarreau72c28532009-01-22 18:56:50 +010037#define MAXLINE 16384
38#define QBITS 4
39
Willy Tarreaudf6f0d12011-07-10 18:15:08 +020040#define SEP(c) ((unsigned char)(c) <= ' ')
41#define SKIP_CHAR(p,c) do { while (1) { int __c = (unsigned char)*p++; if (__c == c) break; if (__c <= ' ') { p--; break; } } } while (0)
Willy Tarreau72c28532009-01-22 18:56:50 +010042
43/* [0] = err/date, [1] = req, [2] = conn, [3] = resp, [4] = data */
44static struct eb_root timers[5] = {
45 EB_ROOT_UNIQUE, EB_ROOT_UNIQUE, EB_ROOT_UNIQUE,
46 EB_ROOT_UNIQUE, EB_ROOT_UNIQUE,
47};
48
49struct timer {
50 struct eb32_node node;
51 unsigned int count;
52};
53
Willy Tarreaud2201062010-05-27 18:17:30 +020054struct srv_st {
55 unsigned int st_cnt[6]; /* 0xx to 5xx */
56 unsigned int nb_ct, nb_rt, nb_ok;
57 unsigned long long cum_ct, cum_rt;
58 struct ebmb_node node;
59 /* don't put anything else here, the server name will be there */
60};
Willy Tarreau72c28532009-01-22 18:56:50 +010061
Willy Tarreauabe45b62010-10-28 20:33:46 +020062struct url_stat {
63 union {
64 struct ebpt_node url;
65 struct eb64_node val;
66 } node;
67 char *url;
68 unsigned long long total_time; /* sum(all reqs' times) */
69 unsigned long long total_time_ok; /* sum(all OK reqs' times) */
70 unsigned int nb_err, nb_req;
71};
72
Willy Tarreau72c28532009-01-22 18:56:50 +010073#define FILT_COUNT_ONLY 0x01
74#define FILT_INVERT 0x02
75#define FILT_QUIET 0x04
76#define FILT_ERRORS_ONLY 0x08
77#define FILT_ACC_DELAY 0x10
78#define FILT_ACC_COUNT 0x20
79#define FILT_GRAPH_TIMERS 0x40
Willy Tarreau214c2032009-02-20 11:02:32 +010080#define FILT_PERCENTILE 0x80
Willy Tarreau5bdfd962009-10-14 15:16:29 +020081#define FILT_TIME_RESP 0x100
82
83#define FILT_INVERT_ERRORS 0x200
84#define FILT_INVERT_TIME_RESP 0x400
Willy Tarreau72c28532009-01-22 18:56:50 +010085
Willy Tarreau0f423a72010-05-03 10:50:54 +020086#define FILT_COUNT_STATUS 0x800
Willy Tarreaud2201062010-05-27 18:17:30 +020087#define FILT_COUNT_SRV_STATUS 0x1000
Willy Tarreaud8fc1102010-09-12 17:56:16 +020088#define FILT_COUNT_TERM_CODES 0x2000
Willy Tarreau0f423a72010-05-03 10:50:54 +020089
Willy Tarreauabe45b62010-10-28 20:33:46 +020090#define FILT_COUNT_URL_ONLY 0x004000
91#define FILT_COUNT_URL_COUNT 0x008000
92#define FILT_COUNT_URL_ERR 0x010000
93#define FILT_COUNT_URL_TTOT 0x020000
94#define FILT_COUNT_URL_TAVG 0x040000
95#define FILT_COUNT_URL_TTOTO 0x080000
96#define FILT_COUNT_URL_TAVGO 0x100000
97#define FILT_COUNT_URL_ANY (FILT_COUNT_URL_ONLY|FILT_COUNT_URL_COUNT|FILT_COUNT_URL_ERR| \
98 FILT_COUNT_URL_TTOT|FILT_COUNT_URL_TAVG|FILT_COUNT_URL_TTOTO|FILT_COUNT_URL_TAVGO)
99
Willy Tarreau70c428f2011-07-10 17:27:40 +0200100#define FILT_HTTP_ONLY 0x200000
Willy Tarreaud3007ff2011-09-05 02:07:23 +0200101#define FILT_TERM_CODE_NAME 0x400000
Hervé COMMOWICK927cddd2011-08-10 17:42:41 +0200102#define FILT_INVERT_TERM_CODE_NAME 0x800000
Willy Tarreau70c428f2011-07-10 17:27:40 +0200103
Willy Tarreaud3007ff2011-09-05 02:07:23 +0200104#define FILT_HTTP_STATUS 0x1000000
105#define FILT_INVERT_HTTP_STATUS 0x2000000
Willy Tarreau08911ff2011-10-13 13:28:36 +0200106#define FILT_QUEUE_ONLY 0x4000000
107#define FILT_QUEUE_SRV_ONLY 0x8000000
Willy Tarreaud3007ff2011-09-05 02:07:23 +0200108
Willy Tarreau72c28532009-01-22 18:56:50 +0100109unsigned int filter = 0;
110unsigned int filter_invert = 0;
Willy Tarreau214c2032009-02-20 11:02:32 +0100111const char *line;
Willy Tarreaua2b39fb2011-07-10 21:39:35 +0200112int linenum = 0;
113int parse_err = 0;
114int lines_out = 0;
Willy Tarreau72c28532009-01-22 18:56:50 +0100115
Willy Tarreau214c2032009-02-20 11:02:32 +0100116const char *fgets2(FILE *stream);
Willy Tarreau72c28532009-01-22 18:56:50 +0100117
Willy Tarreaua2b39fb2011-07-10 21:39:35 +0200118void filter_count_url(const char *accept_field, const char *time_field, struct timer **tptr);
119void filter_count_srv_status(const char *accept_field, const char *time_field, struct timer **tptr);
120void filter_count_term_codes(const char *accept_field, const char *time_field, struct timer **tptr);
121void filter_count_status(const char *accept_field, const char *time_field, struct timer **tptr);
122void filter_graphs(const char *accept_field, const char *time_field, struct timer **tptr);
123void filter_output_line(const char *accept_field, const char *time_field, struct timer **tptr);
124void filter_accept_holes(const char *accept_field, const char *time_field, struct timer **tptr);
125
Willy Tarreau615674c2012-01-23 08:15:51 +0100126void usage(FILE *output, const char *msg)
Willy Tarreau72c28532009-01-22 18:56:50 +0100127{
Willy Tarreau615674c2012-01-23 08:15:51 +0100128 fprintf(output,
Willy Tarreau72c28532009-01-22 18:56:50 +0100129 "%s"
Willy Tarreau615674c2012-01-23 08:15:51 +0100130 "Usage: halog [-h|--help] for long help\n"
131 " halog [-q] [-c] [-v] {-gt|-pct|-st|-tc|-srv|-u|-uc|-ue|-ua|-ut|-uao|-uto}\n"
Hervé COMMOWICK927cddd2011-08-10 17:42:41 +0200132 " [-s <skip>] [-e|-E] [-H] [-rt|-RT <time>] [-ad <delay>] [-ac <count>]\n"
Willy Tarreau08911ff2011-10-13 13:28:36 +0200133 " [-Q|-QS] [-tcn|-TCN <termcode>] [ -hs|-HS [min][:[max]] ] < log\n"
Willy Tarreau72c28532009-01-22 18:56:50 +0100134 "\n",
135 msg ? msg : ""
136 );
Willy Tarreau615674c2012-01-23 08:15:51 +0100137}
138
139void die(const char *msg)
140{
141 usage(stderr, msg);
Willy Tarreau72c28532009-01-22 18:56:50 +0100142 exit(1);
143}
144
Willy Tarreau615674c2012-01-23 08:15:51 +0100145void help()
146{
147 usage(stdout, NULL);
148 printf(
149 "Input filters (several filters may be combined) :\n"
150 " -H only match lines containing HTTP logs (ignore TCP)\n"
151 " -E only match lines without any error (no 5xx status)\n"
152 " -e only match lines with errors (status 5xx or negative)\n"
153 " -rt|-RT <time> only match response times larger|smaller than <time>\n"
154 " -Q|-QS only match queued requests (any queue|server queue)\n"
155 " -tcn|-TCN <code> only match requests with/without termination code <code>\n"
156 " -hs|-HS <[min][:][max]> only match requests with HTTP status codes within/not\n"
157 " within min..max. Any of them may be omitted. Exact\n"
158 " code is checked for if no ':' is specified.\n"
159 "Modifiers\n"
160 " -v invert the input filtering condition\n"
161 " -q don't report errors/warnings\n"
162 "\n"
163 "Output filters - only one may be used at a time\n"
164 " -c only report the number of lines that would have been printed\n"
165 " -pct output connect and response times percentiles\n"
166 " -st output number of requests per HTTP status code\n"
167 " -tc output number of requests per termination code (2 chars)\n"
168 " -srv output statistics per server (time, requests, errors)\n"
169 " -u* output statistics per URL (time, requests, errors)\n"
170 " Additional characters indicate the output sorting key :\n"
171 " -u : by URL, -uc : request count, -ue : error count\n"
172 " -ua : average response time, -uto : average total time\n"
173 " -uao, -uto: average times computed on valid ('OK') requests\n"
174 );
175 exit(0);
176}
177
Willy Tarreau72c28532009-01-22 18:56:50 +0100178
179/* return pointer to first char not part of current field starting at <p>. */
Willy Tarreauf9042062011-09-10 12:26:35 +0200180
181#if defined(__i386__)
182/* this one is always faster on 32-bits */
183static inline const char *field_stop(const char *p)
184{
185 asm(
186 /* Look for spaces */
187 "4: \n\t"
188 "inc %0 \n\t"
189 "cmpb $0x20, -1(%0) \n\t"
190 "ja 4b \n\t"
191 "jz 3f \n\t"
192
193 /* we only get there for control chars 0..31. Leave if we find '\0' */
194 "cmpb $0x0, -1(%0) \n\t"
195 "jnz 4b \n\t"
196
197 /* return %0-1 = position of the last char we checked */
198 "3: \n\t"
199 "dec %0 \n\t"
200 : "=r" (p)
201 : "0" (p)
202 );
203 return p;
204}
205#else
Willy Tarreau72c28532009-01-22 18:56:50 +0100206const char *field_stop(const char *p)
207{
208 unsigned char c;
209
210 while (1) {
211 c = *(p++);
212 if (c > ' ')
213 continue;
Willy Tarreau14389e72011-07-10 22:11:17 +0200214 if (c == ' ' || c == 0)
Willy Tarreau72c28532009-01-22 18:56:50 +0100215 break;
216 }
217 return p - 1;
218}
Willy Tarreauf9042062011-09-10 12:26:35 +0200219#endif
Willy Tarreau72c28532009-01-22 18:56:50 +0100220
221/* return field <field> (starting from 1) in string <p>. Only consider
222 * contiguous spaces (or tabs) as one delimiter. May return pointer to
223 * last char if field is not found. Equivalent to awk '{print $field}'.
224 */
225const char *field_start(const char *p, int field)
226{
Willy Tarreauf9042062011-09-10 12:26:35 +0200227#ifndef PREFER_ASM
Willy Tarreau72c28532009-01-22 18:56:50 +0100228 unsigned char c;
229 while (1) {
230 /* skip spaces */
231 while (1) {
Willy Tarreauf9042062011-09-10 12:26:35 +0200232 c = *(p++);
Willy Tarreau72c28532009-01-22 18:56:50 +0100233 if (c > ' ')
234 break;
Willy Tarreau14389e72011-07-10 22:11:17 +0200235 if (c == ' ')
Willy Tarreauf9042062011-09-10 12:26:35 +0200236 continue;
Willy Tarreau72c28532009-01-22 18:56:50 +0100237 if (!c) /* end of line */
Willy Tarreauf9042062011-09-10 12:26:35 +0200238 return p-1;
Willy Tarreau72c28532009-01-22 18:56:50 +0100239 /* other char => new field */
240 break;
Willy Tarreau72c28532009-01-22 18:56:50 +0100241 }
242
243 /* start of field */
244 field--;
245 if (!field)
Willy Tarreauf9042062011-09-10 12:26:35 +0200246 return p-1;
Willy Tarreau72c28532009-01-22 18:56:50 +0100247
248 /* skip this field */
249 while (1) {
250 c = *(p++);
Willy Tarreau14389e72011-07-10 22:11:17 +0200251 if (c == ' ')
Willy Tarreau72c28532009-01-22 18:56:50 +0100252 break;
Willy Tarreauf9042062011-09-10 12:26:35 +0200253 if (c > ' ')
254 continue;
Willy Tarreau72c28532009-01-22 18:56:50 +0100255 if (c == '\0')
Willy Tarreauf9042062011-09-10 12:26:35 +0200256 return p - 1;
Willy Tarreau72c28532009-01-22 18:56:50 +0100257 }
258 }
Willy Tarreauf9042062011-09-10 12:26:35 +0200259#else
260 /* This version works optimally on i386 and x86_64 but the code above
261 * shows similar performance. However, depending on the version of GCC
262 * used, inlining rules change and it may have difficulties to make
263 * efficient use of this code at other locations and could result in
264 * worse performance (eg: gcc 4.4). You may want to experience.
265 */
266 asm(
267 /* skip spaces */
268 "1: \n\t"
269 "inc %0 \n\t"
270 "cmpb $0x20, -1(%0) \n\t"
271 "ja 2f \n\t"
272 "jz 1b \n\t"
273
274 /* we only get there for control chars 0..31. Leave if we find '\0' */
275 "cmpb $0x0, -1(%0) \n\t"
276 "jz 3f \n\t"
277
278 /* start of field at [%0-1]. Check if we need to skip more fields */
279 "2: \n\t"
280 "dec %1 \n\t"
281 "jz 3f \n\t"
282
283 /* Look for spaces */
284 "4: \n\t"
285 "inc %0 \n\t"
286 "cmpb $0x20, -1(%0) \n\t"
287 "jz 1b \n\t"
288 "ja 4b \n\t"
289
290 /* we only get there for control chars 0..31. Leave if we find '\0' */
291 "cmpb $0x0, -1(%0) \n\t"
292 "jnz 4b \n\t"
293
294 /* return %0-1 = position of the last char we checked */
295 "3: \n\t"
296 "dec %0 \n\t"
297 : "=r" (p)
298 : "r" (field), "0" (p)
299 );
300 return p;
301#endif
Willy Tarreau72c28532009-01-22 18:56:50 +0100302}
303
304/* keep only the <bits> higher bits of <i> */
305static inline unsigned int quantify_u32(unsigned int i, int bits)
306{
307 int high;
308
309 if (!bits)
310 return 0;
311
312 if (i)
313 high = fls_auto(i); // 1 to 32
314 else
315 high = 0;
316
317 if (high <= bits)
318 return i;
319
320 return i & ~((1 << (high - bits)) - 1);
321}
322
323/* keep only the <bits> higher bits of the absolute value of <i>, as well as
324 * its sign. */
325static inline int quantify(int i, int bits)
326{
327 if (i >= 0)
328 return quantify_u32(i, bits);
329 else
330 return -quantify_u32(-i, bits);
331}
332
333/* Insert timer value <v> into tree <r>. A pre-allocated node must be passed
334 * in <alloc>. It may be NULL, in which case the function will allocate it
335 * itself. It will be reset to NULL once consumed. The caller is responsible
336 * for freeing the node once not used anymore. The node where the value was
337 * inserted is returned.
338 */
339struct timer *insert_timer(struct eb_root *r, struct timer **alloc, int v)
340{
341 struct timer *t = *alloc;
342 struct eb32_node *n;
343
344 if (!t) {
345 t = calloc(sizeof(*t), 1);
346 if (unlikely(!t)) {
347 fprintf(stderr, "%s: not enough memory\n", __FUNCTION__);
348 exit(1);
349 }
350 }
351 t->node.key = quantify(v, QBITS); // keep only the higher QBITS bits
352
353 n = eb32i_insert(r, &t->node);
354 if (n == &t->node)
355 t = NULL; /* node inserted, will malloc next time */
356
357 *alloc = t;
358 return container_of(n, struct timer, node);
359}
360
361/* Insert value value <v> into tree <r>. A pre-allocated node must be passed
362 * in <alloc>. It may be NULL, in which case the function will allocate it
363 * itself. It will be reset to NULL once consumed. The caller is responsible
364 * for freeing the node once not used anymore. The node where the value was
365 * inserted is returned.
366 */
367struct timer *insert_value(struct eb_root *r, struct timer **alloc, int v)
368{
369 struct timer *t = *alloc;
370 struct eb32_node *n;
371
372 if (!t) {
373 t = calloc(sizeof(*t), 1);
374 if (unlikely(!t)) {
375 fprintf(stderr, "%s: not enough memory\n", __FUNCTION__);
376 exit(1);
377 }
378 }
379 t->node.key = v;
380
381 n = eb32i_insert(r, &t->node);
382 if (n == &t->node)
383 t = NULL; /* node inserted, will malloc next time */
384
385 *alloc = t;
386 return container_of(n, struct timer, node);
387}
388
389int str2ic(const char *s)
390{
391 int i = 0;
392 int j, k;
393
394 if (*s != '-') {
395 /* positive number */
396 while (1) {
397 j = (*s++) - '0';
398 k = i * 10;
399 if ((unsigned)j > 9)
400 break;
401 i = k + j;
402 }
403 } else {
404 /* negative number */
405 s++;
406 while (1) {
407 j = (*s++) - '0';
408 k = i * 10;
409 if ((unsigned)j > 9)
410 break;
411 i = k - j;
412 }
413 }
414
415 return i;
416}
417
418
419/* Equivalent to strtoul with a length. */
420static inline unsigned int __strl2ui(const char *s, int len)
421{
422 unsigned int i = 0;
423 while (len-- > 0) {
424 i = i * 10 - '0';
425 i += (unsigned char)*s++;
426 }
427 return i;
428}
429
430unsigned int strl2ui(const char *s, int len)
431{
432 return __strl2ui(s, len);
433}
434
435/* Convert "[04/Dec/2008:09:49:40.555]" to an integer equivalent to the time of
436 * the day in milliseconds. It returns -1 for all unparsable values. The parser
437 * looks ugly but gcc emits far better code that way.
438 */
439int convert_date(const char *field)
440{
441 unsigned int h, m, s, ms;
442 unsigned char c;
443 const char *b, *e;
444
445 h = m = s = ms = 0;
446 e = field;
447
448 /* skip the date */
449 while (1) {
450 c = *(e++);
451 if (c == ':')
452 break;
453 if (!c)
454 goto out_err;
455 }
456
457 /* hour + ':' */
458 b = e;
459 while (1) {
460 c = *(e++) - '0';
461 if (c > 9)
462 break;
463 h = h * 10 + c;
464 }
465 if (c == (unsigned char)(0 - '0'))
466 goto out_err;
467
468 /* minute + ':' */
469 b = e;
470 while (1) {
471 c = *(e++) - '0';
472 if (c > 9)
473 break;
474 m = m * 10 + c;
475 }
476 if (c == (unsigned char)(0 - '0'))
477 goto out_err;
478
479 /* second + '.' or ']' */
480 b = e;
481 while (1) {
482 c = *(e++) - '0';
483 if (c > 9)
484 break;
485 s = s * 10 + c;
486 }
487 if (c == (unsigned char)(0 - '0'))
488 goto out_err;
489
490 /* if there's a '.', we have milliseconds */
491 if (c == (unsigned char)('.' - '0')) {
492 /* millisecond second + ']' */
493 b = e;
494 while (1) {
495 c = *(e++) - '0';
496 if (c > 9)
497 break;
498 ms = ms * 10 + c;
499 }
500 if (c == (unsigned char)(0 - '0'))
501 goto out_err;
502 }
503 return (((h * 60) + m) * 60 + s) * 1000 + ms;
504 out_err:
505 return -1;
506}
507
508void truncated_line(int linenum, const char *line)
509{
510 if (!(filter & FILT_QUIET))
511 fprintf(stderr, "Truncated line %d: %s\n", linenum, line);
512}
513
514int main(int argc, char **argv)
515{
Willy Tarreau26deaf52011-07-10 19:47:48 +0200516 const char *b, *e, *p, *time_field, *accept_field;
Hervé COMMOWICK927cddd2011-08-10 17:42:41 +0200517 const char *filter_term_code_name = NULL;
Willy Tarreau72c28532009-01-22 18:56:50 +0100518 const char *output_file = NULL;
Willy Tarreaua2b39fb2011-07-10 21:39:35 +0200519 int f, last, err;
520 struct timer *t = NULL;
Willy Tarreau72c28532009-01-22 18:56:50 +0100521 struct eb32_node *n;
Willy Tarreauabe45b62010-10-28 20:33:46 +0200522 struct url_stat *ustat = NULL;
Willy Tarreau72c28532009-01-22 18:56:50 +0100523 int val, test;
Willy Tarreau72c28532009-01-22 18:56:50 +0100524 int filter_acc_delay = 0, filter_acc_count = 0;
Willy Tarreau5bdfd962009-10-14 15:16:29 +0200525 int filter_time_resp = 0;
Willy Tarreaud3007ff2011-09-05 02:07:23 +0200526 int filt_http_status_low = 0, filt_http_status_high = 0;
Willy Tarreau72c28532009-01-22 18:56:50 +0100527 int skip_fields = 1;
528
Willy Tarreaua2b39fb2011-07-10 21:39:35 +0200529 void (*line_filter)(const char *accept_field, const char *time_field, struct timer **tptr) = NULL;
530
Willy Tarreau72c28532009-01-22 18:56:50 +0100531 argc--; argv++;
532 while (argc > 0) {
533 if (*argv[0] != '-')
534 break;
535
536 if (strcmp(argv[0], "-ad") == 0) {
537 if (argc < 2) die("missing option for -ad");
538 argc--; argv++;
539 filter |= FILT_ACC_DELAY;
540 filter_acc_delay = atol(*argv);
541 }
542 else if (strcmp(argv[0], "-ac") == 0) {
543 if (argc < 2) die("missing option for -ac");
544 argc--; argv++;
545 filter |= FILT_ACC_COUNT;
546 filter_acc_count = atol(*argv);
547 }
Willy Tarreau5bdfd962009-10-14 15:16:29 +0200548 else if (strcmp(argv[0], "-rt") == 0) {
549 if (argc < 2) die("missing option for -rt");
550 argc--; argv++;
551 filter |= FILT_TIME_RESP;
552 filter_time_resp = atol(*argv);
553 }
554 else if (strcmp(argv[0], "-RT") == 0) {
555 if (argc < 2) die("missing option for -RT");
556 argc--; argv++;
557 filter |= FILT_TIME_RESP | FILT_INVERT_TIME_RESP;
558 filter_time_resp = atol(*argv);
559 }
Willy Tarreau72c28532009-01-22 18:56:50 +0100560 else if (strcmp(argv[0], "-s") == 0) {
561 if (argc < 2) die("missing option for -s");
562 argc--; argv++;
563 skip_fields = atol(*argv);
564 }
565 else if (strcmp(argv[0], "-e") == 0)
566 filter |= FILT_ERRORS_ONLY;
Willy Tarreau5bdfd962009-10-14 15:16:29 +0200567 else if (strcmp(argv[0], "-E") == 0)
568 filter |= FILT_ERRORS_ONLY | FILT_INVERT_ERRORS;
Willy Tarreau70c428f2011-07-10 17:27:40 +0200569 else if (strcmp(argv[0], "-H") == 0)
570 filter |= FILT_HTTP_ONLY;
Willy Tarreau08911ff2011-10-13 13:28:36 +0200571 else if (strcmp(argv[0], "-Q") == 0)
572 filter |= FILT_QUEUE_ONLY;
573 else if (strcmp(argv[0], "-QS") == 0)
574 filter |= FILT_QUEUE_SRV_ONLY;
Willy Tarreau72c28532009-01-22 18:56:50 +0100575 else if (strcmp(argv[0], "-c") == 0)
576 filter |= FILT_COUNT_ONLY;
577 else if (strcmp(argv[0], "-q") == 0)
578 filter |= FILT_QUIET;
579 else if (strcmp(argv[0], "-v") == 0)
580 filter_invert = !filter_invert;
581 else if (strcmp(argv[0], "-gt") == 0)
582 filter |= FILT_GRAPH_TIMERS;
Willy Tarreau214c2032009-02-20 11:02:32 +0100583 else if (strcmp(argv[0], "-pct") == 0)
584 filter |= FILT_PERCENTILE;
Willy Tarreau0f423a72010-05-03 10:50:54 +0200585 else if (strcmp(argv[0], "-st") == 0)
586 filter |= FILT_COUNT_STATUS;
Willy Tarreaud2201062010-05-27 18:17:30 +0200587 else if (strcmp(argv[0], "-srv") == 0)
588 filter |= FILT_COUNT_SRV_STATUS;
Willy Tarreaud8fc1102010-09-12 17:56:16 +0200589 else if (strcmp(argv[0], "-tc") == 0)
590 filter |= FILT_COUNT_TERM_CODES;
Hervé COMMOWICK927cddd2011-08-10 17:42:41 +0200591 else if (strcmp(argv[0], "-tcn") == 0) {
592 if (argc < 2) die("missing option for -tcn");
593 argc--; argv++;
594 filter |= FILT_TERM_CODE_NAME;
595 filter_term_code_name = *argv;
596 }
597 else if (strcmp(argv[0], "-TCN") == 0) {
598 if (argc < 2) die("missing option for -TCN");
599 argc--; argv++;
600 filter |= FILT_TERM_CODE_NAME | FILT_INVERT_TERM_CODE_NAME;
601 filter_term_code_name = *argv;
602 }
Willy Tarreaud3007ff2011-09-05 02:07:23 +0200603 else if (strcmp(argv[0], "-hs") == 0 || strcmp(argv[0], "-HS") == 0) {
604 char *sep, *str;
605
606 if (argc < 2) die("missing option for -hs/-HS ([min]:[max])");
607 filter |= FILT_HTTP_STATUS;
608 if (argv[0][1] == 'H')
609 filter |= FILT_INVERT_HTTP_STATUS;
610
611 argc--; argv++;
612 str = *argv;
613 sep = strchr(str, ':'); /* [min]:[max] */
614 if (!sep)
615 sep = str; /* make max point to min */
616 else
617 *sep++ = 0;
618 filt_http_status_low = *str ? atol(str) : 0;
619 filt_http_status_high = *sep ? atol(sep) : 65535;
620 }
Willy Tarreauabe45b62010-10-28 20:33:46 +0200621 else if (strcmp(argv[0], "-u") == 0)
622 filter |= FILT_COUNT_URL_ONLY;
623 else if (strcmp(argv[0], "-uc") == 0)
624 filter |= FILT_COUNT_URL_COUNT;
625 else if (strcmp(argv[0], "-ue") == 0)
626 filter |= FILT_COUNT_URL_ERR;
627 else if (strcmp(argv[0], "-ua") == 0)
628 filter |= FILT_COUNT_URL_TAVG;
629 else if (strcmp(argv[0], "-ut") == 0)
630 filter |= FILT_COUNT_URL_TTOT;
631 else if (strcmp(argv[0], "-uao") == 0)
632 filter |= FILT_COUNT_URL_TAVGO;
633 else if (strcmp(argv[0], "-uto") == 0)
634 filter |= FILT_COUNT_URL_TTOTO;
Willy Tarreau72c28532009-01-22 18:56:50 +0100635 else if (strcmp(argv[0], "-o") == 0) {
636 if (output_file)
637 die("Fatal: output file name already specified.\n");
638 if (argc < 2)
639 die("Fatal: missing output file name.\n");
640 output_file = argv[1];
641 }
Willy Tarreau615674c2012-01-23 08:15:51 +0100642 else if (strcmp(argv[0], "-h") == 0 || strcmp(argv[0], "--help") == 0)
643 help();
Willy Tarreau72c28532009-01-22 18:56:50 +0100644 argc--;
645 argv++;
646 }
647
648 if (!filter)
649 die("No action specified.\n");
650
651 if (filter & FILT_ACC_COUNT && !filter_acc_count)
652 filter_acc_count=1;
653
654 if (filter & FILT_ACC_DELAY && !filter_acc_delay)
655 filter_acc_delay = 1;
656
Willy Tarreaua2b39fb2011-07-10 21:39:35 +0200657
658 /* by default, all lines are printed */
659 line_filter = filter_output_line;
660 if (filter & (FILT_ACC_COUNT|FILT_ACC_DELAY))
661 line_filter = filter_accept_holes;
662 else if (filter & (FILT_GRAPH_TIMERS|FILT_PERCENTILE))
663 line_filter = filter_graphs;
664 else if (filter & FILT_COUNT_STATUS)
665 line_filter = filter_count_status;
666 else if (filter & FILT_COUNT_TERM_CODES)
667 line_filter = filter_count_term_codes;
668 else if (filter & FILT_COUNT_SRV_STATUS)
669 line_filter = filter_count_srv_status;
670 else if (filter & FILT_COUNT_URL_ANY)
671 line_filter = filter_count_url;
672 else if (filter & FILT_COUNT_ONLY)
673 line_filter = NULL;
Willy Tarreau72c28532009-01-22 18:56:50 +0100674
Willy Tarreauf8c95d22012-06-12 09:16:56 +0200675#if defined(POSIX_FADV_SEQUENTIAL)
676 /* around 20% performance improvement is observed on Linux with this
677 * on cold-cache. Surprizingly, WILLNEED is less performant. Don't
678 * use NOREUSE as it flushes the cache and prevents easy data
679 * manipulation on logs!
680 */
681 posix_fadvise(0, 0, 0, POSIX_FADV_SEQUENTIAL);
682#endif
683
Willy Tarreaue1a908c2012-01-03 09:23:03 +0100684 if (!line_filter &&
685 !(filter & (FILT_HTTP_ONLY|FILT_TIME_RESP|FILT_ERRORS_ONLY|FILT_HTTP_STATUS|FILT_QUEUE_ONLY|FILT_QUEUE_SRV_ONLY|FILT_TERM_CODE_NAME))) {
686 /* read the whole file at once first */
687 if (!filter_invert)
688 while (fgets2(stdin) != NULL)
689 lines_out++;
690
691 goto skip_filters;
692 }
693
Willy Tarreau214c2032009-02-20 11:02:32 +0100694 while ((line = fgets2(stdin)) != NULL) {
Willy Tarreau72c28532009-01-22 18:56:50 +0100695 linenum++;
Willy Tarreau26deaf52011-07-10 19:47:48 +0200696 time_field = NULL; accept_field = NULL;
Willy Tarreau72c28532009-01-22 18:56:50 +0100697
Willy Tarreau5bdfd962009-10-14 15:16:29 +0200698 test = 1;
Willy Tarreau26deaf52011-07-10 19:47:48 +0200699
700 /* for any line we process, we first ensure that there is a field
701 * looking like the accept date field (beginning with a '[').
702 */
703 accept_field = field_start(line, ACCEPT_FIELD + skip_fields);
Willy Tarreaua2b39fb2011-07-10 21:39:35 +0200704 if (unlikely(*accept_field != '[')) {
Willy Tarreau26deaf52011-07-10 19:47:48 +0200705 parse_err++;
706 continue;
707 }
708
709 /* the day of month field is begin 01 and 31 */
Willy Tarreaua2b39fb2011-07-10 21:39:35 +0200710 if (accept_field[1] < '0' || accept_field[1] > '3') {
Willy Tarreau26deaf52011-07-10 19:47:48 +0200711 parse_err++;
712 continue;
713 }
714
Willy Tarreaua2b39fb2011-07-10 21:39:35 +0200715 if (filter & FILT_HTTP_ONLY) {
Willy Tarreau70c428f2011-07-10 17:27:40 +0200716 /* only report lines with at least 4 timers */
Willy Tarreaua2b39fb2011-07-10 21:39:35 +0200717 if (!time_field) {
Willy Tarreau26deaf52011-07-10 19:47:48 +0200718 time_field = field_start(accept_field, TIME_FIELD - ACCEPT_FIELD + 1);
Willy Tarreaua2b39fb2011-07-10 21:39:35 +0200719 if (unlikely(!*time_field)) {
Willy Tarreau26deaf52011-07-10 19:47:48 +0200720 truncated_line(linenum, line);
721 continue;
722 }
Willy Tarreau70c428f2011-07-10 17:27:40 +0200723 }
724
Willy Tarreau758a6ea2011-07-10 18:53:44 +0200725 e = field_stop(time_field + 1);
726 /* we have field TIME_FIELD in [time_field]..[e-1] */
727 p = time_field;
Willy Tarreau70c428f2011-07-10 17:27:40 +0200728 f = 0;
Willy Tarreaudf6f0d12011-07-10 18:15:08 +0200729 while (!SEP(*p)) {
Willy Tarreau70c428f2011-07-10 17:27:40 +0200730 if (++f == 4)
731 break;
732 SKIP_CHAR(p, '/');
733 }
734 test &= (f >= 4);
735 }
736
Willy Tarreaua2b39fb2011-07-10 21:39:35 +0200737 if (filter & FILT_TIME_RESP) {
Willy Tarreau5bdfd962009-10-14 15:16:29 +0200738 int tps;
739
740 /* only report lines with response times larger than filter_time_resp */
Willy Tarreaua2b39fb2011-07-10 21:39:35 +0200741 if (!time_field) {
Willy Tarreau26deaf52011-07-10 19:47:48 +0200742 time_field = field_start(accept_field, TIME_FIELD - ACCEPT_FIELD + 1);
Willy Tarreaua2b39fb2011-07-10 21:39:35 +0200743 if (unlikely(!*time_field)) {
Willy Tarreau26deaf52011-07-10 19:47:48 +0200744 truncated_line(linenum, line);
745 continue;
746 }
Willy Tarreau5bdfd962009-10-14 15:16:29 +0200747 }
748
Willy Tarreau758a6ea2011-07-10 18:53:44 +0200749 e = field_stop(time_field + 1);
750 /* we have field TIME_FIELD in [time_field]..[e-1], let's check only the response time */
Willy Tarreau5bdfd962009-10-14 15:16:29 +0200751
Willy Tarreau758a6ea2011-07-10 18:53:44 +0200752 p = time_field;
Willy Tarreau5bdfd962009-10-14 15:16:29 +0200753 err = 0;
Willy Tarreau24bcb4f2010-10-28 20:39:50 +0200754 f = 0;
Willy Tarreaudf6f0d12011-07-10 18:15:08 +0200755 while (!SEP(*p)) {
Willy Tarreau5bdfd962009-10-14 15:16:29 +0200756 tps = str2ic(p);
757 if (tps < 0) {
758 tps = -1;
759 err = 1;
760 }
Willy Tarreau24bcb4f2010-10-28 20:39:50 +0200761 if (++f == 4)
762 break;
Willy Tarreau5bdfd962009-10-14 15:16:29 +0200763 SKIP_CHAR(p, '/');
764 }
765
Willy Tarreaua2b39fb2011-07-10 21:39:35 +0200766 if (unlikely(f < 4)) {
Willy Tarreau5bdfd962009-10-14 15:16:29 +0200767 parse_err++;
768 continue;
769 }
770
771 test &= (tps >= filter_time_resp) ^ !!(filter & FILT_INVERT_TIME_RESP);
772 }
773
Willy Tarreaud3007ff2011-09-05 02:07:23 +0200774 if (filter & (FILT_ERRORS_ONLY | FILT_HTTP_STATUS)) {
775 /* Check both error codes (-1, 5xx) and status code ranges */
Willy Tarreau26deaf52011-07-10 19:47:48 +0200776 if (time_field)
777 b = field_start(time_field, STATUS_FIELD - TIME_FIELD + 1);
778 else
779 b = field_start(accept_field, STATUS_FIELD - ACCEPT_FIELD + 1);
780
Willy Tarreaua2b39fb2011-07-10 21:39:35 +0200781 if (unlikely(!*b)) {
Willy Tarreau72c28532009-01-22 18:56:50 +0100782 truncated_line(linenum, line);
783 continue;
784 }
Willy Tarreaua2b39fb2011-07-10 21:39:35 +0200785
Willy Tarreaud3007ff2011-09-05 02:07:23 +0200786 val = str2ic(b);
787 if (filter & FILT_ERRORS_ONLY)
788 test &= (val < 0 || (val >= 500 && val <= 599)) ^ !!(filter & FILT_INVERT_ERRORS);
789
790 if (filter & FILT_HTTP_STATUS)
791 test &= (val >= filt_http_status_low && val <= filt_http_status_high) ^ !!(filter & FILT_INVERT_HTTP_STATUS);
Willy Tarreau72c28532009-01-22 18:56:50 +0100792 }
793
Willy Tarreau08911ff2011-10-13 13:28:36 +0200794 if (filter & (FILT_QUEUE_ONLY|FILT_QUEUE_SRV_ONLY)) {
795 /* Check if the server's queue is non-nul */
796 if (time_field)
797 b = field_start(time_field, QUEUE_LEN_FIELD - TIME_FIELD + 1);
798 else
799 b = field_start(accept_field, QUEUE_LEN_FIELD - ACCEPT_FIELD + 1);
800
801 if (unlikely(!*b)) {
802 truncated_line(linenum, line);
803 continue;
804 }
805
806 if (*b == '0') {
807 if (filter & FILT_QUEUE_SRV_ONLY) {
808 test = 0;
809 }
810 else {
811 do {
812 b++;
813 if (*b == '/') {
814 b++;
815 break;
816 }
817 } while (*b);
818 test &= ((unsigned char)(*b - '1') < 9);
819 }
820 }
821 }
822
Hervé COMMOWICK927cddd2011-08-10 17:42:41 +0200823 if (filter & FILT_TERM_CODE_NAME) {
824 /* only report corresponding termination code name */
825 if (time_field)
826 b = field_start(time_field, TERM_CODES_FIELD - TIME_FIELD + 1);
827 else
828 b = field_start(accept_field, TERM_CODES_FIELD - ACCEPT_FIELD + 1);
829
830 if (unlikely(!*b)) {
831 truncated_line(linenum, line);
832 continue;
833 }
834
835 test &= (b[0] == filter_term_code_name[0] && b[1] == filter_term_code_name[1]) ^ !!(filter & FILT_INVERT_TERM_CODE_NAME);
836 }
837
838
Willy Tarreau0f423a72010-05-03 10:50:54 +0200839 test ^= filter_invert;
840 if (!test)
841 continue;
842
Willy Tarreaua2b39fb2011-07-10 21:39:35 +0200843 /************** here we process inputs *******************/
Willy Tarreau72c28532009-01-22 18:56:50 +0100844
Willy Tarreaua2b39fb2011-07-10 21:39:35 +0200845 if (line_filter)
846 line_filter(accept_field, time_field, &t);
847 else
848 lines_out++; /* we're just counting lines */
849 }
Willy Tarreauabe45b62010-10-28 20:33:46 +0200850
Willy Tarreaue1a908c2012-01-03 09:23:03 +0100851 skip_filters:
Willy Tarreaua2b39fb2011-07-10 21:39:35 +0200852 /*****************************************************
853 * Here we've finished reading all input. Depending on the
854 * filters, we may still have some analysis to run on the
855 * collected data and to output data in a new format.
856 *************************************************** */
Willy Tarreau72c28532009-01-22 18:56:50 +0100857
858 if (t)
859 free(t);
860
861 if (filter & FILT_COUNT_ONLY) {
Willy Tarreaua2b39fb2011-07-10 21:39:35 +0200862 printf("%d\n", lines_out);
Willy Tarreau72c28532009-01-22 18:56:50 +0100863 exit(0);
864 }
865
Willy Tarreau72c28532009-01-22 18:56:50 +0100866 if (filter & (FILT_ACC_COUNT|FILT_ACC_DELAY)) {
867 /* sort and count all timers. Output will look like this :
868 * <accept_date> <delta_ms from previous one> <nb entries>
869 */
870 n = eb32_first(&timers[0]);
871
872 if (n)
873 last = n->key;
874 while (n) {
875 unsigned int d, h, m, s, ms;
876
877 t = container_of(n, struct timer, node);
878 h = n->key;
879 d = h - last;
880 last = h;
881
882 if (d >= filter_acc_delay && t->count >= filter_acc_count) {
883 ms = h % 1000; h = h / 1000;
884 s = h % 60; h = h / 60;
885 m = h % 60; h = h / 60;
Willy Tarreaua2b39fb2011-07-10 21:39:35 +0200886 lines_out++;
Willy Tarreau72c28532009-01-22 18:56:50 +0100887 printf("%02d:%02d:%02d.%03d %d %d %d\n", h, m, s, ms, last, d, t->count);
888 }
889 n = eb32_next(n);
890 }
891 }
892 else if (filter & FILT_GRAPH_TIMERS) {
893 /* sort all timers */
894 for (f = 0; f < 5; f++) {
895 struct eb32_node *n;
896 int val;
897
898 val = 0;
899 n = eb32_first(&timers[f]);
900 while (n) {
901 int i;
902 double d;
903
904 t = container_of(n, struct timer, node);
905 last = n->key;
906 val = t->count;
907
908 i = (last < 0) ? -last : last;
909 i = fls_auto(i) - QBITS;
910
911 if (i > 0)
912 d = val / (double)(1 << i);
913 else
914 d = val;
915
916 if (d > 0.0) {
917 printf("%d %d %f\n", f, last, d+1.0);
Willy Tarreaua2b39fb2011-07-10 21:39:35 +0200918 lines_out++;
Willy Tarreau72c28532009-01-22 18:56:50 +0100919 }
920
921 n = eb32_next(n);
922 }
Willy Tarreau214c2032009-02-20 11:02:32 +0100923 }
924 }
925 else if (filter & FILT_PERCENTILE) {
926 /* report timers by percentile :
927 * <percent> <total> <max_req_time> <max_conn_time> <max_resp_time> <max_data_time>
928 * We don't count errs.
929 */
930 struct eb32_node *n[5];
931 unsigned long cum[5];
932 double step;
933
Willy Tarreaua2b39fb2011-07-10 21:39:35 +0200934 if (!lines_out)
Willy Tarreau910ba4b2009-11-17 10:16:19 +0100935 goto empty;
936
Willy Tarreau214c2032009-02-20 11:02:32 +0100937 for (f = 1; f < 5; f++) {
938 n[f] = eb32_first(&timers[f]);
939 cum[f] = container_of(n[f], struct timer, node)->count;
940 }
941
942 for (step = 1; step <= 1000;) {
Willy Tarreaua2b39fb2011-07-10 21:39:35 +0200943 unsigned int thres = lines_out * (step / 1000.0);
Willy Tarreau214c2032009-02-20 11:02:32 +0100944
945 printf("%3.1f %d ", step/10.0, thres);
946 for (f = 1; f < 5; f++) {
947 struct eb32_node *next;
948 while (cum[f] < thres) {
949 /* need to find other keys */
950 next = eb32_next(n[f]);
951 if (!next)
952 break;
953 n[f] = next;
954 cum[f] += container_of(next, struct timer, node)->count;
955 }
956
957 /* value still within $step % of total */
958 printf("%d ", n[f]->key);
959 }
960 putchar('\n');
961 if (step >= 100 && step < 900)
962 step += 50; // jump 5% by 5% between those steps.
963 else if (step >= 20 && step < 980)
964 step += 10;
965 else
966 step += 1;
Willy Tarreau72c28532009-01-22 18:56:50 +0100967 }
968 }
Willy Tarreau0f423a72010-05-03 10:50:54 +0200969 else if (filter & FILT_COUNT_STATUS) {
970 /* output all statuses in the form of <status> <occurrences> */
971 n = eb32_first(&timers[0]);
972 while (n) {
973 t = container_of(n, struct timer, node);
974 printf("%d %d\n", n->key, t->count);
Willy Tarreaua2b39fb2011-07-10 21:39:35 +0200975 lines_out++;
Willy Tarreau0f423a72010-05-03 10:50:54 +0200976 n = eb32_next(n);
977 }
978 }
Willy Tarreaua2b39fb2011-07-10 21:39:35 +0200979 else if (filter & FILT_COUNT_SRV_STATUS) {
Willy Tarreaud2201062010-05-27 18:17:30 +0200980 struct ebmb_node *srv_node;
981 struct srv_st *srv;
982
983 printf("#srv_name 1xx 2xx 3xx 4xx 5xx other tot_req req_ok pct_ok avg_ct avg_rt\n");
984
985 srv_node = ebmb_first(&timers[0]);
986 while (srv_node) {
987 int tot_rq;
988
989 srv = container_of(srv_node, struct srv_st, node);
990
991 tot_rq = 0;
992 for (f = 0; f <= 5; f++)
993 tot_rq += srv->st_cnt[f];
994
995 printf("%s %d %d %d %d %d %d %d %d %.1f %d %d\n",
996 srv_node->key, srv->st_cnt[1], srv->st_cnt[2],
997 srv->st_cnt[3], srv->st_cnt[4], srv->st_cnt[5], srv->st_cnt[0],
998 tot_rq,
999 srv->nb_ok, (double)srv->nb_ok * 100.0 / (tot_rq?tot_rq:1),
1000 (int)(srv->cum_ct / (srv->nb_ct?srv->nb_ct:1)), (int)(srv->cum_rt / (srv->nb_rt?srv->nb_rt:1)));
1001 srv_node = ebmb_next(srv_node);
Willy Tarreaua2b39fb2011-07-10 21:39:35 +02001002 lines_out++;
Willy Tarreaud2201062010-05-27 18:17:30 +02001003 }
1004 }
Willy Tarreaud8fc1102010-09-12 17:56:16 +02001005 else if (filter & FILT_COUNT_TERM_CODES) {
1006 /* output all statuses in the form of <code> <occurrences> */
1007 n = eb32_first(&timers[0]);
1008 while (n) {
1009 t = container_of(n, struct timer, node);
1010 printf("%c%c %d\n", (n->key >> 8), (n->key) & 255, t->count);
Willy Tarreaua2b39fb2011-07-10 21:39:35 +02001011 lines_out++;
Willy Tarreaud8fc1102010-09-12 17:56:16 +02001012 n = eb32_next(n);
1013 }
1014 }
Willy Tarreaua2b39fb2011-07-10 21:39:35 +02001015 else if (filter & FILT_COUNT_URL_ANY) {
Willy Tarreauabe45b62010-10-28 20:33:46 +02001016 struct eb_node *node, *next;
1017
1018 if (!(filter & FILT_COUNT_URL_ONLY)) {
1019 /* we have to sort on another criterion. We'll use timers[1] for the
1020 * destination tree.
1021 */
1022
1023 timers[1] = EB_ROOT; /* reconfigure to accept duplicates */
1024 for (node = eb_first(&timers[0]); node; node = next) {
1025 next = eb_next(node);
1026 eb_delete(node);
1027
1028 ustat = container_of(node, struct url_stat, node.url.node);
1029
1030 if (filter & FILT_COUNT_URL_COUNT)
1031 ustat->node.val.key = ustat->nb_req;
1032 else if (filter & FILT_COUNT_URL_ERR)
1033 ustat->node.val.key = ustat->nb_err;
1034 else if (filter & FILT_COUNT_URL_TTOT)
1035 ustat->node.val.key = ustat->total_time;
1036 else if (filter & FILT_COUNT_URL_TAVG)
1037 ustat->node.val.key = ustat->nb_req ? ustat->total_time / ustat->nb_req : 0;
1038 else if (filter & FILT_COUNT_URL_TTOTO)
1039 ustat->node.val.key = ustat->total_time_ok;
1040 else if (filter & FILT_COUNT_URL_TAVGO)
1041 ustat->node.val.key = (ustat->nb_req - ustat->nb_err) ? ustat->total_time_ok / (ustat->nb_req - ustat->nb_err) : 0;
1042 else
1043 ustat->node.val.key = 0;
1044
1045 eb64_insert(&timers[1], &ustat->node.val);
1046 }
1047 /* switch trees */
1048 timers[0] = timers[1];
1049 }
1050
1051 printf("#req err ttot tavg oktot okavg url\n");
1052
1053 /* scan the tree in its reverse sorting order */
1054 node = eb_last(&timers[0]);
1055 while (node) {
1056 ustat = container_of(node, struct url_stat, node.url.node);
1057 printf("%d %d %Ld %Ld %Ld %Ld %s\n",
1058 ustat->nb_req,
1059 ustat->nb_err,
1060 ustat->total_time,
1061 ustat->nb_req ? ustat->total_time / ustat->nb_req : 0,
1062 ustat->total_time_ok,
1063 (ustat->nb_req - ustat->nb_err) ? ustat->total_time_ok / (ustat->nb_req - ustat->nb_err) : 0,
1064 ustat->url);
1065
1066 node = eb_prev(node);
Willy Tarreaua2b39fb2011-07-10 21:39:35 +02001067 lines_out++;
Willy Tarreauabe45b62010-10-28 20:33:46 +02001068 }
1069 }
Willy Tarreaud2201062010-05-27 18:17:30 +02001070
Willy Tarreau910ba4b2009-11-17 10:16:19 +01001071 empty:
Willy Tarreau72c28532009-01-22 18:56:50 +01001072 if (!(filter & FILT_QUIET))
1073 fprintf(stderr, "%d lines in, %d lines out, %d parsing errors\n",
Willy Tarreaua2b39fb2011-07-10 21:39:35 +02001074 linenum, lines_out, parse_err);
Willy Tarreau72c28532009-01-22 18:56:50 +01001075 exit(0);
1076}
1077
Willy Tarreaua2b39fb2011-07-10 21:39:35 +02001078void filter_output_line(const char *accept_field, const char *time_field, struct timer **tptr)
1079{
1080 puts(line);
1081 lines_out++;
1082}
1083
1084void filter_accept_holes(const char *accept_field, const char *time_field, struct timer **tptr)
1085{
1086 struct timer *t2;
1087 int val;
1088
1089 val = convert_date(accept_field);
1090 if (unlikely(val < 0)) {
1091 truncated_line(linenum, line);
1092 return;
1093 }
1094
1095 t2 = insert_value(&timers[0], tptr, val);
1096 t2->count++;
1097 lines_out++;
1098 return;
1099}
1100
1101void filter_count_status(const char *accept_field, const char *time_field, struct timer **tptr)
1102{
1103 struct timer *t2;
1104 const char *b;
1105 int val;
1106
1107 if (time_field)
1108 b = field_start(time_field, STATUS_FIELD - TIME_FIELD + 1);
1109 else
1110 b = field_start(accept_field, STATUS_FIELD - ACCEPT_FIELD + 1);
1111
1112 if (unlikely(!*b)) {
1113 truncated_line(linenum, line);
1114 return;
1115 }
1116
1117 val = str2ic(b);
1118
1119 t2 = insert_value(&timers[0], tptr, val);
1120 t2->count++;
1121}
1122
1123void filter_count_term_codes(const char *accept_field, const char *time_field, struct timer **tptr)
1124{
1125 struct timer *t2;
1126 const char *b;
1127 int val;
1128
1129 if (time_field)
1130 b = field_start(time_field, TERM_CODES_FIELD - TIME_FIELD + 1);
1131 else
1132 b = field_start(accept_field, TERM_CODES_FIELD - ACCEPT_FIELD + 1);
1133
1134 if (unlikely(!*b)) {
1135 truncated_line(linenum, line);
1136 return;
1137 }
1138
1139 val = 256 * b[0] + b[1];
1140
1141 t2 = insert_value(&timers[0], tptr, val);
1142 t2->count++;
1143}
1144
1145void filter_count_srv_status(const char *accept_field, const char *time_field, struct timer **tptr)
1146{
1147 const char *b, *e, *p;
1148 int f, err, array[5];
1149 struct ebmb_node *srv_node;
1150 struct srv_st *srv;
1151 int val;
1152
1153 /* the server field is before the status field, so let's
1154 * parse them in the proper order.
1155 */
1156 b = field_start(accept_field, SERVER_FIELD - ACCEPT_FIELD + 1);
1157 if (unlikely(!*b)) {
1158 truncated_line(linenum, line);
1159 return;
1160 }
1161
1162 e = field_stop(b + 1); /* we have the server name in [b]..[e-1] */
1163
1164 /* the chance that a server name already exists is extremely high,
1165 * so let's perform a normal lookup first.
1166 */
1167 srv_node = ebst_lookup_len(&timers[0], b, e - b);
1168 srv = container_of(srv_node, struct srv_st, node);
1169
1170 if (!srv_node) {
1171 /* server not yet in the tree, let's create it */
1172 srv = (void *)calloc(1, sizeof(struct srv_st) + e - b + 1);
1173 srv_node = &srv->node;
1174 memcpy(&srv_node->key, b, e - b);
1175 srv_node->key[e - b] = '\0';
1176 ebst_insert(&timers[0], srv_node);
1177 }
1178
1179 /* let's collect the connect and response times */
1180 if (!time_field) {
1181 time_field = field_start(e, TIME_FIELD - SERVER_FIELD);
1182 if (unlikely(!*time_field)) {
1183 truncated_line(linenum, line);
1184 return;
1185 }
1186 }
1187
1188 e = field_stop(time_field + 1);
1189 /* we have field TIME_FIELD in [time_field]..[e-1] */
1190
1191 p = time_field;
1192 err = 0;
1193 f = 0;
1194 while (!SEP(*p)) {
1195 array[f] = str2ic(p);
1196 if (array[f] < 0) {
1197 array[f] = -1;
1198 err = 1;
1199 }
1200 if (++f == 5)
1201 break;
1202 SKIP_CHAR(p, '/');
1203 }
1204
1205 if (unlikely(f < 5)){
1206 parse_err++;
1207 return;
1208 }
1209
1210 /* OK we have our timers in array[2,3] */
1211 if (!err)
1212 srv->nb_ok++;
1213
1214 if (array[2] >= 0) {
1215 srv->cum_ct += array[2];
1216 srv->nb_ct++;
1217 }
1218
1219 if (array[3] >= 0) {
1220 srv->cum_rt += array[3];
1221 srv->nb_rt++;
1222 }
1223
1224 /* we're interested in the 5 HTTP status classes (1xx ... 5xx), and
1225 * the invalid ones which will be reported as 0.
1226 */
1227 b = field_start(e, STATUS_FIELD - TIME_FIELD);
1228 if (unlikely(!*b)) {
1229 truncated_line(linenum, line);
1230 return;
1231 }
1232
1233 val = 0;
1234 if (*b >= '1' && *b <= '5')
1235 val = *b - '0';
1236
1237 srv->st_cnt[val]++;
1238}
1239
1240void filter_count_url(const char *accept_field, const char *time_field, struct timer **tptr)
1241{
1242 struct url_stat *ustat = NULL;
1243 struct ebpt_node *ebpt_old;
1244 const char *b, *e;
1245 int f, err, array[5];
1246
1247 /* let's collect the response time */
1248 if (!time_field) {
1249 time_field = field_start(accept_field, TIME_FIELD - ACCEPT_FIELD + 1); // avg 115 ns per line
1250 if (unlikely(!*time_field)) {
1251 truncated_line(linenum, line);
1252 return;
1253 }
1254 }
1255
1256 /* we have the field TIME_FIELD starting at <time_field>. We'll
1257 * parse the 5 timers to detect errors, it takes avg 55 ns per line.
1258 */
1259 e = time_field; err = 0; f = 0;
1260 while (!SEP(*e)) {
1261 array[f] = str2ic(e);
1262 if (array[f] < 0) {
1263 array[f] = -1;
1264 err = 1;
1265 }
1266 if (++f == 5)
1267 break;
1268 SKIP_CHAR(e, '/');
1269 }
1270 if (f < 5) {
1271 parse_err++;
1272 return;
1273 }
1274
1275 /* OK we have our timers in array[3], and err is >0 if at
1276 * least one -1 was seen. <e> points to the first char of
1277 * the last timer. Let's prepare a new node with that.
1278 */
1279 if (unlikely(!ustat))
1280 ustat = calloc(1, sizeof(*ustat));
1281
1282 ustat->nb_err = err;
1283 ustat->nb_req = 1;
1284
1285 /* use array[4] = total time in case of error */
1286 ustat->total_time = (array[3] >= 0) ? array[3] : array[4];
1287 ustat->total_time_ok = (array[3] >= 0) ? array[3] : 0;
1288
1289 /* the line may be truncated because of a bad request or anything like this,
1290 * without a method. Also, if it does not begin with an quote, let's skip to
1291 * the next field because it's a capture. Let's fall back to the "method" itself
1292 * if there's nothing else.
1293 */
1294 e = field_start(e, METH_FIELD - TIME_FIELD + 1); // avg 100 ns per line
Willy Tarreau61a40c72011-09-06 08:11:27 +02001295 while (*e != '"' && *e) {
1296 /* Note: some syslog servers escape quotes ! */
1297 if (*e == '\\' && e[1] == '"')
1298 break;
Willy Tarreaua2b39fb2011-07-10 21:39:35 +02001299 e = field_start(e, 2);
Willy Tarreau61a40c72011-09-06 08:11:27 +02001300 }
Willy Tarreaua2b39fb2011-07-10 21:39:35 +02001301
1302 if (unlikely(!*e)) {
1303 truncated_line(linenum, line);
1304 return;
1305 }
1306
1307 b = field_start(e, URL_FIELD - METH_FIELD + 1); // avg 40 ns per line
1308 if (!*b)
1309 b = e;
1310
1311 /* stop at end of field or first ';' or '?', takes avg 64 ns per line */
1312 e = b;
1313 do {
Willy Tarreau14389e72011-07-10 22:11:17 +02001314 if (*e == ' ' || *e == '?' || *e == ';') {
Willy Tarreaua2b39fb2011-07-10 21:39:35 +02001315 *(char *)e = 0;
1316 break;
1317 }
1318 e++;
1319 } while (*e);
1320
1321 /* now instead of copying the URL for a simple lookup, we'll link
1322 * to it from the node we're trying to insert. If it returns a
1323 * different value, it was already there. Otherwise we just have
1324 * to dynamically realloc an entry using strdup().
1325 */
1326 ustat->node.url.key = (char *)b;
1327 ebpt_old = ebis_insert(&timers[0], &ustat->node.url);
1328
1329 if (ebpt_old != &ustat->node.url) {
1330 struct url_stat *ustat_old;
1331 /* node was already there, let's update previous one */
1332 ustat_old = container_of(ebpt_old, struct url_stat, node.url);
1333 ustat_old->nb_req ++;
1334 ustat_old->nb_err += ustat->nb_err;
1335 ustat_old->total_time += ustat->total_time;
1336 ustat_old->total_time_ok += ustat->total_time_ok;
1337 } else {
1338 ustat->url = ustat->node.url.key = strdup(ustat->node.url.key);
1339 ustat = NULL; /* node was used */
1340 }
1341}
1342
1343void filter_graphs(const char *accept_field, const char *time_field, struct timer **tptr)
1344{
1345 struct timer *t2;
1346 const char *e, *p;
1347 int f, err, array[5];
1348
1349 if (!time_field) {
1350 time_field = field_start(accept_field, TIME_FIELD - ACCEPT_FIELD + 1);
1351 if (unlikely(!*time_field)) {
1352 truncated_line(linenum, line);
1353 return;
1354 }
1355 }
1356
1357 e = field_stop(time_field + 1);
1358 /* we have field TIME_FIELD in [time_field]..[e-1] */
1359
1360 p = time_field;
1361 err = 0;
1362 f = 0;
1363 while (!SEP(*p)) {
1364 array[f] = str2ic(p);
1365 if (array[f] < 0) {
1366 array[f] = -1;
1367 err = 1;
1368 }
1369 if (++f == 5)
1370 break;
1371 SKIP_CHAR(p, '/');
1372 }
1373
1374 if (unlikely(f < 5)) {
1375 parse_err++;
1376 return;
1377 }
1378
1379 /* if we find at least one negative time, we count one error
1380 * with a time equal to the total session time. This will
1381 * emphasize quantum timing effects associated to known
1382 * timeouts. Note that on some buggy machines, it is possible
1383 * that the total time is negative, hence the reason to reset
1384 * it.
1385 */
1386
1387 if (filter & FILT_GRAPH_TIMERS) {
1388 if (err) {
1389 if (array[4] < 0)
1390 array[4] = -1;
1391 t2 = insert_timer(&timers[0], tptr, array[4]); // total time
1392 t2->count++;
1393 } else {
1394 int v;
1395
1396 t2 = insert_timer(&timers[1], tptr, array[0]); t2->count++; // req
1397 t2 = insert_timer(&timers[2], tptr, array[2]); t2->count++; // conn
1398 t2 = insert_timer(&timers[3], tptr, array[3]); t2->count++; // resp
1399
1400 v = array[4] - array[0] - array[1] - array[2] - array[3]; // data time
1401 if (v < 0 && !(filter & FILT_QUIET))
1402 fprintf(stderr, "ERR: %s (%d %d %d %d %d => %d)\n",
1403 line, array[0], array[1], array[2], array[3], array[4], v);
1404 t2 = insert_timer(&timers[4], tptr, v); t2->count++;
1405 lines_out++;
1406 }
1407 } else { /* percentile */
1408 if (err) {
1409 if (array[4] < 0)
1410 array[4] = -1;
1411 t2 = insert_value(&timers[0], tptr, array[4]); // total time
1412 t2->count++;
1413 } else {
1414 int v;
1415
1416 t2 = insert_value(&timers[1], tptr, array[0]); t2->count++; // req
1417 t2 = insert_value(&timers[2], tptr, array[2]); t2->count++; // conn
1418 t2 = insert_value(&timers[3], tptr, array[3]); t2->count++; // resp
1419
1420 v = array[4] - array[0] - array[1] - array[2] - array[3]; // data time
1421 if (v < 0 && !(filter & FILT_QUIET))
1422 fprintf(stderr, "ERR: %s (%d %d %d %d %d => %d)\n",
1423 line, array[0], array[1], array[2], array[3], array[4], v);
1424 t2 = insert_value(&timers[4], tptr, v); t2->count++;
1425 lines_out++;
1426 }
1427 }
1428}
1429
1430
Willy Tarreau72c28532009-01-22 18:56:50 +01001431/*
1432 * Local variables:
1433 * c-indent-level: 8
1434 * c-basic-offset: 8
1435 * End:
1436 */