blob: 5fd4d3f969a0294ed3eba36a099d18b5067fd121 [file] [log] [blame]
William Lallemand74c24fb2016-11-21 17:18:36 +01001/*
2 * Functions dedicated to statistics output and the stats socket
3 *
4 * Copyright 2000-2012 Willy Tarreau <w@1wt.eu>
5 * Copyright 2007-2009 Krzysztof Piotr Oledzki <ole@ans.pl>
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version
10 * 2 of the License, or (at your option) any later version.
11 *
12 */
13
14#include <ctype.h>
15#include <errno.h>
William Lallemand74c24fb2016-11-21 17:18:36 +010016#include <stdio.h>
17#include <stdlib.h>
18#include <string.h>
19#include <pwd.h>
20#include <grp.h>
21
22#include <sys/socket.h>
23#include <sys/stat.h>
24#include <sys/types.h>
25
Willy Tarreaub2551052020-06-09 09:07:15 +020026#include <haproxy/api.h>
Willy Tarreau5d9ddc52021-10-06 19:54:09 +020027#include <haproxy/activity.h>
Willy Tarreau91cefca2022-05-03 17:08:29 +020028#include <haproxy/applet.h>
Willy Tarreau49801602020-06-04 22:50:02 +020029#include <haproxy/backend.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020030#include <haproxy/base64.h>
Willy Tarreau6be78492020-06-05 00:00:29 +020031#include <haproxy/cfgparse.h>
Willy Tarreauf1d32c42020-06-04 21:07:02 +020032#include <haproxy/channel.h>
Willy Tarreau4aa573d2020-06-04 18:21:56 +020033#include <haproxy/check.h>
Willy Tarreau83487a82020-06-04 20:19:54 +020034#include <haproxy/cli.h>
Willy Tarreau55542642021-10-08 09:33:24 +020035#include <haproxy/clock.h>
Willy Tarreau0a3bd392020-06-04 08:52:38 +020036#include <haproxy/compression.h>
Willy Tarreau2a83d602020-05-27 16:58:08 +020037#include <haproxy/debug.h>
Willy Tarreau36979d92020-06-05 17:27:29 +020038#include <haproxy/errors.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020039#include <haproxy/fd.h>
40#include <haproxy/freq_ctr.h>
Willy Tarreau762d7a52020-06-04 11:23:07 +020041#include <haproxy/frontend.h>
Willy Tarreauf268ee82020-06-04 17:05:57 +020042#include <haproxy/global.h>
Willy Tarreaucd72d8c2020-06-02 19:11:26 +020043#include <haproxy/http.h>
Willy Tarreaub7fc4c42021-10-06 18:56:42 +020044#include <haproxy/http_ana.h>
Willy Tarreau87735332020-06-04 09:08:41 +020045#include <haproxy/http_htx.h>
Willy Tarreau16f958c2020-06-03 08:44:35 +020046#include <haproxy/htx.h>
Willy Tarreau853b2972020-05-27 18:01:47 +020047#include <haproxy/list.h>
Willy Tarreau36979d92020-06-05 17:27:29 +020048#include <haproxy/listener.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020049#include <haproxy/log.h>
Willy Tarreau2cd58092020-06-04 15:10:43 +020050#include <haproxy/map-t.h>
Willy Tarreau225a90a2020-06-04 15:06:28 +020051#include <haproxy/pattern-t.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020052#include <haproxy/pipe.h>
53#include <haproxy/pool.h>
Willy Tarreaua264d962020-06-04 22:29:18 +020054#include <haproxy/proxy.h>
Emeric Brunc9437992021-02-12 19:42:55 +010055#include <haproxy/resolvers.h>
Willy Tarreau5edca2f2022-05-27 09:25:10 +020056#include <haproxy/sc_strm.h>
Willy Tarreau1e56f922020-06-04 23:20:13 +020057#include <haproxy/server.h>
Willy Tarreau48d25b32020-06-04 18:58:52 +020058#include <haproxy/session.h>
Willy Tarreau2eec9b52020-06-04 19:58:55 +020059#include <haproxy/stats.h>
Willy Tarreaucb086c62022-05-27 09:47:12 +020060#include <haproxy/stconn.h>
Willy Tarreaudfd3de82020-06-04 23:46:14 +020061#include <haproxy/stream.h>
Willy Tarreaucea0e1b2020-06-04 17:25:40 +020062#include <haproxy/task.h>
Willy Tarreauc2f7c582020-06-02 18:15:32 +020063#include <haproxy/ticks.h>
Willy Tarreau92b4f132020-06-01 11:05:15 +020064#include <haproxy/time.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020065#include <haproxy/tools.h>
Willy Tarreau8c42b8a2020-06-04 19:27:34 +020066#include <haproxy/uri_auth-t.h>
Willy Tarreaud6788052020-05-27 15:59:00 +020067#include <haproxy/version.h>
William Lallemand74c24fb2016-11-21 17:18:36 +010068
William Lallemand74c24fb2016-11-21 17:18:36 +010069
Christopher Faulet2b9b6782019-02-27 16:42:58 +010070/* status codes available for the stats admin page (strictly 4 chars length) */
71const char *stat_status_codes[STAT_STATUS_SIZE] = {
72 [STAT_STATUS_DENY] = "DENY",
73 [STAT_STATUS_DONE] = "DONE",
74 [STAT_STATUS_ERRP] = "ERRP",
75 [STAT_STATUS_EXCD] = "EXCD",
76 [STAT_STATUS_NONE] = "NONE",
77 [STAT_STATUS_PART] = "PART",
78 [STAT_STATUS_UNKN] = "UNKN",
79 [STAT_STATUS_IVAL] = "IVAL",
80};
81
Willy Tarreau0baac8c2016-11-22 16:36:53 +010082/* These are the field names for each INF_* field position. Please pay attention
83 * to always use the exact same name except that the strings for new names must
84 * be lower case or CamelCase while the enum entries must be upper case.
85 */
Willy Tarreaueaa55372019-10-09 07:39:11 +020086const struct name_desc info_fields[INF_TOTAL_FIELDS] = {
Willy Tarreau6d4897e2019-10-11 16:31:46 +020087 [INF_NAME] = { .name = "Name", .desc = "Product name" },
88 [INF_VERSION] = { .name = "Version", .desc = "Product version" },
89 [INF_RELEASE_DATE] = { .name = "Release_date", .desc = "Date of latest source code update" },
90 [INF_NBTHREAD] = { .name = "Nbthread", .desc = "Number of started threads (global.nbthread)" },
Willy Tarreau91358592021-06-15 08:08:04 +020091 [INF_NBPROC] = { .name = "Nbproc", .desc = "Number of started worker processes (historical, always 1)" },
Willy Tarreaue8422bf2021-06-15 09:08:18 +020092 [INF_PROCESS_NUM] = { .name = "Process_num", .desc = "Relative worker process number (1)" },
Willy Tarreau6d4897e2019-10-11 16:31:46 +020093 [INF_PID] = { .name = "Pid", .desc = "This worker process identifier for the system" },
94 [INF_UPTIME] = { .name = "Uptime", .desc = "How long ago this worker process was started (days+hours+minutes+seconds)" },
95 [INF_UPTIME_SEC] = { .name = "Uptime_sec", .desc = "How long ago this worker process was started (seconds)" },
William Dauchydefd1562021-01-15 22:41:38 +010096 [INF_START_TIME_SEC] = { .name = "Start_time_sec", .desc = "Start time in seconds" },
Willy Tarreau6d4897e2019-10-11 16:31:46 +020097 [INF_MEMMAX_MB] = { .name = "Memmax_MB", .desc = "Worker process's hard limit on memory usage in MB (-m on command line)" },
William Dauchya8766cf2021-01-15 22:41:37 +010098 [INF_MEMMAX_BYTES] = { .name = "Memmax_bytes", .desc = "Worker process's hard limit on memory usage in byes (-m on command line)" },
Willy Tarreau6d4897e2019-10-11 16:31:46 +020099 [INF_POOL_ALLOC_MB] = { .name = "PoolAlloc_MB", .desc = "Amount of memory allocated in pools (in MB)" },
William Dauchya8766cf2021-01-15 22:41:37 +0100100 [INF_POOL_ALLOC_BYTES] = { .name = "PoolAlloc_bytes", .desc = "Amount of memory allocated in pools (in bytes)" },
Willy Tarreau6d4897e2019-10-11 16:31:46 +0200101 [INF_POOL_USED_MB] = { .name = "PoolUsed_MB", .desc = "Amount of pool memory currently used (in MB)" },
William Dauchya8766cf2021-01-15 22:41:37 +0100102 [INF_POOL_USED_BYTES] = { .name = "PoolUsed_bytes", .desc = "Amount of pool memory currently used (in bytes)" },
Willy Tarreau6d4897e2019-10-11 16:31:46 +0200103 [INF_POOL_FAILED] = { .name = "PoolFailed", .desc = "Number of failed pool allocations since this worker was started" },
104 [INF_ULIMIT_N] = { .name = "Ulimit-n", .desc = "Hard limit on the number of per-process file descriptors" },
105 [INF_MAXSOCK] = { .name = "Maxsock", .desc = "Hard limit on the number of per-process sockets" },
106 [INF_MAXCONN] = { .name = "Maxconn", .desc = "Hard limit on the number of per-process connections (configured or imposed by Ulimit-n)" },
107 [INF_HARD_MAXCONN] = { .name = "Hard_maxconn", .desc = "Hard limit on the number of per-process connections (imposed by Memmax_MB or Ulimit-n)" },
108 [INF_CURR_CONN] = { .name = "CurrConns", .desc = "Current number of connections on this worker process" },
109 [INF_CUM_CONN] = { .name = "CumConns", .desc = "Total number of connections on this worker process since started" },
110 [INF_CUM_REQ] = { .name = "CumReq", .desc = "Total number of requests on this worker process since started" },
111 [INF_MAX_SSL_CONNS] = { .name = "MaxSslConns", .desc = "Hard limit on the number of per-process SSL endpoints (front+back), 0=unlimited" },
112 [INF_CURR_SSL_CONNS] = { .name = "CurrSslConns", .desc = "Current number of SSL endpoints on this worker process (front+back)" },
113 [INF_CUM_SSL_CONNS] = { .name = "CumSslConns", .desc = "Total number of SSL endpoints on this worker process since started (front+back)" },
114 [INF_MAXPIPES] = { .name = "Maxpipes", .desc = "Hard limit on the number of pipes for splicing, 0=unlimited" },
115 [INF_PIPES_USED] = { .name = "PipesUsed", .desc = "Current number of pipes in use in this worker process" },
116 [INF_PIPES_FREE] = { .name = "PipesFree", .desc = "Current number of allocated and available pipes in this worker process" },
117 [INF_CONN_RATE] = { .name = "ConnRate", .desc = "Number of front connections created on this worker process over the last second" },
118 [INF_CONN_RATE_LIMIT] = { .name = "ConnRateLimit", .desc = "Hard limit for ConnRate (global.maxconnrate)" },
119 [INF_MAX_CONN_RATE] = { .name = "MaxConnRate", .desc = "Highest ConnRate reached on this worker process since started (in connections per second)" },
120 [INF_SESS_RATE] = { .name = "SessRate", .desc = "Number of sessions created on this worker process over the last second" },
121 [INF_SESS_RATE_LIMIT] = { .name = "SessRateLimit", .desc = "Hard limit for SessRate (global.maxsessrate)" },
122 [INF_MAX_SESS_RATE] = { .name = "MaxSessRate", .desc = "Highest SessRate reached on this worker process since started (in sessions per second)" },
123 [INF_SSL_RATE] = { .name = "SslRate", .desc = "Number of SSL connections created on this worker process over the last second" },
124 [INF_SSL_RATE_LIMIT] = { .name = "SslRateLimit", .desc = "Hard limit for SslRate (global.maxsslrate)" },
125 [INF_MAX_SSL_RATE] = { .name = "MaxSslRate", .desc = "Highest SslRate reached on this worker process since started (in connections per second)" },
126 [INF_SSL_FRONTEND_KEY_RATE] = { .name = "SslFrontendKeyRate", .desc = "Number of SSL keys created on frontends in this worker process over the last second" },
127 [INF_SSL_FRONTEND_MAX_KEY_RATE] = { .name = "SslFrontendMaxKeyRate", .desc = "Highest SslFrontendKeyRate reached on this worker process since started (in SSL keys per second)" },
128 [INF_SSL_FRONTEND_SESSION_REUSE_PCT] = { .name = "SslFrontendSessionReuse_pct", .desc = "Percent of frontend SSL connections which did not require a new key" },
129 [INF_SSL_BACKEND_KEY_RATE] = { .name = "SslBackendKeyRate", .desc = "Number of SSL keys created on backends in this worker process over the last second" },
130 [INF_SSL_BACKEND_MAX_KEY_RATE] = { .name = "SslBackendMaxKeyRate", .desc = "Highest SslBackendKeyRate reached on this worker process since started (in SSL keys per second)" },
131 [INF_SSL_CACHE_LOOKUPS] = { .name = "SslCacheLookups", .desc = "Total number of SSL session ID lookups in the SSL session cache on this worker since started" },
132 [INF_SSL_CACHE_MISSES] = { .name = "SslCacheMisses", .desc = "Total number of SSL session ID lookups that didn't find a session in the SSL session cache on this worker since started" },
133 [INF_COMPRESS_BPS_IN] = { .name = "CompressBpsIn", .desc = "Number of bytes submitted to the HTTP compressor in this worker process over the last second" },
134 [INF_COMPRESS_BPS_OUT] = { .name = "CompressBpsOut", .desc = "Number of bytes emitted by the HTTP compressor in this worker process over the last second" },
135 [INF_COMPRESS_BPS_RATE_LIM] = { .name = "CompressBpsRateLim", .desc = "Limit of CompressBpsOut beyond which HTTP compression is automatically disabled" },
136 [INF_ZLIB_MEM_USAGE] = { .name = "ZlibMemUsage", .desc = "Amount of memory currently used by HTTP compression on the current worker process (in bytes)" },
137 [INF_MAX_ZLIB_MEM_USAGE] = { .name = "MaxZlibMemUsage", .desc = "Limit on the amount of memory used by HTTP compression above which it is automatically disabled (in bytes, see global.maxzlibmem)" },
138 [INF_TASKS] = { .name = "Tasks", .desc = "Total number of tasks in the current worker process (active + sleeping)" },
139 [INF_RUN_QUEUE] = { .name = "Run_queue", .desc = "Total number of active tasks+tasklets in the current worker process" },
140 [INF_IDLE_PCT] = { .name = "Idle_pct", .desc = "Percentage of last second spent waiting in the current worker thread" },
141 [INF_NODE] = { .name = "node", .desc = "Node name (global.node)" },
142 [INF_DESCRIPTION] = { .name = "description", .desc = "Node description (global.description)" },
143 [INF_STOPPING] = { .name = "Stopping", .desc = "1 if the worker process is currently stopping, otherwise zero" },
144 [INF_JOBS] = { .name = "Jobs", .desc = "Current number of active jobs on the current worker process (frontend connections, master connections, listeners)" },
145 [INF_UNSTOPPABLE_JOBS] = { .name = "Unstoppable Jobs", .desc = "Current number of unstoppable jobs on the current worker process (master connections)" },
146 [INF_LISTENERS] = { .name = "Listeners", .desc = "Current number of active listeners on the current worker process" },
147 [INF_ACTIVE_PEERS] = { .name = "ActivePeers", .desc = "Current number of verified active peers connections on the current worker process" },
148 [INF_CONNECTED_PEERS] = { .name = "ConnectedPeers", .desc = "Current number of peers having passed the connection step on the current worker process" },
149 [INF_DROPPED_LOGS] = { .name = "DroppedLogs", .desc = "Total number of dropped logs for current worker process since started" },
150 [INF_BUSY_POLLING] = { .name = "BusyPolling", .desc = "1 if busy-polling is currently in use on the worker process, otherwise zero (config.busy-polling)" },
151 [INF_FAILED_RESOLUTIONS] = { .name = "FailedResolutions", .desc = "Total number of failed DNS resolutions in current worker process since started" },
152 [INF_TOTAL_BYTES_OUT] = { .name = "TotalBytesOut", .desc = "Total number of bytes emitted by current worker process since started" },
Christopher Fauletaaa70852020-07-10 13:56:30 +0200153 [INF_TOTAL_SPLICED_BYTES_OUT] = { .name = "TotalSplicdedBytesOut", .desc = "Total number of bytes emitted by current worker process through a kernel pipe since started" },
Willy Tarreau6d4897e2019-10-11 16:31:46 +0200154 [INF_BYTES_OUT_RATE] = { .name = "BytesOutRate", .desc = "Number of bytes emitted by current worker process over the last second" },
Willy Tarreau9b013702019-10-24 18:18:02 +0200155 [INF_DEBUG_COMMANDS_ISSUED] = { .name = "DebugCommandsIssued", .desc = "Number of debug commands issued on this process (anything > 0 is unsafe)" },
Emeric Brun45c457a2020-07-09 23:23:34 +0200156 [INF_CUM_LOG_MSGS] = { .name = "CumRecvLogs", .desc = "Total number of log messages received by log-forwarding listeners on this worker process since started" },
Adis Nezirovicb62b78b2021-01-15 13:12:33 +0100157 [INF_BUILD_INFO] = { .name = "Build info", .desc = "Build info" },
Amaury Denoyelle5dfdf3e2021-05-05 17:09:12 +0200158 [INF_TAINTED] = { .name = "Tainted", .desc = "Experimental features used" },
Willy Tarreau0baac8c2016-11-22 16:36:53 +0100159};
160
Willy Tarreaueaa55372019-10-09 07:39:11 +0200161const struct name_desc stat_fields[ST_F_TOTAL_FIELDS] = {
Willy Tarreau6d4897e2019-10-11 16:31:46 +0200162 [ST_F_PXNAME] = { .name = "pxname", .desc = "Proxy name" },
163 [ST_F_SVNAME] = { .name = "svname", .desc = "Server name" },
William Dauchyeedb9b12021-02-01 13:11:53 +0100164 [ST_F_QCUR] = { .name = "qcur", .desc = "Number of current queued connections" },
165 [ST_F_QMAX] = { .name = "qmax", .desc = "Highest value of queued connections encountered since process started" },
William Dauchy19f7cfc2021-02-01 13:11:54 +0100166 [ST_F_SCUR] = { .name = "scur", .desc = "Number of current sessions on the frontend, backend or server" },
167 [ST_F_SMAX] = { .name = "smax", .desc = "Highest value of current sessions encountered since process started" },
Willy Tarreau6d4897e2019-10-11 16:31:46 +0200168 [ST_F_SLIM] = { .name = "slim", .desc = "Frontend/listener/server's maxconn, backend's fullconn" },
169 [ST_F_STOT] = { .name = "stot", .desc = "Total number of sessions since process started" },
170 [ST_F_BIN] = { .name = "bin", .desc = "Total number of request bytes since process started" },
171 [ST_F_BOUT] = { .name = "bout", .desc = "Total number of response bytes since process started" },
172 [ST_F_DREQ] = { .name = "dreq", .desc = "Total number of denied requests since process started" },
173 [ST_F_DRESP] = { .name = "dresp", .desc = "Total number of denied responses since process started" },
174 [ST_F_EREQ] = { .name = "ereq", .desc = "Total number of invalid requests since process started" },
175 [ST_F_ECON] = { .name = "econ", .desc = "Total number of failed connections to server since the worker process started" },
176 [ST_F_ERESP] = { .name = "eresp", .desc = "Total number of invalid responses since the worker process started" },
177 [ST_F_WRETR] = { .name = "wretr", .desc = "Total number of server connection retries since the worker process started" },
178 [ST_F_WREDIS] = { .name = "wredis", .desc = "Total number of server redispatches due to connection failures since the worker process started" },
179 [ST_F_STATUS] = { .name = "status", .desc = "Frontend/listen status: OPEN/WAITING/FULL/STOP; backend: UP/DOWN; server: last check status" },
Willy Tarreaubd715102020-10-23 22:44:30 +0200180 [ST_F_WEIGHT] = { .name = "weight", .desc = "Server's effective weight, or sum of active servers' effective weights for a backend" },
Willy Tarreau6d4897e2019-10-11 16:31:46 +0200181 [ST_F_ACT] = { .name = "act", .desc = "Total number of active UP servers with a non-zero weight" },
182 [ST_F_BCK] = { .name = "bck", .desc = "Total number of backup UP servers with a non-zero weight" },
183 [ST_F_CHKFAIL] = { .name = "chkfail", .desc = "Total number of failed individual health checks per server/backend, since the worker process started" },
184 [ST_F_CHKDOWN] = { .name = "chkdown", .desc = "Total number of failed checks causing UP to DOWN server transitions, per server/backend, since the worker process started" },
185 [ST_F_LASTCHG] = { .name = "lastchg", .desc = "How long ago the last server state changed, in seconds" },
186 [ST_F_DOWNTIME] = { .name = "downtime", .desc = "Total time spent in DOWN state, for server or backend" },
187 [ST_F_QLIMIT] = { .name = "qlimit", .desc = "Limit on the number of connections in queue, for servers only (maxqueue argument)" },
Willy Tarreaue8422bf2021-06-15 09:08:18 +0200188 [ST_F_PID] = { .name = "pid", .desc = "Relative worker process number (1)" },
Willy Tarreau6d4897e2019-10-11 16:31:46 +0200189 [ST_F_IID] = { .name = "iid", .desc = "Frontend or Backend numeric identifier ('id' setting)" },
190 [ST_F_SID] = { .name = "sid", .desc = "Server numeric identifier ('id' setting)" },
191 [ST_F_THROTTLE] = { .name = "throttle", .desc = "Throttling ratio applied to a server's maxconn and weight during the slowstart period (0 to 100%)" },
192 [ST_F_LBTOT] = { .name = "lbtot", .desc = "Total number of requests routed by load balancing since the worker process started (ignores queue pop and stickiness)" },
193 [ST_F_TRACKED] = { .name = "tracked", .desc = "Name of the other server this server tracks for its state" },
194 [ST_F_TYPE] = { .name = "type", .desc = "Type of the object (Listener, Frontend, Backend, Server)" },
195 [ST_F_RATE] = { .name = "rate", .desc = "Total number of sessions processed by this object over the last second (sessions for listeners/frontends, requests for backends/servers)" },
196 [ST_F_RATE_LIM] = { .name = "rate_lim", .desc = "Limit on the number of sessions accepted in a second (frontend only, 'rate-limit sessions' setting)" },
William Dauchy19f7cfc2021-02-01 13:11:54 +0100197 [ST_F_RATE_MAX] = { .name = "rate_max", .desc = "Highest value of sessions per second observed since the worker process started" },
Willy Tarreau6d4897e2019-10-11 16:31:46 +0200198 [ST_F_CHECK_STATUS] = { .name = "check_status", .desc = "Status report of the server's latest health check, prefixed with '*' if a check is currently in progress" },
199 [ST_F_CHECK_CODE] = { .name = "check_code", .desc = "HTTP/SMTP/LDAP status code reported by the latest server health check" },
200 [ST_F_CHECK_DURATION] = { .name = "check_duration", .desc = "Total duration of the latest server health check, in milliseconds" },
201 [ST_F_HRSP_1XX] = { .name = "hrsp_1xx", .desc = "Total number of HTTP responses with status 100-199 returned by this object since the worker process started" },
202 [ST_F_HRSP_2XX] = { .name = "hrsp_2xx", .desc = "Total number of HTTP responses with status 200-299 returned by this object since the worker process started" },
203 [ST_F_HRSP_3XX] = { .name = "hrsp_3xx", .desc = "Total number of HTTP responses with status 300-399 returned by this object since the worker process started" },
204 [ST_F_HRSP_4XX] = { .name = "hrsp_4xx", .desc = "Total number of HTTP responses with status 400-499 returned by this object since the worker process started" },
205 [ST_F_HRSP_5XX] = { .name = "hrsp_5xx", .desc = "Total number of HTTP responses with status 500-599 returned by this object since the worker process started" },
206 [ST_F_HRSP_OTHER] = { .name = "hrsp_other", .desc = "Total number of HTTP responses with status <100, >599 returned by this object since the worker process started (error -1 included)" },
207 [ST_F_HANAFAIL] = { .name = "hanafail", .desc = "Total number of failed checks caused by an 'on-error' directive after an 'observe' condition matched" },
208 [ST_F_REQ_RATE] = { .name = "req_rate", .desc = "Number of HTTP requests processed over the last second on this object" },
William Dauchy19f7cfc2021-02-01 13:11:54 +0100209 [ST_F_REQ_RATE_MAX] = { .name = "req_rate_max", .desc = "Highest value of http requests observed since the worker process started" },
Willy Tarreau6d4897e2019-10-11 16:31:46 +0200210 [ST_F_REQ_TOT] = { .name = "req_tot", .desc = "Total number of HTTP requests processed by this object since the worker process started" },
211 [ST_F_CLI_ABRT] = { .name = "cli_abrt", .desc = "Total number of requests or connections aborted by the client since the worker process started" },
212 [ST_F_SRV_ABRT] = { .name = "srv_abrt", .desc = "Total number of requests or connections aborted by the server since the worker process started" },
213 [ST_F_COMP_IN] = { .name = "comp_in", .desc = "Total number of bytes submitted to the HTTP compressor for this object since the worker process started" },
214 [ST_F_COMP_OUT] = { .name = "comp_out", .desc = "Total number of bytes emitted by the HTTP compressor for this object since the worker process started" },
215 [ST_F_COMP_BYP] = { .name = "comp_byp", .desc = "Total number of bytes that bypassed HTTP compression for this object since the worker process started (CPU/memory/bandwidth limitation)" },
216 [ST_F_COMP_RSP] = { .name = "comp_rsp", .desc = "Total number of HTTP responses that were compressed for this object since the worker process started" },
217 [ST_F_LASTSESS] = { .name = "lastsess", .desc = "How long ago some traffic was seen on this object on this worker process, in seconds" },
218 [ST_F_LAST_CHK] = { .name = "last_chk", .desc = "Short description of the latest health check report for this server (see also check_desc)" },
219 [ST_F_LAST_AGT] = { .name = "last_agt", .desc = "Short description of the latest agent check report for this server (see also agent_desc)" },
220 [ST_F_QTIME] = { .name = "qtime", .desc = "Time spent in the queue, in milliseconds, averaged over the 1024 last requests (backend/server)" },
221 [ST_F_CTIME] = { .name = "ctime", .desc = "Time spent waiting for a connection to complete, in milliseconds, averaged over the 1024 last requests (backend/server)" },
222 [ST_F_RTIME] = { .name = "rtime", .desc = "Time spent waiting for a server response, in milliseconds, averaged over the 1024 last requests (backend/server)" },
223 [ST_F_TTIME] = { .name = "ttime", .desc = "Total request+response time (request+queue+connect+response+processing), in milliseconds, averaged over the 1024 last requests (backend/server)" },
224 [ST_F_AGENT_STATUS] = { .name = "agent_status", .desc = "Status report of the server's latest agent check, prefixed with '*' if a check is currently in progress" },
225 [ST_F_AGENT_CODE] = { .name = "agent_code", .desc = "Status code reported by the latest server agent check" },
226 [ST_F_AGENT_DURATION] = { .name = "agent_duration", .desc = "Total duration of the latest server agent check, in milliseconds" },
227 [ST_F_CHECK_DESC] = { .name = "check_desc", .desc = "Textual description of the latest health check report for this server" },
228 [ST_F_AGENT_DESC] = { .name = "agent_desc", .desc = "Textual description of the latest agent check report for this server" },
229 [ST_F_CHECK_RISE] = { .name = "check_rise", .desc = "Number of successful health checks before declaring a server UP (server 'rise' setting)" },
230 [ST_F_CHECK_FALL] = { .name = "check_fall", .desc = "Number of failed health checks before declaring a server DOWN (server 'fall' setting)" },
231 [ST_F_CHECK_HEALTH] = { .name = "check_health", .desc = "Current server health check level (0..fall-1=DOWN, fall..rise-1=UP)" },
232 [ST_F_AGENT_RISE] = { .name = "agent_rise", .desc = "Number of successful agent checks before declaring a server UP (server 'rise' setting)" },
233 [ST_F_AGENT_FALL] = { .name = "agent_fall", .desc = "Number of failed agent checks before declaring a server DOWN (server 'fall' setting)" },
234 [ST_F_AGENT_HEALTH] = { .name = "agent_health", .desc = "Current server agent check level (0..fall-1=DOWN, fall..rise-1=UP)" },
235 [ST_F_ADDR] = { .name = "addr", .desc = "Server's address:port, shown only if show-legends is set, or at levels oper/admin for the CLI" },
236 [ST_F_COOKIE] = { .name = "cookie", .desc = "Backend's cookie name or Server's cookie value, shown only if show-legends is set, or at levels oper/admin for the CLI" },
237 [ST_F_MODE] = { .name = "mode", .desc = "'mode' setting (tcp/http/health/cli)" },
238 [ST_F_ALGO] = { .name = "algo", .desc = "Backend's load balancing algorithm, shown only if show-legends is set, or at levels oper/admin for the CLI" },
239 [ST_F_CONN_RATE] = { .name = "conn_rate", .desc = "Number of new connections accepted over the last second on the frontend for this worker process" },
William Dauchy19f7cfc2021-02-01 13:11:54 +0100240 [ST_F_CONN_RATE_MAX] = { .name = "conn_rate_max", .desc = "Highest value of connections per second observed since the worker process started" },
Willy Tarreau6d4897e2019-10-11 16:31:46 +0200241 [ST_F_CONN_TOT] = { .name = "conn_tot", .desc = "Total number of new connections accepted on this frontend since the worker process started" },
242 [ST_F_INTERCEPTED] = { .name = "intercepted", .desc = "Total number of HTTP requests intercepted on the frontend (redirects/stats/services) since the worker process started" },
243 [ST_F_DCON] = { .name = "dcon", .desc = "Total number of incoming connections blocked on a listener/frontend by a tcp-request connection rule since the worker process started" },
244 [ST_F_DSES] = { .name = "dses", .desc = "Total number of incoming sessions blocked on a listener/frontend by a tcp-request connection rule since the worker process started" },
245 [ST_F_WREW] = { .name = "wrew", .desc = "Total number of failed HTTP header rewrites since the worker process started" },
246 [ST_F_CONNECT] = { .name = "connect", .desc = "Total number of outgoing connection attempts on this backend/server since the worker process started" },
247 [ST_F_REUSE] = { .name = "reuse", .desc = "Total number of reused connection on this backend/server since the worker process started" },
248 [ST_F_CACHE_LOOKUPS] = { .name = "cache_lookups", .desc = "Total number of HTTP requests looked up in the cache on this frontend/backend since the worker process started" },
249 [ST_F_CACHE_HITS] = { .name = "cache_hits", .desc = "Total number of HTTP requests not found in the cache on this frontend/backend since the worker process started" },
250 [ST_F_SRV_ICUR] = { .name = "srv_icur", .desc = "Current number of idle connections available for reuse on this server" },
251 [ST_F_SRV_ILIM] = { .name = "src_ilim", .desc = "Limit on the number of available idle connections on this server (server 'pool_max_conn' directive)" },
Christopher Faulet0d1c2a62019-11-08 14:59:51 +0100252 [ST_F_QT_MAX] = { .name = "qtime_max", .desc = "Maximum observed time spent in the queue, in milliseconds (backend/server)" },
253 [ST_F_CT_MAX] = { .name = "ctime_max", .desc = "Maximum observed time spent waiting for a connection to complete, in milliseconds (backend/server)" },
254 [ST_F_RT_MAX] = { .name = "rtime_max", .desc = "Maximum observed time spent waiting for a server response, in milliseconds (backend/server)" },
255 [ST_F_TT_MAX] = { .name = "ttime_max", .desc = "Maximum observed total request+response time (request+queue+connect+response+processing), in milliseconds (backend/server)" },
Christopher Faulet0159ee42019-12-16 14:40:39 +0100256 [ST_F_EINT] = { .name = "eint", .desc = "Total number of internal errors since process started"},
Willy Tarreau3bb617c2020-06-29 13:51:05 +0200257 [ST_F_IDLE_CONN_CUR] = { .name = "idle_conn_cur", .desc = "Current number of unsafe idle connections"},
258 [ST_F_SAFE_CONN_CUR] = { .name = "safe_conn_cur", .desc = "Current number of safe idle connections"},
259 [ST_F_USED_CONN_CUR] = { .name = "used_conn_cur", .desc = "Current number of connections in use"},
Willy Tarreaua9fcecb2020-06-29 15:38:53 +0200260 [ST_F_NEED_CONN_EST] = { .name = "need_conn_est", .desc = "Estimated needed number of connections"},
Willy Tarreaubd715102020-10-23 22:44:30 +0200261 [ST_F_UWEIGHT] = { .name = "uweight", .desc = "Server's user weight, or sum of active servers' user weights for a backend" },
William Dauchy42d7c402021-11-07 10:18:47 +0100262 [ST_F_AGG_SRV_CHECK_STATUS] = { .name = "agg_server_check_status", .desc = "Backend's aggregated gauge of servers' state check status" },
Aurelien DARRAGON745ce8e2022-11-17 16:34:07 +0100263 [ST_F_SRID] = { .name = "srid", .desc = "Server id revision, to prevent server id reuse mixups" },
William Lallemand74c24fb2016-11-21 17:18:36 +0100264};
265
Willy Tarreau0baac8c2016-11-22 16:36:53 +0100266/* one line of info */
William Dauchy5d9b8f32021-01-11 20:07:49 +0100267THREAD_LOCAL struct field info[INF_TOTAL_FIELDS];
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +0200268
269/* description of statistics (static and dynamic) */
270static struct name_desc *stat_f[STATS_DOMAIN_COUNT];
271static size_t stat_count[STATS_DOMAIN_COUNT];
272
273/* one line for stats */
William Dauchyb9577452021-01-17 18:27:46 +0100274THREAD_LOCAL struct field *stat_l[STATS_DOMAIN_COUNT];
William Lallemand74c24fb2016-11-21 17:18:36 +0100275
Amaury Denoyelle58d395e2020-10-05 11:49:40 +0200276/* list of all registered stats module */
277static struct list stats_module_list[STATS_DOMAIN_COUNT] = {
278 LIST_HEAD_INIT(stats_module_list[STATS_DOMAIN_PROXY]),
Emeric Brunf8642ee2021-10-29 17:59:18 +0200279 LIST_HEAD_INIT(stats_module_list[STATS_DOMAIN_RESOLVERS]),
Amaury Denoyelle58d395e2020-10-05 11:49:40 +0200280};
281
Amaury Denoyelle7f8f6cb2020-11-10 14:24:31 +0100282THREAD_LOCAL void *trash_counters;
283
Amaury Denoyelle072f97e2020-10-05 11:49:37 +0200284static inline uint8_t stats_get_domain(uint32_t domain)
285{
286 return domain >> STATS_DOMAIN & STATS_DOMAIN_MASK;
287}
288
Amaury Denoyelle72b16e52020-10-05 11:49:38 +0200289static inline enum stats_domain_px_cap stats_px_get_cap(uint32_t domain)
290{
291 return domain >> STATS_PX_CAP & STATS_PX_CAP_MASK;
292}
293
Christopher Faulet6338a082019-09-09 15:50:54 +0200294static void stats_dump_json_schema(struct buffer *out);
William Lallemand74c24fb2016-11-21 17:18:36 +0100295
Amaury Denoyelle3ca927e2020-10-02 18:32:00 +0200296int stats_putchk(struct channel *chn, struct htx *htx, struct buffer *chk)
Christopher Fauletef779222018-10-31 08:47:01 +0100297{
298 if (htx) {
Christopher Faulet69fc88c2019-01-07 14:27:53 +0100299 if (chk->data >= channel_htx_recv_max(chn, htx))
300 return 0;
Willy Tarreau0a7ef022019-05-28 10:30:11 +0200301 if (!htx_add_data_atonce(htx, ist2(chk->area, chk->data)))
Christopher Fauletef779222018-10-31 08:47:01 +0100302 return 0;
Christopher Faulet5adbeeb2019-01-02 14:34:39 +0100303 channel_add_input(chn, chk->data);
Christopher Fauletef779222018-10-31 08:47:01 +0100304 chk->data = 0;
Christopher Fauletef779222018-10-31 08:47:01 +0100305 }
306 else {
307 if (ci_putchk(chn, chk) == -1)
308 return 0;
309 }
310 return 1;
311}
Willy Tarreau0baac8c2016-11-22 16:36:53 +0100312
Willy Tarreaucaff6312022-05-27 10:17:46 +0200313static const char *stats_scope_ptr(struct appctx *appctx, struct stconn *sc)
Christopher Fauleted7a0662019-01-14 11:07:34 +0100314{
Willy Tarreau91cefca2022-05-03 17:08:29 +0200315 struct show_stat_ctx *ctx = appctx->svcctx;
Willy Tarreaucaff6312022-05-27 10:17:46 +0200316 struct channel *req = sc_oc(sc);
Christopher Fauletb7f88902019-07-15 21:56:43 +0200317 struct htx *htx = htxbuf(&req->buf);
318 struct htx_blk *blk;
319 struct ist uri;
Christopher Fauleted7a0662019-01-14 11:07:34 +0100320
Christopher Fauletb7f88902019-07-15 21:56:43 +0200321 blk = htx_get_head_blk(htx);
Christopher Fauletea009732019-11-18 15:50:25 +0100322 BUG_ON(!blk || htx_get_blk_type(blk) != HTX_BLK_REQ_SL);
Christopher Fauletb7f88902019-07-15 21:56:43 +0200323 ALREADY_CHECKED(blk);
324 uri = htx_sl_req_uri(htx_get_blk_ptr(htx, blk));
Willy Tarreau91cefca2022-05-03 17:08:29 +0200325 return uri.ptr + ctx->scope_str;
Christopher Fauleted7a0662019-01-14 11:07:34 +0100326}
327
William Lallemand74c24fb2016-11-21 17:18:36 +0100328/*
329 * http_stats_io_handler()
330 * -> stats_dump_stat_to_buffer() // same as above, but used for CSV or HTML
331 * -> stats_dump_csv_header() // emits the CSV headers (same as above)
Simon Horman05ee2132017-01-04 09:37:25 +0100332 * -> stats_dump_json_header() // emits the JSON headers (same as above)
William Lallemand74c24fb2016-11-21 17:18:36 +0100333 * -> stats_dump_html_head() // emits the HTML headers
334 * -> stats_dump_html_info() // emits the equivalent of "show info" at the top
335 * -> stats_dump_proxy_to_buffer() // same as above, valid for CSV and HTML
336 * -> stats_dump_html_px_hdr()
337 * -> stats_dump_fe_stats()
338 * -> stats_dump_li_stats()
339 * -> stats_dump_sv_stats()
340 * -> stats_dump_be_stats()
341 * -> stats_dump_html_px_end()
342 * -> stats_dump_html_end() // emits HTML trailer
Simon Horman05ee2132017-01-04 09:37:25 +0100343 * -> stats_dump_json_end() // emits JSON trailer
William Lallemand74c24fb2016-11-21 17:18:36 +0100344 */
345
346
William Lallemand74c24fb2016-11-21 17:18:36 +0100347/* Dumps the stats CSV header to the trash buffer which. The caller is responsible
348 * for clearing it if needed.
349 * NOTE: Some tools happen to rely on the field position instead of its name,
350 * so please only append new fields at the end, never in the middle.
351 */
Amaury Denoyelle50660a82020-10-05 11:49:39 +0200352static void stats_dump_csv_header(enum stats_domain domain)
William Lallemand74c24fb2016-11-21 17:18:36 +0100353{
354 int field;
355
356 chunk_appendf(&trash, "# ");
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +0200357 if (stat_f[domain]) {
358 for (field = 0; field < stat_count[domain]; ++field) {
359 chunk_appendf(&trash, "%s,", stat_f[domain][field].name);
William Lallemand74c24fb2016-11-21 17:18:36 +0100360
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +0200361 /* print special delimiter on proxy stats to mark end of
362 static fields */
363 if (domain == STATS_DOMAIN_PROXY && field + 1 == ST_F_TOTAL_FIELDS)
364 chunk_appendf(&trash, "-,");
Amaury Denoyelle50660a82020-10-05 11:49:39 +0200365 }
366 }
367
William Lallemand74c24fb2016-11-21 17:18:36 +0100368 chunk_appendf(&trash, "\n");
369}
370
William Lallemand74c24fb2016-11-21 17:18:36 +0100371/* Emits a stats field without any surrounding element and properly encoded to
372 * resist CSV output. Returns non-zero on success, 0 if the buffer is full.
373 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200374int stats_emit_raw_data_field(struct buffer *out, const struct field *f)
William Lallemand74c24fb2016-11-21 17:18:36 +0100375{
376 switch (field_format(f, 0)) {
377 case FF_EMPTY: return 1;
378 case FF_S32: return chunk_appendf(out, "%d", f->u.s32);
379 case FF_U32: return chunk_appendf(out, "%u", f->u.u32);
380 case FF_S64: return chunk_appendf(out, "%lld", (long long)f->u.s64);
381 case FF_U64: return chunk_appendf(out, "%llu", (unsigned long long)f->u.u64);
Willy Tarreau065ba312021-05-08 10:38:20 +0200382 case FF_FLT: {
383 size_t prev_data = out->data;
384 out->data = flt_trim(out->area, prev_data, chunk_appendf(out, "%f", f->u.flt));
385 return out->data;
386 }
William Lallemand74c24fb2016-11-21 17:18:36 +0100387 case FF_STR: return csv_enc_append(field_str(f, 0), 1, out) != NULL;
388 default: return chunk_appendf(out, "[INCORRECT_FIELD_TYPE_%08x]", f->type);
389 }
390}
391
Amaury Denoyellee3f576c2020-10-05 11:49:44 +0200392const char *field_to_html_str(const struct field *f)
393{
394 switch (field_format(f, 0)) {
395 case FF_S32: return U2H(f->u.s32);
396 case FF_S64: return U2H(f->u.s64);
397 case FF_U64: return U2H(f->u.u64);
398 case FF_U32: return U2H(f->u.u32);
Willy Tarreau6004fb72021-05-08 07:37:38 +0200399 case FF_FLT: return F2H(f->u.flt);
Amaury Denoyellee3f576c2020-10-05 11:49:44 +0200400 case FF_STR: return field_str(f, 0);
401 case FF_EMPTY:
402 default:
403 return "";
404 }
405}
406
William Lallemand74c24fb2016-11-21 17:18:36 +0100407/* Emits a stats field prefixed with its type. No CSV encoding is prepared, the
408 * output is supposed to be used on its own line. Returns non-zero on success, 0
409 * if the buffer is full.
410 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200411int stats_emit_typed_data_field(struct buffer *out, const struct field *f)
William Lallemand74c24fb2016-11-21 17:18:36 +0100412{
413 switch (field_format(f, 0)) {
414 case FF_EMPTY: return 1;
415 case FF_S32: return chunk_appendf(out, "s32:%d", f->u.s32);
416 case FF_U32: return chunk_appendf(out, "u32:%u", f->u.u32);
417 case FF_S64: return chunk_appendf(out, "s64:%lld", (long long)f->u.s64);
418 case FF_U64: return chunk_appendf(out, "u64:%llu", (unsigned long long)f->u.u64);
Willy Tarreau065ba312021-05-08 10:38:20 +0200419 case FF_FLT: {
420 size_t prev_data = out->data;
421 out->data = flt_trim(out->area, prev_data, chunk_appendf(out, "flt:%f", f->u.flt));
422 return out->data;
423 }
William Lallemand74c24fb2016-11-21 17:18:36 +0100424 case FF_STR: return chunk_appendf(out, "str:%s", field_str(f, 0));
425 default: return chunk_appendf(out, "%08x:?", f->type);
426 }
427}
428
Simon Horman05ee2132017-01-04 09:37:25 +0100429/* Limit JSON integer values to the range [-(2**53)+1, (2**53)-1] as per
430 * the recommendation for interoperable integers in section 6 of RFC 7159.
431 */
432#define JSON_INT_MAX ((1ULL << 53) - 1)
433#define JSON_INT_MIN (0 - JSON_INT_MAX)
434
435/* Emits a stats field value and its type in JSON.
436 * Returns non-zero on success, 0 on error.
437 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200438int stats_emit_json_data_field(struct buffer *out, const struct field *f)
Simon Horman05ee2132017-01-04 09:37:25 +0100439{
440 int old_len;
441 char buf[20];
442 const char *type, *value = buf, *quote = "";
443
444 switch (field_format(f, 0)) {
445 case FF_EMPTY: return 1;
446 case FF_S32: type = "\"s32\"";
447 snprintf(buf, sizeof(buf), "%d", f->u.s32);
448 break;
449 case FF_U32: type = "\"u32\"";
450 snprintf(buf, sizeof(buf), "%u", f->u.u32);
451 break;
452 case FF_S64: type = "\"s64\"";
453 if (f->u.s64 < JSON_INT_MIN || f->u.s64 > JSON_INT_MAX)
454 return 0;
455 type = "\"s64\"";
456 snprintf(buf, sizeof(buf), "%lld", (long long)f->u.s64);
457 break;
458 case FF_U64: if (f->u.u64 > JSON_INT_MAX)
459 return 0;
460 type = "\"u64\"";
461 snprintf(buf, sizeof(buf), "%llu",
462 (unsigned long long) f->u.u64);
Christopher Faulet52c91bb2019-09-28 10:37:31 +0200463 break;
Christopher Faulet88a0db22019-09-24 16:35:10 +0200464 case FF_FLT: type = "\"flt\"";
Willy Tarreau065ba312021-05-08 10:38:20 +0200465 flt_trim(buf, 0, snprintf(buf, sizeof(buf), "%f", f->u.flt));
Simon Horman05ee2132017-01-04 09:37:25 +0100466 break;
467 case FF_STR: type = "\"str\"";
468 value = field_str(f, 0);
469 quote = "\"";
470 break;
471 default: snprintf(buf, sizeof(buf), "%u", f->type);
472 type = buf;
473 value = "unknown";
474 quote = "\"";
475 break;
476 }
477
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200478 old_len = out->data;
Simon Horman05ee2132017-01-04 09:37:25 +0100479 chunk_appendf(out, ",\"value\":{\"type\":%s,\"value\":%s%s%s}",
480 type, quote, value, quote);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200481 return !(old_len == out->data);
Simon Horman05ee2132017-01-04 09:37:25 +0100482}
483
William Lallemand74c24fb2016-11-21 17:18:36 +0100484/* Emits an encoding of the field type on 3 characters followed by a delimiter.
485 * Returns non-zero on success, 0 if the buffer is full.
486 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200487int stats_emit_field_tags(struct buffer *out, const struct field *f,
488 char delim)
William Lallemand74c24fb2016-11-21 17:18:36 +0100489{
490 char origin, nature, scope;
491
492 switch (field_origin(f, 0)) {
493 case FO_METRIC: origin = 'M'; break;
494 case FO_STATUS: origin = 'S'; break;
495 case FO_KEY: origin = 'K'; break;
496 case FO_CONFIG: origin = 'C'; break;
497 case FO_PRODUCT: origin = 'P'; break;
498 default: origin = '?'; break;
499 }
500
501 switch (field_nature(f, 0)) {
502 case FN_GAUGE: nature = 'G'; break;
503 case FN_LIMIT: nature = 'L'; break;
504 case FN_MIN: nature = 'm'; break;
505 case FN_MAX: nature = 'M'; break;
506 case FN_RATE: nature = 'R'; break;
507 case FN_COUNTER: nature = 'C'; break;
508 case FN_DURATION: nature = 'D'; break;
509 case FN_AGE: nature = 'A'; break;
510 case FN_TIME: nature = 'T'; break;
511 case FN_NAME: nature = 'N'; break;
512 case FN_OUTPUT: nature = 'O'; break;
513 case FN_AVG: nature = 'a'; break;
514 default: nature = '?'; break;
515 }
516
517 switch (field_scope(f, 0)) {
518 case FS_PROCESS: scope = 'P'; break;
519 case FS_SERVICE: scope = 'S'; break;
520 case FS_SYSTEM: scope = 's'; break;
521 case FS_CLUSTER: scope = 'C'; break;
522 default: scope = '?'; break;
523 }
524
525 return chunk_appendf(out, "%c%c%c%c", origin, nature, scope, delim);
526}
527
Simon Horman05ee2132017-01-04 09:37:25 +0100528/* Emits an encoding of the field type as JSON.
529 * Returns non-zero on success, 0 if the buffer is full.
530 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200531int stats_emit_json_field_tags(struct buffer *out, const struct field *f)
Simon Horman05ee2132017-01-04 09:37:25 +0100532{
533 const char *origin, *nature, *scope;
534 int old_len;
535
536 switch (field_origin(f, 0)) {
537 case FO_METRIC: origin = "Metric"; break;
538 case FO_STATUS: origin = "Status"; break;
539 case FO_KEY: origin = "Key"; break;
540 case FO_CONFIG: origin = "Config"; break;
541 case FO_PRODUCT: origin = "Product"; break;
542 default: origin = "Unknown"; break;
543 }
544
545 switch (field_nature(f, 0)) {
546 case FN_GAUGE: nature = "Gauge"; break;
547 case FN_LIMIT: nature = "Limit"; break;
548 case FN_MIN: nature = "Min"; break;
549 case FN_MAX: nature = "Max"; break;
550 case FN_RATE: nature = "Rate"; break;
551 case FN_COUNTER: nature = "Counter"; break;
552 case FN_DURATION: nature = "Duration"; break;
553 case FN_AGE: nature = "Age"; break;
554 case FN_TIME: nature = "Time"; break;
555 case FN_NAME: nature = "Name"; break;
556 case FN_OUTPUT: nature = "Output"; break;
557 case FN_AVG: nature = "Avg"; break;
558 default: nature = "Unknown"; break;
559 }
560
561 switch (field_scope(f, 0)) {
562 case FS_PROCESS: scope = "Process"; break;
563 case FS_SERVICE: scope = "Service"; break;
564 case FS_SYSTEM: scope = "System"; break;
565 case FS_CLUSTER: scope = "Cluster"; break;
566 default: scope = "Unknown"; break;
567 }
568
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200569 old_len = out->data;
Simon Horman05ee2132017-01-04 09:37:25 +0100570 chunk_appendf(out, "\"tags\":{"
571 "\"origin\":\"%s\","
572 "\"nature\":\"%s\","
573 "\"scope\":\"%s\""
574 "}", origin, nature, scope);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200575 return !(old_len == out->data);
Simon Horman05ee2132017-01-04 09:37:25 +0100576}
William Lallemand74c24fb2016-11-21 17:18:36 +0100577
578/* Dump all fields from <stats> into <out> using CSV format */
Willy Tarreau83061a82018-07-13 11:56:34 +0200579static int stats_dump_fields_csv(struct buffer *out,
Amaury Denoyelle97323c92020-10-02 18:32:01 +0200580 const struct field *stats, size_t stats_count,
Amaury Denoyelle50660a82020-10-05 11:49:39 +0200581 unsigned int flags,
582 enum stats_domain domain)
William Lallemand74c24fb2016-11-21 17:18:36 +0100583{
584 int field;
585
Amaury Denoyelle50660a82020-10-05 11:49:39 +0200586 for (field = 0; field < stats_count; ++field) {
William Lallemand74c24fb2016-11-21 17:18:36 +0100587 if (!stats_emit_raw_data_field(out, &stats[field]))
588 return 0;
589 if (!chunk_strcat(out, ","))
590 return 0;
Amaury Denoyelle50660a82020-10-05 11:49:39 +0200591
592 /* print special delimiter on proxy stats to mark end of
593 static fields */
594 if (domain == STATS_DOMAIN_PROXY && field + 1 == ST_F_TOTAL_FIELDS) {
595 if (!chunk_strcat(out, "-,"))
596 return 0;
597 }
William Lallemand74c24fb2016-11-21 17:18:36 +0100598 }
Amaury Denoyelle50660a82020-10-05 11:49:39 +0200599
William Lallemand74c24fb2016-11-21 17:18:36 +0100600 chunk_strcat(out, "\n");
601 return 1;
602}
603
604/* Dump all fields from <stats> into <out> using a typed "field:desc:type:value" format */
Willy Tarreau83061a82018-07-13 11:56:34 +0200605static int stats_dump_fields_typed(struct buffer *out,
Amaury Denoyelle97323c92020-10-02 18:32:01 +0200606 const struct field *stats,
607 size_t stats_count,
Amaury Denoyelle072f97e2020-10-05 11:49:37 +0200608 unsigned int flags,
609 enum stats_domain domain)
William Lallemand74c24fb2016-11-21 17:18:36 +0100610{
611 int field;
612
Amaury Denoyelle97323c92020-10-02 18:32:01 +0200613 for (field = 0; field < stats_count; ++field) {
William Lallemand74c24fb2016-11-21 17:18:36 +0100614 if (!stats[field].type)
615 continue;
616
Amaury Denoyelle072f97e2020-10-05 11:49:37 +0200617 switch (domain) {
618 case STATS_DOMAIN_PROXY:
619 chunk_appendf(out, "%c.%u.%u.%d.%s.%u:",
620 stats[ST_F_TYPE].u.u32 == STATS_TYPE_FE ? 'F' :
621 stats[ST_F_TYPE].u.u32 == STATS_TYPE_BE ? 'B' :
622 stats[ST_F_TYPE].u.u32 == STATS_TYPE_SO ? 'L' :
623 stats[ST_F_TYPE].u.u32 == STATS_TYPE_SV ? 'S' :
624 '?',
625 stats[ST_F_IID].u.u32, stats[ST_F_SID].u.u32,
626 field,
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +0200627 stat_f[domain][field].name,
Amaury Denoyelle072f97e2020-10-05 11:49:37 +0200628 stats[ST_F_PID].u.u32);
629 break;
630
Emeric Brunf8642ee2021-10-29 17:59:18 +0200631 case STATS_DOMAIN_RESOLVERS:
632 chunk_appendf(out, "N.%d.%s:", field,
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +0200633 stat_f[domain][field].name);
634 break;
635
Amaury Denoyelle072f97e2020-10-05 11:49:37 +0200636 default:
637 break;
638 }
William Lallemand74c24fb2016-11-21 17:18:36 +0100639
640 if (!stats_emit_field_tags(out, &stats[field], ':'))
641 return 0;
642 if (!stats_emit_typed_data_field(out, &stats[field]))
643 return 0;
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +0200644
Willy Tarreaufc8e4382021-06-17 07:22:27 +0200645 if (flags & STAT_SHOW_FDESC &&
646 !chunk_appendf(out, ":\"%s\"", stat_f[domain][field].desc)) {
Willy Tarreau6b19b142019-10-09 15:44:21 +0200647 return 0;
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +0200648 }
649
William Lallemand74c24fb2016-11-21 17:18:36 +0100650 if (!chunk_strcat(out, "\n"))
651 return 0;
652 }
653 return 1;
654}
655
Simon Horman05ee2132017-01-04 09:37:25 +0100656/* Dump all fields from <stats> into <out> using the "show info json" format */
Willy Tarreau83061a82018-07-13 11:56:34 +0200657static int stats_dump_json_info_fields(struct buffer *out,
Willy Tarreau43241ff2019-10-09 11:27:51 +0200658 const struct field *info, unsigned int flags)
Simon Horman05ee2132017-01-04 09:37:25 +0100659{
660 int field;
661 int started = 0;
662
663 if (!chunk_strcat(out, "["))
664 return 0;
665
666 for (field = 0; field < INF_TOTAL_FIELDS; field++) {
667 int old_len;
668
669 if (!field_format(info, field))
670 continue;
671
672 if (started && !chunk_strcat(out, ","))
673 goto err;
674 started = 1;
675
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200676 old_len = out->data;
Simon Horman05ee2132017-01-04 09:37:25 +0100677 chunk_appendf(out,
678 "{\"field\":{\"pos\":%d,\"name\":\"%s\"},"
679 "\"processNum\":%u,",
Willy Tarreaueaa55372019-10-09 07:39:11 +0200680 field, info_fields[field].name,
Simon Horman05ee2132017-01-04 09:37:25 +0100681 info[INF_PROCESS_NUM].u.u32);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200682 if (old_len == out->data)
Simon Horman05ee2132017-01-04 09:37:25 +0100683 goto err;
684
685 if (!stats_emit_json_field_tags(out, &info[field]))
686 goto err;
687
688 if (!stats_emit_json_data_field(out, &info[field]))
689 goto err;
690
691 if (!chunk_strcat(out, "}"))
692 goto err;
693 }
694
Willy Tarreauc6b7a972022-06-10 15:12:21 +0200695 if (!chunk_strcat(out, "]\n"))
Simon Horman05ee2132017-01-04 09:37:25 +0100696 goto err;
697 return 1;
698
699err:
700 chunk_reset(out);
Willy Tarreauc6b7a972022-06-10 15:12:21 +0200701 chunk_appendf(out, "{\"errorStr\":\"output buffer too short\"}\n");
Simon Horman05ee2132017-01-04 09:37:25 +0100702 return 0;
703}
704
Amaury Denoyellef34017b2020-10-02 18:32:03 +0200705static void stats_print_proxy_field_json(struct buffer *out,
706 const struct field *stat,
707 const char *name,
708 int pos,
709 uint32_t field_type,
710 uint32_t iid,
711 uint32_t sid,
712 uint32_t pid)
713{
714 const char *obj_type;
715 switch (field_type) {
716 case STATS_TYPE_FE: obj_type = "Frontend"; break;
717 case STATS_TYPE_BE: obj_type = "Backend"; break;
718 case STATS_TYPE_SO: obj_type = "Listener"; break;
719 case STATS_TYPE_SV: obj_type = "Server"; break;
720 default: obj_type = "Unknown"; break;
721 }
722
723 chunk_appendf(out,
724 "{"
725 "\"objType\":\"%s\","
726 "\"proxyId\":%u,"
727 "\"id\":%u,"
728 "\"field\":{\"pos\":%d,\"name\":\"%s\"},"
729 "\"processNum\":%u,",
730 obj_type, iid, sid, pos, name, pid);
731}
732
Emeric Brunf8642ee2021-10-29 17:59:18 +0200733static void stats_print_rslv_field_json(struct buffer *out,
734 const struct field *stat,
735 const char *name,
736 int pos)
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +0200737{
738 chunk_appendf(out,
739 "{"
740 "\"field\":{\"pos\":%d,\"name\":\"%s\"},",
741 pos, name);
742}
743
744
Simon Horman05ee2132017-01-04 09:37:25 +0100745/* Dump all fields from <stats> into <out> using a typed "field:desc:type:value" format */
Willy Tarreau83061a82018-07-13 11:56:34 +0200746static int stats_dump_fields_json(struct buffer *out,
Amaury Denoyelle97323c92020-10-02 18:32:01 +0200747 const struct field *stats, size_t stats_count,
Amaury Denoyelle072f97e2020-10-05 11:49:37 +0200748 unsigned int flags,
749 enum stats_domain domain)
Simon Horman05ee2132017-01-04 09:37:25 +0100750{
751 int field;
752 int started = 0;
753
Willy Tarreaub0ce3ad2019-10-09 11:19:29 +0200754 if ((flags & STAT_STARTED) && !chunk_strcat(out, ","))
Simon Horman05ee2132017-01-04 09:37:25 +0100755 return 0;
756 if (!chunk_strcat(out, "["))
757 return 0;
758
Amaury Denoyelle97323c92020-10-02 18:32:01 +0200759 for (field = 0; field < stats_count; field++) {
Simon Horman05ee2132017-01-04 09:37:25 +0100760 int old_len;
761
762 if (!stats[field].type)
763 continue;
764
765 if (started && !chunk_strcat(out, ","))
766 goto err;
767 started = 1;
768
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200769 old_len = out->data;
Amaury Denoyelle072f97e2020-10-05 11:49:37 +0200770 if (domain == STATS_DOMAIN_PROXY) {
771 stats_print_proxy_field_json(out, &stats[field],
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +0200772 stat_f[domain][field].name,
773 field,
Amaury Denoyelle072f97e2020-10-05 11:49:37 +0200774 stats[ST_F_TYPE].u.u32,
775 stats[ST_F_IID].u.u32,
776 stats[ST_F_SID].u.u32,
777 stats[ST_F_PID].u.u32);
Emeric Brunf8642ee2021-10-29 17:59:18 +0200778 } else if (domain == STATS_DOMAIN_RESOLVERS) {
779 stats_print_rslv_field_json(out, &stats[field],
780 stat_f[domain][field].name,
781 field);
Amaury Denoyelle072f97e2020-10-05 11:49:37 +0200782 }
Amaury Denoyellef34017b2020-10-02 18:32:03 +0200783
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200784 if (old_len == out->data)
Simon Horman05ee2132017-01-04 09:37:25 +0100785 goto err;
786
787 if (!stats_emit_json_field_tags(out, &stats[field]))
788 goto err;
789
790 if (!stats_emit_json_data_field(out, &stats[field]))
791 goto err;
792
793 if (!chunk_strcat(out, "}"))
794 goto err;
795 }
796
797 if (!chunk_strcat(out, "]"))
798 goto err;
799
800 return 1;
801
802err:
803 chunk_reset(out);
Willy Tarreaub0ce3ad2019-10-09 11:19:29 +0200804 if (flags & STAT_STARTED)
805 chunk_strcat(out, ",");
Simon Horman05ee2132017-01-04 09:37:25 +0100806 chunk_appendf(out, "{\"errorStr\":\"output buffer too short\"}");
807 return 0;
808}
809
William Lallemand74c24fb2016-11-21 17:18:36 +0100810/* Dump all fields from <stats> into <out> using the HTML format. A column is
Willy Tarreauab02b3f2019-10-09 11:11:46 +0200811 * reserved for the checkbox is STAT_ADMIN is set in <flags>. Some extra info
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +0200812 * are provided if STAT_SHLGNDS is present in <flags>. The statistics from
813 * extra modules are displayed at the end of the lines if STAT_SHMODULES is
814 * present in <flags>.
William Lallemand74c24fb2016-11-21 17:18:36 +0100815 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200816static int stats_dump_fields_html(struct buffer *out,
817 const struct field *stats,
818 unsigned int flags)
William Lallemand74c24fb2016-11-21 17:18:36 +0100819{
Willy Tarreau83061a82018-07-13 11:56:34 +0200820 struct buffer src;
Amaury Denoyellee3f576c2020-10-05 11:49:44 +0200821 struct stats_module *mod;
822 int i = 0, j = 0;
William Lallemand74c24fb2016-11-21 17:18:36 +0100823
824 if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_FE) {
825 chunk_appendf(out,
826 /* name, queue */
827 "<tr class=\"frontend\">");
828
Willy Tarreauab02b3f2019-10-09 11:11:46 +0200829 if (flags & STAT_ADMIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +0100830 /* Column sub-heading for Enable or Disable server */
831 chunk_appendf(out, "<td></td>");
832 }
833
834 chunk_appendf(out,
835 "<td class=ac>"
836 "<a name=\"%s/Frontend\"></a>"
837 "<a class=lfsb href=\"#%s/Frontend\">Frontend</a></td>"
838 "<td colspan=3></td>"
839 "",
840 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_PXNAME));
841
842 chunk_appendf(out,
843 /* sessions rate : current */
844 "<td><u>%s<div class=tips><table class=det>"
845 "<tr><th>Current connection rate:</th><td>%s/s</td></tr>"
846 "<tr><th>Current session rate:</th><td>%s/s</td></tr>"
847 "",
848 U2H(stats[ST_F_RATE].u.u32),
849 U2H(stats[ST_F_CONN_RATE].u.u32),
850 U2H(stats[ST_F_RATE].u.u32));
851
852 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
853 chunk_appendf(out,
854 "<tr><th>Current request rate:</th><td>%s/s</td></tr>",
855 U2H(stats[ST_F_REQ_RATE].u.u32));
856
857 chunk_appendf(out,
858 "</table></div></u></td>"
859 /* sessions rate : max */
860 "<td><u>%s<div class=tips><table class=det>"
861 "<tr><th>Max connection rate:</th><td>%s/s</td></tr>"
862 "<tr><th>Max session rate:</th><td>%s/s</td></tr>"
863 "",
864 U2H(stats[ST_F_RATE_MAX].u.u32),
865 U2H(stats[ST_F_CONN_RATE_MAX].u.u32),
866 U2H(stats[ST_F_RATE_MAX].u.u32));
867
868 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
869 chunk_appendf(out,
870 "<tr><th>Max request rate:</th><td>%s/s</td></tr>",
871 U2H(stats[ST_F_REQ_RATE_MAX].u.u32));
872
873 chunk_appendf(out,
874 "</table></div></u></td>"
875 /* sessions rate : limit */
876 "<td>%s</td>",
877 LIM2A(stats[ST_F_RATE_LIM].u.u32, "-"));
878
879 chunk_appendf(out,
880 /* sessions: current, max, limit, total */
881 "<td>%s</td><td>%s</td><td>%s</td>"
882 "<td><u>%s<div class=tips><table class=det>"
883 "<tr><th>Cum. connections:</th><td>%s</td></tr>"
884 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
885 "",
886 U2H(stats[ST_F_SCUR].u.u32), U2H(stats[ST_F_SMAX].u.u32), U2H(stats[ST_F_SLIM].u.u32),
887 U2H(stats[ST_F_STOT].u.u64),
888 U2H(stats[ST_F_CONN_TOT].u.u64),
889 U2H(stats[ST_F_STOT].u.u64));
890
891 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
892 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0) {
893 chunk_appendf(out,
894 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
895 "<tr><th>- HTTP 1xx responses:</th><td>%s</td></tr>"
896 "<tr><th>- HTTP 2xx responses:</th><td>%s</td></tr>"
897 "<tr><th>&nbsp;&nbsp;Compressed 2xx:</th><td>%s</td><td>(%d%%)</td></tr>"
898 "<tr><th>- HTTP 3xx responses:</th><td>%s</td></tr>"
899 "<tr><th>- HTTP 4xx responses:</th><td>%s</td></tr>"
900 "<tr><th>- HTTP 5xx responses:</th><td>%s</td></tr>"
901 "<tr><th>- other responses:</th><td>%s</td></tr>"
902 "<tr><th>Intercepted requests:</th><td>%s</td></tr>"
Willy Tarreaua1214a52018-12-14 14:00:25 +0100903 "<tr><th>Cache lookups:</th><td>%s</td></tr>"
904 "<tr><th>Cache hits:</th><td>%s</td><td>(%d%%)</td></tr>"
Willy Tarreau1b0f85e2018-05-28 15:12:40 +0200905 "<tr><th>Failed hdr rewrites:</th><td>%s</td></tr>"
Christopher Faulet0159ee42019-12-16 14:40:39 +0100906 "<tr><th>Internal errors:</th><td>%s</td></tr>"
William Lallemand74c24fb2016-11-21 17:18:36 +0100907 "",
908 U2H(stats[ST_F_REQ_TOT].u.u64),
909 U2H(stats[ST_F_HRSP_1XX].u.u64),
910 U2H(stats[ST_F_HRSP_2XX].u.u64),
911 U2H(stats[ST_F_COMP_RSP].u.u64),
912 stats[ST_F_HRSP_2XX].u.u64 ?
913 (int)(100 * stats[ST_F_COMP_RSP].u.u64 / stats[ST_F_HRSP_2XX].u.u64) : 0,
914 U2H(stats[ST_F_HRSP_3XX].u.u64),
915 U2H(stats[ST_F_HRSP_4XX].u.u64),
916 U2H(stats[ST_F_HRSP_5XX].u.u64),
917 U2H(stats[ST_F_HRSP_OTHER].u.u64),
Willy Tarreau1b0f85e2018-05-28 15:12:40 +0200918 U2H(stats[ST_F_INTERCEPTED].u.u64),
Willy Tarreaua1214a52018-12-14 14:00:25 +0100919 U2H(stats[ST_F_CACHE_LOOKUPS].u.u64),
920 U2H(stats[ST_F_CACHE_HITS].u.u64),
921 stats[ST_F_CACHE_LOOKUPS].u.u64 ?
922 (int)(100 * stats[ST_F_CACHE_HITS].u.u64 / stats[ST_F_CACHE_LOOKUPS].u.u64) : 0,
Christopher Faulet0159ee42019-12-16 14:40:39 +0100923 U2H(stats[ST_F_WREW].u.u64),
924 U2H(stats[ST_F_EINT].u.u64));
William Lallemand74c24fb2016-11-21 17:18:36 +0100925 }
926
927 chunk_appendf(out,
928 "</table></div></u></td>"
929 /* sessions: lbtot, lastsess */
930 "<td></td><td></td>"
931 /* bytes : in */
932 "<td>%s</td>"
933 "",
934 U2H(stats[ST_F_BIN].u.u64));
935
936 chunk_appendf(out,
937 /* bytes:out + compression stats (via hover): comp_in, comp_out, comp_byp */
938 "<td>%s%s<div class=tips><table class=det>"
939 "<tr><th>Response bytes in:</th><td>%s</td></tr>"
940 "<tr><th>Compression in:</th><td>%s</td></tr>"
941 "<tr><th>Compression out:</th><td>%s</td><td>(%d%%)</td></tr>"
942 "<tr><th>Compression bypass:</th><td>%s</td></tr>"
943 "<tr><th>Total bytes saved:</th><td>%s</td><td>(%d%%)</td></tr>"
944 "</table></div>%s</td>",
945 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "<u>":"",
946 U2H(stats[ST_F_BOUT].u.u64),
947 U2H(stats[ST_F_BOUT].u.u64),
948 U2H(stats[ST_F_COMP_IN].u.u64),
949 U2H(stats[ST_F_COMP_OUT].u.u64),
950 stats[ST_F_COMP_IN].u.u64 ? (int)(stats[ST_F_COMP_OUT].u.u64 * 100 / stats[ST_F_COMP_IN].u.u64) : 0,
951 U2H(stats[ST_F_COMP_BYP].u.u64),
952 U2H(stats[ST_F_COMP_IN].u.u64 - stats[ST_F_COMP_OUT].u.u64),
953 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,
954 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "</u>":"");
955
956 chunk_appendf(out,
957 /* denied: req, resp */
958 "<td>%s</td><td>%s</td>"
959 /* errors : request, connect, response */
960 "<td>%s</td><td></td><td></td>"
961 /* warnings: retries, redispatches */
962 "<td></td><td></td>"
963 /* server status : reflect frontend status */
964 "<td class=ac>%s</td>"
965 /* rest of server: nothing */
Amaury Denoyellee3f576c2020-10-05 11:49:44 +0200966 "<td class=ac colspan=8></td>"
William Lallemand74c24fb2016-11-21 17:18:36 +0100967 "",
968 U2H(stats[ST_F_DREQ].u.u64), U2H(stats[ST_F_DRESP].u.u64),
969 U2H(stats[ST_F_EREQ].u.u64),
970 field_str(stats, ST_F_STATUS));
Amaury Denoyellee3f576c2020-10-05 11:49:44 +0200971
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +0200972 if (flags & STAT_SHMODULES) {
973 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
Amaury Denoyellee6ee8202020-11-03 15:04:46 +0100974 chunk_appendf(out, "<td>");
975
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +0200976 if (stats_px_get_cap(mod->domain_flags) & STATS_PX_CAP_FE) {
Amaury Denoyellee6ee8202020-11-03 15:04:46 +0100977 chunk_appendf(out,
978 "<u>%s<div class=tips><table class=det>",
979 mod->name);
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +0200980 for (j = 0; j < mod->stats_count; ++j) {
981 chunk_appendf(out,
982 "<tr><th>%s</th><td>%s</td></tr>",
983 mod->stats[j].desc, field_to_html_str(&stats[ST_F_TOTAL_FIELDS + i]));
984 ++i;
985 }
Amaury Denoyellee6ee8202020-11-03 15:04:46 +0100986 chunk_appendf(out, "</table></div></u>");
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +0200987 } else {
988 i += mod->stats_count;
Amaury Denoyellee3f576c2020-10-05 11:49:44 +0200989 }
Amaury Denoyellee3f576c2020-10-05 11:49:44 +0200990
Amaury Denoyellee6ee8202020-11-03 15:04:46 +0100991 chunk_appendf(out, "</td>");
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +0200992 }
Amaury Denoyellee3f576c2020-10-05 11:49:44 +0200993 }
994
995 chunk_appendf(out, "</tr>");
William Lallemand74c24fb2016-11-21 17:18:36 +0100996 }
997 else if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_SO) {
998 chunk_appendf(out, "<tr class=socket>");
Willy Tarreauab02b3f2019-10-09 11:11:46 +0200999 if (flags & STAT_ADMIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001000 /* Column sub-heading for Enable or Disable server */
1001 chunk_appendf(out, "<td></td>");
1002 }
1003
1004 chunk_appendf(out,
1005 /* frontend name, listener name */
1006 "<td class=ac><a name=\"%s/+%s\"></a>%s"
1007 "<a class=lfsb href=\"#%s/+%s\">%s</a>"
1008 "",
1009 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME),
Willy Tarreau708c4162019-10-09 10:19:16 +02001010 (flags & STAT_SHLGNDS)?"<u>":"",
William Lallemand74c24fb2016-11-21 17:18:36 +01001011 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME), field_str(stats, ST_F_SVNAME));
1012
Willy Tarreau708c4162019-10-09 10:19:16 +02001013 if (flags & STAT_SHLGNDS) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001014 chunk_appendf(out, "<div class=tips>");
1015
Willy Tarreau90807112020-02-25 08:16:33 +01001016 if (isdigit((unsigned char)*field_str(stats, ST_F_ADDR)))
William Lallemand74c24fb2016-11-21 17:18:36 +01001017 chunk_appendf(out, "IPv4: %s, ", field_str(stats, ST_F_ADDR));
1018 else if (*field_str(stats, ST_F_ADDR) == '[')
1019 chunk_appendf(out, "IPv6: %s, ", field_str(stats, ST_F_ADDR));
1020 else if (*field_str(stats, ST_F_ADDR))
1021 chunk_appendf(out, "%s, ", field_str(stats, ST_F_ADDR));
1022
1023 /* id */
1024 chunk_appendf(out, "id: %d</div>", stats[ST_F_SID].u.u32);
1025 }
1026
1027 chunk_appendf(out,
1028 /* queue */
1029 "%s</td><td colspan=3></td>"
1030 /* sessions rate: current, max, limit */
1031 "<td colspan=3>&nbsp;</td>"
1032 /* sessions: current, max, limit, total, lbtot, lastsess */
1033 "<td>%s</td><td>%s</td><td>%s</td>"
1034 "<td>%s</td><td>&nbsp;</td><td>&nbsp;</td>"
1035 /* bytes: in, out */
1036 "<td>%s</td><td>%s</td>"
1037 "",
Willy Tarreau708c4162019-10-09 10:19:16 +02001038 (flags & STAT_SHLGNDS)?"</u>":"",
William Lallemand74c24fb2016-11-21 17:18:36 +01001039 U2H(stats[ST_F_SCUR].u.u32), U2H(stats[ST_F_SMAX].u.u32), U2H(stats[ST_F_SLIM].u.u32),
1040 U2H(stats[ST_F_STOT].u.u64), U2H(stats[ST_F_BIN].u.u64), U2H(stats[ST_F_BOUT].u.u64));
1041
1042 chunk_appendf(out,
1043 /* denied: req, resp */
1044 "<td>%s</td><td>%s</td>"
1045 /* errors: request, connect, response */
1046 "<td>%s</td><td></td><td></td>"
1047 /* warnings: retries, redispatches */
1048 "<td></td><td></td>"
1049 /* server status: reflect listener status */
1050 "<td class=ac>%s</td>"
1051 /* rest of server: nothing */
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001052 "<td class=ac colspan=8></td>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001053 "",
1054 U2H(stats[ST_F_DREQ].u.u64), U2H(stats[ST_F_DRESP].u.u64),
1055 U2H(stats[ST_F_EREQ].u.u64),
1056 field_str(stats, ST_F_STATUS));
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001057
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001058 if (flags & STAT_SHMODULES) {
1059 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001060 chunk_appendf(out, "<td>");
1061
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001062 if (stats_px_get_cap(mod->domain_flags) & STATS_PX_CAP_LI) {
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001063 chunk_appendf(out,
1064 "<u>%s<div class=tips><table class=det>",
1065 mod->name);
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001066 for (j = 0; j < mod->stats_count; ++j) {
1067 chunk_appendf(out,
1068 "<tr><th>%s</th><td>%s</td></tr>",
1069 mod->stats[j].desc, field_to_html_str(&stats[ST_F_TOTAL_FIELDS + i]));
1070 ++i;
1071 }
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001072 chunk_appendf(out, "</table></div></u>");
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001073 } else {
1074 i += mod->stats_count;
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001075 }
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001076
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001077 chunk_appendf(out, "</td>");
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001078 }
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001079 }
1080
1081 chunk_appendf(out, "</tr>");
William Lallemand74c24fb2016-11-21 17:18:36 +01001082 }
1083 else if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_SV) {
1084 const char *style;
1085
1086 /* determine the style to use depending on the server's state,
1087 * its health and weight. There isn't a 1-to-1 mapping between
1088 * state and styles for the cases where the server is (still)
1089 * up. The reason is that we don't want to report nolb and
1090 * drain with the same color.
1091 */
1092
1093 if (strcmp(field_str(stats, ST_F_STATUS), "DOWN") == 0 ||
1094 strcmp(field_str(stats, ST_F_STATUS), "DOWN (agent)") == 0) {
1095 style = "down";
1096 }
Florian Apolloner39272c22021-03-30 13:28:35 +02001097 else if (strncmp(field_str(stats, ST_F_STATUS), "DOWN ", strlen("DOWN ")) == 0) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001098 style = "going_up";
1099 }
Daniel Corbettb4285172020-03-28 12:35:50 -04001100 else if (strcmp(field_str(stats, ST_F_STATUS), "DRAIN") == 0) {
1101 style = "draining";
1102 }
Florian Apolloner39272c22021-03-30 13:28:35 +02001103 else if (strncmp(field_str(stats, ST_F_STATUS), "NOLB ", strlen("NOLB ")) == 0) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001104 style = "going_down";
1105 }
1106 else if (strcmp(field_str(stats, ST_F_STATUS), "NOLB") == 0) {
1107 style = "nolb";
1108 }
1109 else if (strcmp(field_str(stats, ST_F_STATUS), "no check") == 0) {
1110 style = "no_check";
1111 }
1112 else if (!stats[ST_F_CHKFAIL].type ||
1113 stats[ST_F_CHECK_HEALTH].u.u32 == stats[ST_F_CHECK_RISE].u.u32 + stats[ST_F_CHECK_FALL].u.u32 - 1) {
1114 /* no check or max health = UP */
1115 if (stats[ST_F_WEIGHT].u.u32)
1116 style = "up";
1117 else
1118 style = "draining";
1119 }
1120 else {
1121 style = "going_down";
1122 }
1123
Willy Tarreau7b524852020-08-11 10:26:36 +02001124 if (strncmp(field_str(stats, ST_F_STATUS), "MAINT", 5) == 0)
William Lallemand74c24fb2016-11-21 17:18:36 +01001125 chunk_appendf(out, "<tr class=\"maintain\">");
1126 else
1127 chunk_appendf(out,
1128 "<tr class=\"%s_%s\">",
1129 (stats[ST_F_BCK].u.u32) ? "backup" : "active", style);
1130
1131
Willy Tarreauab02b3f2019-10-09 11:11:46 +02001132 if (flags & STAT_ADMIN)
William Lallemand74c24fb2016-11-21 17:18:36 +01001133 chunk_appendf(out,
David Harrigand3db35a2016-12-30 12:12:49 +00001134 "<td><input class='%s-checkbox' type=\"checkbox\" name=\"s\" value=\"%s\"></td>",
1135 field_str(stats, ST_F_PXNAME),
William Lallemand74c24fb2016-11-21 17:18:36 +01001136 field_str(stats, ST_F_SVNAME));
1137
1138 chunk_appendf(out,
1139 "<td class=ac><a name=\"%s/%s\"></a>%s"
1140 "<a class=lfsb href=\"#%s/%s\">%s</a>"
1141 "",
1142 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME),
Willy Tarreau708c4162019-10-09 10:19:16 +02001143 (flags & STAT_SHLGNDS) ? "<u>" : "",
William Lallemand74c24fb2016-11-21 17:18:36 +01001144 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME), field_str(stats, ST_F_SVNAME));
1145
Willy Tarreau708c4162019-10-09 10:19:16 +02001146 if (flags & STAT_SHLGNDS) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001147 chunk_appendf(out, "<div class=tips>");
1148
Willy Tarreau90807112020-02-25 08:16:33 +01001149 if (isdigit((unsigned char)*field_str(stats, ST_F_ADDR)))
William Lallemand74c24fb2016-11-21 17:18:36 +01001150 chunk_appendf(out, "IPv4: %s, ", field_str(stats, ST_F_ADDR));
1151 else if (*field_str(stats, ST_F_ADDR) == '[')
1152 chunk_appendf(out, "IPv6: %s, ", field_str(stats, ST_F_ADDR));
1153 else if (*field_str(stats, ST_F_ADDR))
1154 chunk_appendf(out, "%s, ", field_str(stats, ST_F_ADDR));
1155
1156 /* id */
Aurelien DARRAGON745ce8e2022-11-17 16:34:07 +01001157 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 +01001158
1159 /* cookie */
1160 if (stats[ST_F_COOKIE].type) {
1161 chunk_appendf(out, ", cookie: '");
1162 chunk_initstr(&src, field_str(stats, ST_F_COOKIE));
1163 chunk_htmlencode(out, &src);
1164 chunk_appendf(out, "'");
1165 }
1166
1167 chunk_appendf(out, "</div>");
1168 }
1169
1170 chunk_appendf(out,
1171 /* queue : current, max, limit */
1172 "%s</td><td>%s</td><td>%s</td><td>%s</td>"
1173 /* sessions rate : current, max, limit */
1174 "<td>%s</td><td>%s</td><td></td>"
1175 "",
Willy Tarreau708c4162019-10-09 10:19:16 +02001176 (flags & STAT_SHLGNDS) ? "</u>" : "",
William Lallemand74c24fb2016-11-21 17:18:36 +01001177 U2H(stats[ST_F_QCUR].u.u32), U2H(stats[ST_F_QMAX].u.u32), LIM2A(stats[ST_F_QLIMIT].u.u32, "-"),
1178 U2H(stats[ST_F_RATE].u.u32), U2H(stats[ST_F_RATE_MAX].u.u32));
1179
1180 chunk_appendf(out,
1181 /* sessions: current, max, limit, total */
Willy Tarreauf21d17b2019-09-08 09:24:56 +02001182 "<td><u>%s<div class=tips>"
1183 "<table class=det>"
1184 "<tr><th>Current active connections:</th><td>%s</td></tr>"
Willy Tarreau3bb617c2020-06-29 13:51:05 +02001185 "<tr><th>Current used connections:</th><td>%s</td></tr>"
Willy Tarreauf21d17b2019-09-08 09:24:56 +02001186 "<tr><th>Current idle connections:</th><td>%s</td></tr>"
Willy Tarreau3bb617c2020-06-29 13:51:05 +02001187 "<tr><th>- unsafe:</th><td>%s</td></tr>"
1188 "<tr><th>- safe:</th><td>%s</td></tr>"
Willy Tarreaua9fcecb2020-06-29 15:38:53 +02001189 "<tr><th>Estimated need of connections:</th><td>%s</td></tr>"
Willy Tarreauf21d17b2019-09-08 09:24:56 +02001190 "<tr><th>Active connections limit:</th><td>%s</td></tr>"
1191 "<tr><th>Idle connections limit:</th><td>%s</td></tr>"
1192 "</table></div></u>"
1193 "</td><td>%s</td><td>%s</td>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001194 "<td><u>%s<div class=tips><table class=det>"
1195 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
1196 "",
Willy Tarreauf21d17b2019-09-08 09:24:56 +02001197 U2H(stats[ST_F_SCUR].u.u32),
Willy Tarreau3bb617c2020-06-29 13:51:05 +02001198 U2H(stats[ST_F_SCUR].u.u32),
1199 U2H(stats[ST_F_USED_CONN_CUR].u.u32),
1200 U2H(stats[ST_F_SRV_ICUR].u.u32),
1201 U2H(stats[ST_F_IDLE_CONN_CUR].u.u32),
1202 U2H(stats[ST_F_SAFE_CONN_CUR].u.u32),
Willy Tarreaua9fcecb2020-06-29 15:38:53 +02001203 U2H(stats[ST_F_NEED_CONN_EST].u.u32),
Willy Tarreau3bb617c2020-06-29 13:51:05 +02001204
Willy Tarreauf21d17b2019-09-08 09:24:56 +02001205 LIM2A(stats[ST_F_SLIM].u.u32, "-"),
1206 stats[ST_F_SRV_ILIM].type ? U2H(stats[ST_F_SRV_ILIM].u.u32) : "-",
1207 U2H(stats[ST_F_SMAX].u.u32), LIM2A(stats[ST_F_SLIM].u.u32, "-"),
William Lallemand74c24fb2016-11-21 17:18:36 +01001208 U2H(stats[ST_F_STOT].u.u64),
1209 U2H(stats[ST_F_STOT].u.u64));
1210
1211 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
1212 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001213 chunk_appendf(out,
Willy Tarreauf1573842018-12-14 11:35:36 +01001214 "<tr><th>New connections:</th><td>%s</td></tr>"
1215 "<tr><th>Reused connections:</th><td>%s</td><td>(%d%%)</td></tr>"
Marcin Deranek3c27dda2020-05-15 18:32:51 +02001216 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001217 "<tr><th>- HTTP 1xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
1218 "<tr><th>- HTTP 2xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
1219 "<tr><th>- HTTP 3xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
1220 "<tr><th>- HTTP 4xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
1221 "<tr><th>- HTTP 5xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
1222 "<tr><th>- other responses:</th><td>%s</td><td>(%d%%)</td></tr>"
Willy Tarreau1b0f85e2018-05-28 15:12:40 +02001223 "<tr><th>Failed hdr rewrites:</th><td>%s</td></tr>"
Christopher Faulet0159ee42019-12-16 14:40:39 +01001224 "<tr><th>Internal error:</th><td>%s</td></tr>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001225 "",
Willy Tarreauf1573842018-12-14 11:35:36 +01001226 U2H(stats[ST_F_CONNECT].u.u64),
1227 U2H(stats[ST_F_REUSE].u.u64),
1228 (stats[ST_F_CONNECT].u.u64 + stats[ST_F_REUSE].u.u64) ?
1229 (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 +02001230 U2H(stats[ST_F_REQ_TOT].u.u64),
1231 U2H(stats[ST_F_HRSP_1XX].u.u64), stats[ST_F_REQ_TOT].u.u64 ?
1232 (int)(100 * stats[ST_F_HRSP_1XX].u.u64 / stats[ST_F_REQ_TOT].u.u64) : 0,
1233 U2H(stats[ST_F_HRSP_2XX].u.u64), stats[ST_F_REQ_TOT].u.u64 ?
1234 (int)(100 * stats[ST_F_HRSP_2XX].u.u64 / stats[ST_F_REQ_TOT].u.u64) : 0,
1235 U2H(stats[ST_F_HRSP_3XX].u.u64), stats[ST_F_REQ_TOT].u.u64 ?
1236 (int)(100 * stats[ST_F_HRSP_3XX].u.u64 / stats[ST_F_REQ_TOT].u.u64) : 0,
1237 U2H(stats[ST_F_HRSP_4XX].u.u64), stats[ST_F_REQ_TOT].u.u64 ?
1238 (int)(100 * stats[ST_F_HRSP_4XX].u.u64 / stats[ST_F_REQ_TOT].u.u64) : 0,
1239 U2H(stats[ST_F_HRSP_5XX].u.u64), stats[ST_F_REQ_TOT].u.u64 ?
1240 (int)(100 * stats[ST_F_HRSP_5XX].u.u64 / stats[ST_F_REQ_TOT].u.u64) : 0,
1241 U2H(stats[ST_F_HRSP_OTHER].u.u64), stats[ST_F_REQ_TOT].u.u64 ?
1242 (int)(100 * stats[ST_F_HRSP_OTHER].u.u64 / stats[ST_F_REQ_TOT].u.u64) : 0,
Christopher Faulet0159ee42019-12-16 14:40:39 +01001243 U2H(stats[ST_F_WREW].u.u64),
1244 U2H(stats[ST_F_EINT].u.u64));
William Lallemand74c24fb2016-11-21 17:18:36 +01001245 }
1246
Christopher Faulet0d1c2a62019-11-08 14:59:51 +01001247 chunk_appendf(out, "<tr><th colspan=3>Max / Avg over last 1024 success. conn.</th></tr>");
1248 chunk_appendf(out, "<tr><th>- Queue time:</th><td>%s / %s</td><td>ms</td></tr>",
1249 U2H(stats[ST_F_QT_MAX].u.u32), U2H(stats[ST_F_QTIME].u.u32));
1250 chunk_appendf(out, "<tr><th>- Connect time:</th><td>%s / %s</td><td>ms</td></tr>",
1251 U2H(stats[ST_F_CT_MAX].u.u32), U2H(stats[ST_F_CTIME].u.u32));
William Lallemand74c24fb2016-11-21 17:18:36 +01001252 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
Christopher Faulet0d1c2a62019-11-08 14:59:51 +01001253 chunk_appendf(out, "<tr><th>- Responses time:</th><td>%s / %s</td><td>ms</td></tr>",
1254 U2H(stats[ST_F_RT_MAX].u.u32), U2H(stats[ST_F_RTIME].u.u32));
1255 chunk_appendf(out, "<tr><th>- Total time:</th><td>%s / %s</td><td>ms</td></tr>",
1256 U2H(stats[ST_F_TT_MAX].u.u32), U2H(stats[ST_F_TTIME].u.u32));
William Lallemand74c24fb2016-11-21 17:18:36 +01001257
1258 chunk_appendf(out,
1259 "</table></div></u></td>"
1260 /* sessions: lbtot, last */
1261 "<td>%s</td><td>%s</td>",
1262 U2H(stats[ST_F_LBTOT].u.u64),
1263 human_time(stats[ST_F_LASTSESS].u.s32, 1));
1264
1265 chunk_appendf(out,
1266 /* bytes : in, out */
1267 "<td>%s</td><td>%s</td>"
1268 /* denied: req, resp */
1269 "<td></td><td>%s</td>"
1270 /* errors : request, connect */
1271 "<td></td><td>%s</td>"
1272 /* errors : response */
1273 "<td><u>%s<div class=tips>Connection resets during transfers: %lld client, %lld server</div></u></td>"
1274 /* warnings: retries, redispatches */
1275 "<td>%lld</td><td>%lld</td>"
1276 "",
1277 U2H(stats[ST_F_BIN].u.u64), U2H(stats[ST_F_BOUT].u.u64),
1278 U2H(stats[ST_F_DRESP].u.u64),
1279 U2H(stats[ST_F_ECON].u.u64),
1280 U2H(stats[ST_F_ERESP].u.u64),
1281 (long long)stats[ST_F_CLI_ABRT].u.u64,
1282 (long long)stats[ST_F_SRV_ABRT].u.u64,
1283 (long long)stats[ST_F_WRETR].u.u64,
1284 (long long)stats[ST_F_WREDIS].u.u64);
1285
1286 /* status, last change */
1287 chunk_appendf(out, "<td class=ac>");
1288
1289 /* FIXME!!!!
1290 * LASTCHG should contain the last change for *this* server and must be computed
1291 * properly above, as was done below, ie: this server if maint, otherwise ref server
1292 * if tracking. Note that ref is either local or remote depending on tracking.
1293 */
1294
1295
Willy Tarreau7b524852020-08-11 10:26:36 +02001296 if (strncmp(field_str(stats, ST_F_STATUS), "MAINT", 5) == 0) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001297 chunk_appendf(out, "%s MAINT", human_time(stats[ST_F_LASTCHG].u.u32, 1));
1298 }
Willy Tarreau7b524852020-08-11 10:26:36 +02001299 else if (strcmp(field_str(stats, ST_F_STATUS), "no check") == 0) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001300 chunk_strcat(out, "<i>no check</i>");
1301 }
1302 else {
1303 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 +02001304 if (strncmp(field_str(stats, ST_F_STATUS), "DOWN", 4) == 0) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001305 if (stats[ST_F_CHECK_HEALTH].u.u32)
1306 chunk_strcat(out, " &uarr;");
1307 }
1308 else if (stats[ST_F_CHECK_HEALTH].u.u32 < stats[ST_F_CHECK_RISE].u.u32 + stats[ST_F_CHECK_FALL].u.u32 - 1)
1309 chunk_strcat(out, " &darr;");
1310 }
1311
Willy Tarreau7b524852020-08-11 10:26:36 +02001312 if (strncmp(field_str(stats, ST_F_STATUS), "DOWN", 4) == 0 &&
William Lallemand74c24fb2016-11-21 17:18:36 +01001313 stats[ST_F_AGENT_STATUS].type && !stats[ST_F_AGENT_HEALTH].u.u32) {
1314 chunk_appendf(out,
1315 "</td><td class=ac><u> %s",
1316 field_str(stats, ST_F_AGENT_STATUS));
1317
1318 if (stats[ST_F_AGENT_CODE].type)
1319 chunk_appendf(out, "/%d", stats[ST_F_AGENT_CODE].u.u32);
1320
1321 if (stats[ST_F_AGENT_DURATION].type)
1322 chunk_appendf(out, " in %lums", (long)stats[ST_F_AGENT_DURATION].u.u64);
1323
1324 chunk_appendf(out, "<div class=tips>%s", field_str(stats, ST_F_AGENT_DESC));
1325
1326 if (*field_str(stats, ST_F_LAST_AGT)) {
1327 chunk_appendf(out, ": ");
1328 chunk_initstr(&src, field_str(stats, ST_F_LAST_AGT));
1329 chunk_htmlencode(out, &src);
1330 }
1331 chunk_appendf(out, "</div></u>");
1332 }
1333 else if (stats[ST_F_CHECK_STATUS].type) {
1334 chunk_appendf(out,
1335 "</td><td class=ac><u> %s",
1336 field_str(stats, ST_F_CHECK_STATUS));
1337
1338 if (stats[ST_F_CHECK_CODE].type)
1339 chunk_appendf(out, "/%d", stats[ST_F_CHECK_CODE].u.u32);
1340
1341 if (stats[ST_F_CHECK_DURATION].type)
1342 chunk_appendf(out, " in %lums", (long)stats[ST_F_CHECK_DURATION].u.u64);
1343
1344 chunk_appendf(out, "<div class=tips>%s", field_str(stats, ST_F_CHECK_DESC));
1345
1346 if (*field_str(stats, ST_F_LAST_CHK)) {
1347 chunk_appendf(out, ": ");
1348 chunk_initstr(&src, field_str(stats, ST_F_LAST_CHK));
1349 chunk_htmlencode(out, &src);
1350 }
1351 chunk_appendf(out, "</div></u>");
1352 }
1353 else
1354 chunk_appendf(out, "</td><td>");
1355
1356 chunk_appendf(out,
Willy Tarreaubd715102020-10-23 22:44:30 +02001357 /* weight / uweight */
1358 "</td><td class=ac>%d/%d</td>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001359 /* act, bck */
1360 "<td class=ac>%s</td><td class=ac>%s</td>"
1361 "",
Willy Tarreaubd715102020-10-23 22:44:30 +02001362 stats[ST_F_WEIGHT].u.u32, stats[ST_F_UWEIGHT].u.u32,
William Lallemand74c24fb2016-11-21 17:18:36 +01001363 stats[ST_F_BCK].u.u32 ? "-" : "Y",
1364 stats[ST_F_BCK].u.u32 ? "Y" : "-");
1365
1366 /* check failures: unique, fatal, down time */
1367 if (strcmp(field_str(stats, ST_F_STATUS), "MAINT (resolution)") == 0) {
1368 chunk_appendf(out, "<td class=ac colspan=3>resolution</td>");
1369 }
1370 else if (stats[ST_F_CHKFAIL].type) {
1371 chunk_appendf(out, "<td><u>%lld", (long long)stats[ST_F_CHKFAIL].u.u64);
1372
1373 if (stats[ST_F_HANAFAIL].type)
1374 chunk_appendf(out, "/%lld", (long long)stats[ST_F_HANAFAIL].u.u64);
1375
1376 chunk_appendf(out,
1377 "<div class=tips>Failed Health Checks%s</div></u></td>"
1378 "<td>%lld</td><td>%s</td>"
1379 "",
1380 stats[ST_F_HANAFAIL].type ? "/Health Analyses" : "",
1381 (long long)stats[ST_F_CHKDOWN].u.u64, human_time(stats[ST_F_DOWNTIME].u.u32, 1));
1382 }
1383 else if (strcmp(field_str(stats, ST_F_STATUS), "MAINT") != 0 && field_format(stats, ST_F_TRACKED) == FF_STR) {
1384 /* tracking a server (hence inherited maint would appear as "MAINT (via...)" */
1385 chunk_appendf(out,
1386 "<td class=ac colspan=3><a class=lfsb href=\"#%s\">via %s</a></td>",
1387 field_str(stats, ST_F_TRACKED), field_str(stats, ST_F_TRACKED));
1388 }
1389 else
1390 chunk_appendf(out, "<td colspan=3></td>");
1391
1392 /* throttle */
1393 if (stats[ST_F_THROTTLE].type)
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001394 chunk_appendf(out, "<td class=ac>%d %%</td>\n", stats[ST_F_THROTTLE].u.u32);
William Lallemand74c24fb2016-11-21 17:18:36 +01001395 else
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001396 chunk_appendf(out, "<td class=ac>-</td>");
1397
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001398 if (flags & STAT_SHMODULES) {
1399 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001400 chunk_appendf(out, "<td>");
1401
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001402 if (stats_px_get_cap(mod->domain_flags) & STATS_PX_CAP_SRV) {
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001403 chunk_appendf(out,
1404 "<u>%s<div class=tips><table class=det>",
1405 mod->name);
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001406 for (j = 0; j < mod->stats_count; ++j) {
1407 chunk_appendf(out,
1408 "<tr><th>%s</th><td>%s</td></tr>",
1409 mod->stats[j].desc, field_to_html_str(&stats[ST_F_TOTAL_FIELDS + i]));
1410 ++i;
1411 }
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001412 chunk_appendf(out, "</table></div></u>");
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001413 } else {
1414 i += mod->stats_count;
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001415 }
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001416
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001417 chunk_appendf(out, "</td>");
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001418 }
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001419 }
1420
1421 chunk_appendf(out, "</tr>\n");
William Lallemand74c24fb2016-11-21 17:18:36 +01001422 }
1423 else if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_BE) {
1424 chunk_appendf(out, "<tr class=\"backend\">");
Willy Tarreauab02b3f2019-10-09 11:11:46 +02001425 if (flags & STAT_ADMIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001426 /* Column sub-heading for Enable or Disable server */
1427 chunk_appendf(out, "<td></td>");
1428 }
1429 chunk_appendf(out,
1430 "<td class=ac>"
1431 /* name */
1432 "%s<a name=\"%s/Backend\"></a>"
1433 "<a class=lfsb href=\"#%s/Backend\">Backend</a>"
1434 "",
Willy Tarreau708c4162019-10-09 10:19:16 +02001435 (flags & STAT_SHLGNDS)?"<u>":"",
William Lallemand74c24fb2016-11-21 17:18:36 +01001436 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_PXNAME));
1437
Willy Tarreau708c4162019-10-09 10:19:16 +02001438 if (flags & STAT_SHLGNDS) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001439 /* balancing */
1440 chunk_appendf(out, "<div class=tips>balancing: %s",
1441 field_str(stats, ST_F_ALGO));
1442
1443 /* cookie */
1444 if (stats[ST_F_COOKIE].type) {
1445 chunk_appendf(out, ", cookie: '");
1446 chunk_initstr(&src, field_str(stats, ST_F_COOKIE));
1447 chunk_htmlencode(out, &src);
1448 chunk_appendf(out, "'");
1449 }
1450 chunk_appendf(out, "</div>");
1451 }
1452
1453 chunk_appendf(out,
1454 "%s</td>"
1455 /* queue : current, max */
1456 "<td>%s</td><td>%s</td><td></td>"
1457 /* sessions rate : current, max, limit */
1458 "<td>%s</td><td>%s</td><td></td>"
1459 "",
Willy Tarreau708c4162019-10-09 10:19:16 +02001460 (flags & STAT_SHLGNDS)?"</u>":"",
William Lallemand74c24fb2016-11-21 17:18:36 +01001461 U2H(stats[ST_F_QCUR].u.u32), U2H(stats[ST_F_QMAX].u.u32),
1462 U2H(stats[ST_F_RATE].u.u32), U2H(stats[ST_F_RATE_MAX].u.u32));
1463
1464 chunk_appendf(out,
1465 /* sessions: current, max, limit, total */
1466 "<td>%s</td><td>%s</td><td>%s</td>"
1467 "<td><u>%s<div class=tips><table class=det>"
1468 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
1469 "",
Willy Tarreau8e0f1752016-12-12 15:07:29 +01001470 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 +01001471 U2H(stats[ST_F_STOT].u.u64),
1472 U2H(stats[ST_F_STOT].u.u64));
1473
1474 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
1475 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0) {
1476 chunk_appendf(out,
Willy Tarreauf1573842018-12-14 11:35:36 +01001477 "<tr><th>New connections:</th><td>%s</td></tr>"
1478 "<tr><th>Reused connections:</th><td>%s</td><td>(%d%%)</td></tr>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001479 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
1480 "<tr><th>- HTTP 1xx responses:</th><td>%s</td></tr>"
1481 "<tr><th>- HTTP 2xx responses:</th><td>%s</td></tr>"
1482 "<tr><th>&nbsp;&nbsp;Compressed 2xx:</th><td>%s</td><td>(%d%%)</td></tr>"
1483 "<tr><th>- HTTP 3xx responses:</th><td>%s</td></tr>"
1484 "<tr><th>- HTTP 4xx responses:</th><td>%s</td></tr>"
1485 "<tr><th>- HTTP 5xx responses:</th><td>%s</td></tr>"
1486 "<tr><th>- other responses:</th><td>%s</td></tr>"
Willy Tarreaua1214a52018-12-14 14:00:25 +01001487 "<tr><th>Cache lookups:</th><td>%s</td></tr>"
1488 "<tr><th>Cache hits:</th><td>%s</td><td>(%d%%)</td></tr>"
Willy Tarreau1b0f85e2018-05-28 15:12:40 +02001489 "<tr><th>Failed hdr rewrites:</th><td>%s</td></tr>"
Christopher Faulet0159ee42019-12-16 14:40:39 +01001490 "<tr><th>Internal errors:</th><td>%s</td></tr>"
Christopher Faulet0d1c2a62019-11-08 14:59:51 +01001491 "",
Willy Tarreauf1573842018-12-14 11:35:36 +01001492 U2H(stats[ST_F_CONNECT].u.u64),
1493 U2H(stats[ST_F_REUSE].u.u64),
1494 (stats[ST_F_CONNECT].u.u64 + stats[ST_F_REUSE].u.u64) ?
1495 (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 +01001496 U2H(stats[ST_F_REQ_TOT].u.u64),
1497 U2H(stats[ST_F_HRSP_1XX].u.u64),
1498 U2H(stats[ST_F_HRSP_2XX].u.u64),
1499 U2H(stats[ST_F_COMP_RSP].u.u64),
1500 stats[ST_F_HRSP_2XX].u.u64 ?
1501 (int)(100 * stats[ST_F_COMP_RSP].u.u64 / stats[ST_F_HRSP_2XX].u.u64) : 0,
1502 U2H(stats[ST_F_HRSP_3XX].u.u64),
1503 U2H(stats[ST_F_HRSP_4XX].u.u64),
1504 U2H(stats[ST_F_HRSP_5XX].u.u64),
Willy Tarreaufeead3a2018-12-14 13:48:44 +01001505 U2H(stats[ST_F_HRSP_OTHER].u.u64),
Willy Tarreaua1214a52018-12-14 14:00:25 +01001506 U2H(stats[ST_F_CACHE_LOOKUPS].u.u64),
1507 U2H(stats[ST_F_CACHE_HITS].u.u64),
1508 stats[ST_F_CACHE_LOOKUPS].u.u64 ?
1509 (int)(100 * stats[ST_F_CACHE_HITS].u.u64 / stats[ST_F_CACHE_LOOKUPS].u.u64) : 0,
Christopher Faulet0159ee42019-12-16 14:40:39 +01001510 U2H(stats[ST_F_WREW].u.u64),
1511 U2H(stats[ST_F_EINT].u.u64));
William Lallemand74c24fb2016-11-21 17:18:36 +01001512 }
1513
Christopher Faulet0d1c2a62019-11-08 14:59:51 +01001514 chunk_appendf(out, "<tr><th colspan=3>Max / Avg over last 1024 success. conn.</th></tr>");
1515 chunk_appendf(out, "<tr><th>- Queue time:</th><td>%s / %s</td><td>ms</td></tr>",
1516 U2H(stats[ST_F_QT_MAX].u.u32), U2H(stats[ST_F_QTIME].u.u32));
1517 chunk_appendf(out, "<tr><th>- Connect time:</th><td>%s / %s</td><td>ms</td></tr>",
1518 U2H(stats[ST_F_CT_MAX].u.u32), U2H(stats[ST_F_CTIME].u.u32));
William Lallemand74c24fb2016-11-21 17:18:36 +01001519 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
Christopher Faulet0d1c2a62019-11-08 14:59:51 +01001520 chunk_appendf(out, "<tr><th>- Responses time:</th><td>%s / %s</td><td>ms</td></tr>",
1521 U2H(stats[ST_F_RT_MAX].u.u32), U2H(stats[ST_F_RTIME].u.u32));
1522 chunk_appendf(out, "<tr><th>- Total time:</th><td>%s / %s</td><td>ms</td></tr>",
1523 U2H(stats[ST_F_TT_MAX].u.u32), U2H(stats[ST_F_TTIME].u.u32));
William Lallemand74c24fb2016-11-21 17:18:36 +01001524
1525 chunk_appendf(out,
1526 "</table></div></u></td>"
1527 /* sessions: lbtot, last */
1528 "<td>%s</td><td>%s</td>"
1529 /* bytes: in */
1530 "<td>%s</td>"
1531 "",
1532 U2H(stats[ST_F_LBTOT].u.u64),
1533 human_time(stats[ST_F_LASTSESS].u.s32, 1),
1534 U2H(stats[ST_F_BIN].u.u64));
1535
1536 chunk_appendf(out,
1537 /* bytes:out + compression stats (via hover): comp_in, comp_out, comp_byp */
1538 "<td>%s%s<div class=tips><table class=det>"
1539 "<tr><th>Response bytes in:</th><td>%s</td></tr>"
1540 "<tr><th>Compression in:</th><td>%s</td></tr>"
1541 "<tr><th>Compression out:</th><td>%s</td><td>(%d%%)</td></tr>"
1542 "<tr><th>Compression bypass:</th><td>%s</td></tr>"
1543 "<tr><th>Total bytes saved:</th><td>%s</td><td>(%d%%)</td></tr>"
1544 "</table></div>%s</td>",
1545 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "<u>":"",
1546 U2H(stats[ST_F_BOUT].u.u64),
1547 U2H(stats[ST_F_BOUT].u.u64),
1548 U2H(stats[ST_F_COMP_IN].u.u64),
1549 U2H(stats[ST_F_COMP_OUT].u.u64),
1550 stats[ST_F_COMP_IN].u.u64 ? (int)(stats[ST_F_COMP_OUT].u.u64 * 100 / stats[ST_F_COMP_IN].u.u64) : 0,
1551 U2H(stats[ST_F_COMP_BYP].u.u64),
1552 U2H(stats[ST_F_COMP_IN].u.u64 - stats[ST_F_COMP_OUT].u.u64),
1553 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,
1554 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "</u>":"");
1555
1556 chunk_appendf(out,
1557 /* denied: req, resp */
1558 "<td>%s</td><td>%s</td>"
1559 /* errors : request, connect */
1560 "<td></td><td>%s</td>"
1561 /* errors : response */
1562 "<td><u>%s<div class=tips>Connection resets during transfers: %lld client, %lld server</div></u></td>"
1563 /* warnings: retries, redispatches */
1564 "<td>%lld</td><td>%lld</td>"
1565 /* backend status: reflect backend status (up/down): we display UP
1566 * if the backend has known working servers or if it has no server at
1567 * all (eg: for stats). Then we display the total weight, number of
1568 * active and backups. */
Willy Tarreaubd715102020-10-23 22:44:30 +02001569 "<td class=ac>%s %s</td><td class=ac>&nbsp;</td><td class=ac>%d/%d</td>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001570 "<td class=ac>%d</td><td class=ac>%d</td>"
1571 "",
1572 U2H(stats[ST_F_DREQ].u.u64), U2H(stats[ST_F_DRESP].u.u64),
1573 U2H(stats[ST_F_ECON].u.u64),
1574 U2H(stats[ST_F_ERESP].u.u64),
1575 (long long)stats[ST_F_CLI_ABRT].u.u64,
1576 (long long)stats[ST_F_SRV_ABRT].u.u64,
1577 (long long)stats[ST_F_WRETR].u.u64, (long long)stats[ST_F_WREDIS].u.u64,
1578 human_time(stats[ST_F_LASTCHG].u.u32, 1),
1579 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 +02001580 stats[ST_F_WEIGHT].u.u32, stats[ST_F_UWEIGHT].u.u32,
William Lallemand74c24fb2016-11-21 17:18:36 +01001581 stats[ST_F_ACT].u.u32, stats[ST_F_BCK].u.u32);
1582
1583 chunk_appendf(out,
1584 /* rest of backend: nothing, down transitions, total downtime, throttle */
1585 "<td class=ac>&nbsp;</td><td>%d</td>"
1586 "<td>%s</td>"
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001587 "<td></td>",
William Lallemand74c24fb2016-11-21 17:18:36 +01001588 stats[ST_F_CHKDOWN].u.u32,
1589 stats[ST_F_DOWNTIME].type ? human_time(stats[ST_F_DOWNTIME].u.u32, 1) : "&nbsp;");
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001590
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001591 if (flags & STAT_SHMODULES) {
1592 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001593 chunk_appendf(out, "<td>");
1594
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001595 if (stats_px_get_cap(mod->domain_flags) & STATS_PX_CAP_BE) {
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001596 chunk_appendf(out,
1597 "<u>%s<div class=tips><table class=det>",
1598 mod->name);
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001599 for (j = 0; j < mod->stats_count; ++j) {
1600 chunk_appendf(out,
1601 "<tr><th>%s</th><td>%s</td></tr>",
1602 mod->stats[j].desc, field_to_html_str(&stats[ST_F_TOTAL_FIELDS + i]));
1603 ++i;
1604 }
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001605 chunk_appendf(out, "</table></div></u>");
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001606 } else {
1607 i += mod->stats_count;
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001608 }
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001609
1610 chunk_appendf(out, "</td>");
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001611 }
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001612 }
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001613
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001614 chunk_appendf(out, "</tr>");
William Lallemand74c24fb2016-11-21 17:18:36 +01001615 }
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001616
William Lallemand74c24fb2016-11-21 17:18:36 +01001617 return 1;
1618}
1619
Amaury Denoyelle97323c92020-10-02 18:32:01 +02001620int stats_dump_one_line(const struct field *stats, size_t stats_count,
1621 struct appctx *appctx)
William Lallemand74c24fb2016-11-21 17:18:36 +01001622{
Willy Tarreau91cefca2022-05-03 17:08:29 +02001623 struct show_stat_ctx *ctx = appctx->svcctx;
Simon Horman05ee2132017-01-04 09:37:25 +01001624 int ret;
1625
Willy Tarreau91cefca2022-05-03 17:08:29 +02001626 if (ctx->flags & STAT_FMT_HTML)
1627 ret = stats_dump_fields_html(&trash, stats, ctx->flags);
1628 else if (ctx->flags & STAT_FMT_TYPED)
1629 ret = stats_dump_fields_typed(&trash, stats, stats_count, ctx->flags, ctx->domain);
1630 else if (ctx->flags & STAT_FMT_JSON)
1631 ret = stats_dump_fields_json(&trash, stats, stats_count, ctx->flags, ctx->domain);
William Lallemand74c24fb2016-11-21 17:18:36 +01001632 else
Willy Tarreau91cefca2022-05-03 17:08:29 +02001633 ret = stats_dump_fields_csv(&trash, stats, stats_count, ctx->flags, ctx->domain);
Simon Horman05ee2132017-01-04 09:37:25 +01001634
1635 if (ret)
Willy Tarreau91cefca2022-05-03 17:08:29 +02001636 ctx->flags |= STAT_STARTED;
Simon Horman05ee2132017-01-04 09:37:25 +01001637
1638 return ret;
William Lallemand74c24fb2016-11-21 17:18:36 +01001639}
1640
William Dauchy0ef54392021-01-17 18:27:45 +01001641/* Fill <stats> with the frontend statistics. <stats> is preallocated array of
Christopher Fauletd808f172021-01-22 17:33:22 +01001642 * length <len>. If <selected_field> is != NULL, only fill this one. The length
1643 * of the array must be at least ST_F_TOTAL_FIELDS. If this length is less than
1644 * this value, or if the selected field is not implemented for frontends, the
1645 * function returns 0, otherwise, it returns 1.
William Lallemand74c24fb2016-11-21 17:18:36 +01001646 */
William Dauchy0ef54392021-01-17 18:27:45 +01001647int stats_fill_fe_stats(struct proxy *px, struct field *stats, int len,
1648 enum stat_field *selected_field)
William Lallemand74c24fb2016-11-21 17:18:36 +01001649{
William Dauchy0ef54392021-01-17 18:27:45 +01001650 enum stat_field current_field = (selected_field != NULL ? *selected_field : 0);
William Dauchy0ef54392021-01-17 18:27:45 +01001651
William Lallemand74c24fb2016-11-21 17:18:36 +01001652 if (len < ST_F_TOTAL_FIELDS)
1653 return 0;
1654
William Dauchy0ef54392021-01-17 18:27:45 +01001655 for (; current_field < ST_F_TOTAL_FIELDS; current_field++) {
Christopher Faulet8596bfb2021-01-25 15:16:41 +01001656 struct field metric = { 0 };
1657
William Dauchy0ef54392021-01-17 18:27:45 +01001658 switch (current_field) {
1659 case ST_F_PXNAME:
1660 metric = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
1661 break;
1662 case ST_F_SVNAME:
1663 metric = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, "FRONTEND");
1664 break;
1665 case ST_F_MODE:
1666 metric = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
1667 break;
1668 case ST_F_SCUR:
1669 metric = mkf_u32(0, px->feconn);
1670 break;
1671 case ST_F_SMAX:
1672 metric = mkf_u32(FN_MAX, px->fe_counters.conn_max);
1673 break;
1674 case ST_F_SLIM:
1675 metric = mkf_u32(FO_CONFIG|FN_LIMIT, px->maxconn);
1676 break;
1677 case ST_F_STOT:
1678 metric = mkf_u64(FN_COUNTER, px->fe_counters.cum_sess);
1679 break;
1680 case ST_F_BIN:
1681 metric = mkf_u64(FN_COUNTER, px->fe_counters.bytes_in);
1682 break;
1683 case ST_F_BOUT:
1684 metric = mkf_u64(FN_COUNTER, px->fe_counters.bytes_out);
1685 break;
1686 case ST_F_DREQ:
1687 metric = mkf_u64(FN_COUNTER, px->fe_counters.denied_req);
1688 break;
1689 case ST_F_DRESP:
1690 metric = mkf_u64(FN_COUNTER, px->fe_counters.denied_resp);
1691 break;
1692 case ST_F_EREQ:
1693 metric = mkf_u64(FN_COUNTER, px->fe_counters.failed_req);
1694 break;
1695 case ST_F_DCON:
1696 metric = mkf_u64(FN_COUNTER, px->fe_counters.denied_conn);
1697 break;
1698 case ST_F_DSES:
1699 metric = mkf_u64(FN_COUNTER, px->fe_counters.denied_sess);
1700 break;
Aurelien DARRAGONcddec0a2022-09-09 15:58:57 +02001701 case ST_F_STATUS: {
1702 const char *state;
1703
1704 if (px->flags & (PR_FL_DISABLED|PR_FL_STOPPED))
1705 state = "STOP";
1706 else if (px->flags & PR_FL_PAUSED)
1707 state = "PAUSED";
1708 else
1709 state = "OPEN";
1710 metric = mkf_str(FO_STATUS, state);
William Dauchy0ef54392021-01-17 18:27:45 +01001711 break;
Aurelien DARRAGONcddec0a2022-09-09 15:58:57 +02001712 }
William Dauchy0ef54392021-01-17 18:27:45 +01001713 case ST_F_PID:
Willy Tarreaue8422bf2021-06-15 09:08:18 +02001714 metric = mkf_u32(FO_KEY, 1);
William Dauchy0ef54392021-01-17 18:27:45 +01001715 break;
1716 case ST_F_IID:
1717 metric = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
1718 break;
1719 case ST_F_SID:
1720 metric = mkf_u32(FO_KEY|FS_SERVICE, 0);
1721 break;
1722 case ST_F_TYPE:
1723 metric = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_FE);
1724 break;
1725 case ST_F_RATE:
1726 metric = mkf_u32(FN_RATE, read_freq_ctr(&px->fe_sess_per_sec));
1727 break;
1728 case ST_F_RATE_LIM:
1729 metric = mkf_u32(FO_CONFIG|FN_LIMIT, px->fe_sps_lim);
1730 break;
1731 case ST_F_RATE_MAX:
1732 metric = mkf_u32(FN_MAX, px->fe_counters.sps_max);
1733 break;
1734 case ST_F_WREW:
1735 metric = mkf_u64(FN_COUNTER, px->fe_counters.failed_rewrites);
1736 break;
1737 case ST_F_EINT:
1738 metric = mkf_u64(FN_COUNTER, px->fe_counters.internal_errors);
1739 break;
1740 case ST_F_HRSP_1XX:
William Dauchy2107a0f2021-01-22 21:09:48 +01001741 if (px->mode == PR_MODE_HTTP)
1742 metric = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[1]);
William Dauchy0ef54392021-01-17 18:27:45 +01001743 break;
1744 case ST_F_HRSP_2XX:
William Dauchy2107a0f2021-01-22 21:09:48 +01001745 if (px->mode == PR_MODE_HTTP)
1746 metric = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[2]);
William Dauchy0ef54392021-01-17 18:27:45 +01001747 break;
1748 case ST_F_HRSP_3XX:
William Dauchy2107a0f2021-01-22 21:09:48 +01001749 if (px->mode == PR_MODE_HTTP)
1750 metric = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[3]);
William Dauchy0ef54392021-01-17 18:27:45 +01001751 break;
1752 case ST_F_HRSP_4XX:
William Dauchy2107a0f2021-01-22 21:09:48 +01001753 if (px->mode == PR_MODE_HTTP)
1754 metric = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[4]);
William Dauchy0ef54392021-01-17 18:27:45 +01001755 break;
1756 case ST_F_HRSP_5XX:
William Dauchy2107a0f2021-01-22 21:09:48 +01001757 if (px->mode == PR_MODE_HTTP)
1758 metric = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[5]);
William Dauchy0ef54392021-01-17 18:27:45 +01001759 break;
1760 case ST_F_HRSP_OTHER:
William Dauchy2107a0f2021-01-22 21:09:48 +01001761 if (px->mode == PR_MODE_HTTP)
1762 metric = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[0]);
William Dauchy0ef54392021-01-17 18:27:45 +01001763 break;
1764 case ST_F_INTERCEPTED:
William Dauchy2107a0f2021-01-22 21:09:48 +01001765 if (px->mode == PR_MODE_HTTP)
1766 metric = mkf_u64(FN_COUNTER, px->fe_counters.intercepted_req);
William Dauchy0ef54392021-01-17 18:27:45 +01001767 break;
1768 case ST_F_CACHE_LOOKUPS:
William Dauchy2107a0f2021-01-22 21:09:48 +01001769 if (px->mode == PR_MODE_HTTP)
1770 metric = mkf_u64(FN_COUNTER, px->fe_counters.p.http.cache_lookups);
William Dauchy0ef54392021-01-17 18:27:45 +01001771 break;
1772 case ST_F_CACHE_HITS:
William Dauchy2107a0f2021-01-22 21:09:48 +01001773 if (px->mode == PR_MODE_HTTP)
1774 metric = mkf_u64(FN_COUNTER, px->fe_counters.p.http.cache_hits);
William Dauchy0ef54392021-01-17 18:27:45 +01001775 break;
1776 case ST_F_REQ_RATE:
1777 metric = mkf_u32(FN_RATE, read_freq_ctr(&px->fe_req_per_sec));
1778 break;
1779 case ST_F_REQ_RATE_MAX:
1780 metric = mkf_u32(FN_MAX, px->fe_counters.p.http.rps_max);
1781 break;
1782 case ST_F_REQ_TOT:
1783 metric = mkf_u64(FN_COUNTER, px->fe_counters.p.http.cum_req);
1784 break;
1785 case ST_F_COMP_IN:
1786 metric = mkf_u64(FN_COUNTER, px->fe_counters.comp_in);
1787 break;
1788 case ST_F_COMP_OUT:
1789 metric = mkf_u64(FN_COUNTER, px->fe_counters.comp_out);
1790 break;
1791 case ST_F_COMP_BYP:
1792 metric = mkf_u64(FN_COUNTER, px->fe_counters.comp_byp);
1793 break;
1794 case ST_F_COMP_RSP:
1795 metric = mkf_u64(FN_COUNTER, px->fe_counters.p.http.comp_rsp);
1796 break;
1797 case ST_F_CONN_RATE:
1798 metric = mkf_u32(FN_RATE, read_freq_ctr(&px->fe_conn_per_sec));
1799 break;
1800 case ST_F_CONN_RATE_MAX:
1801 metric = mkf_u32(FN_MAX, px->fe_counters.cps_max);
1802 break;
1803 case ST_F_CONN_TOT:
1804 metric = mkf_u64(FN_COUNTER, px->fe_counters.cum_conn);
1805 break;
1806 default:
Christopher Fauletd808f172021-01-22 17:33:22 +01001807 /* not used for frontends. If a specific metric
1808 * is requested, return an error. Otherwise continue.
1809 */
1810 if (selected_field != NULL)
1811 return 0;
1812 continue;
William Dauchy0ef54392021-01-17 18:27:45 +01001813 }
1814 stats[current_field] = metric;
1815 if (selected_field != NULL)
1816 break;
William Lallemand74c24fb2016-11-21 17:18:36 +01001817 }
William Lallemand74c24fb2016-11-21 17:18:36 +01001818 return 1;
1819}
1820
1821/* Dumps a frontend's line to the trash for the current proxy <px> and uses
Willy Tarreaucaff6312022-05-27 10:17:46 +02001822 * the state from stream connector <sc>. The caller is responsible for clearing
William Lallemand74c24fb2016-11-21 17:18:36 +01001823 * the trash if needed. Returns non-zero if it emits anything, zero otherwise.
1824 */
Willy Tarreaucaff6312022-05-27 10:17:46 +02001825static int stats_dump_fe_stats(struct stconn *sc, struct proxy *px)
William Lallemand74c24fb2016-11-21 17:18:36 +01001826{
Willy Tarreaucaff6312022-05-27 10:17:46 +02001827 struct appctx *appctx = __sc_appctx(sc);
Willy Tarreau91cefca2022-05-03 17:08:29 +02001828 struct show_stat_ctx *ctx = appctx->svcctx;
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02001829 struct field *stats = stat_l[STATS_DOMAIN_PROXY];
1830 struct stats_module *mod;
1831 size_t stats_count = ST_F_TOTAL_FIELDS;
William Lallemand74c24fb2016-11-21 17:18:36 +01001832
1833 if (!(px->cap & PR_CAP_FE))
1834 return 0;
1835
Willy Tarreau91cefca2022-05-03 17:08:29 +02001836 if ((ctx->flags & STAT_BOUND) && !(ctx->type & (1 << STATS_TYPE_FE)))
William Lallemand74c24fb2016-11-21 17:18:36 +01001837 return 0;
1838
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02001839 memset(stats, 0, sizeof(struct field) * stat_count[STATS_DOMAIN_PROXY]);
1840
William Dauchy0ef54392021-01-17 18:27:45 +01001841 if (!stats_fill_fe_stats(px, stats, ST_F_TOTAL_FIELDS, NULL))
William Lallemand74c24fb2016-11-21 17:18:36 +01001842 return 0;
1843
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02001844 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
1845 void *counters;
1846
1847 if (!(stats_px_get_cap(mod->domain_flags) & STATS_PX_CAP_FE)) {
1848 stats_count += mod->stats_count;
1849 continue;
1850 }
1851
1852 counters = EXTRA_COUNTERS_GET(px->extra_counters_fe, mod);
1853 mod->fill_stats(counters, stats + stats_count);
1854 stats_count += mod->stats_count;
1855 }
1856
1857 return stats_dump_one_line(stats, stats_count, appctx);
William Lallemand74c24fb2016-11-21 17:18:36 +01001858}
1859
William Dauchy655e14e2021-02-14 23:22:54 +01001860/* Fill <stats> with the listener statistics. <stats> is preallocated array of
1861 * length <len>. The length of the array must be at least ST_F_TOTAL_FIELDS. If
1862 * this length is less then this value, the function returns 0, otherwise, it
1863 * returns 1. If selected_field is != NULL, only fill this one. <flags> can
1864 * take the value STAT_SHLGNDS.
William Lallemand74c24fb2016-11-21 17:18:36 +01001865 */
1866int stats_fill_li_stats(struct proxy *px, struct listener *l, int flags,
William Dauchy655e14e2021-02-14 23:22:54 +01001867 struct field *stats, int len, enum stat_field *selected_field)
William Lallemand74c24fb2016-11-21 17:18:36 +01001868{
William Dauchy655e14e2021-02-14 23:22:54 +01001869 enum stat_field current_field = (selected_field != NULL ? *selected_field : 0);
Willy Tarreau83061a82018-07-13 11:56:34 +02001870 struct buffer *out = get_trash_chunk();
William Lallemand74c24fb2016-11-21 17:18:36 +01001871
1872 if (len < ST_F_TOTAL_FIELDS)
1873 return 0;
1874
1875 if (!l->counters)
1876 return 0;
1877
1878 chunk_reset(out);
William Lallemand74c24fb2016-11-21 17:18:36 +01001879
William Dauchy655e14e2021-02-14 23:22:54 +01001880 for (; current_field < ST_F_TOTAL_FIELDS; current_field++) {
1881 struct field metric = { 0 };
William Lallemand74c24fb2016-11-21 17:18:36 +01001882
William Dauchy655e14e2021-02-14 23:22:54 +01001883 switch (current_field) {
1884 case ST_F_PXNAME:
1885 metric = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
1886 break;
1887 case ST_F_SVNAME:
1888 metric = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, l->name);
1889 break;
1890 case ST_F_MODE:
1891 metric = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
1892 break;
1893 case ST_F_SCUR:
1894 metric = mkf_u32(0, l->nbconn);
1895 break;
1896 case ST_F_SMAX:
1897 metric = mkf_u32(FN_MAX, l->counters->conn_max);
1898 break;
1899 case ST_F_SLIM:
1900 metric = mkf_u32(FO_CONFIG|FN_LIMIT, l->maxconn);
1901 break;
1902 case ST_F_STOT:
1903 metric = mkf_u64(FN_COUNTER, l->counters->cum_conn);
1904 break;
1905 case ST_F_BIN:
1906 metric = mkf_u64(FN_COUNTER, l->counters->bytes_in);
1907 break;
1908 case ST_F_BOUT:
1909 metric = mkf_u64(FN_COUNTER, l->counters->bytes_out);
1910 break;
1911 case ST_F_DREQ:
1912 metric = mkf_u64(FN_COUNTER, l->counters->denied_req);
1913 break;
1914 case ST_F_DRESP:
1915 metric = mkf_u64(FN_COUNTER, l->counters->denied_resp);
1916 break;
1917 case ST_F_EREQ:
1918 metric = mkf_u64(FN_COUNTER, l->counters->failed_req);
1919 break;
1920 case ST_F_DCON:
1921 metric = mkf_u64(FN_COUNTER, l->counters->denied_conn);
1922 break;
1923 case ST_F_DSES:
1924 metric = mkf_u64(FN_COUNTER, l->counters->denied_sess);
1925 break;
1926 case ST_F_STATUS:
William Dauchy3679d0c2021-02-14 23:22:55 +01001927 metric = mkf_str(FO_STATUS, li_status_st[get_li_status(l)]);
William Dauchy655e14e2021-02-14 23:22:54 +01001928 break;
1929 case ST_F_PID:
Willy Tarreaue8422bf2021-06-15 09:08:18 +02001930 metric = mkf_u32(FO_KEY, 1);
William Dauchy655e14e2021-02-14 23:22:54 +01001931 break;
1932 case ST_F_IID:
1933 metric = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
1934 break;
1935 case ST_F_SID:
1936 metric = mkf_u32(FO_KEY|FS_SERVICE, l->luid);
1937 break;
1938 case ST_F_TYPE:
1939 metric = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_SO);
1940 break;
1941 case ST_F_WREW:
1942 metric = mkf_u64(FN_COUNTER, l->counters->failed_rewrites);
1943 break;
1944 case ST_F_EINT:
1945 metric = mkf_u64(FN_COUNTER, l->counters->internal_errors);
1946 break;
1947 case ST_F_ADDR:
1948 if (flags & STAT_SHLGNDS) {
1949 char str[INET6_ADDRSTRLEN];
1950 int port;
William Lallemand74c24fb2016-11-21 17:18:36 +01001951
William Dauchy655e14e2021-02-14 23:22:54 +01001952 port = get_host_port(&l->rx.addr);
1953 switch (addr_to_str(&l->rx.addr, str, sizeof(str))) {
1954 case AF_INET:
1955 metric = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
1956 chunk_appendf(out, "%s:%d", str, port);
1957 break;
1958 case AF_INET6:
1959 metric = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
1960 chunk_appendf(out, "[%s]:%d", str, port);
1961 break;
1962 case AF_UNIX:
1963 metric = mkf_str(FO_CONFIG|FS_SERVICE, "unix");
1964 break;
1965 case -1:
1966 metric = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
1967 chunk_strcat(out, strerror(errno));
1968 break;
1969 default: /* address family not supported */
1970 break;
1971 }
1972 }
1973 break;
1974 default:
1975 /* not used for listen. If a specific metric
1976 * is requested, return an error. Otherwise continue.
1977 */
1978 if (selected_field != NULL)
1979 return 0;
1980 continue;
William Lallemand74c24fb2016-11-21 17:18:36 +01001981 }
William Dauchy655e14e2021-02-14 23:22:54 +01001982 stats[current_field] = metric;
1983 if (selected_field != NULL)
1984 break;
William Lallemand74c24fb2016-11-21 17:18:36 +01001985 }
William Lallemand74c24fb2016-11-21 17:18:36 +01001986 return 1;
1987}
1988
1989/* Dumps a line for listener <l> and proxy <px> to the trash and uses the state
Willy Tarreaucaff6312022-05-27 10:17:46 +02001990 * from stream connector <sc>. The caller is responsible for clearing the trash
Willy Tarreau43241ff2019-10-09 11:27:51 +02001991 * if needed. Returns non-zero if it emits anything, zero otherwise.
William Lallemand74c24fb2016-11-21 17:18:36 +01001992 */
Willy Tarreaucaff6312022-05-27 10:17:46 +02001993static int stats_dump_li_stats(struct stconn *sc, struct proxy *px, struct listener *l)
William Lallemand74c24fb2016-11-21 17:18:36 +01001994{
Willy Tarreaucaff6312022-05-27 10:17:46 +02001995 struct appctx *appctx = __sc_appctx(sc);
Willy Tarreau91cefca2022-05-03 17:08:29 +02001996 struct show_stat_ctx *ctx = appctx->svcctx;
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02001997 struct field *stats = stat_l[STATS_DOMAIN_PROXY];
1998 struct stats_module *mod;
1999 size_t stats_count = ST_F_TOTAL_FIELDS;
2000
2001 memset(stats, 0, sizeof(struct field) * stat_count[STATS_DOMAIN_PROXY]);
William Lallemand74c24fb2016-11-21 17:18:36 +01002002
Willy Tarreau91cefca2022-05-03 17:08:29 +02002003 if (!stats_fill_li_stats(px, l, ctx->flags, stats,
William Dauchy655e14e2021-02-14 23:22:54 +01002004 ST_F_TOTAL_FIELDS, NULL))
William Lallemand74c24fb2016-11-21 17:18:36 +01002005 return 0;
2006
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02002007 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
2008 void *counters;
2009
2010 if (!(stats_px_get_cap(mod->domain_flags) & STATS_PX_CAP_LI)) {
2011 stats_count += mod->stats_count;
2012 continue;
2013 }
2014
2015 counters = EXTRA_COUNTERS_GET(l->extra_counters, mod);
2016 mod->fill_stats(counters, stats + stats_count);
2017 stats_count += mod->stats_count;
2018 }
2019
2020 return stats_dump_one_line(stats, stats_count, appctx);
William Lallemand74c24fb2016-11-21 17:18:36 +01002021}
2022
2023enum srv_stats_state {
2024 SRV_STATS_STATE_DOWN = 0,
2025 SRV_STATS_STATE_DOWN_AGENT,
2026 SRV_STATS_STATE_GOING_UP,
2027 SRV_STATS_STATE_UP_GOING_DOWN,
2028 SRV_STATS_STATE_UP,
2029 SRV_STATS_STATE_NOLB_GOING_DOWN,
2030 SRV_STATS_STATE_NOLB,
2031 SRV_STATS_STATE_DRAIN_GOING_DOWN,
2032 SRV_STATS_STATE_DRAIN,
2033 SRV_STATS_STATE_DRAIN_AGENT,
2034 SRV_STATS_STATE_NO_CHECK,
2035
2036 SRV_STATS_STATE_COUNT, /* Must be last */
2037};
2038
2039static const char *srv_hlt_st[SRV_STATS_STATE_COUNT] = {
2040 [SRV_STATS_STATE_DOWN] = "DOWN",
2041 [SRV_STATS_STATE_DOWN_AGENT] = "DOWN (agent)",
2042 [SRV_STATS_STATE_GOING_UP] = "DOWN %d/%d",
2043 [SRV_STATS_STATE_UP_GOING_DOWN] = "UP %d/%d",
2044 [SRV_STATS_STATE_UP] = "UP",
2045 [SRV_STATS_STATE_NOLB_GOING_DOWN] = "NOLB %d/%d",
2046 [SRV_STATS_STATE_NOLB] = "NOLB",
2047 [SRV_STATS_STATE_DRAIN_GOING_DOWN] = "DRAIN %d/%d",
2048 [SRV_STATS_STATE_DRAIN] = "DRAIN",
2049 [SRV_STATS_STATE_DRAIN_AGENT] = "DRAIN (agent)",
2050 [SRV_STATS_STATE_NO_CHECK] = "no check"
2051};
2052
William Dauchyd3a9a492021-01-25 17:29:03 +01002053/* Compute server state helper
William Lallemand74c24fb2016-11-21 17:18:36 +01002054 */
William Dauchyd3a9a492021-01-25 17:29:03 +01002055static void stats_fill_sv_stats_computestate(struct server *sv, struct server *ref,
2056 enum srv_stats_state *state)
William Lallemand74c24fb2016-11-21 17:18:36 +01002057{
Emeric Brun52a91d32017-08-31 14:41:55 +02002058 if (sv->cur_state == SRV_ST_RUNNING || sv->cur_state == SRV_ST_STARTING) {
William Lallemand74c24fb2016-11-21 17:18:36 +01002059 if ((ref->check.state & CHK_ST_ENABLED) &&
2060 (ref->check.health < ref->check.rise + ref->check.fall - 1)) {
William Dauchyd3a9a492021-01-25 17:29:03 +01002061 *state = SRV_STATS_STATE_UP_GOING_DOWN;
William Lallemand74c24fb2016-11-21 17:18:36 +01002062 } else {
William Dauchyd3a9a492021-01-25 17:29:03 +01002063 *state = SRV_STATS_STATE_UP;
William Lallemand74c24fb2016-11-21 17:18:36 +01002064 }
2065
Emeric Brun52a91d32017-08-31 14:41:55 +02002066 if (sv->cur_admin & SRV_ADMF_DRAIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +01002067 if (ref->agent.state & CHK_ST_ENABLED)
William Dauchyd3a9a492021-01-25 17:29:03 +01002068 *state = SRV_STATS_STATE_DRAIN_AGENT;
2069 else if (*state == SRV_STATS_STATE_UP_GOING_DOWN)
2070 *state = SRV_STATS_STATE_DRAIN_GOING_DOWN;
William Lallemand74c24fb2016-11-21 17:18:36 +01002071 else
William Dauchyd3a9a492021-01-25 17:29:03 +01002072 *state = SRV_STATS_STATE_DRAIN;
William Lallemand74c24fb2016-11-21 17:18:36 +01002073 }
2074
William Dauchyd3a9a492021-01-25 17:29:03 +01002075 if (*state == SRV_STATS_STATE_UP && !(ref->check.state & CHK_ST_ENABLED)) {
2076 *state = SRV_STATS_STATE_NO_CHECK;
William Lallemand74c24fb2016-11-21 17:18:36 +01002077 }
2078 }
Emeric Brun52a91d32017-08-31 14:41:55 +02002079 else if (sv->cur_state == SRV_ST_STOPPING) {
William Lallemand74c24fb2016-11-21 17:18:36 +01002080 if ((!(sv->check.state & CHK_ST_ENABLED) && !sv->track) ||
2081 (ref->check.health == ref->check.rise + ref->check.fall - 1)) {
William Dauchyd3a9a492021-01-25 17:29:03 +01002082 *state = SRV_STATS_STATE_NOLB;
William Lallemand74c24fb2016-11-21 17:18:36 +01002083 } else {
William Dauchyd3a9a492021-01-25 17:29:03 +01002084 *state = SRV_STATS_STATE_NOLB_GOING_DOWN;
William Lallemand74c24fb2016-11-21 17:18:36 +01002085 }
2086 }
2087 else { /* stopped */
2088 if ((ref->agent.state & CHK_ST_ENABLED) && !ref->agent.health) {
William Dauchyd3a9a492021-01-25 17:29:03 +01002089 *state = SRV_STATS_STATE_DOWN_AGENT;
William Lallemand74c24fb2016-11-21 17:18:36 +01002090 } else if ((ref->check.state & CHK_ST_ENABLED) && !ref->check.health) {
William Dauchyd3a9a492021-01-25 17:29:03 +01002091 *state = SRV_STATS_STATE_DOWN; /* DOWN */
William Lallemand74c24fb2016-11-21 17:18:36 +01002092 } else if ((ref->agent.state & CHK_ST_ENABLED) || (ref->check.state & CHK_ST_ENABLED)) {
William Dauchyd3a9a492021-01-25 17:29:03 +01002093 *state = SRV_STATS_STATE_GOING_UP;
William Lallemand74c24fb2016-11-21 17:18:36 +01002094 } else {
William Dauchyd3a9a492021-01-25 17:29:03 +01002095 *state = SRV_STATS_STATE_DOWN; /* DOWN, unchecked */
William Lallemand74c24fb2016-11-21 17:18:36 +01002096 }
2097 }
William Dauchyd3a9a492021-01-25 17:29:03 +01002098}
William Lallemand74c24fb2016-11-21 17:18:36 +01002099
William Dauchyd3a9a492021-01-25 17:29:03 +01002100/* Fill <stats> with the backend statistics. <stats> is preallocated array of
2101 * length <len>. If <selected_field> is != NULL, only fill this one. The length
2102 * of the array must be at least ST_F_TOTAL_FIELDS. If this length is less than
2103 * this value, or if the selected field is not implemented for servers, the
2104 * function returns 0, otherwise, it returns 1. <flags> can take the value
2105 * STAT_SHLGNDS.
2106 */
2107int stats_fill_sv_stats(struct proxy *px, struct server *sv, int flags,
2108 struct field *stats, int len,
2109 enum stat_field *selected_field)
2110{
2111 enum stat_field current_field = (selected_field != NULL ? *selected_field : 0);
2112 struct server *via = sv->track ? sv->track : sv;
2113 struct server *ref = via;
2114 enum srv_stats_state state = 0;
2115 char str[INET6_ADDRSTRLEN];
2116 struct buffer *out = get_trash_chunk();
2117 char *fld_status;
2118 long long srv_samples_counter;
2119 unsigned int srv_samples_window = TIME_STATS_SAMPLES;
William Lallemand74c24fb2016-11-21 17:18:36 +01002120
William Dauchyd3a9a492021-01-25 17:29:03 +01002121 if (len < ST_F_TOTAL_FIELDS)
2122 return 0;
William Lallemand74c24fb2016-11-21 17:18:36 +01002123
William Dauchyd3a9a492021-01-25 17:29:03 +01002124 chunk_reset(out);
William Lallemand74c24fb2016-11-21 17:18:36 +01002125
William Dauchyd3a9a492021-01-25 17:29:03 +01002126 /* compute state for later use */
2127 if (selected_field == NULL || *selected_field == ST_F_STATUS ||
2128 *selected_field == ST_F_CHECK_RISE || *selected_field == ST_F_CHECK_FALL ||
2129 *selected_field == ST_F_CHECK_HEALTH || *selected_field == ST_F_HANAFAIL) {
2130 /* we have "via" which is the tracked server as described in the configuration,
2131 * and "ref" which is the checked server and the end of the chain.
2132 */
2133 while (ref->track)
2134 ref = ref->track;
2135 stats_fill_sv_stats_computestate(sv, ref, &state);
William Lallemand74c24fb2016-11-21 17:18:36 +01002136 }
2137
William Dauchyd3a9a492021-01-25 17:29:03 +01002138 /* compue time values for later use */
2139 if (selected_field == NULL || *selected_field == ST_F_QTIME ||
2140 *selected_field == ST_F_CTIME || *selected_field == ST_F_RTIME ||
2141 *selected_field == ST_F_TTIME) {
2142 srv_samples_counter = (px->mode == PR_MODE_HTTP) ? sv->counters.p.http.cum_req : sv->counters.cum_lbconn;
2143 if (srv_samples_counter < TIME_STATS_SAMPLES && srv_samples_counter > 0)
2144 srv_samples_window = srv_samples_counter;
William Lallemand74c24fb2016-11-21 17:18:36 +01002145 }
2146
William Dauchyd3a9a492021-01-25 17:29:03 +01002147 for (; current_field < ST_F_TOTAL_FIELDS; current_field++) {
2148 struct field metric = { 0 };
William Lallemand74c24fb2016-11-21 17:18:36 +01002149
William Dauchyd3a9a492021-01-25 17:29:03 +01002150 switch (current_field) {
2151 case ST_F_PXNAME:
2152 metric = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
2153 break;
2154 case ST_F_SVNAME:
2155 metric = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, sv->id);
2156 break;
2157 case ST_F_MODE:
2158 metric = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
Christopher Faulet3888b8c2021-01-27 13:32:25 +01002159 break;
William Dauchyd3a9a492021-01-25 17:29:03 +01002160 case ST_F_QCUR:
Willy Tarreaua0570452021-06-18 09:30:30 +02002161 metric = mkf_u32(0, sv->queue.length);
William Dauchyd3a9a492021-01-25 17:29:03 +01002162 break;
2163 case ST_F_QMAX:
2164 metric = mkf_u32(FN_MAX, sv->counters.nbpend_max);
2165 break;
2166 case ST_F_SCUR:
2167 metric = mkf_u32(0, sv->cur_sess);
2168 break;
2169 case ST_F_SMAX:
2170 metric = mkf_u32(FN_MAX, sv->counters.cur_sess_max);
2171 break;
2172 case ST_F_SLIM:
2173 if (sv->maxconn)
2174 metric = mkf_u32(FO_CONFIG|FN_LIMIT, sv->maxconn);
2175 break;
2176 case ST_F_SRV_ICUR:
2177 metric = mkf_u32(0, sv->curr_idle_conns);
2178 break;
2179 case ST_F_SRV_ILIM:
2180 if (sv->max_idle_conns != -1)
2181 metric = mkf_u32(FO_CONFIG|FN_LIMIT, sv->max_idle_conns);
2182 break;
2183 case ST_F_STOT:
2184 metric = mkf_u64(FN_COUNTER, sv->counters.cum_sess);
2185 break;
2186 case ST_F_BIN:
2187 metric = mkf_u64(FN_COUNTER, sv->counters.bytes_in);
2188 break;
2189 case ST_F_BOUT:
2190 metric = mkf_u64(FN_COUNTER, sv->counters.bytes_out);
2191 break;
2192 case ST_F_DRESP:
2193 metric = mkf_u64(FN_COUNTER, sv->counters.denied_resp);
2194 break;
2195 case ST_F_ECON:
2196 metric = mkf_u64(FN_COUNTER, sv->counters.failed_conns);
2197 break;
2198 case ST_F_ERESP:
2199 metric = mkf_u64(FN_COUNTER, sv->counters.failed_resp);
2200 break;
2201 case ST_F_WRETR:
2202 metric = mkf_u64(FN_COUNTER, sv->counters.retries);
2203 break;
2204 case ST_F_WREDIS:
2205 metric = mkf_u64(FN_COUNTER, sv->counters.redispatches);
2206 break;
2207 case ST_F_WREW:
2208 metric = mkf_u64(FN_COUNTER, sv->counters.failed_rewrites);
2209 break;
2210 case ST_F_EINT:
2211 metric = mkf_u64(FN_COUNTER, sv->counters.internal_errors);
2212 break;
2213 case ST_F_CONNECT:
2214 metric = mkf_u64(FN_COUNTER, sv->counters.connect);
2215 break;
2216 case ST_F_REUSE:
2217 metric = mkf_u64(FN_COUNTER, sv->counters.reuse);
2218 break;
2219 case ST_F_IDLE_CONN_CUR:
2220 metric = mkf_u32(0, sv->curr_idle_nb);
2221 break;
2222 case ST_F_SAFE_CONN_CUR:
2223 metric = mkf_u32(0, sv->curr_safe_nb);
2224 break;
2225 case ST_F_USED_CONN_CUR:
2226 metric = mkf_u32(0, sv->curr_used_conns);
2227 break;
2228 case ST_F_NEED_CONN_EST:
2229 metric = mkf_u32(0, sv->est_need_conns);
2230 break;
2231 case ST_F_STATUS:
2232 fld_status = chunk_newstr(out);
2233 if (sv->cur_admin & SRV_ADMF_RMAINT)
2234 chunk_appendf(out, "MAINT (resolution)");
2235 else if (sv->cur_admin & SRV_ADMF_IMAINT)
2236 chunk_appendf(out, "MAINT (via %s/%s)", via->proxy->id, via->id);
2237 else if (sv->cur_admin & SRV_ADMF_MAINT)
2238 chunk_appendf(out, "MAINT");
2239 else
2240 chunk_appendf(out,
2241 srv_hlt_st[state],
2242 (ref->cur_state != SRV_ST_STOPPED) ? (ref->check.health - ref->check.rise + 1) : (ref->check.health),
2243 (ref->cur_state != SRV_ST_STOPPED) ? (ref->check.fall) : (ref->check.rise));
William Lallemand74c24fb2016-11-21 17:18:36 +01002244
William Dauchyd3a9a492021-01-25 17:29:03 +01002245 metric = mkf_str(FO_STATUS, fld_status);
2246 break;
2247 case ST_F_LASTCHG:
2248 metric = mkf_u32(FN_AGE, now.tv_sec - sv->last_change);
2249 break;
2250 case ST_F_WEIGHT:
2251 metric = mkf_u32(FN_AVG, (sv->cur_eweight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv);
2252 break;
2253 case ST_F_UWEIGHT:
2254 metric = mkf_u32(FN_AVG, sv->uweight);
2255 break;
2256 case ST_F_ACT:
2257 metric = mkf_u32(FO_STATUS, (sv->flags & SRV_F_BACKUP) ? 0 : 1);
2258 break;
2259 case ST_F_BCK:
2260 metric = mkf_u32(FO_STATUS, (sv->flags & SRV_F_BACKUP) ? 1 : 0);
2261 break;
2262 case ST_F_CHKFAIL:
2263 if (sv->check.state & CHK_ST_ENABLED)
2264 metric = mkf_u64(FN_COUNTER, sv->counters.failed_checks);
2265 break;
2266 case ST_F_CHKDOWN:
2267 if (sv->check.state & CHK_ST_ENABLED)
2268 metric = mkf_u64(FN_COUNTER, sv->counters.down_trans);
2269 break;
2270 case ST_F_DOWNTIME:
2271 if (sv->check.state & CHK_ST_ENABLED)
2272 metric = mkf_u32(FN_COUNTER, srv_downtime(sv));
2273 break;
2274 case ST_F_QLIMIT:
2275 if (sv->maxqueue)
2276 metric = mkf_u32(FO_CONFIG|FS_SERVICE, sv->maxqueue);
2277 break;
2278 case ST_F_PID:
Willy Tarreaue8422bf2021-06-15 09:08:18 +02002279 metric = mkf_u32(FO_KEY, 1);
William Dauchyd3a9a492021-01-25 17:29:03 +01002280 break;
2281 case ST_F_IID:
2282 metric = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
2283 break;
2284 case ST_F_SID:
2285 metric = mkf_u32(FO_KEY|FS_SERVICE, sv->puid);
2286 break;
Aurelien DARRAGON745ce8e2022-11-17 16:34:07 +01002287 case ST_F_SRID:
2288 metric = mkf_u32(FN_COUNTER, sv->rid);
2289 break;
William Dauchyd3a9a492021-01-25 17:29:03 +01002290 case ST_F_THROTTLE:
2291 if (sv->cur_state == SRV_ST_STARTING && !server_is_draining(sv))
2292 metric = mkf_u32(FN_AVG, server_throttle_rate(sv));
2293 break;
2294 case ST_F_LBTOT:
2295 metric = mkf_u64(FN_COUNTER, sv->counters.cum_lbconn);
2296 break;
2297 case ST_F_TRACKED:
2298 if (sv->track) {
2299 char *fld_track = chunk_newstr(out);
2300 chunk_appendf(out, "%s/%s", sv->track->proxy->id, sv->track->id);
2301 metric = mkf_str(FO_CONFIG|FN_NAME|FS_SERVICE, fld_track);
2302 }
2303 break;
2304 case ST_F_TYPE:
2305 metric = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_SV);
2306 break;
2307 case ST_F_RATE:
2308 metric = mkf_u32(FN_RATE, read_freq_ctr(&sv->sess_per_sec));
2309 break;
2310 case ST_F_RATE_MAX:
2311 metric = mkf_u32(FN_MAX, sv->counters.sps_max);
2312 break;
2313 case ST_F_CHECK_STATUS:
2314 if ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED) {
2315 const char *fld_chksts;
Marcin Deraneka8dbdf32020-05-15 20:02:40 +02002316
William Dauchyd3a9a492021-01-25 17:29:03 +01002317 fld_chksts = chunk_newstr(out);
2318 chunk_strcat(out, "* "); // for check in progress
2319 chunk_strcat(out, get_check_status_info(sv->check.status));
2320 if (!(sv->check.state & CHK_ST_INPROGRESS))
2321 fld_chksts += 2; // skip "* "
Willy Tarreau973a9372021-05-12 17:29:14 +02002322 metric = mkf_str(FN_OUTPUT, fld_chksts);
William Dauchyd3a9a492021-01-25 17:29:03 +01002323 }
2324 break;
2325 case ST_F_CHECK_CODE:
2326 if ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED &&
2327 sv->check.status >= HCHK_STATUS_L57DATA)
2328 metric = mkf_u32(FN_OUTPUT, sv->check.code);
2329 break;
2330 case ST_F_CHECK_DURATION:
2331 if ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED &&
2332 sv->check.status >= HCHK_STATUS_CHECKED)
2333 metric = mkf_u64(FN_DURATION, sv->check.duration);
2334 break;
2335 case ST_F_CHECK_DESC:
2336 if ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED)
2337 metric = mkf_str(FN_OUTPUT, get_check_status_description(sv->check.status));
2338 break;
2339 case ST_F_LAST_CHK:
2340 if ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED)
2341 metric = mkf_str(FN_OUTPUT, sv->check.desc);
2342 break;
2343 case ST_F_CHECK_RISE:
2344 if ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED)
2345 metric = mkf_u32(FO_CONFIG|FS_SERVICE, ref->check.rise);
2346 break;
2347 case ST_F_CHECK_FALL:
2348 if ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED)
2349 metric = mkf_u32(FO_CONFIG|FS_SERVICE, ref->check.fall);
2350 break;
2351 case ST_F_CHECK_HEALTH:
2352 if ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED)
2353 metric = mkf_u32(FO_CONFIG|FS_SERVICE, ref->check.health);
2354 break;
Christopher Faulet59bab612021-07-22 08:04:38 +02002355 case ST_F_AGENT_STATUS:
2356 if ((sv->agent.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED) {
2357 const char *fld_chksts;
2358
2359 fld_chksts = chunk_newstr(out);
2360 chunk_strcat(out, "* "); // for check in progress
2361 chunk_strcat(out, get_check_status_info(sv->agent.status));
2362 if (!(sv->agent.state & CHK_ST_INPROGRESS))
2363 fld_chksts += 2; // skip "* "
2364 metric = mkf_str(FN_OUTPUT, fld_chksts);
2365 }
2366 break;
2367 case ST_F_AGENT_CODE:
2368 if ((sv->agent.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED &&
2369 (sv->agent.status >= HCHK_STATUS_L57DATA))
2370 metric = mkf_u32(FN_OUTPUT, sv->agent.code);
2371 break;
2372 case ST_F_AGENT_DURATION:
2373 if ((sv->agent.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED)
2374 metric = mkf_u64(FN_DURATION, sv->agent.duration);
2375 break;
2376 case ST_F_AGENT_DESC:
2377 if ((sv->agent.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED)
2378 metric = mkf_str(FN_OUTPUT, get_check_status_description(sv->agent.status));
2379 break;
2380 case ST_F_LAST_AGT:
2381 if ((sv->agent.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED)
2382 metric = mkf_str(FN_OUTPUT, sv->agent.desc);
2383 break;
2384 case ST_F_AGENT_RISE:
2385 if ((sv->agent.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED)
2386 metric = mkf_u32(FO_CONFIG|FS_SERVICE, sv->agent.rise);
2387 break;
2388 case ST_F_AGENT_FALL:
2389 if ((sv->agent.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED)
2390 metric = mkf_u32(FO_CONFIG|FS_SERVICE, sv->agent.fall);
2391 break;
2392 case ST_F_AGENT_HEALTH:
2393 if ((sv->agent.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED)
2394 metric = mkf_u32(FO_CONFIG|FS_SERVICE, sv->agent.health);
2395 break;
William Dauchyd3a9a492021-01-25 17:29:03 +01002396 case ST_F_REQ_TOT:
2397 if (px->mode == PR_MODE_HTTP)
2398 metric = mkf_u64(FN_COUNTER, sv->counters.p.http.cum_req);
2399 break;
2400 case ST_F_HRSP_1XX:
2401 if (px->mode == PR_MODE_HTTP)
2402 metric = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[1]);
2403 break;
2404 case ST_F_HRSP_2XX:
2405 if (px->mode == PR_MODE_HTTP)
2406 metric = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[2]);
2407 break;
2408 case ST_F_HRSP_3XX:
2409 if (px->mode == PR_MODE_HTTP)
2410 metric = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[3]);
2411 break;
2412 case ST_F_HRSP_4XX:
2413 if (px->mode == PR_MODE_HTTP)
2414 metric = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[4]);
2415 break;
2416 case ST_F_HRSP_5XX:
2417 if (px->mode == PR_MODE_HTTP)
2418 metric = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[5]);
2419 break;
2420 case ST_F_HRSP_OTHER:
2421 if (px->mode == PR_MODE_HTTP)
2422 metric = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[0]);
2423 break;
2424 case ST_F_HANAFAIL:
2425 if (ref->observe)
2426 metric = mkf_u64(FN_COUNTER, sv->counters.failed_hana);
2427 break;
2428 case ST_F_CLI_ABRT:
2429 metric = mkf_u64(FN_COUNTER, sv->counters.cli_aborts);
2430 break;
2431 case ST_F_SRV_ABRT:
2432 metric = mkf_u64(FN_COUNTER, sv->counters.srv_aborts);
2433 break;
2434 case ST_F_LASTSESS:
2435 metric = mkf_s32(FN_AGE, srv_lastsession(sv));
2436 break;
2437 case ST_F_QTIME:
2438 metric = mkf_u32(FN_AVG, swrate_avg(sv->counters.q_time, srv_samples_window));
2439 break;
2440 case ST_F_CTIME:
2441 metric = mkf_u32(FN_AVG, swrate_avg(sv->counters.c_time, srv_samples_window));
2442 break;
2443 case ST_F_RTIME:
2444 metric = mkf_u32(FN_AVG, swrate_avg(sv->counters.d_time, srv_samples_window));
2445 break;
2446 case ST_F_TTIME:
2447 metric = mkf_u32(FN_AVG, swrate_avg(sv->counters.t_time, srv_samples_window));
2448 break;
2449 case ST_F_QT_MAX:
2450 metric = mkf_u32(FN_MAX, sv->counters.qtime_max);
2451 break;
2452 case ST_F_CT_MAX:
2453 metric = mkf_u32(FN_MAX, sv->counters.ctime_max);
2454 break;
2455 case ST_F_RT_MAX:
2456 metric = mkf_u32(FN_MAX, sv->counters.dtime_max);
2457 break;
2458 case ST_F_TT_MAX:
2459 metric = mkf_u32(FN_MAX, sv->counters.ttime_max);
2460 break;
2461 case ST_F_ADDR:
2462 if (flags & STAT_SHLGNDS) {
2463 switch (addr_to_str(&sv->addr, str, sizeof(str))) {
2464 case AF_INET:
2465 metric = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
2466 chunk_appendf(out, "%s:%d", str, sv->svc_port);
2467 break;
2468 case AF_INET6:
2469 metric = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
2470 chunk_appendf(out, "[%s]:%d", str, sv->svc_port);
2471 break;
2472 case AF_UNIX:
2473 metric = mkf_str(FO_CONFIG|FS_SERVICE, "unix");
2474 break;
2475 case -1:
2476 metric = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
2477 chunk_strcat(out, strerror(errno));
2478 break;
2479 default: /* address family not supported */
2480 break;
2481 }
2482 }
2483 break;
2484 case ST_F_COOKIE:
2485 if (flags & STAT_SHLGNDS && sv->cookie)
2486 metric = mkf_str(FO_CONFIG|FN_NAME|FS_SERVICE, sv->cookie);
2487 break;
2488 default:
2489 /* not used for servers. If a specific metric
2490 * is requested, return an error. Otherwise continue.
2491 */
2492 if (selected_field != NULL)
2493 return 0;
2494 continue;
William Lallemand74c24fb2016-11-21 17:18:36 +01002495 }
William Dauchyd3a9a492021-01-25 17:29:03 +01002496 stats[current_field] = metric;
2497 if (selected_field != NULL)
2498 break;
William Lallemand74c24fb2016-11-21 17:18:36 +01002499 }
William Lallemand74c24fb2016-11-21 17:18:36 +01002500 return 1;
2501}
2502
2503/* Dumps a line for server <sv> and proxy <px> to the trash and uses the state
Willy Tarreaucaff6312022-05-27 10:17:46 +02002504 * from stream connector <sc>, and server state <state>. The caller is
Willy Tarreau43241ff2019-10-09 11:27:51 +02002505 * responsible for clearing the trash if needed. Returns non-zero if it emits
2506 * anything, zero otherwise.
William Lallemand74c24fb2016-11-21 17:18:36 +01002507 */
Willy Tarreaucaff6312022-05-27 10:17:46 +02002508static int stats_dump_sv_stats(struct stconn *sc, struct proxy *px, struct server *sv)
William Lallemand74c24fb2016-11-21 17:18:36 +01002509{
Willy Tarreaucaff6312022-05-27 10:17:46 +02002510 struct appctx *appctx = __sc_appctx(sc);
Willy Tarreau91cefca2022-05-03 17:08:29 +02002511 struct show_stat_ctx *ctx = appctx->svcctx;
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02002512 struct stats_module *mod;
2513 struct field *stats = stat_l[STATS_DOMAIN_PROXY];
2514 size_t stats_count = ST_F_TOTAL_FIELDS;
2515
2516 memset(stats, 0, sizeof(struct field) * stat_count[STATS_DOMAIN_PROXY]);
William Lallemand74c24fb2016-11-21 17:18:36 +01002517
Willy Tarreau91cefca2022-05-03 17:08:29 +02002518 if (!stats_fill_sv_stats(px, sv, ctx->flags, stats,
William Dauchyd3a9a492021-01-25 17:29:03 +01002519 ST_F_TOTAL_FIELDS, NULL))
William Lallemand74c24fb2016-11-21 17:18:36 +01002520 return 0;
2521
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02002522 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
2523 void *counters;
2524
2525 if (stats_get_domain(mod->domain_flags) != STATS_DOMAIN_PROXY)
2526 continue;
2527
2528 if (!(stats_px_get_cap(mod->domain_flags) & STATS_PX_CAP_SRV)) {
2529 stats_count += mod->stats_count;
2530 continue;
2531 }
2532
2533 counters = EXTRA_COUNTERS_GET(sv->extra_counters, mod);
2534 mod->fill_stats(counters, stats + stats_count);
2535 stats_count += mod->stats_count;
2536 }
2537
2538 return stats_dump_one_line(stats, stats_count, appctx);
William Lallemand74c24fb2016-11-21 17:18:36 +01002539}
2540
William Dauchyda3b4662021-01-25 17:29:01 +01002541/* Helper to compute srv values for a given backend
William Lallemand74c24fb2016-11-21 17:18:36 +01002542 */
William Dauchyda3b4662021-01-25 17:29:01 +01002543static void stats_fill_be_stats_computesrv(struct proxy *px, int *nbup, int *nbsrv, int *totuw)
William Lallemand74c24fb2016-11-21 17:18:36 +01002544{
William Dauchyda3b4662021-01-25 17:29:01 +01002545 int nbup_tmp, nbsrv_tmp, totuw_tmp;
Willy Tarreau2fbe6942020-10-23 18:02:54 +02002546 const struct server *srv;
William Lallemand74c24fb2016-11-21 17:18:36 +01002547
William Dauchyda3b4662021-01-25 17:29:01 +01002548 nbup_tmp = nbsrv_tmp = totuw_tmp = 0;
Willy Tarreaubd715102020-10-23 22:44:30 +02002549 for (srv = px->srv; srv; srv = srv->next) {
2550 if (srv->cur_state != SRV_ST_STOPPED) {
William Dauchyda3b4662021-01-25 17:29:01 +01002551 nbup_tmp++;
Willy Tarreaubd715102020-10-23 22:44:30 +02002552 if (srv_currently_usable(srv) &&
2553 (!px->srv_act ^ !(srv->flags & SRV_F_BACKUP)))
William Dauchyda3b4662021-01-25 17:29:01 +01002554 totuw_tmp += srv->uweight;
Willy Tarreau2fbe6942020-10-23 18:02:54 +02002555 }
William Dauchyda3b4662021-01-25 17:29:01 +01002556 nbsrv_tmp++;
Willy Tarreau2fbe6942020-10-23 18:02:54 +02002557 }
2558
Willy Tarreaubd715102020-10-23 22:44:30 +02002559 HA_RWLOCK_RDLOCK(LBPRM_LOCK, &px->lbprm.lock);
2560 if (!px->srv_act && px->lbprm.fbck)
William Dauchyda3b4662021-01-25 17:29:01 +01002561 totuw_tmp = px->lbprm.fbck->uweight;
Willy Tarreaubd715102020-10-23 22:44:30 +02002562 HA_RWLOCK_RDUNLOCK(LBPRM_LOCK, &px->lbprm.lock);
2563
William Dauchyda3b4662021-01-25 17:29:01 +01002564 /* use tmp variable then assign result to make gcc happy */
2565 *nbup = nbup_tmp;
2566 *nbsrv = nbsrv_tmp;
2567 *totuw = totuw_tmp;
2568}
Willy Tarreau2fbe6942020-10-23 18:02:54 +02002569
William Dauchyda3b4662021-01-25 17:29:01 +01002570/* Fill <stats> with the backend statistics. <stats> is preallocated array of
2571 * length <len>. If <selected_field> is != NULL, only fill this one. The length
2572 * of the array must be at least ST_F_TOTAL_FIELDS. If this length is less than
2573 * this value, or if the selected field is not implemented for backends, the
2574 * function returns 0, otherwise, it returns 1. <flags> can take the value
2575 * STAT_SHLGNDS.
2576 */
2577int stats_fill_be_stats(struct proxy *px, int flags, struct field *stats, int len,
2578 enum stat_field *selected_field)
2579{
2580 enum stat_field current_field = (selected_field != NULL ? *selected_field : 0);
2581 long long be_samples_counter;
2582 unsigned int be_samples_window = TIME_STATS_SAMPLES;
2583 struct buffer *out = get_trash_chunk();
2584 int nbup, nbsrv, totuw;
2585 char *fld;
William Lallemand74c24fb2016-11-21 17:18:36 +01002586
William Dauchyda3b4662021-01-25 17:29:01 +01002587 if (len < ST_F_TOTAL_FIELDS)
2588 return 0;
William Lallemand74c24fb2016-11-21 17:18:36 +01002589
William Dauchyda3b4662021-01-25 17:29:01 +01002590 nbup = nbsrv = totuw = 0;
2591 /* some srv values compute for later if we either select all fields or
2592 * need them for one of the mentioned ones */
2593 if (selected_field == NULL || *selected_field == ST_F_STATUS ||
2594 *selected_field == ST_F_UWEIGHT)
2595 stats_fill_be_stats_computesrv(px, &nbup, &nbsrv, &totuw);
William Lallemand74c24fb2016-11-21 17:18:36 +01002596
William Dauchyda3b4662021-01-25 17:29:01 +01002597 /* same here but specific to time fields */
2598 if (selected_field == NULL || *selected_field == ST_F_QTIME ||
2599 *selected_field == ST_F_CTIME || *selected_field == ST_F_RTIME ||
2600 *selected_field == ST_F_TTIME) {
2601 be_samples_counter = (px->mode == PR_MODE_HTTP) ? px->be_counters.p.http.cum_req : px->be_counters.cum_lbconn;
2602 if (be_samples_counter < TIME_STATS_SAMPLES && be_samples_counter > 0)
2603 be_samples_window = be_samples_counter;
William Lallemand74c24fb2016-11-21 17:18:36 +01002604 }
2605
William Dauchyda3b4662021-01-25 17:29:01 +01002606 for (; current_field < ST_F_TOTAL_FIELDS; current_field++) {
2607 struct field metric = { 0 };
William Lallemand74c24fb2016-11-21 17:18:36 +01002608
William Dauchyda3b4662021-01-25 17:29:01 +01002609 switch (current_field) {
2610 case ST_F_PXNAME:
2611 metric = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
2612 break;
2613 case ST_F_SVNAME:
2614 metric = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, "BACKEND");
2615 break;
2616 case ST_F_MODE:
2617 metric = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
2618 break;
2619 case ST_F_QCUR:
Willy Tarreau7f3c1df2021-06-18 09:22:21 +02002620 metric = mkf_u32(0, px->queue.length);
William Dauchyda3b4662021-01-25 17:29:01 +01002621 break;
2622 case ST_F_QMAX:
2623 metric = mkf_u32(FN_MAX, px->be_counters.nbpend_max);
2624 break;
2625 case ST_F_SCUR:
2626 metric = mkf_u32(0, px->beconn);
2627 break;
2628 case ST_F_SMAX:
2629 metric = mkf_u32(FN_MAX, px->be_counters.conn_max);
2630 break;
2631 case ST_F_SLIM:
2632 metric = mkf_u32(FO_CONFIG|FN_LIMIT, px->fullconn);
2633 break;
2634 case ST_F_STOT:
2635 metric = mkf_u64(FN_COUNTER, px->be_counters.cum_conn);
2636 break;
2637 case ST_F_BIN:
2638 metric = mkf_u64(FN_COUNTER, px->be_counters.bytes_in);
2639 break;
2640 case ST_F_BOUT:
2641 metric = mkf_u64(FN_COUNTER, px->be_counters.bytes_out);
2642 break;
2643 case ST_F_DREQ:
2644 metric = mkf_u64(FN_COUNTER, px->be_counters.denied_req);
2645 break;
2646 case ST_F_DRESP:
2647 metric = mkf_u64(FN_COUNTER, px->be_counters.denied_resp);
2648 break;
2649 case ST_F_ECON:
2650 metric = mkf_u64(FN_COUNTER, px->be_counters.failed_conns);
2651 break;
2652 case ST_F_ERESP:
2653 metric = mkf_u64(FN_COUNTER, px->be_counters.failed_resp);
2654 break;
2655 case ST_F_WRETR:
2656 metric = mkf_u64(FN_COUNTER, px->be_counters.retries);
2657 break;
2658 case ST_F_WREDIS:
2659 metric = mkf_u64(FN_COUNTER, px->be_counters.redispatches);
2660 break;
2661 case ST_F_WREW:
2662 metric = mkf_u64(FN_COUNTER, px->be_counters.failed_rewrites);
2663 break;
2664 case ST_F_EINT:
2665 metric = mkf_u64(FN_COUNTER, px->be_counters.internal_errors);
2666 break;
2667 case ST_F_CONNECT:
2668 metric = mkf_u64(FN_COUNTER, px->be_counters.connect);
2669 break;
2670 case ST_F_REUSE:
2671 metric = mkf_u64(FN_COUNTER, px->be_counters.reuse);
2672 break;
2673 case ST_F_STATUS:
2674 fld = chunk_newstr(out);
2675 chunk_appendf(out, "%s", (px->lbprm.tot_weight > 0 || !px->srv) ? "UP" : "DOWN");
2676 if (flags & (STAT_HIDE_MAINT|STAT_HIDE_DOWN))
2677 chunk_appendf(out, " (%d/%d)", nbup, nbsrv);
2678 metric = mkf_str(FO_STATUS, fld);
2679 break;
William Dauchy42d7c402021-11-07 10:18:47 +01002680 case ST_F_AGG_SRV_CHECK_STATUS:
2681 metric = mkf_u32(FN_GAUGE, 0);
2682 break;
William Dauchyda3b4662021-01-25 17:29:01 +01002683 case ST_F_WEIGHT:
2684 metric = mkf_u32(FN_AVG, (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv);
2685 break;
2686 case ST_F_UWEIGHT:
2687 metric = mkf_u32(FN_AVG, totuw);
2688 break;
2689 case ST_F_ACT:
2690 metric = mkf_u32(0, px->srv_act);
2691 break;
2692 case ST_F_BCK:
2693 metric = mkf_u32(0, px->srv_bck);
2694 break;
2695 case ST_F_CHKDOWN:
2696 metric = mkf_u64(FN_COUNTER, px->down_trans);
2697 break;
2698 case ST_F_LASTCHG:
2699 metric = mkf_u32(FN_AGE, now.tv_sec - px->last_change);
2700 break;
2701 case ST_F_DOWNTIME:
2702 if (px->srv)
2703 metric = mkf_u32(FN_COUNTER, be_downtime(px));
2704 break;
2705 case ST_F_PID:
Willy Tarreaue8422bf2021-06-15 09:08:18 +02002706 metric = mkf_u32(FO_KEY, 1);
William Dauchyda3b4662021-01-25 17:29:01 +01002707 break;
2708 case ST_F_IID:
2709 metric = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
2710 break;
2711 case ST_F_SID:
2712 metric = mkf_u32(FO_KEY|FS_SERVICE, 0);
2713 break;
2714 case ST_F_LBTOT:
2715 metric = mkf_u64(FN_COUNTER, px->be_counters.cum_lbconn);
2716 break;
2717 case ST_F_TYPE:
2718 metric = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_BE);
2719 break;
2720 case ST_F_RATE:
2721 metric = mkf_u32(0, read_freq_ctr(&px->be_sess_per_sec));
2722 break;
2723 case ST_F_RATE_MAX:
2724 metric = mkf_u32(0, px->be_counters.sps_max);
2725 break;
2726 case ST_F_COOKIE:
2727 if (flags & STAT_SHLGNDS && px->cookie_name)
2728 metric = mkf_str(FO_CONFIG|FN_NAME|FS_SERVICE, px->cookie_name);
2729 break;
2730 case ST_F_ALGO:
2731 if (flags & STAT_SHLGNDS)
2732 metric = mkf_str(FO_CONFIG|FS_SERVICE, backend_lb_algo_str(px->lbprm.algo & BE_LB_ALGO));
2733 break;
2734 case ST_F_REQ_TOT:
2735 if (px->mode == PR_MODE_HTTP)
2736 metric = mkf_u64(FN_COUNTER, px->be_counters.p.http.cum_req);
2737 break;
2738 case ST_F_HRSP_1XX:
2739 if (px->mode == PR_MODE_HTTP)
2740 metric = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[1]);
2741 break;
2742 case ST_F_HRSP_2XX:
2743 if (px->mode == PR_MODE_HTTP)
2744 metric = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[2]);
2745 break;
2746 case ST_F_HRSP_3XX:
2747 if (px->mode == PR_MODE_HTTP)
2748 metric = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[3]);
2749 break;
2750 case ST_F_HRSP_4XX:
2751 if (px->mode == PR_MODE_HTTP)
2752 metric = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[4]);
2753 break;
2754 case ST_F_HRSP_5XX:
2755 if (px->mode == PR_MODE_HTTP)
2756 metric = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[5]);
2757 break;
2758 case ST_F_HRSP_OTHER:
2759 if (px->mode == PR_MODE_HTTP)
2760 metric = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[0]);
2761 break;
2762 case ST_F_CACHE_LOOKUPS:
2763 if (px->mode == PR_MODE_HTTP)
2764 metric = mkf_u64(FN_COUNTER, px->be_counters.p.http.cache_lookups);
2765 break;
2766 case ST_F_CACHE_HITS:
2767 if (px->mode == PR_MODE_HTTP)
2768 metric = mkf_u64(FN_COUNTER, px->be_counters.p.http.cache_hits);
2769 break;
2770 case ST_F_CLI_ABRT:
2771 metric = mkf_u64(FN_COUNTER, px->be_counters.cli_aborts);
2772 break;
2773 case ST_F_SRV_ABRT:
2774 metric = mkf_u64(FN_COUNTER, px->be_counters.srv_aborts);
2775 break;
2776 case ST_F_COMP_IN:
2777 metric = mkf_u64(FN_COUNTER, px->be_counters.comp_in);
2778 break;
2779 case ST_F_COMP_OUT:
2780 metric = mkf_u64(FN_COUNTER, px->be_counters.comp_out);
2781 break;
2782 case ST_F_COMP_BYP:
2783 metric = mkf_u64(FN_COUNTER, px->be_counters.comp_byp);
2784 break;
2785 case ST_F_COMP_RSP:
2786 metric = mkf_u64(FN_COUNTER, px->be_counters.p.http.comp_rsp);
2787 break;
2788 case ST_F_LASTSESS:
2789 metric = mkf_s32(FN_AGE, be_lastsession(px));
2790 break;
2791 case ST_F_QTIME:
2792 metric = mkf_u32(FN_AVG, swrate_avg(px->be_counters.q_time, be_samples_window));
2793 break;
2794 case ST_F_CTIME:
2795 metric = mkf_u32(FN_AVG, swrate_avg(px->be_counters.c_time, be_samples_window));
2796 break;
2797 case ST_F_RTIME:
2798 metric = mkf_u32(FN_AVG, swrate_avg(px->be_counters.d_time, be_samples_window));
2799 break;
2800 case ST_F_TTIME:
2801 metric = mkf_u32(FN_AVG, swrate_avg(px->be_counters.t_time, be_samples_window));
2802 break;
2803 case ST_F_QT_MAX:
2804 metric = mkf_u32(FN_MAX, px->be_counters.qtime_max);
2805 break;
2806 case ST_F_CT_MAX:
2807 metric = mkf_u32(FN_MAX, px->be_counters.ctime_max);
2808 break;
2809 case ST_F_RT_MAX:
2810 metric = mkf_u32(FN_MAX, px->be_counters.dtime_max);
2811 break;
2812 case ST_F_TT_MAX:
2813 metric = mkf_u32(FN_MAX, px->be_counters.ttime_max);
2814 break;
2815 default:
2816 /* not used for backends. If a specific metric
2817 * is requested, return an error. Otherwise continue.
2818 */
2819 if (selected_field != NULL)
2820 return 0;
2821 continue;
2822 }
2823 stats[current_field] = metric;
2824 if (selected_field != NULL)
2825 break;
2826 }
William Lallemand74c24fb2016-11-21 17:18:36 +01002827 return 1;
2828}
2829
2830/* Dumps a line for backend <px> to the trash for and uses the state from stream
Willy Tarreau43241ff2019-10-09 11:27:51 +02002831 * interface <si>. The caller is responsible for clearing the trash if needed.
2832 * Returns non-zero if it emits anything, zero otherwise.
William Lallemand74c24fb2016-11-21 17:18:36 +01002833 */
Willy Tarreaucaff6312022-05-27 10:17:46 +02002834static int stats_dump_be_stats(struct stconn *sc, struct proxy *px)
William Lallemand74c24fb2016-11-21 17:18:36 +01002835{
Willy Tarreaucaff6312022-05-27 10:17:46 +02002836 struct appctx *appctx = __sc_appctx(sc);
Willy Tarreau91cefca2022-05-03 17:08:29 +02002837 struct show_stat_ctx *ctx = appctx->svcctx;
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02002838 struct field *stats = stat_l[STATS_DOMAIN_PROXY];
2839 struct stats_module *mod;
2840 size_t stats_count = ST_F_TOTAL_FIELDS;
William Lallemand74c24fb2016-11-21 17:18:36 +01002841
2842 if (!(px->cap & PR_CAP_BE))
2843 return 0;
2844
Willy Tarreau91cefca2022-05-03 17:08:29 +02002845 if ((ctx->flags & STAT_BOUND) && !(ctx->type & (1 << STATS_TYPE_BE)))
William Lallemand74c24fb2016-11-21 17:18:36 +01002846 return 0;
2847
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02002848 memset(stats, 0, sizeof(struct field) * stat_count[STATS_DOMAIN_PROXY]);
2849
Willy Tarreau91cefca2022-05-03 17:08:29 +02002850 if (!stats_fill_be_stats(px, ctx->flags, stats, ST_F_TOTAL_FIELDS, NULL))
William Lallemand74c24fb2016-11-21 17:18:36 +01002851 return 0;
2852
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02002853 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
2854 struct extra_counters *counters;
2855
2856 if (stats_get_domain(mod->domain_flags) != STATS_DOMAIN_PROXY)
2857 continue;
2858
2859 if (!(stats_px_get_cap(mod->domain_flags) & STATS_PX_CAP_BE)) {
2860 stats_count += mod->stats_count;
2861 continue;
2862 }
2863
2864 counters = EXTRA_COUNTERS_GET(px->extra_counters_be, mod);
2865 mod->fill_stats(counters, stats + stats_count);
2866 stats_count += mod->stats_count;
2867 }
2868
2869 return stats_dump_one_line(stats, stats_count, appctx);
William Lallemand74c24fb2016-11-21 17:18:36 +01002870}
2871
2872/* Dumps the HTML table header for proxy <px> to the trash for and uses the state from
Willy Tarreaucaff6312022-05-27 10:17:46 +02002873 * stream connector <sc> and per-uri parameters <uri>. The caller is responsible
William Lallemand74c24fb2016-11-21 17:18:36 +01002874 * for clearing the trash if needed.
2875 */
Willy Tarreaucaff6312022-05-27 10:17:46 +02002876static void stats_dump_html_px_hdr(struct stconn *sc, struct proxy *px)
William Lallemand74c24fb2016-11-21 17:18:36 +01002877{
Willy Tarreaucaff6312022-05-27 10:17:46 +02002878 struct appctx *appctx = __sc_appctx(sc);
Willy Tarreau91cefca2022-05-03 17:08:29 +02002879 struct show_stat_ctx *ctx = appctx->svcctx;
William Lallemand74c24fb2016-11-21 17:18:36 +01002880 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02002881 struct stats_module *mod;
2882 int stats_module_len = 0;
William Lallemand74c24fb2016-11-21 17:18:36 +01002883
Willy Tarreau91cefca2022-05-03 17:08:29 +02002884 if (px->cap & PR_CAP_BE && px->srv && (ctx->flags & STAT_ADMIN)) {
William Lallemand74c24fb2016-11-21 17:18:36 +01002885 /* A form to enable/disable this proxy servers */
2886
Willy Tarreau91cefca2022-05-03 17:08:29 +02002887 /* scope_txt = search pattern + search query, ctx->scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
William Lallemand74c24fb2016-11-21 17:18:36 +01002888 scope_txt[0] = 0;
Willy Tarreau91cefca2022-05-03 17:08:29 +02002889 if (ctx->scope_len) {
Willy Tarreaucaff6312022-05-27 10:17:46 +02002890 const char *scope_ptr = stats_scope_ptr(appctx, sc);
Christopher Fauleted7a0662019-01-14 11:07:34 +01002891
William Lallemand74c24fb2016-11-21 17:18:36 +01002892 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Willy Tarreau91cefca2022-05-03 17:08:29 +02002893 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), scope_ptr, ctx->scope_len);
2894 scope_txt[strlen(STAT_SCOPE_PATTERN) + ctx->scope_len] = 0;
William Lallemand74c24fb2016-11-21 17:18:36 +01002895 }
2896
2897 chunk_appendf(&trash,
2898 "<form method=\"post\">");
2899 }
2900
2901 /* print a new table */
2902 chunk_appendf(&trash,
2903 "<table class=\"tbl\" width=\"100%%\">\n"
2904 "<tr class=\"titre\">"
2905 "<th class=\"pxname\" width=\"10%%\">");
2906
2907 chunk_appendf(&trash,
2908 "<a name=\"%s\"></a>%s"
2909 "<a class=px href=\"#%s\">%s</a>",
2910 px->id,
Willy Tarreau91cefca2022-05-03 17:08:29 +02002911 (ctx->flags & STAT_SHLGNDS) ? "<u>":"",
William Lallemand74c24fb2016-11-21 17:18:36 +01002912 px->id, px->id);
2913
Willy Tarreau91cefca2022-05-03 17:08:29 +02002914 if (ctx->flags & STAT_SHLGNDS) {
William Lallemand74c24fb2016-11-21 17:18:36 +01002915 /* cap, mode, id */
2916 chunk_appendf(&trash, "<div class=tips>cap: %s, mode: %s, id: %d",
2917 proxy_cap_str(px->cap), proxy_mode_str(px->mode),
2918 px->uuid);
2919 chunk_appendf(&trash, "</div>");
2920 }
2921
2922 chunk_appendf(&trash,
2923 "%s</th>"
2924 "<th class=\"%s\" width=\"90%%\">%s</th>"
2925 "</tr>\n"
2926 "</table>\n"
2927 "<table class=\"tbl\" width=\"100%%\">\n"
2928 "<tr class=\"titre\">",
Willy Tarreau91cefca2022-05-03 17:08:29 +02002929 (ctx->flags & STAT_SHLGNDS) ? "</u>":"",
William Lallemand74c24fb2016-11-21 17:18:36 +01002930 px->desc ? "desc" : "empty", px->desc ? px->desc : "");
2931
Willy Tarreau91cefca2022-05-03 17:08:29 +02002932 if (ctx->flags & STAT_ADMIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +01002933 /* Column heading for Enable or Disable server */
Christopher Fauletbc271ec2019-12-02 11:29:04 +01002934 if ((px->cap & PR_CAP_BE) && px->srv)
2935 chunk_appendf(&trash,
2936 "<th rowspan=2 width=1><input type=\"checkbox\" "
2937 "onclick=\"for(c in document.getElementsByClassName('%s-checkbox')) "
2938 "document.getElementsByClassName('%s-checkbox').item(c).checked = this.checked\"></th>",
2939 px->id,
2940 px->id);
2941 else
2942 chunk_appendf(&trash, "<th rowspan=2></th>");
William Lallemand74c24fb2016-11-21 17:18:36 +01002943 }
2944
2945 chunk_appendf(&trash,
2946 "<th rowspan=2></th>"
2947 "<th colspan=3>Queue</th>"
2948 "<th colspan=3>Session rate</th><th colspan=6>Sessions</th>"
2949 "<th colspan=2>Bytes</th><th colspan=2>Denied</th>"
2950 "<th colspan=3>Errors</th><th colspan=2>Warnings</th>"
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02002951 "<th colspan=9>Server</th>");
2952
Willy Tarreau91cefca2022-05-03 17:08:29 +02002953 if (ctx->flags & STAT_SHMODULES) {
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02002954 // calculate the count of module for colspan attribute
2955 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
2956 ++stats_module_len;
2957 }
2958 chunk_appendf(&trash, "<th colspan=%d>Extra modules</th>",
2959 stats_module_len);
2960 }
2961
2962 chunk_appendf(&trash,
William Lallemand74c24fb2016-11-21 17:18:36 +01002963 "</tr>\n"
2964 "<tr class=\"titre\">"
2965 "<th>Cur</th><th>Max</th><th>Limit</th>"
2966 "<th>Cur</th><th>Max</th><th>Limit</th><th>Cur</th><th>Max</th>"
2967 "<th>Limit</th><th>Total</th><th>LbTot</th><th>Last</th><th>In</th><th>Out</th>"
2968 "<th>Req</th><th>Resp</th><th>Req</th><th>Conn</th>"
2969 "<th>Resp</th><th>Retr</th><th>Redis</th>"
2970 "<th>Status</th><th>LastChk</th><th>Wght</th><th>Act</th>"
2971 "<th>Bck</th><th>Chk</th><th>Dwn</th><th>Dwntme</th>"
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02002972 "<th>Thrtle</th>\n");
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02002973
Willy Tarreau91cefca2022-05-03 17:08:29 +02002974 if (ctx->flags & STAT_SHMODULES) {
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02002975 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
2976 chunk_appendf(&trash, "<th>%s</th>", mod->name);
2977 }
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02002978 }
2979
2980 chunk_appendf(&trash, "</tr>");
William Lallemand74c24fb2016-11-21 17:18:36 +01002981}
2982
2983/* Dumps the HTML table trailer for proxy <px> to the trash for and uses the state from
Willy Tarreaucaff6312022-05-27 10:17:46 +02002984 * stream connector <sc>. The caller is responsible for clearing the trash if needed.
William Lallemand74c24fb2016-11-21 17:18:36 +01002985 */
Willy Tarreaucaff6312022-05-27 10:17:46 +02002986static void stats_dump_html_px_end(struct stconn *sc, struct proxy *px)
William Lallemand74c24fb2016-11-21 17:18:36 +01002987{
Willy Tarreaucaff6312022-05-27 10:17:46 +02002988 struct appctx *appctx = __sc_appctx(sc);
Willy Tarreau91cefca2022-05-03 17:08:29 +02002989 struct show_stat_ctx *ctx = appctx->svcctx;
Christopher Faulet693b23b2022-02-28 09:09:05 +01002990
William Lallemand74c24fb2016-11-21 17:18:36 +01002991 chunk_appendf(&trash, "</table>");
2992
Willy Tarreau91cefca2022-05-03 17:08:29 +02002993 if ((px->cap & PR_CAP_BE) && px->srv && (ctx->flags & STAT_ADMIN)) {
William Lallemand74c24fb2016-11-21 17:18:36 +01002994 /* close the form used to enable/disable this proxy servers */
2995 chunk_appendf(&trash,
2996 "Choose the action to perform on the checked servers : "
2997 "<select name=action>"
2998 "<option value=\"\"></option>"
2999 "<option value=\"ready\">Set state to READY</option>"
3000 "<option value=\"drain\">Set state to DRAIN</option>"
3001 "<option value=\"maint\">Set state to MAINT</option>"
3002 "<option value=\"dhlth\">Health: disable checks</option>"
3003 "<option value=\"ehlth\">Health: enable checks</option>"
3004 "<option value=\"hrunn\">Health: force UP</option>"
3005 "<option value=\"hnolb\">Health: force NOLB</option>"
3006 "<option value=\"hdown\">Health: force DOWN</option>"
3007 "<option value=\"dagent\">Agent: disable checks</option>"
3008 "<option value=\"eagent\">Agent: enable checks</option>"
3009 "<option value=\"arunn\">Agent: force UP</option>"
3010 "<option value=\"adown\">Agent: force DOWN</option>"
3011 "<option value=\"shutdown\">Kill Sessions</option>"
3012 "</select>"
3013 "<input type=\"hidden\" name=\"b\" value=\"#%d\">"
3014 "&nbsp;<input type=\"submit\" value=\"Apply\">"
3015 "</form>",
3016 px->uuid);
3017 }
3018
3019 chunk_appendf(&trash, "<p>\n");
3020}
3021
3022/*
Willy Tarreau4596fe22022-05-17 19:07:51 +02003023 * Dumps statistics for a proxy. The output is sent to the stream connector's
William Lallemand74c24fb2016-11-21 17:18:36 +01003024 * input buffer. Returns 0 if it had to stop dumping data because of lack of
3025 * buffer space, or non-zero if everything completed. This function is used
3026 * both by the CLI and the HTTP entry points, and is able to dump the output
3027 * in HTML or CSV formats. If the later, <uri> must be NULL.
3028 */
Willy Tarreaucaff6312022-05-27 10:17:46 +02003029int stats_dump_proxy_to_buffer(struct stconn *sc, struct htx *htx,
Christopher Fauletef779222018-10-31 08:47:01 +01003030 struct proxy *px, struct uri_auth *uri)
William Lallemand74c24fb2016-11-21 17:18:36 +01003031{
Willy Tarreaucaff6312022-05-27 10:17:46 +02003032 struct appctx *appctx = __sc_appctx(sc);
Willy Tarreau91cefca2022-05-03 17:08:29 +02003033 struct show_stat_ctx *ctx = appctx->svcctx;
Willy Tarreaucaff6312022-05-27 10:17:46 +02003034 struct stream *s = __sc_strm(sc);
3035 struct channel *rep = sc_ic(sc);
William Lallemand74c24fb2016-11-21 17:18:36 +01003036 struct server *sv, *svs; /* server and server-state, server-state=server or server->track */
3037 struct listener *l;
William Lallemand74c24fb2016-11-21 17:18:36 +01003038
3039 chunk_reset(&trash);
3040
Willy Tarreau91cefca2022-05-03 17:08:29 +02003041 switch (ctx->px_st) {
William Lallemand74c24fb2016-11-21 17:18:36 +01003042 case STAT_PX_ST_INIT:
3043 /* we are on a new proxy */
3044 if (uri && uri->scope) {
3045 /* we have a limited scope, we have to check the proxy name */
3046 struct stat_scope *scope;
3047 int len;
3048
3049 len = strlen(px->id);
3050 scope = uri->scope;
3051
3052 while (scope) {
3053 /* match exact proxy name */
3054 if (scope->px_len == len && !memcmp(px->id, scope->px_id, len))
3055 break;
3056
3057 /* match '.' which means 'self' proxy */
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003058 if (strcmp(scope->px_id, ".") == 0 && px == s->be)
William Lallemand74c24fb2016-11-21 17:18:36 +01003059 break;
3060 scope = scope->next;
3061 }
3062
3063 /* proxy name not found : don't dump anything */
3064 if (scope == NULL)
3065 return 1;
3066 }
3067
3068 /* if the user has requested a limited output and the proxy
3069 * name does not match, skip it.
3070 */
Willy Tarreau91cefca2022-05-03 17:08:29 +02003071 if (ctx->scope_len) {
Willy Tarreaucaff6312022-05-27 10:17:46 +02003072 const char *scope_ptr = stats_scope_ptr(appctx, sc);
Christopher Fauleted7a0662019-01-14 11:07:34 +01003073
Willy Tarreau91cefca2022-05-03 17:08:29 +02003074 if (strnistr(px->id, strlen(px->id), scope_ptr, ctx->scope_len) == NULL)
Christopher Fauleted7a0662019-01-14 11:07:34 +01003075 return 1;
3076 }
William Lallemand74c24fb2016-11-21 17:18:36 +01003077
Willy Tarreau91cefca2022-05-03 17:08:29 +02003078 if ((ctx->flags & STAT_BOUND) &&
3079 (ctx->iid != -1) &&
3080 (px->uuid != ctx->iid))
William Lallemand74c24fb2016-11-21 17:18:36 +01003081 return 1;
3082
Willy Tarreau91cefca2022-05-03 17:08:29 +02003083 ctx->px_st = STAT_PX_ST_TH;
Willy Tarreau9eb93c02022-11-14 07:12:05 +01003084 __fallthrough;
William Lallemand74c24fb2016-11-21 17:18:36 +01003085
3086 case STAT_PX_ST_TH:
Willy Tarreau91cefca2022-05-03 17:08:29 +02003087 if (ctx->flags & STAT_FMT_HTML) {
Willy Tarreaucaff6312022-05-27 10:17:46 +02003088 stats_dump_html_px_hdr(sc, px);
Christopher Fauletef779222018-10-31 08:47:01 +01003089 if (!stats_putchk(rep, htx, &trash))
3090 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01003091 }
3092
Willy Tarreau91cefca2022-05-03 17:08:29 +02003093 ctx->px_st = STAT_PX_ST_FE;
Willy Tarreau9eb93c02022-11-14 07:12:05 +01003094 __fallthrough;
William Lallemand74c24fb2016-11-21 17:18:36 +01003095
3096 case STAT_PX_ST_FE:
3097 /* print the frontend */
Willy Tarreaucaff6312022-05-27 10:17:46 +02003098 if (stats_dump_fe_stats(sc, px)) {
Christopher Fauletef779222018-10-31 08:47:01 +01003099 if (!stats_putchk(rep, htx, &trash))
3100 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01003101 }
3102
Willy Tarreau91cefca2022-05-03 17:08:29 +02003103 ctx->obj2 = px->conf.listeners.n;
3104 ctx->px_st = STAT_PX_ST_LI;
Willy Tarreau9eb93c02022-11-14 07:12:05 +01003105 __fallthrough;
William Lallemand74c24fb2016-11-21 17:18:36 +01003106
3107 case STAT_PX_ST_LI:
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +02003108 /* obj2 points to listeners list as initialized above */
Willy Tarreau91cefca2022-05-03 17:08:29 +02003109 for (; ctx->obj2 != &px->conf.listeners; ctx->obj2 = l->by_fe.n) {
Christopher Fauletef779222018-10-31 08:47:01 +01003110 if (htx) {
3111 if (htx_almost_full(htx))
3112 goto full;
3113 }
3114 else {
3115 if (buffer_almost_full(&rep->buf))
3116 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01003117 }
3118
Willy Tarreau91cefca2022-05-03 17:08:29 +02003119 l = LIST_ELEM(ctx->obj2, struct listener *, by_fe);
William Lallemand74c24fb2016-11-21 17:18:36 +01003120 if (!l->counters)
3121 continue;
3122
Willy Tarreau91cefca2022-05-03 17:08:29 +02003123 if (ctx->flags & STAT_BOUND) {
3124 if (!(ctx->type & (1 << STATS_TYPE_SO)))
William Lallemand74c24fb2016-11-21 17:18:36 +01003125 break;
3126
Willy Tarreau91cefca2022-05-03 17:08:29 +02003127 if (ctx->sid != -1 && l->luid != ctx->sid)
William Lallemand74c24fb2016-11-21 17:18:36 +01003128 continue;
3129 }
3130
3131 /* print the frontend */
Willy Tarreaucaff6312022-05-27 10:17:46 +02003132 if (stats_dump_li_stats(sc, px, l)) {
Christopher Fauletef779222018-10-31 08:47:01 +01003133 if (!stats_putchk(rep, htx, &trash))
3134 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01003135 }
3136 }
3137
Willy Tarreau91cefca2022-05-03 17:08:29 +02003138 ctx->obj2 = px->srv; /* may be NULL */
3139 ctx->px_st = STAT_PX_ST_SV;
Willy Tarreau9eb93c02022-11-14 07:12:05 +01003140 __fallthrough;
William Lallemand74c24fb2016-11-21 17:18:36 +01003141
3142 case STAT_PX_ST_SV:
Amaury Denoyelle0a8d05d2021-08-25 14:39:53 +02003143 /* obj2 points to servers list as initialized above.
3144 *
Amaury Denoyellebc2ebfa2021-08-25 15:34:53 +02003145 * A server may be removed during the stats dumping.
Amaury Denoyelle0a8d05d2021-08-25 14:39:53 +02003146 * Temporarily increment its refcount to prevent its
3147 * anticipated cleaning. Call free_server to release it.
3148 */
Willy Tarreau91cefca2022-05-03 17:08:29 +02003149 for (; ctx->obj2 != NULL;
3150 ctx->obj2 = srv_drop(sv)) {
Amaury Denoyelle0a8d05d2021-08-25 14:39:53 +02003151
Willy Tarreau91cefca2022-05-03 17:08:29 +02003152 sv = ctx->obj2;
Amaury Denoyellebc2ebfa2021-08-25 15:34:53 +02003153 srv_take(sv);
Amaury Denoyelle0a8d05d2021-08-25 14:39:53 +02003154
Christopher Fauletef779222018-10-31 08:47:01 +01003155 if (htx) {
3156 if (htx_almost_full(htx))
3157 goto full;
3158 }
3159 else {
3160 if (buffer_almost_full(&rep->buf))
3161 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01003162 }
3163
Willy Tarreau91cefca2022-05-03 17:08:29 +02003164 if (ctx->flags & STAT_BOUND) {
3165 if (!(ctx->type & (1 << STATS_TYPE_SV))) {
Amaury Denoyellebc2ebfa2021-08-25 15:34:53 +02003166 srv_drop(sv);
William Lallemand74c24fb2016-11-21 17:18:36 +01003167 break;
Amaury Denoyelle0a8d05d2021-08-25 14:39:53 +02003168 }
William Lallemand74c24fb2016-11-21 17:18:36 +01003169
Willy Tarreau91cefca2022-05-03 17:08:29 +02003170 if (ctx->sid != -1 && sv->puid != ctx->sid)
William Lallemand74c24fb2016-11-21 17:18:36 +01003171 continue;
3172 }
3173
Willy Tarreau3e320362020-10-23 17:28:57 +02003174 /* do not report disabled servers */
Willy Tarreau91cefca2022-05-03 17:08:29 +02003175 if (ctx->flags & STAT_HIDE_MAINT &&
Willy Tarreau3e320362020-10-23 17:28:57 +02003176 sv->cur_admin & SRV_ADMF_MAINT) {
3177 continue;
3178 }
3179
William Lallemand74c24fb2016-11-21 17:18:36 +01003180 svs = sv;
3181 while (svs->track)
3182 svs = svs->track;
3183
3184 /* do not report servers which are DOWN and not changing state */
Willy Tarreau91cefca2022-05-03 17:08:29 +02003185 if ((ctx->flags & STAT_HIDE_DOWN) &&
Emeric Brun52a91d32017-08-31 14:41:55 +02003186 ((sv->cur_admin & SRV_ADMF_MAINT) || /* server is in maintenance */
3187 (sv->cur_state == SRV_ST_STOPPED && /* server is down */
William Lallemand74c24fb2016-11-21 17:18:36 +01003188 (!((svs->agent.state | svs->check.state) & CHK_ST_ENABLED) ||
3189 ((svs->agent.state & CHK_ST_ENABLED) && !svs->agent.health) ||
3190 ((svs->check.state & CHK_ST_ENABLED) && !svs->check.health))))) {
3191 continue;
3192 }
3193
Willy Tarreaucaff6312022-05-27 10:17:46 +02003194 if (stats_dump_sv_stats(sc, px, sv)) {
Christopher Fauletef779222018-10-31 08:47:01 +01003195 if (!stats_putchk(rep, htx, &trash))
3196 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01003197 }
3198 } /* for sv */
3199
Willy Tarreau91cefca2022-05-03 17:08:29 +02003200 ctx->px_st = STAT_PX_ST_BE;
Willy Tarreau9eb93c02022-11-14 07:12:05 +01003201 __fallthrough;
William Lallemand74c24fb2016-11-21 17:18:36 +01003202
3203 case STAT_PX_ST_BE:
3204 /* print the backend */
Willy Tarreaucaff6312022-05-27 10:17:46 +02003205 if (stats_dump_be_stats(sc, px)) {
Christopher Fauletef779222018-10-31 08:47:01 +01003206 if (!stats_putchk(rep, htx, &trash))
3207 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01003208 }
3209
Willy Tarreau91cefca2022-05-03 17:08:29 +02003210 ctx->px_st = STAT_PX_ST_END;
Willy Tarreau9eb93c02022-11-14 07:12:05 +01003211 __fallthrough;
William Lallemand74c24fb2016-11-21 17:18:36 +01003212
3213 case STAT_PX_ST_END:
Willy Tarreau91cefca2022-05-03 17:08:29 +02003214 if (ctx->flags & STAT_FMT_HTML) {
Willy Tarreaucaff6312022-05-27 10:17:46 +02003215 stats_dump_html_px_end(sc, px);
Christopher Fauletef779222018-10-31 08:47:01 +01003216 if (!stats_putchk(rep, htx, &trash))
3217 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01003218 }
3219
Willy Tarreau91cefca2022-05-03 17:08:29 +02003220 ctx->px_st = STAT_PX_ST_FIN;
Willy Tarreau9eb93c02022-11-14 07:12:05 +01003221 __fallthrough;
William Lallemand74c24fb2016-11-21 17:18:36 +01003222
3223 case STAT_PX_ST_FIN:
3224 return 1;
3225
3226 default:
3227 /* unknown state, we should put an abort() here ! */
3228 return 1;
3229 }
Christopher Fauletef779222018-10-31 08:47:01 +01003230
3231 full:
Willy Tarreaucaff6312022-05-27 10:17:46 +02003232 sc_need_room(sc);
Christopher Fauletef779222018-10-31 08:47:01 +01003233 return 0;
William Lallemand74c24fb2016-11-21 17:18:36 +01003234}
3235
3236/* Dumps the HTTP stats head block to the trash for and uses the per-uri
3237 * parameters <uri>. The caller is responsible for clearing the trash if needed.
3238 */
Willy Tarreau676c29e2019-10-09 10:50:01 +02003239static void stats_dump_html_head(struct appctx *appctx, struct uri_auth *uri)
William Lallemand74c24fb2016-11-21 17:18:36 +01003240{
Willy Tarreau91cefca2022-05-03 17:08:29 +02003241 struct show_stat_ctx *ctx = appctx->svcctx;
3242
William Lallemand74c24fb2016-11-21 17:18:36 +01003243 /* WARNING! This must fit in the first buffer !!! */
3244 chunk_appendf(&trash,
3245 "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"\n"
3246 "\"http://www.w3.org/TR/html4/loose.dtd\">\n"
3247 "<html><head><title>Statistics Report for " PRODUCT_NAME "%s%s</title>\n"
Thierry Fournierb6b1cde2021-09-24 00:03:07 +02003248 "<link rel=\"icon\" href=\"data:,\">\n"
William Lallemand74c24fb2016-11-21 17:18:36 +01003249 "<meta http-equiv=\"content-type\" content=\"text/html; charset=iso-8859-1\">\n"
3250 "<style type=\"text/css\"><!--\n"
3251 "body {"
3252 " font-family: arial, helvetica, sans-serif;"
3253 " font-size: 12px;"
3254 " font-weight: normal;"
3255 " color: black;"
3256 " background: white;"
3257 "}\n"
3258 "th,td {"
3259 " font-size: 10px;"
3260 "}\n"
3261 "h1 {"
3262 " font-size: x-large;"
3263 " margin-bottom: 0.5em;"
3264 "}\n"
3265 "h2 {"
3266 " font-family: helvetica, arial;"
3267 " font-size: x-large;"
3268 " font-weight: bold;"
3269 " font-style: italic;"
3270 " color: #6020a0;"
3271 " margin-top: 0em;"
3272 " margin-bottom: 0em;"
3273 "}\n"
3274 "h3 {"
3275 " font-family: helvetica, arial;"
3276 " font-size: 16px;"
3277 " font-weight: bold;"
3278 " color: #b00040;"
3279 " background: #e8e8d0;"
3280 " margin-top: 0em;"
3281 " margin-bottom: 0em;"
3282 "}\n"
3283 "li {"
3284 " margin-top: 0.25em;"
3285 " margin-right: 2em;"
3286 "}\n"
3287 ".hr {margin-top: 0.25em;"
3288 " border-color: black;"
3289 " border-bottom-style: solid;"
3290 "}\n"
3291 ".titre {background: #20D0D0;color: #000000; font-weight: bold; text-align: center;}\n"
3292 ".total {background: #20D0D0;color: #ffff80;}\n"
3293 ".frontend {background: #e8e8d0;}\n"
3294 ".socket {background: #d0d0d0;}\n"
3295 ".backend {background: #e8e8d0;}\n"
3296 ".active_down {background: #ff9090;}\n"
3297 ".active_going_up {background: #ffd020;}\n"
3298 ".active_going_down {background: #ffffa0;}\n"
3299 ".active_up {background: #c0ffc0;}\n"
3300 ".active_nolb {background: #20a0ff;}\n"
3301 ".active_draining {background: #20a0FF;}\n"
3302 ".active_no_check {background: #e0e0e0;}\n"
3303 ".backup_down {background: #ff9090;}\n"
3304 ".backup_going_up {background: #ff80ff;}\n"
3305 ".backup_going_down {background: #c060ff;}\n"
3306 ".backup_up {background: #b0d0ff;}\n"
3307 ".backup_nolb {background: #90b0e0;}\n"
3308 ".backup_draining {background: #cc9900;}\n"
3309 ".backup_no_check {background: #e0e0e0;}\n"
3310 ".maintain {background: #c07820;}\n"
3311 ".rls {letter-spacing: 0.2em; margin-right: 1px;}\n" /* right letter spacing (used for grouping digits) */
3312 "\n"
3313 "a.px:link {color: #ffff40; text-decoration: none;}"
3314 "a.px:visited {color: #ffff40; text-decoration: none;}"
3315 "a.px:hover {color: #ffffff; text-decoration: none;}"
3316 "a.lfsb:link {color: #000000; text-decoration: none;}"
3317 "a.lfsb:visited {color: #000000; text-decoration: none;}"
3318 "a.lfsb:hover {color: #505050; text-decoration: none;}"
3319 "\n"
3320 "table.tbl { border-collapse: collapse; border-style: none;}\n"
3321 "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"
3322 "table.tbl td.ac { text-align: center;}\n"
3323 "table.tbl th { border-width: 1px; border-style: solid solid solid solid; border-color: gray;}\n"
3324 "table.tbl th.pxname { background: #b00040; color: #ffff40; font-weight: bold; border-style: solid solid none solid; padding: 2px 3px; white-space: nowrap;}\n"
3325 "table.tbl th.empty { border-style: none; empty-cells: hide; background: white;}\n"
3326 "table.tbl th.desc { background: white; border-style: solid solid none solid; text-align: left; padding: 2px 3px;}\n"
3327 "\n"
3328 "table.lgd { border-collapse: collapse; border-width: 1px; border-style: none none none solid; border-color: black;}\n"
3329 "table.lgd td { border-width: 1px; border-style: solid solid solid solid; border-color: gray; padding: 2px;}\n"
3330 "table.lgd td.noborder { border-style: none; padding: 2px; white-space: nowrap;}\n"
3331 "table.det { border-collapse: collapse; border-style: none; }\n"
3332 "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"
3333 "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"
3334 "u {text-decoration:none; border-bottom: 1px dotted black;}\n"
Thierry Fournierb6b1cde2021-09-24 00:03:07 +02003335 "div.tips {\n"
3336 " display:block;\n"
3337 " visibility:hidden;\n"
3338 " z-index:2147483647;\n"
3339 " position:absolute;\n"
3340 " padding:2px 4px 3px;\n"
3341 " background:#f0f060; color:#000000;\n"
3342 " border:1px solid #7040c0;\n"
3343 " white-space:nowrap;\n"
3344 " font-style:normal;font-size:11px;font-weight:normal;\n"
3345 " -moz-border-radius:3px;-webkit-border-radius:3px;border-radius:3px;\n"
3346 " -moz-box-shadow:gray 2px 2px 3px;-webkit-box-shadow:gray 2px 2px 3px;box-shadow:gray 2px 2px 3px;\n"
3347 "}\n"
3348 "u:hover div.tips {visibility:visible;}\n"
Thierry Fourniere9ed63e2021-09-23 23:59:49 +02003349 "@media (prefers-color-scheme: dark) {\n"
3350 " body { font-family: arial, helvetica, sans-serif; font-size: 12px; font-weight: normal; color: #e8e6e3; background: #131516;}\n"
3351 " h1 { color: #a265e0!important; }\n"
3352 " h2 { color: #a265e0; }\n"
3353 " h3 { color: #ff5190; background-color: #3e3e1f; }\n"
3354 " a { color: #3391ff; }\n"
3355 " input { background-color: #2f3437; }\n"
3356 " .hr { border-color: #8c8273; }\n"
3357 " .titre { background-color: #1aa6a6; color: #e8e6e3; }\n"
3358 " .frontend {background: #2f3437;}\n"
Marno Krahmera690b732022-03-08 13:45:09 +01003359 " .socket {background: #2a2d2f;}\n"
Thierry Fourniere9ed63e2021-09-23 23:59:49 +02003360 " .backend {background: #2f3437;}\n"
3361 " .active_down {background: #760000;}\n"
3362 " .active_going_up {background: #b99200;}\n"
3363 " .active_going_down {background: #6c6c00;}\n"
3364 " .active_up {background: #165900;}\n"
3365 " .active_nolb {background: #006ab9;}\n"
3366 " .active_draining {background: #006ab9;}\n"
3367 " .active_no_check {background: #2a2d2f;}\n"
3368 " .backup_down {background: #760000;}\n"
3369 " .backup_going_up {background: #7f007f;}\n"
3370 " .backup_going_down {background: #580092;}\n"
3371 " .backup_up {background: #2e3234;}\n"
3372 " .backup_nolb {background: #1e3c6a;}\n"
3373 " .backup_draining {background: #a37a00;}\n"
3374 " .backup_no_check {background: #2a2d2f;}\n"
3375 " .maintain {background: #9a601a;}\n"
3376 " a.px:link {color: #d8d83b; text-decoration: none;}\n"
3377 " a.px:visited {color: #d8d83b; text-decoration: none;}\n"
3378 " a.px:hover {color: #ffffff; text-decoration: none;}\n"
3379 " a.lfsb:link {color: #e8e6e3; text-decoration: none;}\n"
3380 " a.lfsb:visited {color: #e8e6e3; text-decoration: none;}\n"
3381 " a.lfsb:hover {color: #b5afa6; text-decoration: none;}\n"
3382 " table.tbl th.empty { background-color: #181a1b; }\n"
Willy Tarreau00147f72022-04-11 07:59:27 +02003383 " table.tbl th.desc { background: #181a1b; }\n"
Thierry Fourniere9ed63e2021-09-23 23:59:49 +02003384 " table.tbl th.pxname { background-color: #8d0033; color: #ffff46; }\n"
3385 " table.tbl th { border-color: #808080; }\n"
3386 " table.tbl td { border-color: #808080; }\n"
3387 " u {text-decoration:none; border-bottom: 1px dotted #e8e6e3;}\n"
3388 " div.tips {\n"
3389 " background:#8e8e0d;\n"
3390 " color:#e8e6e3;\n"
3391 " border-color: #4e2c86;\n"
3392 " -moz-box-shadow: #60686c 2px 2px 3px;\n"
3393 " -webkit-box-shadow: #60686c 2px 2px 3px;\n"
3394 " box-shadow: #60686c 2px 2px 3px;\n"
3395 " }\n"
3396 "}\n"
William Lallemand74c24fb2016-11-21 17:18:36 +01003397 "-->\n"
3398 "</style></head>\n",
Willy Tarreau91cefca2022-05-03 17:08:29 +02003399 (ctx->flags & STAT_SHNODE) ? " on " : "",
3400 (ctx->flags & STAT_SHNODE) ? (uri && uri->node ? uri->node : global.node) : ""
William Lallemand74c24fb2016-11-21 17:18:36 +01003401 );
3402}
3403
3404/* Dumps the HTML stats information block to the trash for and uses the state from
Willy Tarreaucaff6312022-05-27 10:17:46 +02003405 * stream connector <sc> and per-uri parameters <uri>. The caller is responsible
William Lallemand74c24fb2016-11-21 17:18:36 +01003406 * for clearing the trash if needed.
3407 */
Willy Tarreaucaff6312022-05-27 10:17:46 +02003408static void stats_dump_html_info(struct stconn *sc, struct uri_auth *uri)
William Lallemand74c24fb2016-11-21 17:18:36 +01003409{
Willy Tarreaucaff6312022-05-27 10:17:46 +02003410 struct appctx *appctx = __sc_appctx(sc);
Willy Tarreau91cefca2022-05-03 17:08:29 +02003411 struct show_stat_ctx *ctx = appctx->svcctx;
William Lallemand74c24fb2016-11-21 17:18:36 +01003412 unsigned int up = (now.tv_sec - start_date.tv_sec);
3413 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
Willy Tarreaucaff6312022-05-27 10:17:46 +02003414 const char *scope_ptr = stats_scope_ptr(appctx, sc);
Willy Tarreau1713c032019-05-23 12:23:55 +02003415 unsigned long long bps = (unsigned long long)read_freq_ctr(&global.out_32bps) * 32;
3416
3417 /* Turn the bytes per second to bits per second and take care of the
3418 * usual ethernet overhead in order to help figure how far we are from
3419 * interface saturation since it's the only case which usually matters.
3420 * For this we count the total size of an Ethernet frame on the wire
3421 * including preamble and IFG (1538) for the largest TCP segment it
3422 * transports (1448 with TCP timestamps). This is not valid for smaller
3423 * packets (under-estimated), but it gives a reasonably accurate
3424 * estimation of how far we are from uplink saturation.
3425 */
3426 bps = bps * 8 * 1538 / 1448;
William Lallemand74c24fb2016-11-21 17:18:36 +01003427
3428 /* WARNING! this has to fit the first packet too.
3429 * We are around 3.5 kB, add adding entries will
3430 * become tricky if we want to support 4kB buffers !
3431 */
3432 chunk_appendf(&trash,
3433 "<body><h1><a href=\"" PRODUCT_URL "\" style=\"text-decoration: none;\">"
3434 PRODUCT_NAME "%s</a></h1>\n"
3435 "<h2>Statistics Report for pid %d%s%s%s%s</h2>\n"
3436 "<hr width=\"100%%\" class=\"hr\">\n"
3437 "<h3>&gt; General process information</h3>\n"
3438 "<table border=0><tr><td align=\"left\" nowrap width=\"1%%\">\n"
Yves Lafon95317282018-02-26 11:10:37 +01003439 "<p><b>pid = </b> %d (process #%d, nbproc = %d, nbthread = %d)<br>\n"
William Lallemand74c24fb2016-11-21 17:18:36 +01003440 "<b>uptime = </b> %dd %dh%02dm%02ds<br>\n"
3441 "<b>system limits:</b> memmax = %s%s; ulimit-n = %d<br>\n"
3442 "<b>maxsock = </b> %d; <b>maxconn = </b> %d; <b>maxpipes = </b> %d<br>\n"
Willy Tarreau1713c032019-05-23 12:23:55 +02003443 "current conns = %d; current pipes = %d/%d; conn rate = %d/sec; bit rate = %.3f %cbps<br>\n"
William Lallemand74c24fb2016-11-21 17:18:36 +01003444 "Running tasks: %d/%d; idle = %d %%<br>\n"
3445 "</td><td align=\"center\" nowrap>\n"
3446 "<table class=\"lgd\"><tr>\n"
3447 "<td class=\"active_up\">&nbsp;</td><td class=\"noborder\">active UP </td>"
3448 "<td class=\"backup_up\">&nbsp;</td><td class=\"noborder\">backup UP </td>"
3449 "</tr><tr>\n"
3450 "<td class=\"active_going_down\"></td><td class=\"noborder\">active UP, going down </td>"
3451 "<td class=\"backup_going_down\"></td><td class=\"noborder\">backup UP, going down </td>"
3452 "</tr><tr>\n"
3453 "<td class=\"active_going_up\"></td><td class=\"noborder\">active DOWN, going up </td>"
3454 "<td class=\"backup_going_up\"></td><td class=\"noborder\">backup DOWN, going up </td>"
3455 "</tr><tr>\n"
3456 "<td class=\"active_down\"></td><td class=\"noborder\">active or backup DOWN &nbsp;</td>"
3457 "<td class=\"active_no_check\"></td><td class=\"noborder\">not checked </td>"
3458 "</tr><tr>\n"
3459 "<td class=\"maintain\"></td><td class=\"noborder\" colspan=\"3\">active or backup DOWN for maintenance (MAINT) &nbsp;</td>"
3460 "</tr><tr>\n"
3461 "<td class=\"active_draining\"></td><td class=\"noborder\" colspan=\"3\">active or backup SOFT STOPPED for maintenance &nbsp;</td>"
3462 "</tr></table>\n"
3463 "Note: \"NOLB\"/\"DRAIN\" = UP with load-balancing disabled."
3464 "</td>"
3465 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
3466 "<b>Display option:</b><ul style=\"margin-top: 0.25em;\">"
3467 "",
Willy Tarreau91cefca2022-05-03 17:08:29 +02003468 (ctx->flags & STAT_HIDEVER) ? "" : (stats_version_string),
3469 pid, (ctx->flags & STAT_SHNODE) ? " on " : "",
3470 (ctx->flags & STAT_SHNODE) ? (uri->node ? uri->node : global.node) : "",
3471 (ctx->flags & STAT_SHDESC) ? ": " : "",
3472 (ctx->flags & STAT_SHDESC) ? (uri->desc ? uri->desc : global.desc) : "",
Willy Tarreaue8422bf2021-06-15 09:08:18 +02003473 pid, 1, 1, global.nbthread,
William Lallemand74c24fb2016-11-21 17:18:36 +01003474 up / 86400, (up % 86400) / 3600,
3475 (up % 3600) / 60, (up % 60),
3476 global.rlimit_memmax ? ultoa(global.rlimit_memmax) : "unlimited",
3477 global.rlimit_memmax ? " MB" : "",
3478 global.rlimit_nofile,
3479 global.maxsock, global.maxconn, global.maxpipes,
3480 actconn, pipes_used, pipes_used+pipes_free, read_freq_ctr(&global.conn_per_sec),
Willy Tarreau1713c032019-05-23 12:23:55 +02003481 bps >= 1000000000UL ? (bps / 1000000000.0) : bps >= 1000000UL ? (bps / 1000000.0) : (bps / 1000.0),
3482 bps >= 1000000000UL ? 'G' : bps >= 1000000UL ? 'M' : 'k',
Willy Tarreauf9d5e102021-10-08 10:43:59 +02003483 total_run_queues(), total_allocated_tasks(), clock_report_idle()
William Lallemand74c24fb2016-11-21 17:18:36 +01003484 );
3485
Willy Tarreau91cefca2022-05-03 17:08:29 +02003486 /* scope_txt = search query, ctx->scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
3487 memcpy(scope_txt, scope_ptr, ctx->scope_len);
3488 scope_txt[ctx->scope_len] = '\0';
William Lallemand74c24fb2016-11-21 17:18:36 +01003489
3490 chunk_appendf(&trash,
3491 "<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 +02003492 (ctx->scope_len > 0) ? scope_txt : "",
William Lallemand74c24fb2016-11-21 17:18:36 +01003493 STAT_SCOPE_TXT_MAXLEN);
3494
Willy Tarreau91cefca2022-05-03 17:08:29 +02003495 /* scope_txt = search pattern + search query, ctx->scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
William Lallemand74c24fb2016-11-21 17:18:36 +01003496 scope_txt[0] = 0;
Willy Tarreau91cefca2022-05-03 17:08:29 +02003497 if (ctx->scope_len) {
William Lallemand74c24fb2016-11-21 17:18:36 +01003498 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Willy Tarreau91cefca2022-05-03 17:08:29 +02003499 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), scope_ptr, ctx->scope_len);
3500 scope_txt[strlen(STAT_SCOPE_PATTERN) + ctx->scope_len] = 0;
William Lallemand74c24fb2016-11-21 17:18:36 +01003501 }
3502
Willy Tarreau91cefca2022-05-03 17:08:29 +02003503 if (ctx->flags & STAT_HIDE_DOWN)
William Lallemand74c24fb2016-11-21 17:18:36 +01003504 chunk_appendf(&trash,
3505 "<li><a href=\"%s%s%s%s\">Show all servers</a><br>\n",
3506 uri->uri_prefix,
3507 "",
Willy Tarreau91cefca2022-05-03 17:08:29 +02003508 (ctx->flags & STAT_NO_REFRESH) ? ";norefresh" : "",
William Lallemand74c24fb2016-11-21 17:18:36 +01003509 scope_txt);
3510 else
3511 chunk_appendf(&trash,
3512 "<li><a href=\"%s%s%s%s\">Hide 'DOWN' servers</a><br>\n",
3513 uri->uri_prefix,
3514 ";up",
Willy Tarreau91cefca2022-05-03 17:08:29 +02003515 (ctx->flags & STAT_NO_REFRESH) ? ";norefresh" : "",
William Lallemand74c24fb2016-11-21 17:18:36 +01003516 scope_txt);
3517
3518 if (uri->refresh > 0) {
Willy Tarreau91cefca2022-05-03 17:08:29 +02003519 if (ctx->flags & STAT_NO_REFRESH)
William Lallemand74c24fb2016-11-21 17:18:36 +01003520 chunk_appendf(&trash,
3521 "<li><a href=\"%s%s%s%s\">Enable refresh</a><br>\n",
3522 uri->uri_prefix,
Willy Tarreau91cefca2022-05-03 17:08:29 +02003523 (ctx->flags & STAT_HIDE_DOWN) ? ";up" : "",
William Lallemand74c24fb2016-11-21 17:18:36 +01003524 "",
3525 scope_txt);
3526 else
3527 chunk_appendf(&trash,
3528 "<li><a href=\"%s%s%s%s\">Disable refresh</a><br>\n",
3529 uri->uri_prefix,
Willy Tarreau91cefca2022-05-03 17:08:29 +02003530 (ctx->flags & STAT_HIDE_DOWN) ? ";up" : "",
William Lallemand74c24fb2016-11-21 17:18:36 +01003531 ";norefresh",
3532 scope_txt);
3533 }
3534
3535 chunk_appendf(&trash,
3536 "<li><a href=\"%s%s%s%s\">Refresh now</a><br>\n",
3537 uri->uri_prefix,
Willy Tarreau91cefca2022-05-03 17:08:29 +02003538 (ctx->flags & STAT_HIDE_DOWN) ? ";up" : "",
3539 (ctx->flags & STAT_NO_REFRESH) ? ";norefresh" : "",
William Lallemand74c24fb2016-11-21 17:18:36 +01003540 scope_txt);
3541
3542 chunk_appendf(&trash,
3543 "<li><a href=\"%s;csv%s%s\">CSV export</a><br>\n",
3544 uri->uri_prefix,
3545 (uri->refresh > 0) ? ";norefresh" : "",
3546 scope_txt);
3547
3548 chunk_appendf(&trash,
Christopher Faulet6338a082019-09-09 15:50:54 +02003549 "<li><a href=\"%s;json%s%s\">JSON export</a> (<a href=\"%s;json-schema\">schema</a>)<br>\n",
3550 uri->uri_prefix,
3551 (uri->refresh > 0) ? ";norefresh" : "",
3552 scope_txt, uri->uri_prefix);
3553
3554 chunk_appendf(&trash,
William Lallemand74c24fb2016-11-21 17:18:36 +01003555 "</ul></td>"
3556 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
3557 "<b>External resources:</b><ul style=\"margin-top: 0.25em;\">\n"
3558 "<li><a href=\"" PRODUCT_URL "\">Primary site</a><br>\n"
3559 "<li><a href=\"" PRODUCT_URL_UPD "\">Updates (v" PRODUCT_BRANCH ")</a><br>\n"
3560 "<li><a href=\"" PRODUCT_URL_DOC "\">Online manual</a><br>\n"
3561 "</ul>"
3562 "</td>"
3563 "</tr></table>\n"
3564 ""
3565 );
3566
Willy Tarreau91cefca2022-05-03 17:08:29 +02003567 if (ctx->st_code) {
3568 switch (ctx->st_code) {
William Lallemand74c24fb2016-11-21 17:18:36 +01003569 case STAT_STATUS_DONE:
3570 chunk_appendf(&trash,
3571 "<p><div class=active_up>"
3572 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
3573 "Action processed successfully."
3574 "</div>\n", uri->uri_prefix,
Willy Tarreau91cefca2022-05-03 17:08:29 +02003575 (ctx->flags & STAT_HIDE_DOWN) ? ";up" : "",
3576 (ctx->flags & STAT_NO_REFRESH) ? ";norefresh" : "",
William Lallemand74c24fb2016-11-21 17:18:36 +01003577 scope_txt);
3578 break;
3579 case STAT_STATUS_NONE:
3580 chunk_appendf(&trash,
3581 "<p><div class=active_going_down>"
3582 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
3583 "Nothing has changed."
3584 "</div>\n", uri->uri_prefix,
Willy Tarreau91cefca2022-05-03 17:08:29 +02003585 (ctx->flags & STAT_HIDE_DOWN) ? ";up" : "",
3586 (ctx->flags & STAT_NO_REFRESH) ? ";norefresh" : "",
William Lallemand74c24fb2016-11-21 17:18:36 +01003587 scope_txt);
3588 break;
3589 case STAT_STATUS_PART:
3590 chunk_appendf(&trash,
3591 "<p><div class=active_going_down>"
3592 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
3593 "Action partially processed.<br>"
3594 "Some server names are probably unknown or ambiguous (duplicated names in the backend)."
3595 "</div>\n", uri->uri_prefix,
Willy Tarreau91cefca2022-05-03 17:08:29 +02003596 (ctx->flags & STAT_HIDE_DOWN) ? ";up" : "",
3597 (ctx->flags & STAT_NO_REFRESH) ? ";norefresh" : "",
William Lallemand74c24fb2016-11-21 17:18:36 +01003598 scope_txt);
3599 break;
3600 case STAT_STATUS_ERRP:
3601 chunk_appendf(&trash,
3602 "<p><div class=active_down>"
3603 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
3604 "Action not processed because of invalid parameters."
3605 "<ul>"
3606 "<li>The action is maybe unknown.</li>"
Christopher Faulet2f9a41d2019-02-27 15:30:57 +01003607 "<li>Invalid key parameter (empty or too long).</li>"
William Lallemand74c24fb2016-11-21 17:18:36 +01003608 "<li>The backend name is probably unknown or ambiguous (duplicated names).</li>"
3609 "<li>Some server names are probably unknown or ambiguous (duplicated names in the backend).</li>"
3610 "</ul>"
3611 "</div>\n", uri->uri_prefix,
Willy Tarreau91cefca2022-05-03 17:08:29 +02003612 (ctx->flags & STAT_HIDE_DOWN) ? ";up" : "",
3613 (ctx->flags & STAT_NO_REFRESH) ? ";norefresh" : "",
William Lallemand74c24fb2016-11-21 17:18:36 +01003614 scope_txt);
3615 break;
3616 case STAT_STATUS_EXCD:
3617 chunk_appendf(&trash,
3618 "<p><div class=active_down>"
3619 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
3620 "<b>Action not processed : the buffer couldn't store all the data.<br>"
3621 "You should retry with less servers at a time.</b>"
3622 "</div>\n", uri->uri_prefix,
Willy Tarreau91cefca2022-05-03 17:08:29 +02003623 (ctx->flags & STAT_HIDE_DOWN) ? ";up" : "",
3624 (ctx->flags & STAT_NO_REFRESH) ? ";norefresh" : "",
William Lallemand74c24fb2016-11-21 17:18:36 +01003625 scope_txt);
3626 break;
3627 case STAT_STATUS_DENY:
3628 chunk_appendf(&trash,
3629 "<p><div class=active_down>"
3630 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
3631 "<b>Action denied.</b>"
3632 "</div>\n", uri->uri_prefix,
Willy Tarreau91cefca2022-05-03 17:08:29 +02003633 (ctx->flags & STAT_HIDE_DOWN) ? ";up" : "",
3634 (ctx->flags & STAT_NO_REFRESH) ? ";norefresh" : "",
William Lallemand74c24fb2016-11-21 17:18:36 +01003635 scope_txt);
3636 break;
Christopher Faulet3c2ecf72019-02-27 16:41:27 +01003637 case STAT_STATUS_IVAL:
3638 chunk_appendf(&trash,
3639 "<p><div class=active_down>"
3640 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
3641 "<b>Invalid requests (unsupported method or chunked encoded request).</b>"
3642 "</div>\n", uri->uri_prefix,
Willy Tarreau91cefca2022-05-03 17:08:29 +02003643 (ctx->flags & STAT_HIDE_DOWN) ? ";up" : "",
3644 (ctx->flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Christopher Faulet3c2ecf72019-02-27 16:41:27 +01003645 scope_txt);
3646 break;
William Lallemand74c24fb2016-11-21 17:18:36 +01003647 default:
3648 chunk_appendf(&trash,
3649 "<p><div class=active_no_check>"
3650 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
3651 "Unexpected result."
3652 "</div>\n", uri->uri_prefix,
Willy Tarreau91cefca2022-05-03 17:08:29 +02003653 (ctx->flags & STAT_HIDE_DOWN) ? ";up" : "",
3654 (ctx->flags & STAT_NO_REFRESH) ? ";norefresh" : "",
William Lallemand74c24fb2016-11-21 17:18:36 +01003655 scope_txt);
3656 }
3657 chunk_appendf(&trash, "<p>\n");
3658 }
3659}
3660
3661/* Dumps the HTML stats trailer block to the trash. The caller is responsible
3662 * for clearing the trash if needed.
3663 */
3664static void stats_dump_html_end()
3665{
3666 chunk_appendf(&trash, "</body></html>\n");
3667}
3668
Simon Horman05ee2132017-01-04 09:37:25 +01003669/* Dumps the stats JSON header to the trash buffer which. The caller is responsible
3670 * for clearing it if needed.
3671 */
3672static void stats_dump_json_header()
3673{
3674 chunk_strcat(&trash, "[");
3675}
3676
3677
3678/* Dumps the JSON stats trailer block to the trash. The caller is responsible
3679 * for clearing the trash if needed.
3680 */
3681static void stats_dump_json_end()
3682{
Willy Tarreau5a0e7ca2022-06-10 09:21:22 +02003683 chunk_strcat(&trash, "]\n");
Simon Horman05ee2132017-01-04 09:37:25 +01003684}
3685
Amaury Denoyelle98b81cb2020-10-02 18:32:04 +02003686/* Uses <appctx.ctx.stats.obj1> as a pointer to the current proxy and <obj2> as
3687 * a pointer to the current server/listener.
3688 */
Willy Tarreaucaff6312022-05-27 10:17:46 +02003689static int stats_dump_proxies(struct stconn *sc,
Amaury Denoyelle98b81cb2020-10-02 18:32:04 +02003690 struct htx *htx,
3691 struct uri_auth *uri)
3692{
Willy Tarreaucaff6312022-05-27 10:17:46 +02003693 struct appctx *appctx = __sc_appctx(sc);
Willy Tarreau91cefca2022-05-03 17:08:29 +02003694 struct show_stat_ctx *ctx = appctx->svcctx;
Willy Tarreaucaff6312022-05-27 10:17:46 +02003695 struct channel *rep = sc_ic(sc);
Amaury Denoyelle98b81cb2020-10-02 18:32:04 +02003696 struct proxy *px;
3697
3698 /* dump proxies */
Willy Tarreau91cefca2022-05-03 17:08:29 +02003699 while (ctx->obj1) {
Amaury Denoyelle98b81cb2020-10-02 18:32:04 +02003700 if (htx) {
3701 if (htx_almost_full(htx))
3702 goto full;
3703 }
3704 else {
3705 if (buffer_almost_full(&rep->buf))
3706 goto full;
3707 }
3708
Willy Tarreau91cefca2022-05-03 17:08:29 +02003709 px = ctx->obj1;
Marno Krahmer07954fb2021-06-24 16:51:08 +02003710 /* Skip the global frontend proxies and non-networked ones.
William Lallemand85a16b22021-08-03 13:18:11 +02003711 * Also skip proxies that were disabled in the configuration
Marno Krahmer07954fb2021-06-24 16:51:08 +02003712 * This change allows retrieving stats from "old" proxies after a reload.
3713 */
Christopher Fauletdfd10ab2021-10-06 14:24:19 +02003714 if (!(px->flags & PR_FL_DISABLED) && px->uuid > 0 &&
William Lallemande7f74622021-07-28 17:45:18 +02003715 (px->cap & (PR_CAP_FE | PR_CAP_BE)) && !(px->cap & PR_CAP_INT)) {
Willy Tarreaucaff6312022-05-27 10:17:46 +02003716 if (stats_dump_proxy_to_buffer(sc, htx, px, uri) == 0)
Amaury Denoyelle98b81cb2020-10-02 18:32:04 +02003717 return 0;
3718 }
3719
Willy Tarreau91cefca2022-05-03 17:08:29 +02003720 ctx->obj1 = px->next;
3721 ctx->px_st = STAT_PX_ST_INIT;
Amaury Denoyelle98b81cb2020-10-02 18:32:04 +02003722 }
3723
3724 return 1;
3725
3726 full:
Willy Tarreaucaff6312022-05-27 10:17:46 +02003727 sc_need_room(sc);
Amaury Denoyelle98b81cb2020-10-02 18:32:04 +02003728 return 0;
3729}
3730
Willy Tarreau4596fe22022-05-17 19:07:51 +02003731/* This function dumps statistics onto the stream connector's read buffer in
William Lallemand74c24fb2016-11-21 17:18:36 +01003732 * either CSV or HTML format. <uri> contains some HTML-specific parameters that
3733 * are ignored for CSV format (hence <uri> may be NULL there). It returns 0 if
3734 * it had to stop writing data and an I/O is needed, 1 if the dump is finished
3735 * and the stream must be closed, or -1 in case of any error. This function is
3736 * used by both the CLI and the HTTP handlers.
3737 */
Willy Tarreaucaff6312022-05-27 10:17:46 +02003738static int stats_dump_stat_to_buffer(struct stconn *sc, struct htx *htx,
Christopher Fauletef779222018-10-31 08:47:01 +01003739 struct uri_auth *uri)
William Lallemand74c24fb2016-11-21 17:18:36 +01003740{
Willy Tarreaucaff6312022-05-27 10:17:46 +02003741 struct appctx *appctx = __sc_appctx(sc);
Willy Tarreau91cefca2022-05-03 17:08:29 +02003742 struct show_stat_ctx *ctx = appctx->svcctx;
Willy Tarreaucaff6312022-05-27 10:17:46 +02003743 struct channel *rep = sc_ic(sc);
Willy Tarreau91cefca2022-05-03 17:08:29 +02003744 enum stats_domain domain = ctx->domain;
William Lallemand74c24fb2016-11-21 17:18:36 +01003745
3746 chunk_reset(&trash);
3747
Willy Tarreau41f88522022-05-03 18:39:27 +02003748 switch (ctx->state) {
Willy Tarreau6ef16482022-05-06 18:07:53 +02003749 case STAT_STATE_INIT:
3750 ctx->state = STAT_STATE_HEAD; /* let's start producing data */
Willy Tarreau9eb93c02022-11-14 07:12:05 +01003751 __fallthrough;
William Lallemand74c24fb2016-11-21 17:18:36 +01003752
Willy Tarreau6ef16482022-05-06 18:07:53 +02003753 case STAT_STATE_HEAD:
Willy Tarreau91cefca2022-05-03 17:08:29 +02003754 if (ctx->flags & STAT_FMT_HTML)
Willy Tarreau676c29e2019-10-09 10:50:01 +02003755 stats_dump_html_head(appctx, uri);
Willy Tarreau91cefca2022-05-03 17:08:29 +02003756 else if (ctx->flags & STAT_JSON_SCHM)
Christopher Faulet6338a082019-09-09 15:50:54 +02003757 stats_dump_json_schema(&trash);
Willy Tarreau91cefca2022-05-03 17:08:29 +02003758 else if (ctx->flags & STAT_FMT_JSON)
Willy Tarreau9d7fb632017-04-11 07:53:04 +02003759 stats_dump_json_header();
Willy Tarreau91cefca2022-05-03 17:08:29 +02003760 else if (!(ctx->flags & STAT_FMT_TYPED))
3761 stats_dump_csv_header(ctx->domain);
William Lallemand74c24fb2016-11-21 17:18:36 +01003762
Christopher Fauletef779222018-10-31 08:47:01 +01003763 if (!stats_putchk(rep, htx, &trash))
3764 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01003765
Willy Tarreau91cefca2022-05-03 17:08:29 +02003766 if (ctx->flags & STAT_JSON_SCHM) {
Willy Tarreau6ef16482022-05-06 18:07:53 +02003767 ctx->state = STAT_STATE_FIN;
Christopher Faulet6338a082019-09-09 15:50:54 +02003768 return 1;
3769 }
Willy Tarreau6ef16482022-05-06 18:07:53 +02003770 ctx->state = STAT_STATE_INFO;
Willy Tarreau9eb93c02022-11-14 07:12:05 +01003771 __fallthrough;
William Lallemand74c24fb2016-11-21 17:18:36 +01003772
Willy Tarreau6ef16482022-05-06 18:07:53 +02003773 case STAT_STATE_INFO:
Willy Tarreau91cefca2022-05-03 17:08:29 +02003774 if (ctx->flags & STAT_FMT_HTML) {
Willy Tarreaucaff6312022-05-27 10:17:46 +02003775 stats_dump_html_info(sc, uri);
Christopher Fauletef779222018-10-31 08:47:01 +01003776 if (!stats_putchk(rep, htx, &trash))
3777 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01003778 }
3779
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02003780 if (domain == STATS_DOMAIN_PROXY)
Willy Tarreau91cefca2022-05-03 17:08:29 +02003781 ctx->obj1 = proxies_list;
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02003782
Willy Tarreau91cefca2022-05-03 17:08:29 +02003783 ctx->px_st = STAT_PX_ST_INIT;
Willy Tarreau6ef16482022-05-06 18:07:53 +02003784 ctx->state = STAT_STATE_LIST;
Willy Tarreau9eb93c02022-11-14 07:12:05 +01003785 __fallthrough;
William Lallemand74c24fb2016-11-21 17:18:36 +01003786
Willy Tarreau6ef16482022-05-06 18:07:53 +02003787 case STAT_STATE_LIST:
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02003788 switch (domain) {
Emeric Brunf8642ee2021-10-29 17:59:18 +02003789 case STATS_DOMAIN_RESOLVERS:
Willy Tarreaucaff6312022-05-27 10:17:46 +02003790 if (!stats_dump_resolvers(sc, stat_l[domain],
Emeric Brund3b44952021-01-06 16:01:02 +01003791 stat_count[domain],
3792 &stats_module_list[domain])) {
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02003793 return 0;
3794 }
3795 break;
3796
3797 case STATS_DOMAIN_PROXY:
3798 default:
3799 /* dump proxies */
Willy Tarreaucaff6312022-05-27 10:17:46 +02003800 if (!stats_dump_proxies(sc, htx, uri))
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02003801 return 0;
3802 break;
3803 }
William Lallemand74c24fb2016-11-21 17:18:36 +01003804
Willy Tarreau6ef16482022-05-06 18:07:53 +02003805 ctx->state = STAT_STATE_END;
Willy Tarreau9eb93c02022-11-14 07:12:05 +01003806 __fallthrough;
William Lallemand74c24fb2016-11-21 17:18:36 +01003807
Willy Tarreau6ef16482022-05-06 18:07:53 +02003808 case STAT_STATE_END:
Willy Tarreau91cefca2022-05-03 17:08:29 +02003809 if (ctx->flags & (STAT_FMT_HTML|STAT_FMT_JSON)) {
3810 if (ctx->flags & STAT_FMT_HTML)
Simon Horman05ee2132017-01-04 09:37:25 +01003811 stats_dump_html_end();
3812 else
3813 stats_dump_json_end();
Christopher Fauletef779222018-10-31 08:47:01 +01003814 if (!stats_putchk(rep, htx, &trash))
3815 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01003816 }
3817
Willy Tarreau6ef16482022-05-06 18:07:53 +02003818 ctx->state = STAT_STATE_FIN;
Willy Tarreau9eb93c02022-11-14 07:12:05 +01003819 __fallthrough;
William Lallemand74c24fb2016-11-21 17:18:36 +01003820
Willy Tarreau6ef16482022-05-06 18:07:53 +02003821 case STAT_STATE_FIN:
William Lallemand74c24fb2016-11-21 17:18:36 +01003822 return 1;
3823
3824 default:
3825 /* unknown state ! */
Willy Tarreau6ef16482022-05-06 18:07:53 +02003826 ctx->state = STAT_STATE_FIN;
William Lallemand74c24fb2016-11-21 17:18:36 +01003827 return -1;
3828 }
Christopher Fauletef779222018-10-31 08:47:01 +01003829
3830 full:
Willy Tarreaucaff6312022-05-27 10:17:46 +02003831 sc_need_room(sc);
Christopher Fauletef779222018-10-31 08:47:01 +01003832 return 0;
3833
William Lallemand74c24fb2016-11-21 17:18:36 +01003834}
3835
3836/* We reached the stats page through a POST request. The appctx is
3837 * expected to have already been allocated by the caller.
3838 * Parse the posted data and enable/disable servers if necessary.
3839 * Returns 1 if request was parsed or zero if it needs more data.
3840 */
Willy Tarreaucaff6312022-05-27 10:17:46 +02003841static int stats_process_http_post(struct stconn *sc)
William Lallemand74c24fb2016-11-21 17:18:36 +01003842{
Willy Tarreaucaff6312022-05-27 10:17:46 +02003843 struct stream *s = __sc_strm(sc);
3844 struct appctx *appctx = __sc_appctx(sc);
Willy Tarreau91cefca2022-05-03 17:08:29 +02003845 struct show_stat_ctx *ctx = appctx->svcctx;
William Lallemand74c24fb2016-11-21 17:18:36 +01003846
3847 struct proxy *px = NULL;
3848 struct server *sv = NULL;
3849
3850 char key[LINESIZE];
3851 int action = ST_ADM_ACTION_NONE;
3852 int reprocess = 0;
3853
3854 int total_servers = 0;
3855 int altered_servers = 0;
3856
3857 char *first_param, *cur_param, *next_param, *end_params;
3858 char *st_cur_param = NULL;
3859 char *st_next_param = NULL;
3860
Christopher Fauleta3618372018-12-13 21:59:56 +01003861 struct buffer *temp = get_trash_chunk();
William Lallemand74c24fb2016-11-21 17:18:36 +01003862
Christopher Fauletb7f88902019-07-15 21:56:43 +02003863 struct htx *htx = htxbuf(&s->req.buf);
3864 struct htx_blk *blk;
Christopher Fauleta3618372018-12-13 21:59:56 +01003865
Christopher Fauletb7f88902019-07-15 21:56:43 +02003866 /* we need more data */
3867 if (s->txn->req.msg_state < HTTP_MSG_DONE) {
3868 /* check if we can receive more */
3869 if (htx_free_data_space(htx) <= global.tune.maxrewrite) {
Willy Tarreau91cefca2022-05-03 17:08:29 +02003870 ctx->st_code = STAT_STATUS_EXCD;
Christopher Fauletb7f88902019-07-15 21:56:43 +02003871 goto out;
Christopher Fauleta3618372018-12-13 21:59:56 +01003872 }
Christopher Fauletb7f88902019-07-15 21:56:43 +02003873 goto wait;
3874 }
Christopher Fauleta3618372018-12-13 21:59:56 +01003875
Christopher Fauletb7f88902019-07-15 21:56:43 +02003876 /* The request was fully received. Copy data */
3877 blk = htx_get_head_blk(htx);
3878 while (blk) {
3879 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Fauleta3618372018-12-13 21:59:56 +01003880
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01003881 if (type == HTX_BLK_TLR || type == HTX_BLK_EOT)
Christopher Fauletb7f88902019-07-15 21:56:43 +02003882 break;
3883 if (type == HTX_BLK_DATA) {
3884 struct ist v = htx_get_blk_value(htx, blk);
Christopher Fauleta3618372018-12-13 21:59:56 +01003885
Christopher Fauletb7f88902019-07-15 21:56:43 +02003886 if (!chunk_memcat(temp, v.ptr, v.len)) {
Willy Tarreau91cefca2022-05-03 17:08:29 +02003887 ctx->st_code = STAT_STATUS_EXCD;
Christopher Faulet2f9a41d2019-02-27 15:30:57 +01003888 goto out;
3889 }
Christopher Fauleta3618372018-12-13 21:59:56 +01003890 }
Christopher Fauletb7f88902019-07-15 21:56:43 +02003891 blk = htx_get_next_blk(htx, blk);
William Lallemand74c24fb2016-11-21 17:18:36 +01003892 }
3893
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003894 first_param = temp->area;
Christopher Fauleta3618372018-12-13 21:59:56 +01003895 end_params = temp->area + temp->data;
William Lallemand74c24fb2016-11-21 17:18:36 +01003896 cur_param = next_param = end_params;
3897 *end_params = '\0';
3898
Willy Tarreau91cefca2022-05-03 17:08:29 +02003899 ctx->st_code = STAT_STATUS_NONE;
William Lallemand74c24fb2016-11-21 17:18:36 +01003900
3901 /*
3902 * Parse the parameters in reverse order to only store the last value.
3903 * From the html form, the backend and the action are at the end.
3904 */
3905 while (cur_param > first_param) {
3906 char *value;
3907 int poffset, plen;
3908
3909 cur_param--;
3910
3911 if ((*cur_param == '&') || (cur_param == first_param)) {
3912 reprocess_servers:
3913 /* Parse the key */
3914 poffset = (cur_param != first_param ? 1 : 0);
3915 plen = next_param - cur_param + (cur_param == first_param ? 1 : 0);
3916 if ((plen > 0) && (plen <= sizeof(key))) {
3917 strncpy(key, cur_param + poffset, plen);
3918 key[plen - 1] = '\0';
3919 } else {
Willy Tarreau91cefca2022-05-03 17:08:29 +02003920 ctx->st_code = STAT_STATUS_ERRP;
William Lallemand74c24fb2016-11-21 17:18:36 +01003921 goto out;
3922 }
3923
3924 /* Parse the value */
3925 value = key;
3926 while (*value != '\0' && *value != '=') {
3927 value++;
3928 }
3929 if (*value == '=') {
3930 /* Ok, a value is found, we can mark the end of the key */
3931 *value++ = '\0';
3932 }
Willy Tarreau62ba9ba2020-04-23 17:54:47 +02003933 if (url_decode(key, 1) < 0 || url_decode(value, 1) < 0)
William Lallemand74c24fb2016-11-21 17:18:36 +01003934 break;
3935
3936 /* Now we can check the key to see what to do */
3937 if (!px && (strcmp(key, "b") == 0)) {
3938 if ((px = proxy_be_by_name(value)) == NULL) {
3939 /* the backend name is unknown or ambiguous (duplicate names) */
Willy Tarreau91cefca2022-05-03 17:08:29 +02003940 ctx->st_code = STAT_STATUS_ERRP;
William Lallemand74c24fb2016-11-21 17:18:36 +01003941 goto out;
3942 }
3943 }
3944 else if (!action && (strcmp(key, "action") == 0)) {
3945 if (strcmp(value, "ready") == 0) {
3946 action = ST_ADM_ACTION_READY;
3947 }
3948 else if (strcmp(value, "drain") == 0) {
3949 action = ST_ADM_ACTION_DRAIN;
3950 }
3951 else if (strcmp(value, "maint") == 0) {
3952 action = ST_ADM_ACTION_MAINT;
3953 }
3954 else if (strcmp(value, "shutdown") == 0) {
3955 action = ST_ADM_ACTION_SHUTDOWN;
3956 }
3957 else if (strcmp(value, "dhlth") == 0) {
3958 action = ST_ADM_ACTION_DHLTH;
3959 }
3960 else if (strcmp(value, "ehlth") == 0) {
3961 action = ST_ADM_ACTION_EHLTH;
3962 }
3963 else if (strcmp(value, "hrunn") == 0) {
3964 action = ST_ADM_ACTION_HRUNN;
3965 }
3966 else if (strcmp(value, "hnolb") == 0) {
3967 action = ST_ADM_ACTION_HNOLB;
3968 }
3969 else if (strcmp(value, "hdown") == 0) {
3970 action = ST_ADM_ACTION_HDOWN;
3971 }
3972 else if (strcmp(value, "dagent") == 0) {
3973 action = ST_ADM_ACTION_DAGENT;
3974 }
3975 else if (strcmp(value, "eagent") == 0) {
3976 action = ST_ADM_ACTION_EAGENT;
3977 }
3978 else if (strcmp(value, "arunn") == 0) {
3979 action = ST_ADM_ACTION_ARUNN;
3980 }
3981 else if (strcmp(value, "adown") == 0) {
3982 action = ST_ADM_ACTION_ADOWN;
3983 }
3984 /* else these are the old supported methods */
3985 else if (strcmp(value, "disable") == 0) {
3986 action = ST_ADM_ACTION_DISABLE;
3987 }
3988 else if (strcmp(value, "enable") == 0) {
3989 action = ST_ADM_ACTION_ENABLE;
3990 }
3991 else if (strcmp(value, "stop") == 0) {
3992 action = ST_ADM_ACTION_STOP;
3993 }
3994 else if (strcmp(value, "start") == 0) {
3995 action = ST_ADM_ACTION_START;
3996 }
3997 else {
Willy Tarreau91cefca2022-05-03 17:08:29 +02003998 ctx->st_code = STAT_STATUS_ERRP;
William Lallemand74c24fb2016-11-21 17:18:36 +01003999 goto out;
4000 }
4001 }
4002 else if (strcmp(key, "s") == 0) {
4003 if (!(px && action)) {
4004 /*
4005 * Indicates that we'll need to reprocess the parameters
4006 * as soon as backend and action are known
4007 */
4008 if (!reprocess) {
4009 st_cur_param = cur_param;
4010 st_next_param = next_param;
4011 }
4012 reprocess = 1;
4013 }
4014 else if ((sv = findserver(px, value)) != NULL) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004015 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
William Lallemand74c24fb2016-11-21 17:18:36 +01004016 switch (action) {
4017 case ST_ADM_ACTION_DISABLE:
Emeric Brun52a91d32017-08-31 14:41:55 +02004018 if (!(sv->cur_admin & SRV_ADMF_FMAINT)) {
William Lallemand74c24fb2016-11-21 17:18:36 +01004019 altered_servers++;
4020 total_servers++;
4021 srv_set_admin_flag(sv, SRV_ADMF_FMAINT, "'disable' on stats page");
4022 }
4023 break;
4024 case ST_ADM_ACTION_ENABLE:
Emeric Brun52a91d32017-08-31 14:41:55 +02004025 if (sv->cur_admin & SRV_ADMF_FMAINT) {
William Lallemand74c24fb2016-11-21 17:18:36 +01004026 altered_servers++;
4027 total_servers++;
4028 srv_clr_admin_flag(sv, SRV_ADMF_FMAINT);
4029 }
4030 break;
4031 case ST_ADM_ACTION_STOP:
Emeric Brun52a91d32017-08-31 14:41:55 +02004032 if (!(sv->cur_admin & SRV_ADMF_FDRAIN)) {
William Lallemand74c24fb2016-11-21 17:18:36 +01004033 srv_set_admin_flag(sv, SRV_ADMF_FDRAIN, "'stop' on stats page");
4034 altered_servers++;
4035 total_servers++;
4036 }
4037 break;
4038 case ST_ADM_ACTION_START:
Emeric Brun52a91d32017-08-31 14:41:55 +02004039 if (sv->cur_admin & SRV_ADMF_FDRAIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +01004040 srv_clr_admin_flag(sv, SRV_ADMF_FDRAIN);
4041 altered_servers++;
4042 total_servers++;
4043 }
4044 break;
4045 case ST_ADM_ACTION_DHLTH:
4046 if (sv->check.state & CHK_ST_CONFIGURED) {
4047 sv->check.state &= ~CHK_ST_ENABLED;
4048 altered_servers++;
4049 total_servers++;
4050 }
4051 break;
4052 case ST_ADM_ACTION_EHLTH:
4053 if (sv->check.state & CHK_ST_CONFIGURED) {
4054 sv->check.state |= CHK_ST_ENABLED;
4055 altered_servers++;
4056 total_servers++;
4057 }
4058 break;
4059 case ST_ADM_ACTION_HRUNN:
4060 if (!(sv->track)) {
4061 sv->check.health = sv->check.rise + sv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02004062 srv_set_running(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01004063 altered_servers++;
4064 total_servers++;
4065 }
4066 break;
4067 case ST_ADM_ACTION_HNOLB:
4068 if (!(sv->track)) {
4069 sv->check.health = sv->check.rise + sv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02004070 srv_set_stopping(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01004071 altered_servers++;
4072 total_servers++;
4073 }
4074 break;
4075 case ST_ADM_ACTION_HDOWN:
4076 if (!(sv->track)) {
4077 sv->check.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02004078 srv_set_stopped(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01004079 altered_servers++;
4080 total_servers++;
4081 }
4082 break;
4083 case ST_ADM_ACTION_DAGENT:
4084 if (sv->agent.state & CHK_ST_CONFIGURED) {
4085 sv->agent.state &= ~CHK_ST_ENABLED;
4086 altered_servers++;
4087 total_servers++;
4088 }
4089 break;
4090 case ST_ADM_ACTION_EAGENT:
4091 if (sv->agent.state & CHK_ST_CONFIGURED) {
4092 sv->agent.state |= CHK_ST_ENABLED;
4093 altered_servers++;
4094 total_servers++;
4095 }
4096 break;
4097 case ST_ADM_ACTION_ARUNN:
4098 if (sv->agent.state & CHK_ST_ENABLED) {
4099 sv->agent.health = sv->agent.rise + sv->agent.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02004100 srv_set_running(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01004101 altered_servers++;
4102 total_servers++;
4103 }
4104 break;
4105 case ST_ADM_ACTION_ADOWN:
4106 if (sv->agent.state & CHK_ST_ENABLED) {
4107 sv->agent.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02004108 srv_set_stopped(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01004109 altered_servers++;
4110 total_servers++;
4111 }
4112 break;
4113 case ST_ADM_ACTION_READY:
4114 srv_adm_set_ready(sv);
4115 altered_servers++;
4116 total_servers++;
4117 break;
4118 case ST_ADM_ACTION_DRAIN:
4119 srv_adm_set_drain(sv);
4120 altered_servers++;
4121 total_servers++;
4122 break;
4123 case ST_ADM_ACTION_MAINT:
4124 srv_adm_set_maint(sv);
4125 altered_servers++;
4126 total_servers++;
4127 break;
4128 case ST_ADM_ACTION_SHUTDOWN:
Christopher Fauletdfd10ab2021-10-06 14:24:19 +02004129 if (!(px->flags & (PR_FL_DISABLED|PR_FL_STOPPED))) {
Willy Tarreauaf7ea812019-11-14 16:42:04 +01004130 srv_shutdown_streams(sv, SF_ERR_KILLED);
William Lallemand74c24fb2016-11-21 17:18:36 +01004131 altered_servers++;
4132 total_servers++;
4133 }
4134 break;
4135 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004136 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
William Lallemand74c24fb2016-11-21 17:18:36 +01004137 } else {
4138 /* the server name is unknown or ambiguous (duplicate names) */
4139 total_servers++;
4140 }
4141 }
4142 if (reprocess && px && action) {
4143 /* Now, we know the backend and the action chosen by the user.
4144 * We can safely restart from the first server parameter
4145 * to reprocess them
4146 */
4147 cur_param = st_cur_param;
4148 next_param = st_next_param;
4149 reprocess = 0;
4150 goto reprocess_servers;
4151 }
4152
4153 next_param = cur_param;
4154 }
4155 }
4156
4157 if (total_servers == 0) {
Willy Tarreau91cefca2022-05-03 17:08:29 +02004158 ctx->st_code = STAT_STATUS_NONE;
William Lallemand74c24fb2016-11-21 17:18:36 +01004159 }
4160 else if (altered_servers == 0) {
Willy Tarreau91cefca2022-05-03 17:08:29 +02004161 ctx->st_code = STAT_STATUS_ERRP;
William Lallemand74c24fb2016-11-21 17:18:36 +01004162 }
4163 else if (altered_servers == total_servers) {
Willy Tarreau91cefca2022-05-03 17:08:29 +02004164 ctx->st_code = STAT_STATUS_DONE;
William Lallemand74c24fb2016-11-21 17:18:36 +01004165 }
4166 else {
Willy Tarreau91cefca2022-05-03 17:08:29 +02004167 ctx->st_code = STAT_STATUS_PART;
William Lallemand74c24fb2016-11-21 17:18:36 +01004168 }
4169 out:
4170 return 1;
Christopher Faulet2f9a41d2019-02-27 15:30:57 +01004171 wait:
Willy Tarreau91cefca2022-05-03 17:08:29 +02004172 ctx->st_code = STAT_STATUS_NONE;
Christopher Faulet2f9a41d2019-02-27 15:30:57 +01004173 return 0;
Christopher Fauletef779222018-10-31 08:47:01 +01004174}
4175
4176
Willy Tarreaucaff6312022-05-27 10:17:46 +02004177static int stats_send_http_headers(struct stconn *sc, struct htx *htx)
Christopher Fauletef779222018-10-31 08:47:01 +01004178{
Willy Tarreaucaff6312022-05-27 10:17:46 +02004179 struct stream *s = __sc_strm(sc);
Christopher Fauletef779222018-10-31 08:47:01 +01004180 struct uri_auth *uri = s->be->uri_auth;
Willy Tarreaucaff6312022-05-27 10:17:46 +02004181 struct appctx *appctx = __sc_appctx(sc);
Willy Tarreau91cefca2022-05-03 17:08:29 +02004182 struct show_stat_ctx *ctx = appctx->svcctx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004183 struct htx_sl *sl;
4184 unsigned int flags;
Christopher Fauletef779222018-10-31 08:47:01 +01004185
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004186 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);
4187 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), ist("200"), ist("OK"));
4188 if (!sl)
Christopher Fauletef779222018-10-31 08:47:01 +01004189 goto full;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004190 sl->info.res.status = 200;
Christopher Fauletef779222018-10-31 08:47:01 +01004191
Christopher Fauletb829f4c2019-03-29 16:13:55 +01004192 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")))
Christopher Fauletef779222018-10-31 08:47:01 +01004193 goto full;
Willy Tarreau91cefca2022-05-03 17:08:29 +02004194 if (ctx->flags & STAT_FMT_HTML) {
Christopher Fauletef779222018-10-31 08:47:01 +01004195 if (!htx_add_header(htx, ist("Content-Type"), ist("text/html")))
4196 goto full;
4197 }
Willy Tarreau91cefca2022-05-03 17:08:29 +02004198 else if (ctx->flags & (STAT_FMT_JSON|STAT_JSON_SCHM)) {
Christopher Faulet6338a082019-09-09 15:50:54 +02004199 if (!htx_add_header(htx, ist("Content-Type"), ist("application/json")))
4200 goto full;
4201 }
Christopher Fauletef779222018-10-31 08:47:01 +01004202 else {
4203 if (!htx_add_header(htx, ist("Content-Type"), ist("text/plain")))
4204 goto full;
4205 }
4206
Willy Tarreau91cefca2022-05-03 17:08:29 +02004207 if (uri->refresh > 0 && !(ctx->flags & STAT_NO_REFRESH)) {
Christopher Fauletef779222018-10-31 08:47:01 +01004208 const char *refresh = U2A(uri->refresh);
Tim Duesterhusdcf753a2021-03-04 17:31:47 +01004209 if (!htx_add_header(htx, ist("Refresh"), ist(refresh)))
Christopher Fauletef779222018-10-31 08:47:01 +01004210 goto full;
4211 }
4212
Willy Tarreau91cefca2022-05-03 17:08:29 +02004213 if (ctx->flags & STAT_CHUNKED) {
Christopher Fauletef779222018-10-31 08:47:01 +01004214 if (!htx_add_header(htx, ist("Transfer-Encoding"), ist("chunked")))
4215 goto full;
4216 }
4217
4218 if (!htx_add_endof(htx, HTX_BLK_EOH))
4219 goto full;
4220
Christopher Faulet1e2d6362019-02-27 16:28:48 +01004221 channel_add_input(&s->res, htx->data);
Christopher Fauletef779222018-10-31 08:47:01 +01004222 return 1;
4223
4224 full:
4225 htx_reset(htx);
Willy Tarreaucaff6312022-05-27 10:17:46 +02004226 sc_need_room(sc);
Christopher Fauletef779222018-10-31 08:47:01 +01004227 return 0;
William Lallemand74c24fb2016-11-21 17:18:36 +01004228}
4229
Christopher Fauletef779222018-10-31 08:47:01 +01004230
Willy Tarreaucaff6312022-05-27 10:17:46 +02004231static int stats_send_http_redirect(struct stconn *sc, struct htx *htx)
Christopher Fauletef779222018-10-31 08:47:01 +01004232{
4233 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
Willy Tarreaucaff6312022-05-27 10:17:46 +02004234 struct stream *s = __sc_strm(sc);
Christopher Fauletef779222018-10-31 08:47:01 +01004235 struct uri_auth *uri = s->be->uri_auth;
Willy Tarreaucaff6312022-05-27 10:17:46 +02004236 struct appctx *appctx = __sc_appctx(sc);
Willy Tarreau91cefca2022-05-03 17:08:29 +02004237 struct show_stat_ctx *ctx = appctx->svcctx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004238 struct htx_sl *sl;
4239 unsigned int flags;
Christopher Fauletef779222018-10-31 08:47:01 +01004240
Willy Tarreau91cefca2022-05-03 17:08:29 +02004241 /* scope_txt = search pattern + search query, ctx->scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
Christopher Fauletef779222018-10-31 08:47:01 +01004242 scope_txt[0] = 0;
Willy Tarreau91cefca2022-05-03 17:08:29 +02004243 if (ctx->scope_len) {
Willy Tarreaucaff6312022-05-27 10:17:46 +02004244 const char *scope_ptr = stats_scope_ptr(appctx, sc);
Christopher Fauleted7a0662019-01-14 11:07:34 +01004245
Christopher Fauletef779222018-10-31 08:47:01 +01004246 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Willy Tarreau91cefca2022-05-03 17:08:29 +02004247 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), scope_ptr, ctx->scope_len);
4248 scope_txt[strlen(STAT_SCOPE_PATTERN) + ctx->scope_len] = 0;
Christopher Fauletef779222018-10-31 08:47:01 +01004249 }
4250
4251 /* We don't want to land on the posted stats page because a refresh will
4252 * repost the data. We don't want this to happen on accident so we redirect
4253 * the browse to the stats page with a GET.
4254 */
4255 chunk_printf(&trash, "%s;st=%s%s%s%s",
4256 uri->uri_prefix,
Willy Tarreau91cefca2022-05-03 17:08:29 +02004257 ((ctx->st_code > STAT_STATUS_INIT) &&
4258 (ctx->st_code < STAT_STATUS_SIZE) &&
4259 stat_status_codes[ctx->st_code]) ?
4260 stat_status_codes[ctx->st_code] :
Christopher Fauletef779222018-10-31 08:47:01 +01004261 stat_status_codes[STAT_STATUS_UNKN],
Willy Tarreau91cefca2022-05-03 17:08:29 +02004262 (ctx->flags & STAT_HIDE_DOWN) ? ";up" : "",
4263 (ctx->flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Christopher Fauletef779222018-10-31 08:47:01 +01004264 scope_txt);
4265
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004266 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_CHNK);
4267 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), ist("303"), ist("See Other"));
4268 if (!sl)
Christopher Fauletef779222018-10-31 08:47:01 +01004269 goto full;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004270 sl->info.res.status = 303;
Christopher Fauletef779222018-10-31 08:47:01 +01004271
4272 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")) ||
Christopher Fauletef779222018-10-31 08:47:01 +01004273 !htx_add_header(htx, ist("Content-Type"), ist("text/plain")) ||
4274 !htx_add_header(htx, ist("Content-Length"), ist("0")) ||
4275 !htx_add_header(htx, ist("Location"), ist2(trash.area, trash.data)))
4276 goto full;
4277
4278 if (!htx_add_endof(htx, HTX_BLK_EOH))
4279 goto full;
4280
Christopher Faulet1e2d6362019-02-27 16:28:48 +01004281 channel_add_input(&s->res, htx->data);
Christopher Fauletef779222018-10-31 08:47:01 +01004282 return 1;
4283
4284full:
4285 htx_reset(htx);
Willy Tarreaucaff6312022-05-27 10:17:46 +02004286 sc_need_room(sc);
Christopher Fauletef779222018-10-31 08:47:01 +01004287 return 0;
4288}
William Lallemand74c24fb2016-11-21 17:18:36 +01004289
Simon Horman05ee2132017-01-04 09:37:25 +01004290
Willy Tarreau4596fe22022-05-17 19:07:51 +02004291/* This I/O handler runs as an applet embedded in a stream connector. It is
William Lallemand74c24fb2016-11-21 17:18:36 +01004292 * used to send HTTP stats over a TCP socket. The mechanism is very simple.
4293 * appctx->st0 contains the operation in progress (dump, done). The handler
4294 * automatically unregisters itself once transfer is complete.
4295 */
Christopher Fauletb7f88902019-07-15 21:56:43 +02004296static void http_stats_io_handler(struct appctx *appctx)
Christopher Fauletef779222018-10-31 08:47:01 +01004297{
Willy Tarreau91cefca2022-05-03 17:08:29 +02004298 struct show_stat_ctx *ctx = appctx->svcctx;
Willy Tarreauc12b3212022-05-27 11:08:15 +02004299 struct stconn *sc = appctx_sc(appctx);
Willy Tarreaucaff6312022-05-27 10:17:46 +02004300 struct stream *s = __sc_strm(sc);
4301 struct channel *req = sc_oc(sc);
4302 struct channel *res = sc_ic(sc);
Christopher Fauletef779222018-10-31 08:47:01 +01004303 struct htx *req_htx, *res_htx;
4304
Amaury Denoyelle072f97e2020-10-05 11:49:37 +02004305 /* only proxy stats are available via http */
Willy Tarreau91cefca2022-05-03 17:08:29 +02004306 ctx->domain = STATS_DOMAIN_PROXY;
Amaury Denoyelle072f97e2020-10-05 11:49:37 +02004307
Christopher Fauletef779222018-10-31 08:47:01 +01004308 res_htx = htx_from_buf(&res->buf);
4309
Willy Tarreaucaff6312022-05-27 10:17:46 +02004310 if (unlikely(sc->state == SC_ST_DIS || sc->state == SC_ST_CLO))
Christopher Fauletef779222018-10-31 08:47:01 +01004311 goto out;
4312
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05004313 /* Check if the input buffer is available. */
Christopher Fauletef779222018-10-31 08:47:01 +01004314 if (!b_size(&res->buf)) {
Willy Tarreaucaff6312022-05-27 10:17:46 +02004315 sc_need_room(sc);
Christopher Fauletef779222018-10-31 08:47:01 +01004316 goto out;
4317 }
4318
4319 /* check that the output is not closed */
Christopher Faulet3a78aa62019-02-27 16:19:48 +01004320 if (res->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_SHUTR))
4321 appctx->st0 = STAT_HTTP_END;
Christopher Fauletef779222018-10-31 08:47:01 +01004322
4323 /* all states are processed in sequence */
4324 if (appctx->st0 == STAT_HTTP_HEAD) {
Willy Tarreaucaff6312022-05-27 10:17:46 +02004325 if (stats_send_http_headers(sc, res_htx)) {
Christopher Fauletef779222018-10-31 08:47:01 +01004326 if (s->txn->meth == HTTP_METH_HEAD)
4327 appctx->st0 = STAT_HTTP_DONE;
4328 else
4329 appctx->st0 = STAT_HTTP_DUMP;
4330 }
4331 }
4332
4333 if (appctx->st0 == STAT_HTTP_DUMP) {
Willy Tarreaucaff6312022-05-27 10:17:46 +02004334 if (stats_dump_stat_to_buffer(sc, res_htx, s->be->uri_auth))
Christopher Fauletef779222018-10-31 08:47:01 +01004335 appctx->st0 = STAT_HTTP_DONE;
4336 }
4337
4338 if (appctx->st0 == STAT_HTTP_POST) {
Willy Tarreaucaff6312022-05-27 10:17:46 +02004339 if (stats_process_http_post(sc))
Christopher Fauletef779222018-10-31 08:47:01 +01004340 appctx->st0 = STAT_HTTP_LAST;
Christopher Faulet3a78aa62019-02-27 16:19:48 +01004341 else if (req->flags & CF_SHUTR)
Christopher Fauletef779222018-10-31 08:47:01 +01004342 appctx->st0 = STAT_HTTP_DONE;
4343 }
4344
4345 if (appctx->st0 == STAT_HTTP_LAST) {
Willy Tarreaucaff6312022-05-27 10:17:46 +02004346 if (stats_send_http_redirect(sc, res_htx))
Christopher Fauletef779222018-10-31 08:47:01 +01004347 appctx->st0 = STAT_HTTP_DONE;
4348 }
4349
4350 if (appctx->st0 == STAT_HTTP_DONE) {
Christopher Faulet08b45cb2022-04-07 09:25:13 +02004351 /* no more data are expected. If the response buffer is empty,
4352 * be sure to add something (EOT block in this case) to have
4353 * something to send. It is important to be sure the EOM flags
4354 * will be handled by the endpoint.
4355 */
4356 if (htx_is_empty(res_htx)) {
4357 if (!htx_add_endof(res_htx, HTX_BLK_EOT)) {
Willy Tarreaucaff6312022-05-27 10:17:46 +02004358 sc_need_room(sc);
Christopher Faulet08b45cb2022-04-07 09:25:13 +02004359 goto out;
4360 }
4361 channel_add_input(res, 1);
4362 }
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01004363 res_htx->flags |= HTX_FL_EOM;
Christopher Faulet3fa5d192022-03-07 15:52:42 +01004364 res->flags |= CF_EOI;
Willy Tarreaud869e132022-05-17 18:05:31 +02004365 se_fl_set(appctx->sedesc, SE_FL_EOI);
Christopher Faulet3a78aa62019-02-27 16:19:48 +01004366 appctx->st0 = STAT_HTTP_END;
Christopher Fauletef779222018-10-31 08:47:01 +01004367 }
4368
Christopher Faulet3a78aa62019-02-27 16:19:48 +01004369 if (appctx->st0 == STAT_HTTP_END) {
4370 if (!(res->flags & CF_SHUTR)) {
Christopher Fauletef779222018-10-31 08:47:01 +01004371 res->flags |= CF_READ_NULL;
Willy Tarreaucaff6312022-05-27 10:17:46 +02004372 sc_shutr(sc);
Christopher Faulet3a78aa62019-02-27 16:19:48 +01004373 }
4374
4375 /* eat the whole request */
4376 if (co_data(req)) {
4377 req_htx = htx_from_buf(&req->buf);
4378 co_htx_skip(req, req_htx, co_data(req));
4379 htx_to_buf(req_htx, &req->buf);
Christopher Fauletef779222018-10-31 08:47:01 +01004380 }
4381 }
Christopher Faulet3a78aa62019-02-27 16:19:48 +01004382
Christopher Fauletef779222018-10-31 08:47:01 +01004383 out:
4384 /* we have left the request in the buffer for the case where we
4385 * process a POST, and this automatically re-enables activity on
4386 * read. It's better to indicate that we want to stop reading when
4387 * we're sending, so that we know there's at most one direction
4388 * deciding to wake the applet up. It saves it from looping when
4389 * emitting large blocks into small TCP windows.
4390 */
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01004391 htx_to_buf(res_htx, &res->buf);
4392 if (!channel_is_empty(res))
Willy Tarreau75a8f8e2022-05-25 18:12:11 +02004393 applet_wont_consume(appctx);
Christopher Fauletef779222018-10-31 08:47:01 +01004394}
4395
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004396/* Dump all fields from <info> into <out> using the "show info" format (name: value) */
Willy Tarreau83061a82018-07-13 11:56:34 +02004397static int stats_dump_info_fields(struct buffer *out,
Willy Tarreau43241ff2019-10-09 11:27:51 +02004398 const struct field *info, unsigned int flags)
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004399{
4400 int field;
4401
4402 for (field = 0; field < INF_TOTAL_FIELDS; field++) {
4403 if (!field_format(info, field))
4404 continue;
4405
Willy Tarreaueaa55372019-10-09 07:39:11 +02004406 if (!chunk_appendf(out, "%s: ", info_fields[field].name))
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004407 return 0;
4408 if (!stats_emit_raw_data_field(out, &info[field]))
4409 return 0;
Willy Tarreau6b19b142019-10-09 15:44:21 +02004410 if ((flags & STAT_SHOW_FDESC) && !chunk_appendf(out, ":\"%s\"", info_fields[field].desc))
4411 return 0;
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004412 if (!chunk_strcat(out, "\n"))
4413 return 0;
4414 }
4415 return 1;
4416}
4417
4418/* Dump all fields from <info> into <out> using the "show info typed" format */
Willy Tarreau83061a82018-07-13 11:56:34 +02004419static int stats_dump_typed_info_fields(struct buffer *out,
Willy Tarreau43241ff2019-10-09 11:27:51 +02004420 const struct field *info, unsigned int flags)
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004421{
4422 int field;
4423
4424 for (field = 0; field < INF_TOTAL_FIELDS; field++) {
4425 if (!field_format(info, field))
4426 continue;
4427
Willy Tarreaueaa55372019-10-09 07:39:11 +02004428 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 +01004429 return 0;
4430 if (!stats_emit_field_tags(out, &info[field], ':'))
4431 return 0;
4432 if (!stats_emit_typed_data_field(out, &info[field]))
4433 return 0;
Willy Tarreau6b19b142019-10-09 15:44:21 +02004434 if ((flags & STAT_SHOW_FDESC) && !chunk_appendf(out, ":\"%s\"", info_fields[field].desc))
4435 return 0;
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004436 if (!chunk_strcat(out, "\n"))
4437 return 0;
4438 }
4439 return 1;
4440}
4441
Willy Tarreau0b26b382021-05-08 07:43:53 +02004442/* Fill <info> with HAProxy global info. <info> is preallocated array of length
4443 * <len>. The length of the array must be INF_TOTAL_FIELDS. If this length is
4444 * less then this value, the function returns 0, otherwise, it returns 1. Some
4445 * fields' presence or precision may depend on some of the STAT_* flags present
4446 * in <flags>.
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004447 */
Willy Tarreau0b26b382021-05-08 07:43:53 +02004448int stats_fill_info(struct field *info, int len, uint flags)
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004449{
Willy Tarreaud37e26e2021-05-08 07:40:52 +02004450 struct timeval up;
Willy Tarreau83061a82018-07-13 11:56:34 +02004451 struct buffer *out = get_trash_chunk();
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004452
4453#ifdef USE_OPENSSL
Willy Tarreauc5977722021-05-08 08:14:04 +02004454 double ssl_sess_rate = read_freq_ctr_flt(&global.ssl_per_sec);
4455 double ssl_key_rate = read_freq_ctr_flt(&global.ssl_fe_keys_per_sec);
4456 double ssl_reuse = 0;
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004457
Willy Tarreauc5977722021-05-08 08:14:04 +02004458 if (ssl_key_rate < ssl_sess_rate)
4459 ssl_reuse = 100.0 * (1.0 - ssl_key_rate / ssl_sess_rate);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004460#endif
4461
Willy Tarreaud37e26e2021-05-08 07:40:52 +02004462 tv_remain(&start_date, &now, &up);
4463
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004464 if (len < INF_TOTAL_FIELDS)
4465 return 0;
4466
4467 chunk_reset(out);
4468 memset(info, 0, sizeof(*info) * len);
4469
4470 info[INF_NAME] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, PRODUCT_NAME);
Willy Tarreau909b9d82019-01-04 18:20:32 +01004471 info[INF_VERSION] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, haproxy_version);
Adis Nezirovicb62b78b2021-01-15 13:12:33 +01004472 info[INF_BUILD_INFO] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, haproxy_version);
Willy Tarreau909b9d82019-01-04 18:20:32 +01004473 info[INF_RELEASE_DATE] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, haproxy_date);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004474
Yves Lafon95317282018-02-26 11:10:37 +01004475 info[INF_NBTHREAD] = mkf_u32(FO_CONFIG|FS_SERVICE, global.nbthread);
Willy Tarreau91358592021-06-15 08:08:04 +02004476 info[INF_NBPROC] = mkf_u32(FO_CONFIG|FS_SERVICE, 1);
Willy Tarreaue8422bf2021-06-15 09:08:18 +02004477 info[INF_PROCESS_NUM] = mkf_u32(FO_KEY, 1);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004478 info[INF_PID] = mkf_u32(FO_STATUS, pid);
4479
4480 info[INF_UPTIME] = mkf_str(FN_DURATION, chunk_newstr(out));
Willy Tarreaud37e26e2021-05-08 07:40:52 +02004481 chunk_appendf(out, "%ud %uh%02um%02us", (uint)up.tv_sec / 86400, ((uint)up.tv_sec % 86400) / 3600, ((uint)up.tv_sec % 3600) / 60, ((uint)up.tv_sec % 60));
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004482
Willy Tarreaue8abc322021-05-08 07:56:56 +02004483 info[INF_UPTIME_SEC] = (flags & STAT_USE_FLOAT) ? mkf_flt(FN_DURATION, up.tv_sec + up.tv_usec / 1000000.0) : mkf_u32(FN_DURATION, up.tv_sec);
4484 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 +01004485 info[INF_MEMMAX_MB] = mkf_u32(FO_CONFIG|FN_LIMIT, global.rlimit_memmax);
William Dauchya8766cf2021-01-15 22:41:37 +01004486 info[INF_MEMMAX_BYTES] = mkf_u32(FO_CONFIG|FN_LIMIT, global.rlimit_memmax * 1048576L);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004487 info[INF_POOL_ALLOC_MB] = mkf_u32(0, (unsigned)(pool_total_allocated() / 1048576L));
William Dauchya8766cf2021-01-15 22:41:37 +01004488 info[INF_POOL_ALLOC_BYTES] = mkf_u32(0, pool_total_allocated());
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004489 info[INF_POOL_USED_MB] = mkf_u32(0, (unsigned)(pool_total_used() / 1048576L));
William Dauchya8766cf2021-01-15 22:41:37 +01004490 info[INF_POOL_USED_BYTES] = mkf_u32(0, pool_total_used());
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004491 info[INF_POOL_FAILED] = mkf_u32(FN_COUNTER, pool_total_failures());
4492 info[INF_ULIMIT_N] = mkf_u32(FO_CONFIG|FN_LIMIT, global.rlimit_nofile);
4493 info[INF_MAXSOCK] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxsock);
4494 info[INF_MAXCONN] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxconn);
4495 info[INF_HARD_MAXCONN] = mkf_u32(FO_CONFIG|FN_LIMIT, global.hardmaxconn);
4496 info[INF_CURR_CONN] = mkf_u32(0, actconn);
4497 info[INF_CUM_CONN] = mkf_u32(FN_COUNTER, totalconn);
4498 info[INF_CUM_REQ] = mkf_u32(FN_COUNTER, global.req_count);
4499#ifdef USE_OPENSSL
4500 info[INF_MAX_SSL_CONNS] = mkf_u32(FN_MAX, global.maxsslconn);
Willy Tarreau82531f62021-10-06 12:15:18 +02004501 info[INF_CURR_SSL_CONNS] = mkf_u32(0, global.sslconns);
4502 info[INF_CUM_SSL_CONNS] = mkf_u32(FN_COUNTER, global.totalsslconns);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004503#endif
4504 info[INF_MAXPIPES] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxpipes);
4505 info[INF_PIPES_USED] = mkf_u32(0, pipes_used);
4506 info[INF_PIPES_FREE] = mkf_u32(0, pipes_free);
Willy Tarreauc5977722021-05-08 08:14:04 +02004507 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 +01004508 info[INF_CONN_RATE_LIMIT] = mkf_u32(FO_CONFIG|FN_LIMIT, global.cps_lim);
4509 info[INF_MAX_CONN_RATE] = mkf_u32(FN_MAX, global.cps_max);
Willy Tarreauc5977722021-05-08 08:14:04 +02004510 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 +01004511 info[INF_SESS_RATE_LIMIT] = mkf_u32(FO_CONFIG|FN_LIMIT, global.sps_lim);
4512 info[INF_MAX_SESS_RATE] = mkf_u32(FN_RATE, global.sps_max);
4513
4514#ifdef USE_OPENSSL
Willy Tarreauc5977722021-05-08 08:14:04 +02004515 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 +01004516 info[INF_SSL_RATE_LIMIT] = mkf_u32(FO_CONFIG|FN_LIMIT, global.ssl_lim);
4517 info[INF_MAX_SSL_RATE] = mkf_u32(FN_MAX, global.ssl_max);
Willy Tarreauc5977722021-05-08 08:14:04 +02004518 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 +01004519 info[INF_SSL_FRONTEND_MAX_KEY_RATE] = mkf_u32(FN_MAX, global.ssl_fe_keys_max);
Willy Tarreauc5977722021-05-08 08:14:04 +02004520 info[INF_SSL_FRONTEND_SESSION_REUSE_PCT] = (flags & STAT_USE_FLOAT) ? mkf_flt(FN_RATE, ssl_reuse) : mkf_u32(0, ssl_reuse);
4521 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 +01004522 info[INF_SSL_BACKEND_MAX_KEY_RATE] = mkf_u32(FN_MAX, global.ssl_be_keys_max);
4523 info[INF_SSL_CACHE_LOOKUPS] = mkf_u32(FN_COUNTER, global.shctx_lookups);
4524 info[INF_SSL_CACHE_MISSES] = mkf_u32(FN_COUNTER, global.shctx_misses);
4525#endif
Willy Tarreauc5977722021-05-08 08:14:04 +02004526 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));
4527 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 +01004528 info[INF_COMPRESS_BPS_RATE_LIM] = mkf_u32(FO_CONFIG|FN_LIMIT, global.comp_rate_lim);
4529#ifdef USE_ZLIB
4530 info[INF_ZLIB_MEM_USAGE] = mkf_u32(0, zlib_used_memory);
4531 info[INF_MAX_ZLIB_MEM_USAGE] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxzlibmem);
4532#endif
Willy Tarreau955a11e2021-02-24 17:03:30 +01004533 info[INF_TASKS] = mkf_u32(0, total_allocated_tasks());
Willy Tarreau9c7b8082021-02-24 15:10:07 +01004534 info[INF_RUN_QUEUE] = mkf_u32(0, total_run_queues());
Willy Tarreauf9d5e102021-10-08 10:43:59 +02004535 info[INF_IDLE_PCT] = mkf_u32(FN_AVG, clock_report_idle());
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004536 info[INF_NODE] = mkf_str(FO_CONFIG|FN_OUTPUT|FS_SERVICE, global.node);
4537 if (global.desc)
4538 info[INF_DESCRIPTION] = mkf_str(FO_CONFIG|FN_OUTPUT|FS_SERVICE, global.desc);
Willy Tarreau00098ea2018-11-05 14:38:13 +01004539 info[INF_STOPPING] = mkf_u32(0, stopping);
4540 info[INF_JOBS] = mkf_u32(0, jobs);
William Lallemanda7199262018-11-16 16:57:20 +01004541 info[INF_UNSTOPPABLE_JOBS] = mkf_u32(0, unstoppable_jobs);
Willy Tarreau00098ea2018-11-05 14:38:13 +01004542 info[INF_LISTENERS] = mkf_u32(0, listeners);
Willy Tarreau199ad242018-11-05 16:31:22 +01004543 info[INF_ACTIVE_PEERS] = mkf_u32(0, active_peers);
Willy Tarreau2d372c22018-11-05 17:12:27 +01004544 info[INF_CONNECTED_PEERS] = mkf_u32(0, connected_peers);
Willy Tarreau13ef7732018-11-12 07:25:28 +01004545 info[INF_DROPPED_LOGS] = mkf_u32(0, dropped_logs);
Willy Tarreaubeb859a2018-11-22 18:07:59 +01004546 info[INF_BUSY_POLLING] = mkf_u32(0, !!(global.tune.options & GTUNE_BUSY_POLLING));
Emeric Brund30e9a12020-12-23 18:49:16 +01004547 info[INF_FAILED_RESOLUTIONS] = mkf_u32(0, resolv_failed_resolutions);
Willy Tarreau7cf0e452019-05-23 11:39:14 +02004548 info[INF_TOTAL_BYTES_OUT] = mkf_u64(0, global.out_bytes);
Christopher Fauletaaa70852020-07-10 13:56:30 +02004549 info[INF_TOTAL_SPLICED_BYTES_OUT] = mkf_u64(0, global.spliced_out_bytes);
Willy Tarreau7cf0e452019-05-23 11:39:14 +02004550 info[INF_BYTES_OUT_RATE] = mkf_u64(FN_RATE, (unsigned long long)read_freq_ctr(&global.out_32bps) * 32);
Willy Tarreau9b013702019-10-24 18:18:02 +02004551 info[INF_DEBUG_COMMANDS_ISSUED] = mkf_u32(0, debug_commands_issued);
Emeric Brun45c457a2020-07-09 23:23:34 +02004552 info[INF_CUM_LOG_MSGS] = mkf_u32(FN_COUNTER, cum_log_messages);
Amaury Denoyelle5dfdf3e2021-05-05 17:09:12 +02004553
4554 info[INF_TAINTED] = mkf_str(FO_STATUS, chunk_newstr(out));
4555 chunk_appendf(out, "%#x", get_tainted());
4556
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004557 return 1;
4558}
4559
Willy Tarreau4596fe22022-05-17 19:07:51 +02004560/* This function dumps information onto the stream connector's read buffer.
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004561 * It returns 0 as long as it does not complete, non-zero upon completion.
4562 * No state is used.
4563 */
Willy Tarreaucaff6312022-05-27 10:17:46 +02004564static int stats_dump_info_to_buffer(struct stconn *sc)
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004565{
Willy Tarreaucaff6312022-05-27 10:17:46 +02004566 struct appctx *appctx = __sc_appctx(sc);
Willy Tarreau91cefca2022-05-03 17:08:29 +02004567 struct show_stat_ctx *ctx = appctx->svcctx;
Christopher Faulet2da02ae2022-02-24 13:45:27 +01004568
Willy Tarreau91cefca2022-05-03 17:08:29 +02004569 if (!stats_fill_info(info, INF_TOTAL_FIELDS, ctx->flags))
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004570 return 0;
4571
4572 chunk_reset(&trash);
4573
Willy Tarreau91cefca2022-05-03 17:08:29 +02004574 if (ctx->flags & STAT_FMT_TYPED)
4575 stats_dump_typed_info_fields(&trash, info, ctx->flags);
4576 else if (ctx->flags & STAT_FMT_JSON)
4577 stats_dump_json_info_fields(&trash, info, ctx->flags);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004578 else
Willy Tarreau91cefca2022-05-03 17:08:29 +02004579 stats_dump_info_fields(&trash, info, ctx->flags);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004580
Willy Tarreaud0a06d52022-05-18 15:07:19 +02004581 if (applet_putchk(appctx, &trash) == -1)
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004582 return 0;
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004583
4584 return 1;
4585}
4586
Willy Tarreau4596fe22022-05-17 19:07:51 +02004587/* This function dumps the schema onto the stream connector's read buffer.
Simon Horman6f6bb382017-01-04 09:37:26 +01004588 * It returns 0 as long as it does not complete, non-zero upon completion.
4589 * No state is used.
4590 *
Ilya Shipitsinf38a0182020-12-21 01:16:17 +05004591 * Integer values bounded to the range [-(2**53)+1, (2**53)-1] as
Simon Horman6f6bb382017-01-04 09:37:26 +01004592 * per the recommendation for interoperable integers in section 6 of RFC 7159.
4593 */
Willy Tarreau83061a82018-07-13 11:56:34 +02004594static void stats_dump_json_schema(struct buffer *out)
Simon Horman6f6bb382017-01-04 09:37:26 +01004595{
4596
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004597 int old_len = out->data;
Simon Horman6f6bb382017-01-04 09:37:26 +01004598
4599 chunk_strcat(out,
4600 "{"
4601 "\"$schema\":\"http://json-schema.org/draft-04/schema#\","
4602 "\"oneOf\":["
4603 "{"
4604 "\"title\":\"Info\","
4605 "\"type\":\"array\","
4606 "\"items\":{"
Amaury Denoyellea53ce4c2020-10-02 18:31:59 +02004607 "\"title\":\"InfoItem\","
4608 "\"type\":\"object\","
Simon Horman6f6bb382017-01-04 09:37:26 +01004609 "\"properties\":{"
Simon Horman6f6bb382017-01-04 09:37:26 +01004610 "\"field\":{\"$ref\":\"#/definitions/field\"},"
4611 "\"processNum\":{\"$ref\":\"#/definitions/processNum\"},"
4612 "\"tags\":{\"$ref\":\"#/definitions/tags\"},"
4613 "\"value\":{\"$ref\":\"#/definitions/typedValue\"}"
4614 "},"
4615 "\"required\":[\"field\",\"processNum\",\"tags\","
4616 "\"value\"]"
4617 "}"
4618 "},"
4619 "{"
4620 "\"title\":\"Stat\","
4621 "\"type\":\"array\","
4622 "\"items\":{"
4623 "\"title\":\"InfoItem\","
4624 "\"type\":\"object\","
4625 "\"properties\":{"
4626 "\"objType\":{"
4627 "\"enum\":[\"Frontend\",\"Backend\",\"Listener\","
4628 "\"Server\",\"Unknown\"]"
4629 "},"
4630 "\"proxyId\":{"
4631 "\"type\":\"integer\","
4632 "\"minimum\":0"
4633 "},"
4634 "\"id\":{"
4635 "\"type\":\"integer\","
4636 "\"minimum\":0"
4637 "},"
4638 "\"field\":{\"$ref\":\"#/definitions/field\"},"
4639 "\"processNum\":{\"$ref\":\"#/definitions/processNum\"},"
4640 "\"tags\":{\"$ref\":\"#/definitions/tags\"},"
4641 "\"typedValue\":{\"$ref\":\"#/definitions/typedValue\"}"
4642 "},"
4643 "\"required\":[\"objType\",\"proxyId\",\"id\","
4644 "\"field\",\"processNum\",\"tags\","
4645 "\"value\"]"
4646 "}"
4647 "},"
4648 "{"
4649 "\"title\":\"Error\","
4650 "\"type\":\"object\","
4651 "\"properties\":{"
4652 "\"errorStr\":{"
4653 "\"type\":\"string\""
Amaury Denoyellea53ce4c2020-10-02 18:31:59 +02004654 "}"
4655 "},"
4656 "\"required\":[\"errorStr\"]"
Simon Horman6f6bb382017-01-04 09:37:26 +01004657 "}"
4658 "],"
4659 "\"definitions\":{"
4660 "\"field\":{"
4661 "\"type\":\"object\","
4662 "\"pos\":{"
4663 "\"type\":\"integer\","
4664 "\"minimum\":0"
4665 "},"
4666 "\"name\":{"
4667 "\"type\":\"string\""
4668 "},"
4669 "\"required\":[\"pos\",\"name\"]"
4670 "},"
4671 "\"processNum\":{"
4672 "\"type\":\"integer\","
4673 "\"minimum\":1"
4674 "},"
4675 "\"tags\":{"
4676 "\"type\":\"object\","
4677 "\"origin\":{"
4678 "\"type\":\"string\","
4679 "\"enum\":[\"Metric\",\"Status\",\"Key\","
4680 "\"Config\",\"Product\",\"Unknown\"]"
4681 "},"
4682 "\"nature\":{"
4683 "\"type\":\"string\","
4684 "\"enum\":[\"Gauge\",\"Limit\",\"Min\",\"Max\","
4685 "\"Rate\",\"Counter\",\"Duration\","
4686 "\"Age\",\"Time\",\"Name\",\"Output\","
4687 "\"Avg\", \"Unknown\"]"
4688 "},"
4689 "\"scope\":{"
4690 "\"type\":\"string\","
4691 "\"enum\":[\"Cluster\",\"Process\",\"Service\","
4692 "\"System\",\"Unknown\"]"
4693 "},"
4694 "\"required\":[\"origin\",\"nature\",\"scope\"]"
4695 "},"
4696 "\"typedValue\":{"
4697 "\"type\":\"object\","
4698 "\"oneOf\":["
4699 "{\"$ref\":\"#/definitions/typedValue/definitions/s32Value\"},"
4700 "{\"$ref\":\"#/definitions/typedValue/definitions/s64Value\"},"
4701 "{\"$ref\":\"#/definitions/typedValue/definitions/u32Value\"},"
4702 "{\"$ref\":\"#/definitions/typedValue/definitions/u64Value\"},"
4703 "{\"$ref\":\"#/definitions/typedValue/definitions/strValue\"}"
4704 "],"
4705 "\"definitions\":{"
4706 "\"s32Value\":{"
4707 "\"properties\":{"
4708 "\"type\":{"
4709 "\"type\":\"string\","
4710 "\"enum\":[\"s32\"]"
4711 "},"
4712 "\"value\":{"
4713 "\"type\":\"integer\","
4714 "\"minimum\":-2147483648,"
4715 "\"maximum\":2147483647"
4716 "}"
4717 "},"
4718 "\"required\":[\"type\",\"value\"]"
4719 "},"
4720 "\"s64Value\":{"
4721 "\"properties\":{"
4722 "\"type\":{"
4723 "\"type\":\"string\","
4724 "\"enum\":[\"s64\"]"
4725 "},"
4726 "\"value\":{"
4727 "\"type\":\"integer\","
4728 "\"minimum\":-9007199254740991,"
4729 "\"maximum\":9007199254740991"
4730 "}"
4731 "},"
4732 "\"required\":[\"type\",\"value\"]"
4733 "},"
4734 "\"u32Value\":{"
4735 "\"properties\":{"
4736 "\"type\":{"
4737 "\"type\":\"string\","
4738 "\"enum\":[\"u32\"]"
4739 "},"
4740 "\"value\":{"
4741 "\"type\":\"integer\","
4742 "\"minimum\":0,"
4743 "\"maximum\":4294967295"
4744 "}"
4745 "},"
4746 "\"required\":[\"type\",\"value\"]"
4747 "},"
4748 "\"u64Value\":{"
4749 "\"properties\":{"
4750 "\"type\":{"
4751 "\"type\":\"string\","
4752 "\"enum\":[\"u64\"]"
4753 "},"
4754 "\"value\":{"
4755 "\"type\":\"integer\","
4756 "\"minimum\":0,"
4757 "\"maximum\":9007199254740991"
4758 "}"
4759 "},"
4760 "\"required\":[\"type\",\"value\"]"
4761 "},"
4762 "\"strValue\":{"
4763 "\"properties\":{"
4764 "\"type\":{"
4765 "\"type\":\"string\","
4766 "\"enum\":[\"str\"]"
4767 "},"
4768 "\"value\":{\"type\":\"string\"}"
4769 "},"
4770 "\"required\":[\"type\",\"value\"]"
4771 "},"
4772 "\"unknownValue\":{"
4773 "\"properties\":{"
4774 "\"type\":{"
4775 "\"type\":\"integer\","
4776 "\"minimum\":0"
4777 "},"
4778 "\"value\":{"
4779 "\"type\":\"string\","
4780 "\"enum\":[\"unknown\"]"
4781 "}"
4782 "},"
4783 "\"required\":[\"type\",\"value\"]"
4784 "}"
4785 "}"
4786 "}"
4787 "}"
4788 "}");
4789
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004790 if (old_len == out->data) {
Simon Horman6f6bb382017-01-04 09:37:26 +01004791 chunk_reset(out);
4792 chunk_appendf(out,
4793 "{\"errorStr\":\"output buffer too short\"}");
4794 }
Willy Tarreau5a0e7ca2022-06-10 09:21:22 +02004795 chunk_appendf(out, "\n");
Simon Horman6f6bb382017-01-04 09:37:26 +01004796}
4797
Willy Tarreau4596fe22022-05-17 19:07:51 +02004798/* This function dumps the schema onto the stream connector's read buffer.
Simon Horman6f6bb382017-01-04 09:37:26 +01004799 * It returns 0 as long as it does not complete, non-zero upon completion.
4800 * No state is used.
4801 */
Willy Tarreaud0a06d52022-05-18 15:07:19 +02004802static int stats_dump_json_schema_to_buffer(struct appctx *appctx)
Simon Horman6f6bb382017-01-04 09:37:26 +01004803{
4804 chunk_reset(&trash);
4805
4806 stats_dump_json_schema(&trash);
4807
Willy Tarreaud0a06d52022-05-18 15:07:19 +02004808 if (applet_putchk(appctx, &trash) == -1)
Simon Horman6f6bb382017-01-04 09:37:26 +01004809 return 0;
Simon Horman6f6bb382017-01-04 09:37:26 +01004810
4811 return 1;
4812}
4813
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004814static int cli_parse_clear_counters(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau89d467c2016-11-23 11:02:40 +01004815{
4816 struct proxy *px;
4817 struct server *sv;
4818 struct listener *li;
Amaury Denoyelled3700a72020-10-05 11:49:43 +02004819 struct stats_module *mod;
Willy Tarreau89d467c2016-11-23 11:02:40 +01004820 int clrall = 0;
4821
4822 if (strcmp(args[2], "all") == 0)
4823 clrall = 1;
4824
4825 /* check permissions */
4826 if (!cli_has_level(appctx, ACCESS_LVL_OPER) ||
4827 (clrall && !cli_has_level(appctx, ACCESS_LVL_ADMIN)))
4828 return 1;
4829
Olivier Houchardfbc74e82017-11-24 16:54:05 +01004830 for (px = proxies_list; px; px = px->next) {
Willy Tarreau89d467c2016-11-23 11:02:40 +01004831 if (clrall) {
4832 memset(&px->be_counters, 0, sizeof(px->be_counters));
4833 memset(&px->fe_counters, 0, sizeof(px->fe_counters));
4834 }
4835 else {
4836 px->be_counters.conn_max = 0;
4837 px->be_counters.p.http.rps_max = 0;
4838 px->be_counters.sps_max = 0;
4839 px->be_counters.cps_max = 0;
4840 px->be_counters.nbpend_max = 0;
Christopher Fauletefb41f02019-11-08 14:53:15 +01004841 px->be_counters.qtime_max = 0;
4842 px->be_counters.ctime_max = 0;
4843 px->be_counters.dtime_max = 0;
4844 px->be_counters.ttime_max = 0;
Willy Tarreau89d467c2016-11-23 11:02:40 +01004845
4846 px->fe_counters.conn_max = 0;
4847 px->fe_counters.p.http.rps_max = 0;
4848 px->fe_counters.sps_max = 0;
4849 px->fe_counters.cps_max = 0;
Willy Tarreau89d467c2016-11-23 11:02:40 +01004850 }
4851
4852 for (sv = px->srv; sv; sv = sv->next)
4853 if (clrall)
4854 memset(&sv->counters, 0, sizeof(sv->counters));
4855 else {
4856 sv->counters.cur_sess_max = 0;
4857 sv->counters.nbpend_max = 0;
4858 sv->counters.sps_max = 0;
Christopher Fauletefb41f02019-11-08 14:53:15 +01004859 sv->counters.qtime_max = 0;
4860 sv->counters.ctime_max = 0;
4861 sv->counters.dtime_max = 0;
4862 sv->counters.ttime_max = 0;
Willy Tarreau89d467c2016-11-23 11:02:40 +01004863 }
4864
4865 list_for_each_entry(li, &px->conf.listeners, by_fe)
4866 if (li->counters) {
4867 if (clrall)
4868 memset(li->counters, 0, sizeof(*li->counters));
4869 else
4870 li->counters->conn_max = 0;
4871 }
4872 }
4873
4874 global.cps_max = 0;
4875 global.sps_max = 0;
Olivier Houchard00bc3cb2017-10-17 19:23:25 +02004876 global.ssl_max = 0;
4877 global.ssl_fe_keys_max = 0;
4878 global.ssl_be_keys_max = 0;
Willy Tarreaud80cb4e2018-01-20 19:30:13 +01004879
Amaury Denoyelled3700a72020-10-05 11:49:43 +02004880 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
4881 if (!mod->clearable && !clrall)
4882 continue;
4883
4884 for (px = proxies_list; px; px = px->next) {
4885 enum stats_domain_px_cap mod_cap = stats_px_get_cap(mod->domain_flags);
4886
4887 if (px->cap & PR_CAP_FE && mod_cap & STATS_PX_CAP_FE) {
4888 EXTRA_COUNTERS_INIT(px->extra_counters_fe,
4889 mod,
4890 mod->counters,
4891 mod->counters_size);
4892 }
4893
4894 if (px->cap & PR_CAP_BE && mod_cap & STATS_PX_CAP_BE) {
4895 EXTRA_COUNTERS_INIT(px->extra_counters_be,
4896 mod,
4897 mod->counters,
4898 mod->counters_size);
4899 }
4900
4901 if (mod_cap & STATS_PX_CAP_SRV) {
4902 for (sv = px->srv; sv; sv = sv->next) {
4903 EXTRA_COUNTERS_INIT(sv->extra_counters,
4904 mod,
4905 mod->counters,
4906 mod->counters_size);
4907 }
4908 }
4909
4910 if (mod_cap & STATS_PX_CAP_LI) {
4911 list_for_each_entry(li, &px->conf.listeners, by_fe) {
4912 EXTRA_COUNTERS_INIT(li->extra_counters,
4913 mod,
4914 mod->counters,
4915 mod->counters_size);
4916 }
4917 }
4918 }
4919 }
4920
Emeric Brunf8642ee2021-10-29 17:59:18 +02004921 resolv_stats_clear_counters(clrall, &stats_module_list[STATS_DOMAIN_RESOLVERS]);
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02004922
Willy Tarreaud80cb4e2018-01-20 19:30:13 +01004923 memset(activity, 0, sizeof(activity));
Willy Tarreau89d467c2016-11-23 11:02:40 +01004924 return 1;
4925}
4926
4927
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004928static int cli_parse_show_info(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004929{
Willy Tarreau91cefca2022-05-03 17:08:29 +02004930 struct show_stat_ctx *ctx = applet_reserve_svcctx(appctx, sizeof(*ctx));
Willy Tarreau2f397382019-10-09 11:43:59 +02004931 int arg = 2;
4932
Willy Tarreau91cefca2022-05-03 17:08:29 +02004933 ctx->scope_str = 0;
4934 ctx->scope_len = 0;
4935 ctx->flags = 0;
Willy Tarreaud25fc792016-12-16 12:33:47 +01004936
Willy Tarreau2f397382019-10-09 11:43:59 +02004937 while (*args[arg]) {
4938 if (strcmp(args[arg], "typed") == 0)
Willy Tarreau91cefca2022-05-03 17:08:29 +02004939 ctx->flags = (ctx->flags & ~STAT_FMT_MASK) | STAT_FMT_TYPED;
Willy Tarreau2f397382019-10-09 11:43:59 +02004940 else if (strcmp(args[arg], "json") == 0)
Willy Tarreau91cefca2022-05-03 17:08:29 +02004941 ctx->flags = (ctx->flags & ~STAT_FMT_MASK) | STAT_FMT_JSON;
Willy Tarreau2f397382019-10-09 11:43:59 +02004942 else if (strcmp(args[arg], "desc") == 0)
Willy Tarreau91cefca2022-05-03 17:08:29 +02004943 ctx->flags |= STAT_SHOW_FDESC;
Willy Tarreau27456202021-05-08 07:54:24 +02004944 else if (strcmp(args[arg], "float") == 0)
Willy Tarreau91cefca2022-05-03 17:08:29 +02004945 ctx->flags |= STAT_USE_FLOAT;
Willy Tarreau2f397382019-10-09 11:43:59 +02004946 arg++;
4947 }
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004948 return 0;
4949}
4950
4951
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004952static int cli_parse_show_stat(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau2b812e22016-11-22 16:18:05 +01004953{
Willy Tarreau91cefca2022-05-03 17:08:29 +02004954 struct show_stat_ctx *ctx = applet_reserve_svcctx(appctx, sizeof(*ctx));
Willy Tarreau2f397382019-10-09 11:43:59 +02004955 int arg = 2;
4956
Willy Tarreau91cefca2022-05-03 17:08:29 +02004957 ctx->scope_str = 0;
4958 ctx->scope_len = 0;
4959 ctx->flags = STAT_SHNODE | STAT_SHDESC;
Willy Tarreau578d6e42019-10-09 11:00:22 +02004960
Willy Tarreau0698c802022-05-11 14:09:57 +02004961 if ((strm_li(appctx_strm(appctx))->bind_conf->level & ACCESS_LVL_MASK) >= ACCESS_LVL_OPER)
Willy Tarreau91cefca2022-05-03 17:08:29 +02004962 ctx->flags |= STAT_SHLGNDS;
Willy Tarreaud25fc792016-12-16 12:33:47 +01004963
Amaury Denoyelle072f97e2020-10-05 11:49:37 +02004964 /* proxy is the default domain */
Willy Tarreau91cefca2022-05-03 17:08:29 +02004965 ctx->domain = STATS_DOMAIN_PROXY;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01004966 if (strcmp(args[arg], "domain") == 0) {
Amaury Denoyelle072f97e2020-10-05 11:49:37 +02004967 ++args;
4968
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01004969 if (strcmp(args[arg], "proxy") == 0) {
Amaury Denoyelle072f97e2020-10-05 11:49:37 +02004970 ++args;
Emeric Brunf8642ee2021-10-29 17:59:18 +02004971 } else if (strcmp(args[arg], "resolvers") == 0) {
Willy Tarreau91cefca2022-05-03 17:08:29 +02004972 ctx->domain = STATS_DOMAIN_RESOLVERS;
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02004973 ++args;
4974 } else {
Amaury Denoyelle072f97e2020-10-05 11:49:37 +02004975 return cli_err(appctx, "Invalid statistics domain.\n");
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02004976 }
Amaury Denoyelle072f97e2020-10-05 11:49:37 +02004977 }
4978
Willy Tarreau91cefca2022-05-03 17:08:29 +02004979 if (ctx->domain == STATS_DOMAIN_PROXY
Amaury Denoyelle072f97e2020-10-05 11:49:37 +02004980 && *args[arg] && *args[arg+1] && *args[arg+2]) {
Willy Tarreaua1b1ed52016-11-25 08:50:58 +01004981 struct proxy *px;
4982
Willy Tarreau2f397382019-10-09 11:43:59 +02004983 px = proxy_find_by_name(args[arg], 0, 0);
Willy Tarreaua1b1ed52016-11-25 08:50:58 +01004984 if (px)
Willy Tarreau91cefca2022-05-03 17:08:29 +02004985 ctx->iid = px->uuid;
Willy Tarreaua1b1ed52016-11-25 08:50:58 +01004986 else
Willy Tarreau91cefca2022-05-03 17:08:29 +02004987 ctx->iid = atoi(args[arg]);
Willy Tarreaua1b1ed52016-11-25 08:50:58 +01004988
Willy Tarreau91cefca2022-05-03 17:08:29 +02004989 if (!ctx->iid)
Willy Tarreau9d008692019-08-09 11:21:01 +02004990 return cli_err(appctx, "No such proxy.\n");
Willy Tarreaua1b1ed52016-11-25 08:50:58 +01004991
Willy Tarreau91cefca2022-05-03 17:08:29 +02004992 ctx->flags |= STAT_BOUND;
4993 ctx->type = atoi(args[arg+1]);
4994 ctx->sid = atoi(args[arg+2]);
Willy Tarreau2f397382019-10-09 11:43:59 +02004995 arg += 3;
4996 }
4997
4998 while (*args[arg]) {
4999 if (strcmp(args[arg], "typed") == 0)
Willy Tarreau91cefca2022-05-03 17:08:29 +02005000 ctx->flags = (ctx->flags & ~STAT_FMT_MASK) | STAT_FMT_TYPED;
Willy Tarreau2f397382019-10-09 11:43:59 +02005001 else if (strcmp(args[arg], "json") == 0)
Willy Tarreau91cefca2022-05-03 17:08:29 +02005002 ctx->flags = (ctx->flags & ~STAT_FMT_MASK) | STAT_FMT_JSON;
Willy Tarreau2f397382019-10-09 11:43:59 +02005003 else if (strcmp(args[arg], "desc") == 0)
Willy Tarreau91cefca2022-05-03 17:08:29 +02005004 ctx->flags |= STAT_SHOW_FDESC;
Willy Tarreau3e320362020-10-23 17:28:57 +02005005 else if (strcmp(args[arg], "no-maint") == 0)
Willy Tarreau91cefca2022-05-03 17:08:29 +02005006 ctx->flags |= STAT_HIDE_MAINT;
Willy Tarreau65141ff2020-10-23 17:19:48 +02005007 else if (strcmp(args[arg], "up") == 0)
Willy Tarreau91cefca2022-05-03 17:08:29 +02005008 ctx->flags |= STAT_HIDE_DOWN;
Willy Tarreau2f397382019-10-09 11:43:59 +02005009 arg++;
Willy Tarreau2b812e22016-11-22 16:18:05 +01005010 }
Willy Tarreau2b812e22016-11-22 16:18:05 +01005011
Willy Tarreau2b812e22016-11-22 16:18:05 +01005012 return 0;
5013}
5014
Willy Tarreau0baac8c2016-11-22 16:36:53 +01005015static int cli_io_handler_dump_info(struct appctx *appctx)
5016{
Willy Tarreauc12b3212022-05-27 11:08:15 +02005017 return stats_dump_info_to_buffer(appctx_sc(appctx));
Willy Tarreau0baac8c2016-11-22 16:36:53 +01005018}
5019
Willy Tarreau4596fe22022-05-17 19:07:51 +02005020/* This I/O handler runs as an applet embedded in a stream connector. It is
Willy Tarreau2b812e22016-11-22 16:18:05 +01005021 * used to send raw stats over a socket.
5022 */
5023static int cli_io_handler_dump_stat(struct appctx *appctx)
5024{
Willy Tarreauc12b3212022-05-27 11:08:15 +02005025 return stats_dump_stat_to_buffer(appctx_sc(appctx), NULL, NULL);
Willy Tarreau2b812e22016-11-22 16:18:05 +01005026}
5027
Simon Horman6f6bb382017-01-04 09:37:26 +01005028static int cli_io_handler_dump_json_schema(struct appctx *appctx)
5029{
Willy Tarreaud0a06d52022-05-18 15:07:19 +02005030 return stats_dump_json_schema_to_buffer(appctx);
Simon Horman6f6bb382017-01-04 09:37:26 +01005031}
5032
Amaury Denoyelle216a1ce2021-03-18 15:48:14 +01005033int stats_allocate_proxy_counters_internal(struct extra_counters **counters,
5034 int type, int px_cap)
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02005035{
5036 struct stats_module *mod;
5037
5038 EXTRA_COUNTERS_REGISTER(counters, type, alloc_failed);
5039
5040 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
5041 if (!(stats_px_get_cap(mod->domain_flags) & px_cap))
5042 continue;
5043
5044 EXTRA_COUNTERS_ADD(mod, *counters, mod->counters, mod->counters_size);
5045 }
5046
5047 EXTRA_COUNTERS_ALLOC(*counters, alloc_failed);
5048
5049 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
5050 if (!(stats_px_get_cap(mod->domain_flags) & px_cap))
5051 continue;
5052
5053 EXTRA_COUNTERS_INIT(*counters, mod, mod->counters, mod->counters_size);
5054 }
5055
5056 return 1;
5057
5058 alloc_failed:
5059 return 0;
5060}
5061
5062/* Initialize and allocate all extra counters for a proxy and its attached
5063 * servers/listeners with all already registered stats module
5064 */
5065int stats_allocate_proxy_counters(struct proxy *px)
5066{
5067 struct server *sv;
5068 struct listener *li;
5069
5070 if (px->cap & PR_CAP_FE) {
5071 if (!stats_allocate_proxy_counters_internal(&px->extra_counters_fe,
5072 COUNTERS_FE,
5073 STATS_PX_CAP_FE)) {
5074 return 0;
5075 }
5076 }
5077
5078 if (px->cap & PR_CAP_BE) {
5079 if (!stats_allocate_proxy_counters_internal(&px->extra_counters_be,
5080 COUNTERS_BE,
5081 STATS_PX_CAP_BE)) {
5082 return 0;
5083 }
5084 }
5085
5086 for (sv = px->srv; sv; sv = sv->next) {
5087 if (!stats_allocate_proxy_counters_internal(&sv->extra_counters,
5088 COUNTERS_SV,
5089 STATS_PX_CAP_SRV)) {
5090 return 0;
5091 }
5092 }
5093
5094 list_for_each_entry(li, &px->conf.listeners, by_fe) {
5095 if (!stats_allocate_proxy_counters_internal(&li->extra_counters,
5096 COUNTERS_LI,
5097 STATS_PX_CAP_LI)) {
5098 return 0;
5099 }
5100 }
5101
5102 return 1;
5103}
5104
Amaury Denoyelle58d395e2020-10-05 11:49:40 +02005105void stats_register_module(struct stats_module *m)
5106{
5107 const uint8_t domain = stats_get_domain(m->domain_flags);
5108
Willy Tarreau2b718102021-04-21 07:32:39 +02005109 LIST_APPEND(&stats_module_list[domain], &m->list);
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02005110 stat_count[domain] += m->stats_count;
Amaury Denoyelle58d395e2020-10-05 11:49:40 +02005111}
5112
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02005113static int allocate_stats_px_postcheck(void)
5114{
5115 struct stats_module *mod;
5116 size_t i = ST_F_TOTAL_FIELDS;
5117 int err_code = 0;
Amaury Denoyelle27373f72020-10-05 16:57:33 +02005118 struct proxy *px;
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02005119
5120 stat_count[STATS_DOMAIN_PROXY] += ST_F_TOTAL_FIELDS;
5121
5122 stat_f[STATS_DOMAIN_PROXY] = malloc(stat_count[STATS_DOMAIN_PROXY] * sizeof(struct name_desc));
5123 if (!stat_f[STATS_DOMAIN_PROXY]) {
5124 ha_alert("stats: cannot allocate all fields for proxy statistics\n");
5125 err_code |= ERR_ALERT | ERR_FATAL;
5126 return err_code;
5127 }
5128
5129 memcpy(stat_f[STATS_DOMAIN_PROXY], stat_fields,
5130 ST_F_TOTAL_FIELDS * sizeof(struct name_desc));
5131
5132 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
5133 memcpy(stat_f[STATS_DOMAIN_PROXY] + i,
5134 mod->stats,
5135 mod->stats_count * sizeof(struct name_desc));
5136 i += mod->stats_count;
5137 }
5138
Amaury Denoyelle27373f72020-10-05 16:57:33 +02005139 for (px = proxies_list; px; px = px->next) {
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02005140 if (!stats_allocate_proxy_counters(px)) {
5141 ha_alert("stats: cannot allocate all counters for proxy statistics\n");
5142 err_code |= ERR_ALERT | ERR_FATAL;
5143 return err_code;
5144 }
5145 }
5146
Christopher Fauletde79cd22021-01-06 07:41:56 +01005147 /* wait per-thread alloc to perform corresponding stat_l allocation */
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02005148
5149 return err_code;
5150}
5151
5152REGISTER_CONFIG_POSTPARSER("allocate-stats-px", allocate_stats_px_postcheck);
5153
Emeric Brunf8642ee2021-10-29 17:59:18 +02005154static int allocate_stats_rslv_postcheck(void)
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02005155{
5156 struct stats_module *mod;
5157 size_t i = 0;
5158 int err_code = 0;
5159
Emeric Brunf8642ee2021-10-29 17:59:18 +02005160 stat_f[STATS_DOMAIN_RESOLVERS] = malloc(stat_count[STATS_DOMAIN_RESOLVERS] * sizeof(struct name_desc));
5161 if (!stat_f[STATS_DOMAIN_RESOLVERS]) {
5162 ha_alert("stats: cannot allocate all fields for resolver statistics\n");
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02005163 err_code |= ERR_ALERT | ERR_FATAL;
5164 return err_code;
5165 }
5166
Emeric Brunf8642ee2021-10-29 17:59:18 +02005167 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_RESOLVERS], list) {
5168 memcpy(stat_f[STATS_DOMAIN_RESOLVERS] + i,
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02005169 mod->stats,
5170 mod->stats_count * sizeof(struct name_desc));
5171 i += mod->stats_count;
5172 }
5173
Emeric Brunf8642ee2021-10-29 17:59:18 +02005174 if (!resolv_allocate_counters(&stats_module_list[STATS_DOMAIN_RESOLVERS])) {
5175 ha_alert("stats: cannot allocate all counters for resolver statistics\n");
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02005176 err_code |= ERR_ALERT | ERR_FATAL;
5177 return err_code;
5178 }
5179
Christopher Fauletde79cd22021-01-06 07:41:56 +01005180 /* wait per-thread alloc to perform corresponding stat_l allocation */
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02005181
5182 return err_code;
5183}
5184
Emeric Brunf8642ee2021-10-29 17:59:18 +02005185REGISTER_CONFIG_POSTPARSER("allocate-stats-resolver", allocate_stats_rslv_postcheck);
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02005186
Christopher Fauletde79cd22021-01-06 07:41:56 +01005187static int allocate_stat_lines_per_thread(void)
5188{
Emeric Brunf8642ee2021-10-29 17:59:18 +02005189 int domains[] = { STATS_DOMAIN_PROXY, STATS_DOMAIN_RESOLVERS }, i;
Christopher Fauletde79cd22021-01-06 07:41:56 +01005190
5191 for (i = 0; i < STATS_DOMAIN_COUNT; ++i) {
5192 const int domain = domains[i];
5193
5194 stat_l[domain] = malloc(stat_count[domain] * sizeof(struct field));
5195 if (!stat_l[domain])
5196 return 0;
5197 }
5198 return 1;
5199}
5200
5201REGISTER_PER_THREAD_ALLOC(allocate_stat_lines_per_thread);
5202
Amaury Denoyelle7f8f6cb2020-11-10 14:24:31 +01005203static int allocate_trash_counters(void)
5204{
5205 struct stats_module *mod;
Emeric Brunf8642ee2021-10-29 17:59:18 +02005206 int domains[] = { STATS_DOMAIN_PROXY, STATS_DOMAIN_RESOLVERS }, i;
Amaury Denoyelle7f8f6cb2020-11-10 14:24:31 +01005207 size_t max_counters_size = 0;
5208
5209 /* calculate the greatest counters used by any stats modules */
5210 for (i = 0; i < STATS_DOMAIN_COUNT; ++i) {
5211 list_for_each_entry(mod, &stats_module_list[domains[i]], list) {
5212 max_counters_size = mod->counters_size > max_counters_size ?
5213 mod->counters_size : max_counters_size;
5214 }
5215 }
5216
5217 /* allocate the trash with the size of the greatest counters */
5218 if (max_counters_size) {
5219 trash_counters = malloc(max_counters_size);
5220 if (!trash_counters) {
5221 ha_alert("stats: cannot allocate trash counters for statistics\n");
5222 return 0;
5223 }
5224 }
5225
5226 return 1;
5227}
5228
5229REGISTER_PER_THREAD_ALLOC(allocate_trash_counters);
5230
Christopher Fauletde79cd22021-01-06 07:41:56 +01005231static void deinit_stat_lines_per_thread(void)
Amaury Denoyellea2a68992020-11-10 14:24:30 +01005232{
Emeric Brunf8642ee2021-10-29 17:59:18 +02005233 int domains[] = { STATS_DOMAIN_PROXY, STATS_DOMAIN_RESOLVERS }, i;
Amaury Denoyellea2a68992020-11-10 14:24:30 +01005234
5235 for (i = 0; i < STATS_DOMAIN_COUNT; ++i) {
5236 const int domain = domains[i];
5237
Willy Tarreau61cfdf42021-02-20 10:46:51 +01005238 ha_free(&stat_l[domain]);
Christopher Fauletde79cd22021-01-06 07:41:56 +01005239 }
5240}
5241
5242
5243REGISTER_PER_THREAD_FREE(deinit_stat_lines_per_thread);
5244
5245static void deinit_stats(void)
5246{
Emeric Brunf8642ee2021-10-29 17:59:18 +02005247 int domains[] = { STATS_DOMAIN_PROXY, STATS_DOMAIN_RESOLVERS }, i;
Christopher Fauletde79cd22021-01-06 07:41:56 +01005248
5249 for (i = 0; i < STATS_DOMAIN_COUNT; ++i) {
5250 const int domain = domains[i];
Amaury Denoyellea2a68992020-11-10 14:24:30 +01005251
5252 if (stat_f[domain])
5253 free(stat_f[domain]);
5254 }
5255}
5256
5257REGISTER_POST_DEINIT(deinit_stats);
5258
Amaury Denoyelle7f8f6cb2020-11-10 14:24:31 +01005259static void free_trash_counters(void)
5260{
5261 if (trash_counters)
5262 free(trash_counters);
5263}
5264
5265REGISTER_PER_THREAD_FREE(free_trash_counters);
5266
Willy Tarreau2b812e22016-11-22 16:18:05 +01005267/* register cli keywords */
5268static struct cli_kw_list cli_kws = {{ },{
Willy Tarreaub205bfd2021-05-07 11:38:37 +02005269 { { "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 +02005270 { { "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 +02005271 { { "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 },
5272 { { "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 +01005273 {{},}
5274}};
5275
Willy Tarreau0108d902018-11-25 19:14:37 +01005276INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
5277
William Lallemand74c24fb2016-11-21 17:18:36 +01005278struct applet http_stats_applet = {
5279 .obj_type = OBJ_TYPE_APPLET,
5280 .name = "<STATS>", /* used for logging */
5281 .fct = http_stats_io_handler,
5282 .release = NULL,
5283};
5284
William Lallemand74c24fb2016-11-21 17:18:36 +01005285/*
5286 * Local variables:
5287 * c-indent-level: 8
5288 * c-basic-offset: 8
5289 * End:
5290 */