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 | 8a09b66 | 2012-10-10 10:26:22 +0200 | [diff] [blame] | 4 | * Copyright 2000-2012 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 |
Baptiste | 61aaad0 | 2012-09-08 23:10:03 +0200 | [diff] [blame] | 32 | #define BYTES_SENT_FIELD 11 |
Willy Tarreau | d8fc110 | 2010-09-12 17:56:16 +0200 | [diff] [blame] | 33 | #define TERM_CODES_FIELD 14 |
Willy Tarreau | 72c2853 | 2009-01-22 18:56:50 +0100 | [diff] [blame] | 34 | #define CONN_FIELD 15 |
Willy Tarreau | 08911ff | 2011-10-13 13:28:36 +0200 | [diff] [blame] | 35 | #define QUEUE_LEN_FIELD 16 |
Willy Tarreau | abe45b6 | 2010-10-28 20:33:46 +0200 | [diff] [blame] | 36 | #define METH_FIELD 17 |
| 37 | #define URL_FIELD 18 |
Willy Tarreau | 72c2853 | 2009-01-22 18:56:50 +0100 | [diff] [blame] | 38 | #define MAXLINE 16384 |
| 39 | #define QBITS 4 |
| 40 | |
Willy Tarreau | df6f0d1 | 2011-07-10 18:15:08 +0200 | [diff] [blame] | 41 | #define SEP(c) ((unsigned char)(c) <= ' ') |
| 42 | #define SKIP_CHAR(p,c) do { while (1) { int __c = (unsigned char)*p++; if (__c == c) break; if (__c <= ' ') { p--; break; } } } while (0) |
Willy Tarreau | 72c2853 | 2009-01-22 18:56:50 +0100 | [diff] [blame] | 43 | |
| 44 | /* [0] = err/date, [1] = req, [2] = conn, [3] = resp, [4] = data */ |
| 45 | static struct eb_root timers[5] = { |
| 46 | EB_ROOT_UNIQUE, EB_ROOT_UNIQUE, EB_ROOT_UNIQUE, |
| 47 | EB_ROOT_UNIQUE, EB_ROOT_UNIQUE, |
| 48 | }; |
| 49 | |
| 50 | struct timer { |
| 51 | struct eb32_node node; |
| 52 | unsigned int count; |
| 53 | }; |
| 54 | |
Willy Tarreau | d220106 | 2010-05-27 18:17:30 +0200 | [diff] [blame] | 55 | struct srv_st { |
| 56 | unsigned int st_cnt[6]; /* 0xx to 5xx */ |
| 57 | unsigned int nb_ct, nb_rt, nb_ok; |
| 58 | unsigned long long cum_ct, cum_rt; |
| 59 | struct ebmb_node node; |
| 60 | /* don't put anything else here, the server name will be there */ |
| 61 | }; |
Willy Tarreau | 72c2853 | 2009-01-22 18:56:50 +0100 | [diff] [blame] | 62 | |
Willy Tarreau | abe45b6 | 2010-10-28 20:33:46 +0200 | [diff] [blame] | 63 | struct url_stat { |
| 64 | union { |
| 65 | struct ebpt_node url; |
| 66 | struct eb64_node val; |
| 67 | } node; |
| 68 | char *url; |
| 69 | unsigned long long total_time; /* sum(all reqs' times) */ |
| 70 | unsigned long long total_time_ok; /* sum(all OK reqs' times) */ |
Baptiste | 61aaad0 | 2012-09-08 23:10:03 +0200 | [diff] [blame] | 71 | unsigned long long total_bytes_sent; /* sum(all bytes sent) */ |
Willy Tarreau | abe45b6 | 2010-10-28 20:33:46 +0200 | [diff] [blame] | 72 | unsigned int nb_err, nb_req; |
| 73 | }; |
| 74 | |
Willy Tarreau | 72c2853 | 2009-01-22 18:56:50 +0100 | [diff] [blame] | 75 | #define FILT_COUNT_ONLY 0x01 |
| 76 | #define FILT_INVERT 0x02 |
| 77 | #define FILT_QUIET 0x04 |
| 78 | #define FILT_ERRORS_ONLY 0x08 |
| 79 | #define FILT_ACC_DELAY 0x10 |
| 80 | #define FILT_ACC_COUNT 0x20 |
| 81 | #define FILT_GRAPH_TIMERS 0x40 |
Willy Tarreau | 214c203 | 2009-02-20 11:02:32 +0100 | [diff] [blame] | 82 | #define FILT_PERCENTILE 0x80 |
Willy Tarreau | 5bdfd96 | 2009-10-14 15:16:29 +0200 | [diff] [blame] | 83 | #define FILT_TIME_RESP 0x100 |
| 84 | |
| 85 | #define FILT_INVERT_ERRORS 0x200 |
| 86 | #define FILT_INVERT_TIME_RESP 0x400 |
Willy Tarreau | 72c2853 | 2009-01-22 18:56:50 +0100 | [diff] [blame] | 87 | |
Willy Tarreau | 0f423a7 | 2010-05-03 10:50:54 +0200 | [diff] [blame] | 88 | #define FILT_COUNT_STATUS 0x800 |
Willy Tarreau | d220106 | 2010-05-27 18:17:30 +0200 | [diff] [blame] | 89 | #define FILT_COUNT_SRV_STATUS 0x1000 |
Willy Tarreau | d8fc110 | 2010-09-12 17:56:16 +0200 | [diff] [blame] | 90 | #define FILT_COUNT_TERM_CODES 0x2000 |
Willy Tarreau | 0f423a7 | 2010-05-03 10:50:54 +0200 | [diff] [blame] | 91 | |
Willy Tarreau | abe45b6 | 2010-10-28 20:33:46 +0200 | [diff] [blame] | 92 | #define FILT_COUNT_URL_ONLY 0x004000 |
| 93 | #define FILT_COUNT_URL_COUNT 0x008000 |
| 94 | #define FILT_COUNT_URL_ERR 0x010000 |
| 95 | #define FILT_COUNT_URL_TTOT 0x020000 |
| 96 | #define FILT_COUNT_URL_TAVG 0x040000 |
| 97 | #define FILT_COUNT_URL_TTOTO 0x080000 |
| 98 | #define FILT_COUNT_URL_TAVGO 0x100000 |
Willy Tarreau | abe45b6 | 2010-10-28 20:33:46 +0200 | [diff] [blame] | 99 | |
Willy Tarreau | 70c428f | 2011-07-10 17:27:40 +0200 | [diff] [blame] | 100 | #define FILT_HTTP_ONLY 0x200000 |
Willy Tarreau | d3007ff | 2011-09-05 02:07:23 +0200 | [diff] [blame] | 101 | #define FILT_TERM_CODE_NAME 0x400000 |
Hervé COMMOWICK | 927cddd | 2011-08-10 17:42:41 +0200 | [diff] [blame] | 102 | #define FILT_INVERT_TERM_CODE_NAME 0x800000 |
Willy Tarreau | 70c428f | 2011-07-10 17:27:40 +0200 | [diff] [blame] | 103 | |
Willy Tarreau | d3007ff | 2011-09-05 02:07:23 +0200 | [diff] [blame] | 104 | #define FILT_HTTP_STATUS 0x1000000 |
| 105 | #define FILT_INVERT_HTTP_STATUS 0x2000000 |
Willy Tarreau | 08911ff | 2011-10-13 13:28:36 +0200 | [diff] [blame] | 106 | #define FILT_QUEUE_ONLY 0x4000000 |
| 107 | #define FILT_QUEUE_SRV_ONLY 0x8000000 |
Willy Tarreau | d3007ff | 2011-09-05 02:07:23 +0200 | [diff] [blame] | 108 | |
Baptiste | 61aaad0 | 2012-09-08 23:10:03 +0200 | [diff] [blame] | 109 | #define FILT_COUNT_URL_BAVG 0x10000000 |
| 110 | #define FILT_COUNT_URL_BTOT 0x20000000 |
| 111 | |
| 112 | #define FILT_COUNT_URL_ANY (FILT_COUNT_URL_ONLY|FILT_COUNT_URL_COUNT|FILT_COUNT_URL_ERR| \ |
| 113 | FILT_COUNT_URL_TTOT|FILT_COUNT_URL_TAVG|FILT_COUNT_URL_TTOTO|FILT_COUNT_URL_TAVGO| \ |
| 114 | FILT_COUNT_URL_BAVG|FILT_COUNT_URL_BTOT) |
| 115 | |
Willy Tarreau | 8a09b66 | 2012-10-10 10:26:22 +0200 | [diff] [blame] | 116 | #define FILT_COUNT_COOK_CODES 0x40000000 |
| 117 | |
Willy Tarreau | 72c2853 | 2009-01-22 18:56:50 +0100 | [diff] [blame] | 118 | unsigned int filter = 0; |
| 119 | unsigned int filter_invert = 0; |
Willy Tarreau | 214c203 | 2009-02-20 11:02:32 +0100 | [diff] [blame] | 120 | const char *line; |
Willy Tarreau | a2b39fb | 2011-07-10 21:39:35 +0200 | [diff] [blame] | 121 | int linenum = 0; |
| 122 | int parse_err = 0; |
| 123 | int lines_out = 0; |
Willy Tarreau | 667c905 | 2012-10-10 16:49:28 +0200 | [diff] [blame] | 124 | int lines_max = -1; |
Willy Tarreau | 72c2853 | 2009-01-22 18:56:50 +0100 | [diff] [blame] | 125 | |
Willy Tarreau | 214c203 | 2009-02-20 11:02:32 +0100 | [diff] [blame] | 126 | const char *fgets2(FILE *stream); |
Willy Tarreau | 72c2853 | 2009-01-22 18:56:50 +0100 | [diff] [blame] | 127 | |
Willy Tarreau | a2b39fb | 2011-07-10 21:39:35 +0200 | [diff] [blame] | 128 | void filter_count_url(const char *accept_field, const char *time_field, struct timer **tptr); |
| 129 | void filter_count_srv_status(const char *accept_field, const char *time_field, struct timer **tptr); |
Willy Tarreau | 8a09b66 | 2012-10-10 10:26:22 +0200 | [diff] [blame] | 130 | void filter_count_cook_codes(const char *accept_field, const char *time_field, struct timer **tptr); |
Willy Tarreau | a2b39fb | 2011-07-10 21:39:35 +0200 | [diff] [blame] | 131 | void filter_count_term_codes(const char *accept_field, const char *time_field, struct timer **tptr); |
| 132 | void filter_count_status(const char *accept_field, const char *time_field, struct timer **tptr); |
| 133 | void filter_graphs(const char *accept_field, const char *time_field, struct timer **tptr); |
| 134 | void filter_output_line(const char *accept_field, const char *time_field, struct timer **tptr); |
| 135 | void filter_accept_holes(const char *accept_field, const char *time_field, struct timer **tptr); |
| 136 | |
Willy Tarreau | 615674c | 2012-01-23 08:15:51 +0100 | [diff] [blame] | 137 | void usage(FILE *output, const char *msg) |
Willy Tarreau | 72c2853 | 2009-01-22 18:56:50 +0100 | [diff] [blame] | 138 | { |
Willy Tarreau | 615674c | 2012-01-23 08:15:51 +0100 | [diff] [blame] | 139 | fprintf(output, |
Willy Tarreau | 72c2853 | 2009-01-22 18:56:50 +0100 | [diff] [blame] | 140 | "%s" |
Willy Tarreau | 615674c | 2012-01-23 08:15:51 +0100 | [diff] [blame] | 141 | "Usage: halog [-h|--help] for long help\n" |
Willy Tarreau | 667c905 | 2012-10-10 16:49:28 +0200 | [diff] [blame] | 142 | " halog [-q] [-c] [-m <lines>]\n" |
Willy Tarreau | 8a09b66 | 2012-10-10 10:26:22 +0200 | [diff] [blame] | 143 | " {-cc|-gt|-pct|-st|-tc|-srv|-u|-uc|-ue|-ua|-ut|-uao|-uto|-uba|-ubt}\n" |
Hervé COMMOWICK | 927cddd | 2011-08-10 17:42:41 +0200 | [diff] [blame] | 144 | " [-s <skip>] [-e|-E] [-H] [-rt|-RT <time>] [-ad <delay>] [-ac <count>]\n" |
Baptiste | 61aaad0 | 2012-09-08 23:10:03 +0200 | [diff] [blame] | 145 | " [-v] [-Q|-QS] [-tcn|-TCN <termcode>] [ -hs|-HS [min][:[max]] ] < log\n" |
Willy Tarreau | 72c2853 | 2009-01-22 18:56:50 +0100 | [diff] [blame] | 146 | "\n", |
| 147 | msg ? msg : "" |
| 148 | ); |
Willy Tarreau | 615674c | 2012-01-23 08:15:51 +0100 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | void die(const char *msg) |
| 152 | { |
| 153 | usage(stderr, msg); |
Willy Tarreau | 72c2853 | 2009-01-22 18:56:50 +0100 | [diff] [blame] | 154 | exit(1); |
| 155 | } |
| 156 | |
Willy Tarreau | 615674c | 2012-01-23 08:15:51 +0100 | [diff] [blame] | 157 | void help() |
| 158 | { |
| 159 | usage(stdout, NULL); |
| 160 | printf( |
| 161 | "Input filters (several filters may be combined) :\n" |
| 162 | " -H only match lines containing HTTP logs (ignore TCP)\n" |
| 163 | " -E only match lines without any error (no 5xx status)\n" |
| 164 | " -e only match lines with errors (status 5xx or negative)\n" |
| 165 | " -rt|-RT <time> only match response times larger|smaller than <time>\n" |
| 166 | " -Q|-QS only match queued requests (any queue|server queue)\n" |
| 167 | " -tcn|-TCN <code> only match requests with/without termination code <code>\n" |
| 168 | " -hs|-HS <[min][:][max]> only match requests with HTTP status codes within/not\n" |
| 169 | " within min..max. Any of them may be omitted. Exact\n" |
| 170 | " code is checked for if no ':' is specified.\n" |
| 171 | "Modifiers\n" |
| 172 | " -v invert the input filtering condition\n" |
| 173 | " -q don't report errors/warnings\n" |
Willy Tarreau | 667c905 | 2012-10-10 16:49:28 +0200 | [diff] [blame] | 174 | " -m <lines> limit output to the first <lines> lines\n" |
Willy Tarreau | 615674c | 2012-01-23 08:15:51 +0100 | [diff] [blame] | 175 | "Output filters - only one may be used at a time\n" |
| 176 | " -c only report the number of lines that would have been printed\n" |
| 177 | " -pct output connect and response times percentiles\n" |
| 178 | " -st output number of requests per HTTP status code\n" |
Willy Tarreau | 8a09b66 | 2012-10-10 10:26:22 +0200 | [diff] [blame] | 179 | " -cc output number of requests per cookie code (2 chars)\n" |
Willy Tarreau | 615674c | 2012-01-23 08:15:51 +0100 | [diff] [blame] | 180 | " -tc output number of requests per termination code (2 chars)\n" |
| 181 | " -srv output statistics per server (time, requests, errors)\n" |
| 182 | " -u* output statistics per URL (time, requests, errors)\n" |
| 183 | " Additional characters indicate the output sorting key :\n" |
| 184 | " -u : by URL, -uc : request count, -ue : error count\n" |
Willy Tarreau | 4201df7 | 2012-10-10 14:57:35 +0200 | [diff] [blame] | 185 | " -ua : average response time, -ut : average total time\n" |
Willy Tarreau | 615674c | 2012-01-23 08:15:51 +0100 | [diff] [blame] | 186 | " -uao, -uto: average times computed on valid ('OK') requests\n" |
Baptiste | 61aaad0 | 2012-09-08 23:10:03 +0200 | [diff] [blame] | 187 | " -uba, -ubt: average bytes returned, total bytes returned\n" |
Willy Tarreau | 615674c | 2012-01-23 08:15:51 +0100 | [diff] [blame] | 188 | ); |
| 189 | exit(0); |
| 190 | } |
| 191 | |
Willy Tarreau | 72c2853 | 2009-01-22 18:56:50 +0100 | [diff] [blame] | 192 | |
| 193 | /* return pointer to first char not part of current field starting at <p>. */ |
Willy Tarreau | f904206 | 2011-09-10 12:26:35 +0200 | [diff] [blame] | 194 | |
| 195 | #if defined(__i386__) |
| 196 | /* this one is always faster on 32-bits */ |
| 197 | static inline const char *field_stop(const char *p) |
| 198 | { |
| 199 | asm( |
| 200 | /* Look for spaces */ |
| 201 | "4: \n\t" |
| 202 | "inc %0 \n\t" |
| 203 | "cmpb $0x20, -1(%0) \n\t" |
| 204 | "ja 4b \n\t" |
| 205 | "jz 3f \n\t" |
| 206 | |
| 207 | /* we only get there for control chars 0..31. Leave if we find '\0' */ |
| 208 | "cmpb $0x0, -1(%0) \n\t" |
| 209 | "jnz 4b \n\t" |
| 210 | |
| 211 | /* return %0-1 = position of the last char we checked */ |
| 212 | "3: \n\t" |
| 213 | "dec %0 \n\t" |
| 214 | : "=r" (p) |
| 215 | : "0" (p) |
| 216 | ); |
| 217 | return p; |
| 218 | } |
| 219 | #else |
Willy Tarreau | 72c2853 | 2009-01-22 18:56:50 +0100 | [diff] [blame] | 220 | const char *field_stop(const char *p) |
| 221 | { |
| 222 | unsigned char c; |
| 223 | |
| 224 | while (1) { |
| 225 | c = *(p++); |
| 226 | if (c > ' ') |
| 227 | continue; |
Willy Tarreau | 14389e7 | 2011-07-10 22:11:17 +0200 | [diff] [blame] | 228 | if (c == ' ' || c == 0) |
Willy Tarreau | 72c2853 | 2009-01-22 18:56:50 +0100 | [diff] [blame] | 229 | break; |
| 230 | } |
| 231 | return p - 1; |
| 232 | } |
Willy Tarreau | f904206 | 2011-09-10 12:26:35 +0200 | [diff] [blame] | 233 | #endif |
Willy Tarreau | 72c2853 | 2009-01-22 18:56:50 +0100 | [diff] [blame] | 234 | |
| 235 | /* return field <field> (starting from 1) in string <p>. Only consider |
| 236 | * contiguous spaces (or tabs) as one delimiter. May return pointer to |
| 237 | * last char if field is not found. Equivalent to awk '{print $field}'. |
| 238 | */ |
| 239 | const char *field_start(const char *p, int field) |
| 240 | { |
Willy Tarreau | f904206 | 2011-09-10 12:26:35 +0200 | [diff] [blame] | 241 | #ifndef PREFER_ASM |
Willy Tarreau | 72c2853 | 2009-01-22 18:56:50 +0100 | [diff] [blame] | 242 | unsigned char c; |
| 243 | while (1) { |
| 244 | /* skip spaces */ |
| 245 | while (1) { |
Willy Tarreau | f904206 | 2011-09-10 12:26:35 +0200 | [diff] [blame] | 246 | c = *(p++); |
Willy Tarreau | 72c2853 | 2009-01-22 18:56:50 +0100 | [diff] [blame] | 247 | if (c > ' ') |
| 248 | break; |
Willy Tarreau | 14389e7 | 2011-07-10 22:11:17 +0200 | [diff] [blame] | 249 | if (c == ' ') |
Willy Tarreau | f904206 | 2011-09-10 12:26:35 +0200 | [diff] [blame] | 250 | continue; |
Willy Tarreau | 72c2853 | 2009-01-22 18:56:50 +0100 | [diff] [blame] | 251 | if (!c) /* end of line */ |
Willy Tarreau | f904206 | 2011-09-10 12:26:35 +0200 | [diff] [blame] | 252 | return p-1; |
Willy Tarreau | 72c2853 | 2009-01-22 18:56:50 +0100 | [diff] [blame] | 253 | /* other char => new field */ |
| 254 | break; |
Willy Tarreau | 72c2853 | 2009-01-22 18:56:50 +0100 | [diff] [blame] | 255 | } |
| 256 | |
| 257 | /* start of field */ |
| 258 | field--; |
| 259 | if (!field) |
Willy Tarreau | f904206 | 2011-09-10 12:26:35 +0200 | [diff] [blame] | 260 | return p-1; |
Willy Tarreau | 72c2853 | 2009-01-22 18:56:50 +0100 | [diff] [blame] | 261 | |
| 262 | /* skip this field */ |
| 263 | while (1) { |
| 264 | c = *(p++); |
Willy Tarreau | 14389e7 | 2011-07-10 22:11:17 +0200 | [diff] [blame] | 265 | if (c == ' ') |
Willy Tarreau | 72c2853 | 2009-01-22 18:56:50 +0100 | [diff] [blame] | 266 | break; |
Willy Tarreau | f904206 | 2011-09-10 12:26:35 +0200 | [diff] [blame] | 267 | if (c > ' ') |
| 268 | continue; |
Willy Tarreau | 72c2853 | 2009-01-22 18:56:50 +0100 | [diff] [blame] | 269 | if (c == '\0') |
Willy Tarreau | f904206 | 2011-09-10 12:26:35 +0200 | [diff] [blame] | 270 | return p - 1; |
Willy Tarreau | 72c2853 | 2009-01-22 18:56:50 +0100 | [diff] [blame] | 271 | } |
| 272 | } |
Willy Tarreau | f904206 | 2011-09-10 12:26:35 +0200 | [diff] [blame] | 273 | #else |
| 274 | /* This version works optimally on i386 and x86_64 but the code above |
| 275 | * shows similar performance. However, depending on the version of GCC |
| 276 | * used, inlining rules change and it may have difficulties to make |
| 277 | * efficient use of this code at other locations and could result in |
| 278 | * worse performance (eg: gcc 4.4). You may want to experience. |
| 279 | */ |
| 280 | asm( |
| 281 | /* skip spaces */ |
| 282 | "1: \n\t" |
| 283 | "inc %0 \n\t" |
| 284 | "cmpb $0x20, -1(%0) \n\t" |
| 285 | "ja 2f \n\t" |
| 286 | "jz 1b \n\t" |
| 287 | |
| 288 | /* we only get there for control chars 0..31. Leave if we find '\0' */ |
| 289 | "cmpb $0x0, -1(%0) \n\t" |
| 290 | "jz 3f \n\t" |
| 291 | |
| 292 | /* start of field at [%0-1]. Check if we need to skip more fields */ |
| 293 | "2: \n\t" |
| 294 | "dec %1 \n\t" |
| 295 | "jz 3f \n\t" |
| 296 | |
| 297 | /* Look for spaces */ |
| 298 | "4: \n\t" |
| 299 | "inc %0 \n\t" |
| 300 | "cmpb $0x20, -1(%0) \n\t" |
| 301 | "jz 1b \n\t" |
| 302 | "ja 4b \n\t" |
| 303 | |
| 304 | /* we only get there for control chars 0..31. Leave if we find '\0' */ |
| 305 | "cmpb $0x0, -1(%0) \n\t" |
| 306 | "jnz 4b \n\t" |
| 307 | |
| 308 | /* return %0-1 = position of the last char we checked */ |
| 309 | "3: \n\t" |
| 310 | "dec %0 \n\t" |
| 311 | : "=r" (p) |
| 312 | : "r" (field), "0" (p) |
| 313 | ); |
| 314 | return p; |
| 315 | #endif |
Willy Tarreau | 72c2853 | 2009-01-22 18:56:50 +0100 | [diff] [blame] | 316 | } |
| 317 | |
| 318 | /* keep only the <bits> higher bits of <i> */ |
| 319 | static inline unsigned int quantify_u32(unsigned int i, int bits) |
| 320 | { |
| 321 | int high; |
| 322 | |
| 323 | if (!bits) |
| 324 | return 0; |
| 325 | |
| 326 | if (i) |
| 327 | high = fls_auto(i); // 1 to 32 |
| 328 | else |
| 329 | high = 0; |
| 330 | |
| 331 | if (high <= bits) |
| 332 | return i; |
| 333 | |
| 334 | return i & ~((1 << (high - bits)) - 1); |
| 335 | } |
| 336 | |
| 337 | /* keep only the <bits> higher bits of the absolute value of <i>, as well as |
| 338 | * its sign. */ |
| 339 | static inline int quantify(int i, int bits) |
| 340 | { |
| 341 | if (i >= 0) |
| 342 | return quantify_u32(i, bits); |
| 343 | else |
| 344 | return -quantify_u32(-i, bits); |
| 345 | } |
| 346 | |
| 347 | /* Insert timer value <v> into tree <r>. A pre-allocated node must be passed |
| 348 | * in <alloc>. It may be NULL, in which case the function will allocate it |
| 349 | * itself. It will be reset to NULL once consumed. The caller is responsible |
| 350 | * for freeing the node once not used anymore. The node where the value was |
| 351 | * inserted is returned. |
| 352 | */ |
| 353 | struct timer *insert_timer(struct eb_root *r, struct timer **alloc, int v) |
| 354 | { |
| 355 | struct timer *t = *alloc; |
| 356 | struct eb32_node *n; |
| 357 | |
| 358 | if (!t) { |
| 359 | t = calloc(sizeof(*t), 1); |
| 360 | if (unlikely(!t)) { |
| 361 | fprintf(stderr, "%s: not enough memory\n", __FUNCTION__); |
| 362 | exit(1); |
| 363 | } |
| 364 | } |
| 365 | t->node.key = quantify(v, QBITS); // keep only the higher QBITS bits |
| 366 | |
| 367 | n = eb32i_insert(r, &t->node); |
| 368 | if (n == &t->node) |
| 369 | t = NULL; /* node inserted, will malloc next time */ |
| 370 | |
| 371 | *alloc = t; |
| 372 | return container_of(n, struct timer, node); |
| 373 | } |
| 374 | |
| 375 | /* Insert value value <v> into tree <r>. A pre-allocated node must be passed |
| 376 | * in <alloc>. It may be NULL, in which case the function will allocate it |
| 377 | * itself. It will be reset to NULL once consumed. The caller is responsible |
| 378 | * for freeing the node once not used anymore. The node where the value was |
| 379 | * inserted is returned. |
| 380 | */ |
| 381 | struct timer *insert_value(struct eb_root *r, struct timer **alloc, int v) |
| 382 | { |
| 383 | struct timer *t = *alloc; |
| 384 | struct eb32_node *n; |
| 385 | |
| 386 | if (!t) { |
| 387 | t = calloc(sizeof(*t), 1); |
| 388 | if (unlikely(!t)) { |
| 389 | fprintf(stderr, "%s: not enough memory\n", __FUNCTION__); |
| 390 | exit(1); |
| 391 | } |
| 392 | } |
| 393 | t->node.key = v; |
| 394 | |
| 395 | n = eb32i_insert(r, &t->node); |
| 396 | if (n == &t->node) |
| 397 | t = NULL; /* node inserted, will malloc next time */ |
| 398 | |
| 399 | *alloc = t; |
| 400 | return container_of(n, struct timer, node); |
| 401 | } |
| 402 | |
| 403 | int str2ic(const char *s) |
| 404 | { |
| 405 | int i = 0; |
| 406 | int j, k; |
| 407 | |
| 408 | if (*s != '-') { |
| 409 | /* positive number */ |
| 410 | while (1) { |
| 411 | j = (*s++) - '0'; |
| 412 | k = i * 10; |
| 413 | if ((unsigned)j > 9) |
| 414 | break; |
| 415 | i = k + j; |
| 416 | } |
| 417 | } else { |
| 418 | /* negative number */ |
| 419 | s++; |
| 420 | while (1) { |
| 421 | j = (*s++) - '0'; |
| 422 | k = i * 10; |
| 423 | if ((unsigned)j > 9) |
| 424 | break; |
| 425 | i = k - j; |
| 426 | } |
| 427 | } |
| 428 | |
| 429 | return i; |
| 430 | } |
| 431 | |
| 432 | |
| 433 | /* Equivalent to strtoul with a length. */ |
| 434 | static inline unsigned int __strl2ui(const char *s, int len) |
| 435 | { |
| 436 | unsigned int i = 0; |
| 437 | while (len-- > 0) { |
| 438 | i = i * 10 - '0'; |
| 439 | i += (unsigned char)*s++; |
| 440 | } |
| 441 | return i; |
| 442 | } |
| 443 | |
| 444 | unsigned int strl2ui(const char *s, int len) |
| 445 | { |
| 446 | return __strl2ui(s, len); |
| 447 | } |
| 448 | |
| 449 | /* Convert "[04/Dec/2008:09:49:40.555]" to an integer equivalent to the time of |
| 450 | * the day in milliseconds. It returns -1 for all unparsable values. The parser |
| 451 | * looks ugly but gcc emits far better code that way. |
| 452 | */ |
| 453 | int convert_date(const char *field) |
| 454 | { |
| 455 | unsigned int h, m, s, ms; |
| 456 | unsigned char c; |
| 457 | const char *b, *e; |
| 458 | |
| 459 | h = m = s = ms = 0; |
| 460 | e = field; |
| 461 | |
| 462 | /* skip the date */ |
| 463 | while (1) { |
| 464 | c = *(e++); |
| 465 | if (c == ':') |
| 466 | break; |
| 467 | if (!c) |
| 468 | goto out_err; |
| 469 | } |
| 470 | |
| 471 | /* hour + ':' */ |
| 472 | b = e; |
| 473 | while (1) { |
| 474 | c = *(e++) - '0'; |
| 475 | if (c > 9) |
| 476 | break; |
| 477 | h = h * 10 + c; |
| 478 | } |
| 479 | if (c == (unsigned char)(0 - '0')) |
| 480 | goto out_err; |
| 481 | |
| 482 | /* minute + ':' */ |
| 483 | b = e; |
| 484 | while (1) { |
| 485 | c = *(e++) - '0'; |
| 486 | if (c > 9) |
| 487 | break; |
| 488 | m = m * 10 + c; |
| 489 | } |
| 490 | if (c == (unsigned char)(0 - '0')) |
| 491 | goto out_err; |
| 492 | |
| 493 | /* second + '.' or ']' */ |
| 494 | b = e; |
| 495 | while (1) { |
| 496 | c = *(e++) - '0'; |
| 497 | if (c > 9) |
| 498 | break; |
| 499 | s = s * 10 + c; |
| 500 | } |
| 501 | if (c == (unsigned char)(0 - '0')) |
| 502 | goto out_err; |
| 503 | |
| 504 | /* if there's a '.', we have milliseconds */ |
| 505 | if (c == (unsigned char)('.' - '0')) { |
| 506 | /* millisecond second + ']' */ |
| 507 | b = e; |
| 508 | while (1) { |
| 509 | c = *(e++) - '0'; |
| 510 | if (c > 9) |
| 511 | break; |
| 512 | ms = ms * 10 + c; |
| 513 | } |
| 514 | if (c == (unsigned char)(0 - '0')) |
| 515 | goto out_err; |
| 516 | } |
| 517 | return (((h * 60) + m) * 60 + s) * 1000 + ms; |
| 518 | out_err: |
| 519 | return -1; |
| 520 | } |
| 521 | |
| 522 | void truncated_line(int linenum, const char *line) |
| 523 | { |
| 524 | if (!(filter & FILT_QUIET)) |
| 525 | fprintf(stderr, "Truncated line %d: %s\n", linenum, line); |
| 526 | } |
| 527 | |
| 528 | int main(int argc, char **argv) |
| 529 | { |
Willy Tarreau | 26deaf5 | 2011-07-10 19:47:48 +0200 | [diff] [blame] | 530 | const char *b, *e, *p, *time_field, *accept_field; |
Hervé COMMOWICK | 927cddd | 2011-08-10 17:42:41 +0200 | [diff] [blame] | 531 | const char *filter_term_code_name = NULL; |
Willy Tarreau | 72c2853 | 2009-01-22 18:56:50 +0100 | [diff] [blame] | 532 | const char *output_file = NULL; |
Willy Tarreau | a2b39fb | 2011-07-10 21:39:35 +0200 | [diff] [blame] | 533 | int f, last, err; |
| 534 | struct timer *t = NULL; |
Willy Tarreau | 72c2853 | 2009-01-22 18:56:50 +0100 | [diff] [blame] | 535 | struct eb32_node *n; |
Willy Tarreau | abe45b6 | 2010-10-28 20:33:46 +0200 | [diff] [blame] | 536 | struct url_stat *ustat = NULL; |
Willy Tarreau | 72c2853 | 2009-01-22 18:56:50 +0100 | [diff] [blame] | 537 | int val, test; |
Willy Tarreau | 72c2853 | 2009-01-22 18:56:50 +0100 | [diff] [blame] | 538 | int filter_acc_delay = 0, filter_acc_count = 0; |
Willy Tarreau | 5bdfd96 | 2009-10-14 15:16:29 +0200 | [diff] [blame] | 539 | int filter_time_resp = 0; |
Willy Tarreau | d3007ff | 2011-09-05 02:07:23 +0200 | [diff] [blame] | 540 | int filt_http_status_low = 0, filt_http_status_high = 0; |
Willy Tarreau | 72c2853 | 2009-01-22 18:56:50 +0100 | [diff] [blame] | 541 | int skip_fields = 1; |
| 542 | |
Willy Tarreau | a2b39fb | 2011-07-10 21:39:35 +0200 | [diff] [blame] | 543 | void (*line_filter)(const char *accept_field, const char *time_field, struct timer **tptr) = NULL; |
| 544 | |
Willy Tarreau | 72c2853 | 2009-01-22 18:56:50 +0100 | [diff] [blame] | 545 | argc--; argv++; |
| 546 | while (argc > 0) { |
| 547 | if (*argv[0] != '-') |
| 548 | break; |
| 549 | |
| 550 | if (strcmp(argv[0], "-ad") == 0) { |
| 551 | if (argc < 2) die("missing option for -ad"); |
| 552 | argc--; argv++; |
| 553 | filter |= FILT_ACC_DELAY; |
| 554 | filter_acc_delay = atol(*argv); |
| 555 | } |
| 556 | else if (strcmp(argv[0], "-ac") == 0) { |
| 557 | if (argc < 2) die("missing option for -ac"); |
| 558 | argc--; argv++; |
| 559 | filter |= FILT_ACC_COUNT; |
| 560 | filter_acc_count = atol(*argv); |
| 561 | } |
Willy Tarreau | 5bdfd96 | 2009-10-14 15:16:29 +0200 | [diff] [blame] | 562 | else if (strcmp(argv[0], "-rt") == 0) { |
| 563 | if (argc < 2) die("missing option for -rt"); |
| 564 | argc--; argv++; |
| 565 | filter |= FILT_TIME_RESP; |
| 566 | filter_time_resp = atol(*argv); |
| 567 | } |
| 568 | else if (strcmp(argv[0], "-RT") == 0) { |
| 569 | if (argc < 2) die("missing option for -RT"); |
| 570 | argc--; argv++; |
| 571 | filter |= FILT_TIME_RESP | FILT_INVERT_TIME_RESP; |
| 572 | filter_time_resp = atol(*argv); |
| 573 | } |
Willy Tarreau | 72c2853 | 2009-01-22 18:56:50 +0100 | [diff] [blame] | 574 | else if (strcmp(argv[0], "-s") == 0) { |
| 575 | if (argc < 2) die("missing option for -s"); |
| 576 | argc--; argv++; |
| 577 | skip_fields = atol(*argv); |
| 578 | } |
Willy Tarreau | 667c905 | 2012-10-10 16:49:28 +0200 | [diff] [blame] | 579 | else if (strcmp(argv[0], "-m") == 0) { |
| 580 | if (argc < 2) die("missing option for -m"); |
| 581 | argc--; argv++; |
| 582 | lines_max = atol(*argv); |
| 583 | } |
Willy Tarreau | 72c2853 | 2009-01-22 18:56:50 +0100 | [diff] [blame] | 584 | else if (strcmp(argv[0], "-e") == 0) |
| 585 | filter |= FILT_ERRORS_ONLY; |
Willy Tarreau | 5bdfd96 | 2009-10-14 15:16:29 +0200 | [diff] [blame] | 586 | else if (strcmp(argv[0], "-E") == 0) |
| 587 | filter |= FILT_ERRORS_ONLY | FILT_INVERT_ERRORS; |
Willy Tarreau | 70c428f | 2011-07-10 17:27:40 +0200 | [diff] [blame] | 588 | else if (strcmp(argv[0], "-H") == 0) |
| 589 | filter |= FILT_HTTP_ONLY; |
Willy Tarreau | 08911ff | 2011-10-13 13:28:36 +0200 | [diff] [blame] | 590 | else if (strcmp(argv[0], "-Q") == 0) |
| 591 | filter |= FILT_QUEUE_ONLY; |
| 592 | else if (strcmp(argv[0], "-QS") == 0) |
| 593 | filter |= FILT_QUEUE_SRV_ONLY; |
Willy Tarreau | 72c2853 | 2009-01-22 18:56:50 +0100 | [diff] [blame] | 594 | else if (strcmp(argv[0], "-c") == 0) |
| 595 | filter |= FILT_COUNT_ONLY; |
| 596 | else if (strcmp(argv[0], "-q") == 0) |
| 597 | filter |= FILT_QUIET; |
| 598 | else if (strcmp(argv[0], "-v") == 0) |
| 599 | filter_invert = !filter_invert; |
| 600 | else if (strcmp(argv[0], "-gt") == 0) |
| 601 | filter |= FILT_GRAPH_TIMERS; |
Willy Tarreau | 214c203 | 2009-02-20 11:02:32 +0100 | [diff] [blame] | 602 | else if (strcmp(argv[0], "-pct") == 0) |
| 603 | filter |= FILT_PERCENTILE; |
Willy Tarreau | 0f423a7 | 2010-05-03 10:50:54 +0200 | [diff] [blame] | 604 | else if (strcmp(argv[0], "-st") == 0) |
| 605 | filter |= FILT_COUNT_STATUS; |
Willy Tarreau | d220106 | 2010-05-27 18:17:30 +0200 | [diff] [blame] | 606 | else if (strcmp(argv[0], "-srv") == 0) |
| 607 | filter |= FILT_COUNT_SRV_STATUS; |
Willy Tarreau | 8a09b66 | 2012-10-10 10:26:22 +0200 | [diff] [blame] | 608 | else if (strcmp(argv[0], "-cc") == 0) |
| 609 | filter |= FILT_COUNT_COOK_CODES; |
Willy Tarreau | d8fc110 | 2010-09-12 17:56:16 +0200 | [diff] [blame] | 610 | else if (strcmp(argv[0], "-tc") == 0) |
| 611 | filter |= FILT_COUNT_TERM_CODES; |
Hervé COMMOWICK | 927cddd | 2011-08-10 17:42:41 +0200 | [diff] [blame] | 612 | else if (strcmp(argv[0], "-tcn") == 0) { |
| 613 | if (argc < 2) die("missing option for -tcn"); |
| 614 | argc--; argv++; |
| 615 | filter |= FILT_TERM_CODE_NAME; |
| 616 | filter_term_code_name = *argv; |
| 617 | } |
| 618 | else if (strcmp(argv[0], "-TCN") == 0) { |
| 619 | if (argc < 2) die("missing option for -TCN"); |
| 620 | argc--; argv++; |
| 621 | filter |= FILT_TERM_CODE_NAME | FILT_INVERT_TERM_CODE_NAME; |
| 622 | filter_term_code_name = *argv; |
| 623 | } |
Willy Tarreau | d3007ff | 2011-09-05 02:07:23 +0200 | [diff] [blame] | 624 | else if (strcmp(argv[0], "-hs") == 0 || strcmp(argv[0], "-HS") == 0) { |
| 625 | char *sep, *str; |
| 626 | |
| 627 | if (argc < 2) die("missing option for -hs/-HS ([min]:[max])"); |
| 628 | filter |= FILT_HTTP_STATUS; |
| 629 | if (argv[0][1] == 'H') |
| 630 | filter |= FILT_INVERT_HTTP_STATUS; |
| 631 | |
| 632 | argc--; argv++; |
| 633 | str = *argv; |
| 634 | sep = strchr(str, ':'); /* [min]:[max] */ |
| 635 | if (!sep) |
| 636 | sep = str; /* make max point to min */ |
| 637 | else |
| 638 | *sep++ = 0; |
| 639 | filt_http_status_low = *str ? atol(str) : 0; |
| 640 | filt_http_status_high = *sep ? atol(sep) : 65535; |
| 641 | } |
Willy Tarreau | abe45b6 | 2010-10-28 20:33:46 +0200 | [diff] [blame] | 642 | else if (strcmp(argv[0], "-u") == 0) |
| 643 | filter |= FILT_COUNT_URL_ONLY; |
| 644 | else if (strcmp(argv[0], "-uc") == 0) |
| 645 | filter |= FILT_COUNT_URL_COUNT; |
| 646 | else if (strcmp(argv[0], "-ue") == 0) |
| 647 | filter |= FILT_COUNT_URL_ERR; |
| 648 | else if (strcmp(argv[0], "-ua") == 0) |
| 649 | filter |= FILT_COUNT_URL_TAVG; |
| 650 | else if (strcmp(argv[0], "-ut") == 0) |
| 651 | filter |= FILT_COUNT_URL_TTOT; |
| 652 | else if (strcmp(argv[0], "-uao") == 0) |
| 653 | filter |= FILT_COUNT_URL_TAVGO; |
| 654 | else if (strcmp(argv[0], "-uto") == 0) |
| 655 | filter |= FILT_COUNT_URL_TTOTO; |
Baptiste | 61aaad0 | 2012-09-08 23:10:03 +0200 | [diff] [blame] | 656 | else if (strcmp(argv[0], "-uba") == 0) |
| 657 | filter |= FILT_COUNT_URL_BAVG; |
| 658 | else if (strcmp(argv[0], "-ubt") == 0) |
| 659 | filter |= FILT_COUNT_URL_BTOT; |
Willy Tarreau | 72c2853 | 2009-01-22 18:56:50 +0100 | [diff] [blame] | 660 | else if (strcmp(argv[0], "-o") == 0) { |
| 661 | if (output_file) |
| 662 | die("Fatal: output file name already specified.\n"); |
| 663 | if (argc < 2) |
| 664 | die("Fatal: missing output file name.\n"); |
| 665 | output_file = argv[1]; |
| 666 | } |
Willy Tarreau | 615674c | 2012-01-23 08:15:51 +0100 | [diff] [blame] | 667 | else if (strcmp(argv[0], "-h") == 0 || strcmp(argv[0], "--help") == 0) |
| 668 | help(); |
Willy Tarreau | 72c2853 | 2009-01-22 18:56:50 +0100 | [diff] [blame] | 669 | argc--; |
| 670 | argv++; |
| 671 | } |
| 672 | |
| 673 | if (!filter) |
| 674 | die("No action specified.\n"); |
| 675 | |
| 676 | if (filter & FILT_ACC_COUNT && !filter_acc_count) |
| 677 | filter_acc_count=1; |
| 678 | |
| 679 | if (filter & FILT_ACC_DELAY && !filter_acc_delay) |
| 680 | filter_acc_delay = 1; |
| 681 | |
Willy Tarreau | a2b39fb | 2011-07-10 21:39:35 +0200 | [diff] [blame] | 682 | |
| 683 | /* by default, all lines are printed */ |
| 684 | line_filter = filter_output_line; |
| 685 | if (filter & (FILT_ACC_COUNT|FILT_ACC_DELAY)) |
| 686 | line_filter = filter_accept_holes; |
| 687 | else if (filter & (FILT_GRAPH_TIMERS|FILT_PERCENTILE)) |
| 688 | line_filter = filter_graphs; |
| 689 | else if (filter & FILT_COUNT_STATUS) |
| 690 | line_filter = filter_count_status; |
Willy Tarreau | 8a09b66 | 2012-10-10 10:26:22 +0200 | [diff] [blame] | 691 | else if (filter & FILT_COUNT_COOK_CODES) |
| 692 | line_filter = filter_count_cook_codes; |
Willy Tarreau | a2b39fb | 2011-07-10 21:39:35 +0200 | [diff] [blame] | 693 | else if (filter & FILT_COUNT_TERM_CODES) |
| 694 | line_filter = filter_count_term_codes; |
| 695 | else if (filter & FILT_COUNT_SRV_STATUS) |
| 696 | line_filter = filter_count_srv_status; |
| 697 | else if (filter & FILT_COUNT_URL_ANY) |
| 698 | line_filter = filter_count_url; |
| 699 | else if (filter & FILT_COUNT_ONLY) |
| 700 | line_filter = NULL; |
Willy Tarreau | 72c2853 | 2009-01-22 18:56:50 +0100 | [diff] [blame] | 701 | |
Willy Tarreau | f8c95d2 | 2012-06-12 09:16:56 +0200 | [diff] [blame] | 702 | #if defined(POSIX_FADV_SEQUENTIAL) |
| 703 | /* around 20% performance improvement is observed on Linux with this |
| 704 | * on cold-cache. Surprizingly, WILLNEED is less performant. Don't |
| 705 | * use NOREUSE as it flushes the cache and prevents easy data |
| 706 | * manipulation on logs! |
| 707 | */ |
| 708 | posix_fadvise(0, 0, 0, POSIX_FADV_SEQUENTIAL); |
| 709 | #endif |
| 710 | |
Willy Tarreau | a1629a5 | 2012-11-13 20:48:15 +0100 | [diff] [blame] | 711 | if (!line_filter && /* FILT_COUNT_ONLY ( see above), and no input filter (see below) */ |
Willy Tarreau | e1a908c | 2012-01-03 09:23:03 +0100 | [diff] [blame] | 712 | !(filter & (FILT_HTTP_ONLY|FILT_TIME_RESP|FILT_ERRORS_ONLY|FILT_HTTP_STATUS|FILT_QUEUE_ONLY|FILT_QUEUE_SRV_ONLY|FILT_TERM_CODE_NAME))) { |
Willy Tarreau | a1629a5 | 2012-11-13 20:48:15 +0100 | [diff] [blame] | 713 | /* read the whole file at once first, ignore it if inverted output */ |
Willy Tarreau | e1a908c | 2012-01-03 09:23:03 +0100 | [diff] [blame] | 714 | if (!filter_invert) |
Willy Tarreau | a1629a5 | 2012-11-13 20:48:15 +0100 | [diff] [blame] | 715 | while ((lines_max < 0 || lines_out < lines_max) && fgets2(stdin) != NULL) |
Willy Tarreau | e1a908c | 2012-01-03 09:23:03 +0100 | [diff] [blame] | 716 | lines_out++; |
| 717 | |
| 718 | goto skip_filters; |
| 719 | } |
| 720 | |
Willy Tarreau | 214c203 | 2009-02-20 11:02:32 +0100 | [diff] [blame] | 721 | while ((line = fgets2(stdin)) != NULL) { |
Willy Tarreau | 72c2853 | 2009-01-22 18:56:50 +0100 | [diff] [blame] | 722 | linenum++; |
Willy Tarreau | 26deaf5 | 2011-07-10 19:47:48 +0200 | [diff] [blame] | 723 | time_field = NULL; accept_field = NULL; |
Willy Tarreau | 72c2853 | 2009-01-22 18:56:50 +0100 | [diff] [blame] | 724 | |
Willy Tarreau | 5bdfd96 | 2009-10-14 15:16:29 +0200 | [diff] [blame] | 725 | test = 1; |
Willy Tarreau | 26deaf5 | 2011-07-10 19:47:48 +0200 | [diff] [blame] | 726 | |
| 727 | /* for any line we process, we first ensure that there is a field |
| 728 | * looking like the accept date field (beginning with a '['). |
| 729 | */ |
| 730 | accept_field = field_start(line, ACCEPT_FIELD + skip_fields); |
Willy Tarreau | a2b39fb | 2011-07-10 21:39:35 +0200 | [diff] [blame] | 731 | if (unlikely(*accept_field != '[')) { |
Willy Tarreau | 26deaf5 | 2011-07-10 19:47:48 +0200 | [diff] [blame] | 732 | parse_err++; |
| 733 | continue; |
| 734 | } |
| 735 | |
| 736 | /* the day of month field is begin 01 and 31 */ |
Willy Tarreau | a2b39fb | 2011-07-10 21:39:35 +0200 | [diff] [blame] | 737 | if (accept_field[1] < '0' || accept_field[1] > '3') { |
Willy Tarreau | 26deaf5 | 2011-07-10 19:47:48 +0200 | [diff] [blame] | 738 | parse_err++; |
| 739 | continue; |
| 740 | } |
| 741 | |
Willy Tarreau | a2b39fb | 2011-07-10 21:39:35 +0200 | [diff] [blame] | 742 | if (filter & FILT_HTTP_ONLY) { |
Willy Tarreau | 70c428f | 2011-07-10 17:27:40 +0200 | [diff] [blame] | 743 | /* only report lines with at least 4 timers */ |
Willy Tarreau | a2b39fb | 2011-07-10 21:39:35 +0200 | [diff] [blame] | 744 | if (!time_field) { |
Willy Tarreau | 26deaf5 | 2011-07-10 19:47:48 +0200 | [diff] [blame] | 745 | time_field = field_start(accept_field, TIME_FIELD - ACCEPT_FIELD + 1); |
Willy Tarreau | a2b39fb | 2011-07-10 21:39:35 +0200 | [diff] [blame] | 746 | if (unlikely(!*time_field)) { |
Willy Tarreau | 26deaf5 | 2011-07-10 19:47:48 +0200 | [diff] [blame] | 747 | truncated_line(linenum, line); |
| 748 | continue; |
| 749 | } |
Willy Tarreau | 70c428f | 2011-07-10 17:27:40 +0200 | [diff] [blame] | 750 | } |
| 751 | |
Willy Tarreau | 758a6ea | 2011-07-10 18:53:44 +0200 | [diff] [blame] | 752 | e = field_stop(time_field + 1); |
| 753 | /* we have field TIME_FIELD in [time_field]..[e-1] */ |
| 754 | p = time_field; |
Willy Tarreau | 70c428f | 2011-07-10 17:27:40 +0200 | [diff] [blame] | 755 | f = 0; |
Willy Tarreau | df6f0d1 | 2011-07-10 18:15:08 +0200 | [diff] [blame] | 756 | while (!SEP(*p)) { |
Willy Tarreau | 70c428f | 2011-07-10 17:27:40 +0200 | [diff] [blame] | 757 | if (++f == 4) |
| 758 | break; |
| 759 | SKIP_CHAR(p, '/'); |
| 760 | } |
| 761 | test &= (f >= 4); |
| 762 | } |
| 763 | |
Willy Tarreau | a2b39fb | 2011-07-10 21:39:35 +0200 | [diff] [blame] | 764 | if (filter & FILT_TIME_RESP) { |
Willy Tarreau | 5bdfd96 | 2009-10-14 15:16:29 +0200 | [diff] [blame] | 765 | int tps; |
| 766 | |
| 767 | /* only report lines with response times larger than filter_time_resp */ |
Willy Tarreau | a2b39fb | 2011-07-10 21:39:35 +0200 | [diff] [blame] | 768 | if (!time_field) { |
Willy Tarreau | 26deaf5 | 2011-07-10 19:47:48 +0200 | [diff] [blame] | 769 | time_field = field_start(accept_field, TIME_FIELD - ACCEPT_FIELD + 1); |
Willy Tarreau | a2b39fb | 2011-07-10 21:39:35 +0200 | [diff] [blame] | 770 | if (unlikely(!*time_field)) { |
Willy Tarreau | 26deaf5 | 2011-07-10 19:47:48 +0200 | [diff] [blame] | 771 | truncated_line(linenum, line); |
| 772 | continue; |
| 773 | } |
Willy Tarreau | 5bdfd96 | 2009-10-14 15:16:29 +0200 | [diff] [blame] | 774 | } |
| 775 | |
Willy Tarreau | 758a6ea | 2011-07-10 18:53:44 +0200 | [diff] [blame] | 776 | e = field_stop(time_field + 1); |
| 777 | /* we have field TIME_FIELD in [time_field]..[e-1], let's check only the response time */ |
Willy Tarreau | 5bdfd96 | 2009-10-14 15:16:29 +0200 | [diff] [blame] | 778 | |
Willy Tarreau | 758a6ea | 2011-07-10 18:53:44 +0200 | [diff] [blame] | 779 | p = time_field; |
Willy Tarreau | 5bdfd96 | 2009-10-14 15:16:29 +0200 | [diff] [blame] | 780 | err = 0; |
Willy Tarreau | 24bcb4f | 2010-10-28 20:39:50 +0200 | [diff] [blame] | 781 | f = 0; |
Willy Tarreau | df6f0d1 | 2011-07-10 18:15:08 +0200 | [diff] [blame] | 782 | while (!SEP(*p)) { |
Willy Tarreau | 5bdfd96 | 2009-10-14 15:16:29 +0200 | [diff] [blame] | 783 | tps = str2ic(p); |
| 784 | if (tps < 0) { |
| 785 | tps = -1; |
| 786 | err = 1; |
| 787 | } |
Willy Tarreau | 24bcb4f | 2010-10-28 20:39:50 +0200 | [diff] [blame] | 788 | if (++f == 4) |
| 789 | break; |
Willy Tarreau | 5bdfd96 | 2009-10-14 15:16:29 +0200 | [diff] [blame] | 790 | SKIP_CHAR(p, '/'); |
| 791 | } |
| 792 | |
Willy Tarreau | a2b39fb | 2011-07-10 21:39:35 +0200 | [diff] [blame] | 793 | if (unlikely(f < 4)) { |
Willy Tarreau | 5bdfd96 | 2009-10-14 15:16:29 +0200 | [diff] [blame] | 794 | parse_err++; |
| 795 | continue; |
| 796 | } |
| 797 | |
| 798 | test &= (tps >= filter_time_resp) ^ !!(filter & FILT_INVERT_TIME_RESP); |
| 799 | } |
| 800 | |
Willy Tarreau | d3007ff | 2011-09-05 02:07:23 +0200 | [diff] [blame] | 801 | if (filter & (FILT_ERRORS_ONLY | FILT_HTTP_STATUS)) { |
| 802 | /* Check both error codes (-1, 5xx) and status code ranges */ |
Willy Tarreau | 26deaf5 | 2011-07-10 19:47:48 +0200 | [diff] [blame] | 803 | if (time_field) |
| 804 | b = field_start(time_field, STATUS_FIELD - TIME_FIELD + 1); |
| 805 | else |
| 806 | b = field_start(accept_field, STATUS_FIELD - ACCEPT_FIELD + 1); |
| 807 | |
Willy Tarreau | a2b39fb | 2011-07-10 21:39:35 +0200 | [diff] [blame] | 808 | if (unlikely(!*b)) { |
Willy Tarreau | 72c2853 | 2009-01-22 18:56:50 +0100 | [diff] [blame] | 809 | truncated_line(linenum, line); |
| 810 | continue; |
| 811 | } |
Willy Tarreau | a2b39fb | 2011-07-10 21:39:35 +0200 | [diff] [blame] | 812 | |
Willy Tarreau | d3007ff | 2011-09-05 02:07:23 +0200 | [diff] [blame] | 813 | val = str2ic(b); |
| 814 | if (filter & FILT_ERRORS_ONLY) |
| 815 | test &= (val < 0 || (val >= 500 && val <= 599)) ^ !!(filter & FILT_INVERT_ERRORS); |
| 816 | |
| 817 | if (filter & FILT_HTTP_STATUS) |
| 818 | test &= (val >= filt_http_status_low && val <= filt_http_status_high) ^ !!(filter & FILT_INVERT_HTTP_STATUS); |
Willy Tarreau | 72c2853 | 2009-01-22 18:56:50 +0100 | [diff] [blame] | 819 | } |
| 820 | |
Willy Tarreau | 08911ff | 2011-10-13 13:28:36 +0200 | [diff] [blame] | 821 | if (filter & (FILT_QUEUE_ONLY|FILT_QUEUE_SRV_ONLY)) { |
| 822 | /* Check if the server's queue is non-nul */ |
| 823 | if (time_field) |
| 824 | b = field_start(time_field, QUEUE_LEN_FIELD - TIME_FIELD + 1); |
| 825 | else |
| 826 | b = field_start(accept_field, QUEUE_LEN_FIELD - ACCEPT_FIELD + 1); |
| 827 | |
| 828 | if (unlikely(!*b)) { |
| 829 | truncated_line(linenum, line); |
| 830 | continue; |
| 831 | } |
| 832 | |
| 833 | if (*b == '0') { |
| 834 | if (filter & FILT_QUEUE_SRV_ONLY) { |
| 835 | test = 0; |
| 836 | } |
| 837 | else { |
| 838 | do { |
| 839 | b++; |
| 840 | if (*b == '/') { |
| 841 | b++; |
| 842 | break; |
| 843 | } |
| 844 | } while (*b); |
| 845 | test &= ((unsigned char)(*b - '1') < 9); |
| 846 | } |
| 847 | } |
| 848 | } |
| 849 | |
Hervé COMMOWICK | 927cddd | 2011-08-10 17:42:41 +0200 | [diff] [blame] | 850 | if (filter & FILT_TERM_CODE_NAME) { |
| 851 | /* only report corresponding termination code name */ |
| 852 | if (time_field) |
| 853 | b = field_start(time_field, TERM_CODES_FIELD - TIME_FIELD + 1); |
| 854 | else |
| 855 | b = field_start(accept_field, TERM_CODES_FIELD - ACCEPT_FIELD + 1); |
| 856 | |
| 857 | if (unlikely(!*b)) { |
| 858 | truncated_line(linenum, line); |
| 859 | continue; |
| 860 | } |
| 861 | |
| 862 | test &= (b[0] == filter_term_code_name[0] && b[1] == filter_term_code_name[1]) ^ !!(filter & FILT_INVERT_TERM_CODE_NAME); |
| 863 | } |
| 864 | |
| 865 | |
Willy Tarreau | 0f423a7 | 2010-05-03 10:50:54 +0200 | [diff] [blame] | 866 | test ^= filter_invert; |
| 867 | if (!test) |
| 868 | continue; |
| 869 | |
Willy Tarreau | a2b39fb | 2011-07-10 21:39:35 +0200 | [diff] [blame] | 870 | /************** here we process inputs *******************/ |
Willy Tarreau | 72c2853 | 2009-01-22 18:56:50 +0100 | [diff] [blame] | 871 | |
Willy Tarreau | a2b39fb | 2011-07-10 21:39:35 +0200 | [diff] [blame] | 872 | if (line_filter) |
| 873 | line_filter(accept_field, time_field, &t); |
| 874 | else |
Willy Tarreau | a1629a5 | 2012-11-13 20:48:15 +0100 | [diff] [blame] | 875 | lines_out++; /* FILT_COUNT_ONLY was used, so we're just counting lines */ |
| 876 | if (lines_max >= 0 && lines_out >= lines_max) |
Willy Tarreau | 667c905 | 2012-10-10 16:49:28 +0200 | [diff] [blame] | 877 | break; |
Willy Tarreau | a2b39fb | 2011-07-10 21:39:35 +0200 | [diff] [blame] | 878 | } |
Willy Tarreau | abe45b6 | 2010-10-28 20:33:46 +0200 | [diff] [blame] | 879 | |
Willy Tarreau | e1a908c | 2012-01-03 09:23:03 +0100 | [diff] [blame] | 880 | skip_filters: |
Willy Tarreau | a2b39fb | 2011-07-10 21:39:35 +0200 | [diff] [blame] | 881 | /***************************************************** |
| 882 | * Here we've finished reading all input. Depending on the |
| 883 | * filters, we may still have some analysis to run on the |
| 884 | * collected data and to output data in a new format. |
| 885 | *************************************************** */ |
Willy Tarreau | 72c2853 | 2009-01-22 18:56:50 +0100 | [diff] [blame] | 886 | |
| 887 | if (t) |
| 888 | free(t); |
| 889 | |
| 890 | if (filter & FILT_COUNT_ONLY) { |
Willy Tarreau | a2b39fb | 2011-07-10 21:39:35 +0200 | [diff] [blame] | 891 | printf("%d\n", lines_out); |
Willy Tarreau | 72c2853 | 2009-01-22 18:56:50 +0100 | [diff] [blame] | 892 | exit(0); |
| 893 | } |
| 894 | |
Willy Tarreau | 72c2853 | 2009-01-22 18:56:50 +0100 | [diff] [blame] | 895 | if (filter & (FILT_ACC_COUNT|FILT_ACC_DELAY)) { |
| 896 | /* sort and count all timers. Output will look like this : |
| 897 | * <accept_date> <delta_ms from previous one> <nb entries> |
| 898 | */ |
| 899 | n = eb32_first(&timers[0]); |
| 900 | |
| 901 | if (n) |
| 902 | last = n->key; |
| 903 | while (n) { |
| 904 | unsigned int d, h, m, s, ms; |
| 905 | |
| 906 | t = container_of(n, struct timer, node); |
| 907 | h = n->key; |
| 908 | d = h - last; |
| 909 | last = h; |
| 910 | |
| 911 | if (d >= filter_acc_delay && t->count >= filter_acc_count) { |
| 912 | ms = h % 1000; h = h / 1000; |
| 913 | s = h % 60; h = h / 60; |
| 914 | m = h % 60; h = h / 60; |
Willy Tarreau | 72c2853 | 2009-01-22 18:56:50 +0100 | [diff] [blame] | 915 | printf("%02d:%02d:%02d.%03d %d %d %d\n", h, m, s, ms, last, d, t->count); |
Willy Tarreau | 667c905 | 2012-10-10 16:49:28 +0200 | [diff] [blame] | 916 | lines_out++; |
Willy Tarreau | a1629a5 | 2012-11-13 20:48:15 +0100 | [diff] [blame] | 917 | if (lines_max >= 0 && lines_out >= lines_max) |
Willy Tarreau | 667c905 | 2012-10-10 16:49:28 +0200 | [diff] [blame] | 918 | break; |
Willy Tarreau | 72c2853 | 2009-01-22 18:56:50 +0100 | [diff] [blame] | 919 | } |
| 920 | n = eb32_next(n); |
| 921 | } |
| 922 | } |
| 923 | else if (filter & FILT_GRAPH_TIMERS) { |
| 924 | /* sort all timers */ |
| 925 | for (f = 0; f < 5; f++) { |
| 926 | struct eb32_node *n; |
| 927 | int val; |
| 928 | |
| 929 | val = 0; |
| 930 | n = eb32_first(&timers[f]); |
| 931 | while (n) { |
| 932 | int i; |
| 933 | double d; |
| 934 | |
| 935 | t = container_of(n, struct timer, node); |
| 936 | last = n->key; |
| 937 | val = t->count; |
| 938 | |
| 939 | i = (last < 0) ? -last : last; |
| 940 | i = fls_auto(i) - QBITS; |
| 941 | |
| 942 | if (i > 0) |
| 943 | d = val / (double)(1 << i); |
| 944 | else |
| 945 | d = val; |
| 946 | |
Willy Tarreau | a1629a5 | 2012-11-13 20:48:15 +0100 | [diff] [blame] | 947 | if (d > 0.0) |
Willy Tarreau | 72c2853 | 2009-01-22 18:56:50 +0100 | [diff] [blame] | 948 | printf("%d %d %f\n", f, last, d+1.0); |
Willy Tarreau | 72c2853 | 2009-01-22 18:56:50 +0100 | [diff] [blame] | 949 | |
| 950 | n = eb32_next(n); |
| 951 | } |
Willy Tarreau | 214c203 | 2009-02-20 11:02:32 +0100 | [diff] [blame] | 952 | } |
| 953 | } |
| 954 | else if (filter & FILT_PERCENTILE) { |
| 955 | /* report timers by percentile : |
| 956 | * <percent> <total> <max_req_time> <max_conn_time> <max_resp_time> <max_data_time> |
| 957 | * We don't count errs. |
| 958 | */ |
| 959 | struct eb32_node *n[5]; |
| 960 | unsigned long cum[5]; |
| 961 | double step; |
| 962 | |
Willy Tarreau | a2b39fb | 2011-07-10 21:39:35 +0200 | [diff] [blame] | 963 | if (!lines_out) |
Willy Tarreau | 910ba4b | 2009-11-17 10:16:19 +0100 | [diff] [blame] | 964 | goto empty; |
| 965 | |
Willy Tarreau | 214c203 | 2009-02-20 11:02:32 +0100 | [diff] [blame] | 966 | for (f = 1; f < 5; f++) { |
| 967 | n[f] = eb32_first(&timers[f]); |
| 968 | cum[f] = container_of(n[f], struct timer, node)->count; |
| 969 | } |
| 970 | |
| 971 | for (step = 1; step <= 1000;) { |
Willy Tarreau | a2b39fb | 2011-07-10 21:39:35 +0200 | [diff] [blame] | 972 | unsigned int thres = lines_out * (step / 1000.0); |
Willy Tarreau | 214c203 | 2009-02-20 11:02:32 +0100 | [diff] [blame] | 973 | |
| 974 | printf("%3.1f %d ", step/10.0, thres); |
| 975 | for (f = 1; f < 5; f++) { |
| 976 | struct eb32_node *next; |
| 977 | while (cum[f] < thres) { |
| 978 | /* need to find other keys */ |
| 979 | next = eb32_next(n[f]); |
| 980 | if (!next) |
| 981 | break; |
| 982 | n[f] = next; |
| 983 | cum[f] += container_of(next, struct timer, node)->count; |
| 984 | } |
| 985 | |
| 986 | /* value still within $step % of total */ |
| 987 | printf("%d ", n[f]->key); |
| 988 | } |
| 989 | putchar('\n'); |
| 990 | if (step >= 100 && step < 900) |
| 991 | step += 50; // jump 5% by 5% between those steps. |
| 992 | else if (step >= 20 && step < 980) |
| 993 | step += 10; |
| 994 | else |
| 995 | step += 1; |
Willy Tarreau | 72c2853 | 2009-01-22 18:56:50 +0100 | [diff] [blame] | 996 | } |
| 997 | } |
Willy Tarreau | 0f423a7 | 2010-05-03 10:50:54 +0200 | [diff] [blame] | 998 | else if (filter & FILT_COUNT_STATUS) { |
| 999 | /* output all statuses in the form of <status> <occurrences> */ |
| 1000 | n = eb32_first(&timers[0]); |
| 1001 | while (n) { |
| 1002 | t = container_of(n, struct timer, node); |
| 1003 | printf("%d %d\n", n->key, t->count); |
Willy Tarreau | a2b39fb | 2011-07-10 21:39:35 +0200 | [diff] [blame] | 1004 | lines_out++; |
Willy Tarreau | a1629a5 | 2012-11-13 20:48:15 +0100 | [diff] [blame] | 1005 | if (lines_max >= 0 && lines_out >= lines_max) |
Willy Tarreau | 667c905 | 2012-10-10 16:49:28 +0200 | [diff] [blame] | 1006 | break; |
Willy Tarreau | 0f423a7 | 2010-05-03 10:50:54 +0200 | [diff] [blame] | 1007 | n = eb32_next(n); |
| 1008 | } |
| 1009 | } |
Willy Tarreau | a2b39fb | 2011-07-10 21:39:35 +0200 | [diff] [blame] | 1010 | else if (filter & FILT_COUNT_SRV_STATUS) { |
Willy Tarreau | d220106 | 2010-05-27 18:17:30 +0200 | [diff] [blame] | 1011 | struct ebmb_node *srv_node; |
| 1012 | struct srv_st *srv; |
| 1013 | |
| 1014 | printf("#srv_name 1xx 2xx 3xx 4xx 5xx other tot_req req_ok pct_ok avg_ct avg_rt\n"); |
| 1015 | |
| 1016 | srv_node = ebmb_first(&timers[0]); |
| 1017 | while (srv_node) { |
| 1018 | int tot_rq; |
| 1019 | |
| 1020 | srv = container_of(srv_node, struct srv_st, node); |
| 1021 | |
| 1022 | tot_rq = 0; |
| 1023 | for (f = 0; f <= 5; f++) |
| 1024 | tot_rq += srv->st_cnt[f]; |
| 1025 | |
| 1026 | printf("%s %d %d %d %d %d %d %d %d %.1f %d %d\n", |
| 1027 | srv_node->key, srv->st_cnt[1], srv->st_cnt[2], |
| 1028 | srv->st_cnt[3], srv->st_cnt[4], srv->st_cnt[5], srv->st_cnt[0], |
| 1029 | tot_rq, |
| 1030 | srv->nb_ok, (double)srv->nb_ok * 100.0 / (tot_rq?tot_rq:1), |
| 1031 | (int)(srv->cum_ct / (srv->nb_ct?srv->nb_ct:1)), (int)(srv->cum_rt / (srv->nb_rt?srv->nb_rt:1))); |
| 1032 | srv_node = ebmb_next(srv_node); |
Willy Tarreau | a2b39fb | 2011-07-10 21:39:35 +0200 | [diff] [blame] | 1033 | lines_out++; |
Willy Tarreau | a1629a5 | 2012-11-13 20:48:15 +0100 | [diff] [blame] | 1034 | if (lines_max >= 0 && lines_out >= lines_max) |
Willy Tarreau | 667c905 | 2012-10-10 16:49:28 +0200 | [diff] [blame] | 1035 | break; |
Willy Tarreau | d220106 | 2010-05-27 18:17:30 +0200 | [diff] [blame] | 1036 | } |
| 1037 | } |
Willy Tarreau | 8a09b66 | 2012-10-10 10:26:22 +0200 | [diff] [blame] | 1038 | else if (filter & (FILT_COUNT_TERM_CODES|FILT_COUNT_COOK_CODES)) { |
Willy Tarreau | d8fc110 | 2010-09-12 17:56:16 +0200 | [diff] [blame] | 1039 | /* output all statuses in the form of <code> <occurrences> */ |
| 1040 | n = eb32_first(&timers[0]); |
| 1041 | while (n) { |
| 1042 | t = container_of(n, struct timer, node); |
| 1043 | printf("%c%c %d\n", (n->key >> 8), (n->key) & 255, t->count); |
Willy Tarreau | a2b39fb | 2011-07-10 21:39:35 +0200 | [diff] [blame] | 1044 | lines_out++; |
Willy Tarreau | a1629a5 | 2012-11-13 20:48:15 +0100 | [diff] [blame] | 1045 | if (lines_max >= 0 && lines_out >= lines_max) |
Willy Tarreau | 667c905 | 2012-10-10 16:49:28 +0200 | [diff] [blame] | 1046 | break; |
Willy Tarreau | d8fc110 | 2010-09-12 17:56:16 +0200 | [diff] [blame] | 1047 | n = eb32_next(n); |
| 1048 | } |
| 1049 | } |
Willy Tarreau | a2b39fb | 2011-07-10 21:39:35 +0200 | [diff] [blame] | 1050 | else if (filter & FILT_COUNT_URL_ANY) { |
Willy Tarreau | abe45b6 | 2010-10-28 20:33:46 +0200 | [diff] [blame] | 1051 | struct eb_node *node, *next; |
| 1052 | |
| 1053 | if (!(filter & FILT_COUNT_URL_ONLY)) { |
| 1054 | /* we have to sort on another criterion. We'll use timers[1] for the |
| 1055 | * destination tree. |
| 1056 | */ |
| 1057 | |
| 1058 | timers[1] = EB_ROOT; /* reconfigure to accept duplicates */ |
| 1059 | for (node = eb_first(&timers[0]); node; node = next) { |
| 1060 | next = eb_next(node); |
| 1061 | eb_delete(node); |
| 1062 | |
| 1063 | ustat = container_of(node, struct url_stat, node.url.node); |
| 1064 | |
| 1065 | if (filter & FILT_COUNT_URL_COUNT) |
| 1066 | ustat->node.val.key = ustat->nb_req; |
| 1067 | else if (filter & FILT_COUNT_URL_ERR) |
| 1068 | ustat->node.val.key = ustat->nb_err; |
| 1069 | else if (filter & FILT_COUNT_URL_TTOT) |
| 1070 | ustat->node.val.key = ustat->total_time; |
| 1071 | else if (filter & FILT_COUNT_URL_TAVG) |
| 1072 | ustat->node.val.key = ustat->nb_req ? ustat->total_time / ustat->nb_req : 0; |
| 1073 | else if (filter & FILT_COUNT_URL_TTOTO) |
| 1074 | ustat->node.val.key = ustat->total_time_ok; |
| 1075 | else if (filter & FILT_COUNT_URL_TAVGO) |
| 1076 | ustat->node.val.key = (ustat->nb_req - ustat->nb_err) ? ustat->total_time_ok / (ustat->nb_req - ustat->nb_err) : 0; |
Baptiste | 61aaad0 | 2012-09-08 23:10:03 +0200 | [diff] [blame] | 1077 | else if (filter & FILT_COUNT_URL_BAVG) |
| 1078 | ustat->node.val.key = ustat->nb_req ? ustat->total_bytes_sent / ustat->nb_req : 0; |
| 1079 | else if (filter & FILT_COUNT_URL_BTOT) |
| 1080 | ustat->node.val.key = ustat->total_bytes_sent; |
Willy Tarreau | abe45b6 | 2010-10-28 20:33:46 +0200 | [diff] [blame] | 1081 | else |
| 1082 | ustat->node.val.key = 0; |
| 1083 | |
| 1084 | eb64_insert(&timers[1], &ustat->node.val); |
| 1085 | } |
| 1086 | /* switch trees */ |
| 1087 | timers[0] = timers[1]; |
| 1088 | } |
| 1089 | |
Baptiste | 61aaad0 | 2012-09-08 23:10:03 +0200 | [diff] [blame] | 1090 | printf("#req err ttot tavg oktot okavg bavg btot url\n"); |
Willy Tarreau | abe45b6 | 2010-10-28 20:33:46 +0200 | [diff] [blame] | 1091 | |
| 1092 | /* scan the tree in its reverse sorting order */ |
| 1093 | node = eb_last(&timers[0]); |
| 1094 | while (node) { |
| 1095 | ustat = container_of(node, struct url_stat, node.url.node); |
Baptiste | 61aaad0 | 2012-09-08 23:10:03 +0200 | [diff] [blame] | 1096 | printf("%d %d %Ld %Ld %Ld %Ld %Ld %Ld %s\n", |
Willy Tarreau | abe45b6 | 2010-10-28 20:33:46 +0200 | [diff] [blame] | 1097 | ustat->nb_req, |
| 1098 | ustat->nb_err, |
| 1099 | ustat->total_time, |
| 1100 | ustat->nb_req ? ustat->total_time / ustat->nb_req : 0, |
| 1101 | ustat->total_time_ok, |
| 1102 | (ustat->nb_req - ustat->nb_err) ? ustat->total_time_ok / (ustat->nb_req - ustat->nb_err) : 0, |
Baptiste | 61aaad0 | 2012-09-08 23:10:03 +0200 | [diff] [blame] | 1103 | ustat->nb_req ? ustat->total_bytes_sent / ustat->nb_req : 0, |
| 1104 | ustat->total_bytes_sent, |
Willy Tarreau | abe45b6 | 2010-10-28 20:33:46 +0200 | [diff] [blame] | 1105 | ustat->url); |
| 1106 | |
| 1107 | node = eb_prev(node); |
Willy Tarreau | a2b39fb | 2011-07-10 21:39:35 +0200 | [diff] [blame] | 1108 | lines_out++; |
Willy Tarreau | a1629a5 | 2012-11-13 20:48:15 +0100 | [diff] [blame] | 1109 | if (lines_max >= 0 && lines_out >= lines_max) |
Willy Tarreau | 667c905 | 2012-10-10 16:49:28 +0200 | [diff] [blame] | 1110 | break; |
Willy Tarreau | abe45b6 | 2010-10-28 20:33:46 +0200 | [diff] [blame] | 1111 | } |
| 1112 | } |
Willy Tarreau | d220106 | 2010-05-27 18:17:30 +0200 | [diff] [blame] | 1113 | |
Willy Tarreau | 910ba4b | 2009-11-17 10:16:19 +0100 | [diff] [blame] | 1114 | empty: |
Willy Tarreau | 72c2853 | 2009-01-22 18:56:50 +0100 | [diff] [blame] | 1115 | if (!(filter & FILT_QUIET)) |
| 1116 | fprintf(stderr, "%d lines in, %d lines out, %d parsing errors\n", |
Willy Tarreau | a2b39fb | 2011-07-10 21:39:35 +0200 | [diff] [blame] | 1117 | linenum, lines_out, parse_err); |
Willy Tarreau | 72c2853 | 2009-01-22 18:56:50 +0100 | [diff] [blame] | 1118 | exit(0); |
| 1119 | } |
| 1120 | |
Willy Tarreau | a2b39fb | 2011-07-10 21:39:35 +0200 | [diff] [blame] | 1121 | void filter_output_line(const char *accept_field, const char *time_field, struct timer **tptr) |
| 1122 | { |
| 1123 | puts(line); |
| 1124 | lines_out++; |
| 1125 | } |
| 1126 | |
| 1127 | void filter_accept_holes(const char *accept_field, const char *time_field, struct timer **tptr) |
| 1128 | { |
| 1129 | struct timer *t2; |
| 1130 | int val; |
| 1131 | |
| 1132 | val = convert_date(accept_field); |
| 1133 | if (unlikely(val < 0)) { |
| 1134 | truncated_line(linenum, line); |
| 1135 | return; |
| 1136 | } |
| 1137 | |
| 1138 | t2 = insert_value(&timers[0], tptr, val); |
| 1139 | t2->count++; |
Willy Tarreau | a2b39fb | 2011-07-10 21:39:35 +0200 | [diff] [blame] | 1140 | return; |
| 1141 | } |
| 1142 | |
| 1143 | void filter_count_status(const char *accept_field, const char *time_field, struct timer **tptr) |
| 1144 | { |
| 1145 | struct timer *t2; |
| 1146 | const char *b; |
| 1147 | int val; |
| 1148 | |
| 1149 | if (time_field) |
| 1150 | b = field_start(time_field, STATUS_FIELD - TIME_FIELD + 1); |
| 1151 | else |
| 1152 | b = field_start(accept_field, STATUS_FIELD - ACCEPT_FIELD + 1); |
| 1153 | |
| 1154 | if (unlikely(!*b)) { |
| 1155 | truncated_line(linenum, line); |
| 1156 | return; |
| 1157 | } |
| 1158 | |
| 1159 | val = str2ic(b); |
| 1160 | |
| 1161 | t2 = insert_value(&timers[0], tptr, val); |
| 1162 | t2->count++; |
| 1163 | } |
| 1164 | |
Willy Tarreau | 8a09b66 | 2012-10-10 10:26:22 +0200 | [diff] [blame] | 1165 | void filter_count_cook_codes(const char *accept_field, const char *time_field, struct timer **tptr) |
| 1166 | { |
| 1167 | struct timer *t2; |
| 1168 | const char *b; |
| 1169 | int val; |
| 1170 | |
| 1171 | if (time_field) |
| 1172 | b = field_start(time_field, TERM_CODES_FIELD - TIME_FIELD + 1); |
| 1173 | else |
| 1174 | b = field_start(accept_field, TERM_CODES_FIELD - ACCEPT_FIELD + 1); |
| 1175 | |
| 1176 | if (unlikely(!*b)) { |
| 1177 | truncated_line(linenum, line); |
| 1178 | return; |
| 1179 | } |
| 1180 | |
| 1181 | val = 256 * b[2] + b[3]; |
| 1182 | |
| 1183 | t2 = insert_value(&timers[0], tptr, val); |
| 1184 | t2->count++; |
| 1185 | } |
| 1186 | |
Willy Tarreau | a2b39fb | 2011-07-10 21:39:35 +0200 | [diff] [blame] | 1187 | void filter_count_term_codes(const char *accept_field, const char *time_field, struct timer **tptr) |
| 1188 | { |
| 1189 | struct timer *t2; |
| 1190 | const char *b; |
| 1191 | int val; |
| 1192 | |
| 1193 | if (time_field) |
| 1194 | b = field_start(time_field, TERM_CODES_FIELD - TIME_FIELD + 1); |
| 1195 | else |
| 1196 | b = field_start(accept_field, TERM_CODES_FIELD - ACCEPT_FIELD + 1); |
| 1197 | |
| 1198 | if (unlikely(!*b)) { |
| 1199 | truncated_line(linenum, line); |
| 1200 | return; |
| 1201 | } |
| 1202 | |
| 1203 | val = 256 * b[0] + b[1]; |
| 1204 | |
| 1205 | t2 = insert_value(&timers[0], tptr, val); |
| 1206 | t2->count++; |
| 1207 | } |
| 1208 | |
| 1209 | void filter_count_srv_status(const char *accept_field, const char *time_field, struct timer **tptr) |
| 1210 | { |
| 1211 | const char *b, *e, *p; |
| 1212 | int f, err, array[5]; |
| 1213 | struct ebmb_node *srv_node; |
| 1214 | struct srv_st *srv; |
| 1215 | int val; |
| 1216 | |
| 1217 | /* the server field is before the status field, so let's |
| 1218 | * parse them in the proper order. |
| 1219 | */ |
| 1220 | b = field_start(accept_field, SERVER_FIELD - ACCEPT_FIELD + 1); |
| 1221 | if (unlikely(!*b)) { |
| 1222 | truncated_line(linenum, line); |
| 1223 | return; |
| 1224 | } |
| 1225 | |
| 1226 | e = field_stop(b + 1); /* we have the server name in [b]..[e-1] */ |
| 1227 | |
| 1228 | /* the chance that a server name already exists is extremely high, |
| 1229 | * so let's perform a normal lookup first. |
| 1230 | */ |
| 1231 | srv_node = ebst_lookup_len(&timers[0], b, e - b); |
| 1232 | srv = container_of(srv_node, struct srv_st, node); |
| 1233 | |
| 1234 | if (!srv_node) { |
| 1235 | /* server not yet in the tree, let's create it */ |
| 1236 | srv = (void *)calloc(1, sizeof(struct srv_st) + e - b + 1); |
| 1237 | srv_node = &srv->node; |
| 1238 | memcpy(&srv_node->key, b, e - b); |
| 1239 | srv_node->key[e - b] = '\0'; |
| 1240 | ebst_insert(&timers[0], srv_node); |
| 1241 | } |
| 1242 | |
| 1243 | /* let's collect the connect and response times */ |
| 1244 | if (!time_field) { |
| 1245 | time_field = field_start(e, TIME_FIELD - SERVER_FIELD); |
| 1246 | if (unlikely(!*time_field)) { |
| 1247 | truncated_line(linenum, line); |
| 1248 | return; |
| 1249 | } |
| 1250 | } |
| 1251 | |
| 1252 | e = field_stop(time_field + 1); |
| 1253 | /* we have field TIME_FIELD in [time_field]..[e-1] */ |
| 1254 | |
| 1255 | p = time_field; |
| 1256 | err = 0; |
| 1257 | f = 0; |
| 1258 | while (!SEP(*p)) { |
| 1259 | array[f] = str2ic(p); |
| 1260 | if (array[f] < 0) { |
| 1261 | array[f] = -1; |
| 1262 | err = 1; |
| 1263 | } |
| 1264 | if (++f == 5) |
| 1265 | break; |
| 1266 | SKIP_CHAR(p, '/'); |
| 1267 | } |
| 1268 | |
| 1269 | if (unlikely(f < 5)){ |
| 1270 | parse_err++; |
| 1271 | return; |
| 1272 | } |
| 1273 | |
| 1274 | /* OK we have our timers in array[2,3] */ |
| 1275 | if (!err) |
| 1276 | srv->nb_ok++; |
| 1277 | |
| 1278 | if (array[2] >= 0) { |
| 1279 | srv->cum_ct += array[2]; |
| 1280 | srv->nb_ct++; |
| 1281 | } |
| 1282 | |
| 1283 | if (array[3] >= 0) { |
| 1284 | srv->cum_rt += array[3]; |
| 1285 | srv->nb_rt++; |
| 1286 | } |
| 1287 | |
| 1288 | /* we're interested in the 5 HTTP status classes (1xx ... 5xx), and |
| 1289 | * the invalid ones which will be reported as 0. |
| 1290 | */ |
| 1291 | b = field_start(e, STATUS_FIELD - TIME_FIELD); |
| 1292 | if (unlikely(!*b)) { |
| 1293 | truncated_line(linenum, line); |
| 1294 | return; |
| 1295 | } |
| 1296 | |
| 1297 | val = 0; |
| 1298 | if (*b >= '1' && *b <= '5') |
| 1299 | val = *b - '0'; |
| 1300 | |
| 1301 | srv->st_cnt[val]++; |
| 1302 | } |
| 1303 | |
| 1304 | void filter_count_url(const char *accept_field, const char *time_field, struct timer **tptr) |
| 1305 | { |
| 1306 | struct url_stat *ustat = NULL; |
| 1307 | struct ebpt_node *ebpt_old; |
| 1308 | const char *b, *e; |
| 1309 | int f, err, array[5]; |
Baptiste | 61aaad0 | 2012-09-08 23:10:03 +0200 | [diff] [blame] | 1310 | int val; |
Willy Tarreau | a2b39fb | 2011-07-10 21:39:35 +0200 | [diff] [blame] | 1311 | |
| 1312 | /* let's collect the response time */ |
| 1313 | if (!time_field) { |
| 1314 | time_field = field_start(accept_field, TIME_FIELD - ACCEPT_FIELD + 1); // avg 115 ns per line |
| 1315 | if (unlikely(!*time_field)) { |
| 1316 | truncated_line(linenum, line); |
| 1317 | return; |
| 1318 | } |
| 1319 | } |
| 1320 | |
| 1321 | /* we have the field TIME_FIELD starting at <time_field>. We'll |
| 1322 | * parse the 5 timers to detect errors, it takes avg 55 ns per line. |
| 1323 | */ |
| 1324 | e = time_field; err = 0; f = 0; |
| 1325 | while (!SEP(*e)) { |
| 1326 | array[f] = str2ic(e); |
| 1327 | if (array[f] < 0) { |
| 1328 | array[f] = -1; |
| 1329 | err = 1; |
| 1330 | } |
| 1331 | if (++f == 5) |
| 1332 | break; |
| 1333 | SKIP_CHAR(e, '/'); |
| 1334 | } |
| 1335 | if (f < 5) { |
| 1336 | parse_err++; |
| 1337 | return; |
| 1338 | } |
| 1339 | |
| 1340 | /* OK we have our timers in array[3], and err is >0 if at |
| 1341 | * least one -1 was seen. <e> points to the first char of |
| 1342 | * the last timer. Let's prepare a new node with that. |
| 1343 | */ |
| 1344 | if (unlikely(!ustat)) |
| 1345 | ustat = calloc(1, sizeof(*ustat)); |
| 1346 | |
| 1347 | ustat->nb_err = err; |
| 1348 | ustat->nb_req = 1; |
| 1349 | |
| 1350 | /* use array[4] = total time in case of error */ |
| 1351 | ustat->total_time = (array[3] >= 0) ? array[3] : array[4]; |
| 1352 | ustat->total_time_ok = (array[3] >= 0) ? array[3] : 0; |
| 1353 | |
Baptiste | 61aaad0 | 2012-09-08 23:10:03 +0200 | [diff] [blame] | 1354 | e = field_start(e, BYTES_SENT_FIELD - TIME_FIELD + 1); |
| 1355 | val = str2ic(e); |
| 1356 | ustat->total_bytes_sent = val; |
| 1357 | |
Willy Tarreau | a2b39fb | 2011-07-10 21:39:35 +0200 | [diff] [blame] | 1358 | /* the line may be truncated because of a bad request or anything like this, |
| 1359 | * without a method. Also, if it does not begin with an quote, let's skip to |
| 1360 | * the next field because it's a capture. Let's fall back to the "method" itself |
| 1361 | * if there's nothing else. |
| 1362 | */ |
Baptiste | 61aaad0 | 2012-09-08 23:10:03 +0200 | [diff] [blame] | 1363 | e = field_start(e, METH_FIELD - BYTES_SENT_FIELD + 1); |
Willy Tarreau | 61a40c7 | 2011-09-06 08:11:27 +0200 | [diff] [blame] | 1364 | while (*e != '"' && *e) { |
| 1365 | /* Note: some syslog servers escape quotes ! */ |
| 1366 | if (*e == '\\' && e[1] == '"') |
| 1367 | break; |
Willy Tarreau | a2b39fb | 2011-07-10 21:39:35 +0200 | [diff] [blame] | 1368 | e = field_start(e, 2); |
Willy Tarreau | 61a40c7 | 2011-09-06 08:11:27 +0200 | [diff] [blame] | 1369 | } |
Willy Tarreau | a2b39fb | 2011-07-10 21:39:35 +0200 | [diff] [blame] | 1370 | |
| 1371 | if (unlikely(!*e)) { |
| 1372 | truncated_line(linenum, line); |
| 1373 | return; |
| 1374 | } |
| 1375 | |
| 1376 | b = field_start(e, URL_FIELD - METH_FIELD + 1); // avg 40 ns per line |
| 1377 | if (!*b) |
| 1378 | b = e; |
| 1379 | |
| 1380 | /* stop at end of field or first ';' or '?', takes avg 64 ns per line */ |
| 1381 | e = b; |
| 1382 | do { |
Willy Tarreau | 14389e7 | 2011-07-10 22:11:17 +0200 | [diff] [blame] | 1383 | if (*e == ' ' || *e == '?' || *e == ';') { |
Willy Tarreau | a2b39fb | 2011-07-10 21:39:35 +0200 | [diff] [blame] | 1384 | *(char *)e = 0; |
| 1385 | break; |
| 1386 | } |
| 1387 | e++; |
| 1388 | } while (*e); |
| 1389 | |
| 1390 | /* now instead of copying the URL for a simple lookup, we'll link |
| 1391 | * to it from the node we're trying to insert. If it returns a |
| 1392 | * different value, it was already there. Otherwise we just have |
| 1393 | * to dynamically realloc an entry using strdup(). |
| 1394 | */ |
| 1395 | ustat->node.url.key = (char *)b; |
| 1396 | ebpt_old = ebis_insert(&timers[0], &ustat->node.url); |
| 1397 | |
| 1398 | if (ebpt_old != &ustat->node.url) { |
| 1399 | struct url_stat *ustat_old; |
| 1400 | /* node was already there, let's update previous one */ |
| 1401 | ustat_old = container_of(ebpt_old, struct url_stat, node.url); |
| 1402 | ustat_old->nb_req ++; |
| 1403 | ustat_old->nb_err += ustat->nb_err; |
| 1404 | ustat_old->total_time += ustat->total_time; |
| 1405 | ustat_old->total_time_ok += ustat->total_time_ok; |
Baptiste | 61aaad0 | 2012-09-08 23:10:03 +0200 | [diff] [blame] | 1406 | ustat_old->total_bytes_sent += ustat->total_bytes_sent; |
Willy Tarreau | a2b39fb | 2011-07-10 21:39:35 +0200 | [diff] [blame] | 1407 | } else { |
| 1408 | ustat->url = ustat->node.url.key = strdup(ustat->node.url.key); |
| 1409 | ustat = NULL; /* node was used */ |
| 1410 | } |
| 1411 | } |
| 1412 | |
| 1413 | void filter_graphs(const char *accept_field, const char *time_field, struct timer **tptr) |
| 1414 | { |
| 1415 | struct timer *t2; |
| 1416 | const char *e, *p; |
| 1417 | int f, err, array[5]; |
| 1418 | |
| 1419 | if (!time_field) { |
| 1420 | time_field = field_start(accept_field, TIME_FIELD - ACCEPT_FIELD + 1); |
| 1421 | if (unlikely(!*time_field)) { |
| 1422 | truncated_line(linenum, line); |
| 1423 | return; |
| 1424 | } |
| 1425 | } |
| 1426 | |
| 1427 | e = field_stop(time_field + 1); |
| 1428 | /* we have field TIME_FIELD in [time_field]..[e-1] */ |
| 1429 | |
| 1430 | p = time_field; |
| 1431 | err = 0; |
| 1432 | f = 0; |
| 1433 | while (!SEP(*p)) { |
| 1434 | array[f] = str2ic(p); |
| 1435 | if (array[f] < 0) { |
| 1436 | array[f] = -1; |
| 1437 | err = 1; |
| 1438 | } |
| 1439 | if (++f == 5) |
| 1440 | break; |
| 1441 | SKIP_CHAR(p, '/'); |
| 1442 | } |
| 1443 | |
| 1444 | if (unlikely(f < 5)) { |
| 1445 | parse_err++; |
| 1446 | return; |
| 1447 | } |
| 1448 | |
| 1449 | /* if we find at least one negative time, we count one error |
| 1450 | * with a time equal to the total session time. This will |
| 1451 | * emphasize quantum timing effects associated to known |
| 1452 | * timeouts. Note that on some buggy machines, it is possible |
| 1453 | * that the total time is negative, hence the reason to reset |
| 1454 | * it. |
| 1455 | */ |
| 1456 | |
| 1457 | if (filter & FILT_GRAPH_TIMERS) { |
| 1458 | if (err) { |
| 1459 | if (array[4] < 0) |
| 1460 | array[4] = -1; |
| 1461 | t2 = insert_timer(&timers[0], tptr, array[4]); // total time |
| 1462 | t2->count++; |
| 1463 | } else { |
| 1464 | int v; |
| 1465 | |
| 1466 | t2 = insert_timer(&timers[1], tptr, array[0]); t2->count++; // req |
| 1467 | t2 = insert_timer(&timers[2], tptr, array[2]); t2->count++; // conn |
| 1468 | t2 = insert_timer(&timers[3], tptr, array[3]); t2->count++; // resp |
| 1469 | |
| 1470 | v = array[4] - array[0] - array[1] - array[2] - array[3]; // data time |
| 1471 | if (v < 0 && !(filter & FILT_QUIET)) |
| 1472 | fprintf(stderr, "ERR: %s (%d %d %d %d %d => %d)\n", |
| 1473 | line, array[0], array[1], array[2], array[3], array[4], v); |
| 1474 | t2 = insert_timer(&timers[4], tptr, v); t2->count++; |
| 1475 | lines_out++; |
| 1476 | } |
| 1477 | } else { /* percentile */ |
| 1478 | if (err) { |
| 1479 | if (array[4] < 0) |
| 1480 | array[4] = -1; |
| 1481 | t2 = insert_value(&timers[0], tptr, array[4]); // total time |
| 1482 | t2->count++; |
| 1483 | } else { |
| 1484 | int v; |
| 1485 | |
| 1486 | t2 = insert_value(&timers[1], tptr, array[0]); t2->count++; // req |
| 1487 | t2 = insert_value(&timers[2], tptr, array[2]); t2->count++; // conn |
| 1488 | t2 = insert_value(&timers[3], tptr, array[3]); t2->count++; // resp |
| 1489 | |
| 1490 | v = array[4] - array[0] - array[1] - array[2] - array[3]; // data time |
| 1491 | if (v < 0 && !(filter & FILT_QUIET)) |
| 1492 | fprintf(stderr, "ERR: %s (%d %d %d %d %d => %d)\n", |
| 1493 | line, array[0], array[1], array[2], array[3], array[4], v); |
| 1494 | t2 = insert_value(&timers[4], tptr, v); t2->count++; |
| 1495 | lines_out++; |
| 1496 | } |
| 1497 | } |
| 1498 | } |
| 1499 | |
| 1500 | |
Willy Tarreau | 72c2853 | 2009-01-22 18:56:50 +0100 | [diff] [blame] | 1501 | /* |
| 1502 | * Local variables: |
| 1503 | * c-indent-level: 8 |
| 1504 | * c-basic-offset: 8 |
| 1505 | * End: |
| 1506 | */ |