blob: f76fd379081fa5418d8c24c3c7d8053140f12e96 [file] [log] [blame]
William Lallemand74c24fb2016-11-21 17:18:36 +01001/*
2 * Functions dedicated to statistics output and the stats socket
3 *
4 * Copyright 2000-2012 Willy Tarreau <w@1wt.eu>
5 * Copyright 2007-2009 Krzysztof Piotr Oledzki <ole@ans.pl>
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version
10 * 2 of the License, or (at your option) any later version.
11 *
12 */
13
14#include <ctype.h>
15#include <errno.h>
16#include <fcntl.h>
17#include <stdio.h>
18#include <stdlib.h>
19#include <string.h>
20#include <pwd.h>
21#include <grp.h>
22
23#include <sys/socket.h>
24#include <sys/stat.h>
25#include <sys/types.h>
26
27#include <common/cfgparse.h>
28#include <common/compat.h>
29#include <common/config.h>
30#include <common/debug.h>
Willy Tarreau35b51c62018-09-10 15:38:55 +020031#include <common/http.h>
Willy Tarreaub96b77e2018-12-11 10:22:41 +010032#include <common/htx.h>
Willy Tarreau0108d902018-11-25 19:14:37 +010033#include <common/initcall.h>
William Lallemand74c24fb2016-11-21 17:18:36 +010034#include <common/memory.h>
35#include <common/mini-clist.h>
36#include <common/standard.h>
37#include <common/ticks.h>
38#include <common/time.h>
39#include <common/uri_auth.h>
40#include <common/version.h>
41#include <common/base64.h>
42
43#include <types/applet.h>
Willy Tarreau2b812e22016-11-22 16:18:05 +010044#include <types/cli.h>
William Lallemand74c24fb2016-11-21 17:18:36 +010045#include <types/global.h>
46#include <types/dns.h>
William Lallemand9ed62032016-11-21 17:49:11 +010047#include <types/stats.h>
William Lallemand74c24fb2016-11-21 17:18:36 +010048
49#include <proto/backend.h>
50#include <proto/channel.h>
51#include <proto/checks.h>
Willy Tarreau2b812e22016-11-22 16:18:05 +010052#include <proto/cli.h>
William Lallemand74c24fb2016-11-21 17:18:36 +010053#include <proto/compression.h>
Baptiste Assmann333939c2019-01-21 08:34:50 +010054#include <proto/dns.h>
William Lallemand9ed62032016-11-21 17:49:11 +010055#include <proto/stats.h>
William Lallemand74c24fb2016-11-21 17:18:36 +010056#include <proto/fd.h>
57#include <proto/freq_ctr.h>
58#include <proto/frontend.h>
Christopher Fauleted7a0662019-01-14 11:07:34 +010059#include <proto/http_htx.h>
William Lallemand74c24fb2016-11-21 17:18:36 +010060#include <proto/log.h>
61#include <proto/pattern.h>
62#include <proto/pipe.h>
63#include <proto/listener.h>
64#include <proto/map.h>
William Lallemand74c24fb2016-11-21 17:18:36 +010065#include <proto/proxy.h>
66#include <proto/sample.h>
67#include <proto/session.h>
Willy Tarreauc125cef2019-05-10 09:58:43 +020068#include <proto/ssl_sock.h>
William Lallemand74c24fb2016-11-21 17:18:36 +010069#include <proto/stream.h>
70#include <proto/server.h>
71#include <proto/raw_sock.h>
72#include <proto/stream_interface.h>
73#include <proto/task.h>
74
Christopher Faulet2b9b6782019-02-27 16:42:58 +010075/* status codes available for the stats admin page (strictly 4 chars length) */
76const char *stat_status_codes[STAT_STATUS_SIZE] = {
77 [STAT_STATUS_DENY] = "DENY",
78 [STAT_STATUS_DONE] = "DONE",
79 [STAT_STATUS_ERRP] = "ERRP",
80 [STAT_STATUS_EXCD] = "EXCD",
81 [STAT_STATUS_NONE] = "NONE",
82 [STAT_STATUS_PART] = "PART",
83 [STAT_STATUS_UNKN] = "UNKN",
84 [STAT_STATUS_IVAL] = "IVAL",
85};
86
Willy Tarreau0baac8c2016-11-22 16:36:53 +010087/* These are the field names for each INF_* field position. Please pay attention
88 * to always use the exact same name except that the strings for new names must
89 * be lower case or CamelCase while the enum entries must be upper case.
90 */
Willy Tarreaueaa55372019-10-09 07:39:11 +020091const struct name_desc info_fields[INF_TOTAL_FIELDS] = {
Willy Tarreau6d4897e2019-10-11 16:31:46 +020092 [INF_NAME] = { .name = "Name", .desc = "Product name" },
93 [INF_VERSION] = { .name = "Version", .desc = "Product version" },
94 [INF_RELEASE_DATE] = { .name = "Release_date", .desc = "Date of latest source code update" },
95 [INF_NBTHREAD] = { .name = "Nbthread", .desc = "Number of started threads (global.nbthread)" },
96 [INF_NBPROC] = { .name = "Nbproc", .desc = "Number of started worker processes (global.nbproc)" },
97 [INF_PROCESS_NUM] = { .name = "Process_num", .desc = "Relative worker process number (1..Nbproc)" },
98 [INF_PID] = { .name = "Pid", .desc = "This worker process identifier for the system" },
99 [INF_UPTIME] = { .name = "Uptime", .desc = "How long ago this worker process was started (days+hours+minutes+seconds)" },
100 [INF_UPTIME_SEC] = { .name = "Uptime_sec", .desc = "How long ago this worker process was started (seconds)" },
101 [INF_MEMMAX_MB] = { .name = "Memmax_MB", .desc = "Worker process's hard limit on memory usage in MB (-m on command line)" },
102 [INF_POOL_ALLOC_MB] = { .name = "PoolAlloc_MB", .desc = "Amount of memory allocated in pools (in MB)" },
103 [INF_POOL_USED_MB] = { .name = "PoolUsed_MB", .desc = "Amount of pool memory currently used (in MB)" },
104 [INF_POOL_FAILED] = { .name = "PoolFailed", .desc = "Number of failed pool allocations since this worker was started" },
105 [INF_ULIMIT_N] = { .name = "Ulimit-n", .desc = "Hard limit on the number of per-process file descriptors" },
106 [INF_MAXSOCK] = { .name = "Maxsock", .desc = "Hard limit on the number of per-process sockets" },
107 [INF_MAXCONN] = { .name = "Maxconn", .desc = "Hard limit on the number of per-process connections (configured or imposed by Ulimit-n)" },
108 [INF_HARD_MAXCONN] = { .name = "Hard_maxconn", .desc = "Hard limit on the number of per-process connections (imposed by Memmax_MB or Ulimit-n)" },
109 [INF_CURR_CONN] = { .name = "CurrConns", .desc = "Current number of connections on this worker process" },
110 [INF_CUM_CONN] = { .name = "CumConns", .desc = "Total number of connections on this worker process since started" },
111 [INF_CUM_REQ] = { .name = "CumReq", .desc = "Total number of requests on this worker process since started" },
112 [INF_MAX_SSL_CONNS] = { .name = "MaxSslConns", .desc = "Hard limit on the number of per-process SSL endpoints (front+back), 0=unlimited" },
113 [INF_CURR_SSL_CONNS] = { .name = "CurrSslConns", .desc = "Current number of SSL endpoints on this worker process (front+back)" },
114 [INF_CUM_SSL_CONNS] = { .name = "CumSslConns", .desc = "Total number of SSL endpoints on this worker process since started (front+back)" },
115 [INF_MAXPIPES] = { .name = "Maxpipes", .desc = "Hard limit on the number of pipes for splicing, 0=unlimited" },
116 [INF_PIPES_USED] = { .name = "PipesUsed", .desc = "Current number of pipes in use in this worker process" },
117 [INF_PIPES_FREE] = { .name = "PipesFree", .desc = "Current number of allocated and available pipes in this worker process" },
118 [INF_CONN_RATE] = { .name = "ConnRate", .desc = "Number of front connections created on this worker process over the last second" },
119 [INF_CONN_RATE_LIMIT] = { .name = "ConnRateLimit", .desc = "Hard limit for ConnRate (global.maxconnrate)" },
120 [INF_MAX_CONN_RATE] = { .name = "MaxConnRate", .desc = "Highest ConnRate reached on this worker process since started (in connections per second)" },
121 [INF_SESS_RATE] = { .name = "SessRate", .desc = "Number of sessions created on this worker process over the last second" },
122 [INF_SESS_RATE_LIMIT] = { .name = "SessRateLimit", .desc = "Hard limit for SessRate (global.maxsessrate)" },
123 [INF_MAX_SESS_RATE] = { .name = "MaxSessRate", .desc = "Highest SessRate reached on this worker process since started (in sessions per second)" },
124 [INF_SSL_RATE] = { .name = "SslRate", .desc = "Number of SSL connections created on this worker process over the last second" },
125 [INF_SSL_RATE_LIMIT] = { .name = "SslRateLimit", .desc = "Hard limit for SslRate (global.maxsslrate)" },
126 [INF_MAX_SSL_RATE] = { .name = "MaxSslRate", .desc = "Highest SslRate reached on this worker process since started (in connections per second)" },
127 [INF_SSL_FRONTEND_KEY_RATE] = { .name = "SslFrontendKeyRate", .desc = "Number of SSL keys created on frontends in this worker process over the last second" },
128 [INF_SSL_FRONTEND_MAX_KEY_RATE] = { .name = "SslFrontendMaxKeyRate", .desc = "Highest SslFrontendKeyRate reached on this worker process since started (in SSL keys per second)" },
129 [INF_SSL_FRONTEND_SESSION_REUSE_PCT] = { .name = "SslFrontendSessionReuse_pct", .desc = "Percent of frontend SSL connections which did not require a new key" },
130 [INF_SSL_BACKEND_KEY_RATE] = { .name = "SslBackendKeyRate", .desc = "Number of SSL keys created on backends in this worker process over the last second" },
131 [INF_SSL_BACKEND_MAX_KEY_RATE] = { .name = "SslBackendMaxKeyRate", .desc = "Highest SslBackendKeyRate reached on this worker process since started (in SSL keys per second)" },
132 [INF_SSL_CACHE_LOOKUPS] = { .name = "SslCacheLookups", .desc = "Total number of SSL session ID lookups in the SSL session cache on this worker since started" },
133 [INF_SSL_CACHE_MISSES] = { .name = "SslCacheMisses", .desc = "Total number of SSL session ID lookups that didn't find a session in the SSL session cache on this worker since started" },
134 [INF_COMPRESS_BPS_IN] = { .name = "CompressBpsIn", .desc = "Number of bytes submitted to the HTTP compressor in this worker process over the last second" },
135 [INF_COMPRESS_BPS_OUT] = { .name = "CompressBpsOut", .desc = "Number of bytes emitted by the HTTP compressor in this worker process over the last second" },
136 [INF_COMPRESS_BPS_RATE_LIM] = { .name = "CompressBpsRateLim", .desc = "Limit of CompressBpsOut beyond which HTTP compression is automatically disabled" },
137 [INF_ZLIB_MEM_USAGE] = { .name = "ZlibMemUsage", .desc = "Amount of memory currently used by HTTP compression on the current worker process (in bytes)" },
138 [INF_MAX_ZLIB_MEM_USAGE] = { .name = "MaxZlibMemUsage", .desc = "Limit on the amount of memory used by HTTP compression above which it is automatically disabled (in bytes, see global.maxzlibmem)" },
139 [INF_TASKS] = { .name = "Tasks", .desc = "Total number of tasks in the current worker process (active + sleeping)" },
140 [INF_RUN_QUEUE] = { .name = "Run_queue", .desc = "Total number of active tasks+tasklets in the current worker process" },
141 [INF_IDLE_PCT] = { .name = "Idle_pct", .desc = "Percentage of last second spent waiting in the current worker thread" },
142 [INF_NODE] = { .name = "node", .desc = "Node name (global.node)" },
143 [INF_DESCRIPTION] = { .name = "description", .desc = "Node description (global.description)" },
144 [INF_STOPPING] = { .name = "Stopping", .desc = "1 if the worker process is currently stopping, otherwise zero" },
145 [INF_JOBS] = { .name = "Jobs", .desc = "Current number of active jobs on the current worker process (frontend connections, master connections, listeners)" },
146 [INF_UNSTOPPABLE_JOBS] = { .name = "Unstoppable Jobs", .desc = "Current number of unstoppable jobs on the current worker process (master connections)" },
147 [INF_LISTENERS] = { .name = "Listeners", .desc = "Current number of active listeners on the current worker process" },
148 [INF_ACTIVE_PEERS] = { .name = "ActivePeers", .desc = "Current number of verified active peers connections on the current worker process" },
149 [INF_CONNECTED_PEERS] = { .name = "ConnectedPeers", .desc = "Current number of peers having passed the connection step on the current worker process" },
150 [INF_DROPPED_LOGS] = { .name = "DroppedLogs", .desc = "Total number of dropped logs for current worker process since started" },
151 [INF_BUSY_POLLING] = { .name = "BusyPolling", .desc = "1 if busy-polling is currently in use on the worker process, otherwise zero (config.busy-polling)" },
152 [INF_FAILED_RESOLUTIONS] = { .name = "FailedResolutions", .desc = "Total number of failed DNS resolutions in current worker process since started" },
153 [INF_TOTAL_BYTES_OUT] = { .name = "TotalBytesOut", .desc = "Total number of bytes emitted by current worker process since started" },
154 [INF_BYTES_OUT_RATE] = { .name = "BytesOutRate", .desc = "Number of bytes emitted by current worker process over the last second" },
Willy Tarreau9b013702019-10-24 18:18:02 +0200155 [INF_DEBUG_COMMANDS_ISSUED] = { .name = "DebugCommandsIssued", .desc = "Number of debug commands issued on this process (anything > 0 is unsafe)" },
Willy Tarreau0baac8c2016-11-22 16:36:53 +0100156};
157
Willy Tarreaueaa55372019-10-09 07:39:11 +0200158const struct name_desc stat_fields[ST_F_TOTAL_FIELDS] = {
Willy Tarreau6d4897e2019-10-11 16:31:46 +0200159 [ST_F_PXNAME] = { .name = "pxname", .desc = "Proxy name" },
160 [ST_F_SVNAME] = { .name = "svname", .desc = "Server name" },
161 [ST_F_QCUR] = { .name = "qcur", .desc = "Current number of connections waiting in the server of backend queue" },
162 [ST_F_QMAX] = { .name = "qmax", .desc = "Highest value of qcur encountered since process started" },
163 [ST_F_SCUR] = { .name = "scur", .desc = "Current number of sessions on the frontend, backend or server" },
164 [ST_F_SMAX] = { .name = "smax", .desc = "Highest value of scur encountered since process started" },
165 [ST_F_SLIM] = { .name = "slim", .desc = "Frontend/listener/server's maxconn, backend's fullconn" },
166 [ST_F_STOT] = { .name = "stot", .desc = "Total number of sessions since process started" },
167 [ST_F_BIN] = { .name = "bin", .desc = "Total number of request bytes since process started" },
168 [ST_F_BOUT] = { .name = "bout", .desc = "Total number of response bytes since process started" },
169 [ST_F_DREQ] = { .name = "dreq", .desc = "Total number of denied requests since process started" },
170 [ST_F_DRESP] = { .name = "dresp", .desc = "Total number of denied responses since process started" },
171 [ST_F_EREQ] = { .name = "ereq", .desc = "Total number of invalid requests since process started" },
172 [ST_F_ECON] = { .name = "econ", .desc = "Total number of failed connections to server since the worker process started" },
173 [ST_F_ERESP] = { .name = "eresp", .desc = "Total number of invalid responses since the worker process started" },
174 [ST_F_WRETR] = { .name = "wretr", .desc = "Total number of server connection retries since the worker process started" },
175 [ST_F_WREDIS] = { .name = "wredis", .desc = "Total number of server redispatches due to connection failures since the worker process started" },
176 [ST_F_STATUS] = { .name = "status", .desc = "Frontend/listen status: OPEN/WAITING/FULL/STOP; backend: UP/DOWN; server: last check status" },
177 [ST_F_WEIGHT] = { .name = "weight", .desc = "Server weight, or sum of active servers' weights for a backend" },
178 [ST_F_ACT] = { .name = "act", .desc = "Total number of active UP servers with a non-zero weight" },
179 [ST_F_BCK] = { .name = "bck", .desc = "Total number of backup UP servers with a non-zero weight" },
180 [ST_F_CHKFAIL] = { .name = "chkfail", .desc = "Total number of failed individual health checks per server/backend, since the worker process started" },
181 [ST_F_CHKDOWN] = { .name = "chkdown", .desc = "Total number of failed checks causing UP to DOWN server transitions, per server/backend, since the worker process started" },
182 [ST_F_LASTCHG] = { .name = "lastchg", .desc = "How long ago the last server state changed, in seconds" },
183 [ST_F_DOWNTIME] = { .name = "downtime", .desc = "Total time spent in DOWN state, for server or backend" },
184 [ST_F_QLIMIT] = { .name = "qlimit", .desc = "Limit on the number of connections in queue, for servers only (maxqueue argument)" },
185 [ST_F_PID] = { .name = "pid", .desc = "Relative worker process number (1..nbproc)" },
186 [ST_F_IID] = { .name = "iid", .desc = "Frontend or Backend numeric identifier ('id' setting)" },
187 [ST_F_SID] = { .name = "sid", .desc = "Server numeric identifier ('id' setting)" },
188 [ST_F_THROTTLE] = { .name = "throttle", .desc = "Throttling ratio applied to a server's maxconn and weight during the slowstart period (0 to 100%)" },
189 [ST_F_LBTOT] = { .name = "lbtot", .desc = "Total number of requests routed by load balancing since the worker process started (ignores queue pop and stickiness)" },
190 [ST_F_TRACKED] = { .name = "tracked", .desc = "Name of the other server this server tracks for its state" },
191 [ST_F_TYPE] = { .name = "type", .desc = "Type of the object (Listener, Frontend, Backend, Server)" },
192 [ST_F_RATE] = { .name = "rate", .desc = "Total number of sessions processed by this object over the last second (sessions for listeners/frontends, requests for backends/servers)" },
193 [ST_F_RATE_LIM] = { .name = "rate_lim", .desc = "Limit on the number of sessions accepted in a second (frontend only, 'rate-limit sessions' setting)" },
194 [ST_F_RATE_MAX] = { .name = "rate_max", .desc = "Highest value of 'rate' observed since the worker process started" },
195 [ST_F_CHECK_STATUS] = { .name = "check_status", .desc = "Status report of the server's latest health check, prefixed with '*' if a check is currently in progress" },
196 [ST_F_CHECK_CODE] = { .name = "check_code", .desc = "HTTP/SMTP/LDAP status code reported by the latest server health check" },
197 [ST_F_CHECK_DURATION] = { .name = "check_duration", .desc = "Total duration of the latest server health check, in milliseconds" },
198 [ST_F_HRSP_1XX] = { .name = "hrsp_1xx", .desc = "Total number of HTTP responses with status 100-199 returned by this object since the worker process started" },
199 [ST_F_HRSP_2XX] = { .name = "hrsp_2xx", .desc = "Total number of HTTP responses with status 200-299 returned by this object since the worker process started" },
200 [ST_F_HRSP_3XX] = { .name = "hrsp_3xx", .desc = "Total number of HTTP responses with status 300-399 returned by this object since the worker process started" },
201 [ST_F_HRSP_4XX] = { .name = "hrsp_4xx", .desc = "Total number of HTTP responses with status 400-499 returned by this object since the worker process started" },
202 [ST_F_HRSP_5XX] = { .name = "hrsp_5xx", .desc = "Total number of HTTP responses with status 500-599 returned by this object since the worker process started" },
203 [ST_F_HRSP_OTHER] = { .name = "hrsp_other", .desc = "Total number of HTTP responses with status <100, >599 returned by this object since the worker process started (error -1 included)" },
204 [ST_F_HANAFAIL] = { .name = "hanafail", .desc = "Total number of failed checks caused by an 'on-error' directive after an 'observe' condition matched" },
205 [ST_F_REQ_RATE] = { .name = "req_rate", .desc = "Number of HTTP requests processed over the last second on this object" },
206 [ST_F_REQ_RATE_MAX] = { .name = "req_rate_max", .desc = "Highest value of 'req_rate' observed since the worker process started" },
207 [ST_F_REQ_TOT] = { .name = "req_tot", .desc = "Total number of HTTP requests processed by this object since the worker process started" },
208 [ST_F_CLI_ABRT] = { .name = "cli_abrt", .desc = "Total number of requests or connections aborted by the client since the worker process started" },
209 [ST_F_SRV_ABRT] = { .name = "srv_abrt", .desc = "Total number of requests or connections aborted by the server since the worker process started" },
210 [ST_F_COMP_IN] = { .name = "comp_in", .desc = "Total number of bytes submitted to the HTTP compressor for this object since the worker process started" },
211 [ST_F_COMP_OUT] = { .name = "comp_out", .desc = "Total number of bytes emitted by the HTTP compressor for this object since the worker process started" },
212 [ST_F_COMP_BYP] = { .name = "comp_byp", .desc = "Total number of bytes that bypassed HTTP compression for this object since the worker process started (CPU/memory/bandwidth limitation)" },
213 [ST_F_COMP_RSP] = { .name = "comp_rsp", .desc = "Total number of HTTP responses that were compressed for this object since the worker process started" },
214 [ST_F_LASTSESS] = { .name = "lastsess", .desc = "How long ago some traffic was seen on this object on this worker process, in seconds" },
215 [ST_F_LAST_CHK] = { .name = "last_chk", .desc = "Short description of the latest health check report for this server (see also check_desc)" },
216 [ST_F_LAST_AGT] = { .name = "last_agt", .desc = "Short description of the latest agent check report for this server (see also agent_desc)" },
217 [ST_F_QTIME] = { .name = "qtime", .desc = "Time spent in the queue, in milliseconds, averaged over the 1024 last requests (backend/server)" },
218 [ST_F_CTIME] = { .name = "ctime", .desc = "Time spent waiting for a connection to complete, in milliseconds, averaged over the 1024 last requests (backend/server)" },
219 [ST_F_RTIME] = { .name = "rtime", .desc = "Time spent waiting for a server response, in milliseconds, averaged over the 1024 last requests (backend/server)" },
220 [ST_F_TTIME] = { .name = "ttime", .desc = "Total request+response time (request+queue+connect+response+processing), in milliseconds, averaged over the 1024 last requests (backend/server)" },
221 [ST_F_AGENT_STATUS] = { .name = "agent_status", .desc = "Status report of the server's latest agent check, prefixed with '*' if a check is currently in progress" },
222 [ST_F_AGENT_CODE] = { .name = "agent_code", .desc = "Status code reported by the latest server agent check" },
223 [ST_F_AGENT_DURATION] = { .name = "agent_duration", .desc = "Total duration of the latest server agent check, in milliseconds" },
224 [ST_F_CHECK_DESC] = { .name = "check_desc", .desc = "Textual description of the latest health check report for this server" },
225 [ST_F_AGENT_DESC] = { .name = "agent_desc", .desc = "Textual description of the latest agent check report for this server" },
226 [ST_F_CHECK_RISE] = { .name = "check_rise", .desc = "Number of successful health checks before declaring a server UP (server 'rise' setting)" },
227 [ST_F_CHECK_FALL] = { .name = "check_fall", .desc = "Number of failed health checks before declaring a server DOWN (server 'fall' setting)" },
228 [ST_F_CHECK_HEALTH] = { .name = "check_health", .desc = "Current server health check level (0..fall-1=DOWN, fall..rise-1=UP)" },
229 [ST_F_AGENT_RISE] = { .name = "agent_rise", .desc = "Number of successful agent checks before declaring a server UP (server 'rise' setting)" },
230 [ST_F_AGENT_FALL] = { .name = "agent_fall", .desc = "Number of failed agent checks before declaring a server DOWN (server 'fall' setting)" },
231 [ST_F_AGENT_HEALTH] = { .name = "agent_health", .desc = "Current server agent check level (0..fall-1=DOWN, fall..rise-1=UP)" },
232 [ST_F_ADDR] = { .name = "addr", .desc = "Server's address:port, shown only if show-legends is set, or at levels oper/admin for the CLI" },
233 [ST_F_COOKIE] = { .name = "cookie", .desc = "Backend's cookie name or Server's cookie value, shown only if show-legends is set, or at levels oper/admin for the CLI" },
234 [ST_F_MODE] = { .name = "mode", .desc = "'mode' setting (tcp/http/health/cli)" },
235 [ST_F_ALGO] = { .name = "algo", .desc = "Backend's load balancing algorithm, shown only if show-legends is set, or at levels oper/admin for the CLI" },
236 [ST_F_CONN_RATE] = { .name = "conn_rate", .desc = "Number of new connections accepted over the last second on the frontend for this worker process" },
237 [ST_F_CONN_RATE_MAX] = { .name = "conn_rate_max", .desc = "Highest value of 'conn_rate' observed since the worker process started" },
238 [ST_F_CONN_TOT] = { .name = "conn_tot", .desc = "Total number of new connections accepted on this frontend since the worker process started" },
239 [ST_F_INTERCEPTED] = { .name = "intercepted", .desc = "Total number of HTTP requests intercepted on the frontend (redirects/stats/services) since the worker process started" },
240 [ST_F_DCON] = { .name = "dcon", .desc = "Total number of incoming connections blocked on a listener/frontend by a tcp-request connection rule since the worker process started" },
241 [ST_F_DSES] = { .name = "dses", .desc = "Total number of incoming sessions blocked on a listener/frontend by a tcp-request connection rule since the worker process started" },
242 [ST_F_WREW] = { .name = "wrew", .desc = "Total number of failed HTTP header rewrites since the worker process started" },
243 [ST_F_CONNECT] = { .name = "connect", .desc = "Total number of outgoing connection attempts on this backend/server since the worker process started" },
244 [ST_F_REUSE] = { .name = "reuse", .desc = "Total number of reused connection on this backend/server since the worker process started" },
245 [ST_F_CACHE_LOOKUPS] = { .name = "cache_lookups", .desc = "Total number of HTTP requests looked up in the cache on this frontend/backend since the worker process started" },
246 [ST_F_CACHE_HITS] = { .name = "cache_hits", .desc = "Total number of HTTP requests not found in the cache on this frontend/backend since the worker process started" },
247 [ST_F_SRV_ICUR] = { .name = "srv_icur", .desc = "Current number of idle connections available for reuse on this server" },
248 [ST_F_SRV_ILIM] = { .name = "src_ilim", .desc = "Limit on the number of available idle connections on this server (server 'pool_max_conn' directive)" },
Christopher Faulet0d1c2a62019-11-08 14:59:51 +0100249 [ST_F_QT_MAX] = { .name = "qtime_max", .desc = "Maximum observed time spent in the queue, in milliseconds (backend/server)" },
250 [ST_F_CT_MAX] = { .name = "ctime_max", .desc = "Maximum observed time spent waiting for a connection to complete, in milliseconds (backend/server)" },
251 [ST_F_RT_MAX] = { .name = "rtime_max", .desc = "Maximum observed time spent waiting for a server response, in milliseconds (backend/server)" },
252 [ST_F_TT_MAX] = { .name = "ttime_max", .desc = "Maximum observed total request+response time (request+queue+connect+response+processing), in milliseconds (backend/server)" },
Christopher Faulet0159ee42019-12-16 14:40:39 +0100253 [ST_F_EINT] = { .name = "eint", .desc = "Total number of internal errors since process started"},
William Lallemand74c24fb2016-11-21 17:18:36 +0100254};
255
Willy Tarreau0baac8c2016-11-22 16:36:53 +0100256/* one line of info */
Christopher Faulet1bc04c72017-10-29 20:14:08 +0100257static THREAD_LOCAL struct field info[INF_TOTAL_FIELDS];
William Lallemand74c24fb2016-11-21 17:18:36 +0100258/* one line of stats */
Christopher Faulet1bc04c72017-10-29 20:14:08 +0100259static THREAD_LOCAL struct field stats[ST_F_TOTAL_FIELDS];
William Lallemand74c24fb2016-11-21 17:18:36 +0100260
Christopher Faulet6338a082019-09-09 15:50:54 +0200261static void stats_dump_json_schema(struct buffer *out);
William Lallemand74c24fb2016-11-21 17:18:36 +0100262
Christopher Fauletef779222018-10-31 08:47:01 +0100263static int stats_putchk(struct channel *chn, struct htx *htx, struct buffer *chk)
264{
265 if (htx) {
Christopher Faulet69fc88c2019-01-07 14:27:53 +0100266 if (chk->data >= channel_htx_recv_max(chn, htx))
267 return 0;
Willy Tarreau0a7ef022019-05-28 10:30:11 +0200268 if (!htx_add_data_atonce(htx, ist2(chk->area, chk->data)))
Christopher Fauletef779222018-10-31 08:47:01 +0100269 return 0;
Christopher Faulet5adbeeb2019-01-02 14:34:39 +0100270 channel_add_input(chn, chk->data);
Christopher Fauletef779222018-10-31 08:47:01 +0100271 chk->data = 0;
Christopher Fauletef779222018-10-31 08:47:01 +0100272 }
273 else {
274 if (ci_putchk(chn, chk) == -1)
275 return 0;
276 }
277 return 1;
278}
Willy Tarreau0baac8c2016-11-22 16:36:53 +0100279
Christopher Fauleted7a0662019-01-14 11:07:34 +0100280static const char *stats_scope_ptr(struct appctx *appctx, struct stream_interface *si)
281{
Christopher Fauletb7f88902019-07-15 21:56:43 +0200282 struct channel *req = si_oc(si);
283 struct htx *htx = htxbuf(&req->buf);
284 struct htx_blk *blk;
285 struct ist uri;
Christopher Fauleted7a0662019-01-14 11:07:34 +0100286
Christopher Fauletb7f88902019-07-15 21:56:43 +0200287 blk = htx_get_head_blk(htx);
Christopher Fauletea009732019-11-18 15:50:25 +0100288 BUG_ON(!blk || htx_get_blk_type(blk) != HTX_BLK_REQ_SL);
Christopher Fauletb7f88902019-07-15 21:56:43 +0200289 ALREADY_CHECKED(blk);
290 uri = htx_sl_req_uri(htx_get_blk_ptr(htx, blk));
291 return uri.ptr + appctx->ctx.stats.scope_str;
Christopher Fauleted7a0662019-01-14 11:07:34 +0100292}
293
William Lallemand74c24fb2016-11-21 17:18:36 +0100294/*
295 * http_stats_io_handler()
296 * -> stats_dump_stat_to_buffer() // same as above, but used for CSV or HTML
297 * -> stats_dump_csv_header() // emits the CSV headers (same as above)
Simon Horman05ee2132017-01-04 09:37:25 +0100298 * -> stats_dump_json_header() // emits the JSON headers (same as above)
William Lallemand74c24fb2016-11-21 17:18:36 +0100299 * -> stats_dump_html_head() // emits the HTML headers
300 * -> stats_dump_html_info() // emits the equivalent of "show info" at the top
301 * -> stats_dump_proxy_to_buffer() // same as above, valid for CSV and HTML
302 * -> stats_dump_html_px_hdr()
303 * -> stats_dump_fe_stats()
304 * -> stats_dump_li_stats()
305 * -> stats_dump_sv_stats()
306 * -> stats_dump_be_stats()
307 * -> stats_dump_html_px_end()
308 * -> stats_dump_html_end() // emits HTML trailer
Simon Horman05ee2132017-01-04 09:37:25 +0100309 * -> stats_dump_json_end() // emits JSON trailer
William Lallemand74c24fb2016-11-21 17:18:36 +0100310 */
311
312
William Lallemand74c24fb2016-11-21 17:18:36 +0100313/* Dumps the stats CSV header to the trash buffer which. The caller is responsible
314 * for clearing it if needed.
315 * NOTE: Some tools happen to rely on the field position instead of its name,
316 * so please only append new fields at the end, never in the middle.
317 */
318static void stats_dump_csv_header()
319{
320 int field;
321
322 chunk_appendf(&trash, "# ");
323 for (field = 0; field < ST_F_TOTAL_FIELDS; field++)
Willy Tarreaueaa55372019-10-09 07:39:11 +0200324 chunk_appendf(&trash, "%s,", stat_fields[field].name);
William Lallemand74c24fb2016-11-21 17:18:36 +0100325
326 chunk_appendf(&trash, "\n");
327}
328
329
330/* Emits a stats field without any surrounding element and properly encoded to
331 * resist CSV output. Returns non-zero on success, 0 if the buffer is full.
332 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200333int stats_emit_raw_data_field(struct buffer *out, const struct field *f)
William Lallemand74c24fb2016-11-21 17:18:36 +0100334{
335 switch (field_format(f, 0)) {
336 case FF_EMPTY: return 1;
337 case FF_S32: return chunk_appendf(out, "%d", f->u.s32);
338 case FF_U32: return chunk_appendf(out, "%u", f->u.u32);
339 case FF_S64: return chunk_appendf(out, "%lld", (long long)f->u.s64);
340 case FF_U64: return chunk_appendf(out, "%llu", (unsigned long long)f->u.u64);
Christopher Faulet88a0db22019-09-24 16:35:10 +0200341 case FF_FLT: return chunk_appendf(out, "%f", f->u.flt);
William Lallemand74c24fb2016-11-21 17:18:36 +0100342 case FF_STR: return csv_enc_append(field_str(f, 0), 1, out) != NULL;
343 default: return chunk_appendf(out, "[INCORRECT_FIELD_TYPE_%08x]", f->type);
344 }
345}
346
347/* Emits a stats field prefixed with its type. No CSV encoding is prepared, the
348 * output is supposed to be used on its own line. Returns non-zero on success, 0
349 * if the buffer is full.
350 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200351int stats_emit_typed_data_field(struct buffer *out, const struct field *f)
William Lallemand74c24fb2016-11-21 17:18:36 +0100352{
353 switch (field_format(f, 0)) {
354 case FF_EMPTY: return 1;
355 case FF_S32: return chunk_appendf(out, "s32:%d", f->u.s32);
356 case FF_U32: return chunk_appendf(out, "u32:%u", f->u.u32);
357 case FF_S64: return chunk_appendf(out, "s64:%lld", (long long)f->u.s64);
358 case FF_U64: return chunk_appendf(out, "u64:%llu", (unsigned long long)f->u.u64);
Christopher Faulet88a0db22019-09-24 16:35:10 +0200359 case FF_FLT: return chunk_appendf(out, "flt:%f", f->u.flt);
William Lallemand74c24fb2016-11-21 17:18:36 +0100360 case FF_STR: return chunk_appendf(out, "str:%s", field_str(f, 0));
361 default: return chunk_appendf(out, "%08x:?", f->type);
362 }
363}
364
Simon Horman05ee2132017-01-04 09:37:25 +0100365/* Limit JSON integer values to the range [-(2**53)+1, (2**53)-1] as per
366 * the recommendation for interoperable integers in section 6 of RFC 7159.
367 */
368#define JSON_INT_MAX ((1ULL << 53) - 1)
369#define JSON_INT_MIN (0 - JSON_INT_MAX)
370
371/* Emits a stats field value and its type in JSON.
372 * Returns non-zero on success, 0 on error.
373 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200374int stats_emit_json_data_field(struct buffer *out, const struct field *f)
Simon Horman05ee2132017-01-04 09:37:25 +0100375{
376 int old_len;
377 char buf[20];
378 const char *type, *value = buf, *quote = "";
379
380 switch (field_format(f, 0)) {
381 case FF_EMPTY: return 1;
382 case FF_S32: type = "\"s32\"";
383 snprintf(buf, sizeof(buf), "%d", f->u.s32);
384 break;
385 case FF_U32: type = "\"u32\"";
386 snprintf(buf, sizeof(buf), "%u", f->u.u32);
387 break;
388 case FF_S64: type = "\"s64\"";
389 if (f->u.s64 < JSON_INT_MIN || f->u.s64 > JSON_INT_MAX)
390 return 0;
391 type = "\"s64\"";
392 snprintf(buf, sizeof(buf), "%lld", (long long)f->u.s64);
393 break;
394 case FF_U64: if (f->u.u64 > JSON_INT_MAX)
395 return 0;
396 type = "\"u64\"";
397 snprintf(buf, sizeof(buf), "%llu",
398 (unsigned long long) f->u.u64);
Christopher Faulet52c91bb2019-09-28 10:37:31 +0200399 break;
Christopher Faulet88a0db22019-09-24 16:35:10 +0200400 case FF_FLT: type = "\"flt\"";
401 snprintf(buf, sizeof(buf), "%f", f->u.flt);
Simon Horman05ee2132017-01-04 09:37:25 +0100402 break;
403 case FF_STR: type = "\"str\"";
404 value = field_str(f, 0);
405 quote = "\"";
406 break;
407 default: snprintf(buf, sizeof(buf), "%u", f->type);
408 type = buf;
409 value = "unknown";
410 quote = "\"";
411 break;
412 }
413
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200414 old_len = out->data;
Simon Horman05ee2132017-01-04 09:37:25 +0100415 chunk_appendf(out, ",\"value\":{\"type\":%s,\"value\":%s%s%s}",
416 type, quote, value, quote);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200417 return !(old_len == out->data);
Simon Horman05ee2132017-01-04 09:37:25 +0100418}
419
William Lallemand74c24fb2016-11-21 17:18:36 +0100420/* Emits an encoding of the field type on 3 characters followed by a delimiter.
421 * Returns non-zero on success, 0 if the buffer is full.
422 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200423int stats_emit_field_tags(struct buffer *out, const struct field *f,
424 char delim)
William Lallemand74c24fb2016-11-21 17:18:36 +0100425{
426 char origin, nature, scope;
427
428 switch (field_origin(f, 0)) {
429 case FO_METRIC: origin = 'M'; break;
430 case FO_STATUS: origin = 'S'; break;
431 case FO_KEY: origin = 'K'; break;
432 case FO_CONFIG: origin = 'C'; break;
433 case FO_PRODUCT: origin = 'P'; break;
434 default: origin = '?'; break;
435 }
436
437 switch (field_nature(f, 0)) {
438 case FN_GAUGE: nature = 'G'; break;
439 case FN_LIMIT: nature = 'L'; break;
440 case FN_MIN: nature = 'm'; break;
441 case FN_MAX: nature = 'M'; break;
442 case FN_RATE: nature = 'R'; break;
443 case FN_COUNTER: nature = 'C'; break;
444 case FN_DURATION: nature = 'D'; break;
445 case FN_AGE: nature = 'A'; break;
446 case FN_TIME: nature = 'T'; break;
447 case FN_NAME: nature = 'N'; break;
448 case FN_OUTPUT: nature = 'O'; break;
449 case FN_AVG: nature = 'a'; break;
450 default: nature = '?'; break;
451 }
452
453 switch (field_scope(f, 0)) {
454 case FS_PROCESS: scope = 'P'; break;
455 case FS_SERVICE: scope = 'S'; break;
456 case FS_SYSTEM: scope = 's'; break;
457 case FS_CLUSTER: scope = 'C'; break;
458 default: scope = '?'; break;
459 }
460
461 return chunk_appendf(out, "%c%c%c%c", origin, nature, scope, delim);
462}
463
Simon Horman05ee2132017-01-04 09:37:25 +0100464/* Emits an encoding of the field type as JSON.
465 * Returns non-zero on success, 0 if the buffer is full.
466 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200467int stats_emit_json_field_tags(struct buffer *out, const struct field *f)
Simon Horman05ee2132017-01-04 09:37:25 +0100468{
469 const char *origin, *nature, *scope;
470 int old_len;
471
472 switch (field_origin(f, 0)) {
473 case FO_METRIC: origin = "Metric"; break;
474 case FO_STATUS: origin = "Status"; break;
475 case FO_KEY: origin = "Key"; break;
476 case FO_CONFIG: origin = "Config"; break;
477 case FO_PRODUCT: origin = "Product"; break;
478 default: origin = "Unknown"; break;
479 }
480
481 switch (field_nature(f, 0)) {
482 case FN_GAUGE: nature = "Gauge"; break;
483 case FN_LIMIT: nature = "Limit"; break;
484 case FN_MIN: nature = "Min"; break;
485 case FN_MAX: nature = "Max"; break;
486 case FN_RATE: nature = "Rate"; break;
487 case FN_COUNTER: nature = "Counter"; break;
488 case FN_DURATION: nature = "Duration"; break;
489 case FN_AGE: nature = "Age"; break;
490 case FN_TIME: nature = "Time"; break;
491 case FN_NAME: nature = "Name"; break;
492 case FN_OUTPUT: nature = "Output"; break;
493 case FN_AVG: nature = "Avg"; break;
494 default: nature = "Unknown"; break;
495 }
496
497 switch (field_scope(f, 0)) {
498 case FS_PROCESS: scope = "Process"; break;
499 case FS_SERVICE: scope = "Service"; break;
500 case FS_SYSTEM: scope = "System"; break;
501 case FS_CLUSTER: scope = "Cluster"; break;
502 default: scope = "Unknown"; break;
503 }
504
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200505 old_len = out->data;
Simon Horman05ee2132017-01-04 09:37:25 +0100506 chunk_appendf(out, "\"tags\":{"
507 "\"origin\":\"%s\","
508 "\"nature\":\"%s\","
509 "\"scope\":\"%s\""
510 "}", origin, nature, scope);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200511 return !(old_len == out->data);
Simon Horman05ee2132017-01-04 09:37:25 +0100512}
William Lallemand74c24fb2016-11-21 17:18:36 +0100513
514/* Dump all fields from <stats> into <out> using CSV format */
Willy Tarreau83061a82018-07-13 11:56:34 +0200515static int stats_dump_fields_csv(struct buffer *out,
Willy Tarreau43241ff2019-10-09 11:27:51 +0200516 const struct field *stats, unsigned int flags)
William Lallemand74c24fb2016-11-21 17:18:36 +0100517{
518 int field;
519
520 for (field = 0; field < ST_F_TOTAL_FIELDS; field++) {
521 if (!stats_emit_raw_data_field(out, &stats[field]))
522 return 0;
523 if (!chunk_strcat(out, ","))
524 return 0;
525 }
526 chunk_strcat(out, "\n");
527 return 1;
528}
529
530/* Dump all fields from <stats> into <out> using a typed "field:desc:type:value" format */
Willy Tarreau83061a82018-07-13 11:56:34 +0200531static int stats_dump_fields_typed(struct buffer *out,
Willy Tarreau43241ff2019-10-09 11:27:51 +0200532 const struct field *stats, unsigned int flags)
William Lallemand74c24fb2016-11-21 17:18:36 +0100533{
534 int field;
535
536 for (field = 0; field < ST_F_TOTAL_FIELDS; field++) {
537 if (!stats[field].type)
538 continue;
539
540 chunk_appendf(out, "%c.%u.%u.%d.%s.%u:",
541 stats[ST_F_TYPE].u.u32 == STATS_TYPE_FE ? 'F' :
542 stats[ST_F_TYPE].u.u32 == STATS_TYPE_BE ? 'B' :
543 stats[ST_F_TYPE].u.u32 == STATS_TYPE_SO ? 'L' :
544 stats[ST_F_TYPE].u.u32 == STATS_TYPE_SV ? 'S' :
545 '?',
546 stats[ST_F_IID].u.u32, stats[ST_F_SID].u.u32,
Willy Tarreaueaa55372019-10-09 07:39:11 +0200547 field, stat_fields[field].name, stats[ST_F_PID].u.u32);
William Lallemand74c24fb2016-11-21 17:18:36 +0100548
549 if (!stats_emit_field_tags(out, &stats[field], ':'))
550 return 0;
551 if (!stats_emit_typed_data_field(out, &stats[field]))
552 return 0;
Willy Tarreau6b19b142019-10-09 15:44:21 +0200553 if ((flags & STAT_SHOW_FDESC) && !chunk_appendf(out, ":\"%s\"", stat_fields[field].desc))
554 return 0;
William Lallemand74c24fb2016-11-21 17:18:36 +0100555 if (!chunk_strcat(out, "\n"))
556 return 0;
557 }
558 return 1;
559}
560
Simon Horman05ee2132017-01-04 09:37:25 +0100561/* Dump all fields from <stats> into <out> using the "show info json" format */
Willy Tarreau83061a82018-07-13 11:56:34 +0200562static int stats_dump_json_info_fields(struct buffer *out,
Willy Tarreau43241ff2019-10-09 11:27:51 +0200563 const struct field *info, unsigned int flags)
Simon Horman05ee2132017-01-04 09:37:25 +0100564{
565 int field;
566 int started = 0;
567
568 if (!chunk_strcat(out, "["))
569 return 0;
570
571 for (field = 0; field < INF_TOTAL_FIELDS; field++) {
572 int old_len;
573
574 if (!field_format(info, field))
575 continue;
576
577 if (started && !chunk_strcat(out, ","))
578 goto err;
579 started = 1;
580
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200581 old_len = out->data;
Simon Horman05ee2132017-01-04 09:37:25 +0100582 chunk_appendf(out,
583 "{\"field\":{\"pos\":%d,\"name\":\"%s\"},"
584 "\"processNum\":%u,",
Willy Tarreaueaa55372019-10-09 07:39:11 +0200585 field, info_fields[field].name,
Simon Horman05ee2132017-01-04 09:37:25 +0100586 info[INF_PROCESS_NUM].u.u32);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200587 if (old_len == out->data)
Simon Horman05ee2132017-01-04 09:37:25 +0100588 goto err;
589
590 if (!stats_emit_json_field_tags(out, &info[field]))
591 goto err;
592
593 if (!stats_emit_json_data_field(out, &info[field]))
594 goto err;
595
596 if (!chunk_strcat(out, "}"))
597 goto err;
598 }
599
600 if (!chunk_strcat(out, "]"))
601 goto err;
602 return 1;
603
604err:
605 chunk_reset(out);
606 chunk_appendf(out, "{\"errorStr\":\"output buffer too short\"}");
607 return 0;
608}
609
610/* Dump all fields from <stats> into <out> using a typed "field:desc:type:value" format */
Willy Tarreau83061a82018-07-13 11:56:34 +0200611static int stats_dump_fields_json(struct buffer *out,
612 const struct field *stats,
Willy Tarreaub0ce3ad2019-10-09 11:19:29 +0200613 unsigned int flags)
Simon Horman05ee2132017-01-04 09:37:25 +0100614{
615 int field;
616 int started = 0;
617
Willy Tarreaub0ce3ad2019-10-09 11:19:29 +0200618 if ((flags & STAT_STARTED) && !chunk_strcat(out, ","))
Simon Horman05ee2132017-01-04 09:37:25 +0100619 return 0;
620 if (!chunk_strcat(out, "["))
621 return 0;
622
623 for (field = 0; field < ST_F_TOTAL_FIELDS; field++) {
624 const char *obj_type;
625 int old_len;
626
627 if (!stats[field].type)
628 continue;
629
630 if (started && !chunk_strcat(out, ","))
631 goto err;
632 started = 1;
633
634 switch (stats[ST_F_TYPE].u.u32) {
635 case STATS_TYPE_FE: obj_type = "Frontend"; break;
636 case STATS_TYPE_BE: obj_type = "Backend"; break;
637 case STATS_TYPE_SO: obj_type = "Listener"; break;
638 case STATS_TYPE_SV: obj_type = "Server"; break;
639 default: obj_type = "Unknown"; break;
640 }
641
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200642 old_len = out->data;
Simon Horman05ee2132017-01-04 09:37:25 +0100643 chunk_appendf(out,
644 "{"
645 "\"objType\":\"%s\","
646 "\"proxyId\":%d,"
647 "\"id\":%d,"
648 "\"field\":{\"pos\":%d,\"name\":\"%s\"},"
649 "\"processNum\":%u,",
650 obj_type, stats[ST_F_IID].u.u32,
651 stats[ST_F_SID].u.u32, field,
Willy Tarreaueaa55372019-10-09 07:39:11 +0200652 stat_fields[field].name, stats[ST_F_PID].u.u32);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200653 if (old_len == out->data)
Simon Horman05ee2132017-01-04 09:37:25 +0100654 goto err;
655
656 if (!stats_emit_json_field_tags(out, &stats[field]))
657 goto err;
658
659 if (!stats_emit_json_data_field(out, &stats[field]))
660 goto err;
661
662 if (!chunk_strcat(out, "}"))
663 goto err;
664 }
665
666 if (!chunk_strcat(out, "]"))
667 goto err;
668
669 return 1;
670
671err:
672 chunk_reset(out);
Willy Tarreaub0ce3ad2019-10-09 11:19:29 +0200673 if (flags & STAT_STARTED)
674 chunk_strcat(out, ",");
Simon Horman05ee2132017-01-04 09:37:25 +0100675 chunk_appendf(out, "{\"errorStr\":\"output buffer too short\"}");
676 return 0;
677}
678
William Lallemand74c24fb2016-11-21 17:18:36 +0100679/* Dump all fields from <stats> into <out> using the HTML format. A column is
Willy Tarreauab02b3f2019-10-09 11:11:46 +0200680 * reserved for the checkbox is STAT_ADMIN is set in <flags>. Some extra info
Willy Tarreau708c4162019-10-09 10:19:16 +0200681 * are provided if STAT_SHLGNDS is present in <flags>.
William Lallemand74c24fb2016-11-21 17:18:36 +0100682 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200683static int stats_dump_fields_html(struct buffer *out,
684 const struct field *stats,
685 unsigned int flags)
William Lallemand74c24fb2016-11-21 17:18:36 +0100686{
Willy Tarreau83061a82018-07-13 11:56:34 +0200687 struct buffer src;
William Lallemand74c24fb2016-11-21 17:18:36 +0100688
689 if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_FE) {
690 chunk_appendf(out,
691 /* name, queue */
692 "<tr class=\"frontend\">");
693
Willy Tarreauab02b3f2019-10-09 11:11:46 +0200694 if (flags & STAT_ADMIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +0100695 /* Column sub-heading for Enable or Disable server */
696 chunk_appendf(out, "<td></td>");
697 }
698
699 chunk_appendf(out,
700 "<td class=ac>"
701 "<a name=\"%s/Frontend\"></a>"
702 "<a class=lfsb href=\"#%s/Frontend\">Frontend</a></td>"
703 "<td colspan=3></td>"
704 "",
705 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_PXNAME));
706
707 chunk_appendf(out,
708 /* sessions rate : current */
709 "<td><u>%s<div class=tips><table class=det>"
710 "<tr><th>Current connection rate:</th><td>%s/s</td></tr>"
711 "<tr><th>Current session rate:</th><td>%s/s</td></tr>"
712 "",
713 U2H(stats[ST_F_RATE].u.u32),
714 U2H(stats[ST_F_CONN_RATE].u.u32),
715 U2H(stats[ST_F_RATE].u.u32));
716
717 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
718 chunk_appendf(out,
719 "<tr><th>Current request rate:</th><td>%s/s</td></tr>",
720 U2H(stats[ST_F_REQ_RATE].u.u32));
721
722 chunk_appendf(out,
723 "</table></div></u></td>"
724 /* sessions rate : max */
725 "<td><u>%s<div class=tips><table class=det>"
726 "<tr><th>Max connection rate:</th><td>%s/s</td></tr>"
727 "<tr><th>Max session rate:</th><td>%s/s</td></tr>"
728 "",
729 U2H(stats[ST_F_RATE_MAX].u.u32),
730 U2H(stats[ST_F_CONN_RATE_MAX].u.u32),
731 U2H(stats[ST_F_RATE_MAX].u.u32));
732
733 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
734 chunk_appendf(out,
735 "<tr><th>Max request rate:</th><td>%s/s</td></tr>",
736 U2H(stats[ST_F_REQ_RATE_MAX].u.u32));
737
738 chunk_appendf(out,
739 "</table></div></u></td>"
740 /* sessions rate : limit */
741 "<td>%s</td>",
742 LIM2A(stats[ST_F_RATE_LIM].u.u32, "-"));
743
744 chunk_appendf(out,
745 /* sessions: current, max, limit, total */
746 "<td>%s</td><td>%s</td><td>%s</td>"
747 "<td><u>%s<div class=tips><table class=det>"
748 "<tr><th>Cum. connections:</th><td>%s</td></tr>"
749 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
750 "",
751 U2H(stats[ST_F_SCUR].u.u32), U2H(stats[ST_F_SMAX].u.u32), U2H(stats[ST_F_SLIM].u.u32),
752 U2H(stats[ST_F_STOT].u.u64),
753 U2H(stats[ST_F_CONN_TOT].u.u64),
754 U2H(stats[ST_F_STOT].u.u64));
755
756 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
757 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0) {
758 chunk_appendf(out,
759 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
760 "<tr><th>- HTTP 1xx responses:</th><td>%s</td></tr>"
761 "<tr><th>- HTTP 2xx responses:</th><td>%s</td></tr>"
762 "<tr><th>&nbsp;&nbsp;Compressed 2xx:</th><td>%s</td><td>(%d%%)</td></tr>"
763 "<tr><th>- HTTP 3xx responses:</th><td>%s</td></tr>"
764 "<tr><th>- HTTP 4xx responses:</th><td>%s</td></tr>"
765 "<tr><th>- HTTP 5xx responses:</th><td>%s</td></tr>"
766 "<tr><th>- other responses:</th><td>%s</td></tr>"
767 "<tr><th>Intercepted requests:</th><td>%s</td></tr>"
Willy Tarreaua1214a52018-12-14 14:00:25 +0100768 "<tr><th>Cache lookups:</th><td>%s</td></tr>"
769 "<tr><th>Cache hits:</th><td>%s</td><td>(%d%%)</td></tr>"
Willy Tarreau1b0f85e2018-05-28 15:12:40 +0200770 "<tr><th>Failed hdr rewrites:</th><td>%s</td></tr>"
Christopher Faulet0159ee42019-12-16 14:40:39 +0100771 "<tr><th>Internal errors:</th><td>%s</td></tr>"
William Lallemand74c24fb2016-11-21 17:18:36 +0100772 "",
773 U2H(stats[ST_F_REQ_TOT].u.u64),
774 U2H(stats[ST_F_HRSP_1XX].u.u64),
775 U2H(stats[ST_F_HRSP_2XX].u.u64),
776 U2H(stats[ST_F_COMP_RSP].u.u64),
777 stats[ST_F_HRSP_2XX].u.u64 ?
778 (int)(100 * stats[ST_F_COMP_RSP].u.u64 / stats[ST_F_HRSP_2XX].u.u64) : 0,
779 U2H(stats[ST_F_HRSP_3XX].u.u64),
780 U2H(stats[ST_F_HRSP_4XX].u.u64),
781 U2H(stats[ST_F_HRSP_5XX].u.u64),
782 U2H(stats[ST_F_HRSP_OTHER].u.u64),
Willy Tarreau1b0f85e2018-05-28 15:12:40 +0200783 U2H(stats[ST_F_INTERCEPTED].u.u64),
Willy Tarreaua1214a52018-12-14 14:00:25 +0100784 U2H(stats[ST_F_CACHE_LOOKUPS].u.u64),
785 U2H(stats[ST_F_CACHE_HITS].u.u64),
786 stats[ST_F_CACHE_LOOKUPS].u.u64 ?
787 (int)(100 * stats[ST_F_CACHE_HITS].u.u64 / stats[ST_F_CACHE_LOOKUPS].u.u64) : 0,
Christopher Faulet0159ee42019-12-16 14:40:39 +0100788 U2H(stats[ST_F_WREW].u.u64),
789 U2H(stats[ST_F_EINT].u.u64));
William Lallemand74c24fb2016-11-21 17:18:36 +0100790 }
791
792 chunk_appendf(out,
793 "</table></div></u></td>"
794 /* sessions: lbtot, lastsess */
795 "<td></td><td></td>"
796 /* bytes : in */
797 "<td>%s</td>"
798 "",
799 U2H(stats[ST_F_BIN].u.u64));
800
801 chunk_appendf(out,
802 /* bytes:out + compression stats (via hover): comp_in, comp_out, comp_byp */
803 "<td>%s%s<div class=tips><table class=det>"
804 "<tr><th>Response bytes in:</th><td>%s</td></tr>"
805 "<tr><th>Compression in:</th><td>%s</td></tr>"
806 "<tr><th>Compression out:</th><td>%s</td><td>(%d%%)</td></tr>"
807 "<tr><th>Compression bypass:</th><td>%s</td></tr>"
808 "<tr><th>Total bytes saved:</th><td>%s</td><td>(%d%%)</td></tr>"
809 "</table></div>%s</td>",
810 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "<u>":"",
811 U2H(stats[ST_F_BOUT].u.u64),
812 U2H(stats[ST_F_BOUT].u.u64),
813 U2H(stats[ST_F_COMP_IN].u.u64),
814 U2H(stats[ST_F_COMP_OUT].u.u64),
815 stats[ST_F_COMP_IN].u.u64 ? (int)(stats[ST_F_COMP_OUT].u.u64 * 100 / stats[ST_F_COMP_IN].u.u64) : 0,
816 U2H(stats[ST_F_COMP_BYP].u.u64),
817 U2H(stats[ST_F_COMP_IN].u.u64 - stats[ST_F_COMP_OUT].u.u64),
818 stats[ST_F_BOUT].u.u64 ? (int)((stats[ST_F_COMP_IN].u.u64 - stats[ST_F_COMP_OUT].u.u64) * 100 / stats[ST_F_BOUT].u.u64) : 0,
819 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "</u>":"");
820
821 chunk_appendf(out,
822 /* denied: req, resp */
823 "<td>%s</td><td>%s</td>"
824 /* errors : request, connect, response */
825 "<td>%s</td><td></td><td></td>"
826 /* warnings: retries, redispatches */
827 "<td></td><td></td>"
828 /* server status : reflect frontend status */
829 "<td class=ac>%s</td>"
830 /* rest of server: nothing */
831 "<td class=ac colspan=8></td></tr>"
832 "",
833 U2H(stats[ST_F_DREQ].u.u64), U2H(stats[ST_F_DRESP].u.u64),
834 U2H(stats[ST_F_EREQ].u.u64),
835 field_str(stats, ST_F_STATUS));
836 }
837 else if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_SO) {
838 chunk_appendf(out, "<tr class=socket>");
Willy Tarreauab02b3f2019-10-09 11:11:46 +0200839 if (flags & STAT_ADMIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +0100840 /* Column sub-heading for Enable or Disable server */
841 chunk_appendf(out, "<td></td>");
842 }
843
844 chunk_appendf(out,
845 /* frontend name, listener name */
846 "<td class=ac><a name=\"%s/+%s\"></a>%s"
847 "<a class=lfsb href=\"#%s/+%s\">%s</a>"
848 "",
849 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME),
Willy Tarreau708c4162019-10-09 10:19:16 +0200850 (flags & STAT_SHLGNDS)?"<u>":"",
William Lallemand74c24fb2016-11-21 17:18:36 +0100851 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME), field_str(stats, ST_F_SVNAME));
852
Willy Tarreau708c4162019-10-09 10:19:16 +0200853 if (flags & STAT_SHLGNDS) {
William Lallemand74c24fb2016-11-21 17:18:36 +0100854 chunk_appendf(out, "<div class=tips>");
855
Willy Tarreau90807112020-02-25 08:16:33 +0100856 if (isdigit((unsigned char)*field_str(stats, ST_F_ADDR)))
William Lallemand74c24fb2016-11-21 17:18:36 +0100857 chunk_appendf(out, "IPv4: %s, ", field_str(stats, ST_F_ADDR));
858 else if (*field_str(stats, ST_F_ADDR) == '[')
859 chunk_appendf(out, "IPv6: %s, ", field_str(stats, ST_F_ADDR));
860 else if (*field_str(stats, ST_F_ADDR))
861 chunk_appendf(out, "%s, ", field_str(stats, ST_F_ADDR));
862
863 /* id */
864 chunk_appendf(out, "id: %d</div>", stats[ST_F_SID].u.u32);
865 }
866
867 chunk_appendf(out,
868 /* queue */
869 "%s</td><td colspan=3></td>"
870 /* sessions rate: current, max, limit */
871 "<td colspan=3>&nbsp;</td>"
872 /* sessions: current, max, limit, total, lbtot, lastsess */
873 "<td>%s</td><td>%s</td><td>%s</td>"
874 "<td>%s</td><td>&nbsp;</td><td>&nbsp;</td>"
875 /* bytes: in, out */
876 "<td>%s</td><td>%s</td>"
877 "",
Willy Tarreau708c4162019-10-09 10:19:16 +0200878 (flags & STAT_SHLGNDS)?"</u>":"",
William Lallemand74c24fb2016-11-21 17:18:36 +0100879 U2H(stats[ST_F_SCUR].u.u32), U2H(stats[ST_F_SMAX].u.u32), U2H(stats[ST_F_SLIM].u.u32),
880 U2H(stats[ST_F_STOT].u.u64), U2H(stats[ST_F_BIN].u.u64), U2H(stats[ST_F_BOUT].u.u64));
881
882 chunk_appendf(out,
883 /* denied: req, resp */
884 "<td>%s</td><td>%s</td>"
885 /* errors: request, connect, response */
886 "<td>%s</td><td></td><td></td>"
887 /* warnings: retries, redispatches */
888 "<td></td><td></td>"
889 /* server status: reflect listener status */
890 "<td class=ac>%s</td>"
891 /* rest of server: nothing */
892 "<td class=ac colspan=8></td></tr>"
893 "",
894 U2H(stats[ST_F_DREQ].u.u64), U2H(stats[ST_F_DRESP].u.u64),
895 U2H(stats[ST_F_EREQ].u.u64),
896 field_str(stats, ST_F_STATUS));
897 }
898 else if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_SV) {
899 const char *style;
900
901 /* determine the style to use depending on the server's state,
902 * its health and weight. There isn't a 1-to-1 mapping between
903 * state and styles for the cases where the server is (still)
904 * up. The reason is that we don't want to report nolb and
905 * drain with the same color.
906 */
907
908 if (strcmp(field_str(stats, ST_F_STATUS), "DOWN") == 0 ||
909 strcmp(field_str(stats, ST_F_STATUS), "DOWN (agent)") == 0) {
910 style = "down";
911 }
912 else if (strcmp(field_str(stats, ST_F_STATUS), "DOWN ") == 0) {
913 style = "going_up";
914 }
Daniel Corbettb4285172020-03-28 12:35:50 -0400915 else if (strcmp(field_str(stats, ST_F_STATUS), "DRAIN") == 0) {
916 style = "draining";
917 }
William Lallemand74c24fb2016-11-21 17:18:36 +0100918 else if (strcmp(field_str(stats, ST_F_STATUS), "NOLB ") == 0) {
919 style = "going_down";
920 }
921 else if (strcmp(field_str(stats, ST_F_STATUS), "NOLB") == 0) {
922 style = "nolb";
923 }
924 else if (strcmp(field_str(stats, ST_F_STATUS), "no check") == 0) {
925 style = "no_check";
926 }
927 else if (!stats[ST_F_CHKFAIL].type ||
928 stats[ST_F_CHECK_HEALTH].u.u32 == stats[ST_F_CHECK_RISE].u.u32 + stats[ST_F_CHECK_FALL].u.u32 - 1) {
929 /* no check or max health = UP */
930 if (stats[ST_F_WEIGHT].u.u32)
931 style = "up";
932 else
933 style = "draining";
934 }
935 else {
936 style = "going_down";
937 }
938
939 if (memcmp(field_str(stats, ST_F_STATUS), "MAINT", 5) == 0)
940 chunk_appendf(out, "<tr class=\"maintain\">");
941 else
942 chunk_appendf(out,
943 "<tr class=\"%s_%s\">",
944 (stats[ST_F_BCK].u.u32) ? "backup" : "active", style);
945
946
Willy Tarreauab02b3f2019-10-09 11:11:46 +0200947 if (flags & STAT_ADMIN)
William Lallemand74c24fb2016-11-21 17:18:36 +0100948 chunk_appendf(out,
David Harrigand3db35a2016-12-30 12:12:49 +0000949 "<td><input class='%s-checkbox' type=\"checkbox\" name=\"s\" value=\"%s\"></td>",
950 field_str(stats, ST_F_PXNAME),
William Lallemand74c24fb2016-11-21 17:18:36 +0100951 field_str(stats, ST_F_SVNAME));
952
953 chunk_appendf(out,
954 "<td class=ac><a name=\"%s/%s\"></a>%s"
955 "<a class=lfsb href=\"#%s/%s\">%s</a>"
956 "",
957 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME),
Willy Tarreau708c4162019-10-09 10:19:16 +0200958 (flags & STAT_SHLGNDS) ? "<u>" : "",
William Lallemand74c24fb2016-11-21 17:18:36 +0100959 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME), field_str(stats, ST_F_SVNAME));
960
Willy Tarreau708c4162019-10-09 10:19:16 +0200961 if (flags & STAT_SHLGNDS) {
William Lallemand74c24fb2016-11-21 17:18:36 +0100962 chunk_appendf(out, "<div class=tips>");
963
Willy Tarreau90807112020-02-25 08:16:33 +0100964 if (isdigit((unsigned char)*field_str(stats, ST_F_ADDR)))
William Lallemand74c24fb2016-11-21 17:18:36 +0100965 chunk_appendf(out, "IPv4: %s, ", field_str(stats, ST_F_ADDR));
966 else if (*field_str(stats, ST_F_ADDR) == '[')
967 chunk_appendf(out, "IPv6: %s, ", field_str(stats, ST_F_ADDR));
968 else if (*field_str(stats, ST_F_ADDR))
969 chunk_appendf(out, "%s, ", field_str(stats, ST_F_ADDR));
970
971 /* id */
972 chunk_appendf(out, "id: %d", stats[ST_F_SID].u.u32);
973
974 /* cookie */
975 if (stats[ST_F_COOKIE].type) {
976 chunk_appendf(out, ", cookie: '");
977 chunk_initstr(&src, field_str(stats, ST_F_COOKIE));
978 chunk_htmlencode(out, &src);
979 chunk_appendf(out, "'");
980 }
981
982 chunk_appendf(out, "</div>");
983 }
984
985 chunk_appendf(out,
986 /* queue : current, max, limit */
987 "%s</td><td>%s</td><td>%s</td><td>%s</td>"
988 /* sessions rate : current, max, limit */
989 "<td>%s</td><td>%s</td><td></td>"
990 "",
Willy Tarreau708c4162019-10-09 10:19:16 +0200991 (flags & STAT_SHLGNDS) ? "</u>" : "",
William Lallemand74c24fb2016-11-21 17:18:36 +0100992 U2H(stats[ST_F_QCUR].u.u32), U2H(stats[ST_F_QMAX].u.u32), LIM2A(stats[ST_F_QLIMIT].u.u32, "-"),
993 U2H(stats[ST_F_RATE].u.u32), U2H(stats[ST_F_RATE_MAX].u.u32));
994
995 chunk_appendf(out,
996 /* sessions: current, max, limit, total */
Willy Tarreauf21d17b2019-09-08 09:24:56 +0200997 "<td><u>%s<div class=tips>"
998 "<table class=det>"
999 "<tr><th>Current active connections:</th><td>%s</td></tr>"
1000 "<tr><th>Current idle connections:</th><td>%s</td></tr>"
1001 "<tr><th>Active connections limit:</th><td>%s</td></tr>"
1002 "<tr><th>Idle connections limit:</th><td>%s</td></tr>"
1003 "</table></div></u>"
1004 "</td><td>%s</td><td>%s</td>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001005 "<td><u>%s<div class=tips><table class=det>"
1006 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
1007 "",
Willy Tarreauf21d17b2019-09-08 09:24:56 +02001008 U2H(stats[ST_F_SCUR].u.u32),
1009 U2H(stats[ST_F_SCUR].u.u32),
1010 U2H(stats[ST_F_SRV_ICUR].u.u32),
1011 LIM2A(stats[ST_F_SLIM].u.u32, "-"),
1012 stats[ST_F_SRV_ILIM].type ? U2H(stats[ST_F_SRV_ILIM].u.u32) : "-",
1013 U2H(stats[ST_F_SMAX].u.u32), LIM2A(stats[ST_F_SLIM].u.u32, "-"),
William Lallemand74c24fb2016-11-21 17:18:36 +01001014 U2H(stats[ST_F_STOT].u.u64),
1015 U2H(stats[ST_F_STOT].u.u64));
1016
1017 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
1018 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0) {
1019 unsigned long long tot;
1020
1021 tot = stats[ST_F_HRSP_OTHER].u.u64;
1022 tot += stats[ST_F_HRSP_1XX].u.u64;
1023 tot += stats[ST_F_HRSP_2XX].u.u64;
1024 tot += stats[ST_F_HRSP_3XX].u.u64;
1025 tot += stats[ST_F_HRSP_4XX].u.u64;
1026 tot += stats[ST_F_HRSP_5XX].u.u64;
1027
1028 chunk_appendf(out,
Willy Tarreauf1573842018-12-14 11:35:36 +01001029 "<tr><th>New connections:</th><td>%s</td></tr>"
1030 "<tr><th>Reused connections:</th><td>%s</td><td>(%d%%)</td></tr>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001031 "<tr><th>Cum. HTTP responses:</th><td>%s</td></tr>"
1032 "<tr><th>- HTTP 1xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
1033 "<tr><th>- HTTP 2xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
1034 "<tr><th>- HTTP 3xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
1035 "<tr><th>- HTTP 4xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
1036 "<tr><th>- HTTP 5xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
1037 "<tr><th>- other responses:</th><td>%s</td><td>(%d%%)</td></tr>"
Willy Tarreau1b0f85e2018-05-28 15:12:40 +02001038 "<tr><th>Failed hdr rewrites:</th><td>%s</td></tr>"
Christopher Faulet0159ee42019-12-16 14:40:39 +01001039 "<tr><th>Internal error:</th><td>%s</td></tr>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001040 "",
Willy Tarreauf1573842018-12-14 11:35:36 +01001041 U2H(stats[ST_F_CONNECT].u.u64),
1042 U2H(stats[ST_F_REUSE].u.u64),
1043 (stats[ST_F_CONNECT].u.u64 + stats[ST_F_REUSE].u.u64) ?
1044 (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 +01001045 U2H(tot),
1046 U2H(stats[ST_F_HRSP_1XX].u.u64), tot ? (int)(100 * stats[ST_F_HRSP_1XX].u.u64 / tot) : 0,
1047 U2H(stats[ST_F_HRSP_2XX].u.u64), tot ? (int)(100 * stats[ST_F_HRSP_2XX].u.u64 / tot) : 0,
1048 U2H(stats[ST_F_HRSP_3XX].u.u64), tot ? (int)(100 * stats[ST_F_HRSP_3XX].u.u64 / tot) : 0,
1049 U2H(stats[ST_F_HRSP_4XX].u.u64), tot ? (int)(100 * stats[ST_F_HRSP_4XX].u.u64 / tot) : 0,
1050 U2H(stats[ST_F_HRSP_5XX].u.u64), tot ? (int)(100 * stats[ST_F_HRSP_5XX].u.u64 / tot) : 0,
Willy Tarreau1b0f85e2018-05-28 15:12:40 +02001051 U2H(stats[ST_F_HRSP_OTHER].u.u64), tot ? (int)(100 * stats[ST_F_HRSP_OTHER].u.u64 / tot) : 0,
Christopher Faulet0159ee42019-12-16 14:40:39 +01001052 U2H(stats[ST_F_WREW].u.u64),
1053 U2H(stats[ST_F_EINT].u.u64));
William Lallemand74c24fb2016-11-21 17:18:36 +01001054 }
1055
Christopher Faulet0d1c2a62019-11-08 14:59:51 +01001056 chunk_appendf(out, "<tr><th colspan=3>Max / Avg over last 1024 success. conn.</th></tr>");
1057 chunk_appendf(out, "<tr><th>- Queue time:</th><td>%s / %s</td><td>ms</td></tr>",
1058 U2H(stats[ST_F_QT_MAX].u.u32), U2H(stats[ST_F_QTIME].u.u32));
1059 chunk_appendf(out, "<tr><th>- Connect time:</th><td>%s / %s</td><td>ms</td></tr>",
1060 U2H(stats[ST_F_CT_MAX].u.u32), U2H(stats[ST_F_CTIME].u.u32));
William Lallemand74c24fb2016-11-21 17:18:36 +01001061 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
Christopher Faulet0d1c2a62019-11-08 14:59:51 +01001062 chunk_appendf(out, "<tr><th>- Responses time:</th><td>%s / %s</td><td>ms</td></tr>",
1063 U2H(stats[ST_F_RT_MAX].u.u32), U2H(stats[ST_F_RTIME].u.u32));
1064 chunk_appendf(out, "<tr><th>- Total time:</th><td>%s / %s</td><td>ms</td></tr>",
1065 U2H(stats[ST_F_TT_MAX].u.u32), U2H(stats[ST_F_TTIME].u.u32));
William Lallemand74c24fb2016-11-21 17:18:36 +01001066
1067 chunk_appendf(out,
1068 "</table></div></u></td>"
1069 /* sessions: lbtot, last */
1070 "<td>%s</td><td>%s</td>",
1071 U2H(stats[ST_F_LBTOT].u.u64),
1072 human_time(stats[ST_F_LASTSESS].u.s32, 1));
1073
1074 chunk_appendf(out,
1075 /* bytes : in, out */
1076 "<td>%s</td><td>%s</td>"
1077 /* denied: req, resp */
1078 "<td></td><td>%s</td>"
1079 /* errors : request, connect */
1080 "<td></td><td>%s</td>"
1081 /* errors : response */
1082 "<td><u>%s<div class=tips>Connection resets during transfers: %lld client, %lld server</div></u></td>"
1083 /* warnings: retries, redispatches */
1084 "<td>%lld</td><td>%lld</td>"
1085 "",
1086 U2H(stats[ST_F_BIN].u.u64), U2H(stats[ST_F_BOUT].u.u64),
1087 U2H(stats[ST_F_DRESP].u.u64),
1088 U2H(stats[ST_F_ECON].u.u64),
1089 U2H(stats[ST_F_ERESP].u.u64),
1090 (long long)stats[ST_F_CLI_ABRT].u.u64,
1091 (long long)stats[ST_F_SRV_ABRT].u.u64,
1092 (long long)stats[ST_F_WRETR].u.u64,
1093 (long long)stats[ST_F_WREDIS].u.u64);
1094
1095 /* status, last change */
1096 chunk_appendf(out, "<td class=ac>");
1097
1098 /* FIXME!!!!
1099 * LASTCHG should contain the last change for *this* server and must be computed
1100 * properly above, as was done below, ie: this server if maint, otherwise ref server
1101 * if tracking. Note that ref is either local or remote depending on tracking.
1102 */
1103
1104
1105 if (memcmp(field_str(stats, ST_F_STATUS), "MAINT", 5) == 0) {
1106 chunk_appendf(out, "%s MAINT", human_time(stats[ST_F_LASTCHG].u.u32, 1));
1107 }
1108 else if (memcmp(field_str(stats, ST_F_STATUS), "no check", 5) == 0) {
1109 chunk_strcat(out, "<i>no check</i>");
1110 }
1111 else {
1112 chunk_appendf(out, "%s %s", human_time(stats[ST_F_LASTCHG].u.u32, 1), field_str(stats, ST_F_STATUS));
1113 if (memcmp(field_str(stats, ST_F_STATUS), "DOWN", 4) == 0) {
1114 if (stats[ST_F_CHECK_HEALTH].u.u32)
1115 chunk_strcat(out, " &uarr;");
1116 }
1117 else if (stats[ST_F_CHECK_HEALTH].u.u32 < stats[ST_F_CHECK_RISE].u.u32 + stats[ST_F_CHECK_FALL].u.u32 - 1)
1118 chunk_strcat(out, " &darr;");
1119 }
1120
1121 if (memcmp(field_str(stats, ST_F_STATUS), "DOWN", 4) == 0 &&
1122 stats[ST_F_AGENT_STATUS].type && !stats[ST_F_AGENT_HEALTH].u.u32) {
1123 chunk_appendf(out,
1124 "</td><td class=ac><u> %s",
1125 field_str(stats, ST_F_AGENT_STATUS));
1126
1127 if (stats[ST_F_AGENT_CODE].type)
1128 chunk_appendf(out, "/%d", stats[ST_F_AGENT_CODE].u.u32);
1129
1130 if (stats[ST_F_AGENT_DURATION].type)
1131 chunk_appendf(out, " in %lums", (long)stats[ST_F_AGENT_DURATION].u.u64);
1132
1133 chunk_appendf(out, "<div class=tips>%s", field_str(stats, ST_F_AGENT_DESC));
1134
1135 if (*field_str(stats, ST_F_LAST_AGT)) {
1136 chunk_appendf(out, ": ");
1137 chunk_initstr(&src, field_str(stats, ST_F_LAST_AGT));
1138 chunk_htmlencode(out, &src);
1139 }
1140 chunk_appendf(out, "</div></u>");
1141 }
1142 else if (stats[ST_F_CHECK_STATUS].type) {
1143 chunk_appendf(out,
1144 "</td><td class=ac><u> %s",
1145 field_str(stats, ST_F_CHECK_STATUS));
1146
1147 if (stats[ST_F_CHECK_CODE].type)
1148 chunk_appendf(out, "/%d", stats[ST_F_CHECK_CODE].u.u32);
1149
1150 if (stats[ST_F_CHECK_DURATION].type)
1151 chunk_appendf(out, " in %lums", (long)stats[ST_F_CHECK_DURATION].u.u64);
1152
1153 chunk_appendf(out, "<div class=tips>%s", field_str(stats, ST_F_CHECK_DESC));
1154
1155 if (*field_str(stats, ST_F_LAST_CHK)) {
1156 chunk_appendf(out, ": ");
1157 chunk_initstr(&src, field_str(stats, ST_F_LAST_CHK));
1158 chunk_htmlencode(out, &src);
1159 }
1160 chunk_appendf(out, "</div></u>");
1161 }
1162 else
1163 chunk_appendf(out, "</td><td>");
1164
1165 chunk_appendf(out,
1166 /* weight */
1167 "</td><td class=ac>%d</td>"
1168 /* act, bck */
1169 "<td class=ac>%s</td><td class=ac>%s</td>"
1170 "",
1171 stats[ST_F_WEIGHT].u.u32,
1172 stats[ST_F_BCK].u.u32 ? "-" : "Y",
1173 stats[ST_F_BCK].u.u32 ? "Y" : "-");
1174
1175 /* check failures: unique, fatal, down time */
1176 if (strcmp(field_str(stats, ST_F_STATUS), "MAINT (resolution)") == 0) {
1177 chunk_appendf(out, "<td class=ac colspan=3>resolution</td>");
1178 }
1179 else if (stats[ST_F_CHKFAIL].type) {
1180 chunk_appendf(out, "<td><u>%lld", (long long)stats[ST_F_CHKFAIL].u.u64);
1181
1182 if (stats[ST_F_HANAFAIL].type)
1183 chunk_appendf(out, "/%lld", (long long)stats[ST_F_HANAFAIL].u.u64);
1184
1185 chunk_appendf(out,
1186 "<div class=tips>Failed Health Checks%s</div></u></td>"
1187 "<td>%lld</td><td>%s</td>"
1188 "",
1189 stats[ST_F_HANAFAIL].type ? "/Health Analyses" : "",
1190 (long long)stats[ST_F_CHKDOWN].u.u64, human_time(stats[ST_F_DOWNTIME].u.u32, 1));
1191 }
1192 else if (strcmp(field_str(stats, ST_F_STATUS), "MAINT") != 0 && field_format(stats, ST_F_TRACKED) == FF_STR) {
1193 /* tracking a server (hence inherited maint would appear as "MAINT (via...)" */
1194 chunk_appendf(out,
1195 "<td class=ac colspan=3><a class=lfsb href=\"#%s\">via %s</a></td>",
1196 field_str(stats, ST_F_TRACKED), field_str(stats, ST_F_TRACKED));
1197 }
1198 else
1199 chunk_appendf(out, "<td colspan=3></td>");
1200
1201 /* throttle */
1202 if (stats[ST_F_THROTTLE].type)
1203 chunk_appendf(out, "<td class=ac>%d %%</td></tr>\n", stats[ST_F_THROTTLE].u.u32);
1204 else
1205 chunk_appendf(out, "<td class=ac>-</td></tr>\n");
1206 }
1207 else if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_BE) {
1208 chunk_appendf(out, "<tr class=\"backend\">");
Willy Tarreauab02b3f2019-10-09 11:11:46 +02001209 if (flags & STAT_ADMIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001210 /* Column sub-heading for Enable or Disable server */
1211 chunk_appendf(out, "<td></td>");
1212 }
1213 chunk_appendf(out,
1214 "<td class=ac>"
1215 /* name */
1216 "%s<a name=\"%s/Backend\"></a>"
1217 "<a class=lfsb href=\"#%s/Backend\">Backend</a>"
1218 "",
Willy Tarreau708c4162019-10-09 10:19:16 +02001219 (flags & STAT_SHLGNDS)?"<u>":"",
William Lallemand74c24fb2016-11-21 17:18:36 +01001220 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_PXNAME));
1221
Willy Tarreau708c4162019-10-09 10:19:16 +02001222 if (flags & STAT_SHLGNDS) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001223 /* balancing */
1224 chunk_appendf(out, "<div class=tips>balancing: %s",
1225 field_str(stats, ST_F_ALGO));
1226
1227 /* cookie */
1228 if (stats[ST_F_COOKIE].type) {
1229 chunk_appendf(out, ", cookie: '");
1230 chunk_initstr(&src, field_str(stats, ST_F_COOKIE));
1231 chunk_htmlencode(out, &src);
1232 chunk_appendf(out, "'");
1233 }
1234 chunk_appendf(out, "</div>");
1235 }
1236
1237 chunk_appendf(out,
1238 "%s</td>"
1239 /* queue : current, max */
1240 "<td>%s</td><td>%s</td><td></td>"
1241 /* sessions rate : current, max, limit */
1242 "<td>%s</td><td>%s</td><td></td>"
1243 "",
Willy Tarreau708c4162019-10-09 10:19:16 +02001244 (flags & STAT_SHLGNDS)?"</u>":"",
William Lallemand74c24fb2016-11-21 17:18:36 +01001245 U2H(stats[ST_F_QCUR].u.u32), U2H(stats[ST_F_QMAX].u.u32),
1246 U2H(stats[ST_F_RATE].u.u32), U2H(stats[ST_F_RATE_MAX].u.u32));
1247
1248 chunk_appendf(out,
1249 /* sessions: current, max, limit, total */
1250 "<td>%s</td><td>%s</td><td>%s</td>"
1251 "<td><u>%s<div class=tips><table class=det>"
1252 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
1253 "",
Willy Tarreau8e0f1752016-12-12 15:07:29 +01001254 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 +01001255 U2H(stats[ST_F_STOT].u.u64),
1256 U2H(stats[ST_F_STOT].u.u64));
1257
1258 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
1259 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0) {
1260 chunk_appendf(out,
Willy Tarreauf1573842018-12-14 11:35:36 +01001261 "<tr><th>New connections:</th><td>%s</td></tr>"
1262 "<tr><th>Reused connections:</th><td>%s</td><td>(%d%%)</td></tr>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001263 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
1264 "<tr><th>- HTTP 1xx responses:</th><td>%s</td></tr>"
1265 "<tr><th>- HTTP 2xx responses:</th><td>%s</td></tr>"
1266 "<tr><th>&nbsp;&nbsp;Compressed 2xx:</th><td>%s</td><td>(%d%%)</td></tr>"
1267 "<tr><th>- HTTP 3xx responses:</th><td>%s</td></tr>"
1268 "<tr><th>- HTTP 4xx responses:</th><td>%s</td></tr>"
1269 "<tr><th>- HTTP 5xx responses:</th><td>%s</td></tr>"
1270 "<tr><th>- other responses:</th><td>%s</td></tr>"
Willy Tarreaua1214a52018-12-14 14:00:25 +01001271 "<tr><th>Cache lookups:</th><td>%s</td></tr>"
1272 "<tr><th>Cache hits:</th><td>%s</td><td>(%d%%)</td></tr>"
Willy Tarreau1b0f85e2018-05-28 15:12:40 +02001273 "<tr><th>Failed hdr rewrites:</th><td>%s</td></tr>"
Christopher Faulet0159ee42019-12-16 14:40:39 +01001274 "<tr><th>Internal errors:</th><td>%s</td></tr>"
Christopher Faulet0d1c2a62019-11-08 14:59:51 +01001275 "",
Willy Tarreauf1573842018-12-14 11:35:36 +01001276 U2H(stats[ST_F_CONNECT].u.u64),
1277 U2H(stats[ST_F_REUSE].u.u64),
1278 (stats[ST_F_CONNECT].u.u64 + stats[ST_F_REUSE].u.u64) ?
1279 (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 +01001280 U2H(stats[ST_F_REQ_TOT].u.u64),
1281 U2H(stats[ST_F_HRSP_1XX].u.u64),
1282 U2H(stats[ST_F_HRSP_2XX].u.u64),
1283 U2H(stats[ST_F_COMP_RSP].u.u64),
1284 stats[ST_F_HRSP_2XX].u.u64 ?
1285 (int)(100 * stats[ST_F_COMP_RSP].u.u64 / stats[ST_F_HRSP_2XX].u.u64) : 0,
1286 U2H(stats[ST_F_HRSP_3XX].u.u64),
1287 U2H(stats[ST_F_HRSP_4XX].u.u64),
1288 U2H(stats[ST_F_HRSP_5XX].u.u64),
Willy Tarreaufeead3a2018-12-14 13:48:44 +01001289 U2H(stats[ST_F_HRSP_OTHER].u.u64),
Willy Tarreaua1214a52018-12-14 14:00:25 +01001290 U2H(stats[ST_F_CACHE_LOOKUPS].u.u64),
1291 U2H(stats[ST_F_CACHE_HITS].u.u64),
1292 stats[ST_F_CACHE_LOOKUPS].u.u64 ?
1293 (int)(100 * stats[ST_F_CACHE_HITS].u.u64 / stats[ST_F_CACHE_LOOKUPS].u.u64) : 0,
Christopher Faulet0159ee42019-12-16 14:40:39 +01001294 U2H(stats[ST_F_WREW].u.u64),
1295 U2H(stats[ST_F_EINT].u.u64));
William Lallemand74c24fb2016-11-21 17:18:36 +01001296 }
1297
Christopher Faulet0d1c2a62019-11-08 14:59:51 +01001298 chunk_appendf(out, "<tr><th colspan=3>Max / Avg over last 1024 success. conn.</th></tr>");
1299 chunk_appendf(out, "<tr><th>- Queue time:</th><td>%s / %s</td><td>ms</td></tr>",
1300 U2H(stats[ST_F_QT_MAX].u.u32), U2H(stats[ST_F_QTIME].u.u32));
1301 chunk_appendf(out, "<tr><th>- Connect time:</th><td>%s / %s</td><td>ms</td></tr>",
1302 U2H(stats[ST_F_CT_MAX].u.u32), U2H(stats[ST_F_CTIME].u.u32));
William Lallemand74c24fb2016-11-21 17:18:36 +01001303 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
Christopher Faulet0d1c2a62019-11-08 14:59:51 +01001304 chunk_appendf(out, "<tr><th>- Responses time:</th><td>%s / %s</td><td>ms</td></tr>",
1305 U2H(stats[ST_F_RT_MAX].u.u32), U2H(stats[ST_F_RTIME].u.u32));
1306 chunk_appendf(out, "<tr><th>- Total time:</th><td>%s / %s</td><td>ms</td></tr>",
1307 U2H(stats[ST_F_TT_MAX].u.u32), U2H(stats[ST_F_TTIME].u.u32));
William Lallemand74c24fb2016-11-21 17:18:36 +01001308
1309 chunk_appendf(out,
1310 "</table></div></u></td>"
1311 /* sessions: lbtot, last */
1312 "<td>%s</td><td>%s</td>"
1313 /* bytes: in */
1314 "<td>%s</td>"
1315 "",
1316 U2H(stats[ST_F_LBTOT].u.u64),
1317 human_time(stats[ST_F_LASTSESS].u.s32, 1),
1318 U2H(stats[ST_F_BIN].u.u64));
1319
1320 chunk_appendf(out,
1321 /* bytes:out + compression stats (via hover): comp_in, comp_out, comp_byp */
1322 "<td>%s%s<div class=tips><table class=det>"
1323 "<tr><th>Response bytes in:</th><td>%s</td></tr>"
1324 "<tr><th>Compression in:</th><td>%s</td></tr>"
1325 "<tr><th>Compression out:</th><td>%s</td><td>(%d%%)</td></tr>"
1326 "<tr><th>Compression bypass:</th><td>%s</td></tr>"
1327 "<tr><th>Total bytes saved:</th><td>%s</td><td>(%d%%)</td></tr>"
1328 "</table></div>%s</td>",
1329 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "<u>":"",
1330 U2H(stats[ST_F_BOUT].u.u64),
1331 U2H(stats[ST_F_BOUT].u.u64),
1332 U2H(stats[ST_F_COMP_IN].u.u64),
1333 U2H(stats[ST_F_COMP_OUT].u.u64),
1334 stats[ST_F_COMP_IN].u.u64 ? (int)(stats[ST_F_COMP_OUT].u.u64 * 100 / stats[ST_F_COMP_IN].u.u64) : 0,
1335 U2H(stats[ST_F_COMP_BYP].u.u64),
1336 U2H(stats[ST_F_COMP_IN].u.u64 - stats[ST_F_COMP_OUT].u.u64),
1337 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,
1338 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "</u>":"");
1339
1340 chunk_appendf(out,
1341 /* denied: req, resp */
1342 "<td>%s</td><td>%s</td>"
1343 /* errors : request, connect */
1344 "<td></td><td>%s</td>"
1345 /* errors : response */
1346 "<td><u>%s<div class=tips>Connection resets during transfers: %lld client, %lld server</div></u></td>"
1347 /* warnings: retries, redispatches */
1348 "<td>%lld</td><td>%lld</td>"
1349 /* backend status: reflect backend status (up/down): we display UP
1350 * if the backend has known working servers or if it has no server at
1351 * all (eg: for stats). Then we display the total weight, number of
1352 * active and backups. */
1353 "<td class=ac>%s %s</td><td class=ac>&nbsp;</td><td class=ac>%d</td>"
1354 "<td class=ac>%d</td><td class=ac>%d</td>"
1355 "",
1356 U2H(stats[ST_F_DREQ].u.u64), U2H(stats[ST_F_DRESP].u.u64),
1357 U2H(stats[ST_F_ECON].u.u64),
1358 U2H(stats[ST_F_ERESP].u.u64),
1359 (long long)stats[ST_F_CLI_ABRT].u.u64,
1360 (long long)stats[ST_F_SRV_ABRT].u.u64,
1361 (long long)stats[ST_F_WRETR].u.u64, (long long)stats[ST_F_WREDIS].u.u64,
1362 human_time(stats[ST_F_LASTCHG].u.u32, 1),
1363 strcmp(field_str(stats, ST_F_STATUS), "DOWN") ? field_str(stats, ST_F_STATUS) : "<font color=\"red\"><b>DOWN</b></font>",
1364 stats[ST_F_WEIGHT].u.u32,
1365 stats[ST_F_ACT].u.u32, stats[ST_F_BCK].u.u32);
1366
1367 chunk_appendf(out,
1368 /* rest of backend: nothing, down transitions, total downtime, throttle */
1369 "<td class=ac>&nbsp;</td><td>%d</td>"
1370 "<td>%s</td>"
1371 "<td></td>"
1372 "</tr>",
1373 stats[ST_F_CHKDOWN].u.u32,
1374 stats[ST_F_DOWNTIME].type ? human_time(stats[ST_F_DOWNTIME].u.u32, 1) : "&nbsp;");
1375 }
1376 return 1;
1377}
1378
Willy Tarreau43241ff2019-10-09 11:27:51 +02001379static int stats_dump_one_line(const struct field *stats, struct proxy *px, struct appctx *appctx)
William Lallemand74c24fb2016-11-21 17:18:36 +01001380{
Simon Horman05ee2132017-01-04 09:37:25 +01001381 int ret;
1382
William Lallemand74c24fb2016-11-21 17:18:36 +01001383 if (appctx->ctx.stats.flags & STAT_FMT_HTML)
Willy Tarreau43241ff2019-10-09 11:27:51 +02001384 ret = stats_dump_fields_html(&trash, stats, appctx->ctx.stats.flags);
William Lallemand74c24fb2016-11-21 17:18:36 +01001385 else if (appctx->ctx.stats.flags & STAT_FMT_TYPED)
Willy Tarreau43241ff2019-10-09 11:27:51 +02001386 ret = stats_dump_fields_typed(&trash, stats, appctx->ctx.stats.flags);
Simon Horman05ee2132017-01-04 09:37:25 +01001387 else if (appctx->ctx.stats.flags & STAT_FMT_JSON)
Willy Tarreaub0ce3ad2019-10-09 11:19:29 +02001388 ret = stats_dump_fields_json(&trash, stats, appctx->ctx.stats.flags);
William Lallemand74c24fb2016-11-21 17:18:36 +01001389 else
Willy Tarreau43241ff2019-10-09 11:27:51 +02001390 ret = stats_dump_fields_csv(&trash, stats, appctx->ctx.stats.flags);
Simon Horman05ee2132017-01-04 09:37:25 +01001391
1392 if (ret)
1393 appctx->ctx.stats.flags |= STAT_STARTED;
1394
1395 return ret;
William Lallemand74c24fb2016-11-21 17:18:36 +01001396}
1397
1398/* Fill <stats> with the frontend statistics. <stats> is
1399 * preallocated array of length <len>. The length of the array
1400 * must be at least ST_F_TOTAL_FIELDS. If this length is less then
1401 * this value, the function returns 0, otherwise, it returns 1.
1402 */
1403int stats_fill_fe_stats(struct proxy *px, struct field *stats, int len)
1404{
1405 if (len < ST_F_TOTAL_FIELDS)
1406 return 0;
1407
1408 memset(stats, 0, sizeof(*stats) * len);
1409
1410 stats[ST_F_PXNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
1411 stats[ST_F_SVNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, "FRONTEND");
1412 stats[ST_F_MODE] = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
1413 stats[ST_F_SCUR] = mkf_u32(0, px->feconn);
1414 stats[ST_F_SMAX] = mkf_u32(FN_MAX, px->fe_counters.conn_max);
1415 stats[ST_F_SLIM] = mkf_u32(FO_CONFIG|FN_LIMIT, px->maxconn);
1416 stats[ST_F_STOT] = mkf_u64(FN_COUNTER, px->fe_counters.cum_sess);
1417 stats[ST_F_BIN] = mkf_u64(FN_COUNTER, px->fe_counters.bytes_in);
1418 stats[ST_F_BOUT] = mkf_u64(FN_COUNTER, px->fe_counters.bytes_out);
1419 stats[ST_F_DREQ] = mkf_u64(FN_COUNTER, px->fe_counters.denied_req);
1420 stats[ST_F_DRESP] = mkf_u64(FN_COUNTER, px->fe_counters.denied_resp);
1421 stats[ST_F_EREQ] = mkf_u64(FN_COUNTER, px->fe_counters.failed_req);
1422 stats[ST_F_DCON] = mkf_u64(FN_COUNTER, px->fe_counters.denied_conn);
1423 stats[ST_F_DSES] = mkf_u64(FN_COUNTER, px->fe_counters.denied_sess);
1424 stats[ST_F_STATUS] = mkf_str(FO_STATUS, px->state == PR_STREADY ? "OPEN" : px->state == PR_STFULL ? "FULL" : "STOP");
1425 stats[ST_F_PID] = mkf_u32(FO_KEY, relative_pid);
1426 stats[ST_F_IID] = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
1427 stats[ST_F_SID] = mkf_u32(FO_KEY|FS_SERVICE, 0);
1428 stats[ST_F_TYPE] = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_FE);
1429 stats[ST_F_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&px->fe_sess_per_sec));
1430 stats[ST_F_RATE_LIM] = mkf_u32(FO_CONFIG|FN_LIMIT, px->fe_sps_lim);
1431 stats[ST_F_RATE_MAX] = mkf_u32(FN_MAX, px->fe_counters.sps_max);
Tim Duesterhus3fd19732018-05-27 20:35:08 +02001432 stats[ST_F_WREW] = mkf_u64(FN_COUNTER, px->fe_counters.failed_rewrites);
Christopher Faulet0159ee42019-12-16 14:40:39 +01001433 stats[ST_F_EINT] = mkf_u64(FN_COUNTER, px->fe_counters.internal_errors);
William Lallemand74c24fb2016-11-21 17:18:36 +01001434
1435 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
1436 if (px->mode == PR_MODE_HTTP) {
1437 stats[ST_F_HRSP_1XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[1]);
1438 stats[ST_F_HRSP_2XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[2]);
1439 stats[ST_F_HRSP_3XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[3]);
1440 stats[ST_F_HRSP_4XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[4]);
1441 stats[ST_F_HRSP_5XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[5]);
1442 stats[ST_F_HRSP_OTHER] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[0]);
1443 stats[ST_F_INTERCEPTED] = mkf_u64(FN_COUNTER, px->fe_counters.intercepted_req);
Willy Tarreaua1214a52018-12-14 14:00:25 +01001444 stats[ST_F_CACHE_LOOKUPS] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.cache_lookups);
1445 stats[ST_F_CACHE_HITS] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.cache_hits);
William Lallemand74c24fb2016-11-21 17:18:36 +01001446 }
1447
1448 /* requests : req_rate, req_rate_max, req_tot, */
1449 stats[ST_F_REQ_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&px->fe_req_per_sec));
1450 stats[ST_F_REQ_RATE_MAX] = mkf_u32(FN_MAX, px->fe_counters.p.http.rps_max);
1451 stats[ST_F_REQ_TOT] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.cum_req);
1452
1453 /* compression: in, out, bypassed, responses */
1454 stats[ST_F_COMP_IN] = mkf_u64(FN_COUNTER, px->fe_counters.comp_in);
1455 stats[ST_F_COMP_OUT] = mkf_u64(FN_COUNTER, px->fe_counters.comp_out);
1456 stats[ST_F_COMP_BYP] = mkf_u64(FN_COUNTER, px->fe_counters.comp_byp);
1457 stats[ST_F_COMP_RSP] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.comp_rsp);
1458
1459 /* connections : conn_rate, conn_rate_max, conn_tot, conn_max */
1460 stats[ST_F_CONN_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&px->fe_conn_per_sec));
1461 stats[ST_F_CONN_RATE_MAX] = mkf_u32(FN_MAX, px->fe_counters.cps_max);
1462 stats[ST_F_CONN_TOT] = mkf_u64(FN_COUNTER, px->fe_counters.cum_conn);
1463
1464 return 1;
1465}
1466
1467/* Dumps a frontend's line to the trash for the current proxy <px> and uses
1468 * the state from stream interface <si>. The caller is responsible for clearing
1469 * the trash if needed. Returns non-zero if it emits anything, zero otherwise.
1470 */
1471static int stats_dump_fe_stats(struct stream_interface *si, struct proxy *px)
1472{
1473 struct appctx *appctx = __objt_appctx(si->end);
1474
1475 if (!(px->cap & PR_CAP_FE))
1476 return 0;
1477
1478 if ((appctx->ctx.stats.flags & STAT_BOUND) && !(appctx->ctx.stats.type & (1 << STATS_TYPE_FE)))
1479 return 0;
1480
1481 if (!stats_fill_fe_stats(px, stats, ST_F_TOTAL_FIELDS))
1482 return 0;
1483
Willy Tarreau43241ff2019-10-09 11:27:51 +02001484 return stats_dump_one_line(stats, px, appctx);
William Lallemand74c24fb2016-11-21 17:18:36 +01001485}
1486
1487/* Fill <stats> with the listener statistics. <stats> is
1488 * preallocated array of length <len>. The length of the array
1489 * must be at least ST_F_TOTAL_FIELDS. If this length is less
1490 * then this value, the function returns 0, otherwise, it
Willy Tarreau708c4162019-10-09 10:19:16 +02001491 * returns 1. <flags> can take the value STAT_SHLGNDS.
William Lallemand74c24fb2016-11-21 17:18:36 +01001492 */
1493int stats_fill_li_stats(struct proxy *px, struct listener *l, int flags,
1494 struct field *stats, int len)
1495{
Willy Tarreau83061a82018-07-13 11:56:34 +02001496 struct buffer *out = get_trash_chunk();
William Lallemand74c24fb2016-11-21 17:18:36 +01001497
1498 if (len < ST_F_TOTAL_FIELDS)
1499 return 0;
1500
1501 if (!l->counters)
1502 return 0;
1503
1504 chunk_reset(out);
1505 memset(stats, 0, sizeof(*stats) * len);
1506
1507 stats[ST_F_PXNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
1508 stats[ST_F_SVNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, l->name);
1509 stats[ST_F_MODE] = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
1510 stats[ST_F_SCUR] = mkf_u32(0, l->nbconn);
1511 stats[ST_F_SMAX] = mkf_u32(FN_MAX, l->counters->conn_max);
1512 stats[ST_F_SLIM] = mkf_u32(FO_CONFIG|FN_LIMIT, l->maxconn);
1513 stats[ST_F_STOT] = mkf_u64(FN_COUNTER, l->counters->cum_conn);
1514 stats[ST_F_BIN] = mkf_u64(FN_COUNTER, l->counters->bytes_in);
1515 stats[ST_F_BOUT] = mkf_u64(FN_COUNTER, l->counters->bytes_out);
1516 stats[ST_F_DREQ] = mkf_u64(FN_COUNTER, l->counters->denied_req);
1517 stats[ST_F_DRESP] = mkf_u64(FN_COUNTER, l->counters->denied_resp);
1518 stats[ST_F_EREQ] = mkf_u64(FN_COUNTER, l->counters->failed_req);
1519 stats[ST_F_DCON] = mkf_u64(FN_COUNTER, l->counters->denied_conn);
1520 stats[ST_F_DSES] = mkf_u64(FN_COUNTER, l->counters->denied_sess);
Willy Tarreaua8cf66b2019-02-27 16:49:00 +01001521 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 +01001522 stats[ST_F_PID] = mkf_u32(FO_KEY, relative_pid);
1523 stats[ST_F_IID] = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
1524 stats[ST_F_SID] = mkf_u32(FO_KEY|FS_SERVICE, l->luid);
1525 stats[ST_F_TYPE] = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_SO);
Tim Duesterhus3fd19732018-05-27 20:35:08 +02001526 stats[ST_F_WREW] = mkf_u64(FN_COUNTER, l->counters->failed_rewrites);
Christopher Faulet0159ee42019-12-16 14:40:39 +01001527 stats[ST_F_EINT] = mkf_u64(FN_COUNTER, l->counters->internal_errors);
William Lallemand74c24fb2016-11-21 17:18:36 +01001528
Willy Tarreau708c4162019-10-09 10:19:16 +02001529 if (flags & STAT_SHLGNDS) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001530 char str[INET6_ADDRSTRLEN];
1531 int port;
1532
1533 port = get_host_port(&l->addr);
1534 switch (addr_to_str(&l->addr, str, sizeof(str))) {
1535 case AF_INET:
1536 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
1537 chunk_appendf(out, "%s:%d", str, port);
1538 break;
1539 case AF_INET6:
1540 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
1541 chunk_appendf(out, "[%s]:%d", str, port);
1542 break;
1543 case AF_UNIX:
1544 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, "unix");
1545 break;
1546 case -1:
1547 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
1548 chunk_strcat(out, strerror(errno));
1549 break;
1550 default: /* address family not supported */
1551 break;
1552 }
1553 }
1554
1555 return 1;
1556}
1557
1558/* Dumps a line for listener <l> and proxy <px> to the trash and uses the state
Willy Tarreau43241ff2019-10-09 11:27:51 +02001559 * from stream interface <si>. The caller is responsible for clearing the trash
1560 * if needed. Returns non-zero if it emits anything, zero otherwise.
William Lallemand74c24fb2016-11-21 17:18:36 +01001561 */
Willy Tarreau43241ff2019-10-09 11:27:51 +02001562static int stats_dump_li_stats(struct stream_interface *si, struct proxy *px, struct listener *l)
William Lallemand74c24fb2016-11-21 17:18:36 +01001563{
1564 struct appctx *appctx = __objt_appctx(si->end);
1565
Willy Tarreau43241ff2019-10-09 11:27:51 +02001566 if (!stats_fill_li_stats(px, l, appctx->ctx.stats.flags, stats, ST_F_TOTAL_FIELDS))
William Lallemand74c24fb2016-11-21 17:18:36 +01001567 return 0;
1568
Willy Tarreau43241ff2019-10-09 11:27:51 +02001569 return stats_dump_one_line(stats, px, appctx);
William Lallemand74c24fb2016-11-21 17:18:36 +01001570}
1571
1572enum srv_stats_state {
1573 SRV_STATS_STATE_DOWN = 0,
1574 SRV_STATS_STATE_DOWN_AGENT,
1575 SRV_STATS_STATE_GOING_UP,
1576 SRV_STATS_STATE_UP_GOING_DOWN,
1577 SRV_STATS_STATE_UP,
1578 SRV_STATS_STATE_NOLB_GOING_DOWN,
1579 SRV_STATS_STATE_NOLB,
1580 SRV_STATS_STATE_DRAIN_GOING_DOWN,
1581 SRV_STATS_STATE_DRAIN,
1582 SRV_STATS_STATE_DRAIN_AGENT,
1583 SRV_STATS_STATE_NO_CHECK,
1584
1585 SRV_STATS_STATE_COUNT, /* Must be last */
1586};
1587
1588static const char *srv_hlt_st[SRV_STATS_STATE_COUNT] = {
1589 [SRV_STATS_STATE_DOWN] = "DOWN",
1590 [SRV_STATS_STATE_DOWN_AGENT] = "DOWN (agent)",
1591 [SRV_STATS_STATE_GOING_UP] = "DOWN %d/%d",
1592 [SRV_STATS_STATE_UP_GOING_DOWN] = "UP %d/%d",
1593 [SRV_STATS_STATE_UP] = "UP",
1594 [SRV_STATS_STATE_NOLB_GOING_DOWN] = "NOLB %d/%d",
1595 [SRV_STATS_STATE_NOLB] = "NOLB",
1596 [SRV_STATS_STATE_DRAIN_GOING_DOWN] = "DRAIN %d/%d",
1597 [SRV_STATS_STATE_DRAIN] = "DRAIN",
1598 [SRV_STATS_STATE_DRAIN_AGENT] = "DRAIN (agent)",
1599 [SRV_STATS_STATE_NO_CHECK] = "no check"
1600};
1601
1602/* Fill <stats> with the server statistics. <stats> is
1603 * preallocated array of length <len>. The length of the array
1604 * must be at least ST_F_TOTAL_FIELDS. If this length is less
1605 * then this value, the function returns 0, otherwise, it
Willy Tarreau708c4162019-10-09 10:19:16 +02001606 * returns 1. <flags> can take the value STAT_SHLGNDS.
William Lallemand74c24fb2016-11-21 17:18:36 +01001607 */
1608int stats_fill_sv_stats(struct proxy *px, struct server *sv, int flags,
1609 struct field *stats, int len)
1610{
1611 struct server *via, *ref;
1612 char str[INET6_ADDRSTRLEN];
Willy Tarreau83061a82018-07-13 11:56:34 +02001613 struct buffer *out = get_trash_chunk();
William Lallemand74c24fb2016-11-21 17:18:36 +01001614 enum srv_stats_state state;
1615 char *fld_status;
1616
1617 if (len < ST_F_TOTAL_FIELDS)
1618 return 0;
1619
1620 memset(stats, 0, sizeof(*stats) * len);
1621
1622 /* we have "via" which is the tracked server as described in the configuration,
1623 * and "ref" which is the checked server and the end of the chain.
1624 */
1625 via = sv->track ? sv->track : sv;
1626 ref = via;
1627 while (ref->track)
1628 ref = ref->track;
1629
Emeric Brun52a91d32017-08-31 14:41:55 +02001630 if (sv->cur_state == SRV_ST_RUNNING || sv->cur_state == SRV_ST_STARTING) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001631 if ((ref->check.state & CHK_ST_ENABLED) &&
1632 (ref->check.health < ref->check.rise + ref->check.fall - 1)) {
1633 state = SRV_STATS_STATE_UP_GOING_DOWN;
1634 } else {
1635 state = SRV_STATS_STATE_UP;
1636 }
1637
Emeric Brun52a91d32017-08-31 14:41:55 +02001638 if (sv->cur_admin & SRV_ADMF_DRAIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001639 if (ref->agent.state & CHK_ST_ENABLED)
1640 state = SRV_STATS_STATE_DRAIN_AGENT;
1641 else if (state == SRV_STATS_STATE_UP_GOING_DOWN)
1642 state = SRV_STATS_STATE_DRAIN_GOING_DOWN;
1643 else
1644 state = SRV_STATS_STATE_DRAIN;
1645 }
1646
1647 if (state == SRV_STATS_STATE_UP && !(ref->check.state & CHK_ST_ENABLED)) {
1648 state = SRV_STATS_STATE_NO_CHECK;
1649 }
1650 }
Emeric Brun52a91d32017-08-31 14:41:55 +02001651 else if (sv->cur_state == SRV_ST_STOPPING) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001652 if ((!(sv->check.state & CHK_ST_ENABLED) && !sv->track) ||
1653 (ref->check.health == ref->check.rise + ref->check.fall - 1)) {
1654 state = SRV_STATS_STATE_NOLB;
1655 } else {
1656 state = SRV_STATS_STATE_NOLB_GOING_DOWN;
1657 }
1658 }
1659 else { /* stopped */
1660 if ((ref->agent.state & CHK_ST_ENABLED) && !ref->agent.health) {
1661 state = SRV_STATS_STATE_DOWN_AGENT;
1662 } else if ((ref->check.state & CHK_ST_ENABLED) && !ref->check.health) {
1663 state = SRV_STATS_STATE_DOWN; /* DOWN */
1664 } else if ((ref->agent.state & CHK_ST_ENABLED) || (ref->check.state & CHK_ST_ENABLED)) {
1665 state = SRV_STATS_STATE_GOING_UP;
1666 } else {
1667 state = SRV_STATS_STATE_DOWN; /* DOWN, unchecked */
1668 }
1669 }
1670
1671 chunk_reset(out);
1672
1673 stats[ST_F_PXNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
1674 stats[ST_F_SVNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, sv->id);
1675 stats[ST_F_MODE] = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
1676 stats[ST_F_QCUR] = mkf_u32(0, sv->nbpend);
1677 stats[ST_F_QMAX] = mkf_u32(FN_MAX, sv->counters.nbpend_max);
1678 stats[ST_F_SCUR] = mkf_u32(0, sv->cur_sess);
1679 stats[ST_F_SMAX] = mkf_u32(FN_MAX, sv->counters.cur_sess_max);
1680
1681 if (sv->maxconn)
1682 stats[ST_F_SLIM] = mkf_u32(FO_CONFIG|FN_LIMIT, sv->maxconn);
1683
Willy Tarreauf21d17b2019-09-08 09:24:56 +02001684 stats[ST_F_SRV_ICUR] = mkf_u32(0, sv->curr_idle_conns);
1685 if (sv->max_idle_conns != -1)
1686 stats[ST_F_SRV_ILIM] = mkf_u32(FO_CONFIG|FN_LIMIT, sv->max_idle_conns);
1687
William Lallemand74c24fb2016-11-21 17:18:36 +01001688 stats[ST_F_STOT] = mkf_u64(FN_COUNTER, sv->counters.cum_sess);
1689 stats[ST_F_BIN] = mkf_u64(FN_COUNTER, sv->counters.bytes_in);
1690 stats[ST_F_BOUT] = mkf_u64(FN_COUNTER, sv->counters.bytes_out);
Christopher Fauleta08546b2019-12-16 16:07:34 +01001691 stats[ST_F_DRESP] = mkf_u64(FN_COUNTER, sv->counters.denied_resp);
William Lallemand74c24fb2016-11-21 17:18:36 +01001692 stats[ST_F_ECON] = mkf_u64(FN_COUNTER, sv->counters.failed_conns);
1693 stats[ST_F_ERESP] = mkf_u64(FN_COUNTER, sv->counters.failed_resp);
1694 stats[ST_F_WRETR] = mkf_u64(FN_COUNTER, sv->counters.retries);
1695 stats[ST_F_WREDIS] = mkf_u64(FN_COUNTER, sv->counters.redispatches);
Tim Duesterhus3fd19732018-05-27 20:35:08 +02001696 stats[ST_F_WREW] = mkf_u64(FN_COUNTER, sv->counters.failed_rewrites);
Christopher Faulet0159ee42019-12-16 14:40:39 +01001697 stats[ST_F_EINT] = mkf_u64(FN_COUNTER, sv->counters.internal_errors);
Willy Tarreauf1573842018-12-14 11:35:36 +01001698 stats[ST_F_CONNECT] = mkf_u64(FN_COUNTER, sv->counters.connect);
1699 stats[ST_F_REUSE] = mkf_u64(FN_COUNTER, sv->counters.reuse);
William Lallemand74c24fb2016-11-21 17:18:36 +01001700
1701 /* status */
1702 fld_status = chunk_newstr(out);
Emeric Brun52a91d32017-08-31 14:41:55 +02001703 if (sv->cur_admin & SRV_ADMF_RMAINT)
William Lallemand74c24fb2016-11-21 17:18:36 +01001704 chunk_appendf(out, "MAINT (resolution)");
Emeric Brun52a91d32017-08-31 14:41:55 +02001705 else if (sv->cur_admin & SRV_ADMF_IMAINT)
William Lallemand74c24fb2016-11-21 17:18:36 +01001706 chunk_appendf(out, "MAINT (via %s/%s)", via->proxy->id, via->id);
Emeric Brun52a91d32017-08-31 14:41:55 +02001707 else if (sv->cur_admin & SRV_ADMF_MAINT)
William Lallemand74c24fb2016-11-21 17:18:36 +01001708 chunk_appendf(out, "MAINT");
1709 else
1710 chunk_appendf(out,
1711 srv_hlt_st[state],
Emeric Brun52a91d32017-08-31 14:41:55 +02001712 (ref->cur_state != SRV_ST_STOPPED) ? (ref->check.health - ref->check.rise + 1) : (ref->check.health),
1713 (ref->cur_state != SRV_ST_STOPPED) ? (ref->check.fall) : (ref->check.rise));
William Lallemand74c24fb2016-11-21 17:18:36 +01001714
1715 stats[ST_F_STATUS] = mkf_str(FO_STATUS, fld_status);
1716 stats[ST_F_LASTCHG] = mkf_u32(FN_AGE, now.tv_sec - sv->last_change);
Emeric Brun52a91d32017-08-31 14:41:55 +02001717 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 +01001718 stats[ST_F_ACT] = mkf_u32(FO_STATUS, (sv->flags & SRV_F_BACKUP) ? 0 : 1);
1719 stats[ST_F_BCK] = mkf_u32(FO_STATUS, (sv->flags & SRV_F_BACKUP) ? 1 : 0);
1720
1721 /* check failures: unique, fatal; last change, total downtime */
1722 if (sv->check.state & CHK_ST_ENABLED) {
1723 stats[ST_F_CHKFAIL] = mkf_u64(FN_COUNTER, sv->counters.failed_checks);
1724 stats[ST_F_CHKDOWN] = mkf_u64(FN_COUNTER, sv->counters.down_trans);
1725 stats[ST_F_DOWNTIME] = mkf_u32(FN_COUNTER, srv_downtime(sv));
1726 }
1727
1728 if (sv->maxqueue)
1729 stats[ST_F_QLIMIT] = mkf_u32(FO_CONFIG|FS_SERVICE, sv->maxqueue);
1730
1731 stats[ST_F_PID] = mkf_u32(FO_KEY, relative_pid);
1732 stats[ST_F_IID] = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
1733 stats[ST_F_SID] = mkf_u32(FO_KEY|FS_SERVICE, sv->puid);
1734
Emeric Brun52a91d32017-08-31 14:41:55 +02001735 if (sv->cur_state == SRV_ST_STARTING && !server_is_draining(sv))
William Lallemand74c24fb2016-11-21 17:18:36 +01001736 stats[ST_F_THROTTLE] = mkf_u32(FN_AVG, server_throttle_rate(sv));
1737
1738 stats[ST_F_LBTOT] = mkf_u64(FN_COUNTER, sv->counters.cum_lbconn);
1739
1740 if (sv->track) {
1741 char *fld_track = chunk_newstr(out);
1742
1743 chunk_appendf(out, "%s/%s", sv->track->proxy->id, sv->track->id);
1744 stats[ST_F_TRACKED] = mkf_str(FO_CONFIG|FN_NAME|FS_SERVICE, fld_track);
1745 }
1746
1747 stats[ST_F_TYPE] = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_SV);
1748 stats[ST_F_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&sv->sess_per_sec));
1749 stats[ST_F_RATE_MAX] = mkf_u32(FN_MAX, sv->counters.sps_max);
1750
1751 if ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED) {
1752 const char *fld_chksts;
1753
1754 fld_chksts = chunk_newstr(out);
1755 chunk_strcat(out, "* "); // for check in progress
1756 chunk_strcat(out, get_check_status_info(sv->check.status));
1757 if (!(sv->check.state & CHK_ST_INPROGRESS))
1758 fld_chksts += 2; // skip "* "
1759 stats[ST_F_CHECK_STATUS] = mkf_str(FN_OUTPUT, fld_chksts);
1760
1761 if (sv->check.status >= HCHK_STATUS_L57DATA)
1762 stats[ST_F_CHECK_CODE] = mkf_u32(FN_OUTPUT, sv->check.code);
1763
1764 if (sv->check.status >= HCHK_STATUS_CHECKED)
1765 stats[ST_F_CHECK_DURATION] = mkf_u64(FN_DURATION, sv->check.duration);
1766
1767 stats[ST_F_CHECK_DESC] = mkf_str(FN_OUTPUT, get_check_status_description(sv->check.status));
1768 stats[ST_F_LAST_CHK] = mkf_str(FN_OUTPUT, sv->check.desc);
1769 stats[ST_F_CHECK_RISE] = mkf_u32(FO_CONFIG|FS_SERVICE, ref->check.rise);
1770 stats[ST_F_CHECK_FALL] = mkf_u32(FO_CONFIG|FS_SERVICE, ref->check.fall);
1771 stats[ST_F_CHECK_HEALTH] = mkf_u32(FO_CONFIG|FS_SERVICE, ref->check.health);
1772 }
1773
1774 if ((sv->agent.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED) {
1775 const char *fld_chksts;
1776
1777 fld_chksts = chunk_newstr(out);
1778 chunk_strcat(out, "* "); // for check in progress
1779 chunk_strcat(out, get_check_status_info(sv->agent.status));
1780 if (!(sv->agent.state & CHK_ST_INPROGRESS))
1781 fld_chksts += 2; // skip "* "
1782 stats[ST_F_AGENT_STATUS] = mkf_str(FN_OUTPUT, fld_chksts);
1783
1784 if (sv->agent.status >= HCHK_STATUS_L57DATA)
1785 stats[ST_F_AGENT_CODE] = mkf_u32(FN_OUTPUT, sv->agent.code);
1786
1787 if (sv->agent.status >= HCHK_STATUS_CHECKED)
1788 stats[ST_F_AGENT_DURATION] = mkf_u64(FN_DURATION, sv->agent.duration);
1789
1790 stats[ST_F_AGENT_DESC] = mkf_str(FN_OUTPUT, get_check_status_description(sv->agent.status));
1791 stats[ST_F_LAST_AGT] = mkf_str(FN_OUTPUT, sv->agent.desc);
1792 stats[ST_F_AGENT_RISE] = mkf_u32(FO_CONFIG|FS_SERVICE, sv->agent.rise);
1793 stats[ST_F_AGENT_FALL] = mkf_u32(FO_CONFIG|FS_SERVICE, sv->agent.fall);
1794 stats[ST_F_AGENT_HEALTH] = mkf_u32(FO_CONFIG|FS_SERVICE, sv->agent.health);
1795 }
1796
1797 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
1798 if (px->mode == PR_MODE_HTTP) {
1799 stats[ST_F_HRSP_1XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[1]);
1800 stats[ST_F_HRSP_2XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[2]);
1801 stats[ST_F_HRSP_3XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[3]);
1802 stats[ST_F_HRSP_4XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[4]);
1803 stats[ST_F_HRSP_5XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[5]);
1804 stats[ST_F_HRSP_OTHER] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[0]);
1805 }
1806
1807 if (ref->observe)
1808 stats[ST_F_HANAFAIL] = mkf_u64(FN_COUNTER, sv->counters.failed_hana);
1809
1810 stats[ST_F_CLI_ABRT] = mkf_u64(FN_COUNTER, sv->counters.cli_aborts);
1811 stats[ST_F_SRV_ABRT] = mkf_u64(FN_COUNTER, sv->counters.srv_aborts);
1812 stats[ST_F_LASTSESS] = mkf_s32(FN_AGE, srv_lastsession(sv));
1813
1814 stats[ST_F_QTIME] = mkf_u32(FN_AVG, swrate_avg(sv->counters.q_time, TIME_STATS_SAMPLES));
1815 stats[ST_F_CTIME] = mkf_u32(FN_AVG, swrate_avg(sv->counters.c_time, TIME_STATS_SAMPLES));
1816 stats[ST_F_RTIME] = mkf_u32(FN_AVG, swrate_avg(sv->counters.d_time, TIME_STATS_SAMPLES));
1817 stats[ST_F_TTIME] = mkf_u32(FN_AVG, swrate_avg(sv->counters.t_time, TIME_STATS_SAMPLES));
1818
Christopher Faulet0d1c2a62019-11-08 14:59:51 +01001819 stats[ST_F_QT_MAX] = mkf_u32(FN_MAX, sv->counters.qtime_max);
1820 stats[ST_F_CT_MAX] = mkf_u32(FN_MAX, sv->counters.ctime_max);
1821 stats[ST_F_RT_MAX] = mkf_u32(FN_MAX, sv->counters.dtime_max);
1822 stats[ST_F_TT_MAX] = mkf_u32(FN_MAX, sv->counters.ttime_max);
1823
Willy Tarreau708c4162019-10-09 10:19:16 +02001824 if (flags & STAT_SHLGNDS) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001825 switch (addr_to_str(&sv->addr, str, sizeof(str))) {
1826 case AF_INET:
1827 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
Willy Tarreau04276f32017-01-06 17:41:29 +01001828 chunk_appendf(out, "%s:%d", str, sv->svc_port);
William Lallemand74c24fb2016-11-21 17:18:36 +01001829 break;
1830 case AF_INET6:
1831 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
Willy Tarreau04276f32017-01-06 17:41:29 +01001832 chunk_appendf(out, "[%s]:%d", str, sv->svc_port);
William Lallemand74c24fb2016-11-21 17:18:36 +01001833 break;
1834 case AF_UNIX:
1835 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, "unix");
1836 break;
1837 case -1:
1838 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
1839 chunk_strcat(out, strerror(errno));
1840 break;
1841 default: /* address family not supported */
1842 break;
1843 }
1844
1845 if (sv->cookie)
1846 stats[ST_F_COOKIE] = mkf_str(FO_CONFIG|FN_NAME|FS_SERVICE, sv->cookie);
1847 }
1848
1849 return 1;
1850}
1851
1852/* Dumps a line for server <sv> and proxy <px> to the trash and uses the state
Willy Tarreau43241ff2019-10-09 11:27:51 +02001853 * from stream interface <si>, and server state <state>. The caller is
1854 * responsible for clearing the trash if needed. Returns non-zero if it emits
1855 * anything, zero otherwise.
William Lallemand74c24fb2016-11-21 17:18:36 +01001856 */
Willy Tarreau43241ff2019-10-09 11:27:51 +02001857static int stats_dump_sv_stats(struct stream_interface *si, struct proxy *px, struct server *sv)
William Lallemand74c24fb2016-11-21 17:18:36 +01001858{
1859 struct appctx *appctx = __objt_appctx(si->end);
1860
Willy Tarreau43241ff2019-10-09 11:27:51 +02001861 if (!stats_fill_sv_stats(px, sv, appctx->ctx.stats.flags, stats, ST_F_TOTAL_FIELDS))
William Lallemand74c24fb2016-11-21 17:18:36 +01001862 return 0;
1863
Willy Tarreau43241ff2019-10-09 11:27:51 +02001864 return stats_dump_one_line(stats, px, appctx);
William Lallemand74c24fb2016-11-21 17:18:36 +01001865}
1866
1867/* Fill <stats> with the backend statistics. <stats> is
1868 * preallocated array of length <len>. The length of the array
1869 * must be at least ST_F_TOTAL_FIELDS. If this length is less
1870 * then this value, the function returns 0, otherwise, it
Willy Tarreau708c4162019-10-09 10:19:16 +02001871 * returns 1. <flags> can take the value STAT_SHLGNDS.
William Lallemand74c24fb2016-11-21 17:18:36 +01001872 */
1873int stats_fill_be_stats(struct proxy *px, int flags, struct field *stats, int len)
1874{
1875 if (len < ST_F_TOTAL_FIELDS)
1876 return 0;
1877
1878 memset(stats, 0, sizeof(*stats) * len);
1879
1880 stats[ST_F_PXNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
1881 stats[ST_F_SVNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, "BACKEND");
1882 stats[ST_F_MODE] = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
1883 stats[ST_F_QCUR] = mkf_u32(0, px->nbpend);
1884 stats[ST_F_QMAX] = mkf_u32(FN_MAX, px->be_counters.nbpend_max);
Thierry FOURNIER0ff98a42016-12-19 16:50:42 +01001885 stats[ST_F_SCUR] = mkf_u32(0, px->beconn);
William Lallemand74c24fb2016-11-21 17:18:36 +01001886 stats[ST_F_SMAX] = mkf_u32(FN_MAX, px->be_counters.conn_max);
1887 stats[ST_F_SLIM] = mkf_u32(FO_CONFIG|FN_LIMIT, px->fullconn);
1888 stats[ST_F_STOT] = mkf_u64(FN_COUNTER, px->be_counters.cum_conn);
1889 stats[ST_F_BIN] = mkf_u64(FN_COUNTER, px->be_counters.bytes_in);
1890 stats[ST_F_BOUT] = mkf_u64(FN_COUNTER, px->be_counters.bytes_out);
1891 stats[ST_F_DREQ] = mkf_u64(FN_COUNTER, px->be_counters.denied_req);
1892 stats[ST_F_DRESP] = mkf_u64(FN_COUNTER, px->be_counters.denied_resp);
1893 stats[ST_F_ECON] = mkf_u64(FN_COUNTER, px->be_counters.failed_conns);
1894 stats[ST_F_ERESP] = mkf_u64(FN_COUNTER, px->be_counters.failed_resp);
1895 stats[ST_F_WRETR] = mkf_u64(FN_COUNTER, px->be_counters.retries);
1896 stats[ST_F_WREDIS] = mkf_u64(FN_COUNTER, px->be_counters.redispatches);
Tim Duesterhus3fd19732018-05-27 20:35:08 +02001897 stats[ST_F_WREW] = mkf_u64(FN_COUNTER, px->be_counters.failed_rewrites);
Christopher Faulet0159ee42019-12-16 14:40:39 +01001898 stats[ST_F_EINT] = mkf_u64(FN_COUNTER, px->be_counters.internal_errors);
Willy Tarreauf1573842018-12-14 11:35:36 +01001899 stats[ST_F_CONNECT] = mkf_u64(FN_COUNTER, px->be_counters.connect);
1900 stats[ST_F_REUSE] = mkf_u64(FN_COUNTER, px->be_counters.reuse);
William Lallemand74c24fb2016-11-21 17:18:36 +01001901 stats[ST_F_STATUS] = mkf_str(FO_STATUS, (px->lbprm.tot_weight > 0 || !px->srv) ? "UP" : "DOWN");
1902 stats[ST_F_WEIGHT] = mkf_u32(FN_AVG, (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv);
1903 stats[ST_F_ACT] = mkf_u32(0, px->srv_act);
1904 stats[ST_F_BCK] = mkf_u32(0, px->srv_bck);
1905 stats[ST_F_CHKDOWN] = mkf_u64(FN_COUNTER, px->down_trans);
1906 stats[ST_F_LASTCHG] = mkf_u32(FN_AGE, now.tv_sec - px->last_change);
1907 if (px->srv)
1908 stats[ST_F_DOWNTIME] = mkf_u32(FN_COUNTER, be_downtime(px));
1909
1910 stats[ST_F_PID] = mkf_u32(FO_KEY, relative_pid);
1911 stats[ST_F_IID] = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
1912 stats[ST_F_SID] = mkf_u32(FO_KEY|FS_SERVICE, 0);
1913 stats[ST_F_LBTOT] = mkf_u64(FN_COUNTER, px->be_counters.cum_lbconn);
1914 stats[ST_F_TYPE] = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_BE);
1915 stats[ST_F_RATE] = mkf_u32(0, read_freq_ctr(&px->be_sess_per_sec));
1916 stats[ST_F_RATE_MAX] = mkf_u32(0, px->be_counters.sps_max);
1917
Willy Tarreau708c4162019-10-09 10:19:16 +02001918 if (flags & STAT_SHLGNDS) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001919 if (px->cookie_name)
1920 stats[ST_F_COOKIE] = mkf_str(FO_CONFIG|FN_NAME|FS_SERVICE, px->cookie_name);
1921 stats[ST_F_ALGO] = mkf_str(FO_CONFIG|FS_SERVICE, backend_lb_algo_str(px->lbprm.algo & BE_LB_ALGO));
1922 }
1923
1924 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
1925 if (px->mode == PR_MODE_HTTP) {
1926 stats[ST_F_REQ_TOT] = mkf_u64(FN_COUNTER, px->be_counters.p.http.cum_req);
1927 stats[ST_F_HRSP_1XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[1]);
1928 stats[ST_F_HRSP_2XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[2]);
1929 stats[ST_F_HRSP_3XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[3]);
1930 stats[ST_F_HRSP_4XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[4]);
1931 stats[ST_F_HRSP_5XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[5]);
1932 stats[ST_F_HRSP_OTHER] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[0]);
Willy Tarreaua1214a52018-12-14 14:00:25 +01001933 stats[ST_F_CACHE_LOOKUPS] = mkf_u64(FN_COUNTER, px->be_counters.p.http.cache_lookups);
1934 stats[ST_F_CACHE_HITS] = mkf_u64(FN_COUNTER, px->be_counters.p.http.cache_hits);
William Lallemand74c24fb2016-11-21 17:18:36 +01001935 }
1936
1937 stats[ST_F_CLI_ABRT] = mkf_u64(FN_COUNTER, px->be_counters.cli_aborts);
1938 stats[ST_F_SRV_ABRT] = mkf_u64(FN_COUNTER, px->be_counters.srv_aborts);
1939
1940 /* compression: in, out, bypassed, responses */
1941 stats[ST_F_COMP_IN] = mkf_u64(FN_COUNTER, px->be_counters.comp_in);
1942 stats[ST_F_COMP_OUT] = mkf_u64(FN_COUNTER, px->be_counters.comp_out);
1943 stats[ST_F_COMP_BYP] = mkf_u64(FN_COUNTER, px->be_counters.comp_byp);
1944 stats[ST_F_COMP_RSP] = mkf_u64(FN_COUNTER, px->be_counters.p.http.comp_rsp);
1945 stats[ST_F_LASTSESS] = mkf_s32(FN_AGE, be_lastsession(px));
1946
1947 stats[ST_F_QTIME] = mkf_u32(FN_AVG, swrate_avg(px->be_counters.q_time, TIME_STATS_SAMPLES));
1948 stats[ST_F_CTIME] = mkf_u32(FN_AVG, swrate_avg(px->be_counters.c_time, TIME_STATS_SAMPLES));
1949 stats[ST_F_RTIME] = mkf_u32(FN_AVG, swrate_avg(px->be_counters.d_time, TIME_STATS_SAMPLES));
1950 stats[ST_F_TTIME] = mkf_u32(FN_AVG, swrate_avg(px->be_counters.t_time, TIME_STATS_SAMPLES));
1951
Christopher Faulet0d1c2a62019-11-08 14:59:51 +01001952 stats[ST_F_QT_MAX] = mkf_u32(FN_MAX, px->be_counters.qtime_max);
1953 stats[ST_F_CT_MAX] = mkf_u32(FN_MAX, px->be_counters.ctime_max);
1954 stats[ST_F_RT_MAX] = mkf_u32(FN_MAX, px->be_counters.dtime_max);
1955 stats[ST_F_TT_MAX] = mkf_u32(FN_MAX, px->be_counters.ttime_max);
1956
William Lallemand74c24fb2016-11-21 17:18:36 +01001957 return 1;
1958}
1959
1960/* Dumps a line for backend <px> to the trash for and uses the state from stream
Willy Tarreau43241ff2019-10-09 11:27:51 +02001961 * interface <si>. The caller is responsible for clearing the trash if needed.
1962 * Returns non-zero if it emits anything, zero otherwise.
William Lallemand74c24fb2016-11-21 17:18:36 +01001963 */
Willy Tarreau43241ff2019-10-09 11:27:51 +02001964static int stats_dump_be_stats(struct stream_interface *si, struct proxy *px)
William Lallemand74c24fb2016-11-21 17:18:36 +01001965{
1966 struct appctx *appctx = __objt_appctx(si->end);
1967
1968 if (!(px->cap & PR_CAP_BE))
1969 return 0;
1970
1971 if ((appctx->ctx.stats.flags & STAT_BOUND) && !(appctx->ctx.stats.type & (1 << STATS_TYPE_BE)))
1972 return 0;
1973
Willy Tarreau43241ff2019-10-09 11:27:51 +02001974 if (!stats_fill_be_stats(px, appctx->ctx.stats.flags, stats, ST_F_TOTAL_FIELDS))
William Lallemand74c24fb2016-11-21 17:18:36 +01001975 return 0;
1976
Willy Tarreau43241ff2019-10-09 11:27:51 +02001977 return stats_dump_one_line(stats, px, appctx);
William Lallemand74c24fb2016-11-21 17:18:36 +01001978}
1979
1980/* Dumps the HTML table header for proxy <px> to the trash for and uses the state from
1981 * stream interface <si> and per-uri parameters <uri>. The caller is responsible
1982 * for clearing the trash if needed.
1983 */
Willy Tarreau676c29e2019-10-09 10:50:01 +02001984static void stats_dump_html_px_hdr(struct stream_interface *si, struct proxy *px)
William Lallemand74c24fb2016-11-21 17:18:36 +01001985{
1986 struct appctx *appctx = __objt_appctx(si->end);
1987 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
1988
1989 if (px->cap & PR_CAP_BE && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
1990 /* A form to enable/disable this proxy servers */
1991
1992 /* scope_txt = search pattern + search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
1993 scope_txt[0] = 0;
1994 if (appctx->ctx.stats.scope_len) {
Christopher Fauleted7a0662019-01-14 11:07:34 +01001995 const char *scope_ptr = stats_scope_ptr(appctx, si);
1996
William Lallemand74c24fb2016-11-21 17:18:36 +01001997 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Christopher Fauleted7a0662019-01-14 11:07:34 +01001998 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), scope_ptr, appctx->ctx.stats.scope_len);
William Lallemand74c24fb2016-11-21 17:18:36 +01001999 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
2000 }
2001
2002 chunk_appendf(&trash,
2003 "<form method=\"post\">");
2004 }
2005
2006 /* print a new table */
2007 chunk_appendf(&trash,
2008 "<table class=\"tbl\" width=\"100%%\">\n"
2009 "<tr class=\"titre\">"
2010 "<th class=\"pxname\" width=\"10%%\">");
2011
2012 chunk_appendf(&trash,
2013 "<a name=\"%s\"></a>%s"
2014 "<a class=px href=\"#%s\">%s</a>",
2015 px->id,
Willy Tarreau676c29e2019-10-09 10:50:01 +02002016 (appctx->ctx.stats.flags & STAT_SHLGNDS) ? "<u>":"",
William Lallemand74c24fb2016-11-21 17:18:36 +01002017 px->id, px->id);
2018
Willy Tarreau676c29e2019-10-09 10:50:01 +02002019 if (appctx->ctx.stats.flags & STAT_SHLGNDS) {
William Lallemand74c24fb2016-11-21 17:18:36 +01002020 /* cap, mode, id */
2021 chunk_appendf(&trash, "<div class=tips>cap: %s, mode: %s, id: %d",
2022 proxy_cap_str(px->cap), proxy_mode_str(px->mode),
2023 px->uuid);
2024 chunk_appendf(&trash, "</div>");
2025 }
2026
2027 chunk_appendf(&trash,
2028 "%s</th>"
2029 "<th class=\"%s\" width=\"90%%\">%s</th>"
2030 "</tr>\n"
2031 "</table>\n"
2032 "<table class=\"tbl\" width=\"100%%\">\n"
2033 "<tr class=\"titre\">",
Willy Tarreau676c29e2019-10-09 10:50:01 +02002034 (appctx->ctx.stats.flags & STAT_SHLGNDS) ? "</u>":"",
William Lallemand74c24fb2016-11-21 17:18:36 +01002035 px->desc ? "desc" : "empty", px->desc ? px->desc : "");
2036
Christopher Fauletbc271ec2019-12-02 11:29:04 +01002037 if (appctx->ctx.stats.flags & STAT_ADMIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +01002038 /* Column heading for Enable or Disable server */
Christopher Fauletbc271ec2019-12-02 11:29:04 +01002039 if ((px->cap & PR_CAP_BE) && px->srv)
2040 chunk_appendf(&trash,
2041 "<th rowspan=2 width=1><input type=\"checkbox\" "
2042 "onclick=\"for(c in document.getElementsByClassName('%s-checkbox')) "
2043 "document.getElementsByClassName('%s-checkbox').item(c).checked = this.checked\"></th>",
2044 px->id,
2045 px->id);
2046 else
2047 chunk_appendf(&trash, "<th rowspan=2></th>");
William Lallemand74c24fb2016-11-21 17:18:36 +01002048 }
2049
2050 chunk_appendf(&trash,
2051 "<th rowspan=2></th>"
2052 "<th colspan=3>Queue</th>"
2053 "<th colspan=3>Session rate</th><th colspan=6>Sessions</th>"
2054 "<th colspan=2>Bytes</th><th colspan=2>Denied</th>"
2055 "<th colspan=3>Errors</th><th colspan=2>Warnings</th>"
2056 "<th colspan=9>Server</th>"
2057 "</tr>\n"
2058 "<tr class=\"titre\">"
2059 "<th>Cur</th><th>Max</th><th>Limit</th>"
2060 "<th>Cur</th><th>Max</th><th>Limit</th><th>Cur</th><th>Max</th>"
2061 "<th>Limit</th><th>Total</th><th>LbTot</th><th>Last</th><th>In</th><th>Out</th>"
2062 "<th>Req</th><th>Resp</th><th>Req</th><th>Conn</th>"
2063 "<th>Resp</th><th>Retr</th><th>Redis</th>"
2064 "<th>Status</th><th>LastChk</th><th>Wght</th><th>Act</th>"
2065 "<th>Bck</th><th>Chk</th><th>Dwn</th><th>Dwntme</th>"
2066 "<th>Thrtle</th>\n"
2067 "</tr>");
2068}
2069
2070/* Dumps the HTML table trailer for proxy <px> to the trash for and uses the state from
2071 * stream interface <si>. The caller is responsible for clearing the trash if needed.
2072 */
2073static void stats_dump_html_px_end(struct stream_interface *si, struct proxy *px)
2074{
2075 struct appctx *appctx = __objt_appctx(si->end);
2076 chunk_appendf(&trash, "</table>");
2077
2078 if ((px->cap & PR_CAP_BE) && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
2079 /* close the form used to enable/disable this proxy servers */
2080 chunk_appendf(&trash,
2081 "Choose the action to perform on the checked servers : "
2082 "<select name=action>"
2083 "<option value=\"\"></option>"
2084 "<option value=\"ready\">Set state to READY</option>"
2085 "<option value=\"drain\">Set state to DRAIN</option>"
2086 "<option value=\"maint\">Set state to MAINT</option>"
2087 "<option value=\"dhlth\">Health: disable checks</option>"
2088 "<option value=\"ehlth\">Health: enable checks</option>"
2089 "<option value=\"hrunn\">Health: force UP</option>"
2090 "<option value=\"hnolb\">Health: force NOLB</option>"
2091 "<option value=\"hdown\">Health: force DOWN</option>"
2092 "<option value=\"dagent\">Agent: disable checks</option>"
2093 "<option value=\"eagent\">Agent: enable checks</option>"
2094 "<option value=\"arunn\">Agent: force UP</option>"
2095 "<option value=\"adown\">Agent: force DOWN</option>"
2096 "<option value=\"shutdown\">Kill Sessions</option>"
2097 "</select>"
2098 "<input type=\"hidden\" name=\"b\" value=\"#%d\">"
2099 "&nbsp;<input type=\"submit\" value=\"Apply\">"
2100 "</form>",
2101 px->uuid);
2102 }
2103
2104 chunk_appendf(&trash, "<p>\n");
2105}
2106
2107/*
2108 * Dumps statistics for a proxy. The output is sent to the stream interface's
2109 * input buffer. Returns 0 if it had to stop dumping data because of lack of
2110 * buffer space, or non-zero if everything completed. This function is used
2111 * both by the CLI and the HTTP entry points, and is able to dump the output
2112 * in HTML or CSV formats. If the later, <uri> must be NULL.
2113 */
Christopher Fauletef779222018-10-31 08:47:01 +01002114int stats_dump_proxy_to_buffer(struct stream_interface *si, struct htx *htx,
2115 struct proxy *px, struct uri_auth *uri)
William Lallemand74c24fb2016-11-21 17:18:36 +01002116{
2117 struct appctx *appctx = __objt_appctx(si->end);
2118 struct stream *s = si_strm(si);
2119 struct channel *rep = si_ic(si);
2120 struct server *sv, *svs; /* server and server-state, server-state=server or server->track */
2121 struct listener *l;
William Lallemand74c24fb2016-11-21 17:18:36 +01002122
2123 chunk_reset(&trash);
2124
2125 switch (appctx->ctx.stats.px_st) {
2126 case STAT_PX_ST_INIT:
2127 /* we are on a new proxy */
2128 if (uri && uri->scope) {
2129 /* we have a limited scope, we have to check the proxy name */
2130 struct stat_scope *scope;
2131 int len;
2132
2133 len = strlen(px->id);
2134 scope = uri->scope;
2135
2136 while (scope) {
2137 /* match exact proxy name */
2138 if (scope->px_len == len && !memcmp(px->id, scope->px_id, len))
2139 break;
2140
2141 /* match '.' which means 'self' proxy */
2142 if (!strcmp(scope->px_id, ".") && px == s->be)
2143 break;
2144 scope = scope->next;
2145 }
2146
2147 /* proxy name not found : don't dump anything */
2148 if (scope == NULL)
2149 return 1;
2150 }
2151
2152 /* if the user has requested a limited output and the proxy
2153 * name does not match, skip it.
2154 */
Christopher Fauleted7a0662019-01-14 11:07:34 +01002155 if (appctx->ctx.stats.scope_len) {
2156 const char *scope_ptr = stats_scope_ptr(appctx, si);
2157
2158 if (strnistr(px->id, strlen(px->id), scope_ptr, appctx->ctx.stats.scope_len) == NULL)
2159 return 1;
2160 }
William Lallemand74c24fb2016-11-21 17:18:36 +01002161
2162 if ((appctx->ctx.stats.flags & STAT_BOUND) &&
2163 (appctx->ctx.stats.iid != -1) &&
2164 (px->uuid != appctx->ctx.stats.iid))
2165 return 1;
2166
2167 appctx->ctx.stats.px_st = STAT_PX_ST_TH;
2168 /* fall through */
2169
2170 case STAT_PX_ST_TH:
2171 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreau676c29e2019-10-09 10:50:01 +02002172 stats_dump_html_px_hdr(si, px);
Christopher Fauletef779222018-10-31 08:47:01 +01002173 if (!stats_putchk(rep, htx, &trash))
2174 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002175 }
2176
2177 appctx->ctx.stats.px_st = STAT_PX_ST_FE;
2178 /* fall through */
2179
2180 case STAT_PX_ST_FE:
2181 /* print the frontend */
2182 if (stats_dump_fe_stats(si, px)) {
Christopher Fauletef779222018-10-31 08:47:01 +01002183 if (!stats_putchk(rep, htx, &trash))
2184 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002185 }
2186
2187 appctx->ctx.stats.l = px->conf.listeners.n;
2188 appctx->ctx.stats.px_st = STAT_PX_ST_LI;
2189 /* fall through */
2190
2191 case STAT_PX_ST_LI:
2192 /* stats.l has been initialized above */
2193 for (; appctx->ctx.stats.l != &px->conf.listeners; appctx->ctx.stats.l = l->by_fe.n) {
Christopher Fauletef779222018-10-31 08:47:01 +01002194 if (htx) {
2195 if (htx_almost_full(htx))
2196 goto full;
2197 }
2198 else {
2199 if (buffer_almost_full(&rep->buf))
2200 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002201 }
2202
2203 l = LIST_ELEM(appctx->ctx.stats.l, struct listener *, by_fe);
2204 if (!l->counters)
2205 continue;
2206
2207 if (appctx->ctx.stats.flags & STAT_BOUND) {
2208 if (!(appctx->ctx.stats.type & (1 << STATS_TYPE_SO)))
2209 break;
2210
2211 if (appctx->ctx.stats.sid != -1 && l->luid != appctx->ctx.stats.sid)
2212 continue;
2213 }
2214
2215 /* print the frontend */
Willy Tarreau43241ff2019-10-09 11:27:51 +02002216 if (stats_dump_li_stats(si, px, l)) {
Christopher Fauletef779222018-10-31 08:47:01 +01002217 if (!stats_putchk(rep, htx, &trash))
2218 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002219 }
2220 }
2221
2222 appctx->ctx.stats.sv = px->srv; /* may be NULL */
2223 appctx->ctx.stats.px_st = STAT_PX_ST_SV;
2224 /* fall through */
2225
2226 case STAT_PX_ST_SV:
2227 /* stats.sv has been initialized above */
2228 for (; appctx->ctx.stats.sv != NULL; appctx->ctx.stats.sv = sv->next) {
Christopher Fauletef779222018-10-31 08:47:01 +01002229 if (htx) {
2230 if (htx_almost_full(htx))
2231 goto full;
2232 }
2233 else {
2234 if (buffer_almost_full(&rep->buf))
2235 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002236 }
2237
2238 sv = appctx->ctx.stats.sv;
2239
2240 if (appctx->ctx.stats.flags & STAT_BOUND) {
2241 if (!(appctx->ctx.stats.type & (1 << STATS_TYPE_SV)))
2242 break;
2243
2244 if (appctx->ctx.stats.sid != -1 && sv->puid != appctx->ctx.stats.sid)
2245 continue;
2246 }
2247
2248 svs = sv;
2249 while (svs->track)
2250 svs = svs->track;
2251
2252 /* do not report servers which are DOWN and not changing state */
2253 if ((appctx->ctx.stats.flags & STAT_HIDE_DOWN) &&
Emeric Brun52a91d32017-08-31 14:41:55 +02002254 ((sv->cur_admin & SRV_ADMF_MAINT) || /* server is in maintenance */
2255 (sv->cur_state == SRV_ST_STOPPED && /* server is down */
William Lallemand74c24fb2016-11-21 17:18:36 +01002256 (!((svs->agent.state | svs->check.state) & CHK_ST_ENABLED) ||
2257 ((svs->agent.state & CHK_ST_ENABLED) && !svs->agent.health) ||
2258 ((svs->check.state & CHK_ST_ENABLED) && !svs->check.health))))) {
2259 continue;
2260 }
2261
Willy Tarreau43241ff2019-10-09 11:27:51 +02002262 if (stats_dump_sv_stats(si, px, sv)) {
Christopher Fauletef779222018-10-31 08:47:01 +01002263 if (!stats_putchk(rep, htx, &trash))
2264 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002265 }
2266 } /* for sv */
2267
2268 appctx->ctx.stats.px_st = STAT_PX_ST_BE;
2269 /* fall through */
2270
2271 case STAT_PX_ST_BE:
2272 /* print the backend */
Willy Tarreau43241ff2019-10-09 11:27:51 +02002273 if (stats_dump_be_stats(si, px)) {
Christopher Fauletef779222018-10-31 08:47:01 +01002274 if (!stats_putchk(rep, htx, &trash))
2275 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002276 }
2277
2278 appctx->ctx.stats.px_st = STAT_PX_ST_END;
2279 /* fall through */
2280
2281 case STAT_PX_ST_END:
2282 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
2283 stats_dump_html_px_end(si, px);
Christopher Fauletef779222018-10-31 08:47:01 +01002284 if (!stats_putchk(rep, htx, &trash))
2285 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002286 }
2287
2288 appctx->ctx.stats.px_st = STAT_PX_ST_FIN;
2289 /* fall through */
2290
2291 case STAT_PX_ST_FIN:
2292 return 1;
2293
2294 default:
2295 /* unknown state, we should put an abort() here ! */
2296 return 1;
2297 }
Christopher Fauletef779222018-10-31 08:47:01 +01002298
2299 full:
2300 si_rx_room_blk(si);
2301 return 0;
William Lallemand74c24fb2016-11-21 17:18:36 +01002302}
2303
2304/* Dumps the HTTP stats head block to the trash for and uses the per-uri
2305 * parameters <uri>. The caller is responsible for clearing the trash if needed.
2306 */
Willy Tarreau676c29e2019-10-09 10:50:01 +02002307static void stats_dump_html_head(struct appctx *appctx, struct uri_auth *uri)
William Lallemand74c24fb2016-11-21 17:18:36 +01002308{
2309 /* WARNING! This must fit in the first buffer !!! */
2310 chunk_appendf(&trash,
2311 "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"\n"
2312 "\"http://www.w3.org/TR/html4/loose.dtd\">\n"
2313 "<html><head><title>Statistics Report for " PRODUCT_NAME "%s%s</title>\n"
2314 "<meta http-equiv=\"content-type\" content=\"text/html; charset=iso-8859-1\">\n"
2315 "<style type=\"text/css\"><!--\n"
2316 "body {"
2317 " font-family: arial, helvetica, sans-serif;"
2318 " font-size: 12px;"
2319 " font-weight: normal;"
2320 " color: black;"
2321 " background: white;"
2322 "}\n"
2323 "th,td {"
2324 " font-size: 10px;"
2325 "}\n"
2326 "h1 {"
2327 " font-size: x-large;"
2328 " margin-bottom: 0.5em;"
2329 "}\n"
2330 "h2 {"
2331 " font-family: helvetica, arial;"
2332 " font-size: x-large;"
2333 " font-weight: bold;"
2334 " font-style: italic;"
2335 " color: #6020a0;"
2336 " margin-top: 0em;"
2337 " margin-bottom: 0em;"
2338 "}\n"
2339 "h3 {"
2340 " font-family: helvetica, arial;"
2341 " font-size: 16px;"
2342 " font-weight: bold;"
2343 " color: #b00040;"
2344 " background: #e8e8d0;"
2345 " margin-top: 0em;"
2346 " margin-bottom: 0em;"
2347 "}\n"
2348 "li {"
2349 " margin-top: 0.25em;"
2350 " margin-right: 2em;"
2351 "}\n"
2352 ".hr {margin-top: 0.25em;"
2353 " border-color: black;"
2354 " border-bottom-style: solid;"
2355 "}\n"
2356 ".titre {background: #20D0D0;color: #000000; font-weight: bold; text-align: center;}\n"
2357 ".total {background: #20D0D0;color: #ffff80;}\n"
2358 ".frontend {background: #e8e8d0;}\n"
2359 ".socket {background: #d0d0d0;}\n"
2360 ".backend {background: #e8e8d0;}\n"
2361 ".active_down {background: #ff9090;}\n"
2362 ".active_going_up {background: #ffd020;}\n"
2363 ".active_going_down {background: #ffffa0;}\n"
2364 ".active_up {background: #c0ffc0;}\n"
2365 ".active_nolb {background: #20a0ff;}\n"
2366 ".active_draining {background: #20a0FF;}\n"
2367 ".active_no_check {background: #e0e0e0;}\n"
2368 ".backup_down {background: #ff9090;}\n"
2369 ".backup_going_up {background: #ff80ff;}\n"
2370 ".backup_going_down {background: #c060ff;}\n"
2371 ".backup_up {background: #b0d0ff;}\n"
2372 ".backup_nolb {background: #90b0e0;}\n"
2373 ".backup_draining {background: #cc9900;}\n"
2374 ".backup_no_check {background: #e0e0e0;}\n"
2375 ".maintain {background: #c07820;}\n"
2376 ".rls {letter-spacing: 0.2em; margin-right: 1px;}\n" /* right letter spacing (used for grouping digits) */
2377 "\n"
2378 "a.px:link {color: #ffff40; text-decoration: none;}"
2379 "a.px:visited {color: #ffff40; text-decoration: none;}"
2380 "a.px:hover {color: #ffffff; text-decoration: none;}"
2381 "a.lfsb:link {color: #000000; text-decoration: none;}"
2382 "a.lfsb:visited {color: #000000; text-decoration: none;}"
2383 "a.lfsb:hover {color: #505050; text-decoration: none;}"
2384 "\n"
2385 "table.tbl { border-collapse: collapse; border-style: none;}\n"
2386 "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"
2387 "table.tbl td.ac { text-align: center;}\n"
2388 "table.tbl th { border-width: 1px; border-style: solid solid solid solid; border-color: gray;}\n"
2389 "table.tbl th.pxname { background: #b00040; color: #ffff40; font-weight: bold; border-style: solid solid none solid; padding: 2px 3px; white-space: nowrap;}\n"
2390 "table.tbl th.empty { border-style: none; empty-cells: hide; background: white;}\n"
2391 "table.tbl th.desc { background: white; border-style: solid solid none solid; text-align: left; padding: 2px 3px;}\n"
2392 "\n"
2393 "table.lgd { border-collapse: collapse; border-width: 1px; border-style: none none none solid; border-color: black;}\n"
2394 "table.lgd td { border-width: 1px; border-style: solid solid solid solid; border-color: gray; padding: 2px;}\n"
2395 "table.lgd td.noborder { border-style: none; padding: 2px; white-space: nowrap;}\n"
2396 "table.det { border-collapse: collapse; border-style: none; }\n"
2397 "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"
2398 "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"
2399 "u {text-decoration:none; border-bottom: 1px dotted black;}\n"
2400 "div.tips {\n"
2401 " display:block;\n"
2402 " visibility:hidden;\n"
2403 " z-index:2147483647;\n"
2404 " position:absolute;\n"
2405 " padding:2px 4px 3px;\n"
2406 " background:#f0f060; color:#000000;\n"
2407 " border:1px solid #7040c0;\n"
2408 " white-space:nowrap;\n"
2409 " font-style:normal;font-size:11px;font-weight:normal;\n"
2410 " -moz-border-radius:3px;-webkit-border-radius:3px;border-radius:3px;\n"
2411 " -moz-box-shadow:gray 2px 2px 3px;-webkit-box-shadow:gray 2px 2px 3px;box-shadow:gray 2px 2px 3px;\n"
2412 "}\n"
2413 "u:hover div.tips {visibility:visible;}\n"
2414 "-->\n"
2415 "</style></head>\n",
Willy Tarreau676c29e2019-10-09 10:50:01 +02002416 (appctx->ctx.stats.flags & STAT_SHNODE) ? " on " : "",
Willy Tarreau027d2062020-01-23 11:47:13 +01002417 (appctx->ctx.stats.flags & STAT_SHNODE) ? (uri && uri->node ? uri->node : global.node) : ""
William Lallemand74c24fb2016-11-21 17:18:36 +01002418 );
2419}
2420
2421/* Dumps the HTML stats information block to the trash for and uses the state from
2422 * stream interface <si> and per-uri parameters <uri>. The caller is responsible
2423 * for clearing the trash if needed.
2424 */
2425static void stats_dump_html_info(struct stream_interface *si, struct uri_auth *uri)
2426{
2427 struct appctx *appctx = __objt_appctx(si->end);
2428 unsigned int up = (now.tv_sec - start_date.tv_sec);
2429 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
Christopher Fauleted7a0662019-01-14 11:07:34 +01002430 const char *scope_ptr = stats_scope_ptr(appctx, si);
Willy Tarreau1713c032019-05-23 12:23:55 +02002431 unsigned long long bps = (unsigned long long)read_freq_ctr(&global.out_32bps) * 32;
2432
2433 /* Turn the bytes per second to bits per second and take care of the
2434 * usual ethernet overhead in order to help figure how far we are from
2435 * interface saturation since it's the only case which usually matters.
2436 * For this we count the total size of an Ethernet frame on the wire
2437 * including preamble and IFG (1538) for the largest TCP segment it
2438 * transports (1448 with TCP timestamps). This is not valid for smaller
2439 * packets (under-estimated), but it gives a reasonably accurate
2440 * estimation of how far we are from uplink saturation.
2441 */
2442 bps = bps * 8 * 1538 / 1448;
William Lallemand74c24fb2016-11-21 17:18:36 +01002443
2444 /* WARNING! this has to fit the first packet too.
2445 * We are around 3.5 kB, add adding entries will
2446 * become tricky if we want to support 4kB buffers !
2447 */
2448 chunk_appendf(&trash,
2449 "<body><h1><a href=\"" PRODUCT_URL "\" style=\"text-decoration: none;\">"
2450 PRODUCT_NAME "%s</a></h1>\n"
2451 "<h2>Statistics Report for pid %d%s%s%s%s</h2>\n"
2452 "<hr width=\"100%%\" class=\"hr\">\n"
2453 "<h3>&gt; General process information</h3>\n"
2454 "<table border=0><tr><td align=\"left\" nowrap width=\"1%%\">\n"
Yves Lafon95317282018-02-26 11:10:37 +01002455 "<p><b>pid = </b> %d (process #%d, nbproc = %d, nbthread = %d)<br>\n"
William Lallemand74c24fb2016-11-21 17:18:36 +01002456 "<b>uptime = </b> %dd %dh%02dm%02ds<br>\n"
2457 "<b>system limits:</b> memmax = %s%s; ulimit-n = %d<br>\n"
2458 "<b>maxsock = </b> %d; <b>maxconn = </b> %d; <b>maxpipes = </b> %d<br>\n"
Willy Tarreau1713c032019-05-23 12:23:55 +02002459 "current conns = %d; current pipes = %d/%d; conn rate = %d/sec; bit rate = %.3f %cbps<br>\n"
William Lallemand74c24fb2016-11-21 17:18:36 +01002460 "Running tasks: %d/%d; idle = %d %%<br>\n"
2461 "</td><td align=\"center\" nowrap>\n"
2462 "<table class=\"lgd\"><tr>\n"
2463 "<td class=\"active_up\">&nbsp;</td><td class=\"noborder\">active UP </td>"
2464 "<td class=\"backup_up\">&nbsp;</td><td class=\"noborder\">backup UP </td>"
2465 "</tr><tr>\n"
2466 "<td class=\"active_going_down\"></td><td class=\"noborder\">active UP, going down </td>"
2467 "<td class=\"backup_going_down\"></td><td class=\"noborder\">backup UP, going down </td>"
2468 "</tr><tr>\n"
2469 "<td class=\"active_going_up\"></td><td class=\"noborder\">active DOWN, going up </td>"
2470 "<td class=\"backup_going_up\"></td><td class=\"noborder\">backup DOWN, going up </td>"
2471 "</tr><tr>\n"
2472 "<td class=\"active_down\"></td><td class=\"noborder\">active or backup DOWN &nbsp;</td>"
2473 "<td class=\"active_no_check\"></td><td class=\"noborder\">not checked </td>"
2474 "</tr><tr>\n"
2475 "<td class=\"maintain\"></td><td class=\"noborder\" colspan=\"3\">active or backup DOWN for maintenance (MAINT) &nbsp;</td>"
2476 "</tr><tr>\n"
2477 "<td class=\"active_draining\"></td><td class=\"noborder\" colspan=\"3\">active or backup SOFT STOPPED for maintenance &nbsp;</td>"
2478 "</tr></table>\n"
2479 "Note: \"NOLB\"/\"DRAIN\" = UP with load-balancing disabled."
2480 "</td>"
2481 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
2482 "<b>Display option:</b><ul style=\"margin-top: 0.25em;\">"
2483 "",
Willy Tarreau676c29e2019-10-09 10:50:01 +02002484 (appctx->ctx.stats.flags & STAT_HIDEVER) ? "" : (stats_version_string),
2485 pid, (appctx->ctx.stats.flags & STAT_SHNODE) ? " on " : "",
2486 (appctx->ctx.stats.flags & STAT_SHNODE) ? (uri->node ? uri->node : global.node) : "",
2487 (appctx->ctx.stats.flags & STAT_SHDESC) ? ": " : "",
2488 (appctx->ctx.stats.flags & STAT_SHDESC) ? (uri->desc ? uri->desc : global.desc) : "",
Yves Lafon95317282018-02-26 11:10:37 +01002489 pid, relative_pid, global.nbproc, global.nbthread,
William Lallemand74c24fb2016-11-21 17:18:36 +01002490 up / 86400, (up % 86400) / 3600,
2491 (up % 3600) / 60, (up % 60),
2492 global.rlimit_memmax ? ultoa(global.rlimit_memmax) : "unlimited",
2493 global.rlimit_memmax ? " MB" : "",
2494 global.rlimit_nofile,
2495 global.maxsock, global.maxconn, global.maxpipes,
2496 actconn, pipes_used, pipes_used+pipes_free, read_freq_ctr(&global.conn_per_sec),
Willy Tarreau1713c032019-05-23 12:23:55 +02002497 bps >= 1000000000UL ? (bps / 1000000000.0) : bps >= 1000000UL ? (bps / 1000000.0) : (bps / 1000.0),
2498 bps >= 1000000000UL ? 'G' : bps >= 1000000UL ? 'M' : 'k',
Willy Tarreau81036f22019-05-20 19:24:50 +02002499 tasks_run_queue_cur, nb_tasks_cur, ti->idle_pct
William Lallemand74c24fb2016-11-21 17:18:36 +01002500 );
2501
2502 /* scope_txt = search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
Christopher Fauleted7a0662019-01-14 11:07:34 +01002503 memcpy(scope_txt, scope_ptr, appctx->ctx.stats.scope_len);
William Lallemand74c24fb2016-11-21 17:18:36 +01002504 scope_txt[appctx->ctx.stats.scope_len] = '\0';
2505
2506 chunk_appendf(&trash,
2507 "<li><form method=\"GET\">Scope : <input value=\"%s\" name=\"" STAT_SCOPE_INPUT_NAME "\" size=\"8\" maxlength=\"%d\" tabindex=\"1\"/></form>\n",
2508 (appctx->ctx.stats.scope_len > 0) ? scope_txt : "",
2509 STAT_SCOPE_TXT_MAXLEN);
2510
2511 /* scope_txt = search pattern + search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
2512 scope_txt[0] = 0;
2513 if (appctx->ctx.stats.scope_len) {
2514 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Christopher Fauleted7a0662019-01-14 11:07:34 +01002515 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), scope_ptr, appctx->ctx.stats.scope_len);
William Lallemand74c24fb2016-11-21 17:18:36 +01002516 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
2517 }
2518
2519 if (appctx->ctx.stats.flags & STAT_HIDE_DOWN)
2520 chunk_appendf(&trash,
2521 "<li><a href=\"%s%s%s%s\">Show all servers</a><br>\n",
2522 uri->uri_prefix,
2523 "",
2524 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2525 scope_txt);
2526 else
2527 chunk_appendf(&trash,
2528 "<li><a href=\"%s%s%s%s\">Hide 'DOWN' servers</a><br>\n",
2529 uri->uri_prefix,
2530 ";up",
2531 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2532 scope_txt);
2533
2534 if (uri->refresh > 0) {
2535 if (appctx->ctx.stats.flags & STAT_NO_REFRESH)
2536 chunk_appendf(&trash,
2537 "<li><a href=\"%s%s%s%s\">Enable refresh</a><br>\n",
2538 uri->uri_prefix,
2539 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2540 "",
2541 scope_txt);
2542 else
2543 chunk_appendf(&trash,
2544 "<li><a href=\"%s%s%s%s\">Disable refresh</a><br>\n",
2545 uri->uri_prefix,
2546 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2547 ";norefresh",
2548 scope_txt);
2549 }
2550
2551 chunk_appendf(&trash,
2552 "<li><a href=\"%s%s%s%s\">Refresh now</a><br>\n",
2553 uri->uri_prefix,
2554 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2555 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2556 scope_txt);
2557
2558 chunk_appendf(&trash,
2559 "<li><a href=\"%s;csv%s%s\">CSV export</a><br>\n",
2560 uri->uri_prefix,
2561 (uri->refresh > 0) ? ";norefresh" : "",
2562 scope_txt);
2563
2564 chunk_appendf(&trash,
Christopher Faulet6338a082019-09-09 15:50:54 +02002565 "<li><a href=\"%s;json%s%s\">JSON export</a> (<a href=\"%s;json-schema\">schema</a>)<br>\n",
2566 uri->uri_prefix,
2567 (uri->refresh > 0) ? ";norefresh" : "",
2568 scope_txt, uri->uri_prefix);
2569
2570 chunk_appendf(&trash,
William Lallemand74c24fb2016-11-21 17:18:36 +01002571 "</ul></td>"
2572 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
2573 "<b>External resources:</b><ul style=\"margin-top: 0.25em;\">\n"
2574 "<li><a href=\"" PRODUCT_URL "\">Primary site</a><br>\n"
2575 "<li><a href=\"" PRODUCT_URL_UPD "\">Updates (v" PRODUCT_BRANCH ")</a><br>\n"
2576 "<li><a href=\"" PRODUCT_URL_DOC "\">Online manual</a><br>\n"
2577 "</ul>"
2578 "</td>"
2579 "</tr></table>\n"
2580 ""
2581 );
2582
2583 if (appctx->ctx.stats.st_code) {
2584 switch (appctx->ctx.stats.st_code) {
2585 case STAT_STATUS_DONE:
2586 chunk_appendf(&trash,
2587 "<p><div class=active_up>"
2588 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2589 "Action processed successfully."
2590 "</div>\n", uri->uri_prefix,
2591 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2592 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2593 scope_txt);
2594 break;
2595 case STAT_STATUS_NONE:
2596 chunk_appendf(&trash,
2597 "<p><div class=active_going_down>"
2598 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2599 "Nothing has changed."
2600 "</div>\n", uri->uri_prefix,
2601 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2602 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2603 scope_txt);
2604 break;
2605 case STAT_STATUS_PART:
2606 chunk_appendf(&trash,
2607 "<p><div class=active_going_down>"
2608 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2609 "Action partially processed.<br>"
2610 "Some server names are probably unknown or ambiguous (duplicated names in the backend)."
2611 "</div>\n", uri->uri_prefix,
2612 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2613 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2614 scope_txt);
2615 break;
2616 case STAT_STATUS_ERRP:
2617 chunk_appendf(&trash,
2618 "<p><div class=active_down>"
2619 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2620 "Action not processed because of invalid parameters."
2621 "<ul>"
2622 "<li>The action is maybe unknown.</li>"
Christopher Faulet2f9a41d2019-02-27 15:30:57 +01002623 "<li>Invalid key parameter (empty or too long).</li>"
William Lallemand74c24fb2016-11-21 17:18:36 +01002624 "<li>The backend name is probably unknown or ambiguous (duplicated names).</li>"
2625 "<li>Some server names are probably unknown or ambiguous (duplicated names in the backend).</li>"
2626 "</ul>"
2627 "</div>\n", uri->uri_prefix,
2628 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2629 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2630 scope_txt);
2631 break;
2632 case STAT_STATUS_EXCD:
2633 chunk_appendf(&trash,
2634 "<p><div class=active_down>"
2635 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2636 "<b>Action not processed : the buffer couldn't store all the data.<br>"
2637 "You should retry with less servers at a time.</b>"
2638 "</div>\n", uri->uri_prefix,
2639 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2640 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2641 scope_txt);
2642 break;
2643 case STAT_STATUS_DENY:
2644 chunk_appendf(&trash,
2645 "<p><div class=active_down>"
2646 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2647 "<b>Action denied.</b>"
2648 "</div>\n", uri->uri_prefix,
2649 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2650 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2651 scope_txt);
2652 break;
Christopher Faulet3c2ecf72019-02-27 16:41:27 +01002653 case STAT_STATUS_IVAL:
2654 chunk_appendf(&trash,
2655 "<p><div class=active_down>"
2656 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2657 "<b>Invalid requests (unsupported method or chunked encoded request).</b>"
2658 "</div>\n", uri->uri_prefix,
2659 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2660 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2661 scope_txt);
2662 break;
William Lallemand74c24fb2016-11-21 17:18:36 +01002663 default:
2664 chunk_appendf(&trash,
2665 "<p><div class=active_no_check>"
2666 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2667 "Unexpected result."
2668 "</div>\n", uri->uri_prefix,
2669 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2670 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2671 scope_txt);
2672 }
2673 chunk_appendf(&trash, "<p>\n");
2674 }
2675}
2676
2677/* Dumps the HTML stats trailer block to the trash. The caller is responsible
2678 * for clearing the trash if needed.
2679 */
2680static void stats_dump_html_end()
2681{
2682 chunk_appendf(&trash, "</body></html>\n");
2683}
2684
Simon Horman05ee2132017-01-04 09:37:25 +01002685/* Dumps the stats JSON header to the trash buffer which. The caller is responsible
2686 * for clearing it if needed.
2687 */
2688static void stats_dump_json_header()
2689{
2690 chunk_strcat(&trash, "[");
2691}
2692
2693
2694/* Dumps the JSON stats trailer block to the trash. The caller is responsible
2695 * for clearing the trash if needed.
2696 */
2697static void stats_dump_json_end()
2698{
2699 chunk_strcat(&trash, "]");
2700}
2701
William Lallemand74c24fb2016-11-21 17:18:36 +01002702/* This function dumps statistics onto the stream interface's read buffer in
2703 * either CSV or HTML format. <uri> contains some HTML-specific parameters that
2704 * are ignored for CSV format (hence <uri> may be NULL there). It returns 0 if
2705 * it had to stop writing data and an I/O is needed, 1 if the dump is finished
2706 * and the stream must be closed, or -1 in case of any error. This function is
2707 * used by both the CLI and the HTTP handlers.
2708 */
Christopher Fauletef779222018-10-31 08:47:01 +01002709static int stats_dump_stat_to_buffer(struct stream_interface *si, struct htx *htx,
2710 struct uri_auth *uri)
William Lallemand74c24fb2016-11-21 17:18:36 +01002711{
2712 struct appctx *appctx = __objt_appctx(si->end);
2713 struct channel *rep = si_ic(si);
2714 struct proxy *px;
2715
2716 chunk_reset(&trash);
2717
2718 switch (appctx->st2) {
2719 case STAT_ST_INIT:
2720 appctx->st2 = STAT_ST_HEAD; /* let's start producing data */
2721 /* fall through */
2722
2723 case STAT_ST_HEAD:
2724 if (appctx->ctx.stats.flags & STAT_FMT_HTML)
Willy Tarreau676c29e2019-10-09 10:50:01 +02002725 stats_dump_html_head(appctx, uri);
Christopher Faulet6338a082019-09-09 15:50:54 +02002726 else if (appctx->ctx.stats.flags & STAT_JSON_SCHM)
2727 stats_dump_json_schema(&trash);
Simon Horman05ee2132017-01-04 09:37:25 +01002728 else if (appctx->ctx.stats.flags & STAT_FMT_JSON)
Willy Tarreau9d7fb632017-04-11 07:53:04 +02002729 stats_dump_json_header();
William Lallemand74c24fb2016-11-21 17:18:36 +01002730 else if (!(appctx->ctx.stats.flags & STAT_FMT_TYPED))
2731 stats_dump_csv_header();
2732
Christopher Fauletef779222018-10-31 08:47:01 +01002733 if (!stats_putchk(rep, htx, &trash))
2734 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002735
Christopher Faulet6338a082019-09-09 15:50:54 +02002736 if (appctx->ctx.stats.flags & STAT_JSON_SCHM) {
2737 appctx->st2 = STAT_ST_FIN;
2738 return 1;
2739 }
William Lallemand74c24fb2016-11-21 17:18:36 +01002740 appctx->st2 = STAT_ST_INFO;
2741 /* fall through */
2742
2743 case STAT_ST_INFO:
2744 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
2745 stats_dump_html_info(si, uri);
Christopher Fauletef779222018-10-31 08:47:01 +01002746 if (!stats_putchk(rep, htx, &trash))
2747 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002748 }
2749
Olivier Houchardfbc74e82017-11-24 16:54:05 +01002750 appctx->ctx.stats.px = proxies_list;
William Lallemand74c24fb2016-11-21 17:18:36 +01002751 appctx->ctx.stats.px_st = STAT_PX_ST_INIT;
2752 appctx->st2 = STAT_ST_LIST;
2753 /* fall through */
2754
2755 case STAT_ST_LIST:
2756 /* dump proxies */
2757 while (appctx->ctx.stats.px) {
Christopher Fauletef779222018-10-31 08:47:01 +01002758 if (htx) {
2759 if (htx_almost_full(htx))
2760 goto full;
2761 }
2762 else {
2763 if (buffer_almost_full(&rep->buf))
2764 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002765 }
2766
2767 px = appctx->ctx.stats.px;
2768 /* skip the disabled proxies, global frontend and non-networked ones */
2769 if (px->state != PR_STSTOPPED && px->uuid > 0 && (px->cap & (PR_CAP_FE | PR_CAP_BE)))
Christopher Fauletef779222018-10-31 08:47:01 +01002770 if (stats_dump_proxy_to_buffer(si, htx, px, uri) == 0)
William Lallemand74c24fb2016-11-21 17:18:36 +01002771 return 0;
2772
2773 appctx->ctx.stats.px = px->next;
2774 appctx->ctx.stats.px_st = STAT_PX_ST_INIT;
2775 }
2776 /* here, we just have reached the last proxy */
2777
2778 appctx->st2 = STAT_ST_END;
2779 /* fall through */
2780
2781 case STAT_ST_END:
Simon Horman05ee2132017-01-04 09:37:25 +01002782 if (appctx->ctx.stats.flags & (STAT_FMT_HTML|STAT_FMT_JSON)) {
2783 if (appctx->ctx.stats.flags & STAT_FMT_HTML)
2784 stats_dump_html_end();
2785 else
2786 stats_dump_json_end();
Christopher Fauletef779222018-10-31 08:47:01 +01002787 if (!stats_putchk(rep, htx, &trash))
2788 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002789 }
2790
2791 appctx->st2 = STAT_ST_FIN;
2792 /* fall through */
2793
2794 case STAT_ST_FIN:
2795 return 1;
2796
2797 default:
2798 /* unknown state ! */
2799 appctx->st2 = STAT_ST_FIN;
2800 return -1;
2801 }
Christopher Fauletef779222018-10-31 08:47:01 +01002802
2803 full:
2804 si_rx_room_blk(si);
2805 return 0;
2806
William Lallemand74c24fb2016-11-21 17:18:36 +01002807}
2808
2809/* We reached the stats page through a POST request. The appctx is
2810 * expected to have already been allocated by the caller.
2811 * Parse the posted data and enable/disable servers if necessary.
2812 * Returns 1 if request was parsed or zero if it needs more data.
2813 */
2814static int stats_process_http_post(struct stream_interface *si)
2815{
2816 struct stream *s = si_strm(si);
2817 struct appctx *appctx = objt_appctx(si->end);
2818
2819 struct proxy *px = NULL;
2820 struct server *sv = NULL;
2821
2822 char key[LINESIZE];
2823 int action = ST_ADM_ACTION_NONE;
2824 int reprocess = 0;
2825
2826 int total_servers = 0;
2827 int altered_servers = 0;
2828
2829 char *first_param, *cur_param, *next_param, *end_params;
2830 char *st_cur_param = NULL;
2831 char *st_next_param = NULL;
2832
Christopher Fauleta3618372018-12-13 21:59:56 +01002833 struct buffer *temp = get_trash_chunk();
William Lallemand74c24fb2016-11-21 17:18:36 +01002834
Christopher Fauletb7f88902019-07-15 21:56:43 +02002835 struct htx *htx = htxbuf(&s->req.buf);
2836 struct htx_blk *blk;
Christopher Fauleta3618372018-12-13 21:59:56 +01002837
Christopher Fauletb7f88902019-07-15 21:56:43 +02002838 /* we need more data */
2839 if (s->txn->req.msg_state < HTTP_MSG_DONE) {
2840 /* check if we can receive more */
2841 if (htx_free_data_space(htx) <= global.tune.maxrewrite) {
2842 appctx->ctx.stats.st_code = STAT_STATUS_EXCD;
2843 goto out;
Christopher Fauleta3618372018-12-13 21:59:56 +01002844 }
Christopher Fauletb7f88902019-07-15 21:56:43 +02002845 goto wait;
2846 }
Christopher Fauleta3618372018-12-13 21:59:56 +01002847
Christopher Fauletb7f88902019-07-15 21:56:43 +02002848 /* The request was fully received. Copy data */
2849 blk = htx_get_head_blk(htx);
2850 while (blk) {
2851 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Fauleta3618372018-12-13 21:59:56 +01002852
Christopher Fauletb7f88902019-07-15 21:56:43 +02002853 if (type == HTX_BLK_EOM || type == HTX_BLK_TLR || type == HTX_BLK_EOT)
2854 break;
2855 if (type == HTX_BLK_DATA) {
2856 struct ist v = htx_get_blk_value(htx, blk);
Christopher Fauleta3618372018-12-13 21:59:56 +01002857
Christopher Fauletb7f88902019-07-15 21:56:43 +02002858 if (!chunk_memcat(temp, v.ptr, v.len)) {
Christopher Faulet2f9a41d2019-02-27 15:30:57 +01002859 appctx->ctx.stats.st_code = STAT_STATUS_EXCD;
2860 goto out;
2861 }
Christopher Fauleta3618372018-12-13 21:59:56 +01002862 }
Christopher Fauletb7f88902019-07-15 21:56:43 +02002863 blk = htx_get_next_blk(htx, blk);
William Lallemand74c24fb2016-11-21 17:18:36 +01002864 }
2865
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002866 first_param = temp->area;
Christopher Fauleta3618372018-12-13 21:59:56 +01002867 end_params = temp->area + temp->data;
William Lallemand74c24fb2016-11-21 17:18:36 +01002868 cur_param = next_param = end_params;
2869 *end_params = '\0';
2870
2871 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
2872
2873 /*
2874 * Parse the parameters in reverse order to only store the last value.
2875 * From the html form, the backend and the action are at the end.
2876 */
2877 while (cur_param > first_param) {
2878 char *value;
2879 int poffset, plen;
2880
2881 cur_param--;
2882
2883 if ((*cur_param == '&') || (cur_param == first_param)) {
2884 reprocess_servers:
2885 /* Parse the key */
2886 poffset = (cur_param != first_param ? 1 : 0);
2887 plen = next_param - cur_param + (cur_param == first_param ? 1 : 0);
2888 if ((plen > 0) && (plen <= sizeof(key))) {
2889 strncpy(key, cur_param + poffset, plen);
2890 key[plen - 1] = '\0';
2891 } else {
Christopher Faulet2f9a41d2019-02-27 15:30:57 +01002892 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
William Lallemand74c24fb2016-11-21 17:18:36 +01002893 goto out;
2894 }
2895
2896 /* Parse the value */
2897 value = key;
2898 while (*value != '\0' && *value != '=') {
2899 value++;
2900 }
2901 if (*value == '=') {
2902 /* Ok, a value is found, we can mark the end of the key */
2903 *value++ = '\0';
2904 }
Willy Tarreau62ba9ba2020-04-23 17:54:47 +02002905 if (url_decode(key, 1) < 0 || url_decode(value, 1) < 0)
William Lallemand74c24fb2016-11-21 17:18:36 +01002906 break;
2907
2908 /* Now we can check the key to see what to do */
2909 if (!px && (strcmp(key, "b") == 0)) {
2910 if ((px = proxy_be_by_name(value)) == NULL) {
2911 /* the backend name is unknown or ambiguous (duplicate names) */
2912 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
2913 goto out;
2914 }
2915 }
2916 else if (!action && (strcmp(key, "action") == 0)) {
2917 if (strcmp(value, "ready") == 0) {
2918 action = ST_ADM_ACTION_READY;
2919 }
2920 else if (strcmp(value, "drain") == 0) {
2921 action = ST_ADM_ACTION_DRAIN;
2922 }
2923 else if (strcmp(value, "maint") == 0) {
2924 action = ST_ADM_ACTION_MAINT;
2925 }
2926 else if (strcmp(value, "shutdown") == 0) {
2927 action = ST_ADM_ACTION_SHUTDOWN;
2928 }
2929 else if (strcmp(value, "dhlth") == 0) {
2930 action = ST_ADM_ACTION_DHLTH;
2931 }
2932 else if (strcmp(value, "ehlth") == 0) {
2933 action = ST_ADM_ACTION_EHLTH;
2934 }
2935 else if (strcmp(value, "hrunn") == 0) {
2936 action = ST_ADM_ACTION_HRUNN;
2937 }
2938 else if (strcmp(value, "hnolb") == 0) {
2939 action = ST_ADM_ACTION_HNOLB;
2940 }
2941 else if (strcmp(value, "hdown") == 0) {
2942 action = ST_ADM_ACTION_HDOWN;
2943 }
2944 else if (strcmp(value, "dagent") == 0) {
2945 action = ST_ADM_ACTION_DAGENT;
2946 }
2947 else if (strcmp(value, "eagent") == 0) {
2948 action = ST_ADM_ACTION_EAGENT;
2949 }
2950 else if (strcmp(value, "arunn") == 0) {
2951 action = ST_ADM_ACTION_ARUNN;
2952 }
2953 else if (strcmp(value, "adown") == 0) {
2954 action = ST_ADM_ACTION_ADOWN;
2955 }
2956 /* else these are the old supported methods */
2957 else if (strcmp(value, "disable") == 0) {
2958 action = ST_ADM_ACTION_DISABLE;
2959 }
2960 else if (strcmp(value, "enable") == 0) {
2961 action = ST_ADM_ACTION_ENABLE;
2962 }
2963 else if (strcmp(value, "stop") == 0) {
2964 action = ST_ADM_ACTION_STOP;
2965 }
2966 else if (strcmp(value, "start") == 0) {
2967 action = ST_ADM_ACTION_START;
2968 }
2969 else {
2970 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
2971 goto out;
2972 }
2973 }
2974 else if (strcmp(key, "s") == 0) {
2975 if (!(px && action)) {
2976 /*
2977 * Indicates that we'll need to reprocess the parameters
2978 * as soon as backend and action are known
2979 */
2980 if (!reprocess) {
2981 st_cur_param = cur_param;
2982 st_next_param = next_param;
2983 }
2984 reprocess = 1;
2985 }
2986 else if ((sv = findserver(px, value)) != NULL) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002987 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
William Lallemand74c24fb2016-11-21 17:18:36 +01002988 switch (action) {
2989 case ST_ADM_ACTION_DISABLE:
Emeric Brun52a91d32017-08-31 14:41:55 +02002990 if (!(sv->cur_admin & SRV_ADMF_FMAINT)) {
William Lallemand74c24fb2016-11-21 17:18:36 +01002991 altered_servers++;
2992 total_servers++;
2993 srv_set_admin_flag(sv, SRV_ADMF_FMAINT, "'disable' on stats page");
2994 }
2995 break;
2996 case ST_ADM_ACTION_ENABLE:
Emeric Brun52a91d32017-08-31 14:41:55 +02002997 if (sv->cur_admin & SRV_ADMF_FMAINT) {
William Lallemand74c24fb2016-11-21 17:18:36 +01002998 altered_servers++;
2999 total_servers++;
3000 srv_clr_admin_flag(sv, SRV_ADMF_FMAINT);
3001 }
3002 break;
3003 case ST_ADM_ACTION_STOP:
Emeric Brun52a91d32017-08-31 14:41:55 +02003004 if (!(sv->cur_admin & SRV_ADMF_FDRAIN)) {
William Lallemand74c24fb2016-11-21 17:18:36 +01003005 srv_set_admin_flag(sv, SRV_ADMF_FDRAIN, "'stop' on stats page");
3006 altered_servers++;
3007 total_servers++;
3008 }
3009 break;
3010 case ST_ADM_ACTION_START:
Emeric Brun52a91d32017-08-31 14:41:55 +02003011 if (sv->cur_admin & SRV_ADMF_FDRAIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +01003012 srv_clr_admin_flag(sv, SRV_ADMF_FDRAIN);
3013 altered_servers++;
3014 total_servers++;
3015 }
3016 break;
3017 case ST_ADM_ACTION_DHLTH:
3018 if (sv->check.state & CHK_ST_CONFIGURED) {
3019 sv->check.state &= ~CHK_ST_ENABLED;
3020 altered_servers++;
3021 total_servers++;
3022 }
3023 break;
3024 case ST_ADM_ACTION_EHLTH:
3025 if (sv->check.state & CHK_ST_CONFIGURED) {
3026 sv->check.state |= CHK_ST_ENABLED;
3027 altered_servers++;
3028 total_servers++;
3029 }
3030 break;
3031 case ST_ADM_ACTION_HRUNN:
3032 if (!(sv->track)) {
3033 sv->check.health = sv->check.rise + sv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02003034 srv_set_running(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01003035 altered_servers++;
3036 total_servers++;
3037 }
3038 break;
3039 case ST_ADM_ACTION_HNOLB:
3040 if (!(sv->track)) {
3041 sv->check.health = sv->check.rise + sv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02003042 srv_set_stopping(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01003043 altered_servers++;
3044 total_servers++;
3045 }
3046 break;
3047 case ST_ADM_ACTION_HDOWN:
3048 if (!(sv->track)) {
3049 sv->check.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02003050 srv_set_stopped(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01003051 altered_servers++;
3052 total_servers++;
3053 }
3054 break;
3055 case ST_ADM_ACTION_DAGENT:
3056 if (sv->agent.state & CHK_ST_CONFIGURED) {
3057 sv->agent.state &= ~CHK_ST_ENABLED;
3058 altered_servers++;
3059 total_servers++;
3060 }
3061 break;
3062 case ST_ADM_ACTION_EAGENT:
3063 if (sv->agent.state & CHK_ST_CONFIGURED) {
3064 sv->agent.state |= CHK_ST_ENABLED;
3065 altered_servers++;
3066 total_servers++;
3067 }
3068 break;
3069 case ST_ADM_ACTION_ARUNN:
3070 if (sv->agent.state & CHK_ST_ENABLED) {
3071 sv->agent.health = sv->agent.rise + sv->agent.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02003072 srv_set_running(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01003073 altered_servers++;
3074 total_servers++;
3075 }
3076 break;
3077 case ST_ADM_ACTION_ADOWN:
3078 if (sv->agent.state & CHK_ST_ENABLED) {
3079 sv->agent.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02003080 srv_set_stopped(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01003081 altered_servers++;
3082 total_servers++;
3083 }
3084 break;
3085 case ST_ADM_ACTION_READY:
3086 srv_adm_set_ready(sv);
3087 altered_servers++;
3088 total_servers++;
3089 break;
3090 case ST_ADM_ACTION_DRAIN:
3091 srv_adm_set_drain(sv);
3092 altered_servers++;
3093 total_servers++;
3094 break;
3095 case ST_ADM_ACTION_MAINT:
3096 srv_adm_set_maint(sv);
3097 altered_servers++;
3098 total_servers++;
3099 break;
3100 case ST_ADM_ACTION_SHUTDOWN:
3101 if (px->state != PR_STSTOPPED) {
Willy Tarreauaf7ea812019-11-14 16:42:04 +01003102 srv_shutdown_streams(sv, SF_ERR_KILLED);
William Lallemand74c24fb2016-11-21 17:18:36 +01003103 altered_servers++;
3104 total_servers++;
3105 }
3106 break;
3107 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003108 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
William Lallemand74c24fb2016-11-21 17:18:36 +01003109 } else {
3110 /* the server name is unknown or ambiguous (duplicate names) */
3111 total_servers++;
3112 }
3113 }
3114 if (reprocess && px && action) {
3115 /* Now, we know the backend and the action chosen by the user.
3116 * We can safely restart from the first server parameter
3117 * to reprocess them
3118 */
3119 cur_param = st_cur_param;
3120 next_param = st_next_param;
3121 reprocess = 0;
3122 goto reprocess_servers;
3123 }
3124
3125 next_param = cur_param;
3126 }
3127 }
3128
3129 if (total_servers == 0) {
3130 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
3131 }
3132 else if (altered_servers == 0) {
3133 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
3134 }
3135 else if (altered_servers == total_servers) {
3136 appctx->ctx.stats.st_code = STAT_STATUS_DONE;
3137 }
3138 else {
3139 appctx->ctx.stats.st_code = STAT_STATUS_PART;
3140 }
3141 out:
3142 return 1;
Christopher Faulet2f9a41d2019-02-27 15:30:57 +01003143 wait:
3144 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
3145 return 0;
Christopher Fauletef779222018-10-31 08:47:01 +01003146}
3147
3148
Christopher Fauletb7f88902019-07-15 21:56:43 +02003149static int stats_send_http_headers(struct stream_interface *si, struct htx *htx)
Christopher Fauletef779222018-10-31 08:47:01 +01003150{
3151 struct stream *s = si_strm(si);
3152 struct uri_auth *uri = s->be->uri_auth;
3153 struct appctx *appctx = __objt_appctx(si->end);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003154 struct htx_sl *sl;
3155 unsigned int flags;
Christopher Fauletef779222018-10-31 08:47:01 +01003156
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003157 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);
3158 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), ist("200"), ist("OK"));
3159 if (!sl)
Christopher Fauletef779222018-10-31 08:47:01 +01003160 goto full;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003161 sl->info.res.status = 200;
Christopher Fauletef779222018-10-31 08:47:01 +01003162
Christopher Fauletb829f4c2019-03-29 16:13:55 +01003163 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")))
Christopher Fauletef779222018-10-31 08:47:01 +01003164 goto full;
3165 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
3166 if (!htx_add_header(htx, ist("Content-Type"), ist("text/html")))
3167 goto full;
3168 }
Christopher Faulet6338a082019-09-09 15:50:54 +02003169 else if (appctx->ctx.stats.flags & (STAT_FMT_JSON|STAT_JSON_SCHM)) {
3170 if (!htx_add_header(htx, ist("Content-Type"), ist("application/json")))
3171 goto full;
3172 }
Christopher Fauletef779222018-10-31 08:47:01 +01003173 else {
3174 if (!htx_add_header(htx, ist("Content-Type"), ist("text/plain")))
3175 goto full;
3176 }
3177
3178 if (uri->refresh > 0 && !(appctx->ctx.stats.flags & STAT_NO_REFRESH)) {
3179 const char *refresh = U2A(uri->refresh);
3180 if (!htx_add_header(htx, ist("Refresh"), ist2(refresh, strlen(refresh))))
3181 goto full;
3182 }
3183
3184 if (appctx->ctx.stats.flags & STAT_CHUNKED) {
3185 if (!htx_add_header(htx, ist("Transfer-Encoding"), ist("chunked")))
3186 goto full;
3187 }
3188
3189 if (!htx_add_endof(htx, HTX_BLK_EOH))
3190 goto full;
3191
Christopher Faulet1e2d6362019-02-27 16:28:48 +01003192 channel_add_input(&s->res, htx->data);
Christopher Fauletef779222018-10-31 08:47:01 +01003193 return 1;
3194
3195 full:
3196 htx_reset(htx);
3197 si_rx_room_blk(si);
3198 return 0;
William Lallemand74c24fb2016-11-21 17:18:36 +01003199}
3200
Christopher Fauletef779222018-10-31 08:47:01 +01003201
Christopher Fauletb7f88902019-07-15 21:56:43 +02003202static int stats_send_http_redirect(struct stream_interface *si, struct htx *htx)
Christopher Fauletef779222018-10-31 08:47:01 +01003203{
3204 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
3205 struct stream *s = si_strm(si);
3206 struct uri_auth *uri = s->be->uri_auth;
3207 struct appctx *appctx = __objt_appctx(si->end);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003208 struct htx_sl *sl;
3209 unsigned int flags;
Christopher Fauletef779222018-10-31 08:47:01 +01003210
3211 /* scope_txt = search pattern + search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
3212 scope_txt[0] = 0;
3213 if (appctx->ctx.stats.scope_len) {
Christopher Fauleted7a0662019-01-14 11:07:34 +01003214 const char *scope_ptr = stats_scope_ptr(appctx, si);
3215
Christopher Fauletef779222018-10-31 08:47:01 +01003216 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Christopher Fauleted7a0662019-01-14 11:07:34 +01003217 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), scope_ptr, appctx->ctx.stats.scope_len);
Christopher Fauletef779222018-10-31 08:47:01 +01003218 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
3219 }
3220
3221 /* We don't want to land on the posted stats page because a refresh will
3222 * repost the data. We don't want this to happen on accident so we redirect
3223 * the browse to the stats page with a GET.
3224 */
3225 chunk_printf(&trash, "%s;st=%s%s%s%s",
3226 uri->uri_prefix,
3227 ((appctx->ctx.stats.st_code > STAT_STATUS_INIT) &&
3228 (appctx->ctx.stats.st_code < STAT_STATUS_SIZE) &&
3229 stat_status_codes[appctx->ctx.stats.st_code]) ?
3230 stat_status_codes[appctx->ctx.stats.st_code] :
3231 stat_status_codes[STAT_STATUS_UNKN],
3232 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3233 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
3234 scope_txt);
3235
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003236 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_CHNK);
3237 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), ist("303"), ist("See Other"));
3238 if (!sl)
Christopher Fauletef779222018-10-31 08:47:01 +01003239 goto full;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003240 sl->info.res.status = 303;
Christopher Fauletef779222018-10-31 08:47:01 +01003241
3242 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")) ||
Christopher Fauletef779222018-10-31 08:47:01 +01003243 !htx_add_header(htx, ist("Content-Type"), ist("text/plain")) ||
3244 !htx_add_header(htx, ist("Content-Length"), ist("0")) ||
3245 !htx_add_header(htx, ist("Location"), ist2(trash.area, trash.data)))
3246 goto full;
3247
3248 if (!htx_add_endof(htx, HTX_BLK_EOH))
3249 goto full;
3250
Christopher Faulet1e2d6362019-02-27 16:28:48 +01003251 channel_add_input(&s->res, htx->data);
Christopher Fauletef779222018-10-31 08:47:01 +01003252 return 1;
3253
3254full:
3255 htx_reset(htx);
3256 si_rx_room_blk(si);
3257 return 0;
3258}
William Lallemand74c24fb2016-11-21 17:18:36 +01003259
Simon Horman05ee2132017-01-04 09:37:25 +01003260
William Lallemand74c24fb2016-11-21 17:18:36 +01003261/* This I/O handler runs as an applet embedded in a stream interface. It is
3262 * used to send HTTP stats over a TCP socket. The mechanism is very simple.
3263 * appctx->st0 contains the operation in progress (dump, done). The handler
3264 * automatically unregisters itself once transfer is complete.
3265 */
Christopher Fauletb7f88902019-07-15 21:56:43 +02003266static void http_stats_io_handler(struct appctx *appctx)
Christopher Fauletef779222018-10-31 08:47:01 +01003267{
3268 struct stream_interface *si = appctx->owner;
3269 struct stream *s = si_strm(si);
3270 struct channel *req = si_oc(si);
3271 struct channel *res = si_ic(si);
3272 struct htx *req_htx, *res_htx;
3273
3274 res_htx = htx_from_buf(&res->buf);
3275
3276 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
3277 goto out;
3278
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05003279 /* Check if the input buffer is available. */
Christopher Fauletef779222018-10-31 08:47:01 +01003280 if (!b_size(&res->buf)) {
3281 si_rx_room_blk(si);
3282 goto out;
3283 }
3284
3285 /* check that the output is not closed */
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003286 if (res->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_SHUTR))
3287 appctx->st0 = STAT_HTTP_END;
Christopher Fauletef779222018-10-31 08:47:01 +01003288
3289 /* all states are processed in sequence */
3290 if (appctx->st0 == STAT_HTTP_HEAD) {
Christopher Fauletb7f88902019-07-15 21:56:43 +02003291 if (stats_send_http_headers(si, res_htx)) {
Christopher Fauletef779222018-10-31 08:47:01 +01003292 if (s->txn->meth == HTTP_METH_HEAD)
3293 appctx->st0 = STAT_HTTP_DONE;
3294 else
3295 appctx->st0 = STAT_HTTP_DUMP;
3296 }
3297 }
3298
3299 if (appctx->st0 == STAT_HTTP_DUMP) {
3300 if (stats_dump_stat_to_buffer(si, res_htx, s->be->uri_auth))
3301 appctx->st0 = STAT_HTTP_DONE;
3302 }
3303
3304 if (appctx->st0 == STAT_HTTP_POST) {
3305 if (stats_process_http_post(si))
3306 appctx->st0 = STAT_HTTP_LAST;
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003307 else if (req->flags & CF_SHUTR)
Christopher Fauletef779222018-10-31 08:47:01 +01003308 appctx->st0 = STAT_HTTP_DONE;
3309 }
3310
3311 if (appctx->st0 == STAT_HTTP_LAST) {
Christopher Fauletb7f88902019-07-15 21:56:43 +02003312 if (stats_send_http_redirect(si, res_htx))
Christopher Fauletef779222018-10-31 08:47:01 +01003313 appctx->st0 = STAT_HTTP_DONE;
3314 }
3315
3316 if (appctx->st0 == STAT_HTTP_DONE) {
Christopher Faulet54b5e212019-06-04 10:08:28 +02003317 /* Don't add TLR because mux-h1 will take care of it */
Christopher Fauletef779222018-10-31 08:47:01 +01003318 if (!htx_add_endof(res_htx, HTX_BLK_EOM)) {
3319 si_rx_room_blk(si);
3320 goto out;
3321 }
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003322 channel_add_input(&s->res, 1);
3323 appctx->st0 = STAT_HTTP_END;
Christopher Fauletef779222018-10-31 08:47:01 +01003324 }
3325
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003326 if (appctx->st0 == STAT_HTTP_END) {
3327 if (!(res->flags & CF_SHUTR)) {
Christopher Fauletef779222018-10-31 08:47:01 +01003328 res->flags |= CF_READ_NULL;
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003329 si_shutr(si);
3330 }
3331
3332 /* eat the whole request */
3333 if (co_data(req)) {
3334 req_htx = htx_from_buf(&req->buf);
3335 co_htx_skip(req, req_htx, co_data(req));
3336 htx_to_buf(req_htx, &req->buf);
Christopher Fauletef779222018-10-31 08:47:01 +01003337 }
3338 }
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003339
Christopher Fauletef779222018-10-31 08:47:01 +01003340 out:
3341 /* we have left the request in the buffer for the case where we
3342 * process a POST, and this automatically re-enables activity on
3343 * read. It's better to indicate that we want to stop reading when
3344 * we're sending, so that we know there's at most one direction
3345 * deciding to wake the applet up. It saves it from looping when
3346 * emitting large blocks into small TCP windows.
3347 */
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003348 htx_to_buf(res_htx, &res->buf);
3349 if (!channel_is_empty(res))
Christopher Fauletef779222018-10-31 08:47:01 +01003350 si_stop_get(si);
Christopher Fauletef779222018-10-31 08:47:01 +01003351}
3352
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003353/* Dump all fields from <info> into <out> using the "show info" format (name: value) */
Willy Tarreau83061a82018-07-13 11:56:34 +02003354static int stats_dump_info_fields(struct buffer *out,
Willy Tarreau43241ff2019-10-09 11:27:51 +02003355 const struct field *info, unsigned int flags)
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003356{
3357 int field;
3358
3359 for (field = 0; field < INF_TOTAL_FIELDS; field++) {
3360 if (!field_format(info, field))
3361 continue;
3362
Willy Tarreaueaa55372019-10-09 07:39:11 +02003363 if (!chunk_appendf(out, "%s: ", info_fields[field].name))
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003364 return 0;
3365 if (!stats_emit_raw_data_field(out, &info[field]))
3366 return 0;
Willy Tarreau6b19b142019-10-09 15:44:21 +02003367 if ((flags & STAT_SHOW_FDESC) && !chunk_appendf(out, ":\"%s\"", info_fields[field].desc))
3368 return 0;
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003369 if (!chunk_strcat(out, "\n"))
3370 return 0;
3371 }
3372 return 1;
3373}
3374
3375/* Dump all fields from <info> into <out> using the "show info typed" format */
Willy Tarreau83061a82018-07-13 11:56:34 +02003376static int stats_dump_typed_info_fields(struct buffer *out,
Willy Tarreau43241ff2019-10-09 11:27:51 +02003377 const struct field *info, unsigned int flags)
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003378{
3379 int field;
3380
3381 for (field = 0; field < INF_TOTAL_FIELDS; field++) {
3382 if (!field_format(info, field))
3383 continue;
3384
Willy Tarreaueaa55372019-10-09 07:39:11 +02003385 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 +01003386 return 0;
3387 if (!stats_emit_field_tags(out, &info[field], ':'))
3388 return 0;
3389 if (!stats_emit_typed_data_field(out, &info[field]))
3390 return 0;
Willy Tarreau6b19b142019-10-09 15:44:21 +02003391 if ((flags & STAT_SHOW_FDESC) && !chunk_appendf(out, ":\"%s\"", info_fields[field].desc))
3392 return 0;
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003393 if (!chunk_strcat(out, "\n"))
3394 return 0;
3395 }
3396 return 1;
3397}
3398
3399/* Fill <info> with HAProxy global info. <info> is preallocated
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05003400 * array of length <len>. The length of the array must be
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003401 * INF_TOTAL_FIELDS. If this length is less then this value, the
3402 * function returns 0, otherwise, it returns 1.
3403 */
3404int stats_fill_info(struct field *info, int len)
3405{
3406 unsigned int up = (now.tv_sec - start_date.tv_sec);
Willy Tarreau83061a82018-07-13 11:56:34 +02003407 struct buffer *out = get_trash_chunk();
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003408
3409#ifdef USE_OPENSSL
3410 int ssl_sess_rate = read_freq_ctr(&global.ssl_per_sec);
3411 int ssl_key_rate = read_freq_ctr(&global.ssl_fe_keys_per_sec);
3412 int ssl_reuse = 0;
3413
3414 if (ssl_key_rate < ssl_sess_rate) {
3415 /* count the ssl reuse ratio and avoid overflows in both directions */
3416 ssl_reuse = 100 - (100 * ssl_key_rate + (ssl_sess_rate - 1) / 2) / ssl_sess_rate;
3417 }
3418#endif
3419
3420 if (len < INF_TOTAL_FIELDS)
3421 return 0;
3422
3423 chunk_reset(out);
3424 memset(info, 0, sizeof(*info) * len);
3425
3426 info[INF_NAME] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, PRODUCT_NAME);
Willy Tarreau909b9d82019-01-04 18:20:32 +01003427 info[INF_VERSION] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, haproxy_version);
3428 info[INF_RELEASE_DATE] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, haproxy_date);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003429
Yves Lafon95317282018-02-26 11:10:37 +01003430 info[INF_NBTHREAD] = mkf_u32(FO_CONFIG|FS_SERVICE, global.nbthread);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003431 info[INF_NBPROC] = mkf_u32(FO_CONFIG|FS_SERVICE, global.nbproc);
3432 info[INF_PROCESS_NUM] = mkf_u32(FO_KEY, relative_pid);
3433 info[INF_PID] = mkf_u32(FO_STATUS, pid);
3434
3435 info[INF_UPTIME] = mkf_str(FN_DURATION, chunk_newstr(out));
3436 chunk_appendf(out, "%ud %uh%02um%02us", up / 86400, (up % 86400) / 3600, (up % 3600) / 60, (up % 60));
3437
3438 info[INF_UPTIME_SEC] = mkf_u32(FN_DURATION, up);
3439 info[INF_MEMMAX_MB] = mkf_u32(FO_CONFIG|FN_LIMIT, global.rlimit_memmax);
3440 info[INF_POOL_ALLOC_MB] = mkf_u32(0, (unsigned)(pool_total_allocated() / 1048576L));
3441 info[INF_POOL_USED_MB] = mkf_u32(0, (unsigned)(pool_total_used() / 1048576L));
3442 info[INF_POOL_FAILED] = mkf_u32(FN_COUNTER, pool_total_failures());
3443 info[INF_ULIMIT_N] = mkf_u32(FO_CONFIG|FN_LIMIT, global.rlimit_nofile);
3444 info[INF_MAXSOCK] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxsock);
3445 info[INF_MAXCONN] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxconn);
3446 info[INF_HARD_MAXCONN] = mkf_u32(FO_CONFIG|FN_LIMIT, global.hardmaxconn);
3447 info[INF_CURR_CONN] = mkf_u32(0, actconn);
3448 info[INF_CUM_CONN] = mkf_u32(FN_COUNTER, totalconn);
3449 info[INF_CUM_REQ] = mkf_u32(FN_COUNTER, global.req_count);
3450#ifdef USE_OPENSSL
3451 info[INF_MAX_SSL_CONNS] = mkf_u32(FN_MAX, global.maxsslconn);
3452 info[INF_CURR_SSL_CONNS] = mkf_u32(0, sslconns);
3453 info[INF_CUM_SSL_CONNS] = mkf_u32(FN_COUNTER, totalsslconns);
3454#endif
3455 info[INF_MAXPIPES] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxpipes);
3456 info[INF_PIPES_USED] = mkf_u32(0, pipes_used);
3457 info[INF_PIPES_FREE] = mkf_u32(0, pipes_free);
3458 info[INF_CONN_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&global.conn_per_sec));
3459 info[INF_CONN_RATE_LIMIT] = mkf_u32(FO_CONFIG|FN_LIMIT, global.cps_lim);
3460 info[INF_MAX_CONN_RATE] = mkf_u32(FN_MAX, global.cps_max);
3461 info[INF_SESS_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&global.sess_per_sec));
3462 info[INF_SESS_RATE_LIMIT] = mkf_u32(FO_CONFIG|FN_LIMIT, global.sps_lim);
3463 info[INF_MAX_SESS_RATE] = mkf_u32(FN_RATE, global.sps_max);
3464
3465#ifdef USE_OPENSSL
3466 info[INF_SSL_RATE] = mkf_u32(FN_RATE, ssl_sess_rate);
3467 info[INF_SSL_RATE_LIMIT] = mkf_u32(FO_CONFIG|FN_LIMIT, global.ssl_lim);
3468 info[INF_MAX_SSL_RATE] = mkf_u32(FN_MAX, global.ssl_max);
3469 info[INF_SSL_FRONTEND_KEY_RATE] = mkf_u32(0, ssl_key_rate);
3470 info[INF_SSL_FRONTEND_MAX_KEY_RATE] = mkf_u32(FN_MAX, global.ssl_fe_keys_max);
3471 info[INF_SSL_FRONTEND_SESSION_REUSE_PCT] = mkf_u32(0, ssl_reuse);
3472 info[INF_SSL_BACKEND_KEY_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&global.ssl_be_keys_per_sec));
3473 info[INF_SSL_BACKEND_MAX_KEY_RATE] = mkf_u32(FN_MAX, global.ssl_be_keys_max);
3474 info[INF_SSL_CACHE_LOOKUPS] = mkf_u32(FN_COUNTER, global.shctx_lookups);
3475 info[INF_SSL_CACHE_MISSES] = mkf_u32(FN_COUNTER, global.shctx_misses);
3476#endif
3477 info[INF_COMPRESS_BPS_IN] = mkf_u32(FN_RATE, read_freq_ctr(&global.comp_bps_in));
3478 info[INF_COMPRESS_BPS_OUT] = mkf_u32(FN_RATE, read_freq_ctr(&global.comp_bps_out));
3479 info[INF_COMPRESS_BPS_RATE_LIM] = mkf_u32(FO_CONFIG|FN_LIMIT, global.comp_rate_lim);
3480#ifdef USE_ZLIB
3481 info[INF_ZLIB_MEM_USAGE] = mkf_u32(0, zlib_used_memory);
3482 info[INF_MAX_ZLIB_MEM_USAGE] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxzlibmem);
3483#endif
3484 info[INF_TASKS] = mkf_u32(0, nb_tasks_cur);
Christopher Faulet34c5cc92016-12-06 09:15:30 +01003485 info[INF_RUN_QUEUE] = mkf_u32(0, tasks_run_queue_cur);
Willy Tarreau81036f22019-05-20 19:24:50 +02003486 info[INF_IDLE_PCT] = mkf_u32(FN_AVG, ti->idle_pct);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003487 info[INF_NODE] = mkf_str(FO_CONFIG|FN_OUTPUT|FS_SERVICE, global.node);
3488 if (global.desc)
3489 info[INF_DESCRIPTION] = mkf_str(FO_CONFIG|FN_OUTPUT|FS_SERVICE, global.desc);
Willy Tarreau00098ea2018-11-05 14:38:13 +01003490 info[INF_STOPPING] = mkf_u32(0, stopping);
3491 info[INF_JOBS] = mkf_u32(0, jobs);
William Lallemanda7199262018-11-16 16:57:20 +01003492 info[INF_UNSTOPPABLE_JOBS] = mkf_u32(0, unstoppable_jobs);
Willy Tarreau00098ea2018-11-05 14:38:13 +01003493 info[INF_LISTENERS] = mkf_u32(0, listeners);
Willy Tarreau199ad242018-11-05 16:31:22 +01003494 info[INF_ACTIVE_PEERS] = mkf_u32(0, active_peers);
Willy Tarreau2d372c22018-11-05 17:12:27 +01003495 info[INF_CONNECTED_PEERS] = mkf_u32(0, connected_peers);
Willy Tarreau13ef7732018-11-12 07:25:28 +01003496 info[INF_DROPPED_LOGS] = mkf_u32(0, dropped_logs);
Willy Tarreaubeb859a2018-11-22 18:07:59 +01003497 info[INF_BUSY_POLLING] = mkf_u32(0, !!(global.tune.options & GTUNE_BUSY_POLLING));
Baptiste Assmann333939c2019-01-21 08:34:50 +01003498 info[INF_FAILED_RESOLUTIONS] = mkf_u32(0, dns_failed_resolutions);
Willy Tarreau7cf0e452019-05-23 11:39:14 +02003499 info[INF_TOTAL_BYTES_OUT] = mkf_u64(0, global.out_bytes);
3500 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 +02003501 info[INF_DEBUG_COMMANDS_ISSUED] = mkf_u32(0, debug_commands_issued);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003502
3503 return 1;
3504}
3505
3506/* This function dumps information onto the stream interface's read buffer.
3507 * It returns 0 as long as it does not complete, non-zero upon completion.
3508 * No state is used.
3509 */
3510static int stats_dump_info_to_buffer(struct stream_interface *si)
3511{
3512 struct appctx *appctx = __objt_appctx(si->end);
3513
3514 if (!stats_fill_info(info, INF_TOTAL_FIELDS))
3515 return 0;
3516
3517 chunk_reset(&trash);
3518
3519 if (appctx->ctx.stats.flags & STAT_FMT_TYPED)
Willy Tarreau43241ff2019-10-09 11:27:51 +02003520 stats_dump_typed_info_fields(&trash, info, appctx->ctx.stats.flags);
Simon Horman05ee2132017-01-04 09:37:25 +01003521 else if (appctx->ctx.stats.flags & STAT_FMT_JSON)
Willy Tarreau43241ff2019-10-09 11:27:51 +02003522 stats_dump_json_info_fields(&trash, info, appctx->ctx.stats.flags);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003523 else
Willy Tarreau43241ff2019-10-09 11:27:51 +02003524 stats_dump_info_fields(&trash, info, appctx->ctx.stats.flags);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003525
Willy Tarreau06d80a92017-10-19 14:32:15 +02003526 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01003527 si_rx_room_blk(si);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003528 return 0;
3529 }
3530
3531 return 1;
3532}
3533
Simon Horman6f6bb382017-01-04 09:37:26 +01003534/* This function dumps the schema onto the stream interface's read buffer.
3535 * It returns 0 as long as it does not complete, non-zero upon completion.
3536 * No state is used.
3537 *
3538 * Integer values bouned to the range [-(2**53)+1, (2**53)-1] as
3539 * per the recommendation for interoperable integers in section 6 of RFC 7159.
3540 */
Willy Tarreau83061a82018-07-13 11:56:34 +02003541static void stats_dump_json_schema(struct buffer *out)
Simon Horman6f6bb382017-01-04 09:37:26 +01003542{
3543
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003544 int old_len = out->data;
Simon Horman6f6bb382017-01-04 09:37:26 +01003545
3546 chunk_strcat(out,
3547 "{"
3548 "\"$schema\":\"http://json-schema.org/draft-04/schema#\","
3549 "\"oneOf\":["
3550 "{"
3551 "\"title\":\"Info\","
3552 "\"type\":\"array\","
3553 "\"items\":{"
3554 "\"properties\":{"
3555 "\"title\":\"InfoItem\","
3556 "\"type\":\"object\","
3557 "\"field\":{\"$ref\":\"#/definitions/field\"},"
3558 "\"processNum\":{\"$ref\":\"#/definitions/processNum\"},"
3559 "\"tags\":{\"$ref\":\"#/definitions/tags\"},"
3560 "\"value\":{\"$ref\":\"#/definitions/typedValue\"}"
3561 "},"
3562 "\"required\":[\"field\",\"processNum\",\"tags\","
3563 "\"value\"]"
3564 "}"
3565 "},"
3566 "{"
3567 "\"title\":\"Stat\","
3568 "\"type\":\"array\","
3569 "\"items\":{"
3570 "\"title\":\"InfoItem\","
3571 "\"type\":\"object\","
3572 "\"properties\":{"
3573 "\"objType\":{"
3574 "\"enum\":[\"Frontend\",\"Backend\",\"Listener\","
3575 "\"Server\",\"Unknown\"]"
3576 "},"
3577 "\"proxyId\":{"
3578 "\"type\":\"integer\","
3579 "\"minimum\":0"
3580 "},"
3581 "\"id\":{"
3582 "\"type\":\"integer\","
3583 "\"minimum\":0"
3584 "},"
3585 "\"field\":{\"$ref\":\"#/definitions/field\"},"
3586 "\"processNum\":{\"$ref\":\"#/definitions/processNum\"},"
3587 "\"tags\":{\"$ref\":\"#/definitions/tags\"},"
3588 "\"typedValue\":{\"$ref\":\"#/definitions/typedValue\"}"
3589 "},"
3590 "\"required\":[\"objType\",\"proxyId\",\"id\","
3591 "\"field\",\"processNum\",\"tags\","
3592 "\"value\"]"
3593 "}"
3594 "},"
3595 "{"
3596 "\"title\":\"Error\","
3597 "\"type\":\"object\","
3598 "\"properties\":{"
3599 "\"errorStr\":{"
3600 "\"type\":\"string\""
3601 "},"
3602 "\"required\":[\"errorStr\"]"
3603 "}"
3604 "}"
3605 "],"
3606 "\"definitions\":{"
3607 "\"field\":{"
3608 "\"type\":\"object\","
3609 "\"pos\":{"
3610 "\"type\":\"integer\","
3611 "\"minimum\":0"
3612 "},"
3613 "\"name\":{"
3614 "\"type\":\"string\""
3615 "},"
3616 "\"required\":[\"pos\",\"name\"]"
3617 "},"
3618 "\"processNum\":{"
3619 "\"type\":\"integer\","
3620 "\"minimum\":1"
3621 "},"
3622 "\"tags\":{"
3623 "\"type\":\"object\","
3624 "\"origin\":{"
3625 "\"type\":\"string\","
3626 "\"enum\":[\"Metric\",\"Status\",\"Key\","
3627 "\"Config\",\"Product\",\"Unknown\"]"
3628 "},"
3629 "\"nature\":{"
3630 "\"type\":\"string\","
3631 "\"enum\":[\"Gauge\",\"Limit\",\"Min\",\"Max\","
3632 "\"Rate\",\"Counter\",\"Duration\","
3633 "\"Age\",\"Time\",\"Name\",\"Output\","
3634 "\"Avg\", \"Unknown\"]"
3635 "},"
3636 "\"scope\":{"
3637 "\"type\":\"string\","
3638 "\"enum\":[\"Cluster\",\"Process\",\"Service\","
3639 "\"System\",\"Unknown\"]"
3640 "},"
3641 "\"required\":[\"origin\",\"nature\",\"scope\"]"
3642 "},"
3643 "\"typedValue\":{"
3644 "\"type\":\"object\","
3645 "\"oneOf\":["
3646 "{\"$ref\":\"#/definitions/typedValue/definitions/s32Value\"},"
3647 "{\"$ref\":\"#/definitions/typedValue/definitions/s64Value\"},"
3648 "{\"$ref\":\"#/definitions/typedValue/definitions/u32Value\"},"
3649 "{\"$ref\":\"#/definitions/typedValue/definitions/u64Value\"},"
3650 "{\"$ref\":\"#/definitions/typedValue/definitions/strValue\"}"
3651 "],"
3652 "\"definitions\":{"
3653 "\"s32Value\":{"
3654 "\"properties\":{"
3655 "\"type\":{"
3656 "\"type\":\"string\","
3657 "\"enum\":[\"s32\"]"
3658 "},"
3659 "\"value\":{"
3660 "\"type\":\"integer\","
3661 "\"minimum\":-2147483648,"
3662 "\"maximum\":2147483647"
3663 "}"
3664 "},"
3665 "\"required\":[\"type\",\"value\"]"
3666 "},"
3667 "\"s64Value\":{"
3668 "\"properties\":{"
3669 "\"type\":{"
3670 "\"type\":\"string\","
3671 "\"enum\":[\"s64\"]"
3672 "},"
3673 "\"value\":{"
3674 "\"type\":\"integer\","
3675 "\"minimum\":-9007199254740991,"
3676 "\"maximum\":9007199254740991"
3677 "}"
3678 "},"
3679 "\"required\":[\"type\",\"value\"]"
3680 "},"
3681 "\"u32Value\":{"
3682 "\"properties\":{"
3683 "\"type\":{"
3684 "\"type\":\"string\","
3685 "\"enum\":[\"u32\"]"
3686 "},"
3687 "\"value\":{"
3688 "\"type\":\"integer\","
3689 "\"minimum\":0,"
3690 "\"maximum\":4294967295"
3691 "}"
3692 "},"
3693 "\"required\":[\"type\",\"value\"]"
3694 "},"
3695 "\"u64Value\":{"
3696 "\"properties\":{"
3697 "\"type\":{"
3698 "\"type\":\"string\","
3699 "\"enum\":[\"u64\"]"
3700 "},"
3701 "\"value\":{"
3702 "\"type\":\"integer\","
3703 "\"minimum\":0,"
3704 "\"maximum\":9007199254740991"
3705 "}"
3706 "},"
3707 "\"required\":[\"type\",\"value\"]"
3708 "},"
3709 "\"strValue\":{"
3710 "\"properties\":{"
3711 "\"type\":{"
3712 "\"type\":\"string\","
3713 "\"enum\":[\"str\"]"
3714 "},"
3715 "\"value\":{\"type\":\"string\"}"
3716 "},"
3717 "\"required\":[\"type\",\"value\"]"
3718 "},"
3719 "\"unknownValue\":{"
3720 "\"properties\":{"
3721 "\"type\":{"
3722 "\"type\":\"integer\","
3723 "\"minimum\":0"
3724 "},"
3725 "\"value\":{"
3726 "\"type\":\"string\","
3727 "\"enum\":[\"unknown\"]"
3728 "}"
3729 "},"
3730 "\"required\":[\"type\",\"value\"]"
3731 "}"
3732 "}"
3733 "}"
3734 "}"
3735 "}");
3736
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003737 if (old_len == out->data) {
Simon Horman6f6bb382017-01-04 09:37:26 +01003738 chunk_reset(out);
3739 chunk_appendf(out,
3740 "{\"errorStr\":\"output buffer too short\"}");
3741 }
3742}
3743
3744/* This function dumps the schema onto the stream interface's read buffer.
3745 * It returns 0 as long as it does not complete, non-zero upon completion.
3746 * No state is used.
3747 */
3748static int stats_dump_json_schema_to_buffer(struct stream_interface *si)
3749{
3750 chunk_reset(&trash);
3751
3752 stats_dump_json_schema(&trash);
3753
Willy Tarreau06d80a92017-10-19 14:32:15 +02003754 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01003755 si_rx_room_blk(si);
Simon Horman6f6bb382017-01-04 09:37:26 +01003756 return 0;
3757 }
3758
3759 return 1;
3760}
3761
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02003762static int cli_parse_clear_counters(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau89d467c2016-11-23 11:02:40 +01003763{
3764 struct proxy *px;
3765 struct server *sv;
3766 struct listener *li;
3767 int clrall = 0;
3768
3769 if (strcmp(args[2], "all") == 0)
3770 clrall = 1;
3771
3772 /* check permissions */
3773 if (!cli_has_level(appctx, ACCESS_LVL_OPER) ||
3774 (clrall && !cli_has_level(appctx, ACCESS_LVL_ADMIN)))
3775 return 1;
3776
Olivier Houchardfbc74e82017-11-24 16:54:05 +01003777 for (px = proxies_list; px; px = px->next) {
Willy Tarreau89d467c2016-11-23 11:02:40 +01003778 if (clrall) {
3779 memset(&px->be_counters, 0, sizeof(px->be_counters));
3780 memset(&px->fe_counters, 0, sizeof(px->fe_counters));
3781 }
3782 else {
3783 px->be_counters.conn_max = 0;
3784 px->be_counters.p.http.rps_max = 0;
3785 px->be_counters.sps_max = 0;
3786 px->be_counters.cps_max = 0;
3787 px->be_counters.nbpend_max = 0;
Christopher Fauletefb41f02019-11-08 14:53:15 +01003788 px->be_counters.qtime_max = 0;
3789 px->be_counters.ctime_max = 0;
3790 px->be_counters.dtime_max = 0;
3791 px->be_counters.ttime_max = 0;
Willy Tarreau89d467c2016-11-23 11:02:40 +01003792
3793 px->fe_counters.conn_max = 0;
3794 px->fe_counters.p.http.rps_max = 0;
3795 px->fe_counters.sps_max = 0;
3796 px->fe_counters.cps_max = 0;
Willy Tarreau89d467c2016-11-23 11:02:40 +01003797 }
3798
3799 for (sv = px->srv; sv; sv = sv->next)
3800 if (clrall)
3801 memset(&sv->counters, 0, sizeof(sv->counters));
3802 else {
3803 sv->counters.cur_sess_max = 0;
3804 sv->counters.nbpend_max = 0;
3805 sv->counters.sps_max = 0;
Christopher Fauletefb41f02019-11-08 14:53:15 +01003806 sv->counters.qtime_max = 0;
3807 sv->counters.ctime_max = 0;
3808 sv->counters.dtime_max = 0;
3809 sv->counters.ttime_max = 0;
Willy Tarreau89d467c2016-11-23 11:02:40 +01003810 }
3811
3812 list_for_each_entry(li, &px->conf.listeners, by_fe)
3813 if (li->counters) {
3814 if (clrall)
3815 memset(li->counters, 0, sizeof(*li->counters));
3816 else
3817 li->counters->conn_max = 0;
3818 }
3819 }
3820
3821 global.cps_max = 0;
3822 global.sps_max = 0;
Olivier Houchard00bc3cb2017-10-17 19:23:25 +02003823 global.ssl_max = 0;
3824 global.ssl_fe_keys_max = 0;
3825 global.ssl_be_keys_max = 0;
Willy Tarreaud80cb4e2018-01-20 19:30:13 +01003826
3827 memset(activity, 0, sizeof(activity));
Willy Tarreau89d467c2016-11-23 11:02:40 +01003828 return 1;
3829}
3830
3831
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02003832static int cli_parse_show_info(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003833{
Willy Tarreau2f397382019-10-09 11:43:59 +02003834 int arg = 2;
3835
Willy Tarreaud25fc792016-12-16 12:33:47 +01003836 appctx->ctx.stats.scope_str = 0;
3837 appctx->ctx.stats.scope_len = 0;
3838 appctx->ctx.stats.flags = 0;
3839
Willy Tarreau2f397382019-10-09 11:43:59 +02003840 while (*args[arg]) {
3841 if (strcmp(args[arg], "typed") == 0)
3842 appctx->ctx.stats.flags = (appctx->ctx.stats.flags & ~STAT_FMT_MASK) | STAT_FMT_TYPED;
3843 else if (strcmp(args[arg], "json") == 0)
3844 appctx->ctx.stats.flags = (appctx->ctx.stats.flags & ~STAT_FMT_MASK) | STAT_FMT_JSON;
3845 else if (strcmp(args[arg], "desc") == 0)
3846 appctx->ctx.stats.flags |= STAT_SHOW_FDESC;
3847 arg++;
3848 }
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003849 return 0;
3850}
3851
3852
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02003853static int cli_parse_show_stat(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau2b812e22016-11-22 16:18:05 +01003854{
Willy Tarreau2f397382019-10-09 11:43:59 +02003855 int arg = 2;
3856
Willy Tarreaud25fc792016-12-16 12:33:47 +01003857 appctx->ctx.stats.scope_str = 0;
3858 appctx->ctx.stats.scope_len = 0;
Willy Tarreau578d6e42019-10-09 11:00:22 +02003859 appctx->ctx.stats.flags = STAT_SHNODE | STAT_SHDESC;
3860
3861 if ((strm_li(si_strm(appctx->owner))->bind_conf->level & ACCESS_LVL_MASK) >= ACCESS_LVL_OPER)
3862 appctx->ctx.stats.flags |= STAT_SHLGNDS;
Willy Tarreaud25fc792016-12-16 12:33:47 +01003863
Willy Tarreau2f397382019-10-09 11:43:59 +02003864 if (*args[arg] && *args[arg+1] && *args[arg+2]) {
Willy Tarreaua1b1ed52016-11-25 08:50:58 +01003865 struct proxy *px;
3866
Willy Tarreau2f397382019-10-09 11:43:59 +02003867 px = proxy_find_by_name(args[arg], 0, 0);
Willy Tarreaua1b1ed52016-11-25 08:50:58 +01003868 if (px)
3869 appctx->ctx.stats.iid = px->uuid;
3870 else
Willy Tarreau2f397382019-10-09 11:43:59 +02003871 appctx->ctx.stats.iid = atoi(args[arg]);
Willy Tarreaua1b1ed52016-11-25 08:50:58 +01003872
Willy Tarreau9d008692019-08-09 11:21:01 +02003873 if (!appctx->ctx.stats.iid)
3874 return cli_err(appctx, "No such proxy.\n");
Willy Tarreaua1b1ed52016-11-25 08:50:58 +01003875
Willy Tarreau2b812e22016-11-22 16:18:05 +01003876 appctx->ctx.stats.flags |= STAT_BOUND;
Willy Tarreau2f397382019-10-09 11:43:59 +02003877 appctx->ctx.stats.type = atoi(args[arg+1]);
3878 appctx->ctx.stats.sid = atoi(args[arg+2]);
3879 arg += 3;
3880 }
3881
3882 while (*args[arg]) {
3883 if (strcmp(args[arg], "typed") == 0)
3884 appctx->ctx.stats.flags = (appctx->ctx.stats.flags & ~STAT_FMT_MASK) | STAT_FMT_TYPED;
3885 else if (strcmp(args[arg], "json") == 0)
3886 appctx->ctx.stats.flags = (appctx->ctx.stats.flags & ~STAT_FMT_MASK) | STAT_FMT_JSON;
3887 else if (strcmp(args[arg], "desc") == 0)
3888 appctx->ctx.stats.flags |= STAT_SHOW_FDESC;
3889 arg++;
Willy Tarreau2b812e22016-11-22 16:18:05 +01003890 }
Willy Tarreau2b812e22016-11-22 16:18:05 +01003891
Willy Tarreau2b812e22016-11-22 16:18:05 +01003892 return 0;
3893}
3894
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003895static int cli_io_handler_dump_info(struct appctx *appctx)
3896{
3897 return stats_dump_info_to_buffer(appctx->owner);
3898}
3899
Willy Tarreau2b812e22016-11-22 16:18:05 +01003900/* This I/O handler runs as an applet embedded in a stream interface. It is
3901 * used to send raw stats over a socket.
3902 */
3903static int cli_io_handler_dump_stat(struct appctx *appctx)
3904{
Christopher Fauletef779222018-10-31 08:47:01 +01003905 return stats_dump_stat_to_buffer(appctx->owner, NULL, NULL);
Willy Tarreau2b812e22016-11-22 16:18:05 +01003906}
3907
Simon Horman6f6bb382017-01-04 09:37:26 +01003908static int cli_io_handler_dump_json_schema(struct appctx *appctx)
3909{
3910 return stats_dump_json_schema_to_buffer(appctx->owner);
3911}
3912
Willy Tarreau2b812e22016-11-22 16:18:05 +01003913/* register cli keywords */
3914static struct cli_kw_list cli_kws = {{ },{
Willy Tarreau89d467c2016-11-23 11:02:40 +01003915 { { "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 +02003916 { { "show", "info", NULL }, "show info : report information about the running process [desc|json|typed]*", cli_parse_show_info, cli_io_handler_dump_info, NULL },
3917 { { "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 +01003918 { { "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 +01003919 {{},}
3920}};
3921
Willy Tarreau0108d902018-11-25 19:14:37 +01003922INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
3923
William Lallemand74c24fb2016-11-21 17:18:36 +01003924struct applet http_stats_applet = {
3925 .obj_type = OBJ_TYPE_APPLET,
3926 .name = "<STATS>", /* used for logging */
3927 .fct = http_stats_io_handler,
3928 .release = NULL,
3929};
3930
William Lallemand74c24fb2016-11-21 17:18:36 +01003931/*
3932 * Local variables:
3933 * c-indent-level: 8
3934 * c-basic-offset: 8
3935 * End:
3936 */