blob: 250623d8275989fefd555df1e81259f8c8852931 [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 Tarreaue1a908c2012-01-03 09:23:03 +0100675 if (!line_filter &&
676 !(filter & (FILT_HTTP_ONLY|FILT_TIME_RESP|FILT_ERRORS_ONLY|FILT_HTTP_STATUS|FILT_QUEUE_ONLY|FILT_QUEUE_SRV_ONLY|FILT_TERM_CODE_NAME))) {
677 /* read the whole file at once first */
678 if (!filter_invert)
679 while (fgets2(stdin) != NULL)
680 lines_out++;
681
682 goto skip_filters;
683 }
684
Willy Tarreau214c2032009-02-20 11:02:32 +0100685 while ((line = fgets2(stdin)) != NULL) {
Willy Tarreau72c28532009-01-22 18:56:50 +0100686 linenum++;
Willy Tarreau26deaf52011-07-10 19:47:48 +0200687 time_field = NULL; accept_field = NULL;
Willy Tarreau72c28532009-01-22 18:56:50 +0100688
Willy Tarreau5bdfd962009-10-14 15:16:29 +0200689 test = 1;
Willy Tarreau26deaf52011-07-10 19:47:48 +0200690
691 /* for any line we process, we first ensure that there is a field
692 * looking like the accept date field (beginning with a '[').
693 */
694 accept_field = field_start(line, ACCEPT_FIELD + skip_fields);
Willy Tarreaua2b39fb2011-07-10 21:39:35 +0200695 if (unlikely(*accept_field != '[')) {
Willy Tarreau26deaf52011-07-10 19:47:48 +0200696 parse_err++;
697 continue;
698 }
699
700 /* the day of month field is begin 01 and 31 */
Willy Tarreaua2b39fb2011-07-10 21:39:35 +0200701 if (accept_field[1] < '0' || accept_field[1] > '3') {
Willy Tarreau26deaf52011-07-10 19:47:48 +0200702 parse_err++;
703 continue;
704 }
705
Willy Tarreaua2b39fb2011-07-10 21:39:35 +0200706 if (filter & FILT_HTTP_ONLY) {
Willy Tarreau70c428f2011-07-10 17:27:40 +0200707 /* only report lines with at least 4 timers */
Willy Tarreaua2b39fb2011-07-10 21:39:35 +0200708 if (!time_field) {
Willy Tarreau26deaf52011-07-10 19:47:48 +0200709 time_field = field_start(accept_field, TIME_FIELD - ACCEPT_FIELD + 1);
Willy Tarreaua2b39fb2011-07-10 21:39:35 +0200710 if (unlikely(!*time_field)) {
Willy Tarreau26deaf52011-07-10 19:47:48 +0200711 truncated_line(linenum, line);
712 continue;
713 }
Willy Tarreau70c428f2011-07-10 17:27:40 +0200714 }
715
Willy Tarreau758a6ea2011-07-10 18:53:44 +0200716 e = field_stop(time_field + 1);
717 /* we have field TIME_FIELD in [time_field]..[e-1] */
718 p = time_field;
Willy Tarreau70c428f2011-07-10 17:27:40 +0200719 f = 0;
Willy Tarreaudf6f0d12011-07-10 18:15:08 +0200720 while (!SEP(*p)) {
Willy Tarreau70c428f2011-07-10 17:27:40 +0200721 if (++f == 4)
722 break;
723 SKIP_CHAR(p, '/');
724 }
725 test &= (f >= 4);
726 }
727
Willy Tarreaua2b39fb2011-07-10 21:39:35 +0200728 if (filter & FILT_TIME_RESP) {
Willy Tarreau5bdfd962009-10-14 15:16:29 +0200729 int tps;
730
731 /* only report lines with response times larger than filter_time_resp */
Willy Tarreaua2b39fb2011-07-10 21:39:35 +0200732 if (!time_field) {
Willy Tarreau26deaf52011-07-10 19:47:48 +0200733 time_field = field_start(accept_field, TIME_FIELD - ACCEPT_FIELD + 1);
Willy Tarreaua2b39fb2011-07-10 21:39:35 +0200734 if (unlikely(!*time_field)) {
Willy Tarreau26deaf52011-07-10 19:47:48 +0200735 truncated_line(linenum, line);
736 continue;
737 }
Willy Tarreau5bdfd962009-10-14 15:16:29 +0200738 }
739
Willy Tarreau758a6ea2011-07-10 18:53:44 +0200740 e = field_stop(time_field + 1);
741 /* we have field TIME_FIELD in [time_field]..[e-1], let's check only the response time */
Willy Tarreau5bdfd962009-10-14 15:16:29 +0200742
Willy Tarreau758a6ea2011-07-10 18:53:44 +0200743 p = time_field;
Willy Tarreau5bdfd962009-10-14 15:16:29 +0200744 err = 0;
Willy Tarreau24bcb4f2010-10-28 20:39:50 +0200745 f = 0;
Willy Tarreaudf6f0d12011-07-10 18:15:08 +0200746 while (!SEP(*p)) {
Willy Tarreau5bdfd962009-10-14 15:16:29 +0200747 tps = str2ic(p);
748 if (tps < 0) {
749 tps = -1;
750 err = 1;
751 }
Willy Tarreau24bcb4f2010-10-28 20:39:50 +0200752 if (++f == 4)
753 break;
Willy Tarreau5bdfd962009-10-14 15:16:29 +0200754 SKIP_CHAR(p, '/');
755 }
756
Willy Tarreaua2b39fb2011-07-10 21:39:35 +0200757 if (unlikely(f < 4)) {
Willy Tarreau5bdfd962009-10-14 15:16:29 +0200758 parse_err++;
759 continue;
760 }
761
762 test &= (tps >= filter_time_resp) ^ !!(filter & FILT_INVERT_TIME_RESP);
763 }
764
Willy Tarreaud3007ff2011-09-05 02:07:23 +0200765 if (filter & (FILT_ERRORS_ONLY | FILT_HTTP_STATUS)) {
766 /* Check both error codes (-1, 5xx) and status code ranges */
Willy Tarreau26deaf52011-07-10 19:47:48 +0200767 if (time_field)
768 b = field_start(time_field, STATUS_FIELD - TIME_FIELD + 1);
769 else
770 b = field_start(accept_field, STATUS_FIELD - ACCEPT_FIELD + 1);
771
Willy Tarreaua2b39fb2011-07-10 21:39:35 +0200772 if (unlikely(!*b)) {
Willy Tarreau72c28532009-01-22 18:56:50 +0100773 truncated_line(linenum, line);
774 continue;
775 }
Willy Tarreaua2b39fb2011-07-10 21:39:35 +0200776
Willy Tarreaud3007ff2011-09-05 02:07:23 +0200777 val = str2ic(b);
778 if (filter & FILT_ERRORS_ONLY)
779 test &= (val < 0 || (val >= 500 && val <= 599)) ^ !!(filter & FILT_INVERT_ERRORS);
780
781 if (filter & FILT_HTTP_STATUS)
782 test &= (val >= filt_http_status_low && val <= filt_http_status_high) ^ !!(filter & FILT_INVERT_HTTP_STATUS);
Willy Tarreau72c28532009-01-22 18:56:50 +0100783 }
784
Willy Tarreau08911ff2011-10-13 13:28:36 +0200785 if (filter & (FILT_QUEUE_ONLY|FILT_QUEUE_SRV_ONLY)) {
786 /* Check if the server's queue is non-nul */
787 if (time_field)
788 b = field_start(time_field, QUEUE_LEN_FIELD - TIME_FIELD + 1);
789 else
790 b = field_start(accept_field, QUEUE_LEN_FIELD - ACCEPT_FIELD + 1);
791
792 if (unlikely(!*b)) {
793 truncated_line(linenum, line);
794 continue;
795 }
796
797 if (*b == '0') {
798 if (filter & FILT_QUEUE_SRV_ONLY) {
799 test = 0;
800 }
801 else {
802 do {
803 b++;
804 if (*b == '/') {
805 b++;
806 break;
807 }
808 } while (*b);
809 test &= ((unsigned char)(*b - '1') < 9);
810 }
811 }
812 }
813
Hervé COMMOWICK927cddd2011-08-10 17:42:41 +0200814 if (filter & FILT_TERM_CODE_NAME) {
815 /* only report corresponding termination code name */
816 if (time_field)
817 b = field_start(time_field, TERM_CODES_FIELD - TIME_FIELD + 1);
818 else
819 b = field_start(accept_field, TERM_CODES_FIELD - ACCEPT_FIELD + 1);
820
821 if (unlikely(!*b)) {
822 truncated_line(linenum, line);
823 continue;
824 }
825
826 test &= (b[0] == filter_term_code_name[0] && b[1] == filter_term_code_name[1]) ^ !!(filter & FILT_INVERT_TERM_CODE_NAME);
827 }
828
829
Willy Tarreau0f423a72010-05-03 10:50:54 +0200830 test ^= filter_invert;
831 if (!test)
832 continue;
833
Willy Tarreaua2b39fb2011-07-10 21:39:35 +0200834 /************** here we process inputs *******************/
Willy Tarreau72c28532009-01-22 18:56:50 +0100835
Willy Tarreaua2b39fb2011-07-10 21:39:35 +0200836 if (line_filter)
837 line_filter(accept_field, time_field, &t);
838 else
839 lines_out++; /* we're just counting lines */
840 }
Willy Tarreauabe45b62010-10-28 20:33:46 +0200841
Willy Tarreaue1a908c2012-01-03 09:23:03 +0100842 skip_filters:
Willy Tarreaua2b39fb2011-07-10 21:39:35 +0200843 /*****************************************************
844 * Here we've finished reading all input. Depending on the
845 * filters, we may still have some analysis to run on the
846 * collected data and to output data in a new format.
847 *************************************************** */
Willy Tarreau72c28532009-01-22 18:56:50 +0100848
849 if (t)
850 free(t);
851
852 if (filter & FILT_COUNT_ONLY) {
Willy Tarreaua2b39fb2011-07-10 21:39:35 +0200853 printf("%d\n", lines_out);
Willy Tarreau72c28532009-01-22 18:56:50 +0100854 exit(0);
855 }
856
Willy Tarreau72c28532009-01-22 18:56:50 +0100857 if (filter & (FILT_ACC_COUNT|FILT_ACC_DELAY)) {
858 /* sort and count all timers. Output will look like this :
859 * <accept_date> <delta_ms from previous one> <nb entries>
860 */
861 n = eb32_first(&timers[0]);
862
863 if (n)
864 last = n->key;
865 while (n) {
866 unsigned int d, h, m, s, ms;
867
868 t = container_of(n, struct timer, node);
869 h = n->key;
870 d = h - last;
871 last = h;
872
873 if (d >= filter_acc_delay && t->count >= filter_acc_count) {
874 ms = h % 1000; h = h / 1000;
875 s = h % 60; h = h / 60;
876 m = h % 60; h = h / 60;
Willy Tarreaua2b39fb2011-07-10 21:39:35 +0200877 lines_out++;
Willy Tarreau72c28532009-01-22 18:56:50 +0100878 printf("%02d:%02d:%02d.%03d %d %d %d\n", h, m, s, ms, last, d, t->count);
879 }
880 n = eb32_next(n);
881 }
882 }
883 else if (filter & FILT_GRAPH_TIMERS) {
884 /* sort all timers */
885 for (f = 0; f < 5; f++) {
886 struct eb32_node *n;
887 int val;
888
889 val = 0;
890 n = eb32_first(&timers[f]);
891 while (n) {
892 int i;
893 double d;
894
895 t = container_of(n, struct timer, node);
896 last = n->key;
897 val = t->count;
898
899 i = (last < 0) ? -last : last;
900 i = fls_auto(i) - QBITS;
901
902 if (i > 0)
903 d = val / (double)(1 << i);
904 else
905 d = val;
906
907 if (d > 0.0) {
908 printf("%d %d %f\n", f, last, d+1.0);
Willy Tarreaua2b39fb2011-07-10 21:39:35 +0200909 lines_out++;
Willy Tarreau72c28532009-01-22 18:56:50 +0100910 }
911
912 n = eb32_next(n);
913 }
Willy Tarreau214c2032009-02-20 11:02:32 +0100914 }
915 }
916 else if (filter & FILT_PERCENTILE) {
917 /* report timers by percentile :
918 * <percent> <total> <max_req_time> <max_conn_time> <max_resp_time> <max_data_time>
919 * We don't count errs.
920 */
921 struct eb32_node *n[5];
922 unsigned long cum[5];
923 double step;
924
Willy Tarreaua2b39fb2011-07-10 21:39:35 +0200925 if (!lines_out)
Willy Tarreau910ba4b2009-11-17 10:16:19 +0100926 goto empty;
927
Willy Tarreau214c2032009-02-20 11:02:32 +0100928 for (f = 1; f < 5; f++) {
929 n[f] = eb32_first(&timers[f]);
930 cum[f] = container_of(n[f], struct timer, node)->count;
931 }
932
933 for (step = 1; step <= 1000;) {
Willy Tarreaua2b39fb2011-07-10 21:39:35 +0200934 unsigned int thres = lines_out * (step / 1000.0);
Willy Tarreau214c2032009-02-20 11:02:32 +0100935
936 printf("%3.1f %d ", step/10.0, thres);
937 for (f = 1; f < 5; f++) {
938 struct eb32_node *next;
939 while (cum[f] < thres) {
940 /* need to find other keys */
941 next = eb32_next(n[f]);
942 if (!next)
943 break;
944 n[f] = next;
945 cum[f] += container_of(next, struct timer, node)->count;
946 }
947
948 /* value still within $step % of total */
949 printf("%d ", n[f]->key);
950 }
951 putchar('\n');
952 if (step >= 100 && step < 900)
953 step += 50; // jump 5% by 5% between those steps.
954 else if (step >= 20 && step < 980)
955 step += 10;
956 else
957 step += 1;
Willy Tarreau72c28532009-01-22 18:56:50 +0100958 }
959 }
Willy Tarreau0f423a72010-05-03 10:50:54 +0200960 else if (filter & FILT_COUNT_STATUS) {
961 /* output all statuses in the form of <status> <occurrences> */
962 n = eb32_first(&timers[0]);
963 while (n) {
964 t = container_of(n, struct timer, node);
965 printf("%d %d\n", n->key, t->count);
Willy Tarreaua2b39fb2011-07-10 21:39:35 +0200966 lines_out++;
Willy Tarreau0f423a72010-05-03 10:50:54 +0200967 n = eb32_next(n);
968 }
969 }
Willy Tarreaua2b39fb2011-07-10 21:39:35 +0200970 else if (filter & FILT_COUNT_SRV_STATUS) {
Willy Tarreaud2201062010-05-27 18:17:30 +0200971 struct ebmb_node *srv_node;
972 struct srv_st *srv;
973
974 printf("#srv_name 1xx 2xx 3xx 4xx 5xx other tot_req req_ok pct_ok avg_ct avg_rt\n");
975
976 srv_node = ebmb_first(&timers[0]);
977 while (srv_node) {
978 int tot_rq;
979
980 srv = container_of(srv_node, struct srv_st, node);
981
982 tot_rq = 0;
983 for (f = 0; f <= 5; f++)
984 tot_rq += srv->st_cnt[f];
985
986 printf("%s %d %d %d %d %d %d %d %d %.1f %d %d\n",
987 srv_node->key, srv->st_cnt[1], srv->st_cnt[2],
988 srv->st_cnt[3], srv->st_cnt[4], srv->st_cnt[5], srv->st_cnt[0],
989 tot_rq,
990 srv->nb_ok, (double)srv->nb_ok * 100.0 / (tot_rq?tot_rq:1),
991 (int)(srv->cum_ct / (srv->nb_ct?srv->nb_ct:1)), (int)(srv->cum_rt / (srv->nb_rt?srv->nb_rt:1)));
992 srv_node = ebmb_next(srv_node);
Willy Tarreaua2b39fb2011-07-10 21:39:35 +0200993 lines_out++;
Willy Tarreaud2201062010-05-27 18:17:30 +0200994 }
995 }
Willy Tarreaud8fc1102010-09-12 17:56:16 +0200996 else if (filter & FILT_COUNT_TERM_CODES) {
997 /* output all statuses in the form of <code> <occurrences> */
998 n = eb32_first(&timers[0]);
999 while (n) {
1000 t = container_of(n, struct timer, node);
1001 printf("%c%c %d\n", (n->key >> 8), (n->key) & 255, t->count);
Willy Tarreaua2b39fb2011-07-10 21:39:35 +02001002 lines_out++;
Willy Tarreaud8fc1102010-09-12 17:56:16 +02001003 n = eb32_next(n);
1004 }
1005 }
Willy Tarreaua2b39fb2011-07-10 21:39:35 +02001006 else if (filter & FILT_COUNT_URL_ANY) {
Willy Tarreauabe45b62010-10-28 20:33:46 +02001007 struct eb_node *node, *next;
1008
1009 if (!(filter & FILT_COUNT_URL_ONLY)) {
1010 /* we have to sort on another criterion. We'll use timers[1] for the
1011 * destination tree.
1012 */
1013
1014 timers[1] = EB_ROOT; /* reconfigure to accept duplicates */
1015 for (node = eb_first(&timers[0]); node; node = next) {
1016 next = eb_next(node);
1017 eb_delete(node);
1018
1019 ustat = container_of(node, struct url_stat, node.url.node);
1020
1021 if (filter & FILT_COUNT_URL_COUNT)
1022 ustat->node.val.key = ustat->nb_req;
1023 else if (filter & FILT_COUNT_URL_ERR)
1024 ustat->node.val.key = ustat->nb_err;
1025 else if (filter & FILT_COUNT_URL_TTOT)
1026 ustat->node.val.key = ustat->total_time;
1027 else if (filter & FILT_COUNT_URL_TAVG)
1028 ustat->node.val.key = ustat->nb_req ? ustat->total_time / ustat->nb_req : 0;
1029 else if (filter & FILT_COUNT_URL_TTOTO)
1030 ustat->node.val.key = ustat->total_time_ok;
1031 else if (filter & FILT_COUNT_URL_TAVGO)
1032 ustat->node.val.key = (ustat->nb_req - ustat->nb_err) ? ustat->total_time_ok / (ustat->nb_req - ustat->nb_err) : 0;
1033 else
1034 ustat->node.val.key = 0;
1035
1036 eb64_insert(&timers[1], &ustat->node.val);
1037 }
1038 /* switch trees */
1039 timers[0] = timers[1];
1040 }
1041
1042 printf("#req err ttot tavg oktot okavg url\n");
1043
1044 /* scan the tree in its reverse sorting order */
1045 node = eb_last(&timers[0]);
1046 while (node) {
1047 ustat = container_of(node, struct url_stat, node.url.node);
1048 printf("%d %d %Ld %Ld %Ld %Ld %s\n",
1049 ustat->nb_req,
1050 ustat->nb_err,
1051 ustat->total_time,
1052 ustat->nb_req ? ustat->total_time / ustat->nb_req : 0,
1053 ustat->total_time_ok,
1054 (ustat->nb_req - ustat->nb_err) ? ustat->total_time_ok / (ustat->nb_req - ustat->nb_err) : 0,
1055 ustat->url);
1056
1057 node = eb_prev(node);
Willy Tarreaua2b39fb2011-07-10 21:39:35 +02001058 lines_out++;
Willy Tarreauabe45b62010-10-28 20:33:46 +02001059 }
1060 }
Willy Tarreaud2201062010-05-27 18:17:30 +02001061
Willy Tarreau910ba4b2009-11-17 10:16:19 +01001062 empty:
Willy Tarreau72c28532009-01-22 18:56:50 +01001063 if (!(filter & FILT_QUIET))
1064 fprintf(stderr, "%d lines in, %d lines out, %d parsing errors\n",
Willy Tarreaua2b39fb2011-07-10 21:39:35 +02001065 linenum, lines_out, parse_err);
Willy Tarreau72c28532009-01-22 18:56:50 +01001066 exit(0);
1067}
1068
Willy Tarreaua2b39fb2011-07-10 21:39:35 +02001069void filter_output_line(const char *accept_field, const char *time_field, struct timer **tptr)
1070{
1071 puts(line);
1072 lines_out++;
1073}
1074
1075void filter_accept_holes(const char *accept_field, const char *time_field, struct timer **tptr)
1076{
1077 struct timer *t2;
1078 int val;
1079
1080 val = convert_date(accept_field);
1081 if (unlikely(val < 0)) {
1082 truncated_line(linenum, line);
1083 return;
1084 }
1085
1086 t2 = insert_value(&timers[0], tptr, val);
1087 t2->count++;
1088 lines_out++;
1089 return;
1090}
1091
1092void filter_count_status(const char *accept_field, const char *time_field, struct timer **tptr)
1093{
1094 struct timer *t2;
1095 const char *b;
1096 int val;
1097
1098 if (time_field)
1099 b = field_start(time_field, STATUS_FIELD - TIME_FIELD + 1);
1100 else
1101 b = field_start(accept_field, STATUS_FIELD - ACCEPT_FIELD + 1);
1102
1103 if (unlikely(!*b)) {
1104 truncated_line(linenum, line);
1105 return;
1106 }
1107
1108 val = str2ic(b);
1109
1110 t2 = insert_value(&timers[0], tptr, val);
1111 t2->count++;
1112}
1113
1114void filter_count_term_codes(const char *accept_field, const char *time_field, struct timer **tptr)
1115{
1116 struct timer *t2;
1117 const char *b;
1118 int val;
1119
1120 if (time_field)
1121 b = field_start(time_field, TERM_CODES_FIELD - TIME_FIELD + 1);
1122 else
1123 b = field_start(accept_field, TERM_CODES_FIELD - ACCEPT_FIELD + 1);
1124
1125 if (unlikely(!*b)) {
1126 truncated_line(linenum, line);
1127 return;
1128 }
1129
1130 val = 256 * b[0] + b[1];
1131
1132 t2 = insert_value(&timers[0], tptr, val);
1133 t2->count++;
1134}
1135
1136void filter_count_srv_status(const char *accept_field, const char *time_field, struct timer **tptr)
1137{
1138 const char *b, *e, *p;
1139 int f, err, array[5];
1140 struct ebmb_node *srv_node;
1141 struct srv_st *srv;
1142 int val;
1143
1144 /* the server field is before the status field, so let's
1145 * parse them in the proper order.
1146 */
1147 b = field_start(accept_field, SERVER_FIELD - ACCEPT_FIELD + 1);
1148 if (unlikely(!*b)) {
1149 truncated_line(linenum, line);
1150 return;
1151 }
1152
1153 e = field_stop(b + 1); /* we have the server name in [b]..[e-1] */
1154
1155 /* the chance that a server name already exists is extremely high,
1156 * so let's perform a normal lookup first.
1157 */
1158 srv_node = ebst_lookup_len(&timers[0], b, e - b);
1159 srv = container_of(srv_node, struct srv_st, node);
1160
1161 if (!srv_node) {
1162 /* server not yet in the tree, let's create it */
1163 srv = (void *)calloc(1, sizeof(struct srv_st) + e - b + 1);
1164 srv_node = &srv->node;
1165 memcpy(&srv_node->key, b, e - b);
1166 srv_node->key[e - b] = '\0';
1167 ebst_insert(&timers[0], srv_node);
1168 }
1169
1170 /* let's collect the connect and response times */
1171 if (!time_field) {
1172 time_field = field_start(e, TIME_FIELD - SERVER_FIELD);
1173 if (unlikely(!*time_field)) {
1174 truncated_line(linenum, line);
1175 return;
1176 }
1177 }
1178
1179 e = field_stop(time_field + 1);
1180 /* we have field TIME_FIELD in [time_field]..[e-1] */
1181
1182 p = time_field;
1183 err = 0;
1184 f = 0;
1185 while (!SEP(*p)) {
1186 array[f] = str2ic(p);
1187 if (array[f] < 0) {
1188 array[f] = -1;
1189 err = 1;
1190 }
1191 if (++f == 5)
1192 break;
1193 SKIP_CHAR(p, '/');
1194 }
1195
1196 if (unlikely(f < 5)){
1197 parse_err++;
1198 return;
1199 }
1200
1201 /* OK we have our timers in array[2,3] */
1202 if (!err)
1203 srv->nb_ok++;
1204
1205 if (array[2] >= 0) {
1206 srv->cum_ct += array[2];
1207 srv->nb_ct++;
1208 }
1209
1210 if (array[3] >= 0) {
1211 srv->cum_rt += array[3];
1212 srv->nb_rt++;
1213 }
1214
1215 /* we're interested in the 5 HTTP status classes (1xx ... 5xx), and
1216 * the invalid ones which will be reported as 0.
1217 */
1218 b = field_start(e, STATUS_FIELD - TIME_FIELD);
1219 if (unlikely(!*b)) {
1220 truncated_line(linenum, line);
1221 return;
1222 }
1223
1224 val = 0;
1225 if (*b >= '1' && *b <= '5')
1226 val = *b - '0';
1227
1228 srv->st_cnt[val]++;
1229}
1230
1231void filter_count_url(const char *accept_field, const char *time_field, struct timer **tptr)
1232{
1233 struct url_stat *ustat = NULL;
1234 struct ebpt_node *ebpt_old;
1235 const char *b, *e;
1236 int f, err, array[5];
1237
1238 /* let's collect the response time */
1239 if (!time_field) {
1240 time_field = field_start(accept_field, TIME_FIELD - ACCEPT_FIELD + 1); // avg 115 ns per line
1241 if (unlikely(!*time_field)) {
1242 truncated_line(linenum, line);
1243 return;
1244 }
1245 }
1246
1247 /* we have the field TIME_FIELD starting at <time_field>. We'll
1248 * parse the 5 timers to detect errors, it takes avg 55 ns per line.
1249 */
1250 e = time_field; err = 0; f = 0;
1251 while (!SEP(*e)) {
1252 array[f] = str2ic(e);
1253 if (array[f] < 0) {
1254 array[f] = -1;
1255 err = 1;
1256 }
1257 if (++f == 5)
1258 break;
1259 SKIP_CHAR(e, '/');
1260 }
1261 if (f < 5) {
1262 parse_err++;
1263 return;
1264 }
1265
1266 /* OK we have our timers in array[3], and err is >0 if at
1267 * least one -1 was seen. <e> points to the first char of
1268 * the last timer. Let's prepare a new node with that.
1269 */
1270 if (unlikely(!ustat))
1271 ustat = calloc(1, sizeof(*ustat));
1272
1273 ustat->nb_err = err;
1274 ustat->nb_req = 1;
1275
1276 /* use array[4] = total time in case of error */
1277 ustat->total_time = (array[3] >= 0) ? array[3] : array[4];
1278 ustat->total_time_ok = (array[3] >= 0) ? array[3] : 0;
1279
1280 /* the line may be truncated because of a bad request or anything like this,
1281 * without a method. Also, if it does not begin with an quote, let's skip to
1282 * the next field because it's a capture. Let's fall back to the "method" itself
1283 * if there's nothing else.
1284 */
1285 e = field_start(e, METH_FIELD - TIME_FIELD + 1); // avg 100 ns per line
Willy Tarreau61a40c72011-09-06 08:11:27 +02001286 while (*e != '"' && *e) {
1287 /* Note: some syslog servers escape quotes ! */
1288 if (*e == '\\' && e[1] == '"')
1289 break;
Willy Tarreaua2b39fb2011-07-10 21:39:35 +02001290 e = field_start(e, 2);
Willy Tarreau61a40c72011-09-06 08:11:27 +02001291 }
Willy Tarreaua2b39fb2011-07-10 21:39:35 +02001292
1293 if (unlikely(!*e)) {
1294 truncated_line(linenum, line);
1295 return;
1296 }
1297
1298 b = field_start(e, URL_FIELD - METH_FIELD + 1); // avg 40 ns per line
1299 if (!*b)
1300 b = e;
1301
1302 /* stop at end of field or first ';' or '?', takes avg 64 ns per line */
1303 e = b;
1304 do {
Willy Tarreau14389e72011-07-10 22:11:17 +02001305 if (*e == ' ' || *e == '?' || *e == ';') {
Willy Tarreaua2b39fb2011-07-10 21:39:35 +02001306 *(char *)e = 0;
1307 break;
1308 }
1309 e++;
1310 } while (*e);
1311
1312 /* now instead of copying the URL for a simple lookup, we'll link
1313 * to it from the node we're trying to insert. If it returns a
1314 * different value, it was already there. Otherwise we just have
1315 * to dynamically realloc an entry using strdup().
1316 */
1317 ustat->node.url.key = (char *)b;
1318 ebpt_old = ebis_insert(&timers[0], &ustat->node.url);
1319
1320 if (ebpt_old != &ustat->node.url) {
1321 struct url_stat *ustat_old;
1322 /* node was already there, let's update previous one */
1323 ustat_old = container_of(ebpt_old, struct url_stat, node.url);
1324 ustat_old->nb_req ++;
1325 ustat_old->nb_err += ustat->nb_err;
1326 ustat_old->total_time += ustat->total_time;
1327 ustat_old->total_time_ok += ustat->total_time_ok;
1328 } else {
1329 ustat->url = ustat->node.url.key = strdup(ustat->node.url.key);
1330 ustat = NULL; /* node was used */
1331 }
1332}
1333
1334void filter_graphs(const char *accept_field, const char *time_field, struct timer **tptr)
1335{
1336 struct timer *t2;
1337 const char *e, *p;
1338 int f, err, array[5];
1339
1340 if (!time_field) {
1341 time_field = field_start(accept_field, TIME_FIELD - ACCEPT_FIELD + 1);
1342 if (unlikely(!*time_field)) {
1343 truncated_line(linenum, line);
1344 return;
1345 }
1346 }
1347
1348 e = field_stop(time_field + 1);
1349 /* we have field TIME_FIELD in [time_field]..[e-1] */
1350
1351 p = time_field;
1352 err = 0;
1353 f = 0;
1354 while (!SEP(*p)) {
1355 array[f] = str2ic(p);
1356 if (array[f] < 0) {
1357 array[f] = -1;
1358 err = 1;
1359 }
1360 if (++f == 5)
1361 break;
1362 SKIP_CHAR(p, '/');
1363 }
1364
1365 if (unlikely(f < 5)) {
1366 parse_err++;
1367 return;
1368 }
1369
1370 /* if we find at least one negative time, we count one error
1371 * with a time equal to the total session time. This will
1372 * emphasize quantum timing effects associated to known
1373 * timeouts. Note that on some buggy machines, it is possible
1374 * that the total time is negative, hence the reason to reset
1375 * it.
1376 */
1377
1378 if (filter & FILT_GRAPH_TIMERS) {
1379 if (err) {
1380 if (array[4] < 0)
1381 array[4] = -1;
1382 t2 = insert_timer(&timers[0], tptr, array[4]); // total time
1383 t2->count++;
1384 } else {
1385 int v;
1386
1387 t2 = insert_timer(&timers[1], tptr, array[0]); t2->count++; // req
1388 t2 = insert_timer(&timers[2], tptr, array[2]); t2->count++; // conn
1389 t2 = insert_timer(&timers[3], tptr, array[3]); t2->count++; // resp
1390
1391 v = array[4] - array[0] - array[1] - array[2] - array[3]; // data time
1392 if (v < 0 && !(filter & FILT_QUIET))
1393 fprintf(stderr, "ERR: %s (%d %d %d %d %d => %d)\n",
1394 line, array[0], array[1], array[2], array[3], array[4], v);
1395 t2 = insert_timer(&timers[4], tptr, v); t2->count++;
1396 lines_out++;
1397 }
1398 } else { /* percentile */
1399 if (err) {
1400 if (array[4] < 0)
1401 array[4] = -1;
1402 t2 = insert_value(&timers[0], tptr, array[4]); // total time
1403 t2->count++;
1404 } else {
1405 int v;
1406
1407 t2 = insert_value(&timers[1], tptr, array[0]); t2->count++; // req
1408 t2 = insert_value(&timers[2], tptr, array[2]); t2->count++; // conn
1409 t2 = insert_value(&timers[3], tptr, array[3]); t2->count++; // resp
1410
1411 v = array[4] - array[0] - array[1] - array[2] - array[3]; // data time
1412 if (v < 0 && !(filter & FILT_QUIET))
1413 fprintf(stderr, "ERR: %s (%d %d %d %d %d => %d)\n",
1414 line, array[0], array[1], array[2], array[3], array[4], v);
1415 t2 = insert_value(&timers[4], tptr, v); t2->count++;
1416 lines_out++;
1417 }
1418 }
1419}
1420
1421
Willy Tarreau72c28532009-01-22 18:56:50 +01001422/*
1423 * Local variables:
1424 * c-indent-level: 8
1425 * c-basic-offset: 8
1426 * End:
1427 */