blob: 7d34e54c4699714b606b65dd4e5378cbb1e6ed9a [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" },
Aurelien DARRAGON745ce8e2022-11-17 16:34:07 +0100264 [ST_F_SRID] = { .name = "srid", .desc = "Server id revision, to prevent server id reuse mixups" },
William Lallemand74c24fb2016-11-21 17:18:36 +0100265};
266
Willy Tarreau0baac8c2016-11-22 16:36:53 +0100267/* one line of info */
William Dauchy5d9b8f32021-01-11 20:07:49 +0100268THREAD_LOCAL struct field info[INF_TOTAL_FIELDS];
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +0200269
270/* description of statistics (static and dynamic) */
271static struct name_desc *stat_f[STATS_DOMAIN_COUNT];
272static size_t stat_count[STATS_DOMAIN_COUNT];
273
274/* one line for stats */
William Dauchyb9577452021-01-17 18:27:46 +0100275THREAD_LOCAL struct field *stat_l[STATS_DOMAIN_COUNT];
William Lallemand74c24fb2016-11-21 17:18:36 +0100276
Amaury Denoyelle58d395e2020-10-05 11:49:40 +0200277/* list of all registered stats module */
278static struct list stats_module_list[STATS_DOMAIN_COUNT] = {
279 LIST_HEAD_INIT(stats_module_list[STATS_DOMAIN_PROXY]),
Emeric Brunf8642ee2021-10-29 17:59:18 +0200280 LIST_HEAD_INIT(stats_module_list[STATS_DOMAIN_RESOLVERS]),
Amaury Denoyelle58d395e2020-10-05 11:49:40 +0200281};
282
Amaury Denoyelle7f8f6cb2020-11-10 14:24:31 +0100283THREAD_LOCAL void *trash_counters;
284
Amaury Denoyelle072f97e2020-10-05 11:49:37 +0200285static inline uint8_t stats_get_domain(uint32_t domain)
286{
287 return domain >> STATS_DOMAIN & STATS_DOMAIN_MASK;
288}
289
Amaury Denoyelle72b16e52020-10-05 11:49:38 +0200290static inline enum stats_domain_px_cap stats_px_get_cap(uint32_t domain)
291{
292 return domain >> STATS_PX_CAP & STATS_PX_CAP_MASK;
293}
294
Christopher Faulet6338a082019-09-09 15:50:54 +0200295static void stats_dump_json_schema(struct buffer *out);
William Lallemand74c24fb2016-11-21 17:18:36 +0100296
Amaury Denoyelle3ca927e2020-10-02 18:32:00 +0200297int stats_putchk(struct channel *chn, struct htx *htx, struct buffer *chk)
Christopher Fauletef779222018-10-31 08:47:01 +0100298{
299 if (htx) {
Christopher Faulet69fc88c2019-01-07 14:27:53 +0100300 if (chk->data >= channel_htx_recv_max(chn, htx))
301 return 0;
Willy Tarreau0a7ef022019-05-28 10:30:11 +0200302 if (!htx_add_data_atonce(htx, ist2(chk->area, chk->data)))
Christopher Fauletef779222018-10-31 08:47:01 +0100303 return 0;
Christopher Faulet5adbeeb2019-01-02 14:34:39 +0100304 channel_add_input(chn, chk->data);
Christopher Fauletef779222018-10-31 08:47:01 +0100305 chk->data = 0;
Christopher Fauletef779222018-10-31 08:47:01 +0100306 }
307 else {
308 if (ci_putchk(chn, chk) == -1)
309 return 0;
310 }
311 return 1;
312}
Willy Tarreau0baac8c2016-11-22 16:36:53 +0100313
Willy Tarreaucaff6312022-05-27 10:17:46 +0200314static const char *stats_scope_ptr(struct appctx *appctx, struct stconn *sc)
Christopher Fauleted7a0662019-01-14 11:07:34 +0100315{
Willy Tarreau91cefca2022-05-03 17:08:29 +0200316 struct show_stat_ctx *ctx = appctx->svcctx;
Willy Tarreaucaff6312022-05-27 10:17:46 +0200317 struct channel *req = sc_oc(sc);
Christopher Fauletb7f88902019-07-15 21:56:43 +0200318 struct htx *htx = htxbuf(&req->buf);
319 struct htx_blk *blk;
320 struct ist uri;
Christopher Fauleted7a0662019-01-14 11:07:34 +0100321
Christopher Fauletb7f88902019-07-15 21:56:43 +0200322 blk = htx_get_head_blk(htx);
Christopher Fauletea009732019-11-18 15:50:25 +0100323 BUG_ON(!blk || htx_get_blk_type(blk) != HTX_BLK_REQ_SL);
Christopher Fauletb7f88902019-07-15 21:56:43 +0200324 ALREADY_CHECKED(blk);
325 uri = htx_sl_req_uri(htx_get_blk_ptr(htx, blk));
Willy Tarreau91cefca2022-05-03 17:08:29 +0200326 return uri.ptr + ctx->scope_str;
Christopher Fauleted7a0662019-01-14 11:07:34 +0100327}
328
William Lallemand74c24fb2016-11-21 17:18:36 +0100329/*
330 * http_stats_io_handler()
331 * -> stats_dump_stat_to_buffer() // same as above, but used for CSV or HTML
332 * -> stats_dump_csv_header() // emits the CSV headers (same as above)
Simon Horman05ee2132017-01-04 09:37:25 +0100333 * -> stats_dump_json_header() // emits the JSON headers (same as above)
William Lallemand74c24fb2016-11-21 17:18:36 +0100334 * -> stats_dump_html_head() // emits the HTML headers
335 * -> stats_dump_html_info() // emits the equivalent of "show info" at the top
336 * -> stats_dump_proxy_to_buffer() // same as above, valid for CSV and HTML
337 * -> stats_dump_html_px_hdr()
338 * -> stats_dump_fe_stats()
339 * -> stats_dump_li_stats()
340 * -> stats_dump_sv_stats()
341 * -> stats_dump_be_stats()
342 * -> stats_dump_html_px_end()
343 * -> stats_dump_html_end() // emits HTML trailer
Simon Horman05ee2132017-01-04 09:37:25 +0100344 * -> stats_dump_json_end() // emits JSON trailer
William Lallemand74c24fb2016-11-21 17:18:36 +0100345 */
346
347
William Lallemand74c24fb2016-11-21 17:18:36 +0100348/* Dumps the stats CSV header to the trash buffer which. The caller is responsible
349 * for clearing it if needed.
350 * NOTE: Some tools happen to rely on the field position instead of its name,
351 * so please only append new fields at the end, never in the middle.
352 */
Amaury Denoyelle50660a82020-10-05 11:49:39 +0200353static void stats_dump_csv_header(enum stats_domain domain)
William Lallemand74c24fb2016-11-21 17:18:36 +0100354{
355 int field;
356
357 chunk_appendf(&trash, "# ");
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +0200358 if (stat_f[domain]) {
359 for (field = 0; field < stat_count[domain]; ++field) {
360 chunk_appendf(&trash, "%s,", stat_f[domain][field].name);
William Lallemand74c24fb2016-11-21 17:18:36 +0100361
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +0200362 /* print special delimiter on proxy stats to mark end of
363 static fields */
364 if (domain == STATS_DOMAIN_PROXY && field + 1 == ST_F_TOTAL_FIELDS)
365 chunk_appendf(&trash, "-,");
Amaury Denoyelle50660a82020-10-05 11:49:39 +0200366 }
367 }
368
William Lallemand74c24fb2016-11-21 17:18:36 +0100369 chunk_appendf(&trash, "\n");
370}
371
William Lallemand74c24fb2016-11-21 17:18:36 +0100372/* Emits a stats field without any surrounding element and properly encoded to
373 * resist CSV output. Returns non-zero on success, 0 if the buffer is full.
374 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200375int stats_emit_raw_data_field(struct buffer *out, const struct field *f)
William Lallemand74c24fb2016-11-21 17:18:36 +0100376{
377 switch (field_format(f, 0)) {
378 case FF_EMPTY: return 1;
379 case FF_S32: return chunk_appendf(out, "%d", f->u.s32);
380 case FF_U32: return chunk_appendf(out, "%u", f->u.u32);
381 case FF_S64: return chunk_appendf(out, "%lld", (long long)f->u.s64);
382 case FF_U64: return chunk_appendf(out, "%llu", (unsigned long long)f->u.u64);
Willy Tarreau065ba312021-05-08 10:38:20 +0200383 case FF_FLT: {
384 size_t prev_data = out->data;
385 out->data = flt_trim(out->area, prev_data, chunk_appendf(out, "%f", f->u.flt));
386 return out->data;
387 }
William Lallemand74c24fb2016-11-21 17:18:36 +0100388 case FF_STR: return csv_enc_append(field_str(f, 0), 1, out) != NULL;
389 default: return chunk_appendf(out, "[INCORRECT_FIELD_TYPE_%08x]", f->type);
390 }
391}
392
Amaury Denoyellee3f576c2020-10-05 11:49:44 +0200393const char *field_to_html_str(const struct field *f)
394{
395 switch (field_format(f, 0)) {
396 case FF_S32: return U2H(f->u.s32);
397 case FF_S64: return U2H(f->u.s64);
398 case FF_U64: return U2H(f->u.u64);
399 case FF_U32: return U2H(f->u.u32);
Willy Tarreau6004fb72021-05-08 07:37:38 +0200400 case FF_FLT: return F2H(f->u.flt);
Amaury Denoyellee3f576c2020-10-05 11:49:44 +0200401 case FF_STR: return field_str(f, 0);
402 case FF_EMPTY:
403 default:
404 return "";
405 }
406}
407
William Lallemand74c24fb2016-11-21 17:18:36 +0100408/* Emits a stats field prefixed with its type. No CSV encoding is prepared, the
409 * output is supposed to be used on its own line. Returns non-zero on success, 0
410 * if the buffer is full.
411 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200412int stats_emit_typed_data_field(struct buffer *out, const struct field *f)
William Lallemand74c24fb2016-11-21 17:18:36 +0100413{
414 switch (field_format(f, 0)) {
415 case FF_EMPTY: return 1;
416 case FF_S32: return chunk_appendf(out, "s32:%d", f->u.s32);
417 case FF_U32: return chunk_appendf(out, "u32:%u", f->u.u32);
418 case FF_S64: return chunk_appendf(out, "s64:%lld", (long long)f->u.s64);
419 case FF_U64: return chunk_appendf(out, "u64:%llu", (unsigned long long)f->u.u64);
Willy Tarreau065ba312021-05-08 10:38:20 +0200420 case FF_FLT: {
421 size_t prev_data = out->data;
422 out->data = flt_trim(out->area, prev_data, chunk_appendf(out, "flt:%f", f->u.flt));
423 return out->data;
424 }
William Lallemand74c24fb2016-11-21 17:18:36 +0100425 case FF_STR: return chunk_appendf(out, "str:%s", field_str(f, 0));
426 default: return chunk_appendf(out, "%08x:?", f->type);
427 }
428}
429
Simon Horman05ee2132017-01-04 09:37:25 +0100430/* Limit JSON integer values to the range [-(2**53)+1, (2**53)-1] as per
431 * the recommendation for interoperable integers in section 6 of RFC 7159.
432 */
433#define JSON_INT_MAX ((1ULL << 53) - 1)
434#define JSON_INT_MIN (0 - JSON_INT_MAX)
435
436/* Emits a stats field value and its type in JSON.
437 * Returns non-zero on success, 0 on error.
438 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200439int stats_emit_json_data_field(struct buffer *out, const struct field *f)
Simon Horman05ee2132017-01-04 09:37:25 +0100440{
441 int old_len;
442 char buf[20];
443 const char *type, *value = buf, *quote = "";
444
445 switch (field_format(f, 0)) {
446 case FF_EMPTY: return 1;
447 case FF_S32: type = "\"s32\"";
448 snprintf(buf, sizeof(buf), "%d", f->u.s32);
449 break;
450 case FF_U32: type = "\"u32\"";
451 snprintf(buf, sizeof(buf), "%u", f->u.u32);
452 break;
453 case FF_S64: type = "\"s64\"";
454 if (f->u.s64 < JSON_INT_MIN || f->u.s64 > JSON_INT_MAX)
455 return 0;
456 type = "\"s64\"";
457 snprintf(buf, sizeof(buf), "%lld", (long long)f->u.s64);
458 break;
459 case FF_U64: if (f->u.u64 > JSON_INT_MAX)
460 return 0;
461 type = "\"u64\"";
462 snprintf(buf, sizeof(buf), "%llu",
463 (unsigned long long) f->u.u64);
Christopher Faulet52c91bb2019-09-28 10:37:31 +0200464 break;
Christopher Faulet88a0db22019-09-24 16:35:10 +0200465 case FF_FLT: type = "\"flt\"";
Willy Tarreau065ba312021-05-08 10:38:20 +0200466 flt_trim(buf, 0, snprintf(buf, sizeof(buf), "%f", f->u.flt));
Simon Horman05ee2132017-01-04 09:37:25 +0100467 break;
468 case FF_STR: type = "\"str\"";
469 value = field_str(f, 0);
470 quote = "\"";
471 break;
472 default: snprintf(buf, sizeof(buf), "%u", f->type);
473 type = buf;
474 value = "unknown";
475 quote = "\"";
476 break;
477 }
478
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200479 old_len = out->data;
Simon Horman05ee2132017-01-04 09:37:25 +0100480 chunk_appendf(out, ",\"value\":{\"type\":%s,\"value\":%s%s%s}",
481 type, quote, value, quote);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200482 return !(old_len == out->data);
Simon Horman05ee2132017-01-04 09:37:25 +0100483}
484
William Lallemand74c24fb2016-11-21 17:18:36 +0100485/* Emits an encoding of the field type on 3 characters followed by a delimiter.
486 * Returns non-zero on success, 0 if the buffer is full.
487 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200488int stats_emit_field_tags(struct buffer *out, const struct field *f,
489 char delim)
William Lallemand74c24fb2016-11-21 17:18:36 +0100490{
491 char origin, nature, scope;
492
493 switch (field_origin(f, 0)) {
494 case FO_METRIC: origin = 'M'; break;
495 case FO_STATUS: origin = 'S'; break;
496 case FO_KEY: origin = 'K'; break;
497 case FO_CONFIG: origin = 'C'; break;
498 case FO_PRODUCT: origin = 'P'; break;
499 default: origin = '?'; break;
500 }
501
502 switch (field_nature(f, 0)) {
503 case FN_GAUGE: nature = 'G'; break;
504 case FN_LIMIT: nature = 'L'; break;
505 case FN_MIN: nature = 'm'; break;
506 case FN_MAX: nature = 'M'; break;
507 case FN_RATE: nature = 'R'; break;
508 case FN_COUNTER: nature = 'C'; break;
509 case FN_DURATION: nature = 'D'; break;
510 case FN_AGE: nature = 'A'; break;
511 case FN_TIME: nature = 'T'; break;
512 case FN_NAME: nature = 'N'; break;
513 case FN_OUTPUT: nature = 'O'; break;
514 case FN_AVG: nature = 'a'; break;
515 default: nature = '?'; break;
516 }
517
518 switch (field_scope(f, 0)) {
519 case FS_PROCESS: scope = 'P'; break;
520 case FS_SERVICE: scope = 'S'; break;
521 case FS_SYSTEM: scope = 's'; break;
522 case FS_CLUSTER: scope = 'C'; break;
523 default: scope = '?'; break;
524 }
525
526 return chunk_appendf(out, "%c%c%c%c", origin, nature, scope, delim);
527}
528
Simon Horman05ee2132017-01-04 09:37:25 +0100529/* Emits an encoding of the field type as JSON.
530 * Returns non-zero on success, 0 if the buffer is full.
531 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200532int stats_emit_json_field_tags(struct buffer *out, const struct field *f)
Simon Horman05ee2132017-01-04 09:37:25 +0100533{
534 const char *origin, *nature, *scope;
535 int old_len;
536
537 switch (field_origin(f, 0)) {
538 case FO_METRIC: origin = "Metric"; break;
539 case FO_STATUS: origin = "Status"; break;
540 case FO_KEY: origin = "Key"; break;
541 case FO_CONFIG: origin = "Config"; break;
542 case FO_PRODUCT: origin = "Product"; break;
543 default: origin = "Unknown"; break;
544 }
545
546 switch (field_nature(f, 0)) {
547 case FN_GAUGE: nature = "Gauge"; break;
548 case FN_LIMIT: nature = "Limit"; break;
549 case FN_MIN: nature = "Min"; break;
550 case FN_MAX: nature = "Max"; break;
551 case FN_RATE: nature = "Rate"; break;
552 case FN_COUNTER: nature = "Counter"; break;
553 case FN_DURATION: nature = "Duration"; break;
554 case FN_AGE: nature = "Age"; break;
555 case FN_TIME: nature = "Time"; break;
556 case FN_NAME: nature = "Name"; break;
557 case FN_OUTPUT: nature = "Output"; break;
558 case FN_AVG: nature = "Avg"; break;
559 default: nature = "Unknown"; break;
560 }
561
562 switch (field_scope(f, 0)) {
563 case FS_PROCESS: scope = "Process"; break;
564 case FS_SERVICE: scope = "Service"; break;
565 case FS_SYSTEM: scope = "System"; break;
566 case FS_CLUSTER: scope = "Cluster"; break;
567 default: scope = "Unknown"; break;
568 }
569
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200570 old_len = out->data;
Simon Horman05ee2132017-01-04 09:37:25 +0100571 chunk_appendf(out, "\"tags\":{"
572 "\"origin\":\"%s\","
573 "\"nature\":\"%s\","
574 "\"scope\":\"%s\""
575 "}", origin, nature, scope);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200576 return !(old_len == out->data);
Simon Horman05ee2132017-01-04 09:37:25 +0100577}
William Lallemand74c24fb2016-11-21 17:18:36 +0100578
579/* Dump all fields from <stats> into <out> using CSV format */
Willy Tarreau83061a82018-07-13 11:56:34 +0200580static int stats_dump_fields_csv(struct buffer *out,
Amaury Denoyelle97323c92020-10-02 18:32:01 +0200581 const struct field *stats, size_t stats_count,
Amaury Denoyelle50660a82020-10-05 11:49:39 +0200582 unsigned int flags,
583 enum stats_domain domain)
William Lallemand74c24fb2016-11-21 17:18:36 +0100584{
585 int field;
586
Amaury Denoyelle50660a82020-10-05 11:49:39 +0200587 for (field = 0; field < stats_count; ++field) {
William Lallemand74c24fb2016-11-21 17:18:36 +0100588 if (!stats_emit_raw_data_field(out, &stats[field]))
589 return 0;
590 if (!chunk_strcat(out, ","))
591 return 0;
Amaury Denoyelle50660a82020-10-05 11:49:39 +0200592
593 /* print special delimiter on proxy stats to mark end of
594 static fields */
595 if (domain == STATS_DOMAIN_PROXY && field + 1 == ST_F_TOTAL_FIELDS) {
596 if (!chunk_strcat(out, "-,"))
597 return 0;
598 }
William Lallemand74c24fb2016-11-21 17:18:36 +0100599 }
Amaury Denoyelle50660a82020-10-05 11:49:39 +0200600
William Lallemand74c24fb2016-11-21 17:18:36 +0100601 chunk_strcat(out, "\n");
602 return 1;
603}
604
605/* Dump all fields from <stats> into <out> using a typed "field:desc:type:value" format */
Willy Tarreau83061a82018-07-13 11:56:34 +0200606static int stats_dump_fields_typed(struct buffer *out,
Amaury Denoyelle97323c92020-10-02 18:32:01 +0200607 const struct field *stats,
608 size_t stats_count,
Amaury Denoyelle072f97e2020-10-05 11:49:37 +0200609 unsigned int flags,
610 enum stats_domain domain)
William Lallemand74c24fb2016-11-21 17:18:36 +0100611{
612 int field;
613
Amaury Denoyelle97323c92020-10-02 18:32:01 +0200614 for (field = 0; field < stats_count; ++field) {
William Lallemand74c24fb2016-11-21 17:18:36 +0100615 if (!stats[field].type)
616 continue;
617
Amaury Denoyelle072f97e2020-10-05 11:49:37 +0200618 switch (domain) {
619 case STATS_DOMAIN_PROXY:
620 chunk_appendf(out, "%c.%u.%u.%d.%s.%u:",
621 stats[ST_F_TYPE].u.u32 == STATS_TYPE_FE ? 'F' :
622 stats[ST_F_TYPE].u.u32 == STATS_TYPE_BE ? 'B' :
623 stats[ST_F_TYPE].u.u32 == STATS_TYPE_SO ? 'L' :
624 stats[ST_F_TYPE].u.u32 == STATS_TYPE_SV ? 'S' :
625 '?',
626 stats[ST_F_IID].u.u32, stats[ST_F_SID].u.u32,
627 field,
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +0200628 stat_f[domain][field].name,
Amaury Denoyelle072f97e2020-10-05 11:49:37 +0200629 stats[ST_F_PID].u.u32);
630 break;
631
Emeric Brunf8642ee2021-10-29 17:59:18 +0200632 case STATS_DOMAIN_RESOLVERS:
633 chunk_appendf(out, "N.%d.%s:", field,
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +0200634 stat_f[domain][field].name);
635 break;
636
Amaury Denoyelle072f97e2020-10-05 11:49:37 +0200637 default:
638 break;
639 }
William Lallemand74c24fb2016-11-21 17:18:36 +0100640
641 if (!stats_emit_field_tags(out, &stats[field], ':'))
642 return 0;
643 if (!stats_emit_typed_data_field(out, &stats[field]))
644 return 0;
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +0200645
Willy Tarreaufc8e4382021-06-17 07:22:27 +0200646 if (flags & STAT_SHOW_FDESC &&
647 !chunk_appendf(out, ":\"%s\"", stat_f[domain][field].desc)) {
Willy Tarreau6b19b142019-10-09 15:44:21 +0200648 return 0;
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +0200649 }
650
William Lallemand74c24fb2016-11-21 17:18:36 +0100651 if (!chunk_strcat(out, "\n"))
652 return 0;
653 }
654 return 1;
655}
656
Simon Horman05ee2132017-01-04 09:37:25 +0100657/* Dump all fields from <stats> into <out> using the "show info json" format */
Willy Tarreau83061a82018-07-13 11:56:34 +0200658static int stats_dump_json_info_fields(struct buffer *out,
Willy Tarreau43241ff2019-10-09 11:27:51 +0200659 const struct field *info, unsigned int flags)
Simon Horman05ee2132017-01-04 09:37:25 +0100660{
661 int field;
662 int started = 0;
663
664 if (!chunk_strcat(out, "["))
665 return 0;
666
667 for (field = 0; field < INF_TOTAL_FIELDS; field++) {
668 int old_len;
669
670 if (!field_format(info, field))
671 continue;
672
673 if (started && !chunk_strcat(out, ","))
674 goto err;
675 started = 1;
676
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200677 old_len = out->data;
Simon Horman05ee2132017-01-04 09:37:25 +0100678 chunk_appendf(out,
679 "{\"field\":{\"pos\":%d,\"name\":\"%s\"},"
680 "\"processNum\":%u,",
Willy Tarreaueaa55372019-10-09 07:39:11 +0200681 field, info_fields[field].name,
Simon Horman05ee2132017-01-04 09:37:25 +0100682 info[INF_PROCESS_NUM].u.u32);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200683 if (old_len == out->data)
Simon Horman05ee2132017-01-04 09:37:25 +0100684 goto err;
685
686 if (!stats_emit_json_field_tags(out, &info[field]))
687 goto err;
688
689 if (!stats_emit_json_data_field(out, &info[field]))
690 goto err;
691
692 if (!chunk_strcat(out, "}"))
693 goto err;
694 }
695
Willy Tarreauc6b7a972022-06-10 15:12:21 +0200696 if (!chunk_strcat(out, "]\n"))
Simon Horman05ee2132017-01-04 09:37:25 +0100697 goto err;
698 return 1;
699
700err:
701 chunk_reset(out);
Willy Tarreauc6b7a972022-06-10 15:12:21 +0200702 chunk_appendf(out, "{\"errorStr\":\"output buffer too short\"}\n");
Simon Horman05ee2132017-01-04 09:37:25 +0100703 return 0;
704}
705
Amaury Denoyellef34017b2020-10-02 18:32:03 +0200706static void stats_print_proxy_field_json(struct buffer *out,
707 const struct field *stat,
708 const char *name,
709 int pos,
710 uint32_t field_type,
711 uint32_t iid,
712 uint32_t sid,
713 uint32_t pid)
714{
715 const char *obj_type;
716 switch (field_type) {
717 case STATS_TYPE_FE: obj_type = "Frontend"; break;
718 case STATS_TYPE_BE: obj_type = "Backend"; break;
719 case STATS_TYPE_SO: obj_type = "Listener"; break;
720 case STATS_TYPE_SV: obj_type = "Server"; break;
721 default: obj_type = "Unknown"; break;
722 }
723
724 chunk_appendf(out,
725 "{"
726 "\"objType\":\"%s\","
727 "\"proxyId\":%u,"
728 "\"id\":%u,"
729 "\"field\":{\"pos\":%d,\"name\":\"%s\"},"
730 "\"processNum\":%u,",
731 obj_type, iid, sid, pos, name, pid);
732}
733
Emeric Brunf8642ee2021-10-29 17:59:18 +0200734static void stats_print_rslv_field_json(struct buffer *out,
735 const struct field *stat,
736 const char *name,
737 int pos)
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +0200738{
739 chunk_appendf(out,
740 "{"
741 "\"field\":{\"pos\":%d,\"name\":\"%s\"},",
742 pos, name);
743}
744
745
Simon Horman05ee2132017-01-04 09:37:25 +0100746/* Dump all fields from <stats> into <out> using a typed "field:desc:type:value" format */
Willy Tarreau83061a82018-07-13 11:56:34 +0200747static int stats_dump_fields_json(struct buffer *out,
Amaury Denoyelle97323c92020-10-02 18:32:01 +0200748 const struct field *stats, size_t stats_count,
Amaury Denoyelle072f97e2020-10-05 11:49:37 +0200749 unsigned int flags,
750 enum stats_domain domain)
Simon Horman05ee2132017-01-04 09:37:25 +0100751{
752 int field;
753 int started = 0;
754
Willy Tarreaub0ce3ad2019-10-09 11:19:29 +0200755 if ((flags & STAT_STARTED) && !chunk_strcat(out, ","))
Simon Horman05ee2132017-01-04 09:37:25 +0100756 return 0;
757 if (!chunk_strcat(out, "["))
758 return 0;
759
Amaury Denoyelle97323c92020-10-02 18:32:01 +0200760 for (field = 0; field < stats_count; field++) {
Simon Horman05ee2132017-01-04 09:37:25 +0100761 int old_len;
762
763 if (!stats[field].type)
764 continue;
765
766 if (started && !chunk_strcat(out, ","))
767 goto err;
768 started = 1;
769
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200770 old_len = out->data;
Amaury Denoyelle072f97e2020-10-05 11:49:37 +0200771 if (domain == STATS_DOMAIN_PROXY) {
772 stats_print_proxy_field_json(out, &stats[field],
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +0200773 stat_f[domain][field].name,
774 field,
Amaury Denoyelle072f97e2020-10-05 11:49:37 +0200775 stats[ST_F_TYPE].u.u32,
776 stats[ST_F_IID].u.u32,
777 stats[ST_F_SID].u.u32,
778 stats[ST_F_PID].u.u32);
Emeric Brunf8642ee2021-10-29 17:59:18 +0200779 } else if (domain == STATS_DOMAIN_RESOLVERS) {
780 stats_print_rslv_field_json(out, &stats[field],
781 stat_f[domain][field].name,
782 field);
Amaury Denoyelle072f97e2020-10-05 11:49:37 +0200783 }
Amaury Denoyellef34017b2020-10-02 18:32:03 +0200784
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200785 if (old_len == out->data)
Simon Horman05ee2132017-01-04 09:37:25 +0100786 goto err;
787
788 if (!stats_emit_json_field_tags(out, &stats[field]))
789 goto err;
790
791 if (!stats_emit_json_data_field(out, &stats[field]))
792 goto err;
793
794 if (!chunk_strcat(out, "}"))
795 goto err;
796 }
797
798 if (!chunk_strcat(out, "]"))
799 goto err;
800
801 return 1;
802
803err:
804 chunk_reset(out);
Willy Tarreaub0ce3ad2019-10-09 11:19:29 +0200805 if (flags & STAT_STARTED)
806 chunk_strcat(out, ",");
Simon Horman05ee2132017-01-04 09:37:25 +0100807 chunk_appendf(out, "{\"errorStr\":\"output buffer too short\"}");
808 return 0;
809}
810
William Lallemand74c24fb2016-11-21 17:18:36 +0100811/* Dump all fields from <stats> into <out> using the HTML format. A column is
Willy Tarreauab02b3f2019-10-09 11:11:46 +0200812 * reserved for the checkbox is STAT_ADMIN is set in <flags>. Some extra info
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +0200813 * are provided if STAT_SHLGNDS is present in <flags>. The statistics from
814 * extra modules are displayed at the end of the lines if STAT_SHMODULES is
815 * present in <flags>.
William Lallemand74c24fb2016-11-21 17:18:36 +0100816 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200817static int stats_dump_fields_html(struct buffer *out,
818 const struct field *stats,
819 unsigned int flags)
William Lallemand74c24fb2016-11-21 17:18:36 +0100820{
Willy Tarreau83061a82018-07-13 11:56:34 +0200821 struct buffer src;
Amaury Denoyellee3f576c2020-10-05 11:49:44 +0200822 struct stats_module *mod;
823 int i = 0, j = 0;
William Lallemand74c24fb2016-11-21 17:18:36 +0100824
825 if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_FE) {
826 chunk_appendf(out,
827 /* name, queue */
828 "<tr class=\"frontend\">");
829
Willy Tarreauab02b3f2019-10-09 11:11:46 +0200830 if (flags & STAT_ADMIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +0100831 /* Column sub-heading for Enable or Disable server */
832 chunk_appendf(out, "<td></td>");
833 }
834
835 chunk_appendf(out,
836 "<td class=ac>"
837 "<a name=\"%s/Frontend\"></a>"
838 "<a class=lfsb href=\"#%s/Frontend\">Frontend</a></td>"
839 "<td colspan=3></td>"
840 "",
841 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_PXNAME));
842
843 chunk_appendf(out,
844 /* sessions rate : current */
845 "<td><u>%s<div class=tips><table class=det>"
846 "<tr><th>Current connection rate:</th><td>%s/s</td></tr>"
847 "<tr><th>Current session rate:</th><td>%s/s</td></tr>"
848 "",
849 U2H(stats[ST_F_RATE].u.u32),
850 U2H(stats[ST_F_CONN_RATE].u.u32),
851 U2H(stats[ST_F_RATE].u.u32));
852
853 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
854 chunk_appendf(out,
855 "<tr><th>Current request rate:</th><td>%s/s</td></tr>",
856 U2H(stats[ST_F_REQ_RATE].u.u32));
857
858 chunk_appendf(out,
859 "</table></div></u></td>"
860 /* sessions rate : max */
861 "<td><u>%s<div class=tips><table class=det>"
862 "<tr><th>Max connection rate:</th><td>%s/s</td></tr>"
863 "<tr><th>Max session rate:</th><td>%s/s</td></tr>"
864 "",
865 U2H(stats[ST_F_RATE_MAX].u.u32),
866 U2H(stats[ST_F_CONN_RATE_MAX].u.u32),
867 U2H(stats[ST_F_RATE_MAX].u.u32));
868
869 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
870 chunk_appendf(out,
871 "<tr><th>Max request rate:</th><td>%s/s</td></tr>",
872 U2H(stats[ST_F_REQ_RATE_MAX].u.u32));
873
874 chunk_appendf(out,
875 "</table></div></u></td>"
876 /* sessions rate : limit */
877 "<td>%s</td>",
878 LIM2A(stats[ST_F_RATE_LIM].u.u32, "-"));
879
880 chunk_appendf(out,
881 /* sessions: current, max, limit, total */
882 "<td>%s</td><td>%s</td><td>%s</td>"
883 "<td><u>%s<div class=tips><table class=det>"
884 "<tr><th>Cum. connections:</th><td>%s</td></tr>"
885 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
886 "",
887 U2H(stats[ST_F_SCUR].u.u32), U2H(stats[ST_F_SMAX].u.u32), U2H(stats[ST_F_SLIM].u.u32),
888 U2H(stats[ST_F_STOT].u.u64),
889 U2H(stats[ST_F_CONN_TOT].u.u64),
890 U2H(stats[ST_F_STOT].u.u64));
891
892 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
893 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0) {
894 chunk_appendf(out,
895 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
896 "<tr><th>- HTTP 1xx responses:</th><td>%s</td></tr>"
897 "<tr><th>- HTTP 2xx responses:</th><td>%s</td></tr>"
898 "<tr><th>&nbsp;&nbsp;Compressed 2xx:</th><td>%s</td><td>(%d%%)</td></tr>"
899 "<tr><th>- HTTP 3xx responses:</th><td>%s</td></tr>"
900 "<tr><th>- HTTP 4xx responses:</th><td>%s</td></tr>"
901 "<tr><th>- HTTP 5xx responses:</th><td>%s</td></tr>"
902 "<tr><th>- other responses:</th><td>%s</td></tr>"
903 "<tr><th>Intercepted requests:</th><td>%s</td></tr>"
Willy Tarreaua1214a52018-12-14 14:00:25 +0100904 "<tr><th>Cache lookups:</th><td>%s</td></tr>"
905 "<tr><th>Cache hits:</th><td>%s</td><td>(%d%%)</td></tr>"
Willy Tarreau1b0f85e2018-05-28 15:12:40 +0200906 "<tr><th>Failed hdr rewrites:</th><td>%s</td></tr>"
Christopher Faulet0159ee42019-12-16 14:40:39 +0100907 "<tr><th>Internal errors:</th><td>%s</td></tr>"
William Lallemand74c24fb2016-11-21 17:18:36 +0100908 "",
909 U2H(stats[ST_F_REQ_TOT].u.u64),
910 U2H(stats[ST_F_HRSP_1XX].u.u64),
911 U2H(stats[ST_F_HRSP_2XX].u.u64),
912 U2H(stats[ST_F_COMP_RSP].u.u64),
913 stats[ST_F_HRSP_2XX].u.u64 ?
914 (int)(100 * stats[ST_F_COMP_RSP].u.u64 / stats[ST_F_HRSP_2XX].u.u64) : 0,
915 U2H(stats[ST_F_HRSP_3XX].u.u64),
916 U2H(stats[ST_F_HRSP_4XX].u.u64),
917 U2H(stats[ST_F_HRSP_5XX].u.u64),
918 U2H(stats[ST_F_HRSP_OTHER].u.u64),
Willy Tarreau1b0f85e2018-05-28 15:12:40 +0200919 U2H(stats[ST_F_INTERCEPTED].u.u64),
Willy Tarreaua1214a52018-12-14 14:00:25 +0100920 U2H(stats[ST_F_CACHE_LOOKUPS].u.u64),
921 U2H(stats[ST_F_CACHE_HITS].u.u64),
922 stats[ST_F_CACHE_LOOKUPS].u.u64 ?
923 (int)(100 * stats[ST_F_CACHE_HITS].u.u64 / stats[ST_F_CACHE_LOOKUPS].u.u64) : 0,
Christopher Faulet0159ee42019-12-16 14:40:39 +0100924 U2H(stats[ST_F_WREW].u.u64),
925 U2H(stats[ST_F_EINT].u.u64));
William Lallemand74c24fb2016-11-21 17:18:36 +0100926 }
927
928 chunk_appendf(out,
929 "</table></div></u></td>"
930 /* sessions: lbtot, lastsess */
931 "<td></td><td></td>"
932 /* bytes : in */
933 "<td>%s</td>"
934 "",
935 U2H(stats[ST_F_BIN].u.u64));
936
937 chunk_appendf(out,
938 /* bytes:out + compression stats (via hover): comp_in, comp_out, comp_byp */
939 "<td>%s%s<div class=tips><table class=det>"
940 "<tr><th>Response bytes in:</th><td>%s</td></tr>"
941 "<tr><th>Compression in:</th><td>%s</td></tr>"
942 "<tr><th>Compression out:</th><td>%s</td><td>(%d%%)</td></tr>"
943 "<tr><th>Compression bypass:</th><td>%s</td></tr>"
944 "<tr><th>Total bytes saved:</th><td>%s</td><td>(%d%%)</td></tr>"
945 "</table></div>%s</td>",
946 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "<u>":"",
947 U2H(stats[ST_F_BOUT].u.u64),
948 U2H(stats[ST_F_BOUT].u.u64),
949 U2H(stats[ST_F_COMP_IN].u.u64),
950 U2H(stats[ST_F_COMP_OUT].u.u64),
951 stats[ST_F_COMP_IN].u.u64 ? (int)(stats[ST_F_COMP_OUT].u.u64 * 100 / stats[ST_F_COMP_IN].u.u64) : 0,
952 U2H(stats[ST_F_COMP_BYP].u.u64),
953 U2H(stats[ST_F_COMP_IN].u.u64 - stats[ST_F_COMP_OUT].u.u64),
954 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,
955 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "</u>":"");
956
957 chunk_appendf(out,
958 /* denied: req, resp */
959 "<td>%s</td><td>%s</td>"
960 /* errors : request, connect, response */
961 "<td>%s</td><td></td><td></td>"
962 /* warnings: retries, redispatches */
963 "<td></td><td></td>"
964 /* server status : reflect frontend status */
965 "<td class=ac>%s</td>"
966 /* rest of server: nothing */
Amaury Denoyellee3f576c2020-10-05 11:49:44 +0200967 "<td class=ac colspan=8></td>"
William Lallemand74c24fb2016-11-21 17:18:36 +0100968 "",
969 U2H(stats[ST_F_DREQ].u.u64), U2H(stats[ST_F_DRESP].u.u64),
970 U2H(stats[ST_F_EREQ].u.u64),
971 field_str(stats, ST_F_STATUS));
Amaury Denoyellee3f576c2020-10-05 11:49:44 +0200972
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +0200973 if (flags & STAT_SHMODULES) {
974 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
Amaury Denoyellee6ee8202020-11-03 15:04:46 +0100975 chunk_appendf(out, "<td>");
976
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +0200977 if (stats_px_get_cap(mod->domain_flags) & STATS_PX_CAP_FE) {
Amaury Denoyellee6ee8202020-11-03 15:04:46 +0100978 chunk_appendf(out,
979 "<u>%s<div class=tips><table class=det>",
980 mod->name);
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +0200981 for (j = 0; j < mod->stats_count; ++j) {
982 chunk_appendf(out,
983 "<tr><th>%s</th><td>%s</td></tr>",
984 mod->stats[j].desc, field_to_html_str(&stats[ST_F_TOTAL_FIELDS + i]));
985 ++i;
986 }
Amaury Denoyellee6ee8202020-11-03 15:04:46 +0100987 chunk_appendf(out, "</table></div></u>");
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +0200988 } else {
989 i += mod->stats_count;
Amaury Denoyellee3f576c2020-10-05 11:49:44 +0200990 }
Amaury Denoyellee3f576c2020-10-05 11:49:44 +0200991
Amaury Denoyellee6ee8202020-11-03 15:04:46 +0100992 chunk_appendf(out, "</td>");
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +0200993 }
Amaury Denoyellee3f576c2020-10-05 11:49:44 +0200994 }
995
996 chunk_appendf(out, "</tr>");
William Lallemand74c24fb2016-11-21 17:18:36 +0100997 }
998 else if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_SO) {
999 chunk_appendf(out, "<tr class=socket>");
Willy Tarreauab02b3f2019-10-09 11:11:46 +02001000 if (flags & STAT_ADMIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001001 /* Column sub-heading for Enable or Disable server */
1002 chunk_appendf(out, "<td></td>");
1003 }
1004
1005 chunk_appendf(out,
1006 /* frontend name, listener name */
1007 "<td class=ac><a name=\"%s/+%s\"></a>%s"
1008 "<a class=lfsb href=\"#%s/+%s\">%s</a>"
1009 "",
1010 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME),
Willy Tarreau708c4162019-10-09 10:19:16 +02001011 (flags & STAT_SHLGNDS)?"<u>":"",
William Lallemand74c24fb2016-11-21 17:18:36 +01001012 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME), field_str(stats, ST_F_SVNAME));
1013
Willy Tarreau708c4162019-10-09 10:19:16 +02001014 if (flags & STAT_SHLGNDS) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001015 chunk_appendf(out, "<div class=tips>");
1016
Willy Tarreau90807112020-02-25 08:16:33 +01001017 if (isdigit((unsigned char)*field_str(stats, ST_F_ADDR)))
William Lallemand74c24fb2016-11-21 17:18:36 +01001018 chunk_appendf(out, "IPv4: %s, ", field_str(stats, ST_F_ADDR));
1019 else if (*field_str(stats, ST_F_ADDR) == '[')
1020 chunk_appendf(out, "IPv6: %s, ", field_str(stats, ST_F_ADDR));
1021 else if (*field_str(stats, ST_F_ADDR))
1022 chunk_appendf(out, "%s, ", field_str(stats, ST_F_ADDR));
1023
1024 /* id */
1025 chunk_appendf(out, "id: %d</div>", stats[ST_F_SID].u.u32);
1026 }
1027
1028 chunk_appendf(out,
1029 /* queue */
1030 "%s</td><td colspan=3></td>"
1031 /* sessions rate: current, max, limit */
1032 "<td colspan=3>&nbsp;</td>"
1033 /* sessions: current, max, limit, total, lbtot, lastsess */
1034 "<td>%s</td><td>%s</td><td>%s</td>"
1035 "<td>%s</td><td>&nbsp;</td><td>&nbsp;</td>"
1036 /* bytes: in, out */
1037 "<td>%s</td><td>%s</td>"
1038 "",
Willy Tarreau708c4162019-10-09 10:19:16 +02001039 (flags & STAT_SHLGNDS)?"</u>":"",
William Lallemand74c24fb2016-11-21 17:18:36 +01001040 U2H(stats[ST_F_SCUR].u.u32), U2H(stats[ST_F_SMAX].u.u32), U2H(stats[ST_F_SLIM].u.u32),
1041 U2H(stats[ST_F_STOT].u.u64), U2H(stats[ST_F_BIN].u.u64), U2H(stats[ST_F_BOUT].u.u64));
1042
1043 chunk_appendf(out,
1044 /* denied: req, resp */
1045 "<td>%s</td><td>%s</td>"
1046 /* errors: request, connect, response */
1047 "<td>%s</td><td></td><td></td>"
1048 /* warnings: retries, redispatches */
1049 "<td></td><td></td>"
1050 /* server status: reflect listener status */
1051 "<td class=ac>%s</td>"
1052 /* rest of server: nothing */
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001053 "<td class=ac colspan=8></td>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001054 "",
1055 U2H(stats[ST_F_DREQ].u.u64), U2H(stats[ST_F_DRESP].u.u64),
1056 U2H(stats[ST_F_EREQ].u.u64),
1057 field_str(stats, ST_F_STATUS));
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001058
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001059 if (flags & STAT_SHMODULES) {
1060 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001061 chunk_appendf(out, "<td>");
1062
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001063 if (stats_px_get_cap(mod->domain_flags) & STATS_PX_CAP_LI) {
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001064 chunk_appendf(out,
1065 "<u>%s<div class=tips><table class=det>",
1066 mod->name);
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001067 for (j = 0; j < mod->stats_count; ++j) {
1068 chunk_appendf(out,
1069 "<tr><th>%s</th><td>%s</td></tr>",
1070 mod->stats[j].desc, field_to_html_str(&stats[ST_F_TOTAL_FIELDS + i]));
1071 ++i;
1072 }
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001073 chunk_appendf(out, "</table></div></u>");
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001074 } else {
1075 i += mod->stats_count;
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001076 }
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001077
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001078 chunk_appendf(out, "</td>");
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001079 }
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001080 }
1081
1082 chunk_appendf(out, "</tr>");
William Lallemand74c24fb2016-11-21 17:18:36 +01001083 }
1084 else if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_SV) {
1085 const char *style;
1086
1087 /* determine the style to use depending on the server's state,
1088 * its health and weight. There isn't a 1-to-1 mapping between
1089 * state and styles for the cases where the server is (still)
1090 * up. The reason is that we don't want to report nolb and
1091 * drain with the same color.
1092 */
1093
1094 if (strcmp(field_str(stats, ST_F_STATUS), "DOWN") == 0 ||
1095 strcmp(field_str(stats, ST_F_STATUS), "DOWN (agent)") == 0) {
1096 style = "down";
1097 }
Florian Apolloner39272c22021-03-30 13:28:35 +02001098 else if (strncmp(field_str(stats, ST_F_STATUS), "DOWN ", strlen("DOWN ")) == 0) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001099 style = "going_up";
1100 }
Daniel Corbettb4285172020-03-28 12:35:50 -04001101 else if (strcmp(field_str(stats, ST_F_STATUS), "DRAIN") == 0) {
1102 style = "draining";
1103 }
Florian Apolloner39272c22021-03-30 13:28:35 +02001104 else if (strncmp(field_str(stats, ST_F_STATUS), "NOLB ", strlen("NOLB ")) == 0) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001105 style = "going_down";
1106 }
1107 else if (strcmp(field_str(stats, ST_F_STATUS), "NOLB") == 0) {
1108 style = "nolb";
1109 }
1110 else if (strcmp(field_str(stats, ST_F_STATUS), "no check") == 0) {
1111 style = "no_check";
1112 }
1113 else if (!stats[ST_F_CHKFAIL].type ||
1114 stats[ST_F_CHECK_HEALTH].u.u32 == stats[ST_F_CHECK_RISE].u.u32 + stats[ST_F_CHECK_FALL].u.u32 - 1) {
1115 /* no check or max health = UP */
1116 if (stats[ST_F_WEIGHT].u.u32)
1117 style = "up";
1118 else
1119 style = "draining";
1120 }
1121 else {
1122 style = "going_down";
1123 }
1124
Willy Tarreau7b524852020-08-11 10:26:36 +02001125 if (strncmp(field_str(stats, ST_F_STATUS), "MAINT", 5) == 0)
William Lallemand74c24fb2016-11-21 17:18:36 +01001126 chunk_appendf(out, "<tr class=\"maintain\">");
1127 else
1128 chunk_appendf(out,
1129 "<tr class=\"%s_%s\">",
1130 (stats[ST_F_BCK].u.u32) ? "backup" : "active", style);
1131
1132
Willy Tarreauab02b3f2019-10-09 11:11:46 +02001133 if (flags & STAT_ADMIN)
William Lallemand74c24fb2016-11-21 17:18:36 +01001134 chunk_appendf(out,
David Harrigand3db35a2016-12-30 12:12:49 +00001135 "<td><input class='%s-checkbox' type=\"checkbox\" name=\"s\" value=\"%s\"></td>",
1136 field_str(stats, ST_F_PXNAME),
William Lallemand74c24fb2016-11-21 17:18:36 +01001137 field_str(stats, ST_F_SVNAME));
1138
1139 chunk_appendf(out,
1140 "<td class=ac><a name=\"%s/%s\"></a>%s"
1141 "<a class=lfsb href=\"#%s/%s\">%s</a>"
1142 "",
1143 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME),
Willy Tarreau708c4162019-10-09 10:19:16 +02001144 (flags & STAT_SHLGNDS) ? "<u>" : "",
William Lallemand74c24fb2016-11-21 17:18:36 +01001145 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME), field_str(stats, ST_F_SVNAME));
1146
Willy Tarreau708c4162019-10-09 10:19:16 +02001147 if (flags & STAT_SHLGNDS) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001148 chunk_appendf(out, "<div class=tips>");
1149
Willy Tarreau90807112020-02-25 08:16:33 +01001150 if (isdigit((unsigned char)*field_str(stats, ST_F_ADDR)))
William Lallemand74c24fb2016-11-21 17:18:36 +01001151 chunk_appendf(out, "IPv4: %s, ", field_str(stats, ST_F_ADDR));
1152 else if (*field_str(stats, ST_F_ADDR) == '[')
1153 chunk_appendf(out, "IPv6: %s, ", field_str(stats, ST_F_ADDR));
1154 else if (*field_str(stats, ST_F_ADDR))
1155 chunk_appendf(out, "%s, ", field_str(stats, ST_F_ADDR));
1156
1157 /* id */
Aurelien DARRAGON745ce8e2022-11-17 16:34:07 +01001158 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 +01001159
1160 /* cookie */
1161 if (stats[ST_F_COOKIE].type) {
1162 chunk_appendf(out, ", cookie: '");
1163 chunk_initstr(&src, field_str(stats, ST_F_COOKIE));
1164 chunk_htmlencode(out, &src);
1165 chunk_appendf(out, "'");
1166 }
1167
1168 chunk_appendf(out, "</div>");
1169 }
1170
1171 chunk_appendf(out,
1172 /* queue : current, max, limit */
1173 "%s</td><td>%s</td><td>%s</td><td>%s</td>"
1174 /* sessions rate : current, max, limit */
1175 "<td>%s</td><td>%s</td><td></td>"
1176 "",
Willy Tarreau708c4162019-10-09 10:19:16 +02001177 (flags & STAT_SHLGNDS) ? "</u>" : "",
William Lallemand74c24fb2016-11-21 17:18:36 +01001178 U2H(stats[ST_F_QCUR].u.u32), U2H(stats[ST_F_QMAX].u.u32), LIM2A(stats[ST_F_QLIMIT].u.u32, "-"),
1179 U2H(stats[ST_F_RATE].u.u32), U2H(stats[ST_F_RATE_MAX].u.u32));
1180
1181 chunk_appendf(out,
1182 /* sessions: current, max, limit, total */
Willy Tarreauf21d17b2019-09-08 09:24:56 +02001183 "<td><u>%s<div class=tips>"
1184 "<table class=det>"
1185 "<tr><th>Current active connections:</th><td>%s</td></tr>"
Willy Tarreau3bb617c2020-06-29 13:51:05 +02001186 "<tr><th>Current used connections:</th><td>%s</td></tr>"
Willy Tarreauf21d17b2019-09-08 09:24:56 +02001187 "<tr><th>Current idle connections:</th><td>%s</td></tr>"
Willy Tarreau3bb617c2020-06-29 13:51:05 +02001188 "<tr><th>- unsafe:</th><td>%s</td></tr>"
1189 "<tr><th>- safe:</th><td>%s</td></tr>"
Willy Tarreaua9fcecb2020-06-29 15:38:53 +02001190 "<tr><th>Estimated need of connections:</th><td>%s</td></tr>"
Willy Tarreauf21d17b2019-09-08 09:24:56 +02001191 "<tr><th>Active connections limit:</th><td>%s</td></tr>"
1192 "<tr><th>Idle connections limit:</th><td>%s</td></tr>"
1193 "</table></div></u>"
1194 "</td><td>%s</td><td>%s</td>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001195 "<td><u>%s<div class=tips><table class=det>"
1196 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
1197 "",
Willy Tarreauf21d17b2019-09-08 09:24:56 +02001198 U2H(stats[ST_F_SCUR].u.u32),
Willy Tarreau3bb617c2020-06-29 13:51:05 +02001199 U2H(stats[ST_F_SCUR].u.u32),
1200 U2H(stats[ST_F_USED_CONN_CUR].u.u32),
1201 U2H(stats[ST_F_SRV_ICUR].u.u32),
1202 U2H(stats[ST_F_IDLE_CONN_CUR].u.u32),
1203 U2H(stats[ST_F_SAFE_CONN_CUR].u.u32),
Willy Tarreaua9fcecb2020-06-29 15:38:53 +02001204 U2H(stats[ST_F_NEED_CONN_EST].u.u32),
Willy Tarreau3bb617c2020-06-29 13:51:05 +02001205
Willy Tarreauf21d17b2019-09-08 09:24:56 +02001206 LIM2A(stats[ST_F_SLIM].u.u32, "-"),
1207 stats[ST_F_SRV_ILIM].type ? U2H(stats[ST_F_SRV_ILIM].u.u32) : "-",
1208 U2H(stats[ST_F_SMAX].u.u32), LIM2A(stats[ST_F_SLIM].u.u32, "-"),
William Lallemand74c24fb2016-11-21 17:18:36 +01001209 U2H(stats[ST_F_STOT].u.u64),
1210 U2H(stats[ST_F_STOT].u.u64));
1211
1212 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
1213 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001214 chunk_appendf(out,
Willy Tarreauf1573842018-12-14 11:35:36 +01001215 "<tr><th>New connections:</th><td>%s</td></tr>"
1216 "<tr><th>Reused connections:</th><td>%s</td><td>(%d%%)</td></tr>"
Marcin Deranek3c27dda2020-05-15 18:32:51 +02001217 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001218 "<tr><th>- HTTP 1xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
1219 "<tr><th>- HTTP 2xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
1220 "<tr><th>- HTTP 3xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
1221 "<tr><th>- HTTP 4xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
1222 "<tr><th>- HTTP 5xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
1223 "<tr><th>- other responses:</th><td>%s</td><td>(%d%%)</td></tr>"
Willy Tarreau1b0f85e2018-05-28 15:12:40 +02001224 "<tr><th>Failed hdr rewrites:</th><td>%s</td></tr>"
Christopher Faulet0159ee42019-12-16 14:40:39 +01001225 "<tr><th>Internal error:</th><td>%s</td></tr>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001226 "",
Willy Tarreauf1573842018-12-14 11:35:36 +01001227 U2H(stats[ST_F_CONNECT].u.u64),
1228 U2H(stats[ST_F_REUSE].u.u64),
1229 (stats[ST_F_CONNECT].u.u64 + stats[ST_F_REUSE].u.u64) ?
1230 (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 +02001231 U2H(stats[ST_F_REQ_TOT].u.u64),
1232 U2H(stats[ST_F_HRSP_1XX].u.u64), stats[ST_F_REQ_TOT].u.u64 ?
1233 (int)(100 * stats[ST_F_HRSP_1XX].u.u64 / stats[ST_F_REQ_TOT].u.u64) : 0,
1234 U2H(stats[ST_F_HRSP_2XX].u.u64), stats[ST_F_REQ_TOT].u.u64 ?
1235 (int)(100 * stats[ST_F_HRSP_2XX].u.u64 / stats[ST_F_REQ_TOT].u.u64) : 0,
1236 U2H(stats[ST_F_HRSP_3XX].u.u64), stats[ST_F_REQ_TOT].u.u64 ?
1237 (int)(100 * stats[ST_F_HRSP_3XX].u.u64 / stats[ST_F_REQ_TOT].u.u64) : 0,
1238 U2H(stats[ST_F_HRSP_4XX].u.u64), stats[ST_F_REQ_TOT].u.u64 ?
1239 (int)(100 * stats[ST_F_HRSP_4XX].u.u64 / stats[ST_F_REQ_TOT].u.u64) : 0,
1240 U2H(stats[ST_F_HRSP_5XX].u.u64), stats[ST_F_REQ_TOT].u.u64 ?
1241 (int)(100 * stats[ST_F_HRSP_5XX].u.u64 / stats[ST_F_REQ_TOT].u.u64) : 0,
1242 U2H(stats[ST_F_HRSP_OTHER].u.u64), stats[ST_F_REQ_TOT].u.u64 ?
1243 (int)(100 * stats[ST_F_HRSP_OTHER].u.u64 / stats[ST_F_REQ_TOT].u.u64) : 0,
Christopher Faulet0159ee42019-12-16 14:40:39 +01001244 U2H(stats[ST_F_WREW].u.u64),
1245 U2H(stats[ST_F_EINT].u.u64));
William Lallemand74c24fb2016-11-21 17:18:36 +01001246 }
1247
Christopher Faulet0d1c2a62019-11-08 14:59:51 +01001248 chunk_appendf(out, "<tr><th colspan=3>Max / Avg over last 1024 success. conn.</th></tr>");
1249 chunk_appendf(out, "<tr><th>- Queue time:</th><td>%s / %s</td><td>ms</td></tr>",
1250 U2H(stats[ST_F_QT_MAX].u.u32), U2H(stats[ST_F_QTIME].u.u32));
1251 chunk_appendf(out, "<tr><th>- Connect time:</th><td>%s / %s</td><td>ms</td></tr>",
1252 U2H(stats[ST_F_CT_MAX].u.u32), U2H(stats[ST_F_CTIME].u.u32));
William Lallemand74c24fb2016-11-21 17:18:36 +01001253 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
Christopher Faulet0d1c2a62019-11-08 14:59:51 +01001254 chunk_appendf(out, "<tr><th>- Responses time:</th><td>%s / %s</td><td>ms</td></tr>",
1255 U2H(stats[ST_F_RT_MAX].u.u32), U2H(stats[ST_F_RTIME].u.u32));
1256 chunk_appendf(out, "<tr><th>- Total time:</th><td>%s / %s</td><td>ms</td></tr>",
1257 U2H(stats[ST_F_TT_MAX].u.u32), U2H(stats[ST_F_TTIME].u.u32));
William Lallemand74c24fb2016-11-21 17:18:36 +01001258
1259 chunk_appendf(out,
1260 "</table></div></u></td>"
1261 /* sessions: lbtot, last */
1262 "<td>%s</td><td>%s</td>",
1263 U2H(stats[ST_F_LBTOT].u.u64),
1264 human_time(stats[ST_F_LASTSESS].u.s32, 1));
1265
1266 chunk_appendf(out,
1267 /* bytes : in, out */
1268 "<td>%s</td><td>%s</td>"
1269 /* denied: req, resp */
1270 "<td></td><td>%s</td>"
1271 /* errors : request, connect */
1272 "<td></td><td>%s</td>"
1273 /* errors : response */
1274 "<td><u>%s<div class=tips>Connection resets during transfers: %lld client, %lld server</div></u></td>"
1275 /* warnings: retries, redispatches */
1276 "<td>%lld</td><td>%lld</td>"
1277 "",
1278 U2H(stats[ST_F_BIN].u.u64), U2H(stats[ST_F_BOUT].u.u64),
1279 U2H(stats[ST_F_DRESP].u.u64),
1280 U2H(stats[ST_F_ECON].u.u64),
1281 U2H(stats[ST_F_ERESP].u.u64),
1282 (long long)stats[ST_F_CLI_ABRT].u.u64,
1283 (long long)stats[ST_F_SRV_ABRT].u.u64,
1284 (long long)stats[ST_F_WRETR].u.u64,
1285 (long long)stats[ST_F_WREDIS].u.u64);
1286
1287 /* status, last change */
1288 chunk_appendf(out, "<td class=ac>");
1289
1290 /* FIXME!!!!
1291 * LASTCHG should contain the last change for *this* server and must be computed
1292 * properly above, as was done below, ie: this server if maint, otherwise ref server
1293 * if tracking. Note that ref is either local or remote depending on tracking.
1294 */
1295
1296
Willy Tarreau7b524852020-08-11 10:26:36 +02001297 if (strncmp(field_str(stats, ST_F_STATUS), "MAINT", 5) == 0) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001298 chunk_appendf(out, "%s MAINT", human_time(stats[ST_F_LASTCHG].u.u32, 1));
1299 }
Willy Tarreau7b524852020-08-11 10:26:36 +02001300 else if (strcmp(field_str(stats, ST_F_STATUS), "no check") == 0) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001301 chunk_strcat(out, "<i>no check</i>");
1302 }
1303 else {
1304 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 +02001305 if (strncmp(field_str(stats, ST_F_STATUS), "DOWN", 4) == 0) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001306 if (stats[ST_F_CHECK_HEALTH].u.u32)
1307 chunk_strcat(out, " &uarr;");
1308 }
1309 else if (stats[ST_F_CHECK_HEALTH].u.u32 < stats[ST_F_CHECK_RISE].u.u32 + stats[ST_F_CHECK_FALL].u.u32 - 1)
1310 chunk_strcat(out, " &darr;");
1311 }
1312
Willy Tarreau7b524852020-08-11 10:26:36 +02001313 if (strncmp(field_str(stats, ST_F_STATUS), "DOWN", 4) == 0 &&
William Lallemand74c24fb2016-11-21 17:18:36 +01001314 stats[ST_F_AGENT_STATUS].type && !stats[ST_F_AGENT_HEALTH].u.u32) {
1315 chunk_appendf(out,
1316 "</td><td class=ac><u> %s",
1317 field_str(stats, ST_F_AGENT_STATUS));
1318
1319 if (stats[ST_F_AGENT_CODE].type)
1320 chunk_appendf(out, "/%d", stats[ST_F_AGENT_CODE].u.u32);
1321
1322 if (stats[ST_F_AGENT_DURATION].type)
1323 chunk_appendf(out, " in %lums", (long)stats[ST_F_AGENT_DURATION].u.u64);
1324
1325 chunk_appendf(out, "<div class=tips>%s", field_str(stats, ST_F_AGENT_DESC));
1326
1327 if (*field_str(stats, ST_F_LAST_AGT)) {
1328 chunk_appendf(out, ": ");
1329 chunk_initstr(&src, field_str(stats, ST_F_LAST_AGT));
1330 chunk_htmlencode(out, &src);
1331 }
1332 chunk_appendf(out, "</div></u>");
1333 }
1334 else if (stats[ST_F_CHECK_STATUS].type) {
1335 chunk_appendf(out,
1336 "</td><td class=ac><u> %s",
1337 field_str(stats, ST_F_CHECK_STATUS));
1338
1339 if (stats[ST_F_CHECK_CODE].type)
1340 chunk_appendf(out, "/%d", stats[ST_F_CHECK_CODE].u.u32);
1341
1342 if (stats[ST_F_CHECK_DURATION].type)
1343 chunk_appendf(out, " in %lums", (long)stats[ST_F_CHECK_DURATION].u.u64);
1344
1345 chunk_appendf(out, "<div class=tips>%s", field_str(stats, ST_F_CHECK_DESC));
1346
1347 if (*field_str(stats, ST_F_LAST_CHK)) {
1348 chunk_appendf(out, ": ");
1349 chunk_initstr(&src, field_str(stats, ST_F_LAST_CHK));
1350 chunk_htmlencode(out, &src);
1351 }
1352 chunk_appendf(out, "</div></u>");
1353 }
1354 else
1355 chunk_appendf(out, "</td><td>");
1356
1357 chunk_appendf(out,
Willy Tarreaubd715102020-10-23 22:44:30 +02001358 /* weight / uweight */
1359 "</td><td class=ac>%d/%d</td>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001360 /* act, bck */
1361 "<td class=ac>%s</td><td class=ac>%s</td>"
1362 "",
Willy Tarreaubd715102020-10-23 22:44:30 +02001363 stats[ST_F_WEIGHT].u.u32, stats[ST_F_UWEIGHT].u.u32,
William Lallemand74c24fb2016-11-21 17:18:36 +01001364 stats[ST_F_BCK].u.u32 ? "-" : "Y",
1365 stats[ST_F_BCK].u.u32 ? "Y" : "-");
1366
1367 /* check failures: unique, fatal, down time */
1368 if (strcmp(field_str(stats, ST_F_STATUS), "MAINT (resolution)") == 0) {
1369 chunk_appendf(out, "<td class=ac colspan=3>resolution</td>");
1370 }
1371 else if (stats[ST_F_CHKFAIL].type) {
1372 chunk_appendf(out, "<td><u>%lld", (long long)stats[ST_F_CHKFAIL].u.u64);
1373
1374 if (stats[ST_F_HANAFAIL].type)
1375 chunk_appendf(out, "/%lld", (long long)stats[ST_F_HANAFAIL].u.u64);
1376
1377 chunk_appendf(out,
1378 "<div class=tips>Failed Health Checks%s</div></u></td>"
1379 "<td>%lld</td><td>%s</td>"
1380 "",
1381 stats[ST_F_HANAFAIL].type ? "/Health Analyses" : "",
1382 (long long)stats[ST_F_CHKDOWN].u.u64, human_time(stats[ST_F_DOWNTIME].u.u32, 1));
1383 }
1384 else if (strcmp(field_str(stats, ST_F_STATUS), "MAINT") != 0 && field_format(stats, ST_F_TRACKED) == FF_STR) {
1385 /* tracking a server (hence inherited maint would appear as "MAINT (via...)" */
1386 chunk_appendf(out,
1387 "<td class=ac colspan=3><a class=lfsb href=\"#%s\">via %s</a></td>",
1388 field_str(stats, ST_F_TRACKED), field_str(stats, ST_F_TRACKED));
1389 }
1390 else
1391 chunk_appendf(out, "<td colspan=3></td>");
1392
1393 /* throttle */
1394 if (stats[ST_F_THROTTLE].type)
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001395 chunk_appendf(out, "<td class=ac>%d %%</td>\n", stats[ST_F_THROTTLE].u.u32);
William Lallemand74c24fb2016-11-21 17:18:36 +01001396 else
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001397 chunk_appendf(out, "<td class=ac>-</td>");
1398
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001399 if (flags & STAT_SHMODULES) {
1400 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001401 chunk_appendf(out, "<td>");
1402
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001403 if (stats_px_get_cap(mod->domain_flags) & STATS_PX_CAP_SRV) {
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001404 chunk_appendf(out,
1405 "<u>%s<div class=tips><table class=det>",
1406 mod->name);
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001407 for (j = 0; j < mod->stats_count; ++j) {
1408 chunk_appendf(out,
1409 "<tr><th>%s</th><td>%s</td></tr>",
1410 mod->stats[j].desc, field_to_html_str(&stats[ST_F_TOTAL_FIELDS + i]));
1411 ++i;
1412 }
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001413 chunk_appendf(out, "</table></div></u>");
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001414 } else {
1415 i += mod->stats_count;
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001416 }
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001417
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001418 chunk_appendf(out, "</td>");
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001419 }
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001420 }
1421
1422 chunk_appendf(out, "</tr>\n");
William Lallemand74c24fb2016-11-21 17:18:36 +01001423 }
1424 else if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_BE) {
1425 chunk_appendf(out, "<tr class=\"backend\">");
Willy Tarreauab02b3f2019-10-09 11:11:46 +02001426 if (flags & STAT_ADMIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001427 /* Column sub-heading for Enable or Disable server */
1428 chunk_appendf(out, "<td></td>");
1429 }
1430 chunk_appendf(out,
1431 "<td class=ac>"
1432 /* name */
1433 "%s<a name=\"%s/Backend\"></a>"
1434 "<a class=lfsb href=\"#%s/Backend\">Backend</a>"
1435 "",
Willy Tarreau708c4162019-10-09 10:19:16 +02001436 (flags & STAT_SHLGNDS)?"<u>":"",
William Lallemand74c24fb2016-11-21 17:18:36 +01001437 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_PXNAME));
1438
Willy Tarreau708c4162019-10-09 10:19:16 +02001439 if (flags & STAT_SHLGNDS) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001440 /* balancing */
1441 chunk_appendf(out, "<div class=tips>balancing: %s",
1442 field_str(stats, ST_F_ALGO));
1443
1444 /* cookie */
1445 if (stats[ST_F_COOKIE].type) {
1446 chunk_appendf(out, ", cookie: '");
1447 chunk_initstr(&src, field_str(stats, ST_F_COOKIE));
1448 chunk_htmlencode(out, &src);
1449 chunk_appendf(out, "'");
1450 }
1451 chunk_appendf(out, "</div>");
1452 }
1453
1454 chunk_appendf(out,
1455 "%s</td>"
1456 /* queue : current, max */
1457 "<td>%s</td><td>%s</td><td></td>"
1458 /* sessions rate : current, max, limit */
1459 "<td>%s</td><td>%s</td><td></td>"
1460 "",
Willy Tarreau708c4162019-10-09 10:19:16 +02001461 (flags & STAT_SHLGNDS)?"</u>":"",
William Lallemand74c24fb2016-11-21 17:18:36 +01001462 U2H(stats[ST_F_QCUR].u.u32), U2H(stats[ST_F_QMAX].u.u32),
1463 U2H(stats[ST_F_RATE].u.u32), U2H(stats[ST_F_RATE_MAX].u.u32));
1464
1465 chunk_appendf(out,
1466 /* sessions: current, max, limit, total */
1467 "<td>%s</td><td>%s</td><td>%s</td>"
1468 "<td><u>%s<div class=tips><table class=det>"
1469 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
1470 "",
Willy Tarreau8e0f1752016-12-12 15:07:29 +01001471 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 +01001472 U2H(stats[ST_F_STOT].u.u64),
1473 U2H(stats[ST_F_STOT].u.u64));
1474
1475 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
1476 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0) {
1477 chunk_appendf(out,
Willy Tarreauf1573842018-12-14 11:35:36 +01001478 "<tr><th>New connections:</th><td>%s</td></tr>"
1479 "<tr><th>Reused connections:</th><td>%s</td><td>(%d%%)</td></tr>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001480 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
1481 "<tr><th>- HTTP 1xx responses:</th><td>%s</td></tr>"
1482 "<tr><th>- HTTP 2xx responses:</th><td>%s</td></tr>"
1483 "<tr><th>&nbsp;&nbsp;Compressed 2xx:</th><td>%s</td><td>(%d%%)</td></tr>"
1484 "<tr><th>- HTTP 3xx responses:</th><td>%s</td></tr>"
1485 "<tr><th>- HTTP 4xx responses:</th><td>%s</td></tr>"
1486 "<tr><th>- HTTP 5xx responses:</th><td>%s</td></tr>"
1487 "<tr><th>- other responses:</th><td>%s</td></tr>"
Willy Tarreaua1214a52018-12-14 14:00:25 +01001488 "<tr><th>Cache lookups:</th><td>%s</td></tr>"
1489 "<tr><th>Cache hits:</th><td>%s</td><td>(%d%%)</td></tr>"
Willy Tarreau1b0f85e2018-05-28 15:12:40 +02001490 "<tr><th>Failed hdr rewrites:</th><td>%s</td></tr>"
Christopher Faulet0159ee42019-12-16 14:40:39 +01001491 "<tr><th>Internal errors:</th><td>%s</td></tr>"
Christopher Faulet0d1c2a62019-11-08 14:59:51 +01001492 "",
Willy Tarreauf1573842018-12-14 11:35:36 +01001493 U2H(stats[ST_F_CONNECT].u.u64),
1494 U2H(stats[ST_F_REUSE].u.u64),
1495 (stats[ST_F_CONNECT].u.u64 + stats[ST_F_REUSE].u.u64) ?
1496 (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 +01001497 U2H(stats[ST_F_REQ_TOT].u.u64),
1498 U2H(stats[ST_F_HRSP_1XX].u.u64),
1499 U2H(stats[ST_F_HRSP_2XX].u.u64),
1500 U2H(stats[ST_F_COMP_RSP].u.u64),
1501 stats[ST_F_HRSP_2XX].u.u64 ?
1502 (int)(100 * stats[ST_F_COMP_RSP].u.u64 / stats[ST_F_HRSP_2XX].u.u64) : 0,
1503 U2H(stats[ST_F_HRSP_3XX].u.u64),
1504 U2H(stats[ST_F_HRSP_4XX].u.u64),
1505 U2H(stats[ST_F_HRSP_5XX].u.u64),
Willy Tarreaufeead3a2018-12-14 13:48:44 +01001506 U2H(stats[ST_F_HRSP_OTHER].u.u64),
Willy Tarreaua1214a52018-12-14 14:00:25 +01001507 U2H(stats[ST_F_CACHE_LOOKUPS].u.u64),
1508 U2H(stats[ST_F_CACHE_HITS].u.u64),
1509 stats[ST_F_CACHE_LOOKUPS].u.u64 ?
1510 (int)(100 * stats[ST_F_CACHE_HITS].u.u64 / stats[ST_F_CACHE_LOOKUPS].u.u64) : 0,
Christopher Faulet0159ee42019-12-16 14:40:39 +01001511 U2H(stats[ST_F_WREW].u.u64),
1512 U2H(stats[ST_F_EINT].u.u64));
William Lallemand74c24fb2016-11-21 17:18:36 +01001513 }
1514
Christopher Faulet0d1c2a62019-11-08 14:59:51 +01001515 chunk_appendf(out, "<tr><th colspan=3>Max / Avg over last 1024 success. conn.</th></tr>");
1516 chunk_appendf(out, "<tr><th>- Queue time:</th><td>%s / %s</td><td>ms</td></tr>",
1517 U2H(stats[ST_F_QT_MAX].u.u32), U2H(stats[ST_F_QTIME].u.u32));
1518 chunk_appendf(out, "<tr><th>- Connect time:</th><td>%s / %s</td><td>ms</td></tr>",
1519 U2H(stats[ST_F_CT_MAX].u.u32), U2H(stats[ST_F_CTIME].u.u32));
William Lallemand74c24fb2016-11-21 17:18:36 +01001520 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
Christopher Faulet0d1c2a62019-11-08 14:59:51 +01001521 chunk_appendf(out, "<tr><th>- Responses time:</th><td>%s / %s</td><td>ms</td></tr>",
1522 U2H(stats[ST_F_RT_MAX].u.u32), U2H(stats[ST_F_RTIME].u.u32));
1523 chunk_appendf(out, "<tr><th>- Total time:</th><td>%s / %s</td><td>ms</td></tr>",
1524 U2H(stats[ST_F_TT_MAX].u.u32), U2H(stats[ST_F_TTIME].u.u32));
William Lallemand74c24fb2016-11-21 17:18:36 +01001525
1526 chunk_appendf(out,
1527 "</table></div></u></td>"
1528 /* sessions: lbtot, last */
1529 "<td>%s</td><td>%s</td>"
1530 /* bytes: in */
1531 "<td>%s</td>"
1532 "",
1533 U2H(stats[ST_F_LBTOT].u.u64),
1534 human_time(stats[ST_F_LASTSESS].u.s32, 1),
1535 U2H(stats[ST_F_BIN].u.u64));
1536
1537 chunk_appendf(out,
1538 /* bytes:out + compression stats (via hover): comp_in, comp_out, comp_byp */
1539 "<td>%s%s<div class=tips><table class=det>"
1540 "<tr><th>Response bytes in:</th><td>%s</td></tr>"
1541 "<tr><th>Compression in:</th><td>%s</td></tr>"
1542 "<tr><th>Compression out:</th><td>%s</td><td>(%d%%)</td></tr>"
1543 "<tr><th>Compression bypass:</th><td>%s</td></tr>"
1544 "<tr><th>Total bytes saved:</th><td>%s</td><td>(%d%%)</td></tr>"
1545 "</table></div>%s</td>",
1546 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "<u>":"",
1547 U2H(stats[ST_F_BOUT].u.u64),
1548 U2H(stats[ST_F_BOUT].u.u64),
1549 U2H(stats[ST_F_COMP_IN].u.u64),
1550 U2H(stats[ST_F_COMP_OUT].u.u64),
1551 stats[ST_F_COMP_IN].u.u64 ? (int)(stats[ST_F_COMP_OUT].u.u64 * 100 / stats[ST_F_COMP_IN].u.u64) : 0,
1552 U2H(stats[ST_F_COMP_BYP].u.u64),
1553 U2H(stats[ST_F_COMP_IN].u.u64 - stats[ST_F_COMP_OUT].u.u64),
1554 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,
1555 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "</u>":"");
1556
1557 chunk_appendf(out,
1558 /* denied: req, resp */
1559 "<td>%s</td><td>%s</td>"
1560 /* errors : request, connect */
1561 "<td></td><td>%s</td>"
1562 /* errors : response */
1563 "<td><u>%s<div class=tips>Connection resets during transfers: %lld client, %lld server</div></u></td>"
1564 /* warnings: retries, redispatches */
1565 "<td>%lld</td><td>%lld</td>"
1566 /* backend status: reflect backend status (up/down): we display UP
1567 * if the backend has known working servers or if it has no server at
1568 * all (eg: for stats). Then we display the total weight, number of
1569 * active and backups. */
Willy Tarreaubd715102020-10-23 22:44:30 +02001570 "<td class=ac>%s %s</td><td class=ac>&nbsp;</td><td class=ac>%d/%d</td>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001571 "<td class=ac>%d</td><td class=ac>%d</td>"
1572 "",
1573 U2H(stats[ST_F_DREQ].u.u64), U2H(stats[ST_F_DRESP].u.u64),
1574 U2H(stats[ST_F_ECON].u.u64),
1575 U2H(stats[ST_F_ERESP].u.u64),
1576 (long long)stats[ST_F_CLI_ABRT].u.u64,
1577 (long long)stats[ST_F_SRV_ABRT].u.u64,
1578 (long long)stats[ST_F_WRETR].u.u64, (long long)stats[ST_F_WREDIS].u.u64,
1579 human_time(stats[ST_F_LASTCHG].u.u32, 1),
1580 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 +02001581 stats[ST_F_WEIGHT].u.u32, stats[ST_F_UWEIGHT].u.u32,
William Lallemand74c24fb2016-11-21 17:18:36 +01001582 stats[ST_F_ACT].u.u32, stats[ST_F_BCK].u.u32);
1583
1584 chunk_appendf(out,
1585 /* rest of backend: nothing, down transitions, total downtime, throttle */
1586 "<td class=ac>&nbsp;</td><td>%d</td>"
1587 "<td>%s</td>"
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001588 "<td></td>",
William Lallemand74c24fb2016-11-21 17:18:36 +01001589 stats[ST_F_CHKDOWN].u.u32,
1590 stats[ST_F_DOWNTIME].type ? human_time(stats[ST_F_DOWNTIME].u.u32, 1) : "&nbsp;");
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001591
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001592 if (flags & STAT_SHMODULES) {
1593 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001594 chunk_appendf(out, "<td>");
1595
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001596 if (stats_px_get_cap(mod->domain_flags) & STATS_PX_CAP_BE) {
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001597 chunk_appendf(out,
1598 "<u>%s<div class=tips><table class=det>",
1599 mod->name);
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001600 for (j = 0; j < mod->stats_count; ++j) {
1601 chunk_appendf(out,
1602 "<tr><th>%s</th><td>%s</td></tr>",
1603 mod->stats[j].desc, field_to_html_str(&stats[ST_F_TOTAL_FIELDS + i]));
1604 ++i;
1605 }
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001606 chunk_appendf(out, "</table></div></u>");
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001607 } else {
1608 i += mod->stats_count;
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001609 }
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001610
1611 chunk_appendf(out, "</td>");
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001612 }
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001613 }
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001614
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001615 chunk_appendf(out, "</tr>");
William Lallemand74c24fb2016-11-21 17:18:36 +01001616 }
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001617
William Lallemand74c24fb2016-11-21 17:18:36 +01001618 return 1;
1619}
1620
Amaury Denoyelle97323c92020-10-02 18:32:01 +02001621int stats_dump_one_line(const struct field *stats, size_t stats_count,
1622 struct appctx *appctx)
William Lallemand74c24fb2016-11-21 17:18:36 +01001623{
Willy Tarreau91cefca2022-05-03 17:08:29 +02001624 struct show_stat_ctx *ctx = appctx->svcctx;
Simon Horman05ee2132017-01-04 09:37:25 +01001625 int ret;
1626
Willy Tarreau91cefca2022-05-03 17:08:29 +02001627 if (ctx->flags & STAT_FMT_HTML)
1628 ret = stats_dump_fields_html(&trash, stats, ctx->flags);
1629 else if (ctx->flags & STAT_FMT_TYPED)
1630 ret = stats_dump_fields_typed(&trash, stats, stats_count, ctx->flags, ctx->domain);
1631 else if (ctx->flags & STAT_FMT_JSON)
1632 ret = stats_dump_fields_json(&trash, stats, stats_count, ctx->flags, ctx->domain);
William Lallemand74c24fb2016-11-21 17:18:36 +01001633 else
Willy Tarreau91cefca2022-05-03 17:08:29 +02001634 ret = stats_dump_fields_csv(&trash, stats, stats_count, ctx->flags, ctx->domain);
Simon Horman05ee2132017-01-04 09:37:25 +01001635
1636 if (ret)
Willy Tarreau91cefca2022-05-03 17:08:29 +02001637 ctx->flags |= STAT_STARTED;
Simon Horman05ee2132017-01-04 09:37:25 +01001638
1639 return ret;
William Lallemand74c24fb2016-11-21 17:18:36 +01001640}
1641
William Dauchy0ef54392021-01-17 18:27:45 +01001642/* Fill <stats> with the frontend statistics. <stats> is preallocated array of
Christopher Fauletd808f172021-01-22 17:33:22 +01001643 * length <len>. If <selected_field> is != NULL, only fill this one. The length
1644 * of the array must be at least ST_F_TOTAL_FIELDS. If this length is less than
1645 * this value, or if the selected field is not implemented for frontends, the
1646 * function returns 0, otherwise, it returns 1.
William Lallemand74c24fb2016-11-21 17:18:36 +01001647 */
William Dauchy0ef54392021-01-17 18:27:45 +01001648int stats_fill_fe_stats(struct proxy *px, struct field *stats, int len,
1649 enum stat_field *selected_field)
William Lallemand74c24fb2016-11-21 17:18:36 +01001650{
William Dauchy0ef54392021-01-17 18:27:45 +01001651 enum stat_field current_field = (selected_field != NULL ? *selected_field : 0);
William Dauchy0ef54392021-01-17 18:27:45 +01001652
William Lallemand74c24fb2016-11-21 17:18:36 +01001653 if (len < ST_F_TOTAL_FIELDS)
1654 return 0;
1655
William Dauchy0ef54392021-01-17 18:27:45 +01001656 for (; current_field < ST_F_TOTAL_FIELDS; current_field++) {
Christopher Faulet8596bfb2021-01-25 15:16:41 +01001657 struct field metric = { 0 };
1658
William Dauchy0ef54392021-01-17 18:27:45 +01001659 switch (current_field) {
1660 case ST_F_PXNAME:
1661 metric = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
1662 break;
1663 case ST_F_SVNAME:
1664 metric = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, "FRONTEND");
1665 break;
1666 case ST_F_MODE:
1667 metric = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
1668 break;
1669 case ST_F_SCUR:
1670 metric = mkf_u32(0, px->feconn);
1671 break;
1672 case ST_F_SMAX:
1673 metric = mkf_u32(FN_MAX, px->fe_counters.conn_max);
1674 break;
1675 case ST_F_SLIM:
1676 metric = mkf_u32(FO_CONFIG|FN_LIMIT, px->maxconn);
1677 break;
1678 case ST_F_STOT:
1679 metric = mkf_u64(FN_COUNTER, px->fe_counters.cum_sess);
1680 break;
1681 case ST_F_BIN:
1682 metric = mkf_u64(FN_COUNTER, px->fe_counters.bytes_in);
1683 break;
1684 case ST_F_BOUT:
1685 metric = mkf_u64(FN_COUNTER, px->fe_counters.bytes_out);
1686 break;
1687 case ST_F_DREQ:
1688 metric = mkf_u64(FN_COUNTER, px->fe_counters.denied_req);
1689 break;
1690 case ST_F_DRESP:
1691 metric = mkf_u64(FN_COUNTER, px->fe_counters.denied_resp);
1692 break;
1693 case ST_F_EREQ:
1694 metric = mkf_u64(FN_COUNTER, px->fe_counters.failed_req);
1695 break;
1696 case ST_F_DCON:
1697 metric = mkf_u64(FN_COUNTER, px->fe_counters.denied_conn);
1698 break;
1699 case ST_F_DSES:
1700 metric = mkf_u64(FN_COUNTER, px->fe_counters.denied_sess);
1701 break;
Aurelien DARRAGONcddec0a2022-09-09 15:58:57 +02001702 case ST_F_STATUS: {
1703 const char *state;
1704
1705 if (px->flags & (PR_FL_DISABLED|PR_FL_STOPPED))
1706 state = "STOP";
1707 else if (px->flags & PR_FL_PAUSED)
1708 state = "PAUSED";
1709 else
1710 state = "OPEN";
1711 metric = mkf_str(FO_STATUS, state);
William Dauchy0ef54392021-01-17 18:27:45 +01001712 break;
Aurelien DARRAGONcddec0a2022-09-09 15:58:57 +02001713 }
William Dauchy0ef54392021-01-17 18:27:45 +01001714 case ST_F_PID:
Willy Tarreaue8422bf2021-06-15 09:08:18 +02001715 metric = mkf_u32(FO_KEY, 1);
William Dauchy0ef54392021-01-17 18:27:45 +01001716 break;
1717 case ST_F_IID:
1718 metric = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
1719 break;
1720 case ST_F_SID:
1721 metric = mkf_u32(FO_KEY|FS_SERVICE, 0);
1722 break;
1723 case ST_F_TYPE:
1724 metric = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_FE);
1725 break;
1726 case ST_F_RATE:
1727 metric = mkf_u32(FN_RATE, read_freq_ctr(&px->fe_sess_per_sec));
1728 break;
1729 case ST_F_RATE_LIM:
1730 metric = mkf_u32(FO_CONFIG|FN_LIMIT, px->fe_sps_lim);
1731 break;
1732 case ST_F_RATE_MAX:
1733 metric = mkf_u32(FN_MAX, px->fe_counters.sps_max);
1734 break;
1735 case ST_F_WREW:
1736 metric = mkf_u64(FN_COUNTER, px->fe_counters.failed_rewrites);
1737 break;
1738 case ST_F_EINT:
1739 metric = mkf_u64(FN_COUNTER, px->fe_counters.internal_errors);
1740 break;
1741 case ST_F_HRSP_1XX:
William Dauchy2107a0f2021-01-22 21:09:48 +01001742 if (px->mode == PR_MODE_HTTP)
1743 metric = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[1]);
William Dauchy0ef54392021-01-17 18:27:45 +01001744 break;
1745 case ST_F_HRSP_2XX:
William Dauchy2107a0f2021-01-22 21:09:48 +01001746 if (px->mode == PR_MODE_HTTP)
1747 metric = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[2]);
William Dauchy0ef54392021-01-17 18:27:45 +01001748 break;
1749 case ST_F_HRSP_3XX:
William Dauchy2107a0f2021-01-22 21:09:48 +01001750 if (px->mode == PR_MODE_HTTP)
1751 metric = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[3]);
William Dauchy0ef54392021-01-17 18:27:45 +01001752 break;
1753 case ST_F_HRSP_4XX:
William Dauchy2107a0f2021-01-22 21:09:48 +01001754 if (px->mode == PR_MODE_HTTP)
1755 metric = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[4]);
William Dauchy0ef54392021-01-17 18:27:45 +01001756 break;
1757 case ST_F_HRSP_5XX:
William Dauchy2107a0f2021-01-22 21:09:48 +01001758 if (px->mode == PR_MODE_HTTP)
1759 metric = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[5]);
William Dauchy0ef54392021-01-17 18:27:45 +01001760 break;
1761 case ST_F_HRSP_OTHER:
William Dauchy2107a0f2021-01-22 21:09:48 +01001762 if (px->mode == PR_MODE_HTTP)
1763 metric = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[0]);
William Dauchy0ef54392021-01-17 18:27:45 +01001764 break;
1765 case ST_F_INTERCEPTED:
William Dauchy2107a0f2021-01-22 21:09:48 +01001766 if (px->mode == PR_MODE_HTTP)
1767 metric = mkf_u64(FN_COUNTER, px->fe_counters.intercepted_req);
William Dauchy0ef54392021-01-17 18:27:45 +01001768 break;
1769 case ST_F_CACHE_LOOKUPS:
William Dauchy2107a0f2021-01-22 21:09:48 +01001770 if (px->mode == PR_MODE_HTTP)
1771 metric = mkf_u64(FN_COUNTER, px->fe_counters.p.http.cache_lookups);
William Dauchy0ef54392021-01-17 18:27:45 +01001772 break;
1773 case ST_F_CACHE_HITS:
William Dauchy2107a0f2021-01-22 21:09:48 +01001774 if (px->mode == PR_MODE_HTTP)
1775 metric = mkf_u64(FN_COUNTER, px->fe_counters.p.http.cache_hits);
William Dauchy0ef54392021-01-17 18:27:45 +01001776 break;
1777 case ST_F_REQ_RATE:
1778 metric = mkf_u32(FN_RATE, read_freq_ctr(&px->fe_req_per_sec));
1779 break;
1780 case ST_F_REQ_RATE_MAX:
1781 metric = mkf_u32(FN_MAX, px->fe_counters.p.http.rps_max);
1782 break;
1783 case ST_F_REQ_TOT:
1784 metric = mkf_u64(FN_COUNTER, px->fe_counters.p.http.cum_req);
1785 break;
1786 case ST_F_COMP_IN:
1787 metric = mkf_u64(FN_COUNTER, px->fe_counters.comp_in);
1788 break;
1789 case ST_F_COMP_OUT:
1790 metric = mkf_u64(FN_COUNTER, px->fe_counters.comp_out);
1791 break;
1792 case ST_F_COMP_BYP:
1793 metric = mkf_u64(FN_COUNTER, px->fe_counters.comp_byp);
1794 break;
1795 case ST_F_COMP_RSP:
1796 metric = mkf_u64(FN_COUNTER, px->fe_counters.p.http.comp_rsp);
1797 break;
1798 case ST_F_CONN_RATE:
1799 metric = mkf_u32(FN_RATE, read_freq_ctr(&px->fe_conn_per_sec));
1800 break;
1801 case ST_F_CONN_RATE_MAX:
1802 metric = mkf_u32(FN_MAX, px->fe_counters.cps_max);
1803 break;
1804 case ST_F_CONN_TOT:
1805 metric = mkf_u64(FN_COUNTER, px->fe_counters.cum_conn);
1806 break;
1807 default:
Christopher Fauletd808f172021-01-22 17:33:22 +01001808 /* not used for frontends. If a specific metric
1809 * is requested, return an error. Otherwise continue.
1810 */
1811 if (selected_field != NULL)
1812 return 0;
1813 continue;
William Dauchy0ef54392021-01-17 18:27:45 +01001814 }
1815 stats[current_field] = metric;
1816 if (selected_field != NULL)
1817 break;
William Lallemand74c24fb2016-11-21 17:18:36 +01001818 }
William Lallemand74c24fb2016-11-21 17:18:36 +01001819 return 1;
1820}
1821
1822/* Dumps a frontend's line to the trash for the current proxy <px> and uses
Willy Tarreaucaff6312022-05-27 10:17:46 +02001823 * the state from stream connector <sc>. The caller is responsible for clearing
William Lallemand74c24fb2016-11-21 17:18:36 +01001824 * the trash if needed. Returns non-zero if it emits anything, zero otherwise.
1825 */
Willy Tarreaucaff6312022-05-27 10:17:46 +02001826static int stats_dump_fe_stats(struct stconn *sc, struct proxy *px)
William Lallemand74c24fb2016-11-21 17:18:36 +01001827{
Willy Tarreaucaff6312022-05-27 10:17:46 +02001828 struct appctx *appctx = __sc_appctx(sc);
Willy Tarreau91cefca2022-05-03 17:08:29 +02001829 struct show_stat_ctx *ctx = appctx->svcctx;
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02001830 struct field *stats = stat_l[STATS_DOMAIN_PROXY];
1831 struct stats_module *mod;
1832 size_t stats_count = ST_F_TOTAL_FIELDS;
William Lallemand74c24fb2016-11-21 17:18:36 +01001833
1834 if (!(px->cap & PR_CAP_FE))
1835 return 0;
1836
Willy Tarreau91cefca2022-05-03 17:08:29 +02001837 if ((ctx->flags & STAT_BOUND) && !(ctx->type & (1 << STATS_TYPE_FE)))
William Lallemand74c24fb2016-11-21 17:18:36 +01001838 return 0;
1839
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02001840 memset(stats, 0, sizeof(struct field) * stat_count[STATS_DOMAIN_PROXY]);
1841
William Dauchy0ef54392021-01-17 18:27:45 +01001842 if (!stats_fill_fe_stats(px, stats, ST_F_TOTAL_FIELDS, NULL))
William Lallemand74c24fb2016-11-21 17:18:36 +01001843 return 0;
1844
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02001845 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
1846 void *counters;
1847
1848 if (!(stats_px_get_cap(mod->domain_flags) & STATS_PX_CAP_FE)) {
1849 stats_count += mod->stats_count;
1850 continue;
1851 }
1852
1853 counters = EXTRA_COUNTERS_GET(px->extra_counters_fe, mod);
1854 mod->fill_stats(counters, stats + stats_count);
1855 stats_count += mod->stats_count;
1856 }
1857
1858 return stats_dump_one_line(stats, stats_count, appctx);
William Lallemand74c24fb2016-11-21 17:18:36 +01001859}
1860
William Dauchy655e14e2021-02-14 23:22:54 +01001861/* Fill <stats> with the listener statistics. <stats> is preallocated array of
1862 * length <len>. The length of the array must be at least ST_F_TOTAL_FIELDS. If
1863 * this length is less then this value, the function returns 0, otherwise, it
1864 * returns 1. If selected_field is != NULL, only fill this one. <flags> can
1865 * take the value STAT_SHLGNDS.
William Lallemand74c24fb2016-11-21 17:18:36 +01001866 */
1867int stats_fill_li_stats(struct proxy *px, struct listener *l, int flags,
William Dauchy655e14e2021-02-14 23:22:54 +01001868 struct field *stats, int len, enum stat_field *selected_field)
William Lallemand74c24fb2016-11-21 17:18:36 +01001869{
William Dauchy655e14e2021-02-14 23:22:54 +01001870 enum stat_field current_field = (selected_field != NULL ? *selected_field : 0);
Willy Tarreau83061a82018-07-13 11:56:34 +02001871 struct buffer *out = get_trash_chunk();
William Lallemand74c24fb2016-11-21 17:18:36 +01001872
1873 if (len < ST_F_TOTAL_FIELDS)
1874 return 0;
1875
1876 if (!l->counters)
1877 return 0;
1878
1879 chunk_reset(out);
William Lallemand74c24fb2016-11-21 17:18:36 +01001880
William Dauchy655e14e2021-02-14 23:22:54 +01001881 for (; current_field < ST_F_TOTAL_FIELDS; current_field++) {
1882 struct field metric = { 0 };
William Lallemand74c24fb2016-11-21 17:18:36 +01001883
William Dauchy655e14e2021-02-14 23:22:54 +01001884 switch (current_field) {
1885 case ST_F_PXNAME:
1886 metric = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
1887 break;
1888 case ST_F_SVNAME:
1889 metric = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, l->name);
1890 break;
1891 case ST_F_MODE:
1892 metric = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
1893 break;
1894 case ST_F_SCUR:
1895 metric = mkf_u32(0, l->nbconn);
1896 break;
1897 case ST_F_SMAX:
1898 metric = mkf_u32(FN_MAX, l->counters->conn_max);
1899 break;
1900 case ST_F_SLIM:
1901 metric = mkf_u32(FO_CONFIG|FN_LIMIT, l->maxconn);
1902 break;
1903 case ST_F_STOT:
1904 metric = mkf_u64(FN_COUNTER, l->counters->cum_conn);
1905 break;
1906 case ST_F_BIN:
1907 metric = mkf_u64(FN_COUNTER, l->counters->bytes_in);
1908 break;
1909 case ST_F_BOUT:
1910 metric = mkf_u64(FN_COUNTER, l->counters->bytes_out);
1911 break;
1912 case ST_F_DREQ:
1913 metric = mkf_u64(FN_COUNTER, l->counters->denied_req);
1914 break;
1915 case ST_F_DRESP:
1916 metric = mkf_u64(FN_COUNTER, l->counters->denied_resp);
1917 break;
1918 case ST_F_EREQ:
1919 metric = mkf_u64(FN_COUNTER, l->counters->failed_req);
1920 break;
1921 case ST_F_DCON:
1922 metric = mkf_u64(FN_COUNTER, l->counters->denied_conn);
1923 break;
1924 case ST_F_DSES:
1925 metric = mkf_u64(FN_COUNTER, l->counters->denied_sess);
1926 break;
1927 case ST_F_STATUS:
William Dauchy3679d0c2021-02-14 23:22:55 +01001928 metric = mkf_str(FO_STATUS, li_status_st[get_li_status(l)]);
William Dauchy655e14e2021-02-14 23:22:54 +01001929 break;
1930 case ST_F_PID:
Willy Tarreaue8422bf2021-06-15 09:08:18 +02001931 metric = mkf_u32(FO_KEY, 1);
William Dauchy655e14e2021-02-14 23:22:54 +01001932 break;
1933 case ST_F_IID:
1934 metric = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
1935 break;
1936 case ST_F_SID:
1937 metric = mkf_u32(FO_KEY|FS_SERVICE, l->luid);
1938 break;
1939 case ST_F_TYPE:
1940 metric = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_SO);
1941 break;
1942 case ST_F_WREW:
1943 metric = mkf_u64(FN_COUNTER, l->counters->failed_rewrites);
1944 break;
1945 case ST_F_EINT:
1946 metric = mkf_u64(FN_COUNTER, l->counters->internal_errors);
1947 break;
1948 case ST_F_ADDR:
1949 if (flags & STAT_SHLGNDS) {
1950 char str[INET6_ADDRSTRLEN];
1951 int port;
William Lallemand74c24fb2016-11-21 17:18:36 +01001952
William Dauchy655e14e2021-02-14 23:22:54 +01001953 port = get_host_port(&l->rx.addr);
1954 switch (addr_to_str(&l->rx.addr, str, sizeof(str))) {
1955 case AF_INET:
1956 metric = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
1957 chunk_appendf(out, "%s:%d", str, port);
1958 break;
1959 case AF_INET6:
1960 metric = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
1961 chunk_appendf(out, "[%s]:%d", str, port);
1962 break;
1963 case AF_UNIX:
1964 metric = mkf_str(FO_CONFIG|FS_SERVICE, "unix");
1965 break;
1966 case -1:
1967 metric = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
1968 chunk_strcat(out, strerror(errno));
1969 break;
1970 default: /* address family not supported */
1971 break;
1972 }
1973 }
1974 break;
1975 default:
1976 /* not used for listen. If a specific metric
1977 * is requested, return an error. Otherwise continue.
1978 */
1979 if (selected_field != NULL)
1980 return 0;
1981 continue;
William Lallemand74c24fb2016-11-21 17:18:36 +01001982 }
William Dauchy655e14e2021-02-14 23:22:54 +01001983 stats[current_field] = metric;
1984 if (selected_field != NULL)
1985 break;
William Lallemand74c24fb2016-11-21 17:18:36 +01001986 }
William Lallemand74c24fb2016-11-21 17:18:36 +01001987 return 1;
1988}
1989
1990/* Dumps a line for listener <l> and proxy <px> to the trash and uses the state
Willy Tarreaucaff6312022-05-27 10:17:46 +02001991 * from stream connector <sc>. The caller is responsible for clearing the trash
Willy Tarreau43241ff2019-10-09 11:27:51 +02001992 * if needed. Returns non-zero if it emits anything, zero otherwise.
William Lallemand74c24fb2016-11-21 17:18:36 +01001993 */
Willy Tarreaucaff6312022-05-27 10:17:46 +02001994static int stats_dump_li_stats(struct stconn *sc, struct proxy *px, struct listener *l)
William Lallemand74c24fb2016-11-21 17:18:36 +01001995{
Willy Tarreaucaff6312022-05-27 10:17:46 +02001996 struct appctx *appctx = __sc_appctx(sc);
Willy Tarreau91cefca2022-05-03 17:08:29 +02001997 struct show_stat_ctx *ctx = appctx->svcctx;
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02001998 struct field *stats = stat_l[STATS_DOMAIN_PROXY];
1999 struct stats_module *mod;
2000 size_t stats_count = ST_F_TOTAL_FIELDS;
2001
2002 memset(stats, 0, sizeof(struct field) * stat_count[STATS_DOMAIN_PROXY]);
William Lallemand74c24fb2016-11-21 17:18:36 +01002003
Willy Tarreau91cefca2022-05-03 17:08:29 +02002004 if (!stats_fill_li_stats(px, l, ctx->flags, stats,
William Dauchy655e14e2021-02-14 23:22:54 +01002005 ST_F_TOTAL_FIELDS, NULL))
William Lallemand74c24fb2016-11-21 17:18:36 +01002006 return 0;
2007
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02002008 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
2009 void *counters;
2010
2011 if (!(stats_px_get_cap(mod->domain_flags) & STATS_PX_CAP_LI)) {
2012 stats_count += mod->stats_count;
2013 continue;
2014 }
2015
2016 counters = EXTRA_COUNTERS_GET(l->extra_counters, mod);
2017 mod->fill_stats(counters, stats + stats_count);
2018 stats_count += mod->stats_count;
2019 }
2020
2021 return stats_dump_one_line(stats, stats_count, appctx);
William Lallemand74c24fb2016-11-21 17:18:36 +01002022}
2023
2024enum srv_stats_state {
2025 SRV_STATS_STATE_DOWN = 0,
2026 SRV_STATS_STATE_DOWN_AGENT,
2027 SRV_STATS_STATE_GOING_UP,
2028 SRV_STATS_STATE_UP_GOING_DOWN,
2029 SRV_STATS_STATE_UP,
2030 SRV_STATS_STATE_NOLB_GOING_DOWN,
2031 SRV_STATS_STATE_NOLB,
2032 SRV_STATS_STATE_DRAIN_GOING_DOWN,
2033 SRV_STATS_STATE_DRAIN,
2034 SRV_STATS_STATE_DRAIN_AGENT,
2035 SRV_STATS_STATE_NO_CHECK,
2036
2037 SRV_STATS_STATE_COUNT, /* Must be last */
2038};
2039
2040static const char *srv_hlt_st[SRV_STATS_STATE_COUNT] = {
2041 [SRV_STATS_STATE_DOWN] = "DOWN",
2042 [SRV_STATS_STATE_DOWN_AGENT] = "DOWN (agent)",
2043 [SRV_STATS_STATE_GOING_UP] = "DOWN %d/%d",
2044 [SRV_STATS_STATE_UP_GOING_DOWN] = "UP %d/%d",
2045 [SRV_STATS_STATE_UP] = "UP",
2046 [SRV_STATS_STATE_NOLB_GOING_DOWN] = "NOLB %d/%d",
2047 [SRV_STATS_STATE_NOLB] = "NOLB",
2048 [SRV_STATS_STATE_DRAIN_GOING_DOWN] = "DRAIN %d/%d",
2049 [SRV_STATS_STATE_DRAIN] = "DRAIN",
2050 [SRV_STATS_STATE_DRAIN_AGENT] = "DRAIN (agent)",
2051 [SRV_STATS_STATE_NO_CHECK] = "no check"
2052};
2053
William Dauchyd3a9a492021-01-25 17:29:03 +01002054/* Compute server state helper
William Lallemand74c24fb2016-11-21 17:18:36 +01002055 */
William Dauchyd3a9a492021-01-25 17:29:03 +01002056static void stats_fill_sv_stats_computestate(struct server *sv, struct server *ref,
2057 enum srv_stats_state *state)
William Lallemand74c24fb2016-11-21 17:18:36 +01002058{
Emeric Brun52a91d32017-08-31 14:41:55 +02002059 if (sv->cur_state == SRV_ST_RUNNING || sv->cur_state == SRV_ST_STARTING) {
William Lallemand74c24fb2016-11-21 17:18:36 +01002060 if ((ref->check.state & CHK_ST_ENABLED) &&
2061 (ref->check.health < ref->check.rise + ref->check.fall - 1)) {
William Dauchyd3a9a492021-01-25 17:29:03 +01002062 *state = SRV_STATS_STATE_UP_GOING_DOWN;
William Lallemand74c24fb2016-11-21 17:18:36 +01002063 } else {
William Dauchyd3a9a492021-01-25 17:29:03 +01002064 *state = SRV_STATS_STATE_UP;
William Lallemand74c24fb2016-11-21 17:18:36 +01002065 }
2066
Emeric Brun52a91d32017-08-31 14:41:55 +02002067 if (sv->cur_admin & SRV_ADMF_DRAIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +01002068 if (ref->agent.state & CHK_ST_ENABLED)
William Dauchyd3a9a492021-01-25 17:29:03 +01002069 *state = SRV_STATS_STATE_DRAIN_AGENT;
2070 else if (*state == SRV_STATS_STATE_UP_GOING_DOWN)
2071 *state = SRV_STATS_STATE_DRAIN_GOING_DOWN;
William Lallemand74c24fb2016-11-21 17:18:36 +01002072 else
William Dauchyd3a9a492021-01-25 17:29:03 +01002073 *state = SRV_STATS_STATE_DRAIN;
William Lallemand74c24fb2016-11-21 17:18:36 +01002074 }
2075
William Dauchyd3a9a492021-01-25 17:29:03 +01002076 if (*state == SRV_STATS_STATE_UP && !(ref->check.state & CHK_ST_ENABLED)) {
2077 *state = SRV_STATS_STATE_NO_CHECK;
William Lallemand74c24fb2016-11-21 17:18:36 +01002078 }
2079 }
Emeric Brun52a91d32017-08-31 14:41:55 +02002080 else if (sv->cur_state == SRV_ST_STOPPING) {
William Lallemand74c24fb2016-11-21 17:18:36 +01002081 if ((!(sv->check.state & CHK_ST_ENABLED) && !sv->track) ||
2082 (ref->check.health == ref->check.rise + ref->check.fall - 1)) {
William Dauchyd3a9a492021-01-25 17:29:03 +01002083 *state = SRV_STATS_STATE_NOLB;
William Lallemand74c24fb2016-11-21 17:18:36 +01002084 } else {
William Dauchyd3a9a492021-01-25 17:29:03 +01002085 *state = SRV_STATS_STATE_NOLB_GOING_DOWN;
William Lallemand74c24fb2016-11-21 17:18:36 +01002086 }
2087 }
2088 else { /* stopped */
2089 if ((ref->agent.state & CHK_ST_ENABLED) && !ref->agent.health) {
William Dauchyd3a9a492021-01-25 17:29:03 +01002090 *state = SRV_STATS_STATE_DOWN_AGENT;
William Lallemand74c24fb2016-11-21 17:18:36 +01002091 } else if ((ref->check.state & CHK_ST_ENABLED) && !ref->check.health) {
William Dauchyd3a9a492021-01-25 17:29:03 +01002092 *state = SRV_STATS_STATE_DOWN; /* DOWN */
William Lallemand74c24fb2016-11-21 17:18:36 +01002093 } else if ((ref->agent.state & CHK_ST_ENABLED) || (ref->check.state & CHK_ST_ENABLED)) {
William Dauchyd3a9a492021-01-25 17:29:03 +01002094 *state = SRV_STATS_STATE_GOING_UP;
William Lallemand74c24fb2016-11-21 17:18:36 +01002095 } else {
William Dauchyd3a9a492021-01-25 17:29:03 +01002096 *state = SRV_STATS_STATE_DOWN; /* DOWN, unchecked */
William Lallemand74c24fb2016-11-21 17:18:36 +01002097 }
2098 }
William Dauchyd3a9a492021-01-25 17:29:03 +01002099}
William Lallemand74c24fb2016-11-21 17:18:36 +01002100
William Dauchyd3a9a492021-01-25 17:29:03 +01002101/* Fill <stats> with the backend statistics. <stats> is preallocated array of
2102 * length <len>. If <selected_field> is != NULL, only fill this one. The length
2103 * of the array must be at least ST_F_TOTAL_FIELDS. If this length is less than
2104 * this value, or if the selected field is not implemented for servers, the
2105 * function returns 0, otherwise, it returns 1. <flags> can take the value
2106 * STAT_SHLGNDS.
2107 */
2108int stats_fill_sv_stats(struct proxy *px, struct server *sv, int flags,
2109 struct field *stats, int len,
2110 enum stat_field *selected_field)
2111{
2112 enum stat_field current_field = (selected_field != NULL ? *selected_field : 0);
2113 struct server *via = sv->track ? sv->track : sv;
2114 struct server *ref = via;
2115 enum srv_stats_state state = 0;
2116 char str[INET6_ADDRSTRLEN];
2117 struct buffer *out = get_trash_chunk();
2118 char *fld_status;
2119 long long srv_samples_counter;
2120 unsigned int srv_samples_window = TIME_STATS_SAMPLES;
William Lallemand74c24fb2016-11-21 17:18:36 +01002121
William Dauchyd3a9a492021-01-25 17:29:03 +01002122 if (len < ST_F_TOTAL_FIELDS)
2123 return 0;
William Lallemand74c24fb2016-11-21 17:18:36 +01002124
William Dauchyd3a9a492021-01-25 17:29:03 +01002125 chunk_reset(out);
William Lallemand74c24fb2016-11-21 17:18:36 +01002126
William Dauchyd3a9a492021-01-25 17:29:03 +01002127 /* compute state for later use */
2128 if (selected_field == NULL || *selected_field == ST_F_STATUS ||
2129 *selected_field == ST_F_CHECK_RISE || *selected_field == ST_F_CHECK_FALL ||
2130 *selected_field == ST_F_CHECK_HEALTH || *selected_field == ST_F_HANAFAIL) {
2131 /* we have "via" which is the tracked server as described in the configuration,
2132 * and "ref" which is the checked server and the end of the chain.
2133 */
2134 while (ref->track)
2135 ref = ref->track;
2136 stats_fill_sv_stats_computestate(sv, ref, &state);
William Lallemand74c24fb2016-11-21 17:18:36 +01002137 }
2138
William Dauchyd3a9a492021-01-25 17:29:03 +01002139 /* compue time values for later use */
2140 if (selected_field == NULL || *selected_field == ST_F_QTIME ||
2141 *selected_field == ST_F_CTIME || *selected_field == ST_F_RTIME ||
2142 *selected_field == ST_F_TTIME) {
2143 srv_samples_counter = (px->mode == PR_MODE_HTTP) ? sv->counters.p.http.cum_req : sv->counters.cum_lbconn;
2144 if (srv_samples_counter < TIME_STATS_SAMPLES && srv_samples_counter > 0)
2145 srv_samples_window = srv_samples_counter;
William Lallemand74c24fb2016-11-21 17:18:36 +01002146 }
2147
William Dauchyd3a9a492021-01-25 17:29:03 +01002148 for (; current_field < ST_F_TOTAL_FIELDS; current_field++) {
2149 struct field metric = { 0 };
William Lallemand74c24fb2016-11-21 17:18:36 +01002150
William Dauchyd3a9a492021-01-25 17:29:03 +01002151 switch (current_field) {
2152 case ST_F_PXNAME:
2153 metric = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
2154 break;
2155 case ST_F_SVNAME:
2156 metric = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, sv->id);
2157 break;
2158 case ST_F_MODE:
2159 metric = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
Christopher Faulet3888b8c2021-01-27 13:32:25 +01002160 break;
William Dauchyd3a9a492021-01-25 17:29:03 +01002161 case ST_F_QCUR:
Willy Tarreaua0570452021-06-18 09:30:30 +02002162 metric = mkf_u32(0, sv->queue.length);
William Dauchyd3a9a492021-01-25 17:29:03 +01002163 break;
2164 case ST_F_QMAX:
2165 metric = mkf_u32(FN_MAX, sv->counters.nbpend_max);
2166 break;
2167 case ST_F_SCUR:
2168 metric = mkf_u32(0, sv->cur_sess);
2169 break;
2170 case ST_F_SMAX:
2171 metric = mkf_u32(FN_MAX, sv->counters.cur_sess_max);
2172 break;
2173 case ST_F_SLIM:
2174 if (sv->maxconn)
2175 metric = mkf_u32(FO_CONFIG|FN_LIMIT, sv->maxconn);
2176 break;
2177 case ST_F_SRV_ICUR:
2178 metric = mkf_u32(0, sv->curr_idle_conns);
2179 break;
2180 case ST_F_SRV_ILIM:
2181 if (sv->max_idle_conns != -1)
2182 metric = mkf_u32(FO_CONFIG|FN_LIMIT, sv->max_idle_conns);
2183 break;
2184 case ST_F_STOT:
2185 metric = mkf_u64(FN_COUNTER, sv->counters.cum_sess);
2186 break;
2187 case ST_F_BIN:
2188 metric = mkf_u64(FN_COUNTER, sv->counters.bytes_in);
2189 break;
2190 case ST_F_BOUT:
2191 metric = mkf_u64(FN_COUNTER, sv->counters.bytes_out);
2192 break;
2193 case ST_F_DRESP:
2194 metric = mkf_u64(FN_COUNTER, sv->counters.denied_resp);
2195 break;
2196 case ST_F_ECON:
2197 metric = mkf_u64(FN_COUNTER, sv->counters.failed_conns);
2198 break;
2199 case ST_F_ERESP:
2200 metric = mkf_u64(FN_COUNTER, sv->counters.failed_resp);
2201 break;
2202 case ST_F_WRETR:
2203 metric = mkf_u64(FN_COUNTER, sv->counters.retries);
2204 break;
2205 case ST_F_WREDIS:
2206 metric = mkf_u64(FN_COUNTER, sv->counters.redispatches);
2207 break;
2208 case ST_F_WREW:
2209 metric = mkf_u64(FN_COUNTER, sv->counters.failed_rewrites);
2210 break;
2211 case ST_F_EINT:
2212 metric = mkf_u64(FN_COUNTER, sv->counters.internal_errors);
2213 break;
2214 case ST_F_CONNECT:
2215 metric = mkf_u64(FN_COUNTER, sv->counters.connect);
2216 break;
2217 case ST_F_REUSE:
2218 metric = mkf_u64(FN_COUNTER, sv->counters.reuse);
2219 break;
2220 case ST_F_IDLE_CONN_CUR:
2221 metric = mkf_u32(0, sv->curr_idle_nb);
2222 break;
2223 case ST_F_SAFE_CONN_CUR:
2224 metric = mkf_u32(0, sv->curr_safe_nb);
2225 break;
2226 case ST_F_USED_CONN_CUR:
2227 metric = mkf_u32(0, sv->curr_used_conns);
2228 break;
2229 case ST_F_NEED_CONN_EST:
2230 metric = mkf_u32(0, sv->est_need_conns);
2231 break;
2232 case ST_F_STATUS:
2233 fld_status = chunk_newstr(out);
2234 if (sv->cur_admin & SRV_ADMF_RMAINT)
2235 chunk_appendf(out, "MAINT (resolution)");
2236 else if (sv->cur_admin & SRV_ADMF_IMAINT)
2237 chunk_appendf(out, "MAINT (via %s/%s)", via->proxy->id, via->id);
2238 else if (sv->cur_admin & SRV_ADMF_MAINT)
2239 chunk_appendf(out, "MAINT");
2240 else
2241 chunk_appendf(out,
2242 srv_hlt_st[state],
2243 (ref->cur_state != SRV_ST_STOPPED) ? (ref->check.health - ref->check.rise + 1) : (ref->check.health),
2244 (ref->cur_state != SRV_ST_STOPPED) ? (ref->check.fall) : (ref->check.rise));
William Lallemand74c24fb2016-11-21 17:18:36 +01002245
William Dauchyd3a9a492021-01-25 17:29:03 +01002246 metric = mkf_str(FO_STATUS, fld_status);
2247 break;
2248 case ST_F_LASTCHG:
2249 metric = mkf_u32(FN_AGE, now.tv_sec - sv->last_change);
2250 break;
2251 case ST_F_WEIGHT:
2252 metric = mkf_u32(FN_AVG, (sv->cur_eweight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv);
2253 break;
2254 case ST_F_UWEIGHT:
2255 metric = mkf_u32(FN_AVG, sv->uweight);
2256 break;
2257 case ST_F_ACT:
2258 metric = mkf_u32(FO_STATUS, (sv->flags & SRV_F_BACKUP) ? 0 : 1);
2259 break;
2260 case ST_F_BCK:
2261 metric = mkf_u32(FO_STATUS, (sv->flags & SRV_F_BACKUP) ? 1 : 0);
2262 break;
2263 case ST_F_CHKFAIL:
2264 if (sv->check.state & CHK_ST_ENABLED)
2265 metric = mkf_u64(FN_COUNTER, sv->counters.failed_checks);
2266 break;
2267 case ST_F_CHKDOWN:
2268 if (sv->check.state & CHK_ST_ENABLED)
2269 metric = mkf_u64(FN_COUNTER, sv->counters.down_trans);
2270 break;
2271 case ST_F_DOWNTIME:
2272 if (sv->check.state & CHK_ST_ENABLED)
2273 metric = mkf_u32(FN_COUNTER, srv_downtime(sv));
2274 break;
2275 case ST_F_QLIMIT:
2276 if (sv->maxqueue)
2277 metric = mkf_u32(FO_CONFIG|FS_SERVICE, sv->maxqueue);
2278 break;
2279 case ST_F_PID:
Willy Tarreaue8422bf2021-06-15 09:08:18 +02002280 metric = mkf_u32(FO_KEY, 1);
William Dauchyd3a9a492021-01-25 17:29:03 +01002281 break;
2282 case ST_F_IID:
2283 metric = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
2284 break;
2285 case ST_F_SID:
2286 metric = mkf_u32(FO_KEY|FS_SERVICE, sv->puid);
2287 break;
Aurelien DARRAGON745ce8e2022-11-17 16:34:07 +01002288 case ST_F_SRID:
2289 metric = mkf_u32(FN_COUNTER, sv->rid);
2290 break;
William Dauchyd3a9a492021-01-25 17:29:03 +01002291 case ST_F_THROTTLE:
2292 if (sv->cur_state == SRV_ST_STARTING && !server_is_draining(sv))
2293 metric = mkf_u32(FN_AVG, server_throttle_rate(sv));
2294 break;
2295 case ST_F_LBTOT:
2296 metric = mkf_u64(FN_COUNTER, sv->counters.cum_lbconn);
2297 break;
2298 case ST_F_TRACKED:
2299 if (sv->track) {
2300 char *fld_track = chunk_newstr(out);
2301 chunk_appendf(out, "%s/%s", sv->track->proxy->id, sv->track->id);
2302 metric = mkf_str(FO_CONFIG|FN_NAME|FS_SERVICE, fld_track);
2303 }
2304 break;
2305 case ST_F_TYPE:
2306 metric = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_SV);
2307 break;
2308 case ST_F_RATE:
2309 metric = mkf_u32(FN_RATE, read_freq_ctr(&sv->sess_per_sec));
2310 break;
2311 case ST_F_RATE_MAX:
2312 metric = mkf_u32(FN_MAX, sv->counters.sps_max);
2313 break;
2314 case ST_F_CHECK_STATUS:
2315 if ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED) {
2316 const char *fld_chksts;
Marcin Deraneka8dbdf32020-05-15 20:02:40 +02002317
William Dauchyd3a9a492021-01-25 17:29:03 +01002318 fld_chksts = chunk_newstr(out);
2319 chunk_strcat(out, "* "); // for check in progress
2320 chunk_strcat(out, get_check_status_info(sv->check.status));
2321 if (!(sv->check.state & CHK_ST_INPROGRESS))
2322 fld_chksts += 2; // skip "* "
Willy Tarreau973a9372021-05-12 17:29:14 +02002323 metric = mkf_str(FN_OUTPUT, fld_chksts);
William Dauchyd3a9a492021-01-25 17:29:03 +01002324 }
2325 break;
2326 case ST_F_CHECK_CODE:
2327 if ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED &&
2328 sv->check.status >= HCHK_STATUS_L57DATA)
2329 metric = mkf_u32(FN_OUTPUT, sv->check.code);
2330 break;
2331 case ST_F_CHECK_DURATION:
2332 if ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED &&
2333 sv->check.status >= HCHK_STATUS_CHECKED)
Aurelien DARRAGONb118f2f2022-12-07 14:52:10 +01002334 metric = mkf_u64(FN_DURATION, MAX(sv->check.duration, 0));
William Dauchyd3a9a492021-01-25 17:29:03 +01002335 break;
2336 case ST_F_CHECK_DESC:
2337 if ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED)
2338 metric = mkf_str(FN_OUTPUT, get_check_status_description(sv->check.status));
2339 break;
2340 case ST_F_LAST_CHK:
2341 if ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED)
2342 metric = mkf_str(FN_OUTPUT, sv->check.desc);
2343 break;
2344 case ST_F_CHECK_RISE:
2345 if ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED)
2346 metric = mkf_u32(FO_CONFIG|FS_SERVICE, ref->check.rise);
2347 break;
2348 case ST_F_CHECK_FALL:
2349 if ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED)
2350 metric = mkf_u32(FO_CONFIG|FS_SERVICE, ref->check.fall);
2351 break;
2352 case ST_F_CHECK_HEALTH:
2353 if ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED)
2354 metric = mkf_u32(FO_CONFIG|FS_SERVICE, ref->check.health);
2355 break;
Christopher Faulet59bab612021-07-22 08:04:38 +02002356 case ST_F_AGENT_STATUS:
2357 if ((sv->agent.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED) {
2358 const char *fld_chksts;
2359
2360 fld_chksts = chunk_newstr(out);
2361 chunk_strcat(out, "* "); // for check in progress
2362 chunk_strcat(out, get_check_status_info(sv->agent.status));
2363 if (!(sv->agent.state & CHK_ST_INPROGRESS))
2364 fld_chksts += 2; // skip "* "
2365 metric = mkf_str(FN_OUTPUT, fld_chksts);
2366 }
2367 break;
2368 case ST_F_AGENT_CODE:
2369 if ((sv->agent.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED &&
2370 (sv->agent.status >= HCHK_STATUS_L57DATA))
2371 metric = mkf_u32(FN_OUTPUT, sv->agent.code);
2372 break;
2373 case ST_F_AGENT_DURATION:
2374 if ((sv->agent.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED)
2375 metric = mkf_u64(FN_DURATION, sv->agent.duration);
2376 break;
2377 case ST_F_AGENT_DESC:
2378 if ((sv->agent.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED)
2379 metric = mkf_str(FN_OUTPUT, get_check_status_description(sv->agent.status));
2380 break;
2381 case ST_F_LAST_AGT:
2382 if ((sv->agent.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED)
2383 metric = mkf_str(FN_OUTPUT, sv->agent.desc);
2384 break;
2385 case ST_F_AGENT_RISE:
2386 if ((sv->agent.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED)
2387 metric = mkf_u32(FO_CONFIG|FS_SERVICE, sv->agent.rise);
2388 break;
2389 case ST_F_AGENT_FALL:
2390 if ((sv->agent.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED)
2391 metric = mkf_u32(FO_CONFIG|FS_SERVICE, sv->agent.fall);
2392 break;
2393 case ST_F_AGENT_HEALTH:
2394 if ((sv->agent.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED)
2395 metric = mkf_u32(FO_CONFIG|FS_SERVICE, sv->agent.health);
2396 break;
William Dauchyd3a9a492021-01-25 17:29:03 +01002397 case ST_F_REQ_TOT:
2398 if (px->mode == PR_MODE_HTTP)
2399 metric = mkf_u64(FN_COUNTER, sv->counters.p.http.cum_req);
2400 break;
2401 case ST_F_HRSP_1XX:
2402 if (px->mode == PR_MODE_HTTP)
2403 metric = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[1]);
2404 break;
2405 case ST_F_HRSP_2XX:
2406 if (px->mode == PR_MODE_HTTP)
2407 metric = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[2]);
2408 break;
2409 case ST_F_HRSP_3XX:
2410 if (px->mode == PR_MODE_HTTP)
2411 metric = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[3]);
2412 break;
2413 case ST_F_HRSP_4XX:
2414 if (px->mode == PR_MODE_HTTP)
2415 metric = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[4]);
2416 break;
2417 case ST_F_HRSP_5XX:
2418 if (px->mode == PR_MODE_HTTP)
2419 metric = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[5]);
2420 break;
2421 case ST_F_HRSP_OTHER:
2422 if (px->mode == PR_MODE_HTTP)
2423 metric = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[0]);
2424 break;
2425 case ST_F_HANAFAIL:
2426 if (ref->observe)
2427 metric = mkf_u64(FN_COUNTER, sv->counters.failed_hana);
2428 break;
2429 case ST_F_CLI_ABRT:
2430 metric = mkf_u64(FN_COUNTER, sv->counters.cli_aborts);
2431 break;
2432 case ST_F_SRV_ABRT:
2433 metric = mkf_u64(FN_COUNTER, sv->counters.srv_aborts);
2434 break;
2435 case ST_F_LASTSESS:
2436 metric = mkf_s32(FN_AGE, srv_lastsession(sv));
2437 break;
2438 case ST_F_QTIME:
2439 metric = mkf_u32(FN_AVG, swrate_avg(sv->counters.q_time, srv_samples_window));
2440 break;
2441 case ST_F_CTIME:
2442 metric = mkf_u32(FN_AVG, swrate_avg(sv->counters.c_time, srv_samples_window));
2443 break;
2444 case ST_F_RTIME:
2445 metric = mkf_u32(FN_AVG, swrate_avg(sv->counters.d_time, srv_samples_window));
2446 break;
2447 case ST_F_TTIME:
2448 metric = mkf_u32(FN_AVG, swrate_avg(sv->counters.t_time, srv_samples_window));
2449 break;
2450 case ST_F_QT_MAX:
2451 metric = mkf_u32(FN_MAX, sv->counters.qtime_max);
2452 break;
2453 case ST_F_CT_MAX:
2454 metric = mkf_u32(FN_MAX, sv->counters.ctime_max);
2455 break;
2456 case ST_F_RT_MAX:
2457 metric = mkf_u32(FN_MAX, sv->counters.dtime_max);
2458 break;
2459 case ST_F_TT_MAX:
2460 metric = mkf_u32(FN_MAX, sv->counters.ttime_max);
2461 break;
2462 case ST_F_ADDR:
2463 if (flags & STAT_SHLGNDS) {
2464 switch (addr_to_str(&sv->addr, str, sizeof(str))) {
2465 case AF_INET:
2466 metric = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
2467 chunk_appendf(out, "%s:%d", str, sv->svc_port);
2468 break;
2469 case AF_INET6:
2470 metric = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
2471 chunk_appendf(out, "[%s]:%d", str, sv->svc_port);
2472 break;
2473 case AF_UNIX:
2474 metric = mkf_str(FO_CONFIG|FS_SERVICE, "unix");
2475 break;
2476 case -1:
2477 metric = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
2478 chunk_strcat(out, strerror(errno));
2479 break;
2480 default: /* address family not supported */
2481 break;
2482 }
2483 }
2484 break;
2485 case ST_F_COOKIE:
2486 if (flags & STAT_SHLGNDS && sv->cookie)
2487 metric = mkf_str(FO_CONFIG|FN_NAME|FS_SERVICE, sv->cookie);
2488 break;
2489 default:
2490 /* not used for servers. If a specific metric
2491 * is requested, return an error. Otherwise continue.
2492 */
2493 if (selected_field != NULL)
2494 return 0;
2495 continue;
William Lallemand74c24fb2016-11-21 17:18:36 +01002496 }
William Dauchyd3a9a492021-01-25 17:29:03 +01002497 stats[current_field] = metric;
2498 if (selected_field != NULL)
2499 break;
William Lallemand74c24fb2016-11-21 17:18:36 +01002500 }
William Lallemand74c24fb2016-11-21 17:18:36 +01002501 return 1;
2502}
2503
2504/* Dumps a line for server <sv> and proxy <px> to the trash and uses the state
Willy Tarreaucaff6312022-05-27 10:17:46 +02002505 * from stream connector <sc>, and server state <state>. The caller is
Willy Tarreau43241ff2019-10-09 11:27:51 +02002506 * responsible for clearing the trash if needed. Returns non-zero if it emits
2507 * anything, zero otherwise.
William Lallemand74c24fb2016-11-21 17:18:36 +01002508 */
Willy Tarreaucaff6312022-05-27 10:17:46 +02002509static int stats_dump_sv_stats(struct stconn *sc, struct proxy *px, struct server *sv)
William Lallemand74c24fb2016-11-21 17:18:36 +01002510{
Willy Tarreaucaff6312022-05-27 10:17:46 +02002511 struct appctx *appctx = __sc_appctx(sc);
Willy Tarreau91cefca2022-05-03 17:08:29 +02002512 struct show_stat_ctx *ctx = appctx->svcctx;
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02002513 struct stats_module *mod;
2514 struct field *stats = stat_l[STATS_DOMAIN_PROXY];
2515 size_t stats_count = ST_F_TOTAL_FIELDS;
2516
2517 memset(stats, 0, sizeof(struct field) * stat_count[STATS_DOMAIN_PROXY]);
William Lallemand74c24fb2016-11-21 17:18:36 +01002518
Willy Tarreau91cefca2022-05-03 17:08:29 +02002519 if (!stats_fill_sv_stats(px, sv, ctx->flags, stats,
William Dauchyd3a9a492021-01-25 17:29:03 +01002520 ST_F_TOTAL_FIELDS, NULL))
William Lallemand74c24fb2016-11-21 17:18:36 +01002521 return 0;
2522
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02002523 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
2524 void *counters;
2525
2526 if (stats_get_domain(mod->domain_flags) != STATS_DOMAIN_PROXY)
2527 continue;
2528
2529 if (!(stats_px_get_cap(mod->domain_flags) & STATS_PX_CAP_SRV)) {
2530 stats_count += mod->stats_count;
2531 continue;
2532 }
2533
2534 counters = EXTRA_COUNTERS_GET(sv->extra_counters, mod);
2535 mod->fill_stats(counters, stats + stats_count);
2536 stats_count += mod->stats_count;
2537 }
2538
2539 return stats_dump_one_line(stats, stats_count, appctx);
William Lallemand74c24fb2016-11-21 17:18:36 +01002540}
2541
William Dauchyda3b4662021-01-25 17:29:01 +01002542/* Helper to compute srv values for a given backend
William Lallemand74c24fb2016-11-21 17:18:36 +01002543 */
William Dauchyda3b4662021-01-25 17:29:01 +01002544static void stats_fill_be_stats_computesrv(struct proxy *px, int *nbup, int *nbsrv, int *totuw)
William Lallemand74c24fb2016-11-21 17:18:36 +01002545{
William Dauchyda3b4662021-01-25 17:29:01 +01002546 int nbup_tmp, nbsrv_tmp, totuw_tmp;
Willy Tarreau2fbe6942020-10-23 18:02:54 +02002547 const struct server *srv;
William Lallemand74c24fb2016-11-21 17:18:36 +01002548
William Dauchyda3b4662021-01-25 17:29:01 +01002549 nbup_tmp = nbsrv_tmp = totuw_tmp = 0;
Willy Tarreaubd715102020-10-23 22:44:30 +02002550 for (srv = px->srv; srv; srv = srv->next) {
2551 if (srv->cur_state != SRV_ST_STOPPED) {
William Dauchyda3b4662021-01-25 17:29:01 +01002552 nbup_tmp++;
Willy Tarreaubd715102020-10-23 22:44:30 +02002553 if (srv_currently_usable(srv) &&
2554 (!px->srv_act ^ !(srv->flags & SRV_F_BACKUP)))
William Dauchyda3b4662021-01-25 17:29:01 +01002555 totuw_tmp += srv->uweight;
Willy Tarreau2fbe6942020-10-23 18:02:54 +02002556 }
William Dauchyda3b4662021-01-25 17:29:01 +01002557 nbsrv_tmp++;
Willy Tarreau2fbe6942020-10-23 18:02:54 +02002558 }
2559
Willy Tarreaubd715102020-10-23 22:44:30 +02002560 HA_RWLOCK_RDLOCK(LBPRM_LOCK, &px->lbprm.lock);
2561 if (!px->srv_act && px->lbprm.fbck)
William Dauchyda3b4662021-01-25 17:29:01 +01002562 totuw_tmp = px->lbprm.fbck->uweight;
Willy Tarreaubd715102020-10-23 22:44:30 +02002563 HA_RWLOCK_RDUNLOCK(LBPRM_LOCK, &px->lbprm.lock);
2564
William Dauchyda3b4662021-01-25 17:29:01 +01002565 /* use tmp variable then assign result to make gcc happy */
2566 *nbup = nbup_tmp;
2567 *nbsrv = nbsrv_tmp;
2568 *totuw = totuw_tmp;
2569}
Willy Tarreau2fbe6942020-10-23 18:02:54 +02002570
William Dauchyda3b4662021-01-25 17:29:01 +01002571/* Fill <stats> with the backend statistics. <stats> is preallocated array of
2572 * length <len>. If <selected_field> is != NULL, only fill this one. The length
2573 * of the array must be at least ST_F_TOTAL_FIELDS. If this length is less than
2574 * this value, or if the selected field is not implemented for backends, the
2575 * function returns 0, otherwise, it returns 1. <flags> can take the value
2576 * STAT_SHLGNDS.
2577 */
2578int stats_fill_be_stats(struct proxy *px, int flags, struct field *stats, int len,
2579 enum stat_field *selected_field)
2580{
2581 enum stat_field current_field = (selected_field != NULL ? *selected_field : 0);
2582 long long be_samples_counter;
2583 unsigned int be_samples_window = TIME_STATS_SAMPLES;
2584 struct buffer *out = get_trash_chunk();
2585 int nbup, nbsrv, totuw;
2586 char *fld;
William Lallemand74c24fb2016-11-21 17:18:36 +01002587
William Dauchyda3b4662021-01-25 17:29:01 +01002588 if (len < ST_F_TOTAL_FIELDS)
2589 return 0;
William Lallemand74c24fb2016-11-21 17:18:36 +01002590
William Dauchyda3b4662021-01-25 17:29:01 +01002591 nbup = nbsrv = totuw = 0;
2592 /* some srv values compute for later if we either select all fields or
2593 * need them for one of the mentioned ones */
2594 if (selected_field == NULL || *selected_field == ST_F_STATUS ||
2595 *selected_field == ST_F_UWEIGHT)
2596 stats_fill_be_stats_computesrv(px, &nbup, &nbsrv, &totuw);
William Lallemand74c24fb2016-11-21 17:18:36 +01002597
William Dauchyda3b4662021-01-25 17:29:01 +01002598 /* same here but specific to time fields */
2599 if (selected_field == NULL || *selected_field == ST_F_QTIME ||
2600 *selected_field == ST_F_CTIME || *selected_field == ST_F_RTIME ||
2601 *selected_field == ST_F_TTIME) {
2602 be_samples_counter = (px->mode == PR_MODE_HTTP) ? px->be_counters.p.http.cum_req : px->be_counters.cum_lbconn;
2603 if (be_samples_counter < TIME_STATS_SAMPLES && be_samples_counter > 0)
2604 be_samples_window = be_samples_counter;
William Lallemand74c24fb2016-11-21 17:18:36 +01002605 }
2606
William Dauchyda3b4662021-01-25 17:29:01 +01002607 for (; current_field < ST_F_TOTAL_FIELDS; current_field++) {
2608 struct field metric = { 0 };
William Lallemand74c24fb2016-11-21 17:18:36 +01002609
William Dauchyda3b4662021-01-25 17:29:01 +01002610 switch (current_field) {
2611 case ST_F_PXNAME:
2612 metric = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
2613 break;
2614 case ST_F_SVNAME:
2615 metric = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, "BACKEND");
2616 break;
2617 case ST_F_MODE:
2618 metric = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
2619 break;
2620 case ST_F_QCUR:
Willy Tarreau7f3c1df2021-06-18 09:22:21 +02002621 metric = mkf_u32(0, px->queue.length);
William Dauchyda3b4662021-01-25 17:29:01 +01002622 break;
2623 case ST_F_QMAX:
2624 metric = mkf_u32(FN_MAX, px->be_counters.nbpend_max);
2625 break;
2626 case ST_F_SCUR:
2627 metric = mkf_u32(0, px->beconn);
2628 break;
2629 case ST_F_SMAX:
2630 metric = mkf_u32(FN_MAX, px->be_counters.conn_max);
2631 break;
2632 case ST_F_SLIM:
2633 metric = mkf_u32(FO_CONFIG|FN_LIMIT, px->fullconn);
2634 break;
2635 case ST_F_STOT:
2636 metric = mkf_u64(FN_COUNTER, px->be_counters.cum_conn);
2637 break;
2638 case ST_F_BIN:
2639 metric = mkf_u64(FN_COUNTER, px->be_counters.bytes_in);
2640 break;
2641 case ST_F_BOUT:
2642 metric = mkf_u64(FN_COUNTER, px->be_counters.bytes_out);
2643 break;
2644 case ST_F_DREQ:
2645 metric = mkf_u64(FN_COUNTER, px->be_counters.denied_req);
2646 break;
2647 case ST_F_DRESP:
2648 metric = mkf_u64(FN_COUNTER, px->be_counters.denied_resp);
2649 break;
2650 case ST_F_ECON:
2651 metric = mkf_u64(FN_COUNTER, px->be_counters.failed_conns);
2652 break;
2653 case ST_F_ERESP:
2654 metric = mkf_u64(FN_COUNTER, px->be_counters.failed_resp);
2655 break;
2656 case ST_F_WRETR:
2657 metric = mkf_u64(FN_COUNTER, px->be_counters.retries);
2658 break;
2659 case ST_F_WREDIS:
2660 metric = mkf_u64(FN_COUNTER, px->be_counters.redispatches);
2661 break;
2662 case ST_F_WREW:
2663 metric = mkf_u64(FN_COUNTER, px->be_counters.failed_rewrites);
2664 break;
2665 case ST_F_EINT:
2666 metric = mkf_u64(FN_COUNTER, px->be_counters.internal_errors);
2667 break;
2668 case ST_F_CONNECT:
2669 metric = mkf_u64(FN_COUNTER, px->be_counters.connect);
2670 break;
2671 case ST_F_REUSE:
2672 metric = mkf_u64(FN_COUNTER, px->be_counters.reuse);
2673 break;
2674 case ST_F_STATUS:
2675 fld = chunk_newstr(out);
2676 chunk_appendf(out, "%s", (px->lbprm.tot_weight > 0 || !px->srv) ? "UP" : "DOWN");
2677 if (flags & (STAT_HIDE_MAINT|STAT_HIDE_DOWN))
2678 chunk_appendf(out, " (%d/%d)", nbup, nbsrv);
2679 metric = mkf_str(FO_STATUS, fld);
2680 break;
Cedric Paillet7d6644e2022-12-08 09:17:00 +00002681 case ST_F_AGG_SRV_CHECK_STATUS: // DEPRECATED
2682 case ST_F_AGG_SRV_STATUS:
William Dauchy42d7c402021-11-07 10:18:47 +01002683 metric = mkf_u32(FN_GAUGE, 0);
2684 break;
William Dauchyda3b4662021-01-25 17:29:01 +01002685 case ST_F_WEIGHT:
2686 metric = mkf_u32(FN_AVG, (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv);
2687 break;
2688 case ST_F_UWEIGHT:
2689 metric = mkf_u32(FN_AVG, totuw);
2690 break;
2691 case ST_F_ACT:
2692 metric = mkf_u32(0, px->srv_act);
2693 break;
2694 case ST_F_BCK:
2695 metric = mkf_u32(0, px->srv_bck);
2696 break;
2697 case ST_F_CHKDOWN:
2698 metric = mkf_u64(FN_COUNTER, px->down_trans);
2699 break;
2700 case ST_F_LASTCHG:
2701 metric = mkf_u32(FN_AGE, now.tv_sec - px->last_change);
2702 break;
2703 case ST_F_DOWNTIME:
2704 if (px->srv)
2705 metric = mkf_u32(FN_COUNTER, be_downtime(px));
2706 break;
2707 case ST_F_PID:
Willy Tarreaue8422bf2021-06-15 09:08:18 +02002708 metric = mkf_u32(FO_KEY, 1);
William Dauchyda3b4662021-01-25 17:29:01 +01002709 break;
2710 case ST_F_IID:
2711 metric = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
2712 break;
2713 case ST_F_SID:
2714 metric = mkf_u32(FO_KEY|FS_SERVICE, 0);
2715 break;
2716 case ST_F_LBTOT:
2717 metric = mkf_u64(FN_COUNTER, px->be_counters.cum_lbconn);
2718 break;
2719 case ST_F_TYPE:
2720 metric = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_BE);
2721 break;
2722 case ST_F_RATE:
2723 metric = mkf_u32(0, read_freq_ctr(&px->be_sess_per_sec));
2724 break;
2725 case ST_F_RATE_MAX:
2726 metric = mkf_u32(0, px->be_counters.sps_max);
2727 break;
2728 case ST_F_COOKIE:
2729 if (flags & STAT_SHLGNDS && px->cookie_name)
2730 metric = mkf_str(FO_CONFIG|FN_NAME|FS_SERVICE, px->cookie_name);
2731 break;
2732 case ST_F_ALGO:
2733 if (flags & STAT_SHLGNDS)
2734 metric = mkf_str(FO_CONFIG|FS_SERVICE, backend_lb_algo_str(px->lbprm.algo & BE_LB_ALGO));
2735 break;
2736 case ST_F_REQ_TOT:
2737 if (px->mode == PR_MODE_HTTP)
2738 metric = mkf_u64(FN_COUNTER, px->be_counters.p.http.cum_req);
2739 break;
2740 case ST_F_HRSP_1XX:
2741 if (px->mode == PR_MODE_HTTP)
2742 metric = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[1]);
2743 break;
2744 case ST_F_HRSP_2XX:
2745 if (px->mode == PR_MODE_HTTP)
2746 metric = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[2]);
2747 break;
2748 case ST_F_HRSP_3XX:
2749 if (px->mode == PR_MODE_HTTP)
2750 metric = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[3]);
2751 break;
2752 case ST_F_HRSP_4XX:
2753 if (px->mode == PR_MODE_HTTP)
2754 metric = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[4]);
2755 break;
2756 case ST_F_HRSP_5XX:
2757 if (px->mode == PR_MODE_HTTP)
2758 metric = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[5]);
2759 break;
2760 case ST_F_HRSP_OTHER:
2761 if (px->mode == PR_MODE_HTTP)
2762 metric = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[0]);
2763 break;
2764 case ST_F_CACHE_LOOKUPS:
2765 if (px->mode == PR_MODE_HTTP)
2766 metric = mkf_u64(FN_COUNTER, px->be_counters.p.http.cache_lookups);
2767 break;
2768 case ST_F_CACHE_HITS:
2769 if (px->mode == PR_MODE_HTTP)
2770 metric = mkf_u64(FN_COUNTER, px->be_counters.p.http.cache_hits);
2771 break;
2772 case ST_F_CLI_ABRT:
2773 metric = mkf_u64(FN_COUNTER, px->be_counters.cli_aborts);
2774 break;
2775 case ST_F_SRV_ABRT:
2776 metric = mkf_u64(FN_COUNTER, px->be_counters.srv_aborts);
2777 break;
2778 case ST_F_COMP_IN:
2779 metric = mkf_u64(FN_COUNTER, px->be_counters.comp_in);
2780 break;
2781 case ST_F_COMP_OUT:
2782 metric = mkf_u64(FN_COUNTER, px->be_counters.comp_out);
2783 break;
2784 case ST_F_COMP_BYP:
2785 metric = mkf_u64(FN_COUNTER, px->be_counters.comp_byp);
2786 break;
2787 case ST_F_COMP_RSP:
2788 metric = mkf_u64(FN_COUNTER, px->be_counters.p.http.comp_rsp);
2789 break;
2790 case ST_F_LASTSESS:
2791 metric = mkf_s32(FN_AGE, be_lastsession(px));
2792 break;
2793 case ST_F_QTIME:
2794 metric = mkf_u32(FN_AVG, swrate_avg(px->be_counters.q_time, be_samples_window));
2795 break;
2796 case ST_F_CTIME:
2797 metric = mkf_u32(FN_AVG, swrate_avg(px->be_counters.c_time, be_samples_window));
2798 break;
2799 case ST_F_RTIME:
2800 metric = mkf_u32(FN_AVG, swrate_avg(px->be_counters.d_time, be_samples_window));
2801 break;
2802 case ST_F_TTIME:
2803 metric = mkf_u32(FN_AVG, swrate_avg(px->be_counters.t_time, be_samples_window));
2804 break;
2805 case ST_F_QT_MAX:
2806 metric = mkf_u32(FN_MAX, px->be_counters.qtime_max);
2807 break;
2808 case ST_F_CT_MAX:
2809 metric = mkf_u32(FN_MAX, px->be_counters.ctime_max);
2810 break;
2811 case ST_F_RT_MAX:
2812 metric = mkf_u32(FN_MAX, px->be_counters.dtime_max);
2813 break;
2814 case ST_F_TT_MAX:
2815 metric = mkf_u32(FN_MAX, px->be_counters.ttime_max);
2816 break;
2817 default:
2818 /* not used for backends. If a specific metric
2819 * is requested, return an error. Otherwise continue.
2820 */
2821 if (selected_field != NULL)
2822 return 0;
2823 continue;
2824 }
2825 stats[current_field] = metric;
2826 if (selected_field != NULL)
2827 break;
2828 }
William Lallemand74c24fb2016-11-21 17:18:36 +01002829 return 1;
2830}
2831
2832/* Dumps a line for backend <px> to the trash for and uses the state from stream
Willy Tarreau43241ff2019-10-09 11:27:51 +02002833 * interface <si>. The caller is responsible for clearing the trash if needed.
2834 * Returns non-zero if it emits anything, zero otherwise.
William Lallemand74c24fb2016-11-21 17:18:36 +01002835 */
Willy Tarreaucaff6312022-05-27 10:17:46 +02002836static int stats_dump_be_stats(struct stconn *sc, struct proxy *px)
William Lallemand74c24fb2016-11-21 17:18:36 +01002837{
Willy Tarreaucaff6312022-05-27 10:17:46 +02002838 struct appctx *appctx = __sc_appctx(sc);
Willy Tarreau91cefca2022-05-03 17:08:29 +02002839 struct show_stat_ctx *ctx = appctx->svcctx;
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02002840 struct field *stats = stat_l[STATS_DOMAIN_PROXY];
2841 struct stats_module *mod;
2842 size_t stats_count = ST_F_TOTAL_FIELDS;
William Lallemand74c24fb2016-11-21 17:18:36 +01002843
2844 if (!(px->cap & PR_CAP_BE))
2845 return 0;
2846
Willy Tarreau91cefca2022-05-03 17:08:29 +02002847 if ((ctx->flags & STAT_BOUND) && !(ctx->type & (1 << STATS_TYPE_BE)))
William Lallemand74c24fb2016-11-21 17:18:36 +01002848 return 0;
2849
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02002850 memset(stats, 0, sizeof(struct field) * stat_count[STATS_DOMAIN_PROXY]);
2851
Willy Tarreau91cefca2022-05-03 17:08:29 +02002852 if (!stats_fill_be_stats(px, ctx->flags, stats, ST_F_TOTAL_FIELDS, NULL))
William Lallemand74c24fb2016-11-21 17:18:36 +01002853 return 0;
2854
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02002855 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
2856 struct extra_counters *counters;
2857
2858 if (stats_get_domain(mod->domain_flags) != STATS_DOMAIN_PROXY)
2859 continue;
2860
2861 if (!(stats_px_get_cap(mod->domain_flags) & STATS_PX_CAP_BE)) {
2862 stats_count += mod->stats_count;
2863 continue;
2864 }
2865
2866 counters = EXTRA_COUNTERS_GET(px->extra_counters_be, mod);
2867 mod->fill_stats(counters, stats + stats_count);
2868 stats_count += mod->stats_count;
2869 }
2870
2871 return stats_dump_one_line(stats, stats_count, appctx);
William Lallemand74c24fb2016-11-21 17:18:36 +01002872}
2873
2874/* Dumps the HTML table header for proxy <px> to the trash for and uses the state from
Willy Tarreaucaff6312022-05-27 10:17:46 +02002875 * stream connector <sc> and per-uri parameters <uri>. The caller is responsible
William Lallemand74c24fb2016-11-21 17:18:36 +01002876 * for clearing the trash if needed.
2877 */
Willy Tarreaucaff6312022-05-27 10:17:46 +02002878static void stats_dump_html_px_hdr(struct stconn *sc, struct proxy *px)
William Lallemand74c24fb2016-11-21 17:18:36 +01002879{
Willy Tarreaucaff6312022-05-27 10:17:46 +02002880 struct appctx *appctx = __sc_appctx(sc);
Willy Tarreau91cefca2022-05-03 17:08:29 +02002881 struct show_stat_ctx *ctx = appctx->svcctx;
William Lallemand74c24fb2016-11-21 17:18:36 +01002882 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02002883 struct stats_module *mod;
2884 int stats_module_len = 0;
William Lallemand74c24fb2016-11-21 17:18:36 +01002885
Willy Tarreau91cefca2022-05-03 17:08:29 +02002886 if (px->cap & PR_CAP_BE && px->srv && (ctx->flags & STAT_ADMIN)) {
William Lallemand74c24fb2016-11-21 17:18:36 +01002887 /* A form to enable/disable this proxy servers */
2888
Willy Tarreau91cefca2022-05-03 17:08:29 +02002889 /* scope_txt = search pattern + search query, ctx->scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
William Lallemand74c24fb2016-11-21 17:18:36 +01002890 scope_txt[0] = 0;
Willy Tarreau91cefca2022-05-03 17:08:29 +02002891 if (ctx->scope_len) {
Willy Tarreaucaff6312022-05-27 10:17:46 +02002892 const char *scope_ptr = stats_scope_ptr(appctx, sc);
Christopher Fauleted7a0662019-01-14 11:07:34 +01002893
William Lallemand74c24fb2016-11-21 17:18:36 +01002894 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Willy Tarreau91cefca2022-05-03 17:08:29 +02002895 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), scope_ptr, ctx->scope_len);
2896 scope_txt[strlen(STAT_SCOPE_PATTERN) + ctx->scope_len] = 0;
William Lallemand74c24fb2016-11-21 17:18:36 +01002897 }
2898
2899 chunk_appendf(&trash,
2900 "<form method=\"post\">");
2901 }
2902
2903 /* print a new table */
2904 chunk_appendf(&trash,
2905 "<table class=\"tbl\" width=\"100%%\">\n"
2906 "<tr class=\"titre\">"
2907 "<th class=\"pxname\" width=\"10%%\">");
2908
2909 chunk_appendf(&trash,
2910 "<a name=\"%s\"></a>%s"
2911 "<a class=px href=\"#%s\">%s</a>",
2912 px->id,
Willy Tarreau91cefca2022-05-03 17:08:29 +02002913 (ctx->flags & STAT_SHLGNDS) ? "<u>":"",
William Lallemand74c24fb2016-11-21 17:18:36 +01002914 px->id, px->id);
2915
Willy Tarreau91cefca2022-05-03 17:08:29 +02002916 if (ctx->flags & STAT_SHLGNDS) {
William Lallemand74c24fb2016-11-21 17:18:36 +01002917 /* cap, mode, id */
2918 chunk_appendf(&trash, "<div class=tips>cap: %s, mode: %s, id: %d",
2919 proxy_cap_str(px->cap), proxy_mode_str(px->mode),
2920 px->uuid);
2921 chunk_appendf(&trash, "</div>");
2922 }
2923
2924 chunk_appendf(&trash,
2925 "%s</th>"
2926 "<th class=\"%s\" width=\"90%%\">%s</th>"
2927 "</tr>\n"
2928 "</table>\n"
2929 "<table class=\"tbl\" width=\"100%%\">\n"
2930 "<tr class=\"titre\">",
Willy Tarreau91cefca2022-05-03 17:08:29 +02002931 (ctx->flags & STAT_SHLGNDS) ? "</u>":"",
William Lallemand74c24fb2016-11-21 17:18:36 +01002932 px->desc ? "desc" : "empty", px->desc ? px->desc : "");
2933
Willy Tarreau91cefca2022-05-03 17:08:29 +02002934 if (ctx->flags & STAT_ADMIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +01002935 /* Column heading for Enable or Disable server */
Christopher Fauletbc271ec2019-12-02 11:29:04 +01002936 if ((px->cap & PR_CAP_BE) && px->srv)
2937 chunk_appendf(&trash,
2938 "<th rowspan=2 width=1><input type=\"checkbox\" "
2939 "onclick=\"for(c in document.getElementsByClassName('%s-checkbox')) "
2940 "document.getElementsByClassName('%s-checkbox').item(c).checked = this.checked\"></th>",
2941 px->id,
2942 px->id);
2943 else
2944 chunk_appendf(&trash, "<th rowspan=2></th>");
William Lallemand74c24fb2016-11-21 17:18:36 +01002945 }
2946
2947 chunk_appendf(&trash,
2948 "<th rowspan=2></th>"
2949 "<th colspan=3>Queue</th>"
2950 "<th colspan=3>Session rate</th><th colspan=6>Sessions</th>"
2951 "<th colspan=2>Bytes</th><th colspan=2>Denied</th>"
2952 "<th colspan=3>Errors</th><th colspan=2>Warnings</th>"
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02002953 "<th colspan=9>Server</th>");
2954
Willy Tarreau91cefca2022-05-03 17:08:29 +02002955 if (ctx->flags & STAT_SHMODULES) {
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02002956 // calculate the count of module for colspan attribute
2957 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
2958 ++stats_module_len;
2959 }
2960 chunk_appendf(&trash, "<th colspan=%d>Extra modules</th>",
2961 stats_module_len);
2962 }
2963
2964 chunk_appendf(&trash,
William Lallemand74c24fb2016-11-21 17:18:36 +01002965 "</tr>\n"
2966 "<tr class=\"titre\">"
2967 "<th>Cur</th><th>Max</th><th>Limit</th>"
2968 "<th>Cur</th><th>Max</th><th>Limit</th><th>Cur</th><th>Max</th>"
2969 "<th>Limit</th><th>Total</th><th>LbTot</th><th>Last</th><th>In</th><th>Out</th>"
2970 "<th>Req</th><th>Resp</th><th>Req</th><th>Conn</th>"
2971 "<th>Resp</th><th>Retr</th><th>Redis</th>"
2972 "<th>Status</th><th>LastChk</th><th>Wght</th><th>Act</th>"
2973 "<th>Bck</th><th>Chk</th><th>Dwn</th><th>Dwntme</th>"
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02002974 "<th>Thrtle</th>\n");
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02002975
Willy Tarreau91cefca2022-05-03 17:08:29 +02002976 if (ctx->flags & STAT_SHMODULES) {
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02002977 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
2978 chunk_appendf(&trash, "<th>%s</th>", mod->name);
2979 }
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02002980 }
2981
2982 chunk_appendf(&trash, "</tr>");
William Lallemand74c24fb2016-11-21 17:18:36 +01002983}
2984
2985/* Dumps the HTML table trailer for proxy <px> to the trash for and uses the state from
Willy Tarreaucaff6312022-05-27 10:17:46 +02002986 * stream connector <sc>. The caller is responsible for clearing the trash if needed.
William Lallemand74c24fb2016-11-21 17:18:36 +01002987 */
Willy Tarreaucaff6312022-05-27 10:17:46 +02002988static void stats_dump_html_px_end(struct stconn *sc, struct proxy *px)
William Lallemand74c24fb2016-11-21 17:18:36 +01002989{
Willy Tarreaucaff6312022-05-27 10:17:46 +02002990 struct appctx *appctx = __sc_appctx(sc);
Willy Tarreau91cefca2022-05-03 17:08:29 +02002991 struct show_stat_ctx *ctx = appctx->svcctx;
Christopher Faulet693b23b2022-02-28 09:09:05 +01002992
William Lallemand74c24fb2016-11-21 17:18:36 +01002993 chunk_appendf(&trash, "</table>");
2994
Willy Tarreau91cefca2022-05-03 17:08:29 +02002995 if ((px->cap & PR_CAP_BE) && px->srv && (ctx->flags & STAT_ADMIN)) {
William Lallemand74c24fb2016-11-21 17:18:36 +01002996 /* close the form used to enable/disable this proxy servers */
2997 chunk_appendf(&trash,
2998 "Choose the action to perform on the checked servers : "
2999 "<select name=action>"
3000 "<option value=\"\"></option>"
3001 "<option value=\"ready\">Set state to READY</option>"
3002 "<option value=\"drain\">Set state to DRAIN</option>"
3003 "<option value=\"maint\">Set state to MAINT</option>"
3004 "<option value=\"dhlth\">Health: disable checks</option>"
3005 "<option value=\"ehlth\">Health: enable checks</option>"
3006 "<option value=\"hrunn\">Health: force UP</option>"
3007 "<option value=\"hnolb\">Health: force NOLB</option>"
3008 "<option value=\"hdown\">Health: force DOWN</option>"
3009 "<option value=\"dagent\">Agent: disable checks</option>"
3010 "<option value=\"eagent\">Agent: enable checks</option>"
3011 "<option value=\"arunn\">Agent: force UP</option>"
3012 "<option value=\"adown\">Agent: force DOWN</option>"
3013 "<option value=\"shutdown\">Kill Sessions</option>"
3014 "</select>"
3015 "<input type=\"hidden\" name=\"b\" value=\"#%d\">"
3016 "&nbsp;<input type=\"submit\" value=\"Apply\">"
3017 "</form>",
3018 px->uuid);
3019 }
3020
3021 chunk_appendf(&trash, "<p>\n");
3022}
3023
3024/*
Willy Tarreau4596fe22022-05-17 19:07:51 +02003025 * Dumps statistics for a proxy. The output is sent to the stream connector's
William Lallemand74c24fb2016-11-21 17:18:36 +01003026 * input buffer. Returns 0 if it had to stop dumping data because of lack of
3027 * buffer space, or non-zero if everything completed. This function is used
3028 * both by the CLI and the HTTP entry points, and is able to dump the output
3029 * in HTML or CSV formats. If the later, <uri> must be NULL.
3030 */
Willy Tarreaucaff6312022-05-27 10:17:46 +02003031int stats_dump_proxy_to_buffer(struct stconn *sc, struct htx *htx,
Christopher Fauletef779222018-10-31 08:47:01 +01003032 struct proxy *px, struct uri_auth *uri)
William Lallemand74c24fb2016-11-21 17:18:36 +01003033{
Willy Tarreaucaff6312022-05-27 10:17:46 +02003034 struct appctx *appctx = __sc_appctx(sc);
Willy Tarreau91cefca2022-05-03 17:08:29 +02003035 struct show_stat_ctx *ctx = appctx->svcctx;
Willy Tarreaucaff6312022-05-27 10:17:46 +02003036 struct stream *s = __sc_strm(sc);
3037 struct channel *rep = sc_ic(sc);
William Lallemand74c24fb2016-11-21 17:18:36 +01003038 struct server *sv, *svs; /* server and server-state, server-state=server or server->track */
3039 struct listener *l;
William Lallemand74c24fb2016-11-21 17:18:36 +01003040
3041 chunk_reset(&trash);
3042
Willy Tarreau91cefca2022-05-03 17:08:29 +02003043 switch (ctx->px_st) {
William Lallemand74c24fb2016-11-21 17:18:36 +01003044 case STAT_PX_ST_INIT:
3045 /* we are on a new proxy */
3046 if (uri && uri->scope) {
3047 /* we have a limited scope, we have to check the proxy name */
3048 struct stat_scope *scope;
3049 int len;
3050
3051 len = strlen(px->id);
3052 scope = uri->scope;
3053
3054 while (scope) {
3055 /* match exact proxy name */
3056 if (scope->px_len == len && !memcmp(px->id, scope->px_id, len))
3057 break;
3058
3059 /* match '.' which means 'self' proxy */
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003060 if (strcmp(scope->px_id, ".") == 0 && px == s->be)
William Lallemand74c24fb2016-11-21 17:18:36 +01003061 break;
3062 scope = scope->next;
3063 }
3064
3065 /* proxy name not found : don't dump anything */
3066 if (scope == NULL)
3067 return 1;
3068 }
3069
3070 /* if the user has requested a limited output and the proxy
3071 * name does not match, skip it.
3072 */
Willy Tarreau91cefca2022-05-03 17:08:29 +02003073 if (ctx->scope_len) {
Willy Tarreaucaff6312022-05-27 10:17:46 +02003074 const char *scope_ptr = stats_scope_ptr(appctx, sc);
Christopher Fauleted7a0662019-01-14 11:07:34 +01003075
Willy Tarreau91cefca2022-05-03 17:08:29 +02003076 if (strnistr(px->id, strlen(px->id), scope_ptr, ctx->scope_len) == NULL)
Christopher Fauleted7a0662019-01-14 11:07:34 +01003077 return 1;
3078 }
William Lallemand74c24fb2016-11-21 17:18:36 +01003079
Willy Tarreau91cefca2022-05-03 17:08:29 +02003080 if ((ctx->flags & STAT_BOUND) &&
3081 (ctx->iid != -1) &&
3082 (px->uuid != ctx->iid))
William Lallemand74c24fb2016-11-21 17:18:36 +01003083 return 1;
3084
Willy Tarreau91cefca2022-05-03 17:08:29 +02003085 ctx->px_st = STAT_PX_ST_TH;
Willy Tarreau9eb93c02022-11-14 07:12:05 +01003086 __fallthrough;
William Lallemand74c24fb2016-11-21 17:18:36 +01003087
3088 case STAT_PX_ST_TH:
Willy Tarreau91cefca2022-05-03 17:08:29 +02003089 if (ctx->flags & STAT_FMT_HTML) {
Willy Tarreaucaff6312022-05-27 10:17:46 +02003090 stats_dump_html_px_hdr(sc, px);
Christopher Fauletef779222018-10-31 08:47:01 +01003091 if (!stats_putchk(rep, htx, &trash))
3092 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01003093 }
3094
Willy Tarreau91cefca2022-05-03 17:08:29 +02003095 ctx->px_st = STAT_PX_ST_FE;
Willy Tarreau9eb93c02022-11-14 07:12:05 +01003096 __fallthrough;
William Lallemand74c24fb2016-11-21 17:18:36 +01003097
3098 case STAT_PX_ST_FE:
3099 /* print the frontend */
Willy Tarreaucaff6312022-05-27 10:17:46 +02003100 if (stats_dump_fe_stats(sc, px)) {
Christopher Fauletef779222018-10-31 08:47:01 +01003101 if (!stats_putchk(rep, htx, &trash))
3102 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01003103 }
3104
Willy Tarreau91cefca2022-05-03 17:08:29 +02003105 ctx->obj2 = px->conf.listeners.n;
3106 ctx->px_st = STAT_PX_ST_LI;
Willy Tarreau9eb93c02022-11-14 07:12:05 +01003107 __fallthrough;
William Lallemand74c24fb2016-11-21 17:18:36 +01003108
3109 case STAT_PX_ST_LI:
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +02003110 /* obj2 points to listeners list as initialized above */
Willy Tarreau91cefca2022-05-03 17:08:29 +02003111 for (; ctx->obj2 != &px->conf.listeners; ctx->obj2 = l->by_fe.n) {
Christopher Fauletef779222018-10-31 08:47:01 +01003112 if (htx) {
3113 if (htx_almost_full(htx))
3114 goto full;
3115 }
3116 else {
3117 if (buffer_almost_full(&rep->buf))
3118 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01003119 }
3120
Willy Tarreau91cefca2022-05-03 17:08:29 +02003121 l = LIST_ELEM(ctx->obj2, struct listener *, by_fe);
William Lallemand74c24fb2016-11-21 17:18:36 +01003122 if (!l->counters)
3123 continue;
3124
Willy Tarreau91cefca2022-05-03 17:08:29 +02003125 if (ctx->flags & STAT_BOUND) {
3126 if (!(ctx->type & (1 << STATS_TYPE_SO)))
William Lallemand74c24fb2016-11-21 17:18:36 +01003127 break;
3128
Willy Tarreau91cefca2022-05-03 17:08:29 +02003129 if (ctx->sid != -1 && l->luid != ctx->sid)
William Lallemand74c24fb2016-11-21 17:18:36 +01003130 continue;
3131 }
3132
3133 /* print the frontend */
Willy Tarreaucaff6312022-05-27 10:17:46 +02003134 if (stats_dump_li_stats(sc, px, l)) {
Christopher Fauletef779222018-10-31 08:47:01 +01003135 if (!stats_putchk(rep, htx, &trash))
3136 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01003137 }
3138 }
3139
Willy Tarreau91cefca2022-05-03 17:08:29 +02003140 ctx->obj2 = px->srv; /* may be NULL */
3141 ctx->px_st = STAT_PX_ST_SV;
Willy Tarreau9eb93c02022-11-14 07:12:05 +01003142 __fallthrough;
William Lallemand74c24fb2016-11-21 17:18:36 +01003143
3144 case STAT_PX_ST_SV:
Amaury Denoyelle0a8d05d2021-08-25 14:39:53 +02003145 /* obj2 points to servers list as initialized above.
3146 *
Amaury Denoyellebc2ebfa2021-08-25 15:34:53 +02003147 * A server may be removed during the stats dumping.
Amaury Denoyelle0a8d05d2021-08-25 14:39:53 +02003148 * Temporarily increment its refcount to prevent its
3149 * anticipated cleaning. Call free_server to release it.
3150 */
Willy Tarreau91cefca2022-05-03 17:08:29 +02003151 for (; ctx->obj2 != NULL;
3152 ctx->obj2 = srv_drop(sv)) {
Amaury Denoyelle0a8d05d2021-08-25 14:39:53 +02003153
Willy Tarreau91cefca2022-05-03 17:08:29 +02003154 sv = ctx->obj2;
Amaury Denoyellebc2ebfa2021-08-25 15:34:53 +02003155 srv_take(sv);
Amaury Denoyelle0a8d05d2021-08-25 14:39:53 +02003156
Christopher Fauletef779222018-10-31 08:47:01 +01003157 if (htx) {
3158 if (htx_almost_full(htx))
3159 goto full;
3160 }
3161 else {
3162 if (buffer_almost_full(&rep->buf))
3163 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01003164 }
3165
Willy Tarreau91cefca2022-05-03 17:08:29 +02003166 if (ctx->flags & STAT_BOUND) {
3167 if (!(ctx->type & (1 << STATS_TYPE_SV))) {
Amaury Denoyellebc2ebfa2021-08-25 15:34:53 +02003168 srv_drop(sv);
William Lallemand74c24fb2016-11-21 17:18:36 +01003169 break;
Amaury Denoyelle0a8d05d2021-08-25 14:39:53 +02003170 }
William Lallemand74c24fb2016-11-21 17:18:36 +01003171
Willy Tarreau91cefca2022-05-03 17:08:29 +02003172 if (ctx->sid != -1 && sv->puid != ctx->sid)
William Lallemand74c24fb2016-11-21 17:18:36 +01003173 continue;
3174 }
3175
Willy Tarreau3e320362020-10-23 17:28:57 +02003176 /* do not report disabled servers */
Willy Tarreau91cefca2022-05-03 17:08:29 +02003177 if (ctx->flags & STAT_HIDE_MAINT &&
Willy Tarreau3e320362020-10-23 17:28:57 +02003178 sv->cur_admin & SRV_ADMF_MAINT) {
3179 continue;
3180 }
3181
William Lallemand74c24fb2016-11-21 17:18:36 +01003182 svs = sv;
3183 while (svs->track)
3184 svs = svs->track;
3185
3186 /* do not report servers which are DOWN and not changing state */
Willy Tarreau91cefca2022-05-03 17:08:29 +02003187 if ((ctx->flags & STAT_HIDE_DOWN) &&
Emeric Brun52a91d32017-08-31 14:41:55 +02003188 ((sv->cur_admin & SRV_ADMF_MAINT) || /* server is in maintenance */
3189 (sv->cur_state == SRV_ST_STOPPED && /* server is down */
William Lallemand74c24fb2016-11-21 17:18:36 +01003190 (!((svs->agent.state | svs->check.state) & CHK_ST_ENABLED) ||
3191 ((svs->agent.state & CHK_ST_ENABLED) && !svs->agent.health) ||
3192 ((svs->check.state & CHK_ST_ENABLED) && !svs->check.health))))) {
3193 continue;
3194 }
3195
Willy Tarreaucaff6312022-05-27 10:17:46 +02003196 if (stats_dump_sv_stats(sc, px, sv)) {
Christopher Fauletef779222018-10-31 08:47:01 +01003197 if (!stats_putchk(rep, htx, &trash))
3198 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01003199 }
3200 } /* for sv */
3201
Willy Tarreau91cefca2022-05-03 17:08:29 +02003202 ctx->px_st = STAT_PX_ST_BE;
Willy Tarreau9eb93c02022-11-14 07:12:05 +01003203 __fallthrough;
William Lallemand74c24fb2016-11-21 17:18:36 +01003204
3205 case STAT_PX_ST_BE:
3206 /* print the backend */
Willy Tarreaucaff6312022-05-27 10:17:46 +02003207 if (stats_dump_be_stats(sc, px)) {
Christopher Fauletef779222018-10-31 08:47:01 +01003208 if (!stats_putchk(rep, htx, &trash))
3209 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01003210 }
3211
Willy Tarreau91cefca2022-05-03 17:08:29 +02003212 ctx->px_st = STAT_PX_ST_END;
Willy Tarreau9eb93c02022-11-14 07:12:05 +01003213 __fallthrough;
William Lallemand74c24fb2016-11-21 17:18:36 +01003214
3215 case STAT_PX_ST_END:
Willy Tarreau91cefca2022-05-03 17:08:29 +02003216 if (ctx->flags & STAT_FMT_HTML) {
Willy Tarreaucaff6312022-05-27 10:17:46 +02003217 stats_dump_html_px_end(sc, px);
Christopher Fauletef779222018-10-31 08:47:01 +01003218 if (!stats_putchk(rep, htx, &trash))
3219 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01003220 }
3221
Willy Tarreau91cefca2022-05-03 17:08:29 +02003222 ctx->px_st = STAT_PX_ST_FIN;
Willy Tarreau9eb93c02022-11-14 07:12:05 +01003223 __fallthrough;
William Lallemand74c24fb2016-11-21 17:18:36 +01003224
3225 case STAT_PX_ST_FIN:
3226 return 1;
3227
3228 default:
3229 /* unknown state, we should put an abort() here ! */
3230 return 1;
3231 }
Christopher Fauletef779222018-10-31 08:47:01 +01003232
3233 full:
Willy Tarreaucaff6312022-05-27 10:17:46 +02003234 sc_need_room(sc);
Christopher Fauletef779222018-10-31 08:47:01 +01003235 return 0;
William Lallemand74c24fb2016-11-21 17:18:36 +01003236}
3237
3238/* Dumps the HTTP stats head block to the trash for and uses the per-uri
3239 * parameters <uri>. The caller is responsible for clearing the trash if needed.
3240 */
Willy Tarreau676c29e2019-10-09 10:50:01 +02003241static void stats_dump_html_head(struct appctx *appctx, struct uri_auth *uri)
William Lallemand74c24fb2016-11-21 17:18:36 +01003242{
Willy Tarreau91cefca2022-05-03 17:08:29 +02003243 struct show_stat_ctx *ctx = appctx->svcctx;
3244
William Lallemand74c24fb2016-11-21 17:18:36 +01003245 /* WARNING! This must fit in the first buffer !!! */
3246 chunk_appendf(&trash,
3247 "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"\n"
3248 "\"http://www.w3.org/TR/html4/loose.dtd\">\n"
3249 "<html><head><title>Statistics Report for " PRODUCT_NAME "%s%s</title>\n"
Thierry Fournierb6b1cde2021-09-24 00:03:07 +02003250 "<link rel=\"icon\" href=\"data:,\">\n"
William Lallemand74c24fb2016-11-21 17:18:36 +01003251 "<meta http-equiv=\"content-type\" content=\"text/html; charset=iso-8859-1\">\n"
3252 "<style type=\"text/css\"><!--\n"
3253 "body {"
3254 " font-family: arial, helvetica, sans-serif;"
3255 " font-size: 12px;"
3256 " font-weight: normal;"
3257 " color: black;"
3258 " background: white;"
3259 "}\n"
3260 "th,td {"
3261 " font-size: 10px;"
3262 "}\n"
3263 "h1 {"
3264 " font-size: x-large;"
3265 " margin-bottom: 0.5em;"
3266 "}\n"
3267 "h2 {"
3268 " font-family: helvetica, arial;"
3269 " font-size: x-large;"
3270 " font-weight: bold;"
3271 " font-style: italic;"
3272 " color: #6020a0;"
3273 " margin-top: 0em;"
3274 " margin-bottom: 0em;"
3275 "}\n"
3276 "h3 {"
3277 " font-family: helvetica, arial;"
3278 " font-size: 16px;"
3279 " font-weight: bold;"
3280 " color: #b00040;"
3281 " background: #e8e8d0;"
3282 " margin-top: 0em;"
3283 " margin-bottom: 0em;"
3284 "}\n"
3285 "li {"
3286 " margin-top: 0.25em;"
3287 " margin-right: 2em;"
3288 "}\n"
3289 ".hr {margin-top: 0.25em;"
3290 " border-color: black;"
3291 " border-bottom-style: solid;"
3292 "}\n"
3293 ".titre {background: #20D0D0;color: #000000; font-weight: bold; text-align: center;}\n"
3294 ".total {background: #20D0D0;color: #ffff80;}\n"
3295 ".frontend {background: #e8e8d0;}\n"
3296 ".socket {background: #d0d0d0;}\n"
3297 ".backend {background: #e8e8d0;}\n"
3298 ".active_down {background: #ff9090;}\n"
3299 ".active_going_up {background: #ffd020;}\n"
3300 ".active_going_down {background: #ffffa0;}\n"
3301 ".active_up {background: #c0ffc0;}\n"
3302 ".active_nolb {background: #20a0ff;}\n"
3303 ".active_draining {background: #20a0FF;}\n"
3304 ".active_no_check {background: #e0e0e0;}\n"
3305 ".backup_down {background: #ff9090;}\n"
3306 ".backup_going_up {background: #ff80ff;}\n"
3307 ".backup_going_down {background: #c060ff;}\n"
3308 ".backup_up {background: #b0d0ff;}\n"
3309 ".backup_nolb {background: #90b0e0;}\n"
3310 ".backup_draining {background: #cc9900;}\n"
3311 ".backup_no_check {background: #e0e0e0;}\n"
3312 ".maintain {background: #c07820;}\n"
3313 ".rls {letter-spacing: 0.2em; margin-right: 1px;}\n" /* right letter spacing (used for grouping digits) */
3314 "\n"
3315 "a.px:link {color: #ffff40; text-decoration: none;}"
3316 "a.px:visited {color: #ffff40; text-decoration: none;}"
3317 "a.px:hover {color: #ffffff; text-decoration: none;}"
3318 "a.lfsb:link {color: #000000; text-decoration: none;}"
3319 "a.lfsb:visited {color: #000000; text-decoration: none;}"
3320 "a.lfsb:hover {color: #505050; text-decoration: none;}"
3321 "\n"
3322 "table.tbl { border-collapse: collapse; border-style: none;}\n"
3323 "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"
3324 "table.tbl td.ac { text-align: center;}\n"
3325 "table.tbl th { border-width: 1px; border-style: solid solid solid solid; border-color: gray;}\n"
3326 "table.tbl th.pxname { background: #b00040; color: #ffff40; font-weight: bold; border-style: solid solid none solid; padding: 2px 3px; white-space: nowrap;}\n"
3327 "table.tbl th.empty { border-style: none; empty-cells: hide; background: white;}\n"
3328 "table.tbl th.desc { background: white; border-style: solid solid none solid; text-align: left; padding: 2px 3px;}\n"
3329 "\n"
3330 "table.lgd { border-collapse: collapse; border-width: 1px; border-style: none none none solid; border-color: black;}\n"
3331 "table.lgd td { border-width: 1px; border-style: solid solid solid solid; border-color: gray; padding: 2px;}\n"
3332 "table.lgd td.noborder { border-style: none; padding: 2px; white-space: nowrap;}\n"
3333 "table.det { border-collapse: collapse; border-style: none; }\n"
3334 "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"
3335 "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"
3336 "u {text-decoration:none; border-bottom: 1px dotted black;}\n"
Thierry Fournierb6b1cde2021-09-24 00:03:07 +02003337 "div.tips {\n"
3338 " display:block;\n"
3339 " visibility:hidden;\n"
3340 " z-index:2147483647;\n"
3341 " position:absolute;\n"
3342 " padding:2px 4px 3px;\n"
3343 " background:#f0f060; color:#000000;\n"
3344 " border:1px solid #7040c0;\n"
3345 " white-space:nowrap;\n"
3346 " font-style:normal;font-size:11px;font-weight:normal;\n"
3347 " -moz-border-radius:3px;-webkit-border-radius:3px;border-radius:3px;\n"
3348 " -moz-box-shadow:gray 2px 2px 3px;-webkit-box-shadow:gray 2px 2px 3px;box-shadow:gray 2px 2px 3px;\n"
3349 "}\n"
3350 "u:hover div.tips {visibility:visible;}\n"
Thierry Fourniere9ed63e2021-09-23 23:59:49 +02003351 "@media (prefers-color-scheme: dark) {\n"
3352 " body { font-family: arial, helvetica, sans-serif; font-size: 12px; font-weight: normal; color: #e8e6e3; background: #131516;}\n"
3353 " h1 { color: #a265e0!important; }\n"
3354 " h2 { color: #a265e0; }\n"
3355 " h3 { color: #ff5190; background-color: #3e3e1f; }\n"
3356 " a { color: #3391ff; }\n"
3357 " input { background-color: #2f3437; }\n"
3358 " .hr { border-color: #8c8273; }\n"
3359 " .titre { background-color: #1aa6a6; color: #e8e6e3; }\n"
3360 " .frontend {background: #2f3437;}\n"
Marno Krahmera690b732022-03-08 13:45:09 +01003361 " .socket {background: #2a2d2f;}\n"
Thierry Fourniere9ed63e2021-09-23 23:59:49 +02003362 " .backend {background: #2f3437;}\n"
3363 " .active_down {background: #760000;}\n"
3364 " .active_going_up {background: #b99200;}\n"
3365 " .active_going_down {background: #6c6c00;}\n"
3366 " .active_up {background: #165900;}\n"
3367 " .active_nolb {background: #006ab9;}\n"
3368 " .active_draining {background: #006ab9;}\n"
3369 " .active_no_check {background: #2a2d2f;}\n"
3370 " .backup_down {background: #760000;}\n"
3371 " .backup_going_up {background: #7f007f;}\n"
3372 " .backup_going_down {background: #580092;}\n"
3373 " .backup_up {background: #2e3234;}\n"
3374 " .backup_nolb {background: #1e3c6a;}\n"
3375 " .backup_draining {background: #a37a00;}\n"
3376 " .backup_no_check {background: #2a2d2f;}\n"
3377 " .maintain {background: #9a601a;}\n"
3378 " a.px:link {color: #d8d83b; text-decoration: none;}\n"
3379 " a.px:visited {color: #d8d83b; text-decoration: none;}\n"
3380 " a.px:hover {color: #ffffff; text-decoration: none;}\n"
3381 " a.lfsb:link {color: #e8e6e3; text-decoration: none;}\n"
3382 " a.lfsb:visited {color: #e8e6e3; text-decoration: none;}\n"
3383 " a.lfsb:hover {color: #b5afa6; text-decoration: none;}\n"
3384 " table.tbl th.empty { background-color: #181a1b; }\n"
Willy Tarreau00147f72022-04-11 07:59:27 +02003385 " table.tbl th.desc { background: #181a1b; }\n"
Thierry Fourniere9ed63e2021-09-23 23:59:49 +02003386 " table.tbl th.pxname { background-color: #8d0033; color: #ffff46; }\n"
3387 " table.tbl th { border-color: #808080; }\n"
3388 " table.tbl td { border-color: #808080; }\n"
3389 " u {text-decoration:none; border-bottom: 1px dotted #e8e6e3;}\n"
3390 " div.tips {\n"
3391 " background:#8e8e0d;\n"
3392 " color:#e8e6e3;\n"
3393 " border-color: #4e2c86;\n"
3394 " -moz-box-shadow: #60686c 2px 2px 3px;\n"
3395 " -webkit-box-shadow: #60686c 2px 2px 3px;\n"
3396 " box-shadow: #60686c 2px 2px 3px;\n"
3397 " }\n"
3398 "}\n"
William Lallemand74c24fb2016-11-21 17:18:36 +01003399 "-->\n"
3400 "</style></head>\n",
Willy Tarreau91cefca2022-05-03 17:08:29 +02003401 (ctx->flags & STAT_SHNODE) ? " on " : "",
3402 (ctx->flags & STAT_SHNODE) ? (uri && uri->node ? uri->node : global.node) : ""
William Lallemand74c24fb2016-11-21 17:18:36 +01003403 );
3404}
3405
3406/* Dumps the HTML stats information block to the trash for and uses the state from
Willy Tarreaucaff6312022-05-27 10:17:46 +02003407 * stream connector <sc> and per-uri parameters <uri>. The caller is responsible
William Lallemand74c24fb2016-11-21 17:18:36 +01003408 * for clearing the trash if needed.
3409 */
Willy Tarreaucaff6312022-05-27 10:17:46 +02003410static void stats_dump_html_info(struct stconn *sc, struct uri_auth *uri)
William Lallemand74c24fb2016-11-21 17:18:36 +01003411{
Willy Tarreaucaff6312022-05-27 10:17:46 +02003412 struct appctx *appctx = __sc_appctx(sc);
Willy Tarreau91cefca2022-05-03 17:08:29 +02003413 struct show_stat_ctx *ctx = appctx->svcctx;
William Lallemand74c24fb2016-11-21 17:18:36 +01003414 unsigned int up = (now.tv_sec - start_date.tv_sec);
3415 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
Willy Tarreaucaff6312022-05-27 10:17:46 +02003416 const char *scope_ptr = stats_scope_ptr(appctx, sc);
Willy Tarreau1713c032019-05-23 12:23:55 +02003417 unsigned long long bps = (unsigned long long)read_freq_ctr(&global.out_32bps) * 32;
3418
3419 /* Turn the bytes per second to bits per second and take care of the
3420 * usual ethernet overhead in order to help figure how far we are from
3421 * interface saturation since it's the only case which usually matters.
3422 * For this we count the total size of an Ethernet frame on the wire
3423 * including preamble and IFG (1538) for the largest TCP segment it
3424 * transports (1448 with TCP timestamps). This is not valid for smaller
3425 * packets (under-estimated), but it gives a reasonably accurate
3426 * estimation of how far we are from uplink saturation.
3427 */
3428 bps = bps * 8 * 1538 / 1448;
William Lallemand74c24fb2016-11-21 17:18:36 +01003429
3430 /* WARNING! this has to fit the first packet too.
3431 * We are around 3.5 kB, add adding entries will
3432 * become tricky if we want to support 4kB buffers !
3433 */
3434 chunk_appendf(&trash,
3435 "<body><h1><a href=\"" PRODUCT_URL "\" style=\"text-decoration: none;\">"
3436 PRODUCT_NAME "%s</a></h1>\n"
3437 "<h2>Statistics Report for pid %d%s%s%s%s</h2>\n"
3438 "<hr width=\"100%%\" class=\"hr\">\n"
3439 "<h3>&gt; General process information</h3>\n"
3440 "<table border=0><tr><td align=\"left\" nowrap width=\"1%%\">\n"
Yves Lafon95317282018-02-26 11:10:37 +01003441 "<p><b>pid = </b> %d (process #%d, nbproc = %d, nbthread = %d)<br>\n"
William Lallemand74c24fb2016-11-21 17:18:36 +01003442 "<b>uptime = </b> %dd %dh%02dm%02ds<br>\n"
3443 "<b>system limits:</b> memmax = %s%s; ulimit-n = %d<br>\n"
3444 "<b>maxsock = </b> %d; <b>maxconn = </b> %d; <b>maxpipes = </b> %d<br>\n"
Willy Tarreau1713c032019-05-23 12:23:55 +02003445 "current conns = %d; current pipes = %d/%d; conn rate = %d/sec; bit rate = %.3f %cbps<br>\n"
William Lallemand74c24fb2016-11-21 17:18:36 +01003446 "Running tasks: %d/%d; idle = %d %%<br>\n"
3447 "</td><td align=\"center\" nowrap>\n"
3448 "<table class=\"lgd\"><tr>\n"
3449 "<td class=\"active_up\">&nbsp;</td><td class=\"noborder\">active UP </td>"
3450 "<td class=\"backup_up\">&nbsp;</td><td class=\"noborder\">backup UP </td>"
3451 "</tr><tr>\n"
3452 "<td class=\"active_going_down\"></td><td class=\"noborder\">active UP, going down </td>"
3453 "<td class=\"backup_going_down\"></td><td class=\"noborder\">backup UP, going down </td>"
3454 "</tr><tr>\n"
3455 "<td class=\"active_going_up\"></td><td class=\"noborder\">active DOWN, going up </td>"
3456 "<td class=\"backup_going_up\"></td><td class=\"noborder\">backup DOWN, going up </td>"
3457 "</tr><tr>\n"
3458 "<td class=\"active_down\"></td><td class=\"noborder\">active or backup DOWN &nbsp;</td>"
3459 "<td class=\"active_no_check\"></td><td class=\"noborder\">not checked </td>"
3460 "</tr><tr>\n"
3461 "<td class=\"maintain\"></td><td class=\"noborder\" colspan=\"3\">active or backup DOWN for maintenance (MAINT) &nbsp;</td>"
3462 "</tr><tr>\n"
3463 "<td class=\"active_draining\"></td><td class=\"noborder\" colspan=\"3\">active or backup SOFT STOPPED for maintenance &nbsp;</td>"
3464 "</tr></table>\n"
3465 "Note: \"NOLB\"/\"DRAIN\" = UP with load-balancing disabled."
3466 "</td>"
3467 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
3468 "<b>Display option:</b><ul style=\"margin-top: 0.25em;\">"
3469 "",
Willy Tarreau91cefca2022-05-03 17:08:29 +02003470 (ctx->flags & STAT_HIDEVER) ? "" : (stats_version_string),
3471 pid, (ctx->flags & STAT_SHNODE) ? " on " : "",
3472 (ctx->flags & STAT_SHNODE) ? (uri->node ? uri->node : global.node) : "",
3473 (ctx->flags & STAT_SHDESC) ? ": " : "",
3474 (ctx->flags & STAT_SHDESC) ? (uri->desc ? uri->desc : global.desc) : "",
Willy Tarreaue8422bf2021-06-15 09:08:18 +02003475 pid, 1, 1, global.nbthread,
William Lallemand74c24fb2016-11-21 17:18:36 +01003476 up / 86400, (up % 86400) / 3600,
3477 (up % 3600) / 60, (up % 60),
3478 global.rlimit_memmax ? ultoa(global.rlimit_memmax) : "unlimited",
3479 global.rlimit_memmax ? " MB" : "",
3480 global.rlimit_nofile,
3481 global.maxsock, global.maxconn, global.maxpipes,
3482 actconn, pipes_used, pipes_used+pipes_free, read_freq_ctr(&global.conn_per_sec),
Willy Tarreau1713c032019-05-23 12:23:55 +02003483 bps >= 1000000000UL ? (bps / 1000000000.0) : bps >= 1000000UL ? (bps / 1000000.0) : (bps / 1000.0),
3484 bps >= 1000000000UL ? 'G' : bps >= 1000000UL ? 'M' : 'k',
Willy Tarreauf9d5e102021-10-08 10:43:59 +02003485 total_run_queues(), total_allocated_tasks(), clock_report_idle()
William Lallemand74c24fb2016-11-21 17:18:36 +01003486 );
3487
Willy Tarreau91cefca2022-05-03 17:08:29 +02003488 /* scope_txt = search query, ctx->scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
3489 memcpy(scope_txt, scope_ptr, ctx->scope_len);
3490 scope_txt[ctx->scope_len] = '\0';
William Lallemand74c24fb2016-11-21 17:18:36 +01003491
3492 chunk_appendf(&trash,
3493 "<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 +02003494 (ctx->scope_len > 0) ? scope_txt : "",
William Lallemand74c24fb2016-11-21 17:18:36 +01003495 STAT_SCOPE_TXT_MAXLEN);
3496
Willy Tarreau91cefca2022-05-03 17:08:29 +02003497 /* scope_txt = search pattern + search query, ctx->scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
William Lallemand74c24fb2016-11-21 17:18:36 +01003498 scope_txt[0] = 0;
Willy Tarreau91cefca2022-05-03 17:08:29 +02003499 if (ctx->scope_len) {
William Lallemand74c24fb2016-11-21 17:18:36 +01003500 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Willy Tarreau91cefca2022-05-03 17:08:29 +02003501 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), scope_ptr, ctx->scope_len);
3502 scope_txt[strlen(STAT_SCOPE_PATTERN) + ctx->scope_len] = 0;
William Lallemand74c24fb2016-11-21 17:18:36 +01003503 }
3504
Willy Tarreau91cefca2022-05-03 17:08:29 +02003505 if (ctx->flags & STAT_HIDE_DOWN)
William Lallemand74c24fb2016-11-21 17:18:36 +01003506 chunk_appendf(&trash,
3507 "<li><a href=\"%s%s%s%s\">Show all servers</a><br>\n",
3508 uri->uri_prefix,
3509 "",
Willy Tarreau91cefca2022-05-03 17:08:29 +02003510 (ctx->flags & STAT_NO_REFRESH) ? ";norefresh" : "",
William Lallemand74c24fb2016-11-21 17:18:36 +01003511 scope_txt);
3512 else
3513 chunk_appendf(&trash,
3514 "<li><a href=\"%s%s%s%s\">Hide 'DOWN' servers</a><br>\n",
3515 uri->uri_prefix,
3516 ";up",
Willy Tarreau91cefca2022-05-03 17:08:29 +02003517 (ctx->flags & STAT_NO_REFRESH) ? ";norefresh" : "",
William Lallemand74c24fb2016-11-21 17:18:36 +01003518 scope_txt);
3519
3520 if (uri->refresh > 0) {
Willy Tarreau91cefca2022-05-03 17:08:29 +02003521 if (ctx->flags & STAT_NO_REFRESH)
William Lallemand74c24fb2016-11-21 17:18:36 +01003522 chunk_appendf(&trash,
3523 "<li><a href=\"%s%s%s%s\">Enable refresh</a><br>\n",
3524 uri->uri_prefix,
Willy Tarreau91cefca2022-05-03 17:08:29 +02003525 (ctx->flags & STAT_HIDE_DOWN) ? ";up" : "",
William Lallemand74c24fb2016-11-21 17:18:36 +01003526 "",
3527 scope_txt);
3528 else
3529 chunk_appendf(&trash,
3530 "<li><a href=\"%s%s%s%s\">Disable refresh</a><br>\n",
3531 uri->uri_prefix,
Willy Tarreau91cefca2022-05-03 17:08:29 +02003532 (ctx->flags & STAT_HIDE_DOWN) ? ";up" : "",
William Lallemand74c24fb2016-11-21 17:18:36 +01003533 ";norefresh",
3534 scope_txt);
3535 }
3536
3537 chunk_appendf(&trash,
3538 "<li><a href=\"%s%s%s%s\">Refresh now</a><br>\n",
3539 uri->uri_prefix,
Willy Tarreau91cefca2022-05-03 17:08:29 +02003540 (ctx->flags & STAT_HIDE_DOWN) ? ";up" : "",
3541 (ctx->flags & STAT_NO_REFRESH) ? ";norefresh" : "",
William Lallemand74c24fb2016-11-21 17:18:36 +01003542 scope_txt);
3543
3544 chunk_appendf(&trash,
3545 "<li><a href=\"%s;csv%s%s\">CSV export</a><br>\n",
3546 uri->uri_prefix,
3547 (uri->refresh > 0) ? ";norefresh" : "",
3548 scope_txt);
3549
3550 chunk_appendf(&trash,
Christopher Faulet6338a082019-09-09 15:50:54 +02003551 "<li><a href=\"%s;json%s%s\">JSON export</a> (<a href=\"%s;json-schema\">schema</a>)<br>\n",
3552 uri->uri_prefix,
3553 (uri->refresh > 0) ? ";norefresh" : "",
3554 scope_txt, uri->uri_prefix);
3555
3556 chunk_appendf(&trash,
William Lallemand74c24fb2016-11-21 17:18:36 +01003557 "</ul></td>"
3558 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
3559 "<b>External resources:</b><ul style=\"margin-top: 0.25em;\">\n"
3560 "<li><a href=\"" PRODUCT_URL "\">Primary site</a><br>\n"
3561 "<li><a href=\"" PRODUCT_URL_UPD "\">Updates (v" PRODUCT_BRANCH ")</a><br>\n"
3562 "<li><a href=\"" PRODUCT_URL_DOC "\">Online manual</a><br>\n"
3563 "</ul>"
3564 "</td>"
3565 "</tr></table>\n"
3566 ""
3567 );
3568
Willy Tarreau91cefca2022-05-03 17:08:29 +02003569 if (ctx->st_code) {
3570 switch (ctx->st_code) {
William Lallemand74c24fb2016-11-21 17:18:36 +01003571 case STAT_STATUS_DONE:
3572 chunk_appendf(&trash,
3573 "<p><div class=active_up>"
3574 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
3575 "Action processed successfully."
3576 "</div>\n", uri->uri_prefix,
Willy Tarreau91cefca2022-05-03 17:08:29 +02003577 (ctx->flags & STAT_HIDE_DOWN) ? ";up" : "",
3578 (ctx->flags & STAT_NO_REFRESH) ? ";norefresh" : "",
William Lallemand74c24fb2016-11-21 17:18:36 +01003579 scope_txt);
3580 break;
3581 case STAT_STATUS_NONE:
3582 chunk_appendf(&trash,
3583 "<p><div class=active_going_down>"
3584 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
3585 "Nothing has changed."
3586 "</div>\n", uri->uri_prefix,
Willy Tarreau91cefca2022-05-03 17:08:29 +02003587 (ctx->flags & STAT_HIDE_DOWN) ? ";up" : "",
3588 (ctx->flags & STAT_NO_REFRESH) ? ";norefresh" : "",
William Lallemand74c24fb2016-11-21 17:18:36 +01003589 scope_txt);
3590 break;
3591 case STAT_STATUS_PART:
3592 chunk_appendf(&trash,
3593 "<p><div class=active_going_down>"
3594 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
3595 "Action partially processed.<br>"
3596 "Some server names are probably unknown or ambiguous (duplicated names in the backend)."
3597 "</div>\n", uri->uri_prefix,
Willy Tarreau91cefca2022-05-03 17:08:29 +02003598 (ctx->flags & STAT_HIDE_DOWN) ? ";up" : "",
3599 (ctx->flags & STAT_NO_REFRESH) ? ";norefresh" : "",
William Lallemand74c24fb2016-11-21 17:18:36 +01003600 scope_txt);
3601 break;
3602 case STAT_STATUS_ERRP:
3603 chunk_appendf(&trash,
3604 "<p><div class=active_down>"
3605 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
3606 "Action not processed because of invalid parameters."
3607 "<ul>"
3608 "<li>The action is maybe unknown.</li>"
Christopher Faulet2f9a41d2019-02-27 15:30:57 +01003609 "<li>Invalid key parameter (empty or too long).</li>"
William Lallemand74c24fb2016-11-21 17:18:36 +01003610 "<li>The backend name is probably unknown or ambiguous (duplicated names).</li>"
3611 "<li>Some server names are probably unknown or ambiguous (duplicated names in the backend).</li>"
3612 "</ul>"
3613 "</div>\n", uri->uri_prefix,
Willy Tarreau91cefca2022-05-03 17:08:29 +02003614 (ctx->flags & STAT_HIDE_DOWN) ? ";up" : "",
3615 (ctx->flags & STAT_NO_REFRESH) ? ";norefresh" : "",
William Lallemand74c24fb2016-11-21 17:18:36 +01003616 scope_txt);
3617 break;
3618 case STAT_STATUS_EXCD:
3619 chunk_appendf(&trash,
3620 "<p><div class=active_down>"
3621 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
3622 "<b>Action not processed : the buffer couldn't store all the data.<br>"
3623 "You should retry with less servers at a time.</b>"
3624 "</div>\n", uri->uri_prefix,
Willy Tarreau91cefca2022-05-03 17:08:29 +02003625 (ctx->flags & STAT_HIDE_DOWN) ? ";up" : "",
3626 (ctx->flags & STAT_NO_REFRESH) ? ";norefresh" : "",
William Lallemand74c24fb2016-11-21 17:18:36 +01003627 scope_txt);
3628 break;
3629 case STAT_STATUS_DENY:
3630 chunk_appendf(&trash,
3631 "<p><div class=active_down>"
3632 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
3633 "<b>Action denied.</b>"
3634 "</div>\n", uri->uri_prefix,
Willy Tarreau91cefca2022-05-03 17:08:29 +02003635 (ctx->flags & STAT_HIDE_DOWN) ? ";up" : "",
3636 (ctx->flags & STAT_NO_REFRESH) ? ";norefresh" : "",
William Lallemand74c24fb2016-11-21 17:18:36 +01003637 scope_txt);
3638 break;
Christopher Faulet3c2ecf72019-02-27 16:41:27 +01003639 case STAT_STATUS_IVAL:
3640 chunk_appendf(&trash,
3641 "<p><div class=active_down>"
3642 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
3643 "<b>Invalid requests (unsupported method or chunked encoded request).</b>"
3644 "</div>\n", uri->uri_prefix,
Willy Tarreau91cefca2022-05-03 17:08:29 +02003645 (ctx->flags & STAT_HIDE_DOWN) ? ";up" : "",
3646 (ctx->flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Christopher Faulet3c2ecf72019-02-27 16:41:27 +01003647 scope_txt);
3648 break;
William Lallemand74c24fb2016-11-21 17:18:36 +01003649 default:
3650 chunk_appendf(&trash,
3651 "<p><div class=active_no_check>"
3652 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
3653 "Unexpected result."
3654 "</div>\n", uri->uri_prefix,
Willy Tarreau91cefca2022-05-03 17:08:29 +02003655 (ctx->flags & STAT_HIDE_DOWN) ? ";up" : "",
3656 (ctx->flags & STAT_NO_REFRESH) ? ";norefresh" : "",
William Lallemand74c24fb2016-11-21 17:18:36 +01003657 scope_txt);
3658 }
3659 chunk_appendf(&trash, "<p>\n");
3660 }
3661}
3662
3663/* Dumps the HTML stats trailer block to the trash. The caller is responsible
3664 * for clearing the trash if needed.
3665 */
3666static void stats_dump_html_end()
3667{
3668 chunk_appendf(&trash, "</body></html>\n");
3669}
3670
Simon Horman05ee2132017-01-04 09:37:25 +01003671/* Dumps the stats JSON header to the trash buffer which. The caller is responsible
3672 * for clearing it if needed.
3673 */
3674static void stats_dump_json_header()
3675{
3676 chunk_strcat(&trash, "[");
3677}
3678
3679
3680/* Dumps the JSON stats trailer block to the trash. The caller is responsible
3681 * for clearing the trash if needed.
3682 */
3683static void stats_dump_json_end()
3684{
Willy Tarreau5a0e7ca2022-06-10 09:21:22 +02003685 chunk_strcat(&trash, "]\n");
Simon Horman05ee2132017-01-04 09:37:25 +01003686}
3687
Amaury Denoyelle98b81cb2020-10-02 18:32:04 +02003688/* Uses <appctx.ctx.stats.obj1> as a pointer to the current proxy and <obj2> as
3689 * a pointer to the current server/listener.
3690 */
Willy Tarreaucaff6312022-05-27 10:17:46 +02003691static int stats_dump_proxies(struct stconn *sc,
Amaury Denoyelle98b81cb2020-10-02 18:32:04 +02003692 struct htx *htx,
3693 struct uri_auth *uri)
3694{
Willy Tarreaucaff6312022-05-27 10:17:46 +02003695 struct appctx *appctx = __sc_appctx(sc);
Willy Tarreau91cefca2022-05-03 17:08:29 +02003696 struct show_stat_ctx *ctx = appctx->svcctx;
Willy Tarreaucaff6312022-05-27 10:17:46 +02003697 struct channel *rep = sc_ic(sc);
Amaury Denoyelle98b81cb2020-10-02 18:32:04 +02003698 struct proxy *px;
3699
3700 /* dump proxies */
Willy Tarreau91cefca2022-05-03 17:08:29 +02003701 while (ctx->obj1) {
Amaury Denoyelle98b81cb2020-10-02 18:32:04 +02003702 if (htx) {
3703 if (htx_almost_full(htx))
3704 goto full;
3705 }
3706 else {
3707 if (buffer_almost_full(&rep->buf))
3708 goto full;
3709 }
3710
Willy Tarreau91cefca2022-05-03 17:08:29 +02003711 px = ctx->obj1;
Marno Krahmer07954fb2021-06-24 16:51:08 +02003712 /* Skip the global frontend proxies and non-networked ones.
William Lallemand85a16b22021-08-03 13:18:11 +02003713 * Also skip proxies that were disabled in the configuration
Marno Krahmer07954fb2021-06-24 16:51:08 +02003714 * This change allows retrieving stats from "old" proxies after a reload.
3715 */
Christopher Fauletdfd10ab2021-10-06 14:24:19 +02003716 if (!(px->flags & PR_FL_DISABLED) && px->uuid > 0 &&
William Lallemande7f74622021-07-28 17:45:18 +02003717 (px->cap & (PR_CAP_FE | PR_CAP_BE)) && !(px->cap & PR_CAP_INT)) {
Willy Tarreaucaff6312022-05-27 10:17:46 +02003718 if (stats_dump_proxy_to_buffer(sc, htx, px, uri) == 0)
Amaury Denoyelle98b81cb2020-10-02 18:32:04 +02003719 return 0;
3720 }
3721
Willy Tarreau91cefca2022-05-03 17:08:29 +02003722 ctx->obj1 = px->next;
3723 ctx->px_st = STAT_PX_ST_INIT;
Amaury Denoyelle98b81cb2020-10-02 18:32:04 +02003724 }
3725
3726 return 1;
3727
3728 full:
Willy Tarreaucaff6312022-05-27 10:17:46 +02003729 sc_need_room(sc);
Amaury Denoyelle98b81cb2020-10-02 18:32:04 +02003730 return 0;
3731}
3732
Willy Tarreau4596fe22022-05-17 19:07:51 +02003733/* This function dumps statistics onto the stream connector's read buffer in
William Lallemand74c24fb2016-11-21 17:18:36 +01003734 * either CSV or HTML format. <uri> contains some HTML-specific parameters that
3735 * are ignored for CSV format (hence <uri> may be NULL there). It returns 0 if
3736 * it had to stop writing data and an I/O is needed, 1 if the dump is finished
3737 * and the stream must be closed, or -1 in case of any error. This function is
3738 * used by both the CLI and the HTTP handlers.
3739 */
Willy Tarreaucaff6312022-05-27 10:17:46 +02003740static int stats_dump_stat_to_buffer(struct stconn *sc, struct htx *htx,
Christopher Fauletef779222018-10-31 08:47:01 +01003741 struct uri_auth *uri)
William Lallemand74c24fb2016-11-21 17:18:36 +01003742{
Willy Tarreaucaff6312022-05-27 10:17:46 +02003743 struct appctx *appctx = __sc_appctx(sc);
Willy Tarreau91cefca2022-05-03 17:08:29 +02003744 struct show_stat_ctx *ctx = appctx->svcctx;
Willy Tarreaucaff6312022-05-27 10:17:46 +02003745 struct channel *rep = sc_ic(sc);
Willy Tarreau91cefca2022-05-03 17:08:29 +02003746 enum stats_domain domain = ctx->domain;
William Lallemand74c24fb2016-11-21 17:18:36 +01003747
3748 chunk_reset(&trash);
3749
Willy Tarreau41f88522022-05-03 18:39:27 +02003750 switch (ctx->state) {
Willy Tarreau6ef16482022-05-06 18:07:53 +02003751 case STAT_STATE_INIT:
3752 ctx->state = STAT_STATE_HEAD; /* let's start producing data */
Willy Tarreau9eb93c02022-11-14 07:12:05 +01003753 __fallthrough;
William Lallemand74c24fb2016-11-21 17:18:36 +01003754
Willy Tarreau6ef16482022-05-06 18:07:53 +02003755 case STAT_STATE_HEAD:
Willy Tarreau91cefca2022-05-03 17:08:29 +02003756 if (ctx->flags & STAT_FMT_HTML)
Willy Tarreau676c29e2019-10-09 10:50:01 +02003757 stats_dump_html_head(appctx, uri);
Willy Tarreau91cefca2022-05-03 17:08:29 +02003758 else if (ctx->flags & STAT_JSON_SCHM)
Christopher Faulet6338a082019-09-09 15:50:54 +02003759 stats_dump_json_schema(&trash);
Willy Tarreau91cefca2022-05-03 17:08:29 +02003760 else if (ctx->flags & STAT_FMT_JSON)
Willy Tarreau9d7fb632017-04-11 07:53:04 +02003761 stats_dump_json_header();
Willy Tarreau91cefca2022-05-03 17:08:29 +02003762 else if (!(ctx->flags & STAT_FMT_TYPED))
3763 stats_dump_csv_header(ctx->domain);
William Lallemand74c24fb2016-11-21 17:18:36 +01003764
Christopher Fauletef779222018-10-31 08:47:01 +01003765 if (!stats_putchk(rep, htx, &trash))
3766 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01003767
Willy Tarreau91cefca2022-05-03 17:08:29 +02003768 if (ctx->flags & STAT_JSON_SCHM) {
Willy Tarreau6ef16482022-05-06 18:07:53 +02003769 ctx->state = STAT_STATE_FIN;
Christopher Faulet6338a082019-09-09 15:50:54 +02003770 return 1;
3771 }
Willy Tarreau6ef16482022-05-06 18:07:53 +02003772 ctx->state = STAT_STATE_INFO;
Willy Tarreau9eb93c02022-11-14 07:12:05 +01003773 __fallthrough;
William Lallemand74c24fb2016-11-21 17:18:36 +01003774
Willy Tarreau6ef16482022-05-06 18:07:53 +02003775 case STAT_STATE_INFO:
Willy Tarreau91cefca2022-05-03 17:08:29 +02003776 if (ctx->flags & STAT_FMT_HTML) {
Willy Tarreaucaff6312022-05-27 10:17:46 +02003777 stats_dump_html_info(sc, uri);
Christopher Fauletef779222018-10-31 08:47:01 +01003778 if (!stats_putchk(rep, htx, &trash))
3779 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01003780 }
3781
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02003782 if (domain == STATS_DOMAIN_PROXY)
Willy Tarreau91cefca2022-05-03 17:08:29 +02003783 ctx->obj1 = proxies_list;
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02003784
Willy Tarreau91cefca2022-05-03 17:08:29 +02003785 ctx->px_st = STAT_PX_ST_INIT;
Willy Tarreau6ef16482022-05-06 18:07:53 +02003786 ctx->state = STAT_STATE_LIST;
Willy Tarreau9eb93c02022-11-14 07:12:05 +01003787 __fallthrough;
William Lallemand74c24fb2016-11-21 17:18:36 +01003788
Willy Tarreau6ef16482022-05-06 18:07:53 +02003789 case STAT_STATE_LIST:
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02003790 switch (domain) {
Emeric Brunf8642ee2021-10-29 17:59:18 +02003791 case STATS_DOMAIN_RESOLVERS:
Willy Tarreaucaff6312022-05-27 10:17:46 +02003792 if (!stats_dump_resolvers(sc, stat_l[domain],
Emeric Brund3b44952021-01-06 16:01:02 +01003793 stat_count[domain],
3794 &stats_module_list[domain])) {
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02003795 return 0;
3796 }
3797 break;
3798
3799 case STATS_DOMAIN_PROXY:
3800 default:
3801 /* dump proxies */
Willy Tarreaucaff6312022-05-27 10:17:46 +02003802 if (!stats_dump_proxies(sc, htx, uri))
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02003803 return 0;
3804 break;
3805 }
William Lallemand74c24fb2016-11-21 17:18:36 +01003806
Willy Tarreau6ef16482022-05-06 18:07:53 +02003807 ctx->state = STAT_STATE_END;
Willy Tarreau9eb93c02022-11-14 07:12:05 +01003808 __fallthrough;
William Lallemand74c24fb2016-11-21 17:18:36 +01003809
Willy Tarreau6ef16482022-05-06 18:07:53 +02003810 case STAT_STATE_END:
Willy Tarreau91cefca2022-05-03 17:08:29 +02003811 if (ctx->flags & (STAT_FMT_HTML|STAT_FMT_JSON)) {
3812 if (ctx->flags & STAT_FMT_HTML)
Simon Horman05ee2132017-01-04 09:37:25 +01003813 stats_dump_html_end();
3814 else
3815 stats_dump_json_end();
Christopher Fauletef779222018-10-31 08:47:01 +01003816 if (!stats_putchk(rep, htx, &trash))
3817 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01003818 }
3819
Willy Tarreau6ef16482022-05-06 18:07:53 +02003820 ctx->state = STAT_STATE_FIN;
Willy Tarreau9eb93c02022-11-14 07:12:05 +01003821 __fallthrough;
William Lallemand74c24fb2016-11-21 17:18:36 +01003822
Willy Tarreau6ef16482022-05-06 18:07:53 +02003823 case STAT_STATE_FIN:
William Lallemand74c24fb2016-11-21 17:18:36 +01003824 return 1;
3825
3826 default:
3827 /* unknown state ! */
Willy Tarreau6ef16482022-05-06 18:07:53 +02003828 ctx->state = STAT_STATE_FIN;
William Lallemand74c24fb2016-11-21 17:18:36 +01003829 return -1;
3830 }
Christopher Fauletef779222018-10-31 08:47:01 +01003831
3832 full:
Willy Tarreaucaff6312022-05-27 10:17:46 +02003833 sc_need_room(sc);
Christopher Fauletef779222018-10-31 08:47:01 +01003834 return 0;
3835
William Lallemand74c24fb2016-11-21 17:18:36 +01003836}
3837
3838/* We reached the stats page through a POST request. The appctx is
3839 * expected to have already been allocated by the caller.
3840 * Parse the posted data and enable/disable servers if necessary.
3841 * Returns 1 if request was parsed or zero if it needs more data.
3842 */
Willy Tarreaucaff6312022-05-27 10:17:46 +02003843static int stats_process_http_post(struct stconn *sc)
William Lallemand74c24fb2016-11-21 17:18:36 +01003844{
Willy Tarreaucaff6312022-05-27 10:17:46 +02003845 struct stream *s = __sc_strm(sc);
3846 struct appctx *appctx = __sc_appctx(sc);
Willy Tarreau91cefca2022-05-03 17:08:29 +02003847 struct show_stat_ctx *ctx = appctx->svcctx;
William Lallemand74c24fb2016-11-21 17:18:36 +01003848
3849 struct proxy *px = NULL;
3850 struct server *sv = NULL;
3851
3852 char key[LINESIZE];
3853 int action = ST_ADM_ACTION_NONE;
3854 int reprocess = 0;
3855
3856 int total_servers = 0;
3857 int altered_servers = 0;
3858
3859 char *first_param, *cur_param, *next_param, *end_params;
3860 char *st_cur_param = NULL;
3861 char *st_next_param = NULL;
3862
Christopher Fauleta3618372018-12-13 21:59:56 +01003863 struct buffer *temp = get_trash_chunk();
William Lallemand74c24fb2016-11-21 17:18:36 +01003864
Christopher Fauletb7f88902019-07-15 21:56:43 +02003865 struct htx *htx = htxbuf(&s->req.buf);
3866 struct htx_blk *blk;
Christopher Fauleta3618372018-12-13 21:59:56 +01003867
Christopher Fauletb7f88902019-07-15 21:56:43 +02003868 /* we need more data */
3869 if (s->txn->req.msg_state < HTTP_MSG_DONE) {
3870 /* check if we can receive more */
3871 if (htx_free_data_space(htx) <= global.tune.maxrewrite) {
Willy Tarreau91cefca2022-05-03 17:08:29 +02003872 ctx->st_code = STAT_STATUS_EXCD;
Christopher Fauletb7f88902019-07-15 21:56:43 +02003873 goto out;
Christopher Fauleta3618372018-12-13 21:59:56 +01003874 }
Christopher Fauletb7f88902019-07-15 21:56:43 +02003875 goto wait;
3876 }
Christopher Fauleta3618372018-12-13 21:59:56 +01003877
Christopher Fauletb7f88902019-07-15 21:56:43 +02003878 /* The request was fully received. Copy data */
3879 blk = htx_get_head_blk(htx);
3880 while (blk) {
3881 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Fauleta3618372018-12-13 21:59:56 +01003882
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01003883 if (type == HTX_BLK_TLR || type == HTX_BLK_EOT)
Christopher Fauletb7f88902019-07-15 21:56:43 +02003884 break;
3885 if (type == HTX_BLK_DATA) {
3886 struct ist v = htx_get_blk_value(htx, blk);
Christopher Fauleta3618372018-12-13 21:59:56 +01003887
Christopher Fauletb7f88902019-07-15 21:56:43 +02003888 if (!chunk_memcat(temp, v.ptr, v.len)) {
Willy Tarreau91cefca2022-05-03 17:08:29 +02003889 ctx->st_code = STAT_STATUS_EXCD;
Christopher Faulet2f9a41d2019-02-27 15:30:57 +01003890 goto out;
3891 }
Christopher Fauleta3618372018-12-13 21:59:56 +01003892 }
Christopher Fauletb7f88902019-07-15 21:56:43 +02003893 blk = htx_get_next_blk(htx, blk);
William Lallemand74c24fb2016-11-21 17:18:36 +01003894 }
3895
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003896 first_param = temp->area;
Christopher Fauleta3618372018-12-13 21:59:56 +01003897 end_params = temp->area + temp->data;
William Lallemand74c24fb2016-11-21 17:18:36 +01003898 cur_param = next_param = end_params;
3899 *end_params = '\0';
3900
Willy Tarreau91cefca2022-05-03 17:08:29 +02003901 ctx->st_code = STAT_STATUS_NONE;
William Lallemand74c24fb2016-11-21 17:18:36 +01003902
3903 /*
3904 * Parse the parameters in reverse order to only store the last value.
3905 * From the html form, the backend and the action are at the end.
3906 */
3907 while (cur_param > first_param) {
3908 char *value;
3909 int poffset, plen;
3910
3911 cur_param--;
3912
3913 if ((*cur_param == '&') || (cur_param == first_param)) {
3914 reprocess_servers:
3915 /* Parse the key */
3916 poffset = (cur_param != first_param ? 1 : 0);
3917 plen = next_param - cur_param + (cur_param == first_param ? 1 : 0);
3918 if ((plen > 0) && (plen <= sizeof(key))) {
3919 strncpy(key, cur_param + poffset, plen);
3920 key[plen - 1] = '\0';
3921 } else {
Willy Tarreau91cefca2022-05-03 17:08:29 +02003922 ctx->st_code = STAT_STATUS_ERRP;
William Lallemand74c24fb2016-11-21 17:18:36 +01003923 goto out;
3924 }
3925
3926 /* Parse the value */
3927 value = key;
3928 while (*value != '\0' && *value != '=') {
3929 value++;
3930 }
3931 if (*value == '=') {
3932 /* Ok, a value is found, we can mark the end of the key */
3933 *value++ = '\0';
3934 }
Willy Tarreau62ba9ba2020-04-23 17:54:47 +02003935 if (url_decode(key, 1) < 0 || url_decode(value, 1) < 0)
William Lallemand74c24fb2016-11-21 17:18:36 +01003936 break;
3937
3938 /* Now we can check the key to see what to do */
3939 if (!px && (strcmp(key, "b") == 0)) {
3940 if ((px = proxy_be_by_name(value)) == NULL) {
3941 /* the backend name is unknown or ambiguous (duplicate names) */
Willy Tarreau91cefca2022-05-03 17:08:29 +02003942 ctx->st_code = STAT_STATUS_ERRP;
William Lallemand74c24fb2016-11-21 17:18:36 +01003943 goto out;
3944 }
3945 }
3946 else if (!action && (strcmp(key, "action") == 0)) {
3947 if (strcmp(value, "ready") == 0) {
3948 action = ST_ADM_ACTION_READY;
3949 }
3950 else if (strcmp(value, "drain") == 0) {
3951 action = ST_ADM_ACTION_DRAIN;
3952 }
3953 else if (strcmp(value, "maint") == 0) {
3954 action = ST_ADM_ACTION_MAINT;
3955 }
3956 else if (strcmp(value, "shutdown") == 0) {
3957 action = ST_ADM_ACTION_SHUTDOWN;
3958 }
3959 else if (strcmp(value, "dhlth") == 0) {
3960 action = ST_ADM_ACTION_DHLTH;
3961 }
3962 else if (strcmp(value, "ehlth") == 0) {
3963 action = ST_ADM_ACTION_EHLTH;
3964 }
3965 else if (strcmp(value, "hrunn") == 0) {
3966 action = ST_ADM_ACTION_HRUNN;
3967 }
3968 else if (strcmp(value, "hnolb") == 0) {
3969 action = ST_ADM_ACTION_HNOLB;
3970 }
3971 else if (strcmp(value, "hdown") == 0) {
3972 action = ST_ADM_ACTION_HDOWN;
3973 }
3974 else if (strcmp(value, "dagent") == 0) {
3975 action = ST_ADM_ACTION_DAGENT;
3976 }
3977 else if (strcmp(value, "eagent") == 0) {
3978 action = ST_ADM_ACTION_EAGENT;
3979 }
3980 else if (strcmp(value, "arunn") == 0) {
3981 action = ST_ADM_ACTION_ARUNN;
3982 }
3983 else if (strcmp(value, "adown") == 0) {
3984 action = ST_ADM_ACTION_ADOWN;
3985 }
3986 /* else these are the old supported methods */
3987 else if (strcmp(value, "disable") == 0) {
3988 action = ST_ADM_ACTION_DISABLE;
3989 }
3990 else if (strcmp(value, "enable") == 0) {
3991 action = ST_ADM_ACTION_ENABLE;
3992 }
3993 else if (strcmp(value, "stop") == 0) {
3994 action = ST_ADM_ACTION_STOP;
3995 }
3996 else if (strcmp(value, "start") == 0) {
3997 action = ST_ADM_ACTION_START;
3998 }
3999 else {
Willy Tarreau91cefca2022-05-03 17:08:29 +02004000 ctx->st_code = STAT_STATUS_ERRP;
William Lallemand74c24fb2016-11-21 17:18:36 +01004001 goto out;
4002 }
4003 }
4004 else if (strcmp(key, "s") == 0) {
4005 if (!(px && action)) {
4006 /*
4007 * Indicates that we'll need to reprocess the parameters
4008 * as soon as backend and action are known
4009 */
4010 if (!reprocess) {
4011 st_cur_param = cur_param;
4012 st_next_param = next_param;
4013 }
4014 reprocess = 1;
4015 }
4016 else if ((sv = findserver(px, value)) != NULL) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004017 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
William Lallemand74c24fb2016-11-21 17:18:36 +01004018 switch (action) {
4019 case ST_ADM_ACTION_DISABLE:
Emeric Brun52a91d32017-08-31 14:41:55 +02004020 if (!(sv->cur_admin & SRV_ADMF_FMAINT)) {
William Lallemand74c24fb2016-11-21 17:18:36 +01004021 altered_servers++;
4022 total_servers++;
4023 srv_set_admin_flag(sv, SRV_ADMF_FMAINT, "'disable' on stats page");
4024 }
4025 break;
4026 case ST_ADM_ACTION_ENABLE:
Emeric Brun52a91d32017-08-31 14:41:55 +02004027 if (sv->cur_admin & SRV_ADMF_FMAINT) {
William Lallemand74c24fb2016-11-21 17:18:36 +01004028 altered_servers++;
4029 total_servers++;
4030 srv_clr_admin_flag(sv, SRV_ADMF_FMAINT);
4031 }
4032 break;
4033 case ST_ADM_ACTION_STOP:
Emeric Brun52a91d32017-08-31 14:41:55 +02004034 if (!(sv->cur_admin & SRV_ADMF_FDRAIN)) {
William Lallemand74c24fb2016-11-21 17:18:36 +01004035 srv_set_admin_flag(sv, SRV_ADMF_FDRAIN, "'stop' on stats page");
4036 altered_servers++;
4037 total_servers++;
4038 }
4039 break;
4040 case ST_ADM_ACTION_START:
Emeric Brun52a91d32017-08-31 14:41:55 +02004041 if (sv->cur_admin & SRV_ADMF_FDRAIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +01004042 srv_clr_admin_flag(sv, SRV_ADMF_FDRAIN);
4043 altered_servers++;
4044 total_servers++;
4045 }
4046 break;
4047 case ST_ADM_ACTION_DHLTH:
4048 if (sv->check.state & CHK_ST_CONFIGURED) {
4049 sv->check.state &= ~CHK_ST_ENABLED;
4050 altered_servers++;
4051 total_servers++;
4052 }
4053 break;
4054 case ST_ADM_ACTION_EHLTH:
4055 if (sv->check.state & CHK_ST_CONFIGURED) {
4056 sv->check.state |= CHK_ST_ENABLED;
4057 altered_servers++;
4058 total_servers++;
4059 }
4060 break;
4061 case ST_ADM_ACTION_HRUNN:
4062 if (!(sv->track)) {
4063 sv->check.health = sv->check.rise + sv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02004064 srv_set_running(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01004065 altered_servers++;
4066 total_servers++;
4067 }
4068 break;
4069 case ST_ADM_ACTION_HNOLB:
4070 if (!(sv->track)) {
4071 sv->check.health = sv->check.rise + sv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02004072 srv_set_stopping(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01004073 altered_servers++;
4074 total_servers++;
4075 }
4076 break;
4077 case ST_ADM_ACTION_HDOWN:
4078 if (!(sv->track)) {
4079 sv->check.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02004080 srv_set_stopped(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01004081 altered_servers++;
4082 total_servers++;
4083 }
4084 break;
4085 case ST_ADM_ACTION_DAGENT:
4086 if (sv->agent.state & CHK_ST_CONFIGURED) {
4087 sv->agent.state &= ~CHK_ST_ENABLED;
4088 altered_servers++;
4089 total_servers++;
4090 }
4091 break;
4092 case ST_ADM_ACTION_EAGENT:
4093 if (sv->agent.state & CHK_ST_CONFIGURED) {
4094 sv->agent.state |= CHK_ST_ENABLED;
4095 altered_servers++;
4096 total_servers++;
4097 }
4098 break;
4099 case ST_ADM_ACTION_ARUNN:
4100 if (sv->agent.state & CHK_ST_ENABLED) {
4101 sv->agent.health = sv->agent.rise + sv->agent.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02004102 srv_set_running(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01004103 altered_servers++;
4104 total_servers++;
4105 }
4106 break;
4107 case ST_ADM_ACTION_ADOWN:
4108 if (sv->agent.state & CHK_ST_ENABLED) {
4109 sv->agent.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02004110 srv_set_stopped(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01004111 altered_servers++;
4112 total_servers++;
4113 }
4114 break;
4115 case ST_ADM_ACTION_READY:
4116 srv_adm_set_ready(sv);
4117 altered_servers++;
4118 total_servers++;
4119 break;
4120 case ST_ADM_ACTION_DRAIN:
4121 srv_adm_set_drain(sv);
4122 altered_servers++;
4123 total_servers++;
4124 break;
4125 case ST_ADM_ACTION_MAINT:
4126 srv_adm_set_maint(sv);
4127 altered_servers++;
4128 total_servers++;
4129 break;
4130 case ST_ADM_ACTION_SHUTDOWN:
Christopher Fauletdfd10ab2021-10-06 14:24:19 +02004131 if (!(px->flags & (PR_FL_DISABLED|PR_FL_STOPPED))) {
Willy Tarreauaf7ea812019-11-14 16:42:04 +01004132 srv_shutdown_streams(sv, SF_ERR_KILLED);
William Lallemand74c24fb2016-11-21 17:18:36 +01004133 altered_servers++;
4134 total_servers++;
4135 }
4136 break;
4137 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004138 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
William Lallemand74c24fb2016-11-21 17:18:36 +01004139 } else {
4140 /* the server name is unknown or ambiguous (duplicate names) */
4141 total_servers++;
4142 }
4143 }
4144 if (reprocess && px && action) {
4145 /* Now, we know the backend and the action chosen by the user.
4146 * We can safely restart from the first server parameter
4147 * to reprocess them
4148 */
4149 cur_param = st_cur_param;
4150 next_param = st_next_param;
4151 reprocess = 0;
4152 goto reprocess_servers;
4153 }
4154
4155 next_param = cur_param;
4156 }
4157 }
4158
4159 if (total_servers == 0) {
Willy Tarreau91cefca2022-05-03 17:08:29 +02004160 ctx->st_code = STAT_STATUS_NONE;
William Lallemand74c24fb2016-11-21 17:18:36 +01004161 }
4162 else if (altered_servers == 0) {
Willy Tarreau91cefca2022-05-03 17:08:29 +02004163 ctx->st_code = STAT_STATUS_ERRP;
William Lallemand74c24fb2016-11-21 17:18:36 +01004164 }
4165 else if (altered_servers == total_servers) {
Willy Tarreau91cefca2022-05-03 17:08:29 +02004166 ctx->st_code = STAT_STATUS_DONE;
William Lallemand74c24fb2016-11-21 17:18:36 +01004167 }
4168 else {
Willy Tarreau91cefca2022-05-03 17:08:29 +02004169 ctx->st_code = STAT_STATUS_PART;
William Lallemand74c24fb2016-11-21 17:18:36 +01004170 }
4171 out:
4172 return 1;
Christopher Faulet2f9a41d2019-02-27 15:30:57 +01004173 wait:
Willy Tarreau91cefca2022-05-03 17:08:29 +02004174 ctx->st_code = STAT_STATUS_NONE;
Christopher Faulet2f9a41d2019-02-27 15:30:57 +01004175 return 0;
Christopher Fauletef779222018-10-31 08:47:01 +01004176}
4177
4178
Willy Tarreaucaff6312022-05-27 10:17:46 +02004179static int stats_send_http_headers(struct stconn *sc, struct htx *htx)
Christopher Fauletef779222018-10-31 08:47:01 +01004180{
Willy Tarreaucaff6312022-05-27 10:17:46 +02004181 struct stream *s = __sc_strm(sc);
Christopher Fauletef779222018-10-31 08:47:01 +01004182 struct uri_auth *uri = s->be->uri_auth;
Willy Tarreaucaff6312022-05-27 10:17:46 +02004183 struct appctx *appctx = __sc_appctx(sc);
Willy Tarreau91cefca2022-05-03 17:08:29 +02004184 struct show_stat_ctx *ctx = appctx->svcctx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004185 struct htx_sl *sl;
4186 unsigned int flags;
Christopher Fauletef779222018-10-31 08:47:01 +01004187
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004188 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);
4189 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), ist("200"), ist("OK"));
4190 if (!sl)
Christopher Fauletef779222018-10-31 08:47:01 +01004191 goto full;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004192 sl->info.res.status = 200;
Christopher Fauletef779222018-10-31 08:47:01 +01004193
Christopher Fauletb829f4c2019-03-29 16:13:55 +01004194 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")))
Christopher Fauletef779222018-10-31 08:47:01 +01004195 goto full;
Willy Tarreau91cefca2022-05-03 17:08:29 +02004196 if (ctx->flags & STAT_FMT_HTML) {
Christopher Fauletef779222018-10-31 08:47:01 +01004197 if (!htx_add_header(htx, ist("Content-Type"), ist("text/html")))
4198 goto full;
4199 }
Willy Tarreau91cefca2022-05-03 17:08:29 +02004200 else if (ctx->flags & (STAT_FMT_JSON|STAT_JSON_SCHM)) {
Christopher Faulet6338a082019-09-09 15:50:54 +02004201 if (!htx_add_header(htx, ist("Content-Type"), ist("application/json")))
4202 goto full;
4203 }
Christopher Fauletef779222018-10-31 08:47:01 +01004204 else {
4205 if (!htx_add_header(htx, ist("Content-Type"), ist("text/plain")))
4206 goto full;
4207 }
4208
Willy Tarreau91cefca2022-05-03 17:08:29 +02004209 if (uri->refresh > 0 && !(ctx->flags & STAT_NO_REFRESH)) {
Christopher Fauletef779222018-10-31 08:47:01 +01004210 const char *refresh = U2A(uri->refresh);
Tim Duesterhusdcf753a2021-03-04 17:31:47 +01004211 if (!htx_add_header(htx, ist("Refresh"), ist(refresh)))
Christopher Fauletef779222018-10-31 08:47:01 +01004212 goto full;
4213 }
4214
Willy Tarreau91cefca2022-05-03 17:08:29 +02004215 if (ctx->flags & STAT_CHUNKED) {
Christopher Fauletef779222018-10-31 08:47:01 +01004216 if (!htx_add_header(htx, ist("Transfer-Encoding"), ist("chunked")))
4217 goto full;
4218 }
4219
4220 if (!htx_add_endof(htx, HTX_BLK_EOH))
4221 goto full;
4222
Christopher Faulet1e2d6362019-02-27 16:28:48 +01004223 channel_add_input(&s->res, htx->data);
Christopher Fauletef779222018-10-31 08:47:01 +01004224 return 1;
4225
4226 full:
4227 htx_reset(htx);
Willy Tarreaucaff6312022-05-27 10:17:46 +02004228 sc_need_room(sc);
Christopher Fauletef779222018-10-31 08:47:01 +01004229 return 0;
William Lallemand74c24fb2016-11-21 17:18:36 +01004230}
4231
Christopher Fauletef779222018-10-31 08:47:01 +01004232
Willy Tarreaucaff6312022-05-27 10:17:46 +02004233static int stats_send_http_redirect(struct stconn *sc, struct htx *htx)
Christopher Fauletef779222018-10-31 08:47:01 +01004234{
4235 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
Willy Tarreaucaff6312022-05-27 10:17:46 +02004236 struct stream *s = __sc_strm(sc);
Christopher Fauletef779222018-10-31 08:47:01 +01004237 struct uri_auth *uri = s->be->uri_auth;
Willy Tarreaucaff6312022-05-27 10:17:46 +02004238 struct appctx *appctx = __sc_appctx(sc);
Willy Tarreau91cefca2022-05-03 17:08:29 +02004239 struct show_stat_ctx *ctx = appctx->svcctx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004240 struct htx_sl *sl;
4241 unsigned int flags;
Christopher Fauletef779222018-10-31 08:47:01 +01004242
Willy Tarreau91cefca2022-05-03 17:08:29 +02004243 /* scope_txt = search pattern + search query, ctx->scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
Christopher Fauletef779222018-10-31 08:47:01 +01004244 scope_txt[0] = 0;
Willy Tarreau91cefca2022-05-03 17:08:29 +02004245 if (ctx->scope_len) {
Willy Tarreaucaff6312022-05-27 10:17:46 +02004246 const char *scope_ptr = stats_scope_ptr(appctx, sc);
Christopher Fauleted7a0662019-01-14 11:07:34 +01004247
Christopher Fauletef779222018-10-31 08:47:01 +01004248 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Willy Tarreau91cefca2022-05-03 17:08:29 +02004249 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), scope_ptr, ctx->scope_len);
4250 scope_txt[strlen(STAT_SCOPE_PATTERN) + ctx->scope_len] = 0;
Christopher Fauletef779222018-10-31 08:47:01 +01004251 }
4252
4253 /* We don't want to land on the posted stats page because a refresh will
4254 * repost the data. We don't want this to happen on accident so we redirect
4255 * the browse to the stats page with a GET.
4256 */
4257 chunk_printf(&trash, "%s;st=%s%s%s%s",
4258 uri->uri_prefix,
Willy Tarreau91cefca2022-05-03 17:08:29 +02004259 ((ctx->st_code > STAT_STATUS_INIT) &&
4260 (ctx->st_code < STAT_STATUS_SIZE) &&
4261 stat_status_codes[ctx->st_code]) ?
4262 stat_status_codes[ctx->st_code] :
Christopher Fauletef779222018-10-31 08:47:01 +01004263 stat_status_codes[STAT_STATUS_UNKN],
Willy Tarreau91cefca2022-05-03 17:08:29 +02004264 (ctx->flags & STAT_HIDE_DOWN) ? ";up" : "",
4265 (ctx->flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Christopher Fauletef779222018-10-31 08:47:01 +01004266 scope_txt);
4267
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004268 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_CHNK);
4269 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), ist("303"), ist("See Other"));
4270 if (!sl)
Christopher Fauletef779222018-10-31 08:47:01 +01004271 goto full;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004272 sl->info.res.status = 303;
Christopher Fauletef779222018-10-31 08:47:01 +01004273
4274 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")) ||
Christopher Fauletef779222018-10-31 08:47:01 +01004275 !htx_add_header(htx, ist("Content-Type"), ist("text/plain")) ||
4276 !htx_add_header(htx, ist("Content-Length"), ist("0")) ||
4277 !htx_add_header(htx, ist("Location"), ist2(trash.area, trash.data)))
4278 goto full;
4279
4280 if (!htx_add_endof(htx, HTX_BLK_EOH))
4281 goto full;
4282
Christopher Faulet1e2d6362019-02-27 16:28:48 +01004283 channel_add_input(&s->res, htx->data);
Christopher Fauletef779222018-10-31 08:47:01 +01004284 return 1;
4285
4286full:
4287 htx_reset(htx);
Willy Tarreaucaff6312022-05-27 10:17:46 +02004288 sc_need_room(sc);
Christopher Fauletef779222018-10-31 08:47:01 +01004289 return 0;
4290}
William Lallemand74c24fb2016-11-21 17:18:36 +01004291
Simon Horman05ee2132017-01-04 09:37:25 +01004292
Willy Tarreau4596fe22022-05-17 19:07:51 +02004293/* This I/O handler runs as an applet embedded in a stream connector. It is
William Lallemand74c24fb2016-11-21 17:18:36 +01004294 * used to send HTTP stats over a TCP socket. The mechanism is very simple.
4295 * appctx->st0 contains the operation in progress (dump, done). The handler
4296 * automatically unregisters itself once transfer is complete.
4297 */
Christopher Fauletb7f88902019-07-15 21:56:43 +02004298static void http_stats_io_handler(struct appctx *appctx)
Christopher Fauletef779222018-10-31 08:47:01 +01004299{
Willy Tarreau91cefca2022-05-03 17:08:29 +02004300 struct show_stat_ctx *ctx = appctx->svcctx;
Willy Tarreauc12b3212022-05-27 11:08:15 +02004301 struct stconn *sc = appctx_sc(appctx);
Willy Tarreaucaff6312022-05-27 10:17:46 +02004302 struct stream *s = __sc_strm(sc);
4303 struct channel *req = sc_oc(sc);
4304 struct channel *res = sc_ic(sc);
Christopher Fauletef779222018-10-31 08:47:01 +01004305 struct htx *req_htx, *res_htx;
4306
Amaury Denoyelle072f97e2020-10-05 11:49:37 +02004307 /* only proxy stats are available via http */
Willy Tarreau91cefca2022-05-03 17:08:29 +02004308 ctx->domain = STATS_DOMAIN_PROXY;
Amaury Denoyelle072f97e2020-10-05 11:49:37 +02004309
Christopher Fauletef779222018-10-31 08:47:01 +01004310 res_htx = htx_from_buf(&res->buf);
4311
Willy Tarreaucaff6312022-05-27 10:17:46 +02004312 if (unlikely(sc->state == SC_ST_DIS || sc->state == SC_ST_CLO))
Christopher Fauletef779222018-10-31 08:47:01 +01004313 goto out;
4314
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05004315 /* Check if the input buffer is available. */
Christopher Fauletef779222018-10-31 08:47:01 +01004316 if (!b_size(&res->buf)) {
Willy Tarreaucaff6312022-05-27 10:17:46 +02004317 sc_need_room(sc);
Christopher Fauletef779222018-10-31 08:47:01 +01004318 goto out;
4319 }
4320
4321 /* check that the output is not closed */
Christopher Faulet3a78aa62019-02-27 16:19:48 +01004322 if (res->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_SHUTR))
4323 appctx->st0 = STAT_HTTP_END;
Christopher Fauletef779222018-10-31 08:47:01 +01004324
4325 /* all states are processed in sequence */
4326 if (appctx->st0 == STAT_HTTP_HEAD) {
Willy Tarreaucaff6312022-05-27 10:17:46 +02004327 if (stats_send_http_headers(sc, res_htx)) {
Christopher Fauletef779222018-10-31 08:47:01 +01004328 if (s->txn->meth == HTTP_METH_HEAD)
4329 appctx->st0 = STAT_HTTP_DONE;
4330 else
4331 appctx->st0 = STAT_HTTP_DUMP;
4332 }
4333 }
4334
4335 if (appctx->st0 == STAT_HTTP_DUMP) {
Willy Tarreaucaff6312022-05-27 10:17:46 +02004336 if (stats_dump_stat_to_buffer(sc, res_htx, s->be->uri_auth))
Christopher Fauletef779222018-10-31 08:47:01 +01004337 appctx->st0 = STAT_HTTP_DONE;
4338 }
4339
4340 if (appctx->st0 == STAT_HTTP_POST) {
Willy Tarreaucaff6312022-05-27 10:17:46 +02004341 if (stats_process_http_post(sc))
Christopher Fauletef779222018-10-31 08:47:01 +01004342 appctx->st0 = STAT_HTTP_LAST;
Christopher Faulet3a78aa62019-02-27 16:19:48 +01004343 else if (req->flags & CF_SHUTR)
Christopher Fauletef779222018-10-31 08:47:01 +01004344 appctx->st0 = STAT_HTTP_DONE;
4345 }
4346
4347 if (appctx->st0 == STAT_HTTP_LAST) {
Willy Tarreaucaff6312022-05-27 10:17:46 +02004348 if (stats_send_http_redirect(sc, res_htx))
Christopher Fauletef779222018-10-31 08:47:01 +01004349 appctx->st0 = STAT_HTTP_DONE;
4350 }
4351
4352 if (appctx->st0 == STAT_HTTP_DONE) {
Christopher Faulet08b45cb2022-04-07 09:25:13 +02004353 /* no more data are expected. If the response buffer is empty,
4354 * be sure to add something (EOT block in this case) to have
4355 * something to send. It is important to be sure the EOM flags
4356 * will be handled by the endpoint.
4357 */
4358 if (htx_is_empty(res_htx)) {
4359 if (!htx_add_endof(res_htx, HTX_BLK_EOT)) {
Willy Tarreaucaff6312022-05-27 10:17:46 +02004360 sc_need_room(sc);
Christopher Faulet08b45cb2022-04-07 09:25:13 +02004361 goto out;
4362 }
4363 channel_add_input(res, 1);
4364 }
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01004365 res_htx->flags |= HTX_FL_EOM;
Christopher Faulet3fa5d192022-03-07 15:52:42 +01004366 res->flags |= CF_EOI;
Willy Tarreaud869e132022-05-17 18:05:31 +02004367 se_fl_set(appctx->sedesc, SE_FL_EOI);
Christopher Faulet3a78aa62019-02-27 16:19:48 +01004368 appctx->st0 = STAT_HTTP_END;
Christopher Fauletef779222018-10-31 08:47:01 +01004369 }
4370
Christopher Faulet3a78aa62019-02-27 16:19:48 +01004371 if (appctx->st0 == STAT_HTTP_END) {
4372 if (!(res->flags & CF_SHUTR)) {
Christopher Fauletef779222018-10-31 08:47:01 +01004373 res->flags |= CF_READ_NULL;
Willy Tarreaucaff6312022-05-27 10:17:46 +02004374 sc_shutr(sc);
Christopher Faulet3a78aa62019-02-27 16:19:48 +01004375 }
4376
4377 /* eat the whole request */
4378 if (co_data(req)) {
4379 req_htx = htx_from_buf(&req->buf);
4380 co_htx_skip(req, req_htx, co_data(req));
4381 htx_to_buf(req_htx, &req->buf);
Christopher Fauletef779222018-10-31 08:47:01 +01004382 }
4383 }
Christopher Faulet3a78aa62019-02-27 16:19:48 +01004384
Christopher Fauletef779222018-10-31 08:47:01 +01004385 out:
4386 /* we have left the request in the buffer for the case where we
4387 * process a POST, and this automatically re-enables activity on
4388 * read. It's better to indicate that we want to stop reading when
4389 * we're sending, so that we know there's at most one direction
4390 * deciding to wake the applet up. It saves it from looping when
4391 * emitting large blocks into small TCP windows.
4392 */
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01004393 htx_to_buf(res_htx, &res->buf);
4394 if (!channel_is_empty(res))
Willy Tarreau75a8f8e2022-05-25 18:12:11 +02004395 applet_wont_consume(appctx);
Christopher Fauletef779222018-10-31 08:47:01 +01004396}
4397
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004398/* Dump all fields from <info> into <out> using the "show info" format (name: value) */
Willy Tarreau83061a82018-07-13 11:56:34 +02004399static int stats_dump_info_fields(struct buffer *out,
Willy Tarreau43241ff2019-10-09 11:27:51 +02004400 const struct field *info, unsigned int flags)
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004401{
4402 int field;
4403
4404 for (field = 0; field < INF_TOTAL_FIELDS; field++) {
4405 if (!field_format(info, field))
4406 continue;
4407
Willy Tarreaueaa55372019-10-09 07:39:11 +02004408 if (!chunk_appendf(out, "%s: ", info_fields[field].name))
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004409 return 0;
4410 if (!stats_emit_raw_data_field(out, &info[field]))
4411 return 0;
Willy Tarreau6b19b142019-10-09 15:44:21 +02004412 if ((flags & STAT_SHOW_FDESC) && !chunk_appendf(out, ":\"%s\"", info_fields[field].desc))
4413 return 0;
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004414 if (!chunk_strcat(out, "\n"))
4415 return 0;
4416 }
4417 return 1;
4418}
4419
4420/* Dump all fields from <info> into <out> using the "show info typed" format */
Willy Tarreau83061a82018-07-13 11:56:34 +02004421static int stats_dump_typed_info_fields(struct buffer *out,
Willy Tarreau43241ff2019-10-09 11:27:51 +02004422 const struct field *info, unsigned int flags)
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004423{
4424 int field;
4425
4426 for (field = 0; field < INF_TOTAL_FIELDS; field++) {
4427 if (!field_format(info, field))
4428 continue;
4429
Willy Tarreaueaa55372019-10-09 07:39:11 +02004430 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 +01004431 return 0;
4432 if (!stats_emit_field_tags(out, &info[field], ':'))
4433 return 0;
4434 if (!stats_emit_typed_data_field(out, &info[field]))
4435 return 0;
Willy Tarreau6b19b142019-10-09 15:44:21 +02004436 if ((flags & STAT_SHOW_FDESC) && !chunk_appendf(out, ":\"%s\"", info_fields[field].desc))
4437 return 0;
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004438 if (!chunk_strcat(out, "\n"))
4439 return 0;
4440 }
4441 return 1;
4442}
4443
Willy Tarreau0b26b382021-05-08 07:43:53 +02004444/* Fill <info> with HAProxy global info. <info> is preallocated array of length
4445 * <len>. The length of the array must be INF_TOTAL_FIELDS. If this length is
4446 * less then this value, the function returns 0, otherwise, it returns 1. Some
4447 * fields' presence or precision may depend on some of the STAT_* flags present
4448 * in <flags>.
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004449 */
Willy Tarreau0b26b382021-05-08 07:43:53 +02004450int stats_fill_info(struct field *info, int len, uint flags)
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004451{
Willy Tarreaud37e26e2021-05-08 07:40:52 +02004452 struct timeval up;
Willy Tarreau83061a82018-07-13 11:56:34 +02004453 struct buffer *out = get_trash_chunk();
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004454
4455#ifdef USE_OPENSSL
Willy Tarreauc5977722021-05-08 08:14:04 +02004456 double ssl_sess_rate = read_freq_ctr_flt(&global.ssl_per_sec);
4457 double ssl_key_rate = read_freq_ctr_flt(&global.ssl_fe_keys_per_sec);
4458 double ssl_reuse = 0;
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004459
Willy Tarreauc5977722021-05-08 08:14:04 +02004460 if (ssl_key_rate < ssl_sess_rate)
4461 ssl_reuse = 100.0 * (1.0 - ssl_key_rate / ssl_sess_rate);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004462#endif
4463
Willy Tarreaud37e26e2021-05-08 07:40:52 +02004464 tv_remain(&start_date, &now, &up);
4465
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004466 if (len < INF_TOTAL_FIELDS)
4467 return 0;
4468
4469 chunk_reset(out);
4470 memset(info, 0, sizeof(*info) * len);
4471
4472 info[INF_NAME] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, PRODUCT_NAME);
Willy Tarreau909b9d82019-01-04 18:20:32 +01004473 info[INF_VERSION] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, haproxy_version);
Adis Nezirovicb62b78b2021-01-15 13:12:33 +01004474 info[INF_BUILD_INFO] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, haproxy_version);
Willy Tarreau909b9d82019-01-04 18:20:32 +01004475 info[INF_RELEASE_DATE] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, haproxy_date);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004476
Yves Lafon95317282018-02-26 11:10:37 +01004477 info[INF_NBTHREAD] = mkf_u32(FO_CONFIG|FS_SERVICE, global.nbthread);
Willy Tarreau91358592021-06-15 08:08:04 +02004478 info[INF_NBPROC] = mkf_u32(FO_CONFIG|FS_SERVICE, 1);
Willy Tarreaue8422bf2021-06-15 09:08:18 +02004479 info[INF_PROCESS_NUM] = mkf_u32(FO_KEY, 1);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004480 info[INF_PID] = mkf_u32(FO_STATUS, pid);
4481
4482 info[INF_UPTIME] = mkf_str(FN_DURATION, chunk_newstr(out));
Willy Tarreaud37e26e2021-05-08 07:40:52 +02004483 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 +01004484
Willy Tarreaue8abc322021-05-08 07:56:56 +02004485 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);
4486 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 +01004487 info[INF_MEMMAX_MB] = mkf_u32(FO_CONFIG|FN_LIMIT, global.rlimit_memmax);
William Dauchya8766cf2021-01-15 22:41:37 +01004488 info[INF_MEMMAX_BYTES] = mkf_u32(FO_CONFIG|FN_LIMIT, global.rlimit_memmax * 1048576L);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004489 info[INF_POOL_ALLOC_MB] = mkf_u32(0, (unsigned)(pool_total_allocated() / 1048576L));
William Dauchya8766cf2021-01-15 22:41:37 +01004490 info[INF_POOL_ALLOC_BYTES] = mkf_u32(0, pool_total_allocated());
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004491 info[INF_POOL_USED_MB] = mkf_u32(0, (unsigned)(pool_total_used() / 1048576L));
William Dauchya8766cf2021-01-15 22:41:37 +01004492 info[INF_POOL_USED_BYTES] = mkf_u32(0, pool_total_used());
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004493 info[INF_POOL_FAILED] = mkf_u32(FN_COUNTER, pool_total_failures());
4494 info[INF_ULIMIT_N] = mkf_u32(FO_CONFIG|FN_LIMIT, global.rlimit_nofile);
4495 info[INF_MAXSOCK] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxsock);
4496 info[INF_MAXCONN] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxconn);
4497 info[INF_HARD_MAXCONN] = mkf_u32(FO_CONFIG|FN_LIMIT, global.hardmaxconn);
4498 info[INF_CURR_CONN] = mkf_u32(0, actconn);
4499 info[INF_CUM_CONN] = mkf_u32(FN_COUNTER, totalconn);
4500 info[INF_CUM_REQ] = mkf_u32(FN_COUNTER, global.req_count);
4501#ifdef USE_OPENSSL
4502 info[INF_MAX_SSL_CONNS] = mkf_u32(FN_MAX, global.maxsslconn);
Willy Tarreau82531f62021-10-06 12:15:18 +02004503 info[INF_CURR_SSL_CONNS] = mkf_u32(0, global.sslconns);
4504 info[INF_CUM_SSL_CONNS] = mkf_u32(FN_COUNTER, global.totalsslconns);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004505#endif
4506 info[INF_MAXPIPES] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxpipes);
4507 info[INF_PIPES_USED] = mkf_u32(0, pipes_used);
4508 info[INF_PIPES_FREE] = mkf_u32(0, pipes_free);
Willy Tarreauc5977722021-05-08 08:14:04 +02004509 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 +01004510 info[INF_CONN_RATE_LIMIT] = mkf_u32(FO_CONFIG|FN_LIMIT, global.cps_lim);
4511 info[INF_MAX_CONN_RATE] = mkf_u32(FN_MAX, global.cps_max);
Willy Tarreauc5977722021-05-08 08:14:04 +02004512 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 +01004513 info[INF_SESS_RATE_LIMIT] = mkf_u32(FO_CONFIG|FN_LIMIT, global.sps_lim);
4514 info[INF_MAX_SESS_RATE] = mkf_u32(FN_RATE, global.sps_max);
4515
4516#ifdef USE_OPENSSL
Willy Tarreauc5977722021-05-08 08:14:04 +02004517 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 +01004518 info[INF_SSL_RATE_LIMIT] = mkf_u32(FO_CONFIG|FN_LIMIT, global.ssl_lim);
4519 info[INF_MAX_SSL_RATE] = mkf_u32(FN_MAX, global.ssl_max);
Willy Tarreauc5977722021-05-08 08:14:04 +02004520 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 +01004521 info[INF_SSL_FRONTEND_MAX_KEY_RATE] = mkf_u32(FN_MAX, global.ssl_fe_keys_max);
Willy Tarreauc5977722021-05-08 08:14:04 +02004522 info[INF_SSL_FRONTEND_SESSION_REUSE_PCT] = (flags & STAT_USE_FLOAT) ? mkf_flt(FN_RATE, ssl_reuse) : mkf_u32(0, ssl_reuse);
4523 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 +01004524 info[INF_SSL_BACKEND_MAX_KEY_RATE] = mkf_u32(FN_MAX, global.ssl_be_keys_max);
4525 info[INF_SSL_CACHE_LOOKUPS] = mkf_u32(FN_COUNTER, global.shctx_lookups);
4526 info[INF_SSL_CACHE_MISSES] = mkf_u32(FN_COUNTER, global.shctx_misses);
4527#endif
Willy Tarreauc5977722021-05-08 08:14:04 +02004528 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));
4529 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 +01004530 info[INF_COMPRESS_BPS_RATE_LIM] = mkf_u32(FO_CONFIG|FN_LIMIT, global.comp_rate_lim);
4531#ifdef USE_ZLIB
4532 info[INF_ZLIB_MEM_USAGE] = mkf_u32(0, zlib_used_memory);
4533 info[INF_MAX_ZLIB_MEM_USAGE] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxzlibmem);
4534#endif
Willy Tarreau955a11e2021-02-24 17:03:30 +01004535 info[INF_TASKS] = mkf_u32(0, total_allocated_tasks());
Willy Tarreau9c7b8082021-02-24 15:10:07 +01004536 info[INF_RUN_QUEUE] = mkf_u32(0, total_run_queues());
Willy Tarreauf9d5e102021-10-08 10:43:59 +02004537 info[INF_IDLE_PCT] = mkf_u32(FN_AVG, clock_report_idle());
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004538 info[INF_NODE] = mkf_str(FO_CONFIG|FN_OUTPUT|FS_SERVICE, global.node);
4539 if (global.desc)
4540 info[INF_DESCRIPTION] = mkf_str(FO_CONFIG|FN_OUTPUT|FS_SERVICE, global.desc);
Willy Tarreau00098ea2018-11-05 14:38:13 +01004541 info[INF_STOPPING] = mkf_u32(0, stopping);
4542 info[INF_JOBS] = mkf_u32(0, jobs);
William Lallemanda7199262018-11-16 16:57:20 +01004543 info[INF_UNSTOPPABLE_JOBS] = mkf_u32(0, unstoppable_jobs);
Willy Tarreau00098ea2018-11-05 14:38:13 +01004544 info[INF_LISTENERS] = mkf_u32(0, listeners);
Willy Tarreau199ad242018-11-05 16:31:22 +01004545 info[INF_ACTIVE_PEERS] = mkf_u32(0, active_peers);
Willy Tarreau2d372c22018-11-05 17:12:27 +01004546 info[INF_CONNECTED_PEERS] = mkf_u32(0, connected_peers);
Willy Tarreau13ef7732018-11-12 07:25:28 +01004547 info[INF_DROPPED_LOGS] = mkf_u32(0, dropped_logs);
Willy Tarreaubeb859a2018-11-22 18:07:59 +01004548 info[INF_BUSY_POLLING] = mkf_u32(0, !!(global.tune.options & GTUNE_BUSY_POLLING));
Emeric Brund30e9a12020-12-23 18:49:16 +01004549 info[INF_FAILED_RESOLUTIONS] = mkf_u32(0, resolv_failed_resolutions);
Willy Tarreau7cf0e452019-05-23 11:39:14 +02004550 info[INF_TOTAL_BYTES_OUT] = mkf_u64(0, global.out_bytes);
Christopher Fauletaaa70852020-07-10 13:56:30 +02004551 info[INF_TOTAL_SPLICED_BYTES_OUT] = mkf_u64(0, global.spliced_out_bytes);
Willy Tarreau7cf0e452019-05-23 11:39:14 +02004552 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 +02004553 info[INF_DEBUG_COMMANDS_ISSUED] = mkf_u32(0, debug_commands_issued);
Emeric Brun45c457a2020-07-09 23:23:34 +02004554 info[INF_CUM_LOG_MSGS] = mkf_u32(FN_COUNTER, cum_log_messages);
Amaury Denoyelle5dfdf3e2021-05-05 17:09:12 +02004555
4556 info[INF_TAINTED] = mkf_str(FO_STATUS, chunk_newstr(out));
4557 chunk_appendf(out, "%#x", get_tainted());
4558
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004559 return 1;
4560}
4561
Willy Tarreau4596fe22022-05-17 19:07:51 +02004562/* This function dumps information onto the stream connector's read buffer.
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004563 * It returns 0 as long as it does not complete, non-zero upon completion.
4564 * No state is used.
4565 */
Willy Tarreaucaff6312022-05-27 10:17:46 +02004566static int stats_dump_info_to_buffer(struct stconn *sc)
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004567{
Willy Tarreaucaff6312022-05-27 10:17:46 +02004568 struct appctx *appctx = __sc_appctx(sc);
Willy Tarreau91cefca2022-05-03 17:08:29 +02004569 struct show_stat_ctx *ctx = appctx->svcctx;
Christopher Faulet2da02ae2022-02-24 13:45:27 +01004570
Willy Tarreau91cefca2022-05-03 17:08:29 +02004571 if (!stats_fill_info(info, INF_TOTAL_FIELDS, ctx->flags))
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004572 return 0;
4573
4574 chunk_reset(&trash);
4575
Willy Tarreau91cefca2022-05-03 17:08:29 +02004576 if (ctx->flags & STAT_FMT_TYPED)
4577 stats_dump_typed_info_fields(&trash, info, ctx->flags);
4578 else if (ctx->flags & STAT_FMT_JSON)
4579 stats_dump_json_info_fields(&trash, info, ctx->flags);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004580 else
Willy Tarreau91cefca2022-05-03 17:08:29 +02004581 stats_dump_info_fields(&trash, info, ctx->flags);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004582
Willy Tarreaud0a06d52022-05-18 15:07:19 +02004583 if (applet_putchk(appctx, &trash) == -1)
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004584 return 0;
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004585
4586 return 1;
4587}
4588
Willy Tarreau4596fe22022-05-17 19:07:51 +02004589/* This function dumps the schema onto the stream connector's read buffer.
Simon Horman6f6bb382017-01-04 09:37:26 +01004590 * It returns 0 as long as it does not complete, non-zero upon completion.
4591 * No state is used.
4592 *
Ilya Shipitsinf38a0182020-12-21 01:16:17 +05004593 * Integer values bounded to the range [-(2**53)+1, (2**53)-1] as
Simon Horman6f6bb382017-01-04 09:37:26 +01004594 * per the recommendation for interoperable integers in section 6 of RFC 7159.
4595 */
Willy Tarreau83061a82018-07-13 11:56:34 +02004596static void stats_dump_json_schema(struct buffer *out)
Simon Horman6f6bb382017-01-04 09:37:26 +01004597{
4598
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004599 int old_len = out->data;
Simon Horman6f6bb382017-01-04 09:37:26 +01004600
4601 chunk_strcat(out,
4602 "{"
4603 "\"$schema\":\"http://json-schema.org/draft-04/schema#\","
4604 "\"oneOf\":["
4605 "{"
4606 "\"title\":\"Info\","
4607 "\"type\":\"array\","
4608 "\"items\":{"
Amaury Denoyellea53ce4c2020-10-02 18:31:59 +02004609 "\"title\":\"InfoItem\","
4610 "\"type\":\"object\","
Simon Horman6f6bb382017-01-04 09:37:26 +01004611 "\"properties\":{"
Simon Horman6f6bb382017-01-04 09:37:26 +01004612 "\"field\":{\"$ref\":\"#/definitions/field\"},"
4613 "\"processNum\":{\"$ref\":\"#/definitions/processNum\"},"
4614 "\"tags\":{\"$ref\":\"#/definitions/tags\"},"
4615 "\"value\":{\"$ref\":\"#/definitions/typedValue\"}"
4616 "},"
4617 "\"required\":[\"field\",\"processNum\",\"tags\","
4618 "\"value\"]"
4619 "}"
4620 "},"
4621 "{"
4622 "\"title\":\"Stat\","
4623 "\"type\":\"array\","
4624 "\"items\":{"
4625 "\"title\":\"InfoItem\","
4626 "\"type\":\"object\","
4627 "\"properties\":{"
4628 "\"objType\":{"
4629 "\"enum\":[\"Frontend\",\"Backend\",\"Listener\","
4630 "\"Server\",\"Unknown\"]"
4631 "},"
4632 "\"proxyId\":{"
4633 "\"type\":\"integer\","
4634 "\"minimum\":0"
4635 "},"
4636 "\"id\":{"
4637 "\"type\":\"integer\","
4638 "\"minimum\":0"
4639 "},"
4640 "\"field\":{\"$ref\":\"#/definitions/field\"},"
4641 "\"processNum\":{\"$ref\":\"#/definitions/processNum\"},"
4642 "\"tags\":{\"$ref\":\"#/definitions/tags\"},"
4643 "\"typedValue\":{\"$ref\":\"#/definitions/typedValue\"}"
4644 "},"
4645 "\"required\":[\"objType\",\"proxyId\",\"id\","
4646 "\"field\",\"processNum\",\"tags\","
4647 "\"value\"]"
4648 "}"
4649 "},"
4650 "{"
4651 "\"title\":\"Error\","
4652 "\"type\":\"object\","
4653 "\"properties\":{"
4654 "\"errorStr\":{"
4655 "\"type\":\"string\""
Amaury Denoyellea53ce4c2020-10-02 18:31:59 +02004656 "}"
4657 "},"
4658 "\"required\":[\"errorStr\"]"
Simon Horman6f6bb382017-01-04 09:37:26 +01004659 "}"
4660 "],"
4661 "\"definitions\":{"
4662 "\"field\":{"
4663 "\"type\":\"object\","
4664 "\"pos\":{"
4665 "\"type\":\"integer\","
4666 "\"minimum\":0"
4667 "},"
4668 "\"name\":{"
4669 "\"type\":\"string\""
4670 "},"
4671 "\"required\":[\"pos\",\"name\"]"
4672 "},"
4673 "\"processNum\":{"
4674 "\"type\":\"integer\","
4675 "\"minimum\":1"
4676 "},"
4677 "\"tags\":{"
4678 "\"type\":\"object\","
4679 "\"origin\":{"
4680 "\"type\":\"string\","
4681 "\"enum\":[\"Metric\",\"Status\",\"Key\","
4682 "\"Config\",\"Product\",\"Unknown\"]"
4683 "},"
4684 "\"nature\":{"
4685 "\"type\":\"string\","
4686 "\"enum\":[\"Gauge\",\"Limit\",\"Min\",\"Max\","
4687 "\"Rate\",\"Counter\",\"Duration\","
4688 "\"Age\",\"Time\",\"Name\",\"Output\","
4689 "\"Avg\", \"Unknown\"]"
4690 "},"
4691 "\"scope\":{"
4692 "\"type\":\"string\","
4693 "\"enum\":[\"Cluster\",\"Process\",\"Service\","
4694 "\"System\",\"Unknown\"]"
4695 "},"
4696 "\"required\":[\"origin\",\"nature\",\"scope\"]"
4697 "},"
4698 "\"typedValue\":{"
4699 "\"type\":\"object\","
4700 "\"oneOf\":["
4701 "{\"$ref\":\"#/definitions/typedValue/definitions/s32Value\"},"
4702 "{\"$ref\":\"#/definitions/typedValue/definitions/s64Value\"},"
4703 "{\"$ref\":\"#/definitions/typedValue/definitions/u32Value\"},"
4704 "{\"$ref\":\"#/definitions/typedValue/definitions/u64Value\"},"
4705 "{\"$ref\":\"#/definitions/typedValue/definitions/strValue\"}"
4706 "],"
4707 "\"definitions\":{"
4708 "\"s32Value\":{"
4709 "\"properties\":{"
4710 "\"type\":{"
4711 "\"type\":\"string\","
4712 "\"enum\":[\"s32\"]"
4713 "},"
4714 "\"value\":{"
4715 "\"type\":\"integer\","
4716 "\"minimum\":-2147483648,"
4717 "\"maximum\":2147483647"
4718 "}"
4719 "},"
4720 "\"required\":[\"type\",\"value\"]"
4721 "},"
4722 "\"s64Value\":{"
4723 "\"properties\":{"
4724 "\"type\":{"
4725 "\"type\":\"string\","
4726 "\"enum\":[\"s64\"]"
4727 "},"
4728 "\"value\":{"
4729 "\"type\":\"integer\","
4730 "\"minimum\":-9007199254740991,"
4731 "\"maximum\":9007199254740991"
4732 "}"
4733 "},"
4734 "\"required\":[\"type\",\"value\"]"
4735 "},"
4736 "\"u32Value\":{"
4737 "\"properties\":{"
4738 "\"type\":{"
4739 "\"type\":\"string\","
4740 "\"enum\":[\"u32\"]"
4741 "},"
4742 "\"value\":{"
4743 "\"type\":\"integer\","
4744 "\"minimum\":0,"
4745 "\"maximum\":4294967295"
4746 "}"
4747 "},"
4748 "\"required\":[\"type\",\"value\"]"
4749 "},"
4750 "\"u64Value\":{"
4751 "\"properties\":{"
4752 "\"type\":{"
4753 "\"type\":\"string\","
4754 "\"enum\":[\"u64\"]"
4755 "},"
4756 "\"value\":{"
4757 "\"type\":\"integer\","
4758 "\"minimum\":0,"
4759 "\"maximum\":9007199254740991"
4760 "}"
4761 "},"
4762 "\"required\":[\"type\",\"value\"]"
4763 "},"
4764 "\"strValue\":{"
4765 "\"properties\":{"
4766 "\"type\":{"
4767 "\"type\":\"string\","
4768 "\"enum\":[\"str\"]"
4769 "},"
4770 "\"value\":{\"type\":\"string\"}"
4771 "},"
4772 "\"required\":[\"type\",\"value\"]"
4773 "},"
4774 "\"unknownValue\":{"
4775 "\"properties\":{"
4776 "\"type\":{"
4777 "\"type\":\"integer\","
4778 "\"minimum\":0"
4779 "},"
4780 "\"value\":{"
4781 "\"type\":\"string\","
4782 "\"enum\":[\"unknown\"]"
4783 "}"
4784 "},"
4785 "\"required\":[\"type\",\"value\"]"
4786 "}"
4787 "}"
4788 "}"
4789 "}"
4790 "}");
4791
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004792 if (old_len == out->data) {
Simon Horman6f6bb382017-01-04 09:37:26 +01004793 chunk_reset(out);
4794 chunk_appendf(out,
4795 "{\"errorStr\":\"output buffer too short\"}");
4796 }
Willy Tarreau5a0e7ca2022-06-10 09:21:22 +02004797 chunk_appendf(out, "\n");
Simon Horman6f6bb382017-01-04 09:37:26 +01004798}
4799
Willy Tarreau4596fe22022-05-17 19:07:51 +02004800/* This function dumps the schema onto the stream connector's read buffer.
Simon Horman6f6bb382017-01-04 09:37:26 +01004801 * It returns 0 as long as it does not complete, non-zero upon completion.
4802 * No state is used.
4803 */
Willy Tarreaud0a06d52022-05-18 15:07:19 +02004804static int stats_dump_json_schema_to_buffer(struct appctx *appctx)
Simon Horman6f6bb382017-01-04 09:37:26 +01004805{
4806 chunk_reset(&trash);
4807
4808 stats_dump_json_schema(&trash);
4809
Willy Tarreaud0a06d52022-05-18 15:07:19 +02004810 if (applet_putchk(appctx, &trash) == -1)
Simon Horman6f6bb382017-01-04 09:37:26 +01004811 return 0;
Simon Horman6f6bb382017-01-04 09:37:26 +01004812
4813 return 1;
4814}
4815
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004816static int cli_parse_clear_counters(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau89d467c2016-11-23 11:02:40 +01004817{
4818 struct proxy *px;
4819 struct server *sv;
4820 struct listener *li;
Amaury Denoyelled3700a72020-10-05 11:49:43 +02004821 struct stats_module *mod;
Willy Tarreau89d467c2016-11-23 11:02:40 +01004822 int clrall = 0;
4823
4824 if (strcmp(args[2], "all") == 0)
4825 clrall = 1;
4826
4827 /* check permissions */
4828 if (!cli_has_level(appctx, ACCESS_LVL_OPER) ||
4829 (clrall && !cli_has_level(appctx, ACCESS_LVL_ADMIN)))
4830 return 1;
4831
Olivier Houchardfbc74e82017-11-24 16:54:05 +01004832 for (px = proxies_list; px; px = px->next) {
Willy Tarreau89d467c2016-11-23 11:02:40 +01004833 if (clrall) {
4834 memset(&px->be_counters, 0, sizeof(px->be_counters));
4835 memset(&px->fe_counters, 0, sizeof(px->fe_counters));
4836 }
4837 else {
4838 px->be_counters.conn_max = 0;
4839 px->be_counters.p.http.rps_max = 0;
4840 px->be_counters.sps_max = 0;
4841 px->be_counters.cps_max = 0;
4842 px->be_counters.nbpend_max = 0;
Christopher Fauletefb41f02019-11-08 14:53:15 +01004843 px->be_counters.qtime_max = 0;
4844 px->be_counters.ctime_max = 0;
4845 px->be_counters.dtime_max = 0;
4846 px->be_counters.ttime_max = 0;
Willy Tarreau89d467c2016-11-23 11:02:40 +01004847
4848 px->fe_counters.conn_max = 0;
4849 px->fe_counters.p.http.rps_max = 0;
4850 px->fe_counters.sps_max = 0;
4851 px->fe_counters.cps_max = 0;
Willy Tarreau89d467c2016-11-23 11:02:40 +01004852 }
4853
4854 for (sv = px->srv; sv; sv = sv->next)
4855 if (clrall)
4856 memset(&sv->counters, 0, sizeof(sv->counters));
4857 else {
4858 sv->counters.cur_sess_max = 0;
4859 sv->counters.nbpend_max = 0;
4860 sv->counters.sps_max = 0;
Christopher Fauletefb41f02019-11-08 14:53:15 +01004861 sv->counters.qtime_max = 0;
4862 sv->counters.ctime_max = 0;
4863 sv->counters.dtime_max = 0;
4864 sv->counters.ttime_max = 0;
Willy Tarreau89d467c2016-11-23 11:02:40 +01004865 }
4866
4867 list_for_each_entry(li, &px->conf.listeners, by_fe)
4868 if (li->counters) {
4869 if (clrall)
4870 memset(li->counters, 0, sizeof(*li->counters));
4871 else
4872 li->counters->conn_max = 0;
4873 }
4874 }
4875
4876 global.cps_max = 0;
4877 global.sps_max = 0;
Olivier Houchard00bc3cb2017-10-17 19:23:25 +02004878 global.ssl_max = 0;
4879 global.ssl_fe_keys_max = 0;
4880 global.ssl_be_keys_max = 0;
Willy Tarreaud80cb4e2018-01-20 19:30:13 +01004881
Amaury Denoyelled3700a72020-10-05 11:49:43 +02004882 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
4883 if (!mod->clearable && !clrall)
4884 continue;
4885
4886 for (px = proxies_list; px; px = px->next) {
4887 enum stats_domain_px_cap mod_cap = stats_px_get_cap(mod->domain_flags);
4888
4889 if (px->cap & PR_CAP_FE && mod_cap & STATS_PX_CAP_FE) {
4890 EXTRA_COUNTERS_INIT(px->extra_counters_fe,
4891 mod,
4892 mod->counters,
4893 mod->counters_size);
4894 }
4895
4896 if (px->cap & PR_CAP_BE && mod_cap & STATS_PX_CAP_BE) {
4897 EXTRA_COUNTERS_INIT(px->extra_counters_be,
4898 mod,
4899 mod->counters,
4900 mod->counters_size);
4901 }
4902
4903 if (mod_cap & STATS_PX_CAP_SRV) {
4904 for (sv = px->srv; sv; sv = sv->next) {
4905 EXTRA_COUNTERS_INIT(sv->extra_counters,
4906 mod,
4907 mod->counters,
4908 mod->counters_size);
4909 }
4910 }
4911
4912 if (mod_cap & STATS_PX_CAP_LI) {
4913 list_for_each_entry(li, &px->conf.listeners, by_fe) {
4914 EXTRA_COUNTERS_INIT(li->extra_counters,
4915 mod,
4916 mod->counters,
4917 mod->counters_size);
4918 }
4919 }
4920 }
4921 }
4922
Emeric Brunf8642ee2021-10-29 17:59:18 +02004923 resolv_stats_clear_counters(clrall, &stats_module_list[STATS_DOMAIN_RESOLVERS]);
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02004924
Willy Tarreaud80cb4e2018-01-20 19:30:13 +01004925 memset(activity, 0, sizeof(activity));
Willy Tarreau89d467c2016-11-23 11:02:40 +01004926 return 1;
4927}
4928
4929
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004930static int cli_parse_show_info(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004931{
Willy Tarreau91cefca2022-05-03 17:08:29 +02004932 struct show_stat_ctx *ctx = applet_reserve_svcctx(appctx, sizeof(*ctx));
Willy Tarreau2f397382019-10-09 11:43:59 +02004933 int arg = 2;
4934
Willy Tarreau91cefca2022-05-03 17:08:29 +02004935 ctx->scope_str = 0;
4936 ctx->scope_len = 0;
4937 ctx->flags = 0;
Willy Tarreaud25fc792016-12-16 12:33:47 +01004938
Willy Tarreau2f397382019-10-09 11:43:59 +02004939 while (*args[arg]) {
4940 if (strcmp(args[arg], "typed") == 0)
Willy Tarreau91cefca2022-05-03 17:08:29 +02004941 ctx->flags = (ctx->flags & ~STAT_FMT_MASK) | STAT_FMT_TYPED;
Willy Tarreau2f397382019-10-09 11:43:59 +02004942 else if (strcmp(args[arg], "json") == 0)
Willy Tarreau91cefca2022-05-03 17:08:29 +02004943 ctx->flags = (ctx->flags & ~STAT_FMT_MASK) | STAT_FMT_JSON;
Willy Tarreau2f397382019-10-09 11:43:59 +02004944 else if (strcmp(args[arg], "desc") == 0)
Willy Tarreau91cefca2022-05-03 17:08:29 +02004945 ctx->flags |= STAT_SHOW_FDESC;
Willy Tarreau27456202021-05-08 07:54:24 +02004946 else if (strcmp(args[arg], "float") == 0)
Willy Tarreau91cefca2022-05-03 17:08:29 +02004947 ctx->flags |= STAT_USE_FLOAT;
Willy Tarreau2f397382019-10-09 11:43:59 +02004948 arg++;
4949 }
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004950 return 0;
4951}
4952
4953
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004954static int cli_parse_show_stat(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau2b812e22016-11-22 16:18:05 +01004955{
Willy Tarreau91cefca2022-05-03 17:08:29 +02004956 struct show_stat_ctx *ctx = applet_reserve_svcctx(appctx, sizeof(*ctx));
Willy Tarreau2f397382019-10-09 11:43:59 +02004957 int arg = 2;
4958
Willy Tarreau91cefca2022-05-03 17:08:29 +02004959 ctx->scope_str = 0;
4960 ctx->scope_len = 0;
4961 ctx->flags = STAT_SHNODE | STAT_SHDESC;
Willy Tarreau578d6e42019-10-09 11:00:22 +02004962
Willy Tarreau0698c802022-05-11 14:09:57 +02004963 if ((strm_li(appctx_strm(appctx))->bind_conf->level & ACCESS_LVL_MASK) >= ACCESS_LVL_OPER)
Willy Tarreau91cefca2022-05-03 17:08:29 +02004964 ctx->flags |= STAT_SHLGNDS;
Willy Tarreaud25fc792016-12-16 12:33:47 +01004965
Amaury Denoyelle072f97e2020-10-05 11:49:37 +02004966 /* proxy is the default domain */
Willy Tarreau91cefca2022-05-03 17:08:29 +02004967 ctx->domain = STATS_DOMAIN_PROXY;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01004968 if (strcmp(args[arg], "domain") == 0) {
Amaury Denoyelle072f97e2020-10-05 11:49:37 +02004969 ++args;
4970
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01004971 if (strcmp(args[arg], "proxy") == 0) {
Amaury Denoyelle072f97e2020-10-05 11:49:37 +02004972 ++args;
Emeric Brunf8642ee2021-10-29 17:59:18 +02004973 } else if (strcmp(args[arg], "resolvers") == 0) {
Willy Tarreau91cefca2022-05-03 17:08:29 +02004974 ctx->domain = STATS_DOMAIN_RESOLVERS;
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02004975 ++args;
4976 } else {
Amaury Denoyelle072f97e2020-10-05 11:49:37 +02004977 return cli_err(appctx, "Invalid statistics domain.\n");
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02004978 }
Amaury Denoyelle072f97e2020-10-05 11:49:37 +02004979 }
4980
Willy Tarreau91cefca2022-05-03 17:08:29 +02004981 if (ctx->domain == STATS_DOMAIN_PROXY
Amaury Denoyelle072f97e2020-10-05 11:49:37 +02004982 && *args[arg] && *args[arg+1] && *args[arg+2]) {
Willy Tarreaua1b1ed52016-11-25 08:50:58 +01004983 struct proxy *px;
4984
Willy Tarreau2f397382019-10-09 11:43:59 +02004985 px = proxy_find_by_name(args[arg], 0, 0);
Willy Tarreaua1b1ed52016-11-25 08:50:58 +01004986 if (px)
Willy Tarreau91cefca2022-05-03 17:08:29 +02004987 ctx->iid = px->uuid;
Willy Tarreaua1b1ed52016-11-25 08:50:58 +01004988 else
Willy Tarreau91cefca2022-05-03 17:08:29 +02004989 ctx->iid = atoi(args[arg]);
Willy Tarreaua1b1ed52016-11-25 08:50:58 +01004990
Willy Tarreau91cefca2022-05-03 17:08:29 +02004991 if (!ctx->iid)
Willy Tarreau9d008692019-08-09 11:21:01 +02004992 return cli_err(appctx, "No such proxy.\n");
Willy Tarreaua1b1ed52016-11-25 08:50:58 +01004993
Willy Tarreau91cefca2022-05-03 17:08:29 +02004994 ctx->flags |= STAT_BOUND;
4995 ctx->type = atoi(args[arg+1]);
4996 ctx->sid = atoi(args[arg+2]);
Willy Tarreau2f397382019-10-09 11:43:59 +02004997 arg += 3;
4998 }
4999
5000 while (*args[arg]) {
5001 if (strcmp(args[arg], "typed") == 0)
Willy Tarreau91cefca2022-05-03 17:08:29 +02005002 ctx->flags = (ctx->flags & ~STAT_FMT_MASK) | STAT_FMT_TYPED;
Willy Tarreau2f397382019-10-09 11:43:59 +02005003 else if (strcmp(args[arg], "json") == 0)
Willy Tarreau91cefca2022-05-03 17:08:29 +02005004 ctx->flags = (ctx->flags & ~STAT_FMT_MASK) | STAT_FMT_JSON;
Willy Tarreau2f397382019-10-09 11:43:59 +02005005 else if (strcmp(args[arg], "desc") == 0)
Willy Tarreau91cefca2022-05-03 17:08:29 +02005006 ctx->flags |= STAT_SHOW_FDESC;
Willy Tarreau3e320362020-10-23 17:28:57 +02005007 else if (strcmp(args[arg], "no-maint") == 0)
Willy Tarreau91cefca2022-05-03 17:08:29 +02005008 ctx->flags |= STAT_HIDE_MAINT;
Willy Tarreau65141ff2020-10-23 17:19:48 +02005009 else if (strcmp(args[arg], "up") == 0)
Willy Tarreau91cefca2022-05-03 17:08:29 +02005010 ctx->flags |= STAT_HIDE_DOWN;
Willy Tarreau2f397382019-10-09 11:43:59 +02005011 arg++;
Willy Tarreau2b812e22016-11-22 16:18:05 +01005012 }
Willy Tarreau2b812e22016-11-22 16:18:05 +01005013
Willy Tarreau2b812e22016-11-22 16:18:05 +01005014 return 0;
5015}
5016
Willy Tarreau0baac8c2016-11-22 16:36:53 +01005017static int cli_io_handler_dump_info(struct appctx *appctx)
5018{
Willy Tarreauc12b3212022-05-27 11:08:15 +02005019 return stats_dump_info_to_buffer(appctx_sc(appctx));
Willy Tarreau0baac8c2016-11-22 16:36:53 +01005020}
5021
Willy Tarreau4596fe22022-05-17 19:07:51 +02005022/* This I/O handler runs as an applet embedded in a stream connector. It is
Willy Tarreau2b812e22016-11-22 16:18:05 +01005023 * used to send raw stats over a socket.
5024 */
5025static int cli_io_handler_dump_stat(struct appctx *appctx)
5026{
Willy Tarreauc12b3212022-05-27 11:08:15 +02005027 return stats_dump_stat_to_buffer(appctx_sc(appctx), NULL, NULL);
Willy Tarreau2b812e22016-11-22 16:18:05 +01005028}
5029
Simon Horman6f6bb382017-01-04 09:37:26 +01005030static int cli_io_handler_dump_json_schema(struct appctx *appctx)
5031{
Willy Tarreaud0a06d52022-05-18 15:07:19 +02005032 return stats_dump_json_schema_to_buffer(appctx);
Simon Horman6f6bb382017-01-04 09:37:26 +01005033}
5034
Amaury Denoyelle216a1ce2021-03-18 15:48:14 +01005035int stats_allocate_proxy_counters_internal(struct extra_counters **counters,
5036 int type, int px_cap)
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02005037{
5038 struct stats_module *mod;
5039
5040 EXTRA_COUNTERS_REGISTER(counters, type, alloc_failed);
5041
5042 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
5043 if (!(stats_px_get_cap(mod->domain_flags) & px_cap))
5044 continue;
5045
5046 EXTRA_COUNTERS_ADD(mod, *counters, mod->counters, mod->counters_size);
5047 }
5048
5049 EXTRA_COUNTERS_ALLOC(*counters, alloc_failed);
5050
5051 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
5052 if (!(stats_px_get_cap(mod->domain_flags) & px_cap))
5053 continue;
5054
5055 EXTRA_COUNTERS_INIT(*counters, mod, mod->counters, mod->counters_size);
5056 }
5057
5058 return 1;
5059
5060 alloc_failed:
5061 return 0;
5062}
5063
5064/* Initialize and allocate all extra counters for a proxy and its attached
5065 * servers/listeners with all already registered stats module
5066 */
5067int stats_allocate_proxy_counters(struct proxy *px)
5068{
5069 struct server *sv;
5070 struct listener *li;
5071
5072 if (px->cap & PR_CAP_FE) {
5073 if (!stats_allocate_proxy_counters_internal(&px->extra_counters_fe,
5074 COUNTERS_FE,
5075 STATS_PX_CAP_FE)) {
5076 return 0;
5077 }
5078 }
5079
5080 if (px->cap & PR_CAP_BE) {
5081 if (!stats_allocate_proxy_counters_internal(&px->extra_counters_be,
5082 COUNTERS_BE,
5083 STATS_PX_CAP_BE)) {
5084 return 0;
5085 }
5086 }
5087
5088 for (sv = px->srv; sv; sv = sv->next) {
5089 if (!stats_allocate_proxy_counters_internal(&sv->extra_counters,
5090 COUNTERS_SV,
5091 STATS_PX_CAP_SRV)) {
5092 return 0;
5093 }
5094 }
5095
5096 list_for_each_entry(li, &px->conf.listeners, by_fe) {
5097 if (!stats_allocate_proxy_counters_internal(&li->extra_counters,
5098 COUNTERS_LI,
5099 STATS_PX_CAP_LI)) {
5100 return 0;
5101 }
5102 }
5103
5104 return 1;
5105}
5106
Amaury Denoyelle58d395e2020-10-05 11:49:40 +02005107void stats_register_module(struct stats_module *m)
5108{
5109 const uint8_t domain = stats_get_domain(m->domain_flags);
5110
Willy Tarreau2b718102021-04-21 07:32:39 +02005111 LIST_APPEND(&stats_module_list[domain], &m->list);
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02005112 stat_count[domain] += m->stats_count;
Amaury Denoyelle58d395e2020-10-05 11:49:40 +02005113}
5114
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02005115static int allocate_stats_px_postcheck(void)
5116{
5117 struct stats_module *mod;
5118 size_t i = ST_F_TOTAL_FIELDS;
5119 int err_code = 0;
Amaury Denoyelle27373f72020-10-05 16:57:33 +02005120 struct proxy *px;
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02005121
5122 stat_count[STATS_DOMAIN_PROXY] += ST_F_TOTAL_FIELDS;
5123
5124 stat_f[STATS_DOMAIN_PROXY] = malloc(stat_count[STATS_DOMAIN_PROXY] * sizeof(struct name_desc));
5125 if (!stat_f[STATS_DOMAIN_PROXY]) {
5126 ha_alert("stats: cannot allocate all fields for proxy statistics\n");
5127 err_code |= ERR_ALERT | ERR_FATAL;
5128 return err_code;
5129 }
5130
5131 memcpy(stat_f[STATS_DOMAIN_PROXY], stat_fields,
5132 ST_F_TOTAL_FIELDS * sizeof(struct name_desc));
5133
5134 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
5135 memcpy(stat_f[STATS_DOMAIN_PROXY] + i,
5136 mod->stats,
5137 mod->stats_count * sizeof(struct name_desc));
5138 i += mod->stats_count;
5139 }
5140
Amaury Denoyelle27373f72020-10-05 16:57:33 +02005141 for (px = proxies_list; px; px = px->next) {
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02005142 if (!stats_allocate_proxy_counters(px)) {
5143 ha_alert("stats: cannot allocate all counters for proxy statistics\n");
5144 err_code |= ERR_ALERT | ERR_FATAL;
5145 return err_code;
5146 }
5147 }
5148
Christopher Fauletde79cd22021-01-06 07:41:56 +01005149 /* wait per-thread alloc to perform corresponding stat_l allocation */
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02005150
5151 return err_code;
5152}
5153
5154REGISTER_CONFIG_POSTPARSER("allocate-stats-px", allocate_stats_px_postcheck);
5155
Emeric Brunf8642ee2021-10-29 17:59:18 +02005156static int allocate_stats_rslv_postcheck(void)
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02005157{
5158 struct stats_module *mod;
5159 size_t i = 0;
5160 int err_code = 0;
5161
Emeric Brunf8642ee2021-10-29 17:59:18 +02005162 stat_f[STATS_DOMAIN_RESOLVERS] = malloc(stat_count[STATS_DOMAIN_RESOLVERS] * sizeof(struct name_desc));
5163 if (!stat_f[STATS_DOMAIN_RESOLVERS]) {
5164 ha_alert("stats: cannot allocate all fields for resolver statistics\n");
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02005165 err_code |= ERR_ALERT | ERR_FATAL;
5166 return err_code;
5167 }
5168
Emeric Brunf8642ee2021-10-29 17:59:18 +02005169 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_RESOLVERS], list) {
5170 memcpy(stat_f[STATS_DOMAIN_RESOLVERS] + i,
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02005171 mod->stats,
5172 mod->stats_count * sizeof(struct name_desc));
5173 i += mod->stats_count;
5174 }
5175
Emeric Brunf8642ee2021-10-29 17:59:18 +02005176 if (!resolv_allocate_counters(&stats_module_list[STATS_DOMAIN_RESOLVERS])) {
5177 ha_alert("stats: cannot allocate all counters for resolver statistics\n");
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02005178 err_code |= ERR_ALERT | ERR_FATAL;
5179 return err_code;
5180 }
5181
Christopher Fauletde79cd22021-01-06 07:41:56 +01005182 /* wait per-thread alloc to perform corresponding stat_l allocation */
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02005183
5184 return err_code;
5185}
5186
Emeric Brunf8642ee2021-10-29 17:59:18 +02005187REGISTER_CONFIG_POSTPARSER("allocate-stats-resolver", allocate_stats_rslv_postcheck);
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02005188
Christopher Fauletde79cd22021-01-06 07:41:56 +01005189static int allocate_stat_lines_per_thread(void)
5190{
Emeric Brunf8642ee2021-10-29 17:59:18 +02005191 int domains[] = { STATS_DOMAIN_PROXY, STATS_DOMAIN_RESOLVERS }, i;
Christopher Fauletde79cd22021-01-06 07:41:56 +01005192
5193 for (i = 0; i < STATS_DOMAIN_COUNT; ++i) {
5194 const int domain = domains[i];
5195
5196 stat_l[domain] = malloc(stat_count[domain] * sizeof(struct field));
5197 if (!stat_l[domain])
5198 return 0;
5199 }
5200 return 1;
5201}
5202
5203REGISTER_PER_THREAD_ALLOC(allocate_stat_lines_per_thread);
5204
Amaury Denoyelle7f8f6cb2020-11-10 14:24:31 +01005205static int allocate_trash_counters(void)
5206{
5207 struct stats_module *mod;
Emeric Brunf8642ee2021-10-29 17:59:18 +02005208 int domains[] = { STATS_DOMAIN_PROXY, STATS_DOMAIN_RESOLVERS }, i;
Amaury Denoyelle7f8f6cb2020-11-10 14:24:31 +01005209 size_t max_counters_size = 0;
5210
5211 /* calculate the greatest counters used by any stats modules */
5212 for (i = 0; i < STATS_DOMAIN_COUNT; ++i) {
5213 list_for_each_entry(mod, &stats_module_list[domains[i]], list) {
5214 max_counters_size = mod->counters_size > max_counters_size ?
5215 mod->counters_size : max_counters_size;
5216 }
5217 }
5218
5219 /* allocate the trash with the size of the greatest counters */
5220 if (max_counters_size) {
5221 trash_counters = malloc(max_counters_size);
5222 if (!trash_counters) {
5223 ha_alert("stats: cannot allocate trash counters for statistics\n");
5224 return 0;
5225 }
5226 }
5227
5228 return 1;
5229}
5230
5231REGISTER_PER_THREAD_ALLOC(allocate_trash_counters);
5232
Christopher Fauletde79cd22021-01-06 07:41:56 +01005233static void deinit_stat_lines_per_thread(void)
Amaury Denoyellea2a68992020-11-10 14:24:30 +01005234{
Emeric Brunf8642ee2021-10-29 17:59:18 +02005235 int domains[] = { STATS_DOMAIN_PROXY, STATS_DOMAIN_RESOLVERS }, i;
Amaury Denoyellea2a68992020-11-10 14:24:30 +01005236
5237 for (i = 0; i < STATS_DOMAIN_COUNT; ++i) {
5238 const int domain = domains[i];
5239
Willy Tarreau61cfdf42021-02-20 10:46:51 +01005240 ha_free(&stat_l[domain]);
Christopher Fauletde79cd22021-01-06 07:41:56 +01005241 }
5242}
5243
5244
5245REGISTER_PER_THREAD_FREE(deinit_stat_lines_per_thread);
5246
5247static void deinit_stats(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];
Amaury Denoyellea2a68992020-11-10 14:24:30 +01005253
5254 if (stat_f[domain])
5255 free(stat_f[domain]);
5256 }
5257}
5258
5259REGISTER_POST_DEINIT(deinit_stats);
5260
Amaury Denoyelle7f8f6cb2020-11-10 14:24:31 +01005261static void free_trash_counters(void)
5262{
5263 if (trash_counters)
5264 free(trash_counters);
5265}
5266
5267REGISTER_PER_THREAD_FREE(free_trash_counters);
5268
Willy Tarreau2b812e22016-11-22 16:18:05 +01005269/* register cli keywords */
5270static struct cli_kw_list cli_kws = {{ },{
Willy Tarreaub205bfd2021-05-07 11:38:37 +02005271 { { "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 +02005272 { { "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 +02005273 { { "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 },
5274 { { "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 +01005275 {{},}
5276}};
5277
Willy Tarreau0108d902018-11-25 19:14:37 +01005278INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
5279
William Lallemand74c24fb2016-11-21 17:18:36 +01005280struct applet http_stats_applet = {
5281 .obj_type = OBJ_TYPE_APPLET,
5282 .name = "<STATS>", /* used for logging */
5283 .fct = http_stats_io_handler,
5284 .release = NULL,
5285};
5286
William Lallemand74c24fb2016-11-21 17:18:36 +01005287/*
5288 * Local variables:
5289 * c-indent-level: 8
5290 * c-basic-offset: 8
5291 * End:
5292 */