blob: 1d071e2c132d27ab848b2ef086d4431e20418ad7 [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" },
Tim Duesterhus0ababda2023-03-27 15:23:44 +0200153 [INF_TOTAL_SPLICED_BYTES_OUT] = { .name = "TotalSplicedBytesOut", .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 Tarreau3c4a2972023-05-11 12:02:21 +0200159 [INF_WARNINGS] = { .name = "TotalWarnings", .desc = "Total warnings issued" },
Willy Tarreau96151022023-05-11 13:51:31 +0200160 [INF_MAXCONN_REACHED] = { .name = "MaxconnReached", .desc = "Number of times an accepted connection resulted in Maxconn being reached" },
Willy Tarreau5723b382023-05-17 09:05:20 +0200161 [INF_BOOTTIME_MS] = { .name = "BootTime_ms", .desc = "How long ago it took to parse and process the config before being ready (milliseconds)" },
Willy Tarreau0baac8c2016-11-22 16:36:53 +0100162};
163
Willy Tarreaueaa55372019-10-09 07:39:11 +0200164const struct name_desc stat_fields[ST_F_TOTAL_FIELDS] = {
Willy Tarreau6d4897e2019-10-11 16:31:46 +0200165 [ST_F_PXNAME] = { .name = "pxname", .desc = "Proxy name" },
166 [ST_F_SVNAME] = { .name = "svname", .desc = "Server name" },
William Dauchyeedb9b12021-02-01 13:11:53 +0100167 [ST_F_QCUR] = { .name = "qcur", .desc = "Number of current queued connections" },
168 [ST_F_QMAX] = { .name = "qmax", .desc = "Highest value of queued connections encountered since process started" },
William Dauchy19f7cfc2021-02-01 13:11:54 +0100169 [ST_F_SCUR] = { .name = "scur", .desc = "Number of current sessions on the frontend, backend or server" },
170 [ST_F_SMAX] = { .name = "smax", .desc = "Highest value of current sessions encountered since process started" },
Willy Tarreau6d4897e2019-10-11 16:31:46 +0200171 [ST_F_SLIM] = { .name = "slim", .desc = "Frontend/listener/server's maxconn, backend's fullconn" },
172 [ST_F_STOT] = { .name = "stot", .desc = "Total number of sessions since process started" },
173 [ST_F_BIN] = { .name = "bin", .desc = "Total number of request bytes since process started" },
174 [ST_F_BOUT] = { .name = "bout", .desc = "Total number of response bytes since process started" },
175 [ST_F_DREQ] = { .name = "dreq", .desc = "Total number of denied requests since process started" },
176 [ST_F_DRESP] = { .name = "dresp", .desc = "Total number of denied responses since process started" },
177 [ST_F_EREQ] = { .name = "ereq", .desc = "Total number of invalid requests since process started" },
178 [ST_F_ECON] = { .name = "econ", .desc = "Total number of failed connections to server since the worker process started" },
179 [ST_F_ERESP] = { .name = "eresp", .desc = "Total number of invalid responses since the worker process started" },
180 [ST_F_WRETR] = { .name = "wretr", .desc = "Total number of server connection retries since the worker process started" },
181 [ST_F_WREDIS] = { .name = "wredis", .desc = "Total number of server redispatches due to connection failures since the worker process started" },
182 [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 +0200183 [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 +0200184 [ST_F_ACT] = { .name = "act", .desc = "Total number of active UP servers with a non-zero weight" },
185 [ST_F_BCK] = { .name = "bck", .desc = "Total number of backup UP servers with a non-zero weight" },
186 [ST_F_CHKFAIL] = { .name = "chkfail", .desc = "Total number of failed individual health checks per server/backend, since the worker process started" },
187 [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" },
188 [ST_F_LASTCHG] = { .name = "lastchg", .desc = "How long ago the last server state changed, in seconds" },
189 [ST_F_DOWNTIME] = { .name = "downtime", .desc = "Total time spent in DOWN state, for server or backend" },
190 [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 +0200191 [ST_F_PID] = { .name = "pid", .desc = "Relative worker process number (1)" },
Willy Tarreau6d4897e2019-10-11 16:31:46 +0200192 [ST_F_IID] = { .name = "iid", .desc = "Frontend or Backend numeric identifier ('id' setting)" },
193 [ST_F_SID] = { .name = "sid", .desc = "Server numeric identifier ('id' setting)" },
194 [ST_F_THROTTLE] = { .name = "throttle", .desc = "Throttling ratio applied to a server's maxconn and weight during the slowstart period (0 to 100%)" },
195 [ST_F_LBTOT] = { .name = "lbtot", .desc = "Total number of requests routed by load balancing since the worker process started (ignores queue pop and stickiness)" },
196 [ST_F_TRACKED] = { .name = "tracked", .desc = "Name of the other server this server tracks for its state" },
197 [ST_F_TYPE] = { .name = "type", .desc = "Type of the object (Listener, Frontend, Backend, Server)" },
198 [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)" },
199 [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 +0100200 [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 +0200201 [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" },
202 [ST_F_CHECK_CODE] = { .name = "check_code", .desc = "HTTP/SMTP/LDAP status code reported by the latest server health check" },
203 [ST_F_CHECK_DURATION] = { .name = "check_duration", .desc = "Total duration of the latest server health check, in milliseconds" },
204 [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" },
205 [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" },
206 [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" },
207 [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" },
208 [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" },
209 [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)" },
210 [ST_F_HANAFAIL] = { .name = "hanafail", .desc = "Total number of failed checks caused by an 'on-error' directive after an 'observe' condition matched" },
211 [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 +0100212 [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 +0200213 [ST_F_REQ_TOT] = { .name = "req_tot", .desc = "Total number of HTTP requests processed by this object since the worker process started" },
214 [ST_F_CLI_ABRT] = { .name = "cli_abrt", .desc = "Total number of requests or connections aborted by the client since the worker process started" },
215 [ST_F_SRV_ABRT] = { .name = "srv_abrt", .desc = "Total number of requests or connections aborted by the server since the worker process started" },
216 [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" },
217 [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" },
218 [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)" },
219 [ST_F_COMP_RSP] = { .name = "comp_rsp", .desc = "Total number of HTTP responses that were compressed for this object since the worker process started" },
220 [ST_F_LASTSESS] = { .name = "lastsess", .desc = "How long ago some traffic was seen on this object on this worker process, in seconds" },
221 [ST_F_LAST_CHK] = { .name = "last_chk", .desc = "Short description of the latest health check report for this server (see also check_desc)" },
222 [ST_F_LAST_AGT] = { .name = "last_agt", .desc = "Short description of the latest agent check report for this server (see also agent_desc)" },
223 [ST_F_QTIME] = { .name = "qtime", .desc = "Time spent in the queue, in milliseconds, averaged over the 1024 last requests (backend/server)" },
224 [ST_F_CTIME] = { .name = "ctime", .desc = "Time spent waiting for a connection to complete, in milliseconds, averaged over the 1024 last requests (backend/server)" },
225 [ST_F_RTIME] = { .name = "rtime", .desc = "Time spent waiting for a server response, in milliseconds, averaged over the 1024 last requests (backend/server)" },
226 [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)" },
227 [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" },
228 [ST_F_AGENT_CODE] = { .name = "agent_code", .desc = "Status code reported by the latest server agent check" },
229 [ST_F_AGENT_DURATION] = { .name = "agent_duration", .desc = "Total duration of the latest server agent check, in milliseconds" },
230 [ST_F_CHECK_DESC] = { .name = "check_desc", .desc = "Textual description of the latest health check report for this server" },
231 [ST_F_AGENT_DESC] = { .name = "agent_desc", .desc = "Textual description of the latest agent check report for this server" },
232 [ST_F_CHECK_RISE] = { .name = "check_rise", .desc = "Number of successful health checks before declaring a server UP (server 'rise' setting)" },
233 [ST_F_CHECK_FALL] = { .name = "check_fall", .desc = "Number of failed health checks before declaring a server DOWN (server 'fall' setting)" },
234 [ST_F_CHECK_HEALTH] = { .name = "check_health", .desc = "Current server health check level (0..fall-1=DOWN, fall..rise-1=UP)" },
235 [ST_F_AGENT_RISE] = { .name = "agent_rise", .desc = "Number of successful agent checks before declaring a server UP (server 'rise' setting)" },
236 [ST_F_AGENT_FALL] = { .name = "agent_fall", .desc = "Number of failed agent checks before declaring a server DOWN (server 'fall' setting)" },
237 [ST_F_AGENT_HEALTH] = { .name = "agent_health", .desc = "Current server agent check level (0..fall-1=DOWN, fall..rise-1=UP)" },
238 [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" },
239 [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" },
240 [ST_F_MODE] = { .name = "mode", .desc = "'mode' setting (tcp/http/health/cli)" },
241 [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" },
242 [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 +0100243 [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 +0200244 [ST_F_CONN_TOT] = { .name = "conn_tot", .desc = "Total number of new connections accepted on this frontend since the worker process started" },
245 [ST_F_INTERCEPTED] = { .name = "intercepted", .desc = "Total number of HTTP requests intercepted on the frontend (redirects/stats/services) since the worker process started" },
246 [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" },
247 [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" },
248 [ST_F_WREW] = { .name = "wrew", .desc = "Total number of failed HTTP header rewrites since the worker process started" },
249 [ST_F_CONNECT] = { .name = "connect", .desc = "Total number of outgoing connection attempts on this backend/server since the worker process started" },
250 [ST_F_REUSE] = { .name = "reuse", .desc = "Total number of reused connection on this backend/server since the worker process started" },
251 [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" },
252 [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" },
253 [ST_F_SRV_ICUR] = { .name = "srv_icur", .desc = "Current number of idle connections available for reuse on this server" },
254 [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 +0100255 [ST_F_QT_MAX] = { .name = "qtime_max", .desc = "Maximum observed time spent in the queue, in milliseconds (backend/server)" },
256 [ST_F_CT_MAX] = { .name = "ctime_max", .desc = "Maximum observed time spent waiting for a connection to complete, in milliseconds (backend/server)" },
257 [ST_F_RT_MAX] = { .name = "rtime_max", .desc = "Maximum observed time spent waiting for a server response, in milliseconds (backend/server)" },
258 [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 +0100259 [ST_F_EINT] = { .name = "eint", .desc = "Total number of internal errors since process started"},
Willy Tarreau3bb617c2020-06-29 13:51:05 +0200260 [ST_F_IDLE_CONN_CUR] = { .name = "idle_conn_cur", .desc = "Current number of unsafe idle connections"},
261 [ST_F_SAFE_CONN_CUR] = { .name = "safe_conn_cur", .desc = "Current number of safe idle connections"},
262 [ST_F_USED_CONN_CUR] = { .name = "used_conn_cur", .desc = "Current number of connections in use"},
Willy Tarreaua9fcecb2020-06-29 15:38:53 +0200263 [ST_F_NEED_CONN_EST] = { .name = "need_conn_est", .desc = "Estimated needed number of connections"},
Willy Tarreaubd715102020-10-23 22:44:30 +0200264 [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 +0000265 [ST_F_AGG_SRV_CHECK_STATUS] = { .name = "agg_server_check_status", .desc = "[DEPRECATED] Backend's aggregated gauge of servers' status" },
266 [ST_F_AGG_SRV_STATUS ] = { .name = "agg_server_status", .desc = "Backend's aggregated gauge of servers' status" },
Cedric Paillete06e31e2022-12-08 09:17:01 +0000267 [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 +0100268 [ST_F_SRID] = { .name = "srid", .desc = "Server id revision, to prevent server id reuse mixups" },
Frédéric Lécaille9969adb2023-01-18 11:52:21 +0100269 [ST_F_SESS_OTHER] = { .name = "sess_other", .desc = "Total number of sessions other than HTTP since process started" },
270 [ST_F_H1SESS] = { .name = "h1sess", .desc = "Total number of HTTP/1 sessions since process started" },
271 [ST_F_H2SESS] = { .name = "h2sess", .desc = "Total number of HTTP/2 sessions since process started" },
272 [ST_F_H3SESS] = { .name = "h3sess", .desc = "Total number of HTTP/3 sessions since process started" },
273 [ST_F_REQ_OTHER] = { .name = "req_other", .desc = "Total number of sessions other than HTTP processed by this object since the worker process started" },
274 [ST_F_H1REQ] = { .name = "h1req", .desc = "Total number of HTTP/1 sessions processed by this object since the worker process started" },
275 [ST_F_H2REQ] = { .name = "h2req", .desc = "Total number of hTTP/2 sessions processed by this object since the worker process started" },
276 [ST_F_H3REQ] = { .name = "h3req", .desc = "Total number of HTTP/3 sessions processed by this object since the worker process started" },
Tim Duesterhus7a38d472023-06-01 18:58:08 +0200277 [ST_F_PROTO] = { .name = "proto", .desc = "Protocol" },
William Lallemand74c24fb2016-11-21 17:18:36 +0100278};
279
Willy Tarreau0baac8c2016-11-22 16:36:53 +0100280/* one line of info */
William Dauchy5d9b8f32021-01-11 20:07:49 +0100281THREAD_LOCAL struct field info[INF_TOTAL_FIELDS];
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +0200282
283/* description of statistics (static and dynamic) */
284static struct name_desc *stat_f[STATS_DOMAIN_COUNT];
285static size_t stat_count[STATS_DOMAIN_COUNT];
286
287/* one line for stats */
William Dauchyb9577452021-01-17 18:27:46 +0100288THREAD_LOCAL struct field *stat_l[STATS_DOMAIN_COUNT];
William Lallemand74c24fb2016-11-21 17:18:36 +0100289
Amaury Denoyelle58d395e2020-10-05 11:49:40 +0200290/* list of all registered stats module */
291static struct list stats_module_list[STATS_DOMAIN_COUNT] = {
292 LIST_HEAD_INIT(stats_module_list[STATS_DOMAIN_PROXY]),
Emeric Brunf8642ee2021-10-29 17:59:18 +0200293 LIST_HEAD_INIT(stats_module_list[STATS_DOMAIN_RESOLVERS]),
Amaury Denoyelle58d395e2020-10-05 11:49:40 +0200294};
295
Amaury Denoyelle7f8f6cb2020-11-10 14:24:31 +0100296THREAD_LOCAL void *trash_counters;
Christopher Fauleta8b76842022-12-16 15:08:36 +0100297static THREAD_LOCAL struct buffer trash_chunk = BUF_NULL;
298
Amaury Denoyelle7f8f6cb2020-11-10 14:24:31 +0100299
Amaury Denoyelle072f97e2020-10-05 11:49:37 +0200300static inline uint8_t stats_get_domain(uint32_t domain)
301{
302 return domain >> STATS_DOMAIN & STATS_DOMAIN_MASK;
303}
304
Amaury Denoyelle72b16e52020-10-05 11:49:38 +0200305static inline enum stats_domain_px_cap stats_px_get_cap(uint32_t domain)
306{
307 return domain >> STATS_PX_CAP & STATS_PX_CAP_MASK;
308}
309
Christopher Faulet6338a082019-09-09 15:50:54 +0200310static void stats_dump_json_schema(struct buffer *out);
William Lallemand74c24fb2016-11-21 17:18:36 +0100311
Christopher Fauletf4258bd2023-05-05 10:59:39 +0200312int stats_putchk(struct appctx *appctx, struct htx *htx)
Christopher Fauletef779222018-10-31 08:47:01 +0100313{
Christopher Fauletf4258bd2023-05-05 10:59:39 +0200314 struct stconn *sc = appctx_sc(appctx);
315 struct channel *chn = sc_ic(sc);
Aurelien DARRAGONe5958d02023-02-02 17:27:27 +0100316 struct buffer *chk = &trash_chunk;
317
Christopher Fauletef779222018-10-31 08:47:01 +0100318 if (htx) {
Christopher Fauletf4258bd2023-05-05 10:59:39 +0200319 if (chk->data >= channel_htx_recv_max(chn, htx)) {
320 sc_need_room(sc, chk->data);
Christopher Faulet69fc88c2019-01-07 14:27:53 +0100321 return 0;
Christopher Fauletf4258bd2023-05-05 10:59:39 +0200322 }
323 if (!htx_add_data_atonce(htx, ist2(chk->area, chk->data))) {
324 sc_need_room(sc, 0);
Christopher Fauletef779222018-10-31 08:47:01 +0100325 return 0;
Christopher Fauletf4258bd2023-05-05 10:59:39 +0200326 }
Christopher Faulet5adbeeb2019-01-02 14:34:39 +0100327 channel_add_input(chn, chk->data);
Christopher Fauletef779222018-10-31 08:47:01 +0100328 chk->data = 0;
Christopher Fauletef779222018-10-31 08:47:01 +0100329 }
330 else {
Christopher Fauletf4258bd2023-05-05 10:59:39 +0200331 if (applet_putchk(appctx, chk) == -1)
Christopher Fauletef779222018-10-31 08:47:01 +0100332 return 0;
333 }
334 return 1;
335}
Willy Tarreau0baac8c2016-11-22 16:36:53 +0100336
Willy Tarreaucaff6312022-05-27 10:17:46 +0200337static const char *stats_scope_ptr(struct appctx *appctx, struct stconn *sc)
Christopher Fauleted7a0662019-01-14 11:07:34 +0100338{
Willy Tarreau91cefca2022-05-03 17:08:29 +0200339 struct show_stat_ctx *ctx = appctx->svcctx;
Willy Tarreaucaff6312022-05-27 10:17:46 +0200340 struct channel *req = sc_oc(sc);
Christopher Fauletb7f88902019-07-15 21:56:43 +0200341 struct htx *htx = htxbuf(&req->buf);
342 struct htx_blk *blk;
343 struct ist uri;
Christopher Fauleted7a0662019-01-14 11:07:34 +0100344
Christopher Fauletb7f88902019-07-15 21:56:43 +0200345 blk = htx_get_head_blk(htx);
Christopher Fauletea009732019-11-18 15:50:25 +0100346 BUG_ON(!blk || htx_get_blk_type(blk) != HTX_BLK_REQ_SL);
Christopher Fauletb7f88902019-07-15 21:56:43 +0200347 ALREADY_CHECKED(blk);
348 uri = htx_sl_req_uri(htx_get_blk_ptr(htx, blk));
Willy Tarreau91cefca2022-05-03 17:08:29 +0200349 return uri.ptr + ctx->scope_str;
Christopher Fauleted7a0662019-01-14 11:07:34 +0100350}
351
William Lallemand74c24fb2016-11-21 17:18:36 +0100352/*
353 * http_stats_io_handler()
354 * -> stats_dump_stat_to_buffer() // same as above, but used for CSV or HTML
355 * -> stats_dump_csv_header() // emits the CSV headers (same as above)
Simon Horman05ee2132017-01-04 09:37:25 +0100356 * -> stats_dump_json_header() // emits the JSON headers (same as above)
William Lallemand74c24fb2016-11-21 17:18:36 +0100357 * -> stats_dump_html_head() // emits the HTML headers
358 * -> stats_dump_html_info() // emits the equivalent of "show info" at the top
359 * -> stats_dump_proxy_to_buffer() // same as above, valid for CSV and HTML
360 * -> stats_dump_html_px_hdr()
361 * -> stats_dump_fe_stats()
362 * -> stats_dump_li_stats()
363 * -> stats_dump_sv_stats()
364 * -> stats_dump_be_stats()
365 * -> stats_dump_html_px_end()
366 * -> stats_dump_html_end() // emits HTML trailer
Simon Horman05ee2132017-01-04 09:37:25 +0100367 * -> stats_dump_json_end() // emits JSON trailer
William Lallemand74c24fb2016-11-21 17:18:36 +0100368 */
369
370
Christopher Fauleta8b76842022-12-16 15:08:36 +0100371/* Dumps the stats CSV header to the local trash buffer. The caller is
372 * responsible for clearing it if needed.
William Lallemand74c24fb2016-11-21 17:18:36 +0100373 * NOTE: Some tools happen to rely on the field position instead of its name,
374 * so please only append new fields at the end, never in the middle.
375 */
Amaury Denoyelle50660a82020-10-05 11:49:39 +0200376static void stats_dump_csv_header(enum stats_domain domain)
William Lallemand74c24fb2016-11-21 17:18:36 +0100377{
378 int field;
379
Christopher Fauleta8b76842022-12-16 15:08:36 +0100380 chunk_appendf(&trash_chunk, "# ");
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +0200381 if (stat_f[domain]) {
382 for (field = 0; field < stat_count[domain]; ++field) {
Christopher Fauleta8b76842022-12-16 15:08:36 +0100383 chunk_appendf(&trash_chunk, "%s,", stat_f[domain][field].name);
William Lallemand74c24fb2016-11-21 17:18:36 +0100384
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +0200385 /* print special delimiter on proxy stats to mark end of
386 static fields */
387 if (domain == STATS_DOMAIN_PROXY && field + 1 == ST_F_TOTAL_FIELDS)
Christopher Fauleta8b76842022-12-16 15:08:36 +0100388 chunk_appendf(&trash_chunk, "-,");
Amaury Denoyelle50660a82020-10-05 11:49:39 +0200389 }
390 }
391
Christopher Fauleta8b76842022-12-16 15:08:36 +0100392 chunk_appendf(&trash_chunk, "\n");
William Lallemand74c24fb2016-11-21 17:18:36 +0100393}
394
William Lallemand74c24fb2016-11-21 17:18:36 +0100395/* Emits a stats field without any surrounding element and properly encoded to
396 * resist CSV output. Returns non-zero on success, 0 if the buffer is full.
397 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200398int stats_emit_raw_data_field(struct buffer *out, const struct field *f)
William Lallemand74c24fb2016-11-21 17:18:36 +0100399{
400 switch (field_format(f, 0)) {
401 case FF_EMPTY: return 1;
402 case FF_S32: return chunk_appendf(out, "%d", f->u.s32);
403 case FF_U32: return chunk_appendf(out, "%u", f->u.u32);
404 case FF_S64: return chunk_appendf(out, "%lld", (long long)f->u.s64);
405 case FF_U64: return chunk_appendf(out, "%llu", (unsigned long long)f->u.u64);
Willy Tarreau065ba312021-05-08 10:38:20 +0200406 case FF_FLT: {
407 size_t prev_data = out->data;
408 out->data = flt_trim(out->area, prev_data, chunk_appendf(out, "%f", f->u.flt));
409 return out->data;
410 }
William Lallemand74c24fb2016-11-21 17:18:36 +0100411 case FF_STR: return csv_enc_append(field_str(f, 0), 1, out) != NULL;
412 default: return chunk_appendf(out, "[INCORRECT_FIELD_TYPE_%08x]", f->type);
413 }
414}
415
Amaury Denoyellee3f576c2020-10-05 11:49:44 +0200416const char *field_to_html_str(const struct field *f)
417{
418 switch (field_format(f, 0)) {
419 case FF_S32: return U2H(f->u.s32);
420 case FF_S64: return U2H(f->u.s64);
421 case FF_U64: return U2H(f->u.u64);
422 case FF_U32: return U2H(f->u.u32);
Willy Tarreau6004fb72021-05-08 07:37:38 +0200423 case FF_FLT: return F2H(f->u.flt);
Amaury Denoyellee3f576c2020-10-05 11:49:44 +0200424 case FF_STR: return field_str(f, 0);
425 case FF_EMPTY:
426 default:
427 return "";
428 }
429}
430
William Lallemand74c24fb2016-11-21 17:18:36 +0100431/* Emits a stats field prefixed with its type. No CSV encoding is prepared, the
432 * output is supposed to be used on its own line. Returns non-zero on success, 0
433 * if the buffer is full.
434 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200435int stats_emit_typed_data_field(struct buffer *out, const struct field *f)
William Lallemand74c24fb2016-11-21 17:18:36 +0100436{
437 switch (field_format(f, 0)) {
438 case FF_EMPTY: return 1;
439 case FF_S32: return chunk_appendf(out, "s32:%d", f->u.s32);
440 case FF_U32: return chunk_appendf(out, "u32:%u", f->u.u32);
441 case FF_S64: return chunk_appendf(out, "s64:%lld", (long long)f->u.s64);
442 case FF_U64: return chunk_appendf(out, "u64:%llu", (unsigned long long)f->u.u64);
Willy Tarreau065ba312021-05-08 10:38:20 +0200443 case FF_FLT: {
444 size_t prev_data = out->data;
445 out->data = flt_trim(out->area, prev_data, chunk_appendf(out, "flt:%f", f->u.flt));
446 return out->data;
447 }
William Lallemand74c24fb2016-11-21 17:18:36 +0100448 case FF_STR: return chunk_appendf(out, "str:%s", field_str(f, 0));
449 default: return chunk_appendf(out, "%08x:?", f->type);
450 }
451}
452
Simon Horman05ee2132017-01-04 09:37:25 +0100453/* Limit JSON integer values to the range [-(2**53)+1, (2**53)-1] as per
454 * the recommendation for interoperable integers in section 6 of RFC 7159.
455 */
456#define JSON_INT_MAX ((1ULL << 53) - 1)
457#define JSON_INT_MIN (0 - JSON_INT_MAX)
458
459/* Emits a stats field value and its type in JSON.
460 * Returns non-zero on success, 0 on error.
461 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200462int stats_emit_json_data_field(struct buffer *out, const struct field *f)
Simon Horman05ee2132017-01-04 09:37:25 +0100463{
464 int old_len;
465 char buf[20];
466 const char *type, *value = buf, *quote = "";
467
468 switch (field_format(f, 0)) {
469 case FF_EMPTY: return 1;
470 case FF_S32: type = "\"s32\"";
471 snprintf(buf, sizeof(buf), "%d", f->u.s32);
472 break;
473 case FF_U32: type = "\"u32\"";
474 snprintf(buf, sizeof(buf), "%u", f->u.u32);
475 break;
476 case FF_S64: type = "\"s64\"";
477 if (f->u.s64 < JSON_INT_MIN || f->u.s64 > JSON_INT_MAX)
478 return 0;
479 type = "\"s64\"";
480 snprintf(buf, sizeof(buf), "%lld", (long long)f->u.s64);
481 break;
482 case FF_U64: if (f->u.u64 > JSON_INT_MAX)
483 return 0;
484 type = "\"u64\"";
485 snprintf(buf, sizeof(buf), "%llu",
486 (unsigned long long) f->u.u64);
Christopher Faulet52c91bb2019-09-28 10:37:31 +0200487 break;
Christopher Faulet88a0db22019-09-24 16:35:10 +0200488 case FF_FLT: type = "\"flt\"";
Willy Tarreau065ba312021-05-08 10:38:20 +0200489 flt_trim(buf, 0, snprintf(buf, sizeof(buf), "%f", f->u.flt));
Simon Horman05ee2132017-01-04 09:37:25 +0100490 break;
491 case FF_STR: type = "\"str\"";
492 value = field_str(f, 0);
493 quote = "\"";
494 break;
495 default: snprintf(buf, sizeof(buf), "%u", f->type);
496 type = buf;
497 value = "unknown";
498 quote = "\"";
499 break;
500 }
501
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200502 old_len = out->data;
Simon Horman05ee2132017-01-04 09:37:25 +0100503 chunk_appendf(out, ",\"value\":{\"type\":%s,\"value\":%s%s%s}",
504 type, quote, value, quote);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200505 return !(old_len == out->data);
Simon Horman05ee2132017-01-04 09:37:25 +0100506}
507
William Lallemand74c24fb2016-11-21 17:18:36 +0100508/* Emits an encoding of the field type on 3 characters followed by a delimiter.
509 * Returns non-zero on success, 0 if the buffer is full.
510 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200511int stats_emit_field_tags(struct buffer *out, const struct field *f,
512 char delim)
William Lallemand74c24fb2016-11-21 17:18:36 +0100513{
514 char origin, nature, scope;
515
516 switch (field_origin(f, 0)) {
517 case FO_METRIC: origin = 'M'; break;
518 case FO_STATUS: origin = 'S'; break;
519 case FO_KEY: origin = 'K'; break;
520 case FO_CONFIG: origin = 'C'; break;
521 case FO_PRODUCT: origin = 'P'; break;
522 default: origin = '?'; break;
523 }
524
525 switch (field_nature(f, 0)) {
526 case FN_GAUGE: nature = 'G'; break;
527 case FN_LIMIT: nature = 'L'; break;
528 case FN_MIN: nature = 'm'; break;
529 case FN_MAX: nature = 'M'; break;
530 case FN_RATE: nature = 'R'; break;
531 case FN_COUNTER: nature = 'C'; break;
532 case FN_DURATION: nature = 'D'; break;
533 case FN_AGE: nature = 'A'; break;
534 case FN_TIME: nature = 'T'; break;
535 case FN_NAME: nature = 'N'; break;
536 case FN_OUTPUT: nature = 'O'; break;
537 case FN_AVG: nature = 'a'; break;
538 default: nature = '?'; break;
539 }
540
541 switch (field_scope(f, 0)) {
542 case FS_PROCESS: scope = 'P'; break;
543 case FS_SERVICE: scope = 'S'; break;
544 case FS_SYSTEM: scope = 's'; break;
545 case FS_CLUSTER: scope = 'C'; break;
546 default: scope = '?'; break;
547 }
548
549 return chunk_appendf(out, "%c%c%c%c", origin, nature, scope, delim);
550}
551
Simon Horman05ee2132017-01-04 09:37:25 +0100552/* Emits an encoding of the field type as JSON.
553 * Returns non-zero on success, 0 if the buffer is full.
554 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200555int stats_emit_json_field_tags(struct buffer *out, const struct field *f)
Simon Horman05ee2132017-01-04 09:37:25 +0100556{
557 const char *origin, *nature, *scope;
558 int old_len;
559
560 switch (field_origin(f, 0)) {
561 case FO_METRIC: origin = "Metric"; break;
562 case FO_STATUS: origin = "Status"; break;
563 case FO_KEY: origin = "Key"; break;
564 case FO_CONFIG: origin = "Config"; break;
565 case FO_PRODUCT: origin = "Product"; break;
566 default: origin = "Unknown"; break;
567 }
568
569 switch (field_nature(f, 0)) {
570 case FN_GAUGE: nature = "Gauge"; break;
571 case FN_LIMIT: nature = "Limit"; break;
572 case FN_MIN: nature = "Min"; break;
573 case FN_MAX: nature = "Max"; break;
574 case FN_RATE: nature = "Rate"; break;
575 case FN_COUNTER: nature = "Counter"; break;
576 case FN_DURATION: nature = "Duration"; break;
577 case FN_AGE: nature = "Age"; break;
578 case FN_TIME: nature = "Time"; break;
579 case FN_NAME: nature = "Name"; break;
580 case FN_OUTPUT: nature = "Output"; break;
581 case FN_AVG: nature = "Avg"; break;
582 default: nature = "Unknown"; break;
583 }
584
585 switch (field_scope(f, 0)) {
586 case FS_PROCESS: scope = "Process"; break;
587 case FS_SERVICE: scope = "Service"; break;
588 case FS_SYSTEM: scope = "System"; break;
589 case FS_CLUSTER: scope = "Cluster"; break;
590 default: scope = "Unknown"; break;
591 }
592
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200593 old_len = out->data;
Simon Horman05ee2132017-01-04 09:37:25 +0100594 chunk_appendf(out, "\"tags\":{"
595 "\"origin\":\"%s\","
596 "\"nature\":\"%s\","
597 "\"scope\":\"%s\""
598 "}", origin, nature, scope);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200599 return !(old_len == out->data);
Simon Horman05ee2132017-01-04 09:37:25 +0100600}
William Lallemand74c24fb2016-11-21 17:18:36 +0100601
602/* Dump all fields from <stats> into <out> using CSV format */
Willy Tarreau83061a82018-07-13 11:56:34 +0200603static int stats_dump_fields_csv(struct buffer *out,
Amaury Denoyelle97323c92020-10-02 18:32:01 +0200604 const struct field *stats, size_t stats_count,
Aurelien DARRAGONe76a0272022-12-15 12:10:03 +0100605 struct show_stat_ctx *ctx)
William Lallemand74c24fb2016-11-21 17:18:36 +0100606{
Aurelien DARRAGONe76a0272022-12-15 12:10:03 +0100607 int domain = ctx->domain;
William Lallemand74c24fb2016-11-21 17:18:36 +0100608 int field;
609
Amaury Denoyelle50660a82020-10-05 11:49:39 +0200610 for (field = 0; field < stats_count; ++field) {
William Lallemand74c24fb2016-11-21 17:18:36 +0100611 if (!stats_emit_raw_data_field(out, &stats[field]))
612 return 0;
613 if (!chunk_strcat(out, ","))
614 return 0;
Amaury Denoyelle50660a82020-10-05 11:49:39 +0200615
616 /* print special delimiter on proxy stats to mark end of
617 static fields */
618 if (domain == STATS_DOMAIN_PROXY && field + 1 == ST_F_TOTAL_FIELDS) {
619 if (!chunk_strcat(out, "-,"))
620 return 0;
621 }
William Lallemand74c24fb2016-11-21 17:18:36 +0100622 }
Amaury Denoyelle50660a82020-10-05 11:49:39 +0200623
William Lallemand74c24fb2016-11-21 17:18:36 +0100624 chunk_strcat(out, "\n");
625 return 1;
626}
627
628/* Dump all fields from <stats> into <out> using a typed "field:desc:type:value" format */
Willy Tarreau83061a82018-07-13 11:56:34 +0200629static int stats_dump_fields_typed(struct buffer *out,
Amaury Denoyelle97323c92020-10-02 18:32:01 +0200630 const struct field *stats,
631 size_t stats_count,
Aurelien DARRAGONe76a0272022-12-15 12:10:03 +0100632 struct show_stat_ctx * ctx)
William Lallemand74c24fb2016-11-21 17:18:36 +0100633{
Aurelien DARRAGONe76a0272022-12-15 12:10:03 +0100634 int flags = ctx->flags;
635 int domain = ctx->domain;
William Lallemand74c24fb2016-11-21 17:18:36 +0100636 int field;
637
Amaury Denoyelle97323c92020-10-02 18:32:01 +0200638 for (field = 0; field < stats_count; ++field) {
William Lallemand74c24fb2016-11-21 17:18:36 +0100639 if (!stats[field].type)
640 continue;
641
Amaury Denoyelle072f97e2020-10-05 11:49:37 +0200642 switch (domain) {
643 case STATS_DOMAIN_PROXY:
644 chunk_appendf(out, "%c.%u.%u.%d.%s.%u:",
645 stats[ST_F_TYPE].u.u32 == STATS_TYPE_FE ? 'F' :
646 stats[ST_F_TYPE].u.u32 == STATS_TYPE_BE ? 'B' :
647 stats[ST_F_TYPE].u.u32 == STATS_TYPE_SO ? 'L' :
648 stats[ST_F_TYPE].u.u32 == STATS_TYPE_SV ? 'S' :
649 '?',
650 stats[ST_F_IID].u.u32, stats[ST_F_SID].u.u32,
651 field,
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +0200652 stat_f[domain][field].name,
Amaury Denoyelle072f97e2020-10-05 11:49:37 +0200653 stats[ST_F_PID].u.u32);
654 break;
655
Emeric Brunf8642ee2021-10-29 17:59:18 +0200656 case STATS_DOMAIN_RESOLVERS:
657 chunk_appendf(out, "N.%d.%s:", field,
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +0200658 stat_f[domain][field].name);
659 break;
660
Amaury Denoyelle072f97e2020-10-05 11:49:37 +0200661 default:
662 break;
663 }
William Lallemand74c24fb2016-11-21 17:18:36 +0100664
665 if (!stats_emit_field_tags(out, &stats[field], ':'))
666 return 0;
667 if (!stats_emit_typed_data_field(out, &stats[field]))
668 return 0;
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +0200669
Willy Tarreaufc8e4382021-06-17 07:22:27 +0200670 if (flags & STAT_SHOW_FDESC &&
671 !chunk_appendf(out, ":\"%s\"", stat_f[domain][field].desc)) {
Willy Tarreau6b19b142019-10-09 15:44:21 +0200672 return 0;
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +0200673 }
674
William Lallemand74c24fb2016-11-21 17:18:36 +0100675 if (!chunk_strcat(out, "\n"))
676 return 0;
677 }
678 return 1;
679}
680
Simon Horman05ee2132017-01-04 09:37:25 +0100681/* Dump all fields from <stats> into <out> using the "show info json" format */
Willy Tarreau83061a82018-07-13 11:56:34 +0200682static int stats_dump_json_info_fields(struct buffer *out,
Aurelien DARRAGONe76a0272022-12-15 12:10:03 +0100683 const struct field *info,
684 struct show_stat_ctx *ctx)
Simon Horman05ee2132017-01-04 09:37:25 +0100685{
Aurelien DARRAGON16c9ca92022-12-15 14:24:30 +0100686 int started = (ctx->field) ? 1 : 0;
687 int ready_data = 0;
Simon Horman05ee2132017-01-04 09:37:25 +0100688
Aurelien DARRAGON16c9ca92022-12-15 14:24:30 +0100689 if (!started && !chunk_strcat(out, "["))
Simon Horman05ee2132017-01-04 09:37:25 +0100690 return 0;
691
Aurelien DARRAGON16c9ca92022-12-15 14:24:30 +0100692 for (; ctx->field < INF_TOTAL_FIELDS; ctx->field++) {
Simon Horman05ee2132017-01-04 09:37:25 +0100693 int old_len;
Aurelien DARRAGON16c9ca92022-12-15 14:24:30 +0100694 int field = ctx->field;
Simon Horman05ee2132017-01-04 09:37:25 +0100695
696 if (!field_format(info, field))
697 continue;
698
699 if (started && !chunk_strcat(out, ","))
700 goto err;
701 started = 1;
702
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200703 old_len = out->data;
Simon Horman05ee2132017-01-04 09:37:25 +0100704 chunk_appendf(out,
705 "{\"field\":{\"pos\":%d,\"name\":\"%s\"},"
706 "\"processNum\":%u,",
Willy Tarreaueaa55372019-10-09 07:39:11 +0200707 field, info_fields[field].name,
Simon Horman05ee2132017-01-04 09:37:25 +0100708 info[INF_PROCESS_NUM].u.u32);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200709 if (old_len == out->data)
Simon Horman05ee2132017-01-04 09:37:25 +0100710 goto err;
711
712 if (!stats_emit_json_field_tags(out, &info[field]))
713 goto err;
714
715 if (!stats_emit_json_data_field(out, &info[field]))
716 goto err;
717
718 if (!chunk_strcat(out, "}"))
719 goto err;
Aurelien DARRAGON16c9ca92022-12-15 14:24:30 +0100720 ready_data = out->data;
Simon Horman05ee2132017-01-04 09:37:25 +0100721 }
722
Willy Tarreauc6b7a972022-06-10 15:12:21 +0200723 if (!chunk_strcat(out, "]\n"))
Simon Horman05ee2132017-01-04 09:37:25 +0100724 goto err;
Aurelien DARRAGON16c9ca92022-12-15 14:24:30 +0100725 ctx->field = 0; /* we're done */
Simon Horman05ee2132017-01-04 09:37:25 +0100726 return 1;
727
728err:
Aurelien DARRAGON16c9ca92022-12-15 14:24:30 +0100729 if (!ready_data) {
730 /* not enough buffer space for a single entry.. */
731 chunk_reset(out);
732 chunk_appendf(out, "{\"errorStr\":\"output buffer too short\"}\n");
733 return 0; /* hard error */
734 }
735 /* push ready data and wait for a new buffer to complete the dump */
736 out->data = ready_data;
737 return 1;
Simon Horman05ee2132017-01-04 09:37:25 +0100738}
739
Amaury Denoyellef34017b2020-10-02 18:32:03 +0200740static void stats_print_proxy_field_json(struct buffer *out,
741 const struct field *stat,
742 const char *name,
743 int pos,
744 uint32_t field_type,
745 uint32_t iid,
746 uint32_t sid,
747 uint32_t pid)
748{
749 const char *obj_type;
750 switch (field_type) {
751 case STATS_TYPE_FE: obj_type = "Frontend"; break;
752 case STATS_TYPE_BE: obj_type = "Backend"; break;
753 case STATS_TYPE_SO: obj_type = "Listener"; break;
754 case STATS_TYPE_SV: obj_type = "Server"; break;
755 default: obj_type = "Unknown"; break;
756 }
757
758 chunk_appendf(out,
759 "{"
760 "\"objType\":\"%s\","
761 "\"proxyId\":%u,"
762 "\"id\":%u,"
763 "\"field\":{\"pos\":%d,\"name\":\"%s\"},"
764 "\"processNum\":%u,",
765 obj_type, iid, sid, pos, name, pid);
766}
767
Emeric Brunf8642ee2021-10-29 17:59:18 +0200768static void stats_print_rslv_field_json(struct buffer *out,
769 const struct field *stat,
770 const char *name,
771 int pos)
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +0200772{
773 chunk_appendf(out,
774 "{"
775 "\"field\":{\"pos\":%d,\"name\":\"%s\"},",
776 pos, name);
777}
778
779
Simon Horman05ee2132017-01-04 09:37:25 +0100780/* Dump all fields from <stats> into <out> using a typed "field:desc:type:value" format */
Willy Tarreau83061a82018-07-13 11:56:34 +0200781static int stats_dump_fields_json(struct buffer *out,
Amaury Denoyelle97323c92020-10-02 18:32:01 +0200782 const struct field *stats, size_t stats_count,
Aurelien DARRAGONe76a0272022-12-15 12:10:03 +0100783 struct show_stat_ctx *ctx)
Simon Horman05ee2132017-01-04 09:37:25 +0100784{
Aurelien DARRAGONe76a0272022-12-15 12:10:03 +0100785 int flags = ctx->flags;
786 int domain = ctx->domain;
Aurelien DARRAGON42b18fb2022-12-15 14:17:09 +0100787 int started = (ctx->field) ? 1 : 0;
788 int ready_data = 0;
Simon Horman05ee2132017-01-04 09:37:25 +0100789
Aurelien DARRAGON42b18fb2022-12-15 14:17:09 +0100790 if (!started && (flags & STAT_STARTED) && !chunk_strcat(out, ","))
Simon Horman05ee2132017-01-04 09:37:25 +0100791 return 0;
Aurelien DARRAGON42b18fb2022-12-15 14:17:09 +0100792 if (!started && !chunk_strcat(out, "["))
Simon Horman05ee2132017-01-04 09:37:25 +0100793 return 0;
794
Aurelien DARRAGON42b18fb2022-12-15 14:17:09 +0100795 for (; ctx->field < stats_count; ctx->field++) {
Simon Horman05ee2132017-01-04 09:37:25 +0100796 int old_len;
Aurelien DARRAGON42b18fb2022-12-15 14:17:09 +0100797 int field = ctx->field;
Simon Horman05ee2132017-01-04 09:37:25 +0100798
799 if (!stats[field].type)
800 continue;
801
802 if (started && !chunk_strcat(out, ","))
803 goto err;
804 started = 1;
805
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200806 old_len = out->data;
Amaury Denoyelle072f97e2020-10-05 11:49:37 +0200807 if (domain == STATS_DOMAIN_PROXY) {
808 stats_print_proxy_field_json(out, &stats[field],
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +0200809 stat_f[domain][field].name,
810 field,
Amaury Denoyelle072f97e2020-10-05 11:49:37 +0200811 stats[ST_F_TYPE].u.u32,
812 stats[ST_F_IID].u.u32,
813 stats[ST_F_SID].u.u32,
814 stats[ST_F_PID].u.u32);
Emeric Brunf8642ee2021-10-29 17:59:18 +0200815 } else if (domain == STATS_DOMAIN_RESOLVERS) {
816 stats_print_rslv_field_json(out, &stats[field],
817 stat_f[domain][field].name,
818 field);
Amaury Denoyelle072f97e2020-10-05 11:49:37 +0200819 }
Amaury Denoyellef34017b2020-10-02 18:32:03 +0200820
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200821 if (old_len == out->data)
Simon Horman05ee2132017-01-04 09:37:25 +0100822 goto err;
823
824 if (!stats_emit_json_field_tags(out, &stats[field]))
825 goto err;
826
827 if (!stats_emit_json_data_field(out, &stats[field]))
828 goto err;
829
830 if (!chunk_strcat(out, "}"))
831 goto err;
Aurelien DARRAGON42b18fb2022-12-15 14:17:09 +0100832 ready_data = out->data;
Simon Horman05ee2132017-01-04 09:37:25 +0100833 }
834
835 if (!chunk_strcat(out, "]"))
836 goto err;
837
Aurelien DARRAGON42b18fb2022-12-15 14:17:09 +0100838 ctx->field = 0; /* we're done */
Simon Horman05ee2132017-01-04 09:37:25 +0100839 return 1;
840
841err:
Aurelien DARRAGON42b18fb2022-12-15 14:17:09 +0100842 if (!ready_data) {
843 /* not enough buffer space for a single entry.. */
844 chunk_reset(out);
845 if (ctx->flags & STAT_STARTED)
846 chunk_strcat(out, ",");
847 chunk_appendf(out, "{\"errorStr\":\"output buffer too short\"}");
848 return 0; /* hard error */
849 }
850 /* push ready data and wait for a new buffer to complete the dump */
851 out->data = ready_data;
852 return 1;
Simon Horman05ee2132017-01-04 09:37:25 +0100853}
854
William Lallemand74c24fb2016-11-21 17:18:36 +0100855/* Dump all fields from <stats> into <out> using the HTML format. A column is
Willy Tarreauab02b3f2019-10-09 11:11:46 +0200856 * reserved for the checkbox is STAT_ADMIN is set in <flags>. Some extra info
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +0200857 * are provided if STAT_SHLGNDS is present in <flags>. The statistics from
858 * extra modules are displayed at the end of the lines if STAT_SHMODULES is
859 * present in <flags>.
William Lallemand74c24fb2016-11-21 17:18:36 +0100860 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200861static int stats_dump_fields_html(struct buffer *out,
Aurelien DARRAGONe76a0272022-12-15 12:10:03 +0100862 const struct field *stats,
863 struct show_stat_ctx *ctx)
William Lallemand74c24fb2016-11-21 17:18:36 +0100864{
Willy Tarreau83061a82018-07-13 11:56:34 +0200865 struct buffer src;
Amaury Denoyellee3f576c2020-10-05 11:49:44 +0200866 struct stats_module *mod;
Aurelien DARRAGONe76a0272022-12-15 12:10:03 +0100867 int flags = ctx->flags;
Amaury Denoyellee3f576c2020-10-05 11:49:44 +0200868 int i = 0, j = 0;
William Lallemand74c24fb2016-11-21 17:18:36 +0100869
870 if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_FE) {
871 chunk_appendf(out,
872 /* name, queue */
873 "<tr class=\"frontend\">");
874
Willy Tarreauab02b3f2019-10-09 11:11:46 +0200875 if (flags & STAT_ADMIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +0100876 /* Column sub-heading for Enable or Disable server */
877 chunk_appendf(out, "<td></td>");
878 }
879
880 chunk_appendf(out,
881 "<td class=ac>"
882 "<a name=\"%s/Frontend\"></a>"
883 "<a class=lfsb href=\"#%s/Frontend\">Frontend</a></td>"
884 "<td colspan=3></td>"
885 "",
886 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_PXNAME));
887
888 chunk_appendf(out,
889 /* sessions rate : current */
890 "<td><u>%s<div class=tips><table class=det>"
891 "<tr><th>Current connection rate:</th><td>%s/s</td></tr>"
892 "<tr><th>Current session rate:</th><td>%s/s</td></tr>"
893 "",
894 U2H(stats[ST_F_RATE].u.u32),
895 U2H(stats[ST_F_CONN_RATE].u.u32),
896 U2H(stats[ST_F_RATE].u.u32));
897
898 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
899 chunk_appendf(out,
900 "<tr><th>Current request rate:</th><td>%s/s</td></tr>",
901 U2H(stats[ST_F_REQ_RATE].u.u32));
902
903 chunk_appendf(out,
904 "</table></div></u></td>"
905 /* sessions rate : max */
906 "<td><u>%s<div class=tips><table class=det>"
907 "<tr><th>Max connection rate:</th><td>%s/s</td></tr>"
908 "<tr><th>Max session rate:</th><td>%s/s</td></tr>"
909 "",
910 U2H(stats[ST_F_RATE_MAX].u.u32),
911 U2H(stats[ST_F_CONN_RATE_MAX].u.u32),
912 U2H(stats[ST_F_RATE_MAX].u.u32));
913
914 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
915 chunk_appendf(out,
916 "<tr><th>Max request rate:</th><td>%s/s</td></tr>",
917 U2H(stats[ST_F_REQ_RATE_MAX].u.u32));
918
919 chunk_appendf(out,
920 "</table></div></u></td>"
921 /* sessions rate : limit */
922 "<td>%s</td>",
923 LIM2A(stats[ST_F_RATE_LIM].u.u32, "-"));
924
925 chunk_appendf(out,
926 /* sessions: current, max, limit, total */
927 "<td>%s</td><td>%s</td><td>%s</td>"
928 "<td><u>%s<div class=tips><table class=det>"
929 "<tr><th>Cum. connections:</th><td>%s</td></tr>"
930 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
931 "",
932 U2H(stats[ST_F_SCUR].u.u32), U2H(stats[ST_F_SMAX].u.u32), U2H(stats[ST_F_SLIM].u.u32),
933 U2H(stats[ST_F_STOT].u.u64),
934 U2H(stats[ST_F_CONN_TOT].u.u64),
935 U2H(stats[ST_F_STOT].u.u64));
936
937 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
938 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0) {
939 chunk_appendf(out,
Frédéric Lécaille9969adb2023-01-18 11:52:21 +0100940 "<tr><th>- HTTP/1 sessions:</th><td>%s</td></tr>"
941 "<tr><th>- HTTP/2 sessions:</th><td>%s</td></tr>"
942 "<tr><th>- HTTP/3 sessions:</th><td>%s</td></tr>"
943 "<tr><th>- other sessions:</th><td>%s</td></tr>"
William Lallemand74c24fb2016-11-21 17:18:36 +0100944 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
Frédéric Lécaille9969adb2023-01-18 11:52:21 +0100945 "<tr><th>- HTTP/1 requests:</th><td>%s</td></tr>"
946 "<tr><th>- HTTP/2 requests:</th><td>%s</td></tr>"
947 "<tr><th>- HTTP/3 requests:</th><td>%s</td></tr>"
948 "<tr><th>- other requests:</th><td>%s</td></tr>"
949 "",
950 U2H(stats[ST_F_H1SESS].u.u64),
951 U2H(stats[ST_F_H2SESS].u.u64),
952 U2H(stats[ST_F_H3SESS].u.u64),
953 U2H(stats[ST_F_SESS_OTHER].u.u64),
954 U2H(stats[ST_F_REQ_TOT].u.u64),
955 U2H(stats[ST_F_H1REQ].u.u64),
956 U2H(stats[ST_F_H2REQ].u.u64),
957 U2H(stats[ST_F_H3REQ].u.u64),
958 U2H(stats[ST_F_REQ_OTHER].u.u64));
959
960 chunk_appendf(out,
William Lallemand74c24fb2016-11-21 17:18:36 +0100961 "<tr><th>- HTTP 1xx responses:</th><td>%s</td></tr>"
962 "<tr><th>- HTTP 2xx responses:</th><td>%s</td></tr>"
963 "<tr><th>&nbsp;&nbsp;Compressed 2xx:</th><td>%s</td><td>(%d%%)</td></tr>"
964 "<tr><th>- HTTP 3xx responses:</th><td>%s</td></tr>"
965 "<tr><th>- HTTP 4xx responses:</th><td>%s</td></tr>"
966 "<tr><th>- HTTP 5xx responses:</th><td>%s</td></tr>"
967 "<tr><th>- other responses:</th><td>%s</td></tr>"
William Lallemand74c24fb2016-11-21 17:18:36 +0100968 "",
William Lallemand74c24fb2016-11-21 17:18:36 +0100969 U2H(stats[ST_F_HRSP_1XX].u.u64),
970 U2H(stats[ST_F_HRSP_2XX].u.u64),
971 U2H(stats[ST_F_COMP_RSP].u.u64),
972 stats[ST_F_HRSP_2XX].u.u64 ?
973 (int)(100 * stats[ST_F_COMP_RSP].u.u64 / stats[ST_F_HRSP_2XX].u.u64) : 0,
974 U2H(stats[ST_F_HRSP_3XX].u.u64),
975 U2H(stats[ST_F_HRSP_4XX].u.u64),
976 U2H(stats[ST_F_HRSP_5XX].u.u64),
Frédéric Lécaille9969adb2023-01-18 11:52:21 +0100977 U2H(stats[ST_F_HRSP_OTHER].u.u64));
978
979 chunk_appendf(out,
980 "<tr><th>Intercepted requests:</th><td>%s</td></tr>"
981 "<tr><th>Cache lookups:</th><td>%s</td></tr>"
982 "<tr><th>Cache hits:</th><td>%s</td><td>(%d%%)</td></tr>"
983 "<tr><th>Failed hdr rewrites:</th><td>%s</td></tr>"
984 "<tr><th>Internal errors:</th><td>%s</td></tr>"
985 "",
Willy Tarreau1b0f85e2018-05-28 15:12:40 +0200986 U2H(stats[ST_F_INTERCEPTED].u.u64),
Willy Tarreaua1214a52018-12-14 14:00:25 +0100987 U2H(stats[ST_F_CACHE_LOOKUPS].u.u64),
988 U2H(stats[ST_F_CACHE_HITS].u.u64),
989 stats[ST_F_CACHE_LOOKUPS].u.u64 ?
990 (int)(100 * stats[ST_F_CACHE_HITS].u.u64 / stats[ST_F_CACHE_LOOKUPS].u.u64) : 0,
Christopher Faulet0159ee42019-12-16 14:40:39 +0100991 U2H(stats[ST_F_WREW].u.u64),
992 U2H(stats[ST_F_EINT].u.u64));
William Lallemand74c24fb2016-11-21 17:18:36 +0100993 }
994
995 chunk_appendf(out,
996 "</table></div></u></td>"
997 /* sessions: lbtot, lastsess */
998 "<td></td><td></td>"
999 /* bytes : in */
1000 "<td>%s</td>"
1001 "",
1002 U2H(stats[ST_F_BIN].u.u64));
1003
1004 chunk_appendf(out,
1005 /* bytes:out + compression stats (via hover): comp_in, comp_out, comp_byp */
1006 "<td>%s%s<div class=tips><table class=det>"
1007 "<tr><th>Response bytes in:</th><td>%s</td></tr>"
1008 "<tr><th>Compression in:</th><td>%s</td></tr>"
1009 "<tr><th>Compression out:</th><td>%s</td><td>(%d%%)</td></tr>"
1010 "<tr><th>Compression bypass:</th><td>%s</td></tr>"
1011 "<tr><th>Total bytes saved:</th><td>%s</td><td>(%d%%)</td></tr>"
1012 "</table></div>%s</td>",
1013 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "<u>":"",
1014 U2H(stats[ST_F_BOUT].u.u64),
1015 U2H(stats[ST_F_BOUT].u.u64),
1016 U2H(stats[ST_F_COMP_IN].u.u64),
1017 U2H(stats[ST_F_COMP_OUT].u.u64),
1018 stats[ST_F_COMP_IN].u.u64 ? (int)(stats[ST_F_COMP_OUT].u.u64 * 100 / stats[ST_F_COMP_IN].u.u64) : 0,
1019 U2H(stats[ST_F_COMP_BYP].u.u64),
1020 U2H(stats[ST_F_COMP_IN].u.u64 - stats[ST_F_COMP_OUT].u.u64),
1021 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,
1022 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "</u>":"");
1023
1024 chunk_appendf(out,
1025 /* denied: req, resp */
1026 "<td>%s</td><td>%s</td>"
1027 /* errors : request, connect, response */
1028 "<td>%s</td><td></td><td></td>"
1029 /* warnings: retries, redispatches */
1030 "<td></td><td></td>"
1031 /* server status : reflect frontend status */
1032 "<td class=ac>%s</td>"
1033 /* rest of server: nothing */
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001034 "<td class=ac colspan=8></td>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001035 "",
1036 U2H(stats[ST_F_DREQ].u.u64), U2H(stats[ST_F_DRESP].u.u64),
1037 U2H(stats[ST_F_EREQ].u.u64),
1038 field_str(stats, ST_F_STATUS));
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001039
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001040 if (flags & STAT_SHMODULES) {
1041 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001042 chunk_appendf(out, "<td>");
1043
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001044 if (stats_px_get_cap(mod->domain_flags) & STATS_PX_CAP_FE) {
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001045 chunk_appendf(out,
1046 "<u>%s<div class=tips><table class=det>",
1047 mod->name);
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001048 for (j = 0; j < mod->stats_count; ++j) {
1049 chunk_appendf(out,
1050 "<tr><th>%s</th><td>%s</td></tr>",
1051 mod->stats[j].desc, field_to_html_str(&stats[ST_F_TOTAL_FIELDS + i]));
1052 ++i;
1053 }
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001054 chunk_appendf(out, "</table></div></u>");
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001055 } else {
1056 i += mod->stats_count;
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001057 }
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001058
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001059 chunk_appendf(out, "</td>");
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001060 }
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001061 }
1062
1063 chunk_appendf(out, "</tr>");
William Lallemand74c24fb2016-11-21 17:18:36 +01001064 }
1065 else if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_SO) {
1066 chunk_appendf(out, "<tr class=socket>");
Willy Tarreauab02b3f2019-10-09 11:11:46 +02001067 if (flags & STAT_ADMIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001068 /* Column sub-heading for Enable or Disable server */
1069 chunk_appendf(out, "<td></td>");
1070 }
1071
1072 chunk_appendf(out,
1073 /* frontend name, listener name */
1074 "<td class=ac><a name=\"%s/+%s\"></a>%s"
1075 "<a class=lfsb href=\"#%s/+%s\">%s</a>"
1076 "",
1077 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME),
Willy Tarreau708c4162019-10-09 10:19:16 +02001078 (flags & STAT_SHLGNDS)?"<u>":"",
William Lallemand74c24fb2016-11-21 17:18:36 +01001079 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME), field_str(stats, ST_F_SVNAME));
1080
Willy Tarreau708c4162019-10-09 10:19:16 +02001081 if (flags & STAT_SHLGNDS) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001082 chunk_appendf(out, "<div class=tips>");
1083
Willy Tarreau90807112020-02-25 08:16:33 +01001084 if (isdigit((unsigned char)*field_str(stats, ST_F_ADDR)))
William Lallemand74c24fb2016-11-21 17:18:36 +01001085 chunk_appendf(out, "IPv4: %s, ", field_str(stats, ST_F_ADDR));
1086 else if (*field_str(stats, ST_F_ADDR) == '[')
1087 chunk_appendf(out, "IPv6: %s, ", field_str(stats, ST_F_ADDR));
1088 else if (*field_str(stats, ST_F_ADDR))
1089 chunk_appendf(out, "%s, ", field_str(stats, ST_F_ADDR));
1090
Willy Tarreau4cfb0012023-05-11 14:49:43 +02001091 chunk_appendf(out, "proto=%s, ", field_str(stats, ST_F_PROTO));
1092
William Lallemand74c24fb2016-11-21 17:18:36 +01001093 /* id */
1094 chunk_appendf(out, "id: %d</div>", stats[ST_F_SID].u.u32);
1095 }
1096
1097 chunk_appendf(out,
1098 /* queue */
1099 "%s</td><td colspan=3></td>"
1100 /* sessions rate: current, max, limit */
1101 "<td colspan=3>&nbsp;</td>"
1102 /* sessions: current, max, limit, total, lbtot, lastsess */
1103 "<td>%s</td><td>%s</td><td>%s</td>"
1104 "<td>%s</td><td>&nbsp;</td><td>&nbsp;</td>"
1105 /* bytes: in, out */
1106 "<td>%s</td><td>%s</td>"
1107 "",
Willy Tarreau708c4162019-10-09 10:19:16 +02001108 (flags & STAT_SHLGNDS)?"</u>":"",
William Lallemand74c24fb2016-11-21 17:18:36 +01001109 U2H(stats[ST_F_SCUR].u.u32), U2H(stats[ST_F_SMAX].u.u32), U2H(stats[ST_F_SLIM].u.u32),
1110 U2H(stats[ST_F_STOT].u.u64), U2H(stats[ST_F_BIN].u.u64), U2H(stats[ST_F_BOUT].u.u64));
1111
1112 chunk_appendf(out,
1113 /* denied: req, resp */
1114 "<td>%s</td><td>%s</td>"
1115 /* errors: request, connect, response */
1116 "<td>%s</td><td></td><td></td>"
1117 /* warnings: retries, redispatches */
1118 "<td></td><td></td>"
1119 /* server status: reflect listener status */
1120 "<td class=ac>%s</td>"
1121 /* rest of server: nothing */
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001122 "<td class=ac colspan=8></td>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001123 "",
1124 U2H(stats[ST_F_DREQ].u.u64), U2H(stats[ST_F_DRESP].u.u64),
1125 U2H(stats[ST_F_EREQ].u.u64),
1126 field_str(stats, ST_F_STATUS));
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001127
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001128 if (flags & STAT_SHMODULES) {
1129 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001130 chunk_appendf(out, "<td>");
1131
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001132 if (stats_px_get_cap(mod->domain_flags) & STATS_PX_CAP_LI) {
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001133 chunk_appendf(out,
1134 "<u>%s<div class=tips><table class=det>",
1135 mod->name);
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001136 for (j = 0; j < mod->stats_count; ++j) {
1137 chunk_appendf(out,
1138 "<tr><th>%s</th><td>%s</td></tr>",
1139 mod->stats[j].desc, field_to_html_str(&stats[ST_F_TOTAL_FIELDS + i]));
1140 ++i;
1141 }
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001142 chunk_appendf(out, "</table></div></u>");
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001143 } else {
1144 i += mod->stats_count;
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001145 }
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001146
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001147 chunk_appendf(out, "</td>");
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001148 }
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001149 }
1150
1151 chunk_appendf(out, "</tr>");
William Lallemand74c24fb2016-11-21 17:18:36 +01001152 }
1153 else if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_SV) {
1154 const char *style;
1155
1156 /* determine the style to use depending on the server's state,
1157 * its health and weight. There isn't a 1-to-1 mapping between
1158 * state and styles for the cases where the server is (still)
1159 * up. The reason is that we don't want to report nolb and
1160 * drain with the same color.
1161 */
1162
1163 if (strcmp(field_str(stats, ST_F_STATUS), "DOWN") == 0 ||
1164 strcmp(field_str(stats, ST_F_STATUS), "DOWN (agent)") == 0) {
1165 style = "down";
1166 }
Florian Apolloner39272c22021-03-30 13:28:35 +02001167 else if (strncmp(field_str(stats, ST_F_STATUS), "DOWN ", strlen("DOWN ")) == 0) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001168 style = "going_up";
1169 }
Daniel Corbettb4285172020-03-28 12:35:50 -04001170 else if (strcmp(field_str(stats, ST_F_STATUS), "DRAIN") == 0) {
1171 style = "draining";
1172 }
Florian Apolloner39272c22021-03-30 13:28:35 +02001173 else if (strncmp(field_str(stats, ST_F_STATUS), "NOLB ", strlen("NOLB ")) == 0) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001174 style = "going_down";
1175 }
1176 else if (strcmp(field_str(stats, ST_F_STATUS), "NOLB") == 0) {
1177 style = "nolb";
1178 }
1179 else if (strcmp(field_str(stats, ST_F_STATUS), "no check") == 0) {
1180 style = "no_check";
1181 }
1182 else if (!stats[ST_F_CHKFAIL].type ||
1183 stats[ST_F_CHECK_HEALTH].u.u32 == stats[ST_F_CHECK_RISE].u.u32 + stats[ST_F_CHECK_FALL].u.u32 - 1) {
1184 /* no check or max health = UP */
1185 if (stats[ST_F_WEIGHT].u.u32)
1186 style = "up";
1187 else
1188 style = "draining";
1189 }
1190 else {
1191 style = "going_down";
1192 }
1193
Willy Tarreau7b524852020-08-11 10:26:36 +02001194 if (strncmp(field_str(stats, ST_F_STATUS), "MAINT", 5) == 0)
William Lallemand74c24fb2016-11-21 17:18:36 +01001195 chunk_appendf(out, "<tr class=\"maintain\">");
1196 else
1197 chunk_appendf(out,
1198 "<tr class=\"%s_%s\">",
1199 (stats[ST_F_BCK].u.u32) ? "backup" : "active", style);
1200
1201
Willy Tarreauab02b3f2019-10-09 11:11:46 +02001202 if (flags & STAT_ADMIN)
William Lallemand74c24fb2016-11-21 17:18:36 +01001203 chunk_appendf(out,
David Harrigand3db35a2016-12-30 12:12:49 +00001204 "<td><input class='%s-checkbox' type=\"checkbox\" name=\"s\" value=\"%s\"></td>",
1205 field_str(stats, ST_F_PXNAME),
William Lallemand74c24fb2016-11-21 17:18:36 +01001206 field_str(stats, ST_F_SVNAME));
1207
1208 chunk_appendf(out,
1209 "<td class=ac><a name=\"%s/%s\"></a>%s"
1210 "<a class=lfsb href=\"#%s/%s\">%s</a>"
1211 "",
1212 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME),
Willy Tarreau708c4162019-10-09 10:19:16 +02001213 (flags & STAT_SHLGNDS) ? "<u>" : "",
William Lallemand74c24fb2016-11-21 17:18:36 +01001214 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME), field_str(stats, ST_F_SVNAME));
1215
Willy Tarreau708c4162019-10-09 10:19:16 +02001216 if (flags & STAT_SHLGNDS) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001217 chunk_appendf(out, "<div class=tips>");
1218
Willy Tarreau90807112020-02-25 08:16:33 +01001219 if (isdigit((unsigned char)*field_str(stats, ST_F_ADDR)))
William Lallemand74c24fb2016-11-21 17:18:36 +01001220 chunk_appendf(out, "IPv4: %s, ", field_str(stats, ST_F_ADDR));
1221 else if (*field_str(stats, ST_F_ADDR) == '[')
1222 chunk_appendf(out, "IPv6: %s, ", field_str(stats, ST_F_ADDR));
1223 else if (*field_str(stats, ST_F_ADDR))
1224 chunk_appendf(out, "%s, ", field_str(stats, ST_F_ADDR));
1225
1226 /* id */
Aurelien DARRAGON745ce8e2022-11-17 16:34:07 +01001227 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 +01001228
1229 /* cookie */
1230 if (stats[ST_F_COOKIE].type) {
1231 chunk_appendf(out, ", cookie: '");
1232 chunk_initstr(&src, field_str(stats, ST_F_COOKIE));
1233 chunk_htmlencode(out, &src);
1234 chunk_appendf(out, "'");
1235 }
1236
1237 chunk_appendf(out, "</div>");
1238 }
1239
1240 chunk_appendf(out,
1241 /* queue : current, max, limit */
1242 "%s</td><td>%s</td><td>%s</td><td>%s</td>"
1243 /* sessions rate : current, max, limit */
1244 "<td>%s</td><td>%s</td><td></td>"
1245 "",
Willy Tarreau708c4162019-10-09 10:19:16 +02001246 (flags & STAT_SHLGNDS) ? "</u>" : "",
William Lallemand74c24fb2016-11-21 17:18:36 +01001247 U2H(stats[ST_F_QCUR].u.u32), U2H(stats[ST_F_QMAX].u.u32), LIM2A(stats[ST_F_QLIMIT].u.u32, "-"),
1248 U2H(stats[ST_F_RATE].u.u32), U2H(stats[ST_F_RATE_MAX].u.u32));
1249
1250 chunk_appendf(out,
1251 /* sessions: current, max, limit, total */
Willy Tarreauf21d17b2019-09-08 09:24:56 +02001252 "<td><u>%s<div class=tips>"
1253 "<table class=det>"
1254 "<tr><th>Current active connections:</th><td>%s</td></tr>"
Willy Tarreau3bb617c2020-06-29 13:51:05 +02001255 "<tr><th>Current used connections:</th><td>%s</td></tr>"
Willy Tarreauf21d17b2019-09-08 09:24:56 +02001256 "<tr><th>Current idle connections:</th><td>%s</td></tr>"
Willy Tarreau3bb617c2020-06-29 13:51:05 +02001257 "<tr><th>- unsafe:</th><td>%s</td></tr>"
1258 "<tr><th>- safe:</th><td>%s</td></tr>"
Willy Tarreaua9fcecb2020-06-29 15:38:53 +02001259 "<tr><th>Estimated need of connections:</th><td>%s</td></tr>"
Willy Tarreauf21d17b2019-09-08 09:24:56 +02001260 "<tr><th>Active connections limit:</th><td>%s</td></tr>"
1261 "<tr><th>Idle connections limit:</th><td>%s</td></tr>"
1262 "</table></div></u>"
1263 "</td><td>%s</td><td>%s</td>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001264 "<td><u>%s<div class=tips><table class=det>"
1265 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
1266 "",
Willy Tarreauf21d17b2019-09-08 09:24:56 +02001267 U2H(stats[ST_F_SCUR].u.u32),
Willy Tarreau3bb617c2020-06-29 13:51:05 +02001268 U2H(stats[ST_F_SCUR].u.u32),
1269 U2H(stats[ST_F_USED_CONN_CUR].u.u32),
1270 U2H(stats[ST_F_SRV_ICUR].u.u32),
1271 U2H(stats[ST_F_IDLE_CONN_CUR].u.u32),
1272 U2H(stats[ST_F_SAFE_CONN_CUR].u.u32),
Willy Tarreaua9fcecb2020-06-29 15:38:53 +02001273 U2H(stats[ST_F_NEED_CONN_EST].u.u32),
Willy Tarreau3bb617c2020-06-29 13:51:05 +02001274
Willy Tarreauf21d17b2019-09-08 09:24:56 +02001275 LIM2A(stats[ST_F_SLIM].u.u32, "-"),
1276 stats[ST_F_SRV_ILIM].type ? U2H(stats[ST_F_SRV_ILIM].u.u32) : "-",
1277 U2H(stats[ST_F_SMAX].u.u32), LIM2A(stats[ST_F_SLIM].u.u32, "-"),
William Lallemand74c24fb2016-11-21 17:18:36 +01001278 U2H(stats[ST_F_STOT].u.u64),
1279 U2H(stats[ST_F_STOT].u.u64));
1280
1281 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
1282 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001283 chunk_appendf(out,
Willy Tarreauf1573842018-12-14 11:35:36 +01001284 "<tr><th>New connections:</th><td>%s</td></tr>"
1285 "<tr><th>Reused connections:</th><td>%s</td><td>(%d%%)</td></tr>"
Marcin Deranek3c27dda2020-05-15 18:32:51 +02001286 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001287 "<tr><th>- HTTP 1xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
1288 "<tr><th>- HTTP 2xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
1289 "<tr><th>- HTTP 3xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
1290 "<tr><th>- HTTP 4xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
1291 "<tr><th>- HTTP 5xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
1292 "<tr><th>- other responses:</th><td>%s</td><td>(%d%%)</td></tr>"
Willy Tarreau1b0f85e2018-05-28 15:12:40 +02001293 "<tr><th>Failed hdr rewrites:</th><td>%s</td></tr>"
Christopher Faulet0159ee42019-12-16 14:40:39 +01001294 "<tr><th>Internal error:</th><td>%s</td></tr>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001295 "",
Willy Tarreauf1573842018-12-14 11:35:36 +01001296 U2H(stats[ST_F_CONNECT].u.u64),
1297 U2H(stats[ST_F_REUSE].u.u64),
1298 (stats[ST_F_CONNECT].u.u64 + stats[ST_F_REUSE].u.u64) ?
1299 (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 +02001300 U2H(stats[ST_F_REQ_TOT].u.u64),
1301 U2H(stats[ST_F_HRSP_1XX].u.u64), stats[ST_F_REQ_TOT].u.u64 ?
1302 (int)(100 * stats[ST_F_HRSP_1XX].u.u64 / stats[ST_F_REQ_TOT].u.u64) : 0,
1303 U2H(stats[ST_F_HRSP_2XX].u.u64), stats[ST_F_REQ_TOT].u.u64 ?
1304 (int)(100 * stats[ST_F_HRSP_2XX].u.u64 / stats[ST_F_REQ_TOT].u.u64) : 0,
1305 U2H(stats[ST_F_HRSP_3XX].u.u64), stats[ST_F_REQ_TOT].u.u64 ?
1306 (int)(100 * stats[ST_F_HRSP_3XX].u.u64 / stats[ST_F_REQ_TOT].u.u64) : 0,
1307 U2H(stats[ST_F_HRSP_4XX].u.u64), stats[ST_F_REQ_TOT].u.u64 ?
1308 (int)(100 * stats[ST_F_HRSP_4XX].u.u64 / stats[ST_F_REQ_TOT].u.u64) : 0,
1309 U2H(stats[ST_F_HRSP_5XX].u.u64), stats[ST_F_REQ_TOT].u.u64 ?
1310 (int)(100 * stats[ST_F_HRSP_5XX].u.u64 / stats[ST_F_REQ_TOT].u.u64) : 0,
1311 U2H(stats[ST_F_HRSP_OTHER].u.u64), stats[ST_F_REQ_TOT].u.u64 ?
1312 (int)(100 * stats[ST_F_HRSP_OTHER].u.u64 / stats[ST_F_REQ_TOT].u.u64) : 0,
Christopher Faulet0159ee42019-12-16 14:40:39 +01001313 U2H(stats[ST_F_WREW].u.u64),
1314 U2H(stats[ST_F_EINT].u.u64));
William Lallemand74c24fb2016-11-21 17:18:36 +01001315 }
1316
Christopher Faulet0d1c2a62019-11-08 14:59:51 +01001317 chunk_appendf(out, "<tr><th colspan=3>Max / Avg over last 1024 success. conn.</th></tr>");
1318 chunk_appendf(out, "<tr><th>- Queue time:</th><td>%s / %s</td><td>ms</td></tr>",
1319 U2H(stats[ST_F_QT_MAX].u.u32), U2H(stats[ST_F_QTIME].u.u32));
1320 chunk_appendf(out, "<tr><th>- Connect time:</th><td>%s / %s</td><td>ms</td></tr>",
1321 U2H(stats[ST_F_CT_MAX].u.u32), U2H(stats[ST_F_CTIME].u.u32));
William Lallemand74c24fb2016-11-21 17:18:36 +01001322 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
Christopher Faulet0d1c2a62019-11-08 14:59:51 +01001323 chunk_appendf(out, "<tr><th>- Responses time:</th><td>%s / %s</td><td>ms</td></tr>",
1324 U2H(stats[ST_F_RT_MAX].u.u32), U2H(stats[ST_F_RTIME].u.u32));
1325 chunk_appendf(out, "<tr><th>- Total time:</th><td>%s / %s</td><td>ms</td></tr>",
1326 U2H(stats[ST_F_TT_MAX].u.u32), U2H(stats[ST_F_TTIME].u.u32));
William Lallemand74c24fb2016-11-21 17:18:36 +01001327
1328 chunk_appendf(out,
1329 "</table></div></u></td>"
1330 /* sessions: lbtot, last */
1331 "<td>%s</td><td>%s</td>",
1332 U2H(stats[ST_F_LBTOT].u.u64),
1333 human_time(stats[ST_F_LASTSESS].u.s32, 1));
1334
1335 chunk_appendf(out,
1336 /* bytes : in, out */
1337 "<td>%s</td><td>%s</td>"
1338 /* denied: req, resp */
1339 "<td></td><td>%s</td>"
1340 /* errors : request, connect */
1341 "<td></td><td>%s</td>"
1342 /* errors : response */
1343 "<td><u>%s<div class=tips>Connection resets during transfers: %lld client, %lld server</div></u></td>"
1344 /* warnings: retries, redispatches */
1345 "<td>%lld</td><td>%lld</td>"
1346 "",
1347 U2H(stats[ST_F_BIN].u.u64), U2H(stats[ST_F_BOUT].u.u64),
1348 U2H(stats[ST_F_DRESP].u.u64),
1349 U2H(stats[ST_F_ECON].u.u64),
1350 U2H(stats[ST_F_ERESP].u.u64),
1351 (long long)stats[ST_F_CLI_ABRT].u.u64,
1352 (long long)stats[ST_F_SRV_ABRT].u.u64,
1353 (long long)stats[ST_F_WRETR].u.u64,
1354 (long long)stats[ST_F_WREDIS].u.u64);
1355
1356 /* status, last change */
1357 chunk_appendf(out, "<td class=ac>");
1358
1359 /* FIXME!!!!
1360 * LASTCHG should contain the last change for *this* server and must be computed
1361 * properly above, as was done below, ie: this server if maint, otherwise ref server
1362 * if tracking. Note that ref is either local or remote depending on tracking.
1363 */
1364
1365
Willy Tarreau7b524852020-08-11 10:26:36 +02001366 if (strncmp(field_str(stats, ST_F_STATUS), "MAINT", 5) == 0) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001367 chunk_appendf(out, "%s MAINT", human_time(stats[ST_F_LASTCHG].u.u32, 1));
1368 }
Willy Tarreau7b524852020-08-11 10:26:36 +02001369 else if (strcmp(field_str(stats, ST_F_STATUS), "no check") == 0) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001370 chunk_strcat(out, "<i>no check</i>");
1371 }
1372 else {
1373 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 +02001374 if (strncmp(field_str(stats, ST_F_STATUS), "DOWN", 4) == 0) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001375 if (stats[ST_F_CHECK_HEALTH].u.u32)
1376 chunk_strcat(out, " &uarr;");
1377 }
1378 else if (stats[ST_F_CHECK_HEALTH].u.u32 < stats[ST_F_CHECK_RISE].u.u32 + stats[ST_F_CHECK_FALL].u.u32 - 1)
1379 chunk_strcat(out, " &darr;");
1380 }
1381
Willy Tarreau7b524852020-08-11 10:26:36 +02001382 if (strncmp(field_str(stats, ST_F_STATUS), "DOWN", 4) == 0 &&
William Lallemand74c24fb2016-11-21 17:18:36 +01001383 stats[ST_F_AGENT_STATUS].type && !stats[ST_F_AGENT_HEALTH].u.u32) {
1384 chunk_appendf(out,
1385 "</td><td class=ac><u> %s",
1386 field_str(stats, ST_F_AGENT_STATUS));
1387
1388 if (stats[ST_F_AGENT_CODE].type)
1389 chunk_appendf(out, "/%d", stats[ST_F_AGENT_CODE].u.u32);
1390
1391 if (stats[ST_F_AGENT_DURATION].type)
1392 chunk_appendf(out, " in %lums", (long)stats[ST_F_AGENT_DURATION].u.u64);
1393
1394 chunk_appendf(out, "<div class=tips>%s", field_str(stats, ST_F_AGENT_DESC));
1395
1396 if (*field_str(stats, ST_F_LAST_AGT)) {
1397 chunk_appendf(out, ": ");
1398 chunk_initstr(&src, field_str(stats, ST_F_LAST_AGT));
1399 chunk_htmlencode(out, &src);
1400 }
1401 chunk_appendf(out, "</div></u>");
1402 }
1403 else if (stats[ST_F_CHECK_STATUS].type) {
1404 chunk_appendf(out,
1405 "</td><td class=ac><u> %s",
1406 field_str(stats, ST_F_CHECK_STATUS));
1407
1408 if (stats[ST_F_CHECK_CODE].type)
1409 chunk_appendf(out, "/%d", stats[ST_F_CHECK_CODE].u.u32);
1410
1411 if (stats[ST_F_CHECK_DURATION].type)
1412 chunk_appendf(out, " in %lums", (long)stats[ST_F_CHECK_DURATION].u.u64);
1413
1414 chunk_appendf(out, "<div class=tips>%s", field_str(stats, ST_F_CHECK_DESC));
1415
1416 if (*field_str(stats, ST_F_LAST_CHK)) {
1417 chunk_appendf(out, ": ");
1418 chunk_initstr(&src, field_str(stats, ST_F_LAST_CHK));
1419 chunk_htmlencode(out, &src);
1420 }
1421 chunk_appendf(out, "</div></u>");
1422 }
1423 else
1424 chunk_appendf(out, "</td><td>");
1425
1426 chunk_appendf(out,
Willy Tarreaubd715102020-10-23 22:44:30 +02001427 /* weight / uweight */
1428 "</td><td class=ac>%d/%d</td>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001429 /* act, bck */
1430 "<td class=ac>%s</td><td class=ac>%s</td>"
1431 "",
Willy Tarreaubd715102020-10-23 22:44:30 +02001432 stats[ST_F_WEIGHT].u.u32, stats[ST_F_UWEIGHT].u.u32,
William Lallemand74c24fb2016-11-21 17:18:36 +01001433 stats[ST_F_BCK].u.u32 ? "-" : "Y",
1434 stats[ST_F_BCK].u.u32 ? "Y" : "-");
1435
1436 /* check failures: unique, fatal, down time */
1437 if (strcmp(field_str(stats, ST_F_STATUS), "MAINT (resolution)") == 0) {
1438 chunk_appendf(out, "<td class=ac colspan=3>resolution</td>");
1439 }
1440 else if (stats[ST_F_CHKFAIL].type) {
1441 chunk_appendf(out, "<td><u>%lld", (long long)stats[ST_F_CHKFAIL].u.u64);
1442
1443 if (stats[ST_F_HANAFAIL].type)
1444 chunk_appendf(out, "/%lld", (long long)stats[ST_F_HANAFAIL].u.u64);
1445
1446 chunk_appendf(out,
1447 "<div class=tips>Failed Health Checks%s</div></u></td>"
1448 "<td>%lld</td><td>%s</td>"
1449 "",
1450 stats[ST_F_HANAFAIL].type ? "/Health Analyses" : "",
1451 (long long)stats[ST_F_CHKDOWN].u.u64, human_time(stats[ST_F_DOWNTIME].u.u32, 1));
1452 }
1453 else if (strcmp(field_str(stats, ST_F_STATUS), "MAINT") != 0 && field_format(stats, ST_F_TRACKED) == FF_STR) {
1454 /* tracking a server (hence inherited maint would appear as "MAINT (via...)" */
1455 chunk_appendf(out,
1456 "<td class=ac colspan=3><a class=lfsb href=\"#%s\">via %s</a></td>",
1457 field_str(stats, ST_F_TRACKED), field_str(stats, ST_F_TRACKED));
1458 }
1459 else
1460 chunk_appendf(out, "<td colspan=3></td>");
1461
1462 /* throttle */
1463 if (stats[ST_F_THROTTLE].type)
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001464 chunk_appendf(out, "<td class=ac>%d %%</td>\n", stats[ST_F_THROTTLE].u.u32);
William Lallemand74c24fb2016-11-21 17:18:36 +01001465 else
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001466 chunk_appendf(out, "<td class=ac>-</td>");
1467
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001468 if (flags & STAT_SHMODULES) {
1469 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001470 chunk_appendf(out, "<td>");
1471
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001472 if (stats_px_get_cap(mod->domain_flags) & STATS_PX_CAP_SRV) {
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001473 chunk_appendf(out,
1474 "<u>%s<div class=tips><table class=det>",
1475 mod->name);
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001476 for (j = 0; j < mod->stats_count; ++j) {
1477 chunk_appendf(out,
1478 "<tr><th>%s</th><td>%s</td></tr>",
1479 mod->stats[j].desc, field_to_html_str(&stats[ST_F_TOTAL_FIELDS + i]));
1480 ++i;
1481 }
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001482 chunk_appendf(out, "</table></div></u>");
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001483 } else {
1484 i += mod->stats_count;
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001485 }
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001486
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001487 chunk_appendf(out, "</td>");
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001488 }
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001489 }
1490
1491 chunk_appendf(out, "</tr>\n");
William Lallemand74c24fb2016-11-21 17:18:36 +01001492 }
1493 else if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_BE) {
1494 chunk_appendf(out, "<tr class=\"backend\">");
Willy Tarreauab02b3f2019-10-09 11:11:46 +02001495 if (flags & STAT_ADMIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001496 /* Column sub-heading for Enable or Disable server */
1497 chunk_appendf(out, "<td></td>");
1498 }
1499 chunk_appendf(out,
1500 "<td class=ac>"
1501 /* name */
1502 "%s<a name=\"%s/Backend\"></a>"
1503 "<a class=lfsb href=\"#%s/Backend\">Backend</a>"
1504 "",
Willy Tarreau708c4162019-10-09 10:19:16 +02001505 (flags & STAT_SHLGNDS)?"<u>":"",
William Lallemand74c24fb2016-11-21 17:18:36 +01001506 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_PXNAME));
1507
Willy Tarreau708c4162019-10-09 10:19:16 +02001508 if (flags & STAT_SHLGNDS) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001509 /* balancing */
1510 chunk_appendf(out, "<div class=tips>balancing: %s",
1511 field_str(stats, ST_F_ALGO));
1512
1513 /* cookie */
1514 if (stats[ST_F_COOKIE].type) {
1515 chunk_appendf(out, ", cookie: '");
1516 chunk_initstr(&src, field_str(stats, ST_F_COOKIE));
1517 chunk_htmlencode(out, &src);
1518 chunk_appendf(out, "'");
1519 }
1520 chunk_appendf(out, "</div>");
1521 }
1522
1523 chunk_appendf(out,
1524 "%s</td>"
1525 /* queue : current, max */
1526 "<td>%s</td><td>%s</td><td></td>"
1527 /* sessions rate : current, max, limit */
1528 "<td>%s</td><td>%s</td><td></td>"
1529 "",
Willy Tarreau708c4162019-10-09 10:19:16 +02001530 (flags & STAT_SHLGNDS)?"</u>":"",
William Lallemand74c24fb2016-11-21 17:18:36 +01001531 U2H(stats[ST_F_QCUR].u.u32), U2H(stats[ST_F_QMAX].u.u32),
1532 U2H(stats[ST_F_RATE].u.u32), U2H(stats[ST_F_RATE_MAX].u.u32));
1533
1534 chunk_appendf(out,
1535 /* sessions: current, max, limit, total */
1536 "<td>%s</td><td>%s</td><td>%s</td>"
1537 "<td><u>%s<div class=tips><table class=det>"
1538 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
1539 "",
Willy Tarreau8e0f1752016-12-12 15:07:29 +01001540 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 +01001541 U2H(stats[ST_F_STOT].u.u64),
1542 U2H(stats[ST_F_STOT].u.u64));
1543
1544 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
1545 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0) {
1546 chunk_appendf(out,
Willy Tarreauf1573842018-12-14 11:35:36 +01001547 "<tr><th>New connections:</th><td>%s</td></tr>"
1548 "<tr><th>Reused connections:</th><td>%s</td><td>(%d%%)</td></tr>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001549 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
1550 "<tr><th>- HTTP 1xx responses:</th><td>%s</td></tr>"
1551 "<tr><th>- HTTP 2xx responses:</th><td>%s</td></tr>"
1552 "<tr><th>&nbsp;&nbsp;Compressed 2xx:</th><td>%s</td><td>(%d%%)</td></tr>"
1553 "<tr><th>- HTTP 3xx responses:</th><td>%s</td></tr>"
1554 "<tr><th>- HTTP 4xx responses:</th><td>%s</td></tr>"
1555 "<tr><th>- HTTP 5xx responses:</th><td>%s</td></tr>"
1556 "<tr><th>- other responses:</th><td>%s</td></tr>"
Willy Tarreaua1214a52018-12-14 14:00:25 +01001557 "<tr><th>Cache lookups:</th><td>%s</td></tr>"
1558 "<tr><th>Cache hits:</th><td>%s</td><td>(%d%%)</td></tr>"
Willy Tarreau1b0f85e2018-05-28 15:12:40 +02001559 "<tr><th>Failed hdr rewrites:</th><td>%s</td></tr>"
Christopher Faulet0159ee42019-12-16 14:40:39 +01001560 "<tr><th>Internal errors:</th><td>%s</td></tr>"
Christopher Faulet0d1c2a62019-11-08 14:59:51 +01001561 "",
Willy Tarreauf1573842018-12-14 11:35:36 +01001562 U2H(stats[ST_F_CONNECT].u.u64),
1563 U2H(stats[ST_F_REUSE].u.u64),
1564 (stats[ST_F_CONNECT].u.u64 + stats[ST_F_REUSE].u.u64) ?
1565 (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 +01001566 U2H(stats[ST_F_REQ_TOT].u.u64),
1567 U2H(stats[ST_F_HRSP_1XX].u.u64),
1568 U2H(stats[ST_F_HRSP_2XX].u.u64),
1569 U2H(stats[ST_F_COMP_RSP].u.u64),
1570 stats[ST_F_HRSP_2XX].u.u64 ?
1571 (int)(100 * stats[ST_F_COMP_RSP].u.u64 / stats[ST_F_HRSP_2XX].u.u64) : 0,
1572 U2H(stats[ST_F_HRSP_3XX].u.u64),
1573 U2H(stats[ST_F_HRSP_4XX].u.u64),
1574 U2H(stats[ST_F_HRSP_5XX].u.u64),
Willy Tarreaufeead3a2018-12-14 13:48:44 +01001575 U2H(stats[ST_F_HRSP_OTHER].u.u64),
Willy Tarreaua1214a52018-12-14 14:00:25 +01001576 U2H(stats[ST_F_CACHE_LOOKUPS].u.u64),
1577 U2H(stats[ST_F_CACHE_HITS].u.u64),
1578 stats[ST_F_CACHE_LOOKUPS].u.u64 ?
1579 (int)(100 * stats[ST_F_CACHE_HITS].u.u64 / stats[ST_F_CACHE_LOOKUPS].u.u64) : 0,
Christopher Faulet0159ee42019-12-16 14:40:39 +01001580 U2H(stats[ST_F_WREW].u.u64),
1581 U2H(stats[ST_F_EINT].u.u64));
William Lallemand74c24fb2016-11-21 17:18:36 +01001582 }
1583
Christopher Faulet0d1c2a62019-11-08 14:59:51 +01001584 chunk_appendf(out, "<tr><th colspan=3>Max / Avg over last 1024 success. conn.</th></tr>");
1585 chunk_appendf(out, "<tr><th>- Queue time:</th><td>%s / %s</td><td>ms</td></tr>",
1586 U2H(stats[ST_F_QT_MAX].u.u32), U2H(stats[ST_F_QTIME].u.u32));
1587 chunk_appendf(out, "<tr><th>- Connect time:</th><td>%s / %s</td><td>ms</td></tr>",
1588 U2H(stats[ST_F_CT_MAX].u.u32), U2H(stats[ST_F_CTIME].u.u32));
William Lallemand74c24fb2016-11-21 17:18:36 +01001589 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
Christopher Faulet0d1c2a62019-11-08 14:59:51 +01001590 chunk_appendf(out, "<tr><th>- Responses time:</th><td>%s / %s</td><td>ms</td></tr>",
1591 U2H(stats[ST_F_RT_MAX].u.u32), U2H(stats[ST_F_RTIME].u.u32));
1592 chunk_appendf(out, "<tr><th>- Total time:</th><td>%s / %s</td><td>ms</td></tr>",
1593 U2H(stats[ST_F_TT_MAX].u.u32), U2H(stats[ST_F_TTIME].u.u32));
William Lallemand74c24fb2016-11-21 17:18:36 +01001594
1595 chunk_appendf(out,
1596 "</table></div></u></td>"
1597 /* sessions: lbtot, last */
1598 "<td>%s</td><td>%s</td>"
1599 /* bytes: in */
1600 "<td>%s</td>"
1601 "",
1602 U2H(stats[ST_F_LBTOT].u.u64),
1603 human_time(stats[ST_F_LASTSESS].u.s32, 1),
1604 U2H(stats[ST_F_BIN].u.u64));
1605
1606 chunk_appendf(out,
1607 /* bytes:out + compression stats (via hover): comp_in, comp_out, comp_byp */
1608 "<td>%s%s<div class=tips><table class=det>"
1609 "<tr><th>Response bytes in:</th><td>%s</td></tr>"
1610 "<tr><th>Compression in:</th><td>%s</td></tr>"
1611 "<tr><th>Compression out:</th><td>%s</td><td>(%d%%)</td></tr>"
1612 "<tr><th>Compression bypass:</th><td>%s</td></tr>"
1613 "<tr><th>Total bytes saved:</th><td>%s</td><td>(%d%%)</td></tr>"
1614 "</table></div>%s</td>",
1615 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "<u>":"",
1616 U2H(stats[ST_F_BOUT].u.u64),
1617 U2H(stats[ST_F_BOUT].u.u64),
1618 U2H(stats[ST_F_COMP_IN].u.u64),
1619 U2H(stats[ST_F_COMP_OUT].u.u64),
1620 stats[ST_F_COMP_IN].u.u64 ? (int)(stats[ST_F_COMP_OUT].u.u64 * 100 / stats[ST_F_COMP_IN].u.u64) : 0,
1621 U2H(stats[ST_F_COMP_BYP].u.u64),
1622 U2H(stats[ST_F_COMP_IN].u.u64 - stats[ST_F_COMP_OUT].u.u64),
1623 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,
1624 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "</u>":"");
1625
1626 chunk_appendf(out,
1627 /* denied: req, resp */
1628 "<td>%s</td><td>%s</td>"
1629 /* errors : request, connect */
1630 "<td></td><td>%s</td>"
1631 /* errors : response */
1632 "<td><u>%s<div class=tips>Connection resets during transfers: %lld client, %lld server</div></u></td>"
1633 /* warnings: retries, redispatches */
1634 "<td>%lld</td><td>%lld</td>"
1635 /* backend status: reflect backend status (up/down): we display UP
1636 * if the backend has known working servers or if it has no server at
1637 * all (eg: for stats). Then we display the total weight, number of
1638 * active and backups. */
Willy Tarreaubd715102020-10-23 22:44:30 +02001639 "<td class=ac>%s %s</td><td class=ac>&nbsp;</td><td class=ac>%d/%d</td>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001640 "<td class=ac>%d</td><td class=ac>%d</td>"
1641 "",
1642 U2H(stats[ST_F_DREQ].u.u64), U2H(stats[ST_F_DRESP].u.u64),
1643 U2H(stats[ST_F_ECON].u.u64),
1644 U2H(stats[ST_F_ERESP].u.u64),
1645 (long long)stats[ST_F_CLI_ABRT].u.u64,
1646 (long long)stats[ST_F_SRV_ABRT].u.u64,
1647 (long long)stats[ST_F_WRETR].u.u64, (long long)stats[ST_F_WREDIS].u.u64,
1648 human_time(stats[ST_F_LASTCHG].u.u32, 1),
1649 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 +02001650 stats[ST_F_WEIGHT].u.u32, stats[ST_F_UWEIGHT].u.u32,
William Lallemand74c24fb2016-11-21 17:18:36 +01001651 stats[ST_F_ACT].u.u32, stats[ST_F_BCK].u.u32);
1652
1653 chunk_appendf(out,
1654 /* rest of backend: nothing, down transitions, total downtime, throttle */
1655 "<td class=ac>&nbsp;</td><td>%d</td>"
1656 "<td>%s</td>"
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001657 "<td></td>",
William Lallemand74c24fb2016-11-21 17:18:36 +01001658 stats[ST_F_CHKDOWN].u.u32,
1659 stats[ST_F_DOWNTIME].type ? human_time(stats[ST_F_DOWNTIME].u.u32, 1) : "&nbsp;");
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001660
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001661 if (flags & STAT_SHMODULES) {
1662 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001663 chunk_appendf(out, "<td>");
1664
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001665 if (stats_px_get_cap(mod->domain_flags) & STATS_PX_CAP_BE) {
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001666 chunk_appendf(out,
1667 "<u>%s<div class=tips><table class=det>",
1668 mod->name);
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001669 for (j = 0; j < mod->stats_count; ++j) {
1670 chunk_appendf(out,
1671 "<tr><th>%s</th><td>%s</td></tr>",
1672 mod->stats[j].desc, field_to_html_str(&stats[ST_F_TOTAL_FIELDS + i]));
1673 ++i;
1674 }
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001675 chunk_appendf(out, "</table></div></u>");
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001676 } else {
1677 i += mod->stats_count;
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001678 }
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001679
1680 chunk_appendf(out, "</td>");
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001681 }
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001682 }
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001683
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001684 chunk_appendf(out, "</tr>");
William Lallemand74c24fb2016-11-21 17:18:36 +01001685 }
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001686
William Lallemand74c24fb2016-11-21 17:18:36 +01001687 return 1;
1688}
1689
Amaury Denoyelle97323c92020-10-02 18:32:01 +02001690int stats_dump_one_line(const struct field *stats, size_t stats_count,
1691 struct appctx *appctx)
William Lallemand74c24fb2016-11-21 17:18:36 +01001692{
Willy Tarreau91cefca2022-05-03 17:08:29 +02001693 struct show_stat_ctx *ctx = appctx->svcctx;
Simon Horman05ee2132017-01-04 09:37:25 +01001694 int ret;
1695
Willy Tarreau91cefca2022-05-03 17:08:29 +02001696 if (ctx->flags & STAT_FMT_HTML)
Christopher Fauleta8b76842022-12-16 15:08:36 +01001697 ret = stats_dump_fields_html(&trash_chunk, stats, ctx);
Willy Tarreau91cefca2022-05-03 17:08:29 +02001698 else if (ctx->flags & STAT_FMT_TYPED)
Christopher Fauleta8b76842022-12-16 15:08:36 +01001699 ret = stats_dump_fields_typed(&trash_chunk, stats, stats_count, ctx);
Willy Tarreau91cefca2022-05-03 17:08:29 +02001700 else if (ctx->flags & STAT_FMT_JSON)
Christopher Fauleta8b76842022-12-16 15:08:36 +01001701 ret = stats_dump_fields_json(&trash_chunk, stats, stats_count, ctx);
William Lallemand74c24fb2016-11-21 17:18:36 +01001702 else
Christopher Fauleta8b76842022-12-16 15:08:36 +01001703 ret = stats_dump_fields_csv(&trash_chunk, stats, stats_count, ctx);
Simon Horman05ee2132017-01-04 09:37:25 +01001704
Simon Horman05ee2132017-01-04 09:37:25 +01001705 return ret;
William Lallemand74c24fb2016-11-21 17:18:36 +01001706}
1707
William Dauchy0ef54392021-01-17 18:27:45 +01001708/* Fill <stats> with the frontend statistics. <stats> is preallocated array of
Christopher Fauletd808f172021-01-22 17:33:22 +01001709 * length <len>. If <selected_field> is != NULL, only fill this one. The length
1710 * of the array must be at least ST_F_TOTAL_FIELDS. If this length is less than
1711 * this value, or if the selected field is not implemented for frontends, the
1712 * function returns 0, otherwise, it returns 1.
William Lallemand74c24fb2016-11-21 17:18:36 +01001713 */
William Dauchy0ef54392021-01-17 18:27:45 +01001714int stats_fill_fe_stats(struct proxy *px, struct field *stats, int len,
1715 enum stat_field *selected_field)
William Lallemand74c24fb2016-11-21 17:18:36 +01001716{
William Dauchy0ef54392021-01-17 18:27:45 +01001717 enum stat_field current_field = (selected_field != NULL ? *selected_field : 0);
William Dauchy0ef54392021-01-17 18:27:45 +01001718
William Lallemand74c24fb2016-11-21 17:18:36 +01001719 if (len < ST_F_TOTAL_FIELDS)
1720 return 0;
1721
William Dauchy0ef54392021-01-17 18:27:45 +01001722 for (; current_field < ST_F_TOTAL_FIELDS; current_field++) {
Christopher Faulet8596bfb2021-01-25 15:16:41 +01001723 struct field metric = { 0 };
1724
William Dauchy0ef54392021-01-17 18:27:45 +01001725 switch (current_field) {
1726 case ST_F_PXNAME:
1727 metric = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
1728 break;
1729 case ST_F_SVNAME:
1730 metric = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, "FRONTEND");
1731 break;
1732 case ST_F_MODE:
1733 metric = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
1734 break;
1735 case ST_F_SCUR:
1736 metric = mkf_u32(0, px->feconn);
1737 break;
1738 case ST_F_SMAX:
1739 metric = mkf_u32(FN_MAX, px->fe_counters.conn_max);
1740 break;
1741 case ST_F_SLIM:
1742 metric = mkf_u32(FO_CONFIG|FN_LIMIT, px->maxconn);
1743 break;
1744 case ST_F_STOT:
1745 metric = mkf_u64(FN_COUNTER, px->fe_counters.cum_sess);
1746 break;
1747 case ST_F_BIN:
1748 metric = mkf_u64(FN_COUNTER, px->fe_counters.bytes_in);
1749 break;
1750 case ST_F_BOUT:
1751 metric = mkf_u64(FN_COUNTER, px->fe_counters.bytes_out);
1752 break;
1753 case ST_F_DREQ:
1754 metric = mkf_u64(FN_COUNTER, px->fe_counters.denied_req);
1755 break;
1756 case ST_F_DRESP:
1757 metric = mkf_u64(FN_COUNTER, px->fe_counters.denied_resp);
1758 break;
1759 case ST_F_EREQ:
1760 metric = mkf_u64(FN_COUNTER, px->fe_counters.failed_req);
1761 break;
1762 case ST_F_DCON:
1763 metric = mkf_u64(FN_COUNTER, px->fe_counters.denied_conn);
1764 break;
1765 case ST_F_DSES:
1766 metric = mkf_u64(FN_COUNTER, px->fe_counters.denied_sess);
1767 break;
Aurelien DARRAGONcddec0a2022-09-09 15:58:57 +02001768 case ST_F_STATUS: {
1769 const char *state;
1770
1771 if (px->flags & (PR_FL_DISABLED|PR_FL_STOPPED))
1772 state = "STOP";
1773 else if (px->flags & PR_FL_PAUSED)
1774 state = "PAUSED";
1775 else
1776 state = "OPEN";
1777 metric = mkf_str(FO_STATUS, state);
William Dauchy0ef54392021-01-17 18:27:45 +01001778 break;
Aurelien DARRAGONcddec0a2022-09-09 15:58:57 +02001779 }
William Dauchy0ef54392021-01-17 18:27:45 +01001780 case ST_F_PID:
Willy Tarreaue8422bf2021-06-15 09:08:18 +02001781 metric = mkf_u32(FO_KEY, 1);
William Dauchy0ef54392021-01-17 18:27:45 +01001782 break;
1783 case ST_F_IID:
1784 metric = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
1785 break;
1786 case ST_F_SID:
1787 metric = mkf_u32(FO_KEY|FS_SERVICE, 0);
1788 break;
1789 case ST_F_TYPE:
1790 metric = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_FE);
1791 break;
1792 case ST_F_RATE:
1793 metric = mkf_u32(FN_RATE, read_freq_ctr(&px->fe_sess_per_sec));
1794 break;
1795 case ST_F_RATE_LIM:
1796 metric = mkf_u32(FO_CONFIG|FN_LIMIT, px->fe_sps_lim);
1797 break;
1798 case ST_F_RATE_MAX:
1799 metric = mkf_u32(FN_MAX, px->fe_counters.sps_max);
1800 break;
1801 case ST_F_WREW:
1802 metric = mkf_u64(FN_COUNTER, px->fe_counters.failed_rewrites);
1803 break;
1804 case ST_F_EINT:
1805 metric = mkf_u64(FN_COUNTER, px->fe_counters.internal_errors);
1806 break;
1807 case ST_F_HRSP_1XX:
William Dauchy2107a0f2021-01-22 21:09:48 +01001808 if (px->mode == PR_MODE_HTTP)
1809 metric = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[1]);
William Dauchy0ef54392021-01-17 18:27:45 +01001810 break;
1811 case ST_F_HRSP_2XX:
William Dauchy2107a0f2021-01-22 21:09:48 +01001812 if (px->mode == PR_MODE_HTTP)
1813 metric = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[2]);
William Dauchy0ef54392021-01-17 18:27:45 +01001814 break;
1815 case ST_F_HRSP_3XX:
William Dauchy2107a0f2021-01-22 21:09:48 +01001816 if (px->mode == PR_MODE_HTTP)
1817 metric = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[3]);
William Dauchy0ef54392021-01-17 18:27:45 +01001818 break;
1819 case ST_F_HRSP_4XX:
William Dauchy2107a0f2021-01-22 21:09:48 +01001820 if (px->mode == PR_MODE_HTTP)
1821 metric = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[4]);
William Dauchy0ef54392021-01-17 18:27:45 +01001822 break;
1823 case ST_F_HRSP_5XX:
William Dauchy2107a0f2021-01-22 21:09:48 +01001824 if (px->mode == PR_MODE_HTTP)
1825 metric = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[5]);
William Dauchy0ef54392021-01-17 18:27:45 +01001826 break;
1827 case ST_F_HRSP_OTHER:
William Dauchy2107a0f2021-01-22 21:09:48 +01001828 if (px->mode == PR_MODE_HTTP)
1829 metric = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[0]);
William Dauchy0ef54392021-01-17 18:27:45 +01001830 break;
1831 case ST_F_INTERCEPTED:
William Dauchy2107a0f2021-01-22 21:09:48 +01001832 if (px->mode == PR_MODE_HTTP)
1833 metric = mkf_u64(FN_COUNTER, px->fe_counters.intercepted_req);
William Dauchy0ef54392021-01-17 18:27:45 +01001834 break;
1835 case ST_F_CACHE_LOOKUPS:
William Dauchy2107a0f2021-01-22 21:09:48 +01001836 if (px->mode == PR_MODE_HTTP)
1837 metric = mkf_u64(FN_COUNTER, px->fe_counters.p.http.cache_lookups);
William Dauchy0ef54392021-01-17 18:27:45 +01001838 break;
1839 case ST_F_CACHE_HITS:
William Dauchy2107a0f2021-01-22 21:09:48 +01001840 if (px->mode == PR_MODE_HTTP)
1841 metric = mkf_u64(FN_COUNTER, px->fe_counters.p.http.cache_hits);
William Dauchy0ef54392021-01-17 18:27:45 +01001842 break;
1843 case ST_F_REQ_RATE:
1844 metric = mkf_u32(FN_RATE, read_freq_ctr(&px->fe_req_per_sec));
1845 break;
1846 case ST_F_REQ_RATE_MAX:
1847 metric = mkf_u32(FN_MAX, px->fe_counters.p.http.rps_max);
1848 break;
Frédéric Lécaille9969adb2023-01-18 11:52:21 +01001849 case ST_F_REQ_TOT: {
1850 int i;
1851 uint64_t total_req;
1852 size_t nb_reqs =
1853 sizeof(px->fe_counters.p.http.cum_req) / sizeof(*px->fe_counters.p.http.cum_req);
1854
1855 total_req = 0;
1856 for (i = 0; i < nb_reqs; i++)
1857 total_req += px->fe_counters.p.http.cum_req[i];
1858 metric = mkf_u64(FN_COUNTER, total_req);
William Dauchy0ef54392021-01-17 18:27:45 +01001859 break;
Frédéric Lécaille9969adb2023-01-18 11:52:21 +01001860 }
William Dauchy0ef54392021-01-17 18:27:45 +01001861 case ST_F_COMP_IN:
Olivier Houcharddea25f52023-04-06 00:33:01 +02001862 metric = mkf_u64(FN_COUNTER, px->fe_counters.comp_in[COMP_DIR_RES]);
William Dauchy0ef54392021-01-17 18:27:45 +01001863 break;
1864 case ST_F_COMP_OUT:
Olivier Houcharddea25f52023-04-06 00:33:01 +02001865 metric = mkf_u64(FN_COUNTER, px->fe_counters.comp_out[COMP_DIR_RES]);
William Dauchy0ef54392021-01-17 18:27:45 +01001866 break;
1867 case ST_F_COMP_BYP:
Olivier Houcharddea25f52023-04-06 00:33:01 +02001868 metric = mkf_u64(FN_COUNTER, px->fe_counters.comp_byp[COMP_DIR_RES]);
William Dauchy0ef54392021-01-17 18:27:45 +01001869 break;
1870 case ST_F_COMP_RSP:
1871 metric = mkf_u64(FN_COUNTER, px->fe_counters.p.http.comp_rsp);
1872 break;
1873 case ST_F_CONN_RATE:
1874 metric = mkf_u32(FN_RATE, read_freq_ctr(&px->fe_conn_per_sec));
1875 break;
1876 case ST_F_CONN_RATE_MAX:
1877 metric = mkf_u32(FN_MAX, px->fe_counters.cps_max);
1878 break;
1879 case ST_F_CONN_TOT:
1880 metric = mkf_u64(FN_COUNTER, px->fe_counters.cum_conn);
1881 break;
Frédéric Lécaille9969adb2023-01-18 11:52:21 +01001882 case ST_F_SESS_OTHER: {
1883 int i;
1884 uint64_t total_sess;
1885 size_t nb_sess =
1886 sizeof(px->fe_counters.cum_sess_ver) / sizeof(*px->fe_counters.cum_sess_ver);
1887
1888 total_sess = px->fe_counters.cum_sess;
1889 for (i = 0; i < nb_sess; i++)
1890 total_sess -= px->fe_counters.cum_sess_ver[i];
Frédéric Lécailled97d1d72023-02-06 09:23:56 +01001891 total_sess = (int64_t)total_sess < 0 ? 0 : total_sess;
Frédéric Lécaille9969adb2023-01-18 11:52:21 +01001892 metric = mkf_u64(FN_COUNTER, total_sess);
1893 break;
1894 }
1895 case ST_F_H1SESS:
1896 metric = mkf_u64(FN_COUNTER, px->fe_counters.cum_sess_ver[0]);
1897 break;
1898 case ST_F_H2SESS:
1899 metric = mkf_u64(FN_COUNTER, px->fe_counters.cum_sess_ver[1]);
1900 break;
1901 case ST_F_H3SESS:
1902 metric = mkf_u64(FN_COUNTER, px->fe_counters.cum_sess_ver[2]);
1903 break;
1904 case ST_F_REQ_OTHER:
1905 metric = mkf_u64(FN_COUNTER, px->fe_counters.p.http.cum_req[0]);
1906 break;
1907 case ST_F_H1REQ:
1908 metric = mkf_u64(FN_COUNTER, px->fe_counters.p.http.cum_req[1]);
1909 break;
1910 case ST_F_H2REQ:
1911 metric = mkf_u64(FN_COUNTER, px->fe_counters.p.http.cum_req[2]);
1912 break;
1913 case ST_F_H3REQ:
1914 metric = mkf_u64(FN_COUNTER, px->fe_counters.p.http.cum_req[3]);
1915 break;
William Dauchy0ef54392021-01-17 18:27:45 +01001916 default:
Christopher Fauletd808f172021-01-22 17:33:22 +01001917 /* not used for frontends. If a specific metric
1918 * is requested, return an error. Otherwise continue.
1919 */
1920 if (selected_field != NULL)
1921 return 0;
1922 continue;
William Dauchy0ef54392021-01-17 18:27:45 +01001923 }
1924 stats[current_field] = metric;
1925 if (selected_field != NULL)
1926 break;
William Lallemand74c24fb2016-11-21 17:18:36 +01001927 }
William Lallemand74c24fb2016-11-21 17:18:36 +01001928 return 1;
1929}
1930
Christopher Fauleta8b76842022-12-16 15:08:36 +01001931/* Dumps a frontend's line to the local trash buffer for the current proxy <px>
1932 * and uses the state from stream connector <sc>. The caller is responsible for
1933 * clearing the local trash buffer if needed. Returns non-zero if it emits
1934 * anything, zero otherwise.
William Lallemand74c24fb2016-11-21 17:18:36 +01001935 */
Willy Tarreaucaff6312022-05-27 10:17:46 +02001936static int stats_dump_fe_stats(struct stconn *sc, struct proxy *px)
William Lallemand74c24fb2016-11-21 17:18:36 +01001937{
Willy Tarreaucaff6312022-05-27 10:17:46 +02001938 struct appctx *appctx = __sc_appctx(sc);
Willy Tarreau91cefca2022-05-03 17:08:29 +02001939 struct show_stat_ctx *ctx = appctx->svcctx;
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02001940 struct field *stats = stat_l[STATS_DOMAIN_PROXY];
1941 struct stats_module *mod;
1942 size_t stats_count = ST_F_TOTAL_FIELDS;
William Lallemand74c24fb2016-11-21 17:18:36 +01001943
1944 if (!(px->cap & PR_CAP_FE))
1945 return 0;
1946
Willy Tarreau91cefca2022-05-03 17:08:29 +02001947 if ((ctx->flags & STAT_BOUND) && !(ctx->type & (1 << STATS_TYPE_FE)))
William Lallemand74c24fb2016-11-21 17:18:36 +01001948 return 0;
1949
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02001950 memset(stats, 0, sizeof(struct field) * stat_count[STATS_DOMAIN_PROXY]);
1951
William Dauchy0ef54392021-01-17 18:27:45 +01001952 if (!stats_fill_fe_stats(px, stats, ST_F_TOTAL_FIELDS, NULL))
William Lallemand74c24fb2016-11-21 17:18:36 +01001953 return 0;
1954
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02001955 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
1956 void *counters;
1957
1958 if (!(stats_px_get_cap(mod->domain_flags) & STATS_PX_CAP_FE)) {
1959 stats_count += mod->stats_count;
1960 continue;
1961 }
1962
1963 counters = EXTRA_COUNTERS_GET(px->extra_counters_fe, mod);
1964 mod->fill_stats(counters, stats + stats_count);
1965 stats_count += mod->stats_count;
1966 }
1967
1968 return stats_dump_one_line(stats, stats_count, appctx);
William Lallemand74c24fb2016-11-21 17:18:36 +01001969}
1970
William Dauchy655e14e2021-02-14 23:22:54 +01001971/* Fill <stats> with the listener statistics. <stats> is preallocated array of
1972 * length <len>. The length of the array must be at least ST_F_TOTAL_FIELDS. If
1973 * this length is less then this value, the function returns 0, otherwise, it
1974 * returns 1. If selected_field is != NULL, only fill this one. <flags> can
1975 * take the value STAT_SHLGNDS.
William Lallemand74c24fb2016-11-21 17:18:36 +01001976 */
1977int stats_fill_li_stats(struct proxy *px, struct listener *l, int flags,
William Dauchy655e14e2021-02-14 23:22:54 +01001978 struct field *stats, int len, enum stat_field *selected_field)
William Lallemand74c24fb2016-11-21 17:18:36 +01001979{
William Dauchy655e14e2021-02-14 23:22:54 +01001980 enum stat_field current_field = (selected_field != NULL ? *selected_field : 0);
Willy Tarreau83061a82018-07-13 11:56:34 +02001981 struct buffer *out = get_trash_chunk();
William Lallemand74c24fb2016-11-21 17:18:36 +01001982
1983 if (len < ST_F_TOTAL_FIELDS)
1984 return 0;
1985
1986 if (!l->counters)
1987 return 0;
1988
1989 chunk_reset(out);
William Lallemand74c24fb2016-11-21 17:18:36 +01001990
William Dauchy655e14e2021-02-14 23:22:54 +01001991 for (; current_field < ST_F_TOTAL_FIELDS; current_field++) {
1992 struct field metric = { 0 };
William Lallemand74c24fb2016-11-21 17:18:36 +01001993
William Dauchy655e14e2021-02-14 23:22:54 +01001994 switch (current_field) {
1995 case ST_F_PXNAME:
1996 metric = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
1997 break;
1998 case ST_F_SVNAME:
1999 metric = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, l->name);
2000 break;
2001 case ST_F_MODE:
2002 metric = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
2003 break;
2004 case ST_F_SCUR:
2005 metric = mkf_u32(0, l->nbconn);
2006 break;
2007 case ST_F_SMAX:
2008 metric = mkf_u32(FN_MAX, l->counters->conn_max);
2009 break;
2010 case ST_F_SLIM:
Willy Tarreau758c69d2023-01-12 18:59:37 +01002011 metric = mkf_u32(FO_CONFIG|FN_LIMIT, l->bind_conf->maxconn);
William Dauchy655e14e2021-02-14 23:22:54 +01002012 break;
2013 case ST_F_STOT:
2014 metric = mkf_u64(FN_COUNTER, l->counters->cum_conn);
2015 break;
2016 case ST_F_BIN:
2017 metric = mkf_u64(FN_COUNTER, l->counters->bytes_in);
2018 break;
2019 case ST_F_BOUT:
2020 metric = mkf_u64(FN_COUNTER, l->counters->bytes_out);
2021 break;
2022 case ST_F_DREQ:
2023 metric = mkf_u64(FN_COUNTER, l->counters->denied_req);
2024 break;
2025 case ST_F_DRESP:
2026 metric = mkf_u64(FN_COUNTER, l->counters->denied_resp);
2027 break;
2028 case ST_F_EREQ:
2029 metric = mkf_u64(FN_COUNTER, l->counters->failed_req);
2030 break;
2031 case ST_F_DCON:
2032 metric = mkf_u64(FN_COUNTER, l->counters->denied_conn);
2033 break;
2034 case ST_F_DSES:
2035 metric = mkf_u64(FN_COUNTER, l->counters->denied_sess);
2036 break;
2037 case ST_F_STATUS:
William Dauchy3679d0c2021-02-14 23:22:55 +01002038 metric = mkf_str(FO_STATUS, li_status_st[get_li_status(l)]);
William Dauchy655e14e2021-02-14 23:22:54 +01002039 break;
2040 case ST_F_PID:
Willy Tarreaue8422bf2021-06-15 09:08:18 +02002041 metric = mkf_u32(FO_KEY, 1);
William Dauchy655e14e2021-02-14 23:22:54 +01002042 break;
2043 case ST_F_IID:
2044 metric = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
2045 break;
2046 case ST_F_SID:
2047 metric = mkf_u32(FO_KEY|FS_SERVICE, l->luid);
2048 break;
2049 case ST_F_TYPE:
2050 metric = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_SO);
2051 break;
2052 case ST_F_WREW:
2053 metric = mkf_u64(FN_COUNTER, l->counters->failed_rewrites);
2054 break;
2055 case ST_F_EINT:
2056 metric = mkf_u64(FN_COUNTER, l->counters->internal_errors);
2057 break;
2058 case ST_F_ADDR:
2059 if (flags & STAT_SHLGNDS) {
2060 char str[INET6_ADDRSTRLEN];
2061 int port;
William Lallemand74c24fb2016-11-21 17:18:36 +01002062
William Dauchy655e14e2021-02-14 23:22:54 +01002063 port = get_host_port(&l->rx.addr);
2064 switch (addr_to_str(&l->rx.addr, str, sizeof(str))) {
2065 case AF_INET:
2066 metric = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
2067 chunk_appendf(out, "%s:%d", str, port);
2068 break;
2069 case AF_INET6:
2070 metric = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
2071 chunk_appendf(out, "[%s]:%d", str, port);
2072 break;
2073 case AF_UNIX:
2074 metric = mkf_str(FO_CONFIG|FS_SERVICE, "unix");
2075 break;
2076 case -1:
2077 metric = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
2078 chunk_strcat(out, strerror(errno));
2079 break;
2080 default: /* address family not supported */
2081 break;
2082 }
2083 }
2084 break;
Willy Tarreau4cfb0012023-05-11 14:49:43 +02002085 case ST_F_PROTO:
2086 metric = mkf_str(FO_STATUS, l->rx.proto->name);
2087 break;
William Dauchy655e14e2021-02-14 23:22:54 +01002088 default:
2089 /* not used for listen. If a specific metric
2090 * is requested, return an error. Otherwise continue.
2091 */
2092 if (selected_field != NULL)
2093 return 0;
2094 continue;
William Lallemand74c24fb2016-11-21 17:18:36 +01002095 }
William Dauchy655e14e2021-02-14 23:22:54 +01002096 stats[current_field] = metric;
2097 if (selected_field != NULL)
2098 break;
William Lallemand74c24fb2016-11-21 17:18:36 +01002099 }
William Lallemand74c24fb2016-11-21 17:18:36 +01002100 return 1;
2101}
2102
Christopher Fauleta8b76842022-12-16 15:08:36 +01002103/* Dumps a line for listener <l> and proxy <px> to the local trash buffer and
2104 * uses the state from stream connector <sc>. The caller is responsible for
2105 * clearing the local trash buffer if needed. Returns non-zero if it emits
2106 * anything, zero otherwise.
William Lallemand74c24fb2016-11-21 17:18:36 +01002107 */
Willy Tarreaucaff6312022-05-27 10:17:46 +02002108static int stats_dump_li_stats(struct stconn *sc, struct proxy *px, struct listener *l)
William Lallemand74c24fb2016-11-21 17:18:36 +01002109{
Willy Tarreaucaff6312022-05-27 10:17:46 +02002110 struct appctx *appctx = __sc_appctx(sc);
Willy Tarreau91cefca2022-05-03 17:08:29 +02002111 struct show_stat_ctx *ctx = appctx->svcctx;
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02002112 struct field *stats = stat_l[STATS_DOMAIN_PROXY];
2113 struct stats_module *mod;
2114 size_t stats_count = ST_F_TOTAL_FIELDS;
2115
2116 memset(stats, 0, sizeof(struct field) * stat_count[STATS_DOMAIN_PROXY]);
William Lallemand74c24fb2016-11-21 17:18:36 +01002117
Willy Tarreau91cefca2022-05-03 17:08:29 +02002118 if (!stats_fill_li_stats(px, l, ctx->flags, stats,
William Dauchy655e14e2021-02-14 23:22:54 +01002119 ST_F_TOTAL_FIELDS, NULL))
William Lallemand74c24fb2016-11-21 17:18:36 +01002120 return 0;
2121
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02002122 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
2123 void *counters;
2124
2125 if (!(stats_px_get_cap(mod->domain_flags) & STATS_PX_CAP_LI)) {
2126 stats_count += mod->stats_count;
2127 continue;
2128 }
2129
2130 counters = EXTRA_COUNTERS_GET(l->extra_counters, mod);
2131 mod->fill_stats(counters, stats + stats_count);
2132 stats_count += mod->stats_count;
2133 }
2134
2135 return stats_dump_one_line(stats, stats_count, appctx);
William Lallemand74c24fb2016-11-21 17:18:36 +01002136}
2137
2138enum srv_stats_state {
2139 SRV_STATS_STATE_DOWN = 0,
2140 SRV_STATS_STATE_DOWN_AGENT,
2141 SRV_STATS_STATE_GOING_UP,
2142 SRV_STATS_STATE_UP_GOING_DOWN,
2143 SRV_STATS_STATE_UP,
2144 SRV_STATS_STATE_NOLB_GOING_DOWN,
2145 SRV_STATS_STATE_NOLB,
2146 SRV_STATS_STATE_DRAIN_GOING_DOWN,
2147 SRV_STATS_STATE_DRAIN,
2148 SRV_STATS_STATE_DRAIN_AGENT,
2149 SRV_STATS_STATE_NO_CHECK,
2150
2151 SRV_STATS_STATE_COUNT, /* Must be last */
2152};
2153
2154static const char *srv_hlt_st[SRV_STATS_STATE_COUNT] = {
2155 [SRV_STATS_STATE_DOWN] = "DOWN",
2156 [SRV_STATS_STATE_DOWN_AGENT] = "DOWN (agent)",
2157 [SRV_STATS_STATE_GOING_UP] = "DOWN %d/%d",
2158 [SRV_STATS_STATE_UP_GOING_DOWN] = "UP %d/%d",
2159 [SRV_STATS_STATE_UP] = "UP",
2160 [SRV_STATS_STATE_NOLB_GOING_DOWN] = "NOLB %d/%d",
2161 [SRV_STATS_STATE_NOLB] = "NOLB",
2162 [SRV_STATS_STATE_DRAIN_GOING_DOWN] = "DRAIN %d/%d",
2163 [SRV_STATS_STATE_DRAIN] = "DRAIN",
2164 [SRV_STATS_STATE_DRAIN_AGENT] = "DRAIN (agent)",
2165 [SRV_STATS_STATE_NO_CHECK] = "no check"
2166};
2167
William Dauchyd3a9a492021-01-25 17:29:03 +01002168/* Compute server state helper
William Lallemand74c24fb2016-11-21 17:18:36 +01002169 */
William Dauchyd3a9a492021-01-25 17:29:03 +01002170static void stats_fill_sv_stats_computestate(struct server *sv, struct server *ref,
2171 enum srv_stats_state *state)
William Lallemand74c24fb2016-11-21 17:18:36 +01002172{
Emeric Brun52a91d32017-08-31 14:41:55 +02002173 if (sv->cur_state == SRV_ST_RUNNING || sv->cur_state == SRV_ST_STARTING) {
William Lallemand74c24fb2016-11-21 17:18:36 +01002174 if ((ref->check.state & CHK_ST_ENABLED) &&
2175 (ref->check.health < ref->check.rise + ref->check.fall - 1)) {
William Dauchyd3a9a492021-01-25 17:29:03 +01002176 *state = SRV_STATS_STATE_UP_GOING_DOWN;
William Lallemand74c24fb2016-11-21 17:18:36 +01002177 } else {
William Dauchyd3a9a492021-01-25 17:29:03 +01002178 *state = SRV_STATS_STATE_UP;
William Lallemand74c24fb2016-11-21 17:18:36 +01002179 }
2180
Emeric Brun52a91d32017-08-31 14:41:55 +02002181 if (sv->cur_admin & SRV_ADMF_DRAIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +01002182 if (ref->agent.state & CHK_ST_ENABLED)
William Dauchyd3a9a492021-01-25 17:29:03 +01002183 *state = SRV_STATS_STATE_DRAIN_AGENT;
2184 else if (*state == SRV_STATS_STATE_UP_GOING_DOWN)
2185 *state = SRV_STATS_STATE_DRAIN_GOING_DOWN;
William Lallemand74c24fb2016-11-21 17:18:36 +01002186 else
William Dauchyd3a9a492021-01-25 17:29:03 +01002187 *state = SRV_STATS_STATE_DRAIN;
William Lallemand74c24fb2016-11-21 17:18:36 +01002188 }
2189
William Dauchyd3a9a492021-01-25 17:29:03 +01002190 if (*state == SRV_STATS_STATE_UP && !(ref->check.state & CHK_ST_ENABLED)) {
2191 *state = SRV_STATS_STATE_NO_CHECK;
William Lallemand74c24fb2016-11-21 17:18:36 +01002192 }
2193 }
Emeric Brun52a91d32017-08-31 14:41:55 +02002194 else if (sv->cur_state == SRV_ST_STOPPING) {
William Lallemand74c24fb2016-11-21 17:18:36 +01002195 if ((!(sv->check.state & CHK_ST_ENABLED) && !sv->track) ||
2196 (ref->check.health == ref->check.rise + ref->check.fall - 1)) {
William Dauchyd3a9a492021-01-25 17:29:03 +01002197 *state = SRV_STATS_STATE_NOLB;
William Lallemand74c24fb2016-11-21 17:18:36 +01002198 } else {
William Dauchyd3a9a492021-01-25 17:29:03 +01002199 *state = SRV_STATS_STATE_NOLB_GOING_DOWN;
William Lallemand74c24fb2016-11-21 17:18:36 +01002200 }
2201 }
2202 else { /* stopped */
2203 if ((ref->agent.state & CHK_ST_ENABLED) && !ref->agent.health) {
William Dauchyd3a9a492021-01-25 17:29:03 +01002204 *state = SRV_STATS_STATE_DOWN_AGENT;
William Lallemand74c24fb2016-11-21 17:18:36 +01002205 } else if ((ref->check.state & CHK_ST_ENABLED) && !ref->check.health) {
William Dauchyd3a9a492021-01-25 17:29:03 +01002206 *state = SRV_STATS_STATE_DOWN; /* DOWN */
William Lallemand74c24fb2016-11-21 17:18:36 +01002207 } else if ((ref->agent.state & CHK_ST_ENABLED) || (ref->check.state & CHK_ST_ENABLED)) {
William Dauchyd3a9a492021-01-25 17:29:03 +01002208 *state = SRV_STATS_STATE_GOING_UP;
William Lallemand74c24fb2016-11-21 17:18:36 +01002209 } else {
William Dauchyd3a9a492021-01-25 17:29:03 +01002210 *state = SRV_STATS_STATE_DOWN; /* DOWN, unchecked */
William Lallemand74c24fb2016-11-21 17:18:36 +01002211 }
2212 }
William Dauchyd3a9a492021-01-25 17:29:03 +01002213}
William Lallemand74c24fb2016-11-21 17:18:36 +01002214
William Dauchyd3a9a492021-01-25 17:29:03 +01002215/* Fill <stats> with the backend statistics. <stats> is preallocated array of
2216 * length <len>. If <selected_field> is != NULL, only fill this one. The length
2217 * of the array must be at least ST_F_TOTAL_FIELDS. If this length is less than
2218 * this value, or if the selected field is not implemented for servers, the
2219 * function returns 0, otherwise, it returns 1. <flags> can take the value
2220 * STAT_SHLGNDS.
2221 */
2222int stats_fill_sv_stats(struct proxy *px, struct server *sv, int flags,
2223 struct field *stats, int len,
2224 enum stat_field *selected_field)
2225{
2226 enum stat_field current_field = (selected_field != NULL ? *selected_field : 0);
2227 struct server *via = sv->track ? sv->track : sv;
2228 struct server *ref = via;
2229 enum srv_stats_state state = 0;
2230 char str[INET6_ADDRSTRLEN];
2231 struct buffer *out = get_trash_chunk();
2232 char *fld_status;
2233 long long srv_samples_counter;
2234 unsigned int srv_samples_window = TIME_STATS_SAMPLES;
William Lallemand74c24fb2016-11-21 17:18:36 +01002235
William Dauchyd3a9a492021-01-25 17:29:03 +01002236 if (len < ST_F_TOTAL_FIELDS)
2237 return 0;
William Lallemand74c24fb2016-11-21 17:18:36 +01002238
William Dauchyd3a9a492021-01-25 17:29:03 +01002239 chunk_reset(out);
William Lallemand74c24fb2016-11-21 17:18:36 +01002240
William Dauchyd3a9a492021-01-25 17:29:03 +01002241 /* compute state for later use */
2242 if (selected_field == NULL || *selected_field == ST_F_STATUS ||
2243 *selected_field == ST_F_CHECK_RISE || *selected_field == ST_F_CHECK_FALL ||
2244 *selected_field == ST_F_CHECK_HEALTH || *selected_field == ST_F_HANAFAIL) {
2245 /* we have "via" which is the tracked server as described in the configuration,
2246 * and "ref" which is the checked server and the end of the chain.
2247 */
2248 while (ref->track)
2249 ref = ref->track;
2250 stats_fill_sv_stats_computestate(sv, ref, &state);
William Lallemand74c24fb2016-11-21 17:18:36 +01002251 }
2252
William Dauchyd3a9a492021-01-25 17:29:03 +01002253 /* compue time values for later use */
2254 if (selected_field == NULL || *selected_field == ST_F_QTIME ||
2255 *selected_field == ST_F_CTIME || *selected_field == ST_F_RTIME ||
2256 *selected_field == ST_F_TTIME) {
2257 srv_samples_counter = (px->mode == PR_MODE_HTTP) ? sv->counters.p.http.cum_req : sv->counters.cum_lbconn;
2258 if (srv_samples_counter < TIME_STATS_SAMPLES && srv_samples_counter > 0)
2259 srv_samples_window = srv_samples_counter;
William Lallemand74c24fb2016-11-21 17:18:36 +01002260 }
2261
William Dauchyd3a9a492021-01-25 17:29:03 +01002262 for (; current_field < ST_F_TOTAL_FIELDS; current_field++) {
2263 struct field metric = { 0 };
William Lallemand74c24fb2016-11-21 17:18:36 +01002264
William Dauchyd3a9a492021-01-25 17:29:03 +01002265 switch (current_field) {
2266 case ST_F_PXNAME:
2267 metric = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
2268 break;
2269 case ST_F_SVNAME:
2270 metric = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, sv->id);
2271 break;
2272 case ST_F_MODE:
2273 metric = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
Christopher Faulet3888b8c2021-01-27 13:32:25 +01002274 break;
William Dauchyd3a9a492021-01-25 17:29:03 +01002275 case ST_F_QCUR:
Willy Tarreaua0570452021-06-18 09:30:30 +02002276 metric = mkf_u32(0, sv->queue.length);
William Dauchyd3a9a492021-01-25 17:29:03 +01002277 break;
2278 case ST_F_QMAX:
2279 metric = mkf_u32(FN_MAX, sv->counters.nbpend_max);
2280 break;
2281 case ST_F_SCUR:
2282 metric = mkf_u32(0, sv->cur_sess);
2283 break;
2284 case ST_F_SMAX:
2285 metric = mkf_u32(FN_MAX, sv->counters.cur_sess_max);
2286 break;
2287 case ST_F_SLIM:
2288 if (sv->maxconn)
2289 metric = mkf_u32(FO_CONFIG|FN_LIMIT, sv->maxconn);
2290 break;
2291 case ST_F_SRV_ICUR:
2292 metric = mkf_u32(0, sv->curr_idle_conns);
2293 break;
2294 case ST_F_SRV_ILIM:
2295 if (sv->max_idle_conns != -1)
2296 metric = mkf_u32(FO_CONFIG|FN_LIMIT, sv->max_idle_conns);
2297 break;
2298 case ST_F_STOT:
2299 metric = mkf_u64(FN_COUNTER, sv->counters.cum_sess);
2300 break;
2301 case ST_F_BIN:
2302 metric = mkf_u64(FN_COUNTER, sv->counters.bytes_in);
2303 break;
2304 case ST_F_BOUT:
2305 metric = mkf_u64(FN_COUNTER, sv->counters.bytes_out);
2306 break;
2307 case ST_F_DRESP:
2308 metric = mkf_u64(FN_COUNTER, sv->counters.denied_resp);
2309 break;
2310 case ST_F_ECON:
2311 metric = mkf_u64(FN_COUNTER, sv->counters.failed_conns);
2312 break;
2313 case ST_F_ERESP:
2314 metric = mkf_u64(FN_COUNTER, sv->counters.failed_resp);
2315 break;
2316 case ST_F_WRETR:
2317 metric = mkf_u64(FN_COUNTER, sv->counters.retries);
2318 break;
2319 case ST_F_WREDIS:
2320 metric = mkf_u64(FN_COUNTER, sv->counters.redispatches);
2321 break;
2322 case ST_F_WREW:
2323 metric = mkf_u64(FN_COUNTER, sv->counters.failed_rewrites);
2324 break;
2325 case ST_F_EINT:
2326 metric = mkf_u64(FN_COUNTER, sv->counters.internal_errors);
2327 break;
2328 case ST_F_CONNECT:
2329 metric = mkf_u64(FN_COUNTER, sv->counters.connect);
2330 break;
2331 case ST_F_REUSE:
2332 metric = mkf_u64(FN_COUNTER, sv->counters.reuse);
2333 break;
2334 case ST_F_IDLE_CONN_CUR:
2335 metric = mkf_u32(0, sv->curr_idle_nb);
2336 break;
2337 case ST_F_SAFE_CONN_CUR:
2338 metric = mkf_u32(0, sv->curr_safe_nb);
2339 break;
2340 case ST_F_USED_CONN_CUR:
2341 metric = mkf_u32(0, sv->curr_used_conns);
2342 break;
2343 case ST_F_NEED_CONN_EST:
2344 metric = mkf_u32(0, sv->est_need_conns);
2345 break;
2346 case ST_F_STATUS:
2347 fld_status = chunk_newstr(out);
2348 if (sv->cur_admin & SRV_ADMF_RMAINT)
2349 chunk_appendf(out, "MAINT (resolution)");
2350 else if (sv->cur_admin & SRV_ADMF_IMAINT)
2351 chunk_appendf(out, "MAINT (via %s/%s)", via->proxy->id, via->id);
2352 else if (sv->cur_admin & SRV_ADMF_MAINT)
2353 chunk_appendf(out, "MAINT");
2354 else
2355 chunk_appendf(out,
2356 srv_hlt_st[state],
2357 (ref->cur_state != SRV_ST_STOPPED) ? (ref->check.health - ref->check.rise + 1) : (ref->check.health),
2358 (ref->cur_state != SRV_ST_STOPPED) ? (ref->check.fall) : (ref->check.rise));
William Lallemand74c24fb2016-11-21 17:18:36 +01002359
William Dauchyd3a9a492021-01-25 17:29:03 +01002360 metric = mkf_str(FO_STATUS, fld_status);
2361 break;
2362 case ST_F_LASTCHG:
Willy Tarreau69530f52023-04-28 09:16:15 +02002363 metric = mkf_u32(FN_AGE, ns_to_sec(now_ns) - sv->last_change);
William Dauchyd3a9a492021-01-25 17:29:03 +01002364 break;
2365 case ST_F_WEIGHT:
2366 metric = mkf_u32(FN_AVG, (sv->cur_eweight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv);
2367 break;
2368 case ST_F_UWEIGHT:
2369 metric = mkf_u32(FN_AVG, sv->uweight);
2370 break;
2371 case ST_F_ACT:
2372 metric = mkf_u32(FO_STATUS, (sv->flags & SRV_F_BACKUP) ? 0 : 1);
2373 break;
2374 case ST_F_BCK:
2375 metric = mkf_u32(FO_STATUS, (sv->flags & SRV_F_BACKUP) ? 1 : 0);
2376 break;
2377 case ST_F_CHKFAIL:
2378 if (sv->check.state & CHK_ST_ENABLED)
2379 metric = mkf_u64(FN_COUNTER, sv->counters.failed_checks);
2380 break;
2381 case ST_F_CHKDOWN:
2382 if (sv->check.state & CHK_ST_ENABLED)
2383 metric = mkf_u64(FN_COUNTER, sv->counters.down_trans);
2384 break;
2385 case ST_F_DOWNTIME:
2386 if (sv->check.state & CHK_ST_ENABLED)
2387 metric = mkf_u32(FN_COUNTER, srv_downtime(sv));
2388 break;
2389 case ST_F_QLIMIT:
2390 if (sv->maxqueue)
2391 metric = mkf_u32(FO_CONFIG|FS_SERVICE, sv->maxqueue);
2392 break;
2393 case ST_F_PID:
Willy Tarreaue8422bf2021-06-15 09:08:18 +02002394 metric = mkf_u32(FO_KEY, 1);
William Dauchyd3a9a492021-01-25 17:29:03 +01002395 break;
2396 case ST_F_IID:
2397 metric = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
2398 break;
2399 case ST_F_SID:
2400 metric = mkf_u32(FO_KEY|FS_SERVICE, sv->puid);
2401 break;
Aurelien DARRAGON745ce8e2022-11-17 16:34:07 +01002402 case ST_F_SRID:
2403 metric = mkf_u32(FN_COUNTER, sv->rid);
2404 break;
William Dauchyd3a9a492021-01-25 17:29:03 +01002405 case ST_F_THROTTLE:
2406 if (sv->cur_state == SRV_ST_STARTING && !server_is_draining(sv))
2407 metric = mkf_u32(FN_AVG, server_throttle_rate(sv));
2408 break;
2409 case ST_F_LBTOT:
2410 metric = mkf_u64(FN_COUNTER, sv->counters.cum_lbconn);
2411 break;
2412 case ST_F_TRACKED:
2413 if (sv->track) {
2414 char *fld_track = chunk_newstr(out);
2415 chunk_appendf(out, "%s/%s", sv->track->proxy->id, sv->track->id);
2416 metric = mkf_str(FO_CONFIG|FN_NAME|FS_SERVICE, fld_track);
2417 }
2418 break;
2419 case ST_F_TYPE:
2420 metric = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_SV);
2421 break;
2422 case ST_F_RATE:
2423 metric = mkf_u32(FN_RATE, read_freq_ctr(&sv->sess_per_sec));
2424 break;
2425 case ST_F_RATE_MAX:
2426 metric = mkf_u32(FN_MAX, sv->counters.sps_max);
2427 break;
2428 case ST_F_CHECK_STATUS:
2429 if ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED) {
2430 const char *fld_chksts;
Marcin Deraneka8dbdf32020-05-15 20:02:40 +02002431
William Dauchyd3a9a492021-01-25 17:29:03 +01002432 fld_chksts = chunk_newstr(out);
2433 chunk_strcat(out, "* "); // for check in progress
2434 chunk_strcat(out, get_check_status_info(sv->check.status));
2435 if (!(sv->check.state & CHK_ST_INPROGRESS))
2436 fld_chksts += 2; // skip "* "
Willy Tarreau973a9372021-05-12 17:29:14 +02002437 metric = mkf_str(FN_OUTPUT, fld_chksts);
William Dauchyd3a9a492021-01-25 17:29:03 +01002438 }
2439 break;
2440 case ST_F_CHECK_CODE:
2441 if ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED &&
2442 sv->check.status >= HCHK_STATUS_L57DATA)
2443 metric = mkf_u32(FN_OUTPUT, sv->check.code);
2444 break;
2445 case ST_F_CHECK_DURATION:
2446 if ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED &&
2447 sv->check.status >= HCHK_STATUS_CHECKED)
Aurelien DARRAGONb118f2f2022-12-07 14:52:10 +01002448 metric = mkf_u64(FN_DURATION, MAX(sv->check.duration, 0));
William Dauchyd3a9a492021-01-25 17:29:03 +01002449 break;
2450 case ST_F_CHECK_DESC:
2451 if ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED)
2452 metric = mkf_str(FN_OUTPUT, get_check_status_description(sv->check.status));
2453 break;
2454 case ST_F_LAST_CHK:
2455 if ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED)
2456 metric = mkf_str(FN_OUTPUT, sv->check.desc);
2457 break;
2458 case ST_F_CHECK_RISE:
2459 if ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED)
2460 metric = mkf_u32(FO_CONFIG|FS_SERVICE, ref->check.rise);
2461 break;
2462 case ST_F_CHECK_FALL:
2463 if ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED)
2464 metric = mkf_u32(FO_CONFIG|FS_SERVICE, ref->check.fall);
2465 break;
2466 case ST_F_CHECK_HEALTH:
2467 if ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED)
2468 metric = mkf_u32(FO_CONFIG|FS_SERVICE, ref->check.health);
2469 break;
Christopher Faulet59bab612021-07-22 08:04:38 +02002470 case ST_F_AGENT_STATUS:
2471 if ((sv->agent.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED) {
2472 const char *fld_chksts;
2473
2474 fld_chksts = chunk_newstr(out);
2475 chunk_strcat(out, "* "); // for check in progress
2476 chunk_strcat(out, get_check_status_info(sv->agent.status));
2477 if (!(sv->agent.state & CHK_ST_INPROGRESS))
2478 fld_chksts += 2; // skip "* "
2479 metric = mkf_str(FN_OUTPUT, fld_chksts);
2480 }
2481 break;
2482 case ST_F_AGENT_CODE:
2483 if ((sv->agent.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED &&
2484 (sv->agent.status >= HCHK_STATUS_L57DATA))
2485 metric = mkf_u32(FN_OUTPUT, sv->agent.code);
2486 break;
2487 case ST_F_AGENT_DURATION:
2488 if ((sv->agent.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED)
2489 metric = mkf_u64(FN_DURATION, sv->agent.duration);
2490 break;
2491 case ST_F_AGENT_DESC:
2492 if ((sv->agent.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED)
2493 metric = mkf_str(FN_OUTPUT, get_check_status_description(sv->agent.status));
2494 break;
2495 case ST_F_LAST_AGT:
2496 if ((sv->agent.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED)
2497 metric = mkf_str(FN_OUTPUT, sv->agent.desc);
2498 break;
2499 case ST_F_AGENT_RISE:
2500 if ((sv->agent.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED)
2501 metric = mkf_u32(FO_CONFIG|FS_SERVICE, sv->agent.rise);
2502 break;
2503 case ST_F_AGENT_FALL:
2504 if ((sv->agent.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED)
2505 metric = mkf_u32(FO_CONFIG|FS_SERVICE, sv->agent.fall);
2506 break;
2507 case ST_F_AGENT_HEALTH:
2508 if ((sv->agent.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED)
2509 metric = mkf_u32(FO_CONFIG|FS_SERVICE, sv->agent.health);
2510 break;
William Dauchyd3a9a492021-01-25 17:29:03 +01002511 case ST_F_REQ_TOT:
2512 if (px->mode == PR_MODE_HTTP)
2513 metric = mkf_u64(FN_COUNTER, sv->counters.p.http.cum_req);
2514 break;
2515 case ST_F_HRSP_1XX:
2516 if (px->mode == PR_MODE_HTTP)
2517 metric = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[1]);
2518 break;
2519 case ST_F_HRSP_2XX:
2520 if (px->mode == PR_MODE_HTTP)
2521 metric = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[2]);
2522 break;
2523 case ST_F_HRSP_3XX:
2524 if (px->mode == PR_MODE_HTTP)
2525 metric = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[3]);
2526 break;
2527 case ST_F_HRSP_4XX:
2528 if (px->mode == PR_MODE_HTTP)
2529 metric = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[4]);
2530 break;
2531 case ST_F_HRSP_5XX:
2532 if (px->mode == PR_MODE_HTTP)
2533 metric = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[5]);
2534 break;
2535 case ST_F_HRSP_OTHER:
2536 if (px->mode == PR_MODE_HTTP)
2537 metric = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[0]);
2538 break;
2539 case ST_F_HANAFAIL:
2540 if (ref->observe)
2541 metric = mkf_u64(FN_COUNTER, sv->counters.failed_hana);
2542 break;
2543 case ST_F_CLI_ABRT:
2544 metric = mkf_u64(FN_COUNTER, sv->counters.cli_aborts);
2545 break;
2546 case ST_F_SRV_ABRT:
2547 metric = mkf_u64(FN_COUNTER, sv->counters.srv_aborts);
2548 break;
2549 case ST_F_LASTSESS:
2550 metric = mkf_s32(FN_AGE, srv_lastsession(sv));
2551 break;
2552 case ST_F_QTIME:
2553 metric = mkf_u32(FN_AVG, swrate_avg(sv->counters.q_time, srv_samples_window));
2554 break;
2555 case ST_F_CTIME:
2556 metric = mkf_u32(FN_AVG, swrate_avg(sv->counters.c_time, srv_samples_window));
2557 break;
2558 case ST_F_RTIME:
2559 metric = mkf_u32(FN_AVG, swrate_avg(sv->counters.d_time, srv_samples_window));
2560 break;
2561 case ST_F_TTIME:
2562 metric = mkf_u32(FN_AVG, swrate_avg(sv->counters.t_time, srv_samples_window));
2563 break;
2564 case ST_F_QT_MAX:
2565 metric = mkf_u32(FN_MAX, sv->counters.qtime_max);
2566 break;
2567 case ST_F_CT_MAX:
2568 metric = mkf_u32(FN_MAX, sv->counters.ctime_max);
2569 break;
2570 case ST_F_RT_MAX:
2571 metric = mkf_u32(FN_MAX, sv->counters.dtime_max);
2572 break;
2573 case ST_F_TT_MAX:
2574 metric = mkf_u32(FN_MAX, sv->counters.ttime_max);
2575 break;
2576 case ST_F_ADDR:
2577 if (flags & STAT_SHLGNDS) {
2578 switch (addr_to_str(&sv->addr, str, sizeof(str))) {
2579 case AF_INET:
2580 metric = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
2581 chunk_appendf(out, "%s:%d", str, sv->svc_port);
2582 break;
2583 case AF_INET6:
2584 metric = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
2585 chunk_appendf(out, "[%s]:%d", str, sv->svc_port);
2586 break;
2587 case AF_UNIX:
2588 metric = mkf_str(FO_CONFIG|FS_SERVICE, "unix");
2589 break;
2590 case -1:
2591 metric = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
2592 chunk_strcat(out, strerror(errno));
2593 break;
2594 default: /* address family not supported */
2595 break;
2596 }
2597 }
2598 break;
2599 case ST_F_COOKIE:
2600 if (flags & STAT_SHLGNDS && sv->cookie)
2601 metric = mkf_str(FO_CONFIG|FN_NAME|FS_SERVICE, sv->cookie);
2602 break;
2603 default:
2604 /* not used for servers. If a specific metric
2605 * is requested, return an error. Otherwise continue.
2606 */
2607 if (selected_field != NULL)
2608 return 0;
2609 continue;
William Lallemand74c24fb2016-11-21 17:18:36 +01002610 }
William Dauchyd3a9a492021-01-25 17:29:03 +01002611 stats[current_field] = metric;
2612 if (selected_field != NULL)
2613 break;
William Lallemand74c24fb2016-11-21 17:18:36 +01002614 }
William Lallemand74c24fb2016-11-21 17:18:36 +01002615 return 1;
2616}
2617
Christopher Fauleta8b76842022-12-16 15:08:36 +01002618/* Dumps a line for server <sv> and proxy <px> to the local trash vbuffer and
2619 * uses the state from stream connector <sc>, and server state <state>. The
2620 * caller is responsible for clearing the local trash buffer if needed. Returns
2621 * non-zero if it emits anything, zero otherwise.
William Lallemand74c24fb2016-11-21 17:18:36 +01002622 */
Willy Tarreaucaff6312022-05-27 10:17:46 +02002623static int stats_dump_sv_stats(struct stconn *sc, struct proxy *px, struct server *sv)
William Lallemand74c24fb2016-11-21 17:18:36 +01002624{
Willy Tarreaucaff6312022-05-27 10:17:46 +02002625 struct appctx *appctx = __sc_appctx(sc);
Willy Tarreau91cefca2022-05-03 17:08:29 +02002626 struct show_stat_ctx *ctx = appctx->svcctx;
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02002627 struct stats_module *mod;
2628 struct field *stats = stat_l[STATS_DOMAIN_PROXY];
2629 size_t stats_count = ST_F_TOTAL_FIELDS;
2630
2631 memset(stats, 0, sizeof(struct field) * stat_count[STATS_DOMAIN_PROXY]);
William Lallemand74c24fb2016-11-21 17:18:36 +01002632
Willy Tarreau91cefca2022-05-03 17:08:29 +02002633 if (!stats_fill_sv_stats(px, sv, ctx->flags, stats,
William Dauchyd3a9a492021-01-25 17:29:03 +01002634 ST_F_TOTAL_FIELDS, NULL))
William Lallemand74c24fb2016-11-21 17:18:36 +01002635 return 0;
2636
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02002637 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
2638 void *counters;
2639
2640 if (stats_get_domain(mod->domain_flags) != STATS_DOMAIN_PROXY)
2641 continue;
2642
2643 if (!(stats_px_get_cap(mod->domain_flags) & STATS_PX_CAP_SRV)) {
2644 stats_count += mod->stats_count;
2645 continue;
2646 }
2647
2648 counters = EXTRA_COUNTERS_GET(sv->extra_counters, mod);
2649 mod->fill_stats(counters, stats + stats_count);
2650 stats_count += mod->stats_count;
2651 }
2652
2653 return stats_dump_one_line(stats, stats_count, appctx);
William Lallemand74c24fb2016-11-21 17:18:36 +01002654}
2655
William Dauchyda3b4662021-01-25 17:29:01 +01002656/* Helper to compute srv values for a given backend
William Lallemand74c24fb2016-11-21 17:18:36 +01002657 */
William Dauchyda3b4662021-01-25 17:29:01 +01002658static void stats_fill_be_stats_computesrv(struct proxy *px, int *nbup, int *nbsrv, int *totuw)
William Lallemand74c24fb2016-11-21 17:18:36 +01002659{
William Dauchyda3b4662021-01-25 17:29:01 +01002660 int nbup_tmp, nbsrv_tmp, totuw_tmp;
Willy Tarreau2fbe6942020-10-23 18:02:54 +02002661 const struct server *srv;
William Lallemand74c24fb2016-11-21 17:18:36 +01002662
William Dauchyda3b4662021-01-25 17:29:01 +01002663 nbup_tmp = nbsrv_tmp = totuw_tmp = 0;
Willy Tarreaubd715102020-10-23 22:44:30 +02002664 for (srv = px->srv; srv; srv = srv->next) {
2665 if (srv->cur_state != SRV_ST_STOPPED) {
William Dauchyda3b4662021-01-25 17:29:01 +01002666 nbup_tmp++;
Willy Tarreaubd715102020-10-23 22:44:30 +02002667 if (srv_currently_usable(srv) &&
2668 (!px->srv_act ^ !(srv->flags & SRV_F_BACKUP)))
William Dauchyda3b4662021-01-25 17:29:01 +01002669 totuw_tmp += srv->uweight;
Willy Tarreau2fbe6942020-10-23 18:02:54 +02002670 }
William Dauchyda3b4662021-01-25 17:29:01 +01002671 nbsrv_tmp++;
Willy Tarreau2fbe6942020-10-23 18:02:54 +02002672 }
2673
Willy Tarreaubd715102020-10-23 22:44:30 +02002674 HA_RWLOCK_RDLOCK(LBPRM_LOCK, &px->lbprm.lock);
2675 if (!px->srv_act && px->lbprm.fbck)
William Dauchyda3b4662021-01-25 17:29:01 +01002676 totuw_tmp = px->lbprm.fbck->uweight;
Willy Tarreaubd715102020-10-23 22:44:30 +02002677 HA_RWLOCK_RDUNLOCK(LBPRM_LOCK, &px->lbprm.lock);
2678
William Dauchyda3b4662021-01-25 17:29:01 +01002679 /* use tmp variable then assign result to make gcc happy */
2680 *nbup = nbup_tmp;
2681 *nbsrv = nbsrv_tmp;
2682 *totuw = totuw_tmp;
2683}
Willy Tarreau2fbe6942020-10-23 18:02:54 +02002684
William Dauchyda3b4662021-01-25 17:29:01 +01002685/* Fill <stats> with the backend statistics. <stats> is preallocated array of
2686 * length <len>. If <selected_field> is != NULL, only fill this one. The length
2687 * of the array must be at least ST_F_TOTAL_FIELDS. If this length is less than
2688 * this value, or if the selected field is not implemented for backends, the
2689 * function returns 0, otherwise, it returns 1. <flags> can take the value
2690 * STAT_SHLGNDS.
2691 */
2692int stats_fill_be_stats(struct proxy *px, int flags, struct field *stats, int len,
2693 enum stat_field *selected_field)
2694{
2695 enum stat_field current_field = (selected_field != NULL ? *selected_field : 0);
2696 long long be_samples_counter;
2697 unsigned int be_samples_window = TIME_STATS_SAMPLES;
2698 struct buffer *out = get_trash_chunk();
2699 int nbup, nbsrv, totuw;
2700 char *fld;
William Lallemand74c24fb2016-11-21 17:18:36 +01002701
William Dauchyda3b4662021-01-25 17:29:01 +01002702 if (len < ST_F_TOTAL_FIELDS)
2703 return 0;
William Lallemand74c24fb2016-11-21 17:18:36 +01002704
William Dauchyda3b4662021-01-25 17:29:01 +01002705 nbup = nbsrv = totuw = 0;
2706 /* some srv values compute for later if we either select all fields or
2707 * need them for one of the mentioned ones */
2708 if (selected_field == NULL || *selected_field == ST_F_STATUS ||
2709 *selected_field == ST_F_UWEIGHT)
2710 stats_fill_be_stats_computesrv(px, &nbup, &nbsrv, &totuw);
William Lallemand74c24fb2016-11-21 17:18:36 +01002711
William Dauchyda3b4662021-01-25 17:29:01 +01002712 /* same here but specific to time fields */
2713 if (selected_field == NULL || *selected_field == ST_F_QTIME ||
2714 *selected_field == ST_F_CTIME || *selected_field == ST_F_RTIME ||
2715 *selected_field == ST_F_TTIME) {
2716 be_samples_counter = (px->mode == PR_MODE_HTTP) ? px->be_counters.p.http.cum_req : px->be_counters.cum_lbconn;
2717 if (be_samples_counter < TIME_STATS_SAMPLES && be_samples_counter > 0)
2718 be_samples_window = be_samples_counter;
William Lallemand74c24fb2016-11-21 17:18:36 +01002719 }
2720
William Dauchyda3b4662021-01-25 17:29:01 +01002721 for (; current_field < ST_F_TOTAL_FIELDS; current_field++) {
2722 struct field metric = { 0 };
William Lallemand74c24fb2016-11-21 17:18:36 +01002723
William Dauchyda3b4662021-01-25 17:29:01 +01002724 switch (current_field) {
2725 case ST_F_PXNAME:
2726 metric = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
2727 break;
2728 case ST_F_SVNAME:
2729 metric = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, "BACKEND");
2730 break;
2731 case ST_F_MODE:
2732 metric = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
2733 break;
2734 case ST_F_QCUR:
Willy Tarreau7f3c1df2021-06-18 09:22:21 +02002735 metric = mkf_u32(0, px->queue.length);
William Dauchyda3b4662021-01-25 17:29:01 +01002736 break;
2737 case ST_F_QMAX:
2738 metric = mkf_u32(FN_MAX, px->be_counters.nbpend_max);
2739 break;
2740 case ST_F_SCUR:
2741 metric = mkf_u32(0, px->beconn);
2742 break;
2743 case ST_F_SMAX:
2744 metric = mkf_u32(FN_MAX, px->be_counters.conn_max);
2745 break;
2746 case ST_F_SLIM:
2747 metric = mkf_u32(FO_CONFIG|FN_LIMIT, px->fullconn);
2748 break;
2749 case ST_F_STOT:
2750 metric = mkf_u64(FN_COUNTER, px->be_counters.cum_conn);
2751 break;
2752 case ST_F_BIN:
2753 metric = mkf_u64(FN_COUNTER, px->be_counters.bytes_in);
2754 break;
2755 case ST_F_BOUT:
2756 metric = mkf_u64(FN_COUNTER, px->be_counters.bytes_out);
2757 break;
2758 case ST_F_DREQ:
2759 metric = mkf_u64(FN_COUNTER, px->be_counters.denied_req);
2760 break;
2761 case ST_F_DRESP:
2762 metric = mkf_u64(FN_COUNTER, px->be_counters.denied_resp);
2763 break;
2764 case ST_F_ECON:
2765 metric = mkf_u64(FN_COUNTER, px->be_counters.failed_conns);
2766 break;
2767 case ST_F_ERESP:
2768 metric = mkf_u64(FN_COUNTER, px->be_counters.failed_resp);
2769 break;
2770 case ST_F_WRETR:
2771 metric = mkf_u64(FN_COUNTER, px->be_counters.retries);
2772 break;
2773 case ST_F_WREDIS:
2774 metric = mkf_u64(FN_COUNTER, px->be_counters.redispatches);
2775 break;
2776 case ST_F_WREW:
2777 metric = mkf_u64(FN_COUNTER, px->be_counters.failed_rewrites);
2778 break;
2779 case ST_F_EINT:
2780 metric = mkf_u64(FN_COUNTER, px->be_counters.internal_errors);
2781 break;
2782 case ST_F_CONNECT:
2783 metric = mkf_u64(FN_COUNTER, px->be_counters.connect);
2784 break;
2785 case ST_F_REUSE:
2786 metric = mkf_u64(FN_COUNTER, px->be_counters.reuse);
2787 break;
2788 case ST_F_STATUS:
2789 fld = chunk_newstr(out);
2790 chunk_appendf(out, "%s", (px->lbprm.tot_weight > 0 || !px->srv) ? "UP" : "DOWN");
2791 if (flags & (STAT_HIDE_MAINT|STAT_HIDE_DOWN))
2792 chunk_appendf(out, " (%d/%d)", nbup, nbsrv);
2793 metric = mkf_str(FO_STATUS, fld);
2794 break;
Cedric Paillet7d6644e2022-12-08 09:17:00 +00002795 case ST_F_AGG_SRV_CHECK_STATUS: // DEPRECATED
2796 case ST_F_AGG_SRV_STATUS:
William Dauchy42d7c402021-11-07 10:18:47 +01002797 metric = mkf_u32(FN_GAUGE, 0);
2798 break;
Cedric Paillete06e31e2022-12-08 09:17:01 +00002799 case ST_F_AGG_CHECK_STATUS:
2800 metric = mkf_u32(FN_GAUGE, 0);
2801 break;
William Dauchyda3b4662021-01-25 17:29:01 +01002802 case ST_F_WEIGHT:
2803 metric = mkf_u32(FN_AVG, (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv);
2804 break;
2805 case ST_F_UWEIGHT:
2806 metric = mkf_u32(FN_AVG, totuw);
2807 break;
2808 case ST_F_ACT:
2809 metric = mkf_u32(0, px->srv_act);
2810 break;
2811 case ST_F_BCK:
2812 metric = mkf_u32(0, px->srv_bck);
2813 break;
2814 case ST_F_CHKDOWN:
2815 metric = mkf_u64(FN_COUNTER, px->down_trans);
2816 break;
2817 case ST_F_LASTCHG:
Willy Tarreau69530f52023-04-28 09:16:15 +02002818 metric = mkf_u32(FN_AGE, ns_to_sec(now_ns) - px->last_change);
William Dauchyda3b4662021-01-25 17:29:01 +01002819 break;
2820 case ST_F_DOWNTIME:
2821 if (px->srv)
2822 metric = mkf_u32(FN_COUNTER, be_downtime(px));
2823 break;
2824 case ST_F_PID:
Willy Tarreaue8422bf2021-06-15 09:08:18 +02002825 metric = mkf_u32(FO_KEY, 1);
William Dauchyda3b4662021-01-25 17:29:01 +01002826 break;
2827 case ST_F_IID:
2828 metric = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
2829 break;
2830 case ST_F_SID:
2831 metric = mkf_u32(FO_KEY|FS_SERVICE, 0);
2832 break;
2833 case ST_F_LBTOT:
2834 metric = mkf_u64(FN_COUNTER, px->be_counters.cum_lbconn);
2835 break;
2836 case ST_F_TYPE:
2837 metric = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_BE);
2838 break;
2839 case ST_F_RATE:
2840 metric = mkf_u32(0, read_freq_ctr(&px->be_sess_per_sec));
2841 break;
2842 case ST_F_RATE_MAX:
2843 metric = mkf_u32(0, px->be_counters.sps_max);
2844 break;
2845 case ST_F_COOKIE:
2846 if (flags & STAT_SHLGNDS && px->cookie_name)
2847 metric = mkf_str(FO_CONFIG|FN_NAME|FS_SERVICE, px->cookie_name);
2848 break;
2849 case ST_F_ALGO:
2850 if (flags & STAT_SHLGNDS)
2851 metric = mkf_str(FO_CONFIG|FS_SERVICE, backend_lb_algo_str(px->lbprm.algo & BE_LB_ALGO));
2852 break;
2853 case ST_F_REQ_TOT:
2854 if (px->mode == PR_MODE_HTTP)
2855 metric = mkf_u64(FN_COUNTER, px->be_counters.p.http.cum_req);
2856 break;
2857 case ST_F_HRSP_1XX:
2858 if (px->mode == PR_MODE_HTTP)
2859 metric = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[1]);
2860 break;
2861 case ST_F_HRSP_2XX:
2862 if (px->mode == PR_MODE_HTTP)
2863 metric = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[2]);
2864 break;
2865 case ST_F_HRSP_3XX:
2866 if (px->mode == PR_MODE_HTTP)
2867 metric = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[3]);
2868 break;
2869 case ST_F_HRSP_4XX:
2870 if (px->mode == PR_MODE_HTTP)
2871 metric = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[4]);
2872 break;
2873 case ST_F_HRSP_5XX:
2874 if (px->mode == PR_MODE_HTTP)
2875 metric = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[5]);
2876 break;
2877 case ST_F_HRSP_OTHER:
2878 if (px->mode == PR_MODE_HTTP)
2879 metric = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[0]);
2880 break;
2881 case ST_F_CACHE_LOOKUPS:
2882 if (px->mode == PR_MODE_HTTP)
2883 metric = mkf_u64(FN_COUNTER, px->be_counters.p.http.cache_lookups);
2884 break;
2885 case ST_F_CACHE_HITS:
2886 if (px->mode == PR_MODE_HTTP)
2887 metric = mkf_u64(FN_COUNTER, px->be_counters.p.http.cache_hits);
2888 break;
2889 case ST_F_CLI_ABRT:
2890 metric = mkf_u64(FN_COUNTER, px->be_counters.cli_aborts);
2891 break;
2892 case ST_F_SRV_ABRT:
2893 metric = mkf_u64(FN_COUNTER, px->be_counters.srv_aborts);
2894 break;
2895 case ST_F_COMP_IN:
Olivier Houcharddea25f52023-04-06 00:33:01 +02002896 metric = mkf_u64(FN_COUNTER, px->be_counters.comp_in[COMP_DIR_RES]);
William Dauchyda3b4662021-01-25 17:29:01 +01002897 break;
2898 case ST_F_COMP_OUT:
Olivier Houcharddea25f52023-04-06 00:33:01 +02002899 metric = mkf_u64(FN_COUNTER, px->be_counters.comp_out[COMP_DIR_RES]);
William Dauchyda3b4662021-01-25 17:29:01 +01002900 break;
2901 case ST_F_COMP_BYP:
Olivier Houcharddea25f52023-04-06 00:33:01 +02002902 metric = mkf_u64(FN_COUNTER, px->be_counters.comp_byp[COMP_DIR_RES]);
William Dauchyda3b4662021-01-25 17:29:01 +01002903 break;
2904 case ST_F_COMP_RSP:
2905 metric = mkf_u64(FN_COUNTER, px->be_counters.p.http.comp_rsp);
2906 break;
2907 case ST_F_LASTSESS:
2908 metric = mkf_s32(FN_AGE, be_lastsession(px));
2909 break;
2910 case ST_F_QTIME:
2911 metric = mkf_u32(FN_AVG, swrate_avg(px->be_counters.q_time, be_samples_window));
2912 break;
2913 case ST_F_CTIME:
2914 metric = mkf_u32(FN_AVG, swrate_avg(px->be_counters.c_time, be_samples_window));
2915 break;
2916 case ST_F_RTIME:
2917 metric = mkf_u32(FN_AVG, swrate_avg(px->be_counters.d_time, be_samples_window));
2918 break;
2919 case ST_F_TTIME:
2920 metric = mkf_u32(FN_AVG, swrate_avg(px->be_counters.t_time, be_samples_window));
2921 break;
2922 case ST_F_QT_MAX:
2923 metric = mkf_u32(FN_MAX, px->be_counters.qtime_max);
2924 break;
2925 case ST_F_CT_MAX:
2926 metric = mkf_u32(FN_MAX, px->be_counters.ctime_max);
2927 break;
2928 case ST_F_RT_MAX:
2929 metric = mkf_u32(FN_MAX, px->be_counters.dtime_max);
2930 break;
2931 case ST_F_TT_MAX:
2932 metric = mkf_u32(FN_MAX, px->be_counters.ttime_max);
2933 break;
2934 default:
2935 /* not used for backends. If a specific metric
2936 * is requested, return an error. Otherwise continue.
2937 */
2938 if (selected_field != NULL)
2939 return 0;
2940 continue;
2941 }
2942 stats[current_field] = metric;
2943 if (selected_field != NULL)
2944 break;
2945 }
William Lallemand74c24fb2016-11-21 17:18:36 +01002946 return 1;
2947}
2948
Christopher Fauleta8b76842022-12-16 15:08:36 +01002949/* Dumps a line for backend <px> to the local trash buffer for and uses the
2950 * state from stream interface <si>. The caller is responsible for clearing the
2951 * local trash buffer if needed. Returns non-zero if it emits anything, zero
2952 * otherwise.
William Lallemand74c24fb2016-11-21 17:18:36 +01002953 */
Willy Tarreaucaff6312022-05-27 10:17:46 +02002954static int stats_dump_be_stats(struct stconn *sc, struct proxy *px)
William Lallemand74c24fb2016-11-21 17:18:36 +01002955{
Willy Tarreaucaff6312022-05-27 10:17:46 +02002956 struct appctx *appctx = __sc_appctx(sc);
Willy Tarreau91cefca2022-05-03 17:08:29 +02002957 struct show_stat_ctx *ctx = appctx->svcctx;
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02002958 struct field *stats = stat_l[STATS_DOMAIN_PROXY];
2959 struct stats_module *mod;
2960 size_t stats_count = ST_F_TOTAL_FIELDS;
William Lallemand74c24fb2016-11-21 17:18:36 +01002961
2962 if (!(px->cap & PR_CAP_BE))
2963 return 0;
2964
Willy Tarreau91cefca2022-05-03 17:08:29 +02002965 if ((ctx->flags & STAT_BOUND) && !(ctx->type & (1 << STATS_TYPE_BE)))
William Lallemand74c24fb2016-11-21 17:18:36 +01002966 return 0;
2967
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02002968 memset(stats, 0, sizeof(struct field) * stat_count[STATS_DOMAIN_PROXY]);
2969
Willy Tarreau91cefca2022-05-03 17:08:29 +02002970 if (!stats_fill_be_stats(px, ctx->flags, stats, ST_F_TOTAL_FIELDS, NULL))
William Lallemand74c24fb2016-11-21 17:18:36 +01002971 return 0;
2972
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02002973 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
2974 struct extra_counters *counters;
2975
2976 if (stats_get_domain(mod->domain_flags) != STATS_DOMAIN_PROXY)
2977 continue;
2978
2979 if (!(stats_px_get_cap(mod->domain_flags) & STATS_PX_CAP_BE)) {
2980 stats_count += mod->stats_count;
2981 continue;
2982 }
2983
2984 counters = EXTRA_COUNTERS_GET(px->extra_counters_be, mod);
2985 mod->fill_stats(counters, stats + stats_count);
2986 stats_count += mod->stats_count;
2987 }
2988
2989 return stats_dump_one_line(stats, stats_count, appctx);
William Lallemand74c24fb2016-11-21 17:18:36 +01002990}
2991
Christopher Fauleta8b76842022-12-16 15:08:36 +01002992/* Dumps the HTML table header for proxy <px> to the local trash buffer for and
2993 * uses the state from stream connector <sc> and per-uri parameters <uri>. The
2994 * caller is responsible for clearing the local trash buffer if needed.
William Lallemand74c24fb2016-11-21 17:18:36 +01002995 */
Willy Tarreaucaff6312022-05-27 10:17:46 +02002996static void stats_dump_html_px_hdr(struct stconn *sc, struct proxy *px)
William Lallemand74c24fb2016-11-21 17:18:36 +01002997{
Willy Tarreaucaff6312022-05-27 10:17:46 +02002998 struct appctx *appctx = __sc_appctx(sc);
Willy Tarreau91cefca2022-05-03 17:08:29 +02002999 struct show_stat_ctx *ctx = appctx->svcctx;
William Lallemand74c24fb2016-11-21 17:18:36 +01003000 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02003001 struct stats_module *mod;
3002 int stats_module_len = 0;
William Lallemand74c24fb2016-11-21 17:18:36 +01003003
Willy Tarreau91cefca2022-05-03 17:08:29 +02003004 if (px->cap & PR_CAP_BE && px->srv && (ctx->flags & STAT_ADMIN)) {
William Lallemand74c24fb2016-11-21 17:18:36 +01003005 /* A form to enable/disable this proxy servers */
3006
Willy Tarreau91cefca2022-05-03 17:08:29 +02003007 /* scope_txt = search pattern + search query, ctx->scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
William Lallemand74c24fb2016-11-21 17:18:36 +01003008 scope_txt[0] = 0;
Willy Tarreau91cefca2022-05-03 17:08:29 +02003009 if (ctx->scope_len) {
Willy Tarreaucaff6312022-05-27 10:17:46 +02003010 const char *scope_ptr = stats_scope_ptr(appctx, sc);
Christopher Fauleted7a0662019-01-14 11:07:34 +01003011
Willy Tarreaufc458ec2023-04-07 18:11:39 +02003012 strlcpy2(scope_txt, STAT_SCOPE_PATTERN, sizeof(scope_txt));
Willy Tarreau91cefca2022-05-03 17:08:29 +02003013 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), scope_ptr, ctx->scope_len);
3014 scope_txt[strlen(STAT_SCOPE_PATTERN) + ctx->scope_len] = 0;
William Lallemand74c24fb2016-11-21 17:18:36 +01003015 }
3016
Christopher Fauleta8b76842022-12-16 15:08:36 +01003017 chunk_appendf(&trash_chunk,
William Lallemand74c24fb2016-11-21 17:18:36 +01003018 "<form method=\"post\">");
3019 }
3020
3021 /* print a new table */
Christopher Fauleta8b76842022-12-16 15:08:36 +01003022 chunk_appendf(&trash_chunk,
William Lallemand74c24fb2016-11-21 17:18:36 +01003023 "<table class=\"tbl\" width=\"100%%\">\n"
3024 "<tr class=\"titre\">"
3025 "<th class=\"pxname\" width=\"10%%\">");
3026
Christopher Fauleta8b76842022-12-16 15:08:36 +01003027 chunk_appendf(&trash_chunk,
William Lallemand74c24fb2016-11-21 17:18:36 +01003028 "<a name=\"%s\"></a>%s"
3029 "<a class=px href=\"#%s\">%s</a>",
3030 px->id,
Willy Tarreau91cefca2022-05-03 17:08:29 +02003031 (ctx->flags & STAT_SHLGNDS) ? "<u>":"",
William Lallemand74c24fb2016-11-21 17:18:36 +01003032 px->id, px->id);
3033
Willy Tarreau91cefca2022-05-03 17:08:29 +02003034 if (ctx->flags & STAT_SHLGNDS) {
William Lallemand74c24fb2016-11-21 17:18:36 +01003035 /* cap, mode, id */
Christopher Fauleta8b76842022-12-16 15:08:36 +01003036 chunk_appendf(&trash_chunk, "<div class=tips>cap: %s, mode: %s, id: %d",
William Lallemand74c24fb2016-11-21 17:18:36 +01003037 proxy_cap_str(px->cap), proxy_mode_str(px->mode),
3038 px->uuid);
Christopher Fauleta8b76842022-12-16 15:08:36 +01003039 chunk_appendf(&trash_chunk, "</div>");
William Lallemand74c24fb2016-11-21 17:18:36 +01003040 }
3041
Christopher Fauleta8b76842022-12-16 15:08:36 +01003042 chunk_appendf(&trash_chunk,
William Lallemand74c24fb2016-11-21 17:18:36 +01003043 "%s</th>"
3044 "<th class=\"%s\" width=\"90%%\">%s</th>"
3045 "</tr>\n"
3046 "</table>\n"
3047 "<table class=\"tbl\" width=\"100%%\">\n"
3048 "<tr class=\"titre\">",
Willy Tarreau91cefca2022-05-03 17:08:29 +02003049 (ctx->flags & STAT_SHLGNDS) ? "</u>":"",
William Lallemand74c24fb2016-11-21 17:18:36 +01003050 px->desc ? "desc" : "empty", px->desc ? px->desc : "");
3051
Willy Tarreau91cefca2022-05-03 17:08:29 +02003052 if (ctx->flags & STAT_ADMIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +01003053 /* Column heading for Enable or Disable server */
Christopher Fauletbc271ec2019-12-02 11:29:04 +01003054 if ((px->cap & PR_CAP_BE) && px->srv)
Christopher Fauleta8b76842022-12-16 15:08:36 +01003055 chunk_appendf(&trash_chunk,
Christopher Fauletbc271ec2019-12-02 11:29:04 +01003056 "<th rowspan=2 width=1><input type=\"checkbox\" "
3057 "onclick=\"for(c in document.getElementsByClassName('%s-checkbox')) "
3058 "document.getElementsByClassName('%s-checkbox').item(c).checked = this.checked\"></th>",
3059 px->id,
3060 px->id);
3061 else
Christopher Fauleta8b76842022-12-16 15:08:36 +01003062 chunk_appendf(&trash_chunk, "<th rowspan=2></th>");
William Lallemand74c24fb2016-11-21 17:18:36 +01003063 }
3064
Christopher Fauleta8b76842022-12-16 15:08:36 +01003065 chunk_appendf(&trash_chunk,
William Lallemand74c24fb2016-11-21 17:18:36 +01003066 "<th rowspan=2></th>"
3067 "<th colspan=3>Queue</th>"
3068 "<th colspan=3>Session rate</th><th colspan=6>Sessions</th>"
3069 "<th colspan=2>Bytes</th><th colspan=2>Denied</th>"
3070 "<th colspan=3>Errors</th><th colspan=2>Warnings</th>"
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02003071 "<th colspan=9>Server</th>");
3072
Willy Tarreau91cefca2022-05-03 17:08:29 +02003073 if (ctx->flags & STAT_SHMODULES) {
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02003074 // calculate the count of module for colspan attribute
3075 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
3076 ++stats_module_len;
3077 }
Christopher Fauleta8b76842022-12-16 15:08:36 +01003078 chunk_appendf(&trash_chunk, "<th colspan=%d>Extra modules</th>",
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02003079 stats_module_len);
3080 }
3081
Christopher Fauleta8b76842022-12-16 15:08:36 +01003082 chunk_appendf(&trash_chunk,
William Lallemand74c24fb2016-11-21 17:18:36 +01003083 "</tr>\n"
3084 "<tr class=\"titre\">"
3085 "<th>Cur</th><th>Max</th><th>Limit</th>"
3086 "<th>Cur</th><th>Max</th><th>Limit</th><th>Cur</th><th>Max</th>"
3087 "<th>Limit</th><th>Total</th><th>LbTot</th><th>Last</th><th>In</th><th>Out</th>"
3088 "<th>Req</th><th>Resp</th><th>Req</th><th>Conn</th>"
3089 "<th>Resp</th><th>Retr</th><th>Redis</th>"
3090 "<th>Status</th><th>LastChk</th><th>Wght</th><th>Act</th>"
3091 "<th>Bck</th><th>Chk</th><th>Dwn</th><th>Dwntme</th>"
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02003092 "<th>Thrtle</th>\n");
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02003093
Willy Tarreau91cefca2022-05-03 17:08:29 +02003094 if (ctx->flags & STAT_SHMODULES) {
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02003095 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
Christopher Fauleta8b76842022-12-16 15:08:36 +01003096 chunk_appendf(&trash_chunk, "<th>%s</th>", mod->name);
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02003097 }
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02003098 }
3099
Christopher Fauleta8b76842022-12-16 15:08:36 +01003100 chunk_appendf(&trash_chunk, "</tr>");
William Lallemand74c24fb2016-11-21 17:18:36 +01003101}
3102
Christopher Fauleta8b76842022-12-16 15:08:36 +01003103/* Dumps the HTML table trailer for proxy <px> to the local trash buffer for and
3104 * uses the state from stream connector <sc>. The caller is responsible for
3105 * clearing the local trash buffer if needed.
William Lallemand74c24fb2016-11-21 17:18:36 +01003106 */
Willy Tarreaucaff6312022-05-27 10:17:46 +02003107static void stats_dump_html_px_end(struct stconn *sc, struct proxy *px)
William Lallemand74c24fb2016-11-21 17:18:36 +01003108{
Willy Tarreaucaff6312022-05-27 10:17:46 +02003109 struct appctx *appctx = __sc_appctx(sc);
Willy Tarreau91cefca2022-05-03 17:08:29 +02003110 struct show_stat_ctx *ctx = appctx->svcctx;
Christopher Faulet693b23b2022-02-28 09:09:05 +01003111
Christopher Fauleta8b76842022-12-16 15:08:36 +01003112 chunk_appendf(&trash_chunk, "</table>");
William Lallemand74c24fb2016-11-21 17:18:36 +01003113
Willy Tarreau91cefca2022-05-03 17:08:29 +02003114 if ((px->cap & PR_CAP_BE) && px->srv && (ctx->flags & STAT_ADMIN)) {
William Lallemand74c24fb2016-11-21 17:18:36 +01003115 /* close the form used to enable/disable this proxy servers */
Christopher Fauleta8b76842022-12-16 15:08:36 +01003116 chunk_appendf(&trash_chunk,
William Lallemand74c24fb2016-11-21 17:18:36 +01003117 "Choose the action to perform on the checked servers : "
3118 "<select name=action>"
3119 "<option value=\"\"></option>"
3120 "<option value=\"ready\">Set state to READY</option>"
3121 "<option value=\"drain\">Set state to DRAIN</option>"
3122 "<option value=\"maint\">Set state to MAINT</option>"
3123 "<option value=\"dhlth\">Health: disable checks</option>"
3124 "<option value=\"ehlth\">Health: enable checks</option>"
3125 "<option value=\"hrunn\">Health: force UP</option>"
3126 "<option value=\"hnolb\">Health: force NOLB</option>"
3127 "<option value=\"hdown\">Health: force DOWN</option>"
3128 "<option value=\"dagent\">Agent: disable checks</option>"
3129 "<option value=\"eagent\">Agent: enable checks</option>"
3130 "<option value=\"arunn\">Agent: force UP</option>"
3131 "<option value=\"adown\">Agent: force DOWN</option>"
3132 "<option value=\"shutdown\">Kill Sessions</option>"
3133 "</select>"
3134 "<input type=\"hidden\" name=\"b\" value=\"#%d\">"
3135 "&nbsp;<input type=\"submit\" value=\"Apply\">"
3136 "</form>",
3137 px->uuid);
3138 }
3139
Christopher Fauleta8b76842022-12-16 15:08:36 +01003140 chunk_appendf(&trash_chunk, "<p>\n");
William Lallemand74c24fb2016-11-21 17:18:36 +01003141}
3142
3143/*
Willy Tarreau4596fe22022-05-17 19:07:51 +02003144 * Dumps statistics for a proxy. The output is sent to the stream connector's
William Lallemand74c24fb2016-11-21 17:18:36 +01003145 * input buffer. Returns 0 if it had to stop dumping data because of lack of
3146 * buffer space, or non-zero if everything completed. This function is used
3147 * both by the CLI and the HTTP entry points, and is able to dump the output
3148 * in HTML or CSV formats. If the later, <uri> must be NULL.
3149 */
Willy Tarreaucaff6312022-05-27 10:17:46 +02003150int stats_dump_proxy_to_buffer(struct stconn *sc, struct htx *htx,
Christopher Fauletef779222018-10-31 08:47:01 +01003151 struct proxy *px, struct uri_auth *uri)
William Lallemand74c24fb2016-11-21 17:18:36 +01003152{
Willy Tarreaucaff6312022-05-27 10:17:46 +02003153 struct appctx *appctx = __sc_appctx(sc);
Willy Tarreau91cefca2022-05-03 17:08:29 +02003154 struct show_stat_ctx *ctx = appctx->svcctx;
Willy Tarreaucaff6312022-05-27 10:17:46 +02003155 struct stream *s = __sc_strm(sc);
3156 struct channel *rep = sc_ic(sc);
William Lallemand74c24fb2016-11-21 17:18:36 +01003157 struct server *sv, *svs; /* server and server-state, server-state=server or server->track */
3158 struct listener *l;
Aurelien DARRAGON55941842022-12-15 14:01:04 +01003159 int current_field;
Aurelien DARRAGON99a8d0f2023-01-31 09:51:32 +01003160 int px_st = ctx->px_st;
William Lallemand74c24fb2016-11-21 17:18:36 +01003161
Christopher Fauleta8b76842022-12-16 15:08:36 +01003162 chunk_reset(&trash_chunk);
Aurelien DARRAGON55941842022-12-15 14:01:04 +01003163more:
3164 current_field = ctx->field;
William Lallemand74c24fb2016-11-21 17:18:36 +01003165
Willy Tarreau91cefca2022-05-03 17:08:29 +02003166 switch (ctx->px_st) {
William Lallemand74c24fb2016-11-21 17:18:36 +01003167 case STAT_PX_ST_INIT:
3168 /* we are on a new proxy */
3169 if (uri && uri->scope) {
3170 /* we have a limited scope, we have to check the proxy name */
3171 struct stat_scope *scope;
3172 int len;
3173
3174 len = strlen(px->id);
3175 scope = uri->scope;
3176
3177 while (scope) {
3178 /* match exact proxy name */
3179 if (scope->px_len == len && !memcmp(px->id, scope->px_id, len))
3180 break;
3181
3182 /* match '.' which means 'self' proxy */
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003183 if (strcmp(scope->px_id, ".") == 0 && px == s->be)
William Lallemand74c24fb2016-11-21 17:18:36 +01003184 break;
3185 scope = scope->next;
3186 }
3187
3188 /* proxy name not found : don't dump anything */
3189 if (scope == NULL)
3190 return 1;
3191 }
3192
3193 /* if the user has requested a limited output and the proxy
3194 * name does not match, skip it.
3195 */
Willy Tarreau91cefca2022-05-03 17:08:29 +02003196 if (ctx->scope_len) {
Willy Tarreaucaff6312022-05-27 10:17:46 +02003197 const char *scope_ptr = stats_scope_ptr(appctx, sc);
Christopher Fauleted7a0662019-01-14 11:07:34 +01003198
Willy Tarreau91cefca2022-05-03 17:08:29 +02003199 if (strnistr(px->id, strlen(px->id), scope_ptr, ctx->scope_len) == NULL)
Christopher Fauleted7a0662019-01-14 11:07:34 +01003200 return 1;
3201 }
William Lallemand74c24fb2016-11-21 17:18:36 +01003202
Willy Tarreau91cefca2022-05-03 17:08:29 +02003203 if ((ctx->flags & STAT_BOUND) &&
3204 (ctx->iid != -1) &&
3205 (px->uuid != ctx->iid))
William Lallemand74c24fb2016-11-21 17:18:36 +01003206 return 1;
3207
Willy Tarreau91cefca2022-05-03 17:08:29 +02003208 ctx->px_st = STAT_PX_ST_TH;
Willy Tarreau9eb93c02022-11-14 07:12:05 +01003209 __fallthrough;
William Lallemand74c24fb2016-11-21 17:18:36 +01003210
3211 case STAT_PX_ST_TH:
Willy Tarreau91cefca2022-05-03 17:08:29 +02003212 if (ctx->flags & STAT_FMT_HTML) {
Willy Tarreaucaff6312022-05-27 10:17:46 +02003213 stats_dump_html_px_hdr(sc, px);
Christopher Fauletf4258bd2023-05-05 10:59:39 +02003214 if (!stats_putchk(appctx, htx))
Christopher Fauletef779222018-10-31 08:47:01 +01003215 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01003216 }
3217
Willy Tarreau91cefca2022-05-03 17:08:29 +02003218 ctx->px_st = STAT_PX_ST_FE;
Willy Tarreau9eb93c02022-11-14 07:12:05 +01003219 __fallthrough;
William Lallemand74c24fb2016-11-21 17:18:36 +01003220
3221 case STAT_PX_ST_FE:
3222 /* print the frontend */
Willy Tarreaucaff6312022-05-27 10:17:46 +02003223 if (stats_dump_fe_stats(sc, px)) {
Christopher Fauletf4258bd2023-05-05 10:59:39 +02003224 if (!stats_putchk(appctx, htx))
Christopher Fauletef779222018-10-31 08:47:01 +01003225 goto full;
Aurelien DARRAGON90304dc2023-02-02 19:01:02 +01003226 ctx->flags |= STAT_STARTED;
Aurelien DARRAGON55941842022-12-15 14:01:04 +01003227 if (ctx->field)
3228 goto more;
William Lallemand74c24fb2016-11-21 17:18:36 +01003229 }
3230
Aurelien DARRAGON55941842022-12-15 14:01:04 +01003231 current_field = 0;
Willy Tarreau91cefca2022-05-03 17:08:29 +02003232 ctx->obj2 = px->conf.listeners.n;
3233 ctx->px_st = STAT_PX_ST_LI;
Willy Tarreau9eb93c02022-11-14 07:12:05 +01003234 __fallthrough;
William Lallemand74c24fb2016-11-21 17:18:36 +01003235
3236 case STAT_PX_ST_LI:
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +02003237 /* obj2 points to listeners list as initialized above */
Willy Tarreau91cefca2022-05-03 17:08:29 +02003238 for (; ctx->obj2 != &px->conf.listeners; ctx->obj2 = l->by_fe.n) {
Christopher Fauletef779222018-10-31 08:47:01 +01003239 if (htx) {
Christopher Fauleta236c582023-05-09 16:31:01 +02003240 if (htx_almost_full(htx)) {
3241 sc_need_room(sc, htx->size / 2);
Christopher Fauletef779222018-10-31 08:47:01 +01003242 goto full;
Christopher Fauleta236c582023-05-09 16:31:01 +02003243 }
Christopher Fauletef779222018-10-31 08:47:01 +01003244 }
3245 else {
Christopher Fauleta236c582023-05-09 16:31:01 +02003246 if (buffer_almost_full(&rep->buf)) {
3247 sc_need_room(sc, b_size(&rep->buf) / 2);
Christopher Fauletef779222018-10-31 08:47:01 +01003248 goto full;
Christopher Fauleta236c582023-05-09 16:31:01 +02003249 }
William Lallemand74c24fb2016-11-21 17:18:36 +01003250 }
3251
Willy Tarreau91cefca2022-05-03 17:08:29 +02003252 l = LIST_ELEM(ctx->obj2, struct listener *, by_fe);
William Lallemand74c24fb2016-11-21 17:18:36 +01003253 if (!l->counters)
3254 continue;
3255
Willy Tarreau91cefca2022-05-03 17:08:29 +02003256 if (ctx->flags & STAT_BOUND) {
3257 if (!(ctx->type & (1 << STATS_TYPE_SO)))
William Lallemand74c24fb2016-11-21 17:18:36 +01003258 break;
3259
Willy Tarreau91cefca2022-05-03 17:08:29 +02003260 if (ctx->sid != -1 && l->luid != ctx->sid)
William Lallemand74c24fb2016-11-21 17:18:36 +01003261 continue;
3262 }
3263
3264 /* print the frontend */
Willy Tarreaucaff6312022-05-27 10:17:46 +02003265 if (stats_dump_li_stats(sc, px, l)) {
Christopher Fauletf4258bd2023-05-05 10:59:39 +02003266 if (!stats_putchk(appctx, htx))
Christopher Fauletef779222018-10-31 08:47:01 +01003267 goto full;
Aurelien DARRAGON90304dc2023-02-02 19:01:02 +01003268 ctx->flags |= STAT_STARTED;
Aurelien DARRAGON55941842022-12-15 14:01:04 +01003269 if (ctx->field)
3270 goto more;
William Lallemand74c24fb2016-11-21 17:18:36 +01003271 }
Aurelien DARRAGON9b07d4f2023-02-03 11:43:05 +01003272 current_field = 0;
William Lallemand74c24fb2016-11-21 17:18:36 +01003273 }
3274
Willy Tarreau91cefca2022-05-03 17:08:29 +02003275 ctx->obj2 = px->srv; /* may be NULL */
3276 ctx->px_st = STAT_PX_ST_SV;
Willy Tarreau9eb93c02022-11-14 07:12:05 +01003277 __fallthrough;
William Lallemand74c24fb2016-11-21 17:18:36 +01003278
3279 case STAT_PX_ST_SV:
Aurelien DARRAGON99a8d0f2023-01-31 09:51:32 +01003280 /* check for dump resumption */
3281 if (px_st == STAT_PX_ST_SV) {
3282 struct server *cur = ctx->obj2;
3283
3284 /* re-entrant dump */
3285 BUG_ON(!cur);
3286 if (cur->flags & SRV_F_DELETED) {
3287 /* the server could have been marked as deleted
3288 * between two dumping attempts, skip it.
3289 */
3290 cur = cur->next;
3291 }
3292 srv_drop(ctx->obj2); /* drop old srv taken on last dumping attempt */
3293 ctx->obj2 = cur; /* could be NULL */
3294 /* back to normal */
3295 }
3296
Amaury Denoyelle0a8d05d2021-08-25 14:39:53 +02003297 /* obj2 points to servers list as initialized above.
3298 *
Amaury Denoyellebc2ebfa2021-08-25 15:34:53 +02003299 * A server may be removed during the stats dumping.
Amaury Denoyelle0a8d05d2021-08-25 14:39:53 +02003300 * Temporarily increment its refcount to prevent its
Aurelien DARRAGON99a8d0f2023-01-31 09:51:32 +01003301 * anticipated cleaning. Call srv_drop() to release it.
Amaury Denoyelle0a8d05d2021-08-25 14:39:53 +02003302 */
Willy Tarreau91cefca2022-05-03 17:08:29 +02003303 for (; ctx->obj2 != NULL;
3304 ctx->obj2 = srv_drop(sv)) {
Amaury Denoyelle0a8d05d2021-08-25 14:39:53 +02003305
Willy Tarreau91cefca2022-05-03 17:08:29 +02003306 sv = ctx->obj2;
Amaury Denoyellebc2ebfa2021-08-25 15:34:53 +02003307 srv_take(sv);
Amaury Denoyelle0a8d05d2021-08-25 14:39:53 +02003308
Christopher Fauletef779222018-10-31 08:47:01 +01003309 if (htx) {
Christopher Fauleta236c582023-05-09 16:31:01 +02003310 if (htx_almost_full(htx)) {
3311 sc_need_room(sc, htx->size / 2);
Christopher Fauletef779222018-10-31 08:47:01 +01003312 goto full;
Christopher Fauleta236c582023-05-09 16:31:01 +02003313 }
Christopher Fauletef779222018-10-31 08:47:01 +01003314 }
3315 else {
Christopher Fauleta236c582023-05-09 16:31:01 +02003316 if (buffer_almost_full(&rep->buf)) {
3317 sc_need_room(sc, b_size(&rep->buf) / 2);
Christopher Fauletef779222018-10-31 08:47:01 +01003318 goto full;
Christopher Fauleta236c582023-05-09 16:31:01 +02003319 }
William Lallemand74c24fb2016-11-21 17:18:36 +01003320 }
3321
Willy Tarreau91cefca2022-05-03 17:08:29 +02003322 if (ctx->flags & STAT_BOUND) {
3323 if (!(ctx->type & (1 << STATS_TYPE_SV))) {
Amaury Denoyellebc2ebfa2021-08-25 15:34:53 +02003324 srv_drop(sv);
William Lallemand74c24fb2016-11-21 17:18:36 +01003325 break;
Amaury Denoyelle0a8d05d2021-08-25 14:39:53 +02003326 }
William Lallemand74c24fb2016-11-21 17:18:36 +01003327
Willy Tarreau91cefca2022-05-03 17:08:29 +02003328 if (ctx->sid != -1 && sv->puid != ctx->sid)
William Lallemand74c24fb2016-11-21 17:18:36 +01003329 continue;
3330 }
3331
Willy Tarreau3e320362020-10-23 17:28:57 +02003332 /* do not report disabled servers */
Willy Tarreau91cefca2022-05-03 17:08:29 +02003333 if (ctx->flags & STAT_HIDE_MAINT &&
Willy Tarreau3e320362020-10-23 17:28:57 +02003334 sv->cur_admin & SRV_ADMF_MAINT) {
3335 continue;
3336 }
3337
William Lallemand74c24fb2016-11-21 17:18:36 +01003338 svs = sv;
3339 while (svs->track)
3340 svs = svs->track;
3341
3342 /* do not report servers which are DOWN and not changing state */
Willy Tarreau91cefca2022-05-03 17:08:29 +02003343 if ((ctx->flags & STAT_HIDE_DOWN) &&
Emeric Brun52a91d32017-08-31 14:41:55 +02003344 ((sv->cur_admin & SRV_ADMF_MAINT) || /* server is in maintenance */
3345 (sv->cur_state == SRV_ST_STOPPED && /* server is down */
William Lallemand74c24fb2016-11-21 17:18:36 +01003346 (!((svs->agent.state | svs->check.state) & CHK_ST_ENABLED) ||
3347 ((svs->agent.state & CHK_ST_ENABLED) && !svs->agent.health) ||
3348 ((svs->check.state & CHK_ST_ENABLED) && !svs->check.health))))) {
3349 continue;
3350 }
3351
Willy Tarreaucaff6312022-05-27 10:17:46 +02003352 if (stats_dump_sv_stats(sc, px, sv)) {
Christopher Fauletf4258bd2023-05-05 10:59:39 +02003353 if (!stats_putchk(appctx, htx))
Christopher Fauletef779222018-10-31 08:47:01 +01003354 goto full;
Aurelien DARRAGON90304dc2023-02-02 19:01:02 +01003355 ctx->flags |= STAT_STARTED;
Aurelien DARRAGON28a23612023-02-02 18:13:30 +01003356 if (ctx->field)
3357 goto more;
William Lallemand74c24fb2016-11-21 17:18:36 +01003358 }
Aurelien DARRAGON28a23612023-02-02 18:13:30 +01003359 current_field = 0;
William Lallemand74c24fb2016-11-21 17:18:36 +01003360 } /* for sv */
3361
Willy Tarreau91cefca2022-05-03 17:08:29 +02003362 ctx->px_st = STAT_PX_ST_BE;
Willy Tarreau9eb93c02022-11-14 07:12:05 +01003363 __fallthrough;
William Lallemand74c24fb2016-11-21 17:18:36 +01003364
3365 case STAT_PX_ST_BE:
3366 /* print the backend */
Willy Tarreaucaff6312022-05-27 10:17:46 +02003367 if (stats_dump_be_stats(sc, px)) {
Christopher Fauletf4258bd2023-05-05 10:59:39 +02003368 if (!stats_putchk(appctx, htx))
Christopher Fauletef779222018-10-31 08:47:01 +01003369 goto full;
Aurelien DARRAGON90304dc2023-02-02 19:01:02 +01003370 ctx->flags |= STAT_STARTED;
Aurelien DARRAGON55941842022-12-15 14:01:04 +01003371 if (ctx->field)
3372 goto more;
William Lallemand74c24fb2016-11-21 17:18:36 +01003373 }
3374
Aurelien DARRAGON55941842022-12-15 14:01:04 +01003375 current_field = 0;
Willy Tarreau91cefca2022-05-03 17:08:29 +02003376 ctx->px_st = STAT_PX_ST_END;
Willy Tarreau9eb93c02022-11-14 07:12:05 +01003377 __fallthrough;
William Lallemand74c24fb2016-11-21 17:18:36 +01003378
3379 case STAT_PX_ST_END:
Willy Tarreau91cefca2022-05-03 17:08:29 +02003380 if (ctx->flags & STAT_FMT_HTML) {
Willy Tarreaucaff6312022-05-27 10:17:46 +02003381 stats_dump_html_px_end(sc, px);
Christopher Fauletf4258bd2023-05-05 10:59:39 +02003382 if (!stats_putchk(appctx, htx))
Christopher Fauletef779222018-10-31 08:47:01 +01003383 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01003384 }
3385
Willy Tarreau91cefca2022-05-03 17:08:29 +02003386 ctx->px_st = STAT_PX_ST_FIN;
Willy Tarreau9eb93c02022-11-14 07:12:05 +01003387 __fallthrough;
William Lallemand74c24fb2016-11-21 17:18:36 +01003388
3389 case STAT_PX_ST_FIN:
3390 return 1;
3391
3392 default:
3393 /* unknown state, we should put an abort() here ! */
3394 return 1;
3395 }
Christopher Fauletef779222018-10-31 08:47:01 +01003396
3397 full:
Aurelien DARRAGON55941842022-12-15 14:01:04 +01003398 /* restore previous field */
3399 ctx->field = current_field;
Christopher Fauletef779222018-10-31 08:47:01 +01003400 return 0;
William Lallemand74c24fb2016-11-21 17:18:36 +01003401}
3402
Christopher Fauleta8b76842022-12-16 15:08:36 +01003403/* Dumps the HTTP stats head block to the local trash buffer for and uses the
3404 * per-uri parameters <uri>. The caller is responsible for clearing the local
3405 * trash buffer if needed.
William Lallemand74c24fb2016-11-21 17:18:36 +01003406 */
Willy Tarreau676c29e2019-10-09 10:50:01 +02003407static void stats_dump_html_head(struct appctx *appctx, struct uri_auth *uri)
William Lallemand74c24fb2016-11-21 17:18:36 +01003408{
Willy Tarreau91cefca2022-05-03 17:08:29 +02003409 struct show_stat_ctx *ctx = appctx->svcctx;
3410
William Lallemand74c24fb2016-11-21 17:18:36 +01003411 /* WARNING! This must fit in the first buffer !!! */
Christopher Fauleta8b76842022-12-16 15:08:36 +01003412 chunk_appendf(&trash_chunk,
William Lallemand74c24fb2016-11-21 17:18:36 +01003413 "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"\n"
3414 "\"http://www.w3.org/TR/html4/loose.dtd\">\n"
3415 "<html><head><title>Statistics Report for " PRODUCT_NAME "%s%s</title>\n"
Thierry Fournierb6b1cde2021-09-24 00:03:07 +02003416 "<link rel=\"icon\" href=\"data:,\">\n"
William Lallemand74c24fb2016-11-21 17:18:36 +01003417 "<meta http-equiv=\"content-type\" content=\"text/html; charset=iso-8859-1\">\n"
3418 "<style type=\"text/css\"><!--\n"
3419 "body {"
3420 " font-family: arial, helvetica, sans-serif;"
3421 " font-size: 12px;"
3422 " font-weight: normal;"
3423 " color: black;"
3424 " background: white;"
3425 "}\n"
3426 "th,td {"
3427 " font-size: 10px;"
3428 "}\n"
3429 "h1 {"
3430 " font-size: x-large;"
3431 " margin-bottom: 0.5em;"
3432 "}\n"
3433 "h2 {"
3434 " font-family: helvetica, arial;"
3435 " font-size: x-large;"
3436 " font-weight: bold;"
3437 " font-style: italic;"
3438 " color: #6020a0;"
3439 " margin-top: 0em;"
3440 " margin-bottom: 0em;"
3441 "}\n"
3442 "h3 {"
3443 " font-family: helvetica, arial;"
3444 " font-size: 16px;"
3445 " font-weight: bold;"
3446 " color: #b00040;"
3447 " background: #e8e8d0;"
3448 " margin-top: 0em;"
3449 " margin-bottom: 0em;"
3450 "}\n"
3451 "li {"
3452 " margin-top: 0.25em;"
3453 " margin-right: 2em;"
3454 "}\n"
3455 ".hr {margin-top: 0.25em;"
3456 " border-color: black;"
3457 " border-bottom-style: solid;"
3458 "}\n"
3459 ".titre {background: #20D0D0;color: #000000; font-weight: bold; text-align: center;}\n"
3460 ".total {background: #20D0D0;color: #ffff80;}\n"
3461 ".frontend {background: #e8e8d0;}\n"
3462 ".socket {background: #d0d0d0;}\n"
3463 ".backend {background: #e8e8d0;}\n"
3464 ".active_down {background: #ff9090;}\n"
3465 ".active_going_up {background: #ffd020;}\n"
3466 ".active_going_down {background: #ffffa0;}\n"
3467 ".active_up {background: #c0ffc0;}\n"
3468 ".active_nolb {background: #20a0ff;}\n"
3469 ".active_draining {background: #20a0FF;}\n"
3470 ".active_no_check {background: #e0e0e0;}\n"
3471 ".backup_down {background: #ff9090;}\n"
3472 ".backup_going_up {background: #ff80ff;}\n"
3473 ".backup_going_down {background: #c060ff;}\n"
3474 ".backup_up {background: #b0d0ff;}\n"
3475 ".backup_nolb {background: #90b0e0;}\n"
3476 ".backup_draining {background: #cc9900;}\n"
3477 ".backup_no_check {background: #e0e0e0;}\n"
3478 ".maintain {background: #c07820;}\n"
3479 ".rls {letter-spacing: 0.2em; margin-right: 1px;}\n" /* right letter spacing (used for grouping digits) */
3480 "\n"
3481 "a.px:link {color: #ffff40; text-decoration: none;}"
3482 "a.px:visited {color: #ffff40; text-decoration: none;}"
3483 "a.px:hover {color: #ffffff; text-decoration: none;}"
3484 "a.lfsb:link {color: #000000; text-decoration: none;}"
3485 "a.lfsb:visited {color: #000000; text-decoration: none;}"
3486 "a.lfsb:hover {color: #505050; text-decoration: none;}"
3487 "\n"
3488 "table.tbl { border-collapse: collapse; border-style: none;}\n"
3489 "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"
3490 "table.tbl td.ac { text-align: center;}\n"
3491 "table.tbl th { border-width: 1px; border-style: solid solid solid solid; border-color: gray;}\n"
3492 "table.tbl th.pxname { background: #b00040; color: #ffff40; font-weight: bold; border-style: solid solid none solid; padding: 2px 3px; white-space: nowrap;}\n"
3493 "table.tbl th.empty { border-style: none; empty-cells: hide; background: white;}\n"
3494 "table.tbl th.desc { background: white; border-style: solid solid none solid; text-align: left; padding: 2px 3px;}\n"
3495 "\n"
3496 "table.lgd { border-collapse: collapse; border-width: 1px; border-style: none none none solid; border-color: black;}\n"
3497 "table.lgd td { border-width: 1px; border-style: solid solid solid solid; border-color: gray; padding: 2px;}\n"
3498 "table.lgd td.noborder { border-style: none; padding: 2px; white-space: nowrap;}\n"
3499 "table.det { border-collapse: collapse; border-style: none; }\n"
3500 "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"
3501 "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"
3502 "u {text-decoration:none; border-bottom: 1px dotted black;}\n"
Thierry Fournierb6b1cde2021-09-24 00:03:07 +02003503 "div.tips {\n"
3504 " display:block;\n"
3505 " visibility:hidden;\n"
3506 " z-index:2147483647;\n"
3507 " position:absolute;\n"
3508 " padding:2px 4px 3px;\n"
3509 " background:#f0f060; color:#000000;\n"
3510 " border:1px solid #7040c0;\n"
3511 " white-space:nowrap;\n"
3512 " font-style:normal;font-size:11px;font-weight:normal;\n"
3513 " -moz-border-radius:3px;-webkit-border-radius:3px;border-radius:3px;\n"
3514 " -moz-box-shadow:gray 2px 2px 3px;-webkit-box-shadow:gray 2px 2px 3px;box-shadow:gray 2px 2px 3px;\n"
3515 "}\n"
3516 "u:hover div.tips {visibility:visible;}\n"
Thierry Fourniere9ed63e2021-09-23 23:59:49 +02003517 "@media (prefers-color-scheme: dark) {\n"
3518 " body { font-family: arial, helvetica, sans-serif; font-size: 12px; font-weight: normal; color: #e8e6e3; background: #131516;}\n"
3519 " h1 { color: #a265e0!important; }\n"
3520 " h2 { color: #a265e0; }\n"
3521 " h3 { color: #ff5190; background-color: #3e3e1f; }\n"
3522 " a { color: #3391ff; }\n"
3523 " input { background-color: #2f3437; }\n"
3524 " .hr { border-color: #8c8273; }\n"
3525 " .titre { background-color: #1aa6a6; color: #e8e6e3; }\n"
3526 " .frontend {background: #2f3437;}\n"
Marno Krahmera690b732022-03-08 13:45:09 +01003527 " .socket {background: #2a2d2f;}\n"
Thierry Fourniere9ed63e2021-09-23 23:59:49 +02003528 " .backend {background: #2f3437;}\n"
3529 " .active_down {background: #760000;}\n"
3530 " .active_going_up {background: #b99200;}\n"
3531 " .active_going_down {background: #6c6c00;}\n"
3532 " .active_up {background: #165900;}\n"
3533 " .active_nolb {background: #006ab9;}\n"
3534 " .active_draining {background: #006ab9;}\n"
3535 " .active_no_check {background: #2a2d2f;}\n"
3536 " .backup_down {background: #760000;}\n"
3537 " .backup_going_up {background: #7f007f;}\n"
3538 " .backup_going_down {background: #580092;}\n"
3539 " .backup_up {background: #2e3234;}\n"
3540 " .backup_nolb {background: #1e3c6a;}\n"
3541 " .backup_draining {background: #a37a00;}\n"
3542 " .backup_no_check {background: #2a2d2f;}\n"
3543 " .maintain {background: #9a601a;}\n"
3544 " a.px:link {color: #d8d83b; text-decoration: none;}\n"
3545 " a.px:visited {color: #d8d83b; text-decoration: none;}\n"
3546 " a.px:hover {color: #ffffff; text-decoration: none;}\n"
3547 " a.lfsb:link {color: #e8e6e3; text-decoration: none;}\n"
3548 " a.lfsb:visited {color: #e8e6e3; text-decoration: none;}\n"
3549 " a.lfsb:hover {color: #b5afa6; text-decoration: none;}\n"
3550 " table.tbl th.empty { background-color: #181a1b; }\n"
Willy Tarreau00147f72022-04-11 07:59:27 +02003551 " table.tbl th.desc { background: #181a1b; }\n"
Thierry Fourniere9ed63e2021-09-23 23:59:49 +02003552 " table.tbl th.pxname { background-color: #8d0033; color: #ffff46; }\n"
3553 " table.tbl th { border-color: #808080; }\n"
3554 " table.tbl td { border-color: #808080; }\n"
3555 " u {text-decoration:none; border-bottom: 1px dotted #e8e6e3;}\n"
3556 " div.tips {\n"
3557 " background:#8e8e0d;\n"
3558 " color:#e8e6e3;\n"
3559 " border-color: #4e2c86;\n"
3560 " -moz-box-shadow: #60686c 2px 2px 3px;\n"
3561 " -webkit-box-shadow: #60686c 2px 2px 3px;\n"
3562 " box-shadow: #60686c 2px 2px 3px;\n"
3563 " }\n"
3564 "}\n"
William Lallemand74c24fb2016-11-21 17:18:36 +01003565 "-->\n"
3566 "</style></head>\n",
Willy Tarreau91cefca2022-05-03 17:08:29 +02003567 (ctx->flags & STAT_SHNODE) ? " on " : "",
3568 (ctx->flags & STAT_SHNODE) ? (uri && uri->node ? uri->node : global.node) : ""
William Lallemand74c24fb2016-11-21 17:18:36 +01003569 );
3570}
3571
Christopher Fauleta8b76842022-12-16 15:08:36 +01003572/* Dumps the HTML stats information block to the local trash buffer for and uses
3573 * the state from stream connector <sc> and per-uri parameters <uri>. The caller
3574 * is responsible for clearing the local trash buffer if needed.
William Lallemand74c24fb2016-11-21 17:18:36 +01003575 */
Willy Tarreaucaff6312022-05-27 10:17:46 +02003576static void stats_dump_html_info(struct stconn *sc, struct uri_auth *uri)
William Lallemand74c24fb2016-11-21 17:18:36 +01003577{
Willy Tarreaucaff6312022-05-27 10:17:46 +02003578 struct appctx *appctx = __sc_appctx(sc);
Willy Tarreau91cefca2022-05-03 17:08:29 +02003579 struct show_stat_ctx *ctx = appctx->svcctx;
Willy Tarreauc05d30e2023-04-28 14:50:29 +02003580 unsigned int up = ns_to_sec(now_ns - start_time_ns);
William Lallemand74c24fb2016-11-21 17:18:36 +01003581 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
Willy Tarreaucaff6312022-05-27 10:17:46 +02003582 const char *scope_ptr = stats_scope_ptr(appctx, sc);
Willy Tarreau6be8d092023-01-12 16:08:41 +01003583 unsigned long long bps;
3584 int thr;
3585
3586 for (bps = thr = 0; thr < global.nbthread; thr++)
3587 bps += 32ULL * read_freq_ctr(&ha_thread_ctx[thr].out_32bps);
Willy Tarreau1713c032019-05-23 12:23:55 +02003588
3589 /* Turn the bytes per second to bits per second and take care of the
3590 * usual ethernet overhead in order to help figure how far we are from
3591 * interface saturation since it's the only case which usually matters.
3592 * For this we count the total size of an Ethernet frame on the wire
3593 * including preamble and IFG (1538) for the largest TCP segment it
3594 * transports (1448 with TCP timestamps). This is not valid for smaller
3595 * packets (under-estimated), but it gives a reasonably accurate
3596 * estimation of how far we are from uplink saturation.
3597 */
3598 bps = bps * 8 * 1538 / 1448;
William Lallemand74c24fb2016-11-21 17:18:36 +01003599
3600 /* WARNING! this has to fit the first packet too.
3601 * We are around 3.5 kB, add adding entries will
3602 * become tricky if we want to support 4kB buffers !
3603 */
Christopher Fauleta8b76842022-12-16 15:08:36 +01003604 chunk_appendf(&trash_chunk,
William Lallemand74c24fb2016-11-21 17:18:36 +01003605 "<body><h1><a href=\"" PRODUCT_URL "\" style=\"text-decoration: none;\">"
3606 PRODUCT_NAME "%s</a></h1>\n"
3607 "<h2>Statistics Report for pid %d%s%s%s%s</h2>\n"
3608 "<hr width=\"100%%\" class=\"hr\">\n"
3609 "<h3>&gt; General process information</h3>\n"
3610 "<table border=0><tr><td align=\"left\" nowrap width=\"1%%\">\n"
Yves Lafon95317282018-02-26 11:10:37 +01003611 "<p><b>pid = </b> %d (process #%d, nbproc = %d, nbthread = %d)<br>\n"
Willy Tarreau3c4a2972023-05-11 12:02:21 +02003612 "<b>uptime = </b> %dd %dh%02dm%02ds; warnings = %u<br>\n"
William Lallemand74c24fb2016-11-21 17:18:36 +01003613 "<b>system limits:</b> memmax = %s%s; ulimit-n = %d<br>\n"
Willy Tarreau96151022023-05-11 13:51:31 +02003614 "<b>maxsock = </b> %d; <b>maxconn = </b> %d; <b>reached = </b> %llu; <b>maxpipes = </b> %d<br>\n"
Willy Tarreau1713c032019-05-23 12:23:55 +02003615 "current conns = %d; current pipes = %d/%d; conn rate = %d/sec; bit rate = %.3f %cbps<br>\n"
William Lallemand74c24fb2016-11-21 17:18:36 +01003616 "Running tasks: %d/%d; idle = %d %%<br>\n"
3617 "</td><td align=\"center\" nowrap>\n"
3618 "<table class=\"lgd\"><tr>\n"
3619 "<td class=\"active_up\">&nbsp;</td><td class=\"noborder\">active UP </td>"
3620 "<td class=\"backup_up\">&nbsp;</td><td class=\"noborder\">backup UP </td>"
3621 "</tr><tr>\n"
3622 "<td class=\"active_going_down\"></td><td class=\"noborder\">active UP, going down </td>"
3623 "<td class=\"backup_going_down\"></td><td class=\"noborder\">backup UP, going down </td>"
3624 "</tr><tr>\n"
3625 "<td class=\"active_going_up\"></td><td class=\"noborder\">active DOWN, going up </td>"
3626 "<td class=\"backup_going_up\"></td><td class=\"noborder\">backup DOWN, going up </td>"
3627 "</tr><tr>\n"
3628 "<td class=\"active_down\"></td><td class=\"noborder\">active or backup DOWN &nbsp;</td>"
3629 "<td class=\"active_no_check\"></td><td class=\"noborder\">not checked </td>"
3630 "</tr><tr>\n"
3631 "<td class=\"maintain\"></td><td class=\"noborder\" colspan=\"3\">active or backup DOWN for maintenance (MAINT) &nbsp;</td>"
3632 "</tr><tr>\n"
3633 "<td class=\"active_draining\"></td><td class=\"noborder\" colspan=\"3\">active or backup SOFT STOPPED for maintenance &nbsp;</td>"
3634 "</tr></table>\n"
3635 "Note: \"NOLB\"/\"DRAIN\" = UP with load-balancing disabled."
3636 "</td>"
3637 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
3638 "<b>Display option:</b><ul style=\"margin-top: 0.25em;\">"
3639 "",
Willy Tarreau91cefca2022-05-03 17:08:29 +02003640 (ctx->flags & STAT_HIDEVER) ? "" : (stats_version_string),
3641 pid, (ctx->flags & STAT_SHNODE) ? " on " : "",
3642 (ctx->flags & STAT_SHNODE) ? (uri->node ? uri->node : global.node) : "",
3643 (ctx->flags & STAT_SHDESC) ? ": " : "",
3644 (ctx->flags & STAT_SHDESC) ? (uri->desc ? uri->desc : global.desc) : "",
Willy Tarreaue8422bf2021-06-15 09:08:18 +02003645 pid, 1, 1, global.nbthread,
William Lallemand74c24fb2016-11-21 17:18:36 +01003646 up / 86400, (up % 86400) / 3600,
3647 (up % 3600) / 60, (up % 60),
Willy Tarreau3c4a2972023-05-11 12:02:21 +02003648 HA_ATOMIC_LOAD(&tot_warnings),
William Lallemand74c24fb2016-11-21 17:18:36 +01003649 global.rlimit_memmax ? ultoa(global.rlimit_memmax) : "unlimited",
3650 global.rlimit_memmax ? " MB" : "",
3651 global.rlimit_nofile,
Willy Tarreau96151022023-05-11 13:51:31 +02003652 global.maxsock, global.maxconn, HA_ATOMIC_LOAD(&maxconn_reached), global.maxpipes,
William Lallemand74c24fb2016-11-21 17:18:36 +01003653 actconn, pipes_used, pipes_used+pipes_free, read_freq_ctr(&global.conn_per_sec),
Willy Tarreau1713c032019-05-23 12:23:55 +02003654 bps >= 1000000000UL ? (bps / 1000000000.0) : bps >= 1000000UL ? (bps / 1000000.0) : (bps / 1000.0),
3655 bps >= 1000000000UL ? 'G' : bps >= 1000000UL ? 'M' : 'k',
Willy Tarreauf9d5e102021-10-08 10:43:59 +02003656 total_run_queues(), total_allocated_tasks(), clock_report_idle()
William Lallemand74c24fb2016-11-21 17:18:36 +01003657 );
3658
Willy Tarreau91cefca2022-05-03 17:08:29 +02003659 /* scope_txt = search query, ctx->scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
3660 memcpy(scope_txt, scope_ptr, ctx->scope_len);
3661 scope_txt[ctx->scope_len] = '\0';
William Lallemand74c24fb2016-11-21 17:18:36 +01003662
Christopher Fauleta8b76842022-12-16 15:08:36 +01003663 chunk_appendf(&trash_chunk,
William Lallemand74c24fb2016-11-21 17:18:36 +01003664 "<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 +02003665 (ctx->scope_len > 0) ? scope_txt : "",
William Lallemand74c24fb2016-11-21 17:18:36 +01003666 STAT_SCOPE_TXT_MAXLEN);
3667
Willy Tarreau91cefca2022-05-03 17:08:29 +02003668 /* scope_txt = search pattern + search query, ctx->scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
William Lallemand74c24fb2016-11-21 17:18:36 +01003669 scope_txt[0] = 0;
Willy Tarreau91cefca2022-05-03 17:08:29 +02003670 if (ctx->scope_len) {
Willy Tarreaufc458ec2023-04-07 18:11:39 +02003671 strlcpy2(scope_txt, STAT_SCOPE_PATTERN, sizeof(scope_txt));
Willy Tarreau91cefca2022-05-03 17:08:29 +02003672 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), scope_ptr, ctx->scope_len);
3673 scope_txt[strlen(STAT_SCOPE_PATTERN) + ctx->scope_len] = 0;
William Lallemand74c24fb2016-11-21 17:18:36 +01003674 }
3675
Willy Tarreau91cefca2022-05-03 17:08:29 +02003676 if (ctx->flags & STAT_HIDE_DOWN)
Christopher Fauleta8b76842022-12-16 15:08:36 +01003677 chunk_appendf(&trash_chunk,
William Lallemand74c24fb2016-11-21 17:18:36 +01003678 "<li><a href=\"%s%s%s%s\">Show all servers</a><br>\n",
3679 uri->uri_prefix,
3680 "",
Willy Tarreau91cefca2022-05-03 17:08:29 +02003681 (ctx->flags & STAT_NO_REFRESH) ? ";norefresh" : "",
William Lallemand74c24fb2016-11-21 17:18:36 +01003682 scope_txt);
3683 else
Christopher Fauleta8b76842022-12-16 15:08:36 +01003684 chunk_appendf(&trash_chunk,
William Lallemand74c24fb2016-11-21 17:18:36 +01003685 "<li><a href=\"%s%s%s%s\">Hide 'DOWN' servers</a><br>\n",
3686 uri->uri_prefix,
3687 ";up",
Willy Tarreau91cefca2022-05-03 17:08:29 +02003688 (ctx->flags & STAT_NO_REFRESH) ? ";norefresh" : "",
William Lallemand74c24fb2016-11-21 17:18:36 +01003689 scope_txt);
3690
3691 if (uri->refresh > 0) {
Willy Tarreau91cefca2022-05-03 17:08:29 +02003692 if (ctx->flags & STAT_NO_REFRESH)
Christopher Fauleta8b76842022-12-16 15:08:36 +01003693 chunk_appendf(&trash_chunk,
William Lallemand74c24fb2016-11-21 17:18:36 +01003694 "<li><a href=\"%s%s%s%s\">Enable refresh</a><br>\n",
3695 uri->uri_prefix,
Willy Tarreau91cefca2022-05-03 17:08:29 +02003696 (ctx->flags & STAT_HIDE_DOWN) ? ";up" : "",
William Lallemand74c24fb2016-11-21 17:18:36 +01003697 "",
3698 scope_txt);
3699 else
Christopher Fauleta8b76842022-12-16 15:08:36 +01003700 chunk_appendf(&trash_chunk,
William Lallemand74c24fb2016-11-21 17:18:36 +01003701 "<li><a href=\"%s%s%s%s\">Disable refresh</a><br>\n",
3702 uri->uri_prefix,
Willy Tarreau91cefca2022-05-03 17:08:29 +02003703 (ctx->flags & STAT_HIDE_DOWN) ? ";up" : "",
William Lallemand74c24fb2016-11-21 17:18:36 +01003704 ";norefresh",
3705 scope_txt);
3706 }
3707
Christopher Fauleta8b76842022-12-16 15:08:36 +01003708 chunk_appendf(&trash_chunk,
William Lallemand74c24fb2016-11-21 17:18:36 +01003709 "<li><a href=\"%s%s%s%s\">Refresh now</a><br>\n",
3710 uri->uri_prefix,
Willy Tarreau91cefca2022-05-03 17:08:29 +02003711 (ctx->flags & STAT_HIDE_DOWN) ? ";up" : "",
3712 (ctx->flags & STAT_NO_REFRESH) ? ";norefresh" : "",
William Lallemand74c24fb2016-11-21 17:18:36 +01003713 scope_txt);
3714
Christopher Fauleta8b76842022-12-16 15:08:36 +01003715 chunk_appendf(&trash_chunk,
William Lallemand74c24fb2016-11-21 17:18:36 +01003716 "<li><a href=\"%s;csv%s%s\">CSV export</a><br>\n",
3717 uri->uri_prefix,
3718 (uri->refresh > 0) ? ";norefresh" : "",
3719 scope_txt);
3720
Christopher Fauleta8b76842022-12-16 15:08:36 +01003721 chunk_appendf(&trash_chunk,
Christopher Faulet6338a082019-09-09 15:50:54 +02003722 "<li><a href=\"%s;json%s%s\">JSON export</a> (<a href=\"%s;json-schema\">schema</a>)<br>\n",
3723 uri->uri_prefix,
3724 (uri->refresh > 0) ? ";norefresh" : "",
3725 scope_txt, uri->uri_prefix);
3726
Christopher Fauleta8b76842022-12-16 15:08:36 +01003727 chunk_appendf(&trash_chunk,
William Lallemand74c24fb2016-11-21 17:18:36 +01003728 "</ul></td>"
3729 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
3730 "<b>External resources:</b><ul style=\"margin-top: 0.25em;\">\n"
3731 "<li><a href=\"" PRODUCT_URL "\">Primary site</a><br>\n"
3732 "<li><a href=\"" PRODUCT_URL_UPD "\">Updates (v" PRODUCT_BRANCH ")</a><br>\n"
3733 "<li><a href=\"" PRODUCT_URL_DOC "\">Online manual</a><br>\n"
3734 "</ul>"
3735 "</td>"
3736 "</tr></table>\n"
3737 ""
3738 );
3739
Willy Tarreau91cefca2022-05-03 17:08:29 +02003740 if (ctx->st_code) {
3741 switch (ctx->st_code) {
William Lallemand74c24fb2016-11-21 17:18:36 +01003742 case STAT_STATUS_DONE:
Christopher Fauleta8b76842022-12-16 15:08:36 +01003743 chunk_appendf(&trash_chunk,
William Lallemand74c24fb2016-11-21 17:18:36 +01003744 "<p><div class=active_up>"
3745 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
3746 "Action processed successfully."
3747 "</div>\n", uri->uri_prefix,
Willy Tarreau91cefca2022-05-03 17:08:29 +02003748 (ctx->flags & STAT_HIDE_DOWN) ? ";up" : "",
3749 (ctx->flags & STAT_NO_REFRESH) ? ";norefresh" : "",
William Lallemand74c24fb2016-11-21 17:18:36 +01003750 scope_txt);
3751 break;
3752 case STAT_STATUS_NONE:
Christopher Fauleta8b76842022-12-16 15:08:36 +01003753 chunk_appendf(&trash_chunk,
William Lallemand74c24fb2016-11-21 17:18:36 +01003754 "<p><div class=active_going_down>"
3755 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
3756 "Nothing has changed."
3757 "</div>\n", uri->uri_prefix,
Willy Tarreau91cefca2022-05-03 17:08:29 +02003758 (ctx->flags & STAT_HIDE_DOWN) ? ";up" : "",
3759 (ctx->flags & STAT_NO_REFRESH) ? ";norefresh" : "",
William Lallemand74c24fb2016-11-21 17:18:36 +01003760 scope_txt);
3761 break;
3762 case STAT_STATUS_PART:
Christopher Fauleta8b76842022-12-16 15:08:36 +01003763 chunk_appendf(&trash_chunk,
William Lallemand74c24fb2016-11-21 17:18:36 +01003764 "<p><div class=active_going_down>"
3765 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
3766 "Action partially processed.<br>"
3767 "Some server names are probably unknown or ambiguous (duplicated names in the backend)."
3768 "</div>\n", uri->uri_prefix,
Willy Tarreau91cefca2022-05-03 17:08:29 +02003769 (ctx->flags & STAT_HIDE_DOWN) ? ";up" : "",
3770 (ctx->flags & STAT_NO_REFRESH) ? ";norefresh" : "",
William Lallemand74c24fb2016-11-21 17:18:36 +01003771 scope_txt);
3772 break;
3773 case STAT_STATUS_ERRP:
Christopher Fauleta8b76842022-12-16 15:08:36 +01003774 chunk_appendf(&trash_chunk,
William Lallemand74c24fb2016-11-21 17:18:36 +01003775 "<p><div class=active_down>"
3776 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
3777 "Action not processed because of invalid parameters."
3778 "<ul>"
3779 "<li>The action is maybe unknown.</li>"
Christopher Faulet2f9a41d2019-02-27 15:30:57 +01003780 "<li>Invalid key parameter (empty or too long).</li>"
William Lallemand74c24fb2016-11-21 17:18:36 +01003781 "<li>The backend name is probably unknown or ambiguous (duplicated names).</li>"
3782 "<li>Some server names are probably unknown or ambiguous (duplicated names in the backend).</li>"
3783 "</ul>"
3784 "</div>\n", uri->uri_prefix,
Willy Tarreau91cefca2022-05-03 17:08:29 +02003785 (ctx->flags & STAT_HIDE_DOWN) ? ";up" : "",
3786 (ctx->flags & STAT_NO_REFRESH) ? ";norefresh" : "",
William Lallemand74c24fb2016-11-21 17:18:36 +01003787 scope_txt);
3788 break;
3789 case STAT_STATUS_EXCD:
Christopher Fauleta8b76842022-12-16 15:08:36 +01003790 chunk_appendf(&trash_chunk,
William Lallemand74c24fb2016-11-21 17:18:36 +01003791 "<p><div class=active_down>"
3792 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
3793 "<b>Action not processed : the buffer couldn't store all the data.<br>"
3794 "You should retry with less servers at a time.</b>"
3795 "</div>\n", uri->uri_prefix,
Willy Tarreau91cefca2022-05-03 17:08:29 +02003796 (ctx->flags & STAT_HIDE_DOWN) ? ";up" : "",
3797 (ctx->flags & STAT_NO_REFRESH) ? ";norefresh" : "",
William Lallemand74c24fb2016-11-21 17:18:36 +01003798 scope_txt);
3799 break;
3800 case STAT_STATUS_DENY:
Christopher Fauleta8b76842022-12-16 15:08:36 +01003801 chunk_appendf(&trash_chunk,
William Lallemand74c24fb2016-11-21 17:18:36 +01003802 "<p><div class=active_down>"
3803 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
3804 "<b>Action denied.</b>"
3805 "</div>\n", uri->uri_prefix,
Willy Tarreau91cefca2022-05-03 17:08:29 +02003806 (ctx->flags & STAT_HIDE_DOWN) ? ";up" : "",
3807 (ctx->flags & STAT_NO_REFRESH) ? ";norefresh" : "",
William Lallemand74c24fb2016-11-21 17:18:36 +01003808 scope_txt);
3809 break;
Christopher Faulet3c2ecf72019-02-27 16:41:27 +01003810 case STAT_STATUS_IVAL:
Christopher Fauleta8b76842022-12-16 15:08:36 +01003811 chunk_appendf(&trash_chunk,
Christopher Faulet3c2ecf72019-02-27 16:41:27 +01003812 "<p><div class=active_down>"
3813 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
3814 "<b>Invalid requests (unsupported method or chunked encoded request).</b>"
3815 "</div>\n", uri->uri_prefix,
Willy Tarreau91cefca2022-05-03 17:08:29 +02003816 (ctx->flags & STAT_HIDE_DOWN) ? ";up" : "",
3817 (ctx->flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Christopher Faulet3c2ecf72019-02-27 16:41:27 +01003818 scope_txt);
3819 break;
William Lallemand74c24fb2016-11-21 17:18:36 +01003820 default:
Christopher Fauleta8b76842022-12-16 15:08:36 +01003821 chunk_appendf(&trash_chunk,
William Lallemand74c24fb2016-11-21 17:18:36 +01003822 "<p><div class=active_no_check>"
3823 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
3824 "Unexpected result."
3825 "</div>\n", uri->uri_prefix,
Willy Tarreau91cefca2022-05-03 17:08:29 +02003826 (ctx->flags & STAT_HIDE_DOWN) ? ";up" : "",
3827 (ctx->flags & STAT_NO_REFRESH) ? ";norefresh" : "",
William Lallemand74c24fb2016-11-21 17:18:36 +01003828 scope_txt);
3829 }
Christopher Fauleta8b76842022-12-16 15:08:36 +01003830 chunk_appendf(&trash_chunk, "<p>\n");
William Lallemand74c24fb2016-11-21 17:18:36 +01003831 }
3832}
3833
Christopher Fauleta8b76842022-12-16 15:08:36 +01003834/* Dumps the HTML stats trailer block to the local trash buffer. The caller is
3835 * responsible for clearing the local trash buffer if needed.
William Lallemand74c24fb2016-11-21 17:18:36 +01003836 */
3837static void stats_dump_html_end()
3838{
Christopher Fauleta8b76842022-12-16 15:08:36 +01003839 chunk_appendf(&trash_chunk, "</body></html>\n");
William Lallemand74c24fb2016-11-21 17:18:36 +01003840}
3841
Christopher Fauleta8b76842022-12-16 15:08:36 +01003842/* Dumps the stats JSON header to the local trash buffer buffer which. The
3843 * caller is responsible for clearing it if needed.
Simon Horman05ee2132017-01-04 09:37:25 +01003844 */
3845static void stats_dump_json_header()
3846{
Christopher Fauleta8b76842022-12-16 15:08:36 +01003847 chunk_strcat(&trash_chunk, "[");
Simon Horman05ee2132017-01-04 09:37:25 +01003848}
3849
3850
Christopher Fauleta8b76842022-12-16 15:08:36 +01003851/* Dumps the JSON stats trailer block to the local trash buffer. The caller is
3852 * responsible for clearing the local trash buffer if needed.
Simon Horman05ee2132017-01-04 09:37:25 +01003853 */
3854static void stats_dump_json_end()
3855{
Christopher Fauleta8b76842022-12-16 15:08:36 +01003856 chunk_strcat(&trash_chunk, "]\n");
Simon Horman05ee2132017-01-04 09:37:25 +01003857}
3858
Amaury Denoyelle98b81cb2020-10-02 18:32:04 +02003859/* Uses <appctx.ctx.stats.obj1> as a pointer to the current proxy and <obj2> as
3860 * a pointer to the current server/listener.
3861 */
Willy Tarreaucaff6312022-05-27 10:17:46 +02003862static int stats_dump_proxies(struct stconn *sc,
Amaury Denoyelle98b81cb2020-10-02 18:32:04 +02003863 struct htx *htx,
3864 struct uri_auth *uri)
3865{
Willy Tarreaucaff6312022-05-27 10:17:46 +02003866 struct appctx *appctx = __sc_appctx(sc);
Willy Tarreau91cefca2022-05-03 17:08:29 +02003867 struct show_stat_ctx *ctx = appctx->svcctx;
Willy Tarreaucaff6312022-05-27 10:17:46 +02003868 struct channel *rep = sc_ic(sc);
Amaury Denoyelle98b81cb2020-10-02 18:32:04 +02003869 struct proxy *px;
3870
3871 /* dump proxies */
Willy Tarreau91cefca2022-05-03 17:08:29 +02003872 while (ctx->obj1) {
Amaury Denoyelle98b81cb2020-10-02 18:32:04 +02003873 if (htx) {
Christopher Faulet7b3d38a2023-05-05 11:28:45 +02003874 if (htx_almost_full(htx)) {
3875 sc_need_room(sc, htx->size / 2);
Amaury Denoyelle98b81cb2020-10-02 18:32:04 +02003876 goto full;
Christopher Faulet7b3d38a2023-05-05 11:28:45 +02003877 }
Amaury Denoyelle98b81cb2020-10-02 18:32:04 +02003878 }
3879 else {
Christopher Faulet7b3d38a2023-05-05 11:28:45 +02003880 if (buffer_almost_full(&rep->buf)) {
3881 sc_need_room(sc, b_size(&rep->buf) / 2);
Amaury Denoyelle98b81cb2020-10-02 18:32:04 +02003882 goto full;
Christopher Faulet7b3d38a2023-05-05 11:28:45 +02003883 }
Amaury Denoyelle98b81cb2020-10-02 18:32:04 +02003884 }
3885
Willy Tarreau91cefca2022-05-03 17:08:29 +02003886 px = ctx->obj1;
Marno Krahmer07954fb2021-06-24 16:51:08 +02003887 /* Skip the global frontend proxies and non-networked ones.
William Lallemand85a16b22021-08-03 13:18:11 +02003888 * Also skip proxies that were disabled in the configuration
Marno Krahmer07954fb2021-06-24 16:51:08 +02003889 * This change allows retrieving stats from "old" proxies after a reload.
3890 */
Christopher Fauletdfd10ab2021-10-06 14:24:19 +02003891 if (!(px->flags & PR_FL_DISABLED) && px->uuid > 0 &&
William Lallemande7f74622021-07-28 17:45:18 +02003892 (px->cap & (PR_CAP_FE | PR_CAP_BE)) && !(px->cap & PR_CAP_INT)) {
Willy Tarreaucaff6312022-05-27 10:17:46 +02003893 if (stats_dump_proxy_to_buffer(sc, htx, px, uri) == 0)
Amaury Denoyelle98b81cb2020-10-02 18:32:04 +02003894 return 0;
3895 }
3896
Willy Tarreau91cefca2022-05-03 17:08:29 +02003897 ctx->obj1 = px->next;
3898 ctx->px_st = STAT_PX_ST_INIT;
Aurelien DARRAGON55941842022-12-15 14:01:04 +01003899 ctx->field = 0;
Amaury Denoyelle98b81cb2020-10-02 18:32:04 +02003900 }
3901
3902 return 1;
3903
3904 full:
Amaury Denoyelle98b81cb2020-10-02 18:32:04 +02003905 return 0;
3906}
3907
Willy Tarreau4596fe22022-05-17 19:07:51 +02003908/* This function dumps statistics onto the stream connector's read buffer in
William Lallemand74c24fb2016-11-21 17:18:36 +01003909 * either CSV or HTML format. <uri> contains some HTML-specific parameters that
3910 * are ignored for CSV format (hence <uri> may be NULL there). It returns 0 if
3911 * it had to stop writing data and an I/O is needed, 1 if the dump is finished
3912 * and the stream must be closed, or -1 in case of any error. This function is
3913 * used by both the CLI and the HTTP handlers.
3914 */
Willy Tarreaucaff6312022-05-27 10:17:46 +02003915static int stats_dump_stat_to_buffer(struct stconn *sc, struct htx *htx,
Christopher Fauletef779222018-10-31 08:47:01 +01003916 struct uri_auth *uri)
William Lallemand74c24fb2016-11-21 17:18:36 +01003917{
Willy Tarreaucaff6312022-05-27 10:17:46 +02003918 struct appctx *appctx = __sc_appctx(sc);
Willy Tarreau91cefca2022-05-03 17:08:29 +02003919 struct show_stat_ctx *ctx = appctx->svcctx;
Willy Tarreau91cefca2022-05-03 17:08:29 +02003920 enum stats_domain domain = ctx->domain;
William Lallemand74c24fb2016-11-21 17:18:36 +01003921
Christopher Fauleta8b76842022-12-16 15:08:36 +01003922 chunk_reset(&trash_chunk);
William Lallemand74c24fb2016-11-21 17:18:36 +01003923
Willy Tarreau41f88522022-05-03 18:39:27 +02003924 switch (ctx->state) {
Willy Tarreau6ef16482022-05-06 18:07:53 +02003925 case STAT_STATE_INIT:
3926 ctx->state = STAT_STATE_HEAD; /* let's start producing data */
Willy Tarreau9eb93c02022-11-14 07:12:05 +01003927 __fallthrough;
William Lallemand74c24fb2016-11-21 17:18:36 +01003928
Willy Tarreau6ef16482022-05-06 18:07:53 +02003929 case STAT_STATE_HEAD:
Willy Tarreau91cefca2022-05-03 17:08:29 +02003930 if (ctx->flags & STAT_FMT_HTML)
Willy Tarreau676c29e2019-10-09 10:50:01 +02003931 stats_dump_html_head(appctx, uri);
Willy Tarreau91cefca2022-05-03 17:08:29 +02003932 else if (ctx->flags & STAT_JSON_SCHM)
Christopher Fauleta8b76842022-12-16 15:08:36 +01003933 stats_dump_json_schema(&trash_chunk);
Willy Tarreau91cefca2022-05-03 17:08:29 +02003934 else if (ctx->flags & STAT_FMT_JSON)
Willy Tarreau9d7fb632017-04-11 07:53:04 +02003935 stats_dump_json_header();
Willy Tarreau91cefca2022-05-03 17:08:29 +02003936 else if (!(ctx->flags & STAT_FMT_TYPED))
3937 stats_dump_csv_header(ctx->domain);
William Lallemand74c24fb2016-11-21 17:18:36 +01003938
Christopher Fauletf4258bd2023-05-05 10:59:39 +02003939 if (!stats_putchk(appctx, htx))
Christopher Fauletef779222018-10-31 08:47:01 +01003940 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01003941
Willy Tarreau91cefca2022-05-03 17:08:29 +02003942 if (ctx->flags & STAT_JSON_SCHM) {
Willy Tarreau6ef16482022-05-06 18:07:53 +02003943 ctx->state = STAT_STATE_FIN;
Christopher Faulet6338a082019-09-09 15:50:54 +02003944 return 1;
3945 }
Willy Tarreau6ef16482022-05-06 18:07:53 +02003946 ctx->state = STAT_STATE_INFO;
Willy Tarreau9eb93c02022-11-14 07:12:05 +01003947 __fallthrough;
William Lallemand74c24fb2016-11-21 17:18:36 +01003948
Willy Tarreau6ef16482022-05-06 18:07:53 +02003949 case STAT_STATE_INFO:
Willy Tarreau91cefca2022-05-03 17:08:29 +02003950 if (ctx->flags & STAT_FMT_HTML) {
Willy Tarreaucaff6312022-05-27 10:17:46 +02003951 stats_dump_html_info(sc, uri);
Christopher Fauletf4258bd2023-05-05 10:59:39 +02003952 if (!stats_putchk(appctx, htx))
Christopher Fauletef779222018-10-31 08:47:01 +01003953 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01003954 }
3955
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02003956 if (domain == STATS_DOMAIN_PROXY)
Willy Tarreau91cefca2022-05-03 17:08:29 +02003957 ctx->obj1 = proxies_list;
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02003958
Willy Tarreau91cefca2022-05-03 17:08:29 +02003959 ctx->px_st = STAT_PX_ST_INIT;
Aurelien DARRAGON55941842022-12-15 14:01:04 +01003960 ctx->field = 0;
Willy Tarreau6ef16482022-05-06 18:07:53 +02003961 ctx->state = STAT_STATE_LIST;
Willy Tarreau9eb93c02022-11-14 07:12:05 +01003962 __fallthrough;
William Lallemand74c24fb2016-11-21 17:18:36 +01003963
Willy Tarreau6ef16482022-05-06 18:07:53 +02003964 case STAT_STATE_LIST:
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02003965 switch (domain) {
Emeric Brunf8642ee2021-10-29 17:59:18 +02003966 case STATS_DOMAIN_RESOLVERS:
Willy Tarreaucaff6312022-05-27 10:17:46 +02003967 if (!stats_dump_resolvers(sc, stat_l[domain],
Emeric Brund3b44952021-01-06 16:01:02 +01003968 stat_count[domain],
3969 &stats_module_list[domain])) {
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02003970 return 0;
3971 }
3972 break;
3973
3974 case STATS_DOMAIN_PROXY:
3975 default:
3976 /* dump proxies */
Willy Tarreaucaff6312022-05-27 10:17:46 +02003977 if (!stats_dump_proxies(sc, htx, uri))
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02003978 return 0;
3979 break;
3980 }
William Lallemand74c24fb2016-11-21 17:18:36 +01003981
Willy Tarreau6ef16482022-05-06 18:07:53 +02003982 ctx->state = STAT_STATE_END;
Willy Tarreau9eb93c02022-11-14 07:12:05 +01003983 __fallthrough;
William Lallemand74c24fb2016-11-21 17:18:36 +01003984
Willy Tarreau6ef16482022-05-06 18:07:53 +02003985 case STAT_STATE_END:
Willy Tarreau91cefca2022-05-03 17:08:29 +02003986 if (ctx->flags & (STAT_FMT_HTML|STAT_FMT_JSON)) {
3987 if (ctx->flags & STAT_FMT_HTML)
Simon Horman05ee2132017-01-04 09:37:25 +01003988 stats_dump_html_end();
3989 else
3990 stats_dump_json_end();
Christopher Fauletf4258bd2023-05-05 10:59:39 +02003991 if (!stats_putchk(appctx, htx))
Christopher Fauletef779222018-10-31 08:47:01 +01003992 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01003993 }
3994
Willy Tarreau6ef16482022-05-06 18:07:53 +02003995 ctx->state = STAT_STATE_FIN;
Willy Tarreau9eb93c02022-11-14 07:12:05 +01003996 __fallthrough;
William Lallemand74c24fb2016-11-21 17:18:36 +01003997
Willy Tarreau6ef16482022-05-06 18:07:53 +02003998 case STAT_STATE_FIN:
William Lallemand74c24fb2016-11-21 17:18:36 +01003999 return 1;
4000
4001 default:
4002 /* unknown state ! */
Willy Tarreau6ef16482022-05-06 18:07:53 +02004003 ctx->state = STAT_STATE_FIN;
William Lallemand74c24fb2016-11-21 17:18:36 +01004004 return -1;
4005 }
Christopher Fauletef779222018-10-31 08:47:01 +01004006
4007 full:
Christopher Fauletef779222018-10-31 08:47:01 +01004008 return 0;
4009
William Lallemand74c24fb2016-11-21 17:18:36 +01004010}
4011
4012/* We reached the stats page through a POST request. The appctx is
4013 * expected to have already been allocated by the caller.
4014 * Parse the posted data and enable/disable servers if necessary.
4015 * Returns 1 if request was parsed or zero if it needs more data.
4016 */
Willy Tarreaucaff6312022-05-27 10:17:46 +02004017static int stats_process_http_post(struct stconn *sc)
William Lallemand74c24fb2016-11-21 17:18:36 +01004018{
Willy Tarreaucaff6312022-05-27 10:17:46 +02004019 struct stream *s = __sc_strm(sc);
4020 struct appctx *appctx = __sc_appctx(sc);
Willy Tarreau91cefca2022-05-03 17:08:29 +02004021 struct show_stat_ctx *ctx = appctx->svcctx;
William Lallemand74c24fb2016-11-21 17:18:36 +01004022
4023 struct proxy *px = NULL;
4024 struct server *sv = NULL;
4025
4026 char key[LINESIZE];
4027 int action = ST_ADM_ACTION_NONE;
4028 int reprocess = 0;
4029
4030 int total_servers = 0;
4031 int altered_servers = 0;
4032
4033 char *first_param, *cur_param, *next_param, *end_params;
4034 char *st_cur_param = NULL;
4035 char *st_next_param = NULL;
4036
Christopher Fauleta3618372018-12-13 21:59:56 +01004037 struct buffer *temp = get_trash_chunk();
William Lallemand74c24fb2016-11-21 17:18:36 +01004038
Christopher Fauletb7f88902019-07-15 21:56:43 +02004039 struct htx *htx = htxbuf(&s->req.buf);
4040 struct htx_blk *blk;
Christopher Fauleta3618372018-12-13 21:59:56 +01004041
Christopher Fauletb7f88902019-07-15 21:56:43 +02004042 /* we need more data */
4043 if (s->txn->req.msg_state < HTTP_MSG_DONE) {
4044 /* check if we can receive more */
4045 if (htx_free_data_space(htx) <= global.tune.maxrewrite) {
Willy Tarreau91cefca2022-05-03 17:08:29 +02004046 ctx->st_code = STAT_STATUS_EXCD;
Christopher Fauletb7f88902019-07-15 21:56:43 +02004047 goto out;
Christopher Fauleta3618372018-12-13 21:59:56 +01004048 }
Christopher Fauletb7f88902019-07-15 21:56:43 +02004049 goto wait;
4050 }
Christopher Fauleta3618372018-12-13 21:59:56 +01004051
Christopher Fauletb7f88902019-07-15 21:56:43 +02004052 /* The request was fully received. Copy data */
4053 blk = htx_get_head_blk(htx);
4054 while (blk) {
4055 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Fauleta3618372018-12-13 21:59:56 +01004056
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01004057 if (type == HTX_BLK_TLR || type == HTX_BLK_EOT)
Christopher Fauletb7f88902019-07-15 21:56:43 +02004058 break;
4059 if (type == HTX_BLK_DATA) {
4060 struct ist v = htx_get_blk_value(htx, blk);
Christopher Fauleta3618372018-12-13 21:59:56 +01004061
Christopher Fauletb7f88902019-07-15 21:56:43 +02004062 if (!chunk_memcat(temp, v.ptr, v.len)) {
Willy Tarreau91cefca2022-05-03 17:08:29 +02004063 ctx->st_code = STAT_STATUS_EXCD;
Christopher Faulet2f9a41d2019-02-27 15:30:57 +01004064 goto out;
4065 }
Christopher Fauleta3618372018-12-13 21:59:56 +01004066 }
Christopher Fauletb7f88902019-07-15 21:56:43 +02004067 blk = htx_get_next_blk(htx, blk);
William Lallemand74c24fb2016-11-21 17:18:36 +01004068 }
4069
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004070 first_param = temp->area;
Christopher Fauleta3618372018-12-13 21:59:56 +01004071 end_params = temp->area + temp->data;
William Lallemand74c24fb2016-11-21 17:18:36 +01004072 cur_param = next_param = end_params;
4073 *end_params = '\0';
4074
Willy Tarreau91cefca2022-05-03 17:08:29 +02004075 ctx->st_code = STAT_STATUS_NONE;
William Lallemand74c24fb2016-11-21 17:18:36 +01004076
4077 /*
4078 * Parse the parameters in reverse order to only store the last value.
4079 * From the html form, the backend and the action are at the end.
4080 */
4081 while (cur_param > first_param) {
4082 char *value;
4083 int poffset, plen;
4084
4085 cur_param--;
4086
4087 if ((*cur_param == '&') || (cur_param == first_param)) {
4088 reprocess_servers:
4089 /* Parse the key */
4090 poffset = (cur_param != first_param ? 1 : 0);
4091 plen = next_param - cur_param + (cur_param == first_param ? 1 : 0);
4092 if ((plen > 0) && (plen <= sizeof(key))) {
4093 strncpy(key, cur_param + poffset, plen);
4094 key[plen - 1] = '\0';
4095 } else {
Willy Tarreau91cefca2022-05-03 17:08:29 +02004096 ctx->st_code = STAT_STATUS_ERRP;
William Lallemand74c24fb2016-11-21 17:18:36 +01004097 goto out;
4098 }
4099
4100 /* Parse the value */
4101 value = key;
4102 while (*value != '\0' && *value != '=') {
4103 value++;
4104 }
4105 if (*value == '=') {
4106 /* Ok, a value is found, we can mark the end of the key */
4107 *value++ = '\0';
4108 }
Willy Tarreau62ba9ba2020-04-23 17:54:47 +02004109 if (url_decode(key, 1) < 0 || url_decode(value, 1) < 0)
William Lallemand74c24fb2016-11-21 17:18:36 +01004110 break;
4111
4112 /* Now we can check the key to see what to do */
4113 if (!px && (strcmp(key, "b") == 0)) {
4114 if ((px = proxy_be_by_name(value)) == NULL) {
4115 /* the backend name is unknown or ambiguous (duplicate names) */
Willy Tarreau91cefca2022-05-03 17:08:29 +02004116 ctx->st_code = STAT_STATUS_ERRP;
William Lallemand74c24fb2016-11-21 17:18:36 +01004117 goto out;
4118 }
4119 }
4120 else if (!action && (strcmp(key, "action") == 0)) {
4121 if (strcmp(value, "ready") == 0) {
4122 action = ST_ADM_ACTION_READY;
4123 }
4124 else if (strcmp(value, "drain") == 0) {
4125 action = ST_ADM_ACTION_DRAIN;
4126 }
4127 else if (strcmp(value, "maint") == 0) {
4128 action = ST_ADM_ACTION_MAINT;
4129 }
4130 else if (strcmp(value, "shutdown") == 0) {
4131 action = ST_ADM_ACTION_SHUTDOWN;
4132 }
4133 else if (strcmp(value, "dhlth") == 0) {
4134 action = ST_ADM_ACTION_DHLTH;
4135 }
4136 else if (strcmp(value, "ehlth") == 0) {
4137 action = ST_ADM_ACTION_EHLTH;
4138 }
4139 else if (strcmp(value, "hrunn") == 0) {
4140 action = ST_ADM_ACTION_HRUNN;
4141 }
4142 else if (strcmp(value, "hnolb") == 0) {
4143 action = ST_ADM_ACTION_HNOLB;
4144 }
4145 else if (strcmp(value, "hdown") == 0) {
4146 action = ST_ADM_ACTION_HDOWN;
4147 }
4148 else if (strcmp(value, "dagent") == 0) {
4149 action = ST_ADM_ACTION_DAGENT;
4150 }
4151 else if (strcmp(value, "eagent") == 0) {
4152 action = ST_ADM_ACTION_EAGENT;
4153 }
4154 else if (strcmp(value, "arunn") == 0) {
4155 action = ST_ADM_ACTION_ARUNN;
4156 }
4157 else if (strcmp(value, "adown") == 0) {
4158 action = ST_ADM_ACTION_ADOWN;
4159 }
4160 /* else these are the old supported methods */
4161 else if (strcmp(value, "disable") == 0) {
4162 action = ST_ADM_ACTION_DISABLE;
4163 }
4164 else if (strcmp(value, "enable") == 0) {
4165 action = ST_ADM_ACTION_ENABLE;
4166 }
4167 else if (strcmp(value, "stop") == 0) {
4168 action = ST_ADM_ACTION_STOP;
4169 }
4170 else if (strcmp(value, "start") == 0) {
4171 action = ST_ADM_ACTION_START;
4172 }
4173 else {
Willy Tarreau91cefca2022-05-03 17:08:29 +02004174 ctx->st_code = STAT_STATUS_ERRP;
William Lallemand74c24fb2016-11-21 17:18:36 +01004175 goto out;
4176 }
4177 }
4178 else if (strcmp(key, "s") == 0) {
4179 if (!(px && action)) {
4180 /*
4181 * Indicates that we'll need to reprocess the parameters
4182 * as soon as backend and action are known
4183 */
4184 if (!reprocess) {
4185 st_cur_param = cur_param;
4186 st_next_param = next_param;
4187 }
4188 reprocess = 1;
4189 }
4190 else if ((sv = findserver(px, value)) != NULL) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004191 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
William Lallemand74c24fb2016-11-21 17:18:36 +01004192 switch (action) {
4193 case ST_ADM_ACTION_DISABLE:
Emeric Brun52a91d32017-08-31 14:41:55 +02004194 if (!(sv->cur_admin & SRV_ADMF_FMAINT)) {
William Lallemand74c24fb2016-11-21 17:18:36 +01004195 altered_servers++;
4196 total_servers++;
Aurelien DARRAGON9b1ccd72023-04-03 17:40:28 +02004197 srv_set_admin_flag(sv, SRV_ADMF_FMAINT, SRV_ADM_STCHGC_STATS_DISABLE);
William Lallemand74c24fb2016-11-21 17:18:36 +01004198 }
4199 break;
4200 case ST_ADM_ACTION_ENABLE:
Emeric Brun52a91d32017-08-31 14:41:55 +02004201 if (sv->cur_admin & SRV_ADMF_FMAINT) {
William Lallemand74c24fb2016-11-21 17:18:36 +01004202 altered_servers++;
4203 total_servers++;
4204 srv_clr_admin_flag(sv, SRV_ADMF_FMAINT);
4205 }
4206 break;
4207 case ST_ADM_ACTION_STOP:
Emeric Brun52a91d32017-08-31 14:41:55 +02004208 if (!(sv->cur_admin & SRV_ADMF_FDRAIN)) {
Aurelien DARRAGON9b1ccd72023-04-03 17:40:28 +02004209 srv_set_admin_flag(sv, SRV_ADMF_FDRAIN, SRV_ADM_STCHGC_STATS_STOP);
William Lallemand74c24fb2016-11-21 17:18:36 +01004210 altered_servers++;
4211 total_servers++;
4212 }
4213 break;
4214 case ST_ADM_ACTION_START:
Emeric Brun52a91d32017-08-31 14:41:55 +02004215 if (sv->cur_admin & SRV_ADMF_FDRAIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +01004216 srv_clr_admin_flag(sv, SRV_ADMF_FDRAIN);
4217 altered_servers++;
4218 total_servers++;
4219 }
4220 break;
4221 case ST_ADM_ACTION_DHLTH:
4222 if (sv->check.state & CHK_ST_CONFIGURED) {
4223 sv->check.state &= ~CHK_ST_ENABLED;
4224 altered_servers++;
4225 total_servers++;
4226 }
4227 break;
4228 case ST_ADM_ACTION_EHLTH:
4229 if (sv->check.state & CHK_ST_CONFIGURED) {
4230 sv->check.state |= CHK_ST_ENABLED;
4231 altered_servers++;
4232 total_servers++;
4233 }
4234 break;
4235 case ST_ADM_ACTION_HRUNN:
4236 if (!(sv->track)) {
4237 sv->check.health = sv->check.rise + sv->check.fall - 1;
Aurelien DARRAGON1746b562023-04-04 10:17:40 +02004238 srv_set_running(sv, SRV_OP_STCHGC_STATS_WEB);
William Lallemand74c24fb2016-11-21 17:18:36 +01004239 altered_servers++;
4240 total_servers++;
4241 }
4242 break;
4243 case ST_ADM_ACTION_HNOLB:
4244 if (!(sv->track)) {
4245 sv->check.health = sv->check.rise + sv->check.fall - 1;
Aurelien DARRAGON1746b562023-04-04 10:17:40 +02004246 srv_set_stopping(sv, SRV_OP_STCHGC_STATS_WEB);
William Lallemand74c24fb2016-11-21 17:18:36 +01004247 altered_servers++;
4248 total_servers++;
4249 }
4250 break;
4251 case ST_ADM_ACTION_HDOWN:
4252 if (!(sv->track)) {
4253 sv->check.health = 0;
Aurelien DARRAGON1746b562023-04-04 10:17:40 +02004254 srv_set_stopped(sv, SRV_OP_STCHGC_STATS_WEB);
William Lallemand74c24fb2016-11-21 17:18:36 +01004255 altered_servers++;
4256 total_servers++;
4257 }
4258 break;
4259 case ST_ADM_ACTION_DAGENT:
4260 if (sv->agent.state & CHK_ST_CONFIGURED) {
4261 sv->agent.state &= ~CHK_ST_ENABLED;
4262 altered_servers++;
4263 total_servers++;
4264 }
4265 break;
4266 case ST_ADM_ACTION_EAGENT:
4267 if (sv->agent.state & CHK_ST_CONFIGURED) {
4268 sv->agent.state |= CHK_ST_ENABLED;
4269 altered_servers++;
4270 total_servers++;
4271 }
4272 break;
4273 case ST_ADM_ACTION_ARUNN:
4274 if (sv->agent.state & CHK_ST_ENABLED) {
4275 sv->agent.health = sv->agent.rise + sv->agent.fall - 1;
Aurelien DARRAGON1746b562023-04-04 10:17:40 +02004276 srv_set_running(sv, SRV_OP_STCHGC_STATS_WEB);
William Lallemand74c24fb2016-11-21 17:18:36 +01004277 altered_servers++;
4278 total_servers++;
4279 }
4280 break;
4281 case ST_ADM_ACTION_ADOWN:
4282 if (sv->agent.state & CHK_ST_ENABLED) {
4283 sv->agent.health = 0;
Aurelien DARRAGON1746b562023-04-04 10:17:40 +02004284 srv_set_stopped(sv, SRV_OP_STCHGC_STATS_WEB);
William Lallemand74c24fb2016-11-21 17:18:36 +01004285 altered_servers++;
4286 total_servers++;
4287 }
4288 break;
4289 case ST_ADM_ACTION_READY:
4290 srv_adm_set_ready(sv);
4291 altered_servers++;
4292 total_servers++;
4293 break;
4294 case ST_ADM_ACTION_DRAIN:
4295 srv_adm_set_drain(sv);
4296 altered_servers++;
4297 total_servers++;
4298 break;
4299 case ST_ADM_ACTION_MAINT:
4300 srv_adm_set_maint(sv);
4301 altered_servers++;
4302 total_servers++;
4303 break;
4304 case ST_ADM_ACTION_SHUTDOWN:
Christopher Fauletdfd10ab2021-10-06 14:24:19 +02004305 if (!(px->flags & (PR_FL_DISABLED|PR_FL_STOPPED))) {
Willy Tarreauaf7ea812019-11-14 16:42:04 +01004306 srv_shutdown_streams(sv, SF_ERR_KILLED);
William Lallemand74c24fb2016-11-21 17:18:36 +01004307 altered_servers++;
4308 total_servers++;
4309 }
4310 break;
4311 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004312 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
William Lallemand74c24fb2016-11-21 17:18:36 +01004313 } else {
4314 /* the server name is unknown or ambiguous (duplicate names) */
4315 total_servers++;
4316 }
4317 }
4318 if (reprocess && px && action) {
4319 /* Now, we know the backend and the action chosen by the user.
4320 * We can safely restart from the first server parameter
4321 * to reprocess them
4322 */
4323 cur_param = st_cur_param;
4324 next_param = st_next_param;
4325 reprocess = 0;
4326 goto reprocess_servers;
4327 }
4328
4329 next_param = cur_param;
4330 }
4331 }
4332
4333 if (total_servers == 0) {
Willy Tarreau91cefca2022-05-03 17:08:29 +02004334 ctx->st_code = STAT_STATUS_NONE;
William Lallemand74c24fb2016-11-21 17:18:36 +01004335 }
4336 else if (altered_servers == 0) {
Willy Tarreau91cefca2022-05-03 17:08:29 +02004337 ctx->st_code = STAT_STATUS_ERRP;
William Lallemand74c24fb2016-11-21 17:18:36 +01004338 }
4339 else if (altered_servers == total_servers) {
Willy Tarreau91cefca2022-05-03 17:08:29 +02004340 ctx->st_code = STAT_STATUS_DONE;
William Lallemand74c24fb2016-11-21 17:18:36 +01004341 }
4342 else {
Willy Tarreau91cefca2022-05-03 17:08:29 +02004343 ctx->st_code = STAT_STATUS_PART;
William Lallemand74c24fb2016-11-21 17:18:36 +01004344 }
4345 out:
4346 return 1;
Christopher Faulet2f9a41d2019-02-27 15:30:57 +01004347 wait:
Willy Tarreau91cefca2022-05-03 17:08:29 +02004348 ctx->st_code = STAT_STATUS_NONE;
Christopher Faulet2f9a41d2019-02-27 15:30:57 +01004349 return 0;
Christopher Fauletef779222018-10-31 08:47:01 +01004350}
4351
4352
Willy Tarreaucaff6312022-05-27 10:17:46 +02004353static int stats_send_http_headers(struct stconn *sc, struct htx *htx)
Christopher Fauletef779222018-10-31 08:47:01 +01004354{
Willy Tarreaucaff6312022-05-27 10:17:46 +02004355 struct stream *s = __sc_strm(sc);
Christopher Fauletef779222018-10-31 08:47:01 +01004356 struct uri_auth *uri = s->be->uri_auth;
Willy Tarreaucaff6312022-05-27 10:17:46 +02004357 struct appctx *appctx = __sc_appctx(sc);
Willy Tarreau91cefca2022-05-03 17:08:29 +02004358 struct show_stat_ctx *ctx = appctx->svcctx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004359 struct htx_sl *sl;
4360 unsigned int flags;
Christopher Fauletef779222018-10-31 08:47:01 +01004361
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004362 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);
4363 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), ist("200"), ist("OK"));
4364 if (!sl)
Christopher Fauletef779222018-10-31 08:47:01 +01004365 goto full;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004366 sl->info.res.status = 200;
Christopher Fauletef779222018-10-31 08:47:01 +01004367
Christopher Fauletb829f4c2019-03-29 16:13:55 +01004368 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")))
Christopher Fauletef779222018-10-31 08:47:01 +01004369 goto full;
Willy Tarreau91cefca2022-05-03 17:08:29 +02004370 if (ctx->flags & STAT_FMT_HTML) {
Christopher Fauletef779222018-10-31 08:47:01 +01004371 if (!htx_add_header(htx, ist("Content-Type"), ist("text/html")))
4372 goto full;
4373 }
Willy Tarreau91cefca2022-05-03 17:08:29 +02004374 else if (ctx->flags & (STAT_FMT_JSON|STAT_JSON_SCHM)) {
Christopher Faulet6338a082019-09-09 15:50:54 +02004375 if (!htx_add_header(htx, ist("Content-Type"), ist("application/json")))
4376 goto full;
4377 }
Christopher Fauletef779222018-10-31 08:47:01 +01004378 else {
4379 if (!htx_add_header(htx, ist("Content-Type"), ist("text/plain")))
4380 goto full;
4381 }
4382
Willy Tarreau91cefca2022-05-03 17:08:29 +02004383 if (uri->refresh > 0 && !(ctx->flags & STAT_NO_REFRESH)) {
Christopher Fauletef779222018-10-31 08:47:01 +01004384 const char *refresh = U2A(uri->refresh);
Tim Duesterhusdcf753a2021-03-04 17:31:47 +01004385 if (!htx_add_header(htx, ist("Refresh"), ist(refresh)))
Christopher Fauletef779222018-10-31 08:47:01 +01004386 goto full;
4387 }
4388
Willy Tarreau91cefca2022-05-03 17:08:29 +02004389 if (ctx->flags & STAT_CHUNKED) {
Christopher Fauletef779222018-10-31 08:47:01 +01004390 if (!htx_add_header(htx, ist("Transfer-Encoding"), ist("chunked")))
4391 goto full;
4392 }
4393
4394 if (!htx_add_endof(htx, HTX_BLK_EOH))
4395 goto full;
4396
Christopher Faulet1e2d6362019-02-27 16:28:48 +01004397 channel_add_input(&s->res, htx->data);
Christopher Fauletef779222018-10-31 08:47:01 +01004398 return 1;
4399
4400 full:
4401 htx_reset(htx);
Christopher Faulet7b3d38a2023-05-05 11:28:45 +02004402 sc_need_room(sc, 0);
Christopher Fauletef779222018-10-31 08:47:01 +01004403 return 0;
William Lallemand74c24fb2016-11-21 17:18:36 +01004404}
4405
Christopher Fauletef779222018-10-31 08:47:01 +01004406
Willy Tarreaucaff6312022-05-27 10:17:46 +02004407static int stats_send_http_redirect(struct stconn *sc, struct htx *htx)
Christopher Fauletef779222018-10-31 08:47:01 +01004408{
4409 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
Willy Tarreaucaff6312022-05-27 10:17:46 +02004410 struct stream *s = __sc_strm(sc);
Christopher Fauletef779222018-10-31 08:47:01 +01004411 struct uri_auth *uri = s->be->uri_auth;
Willy Tarreaucaff6312022-05-27 10:17:46 +02004412 struct appctx *appctx = __sc_appctx(sc);
Willy Tarreau91cefca2022-05-03 17:08:29 +02004413 struct show_stat_ctx *ctx = appctx->svcctx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004414 struct htx_sl *sl;
4415 unsigned int flags;
Christopher Fauletef779222018-10-31 08:47:01 +01004416
Willy Tarreau91cefca2022-05-03 17:08:29 +02004417 /* scope_txt = search pattern + search query, ctx->scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
Christopher Fauletef779222018-10-31 08:47:01 +01004418 scope_txt[0] = 0;
Willy Tarreau91cefca2022-05-03 17:08:29 +02004419 if (ctx->scope_len) {
Willy Tarreaucaff6312022-05-27 10:17:46 +02004420 const char *scope_ptr = stats_scope_ptr(appctx, sc);
Christopher Fauleted7a0662019-01-14 11:07:34 +01004421
Willy Tarreaufc458ec2023-04-07 18:11:39 +02004422 strlcpy2(scope_txt, STAT_SCOPE_PATTERN, sizeof(scope_txt));
Willy Tarreau91cefca2022-05-03 17:08:29 +02004423 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), scope_ptr, ctx->scope_len);
4424 scope_txt[strlen(STAT_SCOPE_PATTERN) + ctx->scope_len] = 0;
Christopher Fauletef779222018-10-31 08:47:01 +01004425 }
4426
4427 /* We don't want to land on the posted stats page because a refresh will
4428 * repost the data. We don't want this to happen on accident so we redirect
4429 * the browse to the stats page with a GET.
4430 */
4431 chunk_printf(&trash, "%s;st=%s%s%s%s",
4432 uri->uri_prefix,
Willy Tarreau91cefca2022-05-03 17:08:29 +02004433 ((ctx->st_code > STAT_STATUS_INIT) &&
4434 (ctx->st_code < STAT_STATUS_SIZE) &&
4435 stat_status_codes[ctx->st_code]) ?
4436 stat_status_codes[ctx->st_code] :
Christopher Fauletef779222018-10-31 08:47:01 +01004437 stat_status_codes[STAT_STATUS_UNKN],
Willy Tarreau91cefca2022-05-03 17:08:29 +02004438 (ctx->flags & STAT_HIDE_DOWN) ? ";up" : "",
4439 (ctx->flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Christopher Fauletef779222018-10-31 08:47:01 +01004440 scope_txt);
4441
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004442 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_CHNK);
4443 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), ist("303"), ist("See Other"));
4444 if (!sl)
Christopher Fauletef779222018-10-31 08:47:01 +01004445 goto full;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004446 sl->info.res.status = 303;
Christopher Fauletef779222018-10-31 08:47:01 +01004447
4448 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")) ||
Christopher Fauletef779222018-10-31 08:47:01 +01004449 !htx_add_header(htx, ist("Content-Type"), ist("text/plain")) ||
4450 !htx_add_header(htx, ist("Content-Length"), ist("0")) ||
4451 !htx_add_header(htx, ist("Location"), ist2(trash.area, trash.data)))
4452 goto full;
4453
4454 if (!htx_add_endof(htx, HTX_BLK_EOH))
4455 goto full;
4456
Christopher Faulet1e2d6362019-02-27 16:28:48 +01004457 channel_add_input(&s->res, htx->data);
Christopher Fauletef779222018-10-31 08:47:01 +01004458 return 1;
4459
4460full:
4461 htx_reset(htx);
Christopher Faulet7b3d38a2023-05-05 11:28:45 +02004462 sc_need_room(sc, 0);
Christopher Fauletef779222018-10-31 08:47:01 +01004463 return 0;
4464}
William Lallemand74c24fb2016-11-21 17:18:36 +01004465
Simon Horman05ee2132017-01-04 09:37:25 +01004466
Willy Tarreau4596fe22022-05-17 19:07:51 +02004467/* This I/O handler runs as an applet embedded in a stream connector. It is
William Lallemand74c24fb2016-11-21 17:18:36 +01004468 * used to send HTTP stats over a TCP socket. The mechanism is very simple.
4469 * appctx->st0 contains the operation in progress (dump, done). The handler
4470 * automatically unregisters itself once transfer is complete.
4471 */
Christopher Fauletb7f88902019-07-15 21:56:43 +02004472static void http_stats_io_handler(struct appctx *appctx)
Christopher Fauletef779222018-10-31 08:47:01 +01004473{
Willy Tarreau91cefca2022-05-03 17:08:29 +02004474 struct show_stat_ctx *ctx = appctx->svcctx;
Willy Tarreauc12b3212022-05-27 11:08:15 +02004475 struct stconn *sc = appctx_sc(appctx);
Willy Tarreaucaff6312022-05-27 10:17:46 +02004476 struct stream *s = __sc_strm(sc);
4477 struct channel *req = sc_oc(sc);
4478 struct channel *res = sc_ic(sc);
Christopher Fauletef779222018-10-31 08:47:01 +01004479 struct htx *req_htx, *res_htx;
4480
Amaury Denoyelle072f97e2020-10-05 11:49:37 +02004481 /* only proxy stats are available via http */
Willy Tarreau91cefca2022-05-03 17:08:29 +02004482 ctx->domain = STATS_DOMAIN_PROXY;
Amaury Denoyelle072f97e2020-10-05 11:49:37 +02004483
Christopher Fauletef779222018-10-31 08:47:01 +01004484 res_htx = htx_from_buf(&res->buf);
4485
Christopher Faulet9837bd82023-04-11 07:41:30 +02004486 if (unlikely(se_fl_test(appctx->sedesc, (SE_FL_EOS|SE_FL_ERROR|SE_FL_SHR|SE_FL_SHW)))) {
4487 appctx->st0 = STAT_HTTP_END;
Christopher Fauletef779222018-10-31 08:47:01 +01004488 goto out;
Christopher Faulet9837bd82023-04-11 07:41:30 +02004489 }
Christopher Fauletef779222018-10-31 08:47:01 +01004490
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05004491 /* Check if the input buffer is available. */
Christopher Fauletef779222018-10-31 08:47:01 +01004492 if (!b_size(&res->buf)) {
Christopher Faulet7b3d38a2023-05-05 11:28:45 +02004493 sc_need_room(sc, 0);
Christopher Fauletef779222018-10-31 08:47:01 +01004494 goto out;
4495 }
4496
Christopher Fauletef779222018-10-31 08:47:01 +01004497 /* all states are processed in sequence */
4498 if (appctx->st0 == STAT_HTTP_HEAD) {
Willy Tarreaucaff6312022-05-27 10:17:46 +02004499 if (stats_send_http_headers(sc, res_htx)) {
Christopher Fauletef779222018-10-31 08:47:01 +01004500 if (s->txn->meth == HTTP_METH_HEAD)
4501 appctx->st0 = STAT_HTTP_DONE;
4502 else
4503 appctx->st0 = STAT_HTTP_DUMP;
4504 }
4505 }
4506
4507 if (appctx->st0 == STAT_HTTP_DUMP) {
Aurelien DARRAGON14656842023-02-03 08:31:42 +01004508 trash_chunk = b_make(trash.area, res->buf.size, 0, 0);
Aurelien DARRAGON5e7ecbe2023-02-02 15:03:12 +01004509 /* adjust buffer size to take htx overhead into account,
4510 * make sure to perform this call on an empty buffer
4511 */
4512 trash_chunk.size = buf_room_for_htx_data(&trash_chunk);
Willy Tarreaucaff6312022-05-27 10:17:46 +02004513 if (stats_dump_stat_to_buffer(sc, res_htx, s->be->uri_auth))
Christopher Fauletef779222018-10-31 08:47:01 +01004514 appctx->st0 = STAT_HTTP_DONE;
4515 }
4516
4517 if (appctx->st0 == STAT_HTTP_POST) {
Willy Tarreaucaff6312022-05-27 10:17:46 +02004518 if (stats_process_http_post(sc))
Christopher Fauletef779222018-10-31 08:47:01 +01004519 appctx->st0 = STAT_HTTP_LAST;
Christopher Fauletca5309a2023-04-17 16:17:32 +02004520 else if (s->scf->flags & (SC_FL_EOS|SC_FL_ABRT_DONE))
Christopher Fauletef779222018-10-31 08:47:01 +01004521 appctx->st0 = STAT_HTTP_DONE;
4522 }
4523
4524 if (appctx->st0 == STAT_HTTP_LAST) {
Willy Tarreaucaff6312022-05-27 10:17:46 +02004525 if (stats_send_http_redirect(sc, res_htx))
Christopher Fauletef779222018-10-31 08:47:01 +01004526 appctx->st0 = STAT_HTTP_DONE;
4527 }
4528
4529 if (appctx->st0 == STAT_HTTP_DONE) {
Christopher Faulet08b45cb2022-04-07 09:25:13 +02004530 /* no more data are expected. If the response buffer is empty,
4531 * be sure to add something (EOT block in this case) to have
4532 * something to send. It is important to be sure the EOM flags
4533 * will be handled by the endpoint.
4534 */
4535 if (htx_is_empty(res_htx)) {
4536 if (!htx_add_endof(res_htx, HTX_BLK_EOT)) {
Christopher Faulet7b3d38a2023-05-05 11:28:45 +02004537 sc_need_room(sc, sizeof(struct htx_blk) + 1);
Christopher Faulet08b45cb2022-04-07 09:25:13 +02004538 goto out;
4539 }
4540 channel_add_input(res, 1);
4541 }
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01004542 res_htx->flags |= HTX_FL_EOM;
Willy Tarreaud869e132022-05-17 18:05:31 +02004543 se_fl_set(appctx->sedesc, SE_FL_EOI);
Christopher Faulet3a78aa62019-02-27 16:19:48 +01004544 appctx->st0 = STAT_HTTP_END;
Christopher Fauletef779222018-10-31 08:47:01 +01004545 }
4546
Christopher Faulet3a78aa62019-02-27 16:19:48 +01004547 if (appctx->st0 == STAT_HTTP_END) {
Christopher Fauletdf15a5d2023-03-31 11:28:22 +02004548 se_fl_set(appctx->sedesc, SE_FL_EOS);
Christopher Fauletc2c043e2023-03-22 09:30:40 +01004549 applet_will_consume(appctx);
Christopher Fauletef779222018-10-31 08:47:01 +01004550 }
Christopher Faulet3a78aa62019-02-27 16:19:48 +01004551
Christopher Fauletef779222018-10-31 08:47:01 +01004552 out:
4553 /* we have left the request in the buffer for the case where we
4554 * process a POST, and this automatically re-enables activity on
4555 * read. It's better to indicate that we want to stop reading when
4556 * we're sending, so that we know there's at most one direction
4557 * deciding to wake the applet up. It saves it from looping when
4558 * emitting large blocks into small TCP windows.
4559 */
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01004560 htx_to_buf(res_htx, &res->buf);
Christopher Fauletc2c043e2023-03-22 09:30:40 +01004561 if (appctx->st0 == STAT_HTTP_END) {
4562 /* eat the whole request */
4563 if (co_data(req)) {
4564 req_htx = htx_from_buf(&req->buf);
4565 co_htx_skip(req, req_htx, co_data(req));
4566 htx_to_buf(req_htx, &req->buf);
4567 }
4568 }
4569 else if (!channel_is_empty(res))
Willy Tarreau75a8f8e2022-05-25 18:12:11 +02004570 applet_wont_consume(appctx);
Christopher Fauletef779222018-10-31 08:47:01 +01004571}
4572
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004573/* Dump all fields from <info> into <out> using the "show info" format (name: value) */
Willy Tarreau83061a82018-07-13 11:56:34 +02004574static int stats_dump_info_fields(struct buffer *out,
Aurelien DARRAGONe76a0272022-12-15 12:10:03 +01004575 const struct field *info,
4576 struct show_stat_ctx *ctx)
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004577{
Aurelien DARRAGONe76a0272022-12-15 12:10:03 +01004578 int flags = ctx->flags;
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004579 int field;
4580
4581 for (field = 0; field < INF_TOTAL_FIELDS; field++) {
4582 if (!field_format(info, field))
4583 continue;
4584
Willy Tarreaueaa55372019-10-09 07:39:11 +02004585 if (!chunk_appendf(out, "%s: ", info_fields[field].name))
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004586 return 0;
4587 if (!stats_emit_raw_data_field(out, &info[field]))
4588 return 0;
Willy Tarreau6b19b142019-10-09 15:44:21 +02004589 if ((flags & STAT_SHOW_FDESC) && !chunk_appendf(out, ":\"%s\"", info_fields[field].desc))
4590 return 0;
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004591 if (!chunk_strcat(out, "\n"))
4592 return 0;
4593 }
4594 return 1;
4595}
4596
4597/* Dump all fields from <info> into <out> using the "show info typed" format */
Willy Tarreau83061a82018-07-13 11:56:34 +02004598static int stats_dump_typed_info_fields(struct buffer *out,
Aurelien DARRAGONe76a0272022-12-15 12:10:03 +01004599 const struct field *info,
4600 struct show_stat_ctx *ctx)
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004601{
Aurelien DARRAGONe76a0272022-12-15 12:10:03 +01004602 int flags = ctx->flags;
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004603 int field;
4604
4605 for (field = 0; field < INF_TOTAL_FIELDS; field++) {
4606 if (!field_format(info, field))
4607 continue;
4608
Willy Tarreaueaa55372019-10-09 07:39:11 +02004609 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 +01004610 return 0;
4611 if (!stats_emit_field_tags(out, &info[field], ':'))
4612 return 0;
4613 if (!stats_emit_typed_data_field(out, &info[field]))
4614 return 0;
Willy Tarreau6b19b142019-10-09 15:44:21 +02004615 if ((flags & STAT_SHOW_FDESC) && !chunk_appendf(out, ":\"%s\"", info_fields[field].desc))
4616 return 0;
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004617 if (!chunk_strcat(out, "\n"))
4618 return 0;
4619 }
4620 return 1;
4621}
4622
Willy Tarreau0b26b382021-05-08 07:43:53 +02004623/* Fill <info> with HAProxy global info. <info> is preallocated array of length
4624 * <len>. The length of the array must be INF_TOTAL_FIELDS. If this length is
4625 * less then this value, the function returns 0, otherwise, it returns 1. Some
4626 * fields' presence or precision may depend on some of the STAT_* flags present
4627 * in <flags>.
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004628 */
Willy Tarreau0b26b382021-05-08 07:43:53 +02004629int stats_fill_info(struct field *info, int len, uint flags)
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004630{
Willy Tarreau83061a82018-07-13 11:56:34 +02004631 struct buffer *out = get_trash_chunk();
Willy Tarreau6be8d092023-01-12 16:08:41 +01004632 uint64_t glob_out_bytes, glob_spl_bytes, glob_out_b32;
Willy Tarreau563efe62023-04-27 14:41:37 +02004633 uint up_sec, up_usec;
4634 ullong up;
Willy Tarreau5723b382023-05-17 09:05:20 +02004635 ulong boot;
Willy Tarreau6be8d092023-01-12 16:08:41 +01004636 int thr;
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004637
4638#ifdef USE_OPENSSL
Willy Tarreauc5977722021-05-08 08:14:04 +02004639 double ssl_sess_rate = read_freq_ctr_flt(&global.ssl_per_sec);
4640 double ssl_key_rate = read_freq_ctr_flt(&global.ssl_fe_keys_per_sec);
4641 double ssl_reuse = 0;
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004642
Willy Tarreauc5977722021-05-08 08:14:04 +02004643 if (ssl_key_rate < ssl_sess_rate)
4644 ssl_reuse = 100.0 * (1.0 - ssl_key_rate / ssl_sess_rate);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004645#endif
4646
Willy Tarreau6be8d092023-01-12 16:08:41 +01004647 /* sum certain per-thread totals (mostly byte counts) */
4648 glob_out_bytes = glob_spl_bytes = glob_out_b32 = 0;
4649 for (thr = 0; thr < global.nbthread; thr++) {
4650 glob_out_bytes += HA_ATOMIC_LOAD(&ha_thread_ctx[thr].out_bytes);
4651 glob_spl_bytes += HA_ATOMIC_LOAD(&ha_thread_ctx[thr].spliced_out_bytes);
4652 glob_out_b32 += read_freq_ctr(&ha_thread_ctx[thr].out_32bps);
4653 }
4654 glob_out_b32 *= 32; // values are 32-byte units
4655
Willy Tarreauc05d30e2023-04-28 14:50:29 +02004656 up = now_ns - start_time_ns;
Willy Tarreau563efe62023-04-27 14:41:37 +02004657 up_sec = ns_to_sec(up);
4658 up_usec = (up / 1000U) % 1000000U;
Willy Tarreaud37e26e2021-05-08 07:40:52 +02004659
Willy Tarreau5723b382023-05-17 09:05:20 +02004660 boot = tv_ms_remain(&start_date, &ready_date);
4661
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004662 if (len < INF_TOTAL_FIELDS)
4663 return 0;
4664
4665 chunk_reset(out);
4666 memset(info, 0, sizeof(*info) * len);
4667
4668 info[INF_NAME] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, PRODUCT_NAME);
Willy Tarreau909b9d82019-01-04 18:20:32 +01004669 info[INF_VERSION] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, haproxy_version);
Adis Nezirovicb62b78b2021-01-15 13:12:33 +01004670 info[INF_BUILD_INFO] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, haproxy_version);
Willy Tarreau909b9d82019-01-04 18:20:32 +01004671 info[INF_RELEASE_DATE] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, haproxy_date);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004672
Yves Lafon95317282018-02-26 11:10:37 +01004673 info[INF_NBTHREAD] = mkf_u32(FO_CONFIG|FS_SERVICE, global.nbthread);
Willy Tarreau91358592021-06-15 08:08:04 +02004674 info[INF_NBPROC] = mkf_u32(FO_CONFIG|FS_SERVICE, 1);
Willy Tarreaue8422bf2021-06-15 09:08:18 +02004675 info[INF_PROCESS_NUM] = mkf_u32(FO_KEY, 1);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004676 info[INF_PID] = mkf_u32(FO_STATUS, pid);
4677
4678 info[INF_UPTIME] = mkf_str(FN_DURATION, chunk_newstr(out));
Willy Tarreau563efe62023-04-27 14:41:37 +02004679 chunk_appendf(out, "%ud %uh%02um%02us", up_sec / 86400, (up_sec % 86400) / 3600, (up_sec % 3600) / 60, (up_sec % 60));
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004680
Willy Tarreau563efe62023-04-27 14:41:37 +02004681 info[INF_UPTIME_SEC] = (flags & STAT_USE_FLOAT) ? mkf_flt(FN_DURATION, up_sec + up_usec / 1000000.0) : mkf_u32(FN_DURATION, up_sec);
Willy Tarreau7222db72023-04-28 15:57:18 +02004682 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 +01004683 info[INF_MEMMAX_MB] = mkf_u32(FO_CONFIG|FN_LIMIT, global.rlimit_memmax);
William Dauchya8766cf2021-01-15 22:41:37 +01004684 info[INF_MEMMAX_BYTES] = mkf_u32(FO_CONFIG|FN_LIMIT, global.rlimit_memmax * 1048576L);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004685 info[INF_POOL_ALLOC_MB] = mkf_u32(0, (unsigned)(pool_total_allocated() / 1048576L));
Christopher Fauletc960a3b2022-12-22 11:05:48 +01004686 info[INF_POOL_ALLOC_BYTES] = mkf_u64(0, pool_total_allocated());
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004687 info[INF_POOL_USED_MB] = mkf_u32(0, (unsigned)(pool_total_used() / 1048576L));
Christopher Fauletc960a3b2022-12-22 11:05:48 +01004688 info[INF_POOL_USED_BYTES] = mkf_u64(0, pool_total_used());
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004689 info[INF_POOL_FAILED] = mkf_u32(FN_COUNTER, pool_total_failures());
4690 info[INF_ULIMIT_N] = mkf_u32(FO_CONFIG|FN_LIMIT, global.rlimit_nofile);
4691 info[INF_MAXSOCK] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxsock);
4692 info[INF_MAXCONN] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxconn);
4693 info[INF_HARD_MAXCONN] = mkf_u32(FO_CONFIG|FN_LIMIT, global.hardmaxconn);
4694 info[INF_CURR_CONN] = mkf_u32(0, actconn);
4695 info[INF_CUM_CONN] = mkf_u32(FN_COUNTER, totalconn);
4696 info[INF_CUM_REQ] = mkf_u32(FN_COUNTER, global.req_count);
4697#ifdef USE_OPENSSL
4698 info[INF_MAX_SSL_CONNS] = mkf_u32(FN_MAX, global.maxsslconn);
Willy Tarreau82531f62021-10-06 12:15:18 +02004699 info[INF_CURR_SSL_CONNS] = mkf_u32(0, global.sslconns);
4700 info[INF_CUM_SSL_CONNS] = mkf_u32(FN_COUNTER, global.totalsslconns);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004701#endif
4702 info[INF_MAXPIPES] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxpipes);
4703 info[INF_PIPES_USED] = mkf_u32(0, pipes_used);
4704 info[INF_PIPES_FREE] = mkf_u32(0, pipes_free);
Willy Tarreauc5977722021-05-08 08:14:04 +02004705 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 +01004706 info[INF_CONN_RATE_LIMIT] = mkf_u32(FO_CONFIG|FN_LIMIT, global.cps_lim);
4707 info[INF_MAX_CONN_RATE] = mkf_u32(FN_MAX, global.cps_max);
Willy Tarreauc5977722021-05-08 08:14:04 +02004708 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 +01004709 info[INF_SESS_RATE_LIMIT] = mkf_u32(FO_CONFIG|FN_LIMIT, global.sps_lim);
4710 info[INF_MAX_SESS_RATE] = mkf_u32(FN_RATE, global.sps_max);
4711
4712#ifdef USE_OPENSSL
Willy Tarreauc5977722021-05-08 08:14:04 +02004713 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 +01004714 info[INF_SSL_RATE_LIMIT] = mkf_u32(FO_CONFIG|FN_LIMIT, global.ssl_lim);
4715 info[INF_MAX_SSL_RATE] = mkf_u32(FN_MAX, global.ssl_max);
Willy Tarreauc5977722021-05-08 08:14:04 +02004716 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 +01004717 info[INF_SSL_FRONTEND_MAX_KEY_RATE] = mkf_u32(FN_MAX, global.ssl_fe_keys_max);
Willy Tarreauc5977722021-05-08 08:14:04 +02004718 info[INF_SSL_FRONTEND_SESSION_REUSE_PCT] = (flags & STAT_USE_FLOAT) ? mkf_flt(FN_RATE, ssl_reuse) : mkf_u32(0, ssl_reuse);
4719 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 +01004720 info[INF_SSL_BACKEND_MAX_KEY_RATE] = mkf_u32(FN_MAX, global.ssl_be_keys_max);
4721 info[INF_SSL_CACHE_LOOKUPS] = mkf_u32(FN_COUNTER, global.shctx_lookups);
4722 info[INF_SSL_CACHE_MISSES] = mkf_u32(FN_COUNTER, global.shctx_misses);
4723#endif
Willy Tarreauc5977722021-05-08 08:14:04 +02004724 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));
4725 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 +01004726 info[INF_COMPRESS_BPS_RATE_LIM] = mkf_u32(FO_CONFIG|FN_LIMIT, global.comp_rate_lim);
4727#ifdef USE_ZLIB
4728 info[INF_ZLIB_MEM_USAGE] = mkf_u32(0, zlib_used_memory);
4729 info[INF_MAX_ZLIB_MEM_USAGE] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxzlibmem);
4730#endif
Willy Tarreau955a11e2021-02-24 17:03:30 +01004731 info[INF_TASKS] = mkf_u32(0, total_allocated_tasks());
Willy Tarreau9c7b8082021-02-24 15:10:07 +01004732 info[INF_RUN_QUEUE] = mkf_u32(0, total_run_queues());
Willy Tarreauf9d5e102021-10-08 10:43:59 +02004733 info[INF_IDLE_PCT] = mkf_u32(FN_AVG, clock_report_idle());
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004734 info[INF_NODE] = mkf_str(FO_CONFIG|FN_OUTPUT|FS_SERVICE, global.node);
4735 if (global.desc)
4736 info[INF_DESCRIPTION] = mkf_str(FO_CONFIG|FN_OUTPUT|FS_SERVICE, global.desc);
Willy Tarreau00098ea2018-11-05 14:38:13 +01004737 info[INF_STOPPING] = mkf_u32(0, stopping);
4738 info[INF_JOBS] = mkf_u32(0, jobs);
William Lallemanda7199262018-11-16 16:57:20 +01004739 info[INF_UNSTOPPABLE_JOBS] = mkf_u32(0, unstoppable_jobs);
Willy Tarreau00098ea2018-11-05 14:38:13 +01004740 info[INF_LISTENERS] = mkf_u32(0, listeners);
Willy Tarreau199ad242018-11-05 16:31:22 +01004741 info[INF_ACTIVE_PEERS] = mkf_u32(0, active_peers);
Willy Tarreau2d372c22018-11-05 17:12:27 +01004742 info[INF_CONNECTED_PEERS] = mkf_u32(0, connected_peers);
Willy Tarreau13ef7732018-11-12 07:25:28 +01004743 info[INF_DROPPED_LOGS] = mkf_u32(0, dropped_logs);
Willy Tarreaubeb859a2018-11-22 18:07:59 +01004744 info[INF_BUSY_POLLING] = mkf_u32(0, !!(global.tune.options & GTUNE_BUSY_POLLING));
Emeric Brund30e9a12020-12-23 18:49:16 +01004745 info[INF_FAILED_RESOLUTIONS] = mkf_u32(0, resolv_failed_resolutions);
Willy Tarreau6be8d092023-01-12 16:08:41 +01004746 info[INF_TOTAL_BYTES_OUT] = mkf_u64(0, glob_out_bytes);
4747 info[INF_TOTAL_SPLICED_BYTES_OUT] = mkf_u64(0, glob_spl_bytes);
4748 info[INF_BYTES_OUT_RATE] = mkf_u64(FN_RATE, glob_out_b32);
Willy Tarreau9b013702019-10-24 18:18:02 +02004749 info[INF_DEBUG_COMMANDS_ISSUED] = mkf_u32(0, debug_commands_issued);
Emeric Brun45c457a2020-07-09 23:23:34 +02004750 info[INF_CUM_LOG_MSGS] = mkf_u32(FN_COUNTER, cum_log_messages);
Amaury Denoyelle5dfdf3e2021-05-05 17:09:12 +02004751
4752 info[INF_TAINTED] = mkf_str(FO_STATUS, chunk_newstr(out));
Willy Tarreau52fd8792023-05-17 08:43:15 +02004753 chunk_appendf(out, "%#x", get_tainted());
Willy Tarreau3c4a2972023-05-11 12:02:21 +02004754 info[INF_WARNINGS] = mkf_u32(FN_COUNTER, HA_ATOMIC_LOAD(&tot_warnings));
Willy Tarreau96151022023-05-11 13:51:31 +02004755 info[INF_MAXCONN_REACHED] = mkf_u32(FN_COUNTER, HA_ATOMIC_LOAD(&maxconn_reached));
Willy Tarreau5723b382023-05-17 09:05:20 +02004756 info[INF_BOOTTIME_MS] = mkf_u32(FN_DURATION, boot);
Amaury Denoyelle5dfdf3e2021-05-05 17:09:12 +02004757
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004758 return 1;
4759}
4760
Willy Tarreau4596fe22022-05-17 19:07:51 +02004761/* This function dumps information onto the stream connector's read buffer.
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004762 * It returns 0 as long as it does not complete, non-zero upon completion.
4763 * No state is used.
4764 */
Willy Tarreaucaff6312022-05-27 10:17:46 +02004765static int stats_dump_info_to_buffer(struct stconn *sc)
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004766{
Willy Tarreaucaff6312022-05-27 10:17:46 +02004767 struct appctx *appctx = __sc_appctx(sc);
Willy Tarreau91cefca2022-05-03 17:08:29 +02004768 struct show_stat_ctx *ctx = appctx->svcctx;
Aurelien DARRAGON55941842022-12-15 14:01:04 +01004769 int ret;
4770 int current_field;
Christopher Faulet2da02ae2022-02-24 13:45:27 +01004771
Willy Tarreau91cefca2022-05-03 17:08:29 +02004772 if (!stats_fill_info(info, INF_TOTAL_FIELDS, ctx->flags))
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004773 return 0;
4774
Christopher Fauleta8b76842022-12-16 15:08:36 +01004775 chunk_reset(&trash_chunk);
Aurelien DARRAGON55941842022-12-15 14:01:04 +01004776more:
4777 current_field = ctx->field;
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004778
Willy Tarreau91cefca2022-05-03 17:08:29 +02004779 if (ctx->flags & STAT_FMT_TYPED)
Christopher Fauleta8b76842022-12-16 15:08:36 +01004780 ret = stats_dump_typed_info_fields(&trash_chunk, info, ctx);
Willy Tarreau91cefca2022-05-03 17:08:29 +02004781 else if (ctx->flags & STAT_FMT_JSON)
Christopher Fauleta8b76842022-12-16 15:08:36 +01004782 ret = stats_dump_json_info_fields(&trash_chunk, info, ctx);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004783 else
Christopher Fauleta8b76842022-12-16 15:08:36 +01004784 ret = stats_dump_info_fields(&trash_chunk, info, ctx);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004785
Christopher Fauleta8b76842022-12-16 15:08:36 +01004786 if (applet_putchk(appctx, &trash_chunk) == -1) {
Aurelien DARRAGON55941842022-12-15 14:01:04 +01004787 /* restore previous field */
4788 ctx->field = current_field;
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004789 return 0;
Aurelien DARRAGON55941842022-12-15 14:01:04 +01004790 }
4791 if (ret && ctx->field) {
4792 /* partial dump */
4793 goto more;
4794 }
4795 ctx->field = 0;
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004796 return 1;
4797}
4798
Willy Tarreau4596fe22022-05-17 19:07:51 +02004799/* This function dumps the schema onto the stream connector's read buffer.
Simon Horman6f6bb382017-01-04 09:37:26 +01004800 * It returns 0 as long as it does not complete, non-zero upon completion.
4801 * No state is used.
4802 *
Ilya Shipitsinf38a0182020-12-21 01:16:17 +05004803 * Integer values bounded to the range [-(2**53)+1, (2**53)-1] as
Simon Horman6f6bb382017-01-04 09:37:26 +01004804 * per the recommendation for interoperable integers in section 6 of RFC 7159.
4805 */
Willy Tarreau83061a82018-07-13 11:56:34 +02004806static void stats_dump_json_schema(struct buffer *out)
Simon Horman6f6bb382017-01-04 09:37:26 +01004807{
4808
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004809 int old_len = out->data;
Simon Horman6f6bb382017-01-04 09:37:26 +01004810
4811 chunk_strcat(out,
4812 "{"
4813 "\"$schema\":\"http://json-schema.org/draft-04/schema#\","
4814 "\"oneOf\":["
4815 "{"
4816 "\"title\":\"Info\","
4817 "\"type\":\"array\","
4818 "\"items\":{"
Amaury Denoyellea53ce4c2020-10-02 18:31:59 +02004819 "\"title\":\"InfoItem\","
4820 "\"type\":\"object\","
Simon Horman6f6bb382017-01-04 09:37:26 +01004821 "\"properties\":{"
Simon Horman6f6bb382017-01-04 09:37:26 +01004822 "\"field\":{\"$ref\":\"#/definitions/field\"},"
4823 "\"processNum\":{\"$ref\":\"#/definitions/processNum\"},"
4824 "\"tags\":{\"$ref\":\"#/definitions/tags\"},"
4825 "\"value\":{\"$ref\":\"#/definitions/typedValue\"}"
4826 "},"
4827 "\"required\":[\"field\",\"processNum\",\"tags\","
4828 "\"value\"]"
4829 "}"
4830 "},"
4831 "{"
4832 "\"title\":\"Stat\","
4833 "\"type\":\"array\","
4834 "\"items\":{"
4835 "\"title\":\"InfoItem\","
4836 "\"type\":\"object\","
4837 "\"properties\":{"
4838 "\"objType\":{"
4839 "\"enum\":[\"Frontend\",\"Backend\",\"Listener\","
4840 "\"Server\",\"Unknown\"]"
4841 "},"
4842 "\"proxyId\":{"
4843 "\"type\":\"integer\","
4844 "\"minimum\":0"
4845 "},"
4846 "\"id\":{"
4847 "\"type\":\"integer\","
4848 "\"minimum\":0"
4849 "},"
4850 "\"field\":{\"$ref\":\"#/definitions/field\"},"
4851 "\"processNum\":{\"$ref\":\"#/definitions/processNum\"},"
4852 "\"tags\":{\"$ref\":\"#/definitions/tags\"},"
4853 "\"typedValue\":{\"$ref\":\"#/definitions/typedValue\"}"
4854 "},"
4855 "\"required\":[\"objType\",\"proxyId\",\"id\","
4856 "\"field\",\"processNum\",\"tags\","
4857 "\"value\"]"
4858 "}"
4859 "},"
4860 "{"
4861 "\"title\":\"Error\","
4862 "\"type\":\"object\","
4863 "\"properties\":{"
4864 "\"errorStr\":{"
4865 "\"type\":\"string\""
Amaury Denoyellea53ce4c2020-10-02 18:31:59 +02004866 "}"
4867 "},"
4868 "\"required\":[\"errorStr\"]"
Simon Horman6f6bb382017-01-04 09:37:26 +01004869 "}"
4870 "],"
4871 "\"definitions\":{"
4872 "\"field\":{"
4873 "\"type\":\"object\","
4874 "\"pos\":{"
4875 "\"type\":\"integer\","
4876 "\"minimum\":0"
4877 "},"
4878 "\"name\":{"
4879 "\"type\":\"string\""
4880 "},"
4881 "\"required\":[\"pos\",\"name\"]"
4882 "},"
4883 "\"processNum\":{"
4884 "\"type\":\"integer\","
4885 "\"minimum\":1"
4886 "},"
4887 "\"tags\":{"
4888 "\"type\":\"object\","
4889 "\"origin\":{"
4890 "\"type\":\"string\","
4891 "\"enum\":[\"Metric\",\"Status\",\"Key\","
4892 "\"Config\",\"Product\",\"Unknown\"]"
4893 "},"
4894 "\"nature\":{"
4895 "\"type\":\"string\","
4896 "\"enum\":[\"Gauge\",\"Limit\",\"Min\",\"Max\","
4897 "\"Rate\",\"Counter\",\"Duration\","
4898 "\"Age\",\"Time\",\"Name\",\"Output\","
4899 "\"Avg\", \"Unknown\"]"
4900 "},"
4901 "\"scope\":{"
4902 "\"type\":\"string\","
4903 "\"enum\":[\"Cluster\",\"Process\",\"Service\","
4904 "\"System\",\"Unknown\"]"
4905 "},"
4906 "\"required\":[\"origin\",\"nature\",\"scope\"]"
4907 "},"
4908 "\"typedValue\":{"
4909 "\"type\":\"object\","
4910 "\"oneOf\":["
4911 "{\"$ref\":\"#/definitions/typedValue/definitions/s32Value\"},"
4912 "{\"$ref\":\"#/definitions/typedValue/definitions/s64Value\"},"
4913 "{\"$ref\":\"#/definitions/typedValue/definitions/u32Value\"},"
4914 "{\"$ref\":\"#/definitions/typedValue/definitions/u64Value\"},"
4915 "{\"$ref\":\"#/definitions/typedValue/definitions/strValue\"}"
4916 "],"
4917 "\"definitions\":{"
4918 "\"s32Value\":{"
4919 "\"properties\":{"
4920 "\"type\":{"
4921 "\"type\":\"string\","
4922 "\"enum\":[\"s32\"]"
4923 "},"
4924 "\"value\":{"
4925 "\"type\":\"integer\","
4926 "\"minimum\":-2147483648,"
4927 "\"maximum\":2147483647"
4928 "}"
4929 "},"
4930 "\"required\":[\"type\",\"value\"]"
4931 "},"
4932 "\"s64Value\":{"
4933 "\"properties\":{"
4934 "\"type\":{"
4935 "\"type\":\"string\","
4936 "\"enum\":[\"s64\"]"
4937 "},"
4938 "\"value\":{"
4939 "\"type\":\"integer\","
4940 "\"minimum\":-9007199254740991,"
4941 "\"maximum\":9007199254740991"
4942 "}"
4943 "},"
4944 "\"required\":[\"type\",\"value\"]"
4945 "},"
4946 "\"u32Value\":{"
4947 "\"properties\":{"
4948 "\"type\":{"
4949 "\"type\":\"string\","
4950 "\"enum\":[\"u32\"]"
4951 "},"
4952 "\"value\":{"
4953 "\"type\":\"integer\","
4954 "\"minimum\":0,"
4955 "\"maximum\":4294967295"
4956 "}"
4957 "},"
4958 "\"required\":[\"type\",\"value\"]"
4959 "},"
4960 "\"u64Value\":{"
4961 "\"properties\":{"
4962 "\"type\":{"
4963 "\"type\":\"string\","
4964 "\"enum\":[\"u64\"]"
4965 "},"
4966 "\"value\":{"
4967 "\"type\":\"integer\","
4968 "\"minimum\":0,"
4969 "\"maximum\":9007199254740991"
4970 "}"
4971 "},"
4972 "\"required\":[\"type\",\"value\"]"
4973 "},"
4974 "\"strValue\":{"
4975 "\"properties\":{"
4976 "\"type\":{"
4977 "\"type\":\"string\","
4978 "\"enum\":[\"str\"]"
4979 "},"
4980 "\"value\":{\"type\":\"string\"}"
4981 "},"
4982 "\"required\":[\"type\",\"value\"]"
4983 "},"
4984 "\"unknownValue\":{"
4985 "\"properties\":{"
4986 "\"type\":{"
4987 "\"type\":\"integer\","
4988 "\"minimum\":0"
4989 "},"
4990 "\"value\":{"
4991 "\"type\":\"string\","
4992 "\"enum\":[\"unknown\"]"
4993 "}"
4994 "},"
4995 "\"required\":[\"type\",\"value\"]"
4996 "}"
4997 "}"
4998 "}"
4999 "}"
5000 "}");
5001
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005002 if (old_len == out->data) {
Simon Horman6f6bb382017-01-04 09:37:26 +01005003 chunk_reset(out);
5004 chunk_appendf(out,
5005 "{\"errorStr\":\"output buffer too short\"}");
5006 }
Willy Tarreau5a0e7ca2022-06-10 09:21:22 +02005007 chunk_appendf(out, "\n");
Simon Horman6f6bb382017-01-04 09:37:26 +01005008}
5009
Willy Tarreau4596fe22022-05-17 19:07:51 +02005010/* This function dumps the schema onto the stream connector's read buffer.
Simon Horman6f6bb382017-01-04 09:37:26 +01005011 * It returns 0 as long as it does not complete, non-zero upon completion.
5012 * No state is used.
5013 */
Willy Tarreaud0a06d52022-05-18 15:07:19 +02005014static int stats_dump_json_schema_to_buffer(struct appctx *appctx)
Simon Horman6f6bb382017-01-04 09:37:26 +01005015{
Simon Horman6f6bb382017-01-04 09:37:26 +01005016
Christopher Fauleta8b76842022-12-16 15:08:36 +01005017 chunk_reset(&trash_chunk);
Simon Horman6f6bb382017-01-04 09:37:26 +01005018
Christopher Fauleta8b76842022-12-16 15:08:36 +01005019 stats_dump_json_schema(&trash_chunk);
5020
5021 if (applet_putchk(appctx, &trash_chunk) == -1)
Simon Horman6f6bb382017-01-04 09:37:26 +01005022 return 0;
Simon Horman6f6bb382017-01-04 09:37:26 +01005023
5024 return 1;
5025}
5026
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02005027static int cli_parse_clear_counters(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau89d467c2016-11-23 11:02:40 +01005028{
5029 struct proxy *px;
5030 struct server *sv;
5031 struct listener *li;
Amaury Denoyelled3700a72020-10-05 11:49:43 +02005032 struct stats_module *mod;
Willy Tarreau89d467c2016-11-23 11:02:40 +01005033 int clrall = 0;
5034
5035 if (strcmp(args[2], "all") == 0)
5036 clrall = 1;
5037
5038 /* check permissions */
5039 if (!cli_has_level(appctx, ACCESS_LVL_OPER) ||
5040 (clrall && !cli_has_level(appctx, ACCESS_LVL_ADMIN)))
5041 return 1;
5042
Olivier Houchardfbc74e82017-11-24 16:54:05 +01005043 for (px = proxies_list; px; px = px->next) {
Willy Tarreau89d467c2016-11-23 11:02:40 +01005044 if (clrall) {
5045 memset(&px->be_counters, 0, sizeof(px->be_counters));
5046 memset(&px->fe_counters, 0, sizeof(px->fe_counters));
5047 }
5048 else {
5049 px->be_counters.conn_max = 0;
5050 px->be_counters.p.http.rps_max = 0;
5051 px->be_counters.sps_max = 0;
5052 px->be_counters.cps_max = 0;
5053 px->be_counters.nbpend_max = 0;
Christopher Fauletefb41f02019-11-08 14:53:15 +01005054 px->be_counters.qtime_max = 0;
5055 px->be_counters.ctime_max = 0;
5056 px->be_counters.dtime_max = 0;
5057 px->be_counters.ttime_max = 0;
Willy Tarreau89d467c2016-11-23 11:02:40 +01005058
5059 px->fe_counters.conn_max = 0;
5060 px->fe_counters.p.http.rps_max = 0;
5061 px->fe_counters.sps_max = 0;
5062 px->fe_counters.cps_max = 0;
Willy Tarreau89d467c2016-11-23 11:02:40 +01005063 }
5064
5065 for (sv = px->srv; sv; sv = sv->next)
5066 if (clrall)
5067 memset(&sv->counters, 0, sizeof(sv->counters));
5068 else {
5069 sv->counters.cur_sess_max = 0;
5070 sv->counters.nbpend_max = 0;
5071 sv->counters.sps_max = 0;
Christopher Fauletefb41f02019-11-08 14:53:15 +01005072 sv->counters.qtime_max = 0;
5073 sv->counters.ctime_max = 0;
5074 sv->counters.dtime_max = 0;
5075 sv->counters.ttime_max = 0;
Willy Tarreau89d467c2016-11-23 11:02:40 +01005076 }
5077
5078 list_for_each_entry(li, &px->conf.listeners, by_fe)
5079 if (li->counters) {
5080 if (clrall)
5081 memset(li->counters, 0, sizeof(*li->counters));
5082 else
5083 li->counters->conn_max = 0;
5084 }
5085 }
5086
5087 global.cps_max = 0;
5088 global.sps_max = 0;
Olivier Houchard00bc3cb2017-10-17 19:23:25 +02005089 global.ssl_max = 0;
5090 global.ssl_fe_keys_max = 0;
5091 global.ssl_be_keys_max = 0;
Willy Tarreaud80cb4e2018-01-20 19:30:13 +01005092
Amaury Denoyelled3700a72020-10-05 11:49:43 +02005093 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
5094 if (!mod->clearable && !clrall)
5095 continue;
5096
5097 for (px = proxies_list; px; px = px->next) {
5098 enum stats_domain_px_cap mod_cap = stats_px_get_cap(mod->domain_flags);
5099
5100 if (px->cap & PR_CAP_FE && mod_cap & STATS_PX_CAP_FE) {
5101 EXTRA_COUNTERS_INIT(px->extra_counters_fe,
5102 mod,
5103 mod->counters,
5104 mod->counters_size);
5105 }
5106
5107 if (px->cap & PR_CAP_BE && mod_cap & STATS_PX_CAP_BE) {
5108 EXTRA_COUNTERS_INIT(px->extra_counters_be,
5109 mod,
5110 mod->counters,
5111 mod->counters_size);
5112 }
5113
5114 if (mod_cap & STATS_PX_CAP_SRV) {
5115 for (sv = px->srv; sv; sv = sv->next) {
5116 EXTRA_COUNTERS_INIT(sv->extra_counters,
5117 mod,
5118 mod->counters,
5119 mod->counters_size);
5120 }
5121 }
5122
5123 if (mod_cap & STATS_PX_CAP_LI) {
5124 list_for_each_entry(li, &px->conf.listeners, by_fe) {
5125 EXTRA_COUNTERS_INIT(li->extra_counters,
5126 mod,
5127 mod->counters,
5128 mod->counters_size);
5129 }
5130 }
5131 }
5132 }
5133
Emeric Brunf8642ee2021-10-29 17:59:18 +02005134 resolv_stats_clear_counters(clrall, &stats_module_list[STATS_DOMAIN_RESOLVERS]);
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02005135
Willy Tarreaud80cb4e2018-01-20 19:30:13 +01005136 memset(activity, 0, sizeof(activity));
Willy Tarreau89d467c2016-11-23 11:02:40 +01005137 return 1;
5138}
5139
5140
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02005141static int cli_parse_show_info(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau0baac8c2016-11-22 16:36:53 +01005142{
Willy Tarreau91cefca2022-05-03 17:08:29 +02005143 struct show_stat_ctx *ctx = applet_reserve_svcctx(appctx, sizeof(*ctx));
Willy Tarreau2f397382019-10-09 11:43:59 +02005144 int arg = 2;
5145
Willy Tarreau91cefca2022-05-03 17:08:29 +02005146 ctx->scope_str = 0;
5147 ctx->scope_len = 0;
5148 ctx->flags = 0;
Aurelien DARRAGON55941842022-12-15 14:01:04 +01005149 ctx->field = 0; /* explicit default value */
Willy Tarreaud25fc792016-12-16 12:33:47 +01005150
Willy Tarreau2f397382019-10-09 11:43:59 +02005151 while (*args[arg]) {
5152 if (strcmp(args[arg], "typed") == 0)
Willy Tarreau91cefca2022-05-03 17:08:29 +02005153 ctx->flags = (ctx->flags & ~STAT_FMT_MASK) | STAT_FMT_TYPED;
Willy Tarreau2f397382019-10-09 11:43:59 +02005154 else if (strcmp(args[arg], "json") == 0)
Willy Tarreau91cefca2022-05-03 17:08:29 +02005155 ctx->flags = (ctx->flags & ~STAT_FMT_MASK) | STAT_FMT_JSON;
Willy Tarreau2f397382019-10-09 11:43:59 +02005156 else if (strcmp(args[arg], "desc") == 0)
Willy Tarreau91cefca2022-05-03 17:08:29 +02005157 ctx->flags |= STAT_SHOW_FDESC;
Willy Tarreau27456202021-05-08 07:54:24 +02005158 else if (strcmp(args[arg], "float") == 0)
Willy Tarreau91cefca2022-05-03 17:08:29 +02005159 ctx->flags |= STAT_USE_FLOAT;
Willy Tarreau2f397382019-10-09 11:43:59 +02005160 arg++;
5161 }
Willy Tarreau0baac8c2016-11-22 16:36:53 +01005162 return 0;
5163}
5164
5165
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02005166static int cli_parse_show_stat(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau2b812e22016-11-22 16:18:05 +01005167{
Willy Tarreau91cefca2022-05-03 17:08:29 +02005168 struct show_stat_ctx *ctx = applet_reserve_svcctx(appctx, sizeof(*ctx));
Willy Tarreau2f397382019-10-09 11:43:59 +02005169 int arg = 2;
5170
Willy Tarreau91cefca2022-05-03 17:08:29 +02005171 ctx->scope_str = 0;
5172 ctx->scope_len = 0;
5173 ctx->flags = STAT_SHNODE | STAT_SHDESC;
Willy Tarreau578d6e42019-10-09 11:00:22 +02005174
Willy Tarreau0698c802022-05-11 14:09:57 +02005175 if ((strm_li(appctx_strm(appctx))->bind_conf->level & ACCESS_LVL_MASK) >= ACCESS_LVL_OPER)
Willy Tarreau91cefca2022-05-03 17:08:29 +02005176 ctx->flags |= STAT_SHLGNDS;
Willy Tarreaud25fc792016-12-16 12:33:47 +01005177
Amaury Denoyelle072f97e2020-10-05 11:49:37 +02005178 /* proxy is the default domain */
Willy Tarreau91cefca2022-05-03 17:08:29 +02005179 ctx->domain = STATS_DOMAIN_PROXY;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01005180 if (strcmp(args[arg], "domain") == 0) {
Amaury Denoyelle072f97e2020-10-05 11:49:37 +02005181 ++args;
5182
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01005183 if (strcmp(args[arg], "proxy") == 0) {
Amaury Denoyelle072f97e2020-10-05 11:49:37 +02005184 ++args;
Emeric Brunf8642ee2021-10-29 17:59:18 +02005185 } else if (strcmp(args[arg], "resolvers") == 0) {
Willy Tarreau91cefca2022-05-03 17:08:29 +02005186 ctx->domain = STATS_DOMAIN_RESOLVERS;
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02005187 ++args;
5188 } else {
Amaury Denoyelle072f97e2020-10-05 11:49:37 +02005189 return cli_err(appctx, "Invalid statistics domain.\n");
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02005190 }
Amaury Denoyelle072f97e2020-10-05 11:49:37 +02005191 }
5192
Willy Tarreau91cefca2022-05-03 17:08:29 +02005193 if (ctx->domain == STATS_DOMAIN_PROXY
Amaury Denoyelle072f97e2020-10-05 11:49:37 +02005194 && *args[arg] && *args[arg+1] && *args[arg+2]) {
Willy Tarreaua1b1ed52016-11-25 08:50:58 +01005195 struct proxy *px;
5196
Willy Tarreau2f397382019-10-09 11:43:59 +02005197 px = proxy_find_by_name(args[arg], 0, 0);
Willy Tarreaua1b1ed52016-11-25 08:50:58 +01005198 if (px)
Willy Tarreau91cefca2022-05-03 17:08:29 +02005199 ctx->iid = px->uuid;
Willy Tarreaua1b1ed52016-11-25 08:50:58 +01005200 else
Willy Tarreau91cefca2022-05-03 17:08:29 +02005201 ctx->iid = atoi(args[arg]);
Willy Tarreaua1b1ed52016-11-25 08:50:58 +01005202
Willy Tarreau91cefca2022-05-03 17:08:29 +02005203 if (!ctx->iid)
Willy Tarreau9d008692019-08-09 11:21:01 +02005204 return cli_err(appctx, "No such proxy.\n");
Willy Tarreaua1b1ed52016-11-25 08:50:58 +01005205
Willy Tarreau91cefca2022-05-03 17:08:29 +02005206 ctx->flags |= STAT_BOUND;
5207 ctx->type = atoi(args[arg+1]);
5208 ctx->sid = atoi(args[arg+2]);
Willy Tarreau2f397382019-10-09 11:43:59 +02005209 arg += 3;
5210 }
5211
5212 while (*args[arg]) {
5213 if (strcmp(args[arg], "typed") == 0)
Willy Tarreau91cefca2022-05-03 17:08:29 +02005214 ctx->flags = (ctx->flags & ~STAT_FMT_MASK) | STAT_FMT_TYPED;
Willy Tarreau2f397382019-10-09 11:43:59 +02005215 else if (strcmp(args[arg], "json") == 0)
Willy Tarreau91cefca2022-05-03 17:08:29 +02005216 ctx->flags = (ctx->flags & ~STAT_FMT_MASK) | STAT_FMT_JSON;
Willy Tarreau2f397382019-10-09 11:43:59 +02005217 else if (strcmp(args[arg], "desc") == 0)
Willy Tarreau91cefca2022-05-03 17:08:29 +02005218 ctx->flags |= STAT_SHOW_FDESC;
Willy Tarreau3e320362020-10-23 17:28:57 +02005219 else if (strcmp(args[arg], "no-maint") == 0)
Willy Tarreau91cefca2022-05-03 17:08:29 +02005220 ctx->flags |= STAT_HIDE_MAINT;
Willy Tarreau65141ff2020-10-23 17:19:48 +02005221 else if (strcmp(args[arg], "up") == 0)
Willy Tarreau91cefca2022-05-03 17:08:29 +02005222 ctx->flags |= STAT_HIDE_DOWN;
Willy Tarreau2f397382019-10-09 11:43:59 +02005223 arg++;
Willy Tarreau2b812e22016-11-22 16:18:05 +01005224 }
Willy Tarreau2b812e22016-11-22 16:18:05 +01005225
Willy Tarreau2b812e22016-11-22 16:18:05 +01005226 return 0;
5227}
5228
Willy Tarreau0baac8c2016-11-22 16:36:53 +01005229static int cli_io_handler_dump_info(struct appctx *appctx)
5230{
Christopher Fauleta8b76842022-12-16 15:08:36 +01005231 trash_chunk = b_make(trash.area, trash.size, 0, 0);
Willy Tarreauc12b3212022-05-27 11:08:15 +02005232 return stats_dump_info_to_buffer(appctx_sc(appctx));
Willy Tarreau0baac8c2016-11-22 16:36:53 +01005233}
5234
Willy Tarreau4596fe22022-05-17 19:07:51 +02005235/* This I/O handler runs as an applet embedded in a stream connector. It is
Willy Tarreau2b812e22016-11-22 16:18:05 +01005236 * used to send raw stats over a socket.
5237 */
5238static int cli_io_handler_dump_stat(struct appctx *appctx)
5239{
Christopher Fauleta8b76842022-12-16 15:08:36 +01005240 trash_chunk = b_make(trash.area, trash.size, 0, 0);
Willy Tarreauc12b3212022-05-27 11:08:15 +02005241 return stats_dump_stat_to_buffer(appctx_sc(appctx), NULL, NULL);
Willy Tarreau2b812e22016-11-22 16:18:05 +01005242}
5243
Simon Horman6f6bb382017-01-04 09:37:26 +01005244static int cli_io_handler_dump_json_schema(struct appctx *appctx)
5245{
Christopher Fauleta8b76842022-12-16 15:08:36 +01005246 trash_chunk = b_make(trash.area, trash.size, 0, 0);
Willy Tarreaud0a06d52022-05-18 15:07:19 +02005247 return stats_dump_json_schema_to_buffer(appctx);
Simon Horman6f6bb382017-01-04 09:37:26 +01005248}
5249
Amaury Denoyelle216a1ce2021-03-18 15:48:14 +01005250int stats_allocate_proxy_counters_internal(struct extra_counters **counters,
5251 int type, int px_cap)
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02005252{
5253 struct stats_module *mod;
5254
5255 EXTRA_COUNTERS_REGISTER(counters, type, alloc_failed);
5256
5257 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
5258 if (!(stats_px_get_cap(mod->domain_flags) & px_cap))
5259 continue;
5260
5261 EXTRA_COUNTERS_ADD(mod, *counters, mod->counters, mod->counters_size);
5262 }
5263
5264 EXTRA_COUNTERS_ALLOC(*counters, alloc_failed);
5265
5266 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
5267 if (!(stats_px_get_cap(mod->domain_flags) & px_cap))
5268 continue;
5269
5270 EXTRA_COUNTERS_INIT(*counters, mod, mod->counters, mod->counters_size);
5271 }
5272
5273 return 1;
5274
5275 alloc_failed:
5276 return 0;
5277}
5278
5279/* Initialize and allocate all extra counters for a proxy and its attached
5280 * servers/listeners with all already registered stats module
5281 */
5282int stats_allocate_proxy_counters(struct proxy *px)
5283{
5284 struct server *sv;
5285 struct listener *li;
5286
5287 if (px->cap & PR_CAP_FE) {
5288 if (!stats_allocate_proxy_counters_internal(&px->extra_counters_fe,
5289 COUNTERS_FE,
5290 STATS_PX_CAP_FE)) {
5291 return 0;
5292 }
5293 }
5294
5295 if (px->cap & PR_CAP_BE) {
5296 if (!stats_allocate_proxy_counters_internal(&px->extra_counters_be,
5297 COUNTERS_BE,
5298 STATS_PX_CAP_BE)) {
5299 return 0;
5300 }
5301 }
5302
5303 for (sv = px->srv; sv; sv = sv->next) {
5304 if (!stats_allocate_proxy_counters_internal(&sv->extra_counters,
5305 COUNTERS_SV,
5306 STATS_PX_CAP_SRV)) {
5307 return 0;
5308 }
5309 }
5310
5311 list_for_each_entry(li, &px->conf.listeners, by_fe) {
5312 if (!stats_allocate_proxy_counters_internal(&li->extra_counters,
5313 COUNTERS_LI,
5314 STATS_PX_CAP_LI)) {
5315 return 0;
5316 }
5317 }
5318
5319 return 1;
5320}
5321
Amaury Denoyelle58d395e2020-10-05 11:49:40 +02005322void stats_register_module(struct stats_module *m)
5323{
5324 const uint8_t domain = stats_get_domain(m->domain_flags);
5325
Willy Tarreau2b718102021-04-21 07:32:39 +02005326 LIST_APPEND(&stats_module_list[domain], &m->list);
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02005327 stat_count[domain] += m->stats_count;
Amaury Denoyelle58d395e2020-10-05 11:49:40 +02005328}
5329
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02005330static int allocate_stats_px_postcheck(void)
5331{
5332 struct stats_module *mod;
5333 size_t i = ST_F_TOTAL_FIELDS;
5334 int err_code = 0;
Amaury Denoyelle27373f72020-10-05 16:57:33 +02005335 struct proxy *px;
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02005336
5337 stat_count[STATS_DOMAIN_PROXY] += ST_F_TOTAL_FIELDS;
5338
5339 stat_f[STATS_DOMAIN_PROXY] = malloc(stat_count[STATS_DOMAIN_PROXY] * sizeof(struct name_desc));
5340 if (!stat_f[STATS_DOMAIN_PROXY]) {
5341 ha_alert("stats: cannot allocate all fields for proxy statistics\n");
5342 err_code |= ERR_ALERT | ERR_FATAL;
5343 return err_code;
5344 }
5345
5346 memcpy(stat_f[STATS_DOMAIN_PROXY], stat_fields,
5347 ST_F_TOTAL_FIELDS * sizeof(struct name_desc));
5348
5349 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
5350 memcpy(stat_f[STATS_DOMAIN_PROXY] + i,
5351 mod->stats,
5352 mod->stats_count * sizeof(struct name_desc));
5353 i += mod->stats_count;
5354 }
5355
Amaury Denoyelle27373f72020-10-05 16:57:33 +02005356 for (px = proxies_list; px; px = px->next) {
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02005357 if (!stats_allocate_proxy_counters(px)) {
5358 ha_alert("stats: cannot allocate all counters for proxy statistics\n");
5359 err_code |= ERR_ALERT | ERR_FATAL;
5360 return err_code;
5361 }
5362 }
5363
Christopher Fauletde79cd22021-01-06 07:41:56 +01005364 /* wait per-thread alloc to perform corresponding stat_l allocation */
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02005365
5366 return err_code;
5367}
5368
5369REGISTER_CONFIG_POSTPARSER("allocate-stats-px", allocate_stats_px_postcheck);
5370
Emeric Brunf8642ee2021-10-29 17:59:18 +02005371static int allocate_stats_rslv_postcheck(void)
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02005372{
5373 struct stats_module *mod;
5374 size_t i = 0;
5375 int err_code = 0;
5376
Emeric Brunf8642ee2021-10-29 17:59:18 +02005377 stat_f[STATS_DOMAIN_RESOLVERS] = malloc(stat_count[STATS_DOMAIN_RESOLVERS] * sizeof(struct name_desc));
5378 if (!stat_f[STATS_DOMAIN_RESOLVERS]) {
5379 ha_alert("stats: cannot allocate all fields for resolver statistics\n");
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02005380 err_code |= ERR_ALERT | ERR_FATAL;
5381 return err_code;
5382 }
5383
Emeric Brunf8642ee2021-10-29 17:59:18 +02005384 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_RESOLVERS], list) {
5385 memcpy(stat_f[STATS_DOMAIN_RESOLVERS] + i,
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02005386 mod->stats,
5387 mod->stats_count * sizeof(struct name_desc));
5388 i += mod->stats_count;
5389 }
5390
Emeric Brunf8642ee2021-10-29 17:59:18 +02005391 if (!resolv_allocate_counters(&stats_module_list[STATS_DOMAIN_RESOLVERS])) {
5392 ha_alert("stats: cannot allocate all counters for resolver statistics\n");
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02005393 err_code |= ERR_ALERT | ERR_FATAL;
5394 return err_code;
5395 }
5396
Christopher Fauletde79cd22021-01-06 07:41:56 +01005397 /* wait per-thread alloc to perform corresponding stat_l allocation */
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02005398
5399 return err_code;
5400}
5401
Emeric Brunf8642ee2021-10-29 17:59:18 +02005402REGISTER_CONFIG_POSTPARSER("allocate-stats-resolver", allocate_stats_rslv_postcheck);
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02005403
Christopher Fauletde79cd22021-01-06 07:41:56 +01005404static int allocate_stat_lines_per_thread(void)
5405{
Emeric Brunf8642ee2021-10-29 17:59:18 +02005406 int domains[] = { STATS_DOMAIN_PROXY, STATS_DOMAIN_RESOLVERS }, i;
Christopher Fauletde79cd22021-01-06 07:41:56 +01005407
5408 for (i = 0; i < STATS_DOMAIN_COUNT; ++i) {
5409 const int domain = domains[i];
5410
5411 stat_l[domain] = malloc(stat_count[domain] * sizeof(struct field));
5412 if (!stat_l[domain])
5413 return 0;
5414 }
5415 return 1;
5416}
5417
5418REGISTER_PER_THREAD_ALLOC(allocate_stat_lines_per_thread);
5419
Amaury Denoyelle7f8f6cb2020-11-10 14:24:31 +01005420static int allocate_trash_counters(void)
5421{
5422 struct stats_module *mod;
Emeric Brunf8642ee2021-10-29 17:59:18 +02005423 int domains[] = { STATS_DOMAIN_PROXY, STATS_DOMAIN_RESOLVERS }, i;
Amaury Denoyelle7f8f6cb2020-11-10 14:24:31 +01005424 size_t max_counters_size = 0;
5425
5426 /* calculate the greatest counters used by any stats modules */
5427 for (i = 0; i < STATS_DOMAIN_COUNT; ++i) {
5428 list_for_each_entry(mod, &stats_module_list[domains[i]], list) {
5429 max_counters_size = mod->counters_size > max_counters_size ?
5430 mod->counters_size : max_counters_size;
5431 }
5432 }
5433
5434 /* allocate the trash with the size of the greatest counters */
5435 if (max_counters_size) {
5436 trash_counters = malloc(max_counters_size);
5437 if (!trash_counters) {
5438 ha_alert("stats: cannot allocate trash counters for statistics\n");
5439 return 0;
5440 }
5441 }
5442
5443 return 1;
5444}
5445
5446REGISTER_PER_THREAD_ALLOC(allocate_trash_counters);
5447
Christopher Fauletde79cd22021-01-06 07:41:56 +01005448static void deinit_stat_lines_per_thread(void)
Amaury Denoyellea2a68992020-11-10 14:24:30 +01005449{
Emeric Brunf8642ee2021-10-29 17:59:18 +02005450 int domains[] = { STATS_DOMAIN_PROXY, STATS_DOMAIN_RESOLVERS }, i;
Amaury Denoyellea2a68992020-11-10 14:24:30 +01005451
5452 for (i = 0; i < STATS_DOMAIN_COUNT; ++i) {
5453 const int domain = domains[i];
5454
Willy Tarreau61cfdf42021-02-20 10:46:51 +01005455 ha_free(&stat_l[domain]);
Christopher Fauletde79cd22021-01-06 07:41:56 +01005456 }
5457}
5458
5459
5460REGISTER_PER_THREAD_FREE(deinit_stat_lines_per_thread);
5461
5462static void deinit_stats(void)
5463{
Emeric Brunf8642ee2021-10-29 17:59:18 +02005464 int domains[] = { STATS_DOMAIN_PROXY, STATS_DOMAIN_RESOLVERS }, i;
Christopher Fauletde79cd22021-01-06 07:41:56 +01005465
5466 for (i = 0; i < STATS_DOMAIN_COUNT; ++i) {
5467 const int domain = domains[i];
Amaury Denoyellea2a68992020-11-10 14:24:30 +01005468
5469 if (stat_f[domain])
5470 free(stat_f[domain]);
5471 }
5472}
5473
5474REGISTER_POST_DEINIT(deinit_stats);
5475
Amaury Denoyelle7f8f6cb2020-11-10 14:24:31 +01005476static void free_trash_counters(void)
5477{
5478 if (trash_counters)
5479 free(trash_counters);
5480}
5481
5482REGISTER_PER_THREAD_FREE(free_trash_counters);
5483
Willy Tarreau2b812e22016-11-22 16:18:05 +01005484/* register cli keywords */
5485static struct cli_kw_list cli_kws = {{ },{
Willy Tarreaub205bfd2021-05-07 11:38:37 +02005486 { { "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 +02005487 { { "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 +02005488 { { "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 },
5489 { { "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 +01005490 {{},}
5491}};
5492
Willy Tarreau0108d902018-11-25 19:14:37 +01005493INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
5494
William Lallemand74c24fb2016-11-21 17:18:36 +01005495struct applet http_stats_applet = {
5496 .obj_type = OBJ_TYPE_APPLET,
5497 .name = "<STATS>", /* used for logging */
5498 .fct = http_stats_io_handler,
5499 .release = NULL,
5500};
5501
William Lallemand74c24fb2016-11-21 17:18:36 +01005502/*
5503 * Local variables:
5504 * c-indent-level: 8
5505 * c-basic-offset: 8
5506 * End:
5507 */