blob: 38c4ff904d0d715a1db3810ba29d857acdd93e18 [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" },
Adis Nezirovicb62b78b2021-01-15 13:12:33 +0100151 [INF_BUILD_INFO] = { .name = "Build info", .desc = "Build info" },
Willy Tarreau0baac8c2016-11-22 16:36:53 +0100152};
153
Willy Tarreaueaa55372019-10-09 07:39:11 +0200154const struct name_desc stat_fields[ST_F_TOTAL_FIELDS] = {
Willy Tarreau6d4897e2019-10-11 16:31:46 +0200155 [ST_F_PXNAME] = { .name = "pxname", .desc = "Proxy name" },
156 [ST_F_SVNAME] = { .name = "svname", .desc = "Server name" },
157 [ST_F_QCUR] = { .name = "qcur", .desc = "Current number of connections waiting in the server of backend queue" },
158 [ST_F_QMAX] = { .name = "qmax", .desc = "Highest value of qcur encountered since process started" },
159 [ST_F_SCUR] = { .name = "scur", .desc = "Current number of sessions on the frontend, backend or server" },
160 [ST_F_SMAX] = { .name = "smax", .desc = "Highest value of scur encountered since process started" },
161 [ST_F_SLIM] = { .name = "slim", .desc = "Frontend/listener/server's maxconn, backend's fullconn" },
162 [ST_F_STOT] = { .name = "stot", .desc = "Total number of sessions since process started" },
163 [ST_F_BIN] = { .name = "bin", .desc = "Total number of request bytes since process started" },
164 [ST_F_BOUT] = { .name = "bout", .desc = "Total number of response bytes since process started" },
165 [ST_F_DREQ] = { .name = "dreq", .desc = "Total number of denied requests since process started" },
166 [ST_F_DRESP] = { .name = "dresp", .desc = "Total number of denied responses since process started" },
167 [ST_F_EREQ] = { .name = "ereq", .desc = "Total number of invalid requests since process started" },
168 [ST_F_ECON] = { .name = "econ", .desc = "Total number of failed connections to server since the worker process started" },
169 [ST_F_ERESP] = { .name = "eresp", .desc = "Total number of invalid responses since the worker process started" },
170 [ST_F_WRETR] = { .name = "wretr", .desc = "Total number of server connection retries since the worker process started" },
171 [ST_F_WREDIS] = { .name = "wredis", .desc = "Total number of server redispatches due to connection failures since the worker process started" },
172 [ST_F_STATUS] = { .name = "status", .desc = "Frontend/listen status: OPEN/WAITING/FULL/STOP; backend: UP/DOWN; server: last check status" },
Willy Tarreaubd715102020-10-23 22:44:30 +0200173 [ST_F_WEIGHT] = { .name = "weight", .desc = "Server's effective weight, or sum of active servers' effective weights for a backend" },
Willy Tarreau6d4897e2019-10-11 16:31:46 +0200174 [ST_F_ACT] = { .name = "act", .desc = "Total number of active UP servers with a non-zero weight" },
175 [ST_F_BCK] = { .name = "bck", .desc = "Total number of backup UP servers with a non-zero weight" },
176 [ST_F_CHKFAIL] = { .name = "chkfail", .desc = "Total number of failed individual health checks per server/backend, since the worker process started" },
177 [ST_F_CHKDOWN] = { .name = "chkdown", .desc = "Total number of failed checks causing UP to DOWN server transitions, per server/backend, since the worker process started" },
178 [ST_F_LASTCHG] = { .name = "lastchg", .desc = "How long ago the last server state changed, in seconds" },
179 [ST_F_DOWNTIME] = { .name = "downtime", .desc = "Total time spent in DOWN state, for server or backend" },
180 [ST_F_QLIMIT] = { .name = "qlimit", .desc = "Limit on the number of connections in queue, for servers only (maxqueue argument)" },
181 [ST_F_PID] = { .name = "pid", .desc = "Relative worker process number (1..nbproc)" },
182 [ST_F_IID] = { .name = "iid", .desc = "Frontend or Backend numeric identifier ('id' setting)" },
183 [ST_F_SID] = { .name = "sid", .desc = "Server numeric identifier ('id' setting)" },
184 [ST_F_THROTTLE] = { .name = "throttle", .desc = "Throttling ratio applied to a server's maxconn and weight during the slowstart period (0 to 100%)" },
185 [ST_F_LBTOT] = { .name = "lbtot", .desc = "Total number of requests routed by load balancing since the worker process started (ignores queue pop and stickiness)" },
186 [ST_F_TRACKED] = { .name = "tracked", .desc = "Name of the other server this server tracks for its state" },
187 [ST_F_TYPE] = { .name = "type", .desc = "Type of the object (Listener, Frontend, Backend, Server)" },
188 [ST_F_RATE] = { .name = "rate", .desc = "Total number of sessions processed by this object over the last second (sessions for listeners/frontends, requests for backends/servers)" },
189 [ST_F_RATE_LIM] = { .name = "rate_lim", .desc = "Limit on the number of sessions accepted in a second (frontend only, 'rate-limit sessions' setting)" },
190 [ST_F_RATE_MAX] = { .name = "rate_max", .desc = "Highest value of 'rate' observed since the worker process started" },
191 [ST_F_CHECK_STATUS] = { .name = "check_status", .desc = "Status report of the server's latest health check, prefixed with '*' if a check is currently in progress" },
192 [ST_F_CHECK_CODE] = { .name = "check_code", .desc = "HTTP/SMTP/LDAP status code reported by the latest server health check" },
193 [ST_F_CHECK_DURATION] = { .name = "check_duration", .desc = "Total duration of the latest server health check, in milliseconds" },
194 [ST_F_HRSP_1XX] = { .name = "hrsp_1xx", .desc = "Total number of HTTP responses with status 100-199 returned by this object since the worker process started" },
195 [ST_F_HRSP_2XX] = { .name = "hrsp_2xx", .desc = "Total number of HTTP responses with status 200-299 returned by this object since the worker process started" },
196 [ST_F_HRSP_3XX] = { .name = "hrsp_3xx", .desc = "Total number of HTTP responses with status 300-399 returned by this object since the worker process started" },
197 [ST_F_HRSP_4XX] = { .name = "hrsp_4xx", .desc = "Total number of HTTP responses with status 400-499 returned by this object since the worker process started" },
198 [ST_F_HRSP_5XX] = { .name = "hrsp_5xx", .desc = "Total number of HTTP responses with status 500-599 returned by this object since the worker process started" },
199 [ST_F_HRSP_OTHER] = { .name = "hrsp_other", .desc = "Total number of HTTP responses with status <100, >599 returned by this object since the worker process started (error -1 included)" },
200 [ST_F_HANAFAIL] = { .name = "hanafail", .desc = "Total number of failed checks caused by an 'on-error' directive after an 'observe' condition matched" },
201 [ST_F_REQ_RATE] = { .name = "req_rate", .desc = "Number of HTTP requests processed over the last second on this object" },
202 [ST_F_REQ_RATE_MAX] = { .name = "req_rate_max", .desc = "Highest value of 'req_rate' observed since the worker process started" },
203 [ST_F_REQ_TOT] = { .name = "req_tot", .desc = "Total number of HTTP requests processed by this object since the worker process started" },
204 [ST_F_CLI_ABRT] = { .name = "cli_abrt", .desc = "Total number of requests or connections aborted by the client since the worker process started" },
205 [ST_F_SRV_ABRT] = { .name = "srv_abrt", .desc = "Total number of requests or connections aborted by the server since the worker process started" },
206 [ST_F_COMP_IN] = { .name = "comp_in", .desc = "Total number of bytes submitted to the HTTP compressor for this object since the worker process started" },
207 [ST_F_COMP_OUT] = { .name = "comp_out", .desc = "Total number of bytes emitted by the HTTP compressor for this object since the worker process started" },
208 [ST_F_COMP_BYP] = { .name = "comp_byp", .desc = "Total number of bytes that bypassed HTTP compression for this object since the worker process started (CPU/memory/bandwidth limitation)" },
209 [ST_F_COMP_RSP] = { .name = "comp_rsp", .desc = "Total number of HTTP responses that were compressed for this object since the worker process started" },
210 [ST_F_LASTSESS] = { .name = "lastsess", .desc = "How long ago some traffic was seen on this object on this worker process, in seconds" },
211 [ST_F_LAST_CHK] = { .name = "last_chk", .desc = "Short description of the latest health check report for this server (see also check_desc)" },
212 [ST_F_LAST_AGT] = { .name = "last_agt", .desc = "Short description of the latest agent check report for this server (see also agent_desc)" },
213 [ST_F_QTIME] = { .name = "qtime", .desc = "Time spent in the queue, in milliseconds, averaged over the 1024 last requests (backend/server)" },
214 [ST_F_CTIME] = { .name = "ctime", .desc = "Time spent waiting for a connection to complete, in milliseconds, averaged over the 1024 last requests (backend/server)" },
215 [ST_F_RTIME] = { .name = "rtime", .desc = "Time spent waiting for a server response, in milliseconds, averaged over the 1024 last requests (backend/server)" },
216 [ST_F_TTIME] = { .name = "ttime", .desc = "Total request+response time (request+queue+connect+response+processing), in milliseconds, averaged over the 1024 last requests (backend/server)" },
217 [ST_F_AGENT_STATUS] = { .name = "agent_status", .desc = "Status report of the server's latest agent check, prefixed with '*' if a check is currently in progress" },
218 [ST_F_AGENT_CODE] = { .name = "agent_code", .desc = "Status code reported by the latest server agent check" },
219 [ST_F_AGENT_DURATION] = { .name = "agent_duration", .desc = "Total duration of the latest server agent check, in milliseconds" },
220 [ST_F_CHECK_DESC] = { .name = "check_desc", .desc = "Textual description of the latest health check report for this server" },
221 [ST_F_AGENT_DESC] = { .name = "agent_desc", .desc = "Textual description of the latest agent check report for this server" },
222 [ST_F_CHECK_RISE] = { .name = "check_rise", .desc = "Number of successful health checks before declaring a server UP (server 'rise' setting)" },
223 [ST_F_CHECK_FALL] = { .name = "check_fall", .desc = "Number of failed health checks before declaring a server DOWN (server 'fall' setting)" },
224 [ST_F_CHECK_HEALTH] = { .name = "check_health", .desc = "Current server health check level (0..fall-1=DOWN, fall..rise-1=UP)" },
225 [ST_F_AGENT_RISE] = { .name = "agent_rise", .desc = "Number of successful agent checks before declaring a server UP (server 'rise' setting)" },
226 [ST_F_AGENT_FALL] = { .name = "agent_fall", .desc = "Number of failed agent checks before declaring a server DOWN (server 'fall' setting)" },
227 [ST_F_AGENT_HEALTH] = { .name = "agent_health", .desc = "Current server agent check level (0..fall-1=DOWN, fall..rise-1=UP)" },
228 [ST_F_ADDR] = { .name = "addr", .desc = "Server's address:port, shown only if show-legends is set, or at levels oper/admin for the CLI" },
229 [ST_F_COOKIE] = { .name = "cookie", .desc = "Backend's cookie name or Server's cookie value, shown only if show-legends is set, or at levels oper/admin for the CLI" },
230 [ST_F_MODE] = { .name = "mode", .desc = "'mode' setting (tcp/http/health/cli)" },
231 [ST_F_ALGO] = { .name = "algo", .desc = "Backend's load balancing algorithm, shown only if show-legends is set, or at levels oper/admin for the CLI" },
232 [ST_F_CONN_RATE] = { .name = "conn_rate", .desc = "Number of new connections accepted over the last second on the frontend for this worker process" },
233 [ST_F_CONN_RATE_MAX] = { .name = "conn_rate_max", .desc = "Highest value of 'conn_rate' observed since the worker process started" },
234 [ST_F_CONN_TOT] = { .name = "conn_tot", .desc = "Total number of new connections accepted on this frontend since the worker process started" },
235 [ST_F_INTERCEPTED] = { .name = "intercepted", .desc = "Total number of HTTP requests intercepted on the frontend (redirects/stats/services) since the worker process started" },
236 [ST_F_DCON] = { .name = "dcon", .desc = "Total number of incoming connections blocked on a listener/frontend by a tcp-request connection rule since the worker process started" },
237 [ST_F_DSES] = { .name = "dses", .desc = "Total number of incoming sessions blocked on a listener/frontend by a tcp-request connection rule since the worker process started" },
238 [ST_F_WREW] = { .name = "wrew", .desc = "Total number of failed HTTP header rewrites since the worker process started" },
239 [ST_F_CONNECT] = { .name = "connect", .desc = "Total number of outgoing connection attempts on this backend/server since the worker process started" },
240 [ST_F_REUSE] = { .name = "reuse", .desc = "Total number of reused connection on this backend/server since the worker process started" },
241 [ST_F_CACHE_LOOKUPS] = { .name = "cache_lookups", .desc = "Total number of HTTP requests looked up in the cache on this frontend/backend since the worker process started" },
242 [ST_F_CACHE_HITS] = { .name = "cache_hits", .desc = "Total number of HTTP requests not found in the cache on this frontend/backend since the worker process started" },
243 [ST_F_SRV_ICUR] = { .name = "srv_icur", .desc = "Current number of idle connections available for reuse on this server" },
244 [ST_F_SRV_ILIM] = { .name = "src_ilim", .desc = "Limit on the number of available idle connections on this server (server 'pool_max_conn' directive)" },
Christopher Faulet0d1c2a62019-11-08 14:59:51 +0100245 [ST_F_QT_MAX] = { .name = "qtime_max", .desc = "Maximum observed time spent in the queue, in milliseconds (backend/server)" },
246 [ST_F_CT_MAX] = { .name = "ctime_max", .desc = "Maximum observed time spent waiting for a connection to complete, in milliseconds (backend/server)" },
247 [ST_F_RT_MAX] = { .name = "rtime_max", .desc = "Maximum observed time spent waiting for a server response, in milliseconds (backend/server)" },
248 [ST_F_TT_MAX] = { .name = "ttime_max", .desc = "Maximum observed total request+response time (request+queue+connect+response+processing), in milliseconds (backend/server)" },
Christopher Faulet0159ee42019-12-16 14:40:39 +0100249 [ST_F_EINT] = { .name = "eint", .desc = "Total number of internal errors since process started"},
Willy Tarreau3bb617c2020-06-29 13:51:05 +0200250 [ST_F_IDLE_CONN_CUR] = { .name = "idle_conn_cur", .desc = "Current number of unsafe idle connections"},
251 [ST_F_SAFE_CONN_CUR] = { .name = "safe_conn_cur", .desc = "Current number of safe idle connections"},
252 [ST_F_USED_CONN_CUR] = { .name = "used_conn_cur", .desc = "Current number of connections in use"},
Willy Tarreaua9fcecb2020-06-29 15:38:53 +0200253 [ST_F_NEED_CONN_EST] = { .name = "need_conn_est", .desc = "Estimated needed number of connections"},
Willy Tarreaubd715102020-10-23 22:44:30 +0200254 [ST_F_UWEIGHT] = { .name = "uweight", .desc = "Server's user weight, or sum of active servers' user weights for a backend" },
William Lallemand74c24fb2016-11-21 17:18:36 +0100255};
256
Willy Tarreau0baac8c2016-11-22 16:36:53 +0100257/* one line of info */
William Dauchy5d9b8f32021-01-11 20:07:49 +0100258THREAD_LOCAL struct field info[INF_TOTAL_FIELDS];
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +0200259
260/* description of statistics (static and dynamic) */
261static struct name_desc *stat_f[STATS_DOMAIN_COUNT];
262static size_t stat_count[STATS_DOMAIN_COUNT];
263
264/* one line for stats */
Christopher Fauletde79cd22021-01-06 07:41:56 +0100265static THREAD_LOCAL struct field *stat_l[STATS_DOMAIN_COUNT];
William Lallemand74c24fb2016-11-21 17:18:36 +0100266
Amaury Denoyelle58d395e2020-10-05 11:49:40 +0200267/* list of all registered stats module */
268static struct list stats_module_list[STATS_DOMAIN_COUNT] = {
269 LIST_HEAD_INIT(stats_module_list[STATS_DOMAIN_PROXY]),
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +0200270 LIST_HEAD_INIT(stats_module_list[STATS_DOMAIN_DNS]),
Amaury Denoyelle58d395e2020-10-05 11:49:40 +0200271};
272
Amaury Denoyelle7f8f6cb2020-11-10 14:24:31 +0100273THREAD_LOCAL void *trash_counters;
274
Amaury Denoyelle072f97e2020-10-05 11:49:37 +0200275static inline uint8_t stats_get_domain(uint32_t domain)
276{
277 return domain >> STATS_DOMAIN & STATS_DOMAIN_MASK;
278}
279
Amaury Denoyelle72b16e52020-10-05 11:49:38 +0200280static inline enum stats_domain_px_cap stats_px_get_cap(uint32_t domain)
281{
282 return domain >> STATS_PX_CAP & STATS_PX_CAP_MASK;
283}
284
Christopher Faulet6338a082019-09-09 15:50:54 +0200285static void stats_dump_json_schema(struct buffer *out);
William Lallemand74c24fb2016-11-21 17:18:36 +0100286
Amaury Denoyelle3ca927e2020-10-02 18:32:00 +0200287int stats_putchk(struct channel *chn, struct htx *htx, struct buffer *chk)
Christopher Fauletef779222018-10-31 08:47:01 +0100288{
289 if (htx) {
Christopher Faulet69fc88c2019-01-07 14:27:53 +0100290 if (chk->data >= channel_htx_recv_max(chn, htx))
291 return 0;
Willy Tarreau0a7ef022019-05-28 10:30:11 +0200292 if (!htx_add_data_atonce(htx, ist2(chk->area, chk->data)))
Christopher Fauletef779222018-10-31 08:47:01 +0100293 return 0;
Christopher Faulet5adbeeb2019-01-02 14:34:39 +0100294 channel_add_input(chn, chk->data);
Christopher Fauletef779222018-10-31 08:47:01 +0100295 chk->data = 0;
Christopher Fauletef779222018-10-31 08:47:01 +0100296 }
297 else {
298 if (ci_putchk(chn, chk) == -1)
299 return 0;
300 }
301 return 1;
302}
Willy Tarreau0baac8c2016-11-22 16:36:53 +0100303
Christopher Fauleted7a0662019-01-14 11:07:34 +0100304static const char *stats_scope_ptr(struct appctx *appctx, struct stream_interface *si)
305{
Christopher Fauletb7f88902019-07-15 21:56:43 +0200306 struct channel *req = si_oc(si);
307 struct htx *htx = htxbuf(&req->buf);
308 struct htx_blk *blk;
309 struct ist uri;
Christopher Fauleted7a0662019-01-14 11:07:34 +0100310
Christopher Fauletb7f88902019-07-15 21:56:43 +0200311 blk = htx_get_head_blk(htx);
Christopher Fauletea009732019-11-18 15:50:25 +0100312 BUG_ON(!blk || htx_get_blk_type(blk) != HTX_BLK_REQ_SL);
Christopher Fauletb7f88902019-07-15 21:56:43 +0200313 ALREADY_CHECKED(blk);
314 uri = htx_sl_req_uri(htx_get_blk_ptr(htx, blk));
315 return uri.ptr + appctx->ctx.stats.scope_str;
Christopher Fauleted7a0662019-01-14 11:07:34 +0100316}
317
William Lallemand74c24fb2016-11-21 17:18:36 +0100318/*
319 * http_stats_io_handler()
320 * -> stats_dump_stat_to_buffer() // same as above, but used for CSV or HTML
321 * -> stats_dump_csv_header() // emits the CSV headers (same as above)
Simon Horman05ee2132017-01-04 09:37:25 +0100322 * -> stats_dump_json_header() // emits the JSON headers (same as above)
William Lallemand74c24fb2016-11-21 17:18:36 +0100323 * -> stats_dump_html_head() // emits the HTML headers
324 * -> stats_dump_html_info() // emits the equivalent of "show info" at the top
325 * -> stats_dump_proxy_to_buffer() // same as above, valid for CSV and HTML
326 * -> stats_dump_html_px_hdr()
327 * -> stats_dump_fe_stats()
328 * -> stats_dump_li_stats()
329 * -> stats_dump_sv_stats()
330 * -> stats_dump_be_stats()
331 * -> stats_dump_html_px_end()
332 * -> stats_dump_html_end() // emits HTML trailer
Simon Horman05ee2132017-01-04 09:37:25 +0100333 * -> stats_dump_json_end() // emits JSON trailer
William Lallemand74c24fb2016-11-21 17:18:36 +0100334 */
335
336
William Lallemand74c24fb2016-11-21 17:18:36 +0100337/* Dumps the stats CSV header to the trash buffer which. The caller is responsible
338 * for clearing it if needed.
339 * NOTE: Some tools happen to rely on the field position instead of its name,
340 * so please only append new fields at the end, never in the middle.
341 */
Amaury Denoyelle50660a82020-10-05 11:49:39 +0200342static void stats_dump_csv_header(enum stats_domain domain)
William Lallemand74c24fb2016-11-21 17:18:36 +0100343{
344 int field;
345
346 chunk_appendf(&trash, "# ");
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +0200347 if (stat_f[domain]) {
348 for (field = 0; field < stat_count[domain]; ++field) {
349 chunk_appendf(&trash, "%s,", stat_f[domain][field].name);
William Lallemand74c24fb2016-11-21 17:18:36 +0100350
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +0200351 /* print special delimiter on proxy stats to mark end of
352 static fields */
353 if (domain == STATS_DOMAIN_PROXY && field + 1 == ST_F_TOTAL_FIELDS)
354 chunk_appendf(&trash, "-,");
Amaury Denoyelle50660a82020-10-05 11:49:39 +0200355 }
356 }
357
William Lallemand74c24fb2016-11-21 17:18:36 +0100358 chunk_appendf(&trash, "\n");
359}
360
William Lallemand74c24fb2016-11-21 17:18:36 +0100361/* Emits a stats field without any surrounding element and properly encoded to
362 * resist CSV output. Returns non-zero on success, 0 if the buffer is full.
363 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200364int stats_emit_raw_data_field(struct buffer *out, const struct field *f)
William Lallemand74c24fb2016-11-21 17:18:36 +0100365{
366 switch (field_format(f, 0)) {
367 case FF_EMPTY: return 1;
368 case FF_S32: return chunk_appendf(out, "%d", f->u.s32);
369 case FF_U32: return chunk_appendf(out, "%u", f->u.u32);
370 case FF_S64: return chunk_appendf(out, "%lld", (long long)f->u.s64);
371 case FF_U64: return chunk_appendf(out, "%llu", (unsigned long long)f->u.u64);
Christopher Faulet88a0db22019-09-24 16:35:10 +0200372 case FF_FLT: return chunk_appendf(out, "%f", f->u.flt);
William Lallemand74c24fb2016-11-21 17:18:36 +0100373 case FF_STR: return csv_enc_append(field_str(f, 0), 1, out) != NULL;
374 default: return chunk_appendf(out, "[INCORRECT_FIELD_TYPE_%08x]", f->type);
375 }
376}
377
Amaury Denoyellee3f576c2020-10-05 11:49:44 +0200378const char *field_to_html_str(const struct field *f)
379{
380 switch (field_format(f, 0)) {
381 case FF_S32: return U2H(f->u.s32);
382 case FF_S64: return U2H(f->u.s64);
383 case FF_U64: return U2H(f->u.u64);
384 case FF_U32: return U2H(f->u.u32);
385 case FF_STR: return field_str(f, 0);
386 case FF_EMPTY:
387 default:
388 return "";
389 }
390}
391
William Lallemand74c24fb2016-11-21 17:18:36 +0100392/* Emits a stats field prefixed with its type. No CSV encoding is prepared, the
393 * output is supposed to be used on its own line. Returns non-zero on success, 0
394 * if the buffer is full.
395 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200396int stats_emit_typed_data_field(struct buffer *out, const struct field *f)
William Lallemand74c24fb2016-11-21 17:18:36 +0100397{
398 switch (field_format(f, 0)) {
399 case FF_EMPTY: return 1;
400 case FF_S32: return chunk_appendf(out, "s32:%d", f->u.s32);
401 case FF_U32: return chunk_appendf(out, "u32:%u", f->u.u32);
402 case FF_S64: return chunk_appendf(out, "s64:%lld", (long long)f->u.s64);
403 case FF_U64: return chunk_appendf(out, "u64:%llu", (unsigned long long)f->u.u64);
Christopher Faulet88a0db22019-09-24 16:35:10 +0200404 case FF_FLT: return chunk_appendf(out, "flt:%f", f->u.flt);
William Lallemand74c24fb2016-11-21 17:18:36 +0100405 case FF_STR: return chunk_appendf(out, "str:%s", field_str(f, 0));
406 default: return chunk_appendf(out, "%08x:?", f->type);
407 }
408}
409
Simon Horman05ee2132017-01-04 09:37:25 +0100410/* Limit JSON integer values to the range [-(2**53)+1, (2**53)-1] as per
411 * the recommendation for interoperable integers in section 6 of RFC 7159.
412 */
413#define JSON_INT_MAX ((1ULL << 53) - 1)
414#define JSON_INT_MIN (0 - JSON_INT_MAX)
415
416/* Emits a stats field value and its type in JSON.
417 * Returns non-zero on success, 0 on error.
418 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200419int stats_emit_json_data_field(struct buffer *out, const struct field *f)
Simon Horman05ee2132017-01-04 09:37:25 +0100420{
421 int old_len;
422 char buf[20];
423 const char *type, *value = buf, *quote = "";
424
425 switch (field_format(f, 0)) {
426 case FF_EMPTY: return 1;
427 case FF_S32: type = "\"s32\"";
428 snprintf(buf, sizeof(buf), "%d", f->u.s32);
429 break;
430 case FF_U32: type = "\"u32\"";
431 snprintf(buf, sizeof(buf), "%u", f->u.u32);
432 break;
433 case FF_S64: type = "\"s64\"";
434 if (f->u.s64 < JSON_INT_MIN || f->u.s64 > JSON_INT_MAX)
435 return 0;
436 type = "\"s64\"";
437 snprintf(buf, sizeof(buf), "%lld", (long long)f->u.s64);
438 break;
439 case FF_U64: if (f->u.u64 > JSON_INT_MAX)
440 return 0;
441 type = "\"u64\"";
442 snprintf(buf, sizeof(buf), "%llu",
443 (unsigned long long) f->u.u64);
Christopher Faulet52c91bb2019-09-28 10:37:31 +0200444 break;
Christopher Faulet88a0db22019-09-24 16:35:10 +0200445 case FF_FLT: type = "\"flt\"";
446 snprintf(buf, sizeof(buf), "%f", f->u.flt);
Simon Horman05ee2132017-01-04 09:37:25 +0100447 break;
448 case FF_STR: type = "\"str\"";
449 value = field_str(f, 0);
450 quote = "\"";
451 break;
452 default: snprintf(buf, sizeof(buf), "%u", f->type);
453 type = buf;
454 value = "unknown";
455 quote = "\"";
456 break;
457 }
458
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200459 old_len = out->data;
Simon Horman05ee2132017-01-04 09:37:25 +0100460 chunk_appendf(out, ",\"value\":{\"type\":%s,\"value\":%s%s%s}",
461 type, quote, value, quote);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200462 return !(old_len == out->data);
Simon Horman05ee2132017-01-04 09:37:25 +0100463}
464
William Lallemand74c24fb2016-11-21 17:18:36 +0100465/* Emits an encoding of the field type on 3 characters followed by a delimiter.
466 * Returns non-zero on success, 0 if the buffer is full.
467 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200468int stats_emit_field_tags(struct buffer *out, const struct field *f,
469 char delim)
William Lallemand74c24fb2016-11-21 17:18:36 +0100470{
471 char origin, nature, scope;
472
473 switch (field_origin(f, 0)) {
474 case FO_METRIC: origin = 'M'; break;
475 case FO_STATUS: origin = 'S'; break;
476 case FO_KEY: origin = 'K'; break;
477 case FO_CONFIG: origin = 'C'; break;
478 case FO_PRODUCT: origin = 'P'; break;
479 default: origin = '?'; break;
480 }
481
482 switch (field_nature(f, 0)) {
483 case FN_GAUGE: nature = 'G'; break;
484 case FN_LIMIT: nature = 'L'; break;
485 case FN_MIN: nature = 'm'; break;
486 case FN_MAX: nature = 'M'; break;
487 case FN_RATE: nature = 'R'; break;
488 case FN_COUNTER: nature = 'C'; break;
489 case FN_DURATION: nature = 'D'; break;
490 case FN_AGE: nature = 'A'; break;
491 case FN_TIME: nature = 'T'; break;
492 case FN_NAME: nature = 'N'; break;
493 case FN_OUTPUT: nature = 'O'; break;
494 case FN_AVG: nature = 'a'; break;
495 default: nature = '?'; break;
496 }
497
498 switch (field_scope(f, 0)) {
499 case FS_PROCESS: scope = 'P'; break;
500 case FS_SERVICE: scope = 'S'; break;
501 case FS_SYSTEM: scope = 's'; break;
502 case FS_CLUSTER: scope = 'C'; break;
503 default: scope = '?'; break;
504 }
505
506 return chunk_appendf(out, "%c%c%c%c", origin, nature, scope, delim);
507}
508
Simon Horman05ee2132017-01-04 09:37:25 +0100509/* Emits an encoding of the field type as JSON.
510 * Returns non-zero on success, 0 if the buffer is full.
511 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200512int stats_emit_json_field_tags(struct buffer *out, const struct field *f)
Simon Horman05ee2132017-01-04 09:37:25 +0100513{
514 const char *origin, *nature, *scope;
515 int old_len;
516
517 switch (field_origin(f, 0)) {
518 case FO_METRIC: origin = "Metric"; break;
519 case FO_STATUS: origin = "Status"; break;
520 case FO_KEY: origin = "Key"; break;
521 case FO_CONFIG: origin = "Config"; break;
522 case FO_PRODUCT: origin = "Product"; break;
523 default: origin = "Unknown"; break;
524 }
525
526 switch (field_nature(f, 0)) {
527 case FN_GAUGE: nature = "Gauge"; break;
528 case FN_LIMIT: nature = "Limit"; break;
529 case FN_MIN: nature = "Min"; break;
530 case FN_MAX: nature = "Max"; break;
531 case FN_RATE: nature = "Rate"; break;
532 case FN_COUNTER: nature = "Counter"; break;
533 case FN_DURATION: nature = "Duration"; break;
534 case FN_AGE: nature = "Age"; break;
535 case FN_TIME: nature = "Time"; break;
536 case FN_NAME: nature = "Name"; break;
537 case FN_OUTPUT: nature = "Output"; break;
538 case FN_AVG: nature = "Avg"; break;
539 default: nature = "Unknown"; break;
540 }
541
542 switch (field_scope(f, 0)) {
543 case FS_PROCESS: scope = "Process"; break;
544 case FS_SERVICE: scope = "Service"; break;
545 case FS_SYSTEM: scope = "System"; break;
546 case FS_CLUSTER: scope = "Cluster"; break;
547 default: scope = "Unknown"; break;
548 }
549
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200550 old_len = out->data;
Simon Horman05ee2132017-01-04 09:37:25 +0100551 chunk_appendf(out, "\"tags\":{"
552 "\"origin\":\"%s\","
553 "\"nature\":\"%s\","
554 "\"scope\":\"%s\""
555 "}", origin, nature, scope);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200556 return !(old_len == out->data);
Simon Horman05ee2132017-01-04 09:37:25 +0100557}
William Lallemand74c24fb2016-11-21 17:18:36 +0100558
559/* Dump all fields from <stats> into <out> using CSV format */
Willy Tarreau83061a82018-07-13 11:56:34 +0200560static int stats_dump_fields_csv(struct buffer *out,
Amaury Denoyelle97323c92020-10-02 18:32:01 +0200561 const struct field *stats, size_t stats_count,
Amaury Denoyelle50660a82020-10-05 11:49:39 +0200562 unsigned int flags,
563 enum stats_domain domain)
William Lallemand74c24fb2016-11-21 17:18:36 +0100564{
565 int field;
566
Amaury Denoyelle50660a82020-10-05 11:49:39 +0200567 for (field = 0; field < stats_count; ++field) {
William Lallemand74c24fb2016-11-21 17:18:36 +0100568 if (!stats_emit_raw_data_field(out, &stats[field]))
569 return 0;
570 if (!chunk_strcat(out, ","))
571 return 0;
Amaury Denoyelle50660a82020-10-05 11:49:39 +0200572
573 /* print special delimiter on proxy stats to mark end of
574 static fields */
575 if (domain == STATS_DOMAIN_PROXY && field + 1 == ST_F_TOTAL_FIELDS) {
576 if (!chunk_strcat(out, "-,"))
577 return 0;
578 }
William Lallemand74c24fb2016-11-21 17:18:36 +0100579 }
Amaury Denoyelle50660a82020-10-05 11:49:39 +0200580
William Lallemand74c24fb2016-11-21 17:18:36 +0100581 chunk_strcat(out, "\n");
582 return 1;
583}
584
585/* Dump all fields from <stats> into <out> using a typed "field:desc:type:value" format */
Willy Tarreau83061a82018-07-13 11:56:34 +0200586static int stats_dump_fields_typed(struct buffer *out,
Amaury Denoyelle97323c92020-10-02 18:32:01 +0200587 const struct field *stats,
588 size_t stats_count,
Amaury Denoyelle072f97e2020-10-05 11:49:37 +0200589 unsigned int flags,
590 enum stats_domain domain)
William Lallemand74c24fb2016-11-21 17:18:36 +0100591{
592 int field;
593
Amaury Denoyelle97323c92020-10-02 18:32:01 +0200594 for (field = 0; field < stats_count; ++field) {
William Lallemand74c24fb2016-11-21 17:18:36 +0100595 if (!stats[field].type)
596 continue;
597
Amaury Denoyelle072f97e2020-10-05 11:49:37 +0200598 switch (domain) {
599 case STATS_DOMAIN_PROXY:
600 chunk_appendf(out, "%c.%u.%u.%d.%s.%u:",
601 stats[ST_F_TYPE].u.u32 == STATS_TYPE_FE ? 'F' :
602 stats[ST_F_TYPE].u.u32 == STATS_TYPE_BE ? 'B' :
603 stats[ST_F_TYPE].u.u32 == STATS_TYPE_SO ? 'L' :
604 stats[ST_F_TYPE].u.u32 == STATS_TYPE_SV ? 'S' :
605 '?',
606 stats[ST_F_IID].u.u32, stats[ST_F_SID].u.u32,
607 field,
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +0200608 stat_f[domain][field].name,
Amaury Denoyelle072f97e2020-10-05 11:49:37 +0200609 stats[ST_F_PID].u.u32);
610 break;
611
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +0200612 case STATS_DOMAIN_DNS:
613 chunk_appendf(out, "D.%d.%s:", field,
614 stat_f[domain][field].name);
615 break;
616
Amaury Denoyelle072f97e2020-10-05 11:49:37 +0200617 default:
618 break;
619 }
William Lallemand74c24fb2016-11-21 17:18:36 +0100620
621 if (!stats_emit_field_tags(out, &stats[field], ':'))
622 return 0;
623 if (!stats_emit_typed_data_field(out, &stats[field]))
624 return 0;
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +0200625
626 if (flags & STAT_SHOW_FDESC
627 && !chunk_appendf(out, ":\"%s\"", stat_f[domain][field].name)) {
Willy Tarreau6b19b142019-10-09 15:44:21 +0200628 return 0;
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +0200629 }
630
William Lallemand74c24fb2016-11-21 17:18:36 +0100631 if (!chunk_strcat(out, "\n"))
632 return 0;
633 }
634 return 1;
635}
636
Simon Horman05ee2132017-01-04 09:37:25 +0100637/* Dump all fields from <stats> into <out> using the "show info json" format */
Willy Tarreau83061a82018-07-13 11:56:34 +0200638static int stats_dump_json_info_fields(struct buffer *out,
Willy Tarreau43241ff2019-10-09 11:27:51 +0200639 const struct field *info, unsigned int flags)
Simon Horman05ee2132017-01-04 09:37:25 +0100640{
641 int field;
642 int started = 0;
643
644 if (!chunk_strcat(out, "["))
645 return 0;
646
647 for (field = 0; field < INF_TOTAL_FIELDS; field++) {
648 int old_len;
649
650 if (!field_format(info, field))
651 continue;
652
653 if (started && !chunk_strcat(out, ","))
654 goto err;
655 started = 1;
656
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200657 old_len = out->data;
Simon Horman05ee2132017-01-04 09:37:25 +0100658 chunk_appendf(out,
659 "{\"field\":{\"pos\":%d,\"name\":\"%s\"},"
660 "\"processNum\":%u,",
Willy Tarreaueaa55372019-10-09 07:39:11 +0200661 field, info_fields[field].name,
Simon Horman05ee2132017-01-04 09:37:25 +0100662 info[INF_PROCESS_NUM].u.u32);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200663 if (old_len == out->data)
Simon Horman05ee2132017-01-04 09:37:25 +0100664 goto err;
665
666 if (!stats_emit_json_field_tags(out, &info[field]))
667 goto err;
668
669 if (!stats_emit_json_data_field(out, &info[field]))
670 goto err;
671
672 if (!chunk_strcat(out, "}"))
673 goto err;
674 }
675
676 if (!chunk_strcat(out, "]"))
677 goto err;
678 return 1;
679
680err:
681 chunk_reset(out);
682 chunk_appendf(out, "{\"errorStr\":\"output buffer too short\"}");
683 return 0;
684}
685
Amaury Denoyellef34017b2020-10-02 18:32:03 +0200686static void stats_print_proxy_field_json(struct buffer *out,
687 const struct field *stat,
688 const char *name,
689 int pos,
690 uint32_t field_type,
691 uint32_t iid,
692 uint32_t sid,
693 uint32_t pid)
694{
695 const char *obj_type;
696 switch (field_type) {
697 case STATS_TYPE_FE: obj_type = "Frontend"; break;
698 case STATS_TYPE_BE: obj_type = "Backend"; break;
699 case STATS_TYPE_SO: obj_type = "Listener"; break;
700 case STATS_TYPE_SV: obj_type = "Server"; break;
701 default: obj_type = "Unknown"; break;
702 }
703
704 chunk_appendf(out,
705 "{"
706 "\"objType\":\"%s\","
707 "\"proxyId\":%u,"
708 "\"id\":%u,"
709 "\"field\":{\"pos\":%d,\"name\":\"%s\"},"
710 "\"processNum\":%u,",
711 obj_type, iid, sid, pos, name, pid);
712}
713
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +0200714static void stats_print_dns_field_json(struct buffer *out,
715 const struct field *stat,
716 const char *name,
717 int pos)
718{
719 chunk_appendf(out,
720 "{"
721 "\"field\":{\"pos\":%d,\"name\":\"%s\"},",
722 pos, name);
723}
724
725
Simon Horman05ee2132017-01-04 09:37:25 +0100726/* Dump all fields from <stats> into <out> using a typed "field:desc:type:value" format */
Willy Tarreau83061a82018-07-13 11:56:34 +0200727static int stats_dump_fields_json(struct buffer *out,
Amaury Denoyelle97323c92020-10-02 18:32:01 +0200728 const struct field *stats, size_t stats_count,
Amaury Denoyelle072f97e2020-10-05 11:49:37 +0200729 unsigned int flags,
730 enum stats_domain domain)
Simon Horman05ee2132017-01-04 09:37:25 +0100731{
732 int field;
733 int started = 0;
734
Willy Tarreaub0ce3ad2019-10-09 11:19:29 +0200735 if ((flags & STAT_STARTED) && !chunk_strcat(out, ","))
Simon Horman05ee2132017-01-04 09:37:25 +0100736 return 0;
737 if (!chunk_strcat(out, "["))
738 return 0;
739
Amaury Denoyelle97323c92020-10-02 18:32:01 +0200740 for (field = 0; field < stats_count; field++) {
Simon Horman05ee2132017-01-04 09:37:25 +0100741 int old_len;
742
743 if (!stats[field].type)
744 continue;
745
746 if (started && !chunk_strcat(out, ","))
747 goto err;
748 started = 1;
749
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200750 old_len = out->data;
Amaury Denoyelle072f97e2020-10-05 11:49:37 +0200751 if (domain == STATS_DOMAIN_PROXY) {
752 stats_print_proxy_field_json(out, &stats[field],
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +0200753 stat_f[domain][field].name,
754 field,
Amaury Denoyelle072f97e2020-10-05 11:49:37 +0200755 stats[ST_F_TYPE].u.u32,
756 stats[ST_F_IID].u.u32,
757 stats[ST_F_SID].u.u32,
758 stats[ST_F_PID].u.u32);
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +0200759 } else if (domain == STATS_DOMAIN_DNS) {
760 stats_print_dns_field_json(out, &stats[field],
761 stat_f[domain][field].name,
762 field);
Amaury Denoyelle072f97e2020-10-05 11:49:37 +0200763 }
Amaury Denoyellef34017b2020-10-02 18:32:03 +0200764
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200765 if (old_len == out->data)
Simon Horman05ee2132017-01-04 09:37:25 +0100766 goto err;
767
768 if (!stats_emit_json_field_tags(out, &stats[field]))
769 goto err;
770
771 if (!stats_emit_json_data_field(out, &stats[field]))
772 goto err;
773
774 if (!chunk_strcat(out, "}"))
775 goto err;
776 }
777
778 if (!chunk_strcat(out, "]"))
779 goto err;
780
781 return 1;
782
783err:
784 chunk_reset(out);
Willy Tarreaub0ce3ad2019-10-09 11:19:29 +0200785 if (flags & STAT_STARTED)
786 chunk_strcat(out, ",");
Simon Horman05ee2132017-01-04 09:37:25 +0100787 chunk_appendf(out, "{\"errorStr\":\"output buffer too short\"}");
788 return 0;
789}
790
William Lallemand74c24fb2016-11-21 17:18:36 +0100791/* Dump all fields from <stats> into <out> using the HTML format. A column is
Willy Tarreauab02b3f2019-10-09 11:11:46 +0200792 * reserved for the checkbox is STAT_ADMIN is set in <flags>. Some extra info
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +0200793 * are provided if STAT_SHLGNDS is present in <flags>. The statistics from
794 * extra modules are displayed at the end of the lines if STAT_SHMODULES is
795 * present in <flags>.
William Lallemand74c24fb2016-11-21 17:18:36 +0100796 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200797static int stats_dump_fields_html(struct buffer *out,
798 const struct field *stats,
799 unsigned int flags)
William Lallemand74c24fb2016-11-21 17:18:36 +0100800{
Willy Tarreau83061a82018-07-13 11:56:34 +0200801 struct buffer src;
Amaury Denoyellee3f576c2020-10-05 11:49:44 +0200802 struct stats_module *mod;
803 int i = 0, j = 0;
William Lallemand74c24fb2016-11-21 17:18:36 +0100804
805 if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_FE) {
806 chunk_appendf(out,
807 /* name, queue */
808 "<tr class=\"frontend\">");
809
Willy Tarreauab02b3f2019-10-09 11:11:46 +0200810 if (flags & STAT_ADMIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +0100811 /* Column sub-heading for Enable or Disable server */
812 chunk_appendf(out, "<td></td>");
813 }
814
815 chunk_appendf(out,
816 "<td class=ac>"
817 "<a name=\"%s/Frontend\"></a>"
818 "<a class=lfsb href=\"#%s/Frontend\">Frontend</a></td>"
819 "<td colspan=3></td>"
820 "",
821 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_PXNAME));
822
823 chunk_appendf(out,
824 /* sessions rate : current */
825 "<td><u>%s<div class=tips><table class=det>"
826 "<tr><th>Current connection rate:</th><td>%s/s</td></tr>"
827 "<tr><th>Current session rate:</th><td>%s/s</td></tr>"
828 "",
829 U2H(stats[ST_F_RATE].u.u32),
830 U2H(stats[ST_F_CONN_RATE].u.u32),
831 U2H(stats[ST_F_RATE].u.u32));
832
833 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
834 chunk_appendf(out,
835 "<tr><th>Current request rate:</th><td>%s/s</td></tr>",
836 U2H(stats[ST_F_REQ_RATE].u.u32));
837
838 chunk_appendf(out,
839 "</table></div></u></td>"
840 /* sessions rate : max */
841 "<td><u>%s<div class=tips><table class=det>"
842 "<tr><th>Max connection rate:</th><td>%s/s</td></tr>"
843 "<tr><th>Max session rate:</th><td>%s/s</td></tr>"
844 "",
845 U2H(stats[ST_F_RATE_MAX].u.u32),
846 U2H(stats[ST_F_CONN_RATE_MAX].u.u32),
847 U2H(stats[ST_F_RATE_MAX].u.u32));
848
849 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
850 chunk_appendf(out,
851 "<tr><th>Max request rate:</th><td>%s/s</td></tr>",
852 U2H(stats[ST_F_REQ_RATE_MAX].u.u32));
853
854 chunk_appendf(out,
855 "</table></div></u></td>"
856 /* sessions rate : limit */
857 "<td>%s</td>",
858 LIM2A(stats[ST_F_RATE_LIM].u.u32, "-"));
859
860 chunk_appendf(out,
861 /* sessions: current, max, limit, total */
862 "<td>%s</td><td>%s</td><td>%s</td>"
863 "<td><u>%s<div class=tips><table class=det>"
864 "<tr><th>Cum. connections:</th><td>%s</td></tr>"
865 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
866 "",
867 U2H(stats[ST_F_SCUR].u.u32), U2H(stats[ST_F_SMAX].u.u32), U2H(stats[ST_F_SLIM].u.u32),
868 U2H(stats[ST_F_STOT].u.u64),
869 U2H(stats[ST_F_CONN_TOT].u.u64),
870 U2H(stats[ST_F_STOT].u.u64));
871
872 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
873 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0) {
874 chunk_appendf(out,
875 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
876 "<tr><th>- HTTP 1xx responses:</th><td>%s</td></tr>"
877 "<tr><th>- HTTP 2xx responses:</th><td>%s</td></tr>"
878 "<tr><th>&nbsp;&nbsp;Compressed 2xx:</th><td>%s</td><td>(%d%%)</td></tr>"
879 "<tr><th>- HTTP 3xx responses:</th><td>%s</td></tr>"
880 "<tr><th>- HTTP 4xx responses:</th><td>%s</td></tr>"
881 "<tr><th>- HTTP 5xx responses:</th><td>%s</td></tr>"
882 "<tr><th>- other responses:</th><td>%s</td></tr>"
883 "<tr><th>Intercepted requests:</th><td>%s</td></tr>"
Willy Tarreaua1214a52018-12-14 14:00:25 +0100884 "<tr><th>Cache lookups:</th><td>%s</td></tr>"
885 "<tr><th>Cache hits:</th><td>%s</td><td>(%d%%)</td></tr>"
Willy Tarreau1b0f85e2018-05-28 15:12:40 +0200886 "<tr><th>Failed hdr rewrites:</th><td>%s</td></tr>"
Christopher Faulet0159ee42019-12-16 14:40:39 +0100887 "<tr><th>Internal errors:</th><td>%s</td></tr>"
William Lallemand74c24fb2016-11-21 17:18:36 +0100888 "",
889 U2H(stats[ST_F_REQ_TOT].u.u64),
890 U2H(stats[ST_F_HRSP_1XX].u.u64),
891 U2H(stats[ST_F_HRSP_2XX].u.u64),
892 U2H(stats[ST_F_COMP_RSP].u.u64),
893 stats[ST_F_HRSP_2XX].u.u64 ?
894 (int)(100 * stats[ST_F_COMP_RSP].u.u64 / stats[ST_F_HRSP_2XX].u.u64) : 0,
895 U2H(stats[ST_F_HRSP_3XX].u.u64),
896 U2H(stats[ST_F_HRSP_4XX].u.u64),
897 U2H(stats[ST_F_HRSP_5XX].u.u64),
898 U2H(stats[ST_F_HRSP_OTHER].u.u64),
Willy Tarreau1b0f85e2018-05-28 15:12:40 +0200899 U2H(stats[ST_F_INTERCEPTED].u.u64),
Willy Tarreaua1214a52018-12-14 14:00:25 +0100900 U2H(stats[ST_F_CACHE_LOOKUPS].u.u64),
901 U2H(stats[ST_F_CACHE_HITS].u.u64),
902 stats[ST_F_CACHE_LOOKUPS].u.u64 ?
903 (int)(100 * stats[ST_F_CACHE_HITS].u.u64 / stats[ST_F_CACHE_LOOKUPS].u.u64) : 0,
Christopher Faulet0159ee42019-12-16 14:40:39 +0100904 U2H(stats[ST_F_WREW].u.u64),
905 U2H(stats[ST_F_EINT].u.u64));
William Lallemand74c24fb2016-11-21 17:18:36 +0100906 }
907
908 chunk_appendf(out,
909 "</table></div></u></td>"
910 /* sessions: lbtot, lastsess */
911 "<td></td><td></td>"
912 /* bytes : in */
913 "<td>%s</td>"
914 "",
915 U2H(stats[ST_F_BIN].u.u64));
916
917 chunk_appendf(out,
918 /* bytes:out + compression stats (via hover): comp_in, comp_out, comp_byp */
919 "<td>%s%s<div class=tips><table class=det>"
920 "<tr><th>Response bytes in:</th><td>%s</td></tr>"
921 "<tr><th>Compression in:</th><td>%s</td></tr>"
922 "<tr><th>Compression out:</th><td>%s</td><td>(%d%%)</td></tr>"
923 "<tr><th>Compression bypass:</th><td>%s</td></tr>"
924 "<tr><th>Total bytes saved:</th><td>%s</td><td>(%d%%)</td></tr>"
925 "</table></div>%s</td>",
926 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "<u>":"",
927 U2H(stats[ST_F_BOUT].u.u64),
928 U2H(stats[ST_F_BOUT].u.u64),
929 U2H(stats[ST_F_COMP_IN].u.u64),
930 U2H(stats[ST_F_COMP_OUT].u.u64),
931 stats[ST_F_COMP_IN].u.u64 ? (int)(stats[ST_F_COMP_OUT].u.u64 * 100 / stats[ST_F_COMP_IN].u.u64) : 0,
932 U2H(stats[ST_F_COMP_BYP].u.u64),
933 U2H(stats[ST_F_COMP_IN].u.u64 - stats[ST_F_COMP_OUT].u.u64),
934 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,
935 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "</u>":"");
936
937 chunk_appendf(out,
938 /* denied: req, resp */
939 "<td>%s</td><td>%s</td>"
940 /* errors : request, connect, response */
941 "<td>%s</td><td></td><td></td>"
942 /* warnings: retries, redispatches */
943 "<td></td><td></td>"
944 /* server status : reflect frontend status */
945 "<td class=ac>%s</td>"
946 /* rest of server: nothing */
Amaury Denoyellee3f576c2020-10-05 11:49:44 +0200947 "<td class=ac colspan=8></td>"
William Lallemand74c24fb2016-11-21 17:18:36 +0100948 "",
949 U2H(stats[ST_F_DREQ].u.u64), U2H(stats[ST_F_DRESP].u.u64),
950 U2H(stats[ST_F_EREQ].u.u64),
951 field_str(stats, ST_F_STATUS));
Amaury Denoyellee3f576c2020-10-05 11:49:44 +0200952
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +0200953 if (flags & STAT_SHMODULES) {
954 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
Amaury Denoyellee6ee8202020-11-03 15:04:46 +0100955 chunk_appendf(out, "<td>");
956
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +0200957 if (stats_px_get_cap(mod->domain_flags) & STATS_PX_CAP_FE) {
Amaury Denoyellee6ee8202020-11-03 15:04:46 +0100958 chunk_appendf(out,
959 "<u>%s<div class=tips><table class=det>",
960 mod->name);
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +0200961 for (j = 0; j < mod->stats_count; ++j) {
962 chunk_appendf(out,
963 "<tr><th>%s</th><td>%s</td></tr>",
964 mod->stats[j].desc, field_to_html_str(&stats[ST_F_TOTAL_FIELDS + i]));
965 ++i;
966 }
Amaury Denoyellee6ee8202020-11-03 15:04:46 +0100967 chunk_appendf(out, "</table></div></u>");
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +0200968 } else {
969 i += mod->stats_count;
Amaury Denoyellee3f576c2020-10-05 11:49:44 +0200970 }
Amaury Denoyellee3f576c2020-10-05 11:49:44 +0200971
Amaury Denoyellee6ee8202020-11-03 15:04:46 +0100972 chunk_appendf(out, "</td>");
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +0200973 }
Amaury Denoyellee3f576c2020-10-05 11:49:44 +0200974 }
975
976 chunk_appendf(out, "</tr>");
William Lallemand74c24fb2016-11-21 17:18:36 +0100977 }
978 else if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_SO) {
979 chunk_appendf(out, "<tr class=socket>");
Willy Tarreauab02b3f2019-10-09 11:11:46 +0200980 if (flags & STAT_ADMIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +0100981 /* Column sub-heading for Enable or Disable server */
982 chunk_appendf(out, "<td></td>");
983 }
984
985 chunk_appendf(out,
986 /* frontend name, listener name */
987 "<td class=ac><a name=\"%s/+%s\"></a>%s"
988 "<a class=lfsb href=\"#%s/+%s\">%s</a>"
989 "",
990 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME),
Willy Tarreau708c4162019-10-09 10:19:16 +0200991 (flags & STAT_SHLGNDS)?"<u>":"",
William Lallemand74c24fb2016-11-21 17:18:36 +0100992 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME), field_str(stats, ST_F_SVNAME));
993
Willy Tarreau708c4162019-10-09 10:19:16 +0200994 if (flags & STAT_SHLGNDS) {
William Lallemand74c24fb2016-11-21 17:18:36 +0100995 chunk_appendf(out, "<div class=tips>");
996
Willy Tarreau90807112020-02-25 08:16:33 +0100997 if (isdigit((unsigned char)*field_str(stats, ST_F_ADDR)))
William Lallemand74c24fb2016-11-21 17:18:36 +0100998 chunk_appendf(out, "IPv4: %s, ", field_str(stats, ST_F_ADDR));
999 else if (*field_str(stats, ST_F_ADDR) == '[')
1000 chunk_appendf(out, "IPv6: %s, ", field_str(stats, ST_F_ADDR));
1001 else if (*field_str(stats, ST_F_ADDR))
1002 chunk_appendf(out, "%s, ", field_str(stats, ST_F_ADDR));
1003
1004 /* id */
1005 chunk_appendf(out, "id: %d</div>", stats[ST_F_SID].u.u32);
1006 }
1007
1008 chunk_appendf(out,
1009 /* queue */
1010 "%s</td><td colspan=3></td>"
1011 /* sessions rate: current, max, limit */
1012 "<td colspan=3>&nbsp;</td>"
1013 /* sessions: current, max, limit, total, lbtot, lastsess */
1014 "<td>%s</td><td>%s</td><td>%s</td>"
1015 "<td>%s</td><td>&nbsp;</td><td>&nbsp;</td>"
1016 /* bytes: in, out */
1017 "<td>%s</td><td>%s</td>"
1018 "",
Willy Tarreau708c4162019-10-09 10:19:16 +02001019 (flags & STAT_SHLGNDS)?"</u>":"",
William Lallemand74c24fb2016-11-21 17:18:36 +01001020 U2H(stats[ST_F_SCUR].u.u32), U2H(stats[ST_F_SMAX].u.u32), U2H(stats[ST_F_SLIM].u.u32),
1021 U2H(stats[ST_F_STOT].u.u64), U2H(stats[ST_F_BIN].u.u64), U2H(stats[ST_F_BOUT].u.u64));
1022
1023 chunk_appendf(out,
1024 /* denied: req, resp */
1025 "<td>%s</td><td>%s</td>"
1026 /* errors: request, connect, response */
1027 "<td>%s</td><td></td><td></td>"
1028 /* warnings: retries, redispatches */
1029 "<td></td><td></td>"
1030 /* server status: reflect listener status */
1031 "<td class=ac>%s</td>"
1032 /* rest of server: nothing */
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001033 "<td class=ac colspan=8></td>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001034 "",
1035 U2H(stats[ST_F_DREQ].u.u64), U2H(stats[ST_F_DRESP].u.u64),
1036 U2H(stats[ST_F_EREQ].u.u64),
1037 field_str(stats, ST_F_STATUS));
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001038
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001039 if (flags & STAT_SHMODULES) {
1040 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001041 chunk_appendf(out, "<td>");
1042
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001043 if (stats_px_get_cap(mod->domain_flags) & STATS_PX_CAP_LI) {
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001044 chunk_appendf(out,
1045 "<u>%s<div class=tips><table class=det>",
1046 mod->name);
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001047 for (j = 0; j < mod->stats_count; ++j) {
1048 chunk_appendf(out,
1049 "<tr><th>%s</th><td>%s</td></tr>",
1050 mod->stats[j].desc, field_to_html_str(&stats[ST_F_TOTAL_FIELDS + i]));
1051 ++i;
1052 }
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001053 chunk_appendf(out, "</table></div></u>");
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001054 } else {
1055 i += mod->stats_count;
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001056 }
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001057
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001058 chunk_appendf(out, "</td>");
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001059 }
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001060 }
1061
1062 chunk_appendf(out, "</tr>");
William Lallemand74c24fb2016-11-21 17:18:36 +01001063 }
1064 else if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_SV) {
1065 const char *style;
1066
1067 /* determine the style to use depending on the server's state,
1068 * its health and weight. There isn't a 1-to-1 mapping between
1069 * state and styles for the cases where the server is (still)
1070 * up. The reason is that we don't want to report nolb and
1071 * drain with the same color.
1072 */
1073
1074 if (strcmp(field_str(stats, ST_F_STATUS), "DOWN") == 0 ||
1075 strcmp(field_str(stats, ST_F_STATUS), "DOWN (agent)") == 0) {
1076 style = "down";
1077 }
1078 else if (strcmp(field_str(stats, ST_F_STATUS), "DOWN ") == 0) {
1079 style = "going_up";
1080 }
Daniel Corbettb4285172020-03-28 12:35:50 -04001081 else if (strcmp(field_str(stats, ST_F_STATUS), "DRAIN") == 0) {
1082 style = "draining";
1083 }
William Lallemand74c24fb2016-11-21 17:18:36 +01001084 else if (strcmp(field_str(stats, ST_F_STATUS), "NOLB ") == 0) {
1085 style = "going_down";
1086 }
1087 else if (strcmp(field_str(stats, ST_F_STATUS), "NOLB") == 0) {
1088 style = "nolb";
1089 }
1090 else if (strcmp(field_str(stats, ST_F_STATUS), "no check") == 0) {
1091 style = "no_check";
1092 }
1093 else if (!stats[ST_F_CHKFAIL].type ||
1094 stats[ST_F_CHECK_HEALTH].u.u32 == stats[ST_F_CHECK_RISE].u.u32 + stats[ST_F_CHECK_FALL].u.u32 - 1) {
1095 /* no check or max health = UP */
1096 if (stats[ST_F_WEIGHT].u.u32)
1097 style = "up";
1098 else
1099 style = "draining";
1100 }
1101 else {
1102 style = "going_down";
1103 }
1104
Willy Tarreau7b524852020-08-11 10:26:36 +02001105 if (strncmp(field_str(stats, ST_F_STATUS), "MAINT", 5) == 0)
William Lallemand74c24fb2016-11-21 17:18:36 +01001106 chunk_appendf(out, "<tr class=\"maintain\">");
1107 else
1108 chunk_appendf(out,
1109 "<tr class=\"%s_%s\">",
1110 (stats[ST_F_BCK].u.u32) ? "backup" : "active", style);
1111
1112
Willy Tarreauab02b3f2019-10-09 11:11:46 +02001113 if (flags & STAT_ADMIN)
William Lallemand74c24fb2016-11-21 17:18:36 +01001114 chunk_appendf(out,
David Harrigand3db35a2016-12-30 12:12:49 +00001115 "<td><input class='%s-checkbox' type=\"checkbox\" name=\"s\" value=\"%s\"></td>",
1116 field_str(stats, ST_F_PXNAME),
William Lallemand74c24fb2016-11-21 17:18:36 +01001117 field_str(stats, ST_F_SVNAME));
1118
1119 chunk_appendf(out,
1120 "<td class=ac><a name=\"%s/%s\"></a>%s"
1121 "<a class=lfsb href=\"#%s/%s\">%s</a>"
1122 "",
1123 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME),
Willy Tarreau708c4162019-10-09 10:19:16 +02001124 (flags & STAT_SHLGNDS) ? "<u>" : "",
William Lallemand74c24fb2016-11-21 17:18:36 +01001125 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME), field_str(stats, ST_F_SVNAME));
1126
Willy Tarreau708c4162019-10-09 10:19:16 +02001127 if (flags & STAT_SHLGNDS) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001128 chunk_appendf(out, "<div class=tips>");
1129
Willy Tarreau90807112020-02-25 08:16:33 +01001130 if (isdigit((unsigned char)*field_str(stats, ST_F_ADDR)))
William Lallemand74c24fb2016-11-21 17:18:36 +01001131 chunk_appendf(out, "IPv4: %s, ", field_str(stats, ST_F_ADDR));
1132 else if (*field_str(stats, ST_F_ADDR) == '[')
1133 chunk_appendf(out, "IPv6: %s, ", field_str(stats, ST_F_ADDR));
1134 else if (*field_str(stats, ST_F_ADDR))
1135 chunk_appendf(out, "%s, ", field_str(stats, ST_F_ADDR));
1136
1137 /* id */
1138 chunk_appendf(out, "id: %d", stats[ST_F_SID].u.u32);
1139
1140 /* cookie */
1141 if (stats[ST_F_COOKIE].type) {
1142 chunk_appendf(out, ", cookie: '");
1143 chunk_initstr(&src, field_str(stats, ST_F_COOKIE));
1144 chunk_htmlencode(out, &src);
1145 chunk_appendf(out, "'");
1146 }
1147
1148 chunk_appendf(out, "</div>");
1149 }
1150
1151 chunk_appendf(out,
1152 /* queue : current, max, limit */
1153 "%s</td><td>%s</td><td>%s</td><td>%s</td>"
1154 /* sessions rate : current, max, limit */
1155 "<td>%s</td><td>%s</td><td></td>"
1156 "",
Willy Tarreau708c4162019-10-09 10:19:16 +02001157 (flags & STAT_SHLGNDS) ? "</u>" : "",
William Lallemand74c24fb2016-11-21 17:18:36 +01001158 U2H(stats[ST_F_QCUR].u.u32), U2H(stats[ST_F_QMAX].u.u32), LIM2A(stats[ST_F_QLIMIT].u.u32, "-"),
1159 U2H(stats[ST_F_RATE].u.u32), U2H(stats[ST_F_RATE_MAX].u.u32));
1160
1161 chunk_appendf(out,
1162 /* sessions: current, max, limit, total */
Willy Tarreauf21d17b2019-09-08 09:24:56 +02001163 "<td><u>%s<div class=tips>"
1164 "<table class=det>"
1165 "<tr><th>Current active connections:</th><td>%s</td></tr>"
Willy Tarreau3bb617c2020-06-29 13:51:05 +02001166 "<tr><th>Current used connections:</th><td>%s</td></tr>"
Willy Tarreauf21d17b2019-09-08 09:24:56 +02001167 "<tr><th>Current idle connections:</th><td>%s</td></tr>"
Willy Tarreau3bb617c2020-06-29 13:51:05 +02001168 "<tr><th>- unsafe:</th><td>%s</td></tr>"
1169 "<tr><th>- safe:</th><td>%s</td></tr>"
Willy Tarreaua9fcecb2020-06-29 15:38:53 +02001170 "<tr><th>Estimated need of connections:</th><td>%s</td></tr>"
Willy Tarreauf21d17b2019-09-08 09:24:56 +02001171 "<tr><th>Active connections limit:</th><td>%s</td></tr>"
1172 "<tr><th>Idle connections limit:</th><td>%s</td></tr>"
1173 "</table></div></u>"
1174 "</td><td>%s</td><td>%s</td>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001175 "<td><u>%s<div class=tips><table class=det>"
1176 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
1177 "",
Willy Tarreauf21d17b2019-09-08 09:24:56 +02001178 U2H(stats[ST_F_SCUR].u.u32),
Willy Tarreau3bb617c2020-06-29 13:51:05 +02001179 U2H(stats[ST_F_SCUR].u.u32),
1180 U2H(stats[ST_F_USED_CONN_CUR].u.u32),
1181 U2H(stats[ST_F_SRV_ICUR].u.u32),
1182 U2H(stats[ST_F_IDLE_CONN_CUR].u.u32),
1183 U2H(stats[ST_F_SAFE_CONN_CUR].u.u32),
Willy Tarreaua9fcecb2020-06-29 15:38:53 +02001184 U2H(stats[ST_F_NEED_CONN_EST].u.u32),
Willy Tarreau3bb617c2020-06-29 13:51:05 +02001185
Willy Tarreauf21d17b2019-09-08 09:24:56 +02001186 LIM2A(stats[ST_F_SLIM].u.u32, "-"),
1187 stats[ST_F_SRV_ILIM].type ? U2H(stats[ST_F_SRV_ILIM].u.u32) : "-",
1188 U2H(stats[ST_F_SMAX].u.u32), LIM2A(stats[ST_F_SLIM].u.u32, "-"),
William Lallemand74c24fb2016-11-21 17:18:36 +01001189 U2H(stats[ST_F_STOT].u.u64),
1190 U2H(stats[ST_F_STOT].u.u64));
1191
1192 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
1193 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001194 chunk_appendf(out,
Willy Tarreauf1573842018-12-14 11:35:36 +01001195 "<tr><th>New connections:</th><td>%s</td></tr>"
1196 "<tr><th>Reused connections:</th><td>%s</td><td>(%d%%)</td></tr>"
Marcin Deranek3c27dda2020-05-15 18:32:51 +02001197 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001198 "<tr><th>- HTTP 1xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
1199 "<tr><th>- HTTP 2xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
1200 "<tr><th>- HTTP 3xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
1201 "<tr><th>- HTTP 4xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
1202 "<tr><th>- HTTP 5xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
1203 "<tr><th>- other responses:</th><td>%s</td><td>(%d%%)</td></tr>"
Willy Tarreau1b0f85e2018-05-28 15:12:40 +02001204 "<tr><th>Failed hdr rewrites:</th><td>%s</td></tr>"
Christopher Faulet0159ee42019-12-16 14:40:39 +01001205 "<tr><th>Internal error:</th><td>%s</td></tr>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001206 "",
Willy Tarreauf1573842018-12-14 11:35:36 +01001207 U2H(stats[ST_F_CONNECT].u.u64),
1208 U2H(stats[ST_F_REUSE].u.u64),
1209 (stats[ST_F_CONNECT].u.u64 + stats[ST_F_REUSE].u.u64) ?
1210 (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 +02001211 U2H(stats[ST_F_REQ_TOT].u.u64),
1212 U2H(stats[ST_F_HRSP_1XX].u.u64), stats[ST_F_REQ_TOT].u.u64 ?
1213 (int)(100 * stats[ST_F_HRSP_1XX].u.u64 / stats[ST_F_REQ_TOT].u.u64) : 0,
1214 U2H(stats[ST_F_HRSP_2XX].u.u64), stats[ST_F_REQ_TOT].u.u64 ?
1215 (int)(100 * stats[ST_F_HRSP_2XX].u.u64 / stats[ST_F_REQ_TOT].u.u64) : 0,
1216 U2H(stats[ST_F_HRSP_3XX].u.u64), stats[ST_F_REQ_TOT].u.u64 ?
1217 (int)(100 * stats[ST_F_HRSP_3XX].u.u64 / stats[ST_F_REQ_TOT].u.u64) : 0,
1218 U2H(stats[ST_F_HRSP_4XX].u.u64), stats[ST_F_REQ_TOT].u.u64 ?
1219 (int)(100 * stats[ST_F_HRSP_4XX].u.u64 / stats[ST_F_REQ_TOT].u.u64) : 0,
1220 U2H(stats[ST_F_HRSP_5XX].u.u64), stats[ST_F_REQ_TOT].u.u64 ?
1221 (int)(100 * stats[ST_F_HRSP_5XX].u.u64 / stats[ST_F_REQ_TOT].u.u64) : 0,
1222 U2H(stats[ST_F_HRSP_OTHER].u.u64), stats[ST_F_REQ_TOT].u.u64 ?
1223 (int)(100 * stats[ST_F_HRSP_OTHER].u.u64 / stats[ST_F_REQ_TOT].u.u64) : 0,
Christopher Faulet0159ee42019-12-16 14:40:39 +01001224 U2H(stats[ST_F_WREW].u.u64),
1225 U2H(stats[ST_F_EINT].u.u64));
William Lallemand74c24fb2016-11-21 17:18:36 +01001226 }
1227
Christopher Faulet0d1c2a62019-11-08 14:59:51 +01001228 chunk_appendf(out, "<tr><th colspan=3>Max / Avg over last 1024 success. conn.</th></tr>");
1229 chunk_appendf(out, "<tr><th>- Queue time:</th><td>%s / %s</td><td>ms</td></tr>",
1230 U2H(stats[ST_F_QT_MAX].u.u32), U2H(stats[ST_F_QTIME].u.u32));
1231 chunk_appendf(out, "<tr><th>- Connect time:</th><td>%s / %s</td><td>ms</td></tr>",
1232 U2H(stats[ST_F_CT_MAX].u.u32), U2H(stats[ST_F_CTIME].u.u32));
William Lallemand74c24fb2016-11-21 17:18:36 +01001233 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
Christopher Faulet0d1c2a62019-11-08 14:59:51 +01001234 chunk_appendf(out, "<tr><th>- Responses time:</th><td>%s / %s</td><td>ms</td></tr>",
1235 U2H(stats[ST_F_RT_MAX].u.u32), U2H(stats[ST_F_RTIME].u.u32));
1236 chunk_appendf(out, "<tr><th>- Total time:</th><td>%s / %s</td><td>ms</td></tr>",
1237 U2H(stats[ST_F_TT_MAX].u.u32), U2H(stats[ST_F_TTIME].u.u32));
William Lallemand74c24fb2016-11-21 17:18:36 +01001238
1239 chunk_appendf(out,
1240 "</table></div></u></td>"
1241 /* sessions: lbtot, last */
1242 "<td>%s</td><td>%s</td>",
1243 U2H(stats[ST_F_LBTOT].u.u64),
1244 human_time(stats[ST_F_LASTSESS].u.s32, 1));
1245
1246 chunk_appendf(out,
1247 /* bytes : in, out */
1248 "<td>%s</td><td>%s</td>"
1249 /* denied: req, resp */
1250 "<td></td><td>%s</td>"
1251 /* errors : request, connect */
1252 "<td></td><td>%s</td>"
1253 /* errors : response */
1254 "<td><u>%s<div class=tips>Connection resets during transfers: %lld client, %lld server</div></u></td>"
1255 /* warnings: retries, redispatches */
1256 "<td>%lld</td><td>%lld</td>"
1257 "",
1258 U2H(stats[ST_F_BIN].u.u64), U2H(stats[ST_F_BOUT].u.u64),
1259 U2H(stats[ST_F_DRESP].u.u64),
1260 U2H(stats[ST_F_ECON].u.u64),
1261 U2H(stats[ST_F_ERESP].u.u64),
1262 (long long)stats[ST_F_CLI_ABRT].u.u64,
1263 (long long)stats[ST_F_SRV_ABRT].u.u64,
1264 (long long)stats[ST_F_WRETR].u.u64,
1265 (long long)stats[ST_F_WREDIS].u.u64);
1266
1267 /* status, last change */
1268 chunk_appendf(out, "<td class=ac>");
1269
1270 /* FIXME!!!!
1271 * LASTCHG should contain the last change for *this* server and must be computed
1272 * properly above, as was done below, ie: this server if maint, otherwise ref server
1273 * if tracking. Note that ref is either local or remote depending on tracking.
1274 */
1275
1276
Willy Tarreau7b524852020-08-11 10:26:36 +02001277 if (strncmp(field_str(stats, ST_F_STATUS), "MAINT", 5) == 0) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001278 chunk_appendf(out, "%s MAINT", human_time(stats[ST_F_LASTCHG].u.u32, 1));
1279 }
Willy Tarreau7b524852020-08-11 10:26:36 +02001280 else if (strcmp(field_str(stats, ST_F_STATUS), "no check") == 0) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001281 chunk_strcat(out, "<i>no check</i>");
1282 }
1283 else {
1284 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 +02001285 if (strncmp(field_str(stats, ST_F_STATUS), "DOWN", 4) == 0) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001286 if (stats[ST_F_CHECK_HEALTH].u.u32)
1287 chunk_strcat(out, " &uarr;");
1288 }
1289 else if (stats[ST_F_CHECK_HEALTH].u.u32 < stats[ST_F_CHECK_RISE].u.u32 + stats[ST_F_CHECK_FALL].u.u32 - 1)
1290 chunk_strcat(out, " &darr;");
1291 }
1292
Willy Tarreau7b524852020-08-11 10:26:36 +02001293 if (strncmp(field_str(stats, ST_F_STATUS), "DOWN", 4) == 0 &&
William Lallemand74c24fb2016-11-21 17:18:36 +01001294 stats[ST_F_AGENT_STATUS].type && !stats[ST_F_AGENT_HEALTH].u.u32) {
1295 chunk_appendf(out,
1296 "</td><td class=ac><u> %s",
1297 field_str(stats, ST_F_AGENT_STATUS));
1298
1299 if (stats[ST_F_AGENT_CODE].type)
1300 chunk_appendf(out, "/%d", stats[ST_F_AGENT_CODE].u.u32);
1301
1302 if (stats[ST_F_AGENT_DURATION].type)
1303 chunk_appendf(out, " in %lums", (long)stats[ST_F_AGENT_DURATION].u.u64);
1304
1305 chunk_appendf(out, "<div class=tips>%s", field_str(stats, ST_F_AGENT_DESC));
1306
1307 if (*field_str(stats, ST_F_LAST_AGT)) {
1308 chunk_appendf(out, ": ");
1309 chunk_initstr(&src, field_str(stats, ST_F_LAST_AGT));
1310 chunk_htmlencode(out, &src);
1311 }
1312 chunk_appendf(out, "</div></u>");
1313 }
1314 else if (stats[ST_F_CHECK_STATUS].type) {
1315 chunk_appendf(out,
1316 "</td><td class=ac><u> %s",
1317 field_str(stats, ST_F_CHECK_STATUS));
1318
1319 if (stats[ST_F_CHECK_CODE].type)
1320 chunk_appendf(out, "/%d", stats[ST_F_CHECK_CODE].u.u32);
1321
1322 if (stats[ST_F_CHECK_DURATION].type)
1323 chunk_appendf(out, " in %lums", (long)stats[ST_F_CHECK_DURATION].u.u64);
1324
1325 chunk_appendf(out, "<div class=tips>%s", field_str(stats, ST_F_CHECK_DESC));
1326
1327 if (*field_str(stats, ST_F_LAST_CHK)) {
1328 chunk_appendf(out, ": ");
1329 chunk_initstr(&src, field_str(stats, ST_F_LAST_CHK));
1330 chunk_htmlencode(out, &src);
1331 }
1332 chunk_appendf(out, "</div></u>");
1333 }
1334 else
1335 chunk_appendf(out, "</td><td>");
1336
1337 chunk_appendf(out,
Willy Tarreaubd715102020-10-23 22:44:30 +02001338 /* weight / uweight */
1339 "</td><td class=ac>%d/%d</td>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001340 /* act, bck */
1341 "<td class=ac>%s</td><td class=ac>%s</td>"
1342 "",
Willy Tarreaubd715102020-10-23 22:44:30 +02001343 stats[ST_F_WEIGHT].u.u32, stats[ST_F_UWEIGHT].u.u32,
William Lallemand74c24fb2016-11-21 17:18:36 +01001344 stats[ST_F_BCK].u.u32 ? "-" : "Y",
1345 stats[ST_F_BCK].u.u32 ? "Y" : "-");
1346
1347 /* check failures: unique, fatal, down time */
1348 if (strcmp(field_str(stats, ST_F_STATUS), "MAINT (resolution)") == 0) {
1349 chunk_appendf(out, "<td class=ac colspan=3>resolution</td>");
1350 }
1351 else if (stats[ST_F_CHKFAIL].type) {
1352 chunk_appendf(out, "<td><u>%lld", (long long)stats[ST_F_CHKFAIL].u.u64);
1353
1354 if (stats[ST_F_HANAFAIL].type)
1355 chunk_appendf(out, "/%lld", (long long)stats[ST_F_HANAFAIL].u.u64);
1356
1357 chunk_appendf(out,
1358 "<div class=tips>Failed Health Checks%s</div></u></td>"
1359 "<td>%lld</td><td>%s</td>"
1360 "",
1361 stats[ST_F_HANAFAIL].type ? "/Health Analyses" : "",
1362 (long long)stats[ST_F_CHKDOWN].u.u64, human_time(stats[ST_F_DOWNTIME].u.u32, 1));
1363 }
1364 else if (strcmp(field_str(stats, ST_F_STATUS), "MAINT") != 0 && field_format(stats, ST_F_TRACKED) == FF_STR) {
1365 /* tracking a server (hence inherited maint would appear as "MAINT (via...)" */
1366 chunk_appendf(out,
1367 "<td class=ac colspan=3><a class=lfsb href=\"#%s\">via %s</a></td>",
1368 field_str(stats, ST_F_TRACKED), field_str(stats, ST_F_TRACKED));
1369 }
1370 else
1371 chunk_appendf(out, "<td colspan=3></td>");
1372
1373 /* throttle */
1374 if (stats[ST_F_THROTTLE].type)
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001375 chunk_appendf(out, "<td class=ac>%d %%</td>\n", stats[ST_F_THROTTLE].u.u32);
William Lallemand74c24fb2016-11-21 17:18:36 +01001376 else
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001377 chunk_appendf(out, "<td class=ac>-</td>");
1378
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001379 if (flags & STAT_SHMODULES) {
1380 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001381 chunk_appendf(out, "<td>");
1382
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001383 if (stats_px_get_cap(mod->domain_flags) & STATS_PX_CAP_SRV) {
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001384 chunk_appendf(out,
1385 "<u>%s<div class=tips><table class=det>",
1386 mod->name);
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001387 for (j = 0; j < mod->stats_count; ++j) {
1388 chunk_appendf(out,
1389 "<tr><th>%s</th><td>%s</td></tr>",
1390 mod->stats[j].desc, field_to_html_str(&stats[ST_F_TOTAL_FIELDS + i]));
1391 ++i;
1392 }
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001393 chunk_appendf(out, "</table></div></u>");
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001394 } else {
1395 i += mod->stats_count;
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001396 }
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001397
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001398 chunk_appendf(out, "</td>");
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001399 }
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001400 }
1401
1402 chunk_appendf(out, "</tr>\n");
William Lallemand74c24fb2016-11-21 17:18:36 +01001403 }
1404 else if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_BE) {
1405 chunk_appendf(out, "<tr class=\"backend\">");
Willy Tarreauab02b3f2019-10-09 11:11:46 +02001406 if (flags & STAT_ADMIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001407 /* Column sub-heading for Enable or Disable server */
1408 chunk_appendf(out, "<td></td>");
1409 }
1410 chunk_appendf(out,
1411 "<td class=ac>"
1412 /* name */
1413 "%s<a name=\"%s/Backend\"></a>"
1414 "<a class=lfsb href=\"#%s/Backend\">Backend</a>"
1415 "",
Willy Tarreau708c4162019-10-09 10:19:16 +02001416 (flags & STAT_SHLGNDS)?"<u>":"",
William Lallemand74c24fb2016-11-21 17:18:36 +01001417 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_PXNAME));
1418
Willy Tarreau708c4162019-10-09 10:19:16 +02001419 if (flags & STAT_SHLGNDS) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001420 /* balancing */
1421 chunk_appendf(out, "<div class=tips>balancing: %s",
1422 field_str(stats, ST_F_ALGO));
1423
1424 /* cookie */
1425 if (stats[ST_F_COOKIE].type) {
1426 chunk_appendf(out, ", cookie: '");
1427 chunk_initstr(&src, field_str(stats, ST_F_COOKIE));
1428 chunk_htmlencode(out, &src);
1429 chunk_appendf(out, "'");
1430 }
1431 chunk_appendf(out, "</div>");
1432 }
1433
1434 chunk_appendf(out,
1435 "%s</td>"
1436 /* queue : current, max */
1437 "<td>%s</td><td>%s</td><td></td>"
1438 /* sessions rate : current, max, limit */
1439 "<td>%s</td><td>%s</td><td></td>"
1440 "",
Willy Tarreau708c4162019-10-09 10:19:16 +02001441 (flags & STAT_SHLGNDS)?"</u>":"",
William Lallemand74c24fb2016-11-21 17:18:36 +01001442 U2H(stats[ST_F_QCUR].u.u32), U2H(stats[ST_F_QMAX].u.u32),
1443 U2H(stats[ST_F_RATE].u.u32), U2H(stats[ST_F_RATE_MAX].u.u32));
1444
1445 chunk_appendf(out,
1446 /* sessions: current, max, limit, total */
1447 "<td>%s</td><td>%s</td><td>%s</td>"
1448 "<td><u>%s<div class=tips><table class=det>"
1449 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
1450 "",
Willy Tarreau8e0f1752016-12-12 15:07:29 +01001451 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 +01001452 U2H(stats[ST_F_STOT].u.u64),
1453 U2H(stats[ST_F_STOT].u.u64));
1454
1455 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
1456 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0) {
1457 chunk_appendf(out,
Willy Tarreauf1573842018-12-14 11:35:36 +01001458 "<tr><th>New connections:</th><td>%s</td></tr>"
1459 "<tr><th>Reused connections:</th><td>%s</td><td>(%d%%)</td></tr>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001460 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
1461 "<tr><th>- HTTP 1xx responses:</th><td>%s</td></tr>"
1462 "<tr><th>- HTTP 2xx responses:</th><td>%s</td></tr>"
1463 "<tr><th>&nbsp;&nbsp;Compressed 2xx:</th><td>%s</td><td>(%d%%)</td></tr>"
1464 "<tr><th>- HTTP 3xx responses:</th><td>%s</td></tr>"
1465 "<tr><th>- HTTP 4xx responses:</th><td>%s</td></tr>"
1466 "<tr><th>- HTTP 5xx responses:</th><td>%s</td></tr>"
1467 "<tr><th>- other responses:</th><td>%s</td></tr>"
Willy Tarreaua1214a52018-12-14 14:00:25 +01001468 "<tr><th>Cache lookups:</th><td>%s</td></tr>"
1469 "<tr><th>Cache hits:</th><td>%s</td><td>(%d%%)</td></tr>"
Willy Tarreau1b0f85e2018-05-28 15:12:40 +02001470 "<tr><th>Failed hdr rewrites:</th><td>%s</td></tr>"
Christopher Faulet0159ee42019-12-16 14:40:39 +01001471 "<tr><th>Internal errors:</th><td>%s</td></tr>"
Christopher Faulet0d1c2a62019-11-08 14:59:51 +01001472 "",
Willy Tarreauf1573842018-12-14 11:35:36 +01001473 U2H(stats[ST_F_CONNECT].u.u64),
1474 U2H(stats[ST_F_REUSE].u.u64),
1475 (stats[ST_F_CONNECT].u.u64 + stats[ST_F_REUSE].u.u64) ?
1476 (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 +01001477 U2H(stats[ST_F_REQ_TOT].u.u64),
1478 U2H(stats[ST_F_HRSP_1XX].u.u64),
1479 U2H(stats[ST_F_HRSP_2XX].u.u64),
1480 U2H(stats[ST_F_COMP_RSP].u.u64),
1481 stats[ST_F_HRSP_2XX].u.u64 ?
1482 (int)(100 * stats[ST_F_COMP_RSP].u.u64 / stats[ST_F_HRSP_2XX].u.u64) : 0,
1483 U2H(stats[ST_F_HRSP_3XX].u.u64),
1484 U2H(stats[ST_F_HRSP_4XX].u.u64),
1485 U2H(stats[ST_F_HRSP_5XX].u.u64),
Willy Tarreaufeead3a2018-12-14 13:48:44 +01001486 U2H(stats[ST_F_HRSP_OTHER].u.u64),
Willy Tarreaua1214a52018-12-14 14:00:25 +01001487 U2H(stats[ST_F_CACHE_LOOKUPS].u.u64),
1488 U2H(stats[ST_F_CACHE_HITS].u.u64),
1489 stats[ST_F_CACHE_LOOKUPS].u.u64 ?
1490 (int)(100 * stats[ST_F_CACHE_HITS].u.u64 / stats[ST_F_CACHE_LOOKUPS].u.u64) : 0,
Christopher Faulet0159ee42019-12-16 14:40:39 +01001491 U2H(stats[ST_F_WREW].u.u64),
1492 U2H(stats[ST_F_EINT].u.u64));
William Lallemand74c24fb2016-11-21 17:18:36 +01001493 }
1494
Christopher Faulet0d1c2a62019-11-08 14:59:51 +01001495 chunk_appendf(out, "<tr><th colspan=3>Max / Avg over last 1024 success. conn.</th></tr>");
1496 chunk_appendf(out, "<tr><th>- Queue time:</th><td>%s / %s</td><td>ms</td></tr>",
1497 U2H(stats[ST_F_QT_MAX].u.u32), U2H(stats[ST_F_QTIME].u.u32));
1498 chunk_appendf(out, "<tr><th>- Connect time:</th><td>%s / %s</td><td>ms</td></tr>",
1499 U2H(stats[ST_F_CT_MAX].u.u32), U2H(stats[ST_F_CTIME].u.u32));
William Lallemand74c24fb2016-11-21 17:18:36 +01001500 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
Christopher Faulet0d1c2a62019-11-08 14:59:51 +01001501 chunk_appendf(out, "<tr><th>- Responses time:</th><td>%s / %s</td><td>ms</td></tr>",
1502 U2H(stats[ST_F_RT_MAX].u.u32), U2H(stats[ST_F_RTIME].u.u32));
1503 chunk_appendf(out, "<tr><th>- Total time:</th><td>%s / %s</td><td>ms</td></tr>",
1504 U2H(stats[ST_F_TT_MAX].u.u32), U2H(stats[ST_F_TTIME].u.u32));
William Lallemand74c24fb2016-11-21 17:18:36 +01001505
1506 chunk_appendf(out,
1507 "</table></div></u></td>"
1508 /* sessions: lbtot, last */
1509 "<td>%s</td><td>%s</td>"
1510 /* bytes: in */
1511 "<td>%s</td>"
1512 "",
1513 U2H(stats[ST_F_LBTOT].u.u64),
1514 human_time(stats[ST_F_LASTSESS].u.s32, 1),
1515 U2H(stats[ST_F_BIN].u.u64));
1516
1517 chunk_appendf(out,
1518 /* bytes:out + compression stats (via hover): comp_in, comp_out, comp_byp */
1519 "<td>%s%s<div class=tips><table class=det>"
1520 "<tr><th>Response bytes in:</th><td>%s</td></tr>"
1521 "<tr><th>Compression in:</th><td>%s</td></tr>"
1522 "<tr><th>Compression out:</th><td>%s</td><td>(%d%%)</td></tr>"
1523 "<tr><th>Compression bypass:</th><td>%s</td></tr>"
1524 "<tr><th>Total bytes saved:</th><td>%s</td><td>(%d%%)</td></tr>"
1525 "</table></div>%s</td>",
1526 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "<u>":"",
1527 U2H(stats[ST_F_BOUT].u.u64),
1528 U2H(stats[ST_F_BOUT].u.u64),
1529 U2H(stats[ST_F_COMP_IN].u.u64),
1530 U2H(stats[ST_F_COMP_OUT].u.u64),
1531 stats[ST_F_COMP_IN].u.u64 ? (int)(stats[ST_F_COMP_OUT].u.u64 * 100 / stats[ST_F_COMP_IN].u.u64) : 0,
1532 U2H(stats[ST_F_COMP_BYP].u.u64),
1533 U2H(stats[ST_F_COMP_IN].u.u64 - stats[ST_F_COMP_OUT].u.u64),
1534 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,
1535 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "</u>":"");
1536
1537 chunk_appendf(out,
1538 /* denied: req, resp */
1539 "<td>%s</td><td>%s</td>"
1540 /* errors : request, connect */
1541 "<td></td><td>%s</td>"
1542 /* errors : response */
1543 "<td><u>%s<div class=tips>Connection resets during transfers: %lld client, %lld server</div></u></td>"
1544 /* warnings: retries, redispatches */
1545 "<td>%lld</td><td>%lld</td>"
1546 /* backend status: reflect backend status (up/down): we display UP
1547 * if the backend has known working servers or if it has no server at
1548 * all (eg: for stats). Then we display the total weight, number of
1549 * active and backups. */
Willy Tarreaubd715102020-10-23 22:44:30 +02001550 "<td class=ac>%s %s</td><td class=ac>&nbsp;</td><td class=ac>%d/%d</td>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001551 "<td class=ac>%d</td><td class=ac>%d</td>"
1552 "",
1553 U2H(stats[ST_F_DREQ].u.u64), U2H(stats[ST_F_DRESP].u.u64),
1554 U2H(stats[ST_F_ECON].u.u64),
1555 U2H(stats[ST_F_ERESP].u.u64),
1556 (long long)stats[ST_F_CLI_ABRT].u.u64,
1557 (long long)stats[ST_F_SRV_ABRT].u.u64,
1558 (long long)stats[ST_F_WRETR].u.u64, (long long)stats[ST_F_WREDIS].u.u64,
1559 human_time(stats[ST_F_LASTCHG].u.u32, 1),
1560 strcmp(field_str(stats, ST_F_STATUS), "DOWN") ? field_str(stats, ST_F_STATUS) : "<font color=\"red\"><b>DOWN</b></font>",
Willy Tarreaubd715102020-10-23 22:44:30 +02001561 stats[ST_F_WEIGHT].u.u32, stats[ST_F_UWEIGHT].u.u32,
William Lallemand74c24fb2016-11-21 17:18:36 +01001562 stats[ST_F_ACT].u.u32, stats[ST_F_BCK].u.u32);
1563
1564 chunk_appendf(out,
1565 /* rest of backend: nothing, down transitions, total downtime, throttle */
1566 "<td class=ac>&nbsp;</td><td>%d</td>"
1567 "<td>%s</td>"
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001568 "<td></td>",
William Lallemand74c24fb2016-11-21 17:18:36 +01001569 stats[ST_F_CHKDOWN].u.u32,
1570 stats[ST_F_DOWNTIME].type ? human_time(stats[ST_F_DOWNTIME].u.u32, 1) : "&nbsp;");
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001571
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001572 if (flags & STAT_SHMODULES) {
1573 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001574 chunk_appendf(out, "<td>");
1575
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001576 if (stats_px_get_cap(mod->domain_flags) & STATS_PX_CAP_BE) {
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001577 chunk_appendf(out,
1578 "<u>%s<div class=tips><table class=det>",
1579 mod->name);
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001580 for (j = 0; j < mod->stats_count; ++j) {
1581 chunk_appendf(out,
1582 "<tr><th>%s</th><td>%s</td></tr>",
1583 mod->stats[j].desc, field_to_html_str(&stats[ST_F_TOTAL_FIELDS + i]));
1584 ++i;
1585 }
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001586 chunk_appendf(out, "</table></div></u>");
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001587 } else {
1588 i += mod->stats_count;
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001589 }
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001590
1591 chunk_appendf(out, "</td>");
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001592 }
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001593 }
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001594
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001595 chunk_appendf(out, "</tr>");
William Lallemand74c24fb2016-11-21 17:18:36 +01001596 }
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001597
William Lallemand74c24fb2016-11-21 17:18:36 +01001598 return 1;
1599}
1600
Amaury Denoyelle97323c92020-10-02 18:32:01 +02001601int stats_dump_one_line(const struct field *stats, size_t stats_count,
1602 struct appctx *appctx)
William Lallemand74c24fb2016-11-21 17:18:36 +01001603{
Simon Horman05ee2132017-01-04 09:37:25 +01001604 int ret;
1605
William Lallemand74c24fb2016-11-21 17:18:36 +01001606 if (appctx->ctx.stats.flags & STAT_FMT_HTML)
Willy Tarreau43241ff2019-10-09 11:27:51 +02001607 ret = stats_dump_fields_html(&trash, stats, appctx->ctx.stats.flags);
William Lallemand74c24fb2016-11-21 17:18:36 +01001608 else if (appctx->ctx.stats.flags & STAT_FMT_TYPED)
Amaury Denoyelle072f97e2020-10-05 11:49:37 +02001609 ret = stats_dump_fields_typed(&trash, stats, stats_count, appctx->ctx.stats.flags, appctx->ctx.stats.domain);
Simon Horman05ee2132017-01-04 09:37:25 +01001610 else if (appctx->ctx.stats.flags & STAT_FMT_JSON)
Amaury Denoyelle072f97e2020-10-05 11:49:37 +02001611 ret = stats_dump_fields_json(&trash, stats, stats_count, appctx->ctx.stats.flags, appctx->ctx.stats.domain);
William Lallemand74c24fb2016-11-21 17:18:36 +01001612 else
Amaury Denoyelle50660a82020-10-05 11:49:39 +02001613 ret = stats_dump_fields_csv(&trash, stats, stats_count, appctx->ctx.stats.flags, appctx->ctx.stats.domain);
Simon Horman05ee2132017-01-04 09:37:25 +01001614
1615 if (ret)
1616 appctx->ctx.stats.flags |= STAT_STARTED;
1617
1618 return ret;
William Lallemand74c24fb2016-11-21 17:18:36 +01001619}
1620
1621/* Fill <stats> with the frontend statistics. <stats> is
1622 * preallocated array of length <len>. The length of the array
1623 * must be at least ST_F_TOTAL_FIELDS. If this length is less then
1624 * this value, the function returns 0, otherwise, it returns 1.
1625 */
1626int stats_fill_fe_stats(struct proxy *px, struct field *stats, int len)
1627{
1628 if (len < ST_F_TOTAL_FIELDS)
1629 return 0;
1630
William Lallemand74c24fb2016-11-21 17:18:36 +01001631 stats[ST_F_PXNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
1632 stats[ST_F_SVNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, "FRONTEND");
1633 stats[ST_F_MODE] = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
1634 stats[ST_F_SCUR] = mkf_u32(0, px->feconn);
1635 stats[ST_F_SMAX] = mkf_u32(FN_MAX, px->fe_counters.conn_max);
1636 stats[ST_F_SLIM] = mkf_u32(FO_CONFIG|FN_LIMIT, px->maxconn);
1637 stats[ST_F_STOT] = mkf_u64(FN_COUNTER, px->fe_counters.cum_sess);
1638 stats[ST_F_BIN] = mkf_u64(FN_COUNTER, px->fe_counters.bytes_in);
1639 stats[ST_F_BOUT] = mkf_u64(FN_COUNTER, px->fe_counters.bytes_out);
1640 stats[ST_F_DREQ] = mkf_u64(FN_COUNTER, px->fe_counters.denied_req);
1641 stats[ST_F_DRESP] = mkf_u64(FN_COUNTER, px->fe_counters.denied_resp);
1642 stats[ST_F_EREQ] = mkf_u64(FN_COUNTER, px->fe_counters.failed_req);
1643 stats[ST_F_DCON] = mkf_u64(FN_COUNTER, px->fe_counters.denied_conn);
1644 stats[ST_F_DSES] = mkf_u64(FN_COUNTER, px->fe_counters.denied_sess);
Willy Tarreauc3914d42020-09-24 08:39:22 +02001645 stats[ST_F_STATUS] = mkf_str(FO_STATUS, px->disabled ? "STOP" : "OPEN");
William Lallemand74c24fb2016-11-21 17:18:36 +01001646 stats[ST_F_PID] = mkf_u32(FO_KEY, relative_pid);
1647 stats[ST_F_IID] = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
1648 stats[ST_F_SID] = mkf_u32(FO_KEY|FS_SERVICE, 0);
1649 stats[ST_F_TYPE] = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_FE);
1650 stats[ST_F_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&px->fe_sess_per_sec));
1651 stats[ST_F_RATE_LIM] = mkf_u32(FO_CONFIG|FN_LIMIT, px->fe_sps_lim);
1652 stats[ST_F_RATE_MAX] = mkf_u32(FN_MAX, px->fe_counters.sps_max);
Tim Duesterhus3fd19732018-05-27 20:35:08 +02001653 stats[ST_F_WREW] = mkf_u64(FN_COUNTER, px->fe_counters.failed_rewrites);
Christopher Faulet0159ee42019-12-16 14:40:39 +01001654 stats[ST_F_EINT] = mkf_u64(FN_COUNTER, px->fe_counters.internal_errors);
William Lallemand74c24fb2016-11-21 17:18:36 +01001655
1656 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
1657 if (px->mode == PR_MODE_HTTP) {
1658 stats[ST_F_HRSP_1XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[1]);
1659 stats[ST_F_HRSP_2XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[2]);
1660 stats[ST_F_HRSP_3XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[3]);
1661 stats[ST_F_HRSP_4XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[4]);
1662 stats[ST_F_HRSP_5XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[5]);
1663 stats[ST_F_HRSP_OTHER] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[0]);
1664 stats[ST_F_INTERCEPTED] = mkf_u64(FN_COUNTER, px->fe_counters.intercepted_req);
Willy Tarreaua1214a52018-12-14 14:00:25 +01001665 stats[ST_F_CACHE_LOOKUPS] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.cache_lookups);
1666 stats[ST_F_CACHE_HITS] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.cache_hits);
William Lallemand74c24fb2016-11-21 17:18:36 +01001667 }
1668
1669 /* requests : req_rate, req_rate_max, req_tot, */
1670 stats[ST_F_REQ_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&px->fe_req_per_sec));
1671 stats[ST_F_REQ_RATE_MAX] = mkf_u32(FN_MAX, px->fe_counters.p.http.rps_max);
1672 stats[ST_F_REQ_TOT] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.cum_req);
1673
1674 /* compression: in, out, bypassed, responses */
1675 stats[ST_F_COMP_IN] = mkf_u64(FN_COUNTER, px->fe_counters.comp_in);
1676 stats[ST_F_COMP_OUT] = mkf_u64(FN_COUNTER, px->fe_counters.comp_out);
1677 stats[ST_F_COMP_BYP] = mkf_u64(FN_COUNTER, px->fe_counters.comp_byp);
1678 stats[ST_F_COMP_RSP] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.comp_rsp);
1679
1680 /* connections : conn_rate, conn_rate_max, conn_tot, conn_max */
1681 stats[ST_F_CONN_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&px->fe_conn_per_sec));
1682 stats[ST_F_CONN_RATE_MAX] = mkf_u32(FN_MAX, px->fe_counters.cps_max);
1683 stats[ST_F_CONN_TOT] = mkf_u64(FN_COUNTER, px->fe_counters.cum_conn);
1684
1685 return 1;
1686}
1687
1688/* Dumps a frontend's line to the trash for the current proxy <px> and uses
1689 * the state from stream interface <si>. The caller is responsible for clearing
1690 * the trash if needed. Returns non-zero if it emits anything, zero otherwise.
1691 */
1692static int stats_dump_fe_stats(struct stream_interface *si, struct proxy *px)
1693{
1694 struct appctx *appctx = __objt_appctx(si->end);
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02001695 struct field *stats = stat_l[STATS_DOMAIN_PROXY];
1696 struct stats_module *mod;
1697 size_t stats_count = ST_F_TOTAL_FIELDS;
William Lallemand74c24fb2016-11-21 17:18:36 +01001698
1699 if (!(px->cap & PR_CAP_FE))
1700 return 0;
1701
1702 if ((appctx->ctx.stats.flags & STAT_BOUND) && !(appctx->ctx.stats.type & (1 << STATS_TYPE_FE)))
1703 return 0;
1704
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02001705 memset(stats, 0, sizeof(struct field) * stat_count[STATS_DOMAIN_PROXY]);
1706
William Lallemand74c24fb2016-11-21 17:18:36 +01001707 if (!stats_fill_fe_stats(px, stats, ST_F_TOTAL_FIELDS))
1708 return 0;
1709
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02001710 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
1711 void *counters;
1712
1713 if (!(stats_px_get_cap(mod->domain_flags) & STATS_PX_CAP_FE)) {
1714 stats_count += mod->stats_count;
1715 continue;
1716 }
1717
1718 counters = EXTRA_COUNTERS_GET(px->extra_counters_fe, mod);
1719 mod->fill_stats(counters, stats + stats_count);
1720 stats_count += mod->stats_count;
1721 }
1722
1723 return stats_dump_one_line(stats, stats_count, appctx);
William Lallemand74c24fb2016-11-21 17:18:36 +01001724}
1725
1726/* Fill <stats> with the listener statistics. <stats> is
1727 * preallocated array of length <len>. The length of the array
1728 * must be at least ST_F_TOTAL_FIELDS. If this length is less
1729 * then this value, the function returns 0, otherwise, it
Willy Tarreau708c4162019-10-09 10:19:16 +02001730 * returns 1. <flags> can take the value STAT_SHLGNDS.
William Lallemand74c24fb2016-11-21 17:18:36 +01001731 */
1732int stats_fill_li_stats(struct proxy *px, struct listener *l, int flags,
1733 struct field *stats, int len)
1734{
Willy Tarreau83061a82018-07-13 11:56:34 +02001735 struct buffer *out = get_trash_chunk();
William Lallemand74c24fb2016-11-21 17:18:36 +01001736
1737 if (len < ST_F_TOTAL_FIELDS)
1738 return 0;
1739
1740 if (!l->counters)
1741 return 0;
1742
1743 chunk_reset(out);
William Lallemand74c24fb2016-11-21 17:18:36 +01001744
1745 stats[ST_F_PXNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
1746 stats[ST_F_SVNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, l->name);
1747 stats[ST_F_MODE] = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
1748 stats[ST_F_SCUR] = mkf_u32(0, l->nbconn);
1749 stats[ST_F_SMAX] = mkf_u32(FN_MAX, l->counters->conn_max);
1750 stats[ST_F_SLIM] = mkf_u32(FO_CONFIG|FN_LIMIT, l->maxconn);
1751 stats[ST_F_STOT] = mkf_u64(FN_COUNTER, l->counters->cum_conn);
1752 stats[ST_F_BIN] = mkf_u64(FN_COUNTER, l->counters->bytes_in);
1753 stats[ST_F_BOUT] = mkf_u64(FN_COUNTER, l->counters->bytes_out);
1754 stats[ST_F_DREQ] = mkf_u64(FN_COUNTER, l->counters->denied_req);
1755 stats[ST_F_DRESP] = mkf_u64(FN_COUNTER, l->counters->denied_resp);
1756 stats[ST_F_EREQ] = mkf_u64(FN_COUNTER, l->counters->failed_req);
1757 stats[ST_F_DCON] = mkf_u64(FN_COUNTER, l->counters->denied_conn);
1758 stats[ST_F_DSES] = mkf_u64(FN_COUNTER, l->counters->denied_sess);
Willy Tarreaua8cf66b2019-02-27 16:49:00 +01001759 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 +01001760 stats[ST_F_PID] = mkf_u32(FO_KEY, relative_pid);
1761 stats[ST_F_IID] = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
1762 stats[ST_F_SID] = mkf_u32(FO_KEY|FS_SERVICE, l->luid);
1763 stats[ST_F_TYPE] = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_SO);
Tim Duesterhus3fd19732018-05-27 20:35:08 +02001764 stats[ST_F_WREW] = mkf_u64(FN_COUNTER, l->counters->failed_rewrites);
Christopher Faulet0159ee42019-12-16 14:40:39 +01001765 stats[ST_F_EINT] = mkf_u64(FN_COUNTER, l->counters->internal_errors);
William Lallemand74c24fb2016-11-21 17:18:36 +01001766
Willy Tarreau708c4162019-10-09 10:19:16 +02001767 if (flags & STAT_SHLGNDS) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001768 char str[INET6_ADDRSTRLEN];
1769 int port;
1770
Willy Tarreau37159062020-08-27 07:48:42 +02001771 port = get_host_port(&l->rx.addr);
1772 switch (addr_to_str(&l->rx.addr, str, sizeof(str))) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001773 case AF_INET:
1774 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
1775 chunk_appendf(out, "%s:%d", str, port);
1776 break;
1777 case AF_INET6:
1778 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
1779 chunk_appendf(out, "[%s]:%d", str, port);
1780 break;
1781 case AF_UNIX:
1782 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, "unix");
1783 break;
1784 case -1:
1785 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
1786 chunk_strcat(out, strerror(errno));
1787 break;
1788 default: /* address family not supported */
1789 break;
1790 }
1791 }
1792
1793 return 1;
1794}
1795
1796/* Dumps a line for listener <l> and proxy <px> to the trash and uses the state
Willy Tarreau43241ff2019-10-09 11:27:51 +02001797 * from stream interface <si>. The caller is responsible for clearing the trash
1798 * if needed. Returns non-zero if it emits anything, zero otherwise.
William Lallemand74c24fb2016-11-21 17:18:36 +01001799 */
Willy Tarreau43241ff2019-10-09 11:27:51 +02001800static int stats_dump_li_stats(struct stream_interface *si, struct proxy *px, struct listener *l)
William Lallemand74c24fb2016-11-21 17:18:36 +01001801{
1802 struct appctx *appctx = __objt_appctx(si->end);
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02001803 struct field *stats = stat_l[STATS_DOMAIN_PROXY];
1804 struct stats_module *mod;
1805 size_t stats_count = ST_F_TOTAL_FIELDS;
1806
1807 memset(stats, 0, sizeof(struct field) * stat_count[STATS_DOMAIN_PROXY]);
William Lallemand74c24fb2016-11-21 17:18:36 +01001808
Willy Tarreau43241ff2019-10-09 11:27:51 +02001809 if (!stats_fill_li_stats(px, l, appctx->ctx.stats.flags, stats, ST_F_TOTAL_FIELDS))
William Lallemand74c24fb2016-11-21 17:18:36 +01001810 return 0;
1811
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02001812 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
1813 void *counters;
1814
1815 if (!(stats_px_get_cap(mod->domain_flags) & STATS_PX_CAP_LI)) {
1816 stats_count += mod->stats_count;
1817 continue;
1818 }
1819
1820 counters = EXTRA_COUNTERS_GET(l->extra_counters, mod);
1821 mod->fill_stats(counters, stats + stats_count);
1822 stats_count += mod->stats_count;
1823 }
1824
1825 return stats_dump_one_line(stats, stats_count, appctx);
William Lallemand74c24fb2016-11-21 17:18:36 +01001826}
1827
1828enum srv_stats_state {
1829 SRV_STATS_STATE_DOWN = 0,
1830 SRV_STATS_STATE_DOWN_AGENT,
1831 SRV_STATS_STATE_GOING_UP,
1832 SRV_STATS_STATE_UP_GOING_DOWN,
1833 SRV_STATS_STATE_UP,
1834 SRV_STATS_STATE_NOLB_GOING_DOWN,
1835 SRV_STATS_STATE_NOLB,
1836 SRV_STATS_STATE_DRAIN_GOING_DOWN,
1837 SRV_STATS_STATE_DRAIN,
1838 SRV_STATS_STATE_DRAIN_AGENT,
1839 SRV_STATS_STATE_NO_CHECK,
1840
1841 SRV_STATS_STATE_COUNT, /* Must be last */
1842};
1843
1844static const char *srv_hlt_st[SRV_STATS_STATE_COUNT] = {
1845 [SRV_STATS_STATE_DOWN] = "DOWN",
1846 [SRV_STATS_STATE_DOWN_AGENT] = "DOWN (agent)",
1847 [SRV_STATS_STATE_GOING_UP] = "DOWN %d/%d",
1848 [SRV_STATS_STATE_UP_GOING_DOWN] = "UP %d/%d",
1849 [SRV_STATS_STATE_UP] = "UP",
1850 [SRV_STATS_STATE_NOLB_GOING_DOWN] = "NOLB %d/%d",
1851 [SRV_STATS_STATE_NOLB] = "NOLB",
1852 [SRV_STATS_STATE_DRAIN_GOING_DOWN] = "DRAIN %d/%d",
1853 [SRV_STATS_STATE_DRAIN] = "DRAIN",
1854 [SRV_STATS_STATE_DRAIN_AGENT] = "DRAIN (agent)",
1855 [SRV_STATS_STATE_NO_CHECK] = "no check"
1856};
1857
1858/* Fill <stats> with the server statistics. <stats> is
1859 * preallocated array of length <len>. The length of the array
1860 * must be at least ST_F_TOTAL_FIELDS. If this length is less
1861 * then this value, the function returns 0, otherwise, it
Willy Tarreau708c4162019-10-09 10:19:16 +02001862 * returns 1. <flags> can take the value STAT_SHLGNDS.
William Lallemand74c24fb2016-11-21 17:18:36 +01001863 */
1864int stats_fill_sv_stats(struct proxy *px, struct server *sv, int flags,
1865 struct field *stats, int len)
1866{
1867 struct server *via, *ref;
1868 char str[INET6_ADDRSTRLEN];
Willy Tarreau83061a82018-07-13 11:56:34 +02001869 struct buffer *out = get_trash_chunk();
William Lallemand74c24fb2016-11-21 17:18:36 +01001870 enum srv_stats_state state;
1871 char *fld_status;
Marcin Deraneka8dbdf32020-05-15 20:02:40 +02001872 long long srv_samples_counter;
1873 unsigned int srv_samples_window = TIME_STATS_SAMPLES;
William Lallemand74c24fb2016-11-21 17:18:36 +01001874
1875 if (len < ST_F_TOTAL_FIELDS)
1876 return 0;
1877
William Lallemand74c24fb2016-11-21 17:18:36 +01001878 /* we have "via" which is the tracked server as described in the configuration,
1879 * and "ref" which is the checked server and the end of the chain.
1880 */
1881 via = sv->track ? sv->track : sv;
1882 ref = via;
1883 while (ref->track)
1884 ref = ref->track;
1885
Emeric Brun52a91d32017-08-31 14:41:55 +02001886 if (sv->cur_state == SRV_ST_RUNNING || sv->cur_state == SRV_ST_STARTING) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001887 if ((ref->check.state & CHK_ST_ENABLED) &&
1888 (ref->check.health < ref->check.rise + ref->check.fall - 1)) {
1889 state = SRV_STATS_STATE_UP_GOING_DOWN;
1890 } else {
1891 state = SRV_STATS_STATE_UP;
1892 }
1893
Emeric Brun52a91d32017-08-31 14:41:55 +02001894 if (sv->cur_admin & SRV_ADMF_DRAIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001895 if (ref->agent.state & CHK_ST_ENABLED)
1896 state = SRV_STATS_STATE_DRAIN_AGENT;
1897 else if (state == SRV_STATS_STATE_UP_GOING_DOWN)
1898 state = SRV_STATS_STATE_DRAIN_GOING_DOWN;
1899 else
1900 state = SRV_STATS_STATE_DRAIN;
1901 }
1902
1903 if (state == SRV_STATS_STATE_UP && !(ref->check.state & CHK_ST_ENABLED)) {
1904 state = SRV_STATS_STATE_NO_CHECK;
1905 }
1906 }
Emeric Brun52a91d32017-08-31 14:41:55 +02001907 else if (sv->cur_state == SRV_ST_STOPPING) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001908 if ((!(sv->check.state & CHK_ST_ENABLED) && !sv->track) ||
1909 (ref->check.health == ref->check.rise + ref->check.fall - 1)) {
1910 state = SRV_STATS_STATE_NOLB;
1911 } else {
1912 state = SRV_STATS_STATE_NOLB_GOING_DOWN;
1913 }
1914 }
1915 else { /* stopped */
1916 if ((ref->agent.state & CHK_ST_ENABLED) && !ref->agent.health) {
1917 state = SRV_STATS_STATE_DOWN_AGENT;
1918 } else if ((ref->check.state & CHK_ST_ENABLED) && !ref->check.health) {
1919 state = SRV_STATS_STATE_DOWN; /* DOWN */
1920 } else if ((ref->agent.state & CHK_ST_ENABLED) || (ref->check.state & CHK_ST_ENABLED)) {
1921 state = SRV_STATS_STATE_GOING_UP;
1922 } else {
1923 state = SRV_STATS_STATE_DOWN; /* DOWN, unchecked */
1924 }
1925 }
1926
1927 chunk_reset(out);
1928
1929 stats[ST_F_PXNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
1930 stats[ST_F_SVNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, sv->id);
1931 stats[ST_F_MODE] = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
1932 stats[ST_F_QCUR] = mkf_u32(0, sv->nbpend);
1933 stats[ST_F_QMAX] = mkf_u32(FN_MAX, sv->counters.nbpend_max);
1934 stats[ST_F_SCUR] = mkf_u32(0, sv->cur_sess);
1935 stats[ST_F_SMAX] = mkf_u32(FN_MAX, sv->counters.cur_sess_max);
1936
1937 if (sv->maxconn)
1938 stats[ST_F_SLIM] = mkf_u32(FO_CONFIG|FN_LIMIT, sv->maxconn);
1939
Willy Tarreauf21d17b2019-09-08 09:24:56 +02001940 stats[ST_F_SRV_ICUR] = mkf_u32(0, sv->curr_idle_conns);
1941 if (sv->max_idle_conns != -1)
1942 stats[ST_F_SRV_ILIM] = mkf_u32(FO_CONFIG|FN_LIMIT, sv->max_idle_conns);
1943
William Lallemand74c24fb2016-11-21 17:18:36 +01001944 stats[ST_F_STOT] = mkf_u64(FN_COUNTER, sv->counters.cum_sess);
1945 stats[ST_F_BIN] = mkf_u64(FN_COUNTER, sv->counters.bytes_in);
1946 stats[ST_F_BOUT] = mkf_u64(FN_COUNTER, sv->counters.bytes_out);
Christopher Fauleta08546b2019-12-16 16:07:34 +01001947 stats[ST_F_DRESP] = mkf_u64(FN_COUNTER, sv->counters.denied_resp);
William Lallemand74c24fb2016-11-21 17:18:36 +01001948 stats[ST_F_ECON] = mkf_u64(FN_COUNTER, sv->counters.failed_conns);
1949 stats[ST_F_ERESP] = mkf_u64(FN_COUNTER, sv->counters.failed_resp);
1950 stats[ST_F_WRETR] = mkf_u64(FN_COUNTER, sv->counters.retries);
1951 stats[ST_F_WREDIS] = mkf_u64(FN_COUNTER, sv->counters.redispatches);
Tim Duesterhus3fd19732018-05-27 20:35:08 +02001952 stats[ST_F_WREW] = mkf_u64(FN_COUNTER, sv->counters.failed_rewrites);
Christopher Faulet0159ee42019-12-16 14:40:39 +01001953 stats[ST_F_EINT] = mkf_u64(FN_COUNTER, sv->counters.internal_errors);
Willy Tarreauf1573842018-12-14 11:35:36 +01001954 stats[ST_F_CONNECT] = mkf_u64(FN_COUNTER, sv->counters.connect);
1955 stats[ST_F_REUSE] = mkf_u64(FN_COUNTER, sv->counters.reuse);
William Lallemand74c24fb2016-11-21 17:18:36 +01001956
Willy Tarreau3bb617c2020-06-29 13:51:05 +02001957 stats[ST_F_IDLE_CONN_CUR] = mkf_u32(0, sv->curr_idle_nb);
1958 stats[ST_F_SAFE_CONN_CUR] = mkf_u32(0, sv->curr_safe_nb);
1959 stats[ST_F_USED_CONN_CUR] = mkf_u32(0, sv->curr_used_conns);
Willy Tarreaua9fcecb2020-06-29 15:38:53 +02001960 stats[ST_F_NEED_CONN_EST] = mkf_u32(0, sv->est_need_conns);
Willy Tarreau3bb617c2020-06-29 13:51:05 +02001961
William Lallemand74c24fb2016-11-21 17:18:36 +01001962 /* status */
1963 fld_status = chunk_newstr(out);
Emeric Brun52a91d32017-08-31 14:41:55 +02001964 if (sv->cur_admin & SRV_ADMF_RMAINT)
William Lallemand74c24fb2016-11-21 17:18:36 +01001965 chunk_appendf(out, "MAINT (resolution)");
Emeric Brun52a91d32017-08-31 14:41:55 +02001966 else if (sv->cur_admin & SRV_ADMF_IMAINT)
William Lallemand74c24fb2016-11-21 17:18:36 +01001967 chunk_appendf(out, "MAINT (via %s/%s)", via->proxy->id, via->id);
Emeric Brun52a91d32017-08-31 14:41:55 +02001968 else if (sv->cur_admin & SRV_ADMF_MAINT)
William Lallemand74c24fb2016-11-21 17:18:36 +01001969 chunk_appendf(out, "MAINT");
1970 else
1971 chunk_appendf(out,
1972 srv_hlt_st[state],
Emeric Brun52a91d32017-08-31 14:41:55 +02001973 (ref->cur_state != SRV_ST_STOPPED) ? (ref->check.health - ref->check.rise + 1) : (ref->check.health),
1974 (ref->cur_state != SRV_ST_STOPPED) ? (ref->check.fall) : (ref->check.rise));
William Lallemand74c24fb2016-11-21 17:18:36 +01001975
1976 stats[ST_F_STATUS] = mkf_str(FO_STATUS, fld_status);
1977 stats[ST_F_LASTCHG] = mkf_u32(FN_AGE, now.tv_sec - sv->last_change);
Emeric Brun52a91d32017-08-31 14:41:55 +02001978 stats[ST_F_WEIGHT] = mkf_u32(FN_AVG, (sv->cur_eweight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv);
Willy Tarreaubd715102020-10-23 22:44:30 +02001979 stats[ST_F_UWEIGHT] = mkf_u32(FN_AVG, sv->uweight);
William Lallemand74c24fb2016-11-21 17:18:36 +01001980 stats[ST_F_ACT] = mkf_u32(FO_STATUS, (sv->flags & SRV_F_BACKUP) ? 0 : 1);
1981 stats[ST_F_BCK] = mkf_u32(FO_STATUS, (sv->flags & SRV_F_BACKUP) ? 1 : 0);
1982
1983 /* check failures: unique, fatal; last change, total downtime */
1984 if (sv->check.state & CHK_ST_ENABLED) {
1985 stats[ST_F_CHKFAIL] = mkf_u64(FN_COUNTER, sv->counters.failed_checks);
1986 stats[ST_F_CHKDOWN] = mkf_u64(FN_COUNTER, sv->counters.down_trans);
1987 stats[ST_F_DOWNTIME] = mkf_u32(FN_COUNTER, srv_downtime(sv));
1988 }
1989
1990 if (sv->maxqueue)
1991 stats[ST_F_QLIMIT] = mkf_u32(FO_CONFIG|FS_SERVICE, sv->maxqueue);
1992
1993 stats[ST_F_PID] = mkf_u32(FO_KEY, relative_pid);
1994 stats[ST_F_IID] = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
1995 stats[ST_F_SID] = mkf_u32(FO_KEY|FS_SERVICE, sv->puid);
1996
Emeric Brun52a91d32017-08-31 14:41:55 +02001997 if (sv->cur_state == SRV_ST_STARTING && !server_is_draining(sv))
William Lallemand74c24fb2016-11-21 17:18:36 +01001998 stats[ST_F_THROTTLE] = mkf_u32(FN_AVG, server_throttle_rate(sv));
1999
2000 stats[ST_F_LBTOT] = mkf_u64(FN_COUNTER, sv->counters.cum_lbconn);
2001
2002 if (sv->track) {
2003 char *fld_track = chunk_newstr(out);
2004
2005 chunk_appendf(out, "%s/%s", sv->track->proxy->id, sv->track->id);
2006 stats[ST_F_TRACKED] = mkf_str(FO_CONFIG|FN_NAME|FS_SERVICE, fld_track);
2007 }
2008
2009 stats[ST_F_TYPE] = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_SV);
2010 stats[ST_F_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&sv->sess_per_sec));
2011 stats[ST_F_RATE_MAX] = mkf_u32(FN_MAX, sv->counters.sps_max);
2012
2013 if ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED) {
2014 const char *fld_chksts;
2015
2016 fld_chksts = chunk_newstr(out);
2017 chunk_strcat(out, "* "); // for check in progress
2018 chunk_strcat(out, get_check_status_info(sv->check.status));
2019 if (!(sv->check.state & CHK_ST_INPROGRESS))
2020 fld_chksts += 2; // skip "* "
2021 stats[ST_F_CHECK_STATUS] = mkf_str(FN_OUTPUT, fld_chksts);
2022
2023 if (sv->check.status >= HCHK_STATUS_L57DATA)
2024 stats[ST_F_CHECK_CODE] = mkf_u32(FN_OUTPUT, sv->check.code);
2025
2026 if (sv->check.status >= HCHK_STATUS_CHECKED)
2027 stats[ST_F_CHECK_DURATION] = mkf_u64(FN_DURATION, sv->check.duration);
2028
2029 stats[ST_F_CHECK_DESC] = mkf_str(FN_OUTPUT, get_check_status_description(sv->check.status));
2030 stats[ST_F_LAST_CHK] = mkf_str(FN_OUTPUT, sv->check.desc);
2031 stats[ST_F_CHECK_RISE] = mkf_u32(FO_CONFIG|FS_SERVICE, ref->check.rise);
2032 stats[ST_F_CHECK_FALL] = mkf_u32(FO_CONFIG|FS_SERVICE, ref->check.fall);
2033 stats[ST_F_CHECK_HEALTH] = mkf_u32(FO_CONFIG|FS_SERVICE, ref->check.health);
2034 }
2035
2036 if ((sv->agent.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED) {
2037 const char *fld_chksts;
2038
2039 fld_chksts = chunk_newstr(out);
2040 chunk_strcat(out, "* "); // for check in progress
2041 chunk_strcat(out, get_check_status_info(sv->agent.status));
2042 if (!(sv->agent.state & CHK_ST_INPROGRESS))
2043 fld_chksts += 2; // skip "* "
2044 stats[ST_F_AGENT_STATUS] = mkf_str(FN_OUTPUT, fld_chksts);
2045
2046 if (sv->agent.status >= HCHK_STATUS_L57DATA)
2047 stats[ST_F_AGENT_CODE] = mkf_u32(FN_OUTPUT, sv->agent.code);
2048
2049 if (sv->agent.status >= HCHK_STATUS_CHECKED)
2050 stats[ST_F_AGENT_DURATION] = mkf_u64(FN_DURATION, sv->agent.duration);
2051
2052 stats[ST_F_AGENT_DESC] = mkf_str(FN_OUTPUT, get_check_status_description(sv->agent.status));
2053 stats[ST_F_LAST_AGT] = mkf_str(FN_OUTPUT, sv->agent.desc);
2054 stats[ST_F_AGENT_RISE] = mkf_u32(FO_CONFIG|FS_SERVICE, sv->agent.rise);
2055 stats[ST_F_AGENT_FALL] = mkf_u32(FO_CONFIG|FS_SERVICE, sv->agent.fall);
2056 stats[ST_F_AGENT_HEALTH] = mkf_u32(FO_CONFIG|FS_SERVICE, sv->agent.health);
2057 }
2058
2059 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
2060 if (px->mode == PR_MODE_HTTP) {
Marcin Deranek3c27dda2020-05-15 18:32:51 +02002061 stats[ST_F_REQ_TOT] = mkf_u64(FN_COUNTER, sv->counters.p.http.cum_req);
William Lallemand74c24fb2016-11-21 17:18:36 +01002062 stats[ST_F_HRSP_1XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[1]);
2063 stats[ST_F_HRSP_2XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[2]);
2064 stats[ST_F_HRSP_3XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[3]);
2065 stats[ST_F_HRSP_4XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[4]);
2066 stats[ST_F_HRSP_5XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[5]);
2067 stats[ST_F_HRSP_OTHER] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[0]);
2068 }
2069
2070 if (ref->observe)
2071 stats[ST_F_HANAFAIL] = mkf_u64(FN_COUNTER, sv->counters.failed_hana);
2072
2073 stats[ST_F_CLI_ABRT] = mkf_u64(FN_COUNTER, sv->counters.cli_aborts);
2074 stats[ST_F_SRV_ABRT] = mkf_u64(FN_COUNTER, sv->counters.srv_aborts);
2075 stats[ST_F_LASTSESS] = mkf_s32(FN_AGE, srv_lastsession(sv));
2076
Marcin Deraneka8dbdf32020-05-15 20:02:40 +02002077 srv_samples_counter = (px->mode == PR_MODE_HTTP) ? sv->counters.p.http.cum_req : sv->counters.cum_lbconn;
2078 if (srv_samples_counter < TIME_STATS_SAMPLES && srv_samples_counter > 0)
2079 srv_samples_window = srv_samples_counter;
2080
2081 stats[ST_F_QTIME] = mkf_u32(FN_AVG, swrate_avg(sv->counters.q_time, srv_samples_window));
2082 stats[ST_F_CTIME] = mkf_u32(FN_AVG, swrate_avg(sv->counters.c_time, srv_samples_window));
2083 stats[ST_F_RTIME] = mkf_u32(FN_AVG, swrate_avg(sv->counters.d_time, srv_samples_window));
2084 stats[ST_F_TTIME] = mkf_u32(FN_AVG, swrate_avg(sv->counters.t_time, srv_samples_window));
William Lallemand74c24fb2016-11-21 17:18:36 +01002085
Christopher Faulet0d1c2a62019-11-08 14:59:51 +01002086 stats[ST_F_QT_MAX] = mkf_u32(FN_MAX, sv->counters.qtime_max);
2087 stats[ST_F_CT_MAX] = mkf_u32(FN_MAX, sv->counters.ctime_max);
2088 stats[ST_F_RT_MAX] = mkf_u32(FN_MAX, sv->counters.dtime_max);
2089 stats[ST_F_TT_MAX] = mkf_u32(FN_MAX, sv->counters.ttime_max);
2090
Willy Tarreau708c4162019-10-09 10:19:16 +02002091 if (flags & STAT_SHLGNDS) {
William Lallemand74c24fb2016-11-21 17:18:36 +01002092 switch (addr_to_str(&sv->addr, str, sizeof(str))) {
2093 case AF_INET:
2094 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
Willy Tarreau04276f32017-01-06 17:41:29 +01002095 chunk_appendf(out, "%s:%d", str, sv->svc_port);
William Lallemand74c24fb2016-11-21 17:18:36 +01002096 break;
2097 case AF_INET6:
2098 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
Willy Tarreau04276f32017-01-06 17:41:29 +01002099 chunk_appendf(out, "[%s]:%d", str, sv->svc_port);
William Lallemand74c24fb2016-11-21 17:18:36 +01002100 break;
2101 case AF_UNIX:
2102 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, "unix");
2103 break;
2104 case -1:
2105 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
2106 chunk_strcat(out, strerror(errno));
2107 break;
2108 default: /* address family not supported */
2109 break;
2110 }
2111
2112 if (sv->cookie)
2113 stats[ST_F_COOKIE] = mkf_str(FO_CONFIG|FN_NAME|FS_SERVICE, sv->cookie);
2114 }
2115
2116 return 1;
2117}
2118
2119/* Dumps a line for server <sv> and proxy <px> to the trash and uses the state
Willy Tarreau43241ff2019-10-09 11:27:51 +02002120 * from stream interface <si>, and server state <state>. The caller is
2121 * responsible for clearing the trash if needed. Returns non-zero if it emits
2122 * anything, zero otherwise.
William Lallemand74c24fb2016-11-21 17:18:36 +01002123 */
Willy Tarreau43241ff2019-10-09 11:27:51 +02002124static int stats_dump_sv_stats(struct stream_interface *si, struct proxy *px, struct server *sv)
William Lallemand74c24fb2016-11-21 17:18:36 +01002125{
2126 struct appctx *appctx = __objt_appctx(si->end);
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02002127 struct stats_module *mod;
2128 struct field *stats = stat_l[STATS_DOMAIN_PROXY];
2129 size_t stats_count = ST_F_TOTAL_FIELDS;
2130
2131 memset(stats, 0, sizeof(struct field) * stat_count[STATS_DOMAIN_PROXY]);
William Lallemand74c24fb2016-11-21 17:18:36 +01002132
Willy Tarreau43241ff2019-10-09 11:27:51 +02002133 if (!stats_fill_sv_stats(px, sv, appctx->ctx.stats.flags, stats, ST_F_TOTAL_FIELDS))
William Lallemand74c24fb2016-11-21 17:18:36 +01002134 return 0;
2135
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02002136 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
2137 void *counters;
2138
2139 if (stats_get_domain(mod->domain_flags) != STATS_DOMAIN_PROXY)
2140 continue;
2141
2142 if (!(stats_px_get_cap(mod->domain_flags) & STATS_PX_CAP_SRV)) {
2143 stats_count += mod->stats_count;
2144 continue;
2145 }
2146
2147 counters = EXTRA_COUNTERS_GET(sv->extra_counters, mod);
2148 mod->fill_stats(counters, stats + stats_count);
2149 stats_count += mod->stats_count;
2150 }
2151
2152 return stats_dump_one_line(stats, stats_count, appctx);
William Lallemand74c24fb2016-11-21 17:18:36 +01002153}
2154
2155/* Fill <stats> with the backend statistics. <stats> is
2156 * preallocated array of length <len>. The length of the array
2157 * must be at least ST_F_TOTAL_FIELDS. If this length is less
2158 * then this value, the function returns 0, otherwise, it
Willy Tarreau708c4162019-10-09 10:19:16 +02002159 * returns 1. <flags> can take the value STAT_SHLGNDS.
William Lallemand74c24fb2016-11-21 17:18:36 +01002160 */
2161int stats_fill_be_stats(struct proxy *px, int flags, struct field *stats, int len)
2162{
Marcin Deraneka8dbdf32020-05-15 20:02:40 +02002163 long long be_samples_counter;
2164 unsigned int be_samples_window = TIME_STATS_SAMPLES;
Willy Tarreau2fbe6942020-10-23 18:02:54 +02002165 struct buffer *out = get_trash_chunk();
2166 const struct server *srv;
2167 int nbup, nbsrv;
Willy Tarreaubd715102020-10-23 22:44:30 +02002168 int totuw;
Willy Tarreau2fbe6942020-10-23 18:02:54 +02002169 char *fld;
Marcin Deraneka8dbdf32020-05-15 20:02:40 +02002170
William Lallemand74c24fb2016-11-21 17:18:36 +01002171 if (len < ST_F_TOTAL_FIELDS)
2172 return 0;
2173
Willy Tarreaubd715102020-10-23 22:44:30 +02002174 totuw = 0;
Willy Tarreau2fbe6942020-10-23 18:02:54 +02002175 nbup = nbsrv = 0;
Willy Tarreaubd715102020-10-23 22:44:30 +02002176 for (srv = px->srv; srv; srv = srv->next) {
2177 if (srv->cur_state != SRV_ST_STOPPED) {
2178 nbup++;
2179 if (srv_currently_usable(srv) &&
2180 (!px->srv_act ^ !(srv->flags & SRV_F_BACKUP)))
2181 totuw += srv->uweight;
Willy Tarreau2fbe6942020-10-23 18:02:54 +02002182 }
Willy Tarreaubd715102020-10-23 22:44:30 +02002183 nbsrv++;
Willy Tarreau2fbe6942020-10-23 18:02:54 +02002184 }
2185
Willy Tarreaubd715102020-10-23 22:44:30 +02002186 HA_RWLOCK_RDLOCK(LBPRM_LOCK, &px->lbprm.lock);
2187 if (!px->srv_act && px->lbprm.fbck)
2188 totuw = px->lbprm.fbck->uweight;
2189 HA_RWLOCK_RDUNLOCK(LBPRM_LOCK, &px->lbprm.lock);
2190
William Lallemand74c24fb2016-11-21 17:18:36 +01002191 stats[ST_F_PXNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
2192 stats[ST_F_SVNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, "BACKEND");
2193 stats[ST_F_MODE] = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
2194 stats[ST_F_QCUR] = mkf_u32(0, px->nbpend);
2195 stats[ST_F_QMAX] = mkf_u32(FN_MAX, px->be_counters.nbpend_max);
Thierry FOURNIER0ff98a42016-12-19 16:50:42 +01002196 stats[ST_F_SCUR] = mkf_u32(0, px->beconn);
William Lallemand74c24fb2016-11-21 17:18:36 +01002197 stats[ST_F_SMAX] = mkf_u32(FN_MAX, px->be_counters.conn_max);
2198 stats[ST_F_SLIM] = mkf_u32(FO_CONFIG|FN_LIMIT, px->fullconn);
2199 stats[ST_F_STOT] = mkf_u64(FN_COUNTER, px->be_counters.cum_conn);
2200 stats[ST_F_BIN] = mkf_u64(FN_COUNTER, px->be_counters.bytes_in);
2201 stats[ST_F_BOUT] = mkf_u64(FN_COUNTER, px->be_counters.bytes_out);
2202 stats[ST_F_DREQ] = mkf_u64(FN_COUNTER, px->be_counters.denied_req);
2203 stats[ST_F_DRESP] = mkf_u64(FN_COUNTER, px->be_counters.denied_resp);
2204 stats[ST_F_ECON] = mkf_u64(FN_COUNTER, px->be_counters.failed_conns);
2205 stats[ST_F_ERESP] = mkf_u64(FN_COUNTER, px->be_counters.failed_resp);
2206 stats[ST_F_WRETR] = mkf_u64(FN_COUNTER, px->be_counters.retries);
2207 stats[ST_F_WREDIS] = mkf_u64(FN_COUNTER, px->be_counters.redispatches);
Tim Duesterhus3fd19732018-05-27 20:35:08 +02002208 stats[ST_F_WREW] = mkf_u64(FN_COUNTER, px->be_counters.failed_rewrites);
Christopher Faulet0159ee42019-12-16 14:40:39 +01002209 stats[ST_F_EINT] = mkf_u64(FN_COUNTER, px->be_counters.internal_errors);
Willy Tarreauf1573842018-12-14 11:35:36 +01002210 stats[ST_F_CONNECT] = mkf_u64(FN_COUNTER, px->be_counters.connect);
2211 stats[ST_F_REUSE] = mkf_u64(FN_COUNTER, px->be_counters.reuse);
Willy Tarreau2fbe6942020-10-23 18:02:54 +02002212
2213 fld = chunk_newstr(out);
2214 chunk_appendf(out, "%s", (px->lbprm.tot_weight > 0 || !px->srv) ? "UP" : "DOWN");
2215 if (flags & (STAT_HIDE_MAINT|STAT_HIDE_DOWN))
2216 chunk_appendf(out, " (%d/%d)", nbup, nbsrv);
2217
2218 stats[ST_F_STATUS] = mkf_str(FO_STATUS, fld);
William Lallemand74c24fb2016-11-21 17:18:36 +01002219 stats[ST_F_WEIGHT] = mkf_u32(FN_AVG, (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv);
Willy Tarreaubd715102020-10-23 22:44:30 +02002220 stats[ST_F_UWEIGHT] = mkf_u32(FN_AVG, totuw);
William Lallemand74c24fb2016-11-21 17:18:36 +01002221 stats[ST_F_ACT] = mkf_u32(0, px->srv_act);
2222 stats[ST_F_BCK] = mkf_u32(0, px->srv_bck);
2223 stats[ST_F_CHKDOWN] = mkf_u64(FN_COUNTER, px->down_trans);
2224 stats[ST_F_LASTCHG] = mkf_u32(FN_AGE, now.tv_sec - px->last_change);
2225 if (px->srv)
2226 stats[ST_F_DOWNTIME] = mkf_u32(FN_COUNTER, be_downtime(px));
2227
2228 stats[ST_F_PID] = mkf_u32(FO_KEY, relative_pid);
2229 stats[ST_F_IID] = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
2230 stats[ST_F_SID] = mkf_u32(FO_KEY|FS_SERVICE, 0);
2231 stats[ST_F_LBTOT] = mkf_u64(FN_COUNTER, px->be_counters.cum_lbconn);
2232 stats[ST_F_TYPE] = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_BE);
2233 stats[ST_F_RATE] = mkf_u32(0, read_freq_ctr(&px->be_sess_per_sec));
2234 stats[ST_F_RATE_MAX] = mkf_u32(0, px->be_counters.sps_max);
2235
Willy Tarreau708c4162019-10-09 10:19:16 +02002236 if (flags & STAT_SHLGNDS) {
William Lallemand74c24fb2016-11-21 17:18:36 +01002237 if (px->cookie_name)
2238 stats[ST_F_COOKIE] = mkf_str(FO_CONFIG|FN_NAME|FS_SERVICE, px->cookie_name);
2239 stats[ST_F_ALGO] = mkf_str(FO_CONFIG|FS_SERVICE, backend_lb_algo_str(px->lbprm.algo & BE_LB_ALGO));
2240 }
2241
2242 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
2243 if (px->mode == PR_MODE_HTTP) {
2244 stats[ST_F_REQ_TOT] = mkf_u64(FN_COUNTER, px->be_counters.p.http.cum_req);
2245 stats[ST_F_HRSP_1XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[1]);
2246 stats[ST_F_HRSP_2XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[2]);
2247 stats[ST_F_HRSP_3XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[3]);
2248 stats[ST_F_HRSP_4XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[4]);
2249 stats[ST_F_HRSP_5XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[5]);
2250 stats[ST_F_HRSP_OTHER] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[0]);
Willy Tarreaua1214a52018-12-14 14:00:25 +01002251 stats[ST_F_CACHE_LOOKUPS] = mkf_u64(FN_COUNTER, px->be_counters.p.http.cache_lookups);
2252 stats[ST_F_CACHE_HITS] = mkf_u64(FN_COUNTER, px->be_counters.p.http.cache_hits);
William Lallemand74c24fb2016-11-21 17:18:36 +01002253 }
2254
2255 stats[ST_F_CLI_ABRT] = mkf_u64(FN_COUNTER, px->be_counters.cli_aborts);
2256 stats[ST_F_SRV_ABRT] = mkf_u64(FN_COUNTER, px->be_counters.srv_aborts);
2257
2258 /* compression: in, out, bypassed, responses */
2259 stats[ST_F_COMP_IN] = mkf_u64(FN_COUNTER, px->be_counters.comp_in);
2260 stats[ST_F_COMP_OUT] = mkf_u64(FN_COUNTER, px->be_counters.comp_out);
2261 stats[ST_F_COMP_BYP] = mkf_u64(FN_COUNTER, px->be_counters.comp_byp);
2262 stats[ST_F_COMP_RSP] = mkf_u64(FN_COUNTER, px->be_counters.p.http.comp_rsp);
2263 stats[ST_F_LASTSESS] = mkf_s32(FN_AGE, be_lastsession(px));
2264
Marcin Deraneka8dbdf32020-05-15 20:02:40 +02002265 be_samples_counter = (px->mode == PR_MODE_HTTP) ? px->be_counters.p.http.cum_req : px->be_counters.cum_lbconn;
2266 if (be_samples_counter < TIME_STATS_SAMPLES && be_samples_counter > 0)
2267 be_samples_window = be_samples_counter;
2268
2269 stats[ST_F_QTIME] = mkf_u32(FN_AVG, swrate_avg(px->be_counters.q_time, be_samples_window));
2270 stats[ST_F_CTIME] = mkf_u32(FN_AVG, swrate_avg(px->be_counters.c_time, be_samples_window));
2271 stats[ST_F_RTIME] = mkf_u32(FN_AVG, swrate_avg(px->be_counters.d_time, be_samples_window));
2272 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 +01002273
Christopher Faulet0d1c2a62019-11-08 14:59:51 +01002274 stats[ST_F_QT_MAX] = mkf_u32(FN_MAX, px->be_counters.qtime_max);
2275 stats[ST_F_CT_MAX] = mkf_u32(FN_MAX, px->be_counters.ctime_max);
2276 stats[ST_F_RT_MAX] = mkf_u32(FN_MAX, px->be_counters.dtime_max);
2277 stats[ST_F_TT_MAX] = mkf_u32(FN_MAX, px->be_counters.ttime_max);
2278
William Lallemand74c24fb2016-11-21 17:18:36 +01002279 return 1;
2280}
2281
2282/* Dumps a line for backend <px> to the trash for and uses the state from stream
Willy Tarreau43241ff2019-10-09 11:27:51 +02002283 * interface <si>. The caller is responsible for clearing the trash if needed.
2284 * Returns non-zero if it emits anything, zero otherwise.
William Lallemand74c24fb2016-11-21 17:18:36 +01002285 */
Willy Tarreau43241ff2019-10-09 11:27:51 +02002286static int stats_dump_be_stats(struct stream_interface *si, struct proxy *px)
William Lallemand74c24fb2016-11-21 17:18:36 +01002287{
2288 struct appctx *appctx = __objt_appctx(si->end);
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02002289 struct field *stats = stat_l[STATS_DOMAIN_PROXY];
2290 struct stats_module *mod;
2291 size_t stats_count = ST_F_TOTAL_FIELDS;
William Lallemand74c24fb2016-11-21 17:18:36 +01002292
2293 if (!(px->cap & PR_CAP_BE))
2294 return 0;
2295
2296 if ((appctx->ctx.stats.flags & STAT_BOUND) && !(appctx->ctx.stats.type & (1 << STATS_TYPE_BE)))
2297 return 0;
2298
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02002299 memset(stats, 0, sizeof(struct field) * stat_count[STATS_DOMAIN_PROXY]);
2300
Willy Tarreau43241ff2019-10-09 11:27:51 +02002301 if (!stats_fill_be_stats(px, appctx->ctx.stats.flags, stats, ST_F_TOTAL_FIELDS))
William Lallemand74c24fb2016-11-21 17:18:36 +01002302 return 0;
2303
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02002304 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
2305 struct extra_counters *counters;
2306
2307 if (stats_get_domain(mod->domain_flags) != STATS_DOMAIN_PROXY)
2308 continue;
2309
2310 if (!(stats_px_get_cap(mod->domain_flags) & STATS_PX_CAP_BE)) {
2311 stats_count += mod->stats_count;
2312 continue;
2313 }
2314
2315 counters = EXTRA_COUNTERS_GET(px->extra_counters_be, mod);
2316 mod->fill_stats(counters, stats + stats_count);
2317 stats_count += mod->stats_count;
2318 }
2319
2320 return stats_dump_one_line(stats, stats_count, appctx);
William Lallemand74c24fb2016-11-21 17:18:36 +01002321}
2322
2323/* Dumps the HTML table header for proxy <px> to the trash for and uses the state from
2324 * stream interface <si> and per-uri parameters <uri>. The caller is responsible
2325 * for clearing the trash if needed.
2326 */
Willy Tarreau676c29e2019-10-09 10:50:01 +02002327static void stats_dump_html_px_hdr(struct stream_interface *si, struct proxy *px)
William Lallemand74c24fb2016-11-21 17:18:36 +01002328{
2329 struct appctx *appctx = __objt_appctx(si->end);
2330 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02002331 struct stats_module *mod;
2332 int stats_module_len = 0;
William Lallemand74c24fb2016-11-21 17:18:36 +01002333
2334 if (px->cap & PR_CAP_BE && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
2335 /* A form to enable/disable this proxy servers */
2336
2337 /* scope_txt = search pattern + search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
2338 scope_txt[0] = 0;
2339 if (appctx->ctx.stats.scope_len) {
Christopher Fauleted7a0662019-01-14 11:07:34 +01002340 const char *scope_ptr = stats_scope_ptr(appctx, si);
2341
William Lallemand74c24fb2016-11-21 17:18:36 +01002342 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Christopher Fauleted7a0662019-01-14 11:07:34 +01002343 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), scope_ptr, appctx->ctx.stats.scope_len);
William Lallemand74c24fb2016-11-21 17:18:36 +01002344 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
2345 }
2346
2347 chunk_appendf(&trash,
2348 "<form method=\"post\">");
2349 }
2350
2351 /* print a new table */
2352 chunk_appendf(&trash,
2353 "<table class=\"tbl\" width=\"100%%\">\n"
2354 "<tr class=\"titre\">"
2355 "<th class=\"pxname\" width=\"10%%\">");
2356
2357 chunk_appendf(&trash,
2358 "<a name=\"%s\"></a>%s"
2359 "<a class=px href=\"#%s\">%s</a>",
2360 px->id,
Willy Tarreau676c29e2019-10-09 10:50:01 +02002361 (appctx->ctx.stats.flags & STAT_SHLGNDS) ? "<u>":"",
William Lallemand74c24fb2016-11-21 17:18:36 +01002362 px->id, px->id);
2363
Willy Tarreau676c29e2019-10-09 10:50:01 +02002364 if (appctx->ctx.stats.flags & STAT_SHLGNDS) {
William Lallemand74c24fb2016-11-21 17:18:36 +01002365 /* cap, mode, id */
2366 chunk_appendf(&trash, "<div class=tips>cap: %s, mode: %s, id: %d",
2367 proxy_cap_str(px->cap), proxy_mode_str(px->mode),
2368 px->uuid);
2369 chunk_appendf(&trash, "</div>");
2370 }
2371
2372 chunk_appendf(&trash,
2373 "%s</th>"
2374 "<th class=\"%s\" width=\"90%%\">%s</th>"
2375 "</tr>\n"
2376 "</table>\n"
2377 "<table class=\"tbl\" width=\"100%%\">\n"
2378 "<tr class=\"titre\">",
Willy Tarreau676c29e2019-10-09 10:50:01 +02002379 (appctx->ctx.stats.flags & STAT_SHLGNDS) ? "</u>":"",
William Lallemand74c24fb2016-11-21 17:18:36 +01002380 px->desc ? "desc" : "empty", px->desc ? px->desc : "");
2381
Christopher Fauletbc271ec2019-12-02 11:29:04 +01002382 if (appctx->ctx.stats.flags & STAT_ADMIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +01002383 /* Column heading for Enable or Disable server */
Christopher Fauletbc271ec2019-12-02 11:29:04 +01002384 if ((px->cap & PR_CAP_BE) && px->srv)
2385 chunk_appendf(&trash,
2386 "<th rowspan=2 width=1><input type=\"checkbox\" "
2387 "onclick=\"for(c in document.getElementsByClassName('%s-checkbox')) "
2388 "document.getElementsByClassName('%s-checkbox').item(c).checked = this.checked\"></th>",
2389 px->id,
2390 px->id);
2391 else
2392 chunk_appendf(&trash, "<th rowspan=2></th>");
William Lallemand74c24fb2016-11-21 17:18:36 +01002393 }
2394
2395 chunk_appendf(&trash,
2396 "<th rowspan=2></th>"
2397 "<th colspan=3>Queue</th>"
2398 "<th colspan=3>Session rate</th><th colspan=6>Sessions</th>"
2399 "<th colspan=2>Bytes</th><th colspan=2>Denied</th>"
2400 "<th colspan=3>Errors</th><th colspan=2>Warnings</th>"
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02002401 "<th colspan=9>Server</th>");
2402
2403 if (appctx->ctx.stats.flags & STAT_SHMODULES) {
2404 // calculate the count of module for colspan attribute
2405 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
2406 ++stats_module_len;
2407 }
2408 chunk_appendf(&trash, "<th colspan=%d>Extra modules</th>",
2409 stats_module_len);
2410 }
2411
2412 chunk_appendf(&trash,
William Lallemand74c24fb2016-11-21 17:18:36 +01002413 "</tr>\n"
2414 "<tr class=\"titre\">"
2415 "<th>Cur</th><th>Max</th><th>Limit</th>"
2416 "<th>Cur</th><th>Max</th><th>Limit</th><th>Cur</th><th>Max</th>"
2417 "<th>Limit</th><th>Total</th><th>LbTot</th><th>Last</th><th>In</th><th>Out</th>"
2418 "<th>Req</th><th>Resp</th><th>Req</th><th>Conn</th>"
2419 "<th>Resp</th><th>Retr</th><th>Redis</th>"
2420 "<th>Status</th><th>LastChk</th><th>Wght</th><th>Act</th>"
2421 "<th>Bck</th><th>Chk</th><th>Dwn</th><th>Dwntme</th>"
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02002422 "<th>Thrtle</th>\n");
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02002423
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02002424 if (appctx->ctx.stats.flags & STAT_SHMODULES) {
2425 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
2426 chunk_appendf(&trash, "<th>%s</th>", mod->name);
2427 }
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02002428 }
2429
2430 chunk_appendf(&trash, "</tr>");
William Lallemand74c24fb2016-11-21 17:18:36 +01002431}
2432
2433/* Dumps the HTML table trailer for proxy <px> to the trash for and uses the state from
2434 * stream interface <si>. The caller is responsible for clearing the trash if needed.
2435 */
2436static void stats_dump_html_px_end(struct stream_interface *si, struct proxy *px)
2437{
2438 struct appctx *appctx = __objt_appctx(si->end);
2439 chunk_appendf(&trash, "</table>");
2440
2441 if ((px->cap & PR_CAP_BE) && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
2442 /* close the form used to enable/disable this proxy servers */
2443 chunk_appendf(&trash,
2444 "Choose the action to perform on the checked servers : "
2445 "<select name=action>"
2446 "<option value=\"\"></option>"
2447 "<option value=\"ready\">Set state to READY</option>"
2448 "<option value=\"drain\">Set state to DRAIN</option>"
2449 "<option value=\"maint\">Set state to MAINT</option>"
2450 "<option value=\"dhlth\">Health: disable checks</option>"
2451 "<option value=\"ehlth\">Health: enable checks</option>"
2452 "<option value=\"hrunn\">Health: force UP</option>"
2453 "<option value=\"hnolb\">Health: force NOLB</option>"
2454 "<option value=\"hdown\">Health: force DOWN</option>"
2455 "<option value=\"dagent\">Agent: disable checks</option>"
2456 "<option value=\"eagent\">Agent: enable checks</option>"
2457 "<option value=\"arunn\">Agent: force UP</option>"
2458 "<option value=\"adown\">Agent: force DOWN</option>"
2459 "<option value=\"shutdown\">Kill Sessions</option>"
2460 "</select>"
2461 "<input type=\"hidden\" name=\"b\" value=\"#%d\">"
2462 "&nbsp;<input type=\"submit\" value=\"Apply\">"
2463 "</form>",
2464 px->uuid);
2465 }
2466
2467 chunk_appendf(&trash, "<p>\n");
2468}
2469
2470/*
2471 * Dumps statistics for a proxy. The output is sent to the stream interface's
2472 * input buffer. Returns 0 if it had to stop dumping data because of lack of
2473 * buffer space, or non-zero if everything completed. This function is used
2474 * both by the CLI and the HTTP entry points, and is able to dump the output
2475 * in HTML or CSV formats. If the later, <uri> must be NULL.
2476 */
Christopher Fauletef779222018-10-31 08:47:01 +01002477int stats_dump_proxy_to_buffer(struct stream_interface *si, struct htx *htx,
2478 struct proxy *px, struct uri_auth *uri)
William Lallemand74c24fb2016-11-21 17:18:36 +01002479{
2480 struct appctx *appctx = __objt_appctx(si->end);
2481 struct stream *s = si_strm(si);
2482 struct channel *rep = si_ic(si);
2483 struct server *sv, *svs; /* server and server-state, server-state=server or server->track */
2484 struct listener *l;
William Lallemand74c24fb2016-11-21 17:18:36 +01002485
2486 chunk_reset(&trash);
2487
2488 switch (appctx->ctx.stats.px_st) {
2489 case STAT_PX_ST_INIT:
2490 /* we are on a new proxy */
2491 if (uri && uri->scope) {
2492 /* we have a limited scope, we have to check the proxy name */
2493 struct stat_scope *scope;
2494 int len;
2495
2496 len = strlen(px->id);
2497 scope = uri->scope;
2498
2499 while (scope) {
2500 /* match exact proxy name */
2501 if (scope->px_len == len && !memcmp(px->id, scope->px_id, len))
2502 break;
2503
2504 /* match '.' which means 'self' proxy */
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01002505 if (strcmp(scope->px_id, ".") == 0 && px == s->be)
William Lallemand74c24fb2016-11-21 17:18:36 +01002506 break;
2507 scope = scope->next;
2508 }
2509
2510 /* proxy name not found : don't dump anything */
2511 if (scope == NULL)
2512 return 1;
2513 }
2514
2515 /* if the user has requested a limited output and the proxy
2516 * name does not match, skip it.
2517 */
Christopher Fauleted7a0662019-01-14 11:07:34 +01002518 if (appctx->ctx.stats.scope_len) {
2519 const char *scope_ptr = stats_scope_ptr(appctx, si);
2520
2521 if (strnistr(px->id, strlen(px->id), scope_ptr, appctx->ctx.stats.scope_len) == NULL)
2522 return 1;
2523 }
William Lallemand74c24fb2016-11-21 17:18:36 +01002524
2525 if ((appctx->ctx.stats.flags & STAT_BOUND) &&
2526 (appctx->ctx.stats.iid != -1) &&
2527 (px->uuid != appctx->ctx.stats.iid))
2528 return 1;
2529
2530 appctx->ctx.stats.px_st = STAT_PX_ST_TH;
2531 /* fall through */
2532
2533 case STAT_PX_ST_TH:
2534 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreau676c29e2019-10-09 10:50:01 +02002535 stats_dump_html_px_hdr(si, px);
Christopher Fauletef779222018-10-31 08:47:01 +01002536 if (!stats_putchk(rep, htx, &trash))
2537 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002538 }
2539
2540 appctx->ctx.stats.px_st = STAT_PX_ST_FE;
2541 /* fall through */
2542
2543 case STAT_PX_ST_FE:
2544 /* print the frontend */
2545 if (stats_dump_fe_stats(si, px)) {
Christopher Fauletef779222018-10-31 08:47:01 +01002546 if (!stats_putchk(rep, htx, &trash))
2547 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002548 }
2549
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +02002550 appctx->ctx.stats.obj2 = px->conf.listeners.n;
William Lallemand74c24fb2016-11-21 17:18:36 +01002551 appctx->ctx.stats.px_st = STAT_PX_ST_LI;
2552 /* fall through */
2553
2554 case STAT_PX_ST_LI:
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +02002555 /* obj2 points to listeners list as initialized above */
2556 for (; appctx->ctx.stats.obj2 != &px->conf.listeners; appctx->ctx.stats.obj2 = l->by_fe.n) {
Christopher Fauletef779222018-10-31 08:47:01 +01002557 if (htx) {
2558 if (htx_almost_full(htx))
2559 goto full;
2560 }
2561 else {
2562 if (buffer_almost_full(&rep->buf))
2563 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002564 }
2565
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +02002566 l = LIST_ELEM(appctx->ctx.stats.obj2, struct listener *, by_fe);
William Lallemand74c24fb2016-11-21 17:18:36 +01002567 if (!l->counters)
2568 continue;
2569
2570 if (appctx->ctx.stats.flags & STAT_BOUND) {
2571 if (!(appctx->ctx.stats.type & (1 << STATS_TYPE_SO)))
2572 break;
2573
2574 if (appctx->ctx.stats.sid != -1 && l->luid != appctx->ctx.stats.sid)
2575 continue;
2576 }
2577
2578 /* print the frontend */
Willy Tarreau43241ff2019-10-09 11:27:51 +02002579 if (stats_dump_li_stats(si, px, l)) {
Christopher Fauletef779222018-10-31 08:47:01 +01002580 if (!stats_putchk(rep, htx, &trash))
2581 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002582 }
2583 }
2584
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +02002585 appctx->ctx.stats.obj2 = px->srv; /* may be NULL */
William Lallemand74c24fb2016-11-21 17:18:36 +01002586 appctx->ctx.stats.px_st = STAT_PX_ST_SV;
2587 /* fall through */
2588
2589 case STAT_PX_ST_SV:
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +02002590 /* obj2 points to servers list as initialized above */
2591 for (; appctx->ctx.stats.obj2 != NULL; appctx->ctx.stats.obj2 = sv->next) {
Christopher Fauletef779222018-10-31 08:47:01 +01002592 if (htx) {
2593 if (htx_almost_full(htx))
2594 goto full;
2595 }
2596 else {
2597 if (buffer_almost_full(&rep->buf))
2598 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002599 }
2600
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +02002601 sv = appctx->ctx.stats.obj2;
William Lallemand74c24fb2016-11-21 17:18:36 +01002602
2603 if (appctx->ctx.stats.flags & STAT_BOUND) {
2604 if (!(appctx->ctx.stats.type & (1 << STATS_TYPE_SV)))
2605 break;
2606
2607 if (appctx->ctx.stats.sid != -1 && sv->puid != appctx->ctx.stats.sid)
2608 continue;
2609 }
2610
Willy Tarreau3e320362020-10-23 17:28:57 +02002611 /* do not report disabled servers */
2612 if (appctx->ctx.stats.flags & STAT_HIDE_MAINT &&
2613 sv->cur_admin & SRV_ADMF_MAINT) {
2614 continue;
2615 }
2616
William Lallemand74c24fb2016-11-21 17:18:36 +01002617 svs = sv;
2618 while (svs->track)
2619 svs = svs->track;
2620
2621 /* do not report servers which are DOWN and not changing state */
2622 if ((appctx->ctx.stats.flags & STAT_HIDE_DOWN) &&
Emeric Brun52a91d32017-08-31 14:41:55 +02002623 ((sv->cur_admin & SRV_ADMF_MAINT) || /* server is in maintenance */
2624 (sv->cur_state == SRV_ST_STOPPED && /* server is down */
William Lallemand74c24fb2016-11-21 17:18:36 +01002625 (!((svs->agent.state | svs->check.state) & CHK_ST_ENABLED) ||
2626 ((svs->agent.state & CHK_ST_ENABLED) && !svs->agent.health) ||
2627 ((svs->check.state & CHK_ST_ENABLED) && !svs->check.health))))) {
2628 continue;
2629 }
2630
Willy Tarreau43241ff2019-10-09 11:27:51 +02002631 if (stats_dump_sv_stats(si, px, sv)) {
Christopher Fauletef779222018-10-31 08:47:01 +01002632 if (!stats_putchk(rep, htx, &trash))
2633 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002634 }
2635 } /* for sv */
2636
2637 appctx->ctx.stats.px_st = STAT_PX_ST_BE;
2638 /* fall through */
2639
2640 case STAT_PX_ST_BE:
2641 /* print the backend */
Willy Tarreau43241ff2019-10-09 11:27:51 +02002642 if (stats_dump_be_stats(si, px)) {
Christopher Fauletef779222018-10-31 08:47:01 +01002643 if (!stats_putchk(rep, htx, &trash))
2644 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002645 }
2646
2647 appctx->ctx.stats.px_st = STAT_PX_ST_END;
2648 /* fall through */
2649
2650 case STAT_PX_ST_END:
2651 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
2652 stats_dump_html_px_end(si, px);
Christopher Fauletef779222018-10-31 08:47:01 +01002653 if (!stats_putchk(rep, htx, &trash))
2654 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002655 }
2656
2657 appctx->ctx.stats.px_st = STAT_PX_ST_FIN;
2658 /* fall through */
2659
2660 case STAT_PX_ST_FIN:
2661 return 1;
2662
2663 default:
2664 /* unknown state, we should put an abort() here ! */
2665 return 1;
2666 }
Christopher Fauletef779222018-10-31 08:47:01 +01002667
2668 full:
2669 si_rx_room_blk(si);
2670 return 0;
William Lallemand74c24fb2016-11-21 17:18:36 +01002671}
2672
2673/* Dumps the HTTP stats head block to the trash for and uses the per-uri
2674 * parameters <uri>. The caller is responsible for clearing the trash if needed.
2675 */
Willy Tarreau676c29e2019-10-09 10:50:01 +02002676static void stats_dump_html_head(struct appctx *appctx, struct uri_auth *uri)
William Lallemand74c24fb2016-11-21 17:18:36 +01002677{
2678 /* WARNING! This must fit in the first buffer !!! */
2679 chunk_appendf(&trash,
2680 "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"\n"
2681 "\"http://www.w3.org/TR/html4/loose.dtd\">\n"
2682 "<html><head><title>Statistics Report for " PRODUCT_NAME "%s%s</title>\n"
zurikus6d599932020-08-18 11:16:05 +03002683 "<link rel=\"icon\" href=\"data:,\">\n"
William Lallemand74c24fb2016-11-21 17:18:36 +01002684 "<meta http-equiv=\"content-type\" content=\"text/html; charset=iso-8859-1\">\n"
2685 "<style type=\"text/css\"><!--\n"
2686 "body {"
2687 " font-family: arial, helvetica, sans-serif;"
2688 " font-size: 12px;"
2689 " font-weight: normal;"
2690 " color: black;"
2691 " background: white;"
2692 "}\n"
2693 "th,td {"
2694 " font-size: 10px;"
2695 "}\n"
2696 "h1 {"
2697 " font-size: x-large;"
2698 " margin-bottom: 0.5em;"
2699 "}\n"
2700 "h2 {"
2701 " font-family: helvetica, arial;"
2702 " font-size: x-large;"
2703 " font-weight: bold;"
2704 " font-style: italic;"
2705 " color: #6020a0;"
2706 " margin-top: 0em;"
2707 " margin-bottom: 0em;"
2708 "}\n"
2709 "h3 {"
2710 " font-family: helvetica, arial;"
2711 " font-size: 16px;"
2712 " font-weight: bold;"
2713 " color: #b00040;"
2714 " background: #e8e8d0;"
2715 " margin-top: 0em;"
2716 " margin-bottom: 0em;"
2717 "}\n"
2718 "li {"
2719 " margin-top: 0.25em;"
2720 " margin-right: 2em;"
2721 "}\n"
2722 ".hr {margin-top: 0.25em;"
2723 " border-color: black;"
2724 " border-bottom-style: solid;"
2725 "}\n"
2726 ".titre {background: #20D0D0;color: #000000; font-weight: bold; text-align: center;}\n"
2727 ".total {background: #20D0D0;color: #ffff80;}\n"
2728 ".frontend {background: #e8e8d0;}\n"
2729 ".socket {background: #d0d0d0;}\n"
2730 ".backend {background: #e8e8d0;}\n"
2731 ".active_down {background: #ff9090;}\n"
2732 ".active_going_up {background: #ffd020;}\n"
2733 ".active_going_down {background: #ffffa0;}\n"
2734 ".active_up {background: #c0ffc0;}\n"
2735 ".active_nolb {background: #20a0ff;}\n"
2736 ".active_draining {background: #20a0FF;}\n"
2737 ".active_no_check {background: #e0e0e0;}\n"
2738 ".backup_down {background: #ff9090;}\n"
2739 ".backup_going_up {background: #ff80ff;}\n"
2740 ".backup_going_down {background: #c060ff;}\n"
2741 ".backup_up {background: #b0d0ff;}\n"
2742 ".backup_nolb {background: #90b0e0;}\n"
2743 ".backup_draining {background: #cc9900;}\n"
2744 ".backup_no_check {background: #e0e0e0;}\n"
2745 ".maintain {background: #c07820;}\n"
2746 ".rls {letter-spacing: 0.2em; margin-right: 1px;}\n" /* right letter spacing (used for grouping digits) */
2747 "\n"
2748 "a.px:link {color: #ffff40; text-decoration: none;}"
2749 "a.px:visited {color: #ffff40; text-decoration: none;}"
2750 "a.px:hover {color: #ffffff; text-decoration: none;}"
2751 "a.lfsb:link {color: #000000; text-decoration: none;}"
2752 "a.lfsb:visited {color: #000000; text-decoration: none;}"
2753 "a.lfsb:hover {color: #505050; text-decoration: none;}"
2754 "\n"
2755 "table.tbl { border-collapse: collapse; border-style: none;}\n"
2756 "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"
2757 "table.tbl td.ac { text-align: center;}\n"
2758 "table.tbl th { border-width: 1px; border-style: solid solid solid solid; border-color: gray;}\n"
2759 "table.tbl th.pxname { background: #b00040; color: #ffff40; font-weight: bold; border-style: solid solid none solid; padding: 2px 3px; white-space: nowrap;}\n"
2760 "table.tbl th.empty { border-style: none; empty-cells: hide; background: white;}\n"
2761 "table.tbl th.desc { background: white; border-style: solid solid none solid; text-align: left; padding: 2px 3px;}\n"
2762 "\n"
2763 "table.lgd { border-collapse: collapse; border-width: 1px; border-style: none none none solid; border-color: black;}\n"
2764 "table.lgd td { border-width: 1px; border-style: solid solid solid solid; border-color: gray; padding: 2px;}\n"
2765 "table.lgd td.noborder { border-style: none; padding: 2px; white-space: nowrap;}\n"
2766 "table.det { border-collapse: collapse; border-style: none; }\n"
2767 "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"
2768 "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"
2769 "u {text-decoration:none; border-bottom: 1px dotted black;}\n"
2770 "div.tips {\n"
2771 " display:block;\n"
2772 " visibility:hidden;\n"
2773 " z-index:2147483647;\n"
2774 " position:absolute;\n"
2775 " padding:2px 4px 3px;\n"
2776 " background:#f0f060; color:#000000;\n"
2777 " border:1px solid #7040c0;\n"
2778 " white-space:nowrap;\n"
2779 " font-style:normal;font-size:11px;font-weight:normal;\n"
2780 " -moz-border-radius:3px;-webkit-border-radius:3px;border-radius:3px;\n"
2781 " -moz-box-shadow:gray 2px 2px 3px;-webkit-box-shadow:gray 2px 2px 3px;box-shadow:gray 2px 2px 3px;\n"
2782 "}\n"
2783 "u:hover div.tips {visibility:visible;}\n"
2784 "-->\n"
2785 "</style></head>\n",
Willy Tarreau676c29e2019-10-09 10:50:01 +02002786 (appctx->ctx.stats.flags & STAT_SHNODE) ? " on " : "",
Willy Tarreau027d2062020-01-23 11:47:13 +01002787 (appctx->ctx.stats.flags & STAT_SHNODE) ? (uri && uri->node ? uri->node : global.node) : ""
William Lallemand74c24fb2016-11-21 17:18:36 +01002788 );
2789}
2790
2791/* Dumps the HTML stats information block to the trash for and uses the state from
2792 * stream interface <si> and per-uri parameters <uri>. The caller is responsible
2793 * for clearing the trash if needed.
2794 */
2795static void stats_dump_html_info(struct stream_interface *si, struct uri_auth *uri)
2796{
2797 struct appctx *appctx = __objt_appctx(si->end);
2798 unsigned int up = (now.tv_sec - start_date.tv_sec);
2799 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
Christopher Fauleted7a0662019-01-14 11:07:34 +01002800 const char *scope_ptr = stats_scope_ptr(appctx, si);
Willy Tarreau1713c032019-05-23 12:23:55 +02002801 unsigned long long bps = (unsigned long long)read_freq_ctr(&global.out_32bps) * 32;
2802
2803 /* Turn the bytes per second to bits per second and take care of the
2804 * usual ethernet overhead in order to help figure how far we are from
2805 * interface saturation since it's the only case which usually matters.
2806 * For this we count the total size of an Ethernet frame on the wire
2807 * including preamble and IFG (1538) for the largest TCP segment it
2808 * transports (1448 with TCP timestamps). This is not valid for smaller
2809 * packets (under-estimated), but it gives a reasonably accurate
2810 * estimation of how far we are from uplink saturation.
2811 */
2812 bps = bps * 8 * 1538 / 1448;
William Lallemand74c24fb2016-11-21 17:18:36 +01002813
2814 /* WARNING! this has to fit the first packet too.
2815 * We are around 3.5 kB, add adding entries will
2816 * become tricky if we want to support 4kB buffers !
2817 */
2818 chunk_appendf(&trash,
2819 "<body><h1><a href=\"" PRODUCT_URL "\" style=\"text-decoration: none;\">"
2820 PRODUCT_NAME "%s</a></h1>\n"
2821 "<h2>Statistics Report for pid %d%s%s%s%s</h2>\n"
2822 "<hr width=\"100%%\" class=\"hr\">\n"
2823 "<h3>&gt; General process information</h3>\n"
2824 "<table border=0><tr><td align=\"left\" nowrap width=\"1%%\">\n"
Yves Lafon95317282018-02-26 11:10:37 +01002825 "<p><b>pid = </b> %d (process #%d, nbproc = %d, nbthread = %d)<br>\n"
William Lallemand74c24fb2016-11-21 17:18:36 +01002826 "<b>uptime = </b> %dd %dh%02dm%02ds<br>\n"
2827 "<b>system limits:</b> memmax = %s%s; ulimit-n = %d<br>\n"
2828 "<b>maxsock = </b> %d; <b>maxconn = </b> %d; <b>maxpipes = </b> %d<br>\n"
Willy Tarreau1713c032019-05-23 12:23:55 +02002829 "current conns = %d; current pipes = %d/%d; conn rate = %d/sec; bit rate = %.3f %cbps<br>\n"
William Lallemand74c24fb2016-11-21 17:18:36 +01002830 "Running tasks: %d/%d; idle = %d %%<br>\n"
2831 "</td><td align=\"center\" nowrap>\n"
2832 "<table class=\"lgd\"><tr>\n"
2833 "<td class=\"active_up\">&nbsp;</td><td class=\"noborder\">active UP </td>"
2834 "<td class=\"backup_up\">&nbsp;</td><td class=\"noborder\">backup UP </td>"
2835 "</tr><tr>\n"
2836 "<td class=\"active_going_down\"></td><td class=\"noborder\">active UP, going down </td>"
2837 "<td class=\"backup_going_down\"></td><td class=\"noborder\">backup UP, going down </td>"
2838 "</tr><tr>\n"
2839 "<td class=\"active_going_up\"></td><td class=\"noborder\">active DOWN, going up </td>"
2840 "<td class=\"backup_going_up\"></td><td class=\"noborder\">backup DOWN, going up </td>"
2841 "</tr><tr>\n"
2842 "<td class=\"active_down\"></td><td class=\"noborder\">active or backup DOWN &nbsp;</td>"
2843 "<td class=\"active_no_check\"></td><td class=\"noborder\">not checked </td>"
2844 "</tr><tr>\n"
2845 "<td class=\"maintain\"></td><td class=\"noborder\" colspan=\"3\">active or backup DOWN for maintenance (MAINT) &nbsp;</td>"
2846 "</tr><tr>\n"
2847 "<td class=\"active_draining\"></td><td class=\"noborder\" colspan=\"3\">active or backup SOFT STOPPED for maintenance &nbsp;</td>"
2848 "</tr></table>\n"
2849 "Note: \"NOLB\"/\"DRAIN\" = UP with load-balancing disabled."
2850 "</td>"
2851 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
2852 "<b>Display option:</b><ul style=\"margin-top: 0.25em;\">"
2853 "",
Willy Tarreau676c29e2019-10-09 10:50:01 +02002854 (appctx->ctx.stats.flags & STAT_HIDEVER) ? "" : (stats_version_string),
2855 pid, (appctx->ctx.stats.flags & STAT_SHNODE) ? " on " : "",
2856 (appctx->ctx.stats.flags & STAT_SHNODE) ? (uri->node ? uri->node : global.node) : "",
2857 (appctx->ctx.stats.flags & STAT_SHDESC) ? ": " : "",
2858 (appctx->ctx.stats.flags & STAT_SHDESC) ? (uri->desc ? uri->desc : global.desc) : "",
Yves Lafon95317282018-02-26 11:10:37 +01002859 pid, relative_pid, global.nbproc, global.nbthread,
William Lallemand74c24fb2016-11-21 17:18:36 +01002860 up / 86400, (up % 86400) / 3600,
2861 (up % 3600) / 60, (up % 60),
2862 global.rlimit_memmax ? ultoa(global.rlimit_memmax) : "unlimited",
2863 global.rlimit_memmax ? " MB" : "",
2864 global.rlimit_nofile,
2865 global.maxsock, global.maxconn, global.maxpipes,
2866 actconn, pipes_used, pipes_used+pipes_free, read_freq_ctr(&global.conn_per_sec),
Willy Tarreau1713c032019-05-23 12:23:55 +02002867 bps >= 1000000000UL ? (bps / 1000000000.0) : bps >= 1000000UL ? (bps / 1000000.0) : (bps / 1000.0),
2868 bps >= 1000000000UL ? 'G' : bps >= 1000000UL ? 'M' : 'k',
Willy Tarreau81036f22019-05-20 19:24:50 +02002869 tasks_run_queue_cur, nb_tasks_cur, ti->idle_pct
William Lallemand74c24fb2016-11-21 17:18:36 +01002870 );
2871
2872 /* scope_txt = search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
Christopher Fauleted7a0662019-01-14 11:07:34 +01002873 memcpy(scope_txt, scope_ptr, appctx->ctx.stats.scope_len);
William Lallemand74c24fb2016-11-21 17:18:36 +01002874 scope_txt[appctx->ctx.stats.scope_len] = '\0';
2875
2876 chunk_appendf(&trash,
2877 "<li><form method=\"GET\">Scope : <input value=\"%s\" name=\"" STAT_SCOPE_INPUT_NAME "\" size=\"8\" maxlength=\"%d\" tabindex=\"1\"/></form>\n",
2878 (appctx->ctx.stats.scope_len > 0) ? scope_txt : "",
2879 STAT_SCOPE_TXT_MAXLEN);
2880
2881 /* scope_txt = search pattern + search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
2882 scope_txt[0] = 0;
2883 if (appctx->ctx.stats.scope_len) {
2884 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Christopher Fauleted7a0662019-01-14 11:07:34 +01002885 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), scope_ptr, appctx->ctx.stats.scope_len);
William Lallemand74c24fb2016-11-21 17:18:36 +01002886 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
2887 }
2888
2889 if (appctx->ctx.stats.flags & STAT_HIDE_DOWN)
2890 chunk_appendf(&trash,
2891 "<li><a href=\"%s%s%s%s\">Show all servers</a><br>\n",
2892 uri->uri_prefix,
2893 "",
2894 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2895 scope_txt);
2896 else
2897 chunk_appendf(&trash,
2898 "<li><a href=\"%s%s%s%s\">Hide 'DOWN' servers</a><br>\n",
2899 uri->uri_prefix,
2900 ";up",
2901 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2902 scope_txt);
2903
2904 if (uri->refresh > 0) {
2905 if (appctx->ctx.stats.flags & STAT_NO_REFRESH)
2906 chunk_appendf(&trash,
2907 "<li><a href=\"%s%s%s%s\">Enable refresh</a><br>\n",
2908 uri->uri_prefix,
2909 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2910 "",
2911 scope_txt);
2912 else
2913 chunk_appendf(&trash,
2914 "<li><a href=\"%s%s%s%s\">Disable refresh</a><br>\n",
2915 uri->uri_prefix,
2916 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2917 ";norefresh",
2918 scope_txt);
2919 }
2920
2921 chunk_appendf(&trash,
2922 "<li><a href=\"%s%s%s%s\">Refresh now</a><br>\n",
2923 uri->uri_prefix,
2924 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2925 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2926 scope_txt);
2927
2928 chunk_appendf(&trash,
2929 "<li><a href=\"%s;csv%s%s\">CSV export</a><br>\n",
2930 uri->uri_prefix,
2931 (uri->refresh > 0) ? ";norefresh" : "",
2932 scope_txt);
2933
2934 chunk_appendf(&trash,
Christopher Faulet6338a082019-09-09 15:50:54 +02002935 "<li><a href=\"%s;json%s%s\">JSON export</a> (<a href=\"%s;json-schema\">schema</a>)<br>\n",
2936 uri->uri_prefix,
2937 (uri->refresh > 0) ? ";norefresh" : "",
2938 scope_txt, uri->uri_prefix);
2939
2940 chunk_appendf(&trash,
William Lallemand74c24fb2016-11-21 17:18:36 +01002941 "</ul></td>"
2942 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
2943 "<b>External resources:</b><ul style=\"margin-top: 0.25em;\">\n"
2944 "<li><a href=\"" PRODUCT_URL "\">Primary site</a><br>\n"
2945 "<li><a href=\"" PRODUCT_URL_UPD "\">Updates (v" PRODUCT_BRANCH ")</a><br>\n"
2946 "<li><a href=\"" PRODUCT_URL_DOC "\">Online manual</a><br>\n"
2947 "</ul>"
2948 "</td>"
2949 "</tr></table>\n"
2950 ""
2951 );
2952
2953 if (appctx->ctx.stats.st_code) {
2954 switch (appctx->ctx.stats.st_code) {
2955 case STAT_STATUS_DONE:
2956 chunk_appendf(&trash,
2957 "<p><div class=active_up>"
2958 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2959 "Action processed successfully."
2960 "</div>\n", uri->uri_prefix,
2961 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2962 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2963 scope_txt);
2964 break;
2965 case STAT_STATUS_NONE:
2966 chunk_appendf(&trash,
2967 "<p><div class=active_going_down>"
2968 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2969 "Nothing has changed."
2970 "</div>\n", uri->uri_prefix,
2971 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2972 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2973 scope_txt);
2974 break;
2975 case STAT_STATUS_PART:
2976 chunk_appendf(&trash,
2977 "<p><div class=active_going_down>"
2978 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2979 "Action partially processed.<br>"
2980 "Some server names are probably unknown or ambiguous (duplicated names in the backend)."
2981 "</div>\n", uri->uri_prefix,
2982 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2983 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2984 scope_txt);
2985 break;
2986 case STAT_STATUS_ERRP:
2987 chunk_appendf(&trash,
2988 "<p><div class=active_down>"
2989 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2990 "Action not processed because of invalid parameters."
2991 "<ul>"
2992 "<li>The action is maybe unknown.</li>"
Christopher Faulet2f9a41d2019-02-27 15:30:57 +01002993 "<li>Invalid key parameter (empty or too long).</li>"
William Lallemand74c24fb2016-11-21 17:18:36 +01002994 "<li>The backend name is probably unknown or ambiguous (duplicated names).</li>"
2995 "<li>Some server names are probably unknown or ambiguous (duplicated names in the backend).</li>"
2996 "</ul>"
2997 "</div>\n", uri->uri_prefix,
2998 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2999 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
3000 scope_txt);
3001 break;
3002 case STAT_STATUS_EXCD:
3003 chunk_appendf(&trash,
3004 "<p><div class=active_down>"
3005 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
3006 "<b>Action not processed : the buffer couldn't store all the data.<br>"
3007 "You should retry with less servers at a time.</b>"
3008 "</div>\n", uri->uri_prefix,
3009 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3010 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
3011 scope_txt);
3012 break;
3013 case STAT_STATUS_DENY:
3014 chunk_appendf(&trash,
3015 "<p><div class=active_down>"
3016 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
3017 "<b>Action denied.</b>"
3018 "</div>\n", uri->uri_prefix,
3019 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3020 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
3021 scope_txt);
3022 break;
Christopher Faulet3c2ecf72019-02-27 16:41:27 +01003023 case STAT_STATUS_IVAL:
3024 chunk_appendf(&trash,
3025 "<p><div class=active_down>"
3026 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
3027 "<b>Invalid requests (unsupported method or chunked encoded request).</b>"
3028 "</div>\n", uri->uri_prefix,
3029 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3030 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
3031 scope_txt);
3032 break;
William Lallemand74c24fb2016-11-21 17:18:36 +01003033 default:
3034 chunk_appendf(&trash,
3035 "<p><div class=active_no_check>"
3036 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
3037 "Unexpected result."
3038 "</div>\n", uri->uri_prefix,
3039 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3040 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
3041 scope_txt);
3042 }
3043 chunk_appendf(&trash, "<p>\n");
3044 }
3045}
3046
3047/* Dumps the HTML stats trailer block to the trash. The caller is responsible
3048 * for clearing the trash if needed.
3049 */
3050static void stats_dump_html_end()
3051{
3052 chunk_appendf(&trash, "</body></html>\n");
3053}
3054
Simon Horman05ee2132017-01-04 09:37:25 +01003055/* Dumps the stats JSON header to the trash buffer which. The caller is responsible
3056 * for clearing it if needed.
3057 */
3058static void stats_dump_json_header()
3059{
3060 chunk_strcat(&trash, "[");
3061}
3062
3063
3064/* Dumps the JSON stats trailer block to the trash. The caller is responsible
3065 * for clearing the trash if needed.
3066 */
3067static void stats_dump_json_end()
3068{
3069 chunk_strcat(&trash, "]");
3070}
3071
Amaury Denoyelle98b81cb2020-10-02 18:32:04 +02003072/* Uses <appctx.ctx.stats.obj1> as a pointer to the current proxy and <obj2> as
3073 * a pointer to the current server/listener.
3074 */
3075static int stats_dump_proxies(struct stream_interface *si,
3076 struct htx *htx,
3077 struct uri_auth *uri)
3078{
3079 struct appctx *appctx = __objt_appctx(si->end);
3080 struct channel *rep = si_ic(si);
3081 struct proxy *px;
3082
3083 /* dump proxies */
3084 while (appctx->ctx.stats.obj1) {
3085 if (htx) {
3086 if (htx_almost_full(htx))
3087 goto full;
3088 }
3089 else {
3090 if (buffer_almost_full(&rep->buf))
3091 goto full;
3092 }
3093
3094 px = appctx->ctx.stats.obj1;
3095 /* skip the disabled proxies, global frontend and non-networked ones */
Willy Tarreauc3914d42020-09-24 08:39:22 +02003096 if (!px->disabled && px->uuid > 0 && (px->cap & (PR_CAP_FE | PR_CAP_BE))) {
Amaury Denoyelle98b81cb2020-10-02 18:32:04 +02003097 if (stats_dump_proxy_to_buffer(si, htx, px, uri) == 0)
3098 return 0;
3099 }
3100
3101 appctx->ctx.stats.obj1 = px->next;
3102 appctx->ctx.stats.px_st = STAT_PX_ST_INIT;
3103 }
3104
3105 return 1;
3106
3107 full:
3108 si_rx_room_blk(si);
3109 return 0;
3110}
3111
William Lallemand74c24fb2016-11-21 17:18:36 +01003112/* This function dumps statistics onto the stream interface's read buffer in
3113 * either CSV or HTML format. <uri> contains some HTML-specific parameters that
3114 * are ignored for CSV format (hence <uri> may be NULL there). It returns 0 if
3115 * it had to stop writing data and an I/O is needed, 1 if the dump is finished
3116 * and the stream must be closed, or -1 in case of any error. This function is
3117 * used by both the CLI and the HTTP handlers.
3118 */
Christopher Fauletef779222018-10-31 08:47:01 +01003119static int stats_dump_stat_to_buffer(struct stream_interface *si, struct htx *htx,
3120 struct uri_auth *uri)
William Lallemand74c24fb2016-11-21 17:18:36 +01003121{
3122 struct appctx *appctx = __objt_appctx(si->end);
3123 struct channel *rep = si_ic(si);
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02003124 enum stats_domain domain = appctx->ctx.stats.domain;
William Lallemand74c24fb2016-11-21 17:18:36 +01003125
3126 chunk_reset(&trash);
3127
3128 switch (appctx->st2) {
3129 case STAT_ST_INIT:
3130 appctx->st2 = STAT_ST_HEAD; /* let's start producing data */
3131 /* fall through */
3132
3133 case STAT_ST_HEAD:
3134 if (appctx->ctx.stats.flags & STAT_FMT_HTML)
Willy Tarreau676c29e2019-10-09 10:50:01 +02003135 stats_dump_html_head(appctx, uri);
Christopher Faulet6338a082019-09-09 15:50:54 +02003136 else if (appctx->ctx.stats.flags & STAT_JSON_SCHM)
3137 stats_dump_json_schema(&trash);
Simon Horman05ee2132017-01-04 09:37:25 +01003138 else if (appctx->ctx.stats.flags & STAT_FMT_JSON)
Willy Tarreau9d7fb632017-04-11 07:53:04 +02003139 stats_dump_json_header();
William Lallemand74c24fb2016-11-21 17:18:36 +01003140 else if (!(appctx->ctx.stats.flags & STAT_FMT_TYPED))
Amaury Denoyelle50660a82020-10-05 11:49:39 +02003141 stats_dump_csv_header(appctx->ctx.stats.domain);
William Lallemand74c24fb2016-11-21 17:18:36 +01003142
Christopher Fauletef779222018-10-31 08:47:01 +01003143 if (!stats_putchk(rep, htx, &trash))
3144 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01003145
Christopher Faulet6338a082019-09-09 15:50:54 +02003146 if (appctx->ctx.stats.flags & STAT_JSON_SCHM) {
3147 appctx->st2 = STAT_ST_FIN;
3148 return 1;
3149 }
William Lallemand74c24fb2016-11-21 17:18:36 +01003150 appctx->st2 = STAT_ST_INFO;
3151 /* fall through */
3152
3153 case STAT_ST_INFO:
3154 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
3155 stats_dump_html_info(si, uri);
Christopher Fauletef779222018-10-31 08:47:01 +01003156 if (!stats_putchk(rep, htx, &trash))
3157 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01003158 }
3159
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02003160 if (domain == STATS_DOMAIN_PROXY)
3161 appctx->ctx.stats.obj1 = proxies_list;
3162
William Lallemand74c24fb2016-11-21 17:18:36 +01003163 appctx->ctx.stats.px_st = STAT_PX_ST_INIT;
3164 appctx->st2 = STAT_ST_LIST;
3165 /* fall through */
3166
3167 case STAT_ST_LIST:
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02003168 switch (domain) {
3169 case STATS_DOMAIN_DNS:
3170 if (!stats_dump_dns(si, stat_l[domain],
3171 stat_count[domain],
3172 &stats_module_list[domain])) {
3173 return 0;
3174 }
3175 break;
3176
3177 case STATS_DOMAIN_PROXY:
3178 default:
3179 /* dump proxies */
3180 if (!stats_dump_proxies(si, htx, uri))
3181 return 0;
3182 break;
3183 }
William Lallemand74c24fb2016-11-21 17:18:36 +01003184
3185 appctx->st2 = STAT_ST_END;
3186 /* fall through */
3187
3188 case STAT_ST_END:
Simon Horman05ee2132017-01-04 09:37:25 +01003189 if (appctx->ctx.stats.flags & (STAT_FMT_HTML|STAT_FMT_JSON)) {
3190 if (appctx->ctx.stats.flags & STAT_FMT_HTML)
3191 stats_dump_html_end();
3192 else
3193 stats_dump_json_end();
Christopher Fauletef779222018-10-31 08:47:01 +01003194 if (!stats_putchk(rep, htx, &trash))
3195 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01003196 }
3197
3198 appctx->st2 = STAT_ST_FIN;
3199 /* fall through */
3200
3201 case STAT_ST_FIN:
3202 return 1;
3203
3204 default:
3205 /* unknown state ! */
3206 appctx->st2 = STAT_ST_FIN;
3207 return -1;
3208 }
Christopher Fauletef779222018-10-31 08:47:01 +01003209
3210 full:
3211 si_rx_room_blk(si);
3212 return 0;
3213
William Lallemand74c24fb2016-11-21 17:18:36 +01003214}
3215
3216/* We reached the stats page through a POST request. The appctx is
3217 * expected to have already been allocated by the caller.
3218 * Parse the posted data and enable/disable servers if necessary.
3219 * Returns 1 if request was parsed or zero if it needs more data.
3220 */
3221static int stats_process_http_post(struct stream_interface *si)
3222{
3223 struct stream *s = si_strm(si);
3224 struct appctx *appctx = objt_appctx(si->end);
3225
3226 struct proxy *px = NULL;
3227 struct server *sv = NULL;
3228
3229 char key[LINESIZE];
3230 int action = ST_ADM_ACTION_NONE;
3231 int reprocess = 0;
3232
3233 int total_servers = 0;
3234 int altered_servers = 0;
3235
3236 char *first_param, *cur_param, *next_param, *end_params;
3237 char *st_cur_param = NULL;
3238 char *st_next_param = NULL;
3239
Christopher Fauleta3618372018-12-13 21:59:56 +01003240 struct buffer *temp = get_trash_chunk();
William Lallemand74c24fb2016-11-21 17:18:36 +01003241
Christopher Fauletb7f88902019-07-15 21:56:43 +02003242 struct htx *htx = htxbuf(&s->req.buf);
3243 struct htx_blk *blk;
Christopher Fauleta3618372018-12-13 21:59:56 +01003244
Christopher Fauletb7f88902019-07-15 21:56:43 +02003245 /* we need more data */
3246 if (s->txn->req.msg_state < HTTP_MSG_DONE) {
3247 /* check if we can receive more */
3248 if (htx_free_data_space(htx) <= global.tune.maxrewrite) {
3249 appctx->ctx.stats.st_code = STAT_STATUS_EXCD;
3250 goto out;
Christopher Fauleta3618372018-12-13 21:59:56 +01003251 }
Christopher Fauletb7f88902019-07-15 21:56:43 +02003252 goto wait;
3253 }
Christopher Fauleta3618372018-12-13 21:59:56 +01003254
Christopher Fauletb7f88902019-07-15 21:56:43 +02003255 /* The request was fully received. Copy data */
3256 blk = htx_get_head_blk(htx);
3257 while (blk) {
3258 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Fauleta3618372018-12-13 21:59:56 +01003259
Christopher Fauletb7f88902019-07-15 21:56:43 +02003260 if (type == HTX_BLK_EOM || type == HTX_BLK_TLR || type == HTX_BLK_EOT)
3261 break;
3262 if (type == HTX_BLK_DATA) {
3263 struct ist v = htx_get_blk_value(htx, blk);
Christopher Fauleta3618372018-12-13 21:59:56 +01003264
Christopher Fauletb7f88902019-07-15 21:56:43 +02003265 if (!chunk_memcat(temp, v.ptr, v.len)) {
Christopher Faulet2f9a41d2019-02-27 15:30:57 +01003266 appctx->ctx.stats.st_code = STAT_STATUS_EXCD;
3267 goto out;
3268 }
Christopher Fauleta3618372018-12-13 21:59:56 +01003269 }
Christopher Fauletb7f88902019-07-15 21:56:43 +02003270 blk = htx_get_next_blk(htx, blk);
William Lallemand74c24fb2016-11-21 17:18:36 +01003271 }
3272
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003273 first_param = temp->area;
Christopher Fauleta3618372018-12-13 21:59:56 +01003274 end_params = temp->area + temp->data;
William Lallemand74c24fb2016-11-21 17:18:36 +01003275 cur_param = next_param = end_params;
3276 *end_params = '\0';
3277
3278 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
3279
3280 /*
3281 * Parse the parameters in reverse order to only store the last value.
3282 * From the html form, the backend and the action are at the end.
3283 */
3284 while (cur_param > first_param) {
3285 char *value;
3286 int poffset, plen;
3287
3288 cur_param--;
3289
3290 if ((*cur_param == '&') || (cur_param == first_param)) {
3291 reprocess_servers:
3292 /* Parse the key */
3293 poffset = (cur_param != first_param ? 1 : 0);
3294 plen = next_param - cur_param + (cur_param == first_param ? 1 : 0);
3295 if ((plen > 0) && (plen <= sizeof(key))) {
3296 strncpy(key, cur_param + poffset, plen);
3297 key[plen - 1] = '\0';
3298 } else {
Christopher Faulet2f9a41d2019-02-27 15:30:57 +01003299 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
William Lallemand74c24fb2016-11-21 17:18:36 +01003300 goto out;
3301 }
3302
3303 /* Parse the value */
3304 value = key;
3305 while (*value != '\0' && *value != '=') {
3306 value++;
3307 }
3308 if (*value == '=') {
3309 /* Ok, a value is found, we can mark the end of the key */
3310 *value++ = '\0';
3311 }
Willy Tarreau62ba9ba2020-04-23 17:54:47 +02003312 if (url_decode(key, 1) < 0 || url_decode(value, 1) < 0)
William Lallemand74c24fb2016-11-21 17:18:36 +01003313 break;
3314
3315 /* Now we can check the key to see what to do */
3316 if (!px && (strcmp(key, "b") == 0)) {
3317 if ((px = proxy_be_by_name(value)) == NULL) {
3318 /* the backend name is unknown or ambiguous (duplicate names) */
3319 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
3320 goto out;
3321 }
3322 }
3323 else if (!action && (strcmp(key, "action") == 0)) {
3324 if (strcmp(value, "ready") == 0) {
3325 action = ST_ADM_ACTION_READY;
3326 }
3327 else if (strcmp(value, "drain") == 0) {
3328 action = ST_ADM_ACTION_DRAIN;
3329 }
3330 else if (strcmp(value, "maint") == 0) {
3331 action = ST_ADM_ACTION_MAINT;
3332 }
3333 else if (strcmp(value, "shutdown") == 0) {
3334 action = ST_ADM_ACTION_SHUTDOWN;
3335 }
3336 else if (strcmp(value, "dhlth") == 0) {
3337 action = ST_ADM_ACTION_DHLTH;
3338 }
3339 else if (strcmp(value, "ehlth") == 0) {
3340 action = ST_ADM_ACTION_EHLTH;
3341 }
3342 else if (strcmp(value, "hrunn") == 0) {
3343 action = ST_ADM_ACTION_HRUNN;
3344 }
3345 else if (strcmp(value, "hnolb") == 0) {
3346 action = ST_ADM_ACTION_HNOLB;
3347 }
3348 else if (strcmp(value, "hdown") == 0) {
3349 action = ST_ADM_ACTION_HDOWN;
3350 }
3351 else if (strcmp(value, "dagent") == 0) {
3352 action = ST_ADM_ACTION_DAGENT;
3353 }
3354 else if (strcmp(value, "eagent") == 0) {
3355 action = ST_ADM_ACTION_EAGENT;
3356 }
3357 else if (strcmp(value, "arunn") == 0) {
3358 action = ST_ADM_ACTION_ARUNN;
3359 }
3360 else if (strcmp(value, "adown") == 0) {
3361 action = ST_ADM_ACTION_ADOWN;
3362 }
3363 /* else these are the old supported methods */
3364 else if (strcmp(value, "disable") == 0) {
3365 action = ST_ADM_ACTION_DISABLE;
3366 }
3367 else if (strcmp(value, "enable") == 0) {
3368 action = ST_ADM_ACTION_ENABLE;
3369 }
3370 else if (strcmp(value, "stop") == 0) {
3371 action = ST_ADM_ACTION_STOP;
3372 }
3373 else if (strcmp(value, "start") == 0) {
3374 action = ST_ADM_ACTION_START;
3375 }
3376 else {
3377 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
3378 goto out;
3379 }
3380 }
3381 else if (strcmp(key, "s") == 0) {
3382 if (!(px && action)) {
3383 /*
3384 * Indicates that we'll need to reprocess the parameters
3385 * as soon as backend and action are known
3386 */
3387 if (!reprocess) {
3388 st_cur_param = cur_param;
3389 st_next_param = next_param;
3390 }
3391 reprocess = 1;
3392 }
3393 else if ((sv = findserver(px, value)) != NULL) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003394 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
William Lallemand74c24fb2016-11-21 17:18:36 +01003395 switch (action) {
3396 case ST_ADM_ACTION_DISABLE:
Emeric Brun52a91d32017-08-31 14:41:55 +02003397 if (!(sv->cur_admin & SRV_ADMF_FMAINT)) {
William Lallemand74c24fb2016-11-21 17:18:36 +01003398 altered_servers++;
3399 total_servers++;
3400 srv_set_admin_flag(sv, SRV_ADMF_FMAINT, "'disable' on stats page");
3401 }
3402 break;
3403 case ST_ADM_ACTION_ENABLE:
Emeric Brun52a91d32017-08-31 14:41:55 +02003404 if (sv->cur_admin & SRV_ADMF_FMAINT) {
William Lallemand74c24fb2016-11-21 17:18:36 +01003405 altered_servers++;
3406 total_servers++;
3407 srv_clr_admin_flag(sv, SRV_ADMF_FMAINT);
3408 }
3409 break;
3410 case ST_ADM_ACTION_STOP:
Emeric Brun52a91d32017-08-31 14:41:55 +02003411 if (!(sv->cur_admin & SRV_ADMF_FDRAIN)) {
William Lallemand74c24fb2016-11-21 17:18:36 +01003412 srv_set_admin_flag(sv, SRV_ADMF_FDRAIN, "'stop' on stats page");
3413 altered_servers++;
3414 total_servers++;
3415 }
3416 break;
3417 case ST_ADM_ACTION_START:
Emeric Brun52a91d32017-08-31 14:41:55 +02003418 if (sv->cur_admin & SRV_ADMF_FDRAIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +01003419 srv_clr_admin_flag(sv, SRV_ADMF_FDRAIN);
3420 altered_servers++;
3421 total_servers++;
3422 }
3423 break;
3424 case ST_ADM_ACTION_DHLTH:
3425 if (sv->check.state & CHK_ST_CONFIGURED) {
3426 sv->check.state &= ~CHK_ST_ENABLED;
3427 altered_servers++;
3428 total_servers++;
3429 }
3430 break;
3431 case ST_ADM_ACTION_EHLTH:
3432 if (sv->check.state & CHK_ST_CONFIGURED) {
3433 sv->check.state |= CHK_ST_ENABLED;
3434 altered_servers++;
3435 total_servers++;
3436 }
3437 break;
3438 case ST_ADM_ACTION_HRUNN:
3439 if (!(sv->track)) {
3440 sv->check.health = sv->check.rise + sv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02003441 srv_set_running(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01003442 altered_servers++;
3443 total_servers++;
3444 }
3445 break;
3446 case ST_ADM_ACTION_HNOLB:
3447 if (!(sv->track)) {
3448 sv->check.health = sv->check.rise + sv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02003449 srv_set_stopping(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01003450 altered_servers++;
3451 total_servers++;
3452 }
3453 break;
3454 case ST_ADM_ACTION_HDOWN:
3455 if (!(sv->track)) {
3456 sv->check.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02003457 srv_set_stopped(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01003458 altered_servers++;
3459 total_servers++;
3460 }
3461 break;
3462 case ST_ADM_ACTION_DAGENT:
3463 if (sv->agent.state & CHK_ST_CONFIGURED) {
3464 sv->agent.state &= ~CHK_ST_ENABLED;
3465 altered_servers++;
3466 total_servers++;
3467 }
3468 break;
3469 case ST_ADM_ACTION_EAGENT:
3470 if (sv->agent.state & CHK_ST_CONFIGURED) {
3471 sv->agent.state |= CHK_ST_ENABLED;
3472 altered_servers++;
3473 total_servers++;
3474 }
3475 break;
3476 case ST_ADM_ACTION_ARUNN:
3477 if (sv->agent.state & CHK_ST_ENABLED) {
3478 sv->agent.health = sv->agent.rise + sv->agent.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02003479 srv_set_running(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01003480 altered_servers++;
3481 total_servers++;
3482 }
3483 break;
3484 case ST_ADM_ACTION_ADOWN:
3485 if (sv->agent.state & CHK_ST_ENABLED) {
3486 sv->agent.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02003487 srv_set_stopped(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01003488 altered_servers++;
3489 total_servers++;
3490 }
3491 break;
3492 case ST_ADM_ACTION_READY:
3493 srv_adm_set_ready(sv);
3494 altered_servers++;
3495 total_servers++;
3496 break;
3497 case ST_ADM_ACTION_DRAIN:
3498 srv_adm_set_drain(sv);
3499 altered_servers++;
3500 total_servers++;
3501 break;
3502 case ST_ADM_ACTION_MAINT:
3503 srv_adm_set_maint(sv);
3504 altered_servers++;
3505 total_servers++;
3506 break;
3507 case ST_ADM_ACTION_SHUTDOWN:
Willy Tarreauc3914d42020-09-24 08:39:22 +02003508 if (!px->disabled) {
Willy Tarreauaf7ea812019-11-14 16:42:04 +01003509 srv_shutdown_streams(sv, SF_ERR_KILLED);
William Lallemand74c24fb2016-11-21 17:18:36 +01003510 altered_servers++;
3511 total_servers++;
3512 }
3513 break;
3514 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003515 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
William Lallemand74c24fb2016-11-21 17:18:36 +01003516 } else {
3517 /* the server name is unknown or ambiguous (duplicate names) */
3518 total_servers++;
3519 }
3520 }
3521 if (reprocess && px && action) {
3522 /* Now, we know the backend and the action chosen by the user.
3523 * We can safely restart from the first server parameter
3524 * to reprocess them
3525 */
3526 cur_param = st_cur_param;
3527 next_param = st_next_param;
3528 reprocess = 0;
3529 goto reprocess_servers;
3530 }
3531
3532 next_param = cur_param;
3533 }
3534 }
3535
3536 if (total_servers == 0) {
3537 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
3538 }
3539 else if (altered_servers == 0) {
3540 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
3541 }
3542 else if (altered_servers == total_servers) {
3543 appctx->ctx.stats.st_code = STAT_STATUS_DONE;
3544 }
3545 else {
3546 appctx->ctx.stats.st_code = STAT_STATUS_PART;
3547 }
3548 out:
3549 return 1;
Christopher Faulet2f9a41d2019-02-27 15:30:57 +01003550 wait:
3551 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
3552 return 0;
Christopher Fauletef779222018-10-31 08:47:01 +01003553}
3554
3555
Christopher Fauletb7f88902019-07-15 21:56:43 +02003556static int stats_send_http_headers(struct stream_interface *si, struct htx *htx)
Christopher Fauletef779222018-10-31 08:47:01 +01003557{
3558 struct stream *s = si_strm(si);
3559 struct uri_auth *uri = s->be->uri_auth;
3560 struct appctx *appctx = __objt_appctx(si->end);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003561 struct htx_sl *sl;
3562 unsigned int flags;
Christopher Fauletef779222018-10-31 08:47:01 +01003563
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003564 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);
3565 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), ist("200"), ist("OK"));
3566 if (!sl)
Christopher Fauletef779222018-10-31 08:47:01 +01003567 goto full;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003568 sl->info.res.status = 200;
Christopher Fauletef779222018-10-31 08:47:01 +01003569
Christopher Fauletb829f4c2019-03-29 16:13:55 +01003570 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")))
Christopher Fauletef779222018-10-31 08:47:01 +01003571 goto full;
3572 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
3573 if (!htx_add_header(htx, ist("Content-Type"), ist("text/html")))
3574 goto full;
3575 }
Christopher Faulet6338a082019-09-09 15:50:54 +02003576 else if (appctx->ctx.stats.flags & (STAT_FMT_JSON|STAT_JSON_SCHM)) {
3577 if (!htx_add_header(htx, ist("Content-Type"), ist("application/json")))
3578 goto full;
3579 }
Christopher Fauletef779222018-10-31 08:47:01 +01003580 else {
3581 if (!htx_add_header(htx, ist("Content-Type"), ist("text/plain")))
3582 goto full;
3583 }
3584
3585 if (uri->refresh > 0 && !(appctx->ctx.stats.flags & STAT_NO_REFRESH)) {
3586 const char *refresh = U2A(uri->refresh);
3587 if (!htx_add_header(htx, ist("Refresh"), ist2(refresh, strlen(refresh))))
3588 goto full;
3589 }
3590
3591 if (appctx->ctx.stats.flags & STAT_CHUNKED) {
3592 if (!htx_add_header(htx, ist("Transfer-Encoding"), ist("chunked")))
3593 goto full;
3594 }
3595
3596 if (!htx_add_endof(htx, HTX_BLK_EOH))
3597 goto full;
3598
Christopher Faulet1e2d6362019-02-27 16:28:48 +01003599 channel_add_input(&s->res, htx->data);
Christopher Fauletef779222018-10-31 08:47:01 +01003600 return 1;
3601
3602 full:
3603 htx_reset(htx);
3604 si_rx_room_blk(si);
3605 return 0;
William Lallemand74c24fb2016-11-21 17:18:36 +01003606}
3607
Christopher Fauletef779222018-10-31 08:47:01 +01003608
Christopher Fauletb7f88902019-07-15 21:56:43 +02003609static int stats_send_http_redirect(struct stream_interface *si, struct htx *htx)
Christopher Fauletef779222018-10-31 08:47:01 +01003610{
3611 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
3612 struct stream *s = si_strm(si);
3613 struct uri_auth *uri = s->be->uri_auth;
3614 struct appctx *appctx = __objt_appctx(si->end);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003615 struct htx_sl *sl;
3616 unsigned int flags;
Christopher Fauletef779222018-10-31 08:47:01 +01003617
3618 /* scope_txt = search pattern + search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
3619 scope_txt[0] = 0;
3620 if (appctx->ctx.stats.scope_len) {
Christopher Fauleted7a0662019-01-14 11:07:34 +01003621 const char *scope_ptr = stats_scope_ptr(appctx, si);
3622
Christopher Fauletef779222018-10-31 08:47:01 +01003623 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Christopher Fauleted7a0662019-01-14 11:07:34 +01003624 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), scope_ptr, appctx->ctx.stats.scope_len);
Christopher Fauletef779222018-10-31 08:47:01 +01003625 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
3626 }
3627
3628 /* We don't want to land on the posted stats page because a refresh will
3629 * repost the data. We don't want this to happen on accident so we redirect
3630 * the browse to the stats page with a GET.
3631 */
3632 chunk_printf(&trash, "%s;st=%s%s%s%s",
3633 uri->uri_prefix,
3634 ((appctx->ctx.stats.st_code > STAT_STATUS_INIT) &&
3635 (appctx->ctx.stats.st_code < STAT_STATUS_SIZE) &&
3636 stat_status_codes[appctx->ctx.stats.st_code]) ?
3637 stat_status_codes[appctx->ctx.stats.st_code] :
3638 stat_status_codes[STAT_STATUS_UNKN],
3639 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3640 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
3641 scope_txt);
3642
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003643 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_CHNK);
3644 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), ist("303"), ist("See Other"));
3645 if (!sl)
Christopher Fauletef779222018-10-31 08:47:01 +01003646 goto full;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003647 sl->info.res.status = 303;
Christopher Fauletef779222018-10-31 08:47:01 +01003648
3649 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")) ||
Christopher Fauletef779222018-10-31 08:47:01 +01003650 !htx_add_header(htx, ist("Content-Type"), ist("text/plain")) ||
3651 !htx_add_header(htx, ist("Content-Length"), ist("0")) ||
3652 !htx_add_header(htx, ist("Location"), ist2(trash.area, trash.data)))
3653 goto full;
3654
3655 if (!htx_add_endof(htx, HTX_BLK_EOH))
3656 goto full;
3657
Christopher Faulet1e2d6362019-02-27 16:28:48 +01003658 channel_add_input(&s->res, htx->data);
Christopher Fauletef779222018-10-31 08:47:01 +01003659 return 1;
3660
3661full:
3662 htx_reset(htx);
3663 si_rx_room_blk(si);
3664 return 0;
3665}
William Lallemand74c24fb2016-11-21 17:18:36 +01003666
Simon Horman05ee2132017-01-04 09:37:25 +01003667
William Lallemand74c24fb2016-11-21 17:18:36 +01003668/* This I/O handler runs as an applet embedded in a stream interface. It is
3669 * used to send HTTP stats over a TCP socket. The mechanism is very simple.
3670 * appctx->st0 contains the operation in progress (dump, done). The handler
3671 * automatically unregisters itself once transfer is complete.
3672 */
Christopher Fauletb7f88902019-07-15 21:56:43 +02003673static void http_stats_io_handler(struct appctx *appctx)
Christopher Fauletef779222018-10-31 08:47:01 +01003674{
3675 struct stream_interface *si = appctx->owner;
3676 struct stream *s = si_strm(si);
3677 struct channel *req = si_oc(si);
3678 struct channel *res = si_ic(si);
3679 struct htx *req_htx, *res_htx;
3680
Amaury Denoyelle072f97e2020-10-05 11:49:37 +02003681 /* only proxy stats are available via http */
3682 appctx->ctx.stats.domain = STATS_DOMAIN_PROXY;
3683
Christopher Fauletef779222018-10-31 08:47:01 +01003684 res_htx = htx_from_buf(&res->buf);
3685
3686 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
3687 goto out;
3688
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05003689 /* Check if the input buffer is available. */
Christopher Fauletef779222018-10-31 08:47:01 +01003690 if (!b_size(&res->buf)) {
3691 si_rx_room_blk(si);
3692 goto out;
3693 }
3694
3695 /* check that the output is not closed */
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003696 if (res->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_SHUTR))
3697 appctx->st0 = STAT_HTTP_END;
Christopher Fauletef779222018-10-31 08:47:01 +01003698
3699 /* all states are processed in sequence */
3700 if (appctx->st0 == STAT_HTTP_HEAD) {
Christopher Fauletb7f88902019-07-15 21:56:43 +02003701 if (stats_send_http_headers(si, res_htx)) {
Christopher Fauletef779222018-10-31 08:47:01 +01003702 if (s->txn->meth == HTTP_METH_HEAD)
3703 appctx->st0 = STAT_HTTP_DONE;
3704 else
3705 appctx->st0 = STAT_HTTP_DUMP;
3706 }
3707 }
3708
3709 if (appctx->st0 == STAT_HTTP_DUMP) {
3710 if (stats_dump_stat_to_buffer(si, res_htx, s->be->uri_auth))
3711 appctx->st0 = STAT_HTTP_DONE;
3712 }
3713
3714 if (appctx->st0 == STAT_HTTP_POST) {
3715 if (stats_process_http_post(si))
3716 appctx->st0 = STAT_HTTP_LAST;
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003717 else if (req->flags & CF_SHUTR)
Christopher Fauletef779222018-10-31 08:47:01 +01003718 appctx->st0 = STAT_HTTP_DONE;
3719 }
3720
3721 if (appctx->st0 == STAT_HTTP_LAST) {
Christopher Fauletb7f88902019-07-15 21:56:43 +02003722 if (stats_send_http_redirect(si, res_htx))
Christopher Fauletef779222018-10-31 08:47:01 +01003723 appctx->st0 = STAT_HTTP_DONE;
3724 }
3725
3726 if (appctx->st0 == STAT_HTTP_DONE) {
Christopher Faulet54b5e212019-06-04 10:08:28 +02003727 /* Don't add TLR because mux-h1 will take care of it */
Christopher Faulet810df062020-07-22 16:20:34 +02003728 res_htx->flags |= HTX_FL_EOI; /* no more data are expected. Only EOM remains to add now */
Christopher Fauletef779222018-10-31 08:47:01 +01003729 if (!htx_add_endof(res_htx, HTX_BLK_EOM)) {
3730 si_rx_room_blk(si);
3731 goto out;
3732 }
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003733 channel_add_input(&s->res, 1);
3734 appctx->st0 = STAT_HTTP_END;
Christopher Fauletef779222018-10-31 08:47:01 +01003735 }
3736
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003737 if (appctx->st0 == STAT_HTTP_END) {
3738 if (!(res->flags & CF_SHUTR)) {
Christopher Fauletef779222018-10-31 08:47:01 +01003739 res->flags |= CF_READ_NULL;
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003740 si_shutr(si);
3741 }
3742
3743 /* eat the whole request */
3744 if (co_data(req)) {
3745 req_htx = htx_from_buf(&req->buf);
3746 co_htx_skip(req, req_htx, co_data(req));
3747 htx_to_buf(req_htx, &req->buf);
Christopher Fauletef779222018-10-31 08:47:01 +01003748 }
3749 }
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003750
Christopher Fauletef779222018-10-31 08:47:01 +01003751 out:
3752 /* we have left the request in the buffer for the case where we
3753 * process a POST, and this automatically re-enables activity on
3754 * read. It's better to indicate that we want to stop reading when
3755 * we're sending, so that we know there's at most one direction
3756 * deciding to wake the applet up. It saves it from looping when
3757 * emitting large blocks into small TCP windows.
3758 */
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003759 htx_to_buf(res_htx, &res->buf);
3760 if (!channel_is_empty(res))
Christopher Fauletef779222018-10-31 08:47:01 +01003761 si_stop_get(si);
Christopher Fauletef779222018-10-31 08:47:01 +01003762}
3763
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003764/* Dump all fields from <info> into <out> using the "show info" format (name: value) */
Willy Tarreau83061a82018-07-13 11:56:34 +02003765static int stats_dump_info_fields(struct buffer *out,
Willy Tarreau43241ff2019-10-09 11:27:51 +02003766 const struct field *info, unsigned int flags)
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003767{
3768 int field;
3769
3770 for (field = 0; field < INF_TOTAL_FIELDS; field++) {
3771 if (!field_format(info, field))
3772 continue;
3773
Willy Tarreaueaa55372019-10-09 07:39:11 +02003774 if (!chunk_appendf(out, "%s: ", info_fields[field].name))
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003775 return 0;
3776 if (!stats_emit_raw_data_field(out, &info[field]))
3777 return 0;
Willy Tarreau6b19b142019-10-09 15:44:21 +02003778 if ((flags & STAT_SHOW_FDESC) && !chunk_appendf(out, ":\"%s\"", info_fields[field].desc))
3779 return 0;
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003780 if (!chunk_strcat(out, "\n"))
3781 return 0;
3782 }
3783 return 1;
3784}
3785
3786/* Dump all fields from <info> into <out> using the "show info typed" format */
Willy Tarreau83061a82018-07-13 11:56:34 +02003787static int stats_dump_typed_info_fields(struct buffer *out,
Willy Tarreau43241ff2019-10-09 11:27:51 +02003788 const struct field *info, unsigned int flags)
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003789{
3790 int field;
3791
3792 for (field = 0; field < INF_TOTAL_FIELDS; field++) {
3793 if (!field_format(info, field))
3794 continue;
3795
Willy Tarreaueaa55372019-10-09 07:39:11 +02003796 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 +01003797 return 0;
3798 if (!stats_emit_field_tags(out, &info[field], ':'))
3799 return 0;
3800 if (!stats_emit_typed_data_field(out, &info[field]))
3801 return 0;
Willy Tarreau6b19b142019-10-09 15:44:21 +02003802 if ((flags & STAT_SHOW_FDESC) && !chunk_appendf(out, ":\"%s\"", info_fields[field].desc))
3803 return 0;
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003804 if (!chunk_strcat(out, "\n"))
3805 return 0;
3806 }
3807 return 1;
3808}
3809
3810/* Fill <info> with HAProxy global info. <info> is preallocated
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05003811 * array of length <len>. The length of the array must be
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003812 * INF_TOTAL_FIELDS. If this length is less then this value, the
3813 * function returns 0, otherwise, it returns 1.
3814 */
3815int stats_fill_info(struct field *info, int len)
3816{
3817 unsigned int up = (now.tv_sec - start_date.tv_sec);
Willy Tarreau83061a82018-07-13 11:56:34 +02003818 struct buffer *out = get_trash_chunk();
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003819
3820#ifdef USE_OPENSSL
3821 int ssl_sess_rate = read_freq_ctr(&global.ssl_per_sec);
3822 int ssl_key_rate = read_freq_ctr(&global.ssl_fe_keys_per_sec);
3823 int ssl_reuse = 0;
3824
3825 if (ssl_key_rate < ssl_sess_rate) {
3826 /* count the ssl reuse ratio and avoid overflows in both directions */
3827 ssl_reuse = 100 - (100 * ssl_key_rate + (ssl_sess_rate - 1) / 2) / ssl_sess_rate;
3828 }
3829#endif
3830
3831 if (len < INF_TOTAL_FIELDS)
3832 return 0;
3833
3834 chunk_reset(out);
3835 memset(info, 0, sizeof(*info) * len);
3836
3837 info[INF_NAME] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, PRODUCT_NAME);
Willy Tarreau909b9d82019-01-04 18:20:32 +01003838 info[INF_VERSION] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, haproxy_version);
Adis Nezirovicb62b78b2021-01-15 13:12:33 +01003839 info[INF_BUILD_INFO] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, haproxy_version);
Willy Tarreau909b9d82019-01-04 18:20:32 +01003840 info[INF_RELEASE_DATE] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, haproxy_date);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003841
Yves Lafon95317282018-02-26 11:10:37 +01003842 info[INF_NBTHREAD] = mkf_u32(FO_CONFIG|FS_SERVICE, global.nbthread);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003843 info[INF_NBPROC] = mkf_u32(FO_CONFIG|FS_SERVICE, global.nbproc);
3844 info[INF_PROCESS_NUM] = mkf_u32(FO_KEY, relative_pid);
3845 info[INF_PID] = mkf_u32(FO_STATUS, pid);
3846
3847 info[INF_UPTIME] = mkf_str(FN_DURATION, chunk_newstr(out));
3848 chunk_appendf(out, "%ud %uh%02um%02us", up / 86400, (up % 86400) / 3600, (up % 3600) / 60, (up % 60));
3849
3850 info[INF_UPTIME_SEC] = mkf_u32(FN_DURATION, up);
3851 info[INF_MEMMAX_MB] = mkf_u32(FO_CONFIG|FN_LIMIT, global.rlimit_memmax);
3852 info[INF_POOL_ALLOC_MB] = mkf_u32(0, (unsigned)(pool_total_allocated() / 1048576L));
3853 info[INF_POOL_USED_MB] = mkf_u32(0, (unsigned)(pool_total_used() / 1048576L));
3854 info[INF_POOL_FAILED] = mkf_u32(FN_COUNTER, pool_total_failures());
3855 info[INF_ULIMIT_N] = mkf_u32(FO_CONFIG|FN_LIMIT, global.rlimit_nofile);
3856 info[INF_MAXSOCK] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxsock);
3857 info[INF_MAXCONN] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxconn);
3858 info[INF_HARD_MAXCONN] = mkf_u32(FO_CONFIG|FN_LIMIT, global.hardmaxconn);
3859 info[INF_CURR_CONN] = mkf_u32(0, actconn);
3860 info[INF_CUM_CONN] = mkf_u32(FN_COUNTER, totalconn);
3861 info[INF_CUM_REQ] = mkf_u32(FN_COUNTER, global.req_count);
3862#ifdef USE_OPENSSL
3863 info[INF_MAX_SSL_CONNS] = mkf_u32(FN_MAX, global.maxsslconn);
3864 info[INF_CURR_SSL_CONNS] = mkf_u32(0, sslconns);
3865 info[INF_CUM_SSL_CONNS] = mkf_u32(FN_COUNTER, totalsslconns);
3866#endif
3867 info[INF_MAXPIPES] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxpipes);
3868 info[INF_PIPES_USED] = mkf_u32(0, pipes_used);
3869 info[INF_PIPES_FREE] = mkf_u32(0, pipes_free);
3870 info[INF_CONN_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&global.conn_per_sec));
3871 info[INF_CONN_RATE_LIMIT] = mkf_u32(FO_CONFIG|FN_LIMIT, global.cps_lim);
3872 info[INF_MAX_CONN_RATE] = mkf_u32(FN_MAX, global.cps_max);
3873 info[INF_SESS_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&global.sess_per_sec));
3874 info[INF_SESS_RATE_LIMIT] = mkf_u32(FO_CONFIG|FN_LIMIT, global.sps_lim);
3875 info[INF_MAX_SESS_RATE] = mkf_u32(FN_RATE, global.sps_max);
3876
3877#ifdef USE_OPENSSL
3878 info[INF_SSL_RATE] = mkf_u32(FN_RATE, ssl_sess_rate);
3879 info[INF_SSL_RATE_LIMIT] = mkf_u32(FO_CONFIG|FN_LIMIT, global.ssl_lim);
3880 info[INF_MAX_SSL_RATE] = mkf_u32(FN_MAX, global.ssl_max);
3881 info[INF_SSL_FRONTEND_KEY_RATE] = mkf_u32(0, ssl_key_rate);
3882 info[INF_SSL_FRONTEND_MAX_KEY_RATE] = mkf_u32(FN_MAX, global.ssl_fe_keys_max);
3883 info[INF_SSL_FRONTEND_SESSION_REUSE_PCT] = mkf_u32(0, ssl_reuse);
3884 info[INF_SSL_BACKEND_KEY_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&global.ssl_be_keys_per_sec));
3885 info[INF_SSL_BACKEND_MAX_KEY_RATE] = mkf_u32(FN_MAX, global.ssl_be_keys_max);
3886 info[INF_SSL_CACHE_LOOKUPS] = mkf_u32(FN_COUNTER, global.shctx_lookups);
3887 info[INF_SSL_CACHE_MISSES] = mkf_u32(FN_COUNTER, global.shctx_misses);
3888#endif
3889 info[INF_COMPRESS_BPS_IN] = mkf_u32(FN_RATE, read_freq_ctr(&global.comp_bps_in));
3890 info[INF_COMPRESS_BPS_OUT] = mkf_u32(FN_RATE, read_freq_ctr(&global.comp_bps_out));
3891 info[INF_COMPRESS_BPS_RATE_LIM] = mkf_u32(FO_CONFIG|FN_LIMIT, global.comp_rate_lim);
3892#ifdef USE_ZLIB
3893 info[INF_ZLIB_MEM_USAGE] = mkf_u32(0, zlib_used_memory);
3894 info[INF_MAX_ZLIB_MEM_USAGE] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxzlibmem);
3895#endif
3896 info[INF_TASKS] = mkf_u32(0, nb_tasks_cur);
Christopher Faulet34c5cc92016-12-06 09:15:30 +01003897 info[INF_RUN_QUEUE] = mkf_u32(0, tasks_run_queue_cur);
Willy Tarreau81036f22019-05-20 19:24:50 +02003898 info[INF_IDLE_PCT] = mkf_u32(FN_AVG, ti->idle_pct);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003899 info[INF_NODE] = mkf_str(FO_CONFIG|FN_OUTPUT|FS_SERVICE, global.node);
3900 if (global.desc)
3901 info[INF_DESCRIPTION] = mkf_str(FO_CONFIG|FN_OUTPUT|FS_SERVICE, global.desc);
Willy Tarreau00098ea2018-11-05 14:38:13 +01003902 info[INF_STOPPING] = mkf_u32(0, stopping);
3903 info[INF_JOBS] = mkf_u32(0, jobs);
William Lallemanda7199262018-11-16 16:57:20 +01003904 info[INF_UNSTOPPABLE_JOBS] = mkf_u32(0, unstoppable_jobs);
Willy Tarreau00098ea2018-11-05 14:38:13 +01003905 info[INF_LISTENERS] = mkf_u32(0, listeners);
Willy Tarreau199ad242018-11-05 16:31:22 +01003906 info[INF_ACTIVE_PEERS] = mkf_u32(0, active_peers);
Willy Tarreau2d372c22018-11-05 17:12:27 +01003907 info[INF_CONNECTED_PEERS] = mkf_u32(0, connected_peers);
Willy Tarreau13ef7732018-11-12 07:25:28 +01003908 info[INF_DROPPED_LOGS] = mkf_u32(0, dropped_logs);
Willy Tarreaubeb859a2018-11-22 18:07:59 +01003909 info[INF_BUSY_POLLING] = mkf_u32(0, !!(global.tune.options & GTUNE_BUSY_POLLING));
Baptiste Assmann333939c2019-01-21 08:34:50 +01003910 info[INF_FAILED_RESOLUTIONS] = mkf_u32(0, dns_failed_resolutions);
Willy Tarreau7cf0e452019-05-23 11:39:14 +02003911 info[INF_TOTAL_BYTES_OUT] = mkf_u64(0, global.out_bytes);
Christopher Fauletaaa70852020-07-10 13:56:30 +02003912 info[INF_TOTAL_SPLICED_BYTES_OUT] = mkf_u64(0, global.spliced_out_bytes);
Willy Tarreau7cf0e452019-05-23 11:39:14 +02003913 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 +02003914 info[INF_DEBUG_COMMANDS_ISSUED] = mkf_u32(0, debug_commands_issued);
Emeric Brun45c457a2020-07-09 23:23:34 +02003915 info[INF_CUM_LOG_MSGS] = mkf_u32(FN_COUNTER, cum_log_messages);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003916 return 1;
3917}
3918
3919/* This function dumps information onto the stream interface's read buffer.
3920 * It returns 0 as long as it does not complete, non-zero upon completion.
3921 * No state is used.
3922 */
3923static int stats_dump_info_to_buffer(struct stream_interface *si)
3924{
3925 struct appctx *appctx = __objt_appctx(si->end);
3926
3927 if (!stats_fill_info(info, INF_TOTAL_FIELDS))
3928 return 0;
3929
3930 chunk_reset(&trash);
3931
3932 if (appctx->ctx.stats.flags & STAT_FMT_TYPED)
Willy Tarreau43241ff2019-10-09 11:27:51 +02003933 stats_dump_typed_info_fields(&trash, info, appctx->ctx.stats.flags);
Simon Horman05ee2132017-01-04 09:37:25 +01003934 else if (appctx->ctx.stats.flags & STAT_FMT_JSON)
Willy Tarreau43241ff2019-10-09 11:27:51 +02003935 stats_dump_json_info_fields(&trash, info, appctx->ctx.stats.flags);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003936 else
Willy Tarreau43241ff2019-10-09 11:27:51 +02003937 stats_dump_info_fields(&trash, info, appctx->ctx.stats.flags);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003938
Willy Tarreau06d80a92017-10-19 14:32:15 +02003939 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01003940 si_rx_room_blk(si);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003941 return 0;
3942 }
3943
3944 return 1;
3945}
3946
Simon Horman6f6bb382017-01-04 09:37:26 +01003947/* This function dumps the schema onto the stream interface's read buffer.
3948 * It returns 0 as long as it does not complete, non-zero upon completion.
3949 * No state is used.
3950 *
Ilya Shipitsinf38a0182020-12-21 01:16:17 +05003951 * Integer values bounded to the range [-(2**53)+1, (2**53)-1] as
Simon Horman6f6bb382017-01-04 09:37:26 +01003952 * per the recommendation for interoperable integers in section 6 of RFC 7159.
3953 */
Willy Tarreau83061a82018-07-13 11:56:34 +02003954static void stats_dump_json_schema(struct buffer *out)
Simon Horman6f6bb382017-01-04 09:37:26 +01003955{
3956
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003957 int old_len = out->data;
Simon Horman6f6bb382017-01-04 09:37:26 +01003958
3959 chunk_strcat(out,
3960 "{"
3961 "\"$schema\":\"http://json-schema.org/draft-04/schema#\","
3962 "\"oneOf\":["
3963 "{"
3964 "\"title\":\"Info\","
3965 "\"type\":\"array\","
3966 "\"items\":{"
Amaury Denoyellea53ce4c2020-10-02 18:31:59 +02003967 "\"title\":\"InfoItem\","
3968 "\"type\":\"object\","
Simon Horman6f6bb382017-01-04 09:37:26 +01003969 "\"properties\":{"
Simon Horman6f6bb382017-01-04 09:37:26 +01003970 "\"field\":{\"$ref\":\"#/definitions/field\"},"
3971 "\"processNum\":{\"$ref\":\"#/definitions/processNum\"},"
3972 "\"tags\":{\"$ref\":\"#/definitions/tags\"},"
3973 "\"value\":{\"$ref\":\"#/definitions/typedValue\"}"
3974 "},"
3975 "\"required\":[\"field\",\"processNum\",\"tags\","
3976 "\"value\"]"
3977 "}"
3978 "},"
3979 "{"
3980 "\"title\":\"Stat\","
3981 "\"type\":\"array\","
3982 "\"items\":{"
3983 "\"title\":\"InfoItem\","
3984 "\"type\":\"object\","
3985 "\"properties\":{"
3986 "\"objType\":{"
3987 "\"enum\":[\"Frontend\",\"Backend\",\"Listener\","
3988 "\"Server\",\"Unknown\"]"
3989 "},"
3990 "\"proxyId\":{"
3991 "\"type\":\"integer\","
3992 "\"minimum\":0"
3993 "},"
3994 "\"id\":{"
3995 "\"type\":\"integer\","
3996 "\"minimum\":0"
3997 "},"
3998 "\"field\":{\"$ref\":\"#/definitions/field\"},"
3999 "\"processNum\":{\"$ref\":\"#/definitions/processNum\"},"
4000 "\"tags\":{\"$ref\":\"#/definitions/tags\"},"
4001 "\"typedValue\":{\"$ref\":\"#/definitions/typedValue\"}"
4002 "},"
4003 "\"required\":[\"objType\",\"proxyId\",\"id\","
4004 "\"field\",\"processNum\",\"tags\","
4005 "\"value\"]"
4006 "}"
4007 "},"
4008 "{"
4009 "\"title\":\"Error\","
4010 "\"type\":\"object\","
4011 "\"properties\":{"
4012 "\"errorStr\":{"
4013 "\"type\":\"string\""
Amaury Denoyellea53ce4c2020-10-02 18:31:59 +02004014 "}"
4015 "},"
4016 "\"required\":[\"errorStr\"]"
Simon Horman6f6bb382017-01-04 09:37:26 +01004017 "}"
4018 "],"
4019 "\"definitions\":{"
4020 "\"field\":{"
4021 "\"type\":\"object\","
4022 "\"pos\":{"
4023 "\"type\":\"integer\","
4024 "\"minimum\":0"
4025 "},"
4026 "\"name\":{"
4027 "\"type\":\"string\""
4028 "},"
4029 "\"required\":[\"pos\",\"name\"]"
4030 "},"
4031 "\"processNum\":{"
4032 "\"type\":\"integer\","
4033 "\"minimum\":1"
4034 "},"
4035 "\"tags\":{"
4036 "\"type\":\"object\","
4037 "\"origin\":{"
4038 "\"type\":\"string\","
4039 "\"enum\":[\"Metric\",\"Status\",\"Key\","
4040 "\"Config\",\"Product\",\"Unknown\"]"
4041 "},"
4042 "\"nature\":{"
4043 "\"type\":\"string\","
4044 "\"enum\":[\"Gauge\",\"Limit\",\"Min\",\"Max\","
4045 "\"Rate\",\"Counter\",\"Duration\","
4046 "\"Age\",\"Time\",\"Name\",\"Output\","
4047 "\"Avg\", \"Unknown\"]"
4048 "},"
4049 "\"scope\":{"
4050 "\"type\":\"string\","
4051 "\"enum\":[\"Cluster\",\"Process\",\"Service\","
4052 "\"System\",\"Unknown\"]"
4053 "},"
4054 "\"required\":[\"origin\",\"nature\",\"scope\"]"
4055 "},"
4056 "\"typedValue\":{"
4057 "\"type\":\"object\","
4058 "\"oneOf\":["
4059 "{\"$ref\":\"#/definitions/typedValue/definitions/s32Value\"},"
4060 "{\"$ref\":\"#/definitions/typedValue/definitions/s64Value\"},"
4061 "{\"$ref\":\"#/definitions/typedValue/definitions/u32Value\"},"
4062 "{\"$ref\":\"#/definitions/typedValue/definitions/u64Value\"},"
4063 "{\"$ref\":\"#/definitions/typedValue/definitions/strValue\"}"
4064 "],"
4065 "\"definitions\":{"
4066 "\"s32Value\":{"
4067 "\"properties\":{"
4068 "\"type\":{"
4069 "\"type\":\"string\","
4070 "\"enum\":[\"s32\"]"
4071 "},"
4072 "\"value\":{"
4073 "\"type\":\"integer\","
4074 "\"minimum\":-2147483648,"
4075 "\"maximum\":2147483647"
4076 "}"
4077 "},"
4078 "\"required\":[\"type\",\"value\"]"
4079 "},"
4080 "\"s64Value\":{"
4081 "\"properties\":{"
4082 "\"type\":{"
4083 "\"type\":\"string\","
4084 "\"enum\":[\"s64\"]"
4085 "},"
4086 "\"value\":{"
4087 "\"type\":\"integer\","
4088 "\"minimum\":-9007199254740991,"
4089 "\"maximum\":9007199254740991"
4090 "}"
4091 "},"
4092 "\"required\":[\"type\",\"value\"]"
4093 "},"
4094 "\"u32Value\":{"
4095 "\"properties\":{"
4096 "\"type\":{"
4097 "\"type\":\"string\","
4098 "\"enum\":[\"u32\"]"
4099 "},"
4100 "\"value\":{"
4101 "\"type\":\"integer\","
4102 "\"minimum\":0,"
4103 "\"maximum\":4294967295"
4104 "}"
4105 "},"
4106 "\"required\":[\"type\",\"value\"]"
4107 "},"
4108 "\"u64Value\":{"
4109 "\"properties\":{"
4110 "\"type\":{"
4111 "\"type\":\"string\","
4112 "\"enum\":[\"u64\"]"
4113 "},"
4114 "\"value\":{"
4115 "\"type\":\"integer\","
4116 "\"minimum\":0,"
4117 "\"maximum\":9007199254740991"
4118 "}"
4119 "},"
4120 "\"required\":[\"type\",\"value\"]"
4121 "},"
4122 "\"strValue\":{"
4123 "\"properties\":{"
4124 "\"type\":{"
4125 "\"type\":\"string\","
4126 "\"enum\":[\"str\"]"
4127 "},"
4128 "\"value\":{\"type\":\"string\"}"
4129 "},"
4130 "\"required\":[\"type\",\"value\"]"
4131 "},"
4132 "\"unknownValue\":{"
4133 "\"properties\":{"
4134 "\"type\":{"
4135 "\"type\":\"integer\","
4136 "\"minimum\":0"
4137 "},"
4138 "\"value\":{"
4139 "\"type\":\"string\","
4140 "\"enum\":[\"unknown\"]"
4141 "}"
4142 "},"
4143 "\"required\":[\"type\",\"value\"]"
4144 "}"
4145 "}"
4146 "}"
4147 "}"
4148 "}");
4149
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004150 if (old_len == out->data) {
Simon Horman6f6bb382017-01-04 09:37:26 +01004151 chunk_reset(out);
4152 chunk_appendf(out,
4153 "{\"errorStr\":\"output buffer too short\"}");
4154 }
4155}
4156
4157/* This function dumps the schema onto the stream interface's read buffer.
4158 * It returns 0 as long as it does not complete, non-zero upon completion.
4159 * No state is used.
4160 */
4161static int stats_dump_json_schema_to_buffer(struct stream_interface *si)
4162{
4163 chunk_reset(&trash);
4164
4165 stats_dump_json_schema(&trash);
4166
Willy Tarreau06d80a92017-10-19 14:32:15 +02004167 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01004168 si_rx_room_blk(si);
Simon Horman6f6bb382017-01-04 09:37:26 +01004169 return 0;
4170 }
4171
4172 return 1;
4173}
4174
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004175static int cli_parse_clear_counters(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau89d467c2016-11-23 11:02:40 +01004176{
4177 struct proxy *px;
4178 struct server *sv;
4179 struct listener *li;
Amaury Denoyelled3700a72020-10-05 11:49:43 +02004180 struct stats_module *mod;
Willy Tarreau89d467c2016-11-23 11:02:40 +01004181 int clrall = 0;
4182
4183 if (strcmp(args[2], "all") == 0)
4184 clrall = 1;
4185
4186 /* check permissions */
4187 if (!cli_has_level(appctx, ACCESS_LVL_OPER) ||
4188 (clrall && !cli_has_level(appctx, ACCESS_LVL_ADMIN)))
4189 return 1;
4190
Olivier Houchardfbc74e82017-11-24 16:54:05 +01004191 for (px = proxies_list; px; px = px->next) {
Willy Tarreau89d467c2016-11-23 11:02:40 +01004192 if (clrall) {
4193 memset(&px->be_counters, 0, sizeof(px->be_counters));
4194 memset(&px->fe_counters, 0, sizeof(px->fe_counters));
4195 }
4196 else {
4197 px->be_counters.conn_max = 0;
4198 px->be_counters.p.http.rps_max = 0;
4199 px->be_counters.sps_max = 0;
4200 px->be_counters.cps_max = 0;
4201 px->be_counters.nbpend_max = 0;
Christopher Fauletefb41f02019-11-08 14:53:15 +01004202 px->be_counters.qtime_max = 0;
4203 px->be_counters.ctime_max = 0;
4204 px->be_counters.dtime_max = 0;
4205 px->be_counters.ttime_max = 0;
Willy Tarreau89d467c2016-11-23 11:02:40 +01004206
4207 px->fe_counters.conn_max = 0;
4208 px->fe_counters.p.http.rps_max = 0;
4209 px->fe_counters.sps_max = 0;
4210 px->fe_counters.cps_max = 0;
Willy Tarreau89d467c2016-11-23 11:02:40 +01004211 }
4212
4213 for (sv = px->srv; sv; sv = sv->next)
4214 if (clrall)
4215 memset(&sv->counters, 0, sizeof(sv->counters));
4216 else {
4217 sv->counters.cur_sess_max = 0;
4218 sv->counters.nbpend_max = 0;
4219 sv->counters.sps_max = 0;
Christopher Fauletefb41f02019-11-08 14:53:15 +01004220 sv->counters.qtime_max = 0;
4221 sv->counters.ctime_max = 0;
4222 sv->counters.dtime_max = 0;
4223 sv->counters.ttime_max = 0;
Willy Tarreau89d467c2016-11-23 11:02:40 +01004224 }
4225
4226 list_for_each_entry(li, &px->conf.listeners, by_fe)
4227 if (li->counters) {
4228 if (clrall)
4229 memset(li->counters, 0, sizeof(*li->counters));
4230 else
4231 li->counters->conn_max = 0;
4232 }
4233 }
4234
4235 global.cps_max = 0;
4236 global.sps_max = 0;
Olivier Houchard00bc3cb2017-10-17 19:23:25 +02004237 global.ssl_max = 0;
4238 global.ssl_fe_keys_max = 0;
4239 global.ssl_be_keys_max = 0;
Willy Tarreaud80cb4e2018-01-20 19:30:13 +01004240
Amaury Denoyelled3700a72020-10-05 11:49:43 +02004241 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
4242 if (!mod->clearable && !clrall)
4243 continue;
4244
4245 for (px = proxies_list; px; px = px->next) {
4246 enum stats_domain_px_cap mod_cap = stats_px_get_cap(mod->domain_flags);
4247
4248 if (px->cap & PR_CAP_FE && mod_cap & STATS_PX_CAP_FE) {
4249 EXTRA_COUNTERS_INIT(px->extra_counters_fe,
4250 mod,
4251 mod->counters,
4252 mod->counters_size);
4253 }
4254
4255 if (px->cap & PR_CAP_BE && mod_cap & STATS_PX_CAP_BE) {
4256 EXTRA_COUNTERS_INIT(px->extra_counters_be,
4257 mod,
4258 mod->counters,
4259 mod->counters_size);
4260 }
4261
4262 if (mod_cap & STATS_PX_CAP_SRV) {
4263 for (sv = px->srv; sv; sv = sv->next) {
4264 EXTRA_COUNTERS_INIT(sv->extra_counters,
4265 mod,
4266 mod->counters,
4267 mod->counters_size);
4268 }
4269 }
4270
4271 if (mod_cap & STATS_PX_CAP_LI) {
4272 list_for_each_entry(li, &px->conf.listeners, by_fe) {
4273 EXTRA_COUNTERS_INIT(li->extra_counters,
4274 mod,
4275 mod->counters,
4276 mod->counters_size);
4277 }
4278 }
4279 }
4280 }
4281
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02004282 dns_stats_clear_counters(clrall, &stats_module_list[STATS_DOMAIN_DNS]);
4283
Willy Tarreaud80cb4e2018-01-20 19:30:13 +01004284 memset(activity, 0, sizeof(activity));
Willy Tarreau89d467c2016-11-23 11:02:40 +01004285 return 1;
4286}
4287
4288
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004289static int cli_parse_show_info(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004290{
Willy Tarreau2f397382019-10-09 11:43:59 +02004291 int arg = 2;
4292
Willy Tarreaud25fc792016-12-16 12:33:47 +01004293 appctx->ctx.stats.scope_str = 0;
4294 appctx->ctx.stats.scope_len = 0;
4295 appctx->ctx.stats.flags = 0;
4296
Willy Tarreau2f397382019-10-09 11:43:59 +02004297 while (*args[arg]) {
4298 if (strcmp(args[arg], "typed") == 0)
4299 appctx->ctx.stats.flags = (appctx->ctx.stats.flags & ~STAT_FMT_MASK) | STAT_FMT_TYPED;
4300 else if (strcmp(args[arg], "json") == 0)
4301 appctx->ctx.stats.flags = (appctx->ctx.stats.flags & ~STAT_FMT_MASK) | STAT_FMT_JSON;
4302 else if (strcmp(args[arg], "desc") == 0)
4303 appctx->ctx.stats.flags |= STAT_SHOW_FDESC;
4304 arg++;
4305 }
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004306 return 0;
4307}
4308
4309
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004310static int cli_parse_show_stat(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau2b812e22016-11-22 16:18:05 +01004311{
Willy Tarreau2f397382019-10-09 11:43:59 +02004312 int arg = 2;
4313
Willy Tarreaud25fc792016-12-16 12:33:47 +01004314 appctx->ctx.stats.scope_str = 0;
4315 appctx->ctx.stats.scope_len = 0;
Willy Tarreau578d6e42019-10-09 11:00:22 +02004316 appctx->ctx.stats.flags = STAT_SHNODE | STAT_SHDESC;
4317
4318 if ((strm_li(si_strm(appctx->owner))->bind_conf->level & ACCESS_LVL_MASK) >= ACCESS_LVL_OPER)
4319 appctx->ctx.stats.flags |= STAT_SHLGNDS;
Willy Tarreaud25fc792016-12-16 12:33:47 +01004320
Amaury Denoyelle072f97e2020-10-05 11:49:37 +02004321 /* proxy is the default domain */
4322 appctx->ctx.stats.domain = STATS_DOMAIN_PROXY;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01004323 if (strcmp(args[arg], "domain") == 0) {
Amaury Denoyelle072f97e2020-10-05 11:49:37 +02004324 ++args;
4325
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01004326 if (strcmp(args[arg], "proxy") == 0) {
Amaury Denoyelle072f97e2020-10-05 11:49:37 +02004327 ++args;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01004328 } else if (strcmp(args[arg], "dns") == 0) {
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02004329 appctx->ctx.stats.domain = STATS_DOMAIN_DNS;
4330 ++args;
4331 } else {
Amaury Denoyelle072f97e2020-10-05 11:49:37 +02004332 return cli_err(appctx, "Invalid statistics domain.\n");
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02004333 }
Amaury Denoyelle072f97e2020-10-05 11:49:37 +02004334 }
4335
4336 if (appctx->ctx.stats.domain == STATS_DOMAIN_PROXY
4337 && *args[arg] && *args[arg+1] && *args[arg+2]) {
Willy Tarreaua1b1ed52016-11-25 08:50:58 +01004338 struct proxy *px;
4339
Willy Tarreau2f397382019-10-09 11:43:59 +02004340 px = proxy_find_by_name(args[arg], 0, 0);
Willy Tarreaua1b1ed52016-11-25 08:50:58 +01004341 if (px)
4342 appctx->ctx.stats.iid = px->uuid;
4343 else
Willy Tarreau2f397382019-10-09 11:43:59 +02004344 appctx->ctx.stats.iid = atoi(args[arg]);
Willy Tarreaua1b1ed52016-11-25 08:50:58 +01004345
Willy Tarreau9d008692019-08-09 11:21:01 +02004346 if (!appctx->ctx.stats.iid)
4347 return cli_err(appctx, "No such proxy.\n");
Willy Tarreaua1b1ed52016-11-25 08:50:58 +01004348
Willy Tarreau2b812e22016-11-22 16:18:05 +01004349 appctx->ctx.stats.flags |= STAT_BOUND;
Willy Tarreau2f397382019-10-09 11:43:59 +02004350 appctx->ctx.stats.type = atoi(args[arg+1]);
4351 appctx->ctx.stats.sid = atoi(args[arg+2]);
4352 arg += 3;
4353 }
4354
4355 while (*args[arg]) {
4356 if (strcmp(args[arg], "typed") == 0)
4357 appctx->ctx.stats.flags = (appctx->ctx.stats.flags & ~STAT_FMT_MASK) | STAT_FMT_TYPED;
4358 else if (strcmp(args[arg], "json") == 0)
4359 appctx->ctx.stats.flags = (appctx->ctx.stats.flags & ~STAT_FMT_MASK) | STAT_FMT_JSON;
4360 else if (strcmp(args[arg], "desc") == 0)
4361 appctx->ctx.stats.flags |= STAT_SHOW_FDESC;
Willy Tarreau3e320362020-10-23 17:28:57 +02004362 else if (strcmp(args[arg], "no-maint") == 0)
4363 appctx->ctx.stats.flags |= STAT_HIDE_MAINT;
Willy Tarreau65141ff2020-10-23 17:19:48 +02004364 else if (strcmp(args[arg], "up") == 0)
4365 appctx->ctx.stats.flags |= STAT_HIDE_DOWN;
Willy Tarreau2f397382019-10-09 11:43:59 +02004366 arg++;
Willy Tarreau2b812e22016-11-22 16:18:05 +01004367 }
Willy Tarreau2b812e22016-11-22 16:18:05 +01004368
Willy Tarreau2b812e22016-11-22 16:18:05 +01004369 return 0;
4370}
4371
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004372static int cli_io_handler_dump_info(struct appctx *appctx)
4373{
4374 return stats_dump_info_to_buffer(appctx->owner);
4375}
4376
Willy Tarreau2b812e22016-11-22 16:18:05 +01004377/* This I/O handler runs as an applet embedded in a stream interface. It is
4378 * used to send raw stats over a socket.
4379 */
4380static int cli_io_handler_dump_stat(struct appctx *appctx)
4381{
Christopher Fauletef779222018-10-31 08:47:01 +01004382 return stats_dump_stat_to_buffer(appctx->owner, NULL, NULL);
Willy Tarreau2b812e22016-11-22 16:18:05 +01004383}
4384
Simon Horman6f6bb382017-01-04 09:37:26 +01004385static int cli_io_handler_dump_json_schema(struct appctx *appctx)
4386{
4387 return stats_dump_json_schema_to_buffer(appctx->owner);
4388}
4389
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02004390static int stats_allocate_proxy_counters_internal(struct extra_counters **counters,
4391 int type, int px_cap)
4392{
4393 struct stats_module *mod;
4394
4395 EXTRA_COUNTERS_REGISTER(counters, type, alloc_failed);
4396
4397 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
4398 if (!(stats_px_get_cap(mod->domain_flags) & px_cap))
4399 continue;
4400
4401 EXTRA_COUNTERS_ADD(mod, *counters, mod->counters, mod->counters_size);
4402 }
4403
4404 EXTRA_COUNTERS_ALLOC(*counters, alloc_failed);
4405
4406 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
4407 if (!(stats_px_get_cap(mod->domain_flags) & px_cap))
4408 continue;
4409
4410 EXTRA_COUNTERS_INIT(*counters, mod, mod->counters, mod->counters_size);
4411 }
4412
4413 return 1;
4414
4415 alloc_failed:
4416 return 0;
4417}
4418
4419/* Initialize and allocate all extra counters for a proxy and its attached
4420 * servers/listeners with all already registered stats module
4421 */
4422int stats_allocate_proxy_counters(struct proxy *px)
4423{
4424 struct server *sv;
4425 struct listener *li;
4426
4427 if (px->cap & PR_CAP_FE) {
4428 if (!stats_allocate_proxy_counters_internal(&px->extra_counters_fe,
4429 COUNTERS_FE,
4430 STATS_PX_CAP_FE)) {
4431 return 0;
4432 }
4433 }
4434
4435 if (px->cap & PR_CAP_BE) {
4436 if (!stats_allocate_proxy_counters_internal(&px->extra_counters_be,
4437 COUNTERS_BE,
4438 STATS_PX_CAP_BE)) {
4439 return 0;
4440 }
4441 }
4442
4443 for (sv = px->srv; sv; sv = sv->next) {
4444 if (!stats_allocate_proxy_counters_internal(&sv->extra_counters,
4445 COUNTERS_SV,
4446 STATS_PX_CAP_SRV)) {
4447 return 0;
4448 }
4449 }
4450
4451 list_for_each_entry(li, &px->conf.listeners, by_fe) {
4452 if (!stats_allocate_proxy_counters_internal(&li->extra_counters,
4453 COUNTERS_LI,
4454 STATS_PX_CAP_LI)) {
4455 return 0;
4456 }
4457 }
4458
4459 return 1;
4460}
4461
Amaury Denoyelle58d395e2020-10-05 11:49:40 +02004462void stats_register_module(struct stats_module *m)
4463{
4464 const uint8_t domain = stats_get_domain(m->domain_flags);
4465
4466 LIST_ADDQ(&stats_module_list[domain], &m->list);
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02004467 stat_count[domain] += m->stats_count;
Amaury Denoyelle58d395e2020-10-05 11:49:40 +02004468}
4469
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02004470static int allocate_stats_px_postcheck(void)
4471{
4472 struct stats_module *mod;
4473 size_t i = ST_F_TOTAL_FIELDS;
4474 int err_code = 0;
Amaury Denoyelle27373f72020-10-05 16:57:33 +02004475 struct proxy *px;
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02004476
4477 stat_count[STATS_DOMAIN_PROXY] += ST_F_TOTAL_FIELDS;
4478
4479 stat_f[STATS_DOMAIN_PROXY] = malloc(stat_count[STATS_DOMAIN_PROXY] * sizeof(struct name_desc));
4480 if (!stat_f[STATS_DOMAIN_PROXY]) {
4481 ha_alert("stats: cannot allocate all fields for proxy statistics\n");
4482 err_code |= ERR_ALERT | ERR_FATAL;
4483 return err_code;
4484 }
4485
4486 memcpy(stat_f[STATS_DOMAIN_PROXY], stat_fields,
4487 ST_F_TOTAL_FIELDS * sizeof(struct name_desc));
4488
4489 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
4490 memcpy(stat_f[STATS_DOMAIN_PROXY] + i,
4491 mod->stats,
4492 mod->stats_count * sizeof(struct name_desc));
4493 i += mod->stats_count;
4494 }
4495
Amaury Denoyelle27373f72020-10-05 16:57:33 +02004496 for (px = proxies_list; px; px = px->next) {
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02004497 if (!stats_allocate_proxy_counters(px)) {
4498 ha_alert("stats: cannot allocate all counters for proxy statistics\n");
4499 err_code |= ERR_ALERT | ERR_FATAL;
4500 return err_code;
4501 }
4502 }
4503
Christopher Fauletde79cd22021-01-06 07:41:56 +01004504 /* wait per-thread alloc to perform corresponding stat_l allocation */
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02004505
4506 return err_code;
4507}
4508
4509REGISTER_CONFIG_POSTPARSER("allocate-stats-px", allocate_stats_px_postcheck);
4510
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02004511static int allocate_stats_dns_postcheck(void)
4512{
4513 struct stats_module *mod;
4514 size_t i = 0;
4515 int err_code = 0;
4516
4517 stat_f[STATS_DOMAIN_DNS] = malloc(stat_count[STATS_DOMAIN_DNS] * sizeof(struct name_desc));
4518 if (!stat_f[STATS_DOMAIN_DNS]) {
4519 ha_alert("stats: cannot allocate all fields for dns statistics\n");
4520 err_code |= ERR_ALERT | ERR_FATAL;
4521 return err_code;
4522 }
4523
4524 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_DNS], list) {
4525 memcpy(stat_f[STATS_DOMAIN_DNS] + i,
4526 mod->stats,
4527 mod->stats_count * sizeof(struct name_desc));
4528 i += mod->stats_count;
4529 }
4530
4531 if (!dns_allocate_counters(&stats_module_list[STATS_DOMAIN_DNS])) {
4532 ha_alert("stats: cannot allocate all counters for dns statistics\n");
4533 err_code |= ERR_ALERT | ERR_FATAL;
4534 return err_code;
4535 }
4536
Christopher Fauletde79cd22021-01-06 07:41:56 +01004537 /* wait per-thread alloc to perform corresponding stat_l allocation */
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02004538
4539 return err_code;
4540}
4541
4542REGISTER_CONFIG_POSTPARSER("allocate-stats-dns", allocate_stats_dns_postcheck);
4543
Christopher Fauletde79cd22021-01-06 07:41:56 +01004544static int allocate_stat_lines_per_thread(void)
4545{
4546 int domains[] = { STATS_DOMAIN_PROXY, STATS_DOMAIN_DNS }, i;
4547
4548 for (i = 0; i < STATS_DOMAIN_COUNT; ++i) {
4549 const int domain = domains[i];
4550
4551 stat_l[domain] = malloc(stat_count[domain] * sizeof(struct field));
4552 if (!stat_l[domain])
4553 return 0;
4554 }
4555 return 1;
4556}
4557
4558REGISTER_PER_THREAD_ALLOC(allocate_stat_lines_per_thread);
4559
Amaury Denoyelle7f8f6cb2020-11-10 14:24:31 +01004560static int allocate_trash_counters(void)
4561{
4562 struct stats_module *mod;
4563 int domains[] = { STATS_DOMAIN_PROXY, STATS_DOMAIN_DNS }, i;
4564 size_t max_counters_size = 0;
4565
4566 /* calculate the greatest counters used by any stats modules */
4567 for (i = 0; i < STATS_DOMAIN_COUNT; ++i) {
4568 list_for_each_entry(mod, &stats_module_list[domains[i]], list) {
4569 max_counters_size = mod->counters_size > max_counters_size ?
4570 mod->counters_size : max_counters_size;
4571 }
4572 }
4573
4574 /* allocate the trash with the size of the greatest counters */
4575 if (max_counters_size) {
4576 trash_counters = malloc(max_counters_size);
4577 if (!trash_counters) {
4578 ha_alert("stats: cannot allocate trash counters for statistics\n");
4579 return 0;
4580 }
4581 }
4582
4583 return 1;
4584}
4585
4586REGISTER_PER_THREAD_ALLOC(allocate_trash_counters);
4587
Christopher Fauletde79cd22021-01-06 07:41:56 +01004588static void deinit_stat_lines_per_thread(void)
Amaury Denoyellea2a68992020-11-10 14:24:30 +01004589{
4590 int domains[] = { STATS_DOMAIN_PROXY, STATS_DOMAIN_DNS }, i;
4591
4592 for (i = 0; i < STATS_DOMAIN_COUNT; ++i) {
4593 const int domain = domains[i];
4594
Christopher Fauletde79cd22021-01-06 07:41:56 +01004595 free(stat_l[domain]);
4596 stat_l[domain] = NULL;
4597 }
4598}
4599
4600
4601REGISTER_PER_THREAD_FREE(deinit_stat_lines_per_thread);
4602
4603static void deinit_stats(void)
4604{
4605 int domains[] = { STATS_DOMAIN_PROXY, STATS_DOMAIN_DNS }, i;
4606
4607 for (i = 0; i < STATS_DOMAIN_COUNT; ++i) {
4608 const int domain = domains[i];
Amaury Denoyellea2a68992020-11-10 14:24:30 +01004609
4610 if (stat_f[domain])
4611 free(stat_f[domain]);
4612 }
4613}
4614
4615REGISTER_POST_DEINIT(deinit_stats);
4616
Amaury Denoyelle7f8f6cb2020-11-10 14:24:31 +01004617static void free_trash_counters(void)
4618{
4619 if (trash_counters)
4620 free(trash_counters);
4621}
4622
4623REGISTER_PER_THREAD_FREE(free_trash_counters);
4624
Willy Tarreau2b812e22016-11-22 16:18:05 +01004625/* register cli keywords */
4626static struct cli_kw_list cli_kws = {{ },{
Willy Tarreau89d467c2016-11-23 11:02:40 +01004627 { { "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 +02004628 { { "show", "info", NULL }, "show info : report information about the running process [desc|json|typed]*", cli_parse_show_info, cli_io_handler_dump_info, NULL },
Willy Tarreau3e320362020-10-23 17:28:57 +02004629 { { "show", "stat", NULL }, "show stat : report counters for each proxy and server [desc|json|no-maint|typed|up]*", cli_parse_show_stat, cli_io_handler_dump_stat, NULL },
Simon Horman6f6bb382017-01-04 09:37:26 +01004630 { { "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 +01004631 {{},}
4632}};
4633
Willy Tarreau0108d902018-11-25 19:14:37 +01004634INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
4635
William Lallemand74c24fb2016-11-21 17:18:36 +01004636struct applet http_stats_applet = {
4637 .obj_type = OBJ_TYPE_APPLET,
4638 .name = "<STATS>", /* used for logging */
4639 .fct = http_stats_io_handler,
4640 .release = NULL,
4641};
4642
William Lallemand74c24fb2016-11-21 17:18:36 +01004643/*
4644 * Local variables:
4645 * c-indent-level: 8
4646 * c-basic-offset: 8
4647 * End:
4648 */