blob: 1f1e5cd7141a5dbb2987cfdb3bcdb6ea59934237 [file] [log] [blame]
William Lallemand74c24fb2016-11-21 17:18:36 +01001/*
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
Willy Tarreau4c7e4b72020-05-27 12:58:42 +020027#include <haproxy/api.h>
William Lallemand74c24fb2016-11-21 17:18:36 +010028#include <common/cfgparse.h>
Willy Tarreau4aa573d2020-06-04 18:21:56 +020029#include <haproxy/check.h>
Willy Tarreau0a3bd392020-06-04 08:52:38 +020030#include <haproxy/compression.h>
Willy Tarreau2a83d602020-05-27 16:58:08 +020031#include <haproxy/debug.h>
Willy Tarreaueb92deb2020-06-04 10:53:16 +020032#include <haproxy/dns.h>
Willy Tarreau762d7a52020-06-04 11:23:07 +020033#include <haproxy/frontend.h>
Willy Tarreauf268ee82020-06-04 17:05:57 +020034#include <haproxy/global.h>
Willy Tarreaucd72d8c2020-06-02 19:11:26 +020035#include <haproxy/http.h>
Willy Tarreau87735332020-06-04 09:08:41 +020036#include <haproxy/http_htx.h>
Willy Tarreau16f958c2020-06-03 08:44:35 +020037#include <haproxy/htx.h>
Willy Tarreaud0ef4392020-06-02 09:38:52 +020038#include <haproxy/pool.h>
Willy Tarreau853b2972020-05-27 18:01:47 +020039#include <haproxy/list.h>
Willy Tarreau213e9902020-06-04 14:58:24 +020040#include <haproxy/listener.h>
Willy Tarreau2cd58092020-06-04 15:10:43 +020041#include <haproxy/map-t.h>
Willy Tarreau225a90a2020-06-04 15:06:28 +020042#include <haproxy/pattern-t.h>
Willy Tarreaucea0e1b2020-06-04 17:25:40 +020043#include <haproxy/task.h>
Willy Tarreau48fbcae2020-06-03 18:09:46 +020044#include <haproxy/tools.h>
Willy Tarreauc2f7c582020-06-02 18:15:32 +020045#include <haproxy/ticks.h>
Willy Tarreau92b4f132020-06-01 11:05:15 +020046#include <haproxy/time.h>
William Lallemand74c24fb2016-11-21 17:18:36 +010047#include <common/uri_auth.h>
Willy Tarreaud6788052020-05-27 15:59:00 +020048#include <haproxy/version.h>
Willy Tarreau8d366972020-05-27 16:10:29 +020049#include <haproxy/base64.h>
William Lallemand74c24fb2016-11-21 17:18:36 +010050
51#include <types/applet.h>
Willy Tarreau2b812e22016-11-22 16:18:05 +010052#include <types/cli.h>
William Lallemand9ed62032016-11-21 17:49:11 +010053#include <types/stats.h>
William Lallemand74c24fb2016-11-21 17:18:36 +010054
55#include <proto/backend.h>
56#include <proto/channel.h>
Willy Tarreau2b812e22016-11-22 16:18:05 +010057#include <proto/cli.h>
William Lallemand9ed62032016-11-21 17:49:11 +010058#include <proto/stats.h>
Willy Tarreau0f6ffd62020-06-03 19:33:00 +020059#include <haproxy/fd.h>
Willy Tarreau66347942020-06-01 12:18:08 +020060#include <haproxy/freq_ctr.h>
William Lallemand74c24fb2016-11-21 17:18:36 +010061#include <proto/log.h>
Willy Tarreau551271d2020-06-04 08:32:23 +020062#include <haproxy/pipe.h>
William Lallemand74c24fb2016-11-21 17:18:36 +010063#include <proto/proxy.h>
William Lallemand74c24fb2016-11-21 17:18:36 +010064#include <proto/session.h>
Willy Tarreauc125cef2019-05-10 09:58:43 +020065#include <proto/ssl_sock.h>
William Lallemand74c24fb2016-11-21 17:18:36 +010066#include <proto/stream.h>
67#include <proto/server.h>
William Lallemand74c24fb2016-11-21 17:18:36 +010068#include <proto/stream_interface.h>
William Lallemand74c24fb2016-11-21 17:18:36 +010069
Christopher Faulet2b9b6782019-02-27 16:42:58 +010070/* status codes available for the stats admin page (strictly 4 chars length) */
71const char *stat_status_codes[STAT_STATUS_SIZE] = {
72 [STAT_STATUS_DENY] = "DENY",
73 [STAT_STATUS_DONE] = "DONE",
74 [STAT_STATUS_ERRP] = "ERRP",
75 [STAT_STATUS_EXCD] = "EXCD",
76 [STAT_STATUS_NONE] = "NONE",
77 [STAT_STATUS_PART] = "PART",
78 [STAT_STATUS_UNKN] = "UNKN",
79 [STAT_STATUS_IVAL] = "IVAL",
80};
81
Willy Tarreau0baac8c2016-11-22 16:36:53 +010082/* These are the field names for each INF_* field position. Please pay attention
83 * to always use the exact same name except that the strings for new names must
84 * be lower case or CamelCase while the enum entries must be upper case.
85 */
Willy Tarreaueaa55372019-10-09 07:39:11 +020086const struct name_desc info_fields[INF_TOTAL_FIELDS] = {
Willy Tarreau6d4897e2019-10-11 16:31:46 +020087 [INF_NAME] = { .name = "Name", .desc = "Product name" },
88 [INF_VERSION] = { .name = "Version", .desc = "Product version" },
89 [INF_RELEASE_DATE] = { .name = "Release_date", .desc = "Date of latest source code update" },
90 [INF_NBTHREAD] = { .name = "Nbthread", .desc = "Number of started threads (global.nbthread)" },
91 [INF_NBPROC] = { .name = "Nbproc", .desc = "Number of started worker processes (global.nbproc)" },
92 [INF_PROCESS_NUM] = { .name = "Process_num", .desc = "Relative worker process number (1..Nbproc)" },
93 [INF_PID] = { .name = "Pid", .desc = "This worker process identifier for the system" },
94 [INF_UPTIME] = { .name = "Uptime", .desc = "How long ago this worker process was started (days+hours+minutes+seconds)" },
95 [INF_UPTIME_SEC] = { .name = "Uptime_sec", .desc = "How long ago this worker process was started (seconds)" },
96 [INF_MEMMAX_MB] = { .name = "Memmax_MB", .desc = "Worker process's hard limit on memory usage in MB (-m on command line)" },
97 [INF_POOL_ALLOC_MB] = { .name = "PoolAlloc_MB", .desc = "Amount of memory allocated in pools (in MB)" },
98 [INF_POOL_USED_MB] = { .name = "PoolUsed_MB", .desc = "Amount of pool memory currently used (in MB)" },
99 [INF_POOL_FAILED] = { .name = "PoolFailed", .desc = "Number of failed pool allocations since this worker was started" },
100 [INF_ULIMIT_N] = { .name = "Ulimit-n", .desc = "Hard limit on the number of per-process file descriptors" },
101 [INF_MAXSOCK] = { .name = "Maxsock", .desc = "Hard limit on the number of per-process sockets" },
102 [INF_MAXCONN] = { .name = "Maxconn", .desc = "Hard limit on the number of per-process connections (configured or imposed by Ulimit-n)" },
103 [INF_HARD_MAXCONN] = { .name = "Hard_maxconn", .desc = "Hard limit on the number of per-process connections (imposed by Memmax_MB or Ulimit-n)" },
104 [INF_CURR_CONN] = { .name = "CurrConns", .desc = "Current number of connections on this worker process" },
105 [INF_CUM_CONN] = { .name = "CumConns", .desc = "Total number of connections on this worker process since started" },
106 [INF_CUM_REQ] = { .name = "CumReq", .desc = "Total number of requests on this worker process since started" },
107 [INF_MAX_SSL_CONNS] = { .name = "MaxSslConns", .desc = "Hard limit on the number of per-process SSL endpoints (front+back), 0=unlimited" },
108 [INF_CURR_SSL_CONNS] = { .name = "CurrSslConns", .desc = "Current number of SSL endpoints on this worker process (front+back)" },
109 [INF_CUM_SSL_CONNS] = { .name = "CumSslConns", .desc = "Total number of SSL endpoints on this worker process since started (front+back)" },
110 [INF_MAXPIPES] = { .name = "Maxpipes", .desc = "Hard limit on the number of pipes for splicing, 0=unlimited" },
111 [INF_PIPES_USED] = { .name = "PipesUsed", .desc = "Current number of pipes in use in this worker process" },
112 [INF_PIPES_FREE] = { .name = "PipesFree", .desc = "Current number of allocated and available pipes in this worker process" },
113 [INF_CONN_RATE] = { .name = "ConnRate", .desc = "Number of front connections created on this worker process over the last second" },
114 [INF_CONN_RATE_LIMIT] = { .name = "ConnRateLimit", .desc = "Hard limit for ConnRate (global.maxconnrate)" },
115 [INF_MAX_CONN_RATE] = { .name = "MaxConnRate", .desc = "Highest ConnRate reached on this worker process since started (in connections per second)" },
116 [INF_SESS_RATE] = { .name = "SessRate", .desc = "Number of sessions created on this worker process over the last second" },
117 [INF_SESS_RATE_LIMIT] = { .name = "SessRateLimit", .desc = "Hard limit for SessRate (global.maxsessrate)" },
118 [INF_MAX_SESS_RATE] = { .name = "MaxSessRate", .desc = "Highest SessRate reached on this worker process since started (in sessions per second)" },
119 [INF_SSL_RATE] = { .name = "SslRate", .desc = "Number of SSL connections created on this worker process over the last second" },
120 [INF_SSL_RATE_LIMIT] = { .name = "SslRateLimit", .desc = "Hard limit for SslRate (global.maxsslrate)" },
121 [INF_MAX_SSL_RATE] = { .name = "MaxSslRate", .desc = "Highest SslRate reached on this worker process since started (in connections per second)" },
122 [INF_SSL_FRONTEND_KEY_RATE] = { .name = "SslFrontendKeyRate", .desc = "Number of SSL keys created on frontends in this worker process over the last second" },
123 [INF_SSL_FRONTEND_MAX_KEY_RATE] = { .name = "SslFrontendMaxKeyRate", .desc = "Highest SslFrontendKeyRate reached on this worker process since started (in SSL keys per second)" },
124 [INF_SSL_FRONTEND_SESSION_REUSE_PCT] = { .name = "SslFrontendSessionReuse_pct", .desc = "Percent of frontend SSL connections which did not require a new key" },
125 [INF_SSL_BACKEND_KEY_RATE] = { .name = "SslBackendKeyRate", .desc = "Number of SSL keys created on backends in this worker process over the last second" },
126 [INF_SSL_BACKEND_MAX_KEY_RATE] = { .name = "SslBackendMaxKeyRate", .desc = "Highest SslBackendKeyRate reached on this worker process since started (in SSL keys per second)" },
127 [INF_SSL_CACHE_LOOKUPS] = { .name = "SslCacheLookups", .desc = "Total number of SSL session ID lookups in the SSL session cache on this worker since started" },
128 [INF_SSL_CACHE_MISSES] = { .name = "SslCacheMisses", .desc = "Total number of SSL session ID lookups that didn't find a session in the SSL session cache on this worker since started" },
129 [INF_COMPRESS_BPS_IN] = { .name = "CompressBpsIn", .desc = "Number of bytes submitted to the HTTP compressor in this worker process over the last second" },
130 [INF_COMPRESS_BPS_OUT] = { .name = "CompressBpsOut", .desc = "Number of bytes emitted by the HTTP compressor in this worker process over the last second" },
131 [INF_COMPRESS_BPS_RATE_LIM] = { .name = "CompressBpsRateLim", .desc = "Limit of CompressBpsOut beyond which HTTP compression is automatically disabled" },
132 [INF_ZLIB_MEM_USAGE] = { .name = "ZlibMemUsage", .desc = "Amount of memory currently used by HTTP compression on the current worker process (in bytes)" },
133 [INF_MAX_ZLIB_MEM_USAGE] = { .name = "MaxZlibMemUsage", .desc = "Limit on the amount of memory used by HTTP compression above which it is automatically disabled (in bytes, see global.maxzlibmem)" },
134 [INF_TASKS] = { .name = "Tasks", .desc = "Total number of tasks in the current worker process (active + sleeping)" },
135 [INF_RUN_QUEUE] = { .name = "Run_queue", .desc = "Total number of active tasks+tasklets in the current worker process" },
136 [INF_IDLE_PCT] = { .name = "Idle_pct", .desc = "Percentage of last second spent waiting in the current worker thread" },
137 [INF_NODE] = { .name = "node", .desc = "Node name (global.node)" },
138 [INF_DESCRIPTION] = { .name = "description", .desc = "Node description (global.description)" },
139 [INF_STOPPING] = { .name = "Stopping", .desc = "1 if the worker process is currently stopping, otherwise zero" },
140 [INF_JOBS] = { .name = "Jobs", .desc = "Current number of active jobs on the current worker process (frontend connections, master connections, listeners)" },
141 [INF_UNSTOPPABLE_JOBS] = { .name = "Unstoppable Jobs", .desc = "Current number of unstoppable jobs on the current worker process (master connections)" },
142 [INF_LISTENERS] = { .name = "Listeners", .desc = "Current number of active listeners on the current worker process" },
143 [INF_ACTIVE_PEERS] = { .name = "ActivePeers", .desc = "Current number of verified active peers connections on the current worker process" },
144 [INF_CONNECTED_PEERS] = { .name = "ConnectedPeers", .desc = "Current number of peers having passed the connection step on the current worker process" },
145 [INF_DROPPED_LOGS] = { .name = "DroppedLogs", .desc = "Total number of dropped logs for current worker process since started" },
146 [INF_BUSY_POLLING] = { .name = "BusyPolling", .desc = "1 if busy-polling is currently in use on the worker process, otherwise zero (config.busy-polling)" },
147 [INF_FAILED_RESOLUTIONS] = { .name = "FailedResolutions", .desc = "Total number of failed DNS resolutions in current worker process since started" },
148 [INF_TOTAL_BYTES_OUT] = { .name = "TotalBytesOut", .desc = "Total number of bytes emitted by current worker process since started" },
149 [INF_BYTES_OUT_RATE] = { .name = "BytesOutRate", .desc = "Number of bytes emitted by current worker process over the last second" },
Willy Tarreau9b013702019-10-24 18:18:02 +0200150 [INF_DEBUG_COMMANDS_ISSUED] = { .name = "DebugCommandsIssued", .desc = "Number of debug commands issued on this process (anything > 0 is unsafe)" },
Willy Tarreau0baac8c2016-11-22 16:36:53 +0100151};
152
Willy Tarreaueaa55372019-10-09 07:39:11 +0200153const struct name_desc stat_fields[ST_F_TOTAL_FIELDS] = {
Willy Tarreau6d4897e2019-10-11 16:31:46 +0200154 [ST_F_PXNAME] = { .name = "pxname", .desc = "Proxy name" },
155 [ST_F_SVNAME] = { .name = "svname", .desc = "Server name" },
156 [ST_F_QCUR] = { .name = "qcur", .desc = "Current number of connections waiting in the server of backend queue" },
157 [ST_F_QMAX] = { .name = "qmax", .desc = "Highest value of qcur encountered since process started" },
158 [ST_F_SCUR] = { .name = "scur", .desc = "Current number of sessions on the frontend, backend or server" },
159 [ST_F_SMAX] = { .name = "smax", .desc = "Highest value of scur encountered since process started" },
160 [ST_F_SLIM] = { .name = "slim", .desc = "Frontend/listener/server's maxconn, backend's fullconn" },
161 [ST_F_STOT] = { .name = "stot", .desc = "Total number of sessions since process started" },
162 [ST_F_BIN] = { .name = "bin", .desc = "Total number of request bytes since process started" },
163 [ST_F_BOUT] = { .name = "bout", .desc = "Total number of response bytes since process started" },
164 [ST_F_DREQ] = { .name = "dreq", .desc = "Total number of denied requests since process started" },
165 [ST_F_DRESP] = { .name = "dresp", .desc = "Total number of denied responses since process started" },
166 [ST_F_EREQ] = { .name = "ereq", .desc = "Total number of invalid requests since process started" },
167 [ST_F_ECON] = { .name = "econ", .desc = "Total number of failed connections to server since the worker process started" },
168 [ST_F_ERESP] = { .name = "eresp", .desc = "Total number of invalid responses since the worker process started" },
169 [ST_F_WRETR] = { .name = "wretr", .desc = "Total number of server connection retries since the worker process started" },
170 [ST_F_WREDIS] = { .name = "wredis", .desc = "Total number of server redispatches due to connection failures since the worker process started" },
171 [ST_F_STATUS] = { .name = "status", .desc = "Frontend/listen status: OPEN/WAITING/FULL/STOP; backend: UP/DOWN; server: last check status" },
172 [ST_F_WEIGHT] = { .name = "weight", .desc = "Server weight, or sum of active servers' weights for a backend" },
173 [ST_F_ACT] = { .name = "act", .desc = "Total number of active UP servers with a non-zero weight" },
174 [ST_F_BCK] = { .name = "bck", .desc = "Total number of backup UP servers with a non-zero weight" },
175 [ST_F_CHKFAIL] = { .name = "chkfail", .desc = "Total number of failed individual health checks per server/backend, since the worker process started" },
176 [ST_F_CHKDOWN] = { .name = "chkdown", .desc = "Total number of failed checks causing UP to DOWN server transitions, per server/backend, since the worker process started" },
177 [ST_F_LASTCHG] = { .name = "lastchg", .desc = "How long ago the last server state changed, in seconds" },
178 [ST_F_DOWNTIME] = { .name = "downtime", .desc = "Total time spent in DOWN state, for server or backend" },
179 [ST_F_QLIMIT] = { .name = "qlimit", .desc = "Limit on the number of connections in queue, for servers only (maxqueue argument)" },
180 [ST_F_PID] = { .name = "pid", .desc = "Relative worker process number (1..nbproc)" },
181 [ST_F_IID] = { .name = "iid", .desc = "Frontend or Backend numeric identifier ('id' setting)" },
182 [ST_F_SID] = { .name = "sid", .desc = "Server numeric identifier ('id' setting)" },
183 [ST_F_THROTTLE] = { .name = "throttle", .desc = "Throttling ratio applied to a server's maxconn and weight during the slowstart period (0 to 100%)" },
184 [ST_F_LBTOT] = { .name = "lbtot", .desc = "Total number of requests routed by load balancing since the worker process started (ignores queue pop and stickiness)" },
185 [ST_F_TRACKED] = { .name = "tracked", .desc = "Name of the other server this server tracks for its state" },
186 [ST_F_TYPE] = { .name = "type", .desc = "Type of the object (Listener, Frontend, Backend, Server)" },
187 [ST_F_RATE] = { .name = "rate", .desc = "Total number of sessions processed by this object over the last second (sessions for listeners/frontends, requests for backends/servers)" },
188 [ST_F_RATE_LIM] = { .name = "rate_lim", .desc = "Limit on the number of sessions accepted in a second (frontend only, 'rate-limit sessions' setting)" },
189 [ST_F_RATE_MAX] = { .name = "rate_max", .desc = "Highest value of 'rate' observed since the worker process started" },
190 [ST_F_CHECK_STATUS] = { .name = "check_status", .desc = "Status report of the server's latest health check, prefixed with '*' if a check is currently in progress" },
191 [ST_F_CHECK_CODE] = { .name = "check_code", .desc = "HTTP/SMTP/LDAP status code reported by the latest server health check" },
192 [ST_F_CHECK_DURATION] = { .name = "check_duration", .desc = "Total duration of the latest server health check, in milliseconds" },
193 [ST_F_HRSP_1XX] = { .name = "hrsp_1xx", .desc = "Total number of HTTP responses with status 100-199 returned by this object since the worker process started" },
194 [ST_F_HRSP_2XX] = { .name = "hrsp_2xx", .desc = "Total number of HTTP responses with status 200-299 returned by this object since the worker process started" },
195 [ST_F_HRSP_3XX] = { .name = "hrsp_3xx", .desc = "Total number of HTTP responses with status 300-399 returned by this object since the worker process started" },
196 [ST_F_HRSP_4XX] = { .name = "hrsp_4xx", .desc = "Total number of HTTP responses with status 400-499 returned by this object since the worker process started" },
197 [ST_F_HRSP_5XX] = { .name = "hrsp_5xx", .desc = "Total number of HTTP responses with status 500-599 returned by this object since the worker process started" },
198 [ST_F_HRSP_OTHER] = { .name = "hrsp_other", .desc = "Total number of HTTP responses with status <100, >599 returned by this object since the worker process started (error -1 included)" },
199 [ST_F_HANAFAIL] = { .name = "hanafail", .desc = "Total number of failed checks caused by an 'on-error' directive after an 'observe' condition matched" },
200 [ST_F_REQ_RATE] = { .name = "req_rate", .desc = "Number of HTTP requests processed over the last second on this object" },
201 [ST_F_REQ_RATE_MAX] = { .name = "req_rate_max", .desc = "Highest value of 'req_rate' observed since the worker process started" },
202 [ST_F_REQ_TOT] = { .name = "req_tot", .desc = "Total number of HTTP requests processed by this object since the worker process started" },
203 [ST_F_CLI_ABRT] = { .name = "cli_abrt", .desc = "Total number of requests or connections aborted by the client since the worker process started" },
204 [ST_F_SRV_ABRT] = { .name = "srv_abrt", .desc = "Total number of requests or connections aborted by the server since the worker process started" },
205 [ST_F_COMP_IN] = { .name = "comp_in", .desc = "Total number of bytes submitted to the HTTP compressor for this object since the worker process started" },
206 [ST_F_COMP_OUT] = { .name = "comp_out", .desc = "Total number of bytes emitted by the HTTP compressor for this object since the worker process started" },
207 [ST_F_COMP_BYP] = { .name = "comp_byp", .desc = "Total number of bytes that bypassed HTTP compression for this object since the worker process started (CPU/memory/bandwidth limitation)" },
208 [ST_F_COMP_RSP] = { .name = "comp_rsp", .desc = "Total number of HTTP responses that were compressed for this object since the worker process started" },
209 [ST_F_LASTSESS] = { .name = "lastsess", .desc = "How long ago some traffic was seen on this object on this worker process, in seconds" },
210 [ST_F_LAST_CHK] = { .name = "last_chk", .desc = "Short description of the latest health check report for this server (see also check_desc)" },
211 [ST_F_LAST_AGT] = { .name = "last_agt", .desc = "Short description of the latest agent check report for this server (see also agent_desc)" },
212 [ST_F_QTIME] = { .name = "qtime", .desc = "Time spent in the queue, in milliseconds, averaged over the 1024 last requests (backend/server)" },
213 [ST_F_CTIME] = { .name = "ctime", .desc = "Time spent waiting for a connection to complete, in milliseconds, averaged over the 1024 last requests (backend/server)" },
214 [ST_F_RTIME] = { .name = "rtime", .desc = "Time spent waiting for a server response, in milliseconds, averaged over the 1024 last requests (backend/server)" },
215 [ST_F_TTIME] = { .name = "ttime", .desc = "Total request+response time (request+queue+connect+response+processing), in milliseconds, averaged over the 1024 last requests (backend/server)" },
216 [ST_F_AGENT_STATUS] = { .name = "agent_status", .desc = "Status report of the server's latest agent check, prefixed with '*' if a check is currently in progress" },
217 [ST_F_AGENT_CODE] = { .name = "agent_code", .desc = "Status code reported by the latest server agent check" },
218 [ST_F_AGENT_DURATION] = { .name = "agent_duration", .desc = "Total duration of the latest server agent check, in milliseconds" },
219 [ST_F_CHECK_DESC] = { .name = "check_desc", .desc = "Textual description of the latest health check report for this server" },
220 [ST_F_AGENT_DESC] = { .name = "agent_desc", .desc = "Textual description of the latest agent check report for this server" },
221 [ST_F_CHECK_RISE] = { .name = "check_rise", .desc = "Number of successful health checks before declaring a server UP (server 'rise' setting)" },
222 [ST_F_CHECK_FALL] = { .name = "check_fall", .desc = "Number of failed health checks before declaring a server DOWN (server 'fall' setting)" },
223 [ST_F_CHECK_HEALTH] = { .name = "check_health", .desc = "Current server health check level (0..fall-1=DOWN, fall..rise-1=UP)" },
224 [ST_F_AGENT_RISE] = { .name = "agent_rise", .desc = "Number of successful agent checks before declaring a server UP (server 'rise' setting)" },
225 [ST_F_AGENT_FALL] = { .name = "agent_fall", .desc = "Number of failed agent checks before declaring a server DOWN (server 'fall' setting)" },
226 [ST_F_AGENT_HEALTH] = { .name = "agent_health", .desc = "Current server agent check level (0..fall-1=DOWN, fall..rise-1=UP)" },
227 [ST_F_ADDR] = { .name = "addr", .desc = "Server's address:port, shown only if show-legends is set, or at levels oper/admin for the CLI" },
228 [ST_F_COOKIE] = { .name = "cookie", .desc = "Backend's cookie name or Server's cookie value, shown only if show-legends is set, or at levels oper/admin for the CLI" },
229 [ST_F_MODE] = { .name = "mode", .desc = "'mode' setting (tcp/http/health/cli)" },
230 [ST_F_ALGO] = { .name = "algo", .desc = "Backend's load balancing algorithm, shown only if show-legends is set, or at levels oper/admin for the CLI" },
231 [ST_F_CONN_RATE] = { .name = "conn_rate", .desc = "Number of new connections accepted over the last second on the frontend for this worker process" },
232 [ST_F_CONN_RATE_MAX] = { .name = "conn_rate_max", .desc = "Highest value of 'conn_rate' observed since the worker process started" },
233 [ST_F_CONN_TOT] = { .name = "conn_tot", .desc = "Total number of new connections accepted on this frontend since the worker process started" },
234 [ST_F_INTERCEPTED] = { .name = "intercepted", .desc = "Total number of HTTP requests intercepted on the frontend (redirects/stats/services) since the worker process started" },
235 [ST_F_DCON] = { .name = "dcon", .desc = "Total number of incoming connections blocked on a listener/frontend by a tcp-request connection rule since the worker process started" },
236 [ST_F_DSES] = { .name = "dses", .desc = "Total number of incoming sessions blocked on a listener/frontend by a tcp-request connection rule since the worker process started" },
237 [ST_F_WREW] = { .name = "wrew", .desc = "Total number of failed HTTP header rewrites since the worker process started" },
238 [ST_F_CONNECT] = { .name = "connect", .desc = "Total number of outgoing connection attempts on this backend/server since the worker process started" },
239 [ST_F_REUSE] = { .name = "reuse", .desc = "Total number of reused connection on this backend/server since the worker process started" },
240 [ST_F_CACHE_LOOKUPS] = { .name = "cache_lookups", .desc = "Total number of HTTP requests looked up in the cache on this frontend/backend since the worker process started" },
241 [ST_F_CACHE_HITS] = { .name = "cache_hits", .desc = "Total number of HTTP requests not found in the cache on this frontend/backend since the worker process started" },
242 [ST_F_SRV_ICUR] = { .name = "srv_icur", .desc = "Current number of idle connections available for reuse on this server" },
243 [ST_F_SRV_ILIM] = { .name = "src_ilim", .desc = "Limit on the number of available idle connections on this server (server 'pool_max_conn' directive)" },
Christopher Faulet0d1c2a62019-11-08 14:59:51 +0100244 [ST_F_QT_MAX] = { .name = "qtime_max", .desc = "Maximum observed time spent in the queue, in milliseconds (backend/server)" },
245 [ST_F_CT_MAX] = { .name = "ctime_max", .desc = "Maximum observed time spent waiting for a connection to complete, in milliseconds (backend/server)" },
246 [ST_F_RT_MAX] = { .name = "rtime_max", .desc = "Maximum observed time spent waiting for a server response, in milliseconds (backend/server)" },
247 [ST_F_TT_MAX] = { .name = "ttime_max", .desc = "Maximum observed total request+response time (request+queue+connect+response+processing), in milliseconds (backend/server)" },
Christopher Faulet0159ee42019-12-16 14:40:39 +0100248 [ST_F_EINT] = { .name = "eint", .desc = "Total number of internal errors since process started"},
William Lallemand74c24fb2016-11-21 17:18:36 +0100249};
250
Willy Tarreau0baac8c2016-11-22 16:36:53 +0100251/* one line of info */
Christopher Faulet1bc04c72017-10-29 20:14:08 +0100252static THREAD_LOCAL struct field info[INF_TOTAL_FIELDS];
William Lallemand74c24fb2016-11-21 17:18:36 +0100253/* one line of stats */
Christopher Faulet1bc04c72017-10-29 20:14:08 +0100254static THREAD_LOCAL struct field stats[ST_F_TOTAL_FIELDS];
William Lallemand74c24fb2016-11-21 17:18:36 +0100255
Christopher Faulet6338a082019-09-09 15:50:54 +0200256static void stats_dump_json_schema(struct buffer *out);
William Lallemand74c24fb2016-11-21 17:18:36 +0100257
Christopher Fauletef779222018-10-31 08:47:01 +0100258static int stats_putchk(struct channel *chn, struct htx *htx, struct buffer *chk)
259{
260 if (htx) {
Christopher Faulet69fc88c2019-01-07 14:27:53 +0100261 if (chk->data >= channel_htx_recv_max(chn, htx))
262 return 0;
Willy Tarreau0a7ef022019-05-28 10:30:11 +0200263 if (!htx_add_data_atonce(htx, ist2(chk->area, chk->data)))
Christopher Fauletef779222018-10-31 08:47:01 +0100264 return 0;
Christopher Faulet5adbeeb2019-01-02 14:34:39 +0100265 channel_add_input(chn, chk->data);
Christopher Fauletef779222018-10-31 08:47:01 +0100266 chk->data = 0;
Christopher Fauletef779222018-10-31 08:47:01 +0100267 }
268 else {
269 if (ci_putchk(chn, chk) == -1)
270 return 0;
271 }
272 return 1;
273}
Willy Tarreau0baac8c2016-11-22 16:36:53 +0100274
Christopher Fauleted7a0662019-01-14 11:07:34 +0100275static const char *stats_scope_ptr(struct appctx *appctx, struct stream_interface *si)
276{
Christopher Fauletb7f88902019-07-15 21:56:43 +0200277 struct channel *req = si_oc(si);
278 struct htx *htx = htxbuf(&req->buf);
279 struct htx_blk *blk;
280 struct ist uri;
Christopher Fauleted7a0662019-01-14 11:07:34 +0100281
Christopher Fauletb7f88902019-07-15 21:56:43 +0200282 blk = htx_get_head_blk(htx);
Christopher Fauletea009732019-11-18 15:50:25 +0100283 BUG_ON(!blk || htx_get_blk_type(blk) != HTX_BLK_REQ_SL);
Christopher Fauletb7f88902019-07-15 21:56:43 +0200284 ALREADY_CHECKED(blk);
285 uri = htx_sl_req_uri(htx_get_blk_ptr(htx, blk));
286 return uri.ptr + appctx->ctx.stats.scope_str;
Christopher Fauleted7a0662019-01-14 11:07:34 +0100287}
288
William Lallemand74c24fb2016-11-21 17:18:36 +0100289/*
290 * http_stats_io_handler()
291 * -> stats_dump_stat_to_buffer() // same as above, but used for CSV or HTML
292 * -> stats_dump_csv_header() // emits the CSV headers (same as above)
Simon Horman05ee2132017-01-04 09:37:25 +0100293 * -> stats_dump_json_header() // emits the JSON headers (same as above)
William Lallemand74c24fb2016-11-21 17:18:36 +0100294 * -> stats_dump_html_head() // emits the HTML headers
295 * -> stats_dump_html_info() // emits the equivalent of "show info" at the top
296 * -> stats_dump_proxy_to_buffer() // same as above, valid for CSV and HTML
297 * -> stats_dump_html_px_hdr()
298 * -> stats_dump_fe_stats()
299 * -> stats_dump_li_stats()
300 * -> stats_dump_sv_stats()
301 * -> stats_dump_be_stats()
302 * -> stats_dump_html_px_end()
303 * -> stats_dump_html_end() // emits HTML trailer
Simon Horman05ee2132017-01-04 09:37:25 +0100304 * -> stats_dump_json_end() // emits JSON trailer
William Lallemand74c24fb2016-11-21 17:18:36 +0100305 */
306
307
William Lallemand74c24fb2016-11-21 17:18:36 +0100308/* Dumps the stats CSV header to the trash buffer which. The caller is responsible
309 * for clearing it if needed.
310 * NOTE: Some tools happen to rely on the field position instead of its name,
311 * so please only append new fields at the end, never in the middle.
312 */
313static void stats_dump_csv_header()
314{
315 int field;
316
317 chunk_appendf(&trash, "# ");
318 for (field = 0; field < ST_F_TOTAL_FIELDS; field++)
Willy Tarreaueaa55372019-10-09 07:39:11 +0200319 chunk_appendf(&trash, "%s,", stat_fields[field].name);
William Lallemand74c24fb2016-11-21 17:18:36 +0100320
321 chunk_appendf(&trash, "\n");
322}
323
324
325/* Emits a stats field without any surrounding element and properly encoded to
326 * resist CSV output. Returns non-zero on success, 0 if the buffer is full.
327 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200328int stats_emit_raw_data_field(struct buffer *out, const struct field *f)
William Lallemand74c24fb2016-11-21 17:18:36 +0100329{
330 switch (field_format(f, 0)) {
331 case FF_EMPTY: return 1;
332 case FF_S32: return chunk_appendf(out, "%d", f->u.s32);
333 case FF_U32: return chunk_appendf(out, "%u", f->u.u32);
334 case FF_S64: return chunk_appendf(out, "%lld", (long long)f->u.s64);
335 case FF_U64: return chunk_appendf(out, "%llu", (unsigned long long)f->u.u64);
Christopher Faulet88a0db22019-09-24 16:35:10 +0200336 case FF_FLT: return chunk_appendf(out, "%f", f->u.flt);
William Lallemand74c24fb2016-11-21 17:18:36 +0100337 case FF_STR: return csv_enc_append(field_str(f, 0), 1, out) != NULL;
338 default: return chunk_appendf(out, "[INCORRECT_FIELD_TYPE_%08x]", f->type);
339 }
340}
341
342/* Emits a stats field prefixed with its type. No CSV encoding is prepared, the
343 * output is supposed to be used on its own line. Returns non-zero on success, 0
344 * if the buffer is full.
345 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200346int stats_emit_typed_data_field(struct buffer *out, const struct field *f)
William Lallemand74c24fb2016-11-21 17:18:36 +0100347{
348 switch (field_format(f, 0)) {
349 case FF_EMPTY: return 1;
350 case FF_S32: return chunk_appendf(out, "s32:%d", f->u.s32);
351 case FF_U32: return chunk_appendf(out, "u32:%u", f->u.u32);
352 case FF_S64: return chunk_appendf(out, "s64:%lld", (long long)f->u.s64);
353 case FF_U64: return chunk_appendf(out, "u64:%llu", (unsigned long long)f->u.u64);
Christopher Faulet88a0db22019-09-24 16:35:10 +0200354 case FF_FLT: return chunk_appendf(out, "flt:%f", f->u.flt);
William Lallemand74c24fb2016-11-21 17:18:36 +0100355 case FF_STR: return chunk_appendf(out, "str:%s", field_str(f, 0));
356 default: return chunk_appendf(out, "%08x:?", f->type);
357 }
358}
359
Simon Horman05ee2132017-01-04 09:37:25 +0100360/* Limit JSON integer values to the range [-(2**53)+1, (2**53)-1] as per
361 * the recommendation for interoperable integers in section 6 of RFC 7159.
362 */
363#define JSON_INT_MAX ((1ULL << 53) - 1)
364#define JSON_INT_MIN (0 - JSON_INT_MAX)
365
366/* Emits a stats field value and its type in JSON.
367 * Returns non-zero on success, 0 on error.
368 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200369int stats_emit_json_data_field(struct buffer *out, const struct field *f)
Simon Horman05ee2132017-01-04 09:37:25 +0100370{
371 int old_len;
372 char buf[20];
373 const char *type, *value = buf, *quote = "";
374
375 switch (field_format(f, 0)) {
376 case FF_EMPTY: return 1;
377 case FF_S32: type = "\"s32\"";
378 snprintf(buf, sizeof(buf), "%d", f->u.s32);
379 break;
380 case FF_U32: type = "\"u32\"";
381 snprintf(buf, sizeof(buf), "%u", f->u.u32);
382 break;
383 case FF_S64: type = "\"s64\"";
384 if (f->u.s64 < JSON_INT_MIN || f->u.s64 > JSON_INT_MAX)
385 return 0;
386 type = "\"s64\"";
387 snprintf(buf, sizeof(buf), "%lld", (long long)f->u.s64);
388 break;
389 case FF_U64: if (f->u.u64 > JSON_INT_MAX)
390 return 0;
391 type = "\"u64\"";
392 snprintf(buf, sizeof(buf), "%llu",
393 (unsigned long long) f->u.u64);
Christopher Faulet52c91bb2019-09-28 10:37:31 +0200394 break;
Christopher Faulet88a0db22019-09-24 16:35:10 +0200395 case FF_FLT: type = "\"flt\"";
396 snprintf(buf, sizeof(buf), "%f", f->u.flt);
Simon Horman05ee2132017-01-04 09:37:25 +0100397 break;
398 case FF_STR: type = "\"str\"";
399 value = field_str(f, 0);
400 quote = "\"";
401 break;
402 default: snprintf(buf, sizeof(buf), "%u", f->type);
403 type = buf;
404 value = "unknown";
405 quote = "\"";
406 break;
407 }
408
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200409 old_len = out->data;
Simon Horman05ee2132017-01-04 09:37:25 +0100410 chunk_appendf(out, ",\"value\":{\"type\":%s,\"value\":%s%s%s}",
411 type, quote, value, quote);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200412 return !(old_len == out->data);
Simon Horman05ee2132017-01-04 09:37:25 +0100413}
414
William Lallemand74c24fb2016-11-21 17:18:36 +0100415/* Emits an encoding of the field type on 3 characters followed by a delimiter.
416 * Returns non-zero on success, 0 if the buffer is full.
417 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200418int stats_emit_field_tags(struct buffer *out, const struct field *f,
419 char delim)
William Lallemand74c24fb2016-11-21 17:18:36 +0100420{
421 char origin, nature, scope;
422
423 switch (field_origin(f, 0)) {
424 case FO_METRIC: origin = 'M'; break;
425 case FO_STATUS: origin = 'S'; break;
426 case FO_KEY: origin = 'K'; break;
427 case FO_CONFIG: origin = 'C'; break;
428 case FO_PRODUCT: origin = 'P'; break;
429 default: origin = '?'; break;
430 }
431
432 switch (field_nature(f, 0)) {
433 case FN_GAUGE: nature = 'G'; break;
434 case FN_LIMIT: nature = 'L'; break;
435 case FN_MIN: nature = 'm'; break;
436 case FN_MAX: nature = 'M'; break;
437 case FN_RATE: nature = 'R'; break;
438 case FN_COUNTER: nature = 'C'; break;
439 case FN_DURATION: nature = 'D'; break;
440 case FN_AGE: nature = 'A'; break;
441 case FN_TIME: nature = 'T'; break;
442 case FN_NAME: nature = 'N'; break;
443 case FN_OUTPUT: nature = 'O'; break;
444 case FN_AVG: nature = 'a'; break;
445 default: nature = '?'; break;
446 }
447
448 switch (field_scope(f, 0)) {
449 case FS_PROCESS: scope = 'P'; break;
450 case FS_SERVICE: scope = 'S'; break;
451 case FS_SYSTEM: scope = 's'; break;
452 case FS_CLUSTER: scope = 'C'; break;
453 default: scope = '?'; break;
454 }
455
456 return chunk_appendf(out, "%c%c%c%c", origin, nature, scope, delim);
457}
458
Simon Horman05ee2132017-01-04 09:37:25 +0100459/* Emits an encoding of the field type as JSON.
460 * Returns non-zero on success, 0 if the buffer is full.
461 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200462int stats_emit_json_field_tags(struct buffer *out, const struct field *f)
Simon Horman05ee2132017-01-04 09:37:25 +0100463{
464 const char *origin, *nature, *scope;
465 int old_len;
466
467 switch (field_origin(f, 0)) {
468 case FO_METRIC: origin = "Metric"; break;
469 case FO_STATUS: origin = "Status"; break;
470 case FO_KEY: origin = "Key"; break;
471 case FO_CONFIG: origin = "Config"; break;
472 case FO_PRODUCT: origin = "Product"; break;
473 default: origin = "Unknown"; break;
474 }
475
476 switch (field_nature(f, 0)) {
477 case FN_GAUGE: nature = "Gauge"; break;
478 case FN_LIMIT: nature = "Limit"; break;
479 case FN_MIN: nature = "Min"; break;
480 case FN_MAX: nature = "Max"; break;
481 case FN_RATE: nature = "Rate"; break;
482 case FN_COUNTER: nature = "Counter"; break;
483 case FN_DURATION: nature = "Duration"; break;
484 case FN_AGE: nature = "Age"; break;
485 case FN_TIME: nature = "Time"; break;
486 case FN_NAME: nature = "Name"; break;
487 case FN_OUTPUT: nature = "Output"; break;
488 case FN_AVG: nature = "Avg"; break;
489 default: nature = "Unknown"; break;
490 }
491
492 switch (field_scope(f, 0)) {
493 case FS_PROCESS: scope = "Process"; break;
494 case FS_SERVICE: scope = "Service"; break;
495 case FS_SYSTEM: scope = "System"; break;
496 case FS_CLUSTER: scope = "Cluster"; break;
497 default: scope = "Unknown"; break;
498 }
499
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200500 old_len = out->data;
Simon Horman05ee2132017-01-04 09:37:25 +0100501 chunk_appendf(out, "\"tags\":{"
502 "\"origin\":\"%s\","
503 "\"nature\":\"%s\","
504 "\"scope\":\"%s\""
505 "}", origin, nature, scope);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200506 return !(old_len == out->data);
Simon Horman05ee2132017-01-04 09:37:25 +0100507}
William Lallemand74c24fb2016-11-21 17:18:36 +0100508
509/* Dump all fields from <stats> into <out> using CSV format */
Willy Tarreau83061a82018-07-13 11:56:34 +0200510static int stats_dump_fields_csv(struct buffer *out,
Willy Tarreau43241ff2019-10-09 11:27:51 +0200511 const struct field *stats, unsigned int flags)
William Lallemand74c24fb2016-11-21 17:18:36 +0100512{
513 int field;
514
515 for (field = 0; field < ST_F_TOTAL_FIELDS; field++) {
516 if (!stats_emit_raw_data_field(out, &stats[field]))
517 return 0;
518 if (!chunk_strcat(out, ","))
519 return 0;
520 }
521 chunk_strcat(out, "\n");
522 return 1;
523}
524
525/* Dump all fields from <stats> into <out> using a typed "field:desc:type:value" format */
Willy Tarreau83061a82018-07-13 11:56:34 +0200526static int stats_dump_fields_typed(struct buffer *out,
Willy Tarreau43241ff2019-10-09 11:27:51 +0200527 const struct field *stats, unsigned int flags)
William Lallemand74c24fb2016-11-21 17:18:36 +0100528{
529 int field;
530
531 for (field = 0; field < ST_F_TOTAL_FIELDS; field++) {
532 if (!stats[field].type)
533 continue;
534
535 chunk_appendf(out, "%c.%u.%u.%d.%s.%u:",
536 stats[ST_F_TYPE].u.u32 == STATS_TYPE_FE ? 'F' :
537 stats[ST_F_TYPE].u.u32 == STATS_TYPE_BE ? 'B' :
538 stats[ST_F_TYPE].u.u32 == STATS_TYPE_SO ? 'L' :
539 stats[ST_F_TYPE].u.u32 == STATS_TYPE_SV ? 'S' :
540 '?',
541 stats[ST_F_IID].u.u32, stats[ST_F_SID].u.u32,
Willy Tarreaueaa55372019-10-09 07:39:11 +0200542 field, stat_fields[field].name, stats[ST_F_PID].u.u32);
William Lallemand74c24fb2016-11-21 17:18:36 +0100543
544 if (!stats_emit_field_tags(out, &stats[field], ':'))
545 return 0;
546 if (!stats_emit_typed_data_field(out, &stats[field]))
547 return 0;
Willy Tarreau6b19b142019-10-09 15:44:21 +0200548 if ((flags & STAT_SHOW_FDESC) && !chunk_appendf(out, ":\"%s\"", stat_fields[field].desc))
549 return 0;
William Lallemand74c24fb2016-11-21 17:18:36 +0100550 if (!chunk_strcat(out, "\n"))
551 return 0;
552 }
553 return 1;
554}
555
Simon Horman05ee2132017-01-04 09:37:25 +0100556/* Dump all fields from <stats> into <out> using the "show info json" format */
Willy Tarreau83061a82018-07-13 11:56:34 +0200557static int stats_dump_json_info_fields(struct buffer *out,
Willy Tarreau43241ff2019-10-09 11:27:51 +0200558 const struct field *info, unsigned int flags)
Simon Horman05ee2132017-01-04 09:37:25 +0100559{
560 int field;
561 int started = 0;
562
563 if (!chunk_strcat(out, "["))
564 return 0;
565
566 for (field = 0; field < INF_TOTAL_FIELDS; field++) {
567 int old_len;
568
569 if (!field_format(info, field))
570 continue;
571
572 if (started && !chunk_strcat(out, ","))
573 goto err;
574 started = 1;
575
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200576 old_len = out->data;
Simon Horman05ee2132017-01-04 09:37:25 +0100577 chunk_appendf(out,
578 "{\"field\":{\"pos\":%d,\"name\":\"%s\"},"
579 "\"processNum\":%u,",
Willy Tarreaueaa55372019-10-09 07:39:11 +0200580 field, info_fields[field].name,
Simon Horman05ee2132017-01-04 09:37:25 +0100581 info[INF_PROCESS_NUM].u.u32);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200582 if (old_len == out->data)
Simon Horman05ee2132017-01-04 09:37:25 +0100583 goto err;
584
585 if (!stats_emit_json_field_tags(out, &info[field]))
586 goto err;
587
588 if (!stats_emit_json_data_field(out, &info[field]))
589 goto err;
590
591 if (!chunk_strcat(out, "}"))
592 goto err;
593 }
594
595 if (!chunk_strcat(out, "]"))
596 goto err;
597 return 1;
598
599err:
600 chunk_reset(out);
601 chunk_appendf(out, "{\"errorStr\":\"output buffer too short\"}");
602 return 0;
603}
604
605/* Dump all fields from <stats> into <out> using a typed "field:desc:type:value" format */
Willy Tarreau83061a82018-07-13 11:56:34 +0200606static int stats_dump_fields_json(struct buffer *out,
607 const struct field *stats,
Willy Tarreaub0ce3ad2019-10-09 11:19:29 +0200608 unsigned int flags)
Simon Horman05ee2132017-01-04 09:37:25 +0100609{
610 int field;
611 int started = 0;
612
Willy Tarreaub0ce3ad2019-10-09 11:19:29 +0200613 if ((flags & STAT_STARTED) && !chunk_strcat(out, ","))
Simon Horman05ee2132017-01-04 09:37:25 +0100614 return 0;
615 if (!chunk_strcat(out, "["))
616 return 0;
617
618 for (field = 0; field < ST_F_TOTAL_FIELDS; field++) {
619 const char *obj_type;
620 int old_len;
621
622 if (!stats[field].type)
623 continue;
624
625 if (started && !chunk_strcat(out, ","))
626 goto err;
627 started = 1;
628
629 switch (stats[ST_F_TYPE].u.u32) {
630 case STATS_TYPE_FE: obj_type = "Frontend"; break;
631 case STATS_TYPE_BE: obj_type = "Backend"; break;
632 case STATS_TYPE_SO: obj_type = "Listener"; break;
633 case STATS_TYPE_SV: obj_type = "Server"; break;
634 default: obj_type = "Unknown"; break;
635 }
636
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200637 old_len = out->data;
Simon Horman05ee2132017-01-04 09:37:25 +0100638 chunk_appendf(out,
639 "{"
640 "\"objType\":\"%s\","
641 "\"proxyId\":%d,"
642 "\"id\":%d,"
643 "\"field\":{\"pos\":%d,\"name\":\"%s\"},"
644 "\"processNum\":%u,",
645 obj_type, stats[ST_F_IID].u.u32,
646 stats[ST_F_SID].u.u32, field,
Willy Tarreaueaa55372019-10-09 07:39:11 +0200647 stat_fields[field].name, stats[ST_F_PID].u.u32);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200648 if (old_len == out->data)
Simon Horman05ee2132017-01-04 09:37:25 +0100649 goto err;
650
651 if (!stats_emit_json_field_tags(out, &stats[field]))
652 goto err;
653
654 if (!stats_emit_json_data_field(out, &stats[field]))
655 goto err;
656
657 if (!chunk_strcat(out, "}"))
658 goto err;
659 }
660
661 if (!chunk_strcat(out, "]"))
662 goto err;
663
664 return 1;
665
666err:
667 chunk_reset(out);
Willy Tarreaub0ce3ad2019-10-09 11:19:29 +0200668 if (flags & STAT_STARTED)
669 chunk_strcat(out, ",");
Simon Horman05ee2132017-01-04 09:37:25 +0100670 chunk_appendf(out, "{\"errorStr\":\"output buffer too short\"}");
671 return 0;
672}
673
William Lallemand74c24fb2016-11-21 17:18:36 +0100674/* Dump all fields from <stats> into <out> using the HTML format. A column is
Willy Tarreauab02b3f2019-10-09 11:11:46 +0200675 * reserved for the checkbox is STAT_ADMIN is set in <flags>. Some extra info
Willy Tarreau708c4162019-10-09 10:19:16 +0200676 * are provided if STAT_SHLGNDS is present in <flags>.
William Lallemand74c24fb2016-11-21 17:18:36 +0100677 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200678static int stats_dump_fields_html(struct buffer *out,
679 const struct field *stats,
680 unsigned int flags)
William Lallemand74c24fb2016-11-21 17:18:36 +0100681{
Willy Tarreau83061a82018-07-13 11:56:34 +0200682 struct buffer src;
William Lallemand74c24fb2016-11-21 17:18:36 +0100683
684 if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_FE) {
685 chunk_appendf(out,
686 /* name, queue */
687 "<tr class=\"frontend\">");
688
Willy Tarreauab02b3f2019-10-09 11:11:46 +0200689 if (flags & STAT_ADMIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +0100690 /* Column sub-heading for Enable or Disable server */
691 chunk_appendf(out, "<td></td>");
692 }
693
694 chunk_appendf(out,
695 "<td class=ac>"
696 "<a name=\"%s/Frontend\"></a>"
697 "<a class=lfsb href=\"#%s/Frontend\">Frontend</a></td>"
698 "<td colspan=3></td>"
699 "",
700 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_PXNAME));
701
702 chunk_appendf(out,
703 /* sessions rate : current */
704 "<td><u>%s<div class=tips><table class=det>"
705 "<tr><th>Current connection rate:</th><td>%s/s</td></tr>"
706 "<tr><th>Current session rate:</th><td>%s/s</td></tr>"
707 "",
708 U2H(stats[ST_F_RATE].u.u32),
709 U2H(stats[ST_F_CONN_RATE].u.u32),
710 U2H(stats[ST_F_RATE].u.u32));
711
712 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
713 chunk_appendf(out,
714 "<tr><th>Current request rate:</th><td>%s/s</td></tr>",
715 U2H(stats[ST_F_REQ_RATE].u.u32));
716
717 chunk_appendf(out,
718 "</table></div></u></td>"
719 /* sessions rate : max */
720 "<td><u>%s<div class=tips><table class=det>"
721 "<tr><th>Max connection rate:</th><td>%s/s</td></tr>"
722 "<tr><th>Max session rate:</th><td>%s/s</td></tr>"
723 "",
724 U2H(stats[ST_F_RATE_MAX].u.u32),
725 U2H(stats[ST_F_CONN_RATE_MAX].u.u32),
726 U2H(stats[ST_F_RATE_MAX].u.u32));
727
728 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
729 chunk_appendf(out,
730 "<tr><th>Max request rate:</th><td>%s/s</td></tr>",
731 U2H(stats[ST_F_REQ_RATE_MAX].u.u32));
732
733 chunk_appendf(out,
734 "</table></div></u></td>"
735 /* sessions rate : limit */
736 "<td>%s</td>",
737 LIM2A(stats[ST_F_RATE_LIM].u.u32, "-"));
738
739 chunk_appendf(out,
740 /* sessions: current, max, limit, total */
741 "<td>%s</td><td>%s</td><td>%s</td>"
742 "<td><u>%s<div class=tips><table class=det>"
743 "<tr><th>Cum. connections:</th><td>%s</td></tr>"
744 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
745 "",
746 U2H(stats[ST_F_SCUR].u.u32), U2H(stats[ST_F_SMAX].u.u32), U2H(stats[ST_F_SLIM].u.u32),
747 U2H(stats[ST_F_STOT].u.u64),
748 U2H(stats[ST_F_CONN_TOT].u.u64),
749 U2H(stats[ST_F_STOT].u.u64));
750
751 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
752 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0) {
753 chunk_appendf(out,
754 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
755 "<tr><th>- HTTP 1xx responses:</th><td>%s</td></tr>"
756 "<tr><th>- HTTP 2xx responses:</th><td>%s</td></tr>"
757 "<tr><th>&nbsp;&nbsp;Compressed 2xx:</th><td>%s</td><td>(%d%%)</td></tr>"
758 "<tr><th>- HTTP 3xx responses:</th><td>%s</td></tr>"
759 "<tr><th>- HTTP 4xx responses:</th><td>%s</td></tr>"
760 "<tr><th>- HTTP 5xx responses:</th><td>%s</td></tr>"
761 "<tr><th>- other responses:</th><td>%s</td></tr>"
762 "<tr><th>Intercepted requests:</th><td>%s</td></tr>"
Willy Tarreaua1214a52018-12-14 14:00:25 +0100763 "<tr><th>Cache lookups:</th><td>%s</td></tr>"
764 "<tr><th>Cache hits:</th><td>%s</td><td>(%d%%)</td></tr>"
Willy Tarreau1b0f85e2018-05-28 15:12:40 +0200765 "<tr><th>Failed hdr rewrites:</th><td>%s</td></tr>"
Christopher Faulet0159ee42019-12-16 14:40:39 +0100766 "<tr><th>Internal errors:</th><td>%s</td></tr>"
William Lallemand74c24fb2016-11-21 17:18:36 +0100767 "",
768 U2H(stats[ST_F_REQ_TOT].u.u64),
769 U2H(stats[ST_F_HRSP_1XX].u.u64),
770 U2H(stats[ST_F_HRSP_2XX].u.u64),
771 U2H(stats[ST_F_COMP_RSP].u.u64),
772 stats[ST_F_HRSP_2XX].u.u64 ?
773 (int)(100 * stats[ST_F_COMP_RSP].u.u64 / stats[ST_F_HRSP_2XX].u.u64) : 0,
774 U2H(stats[ST_F_HRSP_3XX].u.u64),
775 U2H(stats[ST_F_HRSP_4XX].u.u64),
776 U2H(stats[ST_F_HRSP_5XX].u.u64),
777 U2H(stats[ST_F_HRSP_OTHER].u.u64),
Willy Tarreau1b0f85e2018-05-28 15:12:40 +0200778 U2H(stats[ST_F_INTERCEPTED].u.u64),
Willy Tarreaua1214a52018-12-14 14:00:25 +0100779 U2H(stats[ST_F_CACHE_LOOKUPS].u.u64),
780 U2H(stats[ST_F_CACHE_HITS].u.u64),
781 stats[ST_F_CACHE_LOOKUPS].u.u64 ?
782 (int)(100 * stats[ST_F_CACHE_HITS].u.u64 / stats[ST_F_CACHE_LOOKUPS].u.u64) : 0,
Christopher Faulet0159ee42019-12-16 14:40:39 +0100783 U2H(stats[ST_F_WREW].u.u64),
784 U2H(stats[ST_F_EINT].u.u64));
William Lallemand74c24fb2016-11-21 17:18:36 +0100785 }
786
787 chunk_appendf(out,
788 "</table></div></u></td>"
789 /* sessions: lbtot, lastsess */
790 "<td></td><td></td>"
791 /* bytes : in */
792 "<td>%s</td>"
793 "",
794 U2H(stats[ST_F_BIN].u.u64));
795
796 chunk_appendf(out,
797 /* bytes:out + compression stats (via hover): comp_in, comp_out, comp_byp */
798 "<td>%s%s<div class=tips><table class=det>"
799 "<tr><th>Response bytes in:</th><td>%s</td></tr>"
800 "<tr><th>Compression in:</th><td>%s</td></tr>"
801 "<tr><th>Compression out:</th><td>%s</td><td>(%d%%)</td></tr>"
802 "<tr><th>Compression bypass:</th><td>%s</td></tr>"
803 "<tr><th>Total bytes saved:</th><td>%s</td><td>(%d%%)</td></tr>"
804 "</table></div>%s</td>",
805 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "<u>":"",
806 U2H(stats[ST_F_BOUT].u.u64),
807 U2H(stats[ST_F_BOUT].u.u64),
808 U2H(stats[ST_F_COMP_IN].u.u64),
809 U2H(stats[ST_F_COMP_OUT].u.u64),
810 stats[ST_F_COMP_IN].u.u64 ? (int)(stats[ST_F_COMP_OUT].u.u64 * 100 / stats[ST_F_COMP_IN].u.u64) : 0,
811 U2H(stats[ST_F_COMP_BYP].u.u64),
812 U2H(stats[ST_F_COMP_IN].u.u64 - stats[ST_F_COMP_OUT].u.u64),
813 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,
814 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "</u>":"");
815
816 chunk_appendf(out,
817 /* denied: req, resp */
818 "<td>%s</td><td>%s</td>"
819 /* errors : request, connect, response */
820 "<td>%s</td><td></td><td></td>"
821 /* warnings: retries, redispatches */
822 "<td></td><td></td>"
823 /* server status : reflect frontend status */
824 "<td class=ac>%s</td>"
825 /* rest of server: nothing */
826 "<td class=ac colspan=8></td></tr>"
827 "",
828 U2H(stats[ST_F_DREQ].u.u64), U2H(stats[ST_F_DRESP].u.u64),
829 U2H(stats[ST_F_EREQ].u.u64),
830 field_str(stats, ST_F_STATUS));
831 }
832 else if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_SO) {
833 chunk_appendf(out, "<tr class=socket>");
Willy Tarreauab02b3f2019-10-09 11:11:46 +0200834 if (flags & STAT_ADMIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +0100835 /* Column sub-heading for Enable or Disable server */
836 chunk_appendf(out, "<td></td>");
837 }
838
839 chunk_appendf(out,
840 /* frontend name, listener name */
841 "<td class=ac><a name=\"%s/+%s\"></a>%s"
842 "<a class=lfsb href=\"#%s/+%s\">%s</a>"
843 "",
844 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME),
Willy Tarreau708c4162019-10-09 10:19:16 +0200845 (flags & STAT_SHLGNDS)?"<u>":"",
William Lallemand74c24fb2016-11-21 17:18:36 +0100846 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME), field_str(stats, ST_F_SVNAME));
847
Willy Tarreau708c4162019-10-09 10:19:16 +0200848 if (flags & STAT_SHLGNDS) {
William Lallemand74c24fb2016-11-21 17:18:36 +0100849 chunk_appendf(out, "<div class=tips>");
850
Willy Tarreau90807112020-02-25 08:16:33 +0100851 if (isdigit((unsigned char)*field_str(stats, ST_F_ADDR)))
William Lallemand74c24fb2016-11-21 17:18:36 +0100852 chunk_appendf(out, "IPv4: %s, ", field_str(stats, ST_F_ADDR));
853 else if (*field_str(stats, ST_F_ADDR) == '[')
854 chunk_appendf(out, "IPv6: %s, ", field_str(stats, ST_F_ADDR));
855 else if (*field_str(stats, ST_F_ADDR))
856 chunk_appendf(out, "%s, ", field_str(stats, ST_F_ADDR));
857
858 /* id */
859 chunk_appendf(out, "id: %d</div>", stats[ST_F_SID].u.u32);
860 }
861
862 chunk_appendf(out,
863 /* queue */
864 "%s</td><td colspan=3></td>"
865 /* sessions rate: current, max, limit */
866 "<td colspan=3>&nbsp;</td>"
867 /* sessions: current, max, limit, total, lbtot, lastsess */
868 "<td>%s</td><td>%s</td><td>%s</td>"
869 "<td>%s</td><td>&nbsp;</td><td>&nbsp;</td>"
870 /* bytes: in, out */
871 "<td>%s</td><td>%s</td>"
872 "",
Willy Tarreau708c4162019-10-09 10:19:16 +0200873 (flags & STAT_SHLGNDS)?"</u>":"",
William Lallemand74c24fb2016-11-21 17:18:36 +0100874 U2H(stats[ST_F_SCUR].u.u32), U2H(stats[ST_F_SMAX].u.u32), U2H(stats[ST_F_SLIM].u.u32),
875 U2H(stats[ST_F_STOT].u.u64), U2H(stats[ST_F_BIN].u.u64), U2H(stats[ST_F_BOUT].u.u64));
876
877 chunk_appendf(out,
878 /* denied: req, resp */
879 "<td>%s</td><td>%s</td>"
880 /* errors: request, connect, response */
881 "<td>%s</td><td></td><td></td>"
882 /* warnings: retries, redispatches */
883 "<td></td><td></td>"
884 /* server status: reflect listener status */
885 "<td class=ac>%s</td>"
886 /* rest of server: nothing */
887 "<td class=ac colspan=8></td></tr>"
888 "",
889 U2H(stats[ST_F_DREQ].u.u64), U2H(stats[ST_F_DRESP].u.u64),
890 U2H(stats[ST_F_EREQ].u.u64),
891 field_str(stats, ST_F_STATUS));
892 }
893 else if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_SV) {
894 const char *style;
895
896 /* determine the style to use depending on the server's state,
897 * its health and weight. There isn't a 1-to-1 mapping between
898 * state and styles for the cases where the server is (still)
899 * up. The reason is that we don't want to report nolb and
900 * drain with the same color.
901 */
902
903 if (strcmp(field_str(stats, ST_F_STATUS), "DOWN") == 0 ||
904 strcmp(field_str(stats, ST_F_STATUS), "DOWN (agent)") == 0) {
905 style = "down";
906 }
907 else if (strcmp(field_str(stats, ST_F_STATUS), "DOWN ") == 0) {
908 style = "going_up";
909 }
Daniel Corbettb4285172020-03-28 12:35:50 -0400910 else if (strcmp(field_str(stats, ST_F_STATUS), "DRAIN") == 0) {
911 style = "draining";
912 }
William Lallemand74c24fb2016-11-21 17:18:36 +0100913 else if (strcmp(field_str(stats, ST_F_STATUS), "NOLB ") == 0) {
914 style = "going_down";
915 }
916 else if (strcmp(field_str(stats, ST_F_STATUS), "NOLB") == 0) {
917 style = "nolb";
918 }
919 else if (strcmp(field_str(stats, ST_F_STATUS), "no check") == 0) {
920 style = "no_check";
921 }
922 else if (!stats[ST_F_CHKFAIL].type ||
923 stats[ST_F_CHECK_HEALTH].u.u32 == stats[ST_F_CHECK_RISE].u.u32 + stats[ST_F_CHECK_FALL].u.u32 - 1) {
924 /* no check or max health = UP */
925 if (stats[ST_F_WEIGHT].u.u32)
926 style = "up";
927 else
928 style = "draining";
929 }
930 else {
931 style = "going_down";
932 }
933
934 if (memcmp(field_str(stats, ST_F_STATUS), "MAINT", 5) == 0)
935 chunk_appendf(out, "<tr class=\"maintain\">");
936 else
937 chunk_appendf(out,
938 "<tr class=\"%s_%s\">",
939 (stats[ST_F_BCK].u.u32) ? "backup" : "active", style);
940
941
Willy Tarreauab02b3f2019-10-09 11:11:46 +0200942 if (flags & STAT_ADMIN)
William Lallemand74c24fb2016-11-21 17:18:36 +0100943 chunk_appendf(out,
David Harrigand3db35a2016-12-30 12:12:49 +0000944 "<td><input class='%s-checkbox' type=\"checkbox\" name=\"s\" value=\"%s\"></td>",
945 field_str(stats, ST_F_PXNAME),
William Lallemand74c24fb2016-11-21 17:18:36 +0100946 field_str(stats, ST_F_SVNAME));
947
948 chunk_appendf(out,
949 "<td class=ac><a name=\"%s/%s\"></a>%s"
950 "<a class=lfsb href=\"#%s/%s\">%s</a>"
951 "",
952 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME),
Willy Tarreau708c4162019-10-09 10:19:16 +0200953 (flags & STAT_SHLGNDS) ? "<u>" : "",
William Lallemand74c24fb2016-11-21 17:18:36 +0100954 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME), field_str(stats, ST_F_SVNAME));
955
Willy Tarreau708c4162019-10-09 10:19:16 +0200956 if (flags & STAT_SHLGNDS) {
William Lallemand74c24fb2016-11-21 17:18:36 +0100957 chunk_appendf(out, "<div class=tips>");
958
Willy Tarreau90807112020-02-25 08:16:33 +0100959 if (isdigit((unsigned char)*field_str(stats, ST_F_ADDR)))
William Lallemand74c24fb2016-11-21 17:18:36 +0100960 chunk_appendf(out, "IPv4: %s, ", field_str(stats, ST_F_ADDR));
961 else if (*field_str(stats, ST_F_ADDR) == '[')
962 chunk_appendf(out, "IPv6: %s, ", field_str(stats, ST_F_ADDR));
963 else if (*field_str(stats, ST_F_ADDR))
964 chunk_appendf(out, "%s, ", field_str(stats, ST_F_ADDR));
965
966 /* id */
967 chunk_appendf(out, "id: %d", stats[ST_F_SID].u.u32);
968
969 /* cookie */
970 if (stats[ST_F_COOKIE].type) {
971 chunk_appendf(out, ", cookie: '");
972 chunk_initstr(&src, field_str(stats, ST_F_COOKIE));
973 chunk_htmlencode(out, &src);
974 chunk_appendf(out, "'");
975 }
976
977 chunk_appendf(out, "</div>");
978 }
979
980 chunk_appendf(out,
981 /* queue : current, max, limit */
982 "%s</td><td>%s</td><td>%s</td><td>%s</td>"
983 /* sessions rate : current, max, limit */
984 "<td>%s</td><td>%s</td><td></td>"
985 "",
Willy Tarreau708c4162019-10-09 10:19:16 +0200986 (flags & STAT_SHLGNDS) ? "</u>" : "",
William Lallemand74c24fb2016-11-21 17:18:36 +0100987 U2H(stats[ST_F_QCUR].u.u32), U2H(stats[ST_F_QMAX].u.u32), LIM2A(stats[ST_F_QLIMIT].u.u32, "-"),
988 U2H(stats[ST_F_RATE].u.u32), U2H(stats[ST_F_RATE_MAX].u.u32));
989
990 chunk_appendf(out,
991 /* sessions: current, max, limit, total */
Willy Tarreauf21d17b2019-09-08 09:24:56 +0200992 "<td><u>%s<div class=tips>"
993 "<table class=det>"
994 "<tr><th>Current active connections:</th><td>%s</td></tr>"
995 "<tr><th>Current idle connections:</th><td>%s</td></tr>"
996 "<tr><th>Active connections limit:</th><td>%s</td></tr>"
997 "<tr><th>Idle connections limit:</th><td>%s</td></tr>"
998 "</table></div></u>"
999 "</td><td>%s</td><td>%s</td>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001000 "<td><u>%s<div class=tips><table class=det>"
1001 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
1002 "",
Willy Tarreauf21d17b2019-09-08 09:24:56 +02001003 U2H(stats[ST_F_SCUR].u.u32),
1004 U2H(stats[ST_F_SCUR].u.u32),
1005 U2H(stats[ST_F_SRV_ICUR].u.u32),
1006 LIM2A(stats[ST_F_SLIM].u.u32, "-"),
1007 stats[ST_F_SRV_ILIM].type ? U2H(stats[ST_F_SRV_ILIM].u.u32) : "-",
1008 U2H(stats[ST_F_SMAX].u.u32), LIM2A(stats[ST_F_SLIM].u.u32, "-"),
William Lallemand74c24fb2016-11-21 17:18:36 +01001009 U2H(stats[ST_F_STOT].u.u64),
1010 U2H(stats[ST_F_STOT].u.u64));
1011
1012 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
1013 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001014 chunk_appendf(out,
Willy Tarreauf1573842018-12-14 11:35:36 +01001015 "<tr><th>New connections:</th><td>%s</td></tr>"
1016 "<tr><th>Reused connections:</th><td>%s</td><td>(%d%%)</td></tr>"
Marcin Deranek3c27dda2020-05-15 18:32:51 +02001017 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001018 "<tr><th>- HTTP 1xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
1019 "<tr><th>- HTTP 2xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
1020 "<tr><th>- HTTP 3xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
1021 "<tr><th>- HTTP 4xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
1022 "<tr><th>- HTTP 5xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
1023 "<tr><th>- other responses:</th><td>%s</td><td>(%d%%)</td></tr>"
Willy Tarreau1b0f85e2018-05-28 15:12:40 +02001024 "<tr><th>Failed hdr rewrites:</th><td>%s</td></tr>"
Christopher Faulet0159ee42019-12-16 14:40:39 +01001025 "<tr><th>Internal error:</th><td>%s</td></tr>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001026 "",
Willy Tarreauf1573842018-12-14 11:35:36 +01001027 U2H(stats[ST_F_CONNECT].u.u64),
1028 U2H(stats[ST_F_REUSE].u.u64),
1029 (stats[ST_F_CONNECT].u.u64 + stats[ST_F_REUSE].u.u64) ?
1030 (int)(100 * stats[ST_F_REUSE].u.u64 / (stats[ST_F_CONNECT].u.u64 + stats[ST_F_REUSE].u.u64)) : 0,
Marcin Deranek3c27dda2020-05-15 18:32:51 +02001031 U2H(stats[ST_F_REQ_TOT].u.u64),
1032 U2H(stats[ST_F_HRSP_1XX].u.u64), stats[ST_F_REQ_TOT].u.u64 ?
1033 (int)(100 * stats[ST_F_HRSP_1XX].u.u64 / stats[ST_F_REQ_TOT].u.u64) : 0,
1034 U2H(stats[ST_F_HRSP_2XX].u.u64), stats[ST_F_REQ_TOT].u.u64 ?
1035 (int)(100 * stats[ST_F_HRSP_2XX].u.u64 / stats[ST_F_REQ_TOT].u.u64) : 0,
1036 U2H(stats[ST_F_HRSP_3XX].u.u64), stats[ST_F_REQ_TOT].u.u64 ?
1037 (int)(100 * stats[ST_F_HRSP_3XX].u.u64 / stats[ST_F_REQ_TOT].u.u64) : 0,
1038 U2H(stats[ST_F_HRSP_4XX].u.u64), stats[ST_F_REQ_TOT].u.u64 ?
1039 (int)(100 * stats[ST_F_HRSP_4XX].u.u64 / stats[ST_F_REQ_TOT].u.u64) : 0,
1040 U2H(stats[ST_F_HRSP_5XX].u.u64), stats[ST_F_REQ_TOT].u.u64 ?
1041 (int)(100 * stats[ST_F_HRSP_5XX].u.u64 / stats[ST_F_REQ_TOT].u.u64) : 0,
1042 U2H(stats[ST_F_HRSP_OTHER].u.u64), stats[ST_F_REQ_TOT].u.u64 ?
1043 (int)(100 * stats[ST_F_HRSP_OTHER].u.u64 / stats[ST_F_REQ_TOT].u.u64) : 0,
Christopher Faulet0159ee42019-12-16 14:40:39 +01001044 U2H(stats[ST_F_WREW].u.u64),
1045 U2H(stats[ST_F_EINT].u.u64));
William Lallemand74c24fb2016-11-21 17:18:36 +01001046 }
1047
Christopher Faulet0d1c2a62019-11-08 14:59:51 +01001048 chunk_appendf(out, "<tr><th colspan=3>Max / Avg over last 1024 success. conn.</th></tr>");
1049 chunk_appendf(out, "<tr><th>- Queue time:</th><td>%s / %s</td><td>ms</td></tr>",
1050 U2H(stats[ST_F_QT_MAX].u.u32), U2H(stats[ST_F_QTIME].u.u32));
1051 chunk_appendf(out, "<tr><th>- Connect time:</th><td>%s / %s</td><td>ms</td></tr>",
1052 U2H(stats[ST_F_CT_MAX].u.u32), U2H(stats[ST_F_CTIME].u.u32));
William Lallemand74c24fb2016-11-21 17:18:36 +01001053 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
Christopher Faulet0d1c2a62019-11-08 14:59:51 +01001054 chunk_appendf(out, "<tr><th>- Responses time:</th><td>%s / %s</td><td>ms</td></tr>",
1055 U2H(stats[ST_F_RT_MAX].u.u32), U2H(stats[ST_F_RTIME].u.u32));
1056 chunk_appendf(out, "<tr><th>- Total time:</th><td>%s / %s</td><td>ms</td></tr>",
1057 U2H(stats[ST_F_TT_MAX].u.u32), U2H(stats[ST_F_TTIME].u.u32));
William Lallemand74c24fb2016-11-21 17:18:36 +01001058
1059 chunk_appendf(out,
1060 "</table></div></u></td>"
1061 /* sessions: lbtot, last */
1062 "<td>%s</td><td>%s</td>",
1063 U2H(stats[ST_F_LBTOT].u.u64),
1064 human_time(stats[ST_F_LASTSESS].u.s32, 1));
1065
1066 chunk_appendf(out,
1067 /* bytes : in, out */
1068 "<td>%s</td><td>%s</td>"
1069 /* denied: req, resp */
1070 "<td></td><td>%s</td>"
1071 /* errors : request, connect */
1072 "<td></td><td>%s</td>"
1073 /* errors : response */
1074 "<td><u>%s<div class=tips>Connection resets during transfers: %lld client, %lld server</div></u></td>"
1075 /* warnings: retries, redispatches */
1076 "<td>%lld</td><td>%lld</td>"
1077 "",
1078 U2H(stats[ST_F_BIN].u.u64), U2H(stats[ST_F_BOUT].u.u64),
1079 U2H(stats[ST_F_DRESP].u.u64),
1080 U2H(stats[ST_F_ECON].u.u64),
1081 U2H(stats[ST_F_ERESP].u.u64),
1082 (long long)stats[ST_F_CLI_ABRT].u.u64,
1083 (long long)stats[ST_F_SRV_ABRT].u.u64,
1084 (long long)stats[ST_F_WRETR].u.u64,
1085 (long long)stats[ST_F_WREDIS].u.u64);
1086
1087 /* status, last change */
1088 chunk_appendf(out, "<td class=ac>");
1089
1090 /* FIXME!!!!
1091 * LASTCHG should contain the last change for *this* server and must be computed
1092 * properly above, as was done below, ie: this server if maint, otherwise ref server
1093 * if tracking. Note that ref is either local or remote depending on tracking.
1094 */
1095
1096
1097 if (memcmp(field_str(stats, ST_F_STATUS), "MAINT", 5) == 0) {
1098 chunk_appendf(out, "%s MAINT", human_time(stats[ST_F_LASTCHG].u.u32, 1));
1099 }
1100 else if (memcmp(field_str(stats, ST_F_STATUS), "no check", 5) == 0) {
1101 chunk_strcat(out, "<i>no check</i>");
1102 }
1103 else {
1104 chunk_appendf(out, "%s %s", human_time(stats[ST_F_LASTCHG].u.u32, 1), field_str(stats, ST_F_STATUS));
1105 if (memcmp(field_str(stats, ST_F_STATUS), "DOWN", 4) == 0) {
1106 if (stats[ST_F_CHECK_HEALTH].u.u32)
1107 chunk_strcat(out, " &uarr;");
1108 }
1109 else if (stats[ST_F_CHECK_HEALTH].u.u32 < stats[ST_F_CHECK_RISE].u.u32 + stats[ST_F_CHECK_FALL].u.u32 - 1)
1110 chunk_strcat(out, " &darr;");
1111 }
1112
1113 if (memcmp(field_str(stats, ST_F_STATUS), "DOWN", 4) == 0 &&
1114 stats[ST_F_AGENT_STATUS].type && !stats[ST_F_AGENT_HEALTH].u.u32) {
1115 chunk_appendf(out,
1116 "</td><td class=ac><u> %s",
1117 field_str(stats, ST_F_AGENT_STATUS));
1118
1119 if (stats[ST_F_AGENT_CODE].type)
1120 chunk_appendf(out, "/%d", stats[ST_F_AGENT_CODE].u.u32);
1121
1122 if (stats[ST_F_AGENT_DURATION].type)
1123 chunk_appendf(out, " in %lums", (long)stats[ST_F_AGENT_DURATION].u.u64);
1124
1125 chunk_appendf(out, "<div class=tips>%s", field_str(stats, ST_F_AGENT_DESC));
1126
1127 if (*field_str(stats, ST_F_LAST_AGT)) {
1128 chunk_appendf(out, ": ");
1129 chunk_initstr(&src, field_str(stats, ST_F_LAST_AGT));
1130 chunk_htmlencode(out, &src);
1131 }
1132 chunk_appendf(out, "</div></u>");
1133 }
1134 else if (stats[ST_F_CHECK_STATUS].type) {
1135 chunk_appendf(out,
1136 "</td><td class=ac><u> %s",
1137 field_str(stats, ST_F_CHECK_STATUS));
1138
1139 if (stats[ST_F_CHECK_CODE].type)
1140 chunk_appendf(out, "/%d", stats[ST_F_CHECK_CODE].u.u32);
1141
1142 if (stats[ST_F_CHECK_DURATION].type)
1143 chunk_appendf(out, " in %lums", (long)stats[ST_F_CHECK_DURATION].u.u64);
1144
1145 chunk_appendf(out, "<div class=tips>%s", field_str(stats, ST_F_CHECK_DESC));
1146
1147 if (*field_str(stats, ST_F_LAST_CHK)) {
1148 chunk_appendf(out, ": ");
1149 chunk_initstr(&src, field_str(stats, ST_F_LAST_CHK));
1150 chunk_htmlencode(out, &src);
1151 }
1152 chunk_appendf(out, "</div></u>");
1153 }
1154 else
1155 chunk_appendf(out, "</td><td>");
1156
1157 chunk_appendf(out,
1158 /* weight */
1159 "</td><td class=ac>%d</td>"
1160 /* act, bck */
1161 "<td class=ac>%s</td><td class=ac>%s</td>"
1162 "",
1163 stats[ST_F_WEIGHT].u.u32,
1164 stats[ST_F_BCK].u.u32 ? "-" : "Y",
1165 stats[ST_F_BCK].u.u32 ? "Y" : "-");
1166
1167 /* check failures: unique, fatal, down time */
1168 if (strcmp(field_str(stats, ST_F_STATUS), "MAINT (resolution)") == 0) {
1169 chunk_appendf(out, "<td class=ac colspan=3>resolution</td>");
1170 }
1171 else if (stats[ST_F_CHKFAIL].type) {
1172 chunk_appendf(out, "<td><u>%lld", (long long)stats[ST_F_CHKFAIL].u.u64);
1173
1174 if (stats[ST_F_HANAFAIL].type)
1175 chunk_appendf(out, "/%lld", (long long)stats[ST_F_HANAFAIL].u.u64);
1176
1177 chunk_appendf(out,
1178 "<div class=tips>Failed Health Checks%s</div></u></td>"
1179 "<td>%lld</td><td>%s</td>"
1180 "",
1181 stats[ST_F_HANAFAIL].type ? "/Health Analyses" : "",
1182 (long long)stats[ST_F_CHKDOWN].u.u64, human_time(stats[ST_F_DOWNTIME].u.u32, 1));
1183 }
1184 else if (strcmp(field_str(stats, ST_F_STATUS), "MAINT") != 0 && field_format(stats, ST_F_TRACKED) == FF_STR) {
1185 /* tracking a server (hence inherited maint would appear as "MAINT (via...)" */
1186 chunk_appendf(out,
1187 "<td class=ac colspan=3><a class=lfsb href=\"#%s\">via %s</a></td>",
1188 field_str(stats, ST_F_TRACKED), field_str(stats, ST_F_TRACKED));
1189 }
1190 else
1191 chunk_appendf(out, "<td colspan=3></td>");
1192
1193 /* throttle */
1194 if (stats[ST_F_THROTTLE].type)
1195 chunk_appendf(out, "<td class=ac>%d %%</td></tr>\n", stats[ST_F_THROTTLE].u.u32);
1196 else
1197 chunk_appendf(out, "<td class=ac>-</td></tr>\n");
1198 }
1199 else if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_BE) {
1200 chunk_appendf(out, "<tr class=\"backend\">");
Willy Tarreauab02b3f2019-10-09 11:11:46 +02001201 if (flags & STAT_ADMIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001202 /* Column sub-heading for Enable or Disable server */
1203 chunk_appendf(out, "<td></td>");
1204 }
1205 chunk_appendf(out,
1206 "<td class=ac>"
1207 /* name */
1208 "%s<a name=\"%s/Backend\"></a>"
1209 "<a class=lfsb href=\"#%s/Backend\">Backend</a>"
1210 "",
Willy Tarreau708c4162019-10-09 10:19:16 +02001211 (flags & STAT_SHLGNDS)?"<u>":"",
William Lallemand74c24fb2016-11-21 17:18:36 +01001212 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_PXNAME));
1213
Willy Tarreau708c4162019-10-09 10:19:16 +02001214 if (flags & STAT_SHLGNDS) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001215 /* balancing */
1216 chunk_appendf(out, "<div class=tips>balancing: %s",
1217 field_str(stats, ST_F_ALGO));
1218
1219 /* cookie */
1220 if (stats[ST_F_COOKIE].type) {
1221 chunk_appendf(out, ", cookie: '");
1222 chunk_initstr(&src, field_str(stats, ST_F_COOKIE));
1223 chunk_htmlencode(out, &src);
1224 chunk_appendf(out, "'");
1225 }
1226 chunk_appendf(out, "</div>");
1227 }
1228
1229 chunk_appendf(out,
1230 "%s</td>"
1231 /* queue : current, max */
1232 "<td>%s</td><td>%s</td><td></td>"
1233 /* sessions rate : current, max, limit */
1234 "<td>%s</td><td>%s</td><td></td>"
1235 "",
Willy Tarreau708c4162019-10-09 10:19:16 +02001236 (flags & STAT_SHLGNDS)?"</u>":"",
William Lallemand74c24fb2016-11-21 17:18:36 +01001237 U2H(stats[ST_F_QCUR].u.u32), U2H(stats[ST_F_QMAX].u.u32),
1238 U2H(stats[ST_F_RATE].u.u32), U2H(stats[ST_F_RATE_MAX].u.u32));
1239
1240 chunk_appendf(out,
1241 /* sessions: current, max, limit, total */
1242 "<td>%s</td><td>%s</td><td>%s</td>"
1243 "<td><u>%s<div class=tips><table class=det>"
1244 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
1245 "",
Willy Tarreau8e0f1752016-12-12 15:07:29 +01001246 U2H(stats[ST_F_SCUR].u.u32), U2H(stats[ST_F_SMAX].u.u32), U2H(stats[ST_F_SLIM].u.u32),
William Lallemand74c24fb2016-11-21 17:18:36 +01001247 U2H(stats[ST_F_STOT].u.u64),
1248 U2H(stats[ST_F_STOT].u.u64));
1249
1250 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
1251 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0) {
1252 chunk_appendf(out,
Willy Tarreauf1573842018-12-14 11:35:36 +01001253 "<tr><th>New connections:</th><td>%s</td></tr>"
1254 "<tr><th>Reused connections:</th><td>%s</td><td>(%d%%)</td></tr>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001255 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
1256 "<tr><th>- HTTP 1xx responses:</th><td>%s</td></tr>"
1257 "<tr><th>- HTTP 2xx responses:</th><td>%s</td></tr>"
1258 "<tr><th>&nbsp;&nbsp;Compressed 2xx:</th><td>%s</td><td>(%d%%)</td></tr>"
1259 "<tr><th>- HTTP 3xx responses:</th><td>%s</td></tr>"
1260 "<tr><th>- HTTP 4xx responses:</th><td>%s</td></tr>"
1261 "<tr><th>- HTTP 5xx responses:</th><td>%s</td></tr>"
1262 "<tr><th>- other responses:</th><td>%s</td></tr>"
Willy Tarreaua1214a52018-12-14 14:00:25 +01001263 "<tr><th>Cache lookups:</th><td>%s</td></tr>"
1264 "<tr><th>Cache hits:</th><td>%s</td><td>(%d%%)</td></tr>"
Willy Tarreau1b0f85e2018-05-28 15:12:40 +02001265 "<tr><th>Failed hdr rewrites:</th><td>%s</td></tr>"
Christopher Faulet0159ee42019-12-16 14:40:39 +01001266 "<tr><th>Internal errors:</th><td>%s</td></tr>"
Christopher Faulet0d1c2a62019-11-08 14:59:51 +01001267 "",
Willy Tarreauf1573842018-12-14 11:35:36 +01001268 U2H(stats[ST_F_CONNECT].u.u64),
1269 U2H(stats[ST_F_REUSE].u.u64),
1270 (stats[ST_F_CONNECT].u.u64 + stats[ST_F_REUSE].u.u64) ?
1271 (int)(100 * stats[ST_F_REUSE].u.u64 / (stats[ST_F_CONNECT].u.u64 + stats[ST_F_REUSE].u.u64)) : 0,
William Lallemand74c24fb2016-11-21 17:18:36 +01001272 U2H(stats[ST_F_REQ_TOT].u.u64),
1273 U2H(stats[ST_F_HRSP_1XX].u.u64),
1274 U2H(stats[ST_F_HRSP_2XX].u.u64),
1275 U2H(stats[ST_F_COMP_RSP].u.u64),
1276 stats[ST_F_HRSP_2XX].u.u64 ?
1277 (int)(100 * stats[ST_F_COMP_RSP].u.u64 / stats[ST_F_HRSP_2XX].u.u64) : 0,
1278 U2H(stats[ST_F_HRSP_3XX].u.u64),
1279 U2H(stats[ST_F_HRSP_4XX].u.u64),
1280 U2H(stats[ST_F_HRSP_5XX].u.u64),
Willy Tarreaufeead3a2018-12-14 13:48:44 +01001281 U2H(stats[ST_F_HRSP_OTHER].u.u64),
Willy Tarreaua1214a52018-12-14 14:00:25 +01001282 U2H(stats[ST_F_CACHE_LOOKUPS].u.u64),
1283 U2H(stats[ST_F_CACHE_HITS].u.u64),
1284 stats[ST_F_CACHE_LOOKUPS].u.u64 ?
1285 (int)(100 * stats[ST_F_CACHE_HITS].u.u64 / stats[ST_F_CACHE_LOOKUPS].u.u64) : 0,
Christopher Faulet0159ee42019-12-16 14:40:39 +01001286 U2H(stats[ST_F_WREW].u.u64),
1287 U2H(stats[ST_F_EINT].u.u64));
William Lallemand74c24fb2016-11-21 17:18:36 +01001288 }
1289
Christopher Faulet0d1c2a62019-11-08 14:59:51 +01001290 chunk_appendf(out, "<tr><th colspan=3>Max / Avg over last 1024 success. conn.</th></tr>");
1291 chunk_appendf(out, "<tr><th>- Queue time:</th><td>%s / %s</td><td>ms</td></tr>",
1292 U2H(stats[ST_F_QT_MAX].u.u32), U2H(stats[ST_F_QTIME].u.u32));
1293 chunk_appendf(out, "<tr><th>- Connect time:</th><td>%s / %s</td><td>ms</td></tr>",
1294 U2H(stats[ST_F_CT_MAX].u.u32), U2H(stats[ST_F_CTIME].u.u32));
William Lallemand74c24fb2016-11-21 17:18:36 +01001295 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
Christopher Faulet0d1c2a62019-11-08 14:59:51 +01001296 chunk_appendf(out, "<tr><th>- Responses time:</th><td>%s / %s</td><td>ms</td></tr>",
1297 U2H(stats[ST_F_RT_MAX].u.u32), U2H(stats[ST_F_RTIME].u.u32));
1298 chunk_appendf(out, "<tr><th>- Total time:</th><td>%s / %s</td><td>ms</td></tr>",
1299 U2H(stats[ST_F_TT_MAX].u.u32), U2H(stats[ST_F_TTIME].u.u32));
William Lallemand74c24fb2016-11-21 17:18:36 +01001300
1301 chunk_appendf(out,
1302 "</table></div></u></td>"
1303 /* sessions: lbtot, last */
1304 "<td>%s</td><td>%s</td>"
1305 /* bytes: in */
1306 "<td>%s</td>"
1307 "",
1308 U2H(stats[ST_F_LBTOT].u.u64),
1309 human_time(stats[ST_F_LASTSESS].u.s32, 1),
1310 U2H(stats[ST_F_BIN].u.u64));
1311
1312 chunk_appendf(out,
1313 /* bytes:out + compression stats (via hover): comp_in, comp_out, comp_byp */
1314 "<td>%s%s<div class=tips><table class=det>"
1315 "<tr><th>Response bytes in:</th><td>%s</td></tr>"
1316 "<tr><th>Compression in:</th><td>%s</td></tr>"
1317 "<tr><th>Compression out:</th><td>%s</td><td>(%d%%)</td></tr>"
1318 "<tr><th>Compression bypass:</th><td>%s</td></tr>"
1319 "<tr><th>Total bytes saved:</th><td>%s</td><td>(%d%%)</td></tr>"
1320 "</table></div>%s</td>",
1321 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "<u>":"",
1322 U2H(stats[ST_F_BOUT].u.u64),
1323 U2H(stats[ST_F_BOUT].u.u64),
1324 U2H(stats[ST_F_COMP_IN].u.u64),
1325 U2H(stats[ST_F_COMP_OUT].u.u64),
1326 stats[ST_F_COMP_IN].u.u64 ? (int)(stats[ST_F_COMP_OUT].u.u64 * 100 / stats[ST_F_COMP_IN].u.u64) : 0,
1327 U2H(stats[ST_F_COMP_BYP].u.u64),
1328 U2H(stats[ST_F_COMP_IN].u.u64 - stats[ST_F_COMP_OUT].u.u64),
1329 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,
1330 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "</u>":"");
1331
1332 chunk_appendf(out,
1333 /* denied: req, resp */
1334 "<td>%s</td><td>%s</td>"
1335 /* errors : request, connect */
1336 "<td></td><td>%s</td>"
1337 /* errors : response */
1338 "<td><u>%s<div class=tips>Connection resets during transfers: %lld client, %lld server</div></u></td>"
1339 /* warnings: retries, redispatches */
1340 "<td>%lld</td><td>%lld</td>"
1341 /* backend status: reflect backend status (up/down): we display UP
1342 * if the backend has known working servers or if it has no server at
1343 * all (eg: for stats). Then we display the total weight, number of
1344 * active and backups. */
1345 "<td class=ac>%s %s</td><td class=ac>&nbsp;</td><td class=ac>%d</td>"
1346 "<td class=ac>%d</td><td class=ac>%d</td>"
1347 "",
1348 U2H(stats[ST_F_DREQ].u.u64), U2H(stats[ST_F_DRESP].u.u64),
1349 U2H(stats[ST_F_ECON].u.u64),
1350 U2H(stats[ST_F_ERESP].u.u64),
1351 (long long)stats[ST_F_CLI_ABRT].u.u64,
1352 (long long)stats[ST_F_SRV_ABRT].u.u64,
1353 (long long)stats[ST_F_WRETR].u.u64, (long long)stats[ST_F_WREDIS].u.u64,
1354 human_time(stats[ST_F_LASTCHG].u.u32, 1),
1355 strcmp(field_str(stats, ST_F_STATUS), "DOWN") ? field_str(stats, ST_F_STATUS) : "<font color=\"red\"><b>DOWN</b></font>",
1356 stats[ST_F_WEIGHT].u.u32,
1357 stats[ST_F_ACT].u.u32, stats[ST_F_BCK].u.u32);
1358
1359 chunk_appendf(out,
1360 /* rest of backend: nothing, down transitions, total downtime, throttle */
1361 "<td class=ac>&nbsp;</td><td>%d</td>"
1362 "<td>%s</td>"
1363 "<td></td>"
1364 "</tr>",
1365 stats[ST_F_CHKDOWN].u.u32,
1366 stats[ST_F_DOWNTIME].type ? human_time(stats[ST_F_DOWNTIME].u.u32, 1) : "&nbsp;");
1367 }
1368 return 1;
1369}
1370
Willy Tarreau43241ff2019-10-09 11:27:51 +02001371static int stats_dump_one_line(const struct field *stats, struct proxy *px, struct appctx *appctx)
William Lallemand74c24fb2016-11-21 17:18:36 +01001372{
Simon Horman05ee2132017-01-04 09:37:25 +01001373 int ret;
1374
William Lallemand74c24fb2016-11-21 17:18:36 +01001375 if (appctx->ctx.stats.flags & STAT_FMT_HTML)
Willy Tarreau43241ff2019-10-09 11:27:51 +02001376 ret = stats_dump_fields_html(&trash, stats, appctx->ctx.stats.flags);
William Lallemand74c24fb2016-11-21 17:18:36 +01001377 else if (appctx->ctx.stats.flags & STAT_FMT_TYPED)
Willy Tarreau43241ff2019-10-09 11:27:51 +02001378 ret = stats_dump_fields_typed(&trash, stats, appctx->ctx.stats.flags);
Simon Horman05ee2132017-01-04 09:37:25 +01001379 else if (appctx->ctx.stats.flags & STAT_FMT_JSON)
Willy Tarreaub0ce3ad2019-10-09 11:19:29 +02001380 ret = stats_dump_fields_json(&trash, stats, appctx->ctx.stats.flags);
William Lallemand74c24fb2016-11-21 17:18:36 +01001381 else
Willy Tarreau43241ff2019-10-09 11:27:51 +02001382 ret = stats_dump_fields_csv(&trash, stats, appctx->ctx.stats.flags);
Simon Horman05ee2132017-01-04 09:37:25 +01001383
1384 if (ret)
1385 appctx->ctx.stats.flags |= STAT_STARTED;
1386
1387 return ret;
William Lallemand74c24fb2016-11-21 17:18:36 +01001388}
1389
1390/* Fill <stats> with the frontend statistics. <stats> is
1391 * preallocated array of length <len>. The length of the array
1392 * must be at least ST_F_TOTAL_FIELDS. If this length is less then
1393 * this value, the function returns 0, otherwise, it returns 1.
1394 */
1395int stats_fill_fe_stats(struct proxy *px, struct field *stats, int len)
1396{
1397 if (len < ST_F_TOTAL_FIELDS)
1398 return 0;
1399
1400 memset(stats, 0, sizeof(*stats) * len);
1401
1402 stats[ST_F_PXNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
1403 stats[ST_F_SVNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, "FRONTEND");
1404 stats[ST_F_MODE] = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
1405 stats[ST_F_SCUR] = mkf_u32(0, px->feconn);
1406 stats[ST_F_SMAX] = mkf_u32(FN_MAX, px->fe_counters.conn_max);
1407 stats[ST_F_SLIM] = mkf_u32(FO_CONFIG|FN_LIMIT, px->maxconn);
1408 stats[ST_F_STOT] = mkf_u64(FN_COUNTER, px->fe_counters.cum_sess);
1409 stats[ST_F_BIN] = mkf_u64(FN_COUNTER, px->fe_counters.bytes_in);
1410 stats[ST_F_BOUT] = mkf_u64(FN_COUNTER, px->fe_counters.bytes_out);
1411 stats[ST_F_DREQ] = mkf_u64(FN_COUNTER, px->fe_counters.denied_req);
1412 stats[ST_F_DRESP] = mkf_u64(FN_COUNTER, px->fe_counters.denied_resp);
1413 stats[ST_F_EREQ] = mkf_u64(FN_COUNTER, px->fe_counters.failed_req);
1414 stats[ST_F_DCON] = mkf_u64(FN_COUNTER, px->fe_counters.denied_conn);
1415 stats[ST_F_DSES] = mkf_u64(FN_COUNTER, px->fe_counters.denied_sess);
1416 stats[ST_F_STATUS] = mkf_str(FO_STATUS, px->state == PR_STREADY ? "OPEN" : px->state == PR_STFULL ? "FULL" : "STOP");
1417 stats[ST_F_PID] = mkf_u32(FO_KEY, relative_pid);
1418 stats[ST_F_IID] = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
1419 stats[ST_F_SID] = mkf_u32(FO_KEY|FS_SERVICE, 0);
1420 stats[ST_F_TYPE] = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_FE);
1421 stats[ST_F_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&px->fe_sess_per_sec));
1422 stats[ST_F_RATE_LIM] = mkf_u32(FO_CONFIG|FN_LIMIT, px->fe_sps_lim);
1423 stats[ST_F_RATE_MAX] = mkf_u32(FN_MAX, px->fe_counters.sps_max);
Tim Duesterhus3fd19732018-05-27 20:35:08 +02001424 stats[ST_F_WREW] = mkf_u64(FN_COUNTER, px->fe_counters.failed_rewrites);
Christopher Faulet0159ee42019-12-16 14:40:39 +01001425 stats[ST_F_EINT] = mkf_u64(FN_COUNTER, px->fe_counters.internal_errors);
William Lallemand74c24fb2016-11-21 17:18:36 +01001426
1427 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
1428 if (px->mode == PR_MODE_HTTP) {
1429 stats[ST_F_HRSP_1XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[1]);
1430 stats[ST_F_HRSP_2XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[2]);
1431 stats[ST_F_HRSP_3XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[3]);
1432 stats[ST_F_HRSP_4XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[4]);
1433 stats[ST_F_HRSP_5XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[5]);
1434 stats[ST_F_HRSP_OTHER] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[0]);
1435 stats[ST_F_INTERCEPTED] = mkf_u64(FN_COUNTER, px->fe_counters.intercepted_req);
Willy Tarreaua1214a52018-12-14 14:00:25 +01001436 stats[ST_F_CACHE_LOOKUPS] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.cache_lookups);
1437 stats[ST_F_CACHE_HITS] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.cache_hits);
William Lallemand74c24fb2016-11-21 17:18:36 +01001438 }
1439
1440 /* requests : req_rate, req_rate_max, req_tot, */
1441 stats[ST_F_REQ_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&px->fe_req_per_sec));
1442 stats[ST_F_REQ_RATE_MAX] = mkf_u32(FN_MAX, px->fe_counters.p.http.rps_max);
1443 stats[ST_F_REQ_TOT] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.cum_req);
1444
1445 /* compression: in, out, bypassed, responses */
1446 stats[ST_F_COMP_IN] = mkf_u64(FN_COUNTER, px->fe_counters.comp_in);
1447 stats[ST_F_COMP_OUT] = mkf_u64(FN_COUNTER, px->fe_counters.comp_out);
1448 stats[ST_F_COMP_BYP] = mkf_u64(FN_COUNTER, px->fe_counters.comp_byp);
1449 stats[ST_F_COMP_RSP] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.comp_rsp);
1450
1451 /* connections : conn_rate, conn_rate_max, conn_tot, conn_max */
1452 stats[ST_F_CONN_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&px->fe_conn_per_sec));
1453 stats[ST_F_CONN_RATE_MAX] = mkf_u32(FN_MAX, px->fe_counters.cps_max);
1454 stats[ST_F_CONN_TOT] = mkf_u64(FN_COUNTER, px->fe_counters.cum_conn);
1455
1456 return 1;
1457}
1458
1459/* Dumps a frontend's line to the trash for the current proxy <px> and uses
1460 * the state from stream interface <si>. The caller is responsible for clearing
1461 * the trash if needed. Returns non-zero if it emits anything, zero otherwise.
1462 */
1463static int stats_dump_fe_stats(struct stream_interface *si, struct proxy *px)
1464{
1465 struct appctx *appctx = __objt_appctx(si->end);
1466
1467 if (!(px->cap & PR_CAP_FE))
1468 return 0;
1469
1470 if ((appctx->ctx.stats.flags & STAT_BOUND) && !(appctx->ctx.stats.type & (1 << STATS_TYPE_FE)))
1471 return 0;
1472
1473 if (!stats_fill_fe_stats(px, stats, ST_F_TOTAL_FIELDS))
1474 return 0;
1475
Willy Tarreau43241ff2019-10-09 11:27:51 +02001476 return stats_dump_one_line(stats, px, appctx);
William Lallemand74c24fb2016-11-21 17:18:36 +01001477}
1478
1479/* Fill <stats> with the listener statistics. <stats> is
1480 * preallocated array of length <len>. The length of the array
1481 * must be at least ST_F_TOTAL_FIELDS. If this length is less
1482 * then this value, the function returns 0, otherwise, it
Willy Tarreau708c4162019-10-09 10:19:16 +02001483 * returns 1. <flags> can take the value STAT_SHLGNDS.
William Lallemand74c24fb2016-11-21 17:18:36 +01001484 */
1485int stats_fill_li_stats(struct proxy *px, struct listener *l, int flags,
1486 struct field *stats, int len)
1487{
Willy Tarreau83061a82018-07-13 11:56:34 +02001488 struct buffer *out = get_trash_chunk();
William Lallemand74c24fb2016-11-21 17:18:36 +01001489
1490 if (len < ST_F_TOTAL_FIELDS)
1491 return 0;
1492
1493 if (!l->counters)
1494 return 0;
1495
1496 chunk_reset(out);
1497 memset(stats, 0, sizeof(*stats) * len);
1498
1499 stats[ST_F_PXNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
1500 stats[ST_F_SVNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, l->name);
1501 stats[ST_F_MODE] = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
1502 stats[ST_F_SCUR] = mkf_u32(0, l->nbconn);
1503 stats[ST_F_SMAX] = mkf_u32(FN_MAX, l->counters->conn_max);
1504 stats[ST_F_SLIM] = mkf_u32(FO_CONFIG|FN_LIMIT, l->maxconn);
1505 stats[ST_F_STOT] = mkf_u64(FN_COUNTER, l->counters->cum_conn);
1506 stats[ST_F_BIN] = mkf_u64(FN_COUNTER, l->counters->bytes_in);
1507 stats[ST_F_BOUT] = mkf_u64(FN_COUNTER, l->counters->bytes_out);
1508 stats[ST_F_DREQ] = mkf_u64(FN_COUNTER, l->counters->denied_req);
1509 stats[ST_F_DRESP] = mkf_u64(FN_COUNTER, l->counters->denied_resp);
1510 stats[ST_F_EREQ] = mkf_u64(FN_COUNTER, l->counters->failed_req);
1511 stats[ST_F_DCON] = mkf_u64(FN_COUNTER, l->counters->denied_conn);
1512 stats[ST_F_DSES] = mkf_u64(FN_COUNTER, l->counters->denied_sess);
Willy Tarreaua8cf66b2019-02-27 16:49:00 +01001513 stats[ST_F_STATUS] = mkf_str(FO_STATUS, (!l->maxconn || l->nbconn < l->maxconn) ? (l->state == LI_LIMITED) ? "WAITING" : "OPEN" : "FULL");
William Lallemand74c24fb2016-11-21 17:18:36 +01001514 stats[ST_F_PID] = mkf_u32(FO_KEY, relative_pid);
1515 stats[ST_F_IID] = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
1516 stats[ST_F_SID] = mkf_u32(FO_KEY|FS_SERVICE, l->luid);
1517 stats[ST_F_TYPE] = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_SO);
Tim Duesterhus3fd19732018-05-27 20:35:08 +02001518 stats[ST_F_WREW] = mkf_u64(FN_COUNTER, l->counters->failed_rewrites);
Christopher Faulet0159ee42019-12-16 14:40:39 +01001519 stats[ST_F_EINT] = mkf_u64(FN_COUNTER, l->counters->internal_errors);
William Lallemand74c24fb2016-11-21 17:18:36 +01001520
Willy Tarreau708c4162019-10-09 10:19:16 +02001521 if (flags & STAT_SHLGNDS) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001522 char str[INET6_ADDRSTRLEN];
1523 int port;
1524
1525 port = get_host_port(&l->addr);
1526 switch (addr_to_str(&l->addr, str, sizeof(str))) {
1527 case AF_INET:
1528 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
1529 chunk_appendf(out, "%s:%d", str, port);
1530 break;
1531 case AF_INET6:
1532 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
1533 chunk_appendf(out, "[%s]:%d", str, port);
1534 break;
1535 case AF_UNIX:
1536 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, "unix");
1537 break;
1538 case -1:
1539 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
1540 chunk_strcat(out, strerror(errno));
1541 break;
1542 default: /* address family not supported */
1543 break;
1544 }
1545 }
1546
1547 return 1;
1548}
1549
1550/* Dumps a line for listener <l> and proxy <px> to the trash and uses the state
Willy Tarreau43241ff2019-10-09 11:27:51 +02001551 * from stream interface <si>. The caller is responsible for clearing the trash
1552 * if needed. Returns non-zero if it emits anything, zero otherwise.
William Lallemand74c24fb2016-11-21 17:18:36 +01001553 */
Willy Tarreau43241ff2019-10-09 11:27:51 +02001554static int stats_dump_li_stats(struct stream_interface *si, struct proxy *px, struct listener *l)
William Lallemand74c24fb2016-11-21 17:18:36 +01001555{
1556 struct appctx *appctx = __objt_appctx(si->end);
1557
Willy Tarreau43241ff2019-10-09 11:27:51 +02001558 if (!stats_fill_li_stats(px, l, appctx->ctx.stats.flags, stats, ST_F_TOTAL_FIELDS))
William Lallemand74c24fb2016-11-21 17:18:36 +01001559 return 0;
1560
Willy Tarreau43241ff2019-10-09 11:27:51 +02001561 return stats_dump_one_line(stats, px, appctx);
William Lallemand74c24fb2016-11-21 17:18:36 +01001562}
1563
1564enum srv_stats_state {
1565 SRV_STATS_STATE_DOWN = 0,
1566 SRV_STATS_STATE_DOWN_AGENT,
1567 SRV_STATS_STATE_GOING_UP,
1568 SRV_STATS_STATE_UP_GOING_DOWN,
1569 SRV_STATS_STATE_UP,
1570 SRV_STATS_STATE_NOLB_GOING_DOWN,
1571 SRV_STATS_STATE_NOLB,
1572 SRV_STATS_STATE_DRAIN_GOING_DOWN,
1573 SRV_STATS_STATE_DRAIN,
1574 SRV_STATS_STATE_DRAIN_AGENT,
1575 SRV_STATS_STATE_NO_CHECK,
1576
1577 SRV_STATS_STATE_COUNT, /* Must be last */
1578};
1579
1580static const char *srv_hlt_st[SRV_STATS_STATE_COUNT] = {
1581 [SRV_STATS_STATE_DOWN] = "DOWN",
1582 [SRV_STATS_STATE_DOWN_AGENT] = "DOWN (agent)",
1583 [SRV_STATS_STATE_GOING_UP] = "DOWN %d/%d",
1584 [SRV_STATS_STATE_UP_GOING_DOWN] = "UP %d/%d",
1585 [SRV_STATS_STATE_UP] = "UP",
1586 [SRV_STATS_STATE_NOLB_GOING_DOWN] = "NOLB %d/%d",
1587 [SRV_STATS_STATE_NOLB] = "NOLB",
1588 [SRV_STATS_STATE_DRAIN_GOING_DOWN] = "DRAIN %d/%d",
1589 [SRV_STATS_STATE_DRAIN] = "DRAIN",
1590 [SRV_STATS_STATE_DRAIN_AGENT] = "DRAIN (agent)",
1591 [SRV_STATS_STATE_NO_CHECK] = "no check"
1592};
1593
1594/* Fill <stats> with the server statistics. <stats> is
1595 * preallocated array of length <len>. The length of the array
1596 * must be at least ST_F_TOTAL_FIELDS. If this length is less
1597 * then this value, the function returns 0, otherwise, it
Willy Tarreau708c4162019-10-09 10:19:16 +02001598 * returns 1. <flags> can take the value STAT_SHLGNDS.
William Lallemand74c24fb2016-11-21 17:18:36 +01001599 */
1600int stats_fill_sv_stats(struct proxy *px, struct server *sv, int flags,
1601 struct field *stats, int len)
1602{
1603 struct server *via, *ref;
1604 char str[INET6_ADDRSTRLEN];
Willy Tarreau83061a82018-07-13 11:56:34 +02001605 struct buffer *out = get_trash_chunk();
William Lallemand74c24fb2016-11-21 17:18:36 +01001606 enum srv_stats_state state;
1607 char *fld_status;
Marcin Deraneka8dbdf32020-05-15 20:02:40 +02001608 long long srv_samples_counter;
1609 unsigned int srv_samples_window = TIME_STATS_SAMPLES;
William Lallemand74c24fb2016-11-21 17:18:36 +01001610
1611 if (len < ST_F_TOTAL_FIELDS)
1612 return 0;
1613
1614 memset(stats, 0, sizeof(*stats) * len);
1615
1616 /* we have "via" which is the tracked server as described in the configuration,
1617 * and "ref" which is the checked server and the end of the chain.
1618 */
1619 via = sv->track ? sv->track : sv;
1620 ref = via;
1621 while (ref->track)
1622 ref = ref->track;
1623
Emeric Brun52a91d32017-08-31 14:41:55 +02001624 if (sv->cur_state == SRV_ST_RUNNING || sv->cur_state == SRV_ST_STARTING) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001625 if ((ref->check.state & CHK_ST_ENABLED) &&
1626 (ref->check.health < ref->check.rise + ref->check.fall - 1)) {
1627 state = SRV_STATS_STATE_UP_GOING_DOWN;
1628 } else {
1629 state = SRV_STATS_STATE_UP;
1630 }
1631
Emeric Brun52a91d32017-08-31 14:41:55 +02001632 if (sv->cur_admin & SRV_ADMF_DRAIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001633 if (ref->agent.state & CHK_ST_ENABLED)
1634 state = SRV_STATS_STATE_DRAIN_AGENT;
1635 else if (state == SRV_STATS_STATE_UP_GOING_DOWN)
1636 state = SRV_STATS_STATE_DRAIN_GOING_DOWN;
1637 else
1638 state = SRV_STATS_STATE_DRAIN;
1639 }
1640
1641 if (state == SRV_STATS_STATE_UP && !(ref->check.state & CHK_ST_ENABLED)) {
1642 state = SRV_STATS_STATE_NO_CHECK;
1643 }
1644 }
Emeric Brun52a91d32017-08-31 14:41:55 +02001645 else if (sv->cur_state == SRV_ST_STOPPING) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001646 if ((!(sv->check.state & CHK_ST_ENABLED) && !sv->track) ||
1647 (ref->check.health == ref->check.rise + ref->check.fall - 1)) {
1648 state = SRV_STATS_STATE_NOLB;
1649 } else {
1650 state = SRV_STATS_STATE_NOLB_GOING_DOWN;
1651 }
1652 }
1653 else { /* stopped */
1654 if ((ref->agent.state & CHK_ST_ENABLED) && !ref->agent.health) {
1655 state = SRV_STATS_STATE_DOWN_AGENT;
1656 } else if ((ref->check.state & CHK_ST_ENABLED) && !ref->check.health) {
1657 state = SRV_STATS_STATE_DOWN; /* DOWN */
1658 } else if ((ref->agent.state & CHK_ST_ENABLED) || (ref->check.state & CHK_ST_ENABLED)) {
1659 state = SRV_STATS_STATE_GOING_UP;
1660 } else {
1661 state = SRV_STATS_STATE_DOWN; /* DOWN, unchecked */
1662 }
1663 }
1664
1665 chunk_reset(out);
1666
1667 stats[ST_F_PXNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
1668 stats[ST_F_SVNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, sv->id);
1669 stats[ST_F_MODE] = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
1670 stats[ST_F_QCUR] = mkf_u32(0, sv->nbpend);
1671 stats[ST_F_QMAX] = mkf_u32(FN_MAX, sv->counters.nbpend_max);
1672 stats[ST_F_SCUR] = mkf_u32(0, sv->cur_sess);
1673 stats[ST_F_SMAX] = mkf_u32(FN_MAX, sv->counters.cur_sess_max);
1674
1675 if (sv->maxconn)
1676 stats[ST_F_SLIM] = mkf_u32(FO_CONFIG|FN_LIMIT, sv->maxconn);
1677
Willy Tarreauf21d17b2019-09-08 09:24:56 +02001678 stats[ST_F_SRV_ICUR] = mkf_u32(0, sv->curr_idle_conns);
1679 if (sv->max_idle_conns != -1)
1680 stats[ST_F_SRV_ILIM] = mkf_u32(FO_CONFIG|FN_LIMIT, sv->max_idle_conns);
1681
William Lallemand74c24fb2016-11-21 17:18:36 +01001682 stats[ST_F_STOT] = mkf_u64(FN_COUNTER, sv->counters.cum_sess);
1683 stats[ST_F_BIN] = mkf_u64(FN_COUNTER, sv->counters.bytes_in);
1684 stats[ST_F_BOUT] = mkf_u64(FN_COUNTER, sv->counters.bytes_out);
Christopher Fauleta08546b2019-12-16 16:07:34 +01001685 stats[ST_F_DRESP] = mkf_u64(FN_COUNTER, sv->counters.denied_resp);
William Lallemand74c24fb2016-11-21 17:18:36 +01001686 stats[ST_F_ECON] = mkf_u64(FN_COUNTER, sv->counters.failed_conns);
1687 stats[ST_F_ERESP] = mkf_u64(FN_COUNTER, sv->counters.failed_resp);
1688 stats[ST_F_WRETR] = mkf_u64(FN_COUNTER, sv->counters.retries);
1689 stats[ST_F_WREDIS] = mkf_u64(FN_COUNTER, sv->counters.redispatches);
Tim Duesterhus3fd19732018-05-27 20:35:08 +02001690 stats[ST_F_WREW] = mkf_u64(FN_COUNTER, sv->counters.failed_rewrites);
Christopher Faulet0159ee42019-12-16 14:40:39 +01001691 stats[ST_F_EINT] = mkf_u64(FN_COUNTER, sv->counters.internal_errors);
Willy Tarreauf1573842018-12-14 11:35:36 +01001692 stats[ST_F_CONNECT] = mkf_u64(FN_COUNTER, sv->counters.connect);
1693 stats[ST_F_REUSE] = mkf_u64(FN_COUNTER, sv->counters.reuse);
William Lallemand74c24fb2016-11-21 17:18:36 +01001694
1695 /* status */
1696 fld_status = chunk_newstr(out);
Emeric Brun52a91d32017-08-31 14:41:55 +02001697 if (sv->cur_admin & SRV_ADMF_RMAINT)
William Lallemand74c24fb2016-11-21 17:18:36 +01001698 chunk_appendf(out, "MAINT (resolution)");
Emeric Brun52a91d32017-08-31 14:41:55 +02001699 else if (sv->cur_admin & SRV_ADMF_IMAINT)
William Lallemand74c24fb2016-11-21 17:18:36 +01001700 chunk_appendf(out, "MAINT (via %s/%s)", via->proxy->id, via->id);
Emeric Brun52a91d32017-08-31 14:41:55 +02001701 else if (sv->cur_admin & SRV_ADMF_MAINT)
William Lallemand74c24fb2016-11-21 17:18:36 +01001702 chunk_appendf(out, "MAINT");
1703 else
1704 chunk_appendf(out,
1705 srv_hlt_st[state],
Emeric Brun52a91d32017-08-31 14:41:55 +02001706 (ref->cur_state != SRV_ST_STOPPED) ? (ref->check.health - ref->check.rise + 1) : (ref->check.health),
1707 (ref->cur_state != SRV_ST_STOPPED) ? (ref->check.fall) : (ref->check.rise));
William Lallemand74c24fb2016-11-21 17:18:36 +01001708
1709 stats[ST_F_STATUS] = mkf_str(FO_STATUS, fld_status);
1710 stats[ST_F_LASTCHG] = mkf_u32(FN_AGE, now.tv_sec - sv->last_change);
Emeric Brun52a91d32017-08-31 14:41:55 +02001711 stats[ST_F_WEIGHT] = mkf_u32(FN_AVG, (sv->cur_eweight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv);
William Lallemand74c24fb2016-11-21 17:18:36 +01001712 stats[ST_F_ACT] = mkf_u32(FO_STATUS, (sv->flags & SRV_F_BACKUP) ? 0 : 1);
1713 stats[ST_F_BCK] = mkf_u32(FO_STATUS, (sv->flags & SRV_F_BACKUP) ? 1 : 0);
1714
1715 /* check failures: unique, fatal; last change, total downtime */
1716 if (sv->check.state & CHK_ST_ENABLED) {
1717 stats[ST_F_CHKFAIL] = mkf_u64(FN_COUNTER, sv->counters.failed_checks);
1718 stats[ST_F_CHKDOWN] = mkf_u64(FN_COUNTER, sv->counters.down_trans);
1719 stats[ST_F_DOWNTIME] = mkf_u32(FN_COUNTER, srv_downtime(sv));
1720 }
1721
1722 if (sv->maxqueue)
1723 stats[ST_F_QLIMIT] = mkf_u32(FO_CONFIG|FS_SERVICE, sv->maxqueue);
1724
1725 stats[ST_F_PID] = mkf_u32(FO_KEY, relative_pid);
1726 stats[ST_F_IID] = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
1727 stats[ST_F_SID] = mkf_u32(FO_KEY|FS_SERVICE, sv->puid);
1728
Emeric Brun52a91d32017-08-31 14:41:55 +02001729 if (sv->cur_state == SRV_ST_STARTING && !server_is_draining(sv))
William Lallemand74c24fb2016-11-21 17:18:36 +01001730 stats[ST_F_THROTTLE] = mkf_u32(FN_AVG, server_throttle_rate(sv));
1731
1732 stats[ST_F_LBTOT] = mkf_u64(FN_COUNTER, sv->counters.cum_lbconn);
1733
1734 if (sv->track) {
1735 char *fld_track = chunk_newstr(out);
1736
1737 chunk_appendf(out, "%s/%s", sv->track->proxy->id, sv->track->id);
1738 stats[ST_F_TRACKED] = mkf_str(FO_CONFIG|FN_NAME|FS_SERVICE, fld_track);
1739 }
1740
1741 stats[ST_F_TYPE] = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_SV);
1742 stats[ST_F_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&sv->sess_per_sec));
1743 stats[ST_F_RATE_MAX] = mkf_u32(FN_MAX, sv->counters.sps_max);
1744
1745 if ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED) {
1746 const char *fld_chksts;
1747
1748 fld_chksts = chunk_newstr(out);
1749 chunk_strcat(out, "* "); // for check in progress
1750 chunk_strcat(out, get_check_status_info(sv->check.status));
1751 if (!(sv->check.state & CHK_ST_INPROGRESS))
1752 fld_chksts += 2; // skip "* "
1753 stats[ST_F_CHECK_STATUS] = mkf_str(FN_OUTPUT, fld_chksts);
1754
1755 if (sv->check.status >= HCHK_STATUS_L57DATA)
1756 stats[ST_F_CHECK_CODE] = mkf_u32(FN_OUTPUT, sv->check.code);
1757
1758 if (sv->check.status >= HCHK_STATUS_CHECKED)
1759 stats[ST_F_CHECK_DURATION] = mkf_u64(FN_DURATION, sv->check.duration);
1760
1761 stats[ST_F_CHECK_DESC] = mkf_str(FN_OUTPUT, get_check_status_description(sv->check.status));
1762 stats[ST_F_LAST_CHK] = mkf_str(FN_OUTPUT, sv->check.desc);
1763 stats[ST_F_CHECK_RISE] = mkf_u32(FO_CONFIG|FS_SERVICE, ref->check.rise);
1764 stats[ST_F_CHECK_FALL] = mkf_u32(FO_CONFIG|FS_SERVICE, ref->check.fall);
1765 stats[ST_F_CHECK_HEALTH] = mkf_u32(FO_CONFIG|FS_SERVICE, ref->check.health);
1766 }
1767
1768 if ((sv->agent.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED) {
1769 const char *fld_chksts;
1770
1771 fld_chksts = chunk_newstr(out);
1772 chunk_strcat(out, "* "); // for check in progress
1773 chunk_strcat(out, get_check_status_info(sv->agent.status));
1774 if (!(sv->agent.state & CHK_ST_INPROGRESS))
1775 fld_chksts += 2; // skip "* "
1776 stats[ST_F_AGENT_STATUS] = mkf_str(FN_OUTPUT, fld_chksts);
1777
1778 if (sv->agent.status >= HCHK_STATUS_L57DATA)
1779 stats[ST_F_AGENT_CODE] = mkf_u32(FN_OUTPUT, sv->agent.code);
1780
1781 if (sv->agent.status >= HCHK_STATUS_CHECKED)
1782 stats[ST_F_AGENT_DURATION] = mkf_u64(FN_DURATION, sv->agent.duration);
1783
1784 stats[ST_F_AGENT_DESC] = mkf_str(FN_OUTPUT, get_check_status_description(sv->agent.status));
1785 stats[ST_F_LAST_AGT] = mkf_str(FN_OUTPUT, sv->agent.desc);
1786 stats[ST_F_AGENT_RISE] = mkf_u32(FO_CONFIG|FS_SERVICE, sv->agent.rise);
1787 stats[ST_F_AGENT_FALL] = mkf_u32(FO_CONFIG|FS_SERVICE, sv->agent.fall);
1788 stats[ST_F_AGENT_HEALTH] = mkf_u32(FO_CONFIG|FS_SERVICE, sv->agent.health);
1789 }
1790
1791 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
1792 if (px->mode == PR_MODE_HTTP) {
Marcin Deranek3c27dda2020-05-15 18:32:51 +02001793 stats[ST_F_REQ_TOT] = mkf_u64(FN_COUNTER, sv->counters.p.http.cum_req);
William Lallemand74c24fb2016-11-21 17:18:36 +01001794 stats[ST_F_HRSP_1XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[1]);
1795 stats[ST_F_HRSP_2XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[2]);
1796 stats[ST_F_HRSP_3XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[3]);
1797 stats[ST_F_HRSP_4XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[4]);
1798 stats[ST_F_HRSP_5XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[5]);
1799 stats[ST_F_HRSP_OTHER] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[0]);
1800 }
1801
1802 if (ref->observe)
1803 stats[ST_F_HANAFAIL] = mkf_u64(FN_COUNTER, sv->counters.failed_hana);
1804
1805 stats[ST_F_CLI_ABRT] = mkf_u64(FN_COUNTER, sv->counters.cli_aborts);
1806 stats[ST_F_SRV_ABRT] = mkf_u64(FN_COUNTER, sv->counters.srv_aborts);
1807 stats[ST_F_LASTSESS] = mkf_s32(FN_AGE, srv_lastsession(sv));
1808
Marcin Deraneka8dbdf32020-05-15 20:02:40 +02001809 srv_samples_counter = (px->mode == PR_MODE_HTTP) ? sv->counters.p.http.cum_req : sv->counters.cum_lbconn;
1810 if (srv_samples_counter < TIME_STATS_SAMPLES && srv_samples_counter > 0)
1811 srv_samples_window = srv_samples_counter;
1812
1813 stats[ST_F_QTIME] = mkf_u32(FN_AVG, swrate_avg(sv->counters.q_time, srv_samples_window));
1814 stats[ST_F_CTIME] = mkf_u32(FN_AVG, swrate_avg(sv->counters.c_time, srv_samples_window));
1815 stats[ST_F_RTIME] = mkf_u32(FN_AVG, swrate_avg(sv->counters.d_time, srv_samples_window));
1816 stats[ST_F_TTIME] = mkf_u32(FN_AVG, swrate_avg(sv->counters.t_time, srv_samples_window));
William Lallemand74c24fb2016-11-21 17:18:36 +01001817
Christopher Faulet0d1c2a62019-11-08 14:59:51 +01001818 stats[ST_F_QT_MAX] = mkf_u32(FN_MAX, sv->counters.qtime_max);
1819 stats[ST_F_CT_MAX] = mkf_u32(FN_MAX, sv->counters.ctime_max);
1820 stats[ST_F_RT_MAX] = mkf_u32(FN_MAX, sv->counters.dtime_max);
1821 stats[ST_F_TT_MAX] = mkf_u32(FN_MAX, sv->counters.ttime_max);
1822
Willy Tarreau708c4162019-10-09 10:19:16 +02001823 if (flags & STAT_SHLGNDS) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001824 switch (addr_to_str(&sv->addr, str, sizeof(str))) {
1825 case AF_INET:
1826 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
Willy Tarreau04276f32017-01-06 17:41:29 +01001827 chunk_appendf(out, "%s:%d", str, sv->svc_port);
William Lallemand74c24fb2016-11-21 17:18:36 +01001828 break;
1829 case AF_INET6:
1830 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
Willy Tarreau04276f32017-01-06 17:41:29 +01001831 chunk_appendf(out, "[%s]:%d", str, sv->svc_port);
William Lallemand74c24fb2016-11-21 17:18:36 +01001832 break;
1833 case AF_UNIX:
1834 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, "unix");
1835 break;
1836 case -1:
1837 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
1838 chunk_strcat(out, strerror(errno));
1839 break;
1840 default: /* address family not supported */
1841 break;
1842 }
1843
1844 if (sv->cookie)
1845 stats[ST_F_COOKIE] = mkf_str(FO_CONFIG|FN_NAME|FS_SERVICE, sv->cookie);
1846 }
1847
1848 return 1;
1849}
1850
1851/* Dumps a line for server <sv> and proxy <px> to the trash and uses the state
Willy Tarreau43241ff2019-10-09 11:27:51 +02001852 * from stream interface <si>, and server state <state>. The caller is
1853 * responsible for clearing the trash if needed. Returns non-zero if it emits
1854 * anything, zero otherwise.
William Lallemand74c24fb2016-11-21 17:18:36 +01001855 */
Willy Tarreau43241ff2019-10-09 11:27:51 +02001856static int stats_dump_sv_stats(struct stream_interface *si, struct proxy *px, struct server *sv)
William Lallemand74c24fb2016-11-21 17:18:36 +01001857{
1858 struct appctx *appctx = __objt_appctx(si->end);
1859
Willy Tarreau43241ff2019-10-09 11:27:51 +02001860 if (!stats_fill_sv_stats(px, sv, appctx->ctx.stats.flags, stats, ST_F_TOTAL_FIELDS))
William Lallemand74c24fb2016-11-21 17:18:36 +01001861 return 0;
1862
Willy Tarreau43241ff2019-10-09 11:27:51 +02001863 return stats_dump_one_line(stats, px, appctx);
William Lallemand74c24fb2016-11-21 17:18:36 +01001864}
1865
1866/* Fill <stats> with the backend statistics. <stats> is
1867 * preallocated array of length <len>. The length of the array
1868 * must be at least ST_F_TOTAL_FIELDS. If this length is less
1869 * then this value, the function returns 0, otherwise, it
Willy Tarreau708c4162019-10-09 10:19:16 +02001870 * returns 1. <flags> can take the value STAT_SHLGNDS.
William Lallemand74c24fb2016-11-21 17:18:36 +01001871 */
1872int stats_fill_be_stats(struct proxy *px, int flags, struct field *stats, int len)
1873{
Marcin Deraneka8dbdf32020-05-15 20:02:40 +02001874 long long be_samples_counter;
1875 unsigned int be_samples_window = TIME_STATS_SAMPLES;
1876
William Lallemand74c24fb2016-11-21 17:18:36 +01001877 if (len < ST_F_TOTAL_FIELDS)
1878 return 0;
1879
1880 memset(stats, 0, sizeof(*stats) * len);
1881
1882 stats[ST_F_PXNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
1883 stats[ST_F_SVNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, "BACKEND");
1884 stats[ST_F_MODE] = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
1885 stats[ST_F_QCUR] = mkf_u32(0, px->nbpend);
1886 stats[ST_F_QMAX] = mkf_u32(FN_MAX, px->be_counters.nbpend_max);
Thierry FOURNIER0ff98a42016-12-19 16:50:42 +01001887 stats[ST_F_SCUR] = mkf_u32(0, px->beconn);
William Lallemand74c24fb2016-11-21 17:18:36 +01001888 stats[ST_F_SMAX] = mkf_u32(FN_MAX, px->be_counters.conn_max);
1889 stats[ST_F_SLIM] = mkf_u32(FO_CONFIG|FN_LIMIT, px->fullconn);
1890 stats[ST_F_STOT] = mkf_u64(FN_COUNTER, px->be_counters.cum_conn);
1891 stats[ST_F_BIN] = mkf_u64(FN_COUNTER, px->be_counters.bytes_in);
1892 stats[ST_F_BOUT] = mkf_u64(FN_COUNTER, px->be_counters.bytes_out);
1893 stats[ST_F_DREQ] = mkf_u64(FN_COUNTER, px->be_counters.denied_req);
1894 stats[ST_F_DRESP] = mkf_u64(FN_COUNTER, px->be_counters.denied_resp);
1895 stats[ST_F_ECON] = mkf_u64(FN_COUNTER, px->be_counters.failed_conns);
1896 stats[ST_F_ERESP] = mkf_u64(FN_COUNTER, px->be_counters.failed_resp);
1897 stats[ST_F_WRETR] = mkf_u64(FN_COUNTER, px->be_counters.retries);
1898 stats[ST_F_WREDIS] = mkf_u64(FN_COUNTER, px->be_counters.redispatches);
Tim Duesterhus3fd19732018-05-27 20:35:08 +02001899 stats[ST_F_WREW] = mkf_u64(FN_COUNTER, px->be_counters.failed_rewrites);
Christopher Faulet0159ee42019-12-16 14:40:39 +01001900 stats[ST_F_EINT] = mkf_u64(FN_COUNTER, px->be_counters.internal_errors);
Willy Tarreauf1573842018-12-14 11:35:36 +01001901 stats[ST_F_CONNECT] = mkf_u64(FN_COUNTER, px->be_counters.connect);
1902 stats[ST_F_REUSE] = mkf_u64(FN_COUNTER, px->be_counters.reuse);
William Lallemand74c24fb2016-11-21 17:18:36 +01001903 stats[ST_F_STATUS] = mkf_str(FO_STATUS, (px->lbprm.tot_weight > 0 || !px->srv) ? "UP" : "DOWN");
1904 stats[ST_F_WEIGHT] = mkf_u32(FN_AVG, (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv);
1905 stats[ST_F_ACT] = mkf_u32(0, px->srv_act);
1906 stats[ST_F_BCK] = mkf_u32(0, px->srv_bck);
1907 stats[ST_F_CHKDOWN] = mkf_u64(FN_COUNTER, px->down_trans);
1908 stats[ST_F_LASTCHG] = mkf_u32(FN_AGE, now.tv_sec - px->last_change);
1909 if (px->srv)
1910 stats[ST_F_DOWNTIME] = mkf_u32(FN_COUNTER, be_downtime(px));
1911
1912 stats[ST_F_PID] = mkf_u32(FO_KEY, relative_pid);
1913 stats[ST_F_IID] = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
1914 stats[ST_F_SID] = mkf_u32(FO_KEY|FS_SERVICE, 0);
1915 stats[ST_F_LBTOT] = mkf_u64(FN_COUNTER, px->be_counters.cum_lbconn);
1916 stats[ST_F_TYPE] = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_BE);
1917 stats[ST_F_RATE] = mkf_u32(0, read_freq_ctr(&px->be_sess_per_sec));
1918 stats[ST_F_RATE_MAX] = mkf_u32(0, px->be_counters.sps_max);
1919
Willy Tarreau708c4162019-10-09 10:19:16 +02001920 if (flags & STAT_SHLGNDS) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001921 if (px->cookie_name)
1922 stats[ST_F_COOKIE] = mkf_str(FO_CONFIG|FN_NAME|FS_SERVICE, px->cookie_name);
1923 stats[ST_F_ALGO] = mkf_str(FO_CONFIG|FS_SERVICE, backend_lb_algo_str(px->lbprm.algo & BE_LB_ALGO));
1924 }
1925
1926 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
1927 if (px->mode == PR_MODE_HTTP) {
1928 stats[ST_F_REQ_TOT] = mkf_u64(FN_COUNTER, px->be_counters.p.http.cum_req);
1929 stats[ST_F_HRSP_1XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[1]);
1930 stats[ST_F_HRSP_2XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[2]);
1931 stats[ST_F_HRSP_3XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[3]);
1932 stats[ST_F_HRSP_4XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[4]);
1933 stats[ST_F_HRSP_5XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[5]);
1934 stats[ST_F_HRSP_OTHER] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[0]);
Willy Tarreaua1214a52018-12-14 14:00:25 +01001935 stats[ST_F_CACHE_LOOKUPS] = mkf_u64(FN_COUNTER, px->be_counters.p.http.cache_lookups);
1936 stats[ST_F_CACHE_HITS] = mkf_u64(FN_COUNTER, px->be_counters.p.http.cache_hits);
William Lallemand74c24fb2016-11-21 17:18:36 +01001937 }
1938
1939 stats[ST_F_CLI_ABRT] = mkf_u64(FN_COUNTER, px->be_counters.cli_aborts);
1940 stats[ST_F_SRV_ABRT] = mkf_u64(FN_COUNTER, px->be_counters.srv_aborts);
1941
1942 /* compression: in, out, bypassed, responses */
1943 stats[ST_F_COMP_IN] = mkf_u64(FN_COUNTER, px->be_counters.comp_in);
1944 stats[ST_F_COMP_OUT] = mkf_u64(FN_COUNTER, px->be_counters.comp_out);
1945 stats[ST_F_COMP_BYP] = mkf_u64(FN_COUNTER, px->be_counters.comp_byp);
1946 stats[ST_F_COMP_RSP] = mkf_u64(FN_COUNTER, px->be_counters.p.http.comp_rsp);
1947 stats[ST_F_LASTSESS] = mkf_s32(FN_AGE, be_lastsession(px));
1948
Marcin Deraneka8dbdf32020-05-15 20:02:40 +02001949 be_samples_counter = (px->mode == PR_MODE_HTTP) ? px->be_counters.p.http.cum_req : px->be_counters.cum_lbconn;
1950 if (be_samples_counter < TIME_STATS_SAMPLES && be_samples_counter > 0)
1951 be_samples_window = be_samples_counter;
1952
1953 stats[ST_F_QTIME] = mkf_u32(FN_AVG, swrate_avg(px->be_counters.q_time, be_samples_window));
1954 stats[ST_F_CTIME] = mkf_u32(FN_AVG, swrate_avg(px->be_counters.c_time, be_samples_window));
1955 stats[ST_F_RTIME] = mkf_u32(FN_AVG, swrate_avg(px->be_counters.d_time, be_samples_window));
1956 stats[ST_F_TTIME] = mkf_u32(FN_AVG, swrate_avg(px->be_counters.t_time, be_samples_window));
William Lallemand74c24fb2016-11-21 17:18:36 +01001957
Christopher Faulet0d1c2a62019-11-08 14:59:51 +01001958 stats[ST_F_QT_MAX] = mkf_u32(FN_MAX, px->be_counters.qtime_max);
1959 stats[ST_F_CT_MAX] = mkf_u32(FN_MAX, px->be_counters.ctime_max);
1960 stats[ST_F_RT_MAX] = mkf_u32(FN_MAX, px->be_counters.dtime_max);
1961 stats[ST_F_TT_MAX] = mkf_u32(FN_MAX, px->be_counters.ttime_max);
1962
William Lallemand74c24fb2016-11-21 17:18:36 +01001963 return 1;
1964}
1965
1966/* Dumps a line for backend <px> to the trash for and uses the state from stream
Willy Tarreau43241ff2019-10-09 11:27:51 +02001967 * interface <si>. The caller is responsible for clearing the trash if needed.
1968 * Returns non-zero if it emits anything, zero otherwise.
William Lallemand74c24fb2016-11-21 17:18:36 +01001969 */
Willy Tarreau43241ff2019-10-09 11:27:51 +02001970static int stats_dump_be_stats(struct stream_interface *si, struct proxy *px)
William Lallemand74c24fb2016-11-21 17:18:36 +01001971{
1972 struct appctx *appctx = __objt_appctx(si->end);
1973
1974 if (!(px->cap & PR_CAP_BE))
1975 return 0;
1976
1977 if ((appctx->ctx.stats.flags & STAT_BOUND) && !(appctx->ctx.stats.type & (1 << STATS_TYPE_BE)))
1978 return 0;
1979
Willy Tarreau43241ff2019-10-09 11:27:51 +02001980 if (!stats_fill_be_stats(px, appctx->ctx.stats.flags, stats, ST_F_TOTAL_FIELDS))
William Lallemand74c24fb2016-11-21 17:18:36 +01001981 return 0;
1982
Willy Tarreau43241ff2019-10-09 11:27:51 +02001983 return stats_dump_one_line(stats, px, appctx);
William Lallemand74c24fb2016-11-21 17:18:36 +01001984}
1985
1986/* Dumps the HTML table header for proxy <px> to the trash for and uses the state from
1987 * stream interface <si> and per-uri parameters <uri>. The caller is responsible
1988 * for clearing the trash if needed.
1989 */
Willy Tarreau676c29e2019-10-09 10:50:01 +02001990static void stats_dump_html_px_hdr(struct stream_interface *si, struct proxy *px)
William Lallemand74c24fb2016-11-21 17:18:36 +01001991{
1992 struct appctx *appctx = __objt_appctx(si->end);
1993 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
1994
1995 if (px->cap & PR_CAP_BE && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
1996 /* A form to enable/disable this proxy servers */
1997
1998 /* scope_txt = search pattern + search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
1999 scope_txt[0] = 0;
2000 if (appctx->ctx.stats.scope_len) {
Christopher Fauleted7a0662019-01-14 11:07:34 +01002001 const char *scope_ptr = stats_scope_ptr(appctx, si);
2002
William Lallemand74c24fb2016-11-21 17:18:36 +01002003 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Christopher Fauleted7a0662019-01-14 11:07:34 +01002004 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), scope_ptr, appctx->ctx.stats.scope_len);
William Lallemand74c24fb2016-11-21 17:18:36 +01002005 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
2006 }
2007
2008 chunk_appendf(&trash,
2009 "<form method=\"post\">");
2010 }
2011
2012 /* print a new table */
2013 chunk_appendf(&trash,
2014 "<table class=\"tbl\" width=\"100%%\">\n"
2015 "<tr class=\"titre\">"
2016 "<th class=\"pxname\" width=\"10%%\">");
2017
2018 chunk_appendf(&trash,
2019 "<a name=\"%s\"></a>%s"
2020 "<a class=px href=\"#%s\">%s</a>",
2021 px->id,
Willy Tarreau676c29e2019-10-09 10:50:01 +02002022 (appctx->ctx.stats.flags & STAT_SHLGNDS) ? "<u>":"",
William Lallemand74c24fb2016-11-21 17:18:36 +01002023 px->id, px->id);
2024
Willy Tarreau676c29e2019-10-09 10:50:01 +02002025 if (appctx->ctx.stats.flags & STAT_SHLGNDS) {
William Lallemand74c24fb2016-11-21 17:18:36 +01002026 /* cap, mode, id */
2027 chunk_appendf(&trash, "<div class=tips>cap: %s, mode: %s, id: %d",
2028 proxy_cap_str(px->cap), proxy_mode_str(px->mode),
2029 px->uuid);
2030 chunk_appendf(&trash, "</div>");
2031 }
2032
2033 chunk_appendf(&trash,
2034 "%s</th>"
2035 "<th class=\"%s\" width=\"90%%\">%s</th>"
2036 "</tr>\n"
2037 "</table>\n"
2038 "<table class=\"tbl\" width=\"100%%\">\n"
2039 "<tr class=\"titre\">",
Willy Tarreau676c29e2019-10-09 10:50:01 +02002040 (appctx->ctx.stats.flags & STAT_SHLGNDS) ? "</u>":"",
William Lallemand74c24fb2016-11-21 17:18:36 +01002041 px->desc ? "desc" : "empty", px->desc ? px->desc : "");
2042
Christopher Fauletbc271ec2019-12-02 11:29:04 +01002043 if (appctx->ctx.stats.flags & STAT_ADMIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +01002044 /* Column heading for Enable or Disable server */
Christopher Fauletbc271ec2019-12-02 11:29:04 +01002045 if ((px->cap & PR_CAP_BE) && px->srv)
2046 chunk_appendf(&trash,
2047 "<th rowspan=2 width=1><input type=\"checkbox\" "
2048 "onclick=\"for(c in document.getElementsByClassName('%s-checkbox')) "
2049 "document.getElementsByClassName('%s-checkbox').item(c).checked = this.checked\"></th>",
2050 px->id,
2051 px->id);
2052 else
2053 chunk_appendf(&trash, "<th rowspan=2></th>");
William Lallemand74c24fb2016-11-21 17:18:36 +01002054 }
2055
2056 chunk_appendf(&trash,
2057 "<th rowspan=2></th>"
2058 "<th colspan=3>Queue</th>"
2059 "<th colspan=3>Session rate</th><th colspan=6>Sessions</th>"
2060 "<th colspan=2>Bytes</th><th colspan=2>Denied</th>"
2061 "<th colspan=3>Errors</th><th colspan=2>Warnings</th>"
2062 "<th colspan=9>Server</th>"
2063 "</tr>\n"
2064 "<tr class=\"titre\">"
2065 "<th>Cur</th><th>Max</th><th>Limit</th>"
2066 "<th>Cur</th><th>Max</th><th>Limit</th><th>Cur</th><th>Max</th>"
2067 "<th>Limit</th><th>Total</th><th>LbTot</th><th>Last</th><th>In</th><th>Out</th>"
2068 "<th>Req</th><th>Resp</th><th>Req</th><th>Conn</th>"
2069 "<th>Resp</th><th>Retr</th><th>Redis</th>"
2070 "<th>Status</th><th>LastChk</th><th>Wght</th><th>Act</th>"
2071 "<th>Bck</th><th>Chk</th><th>Dwn</th><th>Dwntme</th>"
2072 "<th>Thrtle</th>\n"
2073 "</tr>");
2074}
2075
2076/* Dumps the HTML table trailer for proxy <px> to the trash for and uses the state from
2077 * stream interface <si>. The caller is responsible for clearing the trash if needed.
2078 */
2079static void stats_dump_html_px_end(struct stream_interface *si, struct proxy *px)
2080{
2081 struct appctx *appctx = __objt_appctx(si->end);
2082 chunk_appendf(&trash, "</table>");
2083
2084 if ((px->cap & PR_CAP_BE) && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
2085 /* close the form used to enable/disable this proxy servers */
2086 chunk_appendf(&trash,
2087 "Choose the action to perform on the checked servers : "
2088 "<select name=action>"
2089 "<option value=\"\"></option>"
2090 "<option value=\"ready\">Set state to READY</option>"
2091 "<option value=\"drain\">Set state to DRAIN</option>"
2092 "<option value=\"maint\">Set state to MAINT</option>"
2093 "<option value=\"dhlth\">Health: disable checks</option>"
2094 "<option value=\"ehlth\">Health: enable checks</option>"
2095 "<option value=\"hrunn\">Health: force UP</option>"
2096 "<option value=\"hnolb\">Health: force NOLB</option>"
2097 "<option value=\"hdown\">Health: force DOWN</option>"
2098 "<option value=\"dagent\">Agent: disable checks</option>"
2099 "<option value=\"eagent\">Agent: enable checks</option>"
2100 "<option value=\"arunn\">Agent: force UP</option>"
2101 "<option value=\"adown\">Agent: force DOWN</option>"
2102 "<option value=\"shutdown\">Kill Sessions</option>"
2103 "</select>"
2104 "<input type=\"hidden\" name=\"b\" value=\"#%d\">"
2105 "&nbsp;<input type=\"submit\" value=\"Apply\">"
2106 "</form>",
2107 px->uuid);
2108 }
2109
2110 chunk_appendf(&trash, "<p>\n");
2111}
2112
2113/*
2114 * Dumps statistics for a proxy. The output is sent to the stream interface's
2115 * input buffer. Returns 0 if it had to stop dumping data because of lack of
2116 * buffer space, or non-zero if everything completed. This function is used
2117 * both by the CLI and the HTTP entry points, and is able to dump the output
2118 * in HTML or CSV formats. If the later, <uri> must be NULL.
2119 */
Christopher Fauletef779222018-10-31 08:47:01 +01002120int stats_dump_proxy_to_buffer(struct stream_interface *si, struct htx *htx,
2121 struct proxy *px, struct uri_auth *uri)
William Lallemand74c24fb2016-11-21 17:18:36 +01002122{
2123 struct appctx *appctx = __objt_appctx(si->end);
2124 struct stream *s = si_strm(si);
2125 struct channel *rep = si_ic(si);
2126 struct server *sv, *svs; /* server and server-state, server-state=server or server->track */
2127 struct listener *l;
William Lallemand74c24fb2016-11-21 17:18:36 +01002128
2129 chunk_reset(&trash);
2130
2131 switch (appctx->ctx.stats.px_st) {
2132 case STAT_PX_ST_INIT:
2133 /* we are on a new proxy */
2134 if (uri && uri->scope) {
2135 /* we have a limited scope, we have to check the proxy name */
2136 struct stat_scope *scope;
2137 int len;
2138
2139 len = strlen(px->id);
2140 scope = uri->scope;
2141
2142 while (scope) {
2143 /* match exact proxy name */
2144 if (scope->px_len == len && !memcmp(px->id, scope->px_id, len))
2145 break;
2146
2147 /* match '.' which means 'self' proxy */
2148 if (!strcmp(scope->px_id, ".") && px == s->be)
2149 break;
2150 scope = scope->next;
2151 }
2152
2153 /* proxy name not found : don't dump anything */
2154 if (scope == NULL)
2155 return 1;
2156 }
2157
2158 /* if the user has requested a limited output and the proxy
2159 * name does not match, skip it.
2160 */
Christopher Fauleted7a0662019-01-14 11:07:34 +01002161 if (appctx->ctx.stats.scope_len) {
2162 const char *scope_ptr = stats_scope_ptr(appctx, si);
2163
2164 if (strnistr(px->id, strlen(px->id), scope_ptr, appctx->ctx.stats.scope_len) == NULL)
2165 return 1;
2166 }
William Lallemand74c24fb2016-11-21 17:18:36 +01002167
2168 if ((appctx->ctx.stats.flags & STAT_BOUND) &&
2169 (appctx->ctx.stats.iid != -1) &&
2170 (px->uuid != appctx->ctx.stats.iid))
2171 return 1;
2172
2173 appctx->ctx.stats.px_st = STAT_PX_ST_TH;
2174 /* fall through */
2175
2176 case STAT_PX_ST_TH:
2177 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreau676c29e2019-10-09 10:50:01 +02002178 stats_dump_html_px_hdr(si, px);
Christopher Fauletef779222018-10-31 08:47:01 +01002179 if (!stats_putchk(rep, htx, &trash))
2180 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002181 }
2182
2183 appctx->ctx.stats.px_st = STAT_PX_ST_FE;
2184 /* fall through */
2185
2186 case STAT_PX_ST_FE:
2187 /* print the frontend */
2188 if (stats_dump_fe_stats(si, px)) {
Christopher Fauletef779222018-10-31 08:47:01 +01002189 if (!stats_putchk(rep, htx, &trash))
2190 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002191 }
2192
2193 appctx->ctx.stats.l = px->conf.listeners.n;
2194 appctx->ctx.stats.px_st = STAT_PX_ST_LI;
2195 /* fall through */
2196
2197 case STAT_PX_ST_LI:
2198 /* stats.l has been initialized above */
2199 for (; appctx->ctx.stats.l != &px->conf.listeners; appctx->ctx.stats.l = l->by_fe.n) {
Christopher Fauletef779222018-10-31 08:47:01 +01002200 if (htx) {
2201 if (htx_almost_full(htx))
2202 goto full;
2203 }
2204 else {
2205 if (buffer_almost_full(&rep->buf))
2206 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002207 }
2208
2209 l = LIST_ELEM(appctx->ctx.stats.l, struct listener *, by_fe);
2210 if (!l->counters)
2211 continue;
2212
2213 if (appctx->ctx.stats.flags & STAT_BOUND) {
2214 if (!(appctx->ctx.stats.type & (1 << STATS_TYPE_SO)))
2215 break;
2216
2217 if (appctx->ctx.stats.sid != -1 && l->luid != appctx->ctx.stats.sid)
2218 continue;
2219 }
2220
2221 /* print the frontend */
Willy Tarreau43241ff2019-10-09 11:27:51 +02002222 if (stats_dump_li_stats(si, px, l)) {
Christopher Fauletef779222018-10-31 08:47:01 +01002223 if (!stats_putchk(rep, htx, &trash))
2224 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002225 }
2226 }
2227
2228 appctx->ctx.stats.sv = px->srv; /* may be NULL */
2229 appctx->ctx.stats.px_st = STAT_PX_ST_SV;
2230 /* fall through */
2231
2232 case STAT_PX_ST_SV:
2233 /* stats.sv has been initialized above */
2234 for (; appctx->ctx.stats.sv != NULL; appctx->ctx.stats.sv = sv->next) {
Christopher Fauletef779222018-10-31 08:47:01 +01002235 if (htx) {
2236 if (htx_almost_full(htx))
2237 goto full;
2238 }
2239 else {
2240 if (buffer_almost_full(&rep->buf))
2241 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002242 }
2243
2244 sv = appctx->ctx.stats.sv;
2245
2246 if (appctx->ctx.stats.flags & STAT_BOUND) {
2247 if (!(appctx->ctx.stats.type & (1 << STATS_TYPE_SV)))
2248 break;
2249
2250 if (appctx->ctx.stats.sid != -1 && sv->puid != appctx->ctx.stats.sid)
2251 continue;
2252 }
2253
2254 svs = sv;
2255 while (svs->track)
2256 svs = svs->track;
2257
2258 /* do not report servers which are DOWN and not changing state */
2259 if ((appctx->ctx.stats.flags & STAT_HIDE_DOWN) &&
Emeric Brun52a91d32017-08-31 14:41:55 +02002260 ((sv->cur_admin & SRV_ADMF_MAINT) || /* server is in maintenance */
2261 (sv->cur_state == SRV_ST_STOPPED && /* server is down */
William Lallemand74c24fb2016-11-21 17:18:36 +01002262 (!((svs->agent.state | svs->check.state) & CHK_ST_ENABLED) ||
2263 ((svs->agent.state & CHK_ST_ENABLED) && !svs->agent.health) ||
2264 ((svs->check.state & CHK_ST_ENABLED) && !svs->check.health))))) {
2265 continue;
2266 }
2267
Willy Tarreau43241ff2019-10-09 11:27:51 +02002268 if (stats_dump_sv_stats(si, px, sv)) {
Christopher Fauletef779222018-10-31 08:47:01 +01002269 if (!stats_putchk(rep, htx, &trash))
2270 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002271 }
2272 } /* for sv */
2273
2274 appctx->ctx.stats.px_st = STAT_PX_ST_BE;
2275 /* fall through */
2276
2277 case STAT_PX_ST_BE:
2278 /* print the backend */
Willy Tarreau43241ff2019-10-09 11:27:51 +02002279 if (stats_dump_be_stats(si, px)) {
Christopher Fauletef779222018-10-31 08:47:01 +01002280 if (!stats_putchk(rep, htx, &trash))
2281 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002282 }
2283
2284 appctx->ctx.stats.px_st = STAT_PX_ST_END;
2285 /* fall through */
2286
2287 case STAT_PX_ST_END:
2288 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
2289 stats_dump_html_px_end(si, px);
Christopher Fauletef779222018-10-31 08:47:01 +01002290 if (!stats_putchk(rep, htx, &trash))
2291 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002292 }
2293
2294 appctx->ctx.stats.px_st = STAT_PX_ST_FIN;
2295 /* fall through */
2296
2297 case STAT_PX_ST_FIN:
2298 return 1;
2299
2300 default:
2301 /* unknown state, we should put an abort() here ! */
2302 return 1;
2303 }
Christopher Fauletef779222018-10-31 08:47:01 +01002304
2305 full:
2306 si_rx_room_blk(si);
2307 return 0;
William Lallemand74c24fb2016-11-21 17:18:36 +01002308}
2309
2310/* Dumps the HTTP stats head block to the trash for and uses the per-uri
2311 * parameters <uri>. The caller is responsible for clearing the trash if needed.
2312 */
Willy Tarreau676c29e2019-10-09 10:50:01 +02002313static void stats_dump_html_head(struct appctx *appctx, struct uri_auth *uri)
William Lallemand74c24fb2016-11-21 17:18:36 +01002314{
2315 /* WARNING! This must fit in the first buffer !!! */
2316 chunk_appendf(&trash,
2317 "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"\n"
2318 "\"http://www.w3.org/TR/html4/loose.dtd\">\n"
2319 "<html><head><title>Statistics Report for " PRODUCT_NAME "%s%s</title>\n"
2320 "<meta http-equiv=\"content-type\" content=\"text/html; charset=iso-8859-1\">\n"
2321 "<style type=\"text/css\"><!--\n"
2322 "body {"
2323 " font-family: arial, helvetica, sans-serif;"
2324 " font-size: 12px;"
2325 " font-weight: normal;"
2326 " color: black;"
2327 " background: white;"
2328 "}\n"
2329 "th,td {"
2330 " font-size: 10px;"
2331 "}\n"
2332 "h1 {"
2333 " font-size: x-large;"
2334 " margin-bottom: 0.5em;"
2335 "}\n"
2336 "h2 {"
2337 " font-family: helvetica, arial;"
2338 " font-size: x-large;"
2339 " font-weight: bold;"
2340 " font-style: italic;"
2341 " color: #6020a0;"
2342 " margin-top: 0em;"
2343 " margin-bottom: 0em;"
2344 "}\n"
2345 "h3 {"
2346 " font-family: helvetica, arial;"
2347 " font-size: 16px;"
2348 " font-weight: bold;"
2349 " color: #b00040;"
2350 " background: #e8e8d0;"
2351 " margin-top: 0em;"
2352 " margin-bottom: 0em;"
2353 "}\n"
2354 "li {"
2355 " margin-top: 0.25em;"
2356 " margin-right: 2em;"
2357 "}\n"
2358 ".hr {margin-top: 0.25em;"
2359 " border-color: black;"
2360 " border-bottom-style: solid;"
2361 "}\n"
2362 ".titre {background: #20D0D0;color: #000000; font-weight: bold; text-align: center;}\n"
2363 ".total {background: #20D0D0;color: #ffff80;}\n"
2364 ".frontend {background: #e8e8d0;}\n"
2365 ".socket {background: #d0d0d0;}\n"
2366 ".backend {background: #e8e8d0;}\n"
2367 ".active_down {background: #ff9090;}\n"
2368 ".active_going_up {background: #ffd020;}\n"
2369 ".active_going_down {background: #ffffa0;}\n"
2370 ".active_up {background: #c0ffc0;}\n"
2371 ".active_nolb {background: #20a0ff;}\n"
2372 ".active_draining {background: #20a0FF;}\n"
2373 ".active_no_check {background: #e0e0e0;}\n"
2374 ".backup_down {background: #ff9090;}\n"
2375 ".backup_going_up {background: #ff80ff;}\n"
2376 ".backup_going_down {background: #c060ff;}\n"
2377 ".backup_up {background: #b0d0ff;}\n"
2378 ".backup_nolb {background: #90b0e0;}\n"
2379 ".backup_draining {background: #cc9900;}\n"
2380 ".backup_no_check {background: #e0e0e0;}\n"
2381 ".maintain {background: #c07820;}\n"
2382 ".rls {letter-spacing: 0.2em; margin-right: 1px;}\n" /* right letter spacing (used for grouping digits) */
2383 "\n"
2384 "a.px:link {color: #ffff40; text-decoration: none;}"
2385 "a.px:visited {color: #ffff40; text-decoration: none;}"
2386 "a.px:hover {color: #ffffff; text-decoration: none;}"
2387 "a.lfsb:link {color: #000000; text-decoration: none;}"
2388 "a.lfsb:visited {color: #000000; text-decoration: none;}"
2389 "a.lfsb:hover {color: #505050; text-decoration: none;}"
2390 "\n"
2391 "table.tbl { border-collapse: collapse; border-style: none;}\n"
2392 "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"
2393 "table.tbl td.ac { text-align: center;}\n"
2394 "table.tbl th { border-width: 1px; border-style: solid solid solid solid; border-color: gray;}\n"
2395 "table.tbl th.pxname { background: #b00040; color: #ffff40; font-weight: bold; border-style: solid solid none solid; padding: 2px 3px; white-space: nowrap;}\n"
2396 "table.tbl th.empty { border-style: none; empty-cells: hide; background: white;}\n"
2397 "table.tbl th.desc { background: white; border-style: solid solid none solid; text-align: left; padding: 2px 3px;}\n"
2398 "\n"
2399 "table.lgd { border-collapse: collapse; border-width: 1px; border-style: none none none solid; border-color: black;}\n"
2400 "table.lgd td { border-width: 1px; border-style: solid solid solid solid; border-color: gray; padding: 2px;}\n"
2401 "table.lgd td.noborder { border-style: none; padding: 2px; white-space: nowrap;}\n"
2402 "table.det { border-collapse: collapse; border-style: none; }\n"
2403 "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"
2404 "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"
2405 "u {text-decoration:none; border-bottom: 1px dotted black;}\n"
2406 "div.tips {\n"
2407 " display:block;\n"
2408 " visibility:hidden;\n"
2409 " z-index:2147483647;\n"
2410 " position:absolute;\n"
2411 " padding:2px 4px 3px;\n"
2412 " background:#f0f060; color:#000000;\n"
2413 " border:1px solid #7040c0;\n"
2414 " white-space:nowrap;\n"
2415 " font-style:normal;font-size:11px;font-weight:normal;\n"
2416 " -moz-border-radius:3px;-webkit-border-radius:3px;border-radius:3px;\n"
2417 " -moz-box-shadow:gray 2px 2px 3px;-webkit-box-shadow:gray 2px 2px 3px;box-shadow:gray 2px 2px 3px;\n"
2418 "}\n"
2419 "u:hover div.tips {visibility:visible;}\n"
2420 "-->\n"
2421 "</style></head>\n",
Willy Tarreau676c29e2019-10-09 10:50:01 +02002422 (appctx->ctx.stats.flags & STAT_SHNODE) ? " on " : "",
Willy Tarreau027d2062020-01-23 11:47:13 +01002423 (appctx->ctx.stats.flags & STAT_SHNODE) ? (uri && uri->node ? uri->node : global.node) : ""
William Lallemand74c24fb2016-11-21 17:18:36 +01002424 );
2425}
2426
2427/* Dumps the HTML stats information block to the trash for and uses the state from
2428 * stream interface <si> and per-uri parameters <uri>. The caller is responsible
2429 * for clearing the trash if needed.
2430 */
2431static void stats_dump_html_info(struct stream_interface *si, struct uri_auth *uri)
2432{
2433 struct appctx *appctx = __objt_appctx(si->end);
2434 unsigned int up = (now.tv_sec - start_date.tv_sec);
2435 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
Christopher Fauleted7a0662019-01-14 11:07:34 +01002436 const char *scope_ptr = stats_scope_ptr(appctx, si);
Willy Tarreau1713c032019-05-23 12:23:55 +02002437 unsigned long long bps = (unsigned long long)read_freq_ctr(&global.out_32bps) * 32;
2438
2439 /* Turn the bytes per second to bits per second and take care of the
2440 * usual ethernet overhead in order to help figure how far we are from
2441 * interface saturation since it's the only case which usually matters.
2442 * For this we count the total size of an Ethernet frame on the wire
2443 * including preamble and IFG (1538) for the largest TCP segment it
2444 * transports (1448 with TCP timestamps). This is not valid for smaller
2445 * packets (under-estimated), but it gives a reasonably accurate
2446 * estimation of how far we are from uplink saturation.
2447 */
2448 bps = bps * 8 * 1538 / 1448;
William Lallemand74c24fb2016-11-21 17:18:36 +01002449
2450 /* WARNING! this has to fit the first packet too.
2451 * We are around 3.5 kB, add adding entries will
2452 * become tricky if we want to support 4kB buffers !
2453 */
2454 chunk_appendf(&trash,
2455 "<body><h1><a href=\"" PRODUCT_URL "\" style=\"text-decoration: none;\">"
2456 PRODUCT_NAME "%s</a></h1>\n"
2457 "<h2>Statistics Report for pid %d%s%s%s%s</h2>\n"
2458 "<hr width=\"100%%\" class=\"hr\">\n"
2459 "<h3>&gt; General process information</h3>\n"
2460 "<table border=0><tr><td align=\"left\" nowrap width=\"1%%\">\n"
Yves Lafon95317282018-02-26 11:10:37 +01002461 "<p><b>pid = </b> %d (process #%d, nbproc = %d, nbthread = %d)<br>\n"
William Lallemand74c24fb2016-11-21 17:18:36 +01002462 "<b>uptime = </b> %dd %dh%02dm%02ds<br>\n"
2463 "<b>system limits:</b> memmax = %s%s; ulimit-n = %d<br>\n"
2464 "<b>maxsock = </b> %d; <b>maxconn = </b> %d; <b>maxpipes = </b> %d<br>\n"
Willy Tarreau1713c032019-05-23 12:23:55 +02002465 "current conns = %d; current pipes = %d/%d; conn rate = %d/sec; bit rate = %.3f %cbps<br>\n"
William Lallemand74c24fb2016-11-21 17:18:36 +01002466 "Running tasks: %d/%d; idle = %d %%<br>\n"
2467 "</td><td align=\"center\" nowrap>\n"
2468 "<table class=\"lgd\"><tr>\n"
2469 "<td class=\"active_up\">&nbsp;</td><td class=\"noborder\">active UP </td>"
2470 "<td class=\"backup_up\">&nbsp;</td><td class=\"noborder\">backup UP </td>"
2471 "</tr><tr>\n"
2472 "<td class=\"active_going_down\"></td><td class=\"noborder\">active UP, going down </td>"
2473 "<td class=\"backup_going_down\"></td><td class=\"noborder\">backup UP, going down </td>"
2474 "</tr><tr>\n"
2475 "<td class=\"active_going_up\"></td><td class=\"noborder\">active DOWN, going up </td>"
2476 "<td class=\"backup_going_up\"></td><td class=\"noborder\">backup DOWN, going up </td>"
2477 "</tr><tr>\n"
2478 "<td class=\"active_down\"></td><td class=\"noborder\">active or backup DOWN &nbsp;</td>"
2479 "<td class=\"active_no_check\"></td><td class=\"noborder\">not checked </td>"
2480 "</tr><tr>\n"
2481 "<td class=\"maintain\"></td><td class=\"noborder\" colspan=\"3\">active or backup DOWN for maintenance (MAINT) &nbsp;</td>"
2482 "</tr><tr>\n"
2483 "<td class=\"active_draining\"></td><td class=\"noborder\" colspan=\"3\">active or backup SOFT STOPPED for maintenance &nbsp;</td>"
2484 "</tr></table>\n"
2485 "Note: \"NOLB\"/\"DRAIN\" = UP with load-balancing disabled."
2486 "</td>"
2487 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
2488 "<b>Display option:</b><ul style=\"margin-top: 0.25em;\">"
2489 "",
Willy Tarreau676c29e2019-10-09 10:50:01 +02002490 (appctx->ctx.stats.flags & STAT_HIDEVER) ? "" : (stats_version_string),
2491 pid, (appctx->ctx.stats.flags & STAT_SHNODE) ? " on " : "",
2492 (appctx->ctx.stats.flags & STAT_SHNODE) ? (uri->node ? uri->node : global.node) : "",
2493 (appctx->ctx.stats.flags & STAT_SHDESC) ? ": " : "",
2494 (appctx->ctx.stats.flags & STAT_SHDESC) ? (uri->desc ? uri->desc : global.desc) : "",
Yves Lafon95317282018-02-26 11:10:37 +01002495 pid, relative_pid, global.nbproc, global.nbthread,
William Lallemand74c24fb2016-11-21 17:18:36 +01002496 up / 86400, (up % 86400) / 3600,
2497 (up % 3600) / 60, (up % 60),
2498 global.rlimit_memmax ? ultoa(global.rlimit_memmax) : "unlimited",
2499 global.rlimit_memmax ? " MB" : "",
2500 global.rlimit_nofile,
2501 global.maxsock, global.maxconn, global.maxpipes,
2502 actconn, pipes_used, pipes_used+pipes_free, read_freq_ctr(&global.conn_per_sec),
Willy Tarreau1713c032019-05-23 12:23:55 +02002503 bps >= 1000000000UL ? (bps / 1000000000.0) : bps >= 1000000UL ? (bps / 1000000.0) : (bps / 1000.0),
2504 bps >= 1000000000UL ? 'G' : bps >= 1000000UL ? 'M' : 'k',
Willy Tarreau81036f22019-05-20 19:24:50 +02002505 tasks_run_queue_cur, nb_tasks_cur, ti->idle_pct
William Lallemand74c24fb2016-11-21 17:18:36 +01002506 );
2507
2508 /* scope_txt = search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
Christopher Fauleted7a0662019-01-14 11:07:34 +01002509 memcpy(scope_txt, scope_ptr, appctx->ctx.stats.scope_len);
William Lallemand74c24fb2016-11-21 17:18:36 +01002510 scope_txt[appctx->ctx.stats.scope_len] = '\0';
2511
2512 chunk_appendf(&trash,
2513 "<li><form method=\"GET\">Scope : <input value=\"%s\" name=\"" STAT_SCOPE_INPUT_NAME "\" size=\"8\" maxlength=\"%d\" tabindex=\"1\"/></form>\n",
2514 (appctx->ctx.stats.scope_len > 0) ? scope_txt : "",
2515 STAT_SCOPE_TXT_MAXLEN);
2516
2517 /* scope_txt = search pattern + search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
2518 scope_txt[0] = 0;
2519 if (appctx->ctx.stats.scope_len) {
2520 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Christopher Fauleted7a0662019-01-14 11:07:34 +01002521 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), scope_ptr, appctx->ctx.stats.scope_len);
William Lallemand74c24fb2016-11-21 17:18:36 +01002522 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
2523 }
2524
2525 if (appctx->ctx.stats.flags & STAT_HIDE_DOWN)
2526 chunk_appendf(&trash,
2527 "<li><a href=\"%s%s%s%s\">Show all servers</a><br>\n",
2528 uri->uri_prefix,
2529 "",
2530 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2531 scope_txt);
2532 else
2533 chunk_appendf(&trash,
2534 "<li><a href=\"%s%s%s%s\">Hide 'DOWN' servers</a><br>\n",
2535 uri->uri_prefix,
2536 ";up",
2537 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2538 scope_txt);
2539
2540 if (uri->refresh > 0) {
2541 if (appctx->ctx.stats.flags & STAT_NO_REFRESH)
2542 chunk_appendf(&trash,
2543 "<li><a href=\"%s%s%s%s\">Enable refresh</a><br>\n",
2544 uri->uri_prefix,
2545 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2546 "",
2547 scope_txt);
2548 else
2549 chunk_appendf(&trash,
2550 "<li><a href=\"%s%s%s%s\">Disable refresh</a><br>\n",
2551 uri->uri_prefix,
2552 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2553 ";norefresh",
2554 scope_txt);
2555 }
2556
2557 chunk_appendf(&trash,
2558 "<li><a href=\"%s%s%s%s\">Refresh now</a><br>\n",
2559 uri->uri_prefix,
2560 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2561 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2562 scope_txt);
2563
2564 chunk_appendf(&trash,
2565 "<li><a href=\"%s;csv%s%s\">CSV export</a><br>\n",
2566 uri->uri_prefix,
2567 (uri->refresh > 0) ? ";norefresh" : "",
2568 scope_txt);
2569
2570 chunk_appendf(&trash,
Christopher Faulet6338a082019-09-09 15:50:54 +02002571 "<li><a href=\"%s;json%s%s\">JSON export</a> (<a href=\"%s;json-schema\">schema</a>)<br>\n",
2572 uri->uri_prefix,
2573 (uri->refresh > 0) ? ";norefresh" : "",
2574 scope_txt, uri->uri_prefix);
2575
2576 chunk_appendf(&trash,
William Lallemand74c24fb2016-11-21 17:18:36 +01002577 "</ul></td>"
2578 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
2579 "<b>External resources:</b><ul style=\"margin-top: 0.25em;\">\n"
2580 "<li><a href=\"" PRODUCT_URL "\">Primary site</a><br>\n"
2581 "<li><a href=\"" PRODUCT_URL_UPD "\">Updates (v" PRODUCT_BRANCH ")</a><br>\n"
2582 "<li><a href=\"" PRODUCT_URL_DOC "\">Online manual</a><br>\n"
2583 "</ul>"
2584 "</td>"
2585 "</tr></table>\n"
2586 ""
2587 );
2588
2589 if (appctx->ctx.stats.st_code) {
2590 switch (appctx->ctx.stats.st_code) {
2591 case STAT_STATUS_DONE:
2592 chunk_appendf(&trash,
2593 "<p><div class=active_up>"
2594 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2595 "Action processed successfully."
2596 "</div>\n", uri->uri_prefix,
2597 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2598 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2599 scope_txt);
2600 break;
2601 case STAT_STATUS_NONE:
2602 chunk_appendf(&trash,
2603 "<p><div class=active_going_down>"
2604 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2605 "Nothing has changed."
2606 "</div>\n", uri->uri_prefix,
2607 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2608 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2609 scope_txt);
2610 break;
2611 case STAT_STATUS_PART:
2612 chunk_appendf(&trash,
2613 "<p><div class=active_going_down>"
2614 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2615 "Action partially processed.<br>"
2616 "Some server names are probably unknown or ambiguous (duplicated names in the backend)."
2617 "</div>\n", uri->uri_prefix,
2618 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2619 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2620 scope_txt);
2621 break;
2622 case STAT_STATUS_ERRP:
2623 chunk_appendf(&trash,
2624 "<p><div class=active_down>"
2625 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2626 "Action not processed because of invalid parameters."
2627 "<ul>"
2628 "<li>The action is maybe unknown.</li>"
Christopher Faulet2f9a41d2019-02-27 15:30:57 +01002629 "<li>Invalid key parameter (empty or too long).</li>"
William Lallemand74c24fb2016-11-21 17:18:36 +01002630 "<li>The backend name is probably unknown or ambiguous (duplicated names).</li>"
2631 "<li>Some server names are probably unknown or ambiguous (duplicated names in the backend).</li>"
2632 "</ul>"
2633 "</div>\n", uri->uri_prefix,
2634 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2635 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2636 scope_txt);
2637 break;
2638 case STAT_STATUS_EXCD:
2639 chunk_appendf(&trash,
2640 "<p><div class=active_down>"
2641 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2642 "<b>Action not processed : the buffer couldn't store all the data.<br>"
2643 "You should retry with less servers at a time.</b>"
2644 "</div>\n", uri->uri_prefix,
2645 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2646 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2647 scope_txt);
2648 break;
2649 case STAT_STATUS_DENY:
2650 chunk_appendf(&trash,
2651 "<p><div class=active_down>"
2652 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2653 "<b>Action denied.</b>"
2654 "</div>\n", uri->uri_prefix,
2655 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2656 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2657 scope_txt);
2658 break;
Christopher Faulet3c2ecf72019-02-27 16:41:27 +01002659 case STAT_STATUS_IVAL:
2660 chunk_appendf(&trash,
2661 "<p><div class=active_down>"
2662 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2663 "<b>Invalid requests (unsupported method or chunked encoded request).</b>"
2664 "</div>\n", uri->uri_prefix,
2665 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2666 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2667 scope_txt);
2668 break;
William Lallemand74c24fb2016-11-21 17:18:36 +01002669 default:
2670 chunk_appendf(&trash,
2671 "<p><div class=active_no_check>"
2672 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2673 "Unexpected result."
2674 "</div>\n", uri->uri_prefix,
2675 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2676 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2677 scope_txt);
2678 }
2679 chunk_appendf(&trash, "<p>\n");
2680 }
2681}
2682
2683/* Dumps the HTML stats trailer block to the trash. The caller is responsible
2684 * for clearing the trash if needed.
2685 */
2686static void stats_dump_html_end()
2687{
2688 chunk_appendf(&trash, "</body></html>\n");
2689}
2690
Simon Horman05ee2132017-01-04 09:37:25 +01002691/* Dumps the stats JSON header to the trash buffer which. The caller is responsible
2692 * for clearing it if needed.
2693 */
2694static void stats_dump_json_header()
2695{
2696 chunk_strcat(&trash, "[");
2697}
2698
2699
2700/* Dumps the JSON stats trailer block to the trash. The caller is responsible
2701 * for clearing the trash if needed.
2702 */
2703static void stats_dump_json_end()
2704{
2705 chunk_strcat(&trash, "]");
2706}
2707
William Lallemand74c24fb2016-11-21 17:18:36 +01002708/* This function dumps statistics onto the stream interface's read buffer in
2709 * either CSV or HTML format. <uri> contains some HTML-specific parameters that
2710 * are ignored for CSV format (hence <uri> may be NULL there). It returns 0 if
2711 * it had to stop writing data and an I/O is needed, 1 if the dump is finished
2712 * and the stream must be closed, or -1 in case of any error. This function is
2713 * used by both the CLI and the HTTP handlers.
2714 */
Christopher Fauletef779222018-10-31 08:47:01 +01002715static int stats_dump_stat_to_buffer(struct stream_interface *si, struct htx *htx,
2716 struct uri_auth *uri)
William Lallemand74c24fb2016-11-21 17:18:36 +01002717{
2718 struct appctx *appctx = __objt_appctx(si->end);
2719 struct channel *rep = si_ic(si);
2720 struct proxy *px;
2721
2722 chunk_reset(&trash);
2723
2724 switch (appctx->st2) {
2725 case STAT_ST_INIT:
2726 appctx->st2 = STAT_ST_HEAD; /* let's start producing data */
2727 /* fall through */
2728
2729 case STAT_ST_HEAD:
2730 if (appctx->ctx.stats.flags & STAT_FMT_HTML)
Willy Tarreau676c29e2019-10-09 10:50:01 +02002731 stats_dump_html_head(appctx, uri);
Christopher Faulet6338a082019-09-09 15:50:54 +02002732 else if (appctx->ctx.stats.flags & STAT_JSON_SCHM)
2733 stats_dump_json_schema(&trash);
Simon Horman05ee2132017-01-04 09:37:25 +01002734 else if (appctx->ctx.stats.flags & STAT_FMT_JSON)
Willy Tarreau9d7fb632017-04-11 07:53:04 +02002735 stats_dump_json_header();
William Lallemand74c24fb2016-11-21 17:18:36 +01002736 else if (!(appctx->ctx.stats.flags & STAT_FMT_TYPED))
2737 stats_dump_csv_header();
2738
Christopher Fauletef779222018-10-31 08:47:01 +01002739 if (!stats_putchk(rep, htx, &trash))
2740 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002741
Christopher Faulet6338a082019-09-09 15:50:54 +02002742 if (appctx->ctx.stats.flags & STAT_JSON_SCHM) {
2743 appctx->st2 = STAT_ST_FIN;
2744 return 1;
2745 }
William Lallemand74c24fb2016-11-21 17:18:36 +01002746 appctx->st2 = STAT_ST_INFO;
2747 /* fall through */
2748
2749 case STAT_ST_INFO:
2750 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
2751 stats_dump_html_info(si, uri);
Christopher Fauletef779222018-10-31 08:47:01 +01002752 if (!stats_putchk(rep, htx, &trash))
2753 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002754 }
2755
Olivier Houchardfbc74e82017-11-24 16:54:05 +01002756 appctx->ctx.stats.px = proxies_list;
William Lallemand74c24fb2016-11-21 17:18:36 +01002757 appctx->ctx.stats.px_st = STAT_PX_ST_INIT;
2758 appctx->st2 = STAT_ST_LIST;
2759 /* fall through */
2760
2761 case STAT_ST_LIST:
2762 /* dump proxies */
2763 while (appctx->ctx.stats.px) {
Christopher Fauletef779222018-10-31 08:47:01 +01002764 if (htx) {
2765 if (htx_almost_full(htx))
2766 goto full;
2767 }
2768 else {
2769 if (buffer_almost_full(&rep->buf))
2770 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002771 }
2772
2773 px = appctx->ctx.stats.px;
2774 /* skip the disabled proxies, global frontend and non-networked ones */
2775 if (px->state != PR_STSTOPPED && px->uuid > 0 && (px->cap & (PR_CAP_FE | PR_CAP_BE)))
Christopher Fauletef779222018-10-31 08:47:01 +01002776 if (stats_dump_proxy_to_buffer(si, htx, px, uri) == 0)
William Lallemand74c24fb2016-11-21 17:18:36 +01002777 return 0;
2778
2779 appctx->ctx.stats.px = px->next;
2780 appctx->ctx.stats.px_st = STAT_PX_ST_INIT;
2781 }
2782 /* here, we just have reached the last proxy */
2783
2784 appctx->st2 = STAT_ST_END;
2785 /* fall through */
2786
2787 case STAT_ST_END:
Simon Horman05ee2132017-01-04 09:37:25 +01002788 if (appctx->ctx.stats.flags & (STAT_FMT_HTML|STAT_FMT_JSON)) {
2789 if (appctx->ctx.stats.flags & STAT_FMT_HTML)
2790 stats_dump_html_end();
2791 else
2792 stats_dump_json_end();
Christopher Fauletef779222018-10-31 08:47:01 +01002793 if (!stats_putchk(rep, htx, &trash))
2794 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002795 }
2796
2797 appctx->st2 = STAT_ST_FIN;
2798 /* fall through */
2799
2800 case STAT_ST_FIN:
2801 return 1;
2802
2803 default:
2804 /* unknown state ! */
2805 appctx->st2 = STAT_ST_FIN;
2806 return -1;
2807 }
Christopher Fauletef779222018-10-31 08:47:01 +01002808
2809 full:
2810 si_rx_room_blk(si);
2811 return 0;
2812
William Lallemand74c24fb2016-11-21 17:18:36 +01002813}
2814
2815/* We reached the stats page through a POST request. The appctx is
2816 * expected to have already been allocated by the caller.
2817 * Parse the posted data and enable/disable servers if necessary.
2818 * Returns 1 if request was parsed or zero if it needs more data.
2819 */
2820static int stats_process_http_post(struct stream_interface *si)
2821{
2822 struct stream *s = si_strm(si);
2823 struct appctx *appctx = objt_appctx(si->end);
2824
2825 struct proxy *px = NULL;
2826 struct server *sv = NULL;
2827
2828 char key[LINESIZE];
2829 int action = ST_ADM_ACTION_NONE;
2830 int reprocess = 0;
2831
2832 int total_servers = 0;
2833 int altered_servers = 0;
2834
2835 char *first_param, *cur_param, *next_param, *end_params;
2836 char *st_cur_param = NULL;
2837 char *st_next_param = NULL;
2838
Christopher Fauleta3618372018-12-13 21:59:56 +01002839 struct buffer *temp = get_trash_chunk();
William Lallemand74c24fb2016-11-21 17:18:36 +01002840
Christopher Fauletb7f88902019-07-15 21:56:43 +02002841 struct htx *htx = htxbuf(&s->req.buf);
2842 struct htx_blk *blk;
Christopher Fauleta3618372018-12-13 21:59:56 +01002843
Christopher Fauletb7f88902019-07-15 21:56:43 +02002844 /* we need more data */
2845 if (s->txn->req.msg_state < HTTP_MSG_DONE) {
2846 /* check if we can receive more */
2847 if (htx_free_data_space(htx) <= global.tune.maxrewrite) {
2848 appctx->ctx.stats.st_code = STAT_STATUS_EXCD;
2849 goto out;
Christopher Fauleta3618372018-12-13 21:59:56 +01002850 }
Christopher Fauletb7f88902019-07-15 21:56:43 +02002851 goto wait;
2852 }
Christopher Fauleta3618372018-12-13 21:59:56 +01002853
Christopher Fauletb7f88902019-07-15 21:56:43 +02002854 /* The request was fully received. Copy data */
2855 blk = htx_get_head_blk(htx);
2856 while (blk) {
2857 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Fauleta3618372018-12-13 21:59:56 +01002858
Christopher Fauletb7f88902019-07-15 21:56:43 +02002859 if (type == HTX_BLK_EOM || type == HTX_BLK_TLR || type == HTX_BLK_EOT)
2860 break;
2861 if (type == HTX_BLK_DATA) {
2862 struct ist v = htx_get_blk_value(htx, blk);
Christopher Fauleta3618372018-12-13 21:59:56 +01002863
Christopher Fauletb7f88902019-07-15 21:56:43 +02002864 if (!chunk_memcat(temp, v.ptr, v.len)) {
Christopher Faulet2f9a41d2019-02-27 15:30:57 +01002865 appctx->ctx.stats.st_code = STAT_STATUS_EXCD;
2866 goto out;
2867 }
Christopher Fauleta3618372018-12-13 21:59:56 +01002868 }
Christopher Fauletb7f88902019-07-15 21:56:43 +02002869 blk = htx_get_next_blk(htx, blk);
William Lallemand74c24fb2016-11-21 17:18:36 +01002870 }
2871
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002872 first_param = temp->area;
Christopher Fauleta3618372018-12-13 21:59:56 +01002873 end_params = temp->area + temp->data;
William Lallemand74c24fb2016-11-21 17:18:36 +01002874 cur_param = next_param = end_params;
2875 *end_params = '\0';
2876
2877 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
2878
2879 /*
2880 * Parse the parameters in reverse order to only store the last value.
2881 * From the html form, the backend and the action are at the end.
2882 */
2883 while (cur_param > first_param) {
2884 char *value;
2885 int poffset, plen;
2886
2887 cur_param--;
2888
2889 if ((*cur_param == '&') || (cur_param == first_param)) {
2890 reprocess_servers:
2891 /* Parse the key */
2892 poffset = (cur_param != first_param ? 1 : 0);
2893 plen = next_param - cur_param + (cur_param == first_param ? 1 : 0);
2894 if ((plen > 0) && (plen <= sizeof(key))) {
2895 strncpy(key, cur_param + poffset, plen);
2896 key[plen - 1] = '\0';
2897 } else {
Christopher Faulet2f9a41d2019-02-27 15:30:57 +01002898 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
William Lallemand74c24fb2016-11-21 17:18:36 +01002899 goto out;
2900 }
2901
2902 /* Parse the value */
2903 value = key;
2904 while (*value != '\0' && *value != '=') {
2905 value++;
2906 }
2907 if (*value == '=') {
2908 /* Ok, a value is found, we can mark the end of the key */
2909 *value++ = '\0';
2910 }
Willy Tarreau62ba9ba2020-04-23 17:54:47 +02002911 if (url_decode(key, 1) < 0 || url_decode(value, 1) < 0)
William Lallemand74c24fb2016-11-21 17:18:36 +01002912 break;
2913
2914 /* Now we can check the key to see what to do */
2915 if (!px && (strcmp(key, "b") == 0)) {
2916 if ((px = proxy_be_by_name(value)) == NULL) {
2917 /* the backend name is unknown or ambiguous (duplicate names) */
2918 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
2919 goto out;
2920 }
2921 }
2922 else if (!action && (strcmp(key, "action") == 0)) {
2923 if (strcmp(value, "ready") == 0) {
2924 action = ST_ADM_ACTION_READY;
2925 }
2926 else if (strcmp(value, "drain") == 0) {
2927 action = ST_ADM_ACTION_DRAIN;
2928 }
2929 else if (strcmp(value, "maint") == 0) {
2930 action = ST_ADM_ACTION_MAINT;
2931 }
2932 else if (strcmp(value, "shutdown") == 0) {
2933 action = ST_ADM_ACTION_SHUTDOWN;
2934 }
2935 else if (strcmp(value, "dhlth") == 0) {
2936 action = ST_ADM_ACTION_DHLTH;
2937 }
2938 else if (strcmp(value, "ehlth") == 0) {
2939 action = ST_ADM_ACTION_EHLTH;
2940 }
2941 else if (strcmp(value, "hrunn") == 0) {
2942 action = ST_ADM_ACTION_HRUNN;
2943 }
2944 else if (strcmp(value, "hnolb") == 0) {
2945 action = ST_ADM_ACTION_HNOLB;
2946 }
2947 else if (strcmp(value, "hdown") == 0) {
2948 action = ST_ADM_ACTION_HDOWN;
2949 }
2950 else if (strcmp(value, "dagent") == 0) {
2951 action = ST_ADM_ACTION_DAGENT;
2952 }
2953 else if (strcmp(value, "eagent") == 0) {
2954 action = ST_ADM_ACTION_EAGENT;
2955 }
2956 else if (strcmp(value, "arunn") == 0) {
2957 action = ST_ADM_ACTION_ARUNN;
2958 }
2959 else if (strcmp(value, "adown") == 0) {
2960 action = ST_ADM_ACTION_ADOWN;
2961 }
2962 /* else these are the old supported methods */
2963 else if (strcmp(value, "disable") == 0) {
2964 action = ST_ADM_ACTION_DISABLE;
2965 }
2966 else if (strcmp(value, "enable") == 0) {
2967 action = ST_ADM_ACTION_ENABLE;
2968 }
2969 else if (strcmp(value, "stop") == 0) {
2970 action = ST_ADM_ACTION_STOP;
2971 }
2972 else if (strcmp(value, "start") == 0) {
2973 action = ST_ADM_ACTION_START;
2974 }
2975 else {
2976 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
2977 goto out;
2978 }
2979 }
2980 else if (strcmp(key, "s") == 0) {
2981 if (!(px && action)) {
2982 /*
2983 * Indicates that we'll need to reprocess the parameters
2984 * as soon as backend and action are known
2985 */
2986 if (!reprocess) {
2987 st_cur_param = cur_param;
2988 st_next_param = next_param;
2989 }
2990 reprocess = 1;
2991 }
2992 else if ((sv = findserver(px, value)) != NULL) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002993 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
William Lallemand74c24fb2016-11-21 17:18:36 +01002994 switch (action) {
2995 case ST_ADM_ACTION_DISABLE:
Emeric Brun52a91d32017-08-31 14:41:55 +02002996 if (!(sv->cur_admin & SRV_ADMF_FMAINT)) {
William Lallemand74c24fb2016-11-21 17:18:36 +01002997 altered_servers++;
2998 total_servers++;
2999 srv_set_admin_flag(sv, SRV_ADMF_FMAINT, "'disable' on stats page");
3000 }
3001 break;
3002 case ST_ADM_ACTION_ENABLE:
Emeric Brun52a91d32017-08-31 14:41:55 +02003003 if (sv->cur_admin & SRV_ADMF_FMAINT) {
William Lallemand74c24fb2016-11-21 17:18:36 +01003004 altered_servers++;
3005 total_servers++;
3006 srv_clr_admin_flag(sv, SRV_ADMF_FMAINT);
3007 }
3008 break;
3009 case ST_ADM_ACTION_STOP:
Emeric Brun52a91d32017-08-31 14:41:55 +02003010 if (!(sv->cur_admin & SRV_ADMF_FDRAIN)) {
William Lallemand74c24fb2016-11-21 17:18:36 +01003011 srv_set_admin_flag(sv, SRV_ADMF_FDRAIN, "'stop' on stats page");
3012 altered_servers++;
3013 total_servers++;
3014 }
3015 break;
3016 case ST_ADM_ACTION_START:
Emeric Brun52a91d32017-08-31 14:41:55 +02003017 if (sv->cur_admin & SRV_ADMF_FDRAIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +01003018 srv_clr_admin_flag(sv, SRV_ADMF_FDRAIN);
3019 altered_servers++;
3020 total_servers++;
3021 }
3022 break;
3023 case ST_ADM_ACTION_DHLTH:
3024 if (sv->check.state & CHK_ST_CONFIGURED) {
3025 sv->check.state &= ~CHK_ST_ENABLED;
3026 altered_servers++;
3027 total_servers++;
3028 }
3029 break;
3030 case ST_ADM_ACTION_EHLTH:
3031 if (sv->check.state & CHK_ST_CONFIGURED) {
3032 sv->check.state |= CHK_ST_ENABLED;
3033 altered_servers++;
3034 total_servers++;
3035 }
3036 break;
3037 case ST_ADM_ACTION_HRUNN:
3038 if (!(sv->track)) {
3039 sv->check.health = sv->check.rise + sv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02003040 srv_set_running(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01003041 altered_servers++;
3042 total_servers++;
3043 }
3044 break;
3045 case ST_ADM_ACTION_HNOLB:
3046 if (!(sv->track)) {
3047 sv->check.health = sv->check.rise + sv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02003048 srv_set_stopping(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01003049 altered_servers++;
3050 total_servers++;
3051 }
3052 break;
3053 case ST_ADM_ACTION_HDOWN:
3054 if (!(sv->track)) {
3055 sv->check.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02003056 srv_set_stopped(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01003057 altered_servers++;
3058 total_servers++;
3059 }
3060 break;
3061 case ST_ADM_ACTION_DAGENT:
3062 if (sv->agent.state & CHK_ST_CONFIGURED) {
3063 sv->agent.state &= ~CHK_ST_ENABLED;
3064 altered_servers++;
3065 total_servers++;
3066 }
3067 break;
3068 case ST_ADM_ACTION_EAGENT:
3069 if (sv->agent.state & CHK_ST_CONFIGURED) {
3070 sv->agent.state |= CHK_ST_ENABLED;
3071 altered_servers++;
3072 total_servers++;
3073 }
3074 break;
3075 case ST_ADM_ACTION_ARUNN:
3076 if (sv->agent.state & CHK_ST_ENABLED) {
3077 sv->agent.health = sv->agent.rise + sv->agent.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02003078 srv_set_running(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01003079 altered_servers++;
3080 total_servers++;
3081 }
3082 break;
3083 case ST_ADM_ACTION_ADOWN:
3084 if (sv->agent.state & CHK_ST_ENABLED) {
3085 sv->agent.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02003086 srv_set_stopped(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01003087 altered_servers++;
3088 total_servers++;
3089 }
3090 break;
3091 case ST_ADM_ACTION_READY:
3092 srv_adm_set_ready(sv);
3093 altered_servers++;
3094 total_servers++;
3095 break;
3096 case ST_ADM_ACTION_DRAIN:
3097 srv_adm_set_drain(sv);
3098 altered_servers++;
3099 total_servers++;
3100 break;
3101 case ST_ADM_ACTION_MAINT:
3102 srv_adm_set_maint(sv);
3103 altered_servers++;
3104 total_servers++;
3105 break;
3106 case ST_ADM_ACTION_SHUTDOWN:
3107 if (px->state != PR_STSTOPPED) {
Willy Tarreauaf7ea812019-11-14 16:42:04 +01003108 srv_shutdown_streams(sv, SF_ERR_KILLED);
William Lallemand74c24fb2016-11-21 17:18:36 +01003109 altered_servers++;
3110 total_servers++;
3111 }
3112 break;
3113 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003114 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
William Lallemand74c24fb2016-11-21 17:18:36 +01003115 } else {
3116 /* the server name is unknown or ambiguous (duplicate names) */
3117 total_servers++;
3118 }
3119 }
3120 if (reprocess && px && action) {
3121 /* Now, we know the backend and the action chosen by the user.
3122 * We can safely restart from the first server parameter
3123 * to reprocess them
3124 */
3125 cur_param = st_cur_param;
3126 next_param = st_next_param;
3127 reprocess = 0;
3128 goto reprocess_servers;
3129 }
3130
3131 next_param = cur_param;
3132 }
3133 }
3134
3135 if (total_servers == 0) {
3136 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
3137 }
3138 else if (altered_servers == 0) {
3139 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
3140 }
3141 else if (altered_servers == total_servers) {
3142 appctx->ctx.stats.st_code = STAT_STATUS_DONE;
3143 }
3144 else {
3145 appctx->ctx.stats.st_code = STAT_STATUS_PART;
3146 }
3147 out:
3148 return 1;
Christopher Faulet2f9a41d2019-02-27 15:30:57 +01003149 wait:
3150 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
3151 return 0;
Christopher Fauletef779222018-10-31 08:47:01 +01003152}
3153
3154
Christopher Fauletb7f88902019-07-15 21:56:43 +02003155static int stats_send_http_headers(struct stream_interface *si, struct htx *htx)
Christopher Fauletef779222018-10-31 08:47:01 +01003156{
3157 struct stream *s = si_strm(si);
3158 struct uri_auth *uri = s->be->uri_auth;
3159 struct appctx *appctx = __objt_appctx(si->end);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003160 struct htx_sl *sl;
3161 unsigned int flags;
Christopher Fauletef779222018-10-31 08:47:01 +01003162
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003163 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);
3164 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), ist("200"), ist("OK"));
3165 if (!sl)
Christopher Fauletef779222018-10-31 08:47:01 +01003166 goto full;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003167 sl->info.res.status = 200;
Christopher Fauletef779222018-10-31 08:47:01 +01003168
Christopher Fauletb829f4c2019-03-29 16:13:55 +01003169 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")))
Christopher Fauletef779222018-10-31 08:47:01 +01003170 goto full;
3171 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
3172 if (!htx_add_header(htx, ist("Content-Type"), ist("text/html")))
3173 goto full;
3174 }
Christopher Faulet6338a082019-09-09 15:50:54 +02003175 else if (appctx->ctx.stats.flags & (STAT_FMT_JSON|STAT_JSON_SCHM)) {
3176 if (!htx_add_header(htx, ist("Content-Type"), ist("application/json")))
3177 goto full;
3178 }
Christopher Fauletef779222018-10-31 08:47:01 +01003179 else {
3180 if (!htx_add_header(htx, ist("Content-Type"), ist("text/plain")))
3181 goto full;
3182 }
3183
3184 if (uri->refresh > 0 && !(appctx->ctx.stats.flags & STAT_NO_REFRESH)) {
3185 const char *refresh = U2A(uri->refresh);
3186 if (!htx_add_header(htx, ist("Refresh"), ist2(refresh, strlen(refresh))))
3187 goto full;
3188 }
3189
3190 if (appctx->ctx.stats.flags & STAT_CHUNKED) {
3191 if (!htx_add_header(htx, ist("Transfer-Encoding"), ist("chunked")))
3192 goto full;
3193 }
3194
3195 if (!htx_add_endof(htx, HTX_BLK_EOH))
3196 goto full;
3197
Christopher Faulet1e2d6362019-02-27 16:28:48 +01003198 channel_add_input(&s->res, htx->data);
Christopher Fauletef779222018-10-31 08:47:01 +01003199 return 1;
3200
3201 full:
3202 htx_reset(htx);
3203 si_rx_room_blk(si);
3204 return 0;
William Lallemand74c24fb2016-11-21 17:18:36 +01003205}
3206
Christopher Fauletef779222018-10-31 08:47:01 +01003207
Christopher Fauletb7f88902019-07-15 21:56:43 +02003208static int stats_send_http_redirect(struct stream_interface *si, struct htx *htx)
Christopher Fauletef779222018-10-31 08:47:01 +01003209{
3210 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
3211 struct stream *s = si_strm(si);
3212 struct uri_auth *uri = s->be->uri_auth;
3213 struct appctx *appctx = __objt_appctx(si->end);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003214 struct htx_sl *sl;
3215 unsigned int flags;
Christopher Fauletef779222018-10-31 08:47:01 +01003216
3217 /* scope_txt = search pattern + search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
3218 scope_txt[0] = 0;
3219 if (appctx->ctx.stats.scope_len) {
Christopher Fauleted7a0662019-01-14 11:07:34 +01003220 const char *scope_ptr = stats_scope_ptr(appctx, si);
3221
Christopher Fauletef779222018-10-31 08:47:01 +01003222 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Christopher Fauleted7a0662019-01-14 11:07:34 +01003223 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), scope_ptr, appctx->ctx.stats.scope_len);
Christopher Fauletef779222018-10-31 08:47:01 +01003224 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
3225 }
3226
3227 /* We don't want to land on the posted stats page because a refresh will
3228 * repost the data. We don't want this to happen on accident so we redirect
3229 * the browse to the stats page with a GET.
3230 */
3231 chunk_printf(&trash, "%s;st=%s%s%s%s",
3232 uri->uri_prefix,
3233 ((appctx->ctx.stats.st_code > STAT_STATUS_INIT) &&
3234 (appctx->ctx.stats.st_code < STAT_STATUS_SIZE) &&
3235 stat_status_codes[appctx->ctx.stats.st_code]) ?
3236 stat_status_codes[appctx->ctx.stats.st_code] :
3237 stat_status_codes[STAT_STATUS_UNKN],
3238 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3239 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
3240 scope_txt);
3241
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003242 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_CHNK);
3243 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), ist("303"), ist("See Other"));
3244 if (!sl)
Christopher Fauletef779222018-10-31 08:47:01 +01003245 goto full;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003246 sl->info.res.status = 303;
Christopher Fauletef779222018-10-31 08:47:01 +01003247
3248 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")) ||
Christopher Fauletef779222018-10-31 08:47:01 +01003249 !htx_add_header(htx, ist("Content-Type"), ist("text/plain")) ||
3250 !htx_add_header(htx, ist("Content-Length"), ist("0")) ||
3251 !htx_add_header(htx, ist("Location"), ist2(trash.area, trash.data)))
3252 goto full;
3253
3254 if (!htx_add_endof(htx, HTX_BLK_EOH))
3255 goto full;
3256
Christopher Faulet1e2d6362019-02-27 16:28:48 +01003257 channel_add_input(&s->res, htx->data);
Christopher Fauletef779222018-10-31 08:47:01 +01003258 return 1;
3259
3260full:
3261 htx_reset(htx);
3262 si_rx_room_blk(si);
3263 return 0;
3264}
William Lallemand74c24fb2016-11-21 17:18:36 +01003265
Simon Horman05ee2132017-01-04 09:37:25 +01003266
William Lallemand74c24fb2016-11-21 17:18:36 +01003267/* This I/O handler runs as an applet embedded in a stream interface. It is
3268 * used to send HTTP stats over a TCP socket. The mechanism is very simple.
3269 * appctx->st0 contains the operation in progress (dump, done). The handler
3270 * automatically unregisters itself once transfer is complete.
3271 */
Christopher Fauletb7f88902019-07-15 21:56:43 +02003272static void http_stats_io_handler(struct appctx *appctx)
Christopher Fauletef779222018-10-31 08:47:01 +01003273{
3274 struct stream_interface *si = appctx->owner;
3275 struct stream *s = si_strm(si);
3276 struct channel *req = si_oc(si);
3277 struct channel *res = si_ic(si);
3278 struct htx *req_htx, *res_htx;
3279
3280 res_htx = htx_from_buf(&res->buf);
3281
3282 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
3283 goto out;
3284
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05003285 /* Check if the input buffer is available. */
Christopher Fauletef779222018-10-31 08:47:01 +01003286 if (!b_size(&res->buf)) {
3287 si_rx_room_blk(si);
3288 goto out;
3289 }
3290
3291 /* check that the output is not closed */
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003292 if (res->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_SHUTR))
3293 appctx->st0 = STAT_HTTP_END;
Christopher Fauletef779222018-10-31 08:47:01 +01003294
3295 /* all states are processed in sequence */
3296 if (appctx->st0 == STAT_HTTP_HEAD) {
Christopher Fauletb7f88902019-07-15 21:56:43 +02003297 if (stats_send_http_headers(si, res_htx)) {
Christopher Fauletef779222018-10-31 08:47:01 +01003298 if (s->txn->meth == HTTP_METH_HEAD)
3299 appctx->st0 = STAT_HTTP_DONE;
3300 else
3301 appctx->st0 = STAT_HTTP_DUMP;
3302 }
3303 }
3304
3305 if (appctx->st0 == STAT_HTTP_DUMP) {
3306 if (stats_dump_stat_to_buffer(si, res_htx, s->be->uri_auth))
3307 appctx->st0 = STAT_HTTP_DONE;
3308 }
3309
3310 if (appctx->st0 == STAT_HTTP_POST) {
3311 if (stats_process_http_post(si))
3312 appctx->st0 = STAT_HTTP_LAST;
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003313 else if (req->flags & CF_SHUTR)
Christopher Fauletef779222018-10-31 08:47:01 +01003314 appctx->st0 = STAT_HTTP_DONE;
3315 }
3316
3317 if (appctx->st0 == STAT_HTTP_LAST) {
Christopher Fauletb7f88902019-07-15 21:56:43 +02003318 if (stats_send_http_redirect(si, res_htx))
Christopher Fauletef779222018-10-31 08:47:01 +01003319 appctx->st0 = STAT_HTTP_DONE;
3320 }
3321
3322 if (appctx->st0 == STAT_HTTP_DONE) {
Christopher Faulet54b5e212019-06-04 10:08:28 +02003323 /* Don't add TLR because mux-h1 will take care of it */
Christopher Fauletef779222018-10-31 08:47:01 +01003324 if (!htx_add_endof(res_htx, HTX_BLK_EOM)) {
3325 si_rx_room_blk(si);
3326 goto out;
3327 }
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003328 channel_add_input(&s->res, 1);
3329 appctx->st0 = STAT_HTTP_END;
Christopher Fauletef779222018-10-31 08:47:01 +01003330 }
3331
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003332 if (appctx->st0 == STAT_HTTP_END) {
3333 if (!(res->flags & CF_SHUTR)) {
Christopher Fauletef779222018-10-31 08:47:01 +01003334 res->flags |= CF_READ_NULL;
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003335 si_shutr(si);
3336 }
3337
3338 /* eat the whole request */
3339 if (co_data(req)) {
3340 req_htx = htx_from_buf(&req->buf);
3341 co_htx_skip(req, req_htx, co_data(req));
3342 htx_to_buf(req_htx, &req->buf);
Christopher Fauletef779222018-10-31 08:47:01 +01003343 }
3344 }
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003345
Christopher Fauletef779222018-10-31 08:47:01 +01003346 out:
3347 /* we have left the request in the buffer for the case where we
3348 * process a POST, and this automatically re-enables activity on
3349 * read. It's better to indicate that we want to stop reading when
3350 * we're sending, so that we know there's at most one direction
3351 * deciding to wake the applet up. It saves it from looping when
3352 * emitting large blocks into small TCP windows.
3353 */
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003354 htx_to_buf(res_htx, &res->buf);
3355 if (!channel_is_empty(res))
Christopher Fauletef779222018-10-31 08:47:01 +01003356 si_stop_get(si);
Christopher Fauletef779222018-10-31 08:47:01 +01003357}
3358
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003359/* Dump all fields from <info> into <out> using the "show info" format (name: value) */
Willy Tarreau83061a82018-07-13 11:56:34 +02003360static int stats_dump_info_fields(struct buffer *out,
Willy Tarreau43241ff2019-10-09 11:27:51 +02003361 const struct field *info, unsigned int flags)
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003362{
3363 int field;
3364
3365 for (field = 0; field < INF_TOTAL_FIELDS; field++) {
3366 if (!field_format(info, field))
3367 continue;
3368
Willy Tarreaueaa55372019-10-09 07:39:11 +02003369 if (!chunk_appendf(out, "%s: ", info_fields[field].name))
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003370 return 0;
3371 if (!stats_emit_raw_data_field(out, &info[field]))
3372 return 0;
Willy Tarreau6b19b142019-10-09 15:44:21 +02003373 if ((flags & STAT_SHOW_FDESC) && !chunk_appendf(out, ":\"%s\"", info_fields[field].desc))
3374 return 0;
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003375 if (!chunk_strcat(out, "\n"))
3376 return 0;
3377 }
3378 return 1;
3379}
3380
3381/* Dump all fields from <info> into <out> using the "show info typed" format */
Willy Tarreau83061a82018-07-13 11:56:34 +02003382static int stats_dump_typed_info_fields(struct buffer *out,
Willy Tarreau43241ff2019-10-09 11:27:51 +02003383 const struct field *info, unsigned int flags)
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003384{
3385 int field;
3386
3387 for (field = 0; field < INF_TOTAL_FIELDS; field++) {
3388 if (!field_format(info, field))
3389 continue;
3390
Willy Tarreaueaa55372019-10-09 07:39:11 +02003391 if (!chunk_appendf(out, "%d.%s.%u:", field, info_fields[field].name, info[INF_PROCESS_NUM].u.u32))
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003392 return 0;
3393 if (!stats_emit_field_tags(out, &info[field], ':'))
3394 return 0;
3395 if (!stats_emit_typed_data_field(out, &info[field]))
3396 return 0;
Willy Tarreau6b19b142019-10-09 15:44:21 +02003397 if ((flags & STAT_SHOW_FDESC) && !chunk_appendf(out, ":\"%s\"", info_fields[field].desc))
3398 return 0;
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003399 if (!chunk_strcat(out, "\n"))
3400 return 0;
3401 }
3402 return 1;
3403}
3404
3405/* Fill <info> with HAProxy global info. <info> is preallocated
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05003406 * array of length <len>. The length of the array must be
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003407 * INF_TOTAL_FIELDS. If this length is less then this value, the
3408 * function returns 0, otherwise, it returns 1.
3409 */
3410int stats_fill_info(struct field *info, int len)
3411{
3412 unsigned int up = (now.tv_sec - start_date.tv_sec);
Willy Tarreau83061a82018-07-13 11:56:34 +02003413 struct buffer *out = get_trash_chunk();
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003414
3415#ifdef USE_OPENSSL
3416 int ssl_sess_rate = read_freq_ctr(&global.ssl_per_sec);
3417 int ssl_key_rate = read_freq_ctr(&global.ssl_fe_keys_per_sec);
3418 int ssl_reuse = 0;
3419
3420 if (ssl_key_rate < ssl_sess_rate) {
3421 /* count the ssl reuse ratio and avoid overflows in both directions */
3422 ssl_reuse = 100 - (100 * ssl_key_rate + (ssl_sess_rate - 1) / 2) / ssl_sess_rate;
3423 }
3424#endif
3425
3426 if (len < INF_TOTAL_FIELDS)
3427 return 0;
3428
3429 chunk_reset(out);
3430 memset(info, 0, sizeof(*info) * len);
3431
3432 info[INF_NAME] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, PRODUCT_NAME);
Willy Tarreau909b9d82019-01-04 18:20:32 +01003433 info[INF_VERSION] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, haproxy_version);
3434 info[INF_RELEASE_DATE] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, haproxy_date);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003435
Yves Lafon95317282018-02-26 11:10:37 +01003436 info[INF_NBTHREAD] = mkf_u32(FO_CONFIG|FS_SERVICE, global.nbthread);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003437 info[INF_NBPROC] = mkf_u32(FO_CONFIG|FS_SERVICE, global.nbproc);
3438 info[INF_PROCESS_NUM] = mkf_u32(FO_KEY, relative_pid);
3439 info[INF_PID] = mkf_u32(FO_STATUS, pid);
3440
3441 info[INF_UPTIME] = mkf_str(FN_DURATION, chunk_newstr(out));
3442 chunk_appendf(out, "%ud %uh%02um%02us", up / 86400, (up % 86400) / 3600, (up % 3600) / 60, (up % 60));
3443
3444 info[INF_UPTIME_SEC] = mkf_u32(FN_DURATION, up);
3445 info[INF_MEMMAX_MB] = mkf_u32(FO_CONFIG|FN_LIMIT, global.rlimit_memmax);
3446 info[INF_POOL_ALLOC_MB] = mkf_u32(0, (unsigned)(pool_total_allocated() / 1048576L));
3447 info[INF_POOL_USED_MB] = mkf_u32(0, (unsigned)(pool_total_used() / 1048576L));
3448 info[INF_POOL_FAILED] = mkf_u32(FN_COUNTER, pool_total_failures());
3449 info[INF_ULIMIT_N] = mkf_u32(FO_CONFIG|FN_LIMIT, global.rlimit_nofile);
3450 info[INF_MAXSOCK] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxsock);
3451 info[INF_MAXCONN] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxconn);
3452 info[INF_HARD_MAXCONN] = mkf_u32(FO_CONFIG|FN_LIMIT, global.hardmaxconn);
3453 info[INF_CURR_CONN] = mkf_u32(0, actconn);
3454 info[INF_CUM_CONN] = mkf_u32(FN_COUNTER, totalconn);
3455 info[INF_CUM_REQ] = mkf_u32(FN_COUNTER, global.req_count);
3456#ifdef USE_OPENSSL
3457 info[INF_MAX_SSL_CONNS] = mkf_u32(FN_MAX, global.maxsslconn);
3458 info[INF_CURR_SSL_CONNS] = mkf_u32(0, sslconns);
3459 info[INF_CUM_SSL_CONNS] = mkf_u32(FN_COUNTER, totalsslconns);
3460#endif
3461 info[INF_MAXPIPES] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxpipes);
3462 info[INF_PIPES_USED] = mkf_u32(0, pipes_used);
3463 info[INF_PIPES_FREE] = mkf_u32(0, pipes_free);
3464 info[INF_CONN_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&global.conn_per_sec));
3465 info[INF_CONN_RATE_LIMIT] = mkf_u32(FO_CONFIG|FN_LIMIT, global.cps_lim);
3466 info[INF_MAX_CONN_RATE] = mkf_u32(FN_MAX, global.cps_max);
3467 info[INF_SESS_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&global.sess_per_sec));
3468 info[INF_SESS_RATE_LIMIT] = mkf_u32(FO_CONFIG|FN_LIMIT, global.sps_lim);
3469 info[INF_MAX_SESS_RATE] = mkf_u32(FN_RATE, global.sps_max);
3470
3471#ifdef USE_OPENSSL
3472 info[INF_SSL_RATE] = mkf_u32(FN_RATE, ssl_sess_rate);
3473 info[INF_SSL_RATE_LIMIT] = mkf_u32(FO_CONFIG|FN_LIMIT, global.ssl_lim);
3474 info[INF_MAX_SSL_RATE] = mkf_u32(FN_MAX, global.ssl_max);
3475 info[INF_SSL_FRONTEND_KEY_RATE] = mkf_u32(0, ssl_key_rate);
3476 info[INF_SSL_FRONTEND_MAX_KEY_RATE] = mkf_u32(FN_MAX, global.ssl_fe_keys_max);
3477 info[INF_SSL_FRONTEND_SESSION_REUSE_PCT] = mkf_u32(0, ssl_reuse);
3478 info[INF_SSL_BACKEND_KEY_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&global.ssl_be_keys_per_sec));
3479 info[INF_SSL_BACKEND_MAX_KEY_RATE] = mkf_u32(FN_MAX, global.ssl_be_keys_max);
3480 info[INF_SSL_CACHE_LOOKUPS] = mkf_u32(FN_COUNTER, global.shctx_lookups);
3481 info[INF_SSL_CACHE_MISSES] = mkf_u32(FN_COUNTER, global.shctx_misses);
3482#endif
3483 info[INF_COMPRESS_BPS_IN] = mkf_u32(FN_RATE, read_freq_ctr(&global.comp_bps_in));
3484 info[INF_COMPRESS_BPS_OUT] = mkf_u32(FN_RATE, read_freq_ctr(&global.comp_bps_out));
3485 info[INF_COMPRESS_BPS_RATE_LIM] = mkf_u32(FO_CONFIG|FN_LIMIT, global.comp_rate_lim);
3486#ifdef USE_ZLIB
3487 info[INF_ZLIB_MEM_USAGE] = mkf_u32(0, zlib_used_memory);
3488 info[INF_MAX_ZLIB_MEM_USAGE] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxzlibmem);
3489#endif
3490 info[INF_TASKS] = mkf_u32(0, nb_tasks_cur);
Christopher Faulet34c5cc92016-12-06 09:15:30 +01003491 info[INF_RUN_QUEUE] = mkf_u32(0, tasks_run_queue_cur);
Willy Tarreau81036f22019-05-20 19:24:50 +02003492 info[INF_IDLE_PCT] = mkf_u32(FN_AVG, ti->idle_pct);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003493 info[INF_NODE] = mkf_str(FO_CONFIG|FN_OUTPUT|FS_SERVICE, global.node);
3494 if (global.desc)
3495 info[INF_DESCRIPTION] = mkf_str(FO_CONFIG|FN_OUTPUT|FS_SERVICE, global.desc);
Willy Tarreau00098ea2018-11-05 14:38:13 +01003496 info[INF_STOPPING] = mkf_u32(0, stopping);
3497 info[INF_JOBS] = mkf_u32(0, jobs);
William Lallemanda7199262018-11-16 16:57:20 +01003498 info[INF_UNSTOPPABLE_JOBS] = mkf_u32(0, unstoppable_jobs);
Willy Tarreau00098ea2018-11-05 14:38:13 +01003499 info[INF_LISTENERS] = mkf_u32(0, listeners);
Willy Tarreau199ad242018-11-05 16:31:22 +01003500 info[INF_ACTIVE_PEERS] = mkf_u32(0, active_peers);
Willy Tarreau2d372c22018-11-05 17:12:27 +01003501 info[INF_CONNECTED_PEERS] = mkf_u32(0, connected_peers);
Willy Tarreau13ef7732018-11-12 07:25:28 +01003502 info[INF_DROPPED_LOGS] = mkf_u32(0, dropped_logs);
Willy Tarreaubeb859a2018-11-22 18:07:59 +01003503 info[INF_BUSY_POLLING] = mkf_u32(0, !!(global.tune.options & GTUNE_BUSY_POLLING));
Baptiste Assmann333939c2019-01-21 08:34:50 +01003504 info[INF_FAILED_RESOLUTIONS] = mkf_u32(0, dns_failed_resolutions);
Willy Tarreau7cf0e452019-05-23 11:39:14 +02003505 info[INF_TOTAL_BYTES_OUT] = mkf_u64(0, global.out_bytes);
3506 info[INF_BYTES_OUT_RATE] = mkf_u64(FN_RATE, (unsigned long long)read_freq_ctr(&global.out_32bps) * 32);
Willy Tarreau9b013702019-10-24 18:18:02 +02003507 info[INF_DEBUG_COMMANDS_ISSUED] = mkf_u32(0, debug_commands_issued);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003508
3509 return 1;
3510}
3511
3512/* This function dumps information onto the stream interface's read buffer.
3513 * It returns 0 as long as it does not complete, non-zero upon completion.
3514 * No state is used.
3515 */
3516static int stats_dump_info_to_buffer(struct stream_interface *si)
3517{
3518 struct appctx *appctx = __objt_appctx(si->end);
3519
3520 if (!stats_fill_info(info, INF_TOTAL_FIELDS))
3521 return 0;
3522
3523 chunk_reset(&trash);
3524
3525 if (appctx->ctx.stats.flags & STAT_FMT_TYPED)
Willy Tarreau43241ff2019-10-09 11:27:51 +02003526 stats_dump_typed_info_fields(&trash, info, appctx->ctx.stats.flags);
Simon Horman05ee2132017-01-04 09:37:25 +01003527 else if (appctx->ctx.stats.flags & STAT_FMT_JSON)
Willy Tarreau43241ff2019-10-09 11:27:51 +02003528 stats_dump_json_info_fields(&trash, info, appctx->ctx.stats.flags);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003529 else
Willy Tarreau43241ff2019-10-09 11:27:51 +02003530 stats_dump_info_fields(&trash, info, appctx->ctx.stats.flags);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003531
Willy Tarreau06d80a92017-10-19 14:32:15 +02003532 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01003533 si_rx_room_blk(si);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003534 return 0;
3535 }
3536
3537 return 1;
3538}
3539
Simon Horman6f6bb382017-01-04 09:37:26 +01003540/* This function dumps the schema onto the stream interface's read buffer.
3541 * It returns 0 as long as it does not complete, non-zero upon completion.
3542 * No state is used.
3543 *
3544 * Integer values bouned to the range [-(2**53)+1, (2**53)-1] as
3545 * per the recommendation for interoperable integers in section 6 of RFC 7159.
3546 */
Willy Tarreau83061a82018-07-13 11:56:34 +02003547static void stats_dump_json_schema(struct buffer *out)
Simon Horman6f6bb382017-01-04 09:37:26 +01003548{
3549
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003550 int old_len = out->data;
Simon Horman6f6bb382017-01-04 09:37:26 +01003551
3552 chunk_strcat(out,
3553 "{"
3554 "\"$schema\":\"http://json-schema.org/draft-04/schema#\","
3555 "\"oneOf\":["
3556 "{"
3557 "\"title\":\"Info\","
3558 "\"type\":\"array\","
3559 "\"items\":{"
3560 "\"properties\":{"
3561 "\"title\":\"InfoItem\","
3562 "\"type\":\"object\","
3563 "\"field\":{\"$ref\":\"#/definitions/field\"},"
3564 "\"processNum\":{\"$ref\":\"#/definitions/processNum\"},"
3565 "\"tags\":{\"$ref\":\"#/definitions/tags\"},"
3566 "\"value\":{\"$ref\":\"#/definitions/typedValue\"}"
3567 "},"
3568 "\"required\":[\"field\",\"processNum\",\"tags\","
3569 "\"value\"]"
3570 "}"
3571 "},"
3572 "{"
3573 "\"title\":\"Stat\","
3574 "\"type\":\"array\","
3575 "\"items\":{"
3576 "\"title\":\"InfoItem\","
3577 "\"type\":\"object\","
3578 "\"properties\":{"
3579 "\"objType\":{"
3580 "\"enum\":[\"Frontend\",\"Backend\",\"Listener\","
3581 "\"Server\",\"Unknown\"]"
3582 "},"
3583 "\"proxyId\":{"
3584 "\"type\":\"integer\","
3585 "\"minimum\":0"
3586 "},"
3587 "\"id\":{"
3588 "\"type\":\"integer\","
3589 "\"minimum\":0"
3590 "},"
3591 "\"field\":{\"$ref\":\"#/definitions/field\"},"
3592 "\"processNum\":{\"$ref\":\"#/definitions/processNum\"},"
3593 "\"tags\":{\"$ref\":\"#/definitions/tags\"},"
3594 "\"typedValue\":{\"$ref\":\"#/definitions/typedValue\"}"
3595 "},"
3596 "\"required\":[\"objType\",\"proxyId\",\"id\","
3597 "\"field\",\"processNum\",\"tags\","
3598 "\"value\"]"
3599 "}"
3600 "},"
3601 "{"
3602 "\"title\":\"Error\","
3603 "\"type\":\"object\","
3604 "\"properties\":{"
3605 "\"errorStr\":{"
3606 "\"type\":\"string\""
3607 "},"
3608 "\"required\":[\"errorStr\"]"
3609 "}"
3610 "}"
3611 "],"
3612 "\"definitions\":{"
3613 "\"field\":{"
3614 "\"type\":\"object\","
3615 "\"pos\":{"
3616 "\"type\":\"integer\","
3617 "\"minimum\":0"
3618 "},"
3619 "\"name\":{"
3620 "\"type\":\"string\""
3621 "},"
3622 "\"required\":[\"pos\",\"name\"]"
3623 "},"
3624 "\"processNum\":{"
3625 "\"type\":\"integer\","
3626 "\"minimum\":1"
3627 "},"
3628 "\"tags\":{"
3629 "\"type\":\"object\","
3630 "\"origin\":{"
3631 "\"type\":\"string\","
3632 "\"enum\":[\"Metric\",\"Status\",\"Key\","
3633 "\"Config\",\"Product\",\"Unknown\"]"
3634 "},"
3635 "\"nature\":{"
3636 "\"type\":\"string\","
3637 "\"enum\":[\"Gauge\",\"Limit\",\"Min\",\"Max\","
3638 "\"Rate\",\"Counter\",\"Duration\","
3639 "\"Age\",\"Time\",\"Name\",\"Output\","
3640 "\"Avg\", \"Unknown\"]"
3641 "},"
3642 "\"scope\":{"
3643 "\"type\":\"string\","
3644 "\"enum\":[\"Cluster\",\"Process\",\"Service\","
3645 "\"System\",\"Unknown\"]"
3646 "},"
3647 "\"required\":[\"origin\",\"nature\",\"scope\"]"
3648 "},"
3649 "\"typedValue\":{"
3650 "\"type\":\"object\","
3651 "\"oneOf\":["
3652 "{\"$ref\":\"#/definitions/typedValue/definitions/s32Value\"},"
3653 "{\"$ref\":\"#/definitions/typedValue/definitions/s64Value\"},"
3654 "{\"$ref\":\"#/definitions/typedValue/definitions/u32Value\"},"
3655 "{\"$ref\":\"#/definitions/typedValue/definitions/u64Value\"},"
3656 "{\"$ref\":\"#/definitions/typedValue/definitions/strValue\"}"
3657 "],"
3658 "\"definitions\":{"
3659 "\"s32Value\":{"
3660 "\"properties\":{"
3661 "\"type\":{"
3662 "\"type\":\"string\","
3663 "\"enum\":[\"s32\"]"
3664 "},"
3665 "\"value\":{"
3666 "\"type\":\"integer\","
3667 "\"minimum\":-2147483648,"
3668 "\"maximum\":2147483647"
3669 "}"
3670 "},"
3671 "\"required\":[\"type\",\"value\"]"
3672 "},"
3673 "\"s64Value\":{"
3674 "\"properties\":{"
3675 "\"type\":{"
3676 "\"type\":\"string\","
3677 "\"enum\":[\"s64\"]"
3678 "},"
3679 "\"value\":{"
3680 "\"type\":\"integer\","
3681 "\"minimum\":-9007199254740991,"
3682 "\"maximum\":9007199254740991"
3683 "}"
3684 "},"
3685 "\"required\":[\"type\",\"value\"]"
3686 "},"
3687 "\"u32Value\":{"
3688 "\"properties\":{"
3689 "\"type\":{"
3690 "\"type\":\"string\","
3691 "\"enum\":[\"u32\"]"
3692 "},"
3693 "\"value\":{"
3694 "\"type\":\"integer\","
3695 "\"minimum\":0,"
3696 "\"maximum\":4294967295"
3697 "}"
3698 "},"
3699 "\"required\":[\"type\",\"value\"]"
3700 "},"
3701 "\"u64Value\":{"
3702 "\"properties\":{"
3703 "\"type\":{"
3704 "\"type\":\"string\","
3705 "\"enum\":[\"u64\"]"
3706 "},"
3707 "\"value\":{"
3708 "\"type\":\"integer\","
3709 "\"minimum\":0,"
3710 "\"maximum\":9007199254740991"
3711 "}"
3712 "},"
3713 "\"required\":[\"type\",\"value\"]"
3714 "},"
3715 "\"strValue\":{"
3716 "\"properties\":{"
3717 "\"type\":{"
3718 "\"type\":\"string\","
3719 "\"enum\":[\"str\"]"
3720 "},"
3721 "\"value\":{\"type\":\"string\"}"
3722 "},"
3723 "\"required\":[\"type\",\"value\"]"
3724 "},"
3725 "\"unknownValue\":{"
3726 "\"properties\":{"
3727 "\"type\":{"
3728 "\"type\":\"integer\","
3729 "\"minimum\":0"
3730 "},"
3731 "\"value\":{"
3732 "\"type\":\"string\","
3733 "\"enum\":[\"unknown\"]"
3734 "}"
3735 "},"
3736 "\"required\":[\"type\",\"value\"]"
3737 "}"
3738 "}"
3739 "}"
3740 "}"
3741 "}");
3742
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003743 if (old_len == out->data) {
Simon Horman6f6bb382017-01-04 09:37:26 +01003744 chunk_reset(out);
3745 chunk_appendf(out,
3746 "{\"errorStr\":\"output buffer too short\"}");
3747 }
3748}
3749
3750/* This function dumps the schema onto the stream interface's read buffer.
3751 * It returns 0 as long as it does not complete, non-zero upon completion.
3752 * No state is used.
3753 */
3754static int stats_dump_json_schema_to_buffer(struct stream_interface *si)
3755{
3756 chunk_reset(&trash);
3757
3758 stats_dump_json_schema(&trash);
3759
Willy Tarreau06d80a92017-10-19 14:32:15 +02003760 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01003761 si_rx_room_blk(si);
Simon Horman6f6bb382017-01-04 09:37:26 +01003762 return 0;
3763 }
3764
3765 return 1;
3766}
3767
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02003768static int cli_parse_clear_counters(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau89d467c2016-11-23 11:02:40 +01003769{
3770 struct proxy *px;
3771 struct server *sv;
3772 struct listener *li;
3773 int clrall = 0;
3774
3775 if (strcmp(args[2], "all") == 0)
3776 clrall = 1;
3777
3778 /* check permissions */
3779 if (!cli_has_level(appctx, ACCESS_LVL_OPER) ||
3780 (clrall && !cli_has_level(appctx, ACCESS_LVL_ADMIN)))
3781 return 1;
3782
Olivier Houchardfbc74e82017-11-24 16:54:05 +01003783 for (px = proxies_list; px; px = px->next) {
Willy Tarreau89d467c2016-11-23 11:02:40 +01003784 if (clrall) {
3785 memset(&px->be_counters, 0, sizeof(px->be_counters));
3786 memset(&px->fe_counters, 0, sizeof(px->fe_counters));
3787 }
3788 else {
3789 px->be_counters.conn_max = 0;
3790 px->be_counters.p.http.rps_max = 0;
3791 px->be_counters.sps_max = 0;
3792 px->be_counters.cps_max = 0;
3793 px->be_counters.nbpend_max = 0;
Christopher Fauletefb41f02019-11-08 14:53:15 +01003794 px->be_counters.qtime_max = 0;
3795 px->be_counters.ctime_max = 0;
3796 px->be_counters.dtime_max = 0;
3797 px->be_counters.ttime_max = 0;
Willy Tarreau89d467c2016-11-23 11:02:40 +01003798
3799 px->fe_counters.conn_max = 0;
3800 px->fe_counters.p.http.rps_max = 0;
3801 px->fe_counters.sps_max = 0;
3802 px->fe_counters.cps_max = 0;
Willy Tarreau89d467c2016-11-23 11:02:40 +01003803 }
3804
3805 for (sv = px->srv; sv; sv = sv->next)
3806 if (clrall)
3807 memset(&sv->counters, 0, sizeof(sv->counters));
3808 else {
3809 sv->counters.cur_sess_max = 0;
3810 sv->counters.nbpend_max = 0;
3811 sv->counters.sps_max = 0;
Christopher Fauletefb41f02019-11-08 14:53:15 +01003812 sv->counters.qtime_max = 0;
3813 sv->counters.ctime_max = 0;
3814 sv->counters.dtime_max = 0;
3815 sv->counters.ttime_max = 0;
Willy Tarreau89d467c2016-11-23 11:02:40 +01003816 }
3817
3818 list_for_each_entry(li, &px->conf.listeners, by_fe)
3819 if (li->counters) {
3820 if (clrall)
3821 memset(li->counters, 0, sizeof(*li->counters));
3822 else
3823 li->counters->conn_max = 0;
3824 }
3825 }
3826
3827 global.cps_max = 0;
3828 global.sps_max = 0;
Olivier Houchard00bc3cb2017-10-17 19:23:25 +02003829 global.ssl_max = 0;
3830 global.ssl_fe_keys_max = 0;
3831 global.ssl_be_keys_max = 0;
Willy Tarreaud80cb4e2018-01-20 19:30:13 +01003832
3833 memset(activity, 0, sizeof(activity));
Willy Tarreau89d467c2016-11-23 11:02:40 +01003834 return 1;
3835}
3836
3837
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02003838static int cli_parse_show_info(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003839{
Willy Tarreau2f397382019-10-09 11:43:59 +02003840 int arg = 2;
3841
Willy Tarreaud25fc792016-12-16 12:33:47 +01003842 appctx->ctx.stats.scope_str = 0;
3843 appctx->ctx.stats.scope_len = 0;
3844 appctx->ctx.stats.flags = 0;
3845
Willy Tarreau2f397382019-10-09 11:43:59 +02003846 while (*args[arg]) {
3847 if (strcmp(args[arg], "typed") == 0)
3848 appctx->ctx.stats.flags = (appctx->ctx.stats.flags & ~STAT_FMT_MASK) | STAT_FMT_TYPED;
3849 else if (strcmp(args[arg], "json") == 0)
3850 appctx->ctx.stats.flags = (appctx->ctx.stats.flags & ~STAT_FMT_MASK) | STAT_FMT_JSON;
3851 else if (strcmp(args[arg], "desc") == 0)
3852 appctx->ctx.stats.flags |= STAT_SHOW_FDESC;
3853 arg++;
3854 }
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003855 return 0;
3856}
3857
3858
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02003859static int cli_parse_show_stat(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau2b812e22016-11-22 16:18:05 +01003860{
Willy Tarreau2f397382019-10-09 11:43:59 +02003861 int arg = 2;
3862
Willy Tarreaud25fc792016-12-16 12:33:47 +01003863 appctx->ctx.stats.scope_str = 0;
3864 appctx->ctx.stats.scope_len = 0;
Willy Tarreau578d6e42019-10-09 11:00:22 +02003865 appctx->ctx.stats.flags = STAT_SHNODE | STAT_SHDESC;
3866
3867 if ((strm_li(si_strm(appctx->owner))->bind_conf->level & ACCESS_LVL_MASK) >= ACCESS_LVL_OPER)
3868 appctx->ctx.stats.flags |= STAT_SHLGNDS;
Willy Tarreaud25fc792016-12-16 12:33:47 +01003869
Willy Tarreau2f397382019-10-09 11:43:59 +02003870 if (*args[arg] && *args[arg+1] && *args[arg+2]) {
Willy Tarreaua1b1ed52016-11-25 08:50:58 +01003871 struct proxy *px;
3872
Willy Tarreau2f397382019-10-09 11:43:59 +02003873 px = proxy_find_by_name(args[arg], 0, 0);
Willy Tarreaua1b1ed52016-11-25 08:50:58 +01003874 if (px)
3875 appctx->ctx.stats.iid = px->uuid;
3876 else
Willy Tarreau2f397382019-10-09 11:43:59 +02003877 appctx->ctx.stats.iid = atoi(args[arg]);
Willy Tarreaua1b1ed52016-11-25 08:50:58 +01003878
Willy Tarreau9d008692019-08-09 11:21:01 +02003879 if (!appctx->ctx.stats.iid)
3880 return cli_err(appctx, "No such proxy.\n");
Willy Tarreaua1b1ed52016-11-25 08:50:58 +01003881
Willy Tarreau2b812e22016-11-22 16:18:05 +01003882 appctx->ctx.stats.flags |= STAT_BOUND;
Willy Tarreau2f397382019-10-09 11:43:59 +02003883 appctx->ctx.stats.type = atoi(args[arg+1]);
3884 appctx->ctx.stats.sid = atoi(args[arg+2]);
3885 arg += 3;
3886 }
3887
3888 while (*args[arg]) {
3889 if (strcmp(args[arg], "typed") == 0)
3890 appctx->ctx.stats.flags = (appctx->ctx.stats.flags & ~STAT_FMT_MASK) | STAT_FMT_TYPED;
3891 else if (strcmp(args[arg], "json") == 0)
3892 appctx->ctx.stats.flags = (appctx->ctx.stats.flags & ~STAT_FMT_MASK) | STAT_FMT_JSON;
3893 else if (strcmp(args[arg], "desc") == 0)
3894 appctx->ctx.stats.flags |= STAT_SHOW_FDESC;
3895 arg++;
Willy Tarreau2b812e22016-11-22 16:18:05 +01003896 }
Willy Tarreau2b812e22016-11-22 16:18:05 +01003897
Willy Tarreau2b812e22016-11-22 16:18:05 +01003898 return 0;
3899}
3900
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003901static int cli_io_handler_dump_info(struct appctx *appctx)
3902{
3903 return stats_dump_info_to_buffer(appctx->owner);
3904}
3905
Willy Tarreau2b812e22016-11-22 16:18:05 +01003906/* This I/O handler runs as an applet embedded in a stream interface. It is
3907 * used to send raw stats over a socket.
3908 */
3909static int cli_io_handler_dump_stat(struct appctx *appctx)
3910{
Christopher Fauletef779222018-10-31 08:47:01 +01003911 return stats_dump_stat_to_buffer(appctx->owner, NULL, NULL);
Willy Tarreau2b812e22016-11-22 16:18:05 +01003912}
3913
Simon Horman6f6bb382017-01-04 09:37:26 +01003914static int cli_io_handler_dump_json_schema(struct appctx *appctx)
3915{
3916 return stats_dump_json_schema_to_buffer(appctx->owner);
3917}
3918
Willy Tarreau2b812e22016-11-22 16:18:05 +01003919/* register cli keywords */
3920static struct cli_kw_list cli_kws = {{ },{
Willy Tarreau89d467c2016-11-23 11:02:40 +01003921 { { "clear", "counters", NULL }, "clear counters : clear max statistics counters (add 'all' for all counters)", cli_parse_clear_counters, NULL, NULL },
Willy Tarreaueaa55372019-10-09 07:39:11 +02003922 { { "show", "info", NULL }, "show info : report information about the running process [desc|json|typed]*", cli_parse_show_info, cli_io_handler_dump_info, NULL },
3923 { { "show", "stat", NULL }, "show stat : report counters for each proxy and server [desc|json|typed]*", cli_parse_show_stat, cli_io_handler_dump_stat, NULL },
Simon Horman6f6bb382017-01-04 09:37:26 +01003924 { { "show", "schema", "json", NULL }, "show schema json : report schema used for stats", NULL, cli_io_handler_dump_json_schema, NULL },
Willy Tarreau2b812e22016-11-22 16:18:05 +01003925 {{},}
3926}};
3927
Willy Tarreau0108d902018-11-25 19:14:37 +01003928INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
3929
William Lallemand74c24fb2016-11-21 17:18:36 +01003930struct applet http_stats_applet = {
3931 .obj_type = OBJ_TYPE_APPLET,
3932 .name = "<STATS>", /* used for logging */
3933 .fct = http_stats_io_handler,
3934 .release = NULL,
3935};
3936
William Lallemand74c24fb2016-11-21 17:18:36 +01003937/*
3938 * Local variables:
3939 * c-indent-level: 8
3940 * c-basic-offset: 8
3941 * End:
3942 */