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