blob: 08444fe22195bd133563613dcf6897d6cc6df3f9 [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
Baptiste61aaad02012-09-08 23:10:03 +020032#define BYTES_SENT_FIELD 11
Willy Tarreaud8fc1102010-09-12 17:56:16 +020033#define TERM_CODES_FIELD 14
Willy Tarreau72c28532009-01-22 18:56:50 +010034#define CONN_FIELD 15
Willy Tarreau08911ff2011-10-13 13:28:36 +020035#define QUEUE_LEN_FIELD 16
Willy Tarreauabe45b62010-10-28 20:33:46 +020036#define METH_FIELD 17
37#define URL_FIELD 18
Willy Tarreau72c28532009-01-22 18:56:50 +010038#define MAXLINE 16384
39#define QBITS 4
40
Willy Tarreaudf6f0d12011-07-10 18:15:08 +020041#define SEP(c) ((unsigned char)(c) <= ' ')
42#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 +010043
44/* [0] = err/date, [1] = req, [2] = conn, [3] = resp, [4] = data */
45static struct eb_root timers[5] = {
46 EB_ROOT_UNIQUE, EB_ROOT_UNIQUE, EB_ROOT_UNIQUE,
47 EB_ROOT_UNIQUE, EB_ROOT_UNIQUE,
48};
49
50struct timer {
51 struct eb32_node node;
52 unsigned int count;
53};
54
Willy Tarreaud2201062010-05-27 18:17:30 +020055struct srv_st {
56 unsigned int st_cnt[6]; /* 0xx to 5xx */
57 unsigned int nb_ct, nb_rt, nb_ok;
58 unsigned long long cum_ct, cum_rt;
59 struct ebmb_node node;
60 /* don't put anything else here, the server name will be there */
61};
Willy Tarreau72c28532009-01-22 18:56:50 +010062
Willy Tarreauabe45b62010-10-28 20:33:46 +020063struct url_stat {
64 union {
65 struct ebpt_node url;
66 struct eb64_node val;
67 } node;
68 char *url;
69 unsigned long long total_time; /* sum(all reqs' times) */
70 unsigned long long total_time_ok; /* sum(all OK reqs' times) */
Baptiste61aaad02012-09-08 23:10:03 +020071 unsigned long long total_bytes_sent; /* sum(all bytes sent) */
Willy Tarreauabe45b62010-10-28 20:33:46 +020072 unsigned int nb_err, nb_req;
73};
74
Willy Tarreau72c28532009-01-22 18:56:50 +010075#define FILT_COUNT_ONLY 0x01
76#define FILT_INVERT 0x02
77#define FILT_QUIET 0x04
78#define FILT_ERRORS_ONLY 0x08
79#define FILT_ACC_DELAY 0x10
80#define FILT_ACC_COUNT 0x20
81#define FILT_GRAPH_TIMERS 0x40
Willy Tarreau214c2032009-02-20 11:02:32 +010082#define FILT_PERCENTILE 0x80
Willy Tarreau5bdfd962009-10-14 15:16:29 +020083#define FILT_TIME_RESP 0x100
84
85#define FILT_INVERT_ERRORS 0x200
86#define FILT_INVERT_TIME_RESP 0x400
Willy Tarreau72c28532009-01-22 18:56:50 +010087
Willy Tarreau0f423a72010-05-03 10:50:54 +020088#define FILT_COUNT_STATUS 0x800
Willy Tarreaud2201062010-05-27 18:17:30 +020089#define FILT_COUNT_SRV_STATUS 0x1000
Willy Tarreaud8fc1102010-09-12 17:56:16 +020090#define FILT_COUNT_TERM_CODES 0x2000
Willy Tarreau0f423a72010-05-03 10:50:54 +020091
Willy Tarreauabe45b62010-10-28 20:33:46 +020092#define FILT_COUNT_URL_ONLY 0x004000
93#define FILT_COUNT_URL_COUNT 0x008000
94#define FILT_COUNT_URL_ERR 0x010000
95#define FILT_COUNT_URL_TTOT 0x020000
96#define FILT_COUNT_URL_TAVG 0x040000
97#define FILT_COUNT_URL_TTOTO 0x080000
98#define FILT_COUNT_URL_TAVGO 0x100000
Willy Tarreauabe45b62010-10-28 20:33:46 +020099
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
Baptiste61aaad02012-09-08 23:10:03 +0200109#define FILT_COUNT_URL_BAVG 0x10000000
110#define FILT_COUNT_URL_BTOT 0x20000000
111
112#define FILT_COUNT_URL_ANY (FILT_COUNT_URL_ONLY|FILT_COUNT_URL_COUNT|FILT_COUNT_URL_ERR| \
113 FILT_COUNT_URL_TTOT|FILT_COUNT_URL_TAVG|FILT_COUNT_URL_TTOTO|FILT_COUNT_URL_TAVGO| \
114 FILT_COUNT_URL_BAVG|FILT_COUNT_URL_BTOT)
115
Willy Tarreau72c28532009-01-22 18:56:50 +0100116unsigned int filter = 0;
117unsigned int filter_invert = 0;
Willy Tarreau214c2032009-02-20 11:02:32 +0100118const char *line;
Willy Tarreaua2b39fb2011-07-10 21:39:35 +0200119int linenum = 0;
120int parse_err = 0;
121int lines_out = 0;
Willy Tarreau72c28532009-01-22 18:56:50 +0100122
Willy Tarreau214c2032009-02-20 11:02:32 +0100123const char *fgets2(FILE *stream);
Willy Tarreau72c28532009-01-22 18:56:50 +0100124
Willy Tarreaua2b39fb2011-07-10 21:39:35 +0200125void filter_count_url(const char *accept_field, const char *time_field, struct timer **tptr);
126void filter_count_srv_status(const char *accept_field, const char *time_field, struct timer **tptr);
127void filter_count_term_codes(const char *accept_field, const char *time_field, struct timer **tptr);
128void filter_count_status(const char *accept_field, const char *time_field, struct timer **tptr);
129void filter_graphs(const char *accept_field, const char *time_field, struct timer **tptr);
130void filter_output_line(const char *accept_field, const char *time_field, struct timer **tptr);
131void filter_accept_holes(const char *accept_field, const char *time_field, struct timer **tptr);
132
Willy Tarreau615674c2012-01-23 08:15:51 +0100133void usage(FILE *output, const char *msg)
Willy Tarreau72c28532009-01-22 18:56:50 +0100134{
Willy Tarreau615674c2012-01-23 08:15:51 +0100135 fprintf(output,
Willy Tarreau72c28532009-01-22 18:56:50 +0100136 "%s"
Willy Tarreau615674c2012-01-23 08:15:51 +0100137 "Usage: halog [-h|--help] for long help\n"
Baptiste61aaad02012-09-08 23:10:03 +0200138 " halog [-q] [-c]\n"
139 " {-gt|-pct|-st|-tc|-srv|-u|-uc|-ue|-ua|-ut|-uao|-uto|-uba|-ubt}\n"
Hervé COMMOWICK927cddd2011-08-10 17:42:41 +0200140 " [-s <skip>] [-e|-E] [-H] [-rt|-RT <time>] [-ad <delay>] [-ac <count>]\n"
Baptiste61aaad02012-09-08 23:10:03 +0200141 " [-v] [-Q|-QS] [-tcn|-TCN <termcode>] [ -hs|-HS [min][:[max]] ] < log\n"
Willy Tarreau72c28532009-01-22 18:56:50 +0100142 "\n",
143 msg ? msg : ""
144 );
Willy Tarreau615674c2012-01-23 08:15:51 +0100145}
146
147void die(const char *msg)
148{
149 usage(stderr, msg);
Willy Tarreau72c28532009-01-22 18:56:50 +0100150 exit(1);
151}
152
Willy Tarreau615674c2012-01-23 08:15:51 +0100153void help()
154{
155 usage(stdout, NULL);
156 printf(
157 "Input filters (several filters may be combined) :\n"
158 " -H only match lines containing HTTP logs (ignore TCP)\n"
159 " -E only match lines without any error (no 5xx status)\n"
160 " -e only match lines with errors (status 5xx or negative)\n"
161 " -rt|-RT <time> only match response times larger|smaller than <time>\n"
162 " -Q|-QS only match queued requests (any queue|server queue)\n"
163 " -tcn|-TCN <code> only match requests with/without termination code <code>\n"
164 " -hs|-HS <[min][:][max]> only match requests with HTTP status codes within/not\n"
165 " within min..max. Any of them may be omitted. Exact\n"
166 " code is checked for if no ':' is specified.\n"
167 "Modifiers\n"
168 " -v invert the input filtering condition\n"
169 " -q don't report errors/warnings\n"
170 "\n"
171 "Output filters - only one may be used at a time\n"
172 " -c only report the number of lines that would have been printed\n"
173 " -pct output connect and response times percentiles\n"
174 " -st output number of requests per HTTP status code\n"
175 " -tc output number of requests per termination code (2 chars)\n"
176 " -srv output statistics per server (time, requests, errors)\n"
177 " -u* output statistics per URL (time, requests, errors)\n"
178 " Additional characters indicate the output sorting key :\n"
179 " -u : by URL, -uc : request count, -ue : error count\n"
180 " -ua : average response time, -uto : average total time\n"
181 " -uao, -uto: average times computed on valid ('OK') requests\n"
Baptiste61aaad02012-09-08 23:10:03 +0200182 " -uba, -ubt: average bytes returned, total bytes returned\n"
Willy Tarreau615674c2012-01-23 08:15:51 +0100183 );
184 exit(0);
185}
186
Willy Tarreau72c28532009-01-22 18:56:50 +0100187
188/* return pointer to first char not part of current field starting at <p>. */
Willy Tarreauf9042062011-09-10 12:26:35 +0200189
190#if defined(__i386__)
191/* this one is always faster on 32-bits */
192static inline const char *field_stop(const char *p)
193{
194 asm(
195 /* Look for spaces */
196 "4: \n\t"
197 "inc %0 \n\t"
198 "cmpb $0x20, -1(%0) \n\t"
199 "ja 4b \n\t"
200 "jz 3f \n\t"
201
202 /* we only get there for control chars 0..31. Leave if we find '\0' */
203 "cmpb $0x0, -1(%0) \n\t"
204 "jnz 4b \n\t"
205
206 /* return %0-1 = position of the last char we checked */
207 "3: \n\t"
208 "dec %0 \n\t"
209 : "=r" (p)
210 : "0" (p)
211 );
212 return p;
213}
214#else
Willy Tarreau72c28532009-01-22 18:56:50 +0100215const char *field_stop(const char *p)
216{
217 unsigned char c;
218
219 while (1) {
220 c = *(p++);
221 if (c > ' ')
222 continue;
Willy Tarreau14389e72011-07-10 22:11:17 +0200223 if (c == ' ' || c == 0)
Willy Tarreau72c28532009-01-22 18:56:50 +0100224 break;
225 }
226 return p - 1;
227}
Willy Tarreauf9042062011-09-10 12:26:35 +0200228#endif
Willy Tarreau72c28532009-01-22 18:56:50 +0100229
230/* return field <field> (starting from 1) in string <p>. Only consider
231 * contiguous spaces (or tabs) as one delimiter. May return pointer to
232 * last char if field is not found. Equivalent to awk '{print $field}'.
233 */
234const char *field_start(const char *p, int field)
235{
Willy Tarreauf9042062011-09-10 12:26:35 +0200236#ifndef PREFER_ASM
Willy Tarreau72c28532009-01-22 18:56:50 +0100237 unsigned char c;
238 while (1) {
239 /* skip spaces */
240 while (1) {
Willy Tarreauf9042062011-09-10 12:26:35 +0200241 c = *(p++);
Willy Tarreau72c28532009-01-22 18:56:50 +0100242 if (c > ' ')
243 break;
Willy Tarreau14389e72011-07-10 22:11:17 +0200244 if (c == ' ')
Willy Tarreauf9042062011-09-10 12:26:35 +0200245 continue;
Willy Tarreau72c28532009-01-22 18:56:50 +0100246 if (!c) /* end of line */
Willy Tarreauf9042062011-09-10 12:26:35 +0200247 return p-1;
Willy Tarreau72c28532009-01-22 18:56:50 +0100248 /* other char => new field */
249 break;
Willy Tarreau72c28532009-01-22 18:56:50 +0100250 }
251
252 /* start of field */
253 field--;
254 if (!field)
Willy Tarreauf9042062011-09-10 12:26:35 +0200255 return p-1;
Willy Tarreau72c28532009-01-22 18:56:50 +0100256
257 /* skip this field */
258 while (1) {
259 c = *(p++);
Willy Tarreau14389e72011-07-10 22:11:17 +0200260 if (c == ' ')
Willy Tarreau72c28532009-01-22 18:56:50 +0100261 break;
Willy Tarreauf9042062011-09-10 12:26:35 +0200262 if (c > ' ')
263 continue;
Willy Tarreau72c28532009-01-22 18:56:50 +0100264 if (c == '\0')
Willy Tarreauf9042062011-09-10 12:26:35 +0200265 return p - 1;
Willy Tarreau72c28532009-01-22 18:56:50 +0100266 }
267 }
Willy Tarreauf9042062011-09-10 12:26:35 +0200268#else
269 /* This version works optimally on i386 and x86_64 but the code above
270 * shows similar performance. However, depending on the version of GCC
271 * used, inlining rules change and it may have difficulties to make
272 * efficient use of this code at other locations and could result in
273 * worse performance (eg: gcc 4.4). You may want to experience.
274 */
275 asm(
276 /* skip spaces */
277 "1: \n\t"
278 "inc %0 \n\t"
279 "cmpb $0x20, -1(%0) \n\t"
280 "ja 2f \n\t"
281 "jz 1b \n\t"
282
283 /* we only get there for control chars 0..31. Leave if we find '\0' */
284 "cmpb $0x0, -1(%0) \n\t"
285 "jz 3f \n\t"
286
287 /* start of field at [%0-1]. Check if we need to skip more fields */
288 "2: \n\t"
289 "dec %1 \n\t"
290 "jz 3f \n\t"
291
292 /* Look for spaces */
293 "4: \n\t"
294 "inc %0 \n\t"
295 "cmpb $0x20, -1(%0) \n\t"
296 "jz 1b \n\t"
297 "ja 4b \n\t"
298
299 /* we only get there for control chars 0..31. Leave if we find '\0' */
300 "cmpb $0x0, -1(%0) \n\t"
301 "jnz 4b \n\t"
302
303 /* return %0-1 = position of the last char we checked */
304 "3: \n\t"
305 "dec %0 \n\t"
306 : "=r" (p)
307 : "r" (field), "0" (p)
308 );
309 return p;
310#endif
Willy Tarreau72c28532009-01-22 18:56:50 +0100311}
312
313/* keep only the <bits> higher bits of <i> */
314static inline unsigned int quantify_u32(unsigned int i, int bits)
315{
316 int high;
317
318 if (!bits)
319 return 0;
320
321 if (i)
322 high = fls_auto(i); // 1 to 32
323 else
324 high = 0;
325
326 if (high <= bits)
327 return i;
328
329 return i & ~((1 << (high - bits)) - 1);
330}
331
332/* keep only the <bits> higher bits of the absolute value of <i>, as well as
333 * its sign. */
334static inline int quantify(int i, int bits)
335{
336 if (i >= 0)
337 return quantify_u32(i, bits);
338 else
339 return -quantify_u32(-i, bits);
340}
341
342/* Insert timer value <v> into tree <r>. A pre-allocated node must be passed
343 * in <alloc>. It may be NULL, in which case the function will allocate it
344 * itself. It will be reset to NULL once consumed. The caller is responsible
345 * for freeing the node once not used anymore. The node where the value was
346 * inserted is returned.
347 */
348struct timer *insert_timer(struct eb_root *r, struct timer **alloc, int v)
349{
350 struct timer *t = *alloc;
351 struct eb32_node *n;
352
353 if (!t) {
354 t = calloc(sizeof(*t), 1);
355 if (unlikely(!t)) {
356 fprintf(stderr, "%s: not enough memory\n", __FUNCTION__);
357 exit(1);
358 }
359 }
360 t->node.key = quantify(v, QBITS); // keep only the higher QBITS bits
361
362 n = eb32i_insert(r, &t->node);
363 if (n == &t->node)
364 t = NULL; /* node inserted, will malloc next time */
365
366 *alloc = t;
367 return container_of(n, struct timer, node);
368}
369
370/* Insert value value <v> into tree <r>. A pre-allocated node must be passed
371 * in <alloc>. It may be NULL, in which case the function will allocate it
372 * itself. It will be reset to NULL once consumed. The caller is responsible
373 * for freeing the node once not used anymore. The node where the value was
374 * inserted is returned.
375 */
376struct timer *insert_value(struct eb_root *r, struct timer **alloc, int v)
377{
378 struct timer *t = *alloc;
379 struct eb32_node *n;
380
381 if (!t) {
382 t = calloc(sizeof(*t), 1);
383 if (unlikely(!t)) {
384 fprintf(stderr, "%s: not enough memory\n", __FUNCTION__);
385 exit(1);
386 }
387 }
388 t->node.key = v;
389
390 n = eb32i_insert(r, &t->node);
391 if (n == &t->node)
392 t = NULL; /* node inserted, will malloc next time */
393
394 *alloc = t;
395 return container_of(n, struct timer, node);
396}
397
398int str2ic(const char *s)
399{
400 int i = 0;
401 int j, k;
402
403 if (*s != '-') {
404 /* positive number */
405 while (1) {
406 j = (*s++) - '0';
407 k = i * 10;
408 if ((unsigned)j > 9)
409 break;
410 i = k + j;
411 }
412 } else {
413 /* negative number */
414 s++;
415 while (1) {
416 j = (*s++) - '0';
417 k = i * 10;
418 if ((unsigned)j > 9)
419 break;
420 i = k - j;
421 }
422 }
423
424 return i;
425}
426
427
428/* Equivalent to strtoul with a length. */
429static inline unsigned int __strl2ui(const char *s, int len)
430{
431 unsigned int i = 0;
432 while (len-- > 0) {
433 i = i * 10 - '0';
434 i += (unsigned char)*s++;
435 }
436 return i;
437}
438
439unsigned int strl2ui(const char *s, int len)
440{
441 return __strl2ui(s, len);
442}
443
444/* Convert "[04/Dec/2008:09:49:40.555]" to an integer equivalent to the time of
445 * the day in milliseconds. It returns -1 for all unparsable values. The parser
446 * looks ugly but gcc emits far better code that way.
447 */
448int convert_date(const char *field)
449{
450 unsigned int h, m, s, ms;
451 unsigned char c;
452 const char *b, *e;
453
454 h = m = s = ms = 0;
455 e = field;
456
457 /* skip the date */
458 while (1) {
459 c = *(e++);
460 if (c == ':')
461 break;
462 if (!c)
463 goto out_err;
464 }
465
466 /* hour + ':' */
467 b = e;
468 while (1) {
469 c = *(e++) - '0';
470 if (c > 9)
471 break;
472 h = h * 10 + c;
473 }
474 if (c == (unsigned char)(0 - '0'))
475 goto out_err;
476
477 /* minute + ':' */
478 b = e;
479 while (1) {
480 c = *(e++) - '0';
481 if (c > 9)
482 break;
483 m = m * 10 + c;
484 }
485 if (c == (unsigned char)(0 - '0'))
486 goto out_err;
487
488 /* second + '.' or ']' */
489 b = e;
490 while (1) {
491 c = *(e++) - '0';
492 if (c > 9)
493 break;
494 s = s * 10 + c;
495 }
496 if (c == (unsigned char)(0 - '0'))
497 goto out_err;
498
499 /* if there's a '.', we have milliseconds */
500 if (c == (unsigned char)('.' - '0')) {
501 /* millisecond second + ']' */
502 b = e;
503 while (1) {
504 c = *(e++) - '0';
505 if (c > 9)
506 break;
507 ms = ms * 10 + c;
508 }
509 if (c == (unsigned char)(0 - '0'))
510 goto out_err;
511 }
512 return (((h * 60) + m) * 60 + s) * 1000 + ms;
513 out_err:
514 return -1;
515}
516
517void truncated_line(int linenum, const char *line)
518{
519 if (!(filter & FILT_QUIET))
520 fprintf(stderr, "Truncated line %d: %s\n", linenum, line);
521}
522
523int main(int argc, char **argv)
524{
Willy Tarreau26deaf52011-07-10 19:47:48 +0200525 const char *b, *e, *p, *time_field, *accept_field;
Hervé COMMOWICK927cddd2011-08-10 17:42:41 +0200526 const char *filter_term_code_name = NULL;
Willy Tarreau72c28532009-01-22 18:56:50 +0100527 const char *output_file = NULL;
Willy Tarreaua2b39fb2011-07-10 21:39:35 +0200528 int f, last, err;
529 struct timer *t = NULL;
Willy Tarreau72c28532009-01-22 18:56:50 +0100530 struct eb32_node *n;
Willy Tarreauabe45b62010-10-28 20:33:46 +0200531 struct url_stat *ustat = NULL;
Willy Tarreau72c28532009-01-22 18:56:50 +0100532 int val, test;
Willy Tarreau72c28532009-01-22 18:56:50 +0100533 int filter_acc_delay = 0, filter_acc_count = 0;
Willy Tarreau5bdfd962009-10-14 15:16:29 +0200534 int filter_time_resp = 0;
Willy Tarreaud3007ff2011-09-05 02:07:23 +0200535 int filt_http_status_low = 0, filt_http_status_high = 0;
Willy Tarreau72c28532009-01-22 18:56:50 +0100536 int skip_fields = 1;
537
Willy Tarreaua2b39fb2011-07-10 21:39:35 +0200538 void (*line_filter)(const char *accept_field, const char *time_field, struct timer **tptr) = NULL;
539
Willy Tarreau72c28532009-01-22 18:56:50 +0100540 argc--; argv++;
541 while (argc > 0) {
542 if (*argv[0] != '-')
543 break;
544
545 if (strcmp(argv[0], "-ad") == 0) {
546 if (argc < 2) die("missing option for -ad");
547 argc--; argv++;
548 filter |= FILT_ACC_DELAY;
549 filter_acc_delay = atol(*argv);
550 }
551 else if (strcmp(argv[0], "-ac") == 0) {
552 if (argc < 2) die("missing option for -ac");
553 argc--; argv++;
554 filter |= FILT_ACC_COUNT;
555 filter_acc_count = atol(*argv);
556 }
Willy Tarreau5bdfd962009-10-14 15:16:29 +0200557 else if (strcmp(argv[0], "-rt") == 0) {
558 if (argc < 2) die("missing option for -rt");
559 argc--; argv++;
560 filter |= FILT_TIME_RESP;
561 filter_time_resp = atol(*argv);
562 }
563 else if (strcmp(argv[0], "-RT") == 0) {
564 if (argc < 2) die("missing option for -RT");
565 argc--; argv++;
566 filter |= FILT_TIME_RESP | FILT_INVERT_TIME_RESP;
567 filter_time_resp = atol(*argv);
568 }
Willy Tarreau72c28532009-01-22 18:56:50 +0100569 else if (strcmp(argv[0], "-s") == 0) {
570 if (argc < 2) die("missing option for -s");
571 argc--; argv++;
572 skip_fields = atol(*argv);
573 }
574 else if (strcmp(argv[0], "-e") == 0)
575 filter |= FILT_ERRORS_ONLY;
Willy Tarreau5bdfd962009-10-14 15:16:29 +0200576 else if (strcmp(argv[0], "-E") == 0)
577 filter |= FILT_ERRORS_ONLY | FILT_INVERT_ERRORS;
Willy Tarreau70c428f2011-07-10 17:27:40 +0200578 else if (strcmp(argv[0], "-H") == 0)
579 filter |= FILT_HTTP_ONLY;
Willy Tarreau08911ff2011-10-13 13:28:36 +0200580 else if (strcmp(argv[0], "-Q") == 0)
581 filter |= FILT_QUEUE_ONLY;
582 else if (strcmp(argv[0], "-QS") == 0)
583 filter |= FILT_QUEUE_SRV_ONLY;
Willy Tarreau72c28532009-01-22 18:56:50 +0100584 else if (strcmp(argv[0], "-c") == 0)
585 filter |= FILT_COUNT_ONLY;
586 else if (strcmp(argv[0], "-q") == 0)
587 filter |= FILT_QUIET;
588 else if (strcmp(argv[0], "-v") == 0)
589 filter_invert = !filter_invert;
590 else if (strcmp(argv[0], "-gt") == 0)
591 filter |= FILT_GRAPH_TIMERS;
Willy Tarreau214c2032009-02-20 11:02:32 +0100592 else if (strcmp(argv[0], "-pct") == 0)
593 filter |= FILT_PERCENTILE;
Willy Tarreau0f423a72010-05-03 10:50:54 +0200594 else if (strcmp(argv[0], "-st") == 0)
595 filter |= FILT_COUNT_STATUS;
Willy Tarreaud2201062010-05-27 18:17:30 +0200596 else if (strcmp(argv[0], "-srv") == 0)
597 filter |= FILT_COUNT_SRV_STATUS;
Willy Tarreaud8fc1102010-09-12 17:56:16 +0200598 else if (strcmp(argv[0], "-tc") == 0)
599 filter |= FILT_COUNT_TERM_CODES;
Hervé COMMOWICK927cddd2011-08-10 17:42:41 +0200600 else if (strcmp(argv[0], "-tcn") == 0) {
601 if (argc < 2) die("missing option for -tcn");
602 argc--; argv++;
603 filter |= FILT_TERM_CODE_NAME;
604 filter_term_code_name = *argv;
605 }
606 else if (strcmp(argv[0], "-TCN") == 0) {
607 if (argc < 2) die("missing option for -TCN");
608 argc--; argv++;
609 filter |= FILT_TERM_CODE_NAME | FILT_INVERT_TERM_CODE_NAME;
610 filter_term_code_name = *argv;
611 }
Willy Tarreaud3007ff2011-09-05 02:07:23 +0200612 else if (strcmp(argv[0], "-hs") == 0 || strcmp(argv[0], "-HS") == 0) {
613 char *sep, *str;
614
615 if (argc < 2) die("missing option for -hs/-HS ([min]:[max])");
616 filter |= FILT_HTTP_STATUS;
617 if (argv[0][1] == 'H')
618 filter |= FILT_INVERT_HTTP_STATUS;
619
620 argc--; argv++;
621 str = *argv;
622 sep = strchr(str, ':'); /* [min]:[max] */
623 if (!sep)
624 sep = str; /* make max point to min */
625 else
626 *sep++ = 0;
627 filt_http_status_low = *str ? atol(str) : 0;
628 filt_http_status_high = *sep ? atol(sep) : 65535;
629 }
Willy Tarreauabe45b62010-10-28 20:33:46 +0200630 else if (strcmp(argv[0], "-u") == 0)
631 filter |= FILT_COUNT_URL_ONLY;
632 else if (strcmp(argv[0], "-uc") == 0)
633 filter |= FILT_COUNT_URL_COUNT;
634 else if (strcmp(argv[0], "-ue") == 0)
635 filter |= FILT_COUNT_URL_ERR;
636 else if (strcmp(argv[0], "-ua") == 0)
637 filter |= FILT_COUNT_URL_TAVG;
638 else if (strcmp(argv[0], "-ut") == 0)
639 filter |= FILT_COUNT_URL_TTOT;
640 else if (strcmp(argv[0], "-uao") == 0)
641 filter |= FILT_COUNT_URL_TAVGO;
642 else if (strcmp(argv[0], "-uto") == 0)
643 filter |= FILT_COUNT_URL_TTOTO;
Baptiste61aaad02012-09-08 23:10:03 +0200644 else if (strcmp(argv[0], "-uba") == 0)
645 filter |= FILT_COUNT_URL_BAVG;
646 else if (strcmp(argv[0], "-ubt") == 0)
647 filter |= FILT_COUNT_URL_BTOT;
Willy Tarreau72c28532009-01-22 18:56:50 +0100648 else if (strcmp(argv[0], "-o") == 0) {
649 if (output_file)
650 die("Fatal: output file name already specified.\n");
651 if (argc < 2)
652 die("Fatal: missing output file name.\n");
653 output_file = argv[1];
654 }
Willy Tarreau615674c2012-01-23 08:15:51 +0100655 else if (strcmp(argv[0], "-h") == 0 || strcmp(argv[0], "--help") == 0)
656 help();
Willy Tarreau72c28532009-01-22 18:56:50 +0100657 argc--;
658 argv++;
659 }
660
661 if (!filter)
662 die("No action specified.\n");
663
664 if (filter & FILT_ACC_COUNT && !filter_acc_count)
665 filter_acc_count=1;
666
667 if (filter & FILT_ACC_DELAY && !filter_acc_delay)
668 filter_acc_delay = 1;
669
Willy Tarreaua2b39fb2011-07-10 21:39:35 +0200670
671 /* by default, all lines are printed */
672 line_filter = filter_output_line;
673 if (filter & (FILT_ACC_COUNT|FILT_ACC_DELAY))
674 line_filter = filter_accept_holes;
675 else if (filter & (FILT_GRAPH_TIMERS|FILT_PERCENTILE))
676 line_filter = filter_graphs;
677 else if (filter & FILT_COUNT_STATUS)
678 line_filter = filter_count_status;
679 else if (filter & FILT_COUNT_TERM_CODES)
680 line_filter = filter_count_term_codes;
681 else if (filter & FILT_COUNT_SRV_STATUS)
682 line_filter = filter_count_srv_status;
683 else if (filter & FILT_COUNT_URL_ANY)
684 line_filter = filter_count_url;
685 else if (filter & FILT_COUNT_ONLY)
686 line_filter = NULL;
Willy Tarreau72c28532009-01-22 18:56:50 +0100687
Willy Tarreauf8c95d22012-06-12 09:16:56 +0200688#if defined(POSIX_FADV_SEQUENTIAL)
689 /* around 20% performance improvement is observed on Linux with this
690 * on cold-cache. Surprizingly, WILLNEED is less performant. Don't
691 * use NOREUSE as it flushes the cache and prevents easy data
692 * manipulation on logs!
693 */
694 posix_fadvise(0, 0, 0, POSIX_FADV_SEQUENTIAL);
695#endif
696
Willy Tarreaue1a908c2012-01-03 09:23:03 +0100697 if (!line_filter &&
698 !(filter & (FILT_HTTP_ONLY|FILT_TIME_RESP|FILT_ERRORS_ONLY|FILT_HTTP_STATUS|FILT_QUEUE_ONLY|FILT_QUEUE_SRV_ONLY|FILT_TERM_CODE_NAME))) {
699 /* read the whole file at once first */
700 if (!filter_invert)
701 while (fgets2(stdin) != NULL)
702 lines_out++;
703
704 goto skip_filters;
705 }
706
Willy Tarreau214c2032009-02-20 11:02:32 +0100707 while ((line = fgets2(stdin)) != NULL) {
Willy Tarreau72c28532009-01-22 18:56:50 +0100708 linenum++;
Willy Tarreau26deaf52011-07-10 19:47:48 +0200709 time_field = NULL; accept_field = NULL;
Willy Tarreau72c28532009-01-22 18:56:50 +0100710
Willy Tarreau5bdfd962009-10-14 15:16:29 +0200711 test = 1;
Willy Tarreau26deaf52011-07-10 19:47:48 +0200712
713 /* for any line we process, we first ensure that there is a field
714 * looking like the accept date field (beginning with a '[').
715 */
716 accept_field = field_start(line, ACCEPT_FIELD + skip_fields);
Willy Tarreaua2b39fb2011-07-10 21:39:35 +0200717 if (unlikely(*accept_field != '[')) {
Willy Tarreau26deaf52011-07-10 19:47:48 +0200718 parse_err++;
719 continue;
720 }
721
722 /* the day of month field is begin 01 and 31 */
Willy Tarreaua2b39fb2011-07-10 21:39:35 +0200723 if (accept_field[1] < '0' || accept_field[1] > '3') {
Willy Tarreau26deaf52011-07-10 19:47:48 +0200724 parse_err++;
725 continue;
726 }
727
Willy Tarreaua2b39fb2011-07-10 21:39:35 +0200728 if (filter & FILT_HTTP_ONLY) {
Willy Tarreau70c428f2011-07-10 17:27:40 +0200729 /* only report lines with at least 4 timers */
Willy Tarreaua2b39fb2011-07-10 21:39:35 +0200730 if (!time_field) {
Willy Tarreau26deaf52011-07-10 19:47:48 +0200731 time_field = field_start(accept_field, TIME_FIELD - ACCEPT_FIELD + 1);
Willy Tarreaua2b39fb2011-07-10 21:39:35 +0200732 if (unlikely(!*time_field)) {
Willy Tarreau26deaf52011-07-10 19:47:48 +0200733 truncated_line(linenum, line);
734 continue;
735 }
Willy Tarreau70c428f2011-07-10 17:27:40 +0200736 }
737
Willy Tarreau758a6ea2011-07-10 18:53:44 +0200738 e = field_stop(time_field + 1);
739 /* we have field TIME_FIELD in [time_field]..[e-1] */
740 p = time_field;
Willy Tarreau70c428f2011-07-10 17:27:40 +0200741 f = 0;
Willy Tarreaudf6f0d12011-07-10 18:15:08 +0200742 while (!SEP(*p)) {
Willy Tarreau70c428f2011-07-10 17:27:40 +0200743 if (++f == 4)
744 break;
745 SKIP_CHAR(p, '/');
746 }
747 test &= (f >= 4);
748 }
749
Willy Tarreaua2b39fb2011-07-10 21:39:35 +0200750 if (filter & FILT_TIME_RESP) {
Willy Tarreau5bdfd962009-10-14 15:16:29 +0200751 int tps;
752
753 /* only report lines with response times larger than filter_time_resp */
Willy Tarreaua2b39fb2011-07-10 21:39:35 +0200754 if (!time_field) {
Willy Tarreau26deaf52011-07-10 19:47:48 +0200755 time_field = field_start(accept_field, TIME_FIELD - ACCEPT_FIELD + 1);
Willy Tarreaua2b39fb2011-07-10 21:39:35 +0200756 if (unlikely(!*time_field)) {
Willy Tarreau26deaf52011-07-10 19:47:48 +0200757 truncated_line(linenum, line);
758 continue;
759 }
Willy Tarreau5bdfd962009-10-14 15:16:29 +0200760 }
761
Willy Tarreau758a6ea2011-07-10 18:53:44 +0200762 e = field_stop(time_field + 1);
763 /* we have field TIME_FIELD in [time_field]..[e-1], let's check only the response time */
Willy Tarreau5bdfd962009-10-14 15:16:29 +0200764
Willy Tarreau758a6ea2011-07-10 18:53:44 +0200765 p = time_field;
Willy Tarreau5bdfd962009-10-14 15:16:29 +0200766 err = 0;
Willy Tarreau24bcb4f2010-10-28 20:39:50 +0200767 f = 0;
Willy Tarreaudf6f0d12011-07-10 18:15:08 +0200768 while (!SEP(*p)) {
Willy Tarreau5bdfd962009-10-14 15:16:29 +0200769 tps = str2ic(p);
770 if (tps < 0) {
771 tps = -1;
772 err = 1;
773 }
Willy Tarreau24bcb4f2010-10-28 20:39:50 +0200774 if (++f == 4)
775 break;
Willy Tarreau5bdfd962009-10-14 15:16:29 +0200776 SKIP_CHAR(p, '/');
777 }
778
Willy Tarreaua2b39fb2011-07-10 21:39:35 +0200779 if (unlikely(f < 4)) {
Willy Tarreau5bdfd962009-10-14 15:16:29 +0200780 parse_err++;
781 continue;
782 }
783
784 test &= (tps >= filter_time_resp) ^ !!(filter & FILT_INVERT_TIME_RESP);
785 }
786
Willy Tarreaud3007ff2011-09-05 02:07:23 +0200787 if (filter & (FILT_ERRORS_ONLY | FILT_HTTP_STATUS)) {
788 /* Check both error codes (-1, 5xx) and status code ranges */
Willy Tarreau26deaf52011-07-10 19:47:48 +0200789 if (time_field)
790 b = field_start(time_field, STATUS_FIELD - TIME_FIELD + 1);
791 else
792 b = field_start(accept_field, STATUS_FIELD - ACCEPT_FIELD + 1);
793
Willy Tarreaua2b39fb2011-07-10 21:39:35 +0200794 if (unlikely(!*b)) {
Willy Tarreau72c28532009-01-22 18:56:50 +0100795 truncated_line(linenum, line);
796 continue;
797 }
Willy Tarreaua2b39fb2011-07-10 21:39:35 +0200798
Willy Tarreaud3007ff2011-09-05 02:07:23 +0200799 val = str2ic(b);
800 if (filter & FILT_ERRORS_ONLY)
801 test &= (val < 0 || (val >= 500 && val <= 599)) ^ !!(filter & FILT_INVERT_ERRORS);
802
803 if (filter & FILT_HTTP_STATUS)
804 test &= (val >= filt_http_status_low && val <= filt_http_status_high) ^ !!(filter & FILT_INVERT_HTTP_STATUS);
Willy Tarreau72c28532009-01-22 18:56:50 +0100805 }
806
Willy Tarreau08911ff2011-10-13 13:28:36 +0200807 if (filter & (FILT_QUEUE_ONLY|FILT_QUEUE_SRV_ONLY)) {
808 /* Check if the server's queue is non-nul */
809 if (time_field)
810 b = field_start(time_field, QUEUE_LEN_FIELD - TIME_FIELD + 1);
811 else
812 b = field_start(accept_field, QUEUE_LEN_FIELD - ACCEPT_FIELD + 1);
813
814 if (unlikely(!*b)) {
815 truncated_line(linenum, line);
816 continue;
817 }
818
819 if (*b == '0') {
820 if (filter & FILT_QUEUE_SRV_ONLY) {
821 test = 0;
822 }
823 else {
824 do {
825 b++;
826 if (*b == '/') {
827 b++;
828 break;
829 }
830 } while (*b);
831 test &= ((unsigned char)(*b - '1') < 9);
832 }
833 }
834 }
835
Hervé COMMOWICK927cddd2011-08-10 17:42:41 +0200836 if (filter & FILT_TERM_CODE_NAME) {
837 /* only report corresponding termination code name */
838 if (time_field)
839 b = field_start(time_field, TERM_CODES_FIELD - TIME_FIELD + 1);
840 else
841 b = field_start(accept_field, TERM_CODES_FIELD - ACCEPT_FIELD + 1);
842
843 if (unlikely(!*b)) {
844 truncated_line(linenum, line);
845 continue;
846 }
847
848 test &= (b[0] == filter_term_code_name[0] && b[1] == filter_term_code_name[1]) ^ !!(filter & FILT_INVERT_TERM_CODE_NAME);
849 }
850
851
Willy Tarreau0f423a72010-05-03 10:50:54 +0200852 test ^= filter_invert;
853 if (!test)
854 continue;
855
Willy Tarreaua2b39fb2011-07-10 21:39:35 +0200856 /************** here we process inputs *******************/
Willy Tarreau72c28532009-01-22 18:56:50 +0100857
Willy Tarreaua2b39fb2011-07-10 21:39:35 +0200858 if (line_filter)
859 line_filter(accept_field, time_field, &t);
860 else
861 lines_out++; /* we're just counting lines */
862 }
Willy Tarreauabe45b62010-10-28 20:33:46 +0200863
Willy Tarreaue1a908c2012-01-03 09:23:03 +0100864 skip_filters:
Willy Tarreaua2b39fb2011-07-10 21:39:35 +0200865 /*****************************************************
866 * Here we've finished reading all input. Depending on the
867 * filters, we may still have some analysis to run on the
868 * collected data and to output data in a new format.
869 *************************************************** */
Willy Tarreau72c28532009-01-22 18:56:50 +0100870
871 if (t)
872 free(t);
873
874 if (filter & FILT_COUNT_ONLY) {
Willy Tarreaua2b39fb2011-07-10 21:39:35 +0200875 printf("%d\n", lines_out);
Willy Tarreau72c28532009-01-22 18:56:50 +0100876 exit(0);
877 }
878
Willy Tarreau72c28532009-01-22 18:56:50 +0100879 if (filter & (FILT_ACC_COUNT|FILT_ACC_DELAY)) {
880 /* sort and count all timers. Output will look like this :
881 * <accept_date> <delta_ms from previous one> <nb entries>
882 */
883 n = eb32_first(&timers[0]);
884
885 if (n)
886 last = n->key;
887 while (n) {
888 unsigned int d, h, m, s, ms;
889
890 t = container_of(n, struct timer, node);
891 h = n->key;
892 d = h - last;
893 last = h;
894
895 if (d >= filter_acc_delay && t->count >= filter_acc_count) {
896 ms = h % 1000; h = h / 1000;
897 s = h % 60; h = h / 60;
898 m = h % 60; h = h / 60;
Willy Tarreaua2b39fb2011-07-10 21:39:35 +0200899 lines_out++;
Willy Tarreau72c28532009-01-22 18:56:50 +0100900 printf("%02d:%02d:%02d.%03d %d %d %d\n", h, m, s, ms, last, d, t->count);
901 }
902 n = eb32_next(n);
903 }
904 }
905 else if (filter & FILT_GRAPH_TIMERS) {
906 /* sort all timers */
907 for (f = 0; f < 5; f++) {
908 struct eb32_node *n;
909 int val;
910
911 val = 0;
912 n = eb32_first(&timers[f]);
913 while (n) {
914 int i;
915 double d;
916
917 t = container_of(n, struct timer, node);
918 last = n->key;
919 val = t->count;
920
921 i = (last < 0) ? -last : last;
922 i = fls_auto(i) - QBITS;
923
924 if (i > 0)
925 d = val / (double)(1 << i);
926 else
927 d = val;
928
929 if (d > 0.0) {
930 printf("%d %d %f\n", f, last, d+1.0);
Willy Tarreaua2b39fb2011-07-10 21:39:35 +0200931 lines_out++;
Willy Tarreau72c28532009-01-22 18:56:50 +0100932 }
933
934 n = eb32_next(n);
935 }
Willy Tarreau214c2032009-02-20 11:02:32 +0100936 }
937 }
938 else if (filter & FILT_PERCENTILE) {
939 /* report timers by percentile :
940 * <percent> <total> <max_req_time> <max_conn_time> <max_resp_time> <max_data_time>
941 * We don't count errs.
942 */
943 struct eb32_node *n[5];
944 unsigned long cum[5];
945 double step;
946
Willy Tarreaua2b39fb2011-07-10 21:39:35 +0200947 if (!lines_out)
Willy Tarreau910ba4b2009-11-17 10:16:19 +0100948 goto empty;
949
Willy Tarreau214c2032009-02-20 11:02:32 +0100950 for (f = 1; f < 5; f++) {
951 n[f] = eb32_first(&timers[f]);
952 cum[f] = container_of(n[f], struct timer, node)->count;
953 }
954
955 for (step = 1; step <= 1000;) {
Willy Tarreaua2b39fb2011-07-10 21:39:35 +0200956 unsigned int thres = lines_out * (step / 1000.0);
Willy Tarreau214c2032009-02-20 11:02:32 +0100957
958 printf("%3.1f %d ", step/10.0, thres);
959 for (f = 1; f < 5; f++) {
960 struct eb32_node *next;
961 while (cum[f] < thres) {
962 /* need to find other keys */
963 next = eb32_next(n[f]);
964 if (!next)
965 break;
966 n[f] = next;
967 cum[f] += container_of(next, struct timer, node)->count;
968 }
969
970 /* value still within $step % of total */
971 printf("%d ", n[f]->key);
972 }
973 putchar('\n');
974 if (step >= 100 && step < 900)
975 step += 50; // jump 5% by 5% between those steps.
976 else if (step >= 20 && step < 980)
977 step += 10;
978 else
979 step += 1;
Willy Tarreau72c28532009-01-22 18:56:50 +0100980 }
981 }
Willy Tarreau0f423a72010-05-03 10:50:54 +0200982 else if (filter & FILT_COUNT_STATUS) {
983 /* output all statuses in the form of <status> <occurrences> */
984 n = eb32_first(&timers[0]);
985 while (n) {
986 t = container_of(n, struct timer, node);
987 printf("%d %d\n", n->key, t->count);
Willy Tarreaua2b39fb2011-07-10 21:39:35 +0200988 lines_out++;
Willy Tarreau0f423a72010-05-03 10:50:54 +0200989 n = eb32_next(n);
990 }
991 }
Willy Tarreaua2b39fb2011-07-10 21:39:35 +0200992 else if (filter & FILT_COUNT_SRV_STATUS) {
Willy Tarreaud2201062010-05-27 18:17:30 +0200993 struct ebmb_node *srv_node;
994 struct srv_st *srv;
995
996 printf("#srv_name 1xx 2xx 3xx 4xx 5xx other tot_req req_ok pct_ok avg_ct avg_rt\n");
997
998 srv_node = ebmb_first(&timers[0]);
999 while (srv_node) {
1000 int tot_rq;
1001
1002 srv = container_of(srv_node, struct srv_st, node);
1003
1004 tot_rq = 0;
1005 for (f = 0; f <= 5; f++)
1006 tot_rq += srv->st_cnt[f];
1007
1008 printf("%s %d %d %d %d %d %d %d %d %.1f %d %d\n",
1009 srv_node->key, srv->st_cnt[1], srv->st_cnt[2],
1010 srv->st_cnt[3], srv->st_cnt[4], srv->st_cnt[5], srv->st_cnt[0],
1011 tot_rq,
1012 srv->nb_ok, (double)srv->nb_ok * 100.0 / (tot_rq?tot_rq:1),
1013 (int)(srv->cum_ct / (srv->nb_ct?srv->nb_ct:1)), (int)(srv->cum_rt / (srv->nb_rt?srv->nb_rt:1)));
1014 srv_node = ebmb_next(srv_node);
Willy Tarreaua2b39fb2011-07-10 21:39:35 +02001015 lines_out++;
Willy Tarreaud2201062010-05-27 18:17:30 +02001016 }
1017 }
Willy Tarreaud8fc1102010-09-12 17:56:16 +02001018 else if (filter & FILT_COUNT_TERM_CODES) {
1019 /* output all statuses in the form of <code> <occurrences> */
1020 n = eb32_first(&timers[0]);
1021 while (n) {
1022 t = container_of(n, struct timer, node);
1023 printf("%c%c %d\n", (n->key >> 8), (n->key) & 255, t->count);
Willy Tarreaua2b39fb2011-07-10 21:39:35 +02001024 lines_out++;
Willy Tarreaud8fc1102010-09-12 17:56:16 +02001025 n = eb32_next(n);
1026 }
1027 }
Willy Tarreaua2b39fb2011-07-10 21:39:35 +02001028 else if (filter & FILT_COUNT_URL_ANY) {
Willy Tarreauabe45b62010-10-28 20:33:46 +02001029 struct eb_node *node, *next;
1030
1031 if (!(filter & FILT_COUNT_URL_ONLY)) {
1032 /* we have to sort on another criterion. We'll use timers[1] for the
1033 * destination tree.
1034 */
1035
1036 timers[1] = EB_ROOT; /* reconfigure to accept duplicates */
1037 for (node = eb_first(&timers[0]); node; node = next) {
1038 next = eb_next(node);
1039 eb_delete(node);
1040
1041 ustat = container_of(node, struct url_stat, node.url.node);
1042
1043 if (filter & FILT_COUNT_URL_COUNT)
1044 ustat->node.val.key = ustat->nb_req;
1045 else if (filter & FILT_COUNT_URL_ERR)
1046 ustat->node.val.key = ustat->nb_err;
1047 else if (filter & FILT_COUNT_URL_TTOT)
1048 ustat->node.val.key = ustat->total_time;
1049 else if (filter & FILT_COUNT_URL_TAVG)
1050 ustat->node.val.key = ustat->nb_req ? ustat->total_time / ustat->nb_req : 0;
1051 else if (filter & FILT_COUNT_URL_TTOTO)
1052 ustat->node.val.key = ustat->total_time_ok;
1053 else if (filter & FILT_COUNT_URL_TAVGO)
1054 ustat->node.val.key = (ustat->nb_req - ustat->nb_err) ? ustat->total_time_ok / (ustat->nb_req - ustat->nb_err) : 0;
Baptiste61aaad02012-09-08 23:10:03 +02001055 else if (filter & FILT_COUNT_URL_BAVG)
1056 ustat->node.val.key = ustat->nb_req ? ustat->total_bytes_sent / ustat->nb_req : 0;
1057 else if (filter & FILT_COUNT_URL_BTOT)
1058 ustat->node.val.key = ustat->total_bytes_sent;
Willy Tarreauabe45b62010-10-28 20:33:46 +02001059 else
1060 ustat->node.val.key = 0;
1061
1062 eb64_insert(&timers[1], &ustat->node.val);
1063 }
1064 /* switch trees */
1065 timers[0] = timers[1];
1066 }
1067
Baptiste61aaad02012-09-08 23:10:03 +02001068 printf("#req err ttot tavg oktot okavg bavg btot url\n");
Willy Tarreauabe45b62010-10-28 20:33:46 +02001069
1070 /* scan the tree in its reverse sorting order */
1071 node = eb_last(&timers[0]);
1072 while (node) {
1073 ustat = container_of(node, struct url_stat, node.url.node);
Baptiste61aaad02012-09-08 23:10:03 +02001074 printf("%d %d %Ld %Ld %Ld %Ld %Ld %Ld %s\n",
Willy Tarreauabe45b62010-10-28 20:33:46 +02001075 ustat->nb_req,
1076 ustat->nb_err,
1077 ustat->total_time,
1078 ustat->nb_req ? ustat->total_time / ustat->nb_req : 0,
1079 ustat->total_time_ok,
1080 (ustat->nb_req - ustat->nb_err) ? ustat->total_time_ok / (ustat->nb_req - ustat->nb_err) : 0,
Baptiste61aaad02012-09-08 23:10:03 +02001081 ustat->nb_req ? ustat->total_bytes_sent / ustat->nb_req : 0,
1082 ustat->total_bytes_sent,
Willy Tarreauabe45b62010-10-28 20:33:46 +02001083 ustat->url);
1084
1085 node = eb_prev(node);
Willy Tarreaua2b39fb2011-07-10 21:39:35 +02001086 lines_out++;
Willy Tarreauabe45b62010-10-28 20:33:46 +02001087 }
1088 }
Willy Tarreaud2201062010-05-27 18:17:30 +02001089
Willy Tarreau910ba4b2009-11-17 10:16:19 +01001090 empty:
Willy Tarreau72c28532009-01-22 18:56:50 +01001091 if (!(filter & FILT_QUIET))
1092 fprintf(stderr, "%d lines in, %d lines out, %d parsing errors\n",
Willy Tarreaua2b39fb2011-07-10 21:39:35 +02001093 linenum, lines_out, parse_err);
Willy Tarreau72c28532009-01-22 18:56:50 +01001094 exit(0);
1095}
1096
Willy Tarreaua2b39fb2011-07-10 21:39:35 +02001097void filter_output_line(const char *accept_field, const char *time_field, struct timer **tptr)
1098{
1099 puts(line);
1100 lines_out++;
1101}
1102
1103void filter_accept_holes(const char *accept_field, const char *time_field, struct timer **tptr)
1104{
1105 struct timer *t2;
1106 int val;
1107
1108 val = convert_date(accept_field);
1109 if (unlikely(val < 0)) {
1110 truncated_line(linenum, line);
1111 return;
1112 }
1113
1114 t2 = insert_value(&timers[0], tptr, val);
1115 t2->count++;
1116 lines_out++;
1117 return;
1118}
1119
1120void filter_count_status(const char *accept_field, const char *time_field, struct timer **tptr)
1121{
1122 struct timer *t2;
1123 const char *b;
1124 int val;
1125
1126 if (time_field)
1127 b = field_start(time_field, STATUS_FIELD - TIME_FIELD + 1);
1128 else
1129 b = field_start(accept_field, STATUS_FIELD - ACCEPT_FIELD + 1);
1130
1131 if (unlikely(!*b)) {
1132 truncated_line(linenum, line);
1133 return;
1134 }
1135
1136 val = str2ic(b);
1137
1138 t2 = insert_value(&timers[0], tptr, val);
1139 t2->count++;
1140}
1141
1142void filter_count_term_codes(const char *accept_field, const char *time_field, struct timer **tptr)
1143{
1144 struct timer *t2;
1145 const char *b;
1146 int val;
1147
1148 if (time_field)
1149 b = field_start(time_field, TERM_CODES_FIELD - TIME_FIELD + 1);
1150 else
1151 b = field_start(accept_field, TERM_CODES_FIELD - ACCEPT_FIELD + 1);
1152
1153 if (unlikely(!*b)) {
1154 truncated_line(linenum, line);
1155 return;
1156 }
1157
1158 val = 256 * b[0] + b[1];
1159
1160 t2 = insert_value(&timers[0], tptr, val);
1161 t2->count++;
1162}
1163
1164void filter_count_srv_status(const char *accept_field, const char *time_field, struct timer **tptr)
1165{
1166 const char *b, *e, *p;
1167 int f, err, array[5];
1168 struct ebmb_node *srv_node;
1169 struct srv_st *srv;
1170 int val;
1171
1172 /* the server field is before the status field, so let's
1173 * parse them in the proper order.
1174 */
1175 b = field_start(accept_field, SERVER_FIELD - ACCEPT_FIELD + 1);
1176 if (unlikely(!*b)) {
1177 truncated_line(linenum, line);
1178 return;
1179 }
1180
1181 e = field_stop(b + 1); /* we have the server name in [b]..[e-1] */
1182
1183 /* the chance that a server name already exists is extremely high,
1184 * so let's perform a normal lookup first.
1185 */
1186 srv_node = ebst_lookup_len(&timers[0], b, e - b);
1187 srv = container_of(srv_node, struct srv_st, node);
1188
1189 if (!srv_node) {
1190 /* server not yet in the tree, let's create it */
1191 srv = (void *)calloc(1, sizeof(struct srv_st) + e - b + 1);
1192 srv_node = &srv->node;
1193 memcpy(&srv_node->key, b, e - b);
1194 srv_node->key[e - b] = '\0';
1195 ebst_insert(&timers[0], srv_node);
1196 }
1197
1198 /* let's collect the connect and response times */
1199 if (!time_field) {
1200 time_field = field_start(e, TIME_FIELD - SERVER_FIELD);
1201 if (unlikely(!*time_field)) {
1202 truncated_line(linenum, line);
1203 return;
1204 }
1205 }
1206
1207 e = field_stop(time_field + 1);
1208 /* we have field TIME_FIELD in [time_field]..[e-1] */
1209
1210 p = time_field;
1211 err = 0;
1212 f = 0;
1213 while (!SEP(*p)) {
1214 array[f] = str2ic(p);
1215 if (array[f] < 0) {
1216 array[f] = -1;
1217 err = 1;
1218 }
1219 if (++f == 5)
1220 break;
1221 SKIP_CHAR(p, '/');
1222 }
1223
1224 if (unlikely(f < 5)){
1225 parse_err++;
1226 return;
1227 }
1228
1229 /* OK we have our timers in array[2,3] */
1230 if (!err)
1231 srv->nb_ok++;
1232
1233 if (array[2] >= 0) {
1234 srv->cum_ct += array[2];
1235 srv->nb_ct++;
1236 }
1237
1238 if (array[3] >= 0) {
1239 srv->cum_rt += array[3];
1240 srv->nb_rt++;
1241 }
1242
1243 /* we're interested in the 5 HTTP status classes (1xx ... 5xx), and
1244 * the invalid ones which will be reported as 0.
1245 */
1246 b = field_start(e, STATUS_FIELD - TIME_FIELD);
1247 if (unlikely(!*b)) {
1248 truncated_line(linenum, line);
1249 return;
1250 }
1251
1252 val = 0;
1253 if (*b >= '1' && *b <= '5')
1254 val = *b - '0';
1255
1256 srv->st_cnt[val]++;
1257}
1258
1259void filter_count_url(const char *accept_field, const char *time_field, struct timer **tptr)
1260{
1261 struct url_stat *ustat = NULL;
1262 struct ebpt_node *ebpt_old;
1263 const char *b, *e;
1264 int f, err, array[5];
Baptiste61aaad02012-09-08 23:10:03 +02001265 int val;
Willy Tarreaua2b39fb2011-07-10 21:39:35 +02001266
1267 /* let's collect the response time */
1268 if (!time_field) {
1269 time_field = field_start(accept_field, TIME_FIELD - ACCEPT_FIELD + 1); // avg 115 ns per line
1270 if (unlikely(!*time_field)) {
1271 truncated_line(linenum, line);
1272 return;
1273 }
1274 }
1275
1276 /* we have the field TIME_FIELD starting at <time_field>. We'll
1277 * parse the 5 timers to detect errors, it takes avg 55 ns per line.
1278 */
1279 e = time_field; err = 0; f = 0;
1280 while (!SEP(*e)) {
1281 array[f] = str2ic(e);
1282 if (array[f] < 0) {
1283 array[f] = -1;
1284 err = 1;
1285 }
1286 if (++f == 5)
1287 break;
1288 SKIP_CHAR(e, '/');
1289 }
1290 if (f < 5) {
1291 parse_err++;
1292 return;
1293 }
1294
1295 /* OK we have our timers in array[3], and err is >0 if at
1296 * least one -1 was seen. <e> points to the first char of
1297 * the last timer. Let's prepare a new node with that.
1298 */
1299 if (unlikely(!ustat))
1300 ustat = calloc(1, sizeof(*ustat));
1301
1302 ustat->nb_err = err;
1303 ustat->nb_req = 1;
1304
1305 /* use array[4] = total time in case of error */
1306 ustat->total_time = (array[3] >= 0) ? array[3] : array[4];
1307 ustat->total_time_ok = (array[3] >= 0) ? array[3] : 0;
1308
Baptiste61aaad02012-09-08 23:10:03 +02001309 e = field_start(e, BYTES_SENT_FIELD - TIME_FIELD + 1);
1310 val = str2ic(e);
1311 ustat->total_bytes_sent = val;
1312
Willy Tarreaua2b39fb2011-07-10 21:39:35 +02001313 /* the line may be truncated because of a bad request or anything like this,
1314 * without a method. Also, if it does not begin with an quote, let's skip to
1315 * the next field because it's a capture. Let's fall back to the "method" itself
1316 * if there's nothing else.
1317 */
Baptiste61aaad02012-09-08 23:10:03 +02001318 e = field_start(e, METH_FIELD - BYTES_SENT_FIELD + 1);
Willy Tarreau61a40c72011-09-06 08:11:27 +02001319 while (*e != '"' && *e) {
1320 /* Note: some syslog servers escape quotes ! */
1321 if (*e == '\\' && e[1] == '"')
1322 break;
Willy Tarreaua2b39fb2011-07-10 21:39:35 +02001323 e = field_start(e, 2);
Willy Tarreau61a40c72011-09-06 08:11:27 +02001324 }
Willy Tarreaua2b39fb2011-07-10 21:39:35 +02001325
1326 if (unlikely(!*e)) {
1327 truncated_line(linenum, line);
1328 return;
1329 }
1330
1331 b = field_start(e, URL_FIELD - METH_FIELD + 1); // avg 40 ns per line
1332 if (!*b)
1333 b = e;
1334
1335 /* stop at end of field or first ';' or '?', takes avg 64 ns per line */
1336 e = b;
1337 do {
Willy Tarreau14389e72011-07-10 22:11:17 +02001338 if (*e == ' ' || *e == '?' || *e == ';') {
Willy Tarreaua2b39fb2011-07-10 21:39:35 +02001339 *(char *)e = 0;
1340 break;
1341 }
1342 e++;
1343 } while (*e);
1344
1345 /* now instead of copying the URL for a simple lookup, we'll link
1346 * to it from the node we're trying to insert. If it returns a
1347 * different value, it was already there. Otherwise we just have
1348 * to dynamically realloc an entry using strdup().
1349 */
1350 ustat->node.url.key = (char *)b;
1351 ebpt_old = ebis_insert(&timers[0], &ustat->node.url);
1352
1353 if (ebpt_old != &ustat->node.url) {
1354 struct url_stat *ustat_old;
1355 /* node was already there, let's update previous one */
1356 ustat_old = container_of(ebpt_old, struct url_stat, node.url);
1357 ustat_old->nb_req ++;
1358 ustat_old->nb_err += ustat->nb_err;
1359 ustat_old->total_time += ustat->total_time;
1360 ustat_old->total_time_ok += ustat->total_time_ok;
Baptiste61aaad02012-09-08 23:10:03 +02001361 ustat_old->total_bytes_sent += ustat->total_bytes_sent;
Willy Tarreaua2b39fb2011-07-10 21:39:35 +02001362 } else {
1363 ustat->url = ustat->node.url.key = strdup(ustat->node.url.key);
1364 ustat = NULL; /* node was used */
1365 }
1366}
1367
1368void filter_graphs(const char *accept_field, const char *time_field, struct timer **tptr)
1369{
1370 struct timer *t2;
1371 const char *e, *p;
1372 int f, err, array[5];
1373
1374 if (!time_field) {
1375 time_field = field_start(accept_field, TIME_FIELD - ACCEPT_FIELD + 1);
1376 if (unlikely(!*time_field)) {
1377 truncated_line(linenum, line);
1378 return;
1379 }
1380 }
1381
1382 e = field_stop(time_field + 1);
1383 /* we have field TIME_FIELD in [time_field]..[e-1] */
1384
1385 p = time_field;
1386 err = 0;
1387 f = 0;
1388 while (!SEP(*p)) {
1389 array[f] = str2ic(p);
1390 if (array[f] < 0) {
1391 array[f] = -1;
1392 err = 1;
1393 }
1394 if (++f == 5)
1395 break;
1396 SKIP_CHAR(p, '/');
1397 }
1398
1399 if (unlikely(f < 5)) {
1400 parse_err++;
1401 return;
1402 }
1403
1404 /* if we find at least one negative time, we count one error
1405 * with a time equal to the total session time. This will
1406 * emphasize quantum timing effects associated to known
1407 * timeouts. Note that on some buggy machines, it is possible
1408 * that the total time is negative, hence the reason to reset
1409 * it.
1410 */
1411
1412 if (filter & FILT_GRAPH_TIMERS) {
1413 if (err) {
1414 if (array[4] < 0)
1415 array[4] = -1;
1416 t2 = insert_timer(&timers[0], tptr, array[4]); // total time
1417 t2->count++;
1418 } else {
1419 int v;
1420
1421 t2 = insert_timer(&timers[1], tptr, array[0]); t2->count++; // req
1422 t2 = insert_timer(&timers[2], tptr, array[2]); t2->count++; // conn
1423 t2 = insert_timer(&timers[3], tptr, array[3]); t2->count++; // resp
1424
1425 v = array[4] - array[0] - array[1] - array[2] - array[3]; // data time
1426 if (v < 0 && !(filter & FILT_QUIET))
1427 fprintf(stderr, "ERR: %s (%d %d %d %d %d => %d)\n",
1428 line, array[0], array[1], array[2], array[3], array[4], v);
1429 t2 = insert_timer(&timers[4], tptr, v); t2->count++;
1430 lines_out++;
1431 }
1432 } else { /* percentile */
1433 if (err) {
1434 if (array[4] < 0)
1435 array[4] = -1;
1436 t2 = insert_value(&timers[0], tptr, array[4]); // total time
1437 t2->count++;
1438 } else {
1439 int v;
1440
1441 t2 = insert_value(&timers[1], tptr, array[0]); t2->count++; // req
1442 t2 = insert_value(&timers[2], tptr, array[2]); t2->count++; // conn
1443 t2 = insert_value(&timers[3], tptr, array[3]); t2->count++; // resp
1444
1445 v = array[4] - array[0] - array[1] - array[2] - array[3]; // data time
1446 if (v < 0 && !(filter & FILT_QUIET))
1447 fprintf(stderr, "ERR: %s (%d %d %d %d %d => %d)\n",
1448 line, array[0], array[1], array[2], array[3], array[4], v);
1449 t2 = insert_value(&timers[4], tptr, v); t2->count++;
1450 lines_out++;
1451 }
1452 }
1453}
1454
1455
Willy Tarreau72c28532009-01-22 18:56:50 +01001456/*
1457 * Local variables:
1458 * c-indent-level: 8
1459 * c-basic-offset: 8
1460 * End:
1461 */