blob: dad626172fbc100fe0bb953e6a730616d6d6f602 [file] [log] [blame]
William Lallemand74c24fb2016-11-21 17:18:36 +01001/*
2 * Functions dedicated to statistics output and the stats socket
3 *
4 * Copyright 2000-2012 Willy Tarreau <w@1wt.eu>
5 * Copyright 2007-2009 Krzysztof Piotr Oledzki <ole@ans.pl>
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version
10 * 2 of the License, or (at your option) any later version.
11 *
12 */
13
14#include <ctype.h>
15#include <errno.h>
William Lallemand74c24fb2016-11-21 17:18:36 +010016#include <stdio.h>
17#include <stdlib.h>
18#include <string.h>
19#include <pwd.h>
20#include <grp.h>
21
22#include <sys/socket.h>
23#include <sys/stat.h>
24#include <sys/types.h>
25
Willy Tarreaub2551052020-06-09 09:07:15 +020026#include <haproxy/api.h>
Willy Tarreau5d9ddc52021-10-06 19:54:09 +020027#include <haproxy/activity.h>
Willy Tarreau91cefca2022-05-03 17:08:29 +020028#include <haproxy/applet.h>
Willy Tarreau49801602020-06-04 22:50:02 +020029#include <haproxy/backend.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020030#include <haproxy/base64.h>
Willy Tarreau6be78492020-06-05 00:00:29 +020031#include <haproxy/cfgparse.h>
Willy Tarreauf1d32c42020-06-04 21:07:02 +020032#include <haproxy/channel.h>
Willy Tarreau4aa573d2020-06-04 18:21:56 +020033#include <haproxy/check.h>
Willy Tarreau83487a82020-06-04 20:19:54 +020034#include <haproxy/cli.h>
Willy Tarreau55542642021-10-08 09:33:24 +020035#include <haproxy/clock.h>
Willy Tarreau0a3bd392020-06-04 08:52:38 +020036#include <haproxy/compression.h>
Willy Tarreau2a83d602020-05-27 16:58:08 +020037#include <haproxy/debug.h>
Willy Tarreau36979d92020-06-05 17:27:29 +020038#include <haproxy/errors.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020039#include <haproxy/fd.h>
40#include <haproxy/freq_ctr.h>
Willy Tarreau762d7a52020-06-04 11:23:07 +020041#include <haproxy/frontend.h>
Willy Tarreauf268ee82020-06-04 17:05:57 +020042#include <haproxy/global.h>
Willy Tarreaucd72d8c2020-06-02 19:11:26 +020043#include <haproxy/http.h>
Willy Tarreaub7fc4c42021-10-06 18:56:42 +020044#include <haproxy/http_ana.h>
Willy Tarreau87735332020-06-04 09:08:41 +020045#include <haproxy/http_htx.h>
Willy Tarreau16f958c2020-06-03 08:44:35 +020046#include <haproxy/htx.h>
Willy Tarreau853b2972020-05-27 18:01:47 +020047#include <haproxy/list.h>
Willy Tarreau36979d92020-06-05 17:27:29 +020048#include <haproxy/listener.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020049#include <haproxy/log.h>
Willy Tarreau2cd58092020-06-04 15:10:43 +020050#include <haproxy/map-t.h>
Willy Tarreau225a90a2020-06-04 15:06:28 +020051#include <haproxy/pattern-t.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020052#include <haproxy/pipe.h>
53#include <haproxy/pool.h>
Willy Tarreaua264d962020-06-04 22:29:18 +020054#include <haproxy/proxy.h>
Emeric Brunc9437992021-02-12 19:42:55 +010055#include <haproxy/resolvers.h>
Willy Tarreau5edca2f2022-05-27 09:25:10 +020056#include <haproxy/sc_strm.h>
Willy Tarreau1e56f922020-06-04 23:20:13 +020057#include <haproxy/server.h>
Willy Tarreau48d25b32020-06-04 18:58:52 +020058#include <haproxy/session.h>
Willy Tarreau2eec9b52020-06-04 19:58:55 +020059#include <haproxy/stats.h>
Willy Tarreaucb086c62022-05-27 09:47:12 +020060#include <haproxy/stconn.h>
Willy Tarreaudfd3de82020-06-04 23:46:14 +020061#include <haproxy/stream.h>
Willy Tarreaucea0e1b2020-06-04 17:25:40 +020062#include <haproxy/task.h>
Willy Tarreauc2f7c582020-06-02 18:15:32 +020063#include <haproxy/ticks.h>
Willy Tarreau92b4f132020-06-01 11:05:15 +020064#include <haproxy/time.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020065#include <haproxy/tools.h>
Willy Tarreau8c42b8a2020-06-04 19:27:34 +020066#include <haproxy/uri_auth-t.h>
Willy Tarreaud6788052020-05-27 15:59:00 +020067#include <haproxy/version.h>
William Lallemand74c24fb2016-11-21 17:18:36 +010068
William Lallemand74c24fb2016-11-21 17:18:36 +010069
Christopher Faulet2b9b6782019-02-27 16:42:58 +010070/* status codes available for the stats admin page (strictly 4 chars length) */
71const char *stat_status_codes[STAT_STATUS_SIZE] = {
72 [STAT_STATUS_DENY] = "DENY",
73 [STAT_STATUS_DONE] = "DONE",
74 [STAT_STATUS_ERRP] = "ERRP",
75 [STAT_STATUS_EXCD] = "EXCD",
76 [STAT_STATUS_NONE] = "NONE",
77 [STAT_STATUS_PART] = "PART",
78 [STAT_STATUS_UNKN] = "UNKN",
79 [STAT_STATUS_IVAL] = "IVAL",
80};
81
Willy Tarreau0baac8c2016-11-22 16:36:53 +010082/* These are the field names for each INF_* field position. Please pay attention
83 * to always use the exact same name except that the strings for new names must
84 * be lower case or CamelCase while the enum entries must be upper case.
85 */
Willy Tarreaueaa55372019-10-09 07:39:11 +020086const struct name_desc info_fields[INF_TOTAL_FIELDS] = {
Willy Tarreau6d4897e2019-10-11 16:31:46 +020087 [INF_NAME] = { .name = "Name", .desc = "Product name" },
88 [INF_VERSION] = { .name = "Version", .desc = "Product version" },
89 [INF_RELEASE_DATE] = { .name = "Release_date", .desc = "Date of latest source code update" },
90 [INF_NBTHREAD] = { .name = "Nbthread", .desc = "Number of started threads (global.nbthread)" },
Willy Tarreau91358592021-06-15 08:08:04 +020091 [INF_NBPROC] = { .name = "Nbproc", .desc = "Number of started worker processes (historical, always 1)" },
Willy Tarreaue8422bf2021-06-15 09:08:18 +020092 [INF_PROCESS_NUM] = { .name = "Process_num", .desc = "Relative worker process number (1)" },
Willy Tarreau6d4897e2019-10-11 16:31:46 +020093 [INF_PID] = { .name = "Pid", .desc = "This worker process identifier for the system" },
94 [INF_UPTIME] = { .name = "Uptime", .desc = "How long ago this worker process was started (days+hours+minutes+seconds)" },
95 [INF_UPTIME_SEC] = { .name = "Uptime_sec", .desc = "How long ago this worker process was started (seconds)" },
William Dauchydefd1562021-01-15 22:41:38 +010096 [INF_START_TIME_SEC] = { .name = "Start_time_sec", .desc = "Start time in seconds" },
Willy Tarreau6d4897e2019-10-11 16:31:46 +020097 [INF_MEMMAX_MB] = { .name = "Memmax_MB", .desc = "Worker process's hard limit on memory usage in MB (-m on command line)" },
William Dauchya8766cf2021-01-15 22:41:37 +010098 [INF_MEMMAX_BYTES] = { .name = "Memmax_bytes", .desc = "Worker process's hard limit on memory usage in byes (-m on command line)" },
Willy Tarreau6d4897e2019-10-11 16:31:46 +020099 [INF_POOL_ALLOC_MB] = { .name = "PoolAlloc_MB", .desc = "Amount of memory allocated in pools (in MB)" },
William Dauchya8766cf2021-01-15 22:41:37 +0100100 [INF_POOL_ALLOC_BYTES] = { .name = "PoolAlloc_bytes", .desc = "Amount of memory allocated in pools (in bytes)" },
Willy Tarreau6d4897e2019-10-11 16:31:46 +0200101 [INF_POOL_USED_MB] = { .name = "PoolUsed_MB", .desc = "Amount of pool memory currently used (in MB)" },
William Dauchya8766cf2021-01-15 22:41:37 +0100102 [INF_POOL_USED_BYTES] = { .name = "PoolUsed_bytes", .desc = "Amount of pool memory currently used (in bytes)" },
Willy Tarreau6d4897e2019-10-11 16:31:46 +0200103 [INF_POOL_FAILED] = { .name = "PoolFailed", .desc = "Number of failed pool allocations since this worker was started" },
104 [INF_ULIMIT_N] = { .name = "Ulimit-n", .desc = "Hard limit on the number of per-process file descriptors" },
105 [INF_MAXSOCK] = { .name = "Maxsock", .desc = "Hard limit on the number of per-process sockets" },
106 [INF_MAXCONN] = { .name = "Maxconn", .desc = "Hard limit on the number of per-process connections (configured or imposed by Ulimit-n)" },
107 [INF_HARD_MAXCONN] = { .name = "Hard_maxconn", .desc = "Hard limit on the number of per-process connections (imposed by Memmax_MB or Ulimit-n)" },
108 [INF_CURR_CONN] = { .name = "CurrConns", .desc = "Current number of connections on this worker process" },
109 [INF_CUM_CONN] = { .name = "CumConns", .desc = "Total number of connections on this worker process since started" },
110 [INF_CUM_REQ] = { .name = "CumReq", .desc = "Total number of requests on this worker process since started" },
111 [INF_MAX_SSL_CONNS] = { .name = "MaxSslConns", .desc = "Hard limit on the number of per-process SSL endpoints (front+back), 0=unlimited" },
112 [INF_CURR_SSL_CONNS] = { .name = "CurrSslConns", .desc = "Current number of SSL endpoints on this worker process (front+back)" },
113 [INF_CUM_SSL_CONNS] = { .name = "CumSslConns", .desc = "Total number of SSL endpoints on this worker process since started (front+back)" },
114 [INF_MAXPIPES] = { .name = "Maxpipes", .desc = "Hard limit on the number of pipes for splicing, 0=unlimited" },
115 [INF_PIPES_USED] = { .name = "PipesUsed", .desc = "Current number of pipes in use in this worker process" },
116 [INF_PIPES_FREE] = { .name = "PipesFree", .desc = "Current number of allocated and available pipes in this worker process" },
117 [INF_CONN_RATE] = { .name = "ConnRate", .desc = "Number of front connections created on this worker process over the last second" },
118 [INF_CONN_RATE_LIMIT] = { .name = "ConnRateLimit", .desc = "Hard limit for ConnRate (global.maxconnrate)" },
119 [INF_MAX_CONN_RATE] = { .name = "MaxConnRate", .desc = "Highest ConnRate reached on this worker process since started (in connections per second)" },
120 [INF_SESS_RATE] = { .name = "SessRate", .desc = "Number of sessions created on this worker process over the last second" },
121 [INF_SESS_RATE_LIMIT] = { .name = "SessRateLimit", .desc = "Hard limit for SessRate (global.maxsessrate)" },
122 [INF_MAX_SESS_RATE] = { .name = "MaxSessRate", .desc = "Highest SessRate reached on this worker process since started (in sessions per second)" },
123 [INF_SSL_RATE] = { .name = "SslRate", .desc = "Number of SSL connections created on this worker process over the last second" },
124 [INF_SSL_RATE_LIMIT] = { .name = "SslRateLimit", .desc = "Hard limit for SslRate (global.maxsslrate)" },
125 [INF_MAX_SSL_RATE] = { .name = "MaxSslRate", .desc = "Highest SslRate reached on this worker process since started (in connections per second)" },
126 [INF_SSL_FRONTEND_KEY_RATE] = { .name = "SslFrontendKeyRate", .desc = "Number of SSL keys created on frontends in this worker process over the last second" },
127 [INF_SSL_FRONTEND_MAX_KEY_RATE] = { .name = "SslFrontendMaxKeyRate", .desc = "Highest SslFrontendKeyRate reached on this worker process since started (in SSL keys per second)" },
128 [INF_SSL_FRONTEND_SESSION_REUSE_PCT] = { .name = "SslFrontendSessionReuse_pct", .desc = "Percent of frontend SSL connections which did not require a new key" },
129 [INF_SSL_BACKEND_KEY_RATE] = { .name = "SslBackendKeyRate", .desc = "Number of SSL keys created on backends in this worker process over the last second" },
130 [INF_SSL_BACKEND_MAX_KEY_RATE] = { .name = "SslBackendMaxKeyRate", .desc = "Highest SslBackendKeyRate reached on this worker process since started (in SSL keys per second)" },
131 [INF_SSL_CACHE_LOOKUPS] = { .name = "SslCacheLookups", .desc = "Total number of SSL session ID lookups in the SSL session cache on this worker since started" },
132 [INF_SSL_CACHE_MISSES] = { .name = "SslCacheMisses", .desc = "Total number of SSL session ID lookups that didn't find a session in the SSL session cache on this worker since started" },
133 [INF_COMPRESS_BPS_IN] = { .name = "CompressBpsIn", .desc = "Number of bytes submitted to the HTTP compressor in this worker process over the last second" },
134 [INF_COMPRESS_BPS_OUT] = { .name = "CompressBpsOut", .desc = "Number of bytes emitted by the HTTP compressor in this worker process over the last second" },
135 [INF_COMPRESS_BPS_RATE_LIM] = { .name = "CompressBpsRateLim", .desc = "Limit of CompressBpsOut beyond which HTTP compression is automatically disabled" },
136 [INF_ZLIB_MEM_USAGE] = { .name = "ZlibMemUsage", .desc = "Amount of memory currently used by HTTP compression on the current worker process (in bytes)" },
137 [INF_MAX_ZLIB_MEM_USAGE] = { .name = "MaxZlibMemUsage", .desc = "Limit on the amount of memory used by HTTP compression above which it is automatically disabled (in bytes, see global.maxzlibmem)" },
138 [INF_TASKS] = { .name = "Tasks", .desc = "Total number of tasks in the current worker process (active + sleeping)" },
139 [INF_RUN_QUEUE] = { .name = "Run_queue", .desc = "Total number of active tasks+tasklets in the current worker process" },
140 [INF_IDLE_PCT] = { .name = "Idle_pct", .desc = "Percentage of last second spent waiting in the current worker thread" },
141 [INF_NODE] = { .name = "node", .desc = "Node name (global.node)" },
142 [INF_DESCRIPTION] = { .name = "description", .desc = "Node description (global.description)" },
143 [INF_STOPPING] = { .name = "Stopping", .desc = "1 if the worker process is currently stopping, otherwise zero" },
144 [INF_JOBS] = { .name = "Jobs", .desc = "Current number of active jobs on the current worker process (frontend connections, master connections, listeners)" },
145 [INF_UNSTOPPABLE_JOBS] = { .name = "Unstoppable Jobs", .desc = "Current number of unstoppable jobs on the current worker process (master connections)" },
146 [INF_LISTENERS] = { .name = "Listeners", .desc = "Current number of active listeners on the current worker process" },
147 [INF_ACTIVE_PEERS] = { .name = "ActivePeers", .desc = "Current number of verified active peers connections on the current worker process" },
148 [INF_CONNECTED_PEERS] = { .name = "ConnectedPeers", .desc = "Current number of peers having passed the connection step on the current worker process" },
149 [INF_DROPPED_LOGS] = { .name = "DroppedLogs", .desc = "Total number of dropped logs for current worker process since started" },
150 [INF_BUSY_POLLING] = { .name = "BusyPolling", .desc = "1 if busy-polling is currently in use on the worker process, otherwise zero (config.busy-polling)" },
151 [INF_FAILED_RESOLUTIONS] = { .name = "FailedResolutions", .desc = "Total number of failed DNS resolutions in current worker process since started" },
152 [INF_TOTAL_BYTES_OUT] = { .name = "TotalBytesOut", .desc = "Total number of bytes emitted by current worker process since started" },
Christopher Fauletaaa70852020-07-10 13:56:30 +0200153 [INF_TOTAL_SPLICED_BYTES_OUT] = { .name = "TotalSplicdedBytesOut", .desc = "Total number of bytes emitted by current worker process through a kernel pipe since started" },
Willy Tarreau6d4897e2019-10-11 16:31:46 +0200154 [INF_BYTES_OUT_RATE] = { .name = "BytesOutRate", .desc = "Number of bytes emitted by current worker process over the last second" },
Willy Tarreau9b013702019-10-24 18:18:02 +0200155 [INF_DEBUG_COMMANDS_ISSUED] = { .name = "DebugCommandsIssued", .desc = "Number of debug commands issued on this process (anything > 0 is unsafe)" },
Emeric Brun45c457a2020-07-09 23:23:34 +0200156 [INF_CUM_LOG_MSGS] = { .name = "CumRecvLogs", .desc = "Total number of log messages received by log-forwarding listeners on this worker process since started" },
Adis Nezirovicb62b78b2021-01-15 13:12:33 +0100157 [INF_BUILD_INFO] = { .name = "Build info", .desc = "Build info" },
Amaury Denoyelle5dfdf3e2021-05-05 17:09:12 +0200158 [INF_TAINTED] = { .name = "Tainted", .desc = "Experimental features used" },
Willy Tarreau0baac8c2016-11-22 16:36:53 +0100159};
160
Willy Tarreaueaa55372019-10-09 07:39:11 +0200161const struct name_desc stat_fields[ST_F_TOTAL_FIELDS] = {
Willy Tarreau6d4897e2019-10-11 16:31:46 +0200162 [ST_F_PXNAME] = { .name = "pxname", .desc = "Proxy name" },
163 [ST_F_SVNAME] = { .name = "svname", .desc = "Server name" },
William Dauchyeedb9b12021-02-01 13:11:53 +0100164 [ST_F_QCUR] = { .name = "qcur", .desc = "Number of current queued connections" },
165 [ST_F_QMAX] = { .name = "qmax", .desc = "Highest value of queued connections encountered since process started" },
William Dauchy19f7cfc2021-02-01 13:11:54 +0100166 [ST_F_SCUR] = { .name = "scur", .desc = "Number of current sessions on the frontend, backend or server" },
167 [ST_F_SMAX] = { .name = "smax", .desc = "Highest value of current sessions encountered since process started" },
Willy Tarreau6d4897e2019-10-11 16:31:46 +0200168 [ST_F_SLIM] = { .name = "slim", .desc = "Frontend/listener/server's maxconn, backend's fullconn" },
169 [ST_F_STOT] = { .name = "stot", .desc = "Total number of sessions since process started" },
170 [ST_F_BIN] = { .name = "bin", .desc = "Total number of request bytes since process started" },
171 [ST_F_BOUT] = { .name = "bout", .desc = "Total number of response bytes since process started" },
172 [ST_F_DREQ] = { .name = "dreq", .desc = "Total number of denied requests since process started" },
173 [ST_F_DRESP] = { .name = "dresp", .desc = "Total number of denied responses since process started" },
174 [ST_F_EREQ] = { .name = "ereq", .desc = "Total number of invalid requests since process started" },
175 [ST_F_ECON] = { .name = "econ", .desc = "Total number of failed connections to server since the worker process started" },
176 [ST_F_ERESP] = { .name = "eresp", .desc = "Total number of invalid responses since the worker process started" },
177 [ST_F_WRETR] = { .name = "wretr", .desc = "Total number of server connection retries since the worker process started" },
178 [ST_F_WREDIS] = { .name = "wredis", .desc = "Total number of server redispatches due to connection failures since the worker process started" },
179 [ST_F_STATUS] = { .name = "status", .desc = "Frontend/listen status: OPEN/WAITING/FULL/STOP; backend: UP/DOWN; server: last check status" },
Willy Tarreaubd715102020-10-23 22:44:30 +0200180 [ST_F_WEIGHT] = { .name = "weight", .desc = "Server's effective weight, or sum of active servers' effective weights for a backend" },
Willy Tarreau6d4897e2019-10-11 16:31:46 +0200181 [ST_F_ACT] = { .name = "act", .desc = "Total number of active UP servers with a non-zero weight" },
182 [ST_F_BCK] = { .name = "bck", .desc = "Total number of backup UP servers with a non-zero weight" },
183 [ST_F_CHKFAIL] = { .name = "chkfail", .desc = "Total number of failed individual health checks per server/backend, since the worker process started" },
184 [ST_F_CHKDOWN] = { .name = "chkdown", .desc = "Total number of failed checks causing UP to DOWN server transitions, per server/backend, since the worker process started" },
185 [ST_F_LASTCHG] = { .name = "lastchg", .desc = "How long ago the last server state changed, in seconds" },
186 [ST_F_DOWNTIME] = { .name = "downtime", .desc = "Total time spent in DOWN state, for server or backend" },
187 [ST_F_QLIMIT] = { .name = "qlimit", .desc = "Limit on the number of connections in queue, for servers only (maxqueue argument)" },
Willy Tarreaue8422bf2021-06-15 09:08:18 +0200188 [ST_F_PID] = { .name = "pid", .desc = "Relative worker process number (1)" },
Willy Tarreau6d4897e2019-10-11 16:31:46 +0200189 [ST_F_IID] = { .name = "iid", .desc = "Frontend or Backend numeric identifier ('id' setting)" },
190 [ST_F_SID] = { .name = "sid", .desc = "Server numeric identifier ('id' setting)" },
191 [ST_F_THROTTLE] = { .name = "throttle", .desc = "Throttling ratio applied to a server's maxconn and weight during the slowstart period (0 to 100%)" },
192 [ST_F_LBTOT] = { .name = "lbtot", .desc = "Total number of requests routed by load balancing since the worker process started (ignores queue pop and stickiness)" },
193 [ST_F_TRACKED] = { .name = "tracked", .desc = "Name of the other server this server tracks for its state" },
194 [ST_F_TYPE] = { .name = "type", .desc = "Type of the object (Listener, Frontend, Backend, Server)" },
195 [ST_F_RATE] = { .name = "rate", .desc = "Total number of sessions processed by this object over the last second (sessions for listeners/frontends, requests for backends/servers)" },
196 [ST_F_RATE_LIM] = { .name = "rate_lim", .desc = "Limit on the number of sessions accepted in a second (frontend only, 'rate-limit sessions' setting)" },
William Dauchy19f7cfc2021-02-01 13:11:54 +0100197 [ST_F_RATE_MAX] = { .name = "rate_max", .desc = "Highest value of sessions per second observed since the worker process started" },
Willy Tarreau6d4897e2019-10-11 16:31:46 +0200198 [ST_F_CHECK_STATUS] = { .name = "check_status", .desc = "Status report of the server's latest health check, prefixed with '*' if a check is currently in progress" },
199 [ST_F_CHECK_CODE] = { .name = "check_code", .desc = "HTTP/SMTP/LDAP status code reported by the latest server health check" },
200 [ST_F_CHECK_DURATION] = { .name = "check_duration", .desc = "Total duration of the latest server health check, in milliseconds" },
201 [ST_F_HRSP_1XX] = { .name = "hrsp_1xx", .desc = "Total number of HTTP responses with status 100-199 returned by this object since the worker process started" },
202 [ST_F_HRSP_2XX] = { .name = "hrsp_2xx", .desc = "Total number of HTTP responses with status 200-299 returned by this object since the worker process started" },
203 [ST_F_HRSP_3XX] = { .name = "hrsp_3xx", .desc = "Total number of HTTP responses with status 300-399 returned by this object since the worker process started" },
204 [ST_F_HRSP_4XX] = { .name = "hrsp_4xx", .desc = "Total number of HTTP responses with status 400-499 returned by this object since the worker process started" },
205 [ST_F_HRSP_5XX] = { .name = "hrsp_5xx", .desc = "Total number of HTTP responses with status 500-599 returned by this object since the worker process started" },
206 [ST_F_HRSP_OTHER] = { .name = "hrsp_other", .desc = "Total number of HTTP responses with status <100, >599 returned by this object since the worker process started (error -1 included)" },
207 [ST_F_HANAFAIL] = { .name = "hanafail", .desc = "Total number of failed checks caused by an 'on-error' directive after an 'observe' condition matched" },
208 [ST_F_REQ_RATE] = { .name = "req_rate", .desc = "Number of HTTP requests processed over the last second on this object" },
William Dauchy19f7cfc2021-02-01 13:11:54 +0100209 [ST_F_REQ_RATE_MAX] = { .name = "req_rate_max", .desc = "Highest value of http requests observed since the worker process started" },
Willy Tarreau6d4897e2019-10-11 16:31:46 +0200210 [ST_F_REQ_TOT] = { .name = "req_tot", .desc = "Total number of HTTP requests processed by this object since the worker process started" },
211 [ST_F_CLI_ABRT] = { .name = "cli_abrt", .desc = "Total number of requests or connections aborted by the client since the worker process started" },
212 [ST_F_SRV_ABRT] = { .name = "srv_abrt", .desc = "Total number of requests or connections aborted by the server since the worker process started" },
213 [ST_F_COMP_IN] = { .name = "comp_in", .desc = "Total number of bytes submitted to the HTTP compressor for this object since the worker process started" },
214 [ST_F_COMP_OUT] = { .name = "comp_out", .desc = "Total number of bytes emitted by the HTTP compressor for this object since the worker process started" },
215 [ST_F_COMP_BYP] = { .name = "comp_byp", .desc = "Total number of bytes that bypassed HTTP compression for this object since the worker process started (CPU/memory/bandwidth limitation)" },
216 [ST_F_COMP_RSP] = { .name = "comp_rsp", .desc = "Total number of HTTP responses that were compressed for this object since the worker process started" },
217 [ST_F_LASTSESS] = { .name = "lastsess", .desc = "How long ago some traffic was seen on this object on this worker process, in seconds" },
218 [ST_F_LAST_CHK] = { .name = "last_chk", .desc = "Short description of the latest health check report for this server (see also check_desc)" },
219 [ST_F_LAST_AGT] = { .name = "last_agt", .desc = "Short description of the latest agent check report for this server (see also agent_desc)" },
220 [ST_F_QTIME] = { .name = "qtime", .desc = "Time spent in the queue, in milliseconds, averaged over the 1024 last requests (backend/server)" },
221 [ST_F_CTIME] = { .name = "ctime", .desc = "Time spent waiting for a connection to complete, in milliseconds, averaged over the 1024 last requests (backend/server)" },
222 [ST_F_RTIME] = { .name = "rtime", .desc = "Time spent waiting for a server response, in milliseconds, averaged over the 1024 last requests (backend/server)" },
223 [ST_F_TTIME] = { .name = "ttime", .desc = "Total request+response time (request+queue+connect+response+processing), in milliseconds, averaged over the 1024 last requests (backend/server)" },
224 [ST_F_AGENT_STATUS] = { .name = "agent_status", .desc = "Status report of the server's latest agent check, prefixed with '*' if a check is currently in progress" },
225 [ST_F_AGENT_CODE] = { .name = "agent_code", .desc = "Status code reported by the latest server agent check" },
226 [ST_F_AGENT_DURATION] = { .name = "agent_duration", .desc = "Total duration of the latest server agent check, in milliseconds" },
227 [ST_F_CHECK_DESC] = { .name = "check_desc", .desc = "Textual description of the latest health check report for this server" },
228 [ST_F_AGENT_DESC] = { .name = "agent_desc", .desc = "Textual description of the latest agent check report for this server" },
229 [ST_F_CHECK_RISE] = { .name = "check_rise", .desc = "Number of successful health checks before declaring a server UP (server 'rise' setting)" },
230 [ST_F_CHECK_FALL] = { .name = "check_fall", .desc = "Number of failed health checks before declaring a server DOWN (server 'fall' setting)" },
231 [ST_F_CHECK_HEALTH] = { .name = "check_health", .desc = "Current server health check level (0..fall-1=DOWN, fall..rise-1=UP)" },
232 [ST_F_AGENT_RISE] = { .name = "agent_rise", .desc = "Number of successful agent checks before declaring a server UP (server 'rise' setting)" },
233 [ST_F_AGENT_FALL] = { .name = "agent_fall", .desc = "Number of failed agent checks before declaring a server DOWN (server 'fall' setting)" },
234 [ST_F_AGENT_HEALTH] = { .name = "agent_health", .desc = "Current server agent check level (0..fall-1=DOWN, fall..rise-1=UP)" },
235 [ST_F_ADDR] = { .name = "addr", .desc = "Server's address:port, shown only if show-legends is set, or at levels oper/admin for the CLI" },
236 [ST_F_COOKIE] = { .name = "cookie", .desc = "Backend's cookie name or Server's cookie value, shown only if show-legends is set, or at levels oper/admin for the CLI" },
237 [ST_F_MODE] = { .name = "mode", .desc = "'mode' setting (tcp/http/health/cli)" },
238 [ST_F_ALGO] = { .name = "algo", .desc = "Backend's load balancing algorithm, shown only if show-legends is set, or at levels oper/admin for the CLI" },
239 [ST_F_CONN_RATE] = { .name = "conn_rate", .desc = "Number of new connections accepted over the last second on the frontend for this worker process" },
William Dauchy19f7cfc2021-02-01 13:11:54 +0100240 [ST_F_CONN_RATE_MAX] = { .name = "conn_rate_max", .desc = "Highest value of connections per second observed since the worker process started" },
Willy Tarreau6d4897e2019-10-11 16:31:46 +0200241 [ST_F_CONN_TOT] = { .name = "conn_tot", .desc = "Total number of new connections accepted on this frontend since the worker process started" },
242 [ST_F_INTERCEPTED] = { .name = "intercepted", .desc = "Total number of HTTP requests intercepted on the frontend (redirects/stats/services) since the worker process started" },
243 [ST_F_DCON] = { .name = "dcon", .desc = "Total number of incoming connections blocked on a listener/frontend by a tcp-request connection rule since the worker process started" },
244 [ST_F_DSES] = { .name = "dses", .desc = "Total number of incoming sessions blocked on a listener/frontend by a tcp-request connection rule since the worker process started" },
245 [ST_F_WREW] = { .name = "wrew", .desc = "Total number of failed HTTP header rewrites since the worker process started" },
246 [ST_F_CONNECT] = { .name = "connect", .desc = "Total number of outgoing connection attempts on this backend/server since the worker process started" },
247 [ST_F_REUSE] = { .name = "reuse", .desc = "Total number of reused connection on this backend/server since the worker process started" },
248 [ST_F_CACHE_LOOKUPS] = { .name = "cache_lookups", .desc = "Total number of HTTP requests looked up in the cache on this frontend/backend since the worker process started" },
249 [ST_F_CACHE_HITS] = { .name = "cache_hits", .desc = "Total number of HTTP requests not found in the cache on this frontend/backend since the worker process started" },
250 [ST_F_SRV_ICUR] = { .name = "srv_icur", .desc = "Current number of idle connections available for reuse on this server" },
251 [ST_F_SRV_ILIM] = { .name = "src_ilim", .desc = "Limit on the number of available idle connections on this server (server 'pool_max_conn' directive)" },
Christopher Faulet0d1c2a62019-11-08 14:59:51 +0100252 [ST_F_QT_MAX] = { .name = "qtime_max", .desc = "Maximum observed time spent in the queue, in milliseconds (backend/server)" },
253 [ST_F_CT_MAX] = { .name = "ctime_max", .desc = "Maximum observed time spent waiting for a connection to complete, in milliseconds (backend/server)" },
254 [ST_F_RT_MAX] = { .name = "rtime_max", .desc = "Maximum observed time spent waiting for a server response, in milliseconds (backend/server)" },
255 [ST_F_TT_MAX] = { .name = "ttime_max", .desc = "Maximum observed total request+response time (request+queue+connect+response+processing), in milliseconds (backend/server)" },
Christopher Faulet0159ee42019-12-16 14:40:39 +0100256 [ST_F_EINT] = { .name = "eint", .desc = "Total number of internal errors since process started"},
Willy Tarreau3bb617c2020-06-29 13:51:05 +0200257 [ST_F_IDLE_CONN_CUR] = { .name = "idle_conn_cur", .desc = "Current number of unsafe idle connections"},
258 [ST_F_SAFE_CONN_CUR] = { .name = "safe_conn_cur", .desc = "Current number of safe idle connections"},
259 [ST_F_USED_CONN_CUR] = { .name = "used_conn_cur", .desc = "Current number of connections in use"},
Willy Tarreaua9fcecb2020-06-29 15:38:53 +0200260 [ST_F_NEED_CONN_EST] = { .name = "need_conn_est", .desc = "Estimated needed number of connections"},
Willy Tarreaubd715102020-10-23 22:44:30 +0200261 [ST_F_UWEIGHT] = { .name = "uweight", .desc = "Server's user weight, or sum of active servers' user weights for a backend" },
Cedric Paillet7d6644e2022-12-08 09:17:00 +0000262 [ST_F_AGG_SRV_CHECK_STATUS] = { .name = "agg_server_check_status", .desc = "[DEPRECATED] Backend's aggregated gauge of servers' status" },
263 [ST_F_AGG_SRV_STATUS ] = { .name = "agg_server_status", .desc = "Backend's aggregated gauge of servers' status" },
Cedric Paillete06e31e2022-12-08 09:17:01 +0000264 [ST_F_AGG_CHECK_STATUS] = { .name = "agg_check_status", .desc = "Backend's aggregated gauge of servers' state check status" },
Aurelien DARRAGON745ce8e2022-11-17 16:34:07 +0100265 [ST_F_SRID] = { .name = "srid", .desc = "Server id revision, to prevent server id reuse mixups" },
William Lallemand74c24fb2016-11-21 17:18:36 +0100266};
267
Willy Tarreau0baac8c2016-11-22 16:36:53 +0100268/* one line of info */
William Dauchy5d9b8f32021-01-11 20:07:49 +0100269THREAD_LOCAL struct field info[INF_TOTAL_FIELDS];
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +0200270
271/* description of statistics (static and dynamic) */
272static struct name_desc *stat_f[STATS_DOMAIN_COUNT];
273static size_t stat_count[STATS_DOMAIN_COUNT];
274
275/* one line for stats */
William Dauchyb9577452021-01-17 18:27:46 +0100276THREAD_LOCAL struct field *stat_l[STATS_DOMAIN_COUNT];
William Lallemand74c24fb2016-11-21 17:18:36 +0100277
Amaury Denoyelle58d395e2020-10-05 11:49:40 +0200278/* list of all registered stats module */
279static struct list stats_module_list[STATS_DOMAIN_COUNT] = {
280 LIST_HEAD_INIT(stats_module_list[STATS_DOMAIN_PROXY]),
Emeric Brunf8642ee2021-10-29 17:59:18 +0200281 LIST_HEAD_INIT(stats_module_list[STATS_DOMAIN_RESOLVERS]),
Amaury Denoyelle58d395e2020-10-05 11:49:40 +0200282};
283
Amaury Denoyelle7f8f6cb2020-11-10 14:24:31 +0100284THREAD_LOCAL void *trash_counters;
285
Amaury Denoyelle072f97e2020-10-05 11:49:37 +0200286static inline uint8_t stats_get_domain(uint32_t domain)
287{
288 return domain >> STATS_DOMAIN & STATS_DOMAIN_MASK;
289}
290
Amaury Denoyelle72b16e52020-10-05 11:49:38 +0200291static inline enum stats_domain_px_cap stats_px_get_cap(uint32_t domain)
292{
293 return domain >> STATS_PX_CAP & STATS_PX_CAP_MASK;
294}
295
Christopher Faulet6338a082019-09-09 15:50:54 +0200296static void stats_dump_json_schema(struct buffer *out);
William Lallemand74c24fb2016-11-21 17:18:36 +0100297
Amaury Denoyelle3ca927e2020-10-02 18:32:00 +0200298int stats_putchk(struct channel *chn, struct htx *htx, struct buffer *chk)
Christopher Fauletef779222018-10-31 08:47:01 +0100299{
300 if (htx) {
Christopher Faulet69fc88c2019-01-07 14:27:53 +0100301 if (chk->data >= channel_htx_recv_max(chn, htx))
302 return 0;
Willy Tarreau0a7ef022019-05-28 10:30:11 +0200303 if (!htx_add_data_atonce(htx, ist2(chk->area, chk->data)))
Christopher Fauletef779222018-10-31 08:47:01 +0100304 return 0;
Christopher Faulet5adbeeb2019-01-02 14:34:39 +0100305 channel_add_input(chn, chk->data);
Christopher Fauletef779222018-10-31 08:47:01 +0100306 chk->data = 0;
Christopher Fauletef779222018-10-31 08:47:01 +0100307 }
308 else {
309 if (ci_putchk(chn, chk) == -1)
310 return 0;
311 }
312 return 1;
313}
Willy Tarreau0baac8c2016-11-22 16:36:53 +0100314
Willy Tarreaucaff6312022-05-27 10:17:46 +0200315static const char *stats_scope_ptr(struct appctx *appctx, struct stconn *sc)
Christopher Fauleted7a0662019-01-14 11:07:34 +0100316{
Willy Tarreau91cefca2022-05-03 17:08:29 +0200317 struct show_stat_ctx *ctx = appctx->svcctx;
Willy Tarreaucaff6312022-05-27 10:17:46 +0200318 struct channel *req = sc_oc(sc);
Christopher Fauletb7f88902019-07-15 21:56:43 +0200319 struct htx *htx = htxbuf(&req->buf);
320 struct htx_blk *blk;
321 struct ist uri;
Christopher Fauleted7a0662019-01-14 11:07:34 +0100322
Christopher Fauletb7f88902019-07-15 21:56:43 +0200323 blk = htx_get_head_blk(htx);
Christopher Fauletea009732019-11-18 15:50:25 +0100324 BUG_ON(!blk || htx_get_blk_type(blk) != HTX_BLK_REQ_SL);
Christopher Fauletb7f88902019-07-15 21:56:43 +0200325 ALREADY_CHECKED(blk);
326 uri = htx_sl_req_uri(htx_get_blk_ptr(htx, blk));
Willy Tarreau91cefca2022-05-03 17:08:29 +0200327 return uri.ptr + ctx->scope_str;
Christopher Fauleted7a0662019-01-14 11:07:34 +0100328}
329
William Lallemand74c24fb2016-11-21 17:18:36 +0100330/*
331 * http_stats_io_handler()
332 * -> stats_dump_stat_to_buffer() // same as above, but used for CSV or HTML
333 * -> stats_dump_csv_header() // emits the CSV headers (same as above)
Simon Horman05ee2132017-01-04 09:37:25 +0100334 * -> stats_dump_json_header() // emits the JSON headers (same as above)
William Lallemand74c24fb2016-11-21 17:18:36 +0100335 * -> stats_dump_html_head() // emits the HTML headers
336 * -> stats_dump_html_info() // emits the equivalent of "show info" at the top
337 * -> stats_dump_proxy_to_buffer() // same as above, valid for CSV and HTML
338 * -> stats_dump_html_px_hdr()
339 * -> stats_dump_fe_stats()
340 * -> stats_dump_li_stats()
341 * -> stats_dump_sv_stats()
342 * -> stats_dump_be_stats()
343 * -> stats_dump_html_px_end()
344 * -> stats_dump_html_end() // emits HTML trailer
Simon Horman05ee2132017-01-04 09:37:25 +0100345 * -> stats_dump_json_end() // emits JSON trailer
William Lallemand74c24fb2016-11-21 17:18:36 +0100346 */
347
348
William Lallemand74c24fb2016-11-21 17:18:36 +0100349/* Dumps the stats CSV header to the trash buffer which. The caller is responsible
350 * for clearing it if needed.
351 * NOTE: Some tools happen to rely on the field position instead of its name,
352 * so please only append new fields at the end, never in the middle.
353 */
Amaury Denoyelle50660a82020-10-05 11:49:39 +0200354static void stats_dump_csv_header(enum stats_domain domain)
William Lallemand74c24fb2016-11-21 17:18:36 +0100355{
356 int field;
357
358 chunk_appendf(&trash, "# ");
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +0200359 if (stat_f[domain]) {
360 for (field = 0; field < stat_count[domain]; ++field) {
361 chunk_appendf(&trash, "%s,", stat_f[domain][field].name);
William Lallemand74c24fb2016-11-21 17:18:36 +0100362
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +0200363 /* print special delimiter on proxy stats to mark end of
364 static fields */
365 if (domain == STATS_DOMAIN_PROXY && field + 1 == ST_F_TOTAL_FIELDS)
366 chunk_appendf(&trash, "-,");
Amaury Denoyelle50660a82020-10-05 11:49:39 +0200367 }
368 }
369
William Lallemand74c24fb2016-11-21 17:18:36 +0100370 chunk_appendf(&trash, "\n");
371}
372
William Lallemand74c24fb2016-11-21 17:18:36 +0100373/* Emits a stats field without any surrounding element and properly encoded to
374 * resist CSV output. Returns non-zero on success, 0 if the buffer is full.
375 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200376int stats_emit_raw_data_field(struct buffer *out, const struct field *f)
William Lallemand74c24fb2016-11-21 17:18:36 +0100377{
378 switch (field_format(f, 0)) {
379 case FF_EMPTY: return 1;
380 case FF_S32: return chunk_appendf(out, "%d", f->u.s32);
381 case FF_U32: return chunk_appendf(out, "%u", f->u.u32);
382 case FF_S64: return chunk_appendf(out, "%lld", (long long)f->u.s64);
383 case FF_U64: return chunk_appendf(out, "%llu", (unsigned long long)f->u.u64);
Willy Tarreau065ba312021-05-08 10:38:20 +0200384 case FF_FLT: {
385 size_t prev_data = out->data;
386 out->data = flt_trim(out->area, prev_data, chunk_appendf(out, "%f", f->u.flt));
387 return out->data;
388 }
William Lallemand74c24fb2016-11-21 17:18:36 +0100389 case FF_STR: return csv_enc_append(field_str(f, 0), 1, out) != NULL;
390 default: return chunk_appendf(out, "[INCORRECT_FIELD_TYPE_%08x]", f->type);
391 }
392}
393
Amaury Denoyellee3f576c2020-10-05 11:49:44 +0200394const char *field_to_html_str(const struct field *f)
395{
396 switch (field_format(f, 0)) {
397 case FF_S32: return U2H(f->u.s32);
398 case FF_S64: return U2H(f->u.s64);
399 case FF_U64: return U2H(f->u.u64);
400 case FF_U32: return U2H(f->u.u32);
Willy Tarreau6004fb72021-05-08 07:37:38 +0200401 case FF_FLT: return F2H(f->u.flt);
Amaury Denoyellee3f576c2020-10-05 11:49:44 +0200402 case FF_STR: return field_str(f, 0);
403 case FF_EMPTY:
404 default:
405 return "";
406 }
407}
408
William Lallemand74c24fb2016-11-21 17:18:36 +0100409/* Emits a stats field prefixed with its type. No CSV encoding is prepared, the
410 * output is supposed to be used on its own line. Returns non-zero on success, 0
411 * if the buffer is full.
412 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200413int stats_emit_typed_data_field(struct buffer *out, const struct field *f)
William Lallemand74c24fb2016-11-21 17:18:36 +0100414{
415 switch (field_format(f, 0)) {
416 case FF_EMPTY: return 1;
417 case FF_S32: return chunk_appendf(out, "s32:%d", f->u.s32);
418 case FF_U32: return chunk_appendf(out, "u32:%u", f->u.u32);
419 case FF_S64: return chunk_appendf(out, "s64:%lld", (long long)f->u.s64);
420 case FF_U64: return chunk_appendf(out, "u64:%llu", (unsigned long long)f->u.u64);
Willy Tarreau065ba312021-05-08 10:38:20 +0200421 case FF_FLT: {
422 size_t prev_data = out->data;
423 out->data = flt_trim(out->area, prev_data, chunk_appendf(out, "flt:%f", f->u.flt));
424 return out->data;
425 }
William Lallemand74c24fb2016-11-21 17:18:36 +0100426 case FF_STR: return chunk_appendf(out, "str:%s", field_str(f, 0));
427 default: return chunk_appendf(out, "%08x:?", f->type);
428 }
429}
430
Simon Horman05ee2132017-01-04 09:37:25 +0100431/* Limit JSON integer values to the range [-(2**53)+1, (2**53)-1] as per
432 * the recommendation for interoperable integers in section 6 of RFC 7159.
433 */
434#define JSON_INT_MAX ((1ULL << 53) - 1)
435#define JSON_INT_MIN (0 - JSON_INT_MAX)
436
437/* Emits a stats field value and its type in JSON.
438 * Returns non-zero on success, 0 on error.
439 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200440int stats_emit_json_data_field(struct buffer *out, const struct field *f)
Simon Horman05ee2132017-01-04 09:37:25 +0100441{
442 int old_len;
443 char buf[20];
444 const char *type, *value = buf, *quote = "";
445
446 switch (field_format(f, 0)) {
447 case FF_EMPTY: return 1;
448 case FF_S32: type = "\"s32\"";
449 snprintf(buf, sizeof(buf), "%d", f->u.s32);
450 break;
451 case FF_U32: type = "\"u32\"";
452 snprintf(buf, sizeof(buf), "%u", f->u.u32);
453 break;
454 case FF_S64: type = "\"s64\"";
455 if (f->u.s64 < JSON_INT_MIN || f->u.s64 > JSON_INT_MAX)
456 return 0;
457 type = "\"s64\"";
458 snprintf(buf, sizeof(buf), "%lld", (long long)f->u.s64);
459 break;
460 case FF_U64: if (f->u.u64 > JSON_INT_MAX)
461 return 0;
462 type = "\"u64\"";
463 snprintf(buf, sizeof(buf), "%llu",
464 (unsigned long long) f->u.u64);
Christopher Faulet52c91bb2019-09-28 10:37:31 +0200465 break;
Christopher Faulet88a0db22019-09-24 16:35:10 +0200466 case FF_FLT: type = "\"flt\"";
Willy Tarreau065ba312021-05-08 10:38:20 +0200467 flt_trim(buf, 0, snprintf(buf, sizeof(buf), "%f", f->u.flt));
Simon Horman05ee2132017-01-04 09:37:25 +0100468 break;
469 case FF_STR: type = "\"str\"";
470 value = field_str(f, 0);
471 quote = "\"";
472 break;
473 default: snprintf(buf, sizeof(buf), "%u", f->type);
474 type = buf;
475 value = "unknown";
476 quote = "\"";
477 break;
478 }
479
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200480 old_len = out->data;
Simon Horman05ee2132017-01-04 09:37:25 +0100481 chunk_appendf(out, ",\"value\":{\"type\":%s,\"value\":%s%s%s}",
482 type, quote, value, quote);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200483 return !(old_len == out->data);
Simon Horman05ee2132017-01-04 09:37:25 +0100484}
485
William Lallemand74c24fb2016-11-21 17:18:36 +0100486/* Emits an encoding of the field type on 3 characters followed by a delimiter.
487 * Returns non-zero on success, 0 if the buffer is full.
488 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200489int stats_emit_field_tags(struct buffer *out, const struct field *f,
490 char delim)
William Lallemand74c24fb2016-11-21 17:18:36 +0100491{
492 char origin, nature, scope;
493
494 switch (field_origin(f, 0)) {
495 case FO_METRIC: origin = 'M'; break;
496 case FO_STATUS: origin = 'S'; break;
497 case FO_KEY: origin = 'K'; break;
498 case FO_CONFIG: origin = 'C'; break;
499 case FO_PRODUCT: origin = 'P'; break;
500 default: origin = '?'; break;
501 }
502
503 switch (field_nature(f, 0)) {
504 case FN_GAUGE: nature = 'G'; break;
505 case FN_LIMIT: nature = 'L'; break;
506 case FN_MIN: nature = 'm'; break;
507 case FN_MAX: nature = 'M'; break;
508 case FN_RATE: nature = 'R'; break;
509 case FN_COUNTER: nature = 'C'; break;
510 case FN_DURATION: nature = 'D'; break;
511 case FN_AGE: nature = 'A'; break;
512 case FN_TIME: nature = 'T'; break;
513 case FN_NAME: nature = 'N'; break;
514 case FN_OUTPUT: nature = 'O'; break;
515 case FN_AVG: nature = 'a'; break;
516 default: nature = '?'; break;
517 }
518
519 switch (field_scope(f, 0)) {
520 case FS_PROCESS: scope = 'P'; break;
521 case FS_SERVICE: scope = 'S'; break;
522 case FS_SYSTEM: scope = 's'; break;
523 case FS_CLUSTER: scope = 'C'; break;
524 default: scope = '?'; break;
525 }
526
527 return chunk_appendf(out, "%c%c%c%c", origin, nature, scope, delim);
528}
529
Simon Horman05ee2132017-01-04 09:37:25 +0100530/* Emits an encoding of the field type as JSON.
531 * Returns non-zero on success, 0 if the buffer is full.
532 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200533int stats_emit_json_field_tags(struct buffer *out, const struct field *f)
Simon Horman05ee2132017-01-04 09:37:25 +0100534{
535 const char *origin, *nature, *scope;
536 int old_len;
537
538 switch (field_origin(f, 0)) {
539 case FO_METRIC: origin = "Metric"; break;
540 case FO_STATUS: origin = "Status"; break;
541 case FO_KEY: origin = "Key"; break;
542 case FO_CONFIG: origin = "Config"; break;
543 case FO_PRODUCT: origin = "Product"; break;
544 default: origin = "Unknown"; break;
545 }
546
547 switch (field_nature(f, 0)) {
548 case FN_GAUGE: nature = "Gauge"; break;
549 case FN_LIMIT: nature = "Limit"; break;
550 case FN_MIN: nature = "Min"; break;
551 case FN_MAX: nature = "Max"; break;
552 case FN_RATE: nature = "Rate"; break;
553 case FN_COUNTER: nature = "Counter"; break;
554 case FN_DURATION: nature = "Duration"; break;
555 case FN_AGE: nature = "Age"; break;
556 case FN_TIME: nature = "Time"; break;
557 case FN_NAME: nature = "Name"; break;
558 case FN_OUTPUT: nature = "Output"; break;
559 case FN_AVG: nature = "Avg"; break;
560 default: nature = "Unknown"; break;
561 }
562
563 switch (field_scope(f, 0)) {
564 case FS_PROCESS: scope = "Process"; break;
565 case FS_SERVICE: scope = "Service"; break;
566 case FS_SYSTEM: scope = "System"; break;
567 case FS_CLUSTER: scope = "Cluster"; break;
568 default: scope = "Unknown"; break;
569 }
570
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200571 old_len = out->data;
Simon Horman05ee2132017-01-04 09:37:25 +0100572 chunk_appendf(out, "\"tags\":{"
573 "\"origin\":\"%s\","
574 "\"nature\":\"%s\","
575 "\"scope\":\"%s\""
576 "}", origin, nature, scope);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200577 return !(old_len == out->data);
Simon Horman05ee2132017-01-04 09:37:25 +0100578}
William Lallemand74c24fb2016-11-21 17:18:36 +0100579
580/* Dump all fields from <stats> into <out> using CSV format */
Willy Tarreau83061a82018-07-13 11:56:34 +0200581static int stats_dump_fields_csv(struct buffer *out,
Amaury Denoyelle97323c92020-10-02 18:32:01 +0200582 const struct field *stats, size_t stats_count,
Aurelien DARRAGONe76a0272022-12-15 12:10:03 +0100583 struct show_stat_ctx *ctx)
William Lallemand74c24fb2016-11-21 17:18:36 +0100584{
Aurelien DARRAGONe76a0272022-12-15 12:10:03 +0100585 int domain = ctx->domain;
William Lallemand74c24fb2016-11-21 17:18:36 +0100586 int field;
587
Amaury Denoyelle50660a82020-10-05 11:49:39 +0200588 for (field = 0; field < stats_count; ++field) {
William Lallemand74c24fb2016-11-21 17:18:36 +0100589 if (!stats_emit_raw_data_field(out, &stats[field]))
590 return 0;
591 if (!chunk_strcat(out, ","))
592 return 0;
Amaury Denoyelle50660a82020-10-05 11:49:39 +0200593
594 /* print special delimiter on proxy stats to mark end of
595 static fields */
596 if (domain == STATS_DOMAIN_PROXY && field + 1 == ST_F_TOTAL_FIELDS) {
597 if (!chunk_strcat(out, "-,"))
598 return 0;
599 }
William Lallemand74c24fb2016-11-21 17:18:36 +0100600 }
Amaury Denoyelle50660a82020-10-05 11:49:39 +0200601
William Lallemand74c24fb2016-11-21 17:18:36 +0100602 chunk_strcat(out, "\n");
603 return 1;
604}
605
606/* Dump all fields from <stats> into <out> using a typed "field:desc:type:value" format */
Willy Tarreau83061a82018-07-13 11:56:34 +0200607static int stats_dump_fields_typed(struct buffer *out,
Amaury Denoyelle97323c92020-10-02 18:32:01 +0200608 const struct field *stats,
609 size_t stats_count,
Aurelien DARRAGONe76a0272022-12-15 12:10:03 +0100610 struct show_stat_ctx * ctx)
William Lallemand74c24fb2016-11-21 17:18:36 +0100611{
Aurelien DARRAGONe76a0272022-12-15 12:10:03 +0100612 int flags = ctx->flags;
613 int domain = ctx->domain;
William Lallemand74c24fb2016-11-21 17:18:36 +0100614 int field;
615
Amaury Denoyelle97323c92020-10-02 18:32:01 +0200616 for (field = 0; field < stats_count; ++field) {
William Lallemand74c24fb2016-11-21 17:18:36 +0100617 if (!stats[field].type)
618 continue;
619
Amaury Denoyelle072f97e2020-10-05 11:49:37 +0200620 switch (domain) {
621 case STATS_DOMAIN_PROXY:
622 chunk_appendf(out, "%c.%u.%u.%d.%s.%u:",
623 stats[ST_F_TYPE].u.u32 == STATS_TYPE_FE ? 'F' :
624 stats[ST_F_TYPE].u.u32 == STATS_TYPE_BE ? 'B' :
625 stats[ST_F_TYPE].u.u32 == STATS_TYPE_SO ? 'L' :
626 stats[ST_F_TYPE].u.u32 == STATS_TYPE_SV ? 'S' :
627 '?',
628 stats[ST_F_IID].u.u32, stats[ST_F_SID].u.u32,
629 field,
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +0200630 stat_f[domain][field].name,
Amaury Denoyelle072f97e2020-10-05 11:49:37 +0200631 stats[ST_F_PID].u.u32);
632 break;
633
Emeric Brunf8642ee2021-10-29 17:59:18 +0200634 case STATS_DOMAIN_RESOLVERS:
635 chunk_appendf(out, "N.%d.%s:", field,
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +0200636 stat_f[domain][field].name);
637 break;
638
Amaury Denoyelle072f97e2020-10-05 11:49:37 +0200639 default:
640 break;
641 }
William Lallemand74c24fb2016-11-21 17:18:36 +0100642
643 if (!stats_emit_field_tags(out, &stats[field], ':'))
644 return 0;
645 if (!stats_emit_typed_data_field(out, &stats[field]))
646 return 0;
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +0200647
Willy Tarreaufc8e4382021-06-17 07:22:27 +0200648 if (flags & STAT_SHOW_FDESC &&
649 !chunk_appendf(out, ":\"%s\"", stat_f[domain][field].desc)) {
Willy Tarreau6b19b142019-10-09 15:44:21 +0200650 return 0;
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +0200651 }
652
William Lallemand74c24fb2016-11-21 17:18:36 +0100653 if (!chunk_strcat(out, "\n"))
654 return 0;
655 }
656 return 1;
657}
658
Simon Horman05ee2132017-01-04 09:37:25 +0100659/* Dump all fields from <stats> into <out> using the "show info json" format */
Willy Tarreau83061a82018-07-13 11:56:34 +0200660static int stats_dump_json_info_fields(struct buffer *out,
Aurelien DARRAGONe76a0272022-12-15 12:10:03 +0100661 const struct field *info,
662 struct show_stat_ctx *ctx)
Simon Horman05ee2132017-01-04 09:37:25 +0100663{
Aurelien DARRAGON16c9ca92022-12-15 14:24:30 +0100664 int started = (ctx->field) ? 1 : 0;
665 int ready_data = 0;
Simon Horman05ee2132017-01-04 09:37:25 +0100666
Aurelien DARRAGON16c9ca92022-12-15 14:24:30 +0100667 if (!started && !chunk_strcat(out, "["))
Simon Horman05ee2132017-01-04 09:37:25 +0100668 return 0;
669
Aurelien DARRAGON16c9ca92022-12-15 14:24:30 +0100670 for (; ctx->field < INF_TOTAL_FIELDS; ctx->field++) {
Simon Horman05ee2132017-01-04 09:37:25 +0100671 int old_len;
Aurelien DARRAGON16c9ca92022-12-15 14:24:30 +0100672 int field = ctx->field;
Simon Horman05ee2132017-01-04 09:37:25 +0100673
674 if (!field_format(info, field))
675 continue;
676
677 if (started && !chunk_strcat(out, ","))
678 goto err;
679 started = 1;
680
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200681 old_len = out->data;
Simon Horman05ee2132017-01-04 09:37:25 +0100682 chunk_appendf(out,
683 "{\"field\":{\"pos\":%d,\"name\":\"%s\"},"
684 "\"processNum\":%u,",
Willy Tarreaueaa55372019-10-09 07:39:11 +0200685 field, info_fields[field].name,
Simon Horman05ee2132017-01-04 09:37:25 +0100686 info[INF_PROCESS_NUM].u.u32);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200687 if (old_len == out->data)
Simon Horman05ee2132017-01-04 09:37:25 +0100688 goto err;
689
690 if (!stats_emit_json_field_tags(out, &info[field]))
691 goto err;
692
693 if (!stats_emit_json_data_field(out, &info[field]))
694 goto err;
695
696 if (!chunk_strcat(out, "}"))
697 goto err;
Aurelien DARRAGON16c9ca92022-12-15 14:24:30 +0100698 ready_data = out->data;
Simon Horman05ee2132017-01-04 09:37:25 +0100699 }
700
Willy Tarreauc6b7a972022-06-10 15:12:21 +0200701 if (!chunk_strcat(out, "]\n"))
Simon Horman05ee2132017-01-04 09:37:25 +0100702 goto err;
Aurelien DARRAGON16c9ca92022-12-15 14:24:30 +0100703 ctx->field = 0; /* we're done */
Simon Horman05ee2132017-01-04 09:37:25 +0100704 return 1;
705
706err:
Aurelien DARRAGON16c9ca92022-12-15 14:24:30 +0100707 if (!ready_data) {
708 /* not enough buffer space for a single entry.. */
709 chunk_reset(out);
710 chunk_appendf(out, "{\"errorStr\":\"output buffer too short\"}\n");
711 return 0; /* hard error */
712 }
713 /* push ready data and wait for a new buffer to complete the dump */
714 out->data = ready_data;
715 return 1;
Simon Horman05ee2132017-01-04 09:37:25 +0100716}
717
Amaury Denoyellef34017b2020-10-02 18:32:03 +0200718static void stats_print_proxy_field_json(struct buffer *out,
719 const struct field *stat,
720 const char *name,
721 int pos,
722 uint32_t field_type,
723 uint32_t iid,
724 uint32_t sid,
725 uint32_t pid)
726{
727 const char *obj_type;
728 switch (field_type) {
729 case STATS_TYPE_FE: obj_type = "Frontend"; break;
730 case STATS_TYPE_BE: obj_type = "Backend"; break;
731 case STATS_TYPE_SO: obj_type = "Listener"; break;
732 case STATS_TYPE_SV: obj_type = "Server"; break;
733 default: obj_type = "Unknown"; break;
734 }
735
736 chunk_appendf(out,
737 "{"
738 "\"objType\":\"%s\","
739 "\"proxyId\":%u,"
740 "\"id\":%u,"
741 "\"field\":{\"pos\":%d,\"name\":\"%s\"},"
742 "\"processNum\":%u,",
743 obj_type, iid, sid, pos, name, pid);
744}
745
Emeric Brunf8642ee2021-10-29 17:59:18 +0200746static void stats_print_rslv_field_json(struct buffer *out,
747 const struct field *stat,
748 const char *name,
749 int pos)
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +0200750{
751 chunk_appendf(out,
752 "{"
753 "\"field\":{\"pos\":%d,\"name\":\"%s\"},",
754 pos, name);
755}
756
757
Simon Horman05ee2132017-01-04 09:37:25 +0100758/* Dump all fields from <stats> into <out> using a typed "field:desc:type:value" format */
Willy Tarreau83061a82018-07-13 11:56:34 +0200759static int stats_dump_fields_json(struct buffer *out,
Amaury Denoyelle97323c92020-10-02 18:32:01 +0200760 const struct field *stats, size_t stats_count,
Aurelien DARRAGONe76a0272022-12-15 12:10:03 +0100761 struct show_stat_ctx *ctx)
Simon Horman05ee2132017-01-04 09:37:25 +0100762{
Aurelien DARRAGONe76a0272022-12-15 12:10:03 +0100763 int flags = ctx->flags;
764 int domain = ctx->domain;
Aurelien DARRAGON42b18fb2022-12-15 14:17:09 +0100765 int started = (ctx->field) ? 1 : 0;
766 int ready_data = 0;
Simon Horman05ee2132017-01-04 09:37:25 +0100767
Aurelien DARRAGON42b18fb2022-12-15 14:17:09 +0100768 if (!started && (flags & STAT_STARTED) && !chunk_strcat(out, ","))
Simon Horman05ee2132017-01-04 09:37:25 +0100769 return 0;
Aurelien DARRAGON42b18fb2022-12-15 14:17:09 +0100770 if (!started && !chunk_strcat(out, "["))
Simon Horman05ee2132017-01-04 09:37:25 +0100771 return 0;
772
Aurelien DARRAGON42b18fb2022-12-15 14:17:09 +0100773 for (; ctx->field < stats_count; ctx->field++) {
Simon Horman05ee2132017-01-04 09:37:25 +0100774 int old_len;
Aurelien DARRAGON42b18fb2022-12-15 14:17:09 +0100775 int field = ctx->field;
Simon Horman05ee2132017-01-04 09:37:25 +0100776
777 if (!stats[field].type)
778 continue;
779
780 if (started && !chunk_strcat(out, ","))
781 goto err;
782 started = 1;
783
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200784 old_len = out->data;
Amaury Denoyelle072f97e2020-10-05 11:49:37 +0200785 if (domain == STATS_DOMAIN_PROXY) {
786 stats_print_proxy_field_json(out, &stats[field],
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +0200787 stat_f[domain][field].name,
788 field,
Amaury Denoyelle072f97e2020-10-05 11:49:37 +0200789 stats[ST_F_TYPE].u.u32,
790 stats[ST_F_IID].u.u32,
791 stats[ST_F_SID].u.u32,
792 stats[ST_F_PID].u.u32);
Emeric Brunf8642ee2021-10-29 17:59:18 +0200793 } else if (domain == STATS_DOMAIN_RESOLVERS) {
794 stats_print_rslv_field_json(out, &stats[field],
795 stat_f[domain][field].name,
796 field);
Amaury Denoyelle072f97e2020-10-05 11:49:37 +0200797 }
Amaury Denoyellef34017b2020-10-02 18:32:03 +0200798
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200799 if (old_len == out->data)
Simon Horman05ee2132017-01-04 09:37:25 +0100800 goto err;
801
802 if (!stats_emit_json_field_tags(out, &stats[field]))
803 goto err;
804
805 if (!stats_emit_json_data_field(out, &stats[field]))
806 goto err;
807
808 if (!chunk_strcat(out, "}"))
809 goto err;
Aurelien DARRAGON42b18fb2022-12-15 14:17:09 +0100810 ready_data = out->data;
Simon Horman05ee2132017-01-04 09:37:25 +0100811 }
812
813 if (!chunk_strcat(out, "]"))
814 goto err;
815
Aurelien DARRAGON42b18fb2022-12-15 14:17:09 +0100816 ctx->field = 0; /* we're done */
Simon Horman05ee2132017-01-04 09:37:25 +0100817 return 1;
818
819err:
Aurelien DARRAGON42b18fb2022-12-15 14:17:09 +0100820 if (!ready_data) {
821 /* not enough buffer space for a single entry.. */
822 chunk_reset(out);
823 if (ctx->flags & STAT_STARTED)
824 chunk_strcat(out, ",");
825 chunk_appendf(out, "{\"errorStr\":\"output buffer too short\"}");
826 return 0; /* hard error */
827 }
828 /* push ready data and wait for a new buffer to complete the dump */
829 out->data = ready_data;
830 return 1;
Simon Horman05ee2132017-01-04 09:37:25 +0100831}
832
William Lallemand74c24fb2016-11-21 17:18:36 +0100833/* Dump all fields from <stats> into <out> using the HTML format. A column is
Willy Tarreauab02b3f2019-10-09 11:11:46 +0200834 * reserved for the checkbox is STAT_ADMIN is set in <flags>. Some extra info
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +0200835 * are provided if STAT_SHLGNDS is present in <flags>. The statistics from
836 * extra modules are displayed at the end of the lines if STAT_SHMODULES is
837 * present in <flags>.
William Lallemand74c24fb2016-11-21 17:18:36 +0100838 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200839static int stats_dump_fields_html(struct buffer *out,
Aurelien DARRAGONe76a0272022-12-15 12:10:03 +0100840 const struct field *stats,
841 struct show_stat_ctx *ctx)
William Lallemand74c24fb2016-11-21 17:18:36 +0100842{
Willy Tarreau83061a82018-07-13 11:56:34 +0200843 struct buffer src;
Amaury Denoyellee3f576c2020-10-05 11:49:44 +0200844 struct stats_module *mod;
Aurelien DARRAGONe76a0272022-12-15 12:10:03 +0100845 int flags = ctx->flags;
Amaury Denoyellee3f576c2020-10-05 11:49:44 +0200846 int i = 0, j = 0;
William Lallemand74c24fb2016-11-21 17:18:36 +0100847
848 if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_FE) {
849 chunk_appendf(out,
850 /* name, queue */
851 "<tr class=\"frontend\">");
852
Willy Tarreauab02b3f2019-10-09 11:11:46 +0200853 if (flags & STAT_ADMIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +0100854 /* Column sub-heading for Enable or Disable server */
855 chunk_appendf(out, "<td></td>");
856 }
857
858 chunk_appendf(out,
859 "<td class=ac>"
860 "<a name=\"%s/Frontend\"></a>"
861 "<a class=lfsb href=\"#%s/Frontend\">Frontend</a></td>"
862 "<td colspan=3></td>"
863 "",
864 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_PXNAME));
865
866 chunk_appendf(out,
867 /* sessions rate : current */
868 "<td><u>%s<div class=tips><table class=det>"
869 "<tr><th>Current connection rate:</th><td>%s/s</td></tr>"
870 "<tr><th>Current session rate:</th><td>%s/s</td></tr>"
871 "",
872 U2H(stats[ST_F_RATE].u.u32),
873 U2H(stats[ST_F_CONN_RATE].u.u32),
874 U2H(stats[ST_F_RATE].u.u32));
875
876 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
877 chunk_appendf(out,
878 "<tr><th>Current request rate:</th><td>%s/s</td></tr>",
879 U2H(stats[ST_F_REQ_RATE].u.u32));
880
881 chunk_appendf(out,
882 "</table></div></u></td>"
883 /* sessions rate : max */
884 "<td><u>%s<div class=tips><table class=det>"
885 "<tr><th>Max connection rate:</th><td>%s/s</td></tr>"
886 "<tr><th>Max session rate:</th><td>%s/s</td></tr>"
887 "",
888 U2H(stats[ST_F_RATE_MAX].u.u32),
889 U2H(stats[ST_F_CONN_RATE_MAX].u.u32),
890 U2H(stats[ST_F_RATE_MAX].u.u32));
891
892 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
893 chunk_appendf(out,
894 "<tr><th>Max request rate:</th><td>%s/s</td></tr>",
895 U2H(stats[ST_F_REQ_RATE_MAX].u.u32));
896
897 chunk_appendf(out,
898 "</table></div></u></td>"
899 /* sessions rate : limit */
900 "<td>%s</td>",
901 LIM2A(stats[ST_F_RATE_LIM].u.u32, "-"));
902
903 chunk_appendf(out,
904 /* sessions: current, max, limit, total */
905 "<td>%s</td><td>%s</td><td>%s</td>"
906 "<td><u>%s<div class=tips><table class=det>"
907 "<tr><th>Cum. connections:</th><td>%s</td></tr>"
908 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
909 "",
910 U2H(stats[ST_F_SCUR].u.u32), U2H(stats[ST_F_SMAX].u.u32), U2H(stats[ST_F_SLIM].u.u32),
911 U2H(stats[ST_F_STOT].u.u64),
912 U2H(stats[ST_F_CONN_TOT].u.u64),
913 U2H(stats[ST_F_STOT].u.u64));
914
915 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
916 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0) {
917 chunk_appendf(out,
918 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
919 "<tr><th>- HTTP 1xx responses:</th><td>%s</td></tr>"
920 "<tr><th>- HTTP 2xx responses:</th><td>%s</td></tr>"
921 "<tr><th>&nbsp;&nbsp;Compressed 2xx:</th><td>%s</td><td>(%d%%)</td></tr>"
922 "<tr><th>- HTTP 3xx responses:</th><td>%s</td></tr>"
923 "<tr><th>- HTTP 4xx responses:</th><td>%s</td></tr>"
924 "<tr><th>- HTTP 5xx responses:</th><td>%s</td></tr>"
925 "<tr><th>- other responses:</th><td>%s</td></tr>"
926 "<tr><th>Intercepted requests:</th><td>%s</td></tr>"
Willy Tarreaua1214a52018-12-14 14:00:25 +0100927 "<tr><th>Cache lookups:</th><td>%s</td></tr>"
928 "<tr><th>Cache hits:</th><td>%s</td><td>(%d%%)</td></tr>"
Willy Tarreau1b0f85e2018-05-28 15:12:40 +0200929 "<tr><th>Failed hdr rewrites:</th><td>%s</td></tr>"
Christopher Faulet0159ee42019-12-16 14:40:39 +0100930 "<tr><th>Internal errors:</th><td>%s</td></tr>"
William Lallemand74c24fb2016-11-21 17:18:36 +0100931 "",
932 U2H(stats[ST_F_REQ_TOT].u.u64),
933 U2H(stats[ST_F_HRSP_1XX].u.u64),
934 U2H(stats[ST_F_HRSP_2XX].u.u64),
935 U2H(stats[ST_F_COMP_RSP].u.u64),
936 stats[ST_F_HRSP_2XX].u.u64 ?
937 (int)(100 * stats[ST_F_COMP_RSP].u.u64 / stats[ST_F_HRSP_2XX].u.u64) : 0,
938 U2H(stats[ST_F_HRSP_3XX].u.u64),
939 U2H(stats[ST_F_HRSP_4XX].u.u64),
940 U2H(stats[ST_F_HRSP_5XX].u.u64),
941 U2H(stats[ST_F_HRSP_OTHER].u.u64),
Willy Tarreau1b0f85e2018-05-28 15:12:40 +0200942 U2H(stats[ST_F_INTERCEPTED].u.u64),
Willy Tarreaua1214a52018-12-14 14:00:25 +0100943 U2H(stats[ST_F_CACHE_LOOKUPS].u.u64),
944 U2H(stats[ST_F_CACHE_HITS].u.u64),
945 stats[ST_F_CACHE_LOOKUPS].u.u64 ?
946 (int)(100 * stats[ST_F_CACHE_HITS].u.u64 / stats[ST_F_CACHE_LOOKUPS].u.u64) : 0,
Christopher Faulet0159ee42019-12-16 14:40:39 +0100947 U2H(stats[ST_F_WREW].u.u64),
948 U2H(stats[ST_F_EINT].u.u64));
William Lallemand74c24fb2016-11-21 17:18:36 +0100949 }
950
951 chunk_appendf(out,
952 "</table></div></u></td>"
953 /* sessions: lbtot, lastsess */
954 "<td></td><td></td>"
955 /* bytes : in */
956 "<td>%s</td>"
957 "",
958 U2H(stats[ST_F_BIN].u.u64));
959
960 chunk_appendf(out,
961 /* bytes:out + compression stats (via hover): comp_in, comp_out, comp_byp */
962 "<td>%s%s<div class=tips><table class=det>"
963 "<tr><th>Response bytes in:</th><td>%s</td></tr>"
964 "<tr><th>Compression in:</th><td>%s</td></tr>"
965 "<tr><th>Compression out:</th><td>%s</td><td>(%d%%)</td></tr>"
966 "<tr><th>Compression bypass:</th><td>%s</td></tr>"
967 "<tr><th>Total bytes saved:</th><td>%s</td><td>(%d%%)</td></tr>"
968 "</table></div>%s</td>",
969 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "<u>":"",
970 U2H(stats[ST_F_BOUT].u.u64),
971 U2H(stats[ST_F_BOUT].u.u64),
972 U2H(stats[ST_F_COMP_IN].u.u64),
973 U2H(stats[ST_F_COMP_OUT].u.u64),
974 stats[ST_F_COMP_IN].u.u64 ? (int)(stats[ST_F_COMP_OUT].u.u64 * 100 / stats[ST_F_COMP_IN].u.u64) : 0,
975 U2H(stats[ST_F_COMP_BYP].u.u64),
976 U2H(stats[ST_F_COMP_IN].u.u64 - stats[ST_F_COMP_OUT].u.u64),
977 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,
978 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "</u>":"");
979
980 chunk_appendf(out,
981 /* denied: req, resp */
982 "<td>%s</td><td>%s</td>"
983 /* errors : request, connect, response */
984 "<td>%s</td><td></td><td></td>"
985 /* warnings: retries, redispatches */
986 "<td></td><td></td>"
987 /* server status : reflect frontend status */
988 "<td class=ac>%s</td>"
989 /* rest of server: nothing */
Amaury Denoyellee3f576c2020-10-05 11:49:44 +0200990 "<td class=ac colspan=8></td>"
William Lallemand74c24fb2016-11-21 17:18:36 +0100991 "",
992 U2H(stats[ST_F_DREQ].u.u64), U2H(stats[ST_F_DRESP].u.u64),
993 U2H(stats[ST_F_EREQ].u.u64),
994 field_str(stats, ST_F_STATUS));
Amaury Denoyellee3f576c2020-10-05 11:49:44 +0200995
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +0200996 if (flags & STAT_SHMODULES) {
997 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
Amaury Denoyellee6ee8202020-11-03 15:04:46 +0100998 chunk_appendf(out, "<td>");
999
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001000 if (stats_px_get_cap(mod->domain_flags) & STATS_PX_CAP_FE) {
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001001 chunk_appendf(out,
1002 "<u>%s<div class=tips><table class=det>",
1003 mod->name);
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001004 for (j = 0; j < mod->stats_count; ++j) {
1005 chunk_appendf(out,
1006 "<tr><th>%s</th><td>%s</td></tr>",
1007 mod->stats[j].desc, field_to_html_str(&stats[ST_F_TOTAL_FIELDS + i]));
1008 ++i;
1009 }
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001010 chunk_appendf(out, "</table></div></u>");
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001011 } else {
1012 i += mod->stats_count;
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001013 }
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001014
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001015 chunk_appendf(out, "</td>");
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001016 }
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001017 }
1018
1019 chunk_appendf(out, "</tr>");
William Lallemand74c24fb2016-11-21 17:18:36 +01001020 }
1021 else if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_SO) {
1022 chunk_appendf(out, "<tr class=socket>");
Willy Tarreauab02b3f2019-10-09 11:11:46 +02001023 if (flags & STAT_ADMIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001024 /* Column sub-heading for Enable or Disable server */
1025 chunk_appendf(out, "<td></td>");
1026 }
1027
1028 chunk_appendf(out,
1029 /* frontend name, listener name */
1030 "<td class=ac><a name=\"%s/+%s\"></a>%s"
1031 "<a class=lfsb href=\"#%s/+%s\">%s</a>"
1032 "",
1033 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME),
Willy Tarreau708c4162019-10-09 10:19:16 +02001034 (flags & STAT_SHLGNDS)?"<u>":"",
William Lallemand74c24fb2016-11-21 17:18:36 +01001035 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME), field_str(stats, ST_F_SVNAME));
1036
Willy Tarreau708c4162019-10-09 10:19:16 +02001037 if (flags & STAT_SHLGNDS) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001038 chunk_appendf(out, "<div class=tips>");
1039
Willy Tarreau90807112020-02-25 08:16:33 +01001040 if (isdigit((unsigned char)*field_str(stats, ST_F_ADDR)))
William Lallemand74c24fb2016-11-21 17:18:36 +01001041 chunk_appendf(out, "IPv4: %s, ", field_str(stats, ST_F_ADDR));
1042 else if (*field_str(stats, ST_F_ADDR) == '[')
1043 chunk_appendf(out, "IPv6: %s, ", field_str(stats, ST_F_ADDR));
1044 else if (*field_str(stats, ST_F_ADDR))
1045 chunk_appendf(out, "%s, ", field_str(stats, ST_F_ADDR));
1046
1047 /* id */
1048 chunk_appendf(out, "id: %d</div>", stats[ST_F_SID].u.u32);
1049 }
1050
1051 chunk_appendf(out,
1052 /* queue */
1053 "%s</td><td colspan=3></td>"
1054 /* sessions rate: current, max, limit */
1055 "<td colspan=3>&nbsp;</td>"
1056 /* sessions: current, max, limit, total, lbtot, lastsess */
1057 "<td>%s</td><td>%s</td><td>%s</td>"
1058 "<td>%s</td><td>&nbsp;</td><td>&nbsp;</td>"
1059 /* bytes: in, out */
1060 "<td>%s</td><td>%s</td>"
1061 "",
Willy Tarreau708c4162019-10-09 10:19:16 +02001062 (flags & STAT_SHLGNDS)?"</u>":"",
William Lallemand74c24fb2016-11-21 17:18:36 +01001063 U2H(stats[ST_F_SCUR].u.u32), U2H(stats[ST_F_SMAX].u.u32), U2H(stats[ST_F_SLIM].u.u32),
1064 U2H(stats[ST_F_STOT].u.u64), U2H(stats[ST_F_BIN].u.u64), U2H(stats[ST_F_BOUT].u.u64));
1065
1066 chunk_appendf(out,
1067 /* denied: req, resp */
1068 "<td>%s</td><td>%s</td>"
1069 /* errors: request, connect, response */
1070 "<td>%s</td><td></td><td></td>"
1071 /* warnings: retries, redispatches */
1072 "<td></td><td></td>"
1073 /* server status: reflect listener status */
1074 "<td class=ac>%s</td>"
1075 /* rest of server: nothing */
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001076 "<td class=ac colspan=8></td>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001077 "",
1078 U2H(stats[ST_F_DREQ].u.u64), U2H(stats[ST_F_DRESP].u.u64),
1079 U2H(stats[ST_F_EREQ].u.u64),
1080 field_str(stats, ST_F_STATUS));
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001081
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001082 if (flags & STAT_SHMODULES) {
1083 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001084 chunk_appendf(out, "<td>");
1085
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001086 if (stats_px_get_cap(mod->domain_flags) & STATS_PX_CAP_LI) {
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001087 chunk_appendf(out,
1088 "<u>%s<div class=tips><table class=det>",
1089 mod->name);
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001090 for (j = 0; j < mod->stats_count; ++j) {
1091 chunk_appendf(out,
1092 "<tr><th>%s</th><td>%s</td></tr>",
1093 mod->stats[j].desc, field_to_html_str(&stats[ST_F_TOTAL_FIELDS + i]));
1094 ++i;
1095 }
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001096 chunk_appendf(out, "</table></div></u>");
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001097 } else {
1098 i += mod->stats_count;
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001099 }
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001100
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001101 chunk_appendf(out, "</td>");
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001102 }
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001103 }
1104
1105 chunk_appendf(out, "</tr>");
William Lallemand74c24fb2016-11-21 17:18:36 +01001106 }
1107 else if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_SV) {
1108 const char *style;
1109
1110 /* determine the style to use depending on the server's state,
1111 * its health and weight. There isn't a 1-to-1 mapping between
1112 * state and styles for the cases where the server is (still)
1113 * up. The reason is that we don't want to report nolb and
1114 * drain with the same color.
1115 */
1116
1117 if (strcmp(field_str(stats, ST_F_STATUS), "DOWN") == 0 ||
1118 strcmp(field_str(stats, ST_F_STATUS), "DOWN (agent)") == 0) {
1119 style = "down";
1120 }
Florian Apolloner39272c22021-03-30 13:28:35 +02001121 else if (strncmp(field_str(stats, ST_F_STATUS), "DOWN ", strlen("DOWN ")) == 0) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001122 style = "going_up";
1123 }
Daniel Corbettb4285172020-03-28 12:35:50 -04001124 else if (strcmp(field_str(stats, ST_F_STATUS), "DRAIN") == 0) {
1125 style = "draining";
1126 }
Florian Apolloner39272c22021-03-30 13:28:35 +02001127 else if (strncmp(field_str(stats, ST_F_STATUS), "NOLB ", strlen("NOLB ")) == 0) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001128 style = "going_down";
1129 }
1130 else if (strcmp(field_str(stats, ST_F_STATUS), "NOLB") == 0) {
1131 style = "nolb";
1132 }
1133 else if (strcmp(field_str(stats, ST_F_STATUS), "no check") == 0) {
1134 style = "no_check";
1135 }
1136 else if (!stats[ST_F_CHKFAIL].type ||
1137 stats[ST_F_CHECK_HEALTH].u.u32 == stats[ST_F_CHECK_RISE].u.u32 + stats[ST_F_CHECK_FALL].u.u32 - 1) {
1138 /* no check or max health = UP */
1139 if (stats[ST_F_WEIGHT].u.u32)
1140 style = "up";
1141 else
1142 style = "draining";
1143 }
1144 else {
1145 style = "going_down";
1146 }
1147
Willy Tarreau7b524852020-08-11 10:26:36 +02001148 if (strncmp(field_str(stats, ST_F_STATUS), "MAINT", 5) == 0)
William Lallemand74c24fb2016-11-21 17:18:36 +01001149 chunk_appendf(out, "<tr class=\"maintain\">");
1150 else
1151 chunk_appendf(out,
1152 "<tr class=\"%s_%s\">",
1153 (stats[ST_F_BCK].u.u32) ? "backup" : "active", style);
1154
1155
Willy Tarreauab02b3f2019-10-09 11:11:46 +02001156 if (flags & STAT_ADMIN)
William Lallemand74c24fb2016-11-21 17:18:36 +01001157 chunk_appendf(out,
David Harrigand3db35a2016-12-30 12:12:49 +00001158 "<td><input class='%s-checkbox' type=\"checkbox\" name=\"s\" value=\"%s\"></td>",
1159 field_str(stats, ST_F_PXNAME),
William Lallemand74c24fb2016-11-21 17:18:36 +01001160 field_str(stats, ST_F_SVNAME));
1161
1162 chunk_appendf(out,
1163 "<td class=ac><a name=\"%s/%s\"></a>%s"
1164 "<a class=lfsb href=\"#%s/%s\">%s</a>"
1165 "",
1166 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME),
Willy Tarreau708c4162019-10-09 10:19:16 +02001167 (flags & STAT_SHLGNDS) ? "<u>" : "",
William Lallemand74c24fb2016-11-21 17:18:36 +01001168 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME), field_str(stats, ST_F_SVNAME));
1169
Willy Tarreau708c4162019-10-09 10:19:16 +02001170 if (flags & STAT_SHLGNDS) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001171 chunk_appendf(out, "<div class=tips>");
1172
Willy Tarreau90807112020-02-25 08:16:33 +01001173 if (isdigit((unsigned char)*field_str(stats, ST_F_ADDR)))
William Lallemand74c24fb2016-11-21 17:18:36 +01001174 chunk_appendf(out, "IPv4: %s, ", field_str(stats, ST_F_ADDR));
1175 else if (*field_str(stats, ST_F_ADDR) == '[')
1176 chunk_appendf(out, "IPv6: %s, ", field_str(stats, ST_F_ADDR));
1177 else if (*field_str(stats, ST_F_ADDR))
1178 chunk_appendf(out, "%s, ", field_str(stats, ST_F_ADDR));
1179
1180 /* id */
Aurelien DARRAGON745ce8e2022-11-17 16:34:07 +01001181 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 +01001182
1183 /* cookie */
1184 if (stats[ST_F_COOKIE].type) {
1185 chunk_appendf(out, ", cookie: '");
1186 chunk_initstr(&src, field_str(stats, ST_F_COOKIE));
1187 chunk_htmlencode(out, &src);
1188 chunk_appendf(out, "'");
1189 }
1190
1191 chunk_appendf(out, "</div>");
1192 }
1193
1194 chunk_appendf(out,
1195 /* queue : current, max, limit */
1196 "%s</td><td>%s</td><td>%s</td><td>%s</td>"
1197 /* sessions rate : current, max, limit */
1198 "<td>%s</td><td>%s</td><td></td>"
1199 "",
Willy Tarreau708c4162019-10-09 10:19:16 +02001200 (flags & STAT_SHLGNDS) ? "</u>" : "",
William Lallemand74c24fb2016-11-21 17:18:36 +01001201 U2H(stats[ST_F_QCUR].u.u32), U2H(stats[ST_F_QMAX].u.u32), LIM2A(stats[ST_F_QLIMIT].u.u32, "-"),
1202 U2H(stats[ST_F_RATE].u.u32), U2H(stats[ST_F_RATE_MAX].u.u32));
1203
1204 chunk_appendf(out,
1205 /* sessions: current, max, limit, total */
Willy Tarreauf21d17b2019-09-08 09:24:56 +02001206 "<td><u>%s<div class=tips>"
1207 "<table class=det>"
1208 "<tr><th>Current active connections:</th><td>%s</td></tr>"
Willy Tarreau3bb617c2020-06-29 13:51:05 +02001209 "<tr><th>Current used connections:</th><td>%s</td></tr>"
Willy Tarreauf21d17b2019-09-08 09:24:56 +02001210 "<tr><th>Current idle connections:</th><td>%s</td></tr>"
Willy Tarreau3bb617c2020-06-29 13:51:05 +02001211 "<tr><th>- unsafe:</th><td>%s</td></tr>"
1212 "<tr><th>- safe:</th><td>%s</td></tr>"
Willy Tarreaua9fcecb2020-06-29 15:38:53 +02001213 "<tr><th>Estimated need of connections:</th><td>%s</td></tr>"
Willy Tarreauf21d17b2019-09-08 09:24:56 +02001214 "<tr><th>Active connections limit:</th><td>%s</td></tr>"
1215 "<tr><th>Idle connections limit:</th><td>%s</td></tr>"
1216 "</table></div></u>"
1217 "</td><td>%s</td><td>%s</td>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001218 "<td><u>%s<div class=tips><table class=det>"
1219 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
1220 "",
Willy Tarreauf21d17b2019-09-08 09:24:56 +02001221 U2H(stats[ST_F_SCUR].u.u32),
Willy Tarreau3bb617c2020-06-29 13:51:05 +02001222 U2H(stats[ST_F_SCUR].u.u32),
1223 U2H(stats[ST_F_USED_CONN_CUR].u.u32),
1224 U2H(stats[ST_F_SRV_ICUR].u.u32),
1225 U2H(stats[ST_F_IDLE_CONN_CUR].u.u32),
1226 U2H(stats[ST_F_SAFE_CONN_CUR].u.u32),
Willy Tarreaua9fcecb2020-06-29 15:38:53 +02001227 U2H(stats[ST_F_NEED_CONN_EST].u.u32),
Willy Tarreau3bb617c2020-06-29 13:51:05 +02001228
Willy Tarreauf21d17b2019-09-08 09:24:56 +02001229 LIM2A(stats[ST_F_SLIM].u.u32, "-"),
1230 stats[ST_F_SRV_ILIM].type ? U2H(stats[ST_F_SRV_ILIM].u.u32) : "-",
1231 U2H(stats[ST_F_SMAX].u.u32), LIM2A(stats[ST_F_SLIM].u.u32, "-"),
William Lallemand74c24fb2016-11-21 17:18:36 +01001232 U2H(stats[ST_F_STOT].u.u64),
1233 U2H(stats[ST_F_STOT].u.u64));
1234
1235 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
1236 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001237 chunk_appendf(out,
Willy Tarreauf1573842018-12-14 11:35:36 +01001238 "<tr><th>New connections:</th><td>%s</td></tr>"
1239 "<tr><th>Reused connections:</th><td>%s</td><td>(%d%%)</td></tr>"
Marcin Deranek3c27dda2020-05-15 18:32:51 +02001240 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001241 "<tr><th>- HTTP 1xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
1242 "<tr><th>- HTTP 2xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
1243 "<tr><th>- HTTP 3xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
1244 "<tr><th>- HTTP 4xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
1245 "<tr><th>- HTTP 5xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
1246 "<tr><th>- other responses:</th><td>%s</td><td>(%d%%)</td></tr>"
Willy Tarreau1b0f85e2018-05-28 15:12:40 +02001247 "<tr><th>Failed hdr rewrites:</th><td>%s</td></tr>"
Christopher Faulet0159ee42019-12-16 14:40:39 +01001248 "<tr><th>Internal error:</th><td>%s</td></tr>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001249 "",
Willy Tarreauf1573842018-12-14 11:35:36 +01001250 U2H(stats[ST_F_CONNECT].u.u64),
1251 U2H(stats[ST_F_REUSE].u.u64),
1252 (stats[ST_F_CONNECT].u.u64 + stats[ST_F_REUSE].u.u64) ?
1253 (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 +02001254 U2H(stats[ST_F_REQ_TOT].u.u64),
1255 U2H(stats[ST_F_HRSP_1XX].u.u64), stats[ST_F_REQ_TOT].u.u64 ?
1256 (int)(100 * stats[ST_F_HRSP_1XX].u.u64 / stats[ST_F_REQ_TOT].u.u64) : 0,
1257 U2H(stats[ST_F_HRSP_2XX].u.u64), stats[ST_F_REQ_TOT].u.u64 ?
1258 (int)(100 * stats[ST_F_HRSP_2XX].u.u64 / stats[ST_F_REQ_TOT].u.u64) : 0,
1259 U2H(stats[ST_F_HRSP_3XX].u.u64), stats[ST_F_REQ_TOT].u.u64 ?
1260 (int)(100 * stats[ST_F_HRSP_3XX].u.u64 / stats[ST_F_REQ_TOT].u.u64) : 0,
1261 U2H(stats[ST_F_HRSP_4XX].u.u64), stats[ST_F_REQ_TOT].u.u64 ?
1262 (int)(100 * stats[ST_F_HRSP_4XX].u.u64 / stats[ST_F_REQ_TOT].u.u64) : 0,
1263 U2H(stats[ST_F_HRSP_5XX].u.u64), stats[ST_F_REQ_TOT].u.u64 ?
1264 (int)(100 * stats[ST_F_HRSP_5XX].u.u64 / stats[ST_F_REQ_TOT].u.u64) : 0,
1265 U2H(stats[ST_F_HRSP_OTHER].u.u64), stats[ST_F_REQ_TOT].u.u64 ?
1266 (int)(100 * stats[ST_F_HRSP_OTHER].u.u64 / stats[ST_F_REQ_TOT].u.u64) : 0,
Christopher Faulet0159ee42019-12-16 14:40:39 +01001267 U2H(stats[ST_F_WREW].u.u64),
1268 U2H(stats[ST_F_EINT].u.u64));
William Lallemand74c24fb2016-11-21 17:18:36 +01001269 }
1270
Christopher Faulet0d1c2a62019-11-08 14:59:51 +01001271 chunk_appendf(out, "<tr><th colspan=3>Max / Avg over last 1024 success. conn.</th></tr>");
1272 chunk_appendf(out, "<tr><th>- Queue time:</th><td>%s / %s</td><td>ms</td></tr>",
1273 U2H(stats[ST_F_QT_MAX].u.u32), U2H(stats[ST_F_QTIME].u.u32));
1274 chunk_appendf(out, "<tr><th>- Connect time:</th><td>%s / %s</td><td>ms</td></tr>",
1275 U2H(stats[ST_F_CT_MAX].u.u32), U2H(stats[ST_F_CTIME].u.u32));
William Lallemand74c24fb2016-11-21 17:18:36 +01001276 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
Christopher Faulet0d1c2a62019-11-08 14:59:51 +01001277 chunk_appendf(out, "<tr><th>- Responses time:</th><td>%s / %s</td><td>ms</td></tr>",
1278 U2H(stats[ST_F_RT_MAX].u.u32), U2H(stats[ST_F_RTIME].u.u32));
1279 chunk_appendf(out, "<tr><th>- Total time:</th><td>%s / %s</td><td>ms</td></tr>",
1280 U2H(stats[ST_F_TT_MAX].u.u32), U2H(stats[ST_F_TTIME].u.u32));
William Lallemand74c24fb2016-11-21 17:18:36 +01001281
1282 chunk_appendf(out,
1283 "</table></div></u></td>"
1284 /* sessions: lbtot, last */
1285 "<td>%s</td><td>%s</td>",
1286 U2H(stats[ST_F_LBTOT].u.u64),
1287 human_time(stats[ST_F_LASTSESS].u.s32, 1));
1288
1289 chunk_appendf(out,
1290 /* bytes : in, out */
1291 "<td>%s</td><td>%s</td>"
1292 /* denied: req, resp */
1293 "<td></td><td>%s</td>"
1294 /* errors : request, connect */
1295 "<td></td><td>%s</td>"
1296 /* errors : response */
1297 "<td><u>%s<div class=tips>Connection resets during transfers: %lld client, %lld server</div></u></td>"
1298 /* warnings: retries, redispatches */
1299 "<td>%lld</td><td>%lld</td>"
1300 "",
1301 U2H(stats[ST_F_BIN].u.u64), U2H(stats[ST_F_BOUT].u.u64),
1302 U2H(stats[ST_F_DRESP].u.u64),
1303 U2H(stats[ST_F_ECON].u.u64),
1304 U2H(stats[ST_F_ERESP].u.u64),
1305 (long long)stats[ST_F_CLI_ABRT].u.u64,
1306 (long long)stats[ST_F_SRV_ABRT].u.u64,
1307 (long long)stats[ST_F_WRETR].u.u64,
1308 (long long)stats[ST_F_WREDIS].u.u64);
1309
1310 /* status, last change */
1311 chunk_appendf(out, "<td class=ac>");
1312
1313 /* FIXME!!!!
1314 * LASTCHG should contain the last change for *this* server and must be computed
1315 * properly above, as was done below, ie: this server if maint, otherwise ref server
1316 * if tracking. Note that ref is either local or remote depending on tracking.
1317 */
1318
1319
Willy Tarreau7b524852020-08-11 10:26:36 +02001320 if (strncmp(field_str(stats, ST_F_STATUS), "MAINT", 5) == 0) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001321 chunk_appendf(out, "%s MAINT", human_time(stats[ST_F_LASTCHG].u.u32, 1));
1322 }
Willy Tarreau7b524852020-08-11 10:26:36 +02001323 else if (strcmp(field_str(stats, ST_F_STATUS), "no check") == 0) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001324 chunk_strcat(out, "<i>no check</i>");
1325 }
1326 else {
1327 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 +02001328 if (strncmp(field_str(stats, ST_F_STATUS), "DOWN", 4) == 0) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001329 if (stats[ST_F_CHECK_HEALTH].u.u32)
1330 chunk_strcat(out, " &uarr;");
1331 }
1332 else if (stats[ST_F_CHECK_HEALTH].u.u32 < stats[ST_F_CHECK_RISE].u.u32 + stats[ST_F_CHECK_FALL].u.u32 - 1)
1333 chunk_strcat(out, " &darr;");
1334 }
1335
Willy Tarreau7b524852020-08-11 10:26:36 +02001336 if (strncmp(field_str(stats, ST_F_STATUS), "DOWN", 4) == 0 &&
William Lallemand74c24fb2016-11-21 17:18:36 +01001337 stats[ST_F_AGENT_STATUS].type && !stats[ST_F_AGENT_HEALTH].u.u32) {
1338 chunk_appendf(out,
1339 "</td><td class=ac><u> %s",
1340 field_str(stats, ST_F_AGENT_STATUS));
1341
1342 if (stats[ST_F_AGENT_CODE].type)
1343 chunk_appendf(out, "/%d", stats[ST_F_AGENT_CODE].u.u32);
1344
1345 if (stats[ST_F_AGENT_DURATION].type)
1346 chunk_appendf(out, " in %lums", (long)stats[ST_F_AGENT_DURATION].u.u64);
1347
1348 chunk_appendf(out, "<div class=tips>%s", field_str(stats, ST_F_AGENT_DESC));
1349
1350 if (*field_str(stats, ST_F_LAST_AGT)) {
1351 chunk_appendf(out, ": ");
1352 chunk_initstr(&src, field_str(stats, ST_F_LAST_AGT));
1353 chunk_htmlencode(out, &src);
1354 }
1355 chunk_appendf(out, "</div></u>");
1356 }
1357 else if (stats[ST_F_CHECK_STATUS].type) {
1358 chunk_appendf(out,
1359 "</td><td class=ac><u> %s",
1360 field_str(stats, ST_F_CHECK_STATUS));
1361
1362 if (stats[ST_F_CHECK_CODE].type)
1363 chunk_appendf(out, "/%d", stats[ST_F_CHECK_CODE].u.u32);
1364
1365 if (stats[ST_F_CHECK_DURATION].type)
1366 chunk_appendf(out, " in %lums", (long)stats[ST_F_CHECK_DURATION].u.u64);
1367
1368 chunk_appendf(out, "<div class=tips>%s", field_str(stats, ST_F_CHECK_DESC));
1369
1370 if (*field_str(stats, ST_F_LAST_CHK)) {
1371 chunk_appendf(out, ": ");
1372 chunk_initstr(&src, field_str(stats, ST_F_LAST_CHK));
1373 chunk_htmlencode(out, &src);
1374 }
1375 chunk_appendf(out, "</div></u>");
1376 }
1377 else
1378 chunk_appendf(out, "</td><td>");
1379
1380 chunk_appendf(out,
Willy Tarreaubd715102020-10-23 22:44:30 +02001381 /* weight / uweight */
1382 "</td><td class=ac>%d/%d</td>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001383 /* act, bck */
1384 "<td class=ac>%s</td><td class=ac>%s</td>"
1385 "",
Willy Tarreaubd715102020-10-23 22:44:30 +02001386 stats[ST_F_WEIGHT].u.u32, stats[ST_F_UWEIGHT].u.u32,
William Lallemand74c24fb2016-11-21 17:18:36 +01001387 stats[ST_F_BCK].u.u32 ? "-" : "Y",
1388 stats[ST_F_BCK].u.u32 ? "Y" : "-");
1389
1390 /* check failures: unique, fatal, down time */
1391 if (strcmp(field_str(stats, ST_F_STATUS), "MAINT (resolution)") == 0) {
1392 chunk_appendf(out, "<td class=ac colspan=3>resolution</td>");
1393 }
1394 else if (stats[ST_F_CHKFAIL].type) {
1395 chunk_appendf(out, "<td><u>%lld", (long long)stats[ST_F_CHKFAIL].u.u64);
1396
1397 if (stats[ST_F_HANAFAIL].type)
1398 chunk_appendf(out, "/%lld", (long long)stats[ST_F_HANAFAIL].u.u64);
1399
1400 chunk_appendf(out,
1401 "<div class=tips>Failed Health Checks%s</div></u></td>"
1402 "<td>%lld</td><td>%s</td>"
1403 "",
1404 stats[ST_F_HANAFAIL].type ? "/Health Analyses" : "",
1405 (long long)stats[ST_F_CHKDOWN].u.u64, human_time(stats[ST_F_DOWNTIME].u.u32, 1));
1406 }
1407 else if (strcmp(field_str(stats, ST_F_STATUS), "MAINT") != 0 && field_format(stats, ST_F_TRACKED) == FF_STR) {
1408 /* tracking a server (hence inherited maint would appear as "MAINT (via...)" */
1409 chunk_appendf(out,
1410 "<td class=ac colspan=3><a class=lfsb href=\"#%s\">via %s</a></td>",
1411 field_str(stats, ST_F_TRACKED), field_str(stats, ST_F_TRACKED));
1412 }
1413 else
1414 chunk_appendf(out, "<td colspan=3></td>");
1415
1416 /* throttle */
1417 if (stats[ST_F_THROTTLE].type)
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001418 chunk_appendf(out, "<td class=ac>%d %%</td>\n", stats[ST_F_THROTTLE].u.u32);
William Lallemand74c24fb2016-11-21 17:18:36 +01001419 else
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001420 chunk_appendf(out, "<td class=ac>-</td>");
1421
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001422 if (flags & STAT_SHMODULES) {
1423 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001424 chunk_appendf(out, "<td>");
1425
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001426 if (stats_px_get_cap(mod->domain_flags) & STATS_PX_CAP_SRV) {
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001427 chunk_appendf(out,
1428 "<u>%s<div class=tips><table class=det>",
1429 mod->name);
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001430 for (j = 0; j < mod->stats_count; ++j) {
1431 chunk_appendf(out,
1432 "<tr><th>%s</th><td>%s</td></tr>",
1433 mod->stats[j].desc, field_to_html_str(&stats[ST_F_TOTAL_FIELDS + i]));
1434 ++i;
1435 }
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001436 chunk_appendf(out, "</table></div></u>");
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001437 } else {
1438 i += mod->stats_count;
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001439 }
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001440
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001441 chunk_appendf(out, "</td>");
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001442 }
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001443 }
1444
1445 chunk_appendf(out, "</tr>\n");
William Lallemand74c24fb2016-11-21 17:18:36 +01001446 }
1447 else if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_BE) {
1448 chunk_appendf(out, "<tr class=\"backend\">");
Willy Tarreauab02b3f2019-10-09 11:11:46 +02001449 if (flags & STAT_ADMIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001450 /* Column sub-heading for Enable or Disable server */
1451 chunk_appendf(out, "<td></td>");
1452 }
1453 chunk_appendf(out,
1454 "<td class=ac>"
1455 /* name */
1456 "%s<a name=\"%s/Backend\"></a>"
1457 "<a class=lfsb href=\"#%s/Backend\">Backend</a>"
1458 "",
Willy Tarreau708c4162019-10-09 10:19:16 +02001459 (flags & STAT_SHLGNDS)?"<u>":"",
William Lallemand74c24fb2016-11-21 17:18:36 +01001460 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_PXNAME));
1461
Willy Tarreau708c4162019-10-09 10:19:16 +02001462 if (flags & STAT_SHLGNDS) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001463 /* balancing */
1464 chunk_appendf(out, "<div class=tips>balancing: %s",
1465 field_str(stats, ST_F_ALGO));
1466
1467 /* cookie */
1468 if (stats[ST_F_COOKIE].type) {
1469 chunk_appendf(out, ", cookie: '");
1470 chunk_initstr(&src, field_str(stats, ST_F_COOKIE));
1471 chunk_htmlencode(out, &src);
1472 chunk_appendf(out, "'");
1473 }
1474 chunk_appendf(out, "</div>");
1475 }
1476
1477 chunk_appendf(out,
1478 "%s</td>"
1479 /* queue : current, max */
1480 "<td>%s</td><td>%s</td><td></td>"
1481 /* sessions rate : current, max, limit */
1482 "<td>%s</td><td>%s</td><td></td>"
1483 "",
Willy Tarreau708c4162019-10-09 10:19:16 +02001484 (flags & STAT_SHLGNDS)?"</u>":"",
William Lallemand74c24fb2016-11-21 17:18:36 +01001485 U2H(stats[ST_F_QCUR].u.u32), U2H(stats[ST_F_QMAX].u.u32),
1486 U2H(stats[ST_F_RATE].u.u32), U2H(stats[ST_F_RATE_MAX].u.u32));
1487
1488 chunk_appendf(out,
1489 /* sessions: current, max, limit, total */
1490 "<td>%s</td><td>%s</td><td>%s</td>"
1491 "<td><u>%s<div class=tips><table class=det>"
1492 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
1493 "",
Willy Tarreau8e0f1752016-12-12 15:07:29 +01001494 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 +01001495 U2H(stats[ST_F_STOT].u.u64),
1496 U2H(stats[ST_F_STOT].u.u64));
1497
1498 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
1499 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0) {
1500 chunk_appendf(out,
Willy Tarreauf1573842018-12-14 11:35:36 +01001501 "<tr><th>New connections:</th><td>%s</td></tr>"
1502 "<tr><th>Reused connections:</th><td>%s</td><td>(%d%%)</td></tr>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001503 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
1504 "<tr><th>- HTTP 1xx responses:</th><td>%s</td></tr>"
1505 "<tr><th>- HTTP 2xx responses:</th><td>%s</td></tr>"
1506 "<tr><th>&nbsp;&nbsp;Compressed 2xx:</th><td>%s</td><td>(%d%%)</td></tr>"
1507 "<tr><th>- HTTP 3xx responses:</th><td>%s</td></tr>"
1508 "<tr><th>- HTTP 4xx responses:</th><td>%s</td></tr>"
1509 "<tr><th>- HTTP 5xx responses:</th><td>%s</td></tr>"
1510 "<tr><th>- other responses:</th><td>%s</td></tr>"
Willy Tarreaua1214a52018-12-14 14:00:25 +01001511 "<tr><th>Cache lookups:</th><td>%s</td></tr>"
1512 "<tr><th>Cache hits:</th><td>%s</td><td>(%d%%)</td></tr>"
Willy Tarreau1b0f85e2018-05-28 15:12:40 +02001513 "<tr><th>Failed hdr rewrites:</th><td>%s</td></tr>"
Christopher Faulet0159ee42019-12-16 14:40:39 +01001514 "<tr><th>Internal errors:</th><td>%s</td></tr>"
Christopher Faulet0d1c2a62019-11-08 14:59:51 +01001515 "",
Willy Tarreauf1573842018-12-14 11:35:36 +01001516 U2H(stats[ST_F_CONNECT].u.u64),
1517 U2H(stats[ST_F_REUSE].u.u64),
1518 (stats[ST_F_CONNECT].u.u64 + stats[ST_F_REUSE].u.u64) ?
1519 (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 +01001520 U2H(stats[ST_F_REQ_TOT].u.u64),
1521 U2H(stats[ST_F_HRSP_1XX].u.u64),
1522 U2H(stats[ST_F_HRSP_2XX].u.u64),
1523 U2H(stats[ST_F_COMP_RSP].u.u64),
1524 stats[ST_F_HRSP_2XX].u.u64 ?
1525 (int)(100 * stats[ST_F_COMP_RSP].u.u64 / stats[ST_F_HRSP_2XX].u.u64) : 0,
1526 U2H(stats[ST_F_HRSP_3XX].u.u64),
1527 U2H(stats[ST_F_HRSP_4XX].u.u64),
1528 U2H(stats[ST_F_HRSP_5XX].u.u64),
Willy Tarreaufeead3a2018-12-14 13:48:44 +01001529 U2H(stats[ST_F_HRSP_OTHER].u.u64),
Willy Tarreaua1214a52018-12-14 14:00:25 +01001530 U2H(stats[ST_F_CACHE_LOOKUPS].u.u64),
1531 U2H(stats[ST_F_CACHE_HITS].u.u64),
1532 stats[ST_F_CACHE_LOOKUPS].u.u64 ?
1533 (int)(100 * stats[ST_F_CACHE_HITS].u.u64 / stats[ST_F_CACHE_LOOKUPS].u.u64) : 0,
Christopher Faulet0159ee42019-12-16 14:40:39 +01001534 U2H(stats[ST_F_WREW].u.u64),
1535 U2H(stats[ST_F_EINT].u.u64));
William Lallemand74c24fb2016-11-21 17:18:36 +01001536 }
1537
Christopher Faulet0d1c2a62019-11-08 14:59:51 +01001538 chunk_appendf(out, "<tr><th colspan=3>Max / Avg over last 1024 success. conn.</th></tr>");
1539 chunk_appendf(out, "<tr><th>- Queue time:</th><td>%s / %s</td><td>ms</td></tr>",
1540 U2H(stats[ST_F_QT_MAX].u.u32), U2H(stats[ST_F_QTIME].u.u32));
1541 chunk_appendf(out, "<tr><th>- Connect time:</th><td>%s / %s</td><td>ms</td></tr>",
1542 U2H(stats[ST_F_CT_MAX].u.u32), U2H(stats[ST_F_CTIME].u.u32));
William Lallemand74c24fb2016-11-21 17:18:36 +01001543 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
Christopher Faulet0d1c2a62019-11-08 14:59:51 +01001544 chunk_appendf(out, "<tr><th>- Responses time:</th><td>%s / %s</td><td>ms</td></tr>",
1545 U2H(stats[ST_F_RT_MAX].u.u32), U2H(stats[ST_F_RTIME].u.u32));
1546 chunk_appendf(out, "<tr><th>- Total time:</th><td>%s / %s</td><td>ms</td></tr>",
1547 U2H(stats[ST_F_TT_MAX].u.u32), U2H(stats[ST_F_TTIME].u.u32));
William Lallemand74c24fb2016-11-21 17:18:36 +01001548
1549 chunk_appendf(out,
1550 "</table></div></u></td>"
1551 /* sessions: lbtot, last */
1552 "<td>%s</td><td>%s</td>"
1553 /* bytes: in */
1554 "<td>%s</td>"
1555 "",
1556 U2H(stats[ST_F_LBTOT].u.u64),
1557 human_time(stats[ST_F_LASTSESS].u.s32, 1),
1558 U2H(stats[ST_F_BIN].u.u64));
1559
1560 chunk_appendf(out,
1561 /* bytes:out + compression stats (via hover): comp_in, comp_out, comp_byp */
1562 "<td>%s%s<div class=tips><table class=det>"
1563 "<tr><th>Response bytes in:</th><td>%s</td></tr>"
1564 "<tr><th>Compression in:</th><td>%s</td></tr>"
1565 "<tr><th>Compression out:</th><td>%s</td><td>(%d%%)</td></tr>"
1566 "<tr><th>Compression bypass:</th><td>%s</td></tr>"
1567 "<tr><th>Total bytes saved:</th><td>%s</td><td>(%d%%)</td></tr>"
1568 "</table></div>%s</td>",
1569 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "<u>":"",
1570 U2H(stats[ST_F_BOUT].u.u64),
1571 U2H(stats[ST_F_BOUT].u.u64),
1572 U2H(stats[ST_F_COMP_IN].u.u64),
1573 U2H(stats[ST_F_COMP_OUT].u.u64),
1574 stats[ST_F_COMP_IN].u.u64 ? (int)(stats[ST_F_COMP_OUT].u.u64 * 100 / stats[ST_F_COMP_IN].u.u64) : 0,
1575 U2H(stats[ST_F_COMP_BYP].u.u64),
1576 U2H(stats[ST_F_COMP_IN].u.u64 - stats[ST_F_COMP_OUT].u.u64),
1577 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,
1578 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "</u>":"");
1579
1580 chunk_appendf(out,
1581 /* denied: req, resp */
1582 "<td>%s</td><td>%s</td>"
1583 /* errors : request, connect */
1584 "<td></td><td>%s</td>"
1585 /* errors : response */
1586 "<td><u>%s<div class=tips>Connection resets during transfers: %lld client, %lld server</div></u></td>"
1587 /* warnings: retries, redispatches */
1588 "<td>%lld</td><td>%lld</td>"
1589 /* backend status: reflect backend status (up/down): we display UP
1590 * if the backend has known working servers or if it has no server at
1591 * all (eg: for stats). Then we display the total weight, number of
1592 * active and backups. */
Willy Tarreaubd715102020-10-23 22:44:30 +02001593 "<td class=ac>%s %s</td><td class=ac>&nbsp;</td><td class=ac>%d/%d</td>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001594 "<td class=ac>%d</td><td class=ac>%d</td>"
1595 "",
1596 U2H(stats[ST_F_DREQ].u.u64), U2H(stats[ST_F_DRESP].u.u64),
1597 U2H(stats[ST_F_ECON].u.u64),
1598 U2H(stats[ST_F_ERESP].u.u64),
1599 (long long)stats[ST_F_CLI_ABRT].u.u64,
1600 (long long)stats[ST_F_SRV_ABRT].u.u64,
1601 (long long)stats[ST_F_WRETR].u.u64, (long long)stats[ST_F_WREDIS].u.u64,
1602 human_time(stats[ST_F_LASTCHG].u.u32, 1),
1603 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 +02001604 stats[ST_F_WEIGHT].u.u32, stats[ST_F_UWEIGHT].u.u32,
William Lallemand74c24fb2016-11-21 17:18:36 +01001605 stats[ST_F_ACT].u.u32, stats[ST_F_BCK].u.u32);
1606
1607 chunk_appendf(out,
1608 /* rest of backend: nothing, down transitions, total downtime, throttle */
1609 "<td class=ac>&nbsp;</td><td>%d</td>"
1610 "<td>%s</td>"
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001611 "<td></td>",
William Lallemand74c24fb2016-11-21 17:18:36 +01001612 stats[ST_F_CHKDOWN].u.u32,
1613 stats[ST_F_DOWNTIME].type ? human_time(stats[ST_F_DOWNTIME].u.u32, 1) : "&nbsp;");
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001614
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001615 if (flags & STAT_SHMODULES) {
1616 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001617 chunk_appendf(out, "<td>");
1618
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001619 if (stats_px_get_cap(mod->domain_flags) & STATS_PX_CAP_BE) {
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001620 chunk_appendf(out,
1621 "<u>%s<div class=tips><table class=det>",
1622 mod->name);
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001623 for (j = 0; j < mod->stats_count; ++j) {
1624 chunk_appendf(out,
1625 "<tr><th>%s</th><td>%s</td></tr>",
1626 mod->stats[j].desc, field_to_html_str(&stats[ST_F_TOTAL_FIELDS + i]));
1627 ++i;
1628 }
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001629 chunk_appendf(out, "</table></div></u>");
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001630 } else {
1631 i += mod->stats_count;
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001632 }
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001633
1634 chunk_appendf(out, "</td>");
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001635 }
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001636 }
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001637
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001638 chunk_appendf(out, "</tr>");
William Lallemand74c24fb2016-11-21 17:18:36 +01001639 }
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001640
William Lallemand74c24fb2016-11-21 17:18:36 +01001641 return 1;
1642}
1643
Amaury Denoyelle97323c92020-10-02 18:32:01 +02001644int stats_dump_one_line(const struct field *stats, size_t stats_count,
1645 struct appctx *appctx)
William Lallemand74c24fb2016-11-21 17:18:36 +01001646{
Willy Tarreau91cefca2022-05-03 17:08:29 +02001647 struct show_stat_ctx *ctx = appctx->svcctx;
Simon Horman05ee2132017-01-04 09:37:25 +01001648 int ret;
1649
Willy Tarreau91cefca2022-05-03 17:08:29 +02001650 if (ctx->flags & STAT_FMT_HTML)
Aurelien DARRAGONe76a0272022-12-15 12:10:03 +01001651 ret = stats_dump_fields_html(&trash, stats, ctx);
Willy Tarreau91cefca2022-05-03 17:08:29 +02001652 else if (ctx->flags & STAT_FMT_TYPED)
Aurelien DARRAGONe76a0272022-12-15 12:10:03 +01001653 ret = stats_dump_fields_typed(&trash, stats, stats_count, ctx);
Willy Tarreau91cefca2022-05-03 17:08:29 +02001654 else if (ctx->flags & STAT_FMT_JSON)
Aurelien DARRAGONe76a0272022-12-15 12:10:03 +01001655 ret = stats_dump_fields_json(&trash, stats, stats_count, ctx);
William Lallemand74c24fb2016-11-21 17:18:36 +01001656 else
Aurelien DARRAGONe76a0272022-12-15 12:10:03 +01001657 ret = stats_dump_fields_csv(&trash, stats, stats_count, ctx);
Simon Horman05ee2132017-01-04 09:37:25 +01001658
1659 if (ret)
Willy Tarreau91cefca2022-05-03 17:08:29 +02001660 ctx->flags |= STAT_STARTED;
Simon Horman05ee2132017-01-04 09:37:25 +01001661
1662 return ret;
William Lallemand74c24fb2016-11-21 17:18:36 +01001663}
1664
William Dauchy0ef54392021-01-17 18:27:45 +01001665/* Fill <stats> with the frontend statistics. <stats> is preallocated array of
Christopher Fauletd808f172021-01-22 17:33:22 +01001666 * length <len>. If <selected_field> is != NULL, only fill this one. The length
1667 * of the array must be at least ST_F_TOTAL_FIELDS. If this length is less than
1668 * this value, or if the selected field is not implemented for frontends, the
1669 * function returns 0, otherwise, it returns 1.
William Lallemand74c24fb2016-11-21 17:18:36 +01001670 */
William Dauchy0ef54392021-01-17 18:27:45 +01001671int stats_fill_fe_stats(struct proxy *px, struct field *stats, int len,
1672 enum stat_field *selected_field)
William Lallemand74c24fb2016-11-21 17:18:36 +01001673{
William Dauchy0ef54392021-01-17 18:27:45 +01001674 enum stat_field current_field = (selected_field != NULL ? *selected_field : 0);
William Dauchy0ef54392021-01-17 18:27:45 +01001675
William Lallemand74c24fb2016-11-21 17:18:36 +01001676 if (len < ST_F_TOTAL_FIELDS)
1677 return 0;
1678
William Dauchy0ef54392021-01-17 18:27:45 +01001679 for (; current_field < ST_F_TOTAL_FIELDS; current_field++) {
Christopher Faulet8596bfb2021-01-25 15:16:41 +01001680 struct field metric = { 0 };
1681
William Dauchy0ef54392021-01-17 18:27:45 +01001682 switch (current_field) {
1683 case ST_F_PXNAME:
1684 metric = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
1685 break;
1686 case ST_F_SVNAME:
1687 metric = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, "FRONTEND");
1688 break;
1689 case ST_F_MODE:
1690 metric = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
1691 break;
1692 case ST_F_SCUR:
1693 metric = mkf_u32(0, px->feconn);
1694 break;
1695 case ST_F_SMAX:
1696 metric = mkf_u32(FN_MAX, px->fe_counters.conn_max);
1697 break;
1698 case ST_F_SLIM:
1699 metric = mkf_u32(FO_CONFIG|FN_LIMIT, px->maxconn);
1700 break;
1701 case ST_F_STOT:
1702 metric = mkf_u64(FN_COUNTER, px->fe_counters.cum_sess);
1703 break;
1704 case ST_F_BIN:
1705 metric = mkf_u64(FN_COUNTER, px->fe_counters.bytes_in);
1706 break;
1707 case ST_F_BOUT:
1708 metric = mkf_u64(FN_COUNTER, px->fe_counters.bytes_out);
1709 break;
1710 case ST_F_DREQ:
1711 metric = mkf_u64(FN_COUNTER, px->fe_counters.denied_req);
1712 break;
1713 case ST_F_DRESP:
1714 metric = mkf_u64(FN_COUNTER, px->fe_counters.denied_resp);
1715 break;
1716 case ST_F_EREQ:
1717 metric = mkf_u64(FN_COUNTER, px->fe_counters.failed_req);
1718 break;
1719 case ST_F_DCON:
1720 metric = mkf_u64(FN_COUNTER, px->fe_counters.denied_conn);
1721 break;
1722 case ST_F_DSES:
1723 metric = mkf_u64(FN_COUNTER, px->fe_counters.denied_sess);
1724 break;
Aurelien DARRAGONcddec0a2022-09-09 15:58:57 +02001725 case ST_F_STATUS: {
1726 const char *state;
1727
1728 if (px->flags & (PR_FL_DISABLED|PR_FL_STOPPED))
1729 state = "STOP";
1730 else if (px->flags & PR_FL_PAUSED)
1731 state = "PAUSED";
1732 else
1733 state = "OPEN";
1734 metric = mkf_str(FO_STATUS, state);
William Dauchy0ef54392021-01-17 18:27:45 +01001735 break;
Aurelien DARRAGONcddec0a2022-09-09 15:58:57 +02001736 }
William Dauchy0ef54392021-01-17 18:27:45 +01001737 case ST_F_PID:
Willy Tarreaue8422bf2021-06-15 09:08:18 +02001738 metric = mkf_u32(FO_KEY, 1);
William Dauchy0ef54392021-01-17 18:27:45 +01001739 break;
1740 case ST_F_IID:
1741 metric = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
1742 break;
1743 case ST_F_SID:
1744 metric = mkf_u32(FO_KEY|FS_SERVICE, 0);
1745 break;
1746 case ST_F_TYPE:
1747 metric = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_FE);
1748 break;
1749 case ST_F_RATE:
1750 metric = mkf_u32(FN_RATE, read_freq_ctr(&px->fe_sess_per_sec));
1751 break;
1752 case ST_F_RATE_LIM:
1753 metric = mkf_u32(FO_CONFIG|FN_LIMIT, px->fe_sps_lim);
1754 break;
1755 case ST_F_RATE_MAX:
1756 metric = mkf_u32(FN_MAX, px->fe_counters.sps_max);
1757 break;
1758 case ST_F_WREW:
1759 metric = mkf_u64(FN_COUNTER, px->fe_counters.failed_rewrites);
1760 break;
1761 case ST_F_EINT:
1762 metric = mkf_u64(FN_COUNTER, px->fe_counters.internal_errors);
1763 break;
1764 case ST_F_HRSP_1XX:
William Dauchy2107a0f2021-01-22 21:09:48 +01001765 if (px->mode == PR_MODE_HTTP)
1766 metric = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[1]);
William Dauchy0ef54392021-01-17 18:27:45 +01001767 break;
1768 case ST_F_HRSP_2XX:
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.rsp[2]);
William Dauchy0ef54392021-01-17 18:27:45 +01001771 break;
1772 case ST_F_HRSP_3XX:
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.rsp[3]);
William Dauchy0ef54392021-01-17 18:27:45 +01001775 break;
1776 case ST_F_HRSP_4XX:
William Dauchy2107a0f2021-01-22 21:09:48 +01001777 if (px->mode == PR_MODE_HTTP)
1778 metric = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[4]);
William Dauchy0ef54392021-01-17 18:27:45 +01001779 break;
1780 case ST_F_HRSP_5XX:
William Dauchy2107a0f2021-01-22 21:09:48 +01001781 if (px->mode == PR_MODE_HTTP)
1782 metric = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[5]);
William Dauchy0ef54392021-01-17 18:27:45 +01001783 break;
1784 case ST_F_HRSP_OTHER:
William Dauchy2107a0f2021-01-22 21:09:48 +01001785 if (px->mode == PR_MODE_HTTP)
1786 metric = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[0]);
William Dauchy0ef54392021-01-17 18:27:45 +01001787 break;
1788 case ST_F_INTERCEPTED:
William Dauchy2107a0f2021-01-22 21:09:48 +01001789 if (px->mode == PR_MODE_HTTP)
1790 metric = mkf_u64(FN_COUNTER, px->fe_counters.intercepted_req);
William Dauchy0ef54392021-01-17 18:27:45 +01001791 break;
1792 case ST_F_CACHE_LOOKUPS:
William Dauchy2107a0f2021-01-22 21:09:48 +01001793 if (px->mode == PR_MODE_HTTP)
1794 metric = mkf_u64(FN_COUNTER, px->fe_counters.p.http.cache_lookups);
William Dauchy0ef54392021-01-17 18:27:45 +01001795 break;
1796 case ST_F_CACHE_HITS:
William Dauchy2107a0f2021-01-22 21:09:48 +01001797 if (px->mode == PR_MODE_HTTP)
1798 metric = mkf_u64(FN_COUNTER, px->fe_counters.p.http.cache_hits);
William Dauchy0ef54392021-01-17 18:27:45 +01001799 break;
1800 case ST_F_REQ_RATE:
1801 metric = mkf_u32(FN_RATE, read_freq_ctr(&px->fe_req_per_sec));
1802 break;
1803 case ST_F_REQ_RATE_MAX:
1804 metric = mkf_u32(FN_MAX, px->fe_counters.p.http.rps_max);
1805 break;
1806 case ST_F_REQ_TOT:
1807 metric = mkf_u64(FN_COUNTER, px->fe_counters.p.http.cum_req);
1808 break;
1809 case ST_F_COMP_IN:
1810 metric = mkf_u64(FN_COUNTER, px->fe_counters.comp_in);
1811 break;
1812 case ST_F_COMP_OUT:
1813 metric = mkf_u64(FN_COUNTER, px->fe_counters.comp_out);
1814 break;
1815 case ST_F_COMP_BYP:
1816 metric = mkf_u64(FN_COUNTER, px->fe_counters.comp_byp);
1817 break;
1818 case ST_F_COMP_RSP:
1819 metric = mkf_u64(FN_COUNTER, px->fe_counters.p.http.comp_rsp);
1820 break;
1821 case ST_F_CONN_RATE:
1822 metric = mkf_u32(FN_RATE, read_freq_ctr(&px->fe_conn_per_sec));
1823 break;
1824 case ST_F_CONN_RATE_MAX:
1825 metric = mkf_u32(FN_MAX, px->fe_counters.cps_max);
1826 break;
1827 case ST_F_CONN_TOT:
1828 metric = mkf_u64(FN_COUNTER, px->fe_counters.cum_conn);
1829 break;
1830 default:
Christopher Fauletd808f172021-01-22 17:33:22 +01001831 /* not used for frontends. If a specific metric
1832 * is requested, return an error. Otherwise continue.
1833 */
1834 if (selected_field != NULL)
1835 return 0;
1836 continue;
William Dauchy0ef54392021-01-17 18:27:45 +01001837 }
1838 stats[current_field] = metric;
1839 if (selected_field != NULL)
1840 break;
William Lallemand74c24fb2016-11-21 17:18:36 +01001841 }
William Lallemand74c24fb2016-11-21 17:18:36 +01001842 return 1;
1843}
1844
1845/* Dumps a frontend's line to the trash for the current proxy <px> and uses
Willy Tarreaucaff6312022-05-27 10:17:46 +02001846 * the state from stream connector <sc>. The caller is responsible for clearing
William Lallemand74c24fb2016-11-21 17:18:36 +01001847 * the trash if needed. Returns non-zero if it emits anything, zero otherwise.
1848 */
Willy Tarreaucaff6312022-05-27 10:17:46 +02001849static int stats_dump_fe_stats(struct stconn *sc, struct proxy *px)
William Lallemand74c24fb2016-11-21 17:18:36 +01001850{
Willy Tarreaucaff6312022-05-27 10:17:46 +02001851 struct appctx *appctx = __sc_appctx(sc);
Willy Tarreau91cefca2022-05-03 17:08:29 +02001852 struct show_stat_ctx *ctx = appctx->svcctx;
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02001853 struct field *stats = stat_l[STATS_DOMAIN_PROXY];
1854 struct stats_module *mod;
1855 size_t stats_count = ST_F_TOTAL_FIELDS;
William Lallemand74c24fb2016-11-21 17:18:36 +01001856
1857 if (!(px->cap & PR_CAP_FE))
1858 return 0;
1859
Willy Tarreau91cefca2022-05-03 17:08:29 +02001860 if ((ctx->flags & STAT_BOUND) && !(ctx->type & (1 << STATS_TYPE_FE)))
William Lallemand74c24fb2016-11-21 17:18:36 +01001861 return 0;
1862
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02001863 memset(stats, 0, sizeof(struct field) * stat_count[STATS_DOMAIN_PROXY]);
1864
William Dauchy0ef54392021-01-17 18:27:45 +01001865 if (!stats_fill_fe_stats(px, stats, ST_F_TOTAL_FIELDS, NULL))
William Lallemand74c24fb2016-11-21 17:18:36 +01001866 return 0;
1867
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02001868 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
1869 void *counters;
1870
1871 if (!(stats_px_get_cap(mod->domain_flags) & STATS_PX_CAP_FE)) {
1872 stats_count += mod->stats_count;
1873 continue;
1874 }
1875
1876 counters = EXTRA_COUNTERS_GET(px->extra_counters_fe, mod);
1877 mod->fill_stats(counters, stats + stats_count);
1878 stats_count += mod->stats_count;
1879 }
1880
1881 return stats_dump_one_line(stats, stats_count, appctx);
William Lallemand74c24fb2016-11-21 17:18:36 +01001882}
1883
William Dauchy655e14e2021-02-14 23:22:54 +01001884/* Fill <stats> with the listener statistics. <stats> is preallocated array of
1885 * length <len>. The length of the array must be at least ST_F_TOTAL_FIELDS. If
1886 * this length is less then this value, the function returns 0, otherwise, it
1887 * returns 1. If selected_field is != NULL, only fill this one. <flags> can
1888 * take the value STAT_SHLGNDS.
William Lallemand74c24fb2016-11-21 17:18:36 +01001889 */
1890int stats_fill_li_stats(struct proxy *px, struct listener *l, int flags,
William Dauchy655e14e2021-02-14 23:22:54 +01001891 struct field *stats, int len, enum stat_field *selected_field)
William Lallemand74c24fb2016-11-21 17:18:36 +01001892{
William Dauchy655e14e2021-02-14 23:22:54 +01001893 enum stat_field current_field = (selected_field != NULL ? *selected_field : 0);
Willy Tarreau83061a82018-07-13 11:56:34 +02001894 struct buffer *out = get_trash_chunk();
William Lallemand74c24fb2016-11-21 17:18:36 +01001895
1896 if (len < ST_F_TOTAL_FIELDS)
1897 return 0;
1898
1899 if (!l->counters)
1900 return 0;
1901
1902 chunk_reset(out);
William Lallemand74c24fb2016-11-21 17:18:36 +01001903
William Dauchy655e14e2021-02-14 23:22:54 +01001904 for (; current_field < ST_F_TOTAL_FIELDS; current_field++) {
1905 struct field metric = { 0 };
William Lallemand74c24fb2016-11-21 17:18:36 +01001906
William Dauchy655e14e2021-02-14 23:22:54 +01001907 switch (current_field) {
1908 case ST_F_PXNAME:
1909 metric = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
1910 break;
1911 case ST_F_SVNAME:
1912 metric = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, l->name);
1913 break;
1914 case ST_F_MODE:
1915 metric = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
1916 break;
1917 case ST_F_SCUR:
1918 metric = mkf_u32(0, l->nbconn);
1919 break;
1920 case ST_F_SMAX:
1921 metric = mkf_u32(FN_MAX, l->counters->conn_max);
1922 break;
1923 case ST_F_SLIM:
1924 metric = mkf_u32(FO_CONFIG|FN_LIMIT, l->maxconn);
1925 break;
1926 case ST_F_STOT:
1927 metric = mkf_u64(FN_COUNTER, l->counters->cum_conn);
1928 break;
1929 case ST_F_BIN:
1930 metric = mkf_u64(FN_COUNTER, l->counters->bytes_in);
1931 break;
1932 case ST_F_BOUT:
1933 metric = mkf_u64(FN_COUNTER, l->counters->bytes_out);
1934 break;
1935 case ST_F_DREQ:
1936 metric = mkf_u64(FN_COUNTER, l->counters->denied_req);
1937 break;
1938 case ST_F_DRESP:
1939 metric = mkf_u64(FN_COUNTER, l->counters->denied_resp);
1940 break;
1941 case ST_F_EREQ:
1942 metric = mkf_u64(FN_COUNTER, l->counters->failed_req);
1943 break;
1944 case ST_F_DCON:
1945 metric = mkf_u64(FN_COUNTER, l->counters->denied_conn);
1946 break;
1947 case ST_F_DSES:
1948 metric = mkf_u64(FN_COUNTER, l->counters->denied_sess);
1949 break;
1950 case ST_F_STATUS:
William Dauchy3679d0c2021-02-14 23:22:55 +01001951 metric = mkf_str(FO_STATUS, li_status_st[get_li_status(l)]);
William Dauchy655e14e2021-02-14 23:22:54 +01001952 break;
1953 case ST_F_PID:
Willy Tarreaue8422bf2021-06-15 09:08:18 +02001954 metric = mkf_u32(FO_KEY, 1);
William Dauchy655e14e2021-02-14 23:22:54 +01001955 break;
1956 case ST_F_IID:
1957 metric = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
1958 break;
1959 case ST_F_SID:
1960 metric = mkf_u32(FO_KEY|FS_SERVICE, l->luid);
1961 break;
1962 case ST_F_TYPE:
1963 metric = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_SO);
1964 break;
1965 case ST_F_WREW:
1966 metric = mkf_u64(FN_COUNTER, l->counters->failed_rewrites);
1967 break;
1968 case ST_F_EINT:
1969 metric = mkf_u64(FN_COUNTER, l->counters->internal_errors);
1970 break;
1971 case ST_F_ADDR:
1972 if (flags & STAT_SHLGNDS) {
1973 char str[INET6_ADDRSTRLEN];
1974 int port;
William Lallemand74c24fb2016-11-21 17:18:36 +01001975
William Dauchy655e14e2021-02-14 23:22:54 +01001976 port = get_host_port(&l->rx.addr);
1977 switch (addr_to_str(&l->rx.addr, str, sizeof(str))) {
1978 case AF_INET:
1979 metric = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
1980 chunk_appendf(out, "%s:%d", str, port);
1981 break;
1982 case AF_INET6:
1983 metric = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
1984 chunk_appendf(out, "[%s]:%d", str, port);
1985 break;
1986 case AF_UNIX:
1987 metric = mkf_str(FO_CONFIG|FS_SERVICE, "unix");
1988 break;
1989 case -1:
1990 metric = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
1991 chunk_strcat(out, strerror(errno));
1992 break;
1993 default: /* address family not supported */
1994 break;
1995 }
1996 }
1997 break;
1998 default:
1999 /* not used for listen. If a specific metric
2000 * is requested, return an error. Otherwise continue.
2001 */
2002 if (selected_field != NULL)
2003 return 0;
2004 continue;
William Lallemand74c24fb2016-11-21 17:18:36 +01002005 }
William Dauchy655e14e2021-02-14 23:22:54 +01002006 stats[current_field] = metric;
2007 if (selected_field != NULL)
2008 break;
William Lallemand74c24fb2016-11-21 17:18:36 +01002009 }
William Lallemand74c24fb2016-11-21 17:18:36 +01002010 return 1;
2011}
2012
2013/* Dumps a line for listener <l> and proxy <px> to the trash and uses the state
Willy Tarreaucaff6312022-05-27 10:17:46 +02002014 * from stream connector <sc>. The caller is responsible for clearing the trash
Willy Tarreau43241ff2019-10-09 11:27:51 +02002015 * if needed. Returns non-zero if it emits anything, zero otherwise.
William Lallemand74c24fb2016-11-21 17:18:36 +01002016 */
Willy Tarreaucaff6312022-05-27 10:17:46 +02002017static int stats_dump_li_stats(struct stconn *sc, struct proxy *px, struct listener *l)
William Lallemand74c24fb2016-11-21 17:18:36 +01002018{
Willy Tarreaucaff6312022-05-27 10:17:46 +02002019 struct appctx *appctx = __sc_appctx(sc);
Willy Tarreau91cefca2022-05-03 17:08:29 +02002020 struct show_stat_ctx *ctx = appctx->svcctx;
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02002021 struct field *stats = stat_l[STATS_DOMAIN_PROXY];
2022 struct stats_module *mod;
2023 size_t stats_count = ST_F_TOTAL_FIELDS;
2024
2025 memset(stats, 0, sizeof(struct field) * stat_count[STATS_DOMAIN_PROXY]);
William Lallemand74c24fb2016-11-21 17:18:36 +01002026
Willy Tarreau91cefca2022-05-03 17:08:29 +02002027 if (!stats_fill_li_stats(px, l, ctx->flags, stats,
William Dauchy655e14e2021-02-14 23:22:54 +01002028 ST_F_TOTAL_FIELDS, NULL))
William Lallemand74c24fb2016-11-21 17:18:36 +01002029 return 0;
2030
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02002031 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
2032 void *counters;
2033
2034 if (!(stats_px_get_cap(mod->domain_flags) & STATS_PX_CAP_LI)) {
2035 stats_count += mod->stats_count;
2036 continue;
2037 }
2038
2039 counters = EXTRA_COUNTERS_GET(l->extra_counters, mod);
2040 mod->fill_stats(counters, stats + stats_count);
2041 stats_count += mod->stats_count;
2042 }
2043
2044 return stats_dump_one_line(stats, stats_count, appctx);
William Lallemand74c24fb2016-11-21 17:18:36 +01002045}
2046
2047enum srv_stats_state {
2048 SRV_STATS_STATE_DOWN = 0,
2049 SRV_STATS_STATE_DOWN_AGENT,
2050 SRV_STATS_STATE_GOING_UP,
2051 SRV_STATS_STATE_UP_GOING_DOWN,
2052 SRV_STATS_STATE_UP,
2053 SRV_STATS_STATE_NOLB_GOING_DOWN,
2054 SRV_STATS_STATE_NOLB,
2055 SRV_STATS_STATE_DRAIN_GOING_DOWN,
2056 SRV_STATS_STATE_DRAIN,
2057 SRV_STATS_STATE_DRAIN_AGENT,
2058 SRV_STATS_STATE_NO_CHECK,
2059
2060 SRV_STATS_STATE_COUNT, /* Must be last */
2061};
2062
2063static const char *srv_hlt_st[SRV_STATS_STATE_COUNT] = {
2064 [SRV_STATS_STATE_DOWN] = "DOWN",
2065 [SRV_STATS_STATE_DOWN_AGENT] = "DOWN (agent)",
2066 [SRV_STATS_STATE_GOING_UP] = "DOWN %d/%d",
2067 [SRV_STATS_STATE_UP_GOING_DOWN] = "UP %d/%d",
2068 [SRV_STATS_STATE_UP] = "UP",
2069 [SRV_STATS_STATE_NOLB_GOING_DOWN] = "NOLB %d/%d",
2070 [SRV_STATS_STATE_NOLB] = "NOLB",
2071 [SRV_STATS_STATE_DRAIN_GOING_DOWN] = "DRAIN %d/%d",
2072 [SRV_STATS_STATE_DRAIN] = "DRAIN",
2073 [SRV_STATS_STATE_DRAIN_AGENT] = "DRAIN (agent)",
2074 [SRV_STATS_STATE_NO_CHECK] = "no check"
2075};
2076
William Dauchyd3a9a492021-01-25 17:29:03 +01002077/* Compute server state helper
William Lallemand74c24fb2016-11-21 17:18:36 +01002078 */
William Dauchyd3a9a492021-01-25 17:29:03 +01002079static void stats_fill_sv_stats_computestate(struct server *sv, struct server *ref,
2080 enum srv_stats_state *state)
William Lallemand74c24fb2016-11-21 17:18:36 +01002081{
Emeric Brun52a91d32017-08-31 14:41:55 +02002082 if (sv->cur_state == SRV_ST_RUNNING || sv->cur_state == SRV_ST_STARTING) {
William Lallemand74c24fb2016-11-21 17:18:36 +01002083 if ((ref->check.state & CHK_ST_ENABLED) &&
2084 (ref->check.health < ref->check.rise + ref->check.fall - 1)) {
William Dauchyd3a9a492021-01-25 17:29:03 +01002085 *state = SRV_STATS_STATE_UP_GOING_DOWN;
William Lallemand74c24fb2016-11-21 17:18:36 +01002086 } else {
William Dauchyd3a9a492021-01-25 17:29:03 +01002087 *state = SRV_STATS_STATE_UP;
William Lallemand74c24fb2016-11-21 17:18:36 +01002088 }
2089
Emeric Brun52a91d32017-08-31 14:41:55 +02002090 if (sv->cur_admin & SRV_ADMF_DRAIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +01002091 if (ref->agent.state & CHK_ST_ENABLED)
William Dauchyd3a9a492021-01-25 17:29:03 +01002092 *state = SRV_STATS_STATE_DRAIN_AGENT;
2093 else if (*state == SRV_STATS_STATE_UP_GOING_DOWN)
2094 *state = SRV_STATS_STATE_DRAIN_GOING_DOWN;
William Lallemand74c24fb2016-11-21 17:18:36 +01002095 else
William Dauchyd3a9a492021-01-25 17:29:03 +01002096 *state = SRV_STATS_STATE_DRAIN;
William Lallemand74c24fb2016-11-21 17:18:36 +01002097 }
2098
William Dauchyd3a9a492021-01-25 17:29:03 +01002099 if (*state == SRV_STATS_STATE_UP && !(ref->check.state & CHK_ST_ENABLED)) {
2100 *state = SRV_STATS_STATE_NO_CHECK;
William Lallemand74c24fb2016-11-21 17:18:36 +01002101 }
2102 }
Emeric Brun52a91d32017-08-31 14:41:55 +02002103 else if (sv->cur_state == SRV_ST_STOPPING) {
William Lallemand74c24fb2016-11-21 17:18:36 +01002104 if ((!(sv->check.state & CHK_ST_ENABLED) && !sv->track) ||
2105 (ref->check.health == ref->check.rise + ref->check.fall - 1)) {
William Dauchyd3a9a492021-01-25 17:29:03 +01002106 *state = SRV_STATS_STATE_NOLB;
William Lallemand74c24fb2016-11-21 17:18:36 +01002107 } else {
William Dauchyd3a9a492021-01-25 17:29:03 +01002108 *state = SRV_STATS_STATE_NOLB_GOING_DOWN;
William Lallemand74c24fb2016-11-21 17:18:36 +01002109 }
2110 }
2111 else { /* stopped */
2112 if ((ref->agent.state & CHK_ST_ENABLED) && !ref->agent.health) {
William Dauchyd3a9a492021-01-25 17:29:03 +01002113 *state = SRV_STATS_STATE_DOWN_AGENT;
William Lallemand74c24fb2016-11-21 17:18:36 +01002114 } else if ((ref->check.state & CHK_ST_ENABLED) && !ref->check.health) {
William Dauchyd3a9a492021-01-25 17:29:03 +01002115 *state = SRV_STATS_STATE_DOWN; /* DOWN */
William Lallemand74c24fb2016-11-21 17:18:36 +01002116 } else if ((ref->agent.state & CHK_ST_ENABLED) || (ref->check.state & CHK_ST_ENABLED)) {
William Dauchyd3a9a492021-01-25 17:29:03 +01002117 *state = SRV_STATS_STATE_GOING_UP;
William Lallemand74c24fb2016-11-21 17:18:36 +01002118 } else {
William Dauchyd3a9a492021-01-25 17:29:03 +01002119 *state = SRV_STATS_STATE_DOWN; /* DOWN, unchecked */
William Lallemand74c24fb2016-11-21 17:18:36 +01002120 }
2121 }
William Dauchyd3a9a492021-01-25 17:29:03 +01002122}
William Lallemand74c24fb2016-11-21 17:18:36 +01002123
William Dauchyd3a9a492021-01-25 17:29:03 +01002124/* Fill <stats> with the backend statistics. <stats> is preallocated array of
2125 * length <len>. If <selected_field> is != NULL, only fill this one. The length
2126 * of the array must be at least ST_F_TOTAL_FIELDS. If this length is less than
2127 * this value, or if the selected field is not implemented for servers, the
2128 * function returns 0, otherwise, it returns 1. <flags> can take the value
2129 * STAT_SHLGNDS.
2130 */
2131int stats_fill_sv_stats(struct proxy *px, struct server *sv, int flags,
2132 struct field *stats, int len,
2133 enum stat_field *selected_field)
2134{
2135 enum stat_field current_field = (selected_field != NULL ? *selected_field : 0);
2136 struct server *via = sv->track ? sv->track : sv;
2137 struct server *ref = via;
2138 enum srv_stats_state state = 0;
2139 char str[INET6_ADDRSTRLEN];
2140 struct buffer *out = get_trash_chunk();
2141 char *fld_status;
2142 long long srv_samples_counter;
2143 unsigned int srv_samples_window = TIME_STATS_SAMPLES;
William Lallemand74c24fb2016-11-21 17:18:36 +01002144
William Dauchyd3a9a492021-01-25 17:29:03 +01002145 if (len < ST_F_TOTAL_FIELDS)
2146 return 0;
William Lallemand74c24fb2016-11-21 17:18:36 +01002147
William Dauchyd3a9a492021-01-25 17:29:03 +01002148 chunk_reset(out);
William Lallemand74c24fb2016-11-21 17:18:36 +01002149
William Dauchyd3a9a492021-01-25 17:29:03 +01002150 /* compute state for later use */
2151 if (selected_field == NULL || *selected_field == ST_F_STATUS ||
2152 *selected_field == ST_F_CHECK_RISE || *selected_field == ST_F_CHECK_FALL ||
2153 *selected_field == ST_F_CHECK_HEALTH || *selected_field == ST_F_HANAFAIL) {
2154 /* we have "via" which is the tracked server as described in the configuration,
2155 * and "ref" which is the checked server and the end of the chain.
2156 */
2157 while (ref->track)
2158 ref = ref->track;
2159 stats_fill_sv_stats_computestate(sv, ref, &state);
William Lallemand74c24fb2016-11-21 17:18:36 +01002160 }
2161
William Dauchyd3a9a492021-01-25 17:29:03 +01002162 /* compue time values for later use */
2163 if (selected_field == NULL || *selected_field == ST_F_QTIME ||
2164 *selected_field == ST_F_CTIME || *selected_field == ST_F_RTIME ||
2165 *selected_field == ST_F_TTIME) {
2166 srv_samples_counter = (px->mode == PR_MODE_HTTP) ? sv->counters.p.http.cum_req : sv->counters.cum_lbconn;
2167 if (srv_samples_counter < TIME_STATS_SAMPLES && srv_samples_counter > 0)
2168 srv_samples_window = srv_samples_counter;
William Lallemand74c24fb2016-11-21 17:18:36 +01002169 }
2170
William Dauchyd3a9a492021-01-25 17:29:03 +01002171 for (; current_field < ST_F_TOTAL_FIELDS; current_field++) {
2172 struct field metric = { 0 };
William Lallemand74c24fb2016-11-21 17:18:36 +01002173
William Dauchyd3a9a492021-01-25 17:29:03 +01002174 switch (current_field) {
2175 case ST_F_PXNAME:
2176 metric = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
2177 break;
2178 case ST_F_SVNAME:
2179 metric = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, sv->id);
2180 break;
2181 case ST_F_MODE:
2182 metric = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
Christopher Faulet3888b8c2021-01-27 13:32:25 +01002183 break;
William Dauchyd3a9a492021-01-25 17:29:03 +01002184 case ST_F_QCUR:
Willy Tarreaua0570452021-06-18 09:30:30 +02002185 metric = mkf_u32(0, sv->queue.length);
William Dauchyd3a9a492021-01-25 17:29:03 +01002186 break;
2187 case ST_F_QMAX:
2188 metric = mkf_u32(FN_MAX, sv->counters.nbpend_max);
2189 break;
2190 case ST_F_SCUR:
2191 metric = mkf_u32(0, sv->cur_sess);
2192 break;
2193 case ST_F_SMAX:
2194 metric = mkf_u32(FN_MAX, sv->counters.cur_sess_max);
2195 break;
2196 case ST_F_SLIM:
2197 if (sv->maxconn)
2198 metric = mkf_u32(FO_CONFIG|FN_LIMIT, sv->maxconn);
2199 break;
2200 case ST_F_SRV_ICUR:
2201 metric = mkf_u32(0, sv->curr_idle_conns);
2202 break;
2203 case ST_F_SRV_ILIM:
2204 if (sv->max_idle_conns != -1)
2205 metric = mkf_u32(FO_CONFIG|FN_LIMIT, sv->max_idle_conns);
2206 break;
2207 case ST_F_STOT:
2208 metric = mkf_u64(FN_COUNTER, sv->counters.cum_sess);
2209 break;
2210 case ST_F_BIN:
2211 metric = mkf_u64(FN_COUNTER, sv->counters.bytes_in);
2212 break;
2213 case ST_F_BOUT:
2214 metric = mkf_u64(FN_COUNTER, sv->counters.bytes_out);
2215 break;
2216 case ST_F_DRESP:
2217 metric = mkf_u64(FN_COUNTER, sv->counters.denied_resp);
2218 break;
2219 case ST_F_ECON:
2220 metric = mkf_u64(FN_COUNTER, sv->counters.failed_conns);
2221 break;
2222 case ST_F_ERESP:
2223 metric = mkf_u64(FN_COUNTER, sv->counters.failed_resp);
2224 break;
2225 case ST_F_WRETR:
2226 metric = mkf_u64(FN_COUNTER, sv->counters.retries);
2227 break;
2228 case ST_F_WREDIS:
2229 metric = mkf_u64(FN_COUNTER, sv->counters.redispatches);
2230 break;
2231 case ST_F_WREW:
2232 metric = mkf_u64(FN_COUNTER, sv->counters.failed_rewrites);
2233 break;
2234 case ST_F_EINT:
2235 metric = mkf_u64(FN_COUNTER, sv->counters.internal_errors);
2236 break;
2237 case ST_F_CONNECT:
2238 metric = mkf_u64(FN_COUNTER, sv->counters.connect);
2239 break;
2240 case ST_F_REUSE:
2241 metric = mkf_u64(FN_COUNTER, sv->counters.reuse);
2242 break;
2243 case ST_F_IDLE_CONN_CUR:
2244 metric = mkf_u32(0, sv->curr_idle_nb);
2245 break;
2246 case ST_F_SAFE_CONN_CUR:
2247 metric = mkf_u32(0, sv->curr_safe_nb);
2248 break;
2249 case ST_F_USED_CONN_CUR:
2250 metric = mkf_u32(0, sv->curr_used_conns);
2251 break;
2252 case ST_F_NEED_CONN_EST:
2253 metric = mkf_u32(0, sv->est_need_conns);
2254 break;
2255 case ST_F_STATUS:
2256 fld_status = chunk_newstr(out);
2257 if (sv->cur_admin & SRV_ADMF_RMAINT)
2258 chunk_appendf(out, "MAINT (resolution)");
2259 else if (sv->cur_admin & SRV_ADMF_IMAINT)
2260 chunk_appendf(out, "MAINT (via %s/%s)", via->proxy->id, via->id);
2261 else if (sv->cur_admin & SRV_ADMF_MAINT)
2262 chunk_appendf(out, "MAINT");
2263 else
2264 chunk_appendf(out,
2265 srv_hlt_st[state],
2266 (ref->cur_state != SRV_ST_STOPPED) ? (ref->check.health - ref->check.rise + 1) : (ref->check.health),
2267 (ref->cur_state != SRV_ST_STOPPED) ? (ref->check.fall) : (ref->check.rise));
William Lallemand74c24fb2016-11-21 17:18:36 +01002268
William Dauchyd3a9a492021-01-25 17:29:03 +01002269 metric = mkf_str(FO_STATUS, fld_status);
2270 break;
2271 case ST_F_LASTCHG:
2272 metric = mkf_u32(FN_AGE, now.tv_sec - sv->last_change);
2273 break;
2274 case ST_F_WEIGHT:
2275 metric = mkf_u32(FN_AVG, (sv->cur_eweight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv);
2276 break;
2277 case ST_F_UWEIGHT:
2278 metric = mkf_u32(FN_AVG, sv->uweight);
2279 break;
2280 case ST_F_ACT:
2281 metric = mkf_u32(FO_STATUS, (sv->flags & SRV_F_BACKUP) ? 0 : 1);
2282 break;
2283 case ST_F_BCK:
2284 metric = mkf_u32(FO_STATUS, (sv->flags & SRV_F_BACKUP) ? 1 : 0);
2285 break;
2286 case ST_F_CHKFAIL:
2287 if (sv->check.state & CHK_ST_ENABLED)
2288 metric = mkf_u64(FN_COUNTER, sv->counters.failed_checks);
2289 break;
2290 case ST_F_CHKDOWN:
2291 if (sv->check.state & CHK_ST_ENABLED)
2292 metric = mkf_u64(FN_COUNTER, sv->counters.down_trans);
2293 break;
2294 case ST_F_DOWNTIME:
2295 if (sv->check.state & CHK_ST_ENABLED)
2296 metric = mkf_u32(FN_COUNTER, srv_downtime(sv));
2297 break;
2298 case ST_F_QLIMIT:
2299 if (sv->maxqueue)
2300 metric = mkf_u32(FO_CONFIG|FS_SERVICE, sv->maxqueue);
2301 break;
2302 case ST_F_PID:
Willy Tarreaue8422bf2021-06-15 09:08:18 +02002303 metric = mkf_u32(FO_KEY, 1);
William Dauchyd3a9a492021-01-25 17:29:03 +01002304 break;
2305 case ST_F_IID:
2306 metric = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
2307 break;
2308 case ST_F_SID:
2309 metric = mkf_u32(FO_KEY|FS_SERVICE, sv->puid);
2310 break;
Aurelien DARRAGON745ce8e2022-11-17 16:34:07 +01002311 case ST_F_SRID:
2312 metric = mkf_u32(FN_COUNTER, sv->rid);
2313 break;
William Dauchyd3a9a492021-01-25 17:29:03 +01002314 case ST_F_THROTTLE:
2315 if (sv->cur_state == SRV_ST_STARTING && !server_is_draining(sv))
2316 metric = mkf_u32(FN_AVG, server_throttle_rate(sv));
2317 break;
2318 case ST_F_LBTOT:
2319 metric = mkf_u64(FN_COUNTER, sv->counters.cum_lbconn);
2320 break;
2321 case ST_F_TRACKED:
2322 if (sv->track) {
2323 char *fld_track = chunk_newstr(out);
2324 chunk_appendf(out, "%s/%s", sv->track->proxy->id, sv->track->id);
2325 metric = mkf_str(FO_CONFIG|FN_NAME|FS_SERVICE, fld_track);
2326 }
2327 break;
2328 case ST_F_TYPE:
2329 metric = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_SV);
2330 break;
2331 case ST_F_RATE:
2332 metric = mkf_u32(FN_RATE, read_freq_ctr(&sv->sess_per_sec));
2333 break;
2334 case ST_F_RATE_MAX:
2335 metric = mkf_u32(FN_MAX, sv->counters.sps_max);
2336 break;
2337 case ST_F_CHECK_STATUS:
2338 if ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED) {
2339 const char *fld_chksts;
Marcin Deraneka8dbdf32020-05-15 20:02:40 +02002340
William Dauchyd3a9a492021-01-25 17:29:03 +01002341 fld_chksts = chunk_newstr(out);
2342 chunk_strcat(out, "* "); // for check in progress
2343 chunk_strcat(out, get_check_status_info(sv->check.status));
2344 if (!(sv->check.state & CHK_ST_INPROGRESS))
2345 fld_chksts += 2; // skip "* "
Willy Tarreau973a9372021-05-12 17:29:14 +02002346 metric = mkf_str(FN_OUTPUT, fld_chksts);
William Dauchyd3a9a492021-01-25 17:29:03 +01002347 }
2348 break;
2349 case ST_F_CHECK_CODE:
2350 if ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED &&
2351 sv->check.status >= HCHK_STATUS_L57DATA)
2352 metric = mkf_u32(FN_OUTPUT, sv->check.code);
2353 break;
2354 case ST_F_CHECK_DURATION:
2355 if ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED &&
2356 sv->check.status >= HCHK_STATUS_CHECKED)
Aurelien DARRAGONb118f2f2022-12-07 14:52:10 +01002357 metric = mkf_u64(FN_DURATION, MAX(sv->check.duration, 0));
William Dauchyd3a9a492021-01-25 17:29:03 +01002358 break;
2359 case ST_F_CHECK_DESC:
2360 if ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED)
2361 metric = mkf_str(FN_OUTPUT, get_check_status_description(sv->check.status));
2362 break;
2363 case ST_F_LAST_CHK:
2364 if ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED)
2365 metric = mkf_str(FN_OUTPUT, sv->check.desc);
2366 break;
2367 case ST_F_CHECK_RISE:
2368 if ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED)
2369 metric = mkf_u32(FO_CONFIG|FS_SERVICE, ref->check.rise);
2370 break;
2371 case ST_F_CHECK_FALL:
2372 if ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED)
2373 metric = mkf_u32(FO_CONFIG|FS_SERVICE, ref->check.fall);
2374 break;
2375 case ST_F_CHECK_HEALTH:
2376 if ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED)
2377 metric = mkf_u32(FO_CONFIG|FS_SERVICE, ref->check.health);
2378 break;
Christopher Faulet59bab612021-07-22 08:04:38 +02002379 case ST_F_AGENT_STATUS:
2380 if ((sv->agent.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED) {
2381 const char *fld_chksts;
2382
2383 fld_chksts = chunk_newstr(out);
2384 chunk_strcat(out, "* "); // for check in progress
2385 chunk_strcat(out, get_check_status_info(sv->agent.status));
2386 if (!(sv->agent.state & CHK_ST_INPROGRESS))
2387 fld_chksts += 2; // skip "* "
2388 metric = mkf_str(FN_OUTPUT, fld_chksts);
2389 }
2390 break;
2391 case ST_F_AGENT_CODE:
2392 if ((sv->agent.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED &&
2393 (sv->agent.status >= HCHK_STATUS_L57DATA))
2394 metric = mkf_u32(FN_OUTPUT, sv->agent.code);
2395 break;
2396 case ST_F_AGENT_DURATION:
2397 if ((sv->agent.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED)
2398 metric = mkf_u64(FN_DURATION, sv->agent.duration);
2399 break;
2400 case ST_F_AGENT_DESC:
2401 if ((sv->agent.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED)
2402 metric = mkf_str(FN_OUTPUT, get_check_status_description(sv->agent.status));
2403 break;
2404 case ST_F_LAST_AGT:
2405 if ((sv->agent.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED)
2406 metric = mkf_str(FN_OUTPUT, sv->agent.desc);
2407 break;
2408 case ST_F_AGENT_RISE:
2409 if ((sv->agent.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED)
2410 metric = mkf_u32(FO_CONFIG|FS_SERVICE, sv->agent.rise);
2411 break;
2412 case ST_F_AGENT_FALL:
2413 if ((sv->agent.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED)
2414 metric = mkf_u32(FO_CONFIG|FS_SERVICE, sv->agent.fall);
2415 break;
2416 case ST_F_AGENT_HEALTH:
2417 if ((sv->agent.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED)
2418 metric = mkf_u32(FO_CONFIG|FS_SERVICE, sv->agent.health);
2419 break;
William Dauchyd3a9a492021-01-25 17:29:03 +01002420 case ST_F_REQ_TOT:
2421 if (px->mode == PR_MODE_HTTP)
2422 metric = mkf_u64(FN_COUNTER, sv->counters.p.http.cum_req);
2423 break;
2424 case ST_F_HRSP_1XX:
2425 if (px->mode == PR_MODE_HTTP)
2426 metric = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[1]);
2427 break;
2428 case ST_F_HRSP_2XX:
2429 if (px->mode == PR_MODE_HTTP)
2430 metric = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[2]);
2431 break;
2432 case ST_F_HRSP_3XX:
2433 if (px->mode == PR_MODE_HTTP)
2434 metric = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[3]);
2435 break;
2436 case ST_F_HRSP_4XX:
2437 if (px->mode == PR_MODE_HTTP)
2438 metric = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[4]);
2439 break;
2440 case ST_F_HRSP_5XX:
2441 if (px->mode == PR_MODE_HTTP)
2442 metric = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[5]);
2443 break;
2444 case ST_F_HRSP_OTHER:
2445 if (px->mode == PR_MODE_HTTP)
2446 metric = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[0]);
2447 break;
2448 case ST_F_HANAFAIL:
2449 if (ref->observe)
2450 metric = mkf_u64(FN_COUNTER, sv->counters.failed_hana);
2451 break;
2452 case ST_F_CLI_ABRT:
2453 metric = mkf_u64(FN_COUNTER, sv->counters.cli_aborts);
2454 break;
2455 case ST_F_SRV_ABRT:
2456 metric = mkf_u64(FN_COUNTER, sv->counters.srv_aborts);
2457 break;
2458 case ST_F_LASTSESS:
2459 metric = mkf_s32(FN_AGE, srv_lastsession(sv));
2460 break;
2461 case ST_F_QTIME:
2462 metric = mkf_u32(FN_AVG, swrate_avg(sv->counters.q_time, srv_samples_window));
2463 break;
2464 case ST_F_CTIME:
2465 metric = mkf_u32(FN_AVG, swrate_avg(sv->counters.c_time, srv_samples_window));
2466 break;
2467 case ST_F_RTIME:
2468 metric = mkf_u32(FN_AVG, swrate_avg(sv->counters.d_time, srv_samples_window));
2469 break;
2470 case ST_F_TTIME:
2471 metric = mkf_u32(FN_AVG, swrate_avg(sv->counters.t_time, srv_samples_window));
2472 break;
2473 case ST_F_QT_MAX:
2474 metric = mkf_u32(FN_MAX, sv->counters.qtime_max);
2475 break;
2476 case ST_F_CT_MAX:
2477 metric = mkf_u32(FN_MAX, sv->counters.ctime_max);
2478 break;
2479 case ST_F_RT_MAX:
2480 metric = mkf_u32(FN_MAX, sv->counters.dtime_max);
2481 break;
2482 case ST_F_TT_MAX:
2483 metric = mkf_u32(FN_MAX, sv->counters.ttime_max);
2484 break;
2485 case ST_F_ADDR:
2486 if (flags & STAT_SHLGNDS) {
2487 switch (addr_to_str(&sv->addr, str, sizeof(str))) {
2488 case AF_INET:
2489 metric = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
2490 chunk_appendf(out, "%s:%d", str, sv->svc_port);
2491 break;
2492 case AF_INET6:
2493 metric = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
2494 chunk_appendf(out, "[%s]:%d", str, sv->svc_port);
2495 break;
2496 case AF_UNIX:
2497 metric = mkf_str(FO_CONFIG|FS_SERVICE, "unix");
2498 break;
2499 case -1:
2500 metric = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
2501 chunk_strcat(out, strerror(errno));
2502 break;
2503 default: /* address family not supported */
2504 break;
2505 }
2506 }
2507 break;
2508 case ST_F_COOKIE:
2509 if (flags & STAT_SHLGNDS && sv->cookie)
2510 metric = mkf_str(FO_CONFIG|FN_NAME|FS_SERVICE, sv->cookie);
2511 break;
2512 default:
2513 /* not used for servers. If a specific metric
2514 * is requested, return an error. Otherwise continue.
2515 */
2516 if (selected_field != NULL)
2517 return 0;
2518 continue;
William Lallemand74c24fb2016-11-21 17:18:36 +01002519 }
William Dauchyd3a9a492021-01-25 17:29:03 +01002520 stats[current_field] = metric;
2521 if (selected_field != NULL)
2522 break;
William Lallemand74c24fb2016-11-21 17:18:36 +01002523 }
William Lallemand74c24fb2016-11-21 17:18:36 +01002524 return 1;
2525}
2526
2527/* Dumps a line for server <sv> and proxy <px> to the trash and uses the state
Willy Tarreaucaff6312022-05-27 10:17:46 +02002528 * from stream connector <sc>, and server state <state>. The caller is
Willy Tarreau43241ff2019-10-09 11:27:51 +02002529 * responsible for clearing the trash if needed. Returns non-zero if it emits
2530 * anything, zero otherwise.
William Lallemand74c24fb2016-11-21 17:18:36 +01002531 */
Willy Tarreaucaff6312022-05-27 10:17:46 +02002532static int stats_dump_sv_stats(struct stconn *sc, struct proxy *px, struct server *sv)
William Lallemand74c24fb2016-11-21 17:18:36 +01002533{
Willy Tarreaucaff6312022-05-27 10:17:46 +02002534 struct appctx *appctx = __sc_appctx(sc);
Willy Tarreau91cefca2022-05-03 17:08:29 +02002535 struct show_stat_ctx *ctx = appctx->svcctx;
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02002536 struct stats_module *mod;
2537 struct field *stats = stat_l[STATS_DOMAIN_PROXY];
2538 size_t stats_count = ST_F_TOTAL_FIELDS;
2539
2540 memset(stats, 0, sizeof(struct field) * stat_count[STATS_DOMAIN_PROXY]);
William Lallemand74c24fb2016-11-21 17:18:36 +01002541
Willy Tarreau91cefca2022-05-03 17:08:29 +02002542 if (!stats_fill_sv_stats(px, sv, ctx->flags, stats,
William Dauchyd3a9a492021-01-25 17:29:03 +01002543 ST_F_TOTAL_FIELDS, NULL))
William Lallemand74c24fb2016-11-21 17:18:36 +01002544 return 0;
2545
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02002546 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
2547 void *counters;
2548
2549 if (stats_get_domain(mod->domain_flags) != STATS_DOMAIN_PROXY)
2550 continue;
2551
2552 if (!(stats_px_get_cap(mod->domain_flags) & STATS_PX_CAP_SRV)) {
2553 stats_count += mod->stats_count;
2554 continue;
2555 }
2556
2557 counters = EXTRA_COUNTERS_GET(sv->extra_counters, mod);
2558 mod->fill_stats(counters, stats + stats_count);
2559 stats_count += mod->stats_count;
2560 }
2561
2562 return stats_dump_one_line(stats, stats_count, appctx);
William Lallemand74c24fb2016-11-21 17:18:36 +01002563}
2564
William Dauchyda3b4662021-01-25 17:29:01 +01002565/* Helper to compute srv values for a given backend
William Lallemand74c24fb2016-11-21 17:18:36 +01002566 */
William Dauchyda3b4662021-01-25 17:29:01 +01002567static void stats_fill_be_stats_computesrv(struct proxy *px, int *nbup, int *nbsrv, int *totuw)
William Lallemand74c24fb2016-11-21 17:18:36 +01002568{
William Dauchyda3b4662021-01-25 17:29:01 +01002569 int nbup_tmp, nbsrv_tmp, totuw_tmp;
Willy Tarreau2fbe6942020-10-23 18:02:54 +02002570 const struct server *srv;
William Lallemand74c24fb2016-11-21 17:18:36 +01002571
William Dauchyda3b4662021-01-25 17:29:01 +01002572 nbup_tmp = nbsrv_tmp = totuw_tmp = 0;
Willy Tarreaubd715102020-10-23 22:44:30 +02002573 for (srv = px->srv; srv; srv = srv->next) {
2574 if (srv->cur_state != SRV_ST_STOPPED) {
William Dauchyda3b4662021-01-25 17:29:01 +01002575 nbup_tmp++;
Willy Tarreaubd715102020-10-23 22:44:30 +02002576 if (srv_currently_usable(srv) &&
2577 (!px->srv_act ^ !(srv->flags & SRV_F_BACKUP)))
William Dauchyda3b4662021-01-25 17:29:01 +01002578 totuw_tmp += srv->uweight;
Willy Tarreau2fbe6942020-10-23 18:02:54 +02002579 }
William Dauchyda3b4662021-01-25 17:29:01 +01002580 nbsrv_tmp++;
Willy Tarreau2fbe6942020-10-23 18:02:54 +02002581 }
2582
Willy Tarreaubd715102020-10-23 22:44:30 +02002583 HA_RWLOCK_RDLOCK(LBPRM_LOCK, &px->lbprm.lock);
2584 if (!px->srv_act && px->lbprm.fbck)
William Dauchyda3b4662021-01-25 17:29:01 +01002585 totuw_tmp = px->lbprm.fbck->uweight;
Willy Tarreaubd715102020-10-23 22:44:30 +02002586 HA_RWLOCK_RDUNLOCK(LBPRM_LOCK, &px->lbprm.lock);
2587
William Dauchyda3b4662021-01-25 17:29:01 +01002588 /* use tmp variable then assign result to make gcc happy */
2589 *nbup = nbup_tmp;
2590 *nbsrv = nbsrv_tmp;
2591 *totuw = totuw_tmp;
2592}
Willy Tarreau2fbe6942020-10-23 18:02:54 +02002593
William Dauchyda3b4662021-01-25 17:29:01 +01002594/* Fill <stats> with the backend statistics. <stats> is preallocated array of
2595 * length <len>. If <selected_field> is != NULL, only fill this one. The length
2596 * of the array must be at least ST_F_TOTAL_FIELDS. If this length is less than
2597 * this value, or if the selected field is not implemented for backends, the
2598 * function returns 0, otherwise, it returns 1. <flags> can take the value
2599 * STAT_SHLGNDS.
2600 */
2601int stats_fill_be_stats(struct proxy *px, int flags, struct field *stats, int len,
2602 enum stat_field *selected_field)
2603{
2604 enum stat_field current_field = (selected_field != NULL ? *selected_field : 0);
2605 long long be_samples_counter;
2606 unsigned int be_samples_window = TIME_STATS_SAMPLES;
2607 struct buffer *out = get_trash_chunk();
2608 int nbup, nbsrv, totuw;
2609 char *fld;
William Lallemand74c24fb2016-11-21 17:18:36 +01002610
William Dauchyda3b4662021-01-25 17:29:01 +01002611 if (len < ST_F_TOTAL_FIELDS)
2612 return 0;
William Lallemand74c24fb2016-11-21 17:18:36 +01002613
William Dauchyda3b4662021-01-25 17:29:01 +01002614 nbup = nbsrv = totuw = 0;
2615 /* some srv values compute for later if we either select all fields or
2616 * need them for one of the mentioned ones */
2617 if (selected_field == NULL || *selected_field == ST_F_STATUS ||
2618 *selected_field == ST_F_UWEIGHT)
2619 stats_fill_be_stats_computesrv(px, &nbup, &nbsrv, &totuw);
William Lallemand74c24fb2016-11-21 17:18:36 +01002620
William Dauchyda3b4662021-01-25 17:29:01 +01002621 /* same here but specific to time fields */
2622 if (selected_field == NULL || *selected_field == ST_F_QTIME ||
2623 *selected_field == ST_F_CTIME || *selected_field == ST_F_RTIME ||
2624 *selected_field == ST_F_TTIME) {
2625 be_samples_counter = (px->mode == PR_MODE_HTTP) ? px->be_counters.p.http.cum_req : px->be_counters.cum_lbconn;
2626 if (be_samples_counter < TIME_STATS_SAMPLES && be_samples_counter > 0)
2627 be_samples_window = be_samples_counter;
William Lallemand74c24fb2016-11-21 17:18:36 +01002628 }
2629
William Dauchyda3b4662021-01-25 17:29:01 +01002630 for (; current_field < ST_F_TOTAL_FIELDS; current_field++) {
2631 struct field metric = { 0 };
William Lallemand74c24fb2016-11-21 17:18:36 +01002632
William Dauchyda3b4662021-01-25 17:29:01 +01002633 switch (current_field) {
2634 case ST_F_PXNAME:
2635 metric = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
2636 break;
2637 case ST_F_SVNAME:
2638 metric = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, "BACKEND");
2639 break;
2640 case ST_F_MODE:
2641 metric = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
2642 break;
2643 case ST_F_QCUR:
Willy Tarreau7f3c1df2021-06-18 09:22:21 +02002644 metric = mkf_u32(0, px->queue.length);
William Dauchyda3b4662021-01-25 17:29:01 +01002645 break;
2646 case ST_F_QMAX:
2647 metric = mkf_u32(FN_MAX, px->be_counters.nbpend_max);
2648 break;
2649 case ST_F_SCUR:
2650 metric = mkf_u32(0, px->beconn);
2651 break;
2652 case ST_F_SMAX:
2653 metric = mkf_u32(FN_MAX, px->be_counters.conn_max);
2654 break;
2655 case ST_F_SLIM:
2656 metric = mkf_u32(FO_CONFIG|FN_LIMIT, px->fullconn);
2657 break;
2658 case ST_F_STOT:
2659 metric = mkf_u64(FN_COUNTER, px->be_counters.cum_conn);
2660 break;
2661 case ST_F_BIN:
2662 metric = mkf_u64(FN_COUNTER, px->be_counters.bytes_in);
2663 break;
2664 case ST_F_BOUT:
2665 metric = mkf_u64(FN_COUNTER, px->be_counters.bytes_out);
2666 break;
2667 case ST_F_DREQ:
2668 metric = mkf_u64(FN_COUNTER, px->be_counters.denied_req);
2669 break;
2670 case ST_F_DRESP:
2671 metric = mkf_u64(FN_COUNTER, px->be_counters.denied_resp);
2672 break;
2673 case ST_F_ECON:
2674 metric = mkf_u64(FN_COUNTER, px->be_counters.failed_conns);
2675 break;
2676 case ST_F_ERESP:
2677 metric = mkf_u64(FN_COUNTER, px->be_counters.failed_resp);
2678 break;
2679 case ST_F_WRETR:
2680 metric = mkf_u64(FN_COUNTER, px->be_counters.retries);
2681 break;
2682 case ST_F_WREDIS:
2683 metric = mkf_u64(FN_COUNTER, px->be_counters.redispatches);
2684 break;
2685 case ST_F_WREW:
2686 metric = mkf_u64(FN_COUNTER, px->be_counters.failed_rewrites);
2687 break;
2688 case ST_F_EINT:
2689 metric = mkf_u64(FN_COUNTER, px->be_counters.internal_errors);
2690 break;
2691 case ST_F_CONNECT:
2692 metric = mkf_u64(FN_COUNTER, px->be_counters.connect);
2693 break;
2694 case ST_F_REUSE:
2695 metric = mkf_u64(FN_COUNTER, px->be_counters.reuse);
2696 break;
2697 case ST_F_STATUS:
2698 fld = chunk_newstr(out);
2699 chunk_appendf(out, "%s", (px->lbprm.tot_weight > 0 || !px->srv) ? "UP" : "DOWN");
2700 if (flags & (STAT_HIDE_MAINT|STAT_HIDE_DOWN))
2701 chunk_appendf(out, " (%d/%d)", nbup, nbsrv);
2702 metric = mkf_str(FO_STATUS, fld);
2703 break;
Cedric Paillet7d6644e2022-12-08 09:17:00 +00002704 case ST_F_AGG_SRV_CHECK_STATUS: // DEPRECATED
2705 case ST_F_AGG_SRV_STATUS:
William Dauchy42d7c402021-11-07 10:18:47 +01002706 metric = mkf_u32(FN_GAUGE, 0);
2707 break;
Cedric Paillete06e31e2022-12-08 09:17:01 +00002708 case ST_F_AGG_CHECK_STATUS:
2709 metric = mkf_u32(FN_GAUGE, 0);
2710 break;
William Dauchyda3b4662021-01-25 17:29:01 +01002711 case ST_F_WEIGHT:
2712 metric = mkf_u32(FN_AVG, (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv);
2713 break;
2714 case ST_F_UWEIGHT:
2715 metric = mkf_u32(FN_AVG, totuw);
2716 break;
2717 case ST_F_ACT:
2718 metric = mkf_u32(0, px->srv_act);
2719 break;
2720 case ST_F_BCK:
2721 metric = mkf_u32(0, px->srv_bck);
2722 break;
2723 case ST_F_CHKDOWN:
2724 metric = mkf_u64(FN_COUNTER, px->down_trans);
2725 break;
2726 case ST_F_LASTCHG:
2727 metric = mkf_u32(FN_AGE, now.tv_sec - px->last_change);
2728 break;
2729 case ST_F_DOWNTIME:
2730 if (px->srv)
2731 metric = mkf_u32(FN_COUNTER, be_downtime(px));
2732 break;
2733 case ST_F_PID:
Willy Tarreaue8422bf2021-06-15 09:08:18 +02002734 metric = mkf_u32(FO_KEY, 1);
William Dauchyda3b4662021-01-25 17:29:01 +01002735 break;
2736 case ST_F_IID:
2737 metric = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
2738 break;
2739 case ST_F_SID:
2740 metric = mkf_u32(FO_KEY|FS_SERVICE, 0);
2741 break;
2742 case ST_F_LBTOT:
2743 metric = mkf_u64(FN_COUNTER, px->be_counters.cum_lbconn);
2744 break;
2745 case ST_F_TYPE:
2746 metric = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_BE);
2747 break;
2748 case ST_F_RATE:
2749 metric = mkf_u32(0, read_freq_ctr(&px->be_sess_per_sec));
2750 break;
2751 case ST_F_RATE_MAX:
2752 metric = mkf_u32(0, px->be_counters.sps_max);
2753 break;
2754 case ST_F_COOKIE:
2755 if (flags & STAT_SHLGNDS && px->cookie_name)
2756 metric = mkf_str(FO_CONFIG|FN_NAME|FS_SERVICE, px->cookie_name);
2757 break;
2758 case ST_F_ALGO:
2759 if (flags & STAT_SHLGNDS)
2760 metric = mkf_str(FO_CONFIG|FS_SERVICE, backend_lb_algo_str(px->lbprm.algo & BE_LB_ALGO));
2761 break;
2762 case ST_F_REQ_TOT:
2763 if (px->mode == PR_MODE_HTTP)
2764 metric = mkf_u64(FN_COUNTER, px->be_counters.p.http.cum_req);
2765 break;
2766 case ST_F_HRSP_1XX:
2767 if (px->mode == PR_MODE_HTTP)
2768 metric = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[1]);
2769 break;
2770 case ST_F_HRSP_2XX:
2771 if (px->mode == PR_MODE_HTTP)
2772 metric = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[2]);
2773 break;
2774 case ST_F_HRSP_3XX:
2775 if (px->mode == PR_MODE_HTTP)
2776 metric = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[3]);
2777 break;
2778 case ST_F_HRSP_4XX:
2779 if (px->mode == PR_MODE_HTTP)
2780 metric = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[4]);
2781 break;
2782 case ST_F_HRSP_5XX:
2783 if (px->mode == PR_MODE_HTTP)
2784 metric = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[5]);
2785 break;
2786 case ST_F_HRSP_OTHER:
2787 if (px->mode == PR_MODE_HTTP)
2788 metric = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[0]);
2789 break;
2790 case ST_F_CACHE_LOOKUPS:
2791 if (px->mode == PR_MODE_HTTP)
2792 metric = mkf_u64(FN_COUNTER, px->be_counters.p.http.cache_lookups);
2793 break;
2794 case ST_F_CACHE_HITS:
2795 if (px->mode == PR_MODE_HTTP)
2796 metric = mkf_u64(FN_COUNTER, px->be_counters.p.http.cache_hits);
2797 break;
2798 case ST_F_CLI_ABRT:
2799 metric = mkf_u64(FN_COUNTER, px->be_counters.cli_aborts);
2800 break;
2801 case ST_F_SRV_ABRT:
2802 metric = mkf_u64(FN_COUNTER, px->be_counters.srv_aborts);
2803 break;
2804 case ST_F_COMP_IN:
2805 metric = mkf_u64(FN_COUNTER, px->be_counters.comp_in);
2806 break;
2807 case ST_F_COMP_OUT:
2808 metric = mkf_u64(FN_COUNTER, px->be_counters.comp_out);
2809 break;
2810 case ST_F_COMP_BYP:
2811 metric = mkf_u64(FN_COUNTER, px->be_counters.comp_byp);
2812 break;
2813 case ST_F_COMP_RSP:
2814 metric = mkf_u64(FN_COUNTER, px->be_counters.p.http.comp_rsp);
2815 break;
2816 case ST_F_LASTSESS:
2817 metric = mkf_s32(FN_AGE, be_lastsession(px));
2818 break;
2819 case ST_F_QTIME:
2820 metric = mkf_u32(FN_AVG, swrate_avg(px->be_counters.q_time, be_samples_window));
2821 break;
2822 case ST_F_CTIME:
2823 metric = mkf_u32(FN_AVG, swrate_avg(px->be_counters.c_time, be_samples_window));
2824 break;
2825 case ST_F_RTIME:
2826 metric = mkf_u32(FN_AVG, swrate_avg(px->be_counters.d_time, be_samples_window));
2827 break;
2828 case ST_F_TTIME:
2829 metric = mkf_u32(FN_AVG, swrate_avg(px->be_counters.t_time, be_samples_window));
2830 break;
2831 case ST_F_QT_MAX:
2832 metric = mkf_u32(FN_MAX, px->be_counters.qtime_max);
2833 break;
2834 case ST_F_CT_MAX:
2835 metric = mkf_u32(FN_MAX, px->be_counters.ctime_max);
2836 break;
2837 case ST_F_RT_MAX:
2838 metric = mkf_u32(FN_MAX, px->be_counters.dtime_max);
2839 break;
2840 case ST_F_TT_MAX:
2841 metric = mkf_u32(FN_MAX, px->be_counters.ttime_max);
2842 break;
2843 default:
2844 /* not used for backends. If a specific metric
2845 * is requested, return an error. Otherwise continue.
2846 */
2847 if (selected_field != NULL)
2848 return 0;
2849 continue;
2850 }
2851 stats[current_field] = metric;
2852 if (selected_field != NULL)
2853 break;
2854 }
William Lallemand74c24fb2016-11-21 17:18:36 +01002855 return 1;
2856}
2857
2858/* Dumps a line for backend <px> to the trash for and uses the state from stream
Willy Tarreau43241ff2019-10-09 11:27:51 +02002859 * interface <si>. The caller is responsible for clearing the trash if needed.
2860 * Returns non-zero if it emits anything, zero otherwise.
William Lallemand74c24fb2016-11-21 17:18:36 +01002861 */
Willy Tarreaucaff6312022-05-27 10:17:46 +02002862static int stats_dump_be_stats(struct stconn *sc, struct proxy *px)
William Lallemand74c24fb2016-11-21 17:18:36 +01002863{
Willy Tarreaucaff6312022-05-27 10:17:46 +02002864 struct appctx *appctx = __sc_appctx(sc);
Willy Tarreau91cefca2022-05-03 17:08:29 +02002865 struct show_stat_ctx *ctx = appctx->svcctx;
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02002866 struct field *stats = stat_l[STATS_DOMAIN_PROXY];
2867 struct stats_module *mod;
2868 size_t stats_count = ST_F_TOTAL_FIELDS;
William Lallemand74c24fb2016-11-21 17:18:36 +01002869
2870 if (!(px->cap & PR_CAP_BE))
2871 return 0;
2872
Willy Tarreau91cefca2022-05-03 17:08:29 +02002873 if ((ctx->flags & STAT_BOUND) && !(ctx->type & (1 << STATS_TYPE_BE)))
William Lallemand74c24fb2016-11-21 17:18:36 +01002874 return 0;
2875
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02002876 memset(stats, 0, sizeof(struct field) * stat_count[STATS_DOMAIN_PROXY]);
2877
Willy Tarreau91cefca2022-05-03 17:08:29 +02002878 if (!stats_fill_be_stats(px, ctx->flags, stats, ST_F_TOTAL_FIELDS, NULL))
William Lallemand74c24fb2016-11-21 17:18:36 +01002879 return 0;
2880
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02002881 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
2882 struct extra_counters *counters;
2883
2884 if (stats_get_domain(mod->domain_flags) != STATS_DOMAIN_PROXY)
2885 continue;
2886
2887 if (!(stats_px_get_cap(mod->domain_flags) & STATS_PX_CAP_BE)) {
2888 stats_count += mod->stats_count;
2889 continue;
2890 }
2891
2892 counters = EXTRA_COUNTERS_GET(px->extra_counters_be, mod);
2893 mod->fill_stats(counters, stats + stats_count);
2894 stats_count += mod->stats_count;
2895 }
2896
2897 return stats_dump_one_line(stats, stats_count, appctx);
William Lallemand74c24fb2016-11-21 17:18:36 +01002898}
2899
2900/* Dumps the HTML table header for proxy <px> to the trash for and uses the state from
Willy Tarreaucaff6312022-05-27 10:17:46 +02002901 * stream connector <sc> and per-uri parameters <uri>. The caller is responsible
William Lallemand74c24fb2016-11-21 17:18:36 +01002902 * for clearing the trash if needed.
2903 */
Willy Tarreaucaff6312022-05-27 10:17:46 +02002904static void stats_dump_html_px_hdr(struct stconn *sc, struct proxy *px)
William Lallemand74c24fb2016-11-21 17:18:36 +01002905{
Willy Tarreaucaff6312022-05-27 10:17:46 +02002906 struct appctx *appctx = __sc_appctx(sc);
Willy Tarreau91cefca2022-05-03 17:08:29 +02002907 struct show_stat_ctx *ctx = appctx->svcctx;
William Lallemand74c24fb2016-11-21 17:18:36 +01002908 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02002909 struct stats_module *mod;
2910 int stats_module_len = 0;
William Lallemand74c24fb2016-11-21 17:18:36 +01002911
Willy Tarreau91cefca2022-05-03 17:08:29 +02002912 if (px->cap & PR_CAP_BE && px->srv && (ctx->flags & STAT_ADMIN)) {
William Lallemand74c24fb2016-11-21 17:18:36 +01002913 /* A form to enable/disable this proxy servers */
2914
Willy Tarreau91cefca2022-05-03 17:08:29 +02002915 /* scope_txt = search pattern + search query, ctx->scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
William Lallemand74c24fb2016-11-21 17:18:36 +01002916 scope_txt[0] = 0;
Willy Tarreau91cefca2022-05-03 17:08:29 +02002917 if (ctx->scope_len) {
Willy Tarreaucaff6312022-05-27 10:17:46 +02002918 const char *scope_ptr = stats_scope_ptr(appctx, sc);
Christopher Fauleted7a0662019-01-14 11:07:34 +01002919
William Lallemand74c24fb2016-11-21 17:18:36 +01002920 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Willy Tarreau91cefca2022-05-03 17:08:29 +02002921 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), scope_ptr, ctx->scope_len);
2922 scope_txt[strlen(STAT_SCOPE_PATTERN) + ctx->scope_len] = 0;
William Lallemand74c24fb2016-11-21 17:18:36 +01002923 }
2924
2925 chunk_appendf(&trash,
2926 "<form method=\"post\">");
2927 }
2928
2929 /* print a new table */
2930 chunk_appendf(&trash,
2931 "<table class=\"tbl\" width=\"100%%\">\n"
2932 "<tr class=\"titre\">"
2933 "<th class=\"pxname\" width=\"10%%\">");
2934
2935 chunk_appendf(&trash,
2936 "<a name=\"%s\"></a>%s"
2937 "<a class=px href=\"#%s\">%s</a>",
2938 px->id,
Willy Tarreau91cefca2022-05-03 17:08:29 +02002939 (ctx->flags & STAT_SHLGNDS) ? "<u>":"",
William Lallemand74c24fb2016-11-21 17:18:36 +01002940 px->id, px->id);
2941
Willy Tarreau91cefca2022-05-03 17:08:29 +02002942 if (ctx->flags & STAT_SHLGNDS) {
William Lallemand74c24fb2016-11-21 17:18:36 +01002943 /* cap, mode, id */
2944 chunk_appendf(&trash, "<div class=tips>cap: %s, mode: %s, id: %d",
2945 proxy_cap_str(px->cap), proxy_mode_str(px->mode),
2946 px->uuid);
2947 chunk_appendf(&trash, "</div>");
2948 }
2949
2950 chunk_appendf(&trash,
2951 "%s</th>"
2952 "<th class=\"%s\" width=\"90%%\">%s</th>"
2953 "</tr>\n"
2954 "</table>\n"
2955 "<table class=\"tbl\" width=\"100%%\">\n"
2956 "<tr class=\"titre\">",
Willy Tarreau91cefca2022-05-03 17:08:29 +02002957 (ctx->flags & STAT_SHLGNDS) ? "</u>":"",
William Lallemand74c24fb2016-11-21 17:18:36 +01002958 px->desc ? "desc" : "empty", px->desc ? px->desc : "");
2959
Willy Tarreau91cefca2022-05-03 17:08:29 +02002960 if (ctx->flags & STAT_ADMIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +01002961 /* Column heading for Enable or Disable server */
Christopher Fauletbc271ec2019-12-02 11:29:04 +01002962 if ((px->cap & PR_CAP_BE) && px->srv)
2963 chunk_appendf(&trash,
2964 "<th rowspan=2 width=1><input type=\"checkbox\" "
2965 "onclick=\"for(c in document.getElementsByClassName('%s-checkbox')) "
2966 "document.getElementsByClassName('%s-checkbox').item(c).checked = this.checked\"></th>",
2967 px->id,
2968 px->id);
2969 else
2970 chunk_appendf(&trash, "<th rowspan=2></th>");
William Lallemand74c24fb2016-11-21 17:18:36 +01002971 }
2972
2973 chunk_appendf(&trash,
2974 "<th rowspan=2></th>"
2975 "<th colspan=3>Queue</th>"
2976 "<th colspan=3>Session rate</th><th colspan=6>Sessions</th>"
2977 "<th colspan=2>Bytes</th><th colspan=2>Denied</th>"
2978 "<th colspan=3>Errors</th><th colspan=2>Warnings</th>"
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02002979 "<th colspan=9>Server</th>");
2980
Willy Tarreau91cefca2022-05-03 17:08:29 +02002981 if (ctx->flags & STAT_SHMODULES) {
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02002982 // calculate the count of module for colspan attribute
2983 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
2984 ++stats_module_len;
2985 }
2986 chunk_appendf(&trash, "<th colspan=%d>Extra modules</th>",
2987 stats_module_len);
2988 }
2989
2990 chunk_appendf(&trash,
William Lallemand74c24fb2016-11-21 17:18:36 +01002991 "</tr>\n"
2992 "<tr class=\"titre\">"
2993 "<th>Cur</th><th>Max</th><th>Limit</th>"
2994 "<th>Cur</th><th>Max</th><th>Limit</th><th>Cur</th><th>Max</th>"
2995 "<th>Limit</th><th>Total</th><th>LbTot</th><th>Last</th><th>In</th><th>Out</th>"
2996 "<th>Req</th><th>Resp</th><th>Req</th><th>Conn</th>"
2997 "<th>Resp</th><th>Retr</th><th>Redis</th>"
2998 "<th>Status</th><th>LastChk</th><th>Wght</th><th>Act</th>"
2999 "<th>Bck</th><th>Chk</th><th>Dwn</th><th>Dwntme</th>"
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02003000 "<th>Thrtle</th>\n");
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02003001
Willy Tarreau91cefca2022-05-03 17:08:29 +02003002 if (ctx->flags & STAT_SHMODULES) {
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02003003 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
3004 chunk_appendf(&trash, "<th>%s</th>", mod->name);
3005 }
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02003006 }
3007
3008 chunk_appendf(&trash, "</tr>");
William Lallemand74c24fb2016-11-21 17:18:36 +01003009}
3010
3011/* Dumps the HTML table trailer for proxy <px> to the trash for and uses the state from
Willy Tarreaucaff6312022-05-27 10:17:46 +02003012 * stream connector <sc>. The caller is responsible for clearing the trash if needed.
William Lallemand74c24fb2016-11-21 17:18:36 +01003013 */
Willy Tarreaucaff6312022-05-27 10:17:46 +02003014static void stats_dump_html_px_end(struct stconn *sc, struct proxy *px)
William Lallemand74c24fb2016-11-21 17:18:36 +01003015{
Willy Tarreaucaff6312022-05-27 10:17:46 +02003016 struct appctx *appctx = __sc_appctx(sc);
Willy Tarreau91cefca2022-05-03 17:08:29 +02003017 struct show_stat_ctx *ctx = appctx->svcctx;
Christopher Faulet693b23b2022-02-28 09:09:05 +01003018
William Lallemand74c24fb2016-11-21 17:18:36 +01003019 chunk_appendf(&trash, "</table>");
3020
Willy Tarreau91cefca2022-05-03 17:08:29 +02003021 if ((px->cap & PR_CAP_BE) && px->srv && (ctx->flags & STAT_ADMIN)) {
William Lallemand74c24fb2016-11-21 17:18:36 +01003022 /* close the form used to enable/disable this proxy servers */
3023 chunk_appendf(&trash,
3024 "Choose the action to perform on the checked servers : "
3025 "<select name=action>"
3026 "<option value=\"\"></option>"
3027 "<option value=\"ready\">Set state to READY</option>"
3028 "<option value=\"drain\">Set state to DRAIN</option>"
3029 "<option value=\"maint\">Set state to MAINT</option>"
3030 "<option value=\"dhlth\">Health: disable checks</option>"
3031 "<option value=\"ehlth\">Health: enable checks</option>"
3032 "<option value=\"hrunn\">Health: force UP</option>"
3033 "<option value=\"hnolb\">Health: force NOLB</option>"
3034 "<option value=\"hdown\">Health: force DOWN</option>"
3035 "<option value=\"dagent\">Agent: disable checks</option>"
3036 "<option value=\"eagent\">Agent: enable checks</option>"
3037 "<option value=\"arunn\">Agent: force UP</option>"
3038 "<option value=\"adown\">Agent: force DOWN</option>"
3039 "<option value=\"shutdown\">Kill Sessions</option>"
3040 "</select>"
3041 "<input type=\"hidden\" name=\"b\" value=\"#%d\">"
3042 "&nbsp;<input type=\"submit\" value=\"Apply\">"
3043 "</form>",
3044 px->uuid);
3045 }
3046
3047 chunk_appendf(&trash, "<p>\n");
3048}
3049
3050/*
Willy Tarreau4596fe22022-05-17 19:07:51 +02003051 * Dumps statistics for a proxy. The output is sent to the stream connector's
William Lallemand74c24fb2016-11-21 17:18:36 +01003052 * input buffer. Returns 0 if it had to stop dumping data because of lack of
3053 * buffer space, or non-zero if everything completed. This function is used
3054 * both by the CLI and the HTTP entry points, and is able to dump the output
3055 * in HTML or CSV formats. If the later, <uri> must be NULL.
3056 */
Willy Tarreaucaff6312022-05-27 10:17:46 +02003057int stats_dump_proxy_to_buffer(struct stconn *sc, struct htx *htx,
Christopher Fauletef779222018-10-31 08:47:01 +01003058 struct proxy *px, struct uri_auth *uri)
William Lallemand74c24fb2016-11-21 17:18:36 +01003059{
Willy Tarreaucaff6312022-05-27 10:17:46 +02003060 struct appctx *appctx = __sc_appctx(sc);
Willy Tarreau91cefca2022-05-03 17:08:29 +02003061 struct show_stat_ctx *ctx = appctx->svcctx;
Willy Tarreaucaff6312022-05-27 10:17:46 +02003062 struct stream *s = __sc_strm(sc);
3063 struct channel *rep = sc_ic(sc);
William Lallemand74c24fb2016-11-21 17:18:36 +01003064 struct server *sv, *svs; /* server and server-state, server-state=server or server->track */
3065 struct listener *l;
Aurelien DARRAGON55941842022-12-15 14:01:04 +01003066 int current_field;
William Lallemand74c24fb2016-11-21 17:18:36 +01003067
3068 chunk_reset(&trash);
Aurelien DARRAGON55941842022-12-15 14:01:04 +01003069more:
3070 current_field = ctx->field;
William Lallemand74c24fb2016-11-21 17:18:36 +01003071
Willy Tarreau91cefca2022-05-03 17:08:29 +02003072 switch (ctx->px_st) {
William Lallemand74c24fb2016-11-21 17:18:36 +01003073 case STAT_PX_ST_INIT:
3074 /* we are on a new proxy */
3075 if (uri && uri->scope) {
3076 /* we have a limited scope, we have to check the proxy name */
3077 struct stat_scope *scope;
3078 int len;
3079
3080 len = strlen(px->id);
3081 scope = uri->scope;
3082
3083 while (scope) {
3084 /* match exact proxy name */
3085 if (scope->px_len == len && !memcmp(px->id, scope->px_id, len))
3086 break;
3087
3088 /* match '.' which means 'self' proxy */
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003089 if (strcmp(scope->px_id, ".") == 0 && px == s->be)
William Lallemand74c24fb2016-11-21 17:18:36 +01003090 break;
3091 scope = scope->next;
3092 }
3093
3094 /* proxy name not found : don't dump anything */
3095 if (scope == NULL)
3096 return 1;
3097 }
3098
3099 /* if the user has requested a limited output and the proxy
3100 * name does not match, skip it.
3101 */
Willy Tarreau91cefca2022-05-03 17:08:29 +02003102 if (ctx->scope_len) {
Willy Tarreaucaff6312022-05-27 10:17:46 +02003103 const char *scope_ptr = stats_scope_ptr(appctx, sc);
Christopher Fauleted7a0662019-01-14 11:07:34 +01003104
Willy Tarreau91cefca2022-05-03 17:08:29 +02003105 if (strnistr(px->id, strlen(px->id), scope_ptr, ctx->scope_len) == NULL)
Christopher Fauleted7a0662019-01-14 11:07:34 +01003106 return 1;
3107 }
William Lallemand74c24fb2016-11-21 17:18:36 +01003108
Willy Tarreau91cefca2022-05-03 17:08:29 +02003109 if ((ctx->flags & STAT_BOUND) &&
3110 (ctx->iid != -1) &&
3111 (px->uuid != ctx->iid))
William Lallemand74c24fb2016-11-21 17:18:36 +01003112 return 1;
3113
Willy Tarreau91cefca2022-05-03 17:08:29 +02003114 ctx->px_st = STAT_PX_ST_TH;
Willy Tarreau9eb93c02022-11-14 07:12:05 +01003115 __fallthrough;
William Lallemand74c24fb2016-11-21 17:18:36 +01003116
3117 case STAT_PX_ST_TH:
Willy Tarreau91cefca2022-05-03 17:08:29 +02003118 if (ctx->flags & STAT_FMT_HTML) {
Willy Tarreaucaff6312022-05-27 10:17:46 +02003119 stats_dump_html_px_hdr(sc, px);
Christopher Fauletef779222018-10-31 08:47:01 +01003120 if (!stats_putchk(rep, htx, &trash))
3121 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01003122 }
3123
Willy Tarreau91cefca2022-05-03 17:08:29 +02003124 ctx->px_st = STAT_PX_ST_FE;
Willy Tarreau9eb93c02022-11-14 07:12:05 +01003125 __fallthrough;
William Lallemand74c24fb2016-11-21 17:18:36 +01003126
3127 case STAT_PX_ST_FE:
3128 /* print the frontend */
Willy Tarreaucaff6312022-05-27 10:17:46 +02003129 if (stats_dump_fe_stats(sc, px)) {
Christopher Fauletef779222018-10-31 08:47:01 +01003130 if (!stats_putchk(rep, htx, &trash))
3131 goto full;
Aurelien DARRAGON55941842022-12-15 14:01:04 +01003132 if (ctx->field)
3133 goto more;
William Lallemand74c24fb2016-11-21 17:18:36 +01003134 }
3135
Aurelien DARRAGON55941842022-12-15 14:01:04 +01003136 current_field = 0;
Willy Tarreau91cefca2022-05-03 17:08:29 +02003137 ctx->obj2 = px->conf.listeners.n;
3138 ctx->px_st = STAT_PX_ST_LI;
Willy Tarreau9eb93c02022-11-14 07:12:05 +01003139 __fallthrough;
William Lallemand74c24fb2016-11-21 17:18:36 +01003140
3141 case STAT_PX_ST_LI:
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +02003142 /* obj2 points to listeners list as initialized above */
Willy Tarreau91cefca2022-05-03 17:08:29 +02003143 for (; ctx->obj2 != &px->conf.listeners; ctx->obj2 = l->by_fe.n) {
Christopher Fauletef779222018-10-31 08:47:01 +01003144 if (htx) {
3145 if (htx_almost_full(htx))
3146 goto full;
3147 }
3148 else {
3149 if (buffer_almost_full(&rep->buf))
3150 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01003151 }
3152
Willy Tarreau91cefca2022-05-03 17:08:29 +02003153 l = LIST_ELEM(ctx->obj2, struct listener *, by_fe);
William Lallemand74c24fb2016-11-21 17:18:36 +01003154 if (!l->counters)
3155 continue;
3156
Willy Tarreau91cefca2022-05-03 17:08:29 +02003157 if (ctx->flags & STAT_BOUND) {
3158 if (!(ctx->type & (1 << STATS_TYPE_SO)))
William Lallemand74c24fb2016-11-21 17:18:36 +01003159 break;
3160
Willy Tarreau91cefca2022-05-03 17:08:29 +02003161 if (ctx->sid != -1 && l->luid != ctx->sid)
William Lallemand74c24fb2016-11-21 17:18:36 +01003162 continue;
3163 }
3164
3165 /* print the frontend */
Willy Tarreaucaff6312022-05-27 10:17:46 +02003166 if (stats_dump_li_stats(sc, px, l)) {
Christopher Fauletef779222018-10-31 08:47:01 +01003167 if (!stats_putchk(rep, htx, &trash))
3168 goto full;
Aurelien DARRAGON55941842022-12-15 14:01:04 +01003169 if (ctx->field)
3170 goto more;
William Lallemand74c24fb2016-11-21 17:18:36 +01003171 }
3172 }
3173
Aurelien DARRAGON55941842022-12-15 14:01:04 +01003174 current_field = 0;
Willy Tarreau91cefca2022-05-03 17:08:29 +02003175 ctx->obj2 = px->srv; /* may be NULL */
3176 ctx->px_st = STAT_PX_ST_SV;
Willy Tarreau9eb93c02022-11-14 07:12:05 +01003177 __fallthrough;
William Lallemand74c24fb2016-11-21 17:18:36 +01003178
3179 case STAT_PX_ST_SV:
Amaury Denoyelle0a8d05d2021-08-25 14:39:53 +02003180 /* obj2 points to servers list as initialized above.
3181 *
Amaury Denoyellebc2ebfa2021-08-25 15:34:53 +02003182 * A server may be removed during the stats dumping.
Amaury Denoyelle0a8d05d2021-08-25 14:39:53 +02003183 * Temporarily increment its refcount to prevent its
3184 * anticipated cleaning. Call free_server to release it.
3185 */
Willy Tarreau91cefca2022-05-03 17:08:29 +02003186 for (; ctx->obj2 != NULL;
3187 ctx->obj2 = srv_drop(sv)) {
Amaury Denoyelle0a8d05d2021-08-25 14:39:53 +02003188
Willy Tarreau91cefca2022-05-03 17:08:29 +02003189 sv = ctx->obj2;
Amaury Denoyellebc2ebfa2021-08-25 15:34:53 +02003190 srv_take(sv);
Amaury Denoyelle0a8d05d2021-08-25 14:39:53 +02003191
Christopher Fauletef779222018-10-31 08:47:01 +01003192 if (htx) {
3193 if (htx_almost_full(htx))
3194 goto full;
3195 }
3196 else {
3197 if (buffer_almost_full(&rep->buf))
3198 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01003199 }
3200
Willy Tarreau91cefca2022-05-03 17:08:29 +02003201 if (ctx->flags & STAT_BOUND) {
3202 if (!(ctx->type & (1 << STATS_TYPE_SV))) {
Amaury Denoyellebc2ebfa2021-08-25 15:34:53 +02003203 srv_drop(sv);
William Lallemand74c24fb2016-11-21 17:18:36 +01003204 break;
Amaury Denoyelle0a8d05d2021-08-25 14:39:53 +02003205 }
William Lallemand74c24fb2016-11-21 17:18:36 +01003206
Willy Tarreau91cefca2022-05-03 17:08:29 +02003207 if (ctx->sid != -1 && sv->puid != ctx->sid)
William Lallemand74c24fb2016-11-21 17:18:36 +01003208 continue;
3209 }
3210
Willy Tarreau3e320362020-10-23 17:28:57 +02003211 /* do not report disabled servers */
Willy Tarreau91cefca2022-05-03 17:08:29 +02003212 if (ctx->flags & STAT_HIDE_MAINT &&
Willy Tarreau3e320362020-10-23 17:28:57 +02003213 sv->cur_admin & SRV_ADMF_MAINT) {
3214 continue;
3215 }
3216
William Lallemand74c24fb2016-11-21 17:18:36 +01003217 svs = sv;
3218 while (svs->track)
3219 svs = svs->track;
3220
3221 /* do not report servers which are DOWN and not changing state */
Willy Tarreau91cefca2022-05-03 17:08:29 +02003222 if ((ctx->flags & STAT_HIDE_DOWN) &&
Emeric Brun52a91d32017-08-31 14:41:55 +02003223 ((sv->cur_admin & SRV_ADMF_MAINT) || /* server is in maintenance */
3224 (sv->cur_state == SRV_ST_STOPPED && /* server is down */
William Lallemand74c24fb2016-11-21 17:18:36 +01003225 (!((svs->agent.state | svs->check.state) & CHK_ST_ENABLED) ||
3226 ((svs->agent.state & CHK_ST_ENABLED) && !svs->agent.health) ||
3227 ((svs->check.state & CHK_ST_ENABLED) && !svs->check.health))))) {
3228 continue;
3229 }
3230
Willy Tarreaucaff6312022-05-27 10:17:46 +02003231 if (stats_dump_sv_stats(sc, px, sv)) {
Christopher Fauletef779222018-10-31 08:47:01 +01003232 if (!stats_putchk(rep, htx, &trash))
3233 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01003234 }
3235 } /* for sv */
3236
Willy Tarreau91cefca2022-05-03 17:08:29 +02003237 ctx->px_st = STAT_PX_ST_BE;
Willy Tarreau9eb93c02022-11-14 07:12:05 +01003238 __fallthrough;
William Lallemand74c24fb2016-11-21 17:18:36 +01003239
3240 case STAT_PX_ST_BE:
3241 /* print the backend */
Willy Tarreaucaff6312022-05-27 10:17:46 +02003242 if (stats_dump_be_stats(sc, px)) {
Christopher Fauletef779222018-10-31 08:47:01 +01003243 if (!stats_putchk(rep, htx, &trash))
3244 goto full;
Aurelien DARRAGON55941842022-12-15 14:01:04 +01003245 if (ctx->field)
3246 goto more;
William Lallemand74c24fb2016-11-21 17:18:36 +01003247 }
3248
Aurelien DARRAGON55941842022-12-15 14:01:04 +01003249 current_field = 0;
Willy Tarreau91cefca2022-05-03 17:08:29 +02003250 ctx->px_st = STAT_PX_ST_END;
Willy Tarreau9eb93c02022-11-14 07:12:05 +01003251 __fallthrough;
William Lallemand74c24fb2016-11-21 17:18:36 +01003252
3253 case STAT_PX_ST_END:
Willy Tarreau91cefca2022-05-03 17:08:29 +02003254 if (ctx->flags & STAT_FMT_HTML) {
Willy Tarreaucaff6312022-05-27 10:17:46 +02003255 stats_dump_html_px_end(sc, px);
Christopher Fauletef779222018-10-31 08:47:01 +01003256 if (!stats_putchk(rep, htx, &trash))
3257 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01003258 }
3259
Willy Tarreau91cefca2022-05-03 17:08:29 +02003260 ctx->px_st = STAT_PX_ST_FIN;
Willy Tarreau9eb93c02022-11-14 07:12:05 +01003261 __fallthrough;
William Lallemand74c24fb2016-11-21 17:18:36 +01003262
3263 case STAT_PX_ST_FIN:
3264 return 1;
3265
3266 default:
3267 /* unknown state, we should put an abort() here ! */
3268 return 1;
3269 }
Christopher Fauletef779222018-10-31 08:47:01 +01003270
3271 full:
Willy Tarreaucaff6312022-05-27 10:17:46 +02003272 sc_need_room(sc);
Aurelien DARRAGON55941842022-12-15 14:01:04 +01003273 /* restore previous field */
3274 ctx->field = current_field;
Christopher Fauletef779222018-10-31 08:47:01 +01003275 return 0;
William Lallemand74c24fb2016-11-21 17:18:36 +01003276}
3277
3278/* Dumps the HTTP stats head block to the trash for and uses the per-uri
3279 * parameters <uri>. The caller is responsible for clearing the trash if needed.
3280 */
Willy Tarreau676c29e2019-10-09 10:50:01 +02003281static void stats_dump_html_head(struct appctx *appctx, struct uri_auth *uri)
William Lallemand74c24fb2016-11-21 17:18:36 +01003282{
Willy Tarreau91cefca2022-05-03 17:08:29 +02003283 struct show_stat_ctx *ctx = appctx->svcctx;
3284
William Lallemand74c24fb2016-11-21 17:18:36 +01003285 /* WARNING! This must fit in the first buffer !!! */
3286 chunk_appendf(&trash,
3287 "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"\n"
3288 "\"http://www.w3.org/TR/html4/loose.dtd\">\n"
3289 "<html><head><title>Statistics Report for " PRODUCT_NAME "%s%s</title>\n"
Thierry Fournierb6b1cde2021-09-24 00:03:07 +02003290 "<link rel=\"icon\" href=\"data:,\">\n"
William Lallemand74c24fb2016-11-21 17:18:36 +01003291 "<meta http-equiv=\"content-type\" content=\"text/html; charset=iso-8859-1\">\n"
3292 "<style type=\"text/css\"><!--\n"
3293 "body {"
3294 " font-family: arial, helvetica, sans-serif;"
3295 " font-size: 12px;"
3296 " font-weight: normal;"
3297 " color: black;"
3298 " background: white;"
3299 "}\n"
3300 "th,td {"
3301 " font-size: 10px;"
3302 "}\n"
3303 "h1 {"
3304 " font-size: x-large;"
3305 " margin-bottom: 0.5em;"
3306 "}\n"
3307 "h2 {"
3308 " font-family: helvetica, arial;"
3309 " font-size: x-large;"
3310 " font-weight: bold;"
3311 " font-style: italic;"
3312 " color: #6020a0;"
3313 " margin-top: 0em;"
3314 " margin-bottom: 0em;"
3315 "}\n"
3316 "h3 {"
3317 " font-family: helvetica, arial;"
3318 " font-size: 16px;"
3319 " font-weight: bold;"
3320 " color: #b00040;"
3321 " background: #e8e8d0;"
3322 " margin-top: 0em;"
3323 " margin-bottom: 0em;"
3324 "}\n"
3325 "li {"
3326 " margin-top: 0.25em;"
3327 " margin-right: 2em;"
3328 "}\n"
3329 ".hr {margin-top: 0.25em;"
3330 " border-color: black;"
3331 " border-bottom-style: solid;"
3332 "}\n"
3333 ".titre {background: #20D0D0;color: #000000; font-weight: bold; text-align: center;}\n"
3334 ".total {background: #20D0D0;color: #ffff80;}\n"
3335 ".frontend {background: #e8e8d0;}\n"
3336 ".socket {background: #d0d0d0;}\n"
3337 ".backend {background: #e8e8d0;}\n"
3338 ".active_down {background: #ff9090;}\n"
3339 ".active_going_up {background: #ffd020;}\n"
3340 ".active_going_down {background: #ffffa0;}\n"
3341 ".active_up {background: #c0ffc0;}\n"
3342 ".active_nolb {background: #20a0ff;}\n"
3343 ".active_draining {background: #20a0FF;}\n"
3344 ".active_no_check {background: #e0e0e0;}\n"
3345 ".backup_down {background: #ff9090;}\n"
3346 ".backup_going_up {background: #ff80ff;}\n"
3347 ".backup_going_down {background: #c060ff;}\n"
3348 ".backup_up {background: #b0d0ff;}\n"
3349 ".backup_nolb {background: #90b0e0;}\n"
3350 ".backup_draining {background: #cc9900;}\n"
3351 ".backup_no_check {background: #e0e0e0;}\n"
3352 ".maintain {background: #c07820;}\n"
3353 ".rls {letter-spacing: 0.2em; margin-right: 1px;}\n" /* right letter spacing (used for grouping digits) */
3354 "\n"
3355 "a.px:link {color: #ffff40; text-decoration: none;}"
3356 "a.px:visited {color: #ffff40; text-decoration: none;}"
3357 "a.px:hover {color: #ffffff; text-decoration: none;}"
3358 "a.lfsb:link {color: #000000; text-decoration: none;}"
3359 "a.lfsb:visited {color: #000000; text-decoration: none;}"
3360 "a.lfsb:hover {color: #505050; text-decoration: none;}"
3361 "\n"
3362 "table.tbl { border-collapse: collapse; border-style: none;}\n"
3363 "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"
3364 "table.tbl td.ac { text-align: center;}\n"
3365 "table.tbl th { border-width: 1px; border-style: solid solid solid solid; border-color: gray;}\n"
3366 "table.tbl th.pxname { background: #b00040; color: #ffff40; font-weight: bold; border-style: solid solid none solid; padding: 2px 3px; white-space: nowrap;}\n"
3367 "table.tbl th.empty { border-style: none; empty-cells: hide; background: white;}\n"
3368 "table.tbl th.desc { background: white; border-style: solid solid none solid; text-align: left; padding: 2px 3px;}\n"
3369 "\n"
3370 "table.lgd { border-collapse: collapse; border-width: 1px; border-style: none none none solid; border-color: black;}\n"
3371 "table.lgd td { border-width: 1px; border-style: solid solid solid solid; border-color: gray; padding: 2px;}\n"
3372 "table.lgd td.noborder { border-style: none; padding: 2px; white-space: nowrap;}\n"
3373 "table.det { border-collapse: collapse; border-style: none; }\n"
3374 "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"
3375 "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"
3376 "u {text-decoration:none; border-bottom: 1px dotted black;}\n"
Thierry Fournierb6b1cde2021-09-24 00:03:07 +02003377 "div.tips {\n"
3378 " display:block;\n"
3379 " visibility:hidden;\n"
3380 " z-index:2147483647;\n"
3381 " position:absolute;\n"
3382 " padding:2px 4px 3px;\n"
3383 " background:#f0f060; color:#000000;\n"
3384 " border:1px solid #7040c0;\n"
3385 " white-space:nowrap;\n"
3386 " font-style:normal;font-size:11px;font-weight:normal;\n"
3387 " -moz-border-radius:3px;-webkit-border-radius:3px;border-radius:3px;\n"
3388 " -moz-box-shadow:gray 2px 2px 3px;-webkit-box-shadow:gray 2px 2px 3px;box-shadow:gray 2px 2px 3px;\n"
3389 "}\n"
3390 "u:hover div.tips {visibility:visible;}\n"
Thierry Fourniere9ed63e2021-09-23 23:59:49 +02003391 "@media (prefers-color-scheme: dark) {\n"
3392 " body { font-family: arial, helvetica, sans-serif; font-size: 12px; font-weight: normal; color: #e8e6e3; background: #131516;}\n"
3393 " h1 { color: #a265e0!important; }\n"
3394 " h2 { color: #a265e0; }\n"
3395 " h3 { color: #ff5190; background-color: #3e3e1f; }\n"
3396 " a { color: #3391ff; }\n"
3397 " input { background-color: #2f3437; }\n"
3398 " .hr { border-color: #8c8273; }\n"
3399 " .titre { background-color: #1aa6a6; color: #e8e6e3; }\n"
3400 " .frontend {background: #2f3437;}\n"
Marno Krahmera690b732022-03-08 13:45:09 +01003401 " .socket {background: #2a2d2f;}\n"
Thierry Fourniere9ed63e2021-09-23 23:59:49 +02003402 " .backend {background: #2f3437;}\n"
3403 " .active_down {background: #760000;}\n"
3404 " .active_going_up {background: #b99200;}\n"
3405 " .active_going_down {background: #6c6c00;}\n"
3406 " .active_up {background: #165900;}\n"
3407 " .active_nolb {background: #006ab9;}\n"
3408 " .active_draining {background: #006ab9;}\n"
3409 " .active_no_check {background: #2a2d2f;}\n"
3410 " .backup_down {background: #760000;}\n"
3411 " .backup_going_up {background: #7f007f;}\n"
3412 " .backup_going_down {background: #580092;}\n"
3413 " .backup_up {background: #2e3234;}\n"
3414 " .backup_nolb {background: #1e3c6a;}\n"
3415 " .backup_draining {background: #a37a00;}\n"
3416 " .backup_no_check {background: #2a2d2f;}\n"
3417 " .maintain {background: #9a601a;}\n"
3418 " a.px:link {color: #d8d83b; text-decoration: none;}\n"
3419 " a.px:visited {color: #d8d83b; text-decoration: none;}\n"
3420 " a.px:hover {color: #ffffff; text-decoration: none;}\n"
3421 " a.lfsb:link {color: #e8e6e3; text-decoration: none;}\n"
3422 " a.lfsb:visited {color: #e8e6e3; text-decoration: none;}\n"
3423 " a.lfsb:hover {color: #b5afa6; text-decoration: none;}\n"
3424 " table.tbl th.empty { background-color: #181a1b; }\n"
Willy Tarreau00147f72022-04-11 07:59:27 +02003425 " table.tbl th.desc { background: #181a1b; }\n"
Thierry Fourniere9ed63e2021-09-23 23:59:49 +02003426 " table.tbl th.pxname { background-color: #8d0033; color: #ffff46; }\n"
3427 " table.tbl th { border-color: #808080; }\n"
3428 " table.tbl td { border-color: #808080; }\n"
3429 " u {text-decoration:none; border-bottom: 1px dotted #e8e6e3;}\n"
3430 " div.tips {\n"
3431 " background:#8e8e0d;\n"
3432 " color:#e8e6e3;\n"
3433 " border-color: #4e2c86;\n"
3434 " -moz-box-shadow: #60686c 2px 2px 3px;\n"
3435 " -webkit-box-shadow: #60686c 2px 2px 3px;\n"
3436 " box-shadow: #60686c 2px 2px 3px;\n"
3437 " }\n"
3438 "}\n"
William Lallemand74c24fb2016-11-21 17:18:36 +01003439 "-->\n"
3440 "</style></head>\n",
Willy Tarreau91cefca2022-05-03 17:08:29 +02003441 (ctx->flags & STAT_SHNODE) ? " on " : "",
3442 (ctx->flags & STAT_SHNODE) ? (uri && uri->node ? uri->node : global.node) : ""
William Lallemand74c24fb2016-11-21 17:18:36 +01003443 );
3444}
3445
3446/* Dumps the HTML stats information block to the trash for and uses the state from
Willy Tarreaucaff6312022-05-27 10:17:46 +02003447 * stream connector <sc> and per-uri parameters <uri>. The caller is responsible
William Lallemand74c24fb2016-11-21 17:18:36 +01003448 * for clearing the trash if needed.
3449 */
Willy Tarreaucaff6312022-05-27 10:17:46 +02003450static void stats_dump_html_info(struct stconn *sc, struct uri_auth *uri)
William Lallemand74c24fb2016-11-21 17:18:36 +01003451{
Willy Tarreaucaff6312022-05-27 10:17:46 +02003452 struct appctx *appctx = __sc_appctx(sc);
Willy Tarreau91cefca2022-05-03 17:08:29 +02003453 struct show_stat_ctx *ctx = appctx->svcctx;
William Lallemand74c24fb2016-11-21 17:18:36 +01003454 unsigned int up = (now.tv_sec - start_date.tv_sec);
3455 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
Willy Tarreaucaff6312022-05-27 10:17:46 +02003456 const char *scope_ptr = stats_scope_ptr(appctx, sc);
Willy Tarreau1713c032019-05-23 12:23:55 +02003457 unsigned long long bps = (unsigned long long)read_freq_ctr(&global.out_32bps) * 32;
3458
3459 /* Turn the bytes per second to bits per second and take care of the
3460 * usual ethernet overhead in order to help figure how far we are from
3461 * interface saturation since it's the only case which usually matters.
3462 * For this we count the total size of an Ethernet frame on the wire
3463 * including preamble and IFG (1538) for the largest TCP segment it
3464 * transports (1448 with TCP timestamps). This is not valid for smaller
3465 * packets (under-estimated), but it gives a reasonably accurate
3466 * estimation of how far we are from uplink saturation.
3467 */
3468 bps = bps * 8 * 1538 / 1448;
William Lallemand74c24fb2016-11-21 17:18:36 +01003469
3470 /* WARNING! this has to fit the first packet too.
3471 * We are around 3.5 kB, add adding entries will
3472 * become tricky if we want to support 4kB buffers !
3473 */
3474 chunk_appendf(&trash,
3475 "<body><h1><a href=\"" PRODUCT_URL "\" style=\"text-decoration: none;\">"
3476 PRODUCT_NAME "%s</a></h1>\n"
3477 "<h2>Statistics Report for pid %d%s%s%s%s</h2>\n"
3478 "<hr width=\"100%%\" class=\"hr\">\n"
3479 "<h3>&gt; General process information</h3>\n"
3480 "<table border=0><tr><td align=\"left\" nowrap width=\"1%%\">\n"
Yves Lafon95317282018-02-26 11:10:37 +01003481 "<p><b>pid = </b> %d (process #%d, nbproc = %d, nbthread = %d)<br>\n"
William Lallemand74c24fb2016-11-21 17:18:36 +01003482 "<b>uptime = </b> %dd %dh%02dm%02ds<br>\n"
3483 "<b>system limits:</b> memmax = %s%s; ulimit-n = %d<br>\n"
3484 "<b>maxsock = </b> %d; <b>maxconn = </b> %d; <b>maxpipes = </b> %d<br>\n"
Willy Tarreau1713c032019-05-23 12:23:55 +02003485 "current conns = %d; current pipes = %d/%d; conn rate = %d/sec; bit rate = %.3f %cbps<br>\n"
William Lallemand74c24fb2016-11-21 17:18:36 +01003486 "Running tasks: %d/%d; idle = %d %%<br>\n"
3487 "</td><td align=\"center\" nowrap>\n"
3488 "<table class=\"lgd\"><tr>\n"
3489 "<td class=\"active_up\">&nbsp;</td><td class=\"noborder\">active UP </td>"
3490 "<td class=\"backup_up\">&nbsp;</td><td class=\"noborder\">backup UP </td>"
3491 "</tr><tr>\n"
3492 "<td class=\"active_going_down\"></td><td class=\"noborder\">active UP, going down </td>"
3493 "<td class=\"backup_going_down\"></td><td class=\"noborder\">backup UP, going down </td>"
3494 "</tr><tr>\n"
3495 "<td class=\"active_going_up\"></td><td class=\"noborder\">active DOWN, going up </td>"
3496 "<td class=\"backup_going_up\"></td><td class=\"noborder\">backup DOWN, going up </td>"
3497 "</tr><tr>\n"
3498 "<td class=\"active_down\"></td><td class=\"noborder\">active or backup DOWN &nbsp;</td>"
3499 "<td class=\"active_no_check\"></td><td class=\"noborder\">not checked </td>"
3500 "</tr><tr>\n"
3501 "<td class=\"maintain\"></td><td class=\"noborder\" colspan=\"3\">active or backup DOWN for maintenance (MAINT) &nbsp;</td>"
3502 "</tr><tr>\n"
3503 "<td class=\"active_draining\"></td><td class=\"noborder\" colspan=\"3\">active or backup SOFT STOPPED for maintenance &nbsp;</td>"
3504 "</tr></table>\n"
3505 "Note: \"NOLB\"/\"DRAIN\" = UP with load-balancing disabled."
3506 "</td>"
3507 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
3508 "<b>Display option:</b><ul style=\"margin-top: 0.25em;\">"
3509 "",
Willy Tarreau91cefca2022-05-03 17:08:29 +02003510 (ctx->flags & STAT_HIDEVER) ? "" : (stats_version_string),
3511 pid, (ctx->flags & STAT_SHNODE) ? " on " : "",
3512 (ctx->flags & STAT_SHNODE) ? (uri->node ? uri->node : global.node) : "",
3513 (ctx->flags & STAT_SHDESC) ? ": " : "",
3514 (ctx->flags & STAT_SHDESC) ? (uri->desc ? uri->desc : global.desc) : "",
Willy Tarreaue8422bf2021-06-15 09:08:18 +02003515 pid, 1, 1, global.nbthread,
William Lallemand74c24fb2016-11-21 17:18:36 +01003516 up / 86400, (up % 86400) / 3600,
3517 (up % 3600) / 60, (up % 60),
3518 global.rlimit_memmax ? ultoa(global.rlimit_memmax) : "unlimited",
3519 global.rlimit_memmax ? " MB" : "",
3520 global.rlimit_nofile,
3521 global.maxsock, global.maxconn, global.maxpipes,
3522 actconn, pipes_used, pipes_used+pipes_free, read_freq_ctr(&global.conn_per_sec),
Willy Tarreau1713c032019-05-23 12:23:55 +02003523 bps >= 1000000000UL ? (bps / 1000000000.0) : bps >= 1000000UL ? (bps / 1000000.0) : (bps / 1000.0),
3524 bps >= 1000000000UL ? 'G' : bps >= 1000000UL ? 'M' : 'k',
Willy Tarreauf9d5e102021-10-08 10:43:59 +02003525 total_run_queues(), total_allocated_tasks(), clock_report_idle()
William Lallemand74c24fb2016-11-21 17:18:36 +01003526 );
3527
Willy Tarreau91cefca2022-05-03 17:08:29 +02003528 /* scope_txt = search query, ctx->scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
3529 memcpy(scope_txt, scope_ptr, ctx->scope_len);
3530 scope_txt[ctx->scope_len] = '\0';
William Lallemand74c24fb2016-11-21 17:18:36 +01003531
3532 chunk_appendf(&trash,
3533 "<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 +02003534 (ctx->scope_len > 0) ? scope_txt : "",
William Lallemand74c24fb2016-11-21 17:18:36 +01003535 STAT_SCOPE_TXT_MAXLEN);
3536
Willy Tarreau91cefca2022-05-03 17:08:29 +02003537 /* scope_txt = search pattern + search query, ctx->scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
William Lallemand74c24fb2016-11-21 17:18:36 +01003538 scope_txt[0] = 0;
Willy Tarreau91cefca2022-05-03 17:08:29 +02003539 if (ctx->scope_len) {
William Lallemand74c24fb2016-11-21 17:18:36 +01003540 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Willy Tarreau91cefca2022-05-03 17:08:29 +02003541 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), scope_ptr, ctx->scope_len);
3542 scope_txt[strlen(STAT_SCOPE_PATTERN) + ctx->scope_len] = 0;
William Lallemand74c24fb2016-11-21 17:18:36 +01003543 }
3544
Willy Tarreau91cefca2022-05-03 17:08:29 +02003545 if (ctx->flags & STAT_HIDE_DOWN)
William Lallemand74c24fb2016-11-21 17:18:36 +01003546 chunk_appendf(&trash,
3547 "<li><a href=\"%s%s%s%s\">Show all servers</a><br>\n",
3548 uri->uri_prefix,
3549 "",
Willy Tarreau91cefca2022-05-03 17:08:29 +02003550 (ctx->flags & STAT_NO_REFRESH) ? ";norefresh" : "",
William Lallemand74c24fb2016-11-21 17:18:36 +01003551 scope_txt);
3552 else
3553 chunk_appendf(&trash,
3554 "<li><a href=\"%s%s%s%s\">Hide 'DOWN' servers</a><br>\n",
3555 uri->uri_prefix,
3556 ";up",
Willy Tarreau91cefca2022-05-03 17:08:29 +02003557 (ctx->flags & STAT_NO_REFRESH) ? ";norefresh" : "",
William Lallemand74c24fb2016-11-21 17:18:36 +01003558 scope_txt);
3559
3560 if (uri->refresh > 0) {
Willy Tarreau91cefca2022-05-03 17:08:29 +02003561 if (ctx->flags & STAT_NO_REFRESH)
William Lallemand74c24fb2016-11-21 17:18:36 +01003562 chunk_appendf(&trash,
3563 "<li><a href=\"%s%s%s%s\">Enable refresh</a><br>\n",
3564 uri->uri_prefix,
Willy Tarreau91cefca2022-05-03 17:08:29 +02003565 (ctx->flags & STAT_HIDE_DOWN) ? ";up" : "",
William Lallemand74c24fb2016-11-21 17:18:36 +01003566 "",
3567 scope_txt);
3568 else
3569 chunk_appendf(&trash,
3570 "<li><a href=\"%s%s%s%s\">Disable refresh</a><br>\n",
3571 uri->uri_prefix,
Willy Tarreau91cefca2022-05-03 17:08:29 +02003572 (ctx->flags & STAT_HIDE_DOWN) ? ";up" : "",
William Lallemand74c24fb2016-11-21 17:18:36 +01003573 ";norefresh",
3574 scope_txt);
3575 }
3576
3577 chunk_appendf(&trash,
3578 "<li><a href=\"%s%s%s%s\">Refresh now</a><br>\n",
3579 uri->uri_prefix,
Willy Tarreau91cefca2022-05-03 17:08:29 +02003580 (ctx->flags & STAT_HIDE_DOWN) ? ";up" : "",
3581 (ctx->flags & STAT_NO_REFRESH) ? ";norefresh" : "",
William Lallemand74c24fb2016-11-21 17:18:36 +01003582 scope_txt);
3583
3584 chunk_appendf(&trash,
3585 "<li><a href=\"%s;csv%s%s\">CSV export</a><br>\n",
3586 uri->uri_prefix,
3587 (uri->refresh > 0) ? ";norefresh" : "",
3588 scope_txt);
3589
3590 chunk_appendf(&trash,
Christopher Faulet6338a082019-09-09 15:50:54 +02003591 "<li><a href=\"%s;json%s%s\">JSON export</a> (<a href=\"%s;json-schema\">schema</a>)<br>\n",
3592 uri->uri_prefix,
3593 (uri->refresh > 0) ? ";norefresh" : "",
3594 scope_txt, uri->uri_prefix);
3595
3596 chunk_appendf(&trash,
William Lallemand74c24fb2016-11-21 17:18:36 +01003597 "</ul></td>"
3598 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
3599 "<b>External resources:</b><ul style=\"margin-top: 0.25em;\">\n"
3600 "<li><a href=\"" PRODUCT_URL "\">Primary site</a><br>\n"
3601 "<li><a href=\"" PRODUCT_URL_UPD "\">Updates (v" PRODUCT_BRANCH ")</a><br>\n"
3602 "<li><a href=\"" PRODUCT_URL_DOC "\">Online manual</a><br>\n"
3603 "</ul>"
3604 "</td>"
3605 "</tr></table>\n"
3606 ""
3607 );
3608
Willy Tarreau91cefca2022-05-03 17:08:29 +02003609 if (ctx->st_code) {
3610 switch (ctx->st_code) {
William Lallemand74c24fb2016-11-21 17:18:36 +01003611 case STAT_STATUS_DONE:
3612 chunk_appendf(&trash,
3613 "<p><div class=active_up>"
3614 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
3615 "Action processed successfully."
3616 "</div>\n", uri->uri_prefix,
Willy Tarreau91cefca2022-05-03 17:08:29 +02003617 (ctx->flags & STAT_HIDE_DOWN) ? ";up" : "",
3618 (ctx->flags & STAT_NO_REFRESH) ? ";norefresh" : "",
William Lallemand74c24fb2016-11-21 17:18:36 +01003619 scope_txt);
3620 break;
3621 case STAT_STATUS_NONE:
3622 chunk_appendf(&trash,
3623 "<p><div class=active_going_down>"
3624 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
3625 "Nothing has changed."
3626 "</div>\n", uri->uri_prefix,
Willy Tarreau91cefca2022-05-03 17:08:29 +02003627 (ctx->flags & STAT_HIDE_DOWN) ? ";up" : "",
3628 (ctx->flags & STAT_NO_REFRESH) ? ";norefresh" : "",
William Lallemand74c24fb2016-11-21 17:18:36 +01003629 scope_txt);
3630 break;
3631 case STAT_STATUS_PART:
3632 chunk_appendf(&trash,
3633 "<p><div class=active_going_down>"
3634 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
3635 "Action partially processed.<br>"
3636 "Some server names are probably unknown or ambiguous (duplicated names in the backend)."
3637 "</div>\n", uri->uri_prefix,
Willy Tarreau91cefca2022-05-03 17:08:29 +02003638 (ctx->flags & STAT_HIDE_DOWN) ? ";up" : "",
3639 (ctx->flags & STAT_NO_REFRESH) ? ";norefresh" : "",
William Lallemand74c24fb2016-11-21 17:18:36 +01003640 scope_txt);
3641 break;
3642 case STAT_STATUS_ERRP:
3643 chunk_appendf(&trash,
3644 "<p><div class=active_down>"
3645 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
3646 "Action not processed because of invalid parameters."
3647 "<ul>"
3648 "<li>The action is maybe unknown.</li>"
Christopher Faulet2f9a41d2019-02-27 15:30:57 +01003649 "<li>Invalid key parameter (empty or too long).</li>"
William Lallemand74c24fb2016-11-21 17:18:36 +01003650 "<li>The backend name is probably unknown or ambiguous (duplicated names).</li>"
3651 "<li>Some server names are probably unknown or ambiguous (duplicated names in the backend).</li>"
3652 "</ul>"
3653 "</div>\n", uri->uri_prefix,
Willy Tarreau91cefca2022-05-03 17:08:29 +02003654 (ctx->flags & STAT_HIDE_DOWN) ? ";up" : "",
3655 (ctx->flags & STAT_NO_REFRESH) ? ";norefresh" : "",
William Lallemand74c24fb2016-11-21 17:18:36 +01003656 scope_txt);
3657 break;
3658 case STAT_STATUS_EXCD:
3659 chunk_appendf(&trash,
3660 "<p><div class=active_down>"
3661 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
3662 "<b>Action not processed : the buffer couldn't store all the data.<br>"
3663 "You should retry with less servers at a time.</b>"
3664 "</div>\n", uri->uri_prefix,
Willy Tarreau91cefca2022-05-03 17:08:29 +02003665 (ctx->flags & STAT_HIDE_DOWN) ? ";up" : "",
3666 (ctx->flags & STAT_NO_REFRESH) ? ";norefresh" : "",
William Lallemand74c24fb2016-11-21 17:18:36 +01003667 scope_txt);
3668 break;
3669 case STAT_STATUS_DENY:
3670 chunk_appendf(&trash,
3671 "<p><div class=active_down>"
3672 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
3673 "<b>Action denied.</b>"
3674 "</div>\n", uri->uri_prefix,
Willy Tarreau91cefca2022-05-03 17:08:29 +02003675 (ctx->flags & STAT_HIDE_DOWN) ? ";up" : "",
3676 (ctx->flags & STAT_NO_REFRESH) ? ";norefresh" : "",
William Lallemand74c24fb2016-11-21 17:18:36 +01003677 scope_txt);
3678 break;
Christopher Faulet3c2ecf72019-02-27 16:41:27 +01003679 case STAT_STATUS_IVAL:
3680 chunk_appendf(&trash,
3681 "<p><div class=active_down>"
3682 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
3683 "<b>Invalid requests (unsupported method or chunked encoded request).</b>"
3684 "</div>\n", uri->uri_prefix,
Willy Tarreau91cefca2022-05-03 17:08:29 +02003685 (ctx->flags & STAT_HIDE_DOWN) ? ";up" : "",
3686 (ctx->flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Christopher Faulet3c2ecf72019-02-27 16:41:27 +01003687 scope_txt);
3688 break;
William Lallemand74c24fb2016-11-21 17:18:36 +01003689 default:
3690 chunk_appendf(&trash,
3691 "<p><div class=active_no_check>"
3692 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
3693 "Unexpected result."
3694 "</div>\n", uri->uri_prefix,
Willy Tarreau91cefca2022-05-03 17:08:29 +02003695 (ctx->flags & STAT_HIDE_DOWN) ? ";up" : "",
3696 (ctx->flags & STAT_NO_REFRESH) ? ";norefresh" : "",
William Lallemand74c24fb2016-11-21 17:18:36 +01003697 scope_txt);
3698 }
3699 chunk_appendf(&trash, "<p>\n");
3700 }
3701}
3702
3703/* Dumps the HTML stats trailer block to the trash. The caller is responsible
3704 * for clearing the trash if needed.
3705 */
3706static void stats_dump_html_end()
3707{
3708 chunk_appendf(&trash, "</body></html>\n");
3709}
3710
Simon Horman05ee2132017-01-04 09:37:25 +01003711/* Dumps the stats JSON header to the trash buffer which. The caller is responsible
3712 * for clearing it if needed.
3713 */
3714static void stats_dump_json_header()
3715{
3716 chunk_strcat(&trash, "[");
3717}
3718
3719
3720/* Dumps the JSON stats trailer block to the trash. The caller is responsible
3721 * for clearing the trash if needed.
3722 */
3723static void stats_dump_json_end()
3724{
Willy Tarreau5a0e7ca2022-06-10 09:21:22 +02003725 chunk_strcat(&trash, "]\n");
Simon Horman05ee2132017-01-04 09:37:25 +01003726}
3727
Amaury Denoyelle98b81cb2020-10-02 18:32:04 +02003728/* Uses <appctx.ctx.stats.obj1> as a pointer to the current proxy and <obj2> as
3729 * a pointer to the current server/listener.
3730 */
Willy Tarreaucaff6312022-05-27 10:17:46 +02003731static int stats_dump_proxies(struct stconn *sc,
Amaury Denoyelle98b81cb2020-10-02 18:32:04 +02003732 struct htx *htx,
3733 struct uri_auth *uri)
3734{
Willy Tarreaucaff6312022-05-27 10:17:46 +02003735 struct appctx *appctx = __sc_appctx(sc);
Willy Tarreau91cefca2022-05-03 17:08:29 +02003736 struct show_stat_ctx *ctx = appctx->svcctx;
Willy Tarreaucaff6312022-05-27 10:17:46 +02003737 struct channel *rep = sc_ic(sc);
Amaury Denoyelle98b81cb2020-10-02 18:32:04 +02003738 struct proxy *px;
3739
3740 /* dump proxies */
Willy Tarreau91cefca2022-05-03 17:08:29 +02003741 while (ctx->obj1) {
Amaury Denoyelle98b81cb2020-10-02 18:32:04 +02003742 if (htx) {
3743 if (htx_almost_full(htx))
3744 goto full;
3745 }
3746 else {
3747 if (buffer_almost_full(&rep->buf))
3748 goto full;
3749 }
3750
Willy Tarreau91cefca2022-05-03 17:08:29 +02003751 px = ctx->obj1;
Marno Krahmer07954fb2021-06-24 16:51:08 +02003752 /* Skip the global frontend proxies and non-networked ones.
William Lallemand85a16b22021-08-03 13:18:11 +02003753 * Also skip proxies that were disabled in the configuration
Marno Krahmer07954fb2021-06-24 16:51:08 +02003754 * This change allows retrieving stats from "old" proxies after a reload.
3755 */
Christopher Fauletdfd10ab2021-10-06 14:24:19 +02003756 if (!(px->flags & PR_FL_DISABLED) && px->uuid > 0 &&
William Lallemande7f74622021-07-28 17:45:18 +02003757 (px->cap & (PR_CAP_FE | PR_CAP_BE)) && !(px->cap & PR_CAP_INT)) {
Willy Tarreaucaff6312022-05-27 10:17:46 +02003758 if (stats_dump_proxy_to_buffer(sc, htx, px, uri) == 0)
Amaury Denoyelle98b81cb2020-10-02 18:32:04 +02003759 return 0;
3760 }
3761
Willy Tarreau91cefca2022-05-03 17:08:29 +02003762 ctx->obj1 = px->next;
3763 ctx->px_st = STAT_PX_ST_INIT;
Aurelien DARRAGON55941842022-12-15 14:01:04 +01003764 ctx->field = 0;
Amaury Denoyelle98b81cb2020-10-02 18:32:04 +02003765 }
3766
3767 return 1;
3768
3769 full:
Willy Tarreaucaff6312022-05-27 10:17:46 +02003770 sc_need_room(sc);
Amaury Denoyelle98b81cb2020-10-02 18:32:04 +02003771 return 0;
3772}
3773
Willy Tarreau4596fe22022-05-17 19:07:51 +02003774/* This function dumps statistics onto the stream connector's read buffer in
William Lallemand74c24fb2016-11-21 17:18:36 +01003775 * either CSV or HTML format. <uri> contains some HTML-specific parameters that
3776 * are ignored for CSV format (hence <uri> may be NULL there). It returns 0 if
3777 * it had to stop writing data and an I/O is needed, 1 if the dump is finished
3778 * and the stream must be closed, or -1 in case of any error. This function is
3779 * used by both the CLI and the HTTP handlers.
3780 */
Willy Tarreaucaff6312022-05-27 10:17:46 +02003781static int stats_dump_stat_to_buffer(struct stconn *sc, struct htx *htx,
Christopher Fauletef779222018-10-31 08:47:01 +01003782 struct uri_auth *uri)
William Lallemand74c24fb2016-11-21 17:18:36 +01003783{
Willy Tarreaucaff6312022-05-27 10:17:46 +02003784 struct appctx *appctx = __sc_appctx(sc);
Willy Tarreau91cefca2022-05-03 17:08:29 +02003785 struct show_stat_ctx *ctx = appctx->svcctx;
Willy Tarreaucaff6312022-05-27 10:17:46 +02003786 struct channel *rep = sc_ic(sc);
Willy Tarreau91cefca2022-05-03 17:08:29 +02003787 enum stats_domain domain = ctx->domain;
William Lallemand74c24fb2016-11-21 17:18:36 +01003788
3789 chunk_reset(&trash);
3790
Willy Tarreau41f88522022-05-03 18:39:27 +02003791 switch (ctx->state) {
Willy Tarreau6ef16482022-05-06 18:07:53 +02003792 case STAT_STATE_INIT:
3793 ctx->state = STAT_STATE_HEAD; /* let's start producing data */
Willy Tarreau9eb93c02022-11-14 07:12:05 +01003794 __fallthrough;
William Lallemand74c24fb2016-11-21 17:18:36 +01003795
Willy Tarreau6ef16482022-05-06 18:07:53 +02003796 case STAT_STATE_HEAD:
Willy Tarreau91cefca2022-05-03 17:08:29 +02003797 if (ctx->flags & STAT_FMT_HTML)
Willy Tarreau676c29e2019-10-09 10:50:01 +02003798 stats_dump_html_head(appctx, uri);
Willy Tarreau91cefca2022-05-03 17:08:29 +02003799 else if (ctx->flags & STAT_JSON_SCHM)
Christopher Faulet6338a082019-09-09 15:50:54 +02003800 stats_dump_json_schema(&trash);
Willy Tarreau91cefca2022-05-03 17:08:29 +02003801 else if (ctx->flags & STAT_FMT_JSON)
Willy Tarreau9d7fb632017-04-11 07:53:04 +02003802 stats_dump_json_header();
Willy Tarreau91cefca2022-05-03 17:08:29 +02003803 else if (!(ctx->flags & STAT_FMT_TYPED))
3804 stats_dump_csv_header(ctx->domain);
William Lallemand74c24fb2016-11-21 17:18:36 +01003805
Christopher Fauletef779222018-10-31 08:47:01 +01003806 if (!stats_putchk(rep, htx, &trash))
3807 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01003808
Willy Tarreau91cefca2022-05-03 17:08:29 +02003809 if (ctx->flags & STAT_JSON_SCHM) {
Willy Tarreau6ef16482022-05-06 18:07:53 +02003810 ctx->state = STAT_STATE_FIN;
Christopher Faulet6338a082019-09-09 15:50:54 +02003811 return 1;
3812 }
Willy Tarreau6ef16482022-05-06 18:07:53 +02003813 ctx->state = STAT_STATE_INFO;
Willy Tarreau9eb93c02022-11-14 07:12:05 +01003814 __fallthrough;
William Lallemand74c24fb2016-11-21 17:18:36 +01003815
Willy Tarreau6ef16482022-05-06 18:07:53 +02003816 case STAT_STATE_INFO:
Willy Tarreau91cefca2022-05-03 17:08:29 +02003817 if (ctx->flags & STAT_FMT_HTML) {
Willy Tarreaucaff6312022-05-27 10:17:46 +02003818 stats_dump_html_info(sc, uri);
Christopher Fauletef779222018-10-31 08:47:01 +01003819 if (!stats_putchk(rep, htx, &trash))
3820 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01003821 }
3822
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02003823 if (domain == STATS_DOMAIN_PROXY)
Willy Tarreau91cefca2022-05-03 17:08:29 +02003824 ctx->obj1 = proxies_list;
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02003825
Willy Tarreau91cefca2022-05-03 17:08:29 +02003826 ctx->px_st = STAT_PX_ST_INIT;
Aurelien DARRAGON55941842022-12-15 14:01:04 +01003827 ctx->field = 0;
Willy Tarreau6ef16482022-05-06 18:07:53 +02003828 ctx->state = STAT_STATE_LIST;
Willy Tarreau9eb93c02022-11-14 07:12:05 +01003829 __fallthrough;
William Lallemand74c24fb2016-11-21 17:18:36 +01003830
Willy Tarreau6ef16482022-05-06 18:07:53 +02003831 case STAT_STATE_LIST:
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02003832 switch (domain) {
Emeric Brunf8642ee2021-10-29 17:59:18 +02003833 case STATS_DOMAIN_RESOLVERS:
Willy Tarreaucaff6312022-05-27 10:17:46 +02003834 if (!stats_dump_resolvers(sc, stat_l[domain],
Emeric Brund3b44952021-01-06 16:01:02 +01003835 stat_count[domain],
3836 &stats_module_list[domain])) {
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02003837 return 0;
3838 }
3839 break;
3840
3841 case STATS_DOMAIN_PROXY:
3842 default:
3843 /* dump proxies */
Willy Tarreaucaff6312022-05-27 10:17:46 +02003844 if (!stats_dump_proxies(sc, htx, uri))
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02003845 return 0;
3846 break;
3847 }
William Lallemand74c24fb2016-11-21 17:18:36 +01003848
Willy Tarreau6ef16482022-05-06 18:07:53 +02003849 ctx->state = STAT_STATE_END;
Willy Tarreau9eb93c02022-11-14 07:12:05 +01003850 __fallthrough;
William Lallemand74c24fb2016-11-21 17:18:36 +01003851
Willy Tarreau6ef16482022-05-06 18:07:53 +02003852 case STAT_STATE_END:
Willy Tarreau91cefca2022-05-03 17:08:29 +02003853 if (ctx->flags & (STAT_FMT_HTML|STAT_FMT_JSON)) {
3854 if (ctx->flags & STAT_FMT_HTML)
Simon Horman05ee2132017-01-04 09:37:25 +01003855 stats_dump_html_end();
3856 else
3857 stats_dump_json_end();
Christopher Fauletef779222018-10-31 08:47:01 +01003858 if (!stats_putchk(rep, htx, &trash))
3859 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01003860 }
3861
Willy Tarreau6ef16482022-05-06 18:07:53 +02003862 ctx->state = STAT_STATE_FIN;
Willy Tarreau9eb93c02022-11-14 07:12:05 +01003863 __fallthrough;
William Lallemand74c24fb2016-11-21 17:18:36 +01003864
Willy Tarreau6ef16482022-05-06 18:07:53 +02003865 case STAT_STATE_FIN:
William Lallemand74c24fb2016-11-21 17:18:36 +01003866 return 1;
3867
3868 default:
3869 /* unknown state ! */
Willy Tarreau6ef16482022-05-06 18:07:53 +02003870 ctx->state = STAT_STATE_FIN;
William Lallemand74c24fb2016-11-21 17:18:36 +01003871 return -1;
3872 }
Christopher Fauletef779222018-10-31 08:47:01 +01003873
3874 full:
Willy Tarreaucaff6312022-05-27 10:17:46 +02003875 sc_need_room(sc);
Christopher Fauletef779222018-10-31 08:47:01 +01003876 return 0;
3877
William Lallemand74c24fb2016-11-21 17:18:36 +01003878}
3879
3880/* We reached the stats page through a POST request. The appctx is
3881 * expected to have already been allocated by the caller.
3882 * Parse the posted data and enable/disable servers if necessary.
3883 * Returns 1 if request was parsed or zero if it needs more data.
3884 */
Willy Tarreaucaff6312022-05-27 10:17:46 +02003885static int stats_process_http_post(struct stconn *sc)
William Lallemand74c24fb2016-11-21 17:18:36 +01003886{
Willy Tarreaucaff6312022-05-27 10:17:46 +02003887 struct stream *s = __sc_strm(sc);
3888 struct appctx *appctx = __sc_appctx(sc);
Willy Tarreau91cefca2022-05-03 17:08:29 +02003889 struct show_stat_ctx *ctx = appctx->svcctx;
William Lallemand74c24fb2016-11-21 17:18:36 +01003890
3891 struct proxy *px = NULL;
3892 struct server *sv = NULL;
3893
3894 char key[LINESIZE];
3895 int action = ST_ADM_ACTION_NONE;
3896 int reprocess = 0;
3897
3898 int total_servers = 0;
3899 int altered_servers = 0;
3900
3901 char *first_param, *cur_param, *next_param, *end_params;
3902 char *st_cur_param = NULL;
3903 char *st_next_param = NULL;
3904
Christopher Fauleta3618372018-12-13 21:59:56 +01003905 struct buffer *temp = get_trash_chunk();
William Lallemand74c24fb2016-11-21 17:18:36 +01003906
Christopher Fauletb7f88902019-07-15 21:56:43 +02003907 struct htx *htx = htxbuf(&s->req.buf);
3908 struct htx_blk *blk;
Christopher Fauleta3618372018-12-13 21:59:56 +01003909
Christopher Fauletb7f88902019-07-15 21:56:43 +02003910 /* we need more data */
3911 if (s->txn->req.msg_state < HTTP_MSG_DONE) {
3912 /* check if we can receive more */
3913 if (htx_free_data_space(htx) <= global.tune.maxrewrite) {
Willy Tarreau91cefca2022-05-03 17:08:29 +02003914 ctx->st_code = STAT_STATUS_EXCD;
Christopher Fauletb7f88902019-07-15 21:56:43 +02003915 goto out;
Christopher Fauleta3618372018-12-13 21:59:56 +01003916 }
Christopher Fauletb7f88902019-07-15 21:56:43 +02003917 goto wait;
3918 }
Christopher Fauleta3618372018-12-13 21:59:56 +01003919
Christopher Fauletb7f88902019-07-15 21:56:43 +02003920 /* The request was fully received. Copy data */
3921 blk = htx_get_head_blk(htx);
3922 while (blk) {
3923 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Fauleta3618372018-12-13 21:59:56 +01003924
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01003925 if (type == HTX_BLK_TLR || type == HTX_BLK_EOT)
Christopher Fauletb7f88902019-07-15 21:56:43 +02003926 break;
3927 if (type == HTX_BLK_DATA) {
3928 struct ist v = htx_get_blk_value(htx, blk);
Christopher Fauleta3618372018-12-13 21:59:56 +01003929
Christopher Fauletb7f88902019-07-15 21:56:43 +02003930 if (!chunk_memcat(temp, v.ptr, v.len)) {
Willy Tarreau91cefca2022-05-03 17:08:29 +02003931 ctx->st_code = STAT_STATUS_EXCD;
Christopher Faulet2f9a41d2019-02-27 15:30:57 +01003932 goto out;
3933 }
Christopher Fauleta3618372018-12-13 21:59:56 +01003934 }
Christopher Fauletb7f88902019-07-15 21:56:43 +02003935 blk = htx_get_next_blk(htx, blk);
William Lallemand74c24fb2016-11-21 17:18:36 +01003936 }
3937
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003938 first_param = temp->area;
Christopher Fauleta3618372018-12-13 21:59:56 +01003939 end_params = temp->area + temp->data;
William Lallemand74c24fb2016-11-21 17:18:36 +01003940 cur_param = next_param = end_params;
3941 *end_params = '\0';
3942
Willy Tarreau91cefca2022-05-03 17:08:29 +02003943 ctx->st_code = STAT_STATUS_NONE;
William Lallemand74c24fb2016-11-21 17:18:36 +01003944
3945 /*
3946 * Parse the parameters in reverse order to only store the last value.
3947 * From the html form, the backend and the action are at the end.
3948 */
3949 while (cur_param > first_param) {
3950 char *value;
3951 int poffset, plen;
3952
3953 cur_param--;
3954
3955 if ((*cur_param == '&') || (cur_param == first_param)) {
3956 reprocess_servers:
3957 /* Parse the key */
3958 poffset = (cur_param != first_param ? 1 : 0);
3959 plen = next_param - cur_param + (cur_param == first_param ? 1 : 0);
3960 if ((plen > 0) && (plen <= sizeof(key))) {
3961 strncpy(key, cur_param + poffset, plen);
3962 key[plen - 1] = '\0';
3963 } else {
Willy Tarreau91cefca2022-05-03 17:08:29 +02003964 ctx->st_code = STAT_STATUS_ERRP;
William Lallemand74c24fb2016-11-21 17:18:36 +01003965 goto out;
3966 }
3967
3968 /* Parse the value */
3969 value = key;
3970 while (*value != '\0' && *value != '=') {
3971 value++;
3972 }
3973 if (*value == '=') {
3974 /* Ok, a value is found, we can mark the end of the key */
3975 *value++ = '\0';
3976 }
Willy Tarreau62ba9ba2020-04-23 17:54:47 +02003977 if (url_decode(key, 1) < 0 || url_decode(value, 1) < 0)
William Lallemand74c24fb2016-11-21 17:18:36 +01003978 break;
3979
3980 /* Now we can check the key to see what to do */
3981 if (!px && (strcmp(key, "b") == 0)) {
3982 if ((px = proxy_be_by_name(value)) == NULL) {
3983 /* the backend name is unknown or ambiguous (duplicate names) */
Willy Tarreau91cefca2022-05-03 17:08:29 +02003984 ctx->st_code = STAT_STATUS_ERRP;
William Lallemand74c24fb2016-11-21 17:18:36 +01003985 goto out;
3986 }
3987 }
3988 else if (!action && (strcmp(key, "action") == 0)) {
3989 if (strcmp(value, "ready") == 0) {
3990 action = ST_ADM_ACTION_READY;
3991 }
3992 else if (strcmp(value, "drain") == 0) {
3993 action = ST_ADM_ACTION_DRAIN;
3994 }
3995 else if (strcmp(value, "maint") == 0) {
3996 action = ST_ADM_ACTION_MAINT;
3997 }
3998 else if (strcmp(value, "shutdown") == 0) {
3999 action = ST_ADM_ACTION_SHUTDOWN;
4000 }
4001 else if (strcmp(value, "dhlth") == 0) {
4002 action = ST_ADM_ACTION_DHLTH;
4003 }
4004 else if (strcmp(value, "ehlth") == 0) {
4005 action = ST_ADM_ACTION_EHLTH;
4006 }
4007 else if (strcmp(value, "hrunn") == 0) {
4008 action = ST_ADM_ACTION_HRUNN;
4009 }
4010 else if (strcmp(value, "hnolb") == 0) {
4011 action = ST_ADM_ACTION_HNOLB;
4012 }
4013 else if (strcmp(value, "hdown") == 0) {
4014 action = ST_ADM_ACTION_HDOWN;
4015 }
4016 else if (strcmp(value, "dagent") == 0) {
4017 action = ST_ADM_ACTION_DAGENT;
4018 }
4019 else if (strcmp(value, "eagent") == 0) {
4020 action = ST_ADM_ACTION_EAGENT;
4021 }
4022 else if (strcmp(value, "arunn") == 0) {
4023 action = ST_ADM_ACTION_ARUNN;
4024 }
4025 else if (strcmp(value, "adown") == 0) {
4026 action = ST_ADM_ACTION_ADOWN;
4027 }
4028 /* else these are the old supported methods */
4029 else if (strcmp(value, "disable") == 0) {
4030 action = ST_ADM_ACTION_DISABLE;
4031 }
4032 else if (strcmp(value, "enable") == 0) {
4033 action = ST_ADM_ACTION_ENABLE;
4034 }
4035 else if (strcmp(value, "stop") == 0) {
4036 action = ST_ADM_ACTION_STOP;
4037 }
4038 else if (strcmp(value, "start") == 0) {
4039 action = ST_ADM_ACTION_START;
4040 }
4041 else {
Willy Tarreau91cefca2022-05-03 17:08:29 +02004042 ctx->st_code = STAT_STATUS_ERRP;
William Lallemand74c24fb2016-11-21 17:18:36 +01004043 goto out;
4044 }
4045 }
4046 else if (strcmp(key, "s") == 0) {
4047 if (!(px && action)) {
4048 /*
4049 * Indicates that we'll need to reprocess the parameters
4050 * as soon as backend and action are known
4051 */
4052 if (!reprocess) {
4053 st_cur_param = cur_param;
4054 st_next_param = next_param;
4055 }
4056 reprocess = 1;
4057 }
4058 else if ((sv = findserver(px, value)) != NULL) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004059 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
William Lallemand74c24fb2016-11-21 17:18:36 +01004060 switch (action) {
4061 case ST_ADM_ACTION_DISABLE:
Emeric Brun52a91d32017-08-31 14:41:55 +02004062 if (!(sv->cur_admin & SRV_ADMF_FMAINT)) {
William Lallemand74c24fb2016-11-21 17:18:36 +01004063 altered_servers++;
4064 total_servers++;
4065 srv_set_admin_flag(sv, SRV_ADMF_FMAINT, "'disable' on stats page");
4066 }
4067 break;
4068 case ST_ADM_ACTION_ENABLE:
Emeric Brun52a91d32017-08-31 14:41:55 +02004069 if (sv->cur_admin & SRV_ADMF_FMAINT) {
William Lallemand74c24fb2016-11-21 17:18:36 +01004070 altered_servers++;
4071 total_servers++;
4072 srv_clr_admin_flag(sv, SRV_ADMF_FMAINT);
4073 }
4074 break;
4075 case ST_ADM_ACTION_STOP:
Emeric Brun52a91d32017-08-31 14:41:55 +02004076 if (!(sv->cur_admin & SRV_ADMF_FDRAIN)) {
William Lallemand74c24fb2016-11-21 17:18:36 +01004077 srv_set_admin_flag(sv, SRV_ADMF_FDRAIN, "'stop' on stats page");
4078 altered_servers++;
4079 total_servers++;
4080 }
4081 break;
4082 case ST_ADM_ACTION_START:
Emeric Brun52a91d32017-08-31 14:41:55 +02004083 if (sv->cur_admin & SRV_ADMF_FDRAIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +01004084 srv_clr_admin_flag(sv, SRV_ADMF_FDRAIN);
4085 altered_servers++;
4086 total_servers++;
4087 }
4088 break;
4089 case ST_ADM_ACTION_DHLTH:
4090 if (sv->check.state & CHK_ST_CONFIGURED) {
4091 sv->check.state &= ~CHK_ST_ENABLED;
4092 altered_servers++;
4093 total_servers++;
4094 }
4095 break;
4096 case ST_ADM_ACTION_EHLTH:
4097 if (sv->check.state & CHK_ST_CONFIGURED) {
4098 sv->check.state |= CHK_ST_ENABLED;
4099 altered_servers++;
4100 total_servers++;
4101 }
4102 break;
4103 case ST_ADM_ACTION_HRUNN:
4104 if (!(sv->track)) {
4105 sv->check.health = sv->check.rise + sv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02004106 srv_set_running(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01004107 altered_servers++;
4108 total_servers++;
4109 }
4110 break;
4111 case ST_ADM_ACTION_HNOLB:
4112 if (!(sv->track)) {
4113 sv->check.health = sv->check.rise + sv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02004114 srv_set_stopping(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01004115 altered_servers++;
4116 total_servers++;
4117 }
4118 break;
4119 case ST_ADM_ACTION_HDOWN:
4120 if (!(sv->track)) {
4121 sv->check.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02004122 srv_set_stopped(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01004123 altered_servers++;
4124 total_servers++;
4125 }
4126 break;
4127 case ST_ADM_ACTION_DAGENT:
4128 if (sv->agent.state & CHK_ST_CONFIGURED) {
4129 sv->agent.state &= ~CHK_ST_ENABLED;
4130 altered_servers++;
4131 total_servers++;
4132 }
4133 break;
4134 case ST_ADM_ACTION_EAGENT:
4135 if (sv->agent.state & CHK_ST_CONFIGURED) {
4136 sv->agent.state |= CHK_ST_ENABLED;
4137 altered_servers++;
4138 total_servers++;
4139 }
4140 break;
4141 case ST_ADM_ACTION_ARUNN:
4142 if (sv->agent.state & CHK_ST_ENABLED) {
4143 sv->agent.health = sv->agent.rise + sv->agent.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02004144 srv_set_running(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01004145 altered_servers++;
4146 total_servers++;
4147 }
4148 break;
4149 case ST_ADM_ACTION_ADOWN:
4150 if (sv->agent.state & CHK_ST_ENABLED) {
4151 sv->agent.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02004152 srv_set_stopped(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01004153 altered_servers++;
4154 total_servers++;
4155 }
4156 break;
4157 case ST_ADM_ACTION_READY:
4158 srv_adm_set_ready(sv);
4159 altered_servers++;
4160 total_servers++;
4161 break;
4162 case ST_ADM_ACTION_DRAIN:
4163 srv_adm_set_drain(sv);
4164 altered_servers++;
4165 total_servers++;
4166 break;
4167 case ST_ADM_ACTION_MAINT:
4168 srv_adm_set_maint(sv);
4169 altered_servers++;
4170 total_servers++;
4171 break;
4172 case ST_ADM_ACTION_SHUTDOWN:
Christopher Fauletdfd10ab2021-10-06 14:24:19 +02004173 if (!(px->flags & (PR_FL_DISABLED|PR_FL_STOPPED))) {
Willy Tarreauaf7ea812019-11-14 16:42:04 +01004174 srv_shutdown_streams(sv, SF_ERR_KILLED);
William Lallemand74c24fb2016-11-21 17:18:36 +01004175 altered_servers++;
4176 total_servers++;
4177 }
4178 break;
4179 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004180 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
William Lallemand74c24fb2016-11-21 17:18:36 +01004181 } else {
4182 /* the server name is unknown or ambiguous (duplicate names) */
4183 total_servers++;
4184 }
4185 }
4186 if (reprocess && px && action) {
4187 /* Now, we know the backend and the action chosen by the user.
4188 * We can safely restart from the first server parameter
4189 * to reprocess them
4190 */
4191 cur_param = st_cur_param;
4192 next_param = st_next_param;
4193 reprocess = 0;
4194 goto reprocess_servers;
4195 }
4196
4197 next_param = cur_param;
4198 }
4199 }
4200
4201 if (total_servers == 0) {
Willy Tarreau91cefca2022-05-03 17:08:29 +02004202 ctx->st_code = STAT_STATUS_NONE;
William Lallemand74c24fb2016-11-21 17:18:36 +01004203 }
4204 else if (altered_servers == 0) {
Willy Tarreau91cefca2022-05-03 17:08:29 +02004205 ctx->st_code = STAT_STATUS_ERRP;
William Lallemand74c24fb2016-11-21 17:18:36 +01004206 }
4207 else if (altered_servers == total_servers) {
Willy Tarreau91cefca2022-05-03 17:08:29 +02004208 ctx->st_code = STAT_STATUS_DONE;
William Lallemand74c24fb2016-11-21 17:18:36 +01004209 }
4210 else {
Willy Tarreau91cefca2022-05-03 17:08:29 +02004211 ctx->st_code = STAT_STATUS_PART;
William Lallemand74c24fb2016-11-21 17:18:36 +01004212 }
4213 out:
4214 return 1;
Christopher Faulet2f9a41d2019-02-27 15:30:57 +01004215 wait:
Willy Tarreau91cefca2022-05-03 17:08:29 +02004216 ctx->st_code = STAT_STATUS_NONE;
Christopher Faulet2f9a41d2019-02-27 15:30:57 +01004217 return 0;
Christopher Fauletef779222018-10-31 08:47:01 +01004218}
4219
4220
Willy Tarreaucaff6312022-05-27 10:17:46 +02004221static int stats_send_http_headers(struct stconn *sc, struct htx *htx)
Christopher Fauletef779222018-10-31 08:47:01 +01004222{
Willy Tarreaucaff6312022-05-27 10:17:46 +02004223 struct stream *s = __sc_strm(sc);
Christopher Fauletef779222018-10-31 08:47:01 +01004224 struct uri_auth *uri = s->be->uri_auth;
Willy Tarreaucaff6312022-05-27 10:17:46 +02004225 struct appctx *appctx = __sc_appctx(sc);
Willy Tarreau91cefca2022-05-03 17:08:29 +02004226 struct show_stat_ctx *ctx = appctx->svcctx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004227 struct htx_sl *sl;
4228 unsigned int flags;
Christopher Fauletef779222018-10-31 08:47:01 +01004229
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004230 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);
4231 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), ist("200"), ist("OK"));
4232 if (!sl)
Christopher Fauletef779222018-10-31 08:47:01 +01004233 goto full;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004234 sl->info.res.status = 200;
Christopher Fauletef779222018-10-31 08:47:01 +01004235
Christopher Fauletb829f4c2019-03-29 16:13:55 +01004236 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")))
Christopher Fauletef779222018-10-31 08:47:01 +01004237 goto full;
Willy Tarreau91cefca2022-05-03 17:08:29 +02004238 if (ctx->flags & STAT_FMT_HTML) {
Christopher Fauletef779222018-10-31 08:47:01 +01004239 if (!htx_add_header(htx, ist("Content-Type"), ist("text/html")))
4240 goto full;
4241 }
Willy Tarreau91cefca2022-05-03 17:08:29 +02004242 else if (ctx->flags & (STAT_FMT_JSON|STAT_JSON_SCHM)) {
Christopher Faulet6338a082019-09-09 15:50:54 +02004243 if (!htx_add_header(htx, ist("Content-Type"), ist("application/json")))
4244 goto full;
4245 }
Christopher Fauletef779222018-10-31 08:47:01 +01004246 else {
4247 if (!htx_add_header(htx, ist("Content-Type"), ist("text/plain")))
4248 goto full;
4249 }
4250
Willy Tarreau91cefca2022-05-03 17:08:29 +02004251 if (uri->refresh > 0 && !(ctx->flags & STAT_NO_REFRESH)) {
Christopher Fauletef779222018-10-31 08:47:01 +01004252 const char *refresh = U2A(uri->refresh);
Tim Duesterhusdcf753a2021-03-04 17:31:47 +01004253 if (!htx_add_header(htx, ist("Refresh"), ist(refresh)))
Christopher Fauletef779222018-10-31 08:47:01 +01004254 goto full;
4255 }
4256
Willy Tarreau91cefca2022-05-03 17:08:29 +02004257 if (ctx->flags & STAT_CHUNKED) {
Christopher Fauletef779222018-10-31 08:47:01 +01004258 if (!htx_add_header(htx, ist("Transfer-Encoding"), ist("chunked")))
4259 goto full;
4260 }
4261
4262 if (!htx_add_endof(htx, HTX_BLK_EOH))
4263 goto full;
4264
Christopher Faulet1e2d6362019-02-27 16:28:48 +01004265 channel_add_input(&s->res, htx->data);
Christopher Fauletef779222018-10-31 08:47:01 +01004266 return 1;
4267
4268 full:
4269 htx_reset(htx);
Willy Tarreaucaff6312022-05-27 10:17:46 +02004270 sc_need_room(sc);
Christopher Fauletef779222018-10-31 08:47:01 +01004271 return 0;
William Lallemand74c24fb2016-11-21 17:18:36 +01004272}
4273
Christopher Fauletef779222018-10-31 08:47:01 +01004274
Willy Tarreaucaff6312022-05-27 10:17:46 +02004275static int stats_send_http_redirect(struct stconn *sc, struct htx *htx)
Christopher Fauletef779222018-10-31 08:47:01 +01004276{
4277 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
Willy Tarreaucaff6312022-05-27 10:17:46 +02004278 struct stream *s = __sc_strm(sc);
Christopher Fauletef779222018-10-31 08:47:01 +01004279 struct uri_auth *uri = s->be->uri_auth;
Willy Tarreaucaff6312022-05-27 10:17:46 +02004280 struct appctx *appctx = __sc_appctx(sc);
Willy Tarreau91cefca2022-05-03 17:08:29 +02004281 struct show_stat_ctx *ctx = appctx->svcctx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004282 struct htx_sl *sl;
4283 unsigned int flags;
Christopher Fauletef779222018-10-31 08:47:01 +01004284
Willy Tarreau91cefca2022-05-03 17:08:29 +02004285 /* scope_txt = search pattern + search query, ctx->scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
Christopher Fauletef779222018-10-31 08:47:01 +01004286 scope_txt[0] = 0;
Willy Tarreau91cefca2022-05-03 17:08:29 +02004287 if (ctx->scope_len) {
Willy Tarreaucaff6312022-05-27 10:17:46 +02004288 const char *scope_ptr = stats_scope_ptr(appctx, sc);
Christopher Fauleted7a0662019-01-14 11:07:34 +01004289
Christopher Fauletef779222018-10-31 08:47:01 +01004290 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Willy Tarreau91cefca2022-05-03 17:08:29 +02004291 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), scope_ptr, ctx->scope_len);
4292 scope_txt[strlen(STAT_SCOPE_PATTERN) + ctx->scope_len] = 0;
Christopher Fauletef779222018-10-31 08:47:01 +01004293 }
4294
4295 /* We don't want to land on the posted stats page because a refresh will
4296 * repost the data. We don't want this to happen on accident so we redirect
4297 * the browse to the stats page with a GET.
4298 */
4299 chunk_printf(&trash, "%s;st=%s%s%s%s",
4300 uri->uri_prefix,
Willy Tarreau91cefca2022-05-03 17:08:29 +02004301 ((ctx->st_code > STAT_STATUS_INIT) &&
4302 (ctx->st_code < STAT_STATUS_SIZE) &&
4303 stat_status_codes[ctx->st_code]) ?
4304 stat_status_codes[ctx->st_code] :
Christopher Fauletef779222018-10-31 08:47:01 +01004305 stat_status_codes[STAT_STATUS_UNKN],
Willy Tarreau91cefca2022-05-03 17:08:29 +02004306 (ctx->flags & STAT_HIDE_DOWN) ? ";up" : "",
4307 (ctx->flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Christopher Fauletef779222018-10-31 08:47:01 +01004308 scope_txt);
4309
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004310 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_CHNK);
4311 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), ist("303"), ist("See Other"));
4312 if (!sl)
Christopher Fauletef779222018-10-31 08:47:01 +01004313 goto full;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004314 sl->info.res.status = 303;
Christopher Fauletef779222018-10-31 08:47:01 +01004315
4316 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")) ||
Christopher Fauletef779222018-10-31 08:47:01 +01004317 !htx_add_header(htx, ist("Content-Type"), ist("text/plain")) ||
4318 !htx_add_header(htx, ist("Content-Length"), ist("0")) ||
4319 !htx_add_header(htx, ist("Location"), ist2(trash.area, trash.data)))
4320 goto full;
4321
4322 if (!htx_add_endof(htx, HTX_BLK_EOH))
4323 goto full;
4324
Christopher Faulet1e2d6362019-02-27 16:28:48 +01004325 channel_add_input(&s->res, htx->data);
Christopher Fauletef779222018-10-31 08:47:01 +01004326 return 1;
4327
4328full:
4329 htx_reset(htx);
Willy Tarreaucaff6312022-05-27 10:17:46 +02004330 sc_need_room(sc);
Christopher Fauletef779222018-10-31 08:47:01 +01004331 return 0;
4332}
William Lallemand74c24fb2016-11-21 17:18:36 +01004333
Simon Horman05ee2132017-01-04 09:37:25 +01004334
Willy Tarreau4596fe22022-05-17 19:07:51 +02004335/* This I/O handler runs as an applet embedded in a stream connector. It is
William Lallemand74c24fb2016-11-21 17:18:36 +01004336 * used to send HTTP stats over a TCP socket. The mechanism is very simple.
4337 * appctx->st0 contains the operation in progress (dump, done). The handler
4338 * automatically unregisters itself once transfer is complete.
4339 */
Christopher Fauletb7f88902019-07-15 21:56:43 +02004340static void http_stats_io_handler(struct appctx *appctx)
Christopher Fauletef779222018-10-31 08:47:01 +01004341{
Willy Tarreau91cefca2022-05-03 17:08:29 +02004342 struct show_stat_ctx *ctx = appctx->svcctx;
Willy Tarreauc12b3212022-05-27 11:08:15 +02004343 struct stconn *sc = appctx_sc(appctx);
Willy Tarreaucaff6312022-05-27 10:17:46 +02004344 struct stream *s = __sc_strm(sc);
4345 struct channel *req = sc_oc(sc);
4346 struct channel *res = sc_ic(sc);
Christopher Fauletef779222018-10-31 08:47:01 +01004347 struct htx *req_htx, *res_htx;
4348
Amaury Denoyelle072f97e2020-10-05 11:49:37 +02004349 /* only proxy stats are available via http */
Willy Tarreau91cefca2022-05-03 17:08:29 +02004350 ctx->domain = STATS_DOMAIN_PROXY;
Amaury Denoyelle072f97e2020-10-05 11:49:37 +02004351
Christopher Fauletef779222018-10-31 08:47:01 +01004352 res_htx = htx_from_buf(&res->buf);
4353
Willy Tarreaucaff6312022-05-27 10:17:46 +02004354 if (unlikely(sc->state == SC_ST_DIS || sc->state == SC_ST_CLO))
Christopher Fauletef779222018-10-31 08:47:01 +01004355 goto out;
4356
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05004357 /* Check if the input buffer is available. */
Christopher Fauletef779222018-10-31 08:47:01 +01004358 if (!b_size(&res->buf)) {
Willy Tarreaucaff6312022-05-27 10:17:46 +02004359 sc_need_room(sc);
Christopher Fauletef779222018-10-31 08:47:01 +01004360 goto out;
4361 }
4362
4363 /* check that the output is not closed */
Christopher Faulet3a78aa62019-02-27 16:19:48 +01004364 if (res->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_SHUTR))
4365 appctx->st0 = STAT_HTTP_END;
Christopher Fauletef779222018-10-31 08:47:01 +01004366
4367 /* all states are processed in sequence */
4368 if (appctx->st0 == STAT_HTTP_HEAD) {
Willy Tarreaucaff6312022-05-27 10:17:46 +02004369 if (stats_send_http_headers(sc, res_htx)) {
Christopher Fauletef779222018-10-31 08:47:01 +01004370 if (s->txn->meth == HTTP_METH_HEAD)
4371 appctx->st0 = STAT_HTTP_DONE;
4372 else
4373 appctx->st0 = STAT_HTTP_DUMP;
4374 }
4375 }
4376
4377 if (appctx->st0 == STAT_HTTP_DUMP) {
Willy Tarreaucaff6312022-05-27 10:17:46 +02004378 if (stats_dump_stat_to_buffer(sc, res_htx, s->be->uri_auth))
Christopher Fauletef779222018-10-31 08:47:01 +01004379 appctx->st0 = STAT_HTTP_DONE;
4380 }
4381
4382 if (appctx->st0 == STAT_HTTP_POST) {
Willy Tarreaucaff6312022-05-27 10:17:46 +02004383 if (stats_process_http_post(sc))
Christopher Fauletef779222018-10-31 08:47:01 +01004384 appctx->st0 = STAT_HTTP_LAST;
Christopher Faulet3a78aa62019-02-27 16:19:48 +01004385 else if (req->flags & CF_SHUTR)
Christopher Fauletef779222018-10-31 08:47:01 +01004386 appctx->st0 = STAT_HTTP_DONE;
4387 }
4388
4389 if (appctx->st0 == STAT_HTTP_LAST) {
Willy Tarreaucaff6312022-05-27 10:17:46 +02004390 if (stats_send_http_redirect(sc, res_htx))
Christopher Fauletef779222018-10-31 08:47:01 +01004391 appctx->st0 = STAT_HTTP_DONE;
4392 }
4393
4394 if (appctx->st0 == STAT_HTTP_DONE) {
Christopher Faulet08b45cb2022-04-07 09:25:13 +02004395 /* no more data are expected. If the response buffer is empty,
4396 * be sure to add something (EOT block in this case) to have
4397 * something to send. It is important to be sure the EOM flags
4398 * will be handled by the endpoint.
4399 */
4400 if (htx_is_empty(res_htx)) {
4401 if (!htx_add_endof(res_htx, HTX_BLK_EOT)) {
Willy Tarreaucaff6312022-05-27 10:17:46 +02004402 sc_need_room(sc);
Christopher Faulet08b45cb2022-04-07 09:25:13 +02004403 goto out;
4404 }
4405 channel_add_input(res, 1);
4406 }
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01004407 res_htx->flags |= HTX_FL_EOM;
Christopher Faulet3fa5d192022-03-07 15:52:42 +01004408 res->flags |= CF_EOI;
Willy Tarreaud869e132022-05-17 18:05:31 +02004409 se_fl_set(appctx->sedesc, SE_FL_EOI);
Christopher Faulet3a78aa62019-02-27 16:19:48 +01004410 appctx->st0 = STAT_HTTP_END;
Christopher Fauletef779222018-10-31 08:47:01 +01004411 }
4412
Christopher Faulet3a78aa62019-02-27 16:19:48 +01004413 if (appctx->st0 == STAT_HTTP_END) {
4414 if (!(res->flags & CF_SHUTR)) {
Christopher Fauletef779222018-10-31 08:47:01 +01004415 res->flags |= CF_READ_NULL;
Willy Tarreaucaff6312022-05-27 10:17:46 +02004416 sc_shutr(sc);
Christopher Faulet3a78aa62019-02-27 16:19:48 +01004417 }
4418
4419 /* eat the whole request */
4420 if (co_data(req)) {
4421 req_htx = htx_from_buf(&req->buf);
4422 co_htx_skip(req, req_htx, co_data(req));
4423 htx_to_buf(req_htx, &req->buf);
Christopher Fauletef779222018-10-31 08:47:01 +01004424 }
4425 }
Christopher Faulet3a78aa62019-02-27 16:19:48 +01004426
Christopher Fauletef779222018-10-31 08:47:01 +01004427 out:
4428 /* we have left the request in the buffer for the case where we
4429 * process a POST, and this automatically re-enables activity on
4430 * read. It's better to indicate that we want to stop reading when
4431 * we're sending, so that we know there's at most one direction
4432 * deciding to wake the applet up. It saves it from looping when
4433 * emitting large blocks into small TCP windows.
4434 */
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01004435 htx_to_buf(res_htx, &res->buf);
4436 if (!channel_is_empty(res))
Willy Tarreau75a8f8e2022-05-25 18:12:11 +02004437 applet_wont_consume(appctx);
Christopher Fauletef779222018-10-31 08:47:01 +01004438}
4439
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004440/* Dump all fields from <info> into <out> using the "show info" format (name: value) */
Willy Tarreau83061a82018-07-13 11:56:34 +02004441static int stats_dump_info_fields(struct buffer *out,
Aurelien DARRAGONe76a0272022-12-15 12:10:03 +01004442 const struct field *info,
4443 struct show_stat_ctx *ctx)
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004444{
Aurelien DARRAGONe76a0272022-12-15 12:10:03 +01004445 int flags = ctx->flags;
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004446 int field;
4447
4448 for (field = 0; field < INF_TOTAL_FIELDS; field++) {
4449 if (!field_format(info, field))
4450 continue;
4451
Willy Tarreaueaa55372019-10-09 07:39:11 +02004452 if (!chunk_appendf(out, "%s: ", info_fields[field].name))
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004453 return 0;
4454 if (!stats_emit_raw_data_field(out, &info[field]))
4455 return 0;
Willy Tarreau6b19b142019-10-09 15:44:21 +02004456 if ((flags & STAT_SHOW_FDESC) && !chunk_appendf(out, ":\"%s\"", info_fields[field].desc))
4457 return 0;
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004458 if (!chunk_strcat(out, "\n"))
4459 return 0;
4460 }
4461 return 1;
4462}
4463
4464/* Dump all fields from <info> into <out> using the "show info typed" format */
Willy Tarreau83061a82018-07-13 11:56:34 +02004465static int stats_dump_typed_info_fields(struct buffer *out,
Aurelien DARRAGONe76a0272022-12-15 12:10:03 +01004466 const struct field *info,
4467 struct show_stat_ctx *ctx)
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004468{
Aurelien DARRAGONe76a0272022-12-15 12:10:03 +01004469 int flags = ctx->flags;
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004470 int field;
4471
4472 for (field = 0; field < INF_TOTAL_FIELDS; field++) {
4473 if (!field_format(info, field))
4474 continue;
4475
Willy Tarreaueaa55372019-10-09 07:39:11 +02004476 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 +01004477 return 0;
4478 if (!stats_emit_field_tags(out, &info[field], ':'))
4479 return 0;
4480 if (!stats_emit_typed_data_field(out, &info[field]))
4481 return 0;
Willy Tarreau6b19b142019-10-09 15:44:21 +02004482 if ((flags & STAT_SHOW_FDESC) && !chunk_appendf(out, ":\"%s\"", info_fields[field].desc))
4483 return 0;
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004484 if (!chunk_strcat(out, "\n"))
4485 return 0;
4486 }
4487 return 1;
4488}
4489
Willy Tarreau0b26b382021-05-08 07:43:53 +02004490/* Fill <info> with HAProxy global info. <info> is preallocated array of length
4491 * <len>. The length of the array must be INF_TOTAL_FIELDS. If this length is
4492 * less then this value, the function returns 0, otherwise, it returns 1. Some
4493 * fields' presence or precision may depend on some of the STAT_* flags present
4494 * in <flags>.
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004495 */
Willy Tarreau0b26b382021-05-08 07:43:53 +02004496int stats_fill_info(struct field *info, int len, uint flags)
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004497{
Willy Tarreaud37e26e2021-05-08 07:40:52 +02004498 struct timeval up;
Willy Tarreau83061a82018-07-13 11:56:34 +02004499 struct buffer *out = get_trash_chunk();
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004500
4501#ifdef USE_OPENSSL
Willy Tarreauc5977722021-05-08 08:14:04 +02004502 double ssl_sess_rate = read_freq_ctr_flt(&global.ssl_per_sec);
4503 double ssl_key_rate = read_freq_ctr_flt(&global.ssl_fe_keys_per_sec);
4504 double ssl_reuse = 0;
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004505
Willy Tarreauc5977722021-05-08 08:14:04 +02004506 if (ssl_key_rate < ssl_sess_rate)
4507 ssl_reuse = 100.0 * (1.0 - ssl_key_rate / ssl_sess_rate);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004508#endif
4509
Willy Tarreaud37e26e2021-05-08 07:40:52 +02004510 tv_remain(&start_date, &now, &up);
4511
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004512 if (len < INF_TOTAL_FIELDS)
4513 return 0;
4514
4515 chunk_reset(out);
4516 memset(info, 0, sizeof(*info) * len);
4517
4518 info[INF_NAME] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, PRODUCT_NAME);
Willy Tarreau909b9d82019-01-04 18:20:32 +01004519 info[INF_VERSION] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, haproxy_version);
Adis Nezirovicb62b78b2021-01-15 13:12:33 +01004520 info[INF_BUILD_INFO] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, haproxy_version);
Willy Tarreau909b9d82019-01-04 18:20:32 +01004521 info[INF_RELEASE_DATE] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, haproxy_date);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004522
Yves Lafon95317282018-02-26 11:10:37 +01004523 info[INF_NBTHREAD] = mkf_u32(FO_CONFIG|FS_SERVICE, global.nbthread);
Willy Tarreau91358592021-06-15 08:08:04 +02004524 info[INF_NBPROC] = mkf_u32(FO_CONFIG|FS_SERVICE, 1);
Willy Tarreaue8422bf2021-06-15 09:08:18 +02004525 info[INF_PROCESS_NUM] = mkf_u32(FO_KEY, 1);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004526 info[INF_PID] = mkf_u32(FO_STATUS, pid);
4527
4528 info[INF_UPTIME] = mkf_str(FN_DURATION, chunk_newstr(out));
Willy Tarreaud37e26e2021-05-08 07:40:52 +02004529 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 +01004530
Willy Tarreaue8abc322021-05-08 07:56:56 +02004531 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);
4532 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 +01004533 info[INF_MEMMAX_MB] = mkf_u32(FO_CONFIG|FN_LIMIT, global.rlimit_memmax);
William Dauchya8766cf2021-01-15 22:41:37 +01004534 info[INF_MEMMAX_BYTES] = mkf_u32(FO_CONFIG|FN_LIMIT, global.rlimit_memmax * 1048576L);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004535 info[INF_POOL_ALLOC_MB] = mkf_u32(0, (unsigned)(pool_total_allocated() / 1048576L));
William Dauchya8766cf2021-01-15 22:41:37 +01004536 info[INF_POOL_ALLOC_BYTES] = mkf_u32(0, pool_total_allocated());
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004537 info[INF_POOL_USED_MB] = mkf_u32(0, (unsigned)(pool_total_used() / 1048576L));
William Dauchya8766cf2021-01-15 22:41:37 +01004538 info[INF_POOL_USED_BYTES] = mkf_u32(0, pool_total_used());
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004539 info[INF_POOL_FAILED] = mkf_u32(FN_COUNTER, pool_total_failures());
4540 info[INF_ULIMIT_N] = mkf_u32(FO_CONFIG|FN_LIMIT, global.rlimit_nofile);
4541 info[INF_MAXSOCK] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxsock);
4542 info[INF_MAXCONN] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxconn);
4543 info[INF_HARD_MAXCONN] = mkf_u32(FO_CONFIG|FN_LIMIT, global.hardmaxconn);
4544 info[INF_CURR_CONN] = mkf_u32(0, actconn);
4545 info[INF_CUM_CONN] = mkf_u32(FN_COUNTER, totalconn);
4546 info[INF_CUM_REQ] = mkf_u32(FN_COUNTER, global.req_count);
4547#ifdef USE_OPENSSL
4548 info[INF_MAX_SSL_CONNS] = mkf_u32(FN_MAX, global.maxsslconn);
Willy Tarreau82531f62021-10-06 12:15:18 +02004549 info[INF_CURR_SSL_CONNS] = mkf_u32(0, global.sslconns);
4550 info[INF_CUM_SSL_CONNS] = mkf_u32(FN_COUNTER, global.totalsslconns);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004551#endif
4552 info[INF_MAXPIPES] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxpipes);
4553 info[INF_PIPES_USED] = mkf_u32(0, pipes_used);
4554 info[INF_PIPES_FREE] = mkf_u32(0, pipes_free);
Willy Tarreauc5977722021-05-08 08:14:04 +02004555 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 +01004556 info[INF_CONN_RATE_LIMIT] = mkf_u32(FO_CONFIG|FN_LIMIT, global.cps_lim);
4557 info[INF_MAX_CONN_RATE] = mkf_u32(FN_MAX, global.cps_max);
Willy Tarreauc5977722021-05-08 08:14:04 +02004558 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 +01004559 info[INF_SESS_RATE_LIMIT] = mkf_u32(FO_CONFIG|FN_LIMIT, global.sps_lim);
4560 info[INF_MAX_SESS_RATE] = mkf_u32(FN_RATE, global.sps_max);
4561
4562#ifdef USE_OPENSSL
Willy Tarreauc5977722021-05-08 08:14:04 +02004563 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 +01004564 info[INF_SSL_RATE_LIMIT] = mkf_u32(FO_CONFIG|FN_LIMIT, global.ssl_lim);
4565 info[INF_MAX_SSL_RATE] = mkf_u32(FN_MAX, global.ssl_max);
Willy Tarreauc5977722021-05-08 08:14:04 +02004566 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 +01004567 info[INF_SSL_FRONTEND_MAX_KEY_RATE] = mkf_u32(FN_MAX, global.ssl_fe_keys_max);
Willy Tarreauc5977722021-05-08 08:14:04 +02004568 info[INF_SSL_FRONTEND_SESSION_REUSE_PCT] = (flags & STAT_USE_FLOAT) ? mkf_flt(FN_RATE, ssl_reuse) : mkf_u32(0, ssl_reuse);
4569 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 +01004570 info[INF_SSL_BACKEND_MAX_KEY_RATE] = mkf_u32(FN_MAX, global.ssl_be_keys_max);
4571 info[INF_SSL_CACHE_LOOKUPS] = mkf_u32(FN_COUNTER, global.shctx_lookups);
4572 info[INF_SSL_CACHE_MISSES] = mkf_u32(FN_COUNTER, global.shctx_misses);
4573#endif
Willy Tarreauc5977722021-05-08 08:14:04 +02004574 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));
4575 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 +01004576 info[INF_COMPRESS_BPS_RATE_LIM] = mkf_u32(FO_CONFIG|FN_LIMIT, global.comp_rate_lim);
4577#ifdef USE_ZLIB
4578 info[INF_ZLIB_MEM_USAGE] = mkf_u32(0, zlib_used_memory);
4579 info[INF_MAX_ZLIB_MEM_USAGE] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxzlibmem);
4580#endif
Willy Tarreau955a11e2021-02-24 17:03:30 +01004581 info[INF_TASKS] = mkf_u32(0, total_allocated_tasks());
Willy Tarreau9c7b8082021-02-24 15:10:07 +01004582 info[INF_RUN_QUEUE] = mkf_u32(0, total_run_queues());
Willy Tarreauf9d5e102021-10-08 10:43:59 +02004583 info[INF_IDLE_PCT] = mkf_u32(FN_AVG, clock_report_idle());
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004584 info[INF_NODE] = mkf_str(FO_CONFIG|FN_OUTPUT|FS_SERVICE, global.node);
4585 if (global.desc)
4586 info[INF_DESCRIPTION] = mkf_str(FO_CONFIG|FN_OUTPUT|FS_SERVICE, global.desc);
Willy Tarreau00098ea2018-11-05 14:38:13 +01004587 info[INF_STOPPING] = mkf_u32(0, stopping);
4588 info[INF_JOBS] = mkf_u32(0, jobs);
William Lallemanda7199262018-11-16 16:57:20 +01004589 info[INF_UNSTOPPABLE_JOBS] = mkf_u32(0, unstoppable_jobs);
Willy Tarreau00098ea2018-11-05 14:38:13 +01004590 info[INF_LISTENERS] = mkf_u32(0, listeners);
Willy Tarreau199ad242018-11-05 16:31:22 +01004591 info[INF_ACTIVE_PEERS] = mkf_u32(0, active_peers);
Willy Tarreau2d372c22018-11-05 17:12:27 +01004592 info[INF_CONNECTED_PEERS] = mkf_u32(0, connected_peers);
Willy Tarreau13ef7732018-11-12 07:25:28 +01004593 info[INF_DROPPED_LOGS] = mkf_u32(0, dropped_logs);
Willy Tarreaubeb859a2018-11-22 18:07:59 +01004594 info[INF_BUSY_POLLING] = mkf_u32(0, !!(global.tune.options & GTUNE_BUSY_POLLING));
Emeric Brund30e9a12020-12-23 18:49:16 +01004595 info[INF_FAILED_RESOLUTIONS] = mkf_u32(0, resolv_failed_resolutions);
Willy Tarreau7cf0e452019-05-23 11:39:14 +02004596 info[INF_TOTAL_BYTES_OUT] = mkf_u64(0, global.out_bytes);
Christopher Fauletaaa70852020-07-10 13:56:30 +02004597 info[INF_TOTAL_SPLICED_BYTES_OUT] = mkf_u64(0, global.spliced_out_bytes);
Willy Tarreau7cf0e452019-05-23 11:39:14 +02004598 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 +02004599 info[INF_DEBUG_COMMANDS_ISSUED] = mkf_u32(0, debug_commands_issued);
Emeric Brun45c457a2020-07-09 23:23:34 +02004600 info[INF_CUM_LOG_MSGS] = mkf_u32(FN_COUNTER, cum_log_messages);
Amaury Denoyelle5dfdf3e2021-05-05 17:09:12 +02004601
4602 info[INF_TAINTED] = mkf_str(FO_STATUS, chunk_newstr(out));
4603 chunk_appendf(out, "%#x", get_tainted());
4604
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004605 return 1;
4606}
4607
Willy Tarreau4596fe22022-05-17 19:07:51 +02004608/* This function dumps information onto the stream connector's read buffer.
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004609 * It returns 0 as long as it does not complete, non-zero upon completion.
4610 * No state is used.
4611 */
Willy Tarreaucaff6312022-05-27 10:17:46 +02004612static int stats_dump_info_to_buffer(struct stconn *sc)
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004613{
Willy Tarreaucaff6312022-05-27 10:17:46 +02004614 struct appctx *appctx = __sc_appctx(sc);
Willy Tarreau91cefca2022-05-03 17:08:29 +02004615 struct show_stat_ctx *ctx = appctx->svcctx;
Aurelien DARRAGON55941842022-12-15 14:01:04 +01004616 int ret;
4617 int current_field;
Christopher Faulet2da02ae2022-02-24 13:45:27 +01004618
Willy Tarreau91cefca2022-05-03 17:08:29 +02004619 if (!stats_fill_info(info, INF_TOTAL_FIELDS, ctx->flags))
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004620 return 0;
4621
4622 chunk_reset(&trash);
Aurelien DARRAGON55941842022-12-15 14:01:04 +01004623more:
4624 current_field = ctx->field;
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004625
Willy Tarreau91cefca2022-05-03 17:08:29 +02004626 if (ctx->flags & STAT_FMT_TYPED)
Aurelien DARRAGON55941842022-12-15 14:01:04 +01004627 ret = stats_dump_typed_info_fields(&trash, info, ctx);
Willy Tarreau91cefca2022-05-03 17:08:29 +02004628 else if (ctx->flags & STAT_FMT_JSON)
Aurelien DARRAGON55941842022-12-15 14:01:04 +01004629 ret = stats_dump_json_info_fields(&trash, info, ctx);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004630 else
Aurelien DARRAGON55941842022-12-15 14:01:04 +01004631 ret = stats_dump_info_fields(&trash, info, ctx);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004632
Aurelien DARRAGON55941842022-12-15 14:01:04 +01004633 if (applet_putchk(appctx, &trash) == -1) {
4634 /* restore previous field */
4635 ctx->field = current_field;
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004636 return 0;
Aurelien DARRAGON55941842022-12-15 14:01:04 +01004637 }
4638 if (ret && ctx->field) {
4639 /* partial dump */
4640 goto more;
4641 }
4642 ctx->field = 0;
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004643 return 1;
4644}
4645
Willy Tarreau4596fe22022-05-17 19:07:51 +02004646/* This function dumps the schema onto the stream connector's read buffer.
Simon Horman6f6bb382017-01-04 09:37:26 +01004647 * It returns 0 as long as it does not complete, non-zero upon completion.
4648 * No state is used.
4649 *
Ilya Shipitsinf38a0182020-12-21 01:16:17 +05004650 * Integer values bounded to the range [-(2**53)+1, (2**53)-1] as
Simon Horman6f6bb382017-01-04 09:37:26 +01004651 * per the recommendation for interoperable integers in section 6 of RFC 7159.
4652 */
Willy Tarreau83061a82018-07-13 11:56:34 +02004653static void stats_dump_json_schema(struct buffer *out)
Simon Horman6f6bb382017-01-04 09:37:26 +01004654{
4655
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004656 int old_len = out->data;
Simon Horman6f6bb382017-01-04 09:37:26 +01004657
4658 chunk_strcat(out,
4659 "{"
4660 "\"$schema\":\"http://json-schema.org/draft-04/schema#\","
4661 "\"oneOf\":["
4662 "{"
4663 "\"title\":\"Info\","
4664 "\"type\":\"array\","
4665 "\"items\":{"
Amaury Denoyellea53ce4c2020-10-02 18:31:59 +02004666 "\"title\":\"InfoItem\","
4667 "\"type\":\"object\","
Simon Horman6f6bb382017-01-04 09:37:26 +01004668 "\"properties\":{"
Simon Horman6f6bb382017-01-04 09:37:26 +01004669 "\"field\":{\"$ref\":\"#/definitions/field\"},"
4670 "\"processNum\":{\"$ref\":\"#/definitions/processNum\"},"
4671 "\"tags\":{\"$ref\":\"#/definitions/tags\"},"
4672 "\"value\":{\"$ref\":\"#/definitions/typedValue\"}"
4673 "},"
4674 "\"required\":[\"field\",\"processNum\",\"tags\","
4675 "\"value\"]"
4676 "}"
4677 "},"
4678 "{"
4679 "\"title\":\"Stat\","
4680 "\"type\":\"array\","
4681 "\"items\":{"
4682 "\"title\":\"InfoItem\","
4683 "\"type\":\"object\","
4684 "\"properties\":{"
4685 "\"objType\":{"
4686 "\"enum\":[\"Frontend\",\"Backend\",\"Listener\","
4687 "\"Server\",\"Unknown\"]"
4688 "},"
4689 "\"proxyId\":{"
4690 "\"type\":\"integer\","
4691 "\"minimum\":0"
4692 "},"
4693 "\"id\":{"
4694 "\"type\":\"integer\","
4695 "\"minimum\":0"
4696 "},"
4697 "\"field\":{\"$ref\":\"#/definitions/field\"},"
4698 "\"processNum\":{\"$ref\":\"#/definitions/processNum\"},"
4699 "\"tags\":{\"$ref\":\"#/definitions/tags\"},"
4700 "\"typedValue\":{\"$ref\":\"#/definitions/typedValue\"}"
4701 "},"
4702 "\"required\":[\"objType\",\"proxyId\",\"id\","
4703 "\"field\",\"processNum\",\"tags\","
4704 "\"value\"]"
4705 "}"
4706 "},"
4707 "{"
4708 "\"title\":\"Error\","
4709 "\"type\":\"object\","
4710 "\"properties\":{"
4711 "\"errorStr\":{"
4712 "\"type\":\"string\""
Amaury Denoyellea53ce4c2020-10-02 18:31:59 +02004713 "}"
4714 "},"
4715 "\"required\":[\"errorStr\"]"
Simon Horman6f6bb382017-01-04 09:37:26 +01004716 "}"
4717 "],"
4718 "\"definitions\":{"
4719 "\"field\":{"
4720 "\"type\":\"object\","
4721 "\"pos\":{"
4722 "\"type\":\"integer\","
4723 "\"minimum\":0"
4724 "},"
4725 "\"name\":{"
4726 "\"type\":\"string\""
4727 "},"
4728 "\"required\":[\"pos\",\"name\"]"
4729 "},"
4730 "\"processNum\":{"
4731 "\"type\":\"integer\","
4732 "\"minimum\":1"
4733 "},"
4734 "\"tags\":{"
4735 "\"type\":\"object\","
4736 "\"origin\":{"
4737 "\"type\":\"string\","
4738 "\"enum\":[\"Metric\",\"Status\",\"Key\","
4739 "\"Config\",\"Product\",\"Unknown\"]"
4740 "},"
4741 "\"nature\":{"
4742 "\"type\":\"string\","
4743 "\"enum\":[\"Gauge\",\"Limit\",\"Min\",\"Max\","
4744 "\"Rate\",\"Counter\",\"Duration\","
4745 "\"Age\",\"Time\",\"Name\",\"Output\","
4746 "\"Avg\", \"Unknown\"]"
4747 "},"
4748 "\"scope\":{"
4749 "\"type\":\"string\","
4750 "\"enum\":[\"Cluster\",\"Process\",\"Service\","
4751 "\"System\",\"Unknown\"]"
4752 "},"
4753 "\"required\":[\"origin\",\"nature\",\"scope\"]"
4754 "},"
4755 "\"typedValue\":{"
4756 "\"type\":\"object\","
4757 "\"oneOf\":["
4758 "{\"$ref\":\"#/definitions/typedValue/definitions/s32Value\"},"
4759 "{\"$ref\":\"#/definitions/typedValue/definitions/s64Value\"},"
4760 "{\"$ref\":\"#/definitions/typedValue/definitions/u32Value\"},"
4761 "{\"$ref\":\"#/definitions/typedValue/definitions/u64Value\"},"
4762 "{\"$ref\":\"#/definitions/typedValue/definitions/strValue\"}"
4763 "],"
4764 "\"definitions\":{"
4765 "\"s32Value\":{"
4766 "\"properties\":{"
4767 "\"type\":{"
4768 "\"type\":\"string\","
4769 "\"enum\":[\"s32\"]"
4770 "},"
4771 "\"value\":{"
4772 "\"type\":\"integer\","
4773 "\"minimum\":-2147483648,"
4774 "\"maximum\":2147483647"
4775 "}"
4776 "},"
4777 "\"required\":[\"type\",\"value\"]"
4778 "},"
4779 "\"s64Value\":{"
4780 "\"properties\":{"
4781 "\"type\":{"
4782 "\"type\":\"string\","
4783 "\"enum\":[\"s64\"]"
4784 "},"
4785 "\"value\":{"
4786 "\"type\":\"integer\","
4787 "\"minimum\":-9007199254740991,"
4788 "\"maximum\":9007199254740991"
4789 "}"
4790 "},"
4791 "\"required\":[\"type\",\"value\"]"
4792 "},"
4793 "\"u32Value\":{"
4794 "\"properties\":{"
4795 "\"type\":{"
4796 "\"type\":\"string\","
4797 "\"enum\":[\"u32\"]"
4798 "},"
4799 "\"value\":{"
4800 "\"type\":\"integer\","
4801 "\"minimum\":0,"
4802 "\"maximum\":4294967295"
4803 "}"
4804 "},"
4805 "\"required\":[\"type\",\"value\"]"
4806 "},"
4807 "\"u64Value\":{"
4808 "\"properties\":{"
4809 "\"type\":{"
4810 "\"type\":\"string\","
4811 "\"enum\":[\"u64\"]"
4812 "},"
4813 "\"value\":{"
4814 "\"type\":\"integer\","
4815 "\"minimum\":0,"
4816 "\"maximum\":9007199254740991"
4817 "}"
4818 "},"
4819 "\"required\":[\"type\",\"value\"]"
4820 "},"
4821 "\"strValue\":{"
4822 "\"properties\":{"
4823 "\"type\":{"
4824 "\"type\":\"string\","
4825 "\"enum\":[\"str\"]"
4826 "},"
4827 "\"value\":{\"type\":\"string\"}"
4828 "},"
4829 "\"required\":[\"type\",\"value\"]"
4830 "},"
4831 "\"unknownValue\":{"
4832 "\"properties\":{"
4833 "\"type\":{"
4834 "\"type\":\"integer\","
4835 "\"minimum\":0"
4836 "},"
4837 "\"value\":{"
4838 "\"type\":\"string\","
4839 "\"enum\":[\"unknown\"]"
4840 "}"
4841 "},"
4842 "\"required\":[\"type\",\"value\"]"
4843 "}"
4844 "}"
4845 "}"
4846 "}"
4847 "}");
4848
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004849 if (old_len == out->data) {
Simon Horman6f6bb382017-01-04 09:37:26 +01004850 chunk_reset(out);
4851 chunk_appendf(out,
4852 "{\"errorStr\":\"output buffer too short\"}");
4853 }
Willy Tarreau5a0e7ca2022-06-10 09:21:22 +02004854 chunk_appendf(out, "\n");
Simon Horman6f6bb382017-01-04 09:37:26 +01004855}
4856
Willy Tarreau4596fe22022-05-17 19:07:51 +02004857/* This function dumps the schema onto the stream connector's read buffer.
Simon Horman6f6bb382017-01-04 09:37:26 +01004858 * It returns 0 as long as it does not complete, non-zero upon completion.
4859 * No state is used.
4860 */
Willy Tarreaud0a06d52022-05-18 15:07:19 +02004861static int stats_dump_json_schema_to_buffer(struct appctx *appctx)
Simon Horman6f6bb382017-01-04 09:37:26 +01004862{
4863 chunk_reset(&trash);
4864
4865 stats_dump_json_schema(&trash);
4866
Willy Tarreaud0a06d52022-05-18 15:07:19 +02004867 if (applet_putchk(appctx, &trash) == -1)
Simon Horman6f6bb382017-01-04 09:37:26 +01004868 return 0;
Simon Horman6f6bb382017-01-04 09:37:26 +01004869
4870 return 1;
4871}
4872
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004873static int cli_parse_clear_counters(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau89d467c2016-11-23 11:02:40 +01004874{
4875 struct proxy *px;
4876 struct server *sv;
4877 struct listener *li;
Amaury Denoyelled3700a72020-10-05 11:49:43 +02004878 struct stats_module *mod;
Willy Tarreau89d467c2016-11-23 11:02:40 +01004879 int clrall = 0;
4880
4881 if (strcmp(args[2], "all") == 0)
4882 clrall = 1;
4883
4884 /* check permissions */
4885 if (!cli_has_level(appctx, ACCESS_LVL_OPER) ||
4886 (clrall && !cli_has_level(appctx, ACCESS_LVL_ADMIN)))
4887 return 1;
4888
Olivier Houchardfbc74e82017-11-24 16:54:05 +01004889 for (px = proxies_list; px; px = px->next) {
Willy Tarreau89d467c2016-11-23 11:02:40 +01004890 if (clrall) {
4891 memset(&px->be_counters, 0, sizeof(px->be_counters));
4892 memset(&px->fe_counters, 0, sizeof(px->fe_counters));
4893 }
4894 else {
4895 px->be_counters.conn_max = 0;
4896 px->be_counters.p.http.rps_max = 0;
4897 px->be_counters.sps_max = 0;
4898 px->be_counters.cps_max = 0;
4899 px->be_counters.nbpend_max = 0;
Christopher Fauletefb41f02019-11-08 14:53:15 +01004900 px->be_counters.qtime_max = 0;
4901 px->be_counters.ctime_max = 0;
4902 px->be_counters.dtime_max = 0;
4903 px->be_counters.ttime_max = 0;
Willy Tarreau89d467c2016-11-23 11:02:40 +01004904
4905 px->fe_counters.conn_max = 0;
4906 px->fe_counters.p.http.rps_max = 0;
4907 px->fe_counters.sps_max = 0;
4908 px->fe_counters.cps_max = 0;
Willy Tarreau89d467c2016-11-23 11:02:40 +01004909 }
4910
4911 for (sv = px->srv; sv; sv = sv->next)
4912 if (clrall)
4913 memset(&sv->counters, 0, sizeof(sv->counters));
4914 else {
4915 sv->counters.cur_sess_max = 0;
4916 sv->counters.nbpend_max = 0;
4917 sv->counters.sps_max = 0;
Christopher Fauletefb41f02019-11-08 14:53:15 +01004918 sv->counters.qtime_max = 0;
4919 sv->counters.ctime_max = 0;
4920 sv->counters.dtime_max = 0;
4921 sv->counters.ttime_max = 0;
Willy Tarreau89d467c2016-11-23 11:02:40 +01004922 }
4923
4924 list_for_each_entry(li, &px->conf.listeners, by_fe)
4925 if (li->counters) {
4926 if (clrall)
4927 memset(li->counters, 0, sizeof(*li->counters));
4928 else
4929 li->counters->conn_max = 0;
4930 }
4931 }
4932
4933 global.cps_max = 0;
4934 global.sps_max = 0;
Olivier Houchard00bc3cb2017-10-17 19:23:25 +02004935 global.ssl_max = 0;
4936 global.ssl_fe_keys_max = 0;
4937 global.ssl_be_keys_max = 0;
Willy Tarreaud80cb4e2018-01-20 19:30:13 +01004938
Amaury Denoyelled3700a72020-10-05 11:49:43 +02004939 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
4940 if (!mod->clearable && !clrall)
4941 continue;
4942
4943 for (px = proxies_list; px; px = px->next) {
4944 enum stats_domain_px_cap mod_cap = stats_px_get_cap(mod->domain_flags);
4945
4946 if (px->cap & PR_CAP_FE && mod_cap & STATS_PX_CAP_FE) {
4947 EXTRA_COUNTERS_INIT(px->extra_counters_fe,
4948 mod,
4949 mod->counters,
4950 mod->counters_size);
4951 }
4952
4953 if (px->cap & PR_CAP_BE && mod_cap & STATS_PX_CAP_BE) {
4954 EXTRA_COUNTERS_INIT(px->extra_counters_be,
4955 mod,
4956 mod->counters,
4957 mod->counters_size);
4958 }
4959
4960 if (mod_cap & STATS_PX_CAP_SRV) {
4961 for (sv = px->srv; sv; sv = sv->next) {
4962 EXTRA_COUNTERS_INIT(sv->extra_counters,
4963 mod,
4964 mod->counters,
4965 mod->counters_size);
4966 }
4967 }
4968
4969 if (mod_cap & STATS_PX_CAP_LI) {
4970 list_for_each_entry(li, &px->conf.listeners, by_fe) {
4971 EXTRA_COUNTERS_INIT(li->extra_counters,
4972 mod,
4973 mod->counters,
4974 mod->counters_size);
4975 }
4976 }
4977 }
4978 }
4979
Emeric Brunf8642ee2021-10-29 17:59:18 +02004980 resolv_stats_clear_counters(clrall, &stats_module_list[STATS_DOMAIN_RESOLVERS]);
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02004981
Willy Tarreaud80cb4e2018-01-20 19:30:13 +01004982 memset(activity, 0, sizeof(activity));
Willy Tarreau89d467c2016-11-23 11:02:40 +01004983 return 1;
4984}
4985
4986
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004987static int cli_parse_show_info(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004988{
Willy Tarreau91cefca2022-05-03 17:08:29 +02004989 struct show_stat_ctx *ctx = applet_reserve_svcctx(appctx, sizeof(*ctx));
Willy Tarreau2f397382019-10-09 11:43:59 +02004990 int arg = 2;
4991
Willy Tarreau91cefca2022-05-03 17:08:29 +02004992 ctx->scope_str = 0;
4993 ctx->scope_len = 0;
4994 ctx->flags = 0;
Aurelien DARRAGON55941842022-12-15 14:01:04 +01004995 ctx->field = 0; /* explicit default value */
Willy Tarreaud25fc792016-12-16 12:33:47 +01004996
Willy Tarreau2f397382019-10-09 11:43:59 +02004997 while (*args[arg]) {
4998 if (strcmp(args[arg], "typed") == 0)
Willy Tarreau91cefca2022-05-03 17:08:29 +02004999 ctx->flags = (ctx->flags & ~STAT_FMT_MASK) | STAT_FMT_TYPED;
Willy Tarreau2f397382019-10-09 11:43:59 +02005000 else if (strcmp(args[arg], "json") == 0)
Willy Tarreau91cefca2022-05-03 17:08:29 +02005001 ctx->flags = (ctx->flags & ~STAT_FMT_MASK) | STAT_FMT_JSON;
Willy Tarreau2f397382019-10-09 11:43:59 +02005002 else if (strcmp(args[arg], "desc") == 0)
Willy Tarreau91cefca2022-05-03 17:08:29 +02005003 ctx->flags |= STAT_SHOW_FDESC;
Willy Tarreau27456202021-05-08 07:54:24 +02005004 else if (strcmp(args[arg], "float") == 0)
Willy Tarreau91cefca2022-05-03 17:08:29 +02005005 ctx->flags |= STAT_USE_FLOAT;
Willy Tarreau2f397382019-10-09 11:43:59 +02005006 arg++;
5007 }
Willy Tarreau0baac8c2016-11-22 16:36:53 +01005008 return 0;
5009}
5010
5011
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02005012static int cli_parse_show_stat(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau2b812e22016-11-22 16:18:05 +01005013{
Willy Tarreau91cefca2022-05-03 17:08:29 +02005014 struct show_stat_ctx *ctx = applet_reserve_svcctx(appctx, sizeof(*ctx));
Willy Tarreau2f397382019-10-09 11:43:59 +02005015 int arg = 2;
5016
Willy Tarreau91cefca2022-05-03 17:08:29 +02005017 ctx->scope_str = 0;
5018 ctx->scope_len = 0;
5019 ctx->flags = STAT_SHNODE | STAT_SHDESC;
Willy Tarreau578d6e42019-10-09 11:00:22 +02005020
Willy Tarreau0698c802022-05-11 14:09:57 +02005021 if ((strm_li(appctx_strm(appctx))->bind_conf->level & ACCESS_LVL_MASK) >= ACCESS_LVL_OPER)
Willy Tarreau91cefca2022-05-03 17:08:29 +02005022 ctx->flags |= STAT_SHLGNDS;
Willy Tarreaud25fc792016-12-16 12:33:47 +01005023
Amaury Denoyelle072f97e2020-10-05 11:49:37 +02005024 /* proxy is the default domain */
Willy Tarreau91cefca2022-05-03 17:08:29 +02005025 ctx->domain = STATS_DOMAIN_PROXY;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01005026 if (strcmp(args[arg], "domain") == 0) {
Amaury Denoyelle072f97e2020-10-05 11:49:37 +02005027 ++args;
5028
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01005029 if (strcmp(args[arg], "proxy") == 0) {
Amaury Denoyelle072f97e2020-10-05 11:49:37 +02005030 ++args;
Emeric Brunf8642ee2021-10-29 17:59:18 +02005031 } else if (strcmp(args[arg], "resolvers") == 0) {
Willy Tarreau91cefca2022-05-03 17:08:29 +02005032 ctx->domain = STATS_DOMAIN_RESOLVERS;
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02005033 ++args;
5034 } else {
Amaury Denoyelle072f97e2020-10-05 11:49:37 +02005035 return cli_err(appctx, "Invalid statistics domain.\n");
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02005036 }
Amaury Denoyelle072f97e2020-10-05 11:49:37 +02005037 }
5038
Willy Tarreau91cefca2022-05-03 17:08:29 +02005039 if (ctx->domain == STATS_DOMAIN_PROXY
Amaury Denoyelle072f97e2020-10-05 11:49:37 +02005040 && *args[arg] && *args[arg+1] && *args[arg+2]) {
Willy Tarreaua1b1ed52016-11-25 08:50:58 +01005041 struct proxy *px;
5042
Willy Tarreau2f397382019-10-09 11:43:59 +02005043 px = proxy_find_by_name(args[arg], 0, 0);
Willy Tarreaua1b1ed52016-11-25 08:50:58 +01005044 if (px)
Willy Tarreau91cefca2022-05-03 17:08:29 +02005045 ctx->iid = px->uuid;
Willy Tarreaua1b1ed52016-11-25 08:50:58 +01005046 else
Willy Tarreau91cefca2022-05-03 17:08:29 +02005047 ctx->iid = atoi(args[arg]);
Willy Tarreaua1b1ed52016-11-25 08:50:58 +01005048
Willy Tarreau91cefca2022-05-03 17:08:29 +02005049 if (!ctx->iid)
Willy Tarreau9d008692019-08-09 11:21:01 +02005050 return cli_err(appctx, "No such proxy.\n");
Willy Tarreaua1b1ed52016-11-25 08:50:58 +01005051
Willy Tarreau91cefca2022-05-03 17:08:29 +02005052 ctx->flags |= STAT_BOUND;
5053 ctx->type = atoi(args[arg+1]);
5054 ctx->sid = atoi(args[arg+2]);
Willy Tarreau2f397382019-10-09 11:43:59 +02005055 arg += 3;
5056 }
5057
5058 while (*args[arg]) {
5059 if (strcmp(args[arg], "typed") == 0)
Willy Tarreau91cefca2022-05-03 17:08:29 +02005060 ctx->flags = (ctx->flags & ~STAT_FMT_MASK) | STAT_FMT_TYPED;
Willy Tarreau2f397382019-10-09 11:43:59 +02005061 else if (strcmp(args[arg], "json") == 0)
Willy Tarreau91cefca2022-05-03 17:08:29 +02005062 ctx->flags = (ctx->flags & ~STAT_FMT_MASK) | STAT_FMT_JSON;
Willy Tarreau2f397382019-10-09 11:43:59 +02005063 else if (strcmp(args[arg], "desc") == 0)
Willy Tarreau91cefca2022-05-03 17:08:29 +02005064 ctx->flags |= STAT_SHOW_FDESC;
Willy Tarreau3e320362020-10-23 17:28:57 +02005065 else if (strcmp(args[arg], "no-maint") == 0)
Willy Tarreau91cefca2022-05-03 17:08:29 +02005066 ctx->flags |= STAT_HIDE_MAINT;
Willy Tarreau65141ff2020-10-23 17:19:48 +02005067 else if (strcmp(args[arg], "up") == 0)
Willy Tarreau91cefca2022-05-03 17:08:29 +02005068 ctx->flags |= STAT_HIDE_DOWN;
Willy Tarreau2f397382019-10-09 11:43:59 +02005069 arg++;
Willy Tarreau2b812e22016-11-22 16:18:05 +01005070 }
Willy Tarreau2b812e22016-11-22 16:18:05 +01005071
Willy Tarreau2b812e22016-11-22 16:18:05 +01005072 return 0;
5073}
5074
Willy Tarreau0baac8c2016-11-22 16:36:53 +01005075static int cli_io_handler_dump_info(struct appctx *appctx)
5076{
Willy Tarreauc12b3212022-05-27 11:08:15 +02005077 return stats_dump_info_to_buffer(appctx_sc(appctx));
Willy Tarreau0baac8c2016-11-22 16:36:53 +01005078}
5079
Willy Tarreau4596fe22022-05-17 19:07:51 +02005080/* This I/O handler runs as an applet embedded in a stream connector. It is
Willy Tarreau2b812e22016-11-22 16:18:05 +01005081 * used to send raw stats over a socket.
5082 */
5083static int cli_io_handler_dump_stat(struct appctx *appctx)
5084{
Willy Tarreauc12b3212022-05-27 11:08:15 +02005085 return stats_dump_stat_to_buffer(appctx_sc(appctx), NULL, NULL);
Willy Tarreau2b812e22016-11-22 16:18:05 +01005086}
5087
Simon Horman6f6bb382017-01-04 09:37:26 +01005088static int cli_io_handler_dump_json_schema(struct appctx *appctx)
5089{
Willy Tarreaud0a06d52022-05-18 15:07:19 +02005090 return stats_dump_json_schema_to_buffer(appctx);
Simon Horman6f6bb382017-01-04 09:37:26 +01005091}
5092
Amaury Denoyelle216a1ce2021-03-18 15:48:14 +01005093int stats_allocate_proxy_counters_internal(struct extra_counters **counters,
5094 int type, int px_cap)
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02005095{
5096 struct stats_module *mod;
5097
5098 EXTRA_COUNTERS_REGISTER(counters, type, alloc_failed);
5099
5100 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
5101 if (!(stats_px_get_cap(mod->domain_flags) & px_cap))
5102 continue;
5103
5104 EXTRA_COUNTERS_ADD(mod, *counters, mod->counters, mod->counters_size);
5105 }
5106
5107 EXTRA_COUNTERS_ALLOC(*counters, alloc_failed);
5108
5109 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
5110 if (!(stats_px_get_cap(mod->domain_flags) & px_cap))
5111 continue;
5112
5113 EXTRA_COUNTERS_INIT(*counters, mod, mod->counters, mod->counters_size);
5114 }
5115
5116 return 1;
5117
5118 alloc_failed:
5119 return 0;
5120}
5121
5122/* Initialize and allocate all extra counters for a proxy and its attached
5123 * servers/listeners with all already registered stats module
5124 */
5125int stats_allocate_proxy_counters(struct proxy *px)
5126{
5127 struct server *sv;
5128 struct listener *li;
5129
5130 if (px->cap & PR_CAP_FE) {
5131 if (!stats_allocate_proxy_counters_internal(&px->extra_counters_fe,
5132 COUNTERS_FE,
5133 STATS_PX_CAP_FE)) {
5134 return 0;
5135 }
5136 }
5137
5138 if (px->cap & PR_CAP_BE) {
5139 if (!stats_allocate_proxy_counters_internal(&px->extra_counters_be,
5140 COUNTERS_BE,
5141 STATS_PX_CAP_BE)) {
5142 return 0;
5143 }
5144 }
5145
5146 for (sv = px->srv; sv; sv = sv->next) {
5147 if (!stats_allocate_proxy_counters_internal(&sv->extra_counters,
5148 COUNTERS_SV,
5149 STATS_PX_CAP_SRV)) {
5150 return 0;
5151 }
5152 }
5153
5154 list_for_each_entry(li, &px->conf.listeners, by_fe) {
5155 if (!stats_allocate_proxy_counters_internal(&li->extra_counters,
5156 COUNTERS_LI,
5157 STATS_PX_CAP_LI)) {
5158 return 0;
5159 }
5160 }
5161
5162 return 1;
5163}
5164
Amaury Denoyelle58d395e2020-10-05 11:49:40 +02005165void stats_register_module(struct stats_module *m)
5166{
5167 const uint8_t domain = stats_get_domain(m->domain_flags);
5168
Willy Tarreau2b718102021-04-21 07:32:39 +02005169 LIST_APPEND(&stats_module_list[domain], &m->list);
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02005170 stat_count[domain] += m->stats_count;
Amaury Denoyelle58d395e2020-10-05 11:49:40 +02005171}
5172
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02005173static int allocate_stats_px_postcheck(void)
5174{
5175 struct stats_module *mod;
5176 size_t i = ST_F_TOTAL_FIELDS;
5177 int err_code = 0;
Amaury Denoyelle27373f72020-10-05 16:57:33 +02005178 struct proxy *px;
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02005179
5180 stat_count[STATS_DOMAIN_PROXY] += ST_F_TOTAL_FIELDS;
5181
5182 stat_f[STATS_DOMAIN_PROXY] = malloc(stat_count[STATS_DOMAIN_PROXY] * sizeof(struct name_desc));
5183 if (!stat_f[STATS_DOMAIN_PROXY]) {
5184 ha_alert("stats: cannot allocate all fields for proxy statistics\n");
5185 err_code |= ERR_ALERT | ERR_FATAL;
5186 return err_code;
5187 }
5188
5189 memcpy(stat_f[STATS_DOMAIN_PROXY], stat_fields,
5190 ST_F_TOTAL_FIELDS * sizeof(struct name_desc));
5191
5192 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
5193 memcpy(stat_f[STATS_DOMAIN_PROXY] + i,
5194 mod->stats,
5195 mod->stats_count * sizeof(struct name_desc));
5196 i += mod->stats_count;
5197 }
5198
Amaury Denoyelle27373f72020-10-05 16:57:33 +02005199 for (px = proxies_list; px; px = px->next) {
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02005200 if (!stats_allocate_proxy_counters(px)) {
5201 ha_alert("stats: cannot allocate all counters for proxy statistics\n");
5202 err_code |= ERR_ALERT | ERR_FATAL;
5203 return err_code;
5204 }
5205 }
5206
Christopher Fauletde79cd22021-01-06 07:41:56 +01005207 /* wait per-thread alloc to perform corresponding stat_l allocation */
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02005208
5209 return err_code;
5210}
5211
5212REGISTER_CONFIG_POSTPARSER("allocate-stats-px", allocate_stats_px_postcheck);
5213
Emeric Brunf8642ee2021-10-29 17:59:18 +02005214static int allocate_stats_rslv_postcheck(void)
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02005215{
5216 struct stats_module *mod;
5217 size_t i = 0;
5218 int err_code = 0;
5219
Emeric Brunf8642ee2021-10-29 17:59:18 +02005220 stat_f[STATS_DOMAIN_RESOLVERS] = malloc(stat_count[STATS_DOMAIN_RESOLVERS] * sizeof(struct name_desc));
5221 if (!stat_f[STATS_DOMAIN_RESOLVERS]) {
5222 ha_alert("stats: cannot allocate all fields for resolver statistics\n");
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02005223 err_code |= ERR_ALERT | ERR_FATAL;
5224 return err_code;
5225 }
5226
Emeric Brunf8642ee2021-10-29 17:59:18 +02005227 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_RESOLVERS], list) {
5228 memcpy(stat_f[STATS_DOMAIN_RESOLVERS] + i,
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02005229 mod->stats,
5230 mod->stats_count * sizeof(struct name_desc));
5231 i += mod->stats_count;
5232 }
5233
Emeric Brunf8642ee2021-10-29 17:59:18 +02005234 if (!resolv_allocate_counters(&stats_module_list[STATS_DOMAIN_RESOLVERS])) {
5235 ha_alert("stats: cannot allocate all counters for resolver statistics\n");
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02005236 err_code |= ERR_ALERT | ERR_FATAL;
5237 return err_code;
5238 }
5239
Christopher Fauletde79cd22021-01-06 07:41:56 +01005240 /* wait per-thread alloc to perform corresponding stat_l allocation */
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02005241
5242 return err_code;
5243}
5244
Emeric Brunf8642ee2021-10-29 17:59:18 +02005245REGISTER_CONFIG_POSTPARSER("allocate-stats-resolver", allocate_stats_rslv_postcheck);
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02005246
Christopher Fauletde79cd22021-01-06 07:41:56 +01005247static int allocate_stat_lines_per_thread(void)
5248{
Emeric Brunf8642ee2021-10-29 17:59:18 +02005249 int domains[] = { STATS_DOMAIN_PROXY, STATS_DOMAIN_RESOLVERS }, i;
Christopher Fauletde79cd22021-01-06 07:41:56 +01005250
5251 for (i = 0; i < STATS_DOMAIN_COUNT; ++i) {
5252 const int domain = domains[i];
5253
5254 stat_l[domain] = malloc(stat_count[domain] * sizeof(struct field));
5255 if (!stat_l[domain])
5256 return 0;
5257 }
5258 return 1;
5259}
5260
5261REGISTER_PER_THREAD_ALLOC(allocate_stat_lines_per_thread);
5262
Amaury Denoyelle7f8f6cb2020-11-10 14:24:31 +01005263static int allocate_trash_counters(void)
5264{
5265 struct stats_module *mod;
Emeric Brunf8642ee2021-10-29 17:59:18 +02005266 int domains[] = { STATS_DOMAIN_PROXY, STATS_DOMAIN_RESOLVERS }, i;
Amaury Denoyelle7f8f6cb2020-11-10 14:24:31 +01005267 size_t max_counters_size = 0;
5268
5269 /* calculate the greatest counters used by any stats modules */
5270 for (i = 0; i < STATS_DOMAIN_COUNT; ++i) {
5271 list_for_each_entry(mod, &stats_module_list[domains[i]], list) {
5272 max_counters_size = mod->counters_size > max_counters_size ?
5273 mod->counters_size : max_counters_size;
5274 }
5275 }
5276
5277 /* allocate the trash with the size of the greatest counters */
5278 if (max_counters_size) {
5279 trash_counters = malloc(max_counters_size);
5280 if (!trash_counters) {
5281 ha_alert("stats: cannot allocate trash counters for statistics\n");
5282 return 0;
5283 }
5284 }
5285
5286 return 1;
5287}
5288
5289REGISTER_PER_THREAD_ALLOC(allocate_trash_counters);
5290
Christopher Fauletde79cd22021-01-06 07:41:56 +01005291static void deinit_stat_lines_per_thread(void)
Amaury Denoyellea2a68992020-11-10 14:24:30 +01005292{
Emeric Brunf8642ee2021-10-29 17:59:18 +02005293 int domains[] = { STATS_DOMAIN_PROXY, STATS_DOMAIN_RESOLVERS }, i;
Amaury Denoyellea2a68992020-11-10 14:24:30 +01005294
5295 for (i = 0; i < STATS_DOMAIN_COUNT; ++i) {
5296 const int domain = domains[i];
5297
Willy Tarreau61cfdf42021-02-20 10:46:51 +01005298 ha_free(&stat_l[domain]);
Christopher Fauletde79cd22021-01-06 07:41:56 +01005299 }
5300}
5301
5302
5303REGISTER_PER_THREAD_FREE(deinit_stat_lines_per_thread);
5304
5305static void deinit_stats(void)
5306{
Emeric Brunf8642ee2021-10-29 17:59:18 +02005307 int domains[] = { STATS_DOMAIN_PROXY, STATS_DOMAIN_RESOLVERS }, i;
Christopher Fauletde79cd22021-01-06 07:41:56 +01005308
5309 for (i = 0; i < STATS_DOMAIN_COUNT; ++i) {
5310 const int domain = domains[i];
Amaury Denoyellea2a68992020-11-10 14:24:30 +01005311
5312 if (stat_f[domain])
5313 free(stat_f[domain]);
5314 }
5315}
5316
5317REGISTER_POST_DEINIT(deinit_stats);
5318
Amaury Denoyelle7f8f6cb2020-11-10 14:24:31 +01005319static void free_trash_counters(void)
5320{
5321 if (trash_counters)
5322 free(trash_counters);
5323}
5324
5325REGISTER_PER_THREAD_FREE(free_trash_counters);
5326
Willy Tarreau2b812e22016-11-22 16:18:05 +01005327/* register cli keywords */
5328static struct cli_kw_list cli_kws = {{ },{
Willy Tarreaub205bfd2021-05-07 11:38:37 +02005329 { { "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 +02005330 { { "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 +02005331 { { "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 },
5332 { { "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 +01005333 {{},}
5334}};
5335
Willy Tarreau0108d902018-11-25 19:14:37 +01005336INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
5337
William Lallemand74c24fb2016-11-21 17:18:36 +01005338struct applet http_stats_applet = {
5339 .obj_type = OBJ_TYPE_APPLET,
5340 .name = "<STATS>", /* used for logging */
5341 .fct = http_stats_io_handler,
5342 .release = NULL,
5343};
5344
William Lallemand74c24fb2016-11-21 17:18:36 +01005345/*
5346 * Local variables:
5347 * c-indent-level: 8
5348 * c-basic-offset: 8
5349 * End:
5350 */