blob: ae2f58d2b73ce25d37cda247308c7a59ce65d7a8 [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 Tarreaub2551052020-06-09 09:07:15 +020027#include <haproxy/api.h>
Willy Tarreau3f0f82e2020-06-04 19:42:41 +020028#include <haproxy/applet-t.h>
Willy Tarreau49801602020-06-04 22:50:02 +020029#include <haproxy/backend.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020030#include <haproxy/base64.h>
Willy Tarreau6be78492020-06-05 00:00:29 +020031#include <haproxy/cfgparse.h>
Willy Tarreauf1d32c42020-06-04 21:07:02 +020032#include <haproxy/channel.h>
Willy Tarreau4aa573d2020-06-04 18:21:56 +020033#include <haproxy/check.h>
Willy Tarreau83487a82020-06-04 20:19:54 +020034#include <haproxy/cli.h>
Willy Tarreau0a3bd392020-06-04 08:52:38 +020035#include <haproxy/compression.h>
Willy Tarreau2a83d602020-05-27 16:58:08 +020036#include <haproxy/debug.h>
Willy Tarreaueb92deb2020-06-04 10:53:16 +020037#include <haproxy/dns.h>
Willy Tarreau36979d92020-06-05 17:27:29 +020038#include <haproxy/errors.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020039#include <haproxy/fd.h>
40#include <haproxy/freq_ctr.h>
Willy Tarreau762d7a52020-06-04 11:23:07 +020041#include <haproxy/frontend.h>
Willy Tarreauf268ee82020-06-04 17:05:57 +020042#include <haproxy/global.h>
Willy Tarreaucd72d8c2020-06-02 19:11:26 +020043#include <haproxy/http.h>
Willy Tarreau87735332020-06-04 09:08:41 +020044#include <haproxy/http_htx.h>
Willy Tarreau16f958c2020-06-03 08:44:35 +020045#include <haproxy/htx.h>
Willy Tarreau853b2972020-05-27 18:01:47 +020046#include <haproxy/list.h>
Willy Tarreau36979d92020-06-05 17:27:29 +020047#include <haproxy/listener.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020048#include <haproxy/log.h>
Willy Tarreau2cd58092020-06-04 15:10:43 +020049#include <haproxy/map-t.h>
Willy Tarreau225a90a2020-06-04 15:06:28 +020050#include <haproxy/pattern-t.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020051#include <haproxy/pipe.h>
52#include <haproxy/pool.h>
Willy Tarreaua264d962020-06-04 22:29:18 +020053#include <haproxy/proxy.h>
Willy Tarreau1e56f922020-06-04 23:20:13 +020054#include <haproxy/server.h>
Willy Tarreau48d25b32020-06-04 18:58:52 +020055#include <haproxy/session.h>
Willy Tarreau209108d2020-06-04 20:30:20 +020056#include <haproxy/ssl_sock.h>
Willy Tarreau2eec9b52020-06-04 19:58:55 +020057#include <haproxy/stats.h>
Willy Tarreaudfd3de82020-06-04 23:46:14 +020058#include <haproxy/stream.h>
Willy Tarreau5e539c92020-06-04 20:45:39 +020059#include <haproxy/stream_interface.h>
Willy Tarreaucea0e1b2020-06-04 17:25:40 +020060#include <haproxy/task.h>
Willy Tarreauc2f7c582020-06-02 18:15:32 +020061#include <haproxy/ticks.h>
Willy Tarreau92b4f132020-06-01 11:05:15 +020062#include <haproxy/time.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020063#include <haproxy/tools.h>
Willy Tarreau8c42b8a2020-06-04 19:27:34 +020064#include <haproxy/uri_auth-t.h>
Willy Tarreaud6788052020-05-27 15:59:00 +020065#include <haproxy/version.h>
William Lallemand74c24fb2016-11-21 17:18:36 +010066
William Lallemand74c24fb2016-11-21 17:18:36 +010067
Christopher Faulet2b9b6782019-02-27 16:42:58 +010068/* status codes available for the stats admin page (strictly 4 chars length) */
69const char *stat_status_codes[STAT_STATUS_SIZE] = {
70 [STAT_STATUS_DENY] = "DENY",
71 [STAT_STATUS_DONE] = "DONE",
72 [STAT_STATUS_ERRP] = "ERRP",
73 [STAT_STATUS_EXCD] = "EXCD",
74 [STAT_STATUS_NONE] = "NONE",
75 [STAT_STATUS_PART] = "PART",
76 [STAT_STATUS_UNKN] = "UNKN",
77 [STAT_STATUS_IVAL] = "IVAL",
78};
79
Willy Tarreau0baac8c2016-11-22 16:36:53 +010080/* These are the field names for each INF_* field position. Please pay attention
81 * to always use the exact same name except that the strings for new names must
82 * be lower case or CamelCase while the enum entries must be upper case.
83 */
Willy Tarreaueaa55372019-10-09 07:39:11 +020084const struct name_desc info_fields[INF_TOTAL_FIELDS] = {
Willy Tarreau6d4897e2019-10-11 16:31:46 +020085 [INF_NAME] = { .name = "Name", .desc = "Product name" },
86 [INF_VERSION] = { .name = "Version", .desc = "Product version" },
87 [INF_RELEASE_DATE] = { .name = "Release_date", .desc = "Date of latest source code update" },
88 [INF_NBTHREAD] = { .name = "Nbthread", .desc = "Number of started threads (global.nbthread)" },
89 [INF_NBPROC] = { .name = "Nbproc", .desc = "Number of started worker processes (global.nbproc)" },
90 [INF_PROCESS_NUM] = { .name = "Process_num", .desc = "Relative worker process number (1..Nbproc)" },
91 [INF_PID] = { .name = "Pid", .desc = "This worker process identifier for the system" },
92 [INF_UPTIME] = { .name = "Uptime", .desc = "How long ago this worker process was started (days+hours+minutes+seconds)" },
93 [INF_UPTIME_SEC] = { .name = "Uptime_sec", .desc = "How long ago this worker process was started (seconds)" },
94 [INF_MEMMAX_MB] = { .name = "Memmax_MB", .desc = "Worker process's hard limit on memory usage in MB (-m on command line)" },
95 [INF_POOL_ALLOC_MB] = { .name = "PoolAlloc_MB", .desc = "Amount of memory allocated in pools (in MB)" },
96 [INF_POOL_USED_MB] = { .name = "PoolUsed_MB", .desc = "Amount of pool memory currently used (in MB)" },
97 [INF_POOL_FAILED] = { .name = "PoolFailed", .desc = "Number of failed pool allocations since this worker was started" },
98 [INF_ULIMIT_N] = { .name = "Ulimit-n", .desc = "Hard limit on the number of per-process file descriptors" },
99 [INF_MAXSOCK] = { .name = "Maxsock", .desc = "Hard limit on the number of per-process sockets" },
100 [INF_MAXCONN] = { .name = "Maxconn", .desc = "Hard limit on the number of per-process connections (configured or imposed by Ulimit-n)" },
101 [INF_HARD_MAXCONN] = { .name = "Hard_maxconn", .desc = "Hard limit on the number of per-process connections (imposed by Memmax_MB or Ulimit-n)" },
102 [INF_CURR_CONN] = { .name = "CurrConns", .desc = "Current number of connections on this worker process" },
103 [INF_CUM_CONN] = { .name = "CumConns", .desc = "Total number of connections on this worker process since started" },
104 [INF_CUM_REQ] = { .name = "CumReq", .desc = "Total number of requests on this worker process since started" },
105 [INF_MAX_SSL_CONNS] = { .name = "MaxSslConns", .desc = "Hard limit on the number of per-process SSL endpoints (front+back), 0=unlimited" },
106 [INF_CURR_SSL_CONNS] = { .name = "CurrSslConns", .desc = "Current number of SSL endpoints on this worker process (front+back)" },
107 [INF_CUM_SSL_CONNS] = { .name = "CumSslConns", .desc = "Total number of SSL endpoints on this worker process since started (front+back)" },
108 [INF_MAXPIPES] = { .name = "Maxpipes", .desc = "Hard limit on the number of pipes for splicing, 0=unlimited" },
109 [INF_PIPES_USED] = { .name = "PipesUsed", .desc = "Current number of pipes in use in this worker process" },
110 [INF_PIPES_FREE] = { .name = "PipesFree", .desc = "Current number of allocated and available pipes in this worker process" },
111 [INF_CONN_RATE] = { .name = "ConnRate", .desc = "Number of front connections created on this worker process over the last second" },
112 [INF_CONN_RATE_LIMIT] = { .name = "ConnRateLimit", .desc = "Hard limit for ConnRate (global.maxconnrate)" },
113 [INF_MAX_CONN_RATE] = { .name = "MaxConnRate", .desc = "Highest ConnRate reached on this worker process since started (in connections per second)" },
114 [INF_SESS_RATE] = { .name = "SessRate", .desc = "Number of sessions created on this worker process over the last second" },
115 [INF_SESS_RATE_LIMIT] = { .name = "SessRateLimit", .desc = "Hard limit for SessRate (global.maxsessrate)" },
116 [INF_MAX_SESS_RATE] = { .name = "MaxSessRate", .desc = "Highest SessRate reached on this worker process since started (in sessions per second)" },
117 [INF_SSL_RATE] = { .name = "SslRate", .desc = "Number of SSL connections created on this worker process over the last second" },
118 [INF_SSL_RATE_LIMIT] = { .name = "SslRateLimit", .desc = "Hard limit for SslRate (global.maxsslrate)" },
119 [INF_MAX_SSL_RATE] = { .name = "MaxSslRate", .desc = "Highest SslRate reached on this worker process since started (in connections per second)" },
120 [INF_SSL_FRONTEND_KEY_RATE] = { .name = "SslFrontendKeyRate", .desc = "Number of SSL keys created on frontends in this worker process over the last second" },
121 [INF_SSL_FRONTEND_MAX_KEY_RATE] = { .name = "SslFrontendMaxKeyRate", .desc = "Highest SslFrontendKeyRate reached on this worker process since started (in SSL keys per second)" },
122 [INF_SSL_FRONTEND_SESSION_REUSE_PCT] = { .name = "SslFrontendSessionReuse_pct", .desc = "Percent of frontend SSL connections which did not require a new key" },
123 [INF_SSL_BACKEND_KEY_RATE] = { .name = "SslBackendKeyRate", .desc = "Number of SSL keys created on backends in this worker process over the last second" },
124 [INF_SSL_BACKEND_MAX_KEY_RATE] = { .name = "SslBackendMaxKeyRate", .desc = "Highest SslBackendKeyRate reached on this worker process since started (in SSL keys per second)" },
125 [INF_SSL_CACHE_LOOKUPS] = { .name = "SslCacheLookups", .desc = "Total number of SSL session ID lookups in the SSL session cache on this worker since started" },
126 [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" },
127 [INF_COMPRESS_BPS_IN] = { .name = "CompressBpsIn", .desc = "Number of bytes submitted to the HTTP compressor in this worker process over the last second" },
128 [INF_COMPRESS_BPS_OUT] = { .name = "CompressBpsOut", .desc = "Number of bytes emitted by the HTTP compressor in this worker process over the last second" },
129 [INF_COMPRESS_BPS_RATE_LIM] = { .name = "CompressBpsRateLim", .desc = "Limit of CompressBpsOut beyond which HTTP compression is automatically disabled" },
130 [INF_ZLIB_MEM_USAGE] = { .name = "ZlibMemUsage", .desc = "Amount of memory currently used by HTTP compression on the current worker process (in bytes)" },
131 [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)" },
132 [INF_TASKS] = { .name = "Tasks", .desc = "Total number of tasks in the current worker process (active + sleeping)" },
133 [INF_RUN_QUEUE] = { .name = "Run_queue", .desc = "Total number of active tasks+tasklets in the current worker process" },
134 [INF_IDLE_PCT] = { .name = "Idle_pct", .desc = "Percentage of last second spent waiting in the current worker thread" },
135 [INF_NODE] = { .name = "node", .desc = "Node name (global.node)" },
136 [INF_DESCRIPTION] = { .name = "description", .desc = "Node description (global.description)" },
137 [INF_STOPPING] = { .name = "Stopping", .desc = "1 if the worker process is currently stopping, otherwise zero" },
138 [INF_JOBS] = { .name = "Jobs", .desc = "Current number of active jobs on the current worker process (frontend connections, master connections, listeners)" },
139 [INF_UNSTOPPABLE_JOBS] = { .name = "Unstoppable Jobs", .desc = "Current number of unstoppable jobs on the current worker process (master connections)" },
140 [INF_LISTENERS] = { .name = "Listeners", .desc = "Current number of active listeners on the current worker process" },
141 [INF_ACTIVE_PEERS] = { .name = "ActivePeers", .desc = "Current number of verified active peers connections on the current worker process" },
142 [INF_CONNECTED_PEERS] = { .name = "ConnectedPeers", .desc = "Current number of peers having passed the connection step on the current worker process" },
143 [INF_DROPPED_LOGS] = { .name = "DroppedLogs", .desc = "Total number of dropped logs for current worker process since started" },
144 [INF_BUSY_POLLING] = { .name = "BusyPolling", .desc = "1 if busy-polling is currently in use on the worker process, otherwise zero (config.busy-polling)" },
145 [INF_FAILED_RESOLUTIONS] = { .name = "FailedResolutions", .desc = "Total number of failed DNS resolutions in current worker process since started" },
146 [INF_TOTAL_BYTES_OUT] = { .name = "TotalBytesOut", .desc = "Total number of bytes emitted by current worker process since started" },
Christopher Fauletaaa70852020-07-10 13:56:30 +0200147 [INF_TOTAL_SPLICED_BYTES_OUT] = { .name = "TotalSplicdedBytesOut", .desc = "Total number of bytes emitted by current worker process through a kernel pipe since started" },
Willy Tarreau6d4897e2019-10-11 16:31:46 +0200148 [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 +0200149 [INF_DEBUG_COMMANDS_ISSUED] = { .name = "DebugCommandsIssued", .desc = "Number of debug commands issued on this process (anything > 0 is unsafe)" },
Emeric Brun45c457a2020-07-09 23:23:34 +0200150 [INF_CUM_LOG_MSGS] = { .name = "CumRecvLogs", .desc = "Total number of log messages received by log-forwarding listeners on this worker process since started" },
Willy Tarreau0baac8c2016-11-22 16:36:53 +0100151};
152
Willy Tarreaueaa55372019-10-09 07:39:11 +0200153const struct name_desc stat_fields[ST_F_TOTAL_FIELDS] = {
Willy Tarreau6d4897e2019-10-11 16:31:46 +0200154 [ST_F_PXNAME] = { .name = "pxname", .desc = "Proxy name" },
155 [ST_F_SVNAME] = { .name = "svname", .desc = "Server name" },
156 [ST_F_QCUR] = { .name = "qcur", .desc = "Current number of connections waiting in the server of backend queue" },
157 [ST_F_QMAX] = { .name = "qmax", .desc = "Highest value of qcur encountered since process started" },
158 [ST_F_SCUR] = { .name = "scur", .desc = "Current number of sessions on the frontend, backend or server" },
159 [ST_F_SMAX] = { .name = "smax", .desc = "Highest value of scur encountered since process started" },
160 [ST_F_SLIM] = { .name = "slim", .desc = "Frontend/listener/server's maxconn, backend's fullconn" },
161 [ST_F_STOT] = { .name = "stot", .desc = "Total number of sessions since process started" },
162 [ST_F_BIN] = { .name = "bin", .desc = "Total number of request bytes since process started" },
163 [ST_F_BOUT] = { .name = "bout", .desc = "Total number of response bytes since process started" },
164 [ST_F_DREQ] = { .name = "dreq", .desc = "Total number of denied requests since process started" },
165 [ST_F_DRESP] = { .name = "dresp", .desc = "Total number of denied responses since process started" },
166 [ST_F_EREQ] = { .name = "ereq", .desc = "Total number of invalid requests since process started" },
167 [ST_F_ECON] = { .name = "econ", .desc = "Total number of failed connections to server since the worker process started" },
168 [ST_F_ERESP] = { .name = "eresp", .desc = "Total number of invalid responses since the worker process started" },
169 [ST_F_WRETR] = { .name = "wretr", .desc = "Total number of server connection retries since the worker process started" },
170 [ST_F_WREDIS] = { .name = "wredis", .desc = "Total number of server redispatches due to connection failures since the worker process started" },
171 [ST_F_STATUS] = { .name = "status", .desc = "Frontend/listen status: OPEN/WAITING/FULL/STOP; backend: UP/DOWN; server: last check status" },
172 [ST_F_WEIGHT] = { .name = "weight", .desc = "Server weight, or sum of active servers' weights for a backend" },
173 [ST_F_ACT] = { .name = "act", .desc = "Total number of active UP servers with a non-zero weight" },
174 [ST_F_BCK] = { .name = "bck", .desc = "Total number of backup UP servers with a non-zero weight" },
175 [ST_F_CHKFAIL] = { .name = "chkfail", .desc = "Total number of failed individual health checks per server/backend, since the worker process started" },
176 [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" },
177 [ST_F_LASTCHG] = { .name = "lastchg", .desc = "How long ago the last server state changed, in seconds" },
178 [ST_F_DOWNTIME] = { .name = "downtime", .desc = "Total time spent in DOWN state, for server or backend" },
179 [ST_F_QLIMIT] = { .name = "qlimit", .desc = "Limit on the number of connections in queue, for servers only (maxqueue argument)" },
180 [ST_F_PID] = { .name = "pid", .desc = "Relative worker process number (1..nbproc)" },
181 [ST_F_IID] = { .name = "iid", .desc = "Frontend or Backend numeric identifier ('id' setting)" },
182 [ST_F_SID] = { .name = "sid", .desc = "Server numeric identifier ('id' setting)" },
183 [ST_F_THROTTLE] = { .name = "throttle", .desc = "Throttling ratio applied to a server's maxconn and weight during the slowstart period (0 to 100%)" },
184 [ST_F_LBTOT] = { .name = "lbtot", .desc = "Total number of requests routed by load balancing since the worker process started (ignores queue pop and stickiness)" },
185 [ST_F_TRACKED] = { .name = "tracked", .desc = "Name of the other server this server tracks for its state" },
186 [ST_F_TYPE] = { .name = "type", .desc = "Type of the object (Listener, Frontend, Backend, Server)" },
187 [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)" },
188 [ST_F_RATE_LIM] = { .name = "rate_lim", .desc = "Limit on the number of sessions accepted in a second (frontend only, 'rate-limit sessions' setting)" },
189 [ST_F_RATE_MAX] = { .name = "rate_max", .desc = "Highest value of 'rate' observed since the worker process started" },
190 [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" },
191 [ST_F_CHECK_CODE] = { .name = "check_code", .desc = "HTTP/SMTP/LDAP status code reported by the latest server health check" },
192 [ST_F_CHECK_DURATION] = { .name = "check_duration", .desc = "Total duration of the latest server health check, in milliseconds" },
193 [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" },
194 [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" },
195 [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" },
196 [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" },
197 [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" },
198 [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)" },
199 [ST_F_HANAFAIL] = { .name = "hanafail", .desc = "Total number of failed checks caused by an 'on-error' directive after an 'observe' condition matched" },
200 [ST_F_REQ_RATE] = { .name = "req_rate", .desc = "Number of HTTP requests processed over the last second on this object" },
201 [ST_F_REQ_RATE_MAX] = { .name = "req_rate_max", .desc = "Highest value of 'req_rate' observed since the worker process started" },
202 [ST_F_REQ_TOT] = { .name = "req_tot", .desc = "Total number of HTTP requests processed by this object since the worker process started" },
203 [ST_F_CLI_ABRT] = { .name = "cli_abrt", .desc = "Total number of requests or connections aborted by the client since the worker process started" },
204 [ST_F_SRV_ABRT] = { .name = "srv_abrt", .desc = "Total number of requests or connections aborted by the server since the worker process started" },
205 [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" },
206 [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" },
207 [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)" },
208 [ST_F_COMP_RSP] = { .name = "comp_rsp", .desc = "Total number of HTTP responses that were compressed for this object since the worker process started" },
209 [ST_F_LASTSESS] = { .name = "lastsess", .desc = "How long ago some traffic was seen on this object on this worker process, in seconds" },
210 [ST_F_LAST_CHK] = { .name = "last_chk", .desc = "Short description of the latest health check report for this server (see also check_desc)" },
211 [ST_F_LAST_AGT] = { .name = "last_agt", .desc = "Short description of the latest agent check report for this server (see also agent_desc)" },
212 [ST_F_QTIME] = { .name = "qtime", .desc = "Time spent in the queue, in milliseconds, averaged over the 1024 last requests (backend/server)" },
213 [ST_F_CTIME] = { .name = "ctime", .desc = "Time spent waiting for a connection to complete, in milliseconds, averaged over the 1024 last requests (backend/server)" },
214 [ST_F_RTIME] = { .name = "rtime", .desc = "Time spent waiting for a server response, in milliseconds, averaged over the 1024 last requests (backend/server)" },
215 [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)" },
216 [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" },
217 [ST_F_AGENT_CODE] = { .name = "agent_code", .desc = "Status code reported by the latest server agent check" },
218 [ST_F_AGENT_DURATION] = { .name = "agent_duration", .desc = "Total duration of the latest server agent check, in milliseconds" },
219 [ST_F_CHECK_DESC] = { .name = "check_desc", .desc = "Textual description of the latest health check report for this server" },
220 [ST_F_AGENT_DESC] = { .name = "agent_desc", .desc = "Textual description of the latest agent check report for this server" },
221 [ST_F_CHECK_RISE] = { .name = "check_rise", .desc = "Number of successful health checks before declaring a server UP (server 'rise' setting)" },
222 [ST_F_CHECK_FALL] = { .name = "check_fall", .desc = "Number of failed health checks before declaring a server DOWN (server 'fall' setting)" },
223 [ST_F_CHECK_HEALTH] = { .name = "check_health", .desc = "Current server health check level (0..fall-1=DOWN, fall..rise-1=UP)" },
224 [ST_F_AGENT_RISE] = { .name = "agent_rise", .desc = "Number of successful agent checks before declaring a server UP (server 'rise' setting)" },
225 [ST_F_AGENT_FALL] = { .name = "agent_fall", .desc = "Number of failed agent checks before declaring a server DOWN (server 'fall' setting)" },
226 [ST_F_AGENT_HEALTH] = { .name = "agent_health", .desc = "Current server agent check level (0..fall-1=DOWN, fall..rise-1=UP)" },
227 [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" },
228 [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" },
229 [ST_F_MODE] = { .name = "mode", .desc = "'mode' setting (tcp/http/health/cli)" },
230 [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" },
231 [ST_F_CONN_RATE] = { .name = "conn_rate", .desc = "Number of new connections accepted over the last second on the frontend for this worker process" },
232 [ST_F_CONN_RATE_MAX] = { .name = "conn_rate_max", .desc = "Highest value of 'conn_rate' observed since the worker process started" },
233 [ST_F_CONN_TOT] = { .name = "conn_tot", .desc = "Total number of new connections accepted on this frontend since the worker process started" },
234 [ST_F_INTERCEPTED] = { .name = "intercepted", .desc = "Total number of HTTP requests intercepted on the frontend (redirects/stats/services) since the worker process started" },
235 [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" },
236 [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" },
237 [ST_F_WREW] = { .name = "wrew", .desc = "Total number of failed HTTP header rewrites since the worker process started" },
238 [ST_F_CONNECT] = { .name = "connect", .desc = "Total number of outgoing connection attempts on this backend/server since the worker process started" },
239 [ST_F_REUSE] = { .name = "reuse", .desc = "Total number of reused connection on this backend/server since the worker process started" },
240 [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" },
241 [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" },
242 [ST_F_SRV_ICUR] = { .name = "srv_icur", .desc = "Current number of idle connections available for reuse on this server" },
243 [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 +0100244 [ST_F_QT_MAX] = { .name = "qtime_max", .desc = "Maximum observed time spent in the queue, in milliseconds (backend/server)" },
245 [ST_F_CT_MAX] = { .name = "ctime_max", .desc = "Maximum observed time spent waiting for a connection to complete, in milliseconds (backend/server)" },
246 [ST_F_RT_MAX] = { .name = "rtime_max", .desc = "Maximum observed time spent waiting for a server response, in milliseconds (backend/server)" },
247 [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 +0100248 [ST_F_EINT] = { .name = "eint", .desc = "Total number of internal errors since process started"},
Willy Tarreau3bb617c2020-06-29 13:51:05 +0200249 [ST_F_IDLE_CONN_CUR] = { .name = "idle_conn_cur", .desc = "Current number of unsafe idle connections"},
250 [ST_F_SAFE_CONN_CUR] = { .name = "safe_conn_cur", .desc = "Current number of safe idle connections"},
251 [ST_F_USED_CONN_CUR] = { .name = "used_conn_cur", .desc = "Current number of connections in use"},
Willy Tarreaua9fcecb2020-06-29 15:38:53 +0200252 [ST_F_NEED_CONN_EST] = { .name = "need_conn_est", .desc = "Estimated needed number of connections"},
William Lallemand74c24fb2016-11-21 17:18:36 +0100253};
254
Willy Tarreau0baac8c2016-11-22 16:36:53 +0100255/* one line of info */
Christopher Faulet1bc04c72017-10-29 20:14:08 +0100256static THREAD_LOCAL struct field info[INF_TOTAL_FIELDS];
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +0200257
258/* description of statistics (static and dynamic) */
259static struct name_desc *stat_f[STATS_DOMAIN_COUNT];
260static size_t stat_count[STATS_DOMAIN_COUNT];
261
262/* one line for stats */
263static struct field *stat_l[STATS_DOMAIN_COUNT];
William Lallemand74c24fb2016-11-21 17:18:36 +0100264
Amaury Denoyelle58d395e2020-10-05 11:49:40 +0200265/* list of all registered stats module */
266static struct list stats_module_list[STATS_DOMAIN_COUNT] = {
267 LIST_HEAD_INIT(stats_module_list[STATS_DOMAIN_PROXY]),
268};
269
Amaury Denoyelle072f97e2020-10-05 11:49:37 +0200270static inline uint8_t stats_get_domain(uint32_t domain)
271{
272 return domain >> STATS_DOMAIN & STATS_DOMAIN_MASK;
273}
274
Amaury Denoyelle72b16e52020-10-05 11:49:38 +0200275static inline enum stats_domain_px_cap stats_px_get_cap(uint32_t domain)
276{
277 return domain >> STATS_PX_CAP & STATS_PX_CAP_MASK;
278}
279
Christopher Faulet6338a082019-09-09 15:50:54 +0200280static void stats_dump_json_schema(struct buffer *out);
William Lallemand74c24fb2016-11-21 17:18:36 +0100281
Amaury Denoyelle3ca927e2020-10-02 18:32:00 +0200282int stats_putchk(struct channel *chn, struct htx *htx, struct buffer *chk)
Christopher Fauletef779222018-10-31 08:47:01 +0100283{
284 if (htx) {
Christopher Faulet69fc88c2019-01-07 14:27:53 +0100285 if (chk->data >= channel_htx_recv_max(chn, htx))
286 return 0;
Willy Tarreau0a7ef022019-05-28 10:30:11 +0200287 if (!htx_add_data_atonce(htx, ist2(chk->area, chk->data)))
Christopher Fauletef779222018-10-31 08:47:01 +0100288 return 0;
Christopher Faulet5adbeeb2019-01-02 14:34:39 +0100289 channel_add_input(chn, chk->data);
Christopher Fauletef779222018-10-31 08:47:01 +0100290 chk->data = 0;
Christopher Fauletef779222018-10-31 08:47:01 +0100291 }
292 else {
293 if (ci_putchk(chn, chk) == -1)
294 return 0;
295 }
296 return 1;
297}
Willy Tarreau0baac8c2016-11-22 16:36:53 +0100298
Christopher Fauleted7a0662019-01-14 11:07:34 +0100299static const char *stats_scope_ptr(struct appctx *appctx, struct stream_interface *si)
300{
Christopher Fauletb7f88902019-07-15 21:56:43 +0200301 struct channel *req = si_oc(si);
302 struct htx *htx = htxbuf(&req->buf);
303 struct htx_blk *blk;
304 struct ist uri;
Christopher Fauleted7a0662019-01-14 11:07:34 +0100305
Christopher Fauletb7f88902019-07-15 21:56:43 +0200306 blk = htx_get_head_blk(htx);
Christopher Fauletea009732019-11-18 15:50:25 +0100307 BUG_ON(!blk || htx_get_blk_type(blk) != HTX_BLK_REQ_SL);
Christopher Fauletb7f88902019-07-15 21:56:43 +0200308 ALREADY_CHECKED(blk);
309 uri = htx_sl_req_uri(htx_get_blk_ptr(htx, blk));
310 return uri.ptr + appctx->ctx.stats.scope_str;
Christopher Fauleted7a0662019-01-14 11:07:34 +0100311}
312
William Lallemand74c24fb2016-11-21 17:18:36 +0100313/*
314 * http_stats_io_handler()
315 * -> stats_dump_stat_to_buffer() // same as above, but used for CSV or HTML
316 * -> stats_dump_csv_header() // emits the CSV headers (same as above)
Simon Horman05ee2132017-01-04 09:37:25 +0100317 * -> stats_dump_json_header() // emits the JSON headers (same as above)
William Lallemand74c24fb2016-11-21 17:18:36 +0100318 * -> stats_dump_html_head() // emits the HTML headers
319 * -> stats_dump_html_info() // emits the equivalent of "show info" at the top
320 * -> stats_dump_proxy_to_buffer() // same as above, valid for CSV and HTML
321 * -> stats_dump_html_px_hdr()
322 * -> stats_dump_fe_stats()
323 * -> stats_dump_li_stats()
324 * -> stats_dump_sv_stats()
325 * -> stats_dump_be_stats()
326 * -> stats_dump_html_px_end()
327 * -> stats_dump_html_end() // emits HTML trailer
Simon Horman05ee2132017-01-04 09:37:25 +0100328 * -> stats_dump_json_end() // emits JSON trailer
William Lallemand74c24fb2016-11-21 17:18:36 +0100329 */
330
331
William Lallemand74c24fb2016-11-21 17:18:36 +0100332/* Dumps the stats CSV header to the trash buffer which. The caller is responsible
333 * for clearing it if needed.
334 * NOTE: Some tools happen to rely on the field position instead of its name,
335 * so please only append new fields at the end, never in the middle.
336 */
Amaury Denoyelle50660a82020-10-05 11:49:39 +0200337static void stats_dump_csv_header(enum stats_domain domain)
William Lallemand74c24fb2016-11-21 17:18:36 +0100338{
339 int field;
340
341 chunk_appendf(&trash, "# ");
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +0200342 if (stat_f[domain]) {
343 for (field = 0; field < stat_count[domain]; ++field) {
344 chunk_appendf(&trash, "%s,", stat_f[domain][field].name);
William Lallemand74c24fb2016-11-21 17:18:36 +0100345
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +0200346 /* print special delimiter on proxy stats to mark end of
347 static fields */
348 if (domain == STATS_DOMAIN_PROXY && field + 1 == ST_F_TOTAL_FIELDS)
349 chunk_appendf(&trash, "-,");
Amaury Denoyelle50660a82020-10-05 11:49:39 +0200350 }
351 }
352
William Lallemand74c24fb2016-11-21 17:18:36 +0100353 chunk_appendf(&trash, "\n");
354}
355
William Lallemand74c24fb2016-11-21 17:18:36 +0100356/* Emits a stats field without any surrounding element and properly encoded to
357 * resist CSV output. Returns non-zero on success, 0 if the buffer is full.
358 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200359int stats_emit_raw_data_field(struct buffer *out, const struct field *f)
William Lallemand74c24fb2016-11-21 17:18:36 +0100360{
361 switch (field_format(f, 0)) {
362 case FF_EMPTY: return 1;
363 case FF_S32: return chunk_appendf(out, "%d", f->u.s32);
364 case FF_U32: return chunk_appendf(out, "%u", f->u.u32);
365 case FF_S64: return chunk_appendf(out, "%lld", (long long)f->u.s64);
366 case FF_U64: return chunk_appendf(out, "%llu", (unsigned long long)f->u.u64);
Christopher Faulet88a0db22019-09-24 16:35:10 +0200367 case FF_FLT: return chunk_appendf(out, "%f", f->u.flt);
William Lallemand74c24fb2016-11-21 17:18:36 +0100368 case FF_STR: return csv_enc_append(field_str(f, 0), 1, out) != NULL;
369 default: return chunk_appendf(out, "[INCORRECT_FIELD_TYPE_%08x]", f->type);
370 }
371}
372
Amaury Denoyellee3f576c2020-10-05 11:49:44 +0200373const char *field_to_html_str(const struct field *f)
374{
375 switch (field_format(f, 0)) {
376 case FF_S32: return U2H(f->u.s32);
377 case FF_S64: return U2H(f->u.s64);
378 case FF_U64: return U2H(f->u.u64);
379 case FF_U32: return U2H(f->u.u32);
380 case FF_STR: return field_str(f, 0);
381 case FF_EMPTY:
382 default:
383 return "";
384 }
385}
386
William Lallemand74c24fb2016-11-21 17:18:36 +0100387/* Emits a stats field prefixed with its type. No CSV encoding is prepared, the
388 * output is supposed to be used on its own line. Returns non-zero on success, 0
389 * if the buffer is full.
390 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200391int stats_emit_typed_data_field(struct buffer *out, const struct field *f)
William Lallemand74c24fb2016-11-21 17:18:36 +0100392{
393 switch (field_format(f, 0)) {
394 case FF_EMPTY: return 1;
395 case FF_S32: return chunk_appendf(out, "s32:%d", f->u.s32);
396 case FF_U32: return chunk_appendf(out, "u32:%u", f->u.u32);
397 case FF_S64: return chunk_appendf(out, "s64:%lld", (long long)f->u.s64);
398 case FF_U64: return chunk_appendf(out, "u64:%llu", (unsigned long long)f->u.u64);
Christopher Faulet88a0db22019-09-24 16:35:10 +0200399 case FF_FLT: return chunk_appendf(out, "flt:%f", f->u.flt);
William Lallemand74c24fb2016-11-21 17:18:36 +0100400 case FF_STR: return chunk_appendf(out, "str:%s", field_str(f, 0));
401 default: return chunk_appendf(out, "%08x:?", f->type);
402 }
403}
404
Simon Horman05ee2132017-01-04 09:37:25 +0100405/* Limit JSON integer values to the range [-(2**53)+1, (2**53)-1] as per
406 * the recommendation for interoperable integers in section 6 of RFC 7159.
407 */
408#define JSON_INT_MAX ((1ULL << 53) - 1)
409#define JSON_INT_MIN (0 - JSON_INT_MAX)
410
411/* Emits a stats field value and its type in JSON.
412 * Returns non-zero on success, 0 on error.
413 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200414int stats_emit_json_data_field(struct buffer *out, const struct field *f)
Simon Horman05ee2132017-01-04 09:37:25 +0100415{
416 int old_len;
417 char buf[20];
418 const char *type, *value = buf, *quote = "";
419
420 switch (field_format(f, 0)) {
421 case FF_EMPTY: return 1;
422 case FF_S32: type = "\"s32\"";
423 snprintf(buf, sizeof(buf), "%d", f->u.s32);
424 break;
425 case FF_U32: type = "\"u32\"";
426 snprintf(buf, sizeof(buf), "%u", f->u.u32);
427 break;
428 case FF_S64: type = "\"s64\"";
429 if (f->u.s64 < JSON_INT_MIN || f->u.s64 > JSON_INT_MAX)
430 return 0;
431 type = "\"s64\"";
432 snprintf(buf, sizeof(buf), "%lld", (long long)f->u.s64);
433 break;
434 case FF_U64: if (f->u.u64 > JSON_INT_MAX)
435 return 0;
436 type = "\"u64\"";
437 snprintf(buf, sizeof(buf), "%llu",
438 (unsigned long long) f->u.u64);
Christopher Faulet52c91bb2019-09-28 10:37:31 +0200439 break;
Christopher Faulet88a0db22019-09-24 16:35:10 +0200440 case FF_FLT: type = "\"flt\"";
441 snprintf(buf, sizeof(buf), "%f", f->u.flt);
Simon Horman05ee2132017-01-04 09:37:25 +0100442 break;
443 case FF_STR: type = "\"str\"";
444 value = field_str(f, 0);
445 quote = "\"";
446 break;
447 default: snprintf(buf, sizeof(buf), "%u", f->type);
448 type = buf;
449 value = "unknown";
450 quote = "\"";
451 break;
452 }
453
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200454 old_len = out->data;
Simon Horman05ee2132017-01-04 09:37:25 +0100455 chunk_appendf(out, ",\"value\":{\"type\":%s,\"value\":%s%s%s}",
456 type, quote, value, quote);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200457 return !(old_len == out->data);
Simon Horman05ee2132017-01-04 09:37:25 +0100458}
459
William Lallemand74c24fb2016-11-21 17:18:36 +0100460/* Emits an encoding of the field type on 3 characters followed by a delimiter.
461 * Returns non-zero on success, 0 if the buffer is full.
462 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200463int stats_emit_field_tags(struct buffer *out, const struct field *f,
464 char delim)
William Lallemand74c24fb2016-11-21 17:18:36 +0100465{
466 char origin, nature, scope;
467
468 switch (field_origin(f, 0)) {
469 case FO_METRIC: origin = 'M'; break;
470 case FO_STATUS: origin = 'S'; break;
471 case FO_KEY: origin = 'K'; break;
472 case FO_CONFIG: origin = 'C'; break;
473 case FO_PRODUCT: origin = 'P'; break;
474 default: origin = '?'; break;
475 }
476
477 switch (field_nature(f, 0)) {
478 case FN_GAUGE: nature = 'G'; break;
479 case FN_LIMIT: nature = 'L'; break;
480 case FN_MIN: nature = 'm'; break;
481 case FN_MAX: nature = 'M'; break;
482 case FN_RATE: nature = 'R'; break;
483 case FN_COUNTER: nature = 'C'; break;
484 case FN_DURATION: nature = 'D'; break;
485 case FN_AGE: nature = 'A'; break;
486 case FN_TIME: nature = 'T'; break;
487 case FN_NAME: nature = 'N'; break;
488 case FN_OUTPUT: nature = 'O'; break;
489 case FN_AVG: nature = 'a'; break;
490 default: nature = '?'; break;
491 }
492
493 switch (field_scope(f, 0)) {
494 case FS_PROCESS: scope = 'P'; break;
495 case FS_SERVICE: scope = 'S'; break;
496 case FS_SYSTEM: scope = 's'; break;
497 case FS_CLUSTER: scope = 'C'; break;
498 default: scope = '?'; break;
499 }
500
501 return chunk_appendf(out, "%c%c%c%c", origin, nature, scope, delim);
502}
503
Simon Horman05ee2132017-01-04 09:37:25 +0100504/* Emits an encoding of the field type as JSON.
505 * Returns non-zero on success, 0 if the buffer is full.
506 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200507int stats_emit_json_field_tags(struct buffer *out, const struct field *f)
Simon Horman05ee2132017-01-04 09:37:25 +0100508{
509 const char *origin, *nature, *scope;
510 int old_len;
511
512 switch (field_origin(f, 0)) {
513 case FO_METRIC: origin = "Metric"; break;
514 case FO_STATUS: origin = "Status"; break;
515 case FO_KEY: origin = "Key"; break;
516 case FO_CONFIG: origin = "Config"; break;
517 case FO_PRODUCT: origin = "Product"; break;
518 default: origin = "Unknown"; break;
519 }
520
521 switch (field_nature(f, 0)) {
522 case FN_GAUGE: nature = "Gauge"; break;
523 case FN_LIMIT: nature = "Limit"; break;
524 case FN_MIN: nature = "Min"; break;
525 case FN_MAX: nature = "Max"; break;
526 case FN_RATE: nature = "Rate"; break;
527 case FN_COUNTER: nature = "Counter"; break;
528 case FN_DURATION: nature = "Duration"; break;
529 case FN_AGE: nature = "Age"; break;
530 case FN_TIME: nature = "Time"; break;
531 case FN_NAME: nature = "Name"; break;
532 case FN_OUTPUT: nature = "Output"; break;
533 case FN_AVG: nature = "Avg"; break;
534 default: nature = "Unknown"; break;
535 }
536
537 switch (field_scope(f, 0)) {
538 case FS_PROCESS: scope = "Process"; break;
539 case FS_SERVICE: scope = "Service"; break;
540 case FS_SYSTEM: scope = "System"; break;
541 case FS_CLUSTER: scope = "Cluster"; break;
542 default: scope = "Unknown"; break;
543 }
544
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200545 old_len = out->data;
Simon Horman05ee2132017-01-04 09:37:25 +0100546 chunk_appendf(out, "\"tags\":{"
547 "\"origin\":\"%s\","
548 "\"nature\":\"%s\","
549 "\"scope\":\"%s\""
550 "}", origin, nature, scope);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200551 return !(old_len == out->data);
Simon Horman05ee2132017-01-04 09:37:25 +0100552}
William Lallemand74c24fb2016-11-21 17:18:36 +0100553
554/* Dump all fields from <stats> into <out> using CSV format */
Willy Tarreau83061a82018-07-13 11:56:34 +0200555static int stats_dump_fields_csv(struct buffer *out,
Amaury Denoyelle97323c92020-10-02 18:32:01 +0200556 const struct field *stats, size_t stats_count,
Amaury Denoyelle50660a82020-10-05 11:49:39 +0200557 unsigned int flags,
558 enum stats_domain domain)
William Lallemand74c24fb2016-11-21 17:18:36 +0100559{
560 int field;
561
Amaury Denoyelle50660a82020-10-05 11:49:39 +0200562 for (field = 0; field < stats_count; ++field) {
William Lallemand74c24fb2016-11-21 17:18:36 +0100563 if (!stats_emit_raw_data_field(out, &stats[field]))
564 return 0;
565 if (!chunk_strcat(out, ","))
566 return 0;
Amaury Denoyelle50660a82020-10-05 11:49:39 +0200567
568 /* print special delimiter on proxy stats to mark end of
569 static fields */
570 if (domain == STATS_DOMAIN_PROXY && field + 1 == ST_F_TOTAL_FIELDS) {
571 if (!chunk_strcat(out, "-,"))
572 return 0;
573 }
William Lallemand74c24fb2016-11-21 17:18:36 +0100574 }
Amaury Denoyelle50660a82020-10-05 11:49:39 +0200575
William Lallemand74c24fb2016-11-21 17:18:36 +0100576 chunk_strcat(out, "\n");
577 return 1;
578}
579
580/* Dump all fields from <stats> into <out> using a typed "field:desc:type:value" format */
Willy Tarreau83061a82018-07-13 11:56:34 +0200581static int stats_dump_fields_typed(struct buffer *out,
Amaury Denoyelle97323c92020-10-02 18:32:01 +0200582 const struct field *stats,
583 size_t stats_count,
Amaury Denoyelle072f97e2020-10-05 11:49:37 +0200584 unsigned int flags,
585 enum stats_domain domain)
William Lallemand74c24fb2016-11-21 17:18:36 +0100586{
587 int field;
588
Amaury Denoyelle97323c92020-10-02 18:32:01 +0200589 for (field = 0; field < stats_count; ++field) {
William Lallemand74c24fb2016-11-21 17:18:36 +0100590 if (!stats[field].type)
591 continue;
592
Amaury Denoyelle072f97e2020-10-05 11:49:37 +0200593 switch (domain) {
594 case STATS_DOMAIN_PROXY:
595 chunk_appendf(out, "%c.%u.%u.%d.%s.%u:",
596 stats[ST_F_TYPE].u.u32 == STATS_TYPE_FE ? 'F' :
597 stats[ST_F_TYPE].u.u32 == STATS_TYPE_BE ? 'B' :
598 stats[ST_F_TYPE].u.u32 == STATS_TYPE_SO ? 'L' :
599 stats[ST_F_TYPE].u.u32 == STATS_TYPE_SV ? 'S' :
600 '?',
601 stats[ST_F_IID].u.u32, stats[ST_F_SID].u.u32,
602 field,
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +0200603 stat_f[domain][field].name,
Amaury Denoyelle072f97e2020-10-05 11:49:37 +0200604 stats[ST_F_PID].u.u32);
605 break;
606
607 default:
608 break;
609 }
William Lallemand74c24fb2016-11-21 17:18:36 +0100610
611 if (!stats_emit_field_tags(out, &stats[field], ':'))
612 return 0;
613 if (!stats_emit_typed_data_field(out, &stats[field]))
614 return 0;
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +0200615
616 if (flags & STAT_SHOW_FDESC
617 && !chunk_appendf(out, ":\"%s\"", stat_f[domain][field].name)) {
Willy Tarreau6b19b142019-10-09 15:44:21 +0200618 return 0;
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +0200619 }
620
William Lallemand74c24fb2016-11-21 17:18:36 +0100621 if (!chunk_strcat(out, "\n"))
622 return 0;
623 }
624 return 1;
625}
626
Simon Horman05ee2132017-01-04 09:37:25 +0100627/* Dump all fields from <stats> into <out> using the "show info json" format */
Willy Tarreau83061a82018-07-13 11:56:34 +0200628static int stats_dump_json_info_fields(struct buffer *out,
Willy Tarreau43241ff2019-10-09 11:27:51 +0200629 const struct field *info, unsigned int flags)
Simon Horman05ee2132017-01-04 09:37:25 +0100630{
631 int field;
632 int started = 0;
633
634 if (!chunk_strcat(out, "["))
635 return 0;
636
637 for (field = 0; field < INF_TOTAL_FIELDS; field++) {
638 int old_len;
639
640 if (!field_format(info, field))
641 continue;
642
643 if (started && !chunk_strcat(out, ","))
644 goto err;
645 started = 1;
646
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200647 old_len = out->data;
Simon Horman05ee2132017-01-04 09:37:25 +0100648 chunk_appendf(out,
649 "{\"field\":{\"pos\":%d,\"name\":\"%s\"},"
650 "\"processNum\":%u,",
Willy Tarreaueaa55372019-10-09 07:39:11 +0200651 field, info_fields[field].name,
Simon Horman05ee2132017-01-04 09:37:25 +0100652 info[INF_PROCESS_NUM].u.u32);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200653 if (old_len == out->data)
Simon Horman05ee2132017-01-04 09:37:25 +0100654 goto err;
655
656 if (!stats_emit_json_field_tags(out, &info[field]))
657 goto err;
658
659 if (!stats_emit_json_data_field(out, &info[field]))
660 goto err;
661
662 if (!chunk_strcat(out, "}"))
663 goto err;
664 }
665
666 if (!chunk_strcat(out, "]"))
667 goto err;
668 return 1;
669
670err:
671 chunk_reset(out);
672 chunk_appendf(out, "{\"errorStr\":\"output buffer too short\"}");
673 return 0;
674}
675
Amaury Denoyellef34017b2020-10-02 18:32:03 +0200676static void stats_print_proxy_field_json(struct buffer *out,
677 const struct field *stat,
678 const char *name,
679 int pos,
680 uint32_t field_type,
681 uint32_t iid,
682 uint32_t sid,
683 uint32_t pid)
684{
685 const char *obj_type;
686 switch (field_type) {
687 case STATS_TYPE_FE: obj_type = "Frontend"; break;
688 case STATS_TYPE_BE: obj_type = "Backend"; break;
689 case STATS_TYPE_SO: obj_type = "Listener"; break;
690 case STATS_TYPE_SV: obj_type = "Server"; break;
691 default: obj_type = "Unknown"; break;
692 }
693
694 chunk_appendf(out,
695 "{"
696 "\"objType\":\"%s\","
697 "\"proxyId\":%u,"
698 "\"id\":%u,"
699 "\"field\":{\"pos\":%d,\"name\":\"%s\"},"
700 "\"processNum\":%u,",
701 obj_type, iid, sid, pos, name, pid);
702}
703
Simon Horman05ee2132017-01-04 09:37:25 +0100704/* Dump all fields from <stats> into <out> using a typed "field:desc:type:value" format */
Willy Tarreau83061a82018-07-13 11:56:34 +0200705static int stats_dump_fields_json(struct buffer *out,
Amaury Denoyelle97323c92020-10-02 18:32:01 +0200706 const struct field *stats, size_t stats_count,
Amaury Denoyelle072f97e2020-10-05 11:49:37 +0200707 unsigned int flags,
708 enum stats_domain domain)
Simon Horman05ee2132017-01-04 09:37:25 +0100709{
710 int field;
711 int started = 0;
712
Willy Tarreaub0ce3ad2019-10-09 11:19:29 +0200713 if ((flags & STAT_STARTED) && !chunk_strcat(out, ","))
Simon Horman05ee2132017-01-04 09:37:25 +0100714 return 0;
715 if (!chunk_strcat(out, "["))
716 return 0;
717
Amaury Denoyelle97323c92020-10-02 18:32:01 +0200718 for (field = 0; field < stats_count; field++) {
Simon Horman05ee2132017-01-04 09:37:25 +0100719 int old_len;
720
721 if (!stats[field].type)
722 continue;
723
724 if (started && !chunk_strcat(out, ","))
725 goto err;
726 started = 1;
727
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200728 old_len = out->data;
Amaury Denoyelle072f97e2020-10-05 11:49:37 +0200729 if (domain == STATS_DOMAIN_PROXY) {
730 stats_print_proxy_field_json(out, &stats[field],
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +0200731 stat_f[domain][field].name,
732 field,
Amaury Denoyelle072f97e2020-10-05 11:49:37 +0200733 stats[ST_F_TYPE].u.u32,
734 stats[ST_F_IID].u.u32,
735 stats[ST_F_SID].u.u32,
736 stats[ST_F_PID].u.u32);
737 }
Amaury Denoyellef34017b2020-10-02 18:32:03 +0200738
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200739 if (old_len == out->data)
Simon Horman05ee2132017-01-04 09:37:25 +0100740 goto err;
741
742 if (!stats_emit_json_field_tags(out, &stats[field]))
743 goto err;
744
745 if (!stats_emit_json_data_field(out, &stats[field]))
746 goto err;
747
748 if (!chunk_strcat(out, "}"))
749 goto err;
750 }
751
752 if (!chunk_strcat(out, "]"))
753 goto err;
754
755 return 1;
756
757err:
758 chunk_reset(out);
Willy Tarreaub0ce3ad2019-10-09 11:19:29 +0200759 if (flags & STAT_STARTED)
760 chunk_strcat(out, ",");
Simon Horman05ee2132017-01-04 09:37:25 +0100761 chunk_appendf(out, "{\"errorStr\":\"output buffer too short\"}");
762 return 0;
763}
764
William Lallemand74c24fb2016-11-21 17:18:36 +0100765/* Dump all fields from <stats> into <out> using the HTML format. A column is
Willy Tarreauab02b3f2019-10-09 11:11:46 +0200766 * reserved for the checkbox is STAT_ADMIN is set in <flags>. Some extra info
Willy Tarreau708c4162019-10-09 10:19:16 +0200767 * are provided if STAT_SHLGNDS is present in <flags>.
William Lallemand74c24fb2016-11-21 17:18:36 +0100768 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200769static int stats_dump_fields_html(struct buffer *out,
770 const struct field *stats,
771 unsigned int flags)
William Lallemand74c24fb2016-11-21 17:18:36 +0100772{
Willy Tarreau83061a82018-07-13 11:56:34 +0200773 struct buffer src;
Amaury Denoyellee3f576c2020-10-05 11:49:44 +0200774 struct stats_module *mod;
775 int i = 0, j = 0;
William Lallemand74c24fb2016-11-21 17:18:36 +0100776
777 if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_FE) {
778 chunk_appendf(out,
779 /* name, queue */
780 "<tr class=\"frontend\">");
781
Willy Tarreauab02b3f2019-10-09 11:11:46 +0200782 if (flags & STAT_ADMIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +0100783 /* Column sub-heading for Enable or Disable server */
784 chunk_appendf(out, "<td></td>");
785 }
786
787 chunk_appendf(out,
788 "<td class=ac>"
789 "<a name=\"%s/Frontend\"></a>"
790 "<a class=lfsb href=\"#%s/Frontend\">Frontend</a></td>"
791 "<td colspan=3></td>"
792 "",
793 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_PXNAME));
794
795 chunk_appendf(out,
796 /* sessions rate : current */
797 "<td><u>%s<div class=tips><table class=det>"
798 "<tr><th>Current connection rate:</th><td>%s/s</td></tr>"
799 "<tr><th>Current session rate:</th><td>%s/s</td></tr>"
800 "",
801 U2H(stats[ST_F_RATE].u.u32),
802 U2H(stats[ST_F_CONN_RATE].u.u32),
803 U2H(stats[ST_F_RATE].u.u32));
804
805 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
806 chunk_appendf(out,
807 "<tr><th>Current request rate:</th><td>%s/s</td></tr>",
808 U2H(stats[ST_F_REQ_RATE].u.u32));
809
810 chunk_appendf(out,
811 "</table></div></u></td>"
812 /* sessions rate : max */
813 "<td><u>%s<div class=tips><table class=det>"
814 "<tr><th>Max connection rate:</th><td>%s/s</td></tr>"
815 "<tr><th>Max session rate:</th><td>%s/s</td></tr>"
816 "",
817 U2H(stats[ST_F_RATE_MAX].u.u32),
818 U2H(stats[ST_F_CONN_RATE_MAX].u.u32),
819 U2H(stats[ST_F_RATE_MAX].u.u32));
820
821 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
822 chunk_appendf(out,
823 "<tr><th>Max request rate:</th><td>%s/s</td></tr>",
824 U2H(stats[ST_F_REQ_RATE_MAX].u.u32));
825
826 chunk_appendf(out,
827 "</table></div></u></td>"
828 /* sessions rate : limit */
829 "<td>%s</td>",
830 LIM2A(stats[ST_F_RATE_LIM].u.u32, "-"));
831
832 chunk_appendf(out,
833 /* sessions: current, max, limit, total */
834 "<td>%s</td><td>%s</td><td>%s</td>"
835 "<td><u>%s<div class=tips><table class=det>"
836 "<tr><th>Cum. connections:</th><td>%s</td></tr>"
837 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
838 "",
839 U2H(stats[ST_F_SCUR].u.u32), U2H(stats[ST_F_SMAX].u.u32), U2H(stats[ST_F_SLIM].u.u32),
840 U2H(stats[ST_F_STOT].u.u64),
841 U2H(stats[ST_F_CONN_TOT].u.u64),
842 U2H(stats[ST_F_STOT].u.u64));
843
844 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
845 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0) {
846 chunk_appendf(out,
847 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
848 "<tr><th>- HTTP 1xx responses:</th><td>%s</td></tr>"
849 "<tr><th>- HTTP 2xx responses:</th><td>%s</td></tr>"
850 "<tr><th>&nbsp;&nbsp;Compressed 2xx:</th><td>%s</td><td>(%d%%)</td></tr>"
851 "<tr><th>- HTTP 3xx responses:</th><td>%s</td></tr>"
852 "<tr><th>- HTTP 4xx responses:</th><td>%s</td></tr>"
853 "<tr><th>- HTTP 5xx responses:</th><td>%s</td></tr>"
854 "<tr><th>- other responses:</th><td>%s</td></tr>"
855 "<tr><th>Intercepted requests:</th><td>%s</td></tr>"
Willy Tarreaua1214a52018-12-14 14:00:25 +0100856 "<tr><th>Cache lookups:</th><td>%s</td></tr>"
857 "<tr><th>Cache hits:</th><td>%s</td><td>(%d%%)</td></tr>"
Willy Tarreau1b0f85e2018-05-28 15:12:40 +0200858 "<tr><th>Failed hdr rewrites:</th><td>%s</td></tr>"
Christopher Faulet0159ee42019-12-16 14:40:39 +0100859 "<tr><th>Internal errors:</th><td>%s</td></tr>"
William Lallemand74c24fb2016-11-21 17:18:36 +0100860 "",
861 U2H(stats[ST_F_REQ_TOT].u.u64),
862 U2H(stats[ST_F_HRSP_1XX].u.u64),
863 U2H(stats[ST_F_HRSP_2XX].u.u64),
864 U2H(stats[ST_F_COMP_RSP].u.u64),
865 stats[ST_F_HRSP_2XX].u.u64 ?
866 (int)(100 * stats[ST_F_COMP_RSP].u.u64 / stats[ST_F_HRSP_2XX].u.u64) : 0,
867 U2H(stats[ST_F_HRSP_3XX].u.u64),
868 U2H(stats[ST_F_HRSP_4XX].u.u64),
869 U2H(stats[ST_F_HRSP_5XX].u.u64),
870 U2H(stats[ST_F_HRSP_OTHER].u.u64),
Willy Tarreau1b0f85e2018-05-28 15:12:40 +0200871 U2H(stats[ST_F_INTERCEPTED].u.u64),
Willy Tarreaua1214a52018-12-14 14:00:25 +0100872 U2H(stats[ST_F_CACHE_LOOKUPS].u.u64),
873 U2H(stats[ST_F_CACHE_HITS].u.u64),
874 stats[ST_F_CACHE_LOOKUPS].u.u64 ?
875 (int)(100 * stats[ST_F_CACHE_HITS].u.u64 / stats[ST_F_CACHE_LOOKUPS].u.u64) : 0,
Christopher Faulet0159ee42019-12-16 14:40:39 +0100876 U2H(stats[ST_F_WREW].u.u64),
877 U2H(stats[ST_F_EINT].u.u64));
William Lallemand74c24fb2016-11-21 17:18:36 +0100878 }
879
880 chunk_appendf(out,
881 "</table></div></u></td>"
882 /* sessions: lbtot, lastsess */
883 "<td></td><td></td>"
884 /* bytes : in */
885 "<td>%s</td>"
886 "",
887 U2H(stats[ST_F_BIN].u.u64));
888
889 chunk_appendf(out,
890 /* bytes:out + compression stats (via hover): comp_in, comp_out, comp_byp */
891 "<td>%s%s<div class=tips><table class=det>"
892 "<tr><th>Response bytes in:</th><td>%s</td></tr>"
893 "<tr><th>Compression in:</th><td>%s</td></tr>"
894 "<tr><th>Compression out:</th><td>%s</td><td>(%d%%)</td></tr>"
895 "<tr><th>Compression bypass:</th><td>%s</td></tr>"
896 "<tr><th>Total bytes saved:</th><td>%s</td><td>(%d%%)</td></tr>"
897 "</table></div>%s</td>",
898 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "<u>":"",
899 U2H(stats[ST_F_BOUT].u.u64),
900 U2H(stats[ST_F_BOUT].u.u64),
901 U2H(stats[ST_F_COMP_IN].u.u64),
902 U2H(stats[ST_F_COMP_OUT].u.u64),
903 stats[ST_F_COMP_IN].u.u64 ? (int)(stats[ST_F_COMP_OUT].u.u64 * 100 / stats[ST_F_COMP_IN].u.u64) : 0,
904 U2H(stats[ST_F_COMP_BYP].u.u64),
905 U2H(stats[ST_F_COMP_IN].u.u64 - stats[ST_F_COMP_OUT].u.u64),
906 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,
907 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "</u>":"");
908
909 chunk_appendf(out,
910 /* denied: req, resp */
911 "<td>%s</td><td>%s</td>"
912 /* errors : request, connect, response */
913 "<td>%s</td><td></td><td></td>"
914 /* warnings: retries, redispatches */
915 "<td></td><td></td>"
916 /* server status : reflect frontend status */
917 "<td class=ac>%s</td>"
918 /* rest of server: nothing */
Amaury Denoyellee3f576c2020-10-05 11:49:44 +0200919 "<td class=ac colspan=8></td>"
William Lallemand74c24fb2016-11-21 17:18:36 +0100920 "",
921 U2H(stats[ST_F_DREQ].u.u64), U2H(stats[ST_F_DRESP].u.u64),
922 U2H(stats[ST_F_EREQ].u.u64),
923 field_str(stats, ST_F_STATUS));
Amaury Denoyellee3f576c2020-10-05 11:49:44 +0200924
925 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
926 chunk_appendf(out,
927 "<td><u>%s<div class=tips><table class=det>",
928 mod->name);
929 if (stats_px_get_cap(mod->domain_flags) & STATS_PX_CAP_FE) {
930 for (j = 0; j < mod->stats_count; ++j) {
931 chunk_appendf(out,
932 "<tr><th>%s</th><td>%s</td></tr>",
933 mod->stats[j].desc, field_to_html_str(&stats[ST_F_TOTAL_FIELDS + i]));
934 ++i;
935 }
936 } else {
937 i += mod->stats_count;
938 }
939
940 chunk_appendf(out,
941 "</table></div></u></td>");
942 }
943
944 chunk_appendf(out, "</tr>");
William Lallemand74c24fb2016-11-21 17:18:36 +0100945 }
946 else if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_SO) {
947 chunk_appendf(out, "<tr class=socket>");
Willy Tarreauab02b3f2019-10-09 11:11:46 +0200948 if (flags & STAT_ADMIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +0100949 /* Column sub-heading for Enable or Disable server */
950 chunk_appendf(out, "<td></td>");
951 }
952
953 chunk_appendf(out,
954 /* frontend name, listener name */
955 "<td class=ac><a name=\"%s/+%s\"></a>%s"
956 "<a class=lfsb href=\"#%s/+%s\">%s</a>"
957 "",
958 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME),
Willy Tarreau708c4162019-10-09 10:19:16 +0200959 (flags & STAT_SHLGNDS)?"<u>":"",
William Lallemand74c24fb2016-11-21 17:18:36 +0100960 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME), field_str(stats, ST_F_SVNAME));
961
Willy Tarreau708c4162019-10-09 10:19:16 +0200962 if (flags & STAT_SHLGNDS) {
William Lallemand74c24fb2016-11-21 17:18:36 +0100963 chunk_appendf(out, "<div class=tips>");
964
Willy Tarreau90807112020-02-25 08:16:33 +0100965 if (isdigit((unsigned char)*field_str(stats, ST_F_ADDR)))
William Lallemand74c24fb2016-11-21 17:18:36 +0100966 chunk_appendf(out, "IPv4: %s, ", field_str(stats, ST_F_ADDR));
967 else if (*field_str(stats, ST_F_ADDR) == '[')
968 chunk_appendf(out, "IPv6: %s, ", field_str(stats, ST_F_ADDR));
969 else if (*field_str(stats, ST_F_ADDR))
970 chunk_appendf(out, "%s, ", field_str(stats, ST_F_ADDR));
971
972 /* id */
973 chunk_appendf(out, "id: %d</div>", stats[ST_F_SID].u.u32);
974 }
975
976 chunk_appendf(out,
977 /* queue */
978 "%s</td><td colspan=3></td>"
979 /* sessions rate: current, max, limit */
980 "<td colspan=3>&nbsp;</td>"
981 /* sessions: current, max, limit, total, lbtot, lastsess */
982 "<td>%s</td><td>%s</td><td>%s</td>"
983 "<td>%s</td><td>&nbsp;</td><td>&nbsp;</td>"
984 /* bytes: in, out */
985 "<td>%s</td><td>%s</td>"
986 "",
Willy Tarreau708c4162019-10-09 10:19:16 +0200987 (flags & STAT_SHLGNDS)?"</u>":"",
William Lallemand74c24fb2016-11-21 17:18:36 +0100988 U2H(stats[ST_F_SCUR].u.u32), U2H(stats[ST_F_SMAX].u.u32), U2H(stats[ST_F_SLIM].u.u32),
989 U2H(stats[ST_F_STOT].u.u64), U2H(stats[ST_F_BIN].u.u64), U2H(stats[ST_F_BOUT].u.u64));
990
991 chunk_appendf(out,
992 /* denied: req, resp */
993 "<td>%s</td><td>%s</td>"
994 /* errors: request, connect, response */
995 "<td>%s</td><td></td><td></td>"
996 /* warnings: retries, redispatches */
997 "<td></td><td></td>"
998 /* server status: reflect listener status */
999 "<td class=ac>%s</td>"
1000 /* rest of server: nothing */
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001001 "<td class=ac colspan=8></td>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001002 "",
1003 U2H(stats[ST_F_DREQ].u.u64), U2H(stats[ST_F_DRESP].u.u64),
1004 U2H(stats[ST_F_EREQ].u.u64),
1005 field_str(stats, ST_F_STATUS));
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001006
1007 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
1008 chunk_appendf(out,
1009 "<td><u>%s<div class=tips><table class=det>",
1010 mod->name);
1011 if (stats_px_get_cap(mod->domain_flags) & STATS_PX_CAP_LI) {
1012 for (j = 0; j < mod->stats_count; ++j) {
1013 chunk_appendf(out,
1014 "<tr><th>%s</th><td>%s</td></tr>",
1015 mod->stats[j].desc, field_to_html_str(&stats[ST_F_TOTAL_FIELDS + i]));
1016 ++i;
1017 }
1018 } else {
1019 i += mod->stats_count;
1020 }
1021
1022 chunk_appendf(out,
1023 "</table></div></u></td>");
1024 }
1025
1026 chunk_appendf(out, "</tr>");
William Lallemand74c24fb2016-11-21 17:18:36 +01001027 }
1028 else if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_SV) {
1029 const char *style;
1030
1031 /* determine the style to use depending on the server's state,
1032 * its health and weight. There isn't a 1-to-1 mapping between
1033 * state and styles for the cases where the server is (still)
1034 * up. The reason is that we don't want to report nolb and
1035 * drain with the same color.
1036 */
1037
1038 if (strcmp(field_str(stats, ST_F_STATUS), "DOWN") == 0 ||
1039 strcmp(field_str(stats, ST_F_STATUS), "DOWN (agent)") == 0) {
1040 style = "down";
1041 }
1042 else if (strcmp(field_str(stats, ST_F_STATUS), "DOWN ") == 0) {
1043 style = "going_up";
1044 }
Daniel Corbettb4285172020-03-28 12:35:50 -04001045 else if (strcmp(field_str(stats, ST_F_STATUS), "DRAIN") == 0) {
1046 style = "draining";
1047 }
William Lallemand74c24fb2016-11-21 17:18:36 +01001048 else if (strcmp(field_str(stats, ST_F_STATUS), "NOLB ") == 0) {
1049 style = "going_down";
1050 }
1051 else if (strcmp(field_str(stats, ST_F_STATUS), "NOLB") == 0) {
1052 style = "nolb";
1053 }
1054 else if (strcmp(field_str(stats, ST_F_STATUS), "no check") == 0) {
1055 style = "no_check";
1056 }
1057 else if (!stats[ST_F_CHKFAIL].type ||
1058 stats[ST_F_CHECK_HEALTH].u.u32 == stats[ST_F_CHECK_RISE].u.u32 + stats[ST_F_CHECK_FALL].u.u32 - 1) {
1059 /* no check or max health = UP */
1060 if (stats[ST_F_WEIGHT].u.u32)
1061 style = "up";
1062 else
1063 style = "draining";
1064 }
1065 else {
1066 style = "going_down";
1067 }
1068
Willy Tarreau7b524852020-08-11 10:26:36 +02001069 if (strncmp(field_str(stats, ST_F_STATUS), "MAINT", 5) == 0)
William Lallemand74c24fb2016-11-21 17:18:36 +01001070 chunk_appendf(out, "<tr class=\"maintain\">");
1071 else
1072 chunk_appendf(out,
1073 "<tr class=\"%s_%s\">",
1074 (stats[ST_F_BCK].u.u32) ? "backup" : "active", style);
1075
1076
Willy Tarreauab02b3f2019-10-09 11:11:46 +02001077 if (flags & STAT_ADMIN)
William Lallemand74c24fb2016-11-21 17:18:36 +01001078 chunk_appendf(out,
David Harrigand3db35a2016-12-30 12:12:49 +00001079 "<td><input class='%s-checkbox' type=\"checkbox\" name=\"s\" value=\"%s\"></td>",
1080 field_str(stats, ST_F_PXNAME),
William Lallemand74c24fb2016-11-21 17:18:36 +01001081 field_str(stats, ST_F_SVNAME));
1082
1083 chunk_appendf(out,
1084 "<td class=ac><a name=\"%s/%s\"></a>%s"
1085 "<a class=lfsb href=\"#%s/%s\">%s</a>"
1086 "",
1087 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME),
Willy Tarreau708c4162019-10-09 10:19:16 +02001088 (flags & STAT_SHLGNDS) ? "<u>" : "",
William Lallemand74c24fb2016-11-21 17:18:36 +01001089 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME), field_str(stats, ST_F_SVNAME));
1090
Willy Tarreau708c4162019-10-09 10:19:16 +02001091 if (flags & STAT_SHLGNDS) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001092 chunk_appendf(out, "<div class=tips>");
1093
Willy Tarreau90807112020-02-25 08:16:33 +01001094 if (isdigit((unsigned char)*field_str(stats, ST_F_ADDR)))
William Lallemand74c24fb2016-11-21 17:18:36 +01001095 chunk_appendf(out, "IPv4: %s, ", field_str(stats, ST_F_ADDR));
1096 else if (*field_str(stats, ST_F_ADDR) == '[')
1097 chunk_appendf(out, "IPv6: %s, ", field_str(stats, ST_F_ADDR));
1098 else if (*field_str(stats, ST_F_ADDR))
1099 chunk_appendf(out, "%s, ", field_str(stats, ST_F_ADDR));
1100
1101 /* id */
1102 chunk_appendf(out, "id: %d", stats[ST_F_SID].u.u32);
1103
1104 /* cookie */
1105 if (stats[ST_F_COOKIE].type) {
1106 chunk_appendf(out, ", cookie: '");
1107 chunk_initstr(&src, field_str(stats, ST_F_COOKIE));
1108 chunk_htmlencode(out, &src);
1109 chunk_appendf(out, "'");
1110 }
1111
1112 chunk_appendf(out, "</div>");
1113 }
1114
1115 chunk_appendf(out,
1116 /* queue : current, max, limit */
1117 "%s</td><td>%s</td><td>%s</td><td>%s</td>"
1118 /* sessions rate : current, max, limit */
1119 "<td>%s</td><td>%s</td><td></td>"
1120 "",
Willy Tarreau708c4162019-10-09 10:19:16 +02001121 (flags & STAT_SHLGNDS) ? "</u>" : "",
William Lallemand74c24fb2016-11-21 17:18:36 +01001122 U2H(stats[ST_F_QCUR].u.u32), U2H(stats[ST_F_QMAX].u.u32), LIM2A(stats[ST_F_QLIMIT].u.u32, "-"),
1123 U2H(stats[ST_F_RATE].u.u32), U2H(stats[ST_F_RATE_MAX].u.u32));
1124
1125 chunk_appendf(out,
1126 /* sessions: current, max, limit, total */
Willy Tarreauf21d17b2019-09-08 09:24:56 +02001127 "<td><u>%s<div class=tips>"
1128 "<table class=det>"
1129 "<tr><th>Current active connections:</th><td>%s</td></tr>"
Willy Tarreau3bb617c2020-06-29 13:51:05 +02001130 "<tr><th>Current used connections:</th><td>%s</td></tr>"
Willy Tarreauf21d17b2019-09-08 09:24:56 +02001131 "<tr><th>Current idle connections:</th><td>%s</td></tr>"
Willy Tarreau3bb617c2020-06-29 13:51:05 +02001132 "<tr><th>- unsafe:</th><td>%s</td></tr>"
1133 "<tr><th>- safe:</th><td>%s</td></tr>"
Willy Tarreaua9fcecb2020-06-29 15:38:53 +02001134 "<tr><th>Estimated need of connections:</th><td>%s</td></tr>"
Willy Tarreauf21d17b2019-09-08 09:24:56 +02001135 "<tr><th>Active connections limit:</th><td>%s</td></tr>"
1136 "<tr><th>Idle connections limit:</th><td>%s</td></tr>"
1137 "</table></div></u>"
1138 "</td><td>%s</td><td>%s</td>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001139 "<td><u>%s<div class=tips><table class=det>"
1140 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
1141 "",
Willy Tarreauf21d17b2019-09-08 09:24:56 +02001142 U2H(stats[ST_F_SCUR].u.u32),
Willy Tarreau3bb617c2020-06-29 13:51:05 +02001143 U2H(stats[ST_F_SCUR].u.u32),
1144 U2H(stats[ST_F_USED_CONN_CUR].u.u32),
1145 U2H(stats[ST_F_SRV_ICUR].u.u32),
1146 U2H(stats[ST_F_IDLE_CONN_CUR].u.u32),
1147 U2H(stats[ST_F_SAFE_CONN_CUR].u.u32),
Willy Tarreaua9fcecb2020-06-29 15:38:53 +02001148 U2H(stats[ST_F_NEED_CONN_EST].u.u32),
Willy Tarreau3bb617c2020-06-29 13:51:05 +02001149
Willy Tarreauf21d17b2019-09-08 09:24:56 +02001150 LIM2A(stats[ST_F_SLIM].u.u32, "-"),
1151 stats[ST_F_SRV_ILIM].type ? U2H(stats[ST_F_SRV_ILIM].u.u32) : "-",
1152 U2H(stats[ST_F_SMAX].u.u32), LIM2A(stats[ST_F_SLIM].u.u32, "-"),
William Lallemand74c24fb2016-11-21 17:18:36 +01001153 U2H(stats[ST_F_STOT].u.u64),
1154 U2H(stats[ST_F_STOT].u.u64));
1155
1156 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
1157 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001158 chunk_appendf(out,
Willy Tarreauf1573842018-12-14 11:35:36 +01001159 "<tr><th>New connections:</th><td>%s</td></tr>"
1160 "<tr><th>Reused connections:</th><td>%s</td><td>(%d%%)</td></tr>"
Marcin Deranek3c27dda2020-05-15 18:32:51 +02001161 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001162 "<tr><th>- HTTP 1xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
1163 "<tr><th>- HTTP 2xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
1164 "<tr><th>- HTTP 3xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
1165 "<tr><th>- HTTP 4xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
1166 "<tr><th>- HTTP 5xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
1167 "<tr><th>- other responses:</th><td>%s</td><td>(%d%%)</td></tr>"
Willy Tarreau1b0f85e2018-05-28 15:12:40 +02001168 "<tr><th>Failed hdr rewrites:</th><td>%s</td></tr>"
Christopher Faulet0159ee42019-12-16 14:40:39 +01001169 "<tr><th>Internal error:</th><td>%s</td></tr>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001170 "",
Willy Tarreauf1573842018-12-14 11:35:36 +01001171 U2H(stats[ST_F_CONNECT].u.u64),
1172 U2H(stats[ST_F_REUSE].u.u64),
1173 (stats[ST_F_CONNECT].u.u64 + stats[ST_F_REUSE].u.u64) ?
1174 (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 +02001175 U2H(stats[ST_F_REQ_TOT].u.u64),
1176 U2H(stats[ST_F_HRSP_1XX].u.u64), stats[ST_F_REQ_TOT].u.u64 ?
1177 (int)(100 * stats[ST_F_HRSP_1XX].u.u64 / stats[ST_F_REQ_TOT].u.u64) : 0,
1178 U2H(stats[ST_F_HRSP_2XX].u.u64), stats[ST_F_REQ_TOT].u.u64 ?
1179 (int)(100 * stats[ST_F_HRSP_2XX].u.u64 / stats[ST_F_REQ_TOT].u.u64) : 0,
1180 U2H(stats[ST_F_HRSP_3XX].u.u64), stats[ST_F_REQ_TOT].u.u64 ?
1181 (int)(100 * stats[ST_F_HRSP_3XX].u.u64 / stats[ST_F_REQ_TOT].u.u64) : 0,
1182 U2H(stats[ST_F_HRSP_4XX].u.u64), stats[ST_F_REQ_TOT].u.u64 ?
1183 (int)(100 * stats[ST_F_HRSP_4XX].u.u64 / stats[ST_F_REQ_TOT].u.u64) : 0,
1184 U2H(stats[ST_F_HRSP_5XX].u.u64), stats[ST_F_REQ_TOT].u.u64 ?
1185 (int)(100 * stats[ST_F_HRSP_5XX].u.u64 / stats[ST_F_REQ_TOT].u.u64) : 0,
1186 U2H(stats[ST_F_HRSP_OTHER].u.u64), stats[ST_F_REQ_TOT].u.u64 ?
1187 (int)(100 * stats[ST_F_HRSP_OTHER].u.u64 / stats[ST_F_REQ_TOT].u.u64) : 0,
Christopher Faulet0159ee42019-12-16 14:40:39 +01001188 U2H(stats[ST_F_WREW].u.u64),
1189 U2H(stats[ST_F_EINT].u.u64));
William Lallemand74c24fb2016-11-21 17:18:36 +01001190 }
1191
Christopher Faulet0d1c2a62019-11-08 14:59:51 +01001192 chunk_appendf(out, "<tr><th colspan=3>Max / Avg over last 1024 success. conn.</th></tr>");
1193 chunk_appendf(out, "<tr><th>- Queue time:</th><td>%s / %s</td><td>ms</td></tr>",
1194 U2H(stats[ST_F_QT_MAX].u.u32), U2H(stats[ST_F_QTIME].u.u32));
1195 chunk_appendf(out, "<tr><th>- Connect time:</th><td>%s / %s</td><td>ms</td></tr>",
1196 U2H(stats[ST_F_CT_MAX].u.u32), U2H(stats[ST_F_CTIME].u.u32));
William Lallemand74c24fb2016-11-21 17:18:36 +01001197 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
Christopher Faulet0d1c2a62019-11-08 14:59:51 +01001198 chunk_appendf(out, "<tr><th>- Responses time:</th><td>%s / %s</td><td>ms</td></tr>",
1199 U2H(stats[ST_F_RT_MAX].u.u32), U2H(stats[ST_F_RTIME].u.u32));
1200 chunk_appendf(out, "<tr><th>- Total time:</th><td>%s / %s</td><td>ms</td></tr>",
1201 U2H(stats[ST_F_TT_MAX].u.u32), U2H(stats[ST_F_TTIME].u.u32));
William Lallemand74c24fb2016-11-21 17:18:36 +01001202
1203 chunk_appendf(out,
1204 "</table></div></u></td>"
1205 /* sessions: lbtot, last */
1206 "<td>%s</td><td>%s</td>",
1207 U2H(stats[ST_F_LBTOT].u.u64),
1208 human_time(stats[ST_F_LASTSESS].u.s32, 1));
1209
1210 chunk_appendf(out,
1211 /* bytes : in, out */
1212 "<td>%s</td><td>%s</td>"
1213 /* denied: req, resp */
1214 "<td></td><td>%s</td>"
1215 /* errors : request, connect */
1216 "<td></td><td>%s</td>"
1217 /* errors : response */
1218 "<td><u>%s<div class=tips>Connection resets during transfers: %lld client, %lld server</div></u></td>"
1219 /* warnings: retries, redispatches */
1220 "<td>%lld</td><td>%lld</td>"
1221 "",
1222 U2H(stats[ST_F_BIN].u.u64), U2H(stats[ST_F_BOUT].u.u64),
1223 U2H(stats[ST_F_DRESP].u.u64),
1224 U2H(stats[ST_F_ECON].u.u64),
1225 U2H(stats[ST_F_ERESP].u.u64),
1226 (long long)stats[ST_F_CLI_ABRT].u.u64,
1227 (long long)stats[ST_F_SRV_ABRT].u.u64,
1228 (long long)stats[ST_F_WRETR].u.u64,
1229 (long long)stats[ST_F_WREDIS].u.u64);
1230
1231 /* status, last change */
1232 chunk_appendf(out, "<td class=ac>");
1233
1234 /* FIXME!!!!
1235 * LASTCHG should contain the last change for *this* server and must be computed
1236 * properly above, as was done below, ie: this server if maint, otherwise ref server
1237 * if tracking. Note that ref is either local or remote depending on tracking.
1238 */
1239
1240
Willy Tarreau7b524852020-08-11 10:26:36 +02001241 if (strncmp(field_str(stats, ST_F_STATUS), "MAINT", 5) == 0) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001242 chunk_appendf(out, "%s MAINT", human_time(stats[ST_F_LASTCHG].u.u32, 1));
1243 }
Willy Tarreau7b524852020-08-11 10:26:36 +02001244 else if (strcmp(field_str(stats, ST_F_STATUS), "no check") == 0) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001245 chunk_strcat(out, "<i>no check</i>");
1246 }
1247 else {
1248 chunk_appendf(out, "%s %s", human_time(stats[ST_F_LASTCHG].u.u32, 1), field_str(stats, ST_F_STATUS));
Willy Tarreau7b524852020-08-11 10:26:36 +02001249 if (strncmp(field_str(stats, ST_F_STATUS), "DOWN", 4) == 0) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001250 if (stats[ST_F_CHECK_HEALTH].u.u32)
1251 chunk_strcat(out, " &uarr;");
1252 }
1253 else if (stats[ST_F_CHECK_HEALTH].u.u32 < stats[ST_F_CHECK_RISE].u.u32 + stats[ST_F_CHECK_FALL].u.u32 - 1)
1254 chunk_strcat(out, " &darr;");
1255 }
1256
Willy Tarreau7b524852020-08-11 10:26:36 +02001257 if (strncmp(field_str(stats, ST_F_STATUS), "DOWN", 4) == 0 &&
William Lallemand74c24fb2016-11-21 17:18:36 +01001258 stats[ST_F_AGENT_STATUS].type && !stats[ST_F_AGENT_HEALTH].u.u32) {
1259 chunk_appendf(out,
1260 "</td><td class=ac><u> %s",
1261 field_str(stats, ST_F_AGENT_STATUS));
1262
1263 if (stats[ST_F_AGENT_CODE].type)
1264 chunk_appendf(out, "/%d", stats[ST_F_AGENT_CODE].u.u32);
1265
1266 if (stats[ST_F_AGENT_DURATION].type)
1267 chunk_appendf(out, " in %lums", (long)stats[ST_F_AGENT_DURATION].u.u64);
1268
1269 chunk_appendf(out, "<div class=tips>%s", field_str(stats, ST_F_AGENT_DESC));
1270
1271 if (*field_str(stats, ST_F_LAST_AGT)) {
1272 chunk_appendf(out, ": ");
1273 chunk_initstr(&src, field_str(stats, ST_F_LAST_AGT));
1274 chunk_htmlencode(out, &src);
1275 }
1276 chunk_appendf(out, "</div></u>");
1277 }
1278 else if (stats[ST_F_CHECK_STATUS].type) {
1279 chunk_appendf(out,
1280 "</td><td class=ac><u> %s",
1281 field_str(stats, ST_F_CHECK_STATUS));
1282
1283 if (stats[ST_F_CHECK_CODE].type)
1284 chunk_appendf(out, "/%d", stats[ST_F_CHECK_CODE].u.u32);
1285
1286 if (stats[ST_F_CHECK_DURATION].type)
1287 chunk_appendf(out, " in %lums", (long)stats[ST_F_CHECK_DURATION].u.u64);
1288
1289 chunk_appendf(out, "<div class=tips>%s", field_str(stats, ST_F_CHECK_DESC));
1290
1291 if (*field_str(stats, ST_F_LAST_CHK)) {
1292 chunk_appendf(out, ": ");
1293 chunk_initstr(&src, field_str(stats, ST_F_LAST_CHK));
1294 chunk_htmlencode(out, &src);
1295 }
1296 chunk_appendf(out, "</div></u>");
1297 }
1298 else
1299 chunk_appendf(out, "</td><td>");
1300
1301 chunk_appendf(out,
1302 /* weight */
1303 "</td><td class=ac>%d</td>"
1304 /* act, bck */
1305 "<td class=ac>%s</td><td class=ac>%s</td>"
1306 "",
1307 stats[ST_F_WEIGHT].u.u32,
1308 stats[ST_F_BCK].u.u32 ? "-" : "Y",
1309 stats[ST_F_BCK].u.u32 ? "Y" : "-");
1310
1311 /* check failures: unique, fatal, down time */
1312 if (strcmp(field_str(stats, ST_F_STATUS), "MAINT (resolution)") == 0) {
1313 chunk_appendf(out, "<td class=ac colspan=3>resolution</td>");
1314 }
1315 else if (stats[ST_F_CHKFAIL].type) {
1316 chunk_appendf(out, "<td><u>%lld", (long long)stats[ST_F_CHKFAIL].u.u64);
1317
1318 if (stats[ST_F_HANAFAIL].type)
1319 chunk_appendf(out, "/%lld", (long long)stats[ST_F_HANAFAIL].u.u64);
1320
1321 chunk_appendf(out,
1322 "<div class=tips>Failed Health Checks%s</div></u></td>"
1323 "<td>%lld</td><td>%s</td>"
1324 "",
1325 stats[ST_F_HANAFAIL].type ? "/Health Analyses" : "",
1326 (long long)stats[ST_F_CHKDOWN].u.u64, human_time(stats[ST_F_DOWNTIME].u.u32, 1));
1327 }
1328 else if (strcmp(field_str(stats, ST_F_STATUS), "MAINT") != 0 && field_format(stats, ST_F_TRACKED) == FF_STR) {
1329 /* tracking a server (hence inherited maint would appear as "MAINT (via...)" */
1330 chunk_appendf(out,
1331 "<td class=ac colspan=3><a class=lfsb href=\"#%s\">via %s</a></td>",
1332 field_str(stats, ST_F_TRACKED), field_str(stats, ST_F_TRACKED));
1333 }
1334 else
1335 chunk_appendf(out, "<td colspan=3></td>");
1336
1337 /* throttle */
1338 if (stats[ST_F_THROTTLE].type)
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001339 chunk_appendf(out, "<td class=ac>%d %%</td>\n", stats[ST_F_THROTTLE].u.u32);
William Lallemand74c24fb2016-11-21 17:18:36 +01001340 else
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001341 chunk_appendf(out, "<td class=ac>-</td>");
1342
1343 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
1344 chunk_appendf(out,
1345 "<td><u>%s<div class=tips><table class=det>",
1346 mod->name);
1347 if (stats_px_get_cap(mod->domain_flags) & STATS_PX_CAP_SRV) {
1348 for (j = 0; j < mod->stats_count; ++j) {
1349 chunk_appendf(out,
1350 "<tr><th>%s</th><td>%s</td></tr>",
1351 mod->stats[j].desc, field_to_html_str(&stats[ST_F_TOTAL_FIELDS + i]));
1352 ++i;
1353 }
1354 } else {
1355 i += mod->stats_count;
1356 }
1357
1358 chunk_appendf(out,
1359 "</table></div></u></td>");
1360 }
1361
1362 chunk_appendf(out, "</tr>\n");
William Lallemand74c24fb2016-11-21 17:18:36 +01001363 }
1364 else if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_BE) {
1365 chunk_appendf(out, "<tr class=\"backend\">");
Willy Tarreauab02b3f2019-10-09 11:11:46 +02001366 if (flags & STAT_ADMIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001367 /* Column sub-heading for Enable or Disable server */
1368 chunk_appendf(out, "<td></td>");
1369 }
1370 chunk_appendf(out,
1371 "<td class=ac>"
1372 /* name */
1373 "%s<a name=\"%s/Backend\"></a>"
1374 "<a class=lfsb href=\"#%s/Backend\">Backend</a>"
1375 "",
Willy Tarreau708c4162019-10-09 10:19:16 +02001376 (flags & STAT_SHLGNDS)?"<u>":"",
William Lallemand74c24fb2016-11-21 17:18:36 +01001377 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_PXNAME));
1378
Willy Tarreau708c4162019-10-09 10:19:16 +02001379 if (flags & STAT_SHLGNDS) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001380 /* balancing */
1381 chunk_appendf(out, "<div class=tips>balancing: %s",
1382 field_str(stats, ST_F_ALGO));
1383
1384 /* cookie */
1385 if (stats[ST_F_COOKIE].type) {
1386 chunk_appendf(out, ", cookie: '");
1387 chunk_initstr(&src, field_str(stats, ST_F_COOKIE));
1388 chunk_htmlencode(out, &src);
1389 chunk_appendf(out, "'");
1390 }
1391 chunk_appendf(out, "</div>");
1392 }
1393
1394 chunk_appendf(out,
1395 "%s</td>"
1396 /* queue : current, max */
1397 "<td>%s</td><td>%s</td><td></td>"
1398 /* sessions rate : current, max, limit */
1399 "<td>%s</td><td>%s</td><td></td>"
1400 "",
Willy Tarreau708c4162019-10-09 10:19:16 +02001401 (flags & STAT_SHLGNDS)?"</u>":"",
William Lallemand74c24fb2016-11-21 17:18:36 +01001402 U2H(stats[ST_F_QCUR].u.u32), U2H(stats[ST_F_QMAX].u.u32),
1403 U2H(stats[ST_F_RATE].u.u32), U2H(stats[ST_F_RATE_MAX].u.u32));
1404
1405 chunk_appendf(out,
1406 /* sessions: current, max, limit, total */
1407 "<td>%s</td><td>%s</td><td>%s</td>"
1408 "<td><u>%s<div class=tips><table class=det>"
1409 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
1410 "",
Willy Tarreau8e0f1752016-12-12 15:07:29 +01001411 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 +01001412 U2H(stats[ST_F_STOT].u.u64),
1413 U2H(stats[ST_F_STOT].u.u64));
1414
1415 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
1416 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0) {
1417 chunk_appendf(out,
Willy Tarreauf1573842018-12-14 11:35:36 +01001418 "<tr><th>New connections:</th><td>%s</td></tr>"
1419 "<tr><th>Reused connections:</th><td>%s</td><td>(%d%%)</td></tr>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001420 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
1421 "<tr><th>- HTTP 1xx responses:</th><td>%s</td></tr>"
1422 "<tr><th>- HTTP 2xx responses:</th><td>%s</td></tr>"
1423 "<tr><th>&nbsp;&nbsp;Compressed 2xx:</th><td>%s</td><td>(%d%%)</td></tr>"
1424 "<tr><th>- HTTP 3xx responses:</th><td>%s</td></tr>"
1425 "<tr><th>- HTTP 4xx responses:</th><td>%s</td></tr>"
1426 "<tr><th>- HTTP 5xx responses:</th><td>%s</td></tr>"
1427 "<tr><th>- other responses:</th><td>%s</td></tr>"
Willy Tarreaua1214a52018-12-14 14:00:25 +01001428 "<tr><th>Cache lookups:</th><td>%s</td></tr>"
1429 "<tr><th>Cache hits:</th><td>%s</td><td>(%d%%)</td></tr>"
Willy Tarreau1b0f85e2018-05-28 15:12:40 +02001430 "<tr><th>Failed hdr rewrites:</th><td>%s</td></tr>"
Christopher Faulet0159ee42019-12-16 14:40:39 +01001431 "<tr><th>Internal errors:</th><td>%s</td></tr>"
Christopher Faulet0d1c2a62019-11-08 14:59:51 +01001432 "",
Willy Tarreauf1573842018-12-14 11:35:36 +01001433 U2H(stats[ST_F_CONNECT].u.u64),
1434 U2H(stats[ST_F_REUSE].u.u64),
1435 (stats[ST_F_CONNECT].u.u64 + stats[ST_F_REUSE].u.u64) ?
1436 (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 +01001437 U2H(stats[ST_F_REQ_TOT].u.u64),
1438 U2H(stats[ST_F_HRSP_1XX].u.u64),
1439 U2H(stats[ST_F_HRSP_2XX].u.u64),
1440 U2H(stats[ST_F_COMP_RSP].u.u64),
1441 stats[ST_F_HRSP_2XX].u.u64 ?
1442 (int)(100 * stats[ST_F_COMP_RSP].u.u64 / stats[ST_F_HRSP_2XX].u.u64) : 0,
1443 U2H(stats[ST_F_HRSP_3XX].u.u64),
1444 U2H(stats[ST_F_HRSP_4XX].u.u64),
1445 U2H(stats[ST_F_HRSP_5XX].u.u64),
Willy Tarreaufeead3a2018-12-14 13:48:44 +01001446 U2H(stats[ST_F_HRSP_OTHER].u.u64),
Willy Tarreaua1214a52018-12-14 14:00:25 +01001447 U2H(stats[ST_F_CACHE_LOOKUPS].u.u64),
1448 U2H(stats[ST_F_CACHE_HITS].u.u64),
1449 stats[ST_F_CACHE_LOOKUPS].u.u64 ?
1450 (int)(100 * stats[ST_F_CACHE_HITS].u.u64 / stats[ST_F_CACHE_LOOKUPS].u.u64) : 0,
Christopher Faulet0159ee42019-12-16 14:40:39 +01001451 U2H(stats[ST_F_WREW].u.u64),
1452 U2H(stats[ST_F_EINT].u.u64));
William Lallemand74c24fb2016-11-21 17:18:36 +01001453 }
1454
Christopher Faulet0d1c2a62019-11-08 14:59:51 +01001455 chunk_appendf(out, "<tr><th colspan=3>Max / Avg over last 1024 success. conn.</th></tr>");
1456 chunk_appendf(out, "<tr><th>- Queue time:</th><td>%s / %s</td><td>ms</td></tr>",
1457 U2H(stats[ST_F_QT_MAX].u.u32), U2H(stats[ST_F_QTIME].u.u32));
1458 chunk_appendf(out, "<tr><th>- Connect time:</th><td>%s / %s</td><td>ms</td></tr>",
1459 U2H(stats[ST_F_CT_MAX].u.u32), U2H(stats[ST_F_CTIME].u.u32));
William Lallemand74c24fb2016-11-21 17:18:36 +01001460 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
Christopher Faulet0d1c2a62019-11-08 14:59:51 +01001461 chunk_appendf(out, "<tr><th>- Responses time:</th><td>%s / %s</td><td>ms</td></tr>",
1462 U2H(stats[ST_F_RT_MAX].u.u32), U2H(stats[ST_F_RTIME].u.u32));
1463 chunk_appendf(out, "<tr><th>- Total time:</th><td>%s / %s</td><td>ms</td></tr>",
1464 U2H(stats[ST_F_TT_MAX].u.u32), U2H(stats[ST_F_TTIME].u.u32));
William Lallemand74c24fb2016-11-21 17:18:36 +01001465
1466 chunk_appendf(out,
1467 "</table></div></u></td>"
1468 /* sessions: lbtot, last */
1469 "<td>%s</td><td>%s</td>"
1470 /* bytes: in */
1471 "<td>%s</td>"
1472 "",
1473 U2H(stats[ST_F_LBTOT].u.u64),
1474 human_time(stats[ST_F_LASTSESS].u.s32, 1),
1475 U2H(stats[ST_F_BIN].u.u64));
1476
1477 chunk_appendf(out,
1478 /* bytes:out + compression stats (via hover): comp_in, comp_out, comp_byp */
1479 "<td>%s%s<div class=tips><table class=det>"
1480 "<tr><th>Response bytes in:</th><td>%s</td></tr>"
1481 "<tr><th>Compression in:</th><td>%s</td></tr>"
1482 "<tr><th>Compression out:</th><td>%s</td><td>(%d%%)</td></tr>"
1483 "<tr><th>Compression bypass:</th><td>%s</td></tr>"
1484 "<tr><th>Total bytes saved:</th><td>%s</td><td>(%d%%)</td></tr>"
1485 "</table></div>%s</td>",
1486 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "<u>":"",
1487 U2H(stats[ST_F_BOUT].u.u64),
1488 U2H(stats[ST_F_BOUT].u.u64),
1489 U2H(stats[ST_F_COMP_IN].u.u64),
1490 U2H(stats[ST_F_COMP_OUT].u.u64),
1491 stats[ST_F_COMP_IN].u.u64 ? (int)(stats[ST_F_COMP_OUT].u.u64 * 100 / stats[ST_F_COMP_IN].u.u64) : 0,
1492 U2H(stats[ST_F_COMP_BYP].u.u64),
1493 U2H(stats[ST_F_COMP_IN].u.u64 - stats[ST_F_COMP_OUT].u.u64),
1494 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,
1495 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "</u>":"");
1496
1497 chunk_appendf(out,
1498 /* denied: req, resp */
1499 "<td>%s</td><td>%s</td>"
1500 /* errors : request, connect */
1501 "<td></td><td>%s</td>"
1502 /* errors : response */
1503 "<td><u>%s<div class=tips>Connection resets during transfers: %lld client, %lld server</div></u></td>"
1504 /* warnings: retries, redispatches */
1505 "<td>%lld</td><td>%lld</td>"
1506 /* backend status: reflect backend status (up/down): we display UP
1507 * if the backend has known working servers or if it has no server at
1508 * all (eg: for stats). Then we display the total weight, number of
1509 * active and backups. */
1510 "<td class=ac>%s %s</td><td class=ac>&nbsp;</td><td class=ac>%d</td>"
1511 "<td class=ac>%d</td><td class=ac>%d</td>"
1512 "",
1513 U2H(stats[ST_F_DREQ].u.u64), U2H(stats[ST_F_DRESP].u.u64),
1514 U2H(stats[ST_F_ECON].u.u64),
1515 U2H(stats[ST_F_ERESP].u.u64),
1516 (long long)stats[ST_F_CLI_ABRT].u.u64,
1517 (long long)stats[ST_F_SRV_ABRT].u.u64,
1518 (long long)stats[ST_F_WRETR].u.u64, (long long)stats[ST_F_WREDIS].u.u64,
1519 human_time(stats[ST_F_LASTCHG].u.u32, 1),
1520 strcmp(field_str(stats, ST_F_STATUS), "DOWN") ? field_str(stats, ST_F_STATUS) : "<font color=\"red\"><b>DOWN</b></font>",
1521 stats[ST_F_WEIGHT].u.u32,
1522 stats[ST_F_ACT].u.u32, stats[ST_F_BCK].u.u32);
1523
1524 chunk_appendf(out,
1525 /* rest of backend: nothing, down transitions, total downtime, throttle */
1526 "<td class=ac>&nbsp;</td><td>%d</td>"
1527 "<td>%s</td>"
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001528 "<td></td>",
William Lallemand74c24fb2016-11-21 17:18:36 +01001529 stats[ST_F_CHKDOWN].u.u32,
1530 stats[ST_F_DOWNTIME].type ? human_time(stats[ST_F_DOWNTIME].u.u32, 1) : "&nbsp;");
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001531
1532 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
1533 chunk_appendf(out,
1534 "<td><u>%s<div class=tips><table class=det>",
1535 mod->name);
1536 if (stats_px_get_cap(mod->domain_flags) & STATS_PX_CAP_BE) {
1537 for (j = 0; j < mod->stats_count; ++j) {
1538 chunk_appendf(out,
1539 "<tr><th>%s</th><td>%s</td></tr>",
1540 mod->stats[j].desc, field_to_html_str(&stats[ST_F_TOTAL_FIELDS + i]));
1541 ++i;
1542 }
1543 } else {
1544 i += mod->stats_count;
1545 }
1546 chunk_appendf(out,
1547 "</table></div></u></td>");
1548 }
1549 chunk_appendf(out, "</tr>");
William Lallemand74c24fb2016-11-21 17:18:36 +01001550 }
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001551
William Lallemand74c24fb2016-11-21 17:18:36 +01001552 return 1;
1553}
1554
Amaury Denoyelle97323c92020-10-02 18:32:01 +02001555int stats_dump_one_line(const struct field *stats, size_t stats_count,
1556 struct appctx *appctx)
William Lallemand74c24fb2016-11-21 17:18:36 +01001557{
Simon Horman05ee2132017-01-04 09:37:25 +01001558 int ret;
1559
William Lallemand74c24fb2016-11-21 17:18:36 +01001560 if (appctx->ctx.stats.flags & STAT_FMT_HTML)
Willy Tarreau43241ff2019-10-09 11:27:51 +02001561 ret = stats_dump_fields_html(&trash, stats, appctx->ctx.stats.flags);
William Lallemand74c24fb2016-11-21 17:18:36 +01001562 else if (appctx->ctx.stats.flags & STAT_FMT_TYPED)
Amaury Denoyelle072f97e2020-10-05 11:49:37 +02001563 ret = stats_dump_fields_typed(&trash, stats, stats_count, appctx->ctx.stats.flags, appctx->ctx.stats.domain);
Simon Horman05ee2132017-01-04 09:37:25 +01001564 else if (appctx->ctx.stats.flags & STAT_FMT_JSON)
Amaury Denoyelle072f97e2020-10-05 11:49:37 +02001565 ret = stats_dump_fields_json(&trash, stats, stats_count, appctx->ctx.stats.flags, appctx->ctx.stats.domain);
William Lallemand74c24fb2016-11-21 17:18:36 +01001566 else
Amaury Denoyelle50660a82020-10-05 11:49:39 +02001567 ret = stats_dump_fields_csv(&trash, stats, stats_count, appctx->ctx.stats.flags, appctx->ctx.stats.domain);
Simon Horman05ee2132017-01-04 09:37:25 +01001568
1569 if (ret)
1570 appctx->ctx.stats.flags |= STAT_STARTED;
1571
1572 return ret;
William Lallemand74c24fb2016-11-21 17:18:36 +01001573}
1574
1575/* Fill <stats> with the frontend statistics. <stats> is
1576 * preallocated array of length <len>. The length of the array
1577 * must be at least ST_F_TOTAL_FIELDS. If this length is less then
1578 * this value, the function returns 0, otherwise, it returns 1.
1579 */
1580int stats_fill_fe_stats(struct proxy *px, struct field *stats, int len)
1581{
1582 if (len < ST_F_TOTAL_FIELDS)
1583 return 0;
1584
William Lallemand74c24fb2016-11-21 17:18:36 +01001585 stats[ST_F_PXNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
1586 stats[ST_F_SVNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, "FRONTEND");
1587 stats[ST_F_MODE] = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
1588 stats[ST_F_SCUR] = mkf_u32(0, px->feconn);
1589 stats[ST_F_SMAX] = mkf_u32(FN_MAX, px->fe_counters.conn_max);
1590 stats[ST_F_SLIM] = mkf_u32(FO_CONFIG|FN_LIMIT, px->maxconn);
1591 stats[ST_F_STOT] = mkf_u64(FN_COUNTER, px->fe_counters.cum_sess);
1592 stats[ST_F_BIN] = mkf_u64(FN_COUNTER, px->fe_counters.bytes_in);
1593 stats[ST_F_BOUT] = mkf_u64(FN_COUNTER, px->fe_counters.bytes_out);
1594 stats[ST_F_DREQ] = mkf_u64(FN_COUNTER, px->fe_counters.denied_req);
1595 stats[ST_F_DRESP] = mkf_u64(FN_COUNTER, px->fe_counters.denied_resp);
1596 stats[ST_F_EREQ] = mkf_u64(FN_COUNTER, px->fe_counters.failed_req);
1597 stats[ST_F_DCON] = mkf_u64(FN_COUNTER, px->fe_counters.denied_conn);
1598 stats[ST_F_DSES] = mkf_u64(FN_COUNTER, px->fe_counters.denied_sess);
1599 stats[ST_F_STATUS] = mkf_str(FO_STATUS, px->state == PR_STREADY ? "OPEN" : px->state == PR_STFULL ? "FULL" : "STOP");
1600 stats[ST_F_PID] = mkf_u32(FO_KEY, relative_pid);
1601 stats[ST_F_IID] = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
1602 stats[ST_F_SID] = mkf_u32(FO_KEY|FS_SERVICE, 0);
1603 stats[ST_F_TYPE] = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_FE);
1604 stats[ST_F_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&px->fe_sess_per_sec));
1605 stats[ST_F_RATE_LIM] = mkf_u32(FO_CONFIG|FN_LIMIT, px->fe_sps_lim);
1606 stats[ST_F_RATE_MAX] = mkf_u32(FN_MAX, px->fe_counters.sps_max);
Tim Duesterhus3fd19732018-05-27 20:35:08 +02001607 stats[ST_F_WREW] = mkf_u64(FN_COUNTER, px->fe_counters.failed_rewrites);
Christopher Faulet0159ee42019-12-16 14:40:39 +01001608 stats[ST_F_EINT] = mkf_u64(FN_COUNTER, px->fe_counters.internal_errors);
William Lallemand74c24fb2016-11-21 17:18:36 +01001609
1610 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
1611 if (px->mode == PR_MODE_HTTP) {
1612 stats[ST_F_HRSP_1XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[1]);
1613 stats[ST_F_HRSP_2XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[2]);
1614 stats[ST_F_HRSP_3XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[3]);
1615 stats[ST_F_HRSP_4XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[4]);
1616 stats[ST_F_HRSP_5XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[5]);
1617 stats[ST_F_HRSP_OTHER] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[0]);
1618 stats[ST_F_INTERCEPTED] = mkf_u64(FN_COUNTER, px->fe_counters.intercepted_req);
Willy Tarreaua1214a52018-12-14 14:00:25 +01001619 stats[ST_F_CACHE_LOOKUPS] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.cache_lookups);
1620 stats[ST_F_CACHE_HITS] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.cache_hits);
William Lallemand74c24fb2016-11-21 17:18:36 +01001621 }
1622
1623 /* requests : req_rate, req_rate_max, req_tot, */
1624 stats[ST_F_REQ_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&px->fe_req_per_sec));
1625 stats[ST_F_REQ_RATE_MAX] = mkf_u32(FN_MAX, px->fe_counters.p.http.rps_max);
1626 stats[ST_F_REQ_TOT] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.cum_req);
1627
1628 /* compression: in, out, bypassed, responses */
1629 stats[ST_F_COMP_IN] = mkf_u64(FN_COUNTER, px->fe_counters.comp_in);
1630 stats[ST_F_COMP_OUT] = mkf_u64(FN_COUNTER, px->fe_counters.comp_out);
1631 stats[ST_F_COMP_BYP] = mkf_u64(FN_COUNTER, px->fe_counters.comp_byp);
1632 stats[ST_F_COMP_RSP] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.comp_rsp);
1633
1634 /* connections : conn_rate, conn_rate_max, conn_tot, conn_max */
1635 stats[ST_F_CONN_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&px->fe_conn_per_sec));
1636 stats[ST_F_CONN_RATE_MAX] = mkf_u32(FN_MAX, px->fe_counters.cps_max);
1637 stats[ST_F_CONN_TOT] = mkf_u64(FN_COUNTER, px->fe_counters.cum_conn);
1638
1639 return 1;
1640}
1641
1642/* Dumps a frontend's line to the trash for the current proxy <px> and uses
1643 * the state from stream interface <si>. The caller is responsible for clearing
1644 * the trash if needed. Returns non-zero if it emits anything, zero otherwise.
1645 */
1646static int stats_dump_fe_stats(struct stream_interface *si, struct proxy *px)
1647{
1648 struct appctx *appctx = __objt_appctx(si->end);
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02001649 struct field *stats = stat_l[STATS_DOMAIN_PROXY];
1650 struct stats_module *mod;
1651 size_t stats_count = ST_F_TOTAL_FIELDS;
William Lallemand74c24fb2016-11-21 17:18:36 +01001652
1653 if (!(px->cap & PR_CAP_FE))
1654 return 0;
1655
1656 if ((appctx->ctx.stats.flags & STAT_BOUND) && !(appctx->ctx.stats.type & (1 << STATS_TYPE_FE)))
1657 return 0;
1658
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02001659 memset(stats, 0, sizeof(struct field) * stat_count[STATS_DOMAIN_PROXY]);
1660
William Lallemand74c24fb2016-11-21 17:18:36 +01001661 if (!stats_fill_fe_stats(px, stats, ST_F_TOTAL_FIELDS))
1662 return 0;
1663
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02001664 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
1665 void *counters;
1666
1667 if (!(stats_px_get_cap(mod->domain_flags) & STATS_PX_CAP_FE)) {
1668 stats_count += mod->stats_count;
1669 continue;
1670 }
1671
1672 counters = EXTRA_COUNTERS_GET(px->extra_counters_fe, mod);
1673 mod->fill_stats(counters, stats + stats_count);
1674 stats_count += mod->stats_count;
1675 }
1676
1677 return stats_dump_one_line(stats, stats_count, appctx);
William Lallemand74c24fb2016-11-21 17:18:36 +01001678}
1679
1680/* Fill <stats> with the listener statistics. <stats> is
1681 * preallocated array of length <len>. The length of the array
1682 * must be at least ST_F_TOTAL_FIELDS. If this length is less
1683 * then this value, the function returns 0, otherwise, it
Willy Tarreau708c4162019-10-09 10:19:16 +02001684 * returns 1. <flags> can take the value STAT_SHLGNDS.
William Lallemand74c24fb2016-11-21 17:18:36 +01001685 */
1686int stats_fill_li_stats(struct proxy *px, struct listener *l, int flags,
1687 struct field *stats, int len)
1688{
Willy Tarreau83061a82018-07-13 11:56:34 +02001689 struct buffer *out = get_trash_chunk();
William Lallemand74c24fb2016-11-21 17:18:36 +01001690
1691 if (len < ST_F_TOTAL_FIELDS)
1692 return 0;
1693
1694 if (!l->counters)
1695 return 0;
1696
1697 chunk_reset(out);
William Lallemand74c24fb2016-11-21 17:18:36 +01001698
1699 stats[ST_F_PXNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
1700 stats[ST_F_SVNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, l->name);
1701 stats[ST_F_MODE] = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
1702 stats[ST_F_SCUR] = mkf_u32(0, l->nbconn);
1703 stats[ST_F_SMAX] = mkf_u32(FN_MAX, l->counters->conn_max);
1704 stats[ST_F_SLIM] = mkf_u32(FO_CONFIG|FN_LIMIT, l->maxconn);
1705 stats[ST_F_STOT] = mkf_u64(FN_COUNTER, l->counters->cum_conn);
1706 stats[ST_F_BIN] = mkf_u64(FN_COUNTER, l->counters->bytes_in);
1707 stats[ST_F_BOUT] = mkf_u64(FN_COUNTER, l->counters->bytes_out);
1708 stats[ST_F_DREQ] = mkf_u64(FN_COUNTER, l->counters->denied_req);
1709 stats[ST_F_DRESP] = mkf_u64(FN_COUNTER, l->counters->denied_resp);
1710 stats[ST_F_EREQ] = mkf_u64(FN_COUNTER, l->counters->failed_req);
1711 stats[ST_F_DCON] = mkf_u64(FN_COUNTER, l->counters->denied_conn);
1712 stats[ST_F_DSES] = mkf_u64(FN_COUNTER, l->counters->denied_sess);
Willy Tarreaua8cf66b2019-02-27 16:49:00 +01001713 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 +01001714 stats[ST_F_PID] = mkf_u32(FO_KEY, relative_pid);
1715 stats[ST_F_IID] = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
1716 stats[ST_F_SID] = mkf_u32(FO_KEY|FS_SERVICE, l->luid);
1717 stats[ST_F_TYPE] = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_SO);
Tim Duesterhus3fd19732018-05-27 20:35:08 +02001718 stats[ST_F_WREW] = mkf_u64(FN_COUNTER, l->counters->failed_rewrites);
Christopher Faulet0159ee42019-12-16 14:40:39 +01001719 stats[ST_F_EINT] = mkf_u64(FN_COUNTER, l->counters->internal_errors);
William Lallemand74c24fb2016-11-21 17:18:36 +01001720
Willy Tarreau708c4162019-10-09 10:19:16 +02001721 if (flags & STAT_SHLGNDS) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001722 char str[INET6_ADDRSTRLEN];
1723 int port;
1724
Willy Tarreau37159062020-08-27 07:48:42 +02001725 port = get_host_port(&l->rx.addr);
1726 switch (addr_to_str(&l->rx.addr, str, sizeof(str))) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001727 case AF_INET:
1728 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
1729 chunk_appendf(out, "%s:%d", str, port);
1730 break;
1731 case AF_INET6:
1732 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
1733 chunk_appendf(out, "[%s]:%d", str, port);
1734 break;
1735 case AF_UNIX:
1736 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, "unix");
1737 break;
1738 case -1:
1739 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
1740 chunk_strcat(out, strerror(errno));
1741 break;
1742 default: /* address family not supported */
1743 break;
1744 }
1745 }
1746
1747 return 1;
1748}
1749
1750/* Dumps a line for listener <l> and proxy <px> to the trash and uses the state
Willy Tarreau43241ff2019-10-09 11:27:51 +02001751 * from stream interface <si>. The caller is responsible for clearing the trash
1752 * if needed. Returns non-zero if it emits anything, zero otherwise.
William Lallemand74c24fb2016-11-21 17:18:36 +01001753 */
Willy Tarreau43241ff2019-10-09 11:27:51 +02001754static int stats_dump_li_stats(struct stream_interface *si, struct proxy *px, struct listener *l)
William Lallemand74c24fb2016-11-21 17:18:36 +01001755{
1756 struct appctx *appctx = __objt_appctx(si->end);
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02001757 struct field *stats = stat_l[STATS_DOMAIN_PROXY];
1758 struct stats_module *mod;
1759 size_t stats_count = ST_F_TOTAL_FIELDS;
1760
1761 memset(stats, 0, sizeof(struct field) * stat_count[STATS_DOMAIN_PROXY]);
William Lallemand74c24fb2016-11-21 17:18:36 +01001762
Willy Tarreau43241ff2019-10-09 11:27:51 +02001763 if (!stats_fill_li_stats(px, l, appctx->ctx.stats.flags, stats, ST_F_TOTAL_FIELDS))
William Lallemand74c24fb2016-11-21 17:18:36 +01001764 return 0;
1765
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02001766 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
1767 void *counters;
1768
1769 if (!(stats_px_get_cap(mod->domain_flags) & STATS_PX_CAP_LI)) {
1770 stats_count += mod->stats_count;
1771 continue;
1772 }
1773
1774 counters = EXTRA_COUNTERS_GET(l->extra_counters, mod);
1775 mod->fill_stats(counters, stats + stats_count);
1776 stats_count += mod->stats_count;
1777 }
1778
1779 return stats_dump_one_line(stats, stats_count, appctx);
William Lallemand74c24fb2016-11-21 17:18:36 +01001780}
1781
1782enum srv_stats_state {
1783 SRV_STATS_STATE_DOWN = 0,
1784 SRV_STATS_STATE_DOWN_AGENT,
1785 SRV_STATS_STATE_GOING_UP,
1786 SRV_STATS_STATE_UP_GOING_DOWN,
1787 SRV_STATS_STATE_UP,
1788 SRV_STATS_STATE_NOLB_GOING_DOWN,
1789 SRV_STATS_STATE_NOLB,
1790 SRV_STATS_STATE_DRAIN_GOING_DOWN,
1791 SRV_STATS_STATE_DRAIN,
1792 SRV_STATS_STATE_DRAIN_AGENT,
1793 SRV_STATS_STATE_NO_CHECK,
1794
1795 SRV_STATS_STATE_COUNT, /* Must be last */
1796};
1797
1798static const char *srv_hlt_st[SRV_STATS_STATE_COUNT] = {
1799 [SRV_STATS_STATE_DOWN] = "DOWN",
1800 [SRV_STATS_STATE_DOWN_AGENT] = "DOWN (agent)",
1801 [SRV_STATS_STATE_GOING_UP] = "DOWN %d/%d",
1802 [SRV_STATS_STATE_UP_GOING_DOWN] = "UP %d/%d",
1803 [SRV_STATS_STATE_UP] = "UP",
1804 [SRV_STATS_STATE_NOLB_GOING_DOWN] = "NOLB %d/%d",
1805 [SRV_STATS_STATE_NOLB] = "NOLB",
1806 [SRV_STATS_STATE_DRAIN_GOING_DOWN] = "DRAIN %d/%d",
1807 [SRV_STATS_STATE_DRAIN] = "DRAIN",
1808 [SRV_STATS_STATE_DRAIN_AGENT] = "DRAIN (agent)",
1809 [SRV_STATS_STATE_NO_CHECK] = "no check"
1810};
1811
1812/* Fill <stats> with the server statistics. <stats> is
1813 * preallocated array of length <len>. The length of the array
1814 * must be at least ST_F_TOTAL_FIELDS. If this length is less
1815 * then this value, the function returns 0, otherwise, it
Willy Tarreau708c4162019-10-09 10:19:16 +02001816 * returns 1. <flags> can take the value STAT_SHLGNDS.
William Lallemand74c24fb2016-11-21 17:18:36 +01001817 */
1818int stats_fill_sv_stats(struct proxy *px, struct server *sv, int flags,
1819 struct field *stats, int len)
1820{
1821 struct server *via, *ref;
1822 char str[INET6_ADDRSTRLEN];
Willy Tarreau83061a82018-07-13 11:56:34 +02001823 struct buffer *out = get_trash_chunk();
William Lallemand74c24fb2016-11-21 17:18:36 +01001824 enum srv_stats_state state;
1825 char *fld_status;
Marcin Deraneka8dbdf32020-05-15 20:02:40 +02001826 long long srv_samples_counter;
1827 unsigned int srv_samples_window = TIME_STATS_SAMPLES;
William Lallemand74c24fb2016-11-21 17:18:36 +01001828
1829 if (len < ST_F_TOTAL_FIELDS)
1830 return 0;
1831
William Lallemand74c24fb2016-11-21 17:18:36 +01001832 /* we have "via" which is the tracked server as described in the configuration,
1833 * and "ref" which is the checked server and the end of the chain.
1834 */
1835 via = sv->track ? sv->track : sv;
1836 ref = via;
1837 while (ref->track)
1838 ref = ref->track;
1839
Emeric Brun52a91d32017-08-31 14:41:55 +02001840 if (sv->cur_state == SRV_ST_RUNNING || sv->cur_state == SRV_ST_STARTING) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001841 if ((ref->check.state & CHK_ST_ENABLED) &&
1842 (ref->check.health < ref->check.rise + ref->check.fall - 1)) {
1843 state = SRV_STATS_STATE_UP_GOING_DOWN;
1844 } else {
1845 state = SRV_STATS_STATE_UP;
1846 }
1847
Emeric Brun52a91d32017-08-31 14:41:55 +02001848 if (sv->cur_admin & SRV_ADMF_DRAIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001849 if (ref->agent.state & CHK_ST_ENABLED)
1850 state = SRV_STATS_STATE_DRAIN_AGENT;
1851 else if (state == SRV_STATS_STATE_UP_GOING_DOWN)
1852 state = SRV_STATS_STATE_DRAIN_GOING_DOWN;
1853 else
1854 state = SRV_STATS_STATE_DRAIN;
1855 }
1856
1857 if (state == SRV_STATS_STATE_UP && !(ref->check.state & CHK_ST_ENABLED)) {
1858 state = SRV_STATS_STATE_NO_CHECK;
1859 }
1860 }
Emeric Brun52a91d32017-08-31 14:41:55 +02001861 else if (sv->cur_state == SRV_ST_STOPPING) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001862 if ((!(sv->check.state & CHK_ST_ENABLED) && !sv->track) ||
1863 (ref->check.health == ref->check.rise + ref->check.fall - 1)) {
1864 state = SRV_STATS_STATE_NOLB;
1865 } else {
1866 state = SRV_STATS_STATE_NOLB_GOING_DOWN;
1867 }
1868 }
1869 else { /* stopped */
1870 if ((ref->agent.state & CHK_ST_ENABLED) && !ref->agent.health) {
1871 state = SRV_STATS_STATE_DOWN_AGENT;
1872 } else if ((ref->check.state & CHK_ST_ENABLED) && !ref->check.health) {
1873 state = SRV_STATS_STATE_DOWN; /* DOWN */
1874 } else if ((ref->agent.state & CHK_ST_ENABLED) || (ref->check.state & CHK_ST_ENABLED)) {
1875 state = SRV_STATS_STATE_GOING_UP;
1876 } else {
1877 state = SRV_STATS_STATE_DOWN; /* DOWN, unchecked */
1878 }
1879 }
1880
1881 chunk_reset(out);
1882
1883 stats[ST_F_PXNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
1884 stats[ST_F_SVNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, sv->id);
1885 stats[ST_F_MODE] = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
1886 stats[ST_F_QCUR] = mkf_u32(0, sv->nbpend);
1887 stats[ST_F_QMAX] = mkf_u32(FN_MAX, sv->counters.nbpend_max);
1888 stats[ST_F_SCUR] = mkf_u32(0, sv->cur_sess);
1889 stats[ST_F_SMAX] = mkf_u32(FN_MAX, sv->counters.cur_sess_max);
1890
1891 if (sv->maxconn)
1892 stats[ST_F_SLIM] = mkf_u32(FO_CONFIG|FN_LIMIT, sv->maxconn);
1893
Willy Tarreauf21d17b2019-09-08 09:24:56 +02001894 stats[ST_F_SRV_ICUR] = mkf_u32(0, sv->curr_idle_conns);
1895 if (sv->max_idle_conns != -1)
1896 stats[ST_F_SRV_ILIM] = mkf_u32(FO_CONFIG|FN_LIMIT, sv->max_idle_conns);
1897
William Lallemand74c24fb2016-11-21 17:18:36 +01001898 stats[ST_F_STOT] = mkf_u64(FN_COUNTER, sv->counters.cum_sess);
1899 stats[ST_F_BIN] = mkf_u64(FN_COUNTER, sv->counters.bytes_in);
1900 stats[ST_F_BOUT] = mkf_u64(FN_COUNTER, sv->counters.bytes_out);
Christopher Fauleta08546b2019-12-16 16:07:34 +01001901 stats[ST_F_DRESP] = mkf_u64(FN_COUNTER, sv->counters.denied_resp);
William Lallemand74c24fb2016-11-21 17:18:36 +01001902 stats[ST_F_ECON] = mkf_u64(FN_COUNTER, sv->counters.failed_conns);
1903 stats[ST_F_ERESP] = mkf_u64(FN_COUNTER, sv->counters.failed_resp);
1904 stats[ST_F_WRETR] = mkf_u64(FN_COUNTER, sv->counters.retries);
1905 stats[ST_F_WREDIS] = mkf_u64(FN_COUNTER, sv->counters.redispatches);
Tim Duesterhus3fd19732018-05-27 20:35:08 +02001906 stats[ST_F_WREW] = mkf_u64(FN_COUNTER, sv->counters.failed_rewrites);
Christopher Faulet0159ee42019-12-16 14:40:39 +01001907 stats[ST_F_EINT] = mkf_u64(FN_COUNTER, sv->counters.internal_errors);
Willy Tarreauf1573842018-12-14 11:35:36 +01001908 stats[ST_F_CONNECT] = mkf_u64(FN_COUNTER, sv->counters.connect);
1909 stats[ST_F_REUSE] = mkf_u64(FN_COUNTER, sv->counters.reuse);
William Lallemand74c24fb2016-11-21 17:18:36 +01001910
Willy Tarreau3bb617c2020-06-29 13:51:05 +02001911 stats[ST_F_IDLE_CONN_CUR] = mkf_u32(0, sv->curr_idle_nb);
1912 stats[ST_F_SAFE_CONN_CUR] = mkf_u32(0, sv->curr_safe_nb);
1913 stats[ST_F_USED_CONN_CUR] = mkf_u32(0, sv->curr_used_conns);
Willy Tarreaua9fcecb2020-06-29 15:38:53 +02001914 stats[ST_F_NEED_CONN_EST] = mkf_u32(0, sv->est_need_conns);
Willy Tarreau3bb617c2020-06-29 13:51:05 +02001915
William Lallemand74c24fb2016-11-21 17:18:36 +01001916 /* status */
1917 fld_status = chunk_newstr(out);
Emeric Brun52a91d32017-08-31 14:41:55 +02001918 if (sv->cur_admin & SRV_ADMF_RMAINT)
William Lallemand74c24fb2016-11-21 17:18:36 +01001919 chunk_appendf(out, "MAINT (resolution)");
Emeric Brun52a91d32017-08-31 14:41:55 +02001920 else if (sv->cur_admin & SRV_ADMF_IMAINT)
William Lallemand74c24fb2016-11-21 17:18:36 +01001921 chunk_appendf(out, "MAINT (via %s/%s)", via->proxy->id, via->id);
Emeric Brun52a91d32017-08-31 14:41:55 +02001922 else if (sv->cur_admin & SRV_ADMF_MAINT)
William Lallemand74c24fb2016-11-21 17:18:36 +01001923 chunk_appendf(out, "MAINT");
1924 else
1925 chunk_appendf(out,
1926 srv_hlt_st[state],
Emeric Brun52a91d32017-08-31 14:41:55 +02001927 (ref->cur_state != SRV_ST_STOPPED) ? (ref->check.health - ref->check.rise + 1) : (ref->check.health),
1928 (ref->cur_state != SRV_ST_STOPPED) ? (ref->check.fall) : (ref->check.rise));
William Lallemand74c24fb2016-11-21 17:18:36 +01001929
1930 stats[ST_F_STATUS] = mkf_str(FO_STATUS, fld_status);
1931 stats[ST_F_LASTCHG] = mkf_u32(FN_AGE, now.tv_sec - sv->last_change);
Emeric Brun52a91d32017-08-31 14:41:55 +02001932 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 +01001933 stats[ST_F_ACT] = mkf_u32(FO_STATUS, (sv->flags & SRV_F_BACKUP) ? 0 : 1);
1934 stats[ST_F_BCK] = mkf_u32(FO_STATUS, (sv->flags & SRV_F_BACKUP) ? 1 : 0);
1935
1936 /* check failures: unique, fatal; last change, total downtime */
1937 if (sv->check.state & CHK_ST_ENABLED) {
1938 stats[ST_F_CHKFAIL] = mkf_u64(FN_COUNTER, sv->counters.failed_checks);
1939 stats[ST_F_CHKDOWN] = mkf_u64(FN_COUNTER, sv->counters.down_trans);
1940 stats[ST_F_DOWNTIME] = mkf_u32(FN_COUNTER, srv_downtime(sv));
1941 }
1942
1943 if (sv->maxqueue)
1944 stats[ST_F_QLIMIT] = mkf_u32(FO_CONFIG|FS_SERVICE, sv->maxqueue);
1945
1946 stats[ST_F_PID] = mkf_u32(FO_KEY, relative_pid);
1947 stats[ST_F_IID] = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
1948 stats[ST_F_SID] = mkf_u32(FO_KEY|FS_SERVICE, sv->puid);
1949
Emeric Brun52a91d32017-08-31 14:41:55 +02001950 if (sv->cur_state == SRV_ST_STARTING && !server_is_draining(sv))
William Lallemand74c24fb2016-11-21 17:18:36 +01001951 stats[ST_F_THROTTLE] = mkf_u32(FN_AVG, server_throttle_rate(sv));
1952
1953 stats[ST_F_LBTOT] = mkf_u64(FN_COUNTER, sv->counters.cum_lbconn);
1954
1955 if (sv->track) {
1956 char *fld_track = chunk_newstr(out);
1957
1958 chunk_appendf(out, "%s/%s", sv->track->proxy->id, sv->track->id);
1959 stats[ST_F_TRACKED] = mkf_str(FO_CONFIG|FN_NAME|FS_SERVICE, fld_track);
1960 }
1961
1962 stats[ST_F_TYPE] = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_SV);
1963 stats[ST_F_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&sv->sess_per_sec));
1964 stats[ST_F_RATE_MAX] = mkf_u32(FN_MAX, sv->counters.sps_max);
1965
1966 if ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED) {
1967 const char *fld_chksts;
1968
1969 fld_chksts = chunk_newstr(out);
1970 chunk_strcat(out, "* "); // for check in progress
1971 chunk_strcat(out, get_check_status_info(sv->check.status));
1972 if (!(sv->check.state & CHK_ST_INPROGRESS))
1973 fld_chksts += 2; // skip "* "
1974 stats[ST_F_CHECK_STATUS] = mkf_str(FN_OUTPUT, fld_chksts);
1975
1976 if (sv->check.status >= HCHK_STATUS_L57DATA)
1977 stats[ST_F_CHECK_CODE] = mkf_u32(FN_OUTPUT, sv->check.code);
1978
1979 if (sv->check.status >= HCHK_STATUS_CHECKED)
1980 stats[ST_F_CHECK_DURATION] = mkf_u64(FN_DURATION, sv->check.duration);
1981
1982 stats[ST_F_CHECK_DESC] = mkf_str(FN_OUTPUT, get_check_status_description(sv->check.status));
1983 stats[ST_F_LAST_CHK] = mkf_str(FN_OUTPUT, sv->check.desc);
1984 stats[ST_F_CHECK_RISE] = mkf_u32(FO_CONFIG|FS_SERVICE, ref->check.rise);
1985 stats[ST_F_CHECK_FALL] = mkf_u32(FO_CONFIG|FS_SERVICE, ref->check.fall);
1986 stats[ST_F_CHECK_HEALTH] = mkf_u32(FO_CONFIG|FS_SERVICE, ref->check.health);
1987 }
1988
1989 if ((sv->agent.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED) {
1990 const char *fld_chksts;
1991
1992 fld_chksts = chunk_newstr(out);
1993 chunk_strcat(out, "* "); // for check in progress
1994 chunk_strcat(out, get_check_status_info(sv->agent.status));
1995 if (!(sv->agent.state & CHK_ST_INPROGRESS))
1996 fld_chksts += 2; // skip "* "
1997 stats[ST_F_AGENT_STATUS] = mkf_str(FN_OUTPUT, fld_chksts);
1998
1999 if (sv->agent.status >= HCHK_STATUS_L57DATA)
2000 stats[ST_F_AGENT_CODE] = mkf_u32(FN_OUTPUT, sv->agent.code);
2001
2002 if (sv->agent.status >= HCHK_STATUS_CHECKED)
2003 stats[ST_F_AGENT_DURATION] = mkf_u64(FN_DURATION, sv->agent.duration);
2004
2005 stats[ST_F_AGENT_DESC] = mkf_str(FN_OUTPUT, get_check_status_description(sv->agent.status));
2006 stats[ST_F_LAST_AGT] = mkf_str(FN_OUTPUT, sv->agent.desc);
2007 stats[ST_F_AGENT_RISE] = mkf_u32(FO_CONFIG|FS_SERVICE, sv->agent.rise);
2008 stats[ST_F_AGENT_FALL] = mkf_u32(FO_CONFIG|FS_SERVICE, sv->agent.fall);
2009 stats[ST_F_AGENT_HEALTH] = mkf_u32(FO_CONFIG|FS_SERVICE, sv->agent.health);
2010 }
2011
2012 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
2013 if (px->mode == PR_MODE_HTTP) {
Marcin Deranek3c27dda2020-05-15 18:32:51 +02002014 stats[ST_F_REQ_TOT] = mkf_u64(FN_COUNTER, sv->counters.p.http.cum_req);
William Lallemand74c24fb2016-11-21 17:18:36 +01002015 stats[ST_F_HRSP_1XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[1]);
2016 stats[ST_F_HRSP_2XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[2]);
2017 stats[ST_F_HRSP_3XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[3]);
2018 stats[ST_F_HRSP_4XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[4]);
2019 stats[ST_F_HRSP_5XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[5]);
2020 stats[ST_F_HRSP_OTHER] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[0]);
2021 }
2022
2023 if (ref->observe)
2024 stats[ST_F_HANAFAIL] = mkf_u64(FN_COUNTER, sv->counters.failed_hana);
2025
2026 stats[ST_F_CLI_ABRT] = mkf_u64(FN_COUNTER, sv->counters.cli_aborts);
2027 stats[ST_F_SRV_ABRT] = mkf_u64(FN_COUNTER, sv->counters.srv_aborts);
2028 stats[ST_F_LASTSESS] = mkf_s32(FN_AGE, srv_lastsession(sv));
2029
Marcin Deraneka8dbdf32020-05-15 20:02:40 +02002030 srv_samples_counter = (px->mode == PR_MODE_HTTP) ? sv->counters.p.http.cum_req : sv->counters.cum_lbconn;
2031 if (srv_samples_counter < TIME_STATS_SAMPLES && srv_samples_counter > 0)
2032 srv_samples_window = srv_samples_counter;
2033
2034 stats[ST_F_QTIME] = mkf_u32(FN_AVG, swrate_avg(sv->counters.q_time, srv_samples_window));
2035 stats[ST_F_CTIME] = mkf_u32(FN_AVG, swrate_avg(sv->counters.c_time, srv_samples_window));
2036 stats[ST_F_RTIME] = mkf_u32(FN_AVG, swrate_avg(sv->counters.d_time, srv_samples_window));
2037 stats[ST_F_TTIME] = mkf_u32(FN_AVG, swrate_avg(sv->counters.t_time, srv_samples_window));
William Lallemand74c24fb2016-11-21 17:18:36 +01002038
Christopher Faulet0d1c2a62019-11-08 14:59:51 +01002039 stats[ST_F_QT_MAX] = mkf_u32(FN_MAX, sv->counters.qtime_max);
2040 stats[ST_F_CT_MAX] = mkf_u32(FN_MAX, sv->counters.ctime_max);
2041 stats[ST_F_RT_MAX] = mkf_u32(FN_MAX, sv->counters.dtime_max);
2042 stats[ST_F_TT_MAX] = mkf_u32(FN_MAX, sv->counters.ttime_max);
2043
Willy Tarreau708c4162019-10-09 10:19:16 +02002044 if (flags & STAT_SHLGNDS) {
William Lallemand74c24fb2016-11-21 17:18:36 +01002045 switch (addr_to_str(&sv->addr, str, sizeof(str))) {
2046 case AF_INET:
2047 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
Willy Tarreau04276f32017-01-06 17:41:29 +01002048 chunk_appendf(out, "%s:%d", str, sv->svc_port);
William Lallemand74c24fb2016-11-21 17:18:36 +01002049 break;
2050 case AF_INET6:
2051 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
Willy Tarreau04276f32017-01-06 17:41:29 +01002052 chunk_appendf(out, "[%s]:%d", str, sv->svc_port);
William Lallemand74c24fb2016-11-21 17:18:36 +01002053 break;
2054 case AF_UNIX:
2055 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, "unix");
2056 break;
2057 case -1:
2058 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
2059 chunk_strcat(out, strerror(errno));
2060 break;
2061 default: /* address family not supported */
2062 break;
2063 }
2064
2065 if (sv->cookie)
2066 stats[ST_F_COOKIE] = mkf_str(FO_CONFIG|FN_NAME|FS_SERVICE, sv->cookie);
2067 }
2068
2069 return 1;
2070}
2071
2072/* Dumps a line for server <sv> and proxy <px> to the trash and uses the state
Willy Tarreau43241ff2019-10-09 11:27:51 +02002073 * from stream interface <si>, and server state <state>. The caller is
2074 * responsible for clearing the trash if needed. Returns non-zero if it emits
2075 * anything, zero otherwise.
William Lallemand74c24fb2016-11-21 17:18:36 +01002076 */
Willy Tarreau43241ff2019-10-09 11:27:51 +02002077static int stats_dump_sv_stats(struct stream_interface *si, struct proxy *px, struct server *sv)
William Lallemand74c24fb2016-11-21 17:18:36 +01002078{
2079 struct appctx *appctx = __objt_appctx(si->end);
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02002080 struct stats_module *mod;
2081 struct field *stats = stat_l[STATS_DOMAIN_PROXY];
2082 size_t stats_count = ST_F_TOTAL_FIELDS;
2083
2084 memset(stats, 0, sizeof(struct field) * stat_count[STATS_DOMAIN_PROXY]);
William Lallemand74c24fb2016-11-21 17:18:36 +01002085
Willy Tarreau43241ff2019-10-09 11:27:51 +02002086 if (!stats_fill_sv_stats(px, sv, appctx->ctx.stats.flags, stats, ST_F_TOTAL_FIELDS))
William Lallemand74c24fb2016-11-21 17:18:36 +01002087 return 0;
2088
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02002089 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
2090 void *counters;
2091
2092 if (stats_get_domain(mod->domain_flags) != STATS_DOMAIN_PROXY)
2093 continue;
2094
2095 if (!(stats_px_get_cap(mod->domain_flags) & STATS_PX_CAP_SRV)) {
2096 stats_count += mod->stats_count;
2097 continue;
2098 }
2099
2100 counters = EXTRA_COUNTERS_GET(sv->extra_counters, mod);
2101 mod->fill_stats(counters, stats + stats_count);
2102 stats_count += mod->stats_count;
2103 }
2104
2105 return stats_dump_one_line(stats, stats_count, appctx);
William Lallemand74c24fb2016-11-21 17:18:36 +01002106}
2107
2108/* Fill <stats> with the backend statistics. <stats> is
2109 * preallocated array of length <len>. The length of the array
2110 * must be at least ST_F_TOTAL_FIELDS. If this length is less
2111 * then this value, the function returns 0, otherwise, it
Willy Tarreau708c4162019-10-09 10:19:16 +02002112 * returns 1. <flags> can take the value STAT_SHLGNDS.
William Lallemand74c24fb2016-11-21 17:18:36 +01002113 */
2114int stats_fill_be_stats(struct proxy *px, int flags, struct field *stats, int len)
2115{
Marcin Deraneka8dbdf32020-05-15 20:02:40 +02002116 long long be_samples_counter;
2117 unsigned int be_samples_window = TIME_STATS_SAMPLES;
2118
William Lallemand74c24fb2016-11-21 17:18:36 +01002119 if (len < ST_F_TOTAL_FIELDS)
2120 return 0;
2121
William Lallemand74c24fb2016-11-21 17:18:36 +01002122 stats[ST_F_PXNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
2123 stats[ST_F_SVNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, "BACKEND");
2124 stats[ST_F_MODE] = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
2125 stats[ST_F_QCUR] = mkf_u32(0, px->nbpend);
2126 stats[ST_F_QMAX] = mkf_u32(FN_MAX, px->be_counters.nbpend_max);
Thierry FOURNIER0ff98a42016-12-19 16:50:42 +01002127 stats[ST_F_SCUR] = mkf_u32(0, px->beconn);
William Lallemand74c24fb2016-11-21 17:18:36 +01002128 stats[ST_F_SMAX] = mkf_u32(FN_MAX, px->be_counters.conn_max);
2129 stats[ST_F_SLIM] = mkf_u32(FO_CONFIG|FN_LIMIT, px->fullconn);
2130 stats[ST_F_STOT] = mkf_u64(FN_COUNTER, px->be_counters.cum_conn);
2131 stats[ST_F_BIN] = mkf_u64(FN_COUNTER, px->be_counters.bytes_in);
2132 stats[ST_F_BOUT] = mkf_u64(FN_COUNTER, px->be_counters.bytes_out);
2133 stats[ST_F_DREQ] = mkf_u64(FN_COUNTER, px->be_counters.denied_req);
2134 stats[ST_F_DRESP] = mkf_u64(FN_COUNTER, px->be_counters.denied_resp);
2135 stats[ST_F_ECON] = mkf_u64(FN_COUNTER, px->be_counters.failed_conns);
2136 stats[ST_F_ERESP] = mkf_u64(FN_COUNTER, px->be_counters.failed_resp);
2137 stats[ST_F_WRETR] = mkf_u64(FN_COUNTER, px->be_counters.retries);
2138 stats[ST_F_WREDIS] = mkf_u64(FN_COUNTER, px->be_counters.redispatches);
Tim Duesterhus3fd19732018-05-27 20:35:08 +02002139 stats[ST_F_WREW] = mkf_u64(FN_COUNTER, px->be_counters.failed_rewrites);
Christopher Faulet0159ee42019-12-16 14:40:39 +01002140 stats[ST_F_EINT] = mkf_u64(FN_COUNTER, px->be_counters.internal_errors);
Willy Tarreauf1573842018-12-14 11:35:36 +01002141 stats[ST_F_CONNECT] = mkf_u64(FN_COUNTER, px->be_counters.connect);
2142 stats[ST_F_REUSE] = mkf_u64(FN_COUNTER, px->be_counters.reuse);
William Lallemand74c24fb2016-11-21 17:18:36 +01002143 stats[ST_F_STATUS] = mkf_str(FO_STATUS, (px->lbprm.tot_weight > 0 || !px->srv) ? "UP" : "DOWN");
2144 stats[ST_F_WEIGHT] = mkf_u32(FN_AVG, (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv);
2145 stats[ST_F_ACT] = mkf_u32(0, px->srv_act);
2146 stats[ST_F_BCK] = mkf_u32(0, px->srv_bck);
2147 stats[ST_F_CHKDOWN] = mkf_u64(FN_COUNTER, px->down_trans);
2148 stats[ST_F_LASTCHG] = mkf_u32(FN_AGE, now.tv_sec - px->last_change);
2149 if (px->srv)
2150 stats[ST_F_DOWNTIME] = mkf_u32(FN_COUNTER, be_downtime(px));
2151
2152 stats[ST_F_PID] = mkf_u32(FO_KEY, relative_pid);
2153 stats[ST_F_IID] = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
2154 stats[ST_F_SID] = mkf_u32(FO_KEY|FS_SERVICE, 0);
2155 stats[ST_F_LBTOT] = mkf_u64(FN_COUNTER, px->be_counters.cum_lbconn);
2156 stats[ST_F_TYPE] = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_BE);
2157 stats[ST_F_RATE] = mkf_u32(0, read_freq_ctr(&px->be_sess_per_sec));
2158 stats[ST_F_RATE_MAX] = mkf_u32(0, px->be_counters.sps_max);
2159
Willy Tarreau708c4162019-10-09 10:19:16 +02002160 if (flags & STAT_SHLGNDS) {
William Lallemand74c24fb2016-11-21 17:18:36 +01002161 if (px->cookie_name)
2162 stats[ST_F_COOKIE] = mkf_str(FO_CONFIG|FN_NAME|FS_SERVICE, px->cookie_name);
2163 stats[ST_F_ALGO] = mkf_str(FO_CONFIG|FS_SERVICE, backend_lb_algo_str(px->lbprm.algo & BE_LB_ALGO));
2164 }
2165
2166 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
2167 if (px->mode == PR_MODE_HTTP) {
2168 stats[ST_F_REQ_TOT] = mkf_u64(FN_COUNTER, px->be_counters.p.http.cum_req);
2169 stats[ST_F_HRSP_1XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[1]);
2170 stats[ST_F_HRSP_2XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[2]);
2171 stats[ST_F_HRSP_3XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[3]);
2172 stats[ST_F_HRSP_4XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[4]);
2173 stats[ST_F_HRSP_5XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[5]);
2174 stats[ST_F_HRSP_OTHER] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[0]);
Willy Tarreaua1214a52018-12-14 14:00:25 +01002175 stats[ST_F_CACHE_LOOKUPS] = mkf_u64(FN_COUNTER, px->be_counters.p.http.cache_lookups);
2176 stats[ST_F_CACHE_HITS] = mkf_u64(FN_COUNTER, px->be_counters.p.http.cache_hits);
William Lallemand74c24fb2016-11-21 17:18:36 +01002177 }
2178
2179 stats[ST_F_CLI_ABRT] = mkf_u64(FN_COUNTER, px->be_counters.cli_aborts);
2180 stats[ST_F_SRV_ABRT] = mkf_u64(FN_COUNTER, px->be_counters.srv_aborts);
2181
2182 /* compression: in, out, bypassed, responses */
2183 stats[ST_F_COMP_IN] = mkf_u64(FN_COUNTER, px->be_counters.comp_in);
2184 stats[ST_F_COMP_OUT] = mkf_u64(FN_COUNTER, px->be_counters.comp_out);
2185 stats[ST_F_COMP_BYP] = mkf_u64(FN_COUNTER, px->be_counters.comp_byp);
2186 stats[ST_F_COMP_RSP] = mkf_u64(FN_COUNTER, px->be_counters.p.http.comp_rsp);
2187 stats[ST_F_LASTSESS] = mkf_s32(FN_AGE, be_lastsession(px));
2188
Marcin Deraneka8dbdf32020-05-15 20:02:40 +02002189 be_samples_counter = (px->mode == PR_MODE_HTTP) ? px->be_counters.p.http.cum_req : px->be_counters.cum_lbconn;
2190 if (be_samples_counter < TIME_STATS_SAMPLES && be_samples_counter > 0)
2191 be_samples_window = be_samples_counter;
2192
2193 stats[ST_F_QTIME] = mkf_u32(FN_AVG, swrate_avg(px->be_counters.q_time, be_samples_window));
2194 stats[ST_F_CTIME] = mkf_u32(FN_AVG, swrate_avg(px->be_counters.c_time, be_samples_window));
2195 stats[ST_F_RTIME] = mkf_u32(FN_AVG, swrate_avg(px->be_counters.d_time, be_samples_window));
2196 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 +01002197
Christopher Faulet0d1c2a62019-11-08 14:59:51 +01002198 stats[ST_F_QT_MAX] = mkf_u32(FN_MAX, px->be_counters.qtime_max);
2199 stats[ST_F_CT_MAX] = mkf_u32(FN_MAX, px->be_counters.ctime_max);
2200 stats[ST_F_RT_MAX] = mkf_u32(FN_MAX, px->be_counters.dtime_max);
2201 stats[ST_F_TT_MAX] = mkf_u32(FN_MAX, px->be_counters.ttime_max);
2202
William Lallemand74c24fb2016-11-21 17:18:36 +01002203 return 1;
2204}
2205
2206/* Dumps a line for backend <px> to the trash for and uses the state from stream
Willy Tarreau43241ff2019-10-09 11:27:51 +02002207 * interface <si>. The caller is responsible for clearing the trash if needed.
2208 * Returns non-zero if it emits anything, zero otherwise.
William Lallemand74c24fb2016-11-21 17:18:36 +01002209 */
Willy Tarreau43241ff2019-10-09 11:27:51 +02002210static int stats_dump_be_stats(struct stream_interface *si, struct proxy *px)
William Lallemand74c24fb2016-11-21 17:18:36 +01002211{
2212 struct appctx *appctx = __objt_appctx(si->end);
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02002213 struct field *stats = stat_l[STATS_DOMAIN_PROXY];
2214 struct stats_module *mod;
2215 size_t stats_count = ST_F_TOTAL_FIELDS;
William Lallemand74c24fb2016-11-21 17:18:36 +01002216
2217 if (!(px->cap & PR_CAP_BE))
2218 return 0;
2219
2220 if ((appctx->ctx.stats.flags & STAT_BOUND) && !(appctx->ctx.stats.type & (1 << STATS_TYPE_BE)))
2221 return 0;
2222
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02002223 memset(stats, 0, sizeof(struct field) * stat_count[STATS_DOMAIN_PROXY]);
2224
Willy Tarreau43241ff2019-10-09 11:27:51 +02002225 if (!stats_fill_be_stats(px, appctx->ctx.stats.flags, stats, ST_F_TOTAL_FIELDS))
William Lallemand74c24fb2016-11-21 17:18:36 +01002226 return 0;
2227
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02002228 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
2229 struct extra_counters *counters;
2230
2231 if (stats_get_domain(mod->domain_flags) != STATS_DOMAIN_PROXY)
2232 continue;
2233
2234 if (!(stats_px_get_cap(mod->domain_flags) & STATS_PX_CAP_BE)) {
2235 stats_count += mod->stats_count;
2236 continue;
2237 }
2238
2239 counters = EXTRA_COUNTERS_GET(px->extra_counters_be, mod);
2240 mod->fill_stats(counters, stats + stats_count);
2241 stats_count += mod->stats_count;
2242 }
2243
2244 return stats_dump_one_line(stats, stats_count, appctx);
William Lallemand74c24fb2016-11-21 17:18:36 +01002245}
2246
2247/* Dumps the HTML table header for proxy <px> to the trash for and uses the state from
2248 * stream interface <si> and per-uri parameters <uri>. The caller is responsible
2249 * for clearing the trash if needed.
2250 */
Willy Tarreau676c29e2019-10-09 10:50:01 +02002251static void stats_dump_html_px_hdr(struct stream_interface *si, struct proxy *px)
William Lallemand74c24fb2016-11-21 17:18:36 +01002252{
2253 struct appctx *appctx = __objt_appctx(si->end);
2254 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02002255 struct stats_module *mod;
2256 int stats_module_len = 0;
William Lallemand74c24fb2016-11-21 17:18:36 +01002257
2258 if (px->cap & PR_CAP_BE && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
2259 /* A form to enable/disable this proxy servers */
2260
2261 /* scope_txt = search pattern + search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
2262 scope_txt[0] = 0;
2263 if (appctx->ctx.stats.scope_len) {
Christopher Fauleted7a0662019-01-14 11:07:34 +01002264 const char *scope_ptr = stats_scope_ptr(appctx, si);
2265
William Lallemand74c24fb2016-11-21 17:18:36 +01002266 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Christopher Fauleted7a0662019-01-14 11:07:34 +01002267 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), scope_ptr, appctx->ctx.stats.scope_len);
William Lallemand74c24fb2016-11-21 17:18:36 +01002268 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
2269 }
2270
2271 chunk_appendf(&trash,
2272 "<form method=\"post\">");
2273 }
2274
2275 /* print a new table */
2276 chunk_appendf(&trash,
2277 "<table class=\"tbl\" width=\"100%%\">\n"
2278 "<tr class=\"titre\">"
2279 "<th class=\"pxname\" width=\"10%%\">");
2280
2281 chunk_appendf(&trash,
2282 "<a name=\"%s\"></a>%s"
2283 "<a class=px href=\"#%s\">%s</a>",
2284 px->id,
Willy Tarreau676c29e2019-10-09 10:50:01 +02002285 (appctx->ctx.stats.flags & STAT_SHLGNDS) ? "<u>":"",
William Lallemand74c24fb2016-11-21 17:18:36 +01002286 px->id, px->id);
2287
Willy Tarreau676c29e2019-10-09 10:50:01 +02002288 if (appctx->ctx.stats.flags & STAT_SHLGNDS) {
William Lallemand74c24fb2016-11-21 17:18:36 +01002289 /* cap, mode, id */
2290 chunk_appendf(&trash, "<div class=tips>cap: %s, mode: %s, id: %d",
2291 proxy_cap_str(px->cap), proxy_mode_str(px->mode),
2292 px->uuid);
2293 chunk_appendf(&trash, "</div>");
2294 }
2295
2296 chunk_appendf(&trash,
2297 "%s</th>"
2298 "<th class=\"%s\" width=\"90%%\">%s</th>"
2299 "</tr>\n"
2300 "</table>\n"
2301 "<table class=\"tbl\" width=\"100%%\">\n"
2302 "<tr class=\"titre\">",
Willy Tarreau676c29e2019-10-09 10:50:01 +02002303 (appctx->ctx.stats.flags & STAT_SHLGNDS) ? "</u>":"",
William Lallemand74c24fb2016-11-21 17:18:36 +01002304 px->desc ? "desc" : "empty", px->desc ? px->desc : "");
2305
Christopher Fauletbc271ec2019-12-02 11:29:04 +01002306 if (appctx->ctx.stats.flags & STAT_ADMIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +01002307 /* Column heading for Enable or Disable server */
Christopher Fauletbc271ec2019-12-02 11:29:04 +01002308 if ((px->cap & PR_CAP_BE) && px->srv)
2309 chunk_appendf(&trash,
2310 "<th rowspan=2 width=1><input type=\"checkbox\" "
2311 "onclick=\"for(c in document.getElementsByClassName('%s-checkbox')) "
2312 "document.getElementsByClassName('%s-checkbox').item(c).checked = this.checked\"></th>",
2313 px->id,
2314 px->id);
2315 else
2316 chunk_appendf(&trash, "<th rowspan=2></th>");
William Lallemand74c24fb2016-11-21 17:18:36 +01002317 }
2318
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02002319 // calculate the count of module for colspan attribute
2320 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
2321 ++stats_module_len;
2322 }
2323
William Lallemand74c24fb2016-11-21 17:18:36 +01002324 chunk_appendf(&trash,
2325 "<th rowspan=2></th>"
2326 "<th colspan=3>Queue</th>"
2327 "<th colspan=3>Session rate</th><th colspan=6>Sessions</th>"
2328 "<th colspan=2>Bytes</th><th colspan=2>Denied</th>"
2329 "<th colspan=3>Errors</th><th colspan=2>Warnings</th>"
2330 "<th colspan=9>Server</th>"
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02002331 "<th colspan=%d>Extra modules</th>"
William Lallemand74c24fb2016-11-21 17:18:36 +01002332 "</tr>\n"
2333 "<tr class=\"titre\">"
2334 "<th>Cur</th><th>Max</th><th>Limit</th>"
2335 "<th>Cur</th><th>Max</th><th>Limit</th><th>Cur</th><th>Max</th>"
2336 "<th>Limit</th><th>Total</th><th>LbTot</th><th>Last</th><th>In</th><th>Out</th>"
2337 "<th>Req</th><th>Resp</th><th>Req</th><th>Conn</th>"
2338 "<th>Resp</th><th>Retr</th><th>Redis</th>"
2339 "<th>Status</th><th>LastChk</th><th>Wght</th><th>Act</th>"
2340 "<th>Bck</th><th>Chk</th><th>Dwn</th><th>Dwntme</th>"
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02002341 "<th>Thrtle</th>\n", stats_module_len);
2342
2343 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
2344 chunk_appendf(&trash, "<th>%s</th>", mod->name);
2345 }
2346
2347 chunk_appendf(&trash, "</tr>");
William Lallemand74c24fb2016-11-21 17:18:36 +01002348}
2349
2350/* Dumps the HTML table trailer for proxy <px> to the trash for and uses the state from
2351 * stream interface <si>. The caller is responsible for clearing the trash if needed.
2352 */
2353static void stats_dump_html_px_end(struct stream_interface *si, struct proxy *px)
2354{
2355 struct appctx *appctx = __objt_appctx(si->end);
2356 chunk_appendf(&trash, "</table>");
2357
2358 if ((px->cap & PR_CAP_BE) && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
2359 /* close the form used to enable/disable this proxy servers */
2360 chunk_appendf(&trash,
2361 "Choose the action to perform on the checked servers : "
2362 "<select name=action>"
2363 "<option value=\"\"></option>"
2364 "<option value=\"ready\">Set state to READY</option>"
2365 "<option value=\"drain\">Set state to DRAIN</option>"
2366 "<option value=\"maint\">Set state to MAINT</option>"
2367 "<option value=\"dhlth\">Health: disable checks</option>"
2368 "<option value=\"ehlth\">Health: enable checks</option>"
2369 "<option value=\"hrunn\">Health: force UP</option>"
2370 "<option value=\"hnolb\">Health: force NOLB</option>"
2371 "<option value=\"hdown\">Health: force DOWN</option>"
2372 "<option value=\"dagent\">Agent: disable checks</option>"
2373 "<option value=\"eagent\">Agent: enable checks</option>"
2374 "<option value=\"arunn\">Agent: force UP</option>"
2375 "<option value=\"adown\">Agent: force DOWN</option>"
2376 "<option value=\"shutdown\">Kill Sessions</option>"
2377 "</select>"
2378 "<input type=\"hidden\" name=\"b\" value=\"#%d\">"
2379 "&nbsp;<input type=\"submit\" value=\"Apply\">"
2380 "</form>",
2381 px->uuid);
2382 }
2383
2384 chunk_appendf(&trash, "<p>\n");
2385}
2386
2387/*
2388 * Dumps statistics for a proxy. The output is sent to the stream interface's
2389 * input buffer. Returns 0 if it had to stop dumping data because of lack of
2390 * buffer space, or non-zero if everything completed. This function is used
2391 * both by the CLI and the HTTP entry points, and is able to dump the output
2392 * in HTML or CSV formats. If the later, <uri> must be NULL.
2393 */
Christopher Fauletef779222018-10-31 08:47:01 +01002394int stats_dump_proxy_to_buffer(struct stream_interface *si, struct htx *htx,
2395 struct proxy *px, struct uri_auth *uri)
William Lallemand74c24fb2016-11-21 17:18:36 +01002396{
2397 struct appctx *appctx = __objt_appctx(si->end);
2398 struct stream *s = si_strm(si);
2399 struct channel *rep = si_ic(si);
2400 struct server *sv, *svs; /* server and server-state, server-state=server or server->track */
2401 struct listener *l;
William Lallemand74c24fb2016-11-21 17:18:36 +01002402
2403 chunk_reset(&trash);
2404
2405 switch (appctx->ctx.stats.px_st) {
2406 case STAT_PX_ST_INIT:
2407 /* we are on a new proxy */
2408 if (uri && uri->scope) {
2409 /* we have a limited scope, we have to check the proxy name */
2410 struct stat_scope *scope;
2411 int len;
2412
2413 len = strlen(px->id);
2414 scope = uri->scope;
2415
2416 while (scope) {
2417 /* match exact proxy name */
2418 if (scope->px_len == len && !memcmp(px->id, scope->px_id, len))
2419 break;
2420
2421 /* match '.' which means 'self' proxy */
2422 if (!strcmp(scope->px_id, ".") && px == s->be)
2423 break;
2424 scope = scope->next;
2425 }
2426
2427 /* proxy name not found : don't dump anything */
2428 if (scope == NULL)
2429 return 1;
2430 }
2431
2432 /* if the user has requested a limited output and the proxy
2433 * name does not match, skip it.
2434 */
Christopher Fauleted7a0662019-01-14 11:07:34 +01002435 if (appctx->ctx.stats.scope_len) {
2436 const char *scope_ptr = stats_scope_ptr(appctx, si);
2437
2438 if (strnistr(px->id, strlen(px->id), scope_ptr, appctx->ctx.stats.scope_len) == NULL)
2439 return 1;
2440 }
William Lallemand74c24fb2016-11-21 17:18:36 +01002441
2442 if ((appctx->ctx.stats.flags & STAT_BOUND) &&
2443 (appctx->ctx.stats.iid != -1) &&
2444 (px->uuid != appctx->ctx.stats.iid))
2445 return 1;
2446
2447 appctx->ctx.stats.px_st = STAT_PX_ST_TH;
2448 /* fall through */
2449
2450 case STAT_PX_ST_TH:
2451 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreau676c29e2019-10-09 10:50:01 +02002452 stats_dump_html_px_hdr(si, px);
Christopher Fauletef779222018-10-31 08:47:01 +01002453 if (!stats_putchk(rep, htx, &trash))
2454 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002455 }
2456
2457 appctx->ctx.stats.px_st = STAT_PX_ST_FE;
2458 /* fall through */
2459
2460 case STAT_PX_ST_FE:
2461 /* print the frontend */
2462 if (stats_dump_fe_stats(si, px)) {
Christopher Fauletef779222018-10-31 08:47:01 +01002463 if (!stats_putchk(rep, htx, &trash))
2464 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002465 }
2466
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +02002467 appctx->ctx.stats.obj2 = px->conf.listeners.n;
William Lallemand74c24fb2016-11-21 17:18:36 +01002468 appctx->ctx.stats.px_st = STAT_PX_ST_LI;
2469 /* fall through */
2470
2471 case STAT_PX_ST_LI:
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +02002472 /* obj2 points to listeners list as initialized above */
2473 for (; appctx->ctx.stats.obj2 != &px->conf.listeners; appctx->ctx.stats.obj2 = l->by_fe.n) {
Christopher Fauletef779222018-10-31 08:47:01 +01002474 if (htx) {
2475 if (htx_almost_full(htx))
2476 goto full;
2477 }
2478 else {
2479 if (buffer_almost_full(&rep->buf))
2480 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002481 }
2482
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +02002483 l = LIST_ELEM(appctx->ctx.stats.obj2, struct listener *, by_fe);
William Lallemand74c24fb2016-11-21 17:18:36 +01002484 if (!l->counters)
2485 continue;
2486
2487 if (appctx->ctx.stats.flags & STAT_BOUND) {
2488 if (!(appctx->ctx.stats.type & (1 << STATS_TYPE_SO)))
2489 break;
2490
2491 if (appctx->ctx.stats.sid != -1 && l->luid != appctx->ctx.stats.sid)
2492 continue;
2493 }
2494
2495 /* print the frontend */
Willy Tarreau43241ff2019-10-09 11:27:51 +02002496 if (stats_dump_li_stats(si, px, l)) {
Christopher Fauletef779222018-10-31 08:47:01 +01002497 if (!stats_putchk(rep, htx, &trash))
2498 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002499 }
2500 }
2501
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +02002502 appctx->ctx.stats.obj2 = px->srv; /* may be NULL */
William Lallemand74c24fb2016-11-21 17:18:36 +01002503 appctx->ctx.stats.px_st = STAT_PX_ST_SV;
2504 /* fall through */
2505
2506 case STAT_PX_ST_SV:
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +02002507 /* obj2 points to servers list as initialized above */
2508 for (; appctx->ctx.stats.obj2 != NULL; appctx->ctx.stats.obj2 = sv->next) {
Christopher Fauletef779222018-10-31 08:47:01 +01002509 if (htx) {
2510 if (htx_almost_full(htx))
2511 goto full;
2512 }
2513 else {
2514 if (buffer_almost_full(&rep->buf))
2515 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002516 }
2517
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +02002518 sv = appctx->ctx.stats.obj2;
William Lallemand74c24fb2016-11-21 17:18:36 +01002519
2520 if (appctx->ctx.stats.flags & STAT_BOUND) {
2521 if (!(appctx->ctx.stats.type & (1 << STATS_TYPE_SV)))
2522 break;
2523
2524 if (appctx->ctx.stats.sid != -1 && sv->puid != appctx->ctx.stats.sid)
2525 continue;
2526 }
2527
2528 svs = sv;
2529 while (svs->track)
2530 svs = svs->track;
2531
2532 /* do not report servers which are DOWN and not changing state */
2533 if ((appctx->ctx.stats.flags & STAT_HIDE_DOWN) &&
Emeric Brun52a91d32017-08-31 14:41:55 +02002534 ((sv->cur_admin & SRV_ADMF_MAINT) || /* server is in maintenance */
2535 (sv->cur_state == SRV_ST_STOPPED && /* server is down */
William Lallemand74c24fb2016-11-21 17:18:36 +01002536 (!((svs->agent.state | svs->check.state) & CHK_ST_ENABLED) ||
2537 ((svs->agent.state & CHK_ST_ENABLED) && !svs->agent.health) ||
2538 ((svs->check.state & CHK_ST_ENABLED) && !svs->check.health))))) {
2539 continue;
2540 }
2541
Willy Tarreau43241ff2019-10-09 11:27:51 +02002542 if (stats_dump_sv_stats(si, px, sv)) {
Christopher Fauletef779222018-10-31 08:47:01 +01002543 if (!stats_putchk(rep, htx, &trash))
2544 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002545 }
2546 } /* for sv */
2547
2548 appctx->ctx.stats.px_st = STAT_PX_ST_BE;
2549 /* fall through */
2550
2551 case STAT_PX_ST_BE:
2552 /* print the backend */
Willy Tarreau43241ff2019-10-09 11:27:51 +02002553 if (stats_dump_be_stats(si, px)) {
Christopher Fauletef779222018-10-31 08:47:01 +01002554 if (!stats_putchk(rep, htx, &trash))
2555 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002556 }
2557
2558 appctx->ctx.stats.px_st = STAT_PX_ST_END;
2559 /* fall through */
2560
2561 case STAT_PX_ST_END:
2562 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
2563 stats_dump_html_px_end(si, px);
Christopher Fauletef779222018-10-31 08:47:01 +01002564 if (!stats_putchk(rep, htx, &trash))
2565 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002566 }
2567
2568 appctx->ctx.stats.px_st = STAT_PX_ST_FIN;
2569 /* fall through */
2570
2571 case STAT_PX_ST_FIN:
2572 return 1;
2573
2574 default:
2575 /* unknown state, we should put an abort() here ! */
2576 return 1;
2577 }
Christopher Fauletef779222018-10-31 08:47:01 +01002578
2579 full:
2580 si_rx_room_blk(si);
2581 return 0;
William Lallemand74c24fb2016-11-21 17:18:36 +01002582}
2583
2584/* Dumps the HTTP stats head block to the trash for and uses the per-uri
2585 * parameters <uri>. The caller is responsible for clearing the trash if needed.
2586 */
Willy Tarreau676c29e2019-10-09 10:50:01 +02002587static void stats_dump_html_head(struct appctx *appctx, struct uri_auth *uri)
William Lallemand74c24fb2016-11-21 17:18:36 +01002588{
2589 /* WARNING! This must fit in the first buffer !!! */
2590 chunk_appendf(&trash,
2591 "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"\n"
2592 "\"http://www.w3.org/TR/html4/loose.dtd\">\n"
2593 "<html><head><title>Statistics Report for " PRODUCT_NAME "%s%s</title>\n"
zurikus6d599932020-08-18 11:16:05 +03002594 "<link rel=\"icon\" href=\"data:,\">\n"
William Lallemand74c24fb2016-11-21 17:18:36 +01002595 "<meta http-equiv=\"content-type\" content=\"text/html; charset=iso-8859-1\">\n"
2596 "<style type=\"text/css\"><!--\n"
2597 "body {"
2598 " font-family: arial, helvetica, sans-serif;"
2599 " font-size: 12px;"
2600 " font-weight: normal;"
2601 " color: black;"
2602 " background: white;"
2603 "}\n"
2604 "th,td {"
2605 " font-size: 10px;"
2606 "}\n"
2607 "h1 {"
2608 " font-size: x-large;"
2609 " margin-bottom: 0.5em;"
2610 "}\n"
2611 "h2 {"
2612 " font-family: helvetica, arial;"
2613 " font-size: x-large;"
2614 " font-weight: bold;"
2615 " font-style: italic;"
2616 " color: #6020a0;"
2617 " margin-top: 0em;"
2618 " margin-bottom: 0em;"
2619 "}\n"
2620 "h3 {"
2621 " font-family: helvetica, arial;"
2622 " font-size: 16px;"
2623 " font-weight: bold;"
2624 " color: #b00040;"
2625 " background: #e8e8d0;"
2626 " margin-top: 0em;"
2627 " margin-bottom: 0em;"
2628 "}\n"
2629 "li {"
2630 " margin-top: 0.25em;"
2631 " margin-right: 2em;"
2632 "}\n"
2633 ".hr {margin-top: 0.25em;"
2634 " border-color: black;"
2635 " border-bottom-style: solid;"
2636 "}\n"
2637 ".titre {background: #20D0D0;color: #000000; font-weight: bold; text-align: center;}\n"
2638 ".total {background: #20D0D0;color: #ffff80;}\n"
2639 ".frontend {background: #e8e8d0;}\n"
2640 ".socket {background: #d0d0d0;}\n"
2641 ".backend {background: #e8e8d0;}\n"
2642 ".active_down {background: #ff9090;}\n"
2643 ".active_going_up {background: #ffd020;}\n"
2644 ".active_going_down {background: #ffffa0;}\n"
2645 ".active_up {background: #c0ffc0;}\n"
2646 ".active_nolb {background: #20a0ff;}\n"
2647 ".active_draining {background: #20a0FF;}\n"
2648 ".active_no_check {background: #e0e0e0;}\n"
2649 ".backup_down {background: #ff9090;}\n"
2650 ".backup_going_up {background: #ff80ff;}\n"
2651 ".backup_going_down {background: #c060ff;}\n"
2652 ".backup_up {background: #b0d0ff;}\n"
2653 ".backup_nolb {background: #90b0e0;}\n"
2654 ".backup_draining {background: #cc9900;}\n"
2655 ".backup_no_check {background: #e0e0e0;}\n"
2656 ".maintain {background: #c07820;}\n"
2657 ".rls {letter-spacing: 0.2em; margin-right: 1px;}\n" /* right letter spacing (used for grouping digits) */
2658 "\n"
2659 "a.px:link {color: #ffff40; text-decoration: none;}"
2660 "a.px:visited {color: #ffff40; text-decoration: none;}"
2661 "a.px:hover {color: #ffffff; text-decoration: none;}"
2662 "a.lfsb:link {color: #000000; text-decoration: none;}"
2663 "a.lfsb:visited {color: #000000; text-decoration: none;}"
2664 "a.lfsb:hover {color: #505050; text-decoration: none;}"
2665 "\n"
2666 "table.tbl { border-collapse: collapse; border-style: none;}\n"
2667 "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"
2668 "table.tbl td.ac { text-align: center;}\n"
2669 "table.tbl th { border-width: 1px; border-style: solid solid solid solid; border-color: gray;}\n"
2670 "table.tbl th.pxname { background: #b00040; color: #ffff40; font-weight: bold; border-style: solid solid none solid; padding: 2px 3px; white-space: nowrap;}\n"
2671 "table.tbl th.empty { border-style: none; empty-cells: hide; background: white;}\n"
2672 "table.tbl th.desc { background: white; border-style: solid solid none solid; text-align: left; padding: 2px 3px;}\n"
2673 "\n"
2674 "table.lgd { border-collapse: collapse; border-width: 1px; border-style: none none none solid; border-color: black;}\n"
2675 "table.lgd td { border-width: 1px; border-style: solid solid solid solid; border-color: gray; padding: 2px;}\n"
2676 "table.lgd td.noborder { border-style: none; padding: 2px; white-space: nowrap;}\n"
2677 "table.det { border-collapse: collapse; border-style: none; }\n"
2678 "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"
2679 "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"
2680 "u {text-decoration:none; border-bottom: 1px dotted black;}\n"
2681 "div.tips {\n"
2682 " display:block;\n"
2683 " visibility:hidden;\n"
2684 " z-index:2147483647;\n"
2685 " position:absolute;\n"
2686 " padding:2px 4px 3px;\n"
2687 " background:#f0f060; color:#000000;\n"
2688 " border:1px solid #7040c0;\n"
2689 " white-space:nowrap;\n"
2690 " font-style:normal;font-size:11px;font-weight:normal;\n"
2691 " -moz-border-radius:3px;-webkit-border-radius:3px;border-radius:3px;\n"
2692 " -moz-box-shadow:gray 2px 2px 3px;-webkit-box-shadow:gray 2px 2px 3px;box-shadow:gray 2px 2px 3px;\n"
2693 "}\n"
2694 "u:hover div.tips {visibility:visible;}\n"
2695 "-->\n"
2696 "</style></head>\n",
Willy Tarreau676c29e2019-10-09 10:50:01 +02002697 (appctx->ctx.stats.flags & STAT_SHNODE) ? " on " : "",
Willy Tarreau027d2062020-01-23 11:47:13 +01002698 (appctx->ctx.stats.flags & STAT_SHNODE) ? (uri && uri->node ? uri->node : global.node) : ""
William Lallemand74c24fb2016-11-21 17:18:36 +01002699 );
2700}
2701
2702/* Dumps the HTML stats information block to the trash for and uses the state from
2703 * stream interface <si> and per-uri parameters <uri>. The caller is responsible
2704 * for clearing the trash if needed.
2705 */
2706static void stats_dump_html_info(struct stream_interface *si, struct uri_auth *uri)
2707{
2708 struct appctx *appctx = __objt_appctx(si->end);
2709 unsigned int up = (now.tv_sec - start_date.tv_sec);
2710 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
Christopher Fauleted7a0662019-01-14 11:07:34 +01002711 const char *scope_ptr = stats_scope_ptr(appctx, si);
Willy Tarreau1713c032019-05-23 12:23:55 +02002712 unsigned long long bps = (unsigned long long)read_freq_ctr(&global.out_32bps) * 32;
2713
2714 /* Turn the bytes per second to bits per second and take care of the
2715 * usual ethernet overhead in order to help figure how far we are from
2716 * interface saturation since it's the only case which usually matters.
2717 * For this we count the total size of an Ethernet frame on the wire
2718 * including preamble and IFG (1538) for the largest TCP segment it
2719 * transports (1448 with TCP timestamps). This is not valid for smaller
2720 * packets (under-estimated), but it gives a reasonably accurate
2721 * estimation of how far we are from uplink saturation.
2722 */
2723 bps = bps * 8 * 1538 / 1448;
William Lallemand74c24fb2016-11-21 17:18:36 +01002724
2725 /* WARNING! this has to fit the first packet too.
2726 * We are around 3.5 kB, add adding entries will
2727 * become tricky if we want to support 4kB buffers !
2728 */
2729 chunk_appendf(&trash,
2730 "<body><h1><a href=\"" PRODUCT_URL "\" style=\"text-decoration: none;\">"
2731 PRODUCT_NAME "%s</a></h1>\n"
2732 "<h2>Statistics Report for pid %d%s%s%s%s</h2>\n"
2733 "<hr width=\"100%%\" class=\"hr\">\n"
2734 "<h3>&gt; General process information</h3>\n"
2735 "<table border=0><tr><td align=\"left\" nowrap width=\"1%%\">\n"
Yves Lafon95317282018-02-26 11:10:37 +01002736 "<p><b>pid = </b> %d (process #%d, nbproc = %d, nbthread = %d)<br>\n"
William Lallemand74c24fb2016-11-21 17:18:36 +01002737 "<b>uptime = </b> %dd %dh%02dm%02ds<br>\n"
2738 "<b>system limits:</b> memmax = %s%s; ulimit-n = %d<br>\n"
2739 "<b>maxsock = </b> %d; <b>maxconn = </b> %d; <b>maxpipes = </b> %d<br>\n"
Willy Tarreau1713c032019-05-23 12:23:55 +02002740 "current conns = %d; current pipes = %d/%d; conn rate = %d/sec; bit rate = %.3f %cbps<br>\n"
William Lallemand74c24fb2016-11-21 17:18:36 +01002741 "Running tasks: %d/%d; idle = %d %%<br>\n"
2742 "</td><td align=\"center\" nowrap>\n"
2743 "<table class=\"lgd\"><tr>\n"
2744 "<td class=\"active_up\">&nbsp;</td><td class=\"noborder\">active UP </td>"
2745 "<td class=\"backup_up\">&nbsp;</td><td class=\"noborder\">backup UP </td>"
2746 "</tr><tr>\n"
2747 "<td class=\"active_going_down\"></td><td class=\"noborder\">active UP, going down </td>"
2748 "<td class=\"backup_going_down\"></td><td class=\"noborder\">backup UP, going down </td>"
2749 "</tr><tr>\n"
2750 "<td class=\"active_going_up\"></td><td class=\"noborder\">active DOWN, going up </td>"
2751 "<td class=\"backup_going_up\"></td><td class=\"noborder\">backup DOWN, going up </td>"
2752 "</tr><tr>\n"
2753 "<td class=\"active_down\"></td><td class=\"noborder\">active or backup DOWN &nbsp;</td>"
2754 "<td class=\"active_no_check\"></td><td class=\"noborder\">not checked </td>"
2755 "</tr><tr>\n"
2756 "<td class=\"maintain\"></td><td class=\"noborder\" colspan=\"3\">active or backup DOWN for maintenance (MAINT) &nbsp;</td>"
2757 "</tr><tr>\n"
2758 "<td class=\"active_draining\"></td><td class=\"noborder\" colspan=\"3\">active or backup SOFT STOPPED for maintenance &nbsp;</td>"
2759 "</tr></table>\n"
2760 "Note: \"NOLB\"/\"DRAIN\" = UP with load-balancing disabled."
2761 "</td>"
2762 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
2763 "<b>Display option:</b><ul style=\"margin-top: 0.25em;\">"
2764 "",
Willy Tarreau676c29e2019-10-09 10:50:01 +02002765 (appctx->ctx.stats.flags & STAT_HIDEVER) ? "" : (stats_version_string),
2766 pid, (appctx->ctx.stats.flags & STAT_SHNODE) ? " on " : "",
2767 (appctx->ctx.stats.flags & STAT_SHNODE) ? (uri->node ? uri->node : global.node) : "",
2768 (appctx->ctx.stats.flags & STAT_SHDESC) ? ": " : "",
2769 (appctx->ctx.stats.flags & STAT_SHDESC) ? (uri->desc ? uri->desc : global.desc) : "",
Yves Lafon95317282018-02-26 11:10:37 +01002770 pid, relative_pid, global.nbproc, global.nbthread,
William Lallemand74c24fb2016-11-21 17:18:36 +01002771 up / 86400, (up % 86400) / 3600,
2772 (up % 3600) / 60, (up % 60),
2773 global.rlimit_memmax ? ultoa(global.rlimit_memmax) : "unlimited",
2774 global.rlimit_memmax ? " MB" : "",
2775 global.rlimit_nofile,
2776 global.maxsock, global.maxconn, global.maxpipes,
2777 actconn, pipes_used, pipes_used+pipes_free, read_freq_ctr(&global.conn_per_sec),
Willy Tarreau1713c032019-05-23 12:23:55 +02002778 bps >= 1000000000UL ? (bps / 1000000000.0) : bps >= 1000000UL ? (bps / 1000000.0) : (bps / 1000.0),
2779 bps >= 1000000000UL ? 'G' : bps >= 1000000UL ? 'M' : 'k',
Willy Tarreau81036f22019-05-20 19:24:50 +02002780 tasks_run_queue_cur, nb_tasks_cur, ti->idle_pct
William Lallemand74c24fb2016-11-21 17:18:36 +01002781 );
2782
2783 /* scope_txt = search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
Christopher Fauleted7a0662019-01-14 11:07:34 +01002784 memcpy(scope_txt, scope_ptr, appctx->ctx.stats.scope_len);
William Lallemand74c24fb2016-11-21 17:18:36 +01002785 scope_txt[appctx->ctx.stats.scope_len] = '\0';
2786
2787 chunk_appendf(&trash,
2788 "<li><form method=\"GET\">Scope : <input value=\"%s\" name=\"" STAT_SCOPE_INPUT_NAME "\" size=\"8\" maxlength=\"%d\" tabindex=\"1\"/></form>\n",
2789 (appctx->ctx.stats.scope_len > 0) ? scope_txt : "",
2790 STAT_SCOPE_TXT_MAXLEN);
2791
2792 /* scope_txt = search pattern + search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
2793 scope_txt[0] = 0;
2794 if (appctx->ctx.stats.scope_len) {
2795 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Christopher Fauleted7a0662019-01-14 11:07:34 +01002796 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), scope_ptr, appctx->ctx.stats.scope_len);
William Lallemand74c24fb2016-11-21 17:18:36 +01002797 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
2798 }
2799
2800 if (appctx->ctx.stats.flags & STAT_HIDE_DOWN)
2801 chunk_appendf(&trash,
2802 "<li><a href=\"%s%s%s%s\">Show all servers</a><br>\n",
2803 uri->uri_prefix,
2804 "",
2805 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2806 scope_txt);
2807 else
2808 chunk_appendf(&trash,
2809 "<li><a href=\"%s%s%s%s\">Hide 'DOWN' servers</a><br>\n",
2810 uri->uri_prefix,
2811 ";up",
2812 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2813 scope_txt);
2814
2815 if (uri->refresh > 0) {
2816 if (appctx->ctx.stats.flags & STAT_NO_REFRESH)
2817 chunk_appendf(&trash,
2818 "<li><a href=\"%s%s%s%s\">Enable refresh</a><br>\n",
2819 uri->uri_prefix,
2820 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2821 "",
2822 scope_txt);
2823 else
2824 chunk_appendf(&trash,
2825 "<li><a href=\"%s%s%s%s\">Disable refresh</a><br>\n",
2826 uri->uri_prefix,
2827 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2828 ";norefresh",
2829 scope_txt);
2830 }
2831
2832 chunk_appendf(&trash,
2833 "<li><a href=\"%s%s%s%s\">Refresh now</a><br>\n",
2834 uri->uri_prefix,
2835 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2836 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2837 scope_txt);
2838
2839 chunk_appendf(&trash,
2840 "<li><a href=\"%s;csv%s%s\">CSV export</a><br>\n",
2841 uri->uri_prefix,
2842 (uri->refresh > 0) ? ";norefresh" : "",
2843 scope_txt);
2844
2845 chunk_appendf(&trash,
Christopher Faulet6338a082019-09-09 15:50:54 +02002846 "<li><a href=\"%s;json%s%s\">JSON export</a> (<a href=\"%s;json-schema\">schema</a>)<br>\n",
2847 uri->uri_prefix,
2848 (uri->refresh > 0) ? ";norefresh" : "",
2849 scope_txt, uri->uri_prefix);
2850
2851 chunk_appendf(&trash,
William Lallemand74c24fb2016-11-21 17:18:36 +01002852 "</ul></td>"
2853 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
2854 "<b>External resources:</b><ul style=\"margin-top: 0.25em;\">\n"
2855 "<li><a href=\"" PRODUCT_URL "\">Primary site</a><br>\n"
2856 "<li><a href=\"" PRODUCT_URL_UPD "\">Updates (v" PRODUCT_BRANCH ")</a><br>\n"
2857 "<li><a href=\"" PRODUCT_URL_DOC "\">Online manual</a><br>\n"
2858 "</ul>"
2859 "</td>"
2860 "</tr></table>\n"
2861 ""
2862 );
2863
2864 if (appctx->ctx.stats.st_code) {
2865 switch (appctx->ctx.stats.st_code) {
2866 case STAT_STATUS_DONE:
2867 chunk_appendf(&trash,
2868 "<p><div class=active_up>"
2869 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2870 "Action processed successfully."
2871 "</div>\n", uri->uri_prefix,
2872 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2873 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2874 scope_txt);
2875 break;
2876 case STAT_STATUS_NONE:
2877 chunk_appendf(&trash,
2878 "<p><div class=active_going_down>"
2879 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2880 "Nothing has changed."
2881 "</div>\n", uri->uri_prefix,
2882 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2883 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2884 scope_txt);
2885 break;
2886 case STAT_STATUS_PART:
2887 chunk_appendf(&trash,
2888 "<p><div class=active_going_down>"
2889 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2890 "Action partially processed.<br>"
2891 "Some server names are probably unknown or ambiguous (duplicated names in the backend)."
2892 "</div>\n", uri->uri_prefix,
2893 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2894 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2895 scope_txt);
2896 break;
2897 case STAT_STATUS_ERRP:
2898 chunk_appendf(&trash,
2899 "<p><div class=active_down>"
2900 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2901 "Action not processed because of invalid parameters."
2902 "<ul>"
2903 "<li>The action is maybe unknown.</li>"
Christopher Faulet2f9a41d2019-02-27 15:30:57 +01002904 "<li>Invalid key parameter (empty or too long).</li>"
William Lallemand74c24fb2016-11-21 17:18:36 +01002905 "<li>The backend name is probably unknown or ambiguous (duplicated names).</li>"
2906 "<li>Some server names are probably unknown or ambiguous (duplicated names in the backend).</li>"
2907 "</ul>"
2908 "</div>\n", uri->uri_prefix,
2909 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2910 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2911 scope_txt);
2912 break;
2913 case STAT_STATUS_EXCD:
2914 chunk_appendf(&trash,
2915 "<p><div class=active_down>"
2916 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2917 "<b>Action not processed : the buffer couldn't store all the data.<br>"
2918 "You should retry with less servers at a time.</b>"
2919 "</div>\n", uri->uri_prefix,
2920 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2921 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2922 scope_txt);
2923 break;
2924 case STAT_STATUS_DENY:
2925 chunk_appendf(&trash,
2926 "<p><div class=active_down>"
2927 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2928 "<b>Action denied.</b>"
2929 "</div>\n", uri->uri_prefix,
2930 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2931 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2932 scope_txt);
2933 break;
Christopher Faulet3c2ecf72019-02-27 16:41:27 +01002934 case STAT_STATUS_IVAL:
2935 chunk_appendf(&trash,
2936 "<p><div class=active_down>"
2937 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2938 "<b>Invalid requests (unsupported method or chunked encoded request).</b>"
2939 "</div>\n", uri->uri_prefix,
2940 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2941 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2942 scope_txt);
2943 break;
William Lallemand74c24fb2016-11-21 17:18:36 +01002944 default:
2945 chunk_appendf(&trash,
2946 "<p><div class=active_no_check>"
2947 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2948 "Unexpected result."
2949 "</div>\n", uri->uri_prefix,
2950 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2951 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2952 scope_txt);
2953 }
2954 chunk_appendf(&trash, "<p>\n");
2955 }
2956}
2957
2958/* Dumps the HTML stats trailer block to the trash. The caller is responsible
2959 * for clearing the trash if needed.
2960 */
2961static void stats_dump_html_end()
2962{
2963 chunk_appendf(&trash, "</body></html>\n");
2964}
2965
Simon Horman05ee2132017-01-04 09:37:25 +01002966/* Dumps the stats JSON header to the trash buffer which. The caller is responsible
2967 * for clearing it if needed.
2968 */
2969static void stats_dump_json_header()
2970{
2971 chunk_strcat(&trash, "[");
2972}
2973
2974
2975/* Dumps the JSON stats trailer block to the trash. The caller is responsible
2976 * for clearing the trash if needed.
2977 */
2978static void stats_dump_json_end()
2979{
2980 chunk_strcat(&trash, "]");
2981}
2982
Amaury Denoyelle98b81cb2020-10-02 18:32:04 +02002983/* Uses <appctx.ctx.stats.obj1> as a pointer to the current proxy and <obj2> as
2984 * a pointer to the current server/listener.
2985 */
2986static int stats_dump_proxies(struct stream_interface *si,
2987 struct htx *htx,
2988 struct uri_auth *uri)
2989{
2990 struct appctx *appctx = __objt_appctx(si->end);
2991 struct channel *rep = si_ic(si);
2992 struct proxy *px;
2993
2994 /* dump proxies */
2995 while (appctx->ctx.stats.obj1) {
2996 if (htx) {
2997 if (htx_almost_full(htx))
2998 goto full;
2999 }
3000 else {
3001 if (buffer_almost_full(&rep->buf))
3002 goto full;
3003 }
3004
3005 px = appctx->ctx.stats.obj1;
3006 /* skip the disabled proxies, global frontend and non-networked ones */
3007 if (px->state != PR_STSTOPPED && px->uuid > 0
3008 && (px->cap & (PR_CAP_FE | PR_CAP_BE))) {
3009
3010 if (stats_dump_proxy_to_buffer(si, htx, px, uri) == 0)
3011 return 0;
3012 }
3013
3014 appctx->ctx.stats.obj1 = px->next;
3015 appctx->ctx.stats.px_st = STAT_PX_ST_INIT;
3016 }
3017
3018 return 1;
3019
3020 full:
3021 si_rx_room_blk(si);
3022 return 0;
3023}
3024
William Lallemand74c24fb2016-11-21 17:18:36 +01003025/* This function dumps statistics onto the stream interface's read buffer in
3026 * either CSV or HTML format. <uri> contains some HTML-specific parameters that
3027 * are ignored for CSV format (hence <uri> may be NULL there). It returns 0 if
3028 * it had to stop writing data and an I/O is needed, 1 if the dump is finished
3029 * and the stream must be closed, or -1 in case of any error. This function is
3030 * used by both the CLI and the HTTP handlers.
3031 */
Christopher Fauletef779222018-10-31 08:47:01 +01003032static int stats_dump_stat_to_buffer(struct stream_interface *si, struct htx *htx,
3033 struct uri_auth *uri)
William Lallemand74c24fb2016-11-21 17:18:36 +01003034{
3035 struct appctx *appctx = __objt_appctx(si->end);
3036 struct channel *rep = si_ic(si);
William Lallemand74c24fb2016-11-21 17:18:36 +01003037
3038 chunk_reset(&trash);
3039
3040 switch (appctx->st2) {
3041 case STAT_ST_INIT:
3042 appctx->st2 = STAT_ST_HEAD; /* let's start producing data */
3043 /* fall through */
3044
3045 case STAT_ST_HEAD:
3046 if (appctx->ctx.stats.flags & STAT_FMT_HTML)
Willy Tarreau676c29e2019-10-09 10:50:01 +02003047 stats_dump_html_head(appctx, uri);
Christopher Faulet6338a082019-09-09 15:50:54 +02003048 else if (appctx->ctx.stats.flags & STAT_JSON_SCHM)
3049 stats_dump_json_schema(&trash);
Simon Horman05ee2132017-01-04 09:37:25 +01003050 else if (appctx->ctx.stats.flags & STAT_FMT_JSON)
Willy Tarreau9d7fb632017-04-11 07:53:04 +02003051 stats_dump_json_header();
William Lallemand74c24fb2016-11-21 17:18:36 +01003052 else if (!(appctx->ctx.stats.flags & STAT_FMT_TYPED))
Amaury Denoyelle50660a82020-10-05 11:49:39 +02003053 stats_dump_csv_header(appctx->ctx.stats.domain);
William Lallemand74c24fb2016-11-21 17:18:36 +01003054
Christopher Fauletef779222018-10-31 08:47:01 +01003055 if (!stats_putchk(rep, htx, &trash))
3056 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01003057
Christopher Faulet6338a082019-09-09 15:50:54 +02003058 if (appctx->ctx.stats.flags & STAT_JSON_SCHM) {
3059 appctx->st2 = STAT_ST_FIN;
3060 return 1;
3061 }
William Lallemand74c24fb2016-11-21 17:18:36 +01003062 appctx->st2 = STAT_ST_INFO;
3063 /* fall through */
3064
3065 case STAT_ST_INFO:
3066 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
3067 stats_dump_html_info(si, uri);
Christopher Fauletef779222018-10-31 08:47:01 +01003068 if (!stats_putchk(rep, htx, &trash))
3069 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01003070 }
3071
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +02003072 appctx->ctx.stats.obj1 = proxies_list;
William Lallemand74c24fb2016-11-21 17:18:36 +01003073 appctx->ctx.stats.px_st = STAT_PX_ST_INIT;
3074 appctx->st2 = STAT_ST_LIST;
3075 /* fall through */
3076
3077 case STAT_ST_LIST:
3078 /* dump proxies */
Amaury Denoyelle98b81cb2020-10-02 18:32:04 +02003079 if (!stats_dump_proxies(si, htx, uri))
3080 return 0;
William Lallemand74c24fb2016-11-21 17:18:36 +01003081
3082 appctx->st2 = STAT_ST_END;
3083 /* fall through */
3084
3085 case STAT_ST_END:
Simon Horman05ee2132017-01-04 09:37:25 +01003086 if (appctx->ctx.stats.flags & (STAT_FMT_HTML|STAT_FMT_JSON)) {
3087 if (appctx->ctx.stats.flags & STAT_FMT_HTML)
3088 stats_dump_html_end();
3089 else
3090 stats_dump_json_end();
Christopher Fauletef779222018-10-31 08:47:01 +01003091 if (!stats_putchk(rep, htx, &trash))
3092 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01003093 }
3094
3095 appctx->st2 = STAT_ST_FIN;
3096 /* fall through */
3097
3098 case STAT_ST_FIN:
3099 return 1;
3100
3101 default:
3102 /* unknown state ! */
3103 appctx->st2 = STAT_ST_FIN;
3104 return -1;
3105 }
Christopher Fauletef779222018-10-31 08:47:01 +01003106
3107 full:
3108 si_rx_room_blk(si);
3109 return 0;
3110
William Lallemand74c24fb2016-11-21 17:18:36 +01003111}
3112
3113/* We reached the stats page through a POST request. The appctx is
3114 * expected to have already been allocated by the caller.
3115 * Parse the posted data and enable/disable servers if necessary.
3116 * Returns 1 if request was parsed or zero if it needs more data.
3117 */
3118static int stats_process_http_post(struct stream_interface *si)
3119{
3120 struct stream *s = si_strm(si);
3121 struct appctx *appctx = objt_appctx(si->end);
3122
3123 struct proxy *px = NULL;
3124 struct server *sv = NULL;
3125
3126 char key[LINESIZE];
3127 int action = ST_ADM_ACTION_NONE;
3128 int reprocess = 0;
3129
3130 int total_servers = 0;
3131 int altered_servers = 0;
3132
3133 char *first_param, *cur_param, *next_param, *end_params;
3134 char *st_cur_param = NULL;
3135 char *st_next_param = NULL;
3136
Christopher Fauleta3618372018-12-13 21:59:56 +01003137 struct buffer *temp = get_trash_chunk();
William Lallemand74c24fb2016-11-21 17:18:36 +01003138
Christopher Fauletb7f88902019-07-15 21:56:43 +02003139 struct htx *htx = htxbuf(&s->req.buf);
3140 struct htx_blk *blk;
Christopher Fauleta3618372018-12-13 21:59:56 +01003141
Christopher Fauletb7f88902019-07-15 21:56:43 +02003142 /* we need more data */
3143 if (s->txn->req.msg_state < HTTP_MSG_DONE) {
3144 /* check if we can receive more */
3145 if (htx_free_data_space(htx) <= global.tune.maxrewrite) {
3146 appctx->ctx.stats.st_code = STAT_STATUS_EXCD;
3147 goto out;
Christopher Fauleta3618372018-12-13 21:59:56 +01003148 }
Christopher Fauletb7f88902019-07-15 21:56:43 +02003149 goto wait;
3150 }
Christopher Fauleta3618372018-12-13 21:59:56 +01003151
Christopher Fauletb7f88902019-07-15 21:56:43 +02003152 /* The request was fully received. Copy data */
3153 blk = htx_get_head_blk(htx);
3154 while (blk) {
3155 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Fauleta3618372018-12-13 21:59:56 +01003156
Christopher Fauletb7f88902019-07-15 21:56:43 +02003157 if (type == HTX_BLK_EOM || type == HTX_BLK_TLR || type == HTX_BLK_EOT)
3158 break;
3159 if (type == HTX_BLK_DATA) {
3160 struct ist v = htx_get_blk_value(htx, blk);
Christopher Fauleta3618372018-12-13 21:59:56 +01003161
Christopher Fauletb7f88902019-07-15 21:56:43 +02003162 if (!chunk_memcat(temp, v.ptr, v.len)) {
Christopher Faulet2f9a41d2019-02-27 15:30:57 +01003163 appctx->ctx.stats.st_code = STAT_STATUS_EXCD;
3164 goto out;
3165 }
Christopher Fauleta3618372018-12-13 21:59:56 +01003166 }
Christopher Fauletb7f88902019-07-15 21:56:43 +02003167 blk = htx_get_next_blk(htx, blk);
William Lallemand74c24fb2016-11-21 17:18:36 +01003168 }
3169
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003170 first_param = temp->area;
Christopher Fauleta3618372018-12-13 21:59:56 +01003171 end_params = temp->area + temp->data;
William Lallemand74c24fb2016-11-21 17:18:36 +01003172 cur_param = next_param = end_params;
3173 *end_params = '\0';
3174
3175 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
3176
3177 /*
3178 * Parse the parameters in reverse order to only store the last value.
3179 * From the html form, the backend and the action are at the end.
3180 */
3181 while (cur_param > first_param) {
3182 char *value;
3183 int poffset, plen;
3184
3185 cur_param--;
3186
3187 if ((*cur_param == '&') || (cur_param == first_param)) {
3188 reprocess_servers:
3189 /* Parse the key */
3190 poffset = (cur_param != first_param ? 1 : 0);
3191 plen = next_param - cur_param + (cur_param == first_param ? 1 : 0);
3192 if ((plen > 0) && (plen <= sizeof(key))) {
3193 strncpy(key, cur_param + poffset, plen);
3194 key[plen - 1] = '\0';
3195 } else {
Christopher Faulet2f9a41d2019-02-27 15:30:57 +01003196 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
William Lallemand74c24fb2016-11-21 17:18:36 +01003197 goto out;
3198 }
3199
3200 /* Parse the value */
3201 value = key;
3202 while (*value != '\0' && *value != '=') {
3203 value++;
3204 }
3205 if (*value == '=') {
3206 /* Ok, a value is found, we can mark the end of the key */
3207 *value++ = '\0';
3208 }
Willy Tarreau62ba9ba2020-04-23 17:54:47 +02003209 if (url_decode(key, 1) < 0 || url_decode(value, 1) < 0)
William Lallemand74c24fb2016-11-21 17:18:36 +01003210 break;
3211
3212 /* Now we can check the key to see what to do */
3213 if (!px && (strcmp(key, "b") == 0)) {
3214 if ((px = proxy_be_by_name(value)) == NULL) {
3215 /* the backend name is unknown or ambiguous (duplicate names) */
3216 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
3217 goto out;
3218 }
3219 }
3220 else if (!action && (strcmp(key, "action") == 0)) {
3221 if (strcmp(value, "ready") == 0) {
3222 action = ST_ADM_ACTION_READY;
3223 }
3224 else if (strcmp(value, "drain") == 0) {
3225 action = ST_ADM_ACTION_DRAIN;
3226 }
3227 else if (strcmp(value, "maint") == 0) {
3228 action = ST_ADM_ACTION_MAINT;
3229 }
3230 else if (strcmp(value, "shutdown") == 0) {
3231 action = ST_ADM_ACTION_SHUTDOWN;
3232 }
3233 else if (strcmp(value, "dhlth") == 0) {
3234 action = ST_ADM_ACTION_DHLTH;
3235 }
3236 else if (strcmp(value, "ehlth") == 0) {
3237 action = ST_ADM_ACTION_EHLTH;
3238 }
3239 else if (strcmp(value, "hrunn") == 0) {
3240 action = ST_ADM_ACTION_HRUNN;
3241 }
3242 else if (strcmp(value, "hnolb") == 0) {
3243 action = ST_ADM_ACTION_HNOLB;
3244 }
3245 else if (strcmp(value, "hdown") == 0) {
3246 action = ST_ADM_ACTION_HDOWN;
3247 }
3248 else if (strcmp(value, "dagent") == 0) {
3249 action = ST_ADM_ACTION_DAGENT;
3250 }
3251 else if (strcmp(value, "eagent") == 0) {
3252 action = ST_ADM_ACTION_EAGENT;
3253 }
3254 else if (strcmp(value, "arunn") == 0) {
3255 action = ST_ADM_ACTION_ARUNN;
3256 }
3257 else if (strcmp(value, "adown") == 0) {
3258 action = ST_ADM_ACTION_ADOWN;
3259 }
3260 /* else these are the old supported methods */
3261 else if (strcmp(value, "disable") == 0) {
3262 action = ST_ADM_ACTION_DISABLE;
3263 }
3264 else if (strcmp(value, "enable") == 0) {
3265 action = ST_ADM_ACTION_ENABLE;
3266 }
3267 else if (strcmp(value, "stop") == 0) {
3268 action = ST_ADM_ACTION_STOP;
3269 }
3270 else if (strcmp(value, "start") == 0) {
3271 action = ST_ADM_ACTION_START;
3272 }
3273 else {
3274 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
3275 goto out;
3276 }
3277 }
3278 else if (strcmp(key, "s") == 0) {
3279 if (!(px && action)) {
3280 /*
3281 * Indicates that we'll need to reprocess the parameters
3282 * as soon as backend and action are known
3283 */
3284 if (!reprocess) {
3285 st_cur_param = cur_param;
3286 st_next_param = next_param;
3287 }
3288 reprocess = 1;
3289 }
3290 else if ((sv = findserver(px, value)) != NULL) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003291 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
William Lallemand74c24fb2016-11-21 17:18:36 +01003292 switch (action) {
3293 case ST_ADM_ACTION_DISABLE:
Emeric Brun52a91d32017-08-31 14:41:55 +02003294 if (!(sv->cur_admin & SRV_ADMF_FMAINT)) {
William Lallemand74c24fb2016-11-21 17:18:36 +01003295 altered_servers++;
3296 total_servers++;
3297 srv_set_admin_flag(sv, SRV_ADMF_FMAINT, "'disable' on stats page");
3298 }
3299 break;
3300 case ST_ADM_ACTION_ENABLE:
Emeric Brun52a91d32017-08-31 14:41:55 +02003301 if (sv->cur_admin & SRV_ADMF_FMAINT) {
William Lallemand74c24fb2016-11-21 17:18:36 +01003302 altered_servers++;
3303 total_servers++;
3304 srv_clr_admin_flag(sv, SRV_ADMF_FMAINT);
3305 }
3306 break;
3307 case ST_ADM_ACTION_STOP:
Emeric Brun52a91d32017-08-31 14:41:55 +02003308 if (!(sv->cur_admin & SRV_ADMF_FDRAIN)) {
William Lallemand74c24fb2016-11-21 17:18:36 +01003309 srv_set_admin_flag(sv, SRV_ADMF_FDRAIN, "'stop' on stats page");
3310 altered_servers++;
3311 total_servers++;
3312 }
3313 break;
3314 case ST_ADM_ACTION_START:
Emeric Brun52a91d32017-08-31 14:41:55 +02003315 if (sv->cur_admin & SRV_ADMF_FDRAIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +01003316 srv_clr_admin_flag(sv, SRV_ADMF_FDRAIN);
3317 altered_servers++;
3318 total_servers++;
3319 }
3320 break;
3321 case ST_ADM_ACTION_DHLTH:
3322 if (sv->check.state & CHK_ST_CONFIGURED) {
3323 sv->check.state &= ~CHK_ST_ENABLED;
3324 altered_servers++;
3325 total_servers++;
3326 }
3327 break;
3328 case ST_ADM_ACTION_EHLTH:
3329 if (sv->check.state & CHK_ST_CONFIGURED) {
3330 sv->check.state |= CHK_ST_ENABLED;
3331 altered_servers++;
3332 total_servers++;
3333 }
3334 break;
3335 case ST_ADM_ACTION_HRUNN:
3336 if (!(sv->track)) {
3337 sv->check.health = sv->check.rise + sv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02003338 srv_set_running(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01003339 altered_servers++;
3340 total_servers++;
3341 }
3342 break;
3343 case ST_ADM_ACTION_HNOLB:
3344 if (!(sv->track)) {
3345 sv->check.health = sv->check.rise + sv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02003346 srv_set_stopping(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01003347 altered_servers++;
3348 total_servers++;
3349 }
3350 break;
3351 case ST_ADM_ACTION_HDOWN:
3352 if (!(sv->track)) {
3353 sv->check.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02003354 srv_set_stopped(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01003355 altered_servers++;
3356 total_servers++;
3357 }
3358 break;
3359 case ST_ADM_ACTION_DAGENT:
3360 if (sv->agent.state & CHK_ST_CONFIGURED) {
3361 sv->agent.state &= ~CHK_ST_ENABLED;
3362 altered_servers++;
3363 total_servers++;
3364 }
3365 break;
3366 case ST_ADM_ACTION_EAGENT:
3367 if (sv->agent.state & CHK_ST_CONFIGURED) {
3368 sv->agent.state |= CHK_ST_ENABLED;
3369 altered_servers++;
3370 total_servers++;
3371 }
3372 break;
3373 case ST_ADM_ACTION_ARUNN:
3374 if (sv->agent.state & CHK_ST_ENABLED) {
3375 sv->agent.health = sv->agent.rise + sv->agent.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02003376 srv_set_running(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01003377 altered_servers++;
3378 total_servers++;
3379 }
3380 break;
3381 case ST_ADM_ACTION_ADOWN:
3382 if (sv->agent.state & CHK_ST_ENABLED) {
3383 sv->agent.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02003384 srv_set_stopped(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01003385 altered_servers++;
3386 total_servers++;
3387 }
3388 break;
3389 case ST_ADM_ACTION_READY:
3390 srv_adm_set_ready(sv);
3391 altered_servers++;
3392 total_servers++;
3393 break;
3394 case ST_ADM_ACTION_DRAIN:
3395 srv_adm_set_drain(sv);
3396 altered_servers++;
3397 total_servers++;
3398 break;
3399 case ST_ADM_ACTION_MAINT:
3400 srv_adm_set_maint(sv);
3401 altered_servers++;
3402 total_servers++;
3403 break;
3404 case ST_ADM_ACTION_SHUTDOWN:
3405 if (px->state != PR_STSTOPPED) {
Willy Tarreauaf7ea812019-11-14 16:42:04 +01003406 srv_shutdown_streams(sv, SF_ERR_KILLED);
William Lallemand74c24fb2016-11-21 17:18:36 +01003407 altered_servers++;
3408 total_servers++;
3409 }
3410 break;
3411 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003412 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
William Lallemand74c24fb2016-11-21 17:18:36 +01003413 } else {
3414 /* the server name is unknown or ambiguous (duplicate names) */
3415 total_servers++;
3416 }
3417 }
3418 if (reprocess && px && action) {
3419 /* Now, we know the backend and the action chosen by the user.
3420 * We can safely restart from the first server parameter
3421 * to reprocess them
3422 */
3423 cur_param = st_cur_param;
3424 next_param = st_next_param;
3425 reprocess = 0;
3426 goto reprocess_servers;
3427 }
3428
3429 next_param = cur_param;
3430 }
3431 }
3432
3433 if (total_servers == 0) {
3434 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
3435 }
3436 else if (altered_servers == 0) {
3437 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
3438 }
3439 else if (altered_servers == total_servers) {
3440 appctx->ctx.stats.st_code = STAT_STATUS_DONE;
3441 }
3442 else {
3443 appctx->ctx.stats.st_code = STAT_STATUS_PART;
3444 }
3445 out:
3446 return 1;
Christopher Faulet2f9a41d2019-02-27 15:30:57 +01003447 wait:
3448 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
3449 return 0;
Christopher Fauletef779222018-10-31 08:47:01 +01003450}
3451
3452
Christopher Fauletb7f88902019-07-15 21:56:43 +02003453static int stats_send_http_headers(struct stream_interface *si, struct htx *htx)
Christopher Fauletef779222018-10-31 08:47:01 +01003454{
3455 struct stream *s = si_strm(si);
3456 struct uri_auth *uri = s->be->uri_auth;
3457 struct appctx *appctx = __objt_appctx(si->end);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003458 struct htx_sl *sl;
3459 unsigned int flags;
Christopher Fauletef779222018-10-31 08:47:01 +01003460
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003461 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);
3462 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), ist("200"), ist("OK"));
3463 if (!sl)
Christopher Fauletef779222018-10-31 08:47:01 +01003464 goto full;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003465 sl->info.res.status = 200;
Christopher Fauletef779222018-10-31 08:47:01 +01003466
Christopher Fauletb829f4c2019-03-29 16:13:55 +01003467 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")))
Christopher Fauletef779222018-10-31 08:47:01 +01003468 goto full;
3469 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
3470 if (!htx_add_header(htx, ist("Content-Type"), ist("text/html")))
3471 goto full;
3472 }
Christopher Faulet6338a082019-09-09 15:50:54 +02003473 else if (appctx->ctx.stats.flags & (STAT_FMT_JSON|STAT_JSON_SCHM)) {
3474 if (!htx_add_header(htx, ist("Content-Type"), ist("application/json")))
3475 goto full;
3476 }
Christopher Fauletef779222018-10-31 08:47:01 +01003477 else {
3478 if (!htx_add_header(htx, ist("Content-Type"), ist("text/plain")))
3479 goto full;
3480 }
3481
3482 if (uri->refresh > 0 && !(appctx->ctx.stats.flags & STAT_NO_REFRESH)) {
3483 const char *refresh = U2A(uri->refresh);
3484 if (!htx_add_header(htx, ist("Refresh"), ist2(refresh, strlen(refresh))))
3485 goto full;
3486 }
3487
3488 if (appctx->ctx.stats.flags & STAT_CHUNKED) {
3489 if (!htx_add_header(htx, ist("Transfer-Encoding"), ist("chunked")))
3490 goto full;
3491 }
3492
3493 if (!htx_add_endof(htx, HTX_BLK_EOH))
3494 goto full;
3495
Christopher Faulet1e2d6362019-02-27 16:28:48 +01003496 channel_add_input(&s->res, htx->data);
Christopher Fauletef779222018-10-31 08:47:01 +01003497 return 1;
3498
3499 full:
3500 htx_reset(htx);
3501 si_rx_room_blk(si);
3502 return 0;
William Lallemand74c24fb2016-11-21 17:18:36 +01003503}
3504
Christopher Fauletef779222018-10-31 08:47:01 +01003505
Christopher Fauletb7f88902019-07-15 21:56:43 +02003506static int stats_send_http_redirect(struct stream_interface *si, struct htx *htx)
Christopher Fauletef779222018-10-31 08:47:01 +01003507{
3508 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
3509 struct stream *s = si_strm(si);
3510 struct uri_auth *uri = s->be->uri_auth;
3511 struct appctx *appctx = __objt_appctx(si->end);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003512 struct htx_sl *sl;
3513 unsigned int flags;
Christopher Fauletef779222018-10-31 08:47:01 +01003514
3515 /* scope_txt = search pattern + search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
3516 scope_txt[0] = 0;
3517 if (appctx->ctx.stats.scope_len) {
Christopher Fauleted7a0662019-01-14 11:07:34 +01003518 const char *scope_ptr = stats_scope_ptr(appctx, si);
3519
Christopher Fauletef779222018-10-31 08:47:01 +01003520 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Christopher Fauleted7a0662019-01-14 11:07:34 +01003521 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), scope_ptr, appctx->ctx.stats.scope_len);
Christopher Fauletef779222018-10-31 08:47:01 +01003522 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
3523 }
3524
3525 /* We don't want to land on the posted stats page because a refresh will
3526 * repost the data. We don't want this to happen on accident so we redirect
3527 * the browse to the stats page with a GET.
3528 */
3529 chunk_printf(&trash, "%s;st=%s%s%s%s",
3530 uri->uri_prefix,
3531 ((appctx->ctx.stats.st_code > STAT_STATUS_INIT) &&
3532 (appctx->ctx.stats.st_code < STAT_STATUS_SIZE) &&
3533 stat_status_codes[appctx->ctx.stats.st_code]) ?
3534 stat_status_codes[appctx->ctx.stats.st_code] :
3535 stat_status_codes[STAT_STATUS_UNKN],
3536 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3537 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
3538 scope_txt);
3539
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003540 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_CHNK);
3541 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), ist("303"), ist("See Other"));
3542 if (!sl)
Christopher Fauletef779222018-10-31 08:47:01 +01003543 goto full;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003544 sl->info.res.status = 303;
Christopher Fauletef779222018-10-31 08:47:01 +01003545
3546 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")) ||
Christopher Fauletef779222018-10-31 08:47:01 +01003547 !htx_add_header(htx, ist("Content-Type"), ist("text/plain")) ||
3548 !htx_add_header(htx, ist("Content-Length"), ist("0")) ||
3549 !htx_add_header(htx, ist("Location"), ist2(trash.area, trash.data)))
3550 goto full;
3551
3552 if (!htx_add_endof(htx, HTX_BLK_EOH))
3553 goto full;
3554
Christopher Faulet1e2d6362019-02-27 16:28:48 +01003555 channel_add_input(&s->res, htx->data);
Christopher Fauletef779222018-10-31 08:47:01 +01003556 return 1;
3557
3558full:
3559 htx_reset(htx);
3560 si_rx_room_blk(si);
3561 return 0;
3562}
William Lallemand74c24fb2016-11-21 17:18:36 +01003563
Simon Horman05ee2132017-01-04 09:37:25 +01003564
William Lallemand74c24fb2016-11-21 17:18:36 +01003565/* This I/O handler runs as an applet embedded in a stream interface. It is
3566 * used to send HTTP stats over a TCP socket. The mechanism is very simple.
3567 * appctx->st0 contains the operation in progress (dump, done). The handler
3568 * automatically unregisters itself once transfer is complete.
3569 */
Christopher Fauletb7f88902019-07-15 21:56:43 +02003570static void http_stats_io_handler(struct appctx *appctx)
Christopher Fauletef779222018-10-31 08:47:01 +01003571{
3572 struct stream_interface *si = appctx->owner;
3573 struct stream *s = si_strm(si);
3574 struct channel *req = si_oc(si);
3575 struct channel *res = si_ic(si);
3576 struct htx *req_htx, *res_htx;
3577
Amaury Denoyelle072f97e2020-10-05 11:49:37 +02003578 /* only proxy stats are available via http */
3579 appctx->ctx.stats.domain = STATS_DOMAIN_PROXY;
3580
Christopher Fauletef779222018-10-31 08:47:01 +01003581 res_htx = htx_from_buf(&res->buf);
3582
3583 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
3584 goto out;
3585
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05003586 /* Check if the input buffer is available. */
Christopher Fauletef779222018-10-31 08:47:01 +01003587 if (!b_size(&res->buf)) {
3588 si_rx_room_blk(si);
3589 goto out;
3590 }
3591
3592 /* check that the output is not closed */
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003593 if (res->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_SHUTR))
3594 appctx->st0 = STAT_HTTP_END;
Christopher Fauletef779222018-10-31 08:47:01 +01003595
3596 /* all states are processed in sequence */
3597 if (appctx->st0 == STAT_HTTP_HEAD) {
Christopher Fauletb7f88902019-07-15 21:56:43 +02003598 if (stats_send_http_headers(si, res_htx)) {
Christopher Fauletef779222018-10-31 08:47:01 +01003599 if (s->txn->meth == HTTP_METH_HEAD)
3600 appctx->st0 = STAT_HTTP_DONE;
3601 else
3602 appctx->st0 = STAT_HTTP_DUMP;
3603 }
3604 }
3605
3606 if (appctx->st0 == STAT_HTTP_DUMP) {
3607 if (stats_dump_stat_to_buffer(si, res_htx, s->be->uri_auth))
3608 appctx->st0 = STAT_HTTP_DONE;
3609 }
3610
3611 if (appctx->st0 == STAT_HTTP_POST) {
3612 if (stats_process_http_post(si))
3613 appctx->st0 = STAT_HTTP_LAST;
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003614 else if (req->flags & CF_SHUTR)
Christopher Fauletef779222018-10-31 08:47:01 +01003615 appctx->st0 = STAT_HTTP_DONE;
3616 }
3617
3618 if (appctx->st0 == STAT_HTTP_LAST) {
Christopher Fauletb7f88902019-07-15 21:56:43 +02003619 if (stats_send_http_redirect(si, res_htx))
Christopher Fauletef779222018-10-31 08:47:01 +01003620 appctx->st0 = STAT_HTTP_DONE;
3621 }
3622
3623 if (appctx->st0 == STAT_HTTP_DONE) {
Christopher Faulet54b5e212019-06-04 10:08:28 +02003624 /* Don't add TLR because mux-h1 will take care of it */
Christopher Faulet810df062020-07-22 16:20:34 +02003625 res_htx->flags |= HTX_FL_EOI; /* no more data are expected. Only EOM remains to add now */
Christopher Fauletef779222018-10-31 08:47:01 +01003626 if (!htx_add_endof(res_htx, HTX_BLK_EOM)) {
3627 si_rx_room_blk(si);
3628 goto out;
3629 }
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003630 channel_add_input(&s->res, 1);
3631 appctx->st0 = STAT_HTTP_END;
Christopher Fauletef779222018-10-31 08:47:01 +01003632 }
3633
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003634 if (appctx->st0 == STAT_HTTP_END) {
3635 if (!(res->flags & CF_SHUTR)) {
Christopher Fauletef779222018-10-31 08:47:01 +01003636 res->flags |= CF_READ_NULL;
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003637 si_shutr(si);
3638 }
3639
3640 /* eat the whole request */
3641 if (co_data(req)) {
3642 req_htx = htx_from_buf(&req->buf);
3643 co_htx_skip(req, req_htx, co_data(req));
3644 htx_to_buf(req_htx, &req->buf);
Christopher Fauletef779222018-10-31 08:47:01 +01003645 }
3646 }
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003647
Christopher Fauletef779222018-10-31 08:47:01 +01003648 out:
3649 /* we have left the request in the buffer for the case where we
3650 * process a POST, and this automatically re-enables activity on
3651 * read. It's better to indicate that we want to stop reading when
3652 * we're sending, so that we know there's at most one direction
3653 * deciding to wake the applet up. It saves it from looping when
3654 * emitting large blocks into small TCP windows.
3655 */
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003656 htx_to_buf(res_htx, &res->buf);
3657 if (!channel_is_empty(res))
Christopher Fauletef779222018-10-31 08:47:01 +01003658 si_stop_get(si);
Christopher Fauletef779222018-10-31 08:47:01 +01003659}
3660
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003661/* Dump all fields from <info> into <out> using the "show info" format (name: value) */
Willy Tarreau83061a82018-07-13 11:56:34 +02003662static int stats_dump_info_fields(struct buffer *out,
Willy Tarreau43241ff2019-10-09 11:27:51 +02003663 const struct field *info, unsigned int flags)
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003664{
3665 int field;
3666
3667 for (field = 0; field < INF_TOTAL_FIELDS; field++) {
3668 if (!field_format(info, field))
3669 continue;
3670
Willy Tarreaueaa55372019-10-09 07:39:11 +02003671 if (!chunk_appendf(out, "%s: ", info_fields[field].name))
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003672 return 0;
3673 if (!stats_emit_raw_data_field(out, &info[field]))
3674 return 0;
Willy Tarreau6b19b142019-10-09 15:44:21 +02003675 if ((flags & STAT_SHOW_FDESC) && !chunk_appendf(out, ":\"%s\"", info_fields[field].desc))
3676 return 0;
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003677 if (!chunk_strcat(out, "\n"))
3678 return 0;
3679 }
3680 return 1;
3681}
3682
3683/* Dump all fields from <info> into <out> using the "show info typed" format */
Willy Tarreau83061a82018-07-13 11:56:34 +02003684static int stats_dump_typed_info_fields(struct buffer *out,
Willy Tarreau43241ff2019-10-09 11:27:51 +02003685 const struct field *info, unsigned int flags)
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003686{
3687 int field;
3688
3689 for (field = 0; field < INF_TOTAL_FIELDS; field++) {
3690 if (!field_format(info, field))
3691 continue;
3692
Willy Tarreaueaa55372019-10-09 07:39:11 +02003693 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 +01003694 return 0;
3695 if (!stats_emit_field_tags(out, &info[field], ':'))
3696 return 0;
3697 if (!stats_emit_typed_data_field(out, &info[field]))
3698 return 0;
Willy Tarreau6b19b142019-10-09 15:44:21 +02003699 if ((flags & STAT_SHOW_FDESC) && !chunk_appendf(out, ":\"%s\"", info_fields[field].desc))
3700 return 0;
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003701 if (!chunk_strcat(out, "\n"))
3702 return 0;
3703 }
3704 return 1;
3705}
3706
3707/* Fill <info> with HAProxy global info. <info> is preallocated
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05003708 * array of length <len>. The length of the array must be
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003709 * INF_TOTAL_FIELDS. If this length is less then this value, the
3710 * function returns 0, otherwise, it returns 1.
3711 */
3712int stats_fill_info(struct field *info, int len)
3713{
3714 unsigned int up = (now.tv_sec - start_date.tv_sec);
Willy Tarreau83061a82018-07-13 11:56:34 +02003715 struct buffer *out = get_trash_chunk();
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003716
3717#ifdef USE_OPENSSL
3718 int ssl_sess_rate = read_freq_ctr(&global.ssl_per_sec);
3719 int ssl_key_rate = read_freq_ctr(&global.ssl_fe_keys_per_sec);
3720 int ssl_reuse = 0;
3721
3722 if (ssl_key_rate < ssl_sess_rate) {
3723 /* count the ssl reuse ratio and avoid overflows in both directions */
3724 ssl_reuse = 100 - (100 * ssl_key_rate + (ssl_sess_rate - 1) / 2) / ssl_sess_rate;
3725 }
3726#endif
3727
3728 if (len < INF_TOTAL_FIELDS)
3729 return 0;
3730
3731 chunk_reset(out);
3732 memset(info, 0, sizeof(*info) * len);
3733
3734 info[INF_NAME] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, PRODUCT_NAME);
Willy Tarreau909b9d82019-01-04 18:20:32 +01003735 info[INF_VERSION] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, haproxy_version);
3736 info[INF_RELEASE_DATE] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, haproxy_date);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003737
Yves Lafon95317282018-02-26 11:10:37 +01003738 info[INF_NBTHREAD] = mkf_u32(FO_CONFIG|FS_SERVICE, global.nbthread);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003739 info[INF_NBPROC] = mkf_u32(FO_CONFIG|FS_SERVICE, global.nbproc);
3740 info[INF_PROCESS_NUM] = mkf_u32(FO_KEY, relative_pid);
3741 info[INF_PID] = mkf_u32(FO_STATUS, pid);
3742
3743 info[INF_UPTIME] = mkf_str(FN_DURATION, chunk_newstr(out));
3744 chunk_appendf(out, "%ud %uh%02um%02us", up / 86400, (up % 86400) / 3600, (up % 3600) / 60, (up % 60));
3745
3746 info[INF_UPTIME_SEC] = mkf_u32(FN_DURATION, up);
3747 info[INF_MEMMAX_MB] = mkf_u32(FO_CONFIG|FN_LIMIT, global.rlimit_memmax);
3748 info[INF_POOL_ALLOC_MB] = mkf_u32(0, (unsigned)(pool_total_allocated() / 1048576L));
3749 info[INF_POOL_USED_MB] = mkf_u32(0, (unsigned)(pool_total_used() / 1048576L));
3750 info[INF_POOL_FAILED] = mkf_u32(FN_COUNTER, pool_total_failures());
3751 info[INF_ULIMIT_N] = mkf_u32(FO_CONFIG|FN_LIMIT, global.rlimit_nofile);
3752 info[INF_MAXSOCK] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxsock);
3753 info[INF_MAXCONN] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxconn);
3754 info[INF_HARD_MAXCONN] = mkf_u32(FO_CONFIG|FN_LIMIT, global.hardmaxconn);
3755 info[INF_CURR_CONN] = mkf_u32(0, actconn);
3756 info[INF_CUM_CONN] = mkf_u32(FN_COUNTER, totalconn);
3757 info[INF_CUM_REQ] = mkf_u32(FN_COUNTER, global.req_count);
3758#ifdef USE_OPENSSL
3759 info[INF_MAX_SSL_CONNS] = mkf_u32(FN_MAX, global.maxsslconn);
3760 info[INF_CURR_SSL_CONNS] = mkf_u32(0, sslconns);
3761 info[INF_CUM_SSL_CONNS] = mkf_u32(FN_COUNTER, totalsslconns);
3762#endif
3763 info[INF_MAXPIPES] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxpipes);
3764 info[INF_PIPES_USED] = mkf_u32(0, pipes_used);
3765 info[INF_PIPES_FREE] = mkf_u32(0, pipes_free);
3766 info[INF_CONN_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&global.conn_per_sec));
3767 info[INF_CONN_RATE_LIMIT] = mkf_u32(FO_CONFIG|FN_LIMIT, global.cps_lim);
3768 info[INF_MAX_CONN_RATE] = mkf_u32(FN_MAX, global.cps_max);
3769 info[INF_SESS_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&global.sess_per_sec));
3770 info[INF_SESS_RATE_LIMIT] = mkf_u32(FO_CONFIG|FN_LIMIT, global.sps_lim);
3771 info[INF_MAX_SESS_RATE] = mkf_u32(FN_RATE, global.sps_max);
3772
3773#ifdef USE_OPENSSL
3774 info[INF_SSL_RATE] = mkf_u32(FN_RATE, ssl_sess_rate);
3775 info[INF_SSL_RATE_LIMIT] = mkf_u32(FO_CONFIG|FN_LIMIT, global.ssl_lim);
3776 info[INF_MAX_SSL_RATE] = mkf_u32(FN_MAX, global.ssl_max);
3777 info[INF_SSL_FRONTEND_KEY_RATE] = mkf_u32(0, ssl_key_rate);
3778 info[INF_SSL_FRONTEND_MAX_KEY_RATE] = mkf_u32(FN_MAX, global.ssl_fe_keys_max);
3779 info[INF_SSL_FRONTEND_SESSION_REUSE_PCT] = mkf_u32(0, ssl_reuse);
3780 info[INF_SSL_BACKEND_KEY_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&global.ssl_be_keys_per_sec));
3781 info[INF_SSL_BACKEND_MAX_KEY_RATE] = mkf_u32(FN_MAX, global.ssl_be_keys_max);
3782 info[INF_SSL_CACHE_LOOKUPS] = mkf_u32(FN_COUNTER, global.shctx_lookups);
3783 info[INF_SSL_CACHE_MISSES] = mkf_u32(FN_COUNTER, global.shctx_misses);
3784#endif
3785 info[INF_COMPRESS_BPS_IN] = mkf_u32(FN_RATE, read_freq_ctr(&global.comp_bps_in));
3786 info[INF_COMPRESS_BPS_OUT] = mkf_u32(FN_RATE, read_freq_ctr(&global.comp_bps_out));
3787 info[INF_COMPRESS_BPS_RATE_LIM] = mkf_u32(FO_CONFIG|FN_LIMIT, global.comp_rate_lim);
3788#ifdef USE_ZLIB
3789 info[INF_ZLIB_MEM_USAGE] = mkf_u32(0, zlib_used_memory);
3790 info[INF_MAX_ZLIB_MEM_USAGE] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxzlibmem);
3791#endif
3792 info[INF_TASKS] = mkf_u32(0, nb_tasks_cur);
Christopher Faulet34c5cc92016-12-06 09:15:30 +01003793 info[INF_RUN_QUEUE] = mkf_u32(0, tasks_run_queue_cur);
Willy Tarreau81036f22019-05-20 19:24:50 +02003794 info[INF_IDLE_PCT] = mkf_u32(FN_AVG, ti->idle_pct);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003795 info[INF_NODE] = mkf_str(FO_CONFIG|FN_OUTPUT|FS_SERVICE, global.node);
3796 if (global.desc)
3797 info[INF_DESCRIPTION] = mkf_str(FO_CONFIG|FN_OUTPUT|FS_SERVICE, global.desc);
Willy Tarreau00098ea2018-11-05 14:38:13 +01003798 info[INF_STOPPING] = mkf_u32(0, stopping);
3799 info[INF_JOBS] = mkf_u32(0, jobs);
William Lallemanda7199262018-11-16 16:57:20 +01003800 info[INF_UNSTOPPABLE_JOBS] = mkf_u32(0, unstoppable_jobs);
Willy Tarreau00098ea2018-11-05 14:38:13 +01003801 info[INF_LISTENERS] = mkf_u32(0, listeners);
Willy Tarreau199ad242018-11-05 16:31:22 +01003802 info[INF_ACTIVE_PEERS] = mkf_u32(0, active_peers);
Willy Tarreau2d372c22018-11-05 17:12:27 +01003803 info[INF_CONNECTED_PEERS] = mkf_u32(0, connected_peers);
Willy Tarreau13ef7732018-11-12 07:25:28 +01003804 info[INF_DROPPED_LOGS] = mkf_u32(0, dropped_logs);
Willy Tarreaubeb859a2018-11-22 18:07:59 +01003805 info[INF_BUSY_POLLING] = mkf_u32(0, !!(global.tune.options & GTUNE_BUSY_POLLING));
Baptiste Assmann333939c2019-01-21 08:34:50 +01003806 info[INF_FAILED_RESOLUTIONS] = mkf_u32(0, dns_failed_resolutions);
Willy Tarreau7cf0e452019-05-23 11:39:14 +02003807 info[INF_TOTAL_BYTES_OUT] = mkf_u64(0, global.out_bytes);
Christopher Fauletaaa70852020-07-10 13:56:30 +02003808 info[INF_TOTAL_SPLICED_BYTES_OUT] = mkf_u64(0, global.spliced_out_bytes);
Willy Tarreau7cf0e452019-05-23 11:39:14 +02003809 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 +02003810 info[INF_DEBUG_COMMANDS_ISSUED] = mkf_u32(0, debug_commands_issued);
Emeric Brun45c457a2020-07-09 23:23:34 +02003811 info[INF_CUM_LOG_MSGS] = mkf_u32(FN_COUNTER, cum_log_messages);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003812
3813 return 1;
3814}
3815
3816/* This function dumps information onto the stream interface's read buffer.
3817 * It returns 0 as long as it does not complete, non-zero upon completion.
3818 * No state is used.
3819 */
3820static int stats_dump_info_to_buffer(struct stream_interface *si)
3821{
3822 struct appctx *appctx = __objt_appctx(si->end);
3823
3824 if (!stats_fill_info(info, INF_TOTAL_FIELDS))
3825 return 0;
3826
3827 chunk_reset(&trash);
3828
3829 if (appctx->ctx.stats.flags & STAT_FMT_TYPED)
Willy Tarreau43241ff2019-10-09 11:27:51 +02003830 stats_dump_typed_info_fields(&trash, info, appctx->ctx.stats.flags);
Simon Horman05ee2132017-01-04 09:37:25 +01003831 else if (appctx->ctx.stats.flags & STAT_FMT_JSON)
Willy Tarreau43241ff2019-10-09 11:27:51 +02003832 stats_dump_json_info_fields(&trash, info, appctx->ctx.stats.flags);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003833 else
Willy Tarreau43241ff2019-10-09 11:27:51 +02003834 stats_dump_info_fields(&trash, info, appctx->ctx.stats.flags);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003835
Willy Tarreau06d80a92017-10-19 14:32:15 +02003836 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01003837 si_rx_room_blk(si);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003838 return 0;
3839 }
3840
3841 return 1;
3842}
3843
Simon Horman6f6bb382017-01-04 09:37:26 +01003844/* This function dumps the schema onto the stream interface's read buffer.
3845 * It returns 0 as long as it does not complete, non-zero upon completion.
3846 * No state is used.
3847 *
3848 * Integer values bouned to the range [-(2**53)+1, (2**53)-1] as
3849 * per the recommendation for interoperable integers in section 6 of RFC 7159.
3850 */
Willy Tarreau83061a82018-07-13 11:56:34 +02003851static void stats_dump_json_schema(struct buffer *out)
Simon Horman6f6bb382017-01-04 09:37:26 +01003852{
3853
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003854 int old_len = out->data;
Simon Horman6f6bb382017-01-04 09:37:26 +01003855
3856 chunk_strcat(out,
3857 "{"
3858 "\"$schema\":\"http://json-schema.org/draft-04/schema#\","
3859 "\"oneOf\":["
3860 "{"
3861 "\"title\":\"Info\","
3862 "\"type\":\"array\","
3863 "\"items\":{"
Amaury Denoyellea53ce4c2020-10-02 18:31:59 +02003864 "\"title\":\"InfoItem\","
3865 "\"type\":\"object\","
Simon Horman6f6bb382017-01-04 09:37:26 +01003866 "\"properties\":{"
Simon Horman6f6bb382017-01-04 09:37:26 +01003867 "\"field\":{\"$ref\":\"#/definitions/field\"},"
3868 "\"processNum\":{\"$ref\":\"#/definitions/processNum\"},"
3869 "\"tags\":{\"$ref\":\"#/definitions/tags\"},"
3870 "\"value\":{\"$ref\":\"#/definitions/typedValue\"}"
3871 "},"
3872 "\"required\":[\"field\",\"processNum\",\"tags\","
3873 "\"value\"]"
3874 "}"
3875 "},"
3876 "{"
3877 "\"title\":\"Stat\","
3878 "\"type\":\"array\","
3879 "\"items\":{"
3880 "\"title\":\"InfoItem\","
3881 "\"type\":\"object\","
3882 "\"properties\":{"
3883 "\"objType\":{"
3884 "\"enum\":[\"Frontend\",\"Backend\",\"Listener\","
3885 "\"Server\",\"Unknown\"]"
3886 "},"
3887 "\"proxyId\":{"
3888 "\"type\":\"integer\","
3889 "\"minimum\":0"
3890 "},"
3891 "\"id\":{"
3892 "\"type\":\"integer\","
3893 "\"minimum\":0"
3894 "},"
3895 "\"field\":{\"$ref\":\"#/definitions/field\"},"
3896 "\"processNum\":{\"$ref\":\"#/definitions/processNum\"},"
3897 "\"tags\":{\"$ref\":\"#/definitions/tags\"},"
3898 "\"typedValue\":{\"$ref\":\"#/definitions/typedValue\"}"
3899 "},"
3900 "\"required\":[\"objType\",\"proxyId\",\"id\","
3901 "\"field\",\"processNum\",\"tags\","
3902 "\"value\"]"
3903 "}"
3904 "},"
3905 "{"
3906 "\"title\":\"Error\","
3907 "\"type\":\"object\","
3908 "\"properties\":{"
3909 "\"errorStr\":{"
3910 "\"type\":\"string\""
Amaury Denoyellea53ce4c2020-10-02 18:31:59 +02003911 "}"
3912 "},"
3913 "\"required\":[\"errorStr\"]"
Simon Horman6f6bb382017-01-04 09:37:26 +01003914 "}"
3915 "],"
3916 "\"definitions\":{"
3917 "\"field\":{"
3918 "\"type\":\"object\","
3919 "\"pos\":{"
3920 "\"type\":\"integer\","
3921 "\"minimum\":0"
3922 "},"
3923 "\"name\":{"
3924 "\"type\":\"string\""
3925 "},"
3926 "\"required\":[\"pos\",\"name\"]"
3927 "},"
3928 "\"processNum\":{"
3929 "\"type\":\"integer\","
3930 "\"minimum\":1"
3931 "},"
3932 "\"tags\":{"
3933 "\"type\":\"object\","
3934 "\"origin\":{"
3935 "\"type\":\"string\","
3936 "\"enum\":[\"Metric\",\"Status\",\"Key\","
3937 "\"Config\",\"Product\",\"Unknown\"]"
3938 "},"
3939 "\"nature\":{"
3940 "\"type\":\"string\","
3941 "\"enum\":[\"Gauge\",\"Limit\",\"Min\",\"Max\","
3942 "\"Rate\",\"Counter\",\"Duration\","
3943 "\"Age\",\"Time\",\"Name\",\"Output\","
3944 "\"Avg\", \"Unknown\"]"
3945 "},"
3946 "\"scope\":{"
3947 "\"type\":\"string\","
3948 "\"enum\":[\"Cluster\",\"Process\",\"Service\","
3949 "\"System\",\"Unknown\"]"
3950 "},"
3951 "\"required\":[\"origin\",\"nature\",\"scope\"]"
3952 "},"
3953 "\"typedValue\":{"
3954 "\"type\":\"object\","
3955 "\"oneOf\":["
3956 "{\"$ref\":\"#/definitions/typedValue/definitions/s32Value\"},"
3957 "{\"$ref\":\"#/definitions/typedValue/definitions/s64Value\"},"
3958 "{\"$ref\":\"#/definitions/typedValue/definitions/u32Value\"},"
3959 "{\"$ref\":\"#/definitions/typedValue/definitions/u64Value\"},"
3960 "{\"$ref\":\"#/definitions/typedValue/definitions/strValue\"}"
3961 "],"
3962 "\"definitions\":{"
3963 "\"s32Value\":{"
3964 "\"properties\":{"
3965 "\"type\":{"
3966 "\"type\":\"string\","
3967 "\"enum\":[\"s32\"]"
3968 "},"
3969 "\"value\":{"
3970 "\"type\":\"integer\","
3971 "\"minimum\":-2147483648,"
3972 "\"maximum\":2147483647"
3973 "}"
3974 "},"
3975 "\"required\":[\"type\",\"value\"]"
3976 "},"
3977 "\"s64Value\":{"
3978 "\"properties\":{"
3979 "\"type\":{"
3980 "\"type\":\"string\","
3981 "\"enum\":[\"s64\"]"
3982 "},"
3983 "\"value\":{"
3984 "\"type\":\"integer\","
3985 "\"minimum\":-9007199254740991,"
3986 "\"maximum\":9007199254740991"
3987 "}"
3988 "},"
3989 "\"required\":[\"type\",\"value\"]"
3990 "},"
3991 "\"u32Value\":{"
3992 "\"properties\":{"
3993 "\"type\":{"
3994 "\"type\":\"string\","
3995 "\"enum\":[\"u32\"]"
3996 "},"
3997 "\"value\":{"
3998 "\"type\":\"integer\","
3999 "\"minimum\":0,"
4000 "\"maximum\":4294967295"
4001 "}"
4002 "},"
4003 "\"required\":[\"type\",\"value\"]"
4004 "},"
4005 "\"u64Value\":{"
4006 "\"properties\":{"
4007 "\"type\":{"
4008 "\"type\":\"string\","
4009 "\"enum\":[\"u64\"]"
4010 "},"
4011 "\"value\":{"
4012 "\"type\":\"integer\","
4013 "\"minimum\":0,"
4014 "\"maximum\":9007199254740991"
4015 "}"
4016 "},"
4017 "\"required\":[\"type\",\"value\"]"
4018 "},"
4019 "\"strValue\":{"
4020 "\"properties\":{"
4021 "\"type\":{"
4022 "\"type\":\"string\","
4023 "\"enum\":[\"str\"]"
4024 "},"
4025 "\"value\":{\"type\":\"string\"}"
4026 "},"
4027 "\"required\":[\"type\",\"value\"]"
4028 "},"
4029 "\"unknownValue\":{"
4030 "\"properties\":{"
4031 "\"type\":{"
4032 "\"type\":\"integer\","
4033 "\"minimum\":0"
4034 "},"
4035 "\"value\":{"
4036 "\"type\":\"string\","
4037 "\"enum\":[\"unknown\"]"
4038 "}"
4039 "},"
4040 "\"required\":[\"type\",\"value\"]"
4041 "}"
4042 "}"
4043 "}"
4044 "}"
4045 "}");
4046
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004047 if (old_len == out->data) {
Simon Horman6f6bb382017-01-04 09:37:26 +01004048 chunk_reset(out);
4049 chunk_appendf(out,
4050 "{\"errorStr\":\"output buffer too short\"}");
4051 }
4052}
4053
4054/* This function dumps the schema onto the stream interface's read buffer.
4055 * It returns 0 as long as it does not complete, non-zero upon completion.
4056 * No state is used.
4057 */
4058static int stats_dump_json_schema_to_buffer(struct stream_interface *si)
4059{
4060 chunk_reset(&trash);
4061
4062 stats_dump_json_schema(&trash);
4063
Willy Tarreau06d80a92017-10-19 14:32:15 +02004064 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01004065 si_rx_room_blk(si);
Simon Horman6f6bb382017-01-04 09:37:26 +01004066 return 0;
4067 }
4068
4069 return 1;
4070}
4071
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004072static int cli_parse_clear_counters(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau89d467c2016-11-23 11:02:40 +01004073{
4074 struct proxy *px;
4075 struct server *sv;
4076 struct listener *li;
Amaury Denoyelled3700a72020-10-05 11:49:43 +02004077 struct stats_module *mod;
Willy Tarreau89d467c2016-11-23 11:02:40 +01004078 int clrall = 0;
4079
4080 if (strcmp(args[2], "all") == 0)
4081 clrall = 1;
4082
4083 /* check permissions */
4084 if (!cli_has_level(appctx, ACCESS_LVL_OPER) ||
4085 (clrall && !cli_has_level(appctx, ACCESS_LVL_ADMIN)))
4086 return 1;
4087
Olivier Houchardfbc74e82017-11-24 16:54:05 +01004088 for (px = proxies_list; px; px = px->next) {
Willy Tarreau89d467c2016-11-23 11:02:40 +01004089 if (clrall) {
4090 memset(&px->be_counters, 0, sizeof(px->be_counters));
4091 memset(&px->fe_counters, 0, sizeof(px->fe_counters));
4092 }
4093 else {
4094 px->be_counters.conn_max = 0;
4095 px->be_counters.p.http.rps_max = 0;
4096 px->be_counters.sps_max = 0;
4097 px->be_counters.cps_max = 0;
4098 px->be_counters.nbpend_max = 0;
Christopher Fauletefb41f02019-11-08 14:53:15 +01004099 px->be_counters.qtime_max = 0;
4100 px->be_counters.ctime_max = 0;
4101 px->be_counters.dtime_max = 0;
4102 px->be_counters.ttime_max = 0;
Willy Tarreau89d467c2016-11-23 11:02:40 +01004103
4104 px->fe_counters.conn_max = 0;
4105 px->fe_counters.p.http.rps_max = 0;
4106 px->fe_counters.sps_max = 0;
4107 px->fe_counters.cps_max = 0;
Willy Tarreau89d467c2016-11-23 11:02:40 +01004108 }
4109
4110 for (sv = px->srv; sv; sv = sv->next)
4111 if (clrall)
4112 memset(&sv->counters, 0, sizeof(sv->counters));
4113 else {
4114 sv->counters.cur_sess_max = 0;
4115 sv->counters.nbpend_max = 0;
4116 sv->counters.sps_max = 0;
Christopher Fauletefb41f02019-11-08 14:53:15 +01004117 sv->counters.qtime_max = 0;
4118 sv->counters.ctime_max = 0;
4119 sv->counters.dtime_max = 0;
4120 sv->counters.ttime_max = 0;
Willy Tarreau89d467c2016-11-23 11:02:40 +01004121 }
4122
4123 list_for_each_entry(li, &px->conf.listeners, by_fe)
4124 if (li->counters) {
4125 if (clrall)
4126 memset(li->counters, 0, sizeof(*li->counters));
4127 else
4128 li->counters->conn_max = 0;
4129 }
4130 }
4131
4132 global.cps_max = 0;
4133 global.sps_max = 0;
Olivier Houchard00bc3cb2017-10-17 19:23:25 +02004134 global.ssl_max = 0;
4135 global.ssl_fe_keys_max = 0;
4136 global.ssl_be_keys_max = 0;
Willy Tarreaud80cb4e2018-01-20 19:30:13 +01004137
Amaury Denoyelled3700a72020-10-05 11:49:43 +02004138 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
4139 if (!mod->clearable && !clrall)
4140 continue;
4141
4142 for (px = proxies_list; px; px = px->next) {
4143 enum stats_domain_px_cap mod_cap = stats_px_get_cap(mod->domain_flags);
4144
4145 if (px->cap & PR_CAP_FE && mod_cap & STATS_PX_CAP_FE) {
4146 EXTRA_COUNTERS_INIT(px->extra_counters_fe,
4147 mod,
4148 mod->counters,
4149 mod->counters_size);
4150 }
4151
4152 if (px->cap & PR_CAP_BE && mod_cap & STATS_PX_CAP_BE) {
4153 EXTRA_COUNTERS_INIT(px->extra_counters_be,
4154 mod,
4155 mod->counters,
4156 mod->counters_size);
4157 }
4158
4159 if (mod_cap & STATS_PX_CAP_SRV) {
4160 for (sv = px->srv; sv; sv = sv->next) {
4161 EXTRA_COUNTERS_INIT(sv->extra_counters,
4162 mod,
4163 mod->counters,
4164 mod->counters_size);
4165 }
4166 }
4167
4168 if (mod_cap & STATS_PX_CAP_LI) {
4169 list_for_each_entry(li, &px->conf.listeners, by_fe) {
4170 EXTRA_COUNTERS_INIT(li->extra_counters,
4171 mod,
4172 mod->counters,
4173 mod->counters_size);
4174 }
4175 }
4176 }
4177 }
4178
Willy Tarreaud80cb4e2018-01-20 19:30:13 +01004179 memset(activity, 0, sizeof(activity));
Willy Tarreau89d467c2016-11-23 11:02:40 +01004180 return 1;
4181}
4182
4183
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004184static int cli_parse_show_info(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004185{
Willy Tarreau2f397382019-10-09 11:43:59 +02004186 int arg = 2;
4187
Willy Tarreaud25fc792016-12-16 12:33:47 +01004188 appctx->ctx.stats.scope_str = 0;
4189 appctx->ctx.stats.scope_len = 0;
4190 appctx->ctx.stats.flags = 0;
4191
Willy Tarreau2f397382019-10-09 11:43:59 +02004192 while (*args[arg]) {
4193 if (strcmp(args[arg], "typed") == 0)
4194 appctx->ctx.stats.flags = (appctx->ctx.stats.flags & ~STAT_FMT_MASK) | STAT_FMT_TYPED;
4195 else if (strcmp(args[arg], "json") == 0)
4196 appctx->ctx.stats.flags = (appctx->ctx.stats.flags & ~STAT_FMT_MASK) | STAT_FMT_JSON;
4197 else if (strcmp(args[arg], "desc") == 0)
4198 appctx->ctx.stats.flags |= STAT_SHOW_FDESC;
4199 arg++;
4200 }
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004201 return 0;
4202}
4203
4204
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004205static int cli_parse_show_stat(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau2b812e22016-11-22 16:18:05 +01004206{
Willy Tarreau2f397382019-10-09 11:43:59 +02004207 int arg = 2;
4208
Willy Tarreaud25fc792016-12-16 12:33:47 +01004209 appctx->ctx.stats.scope_str = 0;
4210 appctx->ctx.stats.scope_len = 0;
Willy Tarreau578d6e42019-10-09 11:00:22 +02004211 appctx->ctx.stats.flags = STAT_SHNODE | STAT_SHDESC;
4212
4213 if ((strm_li(si_strm(appctx->owner))->bind_conf->level & ACCESS_LVL_MASK) >= ACCESS_LVL_OPER)
4214 appctx->ctx.stats.flags |= STAT_SHLGNDS;
Willy Tarreaud25fc792016-12-16 12:33:47 +01004215
Amaury Denoyelle072f97e2020-10-05 11:49:37 +02004216 /* proxy is the default domain */
4217 appctx->ctx.stats.domain = STATS_DOMAIN_PROXY;
4218 if (!strcmp(args[arg], "domain")) {
4219 ++args;
4220
4221 if (!strcmp(args[arg], "proxy"))
4222 ++args;
4223 else
4224 return cli_err(appctx, "Invalid statistics domain.\n");
4225 }
4226
4227 if (appctx->ctx.stats.domain == STATS_DOMAIN_PROXY
4228 && *args[arg] && *args[arg+1] && *args[arg+2]) {
Willy Tarreaua1b1ed52016-11-25 08:50:58 +01004229 struct proxy *px;
4230
Willy Tarreau2f397382019-10-09 11:43:59 +02004231 px = proxy_find_by_name(args[arg], 0, 0);
Willy Tarreaua1b1ed52016-11-25 08:50:58 +01004232 if (px)
4233 appctx->ctx.stats.iid = px->uuid;
4234 else
Willy Tarreau2f397382019-10-09 11:43:59 +02004235 appctx->ctx.stats.iid = atoi(args[arg]);
Willy Tarreaua1b1ed52016-11-25 08:50:58 +01004236
Willy Tarreau9d008692019-08-09 11:21:01 +02004237 if (!appctx->ctx.stats.iid)
4238 return cli_err(appctx, "No such proxy.\n");
Willy Tarreaua1b1ed52016-11-25 08:50:58 +01004239
Willy Tarreau2b812e22016-11-22 16:18:05 +01004240 appctx->ctx.stats.flags |= STAT_BOUND;
Willy Tarreau2f397382019-10-09 11:43:59 +02004241 appctx->ctx.stats.type = atoi(args[arg+1]);
4242 appctx->ctx.stats.sid = atoi(args[arg+2]);
4243 arg += 3;
4244 }
4245
4246 while (*args[arg]) {
4247 if (strcmp(args[arg], "typed") == 0)
4248 appctx->ctx.stats.flags = (appctx->ctx.stats.flags & ~STAT_FMT_MASK) | STAT_FMT_TYPED;
4249 else if (strcmp(args[arg], "json") == 0)
4250 appctx->ctx.stats.flags = (appctx->ctx.stats.flags & ~STAT_FMT_MASK) | STAT_FMT_JSON;
4251 else if (strcmp(args[arg], "desc") == 0)
4252 appctx->ctx.stats.flags |= STAT_SHOW_FDESC;
4253 arg++;
Willy Tarreau2b812e22016-11-22 16:18:05 +01004254 }
Willy Tarreau2b812e22016-11-22 16:18:05 +01004255
Willy Tarreau2b812e22016-11-22 16:18:05 +01004256 return 0;
4257}
4258
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004259static int cli_io_handler_dump_info(struct appctx *appctx)
4260{
4261 return stats_dump_info_to_buffer(appctx->owner);
4262}
4263
Willy Tarreau2b812e22016-11-22 16:18:05 +01004264/* This I/O handler runs as an applet embedded in a stream interface. It is
4265 * used to send raw stats over a socket.
4266 */
4267static int cli_io_handler_dump_stat(struct appctx *appctx)
4268{
Christopher Fauletef779222018-10-31 08:47:01 +01004269 return stats_dump_stat_to_buffer(appctx->owner, NULL, NULL);
Willy Tarreau2b812e22016-11-22 16:18:05 +01004270}
4271
Simon Horman6f6bb382017-01-04 09:37:26 +01004272static int cli_io_handler_dump_json_schema(struct appctx *appctx)
4273{
4274 return stats_dump_json_schema_to_buffer(appctx->owner);
4275}
4276
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02004277static int stats_allocate_proxy_counters_internal(struct extra_counters **counters,
4278 int type, int px_cap)
4279{
4280 struct stats_module *mod;
4281
4282 EXTRA_COUNTERS_REGISTER(counters, type, alloc_failed);
4283
4284 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
4285 if (!(stats_px_get_cap(mod->domain_flags) & px_cap))
4286 continue;
4287
4288 EXTRA_COUNTERS_ADD(mod, *counters, mod->counters, mod->counters_size);
4289 }
4290
4291 EXTRA_COUNTERS_ALLOC(*counters, alloc_failed);
4292
4293 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
4294 if (!(stats_px_get_cap(mod->domain_flags) & px_cap))
4295 continue;
4296
4297 EXTRA_COUNTERS_INIT(*counters, mod, mod->counters, mod->counters_size);
4298 }
4299
4300 return 1;
4301
4302 alloc_failed:
4303 return 0;
4304}
4305
4306/* Initialize and allocate all extra counters for a proxy and its attached
4307 * servers/listeners with all already registered stats module
4308 */
4309int stats_allocate_proxy_counters(struct proxy *px)
4310{
4311 struct server *sv;
4312 struct listener *li;
4313
4314 if (px->cap & PR_CAP_FE) {
4315 if (!stats_allocate_proxy_counters_internal(&px->extra_counters_fe,
4316 COUNTERS_FE,
4317 STATS_PX_CAP_FE)) {
4318 return 0;
4319 }
4320 }
4321
4322 if (px->cap & PR_CAP_BE) {
4323 if (!stats_allocate_proxy_counters_internal(&px->extra_counters_be,
4324 COUNTERS_BE,
4325 STATS_PX_CAP_BE)) {
4326 return 0;
4327 }
4328 }
4329
4330 for (sv = px->srv; sv; sv = sv->next) {
4331 if (!stats_allocate_proxy_counters_internal(&sv->extra_counters,
4332 COUNTERS_SV,
4333 STATS_PX_CAP_SRV)) {
4334 return 0;
4335 }
4336 }
4337
4338 list_for_each_entry(li, &px->conf.listeners, by_fe) {
4339 if (!stats_allocate_proxy_counters_internal(&li->extra_counters,
4340 COUNTERS_LI,
4341 STATS_PX_CAP_LI)) {
4342 return 0;
4343 }
4344 }
4345
4346 return 1;
4347}
4348
Amaury Denoyelle58d395e2020-10-05 11:49:40 +02004349void stats_register_module(struct stats_module *m)
4350{
4351 const uint8_t domain = stats_get_domain(m->domain_flags);
4352
4353 LIST_ADDQ(&stats_module_list[domain], &m->list);
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02004354 stat_count[domain] += m->stats_count;
Amaury Denoyelle58d395e2020-10-05 11:49:40 +02004355}
4356
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02004357static int allocate_stats_px_postcheck(void)
4358{
4359 struct stats_module *mod;
4360 size_t i = ST_F_TOTAL_FIELDS;
4361 int err_code = 0;
4362
4363 stat_count[STATS_DOMAIN_PROXY] += ST_F_TOTAL_FIELDS;
4364
4365 stat_f[STATS_DOMAIN_PROXY] = malloc(stat_count[STATS_DOMAIN_PROXY] * sizeof(struct name_desc));
4366 if (!stat_f[STATS_DOMAIN_PROXY]) {
4367 ha_alert("stats: cannot allocate all fields for proxy statistics\n");
4368 err_code |= ERR_ALERT | ERR_FATAL;
4369 return err_code;
4370 }
4371
4372 memcpy(stat_f[STATS_DOMAIN_PROXY], stat_fields,
4373 ST_F_TOTAL_FIELDS * sizeof(struct name_desc));
4374
4375 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
4376 memcpy(stat_f[STATS_DOMAIN_PROXY] + i,
4377 mod->stats,
4378 mod->stats_count * sizeof(struct name_desc));
4379 i += mod->stats_count;
4380 }
4381
4382 for (struct proxy *px = proxies_list; px; px = px->next) {
4383 if (!stats_allocate_proxy_counters(px)) {
4384 ha_alert("stats: cannot allocate all counters for proxy statistics\n");
4385 err_code |= ERR_ALERT | ERR_FATAL;
4386 return err_code;
4387 }
4388 }
4389
4390 stat_l[STATS_DOMAIN_PROXY] = malloc(stat_count[STATS_DOMAIN_PROXY] * sizeof(struct field));
4391 if (!stat_l[STATS_DOMAIN_PROXY]) {
4392 ha_alert("stats: cannot allocate a line for proxy statistics\n");
4393 err_code |= ERR_ALERT | ERR_FATAL;
4394 return err_code;
4395 }
4396
4397 return err_code;
4398}
4399
4400REGISTER_CONFIG_POSTPARSER("allocate-stats-px", allocate_stats_px_postcheck);
4401
Willy Tarreau2b812e22016-11-22 16:18:05 +01004402/* register cli keywords */
4403static struct cli_kw_list cli_kws = {{ },{
Willy Tarreau89d467c2016-11-23 11:02:40 +01004404 { { "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 +02004405 { { "show", "info", NULL }, "show info : report information about the running process [desc|json|typed]*", cli_parse_show_info, cli_io_handler_dump_info, NULL },
4406 { { "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 +01004407 { { "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 +01004408 {{},}
4409}};
4410
Willy Tarreau0108d902018-11-25 19:14:37 +01004411INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
4412
William Lallemand74c24fb2016-11-21 17:18:36 +01004413struct applet http_stats_applet = {
4414 .obj_type = OBJ_TYPE_APPLET,
4415 .name = "<STATS>", /* used for logging */
4416 .fct = http_stats_io_handler,
4417 .release = NULL,
4418};
4419
William Lallemand74c24fb2016-11-21 17:18:36 +01004420/*
4421 * Local variables:
4422 * c-indent-level: 8
4423 * c-basic-offset: 8
4424 * End:
4425 */