blob: 32236f4570d0796c76c3f56f43091b9d1e85c59b [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)" },
William Lallemand74c24fb2016-11-21 17:18:36 +0100253};
254
Willy Tarreau0baac8c2016-11-22 16:36:53 +0100255/* one line of info */
Christopher Faulet1bc04c72017-10-29 20:14:08 +0100256static THREAD_LOCAL struct field info[INF_TOTAL_FIELDS];
William Lallemand74c24fb2016-11-21 17:18:36 +0100257/* one line of stats */
Christopher Faulet1bc04c72017-10-29 20:14:08 +0100258static THREAD_LOCAL struct field stats[ST_F_TOTAL_FIELDS];
William Lallemand74c24fb2016-11-21 17:18:36 +0100259
Christopher Faulet6338a082019-09-09 15:50:54 +0200260static void stats_dump_json_schema(struct buffer *out);
William Lallemand74c24fb2016-11-21 17:18:36 +0100261
Christopher Fauletef779222018-10-31 08:47:01 +0100262static int stats_putchk(struct channel *chn, struct htx *htx, struct buffer *chk)
263{
264 if (htx) {
Christopher Faulet69fc88c2019-01-07 14:27:53 +0100265 if (chk->data >= channel_htx_recv_max(chn, htx))
266 return 0;
Willy Tarreau0a7ef022019-05-28 10:30:11 +0200267 if (!htx_add_data_atonce(htx, ist2(chk->area, chk->data)))
Christopher Fauletef779222018-10-31 08:47:01 +0100268 return 0;
Christopher Faulet5adbeeb2019-01-02 14:34:39 +0100269 channel_add_input(chn, chk->data);
Christopher Fauletef779222018-10-31 08:47:01 +0100270 chk->data = 0;
Christopher Fauletef779222018-10-31 08:47:01 +0100271 }
272 else {
273 if (ci_putchk(chn, chk) == -1)
274 return 0;
275 }
276 return 1;
277}
Willy Tarreau0baac8c2016-11-22 16:36:53 +0100278
Christopher Fauleted7a0662019-01-14 11:07:34 +0100279static const char *stats_scope_ptr(struct appctx *appctx, struct stream_interface *si)
280{
Christopher Fauletb7f88902019-07-15 21:56:43 +0200281 struct channel *req = si_oc(si);
282 struct htx *htx = htxbuf(&req->buf);
283 struct htx_blk *blk;
284 struct ist uri;
Christopher Fauleted7a0662019-01-14 11:07:34 +0100285
Christopher Fauletb7f88902019-07-15 21:56:43 +0200286 blk = htx_get_head_blk(htx);
Christopher Fauletea009732019-11-18 15:50:25 +0100287 BUG_ON(!blk || htx_get_blk_type(blk) != HTX_BLK_REQ_SL);
Christopher Fauletb7f88902019-07-15 21:56:43 +0200288 ALREADY_CHECKED(blk);
289 uri = htx_sl_req_uri(htx_get_blk_ptr(htx, blk));
290 return uri.ptr + appctx->ctx.stats.scope_str;
Christopher Fauleted7a0662019-01-14 11:07:34 +0100291}
292
William Lallemand74c24fb2016-11-21 17:18:36 +0100293/*
294 * http_stats_io_handler()
295 * -> stats_dump_stat_to_buffer() // same as above, but used for CSV or HTML
296 * -> stats_dump_csv_header() // emits the CSV headers (same as above)
Simon Horman05ee2132017-01-04 09:37:25 +0100297 * -> stats_dump_json_header() // emits the JSON headers (same as above)
William Lallemand74c24fb2016-11-21 17:18:36 +0100298 * -> stats_dump_html_head() // emits the HTML headers
299 * -> stats_dump_html_info() // emits the equivalent of "show info" at the top
300 * -> stats_dump_proxy_to_buffer() // same as above, valid for CSV and HTML
301 * -> stats_dump_html_px_hdr()
302 * -> stats_dump_fe_stats()
303 * -> stats_dump_li_stats()
304 * -> stats_dump_sv_stats()
305 * -> stats_dump_be_stats()
306 * -> stats_dump_html_px_end()
307 * -> stats_dump_html_end() // emits HTML trailer
Simon Horman05ee2132017-01-04 09:37:25 +0100308 * -> stats_dump_json_end() // emits JSON trailer
William Lallemand74c24fb2016-11-21 17:18:36 +0100309 */
310
311
William Lallemand74c24fb2016-11-21 17:18:36 +0100312/* Dumps the stats CSV header to the trash buffer which. The caller is responsible
313 * for clearing it if needed.
314 * NOTE: Some tools happen to rely on the field position instead of its name,
315 * so please only append new fields at the end, never in the middle.
316 */
317static void stats_dump_csv_header()
318{
319 int field;
320
321 chunk_appendf(&trash, "# ");
322 for (field = 0; field < ST_F_TOTAL_FIELDS; field++)
Willy Tarreaueaa55372019-10-09 07:39:11 +0200323 chunk_appendf(&trash, "%s,", stat_fields[field].name);
William Lallemand74c24fb2016-11-21 17:18:36 +0100324
325 chunk_appendf(&trash, "\n");
326}
327
328
329/* Emits a stats field without any surrounding element and properly encoded to
330 * resist CSV output. Returns non-zero on success, 0 if the buffer is full.
331 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200332int stats_emit_raw_data_field(struct buffer *out, const struct field *f)
William Lallemand74c24fb2016-11-21 17:18:36 +0100333{
334 switch (field_format(f, 0)) {
335 case FF_EMPTY: return 1;
336 case FF_S32: return chunk_appendf(out, "%d", f->u.s32);
337 case FF_U32: return chunk_appendf(out, "%u", f->u.u32);
338 case FF_S64: return chunk_appendf(out, "%lld", (long long)f->u.s64);
339 case FF_U64: return chunk_appendf(out, "%llu", (unsigned long long)f->u.u64);
Christopher Faulet88a0db22019-09-24 16:35:10 +0200340 case FF_FLT: return chunk_appendf(out, "%f", f->u.flt);
William Lallemand74c24fb2016-11-21 17:18:36 +0100341 case FF_STR: return csv_enc_append(field_str(f, 0), 1, out) != NULL;
342 default: return chunk_appendf(out, "[INCORRECT_FIELD_TYPE_%08x]", f->type);
343 }
344}
345
346/* Emits a stats field prefixed with its type. No CSV encoding is prepared, the
347 * output is supposed to be used on its own line. Returns non-zero on success, 0
348 * if the buffer is full.
349 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200350int stats_emit_typed_data_field(struct buffer *out, const struct field *f)
William Lallemand74c24fb2016-11-21 17:18:36 +0100351{
352 switch (field_format(f, 0)) {
353 case FF_EMPTY: return 1;
354 case FF_S32: return chunk_appendf(out, "s32:%d", f->u.s32);
355 case FF_U32: return chunk_appendf(out, "u32:%u", f->u.u32);
356 case FF_S64: return chunk_appendf(out, "s64:%lld", (long long)f->u.s64);
357 case FF_U64: return chunk_appendf(out, "u64:%llu", (unsigned long long)f->u.u64);
Christopher Faulet88a0db22019-09-24 16:35:10 +0200358 case FF_FLT: return chunk_appendf(out, "flt:%f", f->u.flt);
William Lallemand74c24fb2016-11-21 17:18:36 +0100359 case FF_STR: return chunk_appendf(out, "str:%s", field_str(f, 0));
360 default: return chunk_appendf(out, "%08x:?", f->type);
361 }
362}
363
Simon Horman05ee2132017-01-04 09:37:25 +0100364/* Limit JSON integer values to the range [-(2**53)+1, (2**53)-1] as per
365 * the recommendation for interoperable integers in section 6 of RFC 7159.
366 */
367#define JSON_INT_MAX ((1ULL << 53) - 1)
368#define JSON_INT_MIN (0 - JSON_INT_MAX)
369
370/* Emits a stats field value and its type in JSON.
371 * Returns non-zero on success, 0 on error.
372 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200373int stats_emit_json_data_field(struct buffer *out, const struct field *f)
Simon Horman05ee2132017-01-04 09:37:25 +0100374{
375 int old_len;
376 char buf[20];
377 const char *type, *value = buf, *quote = "";
378
379 switch (field_format(f, 0)) {
380 case FF_EMPTY: return 1;
381 case FF_S32: type = "\"s32\"";
382 snprintf(buf, sizeof(buf), "%d", f->u.s32);
383 break;
384 case FF_U32: type = "\"u32\"";
385 snprintf(buf, sizeof(buf), "%u", f->u.u32);
386 break;
387 case FF_S64: type = "\"s64\"";
388 if (f->u.s64 < JSON_INT_MIN || f->u.s64 > JSON_INT_MAX)
389 return 0;
390 type = "\"s64\"";
391 snprintf(buf, sizeof(buf), "%lld", (long long)f->u.s64);
392 break;
393 case FF_U64: if (f->u.u64 > JSON_INT_MAX)
394 return 0;
395 type = "\"u64\"";
396 snprintf(buf, sizeof(buf), "%llu",
397 (unsigned long long) f->u.u64);
Christopher Faulet52c91bb2019-09-28 10:37:31 +0200398 break;
Christopher Faulet88a0db22019-09-24 16:35:10 +0200399 case FF_FLT: type = "\"flt\"";
400 snprintf(buf, sizeof(buf), "%f", f->u.flt);
Simon Horman05ee2132017-01-04 09:37:25 +0100401 break;
402 case FF_STR: type = "\"str\"";
403 value = field_str(f, 0);
404 quote = "\"";
405 break;
406 default: snprintf(buf, sizeof(buf), "%u", f->type);
407 type = buf;
408 value = "unknown";
409 quote = "\"";
410 break;
411 }
412
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200413 old_len = out->data;
Simon Horman05ee2132017-01-04 09:37:25 +0100414 chunk_appendf(out, ",\"value\":{\"type\":%s,\"value\":%s%s%s}",
415 type, quote, value, quote);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200416 return !(old_len == out->data);
Simon Horman05ee2132017-01-04 09:37:25 +0100417}
418
William Lallemand74c24fb2016-11-21 17:18:36 +0100419/* Emits an encoding of the field type on 3 characters followed by a delimiter.
420 * Returns non-zero on success, 0 if the buffer is full.
421 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200422int stats_emit_field_tags(struct buffer *out, const struct field *f,
423 char delim)
William Lallemand74c24fb2016-11-21 17:18:36 +0100424{
425 char origin, nature, scope;
426
427 switch (field_origin(f, 0)) {
428 case FO_METRIC: origin = 'M'; break;
429 case FO_STATUS: origin = 'S'; break;
430 case FO_KEY: origin = 'K'; break;
431 case FO_CONFIG: origin = 'C'; break;
432 case FO_PRODUCT: origin = 'P'; break;
433 default: origin = '?'; break;
434 }
435
436 switch (field_nature(f, 0)) {
437 case FN_GAUGE: nature = 'G'; break;
438 case FN_LIMIT: nature = 'L'; break;
439 case FN_MIN: nature = 'm'; break;
440 case FN_MAX: nature = 'M'; break;
441 case FN_RATE: nature = 'R'; break;
442 case FN_COUNTER: nature = 'C'; break;
443 case FN_DURATION: nature = 'D'; break;
444 case FN_AGE: nature = 'A'; break;
445 case FN_TIME: nature = 'T'; break;
446 case FN_NAME: nature = 'N'; break;
447 case FN_OUTPUT: nature = 'O'; break;
448 case FN_AVG: nature = 'a'; break;
449 default: nature = '?'; break;
450 }
451
452 switch (field_scope(f, 0)) {
453 case FS_PROCESS: scope = 'P'; break;
454 case FS_SERVICE: scope = 'S'; break;
455 case FS_SYSTEM: scope = 's'; break;
456 case FS_CLUSTER: scope = 'C'; break;
457 default: scope = '?'; break;
458 }
459
460 return chunk_appendf(out, "%c%c%c%c", origin, nature, scope, delim);
461}
462
Simon Horman05ee2132017-01-04 09:37:25 +0100463/* Emits an encoding of the field type as JSON.
464 * Returns non-zero on success, 0 if the buffer is full.
465 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200466int stats_emit_json_field_tags(struct buffer *out, const struct field *f)
Simon Horman05ee2132017-01-04 09:37:25 +0100467{
468 const char *origin, *nature, *scope;
469 int old_len;
470
471 switch (field_origin(f, 0)) {
472 case FO_METRIC: origin = "Metric"; break;
473 case FO_STATUS: origin = "Status"; break;
474 case FO_KEY: origin = "Key"; break;
475 case FO_CONFIG: origin = "Config"; break;
476 case FO_PRODUCT: origin = "Product"; break;
477 default: origin = "Unknown"; break;
478 }
479
480 switch (field_nature(f, 0)) {
481 case FN_GAUGE: nature = "Gauge"; break;
482 case FN_LIMIT: nature = "Limit"; break;
483 case FN_MIN: nature = "Min"; break;
484 case FN_MAX: nature = "Max"; break;
485 case FN_RATE: nature = "Rate"; break;
486 case FN_COUNTER: nature = "Counter"; break;
487 case FN_DURATION: nature = "Duration"; break;
488 case FN_AGE: nature = "Age"; break;
489 case FN_TIME: nature = "Time"; break;
490 case FN_NAME: nature = "Name"; break;
491 case FN_OUTPUT: nature = "Output"; break;
492 case FN_AVG: nature = "Avg"; break;
493 default: nature = "Unknown"; break;
494 }
495
496 switch (field_scope(f, 0)) {
497 case FS_PROCESS: scope = "Process"; break;
498 case FS_SERVICE: scope = "Service"; break;
499 case FS_SYSTEM: scope = "System"; break;
500 case FS_CLUSTER: scope = "Cluster"; break;
501 default: scope = "Unknown"; break;
502 }
503
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200504 old_len = out->data;
Simon Horman05ee2132017-01-04 09:37:25 +0100505 chunk_appendf(out, "\"tags\":{"
506 "\"origin\":\"%s\","
507 "\"nature\":\"%s\","
508 "\"scope\":\"%s\""
509 "}", origin, nature, scope);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200510 return !(old_len == out->data);
Simon Horman05ee2132017-01-04 09:37:25 +0100511}
William Lallemand74c24fb2016-11-21 17:18:36 +0100512
513/* Dump all fields from <stats> into <out> using CSV format */
Willy Tarreau83061a82018-07-13 11:56:34 +0200514static int stats_dump_fields_csv(struct buffer *out,
Willy Tarreau43241ff2019-10-09 11:27:51 +0200515 const struct field *stats, unsigned int flags)
William Lallemand74c24fb2016-11-21 17:18:36 +0100516{
517 int field;
518
519 for (field = 0; field < ST_F_TOTAL_FIELDS; field++) {
520 if (!stats_emit_raw_data_field(out, &stats[field]))
521 return 0;
522 if (!chunk_strcat(out, ","))
523 return 0;
524 }
525 chunk_strcat(out, "\n");
526 return 1;
527}
528
529/* Dump all fields from <stats> into <out> using a typed "field:desc:type:value" format */
Willy Tarreau83061a82018-07-13 11:56:34 +0200530static int stats_dump_fields_typed(struct buffer *out,
Willy Tarreau43241ff2019-10-09 11:27:51 +0200531 const struct field *stats, unsigned int flags)
William Lallemand74c24fb2016-11-21 17:18:36 +0100532{
533 int field;
534
535 for (field = 0; field < ST_F_TOTAL_FIELDS; field++) {
536 if (!stats[field].type)
537 continue;
538
539 chunk_appendf(out, "%c.%u.%u.%d.%s.%u:",
540 stats[ST_F_TYPE].u.u32 == STATS_TYPE_FE ? 'F' :
541 stats[ST_F_TYPE].u.u32 == STATS_TYPE_BE ? 'B' :
542 stats[ST_F_TYPE].u.u32 == STATS_TYPE_SO ? 'L' :
543 stats[ST_F_TYPE].u.u32 == STATS_TYPE_SV ? 'S' :
544 '?',
545 stats[ST_F_IID].u.u32, stats[ST_F_SID].u.u32,
Willy Tarreaueaa55372019-10-09 07:39:11 +0200546 field, stat_fields[field].name, stats[ST_F_PID].u.u32);
William Lallemand74c24fb2016-11-21 17:18:36 +0100547
548 if (!stats_emit_field_tags(out, &stats[field], ':'))
549 return 0;
550 if (!stats_emit_typed_data_field(out, &stats[field]))
551 return 0;
Willy Tarreau6b19b142019-10-09 15:44:21 +0200552 if ((flags & STAT_SHOW_FDESC) && !chunk_appendf(out, ":\"%s\"", stat_fields[field].desc))
553 return 0;
William Lallemand74c24fb2016-11-21 17:18:36 +0100554 if (!chunk_strcat(out, "\n"))
555 return 0;
556 }
557 return 1;
558}
559
Simon Horman05ee2132017-01-04 09:37:25 +0100560/* Dump all fields from <stats> into <out> using the "show info json" format */
Willy Tarreau83061a82018-07-13 11:56:34 +0200561static int stats_dump_json_info_fields(struct buffer *out,
Willy Tarreau43241ff2019-10-09 11:27:51 +0200562 const struct field *info, unsigned int flags)
Simon Horman05ee2132017-01-04 09:37:25 +0100563{
564 int field;
565 int started = 0;
566
567 if (!chunk_strcat(out, "["))
568 return 0;
569
570 for (field = 0; field < INF_TOTAL_FIELDS; field++) {
571 int old_len;
572
573 if (!field_format(info, field))
574 continue;
575
576 if (started && !chunk_strcat(out, ","))
577 goto err;
578 started = 1;
579
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200580 old_len = out->data;
Simon Horman05ee2132017-01-04 09:37:25 +0100581 chunk_appendf(out,
582 "{\"field\":{\"pos\":%d,\"name\":\"%s\"},"
583 "\"processNum\":%u,",
Willy Tarreaueaa55372019-10-09 07:39:11 +0200584 field, info_fields[field].name,
Simon Horman05ee2132017-01-04 09:37:25 +0100585 info[INF_PROCESS_NUM].u.u32);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200586 if (old_len == out->data)
Simon Horman05ee2132017-01-04 09:37:25 +0100587 goto err;
588
589 if (!stats_emit_json_field_tags(out, &info[field]))
590 goto err;
591
592 if (!stats_emit_json_data_field(out, &info[field]))
593 goto err;
594
595 if (!chunk_strcat(out, "}"))
596 goto err;
597 }
598
599 if (!chunk_strcat(out, "]"))
600 goto err;
601 return 1;
602
603err:
604 chunk_reset(out);
605 chunk_appendf(out, "{\"errorStr\":\"output buffer too short\"}");
606 return 0;
607}
608
609/* Dump all fields from <stats> into <out> using a typed "field:desc:type:value" format */
Willy Tarreau83061a82018-07-13 11:56:34 +0200610static int stats_dump_fields_json(struct buffer *out,
611 const struct field *stats,
Willy Tarreaub0ce3ad2019-10-09 11:19:29 +0200612 unsigned int flags)
Simon Horman05ee2132017-01-04 09:37:25 +0100613{
614 int field;
615 int started = 0;
616
Willy Tarreaub0ce3ad2019-10-09 11:19:29 +0200617 if ((flags & STAT_STARTED) && !chunk_strcat(out, ","))
Simon Horman05ee2132017-01-04 09:37:25 +0100618 return 0;
619 if (!chunk_strcat(out, "["))
620 return 0;
621
622 for (field = 0; field < ST_F_TOTAL_FIELDS; field++) {
623 const char *obj_type;
624 int old_len;
625
626 if (!stats[field].type)
627 continue;
628
629 if (started && !chunk_strcat(out, ","))
630 goto err;
631 started = 1;
632
633 switch (stats[ST_F_TYPE].u.u32) {
634 case STATS_TYPE_FE: obj_type = "Frontend"; break;
635 case STATS_TYPE_BE: obj_type = "Backend"; break;
636 case STATS_TYPE_SO: obj_type = "Listener"; break;
637 case STATS_TYPE_SV: obj_type = "Server"; break;
638 default: obj_type = "Unknown"; break;
639 }
640
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200641 old_len = out->data;
Simon Horman05ee2132017-01-04 09:37:25 +0100642 chunk_appendf(out,
643 "{"
644 "\"objType\":\"%s\","
645 "\"proxyId\":%d,"
646 "\"id\":%d,"
647 "\"field\":{\"pos\":%d,\"name\":\"%s\"},"
648 "\"processNum\":%u,",
649 obj_type, stats[ST_F_IID].u.u32,
650 stats[ST_F_SID].u.u32, field,
Willy Tarreaueaa55372019-10-09 07:39:11 +0200651 stat_fields[field].name, stats[ST_F_PID].u.u32);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200652 if (old_len == out->data)
Simon Horman05ee2132017-01-04 09:37:25 +0100653 goto err;
654
655 if (!stats_emit_json_field_tags(out, &stats[field]))
656 goto err;
657
658 if (!stats_emit_json_data_field(out, &stats[field]))
659 goto err;
660
661 if (!chunk_strcat(out, "}"))
662 goto err;
663 }
664
665 if (!chunk_strcat(out, "]"))
666 goto err;
667
668 return 1;
669
670err:
671 chunk_reset(out);
Willy Tarreaub0ce3ad2019-10-09 11:19:29 +0200672 if (flags & STAT_STARTED)
673 chunk_strcat(out, ",");
Simon Horman05ee2132017-01-04 09:37:25 +0100674 chunk_appendf(out, "{\"errorStr\":\"output buffer too short\"}");
675 return 0;
676}
677
William Lallemand74c24fb2016-11-21 17:18:36 +0100678/* Dump all fields from <stats> into <out> using the HTML format. A column is
Willy Tarreauab02b3f2019-10-09 11:11:46 +0200679 * reserved for the checkbox is STAT_ADMIN is set in <flags>. Some extra info
Willy Tarreau708c4162019-10-09 10:19:16 +0200680 * are provided if STAT_SHLGNDS is present in <flags>.
William Lallemand74c24fb2016-11-21 17:18:36 +0100681 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200682static int stats_dump_fields_html(struct buffer *out,
683 const struct field *stats,
684 unsigned int flags)
William Lallemand74c24fb2016-11-21 17:18:36 +0100685{
Willy Tarreau83061a82018-07-13 11:56:34 +0200686 struct buffer src;
William Lallemand74c24fb2016-11-21 17:18:36 +0100687
688 if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_FE) {
689 chunk_appendf(out,
690 /* name, queue */
691 "<tr class=\"frontend\">");
692
Willy Tarreauab02b3f2019-10-09 11:11:46 +0200693 if (flags & STAT_ADMIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +0100694 /* Column sub-heading for Enable or Disable server */
695 chunk_appendf(out, "<td></td>");
696 }
697
698 chunk_appendf(out,
699 "<td class=ac>"
700 "<a name=\"%s/Frontend\"></a>"
701 "<a class=lfsb href=\"#%s/Frontend\">Frontend</a></td>"
702 "<td colspan=3></td>"
703 "",
704 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_PXNAME));
705
706 chunk_appendf(out,
707 /* sessions rate : current */
708 "<td><u>%s<div class=tips><table class=det>"
709 "<tr><th>Current connection rate:</th><td>%s/s</td></tr>"
710 "<tr><th>Current session rate:</th><td>%s/s</td></tr>"
711 "",
712 U2H(stats[ST_F_RATE].u.u32),
713 U2H(stats[ST_F_CONN_RATE].u.u32),
714 U2H(stats[ST_F_RATE].u.u32));
715
716 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
717 chunk_appendf(out,
718 "<tr><th>Current request rate:</th><td>%s/s</td></tr>",
719 U2H(stats[ST_F_REQ_RATE].u.u32));
720
721 chunk_appendf(out,
722 "</table></div></u></td>"
723 /* sessions rate : max */
724 "<td><u>%s<div class=tips><table class=det>"
725 "<tr><th>Max connection rate:</th><td>%s/s</td></tr>"
726 "<tr><th>Max session rate:</th><td>%s/s</td></tr>"
727 "",
728 U2H(stats[ST_F_RATE_MAX].u.u32),
729 U2H(stats[ST_F_CONN_RATE_MAX].u.u32),
730 U2H(stats[ST_F_RATE_MAX].u.u32));
731
732 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
733 chunk_appendf(out,
734 "<tr><th>Max request rate:</th><td>%s/s</td></tr>",
735 U2H(stats[ST_F_REQ_RATE_MAX].u.u32));
736
737 chunk_appendf(out,
738 "</table></div></u></td>"
739 /* sessions rate : limit */
740 "<td>%s</td>",
741 LIM2A(stats[ST_F_RATE_LIM].u.u32, "-"));
742
743 chunk_appendf(out,
744 /* sessions: current, max, limit, total */
745 "<td>%s</td><td>%s</td><td>%s</td>"
746 "<td><u>%s<div class=tips><table class=det>"
747 "<tr><th>Cum. connections:</th><td>%s</td></tr>"
748 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
749 "",
750 U2H(stats[ST_F_SCUR].u.u32), U2H(stats[ST_F_SMAX].u.u32), U2H(stats[ST_F_SLIM].u.u32),
751 U2H(stats[ST_F_STOT].u.u64),
752 U2H(stats[ST_F_CONN_TOT].u.u64),
753 U2H(stats[ST_F_STOT].u.u64));
754
755 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
756 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0) {
757 chunk_appendf(out,
758 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
759 "<tr><th>- HTTP 1xx responses:</th><td>%s</td></tr>"
760 "<tr><th>- HTTP 2xx responses:</th><td>%s</td></tr>"
761 "<tr><th>&nbsp;&nbsp;Compressed 2xx:</th><td>%s</td><td>(%d%%)</td></tr>"
762 "<tr><th>- HTTP 3xx responses:</th><td>%s</td></tr>"
763 "<tr><th>- HTTP 4xx responses:</th><td>%s</td></tr>"
764 "<tr><th>- HTTP 5xx responses:</th><td>%s</td></tr>"
765 "<tr><th>- other responses:</th><td>%s</td></tr>"
766 "<tr><th>Intercepted requests:</th><td>%s</td></tr>"
Willy Tarreaua1214a52018-12-14 14:00:25 +0100767 "<tr><th>Cache lookups:</th><td>%s</td></tr>"
768 "<tr><th>Cache hits:</th><td>%s</td><td>(%d%%)</td></tr>"
Willy Tarreau1b0f85e2018-05-28 15:12:40 +0200769 "<tr><th>Failed hdr rewrites:</th><td>%s</td></tr>"
William Lallemand74c24fb2016-11-21 17:18:36 +0100770 "",
771 U2H(stats[ST_F_REQ_TOT].u.u64),
772 U2H(stats[ST_F_HRSP_1XX].u.u64),
773 U2H(stats[ST_F_HRSP_2XX].u.u64),
774 U2H(stats[ST_F_COMP_RSP].u.u64),
775 stats[ST_F_HRSP_2XX].u.u64 ?
776 (int)(100 * stats[ST_F_COMP_RSP].u.u64 / stats[ST_F_HRSP_2XX].u.u64) : 0,
777 U2H(stats[ST_F_HRSP_3XX].u.u64),
778 U2H(stats[ST_F_HRSP_4XX].u.u64),
779 U2H(stats[ST_F_HRSP_5XX].u.u64),
780 U2H(stats[ST_F_HRSP_OTHER].u.u64),
Willy Tarreau1b0f85e2018-05-28 15:12:40 +0200781 U2H(stats[ST_F_INTERCEPTED].u.u64),
Willy Tarreaua1214a52018-12-14 14:00:25 +0100782 U2H(stats[ST_F_CACHE_LOOKUPS].u.u64),
783 U2H(stats[ST_F_CACHE_HITS].u.u64),
784 stats[ST_F_CACHE_LOOKUPS].u.u64 ?
785 (int)(100 * stats[ST_F_CACHE_HITS].u.u64 / stats[ST_F_CACHE_LOOKUPS].u.u64) : 0,
Willy Tarreau1b0f85e2018-05-28 15:12:40 +0200786 U2H(stats[ST_F_WREW].u.u64));
William Lallemand74c24fb2016-11-21 17:18:36 +0100787 }
788
789 chunk_appendf(out,
790 "</table></div></u></td>"
791 /* sessions: lbtot, lastsess */
792 "<td></td><td></td>"
793 /* bytes : in */
794 "<td>%s</td>"
795 "",
796 U2H(stats[ST_F_BIN].u.u64));
797
798 chunk_appendf(out,
799 /* bytes:out + compression stats (via hover): comp_in, comp_out, comp_byp */
800 "<td>%s%s<div class=tips><table class=det>"
801 "<tr><th>Response bytes in:</th><td>%s</td></tr>"
802 "<tr><th>Compression in:</th><td>%s</td></tr>"
803 "<tr><th>Compression out:</th><td>%s</td><td>(%d%%)</td></tr>"
804 "<tr><th>Compression bypass:</th><td>%s</td></tr>"
805 "<tr><th>Total bytes saved:</th><td>%s</td><td>(%d%%)</td></tr>"
806 "</table></div>%s</td>",
807 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "<u>":"",
808 U2H(stats[ST_F_BOUT].u.u64),
809 U2H(stats[ST_F_BOUT].u.u64),
810 U2H(stats[ST_F_COMP_IN].u.u64),
811 U2H(stats[ST_F_COMP_OUT].u.u64),
812 stats[ST_F_COMP_IN].u.u64 ? (int)(stats[ST_F_COMP_OUT].u.u64 * 100 / stats[ST_F_COMP_IN].u.u64) : 0,
813 U2H(stats[ST_F_COMP_BYP].u.u64),
814 U2H(stats[ST_F_COMP_IN].u.u64 - stats[ST_F_COMP_OUT].u.u64),
815 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,
816 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "</u>":"");
817
818 chunk_appendf(out,
819 /* denied: req, resp */
820 "<td>%s</td><td>%s</td>"
821 /* errors : request, connect, response */
822 "<td>%s</td><td></td><td></td>"
823 /* warnings: retries, redispatches */
824 "<td></td><td></td>"
825 /* server status : reflect frontend status */
826 "<td class=ac>%s</td>"
827 /* rest of server: nothing */
828 "<td class=ac colspan=8></td></tr>"
829 "",
830 U2H(stats[ST_F_DREQ].u.u64), U2H(stats[ST_F_DRESP].u.u64),
831 U2H(stats[ST_F_EREQ].u.u64),
832 field_str(stats, ST_F_STATUS));
833 }
834 else if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_SO) {
835 chunk_appendf(out, "<tr class=socket>");
Willy Tarreauab02b3f2019-10-09 11:11:46 +0200836 if (flags & STAT_ADMIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +0100837 /* Column sub-heading for Enable or Disable server */
838 chunk_appendf(out, "<td></td>");
839 }
840
841 chunk_appendf(out,
842 /* frontend name, listener name */
843 "<td class=ac><a name=\"%s/+%s\"></a>%s"
844 "<a class=lfsb href=\"#%s/+%s\">%s</a>"
845 "",
846 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME),
Willy Tarreau708c4162019-10-09 10:19:16 +0200847 (flags & STAT_SHLGNDS)?"<u>":"",
William Lallemand74c24fb2016-11-21 17:18:36 +0100848 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME), field_str(stats, ST_F_SVNAME));
849
Willy Tarreau708c4162019-10-09 10:19:16 +0200850 if (flags & STAT_SHLGNDS) {
William Lallemand74c24fb2016-11-21 17:18:36 +0100851 chunk_appendf(out, "<div class=tips>");
852
853 if (isdigit(*field_str(stats, ST_F_ADDR)))
854 chunk_appendf(out, "IPv4: %s, ", field_str(stats, ST_F_ADDR));
855 else if (*field_str(stats, ST_F_ADDR) == '[')
856 chunk_appendf(out, "IPv6: %s, ", field_str(stats, ST_F_ADDR));
857 else if (*field_str(stats, ST_F_ADDR))
858 chunk_appendf(out, "%s, ", field_str(stats, ST_F_ADDR));
859
860 /* id */
861 chunk_appendf(out, "id: %d</div>", stats[ST_F_SID].u.u32);
862 }
863
864 chunk_appendf(out,
865 /* queue */
866 "%s</td><td colspan=3></td>"
867 /* sessions rate: current, max, limit */
868 "<td colspan=3>&nbsp;</td>"
869 /* sessions: current, max, limit, total, lbtot, lastsess */
870 "<td>%s</td><td>%s</td><td>%s</td>"
871 "<td>%s</td><td>&nbsp;</td><td>&nbsp;</td>"
872 /* bytes: in, out */
873 "<td>%s</td><td>%s</td>"
874 "",
Willy Tarreau708c4162019-10-09 10:19:16 +0200875 (flags & STAT_SHLGNDS)?"</u>":"",
William Lallemand74c24fb2016-11-21 17:18:36 +0100876 U2H(stats[ST_F_SCUR].u.u32), U2H(stats[ST_F_SMAX].u.u32), U2H(stats[ST_F_SLIM].u.u32),
877 U2H(stats[ST_F_STOT].u.u64), U2H(stats[ST_F_BIN].u.u64), U2H(stats[ST_F_BOUT].u.u64));
878
879 chunk_appendf(out,
880 /* denied: req, resp */
881 "<td>%s</td><td>%s</td>"
882 /* errors: request, connect, response */
883 "<td>%s</td><td></td><td></td>"
884 /* warnings: retries, redispatches */
885 "<td></td><td></td>"
886 /* server status: reflect listener status */
887 "<td class=ac>%s</td>"
888 /* rest of server: nothing */
889 "<td class=ac colspan=8></td></tr>"
890 "",
891 U2H(stats[ST_F_DREQ].u.u64), U2H(stats[ST_F_DRESP].u.u64),
892 U2H(stats[ST_F_EREQ].u.u64),
893 field_str(stats, ST_F_STATUS));
894 }
895 else if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_SV) {
896 const char *style;
897
898 /* determine the style to use depending on the server's state,
899 * its health and weight. There isn't a 1-to-1 mapping between
900 * state and styles for the cases where the server is (still)
901 * up. The reason is that we don't want to report nolb and
902 * drain with the same color.
903 */
904
905 if (strcmp(field_str(stats, ST_F_STATUS), "DOWN") == 0 ||
906 strcmp(field_str(stats, ST_F_STATUS), "DOWN (agent)") == 0) {
907 style = "down";
908 }
909 else if (strcmp(field_str(stats, ST_F_STATUS), "DOWN ") == 0) {
910 style = "going_up";
911 }
912 else if (strcmp(field_str(stats, ST_F_STATUS), "NOLB ") == 0) {
913 style = "going_down";
914 }
915 else if (strcmp(field_str(stats, ST_F_STATUS), "NOLB") == 0) {
916 style = "nolb";
917 }
918 else if (strcmp(field_str(stats, ST_F_STATUS), "no check") == 0) {
919 style = "no_check";
920 }
921 else if (!stats[ST_F_CHKFAIL].type ||
922 stats[ST_F_CHECK_HEALTH].u.u32 == stats[ST_F_CHECK_RISE].u.u32 + stats[ST_F_CHECK_FALL].u.u32 - 1) {
923 /* no check or max health = UP */
924 if (stats[ST_F_WEIGHT].u.u32)
925 style = "up";
926 else
927 style = "draining";
928 }
929 else {
930 style = "going_down";
931 }
932
933 if (memcmp(field_str(stats, ST_F_STATUS), "MAINT", 5) == 0)
934 chunk_appendf(out, "<tr class=\"maintain\">");
935 else
936 chunk_appendf(out,
937 "<tr class=\"%s_%s\">",
938 (stats[ST_F_BCK].u.u32) ? "backup" : "active", style);
939
940
Willy Tarreauab02b3f2019-10-09 11:11:46 +0200941 if (flags & STAT_ADMIN)
William Lallemand74c24fb2016-11-21 17:18:36 +0100942 chunk_appendf(out,
David Harrigand3db35a2016-12-30 12:12:49 +0000943 "<td><input class='%s-checkbox' type=\"checkbox\" name=\"s\" value=\"%s\"></td>",
944 field_str(stats, ST_F_PXNAME),
William Lallemand74c24fb2016-11-21 17:18:36 +0100945 field_str(stats, ST_F_SVNAME));
946
947 chunk_appendf(out,
948 "<td class=ac><a name=\"%s/%s\"></a>%s"
949 "<a class=lfsb href=\"#%s/%s\">%s</a>"
950 "",
951 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME),
Willy Tarreau708c4162019-10-09 10:19:16 +0200952 (flags & STAT_SHLGNDS) ? "<u>" : "",
William Lallemand74c24fb2016-11-21 17:18:36 +0100953 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME), field_str(stats, ST_F_SVNAME));
954
Willy Tarreau708c4162019-10-09 10:19:16 +0200955 if (flags & STAT_SHLGNDS) {
William Lallemand74c24fb2016-11-21 17:18:36 +0100956 chunk_appendf(out, "<div class=tips>");
957
958 if (isdigit(*field_str(stats, ST_F_ADDR)))
959 chunk_appendf(out, "IPv4: %s, ", field_str(stats, ST_F_ADDR));
960 else if (*field_str(stats, ST_F_ADDR) == '[')
961 chunk_appendf(out, "IPv6: %s, ", field_str(stats, ST_F_ADDR));
962 else if (*field_str(stats, ST_F_ADDR))
963 chunk_appendf(out, "%s, ", field_str(stats, ST_F_ADDR));
964
965 /* id */
966 chunk_appendf(out, "id: %d", stats[ST_F_SID].u.u32);
967
968 /* cookie */
969 if (stats[ST_F_COOKIE].type) {
970 chunk_appendf(out, ", cookie: '");
971 chunk_initstr(&src, field_str(stats, ST_F_COOKIE));
972 chunk_htmlencode(out, &src);
973 chunk_appendf(out, "'");
974 }
975
976 chunk_appendf(out, "</div>");
977 }
978
979 chunk_appendf(out,
980 /* queue : current, max, limit */
981 "%s</td><td>%s</td><td>%s</td><td>%s</td>"
982 /* sessions rate : current, max, limit */
983 "<td>%s</td><td>%s</td><td></td>"
984 "",
Willy Tarreau708c4162019-10-09 10:19:16 +0200985 (flags & STAT_SHLGNDS) ? "</u>" : "",
William Lallemand74c24fb2016-11-21 17:18:36 +0100986 U2H(stats[ST_F_QCUR].u.u32), U2H(stats[ST_F_QMAX].u.u32), LIM2A(stats[ST_F_QLIMIT].u.u32, "-"),
987 U2H(stats[ST_F_RATE].u.u32), U2H(stats[ST_F_RATE_MAX].u.u32));
988
989 chunk_appendf(out,
990 /* sessions: current, max, limit, total */
Willy Tarreauf21d17b2019-09-08 09:24:56 +0200991 "<td><u>%s<div class=tips>"
992 "<table class=det>"
993 "<tr><th>Current active connections:</th><td>%s</td></tr>"
994 "<tr><th>Current idle connections:</th><td>%s</td></tr>"
995 "<tr><th>Active connections limit:</th><td>%s</td></tr>"
996 "<tr><th>Idle connections limit:</th><td>%s</td></tr>"
997 "</table></div></u>"
998 "</td><td>%s</td><td>%s</td>"
William Lallemand74c24fb2016-11-21 17:18:36 +0100999 "<td><u>%s<div class=tips><table class=det>"
1000 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
1001 "",
Willy Tarreauf21d17b2019-09-08 09:24:56 +02001002 U2H(stats[ST_F_SCUR].u.u32),
1003 U2H(stats[ST_F_SCUR].u.u32),
1004 U2H(stats[ST_F_SRV_ICUR].u.u32),
1005 LIM2A(stats[ST_F_SLIM].u.u32, "-"),
1006 stats[ST_F_SRV_ILIM].type ? U2H(stats[ST_F_SRV_ILIM].u.u32) : "-",
1007 U2H(stats[ST_F_SMAX].u.u32), LIM2A(stats[ST_F_SLIM].u.u32, "-"),
William Lallemand74c24fb2016-11-21 17:18:36 +01001008 U2H(stats[ST_F_STOT].u.u64),
1009 U2H(stats[ST_F_STOT].u.u64));
1010
1011 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
1012 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0) {
1013 unsigned long long tot;
1014
1015 tot = stats[ST_F_HRSP_OTHER].u.u64;
1016 tot += stats[ST_F_HRSP_1XX].u.u64;
1017 tot += stats[ST_F_HRSP_2XX].u.u64;
1018 tot += stats[ST_F_HRSP_3XX].u.u64;
1019 tot += stats[ST_F_HRSP_4XX].u.u64;
1020 tot += stats[ST_F_HRSP_5XX].u.u64;
1021
1022 chunk_appendf(out,
Willy Tarreauf1573842018-12-14 11:35:36 +01001023 "<tr><th>New connections:</th><td>%s</td></tr>"
1024 "<tr><th>Reused connections:</th><td>%s</td><td>(%d%%)</td></tr>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001025 "<tr><th>Cum. HTTP responses:</th><td>%s</td></tr>"
1026 "<tr><th>- HTTP 1xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
1027 "<tr><th>- HTTP 2xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
1028 "<tr><th>- HTTP 3xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
1029 "<tr><th>- HTTP 4xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
1030 "<tr><th>- HTTP 5xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
1031 "<tr><th>- other responses:</th><td>%s</td><td>(%d%%)</td></tr>"
Willy Tarreau1b0f85e2018-05-28 15:12:40 +02001032 "<tr><th>Failed hdr rewrites:</th><td>%s</td></tr>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001033 "",
Willy Tarreauf1573842018-12-14 11:35:36 +01001034 U2H(stats[ST_F_CONNECT].u.u64),
1035 U2H(stats[ST_F_REUSE].u.u64),
1036 (stats[ST_F_CONNECT].u.u64 + stats[ST_F_REUSE].u.u64) ?
1037 (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 +01001038 U2H(tot),
1039 U2H(stats[ST_F_HRSP_1XX].u.u64), tot ? (int)(100 * stats[ST_F_HRSP_1XX].u.u64 / tot) : 0,
1040 U2H(stats[ST_F_HRSP_2XX].u.u64), tot ? (int)(100 * stats[ST_F_HRSP_2XX].u.u64 / tot) : 0,
1041 U2H(stats[ST_F_HRSP_3XX].u.u64), tot ? (int)(100 * stats[ST_F_HRSP_3XX].u.u64 / tot) : 0,
1042 U2H(stats[ST_F_HRSP_4XX].u.u64), tot ? (int)(100 * stats[ST_F_HRSP_4XX].u.u64 / tot) : 0,
1043 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 +02001044 U2H(stats[ST_F_HRSP_OTHER].u.u64), tot ? (int)(100 * stats[ST_F_HRSP_OTHER].u.u64 / tot) : 0,
1045 U2H(stats[ST_F_WREW].u.u64));
William Lallemand74c24fb2016-11-21 17:18:36 +01001046 }
1047
Christopher Faulet0d1c2a62019-11-08 14:59:51 +01001048 chunk_appendf(out, "<tr><th colspan=3>Max / Avg over last 1024 success. conn.</th></tr>");
1049 chunk_appendf(out, "<tr><th>- Queue time:</th><td>%s / %s</td><td>ms</td></tr>",
1050 U2H(stats[ST_F_QT_MAX].u.u32), U2H(stats[ST_F_QTIME].u.u32));
1051 chunk_appendf(out, "<tr><th>- Connect time:</th><td>%s / %s</td><td>ms</td></tr>",
1052 U2H(stats[ST_F_CT_MAX].u.u32), U2H(stats[ST_F_CTIME].u.u32));
William Lallemand74c24fb2016-11-21 17:18:36 +01001053 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
Christopher Faulet0d1c2a62019-11-08 14:59:51 +01001054 chunk_appendf(out, "<tr><th>- Responses time:</th><td>%s / %s</td><td>ms</td></tr>",
1055 U2H(stats[ST_F_RT_MAX].u.u32), U2H(stats[ST_F_RTIME].u.u32));
1056 chunk_appendf(out, "<tr><th>- Total time:</th><td>%s / %s</td><td>ms</td></tr>",
1057 U2H(stats[ST_F_TT_MAX].u.u32), U2H(stats[ST_F_TTIME].u.u32));
William Lallemand74c24fb2016-11-21 17:18:36 +01001058
1059 chunk_appendf(out,
1060 "</table></div></u></td>"
1061 /* sessions: lbtot, last */
1062 "<td>%s</td><td>%s</td>",
1063 U2H(stats[ST_F_LBTOT].u.u64),
1064 human_time(stats[ST_F_LASTSESS].u.s32, 1));
1065
1066 chunk_appendf(out,
1067 /* bytes : in, out */
1068 "<td>%s</td><td>%s</td>"
1069 /* denied: req, resp */
1070 "<td></td><td>%s</td>"
1071 /* errors : request, connect */
1072 "<td></td><td>%s</td>"
1073 /* errors : response */
1074 "<td><u>%s<div class=tips>Connection resets during transfers: %lld client, %lld server</div></u></td>"
1075 /* warnings: retries, redispatches */
1076 "<td>%lld</td><td>%lld</td>"
1077 "",
1078 U2H(stats[ST_F_BIN].u.u64), U2H(stats[ST_F_BOUT].u.u64),
1079 U2H(stats[ST_F_DRESP].u.u64),
1080 U2H(stats[ST_F_ECON].u.u64),
1081 U2H(stats[ST_F_ERESP].u.u64),
1082 (long long)stats[ST_F_CLI_ABRT].u.u64,
1083 (long long)stats[ST_F_SRV_ABRT].u.u64,
1084 (long long)stats[ST_F_WRETR].u.u64,
1085 (long long)stats[ST_F_WREDIS].u.u64);
1086
1087 /* status, last change */
1088 chunk_appendf(out, "<td class=ac>");
1089
1090 /* FIXME!!!!
1091 * LASTCHG should contain the last change for *this* server and must be computed
1092 * properly above, as was done below, ie: this server if maint, otherwise ref server
1093 * if tracking. Note that ref is either local or remote depending on tracking.
1094 */
1095
1096
1097 if (memcmp(field_str(stats, ST_F_STATUS), "MAINT", 5) == 0) {
1098 chunk_appendf(out, "%s MAINT", human_time(stats[ST_F_LASTCHG].u.u32, 1));
1099 }
1100 else if (memcmp(field_str(stats, ST_F_STATUS), "no check", 5) == 0) {
1101 chunk_strcat(out, "<i>no check</i>");
1102 }
1103 else {
1104 chunk_appendf(out, "%s %s", human_time(stats[ST_F_LASTCHG].u.u32, 1), field_str(stats, ST_F_STATUS));
1105 if (memcmp(field_str(stats, ST_F_STATUS), "DOWN", 4) == 0) {
1106 if (stats[ST_F_CHECK_HEALTH].u.u32)
1107 chunk_strcat(out, " &uarr;");
1108 }
1109 else if (stats[ST_F_CHECK_HEALTH].u.u32 < stats[ST_F_CHECK_RISE].u.u32 + stats[ST_F_CHECK_FALL].u.u32 - 1)
1110 chunk_strcat(out, " &darr;");
1111 }
1112
1113 if (memcmp(field_str(stats, ST_F_STATUS), "DOWN", 4) == 0 &&
1114 stats[ST_F_AGENT_STATUS].type && !stats[ST_F_AGENT_HEALTH].u.u32) {
1115 chunk_appendf(out,
1116 "</td><td class=ac><u> %s",
1117 field_str(stats, ST_F_AGENT_STATUS));
1118
1119 if (stats[ST_F_AGENT_CODE].type)
1120 chunk_appendf(out, "/%d", stats[ST_F_AGENT_CODE].u.u32);
1121
1122 if (stats[ST_F_AGENT_DURATION].type)
1123 chunk_appendf(out, " in %lums", (long)stats[ST_F_AGENT_DURATION].u.u64);
1124
1125 chunk_appendf(out, "<div class=tips>%s", field_str(stats, ST_F_AGENT_DESC));
1126
1127 if (*field_str(stats, ST_F_LAST_AGT)) {
1128 chunk_appendf(out, ": ");
1129 chunk_initstr(&src, field_str(stats, ST_F_LAST_AGT));
1130 chunk_htmlencode(out, &src);
1131 }
1132 chunk_appendf(out, "</div></u>");
1133 }
1134 else if (stats[ST_F_CHECK_STATUS].type) {
1135 chunk_appendf(out,
1136 "</td><td class=ac><u> %s",
1137 field_str(stats, ST_F_CHECK_STATUS));
1138
1139 if (stats[ST_F_CHECK_CODE].type)
1140 chunk_appendf(out, "/%d", stats[ST_F_CHECK_CODE].u.u32);
1141
1142 if (stats[ST_F_CHECK_DURATION].type)
1143 chunk_appendf(out, " in %lums", (long)stats[ST_F_CHECK_DURATION].u.u64);
1144
1145 chunk_appendf(out, "<div class=tips>%s", field_str(stats, ST_F_CHECK_DESC));
1146
1147 if (*field_str(stats, ST_F_LAST_CHK)) {
1148 chunk_appendf(out, ": ");
1149 chunk_initstr(&src, field_str(stats, ST_F_LAST_CHK));
1150 chunk_htmlencode(out, &src);
1151 }
1152 chunk_appendf(out, "</div></u>");
1153 }
1154 else
1155 chunk_appendf(out, "</td><td>");
1156
1157 chunk_appendf(out,
1158 /* weight */
1159 "</td><td class=ac>%d</td>"
1160 /* act, bck */
1161 "<td class=ac>%s</td><td class=ac>%s</td>"
1162 "",
1163 stats[ST_F_WEIGHT].u.u32,
1164 stats[ST_F_BCK].u.u32 ? "-" : "Y",
1165 stats[ST_F_BCK].u.u32 ? "Y" : "-");
1166
1167 /* check failures: unique, fatal, down time */
1168 if (strcmp(field_str(stats, ST_F_STATUS), "MAINT (resolution)") == 0) {
1169 chunk_appendf(out, "<td class=ac colspan=3>resolution</td>");
1170 }
1171 else if (stats[ST_F_CHKFAIL].type) {
1172 chunk_appendf(out, "<td><u>%lld", (long long)stats[ST_F_CHKFAIL].u.u64);
1173
1174 if (stats[ST_F_HANAFAIL].type)
1175 chunk_appendf(out, "/%lld", (long long)stats[ST_F_HANAFAIL].u.u64);
1176
1177 chunk_appendf(out,
1178 "<div class=tips>Failed Health Checks%s</div></u></td>"
1179 "<td>%lld</td><td>%s</td>"
1180 "",
1181 stats[ST_F_HANAFAIL].type ? "/Health Analyses" : "",
1182 (long long)stats[ST_F_CHKDOWN].u.u64, human_time(stats[ST_F_DOWNTIME].u.u32, 1));
1183 }
1184 else if (strcmp(field_str(stats, ST_F_STATUS), "MAINT") != 0 && field_format(stats, ST_F_TRACKED) == FF_STR) {
1185 /* tracking a server (hence inherited maint would appear as "MAINT (via...)" */
1186 chunk_appendf(out,
1187 "<td class=ac colspan=3><a class=lfsb href=\"#%s\">via %s</a></td>",
1188 field_str(stats, ST_F_TRACKED), field_str(stats, ST_F_TRACKED));
1189 }
1190 else
1191 chunk_appendf(out, "<td colspan=3></td>");
1192
1193 /* throttle */
1194 if (stats[ST_F_THROTTLE].type)
1195 chunk_appendf(out, "<td class=ac>%d %%</td></tr>\n", stats[ST_F_THROTTLE].u.u32);
1196 else
1197 chunk_appendf(out, "<td class=ac>-</td></tr>\n");
1198 }
1199 else if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_BE) {
1200 chunk_appendf(out, "<tr class=\"backend\">");
Willy Tarreauab02b3f2019-10-09 11:11:46 +02001201 if (flags & STAT_ADMIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001202 /* Column sub-heading for Enable or Disable server */
1203 chunk_appendf(out, "<td></td>");
1204 }
1205 chunk_appendf(out,
1206 "<td class=ac>"
1207 /* name */
1208 "%s<a name=\"%s/Backend\"></a>"
1209 "<a class=lfsb href=\"#%s/Backend\">Backend</a>"
1210 "",
Willy Tarreau708c4162019-10-09 10:19:16 +02001211 (flags & STAT_SHLGNDS)?"<u>":"",
William Lallemand74c24fb2016-11-21 17:18:36 +01001212 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_PXNAME));
1213
Willy Tarreau708c4162019-10-09 10:19:16 +02001214 if (flags & STAT_SHLGNDS) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001215 /* balancing */
1216 chunk_appendf(out, "<div class=tips>balancing: %s",
1217 field_str(stats, ST_F_ALGO));
1218
1219 /* cookie */
1220 if (stats[ST_F_COOKIE].type) {
1221 chunk_appendf(out, ", cookie: '");
1222 chunk_initstr(&src, field_str(stats, ST_F_COOKIE));
1223 chunk_htmlencode(out, &src);
1224 chunk_appendf(out, "'");
1225 }
1226 chunk_appendf(out, "</div>");
1227 }
1228
1229 chunk_appendf(out,
1230 "%s</td>"
1231 /* queue : current, max */
1232 "<td>%s</td><td>%s</td><td></td>"
1233 /* sessions rate : current, max, limit */
1234 "<td>%s</td><td>%s</td><td></td>"
1235 "",
Willy Tarreau708c4162019-10-09 10:19:16 +02001236 (flags & STAT_SHLGNDS)?"</u>":"",
William Lallemand74c24fb2016-11-21 17:18:36 +01001237 U2H(stats[ST_F_QCUR].u.u32), U2H(stats[ST_F_QMAX].u.u32),
1238 U2H(stats[ST_F_RATE].u.u32), U2H(stats[ST_F_RATE_MAX].u.u32));
1239
1240 chunk_appendf(out,
1241 /* sessions: current, max, limit, total */
1242 "<td>%s</td><td>%s</td><td>%s</td>"
1243 "<td><u>%s<div class=tips><table class=det>"
1244 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
1245 "",
Willy Tarreau8e0f1752016-12-12 15:07:29 +01001246 U2H(stats[ST_F_SCUR].u.u32), U2H(stats[ST_F_SMAX].u.u32), U2H(stats[ST_F_SLIM].u.u32),
William Lallemand74c24fb2016-11-21 17:18:36 +01001247 U2H(stats[ST_F_STOT].u.u64),
1248 U2H(stats[ST_F_STOT].u.u64));
1249
1250 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
1251 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0) {
1252 chunk_appendf(out,
Willy Tarreauf1573842018-12-14 11:35:36 +01001253 "<tr><th>New connections:</th><td>%s</td></tr>"
1254 "<tr><th>Reused connections:</th><td>%s</td><td>(%d%%)</td></tr>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001255 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
1256 "<tr><th>- HTTP 1xx responses:</th><td>%s</td></tr>"
1257 "<tr><th>- HTTP 2xx responses:</th><td>%s</td></tr>"
1258 "<tr><th>&nbsp;&nbsp;Compressed 2xx:</th><td>%s</td><td>(%d%%)</td></tr>"
1259 "<tr><th>- HTTP 3xx responses:</th><td>%s</td></tr>"
1260 "<tr><th>- HTTP 4xx responses:</th><td>%s</td></tr>"
1261 "<tr><th>- HTTP 5xx responses:</th><td>%s</td></tr>"
1262 "<tr><th>- other responses:</th><td>%s</td></tr>"
Willy Tarreaua1214a52018-12-14 14:00:25 +01001263 "<tr><th>Cache lookups:</th><td>%s</td></tr>"
1264 "<tr><th>Cache hits:</th><td>%s</td><td>(%d%%)</td></tr>"
Willy Tarreau1b0f85e2018-05-28 15:12:40 +02001265 "<tr><th>Failed hdr rewrites:</th><td>%s</td></tr>"
Christopher Faulet0d1c2a62019-11-08 14:59:51 +01001266 "",
Willy Tarreauf1573842018-12-14 11:35:36 +01001267 U2H(stats[ST_F_CONNECT].u.u64),
1268 U2H(stats[ST_F_REUSE].u.u64),
1269 (stats[ST_F_CONNECT].u.u64 + stats[ST_F_REUSE].u.u64) ?
1270 (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 +01001271 U2H(stats[ST_F_REQ_TOT].u.u64),
1272 U2H(stats[ST_F_HRSP_1XX].u.u64),
1273 U2H(stats[ST_F_HRSP_2XX].u.u64),
1274 U2H(stats[ST_F_COMP_RSP].u.u64),
1275 stats[ST_F_HRSP_2XX].u.u64 ?
1276 (int)(100 * stats[ST_F_COMP_RSP].u.u64 / stats[ST_F_HRSP_2XX].u.u64) : 0,
1277 U2H(stats[ST_F_HRSP_3XX].u.u64),
1278 U2H(stats[ST_F_HRSP_4XX].u.u64),
1279 U2H(stats[ST_F_HRSP_5XX].u.u64),
Willy Tarreaufeead3a2018-12-14 13:48:44 +01001280 U2H(stats[ST_F_HRSP_OTHER].u.u64),
Willy Tarreaua1214a52018-12-14 14:00:25 +01001281 U2H(stats[ST_F_CACHE_LOOKUPS].u.u64),
1282 U2H(stats[ST_F_CACHE_HITS].u.u64),
1283 stats[ST_F_CACHE_LOOKUPS].u.u64 ?
1284 (int)(100 * stats[ST_F_CACHE_HITS].u.u64 / stats[ST_F_CACHE_LOOKUPS].u.u64) : 0,
Willy Tarreaufeead3a2018-12-14 13:48:44 +01001285 U2H(stats[ST_F_WREW].u.u64));
William Lallemand74c24fb2016-11-21 17:18:36 +01001286 }
1287
Christopher Faulet0d1c2a62019-11-08 14:59:51 +01001288 chunk_appendf(out, "<tr><th colspan=3>Max / Avg over last 1024 success. conn.</th></tr>");
1289 chunk_appendf(out, "<tr><th>- Queue time:</th><td>%s / %s</td><td>ms</td></tr>",
1290 U2H(stats[ST_F_QT_MAX].u.u32), U2H(stats[ST_F_QTIME].u.u32));
1291 chunk_appendf(out, "<tr><th>- Connect time:</th><td>%s / %s</td><td>ms</td></tr>",
1292 U2H(stats[ST_F_CT_MAX].u.u32), U2H(stats[ST_F_CTIME].u.u32));
William Lallemand74c24fb2016-11-21 17:18:36 +01001293 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
Christopher Faulet0d1c2a62019-11-08 14:59:51 +01001294 chunk_appendf(out, "<tr><th>- Responses time:</th><td>%s / %s</td><td>ms</td></tr>",
1295 U2H(stats[ST_F_RT_MAX].u.u32), U2H(stats[ST_F_RTIME].u.u32));
1296 chunk_appendf(out, "<tr><th>- Total time:</th><td>%s / %s</td><td>ms</td></tr>",
1297 U2H(stats[ST_F_TT_MAX].u.u32), U2H(stats[ST_F_TTIME].u.u32));
William Lallemand74c24fb2016-11-21 17:18:36 +01001298
1299 chunk_appendf(out,
1300 "</table></div></u></td>"
1301 /* sessions: lbtot, last */
1302 "<td>%s</td><td>%s</td>"
1303 /* bytes: in */
1304 "<td>%s</td>"
1305 "",
1306 U2H(stats[ST_F_LBTOT].u.u64),
1307 human_time(stats[ST_F_LASTSESS].u.s32, 1),
1308 U2H(stats[ST_F_BIN].u.u64));
1309
1310 chunk_appendf(out,
1311 /* bytes:out + compression stats (via hover): comp_in, comp_out, comp_byp */
1312 "<td>%s%s<div class=tips><table class=det>"
1313 "<tr><th>Response bytes in:</th><td>%s</td></tr>"
1314 "<tr><th>Compression in:</th><td>%s</td></tr>"
1315 "<tr><th>Compression out:</th><td>%s</td><td>(%d%%)</td></tr>"
1316 "<tr><th>Compression bypass:</th><td>%s</td></tr>"
1317 "<tr><th>Total bytes saved:</th><td>%s</td><td>(%d%%)</td></tr>"
1318 "</table></div>%s</td>",
1319 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "<u>":"",
1320 U2H(stats[ST_F_BOUT].u.u64),
1321 U2H(stats[ST_F_BOUT].u.u64),
1322 U2H(stats[ST_F_COMP_IN].u.u64),
1323 U2H(stats[ST_F_COMP_OUT].u.u64),
1324 stats[ST_F_COMP_IN].u.u64 ? (int)(stats[ST_F_COMP_OUT].u.u64 * 100 / stats[ST_F_COMP_IN].u.u64) : 0,
1325 U2H(stats[ST_F_COMP_BYP].u.u64),
1326 U2H(stats[ST_F_COMP_IN].u.u64 - stats[ST_F_COMP_OUT].u.u64),
1327 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,
1328 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "</u>":"");
1329
1330 chunk_appendf(out,
1331 /* denied: req, resp */
1332 "<td>%s</td><td>%s</td>"
1333 /* errors : request, connect */
1334 "<td></td><td>%s</td>"
1335 /* errors : response */
1336 "<td><u>%s<div class=tips>Connection resets during transfers: %lld client, %lld server</div></u></td>"
1337 /* warnings: retries, redispatches */
1338 "<td>%lld</td><td>%lld</td>"
1339 /* backend status: reflect backend status (up/down): we display UP
1340 * if the backend has known working servers or if it has no server at
1341 * all (eg: for stats). Then we display the total weight, number of
1342 * active and backups. */
1343 "<td class=ac>%s %s</td><td class=ac>&nbsp;</td><td class=ac>%d</td>"
1344 "<td class=ac>%d</td><td class=ac>%d</td>"
1345 "",
1346 U2H(stats[ST_F_DREQ].u.u64), U2H(stats[ST_F_DRESP].u.u64),
1347 U2H(stats[ST_F_ECON].u.u64),
1348 U2H(stats[ST_F_ERESP].u.u64),
1349 (long long)stats[ST_F_CLI_ABRT].u.u64,
1350 (long long)stats[ST_F_SRV_ABRT].u.u64,
1351 (long long)stats[ST_F_WRETR].u.u64, (long long)stats[ST_F_WREDIS].u.u64,
1352 human_time(stats[ST_F_LASTCHG].u.u32, 1),
1353 strcmp(field_str(stats, ST_F_STATUS), "DOWN") ? field_str(stats, ST_F_STATUS) : "<font color=\"red\"><b>DOWN</b></font>",
1354 stats[ST_F_WEIGHT].u.u32,
1355 stats[ST_F_ACT].u.u32, stats[ST_F_BCK].u.u32);
1356
1357 chunk_appendf(out,
1358 /* rest of backend: nothing, down transitions, total downtime, throttle */
1359 "<td class=ac>&nbsp;</td><td>%d</td>"
1360 "<td>%s</td>"
1361 "<td></td>"
1362 "</tr>",
1363 stats[ST_F_CHKDOWN].u.u32,
1364 stats[ST_F_DOWNTIME].type ? human_time(stats[ST_F_DOWNTIME].u.u32, 1) : "&nbsp;");
1365 }
1366 return 1;
1367}
1368
Willy Tarreau43241ff2019-10-09 11:27:51 +02001369static int stats_dump_one_line(const struct field *stats, struct proxy *px, struct appctx *appctx)
William Lallemand74c24fb2016-11-21 17:18:36 +01001370{
Simon Horman05ee2132017-01-04 09:37:25 +01001371 int ret;
1372
William Lallemand74c24fb2016-11-21 17:18:36 +01001373 if (appctx->ctx.stats.flags & STAT_FMT_HTML)
Willy Tarreau43241ff2019-10-09 11:27:51 +02001374 ret = stats_dump_fields_html(&trash, stats, appctx->ctx.stats.flags);
William Lallemand74c24fb2016-11-21 17:18:36 +01001375 else if (appctx->ctx.stats.flags & STAT_FMT_TYPED)
Willy Tarreau43241ff2019-10-09 11:27:51 +02001376 ret = stats_dump_fields_typed(&trash, stats, appctx->ctx.stats.flags);
Simon Horman05ee2132017-01-04 09:37:25 +01001377 else if (appctx->ctx.stats.flags & STAT_FMT_JSON)
Willy Tarreaub0ce3ad2019-10-09 11:19:29 +02001378 ret = stats_dump_fields_json(&trash, stats, appctx->ctx.stats.flags);
William Lallemand74c24fb2016-11-21 17:18:36 +01001379 else
Willy Tarreau43241ff2019-10-09 11:27:51 +02001380 ret = stats_dump_fields_csv(&trash, stats, appctx->ctx.stats.flags);
Simon Horman05ee2132017-01-04 09:37:25 +01001381
1382 if (ret)
1383 appctx->ctx.stats.flags |= STAT_STARTED;
1384
1385 return ret;
William Lallemand74c24fb2016-11-21 17:18:36 +01001386}
1387
1388/* Fill <stats> with the frontend statistics. <stats> is
1389 * preallocated array of length <len>. The length of the array
1390 * must be at least ST_F_TOTAL_FIELDS. If this length is less then
1391 * this value, the function returns 0, otherwise, it returns 1.
1392 */
1393int stats_fill_fe_stats(struct proxy *px, struct field *stats, int len)
1394{
1395 if (len < ST_F_TOTAL_FIELDS)
1396 return 0;
1397
1398 memset(stats, 0, sizeof(*stats) * len);
1399
1400 stats[ST_F_PXNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
1401 stats[ST_F_SVNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, "FRONTEND");
1402 stats[ST_F_MODE] = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
1403 stats[ST_F_SCUR] = mkf_u32(0, px->feconn);
1404 stats[ST_F_SMAX] = mkf_u32(FN_MAX, px->fe_counters.conn_max);
1405 stats[ST_F_SLIM] = mkf_u32(FO_CONFIG|FN_LIMIT, px->maxconn);
1406 stats[ST_F_STOT] = mkf_u64(FN_COUNTER, px->fe_counters.cum_sess);
1407 stats[ST_F_BIN] = mkf_u64(FN_COUNTER, px->fe_counters.bytes_in);
1408 stats[ST_F_BOUT] = mkf_u64(FN_COUNTER, px->fe_counters.bytes_out);
1409 stats[ST_F_DREQ] = mkf_u64(FN_COUNTER, px->fe_counters.denied_req);
1410 stats[ST_F_DRESP] = mkf_u64(FN_COUNTER, px->fe_counters.denied_resp);
1411 stats[ST_F_EREQ] = mkf_u64(FN_COUNTER, px->fe_counters.failed_req);
1412 stats[ST_F_DCON] = mkf_u64(FN_COUNTER, px->fe_counters.denied_conn);
1413 stats[ST_F_DSES] = mkf_u64(FN_COUNTER, px->fe_counters.denied_sess);
1414 stats[ST_F_STATUS] = mkf_str(FO_STATUS, px->state == PR_STREADY ? "OPEN" : px->state == PR_STFULL ? "FULL" : "STOP");
1415 stats[ST_F_PID] = mkf_u32(FO_KEY, relative_pid);
1416 stats[ST_F_IID] = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
1417 stats[ST_F_SID] = mkf_u32(FO_KEY|FS_SERVICE, 0);
1418 stats[ST_F_TYPE] = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_FE);
1419 stats[ST_F_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&px->fe_sess_per_sec));
1420 stats[ST_F_RATE_LIM] = mkf_u32(FO_CONFIG|FN_LIMIT, px->fe_sps_lim);
1421 stats[ST_F_RATE_MAX] = mkf_u32(FN_MAX, px->fe_counters.sps_max);
Tim Duesterhus3fd19732018-05-27 20:35:08 +02001422 stats[ST_F_WREW] = mkf_u64(FN_COUNTER, px->fe_counters.failed_rewrites);
William Lallemand74c24fb2016-11-21 17:18:36 +01001423
1424 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
1425 if (px->mode == PR_MODE_HTTP) {
1426 stats[ST_F_HRSP_1XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[1]);
1427 stats[ST_F_HRSP_2XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[2]);
1428 stats[ST_F_HRSP_3XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[3]);
1429 stats[ST_F_HRSP_4XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[4]);
1430 stats[ST_F_HRSP_5XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[5]);
1431 stats[ST_F_HRSP_OTHER] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[0]);
1432 stats[ST_F_INTERCEPTED] = mkf_u64(FN_COUNTER, px->fe_counters.intercepted_req);
Willy Tarreaua1214a52018-12-14 14:00:25 +01001433 stats[ST_F_CACHE_LOOKUPS] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.cache_lookups);
1434 stats[ST_F_CACHE_HITS] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.cache_hits);
William Lallemand74c24fb2016-11-21 17:18:36 +01001435 }
1436
1437 /* requests : req_rate, req_rate_max, req_tot, */
1438 stats[ST_F_REQ_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&px->fe_req_per_sec));
1439 stats[ST_F_REQ_RATE_MAX] = mkf_u32(FN_MAX, px->fe_counters.p.http.rps_max);
1440 stats[ST_F_REQ_TOT] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.cum_req);
1441
1442 /* compression: in, out, bypassed, responses */
1443 stats[ST_F_COMP_IN] = mkf_u64(FN_COUNTER, px->fe_counters.comp_in);
1444 stats[ST_F_COMP_OUT] = mkf_u64(FN_COUNTER, px->fe_counters.comp_out);
1445 stats[ST_F_COMP_BYP] = mkf_u64(FN_COUNTER, px->fe_counters.comp_byp);
1446 stats[ST_F_COMP_RSP] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.comp_rsp);
1447
1448 /* connections : conn_rate, conn_rate_max, conn_tot, conn_max */
1449 stats[ST_F_CONN_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&px->fe_conn_per_sec));
1450 stats[ST_F_CONN_RATE_MAX] = mkf_u32(FN_MAX, px->fe_counters.cps_max);
1451 stats[ST_F_CONN_TOT] = mkf_u64(FN_COUNTER, px->fe_counters.cum_conn);
1452
1453 return 1;
1454}
1455
1456/* Dumps a frontend's line to the trash for the current proxy <px> and uses
1457 * the state from stream interface <si>. The caller is responsible for clearing
1458 * the trash if needed. Returns non-zero if it emits anything, zero otherwise.
1459 */
1460static int stats_dump_fe_stats(struct stream_interface *si, struct proxy *px)
1461{
1462 struct appctx *appctx = __objt_appctx(si->end);
1463
1464 if (!(px->cap & PR_CAP_FE))
1465 return 0;
1466
1467 if ((appctx->ctx.stats.flags & STAT_BOUND) && !(appctx->ctx.stats.type & (1 << STATS_TYPE_FE)))
1468 return 0;
1469
1470 if (!stats_fill_fe_stats(px, stats, ST_F_TOTAL_FIELDS))
1471 return 0;
1472
Willy Tarreau43241ff2019-10-09 11:27:51 +02001473 return stats_dump_one_line(stats, px, appctx);
William Lallemand74c24fb2016-11-21 17:18:36 +01001474}
1475
1476/* Fill <stats> with the listener statistics. <stats> is
1477 * preallocated array of length <len>. The length of the array
1478 * must be at least ST_F_TOTAL_FIELDS. If this length is less
1479 * then this value, the function returns 0, otherwise, it
Willy Tarreau708c4162019-10-09 10:19:16 +02001480 * returns 1. <flags> can take the value STAT_SHLGNDS.
William Lallemand74c24fb2016-11-21 17:18:36 +01001481 */
1482int stats_fill_li_stats(struct proxy *px, struct listener *l, int flags,
1483 struct field *stats, int len)
1484{
Willy Tarreau83061a82018-07-13 11:56:34 +02001485 struct buffer *out = get_trash_chunk();
William Lallemand74c24fb2016-11-21 17:18:36 +01001486
1487 if (len < ST_F_TOTAL_FIELDS)
1488 return 0;
1489
1490 if (!l->counters)
1491 return 0;
1492
1493 chunk_reset(out);
1494 memset(stats, 0, sizeof(*stats) * len);
1495
1496 stats[ST_F_PXNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
1497 stats[ST_F_SVNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, l->name);
1498 stats[ST_F_MODE] = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
1499 stats[ST_F_SCUR] = mkf_u32(0, l->nbconn);
1500 stats[ST_F_SMAX] = mkf_u32(FN_MAX, l->counters->conn_max);
1501 stats[ST_F_SLIM] = mkf_u32(FO_CONFIG|FN_LIMIT, l->maxconn);
1502 stats[ST_F_STOT] = mkf_u64(FN_COUNTER, l->counters->cum_conn);
1503 stats[ST_F_BIN] = mkf_u64(FN_COUNTER, l->counters->bytes_in);
1504 stats[ST_F_BOUT] = mkf_u64(FN_COUNTER, l->counters->bytes_out);
1505 stats[ST_F_DREQ] = mkf_u64(FN_COUNTER, l->counters->denied_req);
1506 stats[ST_F_DRESP] = mkf_u64(FN_COUNTER, l->counters->denied_resp);
1507 stats[ST_F_EREQ] = mkf_u64(FN_COUNTER, l->counters->failed_req);
1508 stats[ST_F_DCON] = mkf_u64(FN_COUNTER, l->counters->denied_conn);
1509 stats[ST_F_DSES] = mkf_u64(FN_COUNTER, l->counters->denied_sess);
Willy Tarreaua8cf66b2019-02-27 16:49:00 +01001510 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 +01001511 stats[ST_F_PID] = mkf_u32(FO_KEY, relative_pid);
1512 stats[ST_F_IID] = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
1513 stats[ST_F_SID] = mkf_u32(FO_KEY|FS_SERVICE, l->luid);
1514 stats[ST_F_TYPE] = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_SO);
Tim Duesterhus3fd19732018-05-27 20:35:08 +02001515 stats[ST_F_WREW] = mkf_u64(FN_COUNTER, l->counters->failed_rewrites);
William Lallemand74c24fb2016-11-21 17:18:36 +01001516
Willy Tarreau708c4162019-10-09 10:19:16 +02001517 if (flags & STAT_SHLGNDS) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001518 char str[INET6_ADDRSTRLEN];
1519 int port;
1520
1521 port = get_host_port(&l->addr);
1522 switch (addr_to_str(&l->addr, str, sizeof(str))) {
1523 case AF_INET:
1524 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
1525 chunk_appendf(out, "%s:%d", str, port);
1526 break;
1527 case AF_INET6:
1528 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
1529 chunk_appendf(out, "[%s]:%d", str, port);
1530 break;
1531 case AF_UNIX:
1532 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, "unix");
1533 break;
1534 case -1:
1535 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
1536 chunk_strcat(out, strerror(errno));
1537 break;
1538 default: /* address family not supported */
1539 break;
1540 }
1541 }
1542
1543 return 1;
1544}
1545
1546/* Dumps a line for listener <l> and proxy <px> to the trash and uses the state
Willy Tarreau43241ff2019-10-09 11:27:51 +02001547 * from stream interface <si>. The caller is responsible for clearing the trash
1548 * if needed. Returns non-zero if it emits anything, zero otherwise.
William Lallemand74c24fb2016-11-21 17:18:36 +01001549 */
Willy Tarreau43241ff2019-10-09 11:27:51 +02001550static int stats_dump_li_stats(struct stream_interface *si, struct proxy *px, struct listener *l)
William Lallemand74c24fb2016-11-21 17:18:36 +01001551{
1552 struct appctx *appctx = __objt_appctx(si->end);
1553
Willy Tarreau43241ff2019-10-09 11:27:51 +02001554 if (!stats_fill_li_stats(px, l, appctx->ctx.stats.flags, stats, ST_F_TOTAL_FIELDS))
William Lallemand74c24fb2016-11-21 17:18:36 +01001555 return 0;
1556
Willy Tarreau43241ff2019-10-09 11:27:51 +02001557 return stats_dump_one_line(stats, px, appctx);
William Lallemand74c24fb2016-11-21 17:18:36 +01001558}
1559
1560enum srv_stats_state {
1561 SRV_STATS_STATE_DOWN = 0,
1562 SRV_STATS_STATE_DOWN_AGENT,
1563 SRV_STATS_STATE_GOING_UP,
1564 SRV_STATS_STATE_UP_GOING_DOWN,
1565 SRV_STATS_STATE_UP,
1566 SRV_STATS_STATE_NOLB_GOING_DOWN,
1567 SRV_STATS_STATE_NOLB,
1568 SRV_STATS_STATE_DRAIN_GOING_DOWN,
1569 SRV_STATS_STATE_DRAIN,
1570 SRV_STATS_STATE_DRAIN_AGENT,
1571 SRV_STATS_STATE_NO_CHECK,
1572
1573 SRV_STATS_STATE_COUNT, /* Must be last */
1574};
1575
1576static const char *srv_hlt_st[SRV_STATS_STATE_COUNT] = {
1577 [SRV_STATS_STATE_DOWN] = "DOWN",
1578 [SRV_STATS_STATE_DOWN_AGENT] = "DOWN (agent)",
1579 [SRV_STATS_STATE_GOING_UP] = "DOWN %d/%d",
1580 [SRV_STATS_STATE_UP_GOING_DOWN] = "UP %d/%d",
1581 [SRV_STATS_STATE_UP] = "UP",
1582 [SRV_STATS_STATE_NOLB_GOING_DOWN] = "NOLB %d/%d",
1583 [SRV_STATS_STATE_NOLB] = "NOLB",
1584 [SRV_STATS_STATE_DRAIN_GOING_DOWN] = "DRAIN %d/%d",
1585 [SRV_STATS_STATE_DRAIN] = "DRAIN",
1586 [SRV_STATS_STATE_DRAIN_AGENT] = "DRAIN (agent)",
1587 [SRV_STATS_STATE_NO_CHECK] = "no check"
1588};
1589
1590/* Fill <stats> with the server statistics. <stats> is
1591 * preallocated array of length <len>. The length of the array
1592 * must be at least ST_F_TOTAL_FIELDS. If this length is less
1593 * then this value, the function returns 0, otherwise, it
Willy Tarreau708c4162019-10-09 10:19:16 +02001594 * returns 1. <flags> can take the value STAT_SHLGNDS.
William Lallemand74c24fb2016-11-21 17:18:36 +01001595 */
1596int stats_fill_sv_stats(struct proxy *px, struct server *sv, int flags,
1597 struct field *stats, int len)
1598{
1599 struct server *via, *ref;
1600 char str[INET6_ADDRSTRLEN];
Willy Tarreau83061a82018-07-13 11:56:34 +02001601 struct buffer *out = get_trash_chunk();
William Lallemand74c24fb2016-11-21 17:18:36 +01001602 enum srv_stats_state state;
1603 char *fld_status;
1604
1605 if (len < ST_F_TOTAL_FIELDS)
1606 return 0;
1607
1608 memset(stats, 0, sizeof(*stats) * len);
1609
1610 /* we have "via" which is the tracked server as described in the configuration,
1611 * and "ref" which is the checked server and the end of the chain.
1612 */
1613 via = sv->track ? sv->track : sv;
1614 ref = via;
1615 while (ref->track)
1616 ref = ref->track;
1617
Emeric Brun52a91d32017-08-31 14:41:55 +02001618 if (sv->cur_state == SRV_ST_RUNNING || sv->cur_state == SRV_ST_STARTING) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001619 if ((ref->check.state & CHK_ST_ENABLED) &&
1620 (ref->check.health < ref->check.rise + ref->check.fall - 1)) {
1621 state = SRV_STATS_STATE_UP_GOING_DOWN;
1622 } else {
1623 state = SRV_STATS_STATE_UP;
1624 }
1625
Emeric Brun52a91d32017-08-31 14:41:55 +02001626 if (sv->cur_admin & SRV_ADMF_DRAIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001627 if (ref->agent.state & CHK_ST_ENABLED)
1628 state = SRV_STATS_STATE_DRAIN_AGENT;
1629 else if (state == SRV_STATS_STATE_UP_GOING_DOWN)
1630 state = SRV_STATS_STATE_DRAIN_GOING_DOWN;
1631 else
1632 state = SRV_STATS_STATE_DRAIN;
1633 }
1634
1635 if (state == SRV_STATS_STATE_UP && !(ref->check.state & CHK_ST_ENABLED)) {
1636 state = SRV_STATS_STATE_NO_CHECK;
1637 }
1638 }
Emeric Brun52a91d32017-08-31 14:41:55 +02001639 else if (sv->cur_state == SRV_ST_STOPPING) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001640 if ((!(sv->check.state & CHK_ST_ENABLED) && !sv->track) ||
1641 (ref->check.health == ref->check.rise + ref->check.fall - 1)) {
1642 state = SRV_STATS_STATE_NOLB;
1643 } else {
1644 state = SRV_STATS_STATE_NOLB_GOING_DOWN;
1645 }
1646 }
1647 else { /* stopped */
1648 if ((ref->agent.state & CHK_ST_ENABLED) && !ref->agent.health) {
1649 state = SRV_STATS_STATE_DOWN_AGENT;
1650 } else if ((ref->check.state & CHK_ST_ENABLED) && !ref->check.health) {
1651 state = SRV_STATS_STATE_DOWN; /* DOWN */
1652 } else if ((ref->agent.state & CHK_ST_ENABLED) || (ref->check.state & CHK_ST_ENABLED)) {
1653 state = SRV_STATS_STATE_GOING_UP;
1654 } else {
1655 state = SRV_STATS_STATE_DOWN; /* DOWN, unchecked */
1656 }
1657 }
1658
1659 chunk_reset(out);
1660
1661 stats[ST_F_PXNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
1662 stats[ST_F_SVNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, sv->id);
1663 stats[ST_F_MODE] = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
1664 stats[ST_F_QCUR] = mkf_u32(0, sv->nbpend);
1665 stats[ST_F_QMAX] = mkf_u32(FN_MAX, sv->counters.nbpend_max);
1666 stats[ST_F_SCUR] = mkf_u32(0, sv->cur_sess);
1667 stats[ST_F_SMAX] = mkf_u32(FN_MAX, sv->counters.cur_sess_max);
1668
1669 if (sv->maxconn)
1670 stats[ST_F_SLIM] = mkf_u32(FO_CONFIG|FN_LIMIT, sv->maxconn);
1671
Willy Tarreauf21d17b2019-09-08 09:24:56 +02001672 stats[ST_F_SRV_ICUR] = mkf_u32(0, sv->curr_idle_conns);
1673 if (sv->max_idle_conns != -1)
1674 stats[ST_F_SRV_ILIM] = mkf_u32(FO_CONFIG|FN_LIMIT, sv->max_idle_conns);
1675
William Lallemand74c24fb2016-11-21 17:18:36 +01001676 stats[ST_F_STOT] = mkf_u64(FN_COUNTER, sv->counters.cum_sess);
1677 stats[ST_F_BIN] = mkf_u64(FN_COUNTER, sv->counters.bytes_in);
1678 stats[ST_F_BOUT] = mkf_u64(FN_COUNTER, sv->counters.bytes_out);
1679 stats[ST_F_DRESP] = mkf_u64(FN_COUNTER, sv->counters.failed_secu);
1680 stats[ST_F_ECON] = mkf_u64(FN_COUNTER, sv->counters.failed_conns);
1681 stats[ST_F_ERESP] = mkf_u64(FN_COUNTER, sv->counters.failed_resp);
1682 stats[ST_F_WRETR] = mkf_u64(FN_COUNTER, sv->counters.retries);
1683 stats[ST_F_WREDIS] = mkf_u64(FN_COUNTER, sv->counters.redispatches);
Tim Duesterhus3fd19732018-05-27 20:35:08 +02001684 stats[ST_F_WREW] = mkf_u64(FN_COUNTER, sv->counters.failed_rewrites);
Willy Tarreauf1573842018-12-14 11:35:36 +01001685 stats[ST_F_CONNECT] = mkf_u64(FN_COUNTER, sv->counters.connect);
1686 stats[ST_F_REUSE] = mkf_u64(FN_COUNTER, sv->counters.reuse);
William Lallemand74c24fb2016-11-21 17:18:36 +01001687
1688 /* status */
1689 fld_status = chunk_newstr(out);
Emeric Brun52a91d32017-08-31 14:41:55 +02001690 if (sv->cur_admin & SRV_ADMF_RMAINT)
William Lallemand74c24fb2016-11-21 17:18:36 +01001691 chunk_appendf(out, "MAINT (resolution)");
Emeric Brun52a91d32017-08-31 14:41:55 +02001692 else if (sv->cur_admin & SRV_ADMF_IMAINT)
William Lallemand74c24fb2016-11-21 17:18:36 +01001693 chunk_appendf(out, "MAINT (via %s/%s)", via->proxy->id, via->id);
Emeric Brun52a91d32017-08-31 14:41:55 +02001694 else if (sv->cur_admin & SRV_ADMF_MAINT)
William Lallemand74c24fb2016-11-21 17:18:36 +01001695 chunk_appendf(out, "MAINT");
1696 else
1697 chunk_appendf(out,
1698 srv_hlt_st[state],
Emeric Brun52a91d32017-08-31 14:41:55 +02001699 (ref->cur_state != SRV_ST_STOPPED) ? (ref->check.health - ref->check.rise + 1) : (ref->check.health),
1700 (ref->cur_state != SRV_ST_STOPPED) ? (ref->check.fall) : (ref->check.rise));
William Lallemand74c24fb2016-11-21 17:18:36 +01001701
1702 stats[ST_F_STATUS] = mkf_str(FO_STATUS, fld_status);
1703 stats[ST_F_LASTCHG] = mkf_u32(FN_AGE, now.tv_sec - sv->last_change);
Emeric Brun52a91d32017-08-31 14:41:55 +02001704 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 +01001705 stats[ST_F_ACT] = mkf_u32(FO_STATUS, (sv->flags & SRV_F_BACKUP) ? 0 : 1);
1706 stats[ST_F_BCK] = mkf_u32(FO_STATUS, (sv->flags & SRV_F_BACKUP) ? 1 : 0);
1707
1708 /* check failures: unique, fatal; last change, total downtime */
1709 if (sv->check.state & CHK_ST_ENABLED) {
1710 stats[ST_F_CHKFAIL] = mkf_u64(FN_COUNTER, sv->counters.failed_checks);
1711 stats[ST_F_CHKDOWN] = mkf_u64(FN_COUNTER, sv->counters.down_trans);
1712 stats[ST_F_DOWNTIME] = mkf_u32(FN_COUNTER, srv_downtime(sv));
1713 }
1714
1715 if (sv->maxqueue)
1716 stats[ST_F_QLIMIT] = mkf_u32(FO_CONFIG|FS_SERVICE, sv->maxqueue);
1717
1718 stats[ST_F_PID] = mkf_u32(FO_KEY, relative_pid);
1719 stats[ST_F_IID] = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
1720 stats[ST_F_SID] = mkf_u32(FO_KEY|FS_SERVICE, sv->puid);
1721
Emeric Brun52a91d32017-08-31 14:41:55 +02001722 if (sv->cur_state == SRV_ST_STARTING && !server_is_draining(sv))
William Lallemand74c24fb2016-11-21 17:18:36 +01001723 stats[ST_F_THROTTLE] = mkf_u32(FN_AVG, server_throttle_rate(sv));
1724
1725 stats[ST_F_LBTOT] = mkf_u64(FN_COUNTER, sv->counters.cum_lbconn);
1726
1727 if (sv->track) {
1728 char *fld_track = chunk_newstr(out);
1729
1730 chunk_appendf(out, "%s/%s", sv->track->proxy->id, sv->track->id);
1731 stats[ST_F_TRACKED] = mkf_str(FO_CONFIG|FN_NAME|FS_SERVICE, fld_track);
1732 }
1733
1734 stats[ST_F_TYPE] = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_SV);
1735 stats[ST_F_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&sv->sess_per_sec));
1736 stats[ST_F_RATE_MAX] = mkf_u32(FN_MAX, sv->counters.sps_max);
1737
1738 if ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED) {
1739 const char *fld_chksts;
1740
1741 fld_chksts = chunk_newstr(out);
1742 chunk_strcat(out, "* "); // for check in progress
1743 chunk_strcat(out, get_check_status_info(sv->check.status));
1744 if (!(sv->check.state & CHK_ST_INPROGRESS))
1745 fld_chksts += 2; // skip "* "
1746 stats[ST_F_CHECK_STATUS] = mkf_str(FN_OUTPUT, fld_chksts);
1747
1748 if (sv->check.status >= HCHK_STATUS_L57DATA)
1749 stats[ST_F_CHECK_CODE] = mkf_u32(FN_OUTPUT, sv->check.code);
1750
1751 if (sv->check.status >= HCHK_STATUS_CHECKED)
1752 stats[ST_F_CHECK_DURATION] = mkf_u64(FN_DURATION, sv->check.duration);
1753
1754 stats[ST_F_CHECK_DESC] = mkf_str(FN_OUTPUT, get_check_status_description(sv->check.status));
1755 stats[ST_F_LAST_CHK] = mkf_str(FN_OUTPUT, sv->check.desc);
1756 stats[ST_F_CHECK_RISE] = mkf_u32(FO_CONFIG|FS_SERVICE, ref->check.rise);
1757 stats[ST_F_CHECK_FALL] = mkf_u32(FO_CONFIG|FS_SERVICE, ref->check.fall);
1758 stats[ST_F_CHECK_HEALTH] = mkf_u32(FO_CONFIG|FS_SERVICE, ref->check.health);
1759 }
1760
1761 if ((sv->agent.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED) {
1762 const char *fld_chksts;
1763
1764 fld_chksts = chunk_newstr(out);
1765 chunk_strcat(out, "* "); // for check in progress
1766 chunk_strcat(out, get_check_status_info(sv->agent.status));
1767 if (!(sv->agent.state & CHK_ST_INPROGRESS))
1768 fld_chksts += 2; // skip "* "
1769 stats[ST_F_AGENT_STATUS] = mkf_str(FN_OUTPUT, fld_chksts);
1770
1771 if (sv->agent.status >= HCHK_STATUS_L57DATA)
1772 stats[ST_F_AGENT_CODE] = mkf_u32(FN_OUTPUT, sv->agent.code);
1773
1774 if (sv->agent.status >= HCHK_STATUS_CHECKED)
1775 stats[ST_F_AGENT_DURATION] = mkf_u64(FN_DURATION, sv->agent.duration);
1776
1777 stats[ST_F_AGENT_DESC] = mkf_str(FN_OUTPUT, get_check_status_description(sv->agent.status));
1778 stats[ST_F_LAST_AGT] = mkf_str(FN_OUTPUT, sv->agent.desc);
1779 stats[ST_F_AGENT_RISE] = mkf_u32(FO_CONFIG|FS_SERVICE, sv->agent.rise);
1780 stats[ST_F_AGENT_FALL] = mkf_u32(FO_CONFIG|FS_SERVICE, sv->agent.fall);
1781 stats[ST_F_AGENT_HEALTH] = mkf_u32(FO_CONFIG|FS_SERVICE, sv->agent.health);
1782 }
1783
1784 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
1785 if (px->mode == PR_MODE_HTTP) {
1786 stats[ST_F_HRSP_1XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[1]);
1787 stats[ST_F_HRSP_2XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[2]);
1788 stats[ST_F_HRSP_3XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[3]);
1789 stats[ST_F_HRSP_4XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[4]);
1790 stats[ST_F_HRSP_5XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[5]);
1791 stats[ST_F_HRSP_OTHER] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[0]);
1792 }
1793
1794 if (ref->observe)
1795 stats[ST_F_HANAFAIL] = mkf_u64(FN_COUNTER, sv->counters.failed_hana);
1796
1797 stats[ST_F_CLI_ABRT] = mkf_u64(FN_COUNTER, sv->counters.cli_aborts);
1798 stats[ST_F_SRV_ABRT] = mkf_u64(FN_COUNTER, sv->counters.srv_aborts);
1799 stats[ST_F_LASTSESS] = mkf_s32(FN_AGE, srv_lastsession(sv));
1800
1801 stats[ST_F_QTIME] = mkf_u32(FN_AVG, swrate_avg(sv->counters.q_time, TIME_STATS_SAMPLES));
1802 stats[ST_F_CTIME] = mkf_u32(FN_AVG, swrate_avg(sv->counters.c_time, TIME_STATS_SAMPLES));
1803 stats[ST_F_RTIME] = mkf_u32(FN_AVG, swrate_avg(sv->counters.d_time, TIME_STATS_SAMPLES));
1804 stats[ST_F_TTIME] = mkf_u32(FN_AVG, swrate_avg(sv->counters.t_time, TIME_STATS_SAMPLES));
1805
Christopher Faulet0d1c2a62019-11-08 14:59:51 +01001806 stats[ST_F_QT_MAX] = mkf_u32(FN_MAX, sv->counters.qtime_max);
1807 stats[ST_F_CT_MAX] = mkf_u32(FN_MAX, sv->counters.ctime_max);
1808 stats[ST_F_RT_MAX] = mkf_u32(FN_MAX, sv->counters.dtime_max);
1809 stats[ST_F_TT_MAX] = mkf_u32(FN_MAX, sv->counters.ttime_max);
1810
Willy Tarreau708c4162019-10-09 10:19:16 +02001811 if (flags & STAT_SHLGNDS) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001812 switch (addr_to_str(&sv->addr, str, sizeof(str))) {
1813 case AF_INET:
1814 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
Willy Tarreau04276f32017-01-06 17:41:29 +01001815 chunk_appendf(out, "%s:%d", str, sv->svc_port);
William Lallemand74c24fb2016-11-21 17:18:36 +01001816 break;
1817 case AF_INET6:
1818 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
Willy Tarreau04276f32017-01-06 17:41:29 +01001819 chunk_appendf(out, "[%s]:%d", str, sv->svc_port);
William Lallemand74c24fb2016-11-21 17:18:36 +01001820 break;
1821 case AF_UNIX:
1822 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, "unix");
1823 break;
1824 case -1:
1825 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
1826 chunk_strcat(out, strerror(errno));
1827 break;
1828 default: /* address family not supported */
1829 break;
1830 }
1831
1832 if (sv->cookie)
1833 stats[ST_F_COOKIE] = mkf_str(FO_CONFIG|FN_NAME|FS_SERVICE, sv->cookie);
1834 }
1835
1836 return 1;
1837}
1838
1839/* Dumps a line for server <sv> and proxy <px> to the trash and uses the state
Willy Tarreau43241ff2019-10-09 11:27:51 +02001840 * from stream interface <si>, and server state <state>. The caller is
1841 * responsible for clearing the trash if needed. Returns non-zero if it emits
1842 * anything, zero otherwise.
William Lallemand74c24fb2016-11-21 17:18:36 +01001843 */
Willy Tarreau43241ff2019-10-09 11:27:51 +02001844static int stats_dump_sv_stats(struct stream_interface *si, struct proxy *px, struct server *sv)
William Lallemand74c24fb2016-11-21 17:18:36 +01001845{
1846 struct appctx *appctx = __objt_appctx(si->end);
1847
Willy Tarreau43241ff2019-10-09 11:27:51 +02001848 if (!stats_fill_sv_stats(px, sv, appctx->ctx.stats.flags, stats, ST_F_TOTAL_FIELDS))
William Lallemand74c24fb2016-11-21 17:18:36 +01001849 return 0;
1850
Willy Tarreau43241ff2019-10-09 11:27:51 +02001851 return stats_dump_one_line(stats, px, appctx);
William Lallemand74c24fb2016-11-21 17:18:36 +01001852}
1853
1854/* Fill <stats> with the backend statistics. <stats> is
1855 * preallocated array of length <len>. The length of the array
1856 * must be at least ST_F_TOTAL_FIELDS. If this length is less
1857 * then this value, the function returns 0, otherwise, it
Willy Tarreau708c4162019-10-09 10:19:16 +02001858 * returns 1. <flags> can take the value STAT_SHLGNDS.
William Lallemand74c24fb2016-11-21 17:18:36 +01001859 */
1860int stats_fill_be_stats(struct proxy *px, int flags, struct field *stats, int len)
1861{
1862 if (len < ST_F_TOTAL_FIELDS)
1863 return 0;
1864
1865 memset(stats, 0, sizeof(*stats) * len);
1866
1867 stats[ST_F_PXNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
1868 stats[ST_F_SVNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, "BACKEND");
1869 stats[ST_F_MODE] = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
1870 stats[ST_F_QCUR] = mkf_u32(0, px->nbpend);
1871 stats[ST_F_QMAX] = mkf_u32(FN_MAX, px->be_counters.nbpend_max);
Thierry FOURNIER0ff98a42016-12-19 16:50:42 +01001872 stats[ST_F_SCUR] = mkf_u32(0, px->beconn);
William Lallemand74c24fb2016-11-21 17:18:36 +01001873 stats[ST_F_SMAX] = mkf_u32(FN_MAX, px->be_counters.conn_max);
1874 stats[ST_F_SLIM] = mkf_u32(FO_CONFIG|FN_LIMIT, px->fullconn);
1875 stats[ST_F_STOT] = mkf_u64(FN_COUNTER, px->be_counters.cum_conn);
1876 stats[ST_F_BIN] = mkf_u64(FN_COUNTER, px->be_counters.bytes_in);
1877 stats[ST_F_BOUT] = mkf_u64(FN_COUNTER, px->be_counters.bytes_out);
1878 stats[ST_F_DREQ] = mkf_u64(FN_COUNTER, px->be_counters.denied_req);
1879 stats[ST_F_DRESP] = mkf_u64(FN_COUNTER, px->be_counters.denied_resp);
1880 stats[ST_F_ECON] = mkf_u64(FN_COUNTER, px->be_counters.failed_conns);
1881 stats[ST_F_ERESP] = mkf_u64(FN_COUNTER, px->be_counters.failed_resp);
1882 stats[ST_F_WRETR] = mkf_u64(FN_COUNTER, px->be_counters.retries);
1883 stats[ST_F_WREDIS] = mkf_u64(FN_COUNTER, px->be_counters.redispatches);
Tim Duesterhus3fd19732018-05-27 20:35:08 +02001884 stats[ST_F_WREW] = mkf_u64(FN_COUNTER, px->be_counters.failed_rewrites);
Willy Tarreauf1573842018-12-14 11:35:36 +01001885 stats[ST_F_CONNECT] = mkf_u64(FN_COUNTER, px->be_counters.connect);
1886 stats[ST_F_REUSE] = mkf_u64(FN_COUNTER, px->be_counters.reuse);
William Lallemand74c24fb2016-11-21 17:18:36 +01001887 stats[ST_F_STATUS] = mkf_str(FO_STATUS, (px->lbprm.tot_weight > 0 || !px->srv) ? "UP" : "DOWN");
1888 stats[ST_F_WEIGHT] = mkf_u32(FN_AVG, (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv);
1889 stats[ST_F_ACT] = mkf_u32(0, px->srv_act);
1890 stats[ST_F_BCK] = mkf_u32(0, px->srv_bck);
1891 stats[ST_F_CHKDOWN] = mkf_u64(FN_COUNTER, px->down_trans);
1892 stats[ST_F_LASTCHG] = mkf_u32(FN_AGE, now.tv_sec - px->last_change);
1893 if (px->srv)
1894 stats[ST_F_DOWNTIME] = mkf_u32(FN_COUNTER, be_downtime(px));
1895
1896 stats[ST_F_PID] = mkf_u32(FO_KEY, relative_pid);
1897 stats[ST_F_IID] = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
1898 stats[ST_F_SID] = mkf_u32(FO_KEY|FS_SERVICE, 0);
1899 stats[ST_F_LBTOT] = mkf_u64(FN_COUNTER, px->be_counters.cum_lbconn);
1900 stats[ST_F_TYPE] = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_BE);
1901 stats[ST_F_RATE] = mkf_u32(0, read_freq_ctr(&px->be_sess_per_sec));
1902 stats[ST_F_RATE_MAX] = mkf_u32(0, px->be_counters.sps_max);
1903
Willy Tarreau708c4162019-10-09 10:19:16 +02001904 if (flags & STAT_SHLGNDS) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001905 if (px->cookie_name)
1906 stats[ST_F_COOKIE] = mkf_str(FO_CONFIG|FN_NAME|FS_SERVICE, px->cookie_name);
1907 stats[ST_F_ALGO] = mkf_str(FO_CONFIG|FS_SERVICE, backend_lb_algo_str(px->lbprm.algo & BE_LB_ALGO));
1908 }
1909
1910 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
1911 if (px->mode == PR_MODE_HTTP) {
1912 stats[ST_F_REQ_TOT] = mkf_u64(FN_COUNTER, px->be_counters.p.http.cum_req);
1913 stats[ST_F_HRSP_1XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[1]);
1914 stats[ST_F_HRSP_2XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[2]);
1915 stats[ST_F_HRSP_3XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[3]);
1916 stats[ST_F_HRSP_4XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[4]);
1917 stats[ST_F_HRSP_5XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[5]);
1918 stats[ST_F_HRSP_OTHER] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[0]);
Willy Tarreaua1214a52018-12-14 14:00:25 +01001919 stats[ST_F_CACHE_LOOKUPS] = mkf_u64(FN_COUNTER, px->be_counters.p.http.cache_lookups);
1920 stats[ST_F_CACHE_HITS] = mkf_u64(FN_COUNTER, px->be_counters.p.http.cache_hits);
William Lallemand74c24fb2016-11-21 17:18:36 +01001921 }
1922
1923 stats[ST_F_CLI_ABRT] = mkf_u64(FN_COUNTER, px->be_counters.cli_aborts);
1924 stats[ST_F_SRV_ABRT] = mkf_u64(FN_COUNTER, px->be_counters.srv_aborts);
1925
1926 /* compression: in, out, bypassed, responses */
1927 stats[ST_F_COMP_IN] = mkf_u64(FN_COUNTER, px->be_counters.comp_in);
1928 stats[ST_F_COMP_OUT] = mkf_u64(FN_COUNTER, px->be_counters.comp_out);
1929 stats[ST_F_COMP_BYP] = mkf_u64(FN_COUNTER, px->be_counters.comp_byp);
1930 stats[ST_F_COMP_RSP] = mkf_u64(FN_COUNTER, px->be_counters.p.http.comp_rsp);
1931 stats[ST_F_LASTSESS] = mkf_s32(FN_AGE, be_lastsession(px));
1932
1933 stats[ST_F_QTIME] = mkf_u32(FN_AVG, swrate_avg(px->be_counters.q_time, TIME_STATS_SAMPLES));
1934 stats[ST_F_CTIME] = mkf_u32(FN_AVG, swrate_avg(px->be_counters.c_time, TIME_STATS_SAMPLES));
1935 stats[ST_F_RTIME] = mkf_u32(FN_AVG, swrate_avg(px->be_counters.d_time, TIME_STATS_SAMPLES));
1936 stats[ST_F_TTIME] = mkf_u32(FN_AVG, swrate_avg(px->be_counters.t_time, TIME_STATS_SAMPLES));
1937
Christopher Faulet0d1c2a62019-11-08 14:59:51 +01001938 stats[ST_F_QT_MAX] = mkf_u32(FN_MAX, px->be_counters.qtime_max);
1939 stats[ST_F_CT_MAX] = mkf_u32(FN_MAX, px->be_counters.ctime_max);
1940 stats[ST_F_RT_MAX] = mkf_u32(FN_MAX, px->be_counters.dtime_max);
1941 stats[ST_F_TT_MAX] = mkf_u32(FN_MAX, px->be_counters.ttime_max);
1942
William Lallemand74c24fb2016-11-21 17:18:36 +01001943 return 1;
1944}
1945
1946/* Dumps a line for backend <px> to the trash for and uses the state from stream
Willy Tarreau43241ff2019-10-09 11:27:51 +02001947 * interface <si>. The caller is responsible for clearing the trash if needed.
1948 * Returns non-zero if it emits anything, zero otherwise.
William Lallemand74c24fb2016-11-21 17:18:36 +01001949 */
Willy Tarreau43241ff2019-10-09 11:27:51 +02001950static int stats_dump_be_stats(struct stream_interface *si, struct proxy *px)
William Lallemand74c24fb2016-11-21 17:18:36 +01001951{
1952 struct appctx *appctx = __objt_appctx(si->end);
1953
1954 if (!(px->cap & PR_CAP_BE))
1955 return 0;
1956
1957 if ((appctx->ctx.stats.flags & STAT_BOUND) && !(appctx->ctx.stats.type & (1 << STATS_TYPE_BE)))
1958 return 0;
1959
Willy Tarreau43241ff2019-10-09 11:27:51 +02001960 if (!stats_fill_be_stats(px, appctx->ctx.stats.flags, stats, ST_F_TOTAL_FIELDS))
William Lallemand74c24fb2016-11-21 17:18:36 +01001961 return 0;
1962
Willy Tarreau43241ff2019-10-09 11:27:51 +02001963 return stats_dump_one_line(stats, px, appctx);
William Lallemand74c24fb2016-11-21 17:18:36 +01001964}
1965
1966/* Dumps the HTML table header for proxy <px> to the trash for and uses the state from
1967 * stream interface <si> and per-uri parameters <uri>. The caller is responsible
1968 * for clearing the trash if needed.
1969 */
Willy Tarreau676c29e2019-10-09 10:50:01 +02001970static void stats_dump_html_px_hdr(struct stream_interface *si, struct proxy *px)
William Lallemand74c24fb2016-11-21 17:18:36 +01001971{
1972 struct appctx *appctx = __objt_appctx(si->end);
1973 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
1974
1975 if (px->cap & PR_CAP_BE && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
1976 /* A form to enable/disable this proxy servers */
1977
1978 /* scope_txt = search pattern + search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
1979 scope_txt[0] = 0;
1980 if (appctx->ctx.stats.scope_len) {
Christopher Fauleted7a0662019-01-14 11:07:34 +01001981 const char *scope_ptr = stats_scope_ptr(appctx, si);
1982
William Lallemand74c24fb2016-11-21 17:18:36 +01001983 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Christopher Fauleted7a0662019-01-14 11:07:34 +01001984 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), scope_ptr, appctx->ctx.stats.scope_len);
William Lallemand74c24fb2016-11-21 17:18:36 +01001985 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
1986 }
1987
1988 chunk_appendf(&trash,
1989 "<form method=\"post\">");
1990 }
1991
1992 /* print a new table */
1993 chunk_appendf(&trash,
1994 "<table class=\"tbl\" width=\"100%%\">\n"
1995 "<tr class=\"titre\">"
1996 "<th class=\"pxname\" width=\"10%%\">");
1997
1998 chunk_appendf(&trash,
1999 "<a name=\"%s\"></a>%s"
2000 "<a class=px href=\"#%s\">%s</a>",
2001 px->id,
Willy Tarreau676c29e2019-10-09 10:50:01 +02002002 (appctx->ctx.stats.flags & STAT_SHLGNDS) ? "<u>":"",
William Lallemand74c24fb2016-11-21 17:18:36 +01002003 px->id, px->id);
2004
Willy Tarreau676c29e2019-10-09 10:50:01 +02002005 if (appctx->ctx.stats.flags & STAT_SHLGNDS) {
William Lallemand74c24fb2016-11-21 17:18:36 +01002006 /* cap, mode, id */
2007 chunk_appendf(&trash, "<div class=tips>cap: %s, mode: %s, id: %d",
2008 proxy_cap_str(px->cap), proxy_mode_str(px->mode),
2009 px->uuid);
2010 chunk_appendf(&trash, "</div>");
2011 }
2012
2013 chunk_appendf(&trash,
2014 "%s</th>"
2015 "<th class=\"%s\" width=\"90%%\">%s</th>"
2016 "</tr>\n"
2017 "</table>\n"
2018 "<table class=\"tbl\" width=\"100%%\">\n"
2019 "<tr class=\"titre\">",
Willy Tarreau676c29e2019-10-09 10:50:01 +02002020 (appctx->ctx.stats.flags & STAT_SHLGNDS) ? "</u>":"",
William Lallemand74c24fb2016-11-21 17:18:36 +01002021 px->desc ? "desc" : "empty", px->desc ? px->desc : "");
2022
Christopher Fauletbc271ec2019-12-02 11:29:04 +01002023 if (appctx->ctx.stats.flags & STAT_ADMIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +01002024 /* Column heading for Enable or Disable server */
Christopher Fauletbc271ec2019-12-02 11:29:04 +01002025 if ((px->cap & PR_CAP_BE) && px->srv)
2026 chunk_appendf(&trash,
2027 "<th rowspan=2 width=1><input type=\"checkbox\" "
2028 "onclick=\"for(c in document.getElementsByClassName('%s-checkbox')) "
2029 "document.getElementsByClassName('%s-checkbox').item(c).checked = this.checked\"></th>",
2030 px->id,
2031 px->id);
2032 else
2033 chunk_appendf(&trash, "<th rowspan=2></th>");
William Lallemand74c24fb2016-11-21 17:18:36 +01002034 }
2035
2036 chunk_appendf(&trash,
2037 "<th rowspan=2></th>"
2038 "<th colspan=3>Queue</th>"
2039 "<th colspan=3>Session rate</th><th colspan=6>Sessions</th>"
2040 "<th colspan=2>Bytes</th><th colspan=2>Denied</th>"
2041 "<th colspan=3>Errors</th><th colspan=2>Warnings</th>"
2042 "<th colspan=9>Server</th>"
2043 "</tr>\n"
2044 "<tr class=\"titre\">"
2045 "<th>Cur</th><th>Max</th><th>Limit</th>"
2046 "<th>Cur</th><th>Max</th><th>Limit</th><th>Cur</th><th>Max</th>"
2047 "<th>Limit</th><th>Total</th><th>LbTot</th><th>Last</th><th>In</th><th>Out</th>"
2048 "<th>Req</th><th>Resp</th><th>Req</th><th>Conn</th>"
2049 "<th>Resp</th><th>Retr</th><th>Redis</th>"
2050 "<th>Status</th><th>LastChk</th><th>Wght</th><th>Act</th>"
2051 "<th>Bck</th><th>Chk</th><th>Dwn</th><th>Dwntme</th>"
2052 "<th>Thrtle</th>\n"
2053 "</tr>");
2054}
2055
2056/* Dumps the HTML table trailer for proxy <px> to the trash for and uses the state from
2057 * stream interface <si>. The caller is responsible for clearing the trash if needed.
2058 */
2059static void stats_dump_html_px_end(struct stream_interface *si, struct proxy *px)
2060{
2061 struct appctx *appctx = __objt_appctx(si->end);
2062 chunk_appendf(&trash, "</table>");
2063
2064 if ((px->cap & PR_CAP_BE) && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
2065 /* close the form used to enable/disable this proxy servers */
2066 chunk_appendf(&trash,
2067 "Choose the action to perform on the checked servers : "
2068 "<select name=action>"
2069 "<option value=\"\"></option>"
2070 "<option value=\"ready\">Set state to READY</option>"
2071 "<option value=\"drain\">Set state to DRAIN</option>"
2072 "<option value=\"maint\">Set state to MAINT</option>"
2073 "<option value=\"dhlth\">Health: disable checks</option>"
2074 "<option value=\"ehlth\">Health: enable checks</option>"
2075 "<option value=\"hrunn\">Health: force UP</option>"
2076 "<option value=\"hnolb\">Health: force NOLB</option>"
2077 "<option value=\"hdown\">Health: force DOWN</option>"
2078 "<option value=\"dagent\">Agent: disable checks</option>"
2079 "<option value=\"eagent\">Agent: enable checks</option>"
2080 "<option value=\"arunn\">Agent: force UP</option>"
2081 "<option value=\"adown\">Agent: force DOWN</option>"
2082 "<option value=\"shutdown\">Kill Sessions</option>"
2083 "</select>"
2084 "<input type=\"hidden\" name=\"b\" value=\"#%d\">"
2085 "&nbsp;<input type=\"submit\" value=\"Apply\">"
2086 "</form>",
2087 px->uuid);
2088 }
2089
2090 chunk_appendf(&trash, "<p>\n");
2091}
2092
2093/*
2094 * Dumps statistics for a proxy. The output is sent to the stream interface's
2095 * input buffer. Returns 0 if it had to stop dumping data because of lack of
2096 * buffer space, or non-zero if everything completed. This function is used
2097 * both by the CLI and the HTTP entry points, and is able to dump the output
2098 * in HTML or CSV formats. If the later, <uri> must be NULL.
2099 */
Christopher Fauletef779222018-10-31 08:47:01 +01002100int stats_dump_proxy_to_buffer(struct stream_interface *si, struct htx *htx,
2101 struct proxy *px, struct uri_auth *uri)
William Lallemand74c24fb2016-11-21 17:18:36 +01002102{
2103 struct appctx *appctx = __objt_appctx(si->end);
2104 struct stream *s = si_strm(si);
2105 struct channel *rep = si_ic(si);
2106 struct server *sv, *svs; /* server and server-state, server-state=server or server->track */
2107 struct listener *l;
William Lallemand74c24fb2016-11-21 17:18:36 +01002108
2109 chunk_reset(&trash);
2110
2111 switch (appctx->ctx.stats.px_st) {
2112 case STAT_PX_ST_INIT:
2113 /* we are on a new proxy */
2114 if (uri && uri->scope) {
2115 /* we have a limited scope, we have to check the proxy name */
2116 struct stat_scope *scope;
2117 int len;
2118
2119 len = strlen(px->id);
2120 scope = uri->scope;
2121
2122 while (scope) {
2123 /* match exact proxy name */
2124 if (scope->px_len == len && !memcmp(px->id, scope->px_id, len))
2125 break;
2126
2127 /* match '.' which means 'self' proxy */
2128 if (!strcmp(scope->px_id, ".") && px == s->be)
2129 break;
2130 scope = scope->next;
2131 }
2132
2133 /* proxy name not found : don't dump anything */
2134 if (scope == NULL)
2135 return 1;
2136 }
2137
2138 /* if the user has requested a limited output and the proxy
2139 * name does not match, skip it.
2140 */
Christopher Fauleted7a0662019-01-14 11:07:34 +01002141 if (appctx->ctx.stats.scope_len) {
2142 const char *scope_ptr = stats_scope_ptr(appctx, si);
2143
2144 if (strnistr(px->id, strlen(px->id), scope_ptr, appctx->ctx.stats.scope_len) == NULL)
2145 return 1;
2146 }
William Lallemand74c24fb2016-11-21 17:18:36 +01002147
2148 if ((appctx->ctx.stats.flags & STAT_BOUND) &&
2149 (appctx->ctx.stats.iid != -1) &&
2150 (px->uuid != appctx->ctx.stats.iid))
2151 return 1;
2152
2153 appctx->ctx.stats.px_st = STAT_PX_ST_TH;
2154 /* fall through */
2155
2156 case STAT_PX_ST_TH:
2157 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreau676c29e2019-10-09 10:50:01 +02002158 stats_dump_html_px_hdr(si, px);
Christopher Fauletef779222018-10-31 08:47:01 +01002159 if (!stats_putchk(rep, htx, &trash))
2160 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002161 }
2162
2163 appctx->ctx.stats.px_st = STAT_PX_ST_FE;
2164 /* fall through */
2165
2166 case STAT_PX_ST_FE:
2167 /* print the frontend */
2168 if (stats_dump_fe_stats(si, px)) {
Christopher Fauletef779222018-10-31 08:47:01 +01002169 if (!stats_putchk(rep, htx, &trash))
2170 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002171 }
2172
2173 appctx->ctx.stats.l = px->conf.listeners.n;
2174 appctx->ctx.stats.px_st = STAT_PX_ST_LI;
2175 /* fall through */
2176
2177 case STAT_PX_ST_LI:
2178 /* stats.l has been initialized above */
2179 for (; appctx->ctx.stats.l != &px->conf.listeners; appctx->ctx.stats.l = l->by_fe.n) {
Christopher Fauletef779222018-10-31 08:47:01 +01002180 if (htx) {
2181 if (htx_almost_full(htx))
2182 goto full;
2183 }
2184 else {
2185 if (buffer_almost_full(&rep->buf))
2186 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002187 }
2188
2189 l = LIST_ELEM(appctx->ctx.stats.l, struct listener *, by_fe);
2190 if (!l->counters)
2191 continue;
2192
2193 if (appctx->ctx.stats.flags & STAT_BOUND) {
2194 if (!(appctx->ctx.stats.type & (1 << STATS_TYPE_SO)))
2195 break;
2196
2197 if (appctx->ctx.stats.sid != -1 && l->luid != appctx->ctx.stats.sid)
2198 continue;
2199 }
2200
2201 /* print the frontend */
Willy Tarreau43241ff2019-10-09 11:27:51 +02002202 if (stats_dump_li_stats(si, px, l)) {
Christopher Fauletef779222018-10-31 08:47:01 +01002203 if (!stats_putchk(rep, htx, &trash))
2204 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002205 }
2206 }
2207
2208 appctx->ctx.stats.sv = px->srv; /* may be NULL */
2209 appctx->ctx.stats.px_st = STAT_PX_ST_SV;
2210 /* fall through */
2211
2212 case STAT_PX_ST_SV:
2213 /* stats.sv has been initialized above */
2214 for (; appctx->ctx.stats.sv != NULL; appctx->ctx.stats.sv = sv->next) {
Christopher Fauletef779222018-10-31 08:47:01 +01002215 if (htx) {
2216 if (htx_almost_full(htx))
2217 goto full;
2218 }
2219 else {
2220 if (buffer_almost_full(&rep->buf))
2221 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002222 }
2223
2224 sv = appctx->ctx.stats.sv;
2225
2226 if (appctx->ctx.stats.flags & STAT_BOUND) {
2227 if (!(appctx->ctx.stats.type & (1 << STATS_TYPE_SV)))
2228 break;
2229
2230 if (appctx->ctx.stats.sid != -1 && sv->puid != appctx->ctx.stats.sid)
2231 continue;
2232 }
2233
2234 svs = sv;
2235 while (svs->track)
2236 svs = svs->track;
2237
2238 /* do not report servers which are DOWN and not changing state */
2239 if ((appctx->ctx.stats.flags & STAT_HIDE_DOWN) &&
Emeric Brun52a91d32017-08-31 14:41:55 +02002240 ((sv->cur_admin & SRV_ADMF_MAINT) || /* server is in maintenance */
2241 (sv->cur_state == SRV_ST_STOPPED && /* server is down */
William Lallemand74c24fb2016-11-21 17:18:36 +01002242 (!((svs->agent.state | svs->check.state) & CHK_ST_ENABLED) ||
2243 ((svs->agent.state & CHK_ST_ENABLED) && !svs->agent.health) ||
2244 ((svs->check.state & CHK_ST_ENABLED) && !svs->check.health))))) {
2245 continue;
2246 }
2247
Willy Tarreau43241ff2019-10-09 11:27:51 +02002248 if (stats_dump_sv_stats(si, px, sv)) {
Christopher Fauletef779222018-10-31 08:47:01 +01002249 if (!stats_putchk(rep, htx, &trash))
2250 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002251 }
2252 } /* for sv */
2253
2254 appctx->ctx.stats.px_st = STAT_PX_ST_BE;
2255 /* fall through */
2256
2257 case STAT_PX_ST_BE:
2258 /* print the backend */
Willy Tarreau43241ff2019-10-09 11:27:51 +02002259 if (stats_dump_be_stats(si, px)) {
Christopher Fauletef779222018-10-31 08:47:01 +01002260 if (!stats_putchk(rep, htx, &trash))
2261 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002262 }
2263
2264 appctx->ctx.stats.px_st = STAT_PX_ST_END;
2265 /* fall through */
2266
2267 case STAT_PX_ST_END:
2268 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
2269 stats_dump_html_px_end(si, px);
Christopher Fauletef779222018-10-31 08:47:01 +01002270 if (!stats_putchk(rep, htx, &trash))
2271 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002272 }
2273
2274 appctx->ctx.stats.px_st = STAT_PX_ST_FIN;
2275 /* fall through */
2276
2277 case STAT_PX_ST_FIN:
2278 return 1;
2279
2280 default:
2281 /* unknown state, we should put an abort() here ! */
2282 return 1;
2283 }
Christopher Fauletef779222018-10-31 08:47:01 +01002284
2285 full:
2286 si_rx_room_blk(si);
2287 return 0;
William Lallemand74c24fb2016-11-21 17:18:36 +01002288}
2289
2290/* Dumps the HTTP stats head block to the trash for and uses the per-uri
2291 * parameters <uri>. The caller is responsible for clearing the trash if needed.
2292 */
Willy Tarreau676c29e2019-10-09 10:50:01 +02002293static void stats_dump_html_head(struct appctx *appctx, struct uri_auth *uri)
William Lallemand74c24fb2016-11-21 17:18:36 +01002294{
2295 /* WARNING! This must fit in the first buffer !!! */
2296 chunk_appendf(&trash,
2297 "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"\n"
2298 "\"http://www.w3.org/TR/html4/loose.dtd\">\n"
2299 "<html><head><title>Statistics Report for " PRODUCT_NAME "%s%s</title>\n"
2300 "<meta http-equiv=\"content-type\" content=\"text/html; charset=iso-8859-1\">\n"
2301 "<style type=\"text/css\"><!--\n"
2302 "body {"
2303 " font-family: arial, helvetica, sans-serif;"
2304 " font-size: 12px;"
2305 " font-weight: normal;"
2306 " color: black;"
2307 " background: white;"
2308 "}\n"
2309 "th,td {"
2310 " font-size: 10px;"
2311 "}\n"
2312 "h1 {"
2313 " font-size: x-large;"
2314 " margin-bottom: 0.5em;"
2315 "}\n"
2316 "h2 {"
2317 " font-family: helvetica, arial;"
2318 " font-size: x-large;"
2319 " font-weight: bold;"
2320 " font-style: italic;"
2321 " color: #6020a0;"
2322 " margin-top: 0em;"
2323 " margin-bottom: 0em;"
2324 "}\n"
2325 "h3 {"
2326 " font-family: helvetica, arial;"
2327 " font-size: 16px;"
2328 " font-weight: bold;"
2329 " color: #b00040;"
2330 " background: #e8e8d0;"
2331 " margin-top: 0em;"
2332 " margin-bottom: 0em;"
2333 "}\n"
2334 "li {"
2335 " margin-top: 0.25em;"
2336 " margin-right: 2em;"
2337 "}\n"
2338 ".hr {margin-top: 0.25em;"
2339 " border-color: black;"
2340 " border-bottom-style: solid;"
2341 "}\n"
2342 ".titre {background: #20D0D0;color: #000000; font-weight: bold; text-align: center;}\n"
2343 ".total {background: #20D0D0;color: #ffff80;}\n"
2344 ".frontend {background: #e8e8d0;}\n"
2345 ".socket {background: #d0d0d0;}\n"
2346 ".backend {background: #e8e8d0;}\n"
2347 ".active_down {background: #ff9090;}\n"
2348 ".active_going_up {background: #ffd020;}\n"
2349 ".active_going_down {background: #ffffa0;}\n"
2350 ".active_up {background: #c0ffc0;}\n"
2351 ".active_nolb {background: #20a0ff;}\n"
2352 ".active_draining {background: #20a0FF;}\n"
2353 ".active_no_check {background: #e0e0e0;}\n"
2354 ".backup_down {background: #ff9090;}\n"
2355 ".backup_going_up {background: #ff80ff;}\n"
2356 ".backup_going_down {background: #c060ff;}\n"
2357 ".backup_up {background: #b0d0ff;}\n"
2358 ".backup_nolb {background: #90b0e0;}\n"
2359 ".backup_draining {background: #cc9900;}\n"
2360 ".backup_no_check {background: #e0e0e0;}\n"
2361 ".maintain {background: #c07820;}\n"
2362 ".rls {letter-spacing: 0.2em; margin-right: 1px;}\n" /* right letter spacing (used for grouping digits) */
2363 "\n"
2364 "a.px:link {color: #ffff40; text-decoration: none;}"
2365 "a.px:visited {color: #ffff40; text-decoration: none;}"
2366 "a.px:hover {color: #ffffff; text-decoration: none;}"
2367 "a.lfsb:link {color: #000000; text-decoration: none;}"
2368 "a.lfsb:visited {color: #000000; text-decoration: none;}"
2369 "a.lfsb:hover {color: #505050; text-decoration: none;}"
2370 "\n"
2371 "table.tbl { border-collapse: collapse; border-style: none;}\n"
2372 "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"
2373 "table.tbl td.ac { text-align: center;}\n"
2374 "table.tbl th { border-width: 1px; border-style: solid solid solid solid; border-color: gray;}\n"
2375 "table.tbl th.pxname { background: #b00040; color: #ffff40; font-weight: bold; border-style: solid solid none solid; padding: 2px 3px; white-space: nowrap;}\n"
2376 "table.tbl th.empty { border-style: none; empty-cells: hide; background: white;}\n"
2377 "table.tbl th.desc { background: white; border-style: solid solid none solid; text-align: left; padding: 2px 3px;}\n"
2378 "\n"
2379 "table.lgd { border-collapse: collapse; border-width: 1px; border-style: none none none solid; border-color: black;}\n"
2380 "table.lgd td { border-width: 1px; border-style: solid solid solid solid; border-color: gray; padding: 2px;}\n"
2381 "table.lgd td.noborder { border-style: none; padding: 2px; white-space: nowrap;}\n"
2382 "table.det { border-collapse: collapse; border-style: none; }\n"
2383 "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"
2384 "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"
2385 "u {text-decoration:none; border-bottom: 1px dotted black;}\n"
2386 "div.tips {\n"
2387 " display:block;\n"
2388 " visibility:hidden;\n"
2389 " z-index:2147483647;\n"
2390 " position:absolute;\n"
2391 " padding:2px 4px 3px;\n"
2392 " background:#f0f060; color:#000000;\n"
2393 " border:1px solid #7040c0;\n"
2394 " white-space:nowrap;\n"
2395 " font-style:normal;font-size:11px;font-weight:normal;\n"
2396 " -moz-border-radius:3px;-webkit-border-radius:3px;border-radius:3px;\n"
2397 " -moz-box-shadow:gray 2px 2px 3px;-webkit-box-shadow:gray 2px 2px 3px;box-shadow:gray 2px 2px 3px;\n"
2398 "}\n"
2399 "u:hover div.tips {visibility:visible;}\n"
2400 "-->\n"
2401 "</style></head>\n",
Willy Tarreau676c29e2019-10-09 10:50:01 +02002402 (appctx->ctx.stats.flags & STAT_SHNODE) ? " on " : "",
2403 (appctx->ctx.stats.flags & STAT_SHNODE) ? (uri->node ? uri->node : global.node) : ""
William Lallemand74c24fb2016-11-21 17:18:36 +01002404 );
2405}
2406
2407/* Dumps the HTML stats information block to the trash for and uses the state from
2408 * stream interface <si> and per-uri parameters <uri>. The caller is responsible
2409 * for clearing the trash if needed.
2410 */
2411static void stats_dump_html_info(struct stream_interface *si, struct uri_auth *uri)
2412{
2413 struct appctx *appctx = __objt_appctx(si->end);
2414 unsigned int up = (now.tv_sec - start_date.tv_sec);
2415 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
Christopher Fauleted7a0662019-01-14 11:07:34 +01002416 const char *scope_ptr = stats_scope_ptr(appctx, si);
Willy Tarreau1713c032019-05-23 12:23:55 +02002417 unsigned long long bps = (unsigned long long)read_freq_ctr(&global.out_32bps) * 32;
2418
2419 /* Turn the bytes per second to bits per second and take care of the
2420 * usual ethernet overhead in order to help figure how far we are from
2421 * interface saturation since it's the only case which usually matters.
2422 * For this we count the total size of an Ethernet frame on the wire
2423 * including preamble and IFG (1538) for the largest TCP segment it
2424 * transports (1448 with TCP timestamps). This is not valid for smaller
2425 * packets (under-estimated), but it gives a reasonably accurate
2426 * estimation of how far we are from uplink saturation.
2427 */
2428 bps = bps * 8 * 1538 / 1448;
William Lallemand74c24fb2016-11-21 17:18:36 +01002429
2430 /* WARNING! this has to fit the first packet too.
2431 * We are around 3.5 kB, add adding entries will
2432 * become tricky if we want to support 4kB buffers !
2433 */
2434 chunk_appendf(&trash,
2435 "<body><h1><a href=\"" PRODUCT_URL "\" style=\"text-decoration: none;\">"
2436 PRODUCT_NAME "%s</a></h1>\n"
2437 "<h2>Statistics Report for pid %d%s%s%s%s</h2>\n"
2438 "<hr width=\"100%%\" class=\"hr\">\n"
2439 "<h3>&gt; General process information</h3>\n"
2440 "<table border=0><tr><td align=\"left\" nowrap width=\"1%%\">\n"
Yves Lafon95317282018-02-26 11:10:37 +01002441 "<p><b>pid = </b> %d (process #%d, nbproc = %d, nbthread = %d)<br>\n"
William Lallemand74c24fb2016-11-21 17:18:36 +01002442 "<b>uptime = </b> %dd %dh%02dm%02ds<br>\n"
2443 "<b>system limits:</b> memmax = %s%s; ulimit-n = %d<br>\n"
2444 "<b>maxsock = </b> %d; <b>maxconn = </b> %d; <b>maxpipes = </b> %d<br>\n"
Willy Tarreau1713c032019-05-23 12:23:55 +02002445 "current conns = %d; current pipes = %d/%d; conn rate = %d/sec; bit rate = %.3f %cbps<br>\n"
William Lallemand74c24fb2016-11-21 17:18:36 +01002446 "Running tasks: %d/%d; idle = %d %%<br>\n"
2447 "</td><td align=\"center\" nowrap>\n"
2448 "<table class=\"lgd\"><tr>\n"
2449 "<td class=\"active_up\">&nbsp;</td><td class=\"noborder\">active UP </td>"
2450 "<td class=\"backup_up\">&nbsp;</td><td class=\"noborder\">backup UP </td>"
2451 "</tr><tr>\n"
2452 "<td class=\"active_going_down\"></td><td class=\"noborder\">active UP, going down </td>"
2453 "<td class=\"backup_going_down\"></td><td class=\"noborder\">backup UP, going down </td>"
2454 "</tr><tr>\n"
2455 "<td class=\"active_going_up\"></td><td class=\"noborder\">active DOWN, going up </td>"
2456 "<td class=\"backup_going_up\"></td><td class=\"noborder\">backup DOWN, going up </td>"
2457 "</tr><tr>\n"
2458 "<td class=\"active_down\"></td><td class=\"noborder\">active or backup DOWN &nbsp;</td>"
2459 "<td class=\"active_no_check\"></td><td class=\"noborder\">not checked </td>"
2460 "</tr><tr>\n"
2461 "<td class=\"maintain\"></td><td class=\"noborder\" colspan=\"3\">active or backup DOWN for maintenance (MAINT) &nbsp;</td>"
2462 "</tr><tr>\n"
2463 "<td class=\"active_draining\"></td><td class=\"noborder\" colspan=\"3\">active or backup SOFT STOPPED for maintenance &nbsp;</td>"
2464 "</tr></table>\n"
2465 "Note: \"NOLB\"/\"DRAIN\" = UP with load-balancing disabled."
2466 "</td>"
2467 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
2468 "<b>Display option:</b><ul style=\"margin-top: 0.25em;\">"
2469 "",
Willy Tarreau676c29e2019-10-09 10:50:01 +02002470 (appctx->ctx.stats.flags & STAT_HIDEVER) ? "" : (stats_version_string),
2471 pid, (appctx->ctx.stats.flags & STAT_SHNODE) ? " on " : "",
2472 (appctx->ctx.stats.flags & STAT_SHNODE) ? (uri->node ? uri->node : global.node) : "",
2473 (appctx->ctx.stats.flags & STAT_SHDESC) ? ": " : "",
2474 (appctx->ctx.stats.flags & STAT_SHDESC) ? (uri->desc ? uri->desc : global.desc) : "",
Yves Lafon95317282018-02-26 11:10:37 +01002475 pid, relative_pid, global.nbproc, global.nbthread,
William Lallemand74c24fb2016-11-21 17:18:36 +01002476 up / 86400, (up % 86400) / 3600,
2477 (up % 3600) / 60, (up % 60),
2478 global.rlimit_memmax ? ultoa(global.rlimit_memmax) : "unlimited",
2479 global.rlimit_memmax ? " MB" : "",
2480 global.rlimit_nofile,
2481 global.maxsock, global.maxconn, global.maxpipes,
2482 actconn, pipes_used, pipes_used+pipes_free, read_freq_ctr(&global.conn_per_sec),
Willy Tarreau1713c032019-05-23 12:23:55 +02002483 bps >= 1000000000UL ? (bps / 1000000000.0) : bps >= 1000000UL ? (bps / 1000000.0) : (bps / 1000.0),
2484 bps >= 1000000000UL ? 'G' : bps >= 1000000UL ? 'M' : 'k',
Willy Tarreau81036f22019-05-20 19:24:50 +02002485 tasks_run_queue_cur, nb_tasks_cur, ti->idle_pct
William Lallemand74c24fb2016-11-21 17:18:36 +01002486 );
2487
2488 /* scope_txt = search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
Christopher Fauleted7a0662019-01-14 11:07:34 +01002489 memcpy(scope_txt, scope_ptr, appctx->ctx.stats.scope_len);
William Lallemand74c24fb2016-11-21 17:18:36 +01002490 scope_txt[appctx->ctx.stats.scope_len] = '\0';
2491
2492 chunk_appendf(&trash,
2493 "<li><form method=\"GET\">Scope : <input value=\"%s\" name=\"" STAT_SCOPE_INPUT_NAME "\" size=\"8\" maxlength=\"%d\" tabindex=\"1\"/></form>\n",
2494 (appctx->ctx.stats.scope_len > 0) ? scope_txt : "",
2495 STAT_SCOPE_TXT_MAXLEN);
2496
2497 /* scope_txt = search pattern + search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
2498 scope_txt[0] = 0;
2499 if (appctx->ctx.stats.scope_len) {
2500 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Christopher Fauleted7a0662019-01-14 11:07:34 +01002501 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), scope_ptr, appctx->ctx.stats.scope_len);
William Lallemand74c24fb2016-11-21 17:18:36 +01002502 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
2503 }
2504
2505 if (appctx->ctx.stats.flags & STAT_HIDE_DOWN)
2506 chunk_appendf(&trash,
2507 "<li><a href=\"%s%s%s%s\">Show all servers</a><br>\n",
2508 uri->uri_prefix,
2509 "",
2510 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2511 scope_txt);
2512 else
2513 chunk_appendf(&trash,
2514 "<li><a href=\"%s%s%s%s\">Hide 'DOWN' servers</a><br>\n",
2515 uri->uri_prefix,
2516 ";up",
2517 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2518 scope_txt);
2519
2520 if (uri->refresh > 0) {
2521 if (appctx->ctx.stats.flags & STAT_NO_REFRESH)
2522 chunk_appendf(&trash,
2523 "<li><a href=\"%s%s%s%s\">Enable refresh</a><br>\n",
2524 uri->uri_prefix,
2525 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2526 "",
2527 scope_txt);
2528 else
2529 chunk_appendf(&trash,
2530 "<li><a href=\"%s%s%s%s\">Disable refresh</a><br>\n",
2531 uri->uri_prefix,
2532 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2533 ";norefresh",
2534 scope_txt);
2535 }
2536
2537 chunk_appendf(&trash,
2538 "<li><a href=\"%s%s%s%s\">Refresh now</a><br>\n",
2539 uri->uri_prefix,
2540 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2541 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2542 scope_txt);
2543
2544 chunk_appendf(&trash,
2545 "<li><a href=\"%s;csv%s%s\">CSV export</a><br>\n",
2546 uri->uri_prefix,
2547 (uri->refresh > 0) ? ";norefresh" : "",
2548 scope_txt);
2549
2550 chunk_appendf(&trash,
Christopher Faulet6338a082019-09-09 15:50:54 +02002551 "<li><a href=\"%s;json%s%s\">JSON export</a> (<a href=\"%s;json-schema\">schema</a>)<br>\n",
2552 uri->uri_prefix,
2553 (uri->refresh > 0) ? ";norefresh" : "",
2554 scope_txt, uri->uri_prefix);
2555
2556 chunk_appendf(&trash,
William Lallemand74c24fb2016-11-21 17:18:36 +01002557 "</ul></td>"
2558 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
2559 "<b>External resources:</b><ul style=\"margin-top: 0.25em;\">\n"
2560 "<li><a href=\"" PRODUCT_URL "\">Primary site</a><br>\n"
2561 "<li><a href=\"" PRODUCT_URL_UPD "\">Updates (v" PRODUCT_BRANCH ")</a><br>\n"
2562 "<li><a href=\"" PRODUCT_URL_DOC "\">Online manual</a><br>\n"
2563 "</ul>"
2564 "</td>"
2565 "</tr></table>\n"
2566 ""
2567 );
2568
2569 if (appctx->ctx.stats.st_code) {
2570 switch (appctx->ctx.stats.st_code) {
2571 case STAT_STATUS_DONE:
2572 chunk_appendf(&trash,
2573 "<p><div class=active_up>"
2574 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2575 "Action processed successfully."
2576 "</div>\n", uri->uri_prefix,
2577 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2578 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2579 scope_txt);
2580 break;
2581 case STAT_STATUS_NONE:
2582 chunk_appendf(&trash,
2583 "<p><div class=active_going_down>"
2584 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2585 "Nothing has changed."
2586 "</div>\n", uri->uri_prefix,
2587 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2588 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2589 scope_txt);
2590 break;
2591 case STAT_STATUS_PART:
2592 chunk_appendf(&trash,
2593 "<p><div class=active_going_down>"
2594 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2595 "Action partially processed.<br>"
2596 "Some server names are probably unknown or ambiguous (duplicated names in the backend)."
2597 "</div>\n", uri->uri_prefix,
2598 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2599 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2600 scope_txt);
2601 break;
2602 case STAT_STATUS_ERRP:
2603 chunk_appendf(&trash,
2604 "<p><div class=active_down>"
2605 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2606 "Action not processed because of invalid parameters."
2607 "<ul>"
2608 "<li>The action is maybe unknown.</li>"
Christopher Faulet2f9a41d2019-02-27 15:30:57 +01002609 "<li>Invalid key parameter (empty or too long).</li>"
William Lallemand74c24fb2016-11-21 17:18:36 +01002610 "<li>The backend name is probably unknown or ambiguous (duplicated names).</li>"
2611 "<li>Some server names are probably unknown or ambiguous (duplicated names in the backend).</li>"
2612 "</ul>"
2613 "</div>\n", uri->uri_prefix,
2614 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2615 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2616 scope_txt);
2617 break;
2618 case STAT_STATUS_EXCD:
2619 chunk_appendf(&trash,
2620 "<p><div class=active_down>"
2621 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2622 "<b>Action not processed : the buffer couldn't store all the data.<br>"
2623 "You should retry with less servers at a time.</b>"
2624 "</div>\n", uri->uri_prefix,
2625 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2626 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2627 scope_txt);
2628 break;
2629 case STAT_STATUS_DENY:
2630 chunk_appendf(&trash,
2631 "<p><div class=active_down>"
2632 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2633 "<b>Action denied.</b>"
2634 "</div>\n", uri->uri_prefix,
2635 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2636 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2637 scope_txt);
2638 break;
Christopher Faulet3c2ecf72019-02-27 16:41:27 +01002639 case STAT_STATUS_IVAL:
2640 chunk_appendf(&trash,
2641 "<p><div class=active_down>"
2642 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2643 "<b>Invalid requests (unsupported method or chunked encoded request).</b>"
2644 "</div>\n", uri->uri_prefix,
2645 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2646 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2647 scope_txt);
2648 break;
William Lallemand74c24fb2016-11-21 17:18:36 +01002649 default:
2650 chunk_appendf(&trash,
2651 "<p><div class=active_no_check>"
2652 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2653 "Unexpected result."
2654 "</div>\n", uri->uri_prefix,
2655 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2656 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2657 scope_txt);
2658 }
2659 chunk_appendf(&trash, "<p>\n");
2660 }
2661}
2662
2663/* Dumps the HTML stats trailer block to the trash. The caller is responsible
2664 * for clearing the trash if needed.
2665 */
2666static void stats_dump_html_end()
2667{
2668 chunk_appendf(&trash, "</body></html>\n");
2669}
2670
Simon Horman05ee2132017-01-04 09:37:25 +01002671/* Dumps the stats JSON header to the trash buffer which. The caller is responsible
2672 * for clearing it if needed.
2673 */
2674static void stats_dump_json_header()
2675{
2676 chunk_strcat(&trash, "[");
2677}
2678
2679
2680/* Dumps the JSON stats trailer block to the trash. The caller is responsible
2681 * for clearing the trash if needed.
2682 */
2683static void stats_dump_json_end()
2684{
2685 chunk_strcat(&trash, "]");
2686}
2687
William Lallemand74c24fb2016-11-21 17:18:36 +01002688/* This function dumps statistics onto the stream interface's read buffer in
2689 * either CSV or HTML format. <uri> contains some HTML-specific parameters that
2690 * are ignored for CSV format (hence <uri> may be NULL there). It returns 0 if
2691 * it had to stop writing data and an I/O is needed, 1 if the dump is finished
2692 * and the stream must be closed, or -1 in case of any error. This function is
2693 * used by both the CLI and the HTTP handlers.
2694 */
Christopher Fauletef779222018-10-31 08:47:01 +01002695static int stats_dump_stat_to_buffer(struct stream_interface *si, struct htx *htx,
2696 struct uri_auth *uri)
William Lallemand74c24fb2016-11-21 17:18:36 +01002697{
2698 struct appctx *appctx = __objt_appctx(si->end);
2699 struct channel *rep = si_ic(si);
2700 struct proxy *px;
2701
2702 chunk_reset(&trash);
2703
2704 switch (appctx->st2) {
2705 case STAT_ST_INIT:
2706 appctx->st2 = STAT_ST_HEAD; /* let's start producing data */
2707 /* fall through */
2708
2709 case STAT_ST_HEAD:
2710 if (appctx->ctx.stats.flags & STAT_FMT_HTML)
Willy Tarreau676c29e2019-10-09 10:50:01 +02002711 stats_dump_html_head(appctx, uri);
Christopher Faulet6338a082019-09-09 15:50:54 +02002712 else if (appctx->ctx.stats.flags & STAT_JSON_SCHM)
2713 stats_dump_json_schema(&trash);
Simon Horman05ee2132017-01-04 09:37:25 +01002714 else if (appctx->ctx.stats.flags & STAT_FMT_JSON)
Willy Tarreau9d7fb632017-04-11 07:53:04 +02002715 stats_dump_json_header();
William Lallemand74c24fb2016-11-21 17:18:36 +01002716 else if (!(appctx->ctx.stats.flags & STAT_FMT_TYPED))
2717 stats_dump_csv_header();
2718
Christopher Fauletef779222018-10-31 08:47:01 +01002719 if (!stats_putchk(rep, htx, &trash))
2720 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002721
Christopher Faulet6338a082019-09-09 15:50:54 +02002722 if (appctx->ctx.stats.flags & STAT_JSON_SCHM) {
2723 appctx->st2 = STAT_ST_FIN;
2724 return 1;
2725 }
William Lallemand74c24fb2016-11-21 17:18:36 +01002726 appctx->st2 = STAT_ST_INFO;
2727 /* fall through */
2728
2729 case STAT_ST_INFO:
2730 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
2731 stats_dump_html_info(si, uri);
Christopher Fauletef779222018-10-31 08:47:01 +01002732 if (!stats_putchk(rep, htx, &trash))
2733 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002734 }
2735
Olivier Houchardfbc74e82017-11-24 16:54:05 +01002736 appctx->ctx.stats.px = proxies_list;
William Lallemand74c24fb2016-11-21 17:18:36 +01002737 appctx->ctx.stats.px_st = STAT_PX_ST_INIT;
2738 appctx->st2 = STAT_ST_LIST;
2739 /* fall through */
2740
2741 case STAT_ST_LIST:
2742 /* dump proxies */
2743 while (appctx->ctx.stats.px) {
Christopher Fauletef779222018-10-31 08:47:01 +01002744 if (htx) {
2745 if (htx_almost_full(htx))
2746 goto full;
2747 }
2748 else {
2749 if (buffer_almost_full(&rep->buf))
2750 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002751 }
2752
2753 px = appctx->ctx.stats.px;
2754 /* skip the disabled proxies, global frontend and non-networked ones */
2755 if (px->state != PR_STSTOPPED && px->uuid > 0 && (px->cap & (PR_CAP_FE | PR_CAP_BE)))
Christopher Fauletef779222018-10-31 08:47:01 +01002756 if (stats_dump_proxy_to_buffer(si, htx, px, uri) == 0)
William Lallemand74c24fb2016-11-21 17:18:36 +01002757 return 0;
2758
2759 appctx->ctx.stats.px = px->next;
2760 appctx->ctx.stats.px_st = STAT_PX_ST_INIT;
2761 }
2762 /* here, we just have reached the last proxy */
2763
2764 appctx->st2 = STAT_ST_END;
2765 /* fall through */
2766
2767 case STAT_ST_END:
Simon Horman05ee2132017-01-04 09:37:25 +01002768 if (appctx->ctx.stats.flags & (STAT_FMT_HTML|STAT_FMT_JSON)) {
2769 if (appctx->ctx.stats.flags & STAT_FMT_HTML)
2770 stats_dump_html_end();
2771 else
2772 stats_dump_json_end();
Christopher Fauletef779222018-10-31 08:47:01 +01002773 if (!stats_putchk(rep, htx, &trash))
2774 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002775 }
2776
2777 appctx->st2 = STAT_ST_FIN;
2778 /* fall through */
2779
2780 case STAT_ST_FIN:
2781 return 1;
2782
2783 default:
2784 /* unknown state ! */
2785 appctx->st2 = STAT_ST_FIN;
2786 return -1;
2787 }
Christopher Fauletef779222018-10-31 08:47:01 +01002788
2789 full:
2790 si_rx_room_blk(si);
2791 return 0;
2792
William Lallemand74c24fb2016-11-21 17:18:36 +01002793}
2794
2795/* We reached the stats page through a POST request. The appctx is
2796 * expected to have already been allocated by the caller.
2797 * Parse the posted data and enable/disable servers if necessary.
2798 * Returns 1 if request was parsed or zero if it needs more data.
2799 */
2800static int stats_process_http_post(struct stream_interface *si)
2801{
2802 struct stream *s = si_strm(si);
2803 struct appctx *appctx = objt_appctx(si->end);
2804
2805 struct proxy *px = NULL;
2806 struct server *sv = NULL;
2807
2808 char key[LINESIZE];
2809 int action = ST_ADM_ACTION_NONE;
2810 int reprocess = 0;
2811
2812 int total_servers = 0;
2813 int altered_servers = 0;
2814
2815 char *first_param, *cur_param, *next_param, *end_params;
2816 char *st_cur_param = NULL;
2817 char *st_next_param = NULL;
2818
Christopher Fauleta3618372018-12-13 21:59:56 +01002819 struct buffer *temp = get_trash_chunk();
William Lallemand74c24fb2016-11-21 17:18:36 +01002820
Christopher Fauletb7f88902019-07-15 21:56:43 +02002821 struct htx *htx = htxbuf(&s->req.buf);
2822 struct htx_blk *blk;
Christopher Fauleta3618372018-12-13 21:59:56 +01002823
Christopher Fauletb7f88902019-07-15 21:56:43 +02002824 /* we need more data */
2825 if (s->txn->req.msg_state < HTTP_MSG_DONE) {
2826 /* check if we can receive more */
2827 if (htx_free_data_space(htx) <= global.tune.maxrewrite) {
2828 appctx->ctx.stats.st_code = STAT_STATUS_EXCD;
2829 goto out;
Christopher Fauleta3618372018-12-13 21:59:56 +01002830 }
Christopher Fauletb7f88902019-07-15 21:56:43 +02002831 goto wait;
2832 }
Christopher Fauleta3618372018-12-13 21:59:56 +01002833
Christopher Fauletb7f88902019-07-15 21:56:43 +02002834 /* The request was fully received. Copy data */
2835 blk = htx_get_head_blk(htx);
2836 while (blk) {
2837 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Fauleta3618372018-12-13 21:59:56 +01002838
Christopher Fauletb7f88902019-07-15 21:56:43 +02002839 if (type == HTX_BLK_EOM || type == HTX_BLK_TLR || type == HTX_BLK_EOT)
2840 break;
2841 if (type == HTX_BLK_DATA) {
2842 struct ist v = htx_get_blk_value(htx, blk);
Christopher Fauleta3618372018-12-13 21:59:56 +01002843
Christopher Fauletb7f88902019-07-15 21:56:43 +02002844 if (!chunk_memcat(temp, v.ptr, v.len)) {
Christopher Faulet2f9a41d2019-02-27 15:30:57 +01002845 appctx->ctx.stats.st_code = STAT_STATUS_EXCD;
2846 goto out;
2847 }
Christopher Fauleta3618372018-12-13 21:59:56 +01002848 }
Christopher Fauletb7f88902019-07-15 21:56:43 +02002849 blk = htx_get_next_blk(htx, blk);
William Lallemand74c24fb2016-11-21 17:18:36 +01002850 }
2851
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002852 first_param = temp->area;
Christopher Fauleta3618372018-12-13 21:59:56 +01002853 end_params = temp->area + temp->data;
William Lallemand74c24fb2016-11-21 17:18:36 +01002854 cur_param = next_param = end_params;
2855 *end_params = '\0';
2856
2857 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
2858
2859 /*
2860 * Parse the parameters in reverse order to only store the last value.
2861 * From the html form, the backend and the action are at the end.
2862 */
2863 while (cur_param > first_param) {
2864 char *value;
2865 int poffset, plen;
2866
2867 cur_param--;
2868
2869 if ((*cur_param == '&') || (cur_param == first_param)) {
2870 reprocess_servers:
2871 /* Parse the key */
2872 poffset = (cur_param != first_param ? 1 : 0);
2873 plen = next_param - cur_param + (cur_param == first_param ? 1 : 0);
2874 if ((plen > 0) && (plen <= sizeof(key))) {
2875 strncpy(key, cur_param + poffset, plen);
2876 key[plen - 1] = '\0';
2877 } else {
Christopher Faulet2f9a41d2019-02-27 15:30:57 +01002878 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
William Lallemand74c24fb2016-11-21 17:18:36 +01002879 goto out;
2880 }
2881
2882 /* Parse the value */
2883 value = key;
2884 while (*value != '\0' && *value != '=') {
2885 value++;
2886 }
2887 if (*value == '=') {
2888 /* Ok, a value is found, we can mark the end of the key */
2889 *value++ = '\0';
2890 }
2891 if (url_decode(key) < 0 || url_decode(value) < 0)
2892 break;
2893
2894 /* Now we can check the key to see what to do */
2895 if (!px && (strcmp(key, "b") == 0)) {
2896 if ((px = proxy_be_by_name(value)) == NULL) {
2897 /* the backend name is unknown or ambiguous (duplicate names) */
2898 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
2899 goto out;
2900 }
2901 }
2902 else if (!action && (strcmp(key, "action") == 0)) {
2903 if (strcmp(value, "ready") == 0) {
2904 action = ST_ADM_ACTION_READY;
2905 }
2906 else if (strcmp(value, "drain") == 0) {
2907 action = ST_ADM_ACTION_DRAIN;
2908 }
2909 else if (strcmp(value, "maint") == 0) {
2910 action = ST_ADM_ACTION_MAINT;
2911 }
2912 else if (strcmp(value, "shutdown") == 0) {
2913 action = ST_ADM_ACTION_SHUTDOWN;
2914 }
2915 else if (strcmp(value, "dhlth") == 0) {
2916 action = ST_ADM_ACTION_DHLTH;
2917 }
2918 else if (strcmp(value, "ehlth") == 0) {
2919 action = ST_ADM_ACTION_EHLTH;
2920 }
2921 else if (strcmp(value, "hrunn") == 0) {
2922 action = ST_ADM_ACTION_HRUNN;
2923 }
2924 else if (strcmp(value, "hnolb") == 0) {
2925 action = ST_ADM_ACTION_HNOLB;
2926 }
2927 else if (strcmp(value, "hdown") == 0) {
2928 action = ST_ADM_ACTION_HDOWN;
2929 }
2930 else if (strcmp(value, "dagent") == 0) {
2931 action = ST_ADM_ACTION_DAGENT;
2932 }
2933 else if (strcmp(value, "eagent") == 0) {
2934 action = ST_ADM_ACTION_EAGENT;
2935 }
2936 else if (strcmp(value, "arunn") == 0) {
2937 action = ST_ADM_ACTION_ARUNN;
2938 }
2939 else if (strcmp(value, "adown") == 0) {
2940 action = ST_ADM_ACTION_ADOWN;
2941 }
2942 /* else these are the old supported methods */
2943 else if (strcmp(value, "disable") == 0) {
2944 action = ST_ADM_ACTION_DISABLE;
2945 }
2946 else if (strcmp(value, "enable") == 0) {
2947 action = ST_ADM_ACTION_ENABLE;
2948 }
2949 else if (strcmp(value, "stop") == 0) {
2950 action = ST_ADM_ACTION_STOP;
2951 }
2952 else if (strcmp(value, "start") == 0) {
2953 action = ST_ADM_ACTION_START;
2954 }
2955 else {
2956 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
2957 goto out;
2958 }
2959 }
2960 else if (strcmp(key, "s") == 0) {
2961 if (!(px && action)) {
2962 /*
2963 * Indicates that we'll need to reprocess the parameters
2964 * as soon as backend and action are known
2965 */
2966 if (!reprocess) {
2967 st_cur_param = cur_param;
2968 st_next_param = next_param;
2969 }
2970 reprocess = 1;
2971 }
2972 else if ((sv = findserver(px, value)) != NULL) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002973 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
William Lallemand74c24fb2016-11-21 17:18:36 +01002974 switch (action) {
2975 case ST_ADM_ACTION_DISABLE:
Emeric Brun52a91d32017-08-31 14:41:55 +02002976 if (!(sv->cur_admin & SRV_ADMF_FMAINT)) {
William Lallemand74c24fb2016-11-21 17:18:36 +01002977 altered_servers++;
2978 total_servers++;
2979 srv_set_admin_flag(sv, SRV_ADMF_FMAINT, "'disable' on stats page");
2980 }
2981 break;
2982 case ST_ADM_ACTION_ENABLE:
Emeric Brun52a91d32017-08-31 14:41:55 +02002983 if (sv->cur_admin & SRV_ADMF_FMAINT) {
William Lallemand74c24fb2016-11-21 17:18:36 +01002984 altered_servers++;
2985 total_servers++;
2986 srv_clr_admin_flag(sv, SRV_ADMF_FMAINT);
2987 }
2988 break;
2989 case ST_ADM_ACTION_STOP:
Emeric Brun52a91d32017-08-31 14:41:55 +02002990 if (!(sv->cur_admin & SRV_ADMF_FDRAIN)) {
William Lallemand74c24fb2016-11-21 17:18:36 +01002991 srv_set_admin_flag(sv, SRV_ADMF_FDRAIN, "'stop' on stats page");
2992 altered_servers++;
2993 total_servers++;
2994 }
2995 break;
2996 case ST_ADM_ACTION_START:
Emeric Brun52a91d32017-08-31 14:41:55 +02002997 if (sv->cur_admin & SRV_ADMF_FDRAIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +01002998 srv_clr_admin_flag(sv, SRV_ADMF_FDRAIN);
2999 altered_servers++;
3000 total_servers++;
3001 }
3002 break;
3003 case ST_ADM_ACTION_DHLTH:
3004 if (sv->check.state & CHK_ST_CONFIGURED) {
3005 sv->check.state &= ~CHK_ST_ENABLED;
3006 altered_servers++;
3007 total_servers++;
3008 }
3009 break;
3010 case ST_ADM_ACTION_EHLTH:
3011 if (sv->check.state & CHK_ST_CONFIGURED) {
3012 sv->check.state |= CHK_ST_ENABLED;
3013 altered_servers++;
3014 total_servers++;
3015 }
3016 break;
3017 case ST_ADM_ACTION_HRUNN:
3018 if (!(sv->track)) {
3019 sv->check.health = sv->check.rise + sv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02003020 srv_set_running(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01003021 altered_servers++;
3022 total_servers++;
3023 }
3024 break;
3025 case ST_ADM_ACTION_HNOLB:
3026 if (!(sv->track)) {
3027 sv->check.health = sv->check.rise + sv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02003028 srv_set_stopping(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01003029 altered_servers++;
3030 total_servers++;
3031 }
3032 break;
3033 case ST_ADM_ACTION_HDOWN:
3034 if (!(sv->track)) {
3035 sv->check.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02003036 srv_set_stopped(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01003037 altered_servers++;
3038 total_servers++;
3039 }
3040 break;
3041 case ST_ADM_ACTION_DAGENT:
3042 if (sv->agent.state & CHK_ST_CONFIGURED) {
3043 sv->agent.state &= ~CHK_ST_ENABLED;
3044 altered_servers++;
3045 total_servers++;
3046 }
3047 break;
3048 case ST_ADM_ACTION_EAGENT:
3049 if (sv->agent.state & CHK_ST_CONFIGURED) {
3050 sv->agent.state |= CHK_ST_ENABLED;
3051 altered_servers++;
3052 total_servers++;
3053 }
3054 break;
3055 case ST_ADM_ACTION_ARUNN:
3056 if (sv->agent.state & CHK_ST_ENABLED) {
3057 sv->agent.health = sv->agent.rise + sv->agent.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02003058 srv_set_running(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01003059 altered_servers++;
3060 total_servers++;
3061 }
3062 break;
3063 case ST_ADM_ACTION_ADOWN:
3064 if (sv->agent.state & CHK_ST_ENABLED) {
3065 sv->agent.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02003066 srv_set_stopped(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01003067 altered_servers++;
3068 total_servers++;
3069 }
3070 break;
3071 case ST_ADM_ACTION_READY:
3072 srv_adm_set_ready(sv);
3073 altered_servers++;
3074 total_servers++;
3075 break;
3076 case ST_ADM_ACTION_DRAIN:
3077 srv_adm_set_drain(sv);
3078 altered_servers++;
3079 total_servers++;
3080 break;
3081 case ST_ADM_ACTION_MAINT:
3082 srv_adm_set_maint(sv);
3083 altered_servers++;
3084 total_servers++;
3085 break;
3086 case ST_ADM_ACTION_SHUTDOWN:
3087 if (px->state != PR_STSTOPPED) {
Willy Tarreauaf7ea812019-11-14 16:42:04 +01003088 srv_shutdown_streams(sv, SF_ERR_KILLED);
William Lallemand74c24fb2016-11-21 17:18:36 +01003089 altered_servers++;
3090 total_servers++;
3091 }
3092 break;
3093 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003094 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
William Lallemand74c24fb2016-11-21 17:18:36 +01003095 } else {
3096 /* the server name is unknown or ambiguous (duplicate names) */
3097 total_servers++;
3098 }
3099 }
3100 if (reprocess && px && action) {
3101 /* Now, we know the backend and the action chosen by the user.
3102 * We can safely restart from the first server parameter
3103 * to reprocess them
3104 */
3105 cur_param = st_cur_param;
3106 next_param = st_next_param;
3107 reprocess = 0;
3108 goto reprocess_servers;
3109 }
3110
3111 next_param = cur_param;
3112 }
3113 }
3114
3115 if (total_servers == 0) {
3116 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
3117 }
3118 else if (altered_servers == 0) {
3119 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
3120 }
3121 else if (altered_servers == total_servers) {
3122 appctx->ctx.stats.st_code = STAT_STATUS_DONE;
3123 }
3124 else {
3125 appctx->ctx.stats.st_code = STAT_STATUS_PART;
3126 }
3127 out:
3128 return 1;
Christopher Faulet2f9a41d2019-02-27 15:30:57 +01003129 wait:
3130 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
3131 return 0;
Christopher Fauletef779222018-10-31 08:47:01 +01003132}
3133
3134
Christopher Fauletb7f88902019-07-15 21:56:43 +02003135static int stats_send_http_headers(struct stream_interface *si, struct htx *htx)
Christopher Fauletef779222018-10-31 08:47:01 +01003136{
3137 struct stream *s = si_strm(si);
3138 struct uri_auth *uri = s->be->uri_auth;
3139 struct appctx *appctx = __objt_appctx(si->end);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003140 struct htx_sl *sl;
3141 unsigned int flags;
Christopher Fauletef779222018-10-31 08:47:01 +01003142
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003143 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);
3144 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), ist("200"), ist("OK"));
3145 if (!sl)
Christopher Fauletef779222018-10-31 08:47:01 +01003146 goto full;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003147 sl->info.res.status = 200;
Christopher Fauletef779222018-10-31 08:47:01 +01003148
Christopher Fauletb829f4c2019-03-29 16:13:55 +01003149 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")))
Christopher Fauletef779222018-10-31 08:47:01 +01003150 goto full;
3151 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
3152 if (!htx_add_header(htx, ist("Content-Type"), ist("text/html")))
3153 goto full;
3154 }
Christopher Faulet6338a082019-09-09 15:50:54 +02003155 else if (appctx->ctx.stats.flags & (STAT_FMT_JSON|STAT_JSON_SCHM)) {
3156 if (!htx_add_header(htx, ist("Content-Type"), ist("application/json")))
3157 goto full;
3158 }
Christopher Fauletef779222018-10-31 08:47:01 +01003159 else {
3160 if (!htx_add_header(htx, ist("Content-Type"), ist("text/plain")))
3161 goto full;
3162 }
3163
3164 if (uri->refresh > 0 && !(appctx->ctx.stats.flags & STAT_NO_REFRESH)) {
3165 const char *refresh = U2A(uri->refresh);
3166 if (!htx_add_header(htx, ist("Refresh"), ist2(refresh, strlen(refresh))))
3167 goto full;
3168 }
3169
3170 if (appctx->ctx.stats.flags & STAT_CHUNKED) {
3171 if (!htx_add_header(htx, ist("Transfer-Encoding"), ist("chunked")))
3172 goto full;
3173 }
3174
3175 if (!htx_add_endof(htx, HTX_BLK_EOH))
3176 goto full;
3177
Christopher Faulet1e2d6362019-02-27 16:28:48 +01003178 channel_add_input(&s->res, htx->data);
Christopher Fauletef779222018-10-31 08:47:01 +01003179 return 1;
3180
3181 full:
3182 htx_reset(htx);
3183 si_rx_room_blk(si);
3184 return 0;
William Lallemand74c24fb2016-11-21 17:18:36 +01003185}
3186
Christopher Fauletef779222018-10-31 08:47:01 +01003187
Christopher Fauletb7f88902019-07-15 21:56:43 +02003188static int stats_send_http_redirect(struct stream_interface *si, struct htx *htx)
Christopher Fauletef779222018-10-31 08:47:01 +01003189{
3190 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
3191 struct stream *s = si_strm(si);
3192 struct uri_auth *uri = s->be->uri_auth;
3193 struct appctx *appctx = __objt_appctx(si->end);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003194 struct htx_sl *sl;
3195 unsigned int flags;
Christopher Fauletef779222018-10-31 08:47:01 +01003196
3197 /* scope_txt = search pattern + search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
3198 scope_txt[0] = 0;
3199 if (appctx->ctx.stats.scope_len) {
Christopher Fauleted7a0662019-01-14 11:07:34 +01003200 const char *scope_ptr = stats_scope_ptr(appctx, si);
3201
Christopher Fauletef779222018-10-31 08:47:01 +01003202 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Christopher Fauleted7a0662019-01-14 11:07:34 +01003203 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), scope_ptr, appctx->ctx.stats.scope_len);
Christopher Fauletef779222018-10-31 08:47:01 +01003204 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
3205 }
3206
3207 /* We don't want to land on the posted stats page because a refresh will
3208 * repost the data. We don't want this to happen on accident so we redirect
3209 * the browse to the stats page with a GET.
3210 */
3211 chunk_printf(&trash, "%s;st=%s%s%s%s",
3212 uri->uri_prefix,
3213 ((appctx->ctx.stats.st_code > STAT_STATUS_INIT) &&
3214 (appctx->ctx.stats.st_code < STAT_STATUS_SIZE) &&
3215 stat_status_codes[appctx->ctx.stats.st_code]) ?
3216 stat_status_codes[appctx->ctx.stats.st_code] :
3217 stat_status_codes[STAT_STATUS_UNKN],
3218 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3219 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
3220 scope_txt);
3221
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003222 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_CHNK);
3223 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), ist("303"), ist("See Other"));
3224 if (!sl)
Christopher Fauletef779222018-10-31 08:47:01 +01003225 goto full;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003226 sl->info.res.status = 303;
Christopher Fauletef779222018-10-31 08:47:01 +01003227
3228 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")) ||
Christopher Fauletef779222018-10-31 08:47:01 +01003229 !htx_add_header(htx, ist("Content-Type"), ist("text/plain")) ||
3230 !htx_add_header(htx, ist("Content-Length"), ist("0")) ||
3231 !htx_add_header(htx, ist("Location"), ist2(trash.area, trash.data)))
3232 goto full;
3233
3234 if (!htx_add_endof(htx, HTX_BLK_EOH))
3235 goto full;
3236
Christopher Faulet1e2d6362019-02-27 16:28:48 +01003237 channel_add_input(&s->res, htx->data);
Christopher Fauletef779222018-10-31 08:47:01 +01003238 return 1;
3239
3240full:
3241 htx_reset(htx);
3242 si_rx_room_blk(si);
3243 return 0;
3244}
William Lallemand74c24fb2016-11-21 17:18:36 +01003245
Simon Horman05ee2132017-01-04 09:37:25 +01003246
William Lallemand74c24fb2016-11-21 17:18:36 +01003247/* This I/O handler runs as an applet embedded in a stream interface. It is
3248 * used to send HTTP stats over a TCP socket. The mechanism is very simple.
3249 * appctx->st0 contains the operation in progress (dump, done). The handler
3250 * automatically unregisters itself once transfer is complete.
3251 */
Christopher Fauletb7f88902019-07-15 21:56:43 +02003252static void http_stats_io_handler(struct appctx *appctx)
Christopher Fauletef779222018-10-31 08:47:01 +01003253{
3254 struct stream_interface *si = appctx->owner;
3255 struct stream *s = si_strm(si);
3256 struct channel *req = si_oc(si);
3257 struct channel *res = si_ic(si);
3258 struct htx *req_htx, *res_htx;
3259
3260 res_htx = htx_from_buf(&res->buf);
3261
3262 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
3263 goto out;
3264
3265 /* Check if the input buffer is avalaible. */
3266 if (!b_size(&res->buf)) {
3267 si_rx_room_blk(si);
3268 goto out;
3269 }
3270
3271 /* check that the output is not closed */
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003272 if (res->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_SHUTR))
3273 appctx->st0 = STAT_HTTP_END;
Christopher Fauletef779222018-10-31 08:47:01 +01003274
3275 /* all states are processed in sequence */
3276 if (appctx->st0 == STAT_HTTP_HEAD) {
Christopher Fauletb7f88902019-07-15 21:56:43 +02003277 if (stats_send_http_headers(si, res_htx)) {
Christopher Fauletef779222018-10-31 08:47:01 +01003278 if (s->txn->meth == HTTP_METH_HEAD)
3279 appctx->st0 = STAT_HTTP_DONE;
3280 else
3281 appctx->st0 = STAT_HTTP_DUMP;
3282 }
3283 }
3284
3285 if (appctx->st0 == STAT_HTTP_DUMP) {
3286 if (stats_dump_stat_to_buffer(si, res_htx, s->be->uri_auth))
3287 appctx->st0 = STAT_HTTP_DONE;
3288 }
3289
3290 if (appctx->st0 == STAT_HTTP_POST) {
3291 if (stats_process_http_post(si))
3292 appctx->st0 = STAT_HTTP_LAST;
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003293 else if (req->flags & CF_SHUTR)
Christopher Fauletef779222018-10-31 08:47:01 +01003294 appctx->st0 = STAT_HTTP_DONE;
3295 }
3296
3297 if (appctx->st0 == STAT_HTTP_LAST) {
Christopher Fauletb7f88902019-07-15 21:56:43 +02003298 if (stats_send_http_redirect(si, res_htx))
Christopher Fauletef779222018-10-31 08:47:01 +01003299 appctx->st0 = STAT_HTTP_DONE;
3300 }
3301
3302 if (appctx->st0 == STAT_HTTP_DONE) {
Christopher Faulet54b5e212019-06-04 10:08:28 +02003303 /* Don't add TLR because mux-h1 will take care of it */
Christopher Fauletef779222018-10-31 08:47:01 +01003304 if (!htx_add_endof(res_htx, HTX_BLK_EOM)) {
3305 si_rx_room_blk(si);
3306 goto out;
3307 }
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003308 channel_add_input(&s->res, 1);
3309 appctx->st0 = STAT_HTTP_END;
Christopher Fauletef779222018-10-31 08:47:01 +01003310 }
3311
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003312 if (appctx->st0 == STAT_HTTP_END) {
3313 if (!(res->flags & CF_SHUTR)) {
Christopher Fauletef779222018-10-31 08:47:01 +01003314 res->flags |= CF_READ_NULL;
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003315 si_shutr(si);
3316 }
3317
3318 /* eat the whole request */
3319 if (co_data(req)) {
3320 req_htx = htx_from_buf(&req->buf);
3321 co_htx_skip(req, req_htx, co_data(req));
3322 htx_to_buf(req_htx, &req->buf);
Christopher Fauletef779222018-10-31 08:47:01 +01003323 }
3324 }
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003325
Christopher Fauletef779222018-10-31 08:47:01 +01003326 out:
3327 /* we have left the request in the buffer for the case where we
3328 * process a POST, and this automatically re-enables activity on
3329 * read. It's better to indicate that we want to stop reading when
3330 * we're sending, so that we know there's at most one direction
3331 * deciding to wake the applet up. It saves it from looping when
3332 * emitting large blocks into small TCP windows.
3333 */
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003334 htx_to_buf(res_htx, &res->buf);
3335 if (!channel_is_empty(res))
Christopher Fauletef779222018-10-31 08:47:01 +01003336 si_stop_get(si);
Christopher Fauletef779222018-10-31 08:47:01 +01003337}
3338
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003339/* Dump all fields from <info> into <out> using the "show info" format (name: value) */
Willy Tarreau83061a82018-07-13 11:56:34 +02003340static int stats_dump_info_fields(struct buffer *out,
Willy Tarreau43241ff2019-10-09 11:27:51 +02003341 const struct field *info, unsigned int flags)
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003342{
3343 int field;
3344
3345 for (field = 0; field < INF_TOTAL_FIELDS; field++) {
3346 if (!field_format(info, field))
3347 continue;
3348
Willy Tarreaueaa55372019-10-09 07:39:11 +02003349 if (!chunk_appendf(out, "%s: ", info_fields[field].name))
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003350 return 0;
3351 if (!stats_emit_raw_data_field(out, &info[field]))
3352 return 0;
Willy Tarreau6b19b142019-10-09 15:44:21 +02003353 if ((flags & STAT_SHOW_FDESC) && !chunk_appendf(out, ":\"%s\"", info_fields[field].desc))
3354 return 0;
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003355 if (!chunk_strcat(out, "\n"))
3356 return 0;
3357 }
3358 return 1;
3359}
3360
3361/* Dump all fields from <info> into <out> using the "show info typed" format */
Willy Tarreau83061a82018-07-13 11:56:34 +02003362static int stats_dump_typed_info_fields(struct buffer *out,
Willy Tarreau43241ff2019-10-09 11:27:51 +02003363 const struct field *info, unsigned int flags)
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003364{
3365 int field;
3366
3367 for (field = 0; field < INF_TOTAL_FIELDS; field++) {
3368 if (!field_format(info, field))
3369 continue;
3370
Willy Tarreaueaa55372019-10-09 07:39:11 +02003371 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 +01003372 return 0;
3373 if (!stats_emit_field_tags(out, &info[field], ':'))
3374 return 0;
3375 if (!stats_emit_typed_data_field(out, &info[field]))
3376 return 0;
Willy Tarreau6b19b142019-10-09 15:44:21 +02003377 if ((flags & STAT_SHOW_FDESC) && !chunk_appendf(out, ":\"%s\"", info_fields[field].desc))
3378 return 0;
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003379 if (!chunk_strcat(out, "\n"))
3380 return 0;
3381 }
3382 return 1;
3383}
3384
3385/* Fill <info> with HAProxy global info. <info> is preallocated
3386 * array of length <len>. The length of the aray must be
3387 * INF_TOTAL_FIELDS. If this length is less then this value, the
3388 * function returns 0, otherwise, it returns 1.
3389 */
3390int stats_fill_info(struct field *info, int len)
3391{
3392 unsigned int up = (now.tv_sec - start_date.tv_sec);
Willy Tarreau83061a82018-07-13 11:56:34 +02003393 struct buffer *out = get_trash_chunk();
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003394
3395#ifdef USE_OPENSSL
3396 int ssl_sess_rate = read_freq_ctr(&global.ssl_per_sec);
3397 int ssl_key_rate = read_freq_ctr(&global.ssl_fe_keys_per_sec);
3398 int ssl_reuse = 0;
3399
3400 if (ssl_key_rate < ssl_sess_rate) {
3401 /* count the ssl reuse ratio and avoid overflows in both directions */
3402 ssl_reuse = 100 - (100 * ssl_key_rate + (ssl_sess_rate - 1) / 2) / ssl_sess_rate;
3403 }
3404#endif
3405
3406 if (len < INF_TOTAL_FIELDS)
3407 return 0;
3408
3409 chunk_reset(out);
3410 memset(info, 0, sizeof(*info) * len);
3411
3412 info[INF_NAME] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, PRODUCT_NAME);
Willy Tarreau909b9d82019-01-04 18:20:32 +01003413 info[INF_VERSION] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, haproxy_version);
3414 info[INF_RELEASE_DATE] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, haproxy_date);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003415
Yves Lafon95317282018-02-26 11:10:37 +01003416 info[INF_NBTHREAD] = mkf_u32(FO_CONFIG|FS_SERVICE, global.nbthread);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003417 info[INF_NBPROC] = mkf_u32(FO_CONFIG|FS_SERVICE, global.nbproc);
3418 info[INF_PROCESS_NUM] = mkf_u32(FO_KEY, relative_pid);
3419 info[INF_PID] = mkf_u32(FO_STATUS, pid);
3420
3421 info[INF_UPTIME] = mkf_str(FN_DURATION, chunk_newstr(out));
3422 chunk_appendf(out, "%ud %uh%02um%02us", up / 86400, (up % 86400) / 3600, (up % 3600) / 60, (up % 60));
3423
3424 info[INF_UPTIME_SEC] = mkf_u32(FN_DURATION, up);
3425 info[INF_MEMMAX_MB] = mkf_u32(FO_CONFIG|FN_LIMIT, global.rlimit_memmax);
3426 info[INF_POOL_ALLOC_MB] = mkf_u32(0, (unsigned)(pool_total_allocated() / 1048576L));
3427 info[INF_POOL_USED_MB] = mkf_u32(0, (unsigned)(pool_total_used() / 1048576L));
3428 info[INF_POOL_FAILED] = mkf_u32(FN_COUNTER, pool_total_failures());
3429 info[INF_ULIMIT_N] = mkf_u32(FO_CONFIG|FN_LIMIT, global.rlimit_nofile);
3430 info[INF_MAXSOCK] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxsock);
3431 info[INF_MAXCONN] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxconn);
3432 info[INF_HARD_MAXCONN] = mkf_u32(FO_CONFIG|FN_LIMIT, global.hardmaxconn);
3433 info[INF_CURR_CONN] = mkf_u32(0, actconn);
3434 info[INF_CUM_CONN] = mkf_u32(FN_COUNTER, totalconn);
3435 info[INF_CUM_REQ] = mkf_u32(FN_COUNTER, global.req_count);
3436#ifdef USE_OPENSSL
3437 info[INF_MAX_SSL_CONNS] = mkf_u32(FN_MAX, global.maxsslconn);
3438 info[INF_CURR_SSL_CONNS] = mkf_u32(0, sslconns);
3439 info[INF_CUM_SSL_CONNS] = mkf_u32(FN_COUNTER, totalsslconns);
3440#endif
3441 info[INF_MAXPIPES] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxpipes);
3442 info[INF_PIPES_USED] = mkf_u32(0, pipes_used);
3443 info[INF_PIPES_FREE] = mkf_u32(0, pipes_free);
3444 info[INF_CONN_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&global.conn_per_sec));
3445 info[INF_CONN_RATE_LIMIT] = mkf_u32(FO_CONFIG|FN_LIMIT, global.cps_lim);
3446 info[INF_MAX_CONN_RATE] = mkf_u32(FN_MAX, global.cps_max);
3447 info[INF_SESS_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&global.sess_per_sec));
3448 info[INF_SESS_RATE_LIMIT] = mkf_u32(FO_CONFIG|FN_LIMIT, global.sps_lim);
3449 info[INF_MAX_SESS_RATE] = mkf_u32(FN_RATE, global.sps_max);
3450
3451#ifdef USE_OPENSSL
3452 info[INF_SSL_RATE] = mkf_u32(FN_RATE, ssl_sess_rate);
3453 info[INF_SSL_RATE_LIMIT] = mkf_u32(FO_CONFIG|FN_LIMIT, global.ssl_lim);
3454 info[INF_MAX_SSL_RATE] = mkf_u32(FN_MAX, global.ssl_max);
3455 info[INF_SSL_FRONTEND_KEY_RATE] = mkf_u32(0, ssl_key_rate);
3456 info[INF_SSL_FRONTEND_MAX_KEY_RATE] = mkf_u32(FN_MAX, global.ssl_fe_keys_max);
3457 info[INF_SSL_FRONTEND_SESSION_REUSE_PCT] = mkf_u32(0, ssl_reuse);
3458 info[INF_SSL_BACKEND_KEY_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&global.ssl_be_keys_per_sec));
3459 info[INF_SSL_BACKEND_MAX_KEY_RATE] = mkf_u32(FN_MAX, global.ssl_be_keys_max);
3460 info[INF_SSL_CACHE_LOOKUPS] = mkf_u32(FN_COUNTER, global.shctx_lookups);
3461 info[INF_SSL_CACHE_MISSES] = mkf_u32(FN_COUNTER, global.shctx_misses);
3462#endif
3463 info[INF_COMPRESS_BPS_IN] = mkf_u32(FN_RATE, read_freq_ctr(&global.comp_bps_in));
3464 info[INF_COMPRESS_BPS_OUT] = mkf_u32(FN_RATE, read_freq_ctr(&global.comp_bps_out));
3465 info[INF_COMPRESS_BPS_RATE_LIM] = mkf_u32(FO_CONFIG|FN_LIMIT, global.comp_rate_lim);
3466#ifdef USE_ZLIB
3467 info[INF_ZLIB_MEM_USAGE] = mkf_u32(0, zlib_used_memory);
3468 info[INF_MAX_ZLIB_MEM_USAGE] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxzlibmem);
3469#endif
3470 info[INF_TASKS] = mkf_u32(0, nb_tasks_cur);
Christopher Faulet34c5cc92016-12-06 09:15:30 +01003471 info[INF_RUN_QUEUE] = mkf_u32(0, tasks_run_queue_cur);
Willy Tarreau81036f22019-05-20 19:24:50 +02003472 info[INF_IDLE_PCT] = mkf_u32(FN_AVG, ti->idle_pct);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003473 info[INF_NODE] = mkf_str(FO_CONFIG|FN_OUTPUT|FS_SERVICE, global.node);
3474 if (global.desc)
3475 info[INF_DESCRIPTION] = mkf_str(FO_CONFIG|FN_OUTPUT|FS_SERVICE, global.desc);
Willy Tarreau00098ea2018-11-05 14:38:13 +01003476 info[INF_STOPPING] = mkf_u32(0, stopping);
3477 info[INF_JOBS] = mkf_u32(0, jobs);
William Lallemanda7199262018-11-16 16:57:20 +01003478 info[INF_UNSTOPPABLE_JOBS] = mkf_u32(0, unstoppable_jobs);
Willy Tarreau00098ea2018-11-05 14:38:13 +01003479 info[INF_LISTENERS] = mkf_u32(0, listeners);
Willy Tarreau199ad242018-11-05 16:31:22 +01003480 info[INF_ACTIVE_PEERS] = mkf_u32(0, active_peers);
Willy Tarreau2d372c22018-11-05 17:12:27 +01003481 info[INF_CONNECTED_PEERS] = mkf_u32(0, connected_peers);
Willy Tarreau13ef7732018-11-12 07:25:28 +01003482 info[INF_DROPPED_LOGS] = mkf_u32(0, dropped_logs);
Willy Tarreaubeb859a2018-11-22 18:07:59 +01003483 info[INF_BUSY_POLLING] = mkf_u32(0, !!(global.tune.options & GTUNE_BUSY_POLLING));
Baptiste Assmann333939c2019-01-21 08:34:50 +01003484 info[INF_FAILED_RESOLUTIONS] = mkf_u32(0, dns_failed_resolutions);
Willy Tarreau7cf0e452019-05-23 11:39:14 +02003485 info[INF_TOTAL_BYTES_OUT] = mkf_u64(0, global.out_bytes);
3486 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 +02003487 info[INF_DEBUG_COMMANDS_ISSUED] = mkf_u32(0, debug_commands_issued);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003488
3489 return 1;
3490}
3491
3492/* This function dumps information onto the stream interface's read buffer.
3493 * It returns 0 as long as it does not complete, non-zero upon completion.
3494 * No state is used.
3495 */
3496static int stats_dump_info_to_buffer(struct stream_interface *si)
3497{
3498 struct appctx *appctx = __objt_appctx(si->end);
3499
3500 if (!stats_fill_info(info, INF_TOTAL_FIELDS))
3501 return 0;
3502
3503 chunk_reset(&trash);
3504
3505 if (appctx->ctx.stats.flags & STAT_FMT_TYPED)
Willy Tarreau43241ff2019-10-09 11:27:51 +02003506 stats_dump_typed_info_fields(&trash, info, appctx->ctx.stats.flags);
Simon Horman05ee2132017-01-04 09:37:25 +01003507 else if (appctx->ctx.stats.flags & STAT_FMT_JSON)
Willy Tarreau43241ff2019-10-09 11:27:51 +02003508 stats_dump_json_info_fields(&trash, info, appctx->ctx.stats.flags);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003509 else
Willy Tarreau43241ff2019-10-09 11:27:51 +02003510 stats_dump_info_fields(&trash, info, appctx->ctx.stats.flags);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003511
Willy Tarreau06d80a92017-10-19 14:32:15 +02003512 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01003513 si_rx_room_blk(si);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003514 return 0;
3515 }
3516
3517 return 1;
3518}
3519
Simon Horman6f6bb382017-01-04 09:37:26 +01003520/* This function dumps the schema onto the stream interface's read buffer.
3521 * It returns 0 as long as it does not complete, non-zero upon completion.
3522 * No state is used.
3523 *
3524 * Integer values bouned to the range [-(2**53)+1, (2**53)-1] as
3525 * per the recommendation for interoperable integers in section 6 of RFC 7159.
3526 */
Willy Tarreau83061a82018-07-13 11:56:34 +02003527static void stats_dump_json_schema(struct buffer *out)
Simon Horman6f6bb382017-01-04 09:37:26 +01003528{
3529
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003530 int old_len = out->data;
Simon Horman6f6bb382017-01-04 09:37:26 +01003531
3532 chunk_strcat(out,
3533 "{"
3534 "\"$schema\":\"http://json-schema.org/draft-04/schema#\","
3535 "\"oneOf\":["
3536 "{"
3537 "\"title\":\"Info\","
3538 "\"type\":\"array\","
3539 "\"items\":{"
3540 "\"properties\":{"
3541 "\"title\":\"InfoItem\","
3542 "\"type\":\"object\","
3543 "\"field\":{\"$ref\":\"#/definitions/field\"},"
3544 "\"processNum\":{\"$ref\":\"#/definitions/processNum\"},"
3545 "\"tags\":{\"$ref\":\"#/definitions/tags\"},"
3546 "\"value\":{\"$ref\":\"#/definitions/typedValue\"}"
3547 "},"
3548 "\"required\":[\"field\",\"processNum\",\"tags\","
3549 "\"value\"]"
3550 "}"
3551 "},"
3552 "{"
3553 "\"title\":\"Stat\","
3554 "\"type\":\"array\","
3555 "\"items\":{"
3556 "\"title\":\"InfoItem\","
3557 "\"type\":\"object\","
3558 "\"properties\":{"
3559 "\"objType\":{"
3560 "\"enum\":[\"Frontend\",\"Backend\",\"Listener\","
3561 "\"Server\",\"Unknown\"]"
3562 "},"
3563 "\"proxyId\":{"
3564 "\"type\":\"integer\","
3565 "\"minimum\":0"
3566 "},"
3567 "\"id\":{"
3568 "\"type\":\"integer\","
3569 "\"minimum\":0"
3570 "},"
3571 "\"field\":{\"$ref\":\"#/definitions/field\"},"
3572 "\"processNum\":{\"$ref\":\"#/definitions/processNum\"},"
3573 "\"tags\":{\"$ref\":\"#/definitions/tags\"},"
3574 "\"typedValue\":{\"$ref\":\"#/definitions/typedValue\"}"
3575 "},"
3576 "\"required\":[\"objType\",\"proxyId\",\"id\","
3577 "\"field\",\"processNum\",\"tags\","
3578 "\"value\"]"
3579 "}"
3580 "},"
3581 "{"
3582 "\"title\":\"Error\","
3583 "\"type\":\"object\","
3584 "\"properties\":{"
3585 "\"errorStr\":{"
3586 "\"type\":\"string\""
3587 "},"
3588 "\"required\":[\"errorStr\"]"
3589 "}"
3590 "}"
3591 "],"
3592 "\"definitions\":{"
3593 "\"field\":{"
3594 "\"type\":\"object\","
3595 "\"pos\":{"
3596 "\"type\":\"integer\","
3597 "\"minimum\":0"
3598 "},"
3599 "\"name\":{"
3600 "\"type\":\"string\""
3601 "},"
3602 "\"required\":[\"pos\",\"name\"]"
3603 "},"
3604 "\"processNum\":{"
3605 "\"type\":\"integer\","
3606 "\"minimum\":1"
3607 "},"
3608 "\"tags\":{"
3609 "\"type\":\"object\","
3610 "\"origin\":{"
3611 "\"type\":\"string\","
3612 "\"enum\":[\"Metric\",\"Status\",\"Key\","
3613 "\"Config\",\"Product\",\"Unknown\"]"
3614 "},"
3615 "\"nature\":{"
3616 "\"type\":\"string\","
3617 "\"enum\":[\"Gauge\",\"Limit\",\"Min\",\"Max\","
3618 "\"Rate\",\"Counter\",\"Duration\","
3619 "\"Age\",\"Time\",\"Name\",\"Output\","
3620 "\"Avg\", \"Unknown\"]"
3621 "},"
3622 "\"scope\":{"
3623 "\"type\":\"string\","
3624 "\"enum\":[\"Cluster\",\"Process\",\"Service\","
3625 "\"System\",\"Unknown\"]"
3626 "},"
3627 "\"required\":[\"origin\",\"nature\",\"scope\"]"
3628 "},"
3629 "\"typedValue\":{"
3630 "\"type\":\"object\","
3631 "\"oneOf\":["
3632 "{\"$ref\":\"#/definitions/typedValue/definitions/s32Value\"},"
3633 "{\"$ref\":\"#/definitions/typedValue/definitions/s64Value\"},"
3634 "{\"$ref\":\"#/definitions/typedValue/definitions/u32Value\"},"
3635 "{\"$ref\":\"#/definitions/typedValue/definitions/u64Value\"},"
3636 "{\"$ref\":\"#/definitions/typedValue/definitions/strValue\"}"
3637 "],"
3638 "\"definitions\":{"
3639 "\"s32Value\":{"
3640 "\"properties\":{"
3641 "\"type\":{"
3642 "\"type\":\"string\","
3643 "\"enum\":[\"s32\"]"
3644 "},"
3645 "\"value\":{"
3646 "\"type\":\"integer\","
3647 "\"minimum\":-2147483648,"
3648 "\"maximum\":2147483647"
3649 "}"
3650 "},"
3651 "\"required\":[\"type\",\"value\"]"
3652 "},"
3653 "\"s64Value\":{"
3654 "\"properties\":{"
3655 "\"type\":{"
3656 "\"type\":\"string\","
3657 "\"enum\":[\"s64\"]"
3658 "},"
3659 "\"value\":{"
3660 "\"type\":\"integer\","
3661 "\"minimum\":-9007199254740991,"
3662 "\"maximum\":9007199254740991"
3663 "}"
3664 "},"
3665 "\"required\":[\"type\",\"value\"]"
3666 "},"
3667 "\"u32Value\":{"
3668 "\"properties\":{"
3669 "\"type\":{"
3670 "\"type\":\"string\","
3671 "\"enum\":[\"u32\"]"
3672 "},"
3673 "\"value\":{"
3674 "\"type\":\"integer\","
3675 "\"minimum\":0,"
3676 "\"maximum\":4294967295"
3677 "}"
3678 "},"
3679 "\"required\":[\"type\",\"value\"]"
3680 "},"
3681 "\"u64Value\":{"
3682 "\"properties\":{"
3683 "\"type\":{"
3684 "\"type\":\"string\","
3685 "\"enum\":[\"u64\"]"
3686 "},"
3687 "\"value\":{"
3688 "\"type\":\"integer\","
3689 "\"minimum\":0,"
3690 "\"maximum\":9007199254740991"
3691 "}"
3692 "},"
3693 "\"required\":[\"type\",\"value\"]"
3694 "},"
3695 "\"strValue\":{"
3696 "\"properties\":{"
3697 "\"type\":{"
3698 "\"type\":\"string\","
3699 "\"enum\":[\"str\"]"
3700 "},"
3701 "\"value\":{\"type\":\"string\"}"
3702 "},"
3703 "\"required\":[\"type\",\"value\"]"
3704 "},"
3705 "\"unknownValue\":{"
3706 "\"properties\":{"
3707 "\"type\":{"
3708 "\"type\":\"integer\","
3709 "\"minimum\":0"
3710 "},"
3711 "\"value\":{"
3712 "\"type\":\"string\","
3713 "\"enum\":[\"unknown\"]"
3714 "}"
3715 "},"
3716 "\"required\":[\"type\",\"value\"]"
3717 "}"
3718 "}"
3719 "}"
3720 "}"
3721 "}");
3722
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003723 if (old_len == out->data) {
Simon Horman6f6bb382017-01-04 09:37:26 +01003724 chunk_reset(out);
3725 chunk_appendf(out,
3726 "{\"errorStr\":\"output buffer too short\"}");
3727 }
3728}
3729
3730/* This function dumps the schema onto the stream interface's read buffer.
3731 * It returns 0 as long as it does not complete, non-zero upon completion.
3732 * No state is used.
3733 */
3734static int stats_dump_json_schema_to_buffer(struct stream_interface *si)
3735{
3736 chunk_reset(&trash);
3737
3738 stats_dump_json_schema(&trash);
3739
Willy Tarreau06d80a92017-10-19 14:32:15 +02003740 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01003741 si_rx_room_blk(si);
Simon Horman6f6bb382017-01-04 09:37:26 +01003742 return 0;
3743 }
3744
3745 return 1;
3746}
3747
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02003748static int cli_parse_clear_counters(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau89d467c2016-11-23 11:02:40 +01003749{
3750 struct proxy *px;
3751 struct server *sv;
3752 struct listener *li;
3753 int clrall = 0;
3754
3755 if (strcmp(args[2], "all") == 0)
3756 clrall = 1;
3757
3758 /* check permissions */
3759 if (!cli_has_level(appctx, ACCESS_LVL_OPER) ||
3760 (clrall && !cli_has_level(appctx, ACCESS_LVL_ADMIN)))
3761 return 1;
3762
Olivier Houchardfbc74e82017-11-24 16:54:05 +01003763 for (px = proxies_list; px; px = px->next) {
Willy Tarreau89d467c2016-11-23 11:02:40 +01003764 if (clrall) {
3765 memset(&px->be_counters, 0, sizeof(px->be_counters));
3766 memset(&px->fe_counters, 0, sizeof(px->fe_counters));
3767 }
3768 else {
3769 px->be_counters.conn_max = 0;
3770 px->be_counters.p.http.rps_max = 0;
3771 px->be_counters.sps_max = 0;
3772 px->be_counters.cps_max = 0;
3773 px->be_counters.nbpend_max = 0;
Christopher Fauletefb41f02019-11-08 14:53:15 +01003774 px->be_counters.qtime_max = 0;
3775 px->be_counters.ctime_max = 0;
3776 px->be_counters.dtime_max = 0;
3777 px->be_counters.ttime_max = 0;
Willy Tarreau89d467c2016-11-23 11:02:40 +01003778
3779 px->fe_counters.conn_max = 0;
3780 px->fe_counters.p.http.rps_max = 0;
3781 px->fe_counters.sps_max = 0;
3782 px->fe_counters.cps_max = 0;
Willy Tarreau89d467c2016-11-23 11:02:40 +01003783 }
3784
3785 for (sv = px->srv; sv; sv = sv->next)
3786 if (clrall)
3787 memset(&sv->counters, 0, sizeof(sv->counters));
3788 else {
3789 sv->counters.cur_sess_max = 0;
3790 sv->counters.nbpend_max = 0;
3791 sv->counters.sps_max = 0;
Christopher Fauletefb41f02019-11-08 14:53:15 +01003792 sv->counters.qtime_max = 0;
3793 sv->counters.ctime_max = 0;
3794 sv->counters.dtime_max = 0;
3795 sv->counters.ttime_max = 0;
Willy Tarreau89d467c2016-11-23 11:02:40 +01003796 }
3797
3798 list_for_each_entry(li, &px->conf.listeners, by_fe)
3799 if (li->counters) {
3800 if (clrall)
3801 memset(li->counters, 0, sizeof(*li->counters));
3802 else
3803 li->counters->conn_max = 0;
3804 }
3805 }
3806
3807 global.cps_max = 0;
3808 global.sps_max = 0;
Olivier Houchard00bc3cb2017-10-17 19:23:25 +02003809 global.ssl_max = 0;
3810 global.ssl_fe_keys_max = 0;
3811 global.ssl_be_keys_max = 0;
Willy Tarreaud80cb4e2018-01-20 19:30:13 +01003812
3813 memset(activity, 0, sizeof(activity));
Willy Tarreau89d467c2016-11-23 11:02:40 +01003814 return 1;
3815}
3816
3817
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02003818static int cli_parse_show_info(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003819{
Willy Tarreau2f397382019-10-09 11:43:59 +02003820 int arg = 2;
3821
Willy Tarreaud25fc792016-12-16 12:33:47 +01003822 appctx->ctx.stats.scope_str = 0;
3823 appctx->ctx.stats.scope_len = 0;
3824 appctx->ctx.stats.flags = 0;
3825
Willy Tarreau2f397382019-10-09 11:43:59 +02003826 while (*args[arg]) {
3827 if (strcmp(args[arg], "typed") == 0)
3828 appctx->ctx.stats.flags = (appctx->ctx.stats.flags & ~STAT_FMT_MASK) | STAT_FMT_TYPED;
3829 else if (strcmp(args[arg], "json") == 0)
3830 appctx->ctx.stats.flags = (appctx->ctx.stats.flags & ~STAT_FMT_MASK) | STAT_FMT_JSON;
3831 else if (strcmp(args[arg], "desc") == 0)
3832 appctx->ctx.stats.flags |= STAT_SHOW_FDESC;
3833 arg++;
3834 }
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003835 return 0;
3836}
3837
3838
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02003839static int cli_parse_show_stat(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau2b812e22016-11-22 16:18:05 +01003840{
Willy Tarreau2f397382019-10-09 11:43:59 +02003841 int arg = 2;
3842
Willy Tarreaud25fc792016-12-16 12:33:47 +01003843 appctx->ctx.stats.scope_str = 0;
3844 appctx->ctx.stats.scope_len = 0;
Willy Tarreau578d6e42019-10-09 11:00:22 +02003845 appctx->ctx.stats.flags = STAT_SHNODE | STAT_SHDESC;
3846
3847 if ((strm_li(si_strm(appctx->owner))->bind_conf->level & ACCESS_LVL_MASK) >= ACCESS_LVL_OPER)
3848 appctx->ctx.stats.flags |= STAT_SHLGNDS;
Willy Tarreaud25fc792016-12-16 12:33:47 +01003849
Willy Tarreau2f397382019-10-09 11:43:59 +02003850 if (*args[arg] && *args[arg+1] && *args[arg+2]) {
Willy Tarreaua1b1ed52016-11-25 08:50:58 +01003851 struct proxy *px;
3852
Willy Tarreau2f397382019-10-09 11:43:59 +02003853 px = proxy_find_by_name(args[arg], 0, 0);
Willy Tarreaua1b1ed52016-11-25 08:50:58 +01003854 if (px)
3855 appctx->ctx.stats.iid = px->uuid;
3856 else
Willy Tarreau2f397382019-10-09 11:43:59 +02003857 appctx->ctx.stats.iid = atoi(args[arg]);
Willy Tarreaua1b1ed52016-11-25 08:50:58 +01003858
Willy Tarreau9d008692019-08-09 11:21:01 +02003859 if (!appctx->ctx.stats.iid)
3860 return cli_err(appctx, "No such proxy.\n");
Willy Tarreaua1b1ed52016-11-25 08:50:58 +01003861
Willy Tarreau2b812e22016-11-22 16:18:05 +01003862 appctx->ctx.stats.flags |= STAT_BOUND;
Willy Tarreau2f397382019-10-09 11:43:59 +02003863 appctx->ctx.stats.type = atoi(args[arg+1]);
3864 appctx->ctx.stats.sid = atoi(args[arg+2]);
3865 arg += 3;
3866 }
3867
3868 while (*args[arg]) {
3869 if (strcmp(args[arg], "typed") == 0)
3870 appctx->ctx.stats.flags = (appctx->ctx.stats.flags & ~STAT_FMT_MASK) | STAT_FMT_TYPED;
3871 else if (strcmp(args[arg], "json") == 0)
3872 appctx->ctx.stats.flags = (appctx->ctx.stats.flags & ~STAT_FMT_MASK) | STAT_FMT_JSON;
3873 else if (strcmp(args[arg], "desc") == 0)
3874 appctx->ctx.stats.flags |= STAT_SHOW_FDESC;
3875 arg++;
Willy Tarreau2b812e22016-11-22 16:18:05 +01003876 }
Willy Tarreau2b812e22016-11-22 16:18:05 +01003877
Willy Tarreau2b812e22016-11-22 16:18:05 +01003878 return 0;
3879}
3880
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003881static int cli_io_handler_dump_info(struct appctx *appctx)
3882{
3883 return stats_dump_info_to_buffer(appctx->owner);
3884}
3885
Willy Tarreau2b812e22016-11-22 16:18:05 +01003886/* This I/O handler runs as an applet embedded in a stream interface. It is
3887 * used to send raw stats over a socket.
3888 */
3889static int cli_io_handler_dump_stat(struct appctx *appctx)
3890{
Christopher Fauletef779222018-10-31 08:47:01 +01003891 return stats_dump_stat_to_buffer(appctx->owner, NULL, NULL);
Willy Tarreau2b812e22016-11-22 16:18:05 +01003892}
3893
Simon Horman6f6bb382017-01-04 09:37:26 +01003894static int cli_io_handler_dump_json_schema(struct appctx *appctx)
3895{
3896 return stats_dump_json_schema_to_buffer(appctx->owner);
3897}
3898
Willy Tarreau2b812e22016-11-22 16:18:05 +01003899/* register cli keywords */
3900static struct cli_kw_list cli_kws = {{ },{
Willy Tarreau89d467c2016-11-23 11:02:40 +01003901 { { "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 +02003902 { { "show", "info", NULL }, "show info : report information about the running process [desc|json|typed]*", cli_parse_show_info, cli_io_handler_dump_info, NULL },
3903 { { "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 +01003904 { { "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 +01003905 {{},}
3906}};
3907
Willy Tarreau0108d902018-11-25 19:14:37 +01003908INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
3909
William Lallemand74c24fb2016-11-21 17:18:36 +01003910struct applet http_stats_applet = {
3911 .obj_type = OBJ_TYPE_APPLET,
3912 .name = "<STATS>", /* used for logging */
3913 .fct = http_stats_io_handler,
3914 .release = NULL,
3915};
3916
William Lallemand74c24fb2016-11-21 17:18:36 +01003917/*
3918 * Local variables:
3919 * c-indent-level: 8
3920 * c-basic-offset: 8
3921 * End:
3922 */