William Lallemand | 74c24fb | 2016-11-21 17:18:36 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Functions dedicated to statistics output and the stats socket |
| 3 | * |
| 4 | * Copyright 2000-2012 Willy Tarreau <w@1wt.eu> |
| 5 | * Copyright 2007-2009 Krzysztof Piotr Oledzki <ole@ans.pl> |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or |
| 8 | * modify it under the terms of the GNU General Public License |
| 9 | * as published by the Free Software Foundation; either version |
| 10 | * 2 of the License, or (at your option) any later version. |
| 11 | * |
| 12 | */ |
| 13 | |
| 14 | #include <ctype.h> |
| 15 | #include <errno.h> |
| 16 | #include <fcntl.h> |
| 17 | #include <stdio.h> |
| 18 | #include <stdlib.h> |
| 19 | #include <string.h> |
| 20 | #include <pwd.h> |
| 21 | #include <grp.h> |
| 22 | |
| 23 | #include <sys/socket.h> |
| 24 | #include <sys/stat.h> |
| 25 | #include <sys/types.h> |
| 26 | |
| 27 | #include <common/cfgparse.h> |
| 28 | #include <common/compat.h> |
| 29 | #include <common/config.h> |
| 30 | #include <common/debug.h> |
Willy Tarreau | 35b51c6 | 2018-09-10 15:38:55 +0200 | [diff] [blame] | 31 | #include <common/http.h> |
Willy Tarreau | b96b77e | 2018-12-11 10:22:41 +0100 | [diff] [blame] | 32 | #include <common/htx.h> |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 33 | #include <common/initcall.h> |
William Lallemand | 74c24fb | 2016-11-21 17:18:36 +0100 | [diff] [blame] | 34 | #include <common/memory.h> |
| 35 | #include <common/mini-clist.h> |
| 36 | #include <common/standard.h> |
| 37 | #include <common/ticks.h> |
| 38 | #include <common/time.h> |
| 39 | #include <common/uri_auth.h> |
| 40 | #include <common/version.h> |
| 41 | #include <common/base64.h> |
| 42 | |
| 43 | #include <types/applet.h> |
Willy Tarreau | 2b812e2 | 2016-11-22 16:18:05 +0100 | [diff] [blame] | 44 | #include <types/cli.h> |
William Lallemand | 74c24fb | 2016-11-21 17:18:36 +0100 | [diff] [blame] | 45 | #include <types/global.h> |
| 46 | #include <types/dns.h> |
William Lallemand | 9ed6203 | 2016-11-21 17:49:11 +0100 | [diff] [blame] | 47 | #include <types/stats.h> |
William Lallemand | 74c24fb | 2016-11-21 17:18:36 +0100 | [diff] [blame] | 48 | |
| 49 | #include <proto/backend.h> |
| 50 | #include <proto/channel.h> |
| 51 | #include <proto/checks.h> |
Willy Tarreau | 2b812e2 | 2016-11-22 16:18:05 +0100 | [diff] [blame] | 52 | #include <proto/cli.h> |
William Lallemand | 74c24fb | 2016-11-21 17:18:36 +0100 | [diff] [blame] | 53 | #include <proto/compression.h> |
Baptiste Assmann | 333939c | 2019-01-21 08:34:50 +0100 | [diff] [blame] | 54 | #include <proto/dns.h> |
William Lallemand | 9ed6203 | 2016-11-21 17:49:11 +0100 | [diff] [blame] | 55 | #include <proto/stats.h> |
William Lallemand | 74c24fb | 2016-11-21 17:18:36 +0100 | [diff] [blame] | 56 | #include <proto/fd.h> |
| 57 | #include <proto/freq_ctr.h> |
| 58 | #include <proto/frontend.h> |
Christopher Faulet | ed7a066 | 2019-01-14 11:07:34 +0100 | [diff] [blame] | 59 | #include <proto/http_htx.h> |
William Lallemand | 74c24fb | 2016-11-21 17:18:36 +0100 | [diff] [blame] | 60 | #include <proto/log.h> |
| 61 | #include <proto/pattern.h> |
| 62 | #include <proto/pipe.h> |
| 63 | #include <proto/listener.h> |
| 64 | #include <proto/map.h> |
Christopher Faulet | ef77922 | 2018-10-31 08:47:01 +0100 | [diff] [blame] | 65 | #include <proto/proto_http.h> |
William Lallemand | 74c24fb | 2016-11-21 17:18:36 +0100 | [diff] [blame] | 66 | #include <proto/proxy.h> |
| 67 | #include <proto/sample.h> |
| 68 | #include <proto/session.h> |
| 69 | #include <proto/stream.h> |
| 70 | #include <proto/server.h> |
| 71 | #include <proto/raw_sock.h> |
| 72 | #include <proto/stream_interface.h> |
| 73 | #include <proto/task.h> |
| 74 | |
| 75 | #ifdef USE_OPENSSL |
| 76 | #include <proto/ssl_sock.h> |
| 77 | #include <types/ssl_sock.h> |
| 78 | #endif |
| 79 | |
| 80 | |
Christopher Faulet | 2b9b678 | 2019-02-27 16:42:58 +0100 | [diff] [blame] | 81 | /* status codes available for the stats admin page (strictly 4 chars length) */ |
| 82 | const char *stat_status_codes[STAT_STATUS_SIZE] = { |
| 83 | [STAT_STATUS_DENY] = "DENY", |
| 84 | [STAT_STATUS_DONE] = "DONE", |
| 85 | [STAT_STATUS_ERRP] = "ERRP", |
| 86 | [STAT_STATUS_EXCD] = "EXCD", |
| 87 | [STAT_STATUS_NONE] = "NONE", |
| 88 | [STAT_STATUS_PART] = "PART", |
| 89 | [STAT_STATUS_UNKN] = "UNKN", |
| 90 | [STAT_STATUS_IVAL] = "IVAL", |
| 91 | }; |
| 92 | |
Willy Tarreau | 0baac8c | 2016-11-22 16:36:53 +0100 | [diff] [blame] | 93 | /* These are the field names for each INF_* field position. Please pay attention |
| 94 | * to always use the exact same name except that the strings for new names must |
| 95 | * be lower case or CamelCase while the enum entries must be upper case. |
| 96 | */ |
| 97 | const char *info_field_names[INF_TOTAL_FIELDS] = { |
| 98 | [INF_NAME] = "Name", |
| 99 | [INF_VERSION] = "Version", |
| 100 | [INF_RELEASE_DATE] = "Release_date", |
Yves Lafon | 9531728 | 2018-02-26 11:10:37 +0100 | [diff] [blame] | 101 | [INF_NBTHREAD] = "Nbthread", |
Willy Tarreau | 0baac8c | 2016-11-22 16:36:53 +0100 | [diff] [blame] | 102 | [INF_NBPROC] = "Nbproc", |
| 103 | [INF_PROCESS_NUM] = "Process_num", |
| 104 | [INF_PID] = "Pid", |
| 105 | [INF_UPTIME] = "Uptime", |
| 106 | [INF_UPTIME_SEC] = "Uptime_sec", |
| 107 | [INF_MEMMAX_MB] = "Memmax_MB", |
| 108 | [INF_POOL_ALLOC_MB] = "PoolAlloc_MB", |
| 109 | [INF_POOL_USED_MB] = "PoolUsed_MB", |
| 110 | [INF_POOL_FAILED] = "PoolFailed", |
| 111 | [INF_ULIMIT_N] = "Ulimit-n", |
| 112 | [INF_MAXSOCK] = "Maxsock", |
| 113 | [INF_MAXCONN] = "Maxconn", |
| 114 | [INF_HARD_MAXCONN] = "Hard_maxconn", |
| 115 | [INF_CURR_CONN] = "CurrConns", |
| 116 | [INF_CUM_CONN] = "CumConns", |
| 117 | [INF_CUM_REQ] = "CumReq", |
| 118 | [INF_MAX_SSL_CONNS] = "MaxSslConns", |
| 119 | [INF_CURR_SSL_CONNS] = "CurrSslConns", |
| 120 | [INF_CUM_SSL_CONNS] = "CumSslConns", |
| 121 | [INF_MAXPIPES] = "Maxpipes", |
| 122 | [INF_PIPES_USED] = "PipesUsed", |
| 123 | [INF_PIPES_FREE] = "PipesFree", |
| 124 | [INF_CONN_RATE] = "ConnRate", |
| 125 | [INF_CONN_RATE_LIMIT] = "ConnRateLimit", |
| 126 | [INF_MAX_CONN_RATE] = "MaxConnRate", |
| 127 | [INF_SESS_RATE] = "SessRate", |
| 128 | [INF_SESS_RATE_LIMIT] = "SessRateLimit", |
| 129 | [INF_MAX_SESS_RATE] = "MaxSessRate", |
| 130 | [INF_SSL_RATE] = "SslRate", |
| 131 | [INF_SSL_RATE_LIMIT] = "SslRateLimit", |
| 132 | [INF_MAX_SSL_RATE] = "MaxSslRate", |
| 133 | [INF_SSL_FRONTEND_KEY_RATE] = "SslFrontendKeyRate", |
| 134 | [INF_SSL_FRONTEND_MAX_KEY_RATE] = "SslFrontendMaxKeyRate", |
| 135 | [INF_SSL_FRONTEND_SESSION_REUSE_PCT] = "SslFrontendSessionReuse_pct", |
| 136 | [INF_SSL_BACKEND_KEY_RATE] = "SslBackendKeyRate", |
| 137 | [INF_SSL_BACKEND_MAX_KEY_RATE] = "SslBackendMaxKeyRate", |
| 138 | [INF_SSL_CACHE_LOOKUPS] = "SslCacheLookups", |
| 139 | [INF_SSL_CACHE_MISSES] = "SslCacheMisses", |
| 140 | [INF_COMPRESS_BPS_IN] = "CompressBpsIn", |
| 141 | [INF_COMPRESS_BPS_OUT] = "CompressBpsOut", |
| 142 | [INF_COMPRESS_BPS_RATE_LIM] = "CompressBpsRateLim", |
| 143 | [INF_ZLIB_MEM_USAGE] = "ZlibMemUsage", |
| 144 | [INF_MAX_ZLIB_MEM_USAGE] = "MaxZlibMemUsage", |
| 145 | [INF_TASKS] = "Tasks", |
| 146 | [INF_RUN_QUEUE] = "Run_queue", |
| 147 | [INF_IDLE_PCT] = "Idle_pct", |
| 148 | [INF_NODE] = "node", |
| 149 | [INF_DESCRIPTION] = "description", |
Willy Tarreau | 00098ea | 2018-11-05 14:38:13 +0100 | [diff] [blame] | 150 | [INF_STOPPING] = "Stopping", |
| 151 | [INF_JOBS] = "Jobs", |
William Lallemand | a719926 | 2018-11-16 16:57:20 +0100 | [diff] [blame] | 152 | [INF_UNSTOPPABLE_JOBS] = "Unstoppable Jobs", |
Willy Tarreau | 00098ea | 2018-11-05 14:38:13 +0100 | [diff] [blame] | 153 | [INF_LISTENERS] = "Listeners", |
Willy Tarreau | 199ad24 | 2018-11-05 16:31:22 +0100 | [diff] [blame] | 154 | [INF_ACTIVE_PEERS] = "ActivePeers", |
Willy Tarreau | 2d372c2 | 2018-11-05 17:12:27 +0100 | [diff] [blame] | 155 | [INF_CONNECTED_PEERS] = "ConnectedPeers", |
Willy Tarreau | 13ef773 | 2018-11-12 07:25:28 +0100 | [diff] [blame] | 156 | [INF_DROPPED_LOGS] = "DroppedLogs", |
Willy Tarreau | beb859a | 2018-11-22 18:07:59 +0100 | [diff] [blame] | 157 | [INF_BUSY_POLLING] = "BusyPolling", |
Baptiste Assmann | 333939c | 2019-01-21 08:34:50 +0100 | [diff] [blame] | 158 | [INF_FAILED_RESOLUTIONS] = "FailedResolutions", |
Willy Tarreau | 0baac8c | 2016-11-22 16:36:53 +0100 | [diff] [blame] | 159 | }; |
| 160 | |
William Lallemand | 74c24fb | 2016-11-21 17:18:36 +0100 | [diff] [blame] | 161 | const char *stat_field_names[ST_F_TOTAL_FIELDS] = { |
| 162 | [ST_F_PXNAME] = "pxname", |
| 163 | [ST_F_SVNAME] = "svname", |
| 164 | [ST_F_QCUR] = "qcur", |
| 165 | [ST_F_QMAX] = "qmax", |
| 166 | [ST_F_SCUR] = "scur", |
| 167 | [ST_F_SMAX] = "smax", |
| 168 | [ST_F_SLIM] = "slim", |
| 169 | [ST_F_STOT] = "stot", |
| 170 | [ST_F_BIN] = "bin", |
| 171 | [ST_F_BOUT] = "bout", |
| 172 | [ST_F_DREQ] = "dreq", |
| 173 | [ST_F_DRESP] = "dresp", |
| 174 | [ST_F_EREQ] = "ereq", |
| 175 | [ST_F_ECON] = "econ", |
| 176 | [ST_F_ERESP] = "eresp", |
| 177 | [ST_F_WRETR] = "wretr", |
| 178 | [ST_F_WREDIS] = "wredis", |
| 179 | [ST_F_STATUS] = "status", |
| 180 | [ST_F_WEIGHT] = "weight", |
| 181 | [ST_F_ACT] = "act", |
| 182 | [ST_F_BCK] = "bck", |
| 183 | [ST_F_CHKFAIL] = "chkfail", |
| 184 | [ST_F_CHKDOWN] = "chkdown", |
| 185 | [ST_F_LASTCHG] = "lastchg", |
| 186 | [ST_F_DOWNTIME] = "downtime", |
| 187 | [ST_F_QLIMIT] = "qlimit", |
| 188 | [ST_F_PID] = "pid", |
| 189 | [ST_F_IID] = "iid", |
| 190 | [ST_F_SID] = "sid", |
| 191 | [ST_F_THROTTLE] = "throttle", |
| 192 | [ST_F_LBTOT] = "lbtot", |
| 193 | [ST_F_TRACKED] = "tracked", |
| 194 | [ST_F_TYPE] = "type", |
| 195 | [ST_F_RATE] = "rate", |
| 196 | [ST_F_RATE_LIM] = "rate_lim", |
| 197 | [ST_F_RATE_MAX] = "rate_max", |
| 198 | [ST_F_CHECK_STATUS] = "check_status", |
| 199 | [ST_F_CHECK_CODE] = "check_code", |
| 200 | [ST_F_CHECK_DURATION] = "check_duration", |
| 201 | [ST_F_HRSP_1XX] = "hrsp_1xx", |
| 202 | [ST_F_HRSP_2XX] = "hrsp_2xx", |
| 203 | [ST_F_HRSP_3XX] = "hrsp_3xx", |
| 204 | [ST_F_HRSP_4XX] = "hrsp_4xx", |
| 205 | [ST_F_HRSP_5XX] = "hrsp_5xx", |
| 206 | [ST_F_HRSP_OTHER] = "hrsp_other", |
| 207 | [ST_F_HANAFAIL] = "hanafail", |
| 208 | [ST_F_REQ_RATE] = "req_rate", |
| 209 | [ST_F_REQ_RATE_MAX] = "req_rate_max", |
| 210 | [ST_F_REQ_TOT] = "req_tot", |
| 211 | [ST_F_CLI_ABRT] = "cli_abrt", |
| 212 | [ST_F_SRV_ABRT] = "srv_abrt", |
| 213 | [ST_F_COMP_IN] = "comp_in", |
| 214 | [ST_F_COMP_OUT] = "comp_out", |
| 215 | [ST_F_COMP_BYP] = "comp_byp", |
| 216 | [ST_F_COMP_RSP] = "comp_rsp", |
| 217 | [ST_F_LASTSESS] = "lastsess", |
| 218 | [ST_F_LAST_CHK] = "last_chk", |
| 219 | [ST_F_LAST_AGT] = "last_agt", |
| 220 | [ST_F_QTIME] = "qtime", |
| 221 | [ST_F_CTIME] = "ctime", |
| 222 | [ST_F_RTIME] = "rtime", |
| 223 | [ST_F_TTIME] = "ttime", |
| 224 | [ST_F_AGENT_STATUS] = "agent_status", |
| 225 | [ST_F_AGENT_CODE] = "agent_code", |
| 226 | [ST_F_AGENT_DURATION] = "agent_duration", |
| 227 | [ST_F_CHECK_DESC] = "check_desc", |
| 228 | [ST_F_AGENT_DESC] = "agent_desc", |
| 229 | [ST_F_CHECK_RISE] = "check_rise", |
| 230 | [ST_F_CHECK_FALL] = "check_fall", |
| 231 | [ST_F_CHECK_HEALTH] = "check_health", |
| 232 | [ST_F_AGENT_RISE] = "agent_rise", |
| 233 | [ST_F_AGENT_FALL] = "agent_fall", |
| 234 | [ST_F_AGENT_HEALTH] = "agent_health", |
| 235 | [ST_F_ADDR] = "addr", |
| 236 | [ST_F_COOKIE] = "cookie", |
| 237 | [ST_F_MODE] = "mode", |
| 238 | [ST_F_ALGO] = "algo", |
| 239 | [ST_F_CONN_RATE] = "conn_rate", |
| 240 | [ST_F_CONN_RATE_MAX] = "conn_rate_max", |
| 241 | [ST_F_CONN_TOT] = "conn_tot", |
| 242 | [ST_F_INTERCEPTED] = "intercepted", |
| 243 | [ST_F_DCON] = "dcon", |
| 244 | [ST_F_DSES] = "dses", |
Tim Duesterhus | 3fd1973 | 2018-05-27 20:35:08 +0200 | [diff] [blame] | 245 | [ST_F_WREW] = "wrew", |
Willy Tarreau | f157384 | 2018-12-14 11:35:36 +0100 | [diff] [blame] | 246 | [ST_F_CONNECT] = "connect", |
| 247 | [ST_F_REUSE] = "reuse", |
Willy Tarreau | a1214a5 | 2018-12-14 14:00:25 +0100 | [diff] [blame] | 248 | [ST_F_CACHE_LOOKUPS] = "cache_lookups", |
| 249 | [ST_F_CACHE_HITS] = "cache_hits", |
William Lallemand | 74c24fb | 2016-11-21 17:18:36 +0100 | [diff] [blame] | 250 | }; |
| 251 | |
Willy Tarreau | 0baac8c | 2016-11-22 16:36:53 +0100 | [diff] [blame] | 252 | /* one line of info */ |
Christopher Faulet | 1bc04c7 | 2017-10-29 20:14:08 +0100 | [diff] [blame] | 253 | static THREAD_LOCAL struct field info[INF_TOTAL_FIELDS]; |
William Lallemand | 74c24fb | 2016-11-21 17:18:36 +0100 | [diff] [blame] | 254 | /* one line of stats */ |
Christopher Faulet | 1bc04c7 | 2017-10-29 20:14:08 +0100 | [diff] [blame] | 255 | static THREAD_LOCAL struct field stats[ST_F_TOTAL_FIELDS]; |
William Lallemand | 74c24fb | 2016-11-21 17:18:36 +0100 | [diff] [blame] | 256 | |
| 257 | |
Christopher Faulet | ef77922 | 2018-10-31 08:47:01 +0100 | [diff] [blame] | 258 | static int stats_putchk(struct channel *chn, struct htx *htx, struct buffer *chk) |
| 259 | { |
| 260 | if (htx) { |
Christopher Faulet | 69fc88c | 2019-01-07 14:27:53 +0100 | [diff] [blame] | 261 | if (chk->data >= channel_htx_recv_max(chn, htx)) |
| 262 | return 0; |
Christopher Faulet | ef77922 | 2018-10-31 08:47:01 +0100 | [diff] [blame] | 263 | if (!htx_add_data(htx, ist2(chk->area, chk->data))) |
| 264 | return 0; |
Christopher Faulet | 5adbeeb | 2019-01-02 14:34:39 +0100 | [diff] [blame] | 265 | channel_add_input(chn, chk->data); |
Christopher Faulet | ef77922 | 2018-10-31 08:47:01 +0100 | [diff] [blame] | 266 | chk->data = 0; |
Christopher Faulet | ef77922 | 2018-10-31 08:47:01 +0100 | [diff] [blame] | 267 | } |
| 268 | else { |
| 269 | if (ci_putchk(chn, chk) == -1) |
| 270 | return 0; |
| 271 | } |
| 272 | return 1; |
| 273 | } |
Willy Tarreau | 0baac8c | 2016-11-22 16:36:53 +0100 | [diff] [blame] | 274 | |
Christopher Faulet | ed7a066 | 2019-01-14 11:07:34 +0100 | [diff] [blame] | 275 | static const char *stats_scope_ptr(struct appctx *appctx, struct stream_interface *si) |
| 276 | { |
| 277 | const char *p; |
| 278 | |
| 279 | if (IS_HTX_STRM(si_strm(si))) { |
| 280 | struct channel *req = si_oc(si); |
| 281 | struct htx *htx = htxbuf(&req->buf); |
| 282 | struct ist uri = htx_sl_req_uri(http_find_stline(htx)); |
| 283 | |
| 284 | p = uri.ptr; |
| 285 | } |
| 286 | else |
| 287 | p = co_head(si_oc(si)); |
| 288 | |
| 289 | return p + appctx->ctx.stats.scope_str; |
| 290 | } |
| 291 | |
William Lallemand | 74c24fb | 2016-11-21 17:18:36 +0100 | [diff] [blame] | 292 | /* |
| 293 | * http_stats_io_handler() |
| 294 | * -> stats_dump_stat_to_buffer() // same as above, but used for CSV or HTML |
| 295 | * -> stats_dump_csv_header() // emits the CSV headers (same as above) |
Simon Horman | 05ee213 | 2017-01-04 09:37:25 +0100 | [diff] [blame] | 296 | * -> stats_dump_json_header() // emits the JSON headers (same as above) |
William Lallemand | 74c24fb | 2016-11-21 17:18:36 +0100 | [diff] [blame] | 297 | * -> stats_dump_html_head() // emits the HTML headers |
| 298 | * -> stats_dump_html_info() // emits the equivalent of "show info" at the top |
| 299 | * -> stats_dump_proxy_to_buffer() // same as above, valid for CSV and HTML |
| 300 | * -> stats_dump_html_px_hdr() |
| 301 | * -> stats_dump_fe_stats() |
| 302 | * -> stats_dump_li_stats() |
| 303 | * -> stats_dump_sv_stats() |
| 304 | * -> stats_dump_be_stats() |
| 305 | * -> stats_dump_html_px_end() |
| 306 | * -> stats_dump_html_end() // emits HTML trailer |
Simon Horman | 05ee213 | 2017-01-04 09:37:25 +0100 | [diff] [blame] | 307 | * -> stats_dump_json_end() // emits JSON trailer |
William Lallemand | 74c24fb | 2016-11-21 17:18:36 +0100 | [diff] [blame] | 308 | */ |
| 309 | |
| 310 | |
William Lallemand | 74c24fb | 2016-11-21 17:18:36 +0100 | [diff] [blame] | 311 | /* Dumps the stats CSV header to the trash buffer which. The caller is responsible |
| 312 | * for clearing it if needed. |
| 313 | * NOTE: Some tools happen to rely on the field position instead of its name, |
| 314 | * so please only append new fields at the end, never in the middle. |
| 315 | */ |
| 316 | static void stats_dump_csv_header() |
| 317 | { |
| 318 | int field; |
| 319 | |
| 320 | chunk_appendf(&trash, "# "); |
| 321 | for (field = 0; field < ST_F_TOTAL_FIELDS; field++) |
| 322 | chunk_appendf(&trash, "%s,", stat_field_names[field]); |
| 323 | |
| 324 | chunk_appendf(&trash, "\n"); |
| 325 | } |
| 326 | |
| 327 | |
| 328 | /* Emits a stats field without any surrounding element and properly encoded to |
| 329 | * resist CSV output. Returns non-zero on success, 0 if the buffer is full. |
| 330 | */ |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 331 | int stats_emit_raw_data_field(struct buffer *out, const struct field *f) |
William Lallemand | 74c24fb | 2016-11-21 17:18:36 +0100 | [diff] [blame] | 332 | { |
| 333 | switch (field_format(f, 0)) { |
| 334 | case FF_EMPTY: return 1; |
| 335 | case FF_S32: return chunk_appendf(out, "%d", f->u.s32); |
| 336 | case FF_U32: return chunk_appendf(out, "%u", f->u.u32); |
| 337 | case FF_S64: return chunk_appendf(out, "%lld", (long long)f->u.s64); |
| 338 | case FF_U64: return chunk_appendf(out, "%llu", (unsigned long long)f->u.u64); |
| 339 | case FF_STR: return csv_enc_append(field_str(f, 0), 1, out) != NULL; |
| 340 | default: return chunk_appendf(out, "[INCORRECT_FIELD_TYPE_%08x]", f->type); |
| 341 | } |
| 342 | } |
| 343 | |
| 344 | /* Emits a stats field prefixed with its type. No CSV encoding is prepared, the |
| 345 | * output is supposed to be used on its own line. Returns non-zero on success, 0 |
| 346 | * if the buffer is full. |
| 347 | */ |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 348 | int stats_emit_typed_data_field(struct buffer *out, const struct field *f) |
William Lallemand | 74c24fb | 2016-11-21 17:18:36 +0100 | [diff] [blame] | 349 | { |
| 350 | switch (field_format(f, 0)) { |
| 351 | case FF_EMPTY: return 1; |
| 352 | case FF_S32: return chunk_appendf(out, "s32:%d", f->u.s32); |
| 353 | case FF_U32: return chunk_appendf(out, "u32:%u", f->u.u32); |
| 354 | case FF_S64: return chunk_appendf(out, "s64:%lld", (long long)f->u.s64); |
| 355 | case FF_U64: return chunk_appendf(out, "u64:%llu", (unsigned long long)f->u.u64); |
| 356 | case FF_STR: return chunk_appendf(out, "str:%s", field_str(f, 0)); |
| 357 | default: return chunk_appendf(out, "%08x:?", f->type); |
| 358 | } |
| 359 | } |
| 360 | |
Simon Horman | 05ee213 | 2017-01-04 09:37:25 +0100 | [diff] [blame] | 361 | /* Limit JSON integer values to the range [-(2**53)+1, (2**53)-1] as per |
| 362 | * the recommendation for interoperable integers in section 6 of RFC 7159. |
| 363 | */ |
| 364 | #define JSON_INT_MAX ((1ULL << 53) - 1) |
| 365 | #define JSON_INT_MIN (0 - JSON_INT_MAX) |
| 366 | |
| 367 | /* Emits a stats field value and its type in JSON. |
| 368 | * Returns non-zero on success, 0 on error. |
| 369 | */ |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 370 | int stats_emit_json_data_field(struct buffer *out, const struct field *f) |
Simon Horman | 05ee213 | 2017-01-04 09:37:25 +0100 | [diff] [blame] | 371 | { |
| 372 | int old_len; |
| 373 | char buf[20]; |
| 374 | const char *type, *value = buf, *quote = ""; |
| 375 | |
| 376 | switch (field_format(f, 0)) { |
| 377 | case FF_EMPTY: return 1; |
| 378 | case FF_S32: type = "\"s32\""; |
| 379 | snprintf(buf, sizeof(buf), "%d", f->u.s32); |
| 380 | break; |
| 381 | case FF_U32: type = "\"u32\""; |
| 382 | snprintf(buf, sizeof(buf), "%u", f->u.u32); |
| 383 | break; |
| 384 | case FF_S64: type = "\"s64\""; |
| 385 | if (f->u.s64 < JSON_INT_MIN || f->u.s64 > JSON_INT_MAX) |
| 386 | return 0; |
| 387 | type = "\"s64\""; |
| 388 | snprintf(buf, sizeof(buf), "%lld", (long long)f->u.s64); |
| 389 | break; |
| 390 | case FF_U64: if (f->u.u64 > JSON_INT_MAX) |
| 391 | return 0; |
| 392 | type = "\"u64\""; |
| 393 | snprintf(buf, sizeof(buf), "%llu", |
| 394 | (unsigned long long) f->u.u64); |
| 395 | break; |
| 396 | case FF_STR: type = "\"str\""; |
| 397 | value = field_str(f, 0); |
| 398 | quote = "\""; |
| 399 | break; |
| 400 | default: snprintf(buf, sizeof(buf), "%u", f->type); |
| 401 | type = buf; |
| 402 | value = "unknown"; |
| 403 | quote = "\""; |
| 404 | break; |
| 405 | } |
| 406 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 407 | old_len = out->data; |
Simon Horman | 05ee213 | 2017-01-04 09:37:25 +0100 | [diff] [blame] | 408 | chunk_appendf(out, ",\"value\":{\"type\":%s,\"value\":%s%s%s}", |
| 409 | type, quote, value, quote); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 410 | return !(old_len == out->data); |
Simon Horman | 05ee213 | 2017-01-04 09:37:25 +0100 | [diff] [blame] | 411 | } |
| 412 | |
William Lallemand | 74c24fb | 2016-11-21 17:18:36 +0100 | [diff] [blame] | 413 | /* Emits an encoding of the field type on 3 characters followed by a delimiter. |
| 414 | * Returns non-zero on success, 0 if the buffer is full. |
| 415 | */ |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 416 | int stats_emit_field_tags(struct buffer *out, const struct field *f, |
| 417 | char delim) |
William Lallemand | 74c24fb | 2016-11-21 17:18:36 +0100 | [diff] [blame] | 418 | { |
| 419 | char origin, nature, scope; |
| 420 | |
| 421 | switch (field_origin(f, 0)) { |
| 422 | case FO_METRIC: origin = 'M'; break; |
| 423 | case FO_STATUS: origin = 'S'; break; |
| 424 | case FO_KEY: origin = 'K'; break; |
| 425 | case FO_CONFIG: origin = 'C'; break; |
| 426 | case FO_PRODUCT: origin = 'P'; break; |
| 427 | default: origin = '?'; break; |
| 428 | } |
| 429 | |
| 430 | switch (field_nature(f, 0)) { |
| 431 | case FN_GAUGE: nature = 'G'; break; |
| 432 | case FN_LIMIT: nature = 'L'; break; |
| 433 | case FN_MIN: nature = 'm'; break; |
| 434 | case FN_MAX: nature = 'M'; break; |
| 435 | case FN_RATE: nature = 'R'; break; |
| 436 | case FN_COUNTER: nature = 'C'; break; |
| 437 | case FN_DURATION: nature = 'D'; break; |
| 438 | case FN_AGE: nature = 'A'; break; |
| 439 | case FN_TIME: nature = 'T'; break; |
| 440 | case FN_NAME: nature = 'N'; break; |
| 441 | case FN_OUTPUT: nature = 'O'; break; |
| 442 | case FN_AVG: nature = 'a'; break; |
| 443 | default: nature = '?'; break; |
| 444 | } |
| 445 | |
| 446 | switch (field_scope(f, 0)) { |
| 447 | case FS_PROCESS: scope = 'P'; break; |
| 448 | case FS_SERVICE: scope = 'S'; break; |
| 449 | case FS_SYSTEM: scope = 's'; break; |
| 450 | case FS_CLUSTER: scope = 'C'; break; |
| 451 | default: scope = '?'; break; |
| 452 | } |
| 453 | |
| 454 | return chunk_appendf(out, "%c%c%c%c", origin, nature, scope, delim); |
| 455 | } |
| 456 | |
Simon Horman | 05ee213 | 2017-01-04 09:37:25 +0100 | [diff] [blame] | 457 | /* Emits an encoding of the field type as JSON. |
| 458 | * Returns non-zero on success, 0 if the buffer is full. |
| 459 | */ |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 460 | int stats_emit_json_field_tags(struct buffer *out, const struct field *f) |
Simon Horman | 05ee213 | 2017-01-04 09:37:25 +0100 | [diff] [blame] | 461 | { |
| 462 | const char *origin, *nature, *scope; |
| 463 | int old_len; |
| 464 | |
| 465 | switch (field_origin(f, 0)) { |
| 466 | case FO_METRIC: origin = "Metric"; break; |
| 467 | case FO_STATUS: origin = "Status"; break; |
| 468 | case FO_KEY: origin = "Key"; break; |
| 469 | case FO_CONFIG: origin = "Config"; break; |
| 470 | case FO_PRODUCT: origin = "Product"; break; |
| 471 | default: origin = "Unknown"; break; |
| 472 | } |
| 473 | |
| 474 | switch (field_nature(f, 0)) { |
| 475 | case FN_GAUGE: nature = "Gauge"; break; |
| 476 | case FN_LIMIT: nature = "Limit"; break; |
| 477 | case FN_MIN: nature = "Min"; break; |
| 478 | case FN_MAX: nature = "Max"; break; |
| 479 | case FN_RATE: nature = "Rate"; break; |
| 480 | case FN_COUNTER: nature = "Counter"; break; |
| 481 | case FN_DURATION: nature = "Duration"; break; |
| 482 | case FN_AGE: nature = "Age"; break; |
| 483 | case FN_TIME: nature = "Time"; break; |
| 484 | case FN_NAME: nature = "Name"; break; |
| 485 | case FN_OUTPUT: nature = "Output"; break; |
| 486 | case FN_AVG: nature = "Avg"; break; |
| 487 | default: nature = "Unknown"; break; |
| 488 | } |
| 489 | |
| 490 | switch (field_scope(f, 0)) { |
| 491 | case FS_PROCESS: scope = "Process"; break; |
| 492 | case FS_SERVICE: scope = "Service"; break; |
| 493 | case FS_SYSTEM: scope = "System"; break; |
| 494 | case FS_CLUSTER: scope = "Cluster"; break; |
| 495 | default: scope = "Unknown"; break; |
| 496 | } |
| 497 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 498 | old_len = out->data; |
Simon Horman | 05ee213 | 2017-01-04 09:37:25 +0100 | [diff] [blame] | 499 | chunk_appendf(out, "\"tags\":{" |
| 500 | "\"origin\":\"%s\"," |
| 501 | "\"nature\":\"%s\"," |
| 502 | "\"scope\":\"%s\"" |
| 503 | "}", origin, nature, scope); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 504 | return !(old_len == out->data); |
Simon Horman | 05ee213 | 2017-01-04 09:37:25 +0100 | [diff] [blame] | 505 | } |
William Lallemand | 74c24fb | 2016-11-21 17:18:36 +0100 | [diff] [blame] | 506 | |
| 507 | /* Dump all fields from <stats> into <out> using CSV format */ |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 508 | static int stats_dump_fields_csv(struct buffer *out, |
| 509 | const struct field *stats) |
William Lallemand | 74c24fb | 2016-11-21 17:18:36 +0100 | [diff] [blame] | 510 | { |
| 511 | int field; |
| 512 | |
| 513 | for (field = 0; field < ST_F_TOTAL_FIELDS; field++) { |
| 514 | if (!stats_emit_raw_data_field(out, &stats[field])) |
| 515 | return 0; |
| 516 | if (!chunk_strcat(out, ",")) |
| 517 | return 0; |
| 518 | } |
| 519 | chunk_strcat(out, "\n"); |
| 520 | return 1; |
| 521 | } |
| 522 | |
| 523 | /* Dump all fields from <stats> into <out> using a typed "field:desc:type:value" format */ |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 524 | static int stats_dump_fields_typed(struct buffer *out, |
| 525 | const struct field *stats) |
William Lallemand | 74c24fb | 2016-11-21 17:18:36 +0100 | [diff] [blame] | 526 | { |
| 527 | int field; |
| 528 | |
| 529 | for (field = 0; field < ST_F_TOTAL_FIELDS; field++) { |
| 530 | if (!stats[field].type) |
| 531 | continue; |
| 532 | |
| 533 | chunk_appendf(out, "%c.%u.%u.%d.%s.%u:", |
| 534 | stats[ST_F_TYPE].u.u32 == STATS_TYPE_FE ? 'F' : |
| 535 | stats[ST_F_TYPE].u.u32 == STATS_TYPE_BE ? 'B' : |
| 536 | stats[ST_F_TYPE].u.u32 == STATS_TYPE_SO ? 'L' : |
| 537 | stats[ST_F_TYPE].u.u32 == STATS_TYPE_SV ? 'S' : |
| 538 | '?', |
| 539 | stats[ST_F_IID].u.u32, stats[ST_F_SID].u.u32, |
| 540 | field, stat_field_names[field], stats[ST_F_PID].u.u32); |
| 541 | |
| 542 | if (!stats_emit_field_tags(out, &stats[field], ':')) |
| 543 | return 0; |
| 544 | if (!stats_emit_typed_data_field(out, &stats[field])) |
| 545 | return 0; |
| 546 | if (!chunk_strcat(out, "\n")) |
| 547 | return 0; |
| 548 | } |
| 549 | return 1; |
| 550 | } |
| 551 | |
Simon Horman | 05ee213 | 2017-01-04 09:37:25 +0100 | [diff] [blame] | 552 | /* Dump all fields from <stats> into <out> using the "show info json" format */ |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 553 | static int stats_dump_json_info_fields(struct buffer *out, |
Simon Horman | 05ee213 | 2017-01-04 09:37:25 +0100 | [diff] [blame] | 554 | const struct field *info) |
| 555 | { |
| 556 | int field; |
| 557 | int started = 0; |
| 558 | |
| 559 | if (!chunk_strcat(out, "[")) |
| 560 | return 0; |
| 561 | |
| 562 | for (field = 0; field < INF_TOTAL_FIELDS; field++) { |
| 563 | int old_len; |
| 564 | |
| 565 | if (!field_format(info, field)) |
| 566 | continue; |
| 567 | |
| 568 | if (started && !chunk_strcat(out, ",")) |
| 569 | goto err; |
| 570 | started = 1; |
| 571 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 572 | old_len = out->data; |
Simon Horman | 05ee213 | 2017-01-04 09:37:25 +0100 | [diff] [blame] | 573 | chunk_appendf(out, |
| 574 | "{\"field\":{\"pos\":%d,\"name\":\"%s\"}," |
| 575 | "\"processNum\":%u,", |
| 576 | field, info_field_names[field], |
| 577 | info[INF_PROCESS_NUM].u.u32); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 578 | if (old_len == out->data) |
Simon Horman | 05ee213 | 2017-01-04 09:37:25 +0100 | [diff] [blame] | 579 | goto err; |
| 580 | |
| 581 | if (!stats_emit_json_field_tags(out, &info[field])) |
| 582 | goto err; |
| 583 | |
| 584 | if (!stats_emit_json_data_field(out, &info[field])) |
| 585 | goto err; |
| 586 | |
| 587 | if (!chunk_strcat(out, "}")) |
| 588 | goto err; |
| 589 | } |
| 590 | |
| 591 | if (!chunk_strcat(out, "]")) |
| 592 | goto err; |
| 593 | return 1; |
| 594 | |
| 595 | err: |
| 596 | chunk_reset(out); |
| 597 | chunk_appendf(out, "{\"errorStr\":\"output buffer too short\"}"); |
| 598 | return 0; |
| 599 | } |
| 600 | |
| 601 | /* Dump all fields from <stats> into <out> using a typed "field:desc:type:value" format */ |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 602 | static int stats_dump_fields_json(struct buffer *out, |
| 603 | const struct field *stats, |
Simon Horman | 05ee213 | 2017-01-04 09:37:25 +0100 | [diff] [blame] | 604 | int first_stat) |
| 605 | { |
| 606 | int field; |
| 607 | int started = 0; |
| 608 | |
| 609 | if (!first_stat && !chunk_strcat(out, ",")) |
| 610 | return 0; |
| 611 | if (!chunk_strcat(out, "[")) |
| 612 | return 0; |
| 613 | |
| 614 | for (field = 0; field < ST_F_TOTAL_FIELDS; field++) { |
| 615 | const char *obj_type; |
| 616 | int old_len; |
| 617 | |
| 618 | if (!stats[field].type) |
| 619 | continue; |
| 620 | |
| 621 | if (started && !chunk_strcat(out, ",")) |
| 622 | goto err; |
| 623 | started = 1; |
| 624 | |
| 625 | switch (stats[ST_F_TYPE].u.u32) { |
| 626 | case STATS_TYPE_FE: obj_type = "Frontend"; break; |
| 627 | case STATS_TYPE_BE: obj_type = "Backend"; break; |
| 628 | case STATS_TYPE_SO: obj_type = "Listener"; break; |
| 629 | case STATS_TYPE_SV: obj_type = "Server"; break; |
| 630 | default: obj_type = "Unknown"; break; |
| 631 | } |
| 632 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 633 | old_len = out->data; |
Simon Horman | 05ee213 | 2017-01-04 09:37:25 +0100 | [diff] [blame] | 634 | chunk_appendf(out, |
| 635 | "{" |
| 636 | "\"objType\":\"%s\"," |
| 637 | "\"proxyId\":%d," |
| 638 | "\"id\":%d," |
| 639 | "\"field\":{\"pos\":%d,\"name\":\"%s\"}," |
| 640 | "\"processNum\":%u,", |
| 641 | obj_type, stats[ST_F_IID].u.u32, |
| 642 | stats[ST_F_SID].u.u32, field, |
| 643 | stat_field_names[field], stats[ST_F_PID].u.u32); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 644 | if (old_len == out->data) |
Simon Horman | 05ee213 | 2017-01-04 09:37:25 +0100 | [diff] [blame] | 645 | goto err; |
| 646 | |
| 647 | if (!stats_emit_json_field_tags(out, &stats[field])) |
| 648 | goto err; |
| 649 | |
| 650 | if (!stats_emit_json_data_field(out, &stats[field])) |
| 651 | goto err; |
| 652 | |
| 653 | if (!chunk_strcat(out, "}")) |
| 654 | goto err; |
| 655 | } |
| 656 | |
| 657 | if (!chunk_strcat(out, "]")) |
| 658 | goto err; |
| 659 | |
| 660 | return 1; |
| 661 | |
| 662 | err: |
| 663 | chunk_reset(out); |
| 664 | if (!first_stat) |
| 665 | chunk_strcat(out, ","); |
| 666 | chunk_appendf(out, "{\"errorStr\":\"output buffer too short\"}"); |
| 667 | return 0; |
| 668 | } |
| 669 | |
William Lallemand | 74c24fb | 2016-11-21 17:18:36 +0100 | [diff] [blame] | 670 | /* Dump all fields from <stats> into <out> using the HTML format. A column is |
| 671 | * reserved for the checkbox is ST_SHOWADMIN is set in <flags>. Some extra info |
| 672 | * are provided if ST_SHLGNDS is present in <flags>. |
| 673 | */ |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 674 | static int stats_dump_fields_html(struct buffer *out, |
| 675 | const struct field *stats, |
| 676 | unsigned int flags) |
William Lallemand | 74c24fb | 2016-11-21 17:18:36 +0100 | [diff] [blame] | 677 | { |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 678 | struct buffer src; |
William Lallemand | 74c24fb | 2016-11-21 17:18:36 +0100 | [diff] [blame] | 679 | |
| 680 | if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_FE) { |
| 681 | chunk_appendf(out, |
| 682 | /* name, queue */ |
| 683 | "<tr class=\"frontend\">"); |
| 684 | |
| 685 | if (flags & ST_SHOWADMIN) { |
| 686 | /* Column sub-heading for Enable or Disable server */ |
| 687 | chunk_appendf(out, "<td></td>"); |
| 688 | } |
| 689 | |
| 690 | chunk_appendf(out, |
| 691 | "<td class=ac>" |
| 692 | "<a name=\"%s/Frontend\"></a>" |
| 693 | "<a class=lfsb href=\"#%s/Frontend\">Frontend</a></td>" |
| 694 | "<td colspan=3></td>" |
| 695 | "", |
| 696 | field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_PXNAME)); |
| 697 | |
| 698 | chunk_appendf(out, |
| 699 | /* sessions rate : current */ |
| 700 | "<td><u>%s<div class=tips><table class=det>" |
| 701 | "<tr><th>Current connection rate:</th><td>%s/s</td></tr>" |
| 702 | "<tr><th>Current session rate:</th><td>%s/s</td></tr>" |
| 703 | "", |
| 704 | U2H(stats[ST_F_RATE].u.u32), |
| 705 | U2H(stats[ST_F_CONN_RATE].u.u32), |
| 706 | U2H(stats[ST_F_RATE].u.u32)); |
| 707 | |
| 708 | if (strcmp(field_str(stats, ST_F_MODE), "http") == 0) |
| 709 | chunk_appendf(out, |
| 710 | "<tr><th>Current request rate:</th><td>%s/s</td></tr>", |
| 711 | U2H(stats[ST_F_REQ_RATE].u.u32)); |
| 712 | |
| 713 | chunk_appendf(out, |
| 714 | "</table></div></u></td>" |
| 715 | /* sessions rate : max */ |
| 716 | "<td><u>%s<div class=tips><table class=det>" |
| 717 | "<tr><th>Max connection rate:</th><td>%s/s</td></tr>" |
| 718 | "<tr><th>Max session rate:</th><td>%s/s</td></tr>" |
| 719 | "", |
| 720 | U2H(stats[ST_F_RATE_MAX].u.u32), |
| 721 | U2H(stats[ST_F_CONN_RATE_MAX].u.u32), |
| 722 | U2H(stats[ST_F_RATE_MAX].u.u32)); |
| 723 | |
| 724 | if (strcmp(field_str(stats, ST_F_MODE), "http") == 0) |
| 725 | chunk_appendf(out, |
| 726 | "<tr><th>Max request rate:</th><td>%s/s</td></tr>", |
| 727 | U2H(stats[ST_F_REQ_RATE_MAX].u.u32)); |
| 728 | |
| 729 | chunk_appendf(out, |
| 730 | "</table></div></u></td>" |
| 731 | /* sessions rate : limit */ |
| 732 | "<td>%s</td>", |
| 733 | LIM2A(stats[ST_F_RATE_LIM].u.u32, "-")); |
| 734 | |
| 735 | chunk_appendf(out, |
| 736 | /* sessions: current, max, limit, total */ |
| 737 | "<td>%s</td><td>%s</td><td>%s</td>" |
| 738 | "<td><u>%s<div class=tips><table class=det>" |
| 739 | "<tr><th>Cum. connections:</th><td>%s</td></tr>" |
| 740 | "<tr><th>Cum. sessions:</th><td>%s</td></tr>" |
| 741 | "", |
| 742 | U2H(stats[ST_F_SCUR].u.u32), U2H(stats[ST_F_SMAX].u.u32), U2H(stats[ST_F_SLIM].u.u32), |
| 743 | U2H(stats[ST_F_STOT].u.u64), |
| 744 | U2H(stats[ST_F_CONN_TOT].u.u64), |
| 745 | U2H(stats[ST_F_STOT].u.u64)); |
| 746 | |
| 747 | /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */ |
| 748 | if (strcmp(field_str(stats, ST_F_MODE), "http") == 0) { |
| 749 | chunk_appendf(out, |
| 750 | "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>" |
| 751 | "<tr><th>- HTTP 1xx responses:</th><td>%s</td></tr>" |
| 752 | "<tr><th>- HTTP 2xx responses:</th><td>%s</td></tr>" |
| 753 | "<tr><th> Compressed 2xx:</th><td>%s</td><td>(%d%%)</td></tr>" |
| 754 | "<tr><th>- HTTP 3xx responses:</th><td>%s</td></tr>" |
| 755 | "<tr><th>- HTTP 4xx responses:</th><td>%s</td></tr>" |
| 756 | "<tr><th>- HTTP 5xx responses:</th><td>%s</td></tr>" |
| 757 | "<tr><th>- other responses:</th><td>%s</td></tr>" |
| 758 | "<tr><th>Intercepted requests:</th><td>%s</td></tr>" |
Willy Tarreau | a1214a5 | 2018-12-14 14:00:25 +0100 | [diff] [blame] | 759 | "<tr><th>Cache lookups:</th><td>%s</td></tr>" |
| 760 | "<tr><th>Cache hits:</th><td>%s</td><td>(%d%%)</td></tr>" |
Willy Tarreau | 1b0f85e | 2018-05-28 15:12:40 +0200 | [diff] [blame] | 761 | "<tr><th>Failed hdr rewrites:</th><td>%s</td></tr>" |
William Lallemand | 74c24fb | 2016-11-21 17:18:36 +0100 | [diff] [blame] | 762 | "", |
| 763 | U2H(stats[ST_F_REQ_TOT].u.u64), |
| 764 | U2H(stats[ST_F_HRSP_1XX].u.u64), |
| 765 | U2H(stats[ST_F_HRSP_2XX].u.u64), |
| 766 | U2H(stats[ST_F_COMP_RSP].u.u64), |
| 767 | stats[ST_F_HRSP_2XX].u.u64 ? |
| 768 | (int)(100 * stats[ST_F_COMP_RSP].u.u64 / stats[ST_F_HRSP_2XX].u.u64) : 0, |
| 769 | U2H(stats[ST_F_HRSP_3XX].u.u64), |
| 770 | U2H(stats[ST_F_HRSP_4XX].u.u64), |
| 771 | U2H(stats[ST_F_HRSP_5XX].u.u64), |
| 772 | U2H(stats[ST_F_HRSP_OTHER].u.u64), |
Willy Tarreau | 1b0f85e | 2018-05-28 15:12:40 +0200 | [diff] [blame] | 773 | U2H(stats[ST_F_INTERCEPTED].u.u64), |
Willy Tarreau | a1214a5 | 2018-12-14 14:00:25 +0100 | [diff] [blame] | 774 | U2H(stats[ST_F_CACHE_LOOKUPS].u.u64), |
| 775 | U2H(stats[ST_F_CACHE_HITS].u.u64), |
| 776 | stats[ST_F_CACHE_LOOKUPS].u.u64 ? |
| 777 | (int)(100 * stats[ST_F_CACHE_HITS].u.u64 / stats[ST_F_CACHE_LOOKUPS].u.u64) : 0, |
Willy Tarreau | 1b0f85e | 2018-05-28 15:12:40 +0200 | [diff] [blame] | 778 | U2H(stats[ST_F_WREW].u.u64)); |
William Lallemand | 74c24fb | 2016-11-21 17:18:36 +0100 | [diff] [blame] | 779 | } |
| 780 | |
| 781 | chunk_appendf(out, |
| 782 | "</table></div></u></td>" |
| 783 | /* sessions: lbtot, lastsess */ |
| 784 | "<td></td><td></td>" |
| 785 | /* bytes : in */ |
| 786 | "<td>%s</td>" |
| 787 | "", |
| 788 | U2H(stats[ST_F_BIN].u.u64)); |
| 789 | |
| 790 | chunk_appendf(out, |
| 791 | /* bytes:out + compression stats (via hover): comp_in, comp_out, comp_byp */ |
| 792 | "<td>%s%s<div class=tips><table class=det>" |
| 793 | "<tr><th>Response bytes in:</th><td>%s</td></tr>" |
| 794 | "<tr><th>Compression in:</th><td>%s</td></tr>" |
| 795 | "<tr><th>Compression out:</th><td>%s</td><td>(%d%%)</td></tr>" |
| 796 | "<tr><th>Compression bypass:</th><td>%s</td></tr>" |
| 797 | "<tr><th>Total bytes saved:</th><td>%s</td><td>(%d%%)</td></tr>" |
| 798 | "</table></div>%s</td>", |
| 799 | (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "<u>":"", |
| 800 | U2H(stats[ST_F_BOUT].u.u64), |
| 801 | U2H(stats[ST_F_BOUT].u.u64), |
| 802 | U2H(stats[ST_F_COMP_IN].u.u64), |
| 803 | U2H(stats[ST_F_COMP_OUT].u.u64), |
| 804 | stats[ST_F_COMP_IN].u.u64 ? (int)(stats[ST_F_COMP_OUT].u.u64 * 100 / stats[ST_F_COMP_IN].u.u64) : 0, |
| 805 | U2H(stats[ST_F_COMP_BYP].u.u64), |
| 806 | U2H(stats[ST_F_COMP_IN].u.u64 - stats[ST_F_COMP_OUT].u.u64), |
| 807 | stats[ST_F_BOUT].u.u64 ? (int)((stats[ST_F_COMP_IN].u.u64 - stats[ST_F_COMP_OUT].u.u64) * 100 / stats[ST_F_BOUT].u.u64) : 0, |
| 808 | (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "</u>":""); |
| 809 | |
| 810 | chunk_appendf(out, |
| 811 | /* denied: req, resp */ |
| 812 | "<td>%s</td><td>%s</td>" |
| 813 | /* errors : request, connect, response */ |
| 814 | "<td>%s</td><td></td><td></td>" |
| 815 | /* warnings: retries, redispatches */ |
| 816 | "<td></td><td></td>" |
| 817 | /* server status : reflect frontend status */ |
| 818 | "<td class=ac>%s</td>" |
| 819 | /* rest of server: nothing */ |
| 820 | "<td class=ac colspan=8></td></tr>" |
| 821 | "", |
| 822 | U2H(stats[ST_F_DREQ].u.u64), U2H(stats[ST_F_DRESP].u.u64), |
| 823 | U2H(stats[ST_F_EREQ].u.u64), |
| 824 | field_str(stats, ST_F_STATUS)); |
| 825 | } |
| 826 | else if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_SO) { |
| 827 | chunk_appendf(out, "<tr class=socket>"); |
| 828 | if (flags & ST_SHOWADMIN) { |
| 829 | /* Column sub-heading for Enable or Disable server */ |
| 830 | chunk_appendf(out, "<td></td>"); |
| 831 | } |
| 832 | |
| 833 | chunk_appendf(out, |
| 834 | /* frontend name, listener name */ |
| 835 | "<td class=ac><a name=\"%s/+%s\"></a>%s" |
| 836 | "<a class=lfsb href=\"#%s/+%s\">%s</a>" |
| 837 | "", |
| 838 | field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME), |
| 839 | (flags & ST_SHLGNDS)?"<u>":"", |
| 840 | field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME), field_str(stats, ST_F_SVNAME)); |
| 841 | |
| 842 | if (flags & ST_SHLGNDS) { |
| 843 | chunk_appendf(out, "<div class=tips>"); |
| 844 | |
| 845 | if (isdigit(*field_str(stats, ST_F_ADDR))) |
| 846 | chunk_appendf(out, "IPv4: %s, ", field_str(stats, ST_F_ADDR)); |
| 847 | else if (*field_str(stats, ST_F_ADDR) == '[') |
| 848 | chunk_appendf(out, "IPv6: %s, ", field_str(stats, ST_F_ADDR)); |
| 849 | else if (*field_str(stats, ST_F_ADDR)) |
| 850 | chunk_appendf(out, "%s, ", field_str(stats, ST_F_ADDR)); |
| 851 | |
| 852 | /* id */ |
| 853 | chunk_appendf(out, "id: %d</div>", stats[ST_F_SID].u.u32); |
| 854 | } |
| 855 | |
| 856 | chunk_appendf(out, |
| 857 | /* queue */ |
| 858 | "%s</td><td colspan=3></td>" |
| 859 | /* sessions rate: current, max, limit */ |
| 860 | "<td colspan=3> </td>" |
| 861 | /* sessions: current, max, limit, total, lbtot, lastsess */ |
| 862 | "<td>%s</td><td>%s</td><td>%s</td>" |
| 863 | "<td>%s</td><td> </td><td> </td>" |
| 864 | /* bytes: in, out */ |
| 865 | "<td>%s</td><td>%s</td>" |
| 866 | "", |
| 867 | (flags & ST_SHLGNDS)?"</u>":"", |
| 868 | U2H(stats[ST_F_SCUR].u.u32), U2H(stats[ST_F_SMAX].u.u32), U2H(stats[ST_F_SLIM].u.u32), |
| 869 | U2H(stats[ST_F_STOT].u.u64), U2H(stats[ST_F_BIN].u.u64), U2H(stats[ST_F_BOUT].u.u64)); |
| 870 | |
| 871 | chunk_appendf(out, |
| 872 | /* denied: req, resp */ |
| 873 | "<td>%s</td><td>%s</td>" |
| 874 | /* errors: request, connect, response */ |
| 875 | "<td>%s</td><td></td><td></td>" |
| 876 | /* warnings: retries, redispatches */ |
| 877 | "<td></td><td></td>" |
| 878 | /* server status: reflect listener status */ |
| 879 | "<td class=ac>%s</td>" |
| 880 | /* rest of server: nothing */ |
| 881 | "<td class=ac colspan=8></td></tr>" |
| 882 | "", |
| 883 | U2H(stats[ST_F_DREQ].u.u64), U2H(stats[ST_F_DRESP].u.u64), |
| 884 | U2H(stats[ST_F_EREQ].u.u64), |
| 885 | field_str(stats, ST_F_STATUS)); |
| 886 | } |
| 887 | else if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_SV) { |
| 888 | const char *style; |
| 889 | |
| 890 | /* determine the style to use depending on the server's state, |
| 891 | * its health and weight. There isn't a 1-to-1 mapping between |
| 892 | * state and styles for the cases where the server is (still) |
| 893 | * up. The reason is that we don't want to report nolb and |
| 894 | * drain with the same color. |
| 895 | */ |
| 896 | |
| 897 | if (strcmp(field_str(stats, ST_F_STATUS), "DOWN") == 0 || |
| 898 | strcmp(field_str(stats, ST_F_STATUS), "DOWN (agent)") == 0) { |
| 899 | style = "down"; |
| 900 | } |
| 901 | else if (strcmp(field_str(stats, ST_F_STATUS), "DOWN ") == 0) { |
| 902 | style = "going_up"; |
| 903 | } |
| 904 | else if (strcmp(field_str(stats, ST_F_STATUS), "NOLB ") == 0) { |
| 905 | style = "going_down"; |
| 906 | } |
| 907 | else if (strcmp(field_str(stats, ST_F_STATUS), "NOLB") == 0) { |
| 908 | style = "nolb"; |
| 909 | } |
| 910 | else if (strcmp(field_str(stats, ST_F_STATUS), "no check") == 0) { |
| 911 | style = "no_check"; |
| 912 | } |
| 913 | else if (!stats[ST_F_CHKFAIL].type || |
| 914 | stats[ST_F_CHECK_HEALTH].u.u32 == stats[ST_F_CHECK_RISE].u.u32 + stats[ST_F_CHECK_FALL].u.u32 - 1) { |
| 915 | /* no check or max health = UP */ |
| 916 | if (stats[ST_F_WEIGHT].u.u32) |
| 917 | style = "up"; |
| 918 | else |
| 919 | style = "draining"; |
| 920 | } |
| 921 | else { |
| 922 | style = "going_down"; |
| 923 | } |
| 924 | |
| 925 | if (memcmp(field_str(stats, ST_F_STATUS), "MAINT", 5) == 0) |
| 926 | chunk_appendf(out, "<tr class=\"maintain\">"); |
| 927 | else |
| 928 | chunk_appendf(out, |
| 929 | "<tr class=\"%s_%s\">", |
| 930 | (stats[ST_F_BCK].u.u32) ? "backup" : "active", style); |
| 931 | |
| 932 | |
| 933 | if (flags & ST_SHOWADMIN) |
| 934 | chunk_appendf(out, |
David Harrigan | d3db35a | 2016-12-30 12:12:49 +0000 | [diff] [blame] | 935 | "<td><input class='%s-checkbox' type=\"checkbox\" name=\"s\" value=\"%s\"></td>", |
| 936 | field_str(stats, ST_F_PXNAME), |
William Lallemand | 74c24fb | 2016-11-21 17:18:36 +0100 | [diff] [blame] | 937 | field_str(stats, ST_F_SVNAME)); |
| 938 | |
| 939 | chunk_appendf(out, |
| 940 | "<td class=ac><a name=\"%s/%s\"></a>%s" |
| 941 | "<a class=lfsb href=\"#%s/%s\">%s</a>" |
| 942 | "", |
| 943 | field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME), |
| 944 | (flags & ST_SHLGNDS) ? "<u>" : "", |
| 945 | field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME), field_str(stats, ST_F_SVNAME)); |
| 946 | |
| 947 | if (flags & ST_SHLGNDS) { |
| 948 | chunk_appendf(out, "<div class=tips>"); |
| 949 | |
| 950 | if (isdigit(*field_str(stats, ST_F_ADDR))) |
| 951 | chunk_appendf(out, "IPv4: %s, ", field_str(stats, ST_F_ADDR)); |
| 952 | else if (*field_str(stats, ST_F_ADDR) == '[') |
| 953 | chunk_appendf(out, "IPv6: %s, ", field_str(stats, ST_F_ADDR)); |
| 954 | else if (*field_str(stats, ST_F_ADDR)) |
| 955 | chunk_appendf(out, "%s, ", field_str(stats, ST_F_ADDR)); |
| 956 | |
| 957 | /* id */ |
| 958 | chunk_appendf(out, "id: %d", stats[ST_F_SID].u.u32); |
| 959 | |
| 960 | /* cookie */ |
| 961 | if (stats[ST_F_COOKIE].type) { |
| 962 | chunk_appendf(out, ", cookie: '"); |
| 963 | chunk_initstr(&src, field_str(stats, ST_F_COOKIE)); |
| 964 | chunk_htmlencode(out, &src); |
| 965 | chunk_appendf(out, "'"); |
| 966 | } |
| 967 | |
| 968 | chunk_appendf(out, "</div>"); |
| 969 | } |
| 970 | |
| 971 | chunk_appendf(out, |
| 972 | /* queue : current, max, limit */ |
| 973 | "%s</td><td>%s</td><td>%s</td><td>%s</td>" |
| 974 | /* sessions rate : current, max, limit */ |
| 975 | "<td>%s</td><td>%s</td><td></td>" |
| 976 | "", |
| 977 | (flags & ST_SHLGNDS) ? "</u>" : "", |
| 978 | U2H(stats[ST_F_QCUR].u.u32), U2H(stats[ST_F_QMAX].u.u32), LIM2A(stats[ST_F_QLIMIT].u.u32, "-"), |
| 979 | U2H(stats[ST_F_RATE].u.u32), U2H(stats[ST_F_RATE_MAX].u.u32)); |
| 980 | |
| 981 | chunk_appendf(out, |
| 982 | /* sessions: current, max, limit, total */ |
| 983 | "<td>%s</td><td>%s</td><td>%s</td>" |
| 984 | "<td><u>%s<div class=tips><table class=det>" |
| 985 | "<tr><th>Cum. sessions:</th><td>%s</td></tr>" |
| 986 | "", |
| 987 | U2H(stats[ST_F_SCUR].u.u32), U2H(stats[ST_F_SMAX].u.u32), LIM2A(stats[ST_F_SLIM].u.u32, "-"), |
| 988 | U2H(stats[ST_F_STOT].u.u64), |
| 989 | U2H(stats[ST_F_STOT].u.u64)); |
| 990 | |
| 991 | /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */ |
| 992 | if (strcmp(field_str(stats, ST_F_MODE), "http") == 0) { |
| 993 | unsigned long long tot; |
| 994 | |
| 995 | tot = stats[ST_F_HRSP_OTHER].u.u64; |
| 996 | tot += stats[ST_F_HRSP_1XX].u.u64; |
| 997 | tot += stats[ST_F_HRSP_2XX].u.u64; |
| 998 | tot += stats[ST_F_HRSP_3XX].u.u64; |
| 999 | tot += stats[ST_F_HRSP_4XX].u.u64; |
| 1000 | tot += stats[ST_F_HRSP_5XX].u.u64; |
| 1001 | |
| 1002 | chunk_appendf(out, |
Willy Tarreau | f157384 | 2018-12-14 11:35:36 +0100 | [diff] [blame] | 1003 | "<tr><th>New connections:</th><td>%s</td></tr>" |
| 1004 | "<tr><th>Reused connections:</th><td>%s</td><td>(%d%%)</td></tr>" |
William Lallemand | 74c24fb | 2016-11-21 17:18:36 +0100 | [diff] [blame] | 1005 | "<tr><th>Cum. HTTP responses:</th><td>%s</td></tr>" |
| 1006 | "<tr><th>- HTTP 1xx responses:</th><td>%s</td><td>(%d%%)</td></tr>" |
| 1007 | "<tr><th>- HTTP 2xx responses:</th><td>%s</td><td>(%d%%)</td></tr>" |
| 1008 | "<tr><th>- HTTP 3xx responses:</th><td>%s</td><td>(%d%%)</td></tr>" |
| 1009 | "<tr><th>- HTTP 4xx responses:</th><td>%s</td><td>(%d%%)</td></tr>" |
| 1010 | "<tr><th>- HTTP 5xx responses:</th><td>%s</td><td>(%d%%)</td></tr>" |
| 1011 | "<tr><th>- other responses:</th><td>%s</td><td>(%d%%)</td></tr>" |
Willy Tarreau | 1b0f85e | 2018-05-28 15:12:40 +0200 | [diff] [blame] | 1012 | "<tr><th>Failed hdr rewrites:</th><td>%s</td></tr>" |
William Lallemand | 74c24fb | 2016-11-21 17:18:36 +0100 | [diff] [blame] | 1013 | "", |
Willy Tarreau | f157384 | 2018-12-14 11:35:36 +0100 | [diff] [blame] | 1014 | U2H(stats[ST_F_CONNECT].u.u64), |
| 1015 | U2H(stats[ST_F_REUSE].u.u64), |
| 1016 | (stats[ST_F_CONNECT].u.u64 + stats[ST_F_REUSE].u.u64) ? |
| 1017 | (int)(100 * stats[ST_F_REUSE].u.u64 / (stats[ST_F_CONNECT].u.u64 + stats[ST_F_REUSE].u.u64)) : 0, |
William Lallemand | 74c24fb | 2016-11-21 17:18:36 +0100 | [diff] [blame] | 1018 | U2H(tot), |
| 1019 | U2H(stats[ST_F_HRSP_1XX].u.u64), tot ? (int)(100 * stats[ST_F_HRSP_1XX].u.u64 / tot) : 0, |
| 1020 | U2H(stats[ST_F_HRSP_2XX].u.u64), tot ? (int)(100 * stats[ST_F_HRSP_2XX].u.u64 / tot) : 0, |
| 1021 | U2H(stats[ST_F_HRSP_3XX].u.u64), tot ? (int)(100 * stats[ST_F_HRSP_3XX].u.u64 / tot) : 0, |
| 1022 | U2H(stats[ST_F_HRSP_4XX].u.u64), tot ? (int)(100 * stats[ST_F_HRSP_4XX].u.u64 / tot) : 0, |
| 1023 | U2H(stats[ST_F_HRSP_5XX].u.u64), tot ? (int)(100 * stats[ST_F_HRSP_5XX].u.u64 / tot) : 0, |
Willy Tarreau | 1b0f85e | 2018-05-28 15:12:40 +0200 | [diff] [blame] | 1024 | U2H(stats[ST_F_HRSP_OTHER].u.u64), tot ? (int)(100 * stats[ST_F_HRSP_OTHER].u.u64 / tot) : 0, |
| 1025 | U2H(stats[ST_F_WREW].u.u64)); |
William Lallemand | 74c24fb | 2016-11-21 17:18:36 +0100 | [diff] [blame] | 1026 | } |
| 1027 | |
| 1028 | chunk_appendf(out, "<tr><th colspan=3>Avg over last 1024 success. conn.</th></tr>"); |
| 1029 | chunk_appendf(out, "<tr><th>- Queue time:</th><td>%s</td><td>ms</td></tr>", U2H(stats[ST_F_QTIME].u.u32)); |
| 1030 | chunk_appendf(out, "<tr><th>- Connect time:</th><td>%s</td><td>ms</td></tr>", U2H(stats[ST_F_CTIME].u.u32)); |
| 1031 | if (strcmp(field_str(stats, ST_F_MODE), "http") == 0) |
| 1032 | chunk_appendf(out, "<tr><th>- Response time:</th><td>%s</td><td>ms</td></tr>", U2H(stats[ST_F_RTIME].u.u32)); |
| 1033 | chunk_appendf(out, "<tr><th>- Total time:</th><td>%s</td><td>ms</td></tr>", U2H(stats[ST_F_TTIME].u.u32)); |
| 1034 | |
| 1035 | chunk_appendf(out, |
| 1036 | "</table></div></u></td>" |
| 1037 | /* sessions: lbtot, last */ |
| 1038 | "<td>%s</td><td>%s</td>", |
| 1039 | U2H(stats[ST_F_LBTOT].u.u64), |
| 1040 | human_time(stats[ST_F_LASTSESS].u.s32, 1)); |
| 1041 | |
| 1042 | chunk_appendf(out, |
| 1043 | /* bytes : in, out */ |
| 1044 | "<td>%s</td><td>%s</td>" |
| 1045 | /* denied: req, resp */ |
| 1046 | "<td></td><td>%s</td>" |
| 1047 | /* errors : request, connect */ |
| 1048 | "<td></td><td>%s</td>" |
| 1049 | /* errors : response */ |
| 1050 | "<td><u>%s<div class=tips>Connection resets during transfers: %lld client, %lld server</div></u></td>" |
| 1051 | /* warnings: retries, redispatches */ |
| 1052 | "<td>%lld</td><td>%lld</td>" |
| 1053 | "", |
| 1054 | U2H(stats[ST_F_BIN].u.u64), U2H(stats[ST_F_BOUT].u.u64), |
| 1055 | U2H(stats[ST_F_DRESP].u.u64), |
| 1056 | U2H(stats[ST_F_ECON].u.u64), |
| 1057 | U2H(stats[ST_F_ERESP].u.u64), |
| 1058 | (long long)stats[ST_F_CLI_ABRT].u.u64, |
| 1059 | (long long)stats[ST_F_SRV_ABRT].u.u64, |
| 1060 | (long long)stats[ST_F_WRETR].u.u64, |
| 1061 | (long long)stats[ST_F_WREDIS].u.u64); |
| 1062 | |
| 1063 | /* status, last change */ |
| 1064 | chunk_appendf(out, "<td class=ac>"); |
| 1065 | |
| 1066 | /* FIXME!!!! |
| 1067 | * LASTCHG should contain the last change for *this* server and must be computed |
| 1068 | * properly above, as was done below, ie: this server if maint, otherwise ref server |
| 1069 | * if tracking. Note that ref is either local or remote depending on tracking. |
| 1070 | */ |
| 1071 | |
| 1072 | |
| 1073 | if (memcmp(field_str(stats, ST_F_STATUS), "MAINT", 5) == 0) { |
| 1074 | chunk_appendf(out, "%s MAINT", human_time(stats[ST_F_LASTCHG].u.u32, 1)); |
| 1075 | } |
| 1076 | else if (memcmp(field_str(stats, ST_F_STATUS), "no check", 5) == 0) { |
| 1077 | chunk_strcat(out, "<i>no check</i>"); |
| 1078 | } |
| 1079 | else { |
| 1080 | chunk_appendf(out, "%s %s", human_time(stats[ST_F_LASTCHG].u.u32, 1), field_str(stats, ST_F_STATUS)); |
| 1081 | if (memcmp(field_str(stats, ST_F_STATUS), "DOWN", 4) == 0) { |
| 1082 | if (stats[ST_F_CHECK_HEALTH].u.u32) |
| 1083 | chunk_strcat(out, " ↑"); |
| 1084 | } |
| 1085 | else if (stats[ST_F_CHECK_HEALTH].u.u32 < stats[ST_F_CHECK_RISE].u.u32 + stats[ST_F_CHECK_FALL].u.u32 - 1) |
| 1086 | chunk_strcat(out, " ↓"); |
| 1087 | } |
| 1088 | |
| 1089 | if (memcmp(field_str(stats, ST_F_STATUS), "DOWN", 4) == 0 && |
| 1090 | stats[ST_F_AGENT_STATUS].type && !stats[ST_F_AGENT_HEALTH].u.u32) { |
| 1091 | chunk_appendf(out, |
| 1092 | "</td><td class=ac><u> %s", |
| 1093 | field_str(stats, ST_F_AGENT_STATUS)); |
| 1094 | |
| 1095 | if (stats[ST_F_AGENT_CODE].type) |
| 1096 | chunk_appendf(out, "/%d", stats[ST_F_AGENT_CODE].u.u32); |
| 1097 | |
| 1098 | if (stats[ST_F_AGENT_DURATION].type) |
| 1099 | chunk_appendf(out, " in %lums", (long)stats[ST_F_AGENT_DURATION].u.u64); |
| 1100 | |
| 1101 | chunk_appendf(out, "<div class=tips>%s", field_str(stats, ST_F_AGENT_DESC)); |
| 1102 | |
| 1103 | if (*field_str(stats, ST_F_LAST_AGT)) { |
| 1104 | chunk_appendf(out, ": "); |
| 1105 | chunk_initstr(&src, field_str(stats, ST_F_LAST_AGT)); |
| 1106 | chunk_htmlencode(out, &src); |
| 1107 | } |
| 1108 | chunk_appendf(out, "</div></u>"); |
| 1109 | } |
| 1110 | else if (stats[ST_F_CHECK_STATUS].type) { |
| 1111 | chunk_appendf(out, |
| 1112 | "</td><td class=ac><u> %s", |
| 1113 | field_str(stats, ST_F_CHECK_STATUS)); |
| 1114 | |
| 1115 | if (stats[ST_F_CHECK_CODE].type) |
| 1116 | chunk_appendf(out, "/%d", stats[ST_F_CHECK_CODE].u.u32); |
| 1117 | |
| 1118 | if (stats[ST_F_CHECK_DURATION].type) |
| 1119 | chunk_appendf(out, " in %lums", (long)stats[ST_F_CHECK_DURATION].u.u64); |
| 1120 | |
| 1121 | chunk_appendf(out, "<div class=tips>%s", field_str(stats, ST_F_CHECK_DESC)); |
| 1122 | |
| 1123 | if (*field_str(stats, ST_F_LAST_CHK)) { |
| 1124 | chunk_appendf(out, ": "); |
| 1125 | chunk_initstr(&src, field_str(stats, ST_F_LAST_CHK)); |
| 1126 | chunk_htmlencode(out, &src); |
| 1127 | } |
| 1128 | chunk_appendf(out, "</div></u>"); |
| 1129 | } |
| 1130 | else |
| 1131 | chunk_appendf(out, "</td><td>"); |
| 1132 | |
| 1133 | chunk_appendf(out, |
| 1134 | /* weight */ |
| 1135 | "</td><td class=ac>%d</td>" |
| 1136 | /* act, bck */ |
| 1137 | "<td class=ac>%s</td><td class=ac>%s</td>" |
| 1138 | "", |
| 1139 | stats[ST_F_WEIGHT].u.u32, |
| 1140 | stats[ST_F_BCK].u.u32 ? "-" : "Y", |
| 1141 | stats[ST_F_BCK].u.u32 ? "Y" : "-"); |
| 1142 | |
| 1143 | /* check failures: unique, fatal, down time */ |
| 1144 | if (strcmp(field_str(stats, ST_F_STATUS), "MAINT (resolution)") == 0) { |
| 1145 | chunk_appendf(out, "<td class=ac colspan=3>resolution</td>"); |
| 1146 | } |
| 1147 | else if (stats[ST_F_CHKFAIL].type) { |
| 1148 | chunk_appendf(out, "<td><u>%lld", (long long)stats[ST_F_CHKFAIL].u.u64); |
| 1149 | |
| 1150 | if (stats[ST_F_HANAFAIL].type) |
| 1151 | chunk_appendf(out, "/%lld", (long long)stats[ST_F_HANAFAIL].u.u64); |
| 1152 | |
| 1153 | chunk_appendf(out, |
| 1154 | "<div class=tips>Failed Health Checks%s</div></u></td>" |
| 1155 | "<td>%lld</td><td>%s</td>" |
| 1156 | "", |
| 1157 | stats[ST_F_HANAFAIL].type ? "/Health Analyses" : "", |
| 1158 | (long long)stats[ST_F_CHKDOWN].u.u64, human_time(stats[ST_F_DOWNTIME].u.u32, 1)); |
| 1159 | } |
| 1160 | else if (strcmp(field_str(stats, ST_F_STATUS), "MAINT") != 0 && field_format(stats, ST_F_TRACKED) == FF_STR) { |
| 1161 | /* tracking a server (hence inherited maint would appear as "MAINT (via...)" */ |
| 1162 | chunk_appendf(out, |
| 1163 | "<td class=ac colspan=3><a class=lfsb href=\"#%s\">via %s</a></td>", |
| 1164 | field_str(stats, ST_F_TRACKED), field_str(stats, ST_F_TRACKED)); |
| 1165 | } |
| 1166 | else |
| 1167 | chunk_appendf(out, "<td colspan=3></td>"); |
| 1168 | |
| 1169 | /* throttle */ |
| 1170 | if (stats[ST_F_THROTTLE].type) |
| 1171 | chunk_appendf(out, "<td class=ac>%d %%</td></tr>\n", stats[ST_F_THROTTLE].u.u32); |
| 1172 | else |
| 1173 | chunk_appendf(out, "<td class=ac>-</td></tr>\n"); |
| 1174 | } |
| 1175 | else if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_BE) { |
| 1176 | chunk_appendf(out, "<tr class=\"backend\">"); |
| 1177 | if (flags & ST_SHOWADMIN) { |
| 1178 | /* Column sub-heading for Enable or Disable server */ |
| 1179 | chunk_appendf(out, "<td></td>"); |
| 1180 | } |
| 1181 | chunk_appendf(out, |
| 1182 | "<td class=ac>" |
| 1183 | /* name */ |
| 1184 | "%s<a name=\"%s/Backend\"></a>" |
| 1185 | "<a class=lfsb href=\"#%s/Backend\">Backend</a>" |
| 1186 | "", |
| 1187 | (flags & ST_SHLGNDS)?"<u>":"", |
| 1188 | field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_PXNAME)); |
| 1189 | |
| 1190 | if (flags & ST_SHLGNDS) { |
| 1191 | /* balancing */ |
| 1192 | chunk_appendf(out, "<div class=tips>balancing: %s", |
| 1193 | field_str(stats, ST_F_ALGO)); |
| 1194 | |
| 1195 | /* cookie */ |
| 1196 | if (stats[ST_F_COOKIE].type) { |
| 1197 | chunk_appendf(out, ", cookie: '"); |
| 1198 | chunk_initstr(&src, field_str(stats, ST_F_COOKIE)); |
| 1199 | chunk_htmlencode(out, &src); |
| 1200 | chunk_appendf(out, "'"); |
| 1201 | } |
| 1202 | chunk_appendf(out, "</div>"); |
| 1203 | } |
| 1204 | |
| 1205 | chunk_appendf(out, |
| 1206 | "%s</td>" |
| 1207 | /* queue : current, max */ |
| 1208 | "<td>%s</td><td>%s</td><td></td>" |
| 1209 | /* sessions rate : current, max, limit */ |
| 1210 | "<td>%s</td><td>%s</td><td></td>" |
| 1211 | "", |
| 1212 | (flags & ST_SHLGNDS)?"</u>":"", |
| 1213 | U2H(stats[ST_F_QCUR].u.u32), U2H(stats[ST_F_QMAX].u.u32), |
| 1214 | U2H(stats[ST_F_RATE].u.u32), U2H(stats[ST_F_RATE_MAX].u.u32)); |
| 1215 | |
| 1216 | chunk_appendf(out, |
| 1217 | /* sessions: current, max, limit, total */ |
| 1218 | "<td>%s</td><td>%s</td><td>%s</td>" |
| 1219 | "<td><u>%s<div class=tips><table class=det>" |
| 1220 | "<tr><th>Cum. sessions:</th><td>%s</td></tr>" |
| 1221 | "", |
Willy Tarreau | 8e0f175 | 2016-12-12 15:07:29 +0100 | [diff] [blame] | 1222 | U2H(stats[ST_F_SCUR].u.u32), U2H(stats[ST_F_SMAX].u.u32), U2H(stats[ST_F_SLIM].u.u32), |
William Lallemand | 74c24fb | 2016-11-21 17:18:36 +0100 | [diff] [blame] | 1223 | U2H(stats[ST_F_STOT].u.u64), |
| 1224 | U2H(stats[ST_F_STOT].u.u64)); |
| 1225 | |
| 1226 | /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */ |
| 1227 | if (strcmp(field_str(stats, ST_F_MODE), "http") == 0) { |
| 1228 | chunk_appendf(out, |
Willy Tarreau | f157384 | 2018-12-14 11:35:36 +0100 | [diff] [blame] | 1229 | "<tr><th>New connections:</th><td>%s</td></tr>" |
| 1230 | "<tr><th>Reused connections:</th><td>%s</td><td>(%d%%)</td></tr>" |
William Lallemand | 74c24fb | 2016-11-21 17:18:36 +0100 | [diff] [blame] | 1231 | "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>" |
| 1232 | "<tr><th>- HTTP 1xx responses:</th><td>%s</td></tr>" |
| 1233 | "<tr><th>- HTTP 2xx responses:</th><td>%s</td></tr>" |
| 1234 | "<tr><th> Compressed 2xx:</th><td>%s</td><td>(%d%%)</td></tr>" |
| 1235 | "<tr><th>- HTTP 3xx responses:</th><td>%s</td></tr>" |
| 1236 | "<tr><th>- HTTP 4xx responses:</th><td>%s</td></tr>" |
| 1237 | "<tr><th>- HTTP 5xx responses:</th><td>%s</td></tr>" |
| 1238 | "<tr><th>- other responses:</th><td>%s</td></tr>" |
Willy Tarreau | a1214a5 | 2018-12-14 14:00:25 +0100 | [diff] [blame] | 1239 | "<tr><th>Cache lookups:</th><td>%s</td></tr>" |
| 1240 | "<tr><th>Cache hits:</th><td>%s</td><td>(%d%%)</td></tr>" |
Willy Tarreau | 1b0f85e | 2018-05-28 15:12:40 +0200 | [diff] [blame] | 1241 | "<tr><th>Failed hdr rewrites:</th><td>%s</td></tr>" |
William Lallemand | 74c24fb | 2016-11-21 17:18:36 +0100 | [diff] [blame] | 1242 | "<tr><th colspan=3>Avg over last 1024 success. conn.</th></tr>" |
| 1243 | "", |
Willy Tarreau | f157384 | 2018-12-14 11:35:36 +0100 | [diff] [blame] | 1244 | U2H(stats[ST_F_CONNECT].u.u64), |
| 1245 | U2H(stats[ST_F_REUSE].u.u64), |
| 1246 | (stats[ST_F_CONNECT].u.u64 + stats[ST_F_REUSE].u.u64) ? |
| 1247 | (int)(100 * stats[ST_F_REUSE].u.u64 / (stats[ST_F_CONNECT].u.u64 + stats[ST_F_REUSE].u.u64)) : 0, |
William Lallemand | 74c24fb | 2016-11-21 17:18:36 +0100 | [diff] [blame] | 1248 | U2H(stats[ST_F_REQ_TOT].u.u64), |
| 1249 | U2H(stats[ST_F_HRSP_1XX].u.u64), |
| 1250 | U2H(stats[ST_F_HRSP_2XX].u.u64), |
| 1251 | U2H(stats[ST_F_COMP_RSP].u.u64), |
| 1252 | stats[ST_F_HRSP_2XX].u.u64 ? |
| 1253 | (int)(100 * stats[ST_F_COMP_RSP].u.u64 / stats[ST_F_HRSP_2XX].u.u64) : 0, |
| 1254 | U2H(stats[ST_F_HRSP_3XX].u.u64), |
| 1255 | U2H(stats[ST_F_HRSP_4XX].u.u64), |
| 1256 | U2H(stats[ST_F_HRSP_5XX].u.u64), |
Willy Tarreau | feead3a | 2018-12-14 13:48:44 +0100 | [diff] [blame] | 1257 | U2H(stats[ST_F_HRSP_OTHER].u.u64), |
Willy Tarreau | a1214a5 | 2018-12-14 14:00:25 +0100 | [diff] [blame] | 1258 | U2H(stats[ST_F_CACHE_LOOKUPS].u.u64), |
| 1259 | U2H(stats[ST_F_CACHE_HITS].u.u64), |
| 1260 | stats[ST_F_CACHE_LOOKUPS].u.u64 ? |
| 1261 | (int)(100 * stats[ST_F_CACHE_HITS].u.u64 / stats[ST_F_CACHE_LOOKUPS].u.u64) : 0, |
Willy Tarreau | feead3a | 2018-12-14 13:48:44 +0100 | [diff] [blame] | 1262 | U2H(stats[ST_F_WREW].u.u64)); |
William Lallemand | 74c24fb | 2016-11-21 17:18:36 +0100 | [diff] [blame] | 1263 | } |
| 1264 | |
| 1265 | chunk_appendf(out, "<tr><th>- Queue time:</th><td>%s</td><td>ms</td></tr>", U2H(stats[ST_F_QTIME].u.u32)); |
| 1266 | chunk_appendf(out, "<tr><th>- Connect time:</th><td>%s</td><td>ms</td></tr>", U2H(stats[ST_F_CTIME].u.u32)); |
| 1267 | if (strcmp(field_str(stats, ST_F_MODE), "http") == 0) |
| 1268 | chunk_appendf(out, "<tr><th>- Response time:</th><td>%s</td><td>ms</td></tr>", U2H(stats[ST_F_RTIME].u.u32)); |
| 1269 | chunk_appendf(out, "<tr><th>- Total time:</th><td>%s</td><td>ms</td></tr>", U2H(stats[ST_F_TTIME].u.u32)); |
| 1270 | |
| 1271 | chunk_appendf(out, |
| 1272 | "</table></div></u></td>" |
| 1273 | /* sessions: lbtot, last */ |
| 1274 | "<td>%s</td><td>%s</td>" |
| 1275 | /* bytes: in */ |
| 1276 | "<td>%s</td>" |
| 1277 | "", |
| 1278 | U2H(stats[ST_F_LBTOT].u.u64), |
| 1279 | human_time(stats[ST_F_LASTSESS].u.s32, 1), |
| 1280 | U2H(stats[ST_F_BIN].u.u64)); |
| 1281 | |
| 1282 | chunk_appendf(out, |
| 1283 | /* bytes:out + compression stats (via hover): comp_in, comp_out, comp_byp */ |
| 1284 | "<td>%s%s<div class=tips><table class=det>" |
| 1285 | "<tr><th>Response bytes in:</th><td>%s</td></tr>" |
| 1286 | "<tr><th>Compression in:</th><td>%s</td></tr>" |
| 1287 | "<tr><th>Compression out:</th><td>%s</td><td>(%d%%)</td></tr>" |
| 1288 | "<tr><th>Compression bypass:</th><td>%s</td></tr>" |
| 1289 | "<tr><th>Total bytes saved:</th><td>%s</td><td>(%d%%)</td></tr>" |
| 1290 | "</table></div>%s</td>", |
| 1291 | (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "<u>":"", |
| 1292 | U2H(stats[ST_F_BOUT].u.u64), |
| 1293 | U2H(stats[ST_F_BOUT].u.u64), |
| 1294 | U2H(stats[ST_F_COMP_IN].u.u64), |
| 1295 | U2H(stats[ST_F_COMP_OUT].u.u64), |
| 1296 | stats[ST_F_COMP_IN].u.u64 ? (int)(stats[ST_F_COMP_OUT].u.u64 * 100 / stats[ST_F_COMP_IN].u.u64) : 0, |
| 1297 | U2H(stats[ST_F_COMP_BYP].u.u64), |
| 1298 | U2H(stats[ST_F_COMP_IN].u.u64 - stats[ST_F_COMP_OUT].u.u64), |
| 1299 | stats[ST_F_BOUT].u.u64 ? (int)((stats[ST_F_COMP_IN].u.u64 - stats[ST_F_COMP_OUT].u.u64) * 100 / stats[ST_F_BOUT].u.u64) : 0, |
| 1300 | (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "</u>":""); |
| 1301 | |
| 1302 | chunk_appendf(out, |
| 1303 | /* denied: req, resp */ |
| 1304 | "<td>%s</td><td>%s</td>" |
| 1305 | /* errors : request, connect */ |
| 1306 | "<td></td><td>%s</td>" |
| 1307 | /* errors : response */ |
| 1308 | "<td><u>%s<div class=tips>Connection resets during transfers: %lld client, %lld server</div></u></td>" |
| 1309 | /* warnings: retries, redispatches */ |
| 1310 | "<td>%lld</td><td>%lld</td>" |
| 1311 | /* backend status: reflect backend status (up/down): we display UP |
| 1312 | * if the backend has known working servers or if it has no server at |
| 1313 | * all (eg: for stats). Then we display the total weight, number of |
| 1314 | * active and backups. */ |
| 1315 | "<td class=ac>%s %s</td><td class=ac> </td><td class=ac>%d</td>" |
| 1316 | "<td class=ac>%d</td><td class=ac>%d</td>" |
| 1317 | "", |
| 1318 | U2H(stats[ST_F_DREQ].u.u64), U2H(stats[ST_F_DRESP].u.u64), |
| 1319 | U2H(stats[ST_F_ECON].u.u64), |
| 1320 | U2H(stats[ST_F_ERESP].u.u64), |
| 1321 | (long long)stats[ST_F_CLI_ABRT].u.u64, |
| 1322 | (long long)stats[ST_F_SRV_ABRT].u.u64, |
| 1323 | (long long)stats[ST_F_WRETR].u.u64, (long long)stats[ST_F_WREDIS].u.u64, |
| 1324 | human_time(stats[ST_F_LASTCHG].u.u32, 1), |
| 1325 | strcmp(field_str(stats, ST_F_STATUS), "DOWN") ? field_str(stats, ST_F_STATUS) : "<font color=\"red\"><b>DOWN</b></font>", |
| 1326 | stats[ST_F_WEIGHT].u.u32, |
| 1327 | stats[ST_F_ACT].u.u32, stats[ST_F_BCK].u.u32); |
| 1328 | |
| 1329 | chunk_appendf(out, |
| 1330 | /* rest of backend: nothing, down transitions, total downtime, throttle */ |
| 1331 | "<td class=ac> </td><td>%d</td>" |
| 1332 | "<td>%s</td>" |
| 1333 | "<td></td>" |
| 1334 | "</tr>", |
| 1335 | stats[ST_F_CHKDOWN].u.u32, |
| 1336 | stats[ST_F_DOWNTIME].type ? human_time(stats[ST_F_DOWNTIME].u.u32, 1) : " "); |
| 1337 | } |
| 1338 | return 1; |
| 1339 | } |
| 1340 | |
| 1341 | int stats_dump_one_line(const struct field *stats, unsigned int flags, struct proxy *px, struct appctx *appctx) |
| 1342 | { |
Simon Horman | 05ee213 | 2017-01-04 09:37:25 +0100 | [diff] [blame] | 1343 | int ret; |
| 1344 | |
William Lallemand | 74c24fb | 2016-11-21 17:18:36 +0100 | [diff] [blame] | 1345 | if ((px->cap & PR_CAP_BE) && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) |
| 1346 | flags |= ST_SHOWADMIN; |
| 1347 | |
| 1348 | if (appctx->ctx.stats.flags & STAT_FMT_HTML) |
Simon Horman | 05ee213 | 2017-01-04 09:37:25 +0100 | [diff] [blame] | 1349 | ret = stats_dump_fields_html(&trash, stats, flags); |
William Lallemand | 74c24fb | 2016-11-21 17:18:36 +0100 | [diff] [blame] | 1350 | else if (appctx->ctx.stats.flags & STAT_FMT_TYPED) |
Simon Horman | 05ee213 | 2017-01-04 09:37:25 +0100 | [diff] [blame] | 1351 | ret = stats_dump_fields_typed(&trash, stats); |
| 1352 | else if (appctx->ctx.stats.flags & STAT_FMT_JSON) |
| 1353 | ret = stats_dump_fields_json(&trash, stats, |
| 1354 | !(appctx->ctx.stats.flags & |
| 1355 | STAT_STARTED)); |
William Lallemand | 74c24fb | 2016-11-21 17:18:36 +0100 | [diff] [blame] | 1356 | else |
Simon Horman | 05ee213 | 2017-01-04 09:37:25 +0100 | [diff] [blame] | 1357 | ret = stats_dump_fields_csv(&trash, stats); |
| 1358 | |
| 1359 | if (ret) |
| 1360 | appctx->ctx.stats.flags |= STAT_STARTED; |
| 1361 | |
| 1362 | return ret; |
William Lallemand | 74c24fb | 2016-11-21 17:18:36 +0100 | [diff] [blame] | 1363 | } |
| 1364 | |
| 1365 | /* Fill <stats> with the frontend statistics. <stats> is |
| 1366 | * preallocated array of length <len>. The length of the array |
| 1367 | * must be at least ST_F_TOTAL_FIELDS. If this length is less then |
| 1368 | * this value, the function returns 0, otherwise, it returns 1. |
| 1369 | */ |
| 1370 | int stats_fill_fe_stats(struct proxy *px, struct field *stats, int len) |
| 1371 | { |
| 1372 | if (len < ST_F_TOTAL_FIELDS) |
| 1373 | return 0; |
| 1374 | |
| 1375 | memset(stats, 0, sizeof(*stats) * len); |
| 1376 | |
| 1377 | stats[ST_F_PXNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id); |
| 1378 | stats[ST_F_SVNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, "FRONTEND"); |
| 1379 | stats[ST_F_MODE] = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode)); |
| 1380 | stats[ST_F_SCUR] = mkf_u32(0, px->feconn); |
| 1381 | stats[ST_F_SMAX] = mkf_u32(FN_MAX, px->fe_counters.conn_max); |
| 1382 | stats[ST_F_SLIM] = mkf_u32(FO_CONFIG|FN_LIMIT, px->maxconn); |
| 1383 | stats[ST_F_STOT] = mkf_u64(FN_COUNTER, px->fe_counters.cum_sess); |
| 1384 | stats[ST_F_BIN] = mkf_u64(FN_COUNTER, px->fe_counters.bytes_in); |
| 1385 | stats[ST_F_BOUT] = mkf_u64(FN_COUNTER, px->fe_counters.bytes_out); |
| 1386 | stats[ST_F_DREQ] = mkf_u64(FN_COUNTER, px->fe_counters.denied_req); |
| 1387 | stats[ST_F_DRESP] = mkf_u64(FN_COUNTER, px->fe_counters.denied_resp); |
| 1388 | stats[ST_F_EREQ] = mkf_u64(FN_COUNTER, px->fe_counters.failed_req); |
| 1389 | stats[ST_F_DCON] = mkf_u64(FN_COUNTER, px->fe_counters.denied_conn); |
| 1390 | stats[ST_F_DSES] = mkf_u64(FN_COUNTER, px->fe_counters.denied_sess); |
| 1391 | stats[ST_F_STATUS] = mkf_str(FO_STATUS, px->state == PR_STREADY ? "OPEN" : px->state == PR_STFULL ? "FULL" : "STOP"); |
| 1392 | stats[ST_F_PID] = mkf_u32(FO_KEY, relative_pid); |
| 1393 | stats[ST_F_IID] = mkf_u32(FO_KEY|FS_SERVICE, px->uuid); |
| 1394 | stats[ST_F_SID] = mkf_u32(FO_KEY|FS_SERVICE, 0); |
| 1395 | stats[ST_F_TYPE] = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_FE); |
| 1396 | stats[ST_F_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&px->fe_sess_per_sec)); |
| 1397 | stats[ST_F_RATE_LIM] = mkf_u32(FO_CONFIG|FN_LIMIT, px->fe_sps_lim); |
| 1398 | stats[ST_F_RATE_MAX] = mkf_u32(FN_MAX, px->fe_counters.sps_max); |
Tim Duesterhus | 3fd1973 | 2018-05-27 20:35:08 +0200 | [diff] [blame] | 1399 | stats[ST_F_WREW] = mkf_u64(FN_COUNTER, px->fe_counters.failed_rewrites); |
William Lallemand | 74c24fb | 2016-11-21 17:18:36 +0100 | [diff] [blame] | 1400 | |
| 1401 | /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */ |
| 1402 | if (px->mode == PR_MODE_HTTP) { |
| 1403 | stats[ST_F_HRSP_1XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[1]); |
| 1404 | stats[ST_F_HRSP_2XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[2]); |
| 1405 | stats[ST_F_HRSP_3XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[3]); |
| 1406 | stats[ST_F_HRSP_4XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[4]); |
| 1407 | stats[ST_F_HRSP_5XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[5]); |
| 1408 | stats[ST_F_HRSP_OTHER] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[0]); |
| 1409 | stats[ST_F_INTERCEPTED] = mkf_u64(FN_COUNTER, px->fe_counters.intercepted_req); |
Willy Tarreau | a1214a5 | 2018-12-14 14:00:25 +0100 | [diff] [blame] | 1410 | stats[ST_F_CACHE_LOOKUPS] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.cache_lookups); |
| 1411 | stats[ST_F_CACHE_HITS] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.cache_hits); |
William Lallemand | 74c24fb | 2016-11-21 17:18:36 +0100 | [diff] [blame] | 1412 | } |
| 1413 | |
| 1414 | /* requests : req_rate, req_rate_max, req_tot, */ |
| 1415 | stats[ST_F_REQ_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&px->fe_req_per_sec)); |
| 1416 | stats[ST_F_REQ_RATE_MAX] = mkf_u32(FN_MAX, px->fe_counters.p.http.rps_max); |
| 1417 | stats[ST_F_REQ_TOT] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.cum_req); |
| 1418 | |
| 1419 | /* compression: in, out, bypassed, responses */ |
| 1420 | stats[ST_F_COMP_IN] = mkf_u64(FN_COUNTER, px->fe_counters.comp_in); |
| 1421 | stats[ST_F_COMP_OUT] = mkf_u64(FN_COUNTER, px->fe_counters.comp_out); |
| 1422 | stats[ST_F_COMP_BYP] = mkf_u64(FN_COUNTER, px->fe_counters.comp_byp); |
| 1423 | stats[ST_F_COMP_RSP] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.comp_rsp); |
| 1424 | |
| 1425 | /* connections : conn_rate, conn_rate_max, conn_tot, conn_max */ |
| 1426 | stats[ST_F_CONN_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&px->fe_conn_per_sec)); |
| 1427 | stats[ST_F_CONN_RATE_MAX] = mkf_u32(FN_MAX, px->fe_counters.cps_max); |
| 1428 | stats[ST_F_CONN_TOT] = mkf_u64(FN_COUNTER, px->fe_counters.cum_conn); |
| 1429 | |
| 1430 | return 1; |
| 1431 | } |
| 1432 | |
| 1433 | /* Dumps a frontend's line to the trash for the current proxy <px> and uses |
| 1434 | * the state from stream interface <si>. The caller is responsible for clearing |
| 1435 | * the trash if needed. Returns non-zero if it emits anything, zero otherwise. |
| 1436 | */ |
| 1437 | static int stats_dump_fe_stats(struct stream_interface *si, struct proxy *px) |
| 1438 | { |
| 1439 | struct appctx *appctx = __objt_appctx(si->end); |
| 1440 | |
| 1441 | if (!(px->cap & PR_CAP_FE)) |
| 1442 | return 0; |
| 1443 | |
| 1444 | if ((appctx->ctx.stats.flags & STAT_BOUND) && !(appctx->ctx.stats.type & (1 << STATS_TYPE_FE))) |
| 1445 | return 0; |
| 1446 | |
| 1447 | if (!stats_fill_fe_stats(px, stats, ST_F_TOTAL_FIELDS)) |
| 1448 | return 0; |
| 1449 | |
| 1450 | return stats_dump_one_line(stats, 0, px, appctx); |
| 1451 | } |
| 1452 | |
| 1453 | /* Fill <stats> with the listener statistics. <stats> is |
| 1454 | * preallocated array of length <len>. The length of the array |
| 1455 | * must be at least ST_F_TOTAL_FIELDS. If this length is less |
| 1456 | * then this value, the function returns 0, otherwise, it |
| 1457 | * returns 1. <flags> can take the value ST_SHLGNDS. |
| 1458 | */ |
| 1459 | int stats_fill_li_stats(struct proxy *px, struct listener *l, int flags, |
| 1460 | struct field *stats, int len) |
| 1461 | { |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 1462 | struct buffer *out = get_trash_chunk(); |
William Lallemand | 74c24fb | 2016-11-21 17:18:36 +0100 | [diff] [blame] | 1463 | |
| 1464 | if (len < ST_F_TOTAL_FIELDS) |
| 1465 | return 0; |
| 1466 | |
| 1467 | if (!l->counters) |
| 1468 | return 0; |
| 1469 | |
| 1470 | chunk_reset(out); |
| 1471 | memset(stats, 0, sizeof(*stats) * len); |
| 1472 | |
| 1473 | stats[ST_F_PXNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id); |
| 1474 | stats[ST_F_SVNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, l->name); |
| 1475 | stats[ST_F_MODE] = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode)); |
| 1476 | stats[ST_F_SCUR] = mkf_u32(0, l->nbconn); |
| 1477 | stats[ST_F_SMAX] = mkf_u32(FN_MAX, l->counters->conn_max); |
| 1478 | stats[ST_F_SLIM] = mkf_u32(FO_CONFIG|FN_LIMIT, l->maxconn); |
| 1479 | stats[ST_F_STOT] = mkf_u64(FN_COUNTER, l->counters->cum_conn); |
| 1480 | stats[ST_F_BIN] = mkf_u64(FN_COUNTER, l->counters->bytes_in); |
| 1481 | stats[ST_F_BOUT] = mkf_u64(FN_COUNTER, l->counters->bytes_out); |
| 1482 | stats[ST_F_DREQ] = mkf_u64(FN_COUNTER, l->counters->denied_req); |
| 1483 | stats[ST_F_DRESP] = mkf_u64(FN_COUNTER, l->counters->denied_resp); |
| 1484 | stats[ST_F_EREQ] = mkf_u64(FN_COUNTER, l->counters->failed_req); |
| 1485 | stats[ST_F_DCON] = mkf_u64(FN_COUNTER, l->counters->denied_conn); |
| 1486 | stats[ST_F_DSES] = mkf_u64(FN_COUNTER, l->counters->denied_sess); |
Willy Tarreau | a8cf66b | 2019-02-27 16:49:00 +0100 | [diff] [blame] | 1487 | stats[ST_F_STATUS] = mkf_str(FO_STATUS, (!l->maxconn || l->nbconn < l->maxconn) ? (l->state == LI_LIMITED) ? "WAITING" : "OPEN" : "FULL"); |
William Lallemand | 74c24fb | 2016-11-21 17:18:36 +0100 | [diff] [blame] | 1488 | stats[ST_F_PID] = mkf_u32(FO_KEY, relative_pid); |
| 1489 | stats[ST_F_IID] = mkf_u32(FO_KEY|FS_SERVICE, px->uuid); |
| 1490 | stats[ST_F_SID] = mkf_u32(FO_KEY|FS_SERVICE, l->luid); |
| 1491 | stats[ST_F_TYPE] = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_SO); |
Tim Duesterhus | 3fd1973 | 2018-05-27 20:35:08 +0200 | [diff] [blame] | 1492 | stats[ST_F_WREW] = mkf_u64(FN_COUNTER, l->counters->failed_rewrites); |
William Lallemand | 74c24fb | 2016-11-21 17:18:36 +0100 | [diff] [blame] | 1493 | |
| 1494 | if (flags & ST_SHLGNDS) { |
| 1495 | char str[INET6_ADDRSTRLEN]; |
| 1496 | int port; |
| 1497 | |
| 1498 | port = get_host_port(&l->addr); |
| 1499 | switch (addr_to_str(&l->addr, str, sizeof(str))) { |
| 1500 | case AF_INET: |
| 1501 | stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out)); |
| 1502 | chunk_appendf(out, "%s:%d", str, port); |
| 1503 | break; |
| 1504 | case AF_INET6: |
| 1505 | stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out)); |
| 1506 | chunk_appendf(out, "[%s]:%d", str, port); |
| 1507 | break; |
| 1508 | case AF_UNIX: |
| 1509 | stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, "unix"); |
| 1510 | break; |
| 1511 | case -1: |
| 1512 | stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out)); |
| 1513 | chunk_strcat(out, strerror(errno)); |
| 1514 | break; |
| 1515 | default: /* address family not supported */ |
| 1516 | break; |
| 1517 | } |
| 1518 | } |
| 1519 | |
| 1520 | return 1; |
| 1521 | } |
| 1522 | |
| 1523 | /* Dumps a line for listener <l> and proxy <px> to the trash and uses the state |
| 1524 | * from stream interface <si>, and stats flags <flags>. The caller is responsible |
| 1525 | * for clearing the trash if needed. Returns non-zero if it emits anything, zero |
| 1526 | * otherwise. |
| 1527 | */ |
| 1528 | static int stats_dump_li_stats(struct stream_interface *si, struct proxy *px, struct listener *l, int flags) |
| 1529 | { |
| 1530 | struct appctx *appctx = __objt_appctx(si->end); |
| 1531 | |
| 1532 | if (!stats_fill_li_stats(px, l, flags, stats, ST_F_TOTAL_FIELDS)) |
| 1533 | return 0; |
| 1534 | |
| 1535 | return stats_dump_one_line(stats, flags, px, appctx); |
| 1536 | } |
| 1537 | |
| 1538 | enum srv_stats_state { |
| 1539 | SRV_STATS_STATE_DOWN = 0, |
| 1540 | SRV_STATS_STATE_DOWN_AGENT, |
| 1541 | SRV_STATS_STATE_GOING_UP, |
| 1542 | SRV_STATS_STATE_UP_GOING_DOWN, |
| 1543 | SRV_STATS_STATE_UP, |
| 1544 | SRV_STATS_STATE_NOLB_GOING_DOWN, |
| 1545 | SRV_STATS_STATE_NOLB, |
| 1546 | SRV_STATS_STATE_DRAIN_GOING_DOWN, |
| 1547 | SRV_STATS_STATE_DRAIN, |
| 1548 | SRV_STATS_STATE_DRAIN_AGENT, |
| 1549 | SRV_STATS_STATE_NO_CHECK, |
| 1550 | |
| 1551 | SRV_STATS_STATE_COUNT, /* Must be last */ |
| 1552 | }; |
| 1553 | |
| 1554 | static const char *srv_hlt_st[SRV_STATS_STATE_COUNT] = { |
| 1555 | [SRV_STATS_STATE_DOWN] = "DOWN", |
| 1556 | [SRV_STATS_STATE_DOWN_AGENT] = "DOWN (agent)", |
| 1557 | [SRV_STATS_STATE_GOING_UP] = "DOWN %d/%d", |
| 1558 | [SRV_STATS_STATE_UP_GOING_DOWN] = "UP %d/%d", |
| 1559 | [SRV_STATS_STATE_UP] = "UP", |
| 1560 | [SRV_STATS_STATE_NOLB_GOING_DOWN] = "NOLB %d/%d", |
| 1561 | [SRV_STATS_STATE_NOLB] = "NOLB", |
| 1562 | [SRV_STATS_STATE_DRAIN_GOING_DOWN] = "DRAIN %d/%d", |
| 1563 | [SRV_STATS_STATE_DRAIN] = "DRAIN", |
| 1564 | [SRV_STATS_STATE_DRAIN_AGENT] = "DRAIN (agent)", |
| 1565 | [SRV_STATS_STATE_NO_CHECK] = "no check" |
| 1566 | }; |
| 1567 | |
| 1568 | /* Fill <stats> with the server statistics. <stats> is |
| 1569 | * preallocated array of length <len>. The length of the array |
| 1570 | * must be at least ST_F_TOTAL_FIELDS. If this length is less |
| 1571 | * then this value, the function returns 0, otherwise, it |
| 1572 | * returns 1. <flags> can take the value ST_SHLGNDS. |
| 1573 | */ |
| 1574 | int stats_fill_sv_stats(struct proxy *px, struct server *sv, int flags, |
| 1575 | struct field *stats, int len) |
| 1576 | { |
| 1577 | struct server *via, *ref; |
| 1578 | char str[INET6_ADDRSTRLEN]; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 1579 | struct buffer *out = get_trash_chunk(); |
William Lallemand | 74c24fb | 2016-11-21 17:18:36 +0100 | [diff] [blame] | 1580 | enum srv_stats_state state; |
| 1581 | char *fld_status; |
| 1582 | |
| 1583 | if (len < ST_F_TOTAL_FIELDS) |
| 1584 | return 0; |
| 1585 | |
| 1586 | memset(stats, 0, sizeof(*stats) * len); |
| 1587 | |
| 1588 | /* we have "via" which is the tracked server as described in the configuration, |
| 1589 | * and "ref" which is the checked server and the end of the chain. |
| 1590 | */ |
| 1591 | via = sv->track ? sv->track : sv; |
| 1592 | ref = via; |
| 1593 | while (ref->track) |
| 1594 | ref = ref->track; |
| 1595 | |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 1596 | if (sv->cur_state == SRV_ST_RUNNING || sv->cur_state == SRV_ST_STARTING) { |
William Lallemand | 74c24fb | 2016-11-21 17:18:36 +0100 | [diff] [blame] | 1597 | if ((ref->check.state & CHK_ST_ENABLED) && |
| 1598 | (ref->check.health < ref->check.rise + ref->check.fall - 1)) { |
| 1599 | state = SRV_STATS_STATE_UP_GOING_DOWN; |
| 1600 | } else { |
| 1601 | state = SRV_STATS_STATE_UP; |
| 1602 | } |
| 1603 | |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 1604 | if (sv->cur_admin & SRV_ADMF_DRAIN) { |
William Lallemand | 74c24fb | 2016-11-21 17:18:36 +0100 | [diff] [blame] | 1605 | if (ref->agent.state & CHK_ST_ENABLED) |
| 1606 | state = SRV_STATS_STATE_DRAIN_AGENT; |
| 1607 | else if (state == SRV_STATS_STATE_UP_GOING_DOWN) |
| 1608 | state = SRV_STATS_STATE_DRAIN_GOING_DOWN; |
| 1609 | else |
| 1610 | state = SRV_STATS_STATE_DRAIN; |
| 1611 | } |
| 1612 | |
| 1613 | if (state == SRV_STATS_STATE_UP && !(ref->check.state & CHK_ST_ENABLED)) { |
| 1614 | state = SRV_STATS_STATE_NO_CHECK; |
| 1615 | } |
| 1616 | } |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 1617 | else if (sv->cur_state == SRV_ST_STOPPING) { |
William Lallemand | 74c24fb | 2016-11-21 17:18:36 +0100 | [diff] [blame] | 1618 | if ((!(sv->check.state & CHK_ST_ENABLED) && !sv->track) || |
| 1619 | (ref->check.health == ref->check.rise + ref->check.fall - 1)) { |
| 1620 | state = SRV_STATS_STATE_NOLB; |
| 1621 | } else { |
| 1622 | state = SRV_STATS_STATE_NOLB_GOING_DOWN; |
| 1623 | } |
| 1624 | } |
| 1625 | else { /* stopped */ |
| 1626 | if ((ref->agent.state & CHK_ST_ENABLED) && !ref->agent.health) { |
| 1627 | state = SRV_STATS_STATE_DOWN_AGENT; |
| 1628 | } else if ((ref->check.state & CHK_ST_ENABLED) && !ref->check.health) { |
| 1629 | state = SRV_STATS_STATE_DOWN; /* DOWN */ |
| 1630 | } else if ((ref->agent.state & CHK_ST_ENABLED) || (ref->check.state & CHK_ST_ENABLED)) { |
| 1631 | state = SRV_STATS_STATE_GOING_UP; |
| 1632 | } else { |
| 1633 | state = SRV_STATS_STATE_DOWN; /* DOWN, unchecked */ |
| 1634 | } |
| 1635 | } |
| 1636 | |
| 1637 | chunk_reset(out); |
| 1638 | |
| 1639 | stats[ST_F_PXNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id); |
| 1640 | stats[ST_F_SVNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, sv->id); |
| 1641 | stats[ST_F_MODE] = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode)); |
| 1642 | stats[ST_F_QCUR] = mkf_u32(0, sv->nbpend); |
| 1643 | stats[ST_F_QMAX] = mkf_u32(FN_MAX, sv->counters.nbpend_max); |
| 1644 | stats[ST_F_SCUR] = mkf_u32(0, sv->cur_sess); |
| 1645 | stats[ST_F_SMAX] = mkf_u32(FN_MAX, sv->counters.cur_sess_max); |
| 1646 | |
| 1647 | if (sv->maxconn) |
| 1648 | stats[ST_F_SLIM] = mkf_u32(FO_CONFIG|FN_LIMIT, sv->maxconn); |
| 1649 | |
| 1650 | stats[ST_F_STOT] = mkf_u64(FN_COUNTER, sv->counters.cum_sess); |
| 1651 | stats[ST_F_BIN] = mkf_u64(FN_COUNTER, sv->counters.bytes_in); |
| 1652 | stats[ST_F_BOUT] = mkf_u64(FN_COUNTER, sv->counters.bytes_out); |
| 1653 | stats[ST_F_DRESP] = mkf_u64(FN_COUNTER, sv->counters.failed_secu); |
| 1654 | stats[ST_F_ECON] = mkf_u64(FN_COUNTER, sv->counters.failed_conns); |
| 1655 | stats[ST_F_ERESP] = mkf_u64(FN_COUNTER, sv->counters.failed_resp); |
| 1656 | stats[ST_F_WRETR] = mkf_u64(FN_COUNTER, sv->counters.retries); |
| 1657 | stats[ST_F_WREDIS] = mkf_u64(FN_COUNTER, sv->counters.redispatches); |
Tim Duesterhus | 3fd1973 | 2018-05-27 20:35:08 +0200 | [diff] [blame] | 1658 | stats[ST_F_WREW] = mkf_u64(FN_COUNTER, sv->counters.failed_rewrites); |
Willy Tarreau | f157384 | 2018-12-14 11:35:36 +0100 | [diff] [blame] | 1659 | stats[ST_F_CONNECT] = mkf_u64(FN_COUNTER, sv->counters.connect); |
| 1660 | stats[ST_F_REUSE] = mkf_u64(FN_COUNTER, sv->counters.reuse); |
William Lallemand | 74c24fb | 2016-11-21 17:18:36 +0100 | [diff] [blame] | 1661 | |
| 1662 | /* status */ |
| 1663 | fld_status = chunk_newstr(out); |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 1664 | if (sv->cur_admin & SRV_ADMF_RMAINT) |
William Lallemand | 74c24fb | 2016-11-21 17:18:36 +0100 | [diff] [blame] | 1665 | chunk_appendf(out, "MAINT (resolution)"); |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 1666 | else if (sv->cur_admin & SRV_ADMF_IMAINT) |
William Lallemand | 74c24fb | 2016-11-21 17:18:36 +0100 | [diff] [blame] | 1667 | chunk_appendf(out, "MAINT (via %s/%s)", via->proxy->id, via->id); |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 1668 | else if (sv->cur_admin & SRV_ADMF_MAINT) |
William Lallemand | 74c24fb | 2016-11-21 17:18:36 +0100 | [diff] [blame] | 1669 | chunk_appendf(out, "MAINT"); |
| 1670 | else |
| 1671 | chunk_appendf(out, |
| 1672 | srv_hlt_st[state], |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 1673 | (ref->cur_state != SRV_ST_STOPPED) ? (ref->check.health - ref->check.rise + 1) : (ref->check.health), |
| 1674 | (ref->cur_state != SRV_ST_STOPPED) ? (ref->check.fall) : (ref->check.rise)); |
William Lallemand | 74c24fb | 2016-11-21 17:18:36 +0100 | [diff] [blame] | 1675 | |
| 1676 | stats[ST_F_STATUS] = mkf_str(FO_STATUS, fld_status); |
| 1677 | stats[ST_F_LASTCHG] = mkf_u32(FN_AGE, now.tv_sec - sv->last_change); |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 1678 | stats[ST_F_WEIGHT] = mkf_u32(FN_AVG, (sv->cur_eweight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv); |
William Lallemand | 74c24fb | 2016-11-21 17:18:36 +0100 | [diff] [blame] | 1679 | stats[ST_F_ACT] = mkf_u32(FO_STATUS, (sv->flags & SRV_F_BACKUP) ? 0 : 1); |
| 1680 | stats[ST_F_BCK] = mkf_u32(FO_STATUS, (sv->flags & SRV_F_BACKUP) ? 1 : 0); |
| 1681 | |
| 1682 | /* check failures: unique, fatal; last change, total downtime */ |
| 1683 | if (sv->check.state & CHK_ST_ENABLED) { |
| 1684 | stats[ST_F_CHKFAIL] = mkf_u64(FN_COUNTER, sv->counters.failed_checks); |
| 1685 | stats[ST_F_CHKDOWN] = mkf_u64(FN_COUNTER, sv->counters.down_trans); |
| 1686 | stats[ST_F_DOWNTIME] = mkf_u32(FN_COUNTER, srv_downtime(sv)); |
| 1687 | } |
| 1688 | |
| 1689 | if (sv->maxqueue) |
| 1690 | stats[ST_F_QLIMIT] = mkf_u32(FO_CONFIG|FS_SERVICE, sv->maxqueue); |
| 1691 | |
| 1692 | stats[ST_F_PID] = mkf_u32(FO_KEY, relative_pid); |
| 1693 | stats[ST_F_IID] = mkf_u32(FO_KEY|FS_SERVICE, px->uuid); |
| 1694 | stats[ST_F_SID] = mkf_u32(FO_KEY|FS_SERVICE, sv->puid); |
| 1695 | |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 1696 | if (sv->cur_state == SRV_ST_STARTING && !server_is_draining(sv)) |
William Lallemand | 74c24fb | 2016-11-21 17:18:36 +0100 | [diff] [blame] | 1697 | stats[ST_F_THROTTLE] = mkf_u32(FN_AVG, server_throttle_rate(sv)); |
| 1698 | |
| 1699 | stats[ST_F_LBTOT] = mkf_u64(FN_COUNTER, sv->counters.cum_lbconn); |
| 1700 | |
| 1701 | if (sv->track) { |
| 1702 | char *fld_track = chunk_newstr(out); |
| 1703 | |
| 1704 | chunk_appendf(out, "%s/%s", sv->track->proxy->id, sv->track->id); |
| 1705 | stats[ST_F_TRACKED] = mkf_str(FO_CONFIG|FN_NAME|FS_SERVICE, fld_track); |
| 1706 | } |
| 1707 | |
| 1708 | stats[ST_F_TYPE] = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_SV); |
| 1709 | stats[ST_F_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&sv->sess_per_sec)); |
| 1710 | stats[ST_F_RATE_MAX] = mkf_u32(FN_MAX, sv->counters.sps_max); |
| 1711 | |
| 1712 | if ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED) { |
| 1713 | const char *fld_chksts; |
| 1714 | |
| 1715 | fld_chksts = chunk_newstr(out); |
| 1716 | chunk_strcat(out, "* "); // for check in progress |
| 1717 | chunk_strcat(out, get_check_status_info(sv->check.status)); |
| 1718 | if (!(sv->check.state & CHK_ST_INPROGRESS)) |
| 1719 | fld_chksts += 2; // skip "* " |
| 1720 | stats[ST_F_CHECK_STATUS] = mkf_str(FN_OUTPUT, fld_chksts); |
| 1721 | |
| 1722 | if (sv->check.status >= HCHK_STATUS_L57DATA) |
| 1723 | stats[ST_F_CHECK_CODE] = mkf_u32(FN_OUTPUT, sv->check.code); |
| 1724 | |
| 1725 | if (sv->check.status >= HCHK_STATUS_CHECKED) |
| 1726 | stats[ST_F_CHECK_DURATION] = mkf_u64(FN_DURATION, sv->check.duration); |
| 1727 | |
| 1728 | stats[ST_F_CHECK_DESC] = mkf_str(FN_OUTPUT, get_check_status_description(sv->check.status)); |
| 1729 | stats[ST_F_LAST_CHK] = mkf_str(FN_OUTPUT, sv->check.desc); |
| 1730 | stats[ST_F_CHECK_RISE] = mkf_u32(FO_CONFIG|FS_SERVICE, ref->check.rise); |
| 1731 | stats[ST_F_CHECK_FALL] = mkf_u32(FO_CONFIG|FS_SERVICE, ref->check.fall); |
| 1732 | stats[ST_F_CHECK_HEALTH] = mkf_u32(FO_CONFIG|FS_SERVICE, ref->check.health); |
| 1733 | } |
| 1734 | |
| 1735 | if ((sv->agent.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED) { |
| 1736 | const char *fld_chksts; |
| 1737 | |
| 1738 | fld_chksts = chunk_newstr(out); |
| 1739 | chunk_strcat(out, "* "); // for check in progress |
| 1740 | chunk_strcat(out, get_check_status_info(sv->agent.status)); |
| 1741 | if (!(sv->agent.state & CHK_ST_INPROGRESS)) |
| 1742 | fld_chksts += 2; // skip "* " |
| 1743 | stats[ST_F_AGENT_STATUS] = mkf_str(FN_OUTPUT, fld_chksts); |
| 1744 | |
| 1745 | if (sv->agent.status >= HCHK_STATUS_L57DATA) |
| 1746 | stats[ST_F_AGENT_CODE] = mkf_u32(FN_OUTPUT, sv->agent.code); |
| 1747 | |
| 1748 | if (sv->agent.status >= HCHK_STATUS_CHECKED) |
| 1749 | stats[ST_F_AGENT_DURATION] = mkf_u64(FN_DURATION, sv->agent.duration); |
| 1750 | |
| 1751 | stats[ST_F_AGENT_DESC] = mkf_str(FN_OUTPUT, get_check_status_description(sv->agent.status)); |
| 1752 | stats[ST_F_LAST_AGT] = mkf_str(FN_OUTPUT, sv->agent.desc); |
| 1753 | stats[ST_F_AGENT_RISE] = mkf_u32(FO_CONFIG|FS_SERVICE, sv->agent.rise); |
| 1754 | stats[ST_F_AGENT_FALL] = mkf_u32(FO_CONFIG|FS_SERVICE, sv->agent.fall); |
| 1755 | stats[ST_F_AGENT_HEALTH] = mkf_u32(FO_CONFIG|FS_SERVICE, sv->agent.health); |
| 1756 | } |
| 1757 | |
| 1758 | /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */ |
| 1759 | if (px->mode == PR_MODE_HTTP) { |
| 1760 | stats[ST_F_HRSP_1XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[1]); |
| 1761 | stats[ST_F_HRSP_2XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[2]); |
| 1762 | stats[ST_F_HRSP_3XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[3]); |
| 1763 | stats[ST_F_HRSP_4XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[4]); |
| 1764 | stats[ST_F_HRSP_5XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[5]); |
| 1765 | stats[ST_F_HRSP_OTHER] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[0]); |
| 1766 | } |
| 1767 | |
| 1768 | if (ref->observe) |
| 1769 | stats[ST_F_HANAFAIL] = mkf_u64(FN_COUNTER, sv->counters.failed_hana); |
| 1770 | |
| 1771 | stats[ST_F_CLI_ABRT] = mkf_u64(FN_COUNTER, sv->counters.cli_aborts); |
| 1772 | stats[ST_F_SRV_ABRT] = mkf_u64(FN_COUNTER, sv->counters.srv_aborts); |
| 1773 | stats[ST_F_LASTSESS] = mkf_s32(FN_AGE, srv_lastsession(sv)); |
| 1774 | |
| 1775 | stats[ST_F_QTIME] = mkf_u32(FN_AVG, swrate_avg(sv->counters.q_time, TIME_STATS_SAMPLES)); |
| 1776 | stats[ST_F_CTIME] = mkf_u32(FN_AVG, swrate_avg(sv->counters.c_time, TIME_STATS_SAMPLES)); |
| 1777 | stats[ST_F_RTIME] = mkf_u32(FN_AVG, swrate_avg(sv->counters.d_time, TIME_STATS_SAMPLES)); |
| 1778 | stats[ST_F_TTIME] = mkf_u32(FN_AVG, swrate_avg(sv->counters.t_time, TIME_STATS_SAMPLES)); |
| 1779 | |
| 1780 | if (flags & ST_SHLGNDS) { |
| 1781 | switch (addr_to_str(&sv->addr, str, sizeof(str))) { |
| 1782 | case AF_INET: |
| 1783 | stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out)); |
Willy Tarreau | 04276f3 | 2017-01-06 17:41:29 +0100 | [diff] [blame] | 1784 | chunk_appendf(out, "%s:%d", str, sv->svc_port); |
William Lallemand | 74c24fb | 2016-11-21 17:18:36 +0100 | [diff] [blame] | 1785 | break; |
| 1786 | case AF_INET6: |
| 1787 | stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out)); |
Willy Tarreau | 04276f3 | 2017-01-06 17:41:29 +0100 | [diff] [blame] | 1788 | chunk_appendf(out, "[%s]:%d", str, sv->svc_port); |
William Lallemand | 74c24fb | 2016-11-21 17:18:36 +0100 | [diff] [blame] | 1789 | break; |
| 1790 | case AF_UNIX: |
| 1791 | stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, "unix"); |
| 1792 | break; |
| 1793 | case -1: |
| 1794 | stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out)); |
| 1795 | chunk_strcat(out, strerror(errno)); |
| 1796 | break; |
| 1797 | default: /* address family not supported */ |
| 1798 | break; |
| 1799 | } |
| 1800 | |
| 1801 | if (sv->cookie) |
| 1802 | stats[ST_F_COOKIE] = mkf_str(FO_CONFIG|FN_NAME|FS_SERVICE, sv->cookie); |
| 1803 | } |
| 1804 | |
| 1805 | return 1; |
| 1806 | } |
| 1807 | |
| 1808 | /* Dumps a line for server <sv> and proxy <px> to the trash and uses the state |
| 1809 | * from stream interface <si>, stats flags <flags>, and server state <state>. |
| 1810 | * The caller is responsible for clearing the trash if needed. Returns non-zero |
| 1811 | * if it emits anything, zero otherwise. |
| 1812 | */ |
| 1813 | static int stats_dump_sv_stats(struct stream_interface *si, struct proxy *px, int flags, struct server *sv) |
| 1814 | { |
| 1815 | struct appctx *appctx = __objt_appctx(si->end); |
| 1816 | |
| 1817 | if (!stats_fill_sv_stats(px, sv, flags, stats, ST_F_TOTAL_FIELDS)) |
| 1818 | return 0; |
| 1819 | |
| 1820 | return stats_dump_one_line(stats, flags, px, appctx); |
| 1821 | } |
| 1822 | |
| 1823 | /* Fill <stats> with the backend statistics. <stats> is |
| 1824 | * preallocated array of length <len>. The length of the array |
| 1825 | * must be at least ST_F_TOTAL_FIELDS. If this length is less |
| 1826 | * then this value, the function returns 0, otherwise, it |
| 1827 | * returns 1. <flags> can take the value ST_SHLGNDS. |
| 1828 | */ |
| 1829 | int stats_fill_be_stats(struct proxy *px, int flags, struct field *stats, int len) |
| 1830 | { |
| 1831 | if (len < ST_F_TOTAL_FIELDS) |
| 1832 | return 0; |
| 1833 | |
| 1834 | memset(stats, 0, sizeof(*stats) * len); |
| 1835 | |
| 1836 | stats[ST_F_PXNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id); |
| 1837 | stats[ST_F_SVNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, "BACKEND"); |
| 1838 | stats[ST_F_MODE] = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode)); |
| 1839 | stats[ST_F_QCUR] = mkf_u32(0, px->nbpend); |
| 1840 | stats[ST_F_QMAX] = mkf_u32(FN_MAX, px->be_counters.nbpend_max); |
Thierry FOURNIER | 0ff98a4 | 2016-12-19 16:50:42 +0100 | [diff] [blame] | 1841 | stats[ST_F_SCUR] = mkf_u32(0, px->beconn); |
William Lallemand | 74c24fb | 2016-11-21 17:18:36 +0100 | [diff] [blame] | 1842 | stats[ST_F_SMAX] = mkf_u32(FN_MAX, px->be_counters.conn_max); |
| 1843 | stats[ST_F_SLIM] = mkf_u32(FO_CONFIG|FN_LIMIT, px->fullconn); |
| 1844 | stats[ST_F_STOT] = mkf_u64(FN_COUNTER, px->be_counters.cum_conn); |
| 1845 | stats[ST_F_BIN] = mkf_u64(FN_COUNTER, px->be_counters.bytes_in); |
| 1846 | stats[ST_F_BOUT] = mkf_u64(FN_COUNTER, px->be_counters.bytes_out); |
| 1847 | stats[ST_F_DREQ] = mkf_u64(FN_COUNTER, px->be_counters.denied_req); |
| 1848 | stats[ST_F_DRESP] = mkf_u64(FN_COUNTER, px->be_counters.denied_resp); |
| 1849 | stats[ST_F_ECON] = mkf_u64(FN_COUNTER, px->be_counters.failed_conns); |
| 1850 | stats[ST_F_ERESP] = mkf_u64(FN_COUNTER, px->be_counters.failed_resp); |
| 1851 | stats[ST_F_WRETR] = mkf_u64(FN_COUNTER, px->be_counters.retries); |
| 1852 | stats[ST_F_WREDIS] = mkf_u64(FN_COUNTER, px->be_counters.redispatches); |
Tim Duesterhus | 3fd1973 | 2018-05-27 20:35:08 +0200 | [diff] [blame] | 1853 | stats[ST_F_WREW] = mkf_u64(FN_COUNTER, px->be_counters.failed_rewrites); |
Willy Tarreau | f157384 | 2018-12-14 11:35:36 +0100 | [diff] [blame] | 1854 | stats[ST_F_CONNECT] = mkf_u64(FN_COUNTER, px->be_counters.connect); |
| 1855 | stats[ST_F_REUSE] = mkf_u64(FN_COUNTER, px->be_counters.reuse); |
William Lallemand | 74c24fb | 2016-11-21 17:18:36 +0100 | [diff] [blame] | 1856 | stats[ST_F_STATUS] = mkf_str(FO_STATUS, (px->lbprm.tot_weight > 0 || !px->srv) ? "UP" : "DOWN"); |
| 1857 | stats[ST_F_WEIGHT] = mkf_u32(FN_AVG, (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv); |
| 1858 | stats[ST_F_ACT] = mkf_u32(0, px->srv_act); |
| 1859 | stats[ST_F_BCK] = mkf_u32(0, px->srv_bck); |
| 1860 | stats[ST_F_CHKDOWN] = mkf_u64(FN_COUNTER, px->down_trans); |
| 1861 | stats[ST_F_LASTCHG] = mkf_u32(FN_AGE, now.tv_sec - px->last_change); |
| 1862 | if (px->srv) |
| 1863 | stats[ST_F_DOWNTIME] = mkf_u32(FN_COUNTER, be_downtime(px)); |
| 1864 | |
| 1865 | stats[ST_F_PID] = mkf_u32(FO_KEY, relative_pid); |
| 1866 | stats[ST_F_IID] = mkf_u32(FO_KEY|FS_SERVICE, px->uuid); |
| 1867 | stats[ST_F_SID] = mkf_u32(FO_KEY|FS_SERVICE, 0); |
| 1868 | stats[ST_F_LBTOT] = mkf_u64(FN_COUNTER, px->be_counters.cum_lbconn); |
| 1869 | stats[ST_F_TYPE] = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_BE); |
| 1870 | stats[ST_F_RATE] = mkf_u32(0, read_freq_ctr(&px->be_sess_per_sec)); |
| 1871 | stats[ST_F_RATE_MAX] = mkf_u32(0, px->be_counters.sps_max); |
| 1872 | |
| 1873 | if (flags & ST_SHLGNDS) { |
| 1874 | if (px->cookie_name) |
| 1875 | stats[ST_F_COOKIE] = mkf_str(FO_CONFIG|FN_NAME|FS_SERVICE, px->cookie_name); |
| 1876 | stats[ST_F_ALGO] = mkf_str(FO_CONFIG|FS_SERVICE, backend_lb_algo_str(px->lbprm.algo & BE_LB_ALGO)); |
| 1877 | } |
| 1878 | |
| 1879 | /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */ |
| 1880 | if (px->mode == PR_MODE_HTTP) { |
| 1881 | stats[ST_F_REQ_TOT] = mkf_u64(FN_COUNTER, px->be_counters.p.http.cum_req); |
| 1882 | stats[ST_F_HRSP_1XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[1]); |
| 1883 | stats[ST_F_HRSP_2XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[2]); |
| 1884 | stats[ST_F_HRSP_3XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[3]); |
| 1885 | stats[ST_F_HRSP_4XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[4]); |
| 1886 | stats[ST_F_HRSP_5XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[5]); |
| 1887 | stats[ST_F_HRSP_OTHER] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[0]); |
Willy Tarreau | a1214a5 | 2018-12-14 14:00:25 +0100 | [diff] [blame] | 1888 | stats[ST_F_CACHE_LOOKUPS] = mkf_u64(FN_COUNTER, px->be_counters.p.http.cache_lookups); |
| 1889 | stats[ST_F_CACHE_HITS] = mkf_u64(FN_COUNTER, px->be_counters.p.http.cache_hits); |
William Lallemand | 74c24fb | 2016-11-21 17:18:36 +0100 | [diff] [blame] | 1890 | } |
| 1891 | |
| 1892 | stats[ST_F_CLI_ABRT] = mkf_u64(FN_COUNTER, px->be_counters.cli_aborts); |
| 1893 | stats[ST_F_SRV_ABRT] = mkf_u64(FN_COUNTER, px->be_counters.srv_aborts); |
| 1894 | |
| 1895 | /* compression: in, out, bypassed, responses */ |
| 1896 | stats[ST_F_COMP_IN] = mkf_u64(FN_COUNTER, px->be_counters.comp_in); |
| 1897 | stats[ST_F_COMP_OUT] = mkf_u64(FN_COUNTER, px->be_counters.comp_out); |
| 1898 | stats[ST_F_COMP_BYP] = mkf_u64(FN_COUNTER, px->be_counters.comp_byp); |
| 1899 | stats[ST_F_COMP_RSP] = mkf_u64(FN_COUNTER, px->be_counters.p.http.comp_rsp); |
| 1900 | stats[ST_F_LASTSESS] = mkf_s32(FN_AGE, be_lastsession(px)); |
| 1901 | |
| 1902 | stats[ST_F_QTIME] = mkf_u32(FN_AVG, swrate_avg(px->be_counters.q_time, TIME_STATS_SAMPLES)); |
| 1903 | stats[ST_F_CTIME] = mkf_u32(FN_AVG, swrate_avg(px->be_counters.c_time, TIME_STATS_SAMPLES)); |
| 1904 | stats[ST_F_RTIME] = mkf_u32(FN_AVG, swrate_avg(px->be_counters.d_time, TIME_STATS_SAMPLES)); |
| 1905 | stats[ST_F_TTIME] = mkf_u32(FN_AVG, swrate_avg(px->be_counters.t_time, TIME_STATS_SAMPLES)); |
| 1906 | |
| 1907 | return 1; |
| 1908 | } |
| 1909 | |
| 1910 | /* Dumps a line for backend <px> to the trash for and uses the state from stream |
| 1911 | * interface <si> and stats flags <flags>. The caller is responsible for clearing |
| 1912 | * the trash if needed. Returns non-zero if it emits anything, zero otherwise. |
| 1913 | */ |
| 1914 | static int stats_dump_be_stats(struct stream_interface *si, struct proxy *px, int flags) |
| 1915 | { |
| 1916 | struct appctx *appctx = __objt_appctx(si->end); |
| 1917 | |
| 1918 | if (!(px->cap & PR_CAP_BE)) |
| 1919 | return 0; |
| 1920 | |
| 1921 | if ((appctx->ctx.stats.flags & STAT_BOUND) && !(appctx->ctx.stats.type & (1 << STATS_TYPE_BE))) |
| 1922 | return 0; |
| 1923 | |
| 1924 | if (!stats_fill_be_stats(px, flags, stats, ST_F_TOTAL_FIELDS)) |
| 1925 | return 0; |
| 1926 | |
| 1927 | return stats_dump_one_line(stats, flags, px, appctx); |
| 1928 | } |
| 1929 | |
| 1930 | /* Dumps the HTML table header for proxy <px> to the trash for and uses the state from |
| 1931 | * stream interface <si> and per-uri parameters <uri>. The caller is responsible |
| 1932 | * for clearing the trash if needed. |
| 1933 | */ |
| 1934 | static void stats_dump_html_px_hdr(struct stream_interface *si, struct proxy *px, struct uri_auth *uri) |
| 1935 | { |
| 1936 | struct appctx *appctx = __objt_appctx(si->end); |
| 1937 | char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN]; |
| 1938 | |
| 1939 | if (px->cap & PR_CAP_BE && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) { |
| 1940 | /* A form to enable/disable this proxy servers */ |
| 1941 | |
| 1942 | /* scope_txt = search pattern + search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */ |
| 1943 | scope_txt[0] = 0; |
| 1944 | if (appctx->ctx.stats.scope_len) { |
Christopher Faulet | ed7a066 | 2019-01-14 11:07:34 +0100 | [diff] [blame] | 1945 | const char *scope_ptr = stats_scope_ptr(appctx, si); |
| 1946 | |
William Lallemand | 74c24fb | 2016-11-21 17:18:36 +0100 | [diff] [blame] | 1947 | strcpy(scope_txt, STAT_SCOPE_PATTERN); |
Christopher Faulet | ed7a066 | 2019-01-14 11:07:34 +0100 | [diff] [blame] | 1948 | memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), scope_ptr, appctx->ctx.stats.scope_len); |
William Lallemand | 74c24fb | 2016-11-21 17:18:36 +0100 | [diff] [blame] | 1949 | scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0; |
| 1950 | } |
| 1951 | |
| 1952 | chunk_appendf(&trash, |
| 1953 | "<form method=\"post\">"); |
| 1954 | } |
| 1955 | |
| 1956 | /* print a new table */ |
| 1957 | chunk_appendf(&trash, |
| 1958 | "<table class=\"tbl\" width=\"100%%\">\n" |
| 1959 | "<tr class=\"titre\">" |
| 1960 | "<th class=\"pxname\" width=\"10%%\">"); |
| 1961 | |
| 1962 | chunk_appendf(&trash, |
| 1963 | "<a name=\"%s\"></a>%s" |
| 1964 | "<a class=px href=\"#%s\">%s</a>", |
| 1965 | px->id, |
| 1966 | (uri->flags & ST_SHLGNDS) ? "<u>":"", |
| 1967 | px->id, px->id); |
| 1968 | |
| 1969 | if (uri->flags & ST_SHLGNDS) { |
| 1970 | /* cap, mode, id */ |
| 1971 | chunk_appendf(&trash, "<div class=tips>cap: %s, mode: %s, id: %d", |
| 1972 | proxy_cap_str(px->cap), proxy_mode_str(px->mode), |
| 1973 | px->uuid); |
| 1974 | chunk_appendf(&trash, "</div>"); |
| 1975 | } |
| 1976 | |
| 1977 | chunk_appendf(&trash, |
| 1978 | "%s</th>" |
| 1979 | "<th class=\"%s\" width=\"90%%\">%s</th>" |
| 1980 | "</tr>\n" |
| 1981 | "</table>\n" |
| 1982 | "<table class=\"tbl\" width=\"100%%\">\n" |
| 1983 | "<tr class=\"titre\">", |
| 1984 | (uri->flags & ST_SHLGNDS) ? "</u>":"", |
| 1985 | px->desc ? "desc" : "empty", px->desc ? px->desc : ""); |
| 1986 | |
| 1987 | if ((px->cap & PR_CAP_BE) && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) { |
| 1988 | /* Column heading for Enable or Disable server */ |
David Harrigan | d3db35a | 2016-12-30 12:12:49 +0000 | [diff] [blame] | 1989 | chunk_appendf(&trash, |
| 1990 | "<th rowspan=2 width=1><input type=\"checkbox\" \ |
| 1991 | onclick=\"for(c in document.getElementsByClassName('%s-checkbox')) \ |
| 1992 | document.getElementsByClassName('%s-checkbox').item(c).checked = this.checked\"></th>", |
| 1993 | px->id, |
| 1994 | px->id); |
William Lallemand | 74c24fb | 2016-11-21 17:18:36 +0100 | [diff] [blame] | 1995 | } |
| 1996 | |
| 1997 | chunk_appendf(&trash, |
| 1998 | "<th rowspan=2></th>" |
| 1999 | "<th colspan=3>Queue</th>" |
| 2000 | "<th colspan=3>Session rate</th><th colspan=6>Sessions</th>" |
| 2001 | "<th colspan=2>Bytes</th><th colspan=2>Denied</th>" |
| 2002 | "<th colspan=3>Errors</th><th colspan=2>Warnings</th>" |
| 2003 | "<th colspan=9>Server</th>" |
| 2004 | "</tr>\n" |
| 2005 | "<tr class=\"titre\">" |
| 2006 | "<th>Cur</th><th>Max</th><th>Limit</th>" |
| 2007 | "<th>Cur</th><th>Max</th><th>Limit</th><th>Cur</th><th>Max</th>" |
| 2008 | "<th>Limit</th><th>Total</th><th>LbTot</th><th>Last</th><th>In</th><th>Out</th>" |
| 2009 | "<th>Req</th><th>Resp</th><th>Req</th><th>Conn</th>" |
| 2010 | "<th>Resp</th><th>Retr</th><th>Redis</th>" |
| 2011 | "<th>Status</th><th>LastChk</th><th>Wght</th><th>Act</th>" |
| 2012 | "<th>Bck</th><th>Chk</th><th>Dwn</th><th>Dwntme</th>" |
| 2013 | "<th>Thrtle</th>\n" |
| 2014 | "</tr>"); |
| 2015 | } |
| 2016 | |
| 2017 | /* Dumps the HTML table trailer for proxy <px> to the trash for and uses the state from |
| 2018 | * stream interface <si>. The caller is responsible for clearing the trash if needed. |
| 2019 | */ |
| 2020 | static void stats_dump_html_px_end(struct stream_interface *si, struct proxy *px) |
| 2021 | { |
| 2022 | struct appctx *appctx = __objt_appctx(si->end); |
| 2023 | chunk_appendf(&trash, "</table>"); |
| 2024 | |
| 2025 | if ((px->cap & PR_CAP_BE) && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) { |
| 2026 | /* close the form used to enable/disable this proxy servers */ |
| 2027 | chunk_appendf(&trash, |
| 2028 | "Choose the action to perform on the checked servers : " |
| 2029 | "<select name=action>" |
| 2030 | "<option value=\"\"></option>" |
| 2031 | "<option value=\"ready\">Set state to READY</option>" |
| 2032 | "<option value=\"drain\">Set state to DRAIN</option>" |
| 2033 | "<option value=\"maint\">Set state to MAINT</option>" |
| 2034 | "<option value=\"dhlth\">Health: disable checks</option>" |
| 2035 | "<option value=\"ehlth\">Health: enable checks</option>" |
| 2036 | "<option value=\"hrunn\">Health: force UP</option>" |
| 2037 | "<option value=\"hnolb\">Health: force NOLB</option>" |
| 2038 | "<option value=\"hdown\">Health: force DOWN</option>" |
| 2039 | "<option value=\"dagent\">Agent: disable checks</option>" |
| 2040 | "<option value=\"eagent\">Agent: enable checks</option>" |
| 2041 | "<option value=\"arunn\">Agent: force UP</option>" |
| 2042 | "<option value=\"adown\">Agent: force DOWN</option>" |
| 2043 | "<option value=\"shutdown\">Kill Sessions</option>" |
| 2044 | "</select>" |
| 2045 | "<input type=\"hidden\" name=\"b\" value=\"#%d\">" |
| 2046 | " <input type=\"submit\" value=\"Apply\">" |
| 2047 | "</form>", |
| 2048 | px->uuid); |
| 2049 | } |
| 2050 | |
| 2051 | chunk_appendf(&trash, "<p>\n"); |
| 2052 | } |
| 2053 | |
| 2054 | /* |
| 2055 | * Dumps statistics for a proxy. The output is sent to the stream interface's |
| 2056 | * input buffer. Returns 0 if it had to stop dumping data because of lack of |
| 2057 | * buffer space, or non-zero if everything completed. This function is used |
| 2058 | * both by the CLI and the HTTP entry points, and is able to dump the output |
| 2059 | * in HTML or CSV formats. If the later, <uri> must be NULL. |
| 2060 | */ |
Christopher Faulet | ef77922 | 2018-10-31 08:47:01 +0100 | [diff] [blame] | 2061 | int stats_dump_proxy_to_buffer(struct stream_interface *si, struct htx *htx, |
| 2062 | struct proxy *px, struct uri_auth *uri) |
William Lallemand | 74c24fb | 2016-11-21 17:18:36 +0100 | [diff] [blame] | 2063 | { |
| 2064 | struct appctx *appctx = __objt_appctx(si->end); |
| 2065 | struct stream *s = si_strm(si); |
| 2066 | struct channel *rep = si_ic(si); |
| 2067 | struct server *sv, *svs; /* server and server-state, server-state=server or server->track */ |
| 2068 | struct listener *l; |
| 2069 | unsigned int flags; |
| 2070 | |
| 2071 | if (uri) |
| 2072 | flags = uri->flags; |
William Lallemand | 07a62f7 | 2017-05-24 00:57:40 +0200 | [diff] [blame] | 2073 | else if ((strm_li(s)->bind_conf->level & ACCESS_LVL_MASK) >= ACCESS_LVL_OPER) |
William Lallemand | 74c24fb | 2016-11-21 17:18:36 +0100 | [diff] [blame] | 2074 | flags = ST_SHLGNDS | ST_SHNODE | ST_SHDESC; |
| 2075 | else |
| 2076 | flags = ST_SHNODE | ST_SHDESC; |
| 2077 | |
| 2078 | chunk_reset(&trash); |
| 2079 | |
| 2080 | switch (appctx->ctx.stats.px_st) { |
| 2081 | case STAT_PX_ST_INIT: |
| 2082 | /* we are on a new proxy */ |
| 2083 | if (uri && uri->scope) { |
| 2084 | /* we have a limited scope, we have to check the proxy name */ |
| 2085 | struct stat_scope *scope; |
| 2086 | int len; |
| 2087 | |
| 2088 | len = strlen(px->id); |
| 2089 | scope = uri->scope; |
| 2090 | |
| 2091 | while (scope) { |
| 2092 | /* match exact proxy name */ |
| 2093 | if (scope->px_len == len && !memcmp(px->id, scope->px_id, len)) |
| 2094 | break; |
| 2095 | |
| 2096 | /* match '.' which means 'self' proxy */ |
| 2097 | if (!strcmp(scope->px_id, ".") && px == s->be) |
| 2098 | break; |
| 2099 | scope = scope->next; |
| 2100 | } |
| 2101 | |
| 2102 | /* proxy name not found : don't dump anything */ |
| 2103 | if (scope == NULL) |
| 2104 | return 1; |
| 2105 | } |
| 2106 | |
| 2107 | /* if the user has requested a limited output and the proxy |
| 2108 | * name does not match, skip it. |
| 2109 | */ |
Christopher Faulet | ed7a066 | 2019-01-14 11:07:34 +0100 | [diff] [blame] | 2110 | if (appctx->ctx.stats.scope_len) { |
| 2111 | const char *scope_ptr = stats_scope_ptr(appctx, si); |
| 2112 | |
| 2113 | if (strnistr(px->id, strlen(px->id), scope_ptr, appctx->ctx.stats.scope_len) == NULL) |
| 2114 | return 1; |
| 2115 | } |
William Lallemand | 74c24fb | 2016-11-21 17:18:36 +0100 | [diff] [blame] | 2116 | |
| 2117 | if ((appctx->ctx.stats.flags & STAT_BOUND) && |
| 2118 | (appctx->ctx.stats.iid != -1) && |
| 2119 | (px->uuid != appctx->ctx.stats.iid)) |
| 2120 | return 1; |
| 2121 | |
| 2122 | appctx->ctx.stats.px_st = STAT_PX_ST_TH; |
| 2123 | /* fall through */ |
| 2124 | |
| 2125 | case STAT_PX_ST_TH: |
| 2126 | if (appctx->ctx.stats.flags & STAT_FMT_HTML) { |
| 2127 | stats_dump_html_px_hdr(si, px, uri); |
Christopher Faulet | ef77922 | 2018-10-31 08:47:01 +0100 | [diff] [blame] | 2128 | if (!stats_putchk(rep, htx, &trash)) |
| 2129 | goto full; |
William Lallemand | 74c24fb | 2016-11-21 17:18:36 +0100 | [diff] [blame] | 2130 | } |
| 2131 | |
| 2132 | appctx->ctx.stats.px_st = STAT_PX_ST_FE; |
| 2133 | /* fall through */ |
| 2134 | |
| 2135 | case STAT_PX_ST_FE: |
| 2136 | /* print the frontend */ |
| 2137 | if (stats_dump_fe_stats(si, px)) { |
Christopher Faulet | ef77922 | 2018-10-31 08:47:01 +0100 | [diff] [blame] | 2138 | if (!stats_putchk(rep, htx, &trash)) |
| 2139 | goto full; |
William Lallemand | 74c24fb | 2016-11-21 17:18:36 +0100 | [diff] [blame] | 2140 | } |
| 2141 | |
| 2142 | appctx->ctx.stats.l = px->conf.listeners.n; |
| 2143 | appctx->ctx.stats.px_st = STAT_PX_ST_LI; |
| 2144 | /* fall through */ |
| 2145 | |
| 2146 | case STAT_PX_ST_LI: |
| 2147 | /* stats.l has been initialized above */ |
| 2148 | for (; appctx->ctx.stats.l != &px->conf.listeners; appctx->ctx.stats.l = l->by_fe.n) { |
Christopher Faulet | ef77922 | 2018-10-31 08:47:01 +0100 | [diff] [blame] | 2149 | if (htx) { |
| 2150 | if (htx_almost_full(htx)) |
| 2151 | goto full; |
| 2152 | } |
| 2153 | else { |
| 2154 | if (buffer_almost_full(&rep->buf)) |
| 2155 | goto full; |
William Lallemand | 74c24fb | 2016-11-21 17:18:36 +0100 | [diff] [blame] | 2156 | } |
| 2157 | |
| 2158 | l = LIST_ELEM(appctx->ctx.stats.l, struct listener *, by_fe); |
| 2159 | if (!l->counters) |
| 2160 | continue; |
| 2161 | |
| 2162 | if (appctx->ctx.stats.flags & STAT_BOUND) { |
| 2163 | if (!(appctx->ctx.stats.type & (1 << STATS_TYPE_SO))) |
| 2164 | break; |
| 2165 | |
| 2166 | if (appctx->ctx.stats.sid != -1 && l->luid != appctx->ctx.stats.sid) |
| 2167 | continue; |
| 2168 | } |
| 2169 | |
| 2170 | /* print the frontend */ |
| 2171 | if (stats_dump_li_stats(si, px, l, flags)) { |
Christopher Faulet | ef77922 | 2018-10-31 08:47:01 +0100 | [diff] [blame] | 2172 | if (!stats_putchk(rep, htx, &trash)) |
| 2173 | goto full; |
William Lallemand | 74c24fb | 2016-11-21 17:18:36 +0100 | [diff] [blame] | 2174 | } |
| 2175 | } |
| 2176 | |
| 2177 | appctx->ctx.stats.sv = px->srv; /* may be NULL */ |
| 2178 | appctx->ctx.stats.px_st = STAT_PX_ST_SV; |
| 2179 | /* fall through */ |
| 2180 | |
| 2181 | case STAT_PX_ST_SV: |
| 2182 | /* stats.sv has been initialized above */ |
| 2183 | for (; appctx->ctx.stats.sv != NULL; appctx->ctx.stats.sv = sv->next) { |
Christopher Faulet | ef77922 | 2018-10-31 08:47:01 +0100 | [diff] [blame] | 2184 | if (htx) { |
| 2185 | if (htx_almost_full(htx)) |
| 2186 | goto full; |
| 2187 | } |
| 2188 | else { |
| 2189 | if (buffer_almost_full(&rep->buf)) |
| 2190 | goto full; |
William Lallemand | 74c24fb | 2016-11-21 17:18:36 +0100 | [diff] [blame] | 2191 | } |
| 2192 | |
| 2193 | sv = appctx->ctx.stats.sv; |
| 2194 | |
| 2195 | if (appctx->ctx.stats.flags & STAT_BOUND) { |
| 2196 | if (!(appctx->ctx.stats.type & (1 << STATS_TYPE_SV))) |
| 2197 | break; |
| 2198 | |
| 2199 | if (appctx->ctx.stats.sid != -1 && sv->puid != appctx->ctx.stats.sid) |
| 2200 | continue; |
| 2201 | } |
| 2202 | |
| 2203 | svs = sv; |
| 2204 | while (svs->track) |
| 2205 | svs = svs->track; |
| 2206 | |
| 2207 | /* do not report servers which are DOWN and not changing state */ |
| 2208 | if ((appctx->ctx.stats.flags & STAT_HIDE_DOWN) && |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 2209 | ((sv->cur_admin & SRV_ADMF_MAINT) || /* server is in maintenance */ |
| 2210 | (sv->cur_state == SRV_ST_STOPPED && /* server is down */ |
William Lallemand | 74c24fb | 2016-11-21 17:18:36 +0100 | [diff] [blame] | 2211 | (!((svs->agent.state | svs->check.state) & CHK_ST_ENABLED) || |
| 2212 | ((svs->agent.state & CHK_ST_ENABLED) && !svs->agent.health) || |
| 2213 | ((svs->check.state & CHK_ST_ENABLED) && !svs->check.health))))) { |
| 2214 | continue; |
| 2215 | } |
| 2216 | |
| 2217 | if (stats_dump_sv_stats(si, px, flags, sv)) { |
Christopher Faulet | ef77922 | 2018-10-31 08:47:01 +0100 | [diff] [blame] | 2218 | if (!stats_putchk(rep, htx, &trash)) |
| 2219 | goto full; |
William Lallemand | 74c24fb | 2016-11-21 17:18:36 +0100 | [diff] [blame] | 2220 | } |
| 2221 | } /* for sv */ |
| 2222 | |
| 2223 | appctx->ctx.stats.px_st = STAT_PX_ST_BE; |
| 2224 | /* fall through */ |
| 2225 | |
| 2226 | case STAT_PX_ST_BE: |
| 2227 | /* print the backend */ |
| 2228 | if (stats_dump_be_stats(si, px, flags)) { |
Christopher Faulet | ef77922 | 2018-10-31 08:47:01 +0100 | [diff] [blame] | 2229 | if (!stats_putchk(rep, htx, &trash)) |
| 2230 | goto full; |
William Lallemand | 74c24fb | 2016-11-21 17:18:36 +0100 | [diff] [blame] | 2231 | } |
| 2232 | |
| 2233 | appctx->ctx.stats.px_st = STAT_PX_ST_END; |
| 2234 | /* fall through */ |
| 2235 | |
| 2236 | case STAT_PX_ST_END: |
| 2237 | if (appctx->ctx.stats.flags & STAT_FMT_HTML) { |
| 2238 | stats_dump_html_px_end(si, px); |
Christopher Faulet | ef77922 | 2018-10-31 08:47:01 +0100 | [diff] [blame] | 2239 | if (!stats_putchk(rep, htx, &trash)) |
| 2240 | goto full; |
William Lallemand | 74c24fb | 2016-11-21 17:18:36 +0100 | [diff] [blame] | 2241 | } |
| 2242 | |
| 2243 | appctx->ctx.stats.px_st = STAT_PX_ST_FIN; |
| 2244 | /* fall through */ |
| 2245 | |
| 2246 | case STAT_PX_ST_FIN: |
| 2247 | return 1; |
| 2248 | |
| 2249 | default: |
| 2250 | /* unknown state, we should put an abort() here ! */ |
| 2251 | return 1; |
| 2252 | } |
Christopher Faulet | ef77922 | 2018-10-31 08:47:01 +0100 | [diff] [blame] | 2253 | |
| 2254 | full: |
| 2255 | si_rx_room_blk(si); |
| 2256 | return 0; |
William Lallemand | 74c24fb | 2016-11-21 17:18:36 +0100 | [diff] [blame] | 2257 | } |
| 2258 | |
| 2259 | /* Dumps the HTTP stats head block to the trash for and uses the per-uri |
| 2260 | * parameters <uri>. The caller is responsible for clearing the trash if needed. |
| 2261 | */ |
| 2262 | static void stats_dump_html_head(struct uri_auth *uri) |
| 2263 | { |
| 2264 | /* WARNING! This must fit in the first buffer !!! */ |
| 2265 | chunk_appendf(&trash, |
| 2266 | "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"\n" |
| 2267 | "\"http://www.w3.org/TR/html4/loose.dtd\">\n" |
| 2268 | "<html><head><title>Statistics Report for " PRODUCT_NAME "%s%s</title>\n" |
| 2269 | "<meta http-equiv=\"content-type\" content=\"text/html; charset=iso-8859-1\">\n" |
| 2270 | "<style type=\"text/css\"><!--\n" |
| 2271 | "body {" |
| 2272 | " font-family: arial, helvetica, sans-serif;" |
| 2273 | " font-size: 12px;" |
| 2274 | " font-weight: normal;" |
| 2275 | " color: black;" |
| 2276 | " background: white;" |
| 2277 | "}\n" |
| 2278 | "th,td {" |
| 2279 | " font-size: 10px;" |
| 2280 | "}\n" |
| 2281 | "h1 {" |
| 2282 | " font-size: x-large;" |
| 2283 | " margin-bottom: 0.5em;" |
| 2284 | "}\n" |
| 2285 | "h2 {" |
| 2286 | " font-family: helvetica, arial;" |
| 2287 | " font-size: x-large;" |
| 2288 | " font-weight: bold;" |
| 2289 | " font-style: italic;" |
| 2290 | " color: #6020a0;" |
| 2291 | " margin-top: 0em;" |
| 2292 | " margin-bottom: 0em;" |
| 2293 | "}\n" |
| 2294 | "h3 {" |
| 2295 | " font-family: helvetica, arial;" |
| 2296 | " font-size: 16px;" |
| 2297 | " font-weight: bold;" |
| 2298 | " color: #b00040;" |
| 2299 | " background: #e8e8d0;" |
| 2300 | " margin-top: 0em;" |
| 2301 | " margin-bottom: 0em;" |
| 2302 | "}\n" |
| 2303 | "li {" |
| 2304 | " margin-top: 0.25em;" |
| 2305 | " margin-right: 2em;" |
| 2306 | "}\n" |
| 2307 | ".hr {margin-top: 0.25em;" |
| 2308 | " border-color: black;" |
| 2309 | " border-bottom-style: solid;" |
| 2310 | "}\n" |
| 2311 | ".titre {background: #20D0D0;color: #000000; font-weight: bold; text-align: center;}\n" |
| 2312 | ".total {background: #20D0D0;color: #ffff80;}\n" |
| 2313 | ".frontend {background: #e8e8d0;}\n" |
| 2314 | ".socket {background: #d0d0d0;}\n" |
| 2315 | ".backend {background: #e8e8d0;}\n" |
| 2316 | ".active_down {background: #ff9090;}\n" |
| 2317 | ".active_going_up {background: #ffd020;}\n" |
| 2318 | ".active_going_down {background: #ffffa0;}\n" |
| 2319 | ".active_up {background: #c0ffc0;}\n" |
| 2320 | ".active_nolb {background: #20a0ff;}\n" |
| 2321 | ".active_draining {background: #20a0FF;}\n" |
| 2322 | ".active_no_check {background: #e0e0e0;}\n" |
| 2323 | ".backup_down {background: #ff9090;}\n" |
| 2324 | ".backup_going_up {background: #ff80ff;}\n" |
| 2325 | ".backup_going_down {background: #c060ff;}\n" |
| 2326 | ".backup_up {background: #b0d0ff;}\n" |
| 2327 | ".backup_nolb {background: #90b0e0;}\n" |
| 2328 | ".backup_draining {background: #cc9900;}\n" |
| 2329 | ".backup_no_check {background: #e0e0e0;}\n" |
| 2330 | ".maintain {background: #c07820;}\n" |
| 2331 | ".rls {letter-spacing: 0.2em; margin-right: 1px;}\n" /* right letter spacing (used for grouping digits) */ |
| 2332 | "\n" |
| 2333 | "a.px:link {color: #ffff40; text-decoration: none;}" |
| 2334 | "a.px:visited {color: #ffff40; text-decoration: none;}" |
| 2335 | "a.px:hover {color: #ffffff; text-decoration: none;}" |
| 2336 | "a.lfsb:link {color: #000000; text-decoration: none;}" |
| 2337 | "a.lfsb:visited {color: #000000; text-decoration: none;}" |
| 2338 | "a.lfsb:hover {color: #505050; text-decoration: none;}" |
| 2339 | "\n" |
| 2340 | "table.tbl { border-collapse: collapse; border-style: none;}\n" |
| 2341 | "table.tbl td { text-align: right; border-width: 1px 1px 1px 1px; border-style: solid solid solid solid; padding: 2px 3px; border-color: gray; white-space: nowrap;}\n" |
| 2342 | "table.tbl td.ac { text-align: center;}\n" |
| 2343 | "table.tbl th { border-width: 1px; border-style: solid solid solid solid; border-color: gray;}\n" |
| 2344 | "table.tbl th.pxname { background: #b00040; color: #ffff40; font-weight: bold; border-style: solid solid none solid; padding: 2px 3px; white-space: nowrap;}\n" |
| 2345 | "table.tbl th.empty { border-style: none; empty-cells: hide; background: white;}\n" |
| 2346 | "table.tbl th.desc { background: white; border-style: solid solid none solid; text-align: left; padding: 2px 3px;}\n" |
| 2347 | "\n" |
| 2348 | "table.lgd { border-collapse: collapse; border-width: 1px; border-style: none none none solid; border-color: black;}\n" |
| 2349 | "table.lgd td { border-width: 1px; border-style: solid solid solid solid; border-color: gray; padding: 2px;}\n" |
| 2350 | "table.lgd td.noborder { border-style: none; padding: 2px; white-space: nowrap;}\n" |
| 2351 | "table.det { border-collapse: collapse; border-style: none; }\n" |
| 2352 | "table.det th { text-align: left; border-width: 0px; padding: 0px 1px 0px 0px; font-style:normal;font-size:11px;font-weight:bold;font-family: sans-serif;}\n" |
| 2353 | "table.det td { text-align: right; border-width: 0px; padding: 0px 0px 0px 4px; white-space: nowrap; font-style:normal;font-size:11px;font-weight:normal;}\n" |
| 2354 | "u {text-decoration:none; border-bottom: 1px dotted black;}\n" |
| 2355 | "div.tips {\n" |
| 2356 | " display:block;\n" |
| 2357 | " visibility:hidden;\n" |
| 2358 | " z-index:2147483647;\n" |
| 2359 | " position:absolute;\n" |
| 2360 | " padding:2px 4px 3px;\n" |
| 2361 | " background:#f0f060; color:#000000;\n" |
| 2362 | " border:1px solid #7040c0;\n" |
| 2363 | " white-space:nowrap;\n" |
| 2364 | " font-style:normal;font-size:11px;font-weight:normal;\n" |
| 2365 | " -moz-border-radius:3px;-webkit-border-radius:3px;border-radius:3px;\n" |
| 2366 | " -moz-box-shadow:gray 2px 2px 3px;-webkit-box-shadow:gray 2px 2px 3px;box-shadow:gray 2px 2px 3px;\n" |
| 2367 | "}\n" |
| 2368 | "u:hover div.tips {visibility:visible;}\n" |
| 2369 | "-->\n" |
| 2370 | "</style></head>\n", |
| 2371 | (uri->flags & ST_SHNODE) ? " on " : "", |
| 2372 | (uri->flags & ST_SHNODE) ? (uri->node ? uri->node : global.node) : "" |
| 2373 | ); |
| 2374 | } |
| 2375 | |
| 2376 | /* Dumps the HTML stats information block to the trash for and uses the state from |
| 2377 | * stream interface <si> and per-uri parameters <uri>. The caller is responsible |
| 2378 | * for clearing the trash if needed. |
| 2379 | */ |
| 2380 | static void stats_dump_html_info(struct stream_interface *si, struct uri_auth *uri) |
| 2381 | { |
| 2382 | struct appctx *appctx = __objt_appctx(si->end); |
| 2383 | unsigned int up = (now.tv_sec - start_date.tv_sec); |
| 2384 | char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN]; |
Christopher Faulet | ed7a066 | 2019-01-14 11:07:34 +0100 | [diff] [blame] | 2385 | const char *scope_ptr = stats_scope_ptr(appctx, si); |
William Lallemand | 74c24fb | 2016-11-21 17:18:36 +0100 | [diff] [blame] | 2386 | |
| 2387 | /* WARNING! this has to fit the first packet too. |
| 2388 | * We are around 3.5 kB, add adding entries will |
| 2389 | * become tricky if we want to support 4kB buffers ! |
| 2390 | */ |
| 2391 | chunk_appendf(&trash, |
| 2392 | "<body><h1><a href=\"" PRODUCT_URL "\" style=\"text-decoration: none;\">" |
| 2393 | PRODUCT_NAME "%s</a></h1>\n" |
| 2394 | "<h2>Statistics Report for pid %d%s%s%s%s</h2>\n" |
| 2395 | "<hr width=\"100%%\" class=\"hr\">\n" |
| 2396 | "<h3>> General process information</h3>\n" |
| 2397 | "<table border=0><tr><td align=\"left\" nowrap width=\"1%%\">\n" |
Yves Lafon | 9531728 | 2018-02-26 11:10:37 +0100 | [diff] [blame] | 2398 | "<p><b>pid = </b> %d (process #%d, nbproc = %d, nbthread = %d)<br>\n" |
William Lallemand | 74c24fb | 2016-11-21 17:18:36 +0100 | [diff] [blame] | 2399 | "<b>uptime = </b> %dd %dh%02dm%02ds<br>\n" |
| 2400 | "<b>system limits:</b> memmax = %s%s; ulimit-n = %d<br>\n" |
| 2401 | "<b>maxsock = </b> %d; <b>maxconn = </b> %d; <b>maxpipes = </b> %d<br>\n" |
| 2402 | "current conns = %d; current pipes = %d/%d; conn rate = %d/sec<br>\n" |
| 2403 | "Running tasks: %d/%d; idle = %d %%<br>\n" |
| 2404 | "</td><td align=\"center\" nowrap>\n" |
| 2405 | "<table class=\"lgd\"><tr>\n" |
| 2406 | "<td class=\"active_up\"> </td><td class=\"noborder\">active UP </td>" |
| 2407 | "<td class=\"backup_up\"> </td><td class=\"noborder\">backup UP </td>" |
| 2408 | "</tr><tr>\n" |
| 2409 | "<td class=\"active_going_down\"></td><td class=\"noborder\">active UP, going down </td>" |
| 2410 | "<td class=\"backup_going_down\"></td><td class=\"noborder\">backup UP, going down </td>" |
| 2411 | "</tr><tr>\n" |
| 2412 | "<td class=\"active_going_up\"></td><td class=\"noborder\">active DOWN, going up </td>" |
| 2413 | "<td class=\"backup_going_up\"></td><td class=\"noborder\">backup DOWN, going up </td>" |
| 2414 | "</tr><tr>\n" |
| 2415 | "<td class=\"active_down\"></td><td class=\"noborder\">active or backup DOWN </td>" |
| 2416 | "<td class=\"active_no_check\"></td><td class=\"noborder\">not checked </td>" |
| 2417 | "</tr><tr>\n" |
| 2418 | "<td class=\"maintain\"></td><td class=\"noborder\" colspan=\"3\">active or backup DOWN for maintenance (MAINT) </td>" |
| 2419 | "</tr><tr>\n" |
| 2420 | "<td class=\"active_draining\"></td><td class=\"noborder\" colspan=\"3\">active or backup SOFT STOPPED for maintenance </td>" |
| 2421 | "</tr></table>\n" |
| 2422 | "Note: \"NOLB\"/\"DRAIN\" = UP with load-balancing disabled." |
| 2423 | "</td>" |
| 2424 | "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">" |
| 2425 | "<b>Display option:</b><ul style=\"margin-top: 0.25em;\">" |
| 2426 | "", |
Willy Tarreau | 909b9d8 | 2019-01-04 18:20:32 +0100 | [diff] [blame] | 2427 | (uri->flags & ST_HIDEVER) ? "" : (stats_version_string), |
William Lallemand | 74c24fb | 2016-11-21 17:18:36 +0100 | [diff] [blame] | 2428 | pid, (uri->flags & ST_SHNODE) ? " on " : "", |
| 2429 | (uri->flags & ST_SHNODE) ? (uri->node ? uri->node : global.node) : "", |
| 2430 | (uri->flags & ST_SHDESC) ? ": " : "", |
| 2431 | (uri->flags & ST_SHDESC) ? (uri->desc ? uri->desc : global.desc) : "", |
Yves Lafon | 9531728 | 2018-02-26 11:10:37 +0100 | [diff] [blame] | 2432 | pid, relative_pid, global.nbproc, global.nbthread, |
William Lallemand | 74c24fb | 2016-11-21 17:18:36 +0100 | [diff] [blame] | 2433 | up / 86400, (up % 86400) / 3600, |
| 2434 | (up % 3600) / 60, (up % 60), |
| 2435 | global.rlimit_memmax ? ultoa(global.rlimit_memmax) : "unlimited", |
| 2436 | global.rlimit_memmax ? " MB" : "", |
| 2437 | global.rlimit_nofile, |
| 2438 | global.maxsock, global.maxconn, global.maxpipes, |
| 2439 | actconn, pipes_used, pipes_used+pipes_free, read_freq_ctr(&global.conn_per_sec), |
Christopher Faulet | 34c5cc9 | 2016-12-06 09:15:30 +0100 | [diff] [blame] | 2440 | tasks_run_queue_cur, nb_tasks_cur, idle_pct |
William Lallemand | 74c24fb | 2016-11-21 17:18:36 +0100 | [diff] [blame] | 2441 | ); |
| 2442 | |
| 2443 | /* scope_txt = search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */ |
Christopher Faulet | ed7a066 | 2019-01-14 11:07:34 +0100 | [diff] [blame] | 2444 | memcpy(scope_txt, scope_ptr, appctx->ctx.stats.scope_len); |
William Lallemand | 74c24fb | 2016-11-21 17:18:36 +0100 | [diff] [blame] | 2445 | scope_txt[appctx->ctx.stats.scope_len] = '\0'; |
| 2446 | |
| 2447 | chunk_appendf(&trash, |
| 2448 | "<li><form method=\"GET\">Scope : <input value=\"%s\" name=\"" STAT_SCOPE_INPUT_NAME "\" size=\"8\" maxlength=\"%d\" tabindex=\"1\"/></form>\n", |
| 2449 | (appctx->ctx.stats.scope_len > 0) ? scope_txt : "", |
| 2450 | STAT_SCOPE_TXT_MAXLEN); |
| 2451 | |
| 2452 | /* scope_txt = search pattern + search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */ |
| 2453 | scope_txt[0] = 0; |
| 2454 | if (appctx->ctx.stats.scope_len) { |
| 2455 | strcpy(scope_txt, STAT_SCOPE_PATTERN); |
Christopher Faulet | ed7a066 | 2019-01-14 11:07:34 +0100 | [diff] [blame] | 2456 | memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), scope_ptr, appctx->ctx.stats.scope_len); |
William Lallemand | 74c24fb | 2016-11-21 17:18:36 +0100 | [diff] [blame] | 2457 | scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0; |
| 2458 | } |
| 2459 | |
| 2460 | if (appctx->ctx.stats.flags & STAT_HIDE_DOWN) |
| 2461 | chunk_appendf(&trash, |
| 2462 | "<li><a href=\"%s%s%s%s\">Show all servers</a><br>\n", |
| 2463 | uri->uri_prefix, |
| 2464 | "", |
| 2465 | (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "", |
| 2466 | scope_txt); |
| 2467 | else |
| 2468 | chunk_appendf(&trash, |
| 2469 | "<li><a href=\"%s%s%s%s\">Hide 'DOWN' servers</a><br>\n", |
| 2470 | uri->uri_prefix, |
| 2471 | ";up", |
| 2472 | (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "", |
| 2473 | scope_txt); |
| 2474 | |
| 2475 | if (uri->refresh > 0) { |
| 2476 | if (appctx->ctx.stats.flags & STAT_NO_REFRESH) |
| 2477 | chunk_appendf(&trash, |
| 2478 | "<li><a href=\"%s%s%s%s\">Enable refresh</a><br>\n", |
| 2479 | uri->uri_prefix, |
| 2480 | (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "", |
| 2481 | "", |
| 2482 | scope_txt); |
| 2483 | else |
| 2484 | chunk_appendf(&trash, |
| 2485 | "<li><a href=\"%s%s%s%s\">Disable refresh</a><br>\n", |
| 2486 | uri->uri_prefix, |
| 2487 | (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "", |
| 2488 | ";norefresh", |
| 2489 | scope_txt); |
| 2490 | } |
| 2491 | |
| 2492 | chunk_appendf(&trash, |
| 2493 | "<li><a href=\"%s%s%s%s\">Refresh now</a><br>\n", |
| 2494 | uri->uri_prefix, |
| 2495 | (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "", |
| 2496 | (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "", |
| 2497 | scope_txt); |
| 2498 | |
| 2499 | chunk_appendf(&trash, |
| 2500 | "<li><a href=\"%s;csv%s%s\">CSV export</a><br>\n", |
| 2501 | uri->uri_prefix, |
| 2502 | (uri->refresh > 0) ? ";norefresh" : "", |
| 2503 | scope_txt); |
| 2504 | |
| 2505 | chunk_appendf(&trash, |
| 2506 | "</ul></td>" |
| 2507 | "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">" |
| 2508 | "<b>External resources:</b><ul style=\"margin-top: 0.25em;\">\n" |
| 2509 | "<li><a href=\"" PRODUCT_URL "\">Primary site</a><br>\n" |
| 2510 | "<li><a href=\"" PRODUCT_URL_UPD "\">Updates (v" PRODUCT_BRANCH ")</a><br>\n" |
| 2511 | "<li><a href=\"" PRODUCT_URL_DOC "\">Online manual</a><br>\n" |
| 2512 | "</ul>" |
| 2513 | "</td>" |
| 2514 | "</tr></table>\n" |
| 2515 | "" |
| 2516 | ); |
| 2517 | |
| 2518 | if (appctx->ctx.stats.st_code) { |
| 2519 | switch (appctx->ctx.stats.st_code) { |
| 2520 | case STAT_STATUS_DONE: |
| 2521 | chunk_appendf(&trash, |
| 2522 | "<p><div class=active_up>" |
| 2523 | "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> " |
| 2524 | "Action processed successfully." |
| 2525 | "</div>\n", uri->uri_prefix, |
| 2526 | (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "", |
| 2527 | (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "", |
| 2528 | scope_txt); |
| 2529 | break; |
| 2530 | case STAT_STATUS_NONE: |
| 2531 | chunk_appendf(&trash, |
| 2532 | "<p><div class=active_going_down>" |
| 2533 | "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> " |
| 2534 | "Nothing has changed." |
| 2535 | "</div>\n", uri->uri_prefix, |
| 2536 | (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "", |
| 2537 | (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "", |
| 2538 | scope_txt); |
| 2539 | break; |
| 2540 | case STAT_STATUS_PART: |
| 2541 | chunk_appendf(&trash, |
| 2542 | "<p><div class=active_going_down>" |
| 2543 | "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> " |
| 2544 | "Action partially processed.<br>" |
| 2545 | "Some server names are probably unknown or ambiguous (duplicated names in the backend)." |
| 2546 | "</div>\n", uri->uri_prefix, |
| 2547 | (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "", |
| 2548 | (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "", |
| 2549 | scope_txt); |
| 2550 | break; |
| 2551 | case STAT_STATUS_ERRP: |
| 2552 | chunk_appendf(&trash, |
| 2553 | "<p><div class=active_down>" |
| 2554 | "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> " |
| 2555 | "Action not processed because of invalid parameters." |
| 2556 | "<ul>" |
| 2557 | "<li>The action is maybe unknown.</li>" |
Christopher Faulet | 2f9a41d | 2019-02-27 15:30:57 +0100 | [diff] [blame] | 2558 | "<li>Invalid key parameter (empty or too long).</li>" |
William Lallemand | 74c24fb | 2016-11-21 17:18:36 +0100 | [diff] [blame] | 2559 | "<li>The backend name is probably unknown or ambiguous (duplicated names).</li>" |
| 2560 | "<li>Some server names are probably unknown or ambiguous (duplicated names in the backend).</li>" |
| 2561 | "</ul>" |
| 2562 | "</div>\n", uri->uri_prefix, |
| 2563 | (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "", |
| 2564 | (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "", |
| 2565 | scope_txt); |
| 2566 | break; |
| 2567 | case STAT_STATUS_EXCD: |
| 2568 | chunk_appendf(&trash, |
| 2569 | "<p><div class=active_down>" |
| 2570 | "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> " |
| 2571 | "<b>Action not processed : the buffer couldn't store all the data.<br>" |
| 2572 | "You should retry with less servers at a time.</b>" |
| 2573 | "</div>\n", uri->uri_prefix, |
| 2574 | (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "", |
| 2575 | (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "", |
| 2576 | scope_txt); |
| 2577 | break; |
| 2578 | case STAT_STATUS_DENY: |
| 2579 | chunk_appendf(&trash, |
| 2580 | "<p><div class=active_down>" |
| 2581 | "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> " |
| 2582 | "<b>Action denied.</b>" |
| 2583 | "</div>\n", uri->uri_prefix, |
| 2584 | (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "", |
| 2585 | (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "", |
| 2586 | scope_txt); |
| 2587 | break; |
Christopher Faulet | 3c2ecf7 | 2019-02-27 16:41:27 +0100 | [diff] [blame] | 2588 | case STAT_STATUS_IVAL: |
| 2589 | chunk_appendf(&trash, |
| 2590 | "<p><div class=active_down>" |
| 2591 | "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> " |
| 2592 | "<b>Invalid requests (unsupported method or chunked encoded request).</b>" |
| 2593 | "</div>\n", uri->uri_prefix, |
| 2594 | (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "", |
| 2595 | (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "", |
| 2596 | scope_txt); |
| 2597 | break; |
William Lallemand | 74c24fb | 2016-11-21 17:18:36 +0100 | [diff] [blame] | 2598 | default: |
| 2599 | chunk_appendf(&trash, |
| 2600 | "<p><div class=active_no_check>" |
| 2601 | "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> " |
| 2602 | "Unexpected result." |
| 2603 | "</div>\n", uri->uri_prefix, |
| 2604 | (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "", |
| 2605 | (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "", |
| 2606 | scope_txt); |
| 2607 | } |
| 2608 | chunk_appendf(&trash, "<p>\n"); |
| 2609 | } |
| 2610 | } |
| 2611 | |
| 2612 | /* Dumps the HTML stats trailer block to the trash. The caller is responsible |
| 2613 | * for clearing the trash if needed. |
| 2614 | */ |
| 2615 | static void stats_dump_html_end() |
| 2616 | { |
| 2617 | chunk_appendf(&trash, "</body></html>\n"); |
| 2618 | } |
| 2619 | |
Simon Horman | 05ee213 | 2017-01-04 09:37:25 +0100 | [diff] [blame] | 2620 | /* Dumps the stats JSON header to the trash buffer which. The caller is responsible |
| 2621 | * for clearing it if needed. |
| 2622 | */ |
| 2623 | static void stats_dump_json_header() |
| 2624 | { |
| 2625 | chunk_strcat(&trash, "["); |
| 2626 | } |
| 2627 | |
| 2628 | |
| 2629 | /* Dumps the JSON stats trailer block to the trash. The caller is responsible |
| 2630 | * for clearing the trash if needed. |
| 2631 | */ |
| 2632 | static void stats_dump_json_end() |
| 2633 | { |
| 2634 | chunk_strcat(&trash, "]"); |
| 2635 | } |
| 2636 | |
William Lallemand | 74c24fb | 2016-11-21 17:18:36 +0100 | [diff] [blame] | 2637 | /* This function dumps statistics onto the stream interface's read buffer in |
| 2638 | * either CSV or HTML format. <uri> contains some HTML-specific parameters that |
| 2639 | * are ignored for CSV format (hence <uri> may be NULL there). It returns 0 if |
| 2640 | * it had to stop writing data and an I/O is needed, 1 if the dump is finished |
| 2641 | * and the stream must be closed, or -1 in case of any error. This function is |
| 2642 | * used by both the CLI and the HTTP handlers. |
| 2643 | */ |
Christopher Faulet | ef77922 | 2018-10-31 08:47:01 +0100 | [diff] [blame] | 2644 | static int stats_dump_stat_to_buffer(struct stream_interface *si, struct htx *htx, |
| 2645 | struct uri_auth *uri) |
William Lallemand | 74c24fb | 2016-11-21 17:18:36 +0100 | [diff] [blame] | 2646 | { |
| 2647 | struct appctx *appctx = __objt_appctx(si->end); |
| 2648 | struct channel *rep = si_ic(si); |
| 2649 | struct proxy *px; |
| 2650 | |
| 2651 | chunk_reset(&trash); |
| 2652 | |
| 2653 | switch (appctx->st2) { |
| 2654 | case STAT_ST_INIT: |
| 2655 | appctx->st2 = STAT_ST_HEAD; /* let's start producing data */ |
| 2656 | /* fall through */ |
| 2657 | |
| 2658 | case STAT_ST_HEAD: |
| 2659 | if (appctx->ctx.stats.flags & STAT_FMT_HTML) |
| 2660 | stats_dump_html_head(uri); |
Simon Horman | 05ee213 | 2017-01-04 09:37:25 +0100 | [diff] [blame] | 2661 | else if (appctx->ctx.stats.flags & STAT_FMT_JSON) |
Willy Tarreau | 9d7fb63 | 2017-04-11 07:53:04 +0200 | [diff] [blame] | 2662 | stats_dump_json_header(); |
William Lallemand | 74c24fb | 2016-11-21 17:18:36 +0100 | [diff] [blame] | 2663 | else if (!(appctx->ctx.stats.flags & STAT_FMT_TYPED)) |
| 2664 | stats_dump_csv_header(); |
| 2665 | |
Christopher Faulet | ef77922 | 2018-10-31 08:47:01 +0100 | [diff] [blame] | 2666 | if (!stats_putchk(rep, htx, &trash)) |
| 2667 | goto full; |
William Lallemand | 74c24fb | 2016-11-21 17:18:36 +0100 | [diff] [blame] | 2668 | |
| 2669 | appctx->st2 = STAT_ST_INFO; |
| 2670 | /* fall through */ |
| 2671 | |
| 2672 | case STAT_ST_INFO: |
| 2673 | if (appctx->ctx.stats.flags & STAT_FMT_HTML) { |
| 2674 | stats_dump_html_info(si, uri); |
Christopher Faulet | ef77922 | 2018-10-31 08:47:01 +0100 | [diff] [blame] | 2675 | if (!stats_putchk(rep, htx, &trash)) |
| 2676 | goto full; |
William Lallemand | 74c24fb | 2016-11-21 17:18:36 +0100 | [diff] [blame] | 2677 | } |
| 2678 | |
Olivier Houchard | fbc74e8 | 2017-11-24 16:54:05 +0100 | [diff] [blame] | 2679 | appctx->ctx.stats.px = proxies_list; |
William Lallemand | 74c24fb | 2016-11-21 17:18:36 +0100 | [diff] [blame] | 2680 | appctx->ctx.stats.px_st = STAT_PX_ST_INIT; |
| 2681 | appctx->st2 = STAT_ST_LIST; |
| 2682 | /* fall through */ |
| 2683 | |
| 2684 | case STAT_ST_LIST: |
| 2685 | /* dump proxies */ |
| 2686 | while (appctx->ctx.stats.px) { |
Christopher Faulet | ef77922 | 2018-10-31 08:47:01 +0100 | [diff] [blame] | 2687 | if (htx) { |
| 2688 | if (htx_almost_full(htx)) |
| 2689 | goto full; |
| 2690 | } |
| 2691 | else { |
| 2692 | if (buffer_almost_full(&rep->buf)) |
| 2693 | goto full; |
William Lallemand | 74c24fb | 2016-11-21 17:18:36 +0100 | [diff] [blame] | 2694 | } |
| 2695 | |
| 2696 | px = appctx->ctx.stats.px; |
| 2697 | /* skip the disabled proxies, global frontend and non-networked ones */ |
| 2698 | if (px->state != PR_STSTOPPED && px->uuid > 0 && (px->cap & (PR_CAP_FE | PR_CAP_BE))) |
Christopher Faulet | ef77922 | 2018-10-31 08:47:01 +0100 | [diff] [blame] | 2699 | if (stats_dump_proxy_to_buffer(si, htx, px, uri) == 0) |
William Lallemand | 74c24fb | 2016-11-21 17:18:36 +0100 | [diff] [blame] | 2700 | return 0; |
| 2701 | |
| 2702 | appctx->ctx.stats.px = px->next; |
| 2703 | appctx->ctx.stats.px_st = STAT_PX_ST_INIT; |
| 2704 | } |
| 2705 | /* here, we just have reached the last proxy */ |
| 2706 | |
| 2707 | appctx->st2 = STAT_ST_END; |
| 2708 | /* fall through */ |
| 2709 | |
| 2710 | case STAT_ST_END: |
Simon Horman | 05ee213 | 2017-01-04 09:37:25 +0100 | [diff] [blame] | 2711 | if (appctx->ctx.stats.flags & (STAT_FMT_HTML|STAT_FMT_JSON)) { |
| 2712 | if (appctx->ctx.stats.flags & STAT_FMT_HTML) |
| 2713 | stats_dump_html_end(); |
| 2714 | else |
| 2715 | stats_dump_json_end(); |
Christopher Faulet | ef77922 | 2018-10-31 08:47:01 +0100 | [diff] [blame] | 2716 | if (!stats_putchk(rep, htx, &trash)) |
| 2717 | goto full; |
William Lallemand | 74c24fb | 2016-11-21 17:18:36 +0100 | [diff] [blame] | 2718 | } |
| 2719 | |
| 2720 | appctx->st2 = STAT_ST_FIN; |
| 2721 | /* fall through */ |
| 2722 | |
| 2723 | case STAT_ST_FIN: |
| 2724 | return 1; |
| 2725 | |
| 2726 | default: |
| 2727 | /* unknown state ! */ |
| 2728 | appctx->st2 = STAT_ST_FIN; |
| 2729 | return -1; |
| 2730 | } |
Christopher Faulet | ef77922 | 2018-10-31 08:47:01 +0100 | [diff] [blame] | 2731 | |
| 2732 | full: |
| 2733 | si_rx_room_blk(si); |
| 2734 | return 0; |
| 2735 | |
William Lallemand | 74c24fb | 2016-11-21 17:18:36 +0100 | [diff] [blame] | 2736 | } |
| 2737 | |
| 2738 | /* We reached the stats page through a POST request. The appctx is |
| 2739 | * expected to have already been allocated by the caller. |
| 2740 | * Parse the posted data and enable/disable servers if necessary. |
| 2741 | * Returns 1 if request was parsed or zero if it needs more data. |
| 2742 | */ |
| 2743 | static int stats_process_http_post(struct stream_interface *si) |
| 2744 | { |
| 2745 | struct stream *s = si_strm(si); |
| 2746 | struct appctx *appctx = objt_appctx(si->end); |
| 2747 | |
| 2748 | struct proxy *px = NULL; |
| 2749 | struct server *sv = NULL; |
| 2750 | |
| 2751 | char key[LINESIZE]; |
| 2752 | int action = ST_ADM_ACTION_NONE; |
| 2753 | int reprocess = 0; |
| 2754 | |
| 2755 | int total_servers = 0; |
| 2756 | int altered_servers = 0; |
| 2757 | |
| 2758 | char *first_param, *cur_param, *next_param, *end_params; |
| 2759 | char *st_cur_param = NULL; |
| 2760 | char *st_next_param = NULL; |
| 2761 | |
Christopher Faulet | a361837 | 2018-12-13 21:59:56 +0100 | [diff] [blame] | 2762 | struct buffer *temp = get_trash_chunk(); |
William Lallemand | 74c24fb | 2016-11-21 17:18:36 +0100 | [diff] [blame] | 2763 | |
Christopher Faulet | a361837 | 2018-12-13 21:59:56 +0100 | [diff] [blame] | 2764 | if (IS_HTX_STRM(s)) { |
| 2765 | struct htx *htx = htxbuf(&s->req.buf); |
| 2766 | struct htx_blk *blk; |
Christopher Faulet | a361837 | 2018-12-13 21:59:56 +0100 | [diff] [blame] | 2767 | |
Christopher Faulet | 2f9a41d | 2019-02-27 15:30:57 +0100 | [diff] [blame] | 2768 | /* we need more data */ |
| 2769 | if (s->txn->req.msg_state < HTTP_MSG_DONE) { |
| 2770 | /* check if we can receive more */ |
| 2771 | if (htx_free_data_space(htx) <= global.tune.maxrewrite) { |
| 2772 | appctx->ctx.stats.st_code = STAT_STATUS_EXCD; |
| 2773 | goto out; |
Christopher Faulet | a361837 | 2018-12-13 21:59:56 +0100 | [diff] [blame] | 2774 | } |
Christopher Faulet | 2f9a41d | 2019-02-27 15:30:57 +0100 | [diff] [blame] | 2775 | goto wait; |
Christopher Faulet | a361837 | 2018-12-13 21:59:56 +0100 | [diff] [blame] | 2776 | } |
William Lallemand | 74c24fb | 2016-11-21 17:18:36 +0100 | [diff] [blame] | 2777 | |
Christopher Faulet | 2f9a41d | 2019-02-27 15:30:57 +0100 | [diff] [blame] | 2778 | /* The request was fully received. Copy data */ |
| 2779 | blk = htx_get_head_blk(htx); |
| 2780 | while (blk) { |
Christopher Faulet | a361837 | 2018-12-13 21:59:56 +0100 | [diff] [blame] | 2781 | enum htx_blk_type type = htx_get_blk_type(blk); |
Christopher Faulet | a361837 | 2018-12-13 21:59:56 +0100 | [diff] [blame] | 2782 | |
| 2783 | if (type == HTX_BLK_EOM || type == HTX_BLK_EOD) |
| 2784 | break; |
| 2785 | if (type == HTX_BLK_DATA) { |
| 2786 | struct ist v = htx_get_blk_value(htx, blk); |
| 2787 | |
| 2788 | if (!chunk_memcat(temp, v.ptr, v.len)) { |
| 2789 | appctx->ctx.stats.st_code = STAT_STATUS_EXCD; |
| 2790 | goto out; |
| 2791 | } |
| 2792 | } |
Christopher Faulet | ed7a066 | 2019-01-14 11:07:34 +0100 | [diff] [blame] | 2793 | blk = htx_get_next_blk(htx, blk); |
Christopher Faulet | a361837 | 2018-12-13 21:59:56 +0100 | [diff] [blame] | 2794 | } |
| 2795 | } |
| 2796 | else { |
| 2797 | int reql; |
| 2798 | |
Christopher Faulet | 2f9a41d | 2019-02-27 15:30:57 +0100 | [diff] [blame] | 2799 | /* we need more data */ |
| 2800 | if (s->txn->req.msg_state < HTTP_MSG_DONE) { |
| 2801 | /* check if we can receive more */ |
| 2802 | if (c_room(&s->req) <= global.tune.maxrewrite) { |
| 2803 | appctx->ctx.stats.st_code = STAT_STATUS_EXCD; |
| 2804 | goto out; |
| 2805 | } |
| 2806 | goto wait; |
Christopher Faulet | a361837 | 2018-12-13 21:59:56 +0100 | [diff] [blame] | 2807 | } |
Christopher Faulet | a361837 | 2018-12-13 21:59:56 +0100 | [diff] [blame] | 2808 | reql = co_getblk(si_oc(si), temp->area, s->txn->req.body_len, |
| 2809 | s->txn->req.eoh + 2); |
| 2810 | if (reql <= 0) { |
Christopher Faulet | 2f9a41d | 2019-02-27 15:30:57 +0100 | [diff] [blame] | 2811 | appctx->ctx.stats.st_code = STAT_STATUS_EXCD; |
| 2812 | goto out; |
Christopher Faulet | a361837 | 2018-12-13 21:59:56 +0100 | [diff] [blame] | 2813 | } |
| 2814 | temp->data = reql; |
William Lallemand | 74c24fb | 2016-11-21 17:18:36 +0100 | [diff] [blame] | 2815 | } |
| 2816 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2817 | first_param = temp->area; |
Christopher Faulet | a361837 | 2018-12-13 21:59:56 +0100 | [diff] [blame] | 2818 | end_params = temp->area + temp->data; |
William Lallemand | 74c24fb | 2016-11-21 17:18:36 +0100 | [diff] [blame] | 2819 | cur_param = next_param = end_params; |
| 2820 | *end_params = '\0'; |
| 2821 | |
| 2822 | appctx->ctx.stats.st_code = STAT_STATUS_NONE; |
| 2823 | |
| 2824 | /* |
| 2825 | * Parse the parameters in reverse order to only store the last value. |
| 2826 | * From the html form, the backend and the action are at the end. |
| 2827 | */ |
| 2828 | while (cur_param > first_param) { |
| 2829 | char *value; |
| 2830 | int poffset, plen; |
| 2831 | |
| 2832 | cur_param--; |
| 2833 | |
| 2834 | if ((*cur_param == '&') || (cur_param == first_param)) { |
| 2835 | reprocess_servers: |
| 2836 | /* Parse the key */ |
| 2837 | poffset = (cur_param != first_param ? 1 : 0); |
| 2838 | plen = next_param - cur_param + (cur_param == first_param ? 1 : 0); |
| 2839 | if ((plen > 0) && (plen <= sizeof(key))) { |
| 2840 | strncpy(key, cur_param + poffset, plen); |
| 2841 | key[plen - 1] = '\0'; |
| 2842 | } else { |
Christopher Faulet | 2f9a41d | 2019-02-27 15:30:57 +0100 | [diff] [blame] | 2843 | appctx->ctx.stats.st_code = STAT_STATUS_ERRP; |
William Lallemand | 74c24fb | 2016-11-21 17:18:36 +0100 | [diff] [blame] | 2844 | goto out; |
| 2845 | } |
| 2846 | |
| 2847 | /* Parse the value */ |
| 2848 | value = key; |
| 2849 | while (*value != '\0' && *value != '=') { |
| 2850 | value++; |
| 2851 | } |
| 2852 | if (*value == '=') { |
| 2853 | /* Ok, a value is found, we can mark the end of the key */ |
| 2854 | *value++ = '\0'; |
| 2855 | } |
| 2856 | if (url_decode(key) < 0 || url_decode(value) < 0) |
| 2857 | break; |
| 2858 | |
| 2859 | /* Now we can check the key to see what to do */ |
| 2860 | if (!px && (strcmp(key, "b") == 0)) { |
| 2861 | if ((px = proxy_be_by_name(value)) == NULL) { |
| 2862 | /* the backend name is unknown or ambiguous (duplicate names) */ |
| 2863 | appctx->ctx.stats.st_code = STAT_STATUS_ERRP; |
| 2864 | goto out; |
| 2865 | } |
| 2866 | } |
| 2867 | else if (!action && (strcmp(key, "action") == 0)) { |
| 2868 | if (strcmp(value, "ready") == 0) { |
| 2869 | action = ST_ADM_ACTION_READY; |
| 2870 | } |
| 2871 | else if (strcmp(value, "drain") == 0) { |
| 2872 | action = ST_ADM_ACTION_DRAIN; |
| 2873 | } |
| 2874 | else if (strcmp(value, "maint") == 0) { |
| 2875 | action = ST_ADM_ACTION_MAINT; |
| 2876 | } |
| 2877 | else if (strcmp(value, "shutdown") == 0) { |
| 2878 | action = ST_ADM_ACTION_SHUTDOWN; |
| 2879 | } |
| 2880 | else if (strcmp(value, "dhlth") == 0) { |
| 2881 | action = ST_ADM_ACTION_DHLTH; |
| 2882 | } |
| 2883 | else if (strcmp(value, "ehlth") == 0) { |
| 2884 | action = ST_ADM_ACTION_EHLTH; |
| 2885 | } |
| 2886 | else if (strcmp(value, "hrunn") == 0) { |
| 2887 | action = ST_ADM_ACTION_HRUNN; |
| 2888 | } |
| 2889 | else if (strcmp(value, "hnolb") == 0) { |
| 2890 | action = ST_ADM_ACTION_HNOLB; |
| 2891 | } |
| 2892 | else if (strcmp(value, "hdown") == 0) { |
| 2893 | action = ST_ADM_ACTION_HDOWN; |
| 2894 | } |
| 2895 | else if (strcmp(value, "dagent") == 0) { |
| 2896 | action = ST_ADM_ACTION_DAGENT; |
| 2897 | } |
| 2898 | else if (strcmp(value, "eagent") == 0) { |
| 2899 | action = ST_ADM_ACTION_EAGENT; |
| 2900 | } |
| 2901 | else if (strcmp(value, "arunn") == 0) { |
| 2902 | action = ST_ADM_ACTION_ARUNN; |
| 2903 | } |
| 2904 | else if (strcmp(value, "adown") == 0) { |
| 2905 | action = ST_ADM_ACTION_ADOWN; |
| 2906 | } |
| 2907 | /* else these are the old supported methods */ |
| 2908 | else if (strcmp(value, "disable") == 0) { |
| 2909 | action = ST_ADM_ACTION_DISABLE; |
| 2910 | } |
| 2911 | else if (strcmp(value, "enable") == 0) { |
| 2912 | action = ST_ADM_ACTION_ENABLE; |
| 2913 | } |
| 2914 | else if (strcmp(value, "stop") == 0) { |
| 2915 | action = ST_ADM_ACTION_STOP; |
| 2916 | } |
| 2917 | else if (strcmp(value, "start") == 0) { |
| 2918 | action = ST_ADM_ACTION_START; |
| 2919 | } |
| 2920 | else { |
| 2921 | appctx->ctx.stats.st_code = STAT_STATUS_ERRP; |
| 2922 | goto out; |
| 2923 | } |
| 2924 | } |
| 2925 | else if (strcmp(key, "s") == 0) { |
| 2926 | if (!(px && action)) { |
| 2927 | /* |
| 2928 | * Indicates that we'll need to reprocess the parameters |
| 2929 | * as soon as backend and action are known |
| 2930 | */ |
| 2931 | if (!reprocess) { |
| 2932 | st_cur_param = cur_param; |
| 2933 | st_next_param = next_param; |
| 2934 | } |
| 2935 | reprocess = 1; |
| 2936 | } |
| 2937 | else if ((sv = findserver(px, value)) != NULL) { |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 2938 | HA_SPIN_LOCK(SERVER_LOCK, &sv->lock); |
William Lallemand | 74c24fb | 2016-11-21 17:18:36 +0100 | [diff] [blame] | 2939 | switch (action) { |
| 2940 | case ST_ADM_ACTION_DISABLE: |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 2941 | if (!(sv->cur_admin & SRV_ADMF_FMAINT)) { |
William Lallemand | 74c24fb | 2016-11-21 17:18:36 +0100 | [diff] [blame] | 2942 | altered_servers++; |
| 2943 | total_servers++; |
| 2944 | srv_set_admin_flag(sv, SRV_ADMF_FMAINT, "'disable' on stats page"); |
| 2945 | } |
| 2946 | break; |
| 2947 | case ST_ADM_ACTION_ENABLE: |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 2948 | if (sv->cur_admin & SRV_ADMF_FMAINT) { |
William Lallemand | 74c24fb | 2016-11-21 17:18:36 +0100 | [diff] [blame] | 2949 | altered_servers++; |
| 2950 | total_servers++; |
| 2951 | srv_clr_admin_flag(sv, SRV_ADMF_FMAINT); |
| 2952 | } |
| 2953 | break; |
| 2954 | case ST_ADM_ACTION_STOP: |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 2955 | if (!(sv->cur_admin & SRV_ADMF_FDRAIN)) { |
William Lallemand | 74c24fb | 2016-11-21 17:18:36 +0100 | [diff] [blame] | 2956 | srv_set_admin_flag(sv, SRV_ADMF_FDRAIN, "'stop' on stats page"); |
| 2957 | altered_servers++; |
| 2958 | total_servers++; |
| 2959 | } |
| 2960 | break; |
| 2961 | case ST_ADM_ACTION_START: |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 2962 | if (sv->cur_admin & SRV_ADMF_FDRAIN) { |
William Lallemand | 74c24fb | 2016-11-21 17:18:36 +0100 | [diff] [blame] | 2963 | srv_clr_admin_flag(sv, SRV_ADMF_FDRAIN); |
| 2964 | altered_servers++; |
| 2965 | total_servers++; |
| 2966 | } |
| 2967 | break; |
| 2968 | case ST_ADM_ACTION_DHLTH: |
| 2969 | if (sv->check.state & CHK_ST_CONFIGURED) { |
| 2970 | sv->check.state &= ~CHK_ST_ENABLED; |
| 2971 | altered_servers++; |
| 2972 | total_servers++; |
| 2973 | } |
| 2974 | break; |
| 2975 | case ST_ADM_ACTION_EHLTH: |
| 2976 | if (sv->check.state & CHK_ST_CONFIGURED) { |
| 2977 | sv->check.state |= CHK_ST_ENABLED; |
| 2978 | altered_servers++; |
| 2979 | total_servers++; |
| 2980 | } |
| 2981 | break; |
| 2982 | case ST_ADM_ACTION_HRUNN: |
| 2983 | if (!(sv->track)) { |
| 2984 | sv->check.health = sv->check.rise + sv->check.fall - 1; |
Emeric Brun | 5a13351 | 2017-10-19 14:42:30 +0200 | [diff] [blame] | 2985 | srv_set_running(sv, "changed from Web interface", NULL); |
William Lallemand | 74c24fb | 2016-11-21 17:18:36 +0100 | [diff] [blame] | 2986 | altered_servers++; |
| 2987 | total_servers++; |
| 2988 | } |
| 2989 | break; |
| 2990 | case ST_ADM_ACTION_HNOLB: |
| 2991 | if (!(sv->track)) { |
| 2992 | sv->check.health = sv->check.rise + sv->check.fall - 1; |
Emeric Brun | 5a13351 | 2017-10-19 14:42:30 +0200 | [diff] [blame] | 2993 | srv_set_stopping(sv, "changed from Web interface", NULL); |
William Lallemand | 74c24fb | 2016-11-21 17:18:36 +0100 | [diff] [blame] | 2994 | altered_servers++; |
| 2995 | total_servers++; |
| 2996 | } |
| 2997 | break; |
| 2998 | case ST_ADM_ACTION_HDOWN: |
| 2999 | if (!(sv->track)) { |
| 3000 | sv->check.health = 0; |
Emeric Brun | 5a13351 | 2017-10-19 14:42:30 +0200 | [diff] [blame] | 3001 | srv_set_stopped(sv, "changed from Web interface", NULL); |
William Lallemand | 74c24fb | 2016-11-21 17:18:36 +0100 | [diff] [blame] | 3002 | altered_servers++; |
| 3003 | total_servers++; |
| 3004 | } |
| 3005 | break; |
| 3006 | case ST_ADM_ACTION_DAGENT: |
| 3007 | if (sv->agent.state & CHK_ST_CONFIGURED) { |
| 3008 | sv->agent.state &= ~CHK_ST_ENABLED; |
| 3009 | altered_servers++; |
| 3010 | total_servers++; |
| 3011 | } |
| 3012 | break; |
| 3013 | case ST_ADM_ACTION_EAGENT: |
| 3014 | if (sv->agent.state & CHK_ST_CONFIGURED) { |
| 3015 | sv->agent.state |= CHK_ST_ENABLED; |
| 3016 | altered_servers++; |
| 3017 | total_servers++; |
| 3018 | } |
| 3019 | break; |
| 3020 | case ST_ADM_ACTION_ARUNN: |
| 3021 | if (sv->agent.state & CHK_ST_ENABLED) { |
| 3022 | sv->agent.health = sv->agent.rise + sv->agent.fall - 1; |
Emeric Brun | 5a13351 | 2017-10-19 14:42:30 +0200 | [diff] [blame] | 3023 | srv_set_running(sv, "changed from Web interface", NULL); |
William Lallemand | 74c24fb | 2016-11-21 17:18:36 +0100 | [diff] [blame] | 3024 | altered_servers++; |
| 3025 | total_servers++; |
| 3026 | } |
| 3027 | break; |
| 3028 | case ST_ADM_ACTION_ADOWN: |
| 3029 | if (sv->agent.state & CHK_ST_ENABLED) { |
| 3030 | sv->agent.health = 0; |
Emeric Brun | 5a13351 | 2017-10-19 14:42:30 +0200 | [diff] [blame] | 3031 | srv_set_stopped(sv, "changed from Web interface", NULL); |
William Lallemand | 74c24fb | 2016-11-21 17:18:36 +0100 | [diff] [blame] | 3032 | altered_servers++; |
| 3033 | total_servers++; |
| 3034 | } |
| 3035 | break; |
| 3036 | case ST_ADM_ACTION_READY: |
| 3037 | srv_adm_set_ready(sv); |
| 3038 | altered_servers++; |
| 3039 | total_servers++; |
| 3040 | break; |
| 3041 | case ST_ADM_ACTION_DRAIN: |
| 3042 | srv_adm_set_drain(sv); |
| 3043 | altered_servers++; |
| 3044 | total_servers++; |
| 3045 | break; |
| 3046 | case ST_ADM_ACTION_MAINT: |
| 3047 | srv_adm_set_maint(sv); |
| 3048 | altered_servers++; |
| 3049 | total_servers++; |
| 3050 | break; |
| 3051 | case ST_ADM_ACTION_SHUTDOWN: |
| 3052 | if (px->state != PR_STSTOPPED) { |
| 3053 | struct stream *sess, *sess_bck; |
| 3054 | |
| 3055 | list_for_each_entry_safe(sess, sess_bck, &sv->actconns, by_srv) |
| 3056 | if (sess->srv_conn == sv) |
| 3057 | stream_shutdown(sess, SF_ERR_KILLED); |
| 3058 | |
| 3059 | altered_servers++; |
| 3060 | total_servers++; |
| 3061 | } |
| 3062 | break; |
| 3063 | } |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 3064 | HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock); |
William Lallemand | 74c24fb | 2016-11-21 17:18:36 +0100 | [diff] [blame] | 3065 | } else { |
| 3066 | /* the server name is unknown or ambiguous (duplicate names) */ |
| 3067 | total_servers++; |
| 3068 | } |
| 3069 | } |
| 3070 | if (reprocess && px && action) { |
| 3071 | /* Now, we know the backend and the action chosen by the user. |
| 3072 | * We can safely restart from the first server parameter |
| 3073 | * to reprocess them |
| 3074 | */ |
| 3075 | cur_param = st_cur_param; |
| 3076 | next_param = st_next_param; |
| 3077 | reprocess = 0; |
| 3078 | goto reprocess_servers; |
| 3079 | } |
| 3080 | |
| 3081 | next_param = cur_param; |
| 3082 | } |
| 3083 | } |
| 3084 | |
| 3085 | if (total_servers == 0) { |
| 3086 | appctx->ctx.stats.st_code = STAT_STATUS_NONE; |
| 3087 | } |
| 3088 | else if (altered_servers == 0) { |
| 3089 | appctx->ctx.stats.st_code = STAT_STATUS_ERRP; |
| 3090 | } |
| 3091 | else if (altered_servers == total_servers) { |
| 3092 | appctx->ctx.stats.st_code = STAT_STATUS_DONE; |
| 3093 | } |
| 3094 | else { |
| 3095 | appctx->ctx.stats.st_code = STAT_STATUS_PART; |
| 3096 | } |
| 3097 | out: |
| 3098 | return 1; |
Christopher Faulet | 2f9a41d | 2019-02-27 15:30:57 +0100 | [diff] [blame] | 3099 | wait: |
| 3100 | appctx->ctx.stats.st_code = STAT_STATUS_NONE; |
| 3101 | return 0; |
Christopher Faulet | ef77922 | 2018-10-31 08:47:01 +0100 | [diff] [blame] | 3102 | } |
| 3103 | |
| 3104 | |
| 3105 | static int stats_send_htx_headers(struct stream_interface *si, struct htx *htx) |
| 3106 | { |
| 3107 | struct stream *s = si_strm(si); |
| 3108 | struct uri_auth *uri = s->be->uri_auth; |
| 3109 | struct appctx *appctx = __objt_appctx(si->end); |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 3110 | struct htx_sl *sl; |
| 3111 | unsigned int flags; |
Christopher Faulet | ef77922 | 2018-10-31 08:47:01 +0100 | [diff] [blame] | 3112 | |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 3113 | flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_ENC|HTX_SL_F_XFER_LEN|HTX_SL_F_CHNK); |
| 3114 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), ist("200"), ist("OK")); |
| 3115 | if (!sl) |
Christopher Faulet | ef77922 | 2018-10-31 08:47:01 +0100 | [diff] [blame] | 3116 | goto full; |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 3117 | sl->info.res.status = 200; |
Christopher Faulet | ef77922 | 2018-10-31 08:47:01 +0100 | [diff] [blame] | 3118 | |
Christopher Faulet | b829f4c | 2019-03-29 16:13:55 +0100 | [diff] [blame] | 3119 | if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache"))) |
Christopher Faulet | ef77922 | 2018-10-31 08:47:01 +0100 | [diff] [blame] | 3120 | goto full; |
| 3121 | if (appctx->ctx.stats.flags & STAT_FMT_HTML) { |
| 3122 | if (!htx_add_header(htx, ist("Content-Type"), ist("text/html"))) |
| 3123 | goto full; |
| 3124 | } |
| 3125 | else { |
| 3126 | if (!htx_add_header(htx, ist("Content-Type"), ist("text/plain"))) |
| 3127 | goto full; |
| 3128 | } |
| 3129 | |
| 3130 | if (uri->refresh > 0 && !(appctx->ctx.stats.flags & STAT_NO_REFRESH)) { |
| 3131 | const char *refresh = U2A(uri->refresh); |
| 3132 | if (!htx_add_header(htx, ist("Refresh"), ist2(refresh, strlen(refresh)))) |
| 3133 | goto full; |
| 3134 | } |
| 3135 | |
| 3136 | if (appctx->ctx.stats.flags & STAT_CHUNKED) { |
| 3137 | if (!htx_add_header(htx, ist("Transfer-Encoding"), ist("chunked"))) |
| 3138 | goto full; |
| 3139 | } |
| 3140 | |
| 3141 | if (!htx_add_endof(htx, HTX_BLK_EOH)) |
| 3142 | goto full; |
| 3143 | |
Christopher Faulet | 1e2d636 | 2019-02-27 16:28:48 +0100 | [diff] [blame] | 3144 | channel_add_input(&s->res, htx->data); |
Christopher Faulet | ef77922 | 2018-10-31 08:47:01 +0100 | [diff] [blame] | 3145 | return 1; |
| 3146 | |
| 3147 | full: |
| 3148 | htx_reset(htx); |
| 3149 | si_rx_room_blk(si); |
| 3150 | return 0; |
William Lallemand | 74c24fb | 2016-11-21 17:18:36 +0100 | [diff] [blame] | 3151 | } |
| 3152 | |
Christopher Faulet | ef77922 | 2018-10-31 08:47:01 +0100 | [diff] [blame] | 3153 | |
| 3154 | static int stats_send_htx_redirect(struct stream_interface *si, struct htx *htx) |
| 3155 | { |
| 3156 | char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN]; |
| 3157 | struct stream *s = si_strm(si); |
| 3158 | struct uri_auth *uri = s->be->uri_auth; |
| 3159 | struct appctx *appctx = __objt_appctx(si->end); |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 3160 | struct htx_sl *sl; |
| 3161 | unsigned int flags; |
Christopher Faulet | ef77922 | 2018-10-31 08:47:01 +0100 | [diff] [blame] | 3162 | |
| 3163 | /* scope_txt = search pattern + search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */ |
| 3164 | scope_txt[0] = 0; |
| 3165 | if (appctx->ctx.stats.scope_len) { |
Christopher Faulet | ed7a066 | 2019-01-14 11:07:34 +0100 | [diff] [blame] | 3166 | const char *scope_ptr = stats_scope_ptr(appctx, si); |
| 3167 | |
Christopher Faulet | ef77922 | 2018-10-31 08:47:01 +0100 | [diff] [blame] | 3168 | strcpy(scope_txt, STAT_SCOPE_PATTERN); |
Christopher Faulet | ed7a066 | 2019-01-14 11:07:34 +0100 | [diff] [blame] | 3169 | memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), scope_ptr, appctx->ctx.stats.scope_len); |
Christopher Faulet | ef77922 | 2018-10-31 08:47:01 +0100 | [diff] [blame] | 3170 | scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0; |
| 3171 | } |
| 3172 | |
| 3173 | /* We don't want to land on the posted stats page because a refresh will |
| 3174 | * repost the data. We don't want this to happen on accident so we redirect |
| 3175 | * the browse to the stats page with a GET. |
| 3176 | */ |
| 3177 | chunk_printf(&trash, "%s;st=%s%s%s%s", |
| 3178 | uri->uri_prefix, |
| 3179 | ((appctx->ctx.stats.st_code > STAT_STATUS_INIT) && |
| 3180 | (appctx->ctx.stats.st_code < STAT_STATUS_SIZE) && |
| 3181 | stat_status_codes[appctx->ctx.stats.st_code]) ? |
| 3182 | stat_status_codes[appctx->ctx.stats.st_code] : |
| 3183 | stat_status_codes[STAT_STATUS_UNKN], |
| 3184 | (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "", |
| 3185 | (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "", |
| 3186 | scope_txt); |
| 3187 | |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 3188 | flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_CHNK); |
| 3189 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), ist("303"), ist("See Other")); |
| 3190 | if (!sl) |
Christopher Faulet | ef77922 | 2018-10-31 08:47:01 +0100 | [diff] [blame] | 3191 | goto full; |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 3192 | sl->info.res.status = 303; |
Christopher Faulet | ef77922 | 2018-10-31 08:47:01 +0100 | [diff] [blame] | 3193 | |
| 3194 | if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")) || |
Christopher Faulet | ef77922 | 2018-10-31 08:47:01 +0100 | [diff] [blame] | 3195 | !htx_add_header(htx, ist("Content-Type"), ist("text/plain")) || |
| 3196 | !htx_add_header(htx, ist("Content-Length"), ist("0")) || |
| 3197 | !htx_add_header(htx, ist("Location"), ist2(trash.area, trash.data))) |
| 3198 | goto full; |
| 3199 | |
| 3200 | if (!htx_add_endof(htx, HTX_BLK_EOH)) |
| 3201 | goto full; |
| 3202 | |
Christopher Faulet | 1e2d636 | 2019-02-27 16:28:48 +0100 | [diff] [blame] | 3203 | channel_add_input(&s->res, htx->data); |
Christopher Faulet | ef77922 | 2018-10-31 08:47:01 +0100 | [diff] [blame] | 3204 | return 1; |
| 3205 | |
| 3206 | full: |
| 3207 | htx_reset(htx); |
| 3208 | si_rx_room_blk(si); |
| 3209 | return 0; |
| 3210 | } |
William Lallemand | 74c24fb | 2016-11-21 17:18:36 +0100 | [diff] [blame] | 3211 | |
| 3212 | static int stats_send_http_headers(struct stream_interface *si) |
| 3213 | { |
| 3214 | struct stream *s = si_strm(si); |
| 3215 | struct uri_auth *uri = s->be->uri_auth; |
Willy Tarreau | 21ff2c4 | 2018-09-20 11:01:01 +0200 | [diff] [blame] | 3216 | struct appctx *appctx = __objt_appctx(si->end); |
William Lallemand | 74c24fb | 2016-11-21 17:18:36 +0100 | [diff] [blame] | 3217 | |
| 3218 | chunk_printf(&trash, |
| 3219 | "HTTP/1.1 200 OK\r\n" |
| 3220 | "Cache-Control: no-cache\r\n" |
| 3221 | "Connection: close\r\n" |
| 3222 | "Content-Type: %s\r\n", |
| 3223 | (appctx->ctx.stats.flags & STAT_FMT_HTML) ? "text/html" : "text/plain"); |
| 3224 | |
| 3225 | if (uri->refresh > 0 && !(appctx->ctx.stats.flags & STAT_NO_REFRESH)) |
| 3226 | chunk_appendf(&trash, "Refresh: %d\r\n", |
| 3227 | uri->refresh); |
| 3228 | |
| 3229 | /* we don't send the CRLF in chunked mode, it will be sent with the first chunk's size */ |
| 3230 | |
| 3231 | if (appctx->ctx.stats.flags & STAT_CHUNKED) |
| 3232 | chunk_appendf(&trash, "Transfer-Encoding: chunked\r\n"); |
| 3233 | else |
| 3234 | chunk_appendf(&trash, "\r\n"); |
| 3235 | |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3236 | if (ci_putchk(si_ic(si), &trash) == -1) { |
Willy Tarreau | db39843 | 2018-11-15 11:08:52 +0100 | [diff] [blame] | 3237 | si_rx_room_blk(si); |
William Lallemand | 74c24fb | 2016-11-21 17:18:36 +0100 | [diff] [blame] | 3238 | return 0; |
| 3239 | } |
| 3240 | |
| 3241 | return 1; |
| 3242 | } |
| 3243 | |
| 3244 | static int stats_send_http_redirect(struct stream_interface *si) |
| 3245 | { |
| 3246 | char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN]; |
| 3247 | struct stream *s = si_strm(si); |
| 3248 | struct uri_auth *uri = s->be->uri_auth; |
Willy Tarreau | 21ff2c4 | 2018-09-20 11:01:01 +0200 | [diff] [blame] | 3249 | struct appctx *appctx = __objt_appctx(si->end); |
William Lallemand | 74c24fb | 2016-11-21 17:18:36 +0100 | [diff] [blame] | 3250 | |
| 3251 | /* scope_txt = search pattern + search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */ |
| 3252 | scope_txt[0] = 0; |
| 3253 | if (appctx->ctx.stats.scope_len) { |
| 3254 | strcpy(scope_txt, STAT_SCOPE_PATTERN); |
Willy Tarreau | 89faf5d | 2018-06-07 18:16:48 +0200 | [diff] [blame] | 3255 | memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), co_head(si_oc(si)) + appctx->ctx.stats.scope_str, appctx->ctx.stats.scope_len); |
William Lallemand | 74c24fb | 2016-11-21 17:18:36 +0100 | [diff] [blame] | 3256 | scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0; |
| 3257 | } |
| 3258 | |
| 3259 | /* We don't want to land on the posted stats page because a refresh will |
| 3260 | * repost the data. We don't want this to happen on accident so we redirect |
| 3261 | * the browse to the stats page with a GET. |
| 3262 | */ |
| 3263 | chunk_printf(&trash, |
| 3264 | "HTTP/1.1 303 See Other\r\n" |
| 3265 | "Cache-Control: no-cache\r\n" |
| 3266 | "Content-Type: text/plain\r\n" |
| 3267 | "Connection: close\r\n" |
| 3268 | "Location: %s;st=%s%s%s%s\r\n" |
| 3269 | "Content-length: 0\r\n" |
| 3270 | "\r\n", |
| 3271 | uri->uri_prefix, |
| 3272 | ((appctx->ctx.stats.st_code > STAT_STATUS_INIT) && |
| 3273 | (appctx->ctx.stats.st_code < STAT_STATUS_SIZE) && |
| 3274 | stat_status_codes[appctx->ctx.stats.st_code]) ? |
| 3275 | stat_status_codes[appctx->ctx.stats.st_code] : |
| 3276 | stat_status_codes[STAT_STATUS_UNKN], |
| 3277 | (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "", |
| 3278 | (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "", |
| 3279 | scope_txt); |
| 3280 | |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3281 | if (ci_putchk(si_ic(si), &trash) == -1) { |
Willy Tarreau | db39843 | 2018-11-15 11:08:52 +0100 | [diff] [blame] | 3282 | si_rx_room_blk(si); |
William Lallemand | 74c24fb | 2016-11-21 17:18:36 +0100 | [diff] [blame] | 3283 | return 0; |
| 3284 | } |
| 3285 | |
| 3286 | return 1; |
| 3287 | } |
| 3288 | |
Simon Horman | 05ee213 | 2017-01-04 09:37:25 +0100 | [diff] [blame] | 3289 | |
William Lallemand | 74c24fb | 2016-11-21 17:18:36 +0100 | [diff] [blame] | 3290 | /* This I/O handler runs as an applet embedded in a stream interface. It is |
| 3291 | * used to send HTTP stats over a TCP socket. The mechanism is very simple. |
| 3292 | * appctx->st0 contains the operation in progress (dump, done). The handler |
| 3293 | * automatically unregisters itself once transfer is complete. |
| 3294 | */ |
Christopher Faulet | ef77922 | 2018-10-31 08:47:01 +0100 | [diff] [blame] | 3295 | static void htx_stats_io_handler(struct appctx *appctx) |
| 3296 | { |
| 3297 | struct stream_interface *si = appctx->owner; |
| 3298 | struct stream *s = si_strm(si); |
| 3299 | struct channel *req = si_oc(si); |
| 3300 | struct channel *res = si_ic(si); |
| 3301 | struct htx *req_htx, *res_htx; |
| 3302 | |
| 3303 | res_htx = htx_from_buf(&res->buf); |
| 3304 | |
| 3305 | if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) |
| 3306 | goto out; |
| 3307 | |
| 3308 | /* Check if the input buffer is avalaible. */ |
| 3309 | if (!b_size(&res->buf)) { |
| 3310 | si_rx_room_blk(si); |
| 3311 | goto out; |
| 3312 | } |
| 3313 | |
| 3314 | /* check that the output is not closed */ |
Christopher Faulet | 3a78aa6 | 2019-02-27 16:19:48 +0100 | [diff] [blame] | 3315 | if (res->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_SHUTR)) |
| 3316 | appctx->st0 = STAT_HTTP_END; |
Christopher Faulet | ef77922 | 2018-10-31 08:47:01 +0100 | [diff] [blame] | 3317 | |
| 3318 | /* all states are processed in sequence */ |
| 3319 | if (appctx->st0 == STAT_HTTP_HEAD) { |
| 3320 | if (stats_send_htx_headers(si, res_htx)) { |
| 3321 | if (s->txn->meth == HTTP_METH_HEAD) |
| 3322 | appctx->st0 = STAT_HTTP_DONE; |
| 3323 | else |
| 3324 | appctx->st0 = STAT_HTTP_DUMP; |
| 3325 | } |
| 3326 | } |
| 3327 | |
| 3328 | if (appctx->st0 == STAT_HTTP_DUMP) { |
| 3329 | if (stats_dump_stat_to_buffer(si, res_htx, s->be->uri_auth)) |
| 3330 | appctx->st0 = STAT_HTTP_DONE; |
| 3331 | } |
| 3332 | |
| 3333 | if (appctx->st0 == STAT_HTTP_POST) { |
| 3334 | if (stats_process_http_post(si)) |
| 3335 | appctx->st0 = STAT_HTTP_LAST; |
Christopher Faulet | 3a78aa6 | 2019-02-27 16:19:48 +0100 | [diff] [blame] | 3336 | else if (req->flags & CF_SHUTR) |
Christopher Faulet | ef77922 | 2018-10-31 08:47:01 +0100 | [diff] [blame] | 3337 | appctx->st0 = STAT_HTTP_DONE; |
| 3338 | } |
| 3339 | |
| 3340 | if (appctx->st0 == STAT_HTTP_LAST) { |
| 3341 | if (stats_send_htx_redirect(si, res_htx)) |
| 3342 | appctx->st0 = STAT_HTTP_DONE; |
| 3343 | } |
| 3344 | |
| 3345 | if (appctx->st0 == STAT_HTTP_DONE) { |
Christopher Faulet | b1b0821 | 2018-11-21 13:51:07 +0100 | [diff] [blame] | 3346 | /* Don't add EOD and TLR because mux-h1 will take care of it */ |
Christopher Faulet | ef77922 | 2018-10-31 08:47:01 +0100 | [diff] [blame] | 3347 | if (!htx_add_endof(res_htx, HTX_BLK_EOM)) { |
| 3348 | si_rx_room_blk(si); |
| 3349 | goto out; |
| 3350 | } |
Christopher Faulet | 3a78aa6 | 2019-02-27 16:19:48 +0100 | [diff] [blame] | 3351 | channel_add_input(&s->res, 1); |
| 3352 | appctx->st0 = STAT_HTTP_END; |
Christopher Faulet | ef77922 | 2018-10-31 08:47:01 +0100 | [diff] [blame] | 3353 | } |
| 3354 | |
Christopher Faulet | 3a78aa6 | 2019-02-27 16:19:48 +0100 | [diff] [blame] | 3355 | if (appctx->st0 == STAT_HTTP_END) { |
| 3356 | if (!(res->flags & CF_SHUTR)) { |
Christopher Faulet | ef77922 | 2018-10-31 08:47:01 +0100 | [diff] [blame] | 3357 | res->flags |= CF_READ_NULL; |
Christopher Faulet | 3a78aa6 | 2019-02-27 16:19:48 +0100 | [diff] [blame] | 3358 | si_shutr(si); |
| 3359 | } |
| 3360 | |
| 3361 | /* eat the whole request */ |
| 3362 | if (co_data(req)) { |
| 3363 | req_htx = htx_from_buf(&req->buf); |
| 3364 | co_htx_skip(req, req_htx, co_data(req)); |
| 3365 | htx_to_buf(req_htx, &req->buf); |
Christopher Faulet | ef77922 | 2018-10-31 08:47:01 +0100 | [diff] [blame] | 3366 | } |
| 3367 | } |
Christopher Faulet | 3a78aa6 | 2019-02-27 16:19:48 +0100 | [diff] [blame] | 3368 | |
Christopher Faulet | ef77922 | 2018-10-31 08:47:01 +0100 | [diff] [blame] | 3369 | out: |
| 3370 | /* we have left the request in the buffer for the case where we |
| 3371 | * process a POST, and this automatically re-enables activity on |
| 3372 | * read. It's better to indicate that we want to stop reading when |
| 3373 | * we're sending, so that we know there's at most one direction |
| 3374 | * deciding to wake the applet up. It saves it from looping when |
| 3375 | * emitting large blocks into small TCP windows. |
| 3376 | */ |
Christopher Faulet | 27ba2dc | 2018-12-05 11:53:24 +0100 | [diff] [blame] | 3377 | htx_to_buf(res_htx, &res->buf); |
| 3378 | if (!channel_is_empty(res)) |
Christopher Faulet | ef77922 | 2018-10-31 08:47:01 +0100 | [diff] [blame] | 3379 | si_stop_get(si); |
Christopher Faulet | ef77922 | 2018-10-31 08:47:01 +0100 | [diff] [blame] | 3380 | } |
| 3381 | |
| 3382 | |
| 3383 | /* This I/O handler runs as an applet embedded in a stream interface. It is |
| 3384 | * used to send HTTP stats over a TCP socket. The mechanism is very simple. |
| 3385 | * appctx->st0 contains the operation in progress (dump, done). The handler |
| 3386 | * automatically unregisters itself once transfer is complete. |
| 3387 | */ |
William Lallemand | 74c24fb | 2016-11-21 17:18:36 +0100 | [diff] [blame] | 3388 | static void http_stats_io_handler(struct appctx *appctx) |
| 3389 | { |
| 3390 | struct stream_interface *si = appctx->owner; |
| 3391 | struct stream *s = si_strm(si); |
| 3392 | struct channel *req = si_oc(si); |
| 3393 | struct channel *res = si_ic(si); |
| 3394 | |
Christopher Faulet | ef77922 | 2018-10-31 08:47:01 +0100 | [diff] [blame] | 3395 | if (IS_HTX_STRM(s)) |
| 3396 | return htx_stats_io_handler(appctx); |
| 3397 | |
William Lallemand | 74c24fb | 2016-11-21 17:18:36 +0100 | [diff] [blame] | 3398 | if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) |
| 3399 | goto out; |
| 3400 | |
Joseph Herlant | 29023ec | 2018-11-15 13:39:46 -0800 | [diff] [blame] | 3401 | /* Check if the input buffer is available. */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3402 | if (res->buf.size == 0) { |
Willy Tarreau | 4b962a4 | 2018-11-15 11:03:21 +0100 | [diff] [blame] | 3403 | /* already subscribed, we'll be called later once the buffer is |
| 3404 | * available. |
| 3405 | */ |
Christopher Faulet | a73e59b | 2016-12-09 17:30:18 +0100 | [diff] [blame] | 3406 | goto out; |
| 3407 | } |
| 3408 | |
William Lallemand | 74c24fb | 2016-11-21 17:18:36 +0100 | [diff] [blame] | 3409 | /* check that the output is not closed */ |
Christopher Faulet | 3a78aa6 | 2019-02-27 16:19:48 +0100 | [diff] [blame] | 3410 | if (res->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_SHUTR)) |
| 3411 | appctx->st0 = STAT_HTTP_END; |
William Lallemand | 74c24fb | 2016-11-21 17:18:36 +0100 | [diff] [blame] | 3412 | |
| 3413 | /* all states are processed in sequence */ |
| 3414 | if (appctx->st0 == STAT_HTTP_HEAD) { |
| 3415 | if (stats_send_http_headers(si)) { |
| 3416 | if (s->txn->meth == HTTP_METH_HEAD) |
| 3417 | appctx->st0 = STAT_HTTP_DONE; |
| 3418 | else |
| 3419 | appctx->st0 = STAT_HTTP_DUMP; |
| 3420 | } |
| 3421 | } |
| 3422 | |
| 3423 | if (appctx->st0 == STAT_HTTP_DUMP) { |
Willy Tarreau | 97f538b | 2018-06-15 19:41:31 +0200 | [diff] [blame] | 3424 | unsigned int prev_len = ci_data(si_ic(si)); |
William Lallemand | 74c24fb | 2016-11-21 17:18:36 +0100 | [diff] [blame] | 3425 | unsigned int data_len; |
| 3426 | unsigned int last_len; |
| 3427 | unsigned int last_fwd = 0; |
| 3428 | |
| 3429 | if (appctx->ctx.stats.flags & STAT_CHUNKED) { |
| 3430 | /* One difficulty we're facing is that we must prevent |
| 3431 | * the input data from being automatically forwarded to |
| 3432 | * the output area. For this, we temporarily disable |
| 3433 | * forwarding on the channel. |
| 3434 | */ |
| 3435 | last_fwd = si_ic(si)->to_forward; |
| 3436 | si_ic(si)->to_forward = 0; |
| 3437 | chunk_printf(&trash, "\r\n000000\r\n"); |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3438 | if (ci_putchk(si_ic(si), &trash) == -1) { |
Willy Tarreau | db39843 | 2018-11-15 11:08:52 +0100 | [diff] [blame] | 3439 | si_rx_room_blk(si); |
William Lallemand | 74c24fb | 2016-11-21 17:18:36 +0100 | [diff] [blame] | 3440 | si_ic(si)->to_forward = last_fwd; |
| 3441 | goto out; |
| 3442 | } |
| 3443 | } |
| 3444 | |
Willy Tarreau | 97f538b | 2018-06-15 19:41:31 +0200 | [diff] [blame] | 3445 | data_len = ci_data(si_ic(si)); |
Christopher Faulet | ef77922 | 2018-10-31 08:47:01 +0100 | [diff] [blame] | 3446 | if (stats_dump_stat_to_buffer(si, NULL, s->be->uri_auth)) |
William Lallemand | 74c24fb | 2016-11-21 17:18:36 +0100 | [diff] [blame] | 3447 | appctx->st0 = STAT_HTTP_DONE; |
| 3448 | |
Willy Tarreau | 97f538b | 2018-06-15 19:41:31 +0200 | [diff] [blame] | 3449 | last_len = ci_data(si_ic(si)); |
William Lallemand | 74c24fb | 2016-11-21 17:18:36 +0100 | [diff] [blame] | 3450 | |
| 3451 | /* Now we must either adjust or remove the chunk size. This is |
| 3452 | * not easy because the chunk size might wrap at the end of the |
| 3453 | * buffer, so we pretend we have nothing in the buffer, we write |
| 3454 | * the size, then restore the buffer's contents. Note that we can |
| 3455 | * only do that because no forwarding is scheduled on the stats |
| 3456 | * applet. |
| 3457 | */ |
| 3458 | if (appctx->ctx.stats.flags & STAT_CHUNKED) { |
| 3459 | si_ic(si)->total -= (last_len - prev_len); |
Willy Tarreau | 97f538b | 2018-06-15 19:41:31 +0200 | [diff] [blame] | 3460 | b_sub(si_ib(si), (last_len - prev_len)); |
William Lallemand | 74c24fb | 2016-11-21 17:18:36 +0100 | [diff] [blame] | 3461 | |
| 3462 | if (last_len != data_len) { |
| 3463 | chunk_printf(&trash, "\r\n%06x\r\n", (last_len - data_len)); |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3464 | if (ci_putchk(si_ic(si), &trash) == -1) |
Willy Tarreau | db39843 | 2018-11-15 11:08:52 +0100 | [diff] [blame] | 3465 | si_rx_room_blk(si); |
William Lallemand | 74c24fb | 2016-11-21 17:18:36 +0100 | [diff] [blame] | 3466 | |
| 3467 | si_ic(si)->total += (last_len - data_len); |
Willy Tarreau | 97f538b | 2018-06-15 19:41:31 +0200 | [diff] [blame] | 3468 | b_add(si_ib(si), last_len - data_len); |
William Lallemand | 74c24fb | 2016-11-21 17:18:36 +0100 | [diff] [blame] | 3469 | } |
| 3470 | /* now re-enable forwarding */ |
| 3471 | channel_forward(si_ic(si), last_fwd); |
| 3472 | } |
| 3473 | } |
| 3474 | |
| 3475 | if (appctx->st0 == STAT_HTTP_POST) { |
| 3476 | if (stats_process_http_post(si)) |
| 3477 | appctx->st0 = STAT_HTTP_LAST; |
| 3478 | else if (si_oc(si)->flags & CF_SHUTR) |
| 3479 | appctx->st0 = STAT_HTTP_DONE; |
| 3480 | } |
| 3481 | |
| 3482 | if (appctx->st0 == STAT_HTTP_LAST) { |
| 3483 | if (stats_send_http_redirect(si)) |
| 3484 | appctx->st0 = STAT_HTTP_DONE; |
| 3485 | } |
| 3486 | |
| 3487 | if (appctx->st0 == STAT_HTTP_DONE) { |
| 3488 | if (appctx->ctx.stats.flags & STAT_CHUNKED) { |
| 3489 | chunk_printf(&trash, "\r\n0\r\n\r\n"); |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3490 | if (ci_putchk(si_ic(si), &trash) == -1) { |
Willy Tarreau | db39843 | 2018-11-15 11:08:52 +0100 | [diff] [blame] | 3491 | si_rx_room_blk(si); |
William Lallemand | 74c24fb | 2016-11-21 17:18:36 +0100 | [diff] [blame] | 3492 | goto out; |
| 3493 | } |
| 3494 | } |
Christopher Faulet | 3a78aa6 | 2019-02-27 16:19:48 +0100 | [diff] [blame] | 3495 | appctx->st0 = STAT_HTTP_END; |
William Lallemand | 74c24fb | 2016-11-21 17:18:36 +0100 | [diff] [blame] | 3496 | } |
| 3497 | |
Christopher Faulet | 3a78aa6 | 2019-02-27 16:19:48 +0100 | [diff] [blame] | 3498 | if (appctx->st0 == STAT_HTTP_END) { |
| 3499 | if (!(res->flags & CF_SHUTR)) { |
William Lallemand | 74c24fb | 2016-11-21 17:18:36 +0100 | [diff] [blame] | 3500 | res->flags |= CF_READ_NULL; |
Christopher Faulet | 3a78aa6 | 2019-02-27 16:19:48 +0100 | [diff] [blame] | 3501 | si_shutr(si); |
William Lallemand | 74c24fb | 2016-11-21 17:18:36 +0100 | [diff] [blame] | 3502 | } |
Christopher Faulet | 3a78aa6 | 2019-02-27 16:19:48 +0100 | [diff] [blame] | 3503 | |
| 3504 | /* eat the whole request */ |
| 3505 | if (co_data(req)) |
| 3506 | co_skip(si_oc(si), co_data(si_oc(si))); |
William Lallemand | 74c24fb | 2016-11-21 17:18:36 +0100 | [diff] [blame] | 3507 | } |
Christopher Faulet | 3a78aa6 | 2019-02-27 16:19:48 +0100 | [diff] [blame] | 3508 | |
William Lallemand | 74c24fb | 2016-11-21 17:18:36 +0100 | [diff] [blame] | 3509 | out: |
Willy Tarreau | 055ba4f | 2018-07-24 17:05:54 +0200 | [diff] [blame] | 3510 | /* we have left the request in the buffer for the case where we |
| 3511 | * process a POST, and this automatically re-enables activity on |
| 3512 | * read. It's better to indicate that we want to stop reading when |
| 3513 | * we're sending, so that we know there's at most one direction |
| 3514 | * deciding to wake the applet up. It saves it from looping when |
| 3515 | * emitting large blocks into small TCP windows. |
| 3516 | */ |
| 3517 | if (!channel_is_empty(res)) |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 3518 | si_stop_get(si); |
William Lallemand | 74c24fb | 2016-11-21 17:18:36 +0100 | [diff] [blame] | 3519 | } |
| 3520 | |
Willy Tarreau | 0baac8c | 2016-11-22 16:36:53 +0100 | [diff] [blame] | 3521 | /* Dump all fields from <info> into <out> using the "show info" format (name: value) */ |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 3522 | static int stats_dump_info_fields(struct buffer *out, |
| 3523 | const struct field *info) |
Willy Tarreau | 0baac8c | 2016-11-22 16:36:53 +0100 | [diff] [blame] | 3524 | { |
| 3525 | int field; |
| 3526 | |
| 3527 | for (field = 0; field < INF_TOTAL_FIELDS; field++) { |
| 3528 | if (!field_format(info, field)) |
| 3529 | continue; |
| 3530 | |
| 3531 | if (!chunk_appendf(out, "%s: ", info_field_names[field])) |
| 3532 | return 0; |
| 3533 | if (!stats_emit_raw_data_field(out, &info[field])) |
| 3534 | return 0; |
| 3535 | if (!chunk_strcat(out, "\n")) |
| 3536 | return 0; |
| 3537 | } |
| 3538 | return 1; |
| 3539 | } |
| 3540 | |
| 3541 | /* Dump all fields from <info> into <out> using the "show info typed" format */ |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 3542 | static int stats_dump_typed_info_fields(struct buffer *out, |
| 3543 | const struct field *info) |
Willy Tarreau | 0baac8c | 2016-11-22 16:36:53 +0100 | [diff] [blame] | 3544 | { |
| 3545 | int field; |
| 3546 | |
| 3547 | for (field = 0; field < INF_TOTAL_FIELDS; field++) { |
| 3548 | if (!field_format(info, field)) |
| 3549 | continue; |
| 3550 | |
| 3551 | if (!chunk_appendf(out, "%d.%s.%u:", field, info_field_names[field], info[INF_PROCESS_NUM].u.u32)) |
| 3552 | return 0; |
| 3553 | if (!stats_emit_field_tags(out, &info[field], ':')) |
| 3554 | return 0; |
| 3555 | if (!stats_emit_typed_data_field(out, &info[field])) |
| 3556 | return 0; |
| 3557 | if (!chunk_strcat(out, "\n")) |
| 3558 | return 0; |
| 3559 | } |
| 3560 | return 1; |
| 3561 | } |
| 3562 | |
| 3563 | /* Fill <info> with HAProxy global info. <info> is preallocated |
| 3564 | * array of length <len>. The length of the aray must be |
| 3565 | * INF_TOTAL_FIELDS. If this length is less then this value, the |
| 3566 | * function returns 0, otherwise, it returns 1. |
| 3567 | */ |
| 3568 | int stats_fill_info(struct field *info, int len) |
| 3569 | { |
| 3570 | unsigned int up = (now.tv_sec - start_date.tv_sec); |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 3571 | struct buffer *out = get_trash_chunk(); |
Willy Tarreau | 0baac8c | 2016-11-22 16:36:53 +0100 | [diff] [blame] | 3572 | |
| 3573 | #ifdef USE_OPENSSL |
| 3574 | int ssl_sess_rate = read_freq_ctr(&global.ssl_per_sec); |
| 3575 | int ssl_key_rate = read_freq_ctr(&global.ssl_fe_keys_per_sec); |
| 3576 | int ssl_reuse = 0; |
| 3577 | |
| 3578 | if (ssl_key_rate < ssl_sess_rate) { |
| 3579 | /* count the ssl reuse ratio and avoid overflows in both directions */ |
| 3580 | ssl_reuse = 100 - (100 * ssl_key_rate + (ssl_sess_rate - 1) / 2) / ssl_sess_rate; |
| 3581 | } |
| 3582 | #endif |
| 3583 | |
| 3584 | if (len < INF_TOTAL_FIELDS) |
| 3585 | return 0; |
| 3586 | |
| 3587 | chunk_reset(out); |
| 3588 | memset(info, 0, sizeof(*info) * len); |
| 3589 | |
| 3590 | info[INF_NAME] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, PRODUCT_NAME); |
Willy Tarreau | 909b9d8 | 2019-01-04 18:20:32 +0100 | [diff] [blame] | 3591 | info[INF_VERSION] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, haproxy_version); |
| 3592 | info[INF_RELEASE_DATE] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, haproxy_date); |
Willy Tarreau | 0baac8c | 2016-11-22 16:36:53 +0100 | [diff] [blame] | 3593 | |
Yves Lafon | 9531728 | 2018-02-26 11:10:37 +0100 | [diff] [blame] | 3594 | info[INF_NBTHREAD] = mkf_u32(FO_CONFIG|FS_SERVICE, global.nbthread); |
Willy Tarreau | 0baac8c | 2016-11-22 16:36:53 +0100 | [diff] [blame] | 3595 | info[INF_NBPROC] = mkf_u32(FO_CONFIG|FS_SERVICE, global.nbproc); |
| 3596 | info[INF_PROCESS_NUM] = mkf_u32(FO_KEY, relative_pid); |
| 3597 | info[INF_PID] = mkf_u32(FO_STATUS, pid); |
| 3598 | |
| 3599 | info[INF_UPTIME] = mkf_str(FN_DURATION, chunk_newstr(out)); |
| 3600 | chunk_appendf(out, "%ud %uh%02um%02us", up / 86400, (up % 86400) / 3600, (up % 3600) / 60, (up % 60)); |
| 3601 | |
| 3602 | info[INF_UPTIME_SEC] = mkf_u32(FN_DURATION, up); |
| 3603 | info[INF_MEMMAX_MB] = mkf_u32(FO_CONFIG|FN_LIMIT, global.rlimit_memmax); |
| 3604 | info[INF_POOL_ALLOC_MB] = mkf_u32(0, (unsigned)(pool_total_allocated() / 1048576L)); |
| 3605 | info[INF_POOL_USED_MB] = mkf_u32(0, (unsigned)(pool_total_used() / 1048576L)); |
| 3606 | info[INF_POOL_FAILED] = mkf_u32(FN_COUNTER, pool_total_failures()); |
| 3607 | info[INF_ULIMIT_N] = mkf_u32(FO_CONFIG|FN_LIMIT, global.rlimit_nofile); |
| 3608 | info[INF_MAXSOCK] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxsock); |
| 3609 | info[INF_MAXCONN] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxconn); |
| 3610 | info[INF_HARD_MAXCONN] = mkf_u32(FO_CONFIG|FN_LIMIT, global.hardmaxconn); |
| 3611 | info[INF_CURR_CONN] = mkf_u32(0, actconn); |
| 3612 | info[INF_CUM_CONN] = mkf_u32(FN_COUNTER, totalconn); |
| 3613 | info[INF_CUM_REQ] = mkf_u32(FN_COUNTER, global.req_count); |
| 3614 | #ifdef USE_OPENSSL |
| 3615 | info[INF_MAX_SSL_CONNS] = mkf_u32(FN_MAX, global.maxsslconn); |
| 3616 | info[INF_CURR_SSL_CONNS] = mkf_u32(0, sslconns); |
| 3617 | info[INF_CUM_SSL_CONNS] = mkf_u32(FN_COUNTER, totalsslconns); |
| 3618 | #endif |
| 3619 | info[INF_MAXPIPES] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxpipes); |
| 3620 | info[INF_PIPES_USED] = mkf_u32(0, pipes_used); |
| 3621 | info[INF_PIPES_FREE] = mkf_u32(0, pipes_free); |
| 3622 | info[INF_CONN_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&global.conn_per_sec)); |
| 3623 | info[INF_CONN_RATE_LIMIT] = mkf_u32(FO_CONFIG|FN_LIMIT, global.cps_lim); |
| 3624 | info[INF_MAX_CONN_RATE] = mkf_u32(FN_MAX, global.cps_max); |
| 3625 | info[INF_SESS_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&global.sess_per_sec)); |
| 3626 | info[INF_SESS_RATE_LIMIT] = mkf_u32(FO_CONFIG|FN_LIMIT, global.sps_lim); |
| 3627 | info[INF_MAX_SESS_RATE] = mkf_u32(FN_RATE, global.sps_max); |
| 3628 | |
| 3629 | #ifdef USE_OPENSSL |
| 3630 | info[INF_SSL_RATE] = mkf_u32(FN_RATE, ssl_sess_rate); |
| 3631 | info[INF_SSL_RATE_LIMIT] = mkf_u32(FO_CONFIG|FN_LIMIT, global.ssl_lim); |
| 3632 | info[INF_MAX_SSL_RATE] = mkf_u32(FN_MAX, global.ssl_max); |
| 3633 | info[INF_SSL_FRONTEND_KEY_RATE] = mkf_u32(0, ssl_key_rate); |
| 3634 | info[INF_SSL_FRONTEND_MAX_KEY_RATE] = mkf_u32(FN_MAX, global.ssl_fe_keys_max); |
| 3635 | info[INF_SSL_FRONTEND_SESSION_REUSE_PCT] = mkf_u32(0, ssl_reuse); |
| 3636 | info[INF_SSL_BACKEND_KEY_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&global.ssl_be_keys_per_sec)); |
| 3637 | info[INF_SSL_BACKEND_MAX_KEY_RATE] = mkf_u32(FN_MAX, global.ssl_be_keys_max); |
| 3638 | info[INF_SSL_CACHE_LOOKUPS] = mkf_u32(FN_COUNTER, global.shctx_lookups); |
| 3639 | info[INF_SSL_CACHE_MISSES] = mkf_u32(FN_COUNTER, global.shctx_misses); |
| 3640 | #endif |
| 3641 | info[INF_COMPRESS_BPS_IN] = mkf_u32(FN_RATE, read_freq_ctr(&global.comp_bps_in)); |
| 3642 | info[INF_COMPRESS_BPS_OUT] = mkf_u32(FN_RATE, read_freq_ctr(&global.comp_bps_out)); |
| 3643 | info[INF_COMPRESS_BPS_RATE_LIM] = mkf_u32(FO_CONFIG|FN_LIMIT, global.comp_rate_lim); |
| 3644 | #ifdef USE_ZLIB |
| 3645 | info[INF_ZLIB_MEM_USAGE] = mkf_u32(0, zlib_used_memory); |
| 3646 | info[INF_MAX_ZLIB_MEM_USAGE] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxzlibmem); |
| 3647 | #endif |
| 3648 | info[INF_TASKS] = mkf_u32(0, nb_tasks_cur); |
Christopher Faulet | 34c5cc9 | 2016-12-06 09:15:30 +0100 | [diff] [blame] | 3649 | info[INF_RUN_QUEUE] = mkf_u32(0, tasks_run_queue_cur); |
Willy Tarreau | 0baac8c | 2016-11-22 16:36:53 +0100 | [diff] [blame] | 3650 | info[INF_IDLE_PCT] = mkf_u32(FN_AVG, idle_pct); |
| 3651 | info[INF_NODE] = mkf_str(FO_CONFIG|FN_OUTPUT|FS_SERVICE, global.node); |
| 3652 | if (global.desc) |
| 3653 | info[INF_DESCRIPTION] = mkf_str(FO_CONFIG|FN_OUTPUT|FS_SERVICE, global.desc); |
Willy Tarreau | 00098ea | 2018-11-05 14:38:13 +0100 | [diff] [blame] | 3654 | info[INF_STOPPING] = mkf_u32(0, stopping); |
| 3655 | info[INF_JOBS] = mkf_u32(0, jobs); |
William Lallemand | a719926 | 2018-11-16 16:57:20 +0100 | [diff] [blame] | 3656 | info[INF_UNSTOPPABLE_JOBS] = mkf_u32(0, unstoppable_jobs); |
Willy Tarreau | 00098ea | 2018-11-05 14:38:13 +0100 | [diff] [blame] | 3657 | info[INF_LISTENERS] = mkf_u32(0, listeners); |
Willy Tarreau | 199ad24 | 2018-11-05 16:31:22 +0100 | [diff] [blame] | 3658 | info[INF_ACTIVE_PEERS] = mkf_u32(0, active_peers); |
Willy Tarreau | 2d372c2 | 2018-11-05 17:12:27 +0100 | [diff] [blame] | 3659 | info[INF_CONNECTED_PEERS] = mkf_u32(0, connected_peers); |
Willy Tarreau | 13ef773 | 2018-11-12 07:25:28 +0100 | [diff] [blame] | 3660 | info[INF_DROPPED_LOGS] = mkf_u32(0, dropped_logs); |
Willy Tarreau | beb859a | 2018-11-22 18:07:59 +0100 | [diff] [blame] | 3661 | info[INF_BUSY_POLLING] = mkf_u32(0, !!(global.tune.options & GTUNE_BUSY_POLLING)); |
Baptiste Assmann | 333939c | 2019-01-21 08:34:50 +0100 | [diff] [blame] | 3662 | info[INF_FAILED_RESOLUTIONS] = mkf_u32(0, dns_failed_resolutions); |
Willy Tarreau | 0baac8c | 2016-11-22 16:36:53 +0100 | [diff] [blame] | 3663 | |
| 3664 | return 1; |
| 3665 | } |
| 3666 | |
| 3667 | /* This function dumps information onto the stream interface's read buffer. |
| 3668 | * It returns 0 as long as it does not complete, non-zero upon completion. |
| 3669 | * No state is used. |
| 3670 | */ |
| 3671 | static int stats_dump_info_to_buffer(struct stream_interface *si) |
| 3672 | { |
| 3673 | struct appctx *appctx = __objt_appctx(si->end); |
| 3674 | |
| 3675 | if (!stats_fill_info(info, INF_TOTAL_FIELDS)) |
| 3676 | return 0; |
| 3677 | |
| 3678 | chunk_reset(&trash); |
| 3679 | |
| 3680 | if (appctx->ctx.stats.flags & STAT_FMT_TYPED) |
| 3681 | stats_dump_typed_info_fields(&trash, info); |
Simon Horman | 05ee213 | 2017-01-04 09:37:25 +0100 | [diff] [blame] | 3682 | else if (appctx->ctx.stats.flags & STAT_FMT_JSON) |
| 3683 | stats_dump_json_info_fields(&trash, info); |
Willy Tarreau | 0baac8c | 2016-11-22 16:36:53 +0100 | [diff] [blame] | 3684 | else |
| 3685 | stats_dump_info_fields(&trash, info); |
| 3686 | |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3687 | if (ci_putchk(si_ic(si), &trash) == -1) { |
Willy Tarreau | db39843 | 2018-11-15 11:08:52 +0100 | [diff] [blame] | 3688 | si_rx_room_blk(si); |
Willy Tarreau | 0baac8c | 2016-11-22 16:36:53 +0100 | [diff] [blame] | 3689 | return 0; |
| 3690 | } |
| 3691 | |
| 3692 | return 1; |
| 3693 | } |
| 3694 | |
Simon Horman | 6f6bb38 | 2017-01-04 09:37:26 +0100 | [diff] [blame] | 3695 | /* This function dumps the schema onto the stream interface's read buffer. |
| 3696 | * It returns 0 as long as it does not complete, non-zero upon completion. |
| 3697 | * No state is used. |
| 3698 | * |
| 3699 | * Integer values bouned to the range [-(2**53)+1, (2**53)-1] as |
| 3700 | * per the recommendation for interoperable integers in section 6 of RFC 7159. |
| 3701 | */ |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 3702 | static void stats_dump_json_schema(struct buffer *out) |
Simon Horman | 6f6bb38 | 2017-01-04 09:37:26 +0100 | [diff] [blame] | 3703 | { |
| 3704 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3705 | int old_len = out->data; |
Simon Horman | 6f6bb38 | 2017-01-04 09:37:26 +0100 | [diff] [blame] | 3706 | |
| 3707 | chunk_strcat(out, |
| 3708 | "{" |
| 3709 | "\"$schema\":\"http://json-schema.org/draft-04/schema#\"," |
| 3710 | "\"oneOf\":[" |
| 3711 | "{" |
| 3712 | "\"title\":\"Info\"," |
| 3713 | "\"type\":\"array\"," |
| 3714 | "\"items\":{" |
| 3715 | "\"properties\":{" |
| 3716 | "\"title\":\"InfoItem\"," |
| 3717 | "\"type\":\"object\"," |
| 3718 | "\"field\":{\"$ref\":\"#/definitions/field\"}," |
| 3719 | "\"processNum\":{\"$ref\":\"#/definitions/processNum\"}," |
| 3720 | "\"tags\":{\"$ref\":\"#/definitions/tags\"}," |
| 3721 | "\"value\":{\"$ref\":\"#/definitions/typedValue\"}" |
| 3722 | "}," |
| 3723 | "\"required\":[\"field\",\"processNum\",\"tags\"," |
| 3724 | "\"value\"]" |
| 3725 | "}" |
| 3726 | "}," |
| 3727 | "{" |
| 3728 | "\"title\":\"Stat\"," |
| 3729 | "\"type\":\"array\"," |
| 3730 | "\"items\":{" |
| 3731 | "\"title\":\"InfoItem\"," |
| 3732 | "\"type\":\"object\"," |
| 3733 | "\"properties\":{" |
| 3734 | "\"objType\":{" |
| 3735 | "\"enum\":[\"Frontend\",\"Backend\",\"Listener\"," |
| 3736 | "\"Server\",\"Unknown\"]" |
| 3737 | "}," |
| 3738 | "\"proxyId\":{" |
| 3739 | "\"type\":\"integer\"," |
| 3740 | "\"minimum\":0" |
| 3741 | "}," |
| 3742 | "\"id\":{" |
| 3743 | "\"type\":\"integer\"," |
| 3744 | "\"minimum\":0" |
| 3745 | "}," |
| 3746 | "\"field\":{\"$ref\":\"#/definitions/field\"}," |
| 3747 | "\"processNum\":{\"$ref\":\"#/definitions/processNum\"}," |
| 3748 | "\"tags\":{\"$ref\":\"#/definitions/tags\"}," |
| 3749 | "\"typedValue\":{\"$ref\":\"#/definitions/typedValue\"}" |
| 3750 | "}," |
| 3751 | "\"required\":[\"objType\",\"proxyId\",\"id\"," |
| 3752 | "\"field\",\"processNum\",\"tags\"," |
| 3753 | "\"value\"]" |
| 3754 | "}" |
| 3755 | "}," |
| 3756 | "{" |
| 3757 | "\"title\":\"Error\"," |
| 3758 | "\"type\":\"object\"," |
| 3759 | "\"properties\":{" |
| 3760 | "\"errorStr\":{" |
| 3761 | "\"type\":\"string\"" |
| 3762 | "}," |
| 3763 | "\"required\":[\"errorStr\"]" |
| 3764 | "}" |
| 3765 | "}" |
| 3766 | "]," |
| 3767 | "\"definitions\":{" |
| 3768 | "\"field\":{" |
| 3769 | "\"type\":\"object\"," |
| 3770 | "\"pos\":{" |
| 3771 | "\"type\":\"integer\"," |
| 3772 | "\"minimum\":0" |
| 3773 | "}," |
| 3774 | "\"name\":{" |
| 3775 | "\"type\":\"string\"" |
| 3776 | "}," |
| 3777 | "\"required\":[\"pos\",\"name\"]" |
| 3778 | "}," |
| 3779 | "\"processNum\":{" |
| 3780 | "\"type\":\"integer\"," |
| 3781 | "\"minimum\":1" |
| 3782 | "}," |
| 3783 | "\"tags\":{" |
| 3784 | "\"type\":\"object\"," |
| 3785 | "\"origin\":{" |
| 3786 | "\"type\":\"string\"," |
| 3787 | "\"enum\":[\"Metric\",\"Status\",\"Key\"," |
| 3788 | "\"Config\",\"Product\",\"Unknown\"]" |
| 3789 | "}," |
| 3790 | "\"nature\":{" |
| 3791 | "\"type\":\"string\"," |
| 3792 | "\"enum\":[\"Gauge\",\"Limit\",\"Min\",\"Max\"," |
| 3793 | "\"Rate\",\"Counter\",\"Duration\"," |
| 3794 | "\"Age\",\"Time\",\"Name\",\"Output\"," |
| 3795 | "\"Avg\", \"Unknown\"]" |
| 3796 | "}," |
| 3797 | "\"scope\":{" |
| 3798 | "\"type\":\"string\"," |
| 3799 | "\"enum\":[\"Cluster\",\"Process\",\"Service\"," |
| 3800 | "\"System\",\"Unknown\"]" |
| 3801 | "}," |
| 3802 | "\"required\":[\"origin\",\"nature\",\"scope\"]" |
| 3803 | "}," |
| 3804 | "\"typedValue\":{" |
| 3805 | "\"type\":\"object\"," |
| 3806 | "\"oneOf\":[" |
| 3807 | "{\"$ref\":\"#/definitions/typedValue/definitions/s32Value\"}," |
| 3808 | "{\"$ref\":\"#/definitions/typedValue/definitions/s64Value\"}," |
| 3809 | "{\"$ref\":\"#/definitions/typedValue/definitions/u32Value\"}," |
| 3810 | "{\"$ref\":\"#/definitions/typedValue/definitions/u64Value\"}," |
| 3811 | "{\"$ref\":\"#/definitions/typedValue/definitions/strValue\"}" |
| 3812 | "]," |
| 3813 | "\"definitions\":{" |
| 3814 | "\"s32Value\":{" |
| 3815 | "\"properties\":{" |
| 3816 | "\"type\":{" |
| 3817 | "\"type\":\"string\"," |
| 3818 | "\"enum\":[\"s32\"]" |
| 3819 | "}," |
| 3820 | "\"value\":{" |
| 3821 | "\"type\":\"integer\"," |
| 3822 | "\"minimum\":-2147483648," |
| 3823 | "\"maximum\":2147483647" |
| 3824 | "}" |
| 3825 | "}," |
| 3826 | "\"required\":[\"type\",\"value\"]" |
| 3827 | "}," |
| 3828 | "\"s64Value\":{" |
| 3829 | "\"properties\":{" |
| 3830 | "\"type\":{" |
| 3831 | "\"type\":\"string\"," |
| 3832 | "\"enum\":[\"s64\"]" |
| 3833 | "}," |
| 3834 | "\"value\":{" |
| 3835 | "\"type\":\"integer\"," |
| 3836 | "\"minimum\":-9007199254740991," |
| 3837 | "\"maximum\":9007199254740991" |
| 3838 | "}" |
| 3839 | "}," |
| 3840 | "\"required\":[\"type\",\"value\"]" |
| 3841 | "}," |
| 3842 | "\"u32Value\":{" |
| 3843 | "\"properties\":{" |
| 3844 | "\"type\":{" |
| 3845 | "\"type\":\"string\"," |
| 3846 | "\"enum\":[\"u32\"]" |
| 3847 | "}," |
| 3848 | "\"value\":{" |
| 3849 | "\"type\":\"integer\"," |
| 3850 | "\"minimum\":0," |
| 3851 | "\"maximum\":4294967295" |
| 3852 | "}" |
| 3853 | "}," |
| 3854 | "\"required\":[\"type\",\"value\"]" |
| 3855 | "}," |
| 3856 | "\"u64Value\":{" |
| 3857 | "\"properties\":{" |
| 3858 | "\"type\":{" |
| 3859 | "\"type\":\"string\"," |
| 3860 | "\"enum\":[\"u64\"]" |
| 3861 | "}," |
| 3862 | "\"value\":{" |
| 3863 | "\"type\":\"integer\"," |
| 3864 | "\"minimum\":0," |
| 3865 | "\"maximum\":9007199254740991" |
| 3866 | "}" |
| 3867 | "}," |
| 3868 | "\"required\":[\"type\",\"value\"]" |
| 3869 | "}," |
| 3870 | "\"strValue\":{" |
| 3871 | "\"properties\":{" |
| 3872 | "\"type\":{" |
| 3873 | "\"type\":\"string\"," |
| 3874 | "\"enum\":[\"str\"]" |
| 3875 | "}," |
| 3876 | "\"value\":{\"type\":\"string\"}" |
| 3877 | "}," |
| 3878 | "\"required\":[\"type\",\"value\"]" |
| 3879 | "}," |
| 3880 | "\"unknownValue\":{" |
| 3881 | "\"properties\":{" |
| 3882 | "\"type\":{" |
| 3883 | "\"type\":\"integer\"," |
| 3884 | "\"minimum\":0" |
| 3885 | "}," |
| 3886 | "\"value\":{" |
| 3887 | "\"type\":\"string\"," |
| 3888 | "\"enum\":[\"unknown\"]" |
| 3889 | "}" |
| 3890 | "}," |
| 3891 | "\"required\":[\"type\",\"value\"]" |
| 3892 | "}" |
| 3893 | "}" |
| 3894 | "}" |
| 3895 | "}" |
| 3896 | "}"); |
| 3897 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3898 | if (old_len == out->data) { |
Simon Horman | 6f6bb38 | 2017-01-04 09:37:26 +0100 | [diff] [blame] | 3899 | chunk_reset(out); |
| 3900 | chunk_appendf(out, |
| 3901 | "{\"errorStr\":\"output buffer too short\"}"); |
| 3902 | } |
| 3903 | } |
| 3904 | |
| 3905 | /* This function dumps the schema onto the stream interface's read buffer. |
| 3906 | * It returns 0 as long as it does not complete, non-zero upon completion. |
| 3907 | * No state is used. |
| 3908 | */ |
| 3909 | static int stats_dump_json_schema_to_buffer(struct stream_interface *si) |
| 3910 | { |
| 3911 | chunk_reset(&trash); |
| 3912 | |
| 3913 | stats_dump_json_schema(&trash); |
| 3914 | |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3915 | if (ci_putchk(si_ic(si), &trash) == -1) { |
Willy Tarreau | db39843 | 2018-11-15 11:08:52 +0100 | [diff] [blame] | 3916 | si_rx_room_blk(si); |
Simon Horman | 6f6bb38 | 2017-01-04 09:37:26 +0100 | [diff] [blame] | 3917 | return 0; |
| 3918 | } |
| 3919 | |
| 3920 | return 1; |
| 3921 | } |
| 3922 | |
Aurélien Nephtali | abbf607 | 2018-04-18 13:26:46 +0200 | [diff] [blame] | 3923 | static int cli_parse_clear_counters(char **args, char *payload, struct appctx *appctx, void *private) |
Willy Tarreau | 89d467c | 2016-11-23 11:02:40 +0100 | [diff] [blame] | 3924 | { |
| 3925 | struct proxy *px; |
| 3926 | struct server *sv; |
| 3927 | struct listener *li; |
| 3928 | int clrall = 0; |
| 3929 | |
| 3930 | if (strcmp(args[2], "all") == 0) |
| 3931 | clrall = 1; |
| 3932 | |
| 3933 | /* check permissions */ |
| 3934 | if (!cli_has_level(appctx, ACCESS_LVL_OPER) || |
| 3935 | (clrall && !cli_has_level(appctx, ACCESS_LVL_ADMIN))) |
| 3936 | return 1; |
| 3937 | |
Olivier Houchard | fbc74e8 | 2017-11-24 16:54:05 +0100 | [diff] [blame] | 3938 | for (px = proxies_list; px; px = px->next) { |
Willy Tarreau | 89d467c | 2016-11-23 11:02:40 +0100 | [diff] [blame] | 3939 | if (clrall) { |
| 3940 | memset(&px->be_counters, 0, sizeof(px->be_counters)); |
| 3941 | memset(&px->fe_counters, 0, sizeof(px->fe_counters)); |
| 3942 | } |
| 3943 | else { |
| 3944 | px->be_counters.conn_max = 0; |
| 3945 | px->be_counters.p.http.rps_max = 0; |
| 3946 | px->be_counters.sps_max = 0; |
| 3947 | px->be_counters.cps_max = 0; |
| 3948 | px->be_counters.nbpend_max = 0; |
| 3949 | |
| 3950 | px->fe_counters.conn_max = 0; |
| 3951 | px->fe_counters.p.http.rps_max = 0; |
| 3952 | px->fe_counters.sps_max = 0; |
| 3953 | px->fe_counters.cps_max = 0; |
Willy Tarreau | 89d467c | 2016-11-23 11:02:40 +0100 | [diff] [blame] | 3954 | } |
| 3955 | |
| 3956 | for (sv = px->srv; sv; sv = sv->next) |
| 3957 | if (clrall) |
| 3958 | memset(&sv->counters, 0, sizeof(sv->counters)); |
| 3959 | else { |
| 3960 | sv->counters.cur_sess_max = 0; |
| 3961 | sv->counters.nbpend_max = 0; |
| 3962 | sv->counters.sps_max = 0; |
| 3963 | } |
| 3964 | |
| 3965 | list_for_each_entry(li, &px->conf.listeners, by_fe) |
| 3966 | if (li->counters) { |
| 3967 | if (clrall) |
| 3968 | memset(li->counters, 0, sizeof(*li->counters)); |
| 3969 | else |
| 3970 | li->counters->conn_max = 0; |
| 3971 | } |
| 3972 | } |
| 3973 | |
| 3974 | global.cps_max = 0; |
| 3975 | global.sps_max = 0; |
Olivier Houchard | 00bc3cb | 2017-10-17 19:23:25 +0200 | [diff] [blame] | 3976 | global.ssl_max = 0; |
| 3977 | global.ssl_fe_keys_max = 0; |
| 3978 | global.ssl_be_keys_max = 0; |
Willy Tarreau | d80cb4e | 2018-01-20 19:30:13 +0100 | [diff] [blame] | 3979 | |
| 3980 | memset(activity, 0, sizeof(activity)); |
Willy Tarreau | 89d467c | 2016-11-23 11:02:40 +0100 | [diff] [blame] | 3981 | return 1; |
| 3982 | } |
| 3983 | |
| 3984 | |
Aurélien Nephtali | abbf607 | 2018-04-18 13:26:46 +0200 | [diff] [blame] | 3985 | static int cli_parse_show_info(char **args, char *payload, struct appctx *appctx, void *private) |
Willy Tarreau | 0baac8c | 2016-11-22 16:36:53 +0100 | [diff] [blame] | 3986 | { |
Willy Tarreau | d25fc79 | 2016-12-16 12:33:47 +0100 | [diff] [blame] | 3987 | appctx->ctx.stats.scope_str = 0; |
| 3988 | appctx->ctx.stats.scope_len = 0; |
| 3989 | appctx->ctx.stats.flags = 0; |
| 3990 | |
Willy Tarreau | 0baac8c | 2016-11-22 16:36:53 +0100 | [diff] [blame] | 3991 | if (strcmp(args[2], "typed") == 0) |
| 3992 | appctx->ctx.stats.flags |= STAT_FMT_TYPED; |
Simon Horman | 05ee213 | 2017-01-04 09:37:25 +0100 | [diff] [blame] | 3993 | else if (strcmp(args[2], "json") == 0) |
| 3994 | appctx->ctx.stats.flags |= STAT_FMT_JSON; |
Willy Tarreau | 0baac8c | 2016-11-22 16:36:53 +0100 | [diff] [blame] | 3995 | return 0; |
| 3996 | } |
| 3997 | |
| 3998 | |
Aurélien Nephtali | abbf607 | 2018-04-18 13:26:46 +0200 | [diff] [blame] | 3999 | static int cli_parse_show_stat(char **args, char *payload, struct appctx *appctx, void *private) |
Willy Tarreau | 2b812e2 | 2016-11-22 16:18:05 +0100 | [diff] [blame] | 4000 | { |
Willy Tarreau | d25fc79 | 2016-12-16 12:33:47 +0100 | [diff] [blame] | 4001 | appctx->ctx.stats.scope_str = 0; |
| 4002 | appctx->ctx.stats.scope_len = 0; |
| 4003 | appctx->ctx.stats.flags = 0; |
| 4004 | |
Willy Tarreau | 2b812e2 | 2016-11-22 16:18:05 +0100 | [diff] [blame] | 4005 | if (*args[2] && *args[3] && *args[4]) { |
Willy Tarreau | a1b1ed5 | 2016-11-25 08:50:58 +0100 | [diff] [blame] | 4006 | struct proxy *px; |
| 4007 | |
| 4008 | px = proxy_find_by_name(args[2], 0, 0); |
| 4009 | if (px) |
| 4010 | appctx->ctx.stats.iid = px->uuid; |
| 4011 | else |
| 4012 | appctx->ctx.stats.iid = atoi(args[2]); |
| 4013 | |
| 4014 | if (!appctx->ctx.stats.iid) { |
Andjelko Iharos | c3680ec | 2017-07-20 16:49:14 +0200 | [diff] [blame] | 4015 | appctx->ctx.cli.severity = LOG_ERR; |
Willy Tarreau | a1b1ed5 | 2016-11-25 08:50:58 +0100 | [diff] [blame] | 4016 | appctx->ctx.cli.msg = "No such proxy.\n"; |
| 4017 | appctx->st0 = CLI_ST_PRINT; |
| 4018 | return 1; |
| 4019 | } |
| 4020 | |
Willy Tarreau | 2b812e2 | 2016-11-22 16:18:05 +0100 | [diff] [blame] | 4021 | appctx->ctx.stats.flags |= STAT_BOUND; |
Willy Tarreau | 2b812e2 | 2016-11-22 16:18:05 +0100 | [diff] [blame] | 4022 | appctx->ctx.stats.type = atoi(args[3]); |
| 4023 | appctx->ctx.stats.sid = atoi(args[4]); |
| 4024 | if (strcmp(args[5], "typed") == 0) |
| 4025 | appctx->ctx.stats.flags |= STAT_FMT_TYPED; |
Simon Horman | 05ee213 | 2017-01-04 09:37:25 +0100 | [diff] [blame] | 4026 | else if (strcmp(args[5], "json") == 0) |
| 4027 | appctx->ctx.stats.flags |= STAT_FMT_JSON; |
Willy Tarreau | 2b812e2 | 2016-11-22 16:18:05 +0100 | [diff] [blame] | 4028 | } |
| 4029 | else if (strcmp(args[2], "typed") == 0) |
| 4030 | appctx->ctx.stats.flags |= STAT_FMT_TYPED; |
Simon Horman | 05ee213 | 2017-01-04 09:37:25 +0100 | [diff] [blame] | 4031 | else if (strcmp(args[2], "json") == 0) |
| 4032 | appctx->ctx.stats.flags |= STAT_FMT_JSON; |
Willy Tarreau | 2b812e2 | 2016-11-22 16:18:05 +0100 | [diff] [blame] | 4033 | |
Willy Tarreau | 2b812e2 | 2016-11-22 16:18:05 +0100 | [diff] [blame] | 4034 | return 0; |
| 4035 | } |
| 4036 | |
Willy Tarreau | 0baac8c | 2016-11-22 16:36:53 +0100 | [diff] [blame] | 4037 | static int cli_io_handler_dump_info(struct appctx *appctx) |
| 4038 | { |
| 4039 | return stats_dump_info_to_buffer(appctx->owner); |
| 4040 | } |
| 4041 | |
Willy Tarreau | 2b812e2 | 2016-11-22 16:18:05 +0100 | [diff] [blame] | 4042 | /* This I/O handler runs as an applet embedded in a stream interface. It is |
| 4043 | * used to send raw stats over a socket. |
| 4044 | */ |
| 4045 | static int cli_io_handler_dump_stat(struct appctx *appctx) |
| 4046 | { |
Christopher Faulet | ef77922 | 2018-10-31 08:47:01 +0100 | [diff] [blame] | 4047 | return stats_dump_stat_to_buffer(appctx->owner, NULL, NULL); |
Willy Tarreau | 2b812e2 | 2016-11-22 16:18:05 +0100 | [diff] [blame] | 4048 | } |
| 4049 | |
Simon Horman | 6f6bb38 | 2017-01-04 09:37:26 +0100 | [diff] [blame] | 4050 | static int cli_io_handler_dump_json_schema(struct appctx *appctx) |
| 4051 | { |
| 4052 | return stats_dump_json_schema_to_buffer(appctx->owner); |
| 4053 | } |
| 4054 | |
Willy Tarreau | 2b812e2 | 2016-11-22 16:18:05 +0100 | [diff] [blame] | 4055 | /* register cli keywords */ |
| 4056 | static struct cli_kw_list cli_kws = {{ },{ |
Willy Tarreau | 89d467c | 2016-11-23 11:02:40 +0100 | [diff] [blame] | 4057 | { { "clear", "counters", NULL }, "clear counters : clear max statistics counters (add 'all' for all counters)", cli_parse_clear_counters, NULL, NULL }, |
Willy Tarreau | 0baac8c | 2016-11-22 16:36:53 +0100 | [diff] [blame] | 4058 | { { "show", "info", NULL }, "show info : report information about the running process", cli_parse_show_info, cli_io_handler_dump_info, NULL }, |
Willy Tarreau | 2b812e2 | 2016-11-22 16:18:05 +0100 | [diff] [blame] | 4059 | { { "show", "stat", NULL }, "show stat : report counters for each proxy and server", cli_parse_show_stat, cli_io_handler_dump_stat, NULL }, |
Simon Horman | 6f6bb38 | 2017-01-04 09:37:26 +0100 | [diff] [blame] | 4060 | { { "show", "schema", "json", NULL }, "show schema json : report schema used for stats", NULL, cli_io_handler_dump_json_schema, NULL }, |
Willy Tarreau | 2b812e2 | 2016-11-22 16:18:05 +0100 | [diff] [blame] | 4061 | {{},} |
| 4062 | }}; |
| 4063 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 4064 | INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws); |
| 4065 | |
William Lallemand | 74c24fb | 2016-11-21 17:18:36 +0100 | [diff] [blame] | 4066 | struct applet http_stats_applet = { |
| 4067 | .obj_type = OBJ_TYPE_APPLET, |
| 4068 | .name = "<STATS>", /* used for logging */ |
| 4069 | .fct = http_stats_io_handler, |
| 4070 | .release = NULL, |
| 4071 | }; |
| 4072 | |
William Lallemand | 74c24fb | 2016-11-21 17:18:36 +0100 | [diff] [blame] | 4073 | /* |
| 4074 | * Local variables: |
| 4075 | * c-indent-level: 8 |
| 4076 | * c-basic-offset: 8 |
| 4077 | * End: |
| 4078 | */ |