blob: 43654e4178c4c36e162f3cc1296655abbdac9709 [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 Tarreau2a83d602020-05-27 16:58:08 +020029#include <haproxy/debug.h>
Willy Tarreaucd72d8c2020-06-02 19:11:26 +020030#include <haproxy/http.h>
Willy Tarreau16f958c2020-06-03 08:44:35 +020031#include <haproxy/htx.h>
Willy Tarreaud0ef4392020-06-02 09:38:52 +020032#include <haproxy/pool.h>
Willy Tarreau853b2972020-05-27 18:01:47 +020033#include <haproxy/list.h>
Willy Tarreau48fbcae2020-06-03 18:09:46 +020034#include <haproxy/tools.h>
Willy Tarreauc2f7c582020-06-02 18:15:32 +020035#include <haproxy/ticks.h>
Willy Tarreau92b4f132020-06-01 11:05:15 +020036#include <haproxy/time.h>
William Lallemand74c24fb2016-11-21 17:18:36 +010037#include <common/uri_auth.h>
Willy Tarreaud6788052020-05-27 15:59:00 +020038#include <haproxy/version.h>
Willy Tarreau8d366972020-05-27 16:10:29 +020039#include <haproxy/base64.h>
William Lallemand74c24fb2016-11-21 17:18:36 +010040
41#include <types/applet.h>
Willy Tarreau2b812e22016-11-22 16:18:05 +010042#include <types/cli.h>
William Lallemand74c24fb2016-11-21 17:18:36 +010043#include <types/global.h>
44#include <types/dns.h>
William Lallemand9ed62032016-11-21 17:49:11 +010045#include <types/stats.h>
William Lallemand74c24fb2016-11-21 17:18:36 +010046
47#include <proto/backend.h>
48#include <proto/channel.h>
49#include <proto/checks.h>
Willy Tarreau2b812e22016-11-22 16:18:05 +010050#include <proto/cli.h>
William Lallemand74c24fb2016-11-21 17:18:36 +010051#include <proto/compression.h>
Baptiste Assmann333939c2019-01-21 08:34:50 +010052#include <proto/dns.h>
William Lallemand9ed62032016-11-21 17:49:11 +010053#include <proto/stats.h>
Willy Tarreau0f6ffd62020-06-03 19:33:00 +020054#include <haproxy/fd.h>
Willy Tarreau66347942020-06-01 12:18:08 +020055#include <haproxy/freq_ctr.h>
William Lallemand74c24fb2016-11-21 17:18:36 +010056#include <proto/frontend.h>
Christopher Fauleted7a0662019-01-14 11:07:34 +010057#include <proto/http_htx.h>
William Lallemand74c24fb2016-11-21 17:18:36 +010058#include <proto/log.h>
59#include <proto/pattern.h>
Willy Tarreau551271d2020-06-04 08:32:23 +020060#include <haproxy/pipe.h>
William Lallemand74c24fb2016-11-21 17:18:36 +010061#include <proto/listener.h>
62#include <proto/map.h>
William Lallemand74c24fb2016-11-21 17:18:36 +010063#include <proto/proxy.h>
64#include <proto/sample.h>
65#include <proto/session.h>
Willy Tarreauc125cef2019-05-10 09:58:43 +020066#include <proto/ssl_sock.h>
William Lallemand74c24fb2016-11-21 17:18:36 +010067#include <proto/stream.h>
68#include <proto/server.h>
William Lallemand74c24fb2016-11-21 17:18:36 +010069#include <proto/stream_interface.h>
70#include <proto/task.h>
71
Christopher Faulet2b9b6782019-02-27 16:42:58 +010072/* status codes available for the stats admin page (strictly 4 chars length) */
73const char *stat_status_codes[STAT_STATUS_SIZE] = {
74 [STAT_STATUS_DENY] = "DENY",
75 [STAT_STATUS_DONE] = "DONE",
76 [STAT_STATUS_ERRP] = "ERRP",
77 [STAT_STATUS_EXCD] = "EXCD",
78 [STAT_STATUS_NONE] = "NONE",
79 [STAT_STATUS_PART] = "PART",
80 [STAT_STATUS_UNKN] = "UNKN",
81 [STAT_STATUS_IVAL] = "IVAL",
82};
83
Willy Tarreau0baac8c2016-11-22 16:36:53 +010084/* These are the field names for each INF_* field position. Please pay attention
85 * to always use the exact same name except that the strings for new names must
86 * be lower case or CamelCase while the enum entries must be upper case.
87 */
Willy Tarreaueaa55372019-10-09 07:39:11 +020088const struct name_desc info_fields[INF_TOTAL_FIELDS] = {
Willy Tarreau6d4897e2019-10-11 16:31:46 +020089 [INF_NAME] = { .name = "Name", .desc = "Product name" },
90 [INF_VERSION] = { .name = "Version", .desc = "Product version" },
91 [INF_RELEASE_DATE] = { .name = "Release_date", .desc = "Date of latest source code update" },
92 [INF_NBTHREAD] = { .name = "Nbthread", .desc = "Number of started threads (global.nbthread)" },
93 [INF_NBPROC] = { .name = "Nbproc", .desc = "Number of started worker processes (global.nbproc)" },
94 [INF_PROCESS_NUM] = { .name = "Process_num", .desc = "Relative worker process number (1..Nbproc)" },
95 [INF_PID] = { .name = "Pid", .desc = "This worker process identifier for the system" },
96 [INF_UPTIME] = { .name = "Uptime", .desc = "How long ago this worker process was started (days+hours+minutes+seconds)" },
97 [INF_UPTIME_SEC] = { .name = "Uptime_sec", .desc = "How long ago this worker process was started (seconds)" },
98 [INF_MEMMAX_MB] = { .name = "Memmax_MB", .desc = "Worker process's hard limit on memory usage in MB (-m on command line)" },
99 [INF_POOL_ALLOC_MB] = { .name = "PoolAlloc_MB", .desc = "Amount of memory allocated in pools (in MB)" },
100 [INF_POOL_USED_MB] = { .name = "PoolUsed_MB", .desc = "Amount of pool memory currently used (in MB)" },
101 [INF_POOL_FAILED] = { .name = "PoolFailed", .desc = "Number of failed pool allocations since this worker was started" },
102 [INF_ULIMIT_N] = { .name = "Ulimit-n", .desc = "Hard limit on the number of per-process file descriptors" },
103 [INF_MAXSOCK] = { .name = "Maxsock", .desc = "Hard limit on the number of per-process sockets" },
104 [INF_MAXCONN] = { .name = "Maxconn", .desc = "Hard limit on the number of per-process connections (configured or imposed by Ulimit-n)" },
105 [INF_HARD_MAXCONN] = { .name = "Hard_maxconn", .desc = "Hard limit on the number of per-process connections (imposed by Memmax_MB or Ulimit-n)" },
106 [INF_CURR_CONN] = { .name = "CurrConns", .desc = "Current number of connections on this worker process" },
107 [INF_CUM_CONN] = { .name = "CumConns", .desc = "Total number of connections on this worker process since started" },
108 [INF_CUM_REQ] = { .name = "CumReq", .desc = "Total number of requests on this worker process since started" },
109 [INF_MAX_SSL_CONNS] = { .name = "MaxSslConns", .desc = "Hard limit on the number of per-process SSL endpoints (front+back), 0=unlimited" },
110 [INF_CURR_SSL_CONNS] = { .name = "CurrSslConns", .desc = "Current number of SSL endpoints on this worker process (front+back)" },
111 [INF_CUM_SSL_CONNS] = { .name = "CumSslConns", .desc = "Total number of SSL endpoints on this worker process since started (front+back)" },
112 [INF_MAXPIPES] = { .name = "Maxpipes", .desc = "Hard limit on the number of pipes for splicing, 0=unlimited" },
113 [INF_PIPES_USED] = { .name = "PipesUsed", .desc = "Current number of pipes in use in this worker process" },
114 [INF_PIPES_FREE] = { .name = "PipesFree", .desc = "Current number of allocated and available pipes in this worker process" },
115 [INF_CONN_RATE] = { .name = "ConnRate", .desc = "Number of front connections created on this worker process over the last second" },
116 [INF_CONN_RATE_LIMIT] = { .name = "ConnRateLimit", .desc = "Hard limit for ConnRate (global.maxconnrate)" },
117 [INF_MAX_CONN_RATE] = { .name = "MaxConnRate", .desc = "Highest ConnRate reached on this worker process since started (in connections per second)" },
118 [INF_SESS_RATE] = { .name = "SessRate", .desc = "Number of sessions created on this worker process over the last second" },
119 [INF_SESS_RATE_LIMIT] = { .name = "SessRateLimit", .desc = "Hard limit for SessRate (global.maxsessrate)" },
120 [INF_MAX_SESS_RATE] = { .name = "MaxSessRate", .desc = "Highest SessRate reached on this worker process since started (in sessions per second)" },
121 [INF_SSL_RATE] = { .name = "SslRate", .desc = "Number of SSL connections created on this worker process over the last second" },
122 [INF_SSL_RATE_LIMIT] = { .name = "SslRateLimit", .desc = "Hard limit for SslRate (global.maxsslrate)" },
123 [INF_MAX_SSL_RATE] = { .name = "MaxSslRate", .desc = "Highest SslRate reached on this worker process since started (in connections per second)" },
124 [INF_SSL_FRONTEND_KEY_RATE] = { .name = "SslFrontendKeyRate", .desc = "Number of SSL keys created on frontends in this worker process over the last second" },
125 [INF_SSL_FRONTEND_MAX_KEY_RATE] = { .name = "SslFrontendMaxKeyRate", .desc = "Highest SslFrontendKeyRate reached on this worker process since started (in SSL keys per second)" },
126 [INF_SSL_FRONTEND_SESSION_REUSE_PCT] = { .name = "SslFrontendSessionReuse_pct", .desc = "Percent of frontend SSL connections which did not require a new key" },
127 [INF_SSL_BACKEND_KEY_RATE] = { .name = "SslBackendKeyRate", .desc = "Number of SSL keys created on backends in this worker process over the last second" },
128 [INF_SSL_BACKEND_MAX_KEY_RATE] = { .name = "SslBackendMaxKeyRate", .desc = "Highest SslBackendKeyRate reached on this worker process since started (in SSL keys per second)" },
129 [INF_SSL_CACHE_LOOKUPS] = { .name = "SslCacheLookups", .desc = "Total number of SSL session ID lookups in the SSL session cache on this worker since started" },
130 [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" },
131 [INF_COMPRESS_BPS_IN] = { .name = "CompressBpsIn", .desc = "Number of bytes submitted to the HTTP compressor in this worker process over the last second" },
132 [INF_COMPRESS_BPS_OUT] = { .name = "CompressBpsOut", .desc = "Number of bytes emitted by the HTTP compressor in this worker process over the last second" },
133 [INF_COMPRESS_BPS_RATE_LIM] = { .name = "CompressBpsRateLim", .desc = "Limit of CompressBpsOut beyond which HTTP compression is automatically disabled" },
134 [INF_ZLIB_MEM_USAGE] = { .name = "ZlibMemUsage", .desc = "Amount of memory currently used by HTTP compression on the current worker process (in bytes)" },
135 [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)" },
136 [INF_TASKS] = { .name = "Tasks", .desc = "Total number of tasks in the current worker process (active + sleeping)" },
137 [INF_RUN_QUEUE] = { .name = "Run_queue", .desc = "Total number of active tasks+tasklets in the current worker process" },
138 [INF_IDLE_PCT] = { .name = "Idle_pct", .desc = "Percentage of last second spent waiting in the current worker thread" },
139 [INF_NODE] = { .name = "node", .desc = "Node name (global.node)" },
140 [INF_DESCRIPTION] = { .name = "description", .desc = "Node description (global.description)" },
141 [INF_STOPPING] = { .name = "Stopping", .desc = "1 if the worker process is currently stopping, otherwise zero" },
142 [INF_JOBS] = { .name = "Jobs", .desc = "Current number of active jobs on the current worker process (frontend connections, master connections, listeners)" },
143 [INF_UNSTOPPABLE_JOBS] = { .name = "Unstoppable Jobs", .desc = "Current number of unstoppable jobs on the current worker process (master connections)" },
144 [INF_LISTENERS] = { .name = "Listeners", .desc = "Current number of active listeners on the current worker process" },
145 [INF_ACTIVE_PEERS] = { .name = "ActivePeers", .desc = "Current number of verified active peers connections on the current worker process" },
146 [INF_CONNECTED_PEERS] = { .name = "ConnectedPeers", .desc = "Current number of peers having passed the connection step on the current worker process" },
147 [INF_DROPPED_LOGS] = { .name = "DroppedLogs", .desc = "Total number of dropped logs for current worker process since started" },
148 [INF_BUSY_POLLING] = { .name = "BusyPolling", .desc = "1 if busy-polling is currently in use on the worker process, otherwise zero (config.busy-polling)" },
149 [INF_FAILED_RESOLUTIONS] = { .name = "FailedResolutions", .desc = "Total number of failed DNS resolutions in current worker process since started" },
150 [INF_TOTAL_BYTES_OUT] = { .name = "TotalBytesOut", .desc = "Total number of bytes emitted by current worker process since started" },
151 [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 +0200152 [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 +0100153};
154
Willy Tarreaueaa55372019-10-09 07:39:11 +0200155const struct name_desc stat_fields[ST_F_TOTAL_FIELDS] = {
Willy Tarreau6d4897e2019-10-11 16:31:46 +0200156 [ST_F_PXNAME] = { .name = "pxname", .desc = "Proxy name" },
157 [ST_F_SVNAME] = { .name = "svname", .desc = "Server name" },
158 [ST_F_QCUR] = { .name = "qcur", .desc = "Current number of connections waiting in the server of backend queue" },
159 [ST_F_QMAX] = { .name = "qmax", .desc = "Highest value of qcur encountered since process started" },
160 [ST_F_SCUR] = { .name = "scur", .desc = "Current number of sessions on the frontend, backend or server" },
161 [ST_F_SMAX] = { .name = "smax", .desc = "Highest value of scur encountered since process started" },
162 [ST_F_SLIM] = { .name = "slim", .desc = "Frontend/listener/server's maxconn, backend's fullconn" },
163 [ST_F_STOT] = { .name = "stot", .desc = "Total number of sessions since process started" },
164 [ST_F_BIN] = { .name = "bin", .desc = "Total number of request bytes since process started" },
165 [ST_F_BOUT] = { .name = "bout", .desc = "Total number of response bytes since process started" },
166 [ST_F_DREQ] = { .name = "dreq", .desc = "Total number of denied requests since process started" },
167 [ST_F_DRESP] = { .name = "dresp", .desc = "Total number of denied responses since process started" },
168 [ST_F_EREQ] = { .name = "ereq", .desc = "Total number of invalid requests since process started" },
169 [ST_F_ECON] = { .name = "econ", .desc = "Total number of failed connections to server since the worker process started" },
170 [ST_F_ERESP] = { .name = "eresp", .desc = "Total number of invalid responses since the worker process started" },
171 [ST_F_WRETR] = { .name = "wretr", .desc = "Total number of server connection retries since the worker process started" },
172 [ST_F_WREDIS] = { .name = "wredis", .desc = "Total number of server redispatches due to connection failures since the worker process started" },
173 [ST_F_STATUS] = { .name = "status", .desc = "Frontend/listen status: OPEN/WAITING/FULL/STOP; backend: UP/DOWN; server: last check status" },
174 [ST_F_WEIGHT] = { .name = "weight", .desc = "Server weight, or sum of active servers' weights for a backend" },
175 [ST_F_ACT] = { .name = "act", .desc = "Total number of active UP servers with a non-zero weight" },
176 [ST_F_BCK] = { .name = "bck", .desc = "Total number of backup UP servers with a non-zero weight" },
177 [ST_F_CHKFAIL] = { .name = "chkfail", .desc = "Total number of failed individual health checks per server/backend, since the worker process started" },
178 [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" },
179 [ST_F_LASTCHG] = { .name = "lastchg", .desc = "How long ago the last server state changed, in seconds" },
180 [ST_F_DOWNTIME] = { .name = "downtime", .desc = "Total time spent in DOWN state, for server or backend" },
181 [ST_F_QLIMIT] = { .name = "qlimit", .desc = "Limit on the number of connections in queue, for servers only (maxqueue argument)" },
182 [ST_F_PID] = { .name = "pid", .desc = "Relative worker process number (1..nbproc)" },
183 [ST_F_IID] = { .name = "iid", .desc = "Frontend or Backend numeric identifier ('id' setting)" },
184 [ST_F_SID] = { .name = "sid", .desc = "Server numeric identifier ('id' setting)" },
185 [ST_F_THROTTLE] = { .name = "throttle", .desc = "Throttling ratio applied to a server's maxconn and weight during the slowstart period (0 to 100%)" },
186 [ST_F_LBTOT] = { .name = "lbtot", .desc = "Total number of requests routed by load balancing since the worker process started (ignores queue pop and stickiness)" },
187 [ST_F_TRACKED] = { .name = "tracked", .desc = "Name of the other server this server tracks for its state" },
188 [ST_F_TYPE] = { .name = "type", .desc = "Type of the object (Listener, Frontend, Backend, Server)" },
189 [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)" },
190 [ST_F_RATE_LIM] = { .name = "rate_lim", .desc = "Limit on the number of sessions accepted in a second (frontend only, 'rate-limit sessions' setting)" },
191 [ST_F_RATE_MAX] = { .name = "rate_max", .desc = "Highest value of 'rate' observed since the worker process started" },
192 [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" },
193 [ST_F_CHECK_CODE] = { .name = "check_code", .desc = "HTTP/SMTP/LDAP status code reported by the latest server health check" },
194 [ST_F_CHECK_DURATION] = { .name = "check_duration", .desc = "Total duration of the latest server health check, in milliseconds" },
195 [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" },
196 [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" },
197 [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" },
198 [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" },
199 [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" },
200 [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)" },
201 [ST_F_HANAFAIL] = { .name = "hanafail", .desc = "Total number of failed checks caused by an 'on-error' directive after an 'observe' condition matched" },
202 [ST_F_REQ_RATE] = { .name = "req_rate", .desc = "Number of HTTP requests processed over the last second on this object" },
203 [ST_F_REQ_RATE_MAX] = { .name = "req_rate_max", .desc = "Highest value of 'req_rate' observed since the worker process started" },
204 [ST_F_REQ_TOT] = { .name = "req_tot", .desc = "Total number of HTTP requests processed by this object since the worker process started" },
205 [ST_F_CLI_ABRT] = { .name = "cli_abrt", .desc = "Total number of requests or connections aborted by the client since the worker process started" },
206 [ST_F_SRV_ABRT] = { .name = "srv_abrt", .desc = "Total number of requests or connections aborted by the server since the worker process started" },
207 [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" },
208 [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" },
209 [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)" },
210 [ST_F_COMP_RSP] = { .name = "comp_rsp", .desc = "Total number of HTTP responses that were compressed for this object since the worker process started" },
211 [ST_F_LASTSESS] = { .name = "lastsess", .desc = "How long ago some traffic was seen on this object on this worker process, in seconds" },
212 [ST_F_LAST_CHK] = { .name = "last_chk", .desc = "Short description of the latest health check report for this server (see also check_desc)" },
213 [ST_F_LAST_AGT] = { .name = "last_agt", .desc = "Short description of the latest agent check report for this server (see also agent_desc)" },
214 [ST_F_QTIME] = { .name = "qtime", .desc = "Time spent in the queue, in milliseconds, averaged over the 1024 last requests (backend/server)" },
215 [ST_F_CTIME] = { .name = "ctime", .desc = "Time spent waiting for a connection to complete, in milliseconds, averaged over the 1024 last requests (backend/server)" },
216 [ST_F_RTIME] = { .name = "rtime", .desc = "Time spent waiting for a server response, in milliseconds, averaged over the 1024 last requests (backend/server)" },
217 [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)" },
218 [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" },
219 [ST_F_AGENT_CODE] = { .name = "agent_code", .desc = "Status code reported by the latest server agent check" },
220 [ST_F_AGENT_DURATION] = { .name = "agent_duration", .desc = "Total duration of the latest server agent check, in milliseconds" },
221 [ST_F_CHECK_DESC] = { .name = "check_desc", .desc = "Textual description of the latest health check report for this server" },
222 [ST_F_AGENT_DESC] = { .name = "agent_desc", .desc = "Textual description of the latest agent check report for this server" },
223 [ST_F_CHECK_RISE] = { .name = "check_rise", .desc = "Number of successful health checks before declaring a server UP (server 'rise' setting)" },
224 [ST_F_CHECK_FALL] = { .name = "check_fall", .desc = "Number of failed health checks before declaring a server DOWN (server 'fall' setting)" },
225 [ST_F_CHECK_HEALTH] = { .name = "check_health", .desc = "Current server health check level (0..fall-1=DOWN, fall..rise-1=UP)" },
226 [ST_F_AGENT_RISE] = { .name = "agent_rise", .desc = "Number of successful agent checks before declaring a server UP (server 'rise' setting)" },
227 [ST_F_AGENT_FALL] = { .name = "agent_fall", .desc = "Number of failed agent checks before declaring a server DOWN (server 'fall' setting)" },
228 [ST_F_AGENT_HEALTH] = { .name = "agent_health", .desc = "Current server agent check level (0..fall-1=DOWN, fall..rise-1=UP)" },
229 [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" },
230 [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" },
231 [ST_F_MODE] = { .name = "mode", .desc = "'mode' setting (tcp/http/health/cli)" },
232 [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" },
233 [ST_F_CONN_RATE] = { .name = "conn_rate", .desc = "Number of new connections accepted over the last second on the frontend for this worker process" },
234 [ST_F_CONN_RATE_MAX] = { .name = "conn_rate_max", .desc = "Highest value of 'conn_rate' observed since the worker process started" },
235 [ST_F_CONN_TOT] = { .name = "conn_tot", .desc = "Total number of new connections accepted on this frontend since the worker process started" },
236 [ST_F_INTERCEPTED] = { .name = "intercepted", .desc = "Total number of HTTP requests intercepted on the frontend (redirects/stats/services) since the worker process started" },
237 [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" },
238 [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" },
239 [ST_F_WREW] = { .name = "wrew", .desc = "Total number of failed HTTP header rewrites since the worker process started" },
240 [ST_F_CONNECT] = { .name = "connect", .desc = "Total number of outgoing connection attempts on this backend/server since the worker process started" },
241 [ST_F_REUSE] = { .name = "reuse", .desc = "Total number of reused connection on this backend/server since the worker process started" },
242 [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" },
243 [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" },
244 [ST_F_SRV_ICUR] = { .name = "srv_icur", .desc = "Current number of idle connections available for reuse on this server" },
245 [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 +0100246 [ST_F_QT_MAX] = { .name = "qtime_max", .desc = "Maximum observed time spent in the queue, in milliseconds (backend/server)" },
247 [ST_F_CT_MAX] = { .name = "ctime_max", .desc = "Maximum observed time spent waiting for a connection to complete, in milliseconds (backend/server)" },
248 [ST_F_RT_MAX] = { .name = "rtime_max", .desc = "Maximum observed time spent waiting for a server response, in milliseconds (backend/server)" },
249 [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 +0100250 [ST_F_EINT] = { .name = "eint", .desc = "Total number of internal errors since process started"},
William Lallemand74c24fb2016-11-21 17:18:36 +0100251};
252
Willy Tarreau0baac8c2016-11-22 16:36:53 +0100253/* one line of info */
Christopher Faulet1bc04c72017-10-29 20:14:08 +0100254static THREAD_LOCAL struct field info[INF_TOTAL_FIELDS];
William Lallemand74c24fb2016-11-21 17:18:36 +0100255/* one line of stats */
Christopher Faulet1bc04c72017-10-29 20:14:08 +0100256static THREAD_LOCAL struct field stats[ST_F_TOTAL_FIELDS];
William Lallemand74c24fb2016-11-21 17:18:36 +0100257
Christopher Faulet6338a082019-09-09 15:50:54 +0200258static void stats_dump_json_schema(struct buffer *out);
William Lallemand74c24fb2016-11-21 17:18:36 +0100259
Christopher Fauletef779222018-10-31 08:47:01 +0100260static int stats_putchk(struct channel *chn, struct htx *htx, struct buffer *chk)
261{
262 if (htx) {
Christopher Faulet69fc88c2019-01-07 14:27:53 +0100263 if (chk->data >= channel_htx_recv_max(chn, htx))
264 return 0;
Willy Tarreau0a7ef022019-05-28 10:30:11 +0200265 if (!htx_add_data_atonce(htx, ist2(chk->area, chk->data)))
Christopher Fauletef779222018-10-31 08:47:01 +0100266 return 0;
Christopher Faulet5adbeeb2019-01-02 14:34:39 +0100267 channel_add_input(chn, chk->data);
Christopher Fauletef779222018-10-31 08:47:01 +0100268 chk->data = 0;
Christopher Fauletef779222018-10-31 08:47:01 +0100269 }
270 else {
271 if (ci_putchk(chn, chk) == -1)
272 return 0;
273 }
274 return 1;
275}
Willy Tarreau0baac8c2016-11-22 16:36:53 +0100276
Christopher Fauleted7a0662019-01-14 11:07:34 +0100277static const char *stats_scope_ptr(struct appctx *appctx, struct stream_interface *si)
278{
Christopher Fauletb7f88902019-07-15 21:56:43 +0200279 struct channel *req = si_oc(si);
280 struct htx *htx = htxbuf(&req->buf);
281 struct htx_blk *blk;
282 struct ist uri;
Christopher Fauleted7a0662019-01-14 11:07:34 +0100283
Christopher Fauletb7f88902019-07-15 21:56:43 +0200284 blk = htx_get_head_blk(htx);
Christopher Fauletea009732019-11-18 15:50:25 +0100285 BUG_ON(!blk || htx_get_blk_type(blk) != HTX_BLK_REQ_SL);
Christopher Fauletb7f88902019-07-15 21:56:43 +0200286 ALREADY_CHECKED(blk);
287 uri = htx_sl_req_uri(htx_get_blk_ptr(htx, blk));
288 return uri.ptr + appctx->ctx.stats.scope_str;
Christopher Fauleted7a0662019-01-14 11:07:34 +0100289}
290
William Lallemand74c24fb2016-11-21 17:18:36 +0100291/*
292 * http_stats_io_handler()
293 * -> stats_dump_stat_to_buffer() // same as above, but used for CSV or HTML
294 * -> stats_dump_csv_header() // emits the CSV headers (same as above)
Simon Horman05ee2132017-01-04 09:37:25 +0100295 * -> stats_dump_json_header() // emits the JSON headers (same as above)
William Lallemand74c24fb2016-11-21 17:18:36 +0100296 * -> stats_dump_html_head() // emits the HTML headers
297 * -> stats_dump_html_info() // emits the equivalent of "show info" at the top
298 * -> stats_dump_proxy_to_buffer() // same as above, valid for CSV and HTML
299 * -> stats_dump_html_px_hdr()
300 * -> stats_dump_fe_stats()
301 * -> stats_dump_li_stats()
302 * -> stats_dump_sv_stats()
303 * -> stats_dump_be_stats()
304 * -> stats_dump_html_px_end()
305 * -> stats_dump_html_end() // emits HTML trailer
Simon Horman05ee2132017-01-04 09:37:25 +0100306 * -> stats_dump_json_end() // emits JSON trailer
William Lallemand74c24fb2016-11-21 17:18:36 +0100307 */
308
309
William Lallemand74c24fb2016-11-21 17:18:36 +0100310/* Dumps the stats CSV header to the trash buffer which. The caller is responsible
311 * for clearing it if needed.
312 * NOTE: Some tools happen to rely on the field position instead of its name,
313 * so please only append new fields at the end, never in the middle.
314 */
315static void stats_dump_csv_header()
316{
317 int field;
318
319 chunk_appendf(&trash, "# ");
320 for (field = 0; field < ST_F_TOTAL_FIELDS; field++)
Willy Tarreaueaa55372019-10-09 07:39:11 +0200321 chunk_appendf(&trash, "%s,", stat_fields[field].name);
William Lallemand74c24fb2016-11-21 17:18:36 +0100322
323 chunk_appendf(&trash, "\n");
324}
325
326
327/* Emits a stats field without any surrounding element and properly encoded to
328 * resist CSV output. Returns non-zero on success, 0 if the buffer is full.
329 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200330int stats_emit_raw_data_field(struct buffer *out, const struct field *f)
William Lallemand74c24fb2016-11-21 17:18:36 +0100331{
332 switch (field_format(f, 0)) {
333 case FF_EMPTY: return 1;
334 case FF_S32: return chunk_appendf(out, "%d", f->u.s32);
335 case FF_U32: return chunk_appendf(out, "%u", f->u.u32);
336 case FF_S64: return chunk_appendf(out, "%lld", (long long)f->u.s64);
337 case FF_U64: return chunk_appendf(out, "%llu", (unsigned long long)f->u.u64);
Christopher Faulet88a0db22019-09-24 16:35:10 +0200338 case FF_FLT: return chunk_appendf(out, "%f", f->u.flt);
William Lallemand74c24fb2016-11-21 17:18:36 +0100339 case FF_STR: return csv_enc_append(field_str(f, 0), 1, out) != NULL;
340 default: return chunk_appendf(out, "[INCORRECT_FIELD_TYPE_%08x]", f->type);
341 }
342}
343
344/* Emits a stats field prefixed with its type. No CSV encoding is prepared, the
345 * output is supposed to be used on its own line. Returns non-zero on success, 0
346 * if the buffer is full.
347 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200348int stats_emit_typed_data_field(struct buffer *out, const struct field *f)
William Lallemand74c24fb2016-11-21 17:18:36 +0100349{
350 switch (field_format(f, 0)) {
351 case FF_EMPTY: return 1;
352 case FF_S32: return chunk_appendf(out, "s32:%d", f->u.s32);
353 case FF_U32: return chunk_appendf(out, "u32:%u", f->u.u32);
354 case FF_S64: return chunk_appendf(out, "s64:%lld", (long long)f->u.s64);
355 case FF_U64: return chunk_appendf(out, "u64:%llu", (unsigned long long)f->u.u64);
Christopher Faulet88a0db22019-09-24 16:35:10 +0200356 case FF_FLT: return chunk_appendf(out, "flt:%f", f->u.flt);
William Lallemand74c24fb2016-11-21 17:18:36 +0100357 case FF_STR: return chunk_appendf(out, "str:%s", field_str(f, 0));
358 default: return chunk_appendf(out, "%08x:?", f->type);
359 }
360}
361
Simon Horman05ee2132017-01-04 09:37:25 +0100362/* Limit JSON integer values to the range [-(2**53)+1, (2**53)-1] as per
363 * the recommendation for interoperable integers in section 6 of RFC 7159.
364 */
365#define JSON_INT_MAX ((1ULL << 53) - 1)
366#define JSON_INT_MIN (0 - JSON_INT_MAX)
367
368/* Emits a stats field value and its type in JSON.
369 * Returns non-zero on success, 0 on error.
370 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200371int stats_emit_json_data_field(struct buffer *out, const struct field *f)
Simon Horman05ee2132017-01-04 09:37:25 +0100372{
373 int old_len;
374 char buf[20];
375 const char *type, *value = buf, *quote = "";
376
377 switch (field_format(f, 0)) {
378 case FF_EMPTY: return 1;
379 case FF_S32: type = "\"s32\"";
380 snprintf(buf, sizeof(buf), "%d", f->u.s32);
381 break;
382 case FF_U32: type = "\"u32\"";
383 snprintf(buf, sizeof(buf), "%u", f->u.u32);
384 break;
385 case FF_S64: type = "\"s64\"";
386 if (f->u.s64 < JSON_INT_MIN || f->u.s64 > JSON_INT_MAX)
387 return 0;
388 type = "\"s64\"";
389 snprintf(buf, sizeof(buf), "%lld", (long long)f->u.s64);
390 break;
391 case FF_U64: if (f->u.u64 > JSON_INT_MAX)
392 return 0;
393 type = "\"u64\"";
394 snprintf(buf, sizeof(buf), "%llu",
395 (unsigned long long) f->u.u64);
Christopher Faulet52c91bb2019-09-28 10:37:31 +0200396 break;
Christopher Faulet88a0db22019-09-24 16:35:10 +0200397 case FF_FLT: type = "\"flt\"";
398 snprintf(buf, sizeof(buf), "%f", f->u.flt);
Simon Horman05ee2132017-01-04 09:37:25 +0100399 break;
400 case FF_STR: type = "\"str\"";
401 value = field_str(f, 0);
402 quote = "\"";
403 break;
404 default: snprintf(buf, sizeof(buf), "%u", f->type);
405 type = buf;
406 value = "unknown";
407 quote = "\"";
408 break;
409 }
410
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200411 old_len = out->data;
Simon Horman05ee2132017-01-04 09:37:25 +0100412 chunk_appendf(out, ",\"value\":{\"type\":%s,\"value\":%s%s%s}",
413 type, quote, value, quote);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200414 return !(old_len == out->data);
Simon Horman05ee2132017-01-04 09:37:25 +0100415}
416
William Lallemand74c24fb2016-11-21 17:18:36 +0100417/* Emits an encoding of the field type on 3 characters followed by a delimiter.
418 * Returns non-zero on success, 0 if the buffer is full.
419 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200420int stats_emit_field_tags(struct buffer *out, const struct field *f,
421 char delim)
William Lallemand74c24fb2016-11-21 17:18:36 +0100422{
423 char origin, nature, scope;
424
425 switch (field_origin(f, 0)) {
426 case FO_METRIC: origin = 'M'; break;
427 case FO_STATUS: origin = 'S'; break;
428 case FO_KEY: origin = 'K'; break;
429 case FO_CONFIG: origin = 'C'; break;
430 case FO_PRODUCT: origin = 'P'; break;
431 default: origin = '?'; break;
432 }
433
434 switch (field_nature(f, 0)) {
435 case FN_GAUGE: nature = 'G'; break;
436 case FN_LIMIT: nature = 'L'; break;
437 case FN_MIN: nature = 'm'; break;
438 case FN_MAX: nature = 'M'; break;
439 case FN_RATE: nature = 'R'; break;
440 case FN_COUNTER: nature = 'C'; break;
441 case FN_DURATION: nature = 'D'; break;
442 case FN_AGE: nature = 'A'; break;
443 case FN_TIME: nature = 'T'; break;
444 case FN_NAME: nature = 'N'; break;
445 case FN_OUTPUT: nature = 'O'; break;
446 case FN_AVG: nature = 'a'; break;
447 default: nature = '?'; break;
448 }
449
450 switch (field_scope(f, 0)) {
451 case FS_PROCESS: scope = 'P'; break;
452 case FS_SERVICE: scope = 'S'; break;
453 case FS_SYSTEM: scope = 's'; break;
454 case FS_CLUSTER: scope = 'C'; break;
455 default: scope = '?'; break;
456 }
457
458 return chunk_appendf(out, "%c%c%c%c", origin, nature, scope, delim);
459}
460
Simon Horman05ee2132017-01-04 09:37:25 +0100461/* Emits an encoding of the field type as JSON.
462 * Returns non-zero on success, 0 if the buffer is full.
463 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200464int stats_emit_json_field_tags(struct buffer *out, const struct field *f)
Simon Horman05ee2132017-01-04 09:37:25 +0100465{
466 const char *origin, *nature, *scope;
467 int old_len;
468
469 switch (field_origin(f, 0)) {
470 case FO_METRIC: origin = "Metric"; break;
471 case FO_STATUS: origin = "Status"; break;
472 case FO_KEY: origin = "Key"; break;
473 case FO_CONFIG: origin = "Config"; break;
474 case FO_PRODUCT: origin = "Product"; break;
475 default: origin = "Unknown"; break;
476 }
477
478 switch (field_nature(f, 0)) {
479 case FN_GAUGE: nature = "Gauge"; break;
480 case FN_LIMIT: nature = "Limit"; break;
481 case FN_MIN: nature = "Min"; break;
482 case FN_MAX: nature = "Max"; break;
483 case FN_RATE: nature = "Rate"; break;
484 case FN_COUNTER: nature = "Counter"; break;
485 case FN_DURATION: nature = "Duration"; break;
486 case FN_AGE: nature = "Age"; break;
487 case FN_TIME: nature = "Time"; break;
488 case FN_NAME: nature = "Name"; break;
489 case FN_OUTPUT: nature = "Output"; break;
490 case FN_AVG: nature = "Avg"; break;
491 default: nature = "Unknown"; break;
492 }
493
494 switch (field_scope(f, 0)) {
495 case FS_PROCESS: scope = "Process"; break;
496 case FS_SERVICE: scope = "Service"; break;
497 case FS_SYSTEM: scope = "System"; break;
498 case FS_CLUSTER: scope = "Cluster"; break;
499 default: scope = "Unknown"; break;
500 }
501
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200502 old_len = out->data;
Simon Horman05ee2132017-01-04 09:37:25 +0100503 chunk_appendf(out, "\"tags\":{"
504 "\"origin\":\"%s\","
505 "\"nature\":\"%s\","
506 "\"scope\":\"%s\""
507 "}", origin, nature, scope);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200508 return !(old_len == out->data);
Simon Horman05ee2132017-01-04 09:37:25 +0100509}
William Lallemand74c24fb2016-11-21 17:18:36 +0100510
511/* Dump all fields from <stats> into <out> using CSV format */
Willy Tarreau83061a82018-07-13 11:56:34 +0200512static int stats_dump_fields_csv(struct buffer *out,
Willy Tarreau43241ff2019-10-09 11:27:51 +0200513 const struct field *stats, unsigned int flags)
William Lallemand74c24fb2016-11-21 17:18:36 +0100514{
515 int field;
516
517 for (field = 0; field < ST_F_TOTAL_FIELDS; field++) {
518 if (!stats_emit_raw_data_field(out, &stats[field]))
519 return 0;
520 if (!chunk_strcat(out, ","))
521 return 0;
522 }
523 chunk_strcat(out, "\n");
524 return 1;
525}
526
527/* Dump all fields from <stats> into <out> using a typed "field:desc:type:value" format */
Willy Tarreau83061a82018-07-13 11:56:34 +0200528static int stats_dump_fields_typed(struct buffer *out,
Willy Tarreau43241ff2019-10-09 11:27:51 +0200529 const struct field *stats, unsigned int flags)
William Lallemand74c24fb2016-11-21 17:18:36 +0100530{
531 int field;
532
533 for (field = 0; field < ST_F_TOTAL_FIELDS; field++) {
534 if (!stats[field].type)
535 continue;
536
537 chunk_appendf(out, "%c.%u.%u.%d.%s.%u:",
538 stats[ST_F_TYPE].u.u32 == STATS_TYPE_FE ? 'F' :
539 stats[ST_F_TYPE].u.u32 == STATS_TYPE_BE ? 'B' :
540 stats[ST_F_TYPE].u.u32 == STATS_TYPE_SO ? 'L' :
541 stats[ST_F_TYPE].u.u32 == STATS_TYPE_SV ? 'S' :
542 '?',
543 stats[ST_F_IID].u.u32, stats[ST_F_SID].u.u32,
Willy Tarreaueaa55372019-10-09 07:39:11 +0200544 field, stat_fields[field].name, stats[ST_F_PID].u.u32);
William Lallemand74c24fb2016-11-21 17:18:36 +0100545
546 if (!stats_emit_field_tags(out, &stats[field], ':'))
547 return 0;
548 if (!stats_emit_typed_data_field(out, &stats[field]))
549 return 0;
Willy Tarreau6b19b142019-10-09 15:44:21 +0200550 if ((flags & STAT_SHOW_FDESC) && !chunk_appendf(out, ":\"%s\"", stat_fields[field].desc))
551 return 0;
William Lallemand74c24fb2016-11-21 17:18:36 +0100552 if (!chunk_strcat(out, "\n"))
553 return 0;
554 }
555 return 1;
556}
557
Simon Horman05ee2132017-01-04 09:37:25 +0100558/* Dump all fields from <stats> into <out> using the "show info json" format */
Willy Tarreau83061a82018-07-13 11:56:34 +0200559static int stats_dump_json_info_fields(struct buffer *out,
Willy Tarreau43241ff2019-10-09 11:27:51 +0200560 const struct field *info, unsigned int flags)
Simon Horman05ee2132017-01-04 09:37:25 +0100561{
562 int field;
563 int started = 0;
564
565 if (!chunk_strcat(out, "["))
566 return 0;
567
568 for (field = 0; field < INF_TOTAL_FIELDS; field++) {
569 int old_len;
570
571 if (!field_format(info, field))
572 continue;
573
574 if (started && !chunk_strcat(out, ","))
575 goto err;
576 started = 1;
577
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200578 old_len = out->data;
Simon Horman05ee2132017-01-04 09:37:25 +0100579 chunk_appendf(out,
580 "{\"field\":{\"pos\":%d,\"name\":\"%s\"},"
581 "\"processNum\":%u,",
Willy Tarreaueaa55372019-10-09 07:39:11 +0200582 field, info_fields[field].name,
Simon Horman05ee2132017-01-04 09:37:25 +0100583 info[INF_PROCESS_NUM].u.u32);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200584 if (old_len == out->data)
Simon Horman05ee2132017-01-04 09:37:25 +0100585 goto err;
586
587 if (!stats_emit_json_field_tags(out, &info[field]))
588 goto err;
589
590 if (!stats_emit_json_data_field(out, &info[field]))
591 goto err;
592
593 if (!chunk_strcat(out, "}"))
594 goto err;
595 }
596
597 if (!chunk_strcat(out, "]"))
598 goto err;
599 return 1;
600
601err:
602 chunk_reset(out);
603 chunk_appendf(out, "{\"errorStr\":\"output buffer too short\"}");
604 return 0;
605}
606
607/* Dump all fields from <stats> into <out> using a typed "field:desc:type:value" format */
Willy Tarreau83061a82018-07-13 11:56:34 +0200608static int stats_dump_fields_json(struct buffer *out,
609 const struct field *stats,
Willy Tarreaub0ce3ad2019-10-09 11:19:29 +0200610 unsigned int flags)
Simon Horman05ee2132017-01-04 09:37:25 +0100611{
612 int field;
613 int started = 0;
614
Willy Tarreaub0ce3ad2019-10-09 11:19:29 +0200615 if ((flags & STAT_STARTED) && !chunk_strcat(out, ","))
Simon Horman05ee2132017-01-04 09:37:25 +0100616 return 0;
617 if (!chunk_strcat(out, "["))
618 return 0;
619
620 for (field = 0; field < ST_F_TOTAL_FIELDS; field++) {
621 const char *obj_type;
622 int old_len;
623
624 if (!stats[field].type)
625 continue;
626
627 if (started && !chunk_strcat(out, ","))
628 goto err;
629 started = 1;
630
631 switch (stats[ST_F_TYPE].u.u32) {
632 case STATS_TYPE_FE: obj_type = "Frontend"; break;
633 case STATS_TYPE_BE: obj_type = "Backend"; break;
634 case STATS_TYPE_SO: obj_type = "Listener"; break;
635 case STATS_TYPE_SV: obj_type = "Server"; break;
636 default: obj_type = "Unknown"; break;
637 }
638
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200639 old_len = out->data;
Simon Horman05ee2132017-01-04 09:37:25 +0100640 chunk_appendf(out,
641 "{"
642 "\"objType\":\"%s\","
643 "\"proxyId\":%d,"
644 "\"id\":%d,"
645 "\"field\":{\"pos\":%d,\"name\":\"%s\"},"
646 "\"processNum\":%u,",
647 obj_type, stats[ST_F_IID].u.u32,
648 stats[ST_F_SID].u.u32, field,
Willy Tarreaueaa55372019-10-09 07:39:11 +0200649 stat_fields[field].name, stats[ST_F_PID].u.u32);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200650 if (old_len == out->data)
Simon Horman05ee2132017-01-04 09:37:25 +0100651 goto err;
652
653 if (!stats_emit_json_field_tags(out, &stats[field]))
654 goto err;
655
656 if (!stats_emit_json_data_field(out, &stats[field]))
657 goto err;
658
659 if (!chunk_strcat(out, "}"))
660 goto err;
661 }
662
663 if (!chunk_strcat(out, "]"))
664 goto err;
665
666 return 1;
667
668err:
669 chunk_reset(out);
Willy Tarreaub0ce3ad2019-10-09 11:19:29 +0200670 if (flags & STAT_STARTED)
671 chunk_strcat(out, ",");
Simon Horman05ee2132017-01-04 09:37:25 +0100672 chunk_appendf(out, "{\"errorStr\":\"output buffer too short\"}");
673 return 0;
674}
675
William Lallemand74c24fb2016-11-21 17:18:36 +0100676/* Dump all fields from <stats> into <out> using the HTML format. A column is
Willy Tarreauab02b3f2019-10-09 11:11:46 +0200677 * reserved for the checkbox is STAT_ADMIN is set in <flags>. Some extra info
Willy Tarreau708c4162019-10-09 10:19:16 +0200678 * are provided if STAT_SHLGNDS is present in <flags>.
William Lallemand74c24fb2016-11-21 17:18:36 +0100679 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200680static int stats_dump_fields_html(struct buffer *out,
681 const struct field *stats,
682 unsigned int flags)
William Lallemand74c24fb2016-11-21 17:18:36 +0100683{
Willy Tarreau83061a82018-07-13 11:56:34 +0200684 struct buffer src;
William Lallemand74c24fb2016-11-21 17:18:36 +0100685
686 if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_FE) {
687 chunk_appendf(out,
688 /* name, queue */
689 "<tr class=\"frontend\">");
690
Willy Tarreauab02b3f2019-10-09 11:11:46 +0200691 if (flags & STAT_ADMIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +0100692 /* Column sub-heading for Enable or Disable server */
693 chunk_appendf(out, "<td></td>");
694 }
695
696 chunk_appendf(out,
697 "<td class=ac>"
698 "<a name=\"%s/Frontend\"></a>"
699 "<a class=lfsb href=\"#%s/Frontend\">Frontend</a></td>"
700 "<td colspan=3></td>"
701 "",
702 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_PXNAME));
703
704 chunk_appendf(out,
705 /* sessions rate : current */
706 "<td><u>%s<div class=tips><table class=det>"
707 "<tr><th>Current connection rate:</th><td>%s/s</td></tr>"
708 "<tr><th>Current session rate:</th><td>%s/s</td></tr>"
709 "",
710 U2H(stats[ST_F_RATE].u.u32),
711 U2H(stats[ST_F_CONN_RATE].u.u32),
712 U2H(stats[ST_F_RATE].u.u32));
713
714 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
715 chunk_appendf(out,
716 "<tr><th>Current request rate:</th><td>%s/s</td></tr>",
717 U2H(stats[ST_F_REQ_RATE].u.u32));
718
719 chunk_appendf(out,
720 "</table></div></u></td>"
721 /* sessions rate : max */
722 "<td><u>%s<div class=tips><table class=det>"
723 "<tr><th>Max connection rate:</th><td>%s/s</td></tr>"
724 "<tr><th>Max session rate:</th><td>%s/s</td></tr>"
725 "",
726 U2H(stats[ST_F_RATE_MAX].u.u32),
727 U2H(stats[ST_F_CONN_RATE_MAX].u.u32),
728 U2H(stats[ST_F_RATE_MAX].u.u32));
729
730 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
731 chunk_appendf(out,
732 "<tr><th>Max request rate:</th><td>%s/s</td></tr>",
733 U2H(stats[ST_F_REQ_RATE_MAX].u.u32));
734
735 chunk_appendf(out,
736 "</table></div></u></td>"
737 /* sessions rate : limit */
738 "<td>%s</td>",
739 LIM2A(stats[ST_F_RATE_LIM].u.u32, "-"));
740
741 chunk_appendf(out,
742 /* sessions: current, max, limit, total */
743 "<td>%s</td><td>%s</td><td>%s</td>"
744 "<td><u>%s<div class=tips><table class=det>"
745 "<tr><th>Cum. connections:</th><td>%s</td></tr>"
746 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
747 "",
748 U2H(stats[ST_F_SCUR].u.u32), U2H(stats[ST_F_SMAX].u.u32), U2H(stats[ST_F_SLIM].u.u32),
749 U2H(stats[ST_F_STOT].u.u64),
750 U2H(stats[ST_F_CONN_TOT].u.u64),
751 U2H(stats[ST_F_STOT].u.u64));
752
753 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
754 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0) {
755 chunk_appendf(out,
756 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
757 "<tr><th>- HTTP 1xx responses:</th><td>%s</td></tr>"
758 "<tr><th>- HTTP 2xx responses:</th><td>%s</td></tr>"
759 "<tr><th>&nbsp;&nbsp;Compressed 2xx:</th><td>%s</td><td>(%d%%)</td></tr>"
760 "<tr><th>- HTTP 3xx responses:</th><td>%s</td></tr>"
761 "<tr><th>- HTTP 4xx responses:</th><td>%s</td></tr>"
762 "<tr><th>- HTTP 5xx responses:</th><td>%s</td></tr>"
763 "<tr><th>- other responses:</th><td>%s</td></tr>"
764 "<tr><th>Intercepted requests:</th><td>%s</td></tr>"
Willy Tarreaua1214a52018-12-14 14:00:25 +0100765 "<tr><th>Cache lookups:</th><td>%s</td></tr>"
766 "<tr><th>Cache hits:</th><td>%s</td><td>(%d%%)</td></tr>"
Willy Tarreau1b0f85e2018-05-28 15:12:40 +0200767 "<tr><th>Failed hdr rewrites:</th><td>%s</td></tr>"
Christopher Faulet0159ee42019-12-16 14:40:39 +0100768 "<tr><th>Internal errors:</th><td>%s</td></tr>"
William Lallemand74c24fb2016-11-21 17:18:36 +0100769 "",
770 U2H(stats[ST_F_REQ_TOT].u.u64),
771 U2H(stats[ST_F_HRSP_1XX].u.u64),
772 U2H(stats[ST_F_HRSP_2XX].u.u64),
773 U2H(stats[ST_F_COMP_RSP].u.u64),
774 stats[ST_F_HRSP_2XX].u.u64 ?
775 (int)(100 * stats[ST_F_COMP_RSP].u.u64 / stats[ST_F_HRSP_2XX].u.u64) : 0,
776 U2H(stats[ST_F_HRSP_3XX].u.u64),
777 U2H(stats[ST_F_HRSP_4XX].u.u64),
778 U2H(stats[ST_F_HRSP_5XX].u.u64),
779 U2H(stats[ST_F_HRSP_OTHER].u.u64),
Willy Tarreau1b0f85e2018-05-28 15:12:40 +0200780 U2H(stats[ST_F_INTERCEPTED].u.u64),
Willy Tarreaua1214a52018-12-14 14:00:25 +0100781 U2H(stats[ST_F_CACHE_LOOKUPS].u.u64),
782 U2H(stats[ST_F_CACHE_HITS].u.u64),
783 stats[ST_F_CACHE_LOOKUPS].u.u64 ?
784 (int)(100 * stats[ST_F_CACHE_HITS].u.u64 / stats[ST_F_CACHE_LOOKUPS].u.u64) : 0,
Christopher Faulet0159ee42019-12-16 14:40:39 +0100785 U2H(stats[ST_F_WREW].u.u64),
786 U2H(stats[ST_F_EINT].u.u64));
William Lallemand74c24fb2016-11-21 17:18:36 +0100787 }
788
789 chunk_appendf(out,
790 "</table></div></u></td>"
791 /* sessions: lbtot, lastsess */
792 "<td></td><td></td>"
793 /* bytes : in */
794 "<td>%s</td>"
795 "",
796 U2H(stats[ST_F_BIN].u.u64));
797
798 chunk_appendf(out,
799 /* bytes:out + compression stats (via hover): comp_in, comp_out, comp_byp */
800 "<td>%s%s<div class=tips><table class=det>"
801 "<tr><th>Response bytes in:</th><td>%s</td></tr>"
802 "<tr><th>Compression in:</th><td>%s</td></tr>"
803 "<tr><th>Compression out:</th><td>%s</td><td>(%d%%)</td></tr>"
804 "<tr><th>Compression bypass:</th><td>%s</td></tr>"
805 "<tr><th>Total bytes saved:</th><td>%s</td><td>(%d%%)</td></tr>"
806 "</table></div>%s</td>",
807 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "<u>":"",
808 U2H(stats[ST_F_BOUT].u.u64),
809 U2H(stats[ST_F_BOUT].u.u64),
810 U2H(stats[ST_F_COMP_IN].u.u64),
811 U2H(stats[ST_F_COMP_OUT].u.u64),
812 stats[ST_F_COMP_IN].u.u64 ? (int)(stats[ST_F_COMP_OUT].u.u64 * 100 / stats[ST_F_COMP_IN].u.u64) : 0,
813 U2H(stats[ST_F_COMP_BYP].u.u64),
814 U2H(stats[ST_F_COMP_IN].u.u64 - stats[ST_F_COMP_OUT].u.u64),
815 stats[ST_F_BOUT].u.u64 ? (int)((stats[ST_F_COMP_IN].u.u64 - stats[ST_F_COMP_OUT].u.u64) * 100 / stats[ST_F_BOUT].u.u64) : 0,
816 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "</u>":"");
817
818 chunk_appendf(out,
819 /* denied: req, resp */
820 "<td>%s</td><td>%s</td>"
821 /* errors : request, connect, response */
822 "<td>%s</td><td></td><td></td>"
823 /* warnings: retries, redispatches */
824 "<td></td><td></td>"
825 /* server status : reflect frontend status */
826 "<td class=ac>%s</td>"
827 /* rest of server: nothing */
828 "<td class=ac colspan=8></td></tr>"
829 "",
830 U2H(stats[ST_F_DREQ].u.u64), U2H(stats[ST_F_DRESP].u.u64),
831 U2H(stats[ST_F_EREQ].u.u64),
832 field_str(stats, ST_F_STATUS));
833 }
834 else if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_SO) {
835 chunk_appendf(out, "<tr class=socket>");
Willy Tarreauab02b3f2019-10-09 11:11:46 +0200836 if (flags & STAT_ADMIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +0100837 /* Column sub-heading for Enable or Disable server */
838 chunk_appendf(out, "<td></td>");
839 }
840
841 chunk_appendf(out,
842 /* frontend name, listener name */
843 "<td class=ac><a name=\"%s/+%s\"></a>%s"
844 "<a class=lfsb href=\"#%s/+%s\">%s</a>"
845 "",
846 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME),
Willy Tarreau708c4162019-10-09 10:19:16 +0200847 (flags & STAT_SHLGNDS)?"<u>":"",
William Lallemand74c24fb2016-11-21 17:18:36 +0100848 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME), field_str(stats, ST_F_SVNAME));
849
Willy Tarreau708c4162019-10-09 10:19:16 +0200850 if (flags & STAT_SHLGNDS) {
William Lallemand74c24fb2016-11-21 17:18:36 +0100851 chunk_appendf(out, "<div class=tips>");
852
Willy Tarreau90807112020-02-25 08:16:33 +0100853 if (isdigit((unsigned char)*field_str(stats, ST_F_ADDR)))
William Lallemand74c24fb2016-11-21 17:18:36 +0100854 chunk_appendf(out, "IPv4: %s, ", field_str(stats, ST_F_ADDR));
855 else if (*field_str(stats, ST_F_ADDR) == '[')
856 chunk_appendf(out, "IPv6: %s, ", field_str(stats, ST_F_ADDR));
857 else if (*field_str(stats, ST_F_ADDR))
858 chunk_appendf(out, "%s, ", field_str(stats, ST_F_ADDR));
859
860 /* id */
861 chunk_appendf(out, "id: %d</div>", stats[ST_F_SID].u.u32);
862 }
863
864 chunk_appendf(out,
865 /* queue */
866 "%s</td><td colspan=3></td>"
867 /* sessions rate: current, max, limit */
868 "<td colspan=3>&nbsp;</td>"
869 /* sessions: current, max, limit, total, lbtot, lastsess */
870 "<td>%s</td><td>%s</td><td>%s</td>"
871 "<td>%s</td><td>&nbsp;</td><td>&nbsp;</td>"
872 /* bytes: in, out */
873 "<td>%s</td><td>%s</td>"
874 "",
Willy Tarreau708c4162019-10-09 10:19:16 +0200875 (flags & STAT_SHLGNDS)?"</u>":"",
William Lallemand74c24fb2016-11-21 17:18:36 +0100876 U2H(stats[ST_F_SCUR].u.u32), U2H(stats[ST_F_SMAX].u.u32), U2H(stats[ST_F_SLIM].u.u32),
877 U2H(stats[ST_F_STOT].u.u64), U2H(stats[ST_F_BIN].u.u64), U2H(stats[ST_F_BOUT].u.u64));
878
879 chunk_appendf(out,
880 /* denied: req, resp */
881 "<td>%s</td><td>%s</td>"
882 /* errors: request, connect, response */
883 "<td>%s</td><td></td><td></td>"
884 /* warnings: retries, redispatches */
885 "<td></td><td></td>"
886 /* server status: reflect listener status */
887 "<td class=ac>%s</td>"
888 /* rest of server: nothing */
889 "<td class=ac colspan=8></td></tr>"
890 "",
891 U2H(stats[ST_F_DREQ].u.u64), U2H(stats[ST_F_DRESP].u.u64),
892 U2H(stats[ST_F_EREQ].u.u64),
893 field_str(stats, ST_F_STATUS));
894 }
895 else if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_SV) {
896 const char *style;
897
898 /* determine the style to use depending on the server's state,
899 * its health and weight. There isn't a 1-to-1 mapping between
900 * state and styles for the cases where the server is (still)
901 * up. The reason is that we don't want to report nolb and
902 * drain with the same color.
903 */
904
905 if (strcmp(field_str(stats, ST_F_STATUS), "DOWN") == 0 ||
906 strcmp(field_str(stats, ST_F_STATUS), "DOWN (agent)") == 0) {
907 style = "down";
908 }
909 else if (strcmp(field_str(stats, ST_F_STATUS), "DOWN ") == 0) {
910 style = "going_up";
911 }
Daniel Corbettb4285172020-03-28 12:35:50 -0400912 else if (strcmp(field_str(stats, ST_F_STATUS), "DRAIN") == 0) {
913 style = "draining";
914 }
William Lallemand74c24fb2016-11-21 17:18:36 +0100915 else if (strcmp(field_str(stats, ST_F_STATUS), "NOLB ") == 0) {
916 style = "going_down";
917 }
918 else if (strcmp(field_str(stats, ST_F_STATUS), "NOLB") == 0) {
919 style = "nolb";
920 }
921 else if (strcmp(field_str(stats, ST_F_STATUS), "no check") == 0) {
922 style = "no_check";
923 }
924 else if (!stats[ST_F_CHKFAIL].type ||
925 stats[ST_F_CHECK_HEALTH].u.u32 == stats[ST_F_CHECK_RISE].u.u32 + stats[ST_F_CHECK_FALL].u.u32 - 1) {
926 /* no check or max health = UP */
927 if (stats[ST_F_WEIGHT].u.u32)
928 style = "up";
929 else
930 style = "draining";
931 }
932 else {
933 style = "going_down";
934 }
935
936 if (memcmp(field_str(stats, ST_F_STATUS), "MAINT", 5) == 0)
937 chunk_appendf(out, "<tr class=\"maintain\">");
938 else
939 chunk_appendf(out,
940 "<tr class=\"%s_%s\">",
941 (stats[ST_F_BCK].u.u32) ? "backup" : "active", style);
942
943
Willy Tarreauab02b3f2019-10-09 11:11:46 +0200944 if (flags & STAT_ADMIN)
William Lallemand74c24fb2016-11-21 17:18:36 +0100945 chunk_appendf(out,
David Harrigand3db35a2016-12-30 12:12:49 +0000946 "<td><input class='%s-checkbox' type=\"checkbox\" name=\"s\" value=\"%s\"></td>",
947 field_str(stats, ST_F_PXNAME),
William Lallemand74c24fb2016-11-21 17:18:36 +0100948 field_str(stats, ST_F_SVNAME));
949
950 chunk_appendf(out,
951 "<td class=ac><a name=\"%s/%s\"></a>%s"
952 "<a class=lfsb href=\"#%s/%s\">%s</a>"
953 "",
954 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME),
Willy Tarreau708c4162019-10-09 10:19:16 +0200955 (flags & STAT_SHLGNDS) ? "<u>" : "",
William Lallemand74c24fb2016-11-21 17:18:36 +0100956 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME), field_str(stats, ST_F_SVNAME));
957
Willy Tarreau708c4162019-10-09 10:19:16 +0200958 if (flags & STAT_SHLGNDS) {
William Lallemand74c24fb2016-11-21 17:18:36 +0100959 chunk_appendf(out, "<div class=tips>");
960
Willy Tarreau90807112020-02-25 08:16:33 +0100961 if (isdigit((unsigned char)*field_str(stats, ST_F_ADDR)))
William Lallemand74c24fb2016-11-21 17:18:36 +0100962 chunk_appendf(out, "IPv4: %s, ", field_str(stats, ST_F_ADDR));
963 else if (*field_str(stats, ST_F_ADDR) == '[')
964 chunk_appendf(out, "IPv6: %s, ", field_str(stats, ST_F_ADDR));
965 else if (*field_str(stats, ST_F_ADDR))
966 chunk_appendf(out, "%s, ", field_str(stats, ST_F_ADDR));
967
968 /* id */
969 chunk_appendf(out, "id: %d", stats[ST_F_SID].u.u32);
970
971 /* cookie */
972 if (stats[ST_F_COOKIE].type) {
973 chunk_appendf(out, ", cookie: '");
974 chunk_initstr(&src, field_str(stats, ST_F_COOKIE));
975 chunk_htmlencode(out, &src);
976 chunk_appendf(out, "'");
977 }
978
979 chunk_appendf(out, "</div>");
980 }
981
982 chunk_appendf(out,
983 /* queue : current, max, limit */
984 "%s</td><td>%s</td><td>%s</td><td>%s</td>"
985 /* sessions rate : current, max, limit */
986 "<td>%s</td><td>%s</td><td></td>"
987 "",
Willy Tarreau708c4162019-10-09 10:19:16 +0200988 (flags & STAT_SHLGNDS) ? "</u>" : "",
William Lallemand74c24fb2016-11-21 17:18:36 +0100989 U2H(stats[ST_F_QCUR].u.u32), U2H(stats[ST_F_QMAX].u.u32), LIM2A(stats[ST_F_QLIMIT].u.u32, "-"),
990 U2H(stats[ST_F_RATE].u.u32), U2H(stats[ST_F_RATE_MAX].u.u32));
991
992 chunk_appendf(out,
993 /* sessions: current, max, limit, total */
Willy Tarreauf21d17b2019-09-08 09:24:56 +0200994 "<td><u>%s<div class=tips>"
995 "<table class=det>"
996 "<tr><th>Current active connections:</th><td>%s</td></tr>"
997 "<tr><th>Current idle connections:</th><td>%s</td></tr>"
998 "<tr><th>Active connections limit:</th><td>%s</td></tr>"
999 "<tr><th>Idle connections limit:</th><td>%s</td></tr>"
1000 "</table></div></u>"
1001 "</td><td>%s</td><td>%s</td>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001002 "<td><u>%s<div class=tips><table class=det>"
1003 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
1004 "",
Willy Tarreauf21d17b2019-09-08 09:24:56 +02001005 U2H(stats[ST_F_SCUR].u.u32),
1006 U2H(stats[ST_F_SCUR].u.u32),
1007 U2H(stats[ST_F_SRV_ICUR].u.u32),
1008 LIM2A(stats[ST_F_SLIM].u.u32, "-"),
1009 stats[ST_F_SRV_ILIM].type ? U2H(stats[ST_F_SRV_ILIM].u.u32) : "-",
1010 U2H(stats[ST_F_SMAX].u.u32), LIM2A(stats[ST_F_SLIM].u.u32, "-"),
William Lallemand74c24fb2016-11-21 17:18:36 +01001011 U2H(stats[ST_F_STOT].u.u64),
1012 U2H(stats[ST_F_STOT].u.u64));
1013
1014 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
1015 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001016 chunk_appendf(out,
Willy Tarreauf1573842018-12-14 11:35:36 +01001017 "<tr><th>New connections:</th><td>%s</td></tr>"
1018 "<tr><th>Reused connections:</th><td>%s</td><td>(%d%%)</td></tr>"
Marcin Deranek3c27dda2020-05-15 18:32:51 +02001019 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001020 "<tr><th>- HTTP 1xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
1021 "<tr><th>- HTTP 2xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
1022 "<tr><th>- HTTP 3xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
1023 "<tr><th>- HTTP 4xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
1024 "<tr><th>- HTTP 5xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
1025 "<tr><th>- other responses:</th><td>%s</td><td>(%d%%)</td></tr>"
Willy Tarreau1b0f85e2018-05-28 15:12:40 +02001026 "<tr><th>Failed hdr rewrites:</th><td>%s</td></tr>"
Christopher Faulet0159ee42019-12-16 14:40:39 +01001027 "<tr><th>Internal error:</th><td>%s</td></tr>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001028 "",
Willy Tarreauf1573842018-12-14 11:35:36 +01001029 U2H(stats[ST_F_CONNECT].u.u64),
1030 U2H(stats[ST_F_REUSE].u.u64),
1031 (stats[ST_F_CONNECT].u.u64 + stats[ST_F_REUSE].u.u64) ?
1032 (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 +02001033 U2H(stats[ST_F_REQ_TOT].u.u64),
1034 U2H(stats[ST_F_HRSP_1XX].u.u64), stats[ST_F_REQ_TOT].u.u64 ?
1035 (int)(100 * stats[ST_F_HRSP_1XX].u.u64 / stats[ST_F_REQ_TOT].u.u64) : 0,
1036 U2H(stats[ST_F_HRSP_2XX].u.u64), stats[ST_F_REQ_TOT].u.u64 ?
1037 (int)(100 * stats[ST_F_HRSP_2XX].u.u64 / stats[ST_F_REQ_TOT].u.u64) : 0,
1038 U2H(stats[ST_F_HRSP_3XX].u.u64), stats[ST_F_REQ_TOT].u.u64 ?
1039 (int)(100 * stats[ST_F_HRSP_3XX].u.u64 / stats[ST_F_REQ_TOT].u.u64) : 0,
1040 U2H(stats[ST_F_HRSP_4XX].u.u64), stats[ST_F_REQ_TOT].u.u64 ?
1041 (int)(100 * stats[ST_F_HRSP_4XX].u.u64 / stats[ST_F_REQ_TOT].u.u64) : 0,
1042 U2H(stats[ST_F_HRSP_5XX].u.u64), stats[ST_F_REQ_TOT].u.u64 ?
1043 (int)(100 * stats[ST_F_HRSP_5XX].u.u64 / stats[ST_F_REQ_TOT].u.u64) : 0,
1044 U2H(stats[ST_F_HRSP_OTHER].u.u64), stats[ST_F_REQ_TOT].u.u64 ?
1045 (int)(100 * stats[ST_F_HRSP_OTHER].u.u64 / stats[ST_F_REQ_TOT].u.u64) : 0,
Christopher Faulet0159ee42019-12-16 14:40:39 +01001046 U2H(stats[ST_F_WREW].u.u64),
1047 U2H(stats[ST_F_EINT].u.u64));
William Lallemand74c24fb2016-11-21 17:18:36 +01001048 }
1049
Christopher Faulet0d1c2a62019-11-08 14:59:51 +01001050 chunk_appendf(out, "<tr><th colspan=3>Max / Avg over last 1024 success. conn.</th></tr>");
1051 chunk_appendf(out, "<tr><th>- Queue time:</th><td>%s / %s</td><td>ms</td></tr>",
1052 U2H(stats[ST_F_QT_MAX].u.u32), U2H(stats[ST_F_QTIME].u.u32));
1053 chunk_appendf(out, "<tr><th>- Connect time:</th><td>%s / %s</td><td>ms</td></tr>",
1054 U2H(stats[ST_F_CT_MAX].u.u32), U2H(stats[ST_F_CTIME].u.u32));
William Lallemand74c24fb2016-11-21 17:18:36 +01001055 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
Christopher Faulet0d1c2a62019-11-08 14:59:51 +01001056 chunk_appendf(out, "<tr><th>- Responses time:</th><td>%s / %s</td><td>ms</td></tr>",
1057 U2H(stats[ST_F_RT_MAX].u.u32), U2H(stats[ST_F_RTIME].u.u32));
1058 chunk_appendf(out, "<tr><th>- Total time:</th><td>%s / %s</td><td>ms</td></tr>",
1059 U2H(stats[ST_F_TT_MAX].u.u32), U2H(stats[ST_F_TTIME].u.u32));
William Lallemand74c24fb2016-11-21 17:18:36 +01001060
1061 chunk_appendf(out,
1062 "</table></div></u></td>"
1063 /* sessions: lbtot, last */
1064 "<td>%s</td><td>%s</td>",
1065 U2H(stats[ST_F_LBTOT].u.u64),
1066 human_time(stats[ST_F_LASTSESS].u.s32, 1));
1067
1068 chunk_appendf(out,
1069 /* bytes : in, out */
1070 "<td>%s</td><td>%s</td>"
1071 /* denied: req, resp */
1072 "<td></td><td>%s</td>"
1073 /* errors : request, connect */
1074 "<td></td><td>%s</td>"
1075 /* errors : response */
1076 "<td><u>%s<div class=tips>Connection resets during transfers: %lld client, %lld server</div></u></td>"
1077 /* warnings: retries, redispatches */
1078 "<td>%lld</td><td>%lld</td>"
1079 "",
1080 U2H(stats[ST_F_BIN].u.u64), U2H(stats[ST_F_BOUT].u.u64),
1081 U2H(stats[ST_F_DRESP].u.u64),
1082 U2H(stats[ST_F_ECON].u.u64),
1083 U2H(stats[ST_F_ERESP].u.u64),
1084 (long long)stats[ST_F_CLI_ABRT].u.u64,
1085 (long long)stats[ST_F_SRV_ABRT].u.u64,
1086 (long long)stats[ST_F_WRETR].u.u64,
1087 (long long)stats[ST_F_WREDIS].u.u64);
1088
1089 /* status, last change */
1090 chunk_appendf(out, "<td class=ac>");
1091
1092 /* FIXME!!!!
1093 * LASTCHG should contain the last change for *this* server and must be computed
1094 * properly above, as was done below, ie: this server if maint, otherwise ref server
1095 * if tracking. Note that ref is either local or remote depending on tracking.
1096 */
1097
1098
1099 if (memcmp(field_str(stats, ST_F_STATUS), "MAINT", 5) == 0) {
1100 chunk_appendf(out, "%s MAINT", human_time(stats[ST_F_LASTCHG].u.u32, 1));
1101 }
1102 else if (memcmp(field_str(stats, ST_F_STATUS), "no check", 5) == 0) {
1103 chunk_strcat(out, "<i>no check</i>");
1104 }
1105 else {
1106 chunk_appendf(out, "%s %s", human_time(stats[ST_F_LASTCHG].u.u32, 1), field_str(stats, ST_F_STATUS));
1107 if (memcmp(field_str(stats, ST_F_STATUS), "DOWN", 4) == 0) {
1108 if (stats[ST_F_CHECK_HEALTH].u.u32)
1109 chunk_strcat(out, " &uarr;");
1110 }
1111 else if (stats[ST_F_CHECK_HEALTH].u.u32 < stats[ST_F_CHECK_RISE].u.u32 + stats[ST_F_CHECK_FALL].u.u32 - 1)
1112 chunk_strcat(out, " &darr;");
1113 }
1114
1115 if (memcmp(field_str(stats, ST_F_STATUS), "DOWN", 4) == 0 &&
1116 stats[ST_F_AGENT_STATUS].type && !stats[ST_F_AGENT_HEALTH].u.u32) {
1117 chunk_appendf(out,
1118 "</td><td class=ac><u> %s",
1119 field_str(stats, ST_F_AGENT_STATUS));
1120
1121 if (stats[ST_F_AGENT_CODE].type)
1122 chunk_appendf(out, "/%d", stats[ST_F_AGENT_CODE].u.u32);
1123
1124 if (stats[ST_F_AGENT_DURATION].type)
1125 chunk_appendf(out, " in %lums", (long)stats[ST_F_AGENT_DURATION].u.u64);
1126
1127 chunk_appendf(out, "<div class=tips>%s", field_str(stats, ST_F_AGENT_DESC));
1128
1129 if (*field_str(stats, ST_F_LAST_AGT)) {
1130 chunk_appendf(out, ": ");
1131 chunk_initstr(&src, field_str(stats, ST_F_LAST_AGT));
1132 chunk_htmlencode(out, &src);
1133 }
1134 chunk_appendf(out, "</div></u>");
1135 }
1136 else if (stats[ST_F_CHECK_STATUS].type) {
1137 chunk_appendf(out,
1138 "</td><td class=ac><u> %s",
1139 field_str(stats, ST_F_CHECK_STATUS));
1140
1141 if (stats[ST_F_CHECK_CODE].type)
1142 chunk_appendf(out, "/%d", stats[ST_F_CHECK_CODE].u.u32);
1143
1144 if (stats[ST_F_CHECK_DURATION].type)
1145 chunk_appendf(out, " in %lums", (long)stats[ST_F_CHECK_DURATION].u.u64);
1146
1147 chunk_appendf(out, "<div class=tips>%s", field_str(stats, ST_F_CHECK_DESC));
1148
1149 if (*field_str(stats, ST_F_LAST_CHK)) {
1150 chunk_appendf(out, ": ");
1151 chunk_initstr(&src, field_str(stats, ST_F_LAST_CHK));
1152 chunk_htmlencode(out, &src);
1153 }
1154 chunk_appendf(out, "</div></u>");
1155 }
1156 else
1157 chunk_appendf(out, "</td><td>");
1158
1159 chunk_appendf(out,
1160 /* weight */
1161 "</td><td class=ac>%d</td>"
1162 /* act, bck */
1163 "<td class=ac>%s</td><td class=ac>%s</td>"
1164 "",
1165 stats[ST_F_WEIGHT].u.u32,
1166 stats[ST_F_BCK].u.u32 ? "-" : "Y",
1167 stats[ST_F_BCK].u.u32 ? "Y" : "-");
1168
1169 /* check failures: unique, fatal, down time */
1170 if (strcmp(field_str(stats, ST_F_STATUS), "MAINT (resolution)") == 0) {
1171 chunk_appendf(out, "<td class=ac colspan=3>resolution</td>");
1172 }
1173 else if (stats[ST_F_CHKFAIL].type) {
1174 chunk_appendf(out, "<td><u>%lld", (long long)stats[ST_F_CHKFAIL].u.u64);
1175
1176 if (stats[ST_F_HANAFAIL].type)
1177 chunk_appendf(out, "/%lld", (long long)stats[ST_F_HANAFAIL].u.u64);
1178
1179 chunk_appendf(out,
1180 "<div class=tips>Failed Health Checks%s</div></u></td>"
1181 "<td>%lld</td><td>%s</td>"
1182 "",
1183 stats[ST_F_HANAFAIL].type ? "/Health Analyses" : "",
1184 (long long)stats[ST_F_CHKDOWN].u.u64, human_time(stats[ST_F_DOWNTIME].u.u32, 1));
1185 }
1186 else if (strcmp(field_str(stats, ST_F_STATUS), "MAINT") != 0 && field_format(stats, ST_F_TRACKED) == FF_STR) {
1187 /* tracking a server (hence inherited maint would appear as "MAINT (via...)" */
1188 chunk_appendf(out,
1189 "<td class=ac colspan=3><a class=lfsb href=\"#%s\">via %s</a></td>",
1190 field_str(stats, ST_F_TRACKED), field_str(stats, ST_F_TRACKED));
1191 }
1192 else
1193 chunk_appendf(out, "<td colspan=3></td>");
1194
1195 /* throttle */
1196 if (stats[ST_F_THROTTLE].type)
1197 chunk_appendf(out, "<td class=ac>%d %%</td></tr>\n", stats[ST_F_THROTTLE].u.u32);
1198 else
1199 chunk_appendf(out, "<td class=ac>-</td></tr>\n");
1200 }
1201 else if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_BE) {
1202 chunk_appendf(out, "<tr class=\"backend\">");
Willy Tarreauab02b3f2019-10-09 11:11:46 +02001203 if (flags & STAT_ADMIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001204 /* Column sub-heading for Enable or Disable server */
1205 chunk_appendf(out, "<td></td>");
1206 }
1207 chunk_appendf(out,
1208 "<td class=ac>"
1209 /* name */
1210 "%s<a name=\"%s/Backend\"></a>"
1211 "<a class=lfsb href=\"#%s/Backend\">Backend</a>"
1212 "",
Willy Tarreau708c4162019-10-09 10:19:16 +02001213 (flags & STAT_SHLGNDS)?"<u>":"",
William Lallemand74c24fb2016-11-21 17:18:36 +01001214 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_PXNAME));
1215
Willy Tarreau708c4162019-10-09 10:19:16 +02001216 if (flags & STAT_SHLGNDS) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001217 /* balancing */
1218 chunk_appendf(out, "<div class=tips>balancing: %s",
1219 field_str(stats, ST_F_ALGO));
1220
1221 /* cookie */
1222 if (stats[ST_F_COOKIE].type) {
1223 chunk_appendf(out, ", cookie: '");
1224 chunk_initstr(&src, field_str(stats, ST_F_COOKIE));
1225 chunk_htmlencode(out, &src);
1226 chunk_appendf(out, "'");
1227 }
1228 chunk_appendf(out, "</div>");
1229 }
1230
1231 chunk_appendf(out,
1232 "%s</td>"
1233 /* queue : current, max */
1234 "<td>%s</td><td>%s</td><td></td>"
1235 /* sessions rate : current, max, limit */
1236 "<td>%s</td><td>%s</td><td></td>"
1237 "",
Willy Tarreau708c4162019-10-09 10:19:16 +02001238 (flags & STAT_SHLGNDS)?"</u>":"",
William Lallemand74c24fb2016-11-21 17:18:36 +01001239 U2H(stats[ST_F_QCUR].u.u32), U2H(stats[ST_F_QMAX].u.u32),
1240 U2H(stats[ST_F_RATE].u.u32), U2H(stats[ST_F_RATE_MAX].u.u32));
1241
1242 chunk_appendf(out,
1243 /* sessions: current, max, limit, total */
1244 "<td>%s</td><td>%s</td><td>%s</td>"
1245 "<td><u>%s<div class=tips><table class=det>"
1246 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
1247 "",
Willy Tarreau8e0f1752016-12-12 15:07:29 +01001248 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 +01001249 U2H(stats[ST_F_STOT].u.u64),
1250 U2H(stats[ST_F_STOT].u.u64));
1251
1252 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
1253 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0) {
1254 chunk_appendf(out,
Willy Tarreauf1573842018-12-14 11:35:36 +01001255 "<tr><th>New connections:</th><td>%s</td></tr>"
1256 "<tr><th>Reused connections:</th><td>%s</td><td>(%d%%)</td></tr>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001257 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
1258 "<tr><th>- HTTP 1xx responses:</th><td>%s</td></tr>"
1259 "<tr><th>- HTTP 2xx responses:</th><td>%s</td></tr>"
1260 "<tr><th>&nbsp;&nbsp;Compressed 2xx:</th><td>%s</td><td>(%d%%)</td></tr>"
1261 "<tr><th>- HTTP 3xx responses:</th><td>%s</td></tr>"
1262 "<tr><th>- HTTP 4xx responses:</th><td>%s</td></tr>"
1263 "<tr><th>- HTTP 5xx responses:</th><td>%s</td></tr>"
1264 "<tr><th>- other responses:</th><td>%s</td></tr>"
Willy Tarreaua1214a52018-12-14 14:00:25 +01001265 "<tr><th>Cache lookups:</th><td>%s</td></tr>"
1266 "<tr><th>Cache hits:</th><td>%s</td><td>(%d%%)</td></tr>"
Willy Tarreau1b0f85e2018-05-28 15:12:40 +02001267 "<tr><th>Failed hdr rewrites:</th><td>%s</td></tr>"
Christopher Faulet0159ee42019-12-16 14:40:39 +01001268 "<tr><th>Internal errors:</th><td>%s</td></tr>"
Christopher Faulet0d1c2a62019-11-08 14:59:51 +01001269 "",
Willy Tarreauf1573842018-12-14 11:35:36 +01001270 U2H(stats[ST_F_CONNECT].u.u64),
1271 U2H(stats[ST_F_REUSE].u.u64),
1272 (stats[ST_F_CONNECT].u.u64 + stats[ST_F_REUSE].u.u64) ?
1273 (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 +01001274 U2H(stats[ST_F_REQ_TOT].u.u64),
1275 U2H(stats[ST_F_HRSP_1XX].u.u64),
1276 U2H(stats[ST_F_HRSP_2XX].u.u64),
1277 U2H(stats[ST_F_COMP_RSP].u.u64),
1278 stats[ST_F_HRSP_2XX].u.u64 ?
1279 (int)(100 * stats[ST_F_COMP_RSP].u.u64 / stats[ST_F_HRSP_2XX].u.u64) : 0,
1280 U2H(stats[ST_F_HRSP_3XX].u.u64),
1281 U2H(stats[ST_F_HRSP_4XX].u.u64),
1282 U2H(stats[ST_F_HRSP_5XX].u.u64),
Willy Tarreaufeead3a2018-12-14 13:48:44 +01001283 U2H(stats[ST_F_HRSP_OTHER].u.u64),
Willy Tarreaua1214a52018-12-14 14:00:25 +01001284 U2H(stats[ST_F_CACHE_LOOKUPS].u.u64),
1285 U2H(stats[ST_F_CACHE_HITS].u.u64),
1286 stats[ST_F_CACHE_LOOKUPS].u.u64 ?
1287 (int)(100 * stats[ST_F_CACHE_HITS].u.u64 / stats[ST_F_CACHE_LOOKUPS].u.u64) : 0,
Christopher Faulet0159ee42019-12-16 14:40:39 +01001288 U2H(stats[ST_F_WREW].u.u64),
1289 U2H(stats[ST_F_EINT].u.u64));
William Lallemand74c24fb2016-11-21 17:18:36 +01001290 }
1291
Christopher Faulet0d1c2a62019-11-08 14:59:51 +01001292 chunk_appendf(out, "<tr><th colspan=3>Max / Avg over last 1024 success. conn.</th></tr>");
1293 chunk_appendf(out, "<tr><th>- Queue time:</th><td>%s / %s</td><td>ms</td></tr>",
1294 U2H(stats[ST_F_QT_MAX].u.u32), U2H(stats[ST_F_QTIME].u.u32));
1295 chunk_appendf(out, "<tr><th>- Connect time:</th><td>%s / %s</td><td>ms</td></tr>",
1296 U2H(stats[ST_F_CT_MAX].u.u32), U2H(stats[ST_F_CTIME].u.u32));
William Lallemand74c24fb2016-11-21 17:18:36 +01001297 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
Christopher Faulet0d1c2a62019-11-08 14:59:51 +01001298 chunk_appendf(out, "<tr><th>- Responses time:</th><td>%s / %s</td><td>ms</td></tr>",
1299 U2H(stats[ST_F_RT_MAX].u.u32), U2H(stats[ST_F_RTIME].u.u32));
1300 chunk_appendf(out, "<tr><th>- Total time:</th><td>%s / %s</td><td>ms</td></tr>",
1301 U2H(stats[ST_F_TT_MAX].u.u32), U2H(stats[ST_F_TTIME].u.u32));
William Lallemand74c24fb2016-11-21 17:18:36 +01001302
1303 chunk_appendf(out,
1304 "</table></div></u></td>"
1305 /* sessions: lbtot, last */
1306 "<td>%s</td><td>%s</td>"
1307 /* bytes: in */
1308 "<td>%s</td>"
1309 "",
1310 U2H(stats[ST_F_LBTOT].u.u64),
1311 human_time(stats[ST_F_LASTSESS].u.s32, 1),
1312 U2H(stats[ST_F_BIN].u.u64));
1313
1314 chunk_appendf(out,
1315 /* bytes:out + compression stats (via hover): comp_in, comp_out, comp_byp */
1316 "<td>%s%s<div class=tips><table class=det>"
1317 "<tr><th>Response bytes in:</th><td>%s</td></tr>"
1318 "<tr><th>Compression in:</th><td>%s</td></tr>"
1319 "<tr><th>Compression out:</th><td>%s</td><td>(%d%%)</td></tr>"
1320 "<tr><th>Compression bypass:</th><td>%s</td></tr>"
1321 "<tr><th>Total bytes saved:</th><td>%s</td><td>(%d%%)</td></tr>"
1322 "</table></div>%s</td>",
1323 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "<u>":"",
1324 U2H(stats[ST_F_BOUT].u.u64),
1325 U2H(stats[ST_F_BOUT].u.u64),
1326 U2H(stats[ST_F_COMP_IN].u.u64),
1327 U2H(stats[ST_F_COMP_OUT].u.u64),
1328 stats[ST_F_COMP_IN].u.u64 ? (int)(stats[ST_F_COMP_OUT].u.u64 * 100 / stats[ST_F_COMP_IN].u.u64) : 0,
1329 U2H(stats[ST_F_COMP_BYP].u.u64),
1330 U2H(stats[ST_F_COMP_IN].u.u64 - stats[ST_F_COMP_OUT].u.u64),
1331 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,
1332 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "</u>":"");
1333
1334 chunk_appendf(out,
1335 /* denied: req, resp */
1336 "<td>%s</td><td>%s</td>"
1337 /* errors : request, connect */
1338 "<td></td><td>%s</td>"
1339 /* errors : response */
1340 "<td><u>%s<div class=tips>Connection resets during transfers: %lld client, %lld server</div></u></td>"
1341 /* warnings: retries, redispatches */
1342 "<td>%lld</td><td>%lld</td>"
1343 /* backend status: reflect backend status (up/down): we display UP
1344 * if the backend has known working servers or if it has no server at
1345 * all (eg: for stats). Then we display the total weight, number of
1346 * active and backups. */
1347 "<td class=ac>%s %s</td><td class=ac>&nbsp;</td><td class=ac>%d</td>"
1348 "<td class=ac>%d</td><td class=ac>%d</td>"
1349 "",
1350 U2H(stats[ST_F_DREQ].u.u64), U2H(stats[ST_F_DRESP].u.u64),
1351 U2H(stats[ST_F_ECON].u.u64),
1352 U2H(stats[ST_F_ERESP].u.u64),
1353 (long long)stats[ST_F_CLI_ABRT].u.u64,
1354 (long long)stats[ST_F_SRV_ABRT].u.u64,
1355 (long long)stats[ST_F_WRETR].u.u64, (long long)stats[ST_F_WREDIS].u.u64,
1356 human_time(stats[ST_F_LASTCHG].u.u32, 1),
1357 strcmp(field_str(stats, ST_F_STATUS), "DOWN") ? field_str(stats, ST_F_STATUS) : "<font color=\"red\"><b>DOWN</b></font>",
1358 stats[ST_F_WEIGHT].u.u32,
1359 stats[ST_F_ACT].u.u32, stats[ST_F_BCK].u.u32);
1360
1361 chunk_appendf(out,
1362 /* rest of backend: nothing, down transitions, total downtime, throttle */
1363 "<td class=ac>&nbsp;</td><td>%d</td>"
1364 "<td>%s</td>"
1365 "<td></td>"
1366 "</tr>",
1367 stats[ST_F_CHKDOWN].u.u32,
1368 stats[ST_F_DOWNTIME].type ? human_time(stats[ST_F_DOWNTIME].u.u32, 1) : "&nbsp;");
1369 }
1370 return 1;
1371}
1372
Willy Tarreau43241ff2019-10-09 11:27:51 +02001373static int stats_dump_one_line(const struct field *stats, struct proxy *px, struct appctx *appctx)
William Lallemand74c24fb2016-11-21 17:18:36 +01001374{
Simon Horman05ee2132017-01-04 09:37:25 +01001375 int ret;
1376
William Lallemand74c24fb2016-11-21 17:18:36 +01001377 if (appctx->ctx.stats.flags & STAT_FMT_HTML)
Willy Tarreau43241ff2019-10-09 11:27:51 +02001378 ret = stats_dump_fields_html(&trash, stats, appctx->ctx.stats.flags);
William Lallemand74c24fb2016-11-21 17:18:36 +01001379 else if (appctx->ctx.stats.flags & STAT_FMT_TYPED)
Willy Tarreau43241ff2019-10-09 11:27:51 +02001380 ret = stats_dump_fields_typed(&trash, stats, appctx->ctx.stats.flags);
Simon Horman05ee2132017-01-04 09:37:25 +01001381 else if (appctx->ctx.stats.flags & STAT_FMT_JSON)
Willy Tarreaub0ce3ad2019-10-09 11:19:29 +02001382 ret = stats_dump_fields_json(&trash, stats, appctx->ctx.stats.flags);
William Lallemand74c24fb2016-11-21 17:18:36 +01001383 else
Willy Tarreau43241ff2019-10-09 11:27:51 +02001384 ret = stats_dump_fields_csv(&trash, stats, appctx->ctx.stats.flags);
Simon Horman05ee2132017-01-04 09:37:25 +01001385
1386 if (ret)
1387 appctx->ctx.stats.flags |= STAT_STARTED;
1388
1389 return ret;
William Lallemand74c24fb2016-11-21 17:18:36 +01001390}
1391
1392/* Fill <stats> with the frontend statistics. <stats> is
1393 * preallocated array of length <len>. The length of the array
1394 * must be at least ST_F_TOTAL_FIELDS. If this length is less then
1395 * this value, the function returns 0, otherwise, it returns 1.
1396 */
1397int stats_fill_fe_stats(struct proxy *px, struct field *stats, int len)
1398{
1399 if (len < ST_F_TOTAL_FIELDS)
1400 return 0;
1401
1402 memset(stats, 0, sizeof(*stats) * len);
1403
1404 stats[ST_F_PXNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
1405 stats[ST_F_SVNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, "FRONTEND");
1406 stats[ST_F_MODE] = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
1407 stats[ST_F_SCUR] = mkf_u32(0, px->feconn);
1408 stats[ST_F_SMAX] = mkf_u32(FN_MAX, px->fe_counters.conn_max);
1409 stats[ST_F_SLIM] = mkf_u32(FO_CONFIG|FN_LIMIT, px->maxconn);
1410 stats[ST_F_STOT] = mkf_u64(FN_COUNTER, px->fe_counters.cum_sess);
1411 stats[ST_F_BIN] = mkf_u64(FN_COUNTER, px->fe_counters.bytes_in);
1412 stats[ST_F_BOUT] = mkf_u64(FN_COUNTER, px->fe_counters.bytes_out);
1413 stats[ST_F_DREQ] = mkf_u64(FN_COUNTER, px->fe_counters.denied_req);
1414 stats[ST_F_DRESP] = mkf_u64(FN_COUNTER, px->fe_counters.denied_resp);
1415 stats[ST_F_EREQ] = mkf_u64(FN_COUNTER, px->fe_counters.failed_req);
1416 stats[ST_F_DCON] = mkf_u64(FN_COUNTER, px->fe_counters.denied_conn);
1417 stats[ST_F_DSES] = mkf_u64(FN_COUNTER, px->fe_counters.denied_sess);
1418 stats[ST_F_STATUS] = mkf_str(FO_STATUS, px->state == PR_STREADY ? "OPEN" : px->state == PR_STFULL ? "FULL" : "STOP");
1419 stats[ST_F_PID] = mkf_u32(FO_KEY, relative_pid);
1420 stats[ST_F_IID] = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
1421 stats[ST_F_SID] = mkf_u32(FO_KEY|FS_SERVICE, 0);
1422 stats[ST_F_TYPE] = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_FE);
1423 stats[ST_F_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&px->fe_sess_per_sec));
1424 stats[ST_F_RATE_LIM] = mkf_u32(FO_CONFIG|FN_LIMIT, px->fe_sps_lim);
1425 stats[ST_F_RATE_MAX] = mkf_u32(FN_MAX, px->fe_counters.sps_max);
Tim Duesterhus3fd19732018-05-27 20:35:08 +02001426 stats[ST_F_WREW] = mkf_u64(FN_COUNTER, px->fe_counters.failed_rewrites);
Christopher Faulet0159ee42019-12-16 14:40:39 +01001427 stats[ST_F_EINT] = mkf_u64(FN_COUNTER, px->fe_counters.internal_errors);
William Lallemand74c24fb2016-11-21 17:18:36 +01001428
1429 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
1430 if (px->mode == PR_MODE_HTTP) {
1431 stats[ST_F_HRSP_1XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[1]);
1432 stats[ST_F_HRSP_2XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[2]);
1433 stats[ST_F_HRSP_3XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[3]);
1434 stats[ST_F_HRSP_4XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[4]);
1435 stats[ST_F_HRSP_5XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[5]);
1436 stats[ST_F_HRSP_OTHER] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[0]);
1437 stats[ST_F_INTERCEPTED] = mkf_u64(FN_COUNTER, px->fe_counters.intercepted_req);
Willy Tarreaua1214a52018-12-14 14:00:25 +01001438 stats[ST_F_CACHE_LOOKUPS] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.cache_lookups);
1439 stats[ST_F_CACHE_HITS] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.cache_hits);
William Lallemand74c24fb2016-11-21 17:18:36 +01001440 }
1441
1442 /* requests : req_rate, req_rate_max, req_tot, */
1443 stats[ST_F_REQ_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&px->fe_req_per_sec));
1444 stats[ST_F_REQ_RATE_MAX] = mkf_u32(FN_MAX, px->fe_counters.p.http.rps_max);
1445 stats[ST_F_REQ_TOT] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.cum_req);
1446
1447 /* compression: in, out, bypassed, responses */
1448 stats[ST_F_COMP_IN] = mkf_u64(FN_COUNTER, px->fe_counters.comp_in);
1449 stats[ST_F_COMP_OUT] = mkf_u64(FN_COUNTER, px->fe_counters.comp_out);
1450 stats[ST_F_COMP_BYP] = mkf_u64(FN_COUNTER, px->fe_counters.comp_byp);
1451 stats[ST_F_COMP_RSP] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.comp_rsp);
1452
1453 /* connections : conn_rate, conn_rate_max, conn_tot, conn_max */
1454 stats[ST_F_CONN_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&px->fe_conn_per_sec));
1455 stats[ST_F_CONN_RATE_MAX] = mkf_u32(FN_MAX, px->fe_counters.cps_max);
1456 stats[ST_F_CONN_TOT] = mkf_u64(FN_COUNTER, px->fe_counters.cum_conn);
1457
1458 return 1;
1459}
1460
1461/* Dumps a frontend's line to the trash for the current proxy <px> and uses
1462 * the state from stream interface <si>. The caller is responsible for clearing
1463 * the trash if needed. Returns non-zero if it emits anything, zero otherwise.
1464 */
1465static int stats_dump_fe_stats(struct stream_interface *si, struct proxy *px)
1466{
1467 struct appctx *appctx = __objt_appctx(si->end);
1468
1469 if (!(px->cap & PR_CAP_FE))
1470 return 0;
1471
1472 if ((appctx->ctx.stats.flags & STAT_BOUND) && !(appctx->ctx.stats.type & (1 << STATS_TYPE_FE)))
1473 return 0;
1474
1475 if (!stats_fill_fe_stats(px, stats, ST_F_TOTAL_FIELDS))
1476 return 0;
1477
Willy Tarreau43241ff2019-10-09 11:27:51 +02001478 return stats_dump_one_line(stats, px, appctx);
William Lallemand74c24fb2016-11-21 17:18:36 +01001479}
1480
1481/* Fill <stats> with the listener statistics. <stats> is
1482 * preallocated array of length <len>. The length of the array
1483 * must be at least ST_F_TOTAL_FIELDS. If this length is less
1484 * then this value, the function returns 0, otherwise, it
Willy Tarreau708c4162019-10-09 10:19:16 +02001485 * returns 1. <flags> can take the value STAT_SHLGNDS.
William Lallemand74c24fb2016-11-21 17:18:36 +01001486 */
1487int stats_fill_li_stats(struct proxy *px, struct listener *l, int flags,
1488 struct field *stats, int len)
1489{
Willy Tarreau83061a82018-07-13 11:56:34 +02001490 struct buffer *out = get_trash_chunk();
William Lallemand74c24fb2016-11-21 17:18:36 +01001491
1492 if (len < ST_F_TOTAL_FIELDS)
1493 return 0;
1494
1495 if (!l->counters)
1496 return 0;
1497
1498 chunk_reset(out);
1499 memset(stats, 0, sizeof(*stats) * len);
1500
1501 stats[ST_F_PXNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
1502 stats[ST_F_SVNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, l->name);
1503 stats[ST_F_MODE] = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
1504 stats[ST_F_SCUR] = mkf_u32(0, l->nbconn);
1505 stats[ST_F_SMAX] = mkf_u32(FN_MAX, l->counters->conn_max);
1506 stats[ST_F_SLIM] = mkf_u32(FO_CONFIG|FN_LIMIT, l->maxconn);
1507 stats[ST_F_STOT] = mkf_u64(FN_COUNTER, l->counters->cum_conn);
1508 stats[ST_F_BIN] = mkf_u64(FN_COUNTER, l->counters->bytes_in);
1509 stats[ST_F_BOUT] = mkf_u64(FN_COUNTER, l->counters->bytes_out);
1510 stats[ST_F_DREQ] = mkf_u64(FN_COUNTER, l->counters->denied_req);
1511 stats[ST_F_DRESP] = mkf_u64(FN_COUNTER, l->counters->denied_resp);
1512 stats[ST_F_EREQ] = mkf_u64(FN_COUNTER, l->counters->failed_req);
1513 stats[ST_F_DCON] = mkf_u64(FN_COUNTER, l->counters->denied_conn);
1514 stats[ST_F_DSES] = mkf_u64(FN_COUNTER, l->counters->denied_sess);
Willy Tarreaua8cf66b2019-02-27 16:49:00 +01001515 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 +01001516 stats[ST_F_PID] = mkf_u32(FO_KEY, relative_pid);
1517 stats[ST_F_IID] = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
1518 stats[ST_F_SID] = mkf_u32(FO_KEY|FS_SERVICE, l->luid);
1519 stats[ST_F_TYPE] = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_SO);
Tim Duesterhus3fd19732018-05-27 20:35:08 +02001520 stats[ST_F_WREW] = mkf_u64(FN_COUNTER, l->counters->failed_rewrites);
Christopher Faulet0159ee42019-12-16 14:40:39 +01001521 stats[ST_F_EINT] = mkf_u64(FN_COUNTER, l->counters->internal_errors);
William Lallemand74c24fb2016-11-21 17:18:36 +01001522
Willy Tarreau708c4162019-10-09 10:19:16 +02001523 if (flags & STAT_SHLGNDS) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001524 char str[INET6_ADDRSTRLEN];
1525 int port;
1526
1527 port = get_host_port(&l->addr);
1528 switch (addr_to_str(&l->addr, str, sizeof(str))) {
1529 case AF_INET:
1530 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
1531 chunk_appendf(out, "%s:%d", str, port);
1532 break;
1533 case AF_INET6:
1534 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
1535 chunk_appendf(out, "[%s]:%d", str, port);
1536 break;
1537 case AF_UNIX:
1538 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, "unix");
1539 break;
1540 case -1:
1541 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
1542 chunk_strcat(out, strerror(errno));
1543 break;
1544 default: /* address family not supported */
1545 break;
1546 }
1547 }
1548
1549 return 1;
1550}
1551
1552/* Dumps a line for listener <l> and proxy <px> to the trash and uses the state
Willy Tarreau43241ff2019-10-09 11:27:51 +02001553 * from stream interface <si>. The caller is responsible for clearing the trash
1554 * if needed. Returns non-zero if it emits anything, zero otherwise.
William Lallemand74c24fb2016-11-21 17:18:36 +01001555 */
Willy Tarreau43241ff2019-10-09 11:27:51 +02001556static int stats_dump_li_stats(struct stream_interface *si, struct proxy *px, struct listener *l)
William Lallemand74c24fb2016-11-21 17:18:36 +01001557{
1558 struct appctx *appctx = __objt_appctx(si->end);
1559
Willy Tarreau43241ff2019-10-09 11:27:51 +02001560 if (!stats_fill_li_stats(px, l, appctx->ctx.stats.flags, stats, ST_F_TOTAL_FIELDS))
William Lallemand74c24fb2016-11-21 17:18:36 +01001561 return 0;
1562
Willy Tarreau43241ff2019-10-09 11:27:51 +02001563 return stats_dump_one_line(stats, px, appctx);
William Lallemand74c24fb2016-11-21 17:18:36 +01001564}
1565
1566enum srv_stats_state {
1567 SRV_STATS_STATE_DOWN = 0,
1568 SRV_STATS_STATE_DOWN_AGENT,
1569 SRV_STATS_STATE_GOING_UP,
1570 SRV_STATS_STATE_UP_GOING_DOWN,
1571 SRV_STATS_STATE_UP,
1572 SRV_STATS_STATE_NOLB_GOING_DOWN,
1573 SRV_STATS_STATE_NOLB,
1574 SRV_STATS_STATE_DRAIN_GOING_DOWN,
1575 SRV_STATS_STATE_DRAIN,
1576 SRV_STATS_STATE_DRAIN_AGENT,
1577 SRV_STATS_STATE_NO_CHECK,
1578
1579 SRV_STATS_STATE_COUNT, /* Must be last */
1580};
1581
1582static const char *srv_hlt_st[SRV_STATS_STATE_COUNT] = {
1583 [SRV_STATS_STATE_DOWN] = "DOWN",
1584 [SRV_STATS_STATE_DOWN_AGENT] = "DOWN (agent)",
1585 [SRV_STATS_STATE_GOING_UP] = "DOWN %d/%d",
1586 [SRV_STATS_STATE_UP_GOING_DOWN] = "UP %d/%d",
1587 [SRV_STATS_STATE_UP] = "UP",
1588 [SRV_STATS_STATE_NOLB_GOING_DOWN] = "NOLB %d/%d",
1589 [SRV_STATS_STATE_NOLB] = "NOLB",
1590 [SRV_STATS_STATE_DRAIN_GOING_DOWN] = "DRAIN %d/%d",
1591 [SRV_STATS_STATE_DRAIN] = "DRAIN",
1592 [SRV_STATS_STATE_DRAIN_AGENT] = "DRAIN (agent)",
1593 [SRV_STATS_STATE_NO_CHECK] = "no check"
1594};
1595
1596/* Fill <stats> with the server statistics. <stats> is
1597 * preallocated array of length <len>. The length of the array
1598 * must be at least ST_F_TOTAL_FIELDS. If this length is less
1599 * then this value, the function returns 0, otherwise, it
Willy Tarreau708c4162019-10-09 10:19:16 +02001600 * returns 1. <flags> can take the value STAT_SHLGNDS.
William Lallemand74c24fb2016-11-21 17:18:36 +01001601 */
1602int stats_fill_sv_stats(struct proxy *px, struct server *sv, int flags,
1603 struct field *stats, int len)
1604{
1605 struct server *via, *ref;
1606 char str[INET6_ADDRSTRLEN];
Willy Tarreau83061a82018-07-13 11:56:34 +02001607 struct buffer *out = get_trash_chunk();
William Lallemand74c24fb2016-11-21 17:18:36 +01001608 enum srv_stats_state state;
1609 char *fld_status;
Marcin Deraneka8dbdf32020-05-15 20:02:40 +02001610 long long srv_samples_counter;
1611 unsigned int srv_samples_window = TIME_STATS_SAMPLES;
William Lallemand74c24fb2016-11-21 17:18:36 +01001612
1613 if (len < ST_F_TOTAL_FIELDS)
1614 return 0;
1615
1616 memset(stats, 0, sizeof(*stats) * len);
1617
1618 /* we have "via" which is the tracked server as described in the configuration,
1619 * and "ref" which is the checked server and the end of the chain.
1620 */
1621 via = sv->track ? sv->track : sv;
1622 ref = via;
1623 while (ref->track)
1624 ref = ref->track;
1625
Emeric Brun52a91d32017-08-31 14:41:55 +02001626 if (sv->cur_state == SRV_ST_RUNNING || sv->cur_state == SRV_ST_STARTING) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001627 if ((ref->check.state & CHK_ST_ENABLED) &&
1628 (ref->check.health < ref->check.rise + ref->check.fall - 1)) {
1629 state = SRV_STATS_STATE_UP_GOING_DOWN;
1630 } else {
1631 state = SRV_STATS_STATE_UP;
1632 }
1633
Emeric Brun52a91d32017-08-31 14:41:55 +02001634 if (sv->cur_admin & SRV_ADMF_DRAIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001635 if (ref->agent.state & CHK_ST_ENABLED)
1636 state = SRV_STATS_STATE_DRAIN_AGENT;
1637 else if (state == SRV_STATS_STATE_UP_GOING_DOWN)
1638 state = SRV_STATS_STATE_DRAIN_GOING_DOWN;
1639 else
1640 state = SRV_STATS_STATE_DRAIN;
1641 }
1642
1643 if (state == SRV_STATS_STATE_UP && !(ref->check.state & CHK_ST_ENABLED)) {
1644 state = SRV_STATS_STATE_NO_CHECK;
1645 }
1646 }
Emeric Brun52a91d32017-08-31 14:41:55 +02001647 else if (sv->cur_state == SRV_ST_STOPPING) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001648 if ((!(sv->check.state & CHK_ST_ENABLED) && !sv->track) ||
1649 (ref->check.health == ref->check.rise + ref->check.fall - 1)) {
1650 state = SRV_STATS_STATE_NOLB;
1651 } else {
1652 state = SRV_STATS_STATE_NOLB_GOING_DOWN;
1653 }
1654 }
1655 else { /* stopped */
1656 if ((ref->agent.state & CHK_ST_ENABLED) && !ref->agent.health) {
1657 state = SRV_STATS_STATE_DOWN_AGENT;
1658 } else if ((ref->check.state & CHK_ST_ENABLED) && !ref->check.health) {
1659 state = SRV_STATS_STATE_DOWN; /* DOWN */
1660 } else if ((ref->agent.state & CHK_ST_ENABLED) || (ref->check.state & CHK_ST_ENABLED)) {
1661 state = SRV_STATS_STATE_GOING_UP;
1662 } else {
1663 state = SRV_STATS_STATE_DOWN; /* DOWN, unchecked */
1664 }
1665 }
1666
1667 chunk_reset(out);
1668
1669 stats[ST_F_PXNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
1670 stats[ST_F_SVNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, sv->id);
1671 stats[ST_F_MODE] = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
1672 stats[ST_F_QCUR] = mkf_u32(0, sv->nbpend);
1673 stats[ST_F_QMAX] = mkf_u32(FN_MAX, sv->counters.nbpend_max);
1674 stats[ST_F_SCUR] = mkf_u32(0, sv->cur_sess);
1675 stats[ST_F_SMAX] = mkf_u32(FN_MAX, sv->counters.cur_sess_max);
1676
1677 if (sv->maxconn)
1678 stats[ST_F_SLIM] = mkf_u32(FO_CONFIG|FN_LIMIT, sv->maxconn);
1679
Willy Tarreauf21d17b2019-09-08 09:24:56 +02001680 stats[ST_F_SRV_ICUR] = mkf_u32(0, sv->curr_idle_conns);
1681 if (sv->max_idle_conns != -1)
1682 stats[ST_F_SRV_ILIM] = mkf_u32(FO_CONFIG|FN_LIMIT, sv->max_idle_conns);
1683
William Lallemand74c24fb2016-11-21 17:18:36 +01001684 stats[ST_F_STOT] = mkf_u64(FN_COUNTER, sv->counters.cum_sess);
1685 stats[ST_F_BIN] = mkf_u64(FN_COUNTER, sv->counters.bytes_in);
1686 stats[ST_F_BOUT] = mkf_u64(FN_COUNTER, sv->counters.bytes_out);
Christopher Fauleta08546b2019-12-16 16:07:34 +01001687 stats[ST_F_DRESP] = mkf_u64(FN_COUNTER, sv->counters.denied_resp);
William Lallemand74c24fb2016-11-21 17:18:36 +01001688 stats[ST_F_ECON] = mkf_u64(FN_COUNTER, sv->counters.failed_conns);
1689 stats[ST_F_ERESP] = mkf_u64(FN_COUNTER, sv->counters.failed_resp);
1690 stats[ST_F_WRETR] = mkf_u64(FN_COUNTER, sv->counters.retries);
1691 stats[ST_F_WREDIS] = mkf_u64(FN_COUNTER, sv->counters.redispatches);
Tim Duesterhus3fd19732018-05-27 20:35:08 +02001692 stats[ST_F_WREW] = mkf_u64(FN_COUNTER, sv->counters.failed_rewrites);
Christopher Faulet0159ee42019-12-16 14:40:39 +01001693 stats[ST_F_EINT] = mkf_u64(FN_COUNTER, sv->counters.internal_errors);
Willy Tarreauf1573842018-12-14 11:35:36 +01001694 stats[ST_F_CONNECT] = mkf_u64(FN_COUNTER, sv->counters.connect);
1695 stats[ST_F_REUSE] = mkf_u64(FN_COUNTER, sv->counters.reuse);
William Lallemand74c24fb2016-11-21 17:18:36 +01001696
1697 /* status */
1698 fld_status = chunk_newstr(out);
Emeric Brun52a91d32017-08-31 14:41:55 +02001699 if (sv->cur_admin & SRV_ADMF_RMAINT)
William Lallemand74c24fb2016-11-21 17:18:36 +01001700 chunk_appendf(out, "MAINT (resolution)");
Emeric Brun52a91d32017-08-31 14:41:55 +02001701 else if (sv->cur_admin & SRV_ADMF_IMAINT)
William Lallemand74c24fb2016-11-21 17:18:36 +01001702 chunk_appendf(out, "MAINT (via %s/%s)", via->proxy->id, via->id);
Emeric Brun52a91d32017-08-31 14:41:55 +02001703 else if (sv->cur_admin & SRV_ADMF_MAINT)
William Lallemand74c24fb2016-11-21 17:18:36 +01001704 chunk_appendf(out, "MAINT");
1705 else
1706 chunk_appendf(out,
1707 srv_hlt_st[state],
Emeric Brun52a91d32017-08-31 14:41:55 +02001708 (ref->cur_state != SRV_ST_STOPPED) ? (ref->check.health - ref->check.rise + 1) : (ref->check.health),
1709 (ref->cur_state != SRV_ST_STOPPED) ? (ref->check.fall) : (ref->check.rise));
William Lallemand74c24fb2016-11-21 17:18:36 +01001710
1711 stats[ST_F_STATUS] = mkf_str(FO_STATUS, fld_status);
1712 stats[ST_F_LASTCHG] = mkf_u32(FN_AGE, now.tv_sec - sv->last_change);
Emeric Brun52a91d32017-08-31 14:41:55 +02001713 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 +01001714 stats[ST_F_ACT] = mkf_u32(FO_STATUS, (sv->flags & SRV_F_BACKUP) ? 0 : 1);
1715 stats[ST_F_BCK] = mkf_u32(FO_STATUS, (sv->flags & SRV_F_BACKUP) ? 1 : 0);
1716
1717 /* check failures: unique, fatal; last change, total downtime */
1718 if (sv->check.state & CHK_ST_ENABLED) {
1719 stats[ST_F_CHKFAIL] = mkf_u64(FN_COUNTER, sv->counters.failed_checks);
1720 stats[ST_F_CHKDOWN] = mkf_u64(FN_COUNTER, sv->counters.down_trans);
1721 stats[ST_F_DOWNTIME] = mkf_u32(FN_COUNTER, srv_downtime(sv));
1722 }
1723
1724 if (sv->maxqueue)
1725 stats[ST_F_QLIMIT] = mkf_u32(FO_CONFIG|FS_SERVICE, sv->maxqueue);
1726
1727 stats[ST_F_PID] = mkf_u32(FO_KEY, relative_pid);
1728 stats[ST_F_IID] = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
1729 stats[ST_F_SID] = mkf_u32(FO_KEY|FS_SERVICE, sv->puid);
1730
Emeric Brun52a91d32017-08-31 14:41:55 +02001731 if (sv->cur_state == SRV_ST_STARTING && !server_is_draining(sv))
William Lallemand74c24fb2016-11-21 17:18:36 +01001732 stats[ST_F_THROTTLE] = mkf_u32(FN_AVG, server_throttle_rate(sv));
1733
1734 stats[ST_F_LBTOT] = mkf_u64(FN_COUNTER, sv->counters.cum_lbconn);
1735
1736 if (sv->track) {
1737 char *fld_track = chunk_newstr(out);
1738
1739 chunk_appendf(out, "%s/%s", sv->track->proxy->id, sv->track->id);
1740 stats[ST_F_TRACKED] = mkf_str(FO_CONFIG|FN_NAME|FS_SERVICE, fld_track);
1741 }
1742
1743 stats[ST_F_TYPE] = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_SV);
1744 stats[ST_F_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&sv->sess_per_sec));
1745 stats[ST_F_RATE_MAX] = mkf_u32(FN_MAX, sv->counters.sps_max);
1746
1747 if ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED) {
1748 const char *fld_chksts;
1749
1750 fld_chksts = chunk_newstr(out);
1751 chunk_strcat(out, "* "); // for check in progress
1752 chunk_strcat(out, get_check_status_info(sv->check.status));
1753 if (!(sv->check.state & CHK_ST_INPROGRESS))
1754 fld_chksts += 2; // skip "* "
1755 stats[ST_F_CHECK_STATUS] = mkf_str(FN_OUTPUT, fld_chksts);
1756
1757 if (sv->check.status >= HCHK_STATUS_L57DATA)
1758 stats[ST_F_CHECK_CODE] = mkf_u32(FN_OUTPUT, sv->check.code);
1759
1760 if (sv->check.status >= HCHK_STATUS_CHECKED)
1761 stats[ST_F_CHECK_DURATION] = mkf_u64(FN_DURATION, sv->check.duration);
1762
1763 stats[ST_F_CHECK_DESC] = mkf_str(FN_OUTPUT, get_check_status_description(sv->check.status));
1764 stats[ST_F_LAST_CHK] = mkf_str(FN_OUTPUT, sv->check.desc);
1765 stats[ST_F_CHECK_RISE] = mkf_u32(FO_CONFIG|FS_SERVICE, ref->check.rise);
1766 stats[ST_F_CHECK_FALL] = mkf_u32(FO_CONFIG|FS_SERVICE, ref->check.fall);
1767 stats[ST_F_CHECK_HEALTH] = mkf_u32(FO_CONFIG|FS_SERVICE, ref->check.health);
1768 }
1769
1770 if ((sv->agent.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED) {
1771 const char *fld_chksts;
1772
1773 fld_chksts = chunk_newstr(out);
1774 chunk_strcat(out, "* "); // for check in progress
1775 chunk_strcat(out, get_check_status_info(sv->agent.status));
1776 if (!(sv->agent.state & CHK_ST_INPROGRESS))
1777 fld_chksts += 2; // skip "* "
1778 stats[ST_F_AGENT_STATUS] = mkf_str(FN_OUTPUT, fld_chksts);
1779
1780 if (sv->agent.status >= HCHK_STATUS_L57DATA)
1781 stats[ST_F_AGENT_CODE] = mkf_u32(FN_OUTPUT, sv->agent.code);
1782
1783 if (sv->agent.status >= HCHK_STATUS_CHECKED)
1784 stats[ST_F_AGENT_DURATION] = mkf_u64(FN_DURATION, sv->agent.duration);
1785
1786 stats[ST_F_AGENT_DESC] = mkf_str(FN_OUTPUT, get_check_status_description(sv->agent.status));
1787 stats[ST_F_LAST_AGT] = mkf_str(FN_OUTPUT, sv->agent.desc);
1788 stats[ST_F_AGENT_RISE] = mkf_u32(FO_CONFIG|FS_SERVICE, sv->agent.rise);
1789 stats[ST_F_AGENT_FALL] = mkf_u32(FO_CONFIG|FS_SERVICE, sv->agent.fall);
1790 stats[ST_F_AGENT_HEALTH] = mkf_u32(FO_CONFIG|FS_SERVICE, sv->agent.health);
1791 }
1792
1793 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
1794 if (px->mode == PR_MODE_HTTP) {
Marcin Deranek3c27dda2020-05-15 18:32:51 +02001795 stats[ST_F_REQ_TOT] = mkf_u64(FN_COUNTER, sv->counters.p.http.cum_req);
William Lallemand74c24fb2016-11-21 17:18:36 +01001796 stats[ST_F_HRSP_1XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[1]);
1797 stats[ST_F_HRSP_2XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[2]);
1798 stats[ST_F_HRSP_3XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[3]);
1799 stats[ST_F_HRSP_4XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[4]);
1800 stats[ST_F_HRSP_5XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[5]);
1801 stats[ST_F_HRSP_OTHER] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[0]);
1802 }
1803
1804 if (ref->observe)
1805 stats[ST_F_HANAFAIL] = mkf_u64(FN_COUNTER, sv->counters.failed_hana);
1806
1807 stats[ST_F_CLI_ABRT] = mkf_u64(FN_COUNTER, sv->counters.cli_aborts);
1808 stats[ST_F_SRV_ABRT] = mkf_u64(FN_COUNTER, sv->counters.srv_aborts);
1809 stats[ST_F_LASTSESS] = mkf_s32(FN_AGE, srv_lastsession(sv));
1810
Marcin Deraneka8dbdf32020-05-15 20:02:40 +02001811 srv_samples_counter = (px->mode == PR_MODE_HTTP) ? sv->counters.p.http.cum_req : sv->counters.cum_lbconn;
1812 if (srv_samples_counter < TIME_STATS_SAMPLES && srv_samples_counter > 0)
1813 srv_samples_window = srv_samples_counter;
1814
1815 stats[ST_F_QTIME] = mkf_u32(FN_AVG, swrate_avg(sv->counters.q_time, srv_samples_window));
1816 stats[ST_F_CTIME] = mkf_u32(FN_AVG, swrate_avg(sv->counters.c_time, srv_samples_window));
1817 stats[ST_F_RTIME] = mkf_u32(FN_AVG, swrate_avg(sv->counters.d_time, srv_samples_window));
1818 stats[ST_F_TTIME] = mkf_u32(FN_AVG, swrate_avg(sv->counters.t_time, srv_samples_window));
William Lallemand74c24fb2016-11-21 17:18:36 +01001819
Christopher Faulet0d1c2a62019-11-08 14:59:51 +01001820 stats[ST_F_QT_MAX] = mkf_u32(FN_MAX, sv->counters.qtime_max);
1821 stats[ST_F_CT_MAX] = mkf_u32(FN_MAX, sv->counters.ctime_max);
1822 stats[ST_F_RT_MAX] = mkf_u32(FN_MAX, sv->counters.dtime_max);
1823 stats[ST_F_TT_MAX] = mkf_u32(FN_MAX, sv->counters.ttime_max);
1824
Willy Tarreau708c4162019-10-09 10:19:16 +02001825 if (flags & STAT_SHLGNDS) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001826 switch (addr_to_str(&sv->addr, str, sizeof(str))) {
1827 case AF_INET:
1828 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
Willy Tarreau04276f32017-01-06 17:41:29 +01001829 chunk_appendf(out, "%s:%d", str, sv->svc_port);
William Lallemand74c24fb2016-11-21 17:18:36 +01001830 break;
1831 case AF_INET6:
1832 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
Willy Tarreau04276f32017-01-06 17:41:29 +01001833 chunk_appendf(out, "[%s]:%d", str, sv->svc_port);
William Lallemand74c24fb2016-11-21 17:18:36 +01001834 break;
1835 case AF_UNIX:
1836 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, "unix");
1837 break;
1838 case -1:
1839 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
1840 chunk_strcat(out, strerror(errno));
1841 break;
1842 default: /* address family not supported */
1843 break;
1844 }
1845
1846 if (sv->cookie)
1847 stats[ST_F_COOKIE] = mkf_str(FO_CONFIG|FN_NAME|FS_SERVICE, sv->cookie);
1848 }
1849
1850 return 1;
1851}
1852
1853/* Dumps a line for server <sv> and proxy <px> to the trash and uses the state
Willy Tarreau43241ff2019-10-09 11:27:51 +02001854 * from stream interface <si>, and server state <state>. The caller is
1855 * responsible for clearing the trash if needed. Returns non-zero if it emits
1856 * anything, zero otherwise.
William Lallemand74c24fb2016-11-21 17:18:36 +01001857 */
Willy Tarreau43241ff2019-10-09 11:27:51 +02001858static int stats_dump_sv_stats(struct stream_interface *si, struct proxy *px, struct server *sv)
William Lallemand74c24fb2016-11-21 17:18:36 +01001859{
1860 struct appctx *appctx = __objt_appctx(si->end);
1861
Willy Tarreau43241ff2019-10-09 11:27:51 +02001862 if (!stats_fill_sv_stats(px, sv, appctx->ctx.stats.flags, stats, ST_F_TOTAL_FIELDS))
William Lallemand74c24fb2016-11-21 17:18:36 +01001863 return 0;
1864
Willy Tarreau43241ff2019-10-09 11:27:51 +02001865 return stats_dump_one_line(stats, px, appctx);
William Lallemand74c24fb2016-11-21 17:18:36 +01001866}
1867
1868/* Fill <stats> with the backend statistics. <stats> is
1869 * preallocated array of length <len>. The length of the array
1870 * must be at least ST_F_TOTAL_FIELDS. If this length is less
1871 * then this value, the function returns 0, otherwise, it
Willy Tarreau708c4162019-10-09 10:19:16 +02001872 * returns 1. <flags> can take the value STAT_SHLGNDS.
William Lallemand74c24fb2016-11-21 17:18:36 +01001873 */
1874int stats_fill_be_stats(struct proxy *px, int flags, struct field *stats, int len)
1875{
Marcin Deraneka8dbdf32020-05-15 20:02:40 +02001876 long long be_samples_counter;
1877 unsigned int be_samples_window = TIME_STATS_SAMPLES;
1878
William Lallemand74c24fb2016-11-21 17:18:36 +01001879 if (len < ST_F_TOTAL_FIELDS)
1880 return 0;
1881
1882 memset(stats, 0, sizeof(*stats) * len);
1883
1884 stats[ST_F_PXNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
1885 stats[ST_F_SVNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, "BACKEND");
1886 stats[ST_F_MODE] = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
1887 stats[ST_F_QCUR] = mkf_u32(0, px->nbpend);
1888 stats[ST_F_QMAX] = mkf_u32(FN_MAX, px->be_counters.nbpend_max);
Thierry FOURNIER0ff98a42016-12-19 16:50:42 +01001889 stats[ST_F_SCUR] = mkf_u32(0, px->beconn);
William Lallemand74c24fb2016-11-21 17:18:36 +01001890 stats[ST_F_SMAX] = mkf_u32(FN_MAX, px->be_counters.conn_max);
1891 stats[ST_F_SLIM] = mkf_u32(FO_CONFIG|FN_LIMIT, px->fullconn);
1892 stats[ST_F_STOT] = mkf_u64(FN_COUNTER, px->be_counters.cum_conn);
1893 stats[ST_F_BIN] = mkf_u64(FN_COUNTER, px->be_counters.bytes_in);
1894 stats[ST_F_BOUT] = mkf_u64(FN_COUNTER, px->be_counters.bytes_out);
1895 stats[ST_F_DREQ] = mkf_u64(FN_COUNTER, px->be_counters.denied_req);
1896 stats[ST_F_DRESP] = mkf_u64(FN_COUNTER, px->be_counters.denied_resp);
1897 stats[ST_F_ECON] = mkf_u64(FN_COUNTER, px->be_counters.failed_conns);
1898 stats[ST_F_ERESP] = mkf_u64(FN_COUNTER, px->be_counters.failed_resp);
1899 stats[ST_F_WRETR] = mkf_u64(FN_COUNTER, px->be_counters.retries);
1900 stats[ST_F_WREDIS] = mkf_u64(FN_COUNTER, px->be_counters.redispatches);
Tim Duesterhus3fd19732018-05-27 20:35:08 +02001901 stats[ST_F_WREW] = mkf_u64(FN_COUNTER, px->be_counters.failed_rewrites);
Christopher Faulet0159ee42019-12-16 14:40:39 +01001902 stats[ST_F_EINT] = mkf_u64(FN_COUNTER, px->be_counters.internal_errors);
Willy Tarreauf1573842018-12-14 11:35:36 +01001903 stats[ST_F_CONNECT] = mkf_u64(FN_COUNTER, px->be_counters.connect);
1904 stats[ST_F_REUSE] = mkf_u64(FN_COUNTER, px->be_counters.reuse);
William Lallemand74c24fb2016-11-21 17:18:36 +01001905 stats[ST_F_STATUS] = mkf_str(FO_STATUS, (px->lbprm.tot_weight > 0 || !px->srv) ? "UP" : "DOWN");
1906 stats[ST_F_WEIGHT] = mkf_u32(FN_AVG, (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv);
1907 stats[ST_F_ACT] = mkf_u32(0, px->srv_act);
1908 stats[ST_F_BCK] = mkf_u32(0, px->srv_bck);
1909 stats[ST_F_CHKDOWN] = mkf_u64(FN_COUNTER, px->down_trans);
1910 stats[ST_F_LASTCHG] = mkf_u32(FN_AGE, now.tv_sec - px->last_change);
1911 if (px->srv)
1912 stats[ST_F_DOWNTIME] = mkf_u32(FN_COUNTER, be_downtime(px));
1913
1914 stats[ST_F_PID] = mkf_u32(FO_KEY, relative_pid);
1915 stats[ST_F_IID] = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
1916 stats[ST_F_SID] = mkf_u32(FO_KEY|FS_SERVICE, 0);
1917 stats[ST_F_LBTOT] = mkf_u64(FN_COUNTER, px->be_counters.cum_lbconn);
1918 stats[ST_F_TYPE] = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_BE);
1919 stats[ST_F_RATE] = mkf_u32(0, read_freq_ctr(&px->be_sess_per_sec));
1920 stats[ST_F_RATE_MAX] = mkf_u32(0, px->be_counters.sps_max);
1921
Willy Tarreau708c4162019-10-09 10:19:16 +02001922 if (flags & STAT_SHLGNDS) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001923 if (px->cookie_name)
1924 stats[ST_F_COOKIE] = mkf_str(FO_CONFIG|FN_NAME|FS_SERVICE, px->cookie_name);
1925 stats[ST_F_ALGO] = mkf_str(FO_CONFIG|FS_SERVICE, backend_lb_algo_str(px->lbprm.algo & BE_LB_ALGO));
1926 }
1927
1928 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
1929 if (px->mode == PR_MODE_HTTP) {
1930 stats[ST_F_REQ_TOT] = mkf_u64(FN_COUNTER, px->be_counters.p.http.cum_req);
1931 stats[ST_F_HRSP_1XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[1]);
1932 stats[ST_F_HRSP_2XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[2]);
1933 stats[ST_F_HRSP_3XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[3]);
1934 stats[ST_F_HRSP_4XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[4]);
1935 stats[ST_F_HRSP_5XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[5]);
1936 stats[ST_F_HRSP_OTHER] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[0]);
Willy Tarreaua1214a52018-12-14 14:00:25 +01001937 stats[ST_F_CACHE_LOOKUPS] = mkf_u64(FN_COUNTER, px->be_counters.p.http.cache_lookups);
1938 stats[ST_F_CACHE_HITS] = mkf_u64(FN_COUNTER, px->be_counters.p.http.cache_hits);
William Lallemand74c24fb2016-11-21 17:18:36 +01001939 }
1940
1941 stats[ST_F_CLI_ABRT] = mkf_u64(FN_COUNTER, px->be_counters.cli_aborts);
1942 stats[ST_F_SRV_ABRT] = mkf_u64(FN_COUNTER, px->be_counters.srv_aborts);
1943
1944 /* compression: in, out, bypassed, responses */
1945 stats[ST_F_COMP_IN] = mkf_u64(FN_COUNTER, px->be_counters.comp_in);
1946 stats[ST_F_COMP_OUT] = mkf_u64(FN_COUNTER, px->be_counters.comp_out);
1947 stats[ST_F_COMP_BYP] = mkf_u64(FN_COUNTER, px->be_counters.comp_byp);
1948 stats[ST_F_COMP_RSP] = mkf_u64(FN_COUNTER, px->be_counters.p.http.comp_rsp);
1949 stats[ST_F_LASTSESS] = mkf_s32(FN_AGE, be_lastsession(px));
1950
Marcin Deraneka8dbdf32020-05-15 20:02:40 +02001951 be_samples_counter = (px->mode == PR_MODE_HTTP) ? px->be_counters.p.http.cum_req : px->be_counters.cum_lbconn;
1952 if (be_samples_counter < TIME_STATS_SAMPLES && be_samples_counter > 0)
1953 be_samples_window = be_samples_counter;
1954
1955 stats[ST_F_QTIME] = mkf_u32(FN_AVG, swrate_avg(px->be_counters.q_time, be_samples_window));
1956 stats[ST_F_CTIME] = mkf_u32(FN_AVG, swrate_avg(px->be_counters.c_time, be_samples_window));
1957 stats[ST_F_RTIME] = mkf_u32(FN_AVG, swrate_avg(px->be_counters.d_time, be_samples_window));
1958 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 +01001959
Christopher Faulet0d1c2a62019-11-08 14:59:51 +01001960 stats[ST_F_QT_MAX] = mkf_u32(FN_MAX, px->be_counters.qtime_max);
1961 stats[ST_F_CT_MAX] = mkf_u32(FN_MAX, px->be_counters.ctime_max);
1962 stats[ST_F_RT_MAX] = mkf_u32(FN_MAX, px->be_counters.dtime_max);
1963 stats[ST_F_TT_MAX] = mkf_u32(FN_MAX, px->be_counters.ttime_max);
1964
William Lallemand74c24fb2016-11-21 17:18:36 +01001965 return 1;
1966}
1967
1968/* Dumps a line for backend <px> to the trash for and uses the state from stream
Willy Tarreau43241ff2019-10-09 11:27:51 +02001969 * interface <si>. The caller is responsible for clearing the trash if needed.
1970 * Returns non-zero if it emits anything, zero otherwise.
William Lallemand74c24fb2016-11-21 17:18:36 +01001971 */
Willy Tarreau43241ff2019-10-09 11:27:51 +02001972static int stats_dump_be_stats(struct stream_interface *si, struct proxy *px)
William Lallemand74c24fb2016-11-21 17:18:36 +01001973{
1974 struct appctx *appctx = __objt_appctx(si->end);
1975
1976 if (!(px->cap & PR_CAP_BE))
1977 return 0;
1978
1979 if ((appctx->ctx.stats.flags & STAT_BOUND) && !(appctx->ctx.stats.type & (1 << STATS_TYPE_BE)))
1980 return 0;
1981
Willy Tarreau43241ff2019-10-09 11:27:51 +02001982 if (!stats_fill_be_stats(px, appctx->ctx.stats.flags, stats, ST_F_TOTAL_FIELDS))
William Lallemand74c24fb2016-11-21 17:18:36 +01001983 return 0;
1984
Willy Tarreau43241ff2019-10-09 11:27:51 +02001985 return stats_dump_one_line(stats, px, appctx);
William Lallemand74c24fb2016-11-21 17:18:36 +01001986}
1987
1988/* Dumps the HTML table header for proxy <px> to the trash for and uses the state from
1989 * stream interface <si> and per-uri parameters <uri>. The caller is responsible
1990 * for clearing the trash if needed.
1991 */
Willy Tarreau676c29e2019-10-09 10:50:01 +02001992static void stats_dump_html_px_hdr(struct stream_interface *si, struct proxy *px)
William Lallemand74c24fb2016-11-21 17:18:36 +01001993{
1994 struct appctx *appctx = __objt_appctx(si->end);
1995 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
1996
1997 if (px->cap & PR_CAP_BE && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
1998 /* A form to enable/disable this proxy servers */
1999
2000 /* scope_txt = search pattern + search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
2001 scope_txt[0] = 0;
2002 if (appctx->ctx.stats.scope_len) {
Christopher Fauleted7a0662019-01-14 11:07:34 +01002003 const char *scope_ptr = stats_scope_ptr(appctx, si);
2004
William Lallemand74c24fb2016-11-21 17:18:36 +01002005 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Christopher Fauleted7a0662019-01-14 11:07:34 +01002006 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), scope_ptr, appctx->ctx.stats.scope_len);
William Lallemand74c24fb2016-11-21 17:18:36 +01002007 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
2008 }
2009
2010 chunk_appendf(&trash,
2011 "<form method=\"post\">");
2012 }
2013
2014 /* print a new table */
2015 chunk_appendf(&trash,
2016 "<table class=\"tbl\" width=\"100%%\">\n"
2017 "<tr class=\"titre\">"
2018 "<th class=\"pxname\" width=\"10%%\">");
2019
2020 chunk_appendf(&trash,
2021 "<a name=\"%s\"></a>%s"
2022 "<a class=px href=\"#%s\">%s</a>",
2023 px->id,
Willy Tarreau676c29e2019-10-09 10:50:01 +02002024 (appctx->ctx.stats.flags & STAT_SHLGNDS) ? "<u>":"",
William Lallemand74c24fb2016-11-21 17:18:36 +01002025 px->id, px->id);
2026
Willy Tarreau676c29e2019-10-09 10:50:01 +02002027 if (appctx->ctx.stats.flags & STAT_SHLGNDS) {
William Lallemand74c24fb2016-11-21 17:18:36 +01002028 /* cap, mode, id */
2029 chunk_appendf(&trash, "<div class=tips>cap: %s, mode: %s, id: %d",
2030 proxy_cap_str(px->cap), proxy_mode_str(px->mode),
2031 px->uuid);
2032 chunk_appendf(&trash, "</div>");
2033 }
2034
2035 chunk_appendf(&trash,
2036 "%s</th>"
2037 "<th class=\"%s\" width=\"90%%\">%s</th>"
2038 "</tr>\n"
2039 "</table>\n"
2040 "<table class=\"tbl\" width=\"100%%\">\n"
2041 "<tr class=\"titre\">",
Willy Tarreau676c29e2019-10-09 10:50:01 +02002042 (appctx->ctx.stats.flags & STAT_SHLGNDS) ? "</u>":"",
William Lallemand74c24fb2016-11-21 17:18:36 +01002043 px->desc ? "desc" : "empty", px->desc ? px->desc : "");
2044
Christopher Fauletbc271ec2019-12-02 11:29:04 +01002045 if (appctx->ctx.stats.flags & STAT_ADMIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +01002046 /* Column heading for Enable or Disable server */
Christopher Fauletbc271ec2019-12-02 11:29:04 +01002047 if ((px->cap & PR_CAP_BE) && px->srv)
2048 chunk_appendf(&trash,
2049 "<th rowspan=2 width=1><input type=\"checkbox\" "
2050 "onclick=\"for(c in document.getElementsByClassName('%s-checkbox')) "
2051 "document.getElementsByClassName('%s-checkbox').item(c).checked = this.checked\"></th>",
2052 px->id,
2053 px->id);
2054 else
2055 chunk_appendf(&trash, "<th rowspan=2></th>");
William Lallemand74c24fb2016-11-21 17:18:36 +01002056 }
2057
2058 chunk_appendf(&trash,
2059 "<th rowspan=2></th>"
2060 "<th colspan=3>Queue</th>"
2061 "<th colspan=3>Session rate</th><th colspan=6>Sessions</th>"
2062 "<th colspan=2>Bytes</th><th colspan=2>Denied</th>"
2063 "<th colspan=3>Errors</th><th colspan=2>Warnings</th>"
2064 "<th colspan=9>Server</th>"
2065 "</tr>\n"
2066 "<tr class=\"titre\">"
2067 "<th>Cur</th><th>Max</th><th>Limit</th>"
2068 "<th>Cur</th><th>Max</th><th>Limit</th><th>Cur</th><th>Max</th>"
2069 "<th>Limit</th><th>Total</th><th>LbTot</th><th>Last</th><th>In</th><th>Out</th>"
2070 "<th>Req</th><th>Resp</th><th>Req</th><th>Conn</th>"
2071 "<th>Resp</th><th>Retr</th><th>Redis</th>"
2072 "<th>Status</th><th>LastChk</th><th>Wght</th><th>Act</th>"
2073 "<th>Bck</th><th>Chk</th><th>Dwn</th><th>Dwntme</th>"
2074 "<th>Thrtle</th>\n"
2075 "</tr>");
2076}
2077
2078/* Dumps the HTML table trailer for proxy <px> to the trash for and uses the state from
2079 * stream interface <si>. The caller is responsible for clearing the trash if needed.
2080 */
2081static void stats_dump_html_px_end(struct stream_interface *si, struct proxy *px)
2082{
2083 struct appctx *appctx = __objt_appctx(si->end);
2084 chunk_appendf(&trash, "</table>");
2085
2086 if ((px->cap & PR_CAP_BE) && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
2087 /* close the form used to enable/disable this proxy servers */
2088 chunk_appendf(&trash,
2089 "Choose the action to perform on the checked servers : "
2090 "<select name=action>"
2091 "<option value=\"\"></option>"
2092 "<option value=\"ready\">Set state to READY</option>"
2093 "<option value=\"drain\">Set state to DRAIN</option>"
2094 "<option value=\"maint\">Set state to MAINT</option>"
2095 "<option value=\"dhlth\">Health: disable checks</option>"
2096 "<option value=\"ehlth\">Health: enable checks</option>"
2097 "<option value=\"hrunn\">Health: force UP</option>"
2098 "<option value=\"hnolb\">Health: force NOLB</option>"
2099 "<option value=\"hdown\">Health: force DOWN</option>"
2100 "<option value=\"dagent\">Agent: disable checks</option>"
2101 "<option value=\"eagent\">Agent: enable checks</option>"
2102 "<option value=\"arunn\">Agent: force UP</option>"
2103 "<option value=\"adown\">Agent: force DOWN</option>"
2104 "<option value=\"shutdown\">Kill Sessions</option>"
2105 "</select>"
2106 "<input type=\"hidden\" name=\"b\" value=\"#%d\">"
2107 "&nbsp;<input type=\"submit\" value=\"Apply\">"
2108 "</form>",
2109 px->uuid);
2110 }
2111
2112 chunk_appendf(&trash, "<p>\n");
2113}
2114
2115/*
2116 * Dumps statistics for a proxy. The output is sent to the stream interface's
2117 * input buffer. Returns 0 if it had to stop dumping data because of lack of
2118 * buffer space, or non-zero if everything completed. This function is used
2119 * both by the CLI and the HTTP entry points, and is able to dump the output
2120 * in HTML or CSV formats. If the later, <uri> must be NULL.
2121 */
Christopher Fauletef779222018-10-31 08:47:01 +01002122int stats_dump_proxy_to_buffer(struct stream_interface *si, struct htx *htx,
2123 struct proxy *px, struct uri_auth *uri)
William Lallemand74c24fb2016-11-21 17:18:36 +01002124{
2125 struct appctx *appctx = __objt_appctx(si->end);
2126 struct stream *s = si_strm(si);
2127 struct channel *rep = si_ic(si);
2128 struct server *sv, *svs; /* server and server-state, server-state=server or server->track */
2129 struct listener *l;
William Lallemand74c24fb2016-11-21 17:18:36 +01002130
2131 chunk_reset(&trash);
2132
2133 switch (appctx->ctx.stats.px_st) {
2134 case STAT_PX_ST_INIT:
2135 /* we are on a new proxy */
2136 if (uri && uri->scope) {
2137 /* we have a limited scope, we have to check the proxy name */
2138 struct stat_scope *scope;
2139 int len;
2140
2141 len = strlen(px->id);
2142 scope = uri->scope;
2143
2144 while (scope) {
2145 /* match exact proxy name */
2146 if (scope->px_len == len && !memcmp(px->id, scope->px_id, len))
2147 break;
2148
2149 /* match '.' which means 'self' proxy */
2150 if (!strcmp(scope->px_id, ".") && px == s->be)
2151 break;
2152 scope = scope->next;
2153 }
2154
2155 /* proxy name not found : don't dump anything */
2156 if (scope == NULL)
2157 return 1;
2158 }
2159
2160 /* if the user has requested a limited output and the proxy
2161 * name does not match, skip it.
2162 */
Christopher Fauleted7a0662019-01-14 11:07:34 +01002163 if (appctx->ctx.stats.scope_len) {
2164 const char *scope_ptr = stats_scope_ptr(appctx, si);
2165
2166 if (strnistr(px->id, strlen(px->id), scope_ptr, appctx->ctx.stats.scope_len) == NULL)
2167 return 1;
2168 }
William Lallemand74c24fb2016-11-21 17:18:36 +01002169
2170 if ((appctx->ctx.stats.flags & STAT_BOUND) &&
2171 (appctx->ctx.stats.iid != -1) &&
2172 (px->uuid != appctx->ctx.stats.iid))
2173 return 1;
2174
2175 appctx->ctx.stats.px_st = STAT_PX_ST_TH;
2176 /* fall through */
2177
2178 case STAT_PX_ST_TH:
2179 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreau676c29e2019-10-09 10:50:01 +02002180 stats_dump_html_px_hdr(si, px);
Christopher Fauletef779222018-10-31 08:47:01 +01002181 if (!stats_putchk(rep, htx, &trash))
2182 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002183 }
2184
2185 appctx->ctx.stats.px_st = STAT_PX_ST_FE;
2186 /* fall through */
2187
2188 case STAT_PX_ST_FE:
2189 /* print the frontend */
2190 if (stats_dump_fe_stats(si, px)) {
Christopher Fauletef779222018-10-31 08:47:01 +01002191 if (!stats_putchk(rep, htx, &trash))
2192 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002193 }
2194
2195 appctx->ctx.stats.l = px->conf.listeners.n;
2196 appctx->ctx.stats.px_st = STAT_PX_ST_LI;
2197 /* fall through */
2198
2199 case STAT_PX_ST_LI:
2200 /* stats.l has been initialized above */
2201 for (; appctx->ctx.stats.l != &px->conf.listeners; appctx->ctx.stats.l = l->by_fe.n) {
Christopher Fauletef779222018-10-31 08:47:01 +01002202 if (htx) {
2203 if (htx_almost_full(htx))
2204 goto full;
2205 }
2206 else {
2207 if (buffer_almost_full(&rep->buf))
2208 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002209 }
2210
2211 l = LIST_ELEM(appctx->ctx.stats.l, struct listener *, by_fe);
2212 if (!l->counters)
2213 continue;
2214
2215 if (appctx->ctx.stats.flags & STAT_BOUND) {
2216 if (!(appctx->ctx.stats.type & (1 << STATS_TYPE_SO)))
2217 break;
2218
2219 if (appctx->ctx.stats.sid != -1 && l->luid != appctx->ctx.stats.sid)
2220 continue;
2221 }
2222
2223 /* print the frontend */
Willy Tarreau43241ff2019-10-09 11:27:51 +02002224 if (stats_dump_li_stats(si, px, l)) {
Christopher Fauletef779222018-10-31 08:47:01 +01002225 if (!stats_putchk(rep, htx, &trash))
2226 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002227 }
2228 }
2229
2230 appctx->ctx.stats.sv = px->srv; /* may be NULL */
2231 appctx->ctx.stats.px_st = STAT_PX_ST_SV;
2232 /* fall through */
2233
2234 case STAT_PX_ST_SV:
2235 /* stats.sv has been initialized above */
2236 for (; appctx->ctx.stats.sv != NULL; appctx->ctx.stats.sv = sv->next) {
Christopher Fauletef779222018-10-31 08:47:01 +01002237 if (htx) {
2238 if (htx_almost_full(htx))
2239 goto full;
2240 }
2241 else {
2242 if (buffer_almost_full(&rep->buf))
2243 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002244 }
2245
2246 sv = appctx->ctx.stats.sv;
2247
2248 if (appctx->ctx.stats.flags & STAT_BOUND) {
2249 if (!(appctx->ctx.stats.type & (1 << STATS_TYPE_SV)))
2250 break;
2251
2252 if (appctx->ctx.stats.sid != -1 && sv->puid != appctx->ctx.stats.sid)
2253 continue;
2254 }
2255
2256 svs = sv;
2257 while (svs->track)
2258 svs = svs->track;
2259
2260 /* do not report servers which are DOWN and not changing state */
2261 if ((appctx->ctx.stats.flags & STAT_HIDE_DOWN) &&
Emeric Brun52a91d32017-08-31 14:41:55 +02002262 ((sv->cur_admin & SRV_ADMF_MAINT) || /* server is in maintenance */
2263 (sv->cur_state == SRV_ST_STOPPED && /* server is down */
William Lallemand74c24fb2016-11-21 17:18:36 +01002264 (!((svs->agent.state | svs->check.state) & CHK_ST_ENABLED) ||
2265 ((svs->agent.state & CHK_ST_ENABLED) && !svs->agent.health) ||
2266 ((svs->check.state & CHK_ST_ENABLED) && !svs->check.health))))) {
2267 continue;
2268 }
2269
Willy Tarreau43241ff2019-10-09 11:27:51 +02002270 if (stats_dump_sv_stats(si, px, sv)) {
Christopher Fauletef779222018-10-31 08:47:01 +01002271 if (!stats_putchk(rep, htx, &trash))
2272 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002273 }
2274 } /* for sv */
2275
2276 appctx->ctx.stats.px_st = STAT_PX_ST_BE;
2277 /* fall through */
2278
2279 case STAT_PX_ST_BE:
2280 /* print the backend */
Willy Tarreau43241ff2019-10-09 11:27:51 +02002281 if (stats_dump_be_stats(si, px)) {
Christopher Fauletef779222018-10-31 08:47:01 +01002282 if (!stats_putchk(rep, htx, &trash))
2283 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002284 }
2285
2286 appctx->ctx.stats.px_st = STAT_PX_ST_END;
2287 /* fall through */
2288
2289 case STAT_PX_ST_END:
2290 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
2291 stats_dump_html_px_end(si, px);
Christopher Fauletef779222018-10-31 08:47:01 +01002292 if (!stats_putchk(rep, htx, &trash))
2293 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002294 }
2295
2296 appctx->ctx.stats.px_st = STAT_PX_ST_FIN;
2297 /* fall through */
2298
2299 case STAT_PX_ST_FIN:
2300 return 1;
2301
2302 default:
2303 /* unknown state, we should put an abort() here ! */
2304 return 1;
2305 }
Christopher Fauletef779222018-10-31 08:47:01 +01002306
2307 full:
2308 si_rx_room_blk(si);
2309 return 0;
William Lallemand74c24fb2016-11-21 17:18:36 +01002310}
2311
2312/* Dumps the HTTP stats head block to the trash for and uses the per-uri
2313 * parameters <uri>. The caller is responsible for clearing the trash if needed.
2314 */
Willy Tarreau676c29e2019-10-09 10:50:01 +02002315static void stats_dump_html_head(struct appctx *appctx, struct uri_auth *uri)
William Lallemand74c24fb2016-11-21 17:18:36 +01002316{
2317 /* WARNING! This must fit in the first buffer !!! */
2318 chunk_appendf(&trash,
2319 "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"\n"
2320 "\"http://www.w3.org/TR/html4/loose.dtd\">\n"
2321 "<html><head><title>Statistics Report for " PRODUCT_NAME "%s%s</title>\n"
2322 "<meta http-equiv=\"content-type\" content=\"text/html; charset=iso-8859-1\">\n"
2323 "<style type=\"text/css\"><!--\n"
2324 "body {"
2325 " font-family: arial, helvetica, sans-serif;"
2326 " font-size: 12px;"
2327 " font-weight: normal;"
2328 " color: black;"
2329 " background: white;"
2330 "}\n"
2331 "th,td {"
2332 " font-size: 10px;"
2333 "}\n"
2334 "h1 {"
2335 " font-size: x-large;"
2336 " margin-bottom: 0.5em;"
2337 "}\n"
2338 "h2 {"
2339 " font-family: helvetica, arial;"
2340 " font-size: x-large;"
2341 " font-weight: bold;"
2342 " font-style: italic;"
2343 " color: #6020a0;"
2344 " margin-top: 0em;"
2345 " margin-bottom: 0em;"
2346 "}\n"
2347 "h3 {"
2348 " font-family: helvetica, arial;"
2349 " font-size: 16px;"
2350 " font-weight: bold;"
2351 " color: #b00040;"
2352 " background: #e8e8d0;"
2353 " margin-top: 0em;"
2354 " margin-bottom: 0em;"
2355 "}\n"
2356 "li {"
2357 " margin-top: 0.25em;"
2358 " margin-right: 2em;"
2359 "}\n"
2360 ".hr {margin-top: 0.25em;"
2361 " border-color: black;"
2362 " border-bottom-style: solid;"
2363 "}\n"
2364 ".titre {background: #20D0D0;color: #000000; font-weight: bold; text-align: center;}\n"
2365 ".total {background: #20D0D0;color: #ffff80;}\n"
2366 ".frontend {background: #e8e8d0;}\n"
2367 ".socket {background: #d0d0d0;}\n"
2368 ".backend {background: #e8e8d0;}\n"
2369 ".active_down {background: #ff9090;}\n"
2370 ".active_going_up {background: #ffd020;}\n"
2371 ".active_going_down {background: #ffffa0;}\n"
2372 ".active_up {background: #c0ffc0;}\n"
2373 ".active_nolb {background: #20a0ff;}\n"
2374 ".active_draining {background: #20a0FF;}\n"
2375 ".active_no_check {background: #e0e0e0;}\n"
2376 ".backup_down {background: #ff9090;}\n"
2377 ".backup_going_up {background: #ff80ff;}\n"
2378 ".backup_going_down {background: #c060ff;}\n"
2379 ".backup_up {background: #b0d0ff;}\n"
2380 ".backup_nolb {background: #90b0e0;}\n"
2381 ".backup_draining {background: #cc9900;}\n"
2382 ".backup_no_check {background: #e0e0e0;}\n"
2383 ".maintain {background: #c07820;}\n"
2384 ".rls {letter-spacing: 0.2em; margin-right: 1px;}\n" /* right letter spacing (used for grouping digits) */
2385 "\n"
2386 "a.px:link {color: #ffff40; text-decoration: none;}"
2387 "a.px:visited {color: #ffff40; text-decoration: none;}"
2388 "a.px:hover {color: #ffffff; text-decoration: none;}"
2389 "a.lfsb:link {color: #000000; text-decoration: none;}"
2390 "a.lfsb:visited {color: #000000; text-decoration: none;}"
2391 "a.lfsb:hover {color: #505050; text-decoration: none;}"
2392 "\n"
2393 "table.tbl { border-collapse: collapse; border-style: none;}\n"
2394 "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"
2395 "table.tbl td.ac { text-align: center;}\n"
2396 "table.tbl th { border-width: 1px; border-style: solid solid solid solid; border-color: gray;}\n"
2397 "table.tbl th.pxname { background: #b00040; color: #ffff40; font-weight: bold; border-style: solid solid none solid; padding: 2px 3px; white-space: nowrap;}\n"
2398 "table.tbl th.empty { border-style: none; empty-cells: hide; background: white;}\n"
2399 "table.tbl th.desc { background: white; border-style: solid solid none solid; text-align: left; padding: 2px 3px;}\n"
2400 "\n"
2401 "table.lgd { border-collapse: collapse; border-width: 1px; border-style: none none none solid; border-color: black;}\n"
2402 "table.lgd td { border-width: 1px; border-style: solid solid solid solid; border-color: gray; padding: 2px;}\n"
2403 "table.lgd td.noborder { border-style: none; padding: 2px; white-space: nowrap;}\n"
2404 "table.det { border-collapse: collapse; border-style: none; }\n"
2405 "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"
2406 "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"
2407 "u {text-decoration:none; border-bottom: 1px dotted black;}\n"
2408 "div.tips {\n"
2409 " display:block;\n"
2410 " visibility:hidden;\n"
2411 " z-index:2147483647;\n"
2412 " position:absolute;\n"
2413 " padding:2px 4px 3px;\n"
2414 " background:#f0f060; color:#000000;\n"
2415 " border:1px solid #7040c0;\n"
2416 " white-space:nowrap;\n"
2417 " font-style:normal;font-size:11px;font-weight:normal;\n"
2418 " -moz-border-radius:3px;-webkit-border-radius:3px;border-radius:3px;\n"
2419 " -moz-box-shadow:gray 2px 2px 3px;-webkit-box-shadow:gray 2px 2px 3px;box-shadow:gray 2px 2px 3px;\n"
2420 "}\n"
2421 "u:hover div.tips {visibility:visible;}\n"
2422 "-->\n"
2423 "</style></head>\n",
Willy Tarreau676c29e2019-10-09 10:50:01 +02002424 (appctx->ctx.stats.flags & STAT_SHNODE) ? " on " : "",
Willy Tarreau027d2062020-01-23 11:47:13 +01002425 (appctx->ctx.stats.flags & STAT_SHNODE) ? (uri && uri->node ? uri->node : global.node) : ""
William Lallemand74c24fb2016-11-21 17:18:36 +01002426 );
2427}
2428
2429/* Dumps the HTML stats information block to the trash for and uses the state from
2430 * stream interface <si> and per-uri parameters <uri>. The caller is responsible
2431 * for clearing the trash if needed.
2432 */
2433static void stats_dump_html_info(struct stream_interface *si, struct uri_auth *uri)
2434{
2435 struct appctx *appctx = __objt_appctx(si->end);
2436 unsigned int up = (now.tv_sec - start_date.tv_sec);
2437 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
Christopher Fauleted7a0662019-01-14 11:07:34 +01002438 const char *scope_ptr = stats_scope_ptr(appctx, si);
Willy Tarreau1713c032019-05-23 12:23:55 +02002439 unsigned long long bps = (unsigned long long)read_freq_ctr(&global.out_32bps) * 32;
2440
2441 /* Turn the bytes per second to bits per second and take care of the
2442 * usual ethernet overhead in order to help figure how far we are from
2443 * interface saturation since it's the only case which usually matters.
2444 * For this we count the total size of an Ethernet frame on the wire
2445 * including preamble and IFG (1538) for the largest TCP segment it
2446 * transports (1448 with TCP timestamps). This is not valid for smaller
2447 * packets (under-estimated), but it gives a reasonably accurate
2448 * estimation of how far we are from uplink saturation.
2449 */
2450 bps = bps * 8 * 1538 / 1448;
William Lallemand74c24fb2016-11-21 17:18:36 +01002451
2452 /* WARNING! this has to fit the first packet too.
2453 * We are around 3.5 kB, add adding entries will
2454 * become tricky if we want to support 4kB buffers !
2455 */
2456 chunk_appendf(&trash,
2457 "<body><h1><a href=\"" PRODUCT_URL "\" style=\"text-decoration: none;\">"
2458 PRODUCT_NAME "%s</a></h1>\n"
2459 "<h2>Statistics Report for pid %d%s%s%s%s</h2>\n"
2460 "<hr width=\"100%%\" class=\"hr\">\n"
2461 "<h3>&gt; General process information</h3>\n"
2462 "<table border=0><tr><td align=\"left\" nowrap width=\"1%%\">\n"
Yves Lafon95317282018-02-26 11:10:37 +01002463 "<p><b>pid = </b> %d (process #%d, nbproc = %d, nbthread = %d)<br>\n"
William Lallemand74c24fb2016-11-21 17:18:36 +01002464 "<b>uptime = </b> %dd %dh%02dm%02ds<br>\n"
2465 "<b>system limits:</b> memmax = %s%s; ulimit-n = %d<br>\n"
2466 "<b>maxsock = </b> %d; <b>maxconn = </b> %d; <b>maxpipes = </b> %d<br>\n"
Willy Tarreau1713c032019-05-23 12:23:55 +02002467 "current conns = %d; current pipes = %d/%d; conn rate = %d/sec; bit rate = %.3f %cbps<br>\n"
William Lallemand74c24fb2016-11-21 17:18:36 +01002468 "Running tasks: %d/%d; idle = %d %%<br>\n"
2469 "</td><td align=\"center\" nowrap>\n"
2470 "<table class=\"lgd\"><tr>\n"
2471 "<td class=\"active_up\">&nbsp;</td><td class=\"noborder\">active UP </td>"
2472 "<td class=\"backup_up\">&nbsp;</td><td class=\"noborder\">backup UP </td>"
2473 "</tr><tr>\n"
2474 "<td class=\"active_going_down\"></td><td class=\"noborder\">active UP, going down </td>"
2475 "<td class=\"backup_going_down\"></td><td class=\"noborder\">backup UP, going down </td>"
2476 "</tr><tr>\n"
2477 "<td class=\"active_going_up\"></td><td class=\"noborder\">active DOWN, going up </td>"
2478 "<td class=\"backup_going_up\"></td><td class=\"noborder\">backup DOWN, going up </td>"
2479 "</tr><tr>\n"
2480 "<td class=\"active_down\"></td><td class=\"noborder\">active or backup DOWN &nbsp;</td>"
2481 "<td class=\"active_no_check\"></td><td class=\"noborder\">not checked </td>"
2482 "</tr><tr>\n"
2483 "<td class=\"maintain\"></td><td class=\"noborder\" colspan=\"3\">active or backup DOWN for maintenance (MAINT) &nbsp;</td>"
2484 "</tr><tr>\n"
2485 "<td class=\"active_draining\"></td><td class=\"noborder\" colspan=\"3\">active or backup SOFT STOPPED for maintenance &nbsp;</td>"
2486 "</tr></table>\n"
2487 "Note: \"NOLB\"/\"DRAIN\" = UP with load-balancing disabled."
2488 "</td>"
2489 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
2490 "<b>Display option:</b><ul style=\"margin-top: 0.25em;\">"
2491 "",
Willy Tarreau676c29e2019-10-09 10:50:01 +02002492 (appctx->ctx.stats.flags & STAT_HIDEVER) ? "" : (stats_version_string),
2493 pid, (appctx->ctx.stats.flags & STAT_SHNODE) ? " on " : "",
2494 (appctx->ctx.stats.flags & STAT_SHNODE) ? (uri->node ? uri->node : global.node) : "",
2495 (appctx->ctx.stats.flags & STAT_SHDESC) ? ": " : "",
2496 (appctx->ctx.stats.flags & STAT_SHDESC) ? (uri->desc ? uri->desc : global.desc) : "",
Yves Lafon95317282018-02-26 11:10:37 +01002497 pid, relative_pid, global.nbproc, global.nbthread,
William Lallemand74c24fb2016-11-21 17:18:36 +01002498 up / 86400, (up % 86400) / 3600,
2499 (up % 3600) / 60, (up % 60),
2500 global.rlimit_memmax ? ultoa(global.rlimit_memmax) : "unlimited",
2501 global.rlimit_memmax ? " MB" : "",
2502 global.rlimit_nofile,
2503 global.maxsock, global.maxconn, global.maxpipes,
2504 actconn, pipes_used, pipes_used+pipes_free, read_freq_ctr(&global.conn_per_sec),
Willy Tarreau1713c032019-05-23 12:23:55 +02002505 bps >= 1000000000UL ? (bps / 1000000000.0) : bps >= 1000000UL ? (bps / 1000000.0) : (bps / 1000.0),
2506 bps >= 1000000000UL ? 'G' : bps >= 1000000UL ? 'M' : 'k',
Willy Tarreau81036f22019-05-20 19:24:50 +02002507 tasks_run_queue_cur, nb_tasks_cur, ti->idle_pct
William Lallemand74c24fb2016-11-21 17:18:36 +01002508 );
2509
2510 /* scope_txt = search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
Christopher Fauleted7a0662019-01-14 11:07:34 +01002511 memcpy(scope_txt, scope_ptr, appctx->ctx.stats.scope_len);
William Lallemand74c24fb2016-11-21 17:18:36 +01002512 scope_txt[appctx->ctx.stats.scope_len] = '\0';
2513
2514 chunk_appendf(&trash,
2515 "<li><form method=\"GET\">Scope : <input value=\"%s\" name=\"" STAT_SCOPE_INPUT_NAME "\" size=\"8\" maxlength=\"%d\" tabindex=\"1\"/></form>\n",
2516 (appctx->ctx.stats.scope_len > 0) ? scope_txt : "",
2517 STAT_SCOPE_TXT_MAXLEN);
2518
2519 /* scope_txt = search pattern + search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
2520 scope_txt[0] = 0;
2521 if (appctx->ctx.stats.scope_len) {
2522 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Christopher Fauleted7a0662019-01-14 11:07:34 +01002523 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), scope_ptr, appctx->ctx.stats.scope_len);
William Lallemand74c24fb2016-11-21 17:18:36 +01002524 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
2525 }
2526
2527 if (appctx->ctx.stats.flags & STAT_HIDE_DOWN)
2528 chunk_appendf(&trash,
2529 "<li><a href=\"%s%s%s%s\">Show all servers</a><br>\n",
2530 uri->uri_prefix,
2531 "",
2532 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2533 scope_txt);
2534 else
2535 chunk_appendf(&trash,
2536 "<li><a href=\"%s%s%s%s\">Hide 'DOWN' servers</a><br>\n",
2537 uri->uri_prefix,
2538 ";up",
2539 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2540 scope_txt);
2541
2542 if (uri->refresh > 0) {
2543 if (appctx->ctx.stats.flags & STAT_NO_REFRESH)
2544 chunk_appendf(&trash,
2545 "<li><a href=\"%s%s%s%s\">Enable refresh</a><br>\n",
2546 uri->uri_prefix,
2547 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2548 "",
2549 scope_txt);
2550 else
2551 chunk_appendf(&trash,
2552 "<li><a href=\"%s%s%s%s\">Disable refresh</a><br>\n",
2553 uri->uri_prefix,
2554 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2555 ";norefresh",
2556 scope_txt);
2557 }
2558
2559 chunk_appendf(&trash,
2560 "<li><a href=\"%s%s%s%s\">Refresh now</a><br>\n",
2561 uri->uri_prefix,
2562 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2563 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2564 scope_txt);
2565
2566 chunk_appendf(&trash,
2567 "<li><a href=\"%s;csv%s%s\">CSV export</a><br>\n",
2568 uri->uri_prefix,
2569 (uri->refresh > 0) ? ";norefresh" : "",
2570 scope_txt);
2571
2572 chunk_appendf(&trash,
Christopher Faulet6338a082019-09-09 15:50:54 +02002573 "<li><a href=\"%s;json%s%s\">JSON export</a> (<a href=\"%s;json-schema\">schema</a>)<br>\n",
2574 uri->uri_prefix,
2575 (uri->refresh > 0) ? ";norefresh" : "",
2576 scope_txt, uri->uri_prefix);
2577
2578 chunk_appendf(&trash,
William Lallemand74c24fb2016-11-21 17:18:36 +01002579 "</ul></td>"
2580 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
2581 "<b>External resources:</b><ul style=\"margin-top: 0.25em;\">\n"
2582 "<li><a href=\"" PRODUCT_URL "\">Primary site</a><br>\n"
2583 "<li><a href=\"" PRODUCT_URL_UPD "\">Updates (v" PRODUCT_BRANCH ")</a><br>\n"
2584 "<li><a href=\"" PRODUCT_URL_DOC "\">Online manual</a><br>\n"
2585 "</ul>"
2586 "</td>"
2587 "</tr></table>\n"
2588 ""
2589 );
2590
2591 if (appctx->ctx.stats.st_code) {
2592 switch (appctx->ctx.stats.st_code) {
2593 case STAT_STATUS_DONE:
2594 chunk_appendf(&trash,
2595 "<p><div class=active_up>"
2596 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2597 "Action processed successfully."
2598 "</div>\n", uri->uri_prefix,
2599 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2600 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2601 scope_txt);
2602 break;
2603 case STAT_STATUS_NONE:
2604 chunk_appendf(&trash,
2605 "<p><div class=active_going_down>"
2606 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2607 "Nothing has changed."
2608 "</div>\n", uri->uri_prefix,
2609 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2610 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2611 scope_txt);
2612 break;
2613 case STAT_STATUS_PART:
2614 chunk_appendf(&trash,
2615 "<p><div class=active_going_down>"
2616 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2617 "Action partially processed.<br>"
2618 "Some server names are probably unknown or ambiguous (duplicated names in the backend)."
2619 "</div>\n", uri->uri_prefix,
2620 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2621 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2622 scope_txt);
2623 break;
2624 case STAT_STATUS_ERRP:
2625 chunk_appendf(&trash,
2626 "<p><div class=active_down>"
2627 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2628 "Action not processed because of invalid parameters."
2629 "<ul>"
2630 "<li>The action is maybe unknown.</li>"
Christopher Faulet2f9a41d2019-02-27 15:30:57 +01002631 "<li>Invalid key parameter (empty or too long).</li>"
William Lallemand74c24fb2016-11-21 17:18:36 +01002632 "<li>The backend name is probably unknown or ambiguous (duplicated names).</li>"
2633 "<li>Some server names are probably unknown or ambiguous (duplicated names in the backend).</li>"
2634 "</ul>"
2635 "</div>\n", uri->uri_prefix,
2636 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2637 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2638 scope_txt);
2639 break;
2640 case STAT_STATUS_EXCD:
2641 chunk_appendf(&trash,
2642 "<p><div class=active_down>"
2643 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2644 "<b>Action not processed : the buffer couldn't store all the data.<br>"
2645 "You should retry with less servers at a time.</b>"
2646 "</div>\n", uri->uri_prefix,
2647 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2648 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2649 scope_txt);
2650 break;
2651 case STAT_STATUS_DENY:
2652 chunk_appendf(&trash,
2653 "<p><div class=active_down>"
2654 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2655 "<b>Action denied.</b>"
2656 "</div>\n", uri->uri_prefix,
2657 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2658 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2659 scope_txt);
2660 break;
Christopher Faulet3c2ecf72019-02-27 16:41:27 +01002661 case STAT_STATUS_IVAL:
2662 chunk_appendf(&trash,
2663 "<p><div class=active_down>"
2664 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2665 "<b>Invalid requests (unsupported method or chunked encoded request).</b>"
2666 "</div>\n", uri->uri_prefix,
2667 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2668 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2669 scope_txt);
2670 break;
William Lallemand74c24fb2016-11-21 17:18:36 +01002671 default:
2672 chunk_appendf(&trash,
2673 "<p><div class=active_no_check>"
2674 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2675 "Unexpected result."
2676 "</div>\n", uri->uri_prefix,
2677 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2678 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2679 scope_txt);
2680 }
2681 chunk_appendf(&trash, "<p>\n");
2682 }
2683}
2684
2685/* Dumps the HTML stats trailer block to the trash. The caller is responsible
2686 * for clearing the trash if needed.
2687 */
2688static void stats_dump_html_end()
2689{
2690 chunk_appendf(&trash, "</body></html>\n");
2691}
2692
Simon Horman05ee2132017-01-04 09:37:25 +01002693/* Dumps the stats JSON header to the trash buffer which. The caller is responsible
2694 * for clearing it if needed.
2695 */
2696static void stats_dump_json_header()
2697{
2698 chunk_strcat(&trash, "[");
2699}
2700
2701
2702/* Dumps the JSON stats trailer block to the trash. The caller is responsible
2703 * for clearing the trash if needed.
2704 */
2705static void stats_dump_json_end()
2706{
2707 chunk_strcat(&trash, "]");
2708}
2709
William Lallemand74c24fb2016-11-21 17:18:36 +01002710/* This function dumps statistics onto the stream interface's read buffer in
2711 * either CSV or HTML format. <uri> contains some HTML-specific parameters that
2712 * are ignored for CSV format (hence <uri> may be NULL there). It returns 0 if
2713 * it had to stop writing data and an I/O is needed, 1 if the dump is finished
2714 * and the stream must be closed, or -1 in case of any error. This function is
2715 * used by both the CLI and the HTTP handlers.
2716 */
Christopher Fauletef779222018-10-31 08:47:01 +01002717static int stats_dump_stat_to_buffer(struct stream_interface *si, struct htx *htx,
2718 struct uri_auth *uri)
William Lallemand74c24fb2016-11-21 17:18:36 +01002719{
2720 struct appctx *appctx = __objt_appctx(si->end);
2721 struct channel *rep = si_ic(si);
2722 struct proxy *px;
2723
2724 chunk_reset(&trash);
2725
2726 switch (appctx->st2) {
2727 case STAT_ST_INIT:
2728 appctx->st2 = STAT_ST_HEAD; /* let's start producing data */
2729 /* fall through */
2730
2731 case STAT_ST_HEAD:
2732 if (appctx->ctx.stats.flags & STAT_FMT_HTML)
Willy Tarreau676c29e2019-10-09 10:50:01 +02002733 stats_dump_html_head(appctx, uri);
Christopher Faulet6338a082019-09-09 15:50:54 +02002734 else if (appctx->ctx.stats.flags & STAT_JSON_SCHM)
2735 stats_dump_json_schema(&trash);
Simon Horman05ee2132017-01-04 09:37:25 +01002736 else if (appctx->ctx.stats.flags & STAT_FMT_JSON)
Willy Tarreau9d7fb632017-04-11 07:53:04 +02002737 stats_dump_json_header();
William Lallemand74c24fb2016-11-21 17:18:36 +01002738 else if (!(appctx->ctx.stats.flags & STAT_FMT_TYPED))
2739 stats_dump_csv_header();
2740
Christopher Fauletef779222018-10-31 08:47:01 +01002741 if (!stats_putchk(rep, htx, &trash))
2742 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002743
Christopher Faulet6338a082019-09-09 15:50:54 +02002744 if (appctx->ctx.stats.flags & STAT_JSON_SCHM) {
2745 appctx->st2 = STAT_ST_FIN;
2746 return 1;
2747 }
William Lallemand74c24fb2016-11-21 17:18:36 +01002748 appctx->st2 = STAT_ST_INFO;
2749 /* fall through */
2750
2751 case STAT_ST_INFO:
2752 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
2753 stats_dump_html_info(si, uri);
Christopher Fauletef779222018-10-31 08:47:01 +01002754 if (!stats_putchk(rep, htx, &trash))
2755 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002756 }
2757
Olivier Houchardfbc74e82017-11-24 16:54:05 +01002758 appctx->ctx.stats.px = proxies_list;
William Lallemand74c24fb2016-11-21 17:18:36 +01002759 appctx->ctx.stats.px_st = STAT_PX_ST_INIT;
2760 appctx->st2 = STAT_ST_LIST;
2761 /* fall through */
2762
2763 case STAT_ST_LIST:
2764 /* dump proxies */
2765 while (appctx->ctx.stats.px) {
Christopher Fauletef779222018-10-31 08:47:01 +01002766 if (htx) {
2767 if (htx_almost_full(htx))
2768 goto full;
2769 }
2770 else {
2771 if (buffer_almost_full(&rep->buf))
2772 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002773 }
2774
2775 px = appctx->ctx.stats.px;
2776 /* skip the disabled proxies, global frontend and non-networked ones */
2777 if (px->state != PR_STSTOPPED && px->uuid > 0 && (px->cap & (PR_CAP_FE | PR_CAP_BE)))
Christopher Fauletef779222018-10-31 08:47:01 +01002778 if (stats_dump_proxy_to_buffer(si, htx, px, uri) == 0)
William Lallemand74c24fb2016-11-21 17:18:36 +01002779 return 0;
2780
2781 appctx->ctx.stats.px = px->next;
2782 appctx->ctx.stats.px_st = STAT_PX_ST_INIT;
2783 }
2784 /* here, we just have reached the last proxy */
2785
2786 appctx->st2 = STAT_ST_END;
2787 /* fall through */
2788
2789 case STAT_ST_END:
Simon Horman05ee2132017-01-04 09:37:25 +01002790 if (appctx->ctx.stats.flags & (STAT_FMT_HTML|STAT_FMT_JSON)) {
2791 if (appctx->ctx.stats.flags & STAT_FMT_HTML)
2792 stats_dump_html_end();
2793 else
2794 stats_dump_json_end();
Christopher Fauletef779222018-10-31 08:47:01 +01002795 if (!stats_putchk(rep, htx, &trash))
2796 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002797 }
2798
2799 appctx->st2 = STAT_ST_FIN;
2800 /* fall through */
2801
2802 case STAT_ST_FIN:
2803 return 1;
2804
2805 default:
2806 /* unknown state ! */
2807 appctx->st2 = STAT_ST_FIN;
2808 return -1;
2809 }
Christopher Fauletef779222018-10-31 08:47:01 +01002810
2811 full:
2812 si_rx_room_blk(si);
2813 return 0;
2814
William Lallemand74c24fb2016-11-21 17:18:36 +01002815}
2816
2817/* We reached the stats page through a POST request. The appctx is
2818 * expected to have already been allocated by the caller.
2819 * Parse the posted data and enable/disable servers if necessary.
2820 * Returns 1 if request was parsed or zero if it needs more data.
2821 */
2822static int stats_process_http_post(struct stream_interface *si)
2823{
2824 struct stream *s = si_strm(si);
2825 struct appctx *appctx = objt_appctx(si->end);
2826
2827 struct proxy *px = NULL;
2828 struct server *sv = NULL;
2829
2830 char key[LINESIZE];
2831 int action = ST_ADM_ACTION_NONE;
2832 int reprocess = 0;
2833
2834 int total_servers = 0;
2835 int altered_servers = 0;
2836
2837 char *first_param, *cur_param, *next_param, *end_params;
2838 char *st_cur_param = NULL;
2839 char *st_next_param = NULL;
2840
Christopher Fauleta3618372018-12-13 21:59:56 +01002841 struct buffer *temp = get_trash_chunk();
William Lallemand74c24fb2016-11-21 17:18:36 +01002842
Christopher Fauletb7f88902019-07-15 21:56:43 +02002843 struct htx *htx = htxbuf(&s->req.buf);
2844 struct htx_blk *blk;
Christopher Fauleta3618372018-12-13 21:59:56 +01002845
Christopher Fauletb7f88902019-07-15 21:56:43 +02002846 /* we need more data */
2847 if (s->txn->req.msg_state < HTTP_MSG_DONE) {
2848 /* check if we can receive more */
2849 if (htx_free_data_space(htx) <= global.tune.maxrewrite) {
2850 appctx->ctx.stats.st_code = STAT_STATUS_EXCD;
2851 goto out;
Christopher Fauleta3618372018-12-13 21:59:56 +01002852 }
Christopher Fauletb7f88902019-07-15 21:56:43 +02002853 goto wait;
2854 }
Christopher Fauleta3618372018-12-13 21:59:56 +01002855
Christopher Fauletb7f88902019-07-15 21:56:43 +02002856 /* The request was fully received. Copy data */
2857 blk = htx_get_head_blk(htx);
2858 while (blk) {
2859 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Fauleta3618372018-12-13 21:59:56 +01002860
Christopher Fauletb7f88902019-07-15 21:56:43 +02002861 if (type == HTX_BLK_EOM || type == HTX_BLK_TLR || type == HTX_BLK_EOT)
2862 break;
2863 if (type == HTX_BLK_DATA) {
2864 struct ist v = htx_get_blk_value(htx, blk);
Christopher Fauleta3618372018-12-13 21:59:56 +01002865
Christopher Fauletb7f88902019-07-15 21:56:43 +02002866 if (!chunk_memcat(temp, v.ptr, v.len)) {
Christopher Faulet2f9a41d2019-02-27 15:30:57 +01002867 appctx->ctx.stats.st_code = STAT_STATUS_EXCD;
2868 goto out;
2869 }
Christopher Fauleta3618372018-12-13 21:59:56 +01002870 }
Christopher Fauletb7f88902019-07-15 21:56:43 +02002871 blk = htx_get_next_blk(htx, blk);
William Lallemand74c24fb2016-11-21 17:18:36 +01002872 }
2873
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002874 first_param = temp->area;
Christopher Fauleta3618372018-12-13 21:59:56 +01002875 end_params = temp->area + temp->data;
William Lallemand74c24fb2016-11-21 17:18:36 +01002876 cur_param = next_param = end_params;
2877 *end_params = '\0';
2878
2879 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
2880
2881 /*
2882 * Parse the parameters in reverse order to only store the last value.
2883 * From the html form, the backend and the action are at the end.
2884 */
2885 while (cur_param > first_param) {
2886 char *value;
2887 int poffset, plen;
2888
2889 cur_param--;
2890
2891 if ((*cur_param == '&') || (cur_param == first_param)) {
2892 reprocess_servers:
2893 /* Parse the key */
2894 poffset = (cur_param != first_param ? 1 : 0);
2895 plen = next_param - cur_param + (cur_param == first_param ? 1 : 0);
2896 if ((plen > 0) && (plen <= sizeof(key))) {
2897 strncpy(key, cur_param + poffset, plen);
2898 key[plen - 1] = '\0';
2899 } else {
Christopher Faulet2f9a41d2019-02-27 15:30:57 +01002900 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
William Lallemand74c24fb2016-11-21 17:18:36 +01002901 goto out;
2902 }
2903
2904 /* Parse the value */
2905 value = key;
2906 while (*value != '\0' && *value != '=') {
2907 value++;
2908 }
2909 if (*value == '=') {
2910 /* Ok, a value is found, we can mark the end of the key */
2911 *value++ = '\0';
2912 }
Willy Tarreau62ba9ba2020-04-23 17:54:47 +02002913 if (url_decode(key, 1) < 0 || url_decode(value, 1) < 0)
William Lallemand74c24fb2016-11-21 17:18:36 +01002914 break;
2915
2916 /* Now we can check the key to see what to do */
2917 if (!px && (strcmp(key, "b") == 0)) {
2918 if ((px = proxy_be_by_name(value)) == NULL) {
2919 /* the backend name is unknown or ambiguous (duplicate names) */
2920 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
2921 goto out;
2922 }
2923 }
2924 else if (!action && (strcmp(key, "action") == 0)) {
2925 if (strcmp(value, "ready") == 0) {
2926 action = ST_ADM_ACTION_READY;
2927 }
2928 else if (strcmp(value, "drain") == 0) {
2929 action = ST_ADM_ACTION_DRAIN;
2930 }
2931 else if (strcmp(value, "maint") == 0) {
2932 action = ST_ADM_ACTION_MAINT;
2933 }
2934 else if (strcmp(value, "shutdown") == 0) {
2935 action = ST_ADM_ACTION_SHUTDOWN;
2936 }
2937 else if (strcmp(value, "dhlth") == 0) {
2938 action = ST_ADM_ACTION_DHLTH;
2939 }
2940 else if (strcmp(value, "ehlth") == 0) {
2941 action = ST_ADM_ACTION_EHLTH;
2942 }
2943 else if (strcmp(value, "hrunn") == 0) {
2944 action = ST_ADM_ACTION_HRUNN;
2945 }
2946 else if (strcmp(value, "hnolb") == 0) {
2947 action = ST_ADM_ACTION_HNOLB;
2948 }
2949 else if (strcmp(value, "hdown") == 0) {
2950 action = ST_ADM_ACTION_HDOWN;
2951 }
2952 else if (strcmp(value, "dagent") == 0) {
2953 action = ST_ADM_ACTION_DAGENT;
2954 }
2955 else if (strcmp(value, "eagent") == 0) {
2956 action = ST_ADM_ACTION_EAGENT;
2957 }
2958 else if (strcmp(value, "arunn") == 0) {
2959 action = ST_ADM_ACTION_ARUNN;
2960 }
2961 else if (strcmp(value, "adown") == 0) {
2962 action = ST_ADM_ACTION_ADOWN;
2963 }
2964 /* else these are the old supported methods */
2965 else if (strcmp(value, "disable") == 0) {
2966 action = ST_ADM_ACTION_DISABLE;
2967 }
2968 else if (strcmp(value, "enable") == 0) {
2969 action = ST_ADM_ACTION_ENABLE;
2970 }
2971 else if (strcmp(value, "stop") == 0) {
2972 action = ST_ADM_ACTION_STOP;
2973 }
2974 else if (strcmp(value, "start") == 0) {
2975 action = ST_ADM_ACTION_START;
2976 }
2977 else {
2978 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
2979 goto out;
2980 }
2981 }
2982 else if (strcmp(key, "s") == 0) {
2983 if (!(px && action)) {
2984 /*
2985 * Indicates that we'll need to reprocess the parameters
2986 * as soon as backend and action are known
2987 */
2988 if (!reprocess) {
2989 st_cur_param = cur_param;
2990 st_next_param = next_param;
2991 }
2992 reprocess = 1;
2993 }
2994 else if ((sv = findserver(px, value)) != NULL) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002995 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
William Lallemand74c24fb2016-11-21 17:18:36 +01002996 switch (action) {
2997 case ST_ADM_ACTION_DISABLE:
Emeric Brun52a91d32017-08-31 14:41:55 +02002998 if (!(sv->cur_admin & SRV_ADMF_FMAINT)) {
William Lallemand74c24fb2016-11-21 17:18:36 +01002999 altered_servers++;
3000 total_servers++;
3001 srv_set_admin_flag(sv, SRV_ADMF_FMAINT, "'disable' on stats page");
3002 }
3003 break;
3004 case ST_ADM_ACTION_ENABLE:
Emeric Brun52a91d32017-08-31 14:41:55 +02003005 if (sv->cur_admin & SRV_ADMF_FMAINT) {
William Lallemand74c24fb2016-11-21 17:18:36 +01003006 altered_servers++;
3007 total_servers++;
3008 srv_clr_admin_flag(sv, SRV_ADMF_FMAINT);
3009 }
3010 break;
3011 case ST_ADM_ACTION_STOP:
Emeric Brun52a91d32017-08-31 14:41:55 +02003012 if (!(sv->cur_admin & SRV_ADMF_FDRAIN)) {
William Lallemand74c24fb2016-11-21 17:18:36 +01003013 srv_set_admin_flag(sv, SRV_ADMF_FDRAIN, "'stop' on stats page");
3014 altered_servers++;
3015 total_servers++;
3016 }
3017 break;
3018 case ST_ADM_ACTION_START:
Emeric Brun52a91d32017-08-31 14:41:55 +02003019 if (sv->cur_admin & SRV_ADMF_FDRAIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +01003020 srv_clr_admin_flag(sv, SRV_ADMF_FDRAIN);
3021 altered_servers++;
3022 total_servers++;
3023 }
3024 break;
3025 case ST_ADM_ACTION_DHLTH:
3026 if (sv->check.state & CHK_ST_CONFIGURED) {
3027 sv->check.state &= ~CHK_ST_ENABLED;
3028 altered_servers++;
3029 total_servers++;
3030 }
3031 break;
3032 case ST_ADM_ACTION_EHLTH:
3033 if (sv->check.state & CHK_ST_CONFIGURED) {
3034 sv->check.state |= CHK_ST_ENABLED;
3035 altered_servers++;
3036 total_servers++;
3037 }
3038 break;
3039 case ST_ADM_ACTION_HRUNN:
3040 if (!(sv->track)) {
3041 sv->check.health = sv->check.rise + sv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02003042 srv_set_running(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01003043 altered_servers++;
3044 total_servers++;
3045 }
3046 break;
3047 case ST_ADM_ACTION_HNOLB:
3048 if (!(sv->track)) {
3049 sv->check.health = sv->check.rise + sv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02003050 srv_set_stopping(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01003051 altered_servers++;
3052 total_servers++;
3053 }
3054 break;
3055 case ST_ADM_ACTION_HDOWN:
3056 if (!(sv->track)) {
3057 sv->check.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02003058 srv_set_stopped(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01003059 altered_servers++;
3060 total_servers++;
3061 }
3062 break;
3063 case ST_ADM_ACTION_DAGENT:
3064 if (sv->agent.state & CHK_ST_CONFIGURED) {
3065 sv->agent.state &= ~CHK_ST_ENABLED;
3066 altered_servers++;
3067 total_servers++;
3068 }
3069 break;
3070 case ST_ADM_ACTION_EAGENT:
3071 if (sv->agent.state & CHK_ST_CONFIGURED) {
3072 sv->agent.state |= CHK_ST_ENABLED;
3073 altered_servers++;
3074 total_servers++;
3075 }
3076 break;
3077 case ST_ADM_ACTION_ARUNN:
3078 if (sv->agent.state & CHK_ST_ENABLED) {
3079 sv->agent.health = sv->agent.rise + sv->agent.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02003080 srv_set_running(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01003081 altered_servers++;
3082 total_servers++;
3083 }
3084 break;
3085 case ST_ADM_ACTION_ADOWN:
3086 if (sv->agent.state & CHK_ST_ENABLED) {
3087 sv->agent.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02003088 srv_set_stopped(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01003089 altered_servers++;
3090 total_servers++;
3091 }
3092 break;
3093 case ST_ADM_ACTION_READY:
3094 srv_adm_set_ready(sv);
3095 altered_servers++;
3096 total_servers++;
3097 break;
3098 case ST_ADM_ACTION_DRAIN:
3099 srv_adm_set_drain(sv);
3100 altered_servers++;
3101 total_servers++;
3102 break;
3103 case ST_ADM_ACTION_MAINT:
3104 srv_adm_set_maint(sv);
3105 altered_servers++;
3106 total_servers++;
3107 break;
3108 case ST_ADM_ACTION_SHUTDOWN:
3109 if (px->state != PR_STSTOPPED) {
Willy Tarreauaf7ea812019-11-14 16:42:04 +01003110 srv_shutdown_streams(sv, SF_ERR_KILLED);
William Lallemand74c24fb2016-11-21 17:18:36 +01003111 altered_servers++;
3112 total_servers++;
3113 }
3114 break;
3115 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003116 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
William Lallemand74c24fb2016-11-21 17:18:36 +01003117 } else {
3118 /* the server name is unknown or ambiguous (duplicate names) */
3119 total_servers++;
3120 }
3121 }
3122 if (reprocess && px && action) {
3123 /* Now, we know the backend and the action chosen by the user.
3124 * We can safely restart from the first server parameter
3125 * to reprocess them
3126 */
3127 cur_param = st_cur_param;
3128 next_param = st_next_param;
3129 reprocess = 0;
3130 goto reprocess_servers;
3131 }
3132
3133 next_param = cur_param;
3134 }
3135 }
3136
3137 if (total_servers == 0) {
3138 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
3139 }
3140 else if (altered_servers == 0) {
3141 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
3142 }
3143 else if (altered_servers == total_servers) {
3144 appctx->ctx.stats.st_code = STAT_STATUS_DONE;
3145 }
3146 else {
3147 appctx->ctx.stats.st_code = STAT_STATUS_PART;
3148 }
3149 out:
3150 return 1;
Christopher Faulet2f9a41d2019-02-27 15:30:57 +01003151 wait:
3152 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
3153 return 0;
Christopher Fauletef779222018-10-31 08:47:01 +01003154}
3155
3156
Christopher Fauletb7f88902019-07-15 21:56:43 +02003157static int stats_send_http_headers(struct stream_interface *si, struct htx *htx)
Christopher Fauletef779222018-10-31 08:47:01 +01003158{
3159 struct stream *s = si_strm(si);
3160 struct uri_auth *uri = s->be->uri_auth;
3161 struct appctx *appctx = __objt_appctx(si->end);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003162 struct htx_sl *sl;
3163 unsigned int flags;
Christopher Fauletef779222018-10-31 08:47:01 +01003164
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003165 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);
3166 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), ist("200"), ist("OK"));
3167 if (!sl)
Christopher Fauletef779222018-10-31 08:47:01 +01003168 goto full;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003169 sl->info.res.status = 200;
Christopher Fauletef779222018-10-31 08:47:01 +01003170
Christopher Fauletb829f4c2019-03-29 16:13:55 +01003171 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")))
Christopher Fauletef779222018-10-31 08:47:01 +01003172 goto full;
3173 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
3174 if (!htx_add_header(htx, ist("Content-Type"), ist("text/html")))
3175 goto full;
3176 }
Christopher Faulet6338a082019-09-09 15:50:54 +02003177 else if (appctx->ctx.stats.flags & (STAT_FMT_JSON|STAT_JSON_SCHM)) {
3178 if (!htx_add_header(htx, ist("Content-Type"), ist("application/json")))
3179 goto full;
3180 }
Christopher Fauletef779222018-10-31 08:47:01 +01003181 else {
3182 if (!htx_add_header(htx, ist("Content-Type"), ist("text/plain")))
3183 goto full;
3184 }
3185
3186 if (uri->refresh > 0 && !(appctx->ctx.stats.flags & STAT_NO_REFRESH)) {
3187 const char *refresh = U2A(uri->refresh);
3188 if (!htx_add_header(htx, ist("Refresh"), ist2(refresh, strlen(refresh))))
3189 goto full;
3190 }
3191
3192 if (appctx->ctx.stats.flags & STAT_CHUNKED) {
3193 if (!htx_add_header(htx, ist("Transfer-Encoding"), ist("chunked")))
3194 goto full;
3195 }
3196
3197 if (!htx_add_endof(htx, HTX_BLK_EOH))
3198 goto full;
3199
Christopher Faulet1e2d6362019-02-27 16:28:48 +01003200 channel_add_input(&s->res, htx->data);
Christopher Fauletef779222018-10-31 08:47:01 +01003201 return 1;
3202
3203 full:
3204 htx_reset(htx);
3205 si_rx_room_blk(si);
3206 return 0;
William Lallemand74c24fb2016-11-21 17:18:36 +01003207}
3208
Christopher Fauletef779222018-10-31 08:47:01 +01003209
Christopher Fauletb7f88902019-07-15 21:56:43 +02003210static int stats_send_http_redirect(struct stream_interface *si, struct htx *htx)
Christopher Fauletef779222018-10-31 08:47:01 +01003211{
3212 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
3213 struct stream *s = si_strm(si);
3214 struct uri_auth *uri = s->be->uri_auth;
3215 struct appctx *appctx = __objt_appctx(si->end);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003216 struct htx_sl *sl;
3217 unsigned int flags;
Christopher Fauletef779222018-10-31 08:47:01 +01003218
3219 /* scope_txt = search pattern + search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
3220 scope_txt[0] = 0;
3221 if (appctx->ctx.stats.scope_len) {
Christopher Fauleted7a0662019-01-14 11:07:34 +01003222 const char *scope_ptr = stats_scope_ptr(appctx, si);
3223
Christopher Fauletef779222018-10-31 08:47:01 +01003224 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Christopher Fauleted7a0662019-01-14 11:07:34 +01003225 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), scope_ptr, appctx->ctx.stats.scope_len);
Christopher Fauletef779222018-10-31 08:47:01 +01003226 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
3227 }
3228
3229 /* We don't want to land on the posted stats page because a refresh will
3230 * repost the data. We don't want this to happen on accident so we redirect
3231 * the browse to the stats page with a GET.
3232 */
3233 chunk_printf(&trash, "%s;st=%s%s%s%s",
3234 uri->uri_prefix,
3235 ((appctx->ctx.stats.st_code > STAT_STATUS_INIT) &&
3236 (appctx->ctx.stats.st_code < STAT_STATUS_SIZE) &&
3237 stat_status_codes[appctx->ctx.stats.st_code]) ?
3238 stat_status_codes[appctx->ctx.stats.st_code] :
3239 stat_status_codes[STAT_STATUS_UNKN],
3240 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3241 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
3242 scope_txt);
3243
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003244 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_CHNK);
3245 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), ist("303"), ist("See Other"));
3246 if (!sl)
Christopher Fauletef779222018-10-31 08:47:01 +01003247 goto full;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003248 sl->info.res.status = 303;
Christopher Fauletef779222018-10-31 08:47:01 +01003249
3250 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")) ||
Christopher Fauletef779222018-10-31 08:47:01 +01003251 !htx_add_header(htx, ist("Content-Type"), ist("text/plain")) ||
3252 !htx_add_header(htx, ist("Content-Length"), ist("0")) ||
3253 !htx_add_header(htx, ist("Location"), ist2(trash.area, trash.data)))
3254 goto full;
3255
3256 if (!htx_add_endof(htx, HTX_BLK_EOH))
3257 goto full;
3258
Christopher Faulet1e2d6362019-02-27 16:28:48 +01003259 channel_add_input(&s->res, htx->data);
Christopher Fauletef779222018-10-31 08:47:01 +01003260 return 1;
3261
3262full:
3263 htx_reset(htx);
3264 si_rx_room_blk(si);
3265 return 0;
3266}
William Lallemand74c24fb2016-11-21 17:18:36 +01003267
Simon Horman05ee2132017-01-04 09:37:25 +01003268
William Lallemand74c24fb2016-11-21 17:18:36 +01003269/* This I/O handler runs as an applet embedded in a stream interface. It is
3270 * used to send HTTP stats over a TCP socket. The mechanism is very simple.
3271 * appctx->st0 contains the operation in progress (dump, done). The handler
3272 * automatically unregisters itself once transfer is complete.
3273 */
Christopher Fauletb7f88902019-07-15 21:56:43 +02003274static void http_stats_io_handler(struct appctx *appctx)
Christopher Fauletef779222018-10-31 08:47:01 +01003275{
3276 struct stream_interface *si = appctx->owner;
3277 struct stream *s = si_strm(si);
3278 struct channel *req = si_oc(si);
3279 struct channel *res = si_ic(si);
3280 struct htx *req_htx, *res_htx;
3281
3282 res_htx = htx_from_buf(&res->buf);
3283
3284 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
3285 goto out;
3286
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05003287 /* Check if the input buffer is available. */
Christopher Fauletef779222018-10-31 08:47:01 +01003288 if (!b_size(&res->buf)) {
3289 si_rx_room_blk(si);
3290 goto out;
3291 }
3292
3293 /* check that the output is not closed */
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003294 if (res->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_SHUTR))
3295 appctx->st0 = STAT_HTTP_END;
Christopher Fauletef779222018-10-31 08:47:01 +01003296
3297 /* all states are processed in sequence */
3298 if (appctx->st0 == STAT_HTTP_HEAD) {
Christopher Fauletb7f88902019-07-15 21:56:43 +02003299 if (stats_send_http_headers(si, res_htx)) {
Christopher Fauletef779222018-10-31 08:47:01 +01003300 if (s->txn->meth == HTTP_METH_HEAD)
3301 appctx->st0 = STAT_HTTP_DONE;
3302 else
3303 appctx->st0 = STAT_HTTP_DUMP;
3304 }
3305 }
3306
3307 if (appctx->st0 == STAT_HTTP_DUMP) {
3308 if (stats_dump_stat_to_buffer(si, res_htx, s->be->uri_auth))
3309 appctx->st0 = STAT_HTTP_DONE;
3310 }
3311
3312 if (appctx->st0 == STAT_HTTP_POST) {
3313 if (stats_process_http_post(si))
3314 appctx->st0 = STAT_HTTP_LAST;
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003315 else if (req->flags & CF_SHUTR)
Christopher Fauletef779222018-10-31 08:47:01 +01003316 appctx->st0 = STAT_HTTP_DONE;
3317 }
3318
3319 if (appctx->st0 == STAT_HTTP_LAST) {
Christopher Fauletb7f88902019-07-15 21:56:43 +02003320 if (stats_send_http_redirect(si, res_htx))
Christopher Fauletef779222018-10-31 08:47:01 +01003321 appctx->st0 = STAT_HTTP_DONE;
3322 }
3323
3324 if (appctx->st0 == STAT_HTTP_DONE) {
Christopher Faulet54b5e212019-06-04 10:08:28 +02003325 /* Don't add TLR because mux-h1 will take care of it */
Christopher Fauletef779222018-10-31 08:47:01 +01003326 if (!htx_add_endof(res_htx, HTX_BLK_EOM)) {
3327 si_rx_room_blk(si);
3328 goto out;
3329 }
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003330 channel_add_input(&s->res, 1);
3331 appctx->st0 = STAT_HTTP_END;
Christopher Fauletef779222018-10-31 08:47:01 +01003332 }
3333
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003334 if (appctx->st0 == STAT_HTTP_END) {
3335 if (!(res->flags & CF_SHUTR)) {
Christopher Fauletef779222018-10-31 08:47:01 +01003336 res->flags |= CF_READ_NULL;
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003337 si_shutr(si);
3338 }
3339
3340 /* eat the whole request */
3341 if (co_data(req)) {
3342 req_htx = htx_from_buf(&req->buf);
3343 co_htx_skip(req, req_htx, co_data(req));
3344 htx_to_buf(req_htx, &req->buf);
Christopher Fauletef779222018-10-31 08:47:01 +01003345 }
3346 }
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003347
Christopher Fauletef779222018-10-31 08:47:01 +01003348 out:
3349 /* we have left the request in the buffer for the case where we
3350 * process a POST, and this automatically re-enables activity on
3351 * read. It's better to indicate that we want to stop reading when
3352 * we're sending, so that we know there's at most one direction
3353 * deciding to wake the applet up. It saves it from looping when
3354 * emitting large blocks into small TCP windows.
3355 */
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003356 htx_to_buf(res_htx, &res->buf);
3357 if (!channel_is_empty(res))
Christopher Fauletef779222018-10-31 08:47:01 +01003358 si_stop_get(si);
Christopher Fauletef779222018-10-31 08:47:01 +01003359}
3360
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003361/* Dump all fields from <info> into <out> using the "show info" format (name: value) */
Willy Tarreau83061a82018-07-13 11:56:34 +02003362static int stats_dump_info_fields(struct buffer *out,
Willy Tarreau43241ff2019-10-09 11:27:51 +02003363 const struct field *info, unsigned int flags)
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003364{
3365 int field;
3366
3367 for (field = 0; field < INF_TOTAL_FIELDS; field++) {
3368 if (!field_format(info, field))
3369 continue;
3370
Willy Tarreaueaa55372019-10-09 07:39:11 +02003371 if (!chunk_appendf(out, "%s: ", info_fields[field].name))
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003372 return 0;
3373 if (!stats_emit_raw_data_field(out, &info[field]))
3374 return 0;
Willy Tarreau6b19b142019-10-09 15:44:21 +02003375 if ((flags & STAT_SHOW_FDESC) && !chunk_appendf(out, ":\"%s\"", info_fields[field].desc))
3376 return 0;
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003377 if (!chunk_strcat(out, "\n"))
3378 return 0;
3379 }
3380 return 1;
3381}
3382
3383/* Dump all fields from <info> into <out> using the "show info typed" format */
Willy Tarreau83061a82018-07-13 11:56:34 +02003384static int stats_dump_typed_info_fields(struct buffer *out,
Willy Tarreau43241ff2019-10-09 11:27:51 +02003385 const struct field *info, unsigned int flags)
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003386{
3387 int field;
3388
3389 for (field = 0; field < INF_TOTAL_FIELDS; field++) {
3390 if (!field_format(info, field))
3391 continue;
3392
Willy Tarreaueaa55372019-10-09 07:39:11 +02003393 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 +01003394 return 0;
3395 if (!stats_emit_field_tags(out, &info[field], ':'))
3396 return 0;
3397 if (!stats_emit_typed_data_field(out, &info[field]))
3398 return 0;
Willy Tarreau6b19b142019-10-09 15:44:21 +02003399 if ((flags & STAT_SHOW_FDESC) && !chunk_appendf(out, ":\"%s\"", info_fields[field].desc))
3400 return 0;
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003401 if (!chunk_strcat(out, "\n"))
3402 return 0;
3403 }
3404 return 1;
3405}
3406
3407/* Fill <info> with HAProxy global info. <info> is preallocated
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05003408 * array of length <len>. The length of the array must be
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003409 * INF_TOTAL_FIELDS. If this length is less then this value, the
3410 * function returns 0, otherwise, it returns 1.
3411 */
3412int stats_fill_info(struct field *info, int len)
3413{
3414 unsigned int up = (now.tv_sec - start_date.tv_sec);
Willy Tarreau83061a82018-07-13 11:56:34 +02003415 struct buffer *out = get_trash_chunk();
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003416
3417#ifdef USE_OPENSSL
3418 int ssl_sess_rate = read_freq_ctr(&global.ssl_per_sec);
3419 int ssl_key_rate = read_freq_ctr(&global.ssl_fe_keys_per_sec);
3420 int ssl_reuse = 0;
3421
3422 if (ssl_key_rate < ssl_sess_rate) {
3423 /* count the ssl reuse ratio and avoid overflows in both directions */
3424 ssl_reuse = 100 - (100 * ssl_key_rate + (ssl_sess_rate - 1) / 2) / ssl_sess_rate;
3425 }
3426#endif
3427
3428 if (len < INF_TOTAL_FIELDS)
3429 return 0;
3430
3431 chunk_reset(out);
3432 memset(info, 0, sizeof(*info) * len);
3433
3434 info[INF_NAME] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, PRODUCT_NAME);
Willy Tarreau909b9d82019-01-04 18:20:32 +01003435 info[INF_VERSION] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, haproxy_version);
3436 info[INF_RELEASE_DATE] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, haproxy_date);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003437
Yves Lafon95317282018-02-26 11:10:37 +01003438 info[INF_NBTHREAD] = mkf_u32(FO_CONFIG|FS_SERVICE, global.nbthread);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003439 info[INF_NBPROC] = mkf_u32(FO_CONFIG|FS_SERVICE, global.nbproc);
3440 info[INF_PROCESS_NUM] = mkf_u32(FO_KEY, relative_pid);
3441 info[INF_PID] = mkf_u32(FO_STATUS, pid);
3442
3443 info[INF_UPTIME] = mkf_str(FN_DURATION, chunk_newstr(out));
3444 chunk_appendf(out, "%ud %uh%02um%02us", up / 86400, (up % 86400) / 3600, (up % 3600) / 60, (up % 60));
3445
3446 info[INF_UPTIME_SEC] = mkf_u32(FN_DURATION, up);
3447 info[INF_MEMMAX_MB] = mkf_u32(FO_CONFIG|FN_LIMIT, global.rlimit_memmax);
3448 info[INF_POOL_ALLOC_MB] = mkf_u32(0, (unsigned)(pool_total_allocated() / 1048576L));
3449 info[INF_POOL_USED_MB] = mkf_u32(0, (unsigned)(pool_total_used() / 1048576L));
3450 info[INF_POOL_FAILED] = mkf_u32(FN_COUNTER, pool_total_failures());
3451 info[INF_ULIMIT_N] = mkf_u32(FO_CONFIG|FN_LIMIT, global.rlimit_nofile);
3452 info[INF_MAXSOCK] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxsock);
3453 info[INF_MAXCONN] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxconn);
3454 info[INF_HARD_MAXCONN] = mkf_u32(FO_CONFIG|FN_LIMIT, global.hardmaxconn);
3455 info[INF_CURR_CONN] = mkf_u32(0, actconn);
3456 info[INF_CUM_CONN] = mkf_u32(FN_COUNTER, totalconn);
3457 info[INF_CUM_REQ] = mkf_u32(FN_COUNTER, global.req_count);
3458#ifdef USE_OPENSSL
3459 info[INF_MAX_SSL_CONNS] = mkf_u32(FN_MAX, global.maxsslconn);
3460 info[INF_CURR_SSL_CONNS] = mkf_u32(0, sslconns);
3461 info[INF_CUM_SSL_CONNS] = mkf_u32(FN_COUNTER, totalsslconns);
3462#endif
3463 info[INF_MAXPIPES] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxpipes);
3464 info[INF_PIPES_USED] = mkf_u32(0, pipes_used);
3465 info[INF_PIPES_FREE] = mkf_u32(0, pipes_free);
3466 info[INF_CONN_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&global.conn_per_sec));
3467 info[INF_CONN_RATE_LIMIT] = mkf_u32(FO_CONFIG|FN_LIMIT, global.cps_lim);
3468 info[INF_MAX_CONN_RATE] = mkf_u32(FN_MAX, global.cps_max);
3469 info[INF_SESS_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&global.sess_per_sec));
3470 info[INF_SESS_RATE_LIMIT] = mkf_u32(FO_CONFIG|FN_LIMIT, global.sps_lim);
3471 info[INF_MAX_SESS_RATE] = mkf_u32(FN_RATE, global.sps_max);
3472
3473#ifdef USE_OPENSSL
3474 info[INF_SSL_RATE] = mkf_u32(FN_RATE, ssl_sess_rate);
3475 info[INF_SSL_RATE_LIMIT] = mkf_u32(FO_CONFIG|FN_LIMIT, global.ssl_lim);
3476 info[INF_MAX_SSL_RATE] = mkf_u32(FN_MAX, global.ssl_max);
3477 info[INF_SSL_FRONTEND_KEY_RATE] = mkf_u32(0, ssl_key_rate);
3478 info[INF_SSL_FRONTEND_MAX_KEY_RATE] = mkf_u32(FN_MAX, global.ssl_fe_keys_max);
3479 info[INF_SSL_FRONTEND_SESSION_REUSE_PCT] = mkf_u32(0, ssl_reuse);
3480 info[INF_SSL_BACKEND_KEY_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&global.ssl_be_keys_per_sec));
3481 info[INF_SSL_BACKEND_MAX_KEY_RATE] = mkf_u32(FN_MAX, global.ssl_be_keys_max);
3482 info[INF_SSL_CACHE_LOOKUPS] = mkf_u32(FN_COUNTER, global.shctx_lookups);
3483 info[INF_SSL_CACHE_MISSES] = mkf_u32(FN_COUNTER, global.shctx_misses);
3484#endif
3485 info[INF_COMPRESS_BPS_IN] = mkf_u32(FN_RATE, read_freq_ctr(&global.comp_bps_in));
3486 info[INF_COMPRESS_BPS_OUT] = mkf_u32(FN_RATE, read_freq_ctr(&global.comp_bps_out));
3487 info[INF_COMPRESS_BPS_RATE_LIM] = mkf_u32(FO_CONFIG|FN_LIMIT, global.comp_rate_lim);
3488#ifdef USE_ZLIB
3489 info[INF_ZLIB_MEM_USAGE] = mkf_u32(0, zlib_used_memory);
3490 info[INF_MAX_ZLIB_MEM_USAGE] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxzlibmem);
3491#endif
3492 info[INF_TASKS] = mkf_u32(0, nb_tasks_cur);
Christopher Faulet34c5cc92016-12-06 09:15:30 +01003493 info[INF_RUN_QUEUE] = mkf_u32(0, tasks_run_queue_cur);
Willy Tarreau81036f22019-05-20 19:24:50 +02003494 info[INF_IDLE_PCT] = mkf_u32(FN_AVG, ti->idle_pct);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003495 info[INF_NODE] = mkf_str(FO_CONFIG|FN_OUTPUT|FS_SERVICE, global.node);
3496 if (global.desc)
3497 info[INF_DESCRIPTION] = mkf_str(FO_CONFIG|FN_OUTPUT|FS_SERVICE, global.desc);
Willy Tarreau00098ea2018-11-05 14:38:13 +01003498 info[INF_STOPPING] = mkf_u32(0, stopping);
3499 info[INF_JOBS] = mkf_u32(0, jobs);
William Lallemanda7199262018-11-16 16:57:20 +01003500 info[INF_UNSTOPPABLE_JOBS] = mkf_u32(0, unstoppable_jobs);
Willy Tarreau00098ea2018-11-05 14:38:13 +01003501 info[INF_LISTENERS] = mkf_u32(0, listeners);
Willy Tarreau199ad242018-11-05 16:31:22 +01003502 info[INF_ACTIVE_PEERS] = mkf_u32(0, active_peers);
Willy Tarreau2d372c22018-11-05 17:12:27 +01003503 info[INF_CONNECTED_PEERS] = mkf_u32(0, connected_peers);
Willy Tarreau13ef7732018-11-12 07:25:28 +01003504 info[INF_DROPPED_LOGS] = mkf_u32(0, dropped_logs);
Willy Tarreaubeb859a2018-11-22 18:07:59 +01003505 info[INF_BUSY_POLLING] = mkf_u32(0, !!(global.tune.options & GTUNE_BUSY_POLLING));
Baptiste Assmann333939c2019-01-21 08:34:50 +01003506 info[INF_FAILED_RESOLUTIONS] = mkf_u32(0, dns_failed_resolutions);
Willy Tarreau7cf0e452019-05-23 11:39:14 +02003507 info[INF_TOTAL_BYTES_OUT] = mkf_u64(0, global.out_bytes);
3508 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 +02003509 info[INF_DEBUG_COMMANDS_ISSUED] = mkf_u32(0, debug_commands_issued);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003510
3511 return 1;
3512}
3513
3514/* This function dumps information onto the stream interface's read buffer.
3515 * It returns 0 as long as it does not complete, non-zero upon completion.
3516 * No state is used.
3517 */
3518static int stats_dump_info_to_buffer(struct stream_interface *si)
3519{
3520 struct appctx *appctx = __objt_appctx(si->end);
3521
3522 if (!stats_fill_info(info, INF_TOTAL_FIELDS))
3523 return 0;
3524
3525 chunk_reset(&trash);
3526
3527 if (appctx->ctx.stats.flags & STAT_FMT_TYPED)
Willy Tarreau43241ff2019-10-09 11:27:51 +02003528 stats_dump_typed_info_fields(&trash, info, appctx->ctx.stats.flags);
Simon Horman05ee2132017-01-04 09:37:25 +01003529 else if (appctx->ctx.stats.flags & STAT_FMT_JSON)
Willy Tarreau43241ff2019-10-09 11:27:51 +02003530 stats_dump_json_info_fields(&trash, info, appctx->ctx.stats.flags);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003531 else
Willy Tarreau43241ff2019-10-09 11:27:51 +02003532 stats_dump_info_fields(&trash, info, appctx->ctx.stats.flags);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003533
Willy Tarreau06d80a92017-10-19 14:32:15 +02003534 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01003535 si_rx_room_blk(si);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003536 return 0;
3537 }
3538
3539 return 1;
3540}
3541
Simon Horman6f6bb382017-01-04 09:37:26 +01003542/* This function dumps the schema onto the stream interface's read buffer.
3543 * It returns 0 as long as it does not complete, non-zero upon completion.
3544 * No state is used.
3545 *
3546 * Integer values bouned to the range [-(2**53)+1, (2**53)-1] as
3547 * per the recommendation for interoperable integers in section 6 of RFC 7159.
3548 */
Willy Tarreau83061a82018-07-13 11:56:34 +02003549static void stats_dump_json_schema(struct buffer *out)
Simon Horman6f6bb382017-01-04 09:37:26 +01003550{
3551
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003552 int old_len = out->data;
Simon Horman6f6bb382017-01-04 09:37:26 +01003553
3554 chunk_strcat(out,
3555 "{"
3556 "\"$schema\":\"http://json-schema.org/draft-04/schema#\","
3557 "\"oneOf\":["
3558 "{"
3559 "\"title\":\"Info\","
3560 "\"type\":\"array\","
3561 "\"items\":{"
3562 "\"properties\":{"
3563 "\"title\":\"InfoItem\","
3564 "\"type\":\"object\","
3565 "\"field\":{\"$ref\":\"#/definitions/field\"},"
3566 "\"processNum\":{\"$ref\":\"#/definitions/processNum\"},"
3567 "\"tags\":{\"$ref\":\"#/definitions/tags\"},"
3568 "\"value\":{\"$ref\":\"#/definitions/typedValue\"}"
3569 "},"
3570 "\"required\":[\"field\",\"processNum\",\"tags\","
3571 "\"value\"]"
3572 "}"
3573 "},"
3574 "{"
3575 "\"title\":\"Stat\","
3576 "\"type\":\"array\","
3577 "\"items\":{"
3578 "\"title\":\"InfoItem\","
3579 "\"type\":\"object\","
3580 "\"properties\":{"
3581 "\"objType\":{"
3582 "\"enum\":[\"Frontend\",\"Backend\",\"Listener\","
3583 "\"Server\",\"Unknown\"]"
3584 "},"
3585 "\"proxyId\":{"
3586 "\"type\":\"integer\","
3587 "\"minimum\":0"
3588 "},"
3589 "\"id\":{"
3590 "\"type\":\"integer\","
3591 "\"minimum\":0"
3592 "},"
3593 "\"field\":{\"$ref\":\"#/definitions/field\"},"
3594 "\"processNum\":{\"$ref\":\"#/definitions/processNum\"},"
3595 "\"tags\":{\"$ref\":\"#/definitions/tags\"},"
3596 "\"typedValue\":{\"$ref\":\"#/definitions/typedValue\"}"
3597 "},"
3598 "\"required\":[\"objType\",\"proxyId\",\"id\","
3599 "\"field\",\"processNum\",\"tags\","
3600 "\"value\"]"
3601 "}"
3602 "},"
3603 "{"
3604 "\"title\":\"Error\","
3605 "\"type\":\"object\","
3606 "\"properties\":{"
3607 "\"errorStr\":{"
3608 "\"type\":\"string\""
3609 "},"
3610 "\"required\":[\"errorStr\"]"
3611 "}"
3612 "}"
3613 "],"
3614 "\"definitions\":{"
3615 "\"field\":{"
3616 "\"type\":\"object\","
3617 "\"pos\":{"
3618 "\"type\":\"integer\","
3619 "\"minimum\":0"
3620 "},"
3621 "\"name\":{"
3622 "\"type\":\"string\""
3623 "},"
3624 "\"required\":[\"pos\",\"name\"]"
3625 "},"
3626 "\"processNum\":{"
3627 "\"type\":\"integer\","
3628 "\"minimum\":1"
3629 "},"
3630 "\"tags\":{"
3631 "\"type\":\"object\","
3632 "\"origin\":{"
3633 "\"type\":\"string\","
3634 "\"enum\":[\"Metric\",\"Status\",\"Key\","
3635 "\"Config\",\"Product\",\"Unknown\"]"
3636 "},"
3637 "\"nature\":{"
3638 "\"type\":\"string\","
3639 "\"enum\":[\"Gauge\",\"Limit\",\"Min\",\"Max\","
3640 "\"Rate\",\"Counter\",\"Duration\","
3641 "\"Age\",\"Time\",\"Name\",\"Output\","
3642 "\"Avg\", \"Unknown\"]"
3643 "},"
3644 "\"scope\":{"
3645 "\"type\":\"string\","
3646 "\"enum\":[\"Cluster\",\"Process\",\"Service\","
3647 "\"System\",\"Unknown\"]"
3648 "},"
3649 "\"required\":[\"origin\",\"nature\",\"scope\"]"
3650 "},"
3651 "\"typedValue\":{"
3652 "\"type\":\"object\","
3653 "\"oneOf\":["
3654 "{\"$ref\":\"#/definitions/typedValue/definitions/s32Value\"},"
3655 "{\"$ref\":\"#/definitions/typedValue/definitions/s64Value\"},"
3656 "{\"$ref\":\"#/definitions/typedValue/definitions/u32Value\"},"
3657 "{\"$ref\":\"#/definitions/typedValue/definitions/u64Value\"},"
3658 "{\"$ref\":\"#/definitions/typedValue/definitions/strValue\"}"
3659 "],"
3660 "\"definitions\":{"
3661 "\"s32Value\":{"
3662 "\"properties\":{"
3663 "\"type\":{"
3664 "\"type\":\"string\","
3665 "\"enum\":[\"s32\"]"
3666 "},"
3667 "\"value\":{"
3668 "\"type\":\"integer\","
3669 "\"minimum\":-2147483648,"
3670 "\"maximum\":2147483647"
3671 "}"
3672 "},"
3673 "\"required\":[\"type\",\"value\"]"
3674 "},"
3675 "\"s64Value\":{"
3676 "\"properties\":{"
3677 "\"type\":{"
3678 "\"type\":\"string\","
3679 "\"enum\":[\"s64\"]"
3680 "},"
3681 "\"value\":{"
3682 "\"type\":\"integer\","
3683 "\"minimum\":-9007199254740991,"
3684 "\"maximum\":9007199254740991"
3685 "}"
3686 "},"
3687 "\"required\":[\"type\",\"value\"]"
3688 "},"
3689 "\"u32Value\":{"
3690 "\"properties\":{"
3691 "\"type\":{"
3692 "\"type\":\"string\","
3693 "\"enum\":[\"u32\"]"
3694 "},"
3695 "\"value\":{"
3696 "\"type\":\"integer\","
3697 "\"minimum\":0,"
3698 "\"maximum\":4294967295"
3699 "}"
3700 "},"
3701 "\"required\":[\"type\",\"value\"]"
3702 "},"
3703 "\"u64Value\":{"
3704 "\"properties\":{"
3705 "\"type\":{"
3706 "\"type\":\"string\","
3707 "\"enum\":[\"u64\"]"
3708 "},"
3709 "\"value\":{"
3710 "\"type\":\"integer\","
3711 "\"minimum\":0,"
3712 "\"maximum\":9007199254740991"
3713 "}"
3714 "},"
3715 "\"required\":[\"type\",\"value\"]"
3716 "},"
3717 "\"strValue\":{"
3718 "\"properties\":{"
3719 "\"type\":{"
3720 "\"type\":\"string\","
3721 "\"enum\":[\"str\"]"
3722 "},"
3723 "\"value\":{\"type\":\"string\"}"
3724 "},"
3725 "\"required\":[\"type\",\"value\"]"
3726 "},"
3727 "\"unknownValue\":{"
3728 "\"properties\":{"
3729 "\"type\":{"
3730 "\"type\":\"integer\","
3731 "\"minimum\":0"
3732 "},"
3733 "\"value\":{"
3734 "\"type\":\"string\","
3735 "\"enum\":[\"unknown\"]"
3736 "}"
3737 "},"
3738 "\"required\":[\"type\",\"value\"]"
3739 "}"
3740 "}"
3741 "}"
3742 "}"
3743 "}");
3744
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003745 if (old_len == out->data) {
Simon Horman6f6bb382017-01-04 09:37:26 +01003746 chunk_reset(out);
3747 chunk_appendf(out,
3748 "{\"errorStr\":\"output buffer too short\"}");
3749 }
3750}
3751
3752/* This function dumps the schema onto the stream interface's read buffer.
3753 * It returns 0 as long as it does not complete, non-zero upon completion.
3754 * No state is used.
3755 */
3756static int stats_dump_json_schema_to_buffer(struct stream_interface *si)
3757{
3758 chunk_reset(&trash);
3759
3760 stats_dump_json_schema(&trash);
3761
Willy Tarreau06d80a92017-10-19 14:32:15 +02003762 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01003763 si_rx_room_blk(si);
Simon Horman6f6bb382017-01-04 09:37:26 +01003764 return 0;
3765 }
3766
3767 return 1;
3768}
3769
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02003770static int cli_parse_clear_counters(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau89d467c2016-11-23 11:02:40 +01003771{
3772 struct proxy *px;
3773 struct server *sv;
3774 struct listener *li;
3775 int clrall = 0;
3776
3777 if (strcmp(args[2], "all") == 0)
3778 clrall = 1;
3779
3780 /* check permissions */
3781 if (!cli_has_level(appctx, ACCESS_LVL_OPER) ||
3782 (clrall && !cli_has_level(appctx, ACCESS_LVL_ADMIN)))
3783 return 1;
3784
Olivier Houchardfbc74e82017-11-24 16:54:05 +01003785 for (px = proxies_list; px; px = px->next) {
Willy Tarreau89d467c2016-11-23 11:02:40 +01003786 if (clrall) {
3787 memset(&px->be_counters, 0, sizeof(px->be_counters));
3788 memset(&px->fe_counters, 0, sizeof(px->fe_counters));
3789 }
3790 else {
3791 px->be_counters.conn_max = 0;
3792 px->be_counters.p.http.rps_max = 0;
3793 px->be_counters.sps_max = 0;
3794 px->be_counters.cps_max = 0;
3795 px->be_counters.nbpend_max = 0;
Christopher Fauletefb41f02019-11-08 14:53:15 +01003796 px->be_counters.qtime_max = 0;
3797 px->be_counters.ctime_max = 0;
3798 px->be_counters.dtime_max = 0;
3799 px->be_counters.ttime_max = 0;
Willy Tarreau89d467c2016-11-23 11:02:40 +01003800
3801 px->fe_counters.conn_max = 0;
3802 px->fe_counters.p.http.rps_max = 0;
3803 px->fe_counters.sps_max = 0;
3804 px->fe_counters.cps_max = 0;
Willy Tarreau89d467c2016-11-23 11:02:40 +01003805 }
3806
3807 for (sv = px->srv; sv; sv = sv->next)
3808 if (clrall)
3809 memset(&sv->counters, 0, sizeof(sv->counters));
3810 else {
3811 sv->counters.cur_sess_max = 0;
3812 sv->counters.nbpend_max = 0;
3813 sv->counters.sps_max = 0;
Christopher Fauletefb41f02019-11-08 14:53:15 +01003814 sv->counters.qtime_max = 0;
3815 sv->counters.ctime_max = 0;
3816 sv->counters.dtime_max = 0;
3817 sv->counters.ttime_max = 0;
Willy Tarreau89d467c2016-11-23 11:02:40 +01003818 }
3819
3820 list_for_each_entry(li, &px->conf.listeners, by_fe)
3821 if (li->counters) {
3822 if (clrall)
3823 memset(li->counters, 0, sizeof(*li->counters));
3824 else
3825 li->counters->conn_max = 0;
3826 }
3827 }
3828
3829 global.cps_max = 0;
3830 global.sps_max = 0;
Olivier Houchard00bc3cb2017-10-17 19:23:25 +02003831 global.ssl_max = 0;
3832 global.ssl_fe_keys_max = 0;
3833 global.ssl_be_keys_max = 0;
Willy Tarreaud80cb4e2018-01-20 19:30:13 +01003834
3835 memset(activity, 0, sizeof(activity));
Willy Tarreau89d467c2016-11-23 11:02:40 +01003836 return 1;
3837}
3838
3839
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02003840static int cli_parse_show_info(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003841{
Willy Tarreau2f397382019-10-09 11:43:59 +02003842 int arg = 2;
3843
Willy Tarreaud25fc792016-12-16 12:33:47 +01003844 appctx->ctx.stats.scope_str = 0;
3845 appctx->ctx.stats.scope_len = 0;
3846 appctx->ctx.stats.flags = 0;
3847
Willy Tarreau2f397382019-10-09 11:43:59 +02003848 while (*args[arg]) {
3849 if (strcmp(args[arg], "typed") == 0)
3850 appctx->ctx.stats.flags = (appctx->ctx.stats.flags & ~STAT_FMT_MASK) | STAT_FMT_TYPED;
3851 else if (strcmp(args[arg], "json") == 0)
3852 appctx->ctx.stats.flags = (appctx->ctx.stats.flags & ~STAT_FMT_MASK) | STAT_FMT_JSON;
3853 else if (strcmp(args[arg], "desc") == 0)
3854 appctx->ctx.stats.flags |= STAT_SHOW_FDESC;
3855 arg++;
3856 }
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003857 return 0;
3858}
3859
3860
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02003861static int cli_parse_show_stat(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau2b812e22016-11-22 16:18:05 +01003862{
Willy Tarreau2f397382019-10-09 11:43:59 +02003863 int arg = 2;
3864
Willy Tarreaud25fc792016-12-16 12:33:47 +01003865 appctx->ctx.stats.scope_str = 0;
3866 appctx->ctx.stats.scope_len = 0;
Willy Tarreau578d6e42019-10-09 11:00:22 +02003867 appctx->ctx.stats.flags = STAT_SHNODE | STAT_SHDESC;
3868
3869 if ((strm_li(si_strm(appctx->owner))->bind_conf->level & ACCESS_LVL_MASK) >= ACCESS_LVL_OPER)
3870 appctx->ctx.stats.flags |= STAT_SHLGNDS;
Willy Tarreaud25fc792016-12-16 12:33:47 +01003871
Willy Tarreau2f397382019-10-09 11:43:59 +02003872 if (*args[arg] && *args[arg+1] && *args[arg+2]) {
Willy Tarreaua1b1ed52016-11-25 08:50:58 +01003873 struct proxy *px;
3874
Willy Tarreau2f397382019-10-09 11:43:59 +02003875 px = proxy_find_by_name(args[arg], 0, 0);
Willy Tarreaua1b1ed52016-11-25 08:50:58 +01003876 if (px)
3877 appctx->ctx.stats.iid = px->uuid;
3878 else
Willy Tarreau2f397382019-10-09 11:43:59 +02003879 appctx->ctx.stats.iid = atoi(args[arg]);
Willy Tarreaua1b1ed52016-11-25 08:50:58 +01003880
Willy Tarreau9d008692019-08-09 11:21:01 +02003881 if (!appctx->ctx.stats.iid)
3882 return cli_err(appctx, "No such proxy.\n");
Willy Tarreaua1b1ed52016-11-25 08:50:58 +01003883
Willy Tarreau2b812e22016-11-22 16:18:05 +01003884 appctx->ctx.stats.flags |= STAT_BOUND;
Willy Tarreau2f397382019-10-09 11:43:59 +02003885 appctx->ctx.stats.type = atoi(args[arg+1]);
3886 appctx->ctx.stats.sid = atoi(args[arg+2]);
3887 arg += 3;
3888 }
3889
3890 while (*args[arg]) {
3891 if (strcmp(args[arg], "typed") == 0)
3892 appctx->ctx.stats.flags = (appctx->ctx.stats.flags & ~STAT_FMT_MASK) | STAT_FMT_TYPED;
3893 else if (strcmp(args[arg], "json") == 0)
3894 appctx->ctx.stats.flags = (appctx->ctx.stats.flags & ~STAT_FMT_MASK) | STAT_FMT_JSON;
3895 else if (strcmp(args[arg], "desc") == 0)
3896 appctx->ctx.stats.flags |= STAT_SHOW_FDESC;
3897 arg++;
Willy Tarreau2b812e22016-11-22 16:18:05 +01003898 }
Willy Tarreau2b812e22016-11-22 16:18:05 +01003899
Willy Tarreau2b812e22016-11-22 16:18:05 +01003900 return 0;
3901}
3902
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003903static int cli_io_handler_dump_info(struct appctx *appctx)
3904{
3905 return stats_dump_info_to_buffer(appctx->owner);
3906}
3907
Willy Tarreau2b812e22016-11-22 16:18:05 +01003908/* This I/O handler runs as an applet embedded in a stream interface. It is
3909 * used to send raw stats over a socket.
3910 */
3911static int cli_io_handler_dump_stat(struct appctx *appctx)
3912{
Christopher Fauletef779222018-10-31 08:47:01 +01003913 return stats_dump_stat_to_buffer(appctx->owner, NULL, NULL);
Willy Tarreau2b812e22016-11-22 16:18:05 +01003914}
3915
Simon Horman6f6bb382017-01-04 09:37:26 +01003916static int cli_io_handler_dump_json_schema(struct appctx *appctx)
3917{
3918 return stats_dump_json_schema_to_buffer(appctx->owner);
3919}
3920
Willy Tarreau2b812e22016-11-22 16:18:05 +01003921/* register cli keywords */
3922static struct cli_kw_list cli_kws = {{ },{
Willy Tarreau89d467c2016-11-23 11:02:40 +01003923 { { "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 +02003924 { { "show", "info", NULL }, "show info : report information about the running process [desc|json|typed]*", cli_parse_show_info, cli_io_handler_dump_info, NULL },
3925 { { "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 +01003926 { { "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 +01003927 {{},}
3928}};
3929
Willy Tarreau0108d902018-11-25 19:14:37 +01003930INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
3931
William Lallemand74c24fb2016-11-21 17:18:36 +01003932struct applet http_stats_applet = {
3933 .obj_type = OBJ_TYPE_APPLET,
3934 .name = "<STATS>", /* used for logging */
3935 .fct = http_stats_io_handler,
3936 .release = NULL,
3937};
3938
William Lallemand74c24fb2016-11-21 17:18:36 +01003939/*
3940 * Local variables:
3941 * c-indent-level: 8
3942 * c-basic-offset: 8
3943 * End:
3944 */