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