blob: cdf6a1807d1cebd28571caffbfc525a49a121469 [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>
William Lallemand74c24fb2016-11-21 17:18:36 +010016#include <stdio.h>
17#include <stdlib.h>
18#include <string.h>
19#include <pwd.h>
20#include <grp.h>
21
22#include <sys/socket.h>
23#include <sys/stat.h>
24#include <sys/types.h>
25
Willy Tarreaub2551052020-06-09 09:07:15 +020026#include <haproxy/api.h>
Willy Tarreau5d9ddc52021-10-06 19:54:09 +020027#include <haproxy/activity.h>
Willy Tarreau91cefca2022-05-03 17:08:29 +020028#include <haproxy/applet.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 Tarreau55542642021-10-08 09:33:24 +020035#include <haproxy/clock.h>
Willy Tarreau0a3bd392020-06-04 08:52:38 +020036#include <haproxy/compression.h>
Willy Tarreau2a83d602020-05-27 16:58:08 +020037#include <haproxy/debug.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 Tarreaub7fc4c42021-10-06 18:56:42 +020044#include <haproxy/http_ana.h>
Willy Tarreau87735332020-06-04 09:08:41 +020045#include <haproxy/http_htx.h>
Willy Tarreau16f958c2020-06-03 08:44:35 +020046#include <haproxy/htx.h>
Willy Tarreau853b2972020-05-27 18:01:47 +020047#include <haproxy/list.h>
Willy Tarreau36979d92020-06-05 17:27:29 +020048#include <haproxy/listener.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020049#include <haproxy/log.h>
Willy Tarreau2cd58092020-06-04 15:10:43 +020050#include <haproxy/map-t.h>
Willy Tarreau225a90a2020-06-04 15:06:28 +020051#include <haproxy/pattern-t.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020052#include <haproxy/pipe.h>
53#include <haproxy/pool.h>
Willy Tarreaua264d962020-06-04 22:29:18 +020054#include <haproxy/proxy.h>
Emeric Brunc9437992021-02-12 19:42:55 +010055#include <haproxy/resolvers.h>
Willy Tarreau5edca2f2022-05-27 09:25:10 +020056#include <haproxy/sc_strm.h>
Willy Tarreau1e56f922020-06-04 23:20:13 +020057#include <haproxy/server.h>
Willy Tarreau48d25b32020-06-04 18:58:52 +020058#include <haproxy/session.h>
Willy Tarreau2eec9b52020-06-04 19:58:55 +020059#include <haproxy/stats.h>
Willy Tarreaucb086c62022-05-27 09:47:12 +020060#include <haproxy/stconn.h>
Willy Tarreaudfd3de82020-06-04 23:46:14 +020061#include <haproxy/stream.h>
Willy Tarreaucea0e1b2020-06-04 17:25:40 +020062#include <haproxy/task.h>
Willy Tarreauc2f7c582020-06-02 18:15:32 +020063#include <haproxy/ticks.h>
Willy Tarreau92b4f132020-06-01 11:05:15 +020064#include <haproxy/time.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020065#include <haproxy/tools.h>
Willy Tarreau8c42b8a2020-06-04 19:27:34 +020066#include <haproxy/uri_auth-t.h>
Willy Tarreaud6788052020-05-27 15:59:00 +020067#include <haproxy/version.h>
William Lallemand74c24fb2016-11-21 17:18:36 +010068
William Lallemand74c24fb2016-11-21 17:18:36 +010069
Christopher Faulet2b9b6782019-02-27 16:42:58 +010070/* status codes available for the stats admin page (strictly 4 chars length) */
71const char *stat_status_codes[STAT_STATUS_SIZE] = {
72 [STAT_STATUS_DENY] = "DENY",
73 [STAT_STATUS_DONE] = "DONE",
74 [STAT_STATUS_ERRP] = "ERRP",
75 [STAT_STATUS_EXCD] = "EXCD",
76 [STAT_STATUS_NONE] = "NONE",
77 [STAT_STATUS_PART] = "PART",
78 [STAT_STATUS_UNKN] = "UNKN",
79 [STAT_STATUS_IVAL] = "IVAL",
80};
81
Willy Tarreau0baac8c2016-11-22 16:36:53 +010082/* These are the field names for each INF_* field position. Please pay attention
83 * to always use the exact same name except that the strings for new names must
84 * be lower case or CamelCase while the enum entries must be upper case.
85 */
Willy Tarreaueaa55372019-10-09 07:39:11 +020086const struct name_desc info_fields[INF_TOTAL_FIELDS] = {
Willy Tarreau6d4897e2019-10-11 16:31:46 +020087 [INF_NAME] = { .name = "Name", .desc = "Product name" },
88 [INF_VERSION] = { .name = "Version", .desc = "Product version" },
89 [INF_RELEASE_DATE] = { .name = "Release_date", .desc = "Date of latest source code update" },
90 [INF_NBTHREAD] = { .name = "Nbthread", .desc = "Number of started threads (global.nbthread)" },
Willy Tarreau91358592021-06-15 08:08:04 +020091 [INF_NBPROC] = { .name = "Nbproc", .desc = "Number of started worker processes (historical, always 1)" },
Willy Tarreaue8422bf2021-06-15 09:08:18 +020092 [INF_PROCESS_NUM] = { .name = "Process_num", .desc = "Relative worker process number (1)" },
Willy Tarreau6d4897e2019-10-11 16:31:46 +020093 [INF_PID] = { .name = "Pid", .desc = "This worker process identifier for the system" },
94 [INF_UPTIME] = { .name = "Uptime", .desc = "How long ago this worker process was started (days+hours+minutes+seconds)" },
95 [INF_UPTIME_SEC] = { .name = "Uptime_sec", .desc = "How long ago this worker process was started (seconds)" },
William Dauchydefd1562021-01-15 22:41:38 +010096 [INF_START_TIME_SEC] = { .name = "Start_time_sec", .desc = "Start time in seconds" },
Willy Tarreau6d4897e2019-10-11 16:31:46 +020097 [INF_MEMMAX_MB] = { .name = "Memmax_MB", .desc = "Worker process's hard limit on memory usage in MB (-m on command line)" },
William Dauchya8766cf2021-01-15 22:41:37 +010098 [INF_MEMMAX_BYTES] = { .name = "Memmax_bytes", .desc = "Worker process's hard limit on memory usage in byes (-m on command line)" },
Willy Tarreau6d4897e2019-10-11 16:31:46 +020099 [INF_POOL_ALLOC_MB] = { .name = "PoolAlloc_MB", .desc = "Amount of memory allocated in pools (in MB)" },
William Dauchya8766cf2021-01-15 22:41:37 +0100100 [INF_POOL_ALLOC_BYTES] = { .name = "PoolAlloc_bytes", .desc = "Amount of memory allocated in pools (in bytes)" },
Willy Tarreau6d4897e2019-10-11 16:31:46 +0200101 [INF_POOL_USED_MB] = { .name = "PoolUsed_MB", .desc = "Amount of pool memory currently used (in MB)" },
William Dauchya8766cf2021-01-15 22:41:37 +0100102 [INF_POOL_USED_BYTES] = { .name = "PoolUsed_bytes", .desc = "Amount of pool memory currently used (in bytes)" },
Willy Tarreau6d4897e2019-10-11 16:31:46 +0200103 [INF_POOL_FAILED] = { .name = "PoolFailed", .desc = "Number of failed pool allocations since this worker was started" },
104 [INF_ULIMIT_N] = { .name = "Ulimit-n", .desc = "Hard limit on the number of per-process file descriptors" },
105 [INF_MAXSOCK] = { .name = "Maxsock", .desc = "Hard limit on the number of per-process sockets" },
106 [INF_MAXCONN] = { .name = "Maxconn", .desc = "Hard limit on the number of per-process connections (configured or imposed by Ulimit-n)" },
107 [INF_HARD_MAXCONN] = { .name = "Hard_maxconn", .desc = "Hard limit on the number of per-process connections (imposed by Memmax_MB or Ulimit-n)" },
108 [INF_CURR_CONN] = { .name = "CurrConns", .desc = "Current number of connections on this worker process" },
109 [INF_CUM_CONN] = { .name = "CumConns", .desc = "Total number of connections on this worker process since started" },
110 [INF_CUM_REQ] = { .name = "CumReq", .desc = "Total number of requests on this worker process since started" },
111 [INF_MAX_SSL_CONNS] = { .name = "MaxSslConns", .desc = "Hard limit on the number of per-process SSL endpoints (front+back), 0=unlimited" },
112 [INF_CURR_SSL_CONNS] = { .name = "CurrSslConns", .desc = "Current number of SSL endpoints on this worker process (front+back)" },
113 [INF_CUM_SSL_CONNS] = { .name = "CumSslConns", .desc = "Total number of SSL endpoints on this worker process since started (front+back)" },
114 [INF_MAXPIPES] = { .name = "Maxpipes", .desc = "Hard limit on the number of pipes for splicing, 0=unlimited" },
115 [INF_PIPES_USED] = { .name = "PipesUsed", .desc = "Current number of pipes in use in this worker process" },
116 [INF_PIPES_FREE] = { .name = "PipesFree", .desc = "Current number of allocated and available pipes in this worker process" },
117 [INF_CONN_RATE] = { .name = "ConnRate", .desc = "Number of front connections created on this worker process over the last second" },
118 [INF_CONN_RATE_LIMIT] = { .name = "ConnRateLimit", .desc = "Hard limit for ConnRate (global.maxconnrate)" },
119 [INF_MAX_CONN_RATE] = { .name = "MaxConnRate", .desc = "Highest ConnRate reached on this worker process since started (in connections per second)" },
120 [INF_SESS_RATE] = { .name = "SessRate", .desc = "Number of sessions created on this worker process over the last second" },
121 [INF_SESS_RATE_LIMIT] = { .name = "SessRateLimit", .desc = "Hard limit for SessRate (global.maxsessrate)" },
122 [INF_MAX_SESS_RATE] = { .name = "MaxSessRate", .desc = "Highest SessRate reached on this worker process since started (in sessions per second)" },
123 [INF_SSL_RATE] = { .name = "SslRate", .desc = "Number of SSL connections created on this worker process over the last second" },
124 [INF_SSL_RATE_LIMIT] = { .name = "SslRateLimit", .desc = "Hard limit for SslRate (global.maxsslrate)" },
125 [INF_MAX_SSL_RATE] = { .name = "MaxSslRate", .desc = "Highest SslRate reached on this worker process since started (in connections per second)" },
126 [INF_SSL_FRONTEND_KEY_RATE] = { .name = "SslFrontendKeyRate", .desc = "Number of SSL keys created on frontends in this worker process over the last second" },
127 [INF_SSL_FRONTEND_MAX_KEY_RATE] = { .name = "SslFrontendMaxKeyRate", .desc = "Highest SslFrontendKeyRate reached on this worker process since started (in SSL keys per second)" },
128 [INF_SSL_FRONTEND_SESSION_REUSE_PCT] = { .name = "SslFrontendSessionReuse_pct", .desc = "Percent of frontend SSL connections which did not require a new key" },
129 [INF_SSL_BACKEND_KEY_RATE] = { .name = "SslBackendKeyRate", .desc = "Number of SSL keys created on backends in this worker process over the last second" },
130 [INF_SSL_BACKEND_MAX_KEY_RATE] = { .name = "SslBackendMaxKeyRate", .desc = "Highest SslBackendKeyRate reached on this worker process since started (in SSL keys per second)" },
131 [INF_SSL_CACHE_LOOKUPS] = { .name = "SslCacheLookups", .desc = "Total number of SSL session ID lookups in the SSL session cache on this worker since started" },
132 [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" },
133 [INF_COMPRESS_BPS_IN] = { .name = "CompressBpsIn", .desc = "Number of bytes submitted to the HTTP compressor in this worker process over the last second" },
134 [INF_COMPRESS_BPS_OUT] = { .name = "CompressBpsOut", .desc = "Number of bytes emitted by the HTTP compressor in this worker process over the last second" },
135 [INF_COMPRESS_BPS_RATE_LIM] = { .name = "CompressBpsRateLim", .desc = "Limit of CompressBpsOut beyond which HTTP compression is automatically disabled" },
136 [INF_ZLIB_MEM_USAGE] = { .name = "ZlibMemUsage", .desc = "Amount of memory currently used by HTTP compression on the current worker process (in bytes)" },
137 [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)" },
138 [INF_TASKS] = { .name = "Tasks", .desc = "Total number of tasks in the current worker process (active + sleeping)" },
139 [INF_RUN_QUEUE] = { .name = "Run_queue", .desc = "Total number of active tasks+tasklets in the current worker process" },
140 [INF_IDLE_PCT] = { .name = "Idle_pct", .desc = "Percentage of last second spent waiting in the current worker thread" },
141 [INF_NODE] = { .name = "node", .desc = "Node name (global.node)" },
142 [INF_DESCRIPTION] = { .name = "description", .desc = "Node description (global.description)" },
143 [INF_STOPPING] = { .name = "Stopping", .desc = "1 if the worker process is currently stopping, otherwise zero" },
144 [INF_JOBS] = { .name = "Jobs", .desc = "Current number of active jobs on the current worker process (frontend connections, master connections, listeners)" },
145 [INF_UNSTOPPABLE_JOBS] = { .name = "Unstoppable Jobs", .desc = "Current number of unstoppable jobs on the current worker process (master connections)" },
146 [INF_LISTENERS] = { .name = "Listeners", .desc = "Current number of active listeners on the current worker process" },
147 [INF_ACTIVE_PEERS] = { .name = "ActivePeers", .desc = "Current number of verified active peers connections on the current worker process" },
148 [INF_CONNECTED_PEERS] = { .name = "ConnectedPeers", .desc = "Current number of peers having passed the connection step on the current worker process" },
149 [INF_DROPPED_LOGS] = { .name = "DroppedLogs", .desc = "Total number of dropped logs for current worker process since started" },
150 [INF_BUSY_POLLING] = { .name = "BusyPolling", .desc = "1 if busy-polling is currently in use on the worker process, otherwise zero (config.busy-polling)" },
151 [INF_FAILED_RESOLUTIONS] = { .name = "FailedResolutions", .desc = "Total number of failed DNS resolutions in current worker process since started" },
152 [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 +0200153 [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 +0200154 [INF_BYTES_OUT_RATE] = { .name = "BytesOutRate", .desc = "Number of bytes emitted by current worker process over the last second" },
Willy Tarreau9b013702019-10-24 18:18:02 +0200155 [INF_DEBUG_COMMANDS_ISSUED] = { .name = "DebugCommandsIssued", .desc = "Number of debug commands issued on this process (anything > 0 is unsafe)" },
Emeric Brun45c457a2020-07-09 23:23:34 +0200156 [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 +0100157 [INF_BUILD_INFO] = { .name = "Build info", .desc = "Build info" },
Amaury Denoyelle5dfdf3e2021-05-05 17:09:12 +0200158 [INF_TAINTED] = { .name = "Tainted", .desc = "Experimental features used" },
Willy Tarreau0baac8c2016-11-22 16:36:53 +0100159};
160
Willy Tarreaueaa55372019-10-09 07:39:11 +0200161const struct name_desc stat_fields[ST_F_TOTAL_FIELDS] = {
Willy Tarreau6d4897e2019-10-11 16:31:46 +0200162 [ST_F_PXNAME] = { .name = "pxname", .desc = "Proxy name" },
163 [ST_F_SVNAME] = { .name = "svname", .desc = "Server name" },
William Dauchyeedb9b12021-02-01 13:11:53 +0100164 [ST_F_QCUR] = { .name = "qcur", .desc = "Number of current queued connections" },
165 [ST_F_QMAX] = { .name = "qmax", .desc = "Highest value of queued connections encountered since process started" },
William Dauchy19f7cfc2021-02-01 13:11:54 +0100166 [ST_F_SCUR] = { .name = "scur", .desc = "Number of current sessions on the frontend, backend or server" },
167 [ST_F_SMAX] = { .name = "smax", .desc = "Highest value of current sessions encountered since process started" },
Willy Tarreau6d4897e2019-10-11 16:31:46 +0200168 [ST_F_SLIM] = { .name = "slim", .desc = "Frontend/listener/server's maxconn, backend's fullconn" },
169 [ST_F_STOT] = { .name = "stot", .desc = "Total number of sessions since process started" },
170 [ST_F_BIN] = { .name = "bin", .desc = "Total number of request bytes since process started" },
171 [ST_F_BOUT] = { .name = "bout", .desc = "Total number of response bytes since process started" },
172 [ST_F_DREQ] = { .name = "dreq", .desc = "Total number of denied requests since process started" },
173 [ST_F_DRESP] = { .name = "dresp", .desc = "Total number of denied responses since process started" },
174 [ST_F_EREQ] = { .name = "ereq", .desc = "Total number of invalid requests since process started" },
175 [ST_F_ECON] = { .name = "econ", .desc = "Total number of failed connections to server since the worker process started" },
176 [ST_F_ERESP] = { .name = "eresp", .desc = "Total number of invalid responses since the worker process started" },
177 [ST_F_WRETR] = { .name = "wretr", .desc = "Total number of server connection retries since the worker process started" },
178 [ST_F_WREDIS] = { .name = "wredis", .desc = "Total number of server redispatches due to connection failures since the worker process started" },
179 [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 +0200180 [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 +0200181 [ST_F_ACT] = { .name = "act", .desc = "Total number of active UP servers with a non-zero weight" },
182 [ST_F_BCK] = { .name = "bck", .desc = "Total number of backup UP servers with a non-zero weight" },
183 [ST_F_CHKFAIL] = { .name = "chkfail", .desc = "Total number of failed individual health checks per server/backend, since the worker process started" },
184 [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" },
185 [ST_F_LASTCHG] = { .name = "lastchg", .desc = "How long ago the last server state changed, in seconds" },
186 [ST_F_DOWNTIME] = { .name = "downtime", .desc = "Total time spent in DOWN state, for server or backend" },
187 [ST_F_QLIMIT] = { .name = "qlimit", .desc = "Limit on the number of connections in queue, for servers only (maxqueue argument)" },
Willy Tarreaue8422bf2021-06-15 09:08:18 +0200188 [ST_F_PID] = { .name = "pid", .desc = "Relative worker process number (1)" },
Willy Tarreau6d4897e2019-10-11 16:31:46 +0200189 [ST_F_IID] = { .name = "iid", .desc = "Frontend or Backend numeric identifier ('id' setting)" },
190 [ST_F_SID] = { .name = "sid", .desc = "Server numeric identifier ('id' setting)" },
191 [ST_F_THROTTLE] = { .name = "throttle", .desc = "Throttling ratio applied to a server's maxconn and weight during the slowstart period (0 to 100%)" },
192 [ST_F_LBTOT] = { .name = "lbtot", .desc = "Total number of requests routed by load balancing since the worker process started (ignores queue pop and stickiness)" },
193 [ST_F_TRACKED] = { .name = "tracked", .desc = "Name of the other server this server tracks for its state" },
194 [ST_F_TYPE] = { .name = "type", .desc = "Type of the object (Listener, Frontend, Backend, Server)" },
195 [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)" },
196 [ST_F_RATE_LIM] = { .name = "rate_lim", .desc = "Limit on the number of sessions accepted in a second (frontend only, 'rate-limit sessions' setting)" },
William Dauchy19f7cfc2021-02-01 13:11:54 +0100197 [ST_F_RATE_MAX] = { .name = "rate_max", .desc = "Highest value of sessions per second observed since the worker process started" },
Willy Tarreau6d4897e2019-10-11 16:31:46 +0200198 [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" },
199 [ST_F_CHECK_CODE] = { .name = "check_code", .desc = "HTTP/SMTP/LDAP status code reported by the latest server health check" },
200 [ST_F_CHECK_DURATION] = { .name = "check_duration", .desc = "Total duration of the latest server health check, in milliseconds" },
201 [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" },
202 [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" },
203 [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" },
204 [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" },
205 [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" },
206 [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)" },
207 [ST_F_HANAFAIL] = { .name = "hanafail", .desc = "Total number of failed checks caused by an 'on-error' directive after an 'observe' condition matched" },
208 [ST_F_REQ_RATE] = { .name = "req_rate", .desc = "Number of HTTP requests processed over the last second on this object" },
William Dauchy19f7cfc2021-02-01 13:11:54 +0100209 [ST_F_REQ_RATE_MAX] = { .name = "req_rate_max", .desc = "Highest value of http requests observed since the worker process started" },
Willy Tarreau6d4897e2019-10-11 16:31:46 +0200210 [ST_F_REQ_TOT] = { .name = "req_tot", .desc = "Total number of HTTP requests processed by this object since the worker process started" },
211 [ST_F_CLI_ABRT] = { .name = "cli_abrt", .desc = "Total number of requests or connections aborted by the client since the worker process started" },
212 [ST_F_SRV_ABRT] = { .name = "srv_abrt", .desc = "Total number of requests or connections aborted by the server since the worker process started" },
213 [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" },
214 [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" },
215 [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)" },
216 [ST_F_COMP_RSP] = { .name = "comp_rsp", .desc = "Total number of HTTP responses that were compressed for this object since the worker process started" },
217 [ST_F_LASTSESS] = { .name = "lastsess", .desc = "How long ago some traffic was seen on this object on this worker process, in seconds" },
218 [ST_F_LAST_CHK] = { .name = "last_chk", .desc = "Short description of the latest health check report for this server (see also check_desc)" },
219 [ST_F_LAST_AGT] = { .name = "last_agt", .desc = "Short description of the latest agent check report for this server (see also agent_desc)" },
220 [ST_F_QTIME] = { .name = "qtime", .desc = "Time spent in the queue, in milliseconds, averaged over the 1024 last requests (backend/server)" },
221 [ST_F_CTIME] = { .name = "ctime", .desc = "Time spent waiting for a connection to complete, in milliseconds, averaged over the 1024 last requests (backend/server)" },
222 [ST_F_RTIME] = { .name = "rtime", .desc = "Time spent waiting for a server response, in milliseconds, averaged over the 1024 last requests (backend/server)" },
223 [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)" },
224 [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" },
225 [ST_F_AGENT_CODE] = { .name = "agent_code", .desc = "Status code reported by the latest server agent check" },
226 [ST_F_AGENT_DURATION] = { .name = "agent_duration", .desc = "Total duration of the latest server agent check, in milliseconds" },
227 [ST_F_CHECK_DESC] = { .name = "check_desc", .desc = "Textual description of the latest health check report for this server" },
228 [ST_F_AGENT_DESC] = { .name = "agent_desc", .desc = "Textual description of the latest agent check report for this server" },
229 [ST_F_CHECK_RISE] = { .name = "check_rise", .desc = "Number of successful health checks before declaring a server UP (server 'rise' setting)" },
230 [ST_F_CHECK_FALL] = { .name = "check_fall", .desc = "Number of failed health checks before declaring a server DOWN (server 'fall' setting)" },
231 [ST_F_CHECK_HEALTH] = { .name = "check_health", .desc = "Current server health check level (0..fall-1=DOWN, fall..rise-1=UP)" },
232 [ST_F_AGENT_RISE] = { .name = "agent_rise", .desc = "Number of successful agent checks before declaring a server UP (server 'rise' setting)" },
233 [ST_F_AGENT_FALL] = { .name = "agent_fall", .desc = "Number of failed agent checks before declaring a server DOWN (server 'fall' setting)" },
234 [ST_F_AGENT_HEALTH] = { .name = "agent_health", .desc = "Current server agent check level (0..fall-1=DOWN, fall..rise-1=UP)" },
235 [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" },
236 [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" },
237 [ST_F_MODE] = { .name = "mode", .desc = "'mode' setting (tcp/http/health/cli)" },
238 [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" },
239 [ST_F_CONN_RATE] = { .name = "conn_rate", .desc = "Number of new connections accepted over the last second on the frontend for this worker process" },
William Dauchy19f7cfc2021-02-01 13:11:54 +0100240 [ST_F_CONN_RATE_MAX] = { .name = "conn_rate_max", .desc = "Highest value of connections per second observed since the worker process started" },
Willy Tarreau6d4897e2019-10-11 16:31:46 +0200241 [ST_F_CONN_TOT] = { .name = "conn_tot", .desc = "Total number of new connections accepted on this frontend since the worker process started" },
242 [ST_F_INTERCEPTED] = { .name = "intercepted", .desc = "Total number of HTTP requests intercepted on the frontend (redirects/stats/services) since the worker process started" },
243 [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" },
244 [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" },
245 [ST_F_WREW] = { .name = "wrew", .desc = "Total number of failed HTTP header rewrites since the worker process started" },
246 [ST_F_CONNECT] = { .name = "connect", .desc = "Total number of outgoing connection attempts on this backend/server since the worker process started" },
247 [ST_F_REUSE] = { .name = "reuse", .desc = "Total number of reused connection on this backend/server since the worker process started" },
248 [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" },
249 [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" },
250 [ST_F_SRV_ICUR] = { .name = "srv_icur", .desc = "Current number of idle connections available for reuse on this server" },
251 [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 +0100252 [ST_F_QT_MAX] = { .name = "qtime_max", .desc = "Maximum observed time spent in the queue, in milliseconds (backend/server)" },
253 [ST_F_CT_MAX] = { .name = "ctime_max", .desc = "Maximum observed time spent waiting for a connection to complete, in milliseconds (backend/server)" },
254 [ST_F_RT_MAX] = { .name = "rtime_max", .desc = "Maximum observed time spent waiting for a server response, in milliseconds (backend/server)" },
255 [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 +0100256 [ST_F_EINT] = { .name = "eint", .desc = "Total number of internal errors since process started"},
Willy Tarreau3bb617c2020-06-29 13:51:05 +0200257 [ST_F_IDLE_CONN_CUR] = { .name = "idle_conn_cur", .desc = "Current number of unsafe idle connections"},
258 [ST_F_SAFE_CONN_CUR] = { .name = "safe_conn_cur", .desc = "Current number of safe idle connections"},
259 [ST_F_USED_CONN_CUR] = { .name = "used_conn_cur", .desc = "Current number of connections in use"},
Willy Tarreaua9fcecb2020-06-29 15:38:53 +0200260 [ST_F_NEED_CONN_EST] = { .name = "need_conn_est", .desc = "Estimated needed number of connections"},
Willy Tarreaubd715102020-10-23 22:44:30 +0200261 [ST_F_UWEIGHT] = { .name = "uweight", .desc = "Server's user weight, or sum of active servers' user weights for a backend" },
Cedric Paillet7d6644e2022-12-08 09:17:00 +0000262 [ST_F_AGG_SRV_CHECK_STATUS] = { .name = "agg_server_check_status", .desc = "[DEPRECATED] Backend's aggregated gauge of servers' status" },
263 [ST_F_AGG_SRV_STATUS ] = { .name = "agg_server_status", .desc = "Backend's aggregated gauge of servers' status" },
Cedric Paillete06e31e2022-12-08 09:17:01 +0000264 [ST_F_AGG_CHECK_STATUS] = { .name = "agg_check_status", .desc = "Backend's aggregated gauge of servers' state check status" },
Aurelien DARRAGON745ce8e2022-11-17 16:34:07 +0100265 [ST_F_SRID] = { .name = "srid", .desc = "Server id revision, to prevent server id reuse mixups" },
William Lallemand74c24fb2016-11-21 17:18:36 +0100266};
267
Willy Tarreau0baac8c2016-11-22 16:36:53 +0100268/* one line of info */
William Dauchy5d9b8f32021-01-11 20:07:49 +0100269THREAD_LOCAL struct field info[INF_TOTAL_FIELDS];
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +0200270
271/* description of statistics (static and dynamic) */
272static struct name_desc *stat_f[STATS_DOMAIN_COUNT];
273static size_t stat_count[STATS_DOMAIN_COUNT];
274
275/* one line for stats */
William Dauchyb9577452021-01-17 18:27:46 +0100276THREAD_LOCAL struct field *stat_l[STATS_DOMAIN_COUNT];
William Lallemand74c24fb2016-11-21 17:18:36 +0100277
Amaury Denoyelle58d395e2020-10-05 11:49:40 +0200278/* list of all registered stats module */
279static struct list stats_module_list[STATS_DOMAIN_COUNT] = {
280 LIST_HEAD_INIT(stats_module_list[STATS_DOMAIN_PROXY]),
Emeric Brunf8642ee2021-10-29 17:59:18 +0200281 LIST_HEAD_INIT(stats_module_list[STATS_DOMAIN_RESOLVERS]),
Amaury Denoyelle58d395e2020-10-05 11:49:40 +0200282};
283
Amaury Denoyelle7f8f6cb2020-11-10 14:24:31 +0100284THREAD_LOCAL void *trash_counters;
285
Amaury Denoyelle072f97e2020-10-05 11:49:37 +0200286static inline uint8_t stats_get_domain(uint32_t domain)
287{
288 return domain >> STATS_DOMAIN & STATS_DOMAIN_MASK;
289}
290
Amaury Denoyelle72b16e52020-10-05 11:49:38 +0200291static inline enum stats_domain_px_cap stats_px_get_cap(uint32_t domain)
292{
293 return domain >> STATS_PX_CAP & STATS_PX_CAP_MASK;
294}
295
Christopher Faulet6338a082019-09-09 15:50:54 +0200296static void stats_dump_json_schema(struct buffer *out);
William Lallemand74c24fb2016-11-21 17:18:36 +0100297
Amaury Denoyelle3ca927e2020-10-02 18:32:00 +0200298int stats_putchk(struct channel *chn, struct htx *htx, struct buffer *chk)
Christopher Fauletef779222018-10-31 08:47:01 +0100299{
300 if (htx) {
Christopher Faulet69fc88c2019-01-07 14:27:53 +0100301 if (chk->data >= channel_htx_recv_max(chn, htx))
302 return 0;
Willy Tarreau0a7ef022019-05-28 10:30:11 +0200303 if (!htx_add_data_atonce(htx, ist2(chk->area, chk->data)))
Christopher Fauletef779222018-10-31 08:47:01 +0100304 return 0;
Christopher Faulet5adbeeb2019-01-02 14:34:39 +0100305 channel_add_input(chn, chk->data);
Christopher Fauletef779222018-10-31 08:47:01 +0100306 chk->data = 0;
Christopher Fauletef779222018-10-31 08:47:01 +0100307 }
308 else {
309 if (ci_putchk(chn, chk) == -1)
310 return 0;
311 }
312 return 1;
313}
Willy Tarreau0baac8c2016-11-22 16:36:53 +0100314
Willy Tarreaucaff6312022-05-27 10:17:46 +0200315static const char *stats_scope_ptr(struct appctx *appctx, struct stconn *sc)
Christopher Fauleted7a0662019-01-14 11:07:34 +0100316{
Willy Tarreau91cefca2022-05-03 17:08:29 +0200317 struct show_stat_ctx *ctx = appctx->svcctx;
Willy Tarreaucaff6312022-05-27 10:17:46 +0200318 struct channel *req = sc_oc(sc);
Christopher Fauletb7f88902019-07-15 21:56:43 +0200319 struct htx *htx = htxbuf(&req->buf);
320 struct htx_blk *blk;
321 struct ist uri;
Christopher Fauleted7a0662019-01-14 11:07:34 +0100322
Christopher Fauletb7f88902019-07-15 21:56:43 +0200323 blk = htx_get_head_blk(htx);
Christopher Fauletea009732019-11-18 15:50:25 +0100324 BUG_ON(!blk || htx_get_blk_type(blk) != HTX_BLK_REQ_SL);
Christopher Fauletb7f88902019-07-15 21:56:43 +0200325 ALREADY_CHECKED(blk);
326 uri = htx_sl_req_uri(htx_get_blk_ptr(htx, blk));
Willy Tarreau91cefca2022-05-03 17:08:29 +0200327 return uri.ptr + ctx->scope_str;
Christopher Fauleted7a0662019-01-14 11:07:34 +0100328}
329
William Lallemand74c24fb2016-11-21 17:18:36 +0100330/*
331 * http_stats_io_handler()
332 * -> stats_dump_stat_to_buffer() // same as above, but used for CSV or HTML
333 * -> stats_dump_csv_header() // emits the CSV headers (same as above)
Simon Horman05ee2132017-01-04 09:37:25 +0100334 * -> stats_dump_json_header() // emits the JSON headers (same as above)
William Lallemand74c24fb2016-11-21 17:18:36 +0100335 * -> stats_dump_html_head() // emits the HTML headers
336 * -> stats_dump_html_info() // emits the equivalent of "show info" at the top
337 * -> stats_dump_proxy_to_buffer() // same as above, valid for CSV and HTML
338 * -> stats_dump_html_px_hdr()
339 * -> stats_dump_fe_stats()
340 * -> stats_dump_li_stats()
341 * -> stats_dump_sv_stats()
342 * -> stats_dump_be_stats()
343 * -> stats_dump_html_px_end()
344 * -> stats_dump_html_end() // emits HTML trailer
Simon Horman05ee2132017-01-04 09:37:25 +0100345 * -> stats_dump_json_end() // emits JSON trailer
William Lallemand74c24fb2016-11-21 17:18:36 +0100346 */
347
348
William Lallemand74c24fb2016-11-21 17:18:36 +0100349/* Dumps the stats CSV header to the trash buffer which. The caller is responsible
350 * for clearing it if needed.
351 * NOTE: Some tools happen to rely on the field position instead of its name,
352 * so please only append new fields at the end, never in the middle.
353 */
Amaury Denoyelle50660a82020-10-05 11:49:39 +0200354static void stats_dump_csv_header(enum stats_domain domain)
William Lallemand74c24fb2016-11-21 17:18:36 +0100355{
356 int field;
357
358 chunk_appendf(&trash, "# ");
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +0200359 if (stat_f[domain]) {
360 for (field = 0; field < stat_count[domain]; ++field) {
361 chunk_appendf(&trash, "%s,", stat_f[domain][field].name);
William Lallemand74c24fb2016-11-21 17:18:36 +0100362
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +0200363 /* print special delimiter on proxy stats to mark end of
364 static fields */
365 if (domain == STATS_DOMAIN_PROXY && field + 1 == ST_F_TOTAL_FIELDS)
366 chunk_appendf(&trash, "-,");
Amaury Denoyelle50660a82020-10-05 11:49:39 +0200367 }
368 }
369
William Lallemand74c24fb2016-11-21 17:18:36 +0100370 chunk_appendf(&trash, "\n");
371}
372
William Lallemand74c24fb2016-11-21 17:18:36 +0100373/* Emits a stats field without any surrounding element and properly encoded to
374 * resist CSV output. Returns non-zero on success, 0 if the buffer is full.
375 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200376int stats_emit_raw_data_field(struct buffer *out, const struct field *f)
William Lallemand74c24fb2016-11-21 17:18:36 +0100377{
378 switch (field_format(f, 0)) {
379 case FF_EMPTY: return 1;
380 case FF_S32: return chunk_appendf(out, "%d", f->u.s32);
381 case FF_U32: return chunk_appendf(out, "%u", f->u.u32);
382 case FF_S64: return chunk_appendf(out, "%lld", (long long)f->u.s64);
383 case FF_U64: return chunk_appendf(out, "%llu", (unsigned long long)f->u.u64);
Willy Tarreau065ba312021-05-08 10:38:20 +0200384 case FF_FLT: {
385 size_t prev_data = out->data;
386 out->data = flt_trim(out->area, prev_data, chunk_appendf(out, "%f", f->u.flt));
387 return out->data;
388 }
William Lallemand74c24fb2016-11-21 17:18:36 +0100389 case FF_STR: return csv_enc_append(field_str(f, 0), 1, out) != NULL;
390 default: return chunk_appendf(out, "[INCORRECT_FIELD_TYPE_%08x]", f->type);
391 }
392}
393
Amaury Denoyellee3f576c2020-10-05 11:49:44 +0200394const char *field_to_html_str(const struct field *f)
395{
396 switch (field_format(f, 0)) {
397 case FF_S32: return U2H(f->u.s32);
398 case FF_S64: return U2H(f->u.s64);
399 case FF_U64: return U2H(f->u.u64);
400 case FF_U32: return U2H(f->u.u32);
Willy Tarreau6004fb72021-05-08 07:37:38 +0200401 case FF_FLT: return F2H(f->u.flt);
Amaury Denoyellee3f576c2020-10-05 11:49:44 +0200402 case FF_STR: return field_str(f, 0);
403 case FF_EMPTY:
404 default:
405 return "";
406 }
407}
408
William Lallemand74c24fb2016-11-21 17:18:36 +0100409/* Emits a stats field prefixed with its type. No CSV encoding is prepared, the
410 * output is supposed to be used on its own line. Returns non-zero on success, 0
411 * if the buffer is full.
412 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200413int stats_emit_typed_data_field(struct buffer *out, const struct field *f)
William Lallemand74c24fb2016-11-21 17:18:36 +0100414{
415 switch (field_format(f, 0)) {
416 case FF_EMPTY: return 1;
417 case FF_S32: return chunk_appendf(out, "s32:%d", f->u.s32);
418 case FF_U32: return chunk_appendf(out, "u32:%u", f->u.u32);
419 case FF_S64: return chunk_appendf(out, "s64:%lld", (long long)f->u.s64);
420 case FF_U64: return chunk_appendf(out, "u64:%llu", (unsigned long long)f->u.u64);
Willy Tarreau065ba312021-05-08 10:38:20 +0200421 case FF_FLT: {
422 size_t prev_data = out->data;
423 out->data = flt_trim(out->area, prev_data, chunk_appendf(out, "flt:%f", f->u.flt));
424 return out->data;
425 }
William Lallemand74c24fb2016-11-21 17:18:36 +0100426 case FF_STR: return chunk_appendf(out, "str:%s", field_str(f, 0));
427 default: return chunk_appendf(out, "%08x:?", f->type);
428 }
429}
430
Simon Horman05ee2132017-01-04 09:37:25 +0100431/* Limit JSON integer values to the range [-(2**53)+1, (2**53)-1] as per
432 * the recommendation for interoperable integers in section 6 of RFC 7159.
433 */
434#define JSON_INT_MAX ((1ULL << 53) - 1)
435#define JSON_INT_MIN (0 - JSON_INT_MAX)
436
437/* Emits a stats field value and its type in JSON.
438 * Returns non-zero on success, 0 on error.
439 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200440int stats_emit_json_data_field(struct buffer *out, const struct field *f)
Simon Horman05ee2132017-01-04 09:37:25 +0100441{
442 int old_len;
443 char buf[20];
444 const char *type, *value = buf, *quote = "";
445
446 switch (field_format(f, 0)) {
447 case FF_EMPTY: return 1;
448 case FF_S32: type = "\"s32\"";
449 snprintf(buf, sizeof(buf), "%d", f->u.s32);
450 break;
451 case FF_U32: type = "\"u32\"";
452 snprintf(buf, sizeof(buf), "%u", f->u.u32);
453 break;
454 case FF_S64: type = "\"s64\"";
455 if (f->u.s64 < JSON_INT_MIN || f->u.s64 > JSON_INT_MAX)
456 return 0;
457 type = "\"s64\"";
458 snprintf(buf, sizeof(buf), "%lld", (long long)f->u.s64);
459 break;
460 case FF_U64: if (f->u.u64 > JSON_INT_MAX)
461 return 0;
462 type = "\"u64\"";
463 snprintf(buf, sizeof(buf), "%llu",
464 (unsigned long long) f->u.u64);
Christopher Faulet52c91bb2019-09-28 10:37:31 +0200465 break;
Christopher Faulet88a0db22019-09-24 16:35:10 +0200466 case FF_FLT: type = "\"flt\"";
Willy Tarreau065ba312021-05-08 10:38:20 +0200467 flt_trim(buf, 0, snprintf(buf, sizeof(buf), "%f", f->u.flt));
Simon Horman05ee2132017-01-04 09:37:25 +0100468 break;
469 case FF_STR: type = "\"str\"";
470 value = field_str(f, 0);
471 quote = "\"";
472 break;
473 default: snprintf(buf, sizeof(buf), "%u", f->type);
474 type = buf;
475 value = "unknown";
476 quote = "\"";
477 break;
478 }
479
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200480 old_len = out->data;
Simon Horman05ee2132017-01-04 09:37:25 +0100481 chunk_appendf(out, ",\"value\":{\"type\":%s,\"value\":%s%s%s}",
482 type, quote, value, quote);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200483 return !(old_len == out->data);
Simon Horman05ee2132017-01-04 09:37:25 +0100484}
485
William Lallemand74c24fb2016-11-21 17:18:36 +0100486/* Emits an encoding of the field type on 3 characters followed by a delimiter.
487 * Returns non-zero on success, 0 if the buffer is full.
488 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200489int stats_emit_field_tags(struct buffer *out, const struct field *f,
490 char delim)
William Lallemand74c24fb2016-11-21 17:18:36 +0100491{
492 char origin, nature, scope;
493
494 switch (field_origin(f, 0)) {
495 case FO_METRIC: origin = 'M'; break;
496 case FO_STATUS: origin = 'S'; break;
497 case FO_KEY: origin = 'K'; break;
498 case FO_CONFIG: origin = 'C'; break;
499 case FO_PRODUCT: origin = 'P'; break;
500 default: origin = '?'; break;
501 }
502
503 switch (field_nature(f, 0)) {
504 case FN_GAUGE: nature = 'G'; break;
505 case FN_LIMIT: nature = 'L'; break;
506 case FN_MIN: nature = 'm'; break;
507 case FN_MAX: nature = 'M'; break;
508 case FN_RATE: nature = 'R'; break;
509 case FN_COUNTER: nature = 'C'; break;
510 case FN_DURATION: nature = 'D'; break;
511 case FN_AGE: nature = 'A'; break;
512 case FN_TIME: nature = 'T'; break;
513 case FN_NAME: nature = 'N'; break;
514 case FN_OUTPUT: nature = 'O'; break;
515 case FN_AVG: nature = 'a'; break;
516 default: nature = '?'; break;
517 }
518
519 switch (field_scope(f, 0)) {
520 case FS_PROCESS: scope = 'P'; break;
521 case FS_SERVICE: scope = 'S'; break;
522 case FS_SYSTEM: scope = 's'; break;
523 case FS_CLUSTER: scope = 'C'; break;
524 default: scope = '?'; break;
525 }
526
527 return chunk_appendf(out, "%c%c%c%c", origin, nature, scope, delim);
528}
529
Simon Horman05ee2132017-01-04 09:37:25 +0100530/* Emits an encoding of the field type as JSON.
531 * Returns non-zero on success, 0 if the buffer is full.
532 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200533int stats_emit_json_field_tags(struct buffer *out, const struct field *f)
Simon Horman05ee2132017-01-04 09:37:25 +0100534{
535 const char *origin, *nature, *scope;
536 int old_len;
537
538 switch (field_origin(f, 0)) {
539 case FO_METRIC: origin = "Metric"; break;
540 case FO_STATUS: origin = "Status"; break;
541 case FO_KEY: origin = "Key"; break;
542 case FO_CONFIG: origin = "Config"; break;
543 case FO_PRODUCT: origin = "Product"; break;
544 default: origin = "Unknown"; break;
545 }
546
547 switch (field_nature(f, 0)) {
548 case FN_GAUGE: nature = "Gauge"; break;
549 case FN_LIMIT: nature = "Limit"; break;
550 case FN_MIN: nature = "Min"; break;
551 case FN_MAX: nature = "Max"; break;
552 case FN_RATE: nature = "Rate"; break;
553 case FN_COUNTER: nature = "Counter"; break;
554 case FN_DURATION: nature = "Duration"; break;
555 case FN_AGE: nature = "Age"; break;
556 case FN_TIME: nature = "Time"; break;
557 case FN_NAME: nature = "Name"; break;
558 case FN_OUTPUT: nature = "Output"; break;
559 case FN_AVG: nature = "Avg"; break;
560 default: nature = "Unknown"; break;
561 }
562
563 switch (field_scope(f, 0)) {
564 case FS_PROCESS: scope = "Process"; break;
565 case FS_SERVICE: scope = "Service"; break;
566 case FS_SYSTEM: scope = "System"; break;
567 case FS_CLUSTER: scope = "Cluster"; break;
568 default: scope = "Unknown"; break;
569 }
570
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200571 old_len = out->data;
Simon Horman05ee2132017-01-04 09:37:25 +0100572 chunk_appendf(out, "\"tags\":{"
573 "\"origin\":\"%s\","
574 "\"nature\":\"%s\","
575 "\"scope\":\"%s\""
576 "}", origin, nature, scope);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200577 return !(old_len == out->data);
Simon Horman05ee2132017-01-04 09:37:25 +0100578}
William Lallemand74c24fb2016-11-21 17:18:36 +0100579
580/* Dump all fields from <stats> into <out> using CSV format */
Willy Tarreau83061a82018-07-13 11:56:34 +0200581static int stats_dump_fields_csv(struct buffer *out,
Amaury Denoyelle97323c92020-10-02 18:32:01 +0200582 const struct field *stats, size_t stats_count,
Amaury Denoyelle50660a82020-10-05 11:49:39 +0200583 unsigned int flags,
584 enum stats_domain domain)
William Lallemand74c24fb2016-11-21 17:18:36 +0100585{
586 int field;
587
Amaury Denoyelle50660a82020-10-05 11:49:39 +0200588 for (field = 0; field < stats_count; ++field) {
William Lallemand74c24fb2016-11-21 17:18:36 +0100589 if (!stats_emit_raw_data_field(out, &stats[field]))
590 return 0;
591 if (!chunk_strcat(out, ","))
592 return 0;
Amaury Denoyelle50660a82020-10-05 11:49:39 +0200593
594 /* print special delimiter on proxy stats to mark end of
595 static fields */
596 if (domain == STATS_DOMAIN_PROXY && field + 1 == ST_F_TOTAL_FIELDS) {
597 if (!chunk_strcat(out, "-,"))
598 return 0;
599 }
William Lallemand74c24fb2016-11-21 17:18:36 +0100600 }
Amaury Denoyelle50660a82020-10-05 11:49:39 +0200601
William Lallemand74c24fb2016-11-21 17:18:36 +0100602 chunk_strcat(out, "\n");
603 return 1;
604}
605
606/* Dump all fields from <stats> into <out> using a typed "field:desc:type:value" format */
Willy Tarreau83061a82018-07-13 11:56:34 +0200607static int stats_dump_fields_typed(struct buffer *out,
Amaury Denoyelle97323c92020-10-02 18:32:01 +0200608 const struct field *stats,
609 size_t stats_count,
Amaury Denoyelle072f97e2020-10-05 11:49:37 +0200610 unsigned int flags,
611 enum stats_domain domain)
William Lallemand74c24fb2016-11-21 17:18:36 +0100612{
613 int field;
614
Amaury Denoyelle97323c92020-10-02 18:32:01 +0200615 for (field = 0; field < stats_count; ++field) {
William Lallemand74c24fb2016-11-21 17:18:36 +0100616 if (!stats[field].type)
617 continue;
618
Amaury Denoyelle072f97e2020-10-05 11:49:37 +0200619 switch (domain) {
620 case STATS_DOMAIN_PROXY:
621 chunk_appendf(out, "%c.%u.%u.%d.%s.%u:",
622 stats[ST_F_TYPE].u.u32 == STATS_TYPE_FE ? 'F' :
623 stats[ST_F_TYPE].u.u32 == STATS_TYPE_BE ? 'B' :
624 stats[ST_F_TYPE].u.u32 == STATS_TYPE_SO ? 'L' :
625 stats[ST_F_TYPE].u.u32 == STATS_TYPE_SV ? 'S' :
626 '?',
627 stats[ST_F_IID].u.u32, stats[ST_F_SID].u.u32,
628 field,
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +0200629 stat_f[domain][field].name,
Amaury Denoyelle072f97e2020-10-05 11:49:37 +0200630 stats[ST_F_PID].u.u32);
631 break;
632
Emeric Brunf8642ee2021-10-29 17:59:18 +0200633 case STATS_DOMAIN_RESOLVERS:
634 chunk_appendf(out, "N.%d.%s:", field,
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +0200635 stat_f[domain][field].name);
636 break;
637
Amaury Denoyelle072f97e2020-10-05 11:49:37 +0200638 default:
639 break;
640 }
William Lallemand74c24fb2016-11-21 17:18:36 +0100641
642 if (!stats_emit_field_tags(out, &stats[field], ':'))
643 return 0;
644 if (!stats_emit_typed_data_field(out, &stats[field]))
645 return 0;
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +0200646
Willy Tarreaufc8e4382021-06-17 07:22:27 +0200647 if (flags & STAT_SHOW_FDESC &&
648 !chunk_appendf(out, ":\"%s\"", stat_f[domain][field].desc)) {
Willy Tarreau6b19b142019-10-09 15:44:21 +0200649 return 0;
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +0200650 }
651
William Lallemand74c24fb2016-11-21 17:18:36 +0100652 if (!chunk_strcat(out, "\n"))
653 return 0;
654 }
655 return 1;
656}
657
Simon Horman05ee2132017-01-04 09:37:25 +0100658/* Dump all fields from <stats> into <out> using the "show info json" format */
Willy Tarreau83061a82018-07-13 11:56:34 +0200659static int stats_dump_json_info_fields(struct buffer *out,
Willy Tarreau43241ff2019-10-09 11:27:51 +0200660 const struct field *info, unsigned int flags)
Simon Horman05ee2132017-01-04 09:37:25 +0100661{
662 int field;
663 int started = 0;
664
665 if (!chunk_strcat(out, "["))
666 return 0;
667
668 for (field = 0; field < INF_TOTAL_FIELDS; field++) {
669 int old_len;
670
671 if (!field_format(info, field))
672 continue;
673
674 if (started && !chunk_strcat(out, ","))
675 goto err;
676 started = 1;
677
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200678 old_len = out->data;
Simon Horman05ee2132017-01-04 09:37:25 +0100679 chunk_appendf(out,
680 "{\"field\":{\"pos\":%d,\"name\":\"%s\"},"
681 "\"processNum\":%u,",
Willy Tarreaueaa55372019-10-09 07:39:11 +0200682 field, info_fields[field].name,
Simon Horman05ee2132017-01-04 09:37:25 +0100683 info[INF_PROCESS_NUM].u.u32);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200684 if (old_len == out->data)
Simon Horman05ee2132017-01-04 09:37:25 +0100685 goto err;
686
687 if (!stats_emit_json_field_tags(out, &info[field]))
688 goto err;
689
690 if (!stats_emit_json_data_field(out, &info[field]))
691 goto err;
692
693 if (!chunk_strcat(out, "}"))
694 goto err;
695 }
696
Willy Tarreauc6b7a972022-06-10 15:12:21 +0200697 if (!chunk_strcat(out, "]\n"))
Simon Horman05ee2132017-01-04 09:37:25 +0100698 goto err;
699 return 1;
700
701err:
702 chunk_reset(out);
Willy Tarreauc6b7a972022-06-10 15:12:21 +0200703 chunk_appendf(out, "{\"errorStr\":\"output buffer too short\"}\n");
Simon Horman05ee2132017-01-04 09:37:25 +0100704 return 0;
705}
706
Amaury Denoyellef34017b2020-10-02 18:32:03 +0200707static void stats_print_proxy_field_json(struct buffer *out,
708 const struct field *stat,
709 const char *name,
710 int pos,
711 uint32_t field_type,
712 uint32_t iid,
713 uint32_t sid,
714 uint32_t pid)
715{
716 const char *obj_type;
717 switch (field_type) {
718 case STATS_TYPE_FE: obj_type = "Frontend"; break;
719 case STATS_TYPE_BE: obj_type = "Backend"; break;
720 case STATS_TYPE_SO: obj_type = "Listener"; break;
721 case STATS_TYPE_SV: obj_type = "Server"; break;
722 default: obj_type = "Unknown"; break;
723 }
724
725 chunk_appendf(out,
726 "{"
727 "\"objType\":\"%s\","
728 "\"proxyId\":%u,"
729 "\"id\":%u,"
730 "\"field\":{\"pos\":%d,\"name\":\"%s\"},"
731 "\"processNum\":%u,",
732 obj_type, iid, sid, pos, name, pid);
733}
734
Emeric Brunf8642ee2021-10-29 17:59:18 +0200735static void stats_print_rslv_field_json(struct buffer *out,
736 const struct field *stat,
737 const char *name,
738 int pos)
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +0200739{
740 chunk_appendf(out,
741 "{"
742 "\"field\":{\"pos\":%d,\"name\":\"%s\"},",
743 pos, name);
744}
745
746
Simon Horman05ee2132017-01-04 09:37:25 +0100747/* Dump all fields from <stats> into <out> using a typed "field:desc:type:value" format */
Willy Tarreau83061a82018-07-13 11:56:34 +0200748static int stats_dump_fields_json(struct buffer *out,
Amaury Denoyelle97323c92020-10-02 18:32:01 +0200749 const struct field *stats, size_t stats_count,
Amaury Denoyelle072f97e2020-10-05 11:49:37 +0200750 unsigned int flags,
751 enum stats_domain domain)
Simon Horman05ee2132017-01-04 09:37:25 +0100752{
753 int field;
754 int started = 0;
755
Willy Tarreaub0ce3ad2019-10-09 11:19:29 +0200756 if ((flags & STAT_STARTED) && !chunk_strcat(out, ","))
Simon Horman05ee2132017-01-04 09:37:25 +0100757 return 0;
758 if (!chunk_strcat(out, "["))
759 return 0;
760
Amaury Denoyelle97323c92020-10-02 18:32:01 +0200761 for (field = 0; field < stats_count; field++) {
Simon Horman05ee2132017-01-04 09:37:25 +0100762 int old_len;
763
764 if (!stats[field].type)
765 continue;
766
767 if (started && !chunk_strcat(out, ","))
768 goto err;
769 started = 1;
770
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200771 old_len = out->data;
Amaury Denoyelle072f97e2020-10-05 11:49:37 +0200772 if (domain == STATS_DOMAIN_PROXY) {
773 stats_print_proxy_field_json(out, &stats[field],
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +0200774 stat_f[domain][field].name,
775 field,
Amaury Denoyelle072f97e2020-10-05 11:49:37 +0200776 stats[ST_F_TYPE].u.u32,
777 stats[ST_F_IID].u.u32,
778 stats[ST_F_SID].u.u32,
779 stats[ST_F_PID].u.u32);
Emeric Brunf8642ee2021-10-29 17:59:18 +0200780 } else if (domain == STATS_DOMAIN_RESOLVERS) {
781 stats_print_rslv_field_json(out, &stats[field],
782 stat_f[domain][field].name,
783 field);
Amaury Denoyelle072f97e2020-10-05 11:49:37 +0200784 }
Amaury Denoyellef34017b2020-10-02 18:32:03 +0200785
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200786 if (old_len == out->data)
Simon Horman05ee2132017-01-04 09:37:25 +0100787 goto err;
788
789 if (!stats_emit_json_field_tags(out, &stats[field]))
790 goto err;
791
792 if (!stats_emit_json_data_field(out, &stats[field]))
793 goto err;
794
795 if (!chunk_strcat(out, "}"))
796 goto err;
797 }
798
799 if (!chunk_strcat(out, "]"))
800 goto err;
801
802 return 1;
803
804err:
805 chunk_reset(out);
Willy Tarreaub0ce3ad2019-10-09 11:19:29 +0200806 if (flags & STAT_STARTED)
807 chunk_strcat(out, ",");
Simon Horman05ee2132017-01-04 09:37:25 +0100808 chunk_appendf(out, "{\"errorStr\":\"output buffer too short\"}");
809 return 0;
810}
811
William Lallemand74c24fb2016-11-21 17:18:36 +0100812/* Dump all fields from <stats> into <out> using the HTML format. A column is
Willy Tarreauab02b3f2019-10-09 11:11:46 +0200813 * reserved for the checkbox is STAT_ADMIN is set in <flags>. Some extra info
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +0200814 * are provided if STAT_SHLGNDS is present in <flags>. The statistics from
815 * extra modules are displayed at the end of the lines if STAT_SHMODULES is
816 * present in <flags>.
William Lallemand74c24fb2016-11-21 17:18:36 +0100817 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200818static int stats_dump_fields_html(struct buffer *out,
819 const struct field *stats,
820 unsigned int flags)
William Lallemand74c24fb2016-11-21 17:18:36 +0100821{
Willy Tarreau83061a82018-07-13 11:56:34 +0200822 struct buffer src;
Amaury Denoyellee3f576c2020-10-05 11:49:44 +0200823 struct stats_module *mod;
824 int i = 0, j = 0;
William Lallemand74c24fb2016-11-21 17:18:36 +0100825
826 if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_FE) {
827 chunk_appendf(out,
828 /* name, queue */
829 "<tr class=\"frontend\">");
830
Willy Tarreauab02b3f2019-10-09 11:11:46 +0200831 if (flags & STAT_ADMIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +0100832 /* Column sub-heading for Enable or Disable server */
833 chunk_appendf(out, "<td></td>");
834 }
835
836 chunk_appendf(out,
837 "<td class=ac>"
838 "<a name=\"%s/Frontend\"></a>"
839 "<a class=lfsb href=\"#%s/Frontend\">Frontend</a></td>"
840 "<td colspan=3></td>"
841 "",
842 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_PXNAME));
843
844 chunk_appendf(out,
845 /* sessions rate : current */
846 "<td><u>%s<div class=tips><table class=det>"
847 "<tr><th>Current connection rate:</th><td>%s/s</td></tr>"
848 "<tr><th>Current session rate:</th><td>%s/s</td></tr>"
849 "",
850 U2H(stats[ST_F_RATE].u.u32),
851 U2H(stats[ST_F_CONN_RATE].u.u32),
852 U2H(stats[ST_F_RATE].u.u32));
853
854 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
855 chunk_appendf(out,
856 "<tr><th>Current request rate:</th><td>%s/s</td></tr>",
857 U2H(stats[ST_F_REQ_RATE].u.u32));
858
859 chunk_appendf(out,
860 "</table></div></u></td>"
861 /* sessions rate : max */
862 "<td><u>%s<div class=tips><table class=det>"
863 "<tr><th>Max connection rate:</th><td>%s/s</td></tr>"
864 "<tr><th>Max session rate:</th><td>%s/s</td></tr>"
865 "",
866 U2H(stats[ST_F_RATE_MAX].u.u32),
867 U2H(stats[ST_F_CONN_RATE_MAX].u.u32),
868 U2H(stats[ST_F_RATE_MAX].u.u32));
869
870 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
871 chunk_appendf(out,
872 "<tr><th>Max request rate:</th><td>%s/s</td></tr>",
873 U2H(stats[ST_F_REQ_RATE_MAX].u.u32));
874
875 chunk_appendf(out,
876 "</table></div></u></td>"
877 /* sessions rate : limit */
878 "<td>%s</td>",
879 LIM2A(stats[ST_F_RATE_LIM].u.u32, "-"));
880
881 chunk_appendf(out,
882 /* sessions: current, max, limit, total */
883 "<td>%s</td><td>%s</td><td>%s</td>"
884 "<td><u>%s<div class=tips><table class=det>"
885 "<tr><th>Cum. connections:</th><td>%s</td></tr>"
886 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
887 "",
888 U2H(stats[ST_F_SCUR].u.u32), U2H(stats[ST_F_SMAX].u.u32), U2H(stats[ST_F_SLIM].u.u32),
889 U2H(stats[ST_F_STOT].u.u64),
890 U2H(stats[ST_F_CONN_TOT].u.u64),
891 U2H(stats[ST_F_STOT].u.u64));
892
893 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
894 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0) {
895 chunk_appendf(out,
896 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
897 "<tr><th>- HTTP 1xx responses:</th><td>%s</td></tr>"
898 "<tr><th>- HTTP 2xx responses:</th><td>%s</td></tr>"
899 "<tr><th>&nbsp;&nbsp;Compressed 2xx:</th><td>%s</td><td>(%d%%)</td></tr>"
900 "<tr><th>- HTTP 3xx responses:</th><td>%s</td></tr>"
901 "<tr><th>- HTTP 4xx responses:</th><td>%s</td></tr>"
902 "<tr><th>- HTTP 5xx responses:</th><td>%s</td></tr>"
903 "<tr><th>- other responses:</th><td>%s</td></tr>"
904 "<tr><th>Intercepted requests:</th><td>%s</td></tr>"
Willy Tarreaua1214a52018-12-14 14:00:25 +0100905 "<tr><th>Cache lookups:</th><td>%s</td></tr>"
906 "<tr><th>Cache hits:</th><td>%s</td><td>(%d%%)</td></tr>"
Willy Tarreau1b0f85e2018-05-28 15:12:40 +0200907 "<tr><th>Failed hdr rewrites:</th><td>%s</td></tr>"
Christopher Faulet0159ee42019-12-16 14:40:39 +0100908 "<tr><th>Internal errors:</th><td>%s</td></tr>"
William Lallemand74c24fb2016-11-21 17:18:36 +0100909 "",
910 U2H(stats[ST_F_REQ_TOT].u.u64),
911 U2H(stats[ST_F_HRSP_1XX].u.u64),
912 U2H(stats[ST_F_HRSP_2XX].u.u64),
913 U2H(stats[ST_F_COMP_RSP].u.u64),
914 stats[ST_F_HRSP_2XX].u.u64 ?
915 (int)(100 * stats[ST_F_COMP_RSP].u.u64 / stats[ST_F_HRSP_2XX].u.u64) : 0,
916 U2H(stats[ST_F_HRSP_3XX].u.u64),
917 U2H(stats[ST_F_HRSP_4XX].u.u64),
918 U2H(stats[ST_F_HRSP_5XX].u.u64),
919 U2H(stats[ST_F_HRSP_OTHER].u.u64),
Willy Tarreau1b0f85e2018-05-28 15:12:40 +0200920 U2H(stats[ST_F_INTERCEPTED].u.u64),
Willy Tarreaua1214a52018-12-14 14:00:25 +0100921 U2H(stats[ST_F_CACHE_LOOKUPS].u.u64),
922 U2H(stats[ST_F_CACHE_HITS].u.u64),
923 stats[ST_F_CACHE_LOOKUPS].u.u64 ?
924 (int)(100 * stats[ST_F_CACHE_HITS].u.u64 / stats[ST_F_CACHE_LOOKUPS].u.u64) : 0,
Christopher Faulet0159ee42019-12-16 14:40:39 +0100925 U2H(stats[ST_F_WREW].u.u64),
926 U2H(stats[ST_F_EINT].u.u64));
William Lallemand74c24fb2016-11-21 17:18:36 +0100927 }
928
929 chunk_appendf(out,
930 "</table></div></u></td>"
931 /* sessions: lbtot, lastsess */
932 "<td></td><td></td>"
933 /* bytes : in */
934 "<td>%s</td>"
935 "",
936 U2H(stats[ST_F_BIN].u.u64));
937
938 chunk_appendf(out,
939 /* bytes:out + compression stats (via hover): comp_in, comp_out, comp_byp */
940 "<td>%s%s<div class=tips><table class=det>"
941 "<tr><th>Response bytes in:</th><td>%s</td></tr>"
942 "<tr><th>Compression in:</th><td>%s</td></tr>"
943 "<tr><th>Compression out:</th><td>%s</td><td>(%d%%)</td></tr>"
944 "<tr><th>Compression bypass:</th><td>%s</td></tr>"
945 "<tr><th>Total bytes saved:</th><td>%s</td><td>(%d%%)</td></tr>"
946 "</table></div>%s</td>",
947 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "<u>":"",
948 U2H(stats[ST_F_BOUT].u.u64),
949 U2H(stats[ST_F_BOUT].u.u64),
950 U2H(stats[ST_F_COMP_IN].u.u64),
951 U2H(stats[ST_F_COMP_OUT].u.u64),
952 stats[ST_F_COMP_IN].u.u64 ? (int)(stats[ST_F_COMP_OUT].u.u64 * 100 / stats[ST_F_COMP_IN].u.u64) : 0,
953 U2H(stats[ST_F_COMP_BYP].u.u64),
954 U2H(stats[ST_F_COMP_IN].u.u64 - stats[ST_F_COMP_OUT].u.u64),
955 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,
956 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "</u>":"");
957
958 chunk_appendf(out,
959 /* denied: req, resp */
960 "<td>%s</td><td>%s</td>"
961 /* errors : request, connect, response */
962 "<td>%s</td><td></td><td></td>"
963 /* warnings: retries, redispatches */
964 "<td></td><td></td>"
965 /* server status : reflect frontend status */
966 "<td class=ac>%s</td>"
967 /* rest of server: nothing */
Amaury Denoyellee3f576c2020-10-05 11:49:44 +0200968 "<td class=ac colspan=8></td>"
William Lallemand74c24fb2016-11-21 17:18:36 +0100969 "",
970 U2H(stats[ST_F_DREQ].u.u64), U2H(stats[ST_F_DRESP].u.u64),
971 U2H(stats[ST_F_EREQ].u.u64),
972 field_str(stats, ST_F_STATUS));
Amaury Denoyellee3f576c2020-10-05 11:49:44 +0200973
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +0200974 if (flags & STAT_SHMODULES) {
975 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
Amaury Denoyellee6ee8202020-11-03 15:04:46 +0100976 chunk_appendf(out, "<td>");
977
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +0200978 if (stats_px_get_cap(mod->domain_flags) & STATS_PX_CAP_FE) {
Amaury Denoyellee6ee8202020-11-03 15:04:46 +0100979 chunk_appendf(out,
980 "<u>%s<div class=tips><table class=det>",
981 mod->name);
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +0200982 for (j = 0; j < mod->stats_count; ++j) {
983 chunk_appendf(out,
984 "<tr><th>%s</th><td>%s</td></tr>",
985 mod->stats[j].desc, field_to_html_str(&stats[ST_F_TOTAL_FIELDS + i]));
986 ++i;
987 }
Amaury Denoyellee6ee8202020-11-03 15:04:46 +0100988 chunk_appendf(out, "</table></div></u>");
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +0200989 } else {
990 i += mod->stats_count;
Amaury Denoyellee3f576c2020-10-05 11:49:44 +0200991 }
Amaury Denoyellee3f576c2020-10-05 11:49:44 +0200992
Amaury Denoyellee6ee8202020-11-03 15:04:46 +0100993 chunk_appendf(out, "</td>");
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +0200994 }
Amaury Denoyellee3f576c2020-10-05 11:49:44 +0200995 }
996
997 chunk_appendf(out, "</tr>");
William Lallemand74c24fb2016-11-21 17:18:36 +0100998 }
999 else if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_SO) {
1000 chunk_appendf(out, "<tr class=socket>");
Willy Tarreauab02b3f2019-10-09 11:11:46 +02001001 if (flags & STAT_ADMIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001002 /* Column sub-heading for Enable or Disable server */
1003 chunk_appendf(out, "<td></td>");
1004 }
1005
1006 chunk_appendf(out,
1007 /* frontend name, listener name */
1008 "<td class=ac><a name=\"%s/+%s\"></a>%s"
1009 "<a class=lfsb href=\"#%s/+%s\">%s</a>"
1010 "",
1011 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME),
Willy Tarreau708c4162019-10-09 10:19:16 +02001012 (flags & STAT_SHLGNDS)?"<u>":"",
William Lallemand74c24fb2016-11-21 17:18:36 +01001013 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME), field_str(stats, ST_F_SVNAME));
1014
Willy Tarreau708c4162019-10-09 10:19:16 +02001015 if (flags & STAT_SHLGNDS) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001016 chunk_appendf(out, "<div class=tips>");
1017
Willy Tarreau90807112020-02-25 08:16:33 +01001018 if (isdigit((unsigned char)*field_str(stats, ST_F_ADDR)))
William Lallemand74c24fb2016-11-21 17:18:36 +01001019 chunk_appendf(out, "IPv4: %s, ", field_str(stats, ST_F_ADDR));
1020 else if (*field_str(stats, ST_F_ADDR) == '[')
1021 chunk_appendf(out, "IPv6: %s, ", field_str(stats, ST_F_ADDR));
1022 else if (*field_str(stats, ST_F_ADDR))
1023 chunk_appendf(out, "%s, ", field_str(stats, ST_F_ADDR));
1024
1025 /* id */
1026 chunk_appendf(out, "id: %d</div>", stats[ST_F_SID].u.u32);
1027 }
1028
1029 chunk_appendf(out,
1030 /* queue */
1031 "%s</td><td colspan=3></td>"
1032 /* sessions rate: current, max, limit */
1033 "<td colspan=3>&nbsp;</td>"
1034 /* sessions: current, max, limit, total, lbtot, lastsess */
1035 "<td>%s</td><td>%s</td><td>%s</td>"
1036 "<td>%s</td><td>&nbsp;</td><td>&nbsp;</td>"
1037 /* bytes: in, out */
1038 "<td>%s</td><td>%s</td>"
1039 "",
Willy Tarreau708c4162019-10-09 10:19:16 +02001040 (flags & STAT_SHLGNDS)?"</u>":"",
William Lallemand74c24fb2016-11-21 17:18:36 +01001041 U2H(stats[ST_F_SCUR].u.u32), U2H(stats[ST_F_SMAX].u.u32), U2H(stats[ST_F_SLIM].u.u32),
1042 U2H(stats[ST_F_STOT].u.u64), U2H(stats[ST_F_BIN].u.u64), U2H(stats[ST_F_BOUT].u.u64));
1043
1044 chunk_appendf(out,
1045 /* denied: req, resp */
1046 "<td>%s</td><td>%s</td>"
1047 /* errors: request, connect, response */
1048 "<td>%s</td><td></td><td></td>"
1049 /* warnings: retries, redispatches */
1050 "<td></td><td></td>"
1051 /* server status: reflect listener status */
1052 "<td class=ac>%s</td>"
1053 /* rest of server: nothing */
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001054 "<td class=ac colspan=8></td>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001055 "",
1056 U2H(stats[ST_F_DREQ].u.u64), U2H(stats[ST_F_DRESP].u.u64),
1057 U2H(stats[ST_F_EREQ].u.u64),
1058 field_str(stats, ST_F_STATUS));
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001059
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001060 if (flags & STAT_SHMODULES) {
1061 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001062 chunk_appendf(out, "<td>");
1063
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001064 if (stats_px_get_cap(mod->domain_flags) & STATS_PX_CAP_LI) {
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001065 chunk_appendf(out,
1066 "<u>%s<div class=tips><table class=det>",
1067 mod->name);
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001068 for (j = 0; j < mod->stats_count; ++j) {
1069 chunk_appendf(out,
1070 "<tr><th>%s</th><td>%s</td></tr>",
1071 mod->stats[j].desc, field_to_html_str(&stats[ST_F_TOTAL_FIELDS + i]));
1072 ++i;
1073 }
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001074 chunk_appendf(out, "</table></div></u>");
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001075 } else {
1076 i += mod->stats_count;
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001077 }
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001078
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001079 chunk_appendf(out, "</td>");
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001080 }
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001081 }
1082
1083 chunk_appendf(out, "</tr>");
William Lallemand74c24fb2016-11-21 17:18:36 +01001084 }
1085 else if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_SV) {
1086 const char *style;
1087
1088 /* determine the style to use depending on the server's state,
1089 * its health and weight. There isn't a 1-to-1 mapping between
1090 * state and styles for the cases where the server is (still)
1091 * up. The reason is that we don't want to report nolb and
1092 * drain with the same color.
1093 */
1094
1095 if (strcmp(field_str(stats, ST_F_STATUS), "DOWN") == 0 ||
1096 strcmp(field_str(stats, ST_F_STATUS), "DOWN (agent)") == 0) {
1097 style = "down";
1098 }
Florian Apolloner39272c22021-03-30 13:28:35 +02001099 else if (strncmp(field_str(stats, ST_F_STATUS), "DOWN ", strlen("DOWN ")) == 0) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001100 style = "going_up";
1101 }
Daniel Corbettb4285172020-03-28 12:35:50 -04001102 else if (strcmp(field_str(stats, ST_F_STATUS), "DRAIN") == 0) {
1103 style = "draining";
1104 }
Florian Apolloner39272c22021-03-30 13:28:35 +02001105 else if (strncmp(field_str(stats, ST_F_STATUS), "NOLB ", strlen("NOLB ")) == 0) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001106 style = "going_down";
1107 }
1108 else if (strcmp(field_str(stats, ST_F_STATUS), "NOLB") == 0) {
1109 style = "nolb";
1110 }
1111 else if (strcmp(field_str(stats, ST_F_STATUS), "no check") == 0) {
1112 style = "no_check";
1113 }
1114 else if (!stats[ST_F_CHKFAIL].type ||
1115 stats[ST_F_CHECK_HEALTH].u.u32 == stats[ST_F_CHECK_RISE].u.u32 + stats[ST_F_CHECK_FALL].u.u32 - 1) {
1116 /* no check or max health = UP */
1117 if (stats[ST_F_WEIGHT].u.u32)
1118 style = "up";
1119 else
1120 style = "draining";
1121 }
1122 else {
1123 style = "going_down";
1124 }
1125
Willy Tarreau7b524852020-08-11 10:26:36 +02001126 if (strncmp(field_str(stats, ST_F_STATUS), "MAINT", 5) == 0)
William Lallemand74c24fb2016-11-21 17:18:36 +01001127 chunk_appendf(out, "<tr class=\"maintain\">");
1128 else
1129 chunk_appendf(out,
1130 "<tr class=\"%s_%s\">",
1131 (stats[ST_F_BCK].u.u32) ? "backup" : "active", style);
1132
1133
Willy Tarreauab02b3f2019-10-09 11:11:46 +02001134 if (flags & STAT_ADMIN)
William Lallemand74c24fb2016-11-21 17:18:36 +01001135 chunk_appendf(out,
David Harrigand3db35a2016-12-30 12:12:49 +00001136 "<td><input class='%s-checkbox' type=\"checkbox\" name=\"s\" value=\"%s\"></td>",
1137 field_str(stats, ST_F_PXNAME),
William Lallemand74c24fb2016-11-21 17:18:36 +01001138 field_str(stats, ST_F_SVNAME));
1139
1140 chunk_appendf(out,
1141 "<td class=ac><a name=\"%s/%s\"></a>%s"
1142 "<a class=lfsb href=\"#%s/%s\">%s</a>"
1143 "",
1144 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME),
Willy Tarreau708c4162019-10-09 10:19:16 +02001145 (flags & STAT_SHLGNDS) ? "<u>" : "",
William Lallemand74c24fb2016-11-21 17:18:36 +01001146 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME), field_str(stats, ST_F_SVNAME));
1147
Willy Tarreau708c4162019-10-09 10:19:16 +02001148 if (flags & STAT_SHLGNDS) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001149 chunk_appendf(out, "<div class=tips>");
1150
Willy Tarreau90807112020-02-25 08:16:33 +01001151 if (isdigit((unsigned char)*field_str(stats, ST_F_ADDR)))
William Lallemand74c24fb2016-11-21 17:18:36 +01001152 chunk_appendf(out, "IPv4: %s, ", field_str(stats, ST_F_ADDR));
1153 else if (*field_str(stats, ST_F_ADDR) == '[')
1154 chunk_appendf(out, "IPv6: %s, ", field_str(stats, ST_F_ADDR));
1155 else if (*field_str(stats, ST_F_ADDR))
1156 chunk_appendf(out, "%s, ", field_str(stats, ST_F_ADDR));
1157
1158 /* id */
Aurelien DARRAGON745ce8e2022-11-17 16:34:07 +01001159 chunk_appendf(out, "id: %d, rid: %d", stats[ST_F_SID].u.u32, stats[ST_F_SRID].u.u32);
William Lallemand74c24fb2016-11-21 17:18:36 +01001160
1161 /* cookie */
1162 if (stats[ST_F_COOKIE].type) {
1163 chunk_appendf(out, ", cookie: '");
1164 chunk_initstr(&src, field_str(stats, ST_F_COOKIE));
1165 chunk_htmlencode(out, &src);
1166 chunk_appendf(out, "'");
1167 }
1168
1169 chunk_appendf(out, "</div>");
1170 }
1171
1172 chunk_appendf(out,
1173 /* queue : current, max, limit */
1174 "%s</td><td>%s</td><td>%s</td><td>%s</td>"
1175 /* sessions rate : current, max, limit */
1176 "<td>%s</td><td>%s</td><td></td>"
1177 "",
Willy Tarreau708c4162019-10-09 10:19:16 +02001178 (flags & STAT_SHLGNDS) ? "</u>" : "",
William Lallemand74c24fb2016-11-21 17:18:36 +01001179 U2H(stats[ST_F_QCUR].u.u32), U2H(stats[ST_F_QMAX].u.u32), LIM2A(stats[ST_F_QLIMIT].u.u32, "-"),
1180 U2H(stats[ST_F_RATE].u.u32), U2H(stats[ST_F_RATE_MAX].u.u32));
1181
1182 chunk_appendf(out,
1183 /* sessions: current, max, limit, total */
Willy Tarreauf21d17b2019-09-08 09:24:56 +02001184 "<td><u>%s<div class=tips>"
1185 "<table class=det>"
1186 "<tr><th>Current active connections:</th><td>%s</td></tr>"
Willy Tarreau3bb617c2020-06-29 13:51:05 +02001187 "<tr><th>Current used connections:</th><td>%s</td></tr>"
Willy Tarreauf21d17b2019-09-08 09:24:56 +02001188 "<tr><th>Current idle connections:</th><td>%s</td></tr>"
Willy Tarreau3bb617c2020-06-29 13:51:05 +02001189 "<tr><th>- unsafe:</th><td>%s</td></tr>"
1190 "<tr><th>- safe:</th><td>%s</td></tr>"
Willy Tarreaua9fcecb2020-06-29 15:38:53 +02001191 "<tr><th>Estimated need of connections:</th><td>%s</td></tr>"
Willy Tarreauf21d17b2019-09-08 09:24:56 +02001192 "<tr><th>Active connections limit:</th><td>%s</td></tr>"
1193 "<tr><th>Idle connections limit:</th><td>%s</td></tr>"
1194 "</table></div></u>"
1195 "</td><td>%s</td><td>%s</td>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001196 "<td><u>%s<div class=tips><table class=det>"
1197 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
1198 "",
Willy Tarreauf21d17b2019-09-08 09:24:56 +02001199 U2H(stats[ST_F_SCUR].u.u32),
Willy Tarreau3bb617c2020-06-29 13:51:05 +02001200 U2H(stats[ST_F_SCUR].u.u32),
1201 U2H(stats[ST_F_USED_CONN_CUR].u.u32),
1202 U2H(stats[ST_F_SRV_ICUR].u.u32),
1203 U2H(stats[ST_F_IDLE_CONN_CUR].u.u32),
1204 U2H(stats[ST_F_SAFE_CONN_CUR].u.u32),
Willy Tarreaua9fcecb2020-06-29 15:38:53 +02001205 U2H(stats[ST_F_NEED_CONN_EST].u.u32),
Willy Tarreau3bb617c2020-06-29 13:51:05 +02001206
Willy Tarreauf21d17b2019-09-08 09:24:56 +02001207 LIM2A(stats[ST_F_SLIM].u.u32, "-"),
1208 stats[ST_F_SRV_ILIM].type ? U2H(stats[ST_F_SRV_ILIM].u.u32) : "-",
1209 U2H(stats[ST_F_SMAX].u.u32), LIM2A(stats[ST_F_SLIM].u.u32, "-"),
William Lallemand74c24fb2016-11-21 17:18:36 +01001210 U2H(stats[ST_F_STOT].u.u64),
1211 U2H(stats[ST_F_STOT].u.u64));
1212
1213 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
1214 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001215 chunk_appendf(out,
Willy Tarreauf1573842018-12-14 11:35:36 +01001216 "<tr><th>New connections:</th><td>%s</td></tr>"
1217 "<tr><th>Reused connections:</th><td>%s</td><td>(%d%%)</td></tr>"
Marcin Deranek3c27dda2020-05-15 18:32:51 +02001218 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001219 "<tr><th>- HTTP 1xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
1220 "<tr><th>- HTTP 2xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
1221 "<tr><th>- HTTP 3xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
1222 "<tr><th>- HTTP 4xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
1223 "<tr><th>- HTTP 5xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
1224 "<tr><th>- other responses:</th><td>%s</td><td>(%d%%)</td></tr>"
Willy Tarreau1b0f85e2018-05-28 15:12:40 +02001225 "<tr><th>Failed hdr rewrites:</th><td>%s</td></tr>"
Christopher Faulet0159ee42019-12-16 14:40:39 +01001226 "<tr><th>Internal error:</th><td>%s</td></tr>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001227 "",
Willy Tarreauf1573842018-12-14 11:35:36 +01001228 U2H(stats[ST_F_CONNECT].u.u64),
1229 U2H(stats[ST_F_REUSE].u.u64),
1230 (stats[ST_F_CONNECT].u.u64 + stats[ST_F_REUSE].u.u64) ?
1231 (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 +02001232 U2H(stats[ST_F_REQ_TOT].u.u64),
1233 U2H(stats[ST_F_HRSP_1XX].u.u64), stats[ST_F_REQ_TOT].u.u64 ?
1234 (int)(100 * stats[ST_F_HRSP_1XX].u.u64 / stats[ST_F_REQ_TOT].u.u64) : 0,
1235 U2H(stats[ST_F_HRSP_2XX].u.u64), stats[ST_F_REQ_TOT].u.u64 ?
1236 (int)(100 * stats[ST_F_HRSP_2XX].u.u64 / stats[ST_F_REQ_TOT].u.u64) : 0,
1237 U2H(stats[ST_F_HRSP_3XX].u.u64), stats[ST_F_REQ_TOT].u.u64 ?
1238 (int)(100 * stats[ST_F_HRSP_3XX].u.u64 / stats[ST_F_REQ_TOT].u.u64) : 0,
1239 U2H(stats[ST_F_HRSP_4XX].u.u64), stats[ST_F_REQ_TOT].u.u64 ?
1240 (int)(100 * stats[ST_F_HRSP_4XX].u.u64 / stats[ST_F_REQ_TOT].u.u64) : 0,
1241 U2H(stats[ST_F_HRSP_5XX].u.u64), stats[ST_F_REQ_TOT].u.u64 ?
1242 (int)(100 * stats[ST_F_HRSP_5XX].u.u64 / stats[ST_F_REQ_TOT].u.u64) : 0,
1243 U2H(stats[ST_F_HRSP_OTHER].u.u64), stats[ST_F_REQ_TOT].u.u64 ?
1244 (int)(100 * stats[ST_F_HRSP_OTHER].u.u64 / stats[ST_F_REQ_TOT].u.u64) : 0,
Christopher Faulet0159ee42019-12-16 14:40:39 +01001245 U2H(stats[ST_F_WREW].u.u64),
1246 U2H(stats[ST_F_EINT].u.u64));
William Lallemand74c24fb2016-11-21 17:18:36 +01001247 }
1248
Christopher Faulet0d1c2a62019-11-08 14:59:51 +01001249 chunk_appendf(out, "<tr><th colspan=3>Max / Avg over last 1024 success. conn.</th></tr>");
1250 chunk_appendf(out, "<tr><th>- Queue time:</th><td>%s / %s</td><td>ms</td></tr>",
1251 U2H(stats[ST_F_QT_MAX].u.u32), U2H(stats[ST_F_QTIME].u.u32));
1252 chunk_appendf(out, "<tr><th>- Connect time:</th><td>%s / %s</td><td>ms</td></tr>",
1253 U2H(stats[ST_F_CT_MAX].u.u32), U2H(stats[ST_F_CTIME].u.u32));
William Lallemand74c24fb2016-11-21 17:18:36 +01001254 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
Christopher Faulet0d1c2a62019-11-08 14:59:51 +01001255 chunk_appendf(out, "<tr><th>- Responses time:</th><td>%s / %s</td><td>ms</td></tr>",
1256 U2H(stats[ST_F_RT_MAX].u.u32), U2H(stats[ST_F_RTIME].u.u32));
1257 chunk_appendf(out, "<tr><th>- Total time:</th><td>%s / %s</td><td>ms</td></tr>",
1258 U2H(stats[ST_F_TT_MAX].u.u32), U2H(stats[ST_F_TTIME].u.u32));
William Lallemand74c24fb2016-11-21 17:18:36 +01001259
1260 chunk_appendf(out,
1261 "</table></div></u></td>"
1262 /* sessions: lbtot, last */
1263 "<td>%s</td><td>%s</td>",
1264 U2H(stats[ST_F_LBTOT].u.u64),
1265 human_time(stats[ST_F_LASTSESS].u.s32, 1));
1266
1267 chunk_appendf(out,
1268 /* bytes : in, out */
1269 "<td>%s</td><td>%s</td>"
1270 /* denied: req, resp */
1271 "<td></td><td>%s</td>"
1272 /* errors : request, connect */
1273 "<td></td><td>%s</td>"
1274 /* errors : response */
1275 "<td><u>%s<div class=tips>Connection resets during transfers: %lld client, %lld server</div></u></td>"
1276 /* warnings: retries, redispatches */
1277 "<td>%lld</td><td>%lld</td>"
1278 "",
1279 U2H(stats[ST_F_BIN].u.u64), U2H(stats[ST_F_BOUT].u.u64),
1280 U2H(stats[ST_F_DRESP].u.u64),
1281 U2H(stats[ST_F_ECON].u.u64),
1282 U2H(stats[ST_F_ERESP].u.u64),
1283 (long long)stats[ST_F_CLI_ABRT].u.u64,
1284 (long long)stats[ST_F_SRV_ABRT].u.u64,
1285 (long long)stats[ST_F_WRETR].u.u64,
1286 (long long)stats[ST_F_WREDIS].u.u64);
1287
1288 /* status, last change */
1289 chunk_appendf(out, "<td class=ac>");
1290
1291 /* FIXME!!!!
1292 * LASTCHG should contain the last change for *this* server and must be computed
1293 * properly above, as was done below, ie: this server if maint, otherwise ref server
1294 * if tracking. Note that ref is either local or remote depending on tracking.
1295 */
1296
1297
Willy Tarreau7b524852020-08-11 10:26:36 +02001298 if (strncmp(field_str(stats, ST_F_STATUS), "MAINT", 5) == 0) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001299 chunk_appendf(out, "%s MAINT", human_time(stats[ST_F_LASTCHG].u.u32, 1));
1300 }
Willy Tarreau7b524852020-08-11 10:26:36 +02001301 else if (strcmp(field_str(stats, ST_F_STATUS), "no check") == 0) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001302 chunk_strcat(out, "<i>no check</i>");
1303 }
1304 else {
1305 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 +02001306 if (strncmp(field_str(stats, ST_F_STATUS), "DOWN", 4) == 0) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001307 if (stats[ST_F_CHECK_HEALTH].u.u32)
1308 chunk_strcat(out, " &uarr;");
1309 }
1310 else if (stats[ST_F_CHECK_HEALTH].u.u32 < stats[ST_F_CHECK_RISE].u.u32 + stats[ST_F_CHECK_FALL].u.u32 - 1)
1311 chunk_strcat(out, " &darr;");
1312 }
1313
Willy Tarreau7b524852020-08-11 10:26:36 +02001314 if (strncmp(field_str(stats, ST_F_STATUS), "DOWN", 4) == 0 &&
William Lallemand74c24fb2016-11-21 17:18:36 +01001315 stats[ST_F_AGENT_STATUS].type && !stats[ST_F_AGENT_HEALTH].u.u32) {
1316 chunk_appendf(out,
1317 "</td><td class=ac><u> %s",
1318 field_str(stats, ST_F_AGENT_STATUS));
1319
1320 if (stats[ST_F_AGENT_CODE].type)
1321 chunk_appendf(out, "/%d", stats[ST_F_AGENT_CODE].u.u32);
1322
1323 if (stats[ST_F_AGENT_DURATION].type)
1324 chunk_appendf(out, " in %lums", (long)stats[ST_F_AGENT_DURATION].u.u64);
1325
1326 chunk_appendf(out, "<div class=tips>%s", field_str(stats, ST_F_AGENT_DESC));
1327
1328 if (*field_str(stats, ST_F_LAST_AGT)) {
1329 chunk_appendf(out, ": ");
1330 chunk_initstr(&src, field_str(stats, ST_F_LAST_AGT));
1331 chunk_htmlencode(out, &src);
1332 }
1333 chunk_appendf(out, "</div></u>");
1334 }
1335 else if (stats[ST_F_CHECK_STATUS].type) {
1336 chunk_appendf(out,
1337 "</td><td class=ac><u> %s",
1338 field_str(stats, ST_F_CHECK_STATUS));
1339
1340 if (stats[ST_F_CHECK_CODE].type)
1341 chunk_appendf(out, "/%d", stats[ST_F_CHECK_CODE].u.u32);
1342
1343 if (stats[ST_F_CHECK_DURATION].type)
1344 chunk_appendf(out, " in %lums", (long)stats[ST_F_CHECK_DURATION].u.u64);
1345
1346 chunk_appendf(out, "<div class=tips>%s", field_str(stats, ST_F_CHECK_DESC));
1347
1348 if (*field_str(stats, ST_F_LAST_CHK)) {
1349 chunk_appendf(out, ": ");
1350 chunk_initstr(&src, field_str(stats, ST_F_LAST_CHK));
1351 chunk_htmlencode(out, &src);
1352 }
1353 chunk_appendf(out, "</div></u>");
1354 }
1355 else
1356 chunk_appendf(out, "</td><td>");
1357
1358 chunk_appendf(out,
Willy Tarreaubd715102020-10-23 22:44:30 +02001359 /* weight / uweight */
1360 "</td><td class=ac>%d/%d</td>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001361 /* act, bck */
1362 "<td class=ac>%s</td><td class=ac>%s</td>"
1363 "",
Willy Tarreaubd715102020-10-23 22:44:30 +02001364 stats[ST_F_WEIGHT].u.u32, stats[ST_F_UWEIGHT].u.u32,
William Lallemand74c24fb2016-11-21 17:18:36 +01001365 stats[ST_F_BCK].u.u32 ? "-" : "Y",
1366 stats[ST_F_BCK].u.u32 ? "Y" : "-");
1367
1368 /* check failures: unique, fatal, down time */
1369 if (strcmp(field_str(stats, ST_F_STATUS), "MAINT (resolution)") == 0) {
1370 chunk_appendf(out, "<td class=ac colspan=3>resolution</td>");
1371 }
1372 else if (stats[ST_F_CHKFAIL].type) {
1373 chunk_appendf(out, "<td><u>%lld", (long long)stats[ST_F_CHKFAIL].u.u64);
1374
1375 if (stats[ST_F_HANAFAIL].type)
1376 chunk_appendf(out, "/%lld", (long long)stats[ST_F_HANAFAIL].u.u64);
1377
1378 chunk_appendf(out,
1379 "<div class=tips>Failed Health Checks%s</div></u></td>"
1380 "<td>%lld</td><td>%s</td>"
1381 "",
1382 stats[ST_F_HANAFAIL].type ? "/Health Analyses" : "",
1383 (long long)stats[ST_F_CHKDOWN].u.u64, human_time(stats[ST_F_DOWNTIME].u.u32, 1));
1384 }
1385 else if (strcmp(field_str(stats, ST_F_STATUS), "MAINT") != 0 && field_format(stats, ST_F_TRACKED) == FF_STR) {
1386 /* tracking a server (hence inherited maint would appear as "MAINT (via...)" */
1387 chunk_appendf(out,
1388 "<td class=ac colspan=3><a class=lfsb href=\"#%s\">via %s</a></td>",
1389 field_str(stats, ST_F_TRACKED), field_str(stats, ST_F_TRACKED));
1390 }
1391 else
1392 chunk_appendf(out, "<td colspan=3></td>");
1393
1394 /* throttle */
1395 if (stats[ST_F_THROTTLE].type)
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001396 chunk_appendf(out, "<td class=ac>%d %%</td>\n", stats[ST_F_THROTTLE].u.u32);
William Lallemand74c24fb2016-11-21 17:18:36 +01001397 else
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001398 chunk_appendf(out, "<td class=ac>-</td>");
1399
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001400 if (flags & STAT_SHMODULES) {
1401 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001402 chunk_appendf(out, "<td>");
1403
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001404 if (stats_px_get_cap(mod->domain_flags) & STATS_PX_CAP_SRV) {
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001405 chunk_appendf(out,
1406 "<u>%s<div class=tips><table class=det>",
1407 mod->name);
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001408 for (j = 0; j < mod->stats_count; ++j) {
1409 chunk_appendf(out,
1410 "<tr><th>%s</th><td>%s</td></tr>",
1411 mod->stats[j].desc, field_to_html_str(&stats[ST_F_TOTAL_FIELDS + i]));
1412 ++i;
1413 }
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001414 chunk_appendf(out, "</table></div></u>");
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001415 } else {
1416 i += mod->stats_count;
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001417 }
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001418
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001419 chunk_appendf(out, "</td>");
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001420 }
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001421 }
1422
1423 chunk_appendf(out, "</tr>\n");
William Lallemand74c24fb2016-11-21 17:18:36 +01001424 }
1425 else if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_BE) {
1426 chunk_appendf(out, "<tr class=\"backend\">");
Willy Tarreauab02b3f2019-10-09 11:11:46 +02001427 if (flags & STAT_ADMIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001428 /* Column sub-heading for Enable or Disable server */
1429 chunk_appendf(out, "<td></td>");
1430 }
1431 chunk_appendf(out,
1432 "<td class=ac>"
1433 /* name */
1434 "%s<a name=\"%s/Backend\"></a>"
1435 "<a class=lfsb href=\"#%s/Backend\">Backend</a>"
1436 "",
Willy Tarreau708c4162019-10-09 10:19:16 +02001437 (flags & STAT_SHLGNDS)?"<u>":"",
William Lallemand74c24fb2016-11-21 17:18:36 +01001438 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_PXNAME));
1439
Willy Tarreau708c4162019-10-09 10:19:16 +02001440 if (flags & STAT_SHLGNDS) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001441 /* balancing */
1442 chunk_appendf(out, "<div class=tips>balancing: %s",
1443 field_str(stats, ST_F_ALGO));
1444
1445 /* cookie */
1446 if (stats[ST_F_COOKIE].type) {
1447 chunk_appendf(out, ", cookie: '");
1448 chunk_initstr(&src, field_str(stats, ST_F_COOKIE));
1449 chunk_htmlencode(out, &src);
1450 chunk_appendf(out, "'");
1451 }
1452 chunk_appendf(out, "</div>");
1453 }
1454
1455 chunk_appendf(out,
1456 "%s</td>"
1457 /* queue : current, max */
1458 "<td>%s</td><td>%s</td><td></td>"
1459 /* sessions rate : current, max, limit */
1460 "<td>%s</td><td>%s</td><td></td>"
1461 "",
Willy Tarreau708c4162019-10-09 10:19:16 +02001462 (flags & STAT_SHLGNDS)?"</u>":"",
William Lallemand74c24fb2016-11-21 17:18:36 +01001463 U2H(stats[ST_F_QCUR].u.u32), U2H(stats[ST_F_QMAX].u.u32),
1464 U2H(stats[ST_F_RATE].u.u32), U2H(stats[ST_F_RATE_MAX].u.u32));
1465
1466 chunk_appendf(out,
1467 /* sessions: current, max, limit, total */
1468 "<td>%s</td><td>%s</td><td>%s</td>"
1469 "<td><u>%s<div class=tips><table class=det>"
1470 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
1471 "",
Willy Tarreau8e0f1752016-12-12 15:07:29 +01001472 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 +01001473 U2H(stats[ST_F_STOT].u.u64),
1474 U2H(stats[ST_F_STOT].u.u64));
1475
1476 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
1477 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0) {
1478 chunk_appendf(out,
Willy Tarreauf1573842018-12-14 11:35:36 +01001479 "<tr><th>New connections:</th><td>%s</td></tr>"
1480 "<tr><th>Reused connections:</th><td>%s</td><td>(%d%%)</td></tr>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001481 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
1482 "<tr><th>- HTTP 1xx responses:</th><td>%s</td></tr>"
1483 "<tr><th>- HTTP 2xx responses:</th><td>%s</td></tr>"
1484 "<tr><th>&nbsp;&nbsp;Compressed 2xx:</th><td>%s</td><td>(%d%%)</td></tr>"
1485 "<tr><th>- HTTP 3xx responses:</th><td>%s</td></tr>"
1486 "<tr><th>- HTTP 4xx responses:</th><td>%s</td></tr>"
1487 "<tr><th>- HTTP 5xx responses:</th><td>%s</td></tr>"
1488 "<tr><th>- other responses:</th><td>%s</td></tr>"
Willy Tarreaua1214a52018-12-14 14:00:25 +01001489 "<tr><th>Cache lookups:</th><td>%s</td></tr>"
1490 "<tr><th>Cache hits:</th><td>%s</td><td>(%d%%)</td></tr>"
Willy Tarreau1b0f85e2018-05-28 15:12:40 +02001491 "<tr><th>Failed hdr rewrites:</th><td>%s</td></tr>"
Christopher Faulet0159ee42019-12-16 14:40:39 +01001492 "<tr><th>Internal errors:</th><td>%s</td></tr>"
Christopher Faulet0d1c2a62019-11-08 14:59:51 +01001493 "",
Willy Tarreauf1573842018-12-14 11:35:36 +01001494 U2H(stats[ST_F_CONNECT].u.u64),
1495 U2H(stats[ST_F_REUSE].u.u64),
1496 (stats[ST_F_CONNECT].u.u64 + stats[ST_F_REUSE].u.u64) ?
1497 (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 +01001498 U2H(stats[ST_F_REQ_TOT].u.u64),
1499 U2H(stats[ST_F_HRSP_1XX].u.u64),
1500 U2H(stats[ST_F_HRSP_2XX].u.u64),
1501 U2H(stats[ST_F_COMP_RSP].u.u64),
1502 stats[ST_F_HRSP_2XX].u.u64 ?
1503 (int)(100 * stats[ST_F_COMP_RSP].u.u64 / stats[ST_F_HRSP_2XX].u.u64) : 0,
1504 U2H(stats[ST_F_HRSP_3XX].u.u64),
1505 U2H(stats[ST_F_HRSP_4XX].u.u64),
1506 U2H(stats[ST_F_HRSP_5XX].u.u64),
Willy Tarreaufeead3a2018-12-14 13:48:44 +01001507 U2H(stats[ST_F_HRSP_OTHER].u.u64),
Willy Tarreaua1214a52018-12-14 14:00:25 +01001508 U2H(stats[ST_F_CACHE_LOOKUPS].u.u64),
1509 U2H(stats[ST_F_CACHE_HITS].u.u64),
1510 stats[ST_F_CACHE_LOOKUPS].u.u64 ?
1511 (int)(100 * stats[ST_F_CACHE_HITS].u.u64 / stats[ST_F_CACHE_LOOKUPS].u.u64) : 0,
Christopher Faulet0159ee42019-12-16 14:40:39 +01001512 U2H(stats[ST_F_WREW].u.u64),
1513 U2H(stats[ST_F_EINT].u.u64));
William Lallemand74c24fb2016-11-21 17:18:36 +01001514 }
1515
Christopher Faulet0d1c2a62019-11-08 14:59:51 +01001516 chunk_appendf(out, "<tr><th colspan=3>Max / Avg over last 1024 success. conn.</th></tr>");
1517 chunk_appendf(out, "<tr><th>- Queue time:</th><td>%s / %s</td><td>ms</td></tr>",
1518 U2H(stats[ST_F_QT_MAX].u.u32), U2H(stats[ST_F_QTIME].u.u32));
1519 chunk_appendf(out, "<tr><th>- Connect time:</th><td>%s / %s</td><td>ms</td></tr>",
1520 U2H(stats[ST_F_CT_MAX].u.u32), U2H(stats[ST_F_CTIME].u.u32));
William Lallemand74c24fb2016-11-21 17:18:36 +01001521 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
Christopher Faulet0d1c2a62019-11-08 14:59:51 +01001522 chunk_appendf(out, "<tr><th>- Responses time:</th><td>%s / %s</td><td>ms</td></tr>",
1523 U2H(stats[ST_F_RT_MAX].u.u32), U2H(stats[ST_F_RTIME].u.u32));
1524 chunk_appendf(out, "<tr><th>- Total time:</th><td>%s / %s</td><td>ms</td></tr>",
1525 U2H(stats[ST_F_TT_MAX].u.u32), U2H(stats[ST_F_TTIME].u.u32));
William Lallemand74c24fb2016-11-21 17:18:36 +01001526
1527 chunk_appendf(out,
1528 "</table></div></u></td>"
1529 /* sessions: lbtot, last */
1530 "<td>%s</td><td>%s</td>"
1531 /* bytes: in */
1532 "<td>%s</td>"
1533 "",
1534 U2H(stats[ST_F_LBTOT].u.u64),
1535 human_time(stats[ST_F_LASTSESS].u.s32, 1),
1536 U2H(stats[ST_F_BIN].u.u64));
1537
1538 chunk_appendf(out,
1539 /* bytes:out + compression stats (via hover): comp_in, comp_out, comp_byp */
1540 "<td>%s%s<div class=tips><table class=det>"
1541 "<tr><th>Response bytes in:</th><td>%s</td></tr>"
1542 "<tr><th>Compression in:</th><td>%s</td></tr>"
1543 "<tr><th>Compression out:</th><td>%s</td><td>(%d%%)</td></tr>"
1544 "<tr><th>Compression bypass:</th><td>%s</td></tr>"
1545 "<tr><th>Total bytes saved:</th><td>%s</td><td>(%d%%)</td></tr>"
1546 "</table></div>%s</td>",
1547 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "<u>":"",
1548 U2H(stats[ST_F_BOUT].u.u64),
1549 U2H(stats[ST_F_BOUT].u.u64),
1550 U2H(stats[ST_F_COMP_IN].u.u64),
1551 U2H(stats[ST_F_COMP_OUT].u.u64),
1552 stats[ST_F_COMP_IN].u.u64 ? (int)(stats[ST_F_COMP_OUT].u.u64 * 100 / stats[ST_F_COMP_IN].u.u64) : 0,
1553 U2H(stats[ST_F_COMP_BYP].u.u64),
1554 U2H(stats[ST_F_COMP_IN].u.u64 - stats[ST_F_COMP_OUT].u.u64),
1555 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,
1556 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "</u>":"");
1557
1558 chunk_appendf(out,
1559 /* denied: req, resp */
1560 "<td>%s</td><td>%s</td>"
1561 /* errors : request, connect */
1562 "<td></td><td>%s</td>"
1563 /* errors : response */
1564 "<td><u>%s<div class=tips>Connection resets during transfers: %lld client, %lld server</div></u></td>"
1565 /* warnings: retries, redispatches */
1566 "<td>%lld</td><td>%lld</td>"
1567 /* backend status: reflect backend status (up/down): we display UP
1568 * if the backend has known working servers or if it has no server at
1569 * all (eg: for stats). Then we display the total weight, number of
1570 * active and backups. */
Willy Tarreaubd715102020-10-23 22:44:30 +02001571 "<td class=ac>%s %s</td><td class=ac>&nbsp;</td><td class=ac>%d/%d</td>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001572 "<td class=ac>%d</td><td class=ac>%d</td>"
1573 "",
1574 U2H(stats[ST_F_DREQ].u.u64), U2H(stats[ST_F_DRESP].u.u64),
1575 U2H(stats[ST_F_ECON].u.u64),
1576 U2H(stats[ST_F_ERESP].u.u64),
1577 (long long)stats[ST_F_CLI_ABRT].u.u64,
1578 (long long)stats[ST_F_SRV_ABRT].u.u64,
1579 (long long)stats[ST_F_WRETR].u.u64, (long long)stats[ST_F_WREDIS].u.u64,
1580 human_time(stats[ST_F_LASTCHG].u.u32, 1),
1581 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 +02001582 stats[ST_F_WEIGHT].u.u32, stats[ST_F_UWEIGHT].u.u32,
William Lallemand74c24fb2016-11-21 17:18:36 +01001583 stats[ST_F_ACT].u.u32, stats[ST_F_BCK].u.u32);
1584
1585 chunk_appendf(out,
1586 /* rest of backend: nothing, down transitions, total downtime, throttle */
1587 "<td class=ac>&nbsp;</td><td>%d</td>"
1588 "<td>%s</td>"
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001589 "<td></td>",
William Lallemand74c24fb2016-11-21 17:18:36 +01001590 stats[ST_F_CHKDOWN].u.u32,
1591 stats[ST_F_DOWNTIME].type ? human_time(stats[ST_F_DOWNTIME].u.u32, 1) : "&nbsp;");
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001592
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001593 if (flags & STAT_SHMODULES) {
1594 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001595 chunk_appendf(out, "<td>");
1596
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001597 if (stats_px_get_cap(mod->domain_flags) & STATS_PX_CAP_BE) {
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001598 chunk_appendf(out,
1599 "<u>%s<div class=tips><table class=det>",
1600 mod->name);
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001601 for (j = 0; j < mod->stats_count; ++j) {
1602 chunk_appendf(out,
1603 "<tr><th>%s</th><td>%s</td></tr>",
1604 mod->stats[j].desc, field_to_html_str(&stats[ST_F_TOTAL_FIELDS + i]));
1605 ++i;
1606 }
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001607 chunk_appendf(out, "</table></div></u>");
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001608 } else {
1609 i += mod->stats_count;
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001610 }
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001611
1612 chunk_appendf(out, "</td>");
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001613 }
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001614 }
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001615
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001616 chunk_appendf(out, "</tr>");
William Lallemand74c24fb2016-11-21 17:18:36 +01001617 }
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001618
William Lallemand74c24fb2016-11-21 17:18:36 +01001619 return 1;
1620}
1621
Amaury Denoyelle97323c92020-10-02 18:32:01 +02001622int stats_dump_one_line(const struct field *stats, size_t stats_count,
1623 struct appctx *appctx)
William Lallemand74c24fb2016-11-21 17:18:36 +01001624{
Willy Tarreau91cefca2022-05-03 17:08:29 +02001625 struct show_stat_ctx *ctx = appctx->svcctx;
Simon Horman05ee2132017-01-04 09:37:25 +01001626 int ret;
1627
Willy Tarreau91cefca2022-05-03 17:08:29 +02001628 if (ctx->flags & STAT_FMT_HTML)
1629 ret = stats_dump_fields_html(&trash, stats, ctx->flags);
1630 else if (ctx->flags & STAT_FMT_TYPED)
1631 ret = stats_dump_fields_typed(&trash, stats, stats_count, ctx->flags, ctx->domain);
1632 else if (ctx->flags & STAT_FMT_JSON)
1633 ret = stats_dump_fields_json(&trash, stats, stats_count, ctx->flags, ctx->domain);
William Lallemand74c24fb2016-11-21 17:18:36 +01001634 else
Willy Tarreau91cefca2022-05-03 17:08:29 +02001635 ret = stats_dump_fields_csv(&trash, stats, stats_count, ctx->flags, ctx->domain);
Simon Horman05ee2132017-01-04 09:37:25 +01001636
1637 if (ret)
Willy Tarreau91cefca2022-05-03 17:08:29 +02001638 ctx->flags |= STAT_STARTED;
Simon Horman05ee2132017-01-04 09:37:25 +01001639
1640 return ret;
William Lallemand74c24fb2016-11-21 17:18:36 +01001641}
1642
William Dauchy0ef54392021-01-17 18:27:45 +01001643/* Fill <stats> with the frontend statistics. <stats> is preallocated array of
Christopher Fauletd808f172021-01-22 17:33:22 +01001644 * length <len>. If <selected_field> is != NULL, only fill this one. The length
1645 * of the array must be at least ST_F_TOTAL_FIELDS. If this length is less than
1646 * this value, or if the selected field is not implemented for frontends, the
1647 * function returns 0, otherwise, it returns 1.
William Lallemand74c24fb2016-11-21 17:18:36 +01001648 */
William Dauchy0ef54392021-01-17 18:27:45 +01001649int stats_fill_fe_stats(struct proxy *px, struct field *stats, int len,
1650 enum stat_field *selected_field)
William Lallemand74c24fb2016-11-21 17:18:36 +01001651{
William Dauchy0ef54392021-01-17 18:27:45 +01001652 enum stat_field current_field = (selected_field != NULL ? *selected_field : 0);
William Dauchy0ef54392021-01-17 18:27:45 +01001653
William Lallemand74c24fb2016-11-21 17:18:36 +01001654 if (len < ST_F_TOTAL_FIELDS)
1655 return 0;
1656
William Dauchy0ef54392021-01-17 18:27:45 +01001657 for (; current_field < ST_F_TOTAL_FIELDS; current_field++) {
Christopher Faulet8596bfb2021-01-25 15:16:41 +01001658 struct field metric = { 0 };
1659
William Dauchy0ef54392021-01-17 18:27:45 +01001660 switch (current_field) {
1661 case ST_F_PXNAME:
1662 metric = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
1663 break;
1664 case ST_F_SVNAME:
1665 metric = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, "FRONTEND");
1666 break;
1667 case ST_F_MODE:
1668 metric = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
1669 break;
1670 case ST_F_SCUR:
1671 metric = mkf_u32(0, px->feconn);
1672 break;
1673 case ST_F_SMAX:
1674 metric = mkf_u32(FN_MAX, px->fe_counters.conn_max);
1675 break;
1676 case ST_F_SLIM:
1677 metric = mkf_u32(FO_CONFIG|FN_LIMIT, px->maxconn);
1678 break;
1679 case ST_F_STOT:
1680 metric = mkf_u64(FN_COUNTER, px->fe_counters.cum_sess);
1681 break;
1682 case ST_F_BIN:
1683 metric = mkf_u64(FN_COUNTER, px->fe_counters.bytes_in);
1684 break;
1685 case ST_F_BOUT:
1686 metric = mkf_u64(FN_COUNTER, px->fe_counters.bytes_out);
1687 break;
1688 case ST_F_DREQ:
1689 metric = mkf_u64(FN_COUNTER, px->fe_counters.denied_req);
1690 break;
1691 case ST_F_DRESP:
1692 metric = mkf_u64(FN_COUNTER, px->fe_counters.denied_resp);
1693 break;
1694 case ST_F_EREQ:
1695 metric = mkf_u64(FN_COUNTER, px->fe_counters.failed_req);
1696 break;
1697 case ST_F_DCON:
1698 metric = mkf_u64(FN_COUNTER, px->fe_counters.denied_conn);
1699 break;
1700 case ST_F_DSES:
1701 metric = mkf_u64(FN_COUNTER, px->fe_counters.denied_sess);
1702 break;
Aurelien DARRAGONcddec0a2022-09-09 15:58:57 +02001703 case ST_F_STATUS: {
1704 const char *state;
1705
1706 if (px->flags & (PR_FL_DISABLED|PR_FL_STOPPED))
1707 state = "STOP";
1708 else if (px->flags & PR_FL_PAUSED)
1709 state = "PAUSED";
1710 else
1711 state = "OPEN";
1712 metric = mkf_str(FO_STATUS, state);
William Dauchy0ef54392021-01-17 18:27:45 +01001713 break;
Aurelien DARRAGONcddec0a2022-09-09 15:58:57 +02001714 }
William Dauchy0ef54392021-01-17 18:27:45 +01001715 case ST_F_PID:
Willy Tarreaue8422bf2021-06-15 09:08:18 +02001716 metric = mkf_u32(FO_KEY, 1);
William Dauchy0ef54392021-01-17 18:27:45 +01001717 break;
1718 case ST_F_IID:
1719 metric = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
1720 break;
1721 case ST_F_SID:
1722 metric = mkf_u32(FO_KEY|FS_SERVICE, 0);
1723 break;
1724 case ST_F_TYPE:
1725 metric = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_FE);
1726 break;
1727 case ST_F_RATE:
1728 metric = mkf_u32(FN_RATE, read_freq_ctr(&px->fe_sess_per_sec));
1729 break;
1730 case ST_F_RATE_LIM:
1731 metric = mkf_u32(FO_CONFIG|FN_LIMIT, px->fe_sps_lim);
1732 break;
1733 case ST_F_RATE_MAX:
1734 metric = mkf_u32(FN_MAX, px->fe_counters.sps_max);
1735 break;
1736 case ST_F_WREW:
1737 metric = mkf_u64(FN_COUNTER, px->fe_counters.failed_rewrites);
1738 break;
1739 case ST_F_EINT:
1740 metric = mkf_u64(FN_COUNTER, px->fe_counters.internal_errors);
1741 break;
1742 case ST_F_HRSP_1XX:
William Dauchy2107a0f2021-01-22 21:09:48 +01001743 if (px->mode == PR_MODE_HTTP)
1744 metric = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[1]);
William Dauchy0ef54392021-01-17 18:27:45 +01001745 break;
1746 case ST_F_HRSP_2XX:
William Dauchy2107a0f2021-01-22 21:09:48 +01001747 if (px->mode == PR_MODE_HTTP)
1748 metric = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[2]);
William Dauchy0ef54392021-01-17 18:27:45 +01001749 break;
1750 case ST_F_HRSP_3XX:
William Dauchy2107a0f2021-01-22 21:09:48 +01001751 if (px->mode == PR_MODE_HTTP)
1752 metric = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[3]);
William Dauchy0ef54392021-01-17 18:27:45 +01001753 break;
1754 case ST_F_HRSP_4XX:
William Dauchy2107a0f2021-01-22 21:09:48 +01001755 if (px->mode == PR_MODE_HTTP)
1756 metric = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[4]);
William Dauchy0ef54392021-01-17 18:27:45 +01001757 break;
1758 case ST_F_HRSP_5XX:
William Dauchy2107a0f2021-01-22 21:09:48 +01001759 if (px->mode == PR_MODE_HTTP)
1760 metric = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[5]);
William Dauchy0ef54392021-01-17 18:27:45 +01001761 break;
1762 case ST_F_HRSP_OTHER:
William Dauchy2107a0f2021-01-22 21:09:48 +01001763 if (px->mode == PR_MODE_HTTP)
1764 metric = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[0]);
William Dauchy0ef54392021-01-17 18:27:45 +01001765 break;
1766 case ST_F_INTERCEPTED:
William Dauchy2107a0f2021-01-22 21:09:48 +01001767 if (px->mode == PR_MODE_HTTP)
1768 metric = mkf_u64(FN_COUNTER, px->fe_counters.intercepted_req);
William Dauchy0ef54392021-01-17 18:27:45 +01001769 break;
1770 case ST_F_CACHE_LOOKUPS:
William Dauchy2107a0f2021-01-22 21:09:48 +01001771 if (px->mode == PR_MODE_HTTP)
1772 metric = mkf_u64(FN_COUNTER, px->fe_counters.p.http.cache_lookups);
William Dauchy0ef54392021-01-17 18:27:45 +01001773 break;
1774 case ST_F_CACHE_HITS:
William Dauchy2107a0f2021-01-22 21:09:48 +01001775 if (px->mode == PR_MODE_HTTP)
1776 metric = mkf_u64(FN_COUNTER, px->fe_counters.p.http.cache_hits);
William Dauchy0ef54392021-01-17 18:27:45 +01001777 break;
1778 case ST_F_REQ_RATE:
1779 metric = mkf_u32(FN_RATE, read_freq_ctr(&px->fe_req_per_sec));
1780 break;
1781 case ST_F_REQ_RATE_MAX:
1782 metric = mkf_u32(FN_MAX, px->fe_counters.p.http.rps_max);
1783 break;
1784 case ST_F_REQ_TOT:
1785 metric = mkf_u64(FN_COUNTER, px->fe_counters.p.http.cum_req);
1786 break;
1787 case ST_F_COMP_IN:
1788 metric = mkf_u64(FN_COUNTER, px->fe_counters.comp_in);
1789 break;
1790 case ST_F_COMP_OUT:
1791 metric = mkf_u64(FN_COUNTER, px->fe_counters.comp_out);
1792 break;
1793 case ST_F_COMP_BYP:
1794 metric = mkf_u64(FN_COUNTER, px->fe_counters.comp_byp);
1795 break;
1796 case ST_F_COMP_RSP:
1797 metric = mkf_u64(FN_COUNTER, px->fe_counters.p.http.comp_rsp);
1798 break;
1799 case ST_F_CONN_RATE:
1800 metric = mkf_u32(FN_RATE, read_freq_ctr(&px->fe_conn_per_sec));
1801 break;
1802 case ST_F_CONN_RATE_MAX:
1803 metric = mkf_u32(FN_MAX, px->fe_counters.cps_max);
1804 break;
1805 case ST_F_CONN_TOT:
1806 metric = mkf_u64(FN_COUNTER, px->fe_counters.cum_conn);
1807 break;
1808 default:
Christopher Fauletd808f172021-01-22 17:33:22 +01001809 /* not used for frontends. If a specific metric
1810 * is requested, return an error. Otherwise continue.
1811 */
1812 if (selected_field != NULL)
1813 return 0;
1814 continue;
William Dauchy0ef54392021-01-17 18:27:45 +01001815 }
1816 stats[current_field] = metric;
1817 if (selected_field != NULL)
1818 break;
William Lallemand74c24fb2016-11-21 17:18:36 +01001819 }
William Lallemand74c24fb2016-11-21 17:18:36 +01001820 return 1;
1821}
1822
1823/* Dumps a frontend's line to the trash for the current proxy <px> and uses
Willy Tarreaucaff6312022-05-27 10:17:46 +02001824 * the state from stream connector <sc>. The caller is responsible for clearing
William Lallemand74c24fb2016-11-21 17:18:36 +01001825 * the trash if needed. Returns non-zero if it emits anything, zero otherwise.
1826 */
Willy Tarreaucaff6312022-05-27 10:17:46 +02001827static int stats_dump_fe_stats(struct stconn *sc, struct proxy *px)
William Lallemand74c24fb2016-11-21 17:18:36 +01001828{
Willy Tarreaucaff6312022-05-27 10:17:46 +02001829 struct appctx *appctx = __sc_appctx(sc);
Willy Tarreau91cefca2022-05-03 17:08:29 +02001830 struct show_stat_ctx *ctx = appctx->svcctx;
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02001831 struct field *stats = stat_l[STATS_DOMAIN_PROXY];
1832 struct stats_module *mod;
1833 size_t stats_count = ST_F_TOTAL_FIELDS;
William Lallemand74c24fb2016-11-21 17:18:36 +01001834
1835 if (!(px->cap & PR_CAP_FE))
1836 return 0;
1837
Willy Tarreau91cefca2022-05-03 17:08:29 +02001838 if ((ctx->flags & STAT_BOUND) && !(ctx->type & (1 << STATS_TYPE_FE)))
William Lallemand74c24fb2016-11-21 17:18:36 +01001839 return 0;
1840
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02001841 memset(stats, 0, sizeof(struct field) * stat_count[STATS_DOMAIN_PROXY]);
1842
William Dauchy0ef54392021-01-17 18:27:45 +01001843 if (!stats_fill_fe_stats(px, stats, ST_F_TOTAL_FIELDS, NULL))
William Lallemand74c24fb2016-11-21 17:18:36 +01001844 return 0;
1845
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02001846 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
1847 void *counters;
1848
1849 if (!(stats_px_get_cap(mod->domain_flags) & STATS_PX_CAP_FE)) {
1850 stats_count += mod->stats_count;
1851 continue;
1852 }
1853
1854 counters = EXTRA_COUNTERS_GET(px->extra_counters_fe, mod);
1855 mod->fill_stats(counters, stats + stats_count);
1856 stats_count += mod->stats_count;
1857 }
1858
1859 return stats_dump_one_line(stats, stats_count, appctx);
William Lallemand74c24fb2016-11-21 17:18:36 +01001860}
1861
William Dauchy655e14e2021-02-14 23:22:54 +01001862/* Fill <stats> with the listener statistics. <stats> is preallocated array of
1863 * length <len>. The length of the array must be at least ST_F_TOTAL_FIELDS. If
1864 * this length is less then this value, the function returns 0, otherwise, it
1865 * returns 1. If selected_field is != NULL, only fill this one. <flags> can
1866 * take the value STAT_SHLGNDS.
William Lallemand74c24fb2016-11-21 17:18:36 +01001867 */
1868int stats_fill_li_stats(struct proxy *px, struct listener *l, int flags,
William Dauchy655e14e2021-02-14 23:22:54 +01001869 struct field *stats, int len, enum stat_field *selected_field)
William Lallemand74c24fb2016-11-21 17:18:36 +01001870{
William Dauchy655e14e2021-02-14 23:22:54 +01001871 enum stat_field current_field = (selected_field != NULL ? *selected_field : 0);
Willy Tarreau83061a82018-07-13 11:56:34 +02001872 struct buffer *out = get_trash_chunk();
William Lallemand74c24fb2016-11-21 17:18:36 +01001873
1874 if (len < ST_F_TOTAL_FIELDS)
1875 return 0;
1876
1877 if (!l->counters)
1878 return 0;
1879
1880 chunk_reset(out);
William Lallemand74c24fb2016-11-21 17:18:36 +01001881
William Dauchy655e14e2021-02-14 23:22:54 +01001882 for (; current_field < ST_F_TOTAL_FIELDS; current_field++) {
1883 struct field metric = { 0 };
William Lallemand74c24fb2016-11-21 17:18:36 +01001884
William Dauchy655e14e2021-02-14 23:22:54 +01001885 switch (current_field) {
1886 case ST_F_PXNAME:
1887 metric = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
1888 break;
1889 case ST_F_SVNAME:
1890 metric = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, l->name);
1891 break;
1892 case ST_F_MODE:
1893 metric = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
1894 break;
1895 case ST_F_SCUR:
1896 metric = mkf_u32(0, l->nbconn);
1897 break;
1898 case ST_F_SMAX:
1899 metric = mkf_u32(FN_MAX, l->counters->conn_max);
1900 break;
1901 case ST_F_SLIM:
1902 metric = mkf_u32(FO_CONFIG|FN_LIMIT, l->maxconn);
1903 break;
1904 case ST_F_STOT:
1905 metric = mkf_u64(FN_COUNTER, l->counters->cum_conn);
1906 break;
1907 case ST_F_BIN:
1908 metric = mkf_u64(FN_COUNTER, l->counters->bytes_in);
1909 break;
1910 case ST_F_BOUT:
1911 metric = mkf_u64(FN_COUNTER, l->counters->bytes_out);
1912 break;
1913 case ST_F_DREQ:
1914 metric = mkf_u64(FN_COUNTER, l->counters->denied_req);
1915 break;
1916 case ST_F_DRESP:
1917 metric = mkf_u64(FN_COUNTER, l->counters->denied_resp);
1918 break;
1919 case ST_F_EREQ:
1920 metric = mkf_u64(FN_COUNTER, l->counters->failed_req);
1921 break;
1922 case ST_F_DCON:
1923 metric = mkf_u64(FN_COUNTER, l->counters->denied_conn);
1924 break;
1925 case ST_F_DSES:
1926 metric = mkf_u64(FN_COUNTER, l->counters->denied_sess);
1927 break;
1928 case ST_F_STATUS:
William Dauchy3679d0c2021-02-14 23:22:55 +01001929 metric = mkf_str(FO_STATUS, li_status_st[get_li_status(l)]);
William Dauchy655e14e2021-02-14 23:22:54 +01001930 break;
1931 case ST_F_PID:
Willy Tarreaue8422bf2021-06-15 09:08:18 +02001932 metric = mkf_u32(FO_KEY, 1);
William Dauchy655e14e2021-02-14 23:22:54 +01001933 break;
1934 case ST_F_IID:
1935 metric = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
1936 break;
1937 case ST_F_SID:
1938 metric = mkf_u32(FO_KEY|FS_SERVICE, l->luid);
1939 break;
1940 case ST_F_TYPE:
1941 metric = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_SO);
1942 break;
1943 case ST_F_WREW:
1944 metric = mkf_u64(FN_COUNTER, l->counters->failed_rewrites);
1945 break;
1946 case ST_F_EINT:
1947 metric = mkf_u64(FN_COUNTER, l->counters->internal_errors);
1948 break;
1949 case ST_F_ADDR:
1950 if (flags & STAT_SHLGNDS) {
1951 char str[INET6_ADDRSTRLEN];
1952 int port;
William Lallemand74c24fb2016-11-21 17:18:36 +01001953
William Dauchy655e14e2021-02-14 23:22:54 +01001954 port = get_host_port(&l->rx.addr);
1955 switch (addr_to_str(&l->rx.addr, str, sizeof(str))) {
1956 case AF_INET:
1957 metric = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
1958 chunk_appendf(out, "%s:%d", str, port);
1959 break;
1960 case AF_INET6:
1961 metric = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
1962 chunk_appendf(out, "[%s]:%d", str, port);
1963 break;
1964 case AF_UNIX:
1965 metric = mkf_str(FO_CONFIG|FS_SERVICE, "unix");
1966 break;
1967 case -1:
1968 metric = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
1969 chunk_strcat(out, strerror(errno));
1970 break;
1971 default: /* address family not supported */
1972 break;
1973 }
1974 }
1975 break;
1976 default:
1977 /* not used for listen. If a specific metric
1978 * is requested, return an error. Otherwise continue.
1979 */
1980 if (selected_field != NULL)
1981 return 0;
1982 continue;
William Lallemand74c24fb2016-11-21 17:18:36 +01001983 }
William Dauchy655e14e2021-02-14 23:22:54 +01001984 stats[current_field] = metric;
1985 if (selected_field != NULL)
1986 break;
William Lallemand74c24fb2016-11-21 17:18:36 +01001987 }
William Lallemand74c24fb2016-11-21 17:18:36 +01001988 return 1;
1989}
1990
1991/* Dumps a line for listener <l> and proxy <px> to the trash and uses the state
Willy Tarreaucaff6312022-05-27 10:17:46 +02001992 * from stream connector <sc>. The caller is responsible for clearing the trash
Willy Tarreau43241ff2019-10-09 11:27:51 +02001993 * if needed. Returns non-zero if it emits anything, zero otherwise.
William Lallemand74c24fb2016-11-21 17:18:36 +01001994 */
Willy Tarreaucaff6312022-05-27 10:17:46 +02001995static int stats_dump_li_stats(struct stconn *sc, struct proxy *px, struct listener *l)
William Lallemand74c24fb2016-11-21 17:18:36 +01001996{
Willy Tarreaucaff6312022-05-27 10:17:46 +02001997 struct appctx *appctx = __sc_appctx(sc);
Willy Tarreau91cefca2022-05-03 17:08:29 +02001998 struct show_stat_ctx *ctx = appctx->svcctx;
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02001999 struct field *stats = stat_l[STATS_DOMAIN_PROXY];
2000 struct stats_module *mod;
2001 size_t stats_count = ST_F_TOTAL_FIELDS;
2002
2003 memset(stats, 0, sizeof(struct field) * stat_count[STATS_DOMAIN_PROXY]);
William Lallemand74c24fb2016-11-21 17:18:36 +01002004
Willy Tarreau91cefca2022-05-03 17:08:29 +02002005 if (!stats_fill_li_stats(px, l, ctx->flags, stats,
William Dauchy655e14e2021-02-14 23:22:54 +01002006 ST_F_TOTAL_FIELDS, NULL))
William Lallemand74c24fb2016-11-21 17:18:36 +01002007 return 0;
2008
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02002009 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
2010 void *counters;
2011
2012 if (!(stats_px_get_cap(mod->domain_flags) & STATS_PX_CAP_LI)) {
2013 stats_count += mod->stats_count;
2014 continue;
2015 }
2016
2017 counters = EXTRA_COUNTERS_GET(l->extra_counters, mod);
2018 mod->fill_stats(counters, stats + stats_count);
2019 stats_count += mod->stats_count;
2020 }
2021
2022 return stats_dump_one_line(stats, stats_count, appctx);
William Lallemand74c24fb2016-11-21 17:18:36 +01002023}
2024
2025enum srv_stats_state {
2026 SRV_STATS_STATE_DOWN = 0,
2027 SRV_STATS_STATE_DOWN_AGENT,
2028 SRV_STATS_STATE_GOING_UP,
2029 SRV_STATS_STATE_UP_GOING_DOWN,
2030 SRV_STATS_STATE_UP,
2031 SRV_STATS_STATE_NOLB_GOING_DOWN,
2032 SRV_STATS_STATE_NOLB,
2033 SRV_STATS_STATE_DRAIN_GOING_DOWN,
2034 SRV_STATS_STATE_DRAIN,
2035 SRV_STATS_STATE_DRAIN_AGENT,
2036 SRV_STATS_STATE_NO_CHECK,
2037
2038 SRV_STATS_STATE_COUNT, /* Must be last */
2039};
2040
2041static const char *srv_hlt_st[SRV_STATS_STATE_COUNT] = {
2042 [SRV_STATS_STATE_DOWN] = "DOWN",
2043 [SRV_STATS_STATE_DOWN_AGENT] = "DOWN (agent)",
2044 [SRV_STATS_STATE_GOING_UP] = "DOWN %d/%d",
2045 [SRV_STATS_STATE_UP_GOING_DOWN] = "UP %d/%d",
2046 [SRV_STATS_STATE_UP] = "UP",
2047 [SRV_STATS_STATE_NOLB_GOING_DOWN] = "NOLB %d/%d",
2048 [SRV_STATS_STATE_NOLB] = "NOLB",
2049 [SRV_STATS_STATE_DRAIN_GOING_DOWN] = "DRAIN %d/%d",
2050 [SRV_STATS_STATE_DRAIN] = "DRAIN",
2051 [SRV_STATS_STATE_DRAIN_AGENT] = "DRAIN (agent)",
2052 [SRV_STATS_STATE_NO_CHECK] = "no check"
2053};
2054
William Dauchyd3a9a492021-01-25 17:29:03 +01002055/* Compute server state helper
William Lallemand74c24fb2016-11-21 17:18:36 +01002056 */
William Dauchyd3a9a492021-01-25 17:29:03 +01002057static void stats_fill_sv_stats_computestate(struct server *sv, struct server *ref,
2058 enum srv_stats_state *state)
William Lallemand74c24fb2016-11-21 17:18:36 +01002059{
Emeric Brun52a91d32017-08-31 14:41:55 +02002060 if (sv->cur_state == SRV_ST_RUNNING || sv->cur_state == SRV_ST_STARTING) {
William Lallemand74c24fb2016-11-21 17:18:36 +01002061 if ((ref->check.state & CHK_ST_ENABLED) &&
2062 (ref->check.health < ref->check.rise + ref->check.fall - 1)) {
William Dauchyd3a9a492021-01-25 17:29:03 +01002063 *state = SRV_STATS_STATE_UP_GOING_DOWN;
William Lallemand74c24fb2016-11-21 17:18:36 +01002064 } else {
William Dauchyd3a9a492021-01-25 17:29:03 +01002065 *state = SRV_STATS_STATE_UP;
William Lallemand74c24fb2016-11-21 17:18:36 +01002066 }
2067
Emeric Brun52a91d32017-08-31 14:41:55 +02002068 if (sv->cur_admin & SRV_ADMF_DRAIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +01002069 if (ref->agent.state & CHK_ST_ENABLED)
William Dauchyd3a9a492021-01-25 17:29:03 +01002070 *state = SRV_STATS_STATE_DRAIN_AGENT;
2071 else if (*state == SRV_STATS_STATE_UP_GOING_DOWN)
2072 *state = SRV_STATS_STATE_DRAIN_GOING_DOWN;
William Lallemand74c24fb2016-11-21 17:18:36 +01002073 else
William Dauchyd3a9a492021-01-25 17:29:03 +01002074 *state = SRV_STATS_STATE_DRAIN;
William Lallemand74c24fb2016-11-21 17:18:36 +01002075 }
2076
William Dauchyd3a9a492021-01-25 17:29:03 +01002077 if (*state == SRV_STATS_STATE_UP && !(ref->check.state & CHK_ST_ENABLED)) {
2078 *state = SRV_STATS_STATE_NO_CHECK;
William Lallemand74c24fb2016-11-21 17:18:36 +01002079 }
2080 }
Emeric Brun52a91d32017-08-31 14:41:55 +02002081 else if (sv->cur_state == SRV_ST_STOPPING) {
William Lallemand74c24fb2016-11-21 17:18:36 +01002082 if ((!(sv->check.state & CHK_ST_ENABLED) && !sv->track) ||
2083 (ref->check.health == ref->check.rise + ref->check.fall - 1)) {
William Dauchyd3a9a492021-01-25 17:29:03 +01002084 *state = SRV_STATS_STATE_NOLB;
William Lallemand74c24fb2016-11-21 17:18:36 +01002085 } else {
William Dauchyd3a9a492021-01-25 17:29:03 +01002086 *state = SRV_STATS_STATE_NOLB_GOING_DOWN;
William Lallemand74c24fb2016-11-21 17:18:36 +01002087 }
2088 }
2089 else { /* stopped */
2090 if ((ref->agent.state & CHK_ST_ENABLED) && !ref->agent.health) {
William Dauchyd3a9a492021-01-25 17:29:03 +01002091 *state = SRV_STATS_STATE_DOWN_AGENT;
William Lallemand74c24fb2016-11-21 17:18:36 +01002092 } else if ((ref->check.state & CHK_ST_ENABLED) && !ref->check.health) {
William Dauchyd3a9a492021-01-25 17:29:03 +01002093 *state = SRV_STATS_STATE_DOWN; /* DOWN */
William Lallemand74c24fb2016-11-21 17:18:36 +01002094 } else if ((ref->agent.state & CHK_ST_ENABLED) || (ref->check.state & CHK_ST_ENABLED)) {
William Dauchyd3a9a492021-01-25 17:29:03 +01002095 *state = SRV_STATS_STATE_GOING_UP;
William Lallemand74c24fb2016-11-21 17:18:36 +01002096 } else {
William Dauchyd3a9a492021-01-25 17:29:03 +01002097 *state = SRV_STATS_STATE_DOWN; /* DOWN, unchecked */
William Lallemand74c24fb2016-11-21 17:18:36 +01002098 }
2099 }
William Dauchyd3a9a492021-01-25 17:29:03 +01002100}
William Lallemand74c24fb2016-11-21 17:18:36 +01002101
William Dauchyd3a9a492021-01-25 17:29:03 +01002102/* Fill <stats> with the backend statistics. <stats> is preallocated array of
2103 * length <len>. If <selected_field> is != NULL, only fill this one. The length
2104 * of the array must be at least ST_F_TOTAL_FIELDS. If this length is less than
2105 * this value, or if the selected field is not implemented for servers, the
2106 * function returns 0, otherwise, it returns 1. <flags> can take the value
2107 * STAT_SHLGNDS.
2108 */
2109int stats_fill_sv_stats(struct proxy *px, struct server *sv, int flags,
2110 struct field *stats, int len,
2111 enum stat_field *selected_field)
2112{
2113 enum stat_field current_field = (selected_field != NULL ? *selected_field : 0);
2114 struct server *via = sv->track ? sv->track : sv;
2115 struct server *ref = via;
2116 enum srv_stats_state state = 0;
2117 char str[INET6_ADDRSTRLEN];
2118 struct buffer *out = get_trash_chunk();
2119 char *fld_status;
2120 long long srv_samples_counter;
2121 unsigned int srv_samples_window = TIME_STATS_SAMPLES;
William Lallemand74c24fb2016-11-21 17:18:36 +01002122
William Dauchyd3a9a492021-01-25 17:29:03 +01002123 if (len < ST_F_TOTAL_FIELDS)
2124 return 0;
William Lallemand74c24fb2016-11-21 17:18:36 +01002125
William Dauchyd3a9a492021-01-25 17:29:03 +01002126 chunk_reset(out);
William Lallemand74c24fb2016-11-21 17:18:36 +01002127
William Dauchyd3a9a492021-01-25 17:29:03 +01002128 /* compute state for later use */
2129 if (selected_field == NULL || *selected_field == ST_F_STATUS ||
2130 *selected_field == ST_F_CHECK_RISE || *selected_field == ST_F_CHECK_FALL ||
2131 *selected_field == ST_F_CHECK_HEALTH || *selected_field == ST_F_HANAFAIL) {
2132 /* we have "via" which is the tracked server as described in the configuration,
2133 * and "ref" which is the checked server and the end of the chain.
2134 */
2135 while (ref->track)
2136 ref = ref->track;
2137 stats_fill_sv_stats_computestate(sv, ref, &state);
William Lallemand74c24fb2016-11-21 17:18:36 +01002138 }
2139
William Dauchyd3a9a492021-01-25 17:29:03 +01002140 /* compue time values for later use */
2141 if (selected_field == NULL || *selected_field == ST_F_QTIME ||
2142 *selected_field == ST_F_CTIME || *selected_field == ST_F_RTIME ||
2143 *selected_field == ST_F_TTIME) {
2144 srv_samples_counter = (px->mode == PR_MODE_HTTP) ? sv->counters.p.http.cum_req : sv->counters.cum_lbconn;
2145 if (srv_samples_counter < TIME_STATS_SAMPLES && srv_samples_counter > 0)
2146 srv_samples_window = srv_samples_counter;
William Lallemand74c24fb2016-11-21 17:18:36 +01002147 }
2148
William Dauchyd3a9a492021-01-25 17:29:03 +01002149 for (; current_field < ST_F_TOTAL_FIELDS; current_field++) {
2150 struct field metric = { 0 };
William Lallemand74c24fb2016-11-21 17:18:36 +01002151
William Dauchyd3a9a492021-01-25 17:29:03 +01002152 switch (current_field) {
2153 case ST_F_PXNAME:
2154 metric = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
2155 break;
2156 case ST_F_SVNAME:
2157 metric = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, sv->id);
2158 break;
2159 case ST_F_MODE:
2160 metric = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
Christopher Faulet3888b8c2021-01-27 13:32:25 +01002161 break;
William Dauchyd3a9a492021-01-25 17:29:03 +01002162 case ST_F_QCUR:
Willy Tarreaua0570452021-06-18 09:30:30 +02002163 metric = mkf_u32(0, sv->queue.length);
William Dauchyd3a9a492021-01-25 17:29:03 +01002164 break;
2165 case ST_F_QMAX:
2166 metric = mkf_u32(FN_MAX, sv->counters.nbpend_max);
2167 break;
2168 case ST_F_SCUR:
2169 metric = mkf_u32(0, sv->cur_sess);
2170 break;
2171 case ST_F_SMAX:
2172 metric = mkf_u32(FN_MAX, sv->counters.cur_sess_max);
2173 break;
2174 case ST_F_SLIM:
2175 if (sv->maxconn)
2176 metric = mkf_u32(FO_CONFIG|FN_LIMIT, sv->maxconn);
2177 break;
2178 case ST_F_SRV_ICUR:
2179 metric = mkf_u32(0, sv->curr_idle_conns);
2180 break;
2181 case ST_F_SRV_ILIM:
2182 if (sv->max_idle_conns != -1)
2183 metric = mkf_u32(FO_CONFIG|FN_LIMIT, sv->max_idle_conns);
2184 break;
2185 case ST_F_STOT:
2186 metric = mkf_u64(FN_COUNTER, sv->counters.cum_sess);
2187 break;
2188 case ST_F_BIN:
2189 metric = mkf_u64(FN_COUNTER, sv->counters.bytes_in);
2190 break;
2191 case ST_F_BOUT:
2192 metric = mkf_u64(FN_COUNTER, sv->counters.bytes_out);
2193 break;
2194 case ST_F_DRESP:
2195 metric = mkf_u64(FN_COUNTER, sv->counters.denied_resp);
2196 break;
2197 case ST_F_ECON:
2198 metric = mkf_u64(FN_COUNTER, sv->counters.failed_conns);
2199 break;
2200 case ST_F_ERESP:
2201 metric = mkf_u64(FN_COUNTER, sv->counters.failed_resp);
2202 break;
2203 case ST_F_WRETR:
2204 metric = mkf_u64(FN_COUNTER, sv->counters.retries);
2205 break;
2206 case ST_F_WREDIS:
2207 metric = mkf_u64(FN_COUNTER, sv->counters.redispatches);
2208 break;
2209 case ST_F_WREW:
2210 metric = mkf_u64(FN_COUNTER, sv->counters.failed_rewrites);
2211 break;
2212 case ST_F_EINT:
2213 metric = mkf_u64(FN_COUNTER, sv->counters.internal_errors);
2214 break;
2215 case ST_F_CONNECT:
2216 metric = mkf_u64(FN_COUNTER, sv->counters.connect);
2217 break;
2218 case ST_F_REUSE:
2219 metric = mkf_u64(FN_COUNTER, sv->counters.reuse);
2220 break;
2221 case ST_F_IDLE_CONN_CUR:
2222 metric = mkf_u32(0, sv->curr_idle_nb);
2223 break;
2224 case ST_F_SAFE_CONN_CUR:
2225 metric = mkf_u32(0, sv->curr_safe_nb);
2226 break;
2227 case ST_F_USED_CONN_CUR:
2228 metric = mkf_u32(0, sv->curr_used_conns);
2229 break;
2230 case ST_F_NEED_CONN_EST:
2231 metric = mkf_u32(0, sv->est_need_conns);
2232 break;
2233 case ST_F_STATUS:
2234 fld_status = chunk_newstr(out);
2235 if (sv->cur_admin & SRV_ADMF_RMAINT)
2236 chunk_appendf(out, "MAINT (resolution)");
2237 else if (sv->cur_admin & SRV_ADMF_IMAINT)
2238 chunk_appendf(out, "MAINT (via %s/%s)", via->proxy->id, via->id);
2239 else if (sv->cur_admin & SRV_ADMF_MAINT)
2240 chunk_appendf(out, "MAINT");
2241 else
2242 chunk_appendf(out,
2243 srv_hlt_st[state],
2244 (ref->cur_state != SRV_ST_STOPPED) ? (ref->check.health - ref->check.rise + 1) : (ref->check.health),
2245 (ref->cur_state != SRV_ST_STOPPED) ? (ref->check.fall) : (ref->check.rise));
William Lallemand74c24fb2016-11-21 17:18:36 +01002246
William Dauchyd3a9a492021-01-25 17:29:03 +01002247 metric = mkf_str(FO_STATUS, fld_status);
2248 break;
2249 case ST_F_LASTCHG:
2250 metric = mkf_u32(FN_AGE, now.tv_sec - sv->last_change);
2251 break;
2252 case ST_F_WEIGHT:
2253 metric = mkf_u32(FN_AVG, (sv->cur_eweight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv);
2254 break;
2255 case ST_F_UWEIGHT:
2256 metric = mkf_u32(FN_AVG, sv->uweight);
2257 break;
2258 case ST_F_ACT:
2259 metric = mkf_u32(FO_STATUS, (sv->flags & SRV_F_BACKUP) ? 0 : 1);
2260 break;
2261 case ST_F_BCK:
2262 metric = mkf_u32(FO_STATUS, (sv->flags & SRV_F_BACKUP) ? 1 : 0);
2263 break;
2264 case ST_F_CHKFAIL:
2265 if (sv->check.state & CHK_ST_ENABLED)
2266 metric = mkf_u64(FN_COUNTER, sv->counters.failed_checks);
2267 break;
2268 case ST_F_CHKDOWN:
2269 if (sv->check.state & CHK_ST_ENABLED)
2270 metric = mkf_u64(FN_COUNTER, sv->counters.down_trans);
2271 break;
2272 case ST_F_DOWNTIME:
2273 if (sv->check.state & CHK_ST_ENABLED)
2274 metric = mkf_u32(FN_COUNTER, srv_downtime(sv));
2275 break;
2276 case ST_F_QLIMIT:
2277 if (sv->maxqueue)
2278 metric = mkf_u32(FO_CONFIG|FS_SERVICE, sv->maxqueue);
2279 break;
2280 case ST_F_PID:
Willy Tarreaue8422bf2021-06-15 09:08:18 +02002281 metric = mkf_u32(FO_KEY, 1);
William Dauchyd3a9a492021-01-25 17:29:03 +01002282 break;
2283 case ST_F_IID:
2284 metric = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
2285 break;
2286 case ST_F_SID:
2287 metric = mkf_u32(FO_KEY|FS_SERVICE, sv->puid);
2288 break;
Aurelien DARRAGON745ce8e2022-11-17 16:34:07 +01002289 case ST_F_SRID:
2290 metric = mkf_u32(FN_COUNTER, sv->rid);
2291 break;
William Dauchyd3a9a492021-01-25 17:29:03 +01002292 case ST_F_THROTTLE:
2293 if (sv->cur_state == SRV_ST_STARTING && !server_is_draining(sv))
2294 metric = mkf_u32(FN_AVG, server_throttle_rate(sv));
2295 break;
2296 case ST_F_LBTOT:
2297 metric = mkf_u64(FN_COUNTER, sv->counters.cum_lbconn);
2298 break;
2299 case ST_F_TRACKED:
2300 if (sv->track) {
2301 char *fld_track = chunk_newstr(out);
2302 chunk_appendf(out, "%s/%s", sv->track->proxy->id, sv->track->id);
2303 metric = mkf_str(FO_CONFIG|FN_NAME|FS_SERVICE, fld_track);
2304 }
2305 break;
2306 case ST_F_TYPE:
2307 metric = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_SV);
2308 break;
2309 case ST_F_RATE:
2310 metric = mkf_u32(FN_RATE, read_freq_ctr(&sv->sess_per_sec));
2311 break;
2312 case ST_F_RATE_MAX:
2313 metric = mkf_u32(FN_MAX, sv->counters.sps_max);
2314 break;
2315 case ST_F_CHECK_STATUS:
2316 if ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED) {
2317 const char *fld_chksts;
Marcin Deraneka8dbdf32020-05-15 20:02:40 +02002318
William Dauchyd3a9a492021-01-25 17:29:03 +01002319 fld_chksts = chunk_newstr(out);
2320 chunk_strcat(out, "* "); // for check in progress
2321 chunk_strcat(out, get_check_status_info(sv->check.status));
2322 if (!(sv->check.state & CHK_ST_INPROGRESS))
2323 fld_chksts += 2; // skip "* "
Willy Tarreau973a9372021-05-12 17:29:14 +02002324 metric = mkf_str(FN_OUTPUT, fld_chksts);
William Dauchyd3a9a492021-01-25 17:29:03 +01002325 }
2326 break;
2327 case ST_F_CHECK_CODE:
2328 if ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED &&
2329 sv->check.status >= HCHK_STATUS_L57DATA)
2330 metric = mkf_u32(FN_OUTPUT, sv->check.code);
2331 break;
2332 case ST_F_CHECK_DURATION:
2333 if ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED &&
2334 sv->check.status >= HCHK_STATUS_CHECKED)
Aurelien DARRAGONb118f2f2022-12-07 14:52:10 +01002335 metric = mkf_u64(FN_DURATION, MAX(sv->check.duration, 0));
William Dauchyd3a9a492021-01-25 17:29:03 +01002336 break;
2337 case ST_F_CHECK_DESC:
2338 if ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED)
2339 metric = mkf_str(FN_OUTPUT, get_check_status_description(sv->check.status));
2340 break;
2341 case ST_F_LAST_CHK:
2342 if ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED)
2343 metric = mkf_str(FN_OUTPUT, sv->check.desc);
2344 break;
2345 case ST_F_CHECK_RISE:
2346 if ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED)
2347 metric = mkf_u32(FO_CONFIG|FS_SERVICE, ref->check.rise);
2348 break;
2349 case ST_F_CHECK_FALL:
2350 if ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED)
2351 metric = mkf_u32(FO_CONFIG|FS_SERVICE, ref->check.fall);
2352 break;
2353 case ST_F_CHECK_HEALTH:
2354 if ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED)
2355 metric = mkf_u32(FO_CONFIG|FS_SERVICE, ref->check.health);
2356 break;
Christopher Faulet59bab612021-07-22 08:04:38 +02002357 case ST_F_AGENT_STATUS:
2358 if ((sv->agent.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED) {
2359 const char *fld_chksts;
2360
2361 fld_chksts = chunk_newstr(out);
2362 chunk_strcat(out, "* "); // for check in progress
2363 chunk_strcat(out, get_check_status_info(sv->agent.status));
2364 if (!(sv->agent.state & CHK_ST_INPROGRESS))
2365 fld_chksts += 2; // skip "* "
2366 metric = mkf_str(FN_OUTPUT, fld_chksts);
2367 }
2368 break;
2369 case ST_F_AGENT_CODE:
2370 if ((sv->agent.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED &&
2371 (sv->agent.status >= HCHK_STATUS_L57DATA))
2372 metric = mkf_u32(FN_OUTPUT, sv->agent.code);
2373 break;
2374 case ST_F_AGENT_DURATION:
2375 if ((sv->agent.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED)
2376 metric = mkf_u64(FN_DURATION, sv->agent.duration);
2377 break;
2378 case ST_F_AGENT_DESC:
2379 if ((sv->agent.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED)
2380 metric = mkf_str(FN_OUTPUT, get_check_status_description(sv->agent.status));
2381 break;
2382 case ST_F_LAST_AGT:
2383 if ((sv->agent.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED)
2384 metric = mkf_str(FN_OUTPUT, sv->agent.desc);
2385 break;
2386 case ST_F_AGENT_RISE:
2387 if ((sv->agent.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED)
2388 metric = mkf_u32(FO_CONFIG|FS_SERVICE, sv->agent.rise);
2389 break;
2390 case ST_F_AGENT_FALL:
2391 if ((sv->agent.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED)
2392 metric = mkf_u32(FO_CONFIG|FS_SERVICE, sv->agent.fall);
2393 break;
2394 case ST_F_AGENT_HEALTH:
2395 if ((sv->agent.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED)
2396 metric = mkf_u32(FO_CONFIG|FS_SERVICE, sv->agent.health);
2397 break;
William Dauchyd3a9a492021-01-25 17:29:03 +01002398 case ST_F_REQ_TOT:
2399 if (px->mode == PR_MODE_HTTP)
2400 metric = mkf_u64(FN_COUNTER, sv->counters.p.http.cum_req);
2401 break;
2402 case ST_F_HRSP_1XX:
2403 if (px->mode == PR_MODE_HTTP)
2404 metric = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[1]);
2405 break;
2406 case ST_F_HRSP_2XX:
2407 if (px->mode == PR_MODE_HTTP)
2408 metric = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[2]);
2409 break;
2410 case ST_F_HRSP_3XX:
2411 if (px->mode == PR_MODE_HTTP)
2412 metric = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[3]);
2413 break;
2414 case ST_F_HRSP_4XX:
2415 if (px->mode == PR_MODE_HTTP)
2416 metric = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[4]);
2417 break;
2418 case ST_F_HRSP_5XX:
2419 if (px->mode == PR_MODE_HTTP)
2420 metric = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[5]);
2421 break;
2422 case ST_F_HRSP_OTHER:
2423 if (px->mode == PR_MODE_HTTP)
2424 metric = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[0]);
2425 break;
2426 case ST_F_HANAFAIL:
2427 if (ref->observe)
2428 metric = mkf_u64(FN_COUNTER, sv->counters.failed_hana);
2429 break;
2430 case ST_F_CLI_ABRT:
2431 metric = mkf_u64(FN_COUNTER, sv->counters.cli_aborts);
2432 break;
2433 case ST_F_SRV_ABRT:
2434 metric = mkf_u64(FN_COUNTER, sv->counters.srv_aborts);
2435 break;
2436 case ST_F_LASTSESS:
2437 metric = mkf_s32(FN_AGE, srv_lastsession(sv));
2438 break;
2439 case ST_F_QTIME:
2440 metric = mkf_u32(FN_AVG, swrate_avg(sv->counters.q_time, srv_samples_window));
2441 break;
2442 case ST_F_CTIME:
2443 metric = mkf_u32(FN_AVG, swrate_avg(sv->counters.c_time, srv_samples_window));
2444 break;
2445 case ST_F_RTIME:
2446 metric = mkf_u32(FN_AVG, swrate_avg(sv->counters.d_time, srv_samples_window));
2447 break;
2448 case ST_F_TTIME:
2449 metric = mkf_u32(FN_AVG, swrate_avg(sv->counters.t_time, srv_samples_window));
2450 break;
2451 case ST_F_QT_MAX:
2452 metric = mkf_u32(FN_MAX, sv->counters.qtime_max);
2453 break;
2454 case ST_F_CT_MAX:
2455 metric = mkf_u32(FN_MAX, sv->counters.ctime_max);
2456 break;
2457 case ST_F_RT_MAX:
2458 metric = mkf_u32(FN_MAX, sv->counters.dtime_max);
2459 break;
2460 case ST_F_TT_MAX:
2461 metric = mkf_u32(FN_MAX, sv->counters.ttime_max);
2462 break;
2463 case ST_F_ADDR:
2464 if (flags & STAT_SHLGNDS) {
2465 switch (addr_to_str(&sv->addr, str, sizeof(str))) {
2466 case AF_INET:
2467 metric = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
2468 chunk_appendf(out, "%s:%d", str, sv->svc_port);
2469 break;
2470 case AF_INET6:
2471 metric = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
2472 chunk_appendf(out, "[%s]:%d", str, sv->svc_port);
2473 break;
2474 case AF_UNIX:
2475 metric = mkf_str(FO_CONFIG|FS_SERVICE, "unix");
2476 break;
2477 case -1:
2478 metric = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
2479 chunk_strcat(out, strerror(errno));
2480 break;
2481 default: /* address family not supported */
2482 break;
2483 }
2484 }
2485 break;
2486 case ST_F_COOKIE:
2487 if (flags & STAT_SHLGNDS && sv->cookie)
2488 metric = mkf_str(FO_CONFIG|FN_NAME|FS_SERVICE, sv->cookie);
2489 break;
2490 default:
2491 /* not used for servers. If a specific metric
2492 * is requested, return an error. Otherwise continue.
2493 */
2494 if (selected_field != NULL)
2495 return 0;
2496 continue;
William Lallemand74c24fb2016-11-21 17:18:36 +01002497 }
William Dauchyd3a9a492021-01-25 17:29:03 +01002498 stats[current_field] = metric;
2499 if (selected_field != NULL)
2500 break;
William Lallemand74c24fb2016-11-21 17:18:36 +01002501 }
William Lallemand74c24fb2016-11-21 17:18:36 +01002502 return 1;
2503}
2504
2505/* Dumps a line for server <sv> and proxy <px> to the trash and uses the state
Willy Tarreaucaff6312022-05-27 10:17:46 +02002506 * from stream connector <sc>, and server state <state>. The caller is
Willy Tarreau43241ff2019-10-09 11:27:51 +02002507 * responsible for clearing the trash if needed. Returns non-zero if it emits
2508 * anything, zero otherwise.
William Lallemand74c24fb2016-11-21 17:18:36 +01002509 */
Willy Tarreaucaff6312022-05-27 10:17:46 +02002510static int stats_dump_sv_stats(struct stconn *sc, struct proxy *px, struct server *sv)
William Lallemand74c24fb2016-11-21 17:18:36 +01002511{
Willy Tarreaucaff6312022-05-27 10:17:46 +02002512 struct appctx *appctx = __sc_appctx(sc);
Willy Tarreau91cefca2022-05-03 17:08:29 +02002513 struct show_stat_ctx *ctx = appctx->svcctx;
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02002514 struct stats_module *mod;
2515 struct field *stats = stat_l[STATS_DOMAIN_PROXY];
2516 size_t stats_count = ST_F_TOTAL_FIELDS;
2517
2518 memset(stats, 0, sizeof(struct field) * stat_count[STATS_DOMAIN_PROXY]);
William Lallemand74c24fb2016-11-21 17:18:36 +01002519
Willy Tarreau91cefca2022-05-03 17:08:29 +02002520 if (!stats_fill_sv_stats(px, sv, ctx->flags, stats,
William Dauchyd3a9a492021-01-25 17:29:03 +01002521 ST_F_TOTAL_FIELDS, NULL))
William Lallemand74c24fb2016-11-21 17:18:36 +01002522 return 0;
2523
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02002524 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
2525 void *counters;
2526
2527 if (stats_get_domain(mod->domain_flags) != STATS_DOMAIN_PROXY)
2528 continue;
2529
2530 if (!(stats_px_get_cap(mod->domain_flags) & STATS_PX_CAP_SRV)) {
2531 stats_count += mod->stats_count;
2532 continue;
2533 }
2534
2535 counters = EXTRA_COUNTERS_GET(sv->extra_counters, mod);
2536 mod->fill_stats(counters, stats + stats_count);
2537 stats_count += mod->stats_count;
2538 }
2539
2540 return stats_dump_one_line(stats, stats_count, appctx);
William Lallemand74c24fb2016-11-21 17:18:36 +01002541}
2542
William Dauchyda3b4662021-01-25 17:29:01 +01002543/* Helper to compute srv values for a given backend
William Lallemand74c24fb2016-11-21 17:18:36 +01002544 */
William Dauchyda3b4662021-01-25 17:29:01 +01002545static void stats_fill_be_stats_computesrv(struct proxy *px, int *nbup, int *nbsrv, int *totuw)
William Lallemand74c24fb2016-11-21 17:18:36 +01002546{
William Dauchyda3b4662021-01-25 17:29:01 +01002547 int nbup_tmp, nbsrv_tmp, totuw_tmp;
Willy Tarreau2fbe6942020-10-23 18:02:54 +02002548 const struct server *srv;
William Lallemand74c24fb2016-11-21 17:18:36 +01002549
William Dauchyda3b4662021-01-25 17:29:01 +01002550 nbup_tmp = nbsrv_tmp = totuw_tmp = 0;
Willy Tarreaubd715102020-10-23 22:44:30 +02002551 for (srv = px->srv; srv; srv = srv->next) {
2552 if (srv->cur_state != SRV_ST_STOPPED) {
William Dauchyda3b4662021-01-25 17:29:01 +01002553 nbup_tmp++;
Willy Tarreaubd715102020-10-23 22:44:30 +02002554 if (srv_currently_usable(srv) &&
2555 (!px->srv_act ^ !(srv->flags & SRV_F_BACKUP)))
William Dauchyda3b4662021-01-25 17:29:01 +01002556 totuw_tmp += srv->uweight;
Willy Tarreau2fbe6942020-10-23 18:02:54 +02002557 }
William Dauchyda3b4662021-01-25 17:29:01 +01002558 nbsrv_tmp++;
Willy Tarreau2fbe6942020-10-23 18:02:54 +02002559 }
2560
Willy Tarreaubd715102020-10-23 22:44:30 +02002561 HA_RWLOCK_RDLOCK(LBPRM_LOCK, &px->lbprm.lock);
2562 if (!px->srv_act && px->lbprm.fbck)
William Dauchyda3b4662021-01-25 17:29:01 +01002563 totuw_tmp = px->lbprm.fbck->uweight;
Willy Tarreaubd715102020-10-23 22:44:30 +02002564 HA_RWLOCK_RDUNLOCK(LBPRM_LOCK, &px->lbprm.lock);
2565
William Dauchyda3b4662021-01-25 17:29:01 +01002566 /* use tmp variable then assign result to make gcc happy */
2567 *nbup = nbup_tmp;
2568 *nbsrv = nbsrv_tmp;
2569 *totuw = totuw_tmp;
2570}
Willy Tarreau2fbe6942020-10-23 18:02:54 +02002571
William Dauchyda3b4662021-01-25 17:29:01 +01002572/* Fill <stats> with the backend statistics. <stats> is preallocated array of
2573 * length <len>. If <selected_field> is != NULL, only fill this one. The length
2574 * of the array must be at least ST_F_TOTAL_FIELDS. If this length is less than
2575 * this value, or if the selected field is not implemented for backends, the
2576 * function returns 0, otherwise, it returns 1. <flags> can take the value
2577 * STAT_SHLGNDS.
2578 */
2579int stats_fill_be_stats(struct proxy *px, int flags, struct field *stats, int len,
2580 enum stat_field *selected_field)
2581{
2582 enum stat_field current_field = (selected_field != NULL ? *selected_field : 0);
2583 long long be_samples_counter;
2584 unsigned int be_samples_window = TIME_STATS_SAMPLES;
2585 struct buffer *out = get_trash_chunk();
2586 int nbup, nbsrv, totuw;
2587 char *fld;
William Lallemand74c24fb2016-11-21 17:18:36 +01002588
William Dauchyda3b4662021-01-25 17:29:01 +01002589 if (len < ST_F_TOTAL_FIELDS)
2590 return 0;
William Lallemand74c24fb2016-11-21 17:18:36 +01002591
William Dauchyda3b4662021-01-25 17:29:01 +01002592 nbup = nbsrv = totuw = 0;
2593 /* some srv values compute for later if we either select all fields or
2594 * need them for one of the mentioned ones */
2595 if (selected_field == NULL || *selected_field == ST_F_STATUS ||
2596 *selected_field == ST_F_UWEIGHT)
2597 stats_fill_be_stats_computesrv(px, &nbup, &nbsrv, &totuw);
William Lallemand74c24fb2016-11-21 17:18:36 +01002598
William Dauchyda3b4662021-01-25 17:29:01 +01002599 /* same here but specific to time fields */
2600 if (selected_field == NULL || *selected_field == ST_F_QTIME ||
2601 *selected_field == ST_F_CTIME || *selected_field == ST_F_RTIME ||
2602 *selected_field == ST_F_TTIME) {
2603 be_samples_counter = (px->mode == PR_MODE_HTTP) ? px->be_counters.p.http.cum_req : px->be_counters.cum_lbconn;
2604 if (be_samples_counter < TIME_STATS_SAMPLES && be_samples_counter > 0)
2605 be_samples_window = be_samples_counter;
William Lallemand74c24fb2016-11-21 17:18:36 +01002606 }
2607
William Dauchyda3b4662021-01-25 17:29:01 +01002608 for (; current_field < ST_F_TOTAL_FIELDS; current_field++) {
2609 struct field metric = { 0 };
William Lallemand74c24fb2016-11-21 17:18:36 +01002610
William Dauchyda3b4662021-01-25 17:29:01 +01002611 switch (current_field) {
2612 case ST_F_PXNAME:
2613 metric = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
2614 break;
2615 case ST_F_SVNAME:
2616 metric = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, "BACKEND");
2617 break;
2618 case ST_F_MODE:
2619 metric = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
2620 break;
2621 case ST_F_QCUR:
Willy Tarreau7f3c1df2021-06-18 09:22:21 +02002622 metric = mkf_u32(0, px->queue.length);
William Dauchyda3b4662021-01-25 17:29:01 +01002623 break;
2624 case ST_F_QMAX:
2625 metric = mkf_u32(FN_MAX, px->be_counters.nbpend_max);
2626 break;
2627 case ST_F_SCUR:
2628 metric = mkf_u32(0, px->beconn);
2629 break;
2630 case ST_F_SMAX:
2631 metric = mkf_u32(FN_MAX, px->be_counters.conn_max);
2632 break;
2633 case ST_F_SLIM:
2634 metric = mkf_u32(FO_CONFIG|FN_LIMIT, px->fullconn);
2635 break;
2636 case ST_F_STOT:
2637 metric = mkf_u64(FN_COUNTER, px->be_counters.cum_conn);
2638 break;
2639 case ST_F_BIN:
2640 metric = mkf_u64(FN_COUNTER, px->be_counters.bytes_in);
2641 break;
2642 case ST_F_BOUT:
2643 metric = mkf_u64(FN_COUNTER, px->be_counters.bytes_out);
2644 break;
2645 case ST_F_DREQ:
2646 metric = mkf_u64(FN_COUNTER, px->be_counters.denied_req);
2647 break;
2648 case ST_F_DRESP:
2649 metric = mkf_u64(FN_COUNTER, px->be_counters.denied_resp);
2650 break;
2651 case ST_F_ECON:
2652 metric = mkf_u64(FN_COUNTER, px->be_counters.failed_conns);
2653 break;
2654 case ST_F_ERESP:
2655 metric = mkf_u64(FN_COUNTER, px->be_counters.failed_resp);
2656 break;
2657 case ST_F_WRETR:
2658 metric = mkf_u64(FN_COUNTER, px->be_counters.retries);
2659 break;
2660 case ST_F_WREDIS:
2661 metric = mkf_u64(FN_COUNTER, px->be_counters.redispatches);
2662 break;
2663 case ST_F_WREW:
2664 metric = mkf_u64(FN_COUNTER, px->be_counters.failed_rewrites);
2665 break;
2666 case ST_F_EINT:
2667 metric = mkf_u64(FN_COUNTER, px->be_counters.internal_errors);
2668 break;
2669 case ST_F_CONNECT:
2670 metric = mkf_u64(FN_COUNTER, px->be_counters.connect);
2671 break;
2672 case ST_F_REUSE:
2673 metric = mkf_u64(FN_COUNTER, px->be_counters.reuse);
2674 break;
2675 case ST_F_STATUS:
2676 fld = chunk_newstr(out);
2677 chunk_appendf(out, "%s", (px->lbprm.tot_weight > 0 || !px->srv) ? "UP" : "DOWN");
2678 if (flags & (STAT_HIDE_MAINT|STAT_HIDE_DOWN))
2679 chunk_appendf(out, " (%d/%d)", nbup, nbsrv);
2680 metric = mkf_str(FO_STATUS, fld);
2681 break;
Cedric Paillet7d6644e2022-12-08 09:17:00 +00002682 case ST_F_AGG_SRV_CHECK_STATUS: // DEPRECATED
2683 case ST_F_AGG_SRV_STATUS:
William Dauchy42d7c402021-11-07 10:18:47 +01002684 metric = mkf_u32(FN_GAUGE, 0);
2685 break;
Cedric Paillete06e31e2022-12-08 09:17:01 +00002686 case ST_F_AGG_CHECK_STATUS:
2687 metric = mkf_u32(FN_GAUGE, 0);
2688 break;
William Dauchyda3b4662021-01-25 17:29:01 +01002689 case ST_F_WEIGHT:
2690 metric = mkf_u32(FN_AVG, (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv);
2691 break;
2692 case ST_F_UWEIGHT:
2693 metric = mkf_u32(FN_AVG, totuw);
2694 break;
2695 case ST_F_ACT:
2696 metric = mkf_u32(0, px->srv_act);
2697 break;
2698 case ST_F_BCK:
2699 metric = mkf_u32(0, px->srv_bck);
2700 break;
2701 case ST_F_CHKDOWN:
2702 metric = mkf_u64(FN_COUNTER, px->down_trans);
2703 break;
2704 case ST_F_LASTCHG:
2705 metric = mkf_u32(FN_AGE, now.tv_sec - px->last_change);
2706 break;
2707 case ST_F_DOWNTIME:
2708 if (px->srv)
2709 metric = mkf_u32(FN_COUNTER, be_downtime(px));
2710 break;
2711 case ST_F_PID:
Willy Tarreaue8422bf2021-06-15 09:08:18 +02002712 metric = mkf_u32(FO_KEY, 1);
William Dauchyda3b4662021-01-25 17:29:01 +01002713 break;
2714 case ST_F_IID:
2715 metric = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
2716 break;
2717 case ST_F_SID:
2718 metric = mkf_u32(FO_KEY|FS_SERVICE, 0);
2719 break;
2720 case ST_F_LBTOT:
2721 metric = mkf_u64(FN_COUNTER, px->be_counters.cum_lbconn);
2722 break;
2723 case ST_F_TYPE:
2724 metric = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_BE);
2725 break;
2726 case ST_F_RATE:
2727 metric = mkf_u32(0, read_freq_ctr(&px->be_sess_per_sec));
2728 break;
2729 case ST_F_RATE_MAX:
2730 metric = mkf_u32(0, px->be_counters.sps_max);
2731 break;
2732 case ST_F_COOKIE:
2733 if (flags & STAT_SHLGNDS && px->cookie_name)
2734 metric = mkf_str(FO_CONFIG|FN_NAME|FS_SERVICE, px->cookie_name);
2735 break;
2736 case ST_F_ALGO:
2737 if (flags & STAT_SHLGNDS)
2738 metric = mkf_str(FO_CONFIG|FS_SERVICE, backend_lb_algo_str(px->lbprm.algo & BE_LB_ALGO));
2739 break;
2740 case ST_F_REQ_TOT:
2741 if (px->mode == PR_MODE_HTTP)
2742 metric = mkf_u64(FN_COUNTER, px->be_counters.p.http.cum_req);
2743 break;
2744 case ST_F_HRSP_1XX:
2745 if (px->mode == PR_MODE_HTTP)
2746 metric = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[1]);
2747 break;
2748 case ST_F_HRSP_2XX:
2749 if (px->mode == PR_MODE_HTTP)
2750 metric = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[2]);
2751 break;
2752 case ST_F_HRSP_3XX:
2753 if (px->mode == PR_MODE_HTTP)
2754 metric = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[3]);
2755 break;
2756 case ST_F_HRSP_4XX:
2757 if (px->mode == PR_MODE_HTTP)
2758 metric = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[4]);
2759 break;
2760 case ST_F_HRSP_5XX:
2761 if (px->mode == PR_MODE_HTTP)
2762 metric = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[5]);
2763 break;
2764 case ST_F_HRSP_OTHER:
2765 if (px->mode == PR_MODE_HTTP)
2766 metric = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[0]);
2767 break;
2768 case ST_F_CACHE_LOOKUPS:
2769 if (px->mode == PR_MODE_HTTP)
2770 metric = mkf_u64(FN_COUNTER, px->be_counters.p.http.cache_lookups);
2771 break;
2772 case ST_F_CACHE_HITS:
2773 if (px->mode == PR_MODE_HTTP)
2774 metric = mkf_u64(FN_COUNTER, px->be_counters.p.http.cache_hits);
2775 break;
2776 case ST_F_CLI_ABRT:
2777 metric = mkf_u64(FN_COUNTER, px->be_counters.cli_aborts);
2778 break;
2779 case ST_F_SRV_ABRT:
2780 metric = mkf_u64(FN_COUNTER, px->be_counters.srv_aborts);
2781 break;
2782 case ST_F_COMP_IN:
2783 metric = mkf_u64(FN_COUNTER, px->be_counters.comp_in);
2784 break;
2785 case ST_F_COMP_OUT:
2786 metric = mkf_u64(FN_COUNTER, px->be_counters.comp_out);
2787 break;
2788 case ST_F_COMP_BYP:
2789 metric = mkf_u64(FN_COUNTER, px->be_counters.comp_byp);
2790 break;
2791 case ST_F_COMP_RSP:
2792 metric = mkf_u64(FN_COUNTER, px->be_counters.p.http.comp_rsp);
2793 break;
2794 case ST_F_LASTSESS:
2795 metric = mkf_s32(FN_AGE, be_lastsession(px));
2796 break;
2797 case ST_F_QTIME:
2798 metric = mkf_u32(FN_AVG, swrate_avg(px->be_counters.q_time, be_samples_window));
2799 break;
2800 case ST_F_CTIME:
2801 metric = mkf_u32(FN_AVG, swrate_avg(px->be_counters.c_time, be_samples_window));
2802 break;
2803 case ST_F_RTIME:
2804 metric = mkf_u32(FN_AVG, swrate_avg(px->be_counters.d_time, be_samples_window));
2805 break;
2806 case ST_F_TTIME:
2807 metric = mkf_u32(FN_AVG, swrate_avg(px->be_counters.t_time, be_samples_window));
2808 break;
2809 case ST_F_QT_MAX:
2810 metric = mkf_u32(FN_MAX, px->be_counters.qtime_max);
2811 break;
2812 case ST_F_CT_MAX:
2813 metric = mkf_u32(FN_MAX, px->be_counters.ctime_max);
2814 break;
2815 case ST_F_RT_MAX:
2816 metric = mkf_u32(FN_MAX, px->be_counters.dtime_max);
2817 break;
2818 case ST_F_TT_MAX:
2819 metric = mkf_u32(FN_MAX, px->be_counters.ttime_max);
2820 break;
2821 default:
2822 /* not used for backends. If a specific metric
2823 * is requested, return an error. Otherwise continue.
2824 */
2825 if (selected_field != NULL)
2826 return 0;
2827 continue;
2828 }
2829 stats[current_field] = metric;
2830 if (selected_field != NULL)
2831 break;
2832 }
William Lallemand74c24fb2016-11-21 17:18:36 +01002833 return 1;
2834}
2835
2836/* Dumps a line for backend <px> to the trash for and uses the state from stream
Willy Tarreau43241ff2019-10-09 11:27:51 +02002837 * interface <si>. The caller is responsible for clearing the trash if needed.
2838 * Returns non-zero if it emits anything, zero otherwise.
William Lallemand74c24fb2016-11-21 17:18:36 +01002839 */
Willy Tarreaucaff6312022-05-27 10:17:46 +02002840static int stats_dump_be_stats(struct stconn *sc, struct proxy *px)
William Lallemand74c24fb2016-11-21 17:18:36 +01002841{
Willy Tarreaucaff6312022-05-27 10:17:46 +02002842 struct appctx *appctx = __sc_appctx(sc);
Willy Tarreau91cefca2022-05-03 17:08:29 +02002843 struct show_stat_ctx *ctx = appctx->svcctx;
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02002844 struct field *stats = stat_l[STATS_DOMAIN_PROXY];
2845 struct stats_module *mod;
2846 size_t stats_count = ST_F_TOTAL_FIELDS;
William Lallemand74c24fb2016-11-21 17:18:36 +01002847
2848 if (!(px->cap & PR_CAP_BE))
2849 return 0;
2850
Willy Tarreau91cefca2022-05-03 17:08:29 +02002851 if ((ctx->flags & STAT_BOUND) && !(ctx->type & (1 << STATS_TYPE_BE)))
William Lallemand74c24fb2016-11-21 17:18:36 +01002852 return 0;
2853
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02002854 memset(stats, 0, sizeof(struct field) * stat_count[STATS_DOMAIN_PROXY]);
2855
Willy Tarreau91cefca2022-05-03 17:08:29 +02002856 if (!stats_fill_be_stats(px, ctx->flags, stats, ST_F_TOTAL_FIELDS, NULL))
William Lallemand74c24fb2016-11-21 17:18:36 +01002857 return 0;
2858
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02002859 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
2860 struct extra_counters *counters;
2861
2862 if (stats_get_domain(mod->domain_flags) != STATS_DOMAIN_PROXY)
2863 continue;
2864
2865 if (!(stats_px_get_cap(mod->domain_flags) & STATS_PX_CAP_BE)) {
2866 stats_count += mod->stats_count;
2867 continue;
2868 }
2869
2870 counters = EXTRA_COUNTERS_GET(px->extra_counters_be, mod);
2871 mod->fill_stats(counters, stats + stats_count);
2872 stats_count += mod->stats_count;
2873 }
2874
2875 return stats_dump_one_line(stats, stats_count, appctx);
William Lallemand74c24fb2016-11-21 17:18:36 +01002876}
2877
2878/* Dumps the HTML table header for proxy <px> to the trash for and uses the state from
Willy Tarreaucaff6312022-05-27 10:17:46 +02002879 * stream connector <sc> and per-uri parameters <uri>. The caller is responsible
William Lallemand74c24fb2016-11-21 17:18:36 +01002880 * for clearing the trash if needed.
2881 */
Willy Tarreaucaff6312022-05-27 10:17:46 +02002882static void stats_dump_html_px_hdr(struct stconn *sc, struct proxy *px)
William Lallemand74c24fb2016-11-21 17:18:36 +01002883{
Willy Tarreaucaff6312022-05-27 10:17:46 +02002884 struct appctx *appctx = __sc_appctx(sc);
Willy Tarreau91cefca2022-05-03 17:08:29 +02002885 struct show_stat_ctx *ctx = appctx->svcctx;
William Lallemand74c24fb2016-11-21 17:18:36 +01002886 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02002887 struct stats_module *mod;
2888 int stats_module_len = 0;
William Lallemand74c24fb2016-11-21 17:18:36 +01002889
Willy Tarreau91cefca2022-05-03 17:08:29 +02002890 if (px->cap & PR_CAP_BE && px->srv && (ctx->flags & STAT_ADMIN)) {
William Lallemand74c24fb2016-11-21 17:18:36 +01002891 /* A form to enable/disable this proxy servers */
2892
Willy Tarreau91cefca2022-05-03 17:08:29 +02002893 /* scope_txt = search pattern + search query, ctx->scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
William Lallemand74c24fb2016-11-21 17:18:36 +01002894 scope_txt[0] = 0;
Willy Tarreau91cefca2022-05-03 17:08:29 +02002895 if (ctx->scope_len) {
Willy Tarreaucaff6312022-05-27 10:17:46 +02002896 const char *scope_ptr = stats_scope_ptr(appctx, sc);
Christopher Fauleted7a0662019-01-14 11:07:34 +01002897
William Lallemand74c24fb2016-11-21 17:18:36 +01002898 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Willy Tarreau91cefca2022-05-03 17:08:29 +02002899 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), scope_ptr, ctx->scope_len);
2900 scope_txt[strlen(STAT_SCOPE_PATTERN) + ctx->scope_len] = 0;
William Lallemand74c24fb2016-11-21 17:18:36 +01002901 }
2902
2903 chunk_appendf(&trash,
2904 "<form method=\"post\">");
2905 }
2906
2907 /* print a new table */
2908 chunk_appendf(&trash,
2909 "<table class=\"tbl\" width=\"100%%\">\n"
2910 "<tr class=\"titre\">"
2911 "<th class=\"pxname\" width=\"10%%\">");
2912
2913 chunk_appendf(&trash,
2914 "<a name=\"%s\"></a>%s"
2915 "<a class=px href=\"#%s\">%s</a>",
2916 px->id,
Willy Tarreau91cefca2022-05-03 17:08:29 +02002917 (ctx->flags & STAT_SHLGNDS) ? "<u>":"",
William Lallemand74c24fb2016-11-21 17:18:36 +01002918 px->id, px->id);
2919
Willy Tarreau91cefca2022-05-03 17:08:29 +02002920 if (ctx->flags & STAT_SHLGNDS) {
William Lallemand74c24fb2016-11-21 17:18:36 +01002921 /* cap, mode, id */
2922 chunk_appendf(&trash, "<div class=tips>cap: %s, mode: %s, id: %d",
2923 proxy_cap_str(px->cap), proxy_mode_str(px->mode),
2924 px->uuid);
2925 chunk_appendf(&trash, "</div>");
2926 }
2927
2928 chunk_appendf(&trash,
2929 "%s</th>"
2930 "<th class=\"%s\" width=\"90%%\">%s</th>"
2931 "</tr>\n"
2932 "</table>\n"
2933 "<table class=\"tbl\" width=\"100%%\">\n"
2934 "<tr class=\"titre\">",
Willy Tarreau91cefca2022-05-03 17:08:29 +02002935 (ctx->flags & STAT_SHLGNDS) ? "</u>":"",
William Lallemand74c24fb2016-11-21 17:18:36 +01002936 px->desc ? "desc" : "empty", px->desc ? px->desc : "");
2937
Willy Tarreau91cefca2022-05-03 17:08:29 +02002938 if (ctx->flags & STAT_ADMIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +01002939 /* Column heading for Enable or Disable server */
Christopher Fauletbc271ec2019-12-02 11:29:04 +01002940 if ((px->cap & PR_CAP_BE) && px->srv)
2941 chunk_appendf(&trash,
2942 "<th rowspan=2 width=1><input type=\"checkbox\" "
2943 "onclick=\"for(c in document.getElementsByClassName('%s-checkbox')) "
2944 "document.getElementsByClassName('%s-checkbox').item(c).checked = this.checked\"></th>",
2945 px->id,
2946 px->id);
2947 else
2948 chunk_appendf(&trash, "<th rowspan=2></th>");
William Lallemand74c24fb2016-11-21 17:18:36 +01002949 }
2950
2951 chunk_appendf(&trash,
2952 "<th rowspan=2></th>"
2953 "<th colspan=3>Queue</th>"
2954 "<th colspan=3>Session rate</th><th colspan=6>Sessions</th>"
2955 "<th colspan=2>Bytes</th><th colspan=2>Denied</th>"
2956 "<th colspan=3>Errors</th><th colspan=2>Warnings</th>"
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02002957 "<th colspan=9>Server</th>");
2958
Willy Tarreau91cefca2022-05-03 17:08:29 +02002959 if (ctx->flags & STAT_SHMODULES) {
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02002960 // calculate the count of module for colspan attribute
2961 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
2962 ++stats_module_len;
2963 }
2964 chunk_appendf(&trash, "<th colspan=%d>Extra modules</th>",
2965 stats_module_len);
2966 }
2967
2968 chunk_appendf(&trash,
William Lallemand74c24fb2016-11-21 17:18:36 +01002969 "</tr>\n"
2970 "<tr class=\"titre\">"
2971 "<th>Cur</th><th>Max</th><th>Limit</th>"
2972 "<th>Cur</th><th>Max</th><th>Limit</th><th>Cur</th><th>Max</th>"
2973 "<th>Limit</th><th>Total</th><th>LbTot</th><th>Last</th><th>In</th><th>Out</th>"
2974 "<th>Req</th><th>Resp</th><th>Req</th><th>Conn</th>"
2975 "<th>Resp</th><th>Retr</th><th>Redis</th>"
2976 "<th>Status</th><th>LastChk</th><th>Wght</th><th>Act</th>"
2977 "<th>Bck</th><th>Chk</th><th>Dwn</th><th>Dwntme</th>"
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02002978 "<th>Thrtle</th>\n");
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02002979
Willy Tarreau91cefca2022-05-03 17:08:29 +02002980 if (ctx->flags & STAT_SHMODULES) {
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02002981 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
2982 chunk_appendf(&trash, "<th>%s</th>", mod->name);
2983 }
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02002984 }
2985
2986 chunk_appendf(&trash, "</tr>");
William Lallemand74c24fb2016-11-21 17:18:36 +01002987}
2988
2989/* Dumps the HTML table trailer for proxy <px> to the trash for and uses the state from
Willy Tarreaucaff6312022-05-27 10:17:46 +02002990 * stream connector <sc>. The caller is responsible for clearing the trash if needed.
William Lallemand74c24fb2016-11-21 17:18:36 +01002991 */
Willy Tarreaucaff6312022-05-27 10:17:46 +02002992static void stats_dump_html_px_end(struct stconn *sc, struct proxy *px)
William Lallemand74c24fb2016-11-21 17:18:36 +01002993{
Willy Tarreaucaff6312022-05-27 10:17:46 +02002994 struct appctx *appctx = __sc_appctx(sc);
Willy Tarreau91cefca2022-05-03 17:08:29 +02002995 struct show_stat_ctx *ctx = appctx->svcctx;
Christopher Faulet693b23b2022-02-28 09:09:05 +01002996
William Lallemand74c24fb2016-11-21 17:18:36 +01002997 chunk_appendf(&trash, "</table>");
2998
Willy Tarreau91cefca2022-05-03 17:08:29 +02002999 if ((px->cap & PR_CAP_BE) && px->srv && (ctx->flags & STAT_ADMIN)) {
William Lallemand74c24fb2016-11-21 17:18:36 +01003000 /* close the form used to enable/disable this proxy servers */
3001 chunk_appendf(&trash,
3002 "Choose the action to perform on the checked servers : "
3003 "<select name=action>"
3004 "<option value=\"\"></option>"
3005 "<option value=\"ready\">Set state to READY</option>"
3006 "<option value=\"drain\">Set state to DRAIN</option>"
3007 "<option value=\"maint\">Set state to MAINT</option>"
3008 "<option value=\"dhlth\">Health: disable checks</option>"
3009 "<option value=\"ehlth\">Health: enable checks</option>"
3010 "<option value=\"hrunn\">Health: force UP</option>"
3011 "<option value=\"hnolb\">Health: force NOLB</option>"
3012 "<option value=\"hdown\">Health: force DOWN</option>"
3013 "<option value=\"dagent\">Agent: disable checks</option>"
3014 "<option value=\"eagent\">Agent: enable checks</option>"
3015 "<option value=\"arunn\">Agent: force UP</option>"
3016 "<option value=\"adown\">Agent: force DOWN</option>"
3017 "<option value=\"shutdown\">Kill Sessions</option>"
3018 "</select>"
3019 "<input type=\"hidden\" name=\"b\" value=\"#%d\">"
3020 "&nbsp;<input type=\"submit\" value=\"Apply\">"
3021 "</form>",
3022 px->uuid);
3023 }
3024
3025 chunk_appendf(&trash, "<p>\n");
3026}
3027
3028/*
Willy Tarreau4596fe22022-05-17 19:07:51 +02003029 * Dumps statistics for a proxy. The output is sent to the stream connector's
William Lallemand74c24fb2016-11-21 17:18:36 +01003030 * input buffer. Returns 0 if it had to stop dumping data because of lack of
3031 * buffer space, or non-zero if everything completed. This function is used
3032 * both by the CLI and the HTTP entry points, and is able to dump the output
3033 * in HTML or CSV formats. If the later, <uri> must be NULL.
3034 */
Willy Tarreaucaff6312022-05-27 10:17:46 +02003035int stats_dump_proxy_to_buffer(struct stconn *sc, struct htx *htx,
Christopher Fauletef779222018-10-31 08:47:01 +01003036 struct proxy *px, struct uri_auth *uri)
William Lallemand74c24fb2016-11-21 17:18:36 +01003037{
Willy Tarreaucaff6312022-05-27 10:17:46 +02003038 struct appctx *appctx = __sc_appctx(sc);
Willy Tarreau91cefca2022-05-03 17:08:29 +02003039 struct show_stat_ctx *ctx = appctx->svcctx;
Willy Tarreaucaff6312022-05-27 10:17:46 +02003040 struct stream *s = __sc_strm(sc);
3041 struct channel *rep = sc_ic(sc);
William Lallemand74c24fb2016-11-21 17:18:36 +01003042 struct server *sv, *svs; /* server and server-state, server-state=server or server->track */
3043 struct listener *l;
William Lallemand74c24fb2016-11-21 17:18:36 +01003044
3045 chunk_reset(&trash);
3046
Willy Tarreau91cefca2022-05-03 17:08:29 +02003047 switch (ctx->px_st) {
William Lallemand74c24fb2016-11-21 17:18:36 +01003048 case STAT_PX_ST_INIT:
3049 /* we are on a new proxy */
3050 if (uri && uri->scope) {
3051 /* we have a limited scope, we have to check the proxy name */
3052 struct stat_scope *scope;
3053 int len;
3054
3055 len = strlen(px->id);
3056 scope = uri->scope;
3057
3058 while (scope) {
3059 /* match exact proxy name */
3060 if (scope->px_len == len && !memcmp(px->id, scope->px_id, len))
3061 break;
3062
3063 /* match '.' which means 'self' proxy */
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003064 if (strcmp(scope->px_id, ".") == 0 && px == s->be)
William Lallemand74c24fb2016-11-21 17:18:36 +01003065 break;
3066 scope = scope->next;
3067 }
3068
3069 /* proxy name not found : don't dump anything */
3070 if (scope == NULL)
3071 return 1;
3072 }
3073
3074 /* if the user has requested a limited output and the proxy
3075 * name does not match, skip it.
3076 */
Willy Tarreau91cefca2022-05-03 17:08:29 +02003077 if (ctx->scope_len) {
Willy Tarreaucaff6312022-05-27 10:17:46 +02003078 const char *scope_ptr = stats_scope_ptr(appctx, sc);
Christopher Fauleted7a0662019-01-14 11:07:34 +01003079
Willy Tarreau91cefca2022-05-03 17:08:29 +02003080 if (strnistr(px->id, strlen(px->id), scope_ptr, ctx->scope_len) == NULL)
Christopher Fauleted7a0662019-01-14 11:07:34 +01003081 return 1;
3082 }
William Lallemand74c24fb2016-11-21 17:18:36 +01003083
Willy Tarreau91cefca2022-05-03 17:08:29 +02003084 if ((ctx->flags & STAT_BOUND) &&
3085 (ctx->iid != -1) &&
3086 (px->uuid != ctx->iid))
William Lallemand74c24fb2016-11-21 17:18:36 +01003087 return 1;
3088
Willy Tarreau91cefca2022-05-03 17:08:29 +02003089 ctx->px_st = STAT_PX_ST_TH;
Willy Tarreau9eb93c02022-11-14 07:12:05 +01003090 __fallthrough;
William Lallemand74c24fb2016-11-21 17:18:36 +01003091
3092 case STAT_PX_ST_TH:
Willy Tarreau91cefca2022-05-03 17:08:29 +02003093 if (ctx->flags & STAT_FMT_HTML) {
Willy Tarreaucaff6312022-05-27 10:17:46 +02003094 stats_dump_html_px_hdr(sc, px);
Christopher Fauletef779222018-10-31 08:47:01 +01003095 if (!stats_putchk(rep, htx, &trash))
3096 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01003097 }
3098
Willy Tarreau91cefca2022-05-03 17:08:29 +02003099 ctx->px_st = STAT_PX_ST_FE;
Willy Tarreau9eb93c02022-11-14 07:12:05 +01003100 __fallthrough;
William Lallemand74c24fb2016-11-21 17:18:36 +01003101
3102 case STAT_PX_ST_FE:
3103 /* print the frontend */
Willy Tarreaucaff6312022-05-27 10:17:46 +02003104 if (stats_dump_fe_stats(sc, px)) {
Christopher Fauletef779222018-10-31 08:47:01 +01003105 if (!stats_putchk(rep, htx, &trash))
3106 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01003107 }
3108
Willy Tarreau91cefca2022-05-03 17:08:29 +02003109 ctx->obj2 = px->conf.listeners.n;
3110 ctx->px_st = STAT_PX_ST_LI;
Willy Tarreau9eb93c02022-11-14 07:12:05 +01003111 __fallthrough;
William Lallemand74c24fb2016-11-21 17:18:36 +01003112
3113 case STAT_PX_ST_LI:
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +02003114 /* obj2 points to listeners list as initialized above */
Willy Tarreau91cefca2022-05-03 17:08:29 +02003115 for (; ctx->obj2 != &px->conf.listeners; ctx->obj2 = l->by_fe.n) {
Christopher Fauletef779222018-10-31 08:47:01 +01003116 if (htx) {
3117 if (htx_almost_full(htx))
3118 goto full;
3119 }
3120 else {
3121 if (buffer_almost_full(&rep->buf))
3122 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01003123 }
3124
Willy Tarreau91cefca2022-05-03 17:08:29 +02003125 l = LIST_ELEM(ctx->obj2, struct listener *, by_fe);
William Lallemand74c24fb2016-11-21 17:18:36 +01003126 if (!l->counters)
3127 continue;
3128
Willy Tarreau91cefca2022-05-03 17:08:29 +02003129 if (ctx->flags & STAT_BOUND) {
3130 if (!(ctx->type & (1 << STATS_TYPE_SO)))
William Lallemand74c24fb2016-11-21 17:18:36 +01003131 break;
3132
Willy Tarreau91cefca2022-05-03 17:08:29 +02003133 if (ctx->sid != -1 && l->luid != ctx->sid)
William Lallemand74c24fb2016-11-21 17:18:36 +01003134 continue;
3135 }
3136
3137 /* print the frontend */
Willy Tarreaucaff6312022-05-27 10:17:46 +02003138 if (stats_dump_li_stats(sc, px, l)) {
Christopher Fauletef779222018-10-31 08:47:01 +01003139 if (!stats_putchk(rep, htx, &trash))
3140 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01003141 }
3142 }
3143
Willy Tarreau91cefca2022-05-03 17:08:29 +02003144 ctx->obj2 = px->srv; /* may be NULL */
3145 ctx->px_st = STAT_PX_ST_SV;
Willy Tarreau9eb93c02022-11-14 07:12:05 +01003146 __fallthrough;
William Lallemand74c24fb2016-11-21 17:18:36 +01003147
3148 case STAT_PX_ST_SV:
Amaury Denoyelle0a8d05d2021-08-25 14:39:53 +02003149 /* obj2 points to servers list as initialized above.
3150 *
Amaury Denoyellebc2ebfa2021-08-25 15:34:53 +02003151 * A server may be removed during the stats dumping.
Amaury Denoyelle0a8d05d2021-08-25 14:39:53 +02003152 * Temporarily increment its refcount to prevent its
3153 * anticipated cleaning. Call free_server to release it.
3154 */
Willy Tarreau91cefca2022-05-03 17:08:29 +02003155 for (; ctx->obj2 != NULL;
3156 ctx->obj2 = srv_drop(sv)) {
Amaury Denoyelle0a8d05d2021-08-25 14:39:53 +02003157
Willy Tarreau91cefca2022-05-03 17:08:29 +02003158 sv = ctx->obj2;
Amaury Denoyellebc2ebfa2021-08-25 15:34:53 +02003159 srv_take(sv);
Amaury Denoyelle0a8d05d2021-08-25 14:39:53 +02003160
Christopher Fauletef779222018-10-31 08:47:01 +01003161 if (htx) {
3162 if (htx_almost_full(htx))
3163 goto full;
3164 }
3165 else {
3166 if (buffer_almost_full(&rep->buf))
3167 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01003168 }
3169
Willy Tarreau91cefca2022-05-03 17:08:29 +02003170 if (ctx->flags & STAT_BOUND) {
3171 if (!(ctx->type & (1 << STATS_TYPE_SV))) {
Amaury Denoyellebc2ebfa2021-08-25 15:34:53 +02003172 srv_drop(sv);
William Lallemand74c24fb2016-11-21 17:18:36 +01003173 break;
Amaury Denoyelle0a8d05d2021-08-25 14:39:53 +02003174 }
William Lallemand74c24fb2016-11-21 17:18:36 +01003175
Willy Tarreau91cefca2022-05-03 17:08:29 +02003176 if (ctx->sid != -1 && sv->puid != ctx->sid)
William Lallemand74c24fb2016-11-21 17:18:36 +01003177 continue;
3178 }
3179
Willy Tarreau3e320362020-10-23 17:28:57 +02003180 /* do not report disabled servers */
Willy Tarreau91cefca2022-05-03 17:08:29 +02003181 if (ctx->flags & STAT_HIDE_MAINT &&
Willy Tarreau3e320362020-10-23 17:28:57 +02003182 sv->cur_admin & SRV_ADMF_MAINT) {
3183 continue;
3184 }
3185
William Lallemand74c24fb2016-11-21 17:18:36 +01003186 svs = sv;
3187 while (svs->track)
3188 svs = svs->track;
3189
3190 /* do not report servers which are DOWN and not changing state */
Willy Tarreau91cefca2022-05-03 17:08:29 +02003191 if ((ctx->flags & STAT_HIDE_DOWN) &&
Emeric Brun52a91d32017-08-31 14:41:55 +02003192 ((sv->cur_admin & SRV_ADMF_MAINT) || /* server is in maintenance */
3193 (sv->cur_state == SRV_ST_STOPPED && /* server is down */
William Lallemand74c24fb2016-11-21 17:18:36 +01003194 (!((svs->agent.state | svs->check.state) & CHK_ST_ENABLED) ||
3195 ((svs->agent.state & CHK_ST_ENABLED) && !svs->agent.health) ||
3196 ((svs->check.state & CHK_ST_ENABLED) && !svs->check.health))))) {
3197 continue;
3198 }
3199
Willy Tarreaucaff6312022-05-27 10:17:46 +02003200 if (stats_dump_sv_stats(sc, px, sv)) {
Christopher Fauletef779222018-10-31 08:47:01 +01003201 if (!stats_putchk(rep, htx, &trash))
3202 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01003203 }
3204 } /* for sv */
3205
Willy Tarreau91cefca2022-05-03 17:08:29 +02003206 ctx->px_st = STAT_PX_ST_BE;
Willy Tarreau9eb93c02022-11-14 07:12:05 +01003207 __fallthrough;
William Lallemand74c24fb2016-11-21 17:18:36 +01003208
3209 case STAT_PX_ST_BE:
3210 /* print the backend */
Willy Tarreaucaff6312022-05-27 10:17:46 +02003211 if (stats_dump_be_stats(sc, px)) {
Christopher Fauletef779222018-10-31 08:47:01 +01003212 if (!stats_putchk(rep, htx, &trash))
3213 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01003214 }
3215
Willy Tarreau91cefca2022-05-03 17:08:29 +02003216 ctx->px_st = STAT_PX_ST_END;
Willy Tarreau9eb93c02022-11-14 07:12:05 +01003217 __fallthrough;
William Lallemand74c24fb2016-11-21 17:18:36 +01003218
3219 case STAT_PX_ST_END:
Willy Tarreau91cefca2022-05-03 17:08:29 +02003220 if (ctx->flags & STAT_FMT_HTML) {
Willy Tarreaucaff6312022-05-27 10:17:46 +02003221 stats_dump_html_px_end(sc, px);
Christopher Fauletef779222018-10-31 08:47:01 +01003222 if (!stats_putchk(rep, htx, &trash))
3223 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01003224 }
3225
Willy Tarreau91cefca2022-05-03 17:08:29 +02003226 ctx->px_st = STAT_PX_ST_FIN;
Willy Tarreau9eb93c02022-11-14 07:12:05 +01003227 __fallthrough;
William Lallemand74c24fb2016-11-21 17:18:36 +01003228
3229 case STAT_PX_ST_FIN:
3230 return 1;
3231
3232 default:
3233 /* unknown state, we should put an abort() here ! */
3234 return 1;
3235 }
Christopher Fauletef779222018-10-31 08:47:01 +01003236
3237 full:
Willy Tarreaucaff6312022-05-27 10:17:46 +02003238 sc_need_room(sc);
Christopher Fauletef779222018-10-31 08:47:01 +01003239 return 0;
William Lallemand74c24fb2016-11-21 17:18:36 +01003240}
3241
3242/* Dumps the HTTP stats head block to the trash for and uses the per-uri
3243 * parameters <uri>. The caller is responsible for clearing the trash if needed.
3244 */
Willy Tarreau676c29e2019-10-09 10:50:01 +02003245static void stats_dump_html_head(struct appctx *appctx, struct uri_auth *uri)
William Lallemand74c24fb2016-11-21 17:18:36 +01003246{
Willy Tarreau91cefca2022-05-03 17:08:29 +02003247 struct show_stat_ctx *ctx = appctx->svcctx;
3248
William Lallemand74c24fb2016-11-21 17:18:36 +01003249 /* WARNING! This must fit in the first buffer !!! */
3250 chunk_appendf(&trash,
3251 "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"\n"
3252 "\"http://www.w3.org/TR/html4/loose.dtd\">\n"
3253 "<html><head><title>Statistics Report for " PRODUCT_NAME "%s%s</title>\n"
Thierry Fournierb6b1cde2021-09-24 00:03:07 +02003254 "<link rel=\"icon\" href=\"data:,\">\n"
William Lallemand74c24fb2016-11-21 17:18:36 +01003255 "<meta http-equiv=\"content-type\" content=\"text/html; charset=iso-8859-1\">\n"
3256 "<style type=\"text/css\"><!--\n"
3257 "body {"
3258 " font-family: arial, helvetica, sans-serif;"
3259 " font-size: 12px;"
3260 " font-weight: normal;"
3261 " color: black;"
3262 " background: white;"
3263 "}\n"
3264 "th,td {"
3265 " font-size: 10px;"
3266 "}\n"
3267 "h1 {"
3268 " font-size: x-large;"
3269 " margin-bottom: 0.5em;"
3270 "}\n"
3271 "h2 {"
3272 " font-family: helvetica, arial;"
3273 " font-size: x-large;"
3274 " font-weight: bold;"
3275 " font-style: italic;"
3276 " color: #6020a0;"
3277 " margin-top: 0em;"
3278 " margin-bottom: 0em;"
3279 "}\n"
3280 "h3 {"
3281 " font-family: helvetica, arial;"
3282 " font-size: 16px;"
3283 " font-weight: bold;"
3284 " color: #b00040;"
3285 " background: #e8e8d0;"
3286 " margin-top: 0em;"
3287 " margin-bottom: 0em;"
3288 "}\n"
3289 "li {"
3290 " margin-top: 0.25em;"
3291 " margin-right: 2em;"
3292 "}\n"
3293 ".hr {margin-top: 0.25em;"
3294 " border-color: black;"
3295 " border-bottom-style: solid;"
3296 "}\n"
3297 ".titre {background: #20D0D0;color: #000000; font-weight: bold; text-align: center;}\n"
3298 ".total {background: #20D0D0;color: #ffff80;}\n"
3299 ".frontend {background: #e8e8d0;}\n"
3300 ".socket {background: #d0d0d0;}\n"
3301 ".backend {background: #e8e8d0;}\n"
3302 ".active_down {background: #ff9090;}\n"
3303 ".active_going_up {background: #ffd020;}\n"
3304 ".active_going_down {background: #ffffa0;}\n"
3305 ".active_up {background: #c0ffc0;}\n"
3306 ".active_nolb {background: #20a0ff;}\n"
3307 ".active_draining {background: #20a0FF;}\n"
3308 ".active_no_check {background: #e0e0e0;}\n"
3309 ".backup_down {background: #ff9090;}\n"
3310 ".backup_going_up {background: #ff80ff;}\n"
3311 ".backup_going_down {background: #c060ff;}\n"
3312 ".backup_up {background: #b0d0ff;}\n"
3313 ".backup_nolb {background: #90b0e0;}\n"
3314 ".backup_draining {background: #cc9900;}\n"
3315 ".backup_no_check {background: #e0e0e0;}\n"
3316 ".maintain {background: #c07820;}\n"
3317 ".rls {letter-spacing: 0.2em; margin-right: 1px;}\n" /* right letter spacing (used for grouping digits) */
3318 "\n"
3319 "a.px:link {color: #ffff40; text-decoration: none;}"
3320 "a.px:visited {color: #ffff40; text-decoration: none;}"
3321 "a.px:hover {color: #ffffff; text-decoration: none;}"
3322 "a.lfsb:link {color: #000000; text-decoration: none;}"
3323 "a.lfsb:visited {color: #000000; text-decoration: none;}"
3324 "a.lfsb:hover {color: #505050; text-decoration: none;}"
3325 "\n"
3326 "table.tbl { border-collapse: collapse; border-style: none;}\n"
3327 "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"
3328 "table.tbl td.ac { text-align: center;}\n"
3329 "table.tbl th { border-width: 1px; border-style: solid solid solid solid; border-color: gray;}\n"
3330 "table.tbl th.pxname { background: #b00040; color: #ffff40; font-weight: bold; border-style: solid solid none solid; padding: 2px 3px; white-space: nowrap;}\n"
3331 "table.tbl th.empty { border-style: none; empty-cells: hide; background: white;}\n"
3332 "table.tbl th.desc { background: white; border-style: solid solid none solid; text-align: left; padding: 2px 3px;}\n"
3333 "\n"
3334 "table.lgd { border-collapse: collapse; border-width: 1px; border-style: none none none solid; border-color: black;}\n"
3335 "table.lgd td { border-width: 1px; border-style: solid solid solid solid; border-color: gray; padding: 2px;}\n"
3336 "table.lgd td.noborder { border-style: none; padding: 2px; white-space: nowrap;}\n"
3337 "table.det { border-collapse: collapse; border-style: none; }\n"
3338 "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"
3339 "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"
3340 "u {text-decoration:none; border-bottom: 1px dotted black;}\n"
Thierry Fournierb6b1cde2021-09-24 00:03:07 +02003341 "div.tips {\n"
3342 " display:block;\n"
3343 " visibility:hidden;\n"
3344 " z-index:2147483647;\n"
3345 " position:absolute;\n"
3346 " padding:2px 4px 3px;\n"
3347 " background:#f0f060; color:#000000;\n"
3348 " border:1px solid #7040c0;\n"
3349 " white-space:nowrap;\n"
3350 " font-style:normal;font-size:11px;font-weight:normal;\n"
3351 " -moz-border-radius:3px;-webkit-border-radius:3px;border-radius:3px;\n"
3352 " -moz-box-shadow:gray 2px 2px 3px;-webkit-box-shadow:gray 2px 2px 3px;box-shadow:gray 2px 2px 3px;\n"
3353 "}\n"
3354 "u:hover div.tips {visibility:visible;}\n"
Thierry Fourniere9ed63e2021-09-23 23:59:49 +02003355 "@media (prefers-color-scheme: dark) {\n"
3356 " body { font-family: arial, helvetica, sans-serif; font-size: 12px; font-weight: normal; color: #e8e6e3; background: #131516;}\n"
3357 " h1 { color: #a265e0!important; }\n"
3358 " h2 { color: #a265e0; }\n"
3359 " h3 { color: #ff5190; background-color: #3e3e1f; }\n"
3360 " a { color: #3391ff; }\n"
3361 " input { background-color: #2f3437; }\n"
3362 " .hr { border-color: #8c8273; }\n"
3363 " .titre { background-color: #1aa6a6; color: #e8e6e3; }\n"
3364 " .frontend {background: #2f3437;}\n"
Marno Krahmera690b732022-03-08 13:45:09 +01003365 " .socket {background: #2a2d2f;}\n"
Thierry Fourniere9ed63e2021-09-23 23:59:49 +02003366 " .backend {background: #2f3437;}\n"
3367 " .active_down {background: #760000;}\n"
3368 " .active_going_up {background: #b99200;}\n"
3369 " .active_going_down {background: #6c6c00;}\n"
3370 " .active_up {background: #165900;}\n"
3371 " .active_nolb {background: #006ab9;}\n"
3372 " .active_draining {background: #006ab9;}\n"
3373 " .active_no_check {background: #2a2d2f;}\n"
3374 " .backup_down {background: #760000;}\n"
3375 " .backup_going_up {background: #7f007f;}\n"
3376 " .backup_going_down {background: #580092;}\n"
3377 " .backup_up {background: #2e3234;}\n"
3378 " .backup_nolb {background: #1e3c6a;}\n"
3379 " .backup_draining {background: #a37a00;}\n"
3380 " .backup_no_check {background: #2a2d2f;}\n"
3381 " .maintain {background: #9a601a;}\n"
3382 " a.px:link {color: #d8d83b; text-decoration: none;}\n"
3383 " a.px:visited {color: #d8d83b; text-decoration: none;}\n"
3384 " a.px:hover {color: #ffffff; text-decoration: none;}\n"
3385 " a.lfsb:link {color: #e8e6e3; text-decoration: none;}\n"
3386 " a.lfsb:visited {color: #e8e6e3; text-decoration: none;}\n"
3387 " a.lfsb:hover {color: #b5afa6; text-decoration: none;}\n"
3388 " table.tbl th.empty { background-color: #181a1b; }\n"
Willy Tarreau00147f72022-04-11 07:59:27 +02003389 " table.tbl th.desc { background: #181a1b; }\n"
Thierry Fourniere9ed63e2021-09-23 23:59:49 +02003390 " table.tbl th.pxname { background-color: #8d0033; color: #ffff46; }\n"
3391 " table.tbl th { border-color: #808080; }\n"
3392 " table.tbl td { border-color: #808080; }\n"
3393 " u {text-decoration:none; border-bottom: 1px dotted #e8e6e3;}\n"
3394 " div.tips {\n"
3395 " background:#8e8e0d;\n"
3396 " color:#e8e6e3;\n"
3397 " border-color: #4e2c86;\n"
3398 " -moz-box-shadow: #60686c 2px 2px 3px;\n"
3399 " -webkit-box-shadow: #60686c 2px 2px 3px;\n"
3400 " box-shadow: #60686c 2px 2px 3px;\n"
3401 " }\n"
3402 "}\n"
William Lallemand74c24fb2016-11-21 17:18:36 +01003403 "-->\n"
3404 "</style></head>\n",
Willy Tarreau91cefca2022-05-03 17:08:29 +02003405 (ctx->flags & STAT_SHNODE) ? " on " : "",
3406 (ctx->flags & STAT_SHNODE) ? (uri && uri->node ? uri->node : global.node) : ""
William Lallemand74c24fb2016-11-21 17:18:36 +01003407 );
3408}
3409
3410/* Dumps the HTML stats information block to the trash for and uses the state from
Willy Tarreaucaff6312022-05-27 10:17:46 +02003411 * stream connector <sc> and per-uri parameters <uri>. The caller is responsible
William Lallemand74c24fb2016-11-21 17:18:36 +01003412 * for clearing the trash if needed.
3413 */
Willy Tarreaucaff6312022-05-27 10:17:46 +02003414static void stats_dump_html_info(struct stconn *sc, struct uri_auth *uri)
William Lallemand74c24fb2016-11-21 17:18:36 +01003415{
Willy Tarreaucaff6312022-05-27 10:17:46 +02003416 struct appctx *appctx = __sc_appctx(sc);
Willy Tarreau91cefca2022-05-03 17:08:29 +02003417 struct show_stat_ctx *ctx = appctx->svcctx;
William Lallemand74c24fb2016-11-21 17:18:36 +01003418 unsigned int up = (now.tv_sec - start_date.tv_sec);
3419 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
Willy Tarreaucaff6312022-05-27 10:17:46 +02003420 const char *scope_ptr = stats_scope_ptr(appctx, sc);
Willy Tarreau1713c032019-05-23 12:23:55 +02003421 unsigned long long bps = (unsigned long long)read_freq_ctr(&global.out_32bps) * 32;
3422
3423 /* Turn the bytes per second to bits per second and take care of the
3424 * usual ethernet overhead in order to help figure how far we are from
3425 * interface saturation since it's the only case which usually matters.
3426 * For this we count the total size of an Ethernet frame on the wire
3427 * including preamble and IFG (1538) for the largest TCP segment it
3428 * transports (1448 with TCP timestamps). This is not valid for smaller
3429 * packets (under-estimated), but it gives a reasonably accurate
3430 * estimation of how far we are from uplink saturation.
3431 */
3432 bps = bps * 8 * 1538 / 1448;
William Lallemand74c24fb2016-11-21 17:18:36 +01003433
3434 /* WARNING! this has to fit the first packet too.
3435 * We are around 3.5 kB, add adding entries will
3436 * become tricky if we want to support 4kB buffers !
3437 */
3438 chunk_appendf(&trash,
3439 "<body><h1><a href=\"" PRODUCT_URL "\" style=\"text-decoration: none;\">"
3440 PRODUCT_NAME "%s</a></h1>\n"
3441 "<h2>Statistics Report for pid %d%s%s%s%s</h2>\n"
3442 "<hr width=\"100%%\" class=\"hr\">\n"
3443 "<h3>&gt; General process information</h3>\n"
3444 "<table border=0><tr><td align=\"left\" nowrap width=\"1%%\">\n"
Yves Lafon95317282018-02-26 11:10:37 +01003445 "<p><b>pid = </b> %d (process #%d, nbproc = %d, nbthread = %d)<br>\n"
William Lallemand74c24fb2016-11-21 17:18:36 +01003446 "<b>uptime = </b> %dd %dh%02dm%02ds<br>\n"
3447 "<b>system limits:</b> memmax = %s%s; ulimit-n = %d<br>\n"
3448 "<b>maxsock = </b> %d; <b>maxconn = </b> %d; <b>maxpipes = </b> %d<br>\n"
Willy Tarreau1713c032019-05-23 12:23:55 +02003449 "current conns = %d; current pipes = %d/%d; conn rate = %d/sec; bit rate = %.3f %cbps<br>\n"
William Lallemand74c24fb2016-11-21 17:18:36 +01003450 "Running tasks: %d/%d; idle = %d %%<br>\n"
3451 "</td><td align=\"center\" nowrap>\n"
3452 "<table class=\"lgd\"><tr>\n"
3453 "<td class=\"active_up\">&nbsp;</td><td class=\"noborder\">active UP </td>"
3454 "<td class=\"backup_up\">&nbsp;</td><td class=\"noborder\">backup UP </td>"
3455 "</tr><tr>\n"
3456 "<td class=\"active_going_down\"></td><td class=\"noborder\">active UP, going down </td>"
3457 "<td class=\"backup_going_down\"></td><td class=\"noborder\">backup UP, going down </td>"
3458 "</tr><tr>\n"
3459 "<td class=\"active_going_up\"></td><td class=\"noborder\">active DOWN, going up </td>"
3460 "<td class=\"backup_going_up\"></td><td class=\"noborder\">backup DOWN, going up </td>"
3461 "</tr><tr>\n"
3462 "<td class=\"active_down\"></td><td class=\"noborder\">active or backup DOWN &nbsp;</td>"
3463 "<td class=\"active_no_check\"></td><td class=\"noborder\">not checked </td>"
3464 "</tr><tr>\n"
3465 "<td class=\"maintain\"></td><td class=\"noborder\" colspan=\"3\">active or backup DOWN for maintenance (MAINT) &nbsp;</td>"
3466 "</tr><tr>\n"
3467 "<td class=\"active_draining\"></td><td class=\"noborder\" colspan=\"3\">active or backup SOFT STOPPED for maintenance &nbsp;</td>"
3468 "</tr></table>\n"
3469 "Note: \"NOLB\"/\"DRAIN\" = UP with load-balancing disabled."
3470 "</td>"
3471 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
3472 "<b>Display option:</b><ul style=\"margin-top: 0.25em;\">"
3473 "",
Willy Tarreau91cefca2022-05-03 17:08:29 +02003474 (ctx->flags & STAT_HIDEVER) ? "" : (stats_version_string),
3475 pid, (ctx->flags & STAT_SHNODE) ? " on " : "",
3476 (ctx->flags & STAT_SHNODE) ? (uri->node ? uri->node : global.node) : "",
3477 (ctx->flags & STAT_SHDESC) ? ": " : "",
3478 (ctx->flags & STAT_SHDESC) ? (uri->desc ? uri->desc : global.desc) : "",
Willy Tarreaue8422bf2021-06-15 09:08:18 +02003479 pid, 1, 1, global.nbthread,
William Lallemand74c24fb2016-11-21 17:18:36 +01003480 up / 86400, (up % 86400) / 3600,
3481 (up % 3600) / 60, (up % 60),
3482 global.rlimit_memmax ? ultoa(global.rlimit_memmax) : "unlimited",
3483 global.rlimit_memmax ? " MB" : "",
3484 global.rlimit_nofile,
3485 global.maxsock, global.maxconn, global.maxpipes,
3486 actconn, pipes_used, pipes_used+pipes_free, read_freq_ctr(&global.conn_per_sec),
Willy Tarreau1713c032019-05-23 12:23:55 +02003487 bps >= 1000000000UL ? (bps / 1000000000.0) : bps >= 1000000UL ? (bps / 1000000.0) : (bps / 1000.0),
3488 bps >= 1000000000UL ? 'G' : bps >= 1000000UL ? 'M' : 'k',
Willy Tarreauf9d5e102021-10-08 10:43:59 +02003489 total_run_queues(), total_allocated_tasks(), clock_report_idle()
William Lallemand74c24fb2016-11-21 17:18:36 +01003490 );
3491
Willy Tarreau91cefca2022-05-03 17:08:29 +02003492 /* scope_txt = search query, ctx->scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
3493 memcpy(scope_txt, scope_ptr, ctx->scope_len);
3494 scope_txt[ctx->scope_len] = '\0';
William Lallemand74c24fb2016-11-21 17:18:36 +01003495
3496 chunk_appendf(&trash,
3497 "<li><form method=\"GET\">Scope : <input value=\"%s\" name=\"" STAT_SCOPE_INPUT_NAME "\" size=\"8\" maxlength=\"%d\" tabindex=\"1\"/></form>\n",
Willy Tarreau91cefca2022-05-03 17:08:29 +02003498 (ctx->scope_len > 0) ? scope_txt : "",
William Lallemand74c24fb2016-11-21 17:18:36 +01003499 STAT_SCOPE_TXT_MAXLEN);
3500
Willy Tarreau91cefca2022-05-03 17:08:29 +02003501 /* scope_txt = search pattern + search query, ctx->scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
William Lallemand74c24fb2016-11-21 17:18:36 +01003502 scope_txt[0] = 0;
Willy Tarreau91cefca2022-05-03 17:08:29 +02003503 if (ctx->scope_len) {
William Lallemand74c24fb2016-11-21 17:18:36 +01003504 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Willy Tarreau91cefca2022-05-03 17:08:29 +02003505 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), scope_ptr, ctx->scope_len);
3506 scope_txt[strlen(STAT_SCOPE_PATTERN) + ctx->scope_len] = 0;
William Lallemand74c24fb2016-11-21 17:18:36 +01003507 }
3508
Willy Tarreau91cefca2022-05-03 17:08:29 +02003509 if (ctx->flags & STAT_HIDE_DOWN)
William Lallemand74c24fb2016-11-21 17:18:36 +01003510 chunk_appendf(&trash,
3511 "<li><a href=\"%s%s%s%s\">Show all servers</a><br>\n",
3512 uri->uri_prefix,
3513 "",
Willy Tarreau91cefca2022-05-03 17:08:29 +02003514 (ctx->flags & STAT_NO_REFRESH) ? ";norefresh" : "",
William Lallemand74c24fb2016-11-21 17:18:36 +01003515 scope_txt);
3516 else
3517 chunk_appendf(&trash,
3518 "<li><a href=\"%s%s%s%s\">Hide 'DOWN' servers</a><br>\n",
3519 uri->uri_prefix,
3520 ";up",
Willy Tarreau91cefca2022-05-03 17:08:29 +02003521 (ctx->flags & STAT_NO_REFRESH) ? ";norefresh" : "",
William Lallemand74c24fb2016-11-21 17:18:36 +01003522 scope_txt);
3523
3524 if (uri->refresh > 0) {
Willy Tarreau91cefca2022-05-03 17:08:29 +02003525 if (ctx->flags & STAT_NO_REFRESH)
William Lallemand74c24fb2016-11-21 17:18:36 +01003526 chunk_appendf(&trash,
3527 "<li><a href=\"%s%s%s%s\">Enable refresh</a><br>\n",
3528 uri->uri_prefix,
Willy Tarreau91cefca2022-05-03 17:08:29 +02003529 (ctx->flags & STAT_HIDE_DOWN) ? ";up" : "",
William Lallemand74c24fb2016-11-21 17:18:36 +01003530 "",
3531 scope_txt);
3532 else
3533 chunk_appendf(&trash,
3534 "<li><a href=\"%s%s%s%s\">Disable refresh</a><br>\n",
3535 uri->uri_prefix,
Willy Tarreau91cefca2022-05-03 17:08:29 +02003536 (ctx->flags & STAT_HIDE_DOWN) ? ";up" : "",
William Lallemand74c24fb2016-11-21 17:18:36 +01003537 ";norefresh",
3538 scope_txt);
3539 }
3540
3541 chunk_appendf(&trash,
3542 "<li><a href=\"%s%s%s%s\">Refresh now</a><br>\n",
3543 uri->uri_prefix,
Willy Tarreau91cefca2022-05-03 17:08:29 +02003544 (ctx->flags & STAT_HIDE_DOWN) ? ";up" : "",
3545 (ctx->flags & STAT_NO_REFRESH) ? ";norefresh" : "",
William Lallemand74c24fb2016-11-21 17:18:36 +01003546 scope_txt);
3547
3548 chunk_appendf(&trash,
3549 "<li><a href=\"%s;csv%s%s\">CSV export</a><br>\n",
3550 uri->uri_prefix,
3551 (uri->refresh > 0) ? ";norefresh" : "",
3552 scope_txt);
3553
3554 chunk_appendf(&trash,
Christopher Faulet6338a082019-09-09 15:50:54 +02003555 "<li><a href=\"%s;json%s%s\">JSON export</a> (<a href=\"%s;json-schema\">schema</a>)<br>\n",
3556 uri->uri_prefix,
3557 (uri->refresh > 0) ? ";norefresh" : "",
3558 scope_txt, uri->uri_prefix);
3559
3560 chunk_appendf(&trash,
William Lallemand74c24fb2016-11-21 17:18:36 +01003561 "</ul></td>"
3562 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
3563 "<b>External resources:</b><ul style=\"margin-top: 0.25em;\">\n"
3564 "<li><a href=\"" PRODUCT_URL "\">Primary site</a><br>\n"
3565 "<li><a href=\"" PRODUCT_URL_UPD "\">Updates (v" PRODUCT_BRANCH ")</a><br>\n"
3566 "<li><a href=\"" PRODUCT_URL_DOC "\">Online manual</a><br>\n"
3567 "</ul>"
3568 "</td>"
3569 "</tr></table>\n"
3570 ""
3571 );
3572
Willy Tarreau91cefca2022-05-03 17:08:29 +02003573 if (ctx->st_code) {
3574 switch (ctx->st_code) {
William Lallemand74c24fb2016-11-21 17:18:36 +01003575 case STAT_STATUS_DONE:
3576 chunk_appendf(&trash,
3577 "<p><div class=active_up>"
3578 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
3579 "Action processed successfully."
3580 "</div>\n", uri->uri_prefix,
Willy Tarreau91cefca2022-05-03 17:08:29 +02003581 (ctx->flags & STAT_HIDE_DOWN) ? ";up" : "",
3582 (ctx->flags & STAT_NO_REFRESH) ? ";norefresh" : "",
William Lallemand74c24fb2016-11-21 17:18:36 +01003583 scope_txt);
3584 break;
3585 case STAT_STATUS_NONE:
3586 chunk_appendf(&trash,
3587 "<p><div class=active_going_down>"
3588 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
3589 "Nothing has changed."
3590 "</div>\n", uri->uri_prefix,
Willy Tarreau91cefca2022-05-03 17:08:29 +02003591 (ctx->flags & STAT_HIDE_DOWN) ? ";up" : "",
3592 (ctx->flags & STAT_NO_REFRESH) ? ";norefresh" : "",
William Lallemand74c24fb2016-11-21 17:18:36 +01003593 scope_txt);
3594 break;
3595 case STAT_STATUS_PART:
3596 chunk_appendf(&trash,
3597 "<p><div class=active_going_down>"
3598 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
3599 "Action partially processed.<br>"
3600 "Some server names are probably unknown or ambiguous (duplicated names in the backend)."
3601 "</div>\n", uri->uri_prefix,
Willy Tarreau91cefca2022-05-03 17:08:29 +02003602 (ctx->flags & STAT_HIDE_DOWN) ? ";up" : "",
3603 (ctx->flags & STAT_NO_REFRESH) ? ";norefresh" : "",
William Lallemand74c24fb2016-11-21 17:18:36 +01003604 scope_txt);
3605 break;
3606 case STAT_STATUS_ERRP:
3607 chunk_appendf(&trash,
3608 "<p><div class=active_down>"
3609 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
3610 "Action not processed because of invalid parameters."
3611 "<ul>"
3612 "<li>The action is maybe unknown.</li>"
Christopher Faulet2f9a41d2019-02-27 15:30:57 +01003613 "<li>Invalid key parameter (empty or too long).</li>"
William Lallemand74c24fb2016-11-21 17:18:36 +01003614 "<li>The backend name is probably unknown or ambiguous (duplicated names).</li>"
3615 "<li>Some server names are probably unknown or ambiguous (duplicated names in the backend).</li>"
3616 "</ul>"
3617 "</div>\n", uri->uri_prefix,
Willy Tarreau91cefca2022-05-03 17:08:29 +02003618 (ctx->flags & STAT_HIDE_DOWN) ? ";up" : "",
3619 (ctx->flags & STAT_NO_REFRESH) ? ";norefresh" : "",
William Lallemand74c24fb2016-11-21 17:18:36 +01003620 scope_txt);
3621 break;
3622 case STAT_STATUS_EXCD:
3623 chunk_appendf(&trash,
3624 "<p><div class=active_down>"
3625 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
3626 "<b>Action not processed : the buffer couldn't store all the data.<br>"
3627 "You should retry with less servers at a time.</b>"
3628 "</div>\n", uri->uri_prefix,
Willy Tarreau91cefca2022-05-03 17:08:29 +02003629 (ctx->flags & STAT_HIDE_DOWN) ? ";up" : "",
3630 (ctx->flags & STAT_NO_REFRESH) ? ";norefresh" : "",
William Lallemand74c24fb2016-11-21 17:18:36 +01003631 scope_txt);
3632 break;
3633 case STAT_STATUS_DENY:
3634 chunk_appendf(&trash,
3635 "<p><div class=active_down>"
3636 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
3637 "<b>Action denied.</b>"
3638 "</div>\n", uri->uri_prefix,
Willy Tarreau91cefca2022-05-03 17:08:29 +02003639 (ctx->flags & STAT_HIDE_DOWN) ? ";up" : "",
3640 (ctx->flags & STAT_NO_REFRESH) ? ";norefresh" : "",
William Lallemand74c24fb2016-11-21 17:18:36 +01003641 scope_txt);
3642 break;
Christopher Faulet3c2ecf72019-02-27 16:41:27 +01003643 case STAT_STATUS_IVAL:
3644 chunk_appendf(&trash,
3645 "<p><div class=active_down>"
3646 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
3647 "<b>Invalid requests (unsupported method or chunked encoded request).</b>"
3648 "</div>\n", uri->uri_prefix,
Willy Tarreau91cefca2022-05-03 17:08:29 +02003649 (ctx->flags & STAT_HIDE_DOWN) ? ";up" : "",
3650 (ctx->flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Christopher Faulet3c2ecf72019-02-27 16:41:27 +01003651 scope_txt);
3652 break;
William Lallemand74c24fb2016-11-21 17:18:36 +01003653 default:
3654 chunk_appendf(&trash,
3655 "<p><div class=active_no_check>"
3656 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
3657 "Unexpected result."
3658 "</div>\n", uri->uri_prefix,
Willy Tarreau91cefca2022-05-03 17:08:29 +02003659 (ctx->flags & STAT_HIDE_DOWN) ? ";up" : "",
3660 (ctx->flags & STAT_NO_REFRESH) ? ";norefresh" : "",
William Lallemand74c24fb2016-11-21 17:18:36 +01003661 scope_txt);
3662 }
3663 chunk_appendf(&trash, "<p>\n");
3664 }
3665}
3666
3667/* Dumps the HTML stats trailer block to the trash. The caller is responsible
3668 * for clearing the trash if needed.
3669 */
3670static void stats_dump_html_end()
3671{
3672 chunk_appendf(&trash, "</body></html>\n");
3673}
3674
Simon Horman05ee2132017-01-04 09:37:25 +01003675/* Dumps the stats JSON header to the trash buffer which. The caller is responsible
3676 * for clearing it if needed.
3677 */
3678static void stats_dump_json_header()
3679{
3680 chunk_strcat(&trash, "[");
3681}
3682
3683
3684/* Dumps the JSON stats trailer block to the trash. The caller is responsible
3685 * for clearing the trash if needed.
3686 */
3687static void stats_dump_json_end()
3688{
Willy Tarreau5a0e7ca2022-06-10 09:21:22 +02003689 chunk_strcat(&trash, "]\n");
Simon Horman05ee2132017-01-04 09:37:25 +01003690}
3691
Amaury Denoyelle98b81cb2020-10-02 18:32:04 +02003692/* Uses <appctx.ctx.stats.obj1> as a pointer to the current proxy and <obj2> as
3693 * a pointer to the current server/listener.
3694 */
Willy Tarreaucaff6312022-05-27 10:17:46 +02003695static int stats_dump_proxies(struct stconn *sc,
Amaury Denoyelle98b81cb2020-10-02 18:32:04 +02003696 struct htx *htx,
3697 struct uri_auth *uri)
3698{
Willy Tarreaucaff6312022-05-27 10:17:46 +02003699 struct appctx *appctx = __sc_appctx(sc);
Willy Tarreau91cefca2022-05-03 17:08:29 +02003700 struct show_stat_ctx *ctx = appctx->svcctx;
Willy Tarreaucaff6312022-05-27 10:17:46 +02003701 struct channel *rep = sc_ic(sc);
Amaury Denoyelle98b81cb2020-10-02 18:32:04 +02003702 struct proxy *px;
3703
3704 /* dump proxies */
Willy Tarreau91cefca2022-05-03 17:08:29 +02003705 while (ctx->obj1) {
Amaury Denoyelle98b81cb2020-10-02 18:32:04 +02003706 if (htx) {
3707 if (htx_almost_full(htx))
3708 goto full;
3709 }
3710 else {
3711 if (buffer_almost_full(&rep->buf))
3712 goto full;
3713 }
3714
Willy Tarreau91cefca2022-05-03 17:08:29 +02003715 px = ctx->obj1;
Marno Krahmer07954fb2021-06-24 16:51:08 +02003716 /* Skip the global frontend proxies and non-networked ones.
William Lallemand85a16b22021-08-03 13:18:11 +02003717 * Also skip proxies that were disabled in the configuration
Marno Krahmer07954fb2021-06-24 16:51:08 +02003718 * This change allows retrieving stats from "old" proxies after a reload.
3719 */
Christopher Fauletdfd10ab2021-10-06 14:24:19 +02003720 if (!(px->flags & PR_FL_DISABLED) && px->uuid > 0 &&
William Lallemande7f74622021-07-28 17:45:18 +02003721 (px->cap & (PR_CAP_FE | PR_CAP_BE)) && !(px->cap & PR_CAP_INT)) {
Willy Tarreaucaff6312022-05-27 10:17:46 +02003722 if (stats_dump_proxy_to_buffer(sc, htx, px, uri) == 0)
Amaury Denoyelle98b81cb2020-10-02 18:32:04 +02003723 return 0;
3724 }
3725
Willy Tarreau91cefca2022-05-03 17:08:29 +02003726 ctx->obj1 = px->next;
3727 ctx->px_st = STAT_PX_ST_INIT;
Amaury Denoyelle98b81cb2020-10-02 18:32:04 +02003728 }
3729
3730 return 1;
3731
3732 full:
Willy Tarreaucaff6312022-05-27 10:17:46 +02003733 sc_need_room(sc);
Amaury Denoyelle98b81cb2020-10-02 18:32:04 +02003734 return 0;
3735}
3736
Willy Tarreau4596fe22022-05-17 19:07:51 +02003737/* This function dumps statistics onto the stream connector's read buffer in
William Lallemand74c24fb2016-11-21 17:18:36 +01003738 * either CSV or HTML format. <uri> contains some HTML-specific parameters that
3739 * are ignored for CSV format (hence <uri> may be NULL there). It returns 0 if
3740 * it had to stop writing data and an I/O is needed, 1 if the dump is finished
3741 * and the stream must be closed, or -1 in case of any error. This function is
3742 * used by both the CLI and the HTTP handlers.
3743 */
Willy Tarreaucaff6312022-05-27 10:17:46 +02003744static int stats_dump_stat_to_buffer(struct stconn *sc, struct htx *htx,
Christopher Fauletef779222018-10-31 08:47:01 +01003745 struct uri_auth *uri)
William Lallemand74c24fb2016-11-21 17:18:36 +01003746{
Willy Tarreaucaff6312022-05-27 10:17:46 +02003747 struct appctx *appctx = __sc_appctx(sc);
Willy Tarreau91cefca2022-05-03 17:08:29 +02003748 struct show_stat_ctx *ctx = appctx->svcctx;
Willy Tarreaucaff6312022-05-27 10:17:46 +02003749 struct channel *rep = sc_ic(sc);
Willy Tarreau91cefca2022-05-03 17:08:29 +02003750 enum stats_domain domain = ctx->domain;
William Lallemand74c24fb2016-11-21 17:18:36 +01003751
3752 chunk_reset(&trash);
3753
Willy Tarreau41f88522022-05-03 18:39:27 +02003754 switch (ctx->state) {
Willy Tarreau6ef16482022-05-06 18:07:53 +02003755 case STAT_STATE_INIT:
3756 ctx->state = STAT_STATE_HEAD; /* let's start producing data */
Willy Tarreau9eb93c02022-11-14 07:12:05 +01003757 __fallthrough;
William Lallemand74c24fb2016-11-21 17:18:36 +01003758
Willy Tarreau6ef16482022-05-06 18:07:53 +02003759 case STAT_STATE_HEAD:
Willy Tarreau91cefca2022-05-03 17:08:29 +02003760 if (ctx->flags & STAT_FMT_HTML)
Willy Tarreau676c29e2019-10-09 10:50:01 +02003761 stats_dump_html_head(appctx, uri);
Willy Tarreau91cefca2022-05-03 17:08:29 +02003762 else if (ctx->flags & STAT_JSON_SCHM)
Christopher Faulet6338a082019-09-09 15:50:54 +02003763 stats_dump_json_schema(&trash);
Willy Tarreau91cefca2022-05-03 17:08:29 +02003764 else if (ctx->flags & STAT_FMT_JSON)
Willy Tarreau9d7fb632017-04-11 07:53:04 +02003765 stats_dump_json_header();
Willy Tarreau91cefca2022-05-03 17:08:29 +02003766 else if (!(ctx->flags & STAT_FMT_TYPED))
3767 stats_dump_csv_header(ctx->domain);
William Lallemand74c24fb2016-11-21 17:18:36 +01003768
Christopher Fauletef779222018-10-31 08:47:01 +01003769 if (!stats_putchk(rep, htx, &trash))
3770 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01003771
Willy Tarreau91cefca2022-05-03 17:08:29 +02003772 if (ctx->flags & STAT_JSON_SCHM) {
Willy Tarreau6ef16482022-05-06 18:07:53 +02003773 ctx->state = STAT_STATE_FIN;
Christopher Faulet6338a082019-09-09 15:50:54 +02003774 return 1;
3775 }
Willy Tarreau6ef16482022-05-06 18:07:53 +02003776 ctx->state = STAT_STATE_INFO;
Willy Tarreau9eb93c02022-11-14 07:12:05 +01003777 __fallthrough;
William Lallemand74c24fb2016-11-21 17:18:36 +01003778
Willy Tarreau6ef16482022-05-06 18:07:53 +02003779 case STAT_STATE_INFO:
Willy Tarreau91cefca2022-05-03 17:08:29 +02003780 if (ctx->flags & STAT_FMT_HTML) {
Willy Tarreaucaff6312022-05-27 10:17:46 +02003781 stats_dump_html_info(sc, uri);
Christopher Fauletef779222018-10-31 08:47:01 +01003782 if (!stats_putchk(rep, htx, &trash))
3783 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01003784 }
3785
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02003786 if (domain == STATS_DOMAIN_PROXY)
Willy Tarreau91cefca2022-05-03 17:08:29 +02003787 ctx->obj1 = proxies_list;
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02003788
Willy Tarreau91cefca2022-05-03 17:08:29 +02003789 ctx->px_st = STAT_PX_ST_INIT;
Willy Tarreau6ef16482022-05-06 18:07:53 +02003790 ctx->state = STAT_STATE_LIST;
Willy Tarreau9eb93c02022-11-14 07:12:05 +01003791 __fallthrough;
William Lallemand74c24fb2016-11-21 17:18:36 +01003792
Willy Tarreau6ef16482022-05-06 18:07:53 +02003793 case STAT_STATE_LIST:
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02003794 switch (domain) {
Emeric Brunf8642ee2021-10-29 17:59:18 +02003795 case STATS_DOMAIN_RESOLVERS:
Willy Tarreaucaff6312022-05-27 10:17:46 +02003796 if (!stats_dump_resolvers(sc, stat_l[domain],
Emeric Brund3b44952021-01-06 16:01:02 +01003797 stat_count[domain],
3798 &stats_module_list[domain])) {
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02003799 return 0;
3800 }
3801 break;
3802
3803 case STATS_DOMAIN_PROXY:
3804 default:
3805 /* dump proxies */
Willy Tarreaucaff6312022-05-27 10:17:46 +02003806 if (!stats_dump_proxies(sc, htx, uri))
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02003807 return 0;
3808 break;
3809 }
William Lallemand74c24fb2016-11-21 17:18:36 +01003810
Willy Tarreau6ef16482022-05-06 18:07:53 +02003811 ctx->state = STAT_STATE_END;
Willy Tarreau9eb93c02022-11-14 07:12:05 +01003812 __fallthrough;
William Lallemand74c24fb2016-11-21 17:18:36 +01003813
Willy Tarreau6ef16482022-05-06 18:07:53 +02003814 case STAT_STATE_END:
Willy Tarreau91cefca2022-05-03 17:08:29 +02003815 if (ctx->flags & (STAT_FMT_HTML|STAT_FMT_JSON)) {
3816 if (ctx->flags & STAT_FMT_HTML)
Simon Horman05ee2132017-01-04 09:37:25 +01003817 stats_dump_html_end();
3818 else
3819 stats_dump_json_end();
Christopher Fauletef779222018-10-31 08:47:01 +01003820 if (!stats_putchk(rep, htx, &trash))
3821 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01003822 }
3823
Willy Tarreau6ef16482022-05-06 18:07:53 +02003824 ctx->state = STAT_STATE_FIN;
Willy Tarreau9eb93c02022-11-14 07:12:05 +01003825 __fallthrough;
William Lallemand74c24fb2016-11-21 17:18:36 +01003826
Willy Tarreau6ef16482022-05-06 18:07:53 +02003827 case STAT_STATE_FIN:
William Lallemand74c24fb2016-11-21 17:18:36 +01003828 return 1;
3829
3830 default:
3831 /* unknown state ! */
Willy Tarreau6ef16482022-05-06 18:07:53 +02003832 ctx->state = STAT_STATE_FIN;
William Lallemand74c24fb2016-11-21 17:18:36 +01003833 return -1;
3834 }
Christopher Fauletef779222018-10-31 08:47:01 +01003835
3836 full:
Willy Tarreaucaff6312022-05-27 10:17:46 +02003837 sc_need_room(sc);
Christopher Fauletef779222018-10-31 08:47:01 +01003838 return 0;
3839
William Lallemand74c24fb2016-11-21 17:18:36 +01003840}
3841
3842/* We reached the stats page through a POST request. The appctx is
3843 * expected to have already been allocated by the caller.
3844 * Parse the posted data and enable/disable servers if necessary.
3845 * Returns 1 if request was parsed or zero if it needs more data.
3846 */
Willy Tarreaucaff6312022-05-27 10:17:46 +02003847static int stats_process_http_post(struct stconn *sc)
William Lallemand74c24fb2016-11-21 17:18:36 +01003848{
Willy Tarreaucaff6312022-05-27 10:17:46 +02003849 struct stream *s = __sc_strm(sc);
3850 struct appctx *appctx = __sc_appctx(sc);
Willy Tarreau91cefca2022-05-03 17:08:29 +02003851 struct show_stat_ctx *ctx = appctx->svcctx;
William Lallemand74c24fb2016-11-21 17:18:36 +01003852
3853 struct proxy *px = NULL;
3854 struct server *sv = NULL;
3855
3856 char key[LINESIZE];
3857 int action = ST_ADM_ACTION_NONE;
3858 int reprocess = 0;
3859
3860 int total_servers = 0;
3861 int altered_servers = 0;
3862
3863 char *first_param, *cur_param, *next_param, *end_params;
3864 char *st_cur_param = NULL;
3865 char *st_next_param = NULL;
3866
Christopher Fauleta3618372018-12-13 21:59:56 +01003867 struct buffer *temp = get_trash_chunk();
William Lallemand74c24fb2016-11-21 17:18:36 +01003868
Christopher Fauletb7f88902019-07-15 21:56:43 +02003869 struct htx *htx = htxbuf(&s->req.buf);
3870 struct htx_blk *blk;
Christopher Fauleta3618372018-12-13 21:59:56 +01003871
Christopher Fauletb7f88902019-07-15 21:56:43 +02003872 /* we need more data */
3873 if (s->txn->req.msg_state < HTTP_MSG_DONE) {
3874 /* check if we can receive more */
3875 if (htx_free_data_space(htx) <= global.tune.maxrewrite) {
Willy Tarreau91cefca2022-05-03 17:08:29 +02003876 ctx->st_code = STAT_STATUS_EXCD;
Christopher Fauletb7f88902019-07-15 21:56:43 +02003877 goto out;
Christopher Fauleta3618372018-12-13 21:59:56 +01003878 }
Christopher Fauletb7f88902019-07-15 21:56:43 +02003879 goto wait;
3880 }
Christopher Fauleta3618372018-12-13 21:59:56 +01003881
Christopher Fauletb7f88902019-07-15 21:56:43 +02003882 /* The request was fully received. Copy data */
3883 blk = htx_get_head_blk(htx);
3884 while (blk) {
3885 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Fauleta3618372018-12-13 21:59:56 +01003886
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01003887 if (type == HTX_BLK_TLR || type == HTX_BLK_EOT)
Christopher Fauletb7f88902019-07-15 21:56:43 +02003888 break;
3889 if (type == HTX_BLK_DATA) {
3890 struct ist v = htx_get_blk_value(htx, blk);
Christopher Fauleta3618372018-12-13 21:59:56 +01003891
Christopher Fauletb7f88902019-07-15 21:56:43 +02003892 if (!chunk_memcat(temp, v.ptr, v.len)) {
Willy Tarreau91cefca2022-05-03 17:08:29 +02003893 ctx->st_code = STAT_STATUS_EXCD;
Christopher Faulet2f9a41d2019-02-27 15:30:57 +01003894 goto out;
3895 }
Christopher Fauleta3618372018-12-13 21:59:56 +01003896 }
Christopher Fauletb7f88902019-07-15 21:56:43 +02003897 blk = htx_get_next_blk(htx, blk);
William Lallemand74c24fb2016-11-21 17:18:36 +01003898 }
3899
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003900 first_param = temp->area;
Christopher Fauleta3618372018-12-13 21:59:56 +01003901 end_params = temp->area + temp->data;
William Lallemand74c24fb2016-11-21 17:18:36 +01003902 cur_param = next_param = end_params;
3903 *end_params = '\0';
3904
Willy Tarreau91cefca2022-05-03 17:08:29 +02003905 ctx->st_code = STAT_STATUS_NONE;
William Lallemand74c24fb2016-11-21 17:18:36 +01003906
3907 /*
3908 * Parse the parameters in reverse order to only store the last value.
3909 * From the html form, the backend and the action are at the end.
3910 */
3911 while (cur_param > first_param) {
3912 char *value;
3913 int poffset, plen;
3914
3915 cur_param--;
3916
3917 if ((*cur_param == '&') || (cur_param == first_param)) {
3918 reprocess_servers:
3919 /* Parse the key */
3920 poffset = (cur_param != first_param ? 1 : 0);
3921 plen = next_param - cur_param + (cur_param == first_param ? 1 : 0);
3922 if ((plen > 0) && (plen <= sizeof(key))) {
3923 strncpy(key, cur_param + poffset, plen);
3924 key[plen - 1] = '\0';
3925 } else {
Willy Tarreau91cefca2022-05-03 17:08:29 +02003926 ctx->st_code = STAT_STATUS_ERRP;
William Lallemand74c24fb2016-11-21 17:18:36 +01003927 goto out;
3928 }
3929
3930 /* Parse the value */
3931 value = key;
3932 while (*value != '\0' && *value != '=') {
3933 value++;
3934 }
3935 if (*value == '=') {
3936 /* Ok, a value is found, we can mark the end of the key */
3937 *value++ = '\0';
3938 }
Willy Tarreau62ba9ba2020-04-23 17:54:47 +02003939 if (url_decode(key, 1) < 0 || url_decode(value, 1) < 0)
William Lallemand74c24fb2016-11-21 17:18:36 +01003940 break;
3941
3942 /* Now we can check the key to see what to do */
3943 if (!px && (strcmp(key, "b") == 0)) {
3944 if ((px = proxy_be_by_name(value)) == NULL) {
3945 /* the backend name is unknown or ambiguous (duplicate names) */
Willy Tarreau91cefca2022-05-03 17:08:29 +02003946 ctx->st_code = STAT_STATUS_ERRP;
William Lallemand74c24fb2016-11-21 17:18:36 +01003947 goto out;
3948 }
3949 }
3950 else if (!action && (strcmp(key, "action") == 0)) {
3951 if (strcmp(value, "ready") == 0) {
3952 action = ST_ADM_ACTION_READY;
3953 }
3954 else if (strcmp(value, "drain") == 0) {
3955 action = ST_ADM_ACTION_DRAIN;
3956 }
3957 else if (strcmp(value, "maint") == 0) {
3958 action = ST_ADM_ACTION_MAINT;
3959 }
3960 else if (strcmp(value, "shutdown") == 0) {
3961 action = ST_ADM_ACTION_SHUTDOWN;
3962 }
3963 else if (strcmp(value, "dhlth") == 0) {
3964 action = ST_ADM_ACTION_DHLTH;
3965 }
3966 else if (strcmp(value, "ehlth") == 0) {
3967 action = ST_ADM_ACTION_EHLTH;
3968 }
3969 else if (strcmp(value, "hrunn") == 0) {
3970 action = ST_ADM_ACTION_HRUNN;
3971 }
3972 else if (strcmp(value, "hnolb") == 0) {
3973 action = ST_ADM_ACTION_HNOLB;
3974 }
3975 else if (strcmp(value, "hdown") == 0) {
3976 action = ST_ADM_ACTION_HDOWN;
3977 }
3978 else if (strcmp(value, "dagent") == 0) {
3979 action = ST_ADM_ACTION_DAGENT;
3980 }
3981 else if (strcmp(value, "eagent") == 0) {
3982 action = ST_ADM_ACTION_EAGENT;
3983 }
3984 else if (strcmp(value, "arunn") == 0) {
3985 action = ST_ADM_ACTION_ARUNN;
3986 }
3987 else if (strcmp(value, "adown") == 0) {
3988 action = ST_ADM_ACTION_ADOWN;
3989 }
3990 /* else these are the old supported methods */
3991 else if (strcmp(value, "disable") == 0) {
3992 action = ST_ADM_ACTION_DISABLE;
3993 }
3994 else if (strcmp(value, "enable") == 0) {
3995 action = ST_ADM_ACTION_ENABLE;
3996 }
3997 else if (strcmp(value, "stop") == 0) {
3998 action = ST_ADM_ACTION_STOP;
3999 }
4000 else if (strcmp(value, "start") == 0) {
4001 action = ST_ADM_ACTION_START;
4002 }
4003 else {
Willy Tarreau91cefca2022-05-03 17:08:29 +02004004 ctx->st_code = STAT_STATUS_ERRP;
William Lallemand74c24fb2016-11-21 17:18:36 +01004005 goto out;
4006 }
4007 }
4008 else if (strcmp(key, "s") == 0) {
4009 if (!(px && action)) {
4010 /*
4011 * Indicates that we'll need to reprocess the parameters
4012 * as soon as backend and action are known
4013 */
4014 if (!reprocess) {
4015 st_cur_param = cur_param;
4016 st_next_param = next_param;
4017 }
4018 reprocess = 1;
4019 }
4020 else if ((sv = findserver(px, value)) != NULL) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004021 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
William Lallemand74c24fb2016-11-21 17:18:36 +01004022 switch (action) {
4023 case ST_ADM_ACTION_DISABLE:
Emeric Brun52a91d32017-08-31 14:41:55 +02004024 if (!(sv->cur_admin & SRV_ADMF_FMAINT)) {
William Lallemand74c24fb2016-11-21 17:18:36 +01004025 altered_servers++;
4026 total_servers++;
4027 srv_set_admin_flag(sv, SRV_ADMF_FMAINT, "'disable' on stats page");
4028 }
4029 break;
4030 case ST_ADM_ACTION_ENABLE:
Emeric Brun52a91d32017-08-31 14:41:55 +02004031 if (sv->cur_admin & SRV_ADMF_FMAINT) {
William Lallemand74c24fb2016-11-21 17:18:36 +01004032 altered_servers++;
4033 total_servers++;
4034 srv_clr_admin_flag(sv, SRV_ADMF_FMAINT);
4035 }
4036 break;
4037 case ST_ADM_ACTION_STOP:
Emeric Brun52a91d32017-08-31 14:41:55 +02004038 if (!(sv->cur_admin & SRV_ADMF_FDRAIN)) {
William Lallemand74c24fb2016-11-21 17:18:36 +01004039 srv_set_admin_flag(sv, SRV_ADMF_FDRAIN, "'stop' on stats page");
4040 altered_servers++;
4041 total_servers++;
4042 }
4043 break;
4044 case ST_ADM_ACTION_START:
Emeric Brun52a91d32017-08-31 14:41:55 +02004045 if (sv->cur_admin & SRV_ADMF_FDRAIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +01004046 srv_clr_admin_flag(sv, SRV_ADMF_FDRAIN);
4047 altered_servers++;
4048 total_servers++;
4049 }
4050 break;
4051 case ST_ADM_ACTION_DHLTH:
4052 if (sv->check.state & CHK_ST_CONFIGURED) {
4053 sv->check.state &= ~CHK_ST_ENABLED;
4054 altered_servers++;
4055 total_servers++;
4056 }
4057 break;
4058 case ST_ADM_ACTION_EHLTH:
4059 if (sv->check.state & CHK_ST_CONFIGURED) {
4060 sv->check.state |= CHK_ST_ENABLED;
4061 altered_servers++;
4062 total_servers++;
4063 }
4064 break;
4065 case ST_ADM_ACTION_HRUNN:
4066 if (!(sv->track)) {
4067 sv->check.health = sv->check.rise + sv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02004068 srv_set_running(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01004069 altered_servers++;
4070 total_servers++;
4071 }
4072 break;
4073 case ST_ADM_ACTION_HNOLB:
4074 if (!(sv->track)) {
4075 sv->check.health = sv->check.rise + sv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02004076 srv_set_stopping(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01004077 altered_servers++;
4078 total_servers++;
4079 }
4080 break;
4081 case ST_ADM_ACTION_HDOWN:
4082 if (!(sv->track)) {
4083 sv->check.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02004084 srv_set_stopped(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01004085 altered_servers++;
4086 total_servers++;
4087 }
4088 break;
4089 case ST_ADM_ACTION_DAGENT:
4090 if (sv->agent.state & CHK_ST_CONFIGURED) {
4091 sv->agent.state &= ~CHK_ST_ENABLED;
4092 altered_servers++;
4093 total_servers++;
4094 }
4095 break;
4096 case ST_ADM_ACTION_EAGENT:
4097 if (sv->agent.state & CHK_ST_CONFIGURED) {
4098 sv->agent.state |= CHK_ST_ENABLED;
4099 altered_servers++;
4100 total_servers++;
4101 }
4102 break;
4103 case ST_ADM_ACTION_ARUNN:
4104 if (sv->agent.state & CHK_ST_ENABLED) {
4105 sv->agent.health = sv->agent.rise + sv->agent.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02004106 srv_set_running(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01004107 altered_servers++;
4108 total_servers++;
4109 }
4110 break;
4111 case ST_ADM_ACTION_ADOWN:
4112 if (sv->agent.state & CHK_ST_ENABLED) {
4113 sv->agent.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02004114 srv_set_stopped(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01004115 altered_servers++;
4116 total_servers++;
4117 }
4118 break;
4119 case ST_ADM_ACTION_READY:
4120 srv_adm_set_ready(sv);
4121 altered_servers++;
4122 total_servers++;
4123 break;
4124 case ST_ADM_ACTION_DRAIN:
4125 srv_adm_set_drain(sv);
4126 altered_servers++;
4127 total_servers++;
4128 break;
4129 case ST_ADM_ACTION_MAINT:
4130 srv_adm_set_maint(sv);
4131 altered_servers++;
4132 total_servers++;
4133 break;
4134 case ST_ADM_ACTION_SHUTDOWN:
Christopher Fauletdfd10ab2021-10-06 14:24:19 +02004135 if (!(px->flags & (PR_FL_DISABLED|PR_FL_STOPPED))) {
Willy Tarreauaf7ea812019-11-14 16:42:04 +01004136 srv_shutdown_streams(sv, SF_ERR_KILLED);
William Lallemand74c24fb2016-11-21 17:18:36 +01004137 altered_servers++;
4138 total_servers++;
4139 }
4140 break;
4141 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004142 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
William Lallemand74c24fb2016-11-21 17:18:36 +01004143 } else {
4144 /* the server name is unknown or ambiguous (duplicate names) */
4145 total_servers++;
4146 }
4147 }
4148 if (reprocess && px && action) {
4149 /* Now, we know the backend and the action chosen by the user.
4150 * We can safely restart from the first server parameter
4151 * to reprocess them
4152 */
4153 cur_param = st_cur_param;
4154 next_param = st_next_param;
4155 reprocess = 0;
4156 goto reprocess_servers;
4157 }
4158
4159 next_param = cur_param;
4160 }
4161 }
4162
4163 if (total_servers == 0) {
Willy Tarreau91cefca2022-05-03 17:08:29 +02004164 ctx->st_code = STAT_STATUS_NONE;
William Lallemand74c24fb2016-11-21 17:18:36 +01004165 }
4166 else if (altered_servers == 0) {
Willy Tarreau91cefca2022-05-03 17:08:29 +02004167 ctx->st_code = STAT_STATUS_ERRP;
William Lallemand74c24fb2016-11-21 17:18:36 +01004168 }
4169 else if (altered_servers == total_servers) {
Willy Tarreau91cefca2022-05-03 17:08:29 +02004170 ctx->st_code = STAT_STATUS_DONE;
William Lallemand74c24fb2016-11-21 17:18:36 +01004171 }
4172 else {
Willy Tarreau91cefca2022-05-03 17:08:29 +02004173 ctx->st_code = STAT_STATUS_PART;
William Lallemand74c24fb2016-11-21 17:18:36 +01004174 }
4175 out:
4176 return 1;
Christopher Faulet2f9a41d2019-02-27 15:30:57 +01004177 wait:
Willy Tarreau91cefca2022-05-03 17:08:29 +02004178 ctx->st_code = STAT_STATUS_NONE;
Christopher Faulet2f9a41d2019-02-27 15:30:57 +01004179 return 0;
Christopher Fauletef779222018-10-31 08:47:01 +01004180}
4181
4182
Willy Tarreaucaff6312022-05-27 10:17:46 +02004183static int stats_send_http_headers(struct stconn *sc, struct htx *htx)
Christopher Fauletef779222018-10-31 08:47:01 +01004184{
Willy Tarreaucaff6312022-05-27 10:17:46 +02004185 struct stream *s = __sc_strm(sc);
Christopher Fauletef779222018-10-31 08:47:01 +01004186 struct uri_auth *uri = s->be->uri_auth;
Willy Tarreaucaff6312022-05-27 10:17:46 +02004187 struct appctx *appctx = __sc_appctx(sc);
Willy Tarreau91cefca2022-05-03 17:08:29 +02004188 struct show_stat_ctx *ctx = appctx->svcctx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004189 struct htx_sl *sl;
4190 unsigned int flags;
Christopher Fauletef779222018-10-31 08:47:01 +01004191
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004192 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);
4193 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), ist("200"), ist("OK"));
4194 if (!sl)
Christopher Fauletef779222018-10-31 08:47:01 +01004195 goto full;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004196 sl->info.res.status = 200;
Christopher Fauletef779222018-10-31 08:47:01 +01004197
Christopher Fauletb829f4c2019-03-29 16:13:55 +01004198 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")))
Christopher Fauletef779222018-10-31 08:47:01 +01004199 goto full;
Willy Tarreau91cefca2022-05-03 17:08:29 +02004200 if (ctx->flags & STAT_FMT_HTML) {
Christopher Fauletef779222018-10-31 08:47:01 +01004201 if (!htx_add_header(htx, ist("Content-Type"), ist("text/html")))
4202 goto full;
4203 }
Willy Tarreau91cefca2022-05-03 17:08:29 +02004204 else if (ctx->flags & (STAT_FMT_JSON|STAT_JSON_SCHM)) {
Christopher Faulet6338a082019-09-09 15:50:54 +02004205 if (!htx_add_header(htx, ist("Content-Type"), ist("application/json")))
4206 goto full;
4207 }
Christopher Fauletef779222018-10-31 08:47:01 +01004208 else {
4209 if (!htx_add_header(htx, ist("Content-Type"), ist("text/plain")))
4210 goto full;
4211 }
4212
Willy Tarreau91cefca2022-05-03 17:08:29 +02004213 if (uri->refresh > 0 && !(ctx->flags & STAT_NO_REFRESH)) {
Christopher Fauletef779222018-10-31 08:47:01 +01004214 const char *refresh = U2A(uri->refresh);
Tim Duesterhusdcf753a2021-03-04 17:31:47 +01004215 if (!htx_add_header(htx, ist("Refresh"), ist(refresh)))
Christopher Fauletef779222018-10-31 08:47:01 +01004216 goto full;
4217 }
4218
Willy Tarreau91cefca2022-05-03 17:08:29 +02004219 if (ctx->flags & STAT_CHUNKED) {
Christopher Fauletef779222018-10-31 08:47:01 +01004220 if (!htx_add_header(htx, ist("Transfer-Encoding"), ist("chunked")))
4221 goto full;
4222 }
4223
4224 if (!htx_add_endof(htx, HTX_BLK_EOH))
4225 goto full;
4226
Christopher Faulet1e2d6362019-02-27 16:28:48 +01004227 channel_add_input(&s->res, htx->data);
Christopher Fauletef779222018-10-31 08:47:01 +01004228 return 1;
4229
4230 full:
4231 htx_reset(htx);
Willy Tarreaucaff6312022-05-27 10:17:46 +02004232 sc_need_room(sc);
Christopher Fauletef779222018-10-31 08:47:01 +01004233 return 0;
William Lallemand74c24fb2016-11-21 17:18:36 +01004234}
4235
Christopher Fauletef779222018-10-31 08:47:01 +01004236
Willy Tarreaucaff6312022-05-27 10:17:46 +02004237static int stats_send_http_redirect(struct stconn *sc, struct htx *htx)
Christopher Fauletef779222018-10-31 08:47:01 +01004238{
4239 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
Willy Tarreaucaff6312022-05-27 10:17:46 +02004240 struct stream *s = __sc_strm(sc);
Christopher Fauletef779222018-10-31 08:47:01 +01004241 struct uri_auth *uri = s->be->uri_auth;
Willy Tarreaucaff6312022-05-27 10:17:46 +02004242 struct appctx *appctx = __sc_appctx(sc);
Willy Tarreau91cefca2022-05-03 17:08:29 +02004243 struct show_stat_ctx *ctx = appctx->svcctx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004244 struct htx_sl *sl;
4245 unsigned int flags;
Christopher Fauletef779222018-10-31 08:47:01 +01004246
Willy Tarreau91cefca2022-05-03 17:08:29 +02004247 /* scope_txt = search pattern + search query, ctx->scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
Christopher Fauletef779222018-10-31 08:47:01 +01004248 scope_txt[0] = 0;
Willy Tarreau91cefca2022-05-03 17:08:29 +02004249 if (ctx->scope_len) {
Willy Tarreaucaff6312022-05-27 10:17:46 +02004250 const char *scope_ptr = stats_scope_ptr(appctx, sc);
Christopher Fauleted7a0662019-01-14 11:07:34 +01004251
Christopher Fauletef779222018-10-31 08:47:01 +01004252 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Willy Tarreau91cefca2022-05-03 17:08:29 +02004253 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), scope_ptr, ctx->scope_len);
4254 scope_txt[strlen(STAT_SCOPE_PATTERN) + ctx->scope_len] = 0;
Christopher Fauletef779222018-10-31 08:47:01 +01004255 }
4256
4257 /* We don't want to land on the posted stats page because a refresh will
4258 * repost the data. We don't want this to happen on accident so we redirect
4259 * the browse to the stats page with a GET.
4260 */
4261 chunk_printf(&trash, "%s;st=%s%s%s%s",
4262 uri->uri_prefix,
Willy Tarreau91cefca2022-05-03 17:08:29 +02004263 ((ctx->st_code > STAT_STATUS_INIT) &&
4264 (ctx->st_code < STAT_STATUS_SIZE) &&
4265 stat_status_codes[ctx->st_code]) ?
4266 stat_status_codes[ctx->st_code] :
Christopher Fauletef779222018-10-31 08:47:01 +01004267 stat_status_codes[STAT_STATUS_UNKN],
Willy Tarreau91cefca2022-05-03 17:08:29 +02004268 (ctx->flags & STAT_HIDE_DOWN) ? ";up" : "",
4269 (ctx->flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Christopher Fauletef779222018-10-31 08:47:01 +01004270 scope_txt);
4271
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004272 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_CHNK);
4273 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), ist("303"), ist("See Other"));
4274 if (!sl)
Christopher Fauletef779222018-10-31 08:47:01 +01004275 goto full;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004276 sl->info.res.status = 303;
Christopher Fauletef779222018-10-31 08:47:01 +01004277
4278 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")) ||
Christopher Fauletef779222018-10-31 08:47:01 +01004279 !htx_add_header(htx, ist("Content-Type"), ist("text/plain")) ||
4280 !htx_add_header(htx, ist("Content-Length"), ist("0")) ||
4281 !htx_add_header(htx, ist("Location"), ist2(trash.area, trash.data)))
4282 goto full;
4283
4284 if (!htx_add_endof(htx, HTX_BLK_EOH))
4285 goto full;
4286
Christopher Faulet1e2d6362019-02-27 16:28:48 +01004287 channel_add_input(&s->res, htx->data);
Christopher Fauletef779222018-10-31 08:47:01 +01004288 return 1;
4289
4290full:
4291 htx_reset(htx);
Willy Tarreaucaff6312022-05-27 10:17:46 +02004292 sc_need_room(sc);
Christopher Fauletef779222018-10-31 08:47:01 +01004293 return 0;
4294}
William Lallemand74c24fb2016-11-21 17:18:36 +01004295
Simon Horman05ee2132017-01-04 09:37:25 +01004296
Willy Tarreau4596fe22022-05-17 19:07:51 +02004297/* This I/O handler runs as an applet embedded in a stream connector. It is
William Lallemand74c24fb2016-11-21 17:18:36 +01004298 * used to send HTTP stats over a TCP socket. The mechanism is very simple.
4299 * appctx->st0 contains the operation in progress (dump, done). The handler
4300 * automatically unregisters itself once transfer is complete.
4301 */
Christopher Fauletb7f88902019-07-15 21:56:43 +02004302static void http_stats_io_handler(struct appctx *appctx)
Christopher Fauletef779222018-10-31 08:47:01 +01004303{
Willy Tarreau91cefca2022-05-03 17:08:29 +02004304 struct show_stat_ctx *ctx = appctx->svcctx;
Willy Tarreauc12b3212022-05-27 11:08:15 +02004305 struct stconn *sc = appctx_sc(appctx);
Willy Tarreaucaff6312022-05-27 10:17:46 +02004306 struct stream *s = __sc_strm(sc);
4307 struct channel *req = sc_oc(sc);
4308 struct channel *res = sc_ic(sc);
Christopher Fauletef779222018-10-31 08:47:01 +01004309 struct htx *req_htx, *res_htx;
4310
Amaury Denoyelle072f97e2020-10-05 11:49:37 +02004311 /* only proxy stats are available via http */
Willy Tarreau91cefca2022-05-03 17:08:29 +02004312 ctx->domain = STATS_DOMAIN_PROXY;
Amaury Denoyelle072f97e2020-10-05 11:49:37 +02004313
Christopher Fauletef779222018-10-31 08:47:01 +01004314 res_htx = htx_from_buf(&res->buf);
4315
Willy Tarreaucaff6312022-05-27 10:17:46 +02004316 if (unlikely(sc->state == SC_ST_DIS || sc->state == SC_ST_CLO))
Christopher Fauletef779222018-10-31 08:47:01 +01004317 goto out;
4318
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05004319 /* Check if the input buffer is available. */
Christopher Fauletef779222018-10-31 08:47:01 +01004320 if (!b_size(&res->buf)) {
Willy Tarreaucaff6312022-05-27 10:17:46 +02004321 sc_need_room(sc);
Christopher Fauletef779222018-10-31 08:47:01 +01004322 goto out;
4323 }
4324
4325 /* check that the output is not closed */
Christopher Faulet3a78aa62019-02-27 16:19:48 +01004326 if (res->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_SHUTR))
4327 appctx->st0 = STAT_HTTP_END;
Christopher Fauletef779222018-10-31 08:47:01 +01004328
4329 /* all states are processed in sequence */
4330 if (appctx->st0 == STAT_HTTP_HEAD) {
Willy Tarreaucaff6312022-05-27 10:17:46 +02004331 if (stats_send_http_headers(sc, res_htx)) {
Christopher Fauletef779222018-10-31 08:47:01 +01004332 if (s->txn->meth == HTTP_METH_HEAD)
4333 appctx->st0 = STAT_HTTP_DONE;
4334 else
4335 appctx->st0 = STAT_HTTP_DUMP;
4336 }
4337 }
4338
4339 if (appctx->st0 == STAT_HTTP_DUMP) {
Willy Tarreaucaff6312022-05-27 10:17:46 +02004340 if (stats_dump_stat_to_buffer(sc, res_htx, s->be->uri_auth))
Christopher Fauletef779222018-10-31 08:47:01 +01004341 appctx->st0 = STAT_HTTP_DONE;
4342 }
4343
4344 if (appctx->st0 == STAT_HTTP_POST) {
Willy Tarreaucaff6312022-05-27 10:17:46 +02004345 if (stats_process_http_post(sc))
Christopher Fauletef779222018-10-31 08:47:01 +01004346 appctx->st0 = STAT_HTTP_LAST;
Christopher Faulet3a78aa62019-02-27 16:19:48 +01004347 else if (req->flags & CF_SHUTR)
Christopher Fauletef779222018-10-31 08:47:01 +01004348 appctx->st0 = STAT_HTTP_DONE;
4349 }
4350
4351 if (appctx->st0 == STAT_HTTP_LAST) {
Willy Tarreaucaff6312022-05-27 10:17:46 +02004352 if (stats_send_http_redirect(sc, res_htx))
Christopher Fauletef779222018-10-31 08:47:01 +01004353 appctx->st0 = STAT_HTTP_DONE;
4354 }
4355
4356 if (appctx->st0 == STAT_HTTP_DONE) {
Christopher Faulet08b45cb2022-04-07 09:25:13 +02004357 /* no more data are expected. If the response buffer is empty,
4358 * be sure to add something (EOT block in this case) to have
4359 * something to send. It is important to be sure the EOM flags
4360 * will be handled by the endpoint.
4361 */
4362 if (htx_is_empty(res_htx)) {
4363 if (!htx_add_endof(res_htx, HTX_BLK_EOT)) {
Willy Tarreaucaff6312022-05-27 10:17:46 +02004364 sc_need_room(sc);
Christopher Faulet08b45cb2022-04-07 09:25:13 +02004365 goto out;
4366 }
4367 channel_add_input(res, 1);
4368 }
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01004369 res_htx->flags |= HTX_FL_EOM;
Christopher Faulet3fa5d192022-03-07 15:52:42 +01004370 res->flags |= CF_EOI;
Willy Tarreaud869e132022-05-17 18:05:31 +02004371 se_fl_set(appctx->sedesc, SE_FL_EOI);
Christopher Faulet3a78aa62019-02-27 16:19:48 +01004372 appctx->st0 = STAT_HTTP_END;
Christopher Fauletef779222018-10-31 08:47:01 +01004373 }
4374
Christopher Faulet3a78aa62019-02-27 16:19:48 +01004375 if (appctx->st0 == STAT_HTTP_END) {
4376 if (!(res->flags & CF_SHUTR)) {
Christopher Fauletef779222018-10-31 08:47:01 +01004377 res->flags |= CF_READ_NULL;
Willy Tarreaucaff6312022-05-27 10:17:46 +02004378 sc_shutr(sc);
Christopher Faulet3a78aa62019-02-27 16:19:48 +01004379 }
4380
4381 /* eat the whole request */
4382 if (co_data(req)) {
4383 req_htx = htx_from_buf(&req->buf);
4384 co_htx_skip(req, req_htx, co_data(req));
4385 htx_to_buf(req_htx, &req->buf);
Christopher Fauletef779222018-10-31 08:47:01 +01004386 }
4387 }
Christopher Faulet3a78aa62019-02-27 16:19:48 +01004388
Christopher Fauletef779222018-10-31 08:47:01 +01004389 out:
4390 /* we have left the request in the buffer for the case where we
4391 * process a POST, and this automatically re-enables activity on
4392 * read. It's better to indicate that we want to stop reading when
4393 * we're sending, so that we know there's at most one direction
4394 * deciding to wake the applet up. It saves it from looping when
4395 * emitting large blocks into small TCP windows.
4396 */
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01004397 htx_to_buf(res_htx, &res->buf);
4398 if (!channel_is_empty(res))
Willy Tarreau75a8f8e2022-05-25 18:12:11 +02004399 applet_wont_consume(appctx);
Christopher Fauletef779222018-10-31 08:47:01 +01004400}
4401
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004402/* Dump all fields from <info> into <out> using the "show info" format (name: value) */
Willy Tarreau83061a82018-07-13 11:56:34 +02004403static int stats_dump_info_fields(struct buffer *out,
Willy Tarreau43241ff2019-10-09 11:27:51 +02004404 const struct field *info, unsigned int flags)
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004405{
4406 int field;
4407
4408 for (field = 0; field < INF_TOTAL_FIELDS; field++) {
4409 if (!field_format(info, field))
4410 continue;
4411
Willy Tarreaueaa55372019-10-09 07:39:11 +02004412 if (!chunk_appendf(out, "%s: ", info_fields[field].name))
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004413 return 0;
4414 if (!stats_emit_raw_data_field(out, &info[field]))
4415 return 0;
Willy Tarreau6b19b142019-10-09 15:44:21 +02004416 if ((flags & STAT_SHOW_FDESC) && !chunk_appendf(out, ":\"%s\"", info_fields[field].desc))
4417 return 0;
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004418 if (!chunk_strcat(out, "\n"))
4419 return 0;
4420 }
4421 return 1;
4422}
4423
4424/* Dump all fields from <info> into <out> using the "show info typed" format */
Willy Tarreau83061a82018-07-13 11:56:34 +02004425static int stats_dump_typed_info_fields(struct buffer *out,
Willy Tarreau43241ff2019-10-09 11:27:51 +02004426 const struct field *info, unsigned int flags)
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004427{
4428 int field;
4429
4430 for (field = 0; field < INF_TOTAL_FIELDS; field++) {
4431 if (!field_format(info, field))
4432 continue;
4433
Willy Tarreaueaa55372019-10-09 07:39:11 +02004434 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 +01004435 return 0;
4436 if (!stats_emit_field_tags(out, &info[field], ':'))
4437 return 0;
4438 if (!stats_emit_typed_data_field(out, &info[field]))
4439 return 0;
Willy Tarreau6b19b142019-10-09 15:44:21 +02004440 if ((flags & STAT_SHOW_FDESC) && !chunk_appendf(out, ":\"%s\"", info_fields[field].desc))
4441 return 0;
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004442 if (!chunk_strcat(out, "\n"))
4443 return 0;
4444 }
4445 return 1;
4446}
4447
Willy Tarreau0b26b382021-05-08 07:43:53 +02004448/* Fill <info> with HAProxy global info. <info> is preallocated array of length
4449 * <len>. The length of the array must be INF_TOTAL_FIELDS. If this length is
4450 * less then this value, the function returns 0, otherwise, it returns 1. Some
4451 * fields' presence or precision may depend on some of the STAT_* flags present
4452 * in <flags>.
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004453 */
Willy Tarreau0b26b382021-05-08 07:43:53 +02004454int stats_fill_info(struct field *info, int len, uint flags)
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004455{
Willy Tarreaud37e26e2021-05-08 07:40:52 +02004456 struct timeval up;
Willy Tarreau83061a82018-07-13 11:56:34 +02004457 struct buffer *out = get_trash_chunk();
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004458
4459#ifdef USE_OPENSSL
Willy Tarreauc5977722021-05-08 08:14:04 +02004460 double ssl_sess_rate = read_freq_ctr_flt(&global.ssl_per_sec);
4461 double ssl_key_rate = read_freq_ctr_flt(&global.ssl_fe_keys_per_sec);
4462 double ssl_reuse = 0;
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004463
Willy Tarreauc5977722021-05-08 08:14:04 +02004464 if (ssl_key_rate < ssl_sess_rate)
4465 ssl_reuse = 100.0 * (1.0 - ssl_key_rate / ssl_sess_rate);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004466#endif
4467
Willy Tarreaud37e26e2021-05-08 07:40:52 +02004468 tv_remain(&start_date, &now, &up);
4469
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004470 if (len < INF_TOTAL_FIELDS)
4471 return 0;
4472
4473 chunk_reset(out);
4474 memset(info, 0, sizeof(*info) * len);
4475
4476 info[INF_NAME] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, PRODUCT_NAME);
Willy Tarreau909b9d82019-01-04 18:20:32 +01004477 info[INF_VERSION] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, haproxy_version);
Adis Nezirovicb62b78b2021-01-15 13:12:33 +01004478 info[INF_BUILD_INFO] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, haproxy_version);
Willy Tarreau909b9d82019-01-04 18:20:32 +01004479 info[INF_RELEASE_DATE] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, haproxy_date);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004480
Yves Lafon95317282018-02-26 11:10:37 +01004481 info[INF_NBTHREAD] = mkf_u32(FO_CONFIG|FS_SERVICE, global.nbthread);
Willy Tarreau91358592021-06-15 08:08:04 +02004482 info[INF_NBPROC] = mkf_u32(FO_CONFIG|FS_SERVICE, 1);
Willy Tarreaue8422bf2021-06-15 09:08:18 +02004483 info[INF_PROCESS_NUM] = mkf_u32(FO_KEY, 1);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004484 info[INF_PID] = mkf_u32(FO_STATUS, pid);
4485
4486 info[INF_UPTIME] = mkf_str(FN_DURATION, chunk_newstr(out));
Willy Tarreaud37e26e2021-05-08 07:40:52 +02004487 chunk_appendf(out, "%ud %uh%02um%02us", (uint)up.tv_sec / 86400, ((uint)up.tv_sec % 86400) / 3600, ((uint)up.tv_sec % 3600) / 60, ((uint)up.tv_sec % 60));
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004488
Willy Tarreaue8abc322021-05-08 07:56:56 +02004489 info[INF_UPTIME_SEC] = (flags & STAT_USE_FLOAT) ? mkf_flt(FN_DURATION, up.tv_sec + up.tv_usec / 1000000.0) : mkf_u32(FN_DURATION, up.tv_sec);
4490 info[INF_START_TIME_SEC] = (flags & STAT_USE_FLOAT) ? mkf_flt(FN_DURATION, start_date.tv_sec + start_date.tv_usec / 1000000.0) : mkf_u32(FN_DURATION, start_date.tv_sec);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004491 info[INF_MEMMAX_MB] = mkf_u32(FO_CONFIG|FN_LIMIT, global.rlimit_memmax);
William Dauchya8766cf2021-01-15 22:41:37 +01004492 info[INF_MEMMAX_BYTES] = mkf_u32(FO_CONFIG|FN_LIMIT, global.rlimit_memmax * 1048576L);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004493 info[INF_POOL_ALLOC_MB] = mkf_u32(0, (unsigned)(pool_total_allocated() / 1048576L));
William Dauchya8766cf2021-01-15 22:41:37 +01004494 info[INF_POOL_ALLOC_BYTES] = mkf_u32(0, pool_total_allocated());
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004495 info[INF_POOL_USED_MB] = mkf_u32(0, (unsigned)(pool_total_used() / 1048576L));
William Dauchya8766cf2021-01-15 22:41:37 +01004496 info[INF_POOL_USED_BYTES] = mkf_u32(0, pool_total_used());
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004497 info[INF_POOL_FAILED] = mkf_u32(FN_COUNTER, pool_total_failures());
4498 info[INF_ULIMIT_N] = mkf_u32(FO_CONFIG|FN_LIMIT, global.rlimit_nofile);
4499 info[INF_MAXSOCK] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxsock);
4500 info[INF_MAXCONN] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxconn);
4501 info[INF_HARD_MAXCONN] = mkf_u32(FO_CONFIG|FN_LIMIT, global.hardmaxconn);
4502 info[INF_CURR_CONN] = mkf_u32(0, actconn);
4503 info[INF_CUM_CONN] = mkf_u32(FN_COUNTER, totalconn);
4504 info[INF_CUM_REQ] = mkf_u32(FN_COUNTER, global.req_count);
4505#ifdef USE_OPENSSL
4506 info[INF_MAX_SSL_CONNS] = mkf_u32(FN_MAX, global.maxsslconn);
Willy Tarreau82531f62021-10-06 12:15:18 +02004507 info[INF_CURR_SSL_CONNS] = mkf_u32(0, global.sslconns);
4508 info[INF_CUM_SSL_CONNS] = mkf_u32(FN_COUNTER, global.totalsslconns);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004509#endif
4510 info[INF_MAXPIPES] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxpipes);
4511 info[INF_PIPES_USED] = mkf_u32(0, pipes_used);
4512 info[INF_PIPES_FREE] = mkf_u32(0, pipes_free);
Willy Tarreauc5977722021-05-08 08:14:04 +02004513 info[INF_CONN_RATE] = (flags & STAT_USE_FLOAT) ? mkf_flt(FN_RATE, read_freq_ctr_flt(&global.conn_per_sec)) : mkf_u32(FN_RATE, read_freq_ctr(&global.conn_per_sec));
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004514 info[INF_CONN_RATE_LIMIT] = mkf_u32(FO_CONFIG|FN_LIMIT, global.cps_lim);
4515 info[INF_MAX_CONN_RATE] = mkf_u32(FN_MAX, global.cps_max);
Willy Tarreauc5977722021-05-08 08:14:04 +02004516 info[INF_SESS_RATE] = (flags & STAT_USE_FLOAT) ? mkf_flt(FN_RATE, read_freq_ctr_flt(&global.sess_per_sec)) : mkf_u32(FN_RATE, read_freq_ctr(&global.sess_per_sec));
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004517 info[INF_SESS_RATE_LIMIT] = mkf_u32(FO_CONFIG|FN_LIMIT, global.sps_lim);
4518 info[INF_MAX_SESS_RATE] = mkf_u32(FN_RATE, global.sps_max);
4519
4520#ifdef USE_OPENSSL
Willy Tarreauc5977722021-05-08 08:14:04 +02004521 info[INF_SSL_RATE] = (flags & STAT_USE_FLOAT) ? mkf_flt(FN_RATE, ssl_sess_rate) : mkf_u32(FN_RATE, ssl_sess_rate);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004522 info[INF_SSL_RATE_LIMIT] = mkf_u32(FO_CONFIG|FN_LIMIT, global.ssl_lim);
4523 info[INF_MAX_SSL_RATE] = mkf_u32(FN_MAX, global.ssl_max);
Willy Tarreauc5977722021-05-08 08:14:04 +02004524 info[INF_SSL_FRONTEND_KEY_RATE] = (flags & STAT_USE_FLOAT) ? mkf_flt(FN_RATE, ssl_key_rate) : mkf_u32(0, ssl_key_rate);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004525 info[INF_SSL_FRONTEND_MAX_KEY_RATE] = mkf_u32(FN_MAX, global.ssl_fe_keys_max);
Willy Tarreauc5977722021-05-08 08:14:04 +02004526 info[INF_SSL_FRONTEND_SESSION_REUSE_PCT] = (flags & STAT_USE_FLOAT) ? mkf_flt(FN_RATE, ssl_reuse) : mkf_u32(0, ssl_reuse);
4527 info[INF_SSL_BACKEND_KEY_RATE] = (flags & STAT_USE_FLOAT) ? mkf_flt(FN_RATE, read_freq_ctr_flt(&global.ssl_be_keys_per_sec)) : mkf_u32(FN_RATE, read_freq_ctr(&global.ssl_be_keys_per_sec));
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004528 info[INF_SSL_BACKEND_MAX_KEY_RATE] = mkf_u32(FN_MAX, global.ssl_be_keys_max);
4529 info[INF_SSL_CACHE_LOOKUPS] = mkf_u32(FN_COUNTER, global.shctx_lookups);
4530 info[INF_SSL_CACHE_MISSES] = mkf_u32(FN_COUNTER, global.shctx_misses);
4531#endif
Willy Tarreauc5977722021-05-08 08:14:04 +02004532 info[INF_COMPRESS_BPS_IN] = (flags & STAT_USE_FLOAT) ? mkf_flt(FN_RATE, read_freq_ctr_flt(&global.comp_bps_in)) : mkf_u32(FN_RATE, read_freq_ctr(&global.comp_bps_in));
4533 info[INF_COMPRESS_BPS_OUT] = (flags & STAT_USE_FLOAT) ? mkf_flt(FN_RATE, read_freq_ctr_flt(&global.comp_bps_out)) : mkf_u32(FN_RATE, read_freq_ctr(&global.comp_bps_out));
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004534 info[INF_COMPRESS_BPS_RATE_LIM] = mkf_u32(FO_CONFIG|FN_LIMIT, global.comp_rate_lim);
4535#ifdef USE_ZLIB
4536 info[INF_ZLIB_MEM_USAGE] = mkf_u32(0, zlib_used_memory);
4537 info[INF_MAX_ZLIB_MEM_USAGE] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxzlibmem);
4538#endif
Willy Tarreau955a11e2021-02-24 17:03:30 +01004539 info[INF_TASKS] = mkf_u32(0, total_allocated_tasks());
Willy Tarreau9c7b8082021-02-24 15:10:07 +01004540 info[INF_RUN_QUEUE] = mkf_u32(0, total_run_queues());
Willy Tarreauf9d5e102021-10-08 10:43:59 +02004541 info[INF_IDLE_PCT] = mkf_u32(FN_AVG, clock_report_idle());
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004542 info[INF_NODE] = mkf_str(FO_CONFIG|FN_OUTPUT|FS_SERVICE, global.node);
4543 if (global.desc)
4544 info[INF_DESCRIPTION] = mkf_str(FO_CONFIG|FN_OUTPUT|FS_SERVICE, global.desc);
Willy Tarreau00098ea2018-11-05 14:38:13 +01004545 info[INF_STOPPING] = mkf_u32(0, stopping);
4546 info[INF_JOBS] = mkf_u32(0, jobs);
William Lallemanda7199262018-11-16 16:57:20 +01004547 info[INF_UNSTOPPABLE_JOBS] = mkf_u32(0, unstoppable_jobs);
Willy Tarreau00098ea2018-11-05 14:38:13 +01004548 info[INF_LISTENERS] = mkf_u32(0, listeners);
Willy Tarreau199ad242018-11-05 16:31:22 +01004549 info[INF_ACTIVE_PEERS] = mkf_u32(0, active_peers);
Willy Tarreau2d372c22018-11-05 17:12:27 +01004550 info[INF_CONNECTED_PEERS] = mkf_u32(0, connected_peers);
Willy Tarreau13ef7732018-11-12 07:25:28 +01004551 info[INF_DROPPED_LOGS] = mkf_u32(0, dropped_logs);
Willy Tarreaubeb859a2018-11-22 18:07:59 +01004552 info[INF_BUSY_POLLING] = mkf_u32(0, !!(global.tune.options & GTUNE_BUSY_POLLING));
Emeric Brund30e9a12020-12-23 18:49:16 +01004553 info[INF_FAILED_RESOLUTIONS] = mkf_u32(0, resolv_failed_resolutions);
Willy Tarreau7cf0e452019-05-23 11:39:14 +02004554 info[INF_TOTAL_BYTES_OUT] = mkf_u64(0, global.out_bytes);
Christopher Fauletaaa70852020-07-10 13:56:30 +02004555 info[INF_TOTAL_SPLICED_BYTES_OUT] = mkf_u64(0, global.spliced_out_bytes);
Willy Tarreau7cf0e452019-05-23 11:39:14 +02004556 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 +02004557 info[INF_DEBUG_COMMANDS_ISSUED] = mkf_u32(0, debug_commands_issued);
Emeric Brun45c457a2020-07-09 23:23:34 +02004558 info[INF_CUM_LOG_MSGS] = mkf_u32(FN_COUNTER, cum_log_messages);
Amaury Denoyelle5dfdf3e2021-05-05 17:09:12 +02004559
4560 info[INF_TAINTED] = mkf_str(FO_STATUS, chunk_newstr(out));
4561 chunk_appendf(out, "%#x", get_tainted());
4562
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004563 return 1;
4564}
4565
Willy Tarreau4596fe22022-05-17 19:07:51 +02004566/* This function dumps information onto the stream connector's read buffer.
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004567 * It returns 0 as long as it does not complete, non-zero upon completion.
4568 * No state is used.
4569 */
Willy Tarreaucaff6312022-05-27 10:17:46 +02004570static int stats_dump_info_to_buffer(struct stconn *sc)
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004571{
Willy Tarreaucaff6312022-05-27 10:17:46 +02004572 struct appctx *appctx = __sc_appctx(sc);
Willy Tarreau91cefca2022-05-03 17:08:29 +02004573 struct show_stat_ctx *ctx = appctx->svcctx;
Christopher Faulet2da02ae2022-02-24 13:45:27 +01004574
Willy Tarreau91cefca2022-05-03 17:08:29 +02004575 if (!stats_fill_info(info, INF_TOTAL_FIELDS, ctx->flags))
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004576 return 0;
4577
4578 chunk_reset(&trash);
4579
Willy Tarreau91cefca2022-05-03 17:08:29 +02004580 if (ctx->flags & STAT_FMT_TYPED)
4581 stats_dump_typed_info_fields(&trash, info, ctx->flags);
4582 else if (ctx->flags & STAT_FMT_JSON)
4583 stats_dump_json_info_fields(&trash, info, ctx->flags);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004584 else
Willy Tarreau91cefca2022-05-03 17:08:29 +02004585 stats_dump_info_fields(&trash, info, ctx->flags);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004586
Willy Tarreaud0a06d52022-05-18 15:07:19 +02004587 if (applet_putchk(appctx, &trash) == -1)
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004588 return 0;
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004589
4590 return 1;
4591}
4592
Willy Tarreau4596fe22022-05-17 19:07:51 +02004593/* This function dumps the schema onto the stream connector's read buffer.
Simon Horman6f6bb382017-01-04 09:37:26 +01004594 * It returns 0 as long as it does not complete, non-zero upon completion.
4595 * No state is used.
4596 *
Ilya Shipitsinf38a0182020-12-21 01:16:17 +05004597 * Integer values bounded to the range [-(2**53)+1, (2**53)-1] as
Simon Horman6f6bb382017-01-04 09:37:26 +01004598 * per the recommendation for interoperable integers in section 6 of RFC 7159.
4599 */
Willy Tarreau83061a82018-07-13 11:56:34 +02004600static void stats_dump_json_schema(struct buffer *out)
Simon Horman6f6bb382017-01-04 09:37:26 +01004601{
4602
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004603 int old_len = out->data;
Simon Horman6f6bb382017-01-04 09:37:26 +01004604
4605 chunk_strcat(out,
4606 "{"
4607 "\"$schema\":\"http://json-schema.org/draft-04/schema#\","
4608 "\"oneOf\":["
4609 "{"
4610 "\"title\":\"Info\","
4611 "\"type\":\"array\","
4612 "\"items\":{"
Amaury Denoyellea53ce4c2020-10-02 18:31:59 +02004613 "\"title\":\"InfoItem\","
4614 "\"type\":\"object\","
Simon Horman6f6bb382017-01-04 09:37:26 +01004615 "\"properties\":{"
Simon Horman6f6bb382017-01-04 09:37:26 +01004616 "\"field\":{\"$ref\":\"#/definitions/field\"},"
4617 "\"processNum\":{\"$ref\":\"#/definitions/processNum\"},"
4618 "\"tags\":{\"$ref\":\"#/definitions/tags\"},"
4619 "\"value\":{\"$ref\":\"#/definitions/typedValue\"}"
4620 "},"
4621 "\"required\":[\"field\",\"processNum\",\"tags\","
4622 "\"value\"]"
4623 "}"
4624 "},"
4625 "{"
4626 "\"title\":\"Stat\","
4627 "\"type\":\"array\","
4628 "\"items\":{"
4629 "\"title\":\"InfoItem\","
4630 "\"type\":\"object\","
4631 "\"properties\":{"
4632 "\"objType\":{"
4633 "\"enum\":[\"Frontend\",\"Backend\",\"Listener\","
4634 "\"Server\",\"Unknown\"]"
4635 "},"
4636 "\"proxyId\":{"
4637 "\"type\":\"integer\","
4638 "\"minimum\":0"
4639 "},"
4640 "\"id\":{"
4641 "\"type\":\"integer\","
4642 "\"minimum\":0"
4643 "},"
4644 "\"field\":{\"$ref\":\"#/definitions/field\"},"
4645 "\"processNum\":{\"$ref\":\"#/definitions/processNum\"},"
4646 "\"tags\":{\"$ref\":\"#/definitions/tags\"},"
4647 "\"typedValue\":{\"$ref\":\"#/definitions/typedValue\"}"
4648 "},"
4649 "\"required\":[\"objType\",\"proxyId\",\"id\","
4650 "\"field\",\"processNum\",\"tags\","
4651 "\"value\"]"
4652 "}"
4653 "},"
4654 "{"
4655 "\"title\":\"Error\","
4656 "\"type\":\"object\","
4657 "\"properties\":{"
4658 "\"errorStr\":{"
4659 "\"type\":\"string\""
Amaury Denoyellea53ce4c2020-10-02 18:31:59 +02004660 "}"
4661 "},"
4662 "\"required\":[\"errorStr\"]"
Simon Horman6f6bb382017-01-04 09:37:26 +01004663 "}"
4664 "],"
4665 "\"definitions\":{"
4666 "\"field\":{"
4667 "\"type\":\"object\","
4668 "\"pos\":{"
4669 "\"type\":\"integer\","
4670 "\"minimum\":0"
4671 "},"
4672 "\"name\":{"
4673 "\"type\":\"string\""
4674 "},"
4675 "\"required\":[\"pos\",\"name\"]"
4676 "},"
4677 "\"processNum\":{"
4678 "\"type\":\"integer\","
4679 "\"minimum\":1"
4680 "},"
4681 "\"tags\":{"
4682 "\"type\":\"object\","
4683 "\"origin\":{"
4684 "\"type\":\"string\","
4685 "\"enum\":[\"Metric\",\"Status\",\"Key\","
4686 "\"Config\",\"Product\",\"Unknown\"]"
4687 "},"
4688 "\"nature\":{"
4689 "\"type\":\"string\","
4690 "\"enum\":[\"Gauge\",\"Limit\",\"Min\",\"Max\","
4691 "\"Rate\",\"Counter\",\"Duration\","
4692 "\"Age\",\"Time\",\"Name\",\"Output\","
4693 "\"Avg\", \"Unknown\"]"
4694 "},"
4695 "\"scope\":{"
4696 "\"type\":\"string\","
4697 "\"enum\":[\"Cluster\",\"Process\",\"Service\","
4698 "\"System\",\"Unknown\"]"
4699 "},"
4700 "\"required\":[\"origin\",\"nature\",\"scope\"]"
4701 "},"
4702 "\"typedValue\":{"
4703 "\"type\":\"object\","
4704 "\"oneOf\":["
4705 "{\"$ref\":\"#/definitions/typedValue/definitions/s32Value\"},"
4706 "{\"$ref\":\"#/definitions/typedValue/definitions/s64Value\"},"
4707 "{\"$ref\":\"#/definitions/typedValue/definitions/u32Value\"},"
4708 "{\"$ref\":\"#/definitions/typedValue/definitions/u64Value\"},"
4709 "{\"$ref\":\"#/definitions/typedValue/definitions/strValue\"}"
4710 "],"
4711 "\"definitions\":{"
4712 "\"s32Value\":{"
4713 "\"properties\":{"
4714 "\"type\":{"
4715 "\"type\":\"string\","
4716 "\"enum\":[\"s32\"]"
4717 "},"
4718 "\"value\":{"
4719 "\"type\":\"integer\","
4720 "\"minimum\":-2147483648,"
4721 "\"maximum\":2147483647"
4722 "}"
4723 "},"
4724 "\"required\":[\"type\",\"value\"]"
4725 "},"
4726 "\"s64Value\":{"
4727 "\"properties\":{"
4728 "\"type\":{"
4729 "\"type\":\"string\","
4730 "\"enum\":[\"s64\"]"
4731 "},"
4732 "\"value\":{"
4733 "\"type\":\"integer\","
4734 "\"minimum\":-9007199254740991,"
4735 "\"maximum\":9007199254740991"
4736 "}"
4737 "},"
4738 "\"required\":[\"type\",\"value\"]"
4739 "},"
4740 "\"u32Value\":{"
4741 "\"properties\":{"
4742 "\"type\":{"
4743 "\"type\":\"string\","
4744 "\"enum\":[\"u32\"]"
4745 "},"
4746 "\"value\":{"
4747 "\"type\":\"integer\","
4748 "\"minimum\":0,"
4749 "\"maximum\":4294967295"
4750 "}"
4751 "},"
4752 "\"required\":[\"type\",\"value\"]"
4753 "},"
4754 "\"u64Value\":{"
4755 "\"properties\":{"
4756 "\"type\":{"
4757 "\"type\":\"string\","
4758 "\"enum\":[\"u64\"]"
4759 "},"
4760 "\"value\":{"
4761 "\"type\":\"integer\","
4762 "\"minimum\":0,"
4763 "\"maximum\":9007199254740991"
4764 "}"
4765 "},"
4766 "\"required\":[\"type\",\"value\"]"
4767 "},"
4768 "\"strValue\":{"
4769 "\"properties\":{"
4770 "\"type\":{"
4771 "\"type\":\"string\","
4772 "\"enum\":[\"str\"]"
4773 "},"
4774 "\"value\":{\"type\":\"string\"}"
4775 "},"
4776 "\"required\":[\"type\",\"value\"]"
4777 "},"
4778 "\"unknownValue\":{"
4779 "\"properties\":{"
4780 "\"type\":{"
4781 "\"type\":\"integer\","
4782 "\"minimum\":0"
4783 "},"
4784 "\"value\":{"
4785 "\"type\":\"string\","
4786 "\"enum\":[\"unknown\"]"
4787 "}"
4788 "},"
4789 "\"required\":[\"type\",\"value\"]"
4790 "}"
4791 "}"
4792 "}"
4793 "}"
4794 "}");
4795
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004796 if (old_len == out->data) {
Simon Horman6f6bb382017-01-04 09:37:26 +01004797 chunk_reset(out);
4798 chunk_appendf(out,
4799 "{\"errorStr\":\"output buffer too short\"}");
4800 }
Willy Tarreau5a0e7ca2022-06-10 09:21:22 +02004801 chunk_appendf(out, "\n");
Simon Horman6f6bb382017-01-04 09:37:26 +01004802}
4803
Willy Tarreau4596fe22022-05-17 19:07:51 +02004804/* This function dumps the schema onto the stream connector's read buffer.
Simon Horman6f6bb382017-01-04 09:37:26 +01004805 * It returns 0 as long as it does not complete, non-zero upon completion.
4806 * No state is used.
4807 */
Willy Tarreaud0a06d52022-05-18 15:07:19 +02004808static int stats_dump_json_schema_to_buffer(struct appctx *appctx)
Simon Horman6f6bb382017-01-04 09:37:26 +01004809{
4810 chunk_reset(&trash);
4811
4812 stats_dump_json_schema(&trash);
4813
Willy Tarreaud0a06d52022-05-18 15:07:19 +02004814 if (applet_putchk(appctx, &trash) == -1)
Simon Horman6f6bb382017-01-04 09:37:26 +01004815 return 0;
Simon Horman6f6bb382017-01-04 09:37:26 +01004816
4817 return 1;
4818}
4819
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004820static int cli_parse_clear_counters(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau89d467c2016-11-23 11:02:40 +01004821{
4822 struct proxy *px;
4823 struct server *sv;
4824 struct listener *li;
Amaury Denoyelled3700a72020-10-05 11:49:43 +02004825 struct stats_module *mod;
Willy Tarreau89d467c2016-11-23 11:02:40 +01004826 int clrall = 0;
4827
4828 if (strcmp(args[2], "all") == 0)
4829 clrall = 1;
4830
4831 /* check permissions */
4832 if (!cli_has_level(appctx, ACCESS_LVL_OPER) ||
4833 (clrall && !cli_has_level(appctx, ACCESS_LVL_ADMIN)))
4834 return 1;
4835
Olivier Houchardfbc74e82017-11-24 16:54:05 +01004836 for (px = proxies_list; px; px = px->next) {
Willy Tarreau89d467c2016-11-23 11:02:40 +01004837 if (clrall) {
4838 memset(&px->be_counters, 0, sizeof(px->be_counters));
4839 memset(&px->fe_counters, 0, sizeof(px->fe_counters));
4840 }
4841 else {
4842 px->be_counters.conn_max = 0;
4843 px->be_counters.p.http.rps_max = 0;
4844 px->be_counters.sps_max = 0;
4845 px->be_counters.cps_max = 0;
4846 px->be_counters.nbpend_max = 0;
Christopher Fauletefb41f02019-11-08 14:53:15 +01004847 px->be_counters.qtime_max = 0;
4848 px->be_counters.ctime_max = 0;
4849 px->be_counters.dtime_max = 0;
4850 px->be_counters.ttime_max = 0;
Willy Tarreau89d467c2016-11-23 11:02:40 +01004851
4852 px->fe_counters.conn_max = 0;
4853 px->fe_counters.p.http.rps_max = 0;
4854 px->fe_counters.sps_max = 0;
4855 px->fe_counters.cps_max = 0;
Willy Tarreau89d467c2016-11-23 11:02:40 +01004856 }
4857
4858 for (sv = px->srv; sv; sv = sv->next)
4859 if (clrall)
4860 memset(&sv->counters, 0, sizeof(sv->counters));
4861 else {
4862 sv->counters.cur_sess_max = 0;
4863 sv->counters.nbpend_max = 0;
4864 sv->counters.sps_max = 0;
Christopher Fauletefb41f02019-11-08 14:53:15 +01004865 sv->counters.qtime_max = 0;
4866 sv->counters.ctime_max = 0;
4867 sv->counters.dtime_max = 0;
4868 sv->counters.ttime_max = 0;
Willy Tarreau89d467c2016-11-23 11:02:40 +01004869 }
4870
4871 list_for_each_entry(li, &px->conf.listeners, by_fe)
4872 if (li->counters) {
4873 if (clrall)
4874 memset(li->counters, 0, sizeof(*li->counters));
4875 else
4876 li->counters->conn_max = 0;
4877 }
4878 }
4879
4880 global.cps_max = 0;
4881 global.sps_max = 0;
Olivier Houchard00bc3cb2017-10-17 19:23:25 +02004882 global.ssl_max = 0;
4883 global.ssl_fe_keys_max = 0;
4884 global.ssl_be_keys_max = 0;
Willy Tarreaud80cb4e2018-01-20 19:30:13 +01004885
Amaury Denoyelled3700a72020-10-05 11:49:43 +02004886 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
4887 if (!mod->clearable && !clrall)
4888 continue;
4889
4890 for (px = proxies_list; px; px = px->next) {
4891 enum stats_domain_px_cap mod_cap = stats_px_get_cap(mod->domain_flags);
4892
4893 if (px->cap & PR_CAP_FE && mod_cap & STATS_PX_CAP_FE) {
4894 EXTRA_COUNTERS_INIT(px->extra_counters_fe,
4895 mod,
4896 mod->counters,
4897 mod->counters_size);
4898 }
4899
4900 if (px->cap & PR_CAP_BE && mod_cap & STATS_PX_CAP_BE) {
4901 EXTRA_COUNTERS_INIT(px->extra_counters_be,
4902 mod,
4903 mod->counters,
4904 mod->counters_size);
4905 }
4906
4907 if (mod_cap & STATS_PX_CAP_SRV) {
4908 for (sv = px->srv; sv; sv = sv->next) {
4909 EXTRA_COUNTERS_INIT(sv->extra_counters,
4910 mod,
4911 mod->counters,
4912 mod->counters_size);
4913 }
4914 }
4915
4916 if (mod_cap & STATS_PX_CAP_LI) {
4917 list_for_each_entry(li, &px->conf.listeners, by_fe) {
4918 EXTRA_COUNTERS_INIT(li->extra_counters,
4919 mod,
4920 mod->counters,
4921 mod->counters_size);
4922 }
4923 }
4924 }
4925 }
4926
Emeric Brunf8642ee2021-10-29 17:59:18 +02004927 resolv_stats_clear_counters(clrall, &stats_module_list[STATS_DOMAIN_RESOLVERS]);
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02004928
Willy Tarreaud80cb4e2018-01-20 19:30:13 +01004929 memset(activity, 0, sizeof(activity));
Willy Tarreau89d467c2016-11-23 11:02:40 +01004930 return 1;
4931}
4932
4933
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004934static int cli_parse_show_info(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004935{
Willy Tarreau91cefca2022-05-03 17:08:29 +02004936 struct show_stat_ctx *ctx = applet_reserve_svcctx(appctx, sizeof(*ctx));
Willy Tarreau2f397382019-10-09 11:43:59 +02004937 int arg = 2;
4938
Willy Tarreau91cefca2022-05-03 17:08:29 +02004939 ctx->scope_str = 0;
4940 ctx->scope_len = 0;
4941 ctx->flags = 0;
Willy Tarreaud25fc792016-12-16 12:33:47 +01004942
Willy Tarreau2f397382019-10-09 11:43:59 +02004943 while (*args[arg]) {
4944 if (strcmp(args[arg], "typed") == 0)
Willy Tarreau91cefca2022-05-03 17:08:29 +02004945 ctx->flags = (ctx->flags & ~STAT_FMT_MASK) | STAT_FMT_TYPED;
Willy Tarreau2f397382019-10-09 11:43:59 +02004946 else if (strcmp(args[arg], "json") == 0)
Willy Tarreau91cefca2022-05-03 17:08:29 +02004947 ctx->flags = (ctx->flags & ~STAT_FMT_MASK) | STAT_FMT_JSON;
Willy Tarreau2f397382019-10-09 11:43:59 +02004948 else if (strcmp(args[arg], "desc") == 0)
Willy Tarreau91cefca2022-05-03 17:08:29 +02004949 ctx->flags |= STAT_SHOW_FDESC;
Willy Tarreau27456202021-05-08 07:54:24 +02004950 else if (strcmp(args[arg], "float") == 0)
Willy Tarreau91cefca2022-05-03 17:08:29 +02004951 ctx->flags |= STAT_USE_FLOAT;
Willy Tarreau2f397382019-10-09 11:43:59 +02004952 arg++;
4953 }
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004954 return 0;
4955}
4956
4957
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004958static int cli_parse_show_stat(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau2b812e22016-11-22 16:18:05 +01004959{
Willy Tarreau91cefca2022-05-03 17:08:29 +02004960 struct show_stat_ctx *ctx = applet_reserve_svcctx(appctx, sizeof(*ctx));
Willy Tarreau2f397382019-10-09 11:43:59 +02004961 int arg = 2;
4962
Willy Tarreau91cefca2022-05-03 17:08:29 +02004963 ctx->scope_str = 0;
4964 ctx->scope_len = 0;
4965 ctx->flags = STAT_SHNODE | STAT_SHDESC;
Willy Tarreau578d6e42019-10-09 11:00:22 +02004966
Willy Tarreau0698c802022-05-11 14:09:57 +02004967 if ((strm_li(appctx_strm(appctx))->bind_conf->level & ACCESS_LVL_MASK) >= ACCESS_LVL_OPER)
Willy Tarreau91cefca2022-05-03 17:08:29 +02004968 ctx->flags |= STAT_SHLGNDS;
Willy Tarreaud25fc792016-12-16 12:33:47 +01004969
Amaury Denoyelle072f97e2020-10-05 11:49:37 +02004970 /* proxy is the default domain */
Willy Tarreau91cefca2022-05-03 17:08:29 +02004971 ctx->domain = STATS_DOMAIN_PROXY;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01004972 if (strcmp(args[arg], "domain") == 0) {
Amaury Denoyelle072f97e2020-10-05 11:49:37 +02004973 ++args;
4974
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01004975 if (strcmp(args[arg], "proxy") == 0) {
Amaury Denoyelle072f97e2020-10-05 11:49:37 +02004976 ++args;
Emeric Brunf8642ee2021-10-29 17:59:18 +02004977 } else if (strcmp(args[arg], "resolvers") == 0) {
Willy Tarreau91cefca2022-05-03 17:08:29 +02004978 ctx->domain = STATS_DOMAIN_RESOLVERS;
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02004979 ++args;
4980 } else {
Amaury Denoyelle072f97e2020-10-05 11:49:37 +02004981 return cli_err(appctx, "Invalid statistics domain.\n");
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02004982 }
Amaury Denoyelle072f97e2020-10-05 11:49:37 +02004983 }
4984
Willy Tarreau91cefca2022-05-03 17:08:29 +02004985 if (ctx->domain == STATS_DOMAIN_PROXY
Amaury Denoyelle072f97e2020-10-05 11:49:37 +02004986 && *args[arg] && *args[arg+1] && *args[arg+2]) {
Willy Tarreaua1b1ed52016-11-25 08:50:58 +01004987 struct proxy *px;
4988
Willy Tarreau2f397382019-10-09 11:43:59 +02004989 px = proxy_find_by_name(args[arg], 0, 0);
Willy Tarreaua1b1ed52016-11-25 08:50:58 +01004990 if (px)
Willy Tarreau91cefca2022-05-03 17:08:29 +02004991 ctx->iid = px->uuid;
Willy Tarreaua1b1ed52016-11-25 08:50:58 +01004992 else
Willy Tarreau91cefca2022-05-03 17:08:29 +02004993 ctx->iid = atoi(args[arg]);
Willy Tarreaua1b1ed52016-11-25 08:50:58 +01004994
Willy Tarreau91cefca2022-05-03 17:08:29 +02004995 if (!ctx->iid)
Willy Tarreau9d008692019-08-09 11:21:01 +02004996 return cli_err(appctx, "No such proxy.\n");
Willy Tarreaua1b1ed52016-11-25 08:50:58 +01004997
Willy Tarreau91cefca2022-05-03 17:08:29 +02004998 ctx->flags |= STAT_BOUND;
4999 ctx->type = atoi(args[arg+1]);
5000 ctx->sid = atoi(args[arg+2]);
Willy Tarreau2f397382019-10-09 11:43:59 +02005001 arg += 3;
5002 }
5003
5004 while (*args[arg]) {
5005 if (strcmp(args[arg], "typed") == 0)
Willy Tarreau91cefca2022-05-03 17:08:29 +02005006 ctx->flags = (ctx->flags & ~STAT_FMT_MASK) | STAT_FMT_TYPED;
Willy Tarreau2f397382019-10-09 11:43:59 +02005007 else if (strcmp(args[arg], "json") == 0)
Willy Tarreau91cefca2022-05-03 17:08:29 +02005008 ctx->flags = (ctx->flags & ~STAT_FMT_MASK) | STAT_FMT_JSON;
Willy Tarreau2f397382019-10-09 11:43:59 +02005009 else if (strcmp(args[arg], "desc") == 0)
Willy Tarreau91cefca2022-05-03 17:08:29 +02005010 ctx->flags |= STAT_SHOW_FDESC;
Willy Tarreau3e320362020-10-23 17:28:57 +02005011 else if (strcmp(args[arg], "no-maint") == 0)
Willy Tarreau91cefca2022-05-03 17:08:29 +02005012 ctx->flags |= STAT_HIDE_MAINT;
Willy Tarreau65141ff2020-10-23 17:19:48 +02005013 else if (strcmp(args[arg], "up") == 0)
Willy Tarreau91cefca2022-05-03 17:08:29 +02005014 ctx->flags |= STAT_HIDE_DOWN;
Willy Tarreau2f397382019-10-09 11:43:59 +02005015 arg++;
Willy Tarreau2b812e22016-11-22 16:18:05 +01005016 }
Willy Tarreau2b812e22016-11-22 16:18:05 +01005017
Willy Tarreau2b812e22016-11-22 16:18:05 +01005018 return 0;
5019}
5020
Willy Tarreau0baac8c2016-11-22 16:36:53 +01005021static int cli_io_handler_dump_info(struct appctx *appctx)
5022{
Willy Tarreauc12b3212022-05-27 11:08:15 +02005023 return stats_dump_info_to_buffer(appctx_sc(appctx));
Willy Tarreau0baac8c2016-11-22 16:36:53 +01005024}
5025
Willy Tarreau4596fe22022-05-17 19:07:51 +02005026/* This I/O handler runs as an applet embedded in a stream connector. It is
Willy Tarreau2b812e22016-11-22 16:18:05 +01005027 * used to send raw stats over a socket.
5028 */
5029static int cli_io_handler_dump_stat(struct appctx *appctx)
5030{
Willy Tarreauc12b3212022-05-27 11:08:15 +02005031 return stats_dump_stat_to_buffer(appctx_sc(appctx), NULL, NULL);
Willy Tarreau2b812e22016-11-22 16:18:05 +01005032}
5033
Simon Horman6f6bb382017-01-04 09:37:26 +01005034static int cli_io_handler_dump_json_schema(struct appctx *appctx)
5035{
Willy Tarreaud0a06d52022-05-18 15:07:19 +02005036 return stats_dump_json_schema_to_buffer(appctx);
Simon Horman6f6bb382017-01-04 09:37:26 +01005037}
5038
Amaury Denoyelle216a1ce2021-03-18 15:48:14 +01005039int stats_allocate_proxy_counters_internal(struct extra_counters **counters,
5040 int type, int px_cap)
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02005041{
5042 struct stats_module *mod;
5043
5044 EXTRA_COUNTERS_REGISTER(counters, type, alloc_failed);
5045
5046 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
5047 if (!(stats_px_get_cap(mod->domain_flags) & px_cap))
5048 continue;
5049
5050 EXTRA_COUNTERS_ADD(mod, *counters, mod->counters, mod->counters_size);
5051 }
5052
5053 EXTRA_COUNTERS_ALLOC(*counters, alloc_failed);
5054
5055 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
5056 if (!(stats_px_get_cap(mod->domain_flags) & px_cap))
5057 continue;
5058
5059 EXTRA_COUNTERS_INIT(*counters, mod, mod->counters, mod->counters_size);
5060 }
5061
5062 return 1;
5063
5064 alloc_failed:
5065 return 0;
5066}
5067
5068/* Initialize and allocate all extra counters for a proxy and its attached
5069 * servers/listeners with all already registered stats module
5070 */
5071int stats_allocate_proxy_counters(struct proxy *px)
5072{
5073 struct server *sv;
5074 struct listener *li;
5075
5076 if (px->cap & PR_CAP_FE) {
5077 if (!stats_allocate_proxy_counters_internal(&px->extra_counters_fe,
5078 COUNTERS_FE,
5079 STATS_PX_CAP_FE)) {
5080 return 0;
5081 }
5082 }
5083
5084 if (px->cap & PR_CAP_BE) {
5085 if (!stats_allocate_proxy_counters_internal(&px->extra_counters_be,
5086 COUNTERS_BE,
5087 STATS_PX_CAP_BE)) {
5088 return 0;
5089 }
5090 }
5091
5092 for (sv = px->srv; sv; sv = sv->next) {
5093 if (!stats_allocate_proxy_counters_internal(&sv->extra_counters,
5094 COUNTERS_SV,
5095 STATS_PX_CAP_SRV)) {
5096 return 0;
5097 }
5098 }
5099
5100 list_for_each_entry(li, &px->conf.listeners, by_fe) {
5101 if (!stats_allocate_proxy_counters_internal(&li->extra_counters,
5102 COUNTERS_LI,
5103 STATS_PX_CAP_LI)) {
5104 return 0;
5105 }
5106 }
5107
5108 return 1;
5109}
5110
Amaury Denoyelle58d395e2020-10-05 11:49:40 +02005111void stats_register_module(struct stats_module *m)
5112{
5113 const uint8_t domain = stats_get_domain(m->domain_flags);
5114
Willy Tarreau2b718102021-04-21 07:32:39 +02005115 LIST_APPEND(&stats_module_list[domain], &m->list);
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02005116 stat_count[domain] += m->stats_count;
Amaury Denoyelle58d395e2020-10-05 11:49:40 +02005117}
5118
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02005119static int allocate_stats_px_postcheck(void)
5120{
5121 struct stats_module *mod;
5122 size_t i = ST_F_TOTAL_FIELDS;
5123 int err_code = 0;
Amaury Denoyelle27373f72020-10-05 16:57:33 +02005124 struct proxy *px;
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02005125
5126 stat_count[STATS_DOMAIN_PROXY] += ST_F_TOTAL_FIELDS;
5127
5128 stat_f[STATS_DOMAIN_PROXY] = malloc(stat_count[STATS_DOMAIN_PROXY] * sizeof(struct name_desc));
5129 if (!stat_f[STATS_DOMAIN_PROXY]) {
5130 ha_alert("stats: cannot allocate all fields for proxy statistics\n");
5131 err_code |= ERR_ALERT | ERR_FATAL;
5132 return err_code;
5133 }
5134
5135 memcpy(stat_f[STATS_DOMAIN_PROXY], stat_fields,
5136 ST_F_TOTAL_FIELDS * sizeof(struct name_desc));
5137
5138 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
5139 memcpy(stat_f[STATS_DOMAIN_PROXY] + i,
5140 mod->stats,
5141 mod->stats_count * sizeof(struct name_desc));
5142 i += mod->stats_count;
5143 }
5144
Amaury Denoyelle27373f72020-10-05 16:57:33 +02005145 for (px = proxies_list; px; px = px->next) {
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02005146 if (!stats_allocate_proxy_counters(px)) {
5147 ha_alert("stats: cannot allocate all counters for proxy statistics\n");
5148 err_code |= ERR_ALERT | ERR_FATAL;
5149 return err_code;
5150 }
5151 }
5152
Christopher Fauletde79cd22021-01-06 07:41:56 +01005153 /* wait per-thread alloc to perform corresponding stat_l allocation */
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02005154
5155 return err_code;
5156}
5157
5158REGISTER_CONFIG_POSTPARSER("allocate-stats-px", allocate_stats_px_postcheck);
5159
Emeric Brunf8642ee2021-10-29 17:59:18 +02005160static int allocate_stats_rslv_postcheck(void)
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02005161{
5162 struct stats_module *mod;
5163 size_t i = 0;
5164 int err_code = 0;
5165
Emeric Brunf8642ee2021-10-29 17:59:18 +02005166 stat_f[STATS_DOMAIN_RESOLVERS] = malloc(stat_count[STATS_DOMAIN_RESOLVERS] * sizeof(struct name_desc));
5167 if (!stat_f[STATS_DOMAIN_RESOLVERS]) {
5168 ha_alert("stats: cannot allocate all fields for resolver statistics\n");
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02005169 err_code |= ERR_ALERT | ERR_FATAL;
5170 return err_code;
5171 }
5172
Emeric Brunf8642ee2021-10-29 17:59:18 +02005173 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_RESOLVERS], list) {
5174 memcpy(stat_f[STATS_DOMAIN_RESOLVERS] + i,
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02005175 mod->stats,
5176 mod->stats_count * sizeof(struct name_desc));
5177 i += mod->stats_count;
5178 }
5179
Emeric Brunf8642ee2021-10-29 17:59:18 +02005180 if (!resolv_allocate_counters(&stats_module_list[STATS_DOMAIN_RESOLVERS])) {
5181 ha_alert("stats: cannot allocate all counters for resolver statistics\n");
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02005182 err_code |= ERR_ALERT | ERR_FATAL;
5183 return err_code;
5184 }
5185
Christopher Fauletde79cd22021-01-06 07:41:56 +01005186 /* wait per-thread alloc to perform corresponding stat_l allocation */
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02005187
5188 return err_code;
5189}
5190
Emeric Brunf8642ee2021-10-29 17:59:18 +02005191REGISTER_CONFIG_POSTPARSER("allocate-stats-resolver", allocate_stats_rslv_postcheck);
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02005192
Christopher Fauletde79cd22021-01-06 07:41:56 +01005193static int allocate_stat_lines_per_thread(void)
5194{
Emeric Brunf8642ee2021-10-29 17:59:18 +02005195 int domains[] = { STATS_DOMAIN_PROXY, STATS_DOMAIN_RESOLVERS }, i;
Christopher Fauletde79cd22021-01-06 07:41:56 +01005196
5197 for (i = 0; i < STATS_DOMAIN_COUNT; ++i) {
5198 const int domain = domains[i];
5199
5200 stat_l[domain] = malloc(stat_count[domain] * sizeof(struct field));
5201 if (!stat_l[domain])
5202 return 0;
5203 }
5204 return 1;
5205}
5206
5207REGISTER_PER_THREAD_ALLOC(allocate_stat_lines_per_thread);
5208
Amaury Denoyelle7f8f6cb2020-11-10 14:24:31 +01005209static int allocate_trash_counters(void)
5210{
5211 struct stats_module *mod;
Emeric Brunf8642ee2021-10-29 17:59:18 +02005212 int domains[] = { STATS_DOMAIN_PROXY, STATS_DOMAIN_RESOLVERS }, i;
Amaury Denoyelle7f8f6cb2020-11-10 14:24:31 +01005213 size_t max_counters_size = 0;
5214
5215 /* calculate the greatest counters used by any stats modules */
5216 for (i = 0; i < STATS_DOMAIN_COUNT; ++i) {
5217 list_for_each_entry(mod, &stats_module_list[domains[i]], list) {
5218 max_counters_size = mod->counters_size > max_counters_size ?
5219 mod->counters_size : max_counters_size;
5220 }
5221 }
5222
5223 /* allocate the trash with the size of the greatest counters */
5224 if (max_counters_size) {
5225 trash_counters = malloc(max_counters_size);
5226 if (!trash_counters) {
5227 ha_alert("stats: cannot allocate trash counters for statistics\n");
5228 return 0;
5229 }
5230 }
5231
5232 return 1;
5233}
5234
5235REGISTER_PER_THREAD_ALLOC(allocate_trash_counters);
5236
Christopher Fauletde79cd22021-01-06 07:41:56 +01005237static void deinit_stat_lines_per_thread(void)
Amaury Denoyellea2a68992020-11-10 14:24:30 +01005238{
Emeric Brunf8642ee2021-10-29 17:59:18 +02005239 int domains[] = { STATS_DOMAIN_PROXY, STATS_DOMAIN_RESOLVERS }, i;
Amaury Denoyellea2a68992020-11-10 14:24:30 +01005240
5241 for (i = 0; i < STATS_DOMAIN_COUNT; ++i) {
5242 const int domain = domains[i];
5243
Willy Tarreau61cfdf42021-02-20 10:46:51 +01005244 ha_free(&stat_l[domain]);
Christopher Fauletde79cd22021-01-06 07:41:56 +01005245 }
5246}
5247
5248
5249REGISTER_PER_THREAD_FREE(deinit_stat_lines_per_thread);
5250
5251static void deinit_stats(void)
5252{
Emeric Brunf8642ee2021-10-29 17:59:18 +02005253 int domains[] = { STATS_DOMAIN_PROXY, STATS_DOMAIN_RESOLVERS }, i;
Christopher Fauletde79cd22021-01-06 07:41:56 +01005254
5255 for (i = 0; i < STATS_DOMAIN_COUNT; ++i) {
5256 const int domain = domains[i];
Amaury Denoyellea2a68992020-11-10 14:24:30 +01005257
5258 if (stat_f[domain])
5259 free(stat_f[domain]);
5260 }
5261}
5262
5263REGISTER_POST_DEINIT(deinit_stats);
5264
Amaury Denoyelle7f8f6cb2020-11-10 14:24:31 +01005265static void free_trash_counters(void)
5266{
5267 if (trash_counters)
5268 free(trash_counters);
5269}
5270
5271REGISTER_PER_THREAD_FREE(free_trash_counters);
5272
Willy Tarreau2b812e22016-11-22 16:18:05 +01005273/* register cli keywords */
5274static struct cli_kw_list cli_kws = {{ },{
Willy Tarreaub205bfd2021-05-07 11:38:37 +02005275 { { "clear", "counters", NULL }, "clear counters [all] : clear max statistics counters (or all counters)", cli_parse_clear_counters, NULL, NULL },
Willy Tarreau27456202021-05-08 07:54:24 +02005276 { { "show", "info", NULL }, "show info [desc|json|typed|float]* : report information about the running process", cli_parse_show_info, cli_io_handler_dump_info, NULL },
Willy Tarreaub205bfd2021-05-07 11:38:37 +02005277 { { "show", "stat", NULL }, "show stat [desc|json|no-maint|typed|up]*: report counters for each proxy and server", cli_parse_show_stat, cli_io_handler_dump_stat, NULL },
5278 { { "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 +01005279 {{},}
5280}};
5281
Willy Tarreau0108d902018-11-25 19:14:37 +01005282INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
5283
William Lallemand74c24fb2016-11-21 17:18:36 +01005284struct applet http_stats_applet = {
5285 .obj_type = OBJ_TYPE_APPLET,
5286 .name = "<STATS>", /* used for logging */
5287 .fct = http_stats_io_handler,
5288 .release = NULL,
5289};
5290
William Lallemand74c24fb2016-11-21 17:18:36 +01005291/*
5292 * Local variables:
5293 * c-indent-level: 8
5294 * c-basic-offset: 8
5295 * End:
5296 */