blob: 61034eca2ce4382156ea6ebf9921534231461b56 [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 Tarreau8a09b662012-10-10 10:26:22 +02004 * Copyright 2000-2012 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 Tarreau8a09b662012-10-10 10:26:22 +0200116#define FILT_COUNT_COOK_CODES 0x40000000
117
Willy Tarreau72c28532009-01-22 18:56:50 +0100118unsigned int filter = 0;
119unsigned int filter_invert = 0;
Willy Tarreau214c2032009-02-20 11:02:32 +0100120const char *line;
Willy Tarreaua2b39fb2011-07-10 21:39:35 +0200121int linenum = 0;
122int parse_err = 0;
123int lines_out = 0;
Willy Tarreau667c9052012-10-10 16:49:28 +0200124int lines_max = -1;
Willy Tarreau72c28532009-01-22 18:56:50 +0100125
Willy Tarreau214c2032009-02-20 11:02:32 +0100126const char *fgets2(FILE *stream);
Willy Tarreau72c28532009-01-22 18:56:50 +0100127
Willy Tarreaua2b39fb2011-07-10 21:39:35 +0200128void filter_count_url(const char *accept_field, const char *time_field, struct timer **tptr);
129void filter_count_srv_status(const char *accept_field, const char *time_field, struct timer **tptr);
Willy Tarreau8a09b662012-10-10 10:26:22 +0200130void filter_count_cook_codes(const char *accept_field, const char *time_field, struct timer **tptr);
Willy Tarreaua2b39fb2011-07-10 21:39:35 +0200131void filter_count_term_codes(const char *accept_field, const char *time_field, struct timer **tptr);
132void filter_count_status(const char *accept_field, const char *time_field, struct timer **tptr);
133void filter_graphs(const char *accept_field, const char *time_field, struct timer **tptr);
134void filter_output_line(const char *accept_field, const char *time_field, struct timer **tptr);
135void filter_accept_holes(const char *accept_field, const char *time_field, struct timer **tptr);
136
Willy Tarreau615674c2012-01-23 08:15:51 +0100137void usage(FILE *output, const char *msg)
Willy Tarreau72c28532009-01-22 18:56:50 +0100138{
Willy Tarreau615674c2012-01-23 08:15:51 +0100139 fprintf(output,
Willy Tarreau72c28532009-01-22 18:56:50 +0100140 "%s"
Willy Tarreau615674c2012-01-23 08:15:51 +0100141 "Usage: halog [-h|--help] for long help\n"
Willy Tarreau667c9052012-10-10 16:49:28 +0200142 " halog [-q] [-c] [-m <lines>]\n"
Willy Tarreau8a09b662012-10-10 10:26:22 +0200143 " {-cc|-gt|-pct|-st|-tc|-srv|-u|-uc|-ue|-ua|-ut|-uao|-uto|-uba|-ubt}\n"
Hervé COMMOWICK927cddd2011-08-10 17:42:41 +0200144 " [-s <skip>] [-e|-E] [-H] [-rt|-RT <time>] [-ad <delay>] [-ac <count>]\n"
Baptiste61aaad02012-09-08 23:10:03 +0200145 " [-v] [-Q|-QS] [-tcn|-TCN <termcode>] [ -hs|-HS [min][:[max]] ] < log\n"
Willy Tarreau72c28532009-01-22 18:56:50 +0100146 "\n",
147 msg ? msg : ""
148 );
Willy Tarreau615674c2012-01-23 08:15:51 +0100149}
150
151void die(const char *msg)
152{
153 usage(stderr, msg);
Willy Tarreau72c28532009-01-22 18:56:50 +0100154 exit(1);
155}
156
Willy Tarreau615674c2012-01-23 08:15:51 +0100157void help()
158{
159 usage(stdout, NULL);
160 printf(
161 "Input filters (several filters may be combined) :\n"
162 " -H only match lines containing HTTP logs (ignore TCP)\n"
163 " -E only match lines without any error (no 5xx status)\n"
164 " -e only match lines with errors (status 5xx or negative)\n"
165 " -rt|-RT <time> only match response times larger|smaller than <time>\n"
166 " -Q|-QS only match queued requests (any queue|server queue)\n"
167 " -tcn|-TCN <code> only match requests with/without termination code <code>\n"
168 " -hs|-HS <[min][:][max]> only match requests with HTTP status codes within/not\n"
169 " within min..max. Any of them may be omitted. Exact\n"
170 " code is checked for if no ':' is specified.\n"
171 "Modifiers\n"
172 " -v invert the input filtering condition\n"
173 " -q don't report errors/warnings\n"
Willy Tarreau667c9052012-10-10 16:49:28 +0200174 " -m <lines> limit output to the first <lines> lines\n"
Willy Tarreau615674c2012-01-23 08:15:51 +0100175 "Output filters - only one may be used at a time\n"
176 " -c only report the number of lines that would have been printed\n"
177 " -pct output connect and response times percentiles\n"
178 " -st output number of requests per HTTP status code\n"
Willy Tarreau8a09b662012-10-10 10:26:22 +0200179 " -cc output number of requests per cookie code (2 chars)\n"
Willy Tarreau615674c2012-01-23 08:15:51 +0100180 " -tc output number of requests per termination code (2 chars)\n"
181 " -srv output statistics per server (time, requests, errors)\n"
182 " -u* output statistics per URL (time, requests, errors)\n"
183 " Additional characters indicate the output sorting key :\n"
184 " -u : by URL, -uc : request count, -ue : error count\n"
Willy Tarreau4201df72012-10-10 14:57:35 +0200185 " -ua : average response time, -ut : average total time\n"
Willy Tarreau615674c2012-01-23 08:15:51 +0100186 " -uao, -uto: average times computed on valid ('OK') requests\n"
Baptiste61aaad02012-09-08 23:10:03 +0200187 " -uba, -ubt: average bytes returned, total bytes returned\n"
Willy Tarreau615674c2012-01-23 08:15:51 +0100188 );
189 exit(0);
190}
191
Willy Tarreau72c28532009-01-22 18:56:50 +0100192
193/* return pointer to first char not part of current field starting at <p>. */
Willy Tarreauf9042062011-09-10 12:26:35 +0200194
195#if defined(__i386__)
196/* this one is always faster on 32-bits */
197static inline const char *field_stop(const char *p)
198{
199 asm(
200 /* Look for spaces */
201 "4: \n\t"
202 "inc %0 \n\t"
203 "cmpb $0x20, -1(%0) \n\t"
204 "ja 4b \n\t"
205 "jz 3f \n\t"
206
207 /* we only get there for control chars 0..31. Leave if we find '\0' */
208 "cmpb $0x0, -1(%0) \n\t"
209 "jnz 4b \n\t"
210
211 /* return %0-1 = position of the last char we checked */
212 "3: \n\t"
213 "dec %0 \n\t"
214 : "=r" (p)
215 : "0" (p)
216 );
217 return p;
218}
219#else
Willy Tarreau72c28532009-01-22 18:56:50 +0100220const char *field_stop(const char *p)
221{
222 unsigned char c;
223
224 while (1) {
225 c = *(p++);
226 if (c > ' ')
227 continue;
Willy Tarreau14389e72011-07-10 22:11:17 +0200228 if (c == ' ' || c == 0)
Willy Tarreau72c28532009-01-22 18:56:50 +0100229 break;
230 }
231 return p - 1;
232}
Willy Tarreauf9042062011-09-10 12:26:35 +0200233#endif
Willy Tarreau72c28532009-01-22 18:56:50 +0100234
235/* return field <field> (starting from 1) in string <p>. Only consider
236 * contiguous spaces (or tabs) as one delimiter. May return pointer to
237 * last char if field is not found. Equivalent to awk '{print $field}'.
238 */
239const char *field_start(const char *p, int field)
240{
Willy Tarreauf9042062011-09-10 12:26:35 +0200241#ifndef PREFER_ASM
Willy Tarreau72c28532009-01-22 18:56:50 +0100242 unsigned char c;
243 while (1) {
244 /* skip spaces */
245 while (1) {
Willy Tarreauf9042062011-09-10 12:26:35 +0200246 c = *(p++);
Willy Tarreau72c28532009-01-22 18:56:50 +0100247 if (c > ' ')
248 break;
Willy Tarreau14389e72011-07-10 22:11:17 +0200249 if (c == ' ')
Willy Tarreauf9042062011-09-10 12:26:35 +0200250 continue;
Willy Tarreau72c28532009-01-22 18:56:50 +0100251 if (!c) /* end of line */
Willy Tarreauf9042062011-09-10 12:26:35 +0200252 return p-1;
Willy Tarreau72c28532009-01-22 18:56:50 +0100253 /* other char => new field */
254 break;
Willy Tarreau72c28532009-01-22 18:56:50 +0100255 }
256
257 /* start of field */
258 field--;
259 if (!field)
Willy Tarreauf9042062011-09-10 12:26:35 +0200260 return p-1;
Willy Tarreau72c28532009-01-22 18:56:50 +0100261
262 /* skip this field */
263 while (1) {
264 c = *(p++);
Willy Tarreau14389e72011-07-10 22:11:17 +0200265 if (c == ' ')
Willy Tarreau72c28532009-01-22 18:56:50 +0100266 break;
Willy Tarreauf9042062011-09-10 12:26:35 +0200267 if (c > ' ')
268 continue;
Willy Tarreau72c28532009-01-22 18:56:50 +0100269 if (c == '\0')
Willy Tarreauf9042062011-09-10 12:26:35 +0200270 return p - 1;
Willy Tarreau72c28532009-01-22 18:56:50 +0100271 }
272 }
Willy Tarreauf9042062011-09-10 12:26:35 +0200273#else
274 /* This version works optimally on i386 and x86_64 but the code above
275 * shows similar performance. However, depending on the version of GCC
276 * used, inlining rules change and it may have difficulties to make
277 * efficient use of this code at other locations and could result in
278 * worse performance (eg: gcc 4.4). You may want to experience.
279 */
280 asm(
281 /* skip spaces */
282 "1: \n\t"
283 "inc %0 \n\t"
284 "cmpb $0x20, -1(%0) \n\t"
285 "ja 2f \n\t"
286 "jz 1b \n\t"
287
288 /* we only get there for control chars 0..31. Leave if we find '\0' */
289 "cmpb $0x0, -1(%0) \n\t"
290 "jz 3f \n\t"
291
292 /* start of field at [%0-1]. Check if we need to skip more fields */
293 "2: \n\t"
294 "dec %1 \n\t"
295 "jz 3f \n\t"
296
297 /* Look for spaces */
298 "4: \n\t"
299 "inc %0 \n\t"
300 "cmpb $0x20, -1(%0) \n\t"
301 "jz 1b \n\t"
302 "ja 4b \n\t"
303
304 /* we only get there for control chars 0..31. Leave if we find '\0' */
305 "cmpb $0x0, -1(%0) \n\t"
306 "jnz 4b \n\t"
307
308 /* return %0-1 = position of the last char we checked */
309 "3: \n\t"
310 "dec %0 \n\t"
311 : "=r" (p)
312 : "r" (field), "0" (p)
313 );
314 return p;
315#endif
Willy Tarreau72c28532009-01-22 18:56:50 +0100316}
317
318/* keep only the <bits> higher bits of <i> */
319static inline unsigned int quantify_u32(unsigned int i, int bits)
320{
321 int high;
322
323 if (!bits)
324 return 0;
325
326 if (i)
327 high = fls_auto(i); // 1 to 32
328 else
329 high = 0;
330
331 if (high <= bits)
332 return i;
333
334 return i & ~((1 << (high - bits)) - 1);
335}
336
337/* keep only the <bits> higher bits of the absolute value of <i>, as well as
338 * its sign. */
339static inline int quantify(int i, int bits)
340{
341 if (i >= 0)
342 return quantify_u32(i, bits);
343 else
344 return -quantify_u32(-i, bits);
345}
346
347/* Insert timer value <v> into tree <r>. A pre-allocated node must be passed
348 * in <alloc>. It may be NULL, in which case the function will allocate it
349 * itself. It will be reset to NULL once consumed. The caller is responsible
350 * for freeing the node once not used anymore. The node where the value was
351 * inserted is returned.
352 */
353struct timer *insert_timer(struct eb_root *r, struct timer **alloc, int v)
354{
355 struct timer *t = *alloc;
356 struct eb32_node *n;
357
358 if (!t) {
359 t = calloc(sizeof(*t), 1);
360 if (unlikely(!t)) {
361 fprintf(stderr, "%s: not enough memory\n", __FUNCTION__);
362 exit(1);
363 }
364 }
365 t->node.key = quantify(v, QBITS); // keep only the higher QBITS bits
366
367 n = eb32i_insert(r, &t->node);
368 if (n == &t->node)
369 t = NULL; /* node inserted, will malloc next time */
370
371 *alloc = t;
372 return container_of(n, struct timer, node);
373}
374
375/* Insert value value <v> into tree <r>. A pre-allocated node must be passed
376 * in <alloc>. It may be NULL, in which case the function will allocate it
377 * itself. It will be reset to NULL once consumed. The caller is responsible
378 * for freeing the node once not used anymore. The node where the value was
379 * inserted is returned.
380 */
381struct timer *insert_value(struct eb_root *r, struct timer **alloc, int v)
382{
383 struct timer *t = *alloc;
384 struct eb32_node *n;
385
386 if (!t) {
387 t = calloc(sizeof(*t), 1);
388 if (unlikely(!t)) {
389 fprintf(stderr, "%s: not enough memory\n", __FUNCTION__);
390 exit(1);
391 }
392 }
393 t->node.key = v;
394
395 n = eb32i_insert(r, &t->node);
396 if (n == &t->node)
397 t = NULL; /* node inserted, will malloc next time */
398
399 *alloc = t;
400 return container_of(n, struct timer, node);
401}
402
403int str2ic(const char *s)
404{
405 int i = 0;
406 int j, k;
407
408 if (*s != '-') {
409 /* positive number */
410 while (1) {
411 j = (*s++) - '0';
412 k = i * 10;
413 if ((unsigned)j > 9)
414 break;
415 i = k + j;
416 }
417 } else {
418 /* negative number */
419 s++;
420 while (1) {
421 j = (*s++) - '0';
422 k = i * 10;
423 if ((unsigned)j > 9)
424 break;
425 i = k - j;
426 }
427 }
428
429 return i;
430}
431
432
433/* Equivalent to strtoul with a length. */
434static inline unsigned int __strl2ui(const char *s, int len)
435{
436 unsigned int i = 0;
437 while (len-- > 0) {
438 i = i * 10 - '0';
439 i += (unsigned char)*s++;
440 }
441 return i;
442}
443
444unsigned int strl2ui(const char *s, int len)
445{
446 return __strl2ui(s, len);
447}
448
449/* Convert "[04/Dec/2008:09:49:40.555]" to an integer equivalent to the time of
450 * the day in milliseconds. It returns -1 for all unparsable values. The parser
451 * looks ugly but gcc emits far better code that way.
452 */
453int convert_date(const char *field)
454{
455 unsigned int h, m, s, ms;
456 unsigned char c;
457 const char *b, *e;
458
459 h = m = s = ms = 0;
460 e = field;
461
462 /* skip the date */
463 while (1) {
464 c = *(e++);
465 if (c == ':')
466 break;
467 if (!c)
468 goto out_err;
469 }
470
471 /* hour + ':' */
472 b = e;
473 while (1) {
474 c = *(e++) - '0';
475 if (c > 9)
476 break;
477 h = h * 10 + c;
478 }
479 if (c == (unsigned char)(0 - '0'))
480 goto out_err;
481
482 /* minute + ':' */
483 b = e;
484 while (1) {
485 c = *(e++) - '0';
486 if (c > 9)
487 break;
488 m = m * 10 + c;
489 }
490 if (c == (unsigned char)(0 - '0'))
491 goto out_err;
492
493 /* second + '.' or ']' */
494 b = e;
495 while (1) {
496 c = *(e++) - '0';
497 if (c > 9)
498 break;
499 s = s * 10 + c;
500 }
501 if (c == (unsigned char)(0 - '0'))
502 goto out_err;
503
504 /* if there's a '.', we have milliseconds */
505 if (c == (unsigned char)('.' - '0')) {
506 /* millisecond second + ']' */
507 b = e;
508 while (1) {
509 c = *(e++) - '0';
510 if (c > 9)
511 break;
512 ms = ms * 10 + c;
513 }
514 if (c == (unsigned char)(0 - '0'))
515 goto out_err;
516 }
517 return (((h * 60) + m) * 60 + s) * 1000 + ms;
518 out_err:
519 return -1;
520}
521
522void truncated_line(int linenum, const char *line)
523{
524 if (!(filter & FILT_QUIET))
525 fprintf(stderr, "Truncated line %d: %s\n", linenum, line);
526}
527
528int main(int argc, char **argv)
529{
Willy Tarreau26deaf52011-07-10 19:47:48 +0200530 const char *b, *e, *p, *time_field, *accept_field;
Hervé COMMOWICK927cddd2011-08-10 17:42:41 +0200531 const char *filter_term_code_name = NULL;
Willy Tarreau72c28532009-01-22 18:56:50 +0100532 const char *output_file = NULL;
Willy Tarreaua2b39fb2011-07-10 21:39:35 +0200533 int f, last, err;
534 struct timer *t = NULL;
Willy Tarreau72c28532009-01-22 18:56:50 +0100535 struct eb32_node *n;
Willy Tarreauabe45b62010-10-28 20:33:46 +0200536 struct url_stat *ustat = NULL;
Willy Tarreau72c28532009-01-22 18:56:50 +0100537 int val, test;
Willy Tarreau72c28532009-01-22 18:56:50 +0100538 int filter_acc_delay = 0, filter_acc_count = 0;
Willy Tarreau5bdfd962009-10-14 15:16:29 +0200539 int filter_time_resp = 0;
Willy Tarreaud3007ff2011-09-05 02:07:23 +0200540 int filt_http_status_low = 0, filt_http_status_high = 0;
Willy Tarreau72c28532009-01-22 18:56:50 +0100541 int skip_fields = 1;
542
Willy Tarreaua2b39fb2011-07-10 21:39:35 +0200543 void (*line_filter)(const char *accept_field, const char *time_field, struct timer **tptr) = NULL;
544
Willy Tarreau72c28532009-01-22 18:56:50 +0100545 argc--; argv++;
546 while (argc > 0) {
547 if (*argv[0] != '-')
548 break;
549
550 if (strcmp(argv[0], "-ad") == 0) {
551 if (argc < 2) die("missing option for -ad");
552 argc--; argv++;
553 filter |= FILT_ACC_DELAY;
554 filter_acc_delay = atol(*argv);
555 }
556 else if (strcmp(argv[0], "-ac") == 0) {
557 if (argc < 2) die("missing option for -ac");
558 argc--; argv++;
559 filter |= FILT_ACC_COUNT;
560 filter_acc_count = atol(*argv);
561 }
Willy Tarreau5bdfd962009-10-14 15:16:29 +0200562 else if (strcmp(argv[0], "-rt") == 0) {
563 if (argc < 2) die("missing option for -rt");
564 argc--; argv++;
565 filter |= FILT_TIME_RESP;
566 filter_time_resp = atol(*argv);
567 }
568 else if (strcmp(argv[0], "-RT") == 0) {
569 if (argc < 2) die("missing option for -RT");
570 argc--; argv++;
571 filter |= FILT_TIME_RESP | FILT_INVERT_TIME_RESP;
572 filter_time_resp = atol(*argv);
573 }
Willy Tarreau72c28532009-01-22 18:56:50 +0100574 else if (strcmp(argv[0], "-s") == 0) {
575 if (argc < 2) die("missing option for -s");
576 argc--; argv++;
577 skip_fields = atol(*argv);
578 }
Willy Tarreau667c9052012-10-10 16:49:28 +0200579 else if (strcmp(argv[0], "-m") == 0) {
580 if (argc < 2) die("missing option for -m");
581 argc--; argv++;
582 lines_max = atol(*argv);
583 }
Willy Tarreau72c28532009-01-22 18:56:50 +0100584 else if (strcmp(argv[0], "-e") == 0)
585 filter |= FILT_ERRORS_ONLY;
Willy Tarreau5bdfd962009-10-14 15:16:29 +0200586 else if (strcmp(argv[0], "-E") == 0)
587 filter |= FILT_ERRORS_ONLY | FILT_INVERT_ERRORS;
Willy Tarreau70c428f2011-07-10 17:27:40 +0200588 else if (strcmp(argv[0], "-H") == 0)
589 filter |= FILT_HTTP_ONLY;
Willy Tarreau08911ff2011-10-13 13:28:36 +0200590 else if (strcmp(argv[0], "-Q") == 0)
591 filter |= FILT_QUEUE_ONLY;
592 else if (strcmp(argv[0], "-QS") == 0)
593 filter |= FILT_QUEUE_SRV_ONLY;
Willy Tarreau72c28532009-01-22 18:56:50 +0100594 else if (strcmp(argv[0], "-c") == 0)
595 filter |= FILT_COUNT_ONLY;
596 else if (strcmp(argv[0], "-q") == 0)
597 filter |= FILT_QUIET;
598 else if (strcmp(argv[0], "-v") == 0)
599 filter_invert = !filter_invert;
600 else if (strcmp(argv[0], "-gt") == 0)
601 filter |= FILT_GRAPH_TIMERS;
Willy Tarreau214c2032009-02-20 11:02:32 +0100602 else if (strcmp(argv[0], "-pct") == 0)
603 filter |= FILT_PERCENTILE;
Willy Tarreau0f423a72010-05-03 10:50:54 +0200604 else if (strcmp(argv[0], "-st") == 0)
605 filter |= FILT_COUNT_STATUS;
Willy Tarreaud2201062010-05-27 18:17:30 +0200606 else if (strcmp(argv[0], "-srv") == 0)
607 filter |= FILT_COUNT_SRV_STATUS;
Willy Tarreau8a09b662012-10-10 10:26:22 +0200608 else if (strcmp(argv[0], "-cc") == 0)
609 filter |= FILT_COUNT_COOK_CODES;
Willy Tarreaud8fc1102010-09-12 17:56:16 +0200610 else if (strcmp(argv[0], "-tc") == 0)
611 filter |= FILT_COUNT_TERM_CODES;
Hervé COMMOWICK927cddd2011-08-10 17:42:41 +0200612 else if (strcmp(argv[0], "-tcn") == 0) {
613 if (argc < 2) die("missing option for -tcn");
614 argc--; argv++;
615 filter |= FILT_TERM_CODE_NAME;
616 filter_term_code_name = *argv;
617 }
618 else if (strcmp(argv[0], "-TCN") == 0) {
619 if (argc < 2) die("missing option for -TCN");
620 argc--; argv++;
621 filter |= FILT_TERM_CODE_NAME | FILT_INVERT_TERM_CODE_NAME;
622 filter_term_code_name = *argv;
623 }
Willy Tarreaud3007ff2011-09-05 02:07:23 +0200624 else if (strcmp(argv[0], "-hs") == 0 || strcmp(argv[0], "-HS") == 0) {
625 char *sep, *str;
626
627 if (argc < 2) die("missing option for -hs/-HS ([min]:[max])");
628 filter |= FILT_HTTP_STATUS;
629 if (argv[0][1] == 'H')
630 filter |= FILT_INVERT_HTTP_STATUS;
631
632 argc--; argv++;
633 str = *argv;
634 sep = strchr(str, ':'); /* [min]:[max] */
635 if (!sep)
636 sep = str; /* make max point to min */
637 else
638 *sep++ = 0;
639 filt_http_status_low = *str ? atol(str) : 0;
640 filt_http_status_high = *sep ? atol(sep) : 65535;
641 }
Willy Tarreauabe45b62010-10-28 20:33:46 +0200642 else if (strcmp(argv[0], "-u") == 0)
643 filter |= FILT_COUNT_URL_ONLY;
644 else if (strcmp(argv[0], "-uc") == 0)
645 filter |= FILT_COUNT_URL_COUNT;
646 else if (strcmp(argv[0], "-ue") == 0)
647 filter |= FILT_COUNT_URL_ERR;
648 else if (strcmp(argv[0], "-ua") == 0)
649 filter |= FILT_COUNT_URL_TAVG;
650 else if (strcmp(argv[0], "-ut") == 0)
651 filter |= FILT_COUNT_URL_TTOT;
652 else if (strcmp(argv[0], "-uao") == 0)
653 filter |= FILT_COUNT_URL_TAVGO;
654 else if (strcmp(argv[0], "-uto") == 0)
655 filter |= FILT_COUNT_URL_TTOTO;
Baptiste61aaad02012-09-08 23:10:03 +0200656 else if (strcmp(argv[0], "-uba") == 0)
657 filter |= FILT_COUNT_URL_BAVG;
658 else if (strcmp(argv[0], "-ubt") == 0)
659 filter |= FILT_COUNT_URL_BTOT;
Willy Tarreau72c28532009-01-22 18:56:50 +0100660 else if (strcmp(argv[0], "-o") == 0) {
661 if (output_file)
662 die("Fatal: output file name already specified.\n");
663 if (argc < 2)
664 die("Fatal: missing output file name.\n");
665 output_file = argv[1];
666 }
Willy Tarreau615674c2012-01-23 08:15:51 +0100667 else if (strcmp(argv[0], "-h") == 0 || strcmp(argv[0], "--help") == 0)
668 help();
Willy Tarreau72c28532009-01-22 18:56:50 +0100669 argc--;
670 argv++;
671 }
672
673 if (!filter)
674 die("No action specified.\n");
675
676 if (filter & FILT_ACC_COUNT && !filter_acc_count)
677 filter_acc_count=1;
678
679 if (filter & FILT_ACC_DELAY && !filter_acc_delay)
680 filter_acc_delay = 1;
681
Willy Tarreaua2b39fb2011-07-10 21:39:35 +0200682
683 /* by default, all lines are printed */
684 line_filter = filter_output_line;
685 if (filter & (FILT_ACC_COUNT|FILT_ACC_DELAY))
686 line_filter = filter_accept_holes;
687 else if (filter & (FILT_GRAPH_TIMERS|FILT_PERCENTILE))
688 line_filter = filter_graphs;
689 else if (filter & FILT_COUNT_STATUS)
690 line_filter = filter_count_status;
Willy Tarreau8a09b662012-10-10 10:26:22 +0200691 else if (filter & FILT_COUNT_COOK_CODES)
692 line_filter = filter_count_cook_codes;
Willy Tarreaua2b39fb2011-07-10 21:39:35 +0200693 else if (filter & FILT_COUNT_TERM_CODES)
694 line_filter = filter_count_term_codes;
695 else if (filter & FILT_COUNT_SRV_STATUS)
696 line_filter = filter_count_srv_status;
697 else if (filter & FILT_COUNT_URL_ANY)
698 line_filter = filter_count_url;
699 else if (filter & FILT_COUNT_ONLY)
700 line_filter = NULL;
Willy Tarreau72c28532009-01-22 18:56:50 +0100701
Willy Tarreauf8c95d22012-06-12 09:16:56 +0200702#if defined(POSIX_FADV_SEQUENTIAL)
703 /* around 20% performance improvement is observed on Linux with this
704 * on cold-cache. Surprizingly, WILLNEED is less performant. Don't
705 * use NOREUSE as it flushes the cache and prevents easy data
706 * manipulation on logs!
707 */
708 posix_fadvise(0, 0, 0, POSIX_FADV_SEQUENTIAL);
709#endif
710
Willy Tarreaua1629a52012-11-13 20:48:15 +0100711 if (!line_filter && /* FILT_COUNT_ONLY ( see above), and no input filter (see below) */
Willy Tarreaue1a908c2012-01-03 09:23:03 +0100712 !(filter & (FILT_HTTP_ONLY|FILT_TIME_RESP|FILT_ERRORS_ONLY|FILT_HTTP_STATUS|FILT_QUEUE_ONLY|FILT_QUEUE_SRV_ONLY|FILT_TERM_CODE_NAME))) {
Willy Tarreaua1629a52012-11-13 20:48:15 +0100713 /* read the whole file at once first, ignore it if inverted output */
Willy Tarreaue1a908c2012-01-03 09:23:03 +0100714 if (!filter_invert)
Willy Tarreaua1629a52012-11-13 20:48:15 +0100715 while ((lines_max < 0 || lines_out < lines_max) && fgets2(stdin) != NULL)
Willy Tarreaue1a908c2012-01-03 09:23:03 +0100716 lines_out++;
717
718 goto skip_filters;
719 }
720
Willy Tarreau214c2032009-02-20 11:02:32 +0100721 while ((line = fgets2(stdin)) != NULL) {
Willy Tarreau72c28532009-01-22 18:56:50 +0100722 linenum++;
Willy Tarreau26deaf52011-07-10 19:47:48 +0200723 time_field = NULL; accept_field = NULL;
Willy Tarreau72c28532009-01-22 18:56:50 +0100724
Willy Tarreau5bdfd962009-10-14 15:16:29 +0200725 test = 1;
Willy Tarreau26deaf52011-07-10 19:47:48 +0200726
727 /* for any line we process, we first ensure that there is a field
728 * looking like the accept date field (beginning with a '[').
729 */
730 accept_field = field_start(line, ACCEPT_FIELD + skip_fields);
Willy Tarreaua2b39fb2011-07-10 21:39:35 +0200731 if (unlikely(*accept_field != '[')) {
Willy Tarreau26deaf52011-07-10 19:47:48 +0200732 parse_err++;
733 continue;
734 }
735
736 /* the day of month field is begin 01 and 31 */
Willy Tarreaua2b39fb2011-07-10 21:39:35 +0200737 if (accept_field[1] < '0' || accept_field[1] > '3') {
Willy Tarreau26deaf52011-07-10 19:47:48 +0200738 parse_err++;
739 continue;
740 }
741
Willy Tarreaua2b39fb2011-07-10 21:39:35 +0200742 if (filter & FILT_HTTP_ONLY) {
Willy Tarreau70c428f2011-07-10 17:27:40 +0200743 /* only report lines with at least 4 timers */
Willy Tarreaua2b39fb2011-07-10 21:39:35 +0200744 if (!time_field) {
Willy Tarreau26deaf52011-07-10 19:47:48 +0200745 time_field = field_start(accept_field, TIME_FIELD - ACCEPT_FIELD + 1);
Willy Tarreaua2b39fb2011-07-10 21:39:35 +0200746 if (unlikely(!*time_field)) {
Willy Tarreau26deaf52011-07-10 19:47:48 +0200747 truncated_line(linenum, line);
748 continue;
749 }
Willy Tarreau70c428f2011-07-10 17:27:40 +0200750 }
751
Willy Tarreau758a6ea2011-07-10 18:53:44 +0200752 e = field_stop(time_field + 1);
753 /* we have field TIME_FIELD in [time_field]..[e-1] */
754 p = time_field;
Willy Tarreau70c428f2011-07-10 17:27:40 +0200755 f = 0;
Willy Tarreaudf6f0d12011-07-10 18:15:08 +0200756 while (!SEP(*p)) {
Willy Tarreau70c428f2011-07-10 17:27:40 +0200757 if (++f == 4)
758 break;
759 SKIP_CHAR(p, '/');
760 }
761 test &= (f >= 4);
762 }
763
Willy Tarreaua2b39fb2011-07-10 21:39:35 +0200764 if (filter & FILT_TIME_RESP) {
Willy Tarreau5bdfd962009-10-14 15:16:29 +0200765 int tps;
766
767 /* only report lines with response times larger than filter_time_resp */
Willy Tarreaua2b39fb2011-07-10 21:39:35 +0200768 if (!time_field) {
Willy Tarreau26deaf52011-07-10 19:47:48 +0200769 time_field = field_start(accept_field, TIME_FIELD - ACCEPT_FIELD + 1);
Willy Tarreaua2b39fb2011-07-10 21:39:35 +0200770 if (unlikely(!*time_field)) {
Willy Tarreau26deaf52011-07-10 19:47:48 +0200771 truncated_line(linenum, line);
772 continue;
773 }
Willy Tarreau5bdfd962009-10-14 15:16:29 +0200774 }
775
Willy Tarreau758a6ea2011-07-10 18:53:44 +0200776 e = field_stop(time_field + 1);
777 /* we have field TIME_FIELD in [time_field]..[e-1], let's check only the response time */
Willy Tarreau5bdfd962009-10-14 15:16:29 +0200778
Willy Tarreau758a6ea2011-07-10 18:53:44 +0200779 p = time_field;
Willy Tarreau5bdfd962009-10-14 15:16:29 +0200780 err = 0;
Willy Tarreau24bcb4f2010-10-28 20:39:50 +0200781 f = 0;
Willy Tarreaudf6f0d12011-07-10 18:15:08 +0200782 while (!SEP(*p)) {
Willy Tarreau5bdfd962009-10-14 15:16:29 +0200783 tps = str2ic(p);
784 if (tps < 0) {
785 tps = -1;
786 err = 1;
787 }
Willy Tarreau24bcb4f2010-10-28 20:39:50 +0200788 if (++f == 4)
789 break;
Willy Tarreau5bdfd962009-10-14 15:16:29 +0200790 SKIP_CHAR(p, '/');
791 }
792
Willy Tarreaua2b39fb2011-07-10 21:39:35 +0200793 if (unlikely(f < 4)) {
Willy Tarreau5bdfd962009-10-14 15:16:29 +0200794 parse_err++;
795 continue;
796 }
797
798 test &= (tps >= filter_time_resp) ^ !!(filter & FILT_INVERT_TIME_RESP);
799 }
800
Willy Tarreaud3007ff2011-09-05 02:07:23 +0200801 if (filter & (FILT_ERRORS_ONLY | FILT_HTTP_STATUS)) {
802 /* Check both error codes (-1, 5xx) and status code ranges */
Willy Tarreau26deaf52011-07-10 19:47:48 +0200803 if (time_field)
804 b = field_start(time_field, STATUS_FIELD - TIME_FIELD + 1);
805 else
806 b = field_start(accept_field, STATUS_FIELD - ACCEPT_FIELD + 1);
807
Willy Tarreaua2b39fb2011-07-10 21:39:35 +0200808 if (unlikely(!*b)) {
Willy Tarreau72c28532009-01-22 18:56:50 +0100809 truncated_line(linenum, line);
810 continue;
811 }
Willy Tarreaua2b39fb2011-07-10 21:39:35 +0200812
Willy Tarreaud3007ff2011-09-05 02:07:23 +0200813 val = str2ic(b);
814 if (filter & FILT_ERRORS_ONLY)
815 test &= (val < 0 || (val >= 500 && val <= 599)) ^ !!(filter & FILT_INVERT_ERRORS);
816
817 if (filter & FILT_HTTP_STATUS)
818 test &= (val >= filt_http_status_low && val <= filt_http_status_high) ^ !!(filter & FILT_INVERT_HTTP_STATUS);
Willy Tarreau72c28532009-01-22 18:56:50 +0100819 }
820
Willy Tarreau08911ff2011-10-13 13:28:36 +0200821 if (filter & (FILT_QUEUE_ONLY|FILT_QUEUE_SRV_ONLY)) {
822 /* Check if the server's queue is non-nul */
823 if (time_field)
824 b = field_start(time_field, QUEUE_LEN_FIELD - TIME_FIELD + 1);
825 else
826 b = field_start(accept_field, QUEUE_LEN_FIELD - ACCEPT_FIELD + 1);
827
828 if (unlikely(!*b)) {
829 truncated_line(linenum, line);
830 continue;
831 }
832
833 if (*b == '0') {
834 if (filter & FILT_QUEUE_SRV_ONLY) {
835 test = 0;
836 }
837 else {
838 do {
839 b++;
840 if (*b == '/') {
841 b++;
842 break;
843 }
844 } while (*b);
845 test &= ((unsigned char)(*b - '1') < 9);
846 }
847 }
848 }
849
Hervé COMMOWICK927cddd2011-08-10 17:42:41 +0200850 if (filter & FILT_TERM_CODE_NAME) {
851 /* only report corresponding termination code name */
852 if (time_field)
853 b = field_start(time_field, TERM_CODES_FIELD - TIME_FIELD + 1);
854 else
855 b = field_start(accept_field, TERM_CODES_FIELD - ACCEPT_FIELD + 1);
856
857 if (unlikely(!*b)) {
858 truncated_line(linenum, line);
859 continue;
860 }
861
862 test &= (b[0] == filter_term_code_name[0] && b[1] == filter_term_code_name[1]) ^ !!(filter & FILT_INVERT_TERM_CODE_NAME);
863 }
864
865
Willy Tarreau0f423a72010-05-03 10:50:54 +0200866 test ^= filter_invert;
867 if (!test)
868 continue;
869
Willy Tarreaua2b39fb2011-07-10 21:39:35 +0200870 /************** here we process inputs *******************/
Willy Tarreau72c28532009-01-22 18:56:50 +0100871
Willy Tarreaua2b39fb2011-07-10 21:39:35 +0200872 if (line_filter)
873 line_filter(accept_field, time_field, &t);
874 else
Willy Tarreaua1629a52012-11-13 20:48:15 +0100875 lines_out++; /* FILT_COUNT_ONLY was used, so we're just counting lines */
876 if (lines_max >= 0 && lines_out >= lines_max)
Willy Tarreau667c9052012-10-10 16:49:28 +0200877 break;
Willy Tarreaua2b39fb2011-07-10 21:39:35 +0200878 }
Willy Tarreauabe45b62010-10-28 20:33:46 +0200879
Willy Tarreaue1a908c2012-01-03 09:23:03 +0100880 skip_filters:
Willy Tarreaua2b39fb2011-07-10 21:39:35 +0200881 /*****************************************************
882 * Here we've finished reading all input. Depending on the
883 * filters, we may still have some analysis to run on the
884 * collected data and to output data in a new format.
885 *************************************************** */
Willy Tarreau72c28532009-01-22 18:56:50 +0100886
887 if (t)
888 free(t);
889
890 if (filter & FILT_COUNT_ONLY) {
Willy Tarreaua2b39fb2011-07-10 21:39:35 +0200891 printf("%d\n", lines_out);
Willy Tarreau72c28532009-01-22 18:56:50 +0100892 exit(0);
893 }
894
Willy Tarreau72c28532009-01-22 18:56:50 +0100895 if (filter & (FILT_ACC_COUNT|FILT_ACC_DELAY)) {
896 /* sort and count all timers. Output will look like this :
897 * <accept_date> <delta_ms from previous one> <nb entries>
898 */
899 n = eb32_first(&timers[0]);
900
901 if (n)
902 last = n->key;
903 while (n) {
904 unsigned int d, h, m, s, ms;
905
906 t = container_of(n, struct timer, node);
907 h = n->key;
908 d = h - last;
909 last = h;
910
911 if (d >= filter_acc_delay && t->count >= filter_acc_count) {
912 ms = h % 1000; h = h / 1000;
913 s = h % 60; h = h / 60;
914 m = h % 60; h = h / 60;
Willy Tarreau72c28532009-01-22 18:56:50 +0100915 printf("%02d:%02d:%02d.%03d %d %d %d\n", h, m, s, ms, last, d, t->count);
Willy Tarreau667c9052012-10-10 16:49:28 +0200916 lines_out++;
Willy Tarreaua1629a52012-11-13 20:48:15 +0100917 if (lines_max >= 0 && lines_out >= lines_max)
Willy Tarreau667c9052012-10-10 16:49:28 +0200918 break;
Willy Tarreau72c28532009-01-22 18:56:50 +0100919 }
920 n = eb32_next(n);
921 }
922 }
923 else if (filter & FILT_GRAPH_TIMERS) {
924 /* sort all timers */
925 for (f = 0; f < 5; f++) {
926 struct eb32_node *n;
927 int val;
928
929 val = 0;
930 n = eb32_first(&timers[f]);
931 while (n) {
932 int i;
933 double d;
934
935 t = container_of(n, struct timer, node);
936 last = n->key;
937 val = t->count;
938
939 i = (last < 0) ? -last : last;
940 i = fls_auto(i) - QBITS;
941
942 if (i > 0)
943 d = val / (double)(1 << i);
944 else
945 d = val;
946
Willy Tarreaua1629a52012-11-13 20:48:15 +0100947 if (d > 0.0)
Willy Tarreau72c28532009-01-22 18:56:50 +0100948 printf("%d %d %f\n", f, last, d+1.0);
Willy Tarreau72c28532009-01-22 18:56:50 +0100949
950 n = eb32_next(n);
951 }
Willy Tarreau214c2032009-02-20 11:02:32 +0100952 }
953 }
954 else if (filter & FILT_PERCENTILE) {
955 /* report timers by percentile :
956 * <percent> <total> <max_req_time> <max_conn_time> <max_resp_time> <max_data_time>
957 * We don't count errs.
958 */
959 struct eb32_node *n[5];
960 unsigned long cum[5];
961 double step;
962
Willy Tarreaua2b39fb2011-07-10 21:39:35 +0200963 if (!lines_out)
Willy Tarreau910ba4b2009-11-17 10:16:19 +0100964 goto empty;
965
Willy Tarreau214c2032009-02-20 11:02:32 +0100966 for (f = 1; f < 5; f++) {
967 n[f] = eb32_first(&timers[f]);
968 cum[f] = container_of(n[f], struct timer, node)->count;
969 }
970
971 for (step = 1; step <= 1000;) {
Willy Tarreaua2b39fb2011-07-10 21:39:35 +0200972 unsigned int thres = lines_out * (step / 1000.0);
Willy Tarreau214c2032009-02-20 11:02:32 +0100973
974 printf("%3.1f %d ", step/10.0, thres);
975 for (f = 1; f < 5; f++) {
976 struct eb32_node *next;
977 while (cum[f] < thres) {
978 /* need to find other keys */
979 next = eb32_next(n[f]);
980 if (!next)
981 break;
982 n[f] = next;
983 cum[f] += container_of(next, struct timer, node)->count;
984 }
985
986 /* value still within $step % of total */
987 printf("%d ", n[f]->key);
988 }
989 putchar('\n');
990 if (step >= 100 && step < 900)
991 step += 50; // jump 5% by 5% between those steps.
992 else if (step >= 20 && step < 980)
993 step += 10;
994 else
995 step += 1;
Willy Tarreau72c28532009-01-22 18:56:50 +0100996 }
997 }
Willy Tarreau0f423a72010-05-03 10:50:54 +0200998 else if (filter & FILT_COUNT_STATUS) {
999 /* output all statuses in the form of <status> <occurrences> */
1000 n = eb32_first(&timers[0]);
1001 while (n) {
1002 t = container_of(n, struct timer, node);
1003 printf("%d %d\n", n->key, t->count);
Willy Tarreaua2b39fb2011-07-10 21:39:35 +02001004 lines_out++;
Willy Tarreaua1629a52012-11-13 20:48:15 +01001005 if (lines_max >= 0 && lines_out >= lines_max)
Willy Tarreau667c9052012-10-10 16:49:28 +02001006 break;
Willy Tarreau0f423a72010-05-03 10:50:54 +02001007 n = eb32_next(n);
1008 }
1009 }
Willy Tarreaua2b39fb2011-07-10 21:39:35 +02001010 else if (filter & FILT_COUNT_SRV_STATUS) {
Willy Tarreaud2201062010-05-27 18:17:30 +02001011 struct ebmb_node *srv_node;
1012 struct srv_st *srv;
1013
1014 printf("#srv_name 1xx 2xx 3xx 4xx 5xx other tot_req req_ok pct_ok avg_ct avg_rt\n");
1015
1016 srv_node = ebmb_first(&timers[0]);
1017 while (srv_node) {
1018 int tot_rq;
1019
1020 srv = container_of(srv_node, struct srv_st, node);
1021
1022 tot_rq = 0;
1023 for (f = 0; f <= 5; f++)
1024 tot_rq += srv->st_cnt[f];
1025
1026 printf("%s %d %d %d %d %d %d %d %d %.1f %d %d\n",
1027 srv_node->key, srv->st_cnt[1], srv->st_cnt[2],
1028 srv->st_cnt[3], srv->st_cnt[4], srv->st_cnt[5], srv->st_cnt[0],
1029 tot_rq,
1030 srv->nb_ok, (double)srv->nb_ok * 100.0 / (tot_rq?tot_rq:1),
1031 (int)(srv->cum_ct / (srv->nb_ct?srv->nb_ct:1)), (int)(srv->cum_rt / (srv->nb_rt?srv->nb_rt:1)));
1032 srv_node = ebmb_next(srv_node);
Willy Tarreaua2b39fb2011-07-10 21:39:35 +02001033 lines_out++;
Willy Tarreaua1629a52012-11-13 20:48:15 +01001034 if (lines_max >= 0 && lines_out >= lines_max)
Willy Tarreau667c9052012-10-10 16:49:28 +02001035 break;
Willy Tarreaud2201062010-05-27 18:17:30 +02001036 }
1037 }
Willy Tarreau8a09b662012-10-10 10:26:22 +02001038 else if (filter & (FILT_COUNT_TERM_CODES|FILT_COUNT_COOK_CODES)) {
Willy Tarreaud8fc1102010-09-12 17:56:16 +02001039 /* output all statuses in the form of <code> <occurrences> */
1040 n = eb32_first(&timers[0]);
1041 while (n) {
1042 t = container_of(n, struct timer, node);
1043 printf("%c%c %d\n", (n->key >> 8), (n->key) & 255, t->count);
Willy Tarreaua2b39fb2011-07-10 21:39:35 +02001044 lines_out++;
Willy Tarreaua1629a52012-11-13 20:48:15 +01001045 if (lines_max >= 0 && lines_out >= lines_max)
Willy Tarreau667c9052012-10-10 16:49:28 +02001046 break;
Willy Tarreaud8fc1102010-09-12 17:56:16 +02001047 n = eb32_next(n);
1048 }
1049 }
Willy Tarreaua2b39fb2011-07-10 21:39:35 +02001050 else if (filter & FILT_COUNT_URL_ANY) {
Willy Tarreauabe45b62010-10-28 20:33:46 +02001051 struct eb_node *node, *next;
1052
1053 if (!(filter & FILT_COUNT_URL_ONLY)) {
1054 /* we have to sort on another criterion. We'll use timers[1] for the
1055 * destination tree.
1056 */
1057
1058 timers[1] = EB_ROOT; /* reconfigure to accept duplicates */
1059 for (node = eb_first(&timers[0]); node; node = next) {
1060 next = eb_next(node);
1061 eb_delete(node);
1062
1063 ustat = container_of(node, struct url_stat, node.url.node);
1064
1065 if (filter & FILT_COUNT_URL_COUNT)
1066 ustat->node.val.key = ustat->nb_req;
1067 else if (filter & FILT_COUNT_URL_ERR)
1068 ustat->node.val.key = ustat->nb_err;
1069 else if (filter & FILT_COUNT_URL_TTOT)
1070 ustat->node.val.key = ustat->total_time;
1071 else if (filter & FILT_COUNT_URL_TAVG)
1072 ustat->node.val.key = ustat->nb_req ? ustat->total_time / ustat->nb_req : 0;
1073 else if (filter & FILT_COUNT_URL_TTOTO)
1074 ustat->node.val.key = ustat->total_time_ok;
1075 else if (filter & FILT_COUNT_URL_TAVGO)
1076 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 +02001077 else if (filter & FILT_COUNT_URL_BAVG)
1078 ustat->node.val.key = ustat->nb_req ? ustat->total_bytes_sent / ustat->nb_req : 0;
1079 else if (filter & FILT_COUNT_URL_BTOT)
1080 ustat->node.val.key = ustat->total_bytes_sent;
Willy Tarreauabe45b62010-10-28 20:33:46 +02001081 else
1082 ustat->node.val.key = 0;
1083
1084 eb64_insert(&timers[1], &ustat->node.val);
1085 }
1086 /* switch trees */
1087 timers[0] = timers[1];
1088 }
1089
Baptiste61aaad02012-09-08 23:10:03 +02001090 printf("#req err ttot tavg oktot okavg bavg btot url\n");
Willy Tarreauabe45b62010-10-28 20:33:46 +02001091
1092 /* scan the tree in its reverse sorting order */
1093 node = eb_last(&timers[0]);
1094 while (node) {
1095 ustat = container_of(node, struct url_stat, node.url.node);
Baptiste61aaad02012-09-08 23:10:03 +02001096 printf("%d %d %Ld %Ld %Ld %Ld %Ld %Ld %s\n",
Willy Tarreauabe45b62010-10-28 20:33:46 +02001097 ustat->nb_req,
1098 ustat->nb_err,
1099 ustat->total_time,
1100 ustat->nb_req ? ustat->total_time / ustat->nb_req : 0,
1101 ustat->total_time_ok,
1102 (ustat->nb_req - ustat->nb_err) ? ustat->total_time_ok / (ustat->nb_req - ustat->nb_err) : 0,
Baptiste61aaad02012-09-08 23:10:03 +02001103 ustat->nb_req ? ustat->total_bytes_sent / ustat->nb_req : 0,
1104 ustat->total_bytes_sent,
Willy Tarreauabe45b62010-10-28 20:33:46 +02001105 ustat->url);
1106
1107 node = eb_prev(node);
Willy Tarreaua2b39fb2011-07-10 21:39:35 +02001108 lines_out++;
Willy Tarreaua1629a52012-11-13 20:48:15 +01001109 if (lines_max >= 0 && lines_out >= lines_max)
Willy Tarreau667c9052012-10-10 16:49:28 +02001110 break;
Willy Tarreauabe45b62010-10-28 20:33:46 +02001111 }
1112 }
Willy Tarreaud2201062010-05-27 18:17:30 +02001113
Willy Tarreau910ba4b2009-11-17 10:16:19 +01001114 empty:
Willy Tarreau72c28532009-01-22 18:56:50 +01001115 if (!(filter & FILT_QUIET))
1116 fprintf(stderr, "%d lines in, %d lines out, %d parsing errors\n",
Willy Tarreaua2b39fb2011-07-10 21:39:35 +02001117 linenum, lines_out, parse_err);
Willy Tarreau72c28532009-01-22 18:56:50 +01001118 exit(0);
1119}
1120
Willy Tarreaua2b39fb2011-07-10 21:39:35 +02001121void filter_output_line(const char *accept_field, const char *time_field, struct timer **tptr)
1122{
1123 puts(line);
1124 lines_out++;
1125}
1126
1127void filter_accept_holes(const char *accept_field, const char *time_field, struct timer **tptr)
1128{
1129 struct timer *t2;
1130 int val;
1131
1132 val = convert_date(accept_field);
1133 if (unlikely(val < 0)) {
1134 truncated_line(linenum, line);
1135 return;
1136 }
1137
1138 t2 = insert_value(&timers[0], tptr, val);
1139 t2->count++;
Willy Tarreaua2b39fb2011-07-10 21:39:35 +02001140 return;
1141}
1142
1143void filter_count_status(const char *accept_field, const char *time_field, struct timer **tptr)
1144{
1145 struct timer *t2;
1146 const char *b;
1147 int val;
1148
1149 if (time_field)
1150 b = field_start(time_field, STATUS_FIELD - TIME_FIELD + 1);
1151 else
1152 b = field_start(accept_field, STATUS_FIELD - ACCEPT_FIELD + 1);
1153
1154 if (unlikely(!*b)) {
1155 truncated_line(linenum, line);
1156 return;
1157 }
1158
1159 val = str2ic(b);
1160
1161 t2 = insert_value(&timers[0], tptr, val);
1162 t2->count++;
1163}
1164
Willy Tarreau8a09b662012-10-10 10:26:22 +02001165void filter_count_cook_codes(const char *accept_field, const char *time_field, struct timer **tptr)
1166{
1167 struct timer *t2;
1168 const char *b;
1169 int val;
1170
1171 if (time_field)
1172 b = field_start(time_field, TERM_CODES_FIELD - TIME_FIELD + 1);
1173 else
1174 b = field_start(accept_field, TERM_CODES_FIELD - ACCEPT_FIELD + 1);
1175
1176 if (unlikely(!*b)) {
1177 truncated_line(linenum, line);
1178 return;
1179 }
1180
1181 val = 256 * b[2] + b[3];
1182
1183 t2 = insert_value(&timers[0], tptr, val);
1184 t2->count++;
1185}
1186
Willy Tarreaua2b39fb2011-07-10 21:39:35 +02001187void filter_count_term_codes(const char *accept_field, const char *time_field, struct timer **tptr)
1188{
1189 struct timer *t2;
1190 const char *b;
1191 int val;
1192
1193 if (time_field)
1194 b = field_start(time_field, TERM_CODES_FIELD - TIME_FIELD + 1);
1195 else
1196 b = field_start(accept_field, TERM_CODES_FIELD - ACCEPT_FIELD + 1);
1197
1198 if (unlikely(!*b)) {
1199 truncated_line(linenum, line);
1200 return;
1201 }
1202
1203 val = 256 * b[0] + b[1];
1204
1205 t2 = insert_value(&timers[0], tptr, val);
1206 t2->count++;
1207}
1208
1209void filter_count_srv_status(const char *accept_field, const char *time_field, struct timer **tptr)
1210{
1211 const char *b, *e, *p;
1212 int f, err, array[5];
1213 struct ebmb_node *srv_node;
1214 struct srv_st *srv;
1215 int val;
1216
1217 /* the server field is before the status field, so let's
1218 * parse them in the proper order.
1219 */
1220 b = field_start(accept_field, SERVER_FIELD - ACCEPT_FIELD + 1);
1221 if (unlikely(!*b)) {
1222 truncated_line(linenum, line);
1223 return;
1224 }
1225
1226 e = field_stop(b + 1); /* we have the server name in [b]..[e-1] */
1227
1228 /* the chance that a server name already exists is extremely high,
1229 * so let's perform a normal lookup first.
1230 */
1231 srv_node = ebst_lookup_len(&timers[0], b, e - b);
1232 srv = container_of(srv_node, struct srv_st, node);
1233
1234 if (!srv_node) {
1235 /* server not yet in the tree, let's create it */
1236 srv = (void *)calloc(1, sizeof(struct srv_st) + e - b + 1);
1237 srv_node = &srv->node;
1238 memcpy(&srv_node->key, b, e - b);
1239 srv_node->key[e - b] = '\0';
1240 ebst_insert(&timers[0], srv_node);
1241 }
1242
1243 /* let's collect the connect and response times */
1244 if (!time_field) {
1245 time_field = field_start(e, TIME_FIELD - SERVER_FIELD);
1246 if (unlikely(!*time_field)) {
1247 truncated_line(linenum, line);
1248 return;
1249 }
1250 }
1251
1252 e = field_stop(time_field + 1);
1253 /* we have field TIME_FIELD in [time_field]..[e-1] */
1254
1255 p = time_field;
1256 err = 0;
1257 f = 0;
1258 while (!SEP(*p)) {
1259 array[f] = str2ic(p);
1260 if (array[f] < 0) {
1261 array[f] = -1;
1262 err = 1;
1263 }
1264 if (++f == 5)
1265 break;
1266 SKIP_CHAR(p, '/');
1267 }
1268
1269 if (unlikely(f < 5)){
1270 parse_err++;
1271 return;
1272 }
1273
1274 /* OK we have our timers in array[2,3] */
1275 if (!err)
1276 srv->nb_ok++;
1277
1278 if (array[2] >= 0) {
1279 srv->cum_ct += array[2];
1280 srv->nb_ct++;
1281 }
1282
1283 if (array[3] >= 0) {
1284 srv->cum_rt += array[3];
1285 srv->nb_rt++;
1286 }
1287
1288 /* we're interested in the 5 HTTP status classes (1xx ... 5xx), and
1289 * the invalid ones which will be reported as 0.
1290 */
1291 b = field_start(e, STATUS_FIELD - TIME_FIELD);
1292 if (unlikely(!*b)) {
1293 truncated_line(linenum, line);
1294 return;
1295 }
1296
1297 val = 0;
1298 if (*b >= '1' && *b <= '5')
1299 val = *b - '0';
1300
1301 srv->st_cnt[val]++;
1302}
1303
1304void filter_count_url(const char *accept_field, const char *time_field, struct timer **tptr)
1305{
1306 struct url_stat *ustat = NULL;
1307 struct ebpt_node *ebpt_old;
1308 const char *b, *e;
1309 int f, err, array[5];
Baptiste61aaad02012-09-08 23:10:03 +02001310 int val;
Willy Tarreaua2b39fb2011-07-10 21:39:35 +02001311
1312 /* let's collect the response time */
1313 if (!time_field) {
1314 time_field = field_start(accept_field, TIME_FIELD - ACCEPT_FIELD + 1); // avg 115 ns per line
1315 if (unlikely(!*time_field)) {
1316 truncated_line(linenum, line);
1317 return;
1318 }
1319 }
1320
1321 /* we have the field TIME_FIELD starting at <time_field>. We'll
1322 * parse the 5 timers to detect errors, it takes avg 55 ns per line.
1323 */
1324 e = time_field; err = 0; f = 0;
1325 while (!SEP(*e)) {
1326 array[f] = str2ic(e);
1327 if (array[f] < 0) {
1328 array[f] = -1;
1329 err = 1;
1330 }
1331 if (++f == 5)
1332 break;
1333 SKIP_CHAR(e, '/');
1334 }
1335 if (f < 5) {
1336 parse_err++;
1337 return;
1338 }
1339
1340 /* OK we have our timers in array[3], and err is >0 if at
1341 * least one -1 was seen. <e> points to the first char of
1342 * the last timer. Let's prepare a new node with that.
1343 */
1344 if (unlikely(!ustat))
1345 ustat = calloc(1, sizeof(*ustat));
1346
1347 ustat->nb_err = err;
1348 ustat->nb_req = 1;
1349
1350 /* use array[4] = total time in case of error */
1351 ustat->total_time = (array[3] >= 0) ? array[3] : array[4];
1352 ustat->total_time_ok = (array[3] >= 0) ? array[3] : 0;
1353
Baptiste61aaad02012-09-08 23:10:03 +02001354 e = field_start(e, BYTES_SENT_FIELD - TIME_FIELD + 1);
1355 val = str2ic(e);
1356 ustat->total_bytes_sent = val;
1357
Willy Tarreaua2b39fb2011-07-10 21:39:35 +02001358 /* the line may be truncated because of a bad request or anything like this,
1359 * without a method. Also, if it does not begin with an quote, let's skip to
1360 * the next field because it's a capture. Let's fall back to the "method" itself
1361 * if there's nothing else.
1362 */
Baptiste61aaad02012-09-08 23:10:03 +02001363 e = field_start(e, METH_FIELD - BYTES_SENT_FIELD + 1);
Willy Tarreau61a40c72011-09-06 08:11:27 +02001364 while (*e != '"' && *e) {
1365 /* Note: some syslog servers escape quotes ! */
1366 if (*e == '\\' && e[1] == '"')
1367 break;
Willy Tarreaua2b39fb2011-07-10 21:39:35 +02001368 e = field_start(e, 2);
Willy Tarreau61a40c72011-09-06 08:11:27 +02001369 }
Willy Tarreaua2b39fb2011-07-10 21:39:35 +02001370
1371 if (unlikely(!*e)) {
1372 truncated_line(linenum, line);
1373 return;
1374 }
1375
1376 b = field_start(e, URL_FIELD - METH_FIELD + 1); // avg 40 ns per line
1377 if (!*b)
1378 b = e;
1379
1380 /* stop at end of field or first ';' or '?', takes avg 64 ns per line */
1381 e = b;
1382 do {
Willy Tarreau14389e72011-07-10 22:11:17 +02001383 if (*e == ' ' || *e == '?' || *e == ';') {
Willy Tarreaua2b39fb2011-07-10 21:39:35 +02001384 *(char *)e = 0;
1385 break;
1386 }
1387 e++;
1388 } while (*e);
1389
1390 /* now instead of copying the URL for a simple lookup, we'll link
1391 * to it from the node we're trying to insert. If it returns a
1392 * different value, it was already there. Otherwise we just have
1393 * to dynamically realloc an entry using strdup().
1394 */
1395 ustat->node.url.key = (char *)b;
1396 ebpt_old = ebis_insert(&timers[0], &ustat->node.url);
1397
1398 if (ebpt_old != &ustat->node.url) {
1399 struct url_stat *ustat_old;
1400 /* node was already there, let's update previous one */
1401 ustat_old = container_of(ebpt_old, struct url_stat, node.url);
1402 ustat_old->nb_req ++;
1403 ustat_old->nb_err += ustat->nb_err;
1404 ustat_old->total_time += ustat->total_time;
1405 ustat_old->total_time_ok += ustat->total_time_ok;
Baptiste61aaad02012-09-08 23:10:03 +02001406 ustat_old->total_bytes_sent += ustat->total_bytes_sent;
Willy Tarreaua2b39fb2011-07-10 21:39:35 +02001407 } else {
1408 ustat->url = ustat->node.url.key = strdup(ustat->node.url.key);
1409 ustat = NULL; /* node was used */
1410 }
1411}
1412
1413void filter_graphs(const char *accept_field, const char *time_field, struct timer **tptr)
1414{
1415 struct timer *t2;
1416 const char *e, *p;
1417 int f, err, array[5];
1418
1419 if (!time_field) {
1420 time_field = field_start(accept_field, TIME_FIELD - ACCEPT_FIELD + 1);
1421 if (unlikely(!*time_field)) {
1422 truncated_line(linenum, line);
1423 return;
1424 }
1425 }
1426
1427 e = field_stop(time_field + 1);
1428 /* we have field TIME_FIELD in [time_field]..[e-1] */
1429
1430 p = time_field;
1431 err = 0;
1432 f = 0;
1433 while (!SEP(*p)) {
1434 array[f] = str2ic(p);
1435 if (array[f] < 0) {
1436 array[f] = -1;
1437 err = 1;
1438 }
1439 if (++f == 5)
1440 break;
1441 SKIP_CHAR(p, '/');
1442 }
1443
1444 if (unlikely(f < 5)) {
1445 parse_err++;
1446 return;
1447 }
1448
1449 /* if we find at least one negative time, we count one error
1450 * with a time equal to the total session time. This will
1451 * emphasize quantum timing effects associated to known
1452 * timeouts. Note that on some buggy machines, it is possible
1453 * that the total time is negative, hence the reason to reset
1454 * it.
1455 */
1456
1457 if (filter & FILT_GRAPH_TIMERS) {
1458 if (err) {
1459 if (array[4] < 0)
1460 array[4] = -1;
1461 t2 = insert_timer(&timers[0], tptr, array[4]); // total time
1462 t2->count++;
1463 } else {
1464 int v;
1465
1466 t2 = insert_timer(&timers[1], tptr, array[0]); t2->count++; // req
1467 t2 = insert_timer(&timers[2], tptr, array[2]); t2->count++; // conn
1468 t2 = insert_timer(&timers[3], tptr, array[3]); t2->count++; // resp
1469
1470 v = array[4] - array[0] - array[1] - array[2] - array[3]; // data time
1471 if (v < 0 && !(filter & FILT_QUIET))
1472 fprintf(stderr, "ERR: %s (%d %d %d %d %d => %d)\n",
1473 line, array[0], array[1], array[2], array[3], array[4], v);
1474 t2 = insert_timer(&timers[4], tptr, v); t2->count++;
1475 lines_out++;
1476 }
1477 } else { /* percentile */
1478 if (err) {
1479 if (array[4] < 0)
1480 array[4] = -1;
1481 t2 = insert_value(&timers[0], tptr, array[4]); // total time
1482 t2->count++;
1483 } else {
1484 int v;
1485
1486 t2 = insert_value(&timers[1], tptr, array[0]); t2->count++; // req
1487 t2 = insert_value(&timers[2], tptr, array[2]); t2->count++; // conn
1488 t2 = insert_value(&timers[3], tptr, array[3]); t2->count++; // resp
1489
1490 v = array[4] - array[0] - array[1] - array[2] - array[3]; // data time
1491 if (v < 0 && !(filter & FILT_QUIET))
1492 fprintf(stderr, "ERR: %s (%d %d %d %d %d => %d)\n",
1493 line, array[0], array[1], array[2], array[3], array[4], v);
1494 t2 = insert_value(&timers[4], tptr, v); t2->count++;
1495 lines_out++;
1496 }
1497 }
1498}
1499
1500
Willy Tarreau72c28532009-01-22 18:56:50 +01001501/*
1502 * Local variables:
1503 * c-indent-level: 8
1504 * c-basic-offset: 8
1505 * End:
1506 */