Willy Tarreau | 72c2853 | 2009-01-22 18:56:50 +0100 | [diff] [blame] | 1 | /* |
Willy Tarreau | d8fc110 | 2010-09-12 17:56:16 +0200 | [diff] [blame] | 2 | * haproxy log statistics reporter |
Willy Tarreau | 72c2853 | 2009-01-22 18:56:50 +0100 | [diff] [blame] | 3 | * |
Willy Tarreau | d220106 | 2010-05-27 18:17:30 +0200 | [diff] [blame] | 4 | * Copyright 2000-2010 Willy Tarreau <w@1wt.eu> |
Willy Tarreau | 72c2853 | 2009-01-22 18:56:50 +0100 | [diff] [blame] | 5 | * |
| 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 Tarreau | 72c2853 | 2009-01-22 18:56:50 +0100 | [diff] [blame] | 13 | #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 Tarreau | 45cb4fb | 2009-10-26 21:10:04 +0100 | [diff] [blame] | 22 | #include <eb32tree.h> |
Willy Tarreau | abe45b6 | 2010-10-28 20:33:46 +0200 | [diff] [blame] | 23 | #include <eb64tree.h> |
| 24 | #include <ebistree.h> |
Willy Tarreau | d220106 | 2010-05-27 18:17:30 +0200 | [diff] [blame] | 25 | #include <ebsttree.h> |
Willy Tarreau | 72c2853 | 2009-01-22 18:56:50 +0100 | [diff] [blame] | 26 | |
Willy Tarreau | d220106 | 2010-05-27 18:17:30 +0200 | [diff] [blame] | 27 | #define SOURCE_FIELD 5 |
Willy Tarreau | 72c2853 | 2009-01-22 18:56:50 +0100 | [diff] [blame] | 28 | #define ACCEPT_FIELD 6 |
Willy Tarreau | d220106 | 2010-05-27 18:17:30 +0200 | [diff] [blame] | 29 | #define SERVER_FIELD 8 |
Willy Tarreau | 72c2853 | 2009-01-22 18:56:50 +0100 | [diff] [blame] | 30 | #define TIME_FIELD 9 |
| 31 | #define STATUS_FIELD 10 |
Willy Tarreau | d8fc110 | 2010-09-12 17:56:16 +0200 | [diff] [blame] | 32 | #define TERM_CODES_FIELD 14 |
Willy Tarreau | 72c2853 | 2009-01-22 18:56:50 +0100 | [diff] [blame] | 33 | #define CONN_FIELD 15 |
Willy Tarreau | abe45b6 | 2010-10-28 20:33:46 +0200 | [diff] [blame] | 34 | #define METH_FIELD 17 |
| 35 | #define URL_FIELD 18 |
Willy Tarreau | 72c2853 | 2009-01-22 18:56:50 +0100 | [diff] [blame] | 36 | #define MAXLINE 16384 |
| 37 | #define QBITS 4 |
| 38 | |
Willy Tarreau | df6f0d1 | 2011-07-10 18:15:08 +0200 | [diff] [blame] | 39 | #define SEP(c) ((unsigned char)(c) <= ' ') |
| 40 | #define SKIP_CHAR(p,c) do { while (1) { int __c = (unsigned char)*p++; if (__c == c) break; if (__c <= ' ') { p--; break; } } } while (0) |
Willy Tarreau | 72c2853 | 2009-01-22 18:56:50 +0100 | [diff] [blame] | 41 | |
| 42 | /* [0] = err/date, [1] = req, [2] = conn, [3] = resp, [4] = data */ |
| 43 | static struct eb_root timers[5] = { |
| 44 | EB_ROOT_UNIQUE, EB_ROOT_UNIQUE, EB_ROOT_UNIQUE, |
| 45 | EB_ROOT_UNIQUE, EB_ROOT_UNIQUE, |
| 46 | }; |
| 47 | |
| 48 | struct timer { |
| 49 | struct eb32_node node; |
| 50 | unsigned int count; |
| 51 | }; |
| 52 | |
Willy Tarreau | d220106 | 2010-05-27 18:17:30 +0200 | [diff] [blame] | 53 | struct srv_st { |
| 54 | unsigned int st_cnt[6]; /* 0xx to 5xx */ |
| 55 | unsigned int nb_ct, nb_rt, nb_ok; |
| 56 | unsigned long long cum_ct, cum_rt; |
| 57 | struct ebmb_node node; |
| 58 | /* don't put anything else here, the server name will be there */ |
| 59 | }; |
Willy Tarreau | 72c2853 | 2009-01-22 18:56:50 +0100 | [diff] [blame] | 60 | |
Willy Tarreau | abe45b6 | 2010-10-28 20:33:46 +0200 | [diff] [blame] | 61 | struct url_stat { |
| 62 | union { |
| 63 | struct ebpt_node url; |
| 64 | struct eb64_node val; |
| 65 | } node; |
| 66 | char *url; |
| 67 | unsigned long long total_time; /* sum(all reqs' times) */ |
| 68 | unsigned long long total_time_ok; /* sum(all OK reqs' times) */ |
| 69 | unsigned int nb_err, nb_req; |
| 70 | }; |
| 71 | |
Willy Tarreau | 72c2853 | 2009-01-22 18:56:50 +0100 | [diff] [blame] | 72 | #define FILT_COUNT_ONLY 0x01 |
| 73 | #define FILT_INVERT 0x02 |
| 74 | #define FILT_QUIET 0x04 |
| 75 | #define FILT_ERRORS_ONLY 0x08 |
| 76 | #define FILT_ACC_DELAY 0x10 |
| 77 | #define FILT_ACC_COUNT 0x20 |
| 78 | #define FILT_GRAPH_TIMERS 0x40 |
Willy Tarreau | 214c203 | 2009-02-20 11:02:32 +0100 | [diff] [blame] | 79 | #define FILT_PERCENTILE 0x80 |
Willy Tarreau | 5bdfd96 | 2009-10-14 15:16:29 +0200 | [diff] [blame] | 80 | #define FILT_TIME_RESP 0x100 |
| 81 | |
| 82 | #define FILT_INVERT_ERRORS 0x200 |
| 83 | #define FILT_INVERT_TIME_RESP 0x400 |
Willy Tarreau | 72c2853 | 2009-01-22 18:56:50 +0100 | [diff] [blame] | 84 | |
Willy Tarreau | 0f423a7 | 2010-05-03 10:50:54 +0200 | [diff] [blame] | 85 | #define FILT_COUNT_STATUS 0x800 |
Willy Tarreau | d220106 | 2010-05-27 18:17:30 +0200 | [diff] [blame] | 86 | #define FILT_COUNT_SRV_STATUS 0x1000 |
Willy Tarreau | d8fc110 | 2010-09-12 17:56:16 +0200 | [diff] [blame] | 87 | #define FILT_COUNT_TERM_CODES 0x2000 |
Willy Tarreau | 0f423a7 | 2010-05-03 10:50:54 +0200 | [diff] [blame] | 88 | |
Willy Tarreau | abe45b6 | 2010-10-28 20:33:46 +0200 | [diff] [blame] | 89 | #define FILT_COUNT_URL_ONLY 0x004000 |
| 90 | #define FILT_COUNT_URL_COUNT 0x008000 |
| 91 | #define FILT_COUNT_URL_ERR 0x010000 |
| 92 | #define FILT_COUNT_URL_TTOT 0x020000 |
| 93 | #define FILT_COUNT_URL_TAVG 0x040000 |
| 94 | #define FILT_COUNT_URL_TTOTO 0x080000 |
| 95 | #define FILT_COUNT_URL_TAVGO 0x100000 |
| 96 | #define FILT_COUNT_URL_ANY (FILT_COUNT_URL_ONLY|FILT_COUNT_URL_COUNT|FILT_COUNT_URL_ERR| \ |
| 97 | FILT_COUNT_URL_TTOT|FILT_COUNT_URL_TAVG|FILT_COUNT_URL_TTOTO|FILT_COUNT_URL_TAVGO) |
| 98 | |
Willy Tarreau | 70c428f | 2011-07-10 17:27:40 +0200 | [diff] [blame] | 99 | #define FILT_HTTP_ONLY 0x200000 |
| 100 | |
Willy Tarreau | 72c2853 | 2009-01-22 18:56:50 +0100 | [diff] [blame] | 101 | unsigned int filter = 0; |
| 102 | unsigned int filter_invert = 0; |
Willy Tarreau | 214c203 | 2009-02-20 11:02:32 +0100 | [diff] [blame] | 103 | const char *line; |
Willy Tarreau | 72c2853 | 2009-01-22 18:56:50 +0100 | [diff] [blame] | 104 | |
Willy Tarreau | 214c203 | 2009-02-20 11:02:32 +0100 | [diff] [blame] | 105 | const char *fgets2(FILE *stream); |
Willy Tarreau | 72c2853 | 2009-01-22 18:56:50 +0100 | [diff] [blame] | 106 | |
| 107 | void die(const char *msg) |
| 108 | { |
| 109 | fprintf(stderr, |
| 110 | "%s" |
Willy Tarreau | abe45b6 | 2010-10-28 20:33:46 +0200 | [diff] [blame] | 111 | "Usage: halog [-q] [-c] [-v] {-gt|-pct|-st|-tc|-srv|-u|-uc|-ue|-ua|-ut|-uao|-uto}\n" |
Willy Tarreau | 70c428f | 2011-07-10 17:27:40 +0200 | [diff] [blame] | 112 | " [-s <skip>] [-e|-E] [-H] [-rt|-RT <time>] [-ad <delay>] [-ac <count>] < log\n" |
Willy Tarreau | 72c2853 | 2009-01-22 18:56:50 +0100 | [diff] [blame] | 113 | "\n", |
| 114 | msg ? msg : "" |
| 115 | ); |
| 116 | exit(1); |
| 117 | } |
| 118 | |
| 119 | |
| 120 | /* return pointer to first char not part of current field starting at <p>. */ |
| 121 | const char *field_stop(const char *p) |
| 122 | { |
| 123 | unsigned char c; |
| 124 | |
| 125 | while (1) { |
| 126 | c = *(p++); |
| 127 | if (c > ' ') |
| 128 | continue; |
| 129 | if (c == ' ' || c == '\t' || c == 0) |
| 130 | break; |
| 131 | } |
| 132 | return p - 1; |
| 133 | } |
| 134 | |
| 135 | /* return field <field> (starting from 1) in string <p>. Only consider |
| 136 | * contiguous spaces (or tabs) as one delimiter. May return pointer to |
| 137 | * last char if field is not found. Equivalent to awk '{print $field}'. |
| 138 | */ |
| 139 | const char *field_start(const char *p, int field) |
| 140 | { |
| 141 | unsigned char c; |
| 142 | while (1) { |
| 143 | /* skip spaces */ |
| 144 | while (1) { |
| 145 | c = *p; |
| 146 | if (c > ' ') |
| 147 | break; |
| 148 | if (c == ' ' || c == '\t') |
| 149 | goto next; |
| 150 | if (!c) /* end of line */ |
| 151 | return p; |
| 152 | /* other char => new field */ |
| 153 | break; |
| 154 | next: |
| 155 | p++; |
| 156 | } |
| 157 | |
| 158 | /* start of field */ |
| 159 | field--; |
| 160 | if (!field) |
| 161 | return p; |
| 162 | |
| 163 | /* skip this field */ |
| 164 | while (1) { |
| 165 | c = *(p++); |
| 166 | if (c > ' ') |
| 167 | continue; |
| 168 | if (c == ' ' || c == '\t') |
| 169 | break; |
| 170 | if (c == '\0') |
| 171 | return p; |
| 172 | } |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | /* keep only the <bits> higher bits of <i> */ |
| 177 | static inline unsigned int quantify_u32(unsigned int i, int bits) |
| 178 | { |
| 179 | int high; |
| 180 | |
| 181 | if (!bits) |
| 182 | return 0; |
| 183 | |
| 184 | if (i) |
| 185 | high = fls_auto(i); // 1 to 32 |
| 186 | else |
| 187 | high = 0; |
| 188 | |
| 189 | if (high <= bits) |
| 190 | return i; |
| 191 | |
| 192 | return i & ~((1 << (high - bits)) - 1); |
| 193 | } |
| 194 | |
| 195 | /* keep only the <bits> higher bits of the absolute value of <i>, as well as |
| 196 | * its sign. */ |
| 197 | static inline int quantify(int i, int bits) |
| 198 | { |
| 199 | if (i >= 0) |
| 200 | return quantify_u32(i, bits); |
| 201 | else |
| 202 | return -quantify_u32(-i, bits); |
| 203 | } |
| 204 | |
| 205 | /* Insert timer value <v> into tree <r>. A pre-allocated node must be passed |
| 206 | * in <alloc>. It may be NULL, in which case the function will allocate it |
| 207 | * itself. It will be reset to NULL once consumed. The caller is responsible |
| 208 | * for freeing the node once not used anymore. The node where the value was |
| 209 | * inserted is returned. |
| 210 | */ |
| 211 | struct timer *insert_timer(struct eb_root *r, struct timer **alloc, int v) |
| 212 | { |
| 213 | struct timer *t = *alloc; |
| 214 | struct eb32_node *n; |
| 215 | |
| 216 | if (!t) { |
| 217 | t = calloc(sizeof(*t), 1); |
| 218 | if (unlikely(!t)) { |
| 219 | fprintf(stderr, "%s: not enough memory\n", __FUNCTION__); |
| 220 | exit(1); |
| 221 | } |
| 222 | } |
| 223 | t->node.key = quantify(v, QBITS); // keep only the higher QBITS bits |
| 224 | |
| 225 | n = eb32i_insert(r, &t->node); |
| 226 | if (n == &t->node) |
| 227 | t = NULL; /* node inserted, will malloc next time */ |
| 228 | |
| 229 | *alloc = t; |
| 230 | return container_of(n, struct timer, node); |
| 231 | } |
| 232 | |
| 233 | /* Insert value value <v> into tree <r>. A pre-allocated node must be passed |
| 234 | * in <alloc>. It may be NULL, in which case the function will allocate it |
| 235 | * itself. It will be reset to NULL once consumed. The caller is responsible |
| 236 | * for freeing the node once not used anymore. The node where the value was |
| 237 | * inserted is returned. |
| 238 | */ |
| 239 | struct timer *insert_value(struct eb_root *r, struct timer **alloc, int v) |
| 240 | { |
| 241 | struct timer *t = *alloc; |
| 242 | struct eb32_node *n; |
| 243 | |
| 244 | if (!t) { |
| 245 | t = calloc(sizeof(*t), 1); |
| 246 | if (unlikely(!t)) { |
| 247 | fprintf(stderr, "%s: not enough memory\n", __FUNCTION__); |
| 248 | exit(1); |
| 249 | } |
| 250 | } |
| 251 | t->node.key = v; |
| 252 | |
| 253 | n = eb32i_insert(r, &t->node); |
| 254 | if (n == &t->node) |
| 255 | t = NULL; /* node inserted, will malloc next time */ |
| 256 | |
| 257 | *alloc = t; |
| 258 | return container_of(n, struct timer, node); |
| 259 | } |
| 260 | |
| 261 | int str2ic(const char *s) |
| 262 | { |
| 263 | int i = 0; |
| 264 | int j, k; |
| 265 | |
| 266 | if (*s != '-') { |
| 267 | /* positive number */ |
| 268 | while (1) { |
| 269 | j = (*s++) - '0'; |
| 270 | k = i * 10; |
| 271 | if ((unsigned)j > 9) |
| 272 | break; |
| 273 | i = k + j; |
| 274 | } |
| 275 | } else { |
| 276 | /* negative number */ |
| 277 | s++; |
| 278 | while (1) { |
| 279 | j = (*s++) - '0'; |
| 280 | k = i * 10; |
| 281 | if ((unsigned)j > 9) |
| 282 | break; |
| 283 | i = k - j; |
| 284 | } |
| 285 | } |
| 286 | |
| 287 | return i; |
| 288 | } |
| 289 | |
| 290 | |
| 291 | /* Equivalent to strtoul with a length. */ |
| 292 | static inline unsigned int __strl2ui(const char *s, int len) |
| 293 | { |
| 294 | unsigned int i = 0; |
| 295 | while (len-- > 0) { |
| 296 | i = i * 10 - '0'; |
| 297 | i += (unsigned char)*s++; |
| 298 | } |
| 299 | return i; |
| 300 | } |
| 301 | |
| 302 | unsigned int strl2ui(const char *s, int len) |
| 303 | { |
| 304 | return __strl2ui(s, len); |
| 305 | } |
| 306 | |
| 307 | /* Convert "[04/Dec/2008:09:49:40.555]" to an integer equivalent to the time of |
| 308 | * the day in milliseconds. It returns -1 for all unparsable values. The parser |
| 309 | * looks ugly but gcc emits far better code that way. |
| 310 | */ |
| 311 | int convert_date(const char *field) |
| 312 | { |
| 313 | unsigned int h, m, s, ms; |
| 314 | unsigned char c; |
| 315 | const char *b, *e; |
| 316 | |
| 317 | h = m = s = ms = 0; |
| 318 | e = field; |
| 319 | |
| 320 | /* skip the date */ |
| 321 | while (1) { |
| 322 | c = *(e++); |
| 323 | if (c == ':') |
| 324 | break; |
| 325 | if (!c) |
| 326 | goto out_err; |
| 327 | } |
| 328 | |
| 329 | /* hour + ':' */ |
| 330 | b = e; |
| 331 | while (1) { |
| 332 | c = *(e++) - '0'; |
| 333 | if (c > 9) |
| 334 | break; |
| 335 | h = h * 10 + c; |
| 336 | } |
| 337 | if (c == (unsigned char)(0 - '0')) |
| 338 | goto out_err; |
| 339 | |
| 340 | /* minute + ':' */ |
| 341 | b = e; |
| 342 | while (1) { |
| 343 | c = *(e++) - '0'; |
| 344 | if (c > 9) |
| 345 | break; |
| 346 | m = m * 10 + c; |
| 347 | } |
| 348 | if (c == (unsigned char)(0 - '0')) |
| 349 | goto out_err; |
| 350 | |
| 351 | /* second + '.' or ']' */ |
| 352 | b = e; |
| 353 | while (1) { |
| 354 | c = *(e++) - '0'; |
| 355 | if (c > 9) |
| 356 | break; |
| 357 | s = s * 10 + c; |
| 358 | } |
| 359 | if (c == (unsigned char)(0 - '0')) |
| 360 | goto out_err; |
| 361 | |
| 362 | /* if there's a '.', we have milliseconds */ |
| 363 | if (c == (unsigned char)('.' - '0')) { |
| 364 | /* millisecond second + ']' */ |
| 365 | b = e; |
| 366 | while (1) { |
| 367 | c = *(e++) - '0'; |
| 368 | if (c > 9) |
| 369 | break; |
| 370 | ms = ms * 10 + c; |
| 371 | } |
| 372 | if (c == (unsigned char)(0 - '0')) |
| 373 | goto out_err; |
| 374 | } |
| 375 | return (((h * 60) + m) * 60 + s) * 1000 + ms; |
| 376 | out_err: |
| 377 | return -1; |
| 378 | } |
| 379 | |
| 380 | void truncated_line(int linenum, const char *line) |
| 381 | { |
| 382 | if (!(filter & FILT_QUIET)) |
| 383 | fprintf(stderr, "Truncated line %d: %s\n", linenum, line); |
| 384 | } |
| 385 | |
| 386 | int main(int argc, char **argv) |
| 387 | { |
Willy Tarreau | 26deaf5 | 2011-07-10 19:47:48 +0200 | [diff] [blame^] | 388 | const char *b, *e, *p, *time_field, *accept_field; |
Willy Tarreau | 72c2853 | 2009-01-22 18:56:50 +0100 | [diff] [blame] | 389 | const char *output_file = NULL; |
| 390 | int f, tot, last, linenum, err, parse_err; |
| 391 | struct timer *t = NULL, *t2; |
| 392 | struct eb32_node *n; |
Willy Tarreau | abe45b6 | 2010-10-28 20:33:46 +0200 | [diff] [blame] | 393 | struct url_stat *ustat = NULL; |
| 394 | struct ebpt_node *ebpt_old; |
Willy Tarreau | 72c2853 | 2009-01-22 18:56:50 +0100 | [diff] [blame] | 395 | int val, test; |
| 396 | int array[5]; |
| 397 | int filter_acc_delay = 0, filter_acc_count = 0; |
Willy Tarreau | 5bdfd96 | 2009-10-14 15:16:29 +0200 | [diff] [blame] | 398 | int filter_time_resp = 0; |
Willy Tarreau | 72c2853 | 2009-01-22 18:56:50 +0100 | [diff] [blame] | 399 | int skip_fields = 1; |
| 400 | |
| 401 | argc--; argv++; |
| 402 | while (argc > 0) { |
| 403 | if (*argv[0] != '-') |
| 404 | break; |
| 405 | |
| 406 | if (strcmp(argv[0], "-ad") == 0) { |
| 407 | if (argc < 2) die("missing option for -ad"); |
| 408 | argc--; argv++; |
| 409 | filter |= FILT_ACC_DELAY; |
| 410 | filter_acc_delay = atol(*argv); |
| 411 | } |
| 412 | else if (strcmp(argv[0], "-ac") == 0) { |
| 413 | if (argc < 2) die("missing option for -ac"); |
| 414 | argc--; argv++; |
| 415 | filter |= FILT_ACC_COUNT; |
| 416 | filter_acc_count = atol(*argv); |
| 417 | } |
Willy Tarreau | 5bdfd96 | 2009-10-14 15:16:29 +0200 | [diff] [blame] | 418 | else if (strcmp(argv[0], "-rt") == 0) { |
| 419 | if (argc < 2) die("missing option for -rt"); |
| 420 | argc--; argv++; |
| 421 | filter |= FILT_TIME_RESP; |
| 422 | filter_time_resp = atol(*argv); |
| 423 | } |
| 424 | else if (strcmp(argv[0], "-RT") == 0) { |
| 425 | if (argc < 2) die("missing option for -RT"); |
| 426 | argc--; argv++; |
| 427 | filter |= FILT_TIME_RESP | FILT_INVERT_TIME_RESP; |
| 428 | filter_time_resp = atol(*argv); |
| 429 | } |
Willy Tarreau | 72c2853 | 2009-01-22 18:56:50 +0100 | [diff] [blame] | 430 | else if (strcmp(argv[0], "-s") == 0) { |
| 431 | if (argc < 2) die("missing option for -s"); |
| 432 | argc--; argv++; |
| 433 | skip_fields = atol(*argv); |
| 434 | } |
| 435 | else if (strcmp(argv[0], "-e") == 0) |
| 436 | filter |= FILT_ERRORS_ONLY; |
Willy Tarreau | 5bdfd96 | 2009-10-14 15:16:29 +0200 | [diff] [blame] | 437 | else if (strcmp(argv[0], "-E") == 0) |
| 438 | filter |= FILT_ERRORS_ONLY | FILT_INVERT_ERRORS; |
Willy Tarreau | 70c428f | 2011-07-10 17:27:40 +0200 | [diff] [blame] | 439 | else if (strcmp(argv[0], "-H") == 0) |
| 440 | filter |= FILT_HTTP_ONLY; |
Willy Tarreau | 72c2853 | 2009-01-22 18:56:50 +0100 | [diff] [blame] | 441 | else if (strcmp(argv[0], "-c") == 0) |
| 442 | filter |= FILT_COUNT_ONLY; |
| 443 | else if (strcmp(argv[0], "-q") == 0) |
| 444 | filter |= FILT_QUIET; |
| 445 | else if (strcmp(argv[0], "-v") == 0) |
| 446 | filter_invert = !filter_invert; |
| 447 | else if (strcmp(argv[0], "-gt") == 0) |
| 448 | filter |= FILT_GRAPH_TIMERS; |
Willy Tarreau | 214c203 | 2009-02-20 11:02:32 +0100 | [diff] [blame] | 449 | else if (strcmp(argv[0], "-pct") == 0) |
| 450 | filter |= FILT_PERCENTILE; |
Willy Tarreau | 0f423a7 | 2010-05-03 10:50:54 +0200 | [diff] [blame] | 451 | else if (strcmp(argv[0], "-st") == 0) |
| 452 | filter |= FILT_COUNT_STATUS; |
Willy Tarreau | d220106 | 2010-05-27 18:17:30 +0200 | [diff] [blame] | 453 | else if (strcmp(argv[0], "-srv") == 0) |
| 454 | filter |= FILT_COUNT_SRV_STATUS; |
Willy Tarreau | d8fc110 | 2010-09-12 17:56:16 +0200 | [diff] [blame] | 455 | else if (strcmp(argv[0], "-tc") == 0) |
| 456 | filter |= FILT_COUNT_TERM_CODES; |
Willy Tarreau | abe45b6 | 2010-10-28 20:33:46 +0200 | [diff] [blame] | 457 | else if (strcmp(argv[0], "-u") == 0) |
| 458 | filter |= FILT_COUNT_URL_ONLY; |
| 459 | else if (strcmp(argv[0], "-uc") == 0) |
| 460 | filter |= FILT_COUNT_URL_COUNT; |
| 461 | else if (strcmp(argv[0], "-ue") == 0) |
| 462 | filter |= FILT_COUNT_URL_ERR; |
| 463 | else if (strcmp(argv[0], "-ua") == 0) |
| 464 | filter |= FILT_COUNT_URL_TAVG; |
| 465 | else if (strcmp(argv[0], "-ut") == 0) |
| 466 | filter |= FILT_COUNT_URL_TTOT; |
| 467 | else if (strcmp(argv[0], "-uao") == 0) |
| 468 | filter |= FILT_COUNT_URL_TAVGO; |
| 469 | else if (strcmp(argv[0], "-uto") == 0) |
| 470 | filter |= FILT_COUNT_URL_TTOTO; |
Willy Tarreau | 72c2853 | 2009-01-22 18:56:50 +0100 | [diff] [blame] | 471 | else if (strcmp(argv[0], "-o") == 0) { |
| 472 | if (output_file) |
| 473 | die("Fatal: output file name already specified.\n"); |
| 474 | if (argc < 2) |
| 475 | die("Fatal: missing output file name.\n"); |
| 476 | output_file = argv[1]; |
| 477 | } |
| 478 | argc--; |
| 479 | argv++; |
| 480 | } |
| 481 | |
| 482 | if (!filter) |
| 483 | die("No action specified.\n"); |
| 484 | |
| 485 | if (filter & FILT_ACC_COUNT && !filter_acc_count) |
| 486 | filter_acc_count=1; |
| 487 | |
| 488 | if (filter & FILT_ACC_DELAY && !filter_acc_delay) |
| 489 | filter_acc_delay = 1; |
| 490 | |
| 491 | linenum = 0; |
| 492 | tot = 0; |
| 493 | parse_err = 0; |
| 494 | |
Willy Tarreau | 214c203 | 2009-02-20 11:02:32 +0100 | [diff] [blame] | 495 | while ((line = fgets2(stdin)) != NULL) { |
Willy Tarreau | 72c2853 | 2009-01-22 18:56:50 +0100 | [diff] [blame] | 496 | linenum++; |
Willy Tarreau | 26deaf5 | 2011-07-10 19:47:48 +0200 | [diff] [blame^] | 497 | time_field = NULL; accept_field = NULL; |
Willy Tarreau | 72c2853 | 2009-01-22 18:56:50 +0100 | [diff] [blame] | 498 | |
Willy Tarreau | 5bdfd96 | 2009-10-14 15:16:29 +0200 | [diff] [blame] | 499 | test = 1; |
Willy Tarreau | 26deaf5 | 2011-07-10 19:47:48 +0200 | [diff] [blame^] | 500 | |
| 501 | /* for any line we process, we first ensure that there is a field |
| 502 | * looking like the accept date field (beginning with a '['). |
| 503 | */ |
| 504 | accept_field = field_start(line, ACCEPT_FIELD + skip_fields); |
| 505 | if (*accept_field != '[') { |
| 506 | parse_err++; |
| 507 | continue; |
| 508 | } |
| 509 | |
| 510 | /* the day of month field is begin 01 and 31 */ |
| 511 | if (unlikely(accept_field[1] < '0' || accept_field[1] > '3')) { |
| 512 | parse_err++; |
| 513 | continue; |
| 514 | } |
| 515 | |
Willy Tarreau | 70c428f | 2011-07-10 17:27:40 +0200 | [diff] [blame] | 516 | if (unlikely(filter & FILT_HTTP_ONLY)) { |
| 517 | /* only report lines with at least 4 timers */ |
Willy Tarreau | 26deaf5 | 2011-07-10 19:47:48 +0200 | [diff] [blame^] | 518 | if (unlikely(!time_field)) { |
| 519 | time_field = field_start(accept_field, TIME_FIELD - ACCEPT_FIELD + 1); |
| 520 | if (!*time_field) { |
| 521 | truncated_line(linenum, line); |
| 522 | continue; |
| 523 | } |
Willy Tarreau | 70c428f | 2011-07-10 17:27:40 +0200 | [diff] [blame] | 524 | } |
| 525 | |
Willy Tarreau | 758a6ea | 2011-07-10 18:53:44 +0200 | [diff] [blame] | 526 | e = field_stop(time_field + 1); |
| 527 | /* we have field TIME_FIELD in [time_field]..[e-1] */ |
| 528 | p = time_field; |
Willy Tarreau | 70c428f | 2011-07-10 17:27:40 +0200 | [diff] [blame] | 529 | f = 0; |
Willy Tarreau | df6f0d1 | 2011-07-10 18:15:08 +0200 | [diff] [blame] | 530 | while (!SEP(*p)) { |
Willy Tarreau | 70c428f | 2011-07-10 17:27:40 +0200 | [diff] [blame] | 531 | if (++f == 4) |
| 532 | break; |
| 533 | SKIP_CHAR(p, '/'); |
| 534 | } |
| 535 | test &= (f >= 4); |
| 536 | } |
| 537 | |
Willy Tarreau | 2651ac3 | 2010-05-05 12:20:19 +0200 | [diff] [blame] | 538 | if (unlikely(filter & FILT_TIME_RESP)) { |
Willy Tarreau | 5bdfd96 | 2009-10-14 15:16:29 +0200 | [diff] [blame] | 539 | int tps; |
| 540 | |
| 541 | /* only report lines with response times larger than filter_time_resp */ |
Willy Tarreau | 26deaf5 | 2011-07-10 19:47:48 +0200 | [diff] [blame^] | 542 | if (unlikely(!time_field)) { |
| 543 | time_field = field_start(accept_field, TIME_FIELD - ACCEPT_FIELD + 1); |
| 544 | if (!*time_field) { |
| 545 | truncated_line(linenum, line); |
| 546 | continue; |
| 547 | } |
Willy Tarreau | 5bdfd96 | 2009-10-14 15:16:29 +0200 | [diff] [blame] | 548 | } |
| 549 | |
Willy Tarreau | 758a6ea | 2011-07-10 18:53:44 +0200 | [diff] [blame] | 550 | e = field_stop(time_field + 1); |
| 551 | /* we have field TIME_FIELD in [time_field]..[e-1], let's check only the response time */ |
Willy Tarreau | 5bdfd96 | 2009-10-14 15:16:29 +0200 | [diff] [blame] | 552 | |
Willy Tarreau | 758a6ea | 2011-07-10 18:53:44 +0200 | [diff] [blame] | 553 | p = time_field; |
Willy Tarreau | 5bdfd96 | 2009-10-14 15:16:29 +0200 | [diff] [blame] | 554 | err = 0; |
Willy Tarreau | 24bcb4f | 2010-10-28 20:39:50 +0200 | [diff] [blame] | 555 | f = 0; |
Willy Tarreau | df6f0d1 | 2011-07-10 18:15:08 +0200 | [diff] [blame] | 556 | while (!SEP(*p)) { |
Willy Tarreau | 5bdfd96 | 2009-10-14 15:16:29 +0200 | [diff] [blame] | 557 | tps = str2ic(p); |
| 558 | if (tps < 0) { |
| 559 | tps = -1; |
| 560 | err = 1; |
| 561 | } |
Willy Tarreau | 24bcb4f | 2010-10-28 20:39:50 +0200 | [diff] [blame] | 562 | if (++f == 4) |
| 563 | break; |
Willy Tarreau | 5bdfd96 | 2009-10-14 15:16:29 +0200 | [diff] [blame] | 564 | SKIP_CHAR(p, '/'); |
| 565 | } |
| 566 | |
| 567 | if (f < 4) { |
| 568 | parse_err++; |
| 569 | continue; |
| 570 | } |
| 571 | |
| 572 | test &= (tps >= filter_time_resp) ^ !!(filter & FILT_INVERT_TIME_RESP); |
| 573 | } |
| 574 | |
Willy Tarreau | 2651ac3 | 2010-05-05 12:20:19 +0200 | [diff] [blame] | 575 | if (unlikely(filter & FILT_ERRORS_ONLY)) { |
Willy Tarreau | 72c2853 | 2009-01-22 18:56:50 +0100 | [diff] [blame] | 576 | /* only report erroneous status codes */ |
Willy Tarreau | 26deaf5 | 2011-07-10 19:47:48 +0200 | [diff] [blame^] | 577 | if (time_field) |
| 578 | b = field_start(time_field, STATUS_FIELD - TIME_FIELD + 1); |
| 579 | else |
| 580 | b = field_start(accept_field, STATUS_FIELD - ACCEPT_FIELD + 1); |
| 581 | |
Willy Tarreau | 72c2853 | 2009-01-22 18:56:50 +0100 | [diff] [blame] | 582 | if (!*b) { |
| 583 | truncated_line(linenum, line); |
| 584 | continue; |
| 585 | } |
| 586 | if (*b == '-') { |
Willy Tarreau | 5bdfd96 | 2009-10-14 15:16:29 +0200 | [diff] [blame] | 587 | test &= !!(filter & FILT_INVERT_ERRORS); |
Willy Tarreau | 72c2853 | 2009-01-22 18:56:50 +0100 | [diff] [blame] | 588 | } else { |
| 589 | val = strl2ui(b, 3); |
Willy Tarreau | 5bdfd96 | 2009-10-14 15:16:29 +0200 | [diff] [blame] | 590 | test &= (val >= 500 && val <= 599) ^ !!(filter & FILT_INVERT_ERRORS); |
Willy Tarreau | 72c2853 | 2009-01-22 18:56:50 +0100 | [diff] [blame] | 591 | } |
Willy Tarreau | 72c2853 | 2009-01-22 18:56:50 +0100 | [diff] [blame] | 592 | } |
| 593 | |
Willy Tarreau | 0f423a7 | 2010-05-03 10:50:54 +0200 | [diff] [blame] | 594 | test ^= filter_invert; |
| 595 | if (!test) |
| 596 | continue; |
| 597 | |
Willy Tarreau | 2651ac3 | 2010-05-05 12:20:19 +0200 | [diff] [blame] | 598 | if (unlikely(filter & (FILT_ACC_COUNT|FILT_ACC_DELAY))) { |
Willy Tarreau | 26deaf5 | 2011-07-10 19:47:48 +0200 | [diff] [blame^] | 599 | b = accept_field; |
Willy Tarreau | 214c203 | 2009-02-20 11:02:32 +0100 | [diff] [blame] | 600 | tot++; |
Willy Tarreau | 72c2853 | 2009-01-22 18:56:50 +0100 | [diff] [blame] | 601 | val = convert_date(b); |
| 602 | //printf("date=%s => %d\n", b, val); |
| 603 | if (val < 0) { |
| 604 | parse_err++; |
| 605 | continue; |
| 606 | } |
| 607 | |
| 608 | t2 = insert_value(&timers[0], &t, val); |
| 609 | t2->count++; |
| 610 | continue; |
| 611 | } |
| 612 | |
Willy Tarreau | 2651ac3 | 2010-05-05 12:20:19 +0200 | [diff] [blame] | 613 | if (unlikely(filter & (FILT_GRAPH_TIMERS|FILT_PERCENTILE))) { |
Willy Tarreau | 72c2853 | 2009-01-22 18:56:50 +0100 | [diff] [blame] | 614 | int f; |
| 615 | |
Willy Tarreau | 26deaf5 | 2011-07-10 19:47:48 +0200 | [diff] [blame^] | 616 | if (unlikely(!time_field)) { |
| 617 | time_field = field_start(accept_field, TIME_FIELD - ACCEPT_FIELD + 1); |
| 618 | if (!*time_field) { |
| 619 | truncated_line(linenum, line); |
| 620 | continue; |
| 621 | } |
Willy Tarreau | 758a6ea | 2011-07-10 18:53:44 +0200 | [diff] [blame] | 622 | } |
Willy Tarreau | 72c2853 | 2009-01-22 18:56:50 +0100 | [diff] [blame] | 623 | |
Willy Tarreau | 758a6ea | 2011-07-10 18:53:44 +0200 | [diff] [blame] | 624 | e = field_stop(time_field + 1); |
| 625 | /* we have field TIME_FIELD in [time_field]..[e-1] */ |
Willy Tarreau | 72c2853 | 2009-01-22 18:56:50 +0100 | [diff] [blame] | 626 | |
Willy Tarreau | 758a6ea | 2011-07-10 18:53:44 +0200 | [diff] [blame] | 627 | p = time_field; |
Willy Tarreau | 72c2853 | 2009-01-22 18:56:50 +0100 | [diff] [blame] | 628 | err = 0; |
Willy Tarreau | 24bcb4f | 2010-10-28 20:39:50 +0200 | [diff] [blame] | 629 | f = 0; |
Willy Tarreau | df6f0d1 | 2011-07-10 18:15:08 +0200 | [diff] [blame] | 630 | while (!SEP(*p)) { |
Willy Tarreau | 72c2853 | 2009-01-22 18:56:50 +0100 | [diff] [blame] | 631 | array[f] = str2ic(p); |
| 632 | if (array[f] < 0) { |
| 633 | array[f] = -1; |
| 634 | err = 1; |
| 635 | } |
Willy Tarreau | 24bcb4f | 2010-10-28 20:39:50 +0200 | [diff] [blame] | 636 | if (++f == 5) |
| 637 | break; |
Willy Tarreau | 72c2853 | 2009-01-22 18:56:50 +0100 | [diff] [blame] | 638 | SKIP_CHAR(p, '/'); |
| 639 | } |
| 640 | |
| 641 | if (f < 5) { |
| 642 | parse_err++; |
| 643 | continue; |
| 644 | } |
| 645 | |
| 646 | /* if we find at least one negative time, we count one error |
| 647 | * with a time equal to the total session time. This will |
| 648 | * emphasize quantum timing effects associated to known |
| 649 | * timeouts. Note that on some buggy machines, it is possible |
| 650 | * that the total time is negative, hence the reason to reset |
| 651 | * it. |
| 652 | */ |
Willy Tarreau | 214c203 | 2009-02-20 11:02:32 +0100 | [diff] [blame] | 653 | |
| 654 | if (filter & FILT_GRAPH_TIMERS) { |
| 655 | if (err) { |
| 656 | if (array[4] < 0) |
| 657 | array[4] = -1; |
| 658 | t2 = insert_timer(&timers[0], &t, array[4]); // total time |
| 659 | t2->count++; |
| 660 | } else { |
| 661 | int v; |
Willy Tarreau | 72c2853 | 2009-01-22 18:56:50 +0100 | [diff] [blame] | 662 | |
Willy Tarreau | 214c203 | 2009-02-20 11:02:32 +0100 | [diff] [blame] | 663 | t2 = insert_timer(&timers[1], &t, array[0]); t2->count++; // req |
| 664 | t2 = insert_timer(&timers[2], &t, array[2]); t2->count++; // conn |
| 665 | t2 = insert_timer(&timers[3], &t, array[3]); t2->count++; // resp |
| 666 | |
| 667 | v = array[4] - array[0] - array[1] - array[2] - array[3]; // data time |
| 668 | if (v < 0 && !(filter & FILT_QUIET)) |
| 669 | fprintf(stderr, "ERR: %s (%d %d %d %d %d => %d)\n", |
| 670 | line, array[0], array[1], array[2], array[3], array[4], v); |
| 671 | t2 = insert_timer(&timers[4], &t, v); t2->count++; |
| 672 | tot++; |
| 673 | } |
| 674 | } else { /* percentile */ |
| 675 | if (err) { |
| 676 | if (array[4] < 0) |
| 677 | array[4] = -1; |
| 678 | t2 = insert_value(&timers[0], &t, array[4]); // total time |
| 679 | t2->count++; |
| 680 | } else { |
| 681 | int v; |
Willy Tarreau | 72c2853 | 2009-01-22 18:56:50 +0100 | [diff] [blame] | 682 | |
Willy Tarreau | 214c203 | 2009-02-20 11:02:32 +0100 | [diff] [blame] | 683 | t2 = insert_value(&timers[1], &t, array[0]); t2->count++; // req |
| 684 | t2 = insert_value(&timers[2], &t, array[2]); t2->count++; // conn |
| 685 | t2 = insert_value(&timers[3], &t, array[3]); t2->count++; // resp |
| 686 | |
| 687 | v = array[4] - array[0] - array[1] - array[2] - array[3]; // data time |
| 688 | if (v < 0 && !(filter & FILT_QUIET)) |
| 689 | fprintf(stderr, "ERR: %s (%d %d %d %d %d => %d)\n", |
| 690 | line, array[0], array[1], array[2], array[3], array[4], v); |
| 691 | t2 = insert_value(&timers[4], &t, v); t2->count++; |
| 692 | tot++; |
| 693 | } |
Willy Tarreau | 72c2853 | 2009-01-22 18:56:50 +0100 | [diff] [blame] | 694 | } |
| 695 | continue; |
| 696 | } |
| 697 | |
Willy Tarreau | 2651ac3 | 2010-05-05 12:20:19 +0200 | [diff] [blame] | 698 | if (unlikely(filter & FILT_COUNT_STATUS)) { |
Willy Tarreau | 758a6ea | 2011-07-10 18:53:44 +0200 | [diff] [blame] | 699 | if (time_field) |
| 700 | b = field_start(time_field, STATUS_FIELD - TIME_FIELD + 1); |
| 701 | else |
Willy Tarreau | 26deaf5 | 2011-07-10 19:47:48 +0200 | [diff] [blame^] | 702 | b = field_start(accept_field, STATUS_FIELD - ACCEPT_FIELD + 1); |
| 703 | |
Willy Tarreau | 0f423a7 | 2010-05-03 10:50:54 +0200 | [diff] [blame] | 704 | if (!*b) { |
| 705 | truncated_line(linenum, line); |
| 706 | continue; |
| 707 | } |
| 708 | val = str2ic(b); |
| 709 | |
| 710 | t2 = insert_value(&timers[0], &t, val); |
| 711 | t2->count++; |
Willy Tarreau | 5bdfd96 | 2009-10-14 15:16:29 +0200 | [diff] [blame] | 712 | continue; |
Willy Tarreau | 0f423a7 | 2010-05-03 10:50:54 +0200 | [diff] [blame] | 713 | } |
Willy Tarreau | 5bdfd96 | 2009-10-14 15:16:29 +0200 | [diff] [blame] | 714 | |
Willy Tarreau | d8fc110 | 2010-09-12 17:56:16 +0200 | [diff] [blame] | 715 | if (unlikely(filter & FILT_COUNT_TERM_CODES)) { |
Willy Tarreau | 758a6ea | 2011-07-10 18:53:44 +0200 | [diff] [blame] | 716 | if (time_field) |
| 717 | b = field_start(time_field, TERM_CODES_FIELD - TIME_FIELD + 1); |
| 718 | else |
Willy Tarreau | 26deaf5 | 2011-07-10 19:47:48 +0200 | [diff] [blame^] | 719 | b = field_start(accept_field, TERM_CODES_FIELD - ACCEPT_FIELD + 1); |
| 720 | |
Willy Tarreau | d8fc110 | 2010-09-12 17:56:16 +0200 | [diff] [blame] | 721 | if (!*b) { |
| 722 | truncated_line(linenum, line); |
| 723 | continue; |
| 724 | } |
| 725 | val = 256 * b[0] + b[1]; |
| 726 | |
| 727 | t2 = insert_value(&timers[0], &t, val); |
| 728 | t2->count++; |
| 729 | continue; |
| 730 | } |
| 731 | |
Willy Tarreau | d220106 | 2010-05-27 18:17:30 +0200 | [diff] [blame] | 732 | if (unlikely(filter & FILT_COUNT_SRV_STATUS)) { |
| 733 | char *srv_name; |
| 734 | struct ebmb_node *srv_node; |
| 735 | struct srv_st *srv; |
| 736 | |
Willy Tarreau | d220106 | 2010-05-27 18:17:30 +0200 | [diff] [blame] | 737 | /* the server field is before the status field, so let's |
| 738 | * parse them in the proper order. |
| 739 | */ |
Willy Tarreau | 26deaf5 | 2011-07-10 19:47:48 +0200 | [diff] [blame^] | 740 | b = field_start(accept_field, SERVER_FIELD - ACCEPT_FIELD + 1); |
Willy Tarreau | d220106 | 2010-05-27 18:17:30 +0200 | [diff] [blame] | 741 | if (!*b) { |
| 742 | truncated_line(linenum, line); |
| 743 | continue; |
| 744 | } |
| 745 | |
| 746 | e = field_stop(b + 1); /* we have the server name in [b]..[e-1] */ |
| 747 | |
| 748 | /* the chance that a server name already exists is extremely high, |
| 749 | * so let's perform a normal lookup first. |
| 750 | */ |
| 751 | srv_node = ebst_lookup_len(&timers[0], b, e - b); |
| 752 | srv = container_of(srv_node, struct srv_st, node); |
| 753 | |
| 754 | if (!srv_node) { |
| 755 | /* server not yet in the tree, let's create it */ |
| 756 | srv = (void *)calloc(1, sizeof(struct srv_st) + e - b + 1); |
| 757 | srv_node = &srv->node; |
| 758 | memcpy(&srv_node->key, b, e - b); |
| 759 | srv_node->key[e - b] = '\0'; |
| 760 | ebst_insert(&timers[0], srv_node); |
| 761 | } |
| 762 | |
| 763 | /* let's collect the connect and response times */ |
Willy Tarreau | 758a6ea | 2011-07-10 18:53:44 +0200 | [diff] [blame] | 764 | if (!time_field) |
| 765 | time_field = field_start(e, TIME_FIELD - SERVER_FIELD); |
| 766 | if (!*time_field) { |
Willy Tarreau | d220106 | 2010-05-27 18:17:30 +0200 | [diff] [blame] | 767 | truncated_line(linenum, line); |
| 768 | continue; |
| 769 | } |
| 770 | |
Willy Tarreau | 758a6ea | 2011-07-10 18:53:44 +0200 | [diff] [blame] | 771 | e = field_stop(time_field + 1); |
| 772 | /* we have field TIME_FIELD in [time_field]..[e-1] */ |
Willy Tarreau | d220106 | 2010-05-27 18:17:30 +0200 | [diff] [blame] | 773 | |
Willy Tarreau | 758a6ea | 2011-07-10 18:53:44 +0200 | [diff] [blame] | 774 | p = time_field; |
Willy Tarreau | d220106 | 2010-05-27 18:17:30 +0200 | [diff] [blame] | 775 | err = 0; |
Willy Tarreau | 24bcb4f | 2010-10-28 20:39:50 +0200 | [diff] [blame] | 776 | f = 0; |
Willy Tarreau | df6f0d1 | 2011-07-10 18:15:08 +0200 | [diff] [blame] | 777 | while (!SEP(*p)) { |
Willy Tarreau | d220106 | 2010-05-27 18:17:30 +0200 | [diff] [blame] | 778 | array[f] = str2ic(p); |
| 779 | if (array[f] < 0) { |
| 780 | array[f] = -1; |
| 781 | err = 1; |
| 782 | } |
Willy Tarreau | 24bcb4f | 2010-10-28 20:39:50 +0200 | [diff] [blame] | 783 | if (++f == 5) |
| 784 | break; |
Willy Tarreau | d220106 | 2010-05-27 18:17:30 +0200 | [diff] [blame] | 785 | SKIP_CHAR(p, '/'); |
| 786 | } |
| 787 | |
| 788 | if (f < 5) { |
| 789 | parse_err++; |
| 790 | continue; |
| 791 | } |
| 792 | |
| 793 | /* OK we have our timers in array[2,3] */ |
| 794 | if (!err) |
| 795 | srv->nb_ok++; |
| 796 | |
| 797 | if (array[2] >= 0) { |
| 798 | srv->cum_ct += array[2]; |
| 799 | srv->nb_ct++; |
| 800 | } |
| 801 | |
| 802 | if (array[3] >= 0) { |
| 803 | srv->cum_rt += array[3]; |
| 804 | srv->nb_rt++; |
| 805 | } |
| 806 | |
| 807 | /* we're interested in the 5 HTTP status classes (1xx ... 5xx), and |
| 808 | * the invalid ones which will be reported as 0. |
| 809 | */ |
| 810 | b = field_start(e, STATUS_FIELD - TIME_FIELD); |
| 811 | if (!*b) { |
| 812 | truncated_line(linenum, line); |
| 813 | continue; |
| 814 | } |
| 815 | |
| 816 | val = 0; |
| 817 | if (*b >= '1' && *b <= '5') |
| 818 | val = *b - '0'; |
| 819 | |
| 820 | srv->st_cnt[val]++; |
| 821 | continue; |
| 822 | } |
| 823 | |
Willy Tarreau | abe45b6 | 2010-10-28 20:33:46 +0200 | [diff] [blame] | 824 | if (unlikely(filter & FILT_COUNT_URL_ANY)) { |
Willy Tarreau | abe45b6 | 2010-10-28 20:33:46 +0200 | [diff] [blame] | 825 | /* let's collect the response time */ |
Willy Tarreau | 26deaf5 | 2011-07-10 19:47:48 +0200 | [diff] [blame^] | 826 | if (unlikely(!time_field)) { |
| 827 | time_field = field_start(accept_field, TIME_FIELD - ACCEPT_FIELD + 1); // avg 115 ns per line |
| 828 | if (!*time_field) { |
| 829 | truncated_line(linenum, line); |
Willy Tarreau | 758a6ea | 2011-07-10 18:53:44 +0200 | [diff] [blame] | 830 | continue; |
| 831 | } |
Willy Tarreau | abe45b6 | 2010-10-28 20:33:46 +0200 | [diff] [blame] | 832 | } |
| 833 | |
Willy Tarreau | 758a6ea | 2011-07-10 18:53:44 +0200 | [diff] [blame] | 834 | /* we have the field TIME_FIELD starting at <time_field>. We'll |
Willy Tarreau | abe45b6 | 2010-10-28 20:33:46 +0200 | [diff] [blame] | 835 | * parse the 5 timers to detect errors, it takes avg 55 ns per line. |
| 836 | */ |
Willy Tarreau | 758a6ea | 2011-07-10 18:53:44 +0200 | [diff] [blame] | 837 | e = time_field; err = 0; f = 0; |
Willy Tarreau | df6f0d1 | 2011-07-10 18:15:08 +0200 | [diff] [blame] | 838 | while (!SEP(*e)) { |
Willy Tarreau | abe45b6 | 2010-10-28 20:33:46 +0200 | [diff] [blame] | 839 | array[f] = str2ic(e); |
| 840 | if (array[f] < 0) { |
| 841 | array[f] = -1; |
| 842 | err = 1; |
| 843 | } |
| 844 | if (++f == 5) |
| 845 | break; |
| 846 | SKIP_CHAR(e, '/'); |
| 847 | } |
| 848 | if (f < 5) { |
| 849 | parse_err++; |
| 850 | continue; |
| 851 | } |
| 852 | |
| 853 | /* OK we have our timers in array[3], and err is >0 if at |
| 854 | * least one -1 was seen. <e> points to the first char of |
| 855 | * the last timer. Let's prepare a new node with that. |
| 856 | */ |
| 857 | if (unlikely(!ustat)) |
| 858 | ustat = calloc(1, sizeof(*ustat)); |
| 859 | |
| 860 | ustat->nb_err = err; |
| 861 | ustat->nb_req = 1; |
| 862 | |
| 863 | /* use array[4] = total time in case of error */ |
| 864 | ustat->total_time = (array[3] >= 0) ? array[3] : array[4]; |
| 865 | ustat->total_time_ok = (array[3] >= 0) ? array[3] : 0; |
| 866 | |
| 867 | /* the line may be truncated because of a bad request or anything like this, |
| 868 | * without a method. Also, if it does not begin with an quote, let's skip to |
| 869 | * the next field because it's a capture. Let's fall back to the "method" itself |
| 870 | * if there's nothing else. |
| 871 | */ |
| 872 | e = field_start(e, METH_FIELD - TIME_FIELD + 1); // avg 100 ns per line |
| 873 | while (*e != '"' && *e) |
| 874 | e = field_start(e, 2); |
| 875 | |
| 876 | if (!*e) { |
| 877 | truncated_line(linenum, line); |
| 878 | continue; |
| 879 | } |
| 880 | |
| 881 | b = field_start(e, URL_FIELD - METH_FIELD + 1); // avg 40 ns per line |
| 882 | if (!*b) |
| 883 | b = e; |
| 884 | |
| 885 | /* stop at end of field or first ';' or '?', takes avg 64 ns per line */ |
| 886 | e = b; |
| 887 | do { |
| 888 | if (*e == ' ' || *e == '?' || *e == ';' || *e == '\t') { |
| 889 | *(char *)e = 0; |
| 890 | break; |
| 891 | } |
| 892 | e++; |
| 893 | } while (*e); |
| 894 | |
| 895 | /* now instead of copying the URL for a simple lookup, we'll link |
| 896 | * to it from the node we're trying to insert. If it returns a |
| 897 | * different value, it was already there. Otherwise we just have |
| 898 | * to dynamically realloc an entry using strdup(). |
| 899 | */ |
| 900 | ustat->node.url.key = (char *)b; |
| 901 | ebpt_old = ebis_insert(&timers[0], &ustat->node.url); |
| 902 | |
| 903 | if (ebpt_old != &ustat->node.url) { |
| 904 | struct url_stat *ustat_old; |
| 905 | /* node was already there, let's update previous one */ |
| 906 | ustat_old = container_of(ebpt_old, struct url_stat, node.url); |
| 907 | ustat_old->nb_req ++; |
| 908 | ustat_old->nb_err += ustat->nb_err; |
| 909 | ustat_old->total_time += ustat->total_time; |
| 910 | ustat_old->total_time_ok += ustat->total_time_ok; |
| 911 | } else { |
| 912 | ustat->url = ustat->node.url.key = strdup(ustat->node.url.key); |
| 913 | ustat = NULL; /* node was used */ |
| 914 | } |
| 915 | |
| 916 | continue; |
| 917 | } |
| 918 | |
Willy Tarreau | 72c2853 | 2009-01-22 18:56:50 +0100 | [diff] [blame] | 919 | /* all other cases mean we just want to count lines */ |
| 920 | tot++; |
Willy Tarreau | 2651ac3 | 2010-05-05 12:20:19 +0200 | [diff] [blame] | 921 | if (unlikely(!(filter & FILT_COUNT_ONLY))) |
Willy Tarreau | 5bdfd96 | 2009-10-14 15:16:29 +0200 | [diff] [blame] | 922 | puts(line); |
Willy Tarreau | 72c2853 | 2009-01-22 18:56:50 +0100 | [diff] [blame] | 923 | } |
| 924 | |
| 925 | if (t) |
| 926 | free(t); |
| 927 | |
| 928 | if (filter & FILT_COUNT_ONLY) { |
| 929 | printf("%d\n", tot); |
| 930 | exit(0); |
| 931 | } |
| 932 | |
Willy Tarreau | 72c2853 | 2009-01-22 18:56:50 +0100 | [diff] [blame] | 933 | if (filter & (FILT_ACC_COUNT|FILT_ACC_DELAY)) { |
| 934 | /* sort and count all timers. Output will look like this : |
| 935 | * <accept_date> <delta_ms from previous one> <nb entries> |
| 936 | */ |
| 937 | n = eb32_first(&timers[0]); |
| 938 | |
| 939 | if (n) |
| 940 | last = n->key; |
| 941 | while (n) { |
| 942 | unsigned int d, h, m, s, ms; |
| 943 | |
| 944 | t = container_of(n, struct timer, node); |
| 945 | h = n->key; |
| 946 | d = h - last; |
| 947 | last = h; |
| 948 | |
| 949 | if (d >= filter_acc_delay && t->count >= filter_acc_count) { |
| 950 | ms = h % 1000; h = h / 1000; |
| 951 | s = h % 60; h = h / 60; |
| 952 | m = h % 60; h = h / 60; |
| 953 | tot++; |
| 954 | printf("%02d:%02d:%02d.%03d %d %d %d\n", h, m, s, ms, last, d, t->count); |
| 955 | } |
| 956 | n = eb32_next(n); |
| 957 | } |
| 958 | } |
| 959 | else if (filter & FILT_GRAPH_TIMERS) { |
| 960 | /* sort all timers */ |
| 961 | for (f = 0; f < 5; f++) { |
| 962 | struct eb32_node *n; |
| 963 | int val; |
| 964 | |
| 965 | val = 0; |
| 966 | n = eb32_first(&timers[f]); |
| 967 | while (n) { |
| 968 | int i; |
| 969 | double d; |
| 970 | |
| 971 | t = container_of(n, struct timer, node); |
| 972 | last = n->key; |
| 973 | val = t->count; |
| 974 | |
| 975 | i = (last < 0) ? -last : last; |
| 976 | i = fls_auto(i) - QBITS; |
| 977 | |
| 978 | if (i > 0) |
| 979 | d = val / (double)(1 << i); |
| 980 | else |
| 981 | d = val; |
| 982 | |
| 983 | if (d > 0.0) { |
| 984 | printf("%d %d %f\n", f, last, d+1.0); |
| 985 | tot++; |
| 986 | } |
| 987 | |
| 988 | n = eb32_next(n); |
| 989 | } |
Willy Tarreau | 214c203 | 2009-02-20 11:02:32 +0100 | [diff] [blame] | 990 | } |
| 991 | } |
| 992 | else if (filter & FILT_PERCENTILE) { |
| 993 | /* report timers by percentile : |
| 994 | * <percent> <total> <max_req_time> <max_conn_time> <max_resp_time> <max_data_time> |
| 995 | * We don't count errs. |
| 996 | */ |
| 997 | struct eb32_node *n[5]; |
| 998 | unsigned long cum[5]; |
| 999 | double step; |
| 1000 | |
Willy Tarreau | 910ba4b | 2009-11-17 10:16:19 +0100 | [diff] [blame] | 1001 | if (!tot) |
| 1002 | goto empty; |
| 1003 | |
Willy Tarreau | 214c203 | 2009-02-20 11:02:32 +0100 | [diff] [blame] | 1004 | for (f = 1; f < 5; f++) { |
| 1005 | n[f] = eb32_first(&timers[f]); |
| 1006 | cum[f] = container_of(n[f], struct timer, node)->count; |
| 1007 | } |
| 1008 | |
| 1009 | for (step = 1; step <= 1000;) { |
| 1010 | unsigned int thres = tot * (step / 1000.0); |
| 1011 | |
| 1012 | printf("%3.1f %d ", step/10.0, thres); |
| 1013 | for (f = 1; f < 5; f++) { |
| 1014 | struct eb32_node *next; |
| 1015 | while (cum[f] < thres) { |
| 1016 | /* need to find other keys */ |
| 1017 | next = eb32_next(n[f]); |
| 1018 | if (!next) |
| 1019 | break; |
| 1020 | n[f] = next; |
| 1021 | cum[f] += container_of(next, struct timer, node)->count; |
| 1022 | } |
| 1023 | |
| 1024 | /* value still within $step % of total */ |
| 1025 | printf("%d ", n[f]->key); |
| 1026 | } |
| 1027 | putchar('\n'); |
| 1028 | if (step >= 100 && step < 900) |
| 1029 | step += 50; // jump 5% by 5% between those steps. |
| 1030 | else if (step >= 20 && step < 980) |
| 1031 | step += 10; |
| 1032 | else |
| 1033 | step += 1; |
Willy Tarreau | 72c2853 | 2009-01-22 18:56:50 +0100 | [diff] [blame] | 1034 | } |
| 1035 | } |
Willy Tarreau | 0f423a7 | 2010-05-03 10:50:54 +0200 | [diff] [blame] | 1036 | else if (filter & FILT_COUNT_STATUS) { |
| 1037 | /* output all statuses in the form of <status> <occurrences> */ |
| 1038 | n = eb32_first(&timers[0]); |
| 1039 | while (n) { |
| 1040 | t = container_of(n, struct timer, node); |
| 1041 | printf("%d %d\n", n->key, t->count); |
| 1042 | n = eb32_next(n); |
| 1043 | } |
| 1044 | } |
Willy Tarreau | d220106 | 2010-05-27 18:17:30 +0200 | [diff] [blame] | 1045 | else if (unlikely(filter & FILT_COUNT_SRV_STATUS)) { |
| 1046 | char *srv_name; |
| 1047 | struct ebmb_node *srv_node; |
| 1048 | struct srv_st *srv; |
| 1049 | |
| 1050 | printf("#srv_name 1xx 2xx 3xx 4xx 5xx other tot_req req_ok pct_ok avg_ct avg_rt\n"); |
| 1051 | |
| 1052 | srv_node = ebmb_first(&timers[0]); |
| 1053 | while (srv_node) { |
| 1054 | int tot_rq; |
| 1055 | |
| 1056 | srv = container_of(srv_node, struct srv_st, node); |
| 1057 | |
| 1058 | tot_rq = 0; |
| 1059 | for (f = 0; f <= 5; f++) |
| 1060 | tot_rq += srv->st_cnt[f]; |
| 1061 | |
| 1062 | printf("%s %d %d %d %d %d %d %d %d %.1f %d %d\n", |
| 1063 | srv_node->key, srv->st_cnt[1], srv->st_cnt[2], |
| 1064 | srv->st_cnt[3], srv->st_cnt[4], srv->st_cnt[5], srv->st_cnt[0], |
| 1065 | tot_rq, |
| 1066 | srv->nb_ok, (double)srv->nb_ok * 100.0 / (tot_rq?tot_rq:1), |
| 1067 | (int)(srv->cum_ct / (srv->nb_ct?srv->nb_ct:1)), (int)(srv->cum_rt / (srv->nb_rt?srv->nb_rt:1))); |
| 1068 | srv_node = ebmb_next(srv_node); |
| 1069 | tot++; |
| 1070 | } |
| 1071 | } |
Willy Tarreau | d8fc110 | 2010-09-12 17:56:16 +0200 | [diff] [blame] | 1072 | else if (filter & FILT_COUNT_TERM_CODES) { |
| 1073 | /* output all statuses in the form of <code> <occurrences> */ |
| 1074 | n = eb32_first(&timers[0]); |
| 1075 | while (n) { |
| 1076 | t = container_of(n, struct timer, node); |
| 1077 | printf("%c%c %d\n", (n->key >> 8), (n->key) & 255, t->count); |
| 1078 | n = eb32_next(n); |
| 1079 | } |
| 1080 | } |
Willy Tarreau | abe45b6 | 2010-10-28 20:33:46 +0200 | [diff] [blame] | 1081 | else if (unlikely(filter & FILT_COUNT_URL_ANY)) { |
| 1082 | char *srv_name; |
| 1083 | struct eb_node *node, *next; |
| 1084 | |
| 1085 | if (!(filter & FILT_COUNT_URL_ONLY)) { |
| 1086 | /* we have to sort on another criterion. We'll use timers[1] for the |
| 1087 | * destination tree. |
| 1088 | */ |
| 1089 | |
| 1090 | timers[1] = EB_ROOT; /* reconfigure to accept duplicates */ |
| 1091 | for (node = eb_first(&timers[0]); node; node = next) { |
| 1092 | next = eb_next(node); |
| 1093 | eb_delete(node); |
| 1094 | |
| 1095 | ustat = container_of(node, struct url_stat, node.url.node); |
| 1096 | |
| 1097 | if (filter & FILT_COUNT_URL_COUNT) |
| 1098 | ustat->node.val.key = ustat->nb_req; |
| 1099 | else if (filter & FILT_COUNT_URL_ERR) |
| 1100 | ustat->node.val.key = ustat->nb_err; |
| 1101 | else if (filter & FILT_COUNT_URL_TTOT) |
| 1102 | ustat->node.val.key = ustat->total_time; |
| 1103 | else if (filter & FILT_COUNT_URL_TAVG) |
| 1104 | ustat->node.val.key = ustat->nb_req ? ustat->total_time / ustat->nb_req : 0; |
| 1105 | else if (filter & FILT_COUNT_URL_TTOTO) |
| 1106 | ustat->node.val.key = ustat->total_time_ok; |
| 1107 | else if (filter & FILT_COUNT_URL_TAVGO) |
| 1108 | ustat->node.val.key = (ustat->nb_req - ustat->nb_err) ? ustat->total_time_ok / (ustat->nb_req - ustat->nb_err) : 0; |
| 1109 | else |
| 1110 | ustat->node.val.key = 0; |
| 1111 | |
| 1112 | eb64_insert(&timers[1], &ustat->node.val); |
| 1113 | } |
| 1114 | /* switch trees */ |
| 1115 | timers[0] = timers[1]; |
| 1116 | } |
| 1117 | |
| 1118 | printf("#req err ttot tavg oktot okavg url\n"); |
| 1119 | |
| 1120 | /* scan the tree in its reverse sorting order */ |
| 1121 | node = eb_last(&timers[0]); |
| 1122 | while (node) { |
| 1123 | ustat = container_of(node, struct url_stat, node.url.node); |
| 1124 | printf("%d %d %Ld %Ld %Ld %Ld %s\n", |
| 1125 | ustat->nb_req, |
| 1126 | ustat->nb_err, |
| 1127 | ustat->total_time, |
| 1128 | ustat->nb_req ? ustat->total_time / ustat->nb_req : 0, |
| 1129 | ustat->total_time_ok, |
| 1130 | (ustat->nb_req - ustat->nb_err) ? ustat->total_time_ok / (ustat->nb_req - ustat->nb_err) : 0, |
| 1131 | ustat->url); |
| 1132 | |
| 1133 | node = eb_prev(node); |
| 1134 | tot++; |
| 1135 | } |
| 1136 | } |
Willy Tarreau | d220106 | 2010-05-27 18:17:30 +0200 | [diff] [blame] | 1137 | |
Willy Tarreau | 910ba4b | 2009-11-17 10:16:19 +0100 | [diff] [blame] | 1138 | empty: |
Willy Tarreau | 72c2853 | 2009-01-22 18:56:50 +0100 | [diff] [blame] | 1139 | if (!(filter & FILT_QUIET)) |
| 1140 | fprintf(stderr, "%d lines in, %d lines out, %d parsing errors\n", |
| 1141 | linenum, tot, parse_err); |
| 1142 | exit(0); |
| 1143 | } |
| 1144 | |
| 1145 | /* |
| 1146 | * Local variables: |
| 1147 | * c-indent-level: 8 |
| 1148 | * c-basic-offset: 8 |
| 1149 | * End: |
| 1150 | */ |