blob: 7e16cd55623c6e770abb28d7cbf1ceeb57c839aa [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 Tarreau667c9052012-10-10 16:49:28 +0200711 if (!line_filter && lines_max >= 0 &&
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))) {
713 /* read the whole file at once first */
714 if (!filter_invert)
715 while (fgets2(stdin) != NULL)
716 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
875 lines_out++; /* we're just counting lines */
Willy Tarreau667c9052012-10-10 16:49:28 +0200876 if (lines_out >= lines_max)
877 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++;
917 if (lines_out >= lines_max)
918 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
947 if (d > 0.0) {
948 printf("%d %d %f\n", f, last, d+1.0);
Willy Tarreaua2b39fb2011-07-10 21:39:35 +0200949 lines_out++;
Willy Tarreau667c9052012-10-10 16:49:28 +0200950 if (lines_out >= lines_max)
951 break;
Willy Tarreau72c28532009-01-22 18:56:50 +0100952 }
953
954 n = eb32_next(n);
955 }
Willy Tarreau214c2032009-02-20 11:02:32 +0100956 }
957 }
958 else if (filter & FILT_PERCENTILE) {
959 /* report timers by percentile :
960 * <percent> <total> <max_req_time> <max_conn_time> <max_resp_time> <max_data_time>
961 * We don't count errs.
962 */
963 struct eb32_node *n[5];
964 unsigned long cum[5];
965 double step;
966
Willy Tarreaua2b39fb2011-07-10 21:39:35 +0200967 if (!lines_out)
Willy Tarreau910ba4b2009-11-17 10:16:19 +0100968 goto empty;
969
Willy Tarreau214c2032009-02-20 11:02:32 +0100970 for (f = 1; f < 5; f++) {
971 n[f] = eb32_first(&timers[f]);
972 cum[f] = container_of(n[f], struct timer, node)->count;
973 }
974
975 for (step = 1; step <= 1000;) {
Willy Tarreaua2b39fb2011-07-10 21:39:35 +0200976 unsigned int thres = lines_out * (step / 1000.0);
Willy Tarreau214c2032009-02-20 11:02:32 +0100977
978 printf("%3.1f %d ", step/10.0, thres);
979 for (f = 1; f < 5; f++) {
980 struct eb32_node *next;
981 while (cum[f] < thres) {
982 /* need to find other keys */
983 next = eb32_next(n[f]);
984 if (!next)
985 break;
986 n[f] = next;
987 cum[f] += container_of(next, struct timer, node)->count;
988 }
989
990 /* value still within $step % of total */
991 printf("%d ", n[f]->key);
992 }
993 putchar('\n');
994 if (step >= 100 && step < 900)
995 step += 50; // jump 5% by 5% between those steps.
996 else if (step >= 20 && step < 980)
997 step += 10;
998 else
999 step += 1;
Willy Tarreau72c28532009-01-22 18:56:50 +01001000 }
1001 }
Willy Tarreau0f423a72010-05-03 10:50:54 +02001002 else if (filter & FILT_COUNT_STATUS) {
1003 /* output all statuses in the form of <status> <occurrences> */
1004 n = eb32_first(&timers[0]);
1005 while (n) {
1006 t = container_of(n, struct timer, node);
1007 printf("%d %d\n", n->key, t->count);
Willy Tarreaua2b39fb2011-07-10 21:39:35 +02001008 lines_out++;
Willy Tarreau667c9052012-10-10 16:49:28 +02001009 if (lines_out >= lines_max)
1010 break;
Willy Tarreau0f423a72010-05-03 10:50:54 +02001011 n = eb32_next(n);
1012 }
1013 }
Willy Tarreaua2b39fb2011-07-10 21:39:35 +02001014 else if (filter & FILT_COUNT_SRV_STATUS) {
Willy Tarreaud2201062010-05-27 18:17:30 +02001015 struct ebmb_node *srv_node;
1016 struct srv_st *srv;
1017
1018 printf("#srv_name 1xx 2xx 3xx 4xx 5xx other tot_req req_ok pct_ok avg_ct avg_rt\n");
1019
1020 srv_node = ebmb_first(&timers[0]);
1021 while (srv_node) {
1022 int tot_rq;
1023
1024 srv = container_of(srv_node, struct srv_st, node);
1025
1026 tot_rq = 0;
1027 for (f = 0; f <= 5; f++)
1028 tot_rq += srv->st_cnt[f];
1029
1030 printf("%s %d %d %d %d %d %d %d %d %.1f %d %d\n",
1031 srv_node->key, srv->st_cnt[1], srv->st_cnt[2],
1032 srv->st_cnt[3], srv->st_cnt[4], srv->st_cnt[5], srv->st_cnt[0],
1033 tot_rq,
1034 srv->nb_ok, (double)srv->nb_ok * 100.0 / (tot_rq?tot_rq:1),
1035 (int)(srv->cum_ct / (srv->nb_ct?srv->nb_ct:1)), (int)(srv->cum_rt / (srv->nb_rt?srv->nb_rt:1)));
1036 srv_node = ebmb_next(srv_node);
Willy Tarreaua2b39fb2011-07-10 21:39:35 +02001037 lines_out++;
Willy Tarreau667c9052012-10-10 16:49:28 +02001038 if (lines_out >= lines_max)
1039 break;
Willy Tarreaud2201062010-05-27 18:17:30 +02001040 }
1041 }
Willy Tarreau8a09b662012-10-10 10:26:22 +02001042 else if (filter & (FILT_COUNT_TERM_CODES|FILT_COUNT_COOK_CODES)) {
Willy Tarreaud8fc1102010-09-12 17:56:16 +02001043 /* output all statuses in the form of <code> <occurrences> */
1044 n = eb32_first(&timers[0]);
1045 while (n) {
1046 t = container_of(n, struct timer, node);
1047 printf("%c%c %d\n", (n->key >> 8), (n->key) & 255, t->count);
Willy Tarreaua2b39fb2011-07-10 21:39:35 +02001048 lines_out++;
Willy Tarreau667c9052012-10-10 16:49:28 +02001049 if (lines_out >= lines_max)
1050 break;
Willy Tarreaud8fc1102010-09-12 17:56:16 +02001051 n = eb32_next(n);
1052 }
1053 }
Willy Tarreaua2b39fb2011-07-10 21:39:35 +02001054 else if (filter & FILT_COUNT_URL_ANY) {
Willy Tarreauabe45b62010-10-28 20:33:46 +02001055 struct eb_node *node, *next;
1056
1057 if (!(filter & FILT_COUNT_URL_ONLY)) {
1058 /* we have to sort on another criterion. We'll use timers[1] for the
1059 * destination tree.
1060 */
1061
1062 timers[1] = EB_ROOT; /* reconfigure to accept duplicates */
1063 for (node = eb_first(&timers[0]); node; node = next) {
1064 next = eb_next(node);
1065 eb_delete(node);
1066
1067 ustat = container_of(node, struct url_stat, node.url.node);
1068
1069 if (filter & FILT_COUNT_URL_COUNT)
1070 ustat->node.val.key = ustat->nb_req;
1071 else if (filter & FILT_COUNT_URL_ERR)
1072 ustat->node.val.key = ustat->nb_err;
1073 else if (filter & FILT_COUNT_URL_TTOT)
1074 ustat->node.val.key = ustat->total_time;
1075 else if (filter & FILT_COUNT_URL_TAVG)
1076 ustat->node.val.key = ustat->nb_req ? ustat->total_time / ustat->nb_req : 0;
1077 else if (filter & FILT_COUNT_URL_TTOTO)
1078 ustat->node.val.key = ustat->total_time_ok;
1079 else if (filter & FILT_COUNT_URL_TAVGO)
1080 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 +02001081 else if (filter & FILT_COUNT_URL_BAVG)
1082 ustat->node.val.key = ustat->nb_req ? ustat->total_bytes_sent / ustat->nb_req : 0;
1083 else if (filter & FILT_COUNT_URL_BTOT)
1084 ustat->node.val.key = ustat->total_bytes_sent;
Willy Tarreauabe45b62010-10-28 20:33:46 +02001085 else
1086 ustat->node.val.key = 0;
1087
1088 eb64_insert(&timers[1], &ustat->node.val);
1089 }
1090 /* switch trees */
1091 timers[0] = timers[1];
1092 }
1093
Baptiste61aaad02012-09-08 23:10:03 +02001094 printf("#req err ttot tavg oktot okavg bavg btot url\n");
Willy Tarreauabe45b62010-10-28 20:33:46 +02001095
1096 /* scan the tree in its reverse sorting order */
1097 node = eb_last(&timers[0]);
1098 while (node) {
1099 ustat = container_of(node, struct url_stat, node.url.node);
Baptiste61aaad02012-09-08 23:10:03 +02001100 printf("%d %d %Ld %Ld %Ld %Ld %Ld %Ld %s\n",
Willy Tarreauabe45b62010-10-28 20:33:46 +02001101 ustat->nb_req,
1102 ustat->nb_err,
1103 ustat->total_time,
1104 ustat->nb_req ? ustat->total_time / ustat->nb_req : 0,
1105 ustat->total_time_ok,
1106 (ustat->nb_req - ustat->nb_err) ? ustat->total_time_ok / (ustat->nb_req - ustat->nb_err) : 0,
Baptiste61aaad02012-09-08 23:10:03 +02001107 ustat->nb_req ? ustat->total_bytes_sent / ustat->nb_req : 0,
1108 ustat->total_bytes_sent,
Willy Tarreauabe45b62010-10-28 20:33:46 +02001109 ustat->url);
1110
1111 node = eb_prev(node);
Willy Tarreaua2b39fb2011-07-10 21:39:35 +02001112 lines_out++;
Willy Tarreau667c9052012-10-10 16:49:28 +02001113 if (lines_out >= lines_max)
1114 break;
Willy Tarreauabe45b62010-10-28 20:33:46 +02001115 }
1116 }
Willy Tarreaud2201062010-05-27 18:17:30 +02001117
Willy Tarreau910ba4b2009-11-17 10:16:19 +01001118 empty:
Willy Tarreau72c28532009-01-22 18:56:50 +01001119 if (!(filter & FILT_QUIET))
1120 fprintf(stderr, "%d lines in, %d lines out, %d parsing errors\n",
Willy Tarreaua2b39fb2011-07-10 21:39:35 +02001121 linenum, lines_out, parse_err);
Willy Tarreau72c28532009-01-22 18:56:50 +01001122 exit(0);
1123}
1124
Willy Tarreaua2b39fb2011-07-10 21:39:35 +02001125void filter_output_line(const char *accept_field, const char *time_field, struct timer **tptr)
1126{
1127 puts(line);
1128 lines_out++;
1129}
1130
1131void filter_accept_holes(const char *accept_field, const char *time_field, struct timer **tptr)
1132{
1133 struct timer *t2;
1134 int val;
1135
1136 val = convert_date(accept_field);
1137 if (unlikely(val < 0)) {
1138 truncated_line(linenum, line);
1139 return;
1140 }
1141
1142 t2 = insert_value(&timers[0], tptr, val);
1143 t2->count++;
Willy Tarreaua2b39fb2011-07-10 21:39:35 +02001144 return;
1145}
1146
1147void filter_count_status(const char *accept_field, const char *time_field, struct timer **tptr)
1148{
1149 struct timer *t2;
1150 const char *b;
1151 int val;
1152
1153 if (time_field)
1154 b = field_start(time_field, STATUS_FIELD - TIME_FIELD + 1);
1155 else
1156 b = field_start(accept_field, STATUS_FIELD - ACCEPT_FIELD + 1);
1157
1158 if (unlikely(!*b)) {
1159 truncated_line(linenum, line);
1160 return;
1161 }
1162
1163 val = str2ic(b);
1164
1165 t2 = insert_value(&timers[0], tptr, val);
1166 t2->count++;
1167}
1168
Willy Tarreau8a09b662012-10-10 10:26:22 +02001169void filter_count_cook_codes(const char *accept_field, const char *time_field, struct timer **tptr)
1170{
1171 struct timer *t2;
1172 const char *b;
1173 int val;
1174
1175 if (time_field)
1176 b = field_start(time_field, TERM_CODES_FIELD - TIME_FIELD + 1);
1177 else
1178 b = field_start(accept_field, TERM_CODES_FIELD - ACCEPT_FIELD + 1);
1179
1180 if (unlikely(!*b)) {
1181 truncated_line(linenum, line);
1182 return;
1183 }
1184
1185 val = 256 * b[2] + b[3];
1186
1187 t2 = insert_value(&timers[0], tptr, val);
1188 t2->count++;
1189}
1190
Willy Tarreaua2b39fb2011-07-10 21:39:35 +02001191void filter_count_term_codes(const char *accept_field, const char *time_field, struct timer **tptr)
1192{
1193 struct timer *t2;
1194 const char *b;
1195 int val;
1196
1197 if (time_field)
1198 b = field_start(time_field, TERM_CODES_FIELD - TIME_FIELD + 1);
1199 else
1200 b = field_start(accept_field, TERM_CODES_FIELD - ACCEPT_FIELD + 1);
1201
1202 if (unlikely(!*b)) {
1203 truncated_line(linenum, line);
1204 return;
1205 }
1206
1207 val = 256 * b[0] + b[1];
1208
1209 t2 = insert_value(&timers[0], tptr, val);
1210 t2->count++;
1211}
1212
1213void filter_count_srv_status(const char *accept_field, const char *time_field, struct timer **tptr)
1214{
1215 const char *b, *e, *p;
1216 int f, err, array[5];
1217 struct ebmb_node *srv_node;
1218 struct srv_st *srv;
1219 int val;
1220
1221 /* the server field is before the status field, so let's
1222 * parse them in the proper order.
1223 */
1224 b = field_start(accept_field, SERVER_FIELD - ACCEPT_FIELD + 1);
1225 if (unlikely(!*b)) {
1226 truncated_line(linenum, line);
1227 return;
1228 }
1229
1230 e = field_stop(b + 1); /* we have the server name in [b]..[e-1] */
1231
1232 /* the chance that a server name already exists is extremely high,
1233 * so let's perform a normal lookup first.
1234 */
1235 srv_node = ebst_lookup_len(&timers[0], b, e - b);
1236 srv = container_of(srv_node, struct srv_st, node);
1237
1238 if (!srv_node) {
1239 /* server not yet in the tree, let's create it */
1240 srv = (void *)calloc(1, sizeof(struct srv_st) + e - b + 1);
1241 srv_node = &srv->node;
1242 memcpy(&srv_node->key, b, e - b);
1243 srv_node->key[e - b] = '\0';
1244 ebst_insert(&timers[0], srv_node);
1245 }
1246
1247 /* let's collect the connect and response times */
1248 if (!time_field) {
1249 time_field = field_start(e, TIME_FIELD - SERVER_FIELD);
1250 if (unlikely(!*time_field)) {
1251 truncated_line(linenum, line);
1252 return;
1253 }
1254 }
1255
1256 e = field_stop(time_field + 1);
1257 /* we have field TIME_FIELD in [time_field]..[e-1] */
1258
1259 p = time_field;
1260 err = 0;
1261 f = 0;
1262 while (!SEP(*p)) {
1263 array[f] = str2ic(p);
1264 if (array[f] < 0) {
1265 array[f] = -1;
1266 err = 1;
1267 }
1268 if (++f == 5)
1269 break;
1270 SKIP_CHAR(p, '/');
1271 }
1272
1273 if (unlikely(f < 5)){
1274 parse_err++;
1275 return;
1276 }
1277
1278 /* OK we have our timers in array[2,3] */
1279 if (!err)
1280 srv->nb_ok++;
1281
1282 if (array[2] >= 0) {
1283 srv->cum_ct += array[2];
1284 srv->nb_ct++;
1285 }
1286
1287 if (array[3] >= 0) {
1288 srv->cum_rt += array[3];
1289 srv->nb_rt++;
1290 }
1291
1292 /* we're interested in the 5 HTTP status classes (1xx ... 5xx), and
1293 * the invalid ones which will be reported as 0.
1294 */
1295 b = field_start(e, STATUS_FIELD - TIME_FIELD);
1296 if (unlikely(!*b)) {
1297 truncated_line(linenum, line);
1298 return;
1299 }
1300
1301 val = 0;
1302 if (*b >= '1' && *b <= '5')
1303 val = *b - '0';
1304
1305 srv->st_cnt[val]++;
1306}
1307
1308void filter_count_url(const char *accept_field, const char *time_field, struct timer **tptr)
1309{
1310 struct url_stat *ustat = NULL;
1311 struct ebpt_node *ebpt_old;
1312 const char *b, *e;
1313 int f, err, array[5];
Baptiste61aaad02012-09-08 23:10:03 +02001314 int val;
Willy Tarreaua2b39fb2011-07-10 21:39:35 +02001315
1316 /* let's collect the response time */
1317 if (!time_field) {
1318 time_field = field_start(accept_field, TIME_FIELD - ACCEPT_FIELD + 1); // avg 115 ns per line
1319 if (unlikely(!*time_field)) {
1320 truncated_line(linenum, line);
1321 return;
1322 }
1323 }
1324
1325 /* we have the field TIME_FIELD starting at <time_field>. We'll
1326 * parse the 5 timers to detect errors, it takes avg 55 ns per line.
1327 */
1328 e = time_field; err = 0; f = 0;
1329 while (!SEP(*e)) {
1330 array[f] = str2ic(e);
1331 if (array[f] < 0) {
1332 array[f] = -1;
1333 err = 1;
1334 }
1335 if (++f == 5)
1336 break;
1337 SKIP_CHAR(e, '/');
1338 }
1339 if (f < 5) {
1340 parse_err++;
1341 return;
1342 }
1343
1344 /* OK we have our timers in array[3], and err is >0 if at
1345 * least one -1 was seen. <e> points to the first char of
1346 * the last timer. Let's prepare a new node with that.
1347 */
1348 if (unlikely(!ustat))
1349 ustat = calloc(1, sizeof(*ustat));
1350
1351 ustat->nb_err = err;
1352 ustat->nb_req = 1;
1353
1354 /* use array[4] = total time in case of error */
1355 ustat->total_time = (array[3] >= 0) ? array[3] : array[4];
1356 ustat->total_time_ok = (array[3] >= 0) ? array[3] : 0;
1357
Baptiste61aaad02012-09-08 23:10:03 +02001358 e = field_start(e, BYTES_SENT_FIELD - TIME_FIELD + 1);
1359 val = str2ic(e);
1360 ustat->total_bytes_sent = val;
1361
Willy Tarreaua2b39fb2011-07-10 21:39:35 +02001362 /* the line may be truncated because of a bad request or anything like this,
1363 * without a method. Also, if it does not begin with an quote, let's skip to
1364 * the next field because it's a capture. Let's fall back to the "method" itself
1365 * if there's nothing else.
1366 */
Baptiste61aaad02012-09-08 23:10:03 +02001367 e = field_start(e, METH_FIELD - BYTES_SENT_FIELD + 1);
Willy Tarreau61a40c72011-09-06 08:11:27 +02001368 while (*e != '"' && *e) {
1369 /* Note: some syslog servers escape quotes ! */
1370 if (*e == '\\' && e[1] == '"')
1371 break;
Willy Tarreaua2b39fb2011-07-10 21:39:35 +02001372 e = field_start(e, 2);
Willy Tarreau61a40c72011-09-06 08:11:27 +02001373 }
Willy Tarreaua2b39fb2011-07-10 21:39:35 +02001374
1375 if (unlikely(!*e)) {
1376 truncated_line(linenum, line);
1377 return;
1378 }
1379
1380 b = field_start(e, URL_FIELD - METH_FIELD + 1); // avg 40 ns per line
1381 if (!*b)
1382 b = e;
1383
1384 /* stop at end of field or first ';' or '?', takes avg 64 ns per line */
1385 e = b;
1386 do {
Willy Tarreau14389e72011-07-10 22:11:17 +02001387 if (*e == ' ' || *e == '?' || *e == ';') {
Willy Tarreaua2b39fb2011-07-10 21:39:35 +02001388 *(char *)e = 0;
1389 break;
1390 }
1391 e++;
1392 } while (*e);
1393
1394 /* now instead of copying the URL for a simple lookup, we'll link
1395 * to it from the node we're trying to insert. If it returns a
1396 * different value, it was already there. Otherwise we just have
1397 * to dynamically realloc an entry using strdup().
1398 */
1399 ustat->node.url.key = (char *)b;
1400 ebpt_old = ebis_insert(&timers[0], &ustat->node.url);
1401
1402 if (ebpt_old != &ustat->node.url) {
1403 struct url_stat *ustat_old;
1404 /* node was already there, let's update previous one */
1405 ustat_old = container_of(ebpt_old, struct url_stat, node.url);
1406 ustat_old->nb_req ++;
1407 ustat_old->nb_err += ustat->nb_err;
1408 ustat_old->total_time += ustat->total_time;
1409 ustat_old->total_time_ok += ustat->total_time_ok;
Baptiste61aaad02012-09-08 23:10:03 +02001410 ustat_old->total_bytes_sent += ustat->total_bytes_sent;
Willy Tarreaua2b39fb2011-07-10 21:39:35 +02001411 } else {
1412 ustat->url = ustat->node.url.key = strdup(ustat->node.url.key);
1413 ustat = NULL; /* node was used */
1414 }
1415}
1416
1417void filter_graphs(const char *accept_field, const char *time_field, struct timer **tptr)
1418{
1419 struct timer *t2;
1420 const char *e, *p;
1421 int f, err, array[5];
1422
1423 if (!time_field) {
1424 time_field = field_start(accept_field, TIME_FIELD - ACCEPT_FIELD + 1);
1425 if (unlikely(!*time_field)) {
1426 truncated_line(linenum, line);
1427 return;
1428 }
1429 }
1430
1431 e = field_stop(time_field + 1);
1432 /* we have field TIME_FIELD in [time_field]..[e-1] */
1433
1434 p = time_field;
1435 err = 0;
1436 f = 0;
1437 while (!SEP(*p)) {
1438 array[f] = str2ic(p);
1439 if (array[f] < 0) {
1440 array[f] = -1;
1441 err = 1;
1442 }
1443 if (++f == 5)
1444 break;
1445 SKIP_CHAR(p, '/');
1446 }
1447
1448 if (unlikely(f < 5)) {
1449 parse_err++;
1450 return;
1451 }
1452
1453 /* if we find at least one negative time, we count one error
1454 * with a time equal to the total session time. This will
1455 * emphasize quantum timing effects associated to known
1456 * timeouts. Note that on some buggy machines, it is possible
1457 * that the total time is negative, hence the reason to reset
1458 * it.
1459 */
1460
1461 if (filter & FILT_GRAPH_TIMERS) {
1462 if (err) {
1463 if (array[4] < 0)
1464 array[4] = -1;
1465 t2 = insert_timer(&timers[0], tptr, array[4]); // total time
1466 t2->count++;
1467 } else {
1468 int v;
1469
1470 t2 = insert_timer(&timers[1], tptr, array[0]); t2->count++; // req
1471 t2 = insert_timer(&timers[2], tptr, array[2]); t2->count++; // conn
1472 t2 = insert_timer(&timers[3], tptr, array[3]); t2->count++; // resp
1473
1474 v = array[4] - array[0] - array[1] - array[2] - array[3]; // data time
1475 if (v < 0 && !(filter & FILT_QUIET))
1476 fprintf(stderr, "ERR: %s (%d %d %d %d %d => %d)\n",
1477 line, array[0], array[1], array[2], array[3], array[4], v);
1478 t2 = insert_timer(&timers[4], tptr, v); t2->count++;
1479 lines_out++;
1480 }
1481 } else { /* percentile */
1482 if (err) {
1483 if (array[4] < 0)
1484 array[4] = -1;
1485 t2 = insert_value(&timers[0], tptr, array[4]); // total time
1486 t2->count++;
1487 } else {
1488 int v;
1489
1490 t2 = insert_value(&timers[1], tptr, array[0]); t2->count++; // req
1491 t2 = insert_value(&timers[2], tptr, array[2]); t2->count++; // conn
1492 t2 = insert_value(&timers[3], tptr, array[3]); t2->count++; // resp
1493
1494 v = array[4] - array[0] - array[1] - array[2] - array[3]; // data time
1495 if (v < 0 && !(filter & FILT_QUIET))
1496 fprintf(stderr, "ERR: %s (%d %d %d %d %d => %d)\n",
1497 line, array[0], array[1], array[2], array[3], array[4], v);
1498 t2 = insert_value(&timers[4], tptr, v); t2->count++;
1499 lines_out++;
1500 }
1501 }
1502}
1503
1504
Willy Tarreau72c28532009-01-22 18:56:50 +01001505/*
1506 * Local variables:
1507 * c-indent-level: 8
1508 * c-basic-offset: 8
1509 * End:
1510 */