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