blob: 6330512ca88f0f5a2fec6ec5a13085e2d75a5e9d [file] [log] [blame]
William Lallemand74c24fb2016-11-21 17:18:36 +01001/*
2 * Functions dedicated to statistics output and the stats socket
3 *
4 * Copyright 2000-2012 Willy Tarreau <w@1wt.eu>
5 * Copyright 2007-2009 Krzysztof Piotr Oledzki <ole@ans.pl>
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version
10 * 2 of the License, or (at your option) any later version.
11 *
12 */
13
14#include <ctype.h>
15#include <errno.h>
16#include <fcntl.h>
17#include <stdio.h>
18#include <stdlib.h>
19#include <string.h>
20#include <pwd.h>
21#include <grp.h>
22
23#include <sys/socket.h>
24#include <sys/stat.h>
25#include <sys/types.h>
26
27#include <common/cfgparse.h>
28#include <common/compat.h>
29#include <common/config.h>
30#include <common/debug.h>
Willy Tarreau35b51c62018-09-10 15:38:55 +020031#include <common/http.h>
Willy Tarreaub96b77e2018-12-11 10:22:41 +010032#include <common/htx.h>
Willy Tarreau0108d902018-11-25 19:14:37 +010033#include <common/initcall.h>
William Lallemand74c24fb2016-11-21 17:18:36 +010034#include <common/memory.h>
35#include <common/mini-clist.h>
36#include <common/standard.h>
37#include <common/ticks.h>
38#include <common/time.h>
39#include <common/uri_auth.h>
40#include <common/version.h>
41#include <common/base64.h>
42
43#include <types/applet.h>
Willy Tarreau2b812e22016-11-22 16:18:05 +010044#include <types/cli.h>
William Lallemand74c24fb2016-11-21 17:18:36 +010045#include <types/global.h>
46#include <types/dns.h>
William Lallemand9ed62032016-11-21 17:49:11 +010047#include <types/stats.h>
William Lallemand74c24fb2016-11-21 17:18:36 +010048
49#include <proto/backend.h>
50#include <proto/channel.h>
51#include <proto/checks.h>
Willy Tarreau2b812e22016-11-22 16:18:05 +010052#include <proto/cli.h>
William Lallemand74c24fb2016-11-21 17:18:36 +010053#include <proto/compression.h>
Baptiste Assmann333939c2019-01-21 08:34:50 +010054#include <proto/dns.h>
William Lallemand9ed62032016-11-21 17:49:11 +010055#include <proto/stats.h>
William Lallemand74c24fb2016-11-21 17:18:36 +010056#include <proto/fd.h>
57#include <proto/freq_ctr.h>
58#include <proto/frontend.h>
Christopher Fauleted7a0662019-01-14 11:07:34 +010059#include <proto/http_htx.h>
William Lallemand74c24fb2016-11-21 17:18:36 +010060#include <proto/log.h>
61#include <proto/pattern.h>
62#include <proto/pipe.h>
63#include <proto/listener.h>
64#include <proto/map.h>
William Lallemand74c24fb2016-11-21 17:18:36 +010065#include <proto/proxy.h>
66#include <proto/sample.h>
67#include <proto/session.h>
Willy Tarreauc125cef2019-05-10 09:58:43 +020068#include <proto/ssl_sock.h>
William Lallemand74c24fb2016-11-21 17:18:36 +010069#include <proto/stream.h>
70#include <proto/server.h>
71#include <proto/raw_sock.h>
72#include <proto/stream_interface.h>
73#include <proto/task.h>
74
Christopher Faulet2b9b6782019-02-27 16:42:58 +010075/* status codes available for the stats admin page (strictly 4 chars length) */
76const char *stat_status_codes[STAT_STATUS_SIZE] = {
77 [STAT_STATUS_DENY] = "DENY",
78 [STAT_STATUS_DONE] = "DONE",
79 [STAT_STATUS_ERRP] = "ERRP",
80 [STAT_STATUS_EXCD] = "EXCD",
81 [STAT_STATUS_NONE] = "NONE",
82 [STAT_STATUS_PART] = "PART",
83 [STAT_STATUS_UNKN] = "UNKN",
84 [STAT_STATUS_IVAL] = "IVAL",
85};
86
Willy Tarreau0baac8c2016-11-22 16:36:53 +010087/* These are the field names for each INF_* field position. Please pay attention
88 * to always use the exact same name except that the strings for new names must
89 * be lower case or CamelCase while the enum entries must be upper case.
90 */
Willy Tarreaueaa55372019-10-09 07:39:11 +020091const struct name_desc info_fields[INF_TOTAL_FIELDS] = {
Willy Tarreau6d4897e2019-10-11 16:31:46 +020092 [INF_NAME] = { .name = "Name", .desc = "Product name" },
93 [INF_VERSION] = { .name = "Version", .desc = "Product version" },
94 [INF_RELEASE_DATE] = { .name = "Release_date", .desc = "Date of latest source code update" },
95 [INF_NBTHREAD] = { .name = "Nbthread", .desc = "Number of started threads (global.nbthread)" },
96 [INF_NBPROC] = { .name = "Nbproc", .desc = "Number of started worker processes (global.nbproc)" },
97 [INF_PROCESS_NUM] = { .name = "Process_num", .desc = "Relative worker process number (1..Nbproc)" },
98 [INF_PID] = { .name = "Pid", .desc = "This worker process identifier for the system" },
99 [INF_UPTIME] = { .name = "Uptime", .desc = "How long ago this worker process was started (days+hours+minutes+seconds)" },
100 [INF_UPTIME_SEC] = { .name = "Uptime_sec", .desc = "How long ago this worker process was started (seconds)" },
101 [INF_MEMMAX_MB] = { .name = "Memmax_MB", .desc = "Worker process's hard limit on memory usage in MB (-m on command line)" },
102 [INF_POOL_ALLOC_MB] = { .name = "PoolAlloc_MB", .desc = "Amount of memory allocated in pools (in MB)" },
103 [INF_POOL_USED_MB] = { .name = "PoolUsed_MB", .desc = "Amount of pool memory currently used (in MB)" },
104 [INF_POOL_FAILED] = { .name = "PoolFailed", .desc = "Number of failed pool allocations since this worker was started" },
105 [INF_ULIMIT_N] = { .name = "Ulimit-n", .desc = "Hard limit on the number of per-process file descriptors" },
106 [INF_MAXSOCK] = { .name = "Maxsock", .desc = "Hard limit on the number of per-process sockets" },
107 [INF_MAXCONN] = { .name = "Maxconn", .desc = "Hard limit on the number of per-process connections (configured or imposed by Ulimit-n)" },
108 [INF_HARD_MAXCONN] = { .name = "Hard_maxconn", .desc = "Hard limit on the number of per-process connections (imposed by Memmax_MB or Ulimit-n)" },
109 [INF_CURR_CONN] = { .name = "CurrConns", .desc = "Current number of connections on this worker process" },
110 [INF_CUM_CONN] = { .name = "CumConns", .desc = "Total number of connections on this worker process since started" },
111 [INF_CUM_REQ] = { .name = "CumReq", .desc = "Total number of requests on this worker process since started" },
112 [INF_MAX_SSL_CONNS] = { .name = "MaxSslConns", .desc = "Hard limit on the number of per-process SSL endpoints (front+back), 0=unlimited" },
113 [INF_CURR_SSL_CONNS] = { .name = "CurrSslConns", .desc = "Current number of SSL endpoints on this worker process (front+back)" },
114 [INF_CUM_SSL_CONNS] = { .name = "CumSslConns", .desc = "Total number of SSL endpoints on this worker process since started (front+back)" },
115 [INF_MAXPIPES] = { .name = "Maxpipes", .desc = "Hard limit on the number of pipes for splicing, 0=unlimited" },
116 [INF_PIPES_USED] = { .name = "PipesUsed", .desc = "Current number of pipes in use in this worker process" },
117 [INF_PIPES_FREE] = { .name = "PipesFree", .desc = "Current number of allocated and available pipes in this worker process" },
118 [INF_CONN_RATE] = { .name = "ConnRate", .desc = "Number of front connections created on this worker process over the last second" },
119 [INF_CONN_RATE_LIMIT] = { .name = "ConnRateLimit", .desc = "Hard limit for ConnRate (global.maxconnrate)" },
120 [INF_MAX_CONN_RATE] = { .name = "MaxConnRate", .desc = "Highest ConnRate reached on this worker process since started (in connections per second)" },
121 [INF_SESS_RATE] = { .name = "SessRate", .desc = "Number of sessions created on this worker process over the last second" },
122 [INF_SESS_RATE_LIMIT] = { .name = "SessRateLimit", .desc = "Hard limit for SessRate (global.maxsessrate)" },
123 [INF_MAX_SESS_RATE] = { .name = "MaxSessRate", .desc = "Highest SessRate reached on this worker process since started (in sessions per second)" },
124 [INF_SSL_RATE] = { .name = "SslRate", .desc = "Number of SSL connections created on this worker process over the last second" },
125 [INF_SSL_RATE_LIMIT] = { .name = "SslRateLimit", .desc = "Hard limit for SslRate (global.maxsslrate)" },
126 [INF_MAX_SSL_RATE] = { .name = "MaxSslRate", .desc = "Highest SslRate reached on this worker process since started (in connections per second)" },
127 [INF_SSL_FRONTEND_KEY_RATE] = { .name = "SslFrontendKeyRate", .desc = "Number of SSL keys created on frontends in this worker process over the last second" },
128 [INF_SSL_FRONTEND_MAX_KEY_RATE] = { .name = "SslFrontendMaxKeyRate", .desc = "Highest SslFrontendKeyRate reached on this worker process since started (in SSL keys per second)" },
129 [INF_SSL_FRONTEND_SESSION_REUSE_PCT] = { .name = "SslFrontendSessionReuse_pct", .desc = "Percent of frontend SSL connections which did not require a new key" },
130 [INF_SSL_BACKEND_KEY_RATE] = { .name = "SslBackendKeyRate", .desc = "Number of SSL keys created on backends in this worker process over the last second" },
131 [INF_SSL_BACKEND_MAX_KEY_RATE] = { .name = "SslBackendMaxKeyRate", .desc = "Highest SslBackendKeyRate reached on this worker process since started (in SSL keys per second)" },
132 [INF_SSL_CACHE_LOOKUPS] = { .name = "SslCacheLookups", .desc = "Total number of SSL session ID lookups in the SSL session cache on this worker since started" },
133 [INF_SSL_CACHE_MISSES] = { .name = "SslCacheMisses", .desc = "Total number of SSL session ID lookups that didn't find a session in the SSL session cache on this worker since started" },
134 [INF_COMPRESS_BPS_IN] = { .name = "CompressBpsIn", .desc = "Number of bytes submitted to the HTTP compressor in this worker process over the last second" },
135 [INF_COMPRESS_BPS_OUT] = { .name = "CompressBpsOut", .desc = "Number of bytes emitted by the HTTP compressor in this worker process over the last second" },
136 [INF_COMPRESS_BPS_RATE_LIM] = { .name = "CompressBpsRateLim", .desc = "Limit of CompressBpsOut beyond which HTTP compression is automatically disabled" },
137 [INF_ZLIB_MEM_USAGE] = { .name = "ZlibMemUsage", .desc = "Amount of memory currently used by HTTP compression on the current worker process (in bytes)" },
138 [INF_MAX_ZLIB_MEM_USAGE] = { .name = "MaxZlibMemUsage", .desc = "Limit on the amount of memory used by HTTP compression above which it is automatically disabled (in bytes, see global.maxzlibmem)" },
139 [INF_TASKS] = { .name = "Tasks", .desc = "Total number of tasks in the current worker process (active + sleeping)" },
140 [INF_RUN_QUEUE] = { .name = "Run_queue", .desc = "Total number of active tasks+tasklets in the current worker process" },
141 [INF_IDLE_PCT] = { .name = "Idle_pct", .desc = "Percentage of last second spent waiting in the current worker thread" },
142 [INF_NODE] = { .name = "node", .desc = "Node name (global.node)" },
143 [INF_DESCRIPTION] = { .name = "description", .desc = "Node description (global.description)" },
144 [INF_STOPPING] = { .name = "Stopping", .desc = "1 if the worker process is currently stopping, otherwise zero" },
145 [INF_JOBS] = { .name = "Jobs", .desc = "Current number of active jobs on the current worker process (frontend connections, master connections, listeners)" },
146 [INF_UNSTOPPABLE_JOBS] = { .name = "Unstoppable Jobs", .desc = "Current number of unstoppable jobs on the current worker process (master connections)" },
147 [INF_LISTENERS] = { .name = "Listeners", .desc = "Current number of active listeners on the current worker process" },
148 [INF_ACTIVE_PEERS] = { .name = "ActivePeers", .desc = "Current number of verified active peers connections on the current worker process" },
149 [INF_CONNECTED_PEERS] = { .name = "ConnectedPeers", .desc = "Current number of peers having passed the connection step on the current worker process" },
150 [INF_DROPPED_LOGS] = { .name = "DroppedLogs", .desc = "Total number of dropped logs for current worker process since started" },
151 [INF_BUSY_POLLING] = { .name = "BusyPolling", .desc = "1 if busy-polling is currently in use on the worker process, otherwise zero (config.busy-polling)" },
152 [INF_FAILED_RESOLUTIONS] = { .name = "FailedResolutions", .desc = "Total number of failed DNS resolutions in current worker process since started" },
153 [INF_TOTAL_BYTES_OUT] = { .name = "TotalBytesOut", .desc = "Total number of bytes emitted by current worker process since started" },
154 [INF_BYTES_OUT_RATE] = { .name = "BytesOutRate", .desc = "Number of bytes emitted by current worker process over the last second" },
Willy Tarreau9b013702019-10-24 18:18:02 +0200155 [INF_DEBUG_COMMANDS_ISSUED] = { .name = "DebugCommandsIssued", .desc = "Number of debug commands issued on this process (anything > 0 is unsafe)" },
Willy Tarreau0baac8c2016-11-22 16:36:53 +0100156};
157
Willy Tarreaueaa55372019-10-09 07:39:11 +0200158const struct name_desc stat_fields[ST_F_TOTAL_FIELDS] = {
Willy Tarreau6d4897e2019-10-11 16:31:46 +0200159 [ST_F_PXNAME] = { .name = "pxname", .desc = "Proxy name" },
160 [ST_F_SVNAME] = { .name = "svname", .desc = "Server name" },
161 [ST_F_QCUR] = { .name = "qcur", .desc = "Current number of connections waiting in the server of backend queue" },
162 [ST_F_QMAX] = { .name = "qmax", .desc = "Highest value of qcur encountered since process started" },
163 [ST_F_SCUR] = { .name = "scur", .desc = "Current number of sessions on the frontend, backend or server" },
164 [ST_F_SMAX] = { .name = "smax", .desc = "Highest value of scur encountered since process started" },
165 [ST_F_SLIM] = { .name = "slim", .desc = "Frontend/listener/server's maxconn, backend's fullconn" },
166 [ST_F_STOT] = { .name = "stot", .desc = "Total number of sessions since process started" },
167 [ST_F_BIN] = { .name = "bin", .desc = "Total number of request bytes since process started" },
168 [ST_F_BOUT] = { .name = "bout", .desc = "Total number of response bytes since process started" },
169 [ST_F_DREQ] = { .name = "dreq", .desc = "Total number of denied requests since process started" },
170 [ST_F_DRESP] = { .name = "dresp", .desc = "Total number of denied responses since process started" },
171 [ST_F_EREQ] = { .name = "ereq", .desc = "Total number of invalid requests since process started" },
172 [ST_F_ECON] = { .name = "econ", .desc = "Total number of failed connections to server since the worker process started" },
173 [ST_F_ERESP] = { .name = "eresp", .desc = "Total number of invalid responses since the worker process started" },
174 [ST_F_WRETR] = { .name = "wretr", .desc = "Total number of server connection retries since the worker process started" },
175 [ST_F_WREDIS] = { .name = "wredis", .desc = "Total number of server redispatches due to connection failures since the worker process started" },
176 [ST_F_STATUS] = { .name = "status", .desc = "Frontend/listen status: OPEN/WAITING/FULL/STOP; backend: UP/DOWN; server: last check status" },
177 [ST_F_WEIGHT] = { .name = "weight", .desc = "Server weight, or sum of active servers' weights for a backend" },
178 [ST_F_ACT] = { .name = "act", .desc = "Total number of active UP servers with a non-zero weight" },
179 [ST_F_BCK] = { .name = "bck", .desc = "Total number of backup UP servers with a non-zero weight" },
180 [ST_F_CHKFAIL] = { .name = "chkfail", .desc = "Total number of failed individual health checks per server/backend, since the worker process started" },
181 [ST_F_CHKDOWN] = { .name = "chkdown", .desc = "Total number of failed checks causing UP to DOWN server transitions, per server/backend, since the worker process started" },
182 [ST_F_LASTCHG] = { .name = "lastchg", .desc = "How long ago the last server state changed, in seconds" },
183 [ST_F_DOWNTIME] = { .name = "downtime", .desc = "Total time spent in DOWN state, for server or backend" },
184 [ST_F_QLIMIT] = { .name = "qlimit", .desc = "Limit on the number of connections in queue, for servers only (maxqueue argument)" },
185 [ST_F_PID] = { .name = "pid", .desc = "Relative worker process number (1..nbproc)" },
186 [ST_F_IID] = { .name = "iid", .desc = "Frontend or Backend numeric identifier ('id' setting)" },
187 [ST_F_SID] = { .name = "sid", .desc = "Server numeric identifier ('id' setting)" },
188 [ST_F_THROTTLE] = { .name = "throttle", .desc = "Throttling ratio applied to a server's maxconn and weight during the slowstart period (0 to 100%)" },
189 [ST_F_LBTOT] = { .name = "lbtot", .desc = "Total number of requests routed by load balancing since the worker process started (ignores queue pop and stickiness)" },
190 [ST_F_TRACKED] = { .name = "tracked", .desc = "Name of the other server this server tracks for its state" },
191 [ST_F_TYPE] = { .name = "type", .desc = "Type of the object (Listener, Frontend, Backend, Server)" },
192 [ST_F_RATE] = { .name = "rate", .desc = "Total number of sessions processed by this object over the last second (sessions for listeners/frontends, requests for backends/servers)" },
193 [ST_F_RATE_LIM] = { .name = "rate_lim", .desc = "Limit on the number of sessions accepted in a second (frontend only, 'rate-limit sessions' setting)" },
194 [ST_F_RATE_MAX] = { .name = "rate_max", .desc = "Highest value of 'rate' observed since the worker process started" },
195 [ST_F_CHECK_STATUS] = { .name = "check_status", .desc = "Status report of the server's latest health check, prefixed with '*' if a check is currently in progress" },
196 [ST_F_CHECK_CODE] = { .name = "check_code", .desc = "HTTP/SMTP/LDAP status code reported by the latest server health check" },
197 [ST_F_CHECK_DURATION] = { .name = "check_duration", .desc = "Total duration of the latest server health check, in milliseconds" },
198 [ST_F_HRSP_1XX] = { .name = "hrsp_1xx", .desc = "Total number of HTTP responses with status 100-199 returned by this object since the worker process started" },
199 [ST_F_HRSP_2XX] = { .name = "hrsp_2xx", .desc = "Total number of HTTP responses with status 200-299 returned by this object since the worker process started" },
200 [ST_F_HRSP_3XX] = { .name = "hrsp_3xx", .desc = "Total number of HTTP responses with status 300-399 returned by this object since the worker process started" },
201 [ST_F_HRSP_4XX] = { .name = "hrsp_4xx", .desc = "Total number of HTTP responses with status 400-499 returned by this object since the worker process started" },
202 [ST_F_HRSP_5XX] = { .name = "hrsp_5xx", .desc = "Total number of HTTP responses with status 500-599 returned by this object since the worker process started" },
203 [ST_F_HRSP_OTHER] = { .name = "hrsp_other", .desc = "Total number of HTTP responses with status <100, >599 returned by this object since the worker process started (error -1 included)" },
204 [ST_F_HANAFAIL] = { .name = "hanafail", .desc = "Total number of failed checks caused by an 'on-error' directive after an 'observe' condition matched" },
205 [ST_F_REQ_RATE] = { .name = "req_rate", .desc = "Number of HTTP requests processed over the last second on this object" },
206 [ST_F_REQ_RATE_MAX] = { .name = "req_rate_max", .desc = "Highest value of 'req_rate' observed since the worker process started" },
207 [ST_F_REQ_TOT] = { .name = "req_tot", .desc = "Total number of HTTP requests processed by this object since the worker process started" },
208 [ST_F_CLI_ABRT] = { .name = "cli_abrt", .desc = "Total number of requests or connections aborted by the client since the worker process started" },
209 [ST_F_SRV_ABRT] = { .name = "srv_abrt", .desc = "Total number of requests or connections aborted by the server since the worker process started" },
210 [ST_F_COMP_IN] = { .name = "comp_in", .desc = "Total number of bytes submitted to the HTTP compressor for this object since the worker process started" },
211 [ST_F_COMP_OUT] = { .name = "comp_out", .desc = "Total number of bytes emitted by the HTTP compressor for this object since the worker process started" },
212 [ST_F_COMP_BYP] = { .name = "comp_byp", .desc = "Total number of bytes that bypassed HTTP compression for this object since the worker process started (CPU/memory/bandwidth limitation)" },
213 [ST_F_COMP_RSP] = { .name = "comp_rsp", .desc = "Total number of HTTP responses that were compressed for this object since the worker process started" },
214 [ST_F_LASTSESS] = { .name = "lastsess", .desc = "How long ago some traffic was seen on this object on this worker process, in seconds" },
215 [ST_F_LAST_CHK] = { .name = "last_chk", .desc = "Short description of the latest health check report for this server (see also check_desc)" },
216 [ST_F_LAST_AGT] = { .name = "last_agt", .desc = "Short description of the latest agent check report for this server (see also agent_desc)" },
217 [ST_F_QTIME] = { .name = "qtime", .desc = "Time spent in the queue, in milliseconds, averaged over the 1024 last requests (backend/server)" },
218 [ST_F_CTIME] = { .name = "ctime", .desc = "Time spent waiting for a connection to complete, in milliseconds, averaged over the 1024 last requests (backend/server)" },
219 [ST_F_RTIME] = { .name = "rtime", .desc = "Time spent waiting for a server response, in milliseconds, averaged over the 1024 last requests (backend/server)" },
220 [ST_F_TTIME] = { .name = "ttime", .desc = "Total request+response time (request+queue+connect+response+processing), in milliseconds, averaged over the 1024 last requests (backend/server)" },
221 [ST_F_AGENT_STATUS] = { .name = "agent_status", .desc = "Status report of the server's latest agent check, prefixed with '*' if a check is currently in progress" },
222 [ST_F_AGENT_CODE] = { .name = "agent_code", .desc = "Status code reported by the latest server agent check" },
223 [ST_F_AGENT_DURATION] = { .name = "agent_duration", .desc = "Total duration of the latest server agent check, in milliseconds" },
224 [ST_F_CHECK_DESC] = { .name = "check_desc", .desc = "Textual description of the latest health check report for this server" },
225 [ST_F_AGENT_DESC] = { .name = "agent_desc", .desc = "Textual description of the latest agent check report for this server" },
226 [ST_F_CHECK_RISE] = { .name = "check_rise", .desc = "Number of successful health checks before declaring a server UP (server 'rise' setting)" },
227 [ST_F_CHECK_FALL] = { .name = "check_fall", .desc = "Number of failed health checks before declaring a server DOWN (server 'fall' setting)" },
228 [ST_F_CHECK_HEALTH] = { .name = "check_health", .desc = "Current server health check level (0..fall-1=DOWN, fall..rise-1=UP)" },
229 [ST_F_AGENT_RISE] = { .name = "agent_rise", .desc = "Number of successful agent checks before declaring a server UP (server 'rise' setting)" },
230 [ST_F_AGENT_FALL] = { .name = "agent_fall", .desc = "Number of failed agent checks before declaring a server DOWN (server 'fall' setting)" },
231 [ST_F_AGENT_HEALTH] = { .name = "agent_health", .desc = "Current server agent check level (0..fall-1=DOWN, fall..rise-1=UP)" },
232 [ST_F_ADDR] = { .name = "addr", .desc = "Server's address:port, shown only if show-legends is set, or at levels oper/admin for the CLI" },
233 [ST_F_COOKIE] = { .name = "cookie", .desc = "Backend's cookie name or Server's cookie value, shown only if show-legends is set, or at levels oper/admin for the CLI" },
234 [ST_F_MODE] = { .name = "mode", .desc = "'mode' setting (tcp/http/health/cli)" },
235 [ST_F_ALGO] = { .name = "algo", .desc = "Backend's load balancing algorithm, shown only if show-legends is set, or at levels oper/admin for the CLI" },
236 [ST_F_CONN_RATE] = { .name = "conn_rate", .desc = "Number of new connections accepted over the last second on the frontend for this worker process" },
237 [ST_F_CONN_RATE_MAX] = { .name = "conn_rate_max", .desc = "Highest value of 'conn_rate' observed since the worker process started" },
238 [ST_F_CONN_TOT] = { .name = "conn_tot", .desc = "Total number of new connections accepted on this frontend since the worker process started" },
239 [ST_F_INTERCEPTED] = { .name = "intercepted", .desc = "Total number of HTTP requests intercepted on the frontend (redirects/stats/services) since the worker process started" },
240 [ST_F_DCON] = { .name = "dcon", .desc = "Total number of incoming connections blocked on a listener/frontend by a tcp-request connection rule since the worker process started" },
241 [ST_F_DSES] = { .name = "dses", .desc = "Total number of incoming sessions blocked on a listener/frontend by a tcp-request connection rule since the worker process started" },
242 [ST_F_WREW] = { .name = "wrew", .desc = "Total number of failed HTTP header rewrites since the worker process started" },
243 [ST_F_CONNECT] = { .name = "connect", .desc = "Total number of outgoing connection attempts on this backend/server since the worker process started" },
244 [ST_F_REUSE] = { .name = "reuse", .desc = "Total number of reused connection on this backend/server since the worker process started" },
245 [ST_F_CACHE_LOOKUPS] = { .name = "cache_lookups", .desc = "Total number of HTTP requests looked up in the cache on this frontend/backend since the worker process started" },
246 [ST_F_CACHE_HITS] = { .name = "cache_hits", .desc = "Total number of HTTP requests not found in the cache on this frontend/backend since the worker process started" },
247 [ST_F_SRV_ICUR] = { .name = "srv_icur", .desc = "Current number of idle connections available for reuse on this server" },
248 [ST_F_SRV_ILIM] = { .name = "src_ilim", .desc = "Limit on the number of available idle connections on this server (server 'pool_max_conn' directive)" },
Christopher Faulet0d1c2a62019-11-08 14:59:51 +0100249 [ST_F_QT_MAX] = { .name = "qtime_max", .desc = "Maximum observed time spent in the queue, in milliseconds (backend/server)" },
250 [ST_F_CT_MAX] = { .name = "ctime_max", .desc = "Maximum observed time spent waiting for a connection to complete, in milliseconds (backend/server)" },
251 [ST_F_RT_MAX] = { .name = "rtime_max", .desc = "Maximum observed time spent waiting for a server response, in milliseconds (backend/server)" },
252 [ST_F_TT_MAX] = { .name = "ttime_max", .desc = "Maximum observed total request+response time (request+queue+connect+response+processing), in milliseconds (backend/server)" },
Christopher Faulet0159ee42019-12-16 14:40:39 +0100253 [ST_F_EINT] = { .name = "eint", .desc = "Total number of internal errors since process started"},
William Lallemand74c24fb2016-11-21 17:18:36 +0100254};
255
Willy Tarreau0baac8c2016-11-22 16:36:53 +0100256/* one line of info */
Christopher Faulet1bc04c72017-10-29 20:14:08 +0100257static THREAD_LOCAL struct field info[INF_TOTAL_FIELDS];
William Lallemand74c24fb2016-11-21 17:18:36 +0100258/* one line of stats */
Christopher Faulet1bc04c72017-10-29 20:14:08 +0100259static THREAD_LOCAL struct field stats[ST_F_TOTAL_FIELDS];
William Lallemand74c24fb2016-11-21 17:18:36 +0100260
Christopher Faulet6338a082019-09-09 15:50:54 +0200261static void stats_dump_json_schema(struct buffer *out);
William Lallemand74c24fb2016-11-21 17:18:36 +0100262
Christopher Fauletef779222018-10-31 08:47:01 +0100263static int stats_putchk(struct channel *chn, struct htx *htx, struct buffer *chk)
264{
265 if (htx) {
Christopher Faulet69fc88c2019-01-07 14:27:53 +0100266 if (chk->data >= channel_htx_recv_max(chn, htx))
267 return 0;
Willy Tarreau0a7ef022019-05-28 10:30:11 +0200268 if (!htx_add_data_atonce(htx, ist2(chk->area, chk->data)))
Christopher Fauletef779222018-10-31 08:47:01 +0100269 return 0;
Christopher Faulet5adbeeb2019-01-02 14:34:39 +0100270 channel_add_input(chn, chk->data);
Christopher Fauletef779222018-10-31 08:47:01 +0100271 chk->data = 0;
Christopher Fauletef779222018-10-31 08:47:01 +0100272 }
273 else {
274 if (ci_putchk(chn, chk) == -1)
275 return 0;
276 }
277 return 1;
278}
Willy Tarreau0baac8c2016-11-22 16:36:53 +0100279
Christopher Fauleted7a0662019-01-14 11:07:34 +0100280static const char *stats_scope_ptr(struct appctx *appctx, struct stream_interface *si)
281{
Christopher Fauletb7f88902019-07-15 21:56:43 +0200282 struct channel *req = si_oc(si);
283 struct htx *htx = htxbuf(&req->buf);
284 struct htx_blk *blk;
285 struct ist uri;
Christopher Fauleted7a0662019-01-14 11:07:34 +0100286
Christopher Fauletb7f88902019-07-15 21:56:43 +0200287 blk = htx_get_head_blk(htx);
Christopher Fauletea009732019-11-18 15:50:25 +0100288 BUG_ON(!blk || htx_get_blk_type(blk) != HTX_BLK_REQ_SL);
Christopher Fauletb7f88902019-07-15 21:56:43 +0200289 ALREADY_CHECKED(blk);
290 uri = htx_sl_req_uri(htx_get_blk_ptr(htx, blk));
291 return uri.ptr + appctx->ctx.stats.scope_str;
Christopher Fauleted7a0662019-01-14 11:07:34 +0100292}
293
William Lallemand74c24fb2016-11-21 17:18:36 +0100294/*
295 * http_stats_io_handler()
296 * -> stats_dump_stat_to_buffer() // same as above, but used for CSV or HTML
297 * -> stats_dump_csv_header() // emits the CSV headers (same as above)
Simon Horman05ee2132017-01-04 09:37:25 +0100298 * -> stats_dump_json_header() // emits the JSON headers (same as above)
William Lallemand74c24fb2016-11-21 17:18:36 +0100299 * -> stats_dump_html_head() // emits the HTML headers
300 * -> stats_dump_html_info() // emits the equivalent of "show info" at the top
301 * -> stats_dump_proxy_to_buffer() // same as above, valid for CSV and HTML
302 * -> stats_dump_html_px_hdr()
303 * -> stats_dump_fe_stats()
304 * -> stats_dump_li_stats()
305 * -> stats_dump_sv_stats()
306 * -> stats_dump_be_stats()
307 * -> stats_dump_html_px_end()
308 * -> stats_dump_html_end() // emits HTML trailer
Simon Horman05ee2132017-01-04 09:37:25 +0100309 * -> stats_dump_json_end() // emits JSON trailer
William Lallemand74c24fb2016-11-21 17:18:36 +0100310 */
311
312
William Lallemand74c24fb2016-11-21 17:18:36 +0100313/* Dumps the stats CSV header to the trash buffer which. The caller is responsible
314 * for clearing it if needed.
315 * NOTE: Some tools happen to rely on the field position instead of its name,
316 * so please only append new fields at the end, never in the middle.
317 */
318static void stats_dump_csv_header()
319{
320 int field;
321
322 chunk_appendf(&trash, "# ");
323 for (field = 0; field < ST_F_TOTAL_FIELDS; field++)
Willy Tarreaueaa55372019-10-09 07:39:11 +0200324 chunk_appendf(&trash, "%s,", stat_fields[field].name);
William Lallemand74c24fb2016-11-21 17:18:36 +0100325
326 chunk_appendf(&trash, "\n");
327}
328
329
330/* Emits a stats field without any surrounding element and properly encoded to
331 * resist CSV output. Returns non-zero on success, 0 if the buffer is full.
332 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200333int stats_emit_raw_data_field(struct buffer *out, const struct field *f)
William Lallemand74c24fb2016-11-21 17:18:36 +0100334{
335 switch (field_format(f, 0)) {
336 case FF_EMPTY: return 1;
337 case FF_S32: return chunk_appendf(out, "%d", f->u.s32);
338 case FF_U32: return chunk_appendf(out, "%u", f->u.u32);
339 case FF_S64: return chunk_appendf(out, "%lld", (long long)f->u.s64);
340 case FF_U64: return chunk_appendf(out, "%llu", (unsigned long long)f->u.u64);
Christopher Faulet88a0db22019-09-24 16:35:10 +0200341 case FF_FLT: return chunk_appendf(out, "%f", f->u.flt);
William Lallemand74c24fb2016-11-21 17:18:36 +0100342 case FF_STR: return csv_enc_append(field_str(f, 0), 1, out) != NULL;
343 default: return chunk_appendf(out, "[INCORRECT_FIELD_TYPE_%08x]", f->type);
344 }
345}
346
347/* Emits a stats field prefixed with its type. No CSV encoding is prepared, the
348 * output is supposed to be used on its own line. Returns non-zero on success, 0
349 * if the buffer is full.
350 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200351int stats_emit_typed_data_field(struct buffer *out, const struct field *f)
William Lallemand74c24fb2016-11-21 17:18:36 +0100352{
353 switch (field_format(f, 0)) {
354 case FF_EMPTY: return 1;
355 case FF_S32: return chunk_appendf(out, "s32:%d", f->u.s32);
356 case FF_U32: return chunk_appendf(out, "u32:%u", f->u.u32);
357 case FF_S64: return chunk_appendf(out, "s64:%lld", (long long)f->u.s64);
358 case FF_U64: return chunk_appendf(out, "u64:%llu", (unsigned long long)f->u.u64);
Christopher Faulet88a0db22019-09-24 16:35:10 +0200359 case FF_FLT: return chunk_appendf(out, "flt:%f", f->u.flt);
William Lallemand74c24fb2016-11-21 17:18:36 +0100360 case FF_STR: return chunk_appendf(out, "str:%s", field_str(f, 0));
361 default: return chunk_appendf(out, "%08x:?", f->type);
362 }
363}
364
Simon Horman05ee2132017-01-04 09:37:25 +0100365/* Limit JSON integer values to the range [-(2**53)+1, (2**53)-1] as per
366 * the recommendation for interoperable integers in section 6 of RFC 7159.
367 */
368#define JSON_INT_MAX ((1ULL << 53) - 1)
369#define JSON_INT_MIN (0 - JSON_INT_MAX)
370
371/* Emits a stats field value and its type in JSON.
372 * Returns non-zero on success, 0 on error.
373 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200374int stats_emit_json_data_field(struct buffer *out, const struct field *f)
Simon Horman05ee2132017-01-04 09:37:25 +0100375{
376 int old_len;
377 char buf[20];
378 const char *type, *value = buf, *quote = "";
379
380 switch (field_format(f, 0)) {
381 case FF_EMPTY: return 1;
382 case FF_S32: type = "\"s32\"";
383 snprintf(buf, sizeof(buf), "%d", f->u.s32);
384 break;
385 case FF_U32: type = "\"u32\"";
386 snprintf(buf, sizeof(buf), "%u", f->u.u32);
387 break;
388 case FF_S64: type = "\"s64\"";
389 if (f->u.s64 < JSON_INT_MIN || f->u.s64 > JSON_INT_MAX)
390 return 0;
391 type = "\"s64\"";
392 snprintf(buf, sizeof(buf), "%lld", (long long)f->u.s64);
393 break;
394 case FF_U64: if (f->u.u64 > JSON_INT_MAX)
395 return 0;
396 type = "\"u64\"";
397 snprintf(buf, sizeof(buf), "%llu",
398 (unsigned long long) f->u.u64);
Christopher Faulet52c91bb2019-09-28 10:37:31 +0200399 break;
Christopher Faulet88a0db22019-09-24 16:35:10 +0200400 case FF_FLT: type = "\"flt\"";
401 snprintf(buf, sizeof(buf), "%f", f->u.flt);
Simon Horman05ee2132017-01-04 09:37:25 +0100402 break;
403 case FF_STR: type = "\"str\"";
404 value = field_str(f, 0);
405 quote = "\"";
406 break;
407 default: snprintf(buf, sizeof(buf), "%u", f->type);
408 type = buf;
409 value = "unknown";
410 quote = "\"";
411 break;
412 }
413
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200414 old_len = out->data;
Simon Horman05ee2132017-01-04 09:37:25 +0100415 chunk_appendf(out, ",\"value\":{\"type\":%s,\"value\":%s%s%s}",
416 type, quote, value, quote);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200417 return !(old_len == out->data);
Simon Horman05ee2132017-01-04 09:37:25 +0100418}
419
William Lallemand74c24fb2016-11-21 17:18:36 +0100420/* Emits an encoding of the field type on 3 characters followed by a delimiter.
421 * Returns non-zero on success, 0 if the buffer is full.
422 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200423int stats_emit_field_tags(struct buffer *out, const struct field *f,
424 char delim)
William Lallemand74c24fb2016-11-21 17:18:36 +0100425{
426 char origin, nature, scope;
427
428 switch (field_origin(f, 0)) {
429 case FO_METRIC: origin = 'M'; break;
430 case FO_STATUS: origin = 'S'; break;
431 case FO_KEY: origin = 'K'; break;
432 case FO_CONFIG: origin = 'C'; break;
433 case FO_PRODUCT: origin = 'P'; break;
434 default: origin = '?'; break;
435 }
436
437 switch (field_nature(f, 0)) {
438 case FN_GAUGE: nature = 'G'; break;
439 case FN_LIMIT: nature = 'L'; break;
440 case FN_MIN: nature = 'm'; break;
441 case FN_MAX: nature = 'M'; break;
442 case FN_RATE: nature = 'R'; break;
443 case FN_COUNTER: nature = 'C'; break;
444 case FN_DURATION: nature = 'D'; break;
445 case FN_AGE: nature = 'A'; break;
446 case FN_TIME: nature = 'T'; break;
447 case FN_NAME: nature = 'N'; break;
448 case FN_OUTPUT: nature = 'O'; break;
449 case FN_AVG: nature = 'a'; break;
450 default: nature = '?'; break;
451 }
452
453 switch (field_scope(f, 0)) {
454 case FS_PROCESS: scope = 'P'; break;
455 case FS_SERVICE: scope = 'S'; break;
456 case FS_SYSTEM: scope = 's'; break;
457 case FS_CLUSTER: scope = 'C'; break;
458 default: scope = '?'; break;
459 }
460
461 return chunk_appendf(out, "%c%c%c%c", origin, nature, scope, delim);
462}
463
Simon Horman05ee2132017-01-04 09:37:25 +0100464/* Emits an encoding of the field type as JSON.
465 * Returns non-zero on success, 0 if the buffer is full.
466 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200467int stats_emit_json_field_tags(struct buffer *out, const struct field *f)
Simon Horman05ee2132017-01-04 09:37:25 +0100468{
469 const char *origin, *nature, *scope;
470 int old_len;
471
472 switch (field_origin(f, 0)) {
473 case FO_METRIC: origin = "Metric"; break;
474 case FO_STATUS: origin = "Status"; break;
475 case FO_KEY: origin = "Key"; break;
476 case FO_CONFIG: origin = "Config"; break;
477 case FO_PRODUCT: origin = "Product"; break;
478 default: origin = "Unknown"; break;
479 }
480
481 switch (field_nature(f, 0)) {
482 case FN_GAUGE: nature = "Gauge"; break;
483 case FN_LIMIT: nature = "Limit"; break;
484 case FN_MIN: nature = "Min"; break;
485 case FN_MAX: nature = "Max"; break;
486 case FN_RATE: nature = "Rate"; break;
487 case FN_COUNTER: nature = "Counter"; break;
488 case FN_DURATION: nature = "Duration"; break;
489 case FN_AGE: nature = "Age"; break;
490 case FN_TIME: nature = "Time"; break;
491 case FN_NAME: nature = "Name"; break;
492 case FN_OUTPUT: nature = "Output"; break;
493 case FN_AVG: nature = "Avg"; break;
494 default: nature = "Unknown"; break;
495 }
496
497 switch (field_scope(f, 0)) {
498 case FS_PROCESS: scope = "Process"; break;
499 case FS_SERVICE: scope = "Service"; break;
500 case FS_SYSTEM: scope = "System"; break;
501 case FS_CLUSTER: scope = "Cluster"; break;
502 default: scope = "Unknown"; break;
503 }
504
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200505 old_len = out->data;
Simon Horman05ee2132017-01-04 09:37:25 +0100506 chunk_appendf(out, "\"tags\":{"
507 "\"origin\":\"%s\","
508 "\"nature\":\"%s\","
509 "\"scope\":\"%s\""
510 "}", origin, nature, scope);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200511 return !(old_len == out->data);
Simon Horman05ee2132017-01-04 09:37:25 +0100512}
William Lallemand74c24fb2016-11-21 17:18:36 +0100513
514/* Dump all fields from <stats> into <out> using CSV format */
Willy Tarreau83061a82018-07-13 11:56:34 +0200515static int stats_dump_fields_csv(struct buffer *out,
Willy Tarreau43241ff2019-10-09 11:27:51 +0200516 const struct field *stats, unsigned int flags)
William Lallemand74c24fb2016-11-21 17:18:36 +0100517{
518 int field;
519
520 for (field = 0; field < ST_F_TOTAL_FIELDS; field++) {
521 if (!stats_emit_raw_data_field(out, &stats[field]))
522 return 0;
523 if (!chunk_strcat(out, ","))
524 return 0;
525 }
526 chunk_strcat(out, "\n");
527 return 1;
528}
529
530/* Dump all fields from <stats> into <out> using a typed "field:desc:type:value" format */
Willy Tarreau83061a82018-07-13 11:56:34 +0200531static int stats_dump_fields_typed(struct buffer *out,
Willy Tarreau43241ff2019-10-09 11:27:51 +0200532 const struct field *stats, unsigned int flags)
William Lallemand74c24fb2016-11-21 17:18:36 +0100533{
534 int field;
535
536 for (field = 0; field < ST_F_TOTAL_FIELDS; field++) {
537 if (!stats[field].type)
538 continue;
539
540 chunk_appendf(out, "%c.%u.%u.%d.%s.%u:",
541 stats[ST_F_TYPE].u.u32 == STATS_TYPE_FE ? 'F' :
542 stats[ST_F_TYPE].u.u32 == STATS_TYPE_BE ? 'B' :
543 stats[ST_F_TYPE].u.u32 == STATS_TYPE_SO ? 'L' :
544 stats[ST_F_TYPE].u.u32 == STATS_TYPE_SV ? 'S' :
545 '?',
546 stats[ST_F_IID].u.u32, stats[ST_F_SID].u.u32,
Willy Tarreaueaa55372019-10-09 07:39:11 +0200547 field, stat_fields[field].name, stats[ST_F_PID].u.u32);
William Lallemand74c24fb2016-11-21 17:18:36 +0100548
549 if (!stats_emit_field_tags(out, &stats[field], ':'))
550 return 0;
551 if (!stats_emit_typed_data_field(out, &stats[field]))
552 return 0;
Willy Tarreau6b19b142019-10-09 15:44:21 +0200553 if ((flags & STAT_SHOW_FDESC) && !chunk_appendf(out, ":\"%s\"", stat_fields[field].desc))
554 return 0;
William Lallemand74c24fb2016-11-21 17:18:36 +0100555 if (!chunk_strcat(out, "\n"))
556 return 0;
557 }
558 return 1;
559}
560
Simon Horman05ee2132017-01-04 09:37:25 +0100561/* Dump all fields from <stats> into <out> using the "show info json" format */
Willy Tarreau83061a82018-07-13 11:56:34 +0200562static int stats_dump_json_info_fields(struct buffer *out,
Willy Tarreau43241ff2019-10-09 11:27:51 +0200563 const struct field *info, unsigned int flags)
Simon Horman05ee2132017-01-04 09:37:25 +0100564{
565 int field;
566 int started = 0;
567
568 if (!chunk_strcat(out, "["))
569 return 0;
570
571 for (field = 0; field < INF_TOTAL_FIELDS; field++) {
572 int old_len;
573
574 if (!field_format(info, field))
575 continue;
576
577 if (started && !chunk_strcat(out, ","))
578 goto err;
579 started = 1;
580
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200581 old_len = out->data;
Simon Horman05ee2132017-01-04 09:37:25 +0100582 chunk_appendf(out,
583 "{\"field\":{\"pos\":%d,\"name\":\"%s\"},"
584 "\"processNum\":%u,",
Willy Tarreaueaa55372019-10-09 07:39:11 +0200585 field, info_fields[field].name,
Simon Horman05ee2132017-01-04 09:37:25 +0100586 info[INF_PROCESS_NUM].u.u32);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200587 if (old_len == out->data)
Simon Horman05ee2132017-01-04 09:37:25 +0100588 goto err;
589
590 if (!stats_emit_json_field_tags(out, &info[field]))
591 goto err;
592
593 if (!stats_emit_json_data_field(out, &info[field]))
594 goto err;
595
596 if (!chunk_strcat(out, "}"))
597 goto err;
598 }
599
600 if (!chunk_strcat(out, "]"))
601 goto err;
602 return 1;
603
604err:
605 chunk_reset(out);
606 chunk_appendf(out, "{\"errorStr\":\"output buffer too short\"}");
607 return 0;
608}
609
610/* Dump all fields from <stats> into <out> using a typed "field:desc:type:value" format */
Willy Tarreau83061a82018-07-13 11:56:34 +0200611static int stats_dump_fields_json(struct buffer *out,
612 const struct field *stats,
Willy Tarreaub0ce3ad2019-10-09 11:19:29 +0200613 unsigned int flags)
Simon Horman05ee2132017-01-04 09:37:25 +0100614{
615 int field;
616 int started = 0;
617
Willy Tarreaub0ce3ad2019-10-09 11:19:29 +0200618 if ((flags & STAT_STARTED) && !chunk_strcat(out, ","))
Simon Horman05ee2132017-01-04 09:37:25 +0100619 return 0;
620 if (!chunk_strcat(out, "["))
621 return 0;
622
623 for (field = 0; field < ST_F_TOTAL_FIELDS; field++) {
624 const char *obj_type;
625 int old_len;
626
627 if (!stats[field].type)
628 continue;
629
630 if (started && !chunk_strcat(out, ","))
631 goto err;
632 started = 1;
633
634 switch (stats[ST_F_TYPE].u.u32) {
635 case STATS_TYPE_FE: obj_type = "Frontend"; break;
636 case STATS_TYPE_BE: obj_type = "Backend"; break;
637 case STATS_TYPE_SO: obj_type = "Listener"; break;
638 case STATS_TYPE_SV: obj_type = "Server"; break;
639 default: obj_type = "Unknown"; break;
640 }
641
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200642 old_len = out->data;
Simon Horman05ee2132017-01-04 09:37:25 +0100643 chunk_appendf(out,
644 "{"
645 "\"objType\":\"%s\","
646 "\"proxyId\":%d,"
647 "\"id\":%d,"
648 "\"field\":{\"pos\":%d,\"name\":\"%s\"},"
649 "\"processNum\":%u,",
650 obj_type, stats[ST_F_IID].u.u32,
651 stats[ST_F_SID].u.u32, field,
Willy Tarreaueaa55372019-10-09 07:39:11 +0200652 stat_fields[field].name, stats[ST_F_PID].u.u32);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200653 if (old_len == out->data)
Simon Horman05ee2132017-01-04 09:37:25 +0100654 goto err;
655
656 if (!stats_emit_json_field_tags(out, &stats[field]))
657 goto err;
658
659 if (!stats_emit_json_data_field(out, &stats[field]))
660 goto err;
661
662 if (!chunk_strcat(out, "}"))
663 goto err;
664 }
665
666 if (!chunk_strcat(out, "]"))
667 goto err;
668
669 return 1;
670
671err:
672 chunk_reset(out);
Willy Tarreaub0ce3ad2019-10-09 11:19:29 +0200673 if (flags & STAT_STARTED)
674 chunk_strcat(out, ",");
Simon Horman05ee2132017-01-04 09:37:25 +0100675 chunk_appendf(out, "{\"errorStr\":\"output buffer too short\"}");
676 return 0;
677}
678
William Lallemand74c24fb2016-11-21 17:18:36 +0100679/* Dump all fields from <stats> into <out> using the HTML format. A column is
Willy Tarreauab02b3f2019-10-09 11:11:46 +0200680 * reserved for the checkbox is STAT_ADMIN is set in <flags>. Some extra info
Willy Tarreau708c4162019-10-09 10:19:16 +0200681 * are provided if STAT_SHLGNDS is present in <flags>.
William Lallemand74c24fb2016-11-21 17:18:36 +0100682 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200683static int stats_dump_fields_html(struct buffer *out,
684 const struct field *stats,
685 unsigned int flags)
William Lallemand74c24fb2016-11-21 17:18:36 +0100686{
Willy Tarreau83061a82018-07-13 11:56:34 +0200687 struct buffer src;
William Lallemand74c24fb2016-11-21 17:18:36 +0100688
689 if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_FE) {
690 chunk_appendf(out,
691 /* name, queue */
692 "<tr class=\"frontend\">");
693
Willy Tarreauab02b3f2019-10-09 11:11:46 +0200694 if (flags & STAT_ADMIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +0100695 /* Column sub-heading for Enable or Disable server */
696 chunk_appendf(out, "<td></td>");
697 }
698
699 chunk_appendf(out,
700 "<td class=ac>"
701 "<a name=\"%s/Frontend\"></a>"
702 "<a class=lfsb href=\"#%s/Frontend\">Frontend</a></td>"
703 "<td colspan=3></td>"
704 "",
705 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_PXNAME));
706
707 chunk_appendf(out,
708 /* sessions rate : current */
709 "<td><u>%s<div class=tips><table class=det>"
710 "<tr><th>Current connection rate:</th><td>%s/s</td></tr>"
711 "<tr><th>Current session rate:</th><td>%s/s</td></tr>"
712 "",
713 U2H(stats[ST_F_RATE].u.u32),
714 U2H(stats[ST_F_CONN_RATE].u.u32),
715 U2H(stats[ST_F_RATE].u.u32));
716
717 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
718 chunk_appendf(out,
719 "<tr><th>Current request rate:</th><td>%s/s</td></tr>",
720 U2H(stats[ST_F_REQ_RATE].u.u32));
721
722 chunk_appendf(out,
723 "</table></div></u></td>"
724 /* sessions rate : max */
725 "<td><u>%s<div class=tips><table class=det>"
726 "<tr><th>Max connection rate:</th><td>%s/s</td></tr>"
727 "<tr><th>Max session rate:</th><td>%s/s</td></tr>"
728 "",
729 U2H(stats[ST_F_RATE_MAX].u.u32),
730 U2H(stats[ST_F_CONN_RATE_MAX].u.u32),
731 U2H(stats[ST_F_RATE_MAX].u.u32));
732
733 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
734 chunk_appendf(out,
735 "<tr><th>Max request rate:</th><td>%s/s</td></tr>",
736 U2H(stats[ST_F_REQ_RATE_MAX].u.u32));
737
738 chunk_appendf(out,
739 "</table></div></u></td>"
740 /* sessions rate : limit */
741 "<td>%s</td>",
742 LIM2A(stats[ST_F_RATE_LIM].u.u32, "-"));
743
744 chunk_appendf(out,
745 /* sessions: current, max, limit, total */
746 "<td>%s</td><td>%s</td><td>%s</td>"
747 "<td><u>%s<div class=tips><table class=det>"
748 "<tr><th>Cum. connections:</th><td>%s</td></tr>"
749 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
750 "",
751 U2H(stats[ST_F_SCUR].u.u32), U2H(stats[ST_F_SMAX].u.u32), U2H(stats[ST_F_SLIM].u.u32),
752 U2H(stats[ST_F_STOT].u.u64),
753 U2H(stats[ST_F_CONN_TOT].u.u64),
754 U2H(stats[ST_F_STOT].u.u64));
755
756 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
757 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0) {
758 chunk_appendf(out,
759 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
760 "<tr><th>- HTTP 1xx responses:</th><td>%s</td></tr>"
761 "<tr><th>- HTTP 2xx responses:</th><td>%s</td></tr>"
762 "<tr><th>&nbsp;&nbsp;Compressed 2xx:</th><td>%s</td><td>(%d%%)</td></tr>"
763 "<tr><th>- HTTP 3xx responses:</th><td>%s</td></tr>"
764 "<tr><th>- HTTP 4xx responses:</th><td>%s</td></tr>"
765 "<tr><th>- HTTP 5xx responses:</th><td>%s</td></tr>"
766 "<tr><th>- other responses:</th><td>%s</td></tr>"
767 "<tr><th>Intercepted requests:</th><td>%s</td></tr>"
Willy Tarreaua1214a52018-12-14 14:00:25 +0100768 "<tr><th>Cache lookups:</th><td>%s</td></tr>"
769 "<tr><th>Cache hits:</th><td>%s</td><td>(%d%%)</td></tr>"
Willy Tarreau1b0f85e2018-05-28 15:12:40 +0200770 "<tr><th>Failed hdr rewrites:</th><td>%s</td></tr>"
Christopher Faulet0159ee42019-12-16 14:40:39 +0100771 "<tr><th>Internal errors:</th><td>%s</td></tr>"
William Lallemand74c24fb2016-11-21 17:18:36 +0100772 "",
773 U2H(stats[ST_F_REQ_TOT].u.u64),
774 U2H(stats[ST_F_HRSP_1XX].u.u64),
775 U2H(stats[ST_F_HRSP_2XX].u.u64),
776 U2H(stats[ST_F_COMP_RSP].u.u64),
777 stats[ST_F_HRSP_2XX].u.u64 ?
778 (int)(100 * stats[ST_F_COMP_RSP].u.u64 / stats[ST_F_HRSP_2XX].u.u64) : 0,
779 U2H(stats[ST_F_HRSP_3XX].u.u64),
780 U2H(stats[ST_F_HRSP_4XX].u.u64),
781 U2H(stats[ST_F_HRSP_5XX].u.u64),
782 U2H(stats[ST_F_HRSP_OTHER].u.u64),
Willy Tarreau1b0f85e2018-05-28 15:12:40 +0200783 U2H(stats[ST_F_INTERCEPTED].u.u64),
Willy Tarreaua1214a52018-12-14 14:00:25 +0100784 U2H(stats[ST_F_CACHE_LOOKUPS].u.u64),
785 U2H(stats[ST_F_CACHE_HITS].u.u64),
786 stats[ST_F_CACHE_LOOKUPS].u.u64 ?
787 (int)(100 * stats[ST_F_CACHE_HITS].u.u64 / stats[ST_F_CACHE_LOOKUPS].u.u64) : 0,
Christopher Faulet0159ee42019-12-16 14:40:39 +0100788 U2H(stats[ST_F_WREW].u.u64),
789 U2H(stats[ST_F_EINT].u.u64));
William Lallemand74c24fb2016-11-21 17:18:36 +0100790 }
791
792 chunk_appendf(out,
793 "</table></div></u></td>"
794 /* sessions: lbtot, lastsess */
795 "<td></td><td></td>"
796 /* bytes : in */
797 "<td>%s</td>"
798 "",
799 U2H(stats[ST_F_BIN].u.u64));
800
801 chunk_appendf(out,
802 /* bytes:out + compression stats (via hover): comp_in, comp_out, comp_byp */
803 "<td>%s%s<div class=tips><table class=det>"
804 "<tr><th>Response bytes in:</th><td>%s</td></tr>"
805 "<tr><th>Compression in:</th><td>%s</td></tr>"
806 "<tr><th>Compression out:</th><td>%s</td><td>(%d%%)</td></tr>"
807 "<tr><th>Compression bypass:</th><td>%s</td></tr>"
808 "<tr><th>Total bytes saved:</th><td>%s</td><td>(%d%%)</td></tr>"
809 "</table></div>%s</td>",
810 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "<u>":"",
811 U2H(stats[ST_F_BOUT].u.u64),
812 U2H(stats[ST_F_BOUT].u.u64),
813 U2H(stats[ST_F_COMP_IN].u.u64),
814 U2H(stats[ST_F_COMP_OUT].u.u64),
815 stats[ST_F_COMP_IN].u.u64 ? (int)(stats[ST_F_COMP_OUT].u.u64 * 100 / stats[ST_F_COMP_IN].u.u64) : 0,
816 U2H(stats[ST_F_COMP_BYP].u.u64),
817 U2H(stats[ST_F_COMP_IN].u.u64 - stats[ST_F_COMP_OUT].u.u64),
818 stats[ST_F_BOUT].u.u64 ? (int)((stats[ST_F_COMP_IN].u.u64 - stats[ST_F_COMP_OUT].u.u64) * 100 / stats[ST_F_BOUT].u.u64) : 0,
819 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "</u>":"");
820
821 chunk_appendf(out,
822 /* denied: req, resp */
823 "<td>%s</td><td>%s</td>"
824 /* errors : request, connect, response */
825 "<td>%s</td><td></td><td></td>"
826 /* warnings: retries, redispatches */
827 "<td></td><td></td>"
828 /* server status : reflect frontend status */
829 "<td class=ac>%s</td>"
830 /* rest of server: nothing */
831 "<td class=ac colspan=8></td></tr>"
832 "",
833 U2H(stats[ST_F_DREQ].u.u64), U2H(stats[ST_F_DRESP].u.u64),
834 U2H(stats[ST_F_EREQ].u.u64),
835 field_str(stats, ST_F_STATUS));
836 }
837 else if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_SO) {
838 chunk_appendf(out, "<tr class=socket>");
Willy Tarreauab02b3f2019-10-09 11:11:46 +0200839 if (flags & STAT_ADMIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +0100840 /* Column sub-heading for Enable or Disable server */
841 chunk_appendf(out, "<td></td>");
842 }
843
844 chunk_appendf(out,
845 /* frontend name, listener name */
846 "<td class=ac><a name=\"%s/+%s\"></a>%s"
847 "<a class=lfsb href=\"#%s/+%s\">%s</a>"
848 "",
849 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME),
Willy Tarreau708c4162019-10-09 10:19:16 +0200850 (flags & STAT_SHLGNDS)?"<u>":"",
William Lallemand74c24fb2016-11-21 17:18:36 +0100851 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME), field_str(stats, ST_F_SVNAME));
852
Willy Tarreau708c4162019-10-09 10:19:16 +0200853 if (flags & STAT_SHLGNDS) {
William Lallemand74c24fb2016-11-21 17:18:36 +0100854 chunk_appendf(out, "<div class=tips>");
855
856 if (isdigit(*field_str(stats, ST_F_ADDR)))
857 chunk_appendf(out, "IPv4: %s, ", field_str(stats, ST_F_ADDR));
858 else if (*field_str(stats, ST_F_ADDR) == '[')
859 chunk_appendf(out, "IPv6: %s, ", field_str(stats, ST_F_ADDR));
860 else if (*field_str(stats, ST_F_ADDR))
861 chunk_appendf(out, "%s, ", field_str(stats, ST_F_ADDR));
862
863 /* id */
864 chunk_appendf(out, "id: %d</div>", stats[ST_F_SID].u.u32);
865 }
866
867 chunk_appendf(out,
868 /* queue */
869 "%s</td><td colspan=3></td>"
870 /* sessions rate: current, max, limit */
871 "<td colspan=3>&nbsp;</td>"
872 /* sessions: current, max, limit, total, lbtot, lastsess */
873 "<td>%s</td><td>%s</td><td>%s</td>"
874 "<td>%s</td><td>&nbsp;</td><td>&nbsp;</td>"
875 /* bytes: in, out */
876 "<td>%s</td><td>%s</td>"
877 "",
Willy Tarreau708c4162019-10-09 10:19:16 +0200878 (flags & STAT_SHLGNDS)?"</u>":"",
William Lallemand74c24fb2016-11-21 17:18:36 +0100879 U2H(stats[ST_F_SCUR].u.u32), U2H(stats[ST_F_SMAX].u.u32), U2H(stats[ST_F_SLIM].u.u32),
880 U2H(stats[ST_F_STOT].u.u64), U2H(stats[ST_F_BIN].u.u64), U2H(stats[ST_F_BOUT].u.u64));
881
882 chunk_appendf(out,
883 /* denied: req, resp */
884 "<td>%s</td><td>%s</td>"
885 /* errors: request, connect, response */
886 "<td>%s</td><td></td><td></td>"
887 /* warnings: retries, redispatches */
888 "<td></td><td></td>"
889 /* server status: reflect listener status */
890 "<td class=ac>%s</td>"
891 /* rest of server: nothing */
892 "<td class=ac colspan=8></td></tr>"
893 "",
894 U2H(stats[ST_F_DREQ].u.u64), U2H(stats[ST_F_DRESP].u.u64),
895 U2H(stats[ST_F_EREQ].u.u64),
896 field_str(stats, ST_F_STATUS));
897 }
898 else if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_SV) {
899 const char *style;
900
901 /* determine the style to use depending on the server's state,
902 * its health and weight. There isn't a 1-to-1 mapping between
903 * state and styles for the cases where the server is (still)
904 * up. The reason is that we don't want to report nolb and
905 * drain with the same color.
906 */
907
908 if (strcmp(field_str(stats, ST_F_STATUS), "DOWN") == 0 ||
909 strcmp(field_str(stats, ST_F_STATUS), "DOWN (agent)") == 0) {
910 style = "down";
911 }
912 else if (strcmp(field_str(stats, ST_F_STATUS), "DOWN ") == 0) {
913 style = "going_up";
914 }
915 else if (strcmp(field_str(stats, ST_F_STATUS), "NOLB ") == 0) {
916 style = "going_down";
917 }
918 else if (strcmp(field_str(stats, ST_F_STATUS), "NOLB") == 0) {
919 style = "nolb";
920 }
921 else if (strcmp(field_str(stats, ST_F_STATUS), "no check") == 0) {
922 style = "no_check";
923 }
924 else if (!stats[ST_F_CHKFAIL].type ||
925 stats[ST_F_CHECK_HEALTH].u.u32 == stats[ST_F_CHECK_RISE].u.u32 + stats[ST_F_CHECK_FALL].u.u32 - 1) {
926 /* no check or max health = UP */
927 if (stats[ST_F_WEIGHT].u.u32)
928 style = "up";
929 else
930 style = "draining";
931 }
932 else {
933 style = "going_down";
934 }
935
936 if (memcmp(field_str(stats, ST_F_STATUS), "MAINT", 5) == 0)
937 chunk_appendf(out, "<tr class=\"maintain\">");
938 else
939 chunk_appendf(out,
940 "<tr class=\"%s_%s\">",
941 (stats[ST_F_BCK].u.u32) ? "backup" : "active", style);
942
943
Willy Tarreauab02b3f2019-10-09 11:11:46 +0200944 if (flags & STAT_ADMIN)
William Lallemand74c24fb2016-11-21 17:18:36 +0100945 chunk_appendf(out,
David Harrigand3db35a2016-12-30 12:12:49 +0000946 "<td><input class='%s-checkbox' type=\"checkbox\" name=\"s\" value=\"%s\"></td>",
947 field_str(stats, ST_F_PXNAME),
William Lallemand74c24fb2016-11-21 17:18:36 +0100948 field_str(stats, ST_F_SVNAME));
949
950 chunk_appendf(out,
951 "<td class=ac><a name=\"%s/%s\"></a>%s"
952 "<a class=lfsb href=\"#%s/%s\">%s</a>"
953 "",
954 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME),
Willy Tarreau708c4162019-10-09 10:19:16 +0200955 (flags & STAT_SHLGNDS) ? "<u>" : "",
William Lallemand74c24fb2016-11-21 17:18:36 +0100956 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME), field_str(stats, ST_F_SVNAME));
957
Willy Tarreau708c4162019-10-09 10:19:16 +0200958 if (flags & STAT_SHLGNDS) {
William Lallemand74c24fb2016-11-21 17:18:36 +0100959 chunk_appendf(out, "<div class=tips>");
960
961 if (isdigit(*field_str(stats, ST_F_ADDR)))
962 chunk_appendf(out, "IPv4: %s, ", field_str(stats, ST_F_ADDR));
963 else if (*field_str(stats, ST_F_ADDR) == '[')
964 chunk_appendf(out, "IPv6: %s, ", field_str(stats, ST_F_ADDR));
965 else if (*field_str(stats, ST_F_ADDR))
966 chunk_appendf(out, "%s, ", field_str(stats, ST_F_ADDR));
967
968 /* id */
969 chunk_appendf(out, "id: %d", stats[ST_F_SID].u.u32);
970
971 /* cookie */
972 if (stats[ST_F_COOKIE].type) {
973 chunk_appendf(out, ", cookie: '");
974 chunk_initstr(&src, field_str(stats, ST_F_COOKIE));
975 chunk_htmlencode(out, &src);
976 chunk_appendf(out, "'");
977 }
978
979 chunk_appendf(out, "</div>");
980 }
981
982 chunk_appendf(out,
983 /* queue : current, max, limit */
984 "%s</td><td>%s</td><td>%s</td><td>%s</td>"
985 /* sessions rate : current, max, limit */
986 "<td>%s</td><td>%s</td><td></td>"
987 "",
Willy Tarreau708c4162019-10-09 10:19:16 +0200988 (flags & STAT_SHLGNDS) ? "</u>" : "",
William Lallemand74c24fb2016-11-21 17:18:36 +0100989 U2H(stats[ST_F_QCUR].u.u32), U2H(stats[ST_F_QMAX].u.u32), LIM2A(stats[ST_F_QLIMIT].u.u32, "-"),
990 U2H(stats[ST_F_RATE].u.u32), U2H(stats[ST_F_RATE_MAX].u.u32));
991
992 chunk_appendf(out,
993 /* sessions: current, max, limit, total */
Willy Tarreauf21d17b2019-09-08 09:24:56 +0200994 "<td><u>%s<div class=tips>"
995 "<table class=det>"
996 "<tr><th>Current active connections:</th><td>%s</td></tr>"
997 "<tr><th>Current idle connections:</th><td>%s</td></tr>"
998 "<tr><th>Active connections limit:</th><td>%s</td></tr>"
999 "<tr><th>Idle connections limit:</th><td>%s</td></tr>"
1000 "</table></div></u>"
1001 "</td><td>%s</td><td>%s</td>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001002 "<td><u>%s<div class=tips><table class=det>"
1003 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
1004 "",
Willy Tarreauf21d17b2019-09-08 09:24:56 +02001005 U2H(stats[ST_F_SCUR].u.u32),
1006 U2H(stats[ST_F_SCUR].u.u32),
1007 U2H(stats[ST_F_SRV_ICUR].u.u32),
1008 LIM2A(stats[ST_F_SLIM].u.u32, "-"),
1009 stats[ST_F_SRV_ILIM].type ? U2H(stats[ST_F_SRV_ILIM].u.u32) : "-",
1010 U2H(stats[ST_F_SMAX].u.u32), LIM2A(stats[ST_F_SLIM].u.u32, "-"),
William Lallemand74c24fb2016-11-21 17:18:36 +01001011 U2H(stats[ST_F_STOT].u.u64),
1012 U2H(stats[ST_F_STOT].u.u64));
1013
1014 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
1015 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0) {
1016 unsigned long long tot;
1017
1018 tot = stats[ST_F_HRSP_OTHER].u.u64;
1019 tot += stats[ST_F_HRSP_1XX].u.u64;
1020 tot += stats[ST_F_HRSP_2XX].u.u64;
1021 tot += stats[ST_F_HRSP_3XX].u.u64;
1022 tot += stats[ST_F_HRSP_4XX].u.u64;
1023 tot += stats[ST_F_HRSP_5XX].u.u64;
1024
1025 chunk_appendf(out,
Willy Tarreauf1573842018-12-14 11:35:36 +01001026 "<tr><th>New connections:</th><td>%s</td></tr>"
1027 "<tr><th>Reused connections:</th><td>%s</td><td>(%d%%)</td></tr>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001028 "<tr><th>Cum. HTTP responses:</th><td>%s</td></tr>"
1029 "<tr><th>- HTTP 1xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
1030 "<tr><th>- HTTP 2xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
1031 "<tr><th>- HTTP 3xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
1032 "<tr><th>- HTTP 4xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
1033 "<tr><th>- HTTP 5xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
1034 "<tr><th>- other responses:</th><td>%s</td><td>(%d%%)</td></tr>"
Willy Tarreau1b0f85e2018-05-28 15:12:40 +02001035 "<tr><th>Failed hdr rewrites:</th><td>%s</td></tr>"
Christopher Faulet0159ee42019-12-16 14:40:39 +01001036 "<tr><th>Internal error:</th><td>%s</td></tr>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001037 "",
Willy Tarreauf1573842018-12-14 11:35:36 +01001038 U2H(stats[ST_F_CONNECT].u.u64),
1039 U2H(stats[ST_F_REUSE].u.u64),
1040 (stats[ST_F_CONNECT].u.u64 + stats[ST_F_REUSE].u.u64) ?
1041 (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 +01001042 U2H(tot),
1043 U2H(stats[ST_F_HRSP_1XX].u.u64), tot ? (int)(100 * stats[ST_F_HRSP_1XX].u.u64 / tot) : 0,
1044 U2H(stats[ST_F_HRSP_2XX].u.u64), tot ? (int)(100 * stats[ST_F_HRSP_2XX].u.u64 / tot) : 0,
1045 U2H(stats[ST_F_HRSP_3XX].u.u64), tot ? (int)(100 * stats[ST_F_HRSP_3XX].u.u64 / tot) : 0,
1046 U2H(stats[ST_F_HRSP_4XX].u.u64), tot ? (int)(100 * stats[ST_F_HRSP_4XX].u.u64 / tot) : 0,
1047 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 +02001048 U2H(stats[ST_F_HRSP_OTHER].u.u64), tot ? (int)(100 * stats[ST_F_HRSP_OTHER].u.u64 / tot) : 0,
Christopher Faulet0159ee42019-12-16 14:40:39 +01001049 U2H(stats[ST_F_WREW].u.u64),
1050 U2H(stats[ST_F_EINT].u.u64));
William Lallemand74c24fb2016-11-21 17:18:36 +01001051 }
1052
Christopher Faulet0d1c2a62019-11-08 14:59:51 +01001053 chunk_appendf(out, "<tr><th colspan=3>Max / Avg over last 1024 success. conn.</th></tr>");
1054 chunk_appendf(out, "<tr><th>- Queue time:</th><td>%s / %s</td><td>ms</td></tr>",
1055 U2H(stats[ST_F_QT_MAX].u.u32), U2H(stats[ST_F_QTIME].u.u32));
1056 chunk_appendf(out, "<tr><th>- Connect time:</th><td>%s / %s</td><td>ms</td></tr>",
1057 U2H(stats[ST_F_CT_MAX].u.u32), U2H(stats[ST_F_CTIME].u.u32));
William Lallemand74c24fb2016-11-21 17:18:36 +01001058 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
Christopher Faulet0d1c2a62019-11-08 14:59:51 +01001059 chunk_appendf(out, "<tr><th>- Responses time:</th><td>%s / %s</td><td>ms</td></tr>",
1060 U2H(stats[ST_F_RT_MAX].u.u32), U2H(stats[ST_F_RTIME].u.u32));
1061 chunk_appendf(out, "<tr><th>- Total time:</th><td>%s / %s</td><td>ms</td></tr>",
1062 U2H(stats[ST_F_TT_MAX].u.u32), U2H(stats[ST_F_TTIME].u.u32));
William Lallemand74c24fb2016-11-21 17:18:36 +01001063
1064 chunk_appendf(out,
1065 "</table></div></u></td>"
1066 /* sessions: lbtot, last */
1067 "<td>%s</td><td>%s</td>",
1068 U2H(stats[ST_F_LBTOT].u.u64),
1069 human_time(stats[ST_F_LASTSESS].u.s32, 1));
1070
1071 chunk_appendf(out,
1072 /* bytes : in, out */
1073 "<td>%s</td><td>%s</td>"
1074 /* denied: req, resp */
1075 "<td></td><td>%s</td>"
1076 /* errors : request, connect */
1077 "<td></td><td>%s</td>"
1078 /* errors : response */
1079 "<td><u>%s<div class=tips>Connection resets during transfers: %lld client, %lld server</div></u></td>"
1080 /* warnings: retries, redispatches */
1081 "<td>%lld</td><td>%lld</td>"
1082 "",
1083 U2H(stats[ST_F_BIN].u.u64), U2H(stats[ST_F_BOUT].u.u64),
1084 U2H(stats[ST_F_DRESP].u.u64),
1085 U2H(stats[ST_F_ECON].u.u64),
1086 U2H(stats[ST_F_ERESP].u.u64),
1087 (long long)stats[ST_F_CLI_ABRT].u.u64,
1088 (long long)stats[ST_F_SRV_ABRT].u.u64,
1089 (long long)stats[ST_F_WRETR].u.u64,
1090 (long long)stats[ST_F_WREDIS].u.u64);
1091
1092 /* status, last change */
1093 chunk_appendf(out, "<td class=ac>");
1094
1095 /* FIXME!!!!
1096 * LASTCHG should contain the last change for *this* server and must be computed
1097 * properly above, as was done below, ie: this server if maint, otherwise ref server
1098 * if tracking. Note that ref is either local or remote depending on tracking.
1099 */
1100
1101
1102 if (memcmp(field_str(stats, ST_F_STATUS), "MAINT", 5) == 0) {
1103 chunk_appendf(out, "%s MAINT", human_time(stats[ST_F_LASTCHG].u.u32, 1));
1104 }
1105 else if (memcmp(field_str(stats, ST_F_STATUS), "no check", 5) == 0) {
1106 chunk_strcat(out, "<i>no check</i>");
1107 }
1108 else {
1109 chunk_appendf(out, "%s %s", human_time(stats[ST_F_LASTCHG].u.u32, 1), field_str(stats, ST_F_STATUS));
1110 if (memcmp(field_str(stats, ST_F_STATUS), "DOWN", 4) == 0) {
1111 if (stats[ST_F_CHECK_HEALTH].u.u32)
1112 chunk_strcat(out, " &uarr;");
1113 }
1114 else if (stats[ST_F_CHECK_HEALTH].u.u32 < stats[ST_F_CHECK_RISE].u.u32 + stats[ST_F_CHECK_FALL].u.u32 - 1)
1115 chunk_strcat(out, " &darr;");
1116 }
1117
1118 if (memcmp(field_str(stats, ST_F_STATUS), "DOWN", 4) == 0 &&
1119 stats[ST_F_AGENT_STATUS].type && !stats[ST_F_AGENT_HEALTH].u.u32) {
1120 chunk_appendf(out,
1121 "</td><td class=ac><u> %s",
1122 field_str(stats, ST_F_AGENT_STATUS));
1123
1124 if (stats[ST_F_AGENT_CODE].type)
1125 chunk_appendf(out, "/%d", stats[ST_F_AGENT_CODE].u.u32);
1126
1127 if (stats[ST_F_AGENT_DURATION].type)
1128 chunk_appendf(out, " in %lums", (long)stats[ST_F_AGENT_DURATION].u.u64);
1129
1130 chunk_appendf(out, "<div class=tips>%s", field_str(stats, ST_F_AGENT_DESC));
1131
1132 if (*field_str(stats, ST_F_LAST_AGT)) {
1133 chunk_appendf(out, ": ");
1134 chunk_initstr(&src, field_str(stats, ST_F_LAST_AGT));
1135 chunk_htmlencode(out, &src);
1136 }
1137 chunk_appendf(out, "</div></u>");
1138 }
1139 else if (stats[ST_F_CHECK_STATUS].type) {
1140 chunk_appendf(out,
1141 "</td><td class=ac><u> %s",
1142 field_str(stats, ST_F_CHECK_STATUS));
1143
1144 if (stats[ST_F_CHECK_CODE].type)
1145 chunk_appendf(out, "/%d", stats[ST_F_CHECK_CODE].u.u32);
1146
1147 if (stats[ST_F_CHECK_DURATION].type)
1148 chunk_appendf(out, " in %lums", (long)stats[ST_F_CHECK_DURATION].u.u64);
1149
1150 chunk_appendf(out, "<div class=tips>%s", field_str(stats, ST_F_CHECK_DESC));
1151
1152 if (*field_str(stats, ST_F_LAST_CHK)) {
1153 chunk_appendf(out, ": ");
1154 chunk_initstr(&src, field_str(stats, ST_F_LAST_CHK));
1155 chunk_htmlencode(out, &src);
1156 }
1157 chunk_appendf(out, "</div></u>");
1158 }
1159 else
1160 chunk_appendf(out, "</td><td>");
1161
1162 chunk_appendf(out,
1163 /* weight */
1164 "</td><td class=ac>%d</td>"
1165 /* act, bck */
1166 "<td class=ac>%s</td><td class=ac>%s</td>"
1167 "",
1168 stats[ST_F_WEIGHT].u.u32,
1169 stats[ST_F_BCK].u.u32 ? "-" : "Y",
1170 stats[ST_F_BCK].u.u32 ? "Y" : "-");
1171
1172 /* check failures: unique, fatal, down time */
1173 if (strcmp(field_str(stats, ST_F_STATUS), "MAINT (resolution)") == 0) {
1174 chunk_appendf(out, "<td class=ac colspan=3>resolution</td>");
1175 }
1176 else if (stats[ST_F_CHKFAIL].type) {
1177 chunk_appendf(out, "<td><u>%lld", (long long)stats[ST_F_CHKFAIL].u.u64);
1178
1179 if (stats[ST_F_HANAFAIL].type)
1180 chunk_appendf(out, "/%lld", (long long)stats[ST_F_HANAFAIL].u.u64);
1181
1182 chunk_appendf(out,
1183 "<div class=tips>Failed Health Checks%s</div></u></td>"
1184 "<td>%lld</td><td>%s</td>"
1185 "",
1186 stats[ST_F_HANAFAIL].type ? "/Health Analyses" : "",
1187 (long long)stats[ST_F_CHKDOWN].u.u64, human_time(stats[ST_F_DOWNTIME].u.u32, 1));
1188 }
1189 else if (strcmp(field_str(stats, ST_F_STATUS), "MAINT") != 0 && field_format(stats, ST_F_TRACKED) == FF_STR) {
1190 /* tracking a server (hence inherited maint would appear as "MAINT (via...)" */
1191 chunk_appendf(out,
1192 "<td class=ac colspan=3><a class=lfsb href=\"#%s\">via %s</a></td>",
1193 field_str(stats, ST_F_TRACKED), field_str(stats, ST_F_TRACKED));
1194 }
1195 else
1196 chunk_appendf(out, "<td colspan=3></td>");
1197
1198 /* throttle */
1199 if (stats[ST_F_THROTTLE].type)
1200 chunk_appendf(out, "<td class=ac>%d %%</td></tr>\n", stats[ST_F_THROTTLE].u.u32);
1201 else
1202 chunk_appendf(out, "<td class=ac>-</td></tr>\n");
1203 }
1204 else if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_BE) {
1205 chunk_appendf(out, "<tr class=\"backend\">");
Willy Tarreauab02b3f2019-10-09 11:11:46 +02001206 if (flags & STAT_ADMIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001207 /* Column sub-heading for Enable or Disable server */
1208 chunk_appendf(out, "<td></td>");
1209 }
1210 chunk_appendf(out,
1211 "<td class=ac>"
1212 /* name */
1213 "%s<a name=\"%s/Backend\"></a>"
1214 "<a class=lfsb href=\"#%s/Backend\">Backend</a>"
1215 "",
Willy Tarreau708c4162019-10-09 10:19:16 +02001216 (flags & STAT_SHLGNDS)?"<u>":"",
William Lallemand74c24fb2016-11-21 17:18:36 +01001217 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_PXNAME));
1218
Willy Tarreau708c4162019-10-09 10:19:16 +02001219 if (flags & STAT_SHLGNDS) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001220 /* balancing */
1221 chunk_appendf(out, "<div class=tips>balancing: %s",
1222 field_str(stats, ST_F_ALGO));
1223
1224 /* cookie */
1225 if (stats[ST_F_COOKIE].type) {
1226 chunk_appendf(out, ", cookie: '");
1227 chunk_initstr(&src, field_str(stats, ST_F_COOKIE));
1228 chunk_htmlencode(out, &src);
1229 chunk_appendf(out, "'");
1230 }
1231 chunk_appendf(out, "</div>");
1232 }
1233
1234 chunk_appendf(out,
1235 "%s</td>"
1236 /* queue : current, max */
1237 "<td>%s</td><td>%s</td><td></td>"
1238 /* sessions rate : current, max, limit */
1239 "<td>%s</td><td>%s</td><td></td>"
1240 "",
Willy Tarreau708c4162019-10-09 10:19:16 +02001241 (flags & STAT_SHLGNDS)?"</u>":"",
William Lallemand74c24fb2016-11-21 17:18:36 +01001242 U2H(stats[ST_F_QCUR].u.u32), U2H(stats[ST_F_QMAX].u.u32),
1243 U2H(stats[ST_F_RATE].u.u32), U2H(stats[ST_F_RATE_MAX].u.u32));
1244
1245 chunk_appendf(out,
1246 /* sessions: current, max, limit, total */
1247 "<td>%s</td><td>%s</td><td>%s</td>"
1248 "<td><u>%s<div class=tips><table class=det>"
1249 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
1250 "",
Willy Tarreau8e0f1752016-12-12 15:07:29 +01001251 U2H(stats[ST_F_SCUR].u.u32), U2H(stats[ST_F_SMAX].u.u32), U2H(stats[ST_F_SLIM].u.u32),
William Lallemand74c24fb2016-11-21 17:18:36 +01001252 U2H(stats[ST_F_STOT].u.u64),
1253 U2H(stats[ST_F_STOT].u.u64));
1254
1255 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
1256 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0) {
1257 chunk_appendf(out,
Willy Tarreauf1573842018-12-14 11:35:36 +01001258 "<tr><th>New connections:</th><td>%s</td></tr>"
1259 "<tr><th>Reused connections:</th><td>%s</td><td>(%d%%)</td></tr>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001260 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
1261 "<tr><th>- HTTP 1xx responses:</th><td>%s</td></tr>"
1262 "<tr><th>- HTTP 2xx responses:</th><td>%s</td></tr>"
1263 "<tr><th>&nbsp;&nbsp;Compressed 2xx:</th><td>%s</td><td>(%d%%)</td></tr>"
1264 "<tr><th>- HTTP 3xx responses:</th><td>%s</td></tr>"
1265 "<tr><th>- HTTP 4xx responses:</th><td>%s</td></tr>"
1266 "<tr><th>- HTTP 5xx responses:</th><td>%s</td></tr>"
1267 "<tr><th>- other responses:</th><td>%s</td></tr>"
Willy Tarreaua1214a52018-12-14 14:00:25 +01001268 "<tr><th>Cache lookups:</th><td>%s</td></tr>"
1269 "<tr><th>Cache hits:</th><td>%s</td><td>(%d%%)</td></tr>"
Willy Tarreau1b0f85e2018-05-28 15:12:40 +02001270 "<tr><th>Failed hdr rewrites:</th><td>%s</td></tr>"
Christopher Faulet0159ee42019-12-16 14:40:39 +01001271 "<tr><th>Internal errors:</th><td>%s</td></tr>"
Christopher Faulet0d1c2a62019-11-08 14:59:51 +01001272 "",
Willy Tarreauf1573842018-12-14 11:35:36 +01001273 U2H(stats[ST_F_CONNECT].u.u64),
1274 U2H(stats[ST_F_REUSE].u.u64),
1275 (stats[ST_F_CONNECT].u.u64 + stats[ST_F_REUSE].u.u64) ?
1276 (int)(100 * stats[ST_F_REUSE].u.u64 / (stats[ST_F_CONNECT].u.u64 + stats[ST_F_REUSE].u.u64)) : 0,
William Lallemand74c24fb2016-11-21 17:18:36 +01001277 U2H(stats[ST_F_REQ_TOT].u.u64),
1278 U2H(stats[ST_F_HRSP_1XX].u.u64),
1279 U2H(stats[ST_F_HRSP_2XX].u.u64),
1280 U2H(stats[ST_F_COMP_RSP].u.u64),
1281 stats[ST_F_HRSP_2XX].u.u64 ?
1282 (int)(100 * stats[ST_F_COMP_RSP].u.u64 / stats[ST_F_HRSP_2XX].u.u64) : 0,
1283 U2H(stats[ST_F_HRSP_3XX].u.u64),
1284 U2H(stats[ST_F_HRSP_4XX].u.u64),
1285 U2H(stats[ST_F_HRSP_5XX].u.u64),
Willy Tarreaufeead3a2018-12-14 13:48:44 +01001286 U2H(stats[ST_F_HRSP_OTHER].u.u64),
Willy Tarreaua1214a52018-12-14 14:00:25 +01001287 U2H(stats[ST_F_CACHE_LOOKUPS].u.u64),
1288 U2H(stats[ST_F_CACHE_HITS].u.u64),
1289 stats[ST_F_CACHE_LOOKUPS].u.u64 ?
1290 (int)(100 * stats[ST_F_CACHE_HITS].u.u64 / stats[ST_F_CACHE_LOOKUPS].u.u64) : 0,
Christopher Faulet0159ee42019-12-16 14:40:39 +01001291 U2H(stats[ST_F_WREW].u.u64),
1292 U2H(stats[ST_F_EINT].u.u64));
William Lallemand74c24fb2016-11-21 17:18:36 +01001293 }
1294
Christopher Faulet0d1c2a62019-11-08 14:59:51 +01001295 chunk_appendf(out, "<tr><th colspan=3>Max / Avg over last 1024 success. conn.</th></tr>");
1296 chunk_appendf(out, "<tr><th>- Queue time:</th><td>%s / %s</td><td>ms</td></tr>",
1297 U2H(stats[ST_F_QT_MAX].u.u32), U2H(stats[ST_F_QTIME].u.u32));
1298 chunk_appendf(out, "<tr><th>- Connect time:</th><td>%s / %s</td><td>ms</td></tr>",
1299 U2H(stats[ST_F_CT_MAX].u.u32), U2H(stats[ST_F_CTIME].u.u32));
William Lallemand74c24fb2016-11-21 17:18:36 +01001300 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
Christopher Faulet0d1c2a62019-11-08 14:59:51 +01001301 chunk_appendf(out, "<tr><th>- Responses time:</th><td>%s / %s</td><td>ms</td></tr>",
1302 U2H(stats[ST_F_RT_MAX].u.u32), U2H(stats[ST_F_RTIME].u.u32));
1303 chunk_appendf(out, "<tr><th>- Total time:</th><td>%s / %s</td><td>ms</td></tr>",
1304 U2H(stats[ST_F_TT_MAX].u.u32), U2H(stats[ST_F_TTIME].u.u32));
William Lallemand74c24fb2016-11-21 17:18:36 +01001305
1306 chunk_appendf(out,
1307 "</table></div></u></td>"
1308 /* sessions: lbtot, last */
1309 "<td>%s</td><td>%s</td>"
1310 /* bytes: in */
1311 "<td>%s</td>"
1312 "",
1313 U2H(stats[ST_F_LBTOT].u.u64),
1314 human_time(stats[ST_F_LASTSESS].u.s32, 1),
1315 U2H(stats[ST_F_BIN].u.u64));
1316
1317 chunk_appendf(out,
1318 /* bytes:out + compression stats (via hover): comp_in, comp_out, comp_byp */
1319 "<td>%s%s<div class=tips><table class=det>"
1320 "<tr><th>Response bytes in:</th><td>%s</td></tr>"
1321 "<tr><th>Compression in:</th><td>%s</td></tr>"
1322 "<tr><th>Compression out:</th><td>%s</td><td>(%d%%)</td></tr>"
1323 "<tr><th>Compression bypass:</th><td>%s</td></tr>"
1324 "<tr><th>Total bytes saved:</th><td>%s</td><td>(%d%%)</td></tr>"
1325 "</table></div>%s</td>",
1326 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "<u>":"",
1327 U2H(stats[ST_F_BOUT].u.u64),
1328 U2H(stats[ST_F_BOUT].u.u64),
1329 U2H(stats[ST_F_COMP_IN].u.u64),
1330 U2H(stats[ST_F_COMP_OUT].u.u64),
1331 stats[ST_F_COMP_IN].u.u64 ? (int)(stats[ST_F_COMP_OUT].u.u64 * 100 / stats[ST_F_COMP_IN].u.u64) : 0,
1332 U2H(stats[ST_F_COMP_BYP].u.u64),
1333 U2H(stats[ST_F_COMP_IN].u.u64 - stats[ST_F_COMP_OUT].u.u64),
1334 stats[ST_F_BOUT].u.u64 ? (int)((stats[ST_F_COMP_IN].u.u64 - stats[ST_F_COMP_OUT].u.u64) * 100 / stats[ST_F_BOUT].u.u64) : 0,
1335 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "</u>":"");
1336
1337 chunk_appendf(out,
1338 /* denied: req, resp */
1339 "<td>%s</td><td>%s</td>"
1340 /* errors : request, connect */
1341 "<td></td><td>%s</td>"
1342 /* errors : response */
1343 "<td><u>%s<div class=tips>Connection resets during transfers: %lld client, %lld server</div></u></td>"
1344 /* warnings: retries, redispatches */
1345 "<td>%lld</td><td>%lld</td>"
1346 /* backend status: reflect backend status (up/down): we display UP
1347 * if the backend has known working servers or if it has no server at
1348 * all (eg: for stats). Then we display the total weight, number of
1349 * active and backups. */
1350 "<td class=ac>%s %s</td><td class=ac>&nbsp;</td><td class=ac>%d</td>"
1351 "<td class=ac>%d</td><td class=ac>%d</td>"
1352 "",
1353 U2H(stats[ST_F_DREQ].u.u64), U2H(stats[ST_F_DRESP].u.u64),
1354 U2H(stats[ST_F_ECON].u.u64),
1355 U2H(stats[ST_F_ERESP].u.u64),
1356 (long long)stats[ST_F_CLI_ABRT].u.u64,
1357 (long long)stats[ST_F_SRV_ABRT].u.u64,
1358 (long long)stats[ST_F_WRETR].u.u64, (long long)stats[ST_F_WREDIS].u.u64,
1359 human_time(stats[ST_F_LASTCHG].u.u32, 1),
1360 strcmp(field_str(stats, ST_F_STATUS), "DOWN") ? field_str(stats, ST_F_STATUS) : "<font color=\"red\"><b>DOWN</b></font>",
1361 stats[ST_F_WEIGHT].u.u32,
1362 stats[ST_F_ACT].u.u32, stats[ST_F_BCK].u.u32);
1363
1364 chunk_appendf(out,
1365 /* rest of backend: nothing, down transitions, total downtime, throttle */
1366 "<td class=ac>&nbsp;</td><td>%d</td>"
1367 "<td>%s</td>"
1368 "<td></td>"
1369 "</tr>",
1370 stats[ST_F_CHKDOWN].u.u32,
1371 stats[ST_F_DOWNTIME].type ? human_time(stats[ST_F_DOWNTIME].u.u32, 1) : "&nbsp;");
1372 }
1373 return 1;
1374}
1375
Willy Tarreau43241ff2019-10-09 11:27:51 +02001376static int stats_dump_one_line(const struct field *stats, struct proxy *px, struct appctx *appctx)
William Lallemand74c24fb2016-11-21 17:18:36 +01001377{
Simon Horman05ee2132017-01-04 09:37:25 +01001378 int ret;
1379
William Lallemand74c24fb2016-11-21 17:18:36 +01001380 if (appctx->ctx.stats.flags & STAT_FMT_HTML)
Willy Tarreau43241ff2019-10-09 11:27:51 +02001381 ret = stats_dump_fields_html(&trash, stats, appctx->ctx.stats.flags);
William Lallemand74c24fb2016-11-21 17:18:36 +01001382 else if (appctx->ctx.stats.flags & STAT_FMT_TYPED)
Willy Tarreau43241ff2019-10-09 11:27:51 +02001383 ret = stats_dump_fields_typed(&trash, stats, appctx->ctx.stats.flags);
Simon Horman05ee2132017-01-04 09:37:25 +01001384 else if (appctx->ctx.stats.flags & STAT_FMT_JSON)
Willy Tarreaub0ce3ad2019-10-09 11:19:29 +02001385 ret = stats_dump_fields_json(&trash, stats, appctx->ctx.stats.flags);
William Lallemand74c24fb2016-11-21 17:18:36 +01001386 else
Willy Tarreau43241ff2019-10-09 11:27:51 +02001387 ret = stats_dump_fields_csv(&trash, stats, appctx->ctx.stats.flags);
Simon Horman05ee2132017-01-04 09:37:25 +01001388
1389 if (ret)
1390 appctx->ctx.stats.flags |= STAT_STARTED;
1391
1392 return ret;
William Lallemand74c24fb2016-11-21 17:18:36 +01001393}
1394
1395/* Fill <stats> with the frontend statistics. <stats> is
1396 * preallocated array of length <len>. The length of the array
1397 * must be at least ST_F_TOTAL_FIELDS. If this length is less then
1398 * this value, the function returns 0, otherwise, it returns 1.
1399 */
1400int stats_fill_fe_stats(struct proxy *px, struct field *stats, int len)
1401{
1402 if (len < ST_F_TOTAL_FIELDS)
1403 return 0;
1404
1405 memset(stats, 0, sizeof(*stats) * len);
1406
1407 stats[ST_F_PXNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
1408 stats[ST_F_SVNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, "FRONTEND");
1409 stats[ST_F_MODE] = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
1410 stats[ST_F_SCUR] = mkf_u32(0, px->feconn);
1411 stats[ST_F_SMAX] = mkf_u32(FN_MAX, px->fe_counters.conn_max);
1412 stats[ST_F_SLIM] = mkf_u32(FO_CONFIG|FN_LIMIT, px->maxconn);
1413 stats[ST_F_STOT] = mkf_u64(FN_COUNTER, px->fe_counters.cum_sess);
1414 stats[ST_F_BIN] = mkf_u64(FN_COUNTER, px->fe_counters.bytes_in);
1415 stats[ST_F_BOUT] = mkf_u64(FN_COUNTER, px->fe_counters.bytes_out);
1416 stats[ST_F_DREQ] = mkf_u64(FN_COUNTER, px->fe_counters.denied_req);
1417 stats[ST_F_DRESP] = mkf_u64(FN_COUNTER, px->fe_counters.denied_resp);
1418 stats[ST_F_EREQ] = mkf_u64(FN_COUNTER, px->fe_counters.failed_req);
1419 stats[ST_F_DCON] = mkf_u64(FN_COUNTER, px->fe_counters.denied_conn);
1420 stats[ST_F_DSES] = mkf_u64(FN_COUNTER, px->fe_counters.denied_sess);
1421 stats[ST_F_STATUS] = mkf_str(FO_STATUS, px->state == PR_STREADY ? "OPEN" : px->state == PR_STFULL ? "FULL" : "STOP");
1422 stats[ST_F_PID] = mkf_u32(FO_KEY, relative_pid);
1423 stats[ST_F_IID] = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
1424 stats[ST_F_SID] = mkf_u32(FO_KEY|FS_SERVICE, 0);
1425 stats[ST_F_TYPE] = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_FE);
1426 stats[ST_F_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&px->fe_sess_per_sec));
1427 stats[ST_F_RATE_LIM] = mkf_u32(FO_CONFIG|FN_LIMIT, px->fe_sps_lim);
1428 stats[ST_F_RATE_MAX] = mkf_u32(FN_MAX, px->fe_counters.sps_max);
Tim Duesterhus3fd19732018-05-27 20:35:08 +02001429 stats[ST_F_WREW] = mkf_u64(FN_COUNTER, px->fe_counters.failed_rewrites);
Christopher Faulet0159ee42019-12-16 14:40:39 +01001430 stats[ST_F_EINT] = mkf_u64(FN_COUNTER, px->fe_counters.internal_errors);
William Lallemand74c24fb2016-11-21 17:18:36 +01001431
1432 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
1433 if (px->mode == PR_MODE_HTTP) {
1434 stats[ST_F_HRSP_1XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[1]);
1435 stats[ST_F_HRSP_2XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[2]);
1436 stats[ST_F_HRSP_3XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[3]);
1437 stats[ST_F_HRSP_4XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[4]);
1438 stats[ST_F_HRSP_5XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[5]);
1439 stats[ST_F_HRSP_OTHER] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[0]);
1440 stats[ST_F_INTERCEPTED] = mkf_u64(FN_COUNTER, px->fe_counters.intercepted_req);
Willy Tarreaua1214a52018-12-14 14:00:25 +01001441 stats[ST_F_CACHE_LOOKUPS] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.cache_lookups);
1442 stats[ST_F_CACHE_HITS] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.cache_hits);
William Lallemand74c24fb2016-11-21 17:18:36 +01001443 }
1444
1445 /* requests : req_rate, req_rate_max, req_tot, */
1446 stats[ST_F_REQ_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&px->fe_req_per_sec));
1447 stats[ST_F_REQ_RATE_MAX] = mkf_u32(FN_MAX, px->fe_counters.p.http.rps_max);
1448 stats[ST_F_REQ_TOT] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.cum_req);
1449
1450 /* compression: in, out, bypassed, responses */
1451 stats[ST_F_COMP_IN] = mkf_u64(FN_COUNTER, px->fe_counters.comp_in);
1452 stats[ST_F_COMP_OUT] = mkf_u64(FN_COUNTER, px->fe_counters.comp_out);
1453 stats[ST_F_COMP_BYP] = mkf_u64(FN_COUNTER, px->fe_counters.comp_byp);
1454 stats[ST_F_COMP_RSP] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.comp_rsp);
1455
1456 /* connections : conn_rate, conn_rate_max, conn_tot, conn_max */
1457 stats[ST_F_CONN_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&px->fe_conn_per_sec));
1458 stats[ST_F_CONN_RATE_MAX] = mkf_u32(FN_MAX, px->fe_counters.cps_max);
1459 stats[ST_F_CONN_TOT] = mkf_u64(FN_COUNTER, px->fe_counters.cum_conn);
1460
1461 return 1;
1462}
1463
1464/* Dumps a frontend's line to the trash for the current proxy <px> and uses
1465 * the state from stream interface <si>. The caller is responsible for clearing
1466 * the trash if needed. Returns non-zero if it emits anything, zero otherwise.
1467 */
1468static int stats_dump_fe_stats(struct stream_interface *si, struct proxy *px)
1469{
1470 struct appctx *appctx = __objt_appctx(si->end);
1471
1472 if (!(px->cap & PR_CAP_FE))
1473 return 0;
1474
1475 if ((appctx->ctx.stats.flags & STAT_BOUND) && !(appctx->ctx.stats.type & (1 << STATS_TYPE_FE)))
1476 return 0;
1477
1478 if (!stats_fill_fe_stats(px, stats, ST_F_TOTAL_FIELDS))
1479 return 0;
1480
Willy Tarreau43241ff2019-10-09 11:27:51 +02001481 return stats_dump_one_line(stats, px, appctx);
William Lallemand74c24fb2016-11-21 17:18:36 +01001482}
1483
1484/* Fill <stats> with the listener statistics. <stats> is
1485 * preallocated array of length <len>. The length of the array
1486 * must be at least ST_F_TOTAL_FIELDS. If this length is less
1487 * then this value, the function returns 0, otherwise, it
Willy Tarreau708c4162019-10-09 10:19:16 +02001488 * returns 1. <flags> can take the value STAT_SHLGNDS.
William Lallemand74c24fb2016-11-21 17:18:36 +01001489 */
1490int stats_fill_li_stats(struct proxy *px, struct listener *l, int flags,
1491 struct field *stats, int len)
1492{
Willy Tarreau83061a82018-07-13 11:56:34 +02001493 struct buffer *out = get_trash_chunk();
William Lallemand74c24fb2016-11-21 17:18:36 +01001494
1495 if (len < ST_F_TOTAL_FIELDS)
1496 return 0;
1497
1498 if (!l->counters)
1499 return 0;
1500
1501 chunk_reset(out);
1502 memset(stats, 0, sizeof(*stats) * len);
1503
1504 stats[ST_F_PXNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
1505 stats[ST_F_SVNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, l->name);
1506 stats[ST_F_MODE] = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
1507 stats[ST_F_SCUR] = mkf_u32(0, l->nbconn);
1508 stats[ST_F_SMAX] = mkf_u32(FN_MAX, l->counters->conn_max);
1509 stats[ST_F_SLIM] = mkf_u32(FO_CONFIG|FN_LIMIT, l->maxconn);
1510 stats[ST_F_STOT] = mkf_u64(FN_COUNTER, l->counters->cum_conn);
1511 stats[ST_F_BIN] = mkf_u64(FN_COUNTER, l->counters->bytes_in);
1512 stats[ST_F_BOUT] = mkf_u64(FN_COUNTER, l->counters->bytes_out);
1513 stats[ST_F_DREQ] = mkf_u64(FN_COUNTER, l->counters->denied_req);
1514 stats[ST_F_DRESP] = mkf_u64(FN_COUNTER, l->counters->denied_resp);
1515 stats[ST_F_EREQ] = mkf_u64(FN_COUNTER, l->counters->failed_req);
1516 stats[ST_F_DCON] = mkf_u64(FN_COUNTER, l->counters->denied_conn);
1517 stats[ST_F_DSES] = mkf_u64(FN_COUNTER, l->counters->denied_sess);
Willy Tarreaua8cf66b2019-02-27 16:49:00 +01001518 stats[ST_F_STATUS] = mkf_str(FO_STATUS, (!l->maxconn || l->nbconn < l->maxconn) ? (l->state == LI_LIMITED) ? "WAITING" : "OPEN" : "FULL");
William Lallemand74c24fb2016-11-21 17:18:36 +01001519 stats[ST_F_PID] = mkf_u32(FO_KEY, relative_pid);
1520 stats[ST_F_IID] = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
1521 stats[ST_F_SID] = mkf_u32(FO_KEY|FS_SERVICE, l->luid);
1522 stats[ST_F_TYPE] = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_SO);
Tim Duesterhus3fd19732018-05-27 20:35:08 +02001523 stats[ST_F_WREW] = mkf_u64(FN_COUNTER, l->counters->failed_rewrites);
Christopher Faulet0159ee42019-12-16 14:40:39 +01001524 stats[ST_F_EINT] = mkf_u64(FN_COUNTER, l->counters->internal_errors);
William Lallemand74c24fb2016-11-21 17:18:36 +01001525
Willy Tarreau708c4162019-10-09 10:19:16 +02001526 if (flags & STAT_SHLGNDS) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001527 char str[INET6_ADDRSTRLEN];
1528 int port;
1529
1530 port = get_host_port(&l->addr);
1531 switch (addr_to_str(&l->addr, str, sizeof(str))) {
1532 case AF_INET:
1533 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
1534 chunk_appendf(out, "%s:%d", str, port);
1535 break;
1536 case AF_INET6:
1537 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
1538 chunk_appendf(out, "[%s]:%d", str, port);
1539 break;
1540 case AF_UNIX:
1541 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, "unix");
1542 break;
1543 case -1:
1544 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
1545 chunk_strcat(out, strerror(errno));
1546 break;
1547 default: /* address family not supported */
1548 break;
1549 }
1550 }
1551
1552 return 1;
1553}
1554
1555/* Dumps a line for listener <l> and proxy <px> to the trash and uses the state
Willy Tarreau43241ff2019-10-09 11:27:51 +02001556 * from stream interface <si>. The caller is responsible for clearing the trash
1557 * if needed. Returns non-zero if it emits anything, zero otherwise.
William Lallemand74c24fb2016-11-21 17:18:36 +01001558 */
Willy Tarreau43241ff2019-10-09 11:27:51 +02001559static int stats_dump_li_stats(struct stream_interface *si, struct proxy *px, struct listener *l)
William Lallemand74c24fb2016-11-21 17:18:36 +01001560{
1561 struct appctx *appctx = __objt_appctx(si->end);
1562
Willy Tarreau43241ff2019-10-09 11:27:51 +02001563 if (!stats_fill_li_stats(px, l, appctx->ctx.stats.flags, stats, ST_F_TOTAL_FIELDS))
William Lallemand74c24fb2016-11-21 17:18:36 +01001564 return 0;
1565
Willy Tarreau43241ff2019-10-09 11:27:51 +02001566 return stats_dump_one_line(stats, px, appctx);
William Lallemand74c24fb2016-11-21 17:18:36 +01001567}
1568
1569enum srv_stats_state {
1570 SRV_STATS_STATE_DOWN = 0,
1571 SRV_STATS_STATE_DOWN_AGENT,
1572 SRV_STATS_STATE_GOING_UP,
1573 SRV_STATS_STATE_UP_GOING_DOWN,
1574 SRV_STATS_STATE_UP,
1575 SRV_STATS_STATE_NOLB_GOING_DOWN,
1576 SRV_STATS_STATE_NOLB,
1577 SRV_STATS_STATE_DRAIN_GOING_DOWN,
1578 SRV_STATS_STATE_DRAIN,
1579 SRV_STATS_STATE_DRAIN_AGENT,
1580 SRV_STATS_STATE_NO_CHECK,
1581
1582 SRV_STATS_STATE_COUNT, /* Must be last */
1583};
1584
1585static const char *srv_hlt_st[SRV_STATS_STATE_COUNT] = {
1586 [SRV_STATS_STATE_DOWN] = "DOWN",
1587 [SRV_STATS_STATE_DOWN_AGENT] = "DOWN (agent)",
1588 [SRV_STATS_STATE_GOING_UP] = "DOWN %d/%d",
1589 [SRV_STATS_STATE_UP_GOING_DOWN] = "UP %d/%d",
1590 [SRV_STATS_STATE_UP] = "UP",
1591 [SRV_STATS_STATE_NOLB_GOING_DOWN] = "NOLB %d/%d",
1592 [SRV_STATS_STATE_NOLB] = "NOLB",
1593 [SRV_STATS_STATE_DRAIN_GOING_DOWN] = "DRAIN %d/%d",
1594 [SRV_STATS_STATE_DRAIN] = "DRAIN",
1595 [SRV_STATS_STATE_DRAIN_AGENT] = "DRAIN (agent)",
1596 [SRV_STATS_STATE_NO_CHECK] = "no check"
1597};
1598
1599/* Fill <stats> with the server statistics. <stats> is
1600 * preallocated array of length <len>. The length of the array
1601 * must be at least ST_F_TOTAL_FIELDS. If this length is less
1602 * then this value, the function returns 0, otherwise, it
Willy Tarreau708c4162019-10-09 10:19:16 +02001603 * returns 1. <flags> can take the value STAT_SHLGNDS.
William Lallemand74c24fb2016-11-21 17:18:36 +01001604 */
1605int stats_fill_sv_stats(struct proxy *px, struct server *sv, int flags,
1606 struct field *stats, int len)
1607{
1608 struct server *via, *ref;
1609 char str[INET6_ADDRSTRLEN];
Willy Tarreau83061a82018-07-13 11:56:34 +02001610 struct buffer *out = get_trash_chunk();
William Lallemand74c24fb2016-11-21 17:18:36 +01001611 enum srv_stats_state state;
1612 char *fld_status;
1613
1614 if (len < ST_F_TOTAL_FIELDS)
1615 return 0;
1616
1617 memset(stats, 0, sizeof(*stats) * len);
1618
1619 /* we have "via" which is the tracked server as described in the configuration,
1620 * and "ref" which is the checked server and the end of the chain.
1621 */
1622 via = sv->track ? sv->track : sv;
1623 ref = via;
1624 while (ref->track)
1625 ref = ref->track;
1626
Emeric Brun52a91d32017-08-31 14:41:55 +02001627 if (sv->cur_state == SRV_ST_RUNNING || sv->cur_state == SRV_ST_STARTING) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001628 if ((ref->check.state & CHK_ST_ENABLED) &&
1629 (ref->check.health < ref->check.rise + ref->check.fall - 1)) {
1630 state = SRV_STATS_STATE_UP_GOING_DOWN;
1631 } else {
1632 state = SRV_STATS_STATE_UP;
1633 }
1634
Emeric Brun52a91d32017-08-31 14:41:55 +02001635 if (sv->cur_admin & SRV_ADMF_DRAIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001636 if (ref->agent.state & CHK_ST_ENABLED)
1637 state = SRV_STATS_STATE_DRAIN_AGENT;
1638 else if (state == SRV_STATS_STATE_UP_GOING_DOWN)
1639 state = SRV_STATS_STATE_DRAIN_GOING_DOWN;
1640 else
1641 state = SRV_STATS_STATE_DRAIN;
1642 }
1643
1644 if (state == SRV_STATS_STATE_UP && !(ref->check.state & CHK_ST_ENABLED)) {
1645 state = SRV_STATS_STATE_NO_CHECK;
1646 }
1647 }
Emeric Brun52a91d32017-08-31 14:41:55 +02001648 else if (sv->cur_state == SRV_ST_STOPPING) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001649 if ((!(sv->check.state & CHK_ST_ENABLED) && !sv->track) ||
1650 (ref->check.health == ref->check.rise + ref->check.fall - 1)) {
1651 state = SRV_STATS_STATE_NOLB;
1652 } else {
1653 state = SRV_STATS_STATE_NOLB_GOING_DOWN;
1654 }
1655 }
1656 else { /* stopped */
1657 if ((ref->agent.state & CHK_ST_ENABLED) && !ref->agent.health) {
1658 state = SRV_STATS_STATE_DOWN_AGENT;
1659 } else if ((ref->check.state & CHK_ST_ENABLED) && !ref->check.health) {
1660 state = SRV_STATS_STATE_DOWN; /* DOWN */
1661 } else if ((ref->agent.state & CHK_ST_ENABLED) || (ref->check.state & CHK_ST_ENABLED)) {
1662 state = SRV_STATS_STATE_GOING_UP;
1663 } else {
1664 state = SRV_STATS_STATE_DOWN; /* DOWN, unchecked */
1665 }
1666 }
1667
1668 chunk_reset(out);
1669
1670 stats[ST_F_PXNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
1671 stats[ST_F_SVNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, sv->id);
1672 stats[ST_F_MODE] = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
1673 stats[ST_F_QCUR] = mkf_u32(0, sv->nbpend);
1674 stats[ST_F_QMAX] = mkf_u32(FN_MAX, sv->counters.nbpend_max);
1675 stats[ST_F_SCUR] = mkf_u32(0, sv->cur_sess);
1676 stats[ST_F_SMAX] = mkf_u32(FN_MAX, sv->counters.cur_sess_max);
1677
1678 if (sv->maxconn)
1679 stats[ST_F_SLIM] = mkf_u32(FO_CONFIG|FN_LIMIT, sv->maxconn);
1680
Willy Tarreauf21d17b2019-09-08 09:24:56 +02001681 stats[ST_F_SRV_ICUR] = mkf_u32(0, sv->curr_idle_conns);
1682 if (sv->max_idle_conns != -1)
1683 stats[ST_F_SRV_ILIM] = mkf_u32(FO_CONFIG|FN_LIMIT, sv->max_idle_conns);
1684
William Lallemand74c24fb2016-11-21 17:18:36 +01001685 stats[ST_F_STOT] = mkf_u64(FN_COUNTER, sv->counters.cum_sess);
1686 stats[ST_F_BIN] = mkf_u64(FN_COUNTER, sv->counters.bytes_in);
1687 stats[ST_F_BOUT] = mkf_u64(FN_COUNTER, sv->counters.bytes_out);
Christopher Fauleta08546b2019-12-16 16:07:34 +01001688 stats[ST_F_DRESP] = mkf_u64(FN_COUNTER, sv->counters.denied_resp);
William Lallemand74c24fb2016-11-21 17:18:36 +01001689 stats[ST_F_ECON] = mkf_u64(FN_COUNTER, sv->counters.failed_conns);
1690 stats[ST_F_ERESP] = mkf_u64(FN_COUNTER, sv->counters.failed_resp);
1691 stats[ST_F_WRETR] = mkf_u64(FN_COUNTER, sv->counters.retries);
1692 stats[ST_F_WREDIS] = mkf_u64(FN_COUNTER, sv->counters.redispatches);
Tim Duesterhus3fd19732018-05-27 20:35:08 +02001693 stats[ST_F_WREW] = mkf_u64(FN_COUNTER, sv->counters.failed_rewrites);
Christopher Faulet0159ee42019-12-16 14:40:39 +01001694 stats[ST_F_EINT] = mkf_u64(FN_COUNTER, sv->counters.internal_errors);
Willy Tarreauf1573842018-12-14 11:35:36 +01001695 stats[ST_F_CONNECT] = mkf_u64(FN_COUNTER, sv->counters.connect);
1696 stats[ST_F_REUSE] = mkf_u64(FN_COUNTER, sv->counters.reuse);
William Lallemand74c24fb2016-11-21 17:18:36 +01001697
1698 /* status */
1699 fld_status = chunk_newstr(out);
Emeric Brun52a91d32017-08-31 14:41:55 +02001700 if (sv->cur_admin & SRV_ADMF_RMAINT)
William Lallemand74c24fb2016-11-21 17:18:36 +01001701 chunk_appendf(out, "MAINT (resolution)");
Emeric Brun52a91d32017-08-31 14:41:55 +02001702 else if (sv->cur_admin & SRV_ADMF_IMAINT)
William Lallemand74c24fb2016-11-21 17:18:36 +01001703 chunk_appendf(out, "MAINT (via %s/%s)", via->proxy->id, via->id);
Emeric Brun52a91d32017-08-31 14:41:55 +02001704 else if (sv->cur_admin & SRV_ADMF_MAINT)
William Lallemand74c24fb2016-11-21 17:18:36 +01001705 chunk_appendf(out, "MAINT");
1706 else
1707 chunk_appendf(out,
1708 srv_hlt_st[state],
Emeric Brun52a91d32017-08-31 14:41:55 +02001709 (ref->cur_state != SRV_ST_STOPPED) ? (ref->check.health - ref->check.rise + 1) : (ref->check.health),
1710 (ref->cur_state != SRV_ST_STOPPED) ? (ref->check.fall) : (ref->check.rise));
William Lallemand74c24fb2016-11-21 17:18:36 +01001711
1712 stats[ST_F_STATUS] = mkf_str(FO_STATUS, fld_status);
1713 stats[ST_F_LASTCHG] = mkf_u32(FN_AGE, now.tv_sec - sv->last_change);
Emeric Brun52a91d32017-08-31 14:41:55 +02001714 stats[ST_F_WEIGHT] = mkf_u32(FN_AVG, (sv->cur_eweight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv);
William Lallemand74c24fb2016-11-21 17:18:36 +01001715 stats[ST_F_ACT] = mkf_u32(FO_STATUS, (sv->flags & SRV_F_BACKUP) ? 0 : 1);
1716 stats[ST_F_BCK] = mkf_u32(FO_STATUS, (sv->flags & SRV_F_BACKUP) ? 1 : 0);
1717
1718 /* check failures: unique, fatal; last change, total downtime */
1719 if (sv->check.state & CHK_ST_ENABLED) {
1720 stats[ST_F_CHKFAIL] = mkf_u64(FN_COUNTER, sv->counters.failed_checks);
1721 stats[ST_F_CHKDOWN] = mkf_u64(FN_COUNTER, sv->counters.down_trans);
1722 stats[ST_F_DOWNTIME] = mkf_u32(FN_COUNTER, srv_downtime(sv));
1723 }
1724
1725 if (sv->maxqueue)
1726 stats[ST_F_QLIMIT] = mkf_u32(FO_CONFIG|FS_SERVICE, sv->maxqueue);
1727
1728 stats[ST_F_PID] = mkf_u32(FO_KEY, relative_pid);
1729 stats[ST_F_IID] = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
1730 stats[ST_F_SID] = mkf_u32(FO_KEY|FS_SERVICE, sv->puid);
1731
Emeric Brun52a91d32017-08-31 14:41:55 +02001732 if (sv->cur_state == SRV_ST_STARTING && !server_is_draining(sv))
William Lallemand74c24fb2016-11-21 17:18:36 +01001733 stats[ST_F_THROTTLE] = mkf_u32(FN_AVG, server_throttle_rate(sv));
1734
1735 stats[ST_F_LBTOT] = mkf_u64(FN_COUNTER, sv->counters.cum_lbconn);
1736
1737 if (sv->track) {
1738 char *fld_track = chunk_newstr(out);
1739
1740 chunk_appendf(out, "%s/%s", sv->track->proxy->id, sv->track->id);
1741 stats[ST_F_TRACKED] = mkf_str(FO_CONFIG|FN_NAME|FS_SERVICE, fld_track);
1742 }
1743
1744 stats[ST_F_TYPE] = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_SV);
1745 stats[ST_F_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&sv->sess_per_sec));
1746 stats[ST_F_RATE_MAX] = mkf_u32(FN_MAX, sv->counters.sps_max);
1747
1748 if ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED) {
1749 const char *fld_chksts;
1750
1751 fld_chksts = chunk_newstr(out);
1752 chunk_strcat(out, "* "); // for check in progress
1753 chunk_strcat(out, get_check_status_info(sv->check.status));
1754 if (!(sv->check.state & CHK_ST_INPROGRESS))
1755 fld_chksts += 2; // skip "* "
1756 stats[ST_F_CHECK_STATUS] = mkf_str(FN_OUTPUT, fld_chksts);
1757
1758 if (sv->check.status >= HCHK_STATUS_L57DATA)
1759 stats[ST_F_CHECK_CODE] = mkf_u32(FN_OUTPUT, sv->check.code);
1760
1761 if (sv->check.status >= HCHK_STATUS_CHECKED)
1762 stats[ST_F_CHECK_DURATION] = mkf_u64(FN_DURATION, sv->check.duration);
1763
1764 stats[ST_F_CHECK_DESC] = mkf_str(FN_OUTPUT, get_check_status_description(sv->check.status));
1765 stats[ST_F_LAST_CHK] = mkf_str(FN_OUTPUT, sv->check.desc);
1766 stats[ST_F_CHECK_RISE] = mkf_u32(FO_CONFIG|FS_SERVICE, ref->check.rise);
1767 stats[ST_F_CHECK_FALL] = mkf_u32(FO_CONFIG|FS_SERVICE, ref->check.fall);
1768 stats[ST_F_CHECK_HEALTH] = mkf_u32(FO_CONFIG|FS_SERVICE, ref->check.health);
1769 }
1770
1771 if ((sv->agent.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED) {
1772 const char *fld_chksts;
1773
1774 fld_chksts = chunk_newstr(out);
1775 chunk_strcat(out, "* "); // for check in progress
1776 chunk_strcat(out, get_check_status_info(sv->agent.status));
1777 if (!(sv->agent.state & CHK_ST_INPROGRESS))
1778 fld_chksts += 2; // skip "* "
1779 stats[ST_F_AGENT_STATUS] = mkf_str(FN_OUTPUT, fld_chksts);
1780
1781 if (sv->agent.status >= HCHK_STATUS_L57DATA)
1782 stats[ST_F_AGENT_CODE] = mkf_u32(FN_OUTPUT, sv->agent.code);
1783
1784 if (sv->agent.status >= HCHK_STATUS_CHECKED)
1785 stats[ST_F_AGENT_DURATION] = mkf_u64(FN_DURATION, sv->agent.duration);
1786
1787 stats[ST_F_AGENT_DESC] = mkf_str(FN_OUTPUT, get_check_status_description(sv->agent.status));
1788 stats[ST_F_LAST_AGT] = mkf_str(FN_OUTPUT, sv->agent.desc);
1789 stats[ST_F_AGENT_RISE] = mkf_u32(FO_CONFIG|FS_SERVICE, sv->agent.rise);
1790 stats[ST_F_AGENT_FALL] = mkf_u32(FO_CONFIG|FS_SERVICE, sv->agent.fall);
1791 stats[ST_F_AGENT_HEALTH] = mkf_u32(FO_CONFIG|FS_SERVICE, sv->agent.health);
1792 }
1793
1794 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
1795 if (px->mode == PR_MODE_HTTP) {
1796 stats[ST_F_HRSP_1XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[1]);
1797 stats[ST_F_HRSP_2XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[2]);
1798 stats[ST_F_HRSP_3XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[3]);
1799 stats[ST_F_HRSP_4XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[4]);
1800 stats[ST_F_HRSP_5XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[5]);
1801 stats[ST_F_HRSP_OTHER] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[0]);
1802 }
1803
1804 if (ref->observe)
1805 stats[ST_F_HANAFAIL] = mkf_u64(FN_COUNTER, sv->counters.failed_hana);
1806
1807 stats[ST_F_CLI_ABRT] = mkf_u64(FN_COUNTER, sv->counters.cli_aborts);
1808 stats[ST_F_SRV_ABRT] = mkf_u64(FN_COUNTER, sv->counters.srv_aborts);
1809 stats[ST_F_LASTSESS] = mkf_s32(FN_AGE, srv_lastsession(sv));
1810
1811 stats[ST_F_QTIME] = mkf_u32(FN_AVG, swrate_avg(sv->counters.q_time, TIME_STATS_SAMPLES));
1812 stats[ST_F_CTIME] = mkf_u32(FN_AVG, swrate_avg(sv->counters.c_time, TIME_STATS_SAMPLES));
1813 stats[ST_F_RTIME] = mkf_u32(FN_AVG, swrate_avg(sv->counters.d_time, TIME_STATS_SAMPLES));
1814 stats[ST_F_TTIME] = mkf_u32(FN_AVG, swrate_avg(sv->counters.t_time, TIME_STATS_SAMPLES));
1815
Christopher Faulet0d1c2a62019-11-08 14:59:51 +01001816 stats[ST_F_QT_MAX] = mkf_u32(FN_MAX, sv->counters.qtime_max);
1817 stats[ST_F_CT_MAX] = mkf_u32(FN_MAX, sv->counters.ctime_max);
1818 stats[ST_F_RT_MAX] = mkf_u32(FN_MAX, sv->counters.dtime_max);
1819 stats[ST_F_TT_MAX] = mkf_u32(FN_MAX, sv->counters.ttime_max);
1820
Willy Tarreau708c4162019-10-09 10:19:16 +02001821 if (flags & STAT_SHLGNDS) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001822 switch (addr_to_str(&sv->addr, str, sizeof(str))) {
1823 case AF_INET:
1824 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
Willy Tarreau04276f32017-01-06 17:41:29 +01001825 chunk_appendf(out, "%s:%d", str, sv->svc_port);
William Lallemand74c24fb2016-11-21 17:18:36 +01001826 break;
1827 case AF_INET6:
1828 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
Willy Tarreau04276f32017-01-06 17:41:29 +01001829 chunk_appendf(out, "[%s]:%d", str, sv->svc_port);
William Lallemand74c24fb2016-11-21 17:18:36 +01001830 break;
1831 case AF_UNIX:
1832 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, "unix");
1833 break;
1834 case -1:
1835 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
1836 chunk_strcat(out, strerror(errno));
1837 break;
1838 default: /* address family not supported */
1839 break;
1840 }
1841
1842 if (sv->cookie)
1843 stats[ST_F_COOKIE] = mkf_str(FO_CONFIG|FN_NAME|FS_SERVICE, sv->cookie);
1844 }
1845
1846 return 1;
1847}
1848
1849/* Dumps a line for server <sv> and proxy <px> to the trash and uses the state
Willy Tarreau43241ff2019-10-09 11:27:51 +02001850 * from stream interface <si>, and server state <state>. The caller is
1851 * responsible for clearing the trash if needed. Returns non-zero if it emits
1852 * anything, zero otherwise.
William Lallemand74c24fb2016-11-21 17:18:36 +01001853 */
Willy Tarreau43241ff2019-10-09 11:27:51 +02001854static int stats_dump_sv_stats(struct stream_interface *si, struct proxy *px, struct server *sv)
William Lallemand74c24fb2016-11-21 17:18:36 +01001855{
1856 struct appctx *appctx = __objt_appctx(si->end);
1857
Willy Tarreau43241ff2019-10-09 11:27:51 +02001858 if (!stats_fill_sv_stats(px, sv, appctx->ctx.stats.flags, stats, ST_F_TOTAL_FIELDS))
William Lallemand74c24fb2016-11-21 17:18:36 +01001859 return 0;
1860
Willy Tarreau43241ff2019-10-09 11:27:51 +02001861 return stats_dump_one_line(stats, px, appctx);
William Lallemand74c24fb2016-11-21 17:18:36 +01001862}
1863
1864/* Fill <stats> with the backend statistics. <stats> is
1865 * preallocated array of length <len>. The length of the array
1866 * must be at least ST_F_TOTAL_FIELDS. If this length is less
1867 * then this value, the function returns 0, otherwise, it
Willy Tarreau708c4162019-10-09 10:19:16 +02001868 * returns 1. <flags> can take the value STAT_SHLGNDS.
William Lallemand74c24fb2016-11-21 17:18:36 +01001869 */
1870int stats_fill_be_stats(struct proxy *px, int flags, struct field *stats, int len)
1871{
1872 if (len < ST_F_TOTAL_FIELDS)
1873 return 0;
1874
1875 memset(stats, 0, sizeof(*stats) * len);
1876
1877 stats[ST_F_PXNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
1878 stats[ST_F_SVNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, "BACKEND");
1879 stats[ST_F_MODE] = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
1880 stats[ST_F_QCUR] = mkf_u32(0, px->nbpend);
1881 stats[ST_F_QMAX] = mkf_u32(FN_MAX, px->be_counters.nbpend_max);
Thierry FOURNIER0ff98a42016-12-19 16:50:42 +01001882 stats[ST_F_SCUR] = mkf_u32(0, px->beconn);
William Lallemand74c24fb2016-11-21 17:18:36 +01001883 stats[ST_F_SMAX] = mkf_u32(FN_MAX, px->be_counters.conn_max);
1884 stats[ST_F_SLIM] = mkf_u32(FO_CONFIG|FN_LIMIT, px->fullconn);
1885 stats[ST_F_STOT] = mkf_u64(FN_COUNTER, px->be_counters.cum_conn);
1886 stats[ST_F_BIN] = mkf_u64(FN_COUNTER, px->be_counters.bytes_in);
1887 stats[ST_F_BOUT] = mkf_u64(FN_COUNTER, px->be_counters.bytes_out);
1888 stats[ST_F_DREQ] = mkf_u64(FN_COUNTER, px->be_counters.denied_req);
1889 stats[ST_F_DRESP] = mkf_u64(FN_COUNTER, px->be_counters.denied_resp);
1890 stats[ST_F_ECON] = mkf_u64(FN_COUNTER, px->be_counters.failed_conns);
1891 stats[ST_F_ERESP] = mkf_u64(FN_COUNTER, px->be_counters.failed_resp);
1892 stats[ST_F_WRETR] = mkf_u64(FN_COUNTER, px->be_counters.retries);
1893 stats[ST_F_WREDIS] = mkf_u64(FN_COUNTER, px->be_counters.redispatches);
Tim Duesterhus3fd19732018-05-27 20:35:08 +02001894 stats[ST_F_WREW] = mkf_u64(FN_COUNTER, px->be_counters.failed_rewrites);
Christopher Faulet0159ee42019-12-16 14:40:39 +01001895 stats[ST_F_EINT] = mkf_u64(FN_COUNTER, px->be_counters.internal_errors);
Willy Tarreauf1573842018-12-14 11:35:36 +01001896 stats[ST_F_CONNECT] = mkf_u64(FN_COUNTER, px->be_counters.connect);
1897 stats[ST_F_REUSE] = mkf_u64(FN_COUNTER, px->be_counters.reuse);
William Lallemand74c24fb2016-11-21 17:18:36 +01001898 stats[ST_F_STATUS] = mkf_str(FO_STATUS, (px->lbprm.tot_weight > 0 || !px->srv) ? "UP" : "DOWN");
1899 stats[ST_F_WEIGHT] = mkf_u32(FN_AVG, (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv);
1900 stats[ST_F_ACT] = mkf_u32(0, px->srv_act);
1901 stats[ST_F_BCK] = mkf_u32(0, px->srv_bck);
1902 stats[ST_F_CHKDOWN] = mkf_u64(FN_COUNTER, px->down_trans);
1903 stats[ST_F_LASTCHG] = mkf_u32(FN_AGE, now.tv_sec - px->last_change);
1904 if (px->srv)
1905 stats[ST_F_DOWNTIME] = mkf_u32(FN_COUNTER, be_downtime(px));
1906
1907 stats[ST_F_PID] = mkf_u32(FO_KEY, relative_pid);
1908 stats[ST_F_IID] = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
1909 stats[ST_F_SID] = mkf_u32(FO_KEY|FS_SERVICE, 0);
1910 stats[ST_F_LBTOT] = mkf_u64(FN_COUNTER, px->be_counters.cum_lbconn);
1911 stats[ST_F_TYPE] = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_BE);
1912 stats[ST_F_RATE] = mkf_u32(0, read_freq_ctr(&px->be_sess_per_sec));
1913 stats[ST_F_RATE_MAX] = mkf_u32(0, px->be_counters.sps_max);
1914
Willy Tarreau708c4162019-10-09 10:19:16 +02001915 if (flags & STAT_SHLGNDS) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001916 if (px->cookie_name)
1917 stats[ST_F_COOKIE] = mkf_str(FO_CONFIG|FN_NAME|FS_SERVICE, px->cookie_name);
1918 stats[ST_F_ALGO] = mkf_str(FO_CONFIG|FS_SERVICE, backend_lb_algo_str(px->lbprm.algo & BE_LB_ALGO));
1919 }
1920
1921 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
1922 if (px->mode == PR_MODE_HTTP) {
1923 stats[ST_F_REQ_TOT] = mkf_u64(FN_COUNTER, px->be_counters.p.http.cum_req);
1924 stats[ST_F_HRSP_1XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[1]);
1925 stats[ST_F_HRSP_2XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[2]);
1926 stats[ST_F_HRSP_3XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[3]);
1927 stats[ST_F_HRSP_4XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[4]);
1928 stats[ST_F_HRSP_5XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[5]);
1929 stats[ST_F_HRSP_OTHER] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[0]);
Willy Tarreaua1214a52018-12-14 14:00:25 +01001930 stats[ST_F_CACHE_LOOKUPS] = mkf_u64(FN_COUNTER, px->be_counters.p.http.cache_lookups);
1931 stats[ST_F_CACHE_HITS] = mkf_u64(FN_COUNTER, px->be_counters.p.http.cache_hits);
William Lallemand74c24fb2016-11-21 17:18:36 +01001932 }
1933
1934 stats[ST_F_CLI_ABRT] = mkf_u64(FN_COUNTER, px->be_counters.cli_aborts);
1935 stats[ST_F_SRV_ABRT] = mkf_u64(FN_COUNTER, px->be_counters.srv_aborts);
1936
1937 /* compression: in, out, bypassed, responses */
1938 stats[ST_F_COMP_IN] = mkf_u64(FN_COUNTER, px->be_counters.comp_in);
1939 stats[ST_F_COMP_OUT] = mkf_u64(FN_COUNTER, px->be_counters.comp_out);
1940 stats[ST_F_COMP_BYP] = mkf_u64(FN_COUNTER, px->be_counters.comp_byp);
1941 stats[ST_F_COMP_RSP] = mkf_u64(FN_COUNTER, px->be_counters.p.http.comp_rsp);
1942 stats[ST_F_LASTSESS] = mkf_s32(FN_AGE, be_lastsession(px));
1943
1944 stats[ST_F_QTIME] = mkf_u32(FN_AVG, swrate_avg(px->be_counters.q_time, TIME_STATS_SAMPLES));
1945 stats[ST_F_CTIME] = mkf_u32(FN_AVG, swrate_avg(px->be_counters.c_time, TIME_STATS_SAMPLES));
1946 stats[ST_F_RTIME] = mkf_u32(FN_AVG, swrate_avg(px->be_counters.d_time, TIME_STATS_SAMPLES));
1947 stats[ST_F_TTIME] = mkf_u32(FN_AVG, swrate_avg(px->be_counters.t_time, TIME_STATS_SAMPLES));
1948
Christopher Faulet0d1c2a62019-11-08 14:59:51 +01001949 stats[ST_F_QT_MAX] = mkf_u32(FN_MAX, px->be_counters.qtime_max);
1950 stats[ST_F_CT_MAX] = mkf_u32(FN_MAX, px->be_counters.ctime_max);
1951 stats[ST_F_RT_MAX] = mkf_u32(FN_MAX, px->be_counters.dtime_max);
1952 stats[ST_F_TT_MAX] = mkf_u32(FN_MAX, px->be_counters.ttime_max);
1953
William Lallemand74c24fb2016-11-21 17:18:36 +01001954 return 1;
1955}
1956
1957/* Dumps a line for backend <px> to the trash for and uses the state from stream
Willy Tarreau43241ff2019-10-09 11:27:51 +02001958 * interface <si>. The caller is responsible for clearing the trash if needed.
1959 * Returns non-zero if it emits anything, zero otherwise.
William Lallemand74c24fb2016-11-21 17:18:36 +01001960 */
Willy Tarreau43241ff2019-10-09 11:27:51 +02001961static int stats_dump_be_stats(struct stream_interface *si, struct proxy *px)
William Lallemand74c24fb2016-11-21 17:18:36 +01001962{
1963 struct appctx *appctx = __objt_appctx(si->end);
1964
1965 if (!(px->cap & PR_CAP_BE))
1966 return 0;
1967
1968 if ((appctx->ctx.stats.flags & STAT_BOUND) && !(appctx->ctx.stats.type & (1 << STATS_TYPE_BE)))
1969 return 0;
1970
Willy Tarreau43241ff2019-10-09 11:27:51 +02001971 if (!stats_fill_be_stats(px, appctx->ctx.stats.flags, stats, ST_F_TOTAL_FIELDS))
William Lallemand74c24fb2016-11-21 17:18:36 +01001972 return 0;
1973
Willy Tarreau43241ff2019-10-09 11:27:51 +02001974 return stats_dump_one_line(stats, px, appctx);
William Lallemand74c24fb2016-11-21 17:18:36 +01001975}
1976
1977/* Dumps the HTML table header for proxy <px> to the trash for and uses the state from
1978 * stream interface <si> and per-uri parameters <uri>. The caller is responsible
1979 * for clearing the trash if needed.
1980 */
Willy Tarreau676c29e2019-10-09 10:50:01 +02001981static void stats_dump_html_px_hdr(struct stream_interface *si, struct proxy *px)
William Lallemand74c24fb2016-11-21 17:18:36 +01001982{
1983 struct appctx *appctx = __objt_appctx(si->end);
1984 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
1985
1986 if (px->cap & PR_CAP_BE && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
1987 /* A form to enable/disable this proxy servers */
1988
1989 /* scope_txt = search pattern + search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
1990 scope_txt[0] = 0;
1991 if (appctx->ctx.stats.scope_len) {
Christopher Fauleted7a0662019-01-14 11:07:34 +01001992 const char *scope_ptr = stats_scope_ptr(appctx, si);
1993
William Lallemand74c24fb2016-11-21 17:18:36 +01001994 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Christopher Fauleted7a0662019-01-14 11:07:34 +01001995 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), scope_ptr, appctx->ctx.stats.scope_len);
William Lallemand74c24fb2016-11-21 17:18:36 +01001996 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
1997 }
1998
1999 chunk_appendf(&trash,
2000 "<form method=\"post\">");
2001 }
2002
2003 /* print a new table */
2004 chunk_appendf(&trash,
2005 "<table class=\"tbl\" width=\"100%%\">\n"
2006 "<tr class=\"titre\">"
2007 "<th class=\"pxname\" width=\"10%%\">");
2008
2009 chunk_appendf(&trash,
2010 "<a name=\"%s\"></a>%s"
2011 "<a class=px href=\"#%s\">%s</a>",
2012 px->id,
Willy Tarreau676c29e2019-10-09 10:50:01 +02002013 (appctx->ctx.stats.flags & STAT_SHLGNDS) ? "<u>":"",
William Lallemand74c24fb2016-11-21 17:18:36 +01002014 px->id, px->id);
2015
Willy Tarreau676c29e2019-10-09 10:50:01 +02002016 if (appctx->ctx.stats.flags & STAT_SHLGNDS) {
William Lallemand74c24fb2016-11-21 17:18:36 +01002017 /* cap, mode, id */
2018 chunk_appendf(&trash, "<div class=tips>cap: %s, mode: %s, id: %d",
2019 proxy_cap_str(px->cap), proxy_mode_str(px->mode),
2020 px->uuid);
2021 chunk_appendf(&trash, "</div>");
2022 }
2023
2024 chunk_appendf(&trash,
2025 "%s</th>"
2026 "<th class=\"%s\" width=\"90%%\">%s</th>"
2027 "</tr>\n"
2028 "</table>\n"
2029 "<table class=\"tbl\" width=\"100%%\">\n"
2030 "<tr class=\"titre\">",
Willy Tarreau676c29e2019-10-09 10:50:01 +02002031 (appctx->ctx.stats.flags & STAT_SHLGNDS) ? "</u>":"",
William Lallemand74c24fb2016-11-21 17:18:36 +01002032 px->desc ? "desc" : "empty", px->desc ? px->desc : "");
2033
Christopher Fauletbc271ec2019-12-02 11:29:04 +01002034 if (appctx->ctx.stats.flags & STAT_ADMIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +01002035 /* Column heading for Enable or Disable server */
Christopher Fauletbc271ec2019-12-02 11:29:04 +01002036 if ((px->cap & PR_CAP_BE) && px->srv)
2037 chunk_appendf(&trash,
2038 "<th rowspan=2 width=1><input type=\"checkbox\" "
2039 "onclick=\"for(c in document.getElementsByClassName('%s-checkbox')) "
2040 "document.getElementsByClassName('%s-checkbox').item(c).checked = this.checked\"></th>",
2041 px->id,
2042 px->id);
2043 else
2044 chunk_appendf(&trash, "<th rowspan=2></th>");
William Lallemand74c24fb2016-11-21 17:18:36 +01002045 }
2046
2047 chunk_appendf(&trash,
2048 "<th rowspan=2></th>"
2049 "<th colspan=3>Queue</th>"
2050 "<th colspan=3>Session rate</th><th colspan=6>Sessions</th>"
2051 "<th colspan=2>Bytes</th><th colspan=2>Denied</th>"
2052 "<th colspan=3>Errors</th><th colspan=2>Warnings</th>"
2053 "<th colspan=9>Server</th>"
2054 "</tr>\n"
2055 "<tr class=\"titre\">"
2056 "<th>Cur</th><th>Max</th><th>Limit</th>"
2057 "<th>Cur</th><th>Max</th><th>Limit</th><th>Cur</th><th>Max</th>"
2058 "<th>Limit</th><th>Total</th><th>LbTot</th><th>Last</th><th>In</th><th>Out</th>"
2059 "<th>Req</th><th>Resp</th><th>Req</th><th>Conn</th>"
2060 "<th>Resp</th><th>Retr</th><th>Redis</th>"
2061 "<th>Status</th><th>LastChk</th><th>Wght</th><th>Act</th>"
2062 "<th>Bck</th><th>Chk</th><th>Dwn</th><th>Dwntme</th>"
2063 "<th>Thrtle</th>\n"
2064 "</tr>");
2065}
2066
2067/* Dumps the HTML table trailer for proxy <px> to the trash for and uses the state from
2068 * stream interface <si>. The caller is responsible for clearing the trash if needed.
2069 */
2070static void stats_dump_html_px_end(struct stream_interface *si, struct proxy *px)
2071{
2072 struct appctx *appctx = __objt_appctx(si->end);
2073 chunk_appendf(&trash, "</table>");
2074
2075 if ((px->cap & PR_CAP_BE) && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
2076 /* close the form used to enable/disable this proxy servers */
2077 chunk_appendf(&trash,
2078 "Choose the action to perform on the checked servers : "
2079 "<select name=action>"
2080 "<option value=\"\"></option>"
2081 "<option value=\"ready\">Set state to READY</option>"
2082 "<option value=\"drain\">Set state to DRAIN</option>"
2083 "<option value=\"maint\">Set state to MAINT</option>"
2084 "<option value=\"dhlth\">Health: disable checks</option>"
2085 "<option value=\"ehlth\">Health: enable checks</option>"
2086 "<option value=\"hrunn\">Health: force UP</option>"
2087 "<option value=\"hnolb\">Health: force NOLB</option>"
2088 "<option value=\"hdown\">Health: force DOWN</option>"
2089 "<option value=\"dagent\">Agent: disable checks</option>"
2090 "<option value=\"eagent\">Agent: enable checks</option>"
2091 "<option value=\"arunn\">Agent: force UP</option>"
2092 "<option value=\"adown\">Agent: force DOWN</option>"
2093 "<option value=\"shutdown\">Kill Sessions</option>"
2094 "</select>"
2095 "<input type=\"hidden\" name=\"b\" value=\"#%d\">"
2096 "&nbsp;<input type=\"submit\" value=\"Apply\">"
2097 "</form>",
2098 px->uuid);
2099 }
2100
2101 chunk_appendf(&trash, "<p>\n");
2102}
2103
2104/*
2105 * Dumps statistics for a proxy. The output is sent to the stream interface's
2106 * input buffer. Returns 0 if it had to stop dumping data because of lack of
2107 * buffer space, or non-zero if everything completed. This function is used
2108 * both by the CLI and the HTTP entry points, and is able to dump the output
2109 * in HTML or CSV formats. If the later, <uri> must be NULL.
2110 */
Christopher Fauletef779222018-10-31 08:47:01 +01002111int stats_dump_proxy_to_buffer(struct stream_interface *si, struct htx *htx,
2112 struct proxy *px, struct uri_auth *uri)
William Lallemand74c24fb2016-11-21 17:18:36 +01002113{
2114 struct appctx *appctx = __objt_appctx(si->end);
2115 struct stream *s = si_strm(si);
2116 struct channel *rep = si_ic(si);
2117 struct server *sv, *svs; /* server and server-state, server-state=server or server->track */
2118 struct listener *l;
William Lallemand74c24fb2016-11-21 17:18:36 +01002119
2120 chunk_reset(&trash);
2121
2122 switch (appctx->ctx.stats.px_st) {
2123 case STAT_PX_ST_INIT:
2124 /* we are on a new proxy */
2125 if (uri && uri->scope) {
2126 /* we have a limited scope, we have to check the proxy name */
2127 struct stat_scope *scope;
2128 int len;
2129
2130 len = strlen(px->id);
2131 scope = uri->scope;
2132
2133 while (scope) {
2134 /* match exact proxy name */
2135 if (scope->px_len == len && !memcmp(px->id, scope->px_id, len))
2136 break;
2137
2138 /* match '.' which means 'self' proxy */
2139 if (!strcmp(scope->px_id, ".") && px == s->be)
2140 break;
2141 scope = scope->next;
2142 }
2143
2144 /* proxy name not found : don't dump anything */
2145 if (scope == NULL)
2146 return 1;
2147 }
2148
2149 /* if the user has requested a limited output and the proxy
2150 * name does not match, skip it.
2151 */
Christopher Fauleted7a0662019-01-14 11:07:34 +01002152 if (appctx->ctx.stats.scope_len) {
2153 const char *scope_ptr = stats_scope_ptr(appctx, si);
2154
2155 if (strnistr(px->id, strlen(px->id), scope_ptr, appctx->ctx.stats.scope_len) == NULL)
2156 return 1;
2157 }
William Lallemand74c24fb2016-11-21 17:18:36 +01002158
2159 if ((appctx->ctx.stats.flags & STAT_BOUND) &&
2160 (appctx->ctx.stats.iid != -1) &&
2161 (px->uuid != appctx->ctx.stats.iid))
2162 return 1;
2163
2164 appctx->ctx.stats.px_st = STAT_PX_ST_TH;
2165 /* fall through */
2166
2167 case STAT_PX_ST_TH:
2168 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreau676c29e2019-10-09 10:50:01 +02002169 stats_dump_html_px_hdr(si, px);
Christopher Fauletef779222018-10-31 08:47:01 +01002170 if (!stats_putchk(rep, htx, &trash))
2171 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002172 }
2173
2174 appctx->ctx.stats.px_st = STAT_PX_ST_FE;
2175 /* fall through */
2176
2177 case STAT_PX_ST_FE:
2178 /* print the frontend */
2179 if (stats_dump_fe_stats(si, px)) {
Christopher Fauletef779222018-10-31 08:47:01 +01002180 if (!stats_putchk(rep, htx, &trash))
2181 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002182 }
2183
2184 appctx->ctx.stats.l = px->conf.listeners.n;
2185 appctx->ctx.stats.px_st = STAT_PX_ST_LI;
2186 /* fall through */
2187
2188 case STAT_PX_ST_LI:
2189 /* stats.l has been initialized above */
2190 for (; appctx->ctx.stats.l != &px->conf.listeners; appctx->ctx.stats.l = l->by_fe.n) {
Christopher Fauletef779222018-10-31 08:47:01 +01002191 if (htx) {
2192 if (htx_almost_full(htx))
2193 goto full;
2194 }
2195 else {
2196 if (buffer_almost_full(&rep->buf))
2197 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002198 }
2199
2200 l = LIST_ELEM(appctx->ctx.stats.l, struct listener *, by_fe);
2201 if (!l->counters)
2202 continue;
2203
2204 if (appctx->ctx.stats.flags & STAT_BOUND) {
2205 if (!(appctx->ctx.stats.type & (1 << STATS_TYPE_SO)))
2206 break;
2207
2208 if (appctx->ctx.stats.sid != -1 && l->luid != appctx->ctx.stats.sid)
2209 continue;
2210 }
2211
2212 /* print the frontend */
Willy Tarreau43241ff2019-10-09 11:27:51 +02002213 if (stats_dump_li_stats(si, px, l)) {
Christopher Fauletef779222018-10-31 08:47:01 +01002214 if (!stats_putchk(rep, htx, &trash))
2215 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002216 }
2217 }
2218
2219 appctx->ctx.stats.sv = px->srv; /* may be NULL */
2220 appctx->ctx.stats.px_st = STAT_PX_ST_SV;
2221 /* fall through */
2222
2223 case STAT_PX_ST_SV:
2224 /* stats.sv has been initialized above */
2225 for (; appctx->ctx.stats.sv != NULL; appctx->ctx.stats.sv = sv->next) {
Christopher Fauletef779222018-10-31 08:47:01 +01002226 if (htx) {
2227 if (htx_almost_full(htx))
2228 goto full;
2229 }
2230 else {
2231 if (buffer_almost_full(&rep->buf))
2232 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002233 }
2234
2235 sv = appctx->ctx.stats.sv;
2236
2237 if (appctx->ctx.stats.flags & STAT_BOUND) {
2238 if (!(appctx->ctx.stats.type & (1 << STATS_TYPE_SV)))
2239 break;
2240
2241 if (appctx->ctx.stats.sid != -1 && sv->puid != appctx->ctx.stats.sid)
2242 continue;
2243 }
2244
2245 svs = sv;
2246 while (svs->track)
2247 svs = svs->track;
2248
2249 /* do not report servers which are DOWN and not changing state */
2250 if ((appctx->ctx.stats.flags & STAT_HIDE_DOWN) &&
Emeric Brun52a91d32017-08-31 14:41:55 +02002251 ((sv->cur_admin & SRV_ADMF_MAINT) || /* server is in maintenance */
2252 (sv->cur_state == SRV_ST_STOPPED && /* server is down */
William Lallemand74c24fb2016-11-21 17:18:36 +01002253 (!((svs->agent.state | svs->check.state) & CHK_ST_ENABLED) ||
2254 ((svs->agent.state & CHK_ST_ENABLED) && !svs->agent.health) ||
2255 ((svs->check.state & CHK_ST_ENABLED) && !svs->check.health))))) {
2256 continue;
2257 }
2258
Willy Tarreau43241ff2019-10-09 11:27:51 +02002259 if (stats_dump_sv_stats(si, px, sv)) {
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 } /* for sv */
2264
2265 appctx->ctx.stats.px_st = STAT_PX_ST_BE;
2266 /* fall through */
2267
2268 case STAT_PX_ST_BE:
2269 /* print the backend */
Willy Tarreau43241ff2019-10-09 11:27:51 +02002270 if (stats_dump_be_stats(si, px)) {
Christopher Fauletef779222018-10-31 08:47:01 +01002271 if (!stats_putchk(rep, htx, &trash))
2272 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002273 }
2274
2275 appctx->ctx.stats.px_st = STAT_PX_ST_END;
2276 /* fall through */
2277
2278 case STAT_PX_ST_END:
2279 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
2280 stats_dump_html_px_end(si, px);
Christopher Fauletef779222018-10-31 08:47:01 +01002281 if (!stats_putchk(rep, htx, &trash))
2282 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002283 }
2284
2285 appctx->ctx.stats.px_st = STAT_PX_ST_FIN;
2286 /* fall through */
2287
2288 case STAT_PX_ST_FIN:
2289 return 1;
2290
2291 default:
2292 /* unknown state, we should put an abort() here ! */
2293 return 1;
2294 }
Christopher Fauletef779222018-10-31 08:47:01 +01002295
2296 full:
2297 si_rx_room_blk(si);
2298 return 0;
William Lallemand74c24fb2016-11-21 17:18:36 +01002299}
2300
2301/* Dumps the HTTP stats head block to the trash for and uses the per-uri
2302 * parameters <uri>. The caller is responsible for clearing the trash if needed.
2303 */
Willy Tarreau676c29e2019-10-09 10:50:01 +02002304static void stats_dump_html_head(struct appctx *appctx, struct uri_auth *uri)
William Lallemand74c24fb2016-11-21 17:18:36 +01002305{
2306 /* WARNING! This must fit in the first buffer !!! */
2307 chunk_appendf(&trash,
2308 "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"\n"
2309 "\"http://www.w3.org/TR/html4/loose.dtd\">\n"
2310 "<html><head><title>Statistics Report for " PRODUCT_NAME "%s%s</title>\n"
2311 "<meta http-equiv=\"content-type\" content=\"text/html; charset=iso-8859-1\">\n"
2312 "<style type=\"text/css\"><!--\n"
2313 "body {"
2314 " font-family: arial, helvetica, sans-serif;"
2315 " font-size: 12px;"
2316 " font-weight: normal;"
2317 " color: black;"
2318 " background: white;"
2319 "}\n"
2320 "th,td {"
2321 " font-size: 10px;"
2322 "}\n"
2323 "h1 {"
2324 " font-size: x-large;"
2325 " margin-bottom: 0.5em;"
2326 "}\n"
2327 "h2 {"
2328 " font-family: helvetica, arial;"
2329 " font-size: x-large;"
2330 " font-weight: bold;"
2331 " font-style: italic;"
2332 " color: #6020a0;"
2333 " margin-top: 0em;"
2334 " margin-bottom: 0em;"
2335 "}\n"
2336 "h3 {"
2337 " font-family: helvetica, arial;"
2338 " font-size: 16px;"
2339 " font-weight: bold;"
2340 " color: #b00040;"
2341 " background: #e8e8d0;"
2342 " margin-top: 0em;"
2343 " margin-bottom: 0em;"
2344 "}\n"
2345 "li {"
2346 " margin-top: 0.25em;"
2347 " margin-right: 2em;"
2348 "}\n"
2349 ".hr {margin-top: 0.25em;"
2350 " border-color: black;"
2351 " border-bottom-style: solid;"
2352 "}\n"
2353 ".titre {background: #20D0D0;color: #000000; font-weight: bold; text-align: center;}\n"
2354 ".total {background: #20D0D0;color: #ffff80;}\n"
2355 ".frontend {background: #e8e8d0;}\n"
2356 ".socket {background: #d0d0d0;}\n"
2357 ".backend {background: #e8e8d0;}\n"
2358 ".active_down {background: #ff9090;}\n"
2359 ".active_going_up {background: #ffd020;}\n"
2360 ".active_going_down {background: #ffffa0;}\n"
2361 ".active_up {background: #c0ffc0;}\n"
2362 ".active_nolb {background: #20a0ff;}\n"
2363 ".active_draining {background: #20a0FF;}\n"
2364 ".active_no_check {background: #e0e0e0;}\n"
2365 ".backup_down {background: #ff9090;}\n"
2366 ".backup_going_up {background: #ff80ff;}\n"
2367 ".backup_going_down {background: #c060ff;}\n"
2368 ".backup_up {background: #b0d0ff;}\n"
2369 ".backup_nolb {background: #90b0e0;}\n"
2370 ".backup_draining {background: #cc9900;}\n"
2371 ".backup_no_check {background: #e0e0e0;}\n"
2372 ".maintain {background: #c07820;}\n"
2373 ".rls {letter-spacing: 0.2em; margin-right: 1px;}\n" /* right letter spacing (used for grouping digits) */
2374 "\n"
2375 "a.px:link {color: #ffff40; text-decoration: none;}"
2376 "a.px:visited {color: #ffff40; text-decoration: none;}"
2377 "a.px:hover {color: #ffffff; text-decoration: none;}"
2378 "a.lfsb:link {color: #000000; text-decoration: none;}"
2379 "a.lfsb:visited {color: #000000; text-decoration: none;}"
2380 "a.lfsb:hover {color: #505050; text-decoration: none;}"
2381 "\n"
2382 "table.tbl { border-collapse: collapse; border-style: none;}\n"
2383 "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"
2384 "table.tbl td.ac { text-align: center;}\n"
2385 "table.tbl th { border-width: 1px; border-style: solid solid solid solid; border-color: gray;}\n"
2386 "table.tbl th.pxname { background: #b00040; color: #ffff40; font-weight: bold; border-style: solid solid none solid; padding: 2px 3px; white-space: nowrap;}\n"
2387 "table.tbl th.empty { border-style: none; empty-cells: hide; background: white;}\n"
2388 "table.tbl th.desc { background: white; border-style: solid solid none solid; text-align: left; padding: 2px 3px;}\n"
2389 "\n"
2390 "table.lgd { border-collapse: collapse; border-width: 1px; border-style: none none none solid; border-color: black;}\n"
2391 "table.lgd td { border-width: 1px; border-style: solid solid solid solid; border-color: gray; padding: 2px;}\n"
2392 "table.lgd td.noborder { border-style: none; padding: 2px; white-space: nowrap;}\n"
2393 "table.det { border-collapse: collapse; border-style: none; }\n"
2394 "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"
2395 "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"
2396 "u {text-decoration:none; border-bottom: 1px dotted black;}\n"
2397 "div.tips {\n"
2398 " display:block;\n"
2399 " visibility:hidden;\n"
2400 " z-index:2147483647;\n"
2401 " position:absolute;\n"
2402 " padding:2px 4px 3px;\n"
2403 " background:#f0f060; color:#000000;\n"
2404 " border:1px solid #7040c0;\n"
2405 " white-space:nowrap;\n"
2406 " font-style:normal;font-size:11px;font-weight:normal;\n"
2407 " -moz-border-radius:3px;-webkit-border-radius:3px;border-radius:3px;\n"
2408 " -moz-box-shadow:gray 2px 2px 3px;-webkit-box-shadow:gray 2px 2px 3px;box-shadow:gray 2px 2px 3px;\n"
2409 "}\n"
2410 "u:hover div.tips {visibility:visible;}\n"
2411 "-->\n"
2412 "</style></head>\n",
Willy Tarreau676c29e2019-10-09 10:50:01 +02002413 (appctx->ctx.stats.flags & STAT_SHNODE) ? " on " : "",
2414 (appctx->ctx.stats.flags & STAT_SHNODE) ? (uri->node ? uri->node : global.node) : ""
William Lallemand74c24fb2016-11-21 17:18:36 +01002415 );
2416}
2417
2418/* Dumps the HTML stats information block to the trash for and uses the state from
2419 * stream interface <si> and per-uri parameters <uri>. The caller is responsible
2420 * for clearing the trash if needed.
2421 */
2422static void stats_dump_html_info(struct stream_interface *si, struct uri_auth *uri)
2423{
2424 struct appctx *appctx = __objt_appctx(si->end);
2425 unsigned int up = (now.tv_sec - start_date.tv_sec);
2426 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
Christopher Fauleted7a0662019-01-14 11:07:34 +01002427 const char *scope_ptr = stats_scope_ptr(appctx, si);
Willy Tarreau1713c032019-05-23 12:23:55 +02002428 unsigned long long bps = (unsigned long long)read_freq_ctr(&global.out_32bps) * 32;
2429
2430 /* Turn the bytes per second to bits per second and take care of the
2431 * usual ethernet overhead in order to help figure how far we are from
2432 * interface saturation since it's the only case which usually matters.
2433 * For this we count the total size of an Ethernet frame on the wire
2434 * including preamble and IFG (1538) for the largest TCP segment it
2435 * transports (1448 with TCP timestamps). This is not valid for smaller
2436 * packets (under-estimated), but it gives a reasonably accurate
2437 * estimation of how far we are from uplink saturation.
2438 */
2439 bps = bps * 8 * 1538 / 1448;
William Lallemand74c24fb2016-11-21 17:18:36 +01002440
2441 /* WARNING! this has to fit the first packet too.
2442 * We are around 3.5 kB, add adding entries will
2443 * become tricky if we want to support 4kB buffers !
2444 */
2445 chunk_appendf(&trash,
2446 "<body><h1><a href=\"" PRODUCT_URL "\" style=\"text-decoration: none;\">"
2447 PRODUCT_NAME "%s</a></h1>\n"
2448 "<h2>Statistics Report for pid %d%s%s%s%s</h2>\n"
2449 "<hr width=\"100%%\" class=\"hr\">\n"
2450 "<h3>&gt; General process information</h3>\n"
2451 "<table border=0><tr><td align=\"left\" nowrap width=\"1%%\">\n"
Yves Lafon95317282018-02-26 11:10:37 +01002452 "<p><b>pid = </b> %d (process #%d, nbproc = %d, nbthread = %d)<br>\n"
William Lallemand74c24fb2016-11-21 17:18:36 +01002453 "<b>uptime = </b> %dd %dh%02dm%02ds<br>\n"
2454 "<b>system limits:</b> memmax = %s%s; ulimit-n = %d<br>\n"
2455 "<b>maxsock = </b> %d; <b>maxconn = </b> %d; <b>maxpipes = </b> %d<br>\n"
Willy Tarreau1713c032019-05-23 12:23:55 +02002456 "current conns = %d; current pipes = %d/%d; conn rate = %d/sec; bit rate = %.3f %cbps<br>\n"
William Lallemand74c24fb2016-11-21 17:18:36 +01002457 "Running tasks: %d/%d; idle = %d %%<br>\n"
2458 "</td><td align=\"center\" nowrap>\n"
2459 "<table class=\"lgd\"><tr>\n"
2460 "<td class=\"active_up\">&nbsp;</td><td class=\"noborder\">active UP </td>"
2461 "<td class=\"backup_up\">&nbsp;</td><td class=\"noborder\">backup UP </td>"
2462 "</tr><tr>\n"
2463 "<td class=\"active_going_down\"></td><td class=\"noborder\">active UP, going down </td>"
2464 "<td class=\"backup_going_down\"></td><td class=\"noborder\">backup UP, going down </td>"
2465 "</tr><tr>\n"
2466 "<td class=\"active_going_up\"></td><td class=\"noborder\">active DOWN, going up </td>"
2467 "<td class=\"backup_going_up\"></td><td class=\"noborder\">backup DOWN, going up </td>"
2468 "</tr><tr>\n"
2469 "<td class=\"active_down\"></td><td class=\"noborder\">active or backup DOWN &nbsp;</td>"
2470 "<td class=\"active_no_check\"></td><td class=\"noborder\">not checked </td>"
2471 "</tr><tr>\n"
2472 "<td class=\"maintain\"></td><td class=\"noborder\" colspan=\"3\">active or backup DOWN for maintenance (MAINT) &nbsp;</td>"
2473 "</tr><tr>\n"
2474 "<td class=\"active_draining\"></td><td class=\"noborder\" colspan=\"3\">active or backup SOFT STOPPED for maintenance &nbsp;</td>"
2475 "</tr></table>\n"
2476 "Note: \"NOLB\"/\"DRAIN\" = UP with load-balancing disabled."
2477 "</td>"
2478 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
2479 "<b>Display option:</b><ul style=\"margin-top: 0.25em;\">"
2480 "",
Willy Tarreau676c29e2019-10-09 10:50:01 +02002481 (appctx->ctx.stats.flags & STAT_HIDEVER) ? "" : (stats_version_string),
2482 pid, (appctx->ctx.stats.flags & STAT_SHNODE) ? " on " : "",
2483 (appctx->ctx.stats.flags & STAT_SHNODE) ? (uri->node ? uri->node : global.node) : "",
2484 (appctx->ctx.stats.flags & STAT_SHDESC) ? ": " : "",
2485 (appctx->ctx.stats.flags & STAT_SHDESC) ? (uri->desc ? uri->desc : global.desc) : "",
Yves Lafon95317282018-02-26 11:10:37 +01002486 pid, relative_pid, global.nbproc, global.nbthread,
William Lallemand74c24fb2016-11-21 17:18:36 +01002487 up / 86400, (up % 86400) / 3600,
2488 (up % 3600) / 60, (up % 60),
2489 global.rlimit_memmax ? ultoa(global.rlimit_memmax) : "unlimited",
2490 global.rlimit_memmax ? " MB" : "",
2491 global.rlimit_nofile,
2492 global.maxsock, global.maxconn, global.maxpipes,
2493 actconn, pipes_used, pipes_used+pipes_free, read_freq_ctr(&global.conn_per_sec),
Willy Tarreau1713c032019-05-23 12:23:55 +02002494 bps >= 1000000000UL ? (bps / 1000000000.0) : bps >= 1000000UL ? (bps / 1000000.0) : (bps / 1000.0),
2495 bps >= 1000000000UL ? 'G' : bps >= 1000000UL ? 'M' : 'k',
Willy Tarreau81036f22019-05-20 19:24:50 +02002496 tasks_run_queue_cur, nb_tasks_cur, ti->idle_pct
William Lallemand74c24fb2016-11-21 17:18:36 +01002497 );
2498
2499 /* scope_txt = search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
Christopher Fauleted7a0662019-01-14 11:07:34 +01002500 memcpy(scope_txt, scope_ptr, appctx->ctx.stats.scope_len);
William Lallemand74c24fb2016-11-21 17:18:36 +01002501 scope_txt[appctx->ctx.stats.scope_len] = '\0';
2502
2503 chunk_appendf(&trash,
2504 "<li><form method=\"GET\">Scope : <input value=\"%s\" name=\"" STAT_SCOPE_INPUT_NAME "\" size=\"8\" maxlength=\"%d\" tabindex=\"1\"/></form>\n",
2505 (appctx->ctx.stats.scope_len > 0) ? scope_txt : "",
2506 STAT_SCOPE_TXT_MAXLEN);
2507
2508 /* scope_txt = search pattern + search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
2509 scope_txt[0] = 0;
2510 if (appctx->ctx.stats.scope_len) {
2511 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Christopher Fauleted7a0662019-01-14 11:07:34 +01002512 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), scope_ptr, appctx->ctx.stats.scope_len);
William Lallemand74c24fb2016-11-21 17:18:36 +01002513 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
2514 }
2515
2516 if (appctx->ctx.stats.flags & STAT_HIDE_DOWN)
2517 chunk_appendf(&trash,
2518 "<li><a href=\"%s%s%s%s\">Show all servers</a><br>\n",
2519 uri->uri_prefix,
2520 "",
2521 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2522 scope_txt);
2523 else
2524 chunk_appendf(&trash,
2525 "<li><a href=\"%s%s%s%s\">Hide 'DOWN' servers</a><br>\n",
2526 uri->uri_prefix,
2527 ";up",
2528 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2529 scope_txt);
2530
2531 if (uri->refresh > 0) {
2532 if (appctx->ctx.stats.flags & STAT_NO_REFRESH)
2533 chunk_appendf(&trash,
2534 "<li><a href=\"%s%s%s%s\">Enable refresh</a><br>\n",
2535 uri->uri_prefix,
2536 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2537 "",
2538 scope_txt);
2539 else
2540 chunk_appendf(&trash,
2541 "<li><a href=\"%s%s%s%s\">Disable refresh</a><br>\n",
2542 uri->uri_prefix,
2543 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2544 ";norefresh",
2545 scope_txt);
2546 }
2547
2548 chunk_appendf(&trash,
2549 "<li><a href=\"%s%s%s%s\">Refresh now</a><br>\n",
2550 uri->uri_prefix,
2551 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2552 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2553 scope_txt);
2554
2555 chunk_appendf(&trash,
2556 "<li><a href=\"%s;csv%s%s\">CSV export</a><br>\n",
2557 uri->uri_prefix,
2558 (uri->refresh > 0) ? ";norefresh" : "",
2559 scope_txt);
2560
2561 chunk_appendf(&trash,
Christopher Faulet6338a082019-09-09 15:50:54 +02002562 "<li><a href=\"%s;json%s%s\">JSON export</a> (<a href=\"%s;json-schema\">schema</a>)<br>\n",
2563 uri->uri_prefix,
2564 (uri->refresh > 0) ? ";norefresh" : "",
2565 scope_txt, uri->uri_prefix);
2566
2567 chunk_appendf(&trash,
William Lallemand74c24fb2016-11-21 17:18:36 +01002568 "</ul></td>"
2569 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
2570 "<b>External resources:</b><ul style=\"margin-top: 0.25em;\">\n"
2571 "<li><a href=\"" PRODUCT_URL "\">Primary site</a><br>\n"
2572 "<li><a href=\"" PRODUCT_URL_UPD "\">Updates (v" PRODUCT_BRANCH ")</a><br>\n"
2573 "<li><a href=\"" PRODUCT_URL_DOC "\">Online manual</a><br>\n"
2574 "</ul>"
2575 "</td>"
2576 "</tr></table>\n"
2577 ""
2578 );
2579
2580 if (appctx->ctx.stats.st_code) {
2581 switch (appctx->ctx.stats.st_code) {
2582 case STAT_STATUS_DONE:
2583 chunk_appendf(&trash,
2584 "<p><div class=active_up>"
2585 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2586 "Action processed successfully."
2587 "</div>\n", uri->uri_prefix,
2588 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2589 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2590 scope_txt);
2591 break;
2592 case STAT_STATUS_NONE:
2593 chunk_appendf(&trash,
2594 "<p><div class=active_going_down>"
2595 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2596 "Nothing has changed."
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_PART:
2603 chunk_appendf(&trash,
2604 "<p><div class=active_going_down>"
2605 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2606 "Action partially processed.<br>"
2607 "Some server names are probably unknown or ambiguous (duplicated names in the backend)."
2608 "</div>\n", uri->uri_prefix,
2609 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2610 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2611 scope_txt);
2612 break;
2613 case STAT_STATUS_ERRP:
2614 chunk_appendf(&trash,
2615 "<p><div class=active_down>"
2616 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2617 "Action not processed because of invalid parameters."
2618 "<ul>"
2619 "<li>The action is maybe unknown.</li>"
Christopher Faulet2f9a41d2019-02-27 15:30:57 +01002620 "<li>Invalid key parameter (empty or too long).</li>"
William Lallemand74c24fb2016-11-21 17:18:36 +01002621 "<li>The backend name is probably unknown or ambiguous (duplicated names).</li>"
2622 "<li>Some server names are probably unknown or ambiguous (duplicated names in the backend).</li>"
2623 "</ul>"
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_EXCD:
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 not processed : the buffer couldn't store all the data.<br>"
2634 "You should retry with less servers at a time.</b>"
2635 "</div>\n", uri->uri_prefix,
2636 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2637 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2638 scope_txt);
2639 break;
2640 case STAT_STATUS_DENY:
2641 chunk_appendf(&trash,
2642 "<p><div class=active_down>"
2643 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2644 "<b>Action denied.</b>"
2645 "</div>\n", uri->uri_prefix,
2646 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2647 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2648 scope_txt);
2649 break;
Christopher Faulet3c2ecf72019-02-27 16:41:27 +01002650 case STAT_STATUS_IVAL:
2651 chunk_appendf(&trash,
2652 "<p><div class=active_down>"
2653 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2654 "<b>Invalid requests (unsupported method or chunked encoded request).</b>"
2655 "</div>\n", uri->uri_prefix,
2656 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2657 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2658 scope_txt);
2659 break;
William Lallemand74c24fb2016-11-21 17:18:36 +01002660 default:
2661 chunk_appendf(&trash,
2662 "<p><div class=active_no_check>"
2663 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2664 "Unexpected result."
2665 "</div>\n", uri->uri_prefix,
2666 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2667 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2668 scope_txt);
2669 }
2670 chunk_appendf(&trash, "<p>\n");
2671 }
2672}
2673
2674/* Dumps the HTML stats trailer block to the trash. The caller is responsible
2675 * for clearing the trash if needed.
2676 */
2677static void stats_dump_html_end()
2678{
2679 chunk_appendf(&trash, "</body></html>\n");
2680}
2681
Simon Horman05ee2132017-01-04 09:37:25 +01002682/* Dumps the stats JSON header to the trash buffer which. The caller is responsible
2683 * for clearing it if needed.
2684 */
2685static void stats_dump_json_header()
2686{
2687 chunk_strcat(&trash, "[");
2688}
2689
2690
2691/* Dumps the JSON stats trailer block to the trash. The caller is responsible
2692 * for clearing the trash if needed.
2693 */
2694static void stats_dump_json_end()
2695{
2696 chunk_strcat(&trash, "]");
2697}
2698
William Lallemand74c24fb2016-11-21 17:18:36 +01002699/* This function dumps statistics onto the stream interface's read buffer in
2700 * either CSV or HTML format. <uri> contains some HTML-specific parameters that
2701 * are ignored for CSV format (hence <uri> may be NULL there). It returns 0 if
2702 * it had to stop writing data and an I/O is needed, 1 if the dump is finished
2703 * and the stream must be closed, or -1 in case of any error. This function is
2704 * used by both the CLI and the HTTP handlers.
2705 */
Christopher Fauletef779222018-10-31 08:47:01 +01002706static int stats_dump_stat_to_buffer(struct stream_interface *si, struct htx *htx,
2707 struct uri_auth *uri)
William Lallemand74c24fb2016-11-21 17:18:36 +01002708{
2709 struct appctx *appctx = __objt_appctx(si->end);
2710 struct channel *rep = si_ic(si);
2711 struct proxy *px;
2712
2713 chunk_reset(&trash);
2714
2715 switch (appctx->st2) {
2716 case STAT_ST_INIT:
2717 appctx->st2 = STAT_ST_HEAD; /* let's start producing data */
2718 /* fall through */
2719
2720 case STAT_ST_HEAD:
2721 if (appctx->ctx.stats.flags & STAT_FMT_HTML)
Willy Tarreau676c29e2019-10-09 10:50:01 +02002722 stats_dump_html_head(appctx, uri);
Christopher Faulet6338a082019-09-09 15:50:54 +02002723 else if (appctx->ctx.stats.flags & STAT_JSON_SCHM)
2724 stats_dump_json_schema(&trash);
Simon Horman05ee2132017-01-04 09:37:25 +01002725 else if (appctx->ctx.stats.flags & STAT_FMT_JSON)
Willy Tarreau9d7fb632017-04-11 07:53:04 +02002726 stats_dump_json_header();
William Lallemand74c24fb2016-11-21 17:18:36 +01002727 else if (!(appctx->ctx.stats.flags & STAT_FMT_TYPED))
2728 stats_dump_csv_header();
2729
Christopher Fauletef779222018-10-31 08:47:01 +01002730 if (!stats_putchk(rep, htx, &trash))
2731 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002732
Christopher Faulet6338a082019-09-09 15:50:54 +02002733 if (appctx->ctx.stats.flags & STAT_JSON_SCHM) {
2734 appctx->st2 = STAT_ST_FIN;
2735 return 1;
2736 }
William Lallemand74c24fb2016-11-21 17:18:36 +01002737 appctx->st2 = STAT_ST_INFO;
2738 /* fall through */
2739
2740 case STAT_ST_INFO:
2741 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
2742 stats_dump_html_info(si, uri);
Christopher Fauletef779222018-10-31 08:47:01 +01002743 if (!stats_putchk(rep, htx, &trash))
2744 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002745 }
2746
Olivier Houchardfbc74e82017-11-24 16:54:05 +01002747 appctx->ctx.stats.px = proxies_list;
William Lallemand74c24fb2016-11-21 17:18:36 +01002748 appctx->ctx.stats.px_st = STAT_PX_ST_INIT;
2749 appctx->st2 = STAT_ST_LIST;
2750 /* fall through */
2751
2752 case STAT_ST_LIST:
2753 /* dump proxies */
2754 while (appctx->ctx.stats.px) {
Christopher Fauletef779222018-10-31 08:47:01 +01002755 if (htx) {
2756 if (htx_almost_full(htx))
2757 goto full;
2758 }
2759 else {
2760 if (buffer_almost_full(&rep->buf))
2761 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002762 }
2763
2764 px = appctx->ctx.stats.px;
2765 /* skip the disabled proxies, global frontend and non-networked ones */
2766 if (px->state != PR_STSTOPPED && px->uuid > 0 && (px->cap & (PR_CAP_FE | PR_CAP_BE)))
Christopher Fauletef779222018-10-31 08:47:01 +01002767 if (stats_dump_proxy_to_buffer(si, htx, px, uri) == 0)
William Lallemand74c24fb2016-11-21 17:18:36 +01002768 return 0;
2769
2770 appctx->ctx.stats.px = px->next;
2771 appctx->ctx.stats.px_st = STAT_PX_ST_INIT;
2772 }
2773 /* here, we just have reached the last proxy */
2774
2775 appctx->st2 = STAT_ST_END;
2776 /* fall through */
2777
2778 case STAT_ST_END:
Simon Horman05ee2132017-01-04 09:37:25 +01002779 if (appctx->ctx.stats.flags & (STAT_FMT_HTML|STAT_FMT_JSON)) {
2780 if (appctx->ctx.stats.flags & STAT_FMT_HTML)
2781 stats_dump_html_end();
2782 else
2783 stats_dump_json_end();
Christopher Fauletef779222018-10-31 08:47:01 +01002784 if (!stats_putchk(rep, htx, &trash))
2785 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002786 }
2787
2788 appctx->st2 = STAT_ST_FIN;
2789 /* fall through */
2790
2791 case STAT_ST_FIN:
2792 return 1;
2793
2794 default:
2795 /* unknown state ! */
2796 appctx->st2 = STAT_ST_FIN;
2797 return -1;
2798 }
Christopher Fauletef779222018-10-31 08:47:01 +01002799
2800 full:
2801 si_rx_room_blk(si);
2802 return 0;
2803
William Lallemand74c24fb2016-11-21 17:18:36 +01002804}
2805
2806/* We reached the stats page through a POST request. The appctx is
2807 * expected to have already been allocated by the caller.
2808 * Parse the posted data and enable/disable servers if necessary.
2809 * Returns 1 if request was parsed or zero if it needs more data.
2810 */
2811static int stats_process_http_post(struct stream_interface *si)
2812{
2813 struct stream *s = si_strm(si);
2814 struct appctx *appctx = objt_appctx(si->end);
2815
2816 struct proxy *px = NULL;
2817 struct server *sv = NULL;
2818
2819 char key[LINESIZE];
2820 int action = ST_ADM_ACTION_NONE;
2821 int reprocess = 0;
2822
2823 int total_servers = 0;
2824 int altered_servers = 0;
2825
2826 char *first_param, *cur_param, *next_param, *end_params;
2827 char *st_cur_param = NULL;
2828 char *st_next_param = NULL;
2829
Christopher Fauleta3618372018-12-13 21:59:56 +01002830 struct buffer *temp = get_trash_chunk();
William Lallemand74c24fb2016-11-21 17:18:36 +01002831
Christopher Fauletb7f88902019-07-15 21:56:43 +02002832 struct htx *htx = htxbuf(&s->req.buf);
2833 struct htx_blk *blk;
Christopher Fauleta3618372018-12-13 21:59:56 +01002834
Christopher Fauletb7f88902019-07-15 21:56:43 +02002835 /* we need more data */
2836 if (s->txn->req.msg_state < HTTP_MSG_DONE) {
2837 /* check if we can receive more */
2838 if (htx_free_data_space(htx) <= global.tune.maxrewrite) {
2839 appctx->ctx.stats.st_code = STAT_STATUS_EXCD;
2840 goto out;
Christopher Fauleta3618372018-12-13 21:59:56 +01002841 }
Christopher Fauletb7f88902019-07-15 21:56:43 +02002842 goto wait;
2843 }
Christopher Fauleta3618372018-12-13 21:59:56 +01002844
Christopher Fauletb7f88902019-07-15 21:56:43 +02002845 /* The request was fully received. Copy data */
2846 blk = htx_get_head_blk(htx);
2847 while (blk) {
2848 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Fauleta3618372018-12-13 21:59:56 +01002849
Christopher Fauletb7f88902019-07-15 21:56:43 +02002850 if (type == HTX_BLK_EOM || type == HTX_BLK_TLR || type == HTX_BLK_EOT)
2851 break;
2852 if (type == HTX_BLK_DATA) {
2853 struct ist v = htx_get_blk_value(htx, blk);
Christopher Fauleta3618372018-12-13 21:59:56 +01002854
Christopher Fauletb7f88902019-07-15 21:56:43 +02002855 if (!chunk_memcat(temp, v.ptr, v.len)) {
Christopher Faulet2f9a41d2019-02-27 15:30:57 +01002856 appctx->ctx.stats.st_code = STAT_STATUS_EXCD;
2857 goto out;
2858 }
Christopher Fauleta3618372018-12-13 21:59:56 +01002859 }
Christopher Fauletb7f88902019-07-15 21:56:43 +02002860 blk = htx_get_next_blk(htx, blk);
William Lallemand74c24fb2016-11-21 17:18:36 +01002861 }
2862
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002863 first_param = temp->area;
Christopher Fauleta3618372018-12-13 21:59:56 +01002864 end_params = temp->area + temp->data;
William Lallemand74c24fb2016-11-21 17:18:36 +01002865 cur_param = next_param = end_params;
2866 *end_params = '\0';
2867
2868 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
2869
2870 /*
2871 * Parse the parameters in reverse order to only store the last value.
2872 * From the html form, the backend and the action are at the end.
2873 */
2874 while (cur_param > first_param) {
2875 char *value;
2876 int poffset, plen;
2877
2878 cur_param--;
2879
2880 if ((*cur_param == '&') || (cur_param == first_param)) {
2881 reprocess_servers:
2882 /* Parse the key */
2883 poffset = (cur_param != first_param ? 1 : 0);
2884 plen = next_param - cur_param + (cur_param == first_param ? 1 : 0);
2885 if ((plen > 0) && (plen <= sizeof(key))) {
2886 strncpy(key, cur_param + poffset, plen);
2887 key[plen - 1] = '\0';
2888 } else {
Christopher Faulet2f9a41d2019-02-27 15:30:57 +01002889 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
William Lallemand74c24fb2016-11-21 17:18:36 +01002890 goto out;
2891 }
2892
2893 /* Parse the value */
2894 value = key;
2895 while (*value != '\0' && *value != '=') {
2896 value++;
2897 }
2898 if (*value == '=') {
2899 /* Ok, a value is found, we can mark the end of the key */
2900 *value++ = '\0';
2901 }
2902 if (url_decode(key) < 0 || url_decode(value) < 0)
2903 break;
2904
2905 /* Now we can check the key to see what to do */
2906 if (!px && (strcmp(key, "b") == 0)) {
2907 if ((px = proxy_be_by_name(value)) == NULL) {
2908 /* the backend name is unknown or ambiguous (duplicate names) */
2909 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
2910 goto out;
2911 }
2912 }
2913 else if (!action && (strcmp(key, "action") == 0)) {
2914 if (strcmp(value, "ready") == 0) {
2915 action = ST_ADM_ACTION_READY;
2916 }
2917 else if (strcmp(value, "drain") == 0) {
2918 action = ST_ADM_ACTION_DRAIN;
2919 }
2920 else if (strcmp(value, "maint") == 0) {
2921 action = ST_ADM_ACTION_MAINT;
2922 }
2923 else if (strcmp(value, "shutdown") == 0) {
2924 action = ST_ADM_ACTION_SHUTDOWN;
2925 }
2926 else if (strcmp(value, "dhlth") == 0) {
2927 action = ST_ADM_ACTION_DHLTH;
2928 }
2929 else if (strcmp(value, "ehlth") == 0) {
2930 action = ST_ADM_ACTION_EHLTH;
2931 }
2932 else if (strcmp(value, "hrunn") == 0) {
2933 action = ST_ADM_ACTION_HRUNN;
2934 }
2935 else if (strcmp(value, "hnolb") == 0) {
2936 action = ST_ADM_ACTION_HNOLB;
2937 }
2938 else if (strcmp(value, "hdown") == 0) {
2939 action = ST_ADM_ACTION_HDOWN;
2940 }
2941 else if (strcmp(value, "dagent") == 0) {
2942 action = ST_ADM_ACTION_DAGENT;
2943 }
2944 else if (strcmp(value, "eagent") == 0) {
2945 action = ST_ADM_ACTION_EAGENT;
2946 }
2947 else if (strcmp(value, "arunn") == 0) {
2948 action = ST_ADM_ACTION_ARUNN;
2949 }
2950 else if (strcmp(value, "adown") == 0) {
2951 action = ST_ADM_ACTION_ADOWN;
2952 }
2953 /* else these are the old supported methods */
2954 else if (strcmp(value, "disable") == 0) {
2955 action = ST_ADM_ACTION_DISABLE;
2956 }
2957 else if (strcmp(value, "enable") == 0) {
2958 action = ST_ADM_ACTION_ENABLE;
2959 }
2960 else if (strcmp(value, "stop") == 0) {
2961 action = ST_ADM_ACTION_STOP;
2962 }
2963 else if (strcmp(value, "start") == 0) {
2964 action = ST_ADM_ACTION_START;
2965 }
2966 else {
2967 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
2968 goto out;
2969 }
2970 }
2971 else if (strcmp(key, "s") == 0) {
2972 if (!(px && action)) {
2973 /*
2974 * Indicates that we'll need to reprocess the parameters
2975 * as soon as backend and action are known
2976 */
2977 if (!reprocess) {
2978 st_cur_param = cur_param;
2979 st_next_param = next_param;
2980 }
2981 reprocess = 1;
2982 }
2983 else if ((sv = findserver(px, value)) != NULL) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002984 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
William Lallemand74c24fb2016-11-21 17:18:36 +01002985 switch (action) {
2986 case ST_ADM_ACTION_DISABLE:
Emeric Brun52a91d32017-08-31 14:41:55 +02002987 if (!(sv->cur_admin & SRV_ADMF_FMAINT)) {
William Lallemand74c24fb2016-11-21 17:18:36 +01002988 altered_servers++;
2989 total_servers++;
2990 srv_set_admin_flag(sv, SRV_ADMF_FMAINT, "'disable' on stats page");
2991 }
2992 break;
2993 case ST_ADM_ACTION_ENABLE:
Emeric Brun52a91d32017-08-31 14:41:55 +02002994 if (sv->cur_admin & SRV_ADMF_FMAINT) {
William Lallemand74c24fb2016-11-21 17:18:36 +01002995 altered_servers++;
2996 total_servers++;
2997 srv_clr_admin_flag(sv, SRV_ADMF_FMAINT);
2998 }
2999 break;
3000 case ST_ADM_ACTION_STOP:
Emeric Brun52a91d32017-08-31 14:41:55 +02003001 if (!(sv->cur_admin & SRV_ADMF_FDRAIN)) {
William Lallemand74c24fb2016-11-21 17:18:36 +01003002 srv_set_admin_flag(sv, SRV_ADMF_FDRAIN, "'stop' on stats page");
3003 altered_servers++;
3004 total_servers++;
3005 }
3006 break;
3007 case ST_ADM_ACTION_START:
Emeric Brun52a91d32017-08-31 14:41:55 +02003008 if (sv->cur_admin & SRV_ADMF_FDRAIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +01003009 srv_clr_admin_flag(sv, SRV_ADMF_FDRAIN);
3010 altered_servers++;
3011 total_servers++;
3012 }
3013 break;
3014 case ST_ADM_ACTION_DHLTH:
3015 if (sv->check.state & CHK_ST_CONFIGURED) {
3016 sv->check.state &= ~CHK_ST_ENABLED;
3017 altered_servers++;
3018 total_servers++;
3019 }
3020 break;
3021 case ST_ADM_ACTION_EHLTH:
3022 if (sv->check.state & CHK_ST_CONFIGURED) {
3023 sv->check.state |= CHK_ST_ENABLED;
3024 altered_servers++;
3025 total_servers++;
3026 }
3027 break;
3028 case ST_ADM_ACTION_HRUNN:
3029 if (!(sv->track)) {
3030 sv->check.health = sv->check.rise + sv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02003031 srv_set_running(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01003032 altered_servers++;
3033 total_servers++;
3034 }
3035 break;
3036 case ST_ADM_ACTION_HNOLB:
3037 if (!(sv->track)) {
3038 sv->check.health = sv->check.rise + sv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02003039 srv_set_stopping(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01003040 altered_servers++;
3041 total_servers++;
3042 }
3043 break;
3044 case ST_ADM_ACTION_HDOWN:
3045 if (!(sv->track)) {
3046 sv->check.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02003047 srv_set_stopped(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01003048 altered_servers++;
3049 total_servers++;
3050 }
3051 break;
3052 case ST_ADM_ACTION_DAGENT:
3053 if (sv->agent.state & CHK_ST_CONFIGURED) {
3054 sv->agent.state &= ~CHK_ST_ENABLED;
3055 altered_servers++;
3056 total_servers++;
3057 }
3058 break;
3059 case ST_ADM_ACTION_EAGENT:
3060 if (sv->agent.state & CHK_ST_CONFIGURED) {
3061 sv->agent.state |= CHK_ST_ENABLED;
3062 altered_servers++;
3063 total_servers++;
3064 }
3065 break;
3066 case ST_ADM_ACTION_ARUNN:
3067 if (sv->agent.state & CHK_ST_ENABLED) {
3068 sv->agent.health = sv->agent.rise + sv->agent.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02003069 srv_set_running(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01003070 altered_servers++;
3071 total_servers++;
3072 }
3073 break;
3074 case ST_ADM_ACTION_ADOWN:
3075 if (sv->agent.state & CHK_ST_ENABLED) {
3076 sv->agent.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02003077 srv_set_stopped(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01003078 altered_servers++;
3079 total_servers++;
3080 }
3081 break;
3082 case ST_ADM_ACTION_READY:
3083 srv_adm_set_ready(sv);
3084 altered_servers++;
3085 total_servers++;
3086 break;
3087 case ST_ADM_ACTION_DRAIN:
3088 srv_adm_set_drain(sv);
3089 altered_servers++;
3090 total_servers++;
3091 break;
3092 case ST_ADM_ACTION_MAINT:
3093 srv_adm_set_maint(sv);
3094 altered_servers++;
3095 total_servers++;
3096 break;
3097 case ST_ADM_ACTION_SHUTDOWN:
3098 if (px->state != PR_STSTOPPED) {
Willy Tarreauaf7ea812019-11-14 16:42:04 +01003099 srv_shutdown_streams(sv, SF_ERR_KILLED);
William Lallemand74c24fb2016-11-21 17:18:36 +01003100 altered_servers++;
3101 total_servers++;
3102 }
3103 break;
3104 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003105 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
William Lallemand74c24fb2016-11-21 17:18:36 +01003106 } else {
3107 /* the server name is unknown or ambiguous (duplicate names) */
3108 total_servers++;
3109 }
3110 }
3111 if (reprocess && px && action) {
3112 /* Now, we know the backend and the action chosen by the user.
3113 * We can safely restart from the first server parameter
3114 * to reprocess them
3115 */
3116 cur_param = st_cur_param;
3117 next_param = st_next_param;
3118 reprocess = 0;
3119 goto reprocess_servers;
3120 }
3121
3122 next_param = cur_param;
3123 }
3124 }
3125
3126 if (total_servers == 0) {
3127 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
3128 }
3129 else if (altered_servers == 0) {
3130 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
3131 }
3132 else if (altered_servers == total_servers) {
3133 appctx->ctx.stats.st_code = STAT_STATUS_DONE;
3134 }
3135 else {
3136 appctx->ctx.stats.st_code = STAT_STATUS_PART;
3137 }
3138 out:
3139 return 1;
Christopher Faulet2f9a41d2019-02-27 15:30:57 +01003140 wait:
3141 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
3142 return 0;
Christopher Fauletef779222018-10-31 08:47:01 +01003143}
3144
3145
Christopher Fauletb7f88902019-07-15 21:56:43 +02003146static int stats_send_http_headers(struct stream_interface *si, struct htx *htx)
Christopher Fauletef779222018-10-31 08:47:01 +01003147{
3148 struct stream *s = si_strm(si);
3149 struct uri_auth *uri = s->be->uri_auth;
3150 struct appctx *appctx = __objt_appctx(si->end);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003151 struct htx_sl *sl;
3152 unsigned int flags;
Christopher Fauletef779222018-10-31 08:47:01 +01003153
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003154 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);
3155 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), ist("200"), ist("OK"));
3156 if (!sl)
Christopher Fauletef779222018-10-31 08:47:01 +01003157 goto full;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003158 sl->info.res.status = 200;
Christopher Fauletef779222018-10-31 08:47:01 +01003159
Christopher Fauletb829f4c2019-03-29 16:13:55 +01003160 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")))
Christopher Fauletef779222018-10-31 08:47:01 +01003161 goto full;
3162 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
3163 if (!htx_add_header(htx, ist("Content-Type"), ist("text/html")))
3164 goto full;
3165 }
Christopher Faulet6338a082019-09-09 15:50:54 +02003166 else if (appctx->ctx.stats.flags & (STAT_FMT_JSON|STAT_JSON_SCHM)) {
3167 if (!htx_add_header(htx, ist("Content-Type"), ist("application/json")))
3168 goto full;
3169 }
Christopher Fauletef779222018-10-31 08:47:01 +01003170 else {
3171 if (!htx_add_header(htx, ist("Content-Type"), ist("text/plain")))
3172 goto full;
3173 }
3174
3175 if (uri->refresh > 0 && !(appctx->ctx.stats.flags & STAT_NO_REFRESH)) {
3176 const char *refresh = U2A(uri->refresh);
3177 if (!htx_add_header(htx, ist("Refresh"), ist2(refresh, strlen(refresh))))
3178 goto full;
3179 }
3180
3181 if (appctx->ctx.stats.flags & STAT_CHUNKED) {
3182 if (!htx_add_header(htx, ist("Transfer-Encoding"), ist("chunked")))
3183 goto full;
3184 }
3185
3186 if (!htx_add_endof(htx, HTX_BLK_EOH))
3187 goto full;
3188
Christopher Faulet1e2d6362019-02-27 16:28:48 +01003189 channel_add_input(&s->res, htx->data);
Christopher Fauletef779222018-10-31 08:47:01 +01003190 return 1;
3191
3192 full:
3193 htx_reset(htx);
3194 si_rx_room_blk(si);
3195 return 0;
William Lallemand74c24fb2016-11-21 17:18:36 +01003196}
3197
Christopher Fauletef779222018-10-31 08:47:01 +01003198
Christopher Fauletb7f88902019-07-15 21:56:43 +02003199static int stats_send_http_redirect(struct stream_interface *si, struct htx *htx)
Christopher Fauletef779222018-10-31 08:47:01 +01003200{
3201 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
3202 struct stream *s = si_strm(si);
3203 struct uri_auth *uri = s->be->uri_auth;
3204 struct appctx *appctx = __objt_appctx(si->end);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003205 struct htx_sl *sl;
3206 unsigned int flags;
Christopher Fauletef779222018-10-31 08:47:01 +01003207
3208 /* scope_txt = search pattern + search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
3209 scope_txt[0] = 0;
3210 if (appctx->ctx.stats.scope_len) {
Christopher Fauleted7a0662019-01-14 11:07:34 +01003211 const char *scope_ptr = stats_scope_ptr(appctx, si);
3212
Christopher Fauletef779222018-10-31 08:47:01 +01003213 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Christopher Fauleted7a0662019-01-14 11:07:34 +01003214 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), scope_ptr, appctx->ctx.stats.scope_len);
Christopher Fauletef779222018-10-31 08:47:01 +01003215 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
3216 }
3217
3218 /* We don't want to land on the posted stats page because a refresh will
3219 * repost the data. We don't want this to happen on accident so we redirect
3220 * the browse to the stats page with a GET.
3221 */
3222 chunk_printf(&trash, "%s;st=%s%s%s%s",
3223 uri->uri_prefix,
3224 ((appctx->ctx.stats.st_code > STAT_STATUS_INIT) &&
3225 (appctx->ctx.stats.st_code < STAT_STATUS_SIZE) &&
3226 stat_status_codes[appctx->ctx.stats.st_code]) ?
3227 stat_status_codes[appctx->ctx.stats.st_code] :
3228 stat_status_codes[STAT_STATUS_UNKN],
3229 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3230 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
3231 scope_txt);
3232
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003233 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_CHNK);
3234 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), ist("303"), ist("See Other"));
3235 if (!sl)
Christopher Fauletef779222018-10-31 08:47:01 +01003236 goto full;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003237 sl->info.res.status = 303;
Christopher Fauletef779222018-10-31 08:47:01 +01003238
3239 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")) ||
Christopher Fauletef779222018-10-31 08:47:01 +01003240 !htx_add_header(htx, ist("Content-Type"), ist("text/plain")) ||
3241 !htx_add_header(htx, ist("Content-Length"), ist("0")) ||
3242 !htx_add_header(htx, ist("Location"), ist2(trash.area, trash.data)))
3243 goto full;
3244
3245 if (!htx_add_endof(htx, HTX_BLK_EOH))
3246 goto full;
3247
Christopher Faulet1e2d6362019-02-27 16:28:48 +01003248 channel_add_input(&s->res, htx->data);
Christopher Fauletef779222018-10-31 08:47:01 +01003249 return 1;
3250
3251full:
3252 htx_reset(htx);
3253 si_rx_room_blk(si);
3254 return 0;
3255}
William Lallemand74c24fb2016-11-21 17:18:36 +01003256
Simon Horman05ee2132017-01-04 09:37:25 +01003257
William Lallemand74c24fb2016-11-21 17:18:36 +01003258/* This I/O handler runs as an applet embedded in a stream interface. It is
3259 * used to send HTTP stats over a TCP socket. The mechanism is very simple.
3260 * appctx->st0 contains the operation in progress (dump, done). The handler
3261 * automatically unregisters itself once transfer is complete.
3262 */
Christopher Fauletb7f88902019-07-15 21:56:43 +02003263static void http_stats_io_handler(struct appctx *appctx)
Christopher Fauletef779222018-10-31 08:47:01 +01003264{
3265 struct stream_interface *si = appctx->owner;
3266 struct stream *s = si_strm(si);
3267 struct channel *req = si_oc(si);
3268 struct channel *res = si_ic(si);
3269 struct htx *req_htx, *res_htx;
3270
3271 res_htx = htx_from_buf(&res->buf);
3272
3273 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
3274 goto out;
3275
3276 /* Check if the input buffer is avalaible. */
3277 if (!b_size(&res->buf)) {
3278 si_rx_room_blk(si);
3279 goto out;
3280 }
3281
3282 /* check that the output is not closed */
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003283 if (res->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_SHUTR))
3284 appctx->st0 = STAT_HTTP_END;
Christopher Fauletef779222018-10-31 08:47:01 +01003285
3286 /* all states are processed in sequence */
3287 if (appctx->st0 == STAT_HTTP_HEAD) {
Christopher Fauletb7f88902019-07-15 21:56:43 +02003288 if (stats_send_http_headers(si, res_htx)) {
Christopher Fauletef779222018-10-31 08:47:01 +01003289 if (s->txn->meth == HTTP_METH_HEAD)
3290 appctx->st0 = STAT_HTTP_DONE;
3291 else
3292 appctx->st0 = STAT_HTTP_DUMP;
3293 }
3294 }
3295
3296 if (appctx->st0 == STAT_HTTP_DUMP) {
3297 if (stats_dump_stat_to_buffer(si, res_htx, s->be->uri_auth))
3298 appctx->st0 = STAT_HTTP_DONE;
3299 }
3300
3301 if (appctx->st0 == STAT_HTTP_POST) {
3302 if (stats_process_http_post(si))
3303 appctx->st0 = STAT_HTTP_LAST;
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003304 else if (req->flags & CF_SHUTR)
Christopher Fauletef779222018-10-31 08:47:01 +01003305 appctx->st0 = STAT_HTTP_DONE;
3306 }
3307
3308 if (appctx->st0 == STAT_HTTP_LAST) {
Christopher Fauletb7f88902019-07-15 21:56:43 +02003309 if (stats_send_http_redirect(si, res_htx))
Christopher Fauletef779222018-10-31 08:47:01 +01003310 appctx->st0 = STAT_HTTP_DONE;
3311 }
3312
3313 if (appctx->st0 == STAT_HTTP_DONE) {
Christopher Faulet54b5e212019-06-04 10:08:28 +02003314 /* Don't add TLR because mux-h1 will take care of it */
Christopher Fauletef779222018-10-31 08:47:01 +01003315 if (!htx_add_endof(res_htx, HTX_BLK_EOM)) {
3316 si_rx_room_blk(si);
3317 goto out;
3318 }
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003319 channel_add_input(&s->res, 1);
3320 appctx->st0 = STAT_HTTP_END;
Christopher Fauletef779222018-10-31 08:47:01 +01003321 }
3322
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003323 if (appctx->st0 == STAT_HTTP_END) {
3324 if (!(res->flags & CF_SHUTR)) {
Christopher Fauletef779222018-10-31 08:47:01 +01003325 res->flags |= CF_READ_NULL;
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003326 si_shutr(si);
3327 }
3328
3329 /* eat the whole request */
3330 if (co_data(req)) {
3331 req_htx = htx_from_buf(&req->buf);
3332 co_htx_skip(req, req_htx, co_data(req));
3333 htx_to_buf(req_htx, &req->buf);
Christopher Fauletef779222018-10-31 08:47:01 +01003334 }
3335 }
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003336
Christopher Fauletef779222018-10-31 08:47:01 +01003337 out:
3338 /* we have left the request in the buffer for the case where we
3339 * process a POST, and this automatically re-enables activity on
3340 * read. It's better to indicate that we want to stop reading when
3341 * we're sending, so that we know there's at most one direction
3342 * deciding to wake the applet up. It saves it from looping when
3343 * emitting large blocks into small TCP windows.
3344 */
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003345 htx_to_buf(res_htx, &res->buf);
3346 if (!channel_is_empty(res))
Christopher Fauletef779222018-10-31 08:47:01 +01003347 si_stop_get(si);
Christopher Fauletef779222018-10-31 08:47:01 +01003348}
3349
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003350/* Dump all fields from <info> into <out> using the "show info" format (name: value) */
Willy Tarreau83061a82018-07-13 11:56:34 +02003351static int stats_dump_info_fields(struct buffer *out,
Willy Tarreau43241ff2019-10-09 11:27:51 +02003352 const struct field *info, unsigned int flags)
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003353{
3354 int field;
3355
3356 for (field = 0; field < INF_TOTAL_FIELDS; field++) {
3357 if (!field_format(info, field))
3358 continue;
3359
Willy Tarreaueaa55372019-10-09 07:39:11 +02003360 if (!chunk_appendf(out, "%s: ", info_fields[field].name))
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003361 return 0;
3362 if (!stats_emit_raw_data_field(out, &info[field]))
3363 return 0;
Willy Tarreau6b19b142019-10-09 15:44:21 +02003364 if ((flags & STAT_SHOW_FDESC) && !chunk_appendf(out, ":\"%s\"", info_fields[field].desc))
3365 return 0;
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003366 if (!chunk_strcat(out, "\n"))
3367 return 0;
3368 }
3369 return 1;
3370}
3371
3372/* Dump all fields from <info> into <out> using the "show info typed" format */
Willy Tarreau83061a82018-07-13 11:56:34 +02003373static int stats_dump_typed_info_fields(struct buffer *out,
Willy Tarreau43241ff2019-10-09 11:27:51 +02003374 const struct field *info, unsigned int flags)
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003375{
3376 int field;
3377
3378 for (field = 0; field < INF_TOTAL_FIELDS; field++) {
3379 if (!field_format(info, field))
3380 continue;
3381
Willy Tarreaueaa55372019-10-09 07:39:11 +02003382 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 +01003383 return 0;
3384 if (!stats_emit_field_tags(out, &info[field], ':'))
3385 return 0;
3386 if (!stats_emit_typed_data_field(out, &info[field]))
3387 return 0;
Willy Tarreau6b19b142019-10-09 15:44:21 +02003388 if ((flags & STAT_SHOW_FDESC) && !chunk_appendf(out, ":\"%s\"", info_fields[field].desc))
3389 return 0;
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003390 if (!chunk_strcat(out, "\n"))
3391 return 0;
3392 }
3393 return 1;
3394}
3395
3396/* Fill <info> with HAProxy global info. <info> is preallocated
3397 * array of length <len>. The length of the aray must be
3398 * INF_TOTAL_FIELDS. If this length is less then this value, the
3399 * function returns 0, otherwise, it returns 1.
3400 */
3401int stats_fill_info(struct field *info, int len)
3402{
3403 unsigned int up = (now.tv_sec - start_date.tv_sec);
Willy Tarreau83061a82018-07-13 11:56:34 +02003404 struct buffer *out = get_trash_chunk();
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003405
3406#ifdef USE_OPENSSL
3407 int ssl_sess_rate = read_freq_ctr(&global.ssl_per_sec);
3408 int ssl_key_rate = read_freq_ctr(&global.ssl_fe_keys_per_sec);
3409 int ssl_reuse = 0;
3410
3411 if (ssl_key_rate < ssl_sess_rate) {
3412 /* count the ssl reuse ratio and avoid overflows in both directions */
3413 ssl_reuse = 100 - (100 * ssl_key_rate + (ssl_sess_rate - 1) / 2) / ssl_sess_rate;
3414 }
3415#endif
3416
3417 if (len < INF_TOTAL_FIELDS)
3418 return 0;
3419
3420 chunk_reset(out);
3421 memset(info, 0, sizeof(*info) * len);
3422
3423 info[INF_NAME] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, PRODUCT_NAME);
Willy Tarreau909b9d82019-01-04 18:20:32 +01003424 info[INF_VERSION] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, haproxy_version);
3425 info[INF_RELEASE_DATE] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, haproxy_date);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003426
Yves Lafon95317282018-02-26 11:10:37 +01003427 info[INF_NBTHREAD] = mkf_u32(FO_CONFIG|FS_SERVICE, global.nbthread);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003428 info[INF_NBPROC] = mkf_u32(FO_CONFIG|FS_SERVICE, global.nbproc);
3429 info[INF_PROCESS_NUM] = mkf_u32(FO_KEY, relative_pid);
3430 info[INF_PID] = mkf_u32(FO_STATUS, pid);
3431
3432 info[INF_UPTIME] = mkf_str(FN_DURATION, chunk_newstr(out));
3433 chunk_appendf(out, "%ud %uh%02um%02us", up / 86400, (up % 86400) / 3600, (up % 3600) / 60, (up % 60));
3434
3435 info[INF_UPTIME_SEC] = mkf_u32(FN_DURATION, up);
3436 info[INF_MEMMAX_MB] = mkf_u32(FO_CONFIG|FN_LIMIT, global.rlimit_memmax);
3437 info[INF_POOL_ALLOC_MB] = mkf_u32(0, (unsigned)(pool_total_allocated() / 1048576L));
3438 info[INF_POOL_USED_MB] = mkf_u32(0, (unsigned)(pool_total_used() / 1048576L));
3439 info[INF_POOL_FAILED] = mkf_u32(FN_COUNTER, pool_total_failures());
3440 info[INF_ULIMIT_N] = mkf_u32(FO_CONFIG|FN_LIMIT, global.rlimit_nofile);
3441 info[INF_MAXSOCK] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxsock);
3442 info[INF_MAXCONN] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxconn);
3443 info[INF_HARD_MAXCONN] = mkf_u32(FO_CONFIG|FN_LIMIT, global.hardmaxconn);
3444 info[INF_CURR_CONN] = mkf_u32(0, actconn);
3445 info[INF_CUM_CONN] = mkf_u32(FN_COUNTER, totalconn);
3446 info[INF_CUM_REQ] = mkf_u32(FN_COUNTER, global.req_count);
3447#ifdef USE_OPENSSL
3448 info[INF_MAX_SSL_CONNS] = mkf_u32(FN_MAX, global.maxsslconn);
3449 info[INF_CURR_SSL_CONNS] = mkf_u32(0, sslconns);
3450 info[INF_CUM_SSL_CONNS] = mkf_u32(FN_COUNTER, totalsslconns);
3451#endif
3452 info[INF_MAXPIPES] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxpipes);
3453 info[INF_PIPES_USED] = mkf_u32(0, pipes_used);
3454 info[INF_PIPES_FREE] = mkf_u32(0, pipes_free);
3455 info[INF_CONN_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&global.conn_per_sec));
3456 info[INF_CONN_RATE_LIMIT] = mkf_u32(FO_CONFIG|FN_LIMIT, global.cps_lim);
3457 info[INF_MAX_CONN_RATE] = mkf_u32(FN_MAX, global.cps_max);
3458 info[INF_SESS_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&global.sess_per_sec));
3459 info[INF_SESS_RATE_LIMIT] = mkf_u32(FO_CONFIG|FN_LIMIT, global.sps_lim);
3460 info[INF_MAX_SESS_RATE] = mkf_u32(FN_RATE, global.sps_max);
3461
3462#ifdef USE_OPENSSL
3463 info[INF_SSL_RATE] = mkf_u32(FN_RATE, ssl_sess_rate);
3464 info[INF_SSL_RATE_LIMIT] = mkf_u32(FO_CONFIG|FN_LIMIT, global.ssl_lim);
3465 info[INF_MAX_SSL_RATE] = mkf_u32(FN_MAX, global.ssl_max);
3466 info[INF_SSL_FRONTEND_KEY_RATE] = mkf_u32(0, ssl_key_rate);
3467 info[INF_SSL_FRONTEND_MAX_KEY_RATE] = mkf_u32(FN_MAX, global.ssl_fe_keys_max);
3468 info[INF_SSL_FRONTEND_SESSION_REUSE_PCT] = mkf_u32(0, ssl_reuse);
3469 info[INF_SSL_BACKEND_KEY_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&global.ssl_be_keys_per_sec));
3470 info[INF_SSL_BACKEND_MAX_KEY_RATE] = mkf_u32(FN_MAX, global.ssl_be_keys_max);
3471 info[INF_SSL_CACHE_LOOKUPS] = mkf_u32(FN_COUNTER, global.shctx_lookups);
3472 info[INF_SSL_CACHE_MISSES] = mkf_u32(FN_COUNTER, global.shctx_misses);
3473#endif
3474 info[INF_COMPRESS_BPS_IN] = mkf_u32(FN_RATE, read_freq_ctr(&global.comp_bps_in));
3475 info[INF_COMPRESS_BPS_OUT] = mkf_u32(FN_RATE, read_freq_ctr(&global.comp_bps_out));
3476 info[INF_COMPRESS_BPS_RATE_LIM] = mkf_u32(FO_CONFIG|FN_LIMIT, global.comp_rate_lim);
3477#ifdef USE_ZLIB
3478 info[INF_ZLIB_MEM_USAGE] = mkf_u32(0, zlib_used_memory);
3479 info[INF_MAX_ZLIB_MEM_USAGE] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxzlibmem);
3480#endif
3481 info[INF_TASKS] = mkf_u32(0, nb_tasks_cur);
Christopher Faulet34c5cc92016-12-06 09:15:30 +01003482 info[INF_RUN_QUEUE] = mkf_u32(0, tasks_run_queue_cur);
Willy Tarreau81036f22019-05-20 19:24:50 +02003483 info[INF_IDLE_PCT] = mkf_u32(FN_AVG, ti->idle_pct);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003484 info[INF_NODE] = mkf_str(FO_CONFIG|FN_OUTPUT|FS_SERVICE, global.node);
3485 if (global.desc)
3486 info[INF_DESCRIPTION] = mkf_str(FO_CONFIG|FN_OUTPUT|FS_SERVICE, global.desc);
Willy Tarreau00098ea2018-11-05 14:38:13 +01003487 info[INF_STOPPING] = mkf_u32(0, stopping);
3488 info[INF_JOBS] = mkf_u32(0, jobs);
William Lallemanda7199262018-11-16 16:57:20 +01003489 info[INF_UNSTOPPABLE_JOBS] = mkf_u32(0, unstoppable_jobs);
Willy Tarreau00098ea2018-11-05 14:38:13 +01003490 info[INF_LISTENERS] = mkf_u32(0, listeners);
Willy Tarreau199ad242018-11-05 16:31:22 +01003491 info[INF_ACTIVE_PEERS] = mkf_u32(0, active_peers);
Willy Tarreau2d372c22018-11-05 17:12:27 +01003492 info[INF_CONNECTED_PEERS] = mkf_u32(0, connected_peers);
Willy Tarreau13ef7732018-11-12 07:25:28 +01003493 info[INF_DROPPED_LOGS] = mkf_u32(0, dropped_logs);
Willy Tarreaubeb859a2018-11-22 18:07:59 +01003494 info[INF_BUSY_POLLING] = mkf_u32(0, !!(global.tune.options & GTUNE_BUSY_POLLING));
Baptiste Assmann333939c2019-01-21 08:34:50 +01003495 info[INF_FAILED_RESOLUTIONS] = mkf_u32(0, dns_failed_resolutions);
Willy Tarreau7cf0e452019-05-23 11:39:14 +02003496 info[INF_TOTAL_BYTES_OUT] = mkf_u64(0, global.out_bytes);
3497 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 +02003498 info[INF_DEBUG_COMMANDS_ISSUED] = mkf_u32(0, debug_commands_issued);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003499
3500 return 1;
3501}
3502
3503/* This function dumps information onto the stream interface's read buffer.
3504 * It returns 0 as long as it does not complete, non-zero upon completion.
3505 * No state is used.
3506 */
3507static int stats_dump_info_to_buffer(struct stream_interface *si)
3508{
3509 struct appctx *appctx = __objt_appctx(si->end);
3510
3511 if (!stats_fill_info(info, INF_TOTAL_FIELDS))
3512 return 0;
3513
3514 chunk_reset(&trash);
3515
3516 if (appctx->ctx.stats.flags & STAT_FMT_TYPED)
Willy Tarreau43241ff2019-10-09 11:27:51 +02003517 stats_dump_typed_info_fields(&trash, info, appctx->ctx.stats.flags);
Simon Horman05ee2132017-01-04 09:37:25 +01003518 else if (appctx->ctx.stats.flags & STAT_FMT_JSON)
Willy Tarreau43241ff2019-10-09 11:27:51 +02003519 stats_dump_json_info_fields(&trash, info, appctx->ctx.stats.flags);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003520 else
Willy Tarreau43241ff2019-10-09 11:27:51 +02003521 stats_dump_info_fields(&trash, info, appctx->ctx.stats.flags);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003522
Willy Tarreau06d80a92017-10-19 14:32:15 +02003523 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01003524 si_rx_room_blk(si);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003525 return 0;
3526 }
3527
3528 return 1;
3529}
3530
Simon Horman6f6bb382017-01-04 09:37:26 +01003531/* This function dumps the schema onto the stream interface's read buffer.
3532 * It returns 0 as long as it does not complete, non-zero upon completion.
3533 * No state is used.
3534 *
3535 * Integer values bouned to the range [-(2**53)+1, (2**53)-1] as
3536 * per the recommendation for interoperable integers in section 6 of RFC 7159.
3537 */
Willy Tarreau83061a82018-07-13 11:56:34 +02003538static void stats_dump_json_schema(struct buffer *out)
Simon Horman6f6bb382017-01-04 09:37:26 +01003539{
3540
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003541 int old_len = out->data;
Simon Horman6f6bb382017-01-04 09:37:26 +01003542
3543 chunk_strcat(out,
3544 "{"
3545 "\"$schema\":\"http://json-schema.org/draft-04/schema#\","
3546 "\"oneOf\":["
3547 "{"
3548 "\"title\":\"Info\","
3549 "\"type\":\"array\","
3550 "\"items\":{"
3551 "\"properties\":{"
3552 "\"title\":\"InfoItem\","
3553 "\"type\":\"object\","
3554 "\"field\":{\"$ref\":\"#/definitions/field\"},"
3555 "\"processNum\":{\"$ref\":\"#/definitions/processNum\"},"
3556 "\"tags\":{\"$ref\":\"#/definitions/tags\"},"
3557 "\"value\":{\"$ref\":\"#/definitions/typedValue\"}"
3558 "},"
3559 "\"required\":[\"field\",\"processNum\",\"tags\","
3560 "\"value\"]"
3561 "}"
3562 "},"
3563 "{"
3564 "\"title\":\"Stat\","
3565 "\"type\":\"array\","
3566 "\"items\":{"
3567 "\"title\":\"InfoItem\","
3568 "\"type\":\"object\","
3569 "\"properties\":{"
3570 "\"objType\":{"
3571 "\"enum\":[\"Frontend\",\"Backend\",\"Listener\","
3572 "\"Server\",\"Unknown\"]"
3573 "},"
3574 "\"proxyId\":{"
3575 "\"type\":\"integer\","
3576 "\"minimum\":0"
3577 "},"
3578 "\"id\":{"
3579 "\"type\":\"integer\","
3580 "\"minimum\":0"
3581 "},"
3582 "\"field\":{\"$ref\":\"#/definitions/field\"},"
3583 "\"processNum\":{\"$ref\":\"#/definitions/processNum\"},"
3584 "\"tags\":{\"$ref\":\"#/definitions/tags\"},"
3585 "\"typedValue\":{\"$ref\":\"#/definitions/typedValue\"}"
3586 "},"
3587 "\"required\":[\"objType\",\"proxyId\",\"id\","
3588 "\"field\",\"processNum\",\"tags\","
3589 "\"value\"]"
3590 "}"
3591 "},"
3592 "{"
3593 "\"title\":\"Error\","
3594 "\"type\":\"object\","
3595 "\"properties\":{"
3596 "\"errorStr\":{"
3597 "\"type\":\"string\""
3598 "},"
3599 "\"required\":[\"errorStr\"]"
3600 "}"
3601 "}"
3602 "],"
3603 "\"definitions\":{"
3604 "\"field\":{"
3605 "\"type\":\"object\","
3606 "\"pos\":{"
3607 "\"type\":\"integer\","
3608 "\"minimum\":0"
3609 "},"
3610 "\"name\":{"
3611 "\"type\":\"string\""
3612 "},"
3613 "\"required\":[\"pos\",\"name\"]"
3614 "},"
3615 "\"processNum\":{"
3616 "\"type\":\"integer\","
3617 "\"minimum\":1"
3618 "},"
3619 "\"tags\":{"
3620 "\"type\":\"object\","
3621 "\"origin\":{"
3622 "\"type\":\"string\","
3623 "\"enum\":[\"Metric\",\"Status\",\"Key\","
3624 "\"Config\",\"Product\",\"Unknown\"]"
3625 "},"
3626 "\"nature\":{"
3627 "\"type\":\"string\","
3628 "\"enum\":[\"Gauge\",\"Limit\",\"Min\",\"Max\","
3629 "\"Rate\",\"Counter\",\"Duration\","
3630 "\"Age\",\"Time\",\"Name\",\"Output\","
3631 "\"Avg\", \"Unknown\"]"
3632 "},"
3633 "\"scope\":{"
3634 "\"type\":\"string\","
3635 "\"enum\":[\"Cluster\",\"Process\",\"Service\","
3636 "\"System\",\"Unknown\"]"
3637 "},"
3638 "\"required\":[\"origin\",\"nature\",\"scope\"]"
3639 "},"
3640 "\"typedValue\":{"
3641 "\"type\":\"object\","
3642 "\"oneOf\":["
3643 "{\"$ref\":\"#/definitions/typedValue/definitions/s32Value\"},"
3644 "{\"$ref\":\"#/definitions/typedValue/definitions/s64Value\"},"
3645 "{\"$ref\":\"#/definitions/typedValue/definitions/u32Value\"},"
3646 "{\"$ref\":\"#/definitions/typedValue/definitions/u64Value\"},"
3647 "{\"$ref\":\"#/definitions/typedValue/definitions/strValue\"}"
3648 "],"
3649 "\"definitions\":{"
3650 "\"s32Value\":{"
3651 "\"properties\":{"
3652 "\"type\":{"
3653 "\"type\":\"string\","
3654 "\"enum\":[\"s32\"]"
3655 "},"
3656 "\"value\":{"
3657 "\"type\":\"integer\","
3658 "\"minimum\":-2147483648,"
3659 "\"maximum\":2147483647"
3660 "}"
3661 "},"
3662 "\"required\":[\"type\",\"value\"]"
3663 "},"
3664 "\"s64Value\":{"
3665 "\"properties\":{"
3666 "\"type\":{"
3667 "\"type\":\"string\","
3668 "\"enum\":[\"s64\"]"
3669 "},"
3670 "\"value\":{"
3671 "\"type\":\"integer\","
3672 "\"minimum\":-9007199254740991,"
3673 "\"maximum\":9007199254740991"
3674 "}"
3675 "},"
3676 "\"required\":[\"type\",\"value\"]"
3677 "},"
3678 "\"u32Value\":{"
3679 "\"properties\":{"
3680 "\"type\":{"
3681 "\"type\":\"string\","
3682 "\"enum\":[\"u32\"]"
3683 "},"
3684 "\"value\":{"
3685 "\"type\":\"integer\","
3686 "\"minimum\":0,"
3687 "\"maximum\":4294967295"
3688 "}"
3689 "},"
3690 "\"required\":[\"type\",\"value\"]"
3691 "},"
3692 "\"u64Value\":{"
3693 "\"properties\":{"
3694 "\"type\":{"
3695 "\"type\":\"string\","
3696 "\"enum\":[\"u64\"]"
3697 "},"
3698 "\"value\":{"
3699 "\"type\":\"integer\","
3700 "\"minimum\":0,"
3701 "\"maximum\":9007199254740991"
3702 "}"
3703 "},"
3704 "\"required\":[\"type\",\"value\"]"
3705 "},"
3706 "\"strValue\":{"
3707 "\"properties\":{"
3708 "\"type\":{"
3709 "\"type\":\"string\","
3710 "\"enum\":[\"str\"]"
3711 "},"
3712 "\"value\":{\"type\":\"string\"}"
3713 "},"
3714 "\"required\":[\"type\",\"value\"]"
3715 "},"
3716 "\"unknownValue\":{"
3717 "\"properties\":{"
3718 "\"type\":{"
3719 "\"type\":\"integer\","
3720 "\"minimum\":0"
3721 "},"
3722 "\"value\":{"
3723 "\"type\":\"string\","
3724 "\"enum\":[\"unknown\"]"
3725 "}"
3726 "},"
3727 "\"required\":[\"type\",\"value\"]"
3728 "}"
3729 "}"
3730 "}"
3731 "}"
3732 "}");
3733
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003734 if (old_len == out->data) {
Simon Horman6f6bb382017-01-04 09:37:26 +01003735 chunk_reset(out);
3736 chunk_appendf(out,
3737 "{\"errorStr\":\"output buffer too short\"}");
3738 }
3739}
3740
3741/* This function dumps the schema onto the stream interface's read buffer.
3742 * It returns 0 as long as it does not complete, non-zero upon completion.
3743 * No state is used.
3744 */
3745static int stats_dump_json_schema_to_buffer(struct stream_interface *si)
3746{
3747 chunk_reset(&trash);
3748
3749 stats_dump_json_schema(&trash);
3750
Willy Tarreau06d80a92017-10-19 14:32:15 +02003751 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01003752 si_rx_room_blk(si);
Simon Horman6f6bb382017-01-04 09:37:26 +01003753 return 0;
3754 }
3755
3756 return 1;
3757}
3758
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02003759static int cli_parse_clear_counters(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau89d467c2016-11-23 11:02:40 +01003760{
3761 struct proxy *px;
3762 struct server *sv;
3763 struct listener *li;
3764 int clrall = 0;
3765
3766 if (strcmp(args[2], "all") == 0)
3767 clrall = 1;
3768
3769 /* check permissions */
3770 if (!cli_has_level(appctx, ACCESS_LVL_OPER) ||
3771 (clrall && !cli_has_level(appctx, ACCESS_LVL_ADMIN)))
3772 return 1;
3773
Olivier Houchardfbc74e82017-11-24 16:54:05 +01003774 for (px = proxies_list; px; px = px->next) {
Willy Tarreau89d467c2016-11-23 11:02:40 +01003775 if (clrall) {
3776 memset(&px->be_counters, 0, sizeof(px->be_counters));
3777 memset(&px->fe_counters, 0, sizeof(px->fe_counters));
3778 }
3779 else {
3780 px->be_counters.conn_max = 0;
3781 px->be_counters.p.http.rps_max = 0;
3782 px->be_counters.sps_max = 0;
3783 px->be_counters.cps_max = 0;
3784 px->be_counters.nbpend_max = 0;
Christopher Fauletefb41f02019-11-08 14:53:15 +01003785 px->be_counters.qtime_max = 0;
3786 px->be_counters.ctime_max = 0;
3787 px->be_counters.dtime_max = 0;
3788 px->be_counters.ttime_max = 0;
Willy Tarreau89d467c2016-11-23 11:02:40 +01003789
3790 px->fe_counters.conn_max = 0;
3791 px->fe_counters.p.http.rps_max = 0;
3792 px->fe_counters.sps_max = 0;
3793 px->fe_counters.cps_max = 0;
Willy Tarreau89d467c2016-11-23 11:02:40 +01003794 }
3795
3796 for (sv = px->srv; sv; sv = sv->next)
3797 if (clrall)
3798 memset(&sv->counters, 0, sizeof(sv->counters));
3799 else {
3800 sv->counters.cur_sess_max = 0;
3801 sv->counters.nbpend_max = 0;
3802 sv->counters.sps_max = 0;
Christopher Fauletefb41f02019-11-08 14:53:15 +01003803 sv->counters.qtime_max = 0;
3804 sv->counters.ctime_max = 0;
3805 sv->counters.dtime_max = 0;
3806 sv->counters.ttime_max = 0;
Willy Tarreau89d467c2016-11-23 11:02:40 +01003807 }
3808
3809 list_for_each_entry(li, &px->conf.listeners, by_fe)
3810 if (li->counters) {
3811 if (clrall)
3812 memset(li->counters, 0, sizeof(*li->counters));
3813 else
3814 li->counters->conn_max = 0;
3815 }
3816 }
3817
3818 global.cps_max = 0;
3819 global.sps_max = 0;
Olivier Houchard00bc3cb2017-10-17 19:23:25 +02003820 global.ssl_max = 0;
3821 global.ssl_fe_keys_max = 0;
3822 global.ssl_be_keys_max = 0;
Willy Tarreaud80cb4e2018-01-20 19:30:13 +01003823
3824 memset(activity, 0, sizeof(activity));
Willy Tarreau89d467c2016-11-23 11:02:40 +01003825 return 1;
3826}
3827
3828
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02003829static int cli_parse_show_info(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003830{
Willy Tarreau2f397382019-10-09 11:43:59 +02003831 int arg = 2;
3832
Willy Tarreaud25fc792016-12-16 12:33:47 +01003833 appctx->ctx.stats.scope_str = 0;
3834 appctx->ctx.stats.scope_len = 0;
3835 appctx->ctx.stats.flags = 0;
3836
Willy Tarreau2f397382019-10-09 11:43:59 +02003837 while (*args[arg]) {
3838 if (strcmp(args[arg], "typed") == 0)
3839 appctx->ctx.stats.flags = (appctx->ctx.stats.flags & ~STAT_FMT_MASK) | STAT_FMT_TYPED;
3840 else if (strcmp(args[arg], "json") == 0)
3841 appctx->ctx.stats.flags = (appctx->ctx.stats.flags & ~STAT_FMT_MASK) | STAT_FMT_JSON;
3842 else if (strcmp(args[arg], "desc") == 0)
3843 appctx->ctx.stats.flags |= STAT_SHOW_FDESC;
3844 arg++;
3845 }
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003846 return 0;
3847}
3848
3849
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02003850static int cli_parse_show_stat(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau2b812e22016-11-22 16:18:05 +01003851{
Willy Tarreau2f397382019-10-09 11:43:59 +02003852 int arg = 2;
3853
Willy Tarreaud25fc792016-12-16 12:33:47 +01003854 appctx->ctx.stats.scope_str = 0;
3855 appctx->ctx.stats.scope_len = 0;
Willy Tarreau578d6e42019-10-09 11:00:22 +02003856 appctx->ctx.stats.flags = STAT_SHNODE | STAT_SHDESC;
3857
3858 if ((strm_li(si_strm(appctx->owner))->bind_conf->level & ACCESS_LVL_MASK) >= ACCESS_LVL_OPER)
3859 appctx->ctx.stats.flags |= STAT_SHLGNDS;
Willy Tarreaud25fc792016-12-16 12:33:47 +01003860
Willy Tarreau2f397382019-10-09 11:43:59 +02003861 if (*args[arg] && *args[arg+1] && *args[arg+2]) {
Willy Tarreaua1b1ed52016-11-25 08:50:58 +01003862 struct proxy *px;
3863
Willy Tarreau2f397382019-10-09 11:43:59 +02003864 px = proxy_find_by_name(args[arg], 0, 0);
Willy Tarreaua1b1ed52016-11-25 08:50:58 +01003865 if (px)
3866 appctx->ctx.stats.iid = px->uuid;
3867 else
Willy Tarreau2f397382019-10-09 11:43:59 +02003868 appctx->ctx.stats.iid = atoi(args[arg]);
Willy Tarreaua1b1ed52016-11-25 08:50:58 +01003869
Willy Tarreau9d008692019-08-09 11:21:01 +02003870 if (!appctx->ctx.stats.iid)
3871 return cli_err(appctx, "No such proxy.\n");
Willy Tarreaua1b1ed52016-11-25 08:50:58 +01003872
Willy Tarreau2b812e22016-11-22 16:18:05 +01003873 appctx->ctx.stats.flags |= STAT_BOUND;
Willy Tarreau2f397382019-10-09 11:43:59 +02003874 appctx->ctx.stats.type = atoi(args[arg+1]);
3875 appctx->ctx.stats.sid = atoi(args[arg+2]);
3876 arg += 3;
3877 }
3878
3879 while (*args[arg]) {
3880 if (strcmp(args[arg], "typed") == 0)
3881 appctx->ctx.stats.flags = (appctx->ctx.stats.flags & ~STAT_FMT_MASK) | STAT_FMT_TYPED;
3882 else if (strcmp(args[arg], "json") == 0)
3883 appctx->ctx.stats.flags = (appctx->ctx.stats.flags & ~STAT_FMT_MASK) | STAT_FMT_JSON;
3884 else if (strcmp(args[arg], "desc") == 0)
3885 appctx->ctx.stats.flags |= STAT_SHOW_FDESC;
3886 arg++;
Willy Tarreau2b812e22016-11-22 16:18:05 +01003887 }
Willy Tarreau2b812e22016-11-22 16:18:05 +01003888
Willy Tarreau2b812e22016-11-22 16:18:05 +01003889 return 0;
3890}
3891
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003892static int cli_io_handler_dump_info(struct appctx *appctx)
3893{
3894 return stats_dump_info_to_buffer(appctx->owner);
3895}
3896
Willy Tarreau2b812e22016-11-22 16:18:05 +01003897/* This I/O handler runs as an applet embedded in a stream interface. It is
3898 * used to send raw stats over a socket.
3899 */
3900static int cli_io_handler_dump_stat(struct appctx *appctx)
3901{
Christopher Fauletef779222018-10-31 08:47:01 +01003902 return stats_dump_stat_to_buffer(appctx->owner, NULL, NULL);
Willy Tarreau2b812e22016-11-22 16:18:05 +01003903}
3904
Simon Horman6f6bb382017-01-04 09:37:26 +01003905static int cli_io_handler_dump_json_schema(struct appctx *appctx)
3906{
3907 return stats_dump_json_schema_to_buffer(appctx->owner);
3908}
3909
Willy Tarreau2b812e22016-11-22 16:18:05 +01003910/* register cli keywords */
3911static struct cli_kw_list cli_kws = {{ },{
Willy Tarreau89d467c2016-11-23 11:02:40 +01003912 { { "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 +02003913 { { "show", "info", NULL }, "show info : report information about the running process [desc|json|typed]*", cli_parse_show_info, cli_io_handler_dump_info, NULL },
3914 { { "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 +01003915 { { "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 +01003916 {{},}
3917}};
3918
Willy Tarreau0108d902018-11-25 19:14:37 +01003919INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
3920
William Lallemand74c24fb2016-11-21 17:18:36 +01003921struct applet http_stats_applet = {
3922 .obj_type = OBJ_TYPE_APPLET,
3923 .name = "<STATS>", /* used for logging */
3924 .fct = http_stats_io_handler,
3925 .release = NULL,
3926};
3927
William Lallemand74c24fb2016-11-21 17:18:36 +01003928/*
3929 * Local variables:
3930 * c-indent-level: 8
3931 * c-basic-offset: 8
3932 * End:
3933 */