blob: 08879256cb5c27b49bbca9d3b665d690973374c8 [file] [log] [blame]
William Lallemand74c24fb2016-11-21 17:18:36 +01001/*
2 * Functions dedicated to statistics output and the stats socket
3 *
4 * Copyright 2000-2012 Willy Tarreau <w@1wt.eu>
5 * Copyright 2007-2009 Krzysztof Piotr Oledzki <ole@ans.pl>
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version
10 * 2 of the License, or (at your option) any later version.
11 *
12 */
13
14#include <ctype.h>
15#include <errno.h>
16#include <fcntl.h>
17#include <stdio.h>
18#include <stdlib.h>
19#include <string.h>
20#include <pwd.h>
21#include <grp.h>
22
23#include <sys/socket.h>
24#include <sys/stat.h>
25#include <sys/types.h>
26
Willy Tarreau4c7e4b72020-05-27 12:58:42 +020027#include <haproxy/api.h>
William Lallemand74c24fb2016-11-21 17:18:36 +010028#include <common/cfgparse.h>
William Lallemand74c24fb2016-11-21 17:18:36 +010029#include <common/debug.h>
Willy Tarreau35b51c62018-09-10 15:38:55 +020030#include <common/http.h>
Willy Tarreaub96b77e2018-12-11 10:22:41 +010031#include <common/htx.h>
William Lallemand74c24fb2016-11-21 17:18:36 +010032#include <common/memory.h>
33#include <common/mini-clist.h>
34#include <common/standard.h>
35#include <common/ticks.h>
36#include <common/time.h>
37#include <common/uri_auth.h>
Willy Tarreaud6788052020-05-27 15:59:00 +020038#include <haproxy/version.h>
Willy Tarreau8d366972020-05-27 16:10:29 +020039#include <haproxy/base64.h>
William Lallemand74c24fb2016-11-21 17:18:36 +010040
41#include <types/applet.h>
Willy Tarreau2b812e22016-11-22 16:18:05 +010042#include <types/cli.h>
William Lallemand74c24fb2016-11-21 17:18:36 +010043#include <types/global.h>
44#include <types/dns.h>
William Lallemand9ed62032016-11-21 17:49:11 +010045#include <types/stats.h>
William Lallemand74c24fb2016-11-21 17:18:36 +010046
47#include <proto/backend.h>
48#include <proto/channel.h>
49#include <proto/checks.h>
Willy Tarreau2b812e22016-11-22 16:18:05 +010050#include <proto/cli.h>
William Lallemand74c24fb2016-11-21 17:18:36 +010051#include <proto/compression.h>
Baptiste Assmann333939c2019-01-21 08:34:50 +010052#include <proto/dns.h>
William Lallemand9ed62032016-11-21 17:49:11 +010053#include <proto/stats.h>
William Lallemand74c24fb2016-11-21 17:18:36 +010054#include <proto/fd.h>
55#include <proto/freq_ctr.h>
56#include <proto/frontend.h>
Christopher Fauleted7a0662019-01-14 11:07:34 +010057#include <proto/http_htx.h>
William Lallemand74c24fb2016-11-21 17:18:36 +010058#include <proto/log.h>
59#include <proto/pattern.h>
60#include <proto/pipe.h>
61#include <proto/listener.h>
62#include <proto/map.h>
William Lallemand74c24fb2016-11-21 17:18:36 +010063#include <proto/proxy.h>
64#include <proto/sample.h>
65#include <proto/session.h>
Willy Tarreauc125cef2019-05-10 09:58:43 +020066#include <proto/ssl_sock.h>
William Lallemand74c24fb2016-11-21 17:18:36 +010067#include <proto/stream.h>
68#include <proto/server.h>
69#include <proto/raw_sock.h>
70#include <proto/stream_interface.h>
71#include <proto/task.h>
72
Christopher Faulet2b9b6782019-02-27 16:42:58 +010073/* status codes available for the stats admin page (strictly 4 chars length) */
74const char *stat_status_codes[STAT_STATUS_SIZE] = {
75 [STAT_STATUS_DENY] = "DENY",
76 [STAT_STATUS_DONE] = "DONE",
77 [STAT_STATUS_ERRP] = "ERRP",
78 [STAT_STATUS_EXCD] = "EXCD",
79 [STAT_STATUS_NONE] = "NONE",
80 [STAT_STATUS_PART] = "PART",
81 [STAT_STATUS_UNKN] = "UNKN",
82 [STAT_STATUS_IVAL] = "IVAL",
83};
84
Willy Tarreau0baac8c2016-11-22 16:36:53 +010085/* These are the field names for each INF_* field position. Please pay attention
86 * to always use the exact same name except that the strings for new names must
87 * be lower case or CamelCase while the enum entries must be upper case.
88 */
Willy Tarreaueaa55372019-10-09 07:39:11 +020089const struct name_desc info_fields[INF_TOTAL_FIELDS] = {
Willy Tarreau6d4897e2019-10-11 16:31:46 +020090 [INF_NAME] = { .name = "Name", .desc = "Product name" },
91 [INF_VERSION] = { .name = "Version", .desc = "Product version" },
92 [INF_RELEASE_DATE] = { .name = "Release_date", .desc = "Date of latest source code update" },
93 [INF_NBTHREAD] = { .name = "Nbthread", .desc = "Number of started threads (global.nbthread)" },
94 [INF_NBPROC] = { .name = "Nbproc", .desc = "Number of started worker processes (global.nbproc)" },
95 [INF_PROCESS_NUM] = { .name = "Process_num", .desc = "Relative worker process number (1..Nbproc)" },
96 [INF_PID] = { .name = "Pid", .desc = "This worker process identifier for the system" },
97 [INF_UPTIME] = { .name = "Uptime", .desc = "How long ago this worker process was started (days+hours+minutes+seconds)" },
98 [INF_UPTIME_SEC] = { .name = "Uptime_sec", .desc = "How long ago this worker process was started (seconds)" },
99 [INF_MEMMAX_MB] = { .name = "Memmax_MB", .desc = "Worker process's hard limit on memory usage in MB (-m on command line)" },
100 [INF_POOL_ALLOC_MB] = { .name = "PoolAlloc_MB", .desc = "Amount of memory allocated in pools (in MB)" },
101 [INF_POOL_USED_MB] = { .name = "PoolUsed_MB", .desc = "Amount of pool memory currently used (in MB)" },
102 [INF_POOL_FAILED] = { .name = "PoolFailed", .desc = "Number of failed pool allocations since this worker was started" },
103 [INF_ULIMIT_N] = { .name = "Ulimit-n", .desc = "Hard limit on the number of per-process file descriptors" },
104 [INF_MAXSOCK] = { .name = "Maxsock", .desc = "Hard limit on the number of per-process sockets" },
105 [INF_MAXCONN] = { .name = "Maxconn", .desc = "Hard limit on the number of per-process connections (configured or imposed by Ulimit-n)" },
106 [INF_HARD_MAXCONN] = { .name = "Hard_maxconn", .desc = "Hard limit on the number of per-process connections (imposed by Memmax_MB or Ulimit-n)" },
107 [INF_CURR_CONN] = { .name = "CurrConns", .desc = "Current number of connections on this worker process" },
108 [INF_CUM_CONN] = { .name = "CumConns", .desc = "Total number of connections on this worker process since started" },
109 [INF_CUM_REQ] = { .name = "CumReq", .desc = "Total number of requests on this worker process since started" },
110 [INF_MAX_SSL_CONNS] = { .name = "MaxSslConns", .desc = "Hard limit on the number of per-process SSL endpoints (front+back), 0=unlimited" },
111 [INF_CURR_SSL_CONNS] = { .name = "CurrSslConns", .desc = "Current number of SSL endpoints on this worker process (front+back)" },
112 [INF_CUM_SSL_CONNS] = { .name = "CumSslConns", .desc = "Total number of SSL endpoints on this worker process since started (front+back)" },
113 [INF_MAXPIPES] = { .name = "Maxpipes", .desc = "Hard limit on the number of pipes for splicing, 0=unlimited" },
114 [INF_PIPES_USED] = { .name = "PipesUsed", .desc = "Current number of pipes in use in this worker process" },
115 [INF_PIPES_FREE] = { .name = "PipesFree", .desc = "Current number of allocated and available pipes in this worker process" },
116 [INF_CONN_RATE] = { .name = "ConnRate", .desc = "Number of front connections created on this worker process over the last second" },
117 [INF_CONN_RATE_LIMIT] = { .name = "ConnRateLimit", .desc = "Hard limit for ConnRate (global.maxconnrate)" },
118 [INF_MAX_CONN_RATE] = { .name = "MaxConnRate", .desc = "Highest ConnRate reached on this worker process since started (in connections per second)" },
119 [INF_SESS_RATE] = { .name = "SessRate", .desc = "Number of sessions created on this worker process over the last second" },
120 [INF_SESS_RATE_LIMIT] = { .name = "SessRateLimit", .desc = "Hard limit for SessRate (global.maxsessrate)" },
121 [INF_MAX_SESS_RATE] = { .name = "MaxSessRate", .desc = "Highest SessRate reached on this worker process since started (in sessions per second)" },
122 [INF_SSL_RATE] = { .name = "SslRate", .desc = "Number of SSL connections created on this worker process over the last second" },
123 [INF_SSL_RATE_LIMIT] = { .name = "SslRateLimit", .desc = "Hard limit for SslRate (global.maxsslrate)" },
124 [INF_MAX_SSL_RATE] = { .name = "MaxSslRate", .desc = "Highest SslRate reached on this worker process since started (in connections per second)" },
125 [INF_SSL_FRONTEND_KEY_RATE] = { .name = "SslFrontendKeyRate", .desc = "Number of SSL keys created on frontends in this worker process over the last second" },
126 [INF_SSL_FRONTEND_MAX_KEY_RATE] = { .name = "SslFrontendMaxKeyRate", .desc = "Highest SslFrontendKeyRate reached on this worker process since started (in SSL keys per second)" },
127 [INF_SSL_FRONTEND_SESSION_REUSE_PCT] = { .name = "SslFrontendSessionReuse_pct", .desc = "Percent of frontend SSL connections which did not require a new key" },
128 [INF_SSL_BACKEND_KEY_RATE] = { .name = "SslBackendKeyRate", .desc = "Number of SSL keys created on backends in this worker process over the last second" },
129 [INF_SSL_BACKEND_MAX_KEY_RATE] = { .name = "SslBackendMaxKeyRate", .desc = "Highest SslBackendKeyRate reached on this worker process since started (in SSL keys per second)" },
130 [INF_SSL_CACHE_LOOKUPS] = { .name = "SslCacheLookups", .desc = "Total number of SSL session ID lookups in the SSL session cache on this worker since started" },
131 [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" },
132 [INF_COMPRESS_BPS_IN] = { .name = "CompressBpsIn", .desc = "Number of bytes submitted to the HTTP compressor in this worker process over the last second" },
133 [INF_COMPRESS_BPS_OUT] = { .name = "CompressBpsOut", .desc = "Number of bytes emitted by the HTTP compressor in this worker process over the last second" },
134 [INF_COMPRESS_BPS_RATE_LIM] = { .name = "CompressBpsRateLim", .desc = "Limit of CompressBpsOut beyond which HTTP compression is automatically disabled" },
135 [INF_ZLIB_MEM_USAGE] = { .name = "ZlibMemUsage", .desc = "Amount of memory currently used by HTTP compression on the current worker process (in bytes)" },
136 [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)" },
137 [INF_TASKS] = { .name = "Tasks", .desc = "Total number of tasks in the current worker process (active + sleeping)" },
138 [INF_RUN_QUEUE] = { .name = "Run_queue", .desc = "Total number of active tasks+tasklets in the current worker process" },
139 [INF_IDLE_PCT] = { .name = "Idle_pct", .desc = "Percentage of last second spent waiting in the current worker thread" },
140 [INF_NODE] = { .name = "node", .desc = "Node name (global.node)" },
141 [INF_DESCRIPTION] = { .name = "description", .desc = "Node description (global.description)" },
142 [INF_STOPPING] = { .name = "Stopping", .desc = "1 if the worker process is currently stopping, otherwise zero" },
143 [INF_JOBS] = { .name = "Jobs", .desc = "Current number of active jobs on the current worker process (frontend connections, master connections, listeners)" },
144 [INF_UNSTOPPABLE_JOBS] = { .name = "Unstoppable Jobs", .desc = "Current number of unstoppable jobs on the current worker process (master connections)" },
145 [INF_LISTENERS] = { .name = "Listeners", .desc = "Current number of active listeners on the current worker process" },
146 [INF_ACTIVE_PEERS] = { .name = "ActivePeers", .desc = "Current number of verified active peers connections on the current worker process" },
147 [INF_CONNECTED_PEERS] = { .name = "ConnectedPeers", .desc = "Current number of peers having passed the connection step on the current worker process" },
148 [INF_DROPPED_LOGS] = { .name = "DroppedLogs", .desc = "Total number of dropped logs for current worker process since started" },
149 [INF_BUSY_POLLING] = { .name = "BusyPolling", .desc = "1 if busy-polling is currently in use on the worker process, otherwise zero (config.busy-polling)" },
150 [INF_FAILED_RESOLUTIONS] = { .name = "FailedResolutions", .desc = "Total number of failed DNS resolutions in current worker process since started" },
151 [INF_TOTAL_BYTES_OUT] = { .name = "TotalBytesOut", .desc = "Total number of bytes emitted by current worker process since started" },
152 [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 +0200153 [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 +0100154};
155
Willy Tarreaueaa55372019-10-09 07:39:11 +0200156const struct name_desc stat_fields[ST_F_TOTAL_FIELDS] = {
Willy Tarreau6d4897e2019-10-11 16:31:46 +0200157 [ST_F_PXNAME] = { .name = "pxname", .desc = "Proxy name" },
158 [ST_F_SVNAME] = { .name = "svname", .desc = "Server name" },
159 [ST_F_QCUR] = { .name = "qcur", .desc = "Current number of connections waiting in the server of backend queue" },
160 [ST_F_QMAX] = { .name = "qmax", .desc = "Highest value of qcur encountered since process started" },
161 [ST_F_SCUR] = { .name = "scur", .desc = "Current number of sessions on the frontend, backend or server" },
162 [ST_F_SMAX] = { .name = "smax", .desc = "Highest value of scur encountered since process started" },
163 [ST_F_SLIM] = { .name = "slim", .desc = "Frontend/listener/server's maxconn, backend's fullconn" },
164 [ST_F_STOT] = { .name = "stot", .desc = "Total number of sessions since process started" },
165 [ST_F_BIN] = { .name = "bin", .desc = "Total number of request bytes since process started" },
166 [ST_F_BOUT] = { .name = "bout", .desc = "Total number of response bytes since process started" },
167 [ST_F_DREQ] = { .name = "dreq", .desc = "Total number of denied requests since process started" },
168 [ST_F_DRESP] = { .name = "dresp", .desc = "Total number of denied responses since process started" },
169 [ST_F_EREQ] = { .name = "ereq", .desc = "Total number of invalid requests since process started" },
170 [ST_F_ECON] = { .name = "econ", .desc = "Total number of failed connections to server since the worker process started" },
171 [ST_F_ERESP] = { .name = "eresp", .desc = "Total number of invalid responses since the worker process started" },
172 [ST_F_WRETR] = { .name = "wretr", .desc = "Total number of server connection retries since the worker process started" },
173 [ST_F_WREDIS] = { .name = "wredis", .desc = "Total number of server redispatches due to connection failures since the worker process started" },
174 [ST_F_STATUS] = { .name = "status", .desc = "Frontend/listen status: OPEN/WAITING/FULL/STOP; backend: UP/DOWN; server: last check status" },
175 [ST_F_WEIGHT] = { .name = "weight", .desc = "Server weight, or sum of active servers' weights for a backend" },
176 [ST_F_ACT] = { .name = "act", .desc = "Total number of active UP servers with a non-zero weight" },
177 [ST_F_BCK] = { .name = "bck", .desc = "Total number of backup UP servers with a non-zero weight" },
178 [ST_F_CHKFAIL] = { .name = "chkfail", .desc = "Total number of failed individual health checks per server/backend, since the worker process started" },
179 [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" },
180 [ST_F_LASTCHG] = { .name = "lastchg", .desc = "How long ago the last server state changed, in seconds" },
181 [ST_F_DOWNTIME] = { .name = "downtime", .desc = "Total time spent in DOWN state, for server or backend" },
182 [ST_F_QLIMIT] = { .name = "qlimit", .desc = "Limit on the number of connections in queue, for servers only (maxqueue argument)" },
183 [ST_F_PID] = { .name = "pid", .desc = "Relative worker process number (1..nbproc)" },
184 [ST_F_IID] = { .name = "iid", .desc = "Frontend or Backend numeric identifier ('id' setting)" },
185 [ST_F_SID] = { .name = "sid", .desc = "Server numeric identifier ('id' setting)" },
186 [ST_F_THROTTLE] = { .name = "throttle", .desc = "Throttling ratio applied to a server's maxconn and weight during the slowstart period (0 to 100%)" },
187 [ST_F_LBTOT] = { .name = "lbtot", .desc = "Total number of requests routed by load balancing since the worker process started (ignores queue pop and stickiness)" },
188 [ST_F_TRACKED] = { .name = "tracked", .desc = "Name of the other server this server tracks for its state" },
189 [ST_F_TYPE] = { .name = "type", .desc = "Type of the object (Listener, Frontend, Backend, Server)" },
190 [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)" },
191 [ST_F_RATE_LIM] = { .name = "rate_lim", .desc = "Limit on the number of sessions accepted in a second (frontend only, 'rate-limit sessions' setting)" },
192 [ST_F_RATE_MAX] = { .name = "rate_max", .desc = "Highest value of 'rate' observed since the worker process started" },
193 [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" },
194 [ST_F_CHECK_CODE] = { .name = "check_code", .desc = "HTTP/SMTP/LDAP status code reported by the latest server health check" },
195 [ST_F_CHECK_DURATION] = { .name = "check_duration", .desc = "Total duration of the latest server health check, in milliseconds" },
196 [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" },
197 [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" },
198 [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" },
199 [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" },
200 [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" },
201 [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)" },
202 [ST_F_HANAFAIL] = { .name = "hanafail", .desc = "Total number of failed checks caused by an 'on-error' directive after an 'observe' condition matched" },
203 [ST_F_REQ_RATE] = { .name = "req_rate", .desc = "Number of HTTP requests processed over the last second on this object" },
204 [ST_F_REQ_RATE_MAX] = { .name = "req_rate_max", .desc = "Highest value of 'req_rate' observed since the worker process started" },
205 [ST_F_REQ_TOT] = { .name = "req_tot", .desc = "Total number of HTTP requests processed by this object since the worker process started" },
206 [ST_F_CLI_ABRT] = { .name = "cli_abrt", .desc = "Total number of requests or connections aborted by the client since the worker process started" },
207 [ST_F_SRV_ABRT] = { .name = "srv_abrt", .desc = "Total number of requests or connections aborted by the server since the worker process started" },
208 [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" },
209 [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" },
210 [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)" },
211 [ST_F_COMP_RSP] = { .name = "comp_rsp", .desc = "Total number of HTTP responses that were compressed for this object since the worker process started" },
212 [ST_F_LASTSESS] = { .name = "lastsess", .desc = "How long ago some traffic was seen on this object on this worker process, in seconds" },
213 [ST_F_LAST_CHK] = { .name = "last_chk", .desc = "Short description of the latest health check report for this server (see also check_desc)" },
214 [ST_F_LAST_AGT] = { .name = "last_agt", .desc = "Short description of the latest agent check report for this server (see also agent_desc)" },
215 [ST_F_QTIME] = { .name = "qtime", .desc = "Time spent in the queue, in milliseconds, averaged over the 1024 last requests (backend/server)" },
216 [ST_F_CTIME] = { .name = "ctime", .desc = "Time spent waiting for a connection to complete, in milliseconds, averaged over the 1024 last requests (backend/server)" },
217 [ST_F_RTIME] = { .name = "rtime", .desc = "Time spent waiting for a server response, in milliseconds, averaged over the 1024 last requests (backend/server)" },
218 [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)" },
219 [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" },
220 [ST_F_AGENT_CODE] = { .name = "agent_code", .desc = "Status code reported by the latest server agent check" },
221 [ST_F_AGENT_DURATION] = { .name = "agent_duration", .desc = "Total duration of the latest server agent check, in milliseconds" },
222 [ST_F_CHECK_DESC] = { .name = "check_desc", .desc = "Textual description of the latest health check report for this server" },
223 [ST_F_AGENT_DESC] = { .name = "agent_desc", .desc = "Textual description of the latest agent check report for this server" },
224 [ST_F_CHECK_RISE] = { .name = "check_rise", .desc = "Number of successful health checks before declaring a server UP (server 'rise' setting)" },
225 [ST_F_CHECK_FALL] = { .name = "check_fall", .desc = "Number of failed health checks before declaring a server DOWN (server 'fall' setting)" },
226 [ST_F_CHECK_HEALTH] = { .name = "check_health", .desc = "Current server health check level (0..fall-1=DOWN, fall..rise-1=UP)" },
227 [ST_F_AGENT_RISE] = { .name = "agent_rise", .desc = "Number of successful agent checks before declaring a server UP (server 'rise' setting)" },
228 [ST_F_AGENT_FALL] = { .name = "agent_fall", .desc = "Number of failed agent checks before declaring a server DOWN (server 'fall' setting)" },
229 [ST_F_AGENT_HEALTH] = { .name = "agent_health", .desc = "Current server agent check level (0..fall-1=DOWN, fall..rise-1=UP)" },
230 [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" },
231 [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" },
232 [ST_F_MODE] = { .name = "mode", .desc = "'mode' setting (tcp/http/health/cli)" },
233 [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" },
234 [ST_F_CONN_RATE] = { .name = "conn_rate", .desc = "Number of new connections accepted over the last second on the frontend for this worker process" },
235 [ST_F_CONN_RATE_MAX] = { .name = "conn_rate_max", .desc = "Highest value of 'conn_rate' observed since the worker process started" },
236 [ST_F_CONN_TOT] = { .name = "conn_tot", .desc = "Total number of new connections accepted on this frontend since the worker process started" },
237 [ST_F_INTERCEPTED] = { .name = "intercepted", .desc = "Total number of HTTP requests intercepted on the frontend (redirects/stats/services) since the worker process started" },
238 [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" },
239 [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" },
240 [ST_F_WREW] = { .name = "wrew", .desc = "Total number of failed HTTP header rewrites since the worker process started" },
241 [ST_F_CONNECT] = { .name = "connect", .desc = "Total number of outgoing connection attempts on this backend/server since the worker process started" },
242 [ST_F_REUSE] = { .name = "reuse", .desc = "Total number of reused connection on this backend/server since the worker process started" },
243 [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" },
244 [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" },
245 [ST_F_SRV_ICUR] = { .name = "srv_icur", .desc = "Current number of idle connections available for reuse on this server" },
246 [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 +0100247 [ST_F_QT_MAX] = { .name = "qtime_max", .desc = "Maximum observed time spent in the queue, in milliseconds (backend/server)" },
248 [ST_F_CT_MAX] = { .name = "ctime_max", .desc = "Maximum observed time spent waiting for a connection to complete, in milliseconds (backend/server)" },
249 [ST_F_RT_MAX] = { .name = "rtime_max", .desc = "Maximum observed time spent waiting for a server response, in milliseconds (backend/server)" },
250 [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 +0100251 [ST_F_EINT] = { .name = "eint", .desc = "Total number of internal errors since process started"},
William Lallemand74c24fb2016-11-21 17:18:36 +0100252};
253
Willy Tarreau0baac8c2016-11-22 16:36:53 +0100254/* one line of info */
Christopher Faulet1bc04c72017-10-29 20:14:08 +0100255static THREAD_LOCAL struct field info[INF_TOTAL_FIELDS];
William Lallemand74c24fb2016-11-21 17:18:36 +0100256/* one line of stats */
Christopher Faulet1bc04c72017-10-29 20:14:08 +0100257static THREAD_LOCAL struct field stats[ST_F_TOTAL_FIELDS];
William Lallemand74c24fb2016-11-21 17:18:36 +0100258
Christopher Faulet6338a082019-09-09 15:50:54 +0200259static void stats_dump_json_schema(struct buffer *out);
William Lallemand74c24fb2016-11-21 17:18:36 +0100260
Christopher Fauletef779222018-10-31 08:47:01 +0100261static int stats_putchk(struct channel *chn, struct htx *htx, struct buffer *chk)
262{
263 if (htx) {
Christopher Faulet69fc88c2019-01-07 14:27:53 +0100264 if (chk->data >= channel_htx_recv_max(chn, htx))
265 return 0;
Willy Tarreau0a7ef022019-05-28 10:30:11 +0200266 if (!htx_add_data_atonce(htx, ist2(chk->area, chk->data)))
Christopher Fauletef779222018-10-31 08:47:01 +0100267 return 0;
Christopher Faulet5adbeeb2019-01-02 14:34:39 +0100268 channel_add_input(chn, chk->data);
Christopher Fauletef779222018-10-31 08:47:01 +0100269 chk->data = 0;
Christopher Fauletef779222018-10-31 08:47:01 +0100270 }
271 else {
272 if (ci_putchk(chn, chk) == -1)
273 return 0;
274 }
275 return 1;
276}
Willy Tarreau0baac8c2016-11-22 16:36:53 +0100277
Christopher Fauleted7a0662019-01-14 11:07:34 +0100278static const char *stats_scope_ptr(struct appctx *appctx, struct stream_interface *si)
279{
Christopher Fauletb7f88902019-07-15 21:56:43 +0200280 struct channel *req = si_oc(si);
281 struct htx *htx = htxbuf(&req->buf);
282 struct htx_blk *blk;
283 struct ist uri;
Christopher Fauleted7a0662019-01-14 11:07:34 +0100284
Christopher Fauletb7f88902019-07-15 21:56:43 +0200285 blk = htx_get_head_blk(htx);
Christopher Fauletea009732019-11-18 15:50:25 +0100286 BUG_ON(!blk || htx_get_blk_type(blk) != HTX_BLK_REQ_SL);
Christopher Fauletb7f88902019-07-15 21:56:43 +0200287 ALREADY_CHECKED(blk);
288 uri = htx_sl_req_uri(htx_get_blk_ptr(htx, blk));
289 return uri.ptr + appctx->ctx.stats.scope_str;
Christopher Fauleted7a0662019-01-14 11:07:34 +0100290}
291
William Lallemand74c24fb2016-11-21 17:18:36 +0100292/*
293 * http_stats_io_handler()
294 * -> stats_dump_stat_to_buffer() // same as above, but used for CSV or HTML
295 * -> stats_dump_csv_header() // emits the CSV headers (same as above)
Simon Horman05ee2132017-01-04 09:37:25 +0100296 * -> stats_dump_json_header() // emits the JSON headers (same as above)
William Lallemand74c24fb2016-11-21 17:18:36 +0100297 * -> stats_dump_html_head() // emits the HTML headers
298 * -> stats_dump_html_info() // emits the equivalent of "show info" at the top
299 * -> stats_dump_proxy_to_buffer() // same as above, valid for CSV and HTML
300 * -> stats_dump_html_px_hdr()
301 * -> stats_dump_fe_stats()
302 * -> stats_dump_li_stats()
303 * -> stats_dump_sv_stats()
304 * -> stats_dump_be_stats()
305 * -> stats_dump_html_px_end()
306 * -> stats_dump_html_end() // emits HTML trailer
Simon Horman05ee2132017-01-04 09:37:25 +0100307 * -> stats_dump_json_end() // emits JSON trailer
William Lallemand74c24fb2016-11-21 17:18:36 +0100308 */
309
310
William Lallemand74c24fb2016-11-21 17:18:36 +0100311/* Dumps the stats CSV header to the trash buffer which. The caller is responsible
312 * for clearing it if needed.
313 * NOTE: Some tools happen to rely on the field position instead of its name,
314 * so please only append new fields at the end, never in the middle.
315 */
316static void stats_dump_csv_header()
317{
318 int field;
319
320 chunk_appendf(&trash, "# ");
321 for (field = 0; field < ST_F_TOTAL_FIELDS; field++)
Willy Tarreaueaa55372019-10-09 07:39:11 +0200322 chunk_appendf(&trash, "%s,", stat_fields[field].name);
William Lallemand74c24fb2016-11-21 17:18:36 +0100323
324 chunk_appendf(&trash, "\n");
325}
326
327
328/* Emits a stats field without any surrounding element and properly encoded to
329 * resist CSV output. Returns non-zero on success, 0 if the buffer is full.
330 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200331int stats_emit_raw_data_field(struct buffer *out, const struct field *f)
William Lallemand74c24fb2016-11-21 17:18:36 +0100332{
333 switch (field_format(f, 0)) {
334 case FF_EMPTY: return 1;
335 case FF_S32: return chunk_appendf(out, "%d", f->u.s32);
336 case FF_U32: return chunk_appendf(out, "%u", f->u.u32);
337 case FF_S64: return chunk_appendf(out, "%lld", (long long)f->u.s64);
338 case FF_U64: return chunk_appendf(out, "%llu", (unsigned long long)f->u.u64);
Christopher Faulet88a0db22019-09-24 16:35:10 +0200339 case FF_FLT: return chunk_appendf(out, "%f", f->u.flt);
William Lallemand74c24fb2016-11-21 17:18:36 +0100340 case FF_STR: return csv_enc_append(field_str(f, 0), 1, out) != NULL;
341 default: return chunk_appendf(out, "[INCORRECT_FIELD_TYPE_%08x]", f->type);
342 }
343}
344
345/* Emits a stats field prefixed with its type. No CSV encoding is prepared, the
346 * output is supposed to be used on its own line. Returns non-zero on success, 0
347 * if the buffer is full.
348 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200349int stats_emit_typed_data_field(struct buffer *out, const struct field *f)
William Lallemand74c24fb2016-11-21 17:18:36 +0100350{
351 switch (field_format(f, 0)) {
352 case FF_EMPTY: return 1;
353 case FF_S32: return chunk_appendf(out, "s32:%d", f->u.s32);
354 case FF_U32: return chunk_appendf(out, "u32:%u", f->u.u32);
355 case FF_S64: return chunk_appendf(out, "s64:%lld", (long long)f->u.s64);
356 case FF_U64: return chunk_appendf(out, "u64:%llu", (unsigned long long)f->u.u64);
Christopher Faulet88a0db22019-09-24 16:35:10 +0200357 case FF_FLT: return chunk_appendf(out, "flt:%f", f->u.flt);
William Lallemand74c24fb2016-11-21 17:18:36 +0100358 case FF_STR: return chunk_appendf(out, "str:%s", field_str(f, 0));
359 default: return chunk_appendf(out, "%08x:?", f->type);
360 }
361}
362
Simon Horman05ee2132017-01-04 09:37:25 +0100363/* Limit JSON integer values to the range [-(2**53)+1, (2**53)-1] as per
364 * the recommendation for interoperable integers in section 6 of RFC 7159.
365 */
366#define JSON_INT_MAX ((1ULL << 53) - 1)
367#define JSON_INT_MIN (0 - JSON_INT_MAX)
368
369/* Emits a stats field value and its type in JSON.
370 * Returns non-zero on success, 0 on error.
371 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200372int stats_emit_json_data_field(struct buffer *out, const struct field *f)
Simon Horman05ee2132017-01-04 09:37:25 +0100373{
374 int old_len;
375 char buf[20];
376 const char *type, *value = buf, *quote = "";
377
378 switch (field_format(f, 0)) {
379 case FF_EMPTY: return 1;
380 case FF_S32: type = "\"s32\"";
381 snprintf(buf, sizeof(buf), "%d", f->u.s32);
382 break;
383 case FF_U32: type = "\"u32\"";
384 snprintf(buf, sizeof(buf), "%u", f->u.u32);
385 break;
386 case FF_S64: type = "\"s64\"";
387 if (f->u.s64 < JSON_INT_MIN || f->u.s64 > JSON_INT_MAX)
388 return 0;
389 type = "\"s64\"";
390 snprintf(buf, sizeof(buf), "%lld", (long long)f->u.s64);
391 break;
392 case FF_U64: if (f->u.u64 > JSON_INT_MAX)
393 return 0;
394 type = "\"u64\"";
395 snprintf(buf, sizeof(buf), "%llu",
396 (unsigned long long) f->u.u64);
Christopher Faulet52c91bb2019-09-28 10:37:31 +0200397 break;
Christopher Faulet88a0db22019-09-24 16:35:10 +0200398 case FF_FLT: type = "\"flt\"";
399 snprintf(buf, sizeof(buf), "%f", f->u.flt);
Simon Horman05ee2132017-01-04 09:37:25 +0100400 break;
401 case FF_STR: type = "\"str\"";
402 value = field_str(f, 0);
403 quote = "\"";
404 break;
405 default: snprintf(buf, sizeof(buf), "%u", f->type);
406 type = buf;
407 value = "unknown";
408 quote = "\"";
409 break;
410 }
411
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200412 old_len = out->data;
Simon Horman05ee2132017-01-04 09:37:25 +0100413 chunk_appendf(out, ",\"value\":{\"type\":%s,\"value\":%s%s%s}",
414 type, quote, value, quote);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200415 return !(old_len == out->data);
Simon Horman05ee2132017-01-04 09:37:25 +0100416}
417
William Lallemand74c24fb2016-11-21 17:18:36 +0100418/* Emits an encoding of the field type on 3 characters followed by a delimiter.
419 * Returns non-zero on success, 0 if the buffer is full.
420 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200421int stats_emit_field_tags(struct buffer *out, const struct field *f,
422 char delim)
William Lallemand74c24fb2016-11-21 17:18:36 +0100423{
424 char origin, nature, scope;
425
426 switch (field_origin(f, 0)) {
427 case FO_METRIC: origin = 'M'; break;
428 case FO_STATUS: origin = 'S'; break;
429 case FO_KEY: origin = 'K'; break;
430 case FO_CONFIG: origin = 'C'; break;
431 case FO_PRODUCT: origin = 'P'; break;
432 default: origin = '?'; break;
433 }
434
435 switch (field_nature(f, 0)) {
436 case FN_GAUGE: nature = 'G'; break;
437 case FN_LIMIT: nature = 'L'; break;
438 case FN_MIN: nature = 'm'; break;
439 case FN_MAX: nature = 'M'; break;
440 case FN_RATE: nature = 'R'; break;
441 case FN_COUNTER: nature = 'C'; break;
442 case FN_DURATION: nature = 'D'; break;
443 case FN_AGE: nature = 'A'; break;
444 case FN_TIME: nature = 'T'; break;
445 case FN_NAME: nature = 'N'; break;
446 case FN_OUTPUT: nature = 'O'; break;
447 case FN_AVG: nature = 'a'; break;
448 default: nature = '?'; break;
449 }
450
451 switch (field_scope(f, 0)) {
452 case FS_PROCESS: scope = 'P'; break;
453 case FS_SERVICE: scope = 'S'; break;
454 case FS_SYSTEM: scope = 's'; break;
455 case FS_CLUSTER: scope = 'C'; break;
456 default: scope = '?'; break;
457 }
458
459 return chunk_appendf(out, "%c%c%c%c", origin, nature, scope, delim);
460}
461
Simon Horman05ee2132017-01-04 09:37:25 +0100462/* Emits an encoding of the field type as JSON.
463 * Returns non-zero on success, 0 if the buffer is full.
464 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200465int stats_emit_json_field_tags(struct buffer *out, const struct field *f)
Simon Horman05ee2132017-01-04 09:37:25 +0100466{
467 const char *origin, *nature, *scope;
468 int old_len;
469
470 switch (field_origin(f, 0)) {
471 case FO_METRIC: origin = "Metric"; break;
472 case FO_STATUS: origin = "Status"; break;
473 case FO_KEY: origin = "Key"; break;
474 case FO_CONFIG: origin = "Config"; break;
475 case FO_PRODUCT: origin = "Product"; break;
476 default: origin = "Unknown"; break;
477 }
478
479 switch (field_nature(f, 0)) {
480 case FN_GAUGE: nature = "Gauge"; break;
481 case FN_LIMIT: nature = "Limit"; break;
482 case FN_MIN: nature = "Min"; break;
483 case FN_MAX: nature = "Max"; break;
484 case FN_RATE: nature = "Rate"; break;
485 case FN_COUNTER: nature = "Counter"; break;
486 case FN_DURATION: nature = "Duration"; break;
487 case FN_AGE: nature = "Age"; break;
488 case FN_TIME: nature = "Time"; break;
489 case FN_NAME: nature = "Name"; break;
490 case FN_OUTPUT: nature = "Output"; break;
491 case FN_AVG: nature = "Avg"; break;
492 default: nature = "Unknown"; break;
493 }
494
495 switch (field_scope(f, 0)) {
496 case FS_PROCESS: scope = "Process"; break;
497 case FS_SERVICE: scope = "Service"; break;
498 case FS_SYSTEM: scope = "System"; break;
499 case FS_CLUSTER: scope = "Cluster"; break;
500 default: scope = "Unknown"; break;
501 }
502
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200503 old_len = out->data;
Simon Horman05ee2132017-01-04 09:37:25 +0100504 chunk_appendf(out, "\"tags\":{"
505 "\"origin\":\"%s\","
506 "\"nature\":\"%s\","
507 "\"scope\":\"%s\""
508 "}", origin, nature, scope);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200509 return !(old_len == out->data);
Simon Horman05ee2132017-01-04 09:37:25 +0100510}
William Lallemand74c24fb2016-11-21 17:18:36 +0100511
512/* Dump all fields from <stats> into <out> using CSV format */
Willy Tarreau83061a82018-07-13 11:56:34 +0200513static int stats_dump_fields_csv(struct buffer *out,
Willy Tarreau43241ff2019-10-09 11:27:51 +0200514 const struct field *stats, unsigned int flags)
William Lallemand74c24fb2016-11-21 17:18:36 +0100515{
516 int field;
517
518 for (field = 0; field < ST_F_TOTAL_FIELDS; field++) {
519 if (!stats_emit_raw_data_field(out, &stats[field]))
520 return 0;
521 if (!chunk_strcat(out, ","))
522 return 0;
523 }
524 chunk_strcat(out, "\n");
525 return 1;
526}
527
528/* Dump all fields from <stats> into <out> using a typed "field:desc:type:value" format */
Willy Tarreau83061a82018-07-13 11:56:34 +0200529static int stats_dump_fields_typed(struct buffer *out,
Willy Tarreau43241ff2019-10-09 11:27:51 +0200530 const struct field *stats, unsigned int flags)
William Lallemand74c24fb2016-11-21 17:18:36 +0100531{
532 int field;
533
534 for (field = 0; field < ST_F_TOTAL_FIELDS; field++) {
535 if (!stats[field].type)
536 continue;
537
538 chunk_appendf(out, "%c.%u.%u.%d.%s.%u:",
539 stats[ST_F_TYPE].u.u32 == STATS_TYPE_FE ? 'F' :
540 stats[ST_F_TYPE].u.u32 == STATS_TYPE_BE ? 'B' :
541 stats[ST_F_TYPE].u.u32 == STATS_TYPE_SO ? 'L' :
542 stats[ST_F_TYPE].u.u32 == STATS_TYPE_SV ? 'S' :
543 '?',
544 stats[ST_F_IID].u.u32, stats[ST_F_SID].u.u32,
Willy Tarreaueaa55372019-10-09 07:39:11 +0200545 field, stat_fields[field].name, stats[ST_F_PID].u.u32);
William Lallemand74c24fb2016-11-21 17:18:36 +0100546
547 if (!stats_emit_field_tags(out, &stats[field], ':'))
548 return 0;
549 if (!stats_emit_typed_data_field(out, &stats[field]))
550 return 0;
Willy Tarreau6b19b142019-10-09 15:44:21 +0200551 if ((flags & STAT_SHOW_FDESC) && !chunk_appendf(out, ":\"%s\"", stat_fields[field].desc))
552 return 0;
William Lallemand74c24fb2016-11-21 17:18:36 +0100553 if (!chunk_strcat(out, "\n"))
554 return 0;
555 }
556 return 1;
557}
558
Simon Horman05ee2132017-01-04 09:37:25 +0100559/* Dump all fields from <stats> into <out> using the "show info json" format */
Willy Tarreau83061a82018-07-13 11:56:34 +0200560static int stats_dump_json_info_fields(struct buffer *out,
Willy Tarreau43241ff2019-10-09 11:27:51 +0200561 const struct field *info, unsigned int flags)
Simon Horman05ee2132017-01-04 09:37:25 +0100562{
563 int field;
564 int started = 0;
565
566 if (!chunk_strcat(out, "["))
567 return 0;
568
569 for (field = 0; field < INF_TOTAL_FIELDS; field++) {
570 int old_len;
571
572 if (!field_format(info, field))
573 continue;
574
575 if (started && !chunk_strcat(out, ","))
576 goto err;
577 started = 1;
578
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200579 old_len = out->data;
Simon Horman05ee2132017-01-04 09:37:25 +0100580 chunk_appendf(out,
581 "{\"field\":{\"pos\":%d,\"name\":\"%s\"},"
582 "\"processNum\":%u,",
Willy Tarreaueaa55372019-10-09 07:39:11 +0200583 field, info_fields[field].name,
Simon Horman05ee2132017-01-04 09:37:25 +0100584 info[INF_PROCESS_NUM].u.u32);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200585 if (old_len == out->data)
Simon Horman05ee2132017-01-04 09:37:25 +0100586 goto err;
587
588 if (!stats_emit_json_field_tags(out, &info[field]))
589 goto err;
590
591 if (!stats_emit_json_data_field(out, &info[field]))
592 goto err;
593
594 if (!chunk_strcat(out, "}"))
595 goto err;
596 }
597
598 if (!chunk_strcat(out, "]"))
599 goto err;
600 return 1;
601
602err:
603 chunk_reset(out);
604 chunk_appendf(out, "{\"errorStr\":\"output buffer too short\"}");
605 return 0;
606}
607
608/* Dump all fields from <stats> into <out> using a typed "field:desc:type:value" format */
Willy Tarreau83061a82018-07-13 11:56:34 +0200609static int stats_dump_fields_json(struct buffer *out,
610 const struct field *stats,
Willy Tarreaub0ce3ad2019-10-09 11:19:29 +0200611 unsigned int flags)
Simon Horman05ee2132017-01-04 09:37:25 +0100612{
613 int field;
614 int started = 0;
615
Willy Tarreaub0ce3ad2019-10-09 11:19:29 +0200616 if ((flags & STAT_STARTED) && !chunk_strcat(out, ","))
Simon Horman05ee2132017-01-04 09:37:25 +0100617 return 0;
618 if (!chunk_strcat(out, "["))
619 return 0;
620
621 for (field = 0; field < ST_F_TOTAL_FIELDS; field++) {
622 const char *obj_type;
623 int old_len;
624
625 if (!stats[field].type)
626 continue;
627
628 if (started && !chunk_strcat(out, ","))
629 goto err;
630 started = 1;
631
632 switch (stats[ST_F_TYPE].u.u32) {
633 case STATS_TYPE_FE: obj_type = "Frontend"; break;
634 case STATS_TYPE_BE: obj_type = "Backend"; break;
635 case STATS_TYPE_SO: obj_type = "Listener"; break;
636 case STATS_TYPE_SV: obj_type = "Server"; break;
637 default: obj_type = "Unknown"; break;
638 }
639
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200640 old_len = out->data;
Simon Horman05ee2132017-01-04 09:37:25 +0100641 chunk_appendf(out,
642 "{"
643 "\"objType\":\"%s\","
644 "\"proxyId\":%d,"
645 "\"id\":%d,"
646 "\"field\":{\"pos\":%d,\"name\":\"%s\"},"
647 "\"processNum\":%u,",
648 obj_type, stats[ST_F_IID].u.u32,
649 stats[ST_F_SID].u.u32, field,
Willy Tarreaueaa55372019-10-09 07:39:11 +0200650 stat_fields[field].name, stats[ST_F_PID].u.u32);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200651 if (old_len == out->data)
Simon Horman05ee2132017-01-04 09:37:25 +0100652 goto err;
653
654 if (!stats_emit_json_field_tags(out, &stats[field]))
655 goto err;
656
657 if (!stats_emit_json_data_field(out, &stats[field]))
658 goto err;
659
660 if (!chunk_strcat(out, "}"))
661 goto err;
662 }
663
664 if (!chunk_strcat(out, "]"))
665 goto err;
666
667 return 1;
668
669err:
670 chunk_reset(out);
Willy Tarreaub0ce3ad2019-10-09 11:19:29 +0200671 if (flags & STAT_STARTED)
672 chunk_strcat(out, ",");
Simon Horman05ee2132017-01-04 09:37:25 +0100673 chunk_appendf(out, "{\"errorStr\":\"output buffer too short\"}");
674 return 0;
675}
676
William Lallemand74c24fb2016-11-21 17:18:36 +0100677/* Dump all fields from <stats> into <out> using the HTML format. A column is
Willy Tarreauab02b3f2019-10-09 11:11:46 +0200678 * reserved for the checkbox is STAT_ADMIN is set in <flags>. Some extra info
Willy Tarreau708c4162019-10-09 10:19:16 +0200679 * are provided if STAT_SHLGNDS is present in <flags>.
William Lallemand74c24fb2016-11-21 17:18:36 +0100680 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200681static int stats_dump_fields_html(struct buffer *out,
682 const struct field *stats,
683 unsigned int flags)
William Lallemand74c24fb2016-11-21 17:18:36 +0100684{
Willy Tarreau83061a82018-07-13 11:56:34 +0200685 struct buffer src;
William Lallemand74c24fb2016-11-21 17:18:36 +0100686
687 if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_FE) {
688 chunk_appendf(out,
689 /* name, queue */
690 "<tr class=\"frontend\">");
691
Willy Tarreauab02b3f2019-10-09 11:11:46 +0200692 if (flags & STAT_ADMIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +0100693 /* Column sub-heading for Enable or Disable server */
694 chunk_appendf(out, "<td></td>");
695 }
696
697 chunk_appendf(out,
698 "<td class=ac>"
699 "<a name=\"%s/Frontend\"></a>"
700 "<a class=lfsb href=\"#%s/Frontend\">Frontend</a></td>"
701 "<td colspan=3></td>"
702 "",
703 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_PXNAME));
704
705 chunk_appendf(out,
706 /* sessions rate : current */
707 "<td><u>%s<div class=tips><table class=det>"
708 "<tr><th>Current connection rate:</th><td>%s/s</td></tr>"
709 "<tr><th>Current session rate:</th><td>%s/s</td></tr>"
710 "",
711 U2H(stats[ST_F_RATE].u.u32),
712 U2H(stats[ST_F_CONN_RATE].u.u32),
713 U2H(stats[ST_F_RATE].u.u32));
714
715 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
716 chunk_appendf(out,
717 "<tr><th>Current request rate:</th><td>%s/s</td></tr>",
718 U2H(stats[ST_F_REQ_RATE].u.u32));
719
720 chunk_appendf(out,
721 "</table></div></u></td>"
722 /* sessions rate : max */
723 "<td><u>%s<div class=tips><table class=det>"
724 "<tr><th>Max connection rate:</th><td>%s/s</td></tr>"
725 "<tr><th>Max session rate:</th><td>%s/s</td></tr>"
726 "",
727 U2H(stats[ST_F_RATE_MAX].u.u32),
728 U2H(stats[ST_F_CONN_RATE_MAX].u.u32),
729 U2H(stats[ST_F_RATE_MAX].u.u32));
730
731 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
732 chunk_appendf(out,
733 "<tr><th>Max request rate:</th><td>%s/s</td></tr>",
734 U2H(stats[ST_F_REQ_RATE_MAX].u.u32));
735
736 chunk_appendf(out,
737 "</table></div></u></td>"
738 /* sessions rate : limit */
739 "<td>%s</td>",
740 LIM2A(stats[ST_F_RATE_LIM].u.u32, "-"));
741
742 chunk_appendf(out,
743 /* sessions: current, max, limit, total */
744 "<td>%s</td><td>%s</td><td>%s</td>"
745 "<td><u>%s<div class=tips><table class=det>"
746 "<tr><th>Cum. connections:</th><td>%s</td></tr>"
747 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
748 "",
749 U2H(stats[ST_F_SCUR].u.u32), U2H(stats[ST_F_SMAX].u.u32), U2H(stats[ST_F_SLIM].u.u32),
750 U2H(stats[ST_F_STOT].u.u64),
751 U2H(stats[ST_F_CONN_TOT].u.u64),
752 U2H(stats[ST_F_STOT].u.u64));
753
754 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
755 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0) {
756 chunk_appendf(out,
757 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
758 "<tr><th>- HTTP 1xx responses:</th><td>%s</td></tr>"
759 "<tr><th>- HTTP 2xx responses:</th><td>%s</td></tr>"
760 "<tr><th>&nbsp;&nbsp;Compressed 2xx:</th><td>%s</td><td>(%d%%)</td></tr>"
761 "<tr><th>- HTTP 3xx responses:</th><td>%s</td></tr>"
762 "<tr><th>- HTTP 4xx responses:</th><td>%s</td></tr>"
763 "<tr><th>- HTTP 5xx responses:</th><td>%s</td></tr>"
764 "<tr><th>- other responses:</th><td>%s</td></tr>"
765 "<tr><th>Intercepted requests:</th><td>%s</td></tr>"
Willy Tarreaua1214a52018-12-14 14:00:25 +0100766 "<tr><th>Cache lookups:</th><td>%s</td></tr>"
767 "<tr><th>Cache hits:</th><td>%s</td><td>(%d%%)</td></tr>"
Willy Tarreau1b0f85e2018-05-28 15:12:40 +0200768 "<tr><th>Failed hdr rewrites:</th><td>%s</td></tr>"
Christopher Faulet0159ee42019-12-16 14:40:39 +0100769 "<tr><th>Internal errors:</th><td>%s</td></tr>"
William Lallemand74c24fb2016-11-21 17:18:36 +0100770 "",
771 U2H(stats[ST_F_REQ_TOT].u.u64),
772 U2H(stats[ST_F_HRSP_1XX].u.u64),
773 U2H(stats[ST_F_HRSP_2XX].u.u64),
774 U2H(stats[ST_F_COMP_RSP].u.u64),
775 stats[ST_F_HRSP_2XX].u.u64 ?
776 (int)(100 * stats[ST_F_COMP_RSP].u.u64 / stats[ST_F_HRSP_2XX].u.u64) : 0,
777 U2H(stats[ST_F_HRSP_3XX].u.u64),
778 U2H(stats[ST_F_HRSP_4XX].u.u64),
779 U2H(stats[ST_F_HRSP_5XX].u.u64),
780 U2H(stats[ST_F_HRSP_OTHER].u.u64),
Willy Tarreau1b0f85e2018-05-28 15:12:40 +0200781 U2H(stats[ST_F_INTERCEPTED].u.u64),
Willy Tarreaua1214a52018-12-14 14:00:25 +0100782 U2H(stats[ST_F_CACHE_LOOKUPS].u.u64),
783 U2H(stats[ST_F_CACHE_HITS].u.u64),
784 stats[ST_F_CACHE_LOOKUPS].u.u64 ?
785 (int)(100 * stats[ST_F_CACHE_HITS].u.u64 / stats[ST_F_CACHE_LOOKUPS].u.u64) : 0,
Christopher Faulet0159ee42019-12-16 14:40:39 +0100786 U2H(stats[ST_F_WREW].u.u64),
787 U2H(stats[ST_F_EINT].u.u64));
William Lallemand74c24fb2016-11-21 17:18:36 +0100788 }
789
790 chunk_appendf(out,
791 "</table></div></u></td>"
792 /* sessions: lbtot, lastsess */
793 "<td></td><td></td>"
794 /* bytes : in */
795 "<td>%s</td>"
796 "",
797 U2H(stats[ST_F_BIN].u.u64));
798
799 chunk_appendf(out,
800 /* bytes:out + compression stats (via hover): comp_in, comp_out, comp_byp */
801 "<td>%s%s<div class=tips><table class=det>"
802 "<tr><th>Response bytes in:</th><td>%s</td></tr>"
803 "<tr><th>Compression in:</th><td>%s</td></tr>"
804 "<tr><th>Compression out:</th><td>%s</td><td>(%d%%)</td></tr>"
805 "<tr><th>Compression bypass:</th><td>%s</td></tr>"
806 "<tr><th>Total bytes saved:</th><td>%s</td><td>(%d%%)</td></tr>"
807 "</table></div>%s</td>",
808 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "<u>":"",
809 U2H(stats[ST_F_BOUT].u.u64),
810 U2H(stats[ST_F_BOUT].u.u64),
811 U2H(stats[ST_F_COMP_IN].u.u64),
812 U2H(stats[ST_F_COMP_OUT].u.u64),
813 stats[ST_F_COMP_IN].u.u64 ? (int)(stats[ST_F_COMP_OUT].u.u64 * 100 / stats[ST_F_COMP_IN].u.u64) : 0,
814 U2H(stats[ST_F_COMP_BYP].u.u64),
815 U2H(stats[ST_F_COMP_IN].u.u64 - stats[ST_F_COMP_OUT].u.u64),
816 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,
817 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "</u>":"");
818
819 chunk_appendf(out,
820 /* denied: req, resp */
821 "<td>%s</td><td>%s</td>"
822 /* errors : request, connect, response */
823 "<td>%s</td><td></td><td></td>"
824 /* warnings: retries, redispatches */
825 "<td></td><td></td>"
826 /* server status : reflect frontend status */
827 "<td class=ac>%s</td>"
828 /* rest of server: nothing */
829 "<td class=ac colspan=8></td></tr>"
830 "",
831 U2H(stats[ST_F_DREQ].u.u64), U2H(stats[ST_F_DRESP].u.u64),
832 U2H(stats[ST_F_EREQ].u.u64),
833 field_str(stats, ST_F_STATUS));
834 }
835 else if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_SO) {
836 chunk_appendf(out, "<tr class=socket>");
Willy Tarreauab02b3f2019-10-09 11:11:46 +0200837 if (flags & STAT_ADMIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +0100838 /* Column sub-heading for Enable or Disable server */
839 chunk_appendf(out, "<td></td>");
840 }
841
842 chunk_appendf(out,
843 /* frontend name, listener name */
844 "<td class=ac><a name=\"%s/+%s\"></a>%s"
845 "<a class=lfsb href=\"#%s/+%s\">%s</a>"
846 "",
847 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME),
Willy Tarreau708c4162019-10-09 10:19:16 +0200848 (flags & STAT_SHLGNDS)?"<u>":"",
William Lallemand74c24fb2016-11-21 17:18:36 +0100849 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME), field_str(stats, ST_F_SVNAME));
850
Willy Tarreau708c4162019-10-09 10:19:16 +0200851 if (flags & STAT_SHLGNDS) {
William Lallemand74c24fb2016-11-21 17:18:36 +0100852 chunk_appendf(out, "<div class=tips>");
853
Willy Tarreau90807112020-02-25 08:16:33 +0100854 if (isdigit((unsigned char)*field_str(stats, ST_F_ADDR)))
William Lallemand74c24fb2016-11-21 17:18:36 +0100855 chunk_appendf(out, "IPv4: %s, ", field_str(stats, ST_F_ADDR));
856 else if (*field_str(stats, ST_F_ADDR) == '[')
857 chunk_appendf(out, "IPv6: %s, ", field_str(stats, ST_F_ADDR));
858 else if (*field_str(stats, ST_F_ADDR))
859 chunk_appendf(out, "%s, ", field_str(stats, ST_F_ADDR));
860
861 /* id */
862 chunk_appendf(out, "id: %d</div>", stats[ST_F_SID].u.u32);
863 }
864
865 chunk_appendf(out,
866 /* queue */
867 "%s</td><td colspan=3></td>"
868 /* sessions rate: current, max, limit */
869 "<td colspan=3>&nbsp;</td>"
870 /* sessions: current, max, limit, total, lbtot, lastsess */
871 "<td>%s</td><td>%s</td><td>%s</td>"
872 "<td>%s</td><td>&nbsp;</td><td>&nbsp;</td>"
873 /* bytes: in, out */
874 "<td>%s</td><td>%s</td>"
875 "",
Willy Tarreau708c4162019-10-09 10:19:16 +0200876 (flags & STAT_SHLGNDS)?"</u>":"",
William Lallemand74c24fb2016-11-21 17:18:36 +0100877 U2H(stats[ST_F_SCUR].u.u32), U2H(stats[ST_F_SMAX].u.u32), U2H(stats[ST_F_SLIM].u.u32),
878 U2H(stats[ST_F_STOT].u.u64), U2H(stats[ST_F_BIN].u.u64), U2H(stats[ST_F_BOUT].u.u64));
879
880 chunk_appendf(out,
881 /* denied: req, resp */
882 "<td>%s</td><td>%s</td>"
883 /* errors: request, connect, response */
884 "<td>%s</td><td></td><td></td>"
885 /* warnings: retries, redispatches */
886 "<td></td><td></td>"
887 /* server status: reflect listener status */
888 "<td class=ac>%s</td>"
889 /* rest of server: nothing */
890 "<td class=ac colspan=8></td></tr>"
891 "",
892 U2H(stats[ST_F_DREQ].u.u64), U2H(stats[ST_F_DRESP].u.u64),
893 U2H(stats[ST_F_EREQ].u.u64),
894 field_str(stats, ST_F_STATUS));
895 }
896 else if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_SV) {
897 const char *style;
898
899 /* determine the style to use depending on the server's state,
900 * its health and weight. There isn't a 1-to-1 mapping between
901 * state and styles for the cases where the server is (still)
902 * up. The reason is that we don't want to report nolb and
903 * drain with the same color.
904 */
905
906 if (strcmp(field_str(stats, ST_F_STATUS), "DOWN") == 0 ||
907 strcmp(field_str(stats, ST_F_STATUS), "DOWN (agent)") == 0) {
908 style = "down";
909 }
910 else if (strcmp(field_str(stats, ST_F_STATUS), "DOWN ") == 0) {
911 style = "going_up";
912 }
Daniel Corbettb4285172020-03-28 12:35:50 -0400913 else if (strcmp(field_str(stats, ST_F_STATUS), "DRAIN") == 0) {
914 style = "draining";
915 }
William Lallemand74c24fb2016-11-21 17:18:36 +0100916 else if (strcmp(field_str(stats, ST_F_STATUS), "NOLB ") == 0) {
917 style = "going_down";
918 }
919 else if (strcmp(field_str(stats, ST_F_STATUS), "NOLB") == 0) {
920 style = "nolb";
921 }
922 else if (strcmp(field_str(stats, ST_F_STATUS), "no check") == 0) {
923 style = "no_check";
924 }
925 else if (!stats[ST_F_CHKFAIL].type ||
926 stats[ST_F_CHECK_HEALTH].u.u32 == stats[ST_F_CHECK_RISE].u.u32 + stats[ST_F_CHECK_FALL].u.u32 - 1) {
927 /* no check or max health = UP */
928 if (stats[ST_F_WEIGHT].u.u32)
929 style = "up";
930 else
931 style = "draining";
932 }
933 else {
934 style = "going_down";
935 }
936
937 if (memcmp(field_str(stats, ST_F_STATUS), "MAINT", 5) == 0)
938 chunk_appendf(out, "<tr class=\"maintain\">");
939 else
940 chunk_appendf(out,
941 "<tr class=\"%s_%s\">",
942 (stats[ST_F_BCK].u.u32) ? "backup" : "active", style);
943
944
Willy Tarreauab02b3f2019-10-09 11:11:46 +0200945 if (flags & STAT_ADMIN)
William Lallemand74c24fb2016-11-21 17:18:36 +0100946 chunk_appendf(out,
David Harrigand3db35a2016-12-30 12:12:49 +0000947 "<td><input class='%s-checkbox' type=\"checkbox\" name=\"s\" value=\"%s\"></td>",
948 field_str(stats, ST_F_PXNAME),
William Lallemand74c24fb2016-11-21 17:18:36 +0100949 field_str(stats, ST_F_SVNAME));
950
951 chunk_appendf(out,
952 "<td class=ac><a name=\"%s/%s\"></a>%s"
953 "<a class=lfsb href=\"#%s/%s\">%s</a>"
954 "",
955 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME),
Willy Tarreau708c4162019-10-09 10:19:16 +0200956 (flags & STAT_SHLGNDS) ? "<u>" : "",
William Lallemand74c24fb2016-11-21 17:18:36 +0100957 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME), field_str(stats, ST_F_SVNAME));
958
Willy Tarreau708c4162019-10-09 10:19:16 +0200959 if (flags & STAT_SHLGNDS) {
William Lallemand74c24fb2016-11-21 17:18:36 +0100960 chunk_appendf(out, "<div class=tips>");
961
Willy Tarreau90807112020-02-25 08:16:33 +0100962 if (isdigit((unsigned char)*field_str(stats, ST_F_ADDR)))
William Lallemand74c24fb2016-11-21 17:18:36 +0100963 chunk_appendf(out, "IPv4: %s, ", field_str(stats, ST_F_ADDR));
964 else if (*field_str(stats, ST_F_ADDR) == '[')
965 chunk_appendf(out, "IPv6: %s, ", field_str(stats, ST_F_ADDR));
966 else if (*field_str(stats, ST_F_ADDR))
967 chunk_appendf(out, "%s, ", field_str(stats, ST_F_ADDR));
968
969 /* id */
970 chunk_appendf(out, "id: %d", stats[ST_F_SID].u.u32);
971
972 /* cookie */
973 if (stats[ST_F_COOKIE].type) {
974 chunk_appendf(out, ", cookie: '");
975 chunk_initstr(&src, field_str(stats, ST_F_COOKIE));
976 chunk_htmlencode(out, &src);
977 chunk_appendf(out, "'");
978 }
979
980 chunk_appendf(out, "</div>");
981 }
982
983 chunk_appendf(out,
984 /* queue : current, max, limit */
985 "%s</td><td>%s</td><td>%s</td><td>%s</td>"
986 /* sessions rate : current, max, limit */
987 "<td>%s</td><td>%s</td><td></td>"
988 "",
Willy Tarreau708c4162019-10-09 10:19:16 +0200989 (flags & STAT_SHLGNDS) ? "</u>" : "",
William Lallemand74c24fb2016-11-21 17:18:36 +0100990 U2H(stats[ST_F_QCUR].u.u32), U2H(stats[ST_F_QMAX].u.u32), LIM2A(stats[ST_F_QLIMIT].u.u32, "-"),
991 U2H(stats[ST_F_RATE].u.u32), U2H(stats[ST_F_RATE_MAX].u.u32));
992
993 chunk_appendf(out,
994 /* sessions: current, max, limit, total */
Willy Tarreauf21d17b2019-09-08 09:24:56 +0200995 "<td><u>%s<div class=tips>"
996 "<table class=det>"
997 "<tr><th>Current active connections:</th><td>%s</td></tr>"
998 "<tr><th>Current idle connections:</th><td>%s</td></tr>"
999 "<tr><th>Active connections limit:</th><td>%s</td></tr>"
1000 "<tr><th>Idle connections limit:</th><td>%s</td></tr>"
1001 "</table></div></u>"
1002 "</td><td>%s</td><td>%s</td>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001003 "<td><u>%s<div class=tips><table class=det>"
1004 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
1005 "",
Willy Tarreauf21d17b2019-09-08 09:24:56 +02001006 U2H(stats[ST_F_SCUR].u.u32),
1007 U2H(stats[ST_F_SCUR].u.u32),
1008 U2H(stats[ST_F_SRV_ICUR].u.u32),
1009 LIM2A(stats[ST_F_SLIM].u.u32, "-"),
1010 stats[ST_F_SRV_ILIM].type ? U2H(stats[ST_F_SRV_ILIM].u.u32) : "-",
1011 U2H(stats[ST_F_SMAX].u.u32), LIM2A(stats[ST_F_SLIM].u.u32, "-"),
William Lallemand74c24fb2016-11-21 17:18:36 +01001012 U2H(stats[ST_F_STOT].u.u64),
1013 U2H(stats[ST_F_STOT].u.u64));
1014
1015 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
1016 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001017 chunk_appendf(out,
Willy Tarreauf1573842018-12-14 11:35:36 +01001018 "<tr><th>New connections:</th><td>%s</td></tr>"
1019 "<tr><th>Reused connections:</th><td>%s</td><td>(%d%%)</td></tr>"
Marcin Deranek3c27dda2020-05-15 18:32:51 +02001020 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001021 "<tr><th>- HTTP 1xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
1022 "<tr><th>- HTTP 2xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
1023 "<tr><th>- HTTP 3xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
1024 "<tr><th>- HTTP 4xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
1025 "<tr><th>- HTTP 5xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
1026 "<tr><th>- other responses:</th><td>%s</td><td>(%d%%)</td></tr>"
Willy Tarreau1b0f85e2018-05-28 15:12:40 +02001027 "<tr><th>Failed hdr rewrites:</th><td>%s</td></tr>"
Christopher Faulet0159ee42019-12-16 14:40:39 +01001028 "<tr><th>Internal error:</th><td>%s</td></tr>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001029 "",
Willy Tarreauf1573842018-12-14 11:35:36 +01001030 U2H(stats[ST_F_CONNECT].u.u64),
1031 U2H(stats[ST_F_REUSE].u.u64),
1032 (stats[ST_F_CONNECT].u.u64 + stats[ST_F_REUSE].u.u64) ?
1033 (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 +02001034 U2H(stats[ST_F_REQ_TOT].u.u64),
1035 U2H(stats[ST_F_HRSP_1XX].u.u64), stats[ST_F_REQ_TOT].u.u64 ?
1036 (int)(100 * stats[ST_F_HRSP_1XX].u.u64 / stats[ST_F_REQ_TOT].u.u64) : 0,
1037 U2H(stats[ST_F_HRSP_2XX].u.u64), stats[ST_F_REQ_TOT].u.u64 ?
1038 (int)(100 * stats[ST_F_HRSP_2XX].u.u64 / stats[ST_F_REQ_TOT].u.u64) : 0,
1039 U2H(stats[ST_F_HRSP_3XX].u.u64), stats[ST_F_REQ_TOT].u.u64 ?
1040 (int)(100 * stats[ST_F_HRSP_3XX].u.u64 / stats[ST_F_REQ_TOT].u.u64) : 0,
1041 U2H(stats[ST_F_HRSP_4XX].u.u64), stats[ST_F_REQ_TOT].u.u64 ?
1042 (int)(100 * stats[ST_F_HRSP_4XX].u.u64 / stats[ST_F_REQ_TOT].u.u64) : 0,
1043 U2H(stats[ST_F_HRSP_5XX].u.u64), stats[ST_F_REQ_TOT].u.u64 ?
1044 (int)(100 * stats[ST_F_HRSP_5XX].u.u64 / stats[ST_F_REQ_TOT].u.u64) : 0,
1045 U2H(stats[ST_F_HRSP_OTHER].u.u64), stats[ST_F_REQ_TOT].u.u64 ?
1046 (int)(100 * stats[ST_F_HRSP_OTHER].u.u64 / stats[ST_F_REQ_TOT].u.u64) : 0,
Christopher Faulet0159ee42019-12-16 14:40:39 +01001047 U2H(stats[ST_F_WREW].u.u64),
1048 U2H(stats[ST_F_EINT].u.u64));
William Lallemand74c24fb2016-11-21 17:18:36 +01001049 }
1050
Christopher Faulet0d1c2a62019-11-08 14:59:51 +01001051 chunk_appendf(out, "<tr><th colspan=3>Max / Avg over last 1024 success. conn.</th></tr>");
1052 chunk_appendf(out, "<tr><th>- Queue time:</th><td>%s / %s</td><td>ms</td></tr>",
1053 U2H(stats[ST_F_QT_MAX].u.u32), U2H(stats[ST_F_QTIME].u.u32));
1054 chunk_appendf(out, "<tr><th>- Connect time:</th><td>%s / %s</td><td>ms</td></tr>",
1055 U2H(stats[ST_F_CT_MAX].u.u32), U2H(stats[ST_F_CTIME].u.u32));
William Lallemand74c24fb2016-11-21 17:18:36 +01001056 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
Christopher Faulet0d1c2a62019-11-08 14:59:51 +01001057 chunk_appendf(out, "<tr><th>- Responses time:</th><td>%s / %s</td><td>ms</td></tr>",
1058 U2H(stats[ST_F_RT_MAX].u.u32), U2H(stats[ST_F_RTIME].u.u32));
1059 chunk_appendf(out, "<tr><th>- Total time:</th><td>%s / %s</td><td>ms</td></tr>",
1060 U2H(stats[ST_F_TT_MAX].u.u32), U2H(stats[ST_F_TTIME].u.u32));
William Lallemand74c24fb2016-11-21 17:18:36 +01001061
1062 chunk_appendf(out,
1063 "</table></div></u></td>"
1064 /* sessions: lbtot, last */
1065 "<td>%s</td><td>%s</td>",
1066 U2H(stats[ST_F_LBTOT].u.u64),
1067 human_time(stats[ST_F_LASTSESS].u.s32, 1));
1068
1069 chunk_appendf(out,
1070 /* bytes : in, out */
1071 "<td>%s</td><td>%s</td>"
1072 /* denied: req, resp */
1073 "<td></td><td>%s</td>"
1074 /* errors : request, connect */
1075 "<td></td><td>%s</td>"
1076 /* errors : response */
1077 "<td><u>%s<div class=tips>Connection resets during transfers: %lld client, %lld server</div></u></td>"
1078 /* warnings: retries, redispatches */
1079 "<td>%lld</td><td>%lld</td>"
1080 "",
1081 U2H(stats[ST_F_BIN].u.u64), U2H(stats[ST_F_BOUT].u.u64),
1082 U2H(stats[ST_F_DRESP].u.u64),
1083 U2H(stats[ST_F_ECON].u.u64),
1084 U2H(stats[ST_F_ERESP].u.u64),
1085 (long long)stats[ST_F_CLI_ABRT].u.u64,
1086 (long long)stats[ST_F_SRV_ABRT].u.u64,
1087 (long long)stats[ST_F_WRETR].u.u64,
1088 (long long)stats[ST_F_WREDIS].u.u64);
1089
1090 /* status, last change */
1091 chunk_appendf(out, "<td class=ac>");
1092
1093 /* FIXME!!!!
1094 * LASTCHG should contain the last change for *this* server and must be computed
1095 * properly above, as was done below, ie: this server if maint, otherwise ref server
1096 * if tracking. Note that ref is either local or remote depending on tracking.
1097 */
1098
1099
1100 if (memcmp(field_str(stats, ST_F_STATUS), "MAINT", 5) == 0) {
1101 chunk_appendf(out, "%s MAINT", human_time(stats[ST_F_LASTCHG].u.u32, 1));
1102 }
1103 else if (memcmp(field_str(stats, ST_F_STATUS), "no check", 5) == 0) {
1104 chunk_strcat(out, "<i>no check</i>");
1105 }
1106 else {
1107 chunk_appendf(out, "%s %s", human_time(stats[ST_F_LASTCHG].u.u32, 1), field_str(stats, ST_F_STATUS));
1108 if (memcmp(field_str(stats, ST_F_STATUS), "DOWN", 4) == 0) {
1109 if (stats[ST_F_CHECK_HEALTH].u.u32)
1110 chunk_strcat(out, " &uarr;");
1111 }
1112 else if (stats[ST_F_CHECK_HEALTH].u.u32 < stats[ST_F_CHECK_RISE].u.u32 + stats[ST_F_CHECK_FALL].u.u32 - 1)
1113 chunk_strcat(out, " &darr;");
1114 }
1115
1116 if (memcmp(field_str(stats, ST_F_STATUS), "DOWN", 4) == 0 &&
1117 stats[ST_F_AGENT_STATUS].type && !stats[ST_F_AGENT_HEALTH].u.u32) {
1118 chunk_appendf(out,
1119 "</td><td class=ac><u> %s",
1120 field_str(stats, ST_F_AGENT_STATUS));
1121
1122 if (stats[ST_F_AGENT_CODE].type)
1123 chunk_appendf(out, "/%d", stats[ST_F_AGENT_CODE].u.u32);
1124
1125 if (stats[ST_F_AGENT_DURATION].type)
1126 chunk_appendf(out, " in %lums", (long)stats[ST_F_AGENT_DURATION].u.u64);
1127
1128 chunk_appendf(out, "<div class=tips>%s", field_str(stats, ST_F_AGENT_DESC));
1129
1130 if (*field_str(stats, ST_F_LAST_AGT)) {
1131 chunk_appendf(out, ": ");
1132 chunk_initstr(&src, field_str(stats, ST_F_LAST_AGT));
1133 chunk_htmlencode(out, &src);
1134 }
1135 chunk_appendf(out, "</div></u>");
1136 }
1137 else if (stats[ST_F_CHECK_STATUS].type) {
1138 chunk_appendf(out,
1139 "</td><td class=ac><u> %s",
1140 field_str(stats, ST_F_CHECK_STATUS));
1141
1142 if (stats[ST_F_CHECK_CODE].type)
1143 chunk_appendf(out, "/%d", stats[ST_F_CHECK_CODE].u.u32);
1144
1145 if (stats[ST_F_CHECK_DURATION].type)
1146 chunk_appendf(out, " in %lums", (long)stats[ST_F_CHECK_DURATION].u.u64);
1147
1148 chunk_appendf(out, "<div class=tips>%s", field_str(stats, ST_F_CHECK_DESC));
1149
1150 if (*field_str(stats, ST_F_LAST_CHK)) {
1151 chunk_appendf(out, ": ");
1152 chunk_initstr(&src, field_str(stats, ST_F_LAST_CHK));
1153 chunk_htmlencode(out, &src);
1154 }
1155 chunk_appendf(out, "</div></u>");
1156 }
1157 else
1158 chunk_appendf(out, "</td><td>");
1159
1160 chunk_appendf(out,
1161 /* weight */
1162 "</td><td class=ac>%d</td>"
1163 /* act, bck */
1164 "<td class=ac>%s</td><td class=ac>%s</td>"
1165 "",
1166 stats[ST_F_WEIGHT].u.u32,
1167 stats[ST_F_BCK].u.u32 ? "-" : "Y",
1168 stats[ST_F_BCK].u.u32 ? "Y" : "-");
1169
1170 /* check failures: unique, fatal, down time */
1171 if (strcmp(field_str(stats, ST_F_STATUS), "MAINT (resolution)") == 0) {
1172 chunk_appendf(out, "<td class=ac colspan=3>resolution</td>");
1173 }
1174 else if (stats[ST_F_CHKFAIL].type) {
1175 chunk_appendf(out, "<td><u>%lld", (long long)stats[ST_F_CHKFAIL].u.u64);
1176
1177 if (stats[ST_F_HANAFAIL].type)
1178 chunk_appendf(out, "/%lld", (long long)stats[ST_F_HANAFAIL].u.u64);
1179
1180 chunk_appendf(out,
1181 "<div class=tips>Failed Health Checks%s</div></u></td>"
1182 "<td>%lld</td><td>%s</td>"
1183 "",
1184 stats[ST_F_HANAFAIL].type ? "/Health Analyses" : "",
1185 (long long)stats[ST_F_CHKDOWN].u.u64, human_time(stats[ST_F_DOWNTIME].u.u32, 1));
1186 }
1187 else if (strcmp(field_str(stats, ST_F_STATUS), "MAINT") != 0 && field_format(stats, ST_F_TRACKED) == FF_STR) {
1188 /* tracking a server (hence inherited maint would appear as "MAINT (via...)" */
1189 chunk_appendf(out,
1190 "<td class=ac colspan=3><a class=lfsb href=\"#%s\">via %s</a></td>",
1191 field_str(stats, ST_F_TRACKED), field_str(stats, ST_F_TRACKED));
1192 }
1193 else
1194 chunk_appendf(out, "<td colspan=3></td>");
1195
1196 /* throttle */
1197 if (stats[ST_F_THROTTLE].type)
1198 chunk_appendf(out, "<td class=ac>%d %%</td></tr>\n", stats[ST_F_THROTTLE].u.u32);
1199 else
1200 chunk_appendf(out, "<td class=ac>-</td></tr>\n");
1201 }
1202 else if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_BE) {
1203 chunk_appendf(out, "<tr class=\"backend\">");
Willy Tarreauab02b3f2019-10-09 11:11:46 +02001204 if (flags & STAT_ADMIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001205 /* Column sub-heading for Enable or Disable server */
1206 chunk_appendf(out, "<td></td>");
1207 }
1208 chunk_appendf(out,
1209 "<td class=ac>"
1210 /* name */
1211 "%s<a name=\"%s/Backend\"></a>"
1212 "<a class=lfsb href=\"#%s/Backend\">Backend</a>"
1213 "",
Willy Tarreau708c4162019-10-09 10:19:16 +02001214 (flags & STAT_SHLGNDS)?"<u>":"",
William Lallemand74c24fb2016-11-21 17:18:36 +01001215 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_PXNAME));
1216
Willy Tarreau708c4162019-10-09 10:19:16 +02001217 if (flags & STAT_SHLGNDS) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001218 /* balancing */
1219 chunk_appendf(out, "<div class=tips>balancing: %s",
1220 field_str(stats, ST_F_ALGO));
1221
1222 /* cookie */
1223 if (stats[ST_F_COOKIE].type) {
1224 chunk_appendf(out, ", cookie: '");
1225 chunk_initstr(&src, field_str(stats, ST_F_COOKIE));
1226 chunk_htmlencode(out, &src);
1227 chunk_appendf(out, "'");
1228 }
1229 chunk_appendf(out, "</div>");
1230 }
1231
1232 chunk_appendf(out,
1233 "%s</td>"
1234 /* queue : current, max */
1235 "<td>%s</td><td>%s</td><td></td>"
1236 /* sessions rate : current, max, limit */
1237 "<td>%s</td><td>%s</td><td></td>"
1238 "",
Willy Tarreau708c4162019-10-09 10:19:16 +02001239 (flags & STAT_SHLGNDS)?"</u>":"",
William Lallemand74c24fb2016-11-21 17:18:36 +01001240 U2H(stats[ST_F_QCUR].u.u32), U2H(stats[ST_F_QMAX].u.u32),
1241 U2H(stats[ST_F_RATE].u.u32), U2H(stats[ST_F_RATE_MAX].u.u32));
1242
1243 chunk_appendf(out,
1244 /* sessions: current, max, limit, total */
1245 "<td>%s</td><td>%s</td><td>%s</td>"
1246 "<td><u>%s<div class=tips><table class=det>"
1247 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
1248 "",
Willy Tarreau8e0f1752016-12-12 15:07:29 +01001249 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 +01001250 U2H(stats[ST_F_STOT].u.u64),
1251 U2H(stats[ST_F_STOT].u.u64));
1252
1253 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
1254 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0) {
1255 chunk_appendf(out,
Willy Tarreauf1573842018-12-14 11:35:36 +01001256 "<tr><th>New connections:</th><td>%s</td></tr>"
1257 "<tr><th>Reused connections:</th><td>%s</td><td>(%d%%)</td></tr>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001258 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
1259 "<tr><th>- HTTP 1xx responses:</th><td>%s</td></tr>"
1260 "<tr><th>- HTTP 2xx responses:</th><td>%s</td></tr>"
1261 "<tr><th>&nbsp;&nbsp;Compressed 2xx:</th><td>%s</td><td>(%d%%)</td></tr>"
1262 "<tr><th>- HTTP 3xx responses:</th><td>%s</td></tr>"
1263 "<tr><th>- HTTP 4xx responses:</th><td>%s</td></tr>"
1264 "<tr><th>- HTTP 5xx responses:</th><td>%s</td></tr>"
1265 "<tr><th>- other responses:</th><td>%s</td></tr>"
Willy Tarreaua1214a52018-12-14 14:00:25 +01001266 "<tr><th>Cache lookups:</th><td>%s</td></tr>"
1267 "<tr><th>Cache hits:</th><td>%s</td><td>(%d%%)</td></tr>"
Willy Tarreau1b0f85e2018-05-28 15:12:40 +02001268 "<tr><th>Failed hdr rewrites:</th><td>%s</td></tr>"
Christopher Faulet0159ee42019-12-16 14:40:39 +01001269 "<tr><th>Internal errors:</th><td>%s</td></tr>"
Christopher Faulet0d1c2a62019-11-08 14:59:51 +01001270 "",
Willy Tarreauf1573842018-12-14 11:35:36 +01001271 U2H(stats[ST_F_CONNECT].u.u64),
1272 U2H(stats[ST_F_REUSE].u.u64),
1273 (stats[ST_F_CONNECT].u.u64 + stats[ST_F_REUSE].u.u64) ?
1274 (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 +01001275 U2H(stats[ST_F_REQ_TOT].u.u64),
1276 U2H(stats[ST_F_HRSP_1XX].u.u64),
1277 U2H(stats[ST_F_HRSP_2XX].u.u64),
1278 U2H(stats[ST_F_COMP_RSP].u.u64),
1279 stats[ST_F_HRSP_2XX].u.u64 ?
1280 (int)(100 * stats[ST_F_COMP_RSP].u.u64 / stats[ST_F_HRSP_2XX].u.u64) : 0,
1281 U2H(stats[ST_F_HRSP_3XX].u.u64),
1282 U2H(stats[ST_F_HRSP_4XX].u.u64),
1283 U2H(stats[ST_F_HRSP_5XX].u.u64),
Willy Tarreaufeead3a2018-12-14 13:48:44 +01001284 U2H(stats[ST_F_HRSP_OTHER].u.u64),
Willy Tarreaua1214a52018-12-14 14:00:25 +01001285 U2H(stats[ST_F_CACHE_LOOKUPS].u.u64),
1286 U2H(stats[ST_F_CACHE_HITS].u.u64),
1287 stats[ST_F_CACHE_LOOKUPS].u.u64 ?
1288 (int)(100 * stats[ST_F_CACHE_HITS].u.u64 / stats[ST_F_CACHE_LOOKUPS].u.u64) : 0,
Christopher Faulet0159ee42019-12-16 14:40:39 +01001289 U2H(stats[ST_F_WREW].u.u64),
1290 U2H(stats[ST_F_EINT].u.u64));
William Lallemand74c24fb2016-11-21 17:18:36 +01001291 }
1292
Christopher Faulet0d1c2a62019-11-08 14:59:51 +01001293 chunk_appendf(out, "<tr><th colspan=3>Max / Avg over last 1024 success. conn.</th></tr>");
1294 chunk_appendf(out, "<tr><th>- Queue time:</th><td>%s / %s</td><td>ms</td></tr>",
1295 U2H(stats[ST_F_QT_MAX].u.u32), U2H(stats[ST_F_QTIME].u.u32));
1296 chunk_appendf(out, "<tr><th>- Connect time:</th><td>%s / %s</td><td>ms</td></tr>",
1297 U2H(stats[ST_F_CT_MAX].u.u32), U2H(stats[ST_F_CTIME].u.u32));
William Lallemand74c24fb2016-11-21 17:18:36 +01001298 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
Christopher Faulet0d1c2a62019-11-08 14:59:51 +01001299 chunk_appendf(out, "<tr><th>- Responses time:</th><td>%s / %s</td><td>ms</td></tr>",
1300 U2H(stats[ST_F_RT_MAX].u.u32), U2H(stats[ST_F_RTIME].u.u32));
1301 chunk_appendf(out, "<tr><th>- Total time:</th><td>%s / %s</td><td>ms</td></tr>",
1302 U2H(stats[ST_F_TT_MAX].u.u32), U2H(stats[ST_F_TTIME].u.u32));
William Lallemand74c24fb2016-11-21 17:18:36 +01001303
1304 chunk_appendf(out,
1305 "</table></div></u></td>"
1306 /* sessions: lbtot, last */
1307 "<td>%s</td><td>%s</td>"
1308 /* bytes: in */
1309 "<td>%s</td>"
1310 "",
1311 U2H(stats[ST_F_LBTOT].u.u64),
1312 human_time(stats[ST_F_LASTSESS].u.s32, 1),
1313 U2H(stats[ST_F_BIN].u.u64));
1314
1315 chunk_appendf(out,
1316 /* bytes:out + compression stats (via hover): comp_in, comp_out, comp_byp */
1317 "<td>%s%s<div class=tips><table class=det>"
1318 "<tr><th>Response bytes in:</th><td>%s</td></tr>"
1319 "<tr><th>Compression in:</th><td>%s</td></tr>"
1320 "<tr><th>Compression out:</th><td>%s</td><td>(%d%%)</td></tr>"
1321 "<tr><th>Compression bypass:</th><td>%s</td></tr>"
1322 "<tr><th>Total bytes saved:</th><td>%s</td><td>(%d%%)</td></tr>"
1323 "</table></div>%s</td>",
1324 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "<u>":"",
1325 U2H(stats[ST_F_BOUT].u.u64),
1326 U2H(stats[ST_F_BOUT].u.u64),
1327 U2H(stats[ST_F_COMP_IN].u.u64),
1328 U2H(stats[ST_F_COMP_OUT].u.u64),
1329 stats[ST_F_COMP_IN].u.u64 ? (int)(stats[ST_F_COMP_OUT].u.u64 * 100 / stats[ST_F_COMP_IN].u.u64) : 0,
1330 U2H(stats[ST_F_COMP_BYP].u.u64),
1331 U2H(stats[ST_F_COMP_IN].u.u64 - stats[ST_F_COMP_OUT].u.u64),
1332 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,
1333 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "</u>":"");
1334
1335 chunk_appendf(out,
1336 /* denied: req, resp */
1337 "<td>%s</td><td>%s</td>"
1338 /* errors : request, connect */
1339 "<td></td><td>%s</td>"
1340 /* errors : response */
1341 "<td><u>%s<div class=tips>Connection resets during transfers: %lld client, %lld server</div></u></td>"
1342 /* warnings: retries, redispatches */
1343 "<td>%lld</td><td>%lld</td>"
1344 /* backend status: reflect backend status (up/down): we display UP
1345 * if the backend has known working servers or if it has no server at
1346 * all (eg: for stats). Then we display the total weight, number of
1347 * active and backups. */
1348 "<td class=ac>%s %s</td><td class=ac>&nbsp;</td><td class=ac>%d</td>"
1349 "<td class=ac>%d</td><td class=ac>%d</td>"
1350 "",
1351 U2H(stats[ST_F_DREQ].u.u64), U2H(stats[ST_F_DRESP].u.u64),
1352 U2H(stats[ST_F_ECON].u.u64),
1353 U2H(stats[ST_F_ERESP].u.u64),
1354 (long long)stats[ST_F_CLI_ABRT].u.u64,
1355 (long long)stats[ST_F_SRV_ABRT].u.u64,
1356 (long long)stats[ST_F_WRETR].u.u64, (long long)stats[ST_F_WREDIS].u.u64,
1357 human_time(stats[ST_F_LASTCHG].u.u32, 1),
1358 strcmp(field_str(stats, ST_F_STATUS), "DOWN") ? field_str(stats, ST_F_STATUS) : "<font color=\"red\"><b>DOWN</b></font>",
1359 stats[ST_F_WEIGHT].u.u32,
1360 stats[ST_F_ACT].u.u32, stats[ST_F_BCK].u.u32);
1361
1362 chunk_appendf(out,
1363 /* rest of backend: nothing, down transitions, total downtime, throttle */
1364 "<td class=ac>&nbsp;</td><td>%d</td>"
1365 "<td>%s</td>"
1366 "<td></td>"
1367 "</tr>",
1368 stats[ST_F_CHKDOWN].u.u32,
1369 stats[ST_F_DOWNTIME].type ? human_time(stats[ST_F_DOWNTIME].u.u32, 1) : "&nbsp;");
1370 }
1371 return 1;
1372}
1373
Willy Tarreau43241ff2019-10-09 11:27:51 +02001374static int stats_dump_one_line(const struct field *stats, struct proxy *px, struct appctx *appctx)
William Lallemand74c24fb2016-11-21 17:18:36 +01001375{
Simon Horman05ee2132017-01-04 09:37:25 +01001376 int ret;
1377
William Lallemand74c24fb2016-11-21 17:18:36 +01001378 if (appctx->ctx.stats.flags & STAT_FMT_HTML)
Willy Tarreau43241ff2019-10-09 11:27:51 +02001379 ret = stats_dump_fields_html(&trash, stats, appctx->ctx.stats.flags);
William Lallemand74c24fb2016-11-21 17:18:36 +01001380 else if (appctx->ctx.stats.flags & STAT_FMT_TYPED)
Willy Tarreau43241ff2019-10-09 11:27:51 +02001381 ret = stats_dump_fields_typed(&trash, stats, appctx->ctx.stats.flags);
Simon Horman05ee2132017-01-04 09:37:25 +01001382 else if (appctx->ctx.stats.flags & STAT_FMT_JSON)
Willy Tarreaub0ce3ad2019-10-09 11:19:29 +02001383 ret = stats_dump_fields_json(&trash, stats, appctx->ctx.stats.flags);
William Lallemand74c24fb2016-11-21 17:18:36 +01001384 else
Willy Tarreau43241ff2019-10-09 11:27:51 +02001385 ret = stats_dump_fields_csv(&trash, stats, appctx->ctx.stats.flags);
Simon Horman05ee2132017-01-04 09:37:25 +01001386
1387 if (ret)
1388 appctx->ctx.stats.flags |= STAT_STARTED;
1389
1390 return ret;
William Lallemand74c24fb2016-11-21 17:18:36 +01001391}
1392
1393/* Fill <stats> with the frontend statistics. <stats> is
1394 * preallocated array of length <len>. The length of the array
1395 * must be at least ST_F_TOTAL_FIELDS. If this length is less then
1396 * this value, the function returns 0, otherwise, it returns 1.
1397 */
1398int stats_fill_fe_stats(struct proxy *px, struct field *stats, int len)
1399{
1400 if (len < ST_F_TOTAL_FIELDS)
1401 return 0;
1402
1403 memset(stats, 0, sizeof(*stats) * len);
1404
1405 stats[ST_F_PXNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
1406 stats[ST_F_SVNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, "FRONTEND");
1407 stats[ST_F_MODE] = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
1408 stats[ST_F_SCUR] = mkf_u32(0, px->feconn);
1409 stats[ST_F_SMAX] = mkf_u32(FN_MAX, px->fe_counters.conn_max);
1410 stats[ST_F_SLIM] = mkf_u32(FO_CONFIG|FN_LIMIT, px->maxconn);
1411 stats[ST_F_STOT] = mkf_u64(FN_COUNTER, px->fe_counters.cum_sess);
1412 stats[ST_F_BIN] = mkf_u64(FN_COUNTER, px->fe_counters.bytes_in);
1413 stats[ST_F_BOUT] = mkf_u64(FN_COUNTER, px->fe_counters.bytes_out);
1414 stats[ST_F_DREQ] = mkf_u64(FN_COUNTER, px->fe_counters.denied_req);
1415 stats[ST_F_DRESP] = mkf_u64(FN_COUNTER, px->fe_counters.denied_resp);
1416 stats[ST_F_EREQ] = mkf_u64(FN_COUNTER, px->fe_counters.failed_req);
1417 stats[ST_F_DCON] = mkf_u64(FN_COUNTER, px->fe_counters.denied_conn);
1418 stats[ST_F_DSES] = mkf_u64(FN_COUNTER, px->fe_counters.denied_sess);
1419 stats[ST_F_STATUS] = mkf_str(FO_STATUS, px->state == PR_STREADY ? "OPEN" : px->state == PR_STFULL ? "FULL" : "STOP");
1420 stats[ST_F_PID] = mkf_u32(FO_KEY, relative_pid);
1421 stats[ST_F_IID] = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
1422 stats[ST_F_SID] = mkf_u32(FO_KEY|FS_SERVICE, 0);
1423 stats[ST_F_TYPE] = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_FE);
1424 stats[ST_F_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&px->fe_sess_per_sec));
1425 stats[ST_F_RATE_LIM] = mkf_u32(FO_CONFIG|FN_LIMIT, px->fe_sps_lim);
1426 stats[ST_F_RATE_MAX] = mkf_u32(FN_MAX, px->fe_counters.sps_max);
Tim Duesterhus3fd19732018-05-27 20:35:08 +02001427 stats[ST_F_WREW] = mkf_u64(FN_COUNTER, px->fe_counters.failed_rewrites);
Christopher Faulet0159ee42019-12-16 14:40:39 +01001428 stats[ST_F_EINT] = mkf_u64(FN_COUNTER, px->fe_counters.internal_errors);
William Lallemand74c24fb2016-11-21 17:18:36 +01001429
1430 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
1431 if (px->mode == PR_MODE_HTTP) {
1432 stats[ST_F_HRSP_1XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[1]);
1433 stats[ST_F_HRSP_2XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[2]);
1434 stats[ST_F_HRSP_3XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[3]);
1435 stats[ST_F_HRSP_4XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[4]);
1436 stats[ST_F_HRSP_5XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[5]);
1437 stats[ST_F_HRSP_OTHER] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[0]);
1438 stats[ST_F_INTERCEPTED] = mkf_u64(FN_COUNTER, px->fe_counters.intercepted_req);
Willy Tarreaua1214a52018-12-14 14:00:25 +01001439 stats[ST_F_CACHE_LOOKUPS] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.cache_lookups);
1440 stats[ST_F_CACHE_HITS] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.cache_hits);
William Lallemand74c24fb2016-11-21 17:18:36 +01001441 }
1442
1443 /* requests : req_rate, req_rate_max, req_tot, */
1444 stats[ST_F_REQ_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&px->fe_req_per_sec));
1445 stats[ST_F_REQ_RATE_MAX] = mkf_u32(FN_MAX, px->fe_counters.p.http.rps_max);
1446 stats[ST_F_REQ_TOT] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.cum_req);
1447
1448 /* compression: in, out, bypassed, responses */
1449 stats[ST_F_COMP_IN] = mkf_u64(FN_COUNTER, px->fe_counters.comp_in);
1450 stats[ST_F_COMP_OUT] = mkf_u64(FN_COUNTER, px->fe_counters.comp_out);
1451 stats[ST_F_COMP_BYP] = mkf_u64(FN_COUNTER, px->fe_counters.comp_byp);
1452 stats[ST_F_COMP_RSP] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.comp_rsp);
1453
1454 /* connections : conn_rate, conn_rate_max, conn_tot, conn_max */
1455 stats[ST_F_CONN_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&px->fe_conn_per_sec));
1456 stats[ST_F_CONN_RATE_MAX] = mkf_u32(FN_MAX, px->fe_counters.cps_max);
1457 stats[ST_F_CONN_TOT] = mkf_u64(FN_COUNTER, px->fe_counters.cum_conn);
1458
1459 return 1;
1460}
1461
1462/* Dumps a frontend's line to the trash for the current proxy <px> and uses
1463 * the state from stream interface <si>. The caller is responsible for clearing
1464 * the trash if needed. Returns non-zero if it emits anything, zero otherwise.
1465 */
1466static int stats_dump_fe_stats(struct stream_interface *si, struct proxy *px)
1467{
1468 struct appctx *appctx = __objt_appctx(si->end);
1469
1470 if (!(px->cap & PR_CAP_FE))
1471 return 0;
1472
1473 if ((appctx->ctx.stats.flags & STAT_BOUND) && !(appctx->ctx.stats.type & (1 << STATS_TYPE_FE)))
1474 return 0;
1475
1476 if (!stats_fill_fe_stats(px, stats, ST_F_TOTAL_FIELDS))
1477 return 0;
1478
Willy Tarreau43241ff2019-10-09 11:27:51 +02001479 return stats_dump_one_line(stats, px, appctx);
William Lallemand74c24fb2016-11-21 17:18:36 +01001480}
1481
1482/* Fill <stats> with the listener statistics. <stats> is
1483 * preallocated array of length <len>. The length of the array
1484 * must be at least ST_F_TOTAL_FIELDS. If this length is less
1485 * then this value, the function returns 0, otherwise, it
Willy Tarreau708c4162019-10-09 10:19:16 +02001486 * returns 1. <flags> can take the value STAT_SHLGNDS.
William Lallemand74c24fb2016-11-21 17:18:36 +01001487 */
1488int stats_fill_li_stats(struct proxy *px, struct listener *l, int flags,
1489 struct field *stats, int len)
1490{
Willy Tarreau83061a82018-07-13 11:56:34 +02001491 struct buffer *out = get_trash_chunk();
William Lallemand74c24fb2016-11-21 17:18:36 +01001492
1493 if (len < ST_F_TOTAL_FIELDS)
1494 return 0;
1495
1496 if (!l->counters)
1497 return 0;
1498
1499 chunk_reset(out);
1500 memset(stats, 0, sizeof(*stats) * len);
1501
1502 stats[ST_F_PXNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
1503 stats[ST_F_SVNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, l->name);
1504 stats[ST_F_MODE] = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
1505 stats[ST_F_SCUR] = mkf_u32(0, l->nbconn);
1506 stats[ST_F_SMAX] = mkf_u32(FN_MAX, l->counters->conn_max);
1507 stats[ST_F_SLIM] = mkf_u32(FO_CONFIG|FN_LIMIT, l->maxconn);
1508 stats[ST_F_STOT] = mkf_u64(FN_COUNTER, l->counters->cum_conn);
1509 stats[ST_F_BIN] = mkf_u64(FN_COUNTER, l->counters->bytes_in);
1510 stats[ST_F_BOUT] = mkf_u64(FN_COUNTER, l->counters->bytes_out);
1511 stats[ST_F_DREQ] = mkf_u64(FN_COUNTER, l->counters->denied_req);
1512 stats[ST_F_DRESP] = mkf_u64(FN_COUNTER, l->counters->denied_resp);
1513 stats[ST_F_EREQ] = mkf_u64(FN_COUNTER, l->counters->failed_req);
1514 stats[ST_F_DCON] = mkf_u64(FN_COUNTER, l->counters->denied_conn);
1515 stats[ST_F_DSES] = mkf_u64(FN_COUNTER, l->counters->denied_sess);
Willy Tarreaua8cf66b2019-02-27 16:49:00 +01001516 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 +01001517 stats[ST_F_PID] = mkf_u32(FO_KEY, relative_pid);
1518 stats[ST_F_IID] = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
1519 stats[ST_F_SID] = mkf_u32(FO_KEY|FS_SERVICE, l->luid);
1520 stats[ST_F_TYPE] = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_SO);
Tim Duesterhus3fd19732018-05-27 20:35:08 +02001521 stats[ST_F_WREW] = mkf_u64(FN_COUNTER, l->counters->failed_rewrites);
Christopher Faulet0159ee42019-12-16 14:40:39 +01001522 stats[ST_F_EINT] = mkf_u64(FN_COUNTER, l->counters->internal_errors);
William Lallemand74c24fb2016-11-21 17:18:36 +01001523
Willy Tarreau708c4162019-10-09 10:19:16 +02001524 if (flags & STAT_SHLGNDS) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001525 char str[INET6_ADDRSTRLEN];
1526 int port;
1527
1528 port = get_host_port(&l->addr);
1529 switch (addr_to_str(&l->addr, str, sizeof(str))) {
1530 case AF_INET:
1531 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
1532 chunk_appendf(out, "%s:%d", str, port);
1533 break;
1534 case AF_INET6:
1535 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
1536 chunk_appendf(out, "[%s]:%d", str, port);
1537 break;
1538 case AF_UNIX:
1539 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, "unix");
1540 break;
1541 case -1:
1542 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
1543 chunk_strcat(out, strerror(errno));
1544 break;
1545 default: /* address family not supported */
1546 break;
1547 }
1548 }
1549
1550 return 1;
1551}
1552
1553/* Dumps a line for listener <l> and proxy <px> to the trash and uses the state
Willy Tarreau43241ff2019-10-09 11:27:51 +02001554 * from stream interface <si>. The caller is responsible for clearing the trash
1555 * if needed. Returns non-zero if it emits anything, zero otherwise.
William Lallemand74c24fb2016-11-21 17:18:36 +01001556 */
Willy Tarreau43241ff2019-10-09 11:27:51 +02001557static int stats_dump_li_stats(struct stream_interface *si, struct proxy *px, struct listener *l)
William Lallemand74c24fb2016-11-21 17:18:36 +01001558{
1559 struct appctx *appctx = __objt_appctx(si->end);
1560
Willy Tarreau43241ff2019-10-09 11:27:51 +02001561 if (!stats_fill_li_stats(px, l, appctx->ctx.stats.flags, stats, ST_F_TOTAL_FIELDS))
William Lallemand74c24fb2016-11-21 17:18:36 +01001562 return 0;
1563
Willy Tarreau43241ff2019-10-09 11:27:51 +02001564 return stats_dump_one_line(stats, px, appctx);
William Lallemand74c24fb2016-11-21 17:18:36 +01001565}
1566
1567enum srv_stats_state {
1568 SRV_STATS_STATE_DOWN = 0,
1569 SRV_STATS_STATE_DOWN_AGENT,
1570 SRV_STATS_STATE_GOING_UP,
1571 SRV_STATS_STATE_UP_GOING_DOWN,
1572 SRV_STATS_STATE_UP,
1573 SRV_STATS_STATE_NOLB_GOING_DOWN,
1574 SRV_STATS_STATE_NOLB,
1575 SRV_STATS_STATE_DRAIN_GOING_DOWN,
1576 SRV_STATS_STATE_DRAIN,
1577 SRV_STATS_STATE_DRAIN_AGENT,
1578 SRV_STATS_STATE_NO_CHECK,
1579
1580 SRV_STATS_STATE_COUNT, /* Must be last */
1581};
1582
1583static const char *srv_hlt_st[SRV_STATS_STATE_COUNT] = {
1584 [SRV_STATS_STATE_DOWN] = "DOWN",
1585 [SRV_STATS_STATE_DOWN_AGENT] = "DOWN (agent)",
1586 [SRV_STATS_STATE_GOING_UP] = "DOWN %d/%d",
1587 [SRV_STATS_STATE_UP_GOING_DOWN] = "UP %d/%d",
1588 [SRV_STATS_STATE_UP] = "UP",
1589 [SRV_STATS_STATE_NOLB_GOING_DOWN] = "NOLB %d/%d",
1590 [SRV_STATS_STATE_NOLB] = "NOLB",
1591 [SRV_STATS_STATE_DRAIN_GOING_DOWN] = "DRAIN %d/%d",
1592 [SRV_STATS_STATE_DRAIN] = "DRAIN",
1593 [SRV_STATS_STATE_DRAIN_AGENT] = "DRAIN (agent)",
1594 [SRV_STATS_STATE_NO_CHECK] = "no check"
1595};
1596
1597/* Fill <stats> with the server statistics. <stats> is
1598 * preallocated array of length <len>. The length of the array
1599 * must be at least ST_F_TOTAL_FIELDS. If this length is less
1600 * then this value, the function returns 0, otherwise, it
Willy Tarreau708c4162019-10-09 10:19:16 +02001601 * returns 1. <flags> can take the value STAT_SHLGNDS.
William Lallemand74c24fb2016-11-21 17:18:36 +01001602 */
1603int stats_fill_sv_stats(struct proxy *px, struct server *sv, int flags,
1604 struct field *stats, int len)
1605{
1606 struct server *via, *ref;
1607 char str[INET6_ADDRSTRLEN];
Willy Tarreau83061a82018-07-13 11:56:34 +02001608 struct buffer *out = get_trash_chunk();
William Lallemand74c24fb2016-11-21 17:18:36 +01001609 enum srv_stats_state state;
1610 char *fld_status;
Marcin Deraneka8dbdf32020-05-15 20:02:40 +02001611 long long srv_samples_counter;
1612 unsigned int srv_samples_window = TIME_STATS_SAMPLES;
William Lallemand74c24fb2016-11-21 17:18:36 +01001613
1614 if (len < ST_F_TOTAL_FIELDS)
1615 return 0;
1616
1617 memset(stats, 0, sizeof(*stats) * len);
1618
1619 /* we have "via" which is the tracked server as described in the configuration,
1620 * and "ref" which is the checked server and the end of the chain.
1621 */
1622 via = sv->track ? sv->track : sv;
1623 ref = via;
1624 while (ref->track)
1625 ref = ref->track;
1626
Emeric Brun52a91d32017-08-31 14:41:55 +02001627 if (sv->cur_state == SRV_ST_RUNNING || sv->cur_state == SRV_ST_STARTING) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001628 if ((ref->check.state & CHK_ST_ENABLED) &&
1629 (ref->check.health < ref->check.rise + ref->check.fall - 1)) {
1630 state = SRV_STATS_STATE_UP_GOING_DOWN;
1631 } else {
1632 state = SRV_STATS_STATE_UP;
1633 }
1634
Emeric Brun52a91d32017-08-31 14:41:55 +02001635 if (sv->cur_admin & SRV_ADMF_DRAIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001636 if (ref->agent.state & CHK_ST_ENABLED)
1637 state = SRV_STATS_STATE_DRAIN_AGENT;
1638 else if (state == SRV_STATS_STATE_UP_GOING_DOWN)
1639 state = SRV_STATS_STATE_DRAIN_GOING_DOWN;
1640 else
1641 state = SRV_STATS_STATE_DRAIN;
1642 }
1643
1644 if (state == SRV_STATS_STATE_UP && !(ref->check.state & CHK_ST_ENABLED)) {
1645 state = SRV_STATS_STATE_NO_CHECK;
1646 }
1647 }
Emeric Brun52a91d32017-08-31 14:41:55 +02001648 else if (sv->cur_state == SRV_ST_STOPPING) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001649 if ((!(sv->check.state & CHK_ST_ENABLED) && !sv->track) ||
1650 (ref->check.health == ref->check.rise + ref->check.fall - 1)) {
1651 state = SRV_STATS_STATE_NOLB;
1652 } else {
1653 state = SRV_STATS_STATE_NOLB_GOING_DOWN;
1654 }
1655 }
1656 else { /* stopped */
1657 if ((ref->agent.state & CHK_ST_ENABLED) && !ref->agent.health) {
1658 state = SRV_STATS_STATE_DOWN_AGENT;
1659 } else if ((ref->check.state & CHK_ST_ENABLED) && !ref->check.health) {
1660 state = SRV_STATS_STATE_DOWN; /* DOWN */
1661 } else if ((ref->agent.state & CHK_ST_ENABLED) || (ref->check.state & CHK_ST_ENABLED)) {
1662 state = SRV_STATS_STATE_GOING_UP;
1663 } else {
1664 state = SRV_STATS_STATE_DOWN; /* DOWN, unchecked */
1665 }
1666 }
1667
1668 chunk_reset(out);
1669
1670 stats[ST_F_PXNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
1671 stats[ST_F_SVNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, sv->id);
1672 stats[ST_F_MODE] = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
1673 stats[ST_F_QCUR] = mkf_u32(0, sv->nbpend);
1674 stats[ST_F_QMAX] = mkf_u32(FN_MAX, sv->counters.nbpend_max);
1675 stats[ST_F_SCUR] = mkf_u32(0, sv->cur_sess);
1676 stats[ST_F_SMAX] = mkf_u32(FN_MAX, sv->counters.cur_sess_max);
1677
1678 if (sv->maxconn)
1679 stats[ST_F_SLIM] = mkf_u32(FO_CONFIG|FN_LIMIT, sv->maxconn);
1680
Willy Tarreauf21d17b2019-09-08 09:24:56 +02001681 stats[ST_F_SRV_ICUR] = mkf_u32(0, sv->curr_idle_conns);
1682 if (sv->max_idle_conns != -1)
1683 stats[ST_F_SRV_ILIM] = mkf_u32(FO_CONFIG|FN_LIMIT, sv->max_idle_conns);
1684
William Lallemand74c24fb2016-11-21 17:18:36 +01001685 stats[ST_F_STOT] = mkf_u64(FN_COUNTER, sv->counters.cum_sess);
1686 stats[ST_F_BIN] = mkf_u64(FN_COUNTER, sv->counters.bytes_in);
1687 stats[ST_F_BOUT] = mkf_u64(FN_COUNTER, sv->counters.bytes_out);
Christopher Fauleta08546b2019-12-16 16:07:34 +01001688 stats[ST_F_DRESP] = mkf_u64(FN_COUNTER, sv->counters.denied_resp);
William Lallemand74c24fb2016-11-21 17:18:36 +01001689 stats[ST_F_ECON] = mkf_u64(FN_COUNTER, sv->counters.failed_conns);
1690 stats[ST_F_ERESP] = mkf_u64(FN_COUNTER, sv->counters.failed_resp);
1691 stats[ST_F_WRETR] = mkf_u64(FN_COUNTER, sv->counters.retries);
1692 stats[ST_F_WREDIS] = mkf_u64(FN_COUNTER, sv->counters.redispatches);
Tim Duesterhus3fd19732018-05-27 20:35:08 +02001693 stats[ST_F_WREW] = mkf_u64(FN_COUNTER, sv->counters.failed_rewrites);
Christopher Faulet0159ee42019-12-16 14:40:39 +01001694 stats[ST_F_EINT] = mkf_u64(FN_COUNTER, sv->counters.internal_errors);
Willy Tarreauf1573842018-12-14 11:35:36 +01001695 stats[ST_F_CONNECT] = mkf_u64(FN_COUNTER, sv->counters.connect);
1696 stats[ST_F_REUSE] = mkf_u64(FN_COUNTER, sv->counters.reuse);
William Lallemand74c24fb2016-11-21 17:18:36 +01001697
1698 /* status */
1699 fld_status = chunk_newstr(out);
Emeric Brun52a91d32017-08-31 14:41:55 +02001700 if (sv->cur_admin & SRV_ADMF_RMAINT)
William Lallemand74c24fb2016-11-21 17:18:36 +01001701 chunk_appendf(out, "MAINT (resolution)");
Emeric Brun52a91d32017-08-31 14:41:55 +02001702 else if (sv->cur_admin & SRV_ADMF_IMAINT)
William Lallemand74c24fb2016-11-21 17:18:36 +01001703 chunk_appendf(out, "MAINT (via %s/%s)", via->proxy->id, via->id);
Emeric Brun52a91d32017-08-31 14:41:55 +02001704 else if (sv->cur_admin & SRV_ADMF_MAINT)
William Lallemand74c24fb2016-11-21 17:18:36 +01001705 chunk_appendf(out, "MAINT");
1706 else
1707 chunk_appendf(out,
1708 srv_hlt_st[state],
Emeric Brun52a91d32017-08-31 14:41:55 +02001709 (ref->cur_state != SRV_ST_STOPPED) ? (ref->check.health - ref->check.rise + 1) : (ref->check.health),
1710 (ref->cur_state != SRV_ST_STOPPED) ? (ref->check.fall) : (ref->check.rise));
William Lallemand74c24fb2016-11-21 17:18:36 +01001711
1712 stats[ST_F_STATUS] = mkf_str(FO_STATUS, fld_status);
1713 stats[ST_F_LASTCHG] = mkf_u32(FN_AGE, now.tv_sec - sv->last_change);
Emeric Brun52a91d32017-08-31 14:41:55 +02001714 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 +01001715 stats[ST_F_ACT] = mkf_u32(FO_STATUS, (sv->flags & SRV_F_BACKUP) ? 0 : 1);
1716 stats[ST_F_BCK] = mkf_u32(FO_STATUS, (sv->flags & SRV_F_BACKUP) ? 1 : 0);
1717
1718 /* check failures: unique, fatal; last change, total downtime */
1719 if (sv->check.state & CHK_ST_ENABLED) {
1720 stats[ST_F_CHKFAIL] = mkf_u64(FN_COUNTER, sv->counters.failed_checks);
1721 stats[ST_F_CHKDOWN] = mkf_u64(FN_COUNTER, sv->counters.down_trans);
1722 stats[ST_F_DOWNTIME] = mkf_u32(FN_COUNTER, srv_downtime(sv));
1723 }
1724
1725 if (sv->maxqueue)
1726 stats[ST_F_QLIMIT] = mkf_u32(FO_CONFIG|FS_SERVICE, sv->maxqueue);
1727
1728 stats[ST_F_PID] = mkf_u32(FO_KEY, relative_pid);
1729 stats[ST_F_IID] = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
1730 stats[ST_F_SID] = mkf_u32(FO_KEY|FS_SERVICE, sv->puid);
1731
Emeric Brun52a91d32017-08-31 14:41:55 +02001732 if (sv->cur_state == SRV_ST_STARTING && !server_is_draining(sv))
William Lallemand74c24fb2016-11-21 17:18:36 +01001733 stats[ST_F_THROTTLE] = mkf_u32(FN_AVG, server_throttle_rate(sv));
1734
1735 stats[ST_F_LBTOT] = mkf_u64(FN_COUNTER, sv->counters.cum_lbconn);
1736
1737 if (sv->track) {
1738 char *fld_track = chunk_newstr(out);
1739
1740 chunk_appendf(out, "%s/%s", sv->track->proxy->id, sv->track->id);
1741 stats[ST_F_TRACKED] = mkf_str(FO_CONFIG|FN_NAME|FS_SERVICE, fld_track);
1742 }
1743
1744 stats[ST_F_TYPE] = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_SV);
1745 stats[ST_F_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&sv->sess_per_sec));
1746 stats[ST_F_RATE_MAX] = mkf_u32(FN_MAX, sv->counters.sps_max);
1747
1748 if ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED) {
1749 const char *fld_chksts;
1750
1751 fld_chksts = chunk_newstr(out);
1752 chunk_strcat(out, "* "); // for check in progress
1753 chunk_strcat(out, get_check_status_info(sv->check.status));
1754 if (!(sv->check.state & CHK_ST_INPROGRESS))
1755 fld_chksts += 2; // skip "* "
1756 stats[ST_F_CHECK_STATUS] = mkf_str(FN_OUTPUT, fld_chksts);
1757
1758 if (sv->check.status >= HCHK_STATUS_L57DATA)
1759 stats[ST_F_CHECK_CODE] = mkf_u32(FN_OUTPUT, sv->check.code);
1760
1761 if (sv->check.status >= HCHK_STATUS_CHECKED)
1762 stats[ST_F_CHECK_DURATION] = mkf_u64(FN_DURATION, sv->check.duration);
1763
1764 stats[ST_F_CHECK_DESC] = mkf_str(FN_OUTPUT, get_check_status_description(sv->check.status));
1765 stats[ST_F_LAST_CHK] = mkf_str(FN_OUTPUT, sv->check.desc);
1766 stats[ST_F_CHECK_RISE] = mkf_u32(FO_CONFIG|FS_SERVICE, ref->check.rise);
1767 stats[ST_F_CHECK_FALL] = mkf_u32(FO_CONFIG|FS_SERVICE, ref->check.fall);
1768 stats[ST_F_CHECK_HEALTH] = mkf_u32(FO_CONFIG|FS_SERVICE, ref->check.health);
1769 }
1770
1771 if ((sv->agent.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED) {
1772 const char *fld_chksts;
1773
1774 fld_chksts = chunk_newstr(out);
1775 chunk_strcat(out, "* "); // for check in progress
1776 chunk_strcat(out, get_check_status_info(sv->agent.status));
1777 if (!(sv->agent.state & CHK_ST_INPROGRESS))
1778 fld_chksts += 2; // skip "* "
1779 stats[ST_F_AGENT_STATUS] = mkf_str(FN_OUTPUT, fld_chksts);
1780
1781 if (sv->agent.status >= HCHK_STATUS_L57DATA)
1782 stats[ST_F_AGENT_CODE] = mkf_u32(FN_OUTPUT, sv->agent.code);
1783
1784 if (sv->agent.status >= HCHK_STATUS_CHECKED)
1785 stats[ST_F_AGENT_DURATION] = mkf_u64(FN_DURATION, sv->agent.duration);
1786
1787 stats[ST_F_AGENT_DESC] = mkf_str(FN_OUTPUT, get_check_status_description(sv->agent.status));
1788 stats[ST_F_LAST_AGT] = mkf_str(FN_OUTPUT, sv->agent.desc);
1789 stats[ST_F_AGENT_RISE] = mkf_u32(FO_CONFIG|FS_SERVICE, sv->agent.rise);
1790 stats[ST_F_AGENT_FALL] = mkf_u32(FO_CONFIG|FS_SERVICE, sv->agent.fall);
1791 stats[ST_F_AGENT_HEALTH] = mkf_u32(FO_CONFIG|FS_SERVICE, sv->agent.health);
1792 }
1793
1794 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
1795 if (px->mode == PR_MODE_HTTP) {
Marcin Deranek3c27dda2020-05-15 18:32:51 +02001796 stats[ST_F_REQ_TOT] = mkf_u64(FN_COUNTER, sv->counters.p.http.cum_req);
William Lallemand74c24fb2016-11-21 17:18:36 +01001797 stats[ST_F_HRSP_1XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[1]);
1798 stats[ST_F_HRSP_2XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[2]);
1799 stats[ST_F_HRSP_3XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[3]);
1800 stats[ST_F_HRSP_4XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[4]);
1801 stats[ST_F_HRSP_5XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[5]);
1802 stats[ST_F_HRSP_OTHER] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[0]);
1803 }
1804
1805 if (ref->observe)
1806 stats[ST_F_HANAFAIL] = mkf_u64(FN_COUNTER, sv->counters.failed_hana);
1807
1808 stats[ST_F_CLI_ABRT] = mkf_u64(FN_COUNTER, sv->counters.cli_aborts);
1809 stats[ST_F_SRV_ABRT] = mkf_u64(FN_COUNTER, sv->counters.srv_aborts);
1810 stats[ST_F_LASTSESS] = mkf_s32(FN_AGE, srv_lastsession(sv));
1811
Marcin Deraneka8dbdf32020-05-15 20:02:40 +02001812 srv_samples_counter = (px->mode == PR_MODE_HTTP) ? sv->counters.p.http.cum_req : sv->counters.cum_lbconn;
1813 if (srv_samples_counter < TIME_STATS_SAMPLES && srv_samples_counter > 0)
1814 srv_samples_window = srv_samples_counter;
1815
1816 stats[ST_F_QTIME] = mkf_u32(FN_AVG, swrate_avg(sv->counters.q_time, srv_samples_window));
1817 stats[ST_F_CTIME] = mkf_u32(FN_AVG, swrate_avg(sv->counters.c_time, srv_samples_window));
1818 stats[ST_F_RTIME] = mkf_u32(FN_AVG, swrate_avg(sv->counters.d_time, srv_samples_window));
1819 stats[ST_F_TTIME] = mkf_u32(FN_AVG, swrate_avg(sv->counters.t_time, srv_samples_window));
William Lallemand74c24fb2016-11-21 17:18:36 +01001820
Christopher Faulet0d1c2a62019-11-08 14:59:51 +01001821 stats[ST_F_QT_MAX] = mkf_u32(FN_MAX, sv->counters.qtime_max);
1822 stats[ST_F_CT_MAX] = mkf_u32(FN_MAX, sv->counters.ctime_max);
1823 stats[ST_F_RT_MAX] = mkf_u32(FN_MAX, sv->counters.dtime_max);
1824 stats[ST_F_TT_MAX] = mkf_u32(FN_MAX, sv->counters.ttime_max);
1825
Willy Tarreau708c4162019-10-09 10:19:16 +02001826 if (flags & STAT_SHLGNDS) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001827 switch (addr_to_str(&sv->addr, str, sizeof(str))) {
1828 case AF_INET:
1829 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
Willy Tarreau04276f32017-01-06 17:41:29 +01001830 chunk_appendf(out, "%s:%d", str, sv->svc_port);
William Lallemand74c24fb2016-11-21 17:18:36 +01001831 break;
1832 case AF_INET6:
1833 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
Willy Tarreau04276f32017-01-06 17:41:29 +01001834 chunk_appendf(out, "[%s]:%d", str, sv->svc_port);
William Lallemand74c24fb2016-11-21 17:18:36 +01001835 break;
1836 case AF_UNIX:
1837 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, "unix");
1838 break;
1839 case -1:
1840 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
1841 chunk_strcat(out, strerror(errno));
1842 break;
1843 default: /* address family not supported */
1844 break;
1845 }
1846
1847 if (sv->cookie)
1848 stats[ST_F_COOKIE] = mkf_str(FO_CONFIG|FN_NAME|FS_SERVICE, sv->cookie);
1849 }
1850
1851 return 1;
1852}
1853
1854/* Dumps a line for server <sv> and proxy <px> to the trash and uses the state
Willy Tarreau43241ff2019-10-09 11:27:51 +02001855 * from stream interface <si>, and server state <state>. The caller is
1856 * responsible for clearing the trash if needed. Returns non-zero if it emits
1857 * anything, zero otherwise.
William Lallemand74c24fb2016-11-21 17:18:36 +01001858 */
Willy Tarreau43241ff2019-10-09 11:27:51 +02001859static int stats_dump_sv_stats(struct stream_interface *si, struct proxy *px, struct server *sv)
William Lallemand74c24fb2016-11-21 17:18:36 +01001860{
1861 struct appctx *appctx = __objt_appctx(si->end);
1862
Willy Tarreau43241ff2019-10-09 11:27:51 +02001863 if (!stats_fill_sv_stats(px, sv, appctx->ctx.stats.flags, stats, ST_F_TOTAL_FIELDS))
William Lallemand74c24fb2016-11-21 17:18:36 +01001864 return 0;
1865
Willy Tarreau43241ff2019-10-09 11:27:51 +02001866 return stats_dump_one_line(stats, px, appctx);
William Lallemand74c24fb2016-11-21 17:18:36 +01001867}
1868
1869/* Fill <stats> with the backend statistics. <stats> is
1870 * preallocated array of length <len>. The length of the array
1871 * must be at least ST_F_TOTAL_FIELDS. If this length is less
1872 * then this value, the function returns 0, otherwise, it
Willy Tarreau708c4162019-10-09 10:19:16 +02001873 * returns 1. <flags> can take the value STAT_SHLGNDS.
William Lallemand74c24fb2016-11-21 17:18:36 +01001874 */
1875int stats_fill_be_stats(struct proxy *px, int flags, struct field *stats, int len)
1876{
Marcin Deraneka8dbdf32020-05-15 20:02:40 +02001877 long long be_samples_counter;
1878 unsigned int be_samples_window = TIME_STATS_SAMPLES;
1879
William Lallemand74c24fb2016-11-21 17:18:36 +01001880 if (len < ST_F_TOTAL_FIELDS)
1881 return 0;
1882
1883 memset(stats, 0, sizeof(*stats) * len);
1884
1885 stats[ST_F_PXNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
1886 stats[ST_F_SVNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, "BACKEND");
1887 stats[ST_F_MODE] = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
1888 stats[ST_F_QCUR] = mkf_u32(0, px->nbpend);
1889 stats[ST_F_QMAX] = mkf_u32(FN_MAX, px->be_counters.nbpend_max);
Thierry FOURNIER0ff98a42016-12-19 16:50:42 +01001890 stats[ST_F_SCUR] = mkf_u32(0, px->beconn);
William Lallemand74c24fb2016-11-21 17:18:36 +01001891 stats[ST_F_SMAX] = mkf_u32(FN_MAX, px->be_counters.conn_max);
1892 stats[ST_F_SLIM] = mkf_u32(FO_CONFIG|FN_LIMIT, px->fullconn);
1893 stats[ST_F_STOT] = mkf_u64(FN_COUNTER, px->be_counters.cum_conn);
1894 stats[ST_F_BIN] = mkf_u64(FN_COUNTER, px->be_counters.bytes_in);
1895 stats[ST_F_BOUT] = mkf_u64(FN_COUNTER, px->be_counters.bytes_out);
1896 stats[ST_F_DREQ] = mkf_u64(FN_COUNTER, px->be_counters.denied_req);
1897 stats[ST_F_DRESP] = mkf_u64(FN_COUNTER, px->be_counters.denied_resp);
1898 stats[ST_F_ECON] = mkf_u64(FN_COUNTER, px->be_counters.failed_conns);
1899 stats[ST_F_ERESP] = mkf_u64(FN_COUNTER, px->be_counters.failed_resp);
1900 stats[ST_F_WRETR] = mkf_u64(FN_COUNTER, px->be_counters.retries);
1901 stats[ST_F_WREDIS] = mkf_u64(FN_COUNTER, px->be_counters.redispatches);
Tim Duesterhus3fd19732018-05-27 20:35:08 +02001902 stats[ST_F_WREW] = mkf_u64(FN_COUNTER, px->be_counters.failed_rewrites);
Christopher Faulet0159ee42019-12-16 14:40:39 +01001903 stats[ST_F_EINT] = mkf_u64(FN_COUNTER, px->be_counters.internal_errors);
Willy Tarreauf1573842018-12-14 11:35:36 +01001904 stats[ST_F_CONNECT] = mkf_u64(FN_COUNTER, px->be_counters.connect);
1905 stats[ST_F_REUSE] = mkf_u64(FN_COUNTER, px->be_counters.reuse);
William Lallemand74c24fb2016-11-21 17:18:36 +01001906 stats[ST_F_STATUS] = mkf_str(FO_STATUS, (px->lbprm.tot_weight > 0 || !px->srv) ? "UP" : "DOWN");
1907 stats[ST_F_WEIGHT] = mkf_u32(FN_AVG, (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv);
1908 stats[ST_F_ACT] = mkf_u32(0, px->srv_act);
1909 stats[ST_F_BCK] = mkf_u32(0, px->srv_bck);
1910 stats[ST_F_CHKDOWN] = mkf_u64(FN_COUNTER, px->down_trans);
1911 stats[ST_F_LASTCHG] = mkf_u32(FN_AGE, now.tv_sec - px->last_change);
1912 if (px->srv)
1913 stats[ST_F_DOWNTIME] = mkf_u32(FN_COUNTER, be_downtime(px));
1914
1915 stats[ST_F_PID] = mkf_u32(FO_KEY, relative_pid);
1916 stats[ST_F_IID] = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
1917 stats[ST_F_SID] = mkf_u32(FO_KEY|FS_SERVICE, 0);
1918 stats[ST_F_LBTOT] = mkf_u64(FN_COUNTER, px->be_counters.cum_lbconn);
1919 stats[ST_F_TYPE] = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_BE);
1920 stats[ST_F_RATE] = mkf_u32(0, read_freq_ctr(&px->be_sess_per_sec));
1921 stats[ST_F_RATE_MAX] = mkf_u32(0, px->be_counters.sps_max);
1922
Willy Tarreau708c4162019-10-09 10:19:16 +02001923 if (flags & STAT_SHLGNDS) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001924 if (px->cookie_name)
1925 stats[ST_F_COOKIE] = mkf_str(FO_CONFIG|FN_NAME|FS_SERVICE, px->cookie_name);
1926 stats[ST_F_ALGO] = mkf_str(FO_CONFIG|FS_SERVICE, backend_lb_algo_str(px->lbprm.algo & BE_LB_ALGO));
1927 }
1928
1929 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
1930 if (px->mode == PR_MODE_HTTP) {
1931 stats[ST_F_REQ_TOT] = mkf_u64(FN_COUNTER, px->be_counters.p.http.cum_req);
1932 stats[ST_F_HRSP_1XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[1]);
1933 stats[ST_F_HRSP_2XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[2]);
1934 stats[ST_F_HRSP_3XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[3]);
1935 stats[ST_F_HRSP_4XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[4]);
1936 stats[ST_F_HRSP_5XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[5]);
1937 stats[ST_F_HRSP_OTHER] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[0]);
Willy Tarreaua1214a52018-12-14 14:00:25 +01001938 stats[ST_F_CACHE_LOOKUPS] = mkf_u64(FN_COUNTER, px->be_counters.p.http.cache_lookups);
1939 stats[ST_F_CACHE_HITS] = mkf_u64(FN_COUNTER, px->be_counters.p.http.cache_hits);
William Lallemand74c24fb2016-11-21 17:18:36 +01001940 }
1941
1942 stats[ST_F_CLI_ABRT] = mkf_u64(FN_COUNTER, px->be_counters.cli_aborts);
1943 stats[ST_F_SRV_ABRT] = mkf_u64(FN_COUNTER, px->be_counters.srv_aborts);
1944
1945 /* compression: in, out, bypassed, responses */
1946 stats[ST_F_COMP_IN] = mkf_u64(FN_COUNTER, px->be_counters.comp_in);
1947 stats[ST_F_COMP_OUT] = mkf_u64(FN_COUNTER, px->be_counters.comp_out);
1948 stats[ST_F_COMP_BYP] = mkf_u64(FN_COUNTER, px->be_counters.comp_byp);
1949 stats[ST_F_COMP_RSP] = mkf_u64(FN_COUNTER, px->be_counters.p.http.comp_rsp);
1950 stats[ST_F_LASTSESS] = mkf_s32(FN_AGE, be_lastsession(px));
1951
Marcin Deraneka8dbdf32020-05-15 20:02:40 +02001952 be_samples_counter = (px->mode == PR_MODE_HTTP) ? px->be_counters.p.http.cum_req : px->be_counters.cum_lbconn;
1953 if (be_samples_counter < TIME_STATS_SAMPLES && be_samples_counter > 0)
1954 be_samples_window = be_samples_counter;
1955
1956 stats[ST_F_QTIME] = mkf_u32(FN_AVG, swrate_avg(px->be_counters.q_time, be_samples_window));
1957 stats[ST_F_CTIME] = mkf_u32(FN_AVG, swrate_avg(px->be_counters.c_time, be_samples_window));
1958 stats[ST_F_RTIME] = mkf_u32(FN_AVG, swrate_avg(px->be_counters.d_time, be_samples_window));
1959 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 +01001960
Christopher Faulet0d1c2a62019-11-08 14:59:51 +01001961 stats[ST_F_QT_MAX] = mkf_u32(FN_MAX, px->be_counters.qtime_max);
1962 stats[ST_F_CT_MAX] = mkf_u32(FN_MAX, px->be_counters.ctime_max);
1963 stats[ST_F_RT_MAX] = mkf_u32(FN_MAX, px->be_counters.dtime_max);
1964 stats[ST_F_TT_MAX] = mkf_u32(FN_MAX, px->be_counters.ttime_max);
1965
William Lallemand74c24fb2016-11-21 17:18:36 +01001966 return 1;
1967}
1968
1969/* Dumps a line for backend <px> to the trash for and uses the state from stream
Willy Tarreau43241ff2019-10-09 11:27:51 +02001970 * interface <si>. The caller is responsible for clearing the trash if needed.
1971 * Returns non-zero if it emits anything, zero otherwise.
William Lallemand74c24fb2016-11-21 17:18:36 +01001972 */
Willy Tarreau43241ff2019-10-09 11:27:51 +02001973static int stats_dump_be_stats(struct stream_interface *si, struct proxy *px)
William Lallemand74c24fb2016-11-21 17:18:36 +01001974{
1975 struct appctx *appctx = __objt_appctx(si->end);
1976
1977 if (!(px->cap & PR_CAP_BE))
1978 return 0;
1979
1980 if ((appctx->ctx.stats.flags & STAT_BOUND) && !(appctx->ctx.stats.type & (1 << STATS_TYPE_BE)))
1981 return 0;
1982
Willy Tarreau43241ff2019-10-09 11:27:51 +02001983 if (!stats_fill_be_stats(px, appctx->ctx.stats.flags, stats, ST_F_TOTAL_FIELDS))
William Lallemand74c24fb2016-11-21 17:18:36 +01001984 return 0;
1985
Willy Tarreau43241ff2019-10-09 11:27:51 +02001986 return stats_dump_one_line(stats, px, appctx);
William Lallemand74c24fb2016-11-21 17:18:36 +01001987}
1988
1989/* Dumps the HTML table header for proxy <px> to the trash for and uses the state from
1990 * stream interface <si> and per-uri parameters <uri>. The caller is responsible
1991 * for clearing the trash if needed.
1992 */
Willy Tarreau676c29e2019-10-09 10:50:01 +02001993static void stats_dump_html_px_hdr(struct stream_interface *si, struct proxy *px)
William Lallemand74c24fb2016-11-21 17:18:36 +01001994{
1995 struct appctx *appctx = __objt_appctx(si->end);
1996 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
1997
1998 if (px->cap & PR_CAP_BE && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
1999 /* A form to enable/disable this proxy servers */
2000
2001 /* scope_txt = search pattern + search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
2002 scope_txt[0] = 0;
2003 if (appctx->ctx.stats.scope_len) {
Christopher Fauleted7a0662019-01-14 11:07:34 +01002004 const char *scope_ptr = stats_scope_ptr(appctx, si);
2005
William Lallemand74c24fb2016-11-21 17:18:36 +01002006 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Christopher Fauleted7a0662019-01-14 11:07:34 +01002007 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), scope_ptr, appctx->ctx.stats.scope_len);
William Lallemand74c24fb2016-11-21 17:18:36 +01002008 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
2009 }
2010
2011 chunk_appendf(&trash,
2012 "<form method=\"post\">");
2013 }
2014
2015 /* print a new table */
2016 chunk_appendf(&trash,
2017 "<table class=\"tbl\" width=\"100%%\">\n"
2018 "<tr class=\"titre\">"
2019 "<th class=\"pxname\" width=\"10%%\">");
2020
2021 chunk_appendf(&trash,
2022 "<a name=\"%s\"></a>%s"
2023 "<a class=px href=\"#%s\">%s</a>",
2024 px->id,
Willy Tarreau676c29e2019-10-09 10:50:01 +02002025 (appctx->ctx.stats.flags & STAT_SHLGNDS) ? "<u>":"",
William Lallemand74c24fb2016-11-21 17:18:36 +01002026 px->id, px->id);
2027
Willy Tarreau676c29e2019-10-09 10:50:01 +02002028 if (appctx->ctx.stats.flags & STAT_SHLGNDS) {
William Lallemand74c24fb2016-11-21 17:18:36 +01002029 /* cap, mode, id */
2030 chunk_appendf(&trash, "<div class=tips>cap: %s, mode: %s, id: %d",
2031 proxy_cap_str(px->cap), proxy_mode_str(px->mode),
2032 px->uuid);
2033 chunk_appendf(&trash, "</div>");
2034 }
2035
2036 chunk_appendf(&trash,
2037 "%s</th>"
2038 "<th class=\"%s\" width=\"90%%\">%s</th>"
2039 "</tr>\n"
2040 "</table>\n"
2041 "<table class=\"tbl\" width=\"100%%\">\n"
2042 "<tr class=\"titre\">",
Willy Tarreau676c29e2019-10-09 10:50:01 +02002043 (appctx->ctx.stats.flags & STAT_SHLGNDS) ? "</u>":"",
William Lallemand74c24fb2016-11-21 17:18:36 +01002044 px->desc ? "desc" : "empty", px->desc ? px->desc : "");
2045
Christopher Fauletbc271ec2019-12-02 11:29:04 +01002046 if (appctx->ctx.stats.flags & STAT_ADMIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +01002047 /* Column heading for Enable or Disable server */
Christopher Fauletbc271ec2019-12-02 11:29:04 +01002048 if ((px->cap & PR_CAP_BE) && px->srv)
2049 chunk_appendf(&trash,
2050 "<th rowspan=2 width=1><input type=\"checkbox\" "
2051 "onclick=\"for(c in document.getElementsByClassName('%s-checkbox')) "
2052 "document.getElementsByClassName('%s-checkbox').item(c).checked = this.checked\"></th>",
2053 px->id,
2054 px->id);
2055 else
2056 chunk_appendf(&trash, "<th rowspan=2></th>");
William Lallemand74c24fb2016-11-21 17:18:36 +01002057 }
2058
2059 chunk_appendf(&trash,
2060 "<th rowspan=2></th>"
2061 "<th colspan=3>Queue</th>"
2062 "<th colspan=3>Session rate</th><th colspan=6>Sessions</th>"
2063 "<th colspan=2>Bytes</th><th colspan=2>Denied</th>"
2064 "<th colspan=3>Errors</th><th colspan=2>Warnings</th>"
2065 "<th colspan=9>Server</th>"
2066 "</tr>\n"
2067 "<tr class=\"titre\">"
2068 "<th>Cur</th><th>Max</th><th>Limit</th>"
2069 "<th>Cur</th><th>Max</th><th>Limit</th><th>Cur</th><th>Max</th>"
2070 "<th>Limit</th><th>Total</th><th>LbTot</th><th>Last</th><th>In</th><th>Out</th>"
2071 "<th>Req</th><th>Resp</th><th>Req</th><th>Conn</th>"
2072 "<th>Resp</th><th>Retr</th><th>Redis</th>"
2073 "<th>Status</th><th>LastChk</th><th>Wght</th><th>Act</th>"
2074 "<th>Bck</th><th>Chk</th><th>Dwn</th><th>Dwntme</th>"
2075 "<th>Thrtle</th>\n"
2076 "</tr>");
2077}
2078
2079/* Dumps the HTML table trailer for proxy <px> to the trash for and uses the state from
2080 * stream interface <si>. The caller is responsible for clearing the trash if needed.
2081 */
2082static void stats_dump_html_px_end(struct stream_interface *si, struct proxy *px)
2083{
2084 struct appctx *appctx = __objt_appctx(si->end);
2085 chunk_appendf(&trash, "</table>");
2086
2087 if ((px->cap & PR_CAP_BE) && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
2088 /* close the form used to enable/disable this proxy servers */
2089 chunk_appendf(&trash,
2090 "Choose the action to perform on the checked servers : "
2091 "<select name=action>"
2092 "<option value=\"\"></option>"
2093 "<option value=\"ready\">Set state to READY</option>"
2094 "<option value=\"drain\">Set state to DRAIN</option>"
2095 "<option value=\"maint\">Set state to MAINT</option>"
2096 "<option value=\"dhlth\">Health: disable checks</option>"
2097 "<option value=\"ehlth\">Health: enable checks</option>"
2098 "<option value=\"hrunn\">Health: force UP</option>"
2099 "<option value=\"hnolb\">Health: force NOLB</option>"
2100 "<option value=\"hdown\">Health: force DOWN</option>"
2101 "<option value=\"dagent\">Agent: disable checks</option>"
2102 "<option value=\"eagent\">Agent: enable checks</option>"
2103 "<option value=\"arunn\">Agent: force UP</option>"
2104 "<option value=\"adown\">Agent: force DOWN</option>"
2105 "<option value=\"shutdown\">Kill Sessions</option>"
2106 "</select>"
2107 "<input type=\"hidden\" name=\"b\" value=\"#%d\">"
2108 "&nbsp;<input type=\"submit\" value=\"Apply\">"
2109 "</form>",
2110 px->uuid);
2111 }
2112
2113 chunk_appendf(&trash, "<p>\n");
2114}
2115
2116/*
2117 * Dumps statistics for a proxy. The output is sent to the stream interface's
2118 * input buffer. Returns 0 if it had to stop dumping data because of lack of
2119 * buffer space, or non-zero if everything completed. This function is used
2120 * both by the CLI and the HTTP entry points, and is able to dump the output
2121 * in HTML or CSV formats. If the later, <uri> must be NULL.
2122 */
Christopher Fauletef779222018-10-31 08:47:01 +01002123int stats_dump_proxy_to_buffer(struct stream_interface *si, struct htx *htx,
2124 struct proxy *px, struct uri_auth *uri)
William Lallemand74c24fb2016-11-21 17:18:36 +01002125{
2126 struct appctx *appctx = __objt_appctx(si->end);
2127 struct stream *s = si_strm(si);
2128 struct channel *rep = si_ic(si);
2129 struct server *sv, *svs; /* server and server-state, server-state=server or server->track */
2130 struct listener *l;
William Lallemand74c24fb2016-11-21 17:18:36 +01002131
2132 chunk_reset(&trash);
2133
2134 switch (appctx->ctx.stats.px_st) {
2135 case STAT_PX_ST_INIT:
2136 /* we are on a new proxy */
2137 if (uri && uri->scope) {
2138 /* we have a limited scope, we have to check the proxy name */
2139 struct stat_scope *scope;
2140 int len;
2141
2142 len = strlen(px->id);
2143 scope = uri->scope;
2144
2145 while (scope) {
2146 /* match exact proxy name */
2147 if (scope->px_len == len && !memcmp(px->id, scope->px_id, len))
2148 break;
2149
2150 /* match '.' which means 'self' proxy */
2151 if (!strcmp(scope->px_id, ".") && px == s->be)
2152 break;
2153 scope = scope->next;
2154 }
2155
2156 /* proxy name not found : don't dump anything */
2157 if (scope == NULL)
2158 return 1;
2159 }
2160
2161 /* if the user has requested a limited output and the proxy
2162 * name does not match, skip it.
2163 */
Christopher Fauleted7a0662019-01-14 11:07:34 +01002164 if (appctx->ctx.stats.scope_len) {
2165 const char *scope_ptr = stats_scope_ptr(appctx, si);
2166
2167 if (strnistr(px->id, strlen(px->id), scope_ptr, appctx->ctx.stats.scope_len) == NULL)
2168 return 1;
2169 }
William Lallemand74c24fb2016-11-21 17:18:36 +01002170
2171 if ((appctx->ctx.stats.flags & STAT_BOUND) &&
2172 (appctx->ctx.stats.iid != -1) &&
2173 (px->uuid != appctx->ctx.stats.iid))
2174 return 1;
2175
2176 appctx->ctx.stats.px_st = STAT_PX_ST_TH;
2177 /* fall through */
2178
2179 case STAT_PX_ST_TH:
2180 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreau676c29e2019-10-09 10:50:01 +02002181 stats_dump_html_px_hdr(si, px);
Christopher Fauletef779222018-10-31 08:47:01 +01002182 if (!stats_putchk(rep, htx, &trash))
2183 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002184 }
2185
2186 appctx->ctx.stats.px_st = STAT_PX_ST_FE;
2187 /* fall through */
2188
2189 case STAT_PX_ST_FE:
2190 /* print the frontend */
2191 if (stats_dump_fe_stats(si, px)) {
Christopher Fauletef779222018-10-31 08:47:01 +01002192 if (!stats_putchk(rep, htx, &trash))
2193 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002194 }
2195
2196 appctx->ctx.stats.l = px->conf.listeners.n;
2197 appctx->ctx.stats.px_st = STAT_PX_ST_LI;
2198 /* fall through */
2199
2200 case STAT_PX_ST_LI:
2201 /* stats.l has been initialized above */
2202 for (; appctx->ctx.stats.l != &px->conf.listeners; appctx->ctx.stats.l = l->by_fe.n) {
Christopher Fauletef779222018-10-31 08:47:01 +01002203 if (htx) {
2204 if (htx_almost_full(htx))
2205 goto full;
2206 }
2207 else {
2208 if (buffer_almost_full(&rep->buf))
2209 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002210 }
2211
2212 l = LIST_ELEM(appctx->ctx.stats.l, struct listener *, by_fe);
2213 if (!l->counters)
2214 continue;
2215
2216 if (appctx->ctx.stats.flags & STAT_BOUND) {
2217 if (!(appctx->ctx.stats.type & (1 << STATS_TYPE_SO)))
2218 break;
2219
2220 if (appctx->ctx.stats.sid != -1 && l->luid != appctx->ctx.stats.sid)
2221 continue;
2222 }
2223
2224 /* print the frontend */
Willy Tarreau43241ff2019-10-09 11:27:51 +02002225 if (stats_dump_li_stats(si, px, l)) {
Christopher Fauletef779222018-10-31 08:47:01 +01002226 if (!stats_putchk(rep, htx, &trash))
2227 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002228 }
2229 }
2230
2231 appctx->ctx.stats.sv = px->srv; /* may be NULL */
2232 appctx->ctx.stats.px_st = STAT_PX_ST_SV;
2233 /* fall through */
2234
2235 case STAT_PX_ST_SV:
2236 /* stats.sv has been initialized above */
2237 for (; appctx->ctx.stats.sv != NULL; appctx->ctx.stats.sv = sv->next) {
Christopher Fauletef779222018-10-31 08:47:01 +01002238 if (htx) {
2239 if (htx_almost_full(htx))
2240 goto full;
2241 }
2242 else {
2243 if (buffer_almost_full(&rep->buf))
2244 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002245 }
2246
2247 sv = appctx->ctx.stats.sv;
2248
2249 if (appctx->ctx.stats.flags & STAT_BOUND) {
2250 if (!(appctx->ctx.stats.type & (1 << STATS_TYPE_SV)))
2251 break;
2252
2253 if (appctx->ctx.stats.sid != -1 && sv->puid != appctx->ctx.stats.sid)
2254 continue;
2255 }
2256
2257 svs = sv;
2258 while (svs->track)
2259 svs = svs->track;
2260
2261 /* do not report servers which are DOWN and not changing state */
2262 if ((appctx->ctx.stats.flags & STAT_HIDE_DOWN) &&
Emeric Brun52a91d32017-08-31 14:41:55 +02002263 ((sv->cur_admin & SRV_ADMF_MAINT) || /* server is in maintenance */
2264 (sv->cur_state == SRV_ST_STOPPED && /* server is down */
William Lallemand74c24fb2016-11-21 17:18:36 +01002265 (!((svs->agent.state | svs->check.state) & CHK_ST_ENABLED) ||
2266 ((svs->agent.state & CHK_ST_ENABLED) && !svs->agent.health) ||
2267 ((svs->check.state & CHK_ST_ENABLED) && !svs->check.health))))) {
2268 continue;
2269 }
2270
Willy Tarreau43241ff2019-10-09 11:27:51 +02002271 if (stats_dump_sv_stats(si, px, sv)) {
Christopher Fauletef779222018-10-31 08:47:01 +01002272 if (!stats_putchk(rep, htx, &trash))
2273 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002274 }
2275 } /* for sv */
2276
2277 appctx->ctx.stats.px_st = STAT_PX_ST_BE;
2278 /* fall through */
2279
2280 case STAT_PX_ST_BE:
2281 /* print the backend */
Willy Tarreau43241ff2019-10-09 11:27:51 +02002282 if (stats_dump_be_stats(si, px)) {
Christopher Fauletef779222018-10-31 08:47:01 +01002283 if (!stats_putchk(rep, htx, &trash))
2284 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002285 }
2286
2287 appctx->ctx.stats.px_st = STAT_PX_ST_END;
2288 /* fall through */
2289
2290 case STAT_PX_ST_END:
2291 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
2292 stats_dump_html_px_end(si, px);
Christopher Fauletef779222018-10-31 08:47:01 +01002293 if (!stats_putchk(rep, htx, &trash))
2294 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002295 }
2296
2297 appctx->ctx.stats.px_st = STAT_PX_ST_FIN;
2298 /* fall through */
2299
2300 case STAT_PX_ST_FIN:
2301 return 1;
2302
2303 default:
2304 /* unknown state, we should put an abort() here ! */
2305 return 1;
2306 }
Christopher Fauletef779222018-10-31 08:47:01 +01002307
2308 full:
2309 si_rx_room_blk(si);
2310 return 0;
William Lallemand74c24fb2016-11-21 17:18:36 +01002311}
2312
2313/* Dumps the HTTP stats head block to the trash for and uses the per-uri
2314 * parameters <uri>. The caller is responsible for clearing the trash if needed.
2315 */
Willy Tarreau676c29e2019-10-09 10:50:01 +02002316static void stats_dump_html_head(struct appctx *appctx, struct uri_auth *uri)
William Lallemand74c24fb2016-11-21 17:18:36 +01002317{
2318 /* WARNING! This must fit in the first buffer !!! */
2319 chunk_appendf(&trash,
2320 "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"\n"
2321 "\"http://www.w3.org/TR/html4/loose.dtd\">\n"
2322 "<html><head><title>Statistics Report for " PRODUCT_NAME "%s%s</title>\n"
2323 "<meta http-equiv=\"content-type\" content=\"text/html; charset=iso-8859-1\">\n"
2324 "<style type=\"text/css\"><!--\n"
2325 "body {"
2326 " font-family: arial, helvetica, sans-serif;"
2327 " font-size: 12px;"
2328 " font-weight: normal;"
2329 " color: black;"
2330 " background: white;"
2331 "}\n"
2332 "th,td {"
2333 " font-size: 10px;"
2334 "}\n"
2335 "h1 {"
2336 " font-size: x-large;"
2337 " margin-bottom: 0.5em;"
2338 "}\n"
2339 "h2 {"
2340 " font-family: helvetica, arial;"
2341 " font-size: x-large;"
2342 " font-weight: bold;"
2343 " font-style: italic;"
2344 " color: #6020a0;"
2345 " margin-top: 0em;"
2346 " margin-bottom: 0em;"
2347 "}\n"
2348 "h3 {"
2349 " font-family: helvetica, arial;"
2350 " font-size: 16px;"
2351 " font-weight: bold;"
2352 " color: #b00040;"
2353 " background: #e8e8d0;"
2354 " margin-top: 0em;"
2355 " margin-bottom: 0em;"
2356 "}\n"
2357 "li {"
2358 " margin-top: 0.25em;"
2359 " margin-right: 2em;"
2360 "}\n"
2361 ".hr {margin-top: 0.25em;"
2362 " border-color: black;"
2363 " border-bottom-style: solid;"
2364 "}\n"
2365 ".titre {background: #20D0D0;color: #000000; font-weight: bold; text-align: center;}\n"
2366 ".total {background: #20D0D0;color: #ffff80;}\n"
2367 ".frontend {background: #e8e8d0;}\n"
2368 ".socket {background: #d0d0d0;}\n"
2369 ".backend {background: #e8e8d0;}\n"
2370 ".active_down {background: #ff9090;}\n"
2371 ".active_going_up {background: #ffd020;}\n"
2372 ".active_going_down {background: #ffffa0;}\n"
2373 ".active_up {background: #c0ffc0;}\n"
2374 ".active_nolb {background: #20a0ff;}\n"
2375 ".active_draining {background: #20a0FF;}\n"
2376 ".active_no_check {background: #e0e0e0;}\n"
2377 ".backup_down {background: #ff9090;}\n"
2378 ".backup_going_up {background: #ff80ff;}\n"
2379 ".backup_going_down {background: #c060ff;}\n"
2380 ".backup_up {background: #b0d0ff;}\n"
2381 ".backup_nolb {background: #90b0e0;}\n"
2382 ".backup_draining {background: #cc9900;}\n"
2383 ".backup_no_check {background: #e0e0e0;}\n"
2384 ".maintain {background: #c07820;}\n"
2385 ".rls {letter-spacing: 0.2em; margin-right: 1px;}\n" /* right letter spacing (used for grouping digits) */
2386 "\n"
2387 "a.px:link {color: #ffff40; text-decoration: none;}"
2388 "a.px:visited {color: #ffff40; text-decoration: none;}"
2389 "a.px:hover {color: #ffffff; text-decoration: none;}"
2390 "a.lfsb:link {color: #000000; text-decoration: none;}"
2391 "a.lfsb:visited {color: #000000; text-decoration: none;}"
2392 "a.lfsb:hover {color: #505050; text-decoration: none;}"
2393 "\n"
2394 "table.tbl { border-collapse: collapse; border-style: none;}\n"
2395 "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"
2396 "table.tbl td.ac { text-align: center;}\n"
2397 "table.tbl th { border-width: 1px; border-style: solid solid solid solid; border-color: gray;}\n"
2398 "table.tbl th.pxname { background: #b00040; color: #ffff40; font-weight: bold; border-style: solid solid none solid; padding: 2px 3px; white-space: nowrap;}\n"
2399 "table.tbl th.empty { border-style: none; empty-cells: hide; background: white;}\n"
2400 "table.tbl th.desc { background: white; border-style: solid solid none solid; text-align: left; padding: 2px 3px;}\n"
2401 "\n"
2402 "table.lgd { border-collapse: collapse; border-width: 1px; border-style: none none none solid; border-color: black;}\n"
2403 "table.lgd td { border-width: 1px; border-style: solid solid solid solid; border-color: gray; padding: 2px;}\n"
2404 "table.lgd td.noborder { border-style: none; padding: 2px; white-space: nowrap;}\n"
2405 "table.det { border-collapse: collapse; border-style: none; }\n"
2406 "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"
2407 "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"
2408 "u {text-decoration:none; border-bottom: 1px dotted black;}\n"
2409 "div.tips {\n"
2410 " display:block;\n"
2411 " visibility:hidden;\n"
2412 " z-index:2147483647;\n"
2413 " position:absolute;\n"
2414 " padding:2px 4px 3px;\n"
2415 " background:#f0f060; color:#000000;\n"
2416 " border:1px solid #7040c0;\n"
2417 " white-space:nowrap;\n"
2418 " font-style:normal;font-size:11px;font-weight:normal;\n"
2419 " -moz-border-radius:3px;-webkit-border-radius:3px;border-radius:3px;\n"
2420 " -moz-box-shadow:gray 2px 2px 3px;-webkit-box-shadow:gray 2px 2px 3px;box-shadow:gray 2px 2px 3px;\n"
2421 "}\n"
2422 "u:hover div.tips {visibility:visible;}\n"
2423 "-->\n"
2424 "</style></head>\n",
Willy Tarreau676c29e2019-10-09 10:50:01 +02002425 (appctx->ctx.stats.flags & STAT_SHNODE) ? " on " : "",
Willy Tarreau027d2062020-01-23 11:47:13 +01002426 (appctx->ctx.stats.flags & STAT_SHNODE) ? (uri && uri->node ? uri->node : global.node) : ""
William Lallemand74c24fb2016-11-21 17:18:36 +01002427 );
2428}
2429
2430/* Dumps the HTML stats information block to the trash for and uses the state from
2431 * stream interface <si> and per-uri parameters <uri>. The caller is responsible
2432 * for clearing the trash if needed.
2433 */
2434static void stats_dump_html_info(struct stream_interface *si, struct uri_auth *uri)
2435{
2436 struct appctx *appctx = __objt_appctx(si->end);
2437 unsigned int up = (now.tv_sec - start_date.tv_sec);
2438 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
Christopher Fauleted7a0662019-01-14 11:07:34 +01002439 const char *scope_ptr = stats_scope_ptr(appctx, si);
Willy Tarreau1713c032019-05-23 12:23:55 +02002440 unsigned long long bps = (unsigned long long)read_freq_ctr(&global.out_32bps) * 32;
2441
2442 /* Turn the bytes per second to bits per second and take care of the
2443 * usual ethernet overhead in order to help figure how far we are from
2444 * interface saturation since it's the only case which usually matters.
2445 * For this we count the total size of an Ethernet frame on the wire
2446 * including preamble and IFG (1538) for the largest TCP segment it
2447 * transports (1448 with TCP timestamps). This is not valid for smaller
2448 * packets (under-estimated), but it gives a reasonably accurate
2449 * estimation of how far we are from uplink saturation.
2450 */
2451 bps = bps * 8 * 1538 / 1448;
William Lallemand74c24fb2016-11-21 17:18:36 +01002452
2453 /* WARNING! this has to fit the first packet too.
2454 * We are around 3.5 kB, add adding entries will
2455 * become tricky if we want to support 4kB buffers !
2456 */
2457 chunk_appendf(&trash,
2458 "<body><h1><a href=\"" PRODUCT_URL "\" style=\"text-decoration: none;\">"
2459 PRODUCT_NAME "%s</a></h1>\n"
2460 "<h2>Statistics Report for pid %d%s%s%s%s</h2>\n"
2461 "<hr width=\"100%%\" class=\"hr\">\n"
2462 "<h3>&gt; General process information</h3>\n"
2463 "<table border=0><tr><td align=\"left\" nowrap width=\"1%%\">\n"
Yves Lafon95317282018-02-26 11:10:37 +01002464 "<p><b>pid = </b> %d (process #%d, nbproc = %d, nbthread = %d)<br>\n"
William Lallemand74c24fb2016-11-21 17:18:36 +01002465 "<b>uptime = </b> %dd %dh%02dm%02ds<br>\n"
2466 "<b>system limits:</b> memmax = %s%s; ulimit-n = %d<br>\n"
2467 "<b>maxsock = </b> %d; <b>maxconn = </b> %d; <b>maxpipes = </b> %d<br>\n"
Willy Tarreau1713c032019-05-23 12:23:55 +02002468 "current conns = %d; current pipes = %d/%d; conn rate = %d/sec; bit rate = %.3f %cbps<br>\n"
William Lallemand74c24fb2016-11-21 17:18:36 +01002469 "Running tasks: %d/%d; idle = %d %%<br>\n"
2470 "</td><td align=\"center\" nowrap>\n"
2471 "<table class=\"lgd\"><tr>\n"
2472 "<td class=\"active_up\">&nbsp;</td><td class=\"noborder\">active UP </td>"
2473 "<td class=\"backup_up\">&nbsp;</td><td class=\"noborder\">backup UP </td>"
2474 "</tr><tr>\n"
2475 "<td class=\"active_going_down\"></td><td class=\"noborder\">active UP, going down </td>"
2476 "<td class=\"backup_going_down\"></td><td class=\"noborder\">backup UP, going down </td>"
2477 "</tr><tr>\n"
2478 "<td class=\"active_going_up\"></td><td class=\"noborder\">active DOWN, going up </td>"
2479 "<td class=\"backup_going_up\"></td><td class=\"noborder\">backup DOWN, going up </td>"
2480 "</tr><tr>\n"
2481 "<td class=\"active_down\"></td><td class=\"noborder\">active or backup DOWN &nbsp;</td>"
2482 "<td class=\"active_no_check\"></td><td class=\"noborder\">not checked </td>"
2483 "</tr><tr>\n"
2484 "<td class=\"maintain\"></td><td class=\"noborder\" colspan=\"3\">active or backup DOWN for maintenance (MAINT) &nbsp;</td>"
2485 "</tr><tr>\n"
2486 "<td class=\"active_draining\"></td><td class=\"noborder\" colspan=\"3\">active or backup SOFT STOPPED for maintenance &nbsp;</td>"
2487 "</tr></table>\n"
2488 "Note: \"NOLB\"/\"DRAIN\" = UP with load-balancing disabled."
2489 "</td>"
2490 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
2491 "<b>Display option:</b><ul style=\"margin-top: 0.25em;\">"
2492 "",
Willy Tarreau676c29e2019-10-09 10:50:01 +02002493 (appctx->ctx.stats.flags & STAT_HIDEVER) ? "" : (stats_version_string),
2494 pid, (appctx->ctx.stats.flags & STAT_SHNODE) ? " on " : "",
2495 (appctx->ctx.stats.flags & STAT_SHNODE) ? (uri->node ? uri->node : global.node) : "",
2496 (appctx->ctx.stats.flags & STAT_SHDESC) ? ": " : "",
2497 (appctx->ctx.stats.flags & STAT_SHDESC) ? (uri->desc ? uri->desc : global.desc) : "",
Yves Lafon95317282018-02-26 11:10:37 +01002498 pid, relative_pid, global.nbproc, global.nbthread,
William Lallemand74c24fb2016-11-21 17:18:36 +01002499 up / 86400, (up % 86400) / 3600,
2500 (up % 3600) / 60, (up % 60),
2501 global.rlimit_memmax ? ultoa(global.rlimit_memmax) : "unlimited",
2502 global.rlimit_memmax ? " MB" : "",
2503 global.rlimit_nofile,
2504 global.maxsock, global.maxconn, global.maxpipes,
2505 actconn, pipes_used, pipes_used+pipes_free, read_freq_ctr(&global.conn_per_sec),
Willy Tarreau1713c032019-05-23 12:23:55 +02002506 bps >= 1000000000UL ? (bps / 1000000000.0) : bps >= 1000000UL ? (bps / 1000000.0) : (bps / 1000.0),
2507 bps >= 1000000000UL ? 'G' : bps >= 1000000UL ? 'M' : 'k',
Willy Tarreau81036f22019-05-20 19:24:50 +02002508 tasks_run_queue_cur, nb_tasks_cur, ti->idle_pct
William Lallemand74c24fb2016-11-21 17:18:36 +01002509 );
2510
2511 /* scope_txt = search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
Christopher Fauleted7a0662019-01-14 11:07:34 +01002512 memcpy(scope_txt, scope_ptr, appctx->ctx.stats.scope_len);
William Lallemand74c24fb2016-11-21 17:18:36 +01002513 scope_txt[appctx->ctx.stats.scope_len] = '\0';
2514
2515 chunk_appendf(&trash,
2516 "<li><form method=\"GET\">Scope : <input value=\"%s\" name=\"" STAT_SCOPE_INPUT_NAME "\" size=\"8\" maxlength=\"%d\" tabindex=\"1\"/></form>\n",
2517 (appctx->ctx.stats.scope_len > 0) ? scope_txt : "",
2518 STAT_SCOPE_TXT_MAXLEN);
2519
2520 /* scope_txt = search pattern + search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
2521 scope_txt[0] = 0;
2522 if (appctx->ctx.stats.scope_len) {
2523 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Christopher Fauleted7a0662019-01-14 11:07:34 +01002524 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), scope_ptr, appctx->ctx.stats.scope_len);
William Lallemand74c24fb2016-11-21 17:18:36 +01002525 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
2526 }
2527
2528 if (appctx->ctx.stats.flags & STAT_HIDE_DOWN)
2529 chunk_appendf(&trash,
2530 "<li><a href=\"%s%s%s%s\">Show all servers</a><br>\n",
2531 uri->uri_prefix,
2532 "",
2533 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2534 scope_txt);
2535 else
2536 chunk_appendf(&trash,
2537 "<li><a href=\"%s%s%s%s\">Hide 'DOWN' servers</a><br>\n",
2538 uri->uri_prefix,
2539 ";up",
2540 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2541 scope_txt);
2542
2543 if (uri->refresh > 0) {
2544 if (appctx->ctx.stats.flags & STAT_NO_REFRESH)
2545 chunk_appendf(&trash,
2546 "<li><a href=\"%s%s%s%s\">Enable refresh</a><br>\n",
2547 uri->uri_prefix,
2548 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2549 "",
2550 scope_txt);
2551 else
2552 chunk_appendf(&trash,
2553 "<li><a href=\"%s%s%s%s\">Disable refresh</a><br>\n",
2554 uri->uri_prefix,
2555 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2556 ";norefresh",
2557 scope_txt);
2558 }
2559
2560 chunk_appendf(&trash,
2561 "<li><a href=\"%s%s%s%s\">Refresh now</a><br>\n",
2562 uri->uri_prefix,
2563 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2564 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2565 scope_txt);
2566
2567 chunk_appendf(&trash,
2568 "<li><a href=\"%s;csv%s%s\">CSV export</a><br>\n",
2569 uri->uri_prefix,
2570 (uri->refresh > 0) ? ";norefresh" : "",
2571 scope_txt);
2572
2573 chunk_appendf(&trash,
Christopher Faulet6338a082019-09-09 15:50:54 +02002574 "<li><a href=\"%s;json%s%s\">JSON export</a> (<a href=\"%s;json-schema\">schema</a>)<br>\n",
2575 uri->uri_prefix,
2576 (uri->refresh > 0) ? ";norefresh" : "",
2577 scope_txt, uri->uri_prefix);
2578
2579 chunk_appendf(&trash,
William Lallemand74c24fb2016-11-21 17:18:36 +01002580 "</ul></td>"
2581 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
2582 "<b>External resources:</b><ul style=\"margin-top: 0.25em;\">\n"
2583 "<li><a href=\"" PRODUCT_URL "\">Primary site</a><br>\n"
2584 "<li><a href=\"" PRODUCT_URL_UPD "\">Updates (v" PRODUCT_BRANCH ")</a><br>\n"
2585 "<li><a href=\"" PRODUCT_URL_DOC "\">Online manual</a><br>\n"
2586 "</ul>"
2587 "</td>"
2588 "</tr></table>\n"
2589 ""
2590 );
2591
2592 if (appctx->ctx.stats.st_code) {
2593 switch (appctx->ctx.stats.st_code) {
2594 case STAT_STATUS_DONE:
2595 chunk_appendf(&trash,
2596 "<p><div class=active_up>"
2597 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2598 "Action processed successfully."
2599 "</div>\n", uri->uri_prefix,
2600 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2601 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2602 scope_txt);
2603 break;
2604 case STAT_STATUS_NONE:
2605 chunk_appendf(&trash,
2606 "<p><div class=active_going_down>"
2607 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2608 "Nothing has changed."
2609 "</div>\n", uri->uri_prefix,
2610 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2611 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2612 scope_txt);
2613 break;
2614 case STAT_STATUS_PART:
2615 chunk_appendf(&trash,
2616 "<p><div class=active_going_down>"
2617 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2618 "Action partially processed.<br>"
2619 "Some server names are probably unknown or ambiguous (duplicated names in the backend)."
2620 "</div>\n", uri->uri_prefix,
2621 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2622 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2623 scope_txt);
2624 break;
2625 case STAT_STATUS_ERRP:
2626 chunk_appendf(&trash,
2627 "<p><div class=active_down>"
2628 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2629 "Action not processed because of invalid parameters."
2630 "<ul>"
2631 "<li>The action is maybe unknown.</li>"
Christopher Faulet2f9a41d2019-02-27 15:30:57 +01002632 "<li>Invalid key parameter (empty or too long).</li>"
William Lallemand74c24fb2016-11-21 17:18:36 +01002633 "<li>The backend name is probably unknown or ambiguous (duplicated names).</li>"
2634 "<li>Some server names are probably unknown or ambiguous (duplicated names in the backend).</li>"
2635 "</ul>"
2636 "</div>\n", uri->uri_prefix,
2637 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2638 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2639 scope_txt);
2640 break;
2641 case STAT_STATUS_EXCD:
2642 chunk_appendf(&trash,
2643 "<p><div class=active_down>"
2644 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2645 "<b>Action not processed : the buffer couldn't store all the data.<br>"
2646 "You should retry with less servers at a time.</b>"
2647 "</div>\n", uri->uri_prefix,
2648 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2649 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2650 scope_txt);
2651 break;
2652 case STAT_STATUS_DENY:
2653 chunk_appendf(&trash,
2654 "<p><div class=active_down>"
2655 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2656 "<b>Action denied.</b>"
2657 "</div>\n", uri->uri_prefix,
2658 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2659 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2660 scope_txt);
2661 break;
Christopher Faulet3c2ecf72019-02-27 16:41:27 +01002662 case STAT_STATUS_IVAL:
2663 chunk_appendf(&trash,
2664 "<p><div class=active_down>"
2665 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2666 "<b>Invalid requests (unsupported method or chunked encoded request).</b>"
2667 "</div>\n", uri->uri_prefix,
2668 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2669 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2670 scope_txt);
2671 break;
William Lallemand74c24fb2016-11-21 17:18:36 +01002672 default:
2673 chunk_appendf(&trash,
2674 "<p><div class=active_no_check>"
2675 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2676 "Unexpected result."
2677 "</div>\n", uri->uri_prefix,
2678 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2679 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2680 scope_txt);
2681 }
2682 chunk_appendf(&trash, "<p>\n");
2683 }
2684}
2685
2686/* Dumps the HTML stats trailer block to the trash. The caller is responsible
2687 * for clearing the trash if needed.
2688 */
2689static void stats_dump_html_end()
2690{
2691 chunk_appendf(&trash, "</body></html>\n");
2692}
2693
Simon Horman05ee2132017-01-04 09:37:25 +01002694/* Dumps the stats JSON header to the trash buffer which. The caller is responsible
2695 * for clearing it if needed.
2696 */
2697static void stats_dump_json_header()
2698{
2699 chunk_strcat(&trash, "[");
2700}
2701
2702
2703/* Dumps the JSON stats trailer block to the trash. The caller is responsible
2704 * for clearing the trash if needed.
2705 */
2706static void stats_dump_json_end()
2707{
2708 chunk_strcat(&trash, "]");
2709}
2710
William Lallemand74c24fb2016-11-21 17:18:36 +01002711/* This function dumps statistics onto the stream interface's read buffer in
2712 * either CSV or HTML format. <uri> contains some HTML-specific parameters that
2713 * are ignored for CSV format (hence <uri> may be NULL there). It returns 0 if
2714 * it had to stop writing data and an I/O is needed, 1 if the dump is finished
2715 * and the stream must be closed, or -1 in case of any error. This function is
2716 * used by both the CLI and the HTTP handlers.
2717 */
Christopher Fauletef779222018-10-31 08:47:01 +01002718static int stats_dump_stat_to_buffer(struct stream_interface *si, struct htx *htx,
2719 struct uri_auth *uri)
William Lallemand74c24fb2016-11-21 17:18:36 +01002720{
2721 struct appctx *appctx = __objt_appctx(si->end);
2722 struct channel *rep = si_ic(si);
2723 struct proxy *px;
2724
2725 chunk_reset(&trash);
2726
2727 switch (appctx->st2) {
2728 case STAT_ST_INIT:
2729 appctx->st2 = STAT_ST_HEAD; /* let's start producing data */
2730 /* fall through */
2731
2732 case STAT_ST_HEAD:
2733 if (appctx->ctx.stats.flags & STAT_FMT_HTML)
Willy Tarreau676c29e2019-10-09 10:50:01 +02002734 stats_dump_html_head(appctx, uri);
Christopher Faulet6338a082019-09-09 15:50:54 +02002735 else if (appctx->ctx.stats.flags & STAT_JSON_SCHM)
2736 stats_dump_json_schema(&trash);
Simon Horman05ee2132017-01-04 09:37:25 +01002737 else if (appctx->ctx.stats.flags & STAT_FMT_JSON)
Willy Tarreau9d7fb632017-04-11 07:53:04 +02002738 stats_dump_json_header();
William Lallemand74c24fb2016-11-21 17:18:36 +01002739 else if (!(appctx->ctx.stats.flags & STAT_FMT_TYPED))
2740 stats_dump_csv_header();
2741
Christopher Fauletef779222018-10-31 08:47:01 +01002742 if (!stats_putchk(rep, htx, &trash))
2743 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002744
Christopher Faulet6338a082019-09-09 15:50:54 +02002745 if (appctx->ctx.stats.flags & STAT_JSON_SCHM) {
2746 appctx->st2 = STAT_ST_FIN;
2747 return 1;
2748 }
William Lallemand74c24fb2016-11-21 17:18:36 +01002749 appctx->st2 = STAT_ST_INFO;
2750 /* fall through */
2751
2752 case STAT_ST_INFO:
2753 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
2754 stats_dump_html_info(si, uri);
Christopher Fauletef779222018-10-31 08:47:01 +01002755 if (!stats_putchk(rep, htx, &trash))
2756 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002757 }
2758
Olivier Houchardfbc74e82017-11-24 16:54:05 +01002759 appctx->ctx.stats.px = proxies_list;
William Lallemand74c24fb2016-11-21 17:18:36 +01002760 appctx->ctx.stats.px_st = STAT_PX_ST_INIT;
2761 appctx->st2 = STAT_ST_LIST;
2762 /* fall through */
2763
2764 case STAT_ST_LIST:
2765 /* dump proxies */
2766 while (appctx->ctx.stats.px) {
Christopher Fauletef779222018-10-31 08:47:01 +01002767 if (htx) {
2768 if (htx_almost_full(htx))
2769 goto full;
2770 }
2771 else {
2772 if (buffer_almost_full(&rep->buf))
2773 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002774 }
2775
2776 px = appctx->ctx.stats.px;
2777 /* skip the disabled proxies, global frontend and non-networked ones */
2778 if (px->state != PR_STSTOPPED && px->uuid > 0 && (px->cap & (PR_CAP_FE | PR_CAP_BE)))
Christopher Fauletef779222018-10-31 08:47:01 +01002779 if (stats_dump_proxy_to_buffer(si, htx, px, uri) == 0)
William Lallemand74c24fb2016-11-21 17:18:36 +01002780 return 0;
2781
2782 appctx->ctx.stats.px = px->next;
2783 appctx->ctx.stats.px_st = STAT_PX_ST_INIT;
2784 }
2785 /* here, we just have reached the last proxy */
2786
2787 appctx->st2 = STAT_ST_END;
2788 /* fall through */
2789
2790 case STAT_ST_END:
Simon Horman05ee2132017-01-04 09:37:25 +01002791 if (appctx->ctx.stats.flags & (STAT_FMT_HTML|STAT_FMT_JSON)) {
2792 if (appctx->ctx.stats.flags & STAT_FMT_HTML)
2793 stats_dump_html_end();
2794 else
2795 stats_dump_json_end();
Christopher Fauletef779222018-10-31 08:47:01 +01002796 if (!stats_putchk(rep, htx, &trash))
2797 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002798 }
2799
2800 appctx->st2 = STAT_ST_FIN;
2801 /* fall through */
2802
2803 case STAT_ST_FIN:
2804 return 1;
2805
2806 default:
2807 /* unknown state ! */
2808 appctx->st2 = STAT_ST_FIN;
2809 return -1;
2810 }
Christopher Fauletef779222018-10-31 08:47:01 +01002811
2812 full:
2813 si_rx_room_blk(si);
2814 return 0;
2815
William Lallemand74c24fb2016-11-21 17:18:36 +01002816}
2817
2818/* We reached the stats page through a POST request. The appctx is
2819 * expected to have already been allocated by the caller.
2820 * Parse the posted data and enable/disable servers if necessary.
2821 * Returns 1 if request was parsed or zero if it needs more data.
2822 */
2823static int stats_process_http_post(struct stream_interface *si)
2824{
2825 struct stream *s = si_strm(si);
2826 struct appctx *appctx = objt_appctx(si->end);
2827
2828 struct proxy *px = NULL;
2829 struct server *sv = NULL;
2830
2831 char key[LINESIZE];
2832 int action = ST_ADM_ACTION_NONE;
2833 int reprocess = 0;
2834
2835 int total_servers = 0;
2836 int altered_servers = 0;
2837
2838 char *first_param, *cur_param, *next_param, *end_params;
2839 char *st_cur_param = NULL;
2840 char *st_next_param = NULL;
2841
Christopher Fauleta3618372018-12-13 21:59:56 +01002842 struct buffer *temp = get_trash_chunk();
William Lallemand74c24fb2016-11-21 17:18:36 +01002843
Christopher Fauletb7f88902019-07-15 21:56:43 +02002844 struct htx *htx = htxbuf(&s->req.buf);
2845 struct htx_blk *blk;
Christopher Fauleta3618372018-12-13 21:59:56 +01002846
Christopher Fauletb7f88902019-07-15 21:56:43 +02002847 /* we need more data */
2848 if (s->txn->req.msg_state < HTTP_MSG_DONE) {
2849 /* check if we can receive more */
2850 if (htx_free_data_space(htx) <= global.tune.maxrewrite) {
2851 appctx->ctx.stats.st_code = STAT_STATUS_EXCD;
2852 goto out;
Christopher Fauleta3618372018-12-13 21:59:56 +01002853 }
Christopher Fauletb7f88902019-07-15 21:56:43 +02002854 goto wait;
2855 }
Christopher Fauleta3618372018-12-13 21:59:56 +01002856
Christopher Fauletb7f88902019-07-15 21:56:43 +02002857 /* The request was fully received. Copy data */
2858 blk = htx_get_head_blk(htx);
2859 while (blk) {
2860 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Fauleta3618372018-12-13 21:59:56 +01002861
Christopher Fauletb7f88902019-07-15 21:56:43 +02002862 if (type == HTX_BLK_EOM || type == HTX_BLK_TLR || type == HTX_BLK_EOT)
2863 break;
2864 if (type == HTX_BLK_DATA) {
2865 struct ist v = htx_get_blk_value(htx, blk);
Christopher Fauleta3618372018-12-13 21:59:56 +01002866
Christopher Fauletb7f88902019-07-15 21:56:43 +02002867 if (!chunk_memcat(temp, v.ptr, v.len)) {
Christopher Faulet2f9a41d2019-02-27 15:30:57 +01002868 appctx->ctx.stats.st_code = STAT_STATUS_EXCD;
2869 goto out;
2870 }
Christopher Fauleta3618372018-12-13 21:59:56 +01002871 }
Christopher Fauletb7f88902019-07-15 21:56:43 +02002872 blk = htx_get_next_blk(htx, blk);
William Lallemand74c24fb2016-11-21 17:18:36 +01002873 }
2874
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002875 first_param = temp->area;
Christopher Fauleta3618372018-12-13 21:59:56 +01002876 end_params = temp->area + temp->data;
William Lallemand74c24fb2016-11-21 17:18:36 +01002877 cur_param = next_param = end_params;
2878 *end_params = '\0';
2879
2880 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
2881
2882 /*
2883 * Parse the parameters in reverse order to only store the last value.
2884 * From the html form, the backend and the action are at the end.
2885 */
2886 while (cur_param > first_param) {
2887 char *value;
2888 int poffset, plen;
2889
2890 cur_param--;
2891
2892 if ((*cur_param == '&') || (cur_param == first_param)) {
2893 reprocess_servers:
2894 /* Parse the key */
2895 poffset = (cur_param != first_param ? 1 : 0);
2896 plen = next_param - cur_param + (cur_param == first_param ? 1 : 0);
2897 if ((plen > 0) && (plen <= sizeof(key))) {
2898 strncpy(key, cur_param + poffset, plen);
2899 key[plen - 1] = '\0';
2900 } else {
Christopher Faulet2f9a41d2019-02-27 15:30:57 +01002901 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
William Lallemand74c24fb2016-11-21 17:18:36 +01002902 goto out;
2903 }
2904
2905 /* Parse the value */
2906 value = key;
2907 while (*value != '\0' && *value != '=') {
2908 value++;
2909 }
2910 if (*value == '=') {
2911 /* Ok, a value is found, we can mark the end of the key */
2912 *value++ = '\0';
2913 }
Willy Tarreau62ba9ba2020-04-23 17:54:47 +02002914 if (url_decode(key, 1) < 0 || url_decode(value, 1) < 0)
William Lallemand74c24fb2016-11-21 17:18:36 +01002915 break;
2916
2917 /* Now we can check the key to see what to do */
2918 if (!px && (strcmp(key, "b") == 0)) {
2919 if ((px = proxy_be_by_name(value)) == NULL) {
2920 /* the backend name is unknown or ambiguous (duplicate names) */
2921 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
2922 goto out;
2923 }
2924 }
2925 else if (!action && (strcmp(key, "action") == 0)) {
2926 if (strcmp(value, "ready") == 0) {
2927 action = ST_ADM_ACTION_READY;
2928 }
2929 else if (strcmp(value, "drain") == 0) {
2930 action = ST_ADM_ACTION_DRAIN;
2931 }
2932 else if (strcmp(value, "maint") == 0) {
2933 action = ST_ADM_ACTION_MAINT;
2934 }
2935 else if (strcmp(value, "shutdown") == 0) {
2936 action = ST_ADM_ACTION_SHUTDOWN;
2937 }
2938 else if (strcmp(value, "dhlth") == 0) {
2939 action = ST_ADM_ACTION_DHLTH;
2940 }
2941 else if (strcmp(value, "ehlth") == 0) {
2942 action = ST_ADM_ACTION_EHLTH;
2943 }
2944 else if (strcmp(value, "hrunn") == 0) {
2945 action = ST_ADM_ACTION_HRUNN;
2946 }
2947 else if (strcmp(value, "hnolb") == 0) {
2948 action = ST_ADM_ACTION_HNOLB;
2949 }
2950 else if (strcmp(value, "hdown") == 0) {
2951 action = ST_ADM_ACTION_HDOWN;
2952 }
2953 else if (strcmp(value, "dagent") == 0) {
2954 action = ST_ADM_ACTION_DAGENT;
2955 }
2956 else if (strcmp(value, "eagent") == 0) {
2957 action = ST_ADM_ACTION_EAGENT;
2958 }
2959 else if (strcmp(value, "arunn") == 0) {
2960 action = ST_ADM_ACTION_ARUNN;
2961 }
2962 else if (strcmp(value, "adown") == 0) {
2963 action = ST_ADM_ACTION_ADOWN;
2964 }
2965 /* else these are the old supported methods */
2966 else if (strcmp(value, "disable") == 0) {
2967 action = ST_ADM_ACTION_DISABLE;
2968 }
2969 else if (strcmp(value, "enable") == 0) {
2970 action = ST_ADM_ACTION_ENABLE;
2971 }
2972 else if (strcmp(value, "stop") == 0) {
2973 action = ST_ADM_ACTION_STOP;
2974 }
2975 else if (strcmp(value, "start") == 0) {
2976 action = ST_ADM_ACTION_START;
2977 }
2978 else {
2979 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
2980 goto out;
2981 }
2982 }
2983 else if (strcmp(key, "s") == 0) {
2984 if (!(px && action)) {
2985 /*
2986 * Indicates that we'll need to reprocess the parameters
2987 * as soon as backend and action are known
2988 */
2989 if (!reprocess) {
2990 st_cur_param = cur_param;
2991 st_next_param = next_param;
2992 }
2993 reprocess = 1;
2994 }
2995 else if ((sv = findserver(px, value)) != NULL) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002996 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
William Lallemand74c24fb2016-11-21 17:18:36 +01002997 switch (action) {
2998 case ST_ADM_ACTION_DISABLE:
Emeric Brun52a91d32017-08-31 14:41:55 +02002999 if (!(sv->cur_admin & SRV_ADMF_FMAINT)) {
William Lallemand74c24fb2016-11-21 17:18:36 +01003000 altered_servers++;
3001 total_servers++;
3002 srv_set_admin_flag(sv, SRV_ADMF_FMAINT, "'disable' on stats page");
3003 }
3004 break;
3005 case ST_ADM_ACTION_ENABLE:
Emeric Brun52a91d32017-08-31 14:41:55 +02003006 if (sv->cur_admin & SRV_ADMF_FMAINT) {
William Lallemand74c24fb2016-11-21 17:18:36 +01003007 altered_servers++;
3008 total_servers++;
3009 srv_clr_admin_flag(sv, SRV_ADMF_FMAINT);
3010 }
3011 break;
3012 case ST_ADM_ACTION_STOP:
Emeric Brun52a91d32017-08-31 14:41:55 +02003013 if (!(sv->cur_admin & SRV_ADMF_FDRAIN)) {
William Lallemand74c24fb2016-11-21 17:18:36 +01003014 srv_set_admin_flag(sv, SRV_ADMF_FDRAIN, "'stop' on stats page");
3015 altered_servers++;
3016 total_servers++;
3017 }
3018 break;
3019 case ST_ADM_ACTION_START:
Emeric Brun52a91d32017-08-31 14:41:55 +02003020 if (sv->cur_admin & SRV_ADMF_FDRAIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +01003021 srv_clr_admin_flag(sv, SRV_ADMF_FDRAIN);
3022 altered_servers++;
3023 total_servers++;
3024 }
3025 break;
3026 case ST_ADM_ACTION_DHLTH:
3027 if (sv->check.state & CHK_ST_CONFIGURED) {
3028 sv->check.state &= ~CHK_ST_ENABLED;
3029 altered_servers++;
3030 total_servers++;
3031 }
3032 break;
3033 case ST_ADM_ACTION_EHLTH:
3034 if (sv->check.state & CHK_ST_CONFIGURED) {
3035 sv->check.state |= CHK_ST_ENABLED;
3036 altered_servers++;
3037 total_servers++;
3038 }
3039 break;
3040 case ST_ADM_ACTION_HRUNN:
3041 if (!(sv->track)) {
3042 sv->check.health = sv->check.rise + sv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02003043 srv_set_running(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01003044 altered_servers++;
3045 total_servers++;
3046 }
3047 break;
3048 case ST_ADM_ACTION_HNOLB:
3049 if (!(sv->track)) {
3050 sv->check.health = sv->check.rise + sv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02003051 srv_set_stopping(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01003052 altered_servers++;
3053 total_servers++;
3054 }
3055 break;
3056 case ST_ADM_ACTION_HDOWN:
3057 if (!(sv->track)) {
3058 sv->check.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02003059 srv_set_stopped(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01003060 altered_servers++;
3061 total_servers++;
3062 }
3063 break;
3064 case ST_ADM_ACTION_DAGENT:
3065 if (sv->agent.state & CHK_ST_CONFIGURED) {
3066 sv->agent.state &= ~CHK_ST_ENABLED;
3067 altered_servers++;
3068 total_servers++;
3069 }
3070 break;
3071 case ST_ADM_ACTION_EAGENT:
3072 if (sv->agent.state & CHK_ST_CONFIGURED) {
3073 sv->agent.state |= CHK_ST_ENABLED;
3074 altered_servers++;
3075 total_servers++;
3076 }
3077 break;
3078 case ST_ADM_ACTION_ARUNN:
3079 if (sv->agent.state & CHK_ST_ENABLED) {
3080 sv->agent.health = sv->agent.rise + sv->agent.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02003081 srv_set_running(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01003082 altered_servers++;
3083 total_servers++;
3084 }
3085 break;
3086 case ST_ADM_ACTION_ADOWN:
3087 if (sv->agent.state & CHK_ST_ENABLED) {
3088 sv->agent.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02003089 srv_set_stopped(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01003090 altered_servers++;
3091 total_servers++;
3092 }
3093 break;
3094 case ST_ADM_ACTION_READY:
3095 srv_adm_set_ready(sv);
3096 altered_servers++;
3097 total_servers++;
3098 break;
3099 case ST_ADM_ACTION_DRAIN:
3100 srv_adm_set_drain(sv);
3101 altered_servers++;
3102 total_servers++;
3103 break;
3104 case ST_ADM_ACTION_MAINT:
3105 srv_adm_set_maint(sv);
3106 altered_servers++;
3107 total_servers++;
3108 break;
3109 case ST_ADM_ACTION_SHUTDOWN:
3110 if (px->state != PR_STSTOPPED) {
Willy Tarreauaf7ea812019-11-14 16:42:04 +01003111 srv_shutdown_streams(sv, SF_ERR_KILLED);
William Lallemand74c24fb2016-11-21 17:18:36 +01003112 altered_servers++;
3113 total_servers++;
3114 }
3115 break;
3116 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003117 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
William Lallemand74c24fb2016-11-21 17:18:36 +01003118 } else {
3119 /* the server name is unknown or ambiguous (duplicate names) */
3120 total_servers++;
3121 }
3122 }
3123 if (reprocess && px && action) {
3124 /* Now, we know the backend and the action chosen by the user.
3125 * We can safely restart from the first server parameter
3126 * to reprocess them
3127 */
3128 cur_param = st_cur_param;
3129 next_param = st_next_param;
3130 reprocess = 0;
3131 goto reprocess_servers;
3132 }
3133
3134 next_param = cur_param;
3135 }
3136 }
3137
3138 if (total_servers == 0) {
3139 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
3140 }
3141 else if (altered_servers == 0) {
3142 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
3143 }
3144 else if (altered_servers == total_servers) {
3145 appctx->ctx.stats.st_code = STAT_STATUS_DONE;
3146 }
3147 else {
3148 appctx->ctx.stats.st_code = STAT_STATUS_PART;
3149 }
3150 out:
3151 return 1;
Christopher Faulet2f9a41d2019-02-27 15:30:57 +01003152 wait:
3153 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
3154 return 0;
Christopher Fauletef779222018-10-31 08:47:01 +01003155}
3156
3157
Christopher Fauletb7f88902019-07-15 21:56:43 +02003158static int stats_send_http_headers(struct stream_interface *si, struct htx *htx)
Christopher Fauletef779222018-10-31 08:47:01 +01003159{
3160 struct stream *s = si_strm(si);
3161 struct uri_auth *uri = s->be->uri_auth;
3162 struct appctx *appctx = __objt_appctx(si->end);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003163 struct htx_sl *sl;
3164 unsigned int flags;
Christopher Fauletef779222018-10-31 08:47:01 +01003165
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003166 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);
3167 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), ist("200"), ist("OK"));
3168 if (!sl)
Christopher Fauletef779222018-10-31 08:47:01 +01003169 goto full;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003170 sl->info.res.status = 200;
Christopher Fauletef779222018-10-31 08:47:01 +01003171
Christopher Fauletb829f4c2019-03-29 16:13:55 +01003172 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")))
Christopher Fauletef779222018-10-31 08:47:01 +01003173 goto full;
3174 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
3175 if (!htx_add_header(htx, ist("Content-Type"), ist("text/html")))
3176 goto full;
3177 }
Christopher Faulet6338a082019-09-09 15:50:54 +02003178 else if (appctx->ctx.stats.flags & (STAT_FMT_JSON|STAT_JSON_SCHM)) {
3179 if (!htx_add_header(htx, ist("Content-Type"), ist("application/json")))
3180 goto full;
3181 }
Christopher Fauletef779222018-10-31 08:47:01 +01003182 else {
3183 if (!htx_add_header(htx, ist("Content-Type"), ist("text/plain")))
3184 goto full;
3185 }
3186
3187 if (uri->refresh > 0 && !(appctx->ctx.stats.flags & STAT_NO_REFRESH)) {
3188 const char *refresh = U2A(uri->refresh);
3189 if (!htx_add_header(htx, ist("Refresh"), ist2(refresh, strlen(refresh))))
3190 goto full;
3191 }
3192
3193 if (appctx->ctx.stats.flags & STAT_CHUNKED) {
3194 if (!htx_add_header(htx, ist("Transfer-Encoding"), ist("chunked")))
3195 goto full;
3196 }
3197
3198 if (!htx_add_endof(htx, HTX_BLK_EOH))
3199 goto full;
3200
Christopher Faulet1e2d6362019-02-27 16:28:48 +01003201 channel_add_input(&s->res, htx->data);
Christopher Fauletef779222018-10-31 08:47:01 +01003202 return 1;
3203
3204 full:
3205 htx_reset(htx);
3206 si_rx_room_blk(si);
3207 return 0;
William Lallemand74c24fb2016-11-21 17:18:36 +01003208}
3209
Christopher Fauletef779222018-10-31 08:47:01 +01003210
Christopher Fauletb7f88902019-07-15 21:56:43 +02003211static int stats_send_http_redirect(struct stream_interface *si, struct htx *htx)
Christopher Fauletef779222018-10-31 08:47:01 +01003212{
3213 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
3214 struct stream *s = si_strm(si);
3215 struct uri_auth *uri = s->be->uri_auth;
3216 struct appctx *appctx = __objt_appctx(si->end);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003217 struct htx_sl *sl;
3218 unsigned int flags;
Christopher Fauletef779222018-10-31 08:47:01 +01003219
3220 /* scope_txt = search pattern + search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
3221 scope_txt[0] = 0;
3222 if (appctx->ctx.stats.scope_len) {
Christopher Fauleted7a0662019-01-14 11:07:34 +01003223 const char *scope_ptr = stats_scope_ptr(appctx, si);
3224
Christopher Fauletef779222018-10-31 08:47:01 +01003225 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Christopher Fauleted7a0662019-01-14 11:07:34 +01003226 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), scope_ptr, appctx->ctx.stats.scope_len);
Christopher Fauletef779222018-10-31 08:47:01 +01003227 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
3228 }
3229
3230 /* We don't want to land on the posted stats page because a refresh will
3231 * repost the data. We don't want this to happen on accident so we redirect
3232 * the browse to the stats page with a GET.
3233 */
3234 chunk_printf(&trash, "%s;st=%s%s%s%s",
3235 uri->uri_prefix,
3236 ((appctx->ctx.stats.st_code > STAT_STATUS_INIT) &&
3237 (appctx->ctx.stats.st_code < STAT_STATUS_SIZE) &&
3238 stat_status_codes[appctx->ctx.stats.st_code]) ?
3239 stat_status_codes[appctx->ctx.stats.st_code] :
3240 stat_status_codes[STAT_STATUS_UNKN],
3241 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3242 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
3243 scope_txt);
3244
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003245 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_CHNK);
3246 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), ist("303"), ist("See Other"));
3247 if (!sl)
Christopher Fauletef779222018-10-31 08:47:01 +01003248 goto full;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003249 sl->info.res.status = 303;
Christopher Fauletef779222018-10-31 08:47:01 +01003250
3251 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")) ||
Christopher Fauletef779222018-10-31 08:47:01 +01003252 !htx_add_header(htx, ist("Content-Type"), ist("text/plain")) ||
3253 !htx_add_header(htx, ist("Content-Length"), ist("0")) ||
3254 !htx_add_header(htx, ist("Location"), ist2(trash.area, trash.data)))
3255 goto full;
3256
3257 if (!htx_add_endof(htx, HTX_BLK_EOH))
3258 goto full;
3259
Christopher Faulet1e2d6362019-02-27 16:28:48 +01003260 channel_add_input(&s->res, htx->data);
Christopher Fauletef779222018-10-31 08:47:01 +01003261 return 1;
3262
3263full:
3264 htx_reset(htx);
3265 si_rx_room_blk(si);
3266 return 0;
3267}
William Lallemand74c24fb2016-11-21 17:18:36 +01003268
Simon Horman05ee2132017-01-04 09:37:25 +01003269
William Lallemand74c24fb2016-11-21 17:18:36 +01003270/* This I/O handler runs as an applet embedded in a stream interface. It is
3271 * used to send HTTP stats over a TCP socket. The mechanism is very simple.
3272 * appctx->st0 contains the operation in progress (dump, done). The handler
3273 * automatically unregisters itself once transfer is complete.
3274 */
Christopher Fauletb7f88902019-07-15 21:56:43 +02003275static void http_stats_io_handler(struct appctx *appctx)
Christopher Fauletef779222018-10-31 08:47:01 +01003276{
3277 struct stream_interface *si = appctx->owner;
3278 struct stream *s = si_strm(si);
3279 struct channel *req = si_oc(si);
3280 struct channel *res = si_ic(si);
3281 struct htx *req_htx, *res_htx;
3282
3283 res_htx = htx_from_buf(&res->buf);
3284
3285 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
3286 goto out;
3287
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05003288 /* Check if the input buffer is available. */
Christopher Fauletef779222018-10-31 08:47:01 +01003289 if (!b_size(&res->buf)) {
3290 si_rx_room_blk(si);
3291 goto out;
3292 }
3293
3294 /* check that the output is not closed */
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003295 if (res->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_SHUTR))
3296 appctx->st0 = STAT_HTTP_END;
Christopher Fauletef779222018-10-31 08:47:01 +01003297
3298 /* all states are processed in sequence */
3299 if (appctx->st0 == STAT_HTTP_HEAD) {
Christopher Fauletb7f88902019-07-15 21:56:43 +02003300 if (stats_send_http_headers(si, res_htx)) {
Christopher Fauletef779222018-10-31 08:47:01 +01003301 if (s->txn->meth == HTTP_METH_HEAD)
3302 appctx->st0 = STAT_HTTP_DONE;
3303 else
3304 appctx->st0 = STAT_HTTP_DUMP;
3305 }
3306 }
3307
3308 if (appctx->st0 == STAT_HTTP_DUMP) {
3309 if (stats_dump_stat_to_buffer(si, res_htx, s->be->uri_auth))
3310 appctx->st0 = STAT_HTTP_DONE;
3311 }
3312
3313 if (appctx->st0 == STAT_HTTP_POST) {
3314 if (stats_process_http_post(si))
3315 appctx->st0 = STAT_HTTP_LAST;
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003316 else if (req->flags & CF_SHUTR)
Christopher Fauletef779222018-10-31 08:47:01 +01003317 appctx->st0 = STAT_HTTP_DONE;
3318 }
3319
3320 if (appctx->st0 == STAT_HTTP_LAST) {
Christopher Fauletb7f88902019-07-15 21:56:43 +02003321 if (stats_send_http_redirect(si, res_htx))
Christopher Fauletef779222018-10-31 08:47:01 +01003322 appctx->st0 = STAT_HTTP_DONE;
3323 }
3324
3325 if (appctx->st0 == STAT_HTTP_DONE) {
Christopher Faulet54b5e212019-06-04 10:08:28 +02003326 /* Don't add TLR because mux-h1 will take care of it */
Christopher Fauletef779222018-10-31 08:47:01 +01003327 if (!htx_add_endof(res_htx, HTX_BLK_EOM)) {
3328 si_rx_room_blk(si);
3329 goto out;
3330 }
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003331 channel_add_input(&s->res, 1);
3332 appctx->st0 = STAT_HTTP_END;
Christopher Fauletef779222018-10-31 08:47:01 +01003333 }
3334
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003335 if (appctx->st0 == STAT_HTTP_END) {
3336 if (!(res->flags & CF_SHUTR)) {
Christopher Fauletef779222018-10-31 08:47:01 +01003337 res->flags |= CF_READ_NULL;
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003338 si_shutr(si);
3339 }
3340
3341 /* eat the whole request */
3342 if (co_data(req)) {
3343 req_htx = htx_from_buf(&req->buf);
3344 co_htx_skip(req, req_htx, co_data(req));
3345 htx_to_buf(req_htx, &req->buf);
Christopher Fauletef779222018-10-31 08:47:01 +01003346 }
3347 }
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003348
Christopher Fauletef779222018-10-31 08:47:01 +01003349 out:
3350 /* we have left the request in the buffer for the case where we
3351 * process a POST, and this automatically re-enables activity on
3352 * read. It's better to indicate that we want to stop reading when
3353 * we're sending, so that we know there's at most one direction
3354 * deciding to wake the applet up. It saves it from looping when
3355 * emitting large blocks into small TCP windows.
3356 */
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003357 htx_to_buf(res_htx, &res->buf);
3358 if (!channel_is_empty(res))
Christopher Fauletef779222018-10-31 08:47:01 +01003359 si_stop_get(si);
Christopher Fauletef779222018-10-31 08:47:01 +01003360}
3361
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003362/* Dump all fields from <info> into <out> using the "show info" format (name: value) */
Willy Tarreau83061a82018-07-13 11:56:34 +02003363static int stats_dump_info_fields(struct buffer *out,
Willy Tarreau43241ff2019-10-09 11:27:51 +02003364 const struct field *info, unsigned int flags)
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003365{
3366 int field;
3367
3368 for (field = 0; field < INF_TOTAL_FIELDS; field++) {
3369 if (!field_format(info, field))
3370 continue;
3371
Willy Tarreaueaa55372019-10-09 07:39:11 +02003372 if (!chunk_appendf(out, "%s: ", info_fields[field].name))
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003373 return 0;
3374 if (!stats_emit_raw_data_field(out, &info[field]))
3375 return 0;
Willy Tarreau6b19b142019-10-09 15:44:21 +02003376 if ((flags & STAT_SHOW_FDESC) && !chunk_appendf(out, ":\"%s\"", info_fields[field].desc))
3377 return 0;
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003378 if (!chunk_strcat(out, "\n"))
3379 return 0;
3380 }
3381 return 1;
3382}
3383
3384/* Dump all fields from <info> into <out> using the "show info typed" format */
Willy Tarreau83061a82018-07-13 11:56:34 +02003385static int stats_dump_typed_info_fields(struct buffer *out,
Willy Tarreau43241ff2019-10-09 11:27:51 +02003386 const struct field *info, unsigned int flags)
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003387{
3388 int field;
3389
3390 for (field = 0; field < INF_TOTAL_FIELDS; field++) {
3391 if (!field_format(info, field))
3392 continue;
3393
Willy Tarreaueaa55372019-10-09 07:39:11 +02003394 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 +01003395 return 0;
3396 if (!stats_emit_field_tags(out, &info[field], ':'))
3397 return 0;
3398 if (!stats_emit_typed_data_field(out, &info[field]))
3399 return 0;
Willy Tarreau6b19b142019-10-09 15:44:21 +02003400 if ((flags & STAT_SHOW_FDESC) && !chunk_appendf(out, ":\"%s\"", info_fields[field].desc))
3401 return 0;
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003402 if (!chunk_strcat(out, "\n"))
3403 return 0;
3404 }
3405 return 1;
3406}
3407
3408/* Fill <info> with HAProxy global info. <info> is preallocated
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05003409 * array of length <len>. The length of the array must be
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003410 * INF_TOTAL_FIELDS. If this length is less then this value, the
3411 * function returns 0, otherwise, it returns 1.
3412 */
3413int stats_fill_info(struct field *info, int len)
3414{
3415 unsigned int up = (now.tv_sec - start_date.tv_sec);
Willy Tarreau83061a82018-07-13 11:56:34 +02003416 struct buffer *out = get_trash_chunk();
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003417
3418#ifdef USE_OPENSSL
3419 int ssl_sess_rate = read_freq_ctr(&global.ssl_per_sec);
3420 int ssl_key_rate = read_freq_ctr(&global.ssl_fe_keys_per_sec);
3421 int ssl_reuse = 0;
3422
3423 if (ssl_key_rate < ssl_sess_rate) {
3424 /* count the ssl reuse ratio and avoid overflows in both directions */
3425 ssl_reuse = 100 - (100 * ssl_key_rate + (ssl_sess_rate - 1) / 2) / ssl_sess_rate;
3426 }
3427#endif
3428
3429 if (len < INF_TOTAL_FIELDS)
3430 return 0;
3431
3432 chunk_reset(out);
3433 memset(info, 0, sizeof(*info) * len);
3434
3435 info[INF_NAME] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, PRODUCT_NAME);
Willy Tarreau909b9d82019-01-04 18:20:32 +01003436 info[INF_VERSION] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, haproxy_version);
3437 info[INF_RELEASE_DATE] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, haproxy_date);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003438
Yves Lafon95317282018-02-26 11:10:37 +01003439 info[INF_NBTHREAD] = mkf_u32(FO_CONFIG|FS_SERVICE, global.nbthread);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003440 info[INF_NBPROC] = mkf_u32(FO_CONFIG|FS_SERVICE, global.nbproc);
3441 info[INF_PROCESS_NUM] = mkf_u32(FO_KEY, relative_pid);
3442 info[INF_PID] = mkf_u32(FO_STATUS, pid);
3443
3444 info[INF_UPTIME] = mkf_str(FN_DURATION, chunk_newstr(out));
3445 chunk_appendf(out, "%ud %uh%02um%02us", up / 86400, (up % 86400) / 3600, (up % 3600) / 60, (up % 60));
3446
3447 info[INF_UPTIME_SEC] = mkf_u32(FN_DURATION, up);
3448 info[INF_MEMMAX_MB] = mkf_u32(FO_CONFIG|FN_LIMIT, global.rlimit_memmax);
3449 info[INF_POOL_ALLOC_MB] = mkf_u32(0, (unsigned)(pool_total_allocated() / 1048576L));
3450 info[INF_POOL_USED_MB] = mkf_u32(0, (unsigned)(pool_total_used() / 1048576L));
3451 info[INF_POOL_FAILED] = mkf_u32(FN_COUNTER, pool_total_failures());
3452 info[INF_ULIMIT_N] = mkf_u32(FO_CONFIG|FN_LIMIT, global.rlimit_nofile);
3453 info[INF_MAXSOCK] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxsock);
3454 info[INF_MAXCONN] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxconn);
3455 info[INF_HARD_MAXCONN] = mkf_u32(FO_CONFIG|FN_LIMIT, global.hardmaxconn);
3456 info[INF_CURR_CONN] = mkf_u32(0, actconn);
3457 info[INF_CUM_CONN] = mkf_u32(FN_COUNTER, totalconn);
3458 info[INF_CUM_REQ] = mkf_u32(FN_COUNTER, global.req_count);
3459#ifdef USE_OPENSSL
3460 info[INF_MAX_SSL_CONNS] = mkf_u32(FN_MAX, global.maxsslconn);
3461 info[INF_CURR_SSL_CONNS] = mkf_u32(0, sslconns);
3462 info[INF_CUM_SSL_CONNS] = mkf_u32(FN_COUNTER, totalsslconns);
3463#endif
3464 info[INF_MAXPIPES] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxpipes);
3465 info[INF_PIPES_USED] = mkf_u32(0, pipes_used);
3466 info[INF_PIPES_FREE] = mkf_u32(0, pipes_free);
3467 info[INF_CONN_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&global.conn_per_sec));
3468 info[INF_CONN_RATE_LIMIT] = mkf_u32(FO_CONFIG|FN_LIMIT, global.cps_lim);
3469 info[INF_MAX_CONN_RATE] = mkf_u32(FN_MAX, global.cps_max);
3470 info[INF_SESS_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&global.sess_per_sec));
3471 info[INF_SESS_RATE_LIMIT] = mkf_u32(FO_CONFIG|FN_LIMIT, global.sps_lim);
3472 info[INF_MAX_SESS_RATE] = mkf_u32(FN_RATE, global.sps_max);
3473
3474#ifdef USE_OPENSSL
3475 info[INF_SSL_RATE] = mkf_u32(FN_RATE, ssl_sess_rate);
3476 info[INF_SSL_RATE_LIMIT] = mkf_u32(FO_CONFIG|FN_LIMIT, global.ssl_lim);
3477 info[INF_MAX_SSL_RATE] = mkf_u32(FN_MAX, global.ssl_max);
3478 info[INF_SSL_FRONTEND_KEY_RATE] = mkf_u32(0, ssl_key_rate);
3479 info[INF_SSL_FRONTEND_MAX_KEY_RATE] = mkf_u32(FN_MAX, global.ssl_fe_keys_max);
3480 info[INF_SSL_FRONTEND_SESSION_REUSE_PCT] = mkf_u32(0, ssl_reuse);
3481 info[INF_SSL_BACKEND_KEY_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&global.ssl_be_keys_per_sec));
3482 info[INF_SSL_BACKEND_MAX_KEY_RATE] = mkf_u32(FN_MAX, global.ssl_be_keys_max);
3483 info[INF_SSL_CACHE_LOOKUPS] = mkf_u32(FN_COUNTER, global.shctx_lookups);
3484 info[INF_SSL_CACHE_MISSES] = mkf_u32(FN_COUNTER, global.shctx_misses);
3485#endif
3486 info[INF_COMPRESS_BPS_IN] = mkf_u32(FN_RATE, read_freq_ctr(&global.comp_bps_in));
3487 info[INF_COMPRESS_BPS_OUT] = mkf_u32(FN_RATE, read_freq_ctr(&global.comp_bps_out));
3488 info[INF_COMPRESS_BPS_RATE_LIM] = mkf_u32(FO_CONFIG|FN_LIMIT, global.comp_rate_lim);
3489#ifdef USE_ZLIB
3490 info[INF_ZLIB_MEM_USAGE] = mkf_u32(0, zlib_used_memory);
3491 info[INF_MAX_ZLIB_MEM_USAGE] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxzlibmem);
3492#endif
3493 info[INF_TASKS] = mkf_u32(0, nb_tasks_cur);
Christopher Faulet34c5cc92016-12-06 09:15:30 +01003494 info[INF_RUN_QUEUE] = mkf_u32(0, tasks_run_queue_cur);
Willy Tarreau81036f22019-05-20 19:24:50 +02003495 info[INF_IDLE_PCT] = mkf_u32(FN_AVG, ti->idle_pct);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003496 info[INF_NODE] = mkf_str(FO_CONFIG|FN_OUTPUT|FS_SERVICE, global.node);
3497 if (global.desc)
3498 info[INF_DESCRIPTION] = mkf_str(FO_CONFIG|FN_OUTPUT|FS_SERVICE, global.desc);
Willy Tarreau00098ea2018-11-05 14:38:13 +01003499 info[INF_STOPPING] = mkf_u32(0, stopping);
3500 info[INF_JOBS] = mkf_u32(0, jobs);
William Lallemanda7199262018-11-16 16:57:20 +01003501 info[INF_UNSTOPPABLE_JOBS] = mkf_u32(0, unstoppable_jobs);
Willy Tarreau00098ea2018-11-05 14:38:13 +01003502 info[INF_LISTENERS] = mkf_u32(0, listeners);
Willy Tarreau199ad242018-11-05 16:31:22 +01003503 info[INF_ACTIVE_PEERS] = mkf_u32(0, active_peers);
Willy Tarreau2d372c22018-11-05 17:12:27 +01003504 info[INF_CONNECTED_PEERS] = mkf_u32(0, connected_peers);
Willy Tarreau13ef7732018-11-12 07:25:28 +01003505 info[INF_DROPPED_LOGS] = mkf_u32(0, dropped_logs);
Willy Tarreaubeb859a2018-11-22 18:07:59 +01003506 info[INF_BUSY_POLLING] = mkf_u32(0, !!(global.tune.options & GTUNE_BUSY_POLLING));
Baptiste Assmann333939c2019-01-21 08:34:50 +01003507 info[INF_FAILED_RESOLUTIONS] = mkf_u32(0, dns_failed_resolutions);
Willy Tarreau7cf0e452019-05-23 11:39:14 +02003508 info[INF_TOTAL_BYTES_OUT] = mkf_u64(0, global.out_bytes);
3509 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 +02003510 info[INF_DEBUG_COMMANDS_ISSUED] = mkf_u32(0, debug_commands_issued);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003511
3512 return 1;
3513}
3514
3515/* This function dumps information onto the stream interface's read buffer.
3516 * It returns 0 as long as it does not complete, non-zero upon completion.
3517 * No state is used.
3518 */
3519static int stats_dump_info_to_buffer(struct stream_interface *si)
3520{
3521 struct appctx *appctx = __objt_appctx(si->end);
3522
3523 if (!stats_fill_info(info, INF_TOTAL_FIELDS))
3524 return 0;
3525
3526 chunk_reset(&trash);
3527
3528 if (appctx->ctx.stats.flags & STAT_FMT_TYPED)
Willy Tarreau43241ff2019-10-09 11:27:51 +02003529 stats_dump_typed_info_fields(&trash, info, appctx->ctx.stats.flags);
Simon Horman05ee2132017-01-04 09:37:25 +01003530 else if (appctx->ctx.stats.flags & STAT_FMT_JSON)
Willy Tarreau43241ff2019-10-09 11:27:51 +02003531 stats_dump_json_info_fields(&trash, info, appctx->ctx.stats.flags);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003532 else
Willy Tarreau43241ff2019-10-09 11:27:51 +02003533 stats_dump_info_fields(&trash, info, appctx->ctx.stats.flags);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003534
Willy Tarreau06d80a92017-10-19 14:32:15 +02003535 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01003536 si_rx_room_blk(si);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003537 return 0;
3538 }
3539
3540 return 1;
3541}
3542
Simon Horman6f6bb382017-01-04 09:37:26 +01003543/* This function dumps the schema onto the stream interface's read buffer.
3544 * It returns 0 as long as it does not complete, non-zero upon completion.
3545 * No state is used.
3546 *
3547 * Integer values bouned to the range [-(2**53)+1, (2**53)-1] as
3548 * per the recommendation for interoperable integers in section 6 of RFC 7159.
3549 */
Willy Tarreau83061a82018-07-13 11:56:34 +02003550static void stats_dump_json_schema(struct buffer *out)
Simon Horman6f6bb382017-01-04 09:37:26 +01003551{
3552
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003553 int old_len = out->data;
Simon Horman6f6bb382017-01-04 09:37:26 +01003554
3555 chunk_strcat(out,
3556 "{"
3557 "\"$schema\":\"http://json-schema.org/draft-04/schema#\","
3558 "\"oneOf\":["
3559 "{"
3560 "\"title\":\"Info\","
3561 "\"type\":\"array\","
3562 "\"items\":{"
3563 "\"properties\":{"
3564 "\"title\":\"InfoItem\","
3565 "\"type\":\"object\","
3566 "\"field\":{\"$ref\":\"#/definitions/field\"},"
3567 "\"processNum\":{\"$ref\":\"#/definitions/processNum\"},"
3568 "\"tags\":{\"$ref\":\"#/definitions/tags\"},"
3569 "\"value\":{\"$ref\":\"#/definitions/typedValue\"}"
3570 "},"
3571 "\"required\":[\"field\",\"processNum\",\"tags\","
3572 "\"value\"]"
3573 "}"
3574 "},"
3575 "{"
3576 "\"title\":\"Stat\","
3577 "\"type\":\"array\","
3578 "\"items\":{"
3579 "\"title\":\"InfoItem\","
3580 "\"type\":\"object\","
3581 "\"properties\":{"
3582 "\"objType\":{"
3583 "\"enum\":[\"Frontend\",\"Backend\",\"Listener\","
3584 "\"Server\",\"Unknown\"]"
3585 "},"
3586 "\"proxyId\":{"
3587 "\"type\":\"integer\","
3588 "\"minimum\":0"
3589 "},"
3590 "\"id\":{"
3591 "\"type\":\"integer\","
3592 "\"minimum\":0"
3593 "},"
3594 "\"field\":{\"$ref\":\"#/definitions/field\"},"
3595 "\"processNum\":{\"$ref\":\"#/definitions/processNum\"},"
3596 "\"tags\":{\"$ref\":\"#/definitions/tags\"},"
3597 "\"typedValue\":{\"$ref\":\"#/definitions/typedValue\"}"
3598 "},"
3599 "\"required\":[\"objType\",\"proxyId\",\"id\","
3600 "\"field\",\"processNum\",\"tags\","
3601 "\"value\"]"
3602 "}"
3603 "},"
3604 "{"
3605 "\"title\":\"Error\","
3606 "\"type\":\"object\","
3607 "\"properties\":{"
3608 "\"errorStr\":{"
3609 "\"type\":\"string\""
3610 "},"
3611 "\"required\":[\"errorStr\"]"
3612 "}"
3613 "}"
3614 "],"
3615 "\"definitions\":{"
3616 "\"field\":{"
3617 "\"type\":\"object\","
3618 "\"pos\":{"
3619 "\"type\":\"integer\","
3620 "\"minimum\":0"
3621 "},"
3622 "\"name\":{"
3623 "\"type\":\"string\""
3624 "},"
3625 "\"required\":[\"pos\",\"name\"]"
3626 "},"
3627 "\"processNum\":{"
3628 "\"type\":\"integer\","
3629 "\"minimum\":1"
3630 "},"
3631 "\"tags\":{"
3632 "\"type\":\"object\","
3633 "\"origin\":{"
3634 "\"type\":\"string\","
3635 "\"enum\":[\"Metric\",\"Status\",\"Key\","
3636 "\"Config\",\"Product\",\"Unknown\"]"
3637 "},"
3638 "\"nature\":{"
3639 "\"type\":\"string\","
3640 "\"enum\":[\"Gauge\",\"Limit\",\"Min\",\"Max\","
3641 "\"Rate\",\"Counter\",\"Duration\","
3642 "\"Age\",\"Time\",\"Name\",\"Output\","
3643 "\"Avg\", \"Unknown\"]"
3644 "},"
3645 "\"scope\":{"
3646 "\"type\":\"string\","
3647 "\"enum\":[\"Cluster\",\"Process\",\"Service\","
3648 "\"System\",\"Unknown\"]"
3649 "},"
3650 "\"required\":[\"origin\",\"nature\",\"scope\"]"
3651 "},"
3652 "\"typedValue\":{"
3653 "\"type\":\"object\","
3654 "\"oneOf\":["
3655 "{\"$ref\":\"#/definitions/typedValue/definitions/s32Value\"},"
3656 "{\"$ref\":\"#/definitions/typedValue/definitions/s64Value\"},"
3657 "{\"$ref\":\"#/definitions/typedValue/definitions/u32Value\"},"
3658 "{\"$ref\":\"#/definitions/typedValue/definitions/u64Value\"},"
3659 "{\"$ref\":\"#/definitions/typedValue/definitions/strValue\"}"
3660 "],"
3661 "\"definitions\":{"
3662 "\"s32Value\":{"
3663 "\"properties\":{"
3664 "\"type\":{"
3665 "\"type\":\"string\","
3666 "\"enum\":[\"s32\"]"
3667 "},"
3668 "\"value\":{"
3669 "\"type\":\"integer\","
3670 "\"minimum\":-2147483648,"
3671 "\"maximum\":2147483647"
3672 "}"
3673 "},"
3674 "\"required\":[\"type\",\"value\"]"
3675 "},"
3676 "\"s64Value\":{"
3677 "\"properties\":{"
3678 "\"type\":{"
3679 "\"type\":\"string\","
3680 "\"enum\":[\"s64\"]"
3681 "},"
3682 "\"value\":{"
3683 "\"type\":\"integer\","
3684 "\"minimum\":-9007199254740991,"
3685 "\"maximum\":9007199254740991"
3686 "}"
3687 "},"
3688 "\"required\":[\"type\",\"value\"]"
3689 "},"
3690 "\"u32Value\":{"
3691 "\"properties\":{"
3692 "\"type\":{"
3693 "\"type\":\"string\","
3694 "\"enum\":[\"u32\"]"
3695 "},"
3696 "\"value\":{"
3697 "\"type\":\"integer\","
3698 "\"minimum\":0,"
3699 "\"maximum\":4294967295"
3700 "}"
3701 "},"
3702 "\"required\":[\"type\",\"value\"]"
3703 "},"
3704 "\"u64Value\":{"
3705 "\"properties\":{"
3706 "\"type\":{"
3707 "\"type\":\"string\","
3708 "\"enum\":[\"u64\"]"
3709 "},"
3710 "\"value\":{"
3711 "\"type\":\"integer\","
3712 "\"minimum\":0,"
3713 "\"maximum\":9007199254740991"
3714 "}"
3715 "},"
3716 "\"required\":[\"type\",\"value\"]"
3717 "},"
3718 "\"strValue\":{"
3719 "\"properties\":{"
3720 "\"type\":{"
3721 "\"type\":\"string\","
3722 "\"enum\":[\"str\"]"
3723 "},"
3724 "\"value\":{\"type\":\"string\"}"
3725 "},"
3726 "\"required\":[\"type\",\"value\"]"
3727 "},"
3728 "\"unknownValue\":{"
3729 "\"properties\":{"
3730 "\"type\":{"
3731 "\"type\":\"integer\","
3732 "\"minimum\":0"
3733 "},"
3734 "\"value\":{"
3735 "\"type\":\"string\","
3736 "\"enum\":[\"unknown\"]"
3737 "}"
3738 "},"
3739 "\"required\":[\"type\",\"value\"]"
3740 "}"
3741 "}"
3742 "}"
3743 "}"
3744 "}");
3745
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003746 if (old_len == out->data) {
Simon Horman6f6bb382017-01-04 09:37:26 +01003747 chunk_reset(out);
3748 chunk_appendf(out,
3749 "{\"errorStr\":\"output buffer too short\"}");
3750 }
3751}
3752
3753/* This function dumps the schema onto the stream interface's read buffer.
3754 * It returns 0 as long as it does not complete, non-zero upon completion.
3755 * No state is used.
3756 */
3757static int stats_dump_json_schema_to_buffer(struct stream_interface *si)
3758{
3759 chunk_reset(&trash);
3760
3761 stats_dump_json_schema(&trash);
3762
Willy Tarreau06d80a92017-10-19 14:32:15 +02003763 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01003764 si_rx_room_blk(si);
Simon Horman6f6bb382017-01-04 09:37:26 +01003765 return 0;
3766 }
3767
3768 return 1;
3769}
3770
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02003771static int cli_parse_clear_counters(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau89d467c2016-11-23 11:02:40 +01003772{
3773 struct proxy *px;
3774 struct server *sv;
3775 struct listener *li;
3776 int clrall = 0;
3777
3778 if (strcmp(args[2], "all") == 0)
3779 clrall = 1;
3780
3781 /* check permissions */
3782 if (!cli_has_level(appctx, ACCESS_LVL_OPER) ||
3783 (clrall && !cli_has_level(appctx, ACCESS_LVL_ADMIN)))
3784 return 1;
3785
Olivier Houchardfbc74e82017-11-24 16:54:05 +01003786 for (px = proxies_list; px; px = px->next) {
Willy Tarreau89d467c2016-11-23 11:02:40 +01003787 if (clrall) {
3788 memset(&px->be_counters, 0, sizeof(px->be_counters));
3789 memset(&px->fe_counters, 0, sizeof(px->fe_counters));
3790 }
3791 else {
3792 px->be_counters.conn_max = 0;
3793 px->be_counters.p.http.rps_max = 0;
3794 px->be_counters.sps_max = 0;
3795 px->be_counters.cps_max = 0;
3796 px->be_counters.nbpend_max = 0;
Christopher Fauletefb41f02019-11-08 14:53:15 +01003797 px->be_counters.qtime_max = 0;
3798 px->be_counters.ctime_max = 0;
3799 px->be_counters.dtime_max = 0;
3800 px->be_counters.ttime_max = 0;
Willy Tarreau89d467c2016-11-23 11:02:40 +01003801
3802 px->fe_counters.conn_max = 0;
3803 px->fe_counters.p.http.rps_max = 0;
3804 px->fe_counters.sps_max = 0;
3805 px->fe_counters.cps_max = 0;
Willy Tarreau89d467c2016-11-23 11:02:40 +01003806 }
3807
3808 for (sv = px->srv; sv; sv = sv->next)
3809 if (clrall)
3810 memset(&sv->counters, 0, sizeof(sv->counters));
3811 else {
3812 sv->counters.cur_sess_max = 0;
3813 sv->counters.nbpend_max = 0;
3814 sv->counters.sps_max = 0;
Christopher Fauletefb41f02019-11-08 14:53:15 +01003815 sv->counters.qtime_max = 0;
3816 sv->counters.ctime_max = 0;
3817 sv->counters.dtime_max = 0;
3818 sv->counters.ttime_max = 0;
Willy Tarreau89d467c2016-11-23 11:02:40 +01003819 }
3820
3821 list_for_each_entry(li, &px->conf.listeners, by_fe)
3822 if (li->counters) {
3823 if (clrall)
3824 memset(li->counters, 0, sizeof(*li->counters));
3825 else
3826 li->counters->conn_max = 0;
3827 }
3828 }
3829
3830 global.cps_max = 0;
3831 global.sps_max = 0;
Olivier Houchard00bc3cb2017-10-17 19:23:25 +02003832 global.ssl_max = 0;
3833 global.ssl_fe_keys_max = 0;
3834 global.ssl_be_keys_max = 0;
Willy Tarreaud80cb4e2018-01-20 19:30:13 +01003835
3836 memset(activity, 0, sizeof(activity));
Willy Tarreau89d467c2016-11-23 11:02:40 +01003837 return 1;
3838}
3839
3840
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02003841static int cli_parse_show_info(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003842{
Willy Tarreau2f397382019-10-09 11:43:59 +02003843 int arg = 2;
3844
Willy Tarreaud25fc792016-12-16 12:33:47 +01003845 appctx->ctx.stats.scope_str = 0;
3846 appctx->ctx.stats.scope_len = 0;
3847 appctx->ctx.stats.flags = 0;
3848
Willy Tarreau2f397382019-10-09 11:43:59 +02003849 while (*args[arg]) {
3850 if (strcmp(args[arg], "typed") == 0)
3851 appctx->ctx.stats.flags = (appctx->ctx.stats.flags & ~STAT_FMT_MASK) | STAT_FMT_TYPED;
3852 else if (strcmp(args[arg], "json") == 0)
3853 appctx->ctx.stats.flags = (appctx->ctx.stats.flags & ~STAT_FMT_MASK) | STAT_FMT_JSON;
3854 else if (strcmp(args[arg], "desc") == 0)
3855 appctx->ctx.stats.flags |= STAT_SHOW_FDESC;
3856 arg++;
3857 }
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003858 return 0;
3859}
3860
3861
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02003862static int cli_parse_show_stat(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau2b812e22016-11-22 16:18:05 +01003863{
Willy Tarreau2f397382019-10-09 11:43:59 +02003864 int arg = 2;
3865
Willy Tarreaud25fc792016-12-16 12:33:47 +01003866 appctx->ctx.stats.scope_str = 0;
3867 appctx->ctx.stats.scope_len = 0;
Willy Tarreau578d6e42019-10-09 11:00:22 +02003868 appctx->ctx.stats.flags = STAT_SHNODE | STAT_SHDESC;
3869
3870 if ((strm_li(si_strm(appctx->owner))->bind_conf->level & ACCESS_LVL_MASK) >= ACCESS_LVL_OPER)
3871 appctx->ctx.stats.flags |= STAT_SHLGNDS;
Willy Tarreaud25fc792016-12-16 12:33:47 +01003872
Willy Tarreau2f397382019-10-09 11:43:59 +02003873 if (*args[arg] && *args[arg+1] && *args[arg+2]) {
Willy Tarreaua1b1ed52016-11-25 08:50:58 +01003874 struct proxy *px;
3875
Willy Tarreau2f397382019-10-09 11:43:59 +02003876 px = proxy_find_by_name(args[arg], 0, 0);
Willy Tarreaua1b1ed52016-11-25 08:50:58 +01003877 if (px)
3878 appctx->ctx.stats.iid = px->uuid;
3879 else
Willy Tarreau2f397382019-10-09 11:43:59 +02003880 appctx->ctx.stats.iid = atoi(args[arg]);
Willy Tarreaua1b1ed52016-11-25 08:50:58 +01003881
Willy Tarreau9d008692019-08-09 11:21:01 +02003882 if (!appctx->ctx.stats.iid)
3883 return cli_err(appctx, "No such proxy.\n");
Willy Tarreaua1b1ed52016-11-25 08:50:58 +01003884
Willy Tarreau2b812e22016-11-22 16:18:05 +01003885 appctx->ctx.stats.flags |= STAT_BOUND;
Willy Tarreau2f397382019-10-09 11:43:59 +02003886 appctx->ctx.stats.type = atoi(args[arg+1]);
3887 appctx->ctx.stats.sid = atoi(args[arg+2]);
3888 arg += 3;
3889 }
3890
3891 while (*args[arg]) {
3892 if (strcmp(args[arg], "typed") == 0)
3893 appctx->ctx.stats.flags = (appctx->ctx.stats.flags & ~STAT_FMT_MASK) | STAT_FMT_TYPED;
3894 else if (strcmp(args[arg], "json") == 0)
3895 appctx->ctx.stats.flags = (appctx->ctx.stats.flags & ~STAT_FMT_MASK) | STAT_FMT_JSON;
3896 else if (strcmp(args[arg], "desc") == 0)
3897 appctx->ctx.stats.flags |= STAT_SHOW_FDESC;
3898 arg++;
Willy Tarreau2b812e22016-11-22 16:18:05 +01003899 }
Willy Tarreau2b812e22016-11-22 16:18:05 +01003900
Willy Tarreau2b812e22016-11-22 16:18:05 +01003901 return 0;
3902}
3903
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003904static int cli_io_handler_dump_info(struct appctx *appctx)
3905{
3906 return stats_dump_info_to_buffer(appctx->owner);
3907}
3908
Willy Tarreau2b812e22016-11-22 16:18:05 +01003909/* This I/O handler runs as an applet embedded in a stream interface. It is
3910 * used to send raw stats over a socket.
3911 */
3912static int cli_io_handler_dump_stat(struct appctx *appctx)
3913{
Christopher Fauletef779222018-10-31 08:47:01 +01003914 return stats_dump_stat_to_buffer(appctx->owner, NULL, NULL);
Willy Tarreau2b812e22016-11-22 16:18:05 +01003915}
3916
Simon Horman6f6bb382017-01-04 09:37:26 +01003917static int cli_io_handler_dump_json_schema(struct appctx *appctx)
3918{
3919 return stats_dump_json_schema_to_buffer(appctx->owner);
3920}
3921
Willy Tarreau2b812e22016-11-22 16:18:05 +01003922/* register cli keywords */
3923static struct cli_kw_list cli_kws = {{ },{
Willy Tarreau89d467c2016-11-23 11:02:40 +01003924 { { "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 +02003925 { { "show", "info", NULL }, "show info : report information about the running process [desc|json|typed]*", cli_parse_show_info, cli_io_handler_dump_info, NULL },
3926 { { "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 +01003927 { { "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 +01003928 {{},}
3929}};
3930
Willy Tarreau0108d902018-11-25 19:14:37 +01003931INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
3932
William Lallemand74c24fb2016-11-21 17:18:36 +01003933struct applet http_stats_applet = {
3934 .obj_type = OBJ_TYPE_APPLET,
3935 .name = "<STATS>", /* used for logging */
3936 .fct = http_stats_io_handler,
3937 .release = NULL,
3938};
3939
William Lallemand74c24fb2016-11-21 17:18:36 +01003940/*
3941 * Local variables:
3942 * c-indent-level: 8
3943 * c-basic-offset: 8
3944 * End:
3945 */