blob: 1413d3dcbcfadf70116c5af66530bdfb929e83ad [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
27#include <common/cfgparse.h>
28#include <common/compat.h>
29#include <common/config.h>
30#include <common/debug.h>
Willy Tarreau35b51c62018-09-10 15:38:55 +020031#include <common/http.h>
Willy Tarreaub96b77e2018-12-11 10:22:41 +010032#include <common/htx.h>
Willy Tarreau0108d902018-11-25 19:14:37 +010033#include <common/initcall.h>
William Lallemand74c24fb2016-11-21 17:18:36 +010034#include <common/memory.h>
35#include <common/mini-clist.h>
36#include <common/standard.h>
37#include <common/ticks.h>
38#include <common/time.h>
39#include <common/uri_auth.h>
40#include <common/version.h>
41#include <common/base64.h>
42
43#include <types/applet.h>
Willy Tarreau2b812e22016-11-22 16:18:05 +010044#include <types/cli.h>
William Lallemand74c24fb2016-11-21 17:18:36 +010045#include <types/global.h>
46#include <types/dns.h>
William Lallemand9ed62032016-11-21 17:49:11 +010047#include <types/stats.h>
William Lallemand74c24fb2016-11-21 17:18:36 +010048
49#include <proto/backend.h>
50#include <proto/channel.h>
51#include <proto/checks.h>
Willy Tarreau2b812e22016-11-22 16:18:05 +010052#include <proto/cli.h>
William Lallemand74c24fb2016-11-21 17:18:36 +010053#include <proto/compression.h>
Baptiste Assmann333939c2019-01-21 08:34:50 +010054#include <proto/dns.h>
William Lallemand9ed62032016-11-21 17:49:11 +010055#include <proto/stats.h>
William Lallemand74c24fb2016-11-21 17:18:36 +010056#include <proto/fd.h>
57#include <proto/freq_ctr.h>
58#include <proto/frontend.h>
Christopher Fauleted7a0662019-01-14 11:07:34 +010059#include <proto/http_htx.h>
William Lallemand74c24fb2016-11-21 17:18:36 +010060#include <proto/log.h>
61#include <proto/pattern.h>
62#include <proto/pipe.h>
63#include <proto/listener.h>
64#include <proto/map.h>
William Lallemand74c24fb2016-11-21 17:18:36 +010065#include <proto/proxy.h>
66#include <proto/sample.h>
67#include <proto/session.h>
Willy Tarreauc125cef2019-05-10 09:58:43 +020068#include <proto/ssl_sock.h>
William Lallemand74c24fb2016-11-21 17:18:36 +010069#include <proto/stream.h>
70#include <proto/server.h>
71#include <proto/raw_sock.h>
72#include <proto/stream_interface.h>
73#include <proto/task.h>
74
Christopher Faulet2b9b6782019-02-27 16:42:58 +010075/* status codes available for the stats admin page (strictly 4 chars length) */
76const char *stat_status_codes[STAT_STATUS_SIZE] = {
77 [STAT_STATUS_DENY] = "DENY",
78 [STAT_STATUS_DONE] = "DONE",
79 [STAT_STATUS_ERRP] = "ERRP",
80 [STAT_STATUS_EXCD] = "EXCD",
81 [STAT_STATUS_NONE] = "NONE",
82 [STAT_STATUS_PART] = "PART",
83 [STAT_STATUS_UNKN] = "UNKN",
84 [STAT_STATUS_IVAL] = "IVAL",
85};
86
Willy Tarreau0baac8c2016-11-22 16:36:53 +010087/* These are the field names for each INF_* field position. Please pay attention
88 * to always use the exact same name except that the strings for new names must
89 * be lower case or CamelCase while the enum entries must be upper case.
90 */
Willy Tarreaueaa55372019-10-09 07:39:11 +020091const struct name_desc info_fields[INF_TOTAL_FIELDS] = {
Willy Tarreau6d4897e2019-10-11 16:31:46 +020092 [INF_NAME] = { .name = "Name", .desc = "Product name" },
93 [INF_VERSION] = { .name = "Version", .desc = "Product version" },
94 [INF_RELEASE_DATE] = { .name = "Release_date", .desc = "Date of latest source code update" },
95 [INF_NBTHREAD] = { .name = "Nbthread", .desc = "Number of started threads (global.nbthread)" },
96 [INF_NBPROC] = { .name = "Nbproc", .desc = "Number of started worker processes (global.nbproc)" },
97 [INF_PROCESS_NUM] = { .name = "Process_num", .desc = "Relative worker process number (1..Nbproc)" },
98 [INF_PID] = { .name = "Pid", .desc = "This worker process identifier for the system" },
99 [INF_UPTIME] = { .name = "Uptime", .desc = "How long ago this worker process was started (days+hours+minutes+seconds)" },
100 [INF_UPTIME_SEC] = { .name = "Uptime_sec", .desc = "How long ago this worker process was started (seconds)" },
101 [INF_MEMMAX_MB] = { .name = "Memmax_MB", .desc = "Worker process's hard limit on memory usage in MB (-m on command line)" },
102 [INF_POOL_ALLOC_MB] = { .name = "PoolAlloc_MB", .desc = "Amount of memory allocated in pools (in MB)" },
103 [INF_POOL_USED_MB] = { .name = "PoolUsed_MB", .desc = "Amount of pool memory currently used (in MB)" },
104 [INF_POOL_FAILED] = { .name = "PoolFailed", .desc = "Number of failed pool allocations since this worker was started" },
105 [INF_ULIMIT_N] = { .name = "Ulimit-n", .desc = "Hard limit on the number of per-process file descriptors" },
106 [INF_MAXSOCK] = { .name = "Maxsock", .desc = "Hard limit on the number of per-process sockets" },
107 [INF_MAXCONN] = { .name = "Maxconn", .desc = "Hard limit on the number of per-process connections (configured or imposed by Ulimit-n)" },
108 [INF_HARD_MAXCONN] = { .name = "Hard_maxconn", .desc = "Hard limit on the number of per-process connections (imposed by Memmax_MB or Ulimit-n)" },
109 [INF_CURR_CONN] = { .name = "CurrConns", .desc = "Current number of connections on this worker process" },
110 [INF_CUM_CONN] = { .name = "CumConns", .desc = "Total number of connections on this worker process since started" },
111 [INF_CUM_REQ] = { .name = "CumReq", .desc = "Total number of requests on this worker process since started" },
112 [INF_MAX_SSL_CONNS] = { .name = "MaxSslConns", .desc = "Hard limit on the number of per-process SSL endpoints (front+back), 0=unlimited" },
113 [INF_CURR_SSL_CONNS] = { .name = "CurrSslConns", .desc = "Current number of SSL endpoints on this worker process (front+back)" },
114 [INF_CUM_SSL_CONNS] = { .name = "CumSslConns", .desc = "Total number of SSL endpoints on this worker process since started (front+back)" },
115 [INF_MAXPIPES] = { .name = "Maxpipes", .desc = "Hard limit on the number of pipes for splicing, 0=unlimited" },
116 [INF_PIPES_USED] = { .name = "PipesUsed", .desc = "Current number of pipes in use in this worker process" },
117 [INF_PIPES_FREE] = { .name = "PipesFree", .desc = "Current number of allocated and available pipes in this worker process" },
118 [INF_CONN_RATE] = { .name = "ConnRate", .desc = "Number of front connections created on this worker process over the last second" },
119 [INF_CONN_RATE_LIMIT] = { .name = "ConnRateLimit", .desc = "Hard limit for ConnRate (global.maxconnrate)" },
120 [INF_MAX_CONN_RATE] = { .name = "MaxConnRate", .desc = "Highest ConnRate reached on this worker process since started (in connections per second)" },
121 [INF_SESS_RATE] = { .name = "SessRate", .desc = "Number of sessions created on this worker process over the last second" },
122 [INF_SESS_RATE_LIMIT] = { .name = "SessRateLimit", .desc = "Hard limit for SessRate (global.maxsessrate)" },
123 [INF_MAX_SESS_RATE] = { .name = "MaxSessRate", .desc = "Highest SessRate reached on this worker process since started (in sessions per second)" },
124 [INF_SSL_RATE] = { .name = "SslRate", .desc = "Number of SSL connections created on this worker process over the last second" },
125 [INF_SSL_RATE_LIMIT] = { .name = "SslRateLimit", .desc = "Hard limit for SslRate (global.maxsslrate)" },
126 [INF_MAX_SSL_RATE] = { .name = "MaxSslRate", .desc = "Highest SslRate reached on this worker process since started (in connections per second)" },
127 [INF_SSL_FRONTEND_KEY_RATE] = { .name = "SslFrontendKeyRate", .desc = "Number of SSL keys created on frontends in this worker process over the last second" },
128 [INF_SSL_FRONTEND_MAX_KEY_RATE] = { .name = "SslFrontendMaxKeyRate", .desc = "Highest SslFrontendKeyRate reached on this worker process since started (in SSL keys per second)" },
129 [INF_SSL_FRONTEND_SESSION_REUSE_PCT] = { .name = "SslFrontendSessionReuse_pct", .desc = "Percent of frontend SSL connections which did not require a new key" },
130 [INF_SSL_BACKEND_KEY_RATE] = { .name = "SslBackendKeyRate", .desc = "Number of SSL keys created on backends in this worker process over the last second" },
131 [INF_SSL_BACKEND_MAX_KEY_RATE] = { .name = "SslBackendMaxKeyRate", .desc = "Highest SslBackendKeyRate reached on this worker process since started (in SSL keys per second)" },
132 [INF_SSL_CACHE_LOOKUPS] = { .name = "SslCacheLookups", .desc = "Total number of SSL session ID lookups in the SSL session cache on this worker since started" },
133 [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" },
134 [INF_COMPRESS_BPS_IN] = { .name = "CompressBpsIn", .desc = "Number of bytes submitted to the HTTP compressor in this worker process over the last second" },
135 [INF_COMPRESS_BPS_OUT] = { .name = "CompressBpsOut", .desc = "Number of bytes emitted by the HTTP compressor in this worker process over the last second" },
136 [INF_COMPRESS_BPS_RATE_LIM] = { .name = "CompressBpsRateLim", .desc = "Limit of CompressBpsOut beyond which HTTP compression is automatically disabled" },
137 [INF_ZLIB_MEM_USAGE] = { .name = "ZlibMemUsage", .desc = "Amount of memory currently used by HTTP compression on the current worker process (in bytes)" },
138 [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)" },
139 [INF_TASKS] = { .name = "Tasks", .desc = "Total number of tasks in the current worker process (active + sleeping)" },
140 [INF_RUN_QUEUE] = { .name = "Run_queue", .desc = "Total number of active tasks+tasklets in the current worker process" },
141 [INF_IDLE_PCT] = { .name = "Idle_pct", .desc = "Percentage of last second spent waiting in the current worker thread" },
142 [INF_NODE] = { .name = "node", .desc = "Node name (global.node)" },
143 [INF_DESCRIPTION] = { .name = "description", .desc = "Node description (global.description)" },
144 [INF_STOPPING] = { .name = "Stopping", .desc = "1 if the worker process is currently stopping, otherwise zero" },
145 [INF_JOBS] = { .name = "Jobs", .desc = "Current number of active jobs on the current worker process (frontend connections, master connections, listeners)" },
146 [INF_UNSTOPPABLE_JOBS] = { .name = "Unstoppable Jobs", .desc = "Current number of unstoppable jobs on the current worker process (master connections)" },
147 [INF_LISTENERS] = { .name = "Listeners", .desc = "Current number of active listeners on the current worker process" },
148 [INF_ACTIVE_PEERS] = { .name = "ActivePeers", .desc = "Current number of verified active peers connections on the current worker process" },
149 [INF_CONNECTED_PEERS] = { .name = "ConnectedPeers", .desc = "Current number of peers having passed the connection step on the current worker process" },
150 [INF_DROPPED_LOGS] = { .name = "DroppedLogs", .desc = "Total number of dropped logs for current worker process since started" },
151 [INF_BUSY_POLLING] = { .name = "BusyPolling", .desc = "1 if busy-polling is currently in use on the worker process, otherwise zero (config.busy-polling)" },
152 [INF_FAILED_RESOLUTIONS] = { .name = "FailedResolutions", .desc = "Total number of failed DNS resolutions in current worker process since started" },
153 [INF_TOTAL_BYTES_OUT] = { .name = "TotalBytesOut", .desc = "Total number of bytes emitted by current worker process since started" },
154 [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 +0200155 [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 +0100156};
157
Willy Tarreaueaa55372019-10-09 07:39:11 +0200158const struct name_desc stat_fields[ST_F_TOTAL_FIELDS] = {
Willy Tarreau6d4897e2019-10-11 16:31:46 +0200159 [ST_F_PXNAME] = { .name = "pxname", .desc = "Proxy name" },
160 [ST_F_SVNAME] = { .name = "svname", .desc = "Server name" },
161 [ST_F_QCUR] = { .name = "qcur", .desc = "Current number of connections waiting in the server of backend queue" },
162 [ST_F_QMAX] = { .name = "qmax", .desc = "Highest value of qcur encountered since process started" },
163 [ST_F_SCUR] = { .name = "scur", .desc = "Current number of sessions on the frontend, backend or server" },
164 [ST_F_SMAX] = { .name = "smax", .desc = "Highest value of scur encountered since process started" },
165 [ST_F_SLIM] = { .name = "slim", .desc = "Frontend/listener/server's maxconn, backend's fullconn" },
166 [ST_F_STOT] = { .name = "stot", .desc = "Total number of sessions since process started" },
167 [ST_F_BIN] = { .name = "bin", .desc = "Total number of request bytes since process started" },
168 [ST_F_BOUT] = { .name = "bout", .desc = "Total number of response bytes since process started" },
169 [ST_F_DREQ] = { .name = "dreq", .desc = "Total number of denied requests since process started" },
170 [ST_F_DRESP] = { .name = "dresp", .desc = "Total number of denied responses since process started" },
171 [ST_F_EREQ] = { .name = "ereq", .desc = "Total number of invalid requests since process started" },
172 [ST_F_ECON] = { .name = "econ", .desc = "Total number of failed connections to server since the worker process started" },
173 [ST_F_ERESP] = { .name = "eresp", .desc = "Total number of invalid responses since the worker process started" },
174 [ST_F_WRETR] = { .name = "wretr", .desc = "Total number of server connection retries since the worker process started" },
175 [ST_F_WREDIS] = { .name = "wredis", .desc = "Total number of server redispatches due to connection failures since the worker process started" },
176 [ST_F_STATUS] = { .name = "status", .desc = "Frontend/listen status: OPEN/WAITING/FULL/STOP; backend: UP/DOWN; server: last check status" },
177 [ST_F_WEIGHT] = { .name = "weight", .desc = "Server weight, or sum of active servers' weights for a backend" },
178 [ST_F_ACT] = { .name = "act", .desc = "Total number of active UP servers with a non-zero weight" },
179 [ST_F_BCK] = { .name = "bck", .desc = "Total number of backup UP servers with a non-zero weight" },
180 [ST_F_CHKFAIL] = { .name = "chkfail", .desc = "Total number of failed individual health checks per server/backend, since the worker process started" },
181 [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" },
182 [ST_F_LASTCHG] = { .name = "lastchg", .desc = "How long ago the last server state changed, in seconds" },
183 [ST_F_DOWNTIME] = { .name = "downtime", .desc = "Total time spent in DOWN state, for server or backend" },
184 [ST_F_QLIMIT] = { .name = "qlimit", .desc = "Limit on the number of connections in queue, for servers only (maxqueue argument)" },
185 [ST_F_PID] = { .name = "pid", .desc = "Relative worker process number (1..nbproc)" },
186 [ST_F_IID] = { .name = "iid", .desc = "Frontend or Backend numeric identifier ('id' setting)" },
187 [ST_F_SID] = { .name = "sid", .desc = "Server numeric identifier ('id' setting)" },
188 [ST_F_THROTTLE] = { .name = "throttle", .desc = "Throttling ratio applied to a server's maxconn and weight during the slowstart period (0 to 100%)" },
189 [ST_F_LBTOT] = { .name = "lbtot", .desc = "Total number of requests routed by load balancing since the worker process started (ignores queue pop and stickiness)" },
190 [ST_F_TRACKED] = { .name = "tracked", .desc = "Name of the other server this server tracks for its state" },
191 [ST_F_TYPE] = { .name = "type", .desc = "Type of the object (Listener, Frontend, Backend, Server)" },
192 [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)" },
193 [ST_F_RATE_LIM] = { .name = "rate_lim", .desc = "Limit on the number of sessions accepted in a second (frontend only, 'rate-limit sessions' setting)" },
194 [ST_F_RATE_MAX] = { .name = "rate_max", .desc = "Highest value of 'rate' observed since the worker process started" },
195 [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" },
196 [ST_F_CHECK_CODE] = { .name = "check_code", .desc = "HTTP/SMTP/LDAP status code reported by the latest server health check" },
197 [ST_F_CHECK_DURATION] = { .name = "check_duration", .desc = "Total duration of the latest server health check, in milliseconds" },
198 [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" },
199 [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" },
200 [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" },
201 [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" },
202 [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" },
203 [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)" },
204 [ST_F_HANAFAIL] = { .name = "hanafail", .desc = "Total number of failed checks caused by an 'on-error' directive after an 'observe' condition matched" },
205 [ST_F_REQ_RATE] = { .name = "req_rate", .desc = "Number of HTTP requests processed over the last second on this object" },
206 [ST_F_REQ_RATE_MAX] = { .name = "req_rate_max", .desc = "Highest value of 'req_rate' observed since the worker process started" },
207 [ST_F_REQ_TOT] = { .name = "req_tot", .desc = "Total number of HTTP requests processed by this object since the worker process started" },
208 [ST_F_CLI_ABRT] = { .name = "cli_abrt", .desc = "Total number of requests or connections aborted by the client since the worker process started" },
209 [ST_F_SRV_ABRT] = { .name = "srv_abrt", .desc = "Total number of requests or connections aborted by the server since the worker process started" },
210 [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" },
211 [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" },
212 [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)" },
213 [ST_F_COMP_RSP] = { .name = "comp_rsp", .desc = "Total number of HTTP responses that were compressed for this object since the worker process started" },
214 [ST_F_LASTSESS] = { .name = "lastsess", .desc = "How long ago some traffic was seen on this object on this worker process, in seconds" },
215 [ST_F_LAST_CHK] = { .name = "last_chk", .desc = "Short description of the latest health check report for this server (see also check_desc)" },
216 [ST_F_LAST_AGT] = { .name = "last_agt", .desc = "Short description of the latest agent check report for this server (see also agent_desc)" },
217 [ST_F_QTIME] = { .name = "qtime", .desc = "Time spent in the queue, in milliseconds, averaged over the 1024 last requests (backend/server)" },
218 [ST_F_CTIME] = { .name = "ctime", .desc = "Time spent waiting for a connection to complete, in milliseconds, averaged over the 1024 last requests (backend/server)" },
219 [ST_F_RTIME] = { .name = "rtime", .desc = "Time spent waiting for a server response, in milliseconds, averaged over the 1024 last requests (backend/server)" },
220 [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)" },
221 [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" },
222 [ST_F_AGENT_CODE] = { .name = "agent_code", .desc = "Status code reported by the latest server agent check" },
223 [ST_F_AGENT_DURATION] = { .name = "agent_duration", .desc = "Total duration of the latest server agent check, in milliseconds" },
224 [ST_F_CHECK_DESC] = { .name = "check_desc", .desc = "Textual description of the latest health check report for this server" },
225 [ST_F_AGENT_DESC] = { .name = "agent_desc", .desc = "Textual description of the latest agent check report for this server" },
226 [ST_F_CHECK_RISE] = { .name = "check_rise", .desc = "Number of successful health checks before declaring a server UP (server 'rise' setting)" },
227 [ST_F_CHECK_FALL] = { .name = "check_fall", .desc = "Number of failed health checks before declaring a server DOWN (server 'fall' setting)" },
228 [ST_F_CHECK_HEALTH] = { .name = "check_health", .desc = "Current server health check level (0..fall-1=DOWN, fall..rise-1=UP)" },
229 [ST_F_AGENT_RISE] = { .name = "agent_rise", .desc = "Number of successful agent checks before declaring a server UP (server 'rise' setting)" },
230 [ST_F_AGENT_FALL] = { .name = "agent_fall", .desc = "Number of failed agent checks before declaring a server DOWN (server 'fall' setting)" },
231 [ST_F_AGENT_HEALTH] = { .name = "agent_health", .desc = "Current server agent check level (0..fall-1=DOWN, fall..rise-1=UP)" },
232 [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" },
233 [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" },
234 [ST_F_MODE] = { .name = "mode", .desc = "'mode' setting (tcp/http/health/cli)" },
235 [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" },
236 [ST_F_CONN_RATE] = { .name = "conn_rate", .desc = "Number of new connections accepted over the last second on the frontend for this worker process" },
237 [ST_F_CONN_RATE_MAX] = { .name = "conn_rate_max", .desc = "Highest value of 'conn_rate' observed since the worker process started" },
238 [ST_F_CONN_TOT] = { .name = "conn_tot", .desc = "Total number of new connections accepted on this frontend since the worker process started" },
239 [ST_F_INTERCEPTED] = { .name = "intercepted", .desc = "Total number of HTTP requests intercepted on the frontend (redirects/stats/services) since the worker process started" },
240 [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" },
241 [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" },
242 [ST_F_WREW] = { .name = "wrew", .desc = "Total number of failed HTTP header rewrites since the worker process started" },
243 [ST_F_CONNECT] = { .name = "connect", .desc = "Total number of outgoing connection attempts on this backend/server since the worker process started" },
244 [ST_F_REUSE] = { .name = "reuse", .desc = "Total number of reused connection on this backend/server since the worker process started" },
245 [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" },
246 [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" },
247 [ST_F_SRV_ICUR] = { .name = "srv_icur", .desc = "Current number of idle connections available for reuse on this server" },
248 [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 +0100249 [ST_F_QT_MAX] = { .name = "qtime_max", .desc = "Maximum observed time spent in the queue, in milliseconds (backend/server)" },
250 [ST_F_CT_MAX] = { .name = "ctime_max", .desc = "Maximum observed time spent waiting for a connection to complete, in milliseconds (backend/server)" },
251 [ST_F_RT_MAX] = { .name = "rtime_max", .desc = "Maximum observed time spent waiting for a server response, in milliseconds (backend/server)" },
252 [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 +0100253 [ST_F_EINT] = { .name = "eint", .desc = "Total number of internal errors since process started"},
William Lallemand74c24fb2016-11-21 17:18:36 +0100254};
255
Willy Tarreau0baac8c2016-11-22 16:36:53 +0100256/* one line of info */
Christopher Faulet1bc04c72017-10-29 20:14:08 +0100257static THREAD_LOCAL struct field info[INF_TOTAL_FIELDS];
William Lallemand74c24fb2016-11-21 17:18:36 +0100258/* one line of stats */
Christopher Faulet1bc04c72017-10-29 20:14:08 +0100259static THREAD_LOCAL struct field stats[ST_F_TOTAL_FIELDS];
William Lallemand74c24fb2016-11-21 17:18:36 +0100260
Christopher Faulet6338a082019-09-09 15:50:54 +0200261static void stats_dump_json_schema(struct buffer *out);
William Lallemand74c24fb2016-11-21 17:18:36 +0100262
Christopher Fauletef779222018-10-31 08:47:01 +0100263static int stats_putchk(struct channel *chn, struct htx *htx, struct buffer *chk)
264{
265 if (htx) {
Christopher Faulet69fc88c2019-01-07 14:27:53 +0100266 if (chk->data >= channel_htx_recv_max(chn, htx))
267 return 0;
Willy Tarreau0a7ef022019-05-28 10:30:11 +0200268 if (!htx_add_data_atonce(htx, ist2(chk->area, chk->data)))
Christopher Fauletef779222018-10-31 08:47:01 +0100269 return 0;
Christopher Faulet5adbeeb2019-01-02 14:34:39 +0100270 channel_add_input(chn, chk->data);
Christopher Fauletef779222018-10-31 08:47:01 +0100271 chk->data = 0;
Christopher Fauletef779222018-10-31 08:47:01 +0100272 }
273 else {
274 if (ci_putchk(chn, chk) == -1)
275 return 0;
276 }
277 return 1;
278}
Willy Tarreau0baac8c2016-11-22 16:36:53 +0100279
Christopher Fauleted7a0662019-01-14 11:07:34 +0100280static const char *stats_scope_ptr(struct appctx *appctx, struct stream_interface *si)
281{
Christopher Fauletb7f88902019-07-15 21:56:43 +0200282 struct channel *req = si_oc(si);
283 struct htx *htx = htxbuf(&req->buf);
284 struct htx_blk *blk;
285 struct ist uri;
Christopher Fauleted7a0662019-01-14 11:07:34 +0100286
Christopher Fauletb7f88902019-07-15 21:56:43 +0200287 blk = htx_get_head_blk(htx);
Christopher Fauletea009732019-11-18 15:50:25 +0100288 BUG_ON(!blk || htx_get_blk_type(blk) != HTX_BLK_REQ_SL);
Christopher Fauletb7f88902019-07-15 21:56:43 +0200289 ALREADY_CHECKED(blk);
290 uri = htx_sl_req_uri(htx_get_blk_ptr(htx, blk));
291 return uri.ptr + appctx->ctx.stats.scope_str;
Christopher Fauleted7a0662019-01-14 11:07:34 +0100292}
293
William Lallemand74c24fb2016-11-21 17:18:36 +0100294/*
295 * http_stats_io_handler()
296 * -> stats_dump_stat_to_buffer() // same as above, but used for CSV or HTML
297 * -> stats_dump_csv_header() // emits the CSV headers (same as above)
Simon Horman05ee2132017-01-04 09:37:25 +0100298 * -> stats_dump_json_header() // emits the JSON headers (same as above)
William Lallemand74c24fb2016-11-21 17:18:36 +0100299 * -> stats_dump_html_head() // emits the HTML headers
300 * -> stats_dump_html_info() // emits the equivalent of "show info" at the top
301 * -> stats_dump_proxy_to_buffer() // same as above, valid for CSV and HTML
302 * -> stats_dump_html_px_hdr()
303 * -> stats_dump_fe_stats()
304 * -> stats_dump_li_stats()
305 * -> stats_dump_sv_stats()
306 * -> stats_dump_be_stats()
307 * -> stats_dump_html_px_end()
308 * -> stats_dump_html_end() // emits HTML trailer
Simon Horman05ee2132017-01-04 09:37:25 +0100309 * -> stats_dump_json_end() // emits JSON trailer
William Lallemand74c24fb2016-11-21 17:18:36 +0100310 */
311
312
William Lallemand74c24fb2016-11-21 17:18:36 +0100313/* Dumps the stats CSV header to the trash buffer which. The caller is responsible
314 * for clearing it if needed.
315 * NOTE: Some tools happen to rely on the field position instead of its name,
316 * so please only append new fields at the end, never in the middle.
317 */
318static void stats_dump_csv_header()
319{
320 int field;
321
322 chunk_appendf(&trash, "# ");
323 for (field = 0; field < ST_F_TOTAL_FIELDS; field++)
Willy Tarreaueaa55372019-10-09 07:39:11 +0200324 chunk_appendf(&trash, "%s,", stat_fields[field].name);
William Lallemand74c24fb2016-11-21 17:18:36 +0100325
326 chunk_appendf(&trash, "\n");
327}
328
329
330/* Emits a stats field without any surrounding element and properly encoded to
331 * resist CSV output. Returns non-zero on success, 0 if the buffer is full.
332 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200333int stats_emit_raw_data_field(struct buffer *out, const struct field *f)
William Lallemand74c24fb2016-11-21 17:18:36 +0100334{
335 switch (field_format(f, 0)) {
336 case FF_EMPTY: return 1;
337 case FF_S32: return chunk_appendf(out, "%d", f->u.s32);
338 case FF_U32: return chunk_appendf(out, "%u", f->u.u32);
339 case FF_S64: return chunk_appendf(out, "%lld", (long long)f->u.s64);
340 case FF_U64: return chunk_appendf(out, "%llu", (unsigned long long)f->u.u64);
Christopher Faulet88a0db22019-09-24 16:35:10 +0200341 case FF_FLT: return chunk_appendf(out, "%f", f->u.flt);
William Lallemand74c24fb2016-11-21 17:18:36 +0100342 case FF_STR: return csv_enc_append(field_str(f, 0), 1, out) != NULL;
343 default: return chunk_appendf(out, "[INCORRECT_FIELD_TYPE_%08x]", f->type);
344 }
345}
346
347/* Emits a stats field prefixed with its type. No CSV encoding is prepared, the
348 * output is supposed to be used on its own line. Returns non-zero on success, 0
349 * if the buffer is full.
350 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200351int stats_emit_typed_data_field(struct buffer *out, const struct field *f)
William Lallemand74c24fb2016-11-21 17:18:36 +0100352{
353 switch (field_format(f, 0)) {
354 case FF_EMPTY: return 1;
355 case FF_S32: return chunk_appendf(out, "s32:%d", f->u.s32);
356 case FF_U32: return chunk_appendf(out, "u32:%u", f->u.u32);
357 case FF_S64: return chunk_appendf(out, "s64:%lld", (long long)f->u.s64);
358 case FF_U64: return chunk_appendf(out, "u64:%llu", (unsigned long long)f->u.u64);
Christopher Faulet88a0db22019-09-24 16:35:10 +0200359 case FF_FLT: return chunk_appendf(out, "flt:%f", f->u.flt);
William Lallemand74c24fb2016-11-21 17:18:36 +0100360 case FF_STR: return chunk_appendf(out, "str:%s", field_str(f, 0));
361 default: return chunk_appendf(out, "%08x:?", f->type);
362 }
363}
364
Simon Horman05ee2132017-01-04 09:37:25 +0100365/* Limit JSON integer values to the range [-(2**53)+1, (2**53)-1] as per
366 * the recommendation for interoperable integers in section 6 of RFC 7159.
367 */
368#define JSON_INT_MAX ((1ULL << 53) - 1)
369#define JSON_INT_MIN (0 - JSON_INT_MAX)
370
371/* Emits a stats field value and its type in JSON.
372 * Returns non-zero on success, 0 on error.
373 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200374int stats_emit_json_data_field(struct buffer *out, const struct field *f)
Simon Horman05ee2132017-01-04 09:37:25 +0100375{
376 int old_len;
377 char buf[20];
378 const char *type, *value = buf, *quote = "";
379
380 switch (field_format(f, 0)) {
381 case FF_EMPTY: return 1;
382 case FF_S32: type = "\"s32\"";
383 snprintf(buf, sizeof(buf), "%d", f->u.s32);
384 break;
385 case FF_U32: type = "\"u32\"";
386 snprintf(buf, sizeof(buf), "%u", f->u.u32);
387 break;
388 case FF_S64: type = "\"s64\"";
389 if (f->u.s64 < JSON_INT_MIN || f->u.s64 > JSON_INT_MAX)
390 return 0;
391 type = "\"s64\"";
392 snprintf(buf, sizeof(buf), "%lld", (long long)f->u.s64);
393 break;
394 case FF_U64: if (f->u.u64 > JSON_INT_MAX)
395 return 0;
396 type = "\"u64\"";
397 snprintf(buf, sizeof(buf), "%llu",
398 (unsigned long long) f->u.u64);
Christopher Faulet52c91bb2019-09-28 10:37:31 +0200399 break;
Christopher Faulet88a0db22019-09-24 16:35:10 +0200400 case FF_FLT: type = "\"flt\"";
401 snprintf(buf, sizeof(buf), "%f", f->u.flt);
Simon Horman05ee2132017-01-04 09:37:25 +0100402 break;
403 case FF_STR: type = "\"str\"";
404 value = field_str(f, 0);
405 quote = "\"";
406 break;
407 default: snprintf(buf, sizeof(buf), "%u", f->type);
408 type = buf;
409 value = "unknown";
410 quote = "\"";
411 break;
412 }
413
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200414 old_len = out->data;
Simon Horman05ee2132017-01-04 09:37:25 +0100415 chunk_appendf(out, ",\"value\":{\"type\":%s,\"value\":%s%s%s}",
416 type, quote, value, quote);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200417 return !(old_len == out->data);
Simon Horman05ee2132017-01-04 09:37:25 +0100418}
419
William Lallemand74c24fb2016-11-21 17:18:36 +0100420/* Emits an encoding of the field type on 3 characters followed by a delimiter.
421 * Returns non-zero on success, 0 if the buffer is full.
422 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200423int stats_emit_field_tags(struct buffer *out, const struct field *f,
424 char delim)
William Lallemand74c24fb2016-11-21 17:18:36 +0100425{
426 char origin, nature, scope;
427
428 switch (field_origin(f, 0)) {
429 case FO_METRIC: origin = 'M'; break;
430 case FO_STATUS: origin = 'S'; break;
431 case FO_KEY: origin = 'K'; break;
432 case FO_CONFIG: origin = 'C'; break;
433 case FO_PRODUCT: origin = 'P'; break;
434 default: origin = '?'; break;
435 }
436
437 switch (field_nature(f, 0)) {
438 case FN_GAUGE: nature = 'G'; break;
439 case FN_LIMIT: nature = 'L'; break;
440 case FN_MIN: nature = 'm'; break;
441 case FN_MAX: nature = 'M'; break;
442 case FN_RATE: nature = 'R'; break;
443 case FN_COUNTER: nature = 'C'; break;
444 case FN_DURATION: nature = 'D'; break;
445 case FN_AGE: nature = 'A'; break;
446 case FN_TIME: nature = 'T'; break;
447 case FN_NAME: nature = 'N'; break;
448 case FN_OUTPUT: nature = 'O'; break;
449 case FN_AVG: nature = 'a'; break;
450 default: nature = '?'; break;
451 }
452
453 switch (field_scope(f, 0)) {
454 case FS_PROCESS: scope = 'P'; break;
455 case FS_SERVICE: scope = 'S'; break;
456 case FS_SYSTEM: scope = 's'; break;
457 case FS_CLUSTER: scope = 'C'; break;
458 default: scope = '?'; break;
459 }
460
461 return chunk_appendf(out, "%c%c%c%c", origin, nature, scope, delim);
462}
463
Simon Horman05ee2132017-01-04 09:37:25 +0100464/* Emits an encoding of the field type as JSON.
465 * Returns non-zero on success, 0 if the buffer is full.
466 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200467int stats_emit_json_field_tags(struct buffer *out, const struct field *f)
Simon Horman05ee2132017-01-04 09:37:25 +0100468{
469 const char *origin, *nature, *scope;
470 int old_len;
471
472 switch (field_origin(f, 0)) {
473 case FO_METRIC: origin = "Metric"; break;
474 case FO_STATUS: origin = "Status"; break;
475 case FO_KEY: origin = "Key"; break;
476 case FO_CONFIG: origin = "Config"; break;
477 case FO_PRODUCT: origin = "Product"; break;
478 default: origin = "Unknown"; break;
479 }
480
481 switch (field_nature(f, 0)) {
482 case FN_GAUGE: nature = "Gauge"; break;
483 case FN_LIMIT: nature = "Limit"; break;
484 case FN_MIN: nature = "Min"; break;
485 case FN_MAX: nature = "Max"; break;
486 case FN_RATE: nature = "Rate"; break;
487 case FN_COUNTER: nature = "Counter"; break;
488 case FN_DURATION: nature = "Duration"; break;
489 case FN_AGE: nature = "Age"; break;
490 case FN_TIME: nature = "Time"; break;
491 case FN_NAME: nature = "Name"; break;
492 case FN_OUTPUT: nature = "Output"; break;
493 case FN_AVG: nature = "Avg"; break;
494 default: nature = "Unknown"; break;
495 }
496
497 switch (field_scope(f, 0)) {
498 case FS_PROCESS: scope = "Process"; break;
499 case FS_SERVICE: scope = "Service"; break;
500 case FS_SYSTEM: scope = "System"; break;
501 case FS_CLUSTER: scope = "Cluster"; break;
502 default: scope = "Unknown"; break;
503 }
504
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200505 old_len = out->data;
Simon Horman05ee2132017-01-04 09:37:25 +0100506 chunk_appendf(out, "\"tags\":{"
507 "\"origin\":\"%s\","
508 "\"nature\":\"%s\","
509 "\"scope\":\"%s\""
510 "}", origin, nature, scope);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200511 return !(old_len == out->data);
Simon Horman05ee2132017-01-04 09:37:25 +0100512}
William Lallemand74c24fb2016-11-21 17:18:36 +0100513
514/* Dump all fields from <stats> into <out> using CSV format */
Willy Tarreau83061a82018-07-13 11:56:34 +0200515static int stats_dump_fields_csv(struct buffer *out,
Willy Tarreau43241ff2019-10-09 11:27:51 +0200516 const struct field *stats, unsigned int flags)
William Lallemand74c24fb2016-11-21 17:18:36 +0100517{
518 int field;
519
520 for (field = 0; field < ST_F_TOTAL_FIELDS; field++) {
521 if (!stats_emit_raw_data_field(out, &stats[field]))
522 return 0;
523 if (!chunk_strcat(out, ","))
524 return 0;
525 }
526 chunk_strcat(out, "\n");
527 return 1;
528}
529
530/* Dump all fields from <stats> into <out> using a typed "field:desc:type:value" format */
Willy Tarreau83061a82018-07-13 11:56:34 +0200531static int stats_dump_fields_typed(struct buffer *out,
Willy Tarreau43241ff2019-10-09 11:27:51 +0200532 const struct field *stats, unsigned int flags)
William Lallemand74c24fb2016-11-21 17:18:36 +0100533{
534 int field;
535
536 for (field = 0; field < ST_F_TOTAL_FIELDS; field++) {
537 if (!stats[field].type)
538 continue;
539
540 chunk_appendf(out, "%c.%u.%u.%d.%s.%u:",
541 stats[ST_F_TYPE].u.u32 == STATS_TYPE_FE ? 'F' :
542 stats[ST_F_TYPE].u.u32 == STATS_TYPE_BE ? 'B' :
543 stats[ST_F_TYPE].u.u32 == STATS_TYPE_SO ? 'L' :
544 stats[ST_F_TYPE].u.u32 == STATS_TYPE_SV ? 'S' :
545 '?',
546 stats[ST_F_IID].u.u32, stats[ST_F_SID].u.u32,
Willy Tarreaueaa55372019-10-09 07:39:11 +0200547 field, stat_fields[field].name, stats[ST_F_PID].u.u32);
William Lallemand74c24fb2016-11-21 17:18:36 +0100548
549 if (!stats_emit_field_tags(out, &stats[field], ':'))
550 return 0;
551 if (!stats_emit_typed_data_field(out, &stats[field]))
552 return 0;
Willy Tarreau6b19b142019-10-09 15:44:21 +0200553 if ((flags & STAT_SHOW_FDESC) && !chunk_appendf(out, ":\"%s\"", stat_fields[field].desc))
554 return 0;
William Lallemand74c24fb2016-11-21 17:18:36 +0100555 if (!chunk_strcat(out, "\n"))
556 return 0;
557 }
558 return 1;
559}
560
Simon Horman05ee2132017-01-04 09:37:25 +0100561/* Dump all fields from <stats> into <out> using the "show info json" format */
Willy Tarreau83061a82018-07-13 11:56:34 +0200562static int stats_dump_json_info_fields(struct buffer *out,
Willy Tarreau43241ff2019-10-09 11:27:51 +0200563 const struct field *info, unsigned int flags)
Simon Horman05ee2132017-01-04 09:37:25 +0100564{
565 int field;
566 int started = 0;
567
568 if (!chunk_strcat(out, "["))
569 return 0;
570
571 for (field = 0; field < INF_TOTAL_FIELDS; field++) {
572 int old_len;
573
574 if (!field_format(info, field))
575 continue;
576
577 if (started && !chunk_strcat(out, ","))
578 goto err;
579 started = 1;
580
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200581 old_len = out->data;
Simon Horman05ee2132017-01-04 09:37:25 +0100582 chunk_appendf(out,
583 "{\"field\":{\"pos\":%d,\"name\":\"%s\"},"
584 "\"processNum\":%u,",
Willy Tarreaueaa55372019-10-09 07:39:11 +0200585 field, info_fields[field].name,
Simon Horman05ee2132017-01-04 09:37:25 +0100586 info[INF_PROCESS_NUM].u.u32);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200587 if (old_len == out->data)
Simon Horman05ee2132017-01-04 09:37:25 +0100588 goto err;
589
590 if (!stats_emit_json_field_tags(out, &info[field]))
591 goto err;
592
593 if (!stats_emit_json_data_field(out, &info[field]))
594 goto err;
595
596 if (!chunk_strcat(out, "}"))
597 goto err;
598 }
599
600 if (!chunk_strcat(out, "]"))
601 goto err;
602 return 1;
603
604err:
605 chunk_reset(out);
606 chunk_appendf(out, "{\"errorStr\":\"output buffer too short\"}");
607 return 0;
608}
609
610/* Dump all fields from <stats> into <out> using a typed "field:desc:type:value" format */
Willy Tarreau83061a82018-07-13 11:56:34 +0200611static int stats_dump_fields_json(struct buffer *out,
612 const struct field *stats,
Willy Tarreaub0ce3ad2019-10-09 11:19:29 +0200613 unsigned int flags)
Simon Horman05ee2132017-01-04 09:37:25 +0100614{
615 int field;
616 int started = 0;
617
Willy Tarreaub0ce3ad2019-10-09 11:19:29 +0200618 if ((flags & STAT_STARTED) && !chunk_strcat(out, ","))
Simon Horman05ee2132017-01-04 09:37:25 +0100619 return 0;
620 if (!chunk_strcat(out, "["))
621 return 0;
622
623 for (field = 0; field < ST_F_TOTAL_FIELDS; field++) {
624 const char *obj_type;
625 int old_len;
626
627 if (!stats[field].type)
628 continue;
629
630 if (started && !chunk_strcat(out, ","))
631 goto err;
632 started = 1;
633
634 switch (stats[ST_F_TYPE].u.u32) {
635 case STATS_TYPE_FE: obj_type = "Frontend"; break;
636 case STATS_TYPE_BE: obj_type = "Backend"; break;
637 case STATS_TYPE_SO: obj_type = "Listener"; break;
638 case STATS_TYPE_SV: obj_type = "Server"; break;
639 default: obj_type = "Unknown"; break;
640 }
641
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200642 old_len = out->data;
Simon Horman05ee2132017-01-04 09:37:25 +0100643 chunk_appendf(out,
644 "{"
645 "\"objType\":\"%s\","
646 "\"proxyId\":%d,"
647 "\"id\":%d,"
648 "\"field\":{\"pos\":%d,\"name\":\"%s\"},"
649 "\"processNum\":%u,",
650 obj_type, stats[ST_F_IID].u.u32,
651 stats[ST_F_SID].u.u32, field,
Willy Tarreaueaa55372019-10-09 07:39:11 +0200652 stat_fields[field].name, stats[ST_F_PID].u.u32);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200653 if (old_len == out->data)
Simon Horman05ee2132017-01-04 09:37:25 +0100654 goto err;
655
656 if (!stats_emit_json_field_tags(out, &stats[field]))
657 goto err;
658
659 if (!stats_emit_json_data_field(out, &stats[field]))
660 goto err;
661
662 if (!chunk_strcat(out, "}"))
663 goto err;
664 }
665
666 if (!chunk_strcat(out, "]"))
667 goto err;
668
669 return 1;
670
671err:
672 chunk_reset(out);
Willy Tarreaub0ce3ad2019-10-09 11:19:29 +0200673 if (flags & STAT_STARTED)
674 chunk_strcat(out, ",");
Simon Horman05ee2132017-01-04 09:37:25 +0100675 chunk_appendf(out, "{\"errorStr\":\"output buffer too short\"}");
676 return 0;
677}
678
William Lallemand74c24fb2016-11-21 17:18:36 +0100679/* Dump all fields from <stats> into <out> using the HTML format. A column is
Willy Tarreauab02b3f2019-10-09 11:11:46 +0200680 * reserved for the checkbox is STAT_ADMIN is set in <flags>. Some extra info
Willy Tarreau708c4162019-10-09 10:19:16 +0200681 * are provided if STAT_SHLGNDS is present in <flags>.
William Lallemand74c24fb2016-11-21 17:18:36 +0100682 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200683static int stats_dump_fields_html(struct buffer *out,
684 const struct field *stats,
685 unsigned int flags)
William Lallemand74c24fb2016-11-21 17:18:36 +0100686{
Willy Tarreau83061a82018-07-13 11:56:34 +0200687 struct buffer src;
William Lallemand74c24fb2016-11-21 17:18:36 +0100688
689 if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_FE) {
690 chunk_appendf(out,
691 /* name, queue */
692 "<tr class=\"frontend\">");
693
Willy Tarreauab02b3f2019-10-09 11:11:46 +0200694 if (flags & STAT_ADMIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +0100695 /* Column sub-heading for Enable or Disable server */
696 chunk_appendf(out, "<td></td>");
697 }
698
699 chunk_appendf(out,
700 "<td class=ac>"
701 "<a name=\"%s/Frontend\"></a>"
702 "<a class=lfsb href=\"#%s/Frontend\">Frontend</a></td>"
703 "<td colspan=3></td>"
704 "",
705 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_PXNAME));
706
707 chunk_appendf(out,
708 /* sessions rate : current */
709 "<td><u>%s<div class=tips><table class=det>"
710 "<tr><th>Current connection rate:</th><td>%s/s</td></tr>"
711 "<tr><th>Current session rate:</th><td>%s/s</td></tr>"
712 "",
713 U2H(stats[ST_F_RATE].u.u32),
714 U2H(stats[ST_F_CONN_RATE].u.u32),
715 U2H(stats[ST_F_RATE].u.u32));
716
717 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
718 chunk_appendf(out,
719 "<tr><th>Current request rate:</th><td>%s/s</td></tr>",
720 U2H(stats[ST_F_REQ_RATE].u.u32));
721
722 chunk_appendf(out,
723 "</table></div></u></td>"
724 /* sessions rate : max */
725 "<td><u>%s<div class=tips><table class=det>"
726 "<tr><th>Max connection rate:</th><td>%s/s</td></tr>"
727 "<tr><th>Max session rate:</th><td>%s/s</td></tr>"
728 "",
729 U2H(stats[ST_F_RATE_MAX].u.u32),
730 U2H(stats[ST_F_CONN_RATE_MAX].u.u32),
731 U2H(stats[ST_F_RATE_MAX].u.u32));
732
733 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
734 chunk_appendf(out,
735 "<tr><th>Max request rate:</th><td>%s/s</td></tr>",
736 U2H(stats[ST_F_REQ_RATE_MAX].u.u32));
737
738 chunk_appendf(out,
739 "</table></div></u></td>"
740 /* sessions rate : limit */
741 "<td>%s</td>",
742 LIM2A(stats[ST_F_RATE_LIM].u.u32, "-"));
743
744 chunk_appendf(out,
745 /* sessions: current, max, limit, total */
746 "<td>%s</td><td>%s</td><td>%s</td>"
747 "<td><u>%s<div class=tips><table class=det>"
748 "<tr><th>Cum. connections:</th><td>%s</td></tr>"
749 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
750 "",
751 U2H(stats[ST_F_SCUR].u.u32), U2H(stats[ST_F_SMAX].u.u32), U2H(stats[ST_F_SLIM].u.u32),
752 U2H(stats[ST_F_STOT].u.u64),
753 U2H(stats[ST_F_CONN_TOT].u.u64),
754 U2H(stats[ST_F_STOT].u.u64));
755
756 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
757 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0) {
758 chunk_appendf(out,
759 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
760 "<tr><th>- HTTP 1xx responses:</th><td>%s</td></tr>"
761 "<tr><th>- HTTP 2xx responses:</th><td>%s</td></tr>"
762 "<tr><th>&nbsp;&nbsp;Compressed 2xx:</th><td>%s</td><td>(%d%%)</td></tr>"
763 "<tr><th>- HTTP 3xx responses:</th><td>%s</td></tr>"
764 "<tr><th>- HTTP 4xx responses:</th><td>%s</td></tr>"
765 "<tr><th>- HTTP 5xx responses:</th><td>%s</td></tr>"
766 "<tr><th>- other responses:</th><td>%s</td></tr>"
767 "<tr><th>Intercepted requests:</th><td>%s</td></tr>"
Willy Tarreaua1214a52018-12-14 14:00:25 +0100768 "<tr><th>Cache lookups:</th><td>%s</td></tr>"
769 "<tr><th>Cache hits:</th><td>%s</td><td>(%d%%)</td></tr>"
Willy Tarreau1b0f85e2018-05-28 15:12:40 +0200770 "<tr><th>Failed hdr rewrites:</th><td>%s</td></tr>"
Christopher Faulet0159ee42019-12-16 14:40:39 +0100771 "<tr><th>Internal errors:</th><td>%s</td></tr>"
William Lallemand74c24fb2016-11-21 17:18:36 +0100772 "",
773 U2H(stats[ST_F_REQ_TOT].u.u64),
774 U2H(stats[ST_F_HRSP_1XX].u.u64),
775 U2H(stats[ST_F_HRSP_2XX].u.u64),
776 U2H(stats[ST_F_COMP_RSP].u.u64),
777 stats[ST_F_HRSP_2XX].u.u64 ?
778 (int)(100 * stats[ST_F_COMP_RSP].u.u64 / stats[ST_F_HRSP_2XX].u.u64) : 0,
779 U2H(stats[ST_F_HRSP_3XX].u.u64),
780 U2H(stats[ST_F_HRSP_4XX].u.u64),
781 U2H(stats[ST_F_HRSP_5XX].u.u64),
782 U2H(stats[ST_F_HRSP_OTHER].u.u64),
Willy Tarreau1b0f85e2018-05-28 15:12:40 +0200783 U2H(stats[ST_F_INTERCEPTED].u.u64),
Willy Tarreaua1214a52018-12-14 14:00:25 +0100784 U2H(stats[ST_F_CACHE_LOOKUPS].u.u64),
785 U2H(stats[ST_F_CACHE_HITS].u.u64),
786 stats[ST_F_CACHE_LOOKUPS].u.u64 ?
787 (int)(100 * stats[ST_F_CACHE_HITS].u.u64 / stats[ST_F_CACHE_LOOKUPS].u.u64) : 0,
Christopher Faulet0159ee42019-12-16 14:40:39 +0100788 U2H(stats[ST_F_WREW].u.u64),
789 U2H(stats[ST_F_EINT].u.u64));
William Lallemand74c24fb2016-11-21 17:18:36 +0100790 }
791
792 chunk_appendf(out,
793 "</table></div></u></td>"
794 /* sessions: lbtot, lastsess */
795 "<td></td><td></td>"
796 /* bytes : in */
797 "<td>%s</td>"
798 "",
799 U2H(stats[ST_F_BIN].u.u64));
800
801 chunk_appendf(out,
802 /* bytes:out + compression stats (via hover): comp_in, comp_out, comp_byp */
803 "<td>%s%s<div class=tips><table class=det>"
804 "<tr><th>Response bytes in:</th><td>%s</td></tr>"
805 "<tr><th>Compression in:</th><td>%s</td></tr>"
806 "<tr><th>Compression out:</th><td>%s</td><td>(%d%%)</td></tr>"
807 "<tr><th>Compression bypass:</th><td>%s</td></tr>"
808 "<tr><th>Total bytes saved:</th><td>%s</td><td>(%d%%)</td></tr>"
809 "</table></div>%s</td>",
810 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "<u>":"",
811 U2H(stats[ST_F_BOUT].u.u64),
812 U2H(stats[ST_F_BOUT].u.u64),
813 U2H(stats[ST_F_COMP_IN].u.u64),
814 U2H(stats[ST_F_COMP_OUT].u.u64),
815 stats[ST_F_COMP_IN].u.u64 ? (int)(stats[ST_F_COMP_OUT].u.u64 * 100 / stats[ST_F_COMP_IN].u.u64) : 0,
816 U2H(stats[ST_F_COMP_BYP].u.u64),
817 U2H(stats[ST_F_COMP_IN].u.u64 - stats[ST_F_COMP_OUT].u.u64),
818 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,
819 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "</u>":"");
820
821 chunk_appendf(out,
822 /* denied: req, resp */
823 "<td>%s</td><td>%s</td>"
824 /* errors : request, connect, response */
825 "<td>%s</td><td></td><td></td>"
826 /* warnings: retries, redispatches */
827 "<td></td><td></td>"
828 /* server status : reflect frontend status */
829 "<td class=ac>%s</td>"
830 /* rest of server: nothing */
831 "<td class=ac colspan=8></td></tr>"
832 "",
833 U2H(stats[ST_F_DREQ].u.u64), U2H(stats[ST_F_DRESP].u.u64),
834 U2H(stats[ST_F_EREQ].u.u64),
835 field_str(stats, ST_F_STATUS));
836 }
837 else if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_SO) {
838 chunk_appendf(out, "<tr class=socket>");
Willy Tarreauab02b3f2019-10-09 11:11:46 +0200839 if (flags & STAT_ADMIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +0100840 /* Column sub-heading for Enable or Disable server */
841 chunk_appendf(out, "<td></td>");
842 }
843
844 chunk_appendf(out,
845 /* frontend name, listener name */
846 "<td class=ac><a name=\"%s/+%s\"></a>%s"
847 "<a class=lfsb href=\"#%s/+%s\">%s</a>"
848 "",
849 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME),
Willy Tarreau708c4162019-10-09 10:19:16 +0200850 (flags & STAT_SHLGNDS)?"<u>":"",
William Lallemand74c24fb2016-11-21 17:18:36 +0100851 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME), field_str(stats, ST_F_SVNAME));
852
Willy Tarreau708c4162019-10-09 10:19:16 +0200853 if (flags & STAT_SHLGNDS) {
William Lallemand74c24fb2016-11-21 17:18:36 +0100854 chunk_appendf(out, "<div class=tips>");
855
Willy Tarreau90807112020-02-25 08:16:33 +0100856 if (isdigit((unsigned char)*field_str(stats, ST_F_ADDR)))
William Lallemand74c24fb2016-11-21 17:18:36 +0100857 chunk_appendf(out, "IPv4: %s, ", field_str(stats, ST_F_ADDR));
858 else if (*field_str(stats, ST_F_ADDR) == '[')
859 chunk_appendf(out, "IPv6: %s, ", field_str(stats, ST_F_ADDR));
860 else if (*field_str(stats, ST_F_ADDR))
861 chunk_appendf(out, "%s, ", field_str(stats, ST_F_ADDR));
862
863 /* id */
864 chunk_appendf(out, "id: %d</div>", stats[ST_F_SID].u.u32);
865 }
866
867 chunk_appendf(out,
868 /* queue */
869 "%s</td><td colspan=3></td>"
870 /* sessions rate: current, max, limit */
871 "<td colspan=3>&nbsp;</td>"
872 /* sessions: current, max, limit, total, lbtot, lastsess */
873 "<td>%s</td><td>%s</td><td>%s</td>"
874 "<td>%s</td><td>&nbsp;</td><td>&nbsp;</td>"
875 /* bytes: in, out */
876 "<td>%s</td><td>%s</td>"
877 "",
Willy Tarreau708c4162019-10-09 10:19:16 +0200878 (flags & STAT_SHLGNDS)?"</u>":"",
William Lallemand74c24fb2016-11-21 17:18:36 +0100879 U2H(stats[ST_F_SCUR].u.u32), U2H(stats[ST_F_SMAX].u.u32), U2H(stats[ST_F_SLIM].u.u32),
880 U2H(stats[ST_F_STOT].u.u64), U2H(stats[ST_F_BIN].u.u64), U2H(stats[ST_F_BOUT].u.u64));
881
882 chunk_appendf(out,
883 /* denied: req, resp */
884 "<td>%s</td><td>%s</td>"
885 /* errors: request, connect, response */
886 "<td>%s</td><td></td><td></td>"
887 /* warnings: retries, redispatches */
888 "<td></td><td></td>"
889 /* server status: reflect listener status */
890 "<td class=ac>%s</td>"
891 /* rest of server: nothing */
892 "<td class=ac colspan=8></td></tr>"
893 "",
894 U2H(stats[ST_F_DREQ].u.u64), U2H(stats[ST_F_DRESP].u.u64),
895 U2H(stats[ST_F_EREQ].u.u64),
896 field_str(stats, ST_F_STATUS));
897 }
898 else if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_SV) {
899 const char *style;
900
901 /* determine the style to use depending on the server's state,
902 * its health and weight. There isn't a 1-to-1 mapping between
903 * state and styles for the cases where the server is (still)
904 * up. The reason is that we don't want to report nolb and
905 * drain with the same color.
906 */
907
908 if (strcmp(field_str(stats, ST_F_STATUS), "DOWN") == 0 ||
909 strcmp(field_str(stats, ST_F_STATUS), "DOWN (agent)") == 0) {
910 style = "down";
911 }
912 else if (strcmp(field_str(stats, ST_F_STATUS), "DOWN ") == 0) {
913 style = "going_up";
914 }
Daniel Corbettb4285172020-03-28 12:35:50 -0400915 else if (strcmp(field_str(stats, ST_F_STATUS), "DRAIN") == 0) {
916 style = "draining";
917 }
William Lallemand74c24fb2016-11-21 17:18:36 +0100918 else if (strcmp(field_str(stats, ST_F_STATUS), "NOLB ") == 0) {
919 style = "going_down";
920 }
921 else if (strcmp(field_str(stats, ST_F_STATUS), "NOLB") == 0) {
922 style = "nolb";
923 }
924 else if (strcmp(field_str(stats, ST_F_STATUS), "no check") == 0) {
925 style = "no_check";
926 }
927 else if (!stats[ST_F_CHKFAIL].type ||
928 stats[ST_F_CHECK_HEALTH].u.u32 == stats[ST_F_CHECK_RISE].u.u32 + stats[ST_F_CHECK_FALL].u.u32 - 1) {
929 /* no check or max health = UP */
930 if (stats[ST_F_WEIGHT].u.u32)
931 style = "up";
932 else
933 style = "draining";
934 }
935 else {
936 style = "going_down";
937 }
938
939 if (memcmp(field_str(stats, ST_F_STATUS), "MAINT", 5) == 0)
940 chunk_appendf(out, "<tr class=\"maintain\">");
941 else
942 chunk_appendf(out,
943 "<tr class=\"%s_%s\">",
944 (stats[ST_F_BCK].u.u32) ? "backup" : "active", style);
945
946
Willy Tarreauab02b3f2019-10-09 11:11:46 +0200947 if (flags & STAT_ADMIN)
William Lallemand74c24fb2016-11-21 17:18:36 +0100948 chunk_appendf(out,
David Harrigand3db35a2016-12-30 12:12:49 +0000949 "<td><input class='%s-checkbox' type=\"checkbox\" name=\"s\" value=\"%s\"></td>",
950 field_str(stats, ST_F_PXNAME),
William Lallemand74c24fb2016-11-21 17:18:36 +0100951 field_str(stats, ST_F_SVNAME));
952
953 chunk_appendf(out,
954 "<td class=ac><a name=\"%s/%s\"></a>%s"
955 "<a class=lfsb href=\"#%s/%s\">%s</a>"
956 "",
957 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME),
Willy Tarreau708c4162019-10-09 10:19:16 +0200958 (flags & STAT_SHLGNDS) ? "<u>" : "",
William Lallemand74c24fb2016-11-21 17:18:36 +0100959 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME), field_str(stats, ST_F_SVNAME));
960
Willy Tarreau708c4162019-10-09 10:19:16 +0200961 if (flags & STAT_SHLGNDS) {
William Lallemand74c24fb2016-11-21 17:18:36 +0100962 chunk_appendf(out, "<div class=tips>");
963
Willy Tarreau90807112020-02-25 08:16:33 +0100964 if (isdigit((unsigned char)*field_str(stats, ST_F_ADDR)))
William Lallemand74c24fb2016-11-21 17:18:36 +0100965 chunk_appendf(out, "IPv4: %s, ", field_str(stats, ST_F_ADDR));
966 else if (*field_str(stats, ST_F_ADDR) == '[')
967 chunk_appendf(out, "IPv6: %s, ", field_str(stats, ST_F_ADDR));
968 else if (*field_str(stats, ST_F_ADDR))
969 chunk_appendf(out, "%s, ", field_str(stats, ST_F_ADDR));
970
971 /* id */
972 chunk_appendf(out, "id: %d", stats[ST_F_SID].u.u32);
973
974 /* cookie */
975 if (stats[ST_F_COOKIE].type) {
976 chunk_appendf(out, ", cookie: '");
977 chunk_initstr(&src, field_str(stats, ST_F_COOKIE));
978 chunk_htmlencode(out, &src);
979 chunk_appendf(out, "'");
980 }
981
982 chunk_appendf(out, "</div>");
983 }
984
985 chunk_appendf(out,
986 /* queue : current, max, limit */
987 "%s</td><td>%s</td><td>%s</td><td>%s</td>"
988 /* sessions rate : current, max, limit */
989 "<td>%s</td><td>%s</td><td></td>"
990 "",
Willy Tarreau708c4162019-10-09 10:19:16 +0200991 (flags & STAT_SHLGNDS) ? "</u>" : "",
William Lallemand74c24fb2016-11-21 17:18:36 +0100992 U2H(stats[ST_F_QCUR].u.u32), U2H(stats[ST_F_QMAX].u.u32), LIM2A(stats[ST_F_QLIMIT].u.u32, "-"),
993 U2H(stats[ST_F_RATE].u.u32), U2H(stats[ST_F_RATE_MAX].u.u32));
994
995 chunk_appendf(out,
996 /* sessions: current, max, limit, total */
Willy Tarreauf21d17b2019-09-08 09:24:56 +0200997 "<td><u>%s<div class=tips>"
998 "<table class=det>"
999 "<tr><th>Current active connections:</th><td>%s</td></tr>"
1000 "<tr><th>Current idle connections:</th><td>%s</td></tr>"
1001 "<tr><th>Active connections limit:</th><td>%s</td></tr>"
1002 "<tr><th>Idle connections limit:</th><td>%s</td></tr>"
1003 "</table></div></u>"
1004 "</td><td>%s</td><td>%s</td>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001005 "<td><u>%s<div class=tips><table class=det>"
1006 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
1007 "",
Willy Tarreauf21d17b2019-09-08 09:24:56 +02001008 U2H(stats[ST_F_SCUR].u.u32),
1009 U2H(stats[ST_F_SCUR].u.u32),
1010 U2H(stats[ST_F_SRV_ICUR].u.u32),
1011 LIM2A(stats[ST_F_SLIM].u.u32, "-"),
1012 stats[ST_F_SRV_ILIM].type ? U2H(stats[ST_F_SRV_ILIM].u.u32) : "-",
1013 U2H(stats[ST_F_SMAX].u.u32), LIM2A(stats[ST_F_SLIM].u.u32, "-"),
William Lallemand74c24fb2016-11-21 17:18:36 +01001014 U2H(stats[ST_F_STOT].u.u64),
1015 U2H(stats[ST_F_STOT].u.u64));
1016
1017 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
1018 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001019 chunk_appendf(out,
Willy Tarreauf1573842018-12-14 11:35:36 +01001020 "<tr><th>New connections:</th><td>%s</td></tr>"
1021 "<tr><th>Reused connections:</th><td>%s</td><td>(%d%%)</td></tr>"
Marcin Deranek3c27dda2020-05-15 18:32:51 +02001022 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001023 "<tr><th>- HTTP 1xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
1024 "<tr><th>- HTTP 2xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
1025 "<tr><th>- HTTP 3xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
1026 "<tr><th>- HTTP 4xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
1027 "<tr><th>- HTTP 5xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
1028 "<tr><th>- other responses:</th><td>%s</td><td>(%d%%)</td></tr>"
Willy Tarreau1b0f85e2018-05-28 15:12:40 +02001029 "<tr><th>Failed hdr rewrites:</th><td>%s</td></tr>"
Christopher Faulet0159ee42019-12-16 14:40:39 +01001030 "<tr><th>Internal error:</th><td>%s</td></tr>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001031 "",
Willy Tarreauf1573842018-12-14 11:35:36 +01001032 U2H(stats[ST_F_CONNECT].u.u64),
1033 U2H(stats[ST_F_REUSE].u.u64),
1034 (stats[ST_F_CONNECT].u.u64 + stats[ST_F_REUSE].u.u64) ?
1035 (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 +02001036 U2H(stats[ST_F_REQ_TOT].u.u64),
1037 U2H(stats[ST_F_HRSP_1XX].u.u64), stats[ST_F_REQ_TOT].u.u64 ?
1038 (int)(100 * stats[ST_F_HRSP_1XX].u.u64 / stats[ST_F_REQ_TOT].u.u64) : 0,
1039 U2H(stats[ST_F_HRSP_2XX].u.u64), stats[ST_F_REQ_TOT].u.u64 ?
1040 (int)(100 * stats[ST_F_HRSP_2XX].u.u64 / stats[ST_F_REQ_TOT].u.u64) : 0,
1041 U2H(stats[ST_F_HRSP_3XX].u.u64), stats[ST_F_REQ_TOT].u.u64 ?
1042 (int)(100 * stats[ST_F_HRSP_3XX].u.u64 / stats[ST_F_REQ_TOT].u.u64) : 0,
1043 U2H(stats[ST_F_HRSP_4XX].u.u64), stats[ST_F_REQ_TOT].u.u64 ?
1044 (int)(100 * stats[ST_F_HRSP_4XX].u.u64 / stats[ST_F_REQ_TOT].u.u64) : 0,
1045 U2H(stats[ST_F_HRSP_5XX].u.u64), stats[ST_F_REQ_TOT].u.u64 ?
1046 (int)(100 * stats[ST_F_HRSP_5XX].u.u64 / stats[ST_F_REQ_TOT].u.u64) : 0,
1047 U2H(stats[ST_F_HRSP_OTHER].u.u64), stats[ST_F_REQ_TOT].u.u64 ?
1048 (int)(100 * stats[ST_F_HRSP_OTHER].u.u64 / stats[ST_F_REQ_TOT].u.u64) : 0,
Christopher Faulet0159ee42019-12-16 14:40:39 +01001049 U2H(stats[ST_F_WREW].u.u64),
1050 U2H(stats[ST_F_EINT].u.u64));
William Lallemand74c24fb2016-11-21 17:18:36 +01001051 }
1052
Christopher Faulet0d1c2a62019-11-08 14:59:51 +01001053 chunk_appendf(out, "<tr><th colspan=3>Max / Avg over last 1024 success. conn.</th></tr>");
1054 chunk_appendf(out, "<tr><th>- Queue time:</th><td>%s / %s</td><td>ms</td></tr>",
1055 U2H(stats[ST_F_QT_MAX].u.u32), U2H(stats[ST_F_QTIME].u.u32));
1056 chunk_appendf(out, "<tr><th>- Connect time:</th><td>%s / %s</td><td>ms</td></tr>",
1057 U2H(stats[ST_F_CT_MAX].u.u32), U2H(stats[ST_F_CTIME].u.u32));
William Lallemand74c24fb2016-11-21 17:18:36 +01001058 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
Christopher Faulet0d1c2a62019-11-08 14:59:51 +01001059 chunk_appendf(out, "<tr><th>- Responses time:</th><td>%s / %s</td><td>ms</td></tr>",
1060 U2H(stats[ST_F_RT_MAX].u.u32), U2H(stats[ST_F_RTIME].u.u32));
1061 chunk_appendf(out, "<tr><th>- Total time:</th><td>%s / %s</td><td>ms</td></tr>",
1062 U2H(stats[ST_F_TT_MAX].u.u32), U2H(stats[ST_F_TTIME].u.u32));
William Lallemand74c24fb2016-11-21 17:18:36 +01001063
1064 chunk_appendf(out,
1065 "</table></div></u></td>"
1066 /* sessions: lbtot, last */
1067 "<td>%s</td><td>%s</td>",
1068 U2H(stats[ST_F_LBTOT].u.u64),
1069 human_time(stats[ST_F_LASTSESS].u.s32, 1));
1070
1071 chunk_appendf(out,
1072 /* bytes : in, out */
1073 "<td>%s</td><td>%s</td>"
1074 /* denied: req, resp */
1075 "<td></td><td>%s</td>"
1076 /* errors : request, connect */
1077 "<td></td><td>%s</td>"
1078 /* errors : response */
1079 "<td><u>%s<div class=tips>Connection resets during transfers: %lld client, %lld server</div></u></td>"
1080 /* warnings: retries, redispatches */
1081 "<td>%lld</td><td>%lld</td>"
1082 "",
1083 U2H(stats[ST_F_BIN].u.u64), U2H(stats[ST_F_BOUT].u.u64),
1084 U2H(stats[ST_F_DRESP].u.u64),
1085 U2H(stats[ST_F_ECON].u.u64),
1086 U2H(stats[ST_F_ERESP].u.u64),
1087 (long long)stats[ST_F_CLI_ABRT].u.u64,
1088 (long long)stats[ST_F_SRV_ABRT].u.u64,
1089 (long long)stats[ST_F_WRETR].u.u64,
1090 (long long)stats[ST_F_WREDIS].u.u64);
1091
1092 /* status, last change */
1093 chunk_appendf(out, "<td class=ac>");
1094
1095 /* FIXME!!!!
1096 * LASTCHG should contain the last change for *this* server and must be computed
1097 * properly above, as was done below, ie: this server if maint, otherwise ref server
1098 * if tracking. Note that ref is either local or remote depending on tracking.
1099 */
1100
1101
1102 if (memcmp(field_str(stats, ST_F_STATUS), "MAINT", 5) == 0) {
1103 chunk_appendf(out, "%s MAINT", human_time(stats[ST_F_LASTCHG].u.u32, 1));
1104 }
1105 else if (memcmp(field_str(stats, ST_F_STATUS), "no check", 5) == 0) {
1106 chunk_strcat(out, "<i>no check</i>");
1107 }
1108 else {
1109 chunk_appendf(out, "%s %s", human_time(stats[ST_F_LASTCHG].u.u32, 1), field_str(stats, ST_F_STATUS));
1110 if (memcmp(field_str(stats, ST_F_STATUS), "DOWN", 4) == 0) {
1111 if (stats[ST_F_CHECK_HEALTH].u.u32)
1112 chunk_strcat(out, " &uarr;");
1113 }
1114 else if (stats[ST_F_CHECK_HEALTH].u.u32 < stats[ST_F_CHECK_RISE].u.u32 + stats[ST_F_CHECK_FALL].u.u32 - 1)
1115 chunk_strcat(out, " &darr;");
1116 }
1117
1118 if (memcmp(field_str(stats, ST_F_STATUS), "DOWN", 4) == 0 &&
1119 stats[ST_F_AGENT_STATUS].type && !stats[ST_F_AGENT_HEALTH].u.u32) {
1120 chunk_appendf(out,
1121 "</td><td class=ac><u> %s",
1122 field_str(stats, ST_F_AGENT_STATUS));
1123
1124 if (stats[ST_F_AGENT_CODE].type)
1125 chunk_appendf(out, "/%d", stats[ST_F_AGENT_CODE].u.u32);
1126
1127 if (stats[ST_F_AGENT_DURATION].type)
1128 chunk_appendf(out, " in %lums", (long)stats[ST_F_AGENT_DURATION].u.u64);
1129
1130 chunk_appendf(out, "<div class=tips>%s", field_str(stats, ST_F_AGENT_DESC));
1131
1132 if (*field_str(stats, ST_F_LAST_AGT)) {
1133 chunk_appendf(out, ": ");
1134 chunk_initstr(&src, field_str(stats, ST_F_LAST_AGT));
1135 chunk_htmlencode(out, &src);
1136 }
1137 chunk_appendf(out, "</div></u>");
1138 }
1139 else if (stats[ST_F_CHECK_STATUS].type) {
1140 chunk_appendf(out,
1141 "</td><td class=ac><u> %s",
1142 field_str(stats, ST_F_CHECK_STATUS));
1143
1144 if (stats[ST_F_CHECK_CODE].type)
1145 chunk_appendf(out, "/%d", stats[ST_F_CHECK_CODE].u.u32);
1146
1147 if (stats[ST_F_CHECK_DURATION].type)
1148 chunk_appendf(out, " in %lums", (long)stats[ST_F_CHECK_DURATION].u.u64);
1149
1150 chunk_appendf(out, "<div class=tips>%s", field_str(stats, ST_F_CHECK_DESC));
1151
1152 if (*field_str(stats, ST_F_LAST_CHK)) {
1153 chunk_appendf(out, ": ");
1154 chunk_initstr(&src, field_str(stats, ST_F_LAST_CHK));
1155 chunk_htmlencode(out, &src);
1156 }
1157 chunk_appendf(out, "</div></u>");
1158 }
1159 else
1160 chunk_appendf(out, "</td><td>");
1161
1162 chunk_appendf(out,
1163 /* weight */
1164 "</td><td class=ac>%d</td>"
1165 /* act, bck */
1166 "<td class=ac>%s</td><td class=ac>%s</td>"
1167 "",
1168 stats[ST_F_WEIGHT].u.u32,
1169 stats[ST_F_BCK].u.u32 ? "-" : "Y",
1170 stats[ST_F_BCK].u.u32 ? "Y" : "-");
1171
1172 /* check failures: unique, fatal, down time */
1173 if (strcmp(field_str(stats, ST_F_STATUS), "MAINT (resolution)") == 0) {
1174 chunk_appendf(out, "<td class=ac colspan=3>resolution</td>");
1175 }
1176 else if (stats[ST_F_CHKFAIL].type) {
1177 chunk_appendf(out, "<td><u>%lld", (long long)stats[ST_F_CHKFAIL].u.u64);
1178
1179 if (stats[ST_F_HANAFAIL].type)
1180 chunk_appendf(out, "/%lld", (long long)stats[ST_F_HANAFAIL].u.u64);
1181
1182 chunk_appendf(out,
1183 "<div class=tips>Failed Health Checks%s</div></u></td>"
1184 "<td>%lld</td><td>%s</td>"
1185 "",
1186 stats[ST_F_HANAFAIL].type ? "/Health Analyses" : "",
1187 (long long)stats[ST_F_CHKDOWN].u.u64, human_time(stats[ST_F_DOWNTIME].u.u32, 1));
1188 }
1189 else if (strcmp(field_str(stats, ST_F_STATUS), "MAINT") != 0 && field_format(stats, ST_F_TRACKED) == FF_STR) {
1190 /* tracking a server (hence inherited maint would appear as "MAINT (via...)" */
1191 chunk_appendf(out,
1192 "<td class=ac colspan=3><a class=lfsb href=\"#%s\">via %s</a></td>",
1193 field_str(stats, ST_F_TRACKED), field_str(stats, ST_F_TRACKED));
1194 }
1195 else
1196 chunk_appendf(out, "<td colspan=3></td>");
1197
1198 /* throttle */
1199 if (stats[ST_F_THROTTLE].type)
1200 chunk_appendf(out, "<td class=ac>%d %%</td></tr>\n", stats[ST_F_THROTTLE].u.u32);
1201 else
1202 chunk_appendf(out, "<td class=ac>-</td></tr>\n");
1203 }
1204 else if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_BE) {
1205 chunk_appendf(out, "<tr class=\"backend\">");
Willy Tarreauab02b3f2019-10-09 11:11:46 +02001206 if (flags & STAT_ADMIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001207 /* Column sub-heading for Enable or Disable server */
1208 chunk_appendf(out, "<td></td>");
1209 }
1210 chunk_appendf(out,
1211 "<td class=ac>"
1212 /* name */
1213 "%s<a name=\"%s/Backend\"></a>"
1214 "<a class=lfsb href=\"#%s/Backend\">Backend</a>"
1215 "",
Willy Tarreau708c4162019-10-09 10:19:16 +02001216 (flags & STAT_SHLGNDS)?"<u>":"",
William Lallemand74c24fb2016-11-21 17:18:36 +01001217 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_PXNAME));
1218
Willy Tarreau708c4162019-10-09 10:19:16 +02001219 if (flags & STAT_SHLGNDS) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001220 /* balancing */
1221 chunk_appendf(out, "<div class=tips>balancing: %s",
1222 field_str(stats, ST_F_ALGO));
1223
1224 /* cookie */
1225 if (stats[ST_F_COOKIE].type) {
1226 chunk_appendf(out, ", cookie: '");
1227 chunk_initstr(&src, field_str(stats, ST_F_COOKIE));
1228 chunk_htmlencode(out, &src);
1229 chunk_appendf(out, "'");
1230 }
1231 chunk_appendf(out, "</div>");
1232 }
1233
1234 chunk_appendf(out,
1235 "%s</td>"
1236 /* queue : current, max */
1237 "<td>%s</td><td>%s</td><td></td>"
1238 /* sessions rate : current, max, limit */
1239 "<td>%s</td><td>%s</td><td></td>"
1240 "",
Willy Tarreau708c4162019-10-09 10:19:16 +02001241 (flags & STAT_SHLGNDS)?"</u>":"",
William Lallemand74c24fb2016-11-21 17:18:36 +01001242 U2H(stats[ST_F_QCUR].u.u32), U2H(stats[ST_F_QMAX].u.u32),
1243 U2H(stats[ST_F_RATE].u.u32), U2H(stats[ST_F_RATE_MAX].u.u32));
1244
1245 chunk_appendf(out,
1246 /* sessions: current, max, limit, total */
1247 "<td>%s</td><td>%s</td><td>%s</td>"
1248 "<td><u>%s<div class=tips><table class=det>"
1249 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
1250 "",
Willy Tarreau8e0f1752016-12-12 15:07:29 +01001251 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 +01001252 U2H(stats[ST_F_STOT].u.u64),
1253 U2H(stats[ST_F_STOT].u.u64));
1254
1255 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
1256 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0) {
1257 chunk_appendf(out,
Willy Tarreauf1573842018-12-14 11:35:36 +01001258 "<tr><th>New connections:</th><td>%s</td></tr>"
1259 "<tr><th>Reused connections:</th><td>%s</td><td>(%d%%)</td></tr>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001260 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
1261 "<tr><th>- HTTP 1xx responses:</th><td>%s</td></tr>"
1262 "<tr><th>- HTTP 2xx responses:</th><td>%s</td></tr>"
1263 "<tr><th>&nbsp;&nbsp;Compressed 2xx:</th><td>%s</td><td>(%d%%)</td></tr>"
1264 "<tr><th>- HTTP 3xx responses:</th><td>%s</td></tr>"
1265 "<tr><th>- HTTP 4xx responses:</th><td>%s</td></tr>"
1266 "<tr><th>- HTTP 5xx responses:</th><td>%s</td></tr>"
1267 "<tr><th>- other responses:</th><td>%s</td></tr>"
Willy Tarreaua1214a52018-12-14 14:00:25 +01001268 "<tr><th>Cache lookups:</th><td>%s</td></tr>"
1269 "<tr><th>Cache hits:</th><td>%s</td><td>(%d%%)</td></tr>"
Willy Tarreau1b0f85e2018-05-28 15:12:40 +02001270 "<tr><th>Failed hdr rewrites:</th><td>%s</td></tr>"
Christopher Faulet0159ee42019-12-16 14:40:39 +01001271 "<tr><th>Internal errors:</th><td>%s</td></tr>"
Christopher Faulet0d1c2a62019-11-08 14:59:51 +01001272 "",
Willy Tarreauf1573842018-12-14 11:35:36 +01001273 U2H(stats[ST_F_CONNECT].u.u64),
1274 U2H(stats[ST_F_REUSE].u.u64),
1275 (stats[ST_F_CONNECT].u.u64 + stats[ST_F_REUSE].u.u64) ?
1276 (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 +01001277 U2H(stats[ST_F_REQ_TOT].u.u64),
1278 U2H(stats[ST_F_HRSP_1XX].u.u64),
1279 U2H(stats[ST_F_HRSP_2XX].u.u64),
1280 U2H(stats[ST_F_COMP_RSP].u.u64),
1281 stats[ST_F_HRSP_2XX].u.u64 ?
1282 (int)(100 * stats[ST_F_COMP_RSP].u.u64 / stats[ST_F_HRSP_2XX].u.u64) : 0,
1283 U2H(stats[ST_F_HRSP_3XX].u.u64),
1284 U2H(stats[ST_F_HRSP_4XX].u.u64),
1285 U2H(stats[ST_F_HRSP_5XX].u.u64),
Willy Tarreaufeead3a2018-12-14 13:48:44 +01001286 U2H(stats[ST_F_HRSP_OTHER].u.u64),
Willy Tarreaua1214a52018-12-14 14:00:25 +01001287 U2H(stats[ST_F_CACHE_LOOKUPS].u.u64),
1288 U2H(stats[ST_F_CACHE_HITS].u.u64),
1289 stats[ST_F_CACHE_LOOKUPS].u.u64 ?
1290 (int)(100 * stats[ST_F_CACHE_HITS].u.u64 / stats[ST_F_CACHE_LOOKUPS].u.u64) : 0,
Christopher Faulet0159ee42019-12-16 14:40:39 +01001291 U2H(stats[ST_F_WREW].u.u64),
1292 U2H(stats[ST_F_EINT].u.u64));
William Lallemand74c24fb2016-11-21 17:18:36 +01001293 }
1294
Christopher Faulet0d1c2a62019-11-08 14:59:51 +01001295 chunk_appendf(out, "<tr><th colspan=3>Max / Avg over last 1024 success. conn.</th></tr>");
1296 chunk_appendf(out, "<tr><th>- Queue time:</th><td>%s / %s</td><td>ms</td></tr>",
1297 U2H(stats[ST_F_QT_MAX].u.u32), U2H(stats[ST_F_QTIME].u.u32));
1298 chunk_appendf(out, "<tr><th>- Connect time:</th><td>%s / %s</td><td>ms</td></tr>",
1299 U2H(stats[ST_F_CT_MAX].u.u32), U2H(stats[ST_F_CTIME].u.u32));
William Lallemand74c24fb2016-11-21 17:18:36 +01001300 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
Christopher Faulet0d1c2a62019-11-08 14:59:51 +01001301 chunk_appendf(out, "<tr><th>- Responses time:</th><td>%s / %s</td><td>ms</td></tr>",
1302 U2H(stats[ST_F_RT_MAX].u.u32), U2H(stats[ST_F_RTIME].u.u32));
1303 chunk_appendf(out, "<tr><th>- Total time:</th><td>%s / %s</td><td>ms</td></tr>",
1304 U2H(stats[ST_F_TT_MAX].u.u32), U2H(stats[ST_F_TTIME].u.u32));
William Lallemand74c24fb2016-11-21 17:18:36 +01001305
1306 chunk_appendf(out,
1307 "</table></div></u></td>"
1308 /* sessions: lbtot, last */
1309 "<td>%s</td><td>%s</td>"
1310 /* bytes: in */
1311 "<td>%s</td>"
1312 "",
1313 U2H(stats[ST_F_LBTOT].u.u64),
1314 human_time(stats[ST_F_LASTSESS].u.s32, 1),
1315 U2H(stats[ST_F_BIN].u.u64));
1316
1317 chunk_appendf(out,
1318 /* bytes:out + compression stats (via hover): comp_in, comp_out, comp_byp */
1319 "<td>%s%s<div class=tips><table class=det>"
1320 "<tr><th>Response bytes in:</th><td>%s</td></tr>"
1321 "<tr><th>Compression in:</th><td>%s</td></tr>"
1322 "<tr><th>Compression out:</th><td>%s</td><td>(%d%%)</td></tr>"
1323 "<tr><th>Compression bypass:</th><td>%s</td></tr>"
1324 "<tr><th>Total bytes saved:</th><td>%s</td><td>(%d%%)</td></tr>"
1325 "</table></div>%s</td>",
1326 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "<u>":"",
1327 U2H(stats[ST_F_BOUT].u.u64),
1328 U2H(stats[ST_F_BOUT].u.u64),
1329 U2H(stats[ST_F_COMP_IN].u.u64),
1330 U2H(stats[ST_F_COMP_OUT].u.u64),
1331 stats[ST_F_COMP_IN].u.u64 ? (int)(stats[ST_F_COMP_OUT].u.u64 * 100 / stats[ST_F_COMP_IN].u.u64) : 0,
1332 U2H(stats[ST_F_COMP_BYP].u.u64),
1333 U2H(stats[ST_F_COMP_IN].u.u64 - stats[ST_F_COMP_OUT].u.u64),
1334 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,
1335 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "</u>":"");
1336
1337 chunk_appendf(out,
1338 /* denied: req, resp */
1339 "<td>%s</td><td>%s</td>"
1340 /* errors : request, connect */
1341 "<td></td><td>%s</td>"
1342 /* errors : response */
1343 "<td><u>%s<div class=tips>Connection resets during transfers: %lld client, %lld server</div></u></td>"
1344 /* warnings: retries, redispatches */
1345 "<td>%lld</td><td>%lld</td>"
1346 /* backend status: reflect backend status (up/down): we display UP
1347 * if the backend has known working servers or if it has no server at
1348 * all (eg: for stats). Then we display the total weight, number of
1349 * active and backups. */
1350 "<td class=ac>%s %s</td><td class=ac>&nbsp;</td><td class=ac>%d</td>"
1351 "<td class=ac>%d</td><td class=ac>%d</td>"
1352 "",
1353 U2H(stats[ST_F_DREQ].u.u64), U2H(stats[ST_F_DRESP].u.u64),
1354 U2H(stats[ST_F_ECON].u.u64),
1355 U2H(stats[ST_F_ERESP].u.u64),
1356 (long long)stats[ST_F_CLI_ABRT].u.u64,
1357 (long long)stats[ST_F_SRV_ABRT].u.u64,
1358 (long long)stats[ST_F_WRETR].u.u64, (long long)stats[ST_F_WREDIS].u.u64,
1359 human_time(stats[ST_F_LASTCHG].u.u32, 1),
1360 strcmp(field_str(stats, ST_F_STATUS), "DOWN") ? field_str(stats, ST_F_STATUS) : "<font color=\"red\"><b>DOWN</b></font>",
1361 stats[ST_F_WEIGHT].u.u32,
1362 stats[ST_F_ACT].u.u32, stats[ST_F_BCK].u.u32);
1363
1364 chunk_appendf(out,
1365 /* rest of backend: nothing, down transitions, total downtime, throttle */
1366 "<td class=ac>&nbsp;</td><td>%d</td>"
1367 "<td>%s</td>"
1368 "<td></td>"
1369 "</tr>",
1370 stats[ST_F_CHKDOWN].u.u32,
1371 stats[ST_F_DOWNTIME].type ? human_time(stats[ST_F_DOWNTIME].u.u32, 1) : "&nbsp;");
1372 }
1373 return 1;
1374}
1375
Willy Tarreau43241ff2019-10-09 11:27:51 +02001376static int stats_dump_one_line(const struct field *stats, struct proxy *px, struct appctx *appctx)
William Lallemand74c24fb2016-11-21 17:18:36 +01001377{
Simon Horman05ee2132017-01-04 09:37:25 +01001378 int ret;
1379
William Lallemand74c24fb2016-11-21 17:18:36 +01001380 if (appctx->ctx.stats.flags & STAT_FMT_HTML)
Willy Tarreau43241ff2019-10-09 11:27:51 +02001381 ret = stats_dump_fields_html(&trash, stats, appctx->ctx.stats.flags);
William Lallemand74c24fb2016-11-21 17:18:36 +01001382 else if (appctx->ctx.stats.flags & STAT_FMT_TYPED)
Willy Tarreau43241ff2019-10-09 11:27:51 +02001383 ret = stats_dump_fields_typed(&trash, stats, appctx->ctx.stats.flags);
Simon Horman05ee2132017-01-04 09:37:25 +01001384 else if (appctx->ctx.stats.flags & STAT_FMT_JSON)
Willy Tarreaub0ce3ad2019-10-09 11:19:29 +02001385 ret = stats_dump_fields_json(&trash, stats, appctx->ctx.stats.flags);
William Lallemand74c24fb2016-11-21 17:18:36 +01001386 else
Willy Tarreau43241ff2019-10-09 11:27:51 +02001387 ret = stats_dump_fields_csv(&trash, stats, appctx->ctx.stats.flags);
Simon Horman05ee2132017-01-04 09:37:25 +01001388
1389 if (ret)
1390 appctx->ctx.stats.flags |= STAT_STARTED;
1391
1392 return ret;
William Lallemand74c24fb2016-11-21 17:18:36 +01001393}
1394
1395/* Fill <stats> with the frontend statistics. <stats> is
1396 * preallocated array of length <len>. The length of the array
1397 * must be at least ST_F_TOTAL_FIELDS. If this length is less then
1398 * this value, the function returns 0, otherwise, it returns 1.
1399 */
1400int stats_fill_fe_stats(struct proxy *px, struct field *stats, int len)
1401{
1402 if (len < ST_F_TOTAL_FIELDS)
1403 return 0;
1404
1405 memset(stats, 0, sizeof(*stats) * len);
1406
1407 stats[ST_F_PXNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
1408 stats[ST_F_SVNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, "FRONTEND");
1409 stats[ST_F_MODE] = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
1410 stats[ST_F_SCUR] = mkf_u32(0, px->feconn);
1411 stats[ST_F_SMAX] = mkf_u32(FN_MAX, px->fe_counters.conn_max);
1412 stats[ST_F_SLIM] = mkf_u32(FO_CONFIG|FN_LIMIT, px->maxconn);
1413 stats[ST_F_STOT] = mkf_u64(FN_COUNTER, px->fe_counters.cum_sess);
1414 stats[ST_F_BIN] = mkf_u64(FN_COUNTER, px->fe_counters.bytes_in);
1415 stats[ST_F_BOUT] = mkf_u64(FN_COUNTER, px->fe_counters.bytes_out);
1416 stats[ST_F_DREQ] = mkf_u64(FN_COUNTER, px->fe_counters.denied_req);
1417 stats[ST_F_DRESP] = mkf_u64(FN_COUNTER, px->fe_counters.denied_resp);
1418 stats[ST_F_EREQ] = mkf_u64(FN_COUNTER, px->fe_counters.failed_req);
1419 stats[ST_F_DCON] = mkf_u64(FN_COUNTER, px->fe_counters.denied_conn);
1420 stats[ST_F_DSES] = mkf_u64(FN_COUNTER, px->fe_counters.denied_sess);
1421 stats[ST_F_STATUS] = mkf_str(FO_STATUS, px->state == PR_STREADY ? "OPEN" : px->state == PR_STFULL ? "FULL" : "STOP");
1422 stats[ST_F_PID] = mkf_u32(FO_KEY, relative_pid);
1423 stats[ST_F_IID] = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
1424 stats[ST_F_SID] = mkf_u32(FO_KEY|FS_SERVICE, 0);
1425 stats[ST_F_TYPE] = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_FE);
1426 stats[ST_F_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&px->fe_sess_per_sec));
1427 stats[ST_F_RATE_LIM] = mkf_u32(FO_CONFIG|FN_LIMIT, px->fe_sps_lim);
1428 stats[ST_F_RATE_MAX] = mkf_u32(FN_MAX, px->fe_counters.sps_max);
Tim Duesterhus3fd19732018-05-27 20:35:08 +02001429 stats[ST_F_WREW] = mkf_u64(FN_COUNTER, px->fe_counters.failed_rewrites);
Christopher Faulet0159ee42019-12-16 14:40:39 +01001430 stats[ST_F_EINT] = mkf_u64(FN_COUNTER, px->fe_counters.internal_errors);
William Lallemand74c24fb2016-11-21 17:18:36 +01001431
1432 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
1433 if (px->mode == PR_MODE_HTTP) {
1434 stats[ST_F_HRSP_1XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[1]);
1435 stats[ST_F_HRSP_2XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[2]);
1436 stats[ST_F_HRSP_3XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[3]);
1437 stats[ST_F_HRSP_4XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[4]);
1438 stats[ST_F_HRSP_5XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[5]);
1439 stats[ST_F_HRSP_OTHER] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[0]);
1440 stats[ST_F_INTERCEPTED] = mkf_u64(FN_COUNTER, px->fe_counters.intercepted_req);
Willy Tarreaua1214a52018-12-14 14:00:25 +01001441 stats[ST_F_CACHE_LOOKUPS] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.cache_lookups);
1442 stats[ST_F_CACHE_HITS] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.cache_hits);
William Lallemand74c24fb2016-11-21 17:18:36 +01001443 }
1444
1445 /* requests : req_rate, req_rate_max, req_tot, */
1446 stats[ST_F_REQ_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&px->fe_req_per_sec));
1447 stats[ST_F_REQ_RATE_MAX] = mkf_u32(FN_MAX, px->fe_counters.p.http.rps_max);
1448 stats[ST_F_REQ_TOT] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.cum_req);
1449
1450 /* compression: in, out, bypassed, responses */
1451 stats[ST_F_COMP_IN] = mkf_u64(FN_COUNTER, px->fe_counters.comp_in);
1452 stats[ST_F_COMP_OUT] = mkf_u64(FN_COUNTER, px->fe_counters.comp_out);
1453 stats[ST_F_COMP_BYP] = mkf_u64(FN_COUNTER, px->fe_counters.comp_byp);
1454 stats[ST_F_COMP_RSP] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.comp_rsp);
1455
1456 /* connections : conn_rate, conn_rate_max, conn_tot, conn_max */
1457 stats[ST_F_CONN_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&px->fe_conn_per_sec));
1458 stats[ST_F_CONN_RATE_MAX] = mkf_u32(FN_MAX, px->fe_counters.cps_max);
1459 stats[ST_F_CONN_TOT] = mkf_u64(FN_COUNTER, px->fe_counters.cum_conn);
1460
1461 return 1;
1462}
1463
1464/* Dumps a frontend's line to the trash for the current proxy <px> and uses
1465 * the state from stream interface <si>. The caller is responsible for clearing
1466 * the trash if needed. Returns non-zero if it emits anything, zero otherwise.
1467 */
1468static int stats_dump_fe_stats(struct stream_interface *si, struct proxy *px)
1469{
1470 struct appctx *appctx = __objt_appctx(si->end);
1471
1472 if (!(px->cap & PR_CAP_FE))
1473 return 0;
1474
1475 if ((appctx->ctx.stats.flags & STAT_BOUND) && !(appctx->ctx.stats.type & (1 << STATS_TYPE_FE)))
1476 return 0;
1477
1478 if (!stats_fill_fe_stats(px, stats, ST_F_TOTAL_FIELDS))
1479 return 0;
1480
Willy Tarreau43241ff2019-10-09 11:27:51 +02001481 return stats_dump_one_line(stats, px, appctx);
William Lallemand74c24fb2016-11-21 17:18:36 +01001482}
1483
1484/* Fill <stats> with the listener statistics. <stats> is
1485 * preallocated array of length <len>. The length of the array
1486 * must be at least ST_F_TOTAL_FIELDS. If this length is less
1487 * then this value, the function returns 0, otherwise, it
Willy Tarreau708c4162019-10-09 10:19:16 +02001488 * returns 1. <flags> can take the value STAT_SHLGNDS.
William Lallemand74c24fb2016-11-21 17:18:36 +01001489 */
1490int stats_fill_li_stats(struct proxy *px, struct listener *l, int flags,
1491 struct field *stats, int len)
1492{
Willy Tarreau83061a82018-07-13 11:56:34 +02001493 struct buffer *out = get_trash_chunk();
William Lallemand74c24fb2016-11-21 17:18:36 +01001494
1495 if (len < ST_F_TOTAL_FIELDS)
1496 return 0;
1497
1498 if (!l->counters)
1499 return 0;
1500
1501 chunk_reset(out);
1502 memset(stats, 0, sizeof(*stats) * len);
1503
1504 stats[ST_F_PXNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
1505 stats[ST_F_SVNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, l->name);
1506 stats[ST_F_MODE] = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
1507 stats[ST_F_SCUR] = mkf_u32(0, l->nbconn);
1508 stats[ST_F_SMAX] = mkf_u32(FN_MAX, l->counters->conn_max);
1509 stats[ST_F_SLIM] = mkf_u32(FO_CONFIG|FN_LIMIT, l->maxconn);
1510 stats[ST_F_STOT] = mkf_u64(FN_COUNTER, l->counters->cum_conn);
1511 stats[ST_F_BIN] = mkf_u64(FN_COUNTER, l->counters->bytes_in);
1512 stats[ST_F_BOUT] = mkf_u64(FN_COUNTER, l->counters->bytes_out);
1513 stats[ST_F_DREQ] = mkf_u64(FN_COUNTER, l->counters->denied_req);
1514 stats[ST_F_DRESP] = mkf_u64(FN_COUNTER, l->counters->denied_resp);
1515 stats[ST_F_EREQ] = mkf_u64(FN_COUNTER, l->counters->failed_req);
1516 stats[ST_F_DCON] = mkf_u64(FN_COUNTER, l->counters->denied_conn);
1517 stats[ST_F_DSES] = mkf_u64(FN_COUNTER, l->counters->denied_sess);
Willy Tarreaua8cf66b2019-02-27 16:49:00 +01001518 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 +01001519 stats[ST_F_PID] = mkf_u32(FO_KEY, relative_pid);
1520 stats[ST_F_IID] = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
1521 stats[ST_F_SID] = mkf_u32(FO_KEY|FS_SERVICE, l->luid);
1522 stats[ST_F_TYPE] = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_SO);
Tim Duesterhus3fd19732018-05-27 20:35:08 +02001523 stats[ST_F_WREW] = mkf_u64(FN_COUNTER, l->counters->failed_rewrites);
Christopher Faulet0159ee42019-12-16 14:40:39 +01001524 stats[ST_F_EINT] = mkf_u64(FN_COUNTER, l->counters->internal_errors);
William Lallemand74c24fb2016-11-21 17:18:36 +01001525
Willy Tarreau708c4162019-10-09 10:19:16 +02001526 if (flags & STAT_SHLGNDS) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001527 char str[INET6_ADDRSTRLEN];
1528 int port;
1529
1530 port = get_host_port(&l->addr);
1531 switch (addr_to_str(&l->addr, str, sizeof(str))) {
1532 case AF_INET:
1533 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
1534 chunk_appendf(out, "%s:%d", str, port);
1535 break;
1536 case AF_INET6:
1537 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
1538 chunk_appendf(out, "[%s]:%d", str, port);
1539 break;
1540 case AF_UNIX:
1541 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, "unix");
1542 break;
1543 case -1:
1544 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
1545 chunk_strcat(out, strerror(errno));
1546 break;
1547 default: /* address family not supported */
1548 break;
1549 }
1550 }
1551
1552 return 1;
1553}
1554
1555/* Dumps a line for listener <l> and proxy <px> to the trash and uses the state
Willy Tarreau43241ff2019-10-09 11:27:51 +02001556 * from stream interface <si>. The caller is responsible for clearing the trash
1557 * if needed. Returns non-zero if it emits anything, zero otherwise.
William Lallemand74c24fb2016-11-21 17:18:36 +01001558 */
Willy Tarreau43241ff2019-10-09 11:27:51 +02001559static int stats_dump_li_stats(struct stream_interface *si, struct proxy *px, struct listener *l)
William Lallemand74c24fb2016-11-21 17:18:36 +01001560{
1561 struct appctx *appctx = __objt_appctx(si->end);
1562
Willy Tarreau43241ff2019-10-09 11:27:51 +02001563 if (!stats_fill_li_stats(px, l, appctx->ctx.stats.flags, stats, ST_F_TOTAL_FIELDS))
William Lallemand74c24fb2016-11-21 17:18:36 +01001564 return 0;
1565
Willy Tarreau43241ff2019-10-09 11:27:51 +02001566 return stats_dump_one_line(stats, px, appctx);
William Lallemand74c24fb2016-11-21 17:18:36 +01001567}
1568
1569enum srv_stats_state {
1570 SRV_STATS_STATE_DOWN = 0,
1571 SRV_STATS_STATE_DOWN_AGENT,
1572 SRV_STATS_STATE_GOING_UP,
1573 SRV_STATS_STATE_UP_GOING_DOWN,
1574 SRV_STATS_STATE_UP,
1575 SRV_STATS_STATE_NOLB_GOING_DOWN,
1576 SRV_STATS_STATE_NOLB,
1577 SRV_STATS_STATE_DRAIN_GOING_DOWN,
1578 SRV_STATS_STATE_DRAIN,
1579 SRV_STATS_STATE_DRAIN_AGENT,
1580 SRV_STATS_STATE_NO_CHECK,
1581
1582 SRV_STATS_STATE_COUNT, /* Must be last */
1583};
1584
1585static const char *srv_hlt_st[SRV_STATS_STATE_COUNT] = {
1586 [SRV_STATS_STATE_DOWN] = "DOWN",
1587 [SRV_STATS_STATE_DOWN_AGENT] = "DOWN (agent)",
1588 [SRV_STATS_STATE_GOING_UP] = "DOWN %d/%d",
1589 [SRV_STATS_STATE_UP_GOING_DOWN] = "UP %d/%d",
1590 [SRV_STATS_STATE_UP] = "UP",
1591 [SRV_STATS_STATE_NOLB_GOING_DOWN] = "NOLB %d/%d",
1592 [SRV_STATS_STATE_NOLB] = "NOLB",
1593 [SRV_STATS_STATE_DRAIN_GOING_DOWN] = "DRAIN %d/%d",
1594 [SRV_STATS_STATE_DRAIN] = "DRAIN",
1595 [SRV_STATS_STATE_DRAIN_AGENT] = "DRAIN (agent)",
1596 [SRV_STATS_STATE_NO_CHECK] = "no check"
1597};
1598
1599/* Fill <stats> with the server statistics. <stats> is
1600 * preallocated array of length <len>. The length of the array
1601 * must be at least ST_F_TOTAL_FIELDS. If this length is less
1602 * then this value, the function returns 0, otherwise, it
Willy Tarreau708c4162019-10-09 10:19:16 +02001603 * returns 1. <flags> can take the value STAT_SHLGNDS.
William Lallemand74c24fb2016-11-21 17:18:36 +01001604 */
1605int stats_fill_sv_stats(struct proxy *px, struct server *sv, int flags,
1606 struct field *stats, int len)
1607{
1608 struct server *via, *ref;
1609 char str[INET6_ADDRSTRLEN];
Willy Tarreau83061a82018-07-13 11:56:34 +02001610 struct buffer *out = get_trash_chunk();
William Lallemand74c24fb2016-11-21 17:18:36 +01001611 enum srv_stats_state state;
1612 char *fld_status;
Marcin Deraneka8dbdf32020-05-15 20:02:40 +02001613 long long srv_samples_counter;
1614 unsigned int srv_samples_window = TIME_STATS_SAMPLES;
William Lallemand74c24fb2016-11-21 17:18:36 +01001615
1616 if (len < ST_F_TOTAL_FIELDS)
1617 return 0;
1618
1619 memset(stats, 0, sizeof(*stats) * len);
1620
1621 /* we have "via" which is the tracked server as described in the configuration,
1622 * and "ref" which is the checked server and the end of the chain.
1623 */
1624 via = sv->track ? sv->track : sv;
1625 ref = via;
1626 while (ref->track)
1627 ref = ref->track;
1628
Emeric Brun52a91d32017-08-31 14:41:55 +02001629 if (sv->cur_state == SRV_ST_RUNNING || sv->cur_state == SRV_ST_STARTING) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001630 if ((ref->check.state & CHK_ST_ENABLED) &&
1631 (ref->check.health < ref->check.rise + ref->check.fall - 1)) {
1632 state = SRV_STATS_STATE_UP_GOING_DOWN;
1633 } else {
1634 state = SRV_STATS_STATE_UP;
1635 }
1636
Emeric Brun52a91d32017-08-31 14:41:55 +02001637 if (sv->cur_admin & SRV_ADMF_DRAIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001638 if (ref->agent.state & CHK_ST_ENABLED)
1639 state = SRV_STATS_STATE_DRAIN_AGENT;
1640 else if (state == SRV_STATS_STATE_UP_GOING_DOWN)
1641 state = SRV_STATS_STATE_DRAIN_GOING_DOWN;
1642 else
1643 state = SRV_STATS_STATE_DRAIN;
1644 }
1645
1646 if (state == SRV_STATS_STATE_UP && !(ref->check.state & CHK_ST_ENABLED)) {
1647 state = SRV_STATS_STATE_NO_CHECK;
1648 }
1649 }
Emeric Brun52a91d32017-08-31 14:41:55 +02001650 else if (sv->cur_state == SRV_ST_STOPPING) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001651 if ((!(sv->check.state & CHK_ST_ENABLED) && !sv->track) ||
1652 (ref->check.health == ref->check.rise + ref->check.fall - 1)) {
1653 state = SRV_STATS_STATE_NOLB;
1654 } else {
1655 state = SRV_STATS_STATE_NOLB_GOING_DOWN;
1656 }
1657 }
1658 else { /* stopped */
1659 if ((ref->agent.state & CHK_ST_ENABLED) && !ref->agent.health) {
1660 state = SRV_STATS_STATE_DOWN_AGENT;
1661 } else if ((ref->check.state & CHK_ST_ENABLED) && !ref->check.health) {
1662 state = SRV_STATS_STATE_DOWN; /* DOWN */
1663 } else if ((ref->agent.state & CHK_ST_ENABLED) || (ref->check.state & CHK_ST_ENABLED)) {
1664 state = SRV_STATS_STATE_GOING_UP;
1665 } else {
1666 state = SRV_STATS_STATE_DOWN; /* DOWN, unchecked */
1667 }
1668 }
1669
1670 chunk_reset(out);
1671
1672 stats[ST_F_PXNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
1673 stats[ST_F_SVNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, sv->id);
1674 stats[ST_F_MODE] = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
1675 stats[ST_F_QCUR] = mkf_u32(0, sv->nbpend);
1676 stats[ST_F_QMAX] = mkf_u32(FN_MAX, sv->counters.nbpend_max);
1677 stats[ST_F_SCUR] = mkf_u32(0, sv->cur_sess);
1678 stats[ST_F_SMAX] = mkf_u32(FN_MAX, sv->counters.cur_sess_max);
1679
1680 if (sv->maxconn)
1681 stats[ST_F_SLIM] = mkf_u32(FO_CONFIG|FN_LIMIT, sv->maxconn);
1682
Willy Tarreauf21d17b2019-09-08 09:24:56 +02001683 stats[ST_F_SRV_ICUR] = mkf_u32(0, sv->curr_idle_conns);
1684 if (sv->max_idle_conns != -1)
1685 stats[ST_F_SRV_ILIM] = mkf_u32(FO_CONFIG|FN_LIMIT, sv->max_idle_conns);
1686
William Lallemand74c24fb2016-11-21 17:18:36 +01001687 stats[ST_F_STOT] = mkf_u64(FN_COUNTER, sv->counters.cum_sess);
1688 stats[ST_F_BIN] = mkf_u64(FN_COUNTER, sv->counters.bytes_in);
1689 stats[ST_F_BOUT] = mkf_u64(FN_COUNTER, sv->counters.bytes_out);
Christopher Fauleta08546b2019-12-16 16:07:34 +01001690 stats[ST_F_DRESP] = mkf_u64(FN_COUNTER, sv->counters.denied_resp);
William Lallemand74c24fb2016-11-21 17:18:36 +01001691 stats[ST_F_ECON] = mkf_u64(FN_COUNTER, sv->counters.failed_conns);
1692 stats[ST_F_ERESP] = mkf_u64(FN_COUNTER, sv->counters.failed_resp);
1693 stats[ST_F_WRETR] = mkf_u64(FN_COUNTER, sv->counters.retries);
1694 stats[ST_F_WREDIS] = mkf_u64(FN_COUNTER, sv->counters.redispatches);
Tim Duesterhus3fd19732018-05-27 20:35:08 +02001695 stats[ST_F_WREW] = mkf_u64(FN_COUNTER, sv->counters.failed_rewrites);
Christopher Faulet0159ee42019-12-16 14:40:39 +01001696 stats[ST_F_EINT] = mkf_u64(FN_COUNTER, sv->counters.internal_errors);
Willy Tarreauf1573842018-12-14 11:35:36 +01001697 stats[ST_F_CONNECT] = mkf_u64(FN_COUNTER, sv->counters.connect);
1698 stats[ST_F_REUSE] = mkf_u64(FN_COUNTER, sv->counters.reuse);
William Lallemand74c24fb2016-11-21 17:18:36 +01001699
1700 /* status */
1701 fld_status = chunk_newstr(out);
Emeric Brun52a91d32017-08-31 14:41:55 +02001702 if (sv->cur_admin & SRV_ADMF_RMAINT)
William Lallemand74c24fb2016-11-21 17:18:36 +01001703 chunk_appendf(out, "MAINT (resolution)");
Emeric Brun52a91d32017-08-31 14:41:55 +02001704 else if (sv->cur_admin & SRV_ADMF_IMAINT)
William Lallemand74c24fb2016-11-21 17:18:36 +01001705 chunk_appendf(out, "MAINT (via %s/%s)", via->proxy->id, via->id);
Emeric Brun52a91d32017-08-31 14:41:55 +02001706 else if (sv->cur_admin & SRV_ADMF_MAINT)
William Lallemand74c24fb2016-11-21 17:18:36 +01001707 chunk_appendf(out, "MAINT");
1708 else
1709 chunk_appendf(out,
1710 srv_hlt_st[state],
Emeric Brun52a91d32017-08-31 14:41:55 +02001711 (ref->cur_state != SRV_ST_STOPPED) ? (ref->check.health - ref->check.rise + 1) : (ref->check.health),
1712 (ref->cur_state != SRV_ST_STOPPED) ? (ref->check.fall) : (ref->check.rise));
William Lallemand74c24fb2016-11-21 17:18:36 +01001713
1714 stats[ST_F_STATUS] = mkf_str(FO_STATUS, fld_status);
1715 stats[ST_F_LASTCHG] = mkf_u32(FN_AGE, now.tv_sec - sv->last_change);
Emeric Brun52a91d32017-08-31 14:41:55 +02001716 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 +01001717 stats[ST_F_ACT] = mkf_u32(FO_STATUS, (sv->flags & SRV_F_BACKUP) ? 0 : 1);
1718 stats[ST_F_BCK] = mkf_u32(FO_STATUS, (sv->flags & SRV_F_BACKUP) ? 1 : 0);
1719
1720 /* check failures: unique, fatal; last change, total downtime */
1721 if (sv->check.state & CHK_ST_ENABLED) {
1722 stats[ST_F_CHKFAIL] = mkf_u64(FN_COUNTER, sv->counters.failed_checks);
1723 stats[ST_F_CHKDOWN] = mkf_u64(FN_COUNTER, sv->counters.down_trans);
1724 stats[ST_F_DOWNTIME] = mkf_u32(FN_COUNTER, srv_downtime(sv));
1725 }
1726
1727 if (sv->maxqueue)
1728 stats[ST_F_QLIMIT] = mkf_u32(FO_CONFIG|FS_SERVICE, sv->maxqueue);
1729
1730 stats[ST_F_PID] = mkf_u32(FO_KEY, relative_pid);
1731 stats[ST_F_IID] = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
1732 stats[ST_F_SID] = mkf_u32(FO_KEY|FS_SERVICE, sv->puid);
1733
Emeric Brun52a91d32017-08-31 14:41:55 +02001734 if (sv->cur_state == SRV_ST_STARTING && !server_is_draining(sv))
William Lallemand74c24fb2016-11-21 17:18:36 +01001735 stats[ST_F_THROTTLE] = mkf_u32(FN_AVG, server_throttle_rate(sv));
1736
1737 stats[ST_F_LBTOT] = mkf_u64(FN_COUNTER, sv->counters.cum_lbconn);
1738
1739 if (sv->track) {
1740 char *fld_track = chunk_newstr(out);
1741
1742 chunk_appendf(out, "%s/%s", sv->track->proxy->id, sv->track->id);
1743 stats[ST_F_TRACKED] = mkf_str(FO_CONFIG|FN_NAME|FS_SERVICE, fld_track);
1744 }
1745
1746 stats[ST_F_TYPE] = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_SV);
1747 stats[ST_F_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&sv->sess_per_sec));
1748 stats[ST_F_RATE_MAX] = mkf_u32(FN_MAX, sv->counters.sps_max);
1749
1750 if ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED) {
1751 const char *fld_chksts;
1752
1753 fld_chksts = chunk_newstr(out);
1754 chunk_strcat(out, "* "); // for check in progress
1755 chunk_strcat(out, get_check_status_info(sv->check.status));
1756 if (!(sv->check.state & CHK_ST_INPROGRESS))
1757 fld_chksts += 2; // skip "* "
1758 stats[ST_F_CHECK_STATUS] = mkf_str(FN_OUTPUT, fld_chksts);
1759
1760 if (sv->check.status >= HCHK_STATUS_L57DATA)
1761 stats[ST_F_CHECK_CODE] = mkf_u32(FN_OUTPUT, sv->check.code);
1762
1763 if (sv->check.status >= HCHK_STATUS_CHECKED)
1764 stats[ST_F_CHECK_DURATION] = mkf_u64(FN_DURATION, sv->check.duration);
1765
1766 stats[ST_F_CHECK_DESC] = mkf_str(FN_OUTPUT, get_check_status_description(sv->check.status));
1767 stats[ST_F_LAST_CHK] = mkf_str(FN_OUTPUT, sv->check.desc);
1768 stats[ST_F_CHECK_RISE] = mkf_u32(FO_CONFIG|FS_SERVICE, ref->check.rise);
1769 stats[ST_F_CHECK_FALL] = mkf_u32(FO_CONFIG|FS_SERVICE, ref->check.fall);
1770 stats[ST_F_CHECK_HEALTH] = mkf_u32(FO_CONFIG|FS_SERVICE, ref->check.health);
1771 }
1772
1773 if ((sv->agent.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED) {
1774 const char *fld_chksts;
1775
1776 fld_chksts = chunk_newstr(out);
1777 chunk_strcat(out, "* "); // for check in progress
1778 chunk_strcat(out, get_check_status_info(sv->agent.status));
1779 if (!(sv->agent.state & CHK_ST_INPROGRESS))
1780 fld_chksts += 2; // skip "* "
1781 stats[ST_F_AGENT_STATUS] = mkf_str(FN_OUTPUT, fld_chksts);
1782
1783 if (sv->agent.status >= HCHK_STATUS_L57DATA)
1784 stats[ST_F_AGENT_CODE] = mkf_u32(FN_OUTPUT, sv->agent.code);
1785
1786 if (sv->agent.status >= HCHK_STATUS_CHECKED)
1787 stats[ST_F_AGENT_DURATION] = mkf_u64(FN_DURATION, sv->agent.duration);
1788
1789 stats[ST_F_AGENT_DESC] = mkf_str(FN_OUTPUT, get_check_status_description(sv->agent.status));
1790 stats[ST_F_LAST_AGT] = mkf_str(FN_OUTPUT, sv->agent.desc);
1791 stats[ST_F_AGENT_RISE] = mkf_u32(FO_CONFIG|FS_SERVICE, sv->agent.rise);
1792 stats[ST_F_AGENT_FALL] = mkf_u32(FO_CONFIG|FS_SERVICE, sv->agent.fall);
1793 stats[ST_F_AGENT_HEALTH] = mkf_u32(FO_CONFIG|FS_SERVICE, sv->agent.health);
1794 }
1795
1796 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
1797 if (px->mode == PR_MODE_HTTP) {
Marcin Deranek3c27dda2020-05-15 18:32:51 +02001798 stats[ST_F_REQ_TOT] = mkf_u64(FN_COUNTER, sv->counters.p.http.cum_req);
William Lallemand74c24fb2016-11-21 17:18:36 +01001799 stats[ST_F_HRSP_1XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[1]);
1800 stats[ST_F_HRSP_2XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[2]);
1801 stats[ST_F_HRSP_3XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[3]);
1802 stats[ST_F_HRSP_4XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[4]);
1803 stats[ST_F_HRSP_5XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[5]);
1804 stats[ST_F_HRSP_OTHER] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[0]);
1805 }
1806
1807 if (ref->observe)
1808 stats[ST_F_HANAFAIL] = mkf_u64(FN_COUNTER, sv->counters.failed_hana);
1809
1810 stats[ST_F_CLI_ABRT] = mkf_u64(FN_COUNTER, sv->counters.cli_aborts);
1811 stats[ST_F_SRV_ABRT] = mkf_u64(FN_COUNTER, sv->counters.srv_aborts);
1812 stats[ST_F_LASTSESS] = mkf_s32(FN_AGE, srv_lastsession(sv));
1813
Marcin Deraneka8dbdf32020-05-15 20:02:40 +02001814 srv_samples_counter = (px->mode == PR_MODE_HTTP) ? sv->counters.p.http.cum_req : sv->counters.cum_lbconn;
1815 if (srv_samples_counter < TIME_STATS_SAMPLES && srv_samples_counter > 0)
1816 srv_samples_window = srv_samples_counter;
1817
1818 stats[ST_F_QTIME] = mkf_u32(FN_AVG, swrate_avg(sv->counters.q_time, srv_samples_window));
1819 stats[ST_F_CTIME] = mkf_u32(FN_AVG, swrate_avg(sv->counters.c_time, srv_samples_window));
1820 stats[ST_F_RTIME] = mkf_u32(FN_AVG, swrate_avg(sv->counters.d_time, srv_samples_window));
1821 stats[ST_F_TTIME] = mkf_u32(FN_AVG, swrate_avg(sv->counters.t_time, srv_samples_window));
William Lallemand74c24fb2016-11-21 17:18:36 +01001822
Christopher Faulet0d1c2a62019-11-08 14:59:51 +01001823 stats[ST_F_QT_MAX] = mkf_u32(FN_MAX, sv->counters.qtime_max);
1824 stats[ST_F_CT_MAX] = mkf_u32(FN_MAX, sv->counters.ctime_max);
1825 stats[ST_F_RT_MAX] = mkf_u32(FN_MAX, sv->counters.dtime_max);
1826 stats[ST_F_TT_MAX] = mkf_u32(FN_MAX, sv->counters.ttime_max);
1827
Willy Tarreau708c4162019-10-09 10:19:16 +02001828 if (flags & STAT_SHLGNDS) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001829 switch (addr_to_str(&sv->addr, str, sizeof(str))) {
1830 case AF_INET:
1831 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
Willy Tarreau04276f32017-01-06 17:41:29 +01001832 chunk_appendf(out, "%s:%d", str, sv->svc_port);
William Lallemand74c24fb2016-11-21 17:18:36 +01001833 break;
1834 case AF_INET6:
1835 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
Willy Tarreau04276f32017-01-06 17:41:29 +01001836 chunk_appendf(out, "[%s]:%d", str, sv->svc_port);
William Lallemand74c24fb2016-11-21 17:18:36 +01001837 break;
1838 case AF_UNIX:
1839 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, "unix");
1840 break;
1841 case -1:
1842 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
1843 chunk_strcat(out, strerror(errno));
1844 break;
1845 default: /* address family not supported */
1846 break;
1847 }
1848
1849 if (sv->cookie)
1850 stats[ST_F_COOKIE] = mkf_str(FO_CONFIG|FN_NAME|FS_SERVICE, sv->cookie);
1851 }
1852
1853 return 1;
1854}
1855
1856/* Dumps a line for server <sv> and proxy <px> to the trash and uses the state
Willy Tarreau43241ff2019-10-09 11:27:51 +02001857 * from stream interface <si>, and server state <state>. The caller is
1858 * responsible for clearing the trash if needed. Returns non-zero if it emits
1859 * anything, zero otherwise.
William Lallemand74c24fb2016-11-21 17:18:36 +01001860 */
Willy Tarreau43241ff2019-10-09 11:27:51 +02001861static int stats_dump_sv_stats(struct stream_interface *si, struct proxy *px, struct server *sv)
William Lallemand74c24fb2016-11-21 17:18:36 +01001862{
1863 struct appctx *appctx = __objt_appctx(si->end);
1864
Willy Tarreau43241ff2019-10-09 11:27:51 +02001865 if (!stats_fill_sv_stats(px, sv, appctx->ctx.stats.flags, stats, ST_F_TOTAL_FIELDS))
William Lallemand74c24fb2016-11-21 17:18:36 +01001866 return 0;
1867
Willy Tarreau43241ff2019-10-09 11:27:51 +02001868 return stats_dump_one_line(stats, px, appctx);
William Lallemand74c24fb2016-11-21 17:18:36 +01001869}
1870
1871/* Fill <stats> with the backend statistics. <stats> is
1872 * preallocated array of length <len>. The length of the array
1873 * must be at least ST_F_TOTAL_FIELDS. If this length is less
1874 * then this value, the function returns 0, otherwise, it
Willy Tarreau708c4162019-10-09 10:19:16 +02001875 * returns 1. <flags> can take the value STAT_SHLGNDS.
William Lallemand74c24fb2016-11-21 17:18:36 +01001876 */
1877int stats_fill_be_stats(struct proxy *px, int flags, struct field *stats, int len)
1878{
Marcin Deraneka8dbdf32020-05-15 20:02:40 +02001879 long long be_samples_counter;
1880 unsigned int be_samples_window = TIME_STATS_SAMPLES;
1881
William Lallemand74c24fb2016-11-21 17:18:36 +01001882 if (len < ST_F_TOTAL_FIELDS)
1883 return 0;
1884
1885 memset(stats, 0, sizeof(*stats) * len);
1886
1887 stats[ST_F_PXNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
1888 stats[ST_F_SVNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, "BACKEND");
1889 stats[ST_F_MODE] = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
1890 stats[ST_F_QCUR] = mkf_u32(0, px->nbpend);
1891 stats[ST_F_QMAX] = mkf_u32(FN_MAX, px->be_counters.nbpend_max);
Thierry FOURNIER0ff98a42016-12-19 16:50:42 +01001892 stats[ST_F_SCUR] = mkf_u32(0, px->beconn);
William Lallemand74c24fb2016-11-21 17:18:36 +01001893 stats[ST_F_SMAX] = mkf_u32(FN_MAX, px->be_counters.conn_max);
1894 stats[ST_F_SLIM] = mkf_u32(FO_CONFIG|FN_LIMIT, px->fullconn);
1895 stats[ST_F_STOT] = mkf_u64(FN_COUNTER, px->be_counters.cum_conn);
1896 stats[ST_F_BIN] = mkf_u64(FN_COUNTER, px->be_counters.bytes_in);
1897 stats[ST_F_BOUT] = mkf_u64(FN_COUNTER, px->be_counters.bytes_out);
1898 stats[ST_F_DREQ] = mkf_u64(FN_COUNTER, px->be_counters.denied_req);
1899 stats[ST_F_DRESP] = mkf_u64(FN_COUNTER, px->be_counters.denied_resp);
1900 stats[ST_F_ECON] = mkf_u64(FN_COUNTER, px->be_counters.failed_conns);
1901 stats[ST_F_ERESP] = mkf_u64(FN_COUNTER, px->be_counters.failed_resp);
1902 stats[ST_F_WRETR] = mkf_u64(FN_COUNTER, px->be_counters.retries);
1903 stats[ST_F_WREDIS] = mkf_u64(FN_COUNTER, px->be_counters.redispatches);
Tim Duesterhus3fd19732018-05-27 20:35:08 +02001904 stats[ST_F_WREW] = mkf_u64(FN_COUNTER, px->be_counters.failed_rewrites);
Christopher Faulet0159ee42019-12-16 14:40:39 +01001905 stats[ST_F_EINT] = mkf_u64(FN_COUNTER, px->be_counters.internal_errors);
Willy Tarreauf1573842018-12-14 11:35:36 +01001906 stats[ST_F_CONNECT] = mkf_u64(FN_COUNTER, px->be_counters.connect);
1907 stats[ST_F_REUSE] = mkf_u64(FN_COUNTER, px->be_counters.reuse);
William Lallemand74c24fb2016-11-21 17:18:36 +01001908 stats[ST_F_STATUS] = mkf_str(FO_STATUS, (px->lbprm.tot_weight > 0 || !px->srv) ? "UP" : "DOWN");
1909 stats[ST_F_WEIGHT] = mkf_u32(FN_AVG, (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv);
1910 stats[ST_F_ACT] = mkf_u32(0, px->srv_act);
1911 stats[ST_F_BCK] = mkf_u32(0, px->srv_bck);
1912 stats[ST_F_CHKDOWN] = mkf_u64(FN_COUNTER, px->down_trans);
1913 stats[ST_F_LASTCHG] = mkf_u32(FN_AGE, now.tv_sec - px->last_change);
1914 if (px->srv)
1915 stats[ST_F_DOWNTIME] = mkf_u32(FN_COUNTER, be_downtime(px));
1916
1917 stats[ST_F_PID] = mkf_u32(FO_KEY, relative_pid);
1918 stats[ST_F_IID] = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
1919 stats[ST_F_SID] = mkf_u32(FO_KEY|FS_SERVICE, 0);
1920 stats[ST_F_LBTOT] = mkf_u64(FN_COUNTER, px->be_counters.cum_lbconn);
1921 stats[ST_F_TYPE] = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_BE);
1922 stats[ST_F_RATE] = mkf_u32(0, read_freq_ctr(&px->be_sess_per_sec));
1923 stats[ST_F_RATE_MAX] = mkf_u32(0, px->be_counters.sps_max);
1924
Willy Tarreau708c4162019-10-09 10:19:16 +02001925 if (flags & STAT_SHLGNDS) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001926 if (px->cookie_name)
1927 stats[ST_F_COOKIE] = mkf_str(FO_CONFIG|FN_NAME|FS_SERVICE, px->cookie_name);
1928 stats[ST_F_ALGO] = mkf_str(FO_CONFIG|FS_SERVICE, backend_lb_algo_str(px->lbprm.algo & BE_LB_ALGO));
1929 }
1930
1931 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
1932 if (px->mode == PR_MODE_HTTP) {
1933 stats[ST_F_REQ_TOT] = mkf_u64(FN_COUNTER, px->be_counters.p.http.cum_req);
1934 stats[ST_F_HRSP_1XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[1]);
1935 stats[ST_F_HRSP_2XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[2]);
1936 stats[ST_F_HRSP_3XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[3]);
1937 stats[ST_F_HRSP_4XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[4]);
1938 stats[ST_F_HRSP_5XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[5]);
1939 stats[ST_F_HRSP_OTHER] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[0]);
Willy Tarreaua1214a52018-12-14 14:00:25 +01001940 stats[ST_F_CACHE_LOOKUPS] = mkf_u64(FN_COUNTER, px->be_counters.p.http.cache_lookups);
1941 stats[ST_F_CACHE_HITS] = mkf_u64(FN_COUNTER, px->be_counters.p.http.cache_hits);
William Lallemand74c24fb2016-11-21 17:18:36 +01001942 }
1943
1944 stats[ST_F_CLI_ABRT] = mkf_u64(FN_COUNTER, px->be_counters.cli_aborts);
1945 stats[ST_F_SRV_ABRT] = mkf_u64(FN_COUNTER, px->be_counters.srv_aborts);
1946
1947 /* compression: in, out, bypassed, responses */
1948 stats[ST_F_COMP_IN] = mkf_u64(FN_COUNTER, px->be_counters.comp_in);
1949 stats[ST_F_COMP_OUT] = mkf_u64(FN_COUNTER, px->be_counters.comp_out);
1950 stats[ST_F_COMP_BYP] = mkf_u64(FN_COUNTER, px->be_counters.comp_byp);
1951 stats[ST_F_COMP_RSP] = mkf_u64(FN_COUNTER, px->be_counters.p.http.comp_rsp);
1952 stats[ST_F_LASTSESS] = mkf_s32(FN_AGE, be_lastsession(px));
1953
Marcin Deraneka8dbdf32020-05-15 20:02:40 +02001954 be_samples_counter = (px->mode == PR_MODE_HTTP) ? px->be_counters.p.http.cum_req : px->be_counters.cum_lbconn;
1955 if (be_samples_counter < TIME_STATS_SAMPLES && be_samples_counter > 0)
1956 be_samples_window = be_samples_counter;
1957
1958 stats[ST_F_QTIME] = mkf_u32(FN_AVG, swrate_avg(px->be_counters.q_time, be_samples_window));
1959 stats[ST_F_CTIME] = mkf_u32(FN_AVG, swrate_avg(px->be_counters.c_time, be_samples_window));
1960 stats[ST_F_RTIME] = mkf_u32(FN_AVG, swrate_avg(px->be_counters.d_time, be_samples_window));
1961 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 +01001962
Christopher Faulet0d1c2a62019-11-08 14:59:51 +01001963 stats[ST_F_QT_MAX] = mkf_u32(FN_MAX, px->be_counters.qtime_max);
1964 stats[ST_F_CT_MAX] = mkf_u32(FN_MAX, px->be_counters.ctime_max);
1965 stats[ST_F_RT_MAX] = mkf_u32(FN_MAX, px->be_counters.dtime_max);
1966 stats[ST_F_TT_MAX] = mkf_u32(FN_MAX, px->be_counters.ttime_max);
1967
William Lallemand74c24fb2016-11-21 17:18:36 +01001968 return 1;
1969}
1970
1971/* Dumps a line for backend <px> to the trash for and uses the state from stream
Willy Tarreau43241ff2019-10-09 11:27:51 +02001972 * interface <si>. The caller is responsible for clearing the trash if needed.
1973 * Returns non-zero if it emits anything, zero otherwise.
William Lallemand74c24fb2016-11-21 17:18:36 +01001974 */
Willy Tarreau43241ff2019-10-09 11:27:51 +02001975static int stats_dump_be_stats(struct stream_interface *si, struct proxy *px)
William Lallemand74c24fb2016-11-21 17:18:36 +01001976{
1977 struct appctx *appctx = __objt_appctx(si->end);
1978
1979 if (!(px->cap & PR_CAP_BE))
1980 return 0;
1981
1982 if ((appctx->ctx.stats.flags & STAT_BOUND) && !(appctx->ctx.stats.type & (1 << STATS_TYPE_BE)))
1983 return 0;
1984
Willy Tarreau43241ff2019-10-09 11:27:51 +02001985 if (!stats_fill_be_stats(px, appctx->ctx.stats.flags, stats, ST_F_TOTAL_FIELDS))
William Lallemand74c24fb2016-11-21 17:18:36 +01001986 return 0;
1987
Willy Tarreau43241ff2019-10-09 11:27:51 +02001988 return stats_dump_one_line(stats, px, appctx);
William Lallemand74c24fb2016-11-21 17:18:36 +01001989}
1990
1991/* Dumps the HTML table header for proxy <px> to the trash for and uses the state from
1992 * stream interface <si> and per-uri parameters <uri>. The caller is responsible
1993 * for clearing the trash if needed.
1994 */
Willy Tarreau676c29e2019-10-09 10:50:01 +02001995static void stats_dump_html_px_hdr(struct stream_interface *si, struct proxy *px)
William Lallemand74c24fb2016-11-21 17:18:36 +01001996{
1997 struct appctx *appctx = __objt_appctx(si->end);
1998 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
1999
2000 if (px->cap & PR_CAP_BE && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
2001 /* A form to enable/disable this proxy servers */
2002
2003 /* scope_txt = search pattern + search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
2004 scope_txt[0] = 0;
2005 if (appctx->ctx.stats.scope_len) {
Christopher Fauleted7a0662019-01-14 11:07:34 +01002006 const char *scope_ptr = stats_scope_ptr(appctx, si);
2007
William Lallemand74c24fb2016-11-21 17:18:36 +01002008 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Christopher Fauleted7a0662019-01-14 11:07:34 +01002009 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), scope_ptr, appctx->ctx.stats.scope_len);
William Lallemand74c24fb2016-11-21 17:18:36 +01002010 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
2011 }
2012
2013 chunk_appendf(&trash,
2014 "<form method=\"post\">");
2015 }
2016
2017 /* print a new table */
2018 chunk_appendf(&trash,
2019 "<table class=\"tbl\" width=\"100%%\">\n"
2020 "<tr class=\"titre\">"
2021 "<th class=\"pxname\" width=\"10%%\">");
2022
2023 chunk_appendf(&trash,
2024 "<a name=\"%s\"></a>%s"
2025 "<a class=px href=\"#%s\">%s</a>",
2026 px->id,
Willy Tarreau676c29e2019-10-09 10:50:01 +02002027 (appctx->ctx.stats.flags & STAT_SHLGNDS) ? "<u>":"",
William Lallemand74c24fb2016-11-21 17:18:36 +01002028 px->id, px->id);
2029
Willy Tarreau676c29e2019-10-09 10:50:01 +02002030 if (appctx->ctx.stats.flags & STAT_SHLGNDS) {
William Lallemand74c24fb2016-11-21 17:18:36 +01002031 /* cap, mode, id */
2032 chunk_appendf(&trash, "<div class=tips>cap: %s, mode: %s, id: %d",
2033 proxy_cap_str(px->cap), proxy_mode_str(px->mode),
2034 px->uuid);
2035 chunk_appendf(&trash, "</div>");
2036 }
2037
2038 chunk_appendf(&trash,
2039 "%s</th>"
2040 "<th class=\"%s\" width=\"90%%\">%s</th>"
2041 "</tr>\n"
2042 "</table>\n"
2043 "<table class=\"tbl\" width=\"100%%\">\n"
2044 "<tr class=\"titre\">",
Willy Tarreau676c29e2019-10-09 10:50:01 +02002045 (appctx->ctx.stats.flags & STAT_SHLGNDS) ? "</u>":"",
William Lallemand74c24fb2016-11-21 17:18:36 +01002046 px->desc ? "desc" : "empty", px->desc ? px->desc : "");
2047
Christopher Fauletbc271ec2019-12-02 11:29:04 +01002048 if (appctx->ctx.stats.flags & STAT_ADMIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +01002049 /* Column heading for Enable or Disable server */
Christopher Fauletbc271ec2019-12-02 11:29:04 +01002050 if ((px->cap & PR_CAP_BE) && px->srv)
2051 chunk_appendf(&trash,
2052 "<th rowspan=2 width=1><input type=\"checkbox\" "
2053 "onclick=\"for(c in document.getElementsByClassName('%s-checkbox')) "
2054 "document.getElementsByClassName('%s-checkbox').item(c).checked = this.checked\"></th>",
2055 px->id,
2056 px->id);
2057 else
2058 chunk_appendf(&trash, "<th rowspan=2></th>");
William Lallemand74c24fb2016-11-21 17:18:36 +01002059 }
2060
2061 chunk_appendf(&trash,
2062 "<th rowspan=2></th>"
2063 "<th colspan=3>Queue</th>"
2064 "<th colspan=3>Session rate</th><th colspan=6>Sessions</th>"
2065 "<th colspan=2>Bytes</th><th colspan=2>Denied</th>"
2066 "<th colspan=3>Errors</th><th colspan=2>Warnings</th>"
2067 "<th colspan=9>Server</th>"
2068 "</tr>\n"
2069 "<tr class=\"titre\">"
2070 "<th>Cur</th><th>Max</th><th>Limit</th>"
2071 "<th>Cur</th><th>Max</th><th>Limit</th><th>Cur</th><th>Max</th>"
2072 "<th>Limit</th><th>Total</th><th>LbTot</th><th>Last</th><th>In</th><th>Out</th>"
2073 "<th>Req</th><th>Resp</th><th>Req</th><th>Conn</th>"
2074 "<th>Resp</th><th>Retr</th><th>Redis</th>"
2075 "<th>Status</th><th>LastChk</th><th>Wght</th><th>Act</th>"
2076 "<th>Bck</th><th>Chk</th><th>Dwn</th><th>Dwntme</th>"
2077 "<th>Thrtle</th>\n"
2078 "</tr>");
2079}
2080
2081/* Dumps the HTML table trailer for proxy <px> to the trash for and uses the state from
2082 * stream interface <si>. The caller is responsible for clearing the trash if needed.
2083 */
2084static void stats_dump_html_px_end(struct stream_interface *si, struct proxy *px)
2085{
2086 struct appctx *appctx = __objt_appctx(si->end);
2087 chunk_appendf(&trash, "</table>");
2088
2089 if ((px->cap & PR_CAP_BE) && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
2090 /* close the form used to enable/disable this proxy servers */
2091 chunk_appendf(&trash,
2092 "Choose the action to perform on the checked servers : "
2093 "<select name=action>"
2094 "<option value=\"\"></option>"
2095 "<option value=\"ready\">Set state to READY</option>"
2096 "<option value=\"drain\">Set state to DRAIN</option>"
2097 "<option value=\"maint\">Set state to MAINT</option>"
2098 "<option value=\"dhlth\">Health: disable checks</option>"
2099 "<option value=\"ehlth\">Health: enable checks</option>"
2100 "<option value=\"hrunn\">Health: force UP</option>"
2101 "<option value=\"hnolb\">Health: force NOLB</option>"
2102 "<option value=\"hdown\">Health: force DOWN</option>"
2103 "<option value=\"dagent\">Agent: disable checks</option>"
2104 "<option value=\"eagent\">Agent: enable checks</option>"
2105 "<option value=\"arunn\">Agent: force UP</option>"
2106 "<option value=\"adown\">Agent: force DOWN</option>"
2107 "<option value=\"shutdown\">Kill Sessions</option>"
2108 "</select>"
2109 "<input type=\"hidden\" name=\"b\" value=\"#%d\">"
2110 "&nbsp;<input type=\"submit\" value=\"Apply\">"
2111 "</form>",
2112 px->uuid);
2113 }
2114
2115 chunk_appendf(&trash, "<p>\n");
2116}
2117
2118/*
2119 * Dumps statistics for a proxy. The output is sent to the stream interface's
2120 * input buffer. Returns 0 if it had to stop dumping data because of lack of
2121 * buffer space, or non-zero if everything completed. This function is used
2122 * both by the CLI and the HTTP entry points, and is able to dump the output
2123 * in HTML or CSV formats. If the later, <uri> must be NULL.
2124 */
Christopher Fauletef779222018-10-31 08:47:01 +01002125int stats_dump_proxy_to_buffer(struct stream_interface *si, struct htx *htx,
2126 struct proxy *px, struct uri_auth *uri)
William Lallemand74c24fb2016-11-21 17:18:36 +01002127{
2128 struct appctx *appctx = __objt_appctx(si->end);
2129 struct stream *s = si_strm(si);
2130 struct channel *rep = si_ic(si);
2131 struct server *sv, *svs; /* server and server-state, server-state=server or server->track */
2132 struct listener *l;
William Lallemand74c24fb2016-11-21 17:18:36 +01002133
2134 chunk_reset(&trash);
2135
2136 switch (appctx->ctx.stats.px_st) {
2137 case STAT_PX_ST_INIT:
2138 /* we are on a new proxy */
2139 if (uri && uri->scope) {
2140 /* we have a limited scope, we have to check the proxy name */
2141 struct stat_scope *scope;
2142 int len;
2143
2144 len = strlen(px->id);
2145 scope = uri->scope;
2146
2147 while (scope) {
2148 /* match exact proxy name */
2149 if (scope->px_len == len && !memcmp(px->id, scope->px_id, len))
2150 break;
2151
2152 /* match '.' which means 'self' proxy */
2153 if (!strcmp(scope->px_id, ".") && px == s->be)
2154 break;
2155 scope = scope->next;
2156 }
2157
2158 /* proxy name not found : don't dump anything */
2159 if (scope == NULL)
2160 return 1;
2161 }
2162
2163 /* if the user has requested a limited output and the proxy
2164 * name does not match, skip it.
2165 */
Christopher Fauleted7a0662019-01-14 11:07:34 +01002166 if (appctx->ctx.stats.scope_len) {
2167 const char *scope_ptr = stats_scope_ptr(appctx, si);
2168
2169 if (strnistr(px->id, strlen(px->id), scope_ptr, appctx->ctx.stats.scope_len) == NULL)
2170 return 1;
2171 }
William Lallemand74c24fb2016-11-21 17:18:36 +01002172
2173 if ((appctx->ctx.stats.flags & STAT_BOUND) &&
2174 (appctx->ctx.stats.iid != -1) &&
2175 (px->uuid != appctx->ctx.stats.iid))
2176 return 1;
2177
2178 appctx->ctx.stats.px_st = STAT_PX_ST_TH;
2179 /* fall through */
2180
2181 case STAT_PX_ST_TH:
2182 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreau676c29e2019-10-09 10:50:01 +02002183 stats_dump_html_px_hdr(si, px);
Christopher Fauletef779222018-10-31 08:47:01 +01002184 if (!stats_putchk(rep, htx, &trash))
2185 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002186 }
2187
2188 appctx->ctx.stats.px_st = STAT_PX_ST_FE;
2189 /* fall through */
2190
2191 case STAT_PX_ST_FE:
2192 /* print the frontend */
2193 if (stats_dump_fe_stats(si, px)) {
Christopher Fauletef779222018-10-31 08:47:01 +01002194 if (!stats_putchk(rep, htx, &trash))
2195 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002196 }
2197
2198 appctx->ctx.stats.l = px->conf.listeners.n;
2199 appctx->ctx.stats.px_st = STAT_PX_ST_LI;
2200 /* fall through */
2201
2202 case STAT_PX_ST_LI:
2203 /* stats.l has been initialized above */
2204 for (; appctx->ctx.stats.l != &px->conf.listeners; appctx->ctx.stats.l = l->by_fe.n) {
Christopher Fauletef779222018-10-31 08:47:01 +01002205 if (htx) {
2206 if (htx_almost_full(htx))
2207 goto full;
2208 }
2209 else {
2210 if (buffer_almost_full(&rep->buf))
2211 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002212 }
2213
2214 l = LIST_ELEM(appctx->ctx.stats.l, struct listener *, by_fe);
2215 if (!l->counters)
2216 continue;
2217
2218 if (appctx->ctx.stats.flags & STAT_BOUND) {
2219 if (!(appctx->ctx.stats.type & (1 << STATS_TYPE_SO)))
2220 break;
2221
2222 if (appctx->ctx.stats.sid != -1 && l->luid != appctx->ctx.stats.sid)
2223 continue;
2224 }
2225
2226 /* print the frontend */
Willy Tarreau43241ff2019-10-09 11:27:51 +02002227 if (stats_dump_li_stats(si, px, l)) {
Christopher Fauletef779222018-10-31 08:47:01 +01002228 if (!stats_putchk(rep, htx, &trash))
2229 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002230 }
2231 }
2232
2233 appctx->ctx.stats.sv = px->srv; /* may be NULL */
2234 appctx->ctx.stats.px_st = STAT_PX_ST_SV;
2235 /* fall through */
2236
2237 case STAT_PX_ST_SV:
2238 /* stats.sv has been initialized above */
2239 for (; appctx->ctx.stats.sv != NULL; appctx->ctx.stats.sv = sv->next) {
Christopher Fauletef779222018-10-31 08:47:01 +01002240 if (htx) {
2241 if (htx_almost_full(htx))
2242 goto full;
2243 }
2244 else {
2245 if (buffer_almost_full(&rep->buf))
2246 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002247 }
2248
2249 sv = appctx->ctx.stats.sv;
2250
2251 if (appctx->ctx.stats.flags & STAT_BOUND) {
2252 if (!(appctx->ctx.stats.type & (1 << STATS_TYPE_SV)))
2253 break;
2254
2255 if (appctx->ctx.stats.sid != -1 && sv->puid != appctx->ctx.stats.sid)
2256 continue;
2257 }
2258
2259 svs = sv;
2260 while (svs->track)
2261 svs = svs->track;
2262
2263 /* do not report servers which are DOWN and not changing state */
2264 if ((appctx->ctx.stats.flags & STAT_HIDE_DOWN) &&
Emeric Brun52a91d32017-08-31 14:41:55 +02002265 ((sv->cur_admin & SRV_ADMF_MAINT) || /* server is in maintenance */
2266 (sv->cur_state == SRV_ST_STOPPED && /* server is down */
William Lallemand74c24fb2016-11-21 17:18:36 +01002267 (!((svs->agent.state | svs->check.state) & CHK_ST_ENABLED) ||
2268 ((svs->agent.state & CHK_ST_ENABLED) && !svs->agent.health) ||
2269 ((svs->check.state & CHK_ST_ENABLED) && !svs->check.health))))) {
2270 continue;
2271 }
2272
Willy Tarreau43241ff2019-10-09 11:27:51 +02002273 if (stats_dump_sv_stats(si, px, sv)) {
Christopher Fauletef779222018-10-31 08:47:01 +01002274 if (!stats_putchk(rep, htx, &trash))
2275 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002276 }
2277 } /* for sv */
2278
2279 appctx->ctx.stats.px_st = STAT_PX_ST_BE;
2280 /* fall through */
2281
2282 case STAT_PX_ST_BE:
2283 /* print the backend */
Willy Tarreau43241ff2019-10-09 11:27:51 +02002284 if (stats_dump_be_stats(si, px)) {
Christopher Fauletef779222018-10-31 08:47:01 +01002285 if (!stats_putchk(rep, htx, &trash))
2286 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002287 }
2288
2289 appctx->ctx.stats.px_st = STAT_PX_ST_END;
2290 /* fall through */
2291
2292 case STAT_PX_ST_END:
2293 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
2294 stats_dump_html_px_end(si, px);
Christopher Fauletef779222018-10-31 08:47:01 +01002295 if (!stats_putchk(rep, htx, &trash))
2296 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002297 }
2298
2299 appctx->ctx.stats.px_st = STAT_PX_ST_FIN;
2300 /* fall through */
2301
2302 case STAT_PX_ST_FIN:
2303 return 1;
2304
2305 default:
2306 /* unknown state, we should put an abort() here ! */
2307 return 1;
2308 }
Christopher Fauletef779222018-10-31 08:47:01 +01002309
2310 full:
2311 si_rx_room_blk(si);
2312 return 0;
William Lallemand74c24fb2016-11-21 17:18:36 +01002313}
2314
2315/* Dumps the HTTP stats head block to the trash for and uses the per-uri
2316 * parameters <uri>. The caller is responsible for clearing the trash if needed.
2317 */
Willy Tarreau676c29e2019-10-09 10:50:01 +02002318static void stats_dump_html_head(struct appctx *appctx, struct uri_auth *uri)
William Lallemand74c24fb2016-11-21 17:18:36 +01002319{
2320 /* WARNING! This must fit in the first buffer !!! */
2321 chunk_appendf(&trash,
2322 "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"\n"
2323 "\"http://www.w3.org/TR/html4/loose.dtd\">\n"
2324 "<html><head><title>Statistics Report for " PRODUCT_NAME "%s%s</title>\n"
2325 "<meta http-equiv=\"content-type\" content=\"text/html; charset=iso-8859-1\">\n"
2326 "<style type=\"text/css\"><!--\n"
2327 "body {"
2328 " font-family: arial, helvetica, sans-serif;"
2329 " font-size: 12px;"
2330 " font-weight: normal;"
2331 " color: black;"
2332 " background: white;"
2333 "}\n"
2334 "th,td {"
2335 " font-size: 10px;"
2336 "}\n"
2337 "h1 {"
2338 " font-size: x-large;"
2339 " margin-bottom: 0.5em;"
2340 "}\n"
2341 "h2 {"
2342 " font-family: helvetica, arial;"
2343 " font-size: x-large;"
2344 " font-weight: bold;"
2345 " font-style: italic;"
2346 " color: #6020a0;"
2347 " margin-top: 0em;"
2348 " margin-bottom: 0em;"
2349 "}\n"
2350 "h3 {"
2351 " font-family: helvetica, arial;"
2352 " font-size: 16px;"
2353 " font-weight: bold;"
2354 " color: #b00040;"
2355 " background: #e8e8d0;"
2356 " margin-top: 0em;"
2357 " margin-bottom: 0em;"
2358 "}\n"
2359 "li {"
2360 " margin-top: 0.25em;"
2361 " margin-right: 2em;"
2362 "}\n"
2363 ".hr {margin-top: 0.25em;"
2364 " border-color: black;"
2365 " border-bottom-style: solid;"
2366 "}\n"
2367 ".titre {background: #20D0D0;color: #000000; font-weight: bold; text-align: center;}\n"
2368 ".total {background: #20D0D0;color: #ffff80;}\n"
2369 ".frontend {background: #e8e8d0;}\n"
2370 ".socket {background: #d0d0d0;}\n"
2371 ".backend {background: #e8e8d0;}\n"
2372 ".active_down {background: #ff9090;}\n"
2373 ".active_going_up {background: #ffd020;}\n"
2374 ".active_going_down {background: #ffffa0;}\n"
2375 ".active_up {background: #c0ffc0;}\n"
2376 ".active_nolb {background: #20a0ff;}\n"
2377 ".active_draining {background: #20a0FF;}\n"
2378 ".active_no_check {background: #e0e0e0;}\n"
2379 ".backup_down {background: #ff9090;}\n"
2380 ".backup_going_up {background: #ff80ff;}\n"
2381 ".backup_going_down {background: #c060ff;}\n"
2382 ".backup_up {background: #b0d0ff;}\n"
2383 ".backup_nolb {background: #90b0e0;}\n"
2384 ".backup_draining {background: #cc9900;}\n"
2385 ".backup_no_check {background: #e0e0e0;}\n"
2386 ".maintain {background: #c07820;}\n"
2387 ".rls {letter-spacing: 0.2em; margin-right: 1px;}\n" /* right letter spacing (used for grouping digits) */
2388 "\n"
2389 "a.px:link {color: #ffff40; text-decoration: none;}"
2390 "a.px:visited {color: #ffff40; text-decoration: none;}"
2391 "a.px:hover {color: #ffffff; text-decoration: none;}"
2392 "a.lfsb:link {color: #000000; text-decoration: none;}"
2393 "a.lfsb:visited {color: #000000; text-decoration: none;}"
2394 "a.lfsb:hover {color: #505050; text-decoration: none;}"
2395 "\n"
2396 "table.tbl { border-collapse: collapse; border-style: none;}\n"
2397 "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"
2398 "table.tbl td.ac { text-align: center;}\n"
2399 "table.tbl th { border-width: 1px; border-style: solid solid solid solid; border-color: gray;}\n"
2400 "table.tbl th.pxname { background: #b00040; color: #ffff40; font-weight: bold; border-style: solid solid none solid; padding: 2px 3px; white-space: nowrap;}\n"
2401 "table.tbl th.empty { border-style: none; empty-cells: hide; background: white;}\n"
2402 "table.tbl th.desc { background: white; border-style: solid solid none solid; text-align: left; padding: 2px 3px;}\n"
2403 "\n"
2404 "table.lgd { border-collapse: collapse; border-width: 1px; border-style: none none none solid; border-color: black;}\n"
2405 "table.lgd td { border-width: 1px; border-style: solid solid solid solid; border-color: gray; padding: 2px;}\n"
2406 "table.lgd td.noborder { border-style: none; padding: 2px; white-space: nowrap;}\n"
2407 "table.det { border-collapse: collapse; border-style: none; }\n"
2408 "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"
2409 "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"
2410 "u {text-decoration:none; border-bottom: 1px dotted black;}\n"
2411 "div.tips {\n"
2412 " display:block;\n"
2413 " visibility:hidden;\n"
2414 " z-index:2147483647;\n"
2415 " position:absolute;\n"
2416 " padding:2px 4px 3px;\n"
2417 " background:#f0f060; color:#000000;\n"
2418 " border:1px solid #7040c0;\n"
2419 " white-space:nowrap;\n"
2420 " font-style:normal;font-size:11px;font-weight:normal;\n"
2421 " -moz-border-radius:3px;-webkit-border-radius:3px;border-radius:3px;\n"
2422 " -moz-box-shadow:gray 2px 2px 3px;-webkit-box-shadow:gray 2px 2px 3px;box-shadow:gray 2px 2px 3px;\n"
2423 "}\n"
2424 "u:hover div.tips {visibility:visible;}\n"
2425 "-->\n"
2426 "</style></head>\n",
Willy Tarreau676c29e2019-10-09 10:50:01 +02002427 (appctx->ctx.stats.flags & STAT_SHNODE) ? " on " : "",
Willy Tarreau027d2062020-01-23 11:47:13 +01002428 (appctx->ctx.stats.flags & STAT_SHNODE) ? (uri && uri->node ? uri->node : global.node) : ""
William Lallemand74c24fb2016-11-21 17:18:36 +01002429 );
2430}
2431
2432/* Dumps the HTML stats information block to the trash for and uses the state from
2433 * stream interface <si> and per-uri parameters <uri>. The caller is responsible
2434 * for clearing the trash if needed.
2435 */
2436static void stats_dump_html_info(struct stream_interface *si, struct uri_auth *uri)
2437{
2438 struct appctx *appctx = __objt_appctx(si->end);
2439 unsigned int up = (now.tv_sec - start_date.tv_sec);
2440 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
Christopher Fauleted7a0662019-01-14 11:07:34 +01002441 const char *scope_ptr = stats_scope_ptr(appctx, si);
Willy Tarreau1713c032019-05-23 12:23:55 +02002442 unsigned long long bps = (unsigned long long)read_freq_ctr(&global.out_32bps) * 32;
2443
2444 /* Turn the bytes per second to bits per second and take care of the
2445 * usual ethernet overhead in order to help figure how far we are from
2446 * interface saturation since it's the only case which usually matters.
2447 * For this we count the total size of an Ethernet frame on the wire
2448 * including preamble and IFG (1538) for the largest TCP segment it
2449 * transports (1448 with TCP timestamps). This is not valid for smaller
2450 * packets (under-estimated), but it gives a reasonably accurate
2451 * estimation of how far we are from uplink saturation.
2452 */
2453 bps = bps * 8 * 1538 / 1448;
William Lallemand74c24fb2016-11-21 17:18:36 +01002454
2455 /* WARNING! this has to fit the first packet too.
2456 * We are around 3.5 kB, add adding entries will
2457 * become tricky if we want to support 4kB buffers !
2458 */
2459 chunk_appendf(&trash,
2460 "<body><h1><a href=\"" PRODUCT_URL "\" style=\"text-decoration: none;\">"
2461 PRODUCT_NAME "%s</a></h1>\n"
2462 "<h2>Statistics Report for pid %d%s%s%s%s</h2>\n"
2463 "<hr width=\"100%%\" class=\"hr\">\n"
2464 "<h3>&gt; General process information</h3>\n"
2465 "<table border=0><tr><td align=\"left\" nowrap width=\"1%%\">\n"
Yves Lafon95317282018-02-26 11:10:37 +01002466 "<p><b>pid = </b> %d (process #%d, nbproc = %d, nbthread = %d)<br>\n"
William Lallemand74c24fb2016-11-21 17:18:36 +01002467 "<b>uptime = </b> %dd %dh%02dm%02ds<br>\n"
2468 "<b>system limits:</b> memmax = %s%s; ulimit-n = %d<br>\n"
2469 "<b>maxsock = </b> %d; <b>maxconn = </b> %d; <b>maxpipes = </b> %d<br>\n"
Willy Tarreau1713c032019-05-23 12:23:55 +02002470 "current conns = %d; current pipes = %d/%d; conn rate = %d/sec; bit rate = %.3f %cbps<br>\n"
William Lallemand74c24fb2016-11-21 17:18:36 +01002471 "Running tasks: %d/%d; idle = %d %%<br>\n"
2472 "</td><td align=\"center\" nowrap>\n"
2473 "<table class=\"lgd\"><tr>\n"
2474 "<td class=\"active_up\">&nbsp;</td><td class=\"noborder\">active UP </td>"
2475 "<td class=\"backup_up\">&nbsp;</td><td class=\"noborder\">backup UP </td>"
2476 "</tr><tr>\n"
2477 "<td class=\"active_going_down\"></td><td class=\"noborder\">active UP, going down </td>"
2478 "<td class=\"backup_going_down\"></td><td class=\"noborder\">backup UP, going down </td>"
2479 "</tr><tr>\n"
2480 "<td class=\"active_going_up\"></td><td class=\"noborder\">active DOWN, going up </td>"
2481 "<td class=\"backup_going_up\"></td><td class=\"noborder\">backup DOWN, going up </td>"
2482 "</tr><tr>\n"
2483 "<td class=\"active_down\"></td><td class=\"noborder\">active or backup DOWN &nbsp;</td>"
2484 "<td class=\"active_no_check\"></td><td class=\"noborder\">not checked </td>"
2485 "</tr><tr>\n"
2486 "<td class=\"maintain\"></td><td class=\"noborder\" colspan=\"3\">active or backup DOWN for maintenance (MAINT) &nbsp;</td>"
2487 "</tr><tr>\n"
2488 "<td class=\"active_draining\"></td><td class=\"noborder\" colspan=\"3\">active or backup SOFT STOPPED for maintenance &nbsp;</td>"
2489 "</tr></table>\n"
2490 "Note: \"NOLB\"/\"DRAIN\" = UP with load-balancing disabled."
2491 "</td>"
2492 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
2493 "<b>Display option:</b><ul style=\"margin-top: 0.25em;\">"
2494 "",
Willy Tarreau676c29e2019-10-09 10:50:01 +02002495 (appctx->ctx.stats.flags & STAT_HIDEVER) ? "" : (stats_version_string),
2496 pid, (appctx->ctx.stats.flags & STAT_SHNODE) ? " on " : "",
2497 (appctx->ctx.stats.flags & STAT_SHNODE) ? (uri->node ? uri->node : global.node) : "",
2498 (appctx->ctx.stats.flags & STAT_SHDESC) ? ": " : "",
2499 (appctx->ctx.stats.flags & STAT_SHDESC) ? (uri->desc ? uri->desc : global.desc) : "",
Yves Lafon95317282018-02-26 11:10:37 +01002500 pid, relative_pid, global.nbproc, global.nbthread,
William Lallemand74c24fb2016-11-21 17:18:36 +01002501 up / 86400, (up % 86400) / 3600,
2502 (up % 3600) / 60, (up % 60),
2503 global.rlimit_memmax ? ultoa(global.rlimit_memmax) : "unlimited",
2504 global.rlimit_memmax ? " MB" : "",
2505 global.rlimit_nofile,
2506 global.maxsock, global.maxconn, global.maxpipes,
2507 actconn, pipes_used, pipes_used+pipes_free, read_freq_ctr(&global.conn_per_sec),
Willy Tarreau1713c032019-05-23 12:23:55 +02002508 bps >= 1000000000UL ? (bps / 1000000000.0) : bps >= 1000000UL ? (bps / 1000000.0) : (bps / 1000.0),
2509 bps >= 1000000000UL ? 'G' : bps >= 1000000UL ? 'M' : 'k',
Willy Tarreau81036f22019-05-20 19:24:50 +02002510 tasks_run_queue_cur, nb_tasks_cur, ti->idle_pct
William Lallemand74c24fb2016-11-21 17:18:36 +01002511 );
2512
2513 /* scope_txt = search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
Christopher Fauleted7a0662019-01-14 11:07:34 +01002514 memcpy(scope_txt, scope_ptr, appctx->ctx.stats.scope_len);
William Lallemand74c24fb2016-11-21 17:18:36 +01002515 scope_txt[appctx->ctx.stats.scope_len] = '\0';
2516
2517 chunk_appendf(&trash,
2518 "<li><form method=\"GET\">Scope : <input value=\"%s\" name=\"" STAT_SCOPE_INPUT_NAME "\" size=\"8\" maxlength=\"%d\" tabindex=\"1\"/></form>\n",
2519 (appctx->ctx.stats.scope_len > 0) ? scope_txt : "",
2520 STAT_SCOPE_TXT_MAXLEN);
2521
2522 /* scope_txt = search pattern + search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
2523 scope_txt[0] = 0;
2524 if (appctx->ctx.stats.scope_len) {
2525 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Christopher Fauleted7a0662019-01-14 11:07:34 +01002526 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), scope_ptr, appctx->ctx.stats.scope_len);
William Lallemand74c24fb2016-11-21 17:18:36 +01002527 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
2528 }
2529
2530 if (appctx->ctx.stats.flags & STAT_HIDE_DOWN)
2531 chunk_appendf(&trash,
2532 "<li><a href=\"%s%s%s%s\">Show all servers</a><br>\n",
2533 uri->uri_prefix,
2534 "",
2535 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2536 scope_txt);
2537 else
2538 chunk_appendf(&trash,
2539 "<li><a href=\"%s%s%s%s\">Hide 'DOWN' servers</a><br>\n",
2540 uri->uri_prefix,
2541 ";up",
2542 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2543 scope_txt);
2544
2545 if (uri->refresh > 0) {
2546 if (appctx->ctx.stats.flags & STAT_NO_REFRESH)
2547 chunk_appendf(&trash,
2548 "<li><a href=\"%s%s%s%s\">Enable refresh</a><br>\n",
2549 uri->uri_prefix,
2550 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2551 "",
2552 scope_txt);
2553 else
2554 chunk_appendf(&trash,
2555 "<li><a href=\"%s%s%s%s\">Disable refresh</a><br>\n",
2556 uri->uri_prefix,
2557 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2558 ";norefresh",
2559 scope_txt);
2560 }
2561
2562 chunk_appendf(&trash,
2563 "<li><a href=\"%s%s%s%s\">Refresh now</a><br>\n",
2564 uri->uri_prefix,
2565 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2566 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2567 scope_txt);
2568
2569 chunk_appendf(&trash,
2570 "<li><a href=\"%s;csv%s%s\">CSV export</a><br>\n",
2571 uri->uri_prefix,
2572 (uri->refresh > 0) ? ";norefresh" : "",
2573 scope_txt);
2574
2575 chunk_appendf(&trash,
Christopher Faulet6338a082019-09-09 15:50:54 +02002576 "<li><a href=\"%s;json%s%s\">JSON export</a> (<a href=\"%s;json-schema\">schema</a>)<br>\n",
2577 uri->uri_prefix,
2578 (uri->refresh > 0) ? ";norefresh" : "",
2579 scope_txt, uri->uri_prefix);
2580
2581 chunk_appendf(&trash,
William Lallemand74c24fb2016-11-21 17:18:36 +01002582 "</ul></td>"
2583 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
2584 "<b>External resources:</b><ul style=\"margin-top: 0.25em;\">\n"
2585 "<li><a href=\"" PRODUCT_URL "\">Primary site</a><br>\n"
2586 "<li><a href=\"" PRODUCT_URL_UPD "\">Updates (v" PRODUCT_BRANCH ")</a><br>\n"
2587 "<li><a href=\"" PRODUCT_URL_DOC "\">Online manual</a><br>\n"
2588 "</ul>"
2589 "</td>"
2590 "</tr></table>\n"
2591 ""
2592 );
2593
2594 if (appctx->ctx.stats.st_code) {
2595 switch (appctx->ctx.stats.st_code) {
2596 case STAT_STATUS_DONE:
2597 chunk_appendf(&trash,
2598 "<p><div class=active_up>"
2599 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2600 "Action processed successfully."
2601 "</div>\n", uri->uri_prefix,
2602 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2603 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2604 scope_txt);
2605 break;
2606 case STAT_STATUS_NONE:
2607 chunk_appendf(&trash,
2608 "<p><div class=active_going_down>"
2609 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2610 "Nothing has changed."
2611 "</div>\n", uri->uri_prefix,
2612 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2613 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2614 scope_txt);
2615 break;
2616 case STAT_STATUS_PART:
2617 chunk_appendf(&trash,
2618 "<p><div class=active_going_down>"
2619 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2620 "Action partially processed.<br>"
2621 "Some server names are probably unknown or ambiguous (duplicated names in the backend)."
2622 "</div>\n", uri->uri_prefix,
2623 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2624 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2625 scope_txt);
2626 break;
2627 case STAT_STATUS_ERRP:
2628 chunk_appendf(&trash,
2629 "<p><div class=active_down>"
2630 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2631 "Action not processed because of invalid parameters."
2632 "<ul>"
2633 "<li>The action is maybe unknown.</li>"
Christopher Faulet2f9a41d2019-02-27 15:30:57 +01002634 "<li>Invalid key parameter (empty or too long).</li>"
William Lallemand74c24fb2016-11-21 17:18:36 +01002635 "<li>The backend name is probably unknown or ambiguous (duplicated names).</li>"
2636 "<li>Some server names are probably unknown or ambiguous (duplicated names in the backend).</li>"
2637 "</ul>"
2638 "</div>\n", uri->uri_prefix,
2639 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2640 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2641 scope_txt);
2642 break;
2643 case STAT_STATUS_EXCD:
2644 chunk_appendf(&trash,
2645 "<p><div class=active_down>"
2646 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2647 "<b>Action not processed : the buffer couldn't store all the data.<br>"
2648 "You should retry with less servers at a time.</b>"
2649 "</div>\n", uri->uri_prefix,
2650 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2651 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2652 scope_txt);
2653 break;
2654 case STAT_STATUS_DENY:
2655 chunk_appendf(&trash,
2656 "<p><div class=active_down>"
2657 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2658 "<b>Action denied.</b>"
2659 "</div>\n", uri->uri_prefix,
2660 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2661 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2662 scope_txt);
2663 break;
Christopher Faulet3c2ecf72019-02-27 16:41:27 +01002664 case STAT_STATUS_IVAL:
2665 chunk_appendf(&trash,
2666 "<p><div class=active_down>"
2667 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2668 "<b>Invalid requests (unsupported method or chunked encoded request).</b>"
2669 "</div>\n", uri->uri_prefix,
2670 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2671 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2672 scope_txt);
2673 break;
William Lallemand74c24fb2016-11-21 17:18:36 +01002674 default:
2675 chunk_appendf(&trash,
2676 "<p><div class=active_no_check>"
2677 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2678 "Unexpected result."
2679 "</div>\n", uri->uri_prefix,
2680 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2681 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2682 scope_txt);
2683 }
2684 chunk_appendf(&trash, "<p>\n");
2685 }
2686}
2687
2688/* Dumps the HTML stats trailer block to the trash. The caller is responsible
2689 * for clearing the trash if needed.
2690 */
2691static void stats_dump_html_end()
2692{
2693 chunk_appendf(&trash, "</body></html>\n");
2694}
2695
Simon Horman05ee2132017-01-04 09:37:25 +01002696/* Dumps the stats JSON header to the trash buffer which. The caller is responsible
2697 * for clearing it if needed.
2698 */
2699static void stats_dump_json_header()
2700{
2701 chunk_strcat(&trash, "[");
2702}
2703
2704
2705/* Dumps the JSON stats trailer block to the trash. The caller is responsible
2706 * for clearing the trash if needed.
2707 */
2708static void stats_dump_json_end()
2709{
2710 chunk_strcat(&trash, "]");
2711}
2712
William Lallemand74c24fb2016-11-21 17:18:36 +01002713/* This function dumps statistics onto the stream interface's read buffer in
2714 * either CSV or HTML format. <uri> contains some HTML-specific parameters that
2715 * are ignored for CSV format (hence <uri> may be NULL there). It returns 0 if
2716 * it had to stop writing data and an I/O is needed, 1 if the dump is finished
2717 * and the stream must be closed, or -1 in case of any error. This function is
2718 * used by both the CLI and the HTTP handlers.
2719 */
Christopher Fauletef779222018-10-31 08:47:01 +01002720static int stats_dump_stat_to_buffer(struct stream_interface *si, struct htx *htx,
2721 struct uri_auth *uri)
William Lallemand74c24fb2016-11-21 17:18:36 +01002722{
2723 struct appctx *appctx = __objt_appctx(si->end);
2724 struct channel *rep = si_ic(si);
2725 struct proxy *px;
2726
2727 chunk_reset(&trash);
2728
2729 switch (appctx->st2) {
2730 case STAT_ST_INIT:
2731 appctx->st2 = STAT_ST_HEAD; /* let's start producing data */
2732 /* fall through */
2733
2734 case STAT_ST_HEAD:
2735 if (appctx->ctx.stats.flags & STAT_FMT_HTML)
Willy Tarreau676c29e2019-10-09 10:50:01 +02002736 stats_dump_html_head(appctx, uri);
Christopher Faulet6338a082019-09-09 15:50:54 +02002737 else if (appctx->ctx.stats.flags & STAT_JSON_SCHM)
2738 stats_dump_json_schema(&trash);
Simon Horman05ee2132017-01-04 09:37:25 +01002739 else if (appctx->ctx.stats.flags & STAT_FMT_JSON)
Willy Tarreau9d7fb632017-04-11 07:53:04 +02002740 stats_dump_json_header();
William Lallemand74c24fb2016-11-21 17:18:36 +01002741 else if (!(appctx->ctx.stats.flags & STAT_FMT_TYPED))
2742 stats_dump_csv_header();
2743
Christopher Fauletef779222018-10-31 08:47:01 +01002744 if (!stats_putchk(rep, htx, &trash))
2745 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002746
Christopher Faulet6338a082019-09-09 15:50:54 +02002747 if (appctx->ctx.stats.flags & STAT_JSON_SCHM) {
2748 appctx->st2 = STAT_ST_FIN;
2749 return 1;
2750 }
William Lallemand74c24fb2016-11-21 17:18:36 +01002751 appctx->st2 = STAT_ST_INFO;
2752 /* fall through */
2753
2754 case STAT_ST_INFO:
2755 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
2756 stats_dump_html_info(si, uri);
Christopher Fauletef779222018-10-31 08:47:01 +01002757 if (!stats_putchk(rep, htx, &trash))
2758 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002759 }
2760
Olivier Houchardfbc74e82017-11-24 16:54:05 +01002761 appctx->ctx.stats.px = proxies_list;
William Lallemand74c24fb2016-11-21 17:18:36 +01002762 appctx->ctx.stats.px_st = STAT_PX_ST_INIT;
2763 appctx->st2 = STAT_ST_LIST;
2764 /* fall through */
2765
2766 case STAT_ST_LIST:
2767 /* dump proxies */
2768 while (appctx->ctx.stats.px) {
Christopher Fauletef779222018-10-31 08:47:01 +01002769 if (htx) {
2770 if (htx_almost_full(htx))
2771 goto full;
2772 }
2773 else {
2774 if (buffer_almost_full(&rep->buf))
2775 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002776 }
2777
2778 px = appctx->ctx.stats.px;
2779 /* skip the disabled proxies, global frontend and non-networked ones */
2780 if (px->state != PR_STSTOPPED && px->uuid > 0 && (px->cap & (PR_CAP_FE | PR_CAP_BE)))
Christopher Fauletef779222018-10-31 08:47:01 +01002781 if (stats_dump_proxy_to_buffer(si, htx, px, uri) == 0)
William Lallemand74c24fb2016-11-21 17:18:36 +01002782 return 0;
2783
2784 appctx->ctx.stats.px = px->next;
2785 appctx->ctx.stats.px_st = STAT_PX_ST_INIT;
2786 }
2787 /* here, we just have reached the last proxy */
2788
2789 appctx->st2 = STAT_ST_END;
2790 /* fall through */
2791
2792 case STAT_ST_END:
Simon Horman05ee2132017-01-04 09:37:25 +01002793 if (appctx->ctx.stats.flags & (STAT_FMT_HTML|STAT_FMT_JSON)) {
2794 if (appctx->ctx.stats.flags & STAT_FMT_HTML)
2795 stats_dump_html_end();
2796 else
2797 stats_dump_json_end();
Christopher Fauletef779222018-10-31 08:47:01 +01002798 if (!stats_putchk(rep, htx, &trash))
2799 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002800 }
2801
2802 appctx->st2 = STAT_ST_FIN;
2803 /* fall through */
2804
2805 case STAT_ST_FIN:
2806 return 1;
2807
2808 default:
2809 /* unknown state ! */
2810 appctx->st2 = STAT_ST_FIN;
2811 return -1;
2812 }
Christopher Fauletef779222018-10-31 08:47:01 +01002813
2814 full:
2815 si_rx_room_blk(si);
2816 return 0;
2817
William Lallemand74c24fb2016-11-21 17:18:36 +01002818}
2819
2820/* We reached the stats page through a POST request. The appctx is
2821 * expected to have already been allocated by the caller.
2822 * Parse the posted data and enable/disable servers if necessary.
2823 * Returns 1 if request was parsed or zero if it needs more data.
2824 */
2825static int stats_process_http_post(struct stream_interface *si)
2826{
2827 struct stream *s = si_strm(si);
2828 struct appctx *appctx = objt_appctx(si->end);
2829
2830 struct proxy *px = NULL;
2831 struct server *sv = NULL;
2832
2833 char key[LINESIZE];
2834 int action = ST_ADM_ACTION_NONE;
2835 int reprocess = 0;
2836
2837 int total_servers = 0;
2838 int altered_servers = 0;
2839
2840 char *first_param, *cur_param, *next_param, *end_params;
2841 char *st_cur_param = NULL;
2842 char *st_next_param = NULL;
2843
Christopher Fauleta3618372018-12-13 21:59:56 +01002844 struct buffer *temp = get_trash_chunk();
William Lallemand74c24fb2016-11-21 17:18:36 +01002845
Christopher Fauletb7f88902019-07-15 21:56:43 +02002846 struct htx *htx = htxbuf(&s->req.buf);
2847 struct htx_blk *blk;
Christopher Fauleta3618372018-12-13 21:59:56 +01002848
Christopher Fauletb7f88902019-07-15 21:56:43 +02002849 /* we need more data */
2850 if (s->txn->req.msg_state < HTTP_MSG_DONE) {
2851 /* check if we can receive more */
2852 if (htx_free_data_space(htx) <= global.tune.maxrewrite) {
2853 appctx->ctx.stats.st_code = STAT_STATUS_EXCD;
2854 goto out;
Christopher Fauleta3618372018-12-13 21:59:56 +01002855 }
Christopher Fauletb7f88902019-07-15 21:56:43 +02002856 goto wait;
2857 }
Christopher Fauleta3618372018-12-13 21:59:56 +01002858
Christopher Fauletb7f88902019-07-15 21:56:43 +02002859 /* The request was fully received. Copy data */
2860 blk = htx_get_head_blk(htx);
2861 while (blk) {
2862 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Fauleta3618372018-12-13 21:59:56 +01002863
Christopher Fauletb7f88902019-07-15 21:56:43 +02002864 if (type == HTX_BLK_EOM || type == HTX_BLK_TLR || type == HTX_BLK_EOT)
2865 break;
2866 if (type == HTX_BLK_DATA) {
2867 struct ist v = htx_get_blk_value(htx, blk);
Christopher Fauleta3618372018-12-13 21:59:56 +01002868
Christopher Fauletb7f88902019-07-15 21:56:43 +02002869 if (!chunk_memcat(temp, v.ptr, v.len)) {
Christopher Faulet2f9a41d2019-02-27 15:30:57 +01002870 appctx->ctx.stats.st_code = STAT_STATUS_EXCD;
2871 goto out;
2872 }
Christopher Fauleta3618372018-12-13 21:59:56 +01002873 }
Christopher Fauletb7f88902019-07-15 21:56:43 +02002874 blk = htx_get_next_blk(htx, blk);
William Lallemand74c24fb2016-11-21 17:18:36 +01002875 }
2876
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002877 first_param = temp->area;
Christopher Fauleta3618372018-12-13 21:59:56 +01002878 end_params = temp->area + temp->data;
William Lallemand74c24fb2016-11-21 17:18:36 +01002879 cur_param = next_param = end_params;
2880 *end_params = '\0';
2881
2882 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
2883
2884 /*
2885 * Parse the parameters in reverse order to only store the last value.
2886 * From the html form, the backend and the action are at the end.
2887 */
2888 while (cur_param > first_param) {
2889 char *value;
2890 int poffset, plen;
2891
2892 cur_param--;
2893
2894 if ((*cur_param == '&') || (cur_param == first_param)) {
2895 reprocess_servers:
2896 /* Parse the key */
2897 poffset = (cur_param != first_param ? 1 : 0);
2898 plen = next_param - cur_param + (cur_param == first_param ? 1 : 0);
2899 if ((plen > 0) && (plen <= sizeof(key))) {
2900 strncpy(key, cur_param + poffset, plen);
2901 key[plen - 1] = '\0';
2902 } else {
Christopher Faulet2f9a41d2019-02-27 15:30:57 +01002903 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
William Lallemand74c24fb2016-11-21 17:18:36 +01002904 goto out;
2905 }
2906
2907 /* Parse the value */
2908 value = key;
2909 while (*value != '\0' && *value != '=') {
2910 value++;
2911 }
2912 if (*value == '=') {
2913 /* Ok, a value is found, we can mark the end of the key */
2914 *value++ = '\0';
2915 }
Willy Tarreau62ba9ba2020-04-23 17:54:47 +02002916 if (url_decode(key, 1) < 0 || url_decode(value, 1) < 0)
William Lallemand74c24fb2016-11-21 17:18:36 +01002917 break;
2918
2919 /* Now we can check the key to see what to do */
2920 if (!px && (strcmp(key, "b") == 0)) {
2921 if ((px = proxy_be_by_name(value)) == NULL) {
2922 /* the backend name is unknown or ambiguous (duplicate names) */
2923 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
2924 goto out;
2925 }
2926 }
2927 else if (!action && (strcmp(key, "action") == 0)) {
2928 if (strcmp(value, "ready") == 0) {
2929 action = ST_ADM_ACTION_READY;
2930 }
2931 else if (strcmp(value, "drain") == 0) {
2932 action = ST_ADM_ACTION_DRAIN;
2933 }
2934 else if (strcmp(value, "maint") == 0) {
2935 action = ST_ADM_ACTION_MAINT;
2936 }
2937 else if (strcmp(value, "shutdown") == 0) {
2938 action = ST_ADM_ACTION_SHUTDOWN;
2939 }
2940 else if (strcmp(value, "dhlth") == 0) {
2941 action = ST_ADM_ACTION_DHLTH;
2942 }
2943 else if (strcmp(value, "ehlth") == 0) {
2944 action = ST_ADM_ACTION_EHLTH;
2945 }
2946 else if (strcmp(value, "hrunn") == 0) {
2947 action = ST_ADM_ACTION_HRUNN;
2948 }
2949 else if (strcmp(value, "hnolb") == 0) {
2950 action = ST_ADM_ACTION_HNOLB;
2951 }
2952 else if (strcmp(value, "hdown") == 0) {
2953 action = ST_ADM_ACTION_HDOWN;
2954 }
2955 else if (strcmp(value, "dagent") == 0) {
2956 action = ST_ADM_ACTION_DAGENT;
2957 }
2958 else if (strcmp(value, "eagent") == 0) {
2959 action = ST_ADM_ACTION_EAGENT;
2960 }
2961 else if (strcmp(value, "arunn") == 0) {
2962 action = ST_ADM_ACTION_ARUNN;
2963 }
2964 else if (strcmp(value, "adown") == 0) {
2965 action = ST_ADM_ACTION_ADOWN;
2966 }
2967 /* else these are the old supported methods */
2968 else if (strcmp(value, "disable") == 0) {
2969 action = ST_ADM_ACTION_DISABLE;
2970 }
2971 else if (strcmp(value, "enable") == 0) {
2972 action = ST_ADM_ACTION_ENABLE;
2973 }
2974 else if (strcmp(value, "stop") == 0) {
2975 action = ST_ADM_ACTION_STOP;
2976 }
2977 else if (strcmp(value, "start") == 0) {
2978 action = ST_ADM_ACTION_START;
2979 }
2980 else {
2981 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
2982 goto out;
2983 }
2984 }
2985 else if (strcmp(key, "s") == 0) {
2986 if (!(px && action)) {
2987 /*
2988 * Indicates that we'll need to reprocess the parameters
2989 * as soon as backend and action are known
2990 */
2991 if (!reprocess) {
2992 st_cur_param = cur_param;
2993 st_next_param = next_param;
2994 }
2995 reprocess = 1;
2996 }
2997 else if ((sv = findserver(px, value)) != NULL) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002998 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
William Lallemand74c24fb2016-11-21 17:18:36 +01002999 switch (action) {
3000 case ST_ADM_ACTION_DISABLE:
Emeric Brun52a91d32017-08-31 14:41:55 +02003001 if (!(sv->cur_admin & SRV_ADMF_FMAINT)) {
William Lallemand74c24fb2016-11-21 17:18:36 +01003002 altered_servers++;
3003 total_servers++;
3004 srv_set_admin_flag(sv, SRV_ADMF_FMAINT, "'disable' on stats page");
3005 }
3006 break;
3007 case ST_ADM_ACTION_ENABLE:
Emeric Brun52a91d32017-08-31 14:41:55 +02003008 if (sv->cur_admin & SRV_ADMF_FMAINT) {
William Lallemand74c24fb2016-11-21 17:18:36 +01003009 altered_servers++;
3010 total_servers++;
3011 srv_clr_admin_flag(sv, SRV_ADMF_FMAINT);
3012 }
3013 break;
3014 case ST_ADM_ACTION_STOP:
Emeric Brun52a91d32017-08-31 14:41:55 +02003015 if (!(sv->cur_admin & SRV_ADMF_FDRAIN)) {
William Lallemand74c24fb2016-11-21 17:18:36 +01003016 srv_set_admin_flag(sv, SRV_ADMF_FDRAIN, "'stop' on stats page");
3017 altered_servers++;
3018 total_servers++;
3019 }
3020 break;
3021 case ST_ADM_ACTION_START:
Emeric Brun52a91d32017-08-31 14:41:55 +02003022 if (sv->cur_admin & SRV_ADMF_FDRAIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +01003023 srv_clr_admin_flag(sv, SRV_ADMF_FDRAIN);
3024 altered_servers++;
3025 total_servers++;
3026 }
3027 break;
3028 case ST_ADM_ACTION_DHLTH:
3029 if (sv->check.state & CHK_ST_CONFIGURED) {
3030 sv->check.state &= ~CHK_ST_ENABLED;
3031 altered_servers++;
3032 total_servers++;
3033 }
3034 break;
3035 case ST_ADM_ACTION_EHLTH:
3036 if (sv->check.state & CHK_ST_CONFIGURED) {
3037 sv->check.state |= CHK_ST_ENABLED;
3038 altered_servers++;
3039 total_servers++;
3040 }
3041 break;
3042 case ST_ADM_ACTION_HRUNN:
3043 if (!(sv->track)) {
3044 sv->check.health = sv->check.rise + sv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02003045 srv_set_running(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01003046 altered_servers++;
3047 total_servers++;
3048 }
3049 break;
3050 case ST_ADM_ACTION_HNOLB:
3051 if (!(sv->track)) {
3052 sv->check.health = sv->check.rise + sv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02003053 srv_set_stopping(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01003054 altered_servers++;
3055 total_servers++;
3056 }
3057 break;
3058 case ST_ADM_ACTION_HDOWN:
3059 if (!(sv->track)) {
3060 sv->check.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02003061 srv_set_stopped(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01003062 altered_servers++;
3063 total_servers++;
3064 }
3065 break;
3066 case ST_ADM_ACTION_DAGENT:
3067 if (sv->agent.state & CHK_ST_CONFIGURED) {
3068 sv->agent.state &= ~CHK_ST_ENABLED;
3069 altered_servers++;
3070 total_servers++;
3071 }
3072 break;
3073 case ST_ADM_ACTION_EAGENT:
3074 if (sv->agent.state & CHK_ST_CONFIGURED) {
3075 sv->agent.state |= CHK_ST_ENABLED;
3076 altered_servers++;
3077 total_servers++;
3078 }
3079 break;
3080 case ST_ADM_ACTION_ARUNN:
3081 if (sv->agent.state & CHK_ST_ENABLED) {
3082 sv->agent.health = sv->agent.rise + sv->agent.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02003083 srv_set_running(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01003084 altered_servers++;
3085 total_servers++;
3086 }
3087 break;
3088 case ST_ADM_ACTION_ADOWN:
3089 if (sv->agent.state & CHK_ST_ENABLED) {
3090 sv->agent.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02003091 srv_set_stopped(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01003092 altered_servers++;
3093 total_servers++;
3094 }
3095 break;
3096 case ST_ADM_ACTION_READY:
3097 srv_adm_set_ready(sv);
3098 altered_servers++;
3099 total_servers++;
3100 break;
3101 case ST_ADM_ACTION_DRAIN:
3102 srv_adm_set_drain(sv);
3103 altered_servers++;
3104 total_servers++;
3105 break;
3106 case ST_ADM_ACTION_MAINT:
3107 srv_adm_set_maint(sv);
3108 altered_servers++;
3109 total_servers++;
3110 break;
3111 case ST_ADM_ACTION_SHUTDOWN:
3112 if (px->state != PR_STSTOPPED) {
Willy Tarreauaf7ea812019-11-14 16:42:04 +01003113 srv_shutdown_streams(sv, SF_ERR_KILLED);
William Lallemand74c24fb2016-11-21 17:18:36 +01003114 altered_servers++;
3115 total_servers++;
3116 }
3117 break;
3118 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003119 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
William Lallemand74c24fb2016-11-21 17:18:36 +01003120 } else {
3121 /* the server name is unknown or ambiguous (duplicate names) */
3122 total_servers++;
3123 }
3124 }
3125 if (reprocess && px && action) {
3126 /* Now, we know the backend and the action chosen by the user.
3127 * We can safely restart from the first server parameter
3128 * to reprocess them
3129 */
3130 cur_param = st_cur_param;
3131 next_param = st_next_param;
3132 reprocess = 0;
3133 goto reprocess_servers;
3134 }
3135
3136 next_param = cur_param;
3137 }
3138 }
3139
3140 if (total_servers == 0) {
3141 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
3142 }
3143 else if (altered_servers == 0) {
3144 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
3145 }
3146 else if (altered_servers == total_servers) {
3147 appctx->ctx.stats.st_code = STAT_STATUS_DONE;
3148 }
3149 else {
3150 appctx->ctx.stats.st_code = STAT_STATUS_PART;
3151 }
3152 out:
3153 return 1;
Christopher Faulet2f9a41d2019-02-27 15:30:57 +01003154 wait:
3155 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
3156 return 0;
Christopher Fauletef779222018-10-31 08:47:01 +01003157}
3158
3159
Christopher Fauletb7f88902019-07-15 21:56:43 +02003160static int stats_send_http_headers(struct stream_interface *si, struct htx *htx)
Christopher Fauletef779222018-10-31 08:47:01 +01003161{
3162 struct stream *s = si_strm(si);
3163 struct uri_auth *uri = s->be->uri_auth;
3164 struct appctx *appctx = __objt_appctx(si->end);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003165 struct htx_sl *sl;
3166 unsigned int flags;
Christopher Fauletef779222018-10-31 08:47:01 +01003167
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003168 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);
3169 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), ist("200"), ist("OK"));
3170 if (!sl)
Christopher Fauletef779222018-10-31 08:47:01 +01003171 goto full;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003172 sl->info.res.status = 200;
Christopher Fauletef779222018-10-31 08:47:01 +01003173
Christopher Fauletb829f4c2019-03-29 16:13:55 +01003174 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")))
Christopher Fauletef779222018-10-31 08:47:01 +01003175 goto full;
3176 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
3177 if (!htx_add_header(htx, ist("Content-Type"), ist("text/html")))
3178 goto full;
3179 }
Christopher Faulet6338a082019-09-09 15:50:54 +02003180 else if (appctx->ctx.stats.flags & (STAT_FMT_JSON|STAT_JSON_SCHM)) {
3181 if (!htx_add_header(htx, ist("Content-Type"), ist("application/json")))
3182 goto full;
3183 }
Christopher Fauletef779222018-10-31 08:47:01 +01003184 else {
3185 if (!htx_add_header(htx, ist("Content-Type"), ist("text/plain")))
3186 goto full;
3187 }
3188
3189 if (uri->refresh > 0 && !(appctx->ctx.stats.flags & STAT_NO_REFRESH)) {
3190 const char *refresh = U2A(uri->refresh);
3191 if (!htx_add_header(htx, ist("Refresh"), ist2(refresh, strlen(refresh))))
3192 goto full;
3193 }
3194
3195 if (appctx->ctx.stats.flags & STAT_CHUNKED) {
3196 if (!htx_add_header(htx, ist("Transfer-Encoding"), ist("chunked")))
3197 goto full;
3198 }
3199
3200 if (!htx_add_endof(htx, HTX_BLK_EOH))
3201 goto full;
3202
Christopher Faulet1e2d6362019-02-27 16:28:48 +01003203 channel_add_input(&s->res, htx->data);
Christopher Fauletef779222018-10-31 08:47:01 +01003204 return 1;
3205
3206 full:
3207 htx_reset(htx);
3208 si_rx_room_blk(si);
3209 return 0;
William Lallemand74c24fb2016-11-21 17:18:36 +01003210}
3211
Christopher Fauletef779222018-10-31 08:47:01 +01003212
Christopher Fauletb7f88902019-07-15 21:56:43 +02003213static int stats_send_http_redirect(struct stream_interface *si, struct htx *htx)
Christopher Fauletef779222018-10-31 08:47:01 +01003214{
3215 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
3216 struct stream *s = si_strm(si);
3217 struct uri_auth *uri = s->be->uri_auth;
3218 struct appctx *appctx = __objt_appctx(si->end);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003219 struct htx_sl *sl;
3220 unsigned int flags;
Christopher Fauletef779222018-10-31 08:47:01 +01003221
3222 /* scope_txt = search pattern + search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
3223 scope_txt[0] = 0;
3224 if (appctx->ctx.stats.scope_len) {
Christopher Fauleted7a0662019-01-14 11:07:34 +01003225 const char *scope_ptr = stats_scope_ptr(appctx, si);
3226
Christopher Fauletef779222018-10-31 08:47:01 +01003227 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Christopher Fauleted7a0662019-01-14 11:07:34 +01003228 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), scope_ptr, appctx->ctx.stats.scope_len);
Christopher Fauletef779222018-10-31 08:47:01 +01003229 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
3230 }
3231
3232 /* We don't want to land on the posted stats page because a refresh will
3233 * repost the data. We don't want this to happen on accident so we redirect
3234 * the browse to the stats page with a GET.
3235 */
3236 chunk_printf(&trash, "%s;st=%s%s%s%s",
3237 uri->uri_prefix,
3238 ((appctx->ctx.stats.st_code > STAT_STATUS_INIT) &&
3239 (appctx->ctx.stats.st_code < STAT_STATUS_SIZE) &&
3240 stat_status_codes[appctx->ctx.stats.st_code]) ?
3241 stat_status_codes[appctx->ctx.stats.st_code] :
3242 stat_status_codes[STAT_STATUS_UNKN],
3243 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3244 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
3245 scope_txt);
3246
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003247 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_CHNK);
3248 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), ist("303"), ist("See Other"));
3249 if (!sl)
Christopher Fauletef779222018-10-31 08:47:01 +01003250 goto full;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003251 sl->info.res.status = 303;
Christopher Fauletef779222018-10-31 08:47:01 +01003252
3253 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")) ||
Christopher Fauletef779222018-10-31 08:47:01 +01003254 !htx_add_header(htx, ist("Content-Type"), ist("text/plain")) ||
3255 !htx_add_header(htx, ist("Content-Length"), ist("0")) ||
3256 !htx_add_header(htx, ist("Location"), ist2(trash.area, trash.data)))
3257 goto full;
3258
3259 if (!htx_add_endof(htx, HTX_BLK_EOH))
3260 goto full;
3261
Christopher Faulet1e2d6362019-02-27 16:28:48 +01003262 channel_add_input(&s->res, htx->data);
Christopher Fauletef779222018-10-31 08:47:01 +01003263 return 1;
3264
3265full:
3266 htx_reset(htx);
3267 si_rx_room_blk(si);
3268 return 0;
3269}
William Lallemand74c24fb2016-11-21 17:18:36 +01003270
Simon Horman05ee2132017-01-04 09:37:25 +01003271
William Lallemand74c24fb2016-11-21 17:18:36 +01003272/* This I/O handler runs as an applet embedded in a stream interface. It is
3273 * used to send HTTP stats over a TCP socket. The mechanism is very simple.
3274 * appctx->st0 contains the operation in progress (dump, done). The handler
3275 * automatically unregisters itself once transfer is complete.
3276 */
Christopher Fauletb7f88902019-07-15 21:56:43 +02003277static void http_stats_io_handler(struct appctx *appctx)
Christopher Fauletef779222018-10-31 08:47:01 +01003278{
3279 struct stream_interface *si = appctx->owner;
3280 struct stream *s = si_strm(si);
3281 struct channel *req = si_oc(si);
3282 struct channel *res = si_ic(si);
3283 struct htx *req_htx, *res_htx;
3284
3285 res_htx = htx_from_buf(&res->buf);
3286
3287 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
3288 goto out;
3289
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05003290 /* Check if the input buffer is available. */
Christopher Fauletef779222018-10-31 08:47:01 +01003291 if (!b_size(&res->buf)) {
3292 si_rx_room_blk(si);
3293 goto out;
3294 }
3295
3296 /* check that the output is not closed */
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003297 if (res->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_SHUTR))
3298 appctx->st0 = STAT_HTTP_END;
Christopher Fauletef779222018-10-31 08:47:01 +01003299
3300 /* all states are processed in sequence */
3301 if (appctx->st0 == STAT_HTTP_HEAD) {
Christopher Fauletb7f88902019-07-15 21:56:43 +02003302 if (stats_send_http_headers(si, res_htx)) {
Christopher Fauletef779222018-10-31 08:47:01 +01003303 if (s->txn->meth == HTTP_METH_HEAD)
3304 appctx->st0 = STAT_HTTP_DONE;
3305 else
3306 appctx->st0 = STAT_HTTP_DUMP;
3307 }
3308 }
3309
3310 if (appctx->st0 == STAT_HTTP_DUMP) {
3311 if (stats_dump_stat_to_buffer(si, res_htx, s->be->uri_auth))
3312 appctx->st0 = STAT_HTTP_DONE;
3313 }
3314
3315 if (appctx->st0 == STAT_HTTP_POST) {
3316 if (stats_process_http_post(si))
3317 appctx->st0 = STAT_HTTP_LAST;
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003318 else if (req->flags & CF_SHUTR)
Christopher Fauletef779222018-10-31 08:47:01 +01003319 appctx->st0 = STAT_HTTP_DONE;
3320 }
3321
3322 if (appctx->st0 == STAT_HTTP_LAST) {
Christopher Fauletb7f88902019-07-15 21:56:43 +02003323 if (stats_send_http_redirect(si, res_htx))
Christopher Fauletef779222018-10-31 08:47:01 +01003324 appctx->st0 = STAT_HTTP_DONE;
3325 }
3326
3327 if (appctx->st0 == STAT_HTTP_DONE) {
Christopher Faulet54b5e212019-06-04 10:08:28 +02003328 /* Don't add TLR because mux-h1 will take care of it */
Christopher Fauletef779222018-10-31 08:47:01 +01003329 if (!htx_add_endof(res_htx, HTX_BLK_EOM)) {
3330 si_rx_room_blk(si);
3331 goto out;
3332 }
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003333 channel_add_input(&s->res, 1);
3334 appctx->st0 = STAT_HTTP_END;
Christopher Fauletef779222018-10-31 08:47:01 +01003335 }
3336
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003337 if (appctx->st0 == STAT_HTTP_END) {
3338 if (!(res->flags & CF_SHUTR)) {
Christopher Fauletef779222018-10-31 08:47:01 +01003339 res->flags |= CF_READ_NULL;
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003340 si_shutr(si);
3341 }
3342
3343 /* eat the whole request */
3344 if (co_data(req)) {
3345 req_htx = htx_from_buf(&req->buf);
3346 co_htx_skip(req, req_htx, co_data(req));
3347 htx_to_buf(req_htx, &req->buf);
Christopher Fauletef779222018-10-31 08:47:01 +01003348 }
3349 }
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003350
Christopher Fauletef779222018-10-31 08:47:01 +01003351 out:
3352 /* we have left the request in the buffer for the case where we
3353 * process a POST, and this automatically re-enables activity on
3354 * read. It's better to indicate that we want to stop reading when
3355 * we're sending, so that we know there's at most one direction
3356 * deciding to wake the applet up. It saves it from looping when
3357 * emitting large blocks into small TCP windows.
3358 */
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003359 htx_to_buf(res_htx, &res->buf);
3360 if (!channel_is_empty(res))
Christopher Fauletef779222018-10-31 08:47:01 +01003361 si_stop_get(si);
Christopher Fauletef779222018-10-31 08:47:01 +01003362}
3363
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003364/* Dump all fields from <info> into <out> using the "show info" format (name: value) */
Willy Tarreau83061a82018-07-13 11:56:34 +02003365static int stats_dump_info_fields(struct buffer *out,
Willy Tarreau43241ff2019-10-09 11:27:51 +02003366 const struct field *info, unsigned int flags)
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003367{
3368 int field;
3369
3370 for (field = 0; field < INF_TOTAL_FIELDS; field++) {
3371 if (!field_format(info, field))
3372 continue;
3373
Willy Tarreaueaa55372019-10-09 07:39:11 +02003374 if (!chunk_appendf(out, "%s: ", info_fields[field].name))
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003375 return 0;
3376 if (!stats_emit_raw_data_field(out, &info[field]))
3377 return 0;
Willy Tarreau6b19b142019-10-09 15:44:21 +02003378 if ((flags & STAT_SHOW_FDESC) && !chunk_appendf(out, ":\"%s\"", info_fields[field].desc))
3379 return 0;
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003380 if (!chunk_strcat(out, "\n"))
3381 return 0;
3382 }
3383 return 1;
3384}
3385
3386/* Dump all fields from <info> into <out> using the "show info typed" format */
Willy Tarreau83061a82018-07-13 11:56:34 +02003387static int stats_dump_typed_info_fields(struct buffer *out,
Willy Tarreau43241ff2019-10-09 11:27:51 +02003388 const struct field *info, unsigned int flags)
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003389{
3390 int field;
3391
3392 for (field = 0; field < INF_TOTAL_FIELDS; field++) {
3393 if (!field_format(info, field))
3394 continue;
3395
Willy Tarreaueaa55372019-10-09 07:39:11 +02003396 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 +01003397 return 0;
3398 if (!stats_emit_field_tags(out, &info[field], ':'))
3399 return 0;
3400 if (!stats_emit_typed_data_field(out, &info[field]))
3401 return 0;
Willy Tarreau6b19b142019-10-09 15:44:21 +02003402 if ((flags & STAT_SHOW_FDESC) && !chunk_appendf(out, ":\"%s\"", info_fields[field].desc))
3403 return 0;
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003404 if (!chunk_strcat(out, "\n"))
3405 return 0;
3406 }
3407 return 1;
3408}
3409
3410/* Fill <info> with HAProxy global info. <info> is preallocated
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05003411 * array of length <len>. The length of the array must be
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003412 * INF_TOTAL_FIELDS. If this length is less then this value, the
3413 * function returns 0, otherwise, it returns 1.
3414 */
3415int stats_fill_info(struct field *info, int len)
3416{
3417 unsigned int up = (now.tv_sec - start_date.tv_sec);
Willy Tarreau83061a82018-07-13 11:56:34 +02003418 struct buffer *out = get_trash_chunk();
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003419
3420#ifdef USE_OPENSSL
3421 int ssl_sess_rate = read_freq_ctr(&global.ssl_per_sec);
3422 int ssl_key_rate = read_freq_ctr(&global.ssl_fe_keys_per_sec);
3423 int ssl_reuse = 0;
3424
3425 if (ssl_key_rate < ssl_sess_rate) {
3426 /* count the ssl reuse ratio and avoid overflows in both directions */
3427 ssl_reuse = 100 - (100 * ssl_key_rate + (ssl_sess_rate - 1) / 2) / ssl_sess_rate;
3428 }
3429#endif
3430
3431 if (len < INF_TOTAL_FIELDS)
3432 return 0;
3433
3434 chunk_reset(out);
3435 memset(info, 0, sizeof(*info) * len);
3436
3437 info[INF_NAME] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, PRODUCT_NAME);
Willy Tarreau909b9d82019-01-04 18:20:32 +01003438 info[INF_VERSION] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, haproxy_version);
3439 info[INF_RELEASE_DATE] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, haproxy_date);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003440
Yves Lafon95317282018-02-26 11:10:37 +01003441 info[INF_NBTHREAD] = mkf_u32(FO_CONFIG|FS_SERVICE, global.nbthread);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003442 info[INF_NBPROC] = mkf_u32(FO_CONFIG|FS_SERVICE, global.nbproc);
3443 info[INF_PROCESS_NUM] = mkf_u32(FO_KEY, relative_pid);
3444 info[INF_PID] = mkf_u32(FO_STATUS, pid);
3445
3446 info[INF_UPTIME] = mkf_str(FN_DURATION, chunk_newstr(out));
3447 chunk_appendf(out, "%ud %uh%02um%02us", up / 86400, (up % 86400) / 3600, (up % 3600) / 60, (up % 60));
3448
3449 info[INF_UPTIME_SEC] = mkf_u32(FN_DURATION, up);
3450 info[INF_MEMMAX_MB] = mkf_u32(FO_CONFIG|FN_LIMIT, global.rlimit_memmax);
3451 info[INF_POOL_ALLOC_MB] = mkf_u32(0, (unsigned)(pool_total_allocated() / 1048576L));
3452 info[INF_POOL_USED_MB] = mkf_u32(0, (unsigned)(pool_total_used() / 1048576L));
3453 info[INF_POOL_FAILED] = mkf_u32(FN_COUNTER, pool_total_failures());
3454 info[INF_ULIMIT_N] = mkf_u32(FO_CONFIG|FN_LIMIT, global.rlimit_nofile);
3455 info[INF_MAXSOCK] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxsock);
3456 info[INF_MAXCONN] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxconn);
3457 info[INF_HARD_MAXCONN] = mkf_u32(FO_CONFIG|FN_LIMIT, global.hardmaxconn);
3458 info[INF_CURR_CONN] = mkf_u32(0, actconn);
3459 info[INF_CUM_CONN] = mkf_u32(FN_COUNTER, totalconn);
3460 info[INF_CUM_REQ] = mkf_u32(FN_COUNTER, global.req_count);
3461#ifdef USE_OPENSSL
3462 info[INF_MAX_SSL_CONNS] = mkf_u32(FN_MAX, global.maxsslconn);
3463 info[INF_CURR_SSL_CONNS] = mkf_u32(0, sslconns);
3464 info[INF_CUM_SSL_CONNS] = mkf_u32(FN_COUNTER, totalsslconns);
3465#endif
3466 info[INF_MAXPIPES] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxpipes);
3467 info[INF_PIPES_USED] = mkf_u32(0, pipes_used);
3468 info[INF_PIPES_FREE] = mkf_u32(0, pipes_free);
3469 info[INF_CONN_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&global.conn_per_sec));
3470 info[INF_CONN_RATE_LIMIT] = mkf_u32(FO_CONFIG|FN_LIMIT, global.cps_lim);
3471 info[INF_MAX_CONN_RATE] = mkf_u32(FN_MAX, global.cps_max);
3472 info[INF_SESS_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&global.sess_per_sec));
3473 info[INF_SESS_RATE_LIMIT] = mkf_u32(FO_CONFIG|FN_LIMIT, global.sps_lim);
3474 info[INF_MAX_SESS_RATE] = mkf_u32(FN_RATE, global.sps_max);
3475
3476#ifdef USE_OPENSSL
3477 info[INF_SSL_RATE] = mkf_u32(FN_RATE, ssl_sess_rate);
3478 info[INF_SSL_RATE_LIMIT] = mkf_u32(FO_CONFIG|FN_LIMIT, global.ssl_lim);
3479 info[INF_MAX_SSL_RATE] = mkf_u32(FN_MAX, global.ssl_max);
3480 info[INF_SSL_FRONTEND_KEY_RATE] = mkf_u32(0, ssl_key_rate);
3481 info[INF_SSL_FRONTEND_MAX_KEY_RATE] = mkf_u32(FN_MAX, global.ssl_fe_keys_max);
3482 info[INF_SSL_FRONTEND_SESSION_REUSE_PCT] = mkf_u32(0, ssl_reuse);
3483 info[INF_SSL_BACKEND_KEY_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&global.ssl_be_keys_per_sec));
3484 info[INF_SSL_BACKEND_MAX_KEY_RATE] = mkf_u32(FN_MAX, global.ssl_be_keys_max);
3485 info[INF_SSL_CACHE_LOOKUPS] = mkf_u32(FN_COUNTER, global.shctx_lookups);
3486 info[INF_SSL_CACHE_MISSES] = mkf_u32(FN_COUNTER, global.shctx_misses);
3487#endif
3488 info[INF_COMPRESS_BPS_IN] = mkf_u32(FN_RATE, read_freq_ctr(&global.comp_bps_in));
3489 info[INF_COMPRESS_BPS_OUT] = mkf_u32(FN_RATE, read_freq_ctr(&global.comp_bps_out));
3490 info[INF_COMPRESS_BPS_RATE_LIM] = mkf_u32(FO_CONFIG|FN_LIMIT, global.comp_rate_lim);
3491#ifdef USE_ZLIB
3492 info[INF_ZLIB_MEM_USAGE] = mkf_u32(0, zlib_used_memory);
3493 info[INF_MAX_ZLIB_MEM_USAGE] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxzlibmem);
3494#endif
3495 info[INF_TASKS] = mkf_u32(0, nb_tasks_cur);
Christopher Faulet34c5cc92016-12-06 09:15:30 +01003496 info[INF_RUN_QUEUE] = mkf_u32(0, tasks_run_queue_cur);
Willy Tarreau81036f22019-05-20 19:24:50 +02003497 info[INF_IDLE_PCT] = mkf_u32(FN_AVG, ti->idle_pct);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003498 info[INF_NODE] = mkf_str(FO_CONFIG|FN_OUTPUT|FS_SERVICE, global.node);
3499 if (global.desc)
3500 info[INF_DESCRIPTION] = mkf_str(FO_CONFIG|FN_OUTPUT|FS_SERVICE, global.desc);
Willy Tarreau00098ea2018-11-05 14:38:13 +01003501 info[INF_STOPPING] = mkf_u32(0, stopping);
3502 info[INF_JOBS] = mkf_u32(0, jobs);
William Lallemanda7199262018-11-16 16:57:20 +01003503 info[INF_UNSTOPPABLE_JOBS] = mkf_u32(0, unstoppable_jobs);
Willy Tarreau00098ea2018-11-05 14:38:13 +01003504 info[INF_LISTENERS] = mkf_u32(0, listeners);
Willy Tarreau199ad242018-11-05 16:31:22 +01003505 info[INF_ACTIVE_PEERS] = mkf_u32(0, active_peers);
Willy Tarreau2d372c22018-11-05 17:12:27 +01003506 info[INF_CONNECTED_PEERS] = mkf_u32(0, connected_peers);
Willy Tarreau13ef7732018-11-12 07:25:28 +01003507 info[INF_DROPPED_LOGS] = mkf_u32(0, dropped_logs);
Willy Tarreaubeb859a2018-11-22 18:07:59 +01003508 info[INF_BUSY_POLLING] = mkf_u32(0, !!(global.tune.options & GTUNE_BUSY_POLLING));
Baptiste Assmann333939c2019-01-21 08:34:50 +01003509 info[INF_FAILED_RESOLUTIONS] = mkf_u32(0, dns_failed_resolutions);
Willy Tarreau7cf0e452019-05-23 11:39:14 +02003510 info[INF_TOTAL_BYTES_OUT] = mkf_u64(0, global.out_bytes);
3511 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 +02003512 info[INF_DEBUG_COMMANDS_ISSUED] = mkf_u32(0, debug_commands_issued);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003513
3514 return 1;
3515}
3516
3517/* This function dumps information onto the stream interface's read buffer.
3518 * It returns 0 as long as it does not complete, non-zero upon completion.
3519 * No state is used.
3520 */
3521static int stats_dump_info_to_buffer(struct stream_interface *si)
3522{
3523 struct appctx *appctx = __objt_appctx(si->end);
3524
3525 if (!stats_fill_info(info, INF_TOTAL_FIELDS))
3526 return 0;
3527
3528 chunk_reset(&trash);
3529
3530 if (appctx->ctx.stats.flags & STAT_FMT_TYPED)
Willy Tarreau43241ff2019-10-09 11:27:51 +02003531 stats_dump_typed_info_fields(&trash, info, appctx->ctx.stats.flags);
Simon Horman05ee2132017-01-04 09:37:25 +01003532 else if (appctx->ctx.stats.flags & STAT_FMT_JSON)
Willy Tarreau43241ff2019-10-09 11:27:51 +02003533 stats_dump_json_info_fields(&trash, info, appctx->ctx.stats.flags);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003534 else
Willy Tarreau43241ff2019-10-09 11:27:51 +02003535 stats_dump_info_fields(&trash, info, appctx->ctx.stats.flags);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003536
Willy Tarreau06d80a92017-10-19 14:32:15 +02003537 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01003538 si_rx_room_blk(si);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003539 return 0;
3540 }
3541
3542 return 1;
3543}
3544
Simon Horman6f6bb382017-01-04 09:37:26 +01003545/* This function dumps the schema onto the stream interface's read buffer.
3546 * It returns 0 as long as it does not complete, non-zero upon completion.
3547 * No state is used.
3548 *
3549 * Integer values bouned to the range [-(2**53)+1, (2**53)-1] as
3550 * per the recommendation for interoperable integers in section 6 of RFC 7159.
3551 */
Willy Tarreau83061a82018-07-13 11:56:34 +02003552static void stats_dump_json_schema(struct buffer *out)
Simon Horman6f6bb382017-01-04 09:37:26 +01003553{
3554
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003555 int old_len = out->data;
Simon Horman6f6bb382017-01-04 09:37:26 +01003556
3557 chunk_strcat(out,
3558 "{"
3559 "\"$schema\":\"http://json-schema.org/draft-04/schema#\","
3560 "\"oneOf\":["
3561 "{"
3562 "\"title\":\"Info\","
3563 "\"type\":\"array\","
3564 "\"items\":{"
3565 "\"properties\":{"
3566 "\"title\":\"InfoItem\","
3567 "\"type\":\"object\","
3568 "\"field\":{\"$ref\":\"#/definitions/field\"},"
3569 "\"processNum\":{\"$ref\":\"#/definitions/processNum\"},"
3570 "\"tags\":{\"$ref\":\"#/definitions/tags\"},"
3571 "\"value\":{\"$ref\":\"#/definitions/typedValue\"}"
3572 "},"
3573 "\"required\":[\"field\",\"processNum\",\"tags\","
3574 "\"value\"]"
3575 "}"
3576 "},"
3577 "{"
3578 "\"title\":\"Stat\","
3579 "\"type\":\"array\","
3580 "\"items\":{"
3581 "\"title\":\"InfoItem\","
3582 "\"type\":\"object\","
3583 "\"properties\":{"
3584 "\"objType\":{"
3585 "\"enum\":[\"Frontend\",\"Backend\",\"Listener\","
3586 "\"Server\",\"Unknown\"]"
3587 "},"
3588 "\"proxyId\":{"
3589 "\"type\":\"integer\","
3590 "\"minimum\":0"
3591 "},"
3592 "\"id\":{"
3593 "\"type\":\"integer\","
3594 "\"minimum\":0"
3595 "},"
3596 "\"field\":{\"$ref\":\"#/definitions/field\"},"
3597 "\"processNum\":{\"$ref\":\"#/definitions/processNum\"},"
3598 "\"tags\":{\"$ref\":\"#/definitions/tags\"},"
3599 "\"typedValue\":{\"$ref\":\"#/definitions/typedValue\"}"
3600 "},"
3601 "\"required\":[\"objType\",\"proxyId\",\"id\","
3602 "\"field\",\"processNum\",\"tags\","
3603 "\"value\"]"
3604 "}"
3605 "},"
3606 "{"
3607 "\"title\":\"Error\","
3608 "\"type\":\"object\","
3609 "\"properties\":{"
3610 "\"errorStr\":{"
3611 "\"type\":\"string\""
3612 "},"
3613 "\"required\":[\"errorStr\"]"
3614 "}"
3615 "}"
3616 "],"
3617 "\"definitions\":{"
3618 "\"field\":{"
3619 "\"type\":\"object\","
3620 "\"pos\":{"
3621 "\"type\":\"integer\","
3622 "\"minimum\":0"
3623 "},"
3624 "\"name\":{"
3625 "\"type\":\"string\""
3626 "},"
3627 "\"required\":[\"pos\",\"name\"]"
3628 "},"
3629 "\"processNum\":{"
3630 "\"type\":\"integer\","
3631 "\"minimum\":1"
3632 "},"
3633 "\"tags\":{"
3634 "\"type\":\"object\","
3635 "\"origin\":{"
3636 "\"type\":\"string\","
3637 "\"enum\":[\"Metric\",\"Status\",\"Key\","
3638 "\"Config\",\"Product\",\"Unknown\"]"
3639 "},"
3640 "\"nature\":{"
3641 "\"type\":\"string\","
3642 "\"enum\":[\"Gauge\",\"Limit\",\"Min\",\"Max\","
3643 "\"Rate\",\"Counter\",\"Duration\","
3644 "\"Age\",\"Time\",\"Name\",\"Output\","
3645 "\"Avg\", \"Unknown\"]"
3646 "},"
3647 "\"scope\":{"
3648 "\"type\":\"string\","
3649 "\"enum\":[\"Cluster\",\"Process\",\"Service\","
3650 "\"System\",\"Unknown\"]"
3651 "},"
3652 "\"required\":[\"origin\",\"nature\",\"scope\"]"
3653 "},"
3654 "\"typedValue\":{"
3655 "\"type\":\"object\","
3656 "\"oneOf\":["
3657 "{\"$ref\":\"#/definitions/typedValue/definitions/s32Value\"},"
3658 "{\"$ref\":\"#/definitions/typedValue/definitions/s64Value\"},"
3659 "{\"$ref\":\"#/definitions/typedValue/definitions/u32Value\"},"
3660 "{\"$ref\":\"#/definitions/typedValue/definitions/u64Value\"},"
3661 "{\"$ref\":\"#/definitions/typedValue/definitions/strValue\"}"
3662 "],"
3663 "\"definitions\":{"
3664 "\"s32Value\":{"
3665 "\"properties\":{"
3666 "\"type\":{"
3667 "\"type\":\"string\","
3668 "\"enum\":[\"s32\"]"
3669 "},"
3670 "\"value\":{"
3671 "\"type\":\"integer\","
3672 "\"minimum\":-2147483648,"
3673 "\"maximum\":2147483647"
3674 "}"
3675 "},"
3676 "\"required\":[\"type\",\"value\"]"
3677 "},"
3678 "\"s64Value\":{"
3679 "\"properties\":{"
3680 "\"type\":{"
3681 "\"type\":\"string\","
3682 "\"enum\":[\"s64\"]"
3683 "},"
3684 "\"value\":{"
3685 "\"type\":\"integer\","
3686 "\"minimum\":-9007199254740991,"
3687 "\"maximum\":9007199254740991"
3688 "}"
3689 "},"
3690 "\"required\":[\"type\",\"value\"]"
3691 "},"
3692 "\"u32Value\":{"
3693 "\"properties\":{"
3694 "\"type\":{"
3695 "\"type\":\"string\","
3696 "\"enum\":[\"u32\"]"
3697 "},"
3698 "\"value\":{"
3699 "\"type\":\"integer\","
3700 "\"minimum\":0,"
3701 "\"maximum\":4294967295"
3702 "}"
3703 "},"
3704 "\"required\":[\"type\",\"value\"]"
3705 "},"
3706 "\"u64Value\":{"
3707 "\"properties\":{"
3708 "\"type\":{"
3709 "\"type\":\"string\","
3710 "\"enum\":[\"u64\"]"
3711 "},"
3712 "\"value\":{"
3713 "\"type\":\"integer\","
3714 "\"minimum\":0,"
3715 "\"maximum\":9007199254740991"
3716 "}"
3717 "},"
3718 "\"required\":[\"type\",\"value\"]"
3719 "},"
3720 "\"strValue\":{"
3721 "\"properties\":{"
3722 "\"type\":{"
3723 "\"type\":\"string\","
3724 "\"enum\":[\"str\"]"
3725 "},"
3726 "\"value\":{\"type\":\"string\"}"
3727 "},"
3728 "\"required\":[\"type\",\"value\"]"
3729 "},"
3730 "\"unknownValue\":{"
3731 "\"properties\":{"
3732 "\"type\":{"
3733 "\"type\":\"integer\","
3734 "\"minimum\":0"
3735 "},"
3736 "\"value\":{"
3737 "\"type\":\"string\","
3738 "\"enum\":[\"unknown\"]"
3739 "}"
3740 "},"
3741 "\"required\":[\"type\",\"value\"]"
3742 "}"
3743 "}"
3744 "}"
3745 "}"
3746 "}");
3747
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003748 if (old_len == out->data) {
Simon Horman6f6bb382017-01-04 09:37:26 +01003749 chunk_reset(out);
3750 chunk_appendf(out,
3751 "{\"errorStr\":\"output buffer too short\"}");
3752 }
3753}
3754
3755/* This function dumps the schema onto the stream interface's read buffer.
3756 * It returns 0 as long as it does not complete, non-zero upon completion.
3757 * No state is used.
3758 */
3759static int stats_dump_json_schema_to_buffer(struct stream_interface *si)
3760{
3761 chunk_reset(&trash);
3762
3763 stats_dump_json_schema(&trash);
3764
Willy Tarreau06d80a92017-10-19 14:32:15 +02003765 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01003766 si_rx_room_blk(si);
Simon Horman6f6bb382017-01-04 09:37:26 +01003767 return 0;
3768 }
3769
3770 return 1;
3771}
3772
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02003773static int cli_parse_clear_counters(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau89d467c2016-11-23 11:02:40 +01003774{
3775 struct proxy *px;
3776 struct server *sv;
3777 struct listener *li;
3778 int clrall = 0;
3779
3780 if (strcmp(args[2], "all") == 0)
3781 clrall = 1;
3782
3783 /* check permissions */
3784 if (!cli_has_level(appctx, ACCESS_LVL_OPER) ||
3785 (clrall && !cli_has_level(appctx, ACCESS_LVL_ADMIN)))
3786 return 1;
3787
Olivier Houchardfbc74e82017-11-24 16:54:05 +01003788 for (px = proxies_list; px; px = px->next) {
Willy Tarreau89d467c2016-11-23 11:02:40 +01003789 if (clrall) {
3790 memset(&px->be_counters, 0, sizeof(px->be_counters));
3791 memset(&px->fe_counters, 0, sizeof(px->fe_counters));
3792 }
3793 else {
3794 px->be_counters.conn_max = 0;
3795 px->be_counters.p.http.rps_max = 0;
3796 px->be_counters.sps_max = 0;
3797 px->be_counters.cps_max = 0;
3798 px->be_counters.nbpend_max = 0;
Christopher Fauletefb41f02019-11-08 14:53:15 +01003799 px->be_counters.qtime_max = 0;
3800 px->be_counters.ctime_max = 0;
3801 px->be_counters.dtime_max = 0;
3802 px->be_counters.ttime_max = 0;
Willy Tarreau89d467c2016-11-23 11:02:40 +01003803
3804 px->fe_counters.conn_max = 0;
3805 px->fe_counters.p.http.rps_max = 0;
3806 px->fe_counters.sps_max = 0;
3807 px->fe_counters.cps_max = 0;
Willy Tarreau89d467c2016-11-23 11:02:40 +01003808 }
3809
3810 for (sv = px->srv; sv; sv = sv->next)
3811 if (clrall)
3812 memset(&sv->counters, 0, sizeof(sv->counters));
3813 else {
3814 sv->counters.cur_sess_max = 0;
3815 sv->counters.nbpend_max = 0;
3816 sv->counters.sps_max = 0;
Christopher Fauletefb41f02019-11-08 14:53:15 +01003817 sv->counters.qtime_max = 0;
3818 sv->counters.ctime_max = 0;
3819 sv->counters.dtime_max = 0;
3820 sv->counters.ttime_max = 0;
Willy Tarreau89d467c2016-11-23 11:02:40 +01003821 }
3822
3823 list_for_each_entry(li, &px->conf.listeners, by_fe)
3824 if (li->counters) {
3825 if (clrall)
3826 memset(li->counters, 0, sizeof(*li->counters));
3827 else
3828 li->counters->conn_max = 0;
3829 }
3830 }
3831
3832 global.cps_max = 0;
3833 global.sps_max = 0;
Olivier Houchard00bc3cb2017-10-17 19:23:25 +02003834 global.ssl_max = 0;
3835 global.ssl_fe_keys_max = 0;
3836 global.ssl_be_keys_max = 0;
Willy Tarreaud80cb4e2018-01-20 19:30:13 +01003837
3838 memset(activity, 0, sizeof(activity));
Willy Tarreau89d467c2016-11-23 11:02:40 +01003839 return 1;
3840}
3841
3842
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02003843static int cli_parse_show_info(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003844{
Willy Tarreau2f397382019-10-09 11:43:59 +02003845 int arg = 2;
3846
Willy Tarreaud25fc792016-12-16 12:33:47 +01003847 appctx->ctx.stats.scope_str = 0;
3848 appctx->ctx.stats.scope_len = 0;
3849 appctx->ctx.stats.flags = 0;
3850
Willy Tarreau2f397382019-10-09 11:43:59 +02003851 while (*args[arg]) {
3852 if (strcmp(args[arg], "typed") == 0)
3853 appctx->ctx.stats.flags = (appctx->ctx.stats.flags & ~STAT_FMT_MASK) | STAT_FMT_TYPED;
3854 else if (strcmp(args[arg], "json") == 0)
3855 appctx->ctx.stats.flags = (appctx->ctx.stats.flags & ~STAT_FMT_MASK) | STAT_FMT_JSON;
3856 else if (strcmp(args[arg], "desc") == 0)
3857 appctx->ctx.stats.flags |= STAT_SHOW_FDESC;
3858 arg++;
3859 }
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003860 return 0;
3861}
3862
3863
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02003864static int cli_parse_show_stat(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau2b812e22016-11-22 16:18:05 +01003865{
Willy Tarreau2f397382019-10-09 11:43:59 +02003866 int arg = 2;
3867
Willy Tarreaud25fc792016-12-16 12:33:47 +01003868 appctx->ctx.stats.scope_str = 0;
3869 appctx->ctx.stats.scope_len = 0;
Willy Tarreau578d6e42019-10-09 11:00:22 +02003870 appctx->ctx.stats.flags = STAT_SHNODE | STAT_SHDESC;
3871
3872 if ((strm_li(si_strm(appctx->owner))->bind_conf->level & ACCESS_LVL_MASK) >= ACCESS_LVL_OPER)
3873 appctx->ctx.stats.flags |= STAT_SHLGNDS;
Willy Tarreaud25fc792016-12-16 12:33:47 +01003874
Willy Tarreau2f397382019-10-09 11:43:59 +02003875 if (*args[arg] && *args[arg+1] && *args[arg+2]) {
Willy Tarreaua1b1ed52016-11-25 08:50:58 +01003876 struct proxy *px;
3877
Willy Tarreau2f397382019-10-09 11:43:59 +02003878 px = proxy_find_by_name(args[arg], 0, 0);
Willy Tarreaua1b1ed52016-11-25 08:50:58 +01003879 if (px)
3880 appctx->ctx.stats.iid = px->uuid;
3881 else
Willy Tarreau2f397382019-10-09 11:43:59 +02003882 appctx->ctx.stats.iid = atoi(args[arg]);
Willy Tarreaua1b1ed52016-11-25 08:50:58 +01003883
Willy Tarreau9d008692019-08-09 11:21:01 +02003884 if (!appctx->ctx.stats.iid)
3885 return cli_err(appctx, "No such proxy.\n");
Willy Tarreaua1b1ed52016-11-25 08:50:58 +01003886
Willy Tarreau2b812e22016-11-22 16:18:05 +01003887 appctx->ctx.stats.flags |= STAT_BOUND;
Willy Tarreau2f397382019-10-09 11:43:59 +02003888 appctx->ctx.stats.type = atoi(args[arg+1]);
3889 appctx->ctx.stats.sid = atoi(args[arg+2]);
3890 arg += 3;
3891 }
3892
3893 while (*args[arg]) {
3894 if (strcmp(args[arg], "typed") == 0)
3895 appctx->ctx.stats.flags = (appctx->ctx.stats.flags & ~STAT_FMT_MASK) | STAT_FMT_TYPED;
3896 else if (strcmp(args[arg], "json") == 0)
3897 appctx->ctx.stats.flags = (appctx->ctx.stats.flags & ~STAT_FMT_MASK) | STAT_FMT_JSON;
3898 else if (strcmp(args[arg], "desc") == 0)
3899 appctx->ctx.stats.flags |= STAT_SHOW_FDESC;
3900 arg++;
Willy Tarreau2b812e22016-11-22 16:18:05 +01003901 }
Willy Tarreau2b812e22016-11-22 16:18:05 +01003902
Willy Tarreau2b812e22016-11-22 16:18:05 +01003903 return 0;
3904}
3905
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003906static int cli_io_handler_dump_info(struct appctx *appctx)
3907{
3908 return stats_dump_info_to_buffer(appctx->owner);
3909}
3910
Willy Tarreau2b812e22016-11-22 16:18:05 +01003911/* This I/O handler runs as an applet embedded in a stream interface. It is
3912 * used to send raw stats over a socket.
3913 */
3914static int cli_io_handler_dump_stat(struct appctx *appctx)
3915{
Christopher Fauletef779222018-10-31 08:47:01 +01003916 return stats_dump_stat_to_buffer(appctx->owner, NULL, NULL);
Willy Tarreau2b812e22016-11-22 16:18:05 +01003917}
3918
Simon Horman6f6bb382017-01-04 09:37:26 +01003919static int cli_io_handler_dump_json_schema(struct appctx *appctx)
3920{
3921 return stats_dump_json_schema_to_buffer(appctx->owner);
3922}
3923
Willy Tarreau2b812e22016-11-22 16:18:05 +01003924/* register cli keywords */
3925static struct cli_kw_list cli_kws = {{ },{
Willy Tarreau89d467c2016-11-23 11:02:40 +01003926 { { "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 +02003927 { { "show", "info", NULL }, "show info : report information about the running process [desc|json|typed]*", cli_parse_show_info, cli_io_handler_dump_info, NULL },
3928 { { "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 +01003929 { { "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 +01003930 {{},}
3931}};
3932
Willy Tarreau0108d902018-11-25 19:14:37 +01003933INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
3934
William Lallemand74c24fb2016-11-21 17:18:36 +01003935struct applet http_stats_applet = {
3936 .obj_type = OBJ_TYPE_APPLET,
3937 .name = "<STATS>", /* used for logging */
3938 .fct = http_stats_io_handler,
3939 .release = NULL,
3940};
3941
William Lallemand74c24fb2016-11-21 17:18:36 +01003942/*
3943 * Local variables:
3944 * c-indent-level: 8
3945 * c-basic-offset: 8
3946 * End:
3947 */