blob: 391280702fa1be849633a3eebb8f78b684c295cb [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 Tarreau72c28532009-01-22 18:56:50 +0100124
Willy Tarreau214c2032009-02-20 11:02:32 +0100125const char *fgets2(FILE *stream);
Willy Tarreau72c28532009-01-22 18:56:50 +0100126
Willy Tarreaua2b39fb2011-07-10 21:39:35 +0200127void filter_count_url(const char *accept_field, const char *time_field, struct timer **tptr);
128void filter_count_srv_status(const char *accept_field, const char *time_field, struct timer **tptr);
Willy Tarreau8a09b662012-10-10 10:26:22 +0200129void filter_count_cook_codes(const char *accept_field, const char *time_field, struct timer **tptr);
Willy Tarreaua2b39fb2011-07-10 21:39:35 +0200130void filter_count_term_codes(const char *accept_field, const char *time_field, struct timer **tptr);
131void filter_count_status(const char *accept_field, const char *time_field, struct timer **tptr);
132void filter_graphs(const char *accept_field, const char *time_field, struct timer **tptr);
133void filter_output_line(const char *accept_field, const char *time_field, struct timer **tptr);
134void filter_accept_holes(const char *accept_field, const char *time_field, struct timer **tptr);
135
Willy Tarreau615674c2012-01-23 08:15:51 +0100136void usage(FILE *output, const char *msg)
Willy Tarreau72c28532009-01-22 18:56:50 +0100137{
Willy Tarreau615674c2012-01-23 08:15:51 +0100138 fprintf(output,
Willy Tarreau72c28532009-01-22 18:56:50 +0100139 "%s"
Willy Tarreau615674c2012-01-23 08:15:51 +0100140 "Usage: halog [-h|--help] for long help\n"
Baptiste61aaad02012-09-08 23:10:03 +0200141 " halog [-q] [-c]\n"
Willy Tarreau8a09b662012-10-10 10:26:22 +0200142 " {-cc|-gt|-pct|-st|-tc|-srv|-u|-uc|-ue|-ua|-ut|-uao|-uto|-uba|-ubt}\n"
Hervé COMMOWICK927cddd2011-08-10 17:42:41 +0200143 " [-s <skip>] [-e|-E] [-H] [-rt|-RT <time>] [-ad <delay>] [-ac <count>]\n"
Baptiste61aaad02012-09-08 23:10:03 +0200144 " [-v] [-Q|-QS] [-tcn|-TCN <termcode>] [ -hs|-HS [min][:[max]] ] < log\n"
Willy Tarreau72c28532009-01-22 18:56:50 +0100145 "\n",
146 msg ? msg : ""
147 );
Willy Tarreau615674c2012-01-23 08:15:51 +0100148}
149
150void die(const char *msg)
151{
152 usage(stderr, msg);
Willy Tarreau72c28532009-01-22 18:56:50 +0100153 exit(1);
154}
155
Willy Tarreau615674c2012-01-23 08:15:51 +0100156void help()
157{
158 usage(stdout, NULL);
159 printf(
160 "Input filters (several filters may be combined) :\n"
161 " -H only match lines containing HTTP logs (ignore TCP)\n"
162 " -E only match lines without any error (no 5xx status)\n"
163 " -e only match lines with errors (status 5xx or negative)\n"
164 " -rt|-RT <time> only match response times larger|smaller than <time>\n"
165 " -Q|-QS only match queued requests (any queue|server queue)\n"
166 " -tcn|-TCN <code> only match requests with/without termination code <code>\n"
167 " -hs|-HS <[min][:][max]> only match requests with HTTP status codes within/not\n"
168 " within min..max. Any of them may be omitted. Exact\n"
169 " code is checked for if no ':' is specified.\n"
170 "Modifiers\n"
171 " -v invert the input filtering condition\n"
172 " -q don't report errors/warnings\n"
173 "\n"
174 "Output filters - only one may be used at a time\n"
175 " -c only report the number of lines that would have been printed\n"
176 " -pct output connect and response times percentiles\n"
177 " -st output number of requests per HTTP status code\n"
Willy Tarreau8a09b662012-10-10 10:26:22 +0200178 " -cc output number of requests per cookie code (2 chars)\n"
Willy Tarreau615674c2012-01-23 08:15:51 +0100179 " -tc output number of requests per termination code (2 chars)\n"
180 " -srv output statistics per server (time, requests, errors)\n"
181 " -u* output statistics per URL (time, requests, errors)\n"
182 " Additional characters indicate the output sorting key :\n"
183 " -u : by URL, -uc : request count, -ue : error count\n"
Willy Tarreau4201df72012-10-10 14:57:35 +0200184 " -ua : average response time, -ut : average total time\n"
Willy Tarreau615674c2012-01-23 08:15:51 +0100185 " -uao, -uto: average times computed on valid ('OK') requests\n"
Baptiste61aaad02012-09-08 23:10:03 +0200186 " -uba, -ubt: average bytes returned, total bytes returned\n"
Willy Tarreau615674c2012-01-23 08:15:51 +0100187 );
188 exit(0);
189}
190
Willy Tarreau72c28532009-01-22 18:56:50 +0100191
192/* return pointer to first char not part of current field starting at <p>. */
Willy Tarreauf9042062011-09-10 12:26:35 +0200193
194#if defined(__i386__)
195/* this one is always faster on 32-bits */
196static inline const char *field_stop(const char *p)
197{
198 asm(
199 /* Look for spaces */
200 "4: \n\t"
201 "inc %0 \n\t"
202 "cmpb $0x20, -1(%0) \n\t"
203 "ja 4b \n\t"
204 "jz 3f \n\t"
205
206 /* we only get there for control chars 0..31. Leave if we find '\0' */
207 "cmpb $0x0, -1(%0) \n\t"
208 "jnz 4b \n\t"
209
210 /* return %0-1 = position of the last char we checked */
211 "3: \n\t"
212 "dec %0 \n\t"
213 : "=r" (p)
214 : "0" (p)
215 );
216 return p;
217}
218#else
Willy Tarreau72c28532009-01-22 18:56:50 +0100219const char *field_stop(const char *p)
220{
221 unsigned char c;
222
223 while (1) {
224 c = *(p++);
225 if (c > ' ')
226 continue;
Willy Tarreau14389e72011-07-10 22:11:17 +0200227 if (c == ' ' || c == 0)
Willy Tarreau72c28532009-01-22 18:56:50 +0100228 break;
229 }
230 return p - 1;
231}
Willy Tarreauf9042062011-09-10 12:26:35 +0200232#endif
Willy Tarreau72c28532009-01-22 18:56:50 +0100233
234/* return field <field> (starting from 1) in string <p>. Only consider
235 * contiguous spaces (or tabs) as one delimiter. May return pointer to
236 * last char if field is not found. Equivalent to awk '{print $field}'.
237 */
238const char *field_start(const char *p, int field)
239{
Willy Tarreauf9042062011-09-10 12:26:35 +0200240#ifndef PREFER_ASM
Willy Tarreau72c28532009-01-22 18:56:50 +0100241 unsigned char c;
242 while (1) {
243 /* skip spaces */
244 while (1) {
Willy Tarreauf9042062011-09-10 12:26:35 +0200245 c = *(p++);
Willy Tarreau72c28532009-01-22 18:56:50 +0100246 if (c > ' ')
247 break;
Willy Tarreau14389e72011-07-10 22:11:17 +0200248 if (c == ' ')
Willy Tarreauf9042062011-09-10 12:26:35 +0200249 continue;
Willy Tarreau72c28532009-01-22 18:56:50 +0100250 if (!c) /* end of line */
Willy Tarreauf9042062011-09-10 12:26:35 +0200251 return p-1;
Willy Tarreau72c28532009-01-22 18:56:50 +0100252 /* other char => new field */
253 break;
Willy Tarreau72c28532009-01-22 18:56:50 +0100254 }
255
256 /* start of field */
257 field--;
258 if (!field)
Willy Tarreauf9042062011-09-10 12:26:35 +0200259 return p-1;
Willy Tarreau72c28532009-01-22 18:56:50 +0100260
261 /* skip this field */
262 while (1) {
263 c = *(p++);
Willy Tarreau14389e72011-07-10 22:11:17 +0200264 if (c == ' ')
Willy Tarreau72c28532009-01-22 18:56:50 +0100265 break;
Willy Tarreauf9042062011-09-10 12:26:35 +0200266 if (c > ' ')
267 continue;
Willy Tarreau72c28532009-01-22 18:56:50 +0100268 if (c == '\0')
Willy Tarreauf9042062011-09-10 12:26:35 +0200269 return p - 1;
Willy Tarreau72c28532009-01-22 18:56:50 +0100270 }
271 }
Willy Tarreauf9042062011-09-10 12:26:35 +0200272#else
273 /* This version works optimally on i386 and x86_64 but the code above
274 * shows similar performance. However, depending on the version of GCC
275 * used, inlining rules change and it may have difficulties to make
276 * efficient use of this code at other locations and could result in
277 * worse performance (eg: gcc 4.4). You may want to experience.
278 */
279 asm(
280 /* skip spaces */
281 "1: \n\t"
282 "inc %0 \n\t"
283 "cmpb $0x20, -1(%0) \n\t"
284 "ja 2f \n\t"
285 "jz 1b \n\t"
286
287 /* we only get there for control chars 0..31. Leave if we find '\0' */
288 "cmpb $0x0, -1(%0) \n\t"
289 "jz 3f \n\t"
290
291 /* start of field at [%0-1]. Check if we need to skip more fields */
292 "2: \n\t"
293 "dec %1 \n\t"
294 "jz 3f \n\t"
295
296 /* Look for spaces */
297 "4: \n\t"
298 "inc %0 \n\t"
299 "cmpb $0x20, -1(%0) \n\t"
300 "jz 1b \n\t"
301 "ja 4b \n\t"
302
303 /* we only get there for control chars 0..31. Leave if we find '\0' */
304 "cmpb $0x0, -1(%0) \n\t"
305 "jnz 4b \n\t"
306
307 /* return %0-1 = position of the last char we checked */
308 "3: \n\t"
309 "dec %0 \n\t"
310 : "=r" (p)
311 : "r" (field), "0" (p)
312 );
313 return p;
314#endif
Willy Tarreau72c28532009-01-22 18:56:50 +0100315}
316
317/* keep only the <bits> higher bits of <i> */
318static inline unsigned int quantify_u32(unsigned int i, int bits)
319{
320 int high;
321
322 if (!bits)
323 return 0;
324
325 if (i)
326 high = fls_auto(i); // 1 to 32
327 else
328 high = 0;
329
330 if (high <= bits)
331 return i;
332
333 return i & ~((1 << (high - bits)) - 1);
334}
335
336/* keep only the <bits> higher bits of the absolute value of <i>, as well as
337 * its sign. */
338static inline int quantify(int i, int bits)
339{
340 if (i >= 0)
341 return quantify_u32(i, bits);
342 else
343 return -quantify_u32(-i, bits);
344}
345
346/* Insert timer value <v> into tree <r>. A pre-allocated node must be passed
347 * in <alloc>. It may be NULL, in which case the function will allocate it
348 * itself. It will be reset to NULL once consumed. The caller is responsible
349 * for freeing the node once not used anymore. The node where the value was
350 * inserted is returned.
351 */
352struct timer *insert_timer(struct eb_root *r, struct timer **alloc, int v)
353{
354 struct timer *t = *alloc;
355 struct eb32_node *n;
356
357 if (!t) {
358 t = calloc(sizeof(*t), 1);
359 if (unlikely(!t)) {
360 fprintf(stderr, "%s: not enough memory\n", __FUNCTION__);
361 exit(1);
362 }
363 }
364 t->node.key = quantify(v, QBITS); // keep only the higher QBITS bits
365
366 n = eb32i_insert(r, &t->node);
367 if (n == &t->node)
368 t = NULL; /* node inserted, will malloc next time */
369
370 *alloc = t;
371 return container_of(n, struct timer, node);
372}
373
374/* Insert value value <v> into tree <r>. A pre-allocated node must be passed
375 * in <alloc>. It may be NULL, in which case the function will allocate it
376 * itself. It will be reset to NULL once consumed. The caller is responsible
377 * for freeing the node once not used anymore. The node where the value was
378 * inserted is returned.
379 */
380struct timer *insert_value(struct eb_root *r, struct timer **alloc, int v)
381{
382 struct timer *t = *alloc;
383 struct eb32_node *n;
384
385 if (!t) {
386 t = calloc(sizeof(*t), 1);
387 if (unlikely(!t)) {
388 fprintf(stderr, "%s: not enough memory\n", __FUNCTION__);
389 exit(1);
390 }
391 }
392 t->node.key = v;
393
394 n = eb32i_insert(r, &t->node);
395 if (n == &t->node)
396 t = NULL; /* node inserted, will malloc next time */
397
398 *alloc = t;
399 return container_of(n, struct timer, node);
400}
401
402int str2ic(const char *s)
403{
404 int i = 0;
405 int j, k;
406
407 if (*s != '-') {
408 /* positive number */
409 while (1) {
410 j = (*s++) - '0';
411 k = i * 10;
412 if ((unsigned)j > 9)
413 break;
414 i = k + j;
415 }
416 } else {
417 /* negative number */
418 s++;
419 while (1) {
420 j = (*s++) - '0';
421 k = i * 10;
422 if ((unsigned)j > 9)
423 break;
424 i = k - j;
425 }
426 }
427
428 return i;
429}
430
431
432/* Equivalent to strtoul with a length. */
433static inline unsigned int __strl2ui(const char *s, int len)
434{
435 unsigned int i = 0;
436 while (len-- > 0) {
437 i = i * 10 - '0';
438 i += (unsigned char)*s++;
439 }
440 return i;
441}
442
443unsigned int strl2ui(const char *s, int len)
444{
445 return __strl2ui(s, len);
446}
447
448/* Convert "[04/Dec/2008:09:49:40.555]" to an integer equivalent to the time of
449 * the day in milliseconds. It returns -1 for all unparsable values. The parser
450 * looks ugly but gcc emits far better code that way.
451 */
452int convert_date(const char *field)
453{
454 unsigned int h, m, s, ms;
455 unsigned char c;
456 const char *b, *e;
457
458 h = m = s = ms = 0;
459 e = field;
460
461 /* skip the date */
462 while (1) {
463 c = *(e++);
464 if (c == ':')
465 break;
466 if (!c)
467 goto out_err;
468 }
469
470 /* hour + ':' */
471 b = e;
472 while (1) {
473 c = *(e++) - '0';
474 if (c > 9)
475 break;
476 h = h * 10 + c;
477 }
478 if (c == (unsigned char)(0 - '0'))
479 goto out_err;
480
481 /* minute + ':' */
482 b = e;
483 while (1) {
484 c = *(e++) - '0';
485 if (c > 9)
486 break;
487 m = m * 10 + c;
488 }
489 if (c == (unsigned char)(0 - '0'))
490 goto out_err;
491
492 /* second + '.' or ']' */
493 b = e;
494 while (1) {
495 c = *(e++) - '0';
496 if (c > 9)
497 break;
498 s = s * 10 + c;
499 }
500 if (c == (unsigned char)(0 - '0'))
501 goto out_err;
502
503 /* if there's a '.', we have milliseconds */
504 if (c == (unsigned char)('.' - '0')) {
505 /* millisecond second + ']' */
506 b = e;
507 while (1) {
508 c = *(e++) - '0';
509 if (c > 9)
510 break;
511 ms = ms * 10 + c;
512 }
513 if (c == (unsigned char)(0 - '0'))
514 goto out_err;
515 }
516 return (((h * 60) + m) * 60 + s) * 1000 + ms;
517 out_err:
518 return -1;
519}
520
521void truncated_line(int linenum, const char *line)
522{
523 if (!(filter & FILT_QUIET))
524 fprintf(stderr, "Truncated line %d: %s\n", linenum, line);
525}
526
527int main(int argc, char **argv)
528{
Willy Tarreau26deaf52011-07-10 19:47:48 +0200529 const char *b, *e, *p, *time_field, *accept_field;
Hervé COMMOWICK927cddd2011-08-10 17:42:41 +0200530 const char *filter_term_code_name = NULL;
Willy Tarreau72c28532009-01-22 18:56:50 +0100531 const char *output_file = NULL;
Willy Tarreaua2b39fb2011-07-10 21:39:35 +0200532 int f, last, err;
533 struct timer *t = NULL;
Willy Tarreau72c28532009-01-22 18:56:50 +0100534 struct eb32_node *n;
Willy Tarreauabe45b62010-10-28 20:33:46 +0200535 struct url_stat *ustat = NULL;
Willy Tarreau72c28532009-01-22 18:56:50 +0100536 int val, test;
Willy Tarreau72c28532009-01-22 18:56:50 +0100537 int filter_acc_delay = 0, filter_acc_count = 0;
Willy Tarreau5bdfd962009-10-14 15:16:29 +0200538 int filter_time_resp = 0;
Willy Tarreaud3007ff2011-09-05 02:07:23 +0200539 int filt_http_status_low = 0, filt_http_status_high = 0;
Willy Tarreau72c28532009-01-22 18:56:50 +0100540 int skip_fields = 1;
541
Willy Tarreaua2b39fb2011-07-10 21:39:35 +0200542 void (*line_filter)(const char *accept_field, const char *time_field, struct timer **tptr) = NULL;
543
Willy Tarreau72c28532009-01-22 18:56:50 +0100544 argc--; argv++;
545 while (argc > 0) {
546 if (*argv[0] != '-')
547 break;
548
549 if (strcmp(argv[0], "-ad") == 0) {
550 if (argc < 2) die("missing option for -ad");
551 argc--; argv++;
552 filter |= FILT_ACC_DELAY;
553 filter_acc_delay = atol(*argv);
554 }
555 else if (strcmp(argv[0], "-ac") == 0) {
556 if (argc < 2) die("missing option for -ac");
557 argc--; argv++;
558 filter |= FILT_ACC_COUNT;
559 filter_acc_count = atol(*argv);
560 }
Willy Tarreau5bdfd962009-10-14 15:16:29 +0200561 else if (strcmp(argv[0], "-rt") == 0) {
562 if (argc < 2) die("missing option for -rt");
563 argc--; argv++;
564 filter |= FILT_TIME_RESP;
565 filter_time_resp = atol(*argv);
566 }
567 else if (strcmp(argv[0], "-RT") == 0) {
568 if (argc < 2) die("missing option for -RT");
569 argc--; argv++;
570 filter |= FILT_TIME_RESP | FILT_INVERT_TIME_RESP;
571 filter_time_resp = atol(*argv);
572 }
Willy Tarreau72c28532009-01-22 18:56:50 +0100573 else if (strcmp(argv[0], "-s") == 0) {
574 if (argc < 2) die("missing option for -s");
575 argc--; argv++;
576 skip_fields = atol(*argv);
577 }
578 else if (strcmp(argv[0], "-e") == 0)
579 filter |= FILT_ERRORS_ONLY;
Willy Tarreau5bdfd962009-10-14 15:16:29 +0200580 else if (strcmp(argv[0], "-E") == 0)
581 filter |= FILT_ERRORS_ONLY | FILT_INVERT_ERRORS;
Willy Tarreau70c428f2011-07-10 17:27:40 +0200582 else if (strcmp(argv[0], "-H") == 0)
583 filter |= FILT_HTTP_ONLY;
Willy Tarreau08911ff2011-10-13 13:28:36 +0200584 else if (strcmp(argv[0], "-Q") == 0)
585 filter |= FILT_QUEUE_ONLY;
586 else if (strcmp(argv[0], "-QS") == 0)
587 filter |= FILT_QUEUE_SRV_ONLY;
Willy Tarreau72c28532009-01-22 18:56:50 +0100588 else if (strcmp(argv[0], "-c") == 0)
589 filter |= FILT_COUNT_ONLY;
590 else if (strcmp(argv[0], "-q") == 0)
591 filter |= FILT_QUIET;
592 else if (strcmp(argv[0], "-v") == 0)
593 filter_invert = !filter_invert;
594 else if (strcmp(argv[0], "-gt") == 0)
595 filter |= FILT_GRAPH_TIMERS;
Willy Tarreau214c2032009-02-20 11:02:32 +0100596 else if (strcmp(argv[0], "-pct") == 0)
597 filter |= FILT_PERCENTILE;
Willy Tarreau0f423a72010-05-03 10:50:54 +0200598 else if (strcmp(argv[0], "-st") == 0)
599 filter |= FILT_COUNT_STATUS;
Willy Tarreaud2201062010-05-27 18:17:30 +0200600 else if (strcmp(argv[0], "-srv") == 0)
601 filter |= FILT_COUNT_SRV_STATUS;
Willy Tarreau8a09b662012-10-10 10:26:22 +0200602 else if (strcmp(argv[0], "-cc") == 0)
603 filter |= FILT_COUNT_COOK_CODES;
Willy Tarreaud8fc1102010-09-12 17:56:16 +0200604 else if (strcmp(argv[0], "-tc") == 0)
605 filter |= FILT_COUNT_TERM_CODES;
Hervé COMMOWICK927cddd2011-08-10 17:42:41 +0200606 else if (strcmp(argv[0], "-tcn") == 0) {
607 if (argc < 2) die("missing option for -tcn");
608 argc--; argv++;
609 filter |= FILT_TERM_CODE_NAME;
610 filter_term_code_name = *argv;
611 }
612 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 | FILT_INVERT_TERM_CODE_NAME;
616 filter_term_code_name = *argv;
617 }
Willy Tarreaud3007ff2011-09-05 02:07:23 +0200618 else if (strcmp(argv[0], "-hs") == 0 || strcmp(argv[0], "-HS") == 0) {
619 char *sep, *str;
620
621 if (argc < 2) die("missing option for -hs/-HS ([min]:[max])");
622 filter |= FILT_HTTP_STATUS;
623 if (argv[0][1] == 'H')
624 filter |= FILT_INVERT_HTTP_STATUS;
625
626 argc--; argv++;
627 str = *argv;
628 sep = strchr(str, ':'); /* [min]:[max] */
629 if (!sep)
630 sep = str; /* make max point to min */
631 else
632 *sep++ = 0;
633 filt_http_status_low = *str ? atol(str) : 0;
634 filt_http_status_high = *sep ? atol(sep) : 65535;
635 }
Willy Tarreauabe45b62010-10-28 20:33:46 +0200636 else if (strcmp(argv[0], "-u") == 0)
637 filter |= FILT_COUNT_URL_ONLY;
638 else if (strcmp(argv[0], "-uc") == 0)
639 filter |= FILT_COUNT_URL_COUNT;
640 else if (strcmp(argv[0], "-ue") == 0)
641 filter |= FILT_COUNT_URL_ERR;
642 else if (strcmp(argv[0], "-ua") == 0)
643 filter |= FILT_COUNT_URL_TAVG;
644 else if (strcmp(argv[0], "-ut") == 0)
645 filter |= FILT_COUNT_URL_TTOT;
646 else if (strcmp(argv[0], "-uao") == 0)
647 filter |= FILT_COUNT_URL_TAVGO;
648 else if (strcmp(argv[0], "-uto") == 0)
649 filter |= FILT_COUNT_URL_TTOTO;
Baptiste61aaad02012-09-08 23:10:03 +0200650 else if (strcmp(argv[0], "-uba") == 0)
651 filter |= FILT_COUNT_URL_BAVG;
652 else if (strcmp(argv[0], "-ubt") == 0)
653 filter |= FILT_COUNT_URL_BTOT;
Willy Tarreau72c28532009-01-22 18:56:50 +0100654 else if (strcmp(argv[0], "-o") == 0) {
655 if (output_file)
656 die("Fatal: output file name already specified.\n");
657 if (argc < 2)
658 die("Fatal: missing output file name.\n");
659 output_file = argv[1];
660 }
Willy Tarreau615674c2012-01-23 08:15:51 +0100661 else if (strcmp(argv[0], "-h") == 0 || strcmp(argv[0], "--help") == 0)
662 help();
Willy Tarreau72c28532009-01-22 18:56:50 +0100663 argc--;
664 argv++;
665 }
666
667 if (!filter)
668 die("No action specified.\n");
669
670 if (filter & FILT_ACC_COUNT && !filter_acc_count)
671 filter_acc_count=1;
672
673 if (filter & FILT_ACC_DELAY && !filter_acc_delay)
674 filter_acc_delay = 1;
675
Willy Tarreaua2b39fb2011-07-10 21:39:35 +0200676
677 /* by default, all lines are printed */
678 line_filter = filter_output_line;
679 if (filter & (FILT_ACC_COUNT|FILT_ACC_DELAY))
680 line_filter = filter_accept_holes;
681 else if (filter & (FILT_GRAPH_TIMERS|FILT_PERCENTILE))
682 line_filter = filter_graphs;
683 else if (filter & FILT_COUNT_STATUS)
684 line_filter = filter_count_status;
Willy Tarreau8a09b662012-10-10 10:26:22 +0200685 else if (filter & FILT_COUNT_COOK_CODES)
686 line_filter = filter_count_cook_codes;
Willy Tarreaua2b39fb2011-07-10 21:39:35 +0200687 else if (filter & FILT_COUNT_TERM_CODES)
688 line_filter = filter_count_term_codes;
689 else if (filter & FILT_COUNT_SRV_STATUS)
690 line_filter = filter_count_srv_status;
691 else if (filter & FILT_COUNT_URL_ANY)
692 line_filter = filter_count_url;
693 else if (filter & FILT_COUNT_ONLY)
694 line_filter = NULL;
Willy Tarreau72c28532009-01-22 18:56:50 +0100695
Willy Tarreauf8c95d22012-06-12 09:16:56 +0200696#if defined(POSIX_FADV_SEQUENTIAL)
697 /* around 20% performance improvement is observed on Linux with this
698 * on cold-cache. Surprizingly, WILLNEED is less performant. Don't
699 * use NOREUSE as it flushes the cache and prevents easy data
700 * manipulation on logs!
701 */
702 posix_fadvise(0, 0, 0, POSIX_FADV_SEQUENTIAL);
703#endif
704
Willy Tarreaue1a908c2012-01-03 09:23:03 +0100705 if (!line_filter &&
706 !(filter & (FILT_HTTP_ONLY|FILT_TIME_RESP|FILT_ERRORS_ONLY|FILT_HTTP_STATUS|FILT_QUEUE_ONLY|FILT_QUEUE_SRV_ONLY|FILT_TERM_CODE_NAME))) {
707 /* read the whole file at once first */
708 if (!filter_invert)
709 while (fgets2(stdin) != NULL)
710 lines_out++;
711
712 goto skip_filters;
713 }
714
Willy Tarreau214c2032009-02-20 11:02:32 +0100715 while ((line = fgets2(stdin)) != NULL) {
Willy Tarreau72c28532009-01-22 18:56:50 +0100716 linenum++;
Willy Tarreau26deaf52011-07-10 19:47:48 +0200717 time_field = NULL; accept_field = NULL;
Willy Tarreau72c28532009-01-22 18:56:50 +0100718
Willy Tarreau5bdfd962009-10-14 15:16:29 +0200719 test = 1;
Willy Tarreau26deaf52011-07-10 19:47:48 +0200720
721 /* for any line we process, we first ensure that there is a field
722 * looking like the accept date field (beginning with a '[').
723 */
724 accept_field = field_start(line, ACCEPT_FIELD + skip_fields);
Willy Tarreaua2b39fb2011-07-10 21:39:35 +0200725 if (unlikely(*accept_field != '[')) {
Willy Tarreau26deaf52011-07-10 19:47:48 +0200726 parse_err++;
727 continue;
728 }
729
730 /* the day of month field is begin 01 and 31 */
Willy Tarreaua2b39fb2011-07-10 21:39:35 +0200731 if (accept_field[1] < '0' || accept_field[1] > '3') {
Willy Tarreau26deaf52011-07-10 19:47:48 +0200732 parse_err++;
733 continue;
734 }
735
Willy Tarreaua2b39fb2011-07-10 21:39:35 +0200736 if (filter & FILT_HTTP_ONLY) {
Willy Tarreau70c428f2011-07-10 17:27:40 +0200737 /* only report lines with at least 4 timers */
Willy Tarreaua2b39fb2011-07-10 21:39:35 +0200738 if (!time_field) {
Willy Tarreau26deaf52011-07-10 19:47:48 +0200739 time_field = field_start(accept_field, TIME_FIELD - ACCEPT_FIELD + 1);
Willy Tarreaua2b39fb2011-07-10 21:39:35 +0200740 if (unlikely(!*time_field)) {
Willy Tarreau26deaf52011-07-10 19:47:48 +0200741 truncated_line(linenum, line);
742 continue;
743 }
Willy Tarreau70c428f2011-07-10 17:27:40 +0200744 }
745
Willy Tarreau758a6ea2011-07-10 18:53:44 +0200746 e = field_stop(time_field + 1);
747 /* we have field TIME_FIELD in [time_field]..[e-1] */
748 p = time_field;
Willy Tarreau70c428f2011-07-10 17:27:40 +0200749 f = 0;
Willy Tarreaudf6f0d12011-07-10 18:15:08 +0200750 while (!SEP(*p)) {
Willy Tarreau70c428f2011-07-10 17:27:40 +0200751 if (++f == 4)
752 break;
753 SKIP_CHAR(p, '/');
754 }
755 test &= (f >= 4);
756 }
757
Willy Tarreaua2b39fb2011-07-10 21:39:35 +0200758 if (filter & FILT_TIME_RESP) {
Willy Tarreau5bdfd962009-10-14 15:16:29 +0200759 int tps;
760
761 /* only report lines with response times larger than filter_time_resp */
Willy Tarreaua2b39fb2011-07-10 21:39:35 +0200762 if (!time_field) {
Willy Tarreau26deaf52011-07-10 19:47:48 +0200763 time_field = field_start(accept_field, TIME_FIELD - ACCEPT_FIELD + 1);
Willy Tarreaua2b39fb2011-07-10 21:39:35 +0200764 if (unlikely(!*time_field)) {
Willy Tarreau26deaf52011-07-10 19:47:48 +0200765 truncated_line(linenum, line);
766 continue;
767 }
Willy Tarreau5bdfd962009-10-14 15:16:29 +0200768 }
769
Willy Tarreau758a6ea2011-07-10 18:53:44 +0200770 e = field_stop(time_field + 1);
771 /* we have field TIME_FIELD in [time_field]..[e-1], let's check only the response time */
Willy Tarreau5bdfd962009-10-14 15:16:29 +0200772
Willy Tarreau758a6ea2011-07-10 18:53:44 +0200773 p = time_field;
Willy Tarreau5bdfd962009-10-14 15:16:29 +0200774 err = 0;
Willy Tarreau24bcb4f2010-10-28 20:39:50 +0200775 f = 0;
Willy Tarreaudf6f0d12011-07-10 18:15:08 +0200776 while (!SEP(*p)) {
Willy Tarreau5bdfd962009-10-14 15:16:29 +0200777 tps = str2ic(p);
778 if (tps < 0) {
779 tps = -1;
780 err = 1;
781 }
Willy Tarreau24bcb4f2010-10-28 20:39:50 +0200782 if (++f == 4)
783 break;
Willy Tarreau5bdfd962009-10-14 15:16:29 +0200784 SKIP_CHAR(p, '/');
785 }
786
Willy Tarreaua2b39fb2011-07-10 21:39:35 +0200787 if (unlikely(f < 4)) {
Willy Tarreau5bdfd962009-10-14 15:16:29 +0200788 parse_err++;
789 continue;
790 }
791
792 test &= (tps >= filter_time_resp) ^ !!(filter & FILT_INVERT_TIME_RESP);
793 }
794
Willy Tarreaud3007ff2011-09-05 02:07:23 +0200795 if (filter & (FILT_ERRORS_ONLY | FILT_HTTP_STATUS)) {
796 /* Check both error codes (-1, 5xx) and status code ranges */
Willy Tarreau26deaf52011-07-10 19:47:48 +0200797 if (time_field)
798 b = field_start(time_field, STATUS_FIELD - TIME_FIELD + 1);
799 else
800 b = field_start(accept_field, STATUS_FIELD - ACCEPT_FIELD + 1);
801
Willy Tarreaua2b39fb2011-07-10 21:39:35 +0200802 if (unlikely(!*b)) {
Willy Tarreau72c28532009-01-22 18:56:50 +0100803 truncated_line(linenum, line);
804 continue;
805 }
Willy Tarreaua2b39fb2011-07-10 21:39:35 +0200806
Willy Tarreaud3007ff2011-09-05 02:07:23 +0200807 val = str2ic(b);
808 if (filter & FILT_ERRORS_ONLY)
809 test &= (val < 0 || (val >= 500 && val <= 599)) ^ !!(filter & FILT_INVERT_ERRORS);
810
811 if (filter & FILT_HTTP_STATUS)
812 test &= (val >= filt_http_status_low && val <= filt_http_status_high) ^ !!(filter & FILT_INVERT_HTTP_STATUS);
Willy Tarreau72c28532009-01-22 18:56:50 +0100813 }
814
Willy Tarreau08911ff2011-10-13 13:28:36 +0200815 if (filter & (FILT_QUEUE_ONLY|FILT_QUEUE_SRV_ONLY)) {
816 /* Check if the server's queue is non-nul */
817 if (time_field)
818 b = field_start(time_field, QUEUE_LEN_FIELD - TIME_FIELD + 1);
819 else
820 b = field_start(accept_field, QUEUE_LEN_FIELD - ACCEPT_FIELD + 1);
821
822 if (unlikely(!*b)) {
823 truncated_line(linenum, line);
824 continue;
825 }
826
827 if (*b == '0') {
828 if (filter & FILT_QUEUE_SRV_ONLY) {
829 test = 0;
830 }
831 else {
832 do {
833 b++;
834 if (*b == '/') {
835 b++;
836 break;
837 }
838 } while (*b);
839 test &= ((unsigned char)(*b - '1') < 9);
840 }
841 }
842 }
843
Hervé COMMOWICK927cddd2011-08-10 17:42:41 +0200844 if (filter & FILT_TERM_CODE_NAME) {
845 /* only report corresponding termination code name */
846 if (time_field)
847 b = field_start(time_field, TERM_CODES_FIELD - TIME_FIELD + 1);
848 else
849 b = field_start(accept_field, TERM_CODES_FIELD - ACCEPT_FIELD + 1);
850
851 if (unlikely(!*b)) {
852 truncated_line(linenum, line);
853 continue;
854 }
855
856 test &= (b[0] == filter_term_code_name[0] && b[1] == filter_term_code_name[1]) ^ !!(filter & FILT_INVERT_TERM_CODE_NAME);
857 }
858
859
Willy Tarreau0f423a72010-05-03 10:50:54 +0200860 test ^= filter_invert;
861 if (!test)
862 continue;
863
Willy Tarreaua2b39fb2011-07-10 21:39:35 +0200864 /************** here we process inputs *******************/
Willy Tarreau72c28532009-01-22 18:56:50 +0100865
Willy Tarreaua2b39fb2011-07-10 21:39:35 +0200866 if (line_filter)
867 line_filter(accept_field, time_field, &t);
868 else
869 lines_out++; /* we're just counting lines */
870 }
Willy Tarreauabe45b62010-10-28 20:33:46 +0200871
Willy Tarreaue1a908c2012-01-03 09:23:03 +0100872 skip_filters:
Willy Tarreaua2b39fb2011-07-10 21:39:35 +0200873 /*****************************************************
874 * Here we've finished reading all input. Depending on the
875 * filters, we may still have some analysis to run on the
876 * collected data and to output data in a new format.
877 *************************************************** */
Willy Tarreau72c28532009-01-22 18:56:50 +0100878
879 if (t)
880 free(t);
881
882 if (filter & FILT_COUNT_ONLY) {
Willy Tarreaua2b39fb2011-07-10 21:39:35 +0200883 printf("%d\n", lines_out);
Willy Tarreau72c28532009-01-22 18:56:50 +0100884 exit(0);
885 }
886
Willy Tarreau72c28532009-01-22 18:56:50 +0100887 if (filter & (FILT_ACC_COUNT|FILT_ACC_DELAY)) {
888 /* sort and count all timers. Output will look like this :
889 * <accept_date> <delta_ms from previous one> <nb entries>
890 */
891 n = eb32_first(&timers[0]);
892
893 if (n)
894 last = n->key;
895 while (n) {
896 unsigned int d, h, m, s, ms;
897
898 t = container_of(n, struct timer, node);
899 h = n->key;
900 d = h - last;
901 last = h;
902
903 if (d >= filter_acc_delay && t->count >= filter_acc_count) {
904 ms = h % 1000; h = h / 1000;
905 s = h % 60; h = h / 60;
906 m = h % 60; h = h / 60;
Willy Tarreaua2b39fb2011-07-10 21:39:35 +0200907 lines_out++;
Willy Tarreau72c28532009-01-22 18:56:50 +0100908 printf("%02d:%02d:%02d.%03d %d %d %d\n", h, m, s, ms, last, d, t->count);
909 }
910 n = eb32_next(n);
911 }
912 }
913 else if (filter & FILT_GRAPH_TIMERS) {
914 /* sort all timers */
915 for (f = 0; f < 5; f++) {
916 struct eb32_node *n;
917 int val;
918
919 val = 0;
920 n = eb32_first(&timers[f]);
921 while (n) {
922 int i;
923 double d;
924
925 t = container_of(n, struct timer, node);
926 last = n->key;
927 val = t->count;
928
929 i = (last < 0) ? -last : last;
930 i = fls_auto(i) - QBITS;
931
932 if (i > 0)
933 d = val / (double)(1 << i);
934 else
935 d = val;
936
937 if (d > 0.0) {
938 printf("%d %d %f\n", f, last, d+1.0);
Willy Tarreaua2b39fb2011-07-10 21:39:35 +0200939 lines_out++;
Willy Tarreau72c28532009-01-22 18:56:50 +0100940 }
941
942 n = eb32_next(n);
943 }
Willy Tarreau214c2032009-02-20 11:02:32 +0100944 }
945 }
946 else if (filter & FILT_PERCENTILE) {
947 /* report timers by percentile :
948 * <percent> <total> <max_req_time> <max_conn_time> <max_resp_time> <max_data_time>
949 * We don't count errs.
950 */
951 struct eb32_node *n[5];
952 unsigned long cum[5];
953 double step;
954
Willy Tarreaua2b39fb2011-07-10 21:39:35 +0200955 if (!lines_out)
Willy Tarreau910ba4b2009-11-17 10:16:19 +0100956 goto empty;
957
Willy Tarreau214c2032009-02-20 11:02:32 +0100958 for (f = 1; f < 5; f++) {
959 n[f] = eb32_first(&timers[f]);
960 cum[f] = container_of(n[f], struct timer, node)->count;
961 }
962
963 for (step = 1; step <= 1000;) {
Willy Tarreaua2b39fb2011-07-10 21:39:35 +0200964 unsigned int thres = lines_out * (step / 1000.0);
Willy Tarreau214c2032009-02-20 11:02:32 +0100965
966 printf("%3.1f %d ", step/10.0, thres);
967 for (f = 1; f < 5; f++) {
968 struct eb32_node *next;
969 while (cum[f] < thres) {
970 /* need to find other keys */
971 next = eb32_next(n[f]);
972 if (!next)
973 break;
974 n[f] = next;
975 cum[f] += container_of(next, struct timer, node)->count;
976 }
977
978 /* value still within $step % of total */
979 printf("%d ", n[f]->key);
980 }
981 putchar('\n');
982 if (step >= 100 && step < 900)
983 step += 50; // jump 5% by 5% between those steps.
984 else if (step >= 20 && step < 980)
985 step += 10;
986 else
987 step += 1;
Willy Tarreau72c28532009-01-22 18:56:50 +0100988 }
989 }
Willy Tarreau0f423a72010-05-03 10:50:54 +0200990 else if (filter & FILT_COUNT_STATUS) {
991 /* output all statuses in the form of <status> <occurrences> */
992 n = eb32_first(&timers[0]);
993 while (n) {
994 t = container_of(n, struct timer, node);
995 printf("%d %d\n", n->key, t->count);
Willy Tarreaua2b39fb2011-07-10 21:39:35 +0200996 lines_out++;
Willy Tarreau0f423a72010-05-03 10:50:54 +0200997 n = eb32_next(n);
998 }
999 }
Willy Tarreaua2b39fb2011-07-10 21:39:35 +02001000 else if (filter & FILT_COUNT_SRV_STATUS) {
Willy Tarreaud2201062010-05-27 18:17:30 +02001001 struct ebmb_node *srv_node;
1002 struct srv_st *srv;
1003
1004 printf("#srv_name 1xx 2xx 3xx 4xx 5xx other tot_req req_ok pct_ok avg_ct avg_rt\n");
1005
1006 srv_node = ebmb_first(&timers[0]);
1007 while (srv_node) {
1008 int tot_rq;
1009
1010 srv = container_of(srv_node, struct srv_st, node);
1011
1012 tot_rq = 0;
1013 for (f = 0; f <= 5; f++)
1014 tot_rq += srv->st_cnt[f];
1015
1016 printf("%s %d %d %d %d %d %d %d %d %.1f %d %d\n",
1017 srv_node->key, srv->st_cnt[1], srv->st_cnt[2],
1018 srv->st_cnt[3], srv->st_cnt[4], srv->st_cnt[5], srv->st_cnt[0],
1019 tot_rq,
1020 srv->nb_ok, (double)srv->nb_ok * 100.0 / (tot_rq?tot_rq:1),
1021 (int)(srv->cum_ct / (srv->nb_ct?srv->nb_ct:1)), (int)(srv->cum_rt / (srv->nb_rt?srv->nb_rt:1)));
1022 srv_node = ebmb_next(srv_node);
Willy Tarreaua2b39fb2011-07-10 21:39:35 +02001023 lines_out++;
Willy Tarreaud2201062010-05-27 18:17:30 +02001024 }
1025 }
Willy Tarreau8a09b662012-10-10 10:26:22 +02001026 else if (filter & (FILT_COUNT_TERM_CODES|FILT_COUNT_COOK_CODES)) {
Willy Tarreaud8fc1102010-09-12 17:56:16 +02001027 /* output all statuses in the form of <code> <occurrences> */
1028 n = eb32_first(&timers[0]);
1029 while (n) {
1030 t = container_of(n, struct timer, node);
1031 printf("%c%c %d\n", (n->key >> 8), (n->key) & 255, t->count);
Willy Tarreaua2b39fb2011-07-10 21:39:35 +02001032 lines_out++;
Willy Tarreaud8fc1102010-09-12 17:56:16 +02001033 n = eb32_next(n);
1034 }
1035 }
Willy Tarreaua2b39fb2011-07-10 21:39:35 +02001036 else if (filter & FILT_COUNT_URL_ANY) {
Willy Tarreauabe45b62010-10-28 20:33:46 +02001037 struct eb_node *node, *next;
1038
1039 if (!(filter & FILT_COUNT_URL_ONLY)) {
1040 /* we have to sort on another criterion. We'll use timers[1] for the
1041 * destination tree.
1042 */
1043
1044 timers[1] = EB_ROOT; /* reconfigure to accept duplicates */
1045 for (node = eb_first(&timers[0]); node; node = next) {
1046 next = eb_next(node);
1047 eb_delete(node);
1048
1049 ustat = container_of(node, struct url_stat, node.url.node);
1050
1051 if (filter & FILT_COUNT_URL_COUNT)
1052 ustat->node.val.key = ustat->nb_req;
1053 else if (filter & FILT_COUNT_URL_ERR)
1054 ustat->node.val.key = ustat->nb_err;
1055 else if (filter & FILT_COUNT_URL_TTOT)
1056 ustat->node.val.key = ustat->total_time;
1057 else if (filter & FILT_COUNT_URL_TAVG)
1058 ustat->node.val.key = ustat->nb_req ? ustat->total_time / ustat->nb_req : 0;
1059 else if (filter & FILT_COUNT_URL_TTOTO)
1060 ustat->node.val.key = ustat->total_time_ok;
1061 else if (filter & FILT_COUNT_URL_TAVGO)
1062 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 +02001063 else if (filter & FILT_COUNT_URL_BAVG)
1064 ustat->node.val.key = ustat->nb_req ? ustat->total_bytes_sent / ustat->nb_req : 0;
1065 else if (filter & FILT_COUNT_URL_BTOT)
1066 ustat->node.val.key = ustat->total_bytes_sent;
Willy Tarreauabe45b62010-10-28 20:33:46 +02001067 else
1068 ustat->node.val.key = 0;
1069
1070 eb64_insert(&timers[1], &ustat->node.val);
1071 }
1072 /* switch trees */
1073 timers[0] = timers[1];
1074 }
1075
Baptiste61aaad02012-09-08 23:10:03 +02001076 printf("#req err ttot tavg oktot okavg bavg btot url\n");
Willy Tarreauabe45b62010-10-28 20:33:46 +02001077
1078 /* scan the tree in its reverse sorting order */
1079 node = eb_last(&timers[0]);
1080 while (node) {
1081 ustat = container_of(node, struct url_stat, node.url.node);
Baptiste61aaad02012-09-08 23:10:03 +02001082 printf("%d %d %Ld %Ld %Ld %Ld %Ld %Ld %s\n",
Willy Tarreauabe45b62010-10-28 20:33:46 +02001083 ustat->nb_req,
1084 ustat->nb_err,
1085 ustat->total_time,
1086 ustat->nb_req ? ustat->total_time / ustat->nb_req : 0,
1087 ustat->total_time_ok,
1088 (ustat->nb_req - ustat->nb_err) ? ustat->total_time_ok / (ustat->nb_req - ustat->nb_err) : 0,
Baptiste61aaad02012-09-08 23:10:03 +02001089 ustat->nb_req ? ustat->total_bytes_sent / ustat->nb_req : 0,
1090 ustat->total_bytes_sent,
Willy Tarreauabe45b62010-10-28 20:33:46 +02001091 ustat->url);
1092
1093 node = eb_prev(node);
Willy Tarreaua2b39fb2011-07-10 21:39:35 +02001094 lines_out++;
Willy Tarreauabe45b62010-10-28 20:33:46 +02001095 }
1096 }
Willy Tarreaud2201062010-05-27 18:17:30 +02001097
Willy Tarreau910ba4b2009-11-17 10:16:19 +01001098 empty:
Willy Tarreau72c28532009-01-22 18:56:50 +01001099 if (!(filter & FILT_QUIET))
1100 fprintf(stderr, "%d lines in, %d lines out, %d parsing errors\n",
Willy Tarreaua2b39fb2011-07-10 21:39:35 +02001101 linenum, lines_out, parse_err);
Willy Tarreau72c28532009-01-22 18:56:50 +01001102 exit(0);
1103}
1104
Willy Tarreaua2b39fb2011-07-10 21:39:35 +02001105void filter_output_line(const char *accept_field, const char *time_field, struct timer **tptr)
1106{
1107 puts(line);
1108 lines_out++;
1109}
1110
1111void filter_accept_holes(const char *accept_field, const char *time_field, struct timer **tptr)
1112{
1113 struct timer *t2;
1114 int val;
1115
1116 val = convert_date(accept_field);
1117 if (unlikely(val < 0)) {
1118 truncated_line(linenum, line);
1119 return;
1120 }
1121
1122 t2 = insert_value(&timers[0], tptr, val);
1123 t2->count++;
Willy Tarreaua2b39fb2011-07-10 21:39:35 +02001124 return;
1125}
1126
1127void filter_count_status(const char *accept_field, const char *time_field, struct timer **tptr)
1128{
1129 struct timer *t2;
1130 const char *b;
1131 int val;
1132
1133 if (time_field)
1134 b = field_start(time_field, STATUS_FIELD - TIME_FIELD + 1);
1135 else
1136 b = field_start(accept_field, STATUS_FIELD - ACCEPT_FIELD + 1);
1137
1138 if (unlikely(!*b)) {
1139 truncated_line(linenum, line);
1140 return;
1141 }
1142
1143 val = str2ic(b);
1144
1145 t2 = insert_value(&timers[0], tptr, val);
1146 t2->count++;
1147}
1148
Willy Tarreau8a09b662012-10-10 10:26:22 +02001149void filter_count_cook_codes(const char *accept_field, const char *time_field, struct timer **tptr)
1150{
1151 struct timer *t2;
1152 const char *b;
1153 int val;
1154
1155 if (time_field)
1156 b = field_start(time_field, TERM_CODES_FIELD - TIME_FIELD + 1);
1157 else
1158 b = field_start(accept_field, TERM_CODES_FIELD - ACCEPT_FIELD + 1);
1159
1160 if (unlikely(!*b)) {
1161 truncated_line(linenum, line);
1162 return;
1163 }
1164
1165 val = 256 * b[2] + b[3];
1166
1167 t2 = insert_value(&timers[0], tptr, val);
1168 t2->count++;
1169}
1170
Willy Tarreaua2b39fb2011-07-10 21:39:35 +02001171void filter_count_term_codes(const char *accept_field, const char *time_field, struct timer **tptr)
1172{
1173 struct timer *t2;
1174 const char *b;
1175 int val;
1176
1177 if (time_field)
1178 b = field_start(time_field, TERM_CODES_FIELD - TIME_FIELD + 1);
1179 else
1180 b = field_start(accept_field, TERM_CODES_FIELD - ACCEPT_FIELD + 1);
1181
1182 if (unlikely(!*b)) {
1183 truncated_line(linenum, line);
1184 return;
1185 }
1186
1187 val = 256 * b[0] + b[1];
1188
1189 t2 = insert_value(&timers[0], tptr, val);
1190 t2->count++;
1191}
1192
1193void filter_count_srv_status(const char *accept_field, const char *time_field, struct timer **tptr)
1194{
1195 const char *b, *e, *p;
1196 int f, err, array[5];
1197 struct ebmb_node *srv_node;
1198 struct srv_st *srv;
1199 int val;
1200
1201 /* the server field is before the status field, so let's
1202 * parse them in the proper order.
1203 */
1204 b = field_start(accept_field, SERVER_FIELD - ACCEPT_FIELD + 1);
1205 if (unlikely(!*b)) {
1206 truncated_line(linenum, line);
1207 return;
1208 }
1209
1210 e = field_stop(b + 1); /* we have the server name in [b]..[e-1] */
1211
1212 /* the chance that a server name already exists is extremely high,
1213 * so let's perform a normal lookup first.
1214 */
1215 srv_node = ebst_lookup_len(&timers[0], b, e - b);
1216 srv = container_of(srv_node, struct srv_st, node);
1217
1218 if (!srv_node) {
1219 /* server not yet in the tree, let's create it */
1220 srv = (void *)calloc(1, sizeof(struct srv_st) + e - b + 1);
1221 srv_node = &srv->node;
1222 memcpy(&srv_node->key, b, e - b);
1223 srv_node->key[e - b] = '\0';
1224 ebst_insert(&timers[0], srv_node);
1225 }
1226
1227 /* let's collect the connect and response times */
1228 if (!time_field) {
1229 time_field = field_start(e, TIME_FIELD - SERVER_FIELD);
1230 if (unlikely(!*time_field)) {
1231 truncated_line(linenum, line);
1232 return;
1233 }
1234 }
1235
1236 e = field_stop(time_field + 1);
1237 /* we have field TIME_FIELD in [time_field]..[e-1] */
1238
1239 p = time_field;
1240 err = 0;
1241 f = 0;
1242 while (!SEP(*p)) {
1243 array[f] = str2ic(p);
1244 if (array[f] < 0) {
1245 array[f] = -1;
1246 err = 1;
1247 }
1248 if (++f == 5)
1249 break;
1250 SKIP_CHAR(p, '/');
1251 }
1252
1253 if (unlikely(f < 5)){
1254 parse_err++;
1255 return;
1256 }
1257
1258 /* OK we have our timers in array[2,3] */
1259 if (!err)
1260 srv->nb_ok++;
1261
1262 if (array[2] >= 0) {
1263 srv->cum_ct += array[2];
1264 srv->nb_ct++;
1265 }
1266
1267 if (array[3] >= 0) {
1268 srv->cum_rt += array[3];
1269 srv->nb_rt++;
1270 }
1271
1272 /* we're interested in the 5 HTTP status classes (1xx ... 5xx), and
1273 * the invalid ones which will be reported as 0.
1274 */
1275 b = field_start(e, STATUS_FIELD - TIME_FIELD);
1276 if (unlikely(!*b)) {
1277 truncated_line(linenum, line);
1278 return;
1279 }
1280
1281 val = 0;
1282 if (*b >= '1' && *b <= '5')
1283 val = *b - '0';
1284
1285 srv->st_cnt[val]++;
1286}
1287
1288void filter_count_url(const char *accept_field, const char *time_field, struct timer **tptr)
1289{
1290 struct url_stat *ustat = NULL;
1291 struct ebpt_node *ebpt_old;
1292 const char *b, *e;
1293 int f, err, array[5];
Baptiste61aaad02012-09-08 23:10:03 +02001294 int val;
Willy Tarreaua2b39fb2011-07-10 21:39:35 +02001295
1296 /* let's collect the response time */
1297 if (!time_field) {
1298 time_field = field_start(accept_field, TIME_FIELD - ACCEPT_FIELD + 1); // avg 115 ns per line
1299 if (unlikely(!*time_field)) {
1300 truncated_line(linenum, line);
1301 return;
1302 }
1303 }
1304
1305 /* we have the field TIME_FIELD starting at <time_field>. We'll
1306 * parse the 5 timers to detect errors, it takes avg 55 ns per line.
1307 */
1308 e = time_field; err = 0; f = 0;
1309 while (!SEP(*e)) {
1310 array[f] = str2ic(e);
1311 if (array[f] < 0) {
1312 array[f] = -1;
1313 err = 1;
1314 }
1315 if (++f == 5)
1316 break;
1317 SKIP_CHAR(e, '/');
1318 }
1319 if (f < 5) {
1320 parse_err++;
1321 return;
1322 }
1323
1324 /* OK we have our timers in array[3], and err is >0 if at
1325 * least one -1 was seen. <e> points to the first char of
1326 * the last timer. Let's prepare a new node with that.
1327 */
1328 if (unlikely(!ustat))
1329 ustat = calloc(1, sizeof(*ustat));
1330
1331 ustat->nb_err = err;
1332 ustat->nb_req = 1;
1333
1334 /* use array[4] = total time in case of error */
1335 ustat->total_time = (array[3] >= 0) ? array[3] : array[4];
1336 ustat->total_time_ok = (array[3] >= 0) ? array[3] : 0;
1337
Baptiste61aaad02012-09-08 23:10:03 +02001338 e = field_start(e, BYTES_SENT_FIELD - TIME_FIELD + 1);
1339 val = str2ic(e);
1340 ustat->total_bytes_sent = val;
1341
Willy Tarreaua2b39fb2011-07-10 21:39:35 +02001342 /* the line may be truncated because of a bad request or anything like this,
1343 * without a method. Also, if it does not begin with an quote, let's skip to
1344 * the next field because it's a capture. Let's fall back to the "method" itself
1345 * if there's nothing else.
1346 */
Baptiste61aaad02012-09-08 23:10:03 +02001347 e = field_start(e, METH_FIELD - BYTES_SENT_FIELD + 1);
Willy Tarreau61a40c72011-09-06 08:11:27 +02001348 while (*e != '"' && *e) {
1349 /* Note: some syslog servers escape quotes ! */
1350 if (*e == '\\' && e[1] == '"')
1351 break;
Willy Tarreaua2b39fb2011-07-10 21:39:35 +02001352 e = field_start(e, 2);
Willy Tarreau61a40c72011-09-06 08:11:27 +02001353 }
Willy Tarreaua2b39fb2011-07-10 21:39:35 +02001354
1355 if (unlikely(!*e)) {
1356 truncated_line(linenum, line);
1357 return;
1358 }
1359
1360 b = field_start(e, URL_FIELD - METH_FIELD + 1); // avg 40 ns per line
1361 if (!*b)
1362 b = e;
1363
1364 /* stop at end of field or first ';' or '?', takes avg 64 ns per line */
1365 e = b;
1366 do {
Willy Tarreau14389e72011-07-10 22:11:17 +02001367 if (*e == ' ' || *e == '?' || *e == ';') {
Willy Tarreaua2b39fb2011-07-10 21:39:35 +02001368 *(char *)e = 0;
1369 break;
1370 }
1371 e++;
1372 } while (*e);
1373
1374 /* now instead of copying the URL for a simple lookup, we'll link
1375 * to it from the node we're trying to insert. If it returns a
1376 * different value, it was already there. Otherwise we just have
1377 * to dynamically realloc an entry using strdup().
1378 */
1379 ustat->node.url.key = (char *)b;
1380 ebpt_old = ebis_insert(&timers[0], &ustat->node.url);
1381
1382 if (ebpt_old != &ustat->node.url) {
1383 struct url_stat *ustat_old;
1384 /* node was already there, let's update previous one */
1385 ustat_old = container_of(ebpt_old, struct url_stat, node.url);
1386 ustat_old->nb_req ++;
1387 ustat_old->nb_err += ustat->nb_err;
1388 ustat_old->total_time += ustat->total_time;
1389 ustat_old->total_time_ok += ustat->total_time_ok;
Baptiste61aaad02012-09-08 23:10:03 +02001390 ustat_old->total_bytes_sent += ustat->total_bytes_sent;
Willy Tarreaua2b39fb2011-07-10 21:39:35 +02001391 } else {
1392 ustat->url = ustat->node.url.key = strdup(ustat->node.url.key);
1393 ustat = NULL; /* node was used */
1394 }
1395}
1396
1397void filter_graphs(const char *accept_field, const char *time_field, struct timer **tptr)
1398{
1399 struct timer *t2;
1400 const char *e, *p;
1401 int f, err, array[5];
1402
1403 if (!time_field) {
1404 time_field = field_start(accept_field, TIME_FIELD - ACCEPT_FIELD + 1);
1405 if (unlikely(!*time_field)) {
1406 truncated_line(linenum, line);
1407 return;
1408 }
1409 }
1410
1411 e = field_stop(time_field + 1);
1412 /* we have field TIME_FIELD in [time_field]..[e-1] */
1413
1414 p = time_field;
1415 err = 0;
1416 f = 0;
1417 while (!SEP(*p)) {
1418 array[f] = str2ic(p);
1419 if (array[f] < 0) {
1420 array[f] = -1;
1421 err = 1;
1422 }
1423 if (++f == 5)
1424 break;
1425 SKIP_CHAR(p, '/');
1426 }
1427
1428 if (unlikely(f < 5)) {
1429 parse_err++;
1430 return;
1431 }
1432
1433 /* if we find at least one negative time, we count one error
1434 * with a time equal to the total session time. This will
1435 * emphasize quantum timing effects associated to known
1436 * timeouts. Note that on some buggy machines, it is possible
1437 * that the total time is negative, hence the reason to reset
1438 * it.
1439 */
1440
1441 if (filter & FILT_GRAPH_TIMERS) {
1442 if (err) {
1443 if (array[4] < 0)
1444 array[4] = -1;
1445 t2 = insert_timer(&timers[0], tptr, array[4]); // total time
1446 t2->count++;
1447 } else {
1448 int v;
1449
1450 t2 = insert_timer(&timers[1], tptr, array[0]); t2->count++; // req
1451 t2 = insert_timer(&timers[2], tptr, array[2]); t2->count++; // conn
1452 t2 = insert_timer(&timers[3], tptr, array[3]); t2->count++; // resp
1453
1454 v = array[4] - array[0] - array[1] - array[2] - array[3]; // data time
1455 if (v < 0 && !(filter & FILT_QUIET))
1456 fprintf(stderr, "ERR: %s (%d %d %d %d %d => %d)\n",
1457 line, array[0], array[1], array[2], array[3], array[4], v);
1458 t2 = insert_timer(&timers[4], tptr, v); t2->count++;
1459 lines_out++;
1460 }
1461 } else { /* percentile */
1462 if (err) {
1463 if (array[4] < 0)
1464 array[4] = -1;
1465 t2 = insert_value(&timers[0], tptr, array[4]); // total time
1466 t2->count++;
1467 } else {
1468 int v;
1469
1470 t2 = insert_value(&timers[1], tptr, array[0]); t2->count++; // req
1471 t2 = insert_value(&timers[2], tptr, array[2]); t2->count++; // conn
1472 t2 = insert_value(&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_value(&timers[4], tptr, v); t2->count++;
1479 lines_out++;
1480 }
1481 }
1482}
1483
1484
Willy Tarreau72c28532009-01-22 18:56:50 +01001485/*
1486 * Local variables:
1487 * c-indent-level: 8
1488 * c-basic-offset: 8
1489 * End:
1490 */