blob: 79fff90625459542963af349470eeeab8cfc2e2f [file] [log] [blame]
William Lallemand74c24fb2016-11-21 17:18:36 +01001/*
2 * Functions dedicated to statistics output and the stats socket
3 *
4 * Copyright 2000-2012 Willy Tarreau <w@1wt.eu>
5 * Copyright 2007-2009 Krzysztof Piotr Oledzki <ole@ans.pl>
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version
10 * 2 of the License, or (at your option) any later version.
11 *
12 */
13
14#include <ctype.h>
15#include <errno.h>
16#include <fcntl.h>
17#include <stdio.h>
18#include <stdlib.h>
19#include <string.h>
20#include <pwd.h>
21#include <grp.h>
22
23#include <sys/socket.h>
24#include <sys/stat.h>
25#include <sys/types.h>
26
Willy Tarreau4c7e4b72020-05-27 12:58:42 +020027#include <haproxy/api.h>
William Lallemand74c24fb2016-11-21 17:18:36 +010028#include <common/cfgparse.h>
Willy Tarreau0a3bd392020-06-04 08:52:38 +020029#include <haproxy/compression.h>
Willy Tarreau2a83d602020-05-27 16:58:08 +020030#include <haproxy/debug.h>
Willy Tarreaueb92deb2020-06-04 10:53:16 +020031#include <haproxy/dns.h>
Willy Tarreau762d7a52020-06-04 11:23:07 +020032#include <haproxy/frontend.h>
Willy Tarreaucd72d8c2020-06-02 19:11:26 +020033#include <haproxy/http.h>
Willy Tarreau87735332020-06-04 09:08:41 +020034#include <haproxy/http_htx.h>
Willy Tarreau16f958c2020-06-03 08:44:35 +020035#include <haproxy/htx.h>
Willy Tarreaud0ef4392020-06-02 09:38:52 +020036#include <haproxy/pool.h>
Willy Tarreau853b2972020-05-27 18:01:47 +020037#include <haproxy/list.h>
Willy Tarreau48fbcae2020-06-03 18:09:46 +020038#include <haproxy/tools.h>
Willy Tarreauc2f7c582020-06-02 18:15:32 +020039#include <haproxy/ticks.h>
Willy Tarreau92b4f132020-06-01 11:05:15 +020040#include <haproxy/time.h>
William Lallemand74c24fb2016-11-21 17:18:36 +010041#include <common/uri_auth.h>
Willy Tarreaud6788052020-05-27 15:59:00 +020042#include <haproxy/version.h>
Willy Tarreau8d366972020-05-27 16:10:29 +020043#include <haproxy/base64.h>
William Lallemand74c24fb2016-11-21 17:18:36 +010044
45#include <types/applet.h>
Willy Tarreau2b812e22016-11-22 16:18:05 +010046#include <types/cli.h>
William Lallemand74c24fb2016-11-21 17:18:36 +010047#include <types/global.h>
William Lallemand9ed62032016-11-21 17:49:11 +010048#include <types/stats.h>
William Lallemand74c24fb2016-11-21 17:18:36 +010049
50#include <proto/backend.h>
51#include <proto/channel.h>
52#include <proto/checks.h>
Willy Tarreau2b812e22016-11-22 16:18:05 +010053#include <proto/cli.h>
William Lallemand9ed62032016-11-21 17:49:11 +010054#include <proto/stats.h>
Willy Tarreau0f6ffd62020-06-03 19:33:00 +020055#include <haproxy/fd.h>
Willy Tarreau66347942020-06-01 12:18:08 +020056#include <haproxy/freq_ctr.h>
William Lallemand74c24fb2016-11-21 17:18:36 +010057#include <proto/log.h>
58#include <proto/pattern.h>
Willy Tarreau551271d2020-06-04 08:32:23 +020059#include <haproxy/pipe.h>
William Lallemand74c24fb2016-11-21 17:18:36 +010060#include <proto/listener.h>
61#include <proto/map.h>
William Lallemand74c24fb2016-11-21 17:18:36 +010062#include <proto/proxy.h>
63#include <proto/sample.h>
64#include <proto/session.h>
Willy Tarreauc125cef2019-05-10 09:58:43 +020065#include <proto/ssl_sock.h>
William Lallemand74c24fb2016-11-21 17:18:36 +010066#include <proto/stream.h>
67#include <proto/server.h>
William Lallemand74c24fb2016-11-21 17:18:36 +010068#include <proto/stream_interface.h>
69#include <proto/task.h>
70
Christopher Faulet2b9b6782019-02-27 16:42:58 +010071/* status codes available for the stats admin page (strictly 4 chars length) */
72const char *stat_status_codes[STAT_STATUS_SIZE] = {
73 [STAT_STATUS_DENY] = "DENY",
74 [STAT_STATUS_DONE] = "DONE",
75 [STAT_STATUS_ERRP] = "ERRP",
76 [STAT_STATUS_EXCD] = "EXCD",
77 [STAT_STATUS_NONE] = "NONE",
78 [STAT_STATUS_PART] = "PART",
79 [STAT_STATUS_UNKN] = "UNKN",
80 [STAT_STATUS_IVAL] = "IVAL",
81};
82
Willy Tarreau0baac8c2016-11-22 16:36:53 +010083/* These are the field names for each INF_* field position. Please pay attention
84 * to always use the exact same name except that the strings for new names must
85 * be lower case or CamelCase while the enum entries must be upper case.
86 */
Willy Tarreaueaa55372019-10-09 07:39:11 +020087const struct name_desc info_fields[INF_TOTAL_FIELDS] = {
Willy Tarreau6d4897e2019-10-11 16:31:46 +020088 [INF_NAME] = { .name = "Name", .desc = "Product name" },
89 [INF_VERSION] = { .name = "Version", .desc = "Product version" },
90 [INF_RELEASE_DATE] = { .name = "Release_date", .desc = "Date of latest source code update" },
91 [INF_NBTHREAD] = { .name = "Nbthread", .desc = "Number of started threads (global.nbthread)" },
92 [INF_NBPROC] = { .name = "Nbproc", .desc = "Number of started worker processes (global.nbproc)" },
93 [INF_PROCESS_NUM] = { .name = "Process_num", .desc = "Relative worker process number (1..Nbproc)" },
94 [INF_PID] = { .name = "Pid", .desc = "This worker process identifier for the system" },
95 [INF_UPTIME] = { .name = "Uptime", .desc = "How long ago this worker process was started (days+hours+minutes+seconds)" },
96 [INF_UPTIME_SEC] = { .name = "Uptime_sec", .desc = "How long ago this worker process was started (seconds)" },
97 [INF_MEMMAX_MB] = { .name = "Memmax_MB", .desc = "Worker process's hard limit on memory usage in MB (-m on command line)" },
98 [INF_POOL_ALLOC_MB] = { .name = "PoolAlloc_MB", .desc = "Amount of memory allocated in pools (in MB)" },
99 [INF_POOL_USED_MB] = { .name = "PoolUsed_MB", .desc = "Amount of pool memory currently used (in MB)" },
100 [INF_POOL_FAILED] = { .name = "PoolFailed", .desc = "Number of failed pool allocations since this worker was started" },
101 [INF_ULIMIT_N] = { .name = "Ulimit-n", .desc = "Hard limit on the number of per-process file descriptors" },
102 [INF_MAXSOCK] = { .name = "Maxsock", .desc = "Hard limit on the number of per-process sockets" },
103 [INF_MAXCONN] = { .name = "Maxconn", .desc = "Hard limit on the number of per-process connections (configured or imposed by Ulimit-n)" },
104 [INF_HARD_MAXCONN] = { .name = "Hard_maxconn", .desc = "Hard limit on the number of per-process connections (imposed by Memmax_MB or Ulimit-n)" },
105 [INF_CURR_CONN] = { .name = "CurrConns", .desc = "Current number of connections on this worker process" },
106 [INF_CUM_CONN] = { .name = "CumConns", .desc = "Total number of connections on this worker process since started" },
107 [INF_CUM_REQ] = { .name = "CumReq", .desc = "Total number of requests on this worker process since started" },
108 [INF_MAX_SSL_CONNS] = { .name = "MaxSslConns", .desc = "Hard limit on the number of per-process SSL endpoints (front+back), 0=unlimited" },
109 [INF_CURR_SSL_CONNS] = { .name = "CurrSslConns", .desc = "Current number of SSL endpoints on this worker process (front+back)" },
110 [INF_CUM_SSL_CONNS] = { .name = "CumSslConns", .desc = "Total number of SSL endpoints on this worker process since started (front+back)" },
111 [INF_MAXPIPES] = { .name = "Maxpipes", .desc = "Hard limit on the number of pipes for splicing, 0=unlimited" },
112 [INF_PIPES_USED] = { .name = "PipesUsed", .desc = "Current number of pipes in use in this worker process" },
113 [INF_PIPES_FREE] = { .name = "PipesFree", .desc = "Current number of allocated and available pipes in this worker process" },
114 [INF_CONN_RATE] = { .name = "ConnRate", .desc = "Number of front connections created on this worker process over the last second" },
115 [INF_CONN_RATE_LIMIT] = { .name = "ConnRateLimit", .desc = "Hard limit for ConnRate (global.maxconnrate)" },
116 [INF_MAX_CONN_RATE] = { .name = "MaxConnRate", .desc = "Highest ConnRate reached on this worker process since started (in connections per second)" },
117 [INF_SESS_RATE] = { .name = "SessRate", .desc = "Number of sessions created on this worker process over the last second" },
118 [INF_SESS_RATE_LIMIT] = { .name = "SessRateLimit", .desc = "Hard limit for SessRate (global.maxsessrate)" },
119 [INF_MAX_SESS_RATE] = { .name = "MaxSessRate", .desc = "Highest SessRate reached on this worker process since started (in sessions per second)" },
120 [INF_SSL_RATE] = { .name = "SslRate", .desc = "Number of SSL connections created on this worker process over the last second" },
121 [INF_SSL_RATE_LIMIT] = { .name = "SslRateLimit", .desc = "Hard limit for SslRate (global.maxsslrate)" },
122 [INF_MAX_SSL_RATE] = { .name = "MaxSslRate", .desc = "Highest SslRate reached on this worker process since started (in connections per second)" },
123 [INF_SSL_FRONTEND_KEY_RATE] = { .name = "SslFrontendKeyRate", .desc = "Number of SSL keys created on frontends in this worker process over the last second" },
124 [INF_SSL_FRONTEND_MAX_KEY_RATE] = { .name = "SslFrontendMaxKeyRate", .desc = "Highest SslFrontendKeyRate reached on this worker process since started (in SSL keys per second)" },
125 [INF_SSL_FRONTEND_SESSION_REUSE_PCT] = { .name = "SslFrontendSessionReuse_pct", .desc = "Percent of frontend SSL connections which did not require a new key" },
126 [INF_SSL_BACKEND_KEY_RATE] = { .name = "SslBackendKeyRate", .desc = "Number of SSL keys created on backends in this worker process over the last second" },
127 [INF_SSL_BACKEND_MAX_KEY_RATE] = { .name = "SslBackendMaxKeyRate", .desc = "Highest SslBackendKeyRate reached on this worker process since started (in SSL keys per second)" },
128 [INF_SSL_CACHE_LOOKUPS] = { .name = "SslCacheLookups", .desc = "Total number of SSL session ID lookups in the SSL session cache on this worker since started" },
129 [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" },
130 [INF_COMPRESS_BPS_IN] = { .name = "CompressBpsIn", .desc = "Number of bytes submitted to the HTTP compressor in this worker process over the last second" },
131 [INF_COMPRESS_BPS_OUT] = { .name = "CompressBpsOut", .desc = "Number of bytes emitted by the HTTP compressor in this worker process over the last second" },
132 [INF_COMPRESS_BPS_RATE_LIM] = { .name = "CompressBpsRateLim", .desc = "Limit of CompressBpsOut beyond which HTTP compression is automatically disabled" },
133 [INF_ZLIB_MEM_USAGE] = { .name = "ZlibMemUsage", .desc = "Amount of memory currently used by HTTP compression on the current worker process (in bytes)" },
134 [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)" },
135 [INF_TASKS] = { .name = "Tasks", .desc = "Total number of tasks in the current worker process (active + sleeping)" },
136 [INF_RUN_QUEUE] = { .name = "Run_queue", .desc = "Total number of active tasks+tasklets in the current worker process" },
137 [INF_IDLE_PCT] = { .name = "Idle_pct", .desc = "Percentage of last second spent waiting in the current worker thread" },
138 [INF_NODE] = { .name = "node", .desc = "Node name (global.node)" },
139 [INF_DESCRIPTION] = { .name = "description", .desc = "Node description (global.description)" },
140 [INF_STOPPING] = { .name = "Stopping", .desc = "1 if the worker process is currently stopping, otherwise zero" },
141 [INF_JOBS] = { .name = "Jobs", .desc = "Current number of active jobs on the current worker process (frontend connections, master connections, listeners)" },
142 [INF_UNSTOPPABLE_JOBS] = { .name = "Unstoppable Jobs", .desc = "Current number of unstoppable jobs on the current worker process (master connections)" },
143 [INF_LISTENERS] = { .name = "Listeners", .desc = "Current number of active listeners on the current worker process" },
144 [INF_ACTIVE_PEERS] = { .name = "ActivePeers", .desc = "Current number of verified active peers connections on the current worker process" },
145 [INF_CONNECTED_PEERS] = { .name = "ConnectedPeers", .desc = "Current number of peers having passed the connection step on the current worker process" },
146 [INF_DROPPED_LOGS] = { .name = "DroppedLogs", .desc = "Total number of dropped logs for current worker process since started" },
147 [INF_BUSY_POLLING] = { .name = "BusyPolling", .desc = "1 if busy-polling is currently in use on the worker process, otherwise zero (config.busy-polling)" },
148 [INF_FAILED_RESOLUTIONS] = { .name = "FailedResolutions", .desc = "Total number of failed DNS resolutions in current worker process since started" },
149 [INF_TOTAL_BYTES_OUT] = { .name = "TotalBytesOut", .desc = "Total number of bytes emitted by current worker process since started" },
150 [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 +0200151 [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 +0100152};
153
Willy Tarreaueaa55372019-10-09 07:39:11 +0200154const struct name_desc stat_fields[ST_F_TOTAL_FIELDS] = {
Willy Tarreau6d4897e2019-10-11 16:31:46 +0200155 [ST_F_PXNAME] = { .name = "pxname", .desc = "Proxy name" },
156 [ST_F_SVNAME] = { .name = "svname", .desc = "Server name" },
157 [ST_F_QCUR] = { .name = "qcur", .desc = "Current number of connections waiting in the server of backend queue" },
158 [ST_F_QMAX] = { .name = "qmax", .desc = "Highest value of qcur encountered since process started" },
159 [ST_F_SCUR] = { .name = "scur", .desc = "Current number of sessions on the frontend, backend or server" },
160 [ST_F_SMAX] = { .name = "smax", .desc = "Highest value of scur encountered since process started" },
161 [ST_F_SLIM] = { .name = "slim", .desc = "Frontend/listener/server's maxconn, backend's fullconn" },
162 [ST_F_STOT] = { .name = "stot", .desc = "Total number of sessions since process started" },
163 [ST_F_BIN] = { .name = "bin", .desc = "Total number of request bytes since process started" },
164 [ST_F_BOUT] = { .name = "bout", .desc = "Total number of response bytes since process started" },
165 [ST_F_DREQ] = { .name = "dreq", .desc = "Total number of denied requests since process started" },
166 [ST_F_DRESP] = { .name = "dresp", .desc = "Total number of denied responses since process started" },
167 [ST_F_EREQ] = { .name = "ereq", .desc = "Total number of invalid requests since process started" },
168 [ST_F_ECON] = { .name = "econ", .desc = "Total number of failed connections to server since the worker process started" },
169 [ST_F_ERESP] = { .name = "eresp", .desc = "Total number of invalid responses since the worker process started" },
170 [ST_F_WRETR] = { .name = "wretr", .desc = "Total number of server connection retries since the worker process started" },
171 [ST_F_WREDIS] = { .name = "wredis", .desc = "Total number of server redispatches due to connection failures since the worker process started" },
172 [ST_F_STATUS] = { .name = "status", .desc = "Frontend/listen status: OPEN/WAITING/FULL/STOP; backend: UP/DOWN; server: last check status" },
173 [ST_F_WEIGHT] = { .name = "weight", .desc = "Server weight, or sum of active servers' weights for a backend" },
174 [ST_F_ACT] = { .name = "act", .desc = "Total number of active UP servers with a non-zero weight" },
175 [ST_F_BCK] = { .name = "bck", .desc = "Total number of backup UP servers with a non-zero weight" },
176 [ST_F_CHKFAIL] = { .name = "chkfail", .desc = "Total number of failed individual health checks per server/backend, since the worker process started" },
177 [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" },
178 [ST_F_LASTCHG] = { .name = "lastchg", .desc = "How long ago the last server state changed, in seconds" },
179 [ST_F_DOWNTIME] = { .name = "downtime", .desc = "Total time spent in DOWN state, for server or backend" },
180 [ST_F_QLIMIT] = { .name = "qlimit", .desc = "Limit on the number of connections in queue, for servers only (maxqueue argument)" },
181 [ST_F_PID] = { .name = "pid", .desc = "Relative worker process number (1..nbproc)" },
182 [ST_F_IID] = { .name = "iid", .desc = "Frontend or Backend numeric identifier ('id' setting)" },
183 [ST_F_SID] = { .name = "sid", .desc = "Server numeric identifier ('id' setting)" },
184 [ST_F_THROTTLE] = { .name = "throttle", .desc = "Throttling ratio applied to a server's maxconn and weight during the slowstart period (0 to 100%)" },
185 [ST_F_LBTOT] = { .name = "lbtot", .desc = "Total number of requests routed by load balancing since the worker process started (ignores queue pop and stickiness)" },
186 [ST_F_TRACKED] = { .name = "tracked", .desc = "Name of the other server this server tracks for its state" },
187 [ST_F_TYPE] = { .name = "type", .desc = "Type of the object (Listener, Frontend, Backend, Server)" },
188 [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)" },
189 [ST_F_RATE_LIM] = { .name = "rate_lim", .desc = "Limit on the number of sessions accepted in a second (frontend only, 'rate-limit sessions' setting)" },
190 [ST_F_RATE_MAX] = { .name = "rate_max", .desc = "Highest value of 'rate' observed since the worker process started" },
191 [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" },
192 [ST_F_CHECK_CODE] = { .name = "check_code", .desc = "HTTP/SMTP/LDAP status code reported by the latest server health check" },
193 [ST_F_CHECK_DURATION] = { .name = "check_duration", .desc = "Total duration of the latest server health check, in milliseconds" },
194 [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" },
195 [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" },
196 [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" },
197 [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" },
198 [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" },
199 [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)" },
200 [ST_F_HANAFAIL] = { .name = "hanafail", .desc = "Total number of failed checks caused by an 'on-error' directive after an 'observe' condition matched" },
201 [ST_F_REQ_RATE] = { .name = "req_rate", .desc = "Number of HTTP requests processed over the last second on this object" },
202 [ST_F_REQ_RATE_MAX] = { .name = "req_rate_max", .desc = "Highest value of 'req_rate' observed since the worker process started" },
203 [ST_F_REQ_TOT] = { .name = "req_tot", .desc = "Total number of HTTP requests processed by this object since the worker process started" },
204 [ST_F_CLI_ABRT] = { .name = "cli_abrt", .desc = "Total number of requests or connections aborted by the client since the worker process started" },
205 [ST_F_SRV_ABRT] = { .name = "srv_abrt", .desc = "Total number of requests or connections aborted by the server since the worker process started" },
206 [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" },
207 [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" },
208 [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)" },
209 [ST_F_COMP_RSP] = { .name = "comp_rsp", .desc = "Total number of HTTP responses that were compressed for this object since the worker process started" },
210 [ST_F_LASTSESS] = { .name = "lastsess", .desc = "How long ago some traffic was seen on this object on this worker process, in seconds" },
211 [ST_F_LAST_CHK] = { .name = "last_chk", .desc = "Short description of the latest health check report for this server (see also check_desc)" },
212 [ST_F_LAST_AGT] = { .name = "last_agt", .desc = "Short description of the latest agent check report for this server (see also agent_desc)" },
213 [ST_F_QTIME] = { .name = "qtime", .desc = "Time spent in the queue, in milliseconds, averaged over the 1024 last requests (backend/server)" },
214 [ST_F_CTIME] = { .name = "ctime", .desc = "Time spent waiting for a connection to complete, in milliseconds, averaged over the 1024 last requests (backend/server)" },
215 [ST_F_RTIME] = { .name = "rtime", .desc = "Time spent waiting for a server response, in milliseconds, averaged over the 1024 last requests (backend/server)" },
216 [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)" },
217 [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" },
218 [ST_F_AGENT_CODE] = { .name = "agent_code", .desc = "Status code reported by the latest server agent check" },
219 [ST_F_AGENT_DURATION] = { .name = "agent_duration", .desc = "Total duration of the latest server agent check, in milliseconds" },
220 [ST_F_CHECK_DESC] = { .name = "check_desc", .desc = "Textual description of the latest health check report for this server" },
221 [ST_F_AGENT_DESC] = { .name = "agent_desc", .desc = "Textual description of the latest agent check report for this server" },
222 [ST_F_CHECK_RISE] = { .name = "check_rise", .desc = "Number of successful health checks before declaring a server UP (server 'rise' setting)" },
223 [ST_F_CHECK_FALL] = { .name = "check_fall", .desc = "Number of failed health checks before declaring a server DOWN (server 'fall' setting)" },
224 [ST_F_CHECK_HEALTH] = { .name = "check_health", .desc = "Current server health check level (0..fall-1=DOWN, fall..rise-1=UP)" },
225 [ST_F_AGENT_RISE] = { .name = "agent_rise", .desc = "Number of successful agent checks before declaring a server UP (server 'rise' setting)" },
226 [ST_F_AGENT_FALL] = { .name = "agent_fall", .desc = "Number of failed agent checks before declaring a server DOWN (server 'fall' setting)" },
227 [ST_F_AGENT_HEALTH] = { .name = "agent_health", .desc = "Current server agent check level (0..fall-1=DOWN, fall..rise-1=UP)" },
228 [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" },
229 [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" },
230 [ST_F_MODE] = { .name = "mode", .desc = "'mode' setting (tcp/http/health/cli)" },
231 [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" },
232 [ST_F_CONN_RATE] = { .name = "conn_rate", .desc = "Number of new connections accepted over the last second on the frontend for this worker process" },
233 [ST_F_CONN_RATE_MAX] = { .name = "conn_rate_max", .desc = "Highest value of 'conn_rate' observed since the worker process started" },
234 [ST_F_CONN_TOT] = { .name = "conn_tot", .desc = "Total number of new connections accepted on this frontend since the worker process started" },
235 [ST_F_INTERCEPTED] = { .name = "intercepted", .desc = "Total number of HTTP requests intercepted on the frontend (redirects/stats/services) since the worker process started" },
236 [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" },
237 [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" },
238 [ST_F_WREW] = { .name = "wrew", .desc = "Total number of failed HTTP header rewrites since the worker process started" },
239 [ST_F_CONNECT] = { .name = "connect", .desc = "Total number of outgoing connection attempts on this backend/server since the worker process started" },
240 [ST_F_REUSE] = { .name = "reuse", .desc = "Total number of reused connection on this backend/server since the worker process started" },
241 [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" },
242 [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" },
243 [ST_F_SRV_ICUR] = { .name = "srv_icur", .desc = "Current number of idle connections available for reuse on this server" },
244 [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 +0100245 [ST_F_QT_MAX] = { .name = "qtime_max", .desc = "Maximum observed time spent in the queue, in milliseconds (backend/server)" },
246 [ST_F_CT_MAX] = { .name = "ctime_max", .desc = "Maximum observed time spent waiting for a connection to complete, in milliseconds (backend/server)" },
247 [ST_F_RT_MAX] = { .name = "rtime_max", .desc = "Maximum observed time spent waiting for a server response, in milliseconds (backend/server)" },
248 [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 +0100249 [ST_F_EINT] = { .name = "eint", .desc = "Total number of internal errors since process started"},
William Lallemand74c24fb2016-11-21 17:18:36 +0100250};
251
Willy Tarreau0baac8c2016-11-22 16:36:53 +0100252/* one line of info */
Christopher Faulet1bc04c72017-10-29 20:14:08 +0100253static THREAD_LOCAL struct field info[INF_TOTAL_FIELDS];
William Lallemand74c24fb2016-11-21 17:18:36 +0100254/* one line of stats */
Christopher Faulet1bc04c72017-10-29 20:14:08 +0100255static THREAD_LOCAL struct field stats[ST_F_TOTAL_FIELDS];
William Lallemand74c24fb2016-11-21 17:18:36 +0100256
Christopher Faulet6338a082019-09-09 15:50:54 +0200257static void stats_dump_json_schema(struct buffer *out);
William Lallemand74c24fb2016-11-21 17:18:36 +0100258
Christopher Fauletef779222018-10-31 08:47:01 +0100259static int stats_putchk(struct channel *chn, struct htx *htx, struct buffer *chk)
260{
261 if (htx) {
Christopher Faulet69fc88c2019-01-07 14:27:53 +0100262 if (chk->data >= channel_htx_recv_max(chn, htx))
263 return 0;
Willy Tarreau0a7ef022019-05-28 10:30:11 +0200264 if (!htx_add_data_atonce(htx, ist2(chk->area, chk->data)))
Christopher Fauletef779222018-10-31 08:47:01 +0100265 return 0;
Christopher Faulet5adbeeb2019-01-02 14:34:39 +0100266 channel_add_input(chn, chk->data);
Christopher Fauletef779222018-10-31 08:47:01 +0100267 chk->data = 0;
Christopher Fauletef779222018-10-31 08:47:01 +0100268 }
269 else {
270 if (ci_putchk(chn, chk) == -1)
271 return 0;
272 }
273 return 1;
274}
Willy Tarreau0baac8c2016-11-22 16:36:53 +0100275
Christopher Fauleted7a0662019-01-14 11:07:34 +0100276static const char *stats_scope_ptr(struct appctx *appctx, struct stream_interface *si)
277{
Christopher Fauletb7f88902019-07-15 21:56:43 +0200278 struct channel *req = si_oc(si);
279 struct htx *htx = htxbuf(&req->buf);
280 struct htx_blk *blk;
281 struct ist uri;
Christopher Fauleted7a0662019-01-14 11:07:34 +0100282
Christopher Fauletb7f88902019-07-15 21:56:43 +0200283 blk = htx_get_head_blk(htx);
Christopher Fauletea009732019-11-18 15:50:25 +0100284 BUG_ON(!blk || htx_get_blk_type(blk) != HTX_BLK_REQ_SL);
Christopher Fauletb7f88902019-07-15 21:56:43 +0200285 ALREADY_CHECKED(blk);
286 uri = htx_sl_req_uri(htx_get_blk_ptr(htx, blk));
287 return uri.ptr + appctx->ctx.stats.scope_str;
Christopher Fauleted7a0662019-01-14 11:07:34 +0100288}
289
William Lallemand74c24fb2016-11-21 17:18:36 +0100290/*
291 * http_stats_io_handler()
292 * -> stats_dump_stat_to_buffer() // same as above, but used for CSV or HTML
293 * -> stats_dump_csv_header() // emits the CSV headers (same as above)
Simon Horman05ee2132017-01-04 09:37:25 +0100294 * -> stats_dump_json_header() // emits the JSON headers (same as above)
William Lallemand74c24fb2016-11-21 17:18:36 +0100295 * -> stats_dump_html_head() // emits the HTML headers
296 * -> stats_dump_html_info() // emits the equivalent of "show info" at the top
297 * -> stats_dump_proxy_to_buffer() // same as above, valid for CSV and HTML
298 * -> stats_dump_html_px_hdr()
299 * -> stats_dump_fe_stats()
300 * -> stats_dump_li_stats()
301 * -> stats_dump_sv_stats()
302 * -> stats_dump_be_stats()
303 * -> stats_dump_html_px_end()
304 * -> stats_dump_html_end() // emits HTML trailer
Simon Horman05ee2132017-01-04 09:37:25 +0100305 * -> stats_dump_json_end() // emits JSON trailer
William Lallemand74c24fb2016-11-21 17:18:36 +0100306 */
307
308
William Lallemand74c24fb2016-11-21 17:18:36 +0100309/* Dumps the stats CSV header to the trash buffer which. The caller is responsible
310 * for clearing it if needed.
311 * NOTE: Some tools happen to rely on the field position instead of its name,
312 * so please only append new fields at the end, never in the middle.
313 */
314static void stats_dump_csv_header()
315{
316 int field;
317
318 chunk_appendf(&trash, "# ");
319 for (field = 0; field < ST_F_TOTAL_FIELDS; field++)
Willy Tarreaueaa55372019-10-09 07:39:11 +0200320 chunk_appendf(&trash, "%s,", stat_fields[field].name);
William Lallemand74c24fb2016-11-21 17:18:36 +0100321
322 chunk_appendf(&trash, "\n");
323}
324
325
326/* Emits a stats field without any surrounding element and properly encoded to
327 * resist CSV output. Returns non-zero on success, 0 if the buffer is full.
328 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200329int stats_emit_raw_data_field(struct buffer *out, const struct field *f)
William Lallemand74c24fb2016-11-21 17:18:36 +0100330{
331 switch (field_format(f, 0)) {
332 case FF_EMPTY: return 1;
333 case FF_S32: return chunk_appendf(out, "%d", f->u.s32);
334 case FF_U32: return chunk_appendf(out, "%u", f->u.u32);
335 case FF_S64: return chunk_appendf(out, "%lld", (long long)f->u.s64);
336 case FF_U64: return chunk_appendf(out, "%llu", (unsigned long long)f->u.u64);
Christopher Faulet88a0db22019-09-24 16:35:10 +0200337 case FF_FLT: return chunk_appendf(out, "%f", f->u.flt);
William Lallemand74c24fb2016-11-21 17:18:36 +0100338 case FF_STR: return csv_enc_append(field_str(f, 0), 1, out) != NULL;
339 default: return chunk_appendf(out, "[INCORRECT_FIELD_TYPE_%08x]", f->type);
340 }
341}
342
343/* Emits a stats field prefixed with its type. No CSV encoding is prepared, the
344 * output is supposed to be used on its own line. Returns non-zero on success, 0
345 * if the buffer is full.
346 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200347int stats_emit_typed_data_field(struct buffer *out, const struct field *f)
William Lallemand74c24fb2016-11-21 17:18:36 +0100348{
349 switch (field_format(f, 0)) {
350 case FF_EMPTY: return 1;
351 case FF_S32: return chunk_appendf(out, "s32:%d", f->u.s32);
352 case FF_U32: return chunk_appendf(out, "u32:%u", f->u.u32);
353 case FF_S64: return chunk_appendf(out, "s64:%lld", (long long)f->u.s64);
354 case FF_U64: return chunk_appendf(out, "u64:%llu", (unsigned long long)f->u.u64);
Christopher Faulet88a0db22019-09-24 16:35:10 +0200355 case FF_FLT: return chunk_appendf(out, "flt:%f", f->u.flt);
William Lallemand74c24fb2016-11-21 17:18:36 +0100356 case FF_STR: return chunk_appendf(out, "str:%s", field_str(f, 0));
357 default: return chunk_appendf(out, "%08x:?", f->type);
358 }
359}
360
Simon Horman05ee2132017-01-04 09:37:25 +0100361/* Limit JSON integer values to the range [-(2**53)+1, (2**53)-1] as per
362 * the recommendation for interoperable integers in section 6 of RFC 7159.
363 */
364#define JSON_INT_MAX ((1ULL << 53) - 1)
365#define JSON_INT_MIN (0 - JSON_INT_MAX)
366
367/* Emits a stats field value and its type in JSON.
368 * Returns non-zero on success, 0 on error.
369 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200370int stats_emit_json_data_field(struct buffer *out, const struct field *f)
Simon Horman05ee2132017-01-04 09:37:25 +0100371{
372 int old_len;
373 char buf[20];
374 const char *type, *value = buf, *quote = "";
375
376 switch (field_format(f, 0)) {
377 case FF_EMPTY: return 1;
378 case FF_S32: type = "\"s32\"";
379 snprintf(buf, sizeof(buf), "%d", f->u.s32);
380 break;
381 case FF_U32: type = "\"u32\"";
382 snprintf(buf, sizeof(buf), "%u", f->u.u32);
383 break;
384 case FF_S64: type = "\"s64\"";
385 if (f->u.s64 < JSON_INT_MIN || f->u.s64 > JSON_INT_MAX)
386 return 0;
387 type = "\"s64\"";
388 snprintf(buf, sizeof(buf), "%lld", (long long)f->u.s64);
389 break;
390 case FF_U64: if (f->u.u64 > JSON_INT_MAX)
391 return 0;
392 type = "\"u64\"";
393 snprintf(buf, sizeof(buf), "%llu",
394 (unsigned long long) f->u.u64);
Christopher Faulet52c91bb2019-09-28 10:37:31 +0200395 break;
Christopher Faulet88a0db22019-09-24 16:35:10 +0200396 case FF_FLT: type = "\"flt\"";
397 snprintf(buf, sizeof(buf), "%f", f->u.flt);
Simon Horman05ee2132017-01-04 09:37:25 +0100398 break;
399 case FF_STR: type = "\"str\"";
400 value = field_str(f, 0);
401 quote = "\"";
402 break;
403 default: snprintf(buf, sizeof(buf), "%u", f->type);
404 type = buf;
405 value = "unknown";
406 quote = "\"";
407 break;
408 }
409
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200410 old_len = out->data;
Simon Horman05ee2132017-01-04 09:37:25 +0100411 chunk_appendf(out, ",\"value\":{\"type\":%s,\"value\":%s%s%s}",
412 type, quote, value, quote);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200413 return !(old_len == out->data);
Simon Horman05ee2132017-01-04 09:37:25 +0100414}
415
William Lallemand74c24fb2016-11-21 17:18:36 +0100416/* Emits an encoding of the field type on 3 characters followed by a delimiter.
417 * Returns non-zero on success, 0 if the buffer is full.
418 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200419int stats_emit_field_tags(struct buffer *out, const struct field *f,
420 char delim)
William Lallemand74c24fb2016-11-21 17:18:36 +0100421{
422 char origin, nature, scope;
423
424 switch (field_origin(f, 0)) {
425 case FO_METRIC: origin = 'M'; break;
426 case FO_STATUS: origin = 'S'; break;
427 case FO_KEY: origin = 'K'; break;
428 case FO_CONFIG: origin = 'C'; break;
429 case FO_PRODUCT: origin = 'P'; break;
430 default: origin = '?'; break;
431 }
432
433 switch (field_nature(f, 0)) {
434 case FN_GAUGE: nature = 'G'; break;
435 case FN_LIMIT: nature = 'L'; break;
436 case FN_MIN: nature = 'm'; break;
437 case FN_MAX: nature = 'M'; break;
438 case FN_RATE: nature = 'R'; break;
439 case FN_COUNTER: nature = 'C'; break;
440 case FN_DURATION: nature = 'D'; break;
441 case FN_AGE: nature = 'A'; break;
442 case FN_TIME: nature = 'T'; break;
443 case FN_NAME: nature = 'N'; break;
444 case FN_OUTPUT: nature = 'O'; break;
445 case FN_AVG: nature = 'a'; break;
446 default: nature = '?'; break;
447 }
448
449 switch (field_scope(f, 0)) {
450 case FS_PROCESS: scope = 'P'; break;
451 case FS_SERVICE: scope = 'S'; break;
452 case FS_SYSTEM: scope = 's'; break;
453 case FS_CLUSTER: scope = 'C'; break;
454 default: scope = '?'; break;
455 }
456
457 return chunk_appendf(out, "%c%c%c%c", origin, nature, scope, delim);
458}
459
Simon Horman05ee2132017-01-04 09:37:25 +0100460/* Emits an encoding of the field type as JSON.
461 * Returns non-zero on success, 0 if the buffer is full.
462 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200463int stats_emit_json_field_tags(struct buffer *out, const struct field *f)
Simon Horman05ee2132017-01-04 09:37:25 +0100464{
465 const char *origin, *nature, *scope;
466 int old_len;
467
468 switch (field_origin(f, 0)) {
469 case FO_METRIC: origin = "Metric"; break;
470 case FO_STATUS: origin = "Status"; break;
471 case FO_KEY: origin = "Key"; break;
472 case FO_CONFIG: origin = "Config"; break;
473 case FO_PRODUCT: origin = "Product"; break;
474 default: origin = "Unknown"; break;
475 }
476
477 switch (field_nature(f, 0)) {
478 case FN_GAUGE: nature = "Gauge"; break;
479 case FN_LIMIT: nature = "Limit"; break;
480 case FN_MIN: nature = "Min"; break;
481 case FN_MAX: nature = "Max"; break;
482 case FN_RATE: nature = "Rate"; break;
483 case FN_COUNTER: nature = "Counter"; break;
484 case FN_DURATION: nature = "Duration"; break;
485 case FN_AGE: nature = "Age"; break;
486 case FN_TIME: nature = "Time"; break;
487 case FN_NAME: nature = "Name"; break;
488 case FN_OUTPUT: nature = "Output"; break;
489 case FN_AVG: nature = "Avg"; break;
490 default: nature = "Unknown"; break;
491 }
492
493 switch (field_scope(f, 0)) {
494 case FS_PROCESS: scope = "Process"; break;
495 case FS_SERVICE: scope = "Service"; break;
496 case FS_SYSTEM: scope = "System"; break;
497 case FS_CLUSTER: scope = "Cluster"; break;
498 default: scope = "Unknown"; break;
499 }
500
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200501 old_len = out->data;
Simon Horman05ee2132017-01-04 09:37:25 +0100502 chunk_appendf(out, "\"tags\":{"
503 "\"origin\":\"%s\","
504 "\"nature\":\"%s\","
505 "\"scope\":\"%s\""
506 "}", origin, nature, scope);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200507 return !(old_len == out->data);
Simon Horman05ee2132017-01-04 09:37:25 +0100508}
William Lallemand74c24fb2016-11-21 17:18:36 +0100509
510/* Dump all fields from <stats> into <out> using CSV format */
Willy Tarreau83061a82018-07-13 11:56:34 +0200511static int stats_dump_fields_csv(struct buffer *out,
Willy Tarreau43241ff2019-10-09 11:27:51 +0200512 const struct field *stats, unsigned int flags)
William Lallemand74c24fb2016-11-21 17:18:36 +0100513{
514 int field;
515
516 for (field = 0; field < ST_F_TOTAL_FIELDS; field++) {
517 if (!stats_emit_raw_data_field(out, &stats[field]))
518 return 0;
519 if (!chunk_strcat(out, ","))
520 return 0;
521 }
522 chunk_strcat(out, "\n");
523 return 1;
524}
525
526/* Dump all fields from <stats> into <out> using a typed "field:desc:type:value" format */
Willy Tarreau83061a82018-07-13 11:56:34 +0200527static int stats_dump_fields_typed(struct buffer *out,
Willy Tarreau43241ff2019-10-09 11:27:51 +0200528 const struct field *stats, unsigned int flags)
William Lallemand74c24fb2016-11-21 17:18:36 +0100529{
530 int field;
531
532 for (field = 0; field < ST_F_TOTAL_FIELDS; field++) {
533 if (!stats[field].type)
534 continue;
535
536 chunk_appendf(out, "%c.%u.%u.%d.%s.%u:",
537 stats[ST_F_TYPE].u.u32 == STATS_TYPE_FE ? 'F' :
538 stats[ST_F_TYPE].u.u32 == STATS_TYPE_BE ? 'B' :
539 stats[ST_F_TYPE].u.u32 == STATS_TYPE_SO ? 'L' :
540 stats[ST_F_TYPE].u.u32 == STATS_TYPE_SV ? 'S' :
541 '?',
542 stats[ST_F_IID].u.u32, stats[ST_F_SID].u.u32,
Willy Tarreaueaa55372019-10-09 07:39:11 +0200543 field, stat_fields[field].name, stats[ST_F_PID].u.u32);
William Lallemand74c24fb2016-11-21 17:18:36 +0100544
545 if (!stats_emit_field_tags(out, &stats[field], ':'))
546 return 0;
547 if (!stats_emit_typed_data_field(out, &stats[field]))
548 return 0;
Willy Tarreau6b19b142019-10-09 15:44:21 +0200549 if ((flags & STAT_SHOW_FDESC) && !chunk_appendf(out, ":\"%s\"", stat_fields[field].desc))
550 return 0;
William Lallemand74c24fb2016-11-21 17:18:36 +0100551 if (!chunk_strcat(out, "\n"))
552 return 0;
553 }
554 return 1;
555}
556
Simon Horman05ee2132017-01-04 09:37:25 +0100557/* Dump all fields from <stats> into <out> using the "show info json" format */
Willy Tarreau83061a82018-07-13 11:56:34 +0200558static int stats_dump_json_info_fields(struct buffer *out,
Willy Tarreau43241ff2019-10-09 11:27:51 +0200559 const struct field *info, unsigned int flags)
Simon Horman05ee2132017-01-04 09:37:25 +0100560{
561 int field;
562 int started = 0;
563
564 if (!chunk_strcat(out, "["))
565 return 0;
566
567 for (field = 0; field < INF_TOTAL_FIELDS; field++) {
568 int old_len;
569
570 if (!field_format(info, field))
571 continue;
572
573 if (started && !chunk_strcat(out, ","))
574 goto err;
575 started = 1;
576
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200577 old_len = out->data;
Simon Horman05ee2132017-01-04 09:37:25 +0100578 chunk_appendf(out,
579 "{\"field\":{\"pos\":%d,\"name\":\"%s\"},"
580 "\"processNum\":%u,",
Willy Tarreaueaa55372019-10-09 07:39:11 +0200581 field, info_fields[field].name,
Simon Horman05ee2132017-01-04 09:37:25 +0100582 info[INF_PROCESS_NUM].u.u32);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200583 if (old_len == out->data)
Simon Horman05ee2132017-01-04 09:37:25 +0100584 goto err;
585
586 if (!stats_emit_json_field_tags(out, &info[field]))
587 goto err;
588
589 if (!stats_emit_json_data_field(out, &info[field]))
590 goto err;
591
592 if (!chunk_strcat(out, "}"))
593 goto err;
594 }
595
596 if (!chunk_strcat(out, "]"))
597 goto err;
598 return 1;
599
600err:
601 chunk_reset(out);
602 chunk_appendf(out, "{\"errorStr\":\"output buffer too short\"}");
603 return 0;
604}
605
606/* Dump all fields from <stats> into <out> using a typed "field:desc:type:value" format */
Willy Tarreau83061a82018-07-13 11:56:34 +0200607static int stats_dump_fields_json(struct buffer *out,
608 const struct field *stats,
Willy Tarreaub0ce3ad2019-10-09 11:19:29 +0200609 unsigned int flags)
Simon Horman05ee2132017-01-04 09:37:25 +0100610{
611 int field;
612 int started = 0;
613
Willy Tarreaub0ce3ad2019-10-09 11:19:29 +0200614 if ((flags & STAT_STARTED) && !chunk_strcat(out, ","))
Simon Horman05ee2132017-01-04 09:37:25 +0100615 return 0;
616 if (!chunk_strcat(out, "["))
617 return 0;
618
619 for (field = 0; field < ST_F_TOTAL_FIELDS; field++) {
620 const char *obj_type;
621 int old_len;
622
623 if (!stats[field].type)
624 continue;
625
626 if (started && !chunk_strcat(out, ","))
627 goto err;
628 started = 1;
629
630 switch (stats[ST_F_TYPE].u.u32) {
631 case STATS_TYPE_FE: obj_type = "Frontend"; break;
632 case STATS_TYPE_BE: obj_type = "Backend"; break;
633 case STATS_TYPE_SO: obj_type = "Listener"; break;
634 case STATS_TYPE_SV: obj_type = "Server"; break;
635 default: obj_type = "Unknown"; break;
636 }
637
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200638 old_len = out->data;
Simon Horman05ee2132017-01-04 09:37:25 +0100639 chunk_appendf(out,
640 "{"
641 "\"objType\":\"%s\","
642 "\"proxyId\":%d,"
643 "\"id\":%d,"
644 "\"field\":{\"pos\":%d,\"name\":\"%s\"},"
645 "\"processNum\":%u,",
646 obj_type, stats[ST_F_IID].u.u32,
647 stats[ST_F_SID].u.u32, field,
Willy Tarreaueaa55372019-10-09 07:39:11 +0200648 stat_fields[field].name, stats[ST_F_PID].u.u32);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200649 if (old_len == out->data)
Simon Horman05ee2132017-01-04 09:37:25 +0100650 goto err;
651
652 if (!stats_emit_json_field_tags(out, &stats[field]))
653 goto err;
654
655 if (!stats_emit_json_data_field(out, &stats[field]))
656 goto err;
657
658 if (!chunk_strcat(out, "}"))
659 goto err;
660 }
661
662 if (!chunk_strcat(out, "]"))
663 goto err;
664
665 return 1;
666
667err:
668 chunk_reset(out);
Willy Tarreaub0ce3ad2019-10-09 11:19:29 +0200669 if (flags & STAT_STARTED)
670 chunk_strcat(out, ",");
Simon Horman05ee2132017-01-04 09:37:25 +0100671 chunk_appendf(out, "{\"errorStr\":\"output buffer too short\"}");
672 return 0;
673}
674
William Lallemand74c24fb2016-11-21 17:18:36 +0100675/* Dump all fields from <stats> into <out> using the HTML format. A column is
Willy Tarreauab02b3f2019-10-09 11:11:46 +0200676 * reserved for the checkbox is STAT_ADMIN is set in <flags>. Some extra info
Willy Tarreau708c4162019-10-09 10:19:16 +0200677 * are provided if STAT_SHLGNDS is present in <flags>.
William Lallemand74c24fb2016-11-21 17:18:36 +0100678 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200679static int stats_dump_fields_html(struct buffer *out,
680 const struct field *stats,
681 unsigned int flags)
William Lallemand74c24fb2016-11-21 17:18:36 +0100682{
Willy Tarreau83061a82018-07-13 11:56:34 +0200683 struct buffer src;
William Lallemand74c24fb2016-11-21 17:18:36 +0100684
685 if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_FE) {
686 chunk_appendf(out,
687 /* name, queue */
688 "<tr class=\"frontend\">");
689
Willy Tarreauab02b3f2019-10-09 11:11:46 +0200690 if (flags & STAT_ADMIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +0100691 /* Column sub-heading for Enable or Disable server */
692 chunk_appendf(out, "<td></td>");
693 }
694
695 chunk_appendf(out,
696 "<td class=ac>"
697 "<a name=\"%s/Frontend\"></a>"
698 "<a class=lfsb href=\"#%s/Frontend\">Frontend</a></td>"
699 "<td colspan=3></td>"
700 "",
701 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_PXNAME));
702
703 chunk_appendf(out,
704 /* sessions rate : current */
705 "<td><u>%s<div class=tips><table class=det>"
706 "<tr><th>Current connection rate:</th><td>%s/s</td></tr>"
707 "<tr><th>Current session rate:</th><td>%s/s</td></tr>"
708 "",
709 U2H(stats[ST_F_RATE].u.u32),
710 U2H(stats[ST_F_CONN_RATE].u.u32),
711 U2H(stats[ST_F_RATE].u.u32));
712
713 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
714 chunk_appendf(out,
715 "<tr><th>Current request rate:</th><td>%s/s</td></tr>",
716 U2H(stats[ST_F_REQ_RATE].u.u32));
717
718 chunk_appendf(out,
719 "</table></div></u></td>"
720 /* sessions rate : max */
721 "<td><u>%s<div class=tips><table class=det>"
722 "<tr><th>Max connection rate:</th><td>%s/s</td></tr>"
723 "<tr><th>Max session rate:</th><td>%s/s</td></tr>"
724 "",
725 U2H(stats[ST_F_RATE_MAX].u.u32),
726 U2H(stats[ST_F_CONN_RATE_MAX].u.u32),
727 U2H(stats[ST_F_RATE_MAX].u.u32));
728
729 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
730 chunk_appendf(out,
731 "<tr><th>Max request rate:</th><td>%s/s</td></tr>",
732 U2H(stats[ST_F_REQ_RATE_MAX].u.u32));
733
734 chunk_appendf(out,
735 "</table></div></u></td>"
736 /* sessions rate : limit */
737 "<td>%s</td>",
738 LIM2A(stats[ST_F_RATE_LIM].u.u32, "-"));
739
740 chunk_appendf(out,
741 /* sessions: current, max, limit, total */
742 "<td>%s</td><td>%s</td><td>%s</td>"
743 "<td><u>%s<div class=tips><table class=det>"
744 "<tr><th>Cum. connections:</th><td>%s</td></tr>"
745 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
746 "",
747 U2H(stats[ST_F_SCUR].u.u32), U2H(stats[ST_F_SMAX].u.u32), U2H(stats[ST_F_SLIM].u.u32),
748 U2H(stats[ST_F_STOT].u.u64),
749 U2H(stats[ST_F_CONN_TOT].u.u64),
750 U2H(stats[ST_F_STOT].u.u64));
751
752 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
753 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0) {
754 chunk_appendf(out,
755 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
756 "<tr><th>- HTTP 1xx responses:</th><td>%s</td></tr>"
757 "<tr><th>- HTTP 2xx responses:</th><td>%s</td></tr>"
758 "<tr><th>&nbsp;&nbsp;Compressed 2xx:</th><td>%s</td><td>(%d%%)</td></tr>"
759 "<tr><th>- HTTP 3xx responses:</th><td>%s</td></tr>"
760 "<tr><th>- HTTP 4xx responses:</th><td>%s</td></tr>"
761 "<tr><th>- HTTP 5xx responses:</th><td>%s</td></tr>"
762 "<tr><th>- other responses:</th><td>%s</td></tr>"
763 "<tr><th>Intercepted requests:</th><td>%s</td></tr>"
Willy Tarreaua1214a52018-12-14 14:00:25 +0100764 "<tr><th>Cache lookups:</th><td>%s</td></tr>"
765 "<tr><th>Cache hits:</th><td>%s</td><td>(%d%%)</td></tr>"
Willy Tarreau1b0f85e2018-05-28 15:12:40 +0200766 "<tr><th>Failed hdr rewrites:</th><td>%s</td></tr>"
Christopher Faulet0159ee42019-12-16 14:40:39 +0100767 "<tr><th>Internal errors:</th><td>%s</td></tr>"
William Lallemand74c24fb2016-11-21 17:18:36 +0100768 "",
769 U2H(stats[ST_F_REQ_TOT].u.u64),
770 U2H(stats[ST_F_HRSP_1XX].u.u64),
771 U2H(stats[ST_F_HRSP_2XX].u.u64),
772 U2H(stats[ST_F_COMP_RSP].u.u64),
773 stats[ST_F_HRSP_2XX].u.u64 ?
774 (int)(100 * stats[ST_F_COMP_RSP].u.u64 / stats[ST_F_HRSP_2XX].u.u64) : 0,
775 U2H(stats[ST_F_HRSP_3XX].u.u64),
776 U2H(stats[ST_F_HRSP_4XX].u.u64),
777 U2H(stats[ST_F_HRSP_5XX].u.u64),
778 U2H(stats[ST_F_HRSP_OTHER].u.u64),
Willy Tarreau1b0f85e2018-05-28 15:12:40 +0200779 U2H(stats[ST_F_INTERCEPTED].u.u64),
Willy Tarreaua1214a52018-12-14 14:00:25 +0100780 U2H(stats[ST_F_CACHE_LOOKUPS].u.u64),
781 U2H(stats[ST_F_CACHE_HITS].u.u64),
782 stats[ST_F_CACHE_LOOKUPS].u.u64 ?
783 (int)(100 * stats[ST_F_CACHE_HITS].u.u64 / stats[ST_F_CACHE_LOOKUPS].u.u64) : 0,
Christopher Faulet0159ee42019-12-16 14:40:39 +0100784 U2H(stats[ST_F_WREW].u.u64),
785 U2H(stats[ST_F_EINT].u.u64));
William Lallemand74c24fb2016-11-21 17:18:36 +0100786 }
787
788 chunk_appendf(out,
789 "</table></div></u></td>"
790 /* sessions: lbtot, lastsess */
791 "<td></td><td></td>"
792 /* bytes : in */
793 "<td>%s</td>"
794 "",
795 U2H(stats[ST_F_BIN].u.u64));
796
797 chunk_appendf(out,
798 /* bytes:out + compression stats (via hover): comp_in, comp_out, comp_byp */
799 "<td>%s%s<div class=tips><table class=det>"
800 "<tr><th>Response bytes in:</th><td>%s</td></tr>"
801 "<tr><th>Compression in:</th><td>%s</td></tr>"
802 "<tr><th>Compression out:</th><td>%s</td><td>(%d%%)</td></tr>"
803 "<tr><th>Compression bypass:</th><td>%s</td></tr>"
804 "<tr><th>Total bytes saved:</th><td>%s</td><td>(%d%%)</td></tr>"
805 "</table></div>%s</td>",
806 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "<u>":"",
807 U2H(stats[ST_F_BOUT].u.u64),
808 U2H(stats[ST_F_BOUT].u.u64),
809 U2H(stats[ST_F_COMP_IN].u.u64),
810 U2H(stats[ST_F_COMP_OUT].u.u64),
811 stats[ST_F_COMP_IN].u.u64 ? (int)(stats[ST_F_COMP_OUT].u.u64 * 100 / stats[ST_F_COMP_IN].u.u64) : 0,
812 U2H(stats[ST_F_COMP_BYP].u.u64),
813 U2H(stats[ST_F_COMP_IN].u.u64 - stats[ST_F_COMP_OUT].u.u64),
814 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,
815 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "</u>":"");
816
817 chunk_appendf(out,
818 /* denied: req, resp */
819 "<td>%s</td><td>%s</td>"
820 /* errors : request, connect, response */
821 "<td>%s</td><td></td><td></td>"
822 /* warnings: retries, redispatches */
823 "<td></td><td></td>"
824 /* server status : reflect frontend status */
825 "<td class=ac>%s</td>"
826 /* rest of server: nothing */
827 "<td class=ac colspan=8></td></tr>"
828 "",
829 U2H(stats[ST_F_DREQ].u.u64), U2H(stats[ST_F_DRESP].u.u64),
830 U2H(stats[ST_F_EREQ].u.u64),
831 field_str(stats, ST_F_STATUS));
832 }
833 else if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_SO) {
834 chunk_appendf(out, "<tr class=socket>");
Willy Tarreauab02b3f2019-10-09 11:11:46 +0200835 if (flags & STAT_ADMIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +0100836 /* Column sub-heading for Enable or Disable server */
837 chunk_appendf(out, "<td></td>");
838 }
839
840 chunk_appendf(out,
841 /* frontend name, listener name */
842 "<td class=ac><a name=\"%s/+%s\"></a>%s"
843 "<a class=lfsb href=\"#%s/+%s\">%s</a>"
844 "",
845 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME),
Willy Tarreau708c4162019-10-09 10:19:16 +0200846 (flags & STAT_SHLGNDS)?"<u>":"",
William Lallemand74c24fb2016-11-21 17:18:36 +0100847 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME), field_str(stats, ST_F_SVNAME));
848
Willy Tarreau708c4162019-10-09 10:19:16 +0200849 if (flags & STAT_SHLGNDS) {
William Lallemand74c24fb2016-11-21 17:18:36 +0100850 chunk_appendf(out, "<div class=tips>");
851
Willy Tarreau90807112020-02-25 08:16:33 +0100852 if (isdigit((unsigned char)*field_str(stats, ST_F_ADDR)))
William Lallemand74c24fb2016-11-21 17:18:36 +0100853 chunk_appendf(out, "IPv4: %s, ", field_str(stats, ST_F_ADDR));
854 else if (*field_str(stats, ST_F_ADDR) == '[')
855 chunk_appendf(out, "IPv6: %s, ", field_str(stats, ST_F_ADDR));
856 else if (*field_str(stats, ST_F_ADDR))
857 chunk_appendf(out, "%s, ", field_str(stats, ST_F_ADDR));
858
859 /* id */
860 chunk_appendf(out, "id: %d</div>", stats[ST_F_SID].u.u32);
861 }
862
863 chunk_appendf(out,
864 /* queue */
865 "%s</td><td colspan=3></td>"
866 /* sessions rate: current, max, limit */
867 "<td colspan=3>&nbsp;</td>"
868 /* sessions: current, max, limit, total, lbtot, lastsess */
869 "<td>%s</td><td>%s</td><td>%s</td>"
870 "<td>%s</td><td>&nbsp;</td><td>&nbsp;</td>"
871 /* bytes: in, out */
872 "<td>%s</td><td>%s</td>"
873 "",
Willy Tarreau708c4162019-10-09 10:19:16 +0200874 (flags & STAT_SHLGNDS)?"</u>":"",
William Lallemand74c24fb2016-11-21 17:18:36 +0100875 U2H(stats[ST_F_SCUR].u.u32), U2H(stats[ST_F_SMAX].u.u32), U2H(stats[ST_F_SLIM].u.u32),
876 U2H(stats[ST_F_STOT].u.u64), U2H(stats[ST_F_BIN].u.u64), U2H(stats[ST_F_BOUT].u.u64));
877
878 chunk_appendf(out,
879 /* denied: req, resp */
880 "<td>%s</td><td>%s</td>"
881 /* errors: request, connect, response */
882 "<td>%s</td><td></td><td></td>"
883 /* warnings: retries, redispatches */
884 "<td></td><td></td>"
885 /* server status: reflect listener status */
886 "<td class=ac>%s</td>"
887 /* rest of server: nothing */
888 "<td class=ac colspan=8></td></tr>"
889 "",
890 U2H(stats[ST_F_DREQ].u.u64), U2H(stats[ST_F_DRESP].u.u64),
891 U2H(stats[ST_F_EREQ].u.u64),
892 field_str(stats, ST_F_STATUS));
893 }
894 else if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_SV) {
895 const char *style;
896
897 /* determine the style to use depending on the server's state,
898 * its health and weight. There isn't a 1-to-1 mapping between
899 * state and styles for the cases where the server is (still)
900 * up. The reason is that we don't want to report nolb and
901 * drain with the same color.
902 */
903
904 if (strcmp(field_str(stats, ST_F_STATUS), "DOWN") == 0 ||
905 strcmp(field_str(stats, ST_F_STATUS), "DOWN (agent)") == 0) {
906 style = "down";
907 }
908 else if (strcmp(field_str(stats, ST_F_STATUS), "DOWN ") == 0) {
909 style = "going_up";
910 }
Daniel Corbettb4285172020-03-28 12:35:50 -0400911 else if (strcmp(field_str(stats, ST_F_STATUS), "DRAIN") == 0) {
912 style = "draining";
913 }
William Lallemand74c24fb2016-11-21 17:18:36 +0100914 else if (strcmp(field_str(stats, ST_F_STATUS), "NOLB ") == 0) {
915 style = "going_down";
916 }
917 else if (strcmp(field_str(stats, ST_F_STATUS), "NOLB") == 0) {
918 style = "nolb";
919 }
920 else if (strcmp(field_str(stats, ST_F_STATUS), "no check") == 0) {
921 style = "no_check";
922 }
923 else if (!stats[ST_F_CHKFAIL].type ||
924 stats[ST_F_CHECK_HEALTH].u.u32 == stats[ST_F_CHECK_RISE].u.u32 + stats[ST_F_CHECK_FALL].u.u32 - 1) {
925 /* no check or max health = UP */
926 if (stats[ST_F_WEIGHT].u.u32)
927 style = "up";
928 else
929 style = "draining";
930 }
931 else {
932 style = "going_down";
933 }
934
935 if (memcmp(field_str(stats, ST_F_STATUS), "MAINT", 5) == 0)
936 chunk_appendf(out, "<tr class=\"maintain\">");
937 else
938 chunk_appendf(out,
939 "<tr class=\"%s_%s\">",
940 (stats[ST_F_BCK].u.u32) ? "backup" : "active", style);
941
942
Willy Tarreauab02b3f2019-10-09 11:11:46 +0200943 if (flags & STAT_ADMIN)
William Lallemand74c24fb2016-11-21 17:18:36 +0100944 chunk_appendf(out,
David Harrigand3db35a2016-12-30 12:12:49 +0000945 "<td><input class='%s-checkbox' type=\"checkbox\" name=\"s\" value=\"%s\"></td>",
946 field_str(stats, ST_F_PXNAME),
William Lallemand74c24fb2016-11-21 17:18:36 +0100947 field_str(stats, ST_F_SVNAME));
948
949 chunk_appendf(out,
950 "<td class=ac><a name=\"%s/%s\"></a>%s"
951 "<a class=lfsb href=\"#%s/%s\">%s</a>"
952 "",
953 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME),
Willy Tarreau708c4162019-10-09 10:19:16 +0200954 (flags & STAT_SHLGNDS) ? "<u>" : "",
William Lallemand74c24fb2016-11-21 17:18:36 +0100955 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME), field_str(stats, ST_F_SVNAME));
956
Willy Tarreau708c4162019-10-09 10:19:16 +0200957 if (flags & STAT_SHLGNDS) {
William Lallemand74c24fb2016-11-21 17:18:36 +0100958 chunk_appendf(out, "<div class=tips>");
959
Willy Tarreau90807112020-02-25 08:16:33 +0100960 if (isdigit((unsigned char)*field_str(stats, ST_F_ADDR)))
William Lallemand74c24fb2016-11-21 17:18:36 +0100961 chunk_appendf(out, "IPv4: %s, ", field_str(stats, ST_F_ADDR));
962 else if (*field_str(stats, ST_F_ADDR) == '[')
963 chunk_appendf(out, "IPv6: %s, ", field_str(stats, ST_F_ADDR));
964 else if (*field_str(stats, ST_F_ADDR))
965 chunk_appendf(out, "%s, ", field_str(stats, ST_F_ADDR));
966
967 /* id */
968 chunk_appendf(out, "id: %d", stats[ST_F_SID].u.u32);
969
970 /* cookie */
971 if (stats[ST_F_COOKIE].type) {
972 chunk_appendf(out, ", cookie: '");
973 chunk_initstr(&src, field_str(stats, ST_F_COOKIE));
974 chunk_htmlencode(out, &src);
975 chunk_appendf(out, "'");
976 }
977
978 chunk_appendf(out, "</div>");
979 }
980
981 chunk_appendf(out,
982 /* queue : current, max, limit */
983 "%s</td><td>%s</td><td>%s</td><td>%s</td>"
984 /* sessions rate : current, max, limit */
985 "<td>%s</td><td>%s</td><td></td>"
986 "",
Willy Tarreau708c4162019-10-09 10:19:16 +0200987 (flags & STAT_SHLGNDS) ? "</u>" : "",
William Lallemand74c24fb2016-11-21 17:18:36 +0100988 U2H(stats[ST_F_QCUR].u.u32), U2H(stats[ST_F_QMAX].u.u32), LIM2A(stats[ST_F_QLIMIT].u.u32, "-"),
989 U2H(stats[ST_F_RATE].u.u32), U2H(stats[ST_F_RATE_MAX].u.u32));
990
991 chunk_appendf(out,
992 /* sessions: current, max, limit, total */
Willy Tarreauf21d17b2019-09-08 09:24:56 +0200993 "<td><u>%s<div class=tips>"
994 "<table class=det>"
995 "<tr><th>Current active connections:</th><td>%s</td></tr>"
996 "<tr><th>Current idle connections:</th><td>%s</td></tr>"
997 "<tr><th>Active connections limit:</th><td>%s</td></tr>"
998 "<tr><th>Idle connections limit:</th><td>%s</td></tr>"
999 "</table></div></u>"
1000 "</td><td>%s</td><td>%s</td>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001001 "<td><u>%s<div class=tips><table class=det>"
1002 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
1003 "",
Willy Tarreauf21d17b2019-09-08 09:24:56 +02001004 U2H(stats[ST_F_SCUR].u.u32),
1005 U2H(stats[ST_F_SCUR].u.u32),
1006 U2H(stats[ST_F_SRV_ICUR].u.u32),
1007 LIM2A(stats[ST_F_SLIM].u.u32, "-"),
1008 stats[ST_F_SRV_ILIM].type ? U2H(stats[ST_F_SRV_ILIM].u.u32) : "-",
1009 U2H(stats[ST_F_SMAX].u.u32), LIM2A(stats[ST_F_SLIM].u.u32, "-"),
William Lallemand74c24fb2016-11-21 17:18:36 +01001010 U2H(stats[ST_F_STOT].u.u64),
1011 U2H(stats[ST_F_STOT].u.u64));
1012
1013 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
1014 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001015 chunk_appendf(out,
Willy Tarreauf1573842018-12-14 11:35:36 +01001016 "<tr><th>New connections:</th><td>%s</td></tr>"
1017 "<tr><th>Reused connections:</th><td>%s</td><td>(%d%%)</td></tr>"
Marcin Deranek3c27dda2020-05-15 18:32:51 +02001018 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001019 "<tr><th>- HTTP 1xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
1020 "<tr><th>- HTTP 2xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
1021 "<tr><th>- HTTP 3xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
1022 "<tr><th>- HTTP 4xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
1023 "<tr><th>- HTTP 5xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
1024 "<tr><th>- other responses:</th><td>%s</td><td>(%d%%)</td></tr>"
Willy Tarreau1b0f85e2018-05-28 15:12:40 +02001025 "<tr><th>Failed hdr rewrites:</th><td>%s</td></tr>"
Christopher Faulet0159ee42019-12-16 14:40:39 +01001026 "<tr><th>Internal error:</th><td>%s</td></tr>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001027 "",
Willy Tarreauf1573842018-12-14 11:35:36 +01001028 U2H(stats[ST_F_CONNECT].u.u64),
1029 U2H(stats[ST_F_REUSE].u.u64),
1030 (stats[ST_F_CONNECT].u.u64 + stats[ST_F_REUSE].u.u64) ?
1031 (int)(100 * stats[ST_F_REUSE].u.u64 / (stats[ST_F_CONNECT].u.u64 + stats[ST_F_REUSE].u.u64)) : 0,
Marcin Deranek3c27dda2020-05-15 18:32:51 +02001032 U2H(stats[ST_F_REQ_TOT].u.u64),
1033 U2H(stats[ST_F_HRSP_1XX].u.u64), stats[ST_F_REQ_TOT].u.u64 ?
1034 (int)(100 * stats[ST_F_HRSP_1XX].u.u64 / stats[ST_F_REQ_TOT].u.u64) : 0,
1035 U2H(stats[ST_F_HRSP_2XX].u.u64), stats[ST_F_REQ_TOT].u.u64 ?
1036 (int)(100 * stats[ST_F_HRSP_2XX].u.u64 / stats[ST_F_REQ_TOT].u.u64) : 0,
1037 U2H(stats[ST_F_HRSP_3XX].u.u64), stats[ST_F_REQ_TOT].u.u64 ?
1038 (int)(100 * stats[ST_F_HRSP_3XX].u.u64 / stats[ST_F_REQ_TOT].u.u64) : 0,
1039 U2H(stats[ST_F_HRSP_4XX].u.u64), stats[ST_F_REQ_TOT].u.u64 ?
1040 (int)(100 * stats[ST_F_HRSP_4XX].u.u64 / stats[ST_F_REQ_TOT].u.u64) : 0,
1041 U2H(stats[ST_F_HRSP_5XX].u.u64), stats[ST_F_REQ_TOT].u.u64 ?
1042 (int)(100 * stats[ST_F_HRSP_5XX].u.u64 / stats[ST_F_REQ_TOT].u.u64) : 0,
1043 U2H(stats[ST_F_HRSP_OTHER].u.u64), stats[ST_F_REQ_TOT].u.u64 ?
1044 (int)(100 * stats[ST_F_HRSP_OTHER].u.u64 / stats[ST_F_REQ_TOT].u.u64) : 0,
Christopher Faulet0159ee42019-12-16 14:40:39 +01001045 U2H(stats[ST_F_WREW].u.u64),
1046 U2H(stats[ST_F_EINT].u.u64));
William Lallemand74c24fb2016-11-21 17:18:36 +01001047 }
1048
Christopher Faulet0d1c2a62019-11-08 14:59:51 +01001049 chunk_appendf(out, "<tr><th colspan=3>Max / Avg over last 1024 success. conn.</th></tr>");
1050 chunk_appendf(out, "<tr><th>- Queue time:</th><td>%s / %s</td><td>ms</td></tr>",
1051 U2H(stats[ST_F_QT_MAX].u.u32), U2H(stats[ST_F_QTIME].u.u32));
1052 chunk_appendf(out, "<tr><th>- Connect time:</th><td>%s / %s</td><td>ms</td></tr>",
1053 U2H(stats[ST_F_CT_MAX].u.u32), U2H(stats[ST_F_CTIME].u.u32));
William Lallemand74c24fb2016-11-21 17:18:36 +01001054 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
Christopher Faulet0d1c2a62019-11-08 14:59:51 +01001055 chunk_appendf(out, "<tr><th>- Responses time:</th><td>%s / %s</td><td>ms</td></tr>",
1056 U2H(stats[ST_F_RT_MAX].u.u32), U2H(stats[ST_F_RTIME].u.u32));
1057 chunk_appendf(out, "<tr><th>- Total time:</th><td>%s / %s</td><td>ms</td></tr>",
1058 U2H(stats[ST_F_TT_MAX].u.u32), U2H(stats[ST_F_TTIME].u.u32));
William Lallemand74c24fb2016-11-21 17:18:36 +01001059
1060 chunk_appendf(out,
1061 "</table></div></u></td>"
1062 /* sessions: lbtot, last */
1063 "<td>%s</td><td>%s</td>",
1064 U2H(stats[ST_F_LBTOT].u.u64),
1065 human_time(stats[ST_F_LASTSESS].u.s32, 1));
1066
1067 chunk_appendf(out,
1068 /* bytes : in, out */
1069 "<td>%s</td><td>%s</td>"
1070 /* denied: req, resp */
1071 "<td></td><td>%s</td>"
1072 /* errors : request, connect */
1073 "<td></td><td>%s</td>"
1074 /* errors : response */
1075 "<td><u>%s<div class=tips>Connection resets during transfers: %lld client, %lld server</div></u></td>"
1076 /* warnings: retries, redispatches */
1077 "<td>%lld</td><td>%lld</td>"
1078 "",
1079 U2H(stats[ST_F_BIN].u.u64), U2H(stats[ST_F_BOUT].u.u64),
1080 U2H(stats[ST_F_DRESP].u.u64),
1081 U2H(stats[ST_F_ECON].u.u64),
1082 U2H(stats[ST_F_ERESP].u.u64),
1083 (long long)stats[ST_F_CLI_ABRT].u.u64,
1084 (long long)stats[ST_F_SRV_ABRT].u.u64,
1085 (long long)stats[ST_F_WRETR].u.u64,
1086 (long long)stats[ST_F_WREDIS].u.u64);
1087
1088 /* status, last change */
1089 chunk_appendf(out, "<td class=ac>");
1090
1091 /* FIXME!!!!
1092 * LASTCHG should contain the last change for *this* server and must be computed
1093 * properly above, as was done below, ie: this server if maint, otherwise ref server
1094 * if tracking. Note that ref is either local or remote depending on tracking.
1095 */
1096
1097
1098 if (memcmp(field_str(stats, ST_F_STATUS), "MAINT", 5) == 0) {
1099 chunk_appendf(out, "%s MAINT", human_time(stats[ST_F_LASTCHG].u.u32, 1));
1100 }
1101 else if (memcmp(field_str(stats, ST_F_STATUS), "no check", 5) == 0) {
1102 chunk_strcat(out, "<i>no check</i>");
1103 }
1104 else {
1105 chunk_appendf(out, "%s %s", human_time(stats[ST_F_LASTCHG].u.u32, 1), field_str(stats, ST_F_STATUS));
1106 if (memcmp(field_str(stats, ST_F_STATUS), "DOWN", 4) == 0) {
1107 if (stats[ST_F_CHECK_HEALTH].u.u32)
1108 chunk_strcat(out, " &uarr;");
1109 }
1110 else if (stats[ST_F_CHECK_HEALTH].u.u32 < stats[ST_F_CHECK_RISE].u.u32 + stats[ST_F_CHECK_FALL].u.u32 - 1)
1111 chunk_strcat(out, " &darr;");
1112 }
1113
1114 if (memcmp(field_str(stats, ST_F_STATUS), "DOWN", 4) == 0 &&
1115 stats[ST_F_AGENT_STATUS].type && !stats[ST_F_AGENT_HEALTH].u.u32) {
1116 chunk_appendf(out,
1117 "</td><td class=ac><u> %s",
1118 field_str(stats, ST_F_AGENT_STATUS));
1119
1120 if (stats[ST_F_AGENT_CODE].type)
1121 chunk_appendf(out, "/%d", stats[ST_F_AGENT_CODE].u.u32);
1122
1123 if (stats[ST_F_AGENT_DURATION].type)
1124 chunk_appendf(out, " in %lums", (long)stats[ST_F_AGENT_DURATION].u.u64);
1125
1126 chunk_appendf(out, "<div class=tips>%s", field_str(stats, ST_F_AGENT_DESC));
1127
1128 if (*field_str(stats, ST_F_LAST_AGT)) {
1129 chunk_appendf(out, ": ");
1130 chunk_initstr(&src, field_str(stats, ST_F_LAST_AGT));
1131 chunk_htmlencode(out, &src);
1132 }
1133 chunk_appendf(out, "</div></u>");
1134 }
1135 else if (stats[ST_F_CHECK_STATUS].type) {
1136 chunk_appendf(out,
1137 "</td><td class=ac><u> %s",
1138 field_str(stats, ST_F_CHECK_STATUS));
1139
1140 if (stats[ST_F_CHECK_CODE].type)
1141 chunk_appendf(out, "/%d", stats[ST_F_CHECK_CODE].u.u32);
1142
1143 if (stats[ST_F_CHECK_DURATION].type)
1144 chunk_appendf(out, " in %lums", (long)stats[ST_F_CHECK_DURATION].u.u64);
1145
1146 chunk_appendf(out, "<div class=tips>%s", field_str(stats, ST_F_CHECK_DESC));
1147
1148 if (*field_str(stats, ST_F_LAST_CHK)) {
1149 chunk_appendf(out, ": ");
1150 chunk_initstr(&src, field_str(stats, ST_F_LAST_CHK));
1151 chunk_htmlencode(out, &src);
1152 }
1153 chunk_appendf(out, "</div></u>");
1154 }
1155 else
1156 chunk_appendf(out, "</td><td>");
1157
1158 chunk_appendf(out,
1159 /* weight */
1160 "</td><td class=ac>%d</td>"
1161 /* act, bck */
1162 "<td class=ac>%s</td><td class=ac>%s</td>"
1163 "",
1164 stats[ST_F_WEIGHT].u.u32,
1165 stats[ST_F_BCK].u.u32 ? "-" : "Y",
1166 stats[ST_F_BCK].u.u32 ? "Y" : "-");
1167
1168 /* check failures: unique, fatal, down time */
1169 if (strcmp(field_str(stats, ST_F_STATUS), "MAINT (resolution)") == 0) {
1170 chunk_appendf(out, "<td class=ac colspan=3>resolution</td>");
1171 }
1172 else if (stats[ST_F_CHKFAIL].type) {
1173 chunk_appendf(out, "<td><u>%lld", (long long)stats[ST_F_CHKFAIL].u.u64);
1174
1175 if (stats[ST_F_HANAFAIL].type)
1176 chunk_appendf(out, "/%lld", (long long)stats[ST_F_HANAFAIL].u.u64);
1177
1178 chunk_appendf(out,
1179 "<div class=tips>Failed Health Checks%s</div></u></td>"
1180 "<td>%lld</td><td>%s</td>"
1181 "",
1182 stats[ST_F_HANAFAIL].type ? "/Health Analyses" : "",
1183 (long long)stats[ST_F_CHKDOWN].u.u64, human_time(stats[ST_F_DOWNTIME].u.u32, 1));
1184 }
1185 else if (strcmp(field_str(stats, ST_F_STATUS), "MAINT") != 0 && field_format(stats, ST_F_TRACKED) == FF_STR) {
1186 /* tracking a server (hence inherited maint would appear as "MAINT (via...)" */
1187 chunk_appendf(out,
1188 "<td class=ac colspan=3><a class=lfsb href=\"#%s\">via %s</a></td>",
1189 field_str(stats, ST_F_TRACKED), field_str(stats, ST_F_TRACKED));
1190 }
1191 else
1192 chunk_appendf(out, "<td colspan=3></td>");
1193
1194 /* throttle */
1195 if (stats[ST_F_THROTTLE].type)
1196 chunk_appendf(out, "<td class=ac>%d %%</td></tr>\n", stats[ST_F_THROTTLE].u.u32);
1197 else
1198 chunk_appendf(out, "<td class=ac>-</td></tr>\n");
1199 }
1200 else if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_BE) {
1201 chunk_appendf(out, "<tr class=\"backend\">");
Willy Tarreauab02b3f2019-10-09 11:11:46 +02001202 if (flags & STAT_ADMIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001203 /* Column sub-heading for Enable or Disable server */
1204 chunk_appendf(out, "<td></td>");
1205 }
1206 chunk_appendf(out,
1207 "<td class=ac>"
1208 /* name */
1209 "%s<a name=\"%s/Backend\"></a>"
1210 "<a class=lfsb href=\"#%s/Backend\">Backend</a>"
1211 "",
Willy Tarreau708c4162019-10-09 10:19:16 +02001212 (flags & STAT_SHLGNDS)?"<u>":"",
William Lallemand74c24fb2016-11-21 17:18:36 +01001213 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_PXNAME));
1214
Willy Tarreau708c4162019-10-09 10:19:16 +02001215 if (flags & STAT_SHLGNDS) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001216 /* balancing */
1217 chunk_appendf(out, "<div class=tips>balancing: %s",
1218 field_str(stats, ST_F_ALGO));
1219
1220 /* cookie */
1221 if (stats[ST_F_COOKIE].type) {
1222 chunk_appendf(out, ", cookie: '");
1223 chunk_initstr(&src, field_str(stats, ST_F_COOKIE));
1224 chunk_htmlencode(out, &src);
1225 chunk_appendf(out, "'");
1226 }
1227 chunk_appendf(out, "</div>");
1228 }
1229
1230 chunk_appendf(out,
1231 "%s</td>"
1232 /* queue : current, max */
1233 "<td>%s</td><td>%s</td><td></td>"
1234 /* sessions rate : current, max, limit */
1235 "<td>%s</td><td>%s</td><td></td>"
1236 "",
Willy Tarreau708c4162019-10-09 10:19:16 +02001237 (flags & STAT_SHLGNDS)?"</u>":"",
William Lallemand74c24fb2016-11-21 17:18:36 +01001238 U2H(stats[ST_F_QCUR].u.u32), U2H(stats[ST_F_QMAX].u.u32),
1239 U2H(stats[ST_F_RATE].u.u32), U2H(stats[ST_F_RATE_MAX].u.u32));
1240
1241 chunk_appendf(out,
1242 /* sessions: current, max, limit, total */
1243 "<td>%s</td><td>%s</td><td>%s</td>"
1244 "<td><u>%s<div class=tips><table class=det>"
1245 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
1246 "",
Willy Tarreau8e0f1752016-12-12 15:07:29 +01001247 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 +01001248 U2H(stats[ST_F_STOT].u.u64),
1249 U2H(stats[ST_F_STOT].u.u64));
1250
1251 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
1252 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0) {
1253 chunk_appendf(out,
Willy Tarreauf1573842018-12-14 11:35:36 +01001254 "<tr><th>New connections:</th><td>%s</td></tr>"
1255 "<tr><th>Reused connections:</th><td>%s</td><td>(%d%%)</td></tr>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001256 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
1257 "<tr><th>- HTTP 1xx responses:</th><td>%s</td></tr>"
1258 "<tr><th>- HTTP 2xx responses:</th><td>%s</td></tr>"
1259 "<tr><th>&nbsp;&nbsp;Compressed 2xx:</th><td>%s</td><td>(%d%%)</td></tr>"
1260 "<tr><th>- HTTP 3xx responses:</th><td>%s</td></tr>"
1261 "<tr><th>- HTTP 4xx responses:</th><td>%s</td></tr>"
1262 "<tr><th>- HTTP 5xx responses:</th><td>%s</td></tr>"
1263 "<tr><th>- other responses:</th><td>%s</td></tr>"
Willy Tarreaua1214a52018-12-14 14:00:25 +01001264 "<tr><th>Cache lookups:</th><td>%s</td></tr>"
1265 "<tr><th>Cache hits:</th><td>%s</td><td>(%d%%)</td></tr>"
Willy Tarreau1b0f85e2018-05-28 15:12:40 +02001266 "<tr><th>Failed hdr rewrites:</th><td>%s</td></tr>"
Christopher Faulet0159ee42019-12-16 14:40:39 +01001267 "<tr><th>Internal errors:</th><td>%s</td></tr>"
Christopher Faulet0d1c2a62019-11-08 14:59:51 +01001268 "",
Willy Tarreauf1573842018-12-14 11:35:36 +01001269 U2H(stats[ST_F_CONNECT].u.u64),
1270 U2H(stats[ST_F_REUSE].u.u64),
1271 (stats[ST_F_CONNECT].u.u64 + stats[ST_F_REUSE].u.u64) ?
1272 (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 +01001273 U2H(stats[ST_F_REQ_TOT].u.u64),
1274 U2H(stats[ST_F_HRSP_1XX].u.u64),
1275 U2H(stats[ST_F_HRSP_2XX].u.u64),
1276 U2H(stats[ST_F_COMP_RSP].u.u64),
1277 stats[ST_F_HRSP_2XX].u.u64 ?
1278 (int)(100 * stats[ST_F_COMP_RSP].u.u64 / stats[ST_F_HRSP_2XX].u.u64) : 0,
1279 U2H(stats[ST_F_HRSP_3XX].u.u64),
1280 U2H(stats[ST_F_HRSP_4XX].u.u64),
1281 U2H(stats[ST_F_HRSP_5XX].u.u64),
Willy Tarreaufeead3a2018-12-14 13:48:44 +01001282 U2H(stats[ST_F_HRSP_OTHER].u.u64),
Willy Tarreaua1214a52018-12-14 14:00:25 +01001283 U2H(stats[ST_F_CACHE_LOOKUPS].u.u64),
1284 U2H(stats[ST_F_CACHE_HITS].u.u64),
1285 stats[ST_F_CACHE_LOOKUPS].u.u64 ?
1286 (int)(100 * stats[ST_F_CACHE_HITS].u.u64 / stats[ST_F_CACHE_LOOKUPS].u.u64) : 0,
Christopher Faulet0159ee42019-12-16 14:40:39 +01001287 U2H(stats[ST_F_WREW].u.u64),
1288 U2H(stats[ST_F_EINT].u.u64));
William Lallemand74c24fb2016-11-21 17:18:36 +01001289 }
1290
Christopher Faulet0d1c2a62019-11-08 14:59:51 +01001291 chunk_appendf(out, "<tr><th colspan=3>Max / Avg over last 1024 success. conn.</th></tr>");
1292 chunk_appendf(out, "<tr><th>- Queue time:</th><td>%s / %s</td><td>ms</td></tr>",
1293 U2H(stats[ST_F_QT_MAX].u.u32), U2H(stats[ST_F_QTIME].u.u32));
1294 chunk_appendf(out, "<tr><th>- Connect time:</th><td>%s / %s</td><td>ms</td></tr>",
1295 U2H(stats[ST_F_CT_MAX].u.u32), U2H(stats[ST_F_CTIME].u.u32));
William Lallemand74c24fb2016-11-21 17:18:36 +01001296 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
Christopher Faulet0d1c2a62019-11-08 14:59:51 +01001297 chunk_appendf(out, "<tr><th>- Responses time:</th><td>%s / %s</td><td>ms</td></tr>",
1298 U2H(stats[ST_F_RT_MAX].u.u32), U2H(stats[ST_F_RTIME].u.u32));
1299 chunk_appendf(out, "<tr><th>- Total time:</th><td>%s / %s</td><td>ms</td></tr>",
1300 U2H(stats[ST_F_TT_MAX].u.u32), U2H(stats[ST_F_TTIME].u.u32));
William Lallemand74c24fb2016-11-21 17:18:36 +01001301
1302 chunk_appendf(out,
1303 "</table></div></u></td>"
1304 /* sessions: lbtot, last */
1305 "<td>%s</td><td>%s</td>"
1306 /* bytes: in */
1307 "<td>%s</td>"
1308 "",
1309 U2H(stats[ST_F_LBTOT].u.u64),
1310 human_time(stats[ST_F_LASTSESS].u.s32, 1),
1311 U2H(stats[ST_F_BIN].u.u64));
1312
1313 chunk_appendf(out,
1314 /* bytes:out + compression stats (via hover): comp_in, comp_out, comp_byp */
1315 "<td>%s%s<div class=tips><table class=det>"
1316 "<tr><th>Response bytes in:</th><td>%s</td></tr>"
1317 "<tr><th>Compression in:</th><td>%s</td></tr>"
1318 "<tr><th>Compression out:</th><td>%s</td><td>(%d%%)</td></tr>"
1319 "<tr><th>Compression bypass:</th><td>%s</td></tr>"
1320 "<tr><th>Total bytes saved:</th><td>%s</td><td>(%d%%)</td></tr>"
1321 "</table></div>%s</td>",
1322 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "<u>":"",
1323 U2H(stats[ST_F_BOUT].u.u64),
1324 U2H(stats[ST_F_BOUT].u.u64),
1325 U2H(stats[ST_F_COMP_IN].u.u64),
1326 U2H(stats[ST_F_COMP_OUT].u.u64),
1327 stats[ST_F_COMP_IN].u.u64 ? (int)(stats[ST_F_COMP_OUT].u.u64 * 100 / stats[ST_F_COMP_IN].u.u64) : 0,
1328 U2H(stats[ST_F_COMP_BYP].u.u64),
1329 U2H(stats[ST_F_COMP_IN].u.u64 - stats[ST_F_COMP_OUT].u.u64),
1330 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,
1331 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "</u>":"");
1332
1333 chunk_appendf(out,
1334 /* denied: req, resp */
1335 "<td>%s</td><td>%s</td>"
1336 /* errors : request, connect */
1337 "<td></td><td>%s</td>"
1338 /* errors : response */
1339 "<td><u>%s<div class=tips>Connection resets during transfers: %lld client, %lld server</div></u></td>"
1340 /* warnings: retries, redispatches */
1341 "<td>%lld</td><td>%lld</td>"
1342 /* backend status: reflect backend status (up/down): we display UP
1343 * if the backend has known working servers or if it has no server at
1344 * all (eg: for stats). Then we display the total weight, number of
1345 * active and backups. */
1346 "<td class=ac>%s %s</td><td class=ac>&nbsp;</td><td class=ac>%d</td>"
1347 "<td class=ac>%d</td><td class=ac>%d</td>"
1348 "",
1349 U2H(stats[ST_F_DREQ].u.u64), U2H(stats[ST_F_DRESP].u.u64),
1350 U2H(stats[ST_F_ECON].u.u64),
1351 U2H(stats[ST_F_ERESP].u.u64),
1352 (long long)stats[ST_F_CLI_ABRT].u.u64,
1353 (long long)stats[ST_F_SRV_ABRT].u.u64,
1354 (long long)stats[ST_F_WRETR].u.u64, (long long)stats[ST_F_WREDIS].u.u64,
1355 human_time(stats[ST_F_LASTCHG].u.u32, 1),
1356 strcmp(field_str(stats, ST_F_STATUS), "DOWN") ? field_str(stats, ST_F_STATUS) : "<font color=\"red\"><b>DOWN</b></font>",
1357 stats[ST_F_WEIGHT].u.u32,
1358 stats[ST_F_ACT].u.u32, stats[ST_F_BCK].u.u32);
1359
1360 chunk_appendf(out,
1361 /* rest of backend: nothing, down transitions, total downtime, throttle */
1362 "<td class=ac>&nbsp;</td><td>%d</td>"
1363 "<td>%s</td>"
1364 "<td></td>"
1365 "</tr>",
1366 stats[ST_F_CHKDOWN].u.u32,
1367 stats[ST_F_DOWNTIME].type ? human_time(stats[ST_F_DOWNTIME].u.u32, 1) : "&nbsp;");
1368 }
1369 return 1;
1370}
1371
Willy Tarreau43241ff2019-10-09 11:27:51 +02001372static int stats_dump_one_line(const struct field *stats, struct proxy *px, struct appctx *appctx)
William Lallemand74c24fb2016-11-21 17:18:36 +01001373{
Simon Horman05ee2132017-01-04 09:37:25 +01001374 int ret;
1375
William Lallemand74c24fb2016-11-21 17:18:36 +01001376 if (appctx->ctx.stats.flags & STAT_FMT_HTML)
Willy Tarreau43241ff2019-10-09 11:27:51 +02001377 ret = stats_dump_fields_html(&trash, stats, appctx->ctx.stats.flags);
William Lallemand74c24fb2016-11-21 17:18:36 +01001378 else if (appctx->ctx.stats.flags & STAT_FMT_TYPED)
Willy Tarreau43241ff2019-10-09 11:27:51 +02001379 ret = stats_dump_fields_typed(&trash, stats, appctx->ctx.stats.flags);
Simon Horman05ee2132017-01-04 09:37:25 +01001380 else if (appctx->ctx.stats.flags & STAT_FMT_JSON)
Willy Tarreaub0ce3ad2019-10-09 11:19:29 +02001381 ret = stats_dump_fields_json(&trash, stats, appctx->ctx.stats.flags);
William Lallemand74c24fb2016-11-21 17:18:36 +01001382 else
Willy Tarreau43241ff2019-10-09 11:27:51 +02001383 ret = stats_dump_fields_csv(&trash, stats, appctx->ctx.stats.flags);
Simon Horman05ee2132017-01-04 09:37:25 +01001384
1385 if (ret)
1386 appctx->ctx.stats.flags |= STAT_STARTED;
1387
1388 return ret;
William Lallemand74c24fb2016-11-21 17:18:36 +01001389}
1390
1391/* Fill <stats> with the frontend statistics. <stats> is
1392 * preallocated array of length <len>. The length of the array
1393 * must be at least ST_F_TOTAL_FIELDS. If this length is less then
1394 * this value, the function returns 0, otherwise, it returns 1.
1395 */
1396int stats_fill_fe_stats(struct proxy *px, struct field *stats, int len)
1397{
1398 if (len < ST_F_TOTAL_FIELDS)
1399 return 0;
1400
1401 memset(stats, 0, sizeof(*stats) * len);
1402
1403 stats[ST_F_PXNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
1404 stats[ST_F_SVNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, "FRONTEND");
1405 stats[ST_F_MODE] = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
1406 stats[ST_F_SCUR] = mkf_u32(0, px->feconn);
1407 stats[ST_F_SMAX] = mkf_u32(FN_MAX, px->fe_counters.conn_max);
1408 stats[ST_F_SLIM] = mkf_u32(FO_CONFIG|FN_LIMIT, px->maxconn);
1409 stats[ST_F_STOT] = mkf_u64(FN_COUNTER, px->fe_counters.cum_sess);
1410 stats[ST_F_BIN] = mkf_u64(FN_COUNTER, px->fe_counters.bytes_in);
1411 stats[ST_F_BOUT] = mkf_u64(FN_COUNTER, px->fe_counters.bytes_out);
1412 stats[ST_F_DREQ] = mkf_u64(FN_COUNTER, px->fe_counters.denied_req);
1413 stats[ST_F_DRESP] = mkf_u64(FN_COUNTER, px->fe_counters.denied_resp);
1414 stats[ST_F_EREQ] = mkf_u64(FN_COUNTER, px->fe_counters.failed_req);
1415 stats[ST_F_DCON] = mkf_u64(FN_COUNTER, px->fe_counters.denied_conn);
1416 stats[ST_F_DSES] = mkf_u64(FN_COUNTER, px->fe_counters.denied_sess);
1417 stats[ST_F_STATUS] = mkf_str(FO_STATUS, px->state == PR_STREADY ? "OPEN" : px->state == PR_STFULL ? "FULL" : "STOP");
1418 stats[ST_F_PID] = mkf_u32(FO_KEY, relative_pid);
1419 stats[ST_F_IID] = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
1420 stats[ST_F_SID] = mkf_u32(FO_KEY|FS_SERVICE, 0);
1421 stats[ST_F_TYPE] = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_FE);
1422 stats[ST_F_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&px->fe_sess_per_sec));
1423 stats[ST_F_RATE_LIM] = mkf_u32(FO_CONFIG|FN_LIMIT, px->fe_sps_lim);
1424 stats[ST_F_RATE_MAX] = mkf_u32(FN_MAX, px->fe_counters.sps_max);
Tim Duesterhus3fd19732018-05-27 20:35:08 +02001425 stats[ST_F_WREW] = mkf_u64(FN_COUNTER, px->fe_counters.failed_rewrites);
Christopher Faulet0159ee42019-12-16 14:40:39 +01001426 stats[ST_F_EINT] = mkf_u64(FN_COUNTER, px->fe_counters.internal_errors);
William Lallemand74c24fb2016-11-21 17:18:36 +01001427
1428 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
1429 if (px->mode == PR_MODE_HTTP) {
1430 stats[ST_F_HRSP_1XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[1]);
1431 stats[ST_F_HRSP_2XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[2]);
1432 stats[ST_F_HRSP_3XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[3]);
1433 stats[ST_F_HRSP_4XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[4]);
1434 stats[ST_F_HRSP_5XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[5]);
1435 stats[ST_F_HRSP_OTHER] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[0]);
1436 stats[ST_F_INTERCEPTED] = mkf_u64(FN_COUNTER, px->fe_counters.intercepted_req);
Willy Tarreaua1214a52018-12-14 14:00:25 +01001437 stats[ST_F_CACHE_LOOKUPS] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.cache_lookups);
1438 stats[ST_F_CACHE_HITS] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.cache_hits);
William Lallemand74c24fb2016-11-21 17:18:36 +01001439 }
1440
1441 /* requests : req_rate, req_rate_max, req_tot, */
1442 stats[ST_F_REQ_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&px->fe_req_per_sec));
1443 stats[ST_F_REQ_RATE_MAX] = mkf_u32(FN_MAX, px->fe_counters.p.http.rps_max);
1444 stats[ST_F_REQ_TOT] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.cum_req);
1445
1446 /* compression: in, out, bypassed, responses */
1447 stats[ST_F_COMP_IN] = mkf_u64(FN_COUNTER, px->fe_counters.comp_in);
1448 stats[ST_F_COMP_OUT] = mkf_u64(FN_COUNTER, px->fe_counters.comp_out);
1449 stats[ST_F_COMP_BYP] = mkf_u64(FN_COUNTER, px->fe_counters.comp_byp);
1450 stats[ST_F_COMP_RSP] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.comp_rsp);
1451
1452 /* connections : conn_rate, conn_rate_max, conn_tot, conn_max */
1453 stats[ST_F_CONN_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&px->fe_conn_per_sec));
1454 stats[ST_F_CONN_RATE_MAX] = mkf_u32(FN_MAX, px->fe_counters.cps_max);
1455 stats[ST_F_CONN_TOT] = mkf_u64(FN_COUNTER, px->fe_counters.cum_conn);
1456
1457 return 1;
1458}
1459
1460/* Dumps a frontend's line to the trash for the current proxy <px> and uses
1461 * the state from stream interface <si>. The caller is responsible for clearing
1462 * the trash if needed. Returns non-zero if it emits anything, zero otherwise.
1463 */
1464static int stats_dump_fe_stats(struct stream_interface *si, struct proxy *px)
1465{
1466 struct appctx *appctx = __objt_appctx(si->end);
1467
1468 if (!(px->cap & PR_CAP_FE))
1469 return 0;
1470
1471 if ((appctx->ctx.stats.flags & STAT_BOUND) && !(appctx->ctx.stats.type & (1 << STATS_TYPE_FE)))
1472 return 0;
1473
1474 if (!stats_fill_fe_stats(px, stats, ST_F_TOTAL_FIELDS))
1475 return 0;
1476
Willy Tarreau43241ff2019-10-09 11:27:51 +02001477 return stats_dump_one_line(stats, px, appctx);
William Lallemand74c24fb2016-11-21 17:18:36 +01001478}
1479
1480/* Fill <stats> with the listener statistics. <stats> is
1481 * preallocated array of length <len>. The length of the array
1482 * must be at least ST_F_TOTAL_FIELDS. If this length is less
1483 * then this value, the function returns 0, otherwise, it
Willy Tarreau708c4162019-10-09 10:19:16 +02001484 * returns 1. <flags> can take the value STAT_SHLGNDS.
William Lallemand74c24fb2016-11-21 17:18:36 +01001485 */
1486int stats_fill_li_stats(struct proxy *px, struct listener *l, int flags,
1487 struct field *stats, int len)
1488{
Willy Tarreau83061a82018-07-13 11:56:34 +02001489 struct buffer *out = get_trash_chunk();
William Lallemand74c24fb2016-11-21 17:18:36 +01001490
1491 if (len < ST_F_TOTAL_FIELDS)
1492 return 0;
1493
1494 if (!l->counters)
1495 return 0;
1496
1497 chunk_reset(out);
1498 memset(stats, 0, sizeof(*stats) * len);
1499
1500 stats[ST_F_PXNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
1501 stats[ST_F_SVNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, l->name);
1502 stats[ST_F_MODE] = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
1503 stats[ST_F_SCUR] = mkf_u32(0, l->nbconn);
1504 stats[ST_F_SMAX] = mkf_u32(FN_MAX, l->counters->conn_max);
1505 stats[ST_F_SLIM] = mkf_u32(FO_CONFIG|FN_LIMIT, l->maxconn);
1506 stats[ST_F_STOT] = mkf_u64(FN_COUNTER, l->counters->cum_conn);
1507 stats[ST_F_BIN] = mkf_u64(FN_COUNTER, l->counters->bytes_in);
1508 stats[ST_F_BOUT] = mkf_u64(FN_COUNTER, l->counters->bytes_out);
1509 stats[ST_F_DREQ] = mkf_u64(FN_COUNTER, l->counters->denied_req);
1510 stats[ST_F_DRESP] = mkf_u64(FN_COUNTER, l->counters->denied_resp);
1511 stats[ST_F_EREQ] = mkf_u64(FN_COUNTER, l->counters->failed_req);
1512 stats[ST_F_DCON] = mkf_u64(FN_COUNTER, l->counters->denied_conn);
1513 stats[ST_F_DSES] = mkf_u64(FN_COUNTER, l->counters->denied_sess);
Willy Tarreaua8cf66b2019-02-27 16:49:00 +01001514 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 +01001515 stats[ST_F_PID] = mkf_u32(FO_KEY, relative_pid);
1516 stats[ST_F_IID] = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
1517 stats[ST_F_SID] = mkf_u32(FO_KEY|FS_SERVICE, l->luid);
1518 stats[ST_F_TYPE] = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_SO);
Tim Duesterhus3fd19732018-05-27 20:35:08 +02001519 stats[ST_F_WREW] = mkf_u64(FN_COUNTER, l->counters->failed_rewrites);
Christopher Faulet0159ee42019-12-16 14:40:39 +01001520 stats[ST_F_EINT] = mkf_u64(FN_COUNTER, l->counters->internal_errors);
William Lallemand74c24fb2016-11-21 17:18:36 +01001521
Willy Tarreau708c4162019-10-09 10:19:16 +02001522 if (flags & STAT_SHLGNDS) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001523 char str[INET6_ADDRSTRLEN];
1524 int port;
1525
1526 port = get_host_port(&l->addr);
1527 switch (addr_to_str(&l->addr, str, sizeof(str))) {
1528 case AF_INET:
1529 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
1530 chunk_appendf(out, "%s:%d", str, port);
1531 break;
1532 case AF_INET6:
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_UNIX:
1537 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, "unix");
1538 break;
1539 case -1:
1540 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
1541 chunk_strcat(out, strerror(errno));
1542 break;
1543 default: /* address family not supported */
1544 break;
1545 }
1546 }
1547
1548 return 1;
1549}
1550
1551/* Dumps a line for listener <l> and proxy <px> to the trash and uses the state
Willy Tarreau43241ff2019-10-09 11:27:51 +02001552 * from stream interface <si>. The caller is responsible for clearing the trash
1553 * if needed. Returns non-zero if it emits anything, zero otherwise.
William Lallemand74c24fb2016-11-21 17:18:36 +01001554 */
Willy Tarreau43241ff2019-10-09 11:27:51 +02001555static int stats_dump_li_stats(struct stream_interface *si, struct proxy *px, struct listener *l)
William Lallemand74c24fb2016-11-21 17:18:36 +01001556{
1557 struct appctx *appctx = __objt_appctx(si->end);
1558
Willy Tarreau43241ff2019-10-09 11:27:51 +02001559 if (!stats_fill_li_stats(px, l, appctx->ctx.stats.flags, stats, ST_F_TOTAL_FIELDS))
William Lallemand74c24fb2016-11-21 17:18:36 +01001560 return 0;
1561
Willy Tarreau43241ff2019-10-09 11:27:51 +02001562 return stats_dump_one_line(stats, px, appctx);
William Lallemand74c24fb2016-11-21 17:18:36 +01001563}
1564
1565enum srv_stats_state {
1566 SRV_STATS_STATE_DOWN = 0,
1567 SRV_STATS_STATE_DOWN_AGENT,
1568 SRV_STATS_STATE_GOING_UP,
1569 SRV_STATS_STATE_UP_GOING_DOWN,
1570 SRV_STATS_STATE_UP,
1571 SRV_STATS_STATE_NOLB_GOING_DOWN,
1572 SRV_STATS_STATE_NOLB,
1573 SRV_STATS_STATE_DRAIN_GOING_DOWN,
1574 SRV_STATS_STATE_DRAIN,
1575 SRV_STATS_STATE_DRAIN_AGENT,
1576 SRV_STATS_STATE_NO_CHECK,
1577
1578 SRV_STATS_STATE_COUNT, /* Must be last */
1579};
1580
1581static const char *srv_hlt_st[SRV_STATS_STATE_COUNT] = {
1582 [SRV_STATS_STATE_DOWN] = "DOWN",
1583 [SRV_STATS_STATE_DOWN_AGENT] = "DOWN (agent)",
1584 [SRV_STATS_STATE_GOING_UP] = "DOWN %d/%d",
1585 [SRV_STATS_STATE_UP_GOING_DOWN] = "UP %d/%d",
1586 [SRV_STATS_STATE_UP] = "UP",
1587 [SRV_STATS_STATE_NOLB_GOING_DOWN] = "NOLB %d/%d",
1588 [SRV_STATS_STATE_NOLB] = "NOLB",
1589 [SRV_STATS_STATE_DRAIN_GOING_DOWN] = "DRAIN %d/%d",
1590 [SRV_STATS_STATE_DRAIN] = "DRAIN",
1591 [SRV_STATS_STATE_DRAIN_AGENT] = "DRAIN (agent)",
1592 [SRV_STATS_STATE_NO_CHECK] = "no check"
1593};
1594
1595/* Fill <stats> with the server statistics. <stats> is
1596 * preallocated array of length <len>. The length of the array
1597 * must be at least ST_F_TOTAL_FIELDS. If this length is less
1598 * then this value, the function returns 0, otherwise, it
Willy Tarreau708c4162019-10-09 10:19:16 +02001599 * returns 1. <flags> can take the value STAT_SHLGNDS.
William Lallemand74c24fb2016-11-21 17:18:36 +01001600 */
1601int stats_fill_sv_stats(struct proxy *px, struct server *sv, int flags,
1602 struct field *stats, int len)
1603{
1604 struct server *via, *ref;
1605 char str[INET6_ADDRSTRLEN];
Willy Tarreau83061a82018-07-13 11:56:34 +02001606 struct buffer *out = get_trash_chunk();
William Lallemand74c24fb2016-11-21 17:18:36 +01001607 enum srv_stats_state state;
1608 char *fld_status;
Marcin Deraneka8dbdf32020-05-15 20:02:40 +02001609 long long srv_samples_counter;
1610 unsigned int srv_samples_window = TIME_STATS_SAMPLES;
William Lallemand74c24fb2016-11-21 17:18:36 +01001611
1612 if (len < ST_F_TOTAL_FIELDS)
1613 return 0;
1614
1615 memset(stats, 0, sizeof(*stats) * len);
1616
1617 /* we have "via" which is the tracked server as described in the configuration,
1618 * and "ref" which is the checked server and the end of the chain.
1619 */
1620 via = sv->track ? sv->track : sv;
1621 ref = via;
1622 while (ref->track)
1623 ref = ref->track;
1624
Emeric Brun52a91d32017-08-31 14:41:55 +02001625 if (sv->cur_state == SRV_ST_RUNNING || sv->cur_state == SRV_ST_STARTING) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001626 if ((ref->check.state & CHK_ST_ENABLED) &&
1627 (ref->check.health < ref->check.rise + ref->check.fall - 1)) {
1628 state = SRV_STATS_STATE_UP_GOING_DOWN;
1629 } else {
1630 state = SRV_STATS_STATE_UP;
1631 }
1632
Emeric Brun52a91d32017-08-31 14:41:55 +02001633 if (sv->cur_admin & SRV_ADMF_DRAIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001634 if (ref->agent.state & CHK_ST_ENABLED)
1635 state = SRV_STATS_STATE_DRAIN_AGENT;
1636 else if (state == SRV_STATS_STATE_UP_GOING_DOWN)
1637 state = SRV_STATS_STATE_DRAIN_GOING_DOWN;
1638 else
1639 state = SRV_STATS_STATE_DRAIN;
1640 }
1641
1642 if (state == SRV_STATS_STATE_UP && !(ref->check.state & CHK_ST_ENABLED)) {
1643 state = SRV_STATS_STATE_NO_CHECK;
1644 }
1645 }
Emeric Brun52a91d32017-08-31 14:41:55 +02001646 else if (sv->cur_state == SRV_ST_STOPPING) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001647 if ((!(sv->check.state & CHK_ST_ENABLED) && !sv->track) ||
1648 (ref->check.health == ref->check.rise + ref->check.fall - 1)) {
1649 state = SRV_STATS_STATE_NOLB;
1650 } else {
1651 state = SRV_STATS_STATE_NOLB_GOING_DOWN;
1652 }
1653 }
1654 else { /* stopped */
1655 if ((ref->agent.state & CHK_ST_ENABLED) && !ref->agent.health) {
1656 state = SRV_STATS_STATE_DOWN_AGENT;
1657 } else if ((ref->check.state & CHK_ST_ENABLED) && !ref->check.health) {
1658 state = SRV_STATS_STATE_DOWN; /* DOWN */
1659 } else if ((ref->agent.state & CHK_ST_ENABLED) || (ref->check.state & CHK_ST_ENABLED)) {
1660 state = SRV_STATS_STATE_GOING_UP;
1661 } else {
1662 state = SRV_STATS_STATE_DOWN; /* DOWN, unchecked */
1663 }
1664 }
1665
1666 chunk_reset(out);
1667
1668 stats[ST_F_PXNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
1669 stats[ST_F_SVNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, sv->id);
1670 stats[ST_F_MODE] = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
1671 stats[ST_F_QCUR] = mkf_u32(0, sv->nbpend);
1672 stats[ST_F_QMAX] = mkf_u32(FN_MAX, sv->counters.nbpend_max);
1673 stats[ST_F_SCUR] = mkf_u32(0, sv->cur_sess);
1674 stats[ST_F_SMAX] = mkf_u32(FN_MAX, sv->counters.cur_sess_max);
1675
1676 if (sv->maxconn)
1677 stats[ST_F_SLIM] = mkf_u32(FO_CONFIG|FN_LIMIT, sv->maxconn);
1678
Willy Tarreauf21d17b2019-09-08 09:24:56 +02001679 stats[ST_F_SRV_ICUR] = mkf_u32(0, sv->curr_idle_conns);
1680 if (sv->max_idle_conns != -1)
1681 stats[ST_F_SRV_ILIM] = mkf_u32(FO_CONFIG|FN_LIMIT, sv->max_idle_conns);
1682
William Lallemand74c24fb2016-11-21 17:18:36 +01001683 stats[ST_F_STOT] = mkf_u64(FN_COUNTER, sv->counters.cum_sess);
1684 stats[ST_F_BIN] = mkf_u64(FN_COUNTER, sv->counters.bytes_in);
1685 stats[ST_F_BOUT] = mkf_u64(FN_COUNTER, sv->counters.bytes_out);
Christopher Fauleta08546b2019-12-16 16:07:34 +01001686 stats[ST_F_DRESP] = mkf_u64(FN_COUNTER, sv->counters.denied_resp);
William Lallemand74c24fb2016-11-21 17:18:36 +01001687 stats[ST_F_ECON] = mkf_u64(FN_COUNTER, sv->counters.failed_conns);
1688 stats[ST_F_ERESP] = mkf_u64(FN_COUNTER, sv->counters.failed_resp);
1689 stats[ST_F_WRETR] = mkf_u64(FN_COUNTER, sv->counters.retries);
1690 stats[ST_F_WREDIS] = mkf_u64(FN_COUNTER, sv->counters.redispatches);
Tim Duesterhus3fd19732018-05-27 20:35:08 +02001691 stats[ST_F_WREW] = mkf_u64(FN_COUNTER, sv->counters.failed_rewrites);
Christopher Faulet0159ee42019-12-16 14:40:39 +01001692 stats[ST_F_EINT] = mkf_u64(FN_COUNTER, sv->counters.internal_errors);
Willy Tarreauf1573842018-12-14 11:35:36 +01001693 stats[ST_F_CONNECT] = mkf_u64(FN_COUNTER, sv->counters.connect);
1694 stats[ST_F_REUSE] = mkf_u64(FN_COUNTER, sv->counters.reuse);
William Lallemand74c24fb2016-11-21 17:18:36 +01001695
1696 /* status */
1697 fld_status = chunk_newstr(out);
Emeric Brun52a91d32017-08-31 14:41:55 +02001698 if (sv->cur_admin & SRV_ADMF_RMAINT)
William Lallemand74c24fb2016-11-21 17:18:36 +01001699 chunk_appendf(out, "MAINT (resolution)");
Emeric Brun52a91d32017-08-31 14:41:55 +02001700 else if (sv->cur_admin & SRV_ADMF_IMAINT)
William Lallemand74c24fb2016-11-21 17:18:36 +01001701 chunk_appendf(out, "MAINT (via %s/%s)", via->proxy->id, via->id);
Emeric Brun52a91d32017-08-31 14:41:55 +02001702 else if (sv->cur_admin & SRV_ADMF_MAINT)
William Lallemand74c24fb2016-11-21 17:18:36 +01001703 chunk_appendf(out, "MAINT");
1704 else
1705 chunk_appendf(out,
1706 srv_hlt_st[state],
Emeric Brun52a91d32017-08-31 14:41:55 +02001707 (ref->cur_state != SRV_ST_STOPPED) ? (ref->check.health - ref->check.rise + 1) : (ref->check.health),
1708 (ref->cur_state != SRV_ST_STOPPED) ? (ref->check.fall) : (ref->check.rise));
William Lallemand74c24fb2016-11-21 17:18:36 +01001709
1710 stats[ST_F_STATUS] = mkf_str(FO_STATUS, fld_status);
1711 stats[ST_F_LASTCHG] = mkf_u32(FN_AGE, now.tv_sec - sv->last_change);
Emeric Brun52a91d32017-08-31 14:41:55 +02001712 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 +01001713 stats[ST_F_ACT] = mkf_u32(FO_STATUS, (sv->flags & SRV_F_BACKUP) ? 0 : 1);
1714 stats[ST_F_BCK] = mkf_u32(FO_STATUS, (sv->flags & SRV_F_BACKUP) ? 1 : 0);
1715
1716 /* check failures: unique, fatal; last change, total downtime */
1717 if (sv->check.state & CHK_ST_ENABLED) {
1718 stats[ST_F_CHKFAIL] = mkf_u64(FN_COUNTER, sv->counters.failed_checks);
1719 stats[ST_F_CHKDOWN] = mkf_u64(FN_COUNTER, sv->counters.down_trans);
1720 stats[ST_F_DOWNTIME] = mkf_u32(FN_COUNTER, srv_downtime(sv));
1721 }
1722
1723 if (sv->maxqueue)
1724 stats[ST_F_QLIMIT] = mkf_u32(FO_CONFIG|FS_SERVICE, sv->maxqueue);
1725
1726 stats[ST_F_PID] = mkf_u32(FO_KEY, relative_pid);
1727 stats[ST_F_IID] = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
1728 stats[ST_F_SID] = mkf_u32(FO_KEY|FS_SERVICE, sv->puid);
1729
Emeric Brun52a91d32017-08-31 14:41:55 +02001730 if (sv->cur_state == SRV_ST_STARTING && !server_is_draining(sv))
William Lallemand74c24fb2016-11-21 17:18:36 +01001731 stats[ST_F_THROTTLE] = mkf_u32(FN_AVG, server_throttle_rate(sv));
1732
1733 stats[ST_F_LBTOT] = mkf_u64(FN_COUNTER, sv->counters.cum_lbconn);
1734
1735 if (sv->track) {
1736 char *fld_track = chunk_newstr(out);
1737
1738 chunk_appendf(out, "%s/%s", sv->track->proxy->id, sv->track->id);
1739 stats[ST_F_TRACKED] = mkf_str(FO_CONFIG|FN_NAME|FS_SERVICE, fld_track);
1740 }
1741
1742 stats[ST_F_TYPE] = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_SV);
1743 stats[ST_F_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&sv->sess_per_sec));
1744 stats[ST_F_RATE_MAX] = mkf_u32(FN_MAX, sv->counters.sps_max);
1745
1746 if ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED) {
1747 const char *fld_chksts;
1748
1749 fld_chksts = chunk_newstr(out);
1750 chunk_strcat(out, "* "); // for check in progress
1751 chunk_strcat(out, get_check_status_info(sv->check.status));
1752 if (!(sv->check.state & CHK_ST_INPROGRESS))
1753 fld_chksts += 2; // skip "* "
1754 stats[ST_F_CHECK_STATUS] = mkf_str(FN_OUTPUT, fld_chksts);
1755
1756 if (sv->check.status >= HCHK_STATUS_L57DATA)
1757 stats[ST_F_CHECK_CODE] = mkf_u32(FN_OUTPUT, sv->check.code);
1758
1759 if (sv->check.status >= HCHK_STATUS_CHECKED)
1760 stats[ST_F_CHECK_DURATION] = mkf_u64(FN_DURATION, sv->check.duration);
1761
1762 stats[ST_F_CHECK_DESC] = mkf_str(FN_OUTPUT, get_check_status_description(sv->check.status));
1763 stats[ST_F_LAST_CHK] = mkf_str(FN_OUTPUT, sv->check.desc);
1764 stats[ST_F_CHECK_RISE] = mkf_u32(FO_CONFIG|FS_SERVICE, ref->check.rise);
1765 stats[ST_F_CHECK_FALL] = mkf_u32(FO_CONFIG|FS_SERVICE, ref->check.fall);
1766 stats[ST_F_CHECK_HEALTH] = mkf_u32(FO_CONFIG|FS_SERVICE, ref->check.health);
1767 }
1768
1769 if ((sv->agent.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED) {
1770 const char *fld_chksts;
1771
1772 fld_chksts = chunk_newstr(out);
1773 chunk_strcat(out, "* "); // for check in progress
1774 chunk_strcat(out, get_check_status_info(sv->agent.status));
1775 if (!(sv->agent.state & CHK_ST_INPROGRESS))
1776 fld_chksts += 2; // skip "* "
1777 stats[ST_F_AGENT_STATUS] = mkf_str(FN_OUTPUT, fld_chksts);
1778
1779 if (sv->agent.status >= HCHK_STATUS_L57DATA)
1780 stats[ST_F_AGENT_CODE] = mkf_u32(FN_OUTPUT, sv->agent.code);
1781
1782 if (sv->agent.status >= HCHK_STATUS_CHECKED)
1783 stats[ST_F_AGENT_DURATION] = mkf_u64(FN_DURATION, sv->agent.duration);
1784
1785 stats[ST_F_AGENT_DESC] = mkf_str(FN_OUTPUT, get_check_status_description(sv->agent.status));
1786 stats[ST_F_LAST_AGT] = mkf_str(FN_OUTPUT, sv->agent.desc);
1787 stats[ST_F_AGENT_RISE] = mkf_u32(FO_CONFIG|FS_SERVICE, sv->agent.rise);
1788 stats[ST_F_AGENT_FALL] = mkf_u32(FO_CONFIG|FS_SERVICE, sv->agent.fall);
1789 stats[ST_F_AGENT_HEALTH] = mkf_u32(FO_CONFIG|FS_SERVICE, sv->agent.health);
1790 }
1791
1792 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
1793 if (px->mode == PR_MODE_HTTP) {
Marcin Deranek3c27dda2020-05-15 18:32:51 +02001794 stats[ST_F_REQ_TOT] = mkf_u64(FN_COUNTER, sv->counters.p.http.cum_req);
William Lallemand74c24fb2016-11-21 17:18:36 +01001795 stats[ST_F_HRSP_1XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[1]);
1796 stats[ST_F_HRSP_2XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[2]);
1797 stats[ST_F_HRSP_3XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[3]);
1798 stats[ST_F_HRSP_4XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[4]);
1799 stats[ST_F_HRSP_5XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[5]);
1800 stats[ST_F_HRSP_OTHER] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[0]);
1801 }
1802
1803 if (ref->observe)
1804 stats[ST_F_HANAFAIL] = mkf_u64(FN_COUNTER, sv->counters.failed_hana);
1805
1806 stats[ST_F_CLI_ABRT] = mkf_u64(FN_COUNTER, sv->counters.cli_aborts);
1807 stats[ST_F_SRV_ABRT] = mkf_u64(FN_COUNTER, sv->counters.srv_aborts);
1808 stats[ST_F_LASTSESS] = mkf_s32(FN_AGE, srv_lastsession(sv));
1809
Marcin Deraneka8dbdf32020-05-15 20:02:40 +02001810 srv_samples_counter = (px->mode == PR_MODE_HTTP) ? sv->counters.p.http.cum_req : sv->counters.cum_lbconn;
1811 if (srv_samples_counter < TIME_STATS_SAMPLES && srv_samples_counter > 0)
1812 srv_samples_window = srv_samples_counter;
1813
1814 stats[ST_F_QTIME] = mkf_u32(FN_AVG, swrate_avg(sv->counters.q_time, srv_samples_window));
1815 stats[ST_F_CTIME] = mkf_u32(FN_AVG, swrate_avg(sv->counters.c_time, srv_samples_window));
1816 stats[ST_F_RTIME] = mkf_u32(FN_AVG, swrate_avg(sv->counters.d_time, srv_samples_window));
1817 stats[ST_F_TTIME] = mkf_u32(FN_AVG, swrate_avg(sv->counters.t_time, srv_samples_window));
William Lallemand74c24fb2016-11-21 17:18:36 +01001818
Christopher Faulet0d1c2a62019-11-08 14:59:51 +01001819 stats[ST_F_QT_MAX] = mkf_u32(FN_MAX, sv->counters.qtime_max);
1820 stats[ST_F_CT_MAX] = mkf_u32(FN_MAX, sv->counters.ctime_max);
1821 stats[ST_F_RT_MAX] = mkf_u32(FN_MAX, sv->counters.dtime_max);
1822 stats[ST_F_TT_MAX] = mkf_u32(FN_MAX, sv->counters.ttime_max);
1823
Willy Tarreau708c4162019-10-09 10:19:16 +02001824 if (flags & STAT_SHLGNDS) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001825 switch (addr_to_str(&sv->addr, str, sizeof(str))) {
1826 case AF_INET:
1827 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
Willy Tarreau04276f32017-01-06 17:41:29 +01001828 chunk_appendf(out, "%s:%d", str, sv->svc_port);
William Lallemand74c24fb2016-11-21 17:18:36 +01001829 break;
1830 case AF_INET6:
1831 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
Willy Tarreau04276f32017-01-06 17:41:29 +01001832 chunk_appendf(out, "[%s]:%d", str, sv->svc_port);
William Lallemand74c24fb2016-11-21 17:18:36 +01001833 break;
1834 case AF_UNIX:
1835 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, "unix");
1836 break;
1837 case -1:
1838 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
1839 chunk_strcat(out, strerror(errno));
1840 break;
1841 default: /* address family not supported */
1842 break;
1843 }
1844
1845 if (sv->cookie)
1846 stats[ST_F_COOKIE] = mkf_str(FO_CONFIG|FN_NAME|FS_SERVICE, sv->cookie);
1847 }
1848
1849 return 1;
1850}
1851
1852/* Dumps a line for server <sv> and proxy <px> to the trash and uses the state
Willy Tarreau43241ff2019-10-09 11:27:51 +02001853 * from stream interface <si>, and server state <state>. The caller is
1854 * responsible for clearing the trash if needed. Returns non-zero if it emits
1855 * anything, zero otherwise.
William Lallemand74c24fb2016-11-21 17:18:36 +01001856 */
Willy Tarreau43241ff2019-10-09 11:27:51 +02001857static int stats_dump_sv_stats(struct stream_interface *si, struct proxy *px, struct server *sv)
William Lallemand74c24fb2016-11-21 17:18:36 +01001858{
1859 struct appctx *appctx = __objt_appctx(si->end);
1860
Willy Tarreau43241ff2019-10-09 11:27:51 +02001861 if (!stats_fill_sv_stats(px, sv, appctx->ctx.stats.flags, stats, ST_F_TOTAL_FIELDS))
William Lallemand74c24fb2016-11-21 17:18:36 +01001862 return 0;
1863
Willy Tarreau43241ff2019-10-09 11:27:51 +02001864 return stats_dump_one_line(stats, px, appctx);
William Lallemand74c24fb2016-11-21 17:18:36 +01001865}
1866
1867/* Fill <stats> with the backend statistics. <stats> is
1868 * preallocated array of length <len>. The length of the array
1869 * must be at least ST_F_TOTAL_FIELDS. If this length is less
1870 * then this value, the function returns 0, otherwise, it
Willy Tarreau708c4162019-10-09 10:19:16 +02001871 * returns 1. <flags> can take the value STAT_SHLGNDS.
William Lallemand74c24fb2016-11-21 17:18:36 +01001872 */
1873int stats_fill_be_stats(struct proxy *px, int flags, struct field *stats, int len)
1874{
Marcin Deraneka8dbdf32020-05-15 20:02:40 +02001875 long long be_samples_counter;
1876 unsigned int be_samples_window = TIME_STATS_SAMPLES;
1877
William Lallemand74c24fb2016-11-21 17:18:36 +01001878 if (len < ST_F_TOTAL_FIELDS)
1879 return 0;
1880
1881 memset(stats, 0, sizeof(*stats) * len);
1882
1883 stats[ST_F_PXNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
1884 stats[ST_F_SVNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, "BACKEND");
1885 stats[ST_F_MODE] = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
1886 stats[ST_F_QCUR] = mkf_u32(0, px->nbpend);
1887 stats[ST_F_QMAX] = mkf_u32(FN_MAX, px->be_counters.nbpend_max);
Thierry FOURNIER0ff98a42016-12-19 16:50:42 +01001888 stats[ST_F_SCUR] = mkf_u32(0, px->beconn);
William Lallemand74c24fb2016-11-21 17:18:36 +01001889 stats[ST_F_SMAX] = mkf_u32(FN_MAX, px->be_counters.conn_max);
1890 stats[ST_F_SLIM] = mkf_u32(FO_CONFIG|FN_LIMIT, px->fullconn);
1891 stats[ST_F_STOT] = mkf_u64(FN_COUNTER, px->be_counters.cum_conn);
1892 stats[ST_F_BIN] = mkf_u64(FN_COUNTER, px->be_counters.bytes_in);
1893 stats[ST_F_BOUT] = mkf_u64(FN_COUNTER, px->be_counters.bytes_out);
1894 stats[ST_F_DREQ] = mkf_u64(FN_COUNTER, px->be_counters.denied_req);
1895 stats[ST_F_DRESP] = mkf_u64(FN_COUNTER, px->be_counters.denied_resp);
1896 stats[ST_F_ECON] = mkf_u64(FN_COUNTER, px->be_counters.failed_conns);
1897 stats[ST_F_ERESP] = mkf_u64(FN_COUNTER, px->be_counters.failed_resp);
1898 stats[ST_F_WRETR] = mkf_u64(FN_COUNTER, px->be_counters.retries);
1899 stats[ST_F_WREDIS] = mkf_u64(FN_COUNTER, px->be_counters.redispatches);
Tim Duesterhus3fd19732018-05-27 20:35:08 +02001900 stats[ST_F_WREW] = mkf_u64(FN_COUNTER, px->be_counters.failed_rewrites);
Christopher Faulet0159ee42019-12-16 14:40:39 +01001901 stats[ST_F_EINT] = mkf_u64(FN_COUNTER, px->be_counters.internal_errors);
Willy Tarreauf1573842018-12-14 11:35:36 +01001902 stats[ST_F_CONNECT] = mkf_u64(FN_COUNTER, px->be_counters.connect);
1903 stats[ST_F_REUSE] = mkf_u64(FN_COUNTER, px->be_counters.reuse);
William Lallemand74c24fb2016-11-21 17:18:36 +01001904 stats[ST_F_STATUS] = mkf_str(FO_STATUS, (px->lbprm.tot_weight > 0 || !px->srv) ? "UP" : "DOWN");
1905 stats[ST_F_WEIGHT] = mkf_u32(FN_AVG, (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv);
1906 stats[ST_F_ACT] = mkf_u32(0, px->srv_act);
1907 stats[ST_F_BCK] = mkf_u32(0, px->srv_bck);
1908 stats[ST_F_CHKDOWN] = mkf_u64(FN_COUNTER, px->down_trans);
1909 stats[ST_F_LASTCHG] = mkf_u32(FN_AGE, now.tv_sec - px->last_change);
1910 if (px->srv)
1911 stats[ST_F_DOWNTIME] = mkf_u32(FN_COUNTER, be_downtime(px));
1912
1913 stats[ST_F_PID] = mkf_u32(FO_KEY, relative_pid);
1914 stats[ST_F_IID] = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
1915 stats[ST_F_SID] = mkf_u32(FO_KEY|FS_SERVICE, 0);
1916 stats[ST_F_LBTOT] = mkf_u64(FN_COUNTER, px->be_counters.cum_lbconn);
1917 stats[ST_F_TYPE] = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_BE);
1918 stats[ST_F_RATE] = mkf_u32(0, read_freq_ctr(&px->be_sess_per_sec));
1919 stats[ST_F_RATE_MAX] = mkf_u32(0, px->be_counters.sps_max);
1920
Willy Tarreau708c4162019-10-09 10:19:16 +02001921 if (flags & STAT_SHLGNDS) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001922 if (px->cookie_name)
1923 stats[ST_F_COOKIE] = mkf_str(FO_CONFIG|FN_NAME|FS_SERVICE, px->cookie_name);
1924 stats[ST_F_ALGO] = mkf_str(FO_CONFIG|FS_SERVICE, backend_lb_algo_str(px->lbprm.algo & BE_LB_ALGO));
1925 }
1926
1927 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
1928 if (px->mode == PR_MODE_HTTP) {
1929 stats[ST_F_REQ_TOT] = mkf_u64(FN_COUNTER, px->be_counters.p.http.cum_req);
1930 stats[ST_F_HRSP_1XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[1]);
1931 stats[ST_F_HRSP_2XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[2]);
1932 stats[ST_F_HRSP_3XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[3]);
1933 stats[ST_F_HRSP_4XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[4]);
1934 stats[ST_F_HRSP_5XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[5]);
1935 stats[ST_F_HRSP_OTHER] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[0]);
Willy Tarreaua1214a52018-12-14 14:00:25 +01001936 stats[ST_F_CACHE_LOOKUPS] = mkf_u64(FN_COUNTER, px->be_counters.p.http.cache_lookups);
1937 stats[ST_F_CACHE_HITS] = mkf_u64(FN_COUNTER, px->be_counters.p.http.cache_hits);
William Lallemand74c24fb2016-11-21 17:18:36 +01001938 }
1939
1940 stats[ST_F_CLI_ABRT] = mkf_u64(FN_COUNTER, px->be_counters.cli_aborts);
1941 stats[ST_F_SRV_ABRT] = mkf_u64(FN_COUNTER, px->be_counters.srv_aborts);
1942
1943 /* compression: in, out, bypassed, responses */
1944 stats[ST_F_COMP_IN] = mkf_u64(FN_COUNTER, px->be_counters.comp_in);
1945 stats[ST_F_COMP_OUT] = mkf_u64(FN_COUNTER, px->be_counters.comp_out);
1946 stats[ST_F_COMP_BYP] = mkf_u64(FN_COUNTER, px->be_counters.comp_byp);
1947 stats[ST_F_COMP_RSP] = mkf_u64(FN_COUNTER, px->be_counters.p.http.comp_rsp);
1948 stats[ST_F_LASTSESS] = mkf_s32(FN_AGE, be_lastsession(px));
1949
Marcin Deraneka8dbdf32020-05-15 20:02:40 +02001950 be_samples_counter = (px->mode == PR_MODE_HTTP) ? px->be_counters.p.http.cum_req : px->be_counters.cum_lbconn;
1951 if (be_samples_counter < TIME_STATS_SAMPLES && be_samples_counter > 0)
1952 be_samples_window = be_samples_counter;
1953
1954 stats[ST_F_QTIME] = mkf_u32(FN_AVG, swrate_avg(px->be_counters.q_time, be_samples_window));
1955 stats[ST_F_CTIME] = mkf_u32(FN_AVG, swrate_avg(px->be_counters.c_time, be_samples_window));
1956 stats[ST_F_RTIME] = mkf_u32(FN_AVG, swrate_avg(px->be_counters.d_time, be_samples_window));
1957 stats[ST_F_TTIME] = mkf_u32(FN_AVG, swrate_avg(px->be_counters.t_time, be_samples_window));
William Lallemand74c24fb2016-11-21 17:18:36 +01001958
Christopher Faulet0d1c2a62019-11-08 14:59:51 +01001959 stats[ST_F_QT_MAX] = mkf_u32(FN_MAX, px->be_counters.qtime_max);
1960 stats[ST_F_CT_MAX] = mkf_u32(FN_MAX, px->be_counters.ctime_max);
1961 stats[ST_F_RT_MAX] = mkf_u32(FN_MAX, px->be_counters.dtime_max);
1962 stats[ST_F_TT_MAX] = mkf_u32(FN_MAX, px->be_counters.ttime_max);
1963
William Lallemand74c24fb2016-11-21 17:18:36 +01001964 return 1;
1965}
1966
1967/* Dumps a line for backend <px> to the trash for and uses the state from stream
Willy Tarreau43241ff2019-10-09 11:27:51 +02001968 * interface <si>. The caller is responsible for clearing the trash if needed.
1969 * Returns non-zero if it emits anything, zero otherwise.
William Lallemand74c24fb2016-11-21 17:18:36 +01001970 */
Willy Tarreau43241ff2019-10-09 11:27:51 +02001971static int stats_dump_be_stats(struct stream_interface *si, struct proxy *px)
William Lallemand74c24fb2016-11-21 17:18:36 +01001972{
1973 struct appctx *appctx = __objt_appctx(si->end);
1974
1975 if (!(px->cap & PR_CAP_BE))
1976 return 0;
1977
1978 if ((appctx->ctx.stats.flags & STAT_BOUND) && !(appctx->ctx.stats.type & (1 << STATS_TYPE_BE)))
1979 return 0;
1980
Willy Tarreau43241ff2019-10-09 11:27:51 +02001981 if (!stats_fill_be_stats(px, appctx->ctx.stats.flags, stats, ST_F_TOTAL_FIELDS))
William Lallemand74c24fb2016-11-21 17:18:36 +01001982 return 0;
1983
Willy Tarreau43241ff2019-10-09 11:27:51 +02001984 return stats_dump_one_line(stats, px, appctx);
William Lallemand74c24fb2016-11-21 17:18:36 +01001985}
1986
1987/* Dumps the HTML table header for proxy <px> to the trash for and uses the state from
1988 * stream interface <si> and per-uri parameters <uri>. The caller is responsible
1989 * for clearing the trash if needed.
1990 */
Willy Tarreau676c29e2019-10-09 10:50:01 +02001991static void stats_dump_html_px_hdr(struct stream_interface *si, struct proxy *px)
William Lallemand74c24fb2016-11-21 17:18:36 +01001992{
1993 struct appctx *appctx = __objt_appctx(si->end);
1994 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
1995
1996 if (px->cap & PR_CAP_BE && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
1997 /* A form to enable/disable this proxy servers */
1998
1999 /* scope_txt = search pattern + search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
2000 scope_txt[0] = 0;
2001 if (appctx->ctx.stats.scope_len) {
Christopher Fauleted7a0662019-01-14 11:07:34 +01002002 const char *scope_ptr = stats_scope_ptr(appctx, si);
2003
William Lallemand74c24fb2016-11-21 17:18:36 +01002004 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Christopher Fauleted7a0662019-01-14 11:07:34 +01002005 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), scope_ptr, appctx->ctx.stats.scope_len);
William Lallemand74c24fb2016-11-21 17:18:36 +01002006 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
2007 }
2008
2009 chunk_appendf(&trash,
2010 "<form method=\"post\">");
2011 }
2012
2013 /* print a new table */
2014 chunk_appendf(&trash,
2015 "<table class=\"tbl\" width=\"100%%\">\n"
2016 "<tr class=\"titre\">"
2017 "<th class=\"pxname\" width=\"10%%\">");
2018
2019 chunk_appendf(&trash,
2020 "<a name=\"%s\"></a>%s"
2021 "<a class=px href=\"#%s\">%s</a>",
2022 px->id,
Willy Tarreau676c29e2019-10-09 10:50:01 +02002023 (appctx->ctx.stats.flags & STAT_SHLGNDS) ? "<u>":"",
William Lallemand74c24fb2016-11-21 17:18:36 +01002024 px->id, px->id);
2025
Willy Tarreau676c29e2019-10-09 10:50:01 +02002026 if (appctx->ctx.stats.flags & STAT_SHLGNDS) {
William Lallemand74c24fb2016-11-21 17:18:36 +01002027 /* cap, mode, id */
2028 chunk_appendf(&trash, "<div class=tips>cap: %s, mode: %s, id: %d",
2029 proxy_cap_str(px->cap), proxy_mode_str(px->mode),
2030 px->uuid);
2031 chunk_appendf(&trash, "</div>");
2032 }
2033
2034 chunk_appendf(&trash,
2035 "%s</th>"
2036 "<th class=\"%s\" width=\"90%%\">%s</th>"
2037 "</tr>\n"
2038 "</table>\n"
2039 "<table class=\"tbl\" width=\"100%%\">\n"
2040 "<tr class=\"titre\">",
Willy Tarreau676c29e2019-10-09 10:50:01 +02002041 (appctx->ctx.stats.flags & STAT_SHLGNDS) ? "</u>":"",
William Lallemand74c24fb2016-11-21 17:18:36 +01002042 px->desc ? "desc" : "empty", px->desc ? px->desc : "");
2043
Christopher Fauletbc271ec2019-12-02 11:29:04 +01002044 if (appctx->ctx.stats.flags & STAT_ADMIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +01002045 /* Column heading for Enable or Disable server */
Christopher Fauletbc271ec2019-12-02 11:29:04 +01002046 if ((px->cap & PR_CAP_BE) && px->srv)
2047 chunk_appendf(&trash,
2048 "<th rowspan=2 width=1><input type=\"checkbox\" "
2049 "onclick=\"for(c in document.getElementsByClassName('%s-checkbox')) "
2050 "document.getElementsByClassName('%s-checkbox').item(c).checked = this.checked\"></th>",
2051 px->id,
2052 px->id);
2053 else
2054 chunk_appendf(&trash, "<th rowspan=2></th>");
William Lallemand74c24fb2016-11-21 17:18:36 +01002055 }
2056
2057 chunk_appendf(&trash,
2058 "<th rowspan=2></th>"
2059 "<th colspan=3>Queue</th>"
2060 "<th colspan=3>Session rate</th><th colspan=6>Sessions</th>"
2061 "<th colspan=2>Bytes</th><th colspan=2>Denied</th>"
2062 "<th colspan=3>Errors</th><th colspan=2>Warnings</th>"
2063 "<th colspan=9>Server</th>"
2064 "</tr>\n"
2065 "<tr class=\"titre\">"
2066 "<th>Cur</th><th>Max</th><th>Limit</th>"
2067 "<th>Cur</th><th>Max</th><th>Limit</th><th>Cur</th><th>Max</th>"
2068 "<th>Limit</th><th>Total</th><th>LbTot</th><th>Last</th><th>In</th><th>Out</th>"
2069 "<th>Req</th><th>Resp</th><th>Req</th><th>Conn</th>"
2070 "<th>Resp</th><th>Retr</th><th>Redis</th>"
2071 "<th>Status</th><th>LastChk</th><th>Wght</th><th>Act</th>"
2072 "<th>Bck</th><th>Chk</th><th>Dwn</th><th>Dwntme</th>"
2073 "<th>Thrtle</th>\n"
2074 "</tr>");
2075}
2076
2077/* Dumps the HTML table trailer for proxy <px> to the trash for and uses the state from
2078 * stream interface <si>. The caller is responsible for clearing the trash if needed.
2079 */
2080static void stats_dump_html_px_end(struct stream_interface *si, struct proxy *px)
2081{
2082 struct appctx *appctx = __objt_appctx(si->end);
2083 chunk_appendf(&trash, "</table>");
2084
2085 if ((px->cap & PR_CAP_BE) && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
2086 /* close the form used to enable/disable this proxy servers */
2087 chunk_appendf(&trash,
2088 "Choose the action to perform on the checked servers : "
2089 "<select name=action>"
2090 "<option value=\"\"></option>"
2091 "<option value=\"ready\">Set state to READY</option>"
2092 "<option value=\"drain\">Set state to DRAIN</option>"
2093 "<option value=\"maint\">Set state to MAINT</option>"
2094 "<option value=\"dhlth\">Health: disable checks</option>"
2095 "<option value=\"ehlth\">Health: enable checks</option>"
2096 "<option value=\"hrunn\">Health: force UP</option>"
2097 "<option value=\"hnolb\">Health: force NOLB</option>"
2098 "<option value=\"hdown\">Health: force DOWN</option>"
2099 "<option value=\"dagent\">Agent: disable checks</option>"
2100 "<option value=\"eagent\">Agent: enable checks</option>"
2101 "<option value=\"arunn\">Agent: force UP</option>"
2102 "<option value=\"adown\">Agent: force DOWN</option>"
2103 "<option value=\"shutdown\">Kill Sessions</option>"
2104 "</select>"
2105 "<input type=\"hidden\" name=\"b\" value=\"#%d\">"
2106 "&nbsp;<input type=\"submit\" value=\"Apply\">"
2107 "</form>",
2108 px->uuid);
2109 }
2110
2111 chunk_appendf(&trash, "<p>\n");
2112}
2113
2114/*
2115 * Dumps statistics for a proxy. The output is sent to the stream interface's
2116 * input buffer. Returns 0 if it had to stop dumping data because of lack of
2117 * buffer space, or non-zero if everything completed. This function is used
2118 * both by the CLI and the HTTP entry points, and is able to dump the output
2119 * in HTML or CSV formats. If the later, <uri> must be NULL.
2120 */
Christopher Fauletef779222018-10-31 08:47:01 +01002121int stats_dump_proxy_to_buffer(struct stream_interface *si, struct htx *htx,
2122 struct proxy *px, struct uri_auth *uri)
William Lallemand74c24fb2016-11-21 17:18:36 +01002123{
2124 struct appctx *appctx = __objt_appctx(si->end);
2125 struct stream *s = si_strm(si);
2126 struct channel *rep = si_ic(si);
2127 struct server *sv, *svs; /* server and server-state, server-state=server or server->track */
2128 struct listener *l;
William Lallemand74c24fb2016-11-21 17:18:36 +01002129
2130 chunk_reset(&trash);
2131
2132 switch (appctx->ctx.stats.px_st) {
2133 case STAT_PX_ST_INIT:
2134 /* we are on a new proxy */
2135 if (uri && uri->scope) {
2136 /* we have a limited scope, we have to check the proxy name */
2137 struct stat_scope *scope;
2138 int len;
2139
2140 len = strlen(px->id);
2141 scope = uri->scope;
2142
2143 while (scope) {
2144 /* match exact proxy name */
2145 if (scope->px_len == len && !memcmp(px->id, scope->px_id, len))
2146 break;
2147
2148 /* match '.' which means 'self' proxy */
2149 if (!strcmp(scope->px_id, ".") && px == s->be)
2150 break;
2151 scope = scope->next;
2152 }
2153
2154 /* proxy name not found : don't dump anything */
2155 if (scope == NULL)
2156 return 1;
2157 }
2158
2159 /* if the user has requested a limited output and the proxy
2160 * name does not match, skip it.
2161 */
Christopher Fauleted7a0662019-01-14 11:07:34 +01002162 if (appctx->ctx.stats.scope_len) {
2163 const char *scope_ptr = stats_scope_ptr(appctx, si);
2164
2165 if (strnistr(px->id, strlen(px->id), scope_ptr, appctx->ctx.stats.scope_len) == NULL)
2166 return 1;
2167 }
William Lallemand74c24fb2016-11-21 17:18:36 +01002168
2169 if ((appctx->ctx.stats.flags & STAT_BOUND) &&
2170 (appctx->ctx.stats.iid != -1) &&
2171 (px->uuid != appctx->ctx.stats.iid))
2172 return 1;
2173
2174 appctx->ctx.stats.px_st = STAT_PX_ST_TH;
2175 /* fall through */
2176
2177 case STAT_PX_ST_TH:
2178 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreau676c29e2019-10-09 10:50:01 +02002179 stats_dump_html_px_hdr(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.px_st = STAT_PX_ST_FE;
2185 /* fall through */
2186
2187 case STAT_PX_ST_FE:
2188 /* print the frontend */
2189 if (stats_dump_fe_stats(si, px)) {
Christopher Fauletef779222018-10-31 08:47:01 +01002190 if (!stats_putchk(rep, htx, &trash))
2191 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002192 }
2193
2194 appctx->ctx.stats.l = px->conf.listeners.n;
2195 appctx->ctx.stats.px_st = STAT_PX_ST_LI;
2196 /* fall through */
2197
2198 case STAT_PX_ST_LI:
2199 /* stats.l has been initialized above */
2200 for (; appctx->ctx.stats.l != &px->conf.listeners; appctx->ctx.stats.l = l->by_fe.n) {
Christopher Fauletef779222018-10-31 08:47:01 +01002201 if (htx) {
2202 if (htx_almost_full(htx))
2203 goto full;
2204 }
2205 else {
2206 if (buffer_almost_full(&rep->buf))
2207 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002208 }
2209
2210 l = LIST_ELEM(appctx->ctx.stats.l, struct listener *, by_fe);
2211 if (!l->counters)
2212 continue;
2213
2214 if (appctx->ctx.stats.flags & STAT_BOUND) {
2215 if (!(appctx->ctx.stats.type & (1 << STATS_TYPE_SO)))
2216 break;
2217
2218 if (appctx->ctx.stats.sid != -1 && l->luid != appctx->ctx.stats.sid)
2219 continue;
2220 }
2221
2222 /* print the frontend */
Willy Tarreau43241ff2019-10-09 11:27:51 +02002223 if (stats_dump_li_stats(si, px, l)) {
Christopher Fauletef779222018-10-31 08:47:01 +01002224 if (!stats_putchk(rep, htx, &trash))
2225 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002226 }
2227 }
2228
2229 appctx->ctx.stats.sv = px->srv; /* may be NULL */
2230 appctx->ctx.stats.px_st = STAT_PX_ST_SV;
2231 /* fall through */
2232
2233 case STAT_PX_ST_SV:
2234 /* stats.sv has been initialized above */
2235 for (; appctx->ctx.stats.sv != NULL; appctx->ctx.stats.sv = sv->next) {
Christopher Fauletef779222018-10-31 08:47:01 +01002236 if (htx) {
2237 if (htx_almost_full(htx))
2238 goto full;
2239 }
2240 else {
2241 if (buffer_almost_full(&rep->buf))
2242 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002243 }
2244
2245 sv = appctx->ctx.stats.sv;
2246
2247 if (appctx->ctx.stats.flags & STAT_BOUND) {
2248 if (!(appctx->ctx.stats.type & (1 << STATS_TYPE_SV)))
2249 break;
2250
2251 if (appctx->ctx.stats.sid != -1 && sv->puid != appctx->ctx.stats.sid)
2252 continue;
2253 }
2254
2255 svs = sv;
2256 while (svs->track)
2257 svs = svs->track;
2258
2259 /* do not report servers which are DOWN and not changing state */
2260 if ((appctx->ctx.stats.flags & STAT_HIDE_DOWN) &&
Emeric Brun52a91d32017-08-31 14:41:55 +02002261 ((sv->cur_admin & SRV_ADMF_MAINT) || /* server is in maintenance */
2262 (sv->cur_state == SRV_ST_STOPPED && /* server is down */
William Lallemand74c24fb2016-11-21 17:18:36 +01002263 (!((svs->agent.state | svs->check.state) & CHK_ST_ENABLED) ||
2264 ((svs->agent.state & CHK_ST_ENABLED) && !svs->agent.health) ||
2265 ((svs->check.state & CHK_ST_ENABLED) && !svs->check.health))))) {
2266 continue;
2267 }
2268
Willy Tarreau43241ff2019-10-09 11:27:51 +02002269 if (stats_dump_sv_stats(si, px, sv)) {
Christopher Fauletef779222018-10-31 08:47:01 +01002270 if (!stats_putchk(rep, htx, &trash))
2271 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002272 }
2273 } /* for sv */
2274
2275 appctx->ctx.stats.px_st = STAT_PX_ST_BE;
2276 /* fall through */
2277
2278 case STAT_PX_ST_BE:
2279 /* print the backend */
Willy Tarreau43241ff2019-10-09 11:27:51 +02002280 if (stats_dump_be_stats(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_END;
2286 /* fall through */
2287
2288 case STAT_PX_ST_END:
2289 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
2290 stats_dump_html_px_end(si, px);
Christopher Fauletef779222018-10-31 08:47:01 +01002291 if (!stats_putchk(rep, htx, &trash))
2292 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002293 }
2294
2295 appctx->ctx.stats.px_st = STAT_PX_ST_FIN;
2296 /* fall through */
2297
2298 case STAT_PX_ST_FIN:
2299 return 1;
2300
2301 default:
2302 /* unknown state, we should put an abort() here ! */
2303 return 1;
2304 }
Christopher Fauletef779222018-10-31 08:47:01 +01002305
2306 full:
2307 si_rx_room_blk(si);
2308 return 0;
William Lallemand74c24fb2016-11-21 17:18:36 +01002309}
2310
2311/* Dumps the HTTP stats head block to the trash for and uses the per-uri
2312 * parameters <uri>. The caller is responsible for clearing the trash if needed.
2313 */
Willy Tarreau676c29e2019-10-09 10:50:01 +02002314static void stats_dump_html_head(struct appctx *appctx, struct uri_auth *uri)
William Lallemand74c24fb2016-11-21 17:18:36 +01002315{
2316 /* WARNING! This must fit in the first buffer !!! */
2317 chunk_appendf(&trash,
2318 "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"\n"
2319 "\"http://www.w3.org/TR/html4/loose.dtd\">\n"
2320 "<html><head><title>Statistics Report for " PRODUCT_NAME "%s%s</title>\n"
2321 "<meta http-equiv=\"content-type\" content=\"text/html; charset=iso-8859-1\">\n"
2322 "<style type=\"text/css\"><!--\n"
2323 "body {"
2324 " font-family: arial, helvetica, sans-serif;"
2325 " font-size: 12px;"
2326 " font-weight: normal;"
2327 " color: black;"
2328 " background: white;"
2329 "}\n"
2330 "th,td {"
2331 " font-size: 10px;"
2332 "}\n"
2333 "h1 {"
2334 " font-size: x-large;"
2335 " margin-bottom: 0.5em;"
2336 "}\n"
2337 "h2 {"
2338 " font-family: helvetica, arial;"
2339 " font-size: x-large;"
2340 " font-weight: bold;"
2341 " font-style: italic;"
2342 " color: #6020a0;"
2343 " margin-top: 0em;"
2344 " margin-bottom: 0em;"
2345 "}\n"
2346 "h3 {"
2347 " font-family: helvetica, arial;"
2348 " font-size: 16px;"
2349 " font-weight: bold;"
2350 " color: #b00040;"
2351 " background: #e8e8d0;"
2352 " margin-top: 0em;"
2353 " margin-bottom: 0em;"
2354 "}\n"
2355 "li {"
2356 " margin-top: 0.25em;"
2357 " margin-right: 2em;"
2358 "}\n"
2359 ".hr {margin-top: 0.25em;"
2360 " border-color: black;"
2361 " border-bottom-style: solid;"
2362 "}\n"
2363 ".titre {background: #20D0D0;color: #000000; font-weight: bold; text-align: center;}\n"
2364 ".total {background: #20D0D0;color: #ffff80;}\n"
2365 ".frontend {background: #e8e8d0;}\n"
2366 ".socket {background: #d0d0d0;}\n"
2367 ".backend {background: #e8e8d0;}\n"
2368 ".active_down {background: #ff9090;}\n"
2369 ".active_going_up {background: #ffd020;}\n"
2370 ".active_going_down {background: #ffffa0;}\n"
2371 ".active_up {background: #c0ffc0;}\n"
2372 ".active_nolb {background: #20a0ff;}\n"
2373 ".active_draining {background: #20a0FF;}\n"
2374 ".active_no_check {background: #e0e0e0;}\n"
2375 ".backup_down {background: #ff9090;}\n"
2376 ".backup_going_up {background: #ff80ff;}\n"
2377 ".backup_going_down {background: #c060ff;}\n"
2378 ".backup_up {background: #b0d0ff;}\n"
2379 ".backup_nolb {background: #90b0e0;}\n"
2380 ".backup_draining {background: #cc9900;}\n"
2381 ".backup_no_check {background: #e0e0e0;}\n"
2382 ".maintain {background: #c07820;}\n"
2383 ".rls {letter-spacing: 0.2em; margin-right: 1px;}\n" /* right letter spacing (used for grouping digits) */
2384 "\n"
2385 "a.px:link {color: #ffff40; text-decoration: none;}"
2386 "a.px:visited {color: #ffff40; text-decoration: none;}"
2387 "a.px:hover {color: #ffffff; text-decoration: none;}"
2388 "a.lfsb:link {color: #000000; text-decoration: none;}"
2389 "a.lfsb:visited {color: #000000; text-decoration: none;}"
2390 "a.lfsb:hover {color: #505050; text-decoration: none;}"
2391 "\n"
2392 "table.tbl { border-collapse: collapse; border-style: none;}\n"
2393 "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"
2394 "table.tbl td.ac { text-align: center;}\n"
2395 "table.tbl th { border-width: 1px; border-style: solid solid solid solid; border-color: gray;}\n"
2396 "table.tbl th.pxname { background: #b00040; color: #ffff40; font-weight: bold; border-style: solid solid none solid; padding: 2px 3px; white-space: nowrap;}\n"
2397 "table.tbl th.empty { border-style: none; empty-cells: hide; background: white;}\n"
2398 "table.tbl th.desc { background: white; border-style: solid solid none solid; text-align: left; padding: 2px 3px;}\n"
2399 "\n"
2400 "table.lgd { border-collapse: collapse; border-width: 1px; border-style: none none none solid; border-color: black;}\n"
2401 "table.lgd td { border-width: 1px; border-style: solid solid solid solid; border-color: gray; padding: 2px;}\n"
2402 "table.lgd td.noborder { border-style: none; padding: 2px; white-space: nowrap;}\n"
2403 "table.det { border-collapse: collapse; border-style: none; }\n"
2404 "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"
2405 "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"
2406 "u {text-decoration:none; border-bottom: 1px dotted black;}\n"
2407 "div.tips {\n"
2408 " display:block;\n"
2409 " visibility:hidden;\n"
2410 " z-index:2147483647;\n"
2411 " position:absolute;\n"
2412 " padding:2px 4px 3px;\n"
2413 " background:#f0f060; color:#000000;\n"
2414 " border:1px solid #7040c0;\n"
2415 " white-space:nowrap;\n"
2416 " font-style:normal;font-size:11px;font-weight:normal;\n"
2417 " -moz-border-radius:3px;-webkit-border-radius:3px;border-radius:3px;\n"
2418 " -moz-box-shadow:gray 2px 2px 3px;-webkit-box-shadow:gray 2px 2px 3px;box-shadow:gray 2px 2px 3px;\n"
2419 "}\n"
2420 "u:hover div.tips {visibility:visible;}\n"
2421 "-->\n"
2422 "</style></head>\n",
Willy Tarreau676c29e2019-10-09 10:50:01 +02002423 (appctx->ctx.stats.flags & STAT_SHNODE) ? " on " : "",
Willy Tarreau027d2062020-01-23 11:47:13 +01002424 (appctx->ctx.stats.flags & STAT_SHNODE) ? (uri && uri->node ? uri->node : global.node) : ""
William Lallemand74c24fb2016-11-21 17:18:36 +01002425 );
2426}
2427
2428/* Dumps the HTML stats information block to the trash for and uses the state from
2429 * stream interface <si> and per-uri parameters <uri>. The caller is responsible
2430 * for clearing the trash if needed.
2431 */
2432static void stats_dump_html_info(struct stream_interface *si, struct uri_auth *uri)
2433{
2434 struct appctx *appctx = __objt_appctx(si->end);
2435 unsigned int up = (now.tv_sec - start_date.tv_sec);
2436 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
Christopher Fauleted7a0662019-01-14 11:07:34 +01002437 const char *scope_ptr = stats_scope_ptr(appctx, si);
Willy Tarreau1713c032019-05-23 12:23:55 +02002438 unsigned long long bps = (unsigned long long)read_freq_ctr(&global.out_32bps) * 32;
2439
2440 /* Turn the bytes per second to bits per second and take care of the
2441 * usual ethernet overhead in order to help figure how far we are from
2442 * interface saturation since it's the only case which usually matters.
2443 * For this we count the total size of an Ethernet frame on the wire
2444 * including preamble and IFG (1538) for the largest TCP segment it
2445 * transports (1448 with TCP timestamps). This is not valid for smaller
2446 * packets (under-estimated), but it gives a reasonably accurate
2447 * estimation of how far we are from uplink saturation.
2448 */
2449 bps = bps * 8 * 1538 / 1448;
William Lallemand74c24fb2016-11-21 17:18:36 +01002450
2451 /* WARNING! this has to fit the first packet too.
2452 * We are around 3.5 kB, add adding entries will
2453 * become tricky if we want to support 4kB buffers !
2454 */
2455 chunk_appendf(&trash,
2456 "<body><h1><a href=\"" PRODUCT_URL "\" style=\"text-decoration: none;\">"
2457 PRODUCT_NAME "%s</a></h1>\n"
2458 "<h2>Statistics Report for pid %d%s%s%s%s</h2>\n"
2459 "<hr width=\"100%%\" class=\"hr\">\n"
2460 "<h3>&gt; General process information</h3>\n"
2461 "<table border=0><tr><td align=\"left\" nowrap width=\"1%%\">\n"
Yves Lafon95317282018-02-26 11:10:37 +01002462 "<p><b>pid = </b> %d (process #%d, nbproc = %d, nbthread = %d)<br>\n"
William Lallemand74c24fb2016-11-21 17:18:36 +01002463 "<b>uptime = </b> %dd %dh%02dm%02ds<br>\n"
2464 "<b>system limits:</b> memmax = %s%s; ulimit-n = %d<br>\n"
2465 "<b>maxsock = </b> %d; <b>maxconn = </b> %d; <b>maxpipes = </b> %d<br>\n"
Willy Tarreau1713c032019-05-23 12:23:55 +02002466 "current conns = %d; current pipes = %d/%d; conn rate = %d/sec; bit rate = %.3f %cbps<br>\n"
William Lallemand74c24fb2016-11-21 17:18:36 +01002467 "Running tasks: %d/%d; idle = %d %%<br>\n"
2468 "</td><td align=\"center\" nowrap>\n"
2469 "<table class=\"lgd\"><tr>\n"
2470 "<td class=\"active_up\">&nbsp;</td><td class=\"noborder\">active UP </td>"
2471 "<td class=\"backup_up\">&nbsp;</td><td class=\"noborder\">backup UP </td>"
2472 "</tr><tr>\n"
2473 "<td class=\"active_going_down\"></td><td class=\"noborder\">active UP, going down </td>"
2474 "<td class=\"backup_going_down\"></td><td class=\"noborder\">backup UP, going down </td>"
2475 "</tr><tr>\n"
2476 "<td class=\"active_going_up\"></td><td class=\"noborder\">active DOWN, going up </td>"
2477 "<td class=\"backup_going_up\"></td><td class=\"noborder\">backup DOWN, going up </td>"
2478 "</tr><tr>\n"
2479 "<td class=\"active_down\"></td><td class=\"noborder\">active or backup DOWN &nbsp;</td>"
2480 "<td class=\"active_no_check\"></td><td class=\"noborder\">not checked </td>"
2481 "</tr><tr>\n"
2482 "<td class=\"maintain\"></td><td class=\"noborder\" colspan=\"3\">active or backup DOWN for maintenance (MAINT) &nbsp;</td>"
2483 "</tr><tr>\n"
2484 "<td class=\"active_draining\"></td><td class=\"noborder\" colspan=\"3\">active or backup SOFT STOPPED for maintenance &nbsp;</td>"
2485 "</tr></table>\n"
2486 "Note: \"NOLB\"/\"DRAIN\" = UP with load-balancing disabled."
2487 "</td>"
2488 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
2489 "<b>Display option:</b><ul style=\"margin-top: 0.25em;\">"
2490 "",
Willy Tarreau676c29e2019-10-09 10:50:01 +02002491 (appctx->ctx.stats.flags & STAT_HIDEVER) ? "" : (stats_version_string),
2492 pid, (appctx->ctx.stats.flags & STAT_SHNODE) ? " on " : "",
2493 (appctx->ctx.stats.flags & STAT_SHNODE) ? (uri->node ? uri->node : global.node) : "",
2494 (appctx->ctx.stats.flags & STAT_SHDESC) ? ": " : "",
2495 (appctx->ctx.stats.flags & STAT_SHDESC) ? (uri->desc ? uri->desc : global.desc) : "",
Yves Lafon95317282018-02-26 11:10:37 +01002496 pid, relative_pid, global.nbproc, global.nbthread,
William Lallemand74c24fb2016-11-21 17:18:36 +01002497 up / 86400, (up % 86400) / 3600,
2498 (up % 3600) / 60, (up % 60),
2499 global.rlimit_memmax ? ultoa(global.rlimit_memmax) : "unlimited",
2500 global.rlimit_memmax ? " MB" : "",
2501 global.rlimit_nofile,
2502 global.maxsock, global.maxconn, global.maxpipes,
2503 actconn, pipes_used, pipes_used+pipes_free, read_freq_ctr(&global.conn_per_sec),
Willy Tarreau1713c032019-05-23 12:23:55 +02002504 bps >= 1000000000UL ? (bps / 1000000000.0) : bps >= 1000000UL ? (bps / 1000000.0) : (bps / 1000.0),
2505 bps >= 1000000000UL ? 'G' : bps >= 1000000UL ? 'M' : 'k',
Willy Tarreau81036f22019-05-20 19:24:50 +02002506 tasks_run_queue_cur, nb_tasks_cur, ti->idle_pct
William Lallemand74c24fb2016-11-21 17:18:36 +01002507 );
2508
2509 /* scope_txt = search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
Christopher Fauleted7a0662019-01-14 11:07:34 +01002510 memcpy(scope_txt, scope_ptr, appctx->ctx.stats.scope_len);
William Lallemand74c24fb2016-11-21 17:18:36 +01002511 scope_txt[appctx->ctx.stats.scope_len] = '\0';
2512
2513 chunk_appendf(&trash,
2514 "<li><form method=\"GET\">Scope : <input value=\"%s\" name=\"" STAT_SCOPE_INPUT_NAME "\" size=\"8\" maxlength=\"%d\" tabindex=\"1\"/></form>\n",
2515 (appctx->ctx.stats.scope_len > 0) ? scope_txt : "",
2516 STAT_SCOPE_TXT_MAXLEN);
2517
2518 /* scope_txt = search pattern + search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
2519 scope_txt[0] = 0;
2520 if (appctx->ctx.stats.scope_len) {
2521 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Christopher Fauleted7a0662019-01-14 11:07:34 +01002522 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), scope_ptr, appctx->ctx.stats.scope_len);
William Lallemand74c24fb2016-11-21 17:18:36 +01002523 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
2524 }
2525
2526 if (appctx->ctx.stats.flags & STAT_HIDE_DOWN)
2527 chunk_appendf(&trash,
2528 "<li><a href=\"%s%s%s%s\">Show all servers</a><br>\n",
2529 uri->uri_prefix,
2530 "",
2531 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2532 scope_txt);
2533 else
2534 chunk_appendf(&trash,
2535 "<li><a href=\"%s%s%s%s\">Hide 'DOWN' servers</a><br>\n",
2536 uri->uri_prefix,
2537 ";up",
2538 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2539 scope_txt);
2540
2541 if (uri->refresh > 0) {
2542 if (appctx->ctx.stats.flags & STAT_NO_REFRESH)
2543 chunk_appendf(&trash,
2544 "<li><a href=\"%s%s%s%s\">Enable refresh</a><br>\n",
2545 uri->uri_prefix,
2546 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2547 "",
2548 scope_txt);
2549 else
2550 chunk_appendf(&trash,
2551 "<li><a href=\"%s%s%s%s\">Disable refresh</a><br>\n",
2552 uri->uri_prefix,
2553 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2554 ";norefresh",
2555 scope_txt);
2556 }
2557
2558 chunk_appendf(&trash,
2559 "<li><a href=\"%s%s%s%s\">Refresh now</a><br>\n",
2560 uri->uri_prefix,
2561 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2562 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2563 scope_txt);
2564
2565 chunk_appendf(&trash,
2566 "<li><a href=\"%s;csv%s%s\">CSV export</a><br>\n",
2567 uri->uri_prefix,
2568 (uri->refresh > 0) ? ";norefresh" : "",
2569 scope_txt);
2570
2571 chunk_appendf(&trash,
Christopher Faulet6338a082019-09-09 15:50:54 +02002572 "<li><a href=\"%s;json%s%s\">JSON export</a> (<a href=\"%s;json-schema\">schema</a>)<br>\n",
2573 uri->uri_prefix,
2574 (uri->refresh > 0) ? ";norefresh" : "",
2575 scope_txt, uri->uri_prefix);
2576
2577 chunk_appendf(&trash,
William Lallemand74c24fb2016-11-21 17:18:36 +01002578 "</ul></td>"
2579 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
2580 "<b>External resources:</b><ul style=\"margin-top: 0.25em;\">\n"
2581 "<li><a href=\"" PRODUCT_URL "\">Primary site</a><br>\n"
2582 "<li><a href=\"" PRODUCT_URL_UPD "\">Updates (v" PRODUCT_BRANCH ")</a><br>\n"
2583 "<li><a href=\"" PRODUCT_URL_DOC "\">Online manual</a><br>\n"
2584 "</ul>"
2585 "</td>"
2586 "</tr></table>\n"
2587 ""
2588 );
2589
2590 if (appctx->ctx.stats.st_code) {
2591 switch (appctx->ctx.stats.st_code) {
2592 case STAT_STATUS_DONE:
2593 chunk_appendf(&trash,
2594 "<p><div class=active_up>"
2595 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2596 "Action processed successfully."
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_NONE:
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 "Nothing has changed."
2607 "</div>\n", uri->uri_prefix,
2608 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2609 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2610 scope_txt);
2611 break;
2612 case STAT_STATUS_PART:
2613 chunk_appendf(&trash,
2614 "<p><div class=active_going_down>"
2615 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2616 "Action partially processed.<br>"
2617 "Some server names are probably unknown or ambiguous (duplicated names in the backend)."
2618 "</div>\n", uri->uri_prefix,
2619 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2620 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2621 scope_txt);
2622 break;
2623 case STAT_STATUS_ERRP:
2624 chunk_appendf(&trash,
2625 "<p><div class=active_down>"
2626 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2627 "Action not processed because of invalid parameters."
2628 "<ul>"
2629 "<li>The action is maybe unknown.</li>"
Christopher Faulet2f9a41d2019-02-27 15:30:57 +01002630 "<li>Invalid key parameter (empty or too long).</li>"
William Lallemand74c24fb2016-11-21 17:18:36 +01002631 "<li>The backend name is probably unknown or ambiguous (duplicated names).</li>"
2632 "<li>Some server names are probably unknown or ambiguous (duplicated names in the backend).</li>"
2633 "</ul>"
2634 "</div>\n", uri->uri_prefix,
2635 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2636 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2637 scope_txt);
2638 break;
2639 case STAT_STATUS_EXCD:
2640 chunk_appendf(&trash,
2641 "<p><div class=active_down>"
2642 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2643 "<b>Action not processed : the buffer couldn't store all the data.<br>"
2644 "You should retry with less servers at a time.</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;
2650 case STAT_STATUS_DENY:
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>Action denied.</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;
Christopher Faulet3c2ecf72019-02-27 16:41:27 +01002660 case STAT_STATUS_IVAL:
2661 chunk_appendf(&trash,
2662 "<p><div class=active_down>"
2663 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2664 "<b>Invalid requests (unsupported method or chunked encoded request).</b>"
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 break;
William Lallemand74c24fb2016-11-21 17:18:36 +01002670 default:
2671 chunk_appendf(&trash,
2672 "<p><div class=active_no_check>"
2673 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2674 "Unexpected result."
2675 "</div>\n", uri->uri_prefix,
2676 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2677 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2678 scope_txt);
2679 }
2680 chunk_appendf(&trash, "<p>\n");
2681 }
2682}
2683
2684/* Dumps the HTML stats trailer block to the trash. The caller is responsible
2685 * for clearing the trash if needed.
2686 */
2687static void stats_dump_html_end()
2688{
2689 chunk_appendf(&trash, "</body></html>\n");
2690}
2691
Simon Horman05ee2132017-01-04 09:37:25 +01002692/* Dumps the stats JSON header to the trash buffer which. The caller is responsible
2693 * for clearing it if needed.
2694 */
2695static void stats_dump_json_header()
2696{
2697 chunk_strcat(&trash, "[");
2698}
2699
2700
2701/* Dumps the JSON stats trailer block to the trash. The caller is responsible
2702 * for clearing the trash if needed.
2703 */
2704static void stats_dump_json_end()
2705{
2706 chunk_strcat(&trash, "]");
2707}
2708
William Lallemand74c24fb2016-11-21 17:18:36 +01002709/* This function dumps statistics onto the stream interface's read buffer in
2710 * either CSV or HTML format. <uri> contains some HTML-specific parameters that
2711 * are ignored for CSV format (hence <uri> may be NULL there). It returns 0 if
2712 * it had to stop writing data and an I/O is needed, 1 if the dump is finished
2713 * and the stream must be closed, or -1 in case of any error. This function is
2714 * used by both the CLI and the HTTP handlers.
2715 */
Christopher Fauletef779222018-10-31 08:47:01 +01002716static int stats_dump_stat_to_buffer(struct stream_interface *si, struct htx *htx,
2717 struct uri_auth *uri)
William Lallemand74c24fb2016-11-21 17:18:36 +01002718{
2719 struct appctx *appctx = __objt_appctx(si->end);
2720 struct channel *rep = si_ic(si);
2721 struct proxy *px;
2722
2723 chunk_reset(&trash);
2724
2725 switch (appctx->st2) {
2726 case STAT_ST_INIT:
2727 appctx->st2 = STAT_ST_HEAD; /* let's start producing data */
2728 /* fall through */
2729
2730 case STAT_ST_HEAD:
2731 if (appctx->ctx.stats.flags & STAT_FMT_HTML)
Willy Tarreau676c29e2019-10-09 10:50:01 +02002732 stats_dump_html_head(appctx, uri);
Christopher Faulet6338a082019-09-09 15:50:54 +02002733 else if (appctx->ctx.stats.flags & STAT_JSON_SCHM)
2734 stats_dump_json_schema(&trash);
Simon Horman05ee2132017-01-04 09:37:25 +01002735 else if (appctx->ctx.stats.flags & STAT_FMT_JSON)
Willy Tarreau9d7fb632017-04-11 07:53:04 +02002736 stats_dump_json_header();
William Lallemand74c24fb2016-11-21 17:18:36 +01002737 else if (!(appctx->ctx.stats.flags & STAT_FMT_TYPED))
2738 stats_dump_csv_header();
2739
Christopher Fauletef779222018-10-31 08:47:01 +01002740 if (!stats_putchk(rep, htx, &trash))
2741 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002742
Christopher Faulet6338a082019-09-09 15:50:54 +02002743 if (appctx->ctx.stats.flags & STAT_JSON_SCHM) {
2744 appctx->st2 = STAT_ST_FIN;
2745 return 1;
2746 }
William Lallemand74c24fb2016-11-21 17:18:36 +01002747 appctx->st2 = STAT_ST_INFO;
2748 /* fall through */
2749
2750 case STAT_ST_INFO:
2751 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
2752 stats_dump_html_info(si, uri);
Christopher Fauletef779222018-10-31 08:47:01 +01002753 if (!stats_putchk(rep, htx, &trash))
2754 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002755 }
2756
Olivier Houchardfbc74e82017-11-24 16:54:05 +01002757 appctx->ctx.stats.px = proxies_list;
William Lallemand74c24fb2016-11-21 17:18:36 +01002758 appctx->ctx.stats.px_st = STAT_PX_ST_INIT;
2759 appctx->st2 = STAT_ST_LIST;
2760 /* fall through */
2761
2762 case STAT_ST_LIST:
2763 /* dump proxies */
2764 while (appctx->ctx.stats.px) {
Christopher Fauletef779222018-10-31 08:47:01 +01002765 if (htx) {
2766 if (htx_almost_full(htx))
2767 goto full;
2768 }
2769 else {
2770 if (buffer_almost_full(&rep->buf))
2771 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002772 }
2773
2774 px = appctx->ctx.stats.px;
2775 /* skip the disabled proxies, global frontend and non-networked ones */
2776 if (px->state != PR_STSTOPPED && px->uuid > 0 && (px->cap & (PR_CAP_FE | PR_CAP_BE)))
Christopher Fauletef779222018-10-31 08:47:01 +01002777 if (stats_dump_proxy_to_buffer(si, htx, px, uri) == 0)
William Lallemand74c24fb2016-11-21 17:18:36 +01002778 return 0;
2779
2780 appctx->ctx.stats.px = px->next;
2781 appctx->ctx.stats.px_st = STAT_PX_ST_INIT;
2782 }
2783 /* here, we just have reached the last proxy */
2784
2785 appctx->st2 = STAT_ST_END;
2786 /* fall through */
2787
2788 case STAT_ST_END:
Simon Horman05ee2132017-01-04 09:37:25 +01002789 if (appctx->ctx.stats.flags & (STAT_FMT_HTML|STAT_FMT_JSON)) {
2790 if (appctx->ctx.stats.flags & STAT_FMT_HTML)
2791 stats_dump_html_end();
2792 else
2793 stats_dump_json_end();
Christopher Fauletef779222018-10-31 08:47:01 +01002794 if (!stats_putchk(rep, htx, &trash))
2795 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002796 }
2797
2798 appctx->st2 = STAT_ST_FIN;
2799 /* fall through */
2800
2801 case STAT_ST_FIN:
2802 return 1;
2803
2804 default:
2805 /* unknown state ! */
2806 appctx->st2 = STAT_ST_FIN;
2807 return -1;
2808 }
Christopher Fauletef779222018-10-31 08:47:01 +01002809
2810 full:
2811 si_rx_room_blk(si);
2812 return 0;
2813
William Lallemand74c24fb2016-11-21 17:18:36 +01002814}
2815
2816/* We reached the stats page through a POST request. The appctx is
2817 * expected to have already been allocated by the caller.
2818 * Parse the posted data and enable/disable servers if necessary.
2819 * Returns 1 if request was parsed or zero if it needs more data.
2820 */
2821static int stats_process_http_post(struct stream_interface *si)
2822{
2823 struct stream *s = si_strm(si);
2824 struct appctx *appctx = objt_appctx(si->end);
2825
2826 struct proxy *px = NULL;
2827 struct server *sv = NULL;
2828
2829 char key[LINESIZE];
2830 int action = ST_ADM_ACTION_NONE;
2831 int reprocess = 0;
2832
2833 int total_servers = 0;
2834 int altered_servers = 0;
2835
2836 char *first_param, *cur_param, *next_param, *end_params;
2837 char *st_cur_param = NULL;
2838 char *st_next_param = NULL;
2839
Christopher Fauleta3618372018-12-13 21:59:56 +01002840 struct buffer *temp = get_trash_chunk();
William Lallemand74c24fb2016-11-21 17:18:36 +01002841
Christopher Fauletb7f88902019-07-15 21:56:43 +02002842 struct htx *htx = htxbuf(&s->req.buf);
2843 struct htx_blk *blk;
Christopher Fauleta3618372018-12-13 21:59:56 +01002844
Christopher Fauletb7f88902019-07-15 21:56:43 +02002845 /* we need more data */
2846 if (s->txn->req.msg_state < HTTP_MSG_DONE) {
2847 /* check if we can receive more */
2848 if (htx_free_data_space(htx) <= global.tune.maxrewrite) {
2849 appctx->ctx.stats.st_code = STAT_STATUS_EXCD;
2850 goto out;
Christopher Fauleta3618372018-12-13 21:59:56 +01002851 }
Christopher Fauletb7f88902019-07-15 21:56:43 +02002852 goto wait;
2853 }
Christopher Fauleta3618372018-12-13 21:59:56 +01002854
Christopher Fauletb7f88902019-07-15 21:56:43 +02002855 /* The request was fully received. Copy data */
2856 blk = htx_get_head_blk(htx);
2857 while (blk) {
2858 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Fauleta3618372018-12-13 21:59:56 +01002859
Christopher Fauletb7f88902019-07-15 21:56:43 +02002860 if (type == HTX_BLK_EOM || type == HTX_BLK_TLR || type == HTX_BLK_EOT)
2861 break;
2862 if (type == HTX_BLK_DATA) {
2863 struct ist v = htx_get_blk_value(htx, blk);
Christopher Fauleta3618372018-12-13 21:59:56 +01002864
Christopher Fauletb7f88902019-07-15 21:56:43 +02002865 if (!chunk_memcat(temp, v.ptr, v.len)) {
Christopher Faulet2f9a41d2019-02-27 15:30:57 +01002866 appctx->ctx.stats.st_code = STAT_STATUS_EXCD;
2867 goto out;
2868 }
Christopher Fauleta3618372018-12-13 21:59:56 +01002869 }
Christopher Fauletb7f88902019-07-15 21:56:43 +02002870 blk = htx_get_next_blk(htx, blk);
William Lallemand74c24fb2016-11-21 17:18:36 +01002871 }
2872
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002873 first_param = temp->area;
Christopher Fauleta3618372018-12-13 21:59:56 +01002874 end_params = temp->area + temp->data;
William Lallemand74c24fb2016-11-21 17:18:36 +01002875 cur_param = next_param = end_params;
2876 *end_params = '\0';
2877
2878 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
2879
2880 /*
2881 * Parse the parameters in reverse order to only store the last value.
2882 * From the html form, the backend and the action are at the end.
2883 */
2884 while (cur_param > first_param) {
2885 char *value;
2886 int poffset, plen;
2887
2888 cur_param--;
2889
2890 if ((*cur_param == '&') || (cur_param == first_param)) {
2891 reprocess_servers:
2892 /* Parse the key */
2893 poffset = (cur_param != first_param ? 1 : 0);
2894 plen = next_param - cur_param + (cur_param == first_param ? 1 : 0);
2895 if ((plen > 0) && (plen <= sizeof(key))) {
2896 strncpy(key, cur_param + poffset, plen);
2897 key[plen - 1] = '\0';
2898 } else {
Christopher Faulet2f9a41d2019-02-27 15:30:57 +01002899 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
William Lallemand74c24fb2016-11-21 17:18:36 +01002900 goto out;
2901 }
2902
2903 /* Parse the value */
2904 value = key;
2905 while (*value != '\0' && *value != '=') {
2906 value++;
2907 }
2908 if (*value == '=') {
2909 /* Ok, a value is found, we can mark the end of the key */
2910 *value++ = '\0';
2911 }
Willy Tarreau62ba9ba2020-04-23 17:54:47 +02002912 if (url_decode(key, 1) < 0 || url_decode(value, 1) < 0)
William Lallemand74c24fb2016-11-21 17:18:36 +01002913 break;
2914
2915 /* Now we can check the key to see what to do */
2916 if (!px && (strcmp(key, "b") == 0)) {
2917 if ((px = proxy_be_by_name(value)) == NULL) {
2918 /* the backend name is unknown or ambiguous (duplicate names) */
2919 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
2920 goto out;
2921 }
2922 }
2923 else if (!action && (strcmp(key, "action") == 0)) {
2924 if (strcmp(value, "ready") == 0) {
2925 action = ST_ADM_ACTION_READY;
2926 }
2927 else if (strcmp(value, "drain") == 0) {
2928 action = ST_ADM_ACTION_DRAIN;
2929 }
2930 else if (strcmp(value, "maint") == 0) {
2931 action = ST_ADM_ACTION_MAINT;
2932 }
2933 else if (strcmp(value, "shutdown") == 0) {
2934 action = ST_ADM_ACTION_SHUTDOWN;
2935 }
2936 else if (strcmp(value, "dhlth") == 0) {
2937 action = ST_ADM_ACTION_DHLTH;
2938 }
2939 else if (strcmp(value, "ehlth") == 0) {
2940 action = ST_ADM_ACTION_EHLTH;
2941 }
2942 else if (strcmp(value, "hrunn") == 0) {
2943 action = ST_ADM_ACTION_HRUNN;
2944 }
2945 else if (strcmp(value, "hnolb") == 0) {
2946 action = ST_ADM_ACTION_HNOLB;
2947 }
2948 else if (strcmp(value, "hdown") == 0) {
2949 action = ST_ADM_ACTION_HDOWN;
2950 }
2951 else if (strcmp(value, "dagent") == 0) {
2952 action = ST_ADM_ACTION_DAGENT;
2953 }
2954 else if (strcmp(value, "eagent") == 0) {
2955 action = ST_ADM_ACTION_EAGENT;
2956 }
2957 else if (strcmp(value, "arunn") == 0) {
2958 action = ST_ADM_ACTION_ARUNN;
2959 }
2960 else if (strcmp(value, "adown") == 0) {
2961 action = ST_ADM_ACTION_ADOWN;
2962 }
2963 /* else these are the old supported methods */
2964 else if (strcmp(value, "disable") == 0) {
2965 action = ST_ADM_ACTION_DISABLE;
2966 }
2967 else if (strcmp(value, "enable") == 0) {
2968 action = ST_ADM_ACTION_ENABLE;
2969 }
2970 else if (strcmp(value, "stop") == 0) {
2971 action = ST_ADM_ACTION_STOP;
2972 }
2973 else if (strcmp(value, "start") == 0) {
2974 action = ST_ADM_ACTION_START;
2975 }
2976 else {
2977 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
2978 goto out;
2979 }
2980 }
2981 else if (strcmp(key, "s") == 0) {
2982 if (!(px && action)) {
2983 /*
2984 * Indicates that we'll need to reprocess the parameters
2985 * as soon as backend and action are known
2986 */
2987 if (!reprocess) {
2988 st_cur_param = cur_param;
2989 st_next_param = next_param;
2990 }
2991 reprocess = 1;
2992 }
2993 else if ((sv = findserver(px, value)) != NULL) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002994 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
William Lallemand74c24fb2016-11-21 17:18:36 +01002995 switch (action) {
2996 case ST_ADM_ACTION_DISABLE:
Emeric Brun52a91d32017-08-31 14:41:55 +02002997 if (!(sv->cur_admin & SRV_ADMF_FMAINT)) {
William Lallemand74c24fb2016-11-21 17:18:36 +01002998 altered_servers++;
2999 total_servers++;
3000 srv_set_admin_flag(sv, SRV_ADMF_FMAINT, "'disable' on stats page");
3001 }
3002 break;
3003 case ST_ADM_ACTION_ENABLE:
Emeric Brun52a91d32017-08-31 14:41:55 +02003004 if (sv->cur_admin & SRV_ADMF_FMAINT) {
William Lallemand74c24fb2016-11-21 17:18:36 +01003005 altered_servers++;
3006 total_servers++;
3007 srv_clr_admin_flag(sv, SRV_ADMF_FMAINT);
3008 }
3009 break;
3010 case ST_ADM_ACTION_STOP:
Emeric Brun52a91d32017-08-31 14:41:55 +02003011 if (!(sv->cur_admin & SRV_ADMF_FDRAIN)) {
William Lallemand74c24fb2016-11-21 17:18:36 +01003012 srv_set_admin_flag(sv, SRV_ADMF_FDRAIN, "'stop' on stats page");
3013 altered_servers++;
3014 total_servers++;
3015 }
3016 break;
3017 case ST_ADM_ACTION_START:
Emeric Brun52a91d32017-08-31 14:41:55 +02003018 if (sv->cur_admin & SRV_ADMF_FDRAIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +01003019 srv_clr_admin_flag(sv, SRV_ADMF_FDRAIN);
3020 altered_servers++;
3021 total_servers++;
3022 }
3023 break;
3024 case ST_ADM_ACTION_DHLTH:
3025 if (sv->check.state & CHK_ST_CONFIGURED) {
3026 sv->check.state &= ~CHK_ST_ENABLED;
3027 altered_servers++;
3028 total_servers++;
3029 }
3030 break;
3031 case ST_ADM_ACTION_EHLTH:
3032 if (sv->check.state & CHK_ST_CONFIGURED) {
3033 sv->check.state |= CHK_ST_ENABLED;
3034 altered_servers++;
3035 total_servers++;
3036 }
3037 break;
3038 case ST_ADM_ACTION_HRUNN:
3039 if (!(sv->track)) {
3040 sv->check.health = sv->check.rise + sv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02003041 srv_set_running(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01003042 altered_servers++;
3043 total_servers++;
3044 }
3045 break;
3046 case ST_ADM_ACTION_HNOLB:
3047 if (!(sv->track)) {
3048 sv->check.health = sv->check.rise + sv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02003049 srv_set_stopping(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01003050 altered_servers++;
3051 total_servers++;
3052 }
3053 break;
3054 case ST_ADM_ACTION_HDOWN:
3055 if (!(sv->track)) {
3056 sv->check.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02003057 srv_set_stopped(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01003058 altered_servers++;
3059 total_servers++;
3060 }
3061 break;
3062 case ST_ADM_ACTION_DAGENT:
3063 if (sv->agent.state & CHK_ST_CONFIGURED) {
3064 sv->agent.state &= ~CHK_ST_ENABLED;
3065 altered_servers++;
3066 total_servers++;
3067 }
3068 break;
3069 case ST_ADM_ACTION_EAGENT:
3070 if (sv->agent.state & CHK_ST_CONFIGURED) {
3071 sv->agent.state |= CHK_ST_ENABLED;
3072 altered_servers++;
3073 total_servers++;
3074 }
3075 break;
3076 case ST_ADM_ACTION_ARUNN:
3077 if (sv->agent.state & CHK_ST_ENABLED) {
3078 sv->agent.health = sv->agent.rise + sv->agent.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02003079 srv_set_running(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01003080 altered_servers++;
3081 total_servers++;
3082 }
3083 break;
3084 case ST_ADM_ACTION_ADOWN:
3085 if (sv->agent.state & CHK_ST_ENABLED) {
3086 sv->agent.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02003087 srv_set_stopped(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01003088 altered_servers++;
3089 total_servers++;
3090 }
3091 break;
3092 case ST_ADM_ACTION_READY:
3093 srv_adm_set_ready(sv);
3094 altered_servers++;
3095 total_servers++;
3096 break;
3097 case ST_ADM_ACTION_DRAIN:
3098 srv_adm_set_drain(sv);
3099 altered_servers++;
3100 total_servers++;
3101 break;
3102 case ST_ADM_ACTION_MAINT:
3103 srv_adm_set_maint(sv);
3104 altered_servers++;
3105 total_servers++;
3106 break;
3107 case ST_ADM_ACTION_SHUTDOWN:
3108 if (px->state != PR_STSTOPPED) {
Willy Tarreauaf7ea812019-11-14 16:42:04 +01003109 srv_shutdown_streams(sv, SF_ERR_KILLED);
William Lallemand74c24fb2016-11-21 17:18:36 +01003110 altered_servers++;
3111 total_servers++;
3112 }
3113 break;
3114 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003115 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
William Lallemand74c24fb2016-11-21 17:18:36 +01003116 } else {
3117 /* the server name is unknown or ambiguous (duplicate names) */
3118 total_servers++;
3119 }
3120 }
3121 if (reprocess && px && action) {
3122 /* Now, we know the backend and the action chosen by the user.
3123 * We can safely restart from the first server parameter
3124 * to reprocess them
3125 */
3126 cur_param = st_cur_param;
3127 next_param = st_next_param;
3128 reprocess = 0;
3129 goto reprocess_servers;
3130 }
3131
3132 next_param = cur_param;
3133 }
3134 }
3135
3136 if (total_servers == 0) {
3137 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
3138 }
3139 else if (altered_servers == 0) {
3140 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
3141 }
3142 else if (altered_servers == total_servers) {
3143 appctx->ctx.stats.st_code = STAT_STATUS_DONE;
3144 }
3145 else {
3146 appctx->ctx.stats.st_code = STAT_STATUS_PART;
3147 }
3148 out:
3149 return 1;
Christopher Faulet2f9a41d2019-02-27 15:30:57 +01003150 wait:
3151 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
3152 return 0;
Christopher Fauletef779222018-10-31 08:47:01 +01003153}
3154
3155
Christopher Fauletb7f88902019-07-15 21:56:43 +02003156static int stats_send_http_headers(struct stream_interface *si, struct htx *htx)
Christopher Fauletef779222018-10-31 08:47:01 +01003157{
3158 struct stream *s = si_strm(si);
3159 struct uri_auth *uri = s->be->uri_auth;
3160 struct appctx *appctx = __objt_appctx(si->end);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003161 struct htx_sl *sl;
3162 unsigned int flags;
Christopher Fauletef779222018-10-31 08:47:01 +01003163
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003164 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);
3165 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), ist("200"), ist("OK"));
3166 if (!sl)
Christopher Fauletef779222018-10-31 08:47:01 +01003167 goto full;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003168 sl->info.res.status = 200;
Christopher Fauletef779222018-10-31 08:47:01 +01003169
Christopher Fauletb829f4c2019-03-29 16:13:55 +01003170 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")))
Christopher Fauletef779222018-10-31 08:47:01 +01003171 goto full;
3172 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
3173 if (!htx_add_header(htx, ist("Content-Type"), ist("text/html")))
3174 goto full;
3175 }
Christopher Faulet6338a082019-09-09 15:50:54 +02003176 else if (appctx->ctx.stats.flags & (STAT_FMT_JSON|STAT_JSON_SCHM)) {
3177 if (!htx_add_header(htx, ist("Content-Type"), ist("application/json")))
3178 goto full;
3179 }
Christopher Fauletef779222018-10-31 08:47:01 +01003180 else {
3181 if (!htx_add_header(htx, ist("Content-Type"), ist("text/plain")))
3182 goto full;
3183 }
3184
3185 if (uri->refresh > 0 && !(appctx->ctx.stats.flags & STAT_NO_REFRESH)) {
3186 const char *refresh = U2A(uri->refresh);
3187 if (!htx_add_header(htx, ist("Refresh"), ist2(refresh, strlen(refresh))))
3188 goto full;
3189 }
3190
3191 if (appctx->ctx.stats.flags & STAT_CHUNKED) {
3192 if (!htx_add_header(htx, ist("Transfer-Encoding"), ist("chunked")))
3193 goto full;
3194 }
3195
3196 if (!htx_add_endof(htx, HTX_BLK_EOH))
3197 goto full;
3198
Christopher Faulet1e2d6362019-02-27 16:28:48 +01003199 channel_add_input(&s->res, htx->data);
Christopher Fauletef779222018-10-31 08:47:01 +01003200 return 1;
3201
3202 full:
3203 htx_reset(htx);
3204 si_rx_room_blk(si);
3205 return 0;
William Lallemand74c24fb2016-11-21 17:18:36 +01003206}
3207
Christopher Fauletef779222018-10-31 08:47:01 +01003208
Christopher Fauletb7f88902019-07-15 21:56:43 +02003209static int stats_send_http_redirect(struct stream_interface *si, struct htx *htx)
Christopher Fauletef779222018-10-31 08:47:01 +01003210{
3211 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
3212 struct stream *s = si_strm(si);
3213 struct uri_auth *uri = s->be->uri_auth;
3214 struct appctx *appctx = __objt_appctx(si->end);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003215 struct htx_sl *sl;
3216 unsigned int flags;
Christopher Fauletef779222018-10-31 08:47:01 +01003217
3218 /* scope_txt = search pattern + search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
3219 scope_txt[0] = 0;
3220 if (appctx->ctx.stats.scope_len) {
Christopher Fauleted7a0662019-01-14 11:07:34 +01003221 const char *scope_ptr = stats_scope_ptr(appctx, si);
3222
Christopher Fauletef779222018-10-31 08:47:01 +01003223 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Christopher Fauleted7a0662019-01-14 11:07:34 +01003224 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), scope_ptr, appctx->ctx.stats.scope_len);
Christopher Fauletef779222018-10-31 08:47:01 +01003225 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
3226 }
3227
3228 /* We don't want to land on the posted stats page because a refresh will
3229 * repost the data. We don't want this to happen on accident so we redirect
3230 * the browse to the stats page with a GET.
3231 */
3232 chunk_printf(&trash, "%s;st=%s%s%s%s",
3233 uri->uri_prefix,
3234 ((appctx->ctx.stats.st_code > STAT_STATUS_INIT) &&
3235 (appctx->ctx.stats.st_code < STAT_STATUS_SIZE) &&
3236 stat_status_codes[appctx->ctx.stats.st_code]) ?
3237 stat_status_codes[appctx->ctx.stats.st_code] :
3238 stat_status_codes[STAT_STATUS_UNKN],
3239 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3240 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
3241 scope_txt);
3242
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003243 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_CHNK);
3244 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), ist("303"), ist("See Other"));
3245 if (!sl)
Christopher Fauletef779222018-10-31 08:47:01 +01003246 goto full;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003247 sl->info.res.status = 303;
Christopher Fauletef779222018-10-31 08:47:01 +01003248
3249 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")) ||
Christopher Fauletef779222018-10-31 08:47:01 +01003250 !htx_add_header(htx, ist("Content-Type"), ist("text/plain")) ||
3251 !htx_add_header(htx, ist("Content-Length"), ist("0")) ||
3252 !htx_add_header(htx, ist("Location"), ist2(trash.area, trash.data)))
3253 goto full;
3254
3255 if (!htx_add_endof(htx, HTX_BLK_EOH))
3256 goto full;
3257
Christopher Faulet1e2d6362019-02-27 16:28:48 +01003258 channel_add_input(&s->res, htx->data);
Christopher Fauletef779222018-10-31 08:47:01 +01003259 return 1;
3260
3261full:
3262 htx_reset(htx);
3263 si_rx_room_blk(si);
3264 return 0;
3265}
William Lallemand74c24fb2016-11-21 17:18:36 +01003266
Simon Horman05ee2132017-01-04 09:37:25 +01003267
William Lallemand74c24fb2016-11-21 17:18:36 +01003268/* This I/O handler runs as an applet embedded in a stream interface. It is
3269 * used to send HTTP stats over a TCP socket. The mechanism is very simple.
3270 * appctx->st0 contains the operation in progress (dump, done). The handler
3271 * automatically unregisters itself once transfer is complete.
3272 */
Christopher Fauletb7f88902019-07-15 21:56:43 +02003273static void http_stats_io_handler(struct appctx *appctx)
Christopher Fauletef779222018-10-31 08:47:01 +01003274{
3275 struct stream_interface *si = appctx->owner;
3276 struct stream *s = si_strm(si);
3277 struct channel *req = si_oc(si);
3278 struct channel *res = si_ic(si);
3279 struct htx *req_htx, *res_htx;
3280
3281 res_htx = htx_from_buf(&res->buf);
3282
3283 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
3284 goto out;
3285
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05003286 /* Check if the input buffer is available. */
Christopher Fauletef779222018-10-31 08:47:01 +01003287 if (!b_size(&res->buf)) {
3288 si_rx_room_blk(si);
3289 goto out;
3290 }
3291
3292 /* check that the output is not closed */
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003293 if (res->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_SHUTR))
3294 appctx->st0 = STAT_HTTP_END;
Christopher Fauletef779222018-10-31 08:47:01 +01003295
3296 /* all states are processed in sequence */
3297 if (appctx->st0 == STAT_HTTP_HEAD) {
Christopher Fauletb7f88902019-07-15 21:56:43 +02003298 if (stats_send_http_headers(si, res_htx)) {
Christopher Fauletef779222018-10-31 08:47:01 +01003299 if (s->txn->meth == HTTP_METH_HEAD)
3300 appctx->st0 = STAT_HTTP_DONE;
3301 else
3302 appctx->st0 = STAT_HTTP_DUMP;
3303 }
3304 }
3305
3306 if (appctx->st0 == STAT_HTTP_DUMP) {
3307 if (stats_dump_stat_to_buffer(si, res_htx, s->be->uri_auth))
3308 appctx->st0 = STAT_HTTP_DONE;
3309 }
3310
3311 if (appctx->st0 == STAT_HTTP_POST) {
3312 if (stats_process_http_post(si))
3313 appctx->st0 = STAT_HTTP_LAST;
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003314 else if (req->flags & CF_SHUTR)
Christopher Fauletef779222018-10-31 08:47:01 +01003315 appctx->st0 = STAT_HTTP_DONE;
3316 }
3317
3318 if (appctx->st0 == STAT_HTTP_LAST) {
Christopher Fauletb7f88902019-07-15 21:56:43 +02003319 if (stats_send_http_redirect(si, res_htx))
Christopher Fauletef779222018-10-31 08:47:01 +01003320 appctx->st0 = STAT_HTTP_DONE;
3321 }
3322
3323 if (appctx->st0 == STAT_HTTP_DONE) {
Christopher Faulet54b5e212019-06-04 10:08:28 +02003324 /* Don't add TLR because mux-h1 will take care of it */
Christopher Fauletef779222018-10-31 08:47:01 +01003325 if (!htx_add_endof(res_htx, HTX_BLK_EOM)) {
3326 si_rx_room_blk(si);
3327 goto out;
3328 }
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003329 channel_add_input(&s->res, 1);
3330 appctx->st0 = STAT_HTTP_END;
Christopher Fauletef779222018-10-31 08:47:01 +01003331 }
3332
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003333 if (appctx->st0 == STAT_HTTP_END) {
3334 if (!(res->flags & CF_SHUTR)) {
Christopher Fauletef779222018-10-31 08:47:01 +01003335 res->flags |= CF_READ_NULL;
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003336 si_shutr(si);
3337 }
3338
3339 /* eat the whole request */
3340 if (co_data(req)) {
3341 req_htx = htx_from_buf(&req->buf);
3342 co_htx_skip(req, req_htx, co_data(req));
3343 htx_to_buf(req_htx, &req->buf);
Christopher Fauletef779222018-10-31 08:47:01 +01003344 }
3345 }
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003346
Christopher Fauletef779222018-10-31 08:47:01 +01003347 out:
3348 /* we have left the request in the buffer for the case where we
3349 * process a POST, and this automatically re-enables activity on
3350 * read. It's better to indicate that we want to stop reading when
3351 * we're sending, so that we know there's at most one direction
3352 * deciding to wake the applet up. It saves it from looping when
3353 * emitting large blocks into small TCP windows.
3354 */
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003355 htx_to_buf(res_htx, &res->buf);
3356 if (!channel_is_empty(res))
Christopher Fauletef779222018-10-31 08:47:01 +01003357 si_stop_get(si);
Christopher Fauletef779222018-10-31 08:47:01 +01003358}
3359
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003360/* Dump all fields from <info> into <out> using the "show info" format (name: value) */
Willy Tarreau83061a82018-07-13 11:56:34 +02003361static int stats_dump_info_fields(struct buffer *out,
Willy Tarreau43241ff2019-10-09 11:27:51 +02003362 const struct field *info, unsigned int flags)
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003363{
3364 int field;
3365
3366 for (field = 0; field < INF_TOTAL_FIELDS; field++) {
3367 if (!field_format(info, field))
3368 continue;
3369
Willy Tarreaueaa55372019-10-09 07:39:11 +02003370 if (!chunk_appendf(out, "%s: ", info_fields[field].name))
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003371 return 0;
3372 if (!stats_emit_raw_data_field(out, &info[field]))
3373 return 0;
Willy Tarreau6b19b142019-10-09 15:44:21 +02003374 if ((flags & STAT_SHOW_FDESC) && !chunk_appendf(out, ":\"%s\"", info_fields[field].desc))
3375 return 0;
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003376 if (!chunk_strcat(out, "\n"))
3377 return 0;
3378 }
3379 return 1;
3380}
3381
3382/* Dump all fields from <info> into <out> using the "show info typed" format */
Willy Tarreau83061a82018-07-13 11:56:34 +02003383static int stats_dump_typed_info_fields(struct buffer *out,
Willy Tarreau43241ff2019-10-09 11:27:51 +02003384 const struct field *info, unsigned int flags)
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003385{
3386 int field;
3387
3388 for (field = 0; field < INF_TOTAL_FIELDS; field++) {
3389 if (!field_format(info, field))
3390 continue;
3391
Willy Tarreaueaa55372019-10-09 07:39:11 +02003392 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 +01003393 return 0;
3394 if (!stats_emit_field_tags(out, &info[field], ':'))
3395 return 0;
3396 if (!stats_emit_typed_data_field(out, &info[field]))
3397 return 0;
Willy Tarreau6b19b142019-10-09 15:44:21 +02003398 if ((flags & STAT_SHOW_FDESC) && !chunk_appendf(out, ":\"%s\"", info_fields[field].desc))
3399 return 0;
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003400 if (!chunk_strcat(out, "\n"))
3401 return 0;
3402 }
3403 return 1;
3404}
3405
3406/* Fill <info> with HAProxy global info. <info> is preallocated
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05003407 * array of length <len>. The length of the array must be
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003408 * INF_TOTAL_FIELDS. If this length is less then this value, the
3409 * function returns 0, otherwise, it returns 1.
3410 */
3411int stats_fill_info(struct field *info, int len)
3412{
3413 unsigned int up = (now.tv_sec - start_date.tv_sec);
Willy Tarreau83061a82018-07-13 11:56:34 +02003414 struct buffer *out = get_trash_chunk();
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003415
3416#ifdef USE_OPENSSL
3417 int ssl_sess_rate = read_freq_ctr(&global.ssl_per_sec);
3418 int ssl_key_rate = read_freq_ctr(&global.ssl_fe_keys_per_sec);
3419 int ssl_reuse = 0;
3420
3421 if (ssl_key_rate < ssl_sess_rate) {
3422 /* count the ssl reuse ratio and avoid overflows in both directions */
3423 ssl_reuse = 100 - (100 * ssl_key_rate + (ssl_sess_rate - 1) / 2) / ssl_sess_rate;
3424 }
3425#endif
3426
3427 if (len < INF_TOTAL_FIELDS)
3428 return 0;
3429
3430 chunk_reset(out);
3431 memset(info, 0, sizeof(*info) * len);
3432
3433 info[INF_NAME] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, PRODUCT_NAME);
Willy Tarreau909b9d82019-01-04 18:20:32 +01003434 info[INF_VERSION] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, haproxy_version);
3435 info[INF_RELEASE_DATE] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, haproxy_date);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003436
Yves Lafon95317282018-02-26 11:10:37 +01003437 info[INF_NBTHREAD] = mkf_u32(FO_CONFIG|FS_SERVICE, global.nbthread);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003438 info[INF_NBPROC] = mkf_u32(FO_CONFIG|FS_SERVICE, global.nbproc);
3439 info[INF_PROCESS_NUM] = mkf_u32(FO_KEY, relative_pid);
3440 info[INF_PID] = mkf_u32(FO_STATUS, pid);
3441
3442 info[INF_UPTIME] = mkf_str(FN_DURATION, chunk_newstr(out));
3443 chunk_appendf(out, "%ud %uh%02um%02us", up / 86400, (up % 86400) / 3600, (up % 3600) / 60, (up % 60));
3444
3445 info[INF_UPTIME_SEC] = mkf_u32(FN_DURATION, up);
3446 info[INF_MEMMAX_MB] = mkf_u32(FO_CONFIG|FN_LIMIT, global.rlimit_memmax);
3447 info[INF_POOL_ALLOC_MB] = mkf_u32(0, (unsigned)(pool_total_allocated() / 1048576L));
3448 info[INF_POOL_USED_MB] = mkf_u32(0, (unsigned)(pool_total_used() / 1048576L));
3449 info[INF_POOL_FAILED] = mkf_u32(FN_COUNTER, pool_total_failures());
3450 info[INF_ULIMIT_N] = mkf_u32(FO_CONFIG|FN_LIMIT, global.rlimit_nofile);
3451 info[INF_MAXSOCK] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxsock);
3452 info[INF_MAXCONN] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxconn);
3453 info[INF_HARD_MAXCONN] = mkf_u32(FO_CONFIG|FN_LIMIT, global.hardmaxconn);
3454 info[INF_CURR_CONN] = mkf_u32(0, actconn);
3455 info[INF_CUM_CONN] = mkf_u32(FN_COUNTER, totalconn);
3456 info[INF_CUM_REQ] = mkf_u32(FN_COUNTER, global.req_count);
3457#ifdef USE_OPENSSL
3458 info[INF_MAX_SSL_CONNS] = mkf_u32(FN_MAX, global.maxsslconn);
3459 info[INF_CURR_SSL_CONNS] = mkf_u32(0, sslconns);
3460 info[INF_CUM_SSL_CONNS] = mkf_u32(FN_COUNTER, totalsslconns);
3461#endif
3462 info[INF_MAXPIPES] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxpipes);
3463 info[INF_PIPES_USED] = mkf_u32(0, pipes_used);
3464 info[INF_PIPES_FREE] = mkf_u32(0, pipes_free);
3465 info[INF_CONN_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&global.conn_per_sec));
3466 info[INF_CONN_RATE_LIMIT] = mkf_u32(FO_CONFIG|FN_LIMIT, global.cps_lim);
3467 info[INF_MAX_CONN_RATE] = mkf_u32(FN_MAX, global.cps_max);
3468 info[INF_SESS_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&global.sess_per_sec));
3469 info[INF_SESS_RATE_LIMIT] = mkf_u32(FO_CONFIG|FN_LIMIT, global.sps_lim);
3470 info[INF_MAX_SESS_RATE] = mkf_u32(FN_RATE, global.sps_max);
3471
3472#ifdef USE_OPENSSL
3473 info[INF_SSL_RATE] = mkf_u32(FN_RATE, ssl_sess_rate);
3474 info[INF_SSL_RATE_LIMIT] = mkf_u32(FO_CONFIG|FN_LIMIT, global.ssl_lim);
3475 info[INF_MAX_SSL_RATE] = mkf_u32(FN_MAX, global.ssl_max);
3476 info[INF_SSL_FRONTEND_KEY_RATE] = mkf_u32(0, ssl_key_rate);
3477 info[INF_SSL_FRONTEND_MAX_KEY_RATE] = mkf_u32(FN_MAX, global.ssl_fe_keys_max);
3478 info[INF_SSL_FRONTEND_SESSION_REUSE_PCT] = mkf_u32(0, ssl_reuse);
3479 info[INF_SSL_BACKEND_KEY_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&global.ssl_be_keys_per_sec));
3480 info[INF_SSL_BACKEND_MAX_KEY_RATE] = mkf_u32(FN_MAX, global.ssl_be_keys_max);
3481 info[INF_SSL_CACHE_LOOKUPS] = mkf_u32(FN_COUNTER, global.shctx_lookups);
3482 info[INF_SSL_CACHE_MISSES] = mkf_u32(FN_COUNTER, global.shctx_misses);
3483#endif
3484 info[INF_COMPRESS_BPS_IN] = mkf_u32(FN_RATE, read_freq_ctr(&global.comp_bps_in));
3485 info[INF_COMPRESS_BPS_OUT] = mkf_u32(FN_RATE, read_freq_ctr(&global.comp_bps_out));
3486 info[INF_COMPRESS_BPS_RATE_LIM] = mkf_u32(FO_CONFIG|FN_LIMIT, global.comp_rate_lim);
3487#ifdef USE_ZLIB
3488 info[INF_ZLIB_MEM_USAGE] = mkf_u32(0, zlib_used_memory);
3489 info[INF_MAX_ZLIB_MEM_USAGE] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxzlibmem);
3490#endif
3491 info[INF_TASKS] = mkf_u32(0, nb_tasks_cur);
Christopher Faulet34c5cc92016-12-06 09:15:30 +01003492 info[INF_RUN_QUEUE] = mkf_u32(0, tasks_run_queue_cur);
Willy Tarreau81036f22019-05-20 19:24:50 +02003493 info[INF_IDLE_PCT] = mkf_u32(FN_AVG, ti->idle_pct);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003494 info[INF_NODE] = mkf_str(FO_CONFIG|FN_OUTPUT|FS_SERVICE, global.node);
3495 if (global.desc)
3496 info[INF_DESCRIPTION] = mkf_str(FO_CONFIG|FN_OUTPUT|FS_SERVICE, global.desc);
Willy Tarreau00098ea2018-11-05 14:38:13 +01003497 info[INF_STOPPING] = mkf_u32(0, stopping);
3498 info[INF_JOBS] = mkf_u32(0, jobs);
William Lallemanda7199262018-11-16 16:57:20 +01003499 info[INF_UNSTOPPABLE_JOBS] = mkf_u32(0, unstoppable_jobs);
Willy Tarreau00098ea2018-11-05 14:38:13 +01003500 info[INF_LISTENERS] = mkf_u32(0, listeners);
Willy Tarreau199ad242018-11-05 16:31:22 +01003501 info[INF_ACTIVE_PEERS] = mkf_u32(0, active_peers);
Willy Tarreau2d372c22018-11-05 17:12:27 +01003502 info[INF_CONNECTED_PEERS] = mkf_u32(0, connected_peers);
Willy Tarreau13ef7732018-11-12 07:25:28 +01003503 info[INF_DROPPED_LOGS] = mkf_u32(0, dropped_logs);
Willy Tarreaubeb859a2018-11-22 18:07:59 +01003504 info[INF_BUSY_POLLING] = mkf_u32(0, !!(global.tune.options & GTUNE_BUSY_POLLING));
Baptiste Assmann333939c2019-01-21 08:34:50 +01003505 info[INF_FAILED_RESOLUTIONS] = mkf_u32(0, dns_failed_resolutions);
Willy Tarreau7cf0e452019-05-23 11:39:14 +02003506 info[INF_TOTAL_BYTES_OUT] = mkf_u64(0, global.out_bytes);
3507 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 +02003508 info[INF_DEBUG_COMMANDS_ISSUED] = mkf_u32(0, debug_commands_issued);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003509
3510 return 1;
3511}
3512
3513/* This function dumps information onto the stream interface's read buffer.
3514 * It returns 0 as long as it does not complete, non-zero upon completion.
3515 * No state is used.
3516 */
3517static int stats_dump_info_to_buffer(struct stream_interface *si)
3518{
3519 struct appctx *appctx = __objt_appctx(si->end);
3520
3521 if (!stats_fill_info(info, INF_TOTAL_FIELDS))
3522 return 0;
3523
3524 chunk_reset(&trash);
3525
3526 if (appctx->ctx.stats.flags & STAT_FMT_TYPED)
Willy Tarreau43241ff2019-10-09 11:27:51 +02003527 stats_dump_typed_info_fields(&trash, info, appctx->ctx.stats.flags);
Simon Horman05ee2132017-01-04 09:37:25 +01003528 else if (appctx->ctx.stats.flags & STAT_FMT_JSON)
Willy Tarreau43241ff2019-10-09 11:27:51 +02003529 stats_dump_json_info_fields(&trash, info, appctx->ctx.stats.flags);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003530 else
Willy Tarreau43241ff2019-10-09 11:27:51 +02003531 stats_dump_info_fields(&trash, info, appctx->ctx.stats.flags);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003532
Willy Tarreau06d80a92017-10-19 14:32:15 +02003533 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01003534 si_rx_room_blk(si);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003535 return 0;
3536 }
3537
3538 return 1;
3539}
3540
Simon Horman6f6bb382017-01-04 09:37:26 +01003541/* This function dumps the schema onto the stream interface's read buffer.
3542 * It returns 0 as long as it does not complete, non-zero upon completion.
3543 * No state is used.
3544 *
3545 * Integer values bouned to the range [-(2**53)+1, (2**53)-1] as
3546 * per the recommendation for interoperable integers in section 6 of RFC 7159.
3547 */
Willy Tarreau83061a82018-07-13 11:56:34 +02003548static void stats_dump_json_schema(struct buffer *out)
Simon Horman6f6bb382017-01-04 09:37:26 +01003549{
3550
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003551 int old_len = out->data;
Simon Horman6f6bb382017-01-04 09:37:26 +01003552
3553 chunk_strcat(out,
3554 "{"
3555 "\"$schema\":\"http://json-schema.org/draft-04/schema#\","
3556 "\"oneOf\":["
3557 "{"
3558 "\"title\":\"Info\","
3559 "\"type\":\"array\","
3560 "\"items\":{"
3561 "\"properties\":{"
3562 "\"title\":\"InfoItem\","
3563 "\"type\":\"object\","
3564 "\"field\":{\"$ref\":\"#/definitions/field\"},"
3565 "\"processNum\":{\"$ref\":\"#/definitions/processNum\"},"
3566 "\"tags\":{\"$ref\":\"#/definitions/tags\"},"
3567 "\"value\":{\"$ref\":\"#/definitions/typedValue\"}"
3568 "},"
3569 "\"required\":[\"field\",\"processNum\",\"tags\","
3570 "\"value\"]"
3571 "}"
3572 "},"
3573 "{"
3574 "\"title\":\"Stat\","
3575 "\"type\":\"array\","
3576 "\"items\":{"
3577 "\"title\":\"InfoItem\","
3578 "\"type\":\"object\","
3579 "\"properties\":{"
3580 "\"objType\":{"
3581 "\"enum\":[\"Frontend\",\"Backend\",\"Listener\","
3582 "\"Server\",\"Unknown\"]"
3583 "},"
3584 "\"proxyId\":{"
3585 "\"type\":\"integer\","
3586 "\"minimum\":0"
3587 "},"
3588 "\"id\":{"
3589 "\"type\":\"integer\","
3590 "\"minimum\":0"
3591 "},"
3592 "\"field\":{\"$ref\":\"#/definitions/field\"},"
3593 "\"processNum\":{\"$ref\":\"#/definitions/processNum\"},"
3594 "\"tags\":{\"$ref\":\"#/definitions/tags\"},"
3595 "\"typedValue\":{\"$ref\":\"#/definitions/typedValue\"}"
3596 "},"
3597 "\"required\":[\"objType\",\"proxyId\",\"id\","
3598 "\"field\",\"processNum\",\"tags\","
3599 "\"value\"]"
3600 "}"
3601 "},"
3602 "{"
3603 "\"title\":\"Error\","
3604 "\"type\":\"object\","
3605 "\"properties\":{"
3606 "\"errorStr\":{"
3607 "\"type\":\"string\""
3608 "},"
3609 "\"required\":[\"errorStr\"]"
3610 "}"
3611 "}"
3612 "],"
3613 "\"definitions\":{"
3614 "\"field\":{"
3615 "\"type\":\"object\","
3616 "\"pos\":{"
3617 "\"type\":\"integer\","
3618 "\"minimum\":0"
3619 "},"
3620 "\"name\":{"
3621 "\"type\":\"string\""
3622 "},"
3623 "\"required\":[\"pos\",\"name\"]"
3624 "},"
3625 "\"processNum\":{"
3626 "\"type\":\"integer\","
3627 "\"minimum\":1"
3628 "},"
3629 "\"tags\":{"
3630 "\"type\":\"object\","
3631 "\"origin\":{"
3632 "\"type\":\"string\","
3633 "\"enum\":[\"Metric\",\"Status\",\"Key\","
3634 "\"Config\",\"Product\",\"Unknown\"]"
3635 "},"
3636 "\"nature\":{"
3637 "\"type\":\"string\","
3638 "\"enum\":[\"Gauge\",\"Limit\",\"Min\",\"Max\","
3639 "\"Rate\",\"Counter\",\"Duration\","
3640 "\"Age\",\"Time\",\"Name\",\"Output\","
3641 "\"Avg\", \"Unknown\"]"
3642 "},"
3643 "\"scope\":{"
3644 "\"type\":\"string\","
3645 "\"enum\":[\"Cluster\",\"Process\",\"Service\","
3646 "\"System\",\"Unknown\"]"
3647 "},"
3648 "\"required\":[\"origin\",\"nature\",\"scope\"]"
3649 "},"
3650 "\"typedValue\":{"
3651 "\"type\":\"object\","
3652 "\"oneOf\":["
3653 "{\"$ref\":\"#/definitions/typedValue/definitions/s32Value\"},"
3654 "{\"$ref\":\"#/definitions/typedValue/definitions/s64Value\"},"
3655 "{\"$ref\":\"#/definitions/typedValue/definitions/u32Value\"},"
3656 "{\"$ref\":\"#/definitions/typedValue/definitions/u64Value\"},"
3657 "{\"$ref\":\"#/definitions/typedValue/definitions/strValue\"}"
3658 "],"
3659 "\"definitions\":{"
3660 "\"s32Value\":{"
3661 "\"properties\":{"
3662 "\"type\":{"
3663 "\"type\":\"string\","
3664 "\"enum\":[\"s32\"]"
3665 "},"
3666 "\"value\":{"
3667 "\"type\":\"integer\","
3668 "\"minimum\":-2147483648,"
3669 "\"maximum\":2147483647"
3670 "}"
3671 "},"
3672 "\"required\":[\"type\",\"value\"]"
3673 "},"
3674 "\"s64Value\":{"
3675 "\"properties\":{"
3676 "\"type\":{"
3677 "\"type\":\"string\","
3678 "\"enum\":[\"s64\"]"
3679 "},"
3680 "\"value\":{"
3681 "\"type\":\"integer\","
3682 "\"minimum\":-9007199254740991,"
3683 "\"maximum\":9007199254740991"
3684 "}"
3685 "},"
3686 "\"required\":[\"type\",\"value\"]"
3687 "},"
3688 "\"u32Value\":{"
3689 "\"properties\":{"
3690 "\"type\":{"
3691 "\"type\":\"string\","
3692 "\"enum\":[\"u32\"]"
3693 "},"
3694 "\"value\":{"
3695 "\"type\":\"integer\","
3696 "\"minimum\":0,"
3697 "\"maximum\":4294967295"
3698 "}"
3699 "},"
3700 "\"required\":[\"type\",\"value\"]"
3701 "},"
3702 "\"u64Value\":{"
3703 "\"properties\":{"
3704 "\"type\":{"
3705 "\"type\":\"string\","
3706 "\"enum\":[\"u64\"]"
3707 "},"
3708 "\"value\":{"
3709 "\"type\":\"integer\","
3710 "\"minimum\":0,"
3711 "\"maximum\":9007199254740991"
3712 "}"
3713 "},"
3714 "\"required\":[\"type\",\"value\"]"
3715 "},"
3716 "\"strValue\":{"
3717 "\"properties\":{"
3718 "\"type\":{"
3719 "\"type\":\"string\","
3720 "\"enum\":[\"str\"]"
3721 "},"
3722 "\"value\":{\"type\":\"string\"}"
3723 "},"
3724 "\"required\":[\"type\",\"value\"]"
3725 "},"
3726 "\"unknownValue\":{"
3727 "\"properties\":{"
3728 "\"type\":{"
3729 "\"type\":\"integer\","
3730 "\"minimum\":0"
3731 "},"
3732 "\"value\":{"
3733 "\"type\":\"string\","
3734 "\"enum\":[\"unknown\"]"
3735 "}"
3736 "},"
3737 "\"required\":[\"type\",\"value\"]"
3738 "}"
3739 "}"
3740 "}"
3741 "}"
3742 "}");
3743
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003744 if (old_len == out->data) {
Simon Horman6f6bb382017-01-04 09:37:26 +01003745 chunk_reset(out);
3746 chunk_appendf(out,
3747 "{\"errorStr\":\"output buffer too short\"}");
3748 }
3749}
3750
3751/* This function dumps the schema onto the stream interface's read buffer.
3752 * It returns 0 as long as it does not complete, non-zero upon completion.
3753 * No state is used.
3754 */
3755static int stats_dump_json_schema_to_buffer(struct stream_interface *si)
3756{
3757 chunk_reset(&trash);
3758
3759 stats_dump_json_schema(&trash);
3760
Willy Tarreau06d80a92017-10-19 14:32:15 +02003761 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01003762 si_rx_room_blk(si);
Simon Horman6f6bb382017-01-04 09:37:26 +01003763 return 0;
3764 }
3765
3766 return 1;
3767}
3768
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02003769static int cli_parse_clear_counters(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau89d467c2016-11-23 11:02:40 +01003770{
3771 struct proxy *px;
3772 struct server *sv;
3773 struct listener *li;
3774 int clrall = 0;
3775
3776 if (strcmp(args[2], "all") == 0)
3777 clrall = 1;
3778
3779 /* check permissions */
3780 if (!cli_has_level(appctx, ACCESS_LVL_OPER) ||
3781 (clrall && !cli_has_level(appctx, ACCESS_LVL_ADMIN)))
3782 return 1;
3783
Olivier Houchardfbc74e82017-11-24 16:54:05 +01003784 for (px = proxies_list; px; px = px->next) {
Willy Tarreau89d467c2016-11-23 11:02:40 +01003785 if (clrall) {
3786 memset(&px->be_counters, 0, sizeof(px->be_counters));
3787 memset(&px->fe_counters, 0, sizeof(px->fe_counters));
3788 }
3789 else {
3790 px->be_counters.conn_max = 0;
3791 px->be_counters.p.http.rps_max = 0;
3792 px->be_counters.sps_max = 0;
3793 px->be_counters.cps_max = 0;
3794 px->be_counters.nbpend_max = 0;
Christopher Fauletefb41f02019-11-08 14:53:15 +01003795 px->be_counters.qtime_max = 0;
3796 px->be_counters.ctime_max = 0;
3797 px->be_counters.dtime_max = 0;
3798 px->be_counters.ttime_max = 0;
Willy Tarreau89d467c2016-11-23 11:02:40 +01003799
3800 px->fe_counters.conn_max = 0;
3801 px->fe_counters.p.http.rps_max = 0;
3802 px->fe_counters.sps_max = 0;
3803 px->fe_counters.cps_max = 0;
Willy Tarreau89d467c2016-11-23 11:02:40 +01003804 }
3805
3806 for (sv = px->srv; sv; sv = sv->next)
3807 if (clrall)
3808 memset(&sv->counters, 0, sizeof(sv->counters));
3809 else {
3810 sv->counters.cur_sess_max = 0;
3811 sv->counters.nbpend_max = 0;
3812 sv->counters.sps_max = 0;
Christopher Fauletefb41f02019-11-08 14:53:15 +01003813 sv->counters.qtime_max = 0;
3814 sv->counters.ctime_max = 0;
3815 sv->counters.dtime_max = 0;
3816 sv->counters.ttime_max = 0;
Willy Tarreau89d467c2016-11-23 11:02:40 +01003817 }
3818
3819 list_for_each_entry(li, &px->conf.listeners, by_fe)
3820 if (li->counters) {
3821 if (clrall)
3822 memset(li->counters, 0, sizeof(*li->counters));
3823 else
3824 li->counters->conn_max = 0;
3825 }
3826 }
3827
3828 global.cps_max = 0;
3829 global.sps_max = 0;
Olivier Houchard00bc3cb2017-10-17 19:23:25 +02003830 global.ssl_max = 0;
3831 global.ssl_fe_keys_max = 0;
3832 global.ssl_be_keys_max = 0;
Willy Tarreaud80cb4e2018-01-20 19:30:13 +01003833
3834 memset(activity, 0, sizeof(activity));
Willy Tarreau89d467c2016-11-23 11:02:40 +01003835 return 1;
3836}
3837
3838
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02003839static int cli_parse_show_info(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003840{
Willy Tarreau2f397382019-10-09 11:43:59 +02003841 int arg = 2;
3842
Willy Tarreaud25fc792016-12-16 12:33:47 +01003843 appctx->ctx.stats.scope_str = 0;
3844 appctx->ctx.stats.scope_len = 0;
3845 appctx->ctx.stats.flags = 0;
3846
Willy Tarreau2f397382019-10-09 11:43:59 +02003847 while (*args[arg]) {
3848 if (strcmp(args[arg], "typed") == 0)
3849 appctx->ctx.stats.flags = (appctx->ctx.stats.flags & ~STAT_FMT_MASK) | STAT_FMT_TYPED;
3850 else if (strcmp(args[arg], "json") == 0)
3851 appctx->ctx.stats.flags = (appctx->ctx.stats.flags & ~STAT_FMT_MASK) | STAT_FMT_JSON;
3852 else if (strcmp(args[arg], "desc") == 0)
3853 appctx->ctx.stats.flags |= STAT_SHOW_FDESC;
3854 arg++;
3855 }
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003856 return 0;
3857}
3858
3859
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02003860static int cli_parse_show_stat(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau2b812e22016-11-22 16:18:05 +01003861{
Willy Tarreau2f397382019-10-09 11:43:59 +02003862 int arg = 2;
3863
Willy Tarreaud25fc792016-12-16 12:33:47 +01003864 appctx->ctx.stats.scope_str = 0;
3865 appctx->ctx.stats.scope_len = 0;
Willy Tarreau578d6e42019-10-09 11:00:22 +02003866 appctx->ctx.stats.flags = STAT_SHNODE | STAT_SHDESC;
3867
3868 if ((strm_li(si_strm(appctx->owner))->bind_conf->level & ACCESS_LVL_MASK) >= ACCESS_LVL_OPER)
3869 appctx->ctx.stats.flags |= STAT_SHLGNDS;
Willy Tarreaud25fc792016-12-16 12:33:47 +01003870
Willy Tarreau2f397382019-10-09 11:43:59 +02003871 if (*args[arg] && *args[arg+1] && *args[arg+2]) {
Willy Tarreaua1b1ed52016-11-25 08:50:58 +01003872 struct proxy *px;
3873
Willy Tarreau2f397382019-10-09 11:43:59 +02003874 px = proxy_find_by_name(args[arg], 0, 0);
Willy Tarreaua1b1ed52016-11-25 08:50:58 +01003875 if (px)
3876 appctx->ctx.stats.iid = px->uuid;
3877 else
Willy Tarreau2f397382019-10-09 11:43:59 +02003878 appctx->ctx.stats.iid = atoi(args[arg]);
Willy Tarreaua1b1ed52016-11-25 08:50:58 +01003879
Willy Tarreau9d008692019-08-09 11:21:01 +02003880 if (!appctx->ctx.stats.iid)
3881 return cli_err(appctx, "No such proxy.\n");
Willy Tarreaua1b1ed52016-11-25 08:50:58 +01003882
Willy Tarreau2b812e22016-11-22 16:18:05 +01003883 appctx->ctx.stats.flags |= STAT_BOUND;
Willy Tarreau2f397382019-10-09 11:43:59 +02003884 appctx->ctx.stats.type = atoi(args[arg+1]);
3885 appctx->ctx.stats.sid = atoi(args[arg+2]);
3886 arg += 3;
3887 }
3888
3889 while (*args[arg]) {
3890 if (strcmp(args[arg], "typed") == 0)
3891 appctx->ctx.stats.flags = (appctx->ctx.stats.flags & ~STAT_FMT_MASK) | STAT_FMT_TYPED;
3892 else if (strcmp(args[arg], "json") == 0)
3893 appctx->ctx.stats.flags = (appctx->ctx.stats.flags & ~STAT_FMT_MASK) | STAT_FMT_JSON;
3894 else if (strcmp(args[arg], "desc") == 0)
3895 appctx->ctx.stats.flags |= STAT_SHOW_FDESC;
3896 arg++;
Willy Tarreau2b812e22016-11-22 16:18:05 +01003897 }
Willy Tarreau2b812e22016-11-22 16:18:05 +01003898
Willy Tarreau2b812e22016-11-22 16:18:05 +01003899 return 0;
3900}
3901
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003902static int cli_io_handler_dump_info(struct appctx *appctx)
3903{
3904 return stats_dump_info_to_buffer(appctx->owner);
3905}
3906
Willy Tarreau2b812e22016-11-22 16:18:05 +01003907/* This I/O handler runs as an applet embedded in a stream interface. It is
3908 * used to send raw stats over a socket.
3909 */
3910static int cli_io_handler_dump_stat(struct appctx *appctx)
3911{
Christopher Fauletef779222018-10-31 08:47:01 +01003912 return stats_dump_stat_to_buffer(appctx->owner, NULL, NULL);
Willy Tarreau2b812e22016-11-22 16:18:05 +01003913}
3914
Simon Horman6f6bb382017-01-04 09:37:26 +01003915static int cli_io_handler_dump_json_schema(struct appctx *appctx)
3916{
3917 return stats_dump_json_schema_to_buffer(appctx->owner);
3918}
3919
Willy Tarreau2b812e22016-11-22 16:18:05 +01003920/* register cli keywords */
3921static struct cli_kw_list cli_kws = {{ },{
Willy Tarreau89d467c2016-11-23 11:02:40 +01003922 { { "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 +02003923 { { "show", "info", NULL }, "show info : report information about the running process [desc|json|typed]*", cli_parse_show_info, cli_io_handler_dump_info, NULL },
3924 { { "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 +01003925 { { "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 +01003926 {{},}
3927}};
3928
Willy Tarreau0108d902018-11-25 19:14:37 +01003929INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
3930
William Lallemand74c24fb2016-11-21 17:18:36 +01003931struct applet http_stats_applet = {
3932 .obj_type = OBJ_TYPE_APPLET,
3933 .name = "<STATS>", /* used for logging */
3934 .fct = http_stats_io_handler,
3935 .release = NULL,
3936};
3937
William Lallemand74c24fb2016-11-21 17:18:36 +01003938/*
3939 * Local variables:
3940 * c-indent-level: 8
3941 * c-basic-offset: 8
3942 * End:
3943 */