blob: 22e0d84812ae8797a009f12ce0143e29453eaff8 [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>
16#include <fcntl.h>
17#include <stdio.h>
18#include <stdlib.h>
19#include <string.h>
20#include <pwd.h>
21#include <grp.h>
22
23#include <sys/socket.h>
24#include <sys/stat.h>
25#include <sys/types.h>
26
Willy Tarreaub2551052020-06-09 09:07:15 +020027#include <haproxy/api.h>
Willy Tarreau5d9ddc52021-10-06 19:54:09 +020028#include <haproxy/activity.h>
Willy Tarreau3f0f82e2020-06-04 19:42:41 +020029#include <haproxy/applet-t.h>
Willy Tarreau49801602020-06-04 22:50:02 +020030#include <haproxy/backend.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020031#include <haproxy/base64.h>
Willy Tarreau6be78492020-06-05 00:00:29 +020032#include <haproxy/cfgparse.h>
Willy Tarreauf1d32c42020-06-04 21:07:02 +020033#include <haproxy/channel.h>
Willy Tarreau4aa573d2020-06-04 18:21:56 +020034#include <haproxy/check.h>
Willy Tarreau83487a82020-06-04 20:19:54 +020035#include <haproxy/cli.h>
Willy Tarreau55542642021-10-08 09:33:24 +020036#include <haproxy/clock.h>
Willy Tarreau0a3bd392020-06-04 08:52:38 +020037#include <haproxy/compression.h>
Christopher Faulet908628c2022-03-25 16:43:49 +010038#include <haproxy/conn_stream.h>
39#include <haproxy/cs_utils.h>
Willy Tarreau2a83d602020-05-27 16:58:08 +020040#include <haproxy/debug.h>
Willy Tarreau36979d92020-06-05 17:27:29 +020041#include <haproxy/errors.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020042#include <haproxy/fd.h>
43#include <haproxy/freq_ctr.h>
Willy Tarreau762d7a52020-06-04 11:23:07 +020044#include <haproxy/frontend.h>
Willy Tarreauf268ee82020-06-04 17:05:57 +020045#include <haproxy/global.h>
Willy Tarreaucd72d8c2020-06-02 19:11:26 +020046#include <haproxy/http.h>
Willy Tarreaub7fc4c42021-10-06 18:56:42 +020047#include <haproxy/http_ana.h>
Willy Tarreau87735332020-06-04 09:08:41 +020048#include <haproxy/http_htx.h>
Willy Tarreau16f958c2020-06-03 08:44:35 +020049#include <haproxy/htx.h>
Willy Tarreau853b2972020-05-27 18:01:47 +020050#include <haproxy/list.h>
Willy Tarreau36979d92020-06-05 17:27:29 +020051#include <haproxy/listener.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020052#include <haproxy/log.h>
Willy Tarreau2cd58092020-06-04 15:10:43 +020053#include <haproxy/map-t.h>
Willy Tarreau225a90a2020-06-04 15:06:28 +020054#include <haproxy/pattern-t.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020055#include <haproxy/pipe.h>
56#include <haproxy/pool.h>
Willy Tarreaua264d962020-06-04 22:29:18 +020057#include <haproxy/proxy.h>
Emeric Brunc9437992021-02-12 19:42:55 +010058#include <haproxy/resolvers.h>
Willy Tarreau1e56f922020-06-04 23:20:13 +020059#include <haproxy/server.h>
Willy Tarreau48d25b32020-06-04 18:58:52 +020060#include <haproxy/session.h>
Willy Tarreau2eec9b52020-06-04 19:58:55 +020061#include <haproxy/stats.h>
Willy Tarreaudfd3de82020-06-04 23:46:14 +020062#include <haproxy/stream.h>
Willy Tarreau5e539c92020-06-04 20:45:39 +020063#include <haproxy/stream_interface.h>
Willy Tarreaucea0e1b2020-06-04 17:25:40 +020064#include <haproxy/task.h>
Willy Tarreauc2f7c582020-06-02 18:15:32 +020065#include <haproxy/ticks.h>
Willy Tarreau92b4f132020-06-01 11:05:15 +020066#include <haproxy/time.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020067#include <haproxy/tools.h>
Willy Tarreau8c42b8a2020-06-04 19:27:34 +020068#include <haproxy/uri_auth-t.h>
Willy Tarreaud6788052020-05-27 15:59:00 +020069#include <haproxy/version.h>
William Lallemand74c24fb2016-11-21 17:18:36 +010070
William Lallemand74c24fb2016-11-21 17:18:36 +010071
Christopher Faulet2b9b6782019-02-27 16:42:58 +010072/* status codes available for the stats admin page (strictly 4 chars length) */
73const char *stat_status_codes[STAT_STATUS_SIZE] = {
74 [STAT_STATUS_DENY] = "DENY",
75 [STAT_STATUS_DONE] = "DONE",
76 [STAT_STATUS_ERRP] = "ERRP",
77 [STAT_STATUS_EXCD] = "EXCD",
78 [STAT_STATUS_NONE] = "NONE",
79 [STAT_STATUS_PART] = "PART",
80 [STAT_STATUS_UNKN] = "UNKN",
81 [STAT_STATUS_IVAL] = "IVAL",
82};
83
Willy Tarreau0baac8c2016-11-22 16:36:53 +010084/* These are the field names for each INF_* field position. Please pay attention
85 * to always use the exact same name except that the strings for new names must
86 * be lower case or CamelCase while the enum entries must be upper case.
87 */
Willy Tarreaueaa55372019-10-09 07:39:11 +020088const struct name_desc info_fields[INF_TOTAL_FIELDS] = {
Willy Tarreau6d4897e2019-10-11 16:31:46 +020089 [INF_NAME] = { .name = "Name", .desc = "Product name" },
90 [INF_VERSION] = { .name = "Version", .desc = "Product version" },
91 [INF_RELEASE_DATE] = { .name = "Release_date", .desc = "Date of latest source code update" },
92 [INF_NBTHREAD] = { .name = "Nbthread", .desc = "Number of started threads (global.nbthread)" },
Willy Tarreau91358592021-06-15 08:08:04 +020093 [INF_NBPROC] = { .name = "Nbproc", .desc = "Number of started worker processes (historical, always 1)" },
Willy Tarreaue8422bf2021-06-15 09:08:18 +020094 [INF_PROCESS_NUM] = { .name = "Process_num", .desc = "Relative worker process number (1)" },
Willy Tarreau6d4897e2019-10-11 16:31:46 +020095 [INF_PID] = { .name = "Pid", .desc = "This worker process identifier for the system" },
96 [INF_UPTIME] = { .name = "Uptime", .desc = "How long ago this worker process was started (days+hours+minutes+seconds)" },
97 [INF_UPTIME_SEC] = { .name = "Uptime_sec", .desc = "How long ago this worker process was started (seconds)" },
William Dauchydefd1562021-01-15 22:41:38 +010098 [INF_START_TIME_SEC] = { .name = "Start_time_sec", .desc = "Start time in seconds" },
Willy Tarreau6d4897e2019-10-11 16:31:46 +020099 [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 +0100100 [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 +0200101 [INF_POOL_ALLOC_MB] = { .name = "PoolAlloc_MB", .desc = "Amount of memory allocated in pools (in MB)" },
William Dauchya8766cf2021-01-15 22:41:37 +0100102 [INF_POOL_ALLOC_BYTES] = { .name = "PoolAlloc_bytes", .desc = "Amount of memory allocated in pools (in bytes)" },
Willy Tarreau6d4897e2019-10-11 16:31:46 +0200103 [INF_POOL_USED_MB] = { .name = "PoolUsed_MB", .desc = "Amount of pool memory currently used (in MB)" },
William Dauchya8766cf2021-01-15 22:41:37 +0100104 [INF_POOL_USED_BYTES] = { .name = "PoolUsed_bytes", .desc = "Amount of pool memory currently used (in bytes)" },
Willy Tarreau6d4897e2019-10-11 16:31:46 +0200105 [INF_POOL_FAILED] = { .name = "PoolFailed", .desc = "Number of failed pool allocations since this worker was started" },
106 [INF_ULIMIT_N] = { .name = "Ulimit-n", .desc = "Hard limit on the number of per-process file descriptors" },
107 [INF_MAXSOCK] = { .name = "Maxsock", .desc = "Hard limit on the number of per-process sockets" },
108 [INF_MAXCONN] = { .name = "Maxconn", .desc = "Hard limit on the number of per-process connections (configured or imposed by Ulimit-n)" },
109 [INF_HARD_MAXCONN] = { .name = "Hard_maxconn", .desc = "Hard limit on the number of per-process connections (imposed by Memmax_MB or Ulimit-n)" },
110 [INF_CURR_CONN] = { .name = "CurrConns", .desc = "Current number of connections on this worker process" },
111 [INF_CUM_CONN] = { .name = "CumConns", .desc = "Total number of connections on this worker process since started" },
112 [INF_CUM_REQ] = { .name = "CumReq", .desc = "Total number of requests on this worker process since started" },
113 [INF_MAX_SSL_CONNS] = { .name = "MaxSslConns", .desc = "Hard limit on the number of per-process SSL endpoints (front+back), 0=unlimited" },
114 [INF_CURR_SSL_CONNS] = { .name = "CurrSslConns", .desc = "Current number of SSL endpoints on this worker process (front+back)" },
115 [INF_CUM_SSL_CONNS] = { .name = "CumSslConns", .desc = "Total number of SSL endpoints on this worker process since started (front+back)" },
116 [INF_MAXPIPES] = { .name = "Maxpipes", .desc = "Hard limit on the number of pipes for splicing, 0=unlimited" },
117 [INF_PIPES_USED] = { .name = "PipesUsed", .desc = "Current number of pipes in use in this worker process" },
118 [INF_PIPES_FREE] = { .name = "PipesFree", .desc = "Current number of allocated and available pipes in this worker process" },
119 [INF_CONN_RATE] = { .name = "ConnRate", .desc = "Number of front connections created on this worker process over the last second" },
120 [INF_CONN_RATE_LIMIT] = { .name = "ConnRateLimit", .desc = "Hard limit for ConnRate (global.maxconnrate)" },
121 [INF_MAX_CONN_RATE] = { .name = "MaxConnRate", .desc = "Highest ConnRate reached on this worker process since started (in connections per second)" },
122 [INF_SESS_RATE] = { .name = "SessRate", .desc = "Number of sessions created on this worker process over the last second" },
123 [INF_SESS_RATE_LIMIT] = { .name = "SessRateLimit", .desc = "Hard limit for SessRate (global.maxsessrate)" },
124 [INF_MAX_SESS_RATE] = { .name = "MaxSessRate", .desc = "Highest SessRate reached on this worker process since started (in sessions per second)" },
125 [INF_SSL_RATE] = { .name = "SslRate", .desc = "Number of SSL connections created on this worker process over the last second" },
126 [INF_SSL_RATE_LIMIT] = { .name = "SslRateLimit", .desc = "Hard limit for SslRate (global.maxsslrate)" },
127 [INF_MAX_SSL_RATE] = { .name = "MaxSslRate", .desc = "Highest SslRate reached on this worker process since started (in connections per second)" },
128 [INF_SSL_FRONTEND_KEY_RATE] = { .name = "SslFrontendKeyRate", .desc = "Number of SSL keys created on frontends in this worker process over the last second" },
129 [INF_SSL_FRONTEND_MAX_KEY_RATE] = { .name = "SslFrontendMaxKeyRate", .desc = "Highest SslFrontendKeyRate reached on this worker process since started (in SSL keys per second)" },
130 [INF_SSL_FRONTEND_SESSION_REUSE_PCT] = { .name = "SslFrontendSessionReuse_pct", .desc = "Percent of frontend SSL connections which did not require a new key" },
131 [INF_SSL_BACKEND_KEY_RATE] = { .name = "SslBackendKeyRate", .desc = "Number of SSL keys created on backends in this worker process over the last second" },
132 [INF_SSL_BACKEND_MAX_KEY_RATE] = { .name = "SslBackendMaxKeyRate", .desc = "Highest SslBackendKeyRate reached on this worker process since started (in SSL keys per second)" },
133 [INF_SSL_CACHE_LOOKUPS] = { .name = "SslCacheLookups", .desc = "Total number of SSL session ID lookups in the SSL session cache on this worker since started" },
134 [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" },
135 [INF_COMPRESS_BPS_IN] = { .name = "CompressBpsIn", .desc = "Number of bytes submitted to the HTTP compressor in this worker process over the last second" },
136 [INF_COMPRESS_BPS_OUT] = { .name = "CompressBpsOut", .desc = "Number of bytes emitted by the HTTP compressor in this worker process over the last second" },
137 [INF_COMPRESS_BPS_RATE_LIM] = { .name = "CompressBpsRateLim", .desc = "Limit of CompressBpsOut beyond which HTTP compression is automatically disabled" },
138 [INF_ZLIB_MEM_USAGE] = { .name = "ZlibMemUsage", .desc = "Amount of memory currently used by HTTP compression on the current worker process (in bytes)" },
139 [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)" },
140 [INF_TASKS] = { .name = "Tasks", .desc = "Total number of tasks in the current worker process (active + sleeping)" },
141 [INF_RUN_QUEUE] = { .name = "Run_queue", .desc = "Total number of active tasks+tasklets in the current worker process" },
142 [INF_IDLE_PCT] = { .name = "Idle_pct", .desc = "Percentage of last second spent waiting in the current worker thread" },
143 [INF_NODE] = { .name = "node", .desc = "Node name (global.node)" },
144 [INF_DESCRIPTION] = { .name = "description", .desc = "Node description (global.description)" },
145 [INF_STOPPING] = { .name = "Stopping", .desc = "1 if the worker process is currently stopping, otherwise zero" },
146 [INF_JOBS] = { .name = "Jobs", .desc = "Current number of active jobs on the current worker process (frontend connections, master connections, listeners)" },
147 [INF_UNSTOPPABLE_JOBS] = { .name = "Unstoppable Jobs", .desc = "Current number of unstoppable jobs on the current worker process (master connections)" },
148 [INF_LISTENERS] = { .name = "Listeners", .desc = "Current number of active listeners on the current worker process" },
149 [INF_ACTIVE_PEERS] = { .name = "ActivePeers", .desc = "Current number of verified active peers connections on the current worker process" },
150 [INF_CONNECTED_PEERS] = { .name = "ConnectedPeers", .desc = "Current number of peers having passed the connection step on the current worker process" },
151 [INF_DROPPED_LOGS] = { .name = "DroppedLogs", .desc = "Total number of dropped logs for current worker process since started" },
152 [INF_BUSY_POLLING] = { .name = "BusyPolling", .desc = "1 if busy-polling is currently in use on the worker process, otherwise zero (config.busy-polling)" },
153 [INF_FAILED_RESOLUTIONS] = { .name = "FailedResolutions", .desc = "Total number of failed DNS resolutions in current worker process since started" },
154 [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 +0200155 [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 +0200156 [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 +0200157 [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 +0200158 [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 +0100159 [INF_BUILD_INFO] = { .name = "Build info", .desc = "Build info" },
Amaury Denoyelle5dfdf3e2021-05-05 17:09:12 +0200160 [INF_TAINTED] = { .name = "Tainted", .desc = "Experimental features used" },
Willy Tarreau0baac8c2016-11-22 16:36:53 +0100161};
162
Willy Tarreaueaa55372019-10-09 07:39:11 +0200163const struct name_desc stat_fields[ST_F_TOTAL_FIELDS] = {
Willy Tarreau6d4897e2019-10-11 16:31:46 +0200164 [ST_F_PXNAME] = { .name = "pxname", .desc = "Proxy name" },
165 [ST_F_SVNAME] = { .name = "svname", .desc = "Server name" },
William Dauchyeedb9b12021-02-01 13:11:53 +0100166 [ST_F_QCUR] = { .name = "qcur", .desc = "Number of current queued connections" },
167 [ST_F_QMAX] = { .name = "qmax", .desc = "Highest value of queued connections encountered since process started" },
William Dauchy19f7cfc2021-02-01 13:11:54 +0100168 [ST_F_SCUR] = { .name = "scur", .desc = "Number of current sessions on the frontend, backend or server" },
169 [ST_F_SMAX] = { .name = "smax", .desc = "Highest value of current sessions encountered since process started" },
Willy Tarreau6d4897e2019-10-11 16:31:46 +0200170 [ST_F_SLIM] = { .name = "slim", .desc = "Frontend/listener/server's maxconn, backend's fullconn" },
171 [ST_F_STOT] = { .name = "stot", .desc = "Total number of sessions since process started" },
172 [ST_F_BIN] = { .name = "bin", .desc = "Total number of request bytes since process started" },
173 [ST_F_BOUT] = { .name = "bout", .desc = "Total number of response bytes since process started" },
174 [ST_F_DREQ] = { .name = "dreq", .desc = "Total number of denied requests since process started" },
175 [ST_F_DRESP] = { .name = "dresp", .desc = "Total number of denied responses since process started" },
176 [ST_F_EREQ] = { .name = "ereq", .desc = "Total number of invalid requests since process started" },
177 [ST_F_ECON] = { .name = "econ", .desc = "Total number of failed connections to server since the worker process started" },
178 [ST_F_ERESP] = { .name = "eresp", .desc = "Total number of invalid responses since the worker process started" },
179 [ST_F_WRETR] = { .name = "wretr", .desc = "Total number of server connection retries since the worker process started" },
180 [ST_F_WREDIS] = { .name = "wredis", .desc = "Total number of server redispatches due to connection failures since the worker process started" },
181 [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 +0200182 [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 +0200183 [ST_F_ACT] = { .name = "act", .desc = "Total number of active UP servers with a non-zero weight" },
184 [ST_F_BCK] = { .name = "bck", .desc = "Total number of backup UP servers with a non-zero weight" },
185 [ST_F_CHKFAIL] = { .name = "chkfail", .desc = "Total number of failed individual health checks per server/backend, since the worker process started" },
186 [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" },
187 [ST_F_LASTCHG] = { .name = "lastchg", .desc = "How long ago the last server state changed, in seconds" },
188 [ST_F_DOWNTIME] = { .name = "downtime", .desc = "Total time spent in DOWN state, for server or backend" },
189 [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 +0200190 [ST_F_PID] = { .name = "pid", .desc = "Relative worker process number (1)" },
Willy Tarreau6d4897e2019-10-11 16:31:46 +0200191 [ST_F_IID] = { .name = "iid", .desc = "Frontend or Backend numeric identifier ('id' setting)" },
192 [ST_F_SID] = { .name = "sid", .desc = "Server numeric identifier ('id' setting)" },
193 [ST_F_THROTTLE] = { .name = "throttle", .desc = "Throttling ratio applied to a server's maxconn and weight during the slowstart period (0 to 100%)" },
194 [ST_F_LBTOT] = { .name = "lbtot", .desc = "Total number of requests routed by load balancing since the worker process started (ignores queue pop and stickiness)" },
195 [ST_F_TRACKED] = { .name = "tracked", .desc = "Name of the other server this server tracks for its state" },
196 [ST_F_TYPE] = { .name = "type", .desc = "Type of the object (Listener, Frontend, Backend, Server)" },
197 [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)" },
198 [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 +0100199 [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 +0200200 [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" },
201 [ST_F_CHECK_CODE] = { .name = "check_code", .desc = "HTTP/SMTP/LDAP status code reported by the latest server health check" },
202 [ST_F_CHECK_DURATION] = { .name = "check_duration", .desc = "Total duration of the latest server health check, in milliseconds" },
203 [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" },
204 [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" },
205 [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" },
206 [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" },
207 [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" },
208 [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)" },
209 [ST_F_HANAFAIL] = { .name = "hanafail", .desc = "Total number of failed checks caused by an 'on-error' directive after an 'observe' condition matched" },
210 [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 +0100211 [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 +0200212 [ST_F_REQ_TOT] = { .name = "req_tot", .desc = "Total number of HTTP requests processed by this object since the worker process started" },
213 [ST_F_CLI_ABRT] = { .name = "cli_abrt", .desc = "Total number of requests or connections aborted by the client since the worker process started" },
214 [ST_F_SRV_ABRT] = { .name = "srv_abrt", .desc = "Total number of requests or connections aborted by the server since the worker process started" },
215 [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" },
216 [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" },
217 [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)" },
218 [ST_F_COMP_RSP] = { .name = "comp_rsp", .desc = "Total number of HTTP responses that were compressed for this object since the worker process started" },
219 [ST_F_LASTSESS] = { .name = "lastsess", .desc = "How long ago some traffic was seen on this object on this worker process, in seconds" },
220 [ST_F_LAST_CHK] = { .name = "last_chk", .desc = "Short description of the latest health check report for this server (see also check_desc)" },
221 [ST_F_LAST_AGT] = { .name = "last_agt", .desc = "Short description of the latest agent check report for this server (see also agent_desc)" },
222 [ST_F_QTIME] = { .name = "qtime", .desc = "Time spent in the queue, in milliseconds, averaged over the 1024 last requests (backend/server)" },
223 [ST_F_CTIME] = { .name = "ctime", .desc = "Time spent waiting for a connection to complete, in milliseconds, averaged over the 1024 last requests (backend/server)" },
224 [ST_F_RTIME] = { .name = "rtime", .desc = "Time spent waiting for a server response, in milliseconds, averaged over the 1024 last requests (backend/server)" },
225 [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)" },
226 [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" },
227 [ST_F_AGENT_CODE] = { .name = "agent_code", .desc = "Status code reported by the latest server agent check" },
228 [ST_F_AGENT_DURATION] = { .name = "agent_duration", .desc = "Total duration of the latest server agent check, in milliseconds" },
229 [ST_F_CHECK_DESC] = { .name = "check_desc", .desc = "Textual description of the latest health check report for this server" },
230 [ST_F_AGENT_DESC] = { .name = "agent_desc", .desc = "Textual description of the latest agent check report for this server" },
231 [ST_F_CHECK_RISE] = { .name = "check_rise", .desc = "Number of successful health checks before declaring a server UP (server 'rise' setting)" },
232 [ST_F_CHECK_FALL] = { .name = "check_fall", .desc = "Number of failed health checks before declaring a server DOWN (server 'fall' setting)" },
233 [ST_F_CHECK_HEALTH] = { .name = "check_health", .desc = "Current server health check level (0..fall-1=DOWN, fall..rise-1=UP)" },
234 [ST_F_AGENT_RISE] = { .name = "agent_rise", .desc = "Number of successful agent checks before declaring a server UP (server 'rise' setting)" },
235 [ST_F_AGENT_FALL] = { .name = "agent_fall", .desc = "Number of failed agent checks before declaring a server DOWN (server 'fall' setting)" },
236 [ST_F_AGENT_HEALTH] = { .name = "agent_health", .desc = "Current server agent check level (0..fall-1=DOWN, fall..rise-1=UP)" },
237 [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" },
238 [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" },
239 [ST_F_MODE] = { .name = "mode", .desc = "'mode' setting (tcp/http/health/cli)" },
240 [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" },
241 [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 +0100242 [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 +0200243 [ST_F_CONN_TOT] = { .name = "conn_tot", .desc = "Total number of new connections accepted on this frontend since the worker process started" },
244 [ST_F_INTERCEPTED] = { .name = "intercepted", .desc = "Total number of HTTP requests intercepted on the frontend (redirects/stats/services) since the worker process started" },
245 [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" },
246 [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" },
247 [ST_F_WREW] = { .name = "wrew", .desc = "Total number of failed HTTP header rewrites since the worker process started" },
248 [ST_F_CONNECT] = { .name = "connect", .desc = "Total number of outgoing connection attempts on this backend/server since the worker process started" },
249 [ST_F_REUSE] = { .name = "reuse", .desc = "Total number of reused connection on this backend/server since the worker process started" },
250 [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" },
251 [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" },
252 [ST_F_SRV_ICUR] = { .name = "srv_icur", .desc = "Current number of idle connections available for reuse on this server" },
253 [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 +0100254 [ST_F_QT_MAX] = { .name = "qtime_max", .desc = "Maximum observed time spent in the queue, in milliseconds (backend/server)" },
255 [ST_F_CT_MAX] = { .name = "ctime_max", .desc = "Maximum observed time spent waiting for a connection to complete, in milliseconds (backend/server)" },
256 [ST_F_RT_MAX] = { .name = "rtime_max", .desc = "Maximum observed time spent waiting for a server response, in milliseconds (backend/server)" },
257 [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 +0100258 [ST_F_EINT] = { .name = "eint", .desc = "Total number of internal errors since process started"},
Willy Tarreau3bb617c2020-06-29 13:51:05 +0200259 [ST_F_IDLE_CONN_CUR] = { .name = "idle_conn_cur", .desc = "Current number of unsafe idle connections"},
260 [ST_F_SAFE_CONN_CUR] = { .name = "safe_conn_cur", .desc = "Current number of safe idle connections"},
261 [ST_F_USED_CONN_CUR] = { .name = "used_conn_cur", .desc = "Current number of connections in use"},
Willy Tarreaua9fcecb2020-06-29 15:38:53 +0200262 [ST_F_NEED_CONN_EST] = { .name = "need_conn_est", .desc = "Estimated needed number of connections"},
Willy Tarreaubd715102020-10-23 22:44:30 +0200263 [ST_F_UWEIGHT] = { .name = "uweight", .desc = "Server's user weight, or sum of active servers' user weights for a backend" },
William Dauchy42d7c402021-11-07 10:18:47 +0100264 [ST_F_AGG_SRV_CHECK_STATUS] = { .name = "agg_server_check_status", .desc = "Backend's aggregated gauge of servers' state check status" },
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
Christopher Faulet908628c2022-03-25 16:43:49 +0100314static const char *stats_scope_ptr(struct appctx *appctx, struct conn_stream *cs)
Christopher Fauleted7a0662019-01-14 11:07:34 +0100315{
Christopher Faulet908628c2022-03-25 16:43:49 +0100316 struct channel *req = cs_oc(cs);
Christopher Fauletb7f88902019-07-15 21:56:43 +0200317 struct htx *htx = htxbuf(&req->buf);
318 struct htx_blk *blk;
319 struct ist uri;
Christopher Fauleted7a0662019-01-14 11:07:34 +0100320
Christopher Fauletb7f88902019-07-15 21:56:43 +0200321 blk = htx_get_head_blk(htx);
Christopher Fauletea009732019-11-18 15:50:25 +0100322 BUG_ON(!blk || htx_get_blk_type(blk) != HTX_BLK_REQ_SL);
Christopher Fauletb7f88902019-07-15 21:56:43 +0200323 ALREADY_CHECKED(blk);
324 uri = htx_sl_req_uri(htx_get_blk_ptr(htx, blk));
325 return uri.ptr + appctx->ctx.stats.scope_str;
Christopher Fauleted7a0662019-01-14 11:07:34 +0100326}
327
William Lallemand74c24fb2016-11-21 17:18:36 +0100328/*
329 * http_stats_io_handler()
330 * -> stats_dump_stat_to_buffer() // same as above, but used for CSV or HTML
331 * -> stats_dump_csv_header() // emits the CSV headers (same as above)
Simon Horman05ee2132017-01-04 09:37:25 +0100332 * -> stats_dump_json_header() // emits the JSON headers (same as above)
William Lallemand74c24fb2016-11-21 17:18:36 +0100333 * -> stats_dump_html_head() // emits the HTML headers
334 * -> stats_dump_html_info() // emits the equivalent of "show info" at the top
335 * -> stats_dump_proxy_to_buffer() // same as above, valid for CSV and HTML
336 * -> stats_dump_html_px_hdr()
337 * -> stats_dump_fe_stats()
338 * -> stats_dump_li_stats()
339 * -> stats_dump_sv_stats()
340 * -> stats_dump_be_stats()
341 * -> stats_dump_html_px_end()
342 * -> stats_dump_html_end() // emits HTML trailer
Simon Horman05ee2132017-01-04 09:37:25 +0100343 * -> stats_dump_json_end() // emits JSON trailer
William Lallemand74c24fb2016-11-21 17:18:36 +0100344 */
345
346
William Lallemand74c24fb2016-11-21 17:18:36 +0100347/* Dumps the stats CSV header to the trash buffer which. The caller is responsible
348 * for clearing it if needed.
349 * NOTE: Some tools happen to rely on the field position instead of its name,
350 * so please only append new fields at the end, never in the middle.
351 */
Amaury Denoyelle50660a82020-10-05 11:49:39 +0200352static void stats_dump_csv_header(enum stats_domain domain)
William Lallemand74c24fb2016-11-21 17:18:36 +0100353{
354 int field;
355
356 chunk_appendf(&trash, "# ");
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +0200357 if (stat_f[domain]) {
358 for (field = 0; field < stat_count[domain]; ++field) {
359 chunk_appendf(&trash, "%s,", stat_f[domain][field].name);
William Lallemand74c24fb2016-11-21 17:18:36 +0100360
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +0200361 /* print special delimiter on proxy stats to mark end of
362 static fields */
363 if (domain == STATS_DOMAIN_PROXY && field + 1 == ST_F_TOTAL_FIELDS)
364 chunk_appendf(&trash, "-,");
Amaury Denoyelle50660a82020-10-05 11:49:39 +0200365 }
366 }
367
William Lallemand74c24fb2016-11-21 17:18:36 +0100368 chunk_appendf(&trash, "\n");
369}
370
William Lallemand74c24fb2016-11-21 17:18:36 +0100371/* Emits a stats field without any surrounding element and properly encoded to
372 * resist CSV output. Returns non-zero on success, 0 if the buffer is full.
373 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200374int stats_emit_raw_data_field(struct buffer *out, const struct field *f)
William Lallemand74c24fb2016-11-21 17:18:36 +0100375{
376 switch (field_format(f, 0)) {
377 case FF_EMPTY: return 1;
378 case FF_S32: return chunk_appendf(out, "%d", f->u.s32);
379 case FF_U32: return chunk_appendf(out, "%u", f->u.u32);
380 case FF_S64: return chunk_appendf(out, "%lld", (long long)f->u.s64);
381 case FF_U64: return chunk_appendf(out, "%llu", (unsigned long long)f->u.u64);
Willy Tarreau065ba312021-05-08 10:38:20 +0200382 case FF_FLT: {
383 size_t prev_data = out->data;
384 out->data = flt_trim(out->area, prev_data, chunk_appendf(out, "%f", f->u.flt));
385 return out->data;
386 }
William Lallemand74c24fb2016-11-21 17:18:36 +0100387 case FF_STR: return csv_enc_append(field_str(f, 0), 1, out) != NULL;
388 default: return chunk_appendf(out, "[INCORRECT_FIELD_TYPE_%08x]", f->type);
389 }
390}
391
Amaury Denoyellee3f576c2020-10-05 11:49:44 +0200392const char *field_to_html_str(const struct field *f)
393{
394 switch (field_format(f, 0)) {
395 case FF_S32: return U2H(f->u.s32);
396 case FF_S64: return U2H(f->u.s64);
397 case FF_U64: return U2H(f->u.u64);
398 case FF_U32: return U2H(f->u.u32);
Willy Tarreau6004fb72021-05-08 07:37:38 +0200399 case FF_FLT: return F2H(f->u.flt);
Amaury Denoyellee3f576c2020-10-05 11:49:44 +0200400 case FF_STR: return field_str(f, 0);
401 case FF_EMPTY:
402 default:
403 return "";
404 }
405}
406
William Lallemand74c24fb2016-11-21 17:18:36 +0100407/* Emits a stats field prefixed with its type. No CSV encoding is prepared, the
408 * output is supposed to be used on its own line. Returns non-zero on success, 0
409 * if the buffer is full.
410 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200411int stats_emit_typed_data_field(struct buffer *out, const struct field *f)
William Lallemand74c24fb2016-11-21 17:18:36 +0100412{
413 switch (field_format(f, 0)) {
414 case FF_EMPTY: return 1;
415 case FF_S32: return chunk_appendf(out, "s32:%d", f->u.s32);
416 case FF_U32: return chunk_appendf(out, "u32:%u", f->u.u32);
417 case FF_S64: return chunk_appendf(out, "s64:%lld", (long long)f->u.s64);
418 case FF_U64: return chunk_appendf(out, "u64:%llu", (unsigned long long)f->u.u64);
Willy Tarreau065ba312021-05-08 10:38:20 +0200419 case FF_FLT: {
420 size_t prev_data = out->data;
421 out->data = flt_trim(out->area, prev_data, chunk_appendf(out, "flt:%f", f->u.flt));
422 return out->data;
423 }
William Lallemand74c24fb2016-11-21 17:18:36 +0100424 case FF_STR: return chunk_appendf(out, "str:%s", field_str(f, 0));
425 default: return chunk_appendf(out, "%08x:?", f->type);
426 }
427}
428
Simon Horman05ee2132017-01-04 09:37:25 +0100429/* Limit JSON integer values to the range [-(2**53)+1, (2**53)-1] as per
430 * the recommendation for interoperable integers in section 6 of RFC 7159.
431 */
432#define JSON_INT_MAX ((1ULL << 53) - 1)
433#define JSON_INT_MIN (0 - JSON_INT_MAX)
434
435/* Emits a stats field value and its type in JSON.
436 * Returns non-zero on success, 0 on error.
437 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200438int stats_emit_json_data_field(struct buffer *out, const struct field *f)
Simon Horman05ee2132017-01-04 09:37:25 +0100439{
440 int old_len;
441 char buf[20];
442 const char *type, *value = buf, *quote = "";
443
444 switch (field_format(f, 0)) {
445 case FF_EMPTY: return 1;
446 case FF_S32: type = "\"s32\"";
447 snprintf(buf, sizeof(buf), "%d", f->u.s32);
448 break;
449 case FF_U32: type = "\"u32\"";
450 snprintf(buf, sizeof(buf), "%u", f->u.u32);
451 break;
452 case FF_S64: type = "\"s64\"";
453 if (f->u.s64 < JSON_INT_MIN || f->u.s64 > JSON_INT_MAX)
454 return 0;
455 type = "\"s64\"";
456 snprintf(buf, sizeof(buf), "%lld", (long long)f->u.s64);
457 break;
458 case FF_U64: if (f->u.u64 > JSON_INT_MAX)
459 return 0;
460 type = "\"u64\"";
461 snprintf(buf, sizeof(buf), "%llu",
462 (unsigned long long) f->u.u64);
Christopher Faulet52c91bb2019-09-28 10:37:31 +0200463 break;
Christopher Faulet88a0db22019-09-24 16:35:10 +0200464 case FF_FLT: type = "\"flt\"";
Willy Tarreau065ba312021-05-08 10:38:20 +0200465 flt_trim(buf, 0, snprintf(buf, sizeof(buf), "%f", f->u.flt));
Simon Horman05ee2132017-01-04 09:37:25 +0100466 break;
467 case FF_STR: type = "\"str\"";
468 value = field_str(f, 0);
469 quote = "\"";
470 break;
471 default: snprintf(buf, sizeof(buf), "%u", f->type);
472 type = buf;
473 value = "unknown";
474 quote = "\"";
475 break;
476 }
477
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200478 old_len = out->data;
Simon Horman05ee2132017-01-04 09:37:25 +0100479 chunk_appendf(out, ",\"value\":{\"type\":%s,\"value\":%s%s%s}",
480 type, quote, value, quote);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200481 return !(old_len == out->data);
Simon Horman05ee2132017-01-04 09:37:25 +0100482}
483
William Lallemand74c24fb2016-11-21 17:18:36 +0100484/* Emits an encoding of the field type on 3 characters followed by a delimiter.
485 * Returns non-zero on success, 0 if the buffer is full.
486 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200487int stats_emit_field_tags(struct buffer *out, const struct field *f,
488 char delim)
William Lallemand74c24fb2016-11-21 17:18:36 +0100489{
490 char origin, nature, scope;
491
492 switch (field_origin(f, 0)) {
493 case FO_METRIC: origin = 'M'; break;
494 case FO_STATUS: origin = 'S'; break;
495 case FO_KEY: origin = 'K'; break;
496 case FO_CONFIG: origin = 'C'; break;
497 case FO_PRODUCT: origin = 'P'; break;
498 default: origin = '?'; break;
499 }
500
501 switch (field_nature(f, 0)) {
502 case FN_GAUGE: nature = 'G'; break;
503 case FN_LIMIT: nature = 'L'; break;
504 case FN_MIN: nature = 'm'; break;
505 case FN_MAX: nature = 'M'; break;
506 case FN_RATE: nature = 'R'; break;
507 case FN_COUNTER: nature = 'C'; break;
508 case FN_DURATION: nature = 'D'; break;
509 case FN_AGE: nature = 'A'; break;
510 case FN_TIME: nature = 'T'; break;
511 case FN_NAME: nature = 'N'; break;
512 case FN_OUTPUT: nature = 'O'; break;
513 case FN_AVG: nature = 'a'; break;
514 default: nature = '?'; break;
515 }
516
517 switch (field_scope(f, 0)) {
518 case FS_PROCESS: scope = 'P'; break;
519 case FS_SERVICE: scope = 'S'; break;
520 case FS_SYSTEM: scope = 's'; break;
521 case FS_CLUSTER: scope = 'C'; break;
522 default: scope = '?'; break;
523 }
524
525 return chunk_appendf(out, "%c%c%c%c", origin, nature, scope, delim);
526}
527
Simon Horman05ee2132017-01-04 09:37:25 +0100528/* Emits an encoding of the field type as JSON.
529 * Returns non-zero on success, 0 if the buffer is full.
530 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200531int stats_emit_json_field_tags(struct buffer *out, const struct field *f)
Simon Horman05ee2132017-01-04 09:37:25 +0100532{
533 const char *origin, *nature, *scope;
534 int old_len;
535
536 switch (field_origin(f, 0)) {
537 case FO_METRIC: origin = "Metric"; break;
538 case FO_STATUS: origin = "Status"; break;
539 case FO_KEY: origin = "Key"; break;
540 case FO_CONFIG: origin = "Config"; break;
541 case FO_PRODUCT: origin = "Product"; break;
542 default: origin = "Unknown"; break;
543 }
544
545 switch (field_nature(f, 0)) {
546 case FN_GAUGE: nature = "Gauge"; break;
547 case FN_LIMIT: nature = "Limit"; break;
548 case FN_MIN: nature = "Min"; break;
549 case FN_MAX: nature = "Max"; break;
550 case FN_RATE: nature = "Rate"; break;
551 case FN_COUNTER: nature = "Counter"; break;
552 case FN_DURATION: nature = "Duration"; break;
553 case FN_AGE: nature = "Age"; break;
554 case FN_TIME: nature = "Time"; break;
555 case FN_NAME: nature = "Name"; break;
556 case FN_OUTPUT: nature = "Output"; break;
557 case FN_AVG: nature = "Avg"; break;
558 default: nature = "Unknown"; break;
559 }
560
561 switch (field_scope(f, 0)) {
562 case FS_PROCESS: scope = "Process"; break;
563 case FS_SERVICE: scope = "Service"; break;
564 case FS_SYSTEM: scope = "System"; break;
565 case FS_CLUSTER: scope = "Cluster"; break;
566 default: scope = "Unknown"; break;
567 }
568
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200569 old_len = out->data;
Simon Horman05ee2132017-01-04 09:37:25 +0100570 chunk_appendf(out, "\"tags\":{"
571 "\"origin\":\"%s\","
572 "\"nature\":\"%s\","
573 "\"scope\":\"%s\""
574 "}", origin, nature, scope);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200575 return !(old_len == out->data);
Simon Horman05ee2132017-01-04 09:37:25 +0100576}
William Lallemand74c24fb2016-11-21 17:18:36 +0100577
578/* Dump all fields from <stats> into <out> using CSV format */
Willy Tarreau83061a82018-07-13 11:56:34 +0200579static int stats_dump_fields_csv(struct buffer *out,
Amaury Denoyelle97323c92020-10-02 18:32:01 +0200580 const struct field *stats, size_t stats_count,
Amaury Denoyelle50660a82020-10-05 11:49:39 +0200581 unsigned int flags,
582 enum stats_domain domain)
William Lallemand74c24fb2016-11-21 17:18:36 +0100583{
584 int field;
585
Amaury Denoyelle50660a82020-10-05 11:49:39 +0200586 for (field = 0; field < stats_count; ++field) {
William Lallemand74c24fb2016-11-21 17:18:36 +0100587 if (!stats_emit_raw_data_field(out, &stats[field]))
588 return 0;
589 if (!chunk_strcat(out, ","))
590 return 0;
Amaury Denoyelle50660a82020-10-05 11:49:39 +0200591
592 /* print special delimiter on proxy stats to mark end of
593 static fields */
594 if (domain == STATS_DOMAIN_PROXY && field + 1 == ST_F_TOTAL_FIELDS) {
595 if (!chunk_strcat(out, "-,"))
596 return 0;
597 }
William Lallemand74c24fb2016-11-21 17:18:36 +0100598 }
Amaury Denoyelle50660a82020-10-05 11:49:39 +0200599
William Lallemand74c24fb2016-11-21 17:18:36 +0100600 chunk_strcat(out, "\n");
601 return 1;
602}
603
604/* Dump all fields from <stats> into <out> using a typed "field:desc:type:value" format */
Willy Tarreau83061a82018-07-13 11:56:34 +0200605static int stats_dump_fields_typed(struct buffer *out,
Amaury Denoyelle97323c92020-10-02 18:32:01 +0200606 const struct field *stats,
607 size_t stats_count,
Amaury Denoyelle072f97e2020-10-05 11:49:37 +0200608 unsigned int flags,
609 enum stats_domain domain)
William Lallemand74c24fb2016-11-21 17:18:36 +0100610{
611 int field;
612
Amaury Denoyelle97323c92020-10-02 18:32:01 +0200613 for (field = 0; field < stats_count; ++field) {
William Lallemand74c24fb2016-11-21 17:18:36 +0100614 if (!stats[field].type)
615 continue;
616
Amaury Denoyelle072f97e2020-10-05 11:49:37 +0200617 switch (domain) {
618 case STATS_DOMAIN_PROXY:
619 chunk_appendf(out, "%c.%u.%u.%d.%s.%u:",
620 stats[ST_F_TYPE].u.u32 == STATS_TYPE_FE ? 'F' :
621 stats[ST_F_TYPE].u.u32 == STATS_TYPE_BE ? 'B' :
622 stats[ST_F_TYPE].u.u32 == STATS_TYPE_SO ? 'L' :
623 stats[ST_F_TYPE].u.u32 == STATS_TYPE_SV ? 'S' :
624 '?',
625 stats[ST_F_IID].u.u32, stats[ST_F_SID].u.u32,
626 field,
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +0200627 stat_f[domain][field].name,
Amaury Denoyelle072f97e2020-10-05 11:49:37 +0200628 stats[ST_F_PID].u.u32);
629 break;
630
Emeric Brunf8642ee2021-10-29 17:59:18 +0200631 case STATS_DOMAIN_RESOLVERS:
632 chunk_appendf(out, "N.%d.%s:", field,
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +0200633 stat_f[domain][field].name);
634 break;
635
Amaury Denoyelle072f97e2020-10-05 11:49:37 +0200636 default:
637 break;
638 }
William Lallemand74c24fb2016-11-21 17:18:36 +0100639
640 if (!stats_emit_field_tags(out, &stats[field], ':'))
641 return 0;
642 if (!stats_emit_typed_data_field(out, &stats[field]))
643 return 0;
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +0200644
Willy Tarreaufc8e4382021-06-17 07:22:27 +0200645 if (flags & STAT_SHOW_FDESC &&
646 !chunk_appendf(out, ":\"%s\"", stat_f[domain][field].desc)) {
Willy Tarreau6b19b142019-10-09 15:44:21 +0200647 return 0;
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +0200648 }
649
William Lallemand74c24fb2016-11-21 17:18:36 +0100650 if (!chunk_strcat(out, "\n"))
651 return 0;
652 }
653 return 1;
654}
655
Simon Horman05ee2132017-01-04 09:37:25 +0100656/* Dump all fields from <stats> into <out> using the "show info json" format */
Willy Tarreau83061a82018-07-13 11:56:34 +0200657static int stats_dump_json_info_fields(struct buffer *out,
Willy Tarreau43241ff2019-10-09 11:27:51 +0200658 const struct field *info, unsigned int flags)
Simon Horman05ee2132017-01-04 09:37:25 +0100659{
660 int field;
661 int started = 0;
662
663 if (!chunk_strcat(out, "["))
664 return 0;
665
666 for (field = 0; field < INF_TOTAL_FIELDS; field++) {
667 int old_len;
668
669 if (!field_format(info, field))
670 continue;
671
672 if (started && !chunk_strcat(out, ","))
673 goto err;
674 started = 1;
675
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200676 old_len = out->data;
Simon Horman05ee2132017-01-04 09:37:25 +0100677 chunk_appendf(out,
678 "{\"field\":{\"pos\":%d,\"name\":\"%s\"},"
679 "\"processNum\":%u,",
Willy Tarreaueaa55372019-10-09 07:39:11 +0200680 field, info_fields[field].name,
Simon Horman05ee2132017-01-04 09:37:25 +0100681 info[INF_PROCESS_NUM].u.u32);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200682 if (old_len == out->data)
Simon Horman05ee2132017-01-04 09:37:25 +0100683 goto err;
684
685 if (!stats_emit_json_field_tags(out, &info[field]))
686 goto err;
687
688 if (!stats_emit_json_data_field(out, &info[field]))
689 goto err;
690
691 if (!chunk_strcat(out, "}"))
692 goto err;
693 }
694
695 if (!chunk_strcat(out, "]"))
696 goto err;
697 return 1;
698
699err:
700 chunk_reset(out);
701 chunk_appendf(out, "{\"errorStr\":\"output buffer too short\"}");
702 return 0;
703}
704
Amaury Denoyellef34017b2020-10-02 18:32:03 +0200705static void stats_print_proxy_field_json(struct buffer *out,
706 const struct field *stat,
707 const char *name,
708 int pos,
709 uint32_t field_type,
710 uint32_t iid,
711 uint32_t sid,
712 uint32_t pid)
713{
714 const char *obj_type;
715 switch (field_type) {
716 case STATS_TYPE_FE: obj_type = "Frontend"; break;
717 case STATS_TYPE_BE: obj_type = "Backend"; break;
718 case STATS_TYPE_SO: obj_type = "Listener"; break;
719 case STATS_TYPE_SV: obj_type = "Server"; break;
720 default: obj_type = "Unknown"; break;
721 }
722
723 chunk_appendf(out,
724 "{"
725 "\"objType\":\"%s\","
726 "\"proxyId\":%u,"
727 "\"id\":%u,"
728 "\"field\":{\"pos\":%d,\"name\":\"%s\"},"
729 "\"processNum\":%u,",
730 obj_type, iid, sid, pos, name, pid);
731}
732
Emeric Brunf8642ee2021-10-29 17:59:18 +0200733static void stats_print_rslv_field_json(struct buffer *out,
734 const struct field *stat,
735 const char *name,
736 int pos)
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +0200737{
738 chunk_appendf(out,
739 "{"
740 "\"field\":{\"pos\":%d,\"name\":\"%s\"},",
741 pos, name);
742}
743
744
Simon Horman05ee2132017-01-04 09:37:25 +0100745/* Dump all fields from <stats> into <out> using a typed "field:desc:type:value" format */
Willy Tarreau83061a82018-07-13 11:56:34 +0200746static int stats_dump_fields_json(struct buffer *out,
Amaury Denoyelle97323c92020-10-02 18:32:01 +0200747 const struct field *stats, size_t stats_count,
Amaury Denoyelle072f97e2020-10-05 11:49:37 +0200748 unsigned int flags,
749 enum stats_domain domain)
Simon Horman05ee2132017-01-04 09:37:25 +0100750{
751 int field;
752 int started = 0;
753
Willy Tarreaub0ce3ad2019-10-09 11:19:29 +0200754 if ((flags & STAT_STARTED) && !chunk_strcat(out, ","))
Simon Horman05ee2132017-01-04 09:37:25 +0100755 return 0;
756 if (!chunk_strcat(out, "["))
757 return 0;
758
Amaury Denoyelle97323c92020-10-02 18:32:01 +0200759 for (field = 0; field < stats_count; field++) {
Simon Horman05ee2132017-01-04 09:37:25 +0100760 int old_len;
761
762 if (!stats[field].type)
763 continue;
764
765 if (started && !chunk_strcat(out, ","))
766 goto err;
767 started = 1;
768
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200769 old_len = out->data;
Amaury Denoyelle072f97e2020-10-05 11:49:37 +0200770 if (domain == STATS_DOMAIN_PROXY) {
771 stats_print_proxy_field_json(out, &stats[field],
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +0200772 stat_f[domain][field].name,
773 field,
Amaury Denoyelle072f97e2020-10-05 11:49:37 +0200774 stats[ST_F_TYPE].u.u32,
775 stats[ST_F_IID].u.u32,
776 stats[ST_F_SID].u.u32,
777 stats[ST_F_PID].u.u32);
Emeric Brunf8642ee2021-10-29 17:59:18 +0200778 } else if (domain == STATS_DOMAIN_RESOLVERS) {
779 stats_print_rslv_field_json(out, &stats[field],
780 stat_f[domain][field].name,
781 field);
Amaury Denoyelle072f97e2020-10-05 11:49:37 +0200782 }
Amaury Denoyellef34017b2020-10-02 18:32:03 +0200783
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200784 if (old_len == out->data)
Simon Horman05ee2132017-01-04 09:37:25 +0100785 goto err;
786
787 if (!stats_emit_json_field_tags(out, &stats[field]))
788 goto err;
789
790 if (!stats_emit_json_data_field(out, &stats[field]))
791 goto err;
792
793 if (!chunk_strcat(out, "}"))
794 goto err;
795 }
796
797 if (!chunk_strcat(out, "]"))
798 goto err;
799
800 return 1;
801
802err:
803 chunk_reset(out);
Willy Tarreaub0ce3ad2019-10-09 11:19:29 +0200804 if (flags & STAT_STARTED)
805 chunk_strcat(out, ",");
Simon Horman05ee2132017-01-04 09:37:25 +0100806 chunk_appendf(out, "{\"errorStr\":\"output buffer too short\"}");
807 return 0;
808}
809
William Lallemand74c24fb2016-11-21 17:18:36 +0100810/* Dump all fields from <stats> into <out> using the HTML format. A column is
Willy Tarreauab02b3f2019-10-09 11:11:46 +0200811 * reserved for the checkbox is STAT_ADMIN is set in <flags>. Some extra info
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +0200812 * are provided if STAT_SHLGNDS is present in <flags>. The statistics from
813 * extra modules are displayed at the end of the lines if STAT_SHMODULES is
814 * present in <flags>.
William Lallemand74c24fb2016-11-21 17:18:36 +0100815 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200816static int stats_dump_fields_html(struct buffer *out,
817 const struct field *stats,
818 unsigned int flags)
William Lallemand74c24fb2016-11-21 17:18:36 +0100819{
Willy Tarreau83061a82018-07-13 11:56:34 +0200820 struct buffer src;
Amaury Denoyellee3f576c2020-10-05 11:49:44 +0200821 struct stats_module *mod;
822 int i = 0, j = 0;
William Lallemand74c24fb2016-11-21 17:18:36 +0100823
824 if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_FE) {
825 chunk_appendf(out,
826 /* name, queue */
827 "<tr class=\"frontend\">");
828
Willy Tarreauab02b3f2019-10-09 11:11:46 +0200829 if (flags & STAT_ADMIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +0100830 /* Column sub-heading for Enable or Disable server */
831 chunk_appendf(out, "<td></td>");
832 }
833
834 chunk_appendf(out,
835 "<td class=ac>"
836 "<a name=\"%s/Frontend\"></a>"
837 "<a class=lfsb href=\"#%s/Frontend\">Frontend</a></td>"
838 "<td colspan=3></td>"
839 "",
840 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_PXNAME));
841
842 chunk_appendf(out,
843 /* sessions rate : current */
844 "<td><u>%s<div class=tips><table class=det>"
845 "<tr><th>Current connection rate:</th><td>%s/s</td></tr>"
846 "<tr><th>Current session rate:</th><td>%s/s</td></tr>"
847 "",
848 U2H(stats[ST_F_RATE].u.u32),
849 U2H(stats[ST_F_CONN_RATE].u.u32),
850 U2H(stats[ST_F_RATE].u.u32));
851
852 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
853 chunk_appendf(out,
854 "<tr><th>Current request rate:</th><td>%s/s</td></tr>",
855 U2H(stats[ST_F_REQ_RATE].u.u32));
856
857 chunk_appendf(out,
858 "</table></div></u></td>"
859 /* sessions rate : max */
860 "<td><u>%s<div class=tips><table class=det>"
861 "<tr><th>Max connection rate:</th><td>%s/s</td></tr>"
862 "<tr><th>Max session rate:</th><td>%s/s</td></tr>"
863 "",
864 U2H(stats[ST_F_RATE_MAX].u.u32),
865 U2H(stats[ST_F_CONN_RATE_MAX].u.u32),
866 U2H(stats[ST_F_RATE_MAX].u.u32));
867
868 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
869 chunk_appendf(out,
870 "<tr><th>Max request rate:</th><td>%s/s</td></tr>",
871 U2H(stats[ST_F_REQ_RATE_MAX].u.u32));
872
873 chunk_appendf(out,
874 "</table></div></u></td>"
875 /* sessions rate : limit */
876 "<td>%s</td>",
877 LIM2A(stats[ST_F_RATE_LIM].u.u32, "-"));
878
879 chunk_appendf(out,
880 /* sessions: current, max, limit, total */
881 "<td>%s</td><td>%s</td><td>%s</td>"
882 "<td><u>%s<div class=tips><table class=det>"
883 "<tr><th>Cum. connections:</th><td>%s</td></tr>"
884 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
885 "",
886 U2H(stats[ST_F_SCUR].u.u32), U2H(stats[ST_F_SMAX].u.u32), U2H(stats[ST_F_SLIM].u.u32),
887 U2H(stats[ST_F_STOT].u.u64),
888 U2H(stats[ST_F_CONN_TOT].u.u64),
889 U2H(stats[ST_F_STOT].u.u64));
890
891 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
892 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0) {
893 chunk_appendf(out,
894 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
895 "<tr><th>- HTTP 1xx responses:</th><td>%s</td></tr>"
896 "<tr><th>- HTTP 2xx responses:</th><td>%s</td></tr>"
897 "<tr><th>&nbsp;&nbsp;Compressed 2xx:</th><td>%s</td><td>(%d%%)</td></tr>"
898 "<tr><th>- HTTP 3xx responses:</th><td>%s</td></tr>"
899 "<tr><th>- HTTP 4xx responses:</th><td>%s</td></tr>"
900 "<tr><th>- HTTP 5xx responses:</th><td>%s</td></tr>"
901 "<tr><th>- other responses:</th><td>%s</td></tr>"
902 "<tr><th>Intercepted requests:</th><td>%s</td></tr>"
Willy Tarreaua1214a52018-12-14 14:00:25 +0100903 "<tr><th>Cache lookups:</th><td>%s</td></tr>"
904 "<tr><th>Cache hits:</th><td>%s</td><td>(%d%%)</td></tr>"
Willy Tarreau1b0f85e2018-05-28 15:12:40 +0200905 "<tr><th>Failed hdr rewrites:</th><td>%s</td></tr>"
Christopher Faulet0159ee42019-12-16 14:40:39 +0100906 "<tr><th>Internal errors:</th><td>%s</td></tr>"
William Lallemand74c24fb2016-11-21 17:18:36 +0100907 "",
908 U2H(stats[ST_F_REQ_TOT].u.u64),
909 U2H(stats[ST_F_HRSP_1XX].u.u64),
910 U2H(stats[ST_F_HRSP_2XX].u.u64),
911 U2H(stats[ST_F_COMP_RSP].u.u64),
912 stats[ST_F_HRSP_2XX].u.u64 ?
913 (int)(100 * stats[ST_F_COMP_RSP].u.u64 / stats[ST_F_HRSP_2XX].u.u64) : 0,
914 U2H(stats[ST_F_HRSP_3XX].u.u64),
915 U2H(stats[ST_F_HRSP_4XX].u.u64),
916 U2H(stats[ST_F_HRSP_5XX].u.u64),
917 U2H(stats[ST_F_HRSP_OTHER].u.u64),
Willy Tarreau1b0f85e2018-05-28 15:12:40 +0200918 U2H(stats[ST_F_INTERCEPTED].u.u64),
Willy Tarreaua1214a52018-12-14 14:00:25 +0100919 U2H(stats[ST_F_CACHE_LOOKUPS].u.u64),
920 U2H(stats[ST_F_CACHE_HITS].u.u64),
921 stats[ST_F_CACHE_LOOKUPS].u.u64 ?
922 (int)(100 * stats[ST_F_CACHE_HITS].u.u64 / stats[ST_F_CACHE_LOOKUPS].u.u64) : 0,
Christopher Faulet0159ee42019-12-16 14:40:39 +0100923 U2H(stats[ST_F_WREW].u.u64),
924 U2H(stats[ST_F_EINT].u.u64));
William Lallemand74c24fb2016-11-21 17:18:36 +0100925 }
926
927 chunk_appendf(out,
928 "</table></div></u></td>"
929 /* sessions: lbtot, lastsess */
930 "<td></td><td></td>"
931 /* bytes : in */
932 "<td>%s</td>"
933 "",
934 U2H(stats[ST_F_BIN].u.u64));
935
936 chunk_appendf(out,
937 /* bytes:out + compression stats (via hover): comp_in, comp_out, comp_byp */
938 "<td>%s%s<div class=tips><table class=det>"
939 "<tr><th>Response bytes in:</th><td>%s</td></tr>"
940 "<tr><th>Compression in:</th><td>%s</td></tr>"
941 "<tr><th>Compression out:</th><td>%s</td><td>(%d%%)</td></tr>"
942 "<tr><th>Compression bypass:</th><td>%s</td></tr>"
943 "<tr><th>Total bytes saved:</th><td>%s</td><td>(%d%%)</td></tr>"
944 "</table></div>%s</td>",
945 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "<u>":"",
946 U2H(stats[ST_F_BOUT].u.u64),
947 U2H(stats[ST_F_BOUT].u.u64),
948 U2H(stats[ST_F_COMP_IN].u.u64),
949 U2H(stats[ST_F_COMP_OUT].u.u64),
950 stats[ST_F_COMP_IN].u.u64 ? (int)(stats[ST_F_COMP_OUT].u.u64 * 100 / stats[ST_F_COMP_IN].u.u64) : 0,
951 U2H(stats[ST_F_COMP_BYP].u.u64),
952 U2H(stats[ST_F_COMP_IN].u.u64 - stats[ST_F_COMP_OUT].u.u64),
953 stats[ST_F_BOUT].u.u64 ? (int)((stats[ST_F_COMP_IN].u.u64 - stats[ST_F_COMP_OUT].u.u64) * 100 / stats[ST_F_BOUT].u.u64) : 0,
954 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "</u>":"");
955
956 chunk_appendf(out,
957 /* denied: req, resp */
958 "<td>%s</td><td>%s</td>"
959 /* errors : request, connect, response */
960 "<td>%s</td><td></td><td></td>"
961 /* warnings: retries, redispatches */
962 "<td></td><td></td>"
963 /* server status : reflect frontend status */
964 "<td class=ac>%s</td>"
965 /* rest of server: nothing */
Amaury Denoyellee3f576c2020-10-05 11:49:44 +0200966 "<td class=ac colspan=8></td>"
William Lallemand74c24fb2016-11-21 17:18:36 +0100967 "",
968 U2H(stats[ST_F_DREQ].u.u64), U2H(stats[ST_F_DRESP].u.u64),
969 U2H(stats[ST_F_EREQ].u.u64),
970 field_str(stats, ST_F_STATUS));
Amaury Denoyellee3f576c2020-10-05 11:49:44 +0200971
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +0200972 if (flags & STAT_SHMODULES) {
973 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
Amaury Denoyellee6ee8202020-11-03 15:04:46 +0100974 chunk_appendf(out, "<td>");
975
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +0200976 if (stats_px_get_cap(mod->domain_flags) & STATS_PX_CAP_FE) {
Amaury Denoyellee6ee8202020-11-03 15:04:46 +0100977 chunk_appendf(out,
978 "<u>%s<div class=tips><table class=det>",
979 mod->name);
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +0200980 for (j = 0; j < mod->stats_count; ++j) {
981 chunk_appendf(out,
982 "<tr><th>%s</th><td>%s</td></tr>",
983 mod->stats[j].desc, field_to_html_str(&stats[ST_F_TOTAL_FIELDS + i]));
984 ++i;
985 }
Amaury Denoyellee6ee8202020-11-03 15:04:46 +0100986 chunk_appendf(out, "</table></div></u>");
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +0200987 } else {
988 i += mod->stats_count;
Amaury Denoyellee3f576c2020-10-05 11:49:44 +0200989 }
Amaury Denoyellee3f576c2020-10-05 11:49:44 +0200990
Amaury Denoyellee6ee8202020-11-03 15:04:46 +0100991 chunk_appendf(out, "</td>");
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +0200992 }
Amaury Denoyellee3f576c2020-10-05 11:49:44 +0200993 }
994
995 chunk_appendf(out, "</tr>");
William Lallemand74c24fb2016-11-21 17:18:36 +0100996 }
997 else if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_SO) {
998 chunk_appendf(out, "<tr class=socket>");
Willy Tarreauab02b3f2019-10-09 11:11:46 +0200999 if (flags & STAT_ADMIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001000 /* Column sub-heading for Enable or Disable server */
1001 chunk_appendf(out, "<td></td>");
1002 }
1003
1004 chunk_appendf(out,
1005 /* frontend name, listener name */
1006 "<td class=ac><a name=\"%s/+%s\"></a>%s"
1007 "<a class=lfsb href=\"#%s/+%s\">%s</a>"
1008 "",
1009 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME),
Willy Tarreau708c4162019-10-09 10:19:16 +02001010 (flags & STAT_SHLGNDS)?"<u>":"",
William Lallemand74c24fb2016-11-21 17:18:36 +01001011 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME), field_str(stats, ST_F_SVNAME));
1012
Willy Tarreau708c4162019-10-09 10:19:16 +02001013 if (flags & STAT_SHLGNDS) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001014 chunk_appendf(out, "<div class=tips>");
1015
Willy Tarreau90807112020-02-25 08:16:33 +01001016 if (isdigit((unsigned char)*field_str(stats, ST_F_ADDR)))
William Lallemand74c24fb2016-11-21 17:18:36 +01001017 chunk_appendf(out, "IPv4: %s, ", field_str(stats, ST_F_ADDR));
1018 else if (*field_str(stats, ST_F_ADDR) == '[')
1019 chunk_appendf(out, "IPv6: %s, ", field_str(stats, ST_F_ADDR));
1020 else if (*field_str(stats, ST_F_ADDR))
1021 chunk_appendf(out, "%s, ", field_str(stats, ST_F_ADDR));
1022
1023 /* id */
1024 chunk_appendf(out, "id: %d</div>", stats[ST_F_SID].u.u32);
1025 }
1026
1027 chunk_appendf(out,
1028 /* queue */
1029 "%s</td><td colspan=3></td>"
1030 /* sessions rate: current, max, limit */
1031 "<td colspan=3>&nbsp;</td>"
1032 /* sessions: current, max, limit, total, lbtot, lastsess */
1033 "<td>%s</td><td>%s</td><td>%s</td>"
1034 "<td>%s</td><td>&nbsp;</td><td>&nbsp;</td>"
1035 /* bytes: in, out */
1036 "<td>%s</td><td>%s</td>"
1037 "",
Willy Tarreau708c4162019-10-09 10:19:16 +02001038 (flags & STAT_SHLGNDS)?"</u>":"",
William Lallemand74c24fb2016-11-21 17:18:36 +01001039 U2H(stats[ST_F_SCUR].u.u32), U2H(stats[ST_F_SMAX].u.u32), U2H(stats[ST_F_SLIM].u.u32),
1040 U2H(stats[ST_F_STOT].u.u64), U2H(stats[ST_F_BIN].u.u64), U2H(stats[ST_F_BOUT].u.u64));
1041
1042 chunk_appendf(out,
1043 /* denied: req, resp */
1044 "<td>%s</td><td>%s</td>"
1045 /* errors: request, connect, response */
1046 "<td>%s</td><td></td><td></td>"
1047 /* warnings: retries, redispatches */
1048 "<td></td><td></td>"
1049 /* server status: reflect listener status */
1050 "<td class=ac>%s</td>"
1051 /* rest of server: nothing */
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001052 "<td class=ac colspan=8></td>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001053 "",
1054 U2H(stats[ST_F_DREQ].u.u64), U2H(stats[ST_F_DRESP].u.u64),
1055 U2H(stats[ST_F_EREQ].u.u64),
1056 field_str(stats, ST_F_STATUS));
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001057
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001058 if (flags & STAT_SHMODULES) {
1059 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001060 chunk_appendf(out, "<td>");
1061
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001062 if (stats_px_get_cap(mod->domain_flags) & STATS_PX_CAP_LI) {
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001063 chunk_appendf(out,
1064 "<u>%s<div class=tips><table class=det>",
1065 mod->name);
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001066 for (j = 0; j < mod->stats_count; ++j) {
1067 chunk_appendf(out,
1068 "<tr><th>%s</th><td>%s</td></tr>",
1069 mod->stats[j].desc, field_to_html_str(&stats[ST_F_TOTAL_FIELDS + i]));
1070 ++i;
1071 }
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001072 chunk_appendf(out, "</table></div></u>");
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001073 } else {
1074 i += mod->stats_count;
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001075 }
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001076
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001077 chunk_appendf(out, "</td>");
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001078 }
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001079 }
1080
1081 chunk_appendf(out, "</tr>");
William Lallemand74c24fb2016-11-21 17:18:36 +01001082 }
1083 else if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_SV) {
1084 const char *style;
1085
1086 /* determine the style to use depending on the server's state,
1087 * its health and weight. There isn't a 1-to-1 mapping between
1088 * state and styles for the cases where the server is (still)
1089 * up. The reason is that we don't want to report nolb and
1090 * drain with the same color.
1091 */
1092
1093 if (strcmp(field_str(stats, ST_F_STATUS), "DOWN") == 0 ||
1094 strcmp(field_str(stats, ST_F_STATUS), "DOWN (agent)") == 0) {
1095 style = "down";
1096 }
Florian Apolloner39272c22021-03-30 13:28:35 +02001097 else if (strncmp(field_str(stats, ST_F_STATUS), "DOWN ", strlen("DOWN ")) == 0) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001098 style = "going_up";
1099 }
Daniel Corbettb4285172020-03-28 12:35:50 -04001100 else if (strcmp(field_str(stats, ST_F_STATUS), "DRAIN") == 0) {
1101 style = "draining";
1102 }
Florian Apolloner39272c22021-03-30 13:28:35 +02001103 else if (strncmp(field_str(stats, ST_F_STATUS), "NOLB ", strlen("NOLB ")) == 0) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001104 style = "going_down";
1105 }
1106 else if (strcmp(field_str(stats, ST_F_STATUS), "NOLB") == 0) {
1107 style = "nolb";
1108 }
1109 else if (strcmp(field_str(stats, ST_F_STATUS), "no check") == 0) {
1110 style = "no_check";
1111 }
1112 else if (!stats[ST_F_CHKFAIL].type ||
1113 stats[ST_F_CHECK_HEALTH].u.u32 == stats[ST_F_CHECK_RISE].u.u32 + stats[ST_F_CHECK_FALL].u.u32 - 1) {
1114 /* no check or max health = UP */
1115 if (stats[ST_F_WEIGHT].u.u32)
1116 style = "up";
1117 else
1118 style = "draining";
1119 }
1120 else {
1121 style = "going_down";
1122 }
1123
Willy Tarreau7b524852020-08-11 10:26:36 +02001124 if (strncmp(field_str(stats, ST_F_STATUS), "MAINT", 5) == 0)
William Lallemand74c24fb2016-11-21 17:18:36 +01001125 chunk_appendf(out, "<tr class=\"maintain\">");
1126 else
1127 chunk_appendf(out,
1128 "<tr class=\"%s_%s\">",
1129 (stats[ST_F_BCK].u.u32) ? "backup" : "active", style);
1130
1131
Willy Tarreauab02b3f2019-10-09 11:11:46 +02001132 if (flags & STAT_ADMIN)
William Lallemand74c24fb2016-11-21 17:18:36 +01001133 chunk_appendf(out,
David Harrigand3db35a2016-12-30 12:12:49 +00001134 "<td><input class='%s-checkbox' type=\"checkbox\" name=\"s\" value=\"%s\"></td>",
1135 field_str(stats, ST_F_PXNAME),
William Lallemand74c24fb2016-11-21 17:18:36 +01001136 field_str(stats, ST_F_SVNAME));
1137
1138 chunk_appendf(out,
1139 "<td class=ac><a name=\"%s/%s\"></a>%s"
1140 "<a class=lfsb href=\"#%s/%s\">%s</a>"
1141 "",
1142 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME),
Willy Tarreau708c4162019-10-09 10:19:16 +02001143 (flags & STAT_SHLGNDS) ? "<u>" : "",
William Lallemand74c24fb2016-11-21 17:18:36 +01001144 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME), field_str(stats, ST_F_SVNAME));
1145
Willy Tarreau708c4162019-10-09 10:19:16 +02001146 if (flags & STAT_SHLGNDS) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001147 chunk_appendf(out, "<div class=tips>");
1148
Willy Tarreau90807112020-02-25 08:16:33 +01001149 if (isdigit((unsigned char)*field_str(stats, ST_F_ADDR)))
William Lallemand74c24fb2016-11-21 17:18:36 +01001150 chunk_appendf(out, "IPv4: %s, ", field_str(stats, ST_F_ADDR));
1151 else if (*field_str(stats, ST_F_ADDR) == '[')
1152 chunk_appendf(out, "IPv6: %s, ", field_str(stats, ST_F_ADDR));
1153 else if (*field_str(stats, ST_F_ADDR))
1154 chunk_appendf(out, "%s, ", field_str(stats, ST_F_ADDR));
1155
1156 /* id */
1157 chunk_appendf(out, "id: %d", stats[ST_F_SID].u.u32);
1158
1159 /* cookie */
1160 if (stats[ST_F_COOKIE].type) {
1161 chunk_appendf(out, ", cookie: '");
1162 chunk_initstr(&src, field_str(stats, ST_F_COOKIE));
1163 chunk_htmlencode(out, &src);
1164 chunk_appendf(out, "'");
1165 }
1166
1167 chunk_appendf(out, "</div>");
1168 }
1169
1170 chunk_appendf(out,
1171 /* queue : current, max, limit */
1172 "%s</td><td>%s</td><td>%s</td><td>%s</td>"
1173 /* sessions rate : current, max, limit */
1174 "<td>%s</td><td>%s</td><td></td>"
1175 "",
Willy Tarreau708c4162019-10-09 10:19:16 +02001176 (flags & STAT_SHLGNDS) ? "</u>" : "",
William Lallemand74c24fb2016-11-21 17:18:36 +01001177 U2H(stats[ST_F_QCUR].u.u32), U2H(stats[ST_F_QMAX].u.u32), LIM2A(stats[ST_F_QLIMIT].u.u32, "-"),
1178 U2H(stats[ST_F_RATE].u.u32), U2H(stats[ST_F_RATE_MAX].u.u32));
1179
1180 chunk_appendf(out,
1181 /* sessions: current, max, limit, total */
Willy Tarreauf21d17b2019-09-08 09:24:56 +02001182 "<td><u>%s<div class=tips>"
1183 "<table class=det>"
1184 "<tr><th>Current active connections:</th><td>%s</td></tr>"
Willy Tarreau3bb617c2020-06-29 13:51:05 +02001185 "<tr><th>Current used connections:</th><td>%s</td></tr>"
Willy Tarreauf21d17b2019-09-08 09:24:56 +02001186 "<tr><th>Current idle connections:</th><td>%s</td></tr>"
Willy Tarreau3bb617c2020-06-29 13:51:05 +02001187 "<tr><th>- unsafe:</th><td>%s</td></tr>"
1188 "<tr><th>- safe:</th><td>%s</td></tr>"
Willy Tarreaua9fcecb2020-06-29 15:38:53 +02001189 "<tr><th>Estimated need of connections:</th><td>%s</td></tr>"
Willy Tarreauf21d17b2019-09-08 09:24:56 +02001190 "<tr><th>Active connections limit:</th><td>%s</td></tr>"
1191 "<tr><th>Idle connections limit:</th><td>%s</td></tr>"
1192 "</table></div></u>"
1193 "</td><td>%s</td><td>%s</td>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001194 "<td><u>%s<div class=tips><table class=det>"
1195 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
1196 "",
Willy Tarreauf21d17b2019-09-08 09:24:56 +02001197 U2H(stats[ST_F_SCUR].u.u32),
Willy Tarreau3bb617c2020-06-29 13:51:05 +02001198 U2H(stats[ST_F_SCUR].u.u32),
1199 U2H(stats[ST_F_USED_CONN_CUR].u.u32),
1200 U2H(stats[ST_F_SRV_ICUR].u.u32),
1201 U2H(stats[ST_F_IDLE_CONN_CUR].u.u32),
1202 U2H(stats[ST_F_SAFE_CONN_CUR].u.u32),
Willy Tarreaua9fcecb2020-06-29 15:38:53 +02001203 U2H(stats[ST_F_NEED_CONN_EST].u.u32),
Willy Tarreau3bb617c2020-06-29 13:51:05 +02001204
Willy Tarreauf21d17b2019-09-08 09:24:56 +02001205 LIM2A(stats[ST_F_SLIM].u.u32, "-"),
1206 stats[ST_F_SRV_ILIM].type ? U2H(stats[ST_F_SRV_ILIM].u.u32) : "-",
1207 U2H(stats[ST_F_SMAX].u.u32), LIM2A(stats[ST_F_SLIM].u.u32, "-"),
William Lallemand74c24fb2016-11-21 17:18:36 +01001208 U2H(stats[ST_F_STOT].u.u64),
1209 U2H(stats[ST_F_STOT].u.u64));
1210
1211 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
1212 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001213 chunk_appendf(out,
Willy Tarreauf1573842018-12-14 11:35:36 +01001214 "<tr><th>New connections:</th><td>%s</td></tr>"
1215 "<tr><th>Reused connections:</th><td>%s</td><td>(%d%%)</td></tr>"
Marcin Deranek3c27dda2020-05-15 18:32:51 +02001216 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001217 "<tr><th>- HTTP 1xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
1218 "<tr><th>- HTTP 2xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
1219 "<tr><th>- HTTP 3xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
1220 "<tr><th>- HTTP 4xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
1221 "<tr><th>- HTTP 5xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
1222 "<tr><th>- other responses:</th><td>%s</td><td>(%d%%)</td></tr>"
Willy Tarreau1b0f85e2018-05-28 15:12:40 +02001223 "<tr><th>Failed hdr rewrites:</th><td>%s</td></tr>"
Christopher Faulet0159ee42019-12-16 14:40:39 +01001224 "<tr><th>Internal error:</th><td>%s</td></tr>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001225 "",
Willy Tarreauf1573842018-12-14 11:35:36 +01001226 U2H(stats[ST_F_CONNECT].u.u64),
1227 U2H(stats[ST_F_REUSE].u.u64),
1228 (stats[ST_F_CONNECT].u.u64 + stats[ST_F_REUSE].u.u64) ?
1229 (int)(100 * stats[ST_F_REUSE].u.u64 / (stats[ST_F_CONNECT].u.u64 + stats[ST_F_REUSE].u.u64)) : 0,
Marcin Deranek3c27dda2020-05-15 18:32:51 +02001230 U2H(stats[ST_F_REQ_TOT].u.u64),
1231 U2H(stats[ST_F_HRSP_1XX].u.u64), stats[ST_F_REQ_TOT].u.u64 ?
1232 (int)(100 * stats[ST_F_HRSP_1XX].u.u64 / stats[ST_F_REQ_TOT].u.u64) : 0,
1233 U2H(stats[ST_F_HRSP_2XX].u.u64), stats[ST_F_REQ_TOT].u.u64 ?
1234 (int)(100 * stats[ST_F_HRSP_2XX].u.u64 / stats[ST_F_REQ_TOT].u.u64) : 0,
1235 U2H(stats[ST_F_HRSP_3XX].u.u64), stats[ST_F_REQ_TOT].u.u64 ?
1236 (int)(100 * stats[ST_F_HRSP_3XX].u.u64 / stats[ST_F_REQ_TOT].u.u64) : 0,
1237 U2H(stats[ST_F_HRSP_4XX].u.u64), stats[ST_F_REQ_TOT].u.u64 ?
1238 (int)(100 * stats[ST_F_HRSP_4XX].u.u64 / stats[ST_F_REQ_TOT].u.u64) : 0,
1239 U2H(stats[ST_F_HRSP_5XX].u.u64), stats[ST_F_REQ_TOT].u.u64 ?
1240 (int)(100 * stats[ST_F_HRSP_5XX].u.u64 / stats[ST_F_REQ_TOT].u.u64) : 0,
1241 U2H(stats[ST_F_HRSP_OTHER].u.u64), stats[ST_F_REQ_TOT].u.u64 ?
1242 (int)(100 * stats[ST_F_HRSP_OTHER].u.u64 / stats[ST_F_REQ_TOT].u.u64) : 0,
Christopher Faulet0159ee42019-12-16 14:40:39 +01001243 U2H(stats[ST_F_WREW].u.u64),
1244 U2H(stats[ST_F_EINT].u.u64));
William Lallemand74c24fb2016-11-21 17:18:36 +01001245 }
1246
Christopher Faulet0d1c2a62019-11-08 14:59:51 +01001247 chunk_appendf(out, "<tr><th colspan=3>Max / Avg over last 1024 success. conn.</th></tr>");
1248 chunk_appendf(out, "<tr><th>- Queue time:</th><td>%s / %s</td><td>ms</td></tr>",
1249 U2H(stats[ST_F_QT_MAX].u.u32), U2H(stats[ST_F_QTIME].u.u32));
1250 chunk_appendf(out, "<tr><th>- Connect time:</th><td>%s / %s</td><td>ms</td></tr>",
1251 U2H(stats[ST_F_CT_MAX].u.u32), U2H(stats[ST_F_CTIME].u.u32));
William Lallemand74c24fb2016-11-21 17:18:36 +01001252 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
Christopher Faulet0d1c2a62019-11-08 14:59:51 +01001253 chunk_appendf(out, "<tr><th>- Responses time:</th><td>%s / %s</td><td>ms</td></tr>",
1254 U2H(stats[ST_F_RT_MAX].u.u32), U2H(stats[ST_F_RTIME].u.u32));
1255 chunk_appendf(out, "<tr><th>- Total time:</th><td>%s / %s</td><td>ms</td></tr>",
1256 U2H(stats[ST_F_TT_MAX].u.u32), U2H(stats[ST_F_TTIME].u.u32));
William Lallemand74c24fb2016-11-21 17:18:36 +01001257
1258 chunk_appendf(out,
1259 "</table></div></u></td>"
1260 /* sessions: lbtot, last */
1261 "<td>%s</td><td>%s</td>",
1262 U2H(stats[ST_F_LBTOT].u.u64),
1263 human_time(stats[ST_F_LASTSESS].u.s32, 1));
1264
1265 chunk_appendf(out,
1266 /* bytes : in, out */
1267 "<td>%s</td><td>%s</td>"
1268 /* denied: req, resp */
1269 "<td></td><td>%s</td>"
1270 /* errors : request, connect */
1271 "<td></td><td>%s</td>"
1272 /* errors : response */
1273 "<td><u>%s<div class=tips>Connection resets during transfers: %lld client, %lld server</div></u></td>"
1274 /* warnings: retries, redispatches */
1275 "<td>%lld</td><td>%lld</td>"
1276 "",
1277 U2H(stats[ST_F_BIN].u.u64), U2H(stats[ST_F_BOUT].u.u64),
1278 U2H(stats[ST_F_DRESP].u.u64),
1279 U2H(stats[ST_F_ECON].u.u64),
1280 U2H(stats[ST_F_ERESP].u.u64),
1281 (long long)stats[ST_F_CLI_ABRT].u.u64,
1282 (long long)stats[ST_F_SRV_ABRT].u.u64,
1283 (long long)stats[ST_F_WRETR].u.u64,
1284 (long long)stats[ST_F_WREDIS].u.u64);
1285
1286 /* status, last change */
1287 chunk_appendf(out, "<td class=ac>");
1288
1289 /* FIXME!!!!
1290 * LASTCHG should contain the last change for *this* server and must be computed
1291 * properly above, as was done below, ie: this server if maint, otherwise ref server
1292 * if tracking. Note that ref is either local or remote depending on tracking.
1293 */
1294
1295
Willy Tarreau7b524852020-08-11 10:26:36 +02001296 if (strncmp(field_str(stats, ST_F_STATUS), "MAINT", 5) == 0) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001297 chunk_appendf(out, "%s MAINT", human_time(stats[ST_F_LASTCHG].u.u32, 1));
1298 }
Willy Tarreau7b524852020-08-11 10:26:36 +02001299 else if (strcmp(field_str(stats, ST_F_STATUS), "no check") == 0) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001300 chunk_strcat(out, "<i>no check</i>");
1301 }
1302 else {
1303 chunk_appendf(out, "%s %s", human_time(stats[ST_F_LASTCHG].u.u32, 1), field_str(stats, ST_F_STATUS));
Willy Tarreau7b524852020-08-11 10:26:36 +02001304 if (strncmp(field_str(stats, ST_F_STATUS), "DOWN", 4) == 0) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001305 if (stats[ST_F_CHECK_HEALTH].u.u32)
1306 chunk_strcat(out, " &uarr;");
1307 }
1308 else if (stats[ST_F_CHECK_HEALTH].u.u32 < stats[ST_F_CHECK_RISE].u.u32 + stats[ST_F_CHECK_FALL].u.u32 - 1)
1309 chunk_strcat(out, " &darr;");
1310 }
1311
Willy Tarreau7b524852020-08-11 10:26:36 +02001312 if (strncmp(field_str(stats, ST_F_STATUS), "DOWN", 4) == 0 &&
William Lallemand74c24fb2016-11-21 17:18:36 +01001313 stats[ST_F_AGENT_STATUS].type && !stats[ST_F_AGENT_HEALTH].u.u32) {
1314 chunk_appendf(out,
1315 "</td><td class=ac><u> %s",
1316 field_str(stats, ST_F_AGENT_STATUS));
1317
1318 if (stats[ST_F_AGENT_CODE].type)
1319 chunk_appendf(out, "/%d", stats[ST_F_AGENT_CODE].u.u32);
1320
1321 if (stats[ST_F_AGENT_DURATION].type)
1322 chunk_appendf(out, " in %lums", (long)stats[ST_F_AGENT_DURATION].u.u64);
1323
1324 chunk_appendf(out, "<div class=tips>%s", field_str(stats, ST_F_AGENT_DESC));
1325
1326 if (*field_str(stats, ST_F_LAST_AGT)) {
1327 chunk_appendf(out, ": ");
1328 chunk_initstr(&src, field_str(stats, ST_F_LAST_AGT));
1329 chunk_htmlencode(out, &src);
1330 }
1331 chunk_appendf(out, "</div></u>");
1332 }
1333 else if (stats[ST_F_CHECK_STATUS].type) {
1334 chunk_appendf(out,
1335 "</td><td class=ac><u> %s",
1336 field_str(stats, ST_F_CHECK_STATUS));
1337
1338 if (stats[ST_F_CHECK_CODE].type)
1339 chunk_appendf(out, "/%d", stats[ST_F_CHECK_CODE].u.u32);
1340
1341 if (stats[ST_F_CHECK_DURATION].type)
1342 chunk_appendf(out, " in %lums", (long)stats[ST_F_CHECK_DURATION].u.u64);
1343
1344 chunk_appendf(out, "<div class=tips>%s", field_str(stats, ST_F_CHECK_DESC));
1345
1346 if (*field_str(stats, ST_F_LAST_CHK)) {
1347 chunk_appendf(out, ": ");
1348 chunk_initstr(&src, field_str(stats, ST_F_LAST_CHK));
1349 chunk_htmlencode(out, &src);
1350 }
1351 chunk_appendf(out, "</div></u>");
1352 }
1353 else
1354 chunk_appendf(out, "</td><td>");
1355
1356 chunk_appendf(out,
Willy Tarreaubd715102020-10-23 22:44:30 +02001357 /* weight / uweight */
1358 "</td><td class=ac>%d/%d</td>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001359 /* act, bck */
1360 "<td class=ac>%s</td><td class=ac>%s</td>"
1361 "",
Willy Tarreaubd715102020-10-23 22:44:30 +02001362 stats[ST_F_WEIGHT].u.u32, stats[ST_F_UWEIGHT].u.u32,
William Lallemand74c24fb2016-11-21 17:18:36 +01001363 stats[ST_F_BCK].u.u32 ? "-" : "Y",
1364 stats[ST_F_BCK].u.u32 ? "Y" : "-");
1365
1366 /* check failures: unique, fatal, down time */
1367 if (strcmp(field_str(stats, ST_F_STATUS), "MAINT (resolution)") == 0) {
1368 chunk_appendf(out, "<td class=ac colspan=3>resolution</td>");
1369 }
1370 else if (stats[ST_F_CHKFAIL].type) {
1371 chunk_appendf(out, "<td><u>%lld", (long long)stats[ST_F_CHKFAIL].u.u64);
1372
1373 if (stats[ST_F_HANAFAIL].type)
1374 chunk_appendf(out, "/%lld", (long long)stats[ST_F_HANAFAIL].u.u64);
1375
1376 chunk_appendf(out,
1377 "<div class=tips>Failed Health Checks%s</div></u></td>"
1378 "<td>%lld</td><td>%s</td>"
1379 "",
1380 stats[ST_F_HANAFAIL].type ? "/Health Analyses" : "",
1381 (long long)stats[ST_F_CHKDOWN].u.u64, human_time(stats[ST_F_DOWNTIME].u.u32, 1));
1382 }
1383 else if (strcmp(field_str(stats, ST_F_STATUS), "MAINT") != 0 && field_format(stats, ST_F_TRACKED) == FF_STR) {
1384 /* tracking a server (hence inherited maint would appear as "MAINT (via...)" */
1385 chunk_appendf(out,
1386 "<td class=ac colspan=3><a class=lfsb href=\"#%s\">via %s</a></td>",
1387 field_str(stats, ST_F_TRACKED), field_str(stats, ST_F_TRACKED));
1388 }
1389 else
1390 chunk_appendf(out, "<td colspan=3></td>");
1391
1392 /* throttle */
1393 if (stats[ST_F_THROTTLE].type)
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001394 chunk_appendf(out, "<td class=ac>%d %%</td>\n", stats[ST_F_THROTTLE].u.u32);
William Lallemand74c24fb2016-11-21 17:18:36 +01001395 else
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001396 chunk_appendf(out, "<td class=ac>-</td>");
1397
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001398 if (flags & STAT_SHMODULES) {
1399 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001400 chunk_appendf(out, "<td>");
1401
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001402 if (stats_px_get_cap(mod->domain_flags) & STATS_PX_CAP_SRV) {
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001403 chunk_appendf(out,
1404 "<u>%s<div class=tips><table class=det>",
1405 mod->name);
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001406 for (j = 0; j < mod->stats_count; ++j) {
1407 chunk_appendf(out,
1408 "<tr><th>%s</th><td>%s</td></tr>",
1409 mod->stats[j].desc, field_to_html_str(&stats[ST_F_TOTAL_FIELDS + i]));
1410 ++i;
1411 }
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001412 chunk_appendf(out, "</table></div></u>");
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001413 } else {
1414 i += mod->stats_count;
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001415 }
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001416
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001417 chunk_appendf(out, "</td>");
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001418 }
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001419 }
1420
1421 chunk_appendf(out, "</tr>\n");
William Lallemand74c24fb2016-11-21 17:18:36 +01001422 }
1423 else if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_BE) {
1424 chunk_appendf(out, "<tr class=\"backend\">");
Willy Tarreauab02b3f2019-10-09 11:11:46 +02001425 if (flags & STAT_ADMIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001426 /* Column sub-heading for Enable or Disable server */
1427 chunk_appendf(out, "<td></td>");
1428 }
1429 chunk_appendf(out,
1430 "<td class=ac>"
1431 /* name */
1432 "%s<a name=\"%s/Backend\"></a>"
1433 "<a class=lfsb href=\"#%s/Backend\">Backend</a>"
1434 "",
Willy Tarreau708c4162019-10-09 10:19:16 +02001435 (flags & STAT_SHLGNDS)?"<u>":"",
William Lallemand74c24fb2016-11-21 17:18:36 +01001436 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_PXNAME));
1437
Willy Tarreau708c4162019-10-09 10:19:16 +02001438 if (flags & STAT_SHLGNDS) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001439 /* balancing */
1440 chunk_appendf(out, "<div class=tips>balancing: %s",
1441 field_str(stats, ST_F_ALGO));
1442
1443 /* cookie */
1444 if (stats[ST_F_COOKIE].type) {
1445 chunk_appendf(out, ", cookie: '");
1446 chunk_initstr(&src, field_str(stats, ST_F_COOKIE));
1447 chunk_htmlencode(out, &src);
1448 chunk_appendf(out, "'");
1449 }
1450 chunk_appendf(out, "</div>");
1451 }
1452
1453 chunk_appendf(out,
1454 "%s</td>"
1455 /* queue : current, max */
1456 "<td>%s</td><td>%s</td><td></td>"
1457 /* sessions rate : current, max, limit */
1458 "<td>%s</td><td>%s</td><td></td>"
1459 "",
Willy Tarreau708c4162019-10-09 10:19:16 +02001460 (flags & STAT_SHLGNDS)?"</u>":"",
William Lallemand74c24fb2016-11-21 17:18:36 +01001461 U2H(stats[ST_F_QCUR].u.u32), U2H(stats[ST_F_QMAX].u.u32),
1462 U2H(stats[ST_F_RATE].u.u32), U2H(stats[ST_F_RATE_MAX].u.u32));
1463
1464 chunk_appendf(out,
1465 /* sessions: current, max, limit, total */
1466 "<td>%s</td><td>%s</td><td>%s</td>"
1467 "<td><u>%s<div class=tips><table class=det>"
1468 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
1469 "",
Willy Tarreau8e0f1752016-12-12 15:07:29 +01001470 U2H(stats[ST_F_SCUR].u.u32), U2H(stats[ST_F_SMAX].u.u32), U2H(stats[ST_F_SLIM].u.u32),
William Lallemand74c24fb2016-11-21 17:18:36 +01001471 U2H(stats[ST_F_STOT].u.u64),
1472 U2H(stats[ST_F_STOT].u.u64));
1473
1474 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
1475 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0) {
1476 chunk_appendf(out,
Willy Tarreauf1573842018-12-14 11:35:36 +01001477 "<tr><th>New connections:</th><td>%s</td></tr>"
1478 "<tr><th>Reused connections:</th><td>%s</td><td>(%d%%)</td></tr>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001479 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
1480 "<tr><th>- HTTP 1xx responses:</th><td>%s</td></tr>"
1481 "<tr><th>- HTTP 2xx responses:</th><td>%s</td></tr>"
1482 "<tr><th>&nbsp;&nbsp;Compressed 2xx:</th><td>%s</td><td>(%d%%)</td></tr>"
1483 "<tr><th>- HTTP 3xx responses:</th><td>%s</td></tr>"
1484 "<tr><th>- HTTP 4xx responses:</th><td>%s</td></tr>"
1485 "<tr><th>- HTTP 5xx responses:</th><td>%s</td></tr>"
1486 "<tr><th>- other responses:</th><td>%s</td></tr>"
Willy Tarreaua1214a52018-12-14 14:00:25 +01001487 "<tr><th>Cache lookups:</th><td>%s</td></tr>"
1488 "<tr><th>Cache hits:</th><td>%s</td><td>(%d%%)</td></tr>"
Willy Tarreau1b0f85e2018-05-28 15:12:40 +02001489 "<tr><th>Failed hdr rewrites:</th><td>%s</td></tr>"
Christopher Faulet0159ee42019-12-16 14:40:39 +01001490 "<tr><th>Internal errors:</th><td>%s</td></tr>"
Christopher Faulet0d1c2a62019-11-08 14:59:51 +01001491 "",
Willy Tarreauf1573842018-12-14 11:35:36 +01001492 U2H(stats[ST_F_CONNECT].u.u64),
1493 U2H(stats[ST_F_REUSE].u.u64),
1494 (stats[ST_F_CONNECT].u.u64 + stats[ST_F_REUSE].u.u64) ?
1495 (int)(100 * stats[ST_F_REUSE].u.u64 / (stats[ST_F_CONNECT].u.u64 + stats[ST_F_REUSE].u.u64)) : 0,
William Lallemand74c24fb2016-11-21 17:18:36 +01001496 U2H(stats[ST_F_REQ_TOT].u.u64),
1497 U2H(stats[ST_F_HRSP_1XX].u.u64),
1498 U2H(stats[ST_F_HRSP_2XX].u.u64),
1499 U2H(stats[ST_F_COMP_RSP].u.u64),
1500 stats[ST_F_HRSP_2XX].u.u64 ?
1501 (int)(100 * stats[ST_F_COMP_RSP].u.u64 / stats[ST_F_HRSP_2XX].u.u64) : 0,
1502 U2H(stats[ST_F_HRSP_3XX].u.u64),
1503 U2H(stats[ST_F_HRSP_4XX].u.u64),
1504 U2H(stats[ST_F_HRSP_5XX].u.u64),
Willy Tarreaufeead3a2018-12-14 13:48:44 +01001505 U2H(stats[ST_F_HRSP_OTHER].u.u64),
Willy Tarreaua1214a52018-12-14 14:00:25 +01001506 U2H(stats[ST_F_CACHE_LOOKUPS].u.u64),
1507 U2H(stats[ST_F_CACHE_HITS].u.u64),
1508 stats[ST_F_CACHE_LOOKUPS].u.u64 ?
1509 (int)(100 * stats[ST_F_CACHE_HITS].u.u64 / stats[ST_F_CACHE_LOOKUPS].u.u64) : 0,
Christopher Faulet0159ee42019-12-16 14:40:39 +01001510 U2H(stats[ST_F_WREW].u.u64),
1511 U2H(stats[ST_F_EINT].u.u64));
William Lallemand74c24fb2016-11-21 17:18:36 +01001512 }
1513
Christopher Faulet0d1c2a62019-11-08 14:59:51 +01001514 chunk_appendf(out, "<tr><th colspan=3>Max / Avg over last 1024 success. conn.</th></tr>");
1515 chunk_appendf(out, "<tr><th>- Queue time:</th><td>%s / %s</td><td>ms</td></tr>",
1516 U2H(stats[ST_F_QT_MAX].u.u32), U2H(stats[ST_F_QTIME].u.u32));
1517 chunk_appendf(out, "<tr><th>- Connect time:</th><td>%s / %s</td><td>ms</td></tr>",
1518 U2H(stats[ST_F_CT_MAX].u.u32), U2H(stats[ST_F_CTIME].u.u32));
William Lallemand74c24fb2016-11-21 17:18:36 +01001519 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
Christopher Faulet0d1c2a62019-11-08 14:59:51 +01001520 chunk_appendf(out, "<tr><th>- Responses time:</th><td>%s / %s</td><td>ms</td></tr>",
1521 U2H(stats[ST_F_RT_MAX].u.u32), U2H(stats[ST_F_RTIME].u.u32));
1522 chunk_appendf(out, "<tr><th>- Total time:</th><td>%s / %s</td><td>ms</td></tr>",
1523 U2H(stats[ST_F_TT_MAX].u.u32), U2H(stats[ST_F_TTIME].u.u32));
William Lallemand74c24fb2016-11-21 17:18:36 +01001524
1525 chunk_appendf(out,
1526 "</table></div></u></td>"
1527 /* sessions: lbtot, last */
1528 "<td>%s</td><td>%s</td>"
1529 /* bytes: in */
1530 "<td>%s</td>"
1531 "",
1532 U2H(stats[ST_F_LBTOT].u.u64),
1533 human_time(stats[ST_F_LASTSESS].u.s32, 1),
1534 U2H(stats[ST_F_BIN].u.u64));
1535
1536 chunk_appendf(out,
1537 /* bytes:out + compression stats (via hover): comp_in, comp_out, comp_byp */
1538 "<td>%s%s<div class=tips><table class=det>"
1539 "<tr><th>Response bytes in:</th><td>%s</td></tr>"
1540 "<tr><th>Compression in:</th><td>%s</td></tr>"
1541 "<tr><th>Compression out:</th><td>%s</td><td>(%d%%)</td></tr>"
1542 "<tr><th>Compression bypass:</th><td>%s</td></tr>"
1543 "<tr><th>Total bytes saved:</th><td>%s</td><td>(%d%%)</td></tr>"
1544 "</table></div>%s</td>",
1545 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "<u>":"",
1546 U2H(stats[ST_F_BOUT].u.u64),
1547 U2H(stats[ST_F_BOUT].u.u64),
1548 U2H(stats[ST_F_COMP_IN].u.u64),
1549 U2H(stats[ST_F_COMP_OUT].u.u64),
1550 stats[ST_F_COMP_IN].u.u64 ? (int)(stats[ST_F_COMP_OUT].u.u64 * 100 / stats[ST_F_COMP_IN].u.u64) : 0,
1551 U2H(stats[ST_F_COMP_BYP].u.u64),
1552 U2H(stats[ST_F_COMP_IN].u.u64 - stats[ST_F_COMP_OUT].u.u64),
1553 stats[ST_F_BOUT].u.u64 ? (int)((stats[ST_F_COMP_IN].u.u64 - stats[ST_F_COMP_OUT].u.u64) * 100 / stats[ST_F_BOUT].u.u64) : 0,
1554 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "</u>":"");
1555
1556 chunk_appendf(out,
1557 /* denied: req, resp */
1558 "<td>%s</td><td>%s</td>"
1559 /* errors : request, connect */
1560 "<td></td><td>%s</td>"
1561 /* errors : response */
1562 "<td><u>%s<div class=tips>Connection resets during transfers: %lld client, %lld server</div></u></td>"
1563 /* warnings: retries, redispatches */
1564 "<td>%lld</td><td>%lld</td>"
1565 /* backend status: reflect backend status (up/down): we display UP
1566 * if the backend has known working servers or if it has no server at
1567 * all (eg: for stats). Then we display the total weight, number of
1568 * active and backups. */
Willy Tarreaubd715102020-10-23 22:44:30 +02001569 "<td class=ac>%s %s</td><td class=ac>&nbsp;</td><td class=ac>%d/%d</td>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001570 "<td class=ac>%d</td><td class=ac>%d</td>"
1571 "",
1572 U2H(stats[ST_F_DREQ].u.u64), U2H(stats[ST_F_DRESP].u.u64),
1573 U2H(stats[ST_F_ECON].u.u64),
1574 U2H(stats[ST_F_ERESP].u.u64),
1575 (long long)stats[ST_F_CLI_ABRT].u.u64,
1576 (long long)stats[ST_F_SRV_ABRT].u.u64,
1577 (long long)stats[ST_F_WRETR].u.u64, (long long)stats[ST_F_WREDIS].u.u64,
1578 human_time(stats[ST_F_LASTCHG].u.u32, 1),
1579 strcmp(field_str(stats, ST_F_STATUS), "DOWN") ? field_str(stats, ST_F_STATUS) : "<font color=\"red\"><b>DOWN</b></font>",
Willy Tarreaubd715102020-10-23 22:44:30 +02001580 stats[ST_F_WEIGHT].u.u32, stats[ST_F_UWEIGHT].u.u32,
William Lallemand74c24fb2016-11-21 17:18:36 +01001581 stats[ST_F_ACT].u.u32, stats[ST_F_BCK].u.u32);
1582
1583 chunk_appendf(out,
1584 /* rest of backend: nothing, down transitions, total downtime, throttle */
1585 "<td class=ac>&nbsp;</td><td>%d</td>"
1586 "<td>%s</td>"
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001587 "<td></td>",
William Lallemand74c24fb2016-11-21 17:18:36 +01001588 stats[ST_F_CHKDOWN].u.u32,
1589 stats[ST_F_DOWNTIME].type ? human_time(stats[ST_F_DOWNTIME].u.u32, 1) : "&nbsp;");
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001590
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001591 if (flags & STAT_SHMODULES) {
1592 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001593 chunk_appendf(out, "<td>");
1594
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001595 if (stats_px_get_cap(mod->domain_flags) & STATS_PX_CAP_BE) {
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001596 chunk_appendf(out,
1597 "<u>%s<div class=tips><table class=det>",
1598 mod->name);
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001599 for (j = 0; j < mod->stats_count; ++j) {
1600 chunk_appendf(out,
1601 "<tr><th>%s</th><td>%s</td></tr>",
1602 mod->stats[j].desc, field_to_html_str(&stats[ST_F_TOTAL_FIELDS + i]));
1603 ++i;
1604 }
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001605 chunk_appendf(out, "</table></div></u>");
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001606 } else {
1607 i += mod->stats_count;
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001608 }
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001609
1610 chunk_appendf(out, "</td>");
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001611 }
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001612 }
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001613
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001614 chunk_appendf(out, "</tr>");
William Lallemand74c24fb2016-11-21 17:18:36 +01001615 }
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001616
William Lallemand74c24fb2016-11-21 17:18:36 +01001617 return 1;
1618}
1619
Amaury Denoyelle97323c92020-10-02 18:32:01 +02001620int stats_dump_one_line(const struct field *stats, size_t stats_count,
1621 struct appctx *appctx)
William Lallemand74c24fb2016-11-21 17:18:36 +01001622{
Simon Horman05ee2132017-01-04 09:37:25 +01001623 int ret;
1624
William Lallemand74c24fb2016-11-21 17:18:36 +01001625 if (appctx->ctx.stats.flags & STAT_FMT_HTML)
Willy Tarreau43241ff2019-10-09 11:27:51 +02001626 ret = stats_dump_fields_html(&trash, stats, appctx->ctx.stats.flags);
William Lallemand74c24fb2016-11-21 17:18:36 +01001627 else if (appctx->ctx.stats.flags & STAT_FMT_TYPED)
Amaury Denoyelle072f97e2020-10-05 11:49:37 +02001628 ret = stats_dump_fields_typed(&trash, stats, stats_count, appctx->ctx.stats.flags, appctx->ctx.stats.domain);
Simon Horman05ee2132017-01-04 09:37:25 +01001629 else if (appctx->ctx.stats.flags & STAT_FMT_JSON)
Amaury Denoyelle072f97e2020-10-05 11:49:37 +02001630 ret = stats_dump_fields_json(&trash, stats, stats_count, appctx->ctx.stats.flags, appctx->ctx.stats.domain);
William Lallemand74c24fb2016-11-21 17:18:36 +01001631 else
Amaury Denoyelle50660a82020-10-05 11:49:39 +02001632 ret = stats_dump_fields_csv(&trash, stats, stats_count, appctx->ctx.stats.flags, appctx->ctx.stats.domain);
Simon Horman05ee2132017-01-04 09:37:25 +01001633
1634 if (ret)
1635 appctx->ctx.stats.flags |= STAT_STARTED;
1636
1637 return ret;
William Lallemand74c24fb2016-11-21 17:18:36 +01001638}
1639
William Dauchy0ef54392021-01-17 18:27:45 +01001640/* Fill <stats> with the frontend statistics. <stats> is preallocated array of
Christopher Fauletd808f172021-01-22 17:33:22 +01001641 * length <len>. If <selected_field> is != NULL, only fill this one. The length
1642 * of the array must be at least ST_F_TOTAL_FIELDS. If this length is less than
1643 * this value, or if the selected field is not implemented for frontends, the
1644 * function returns 0, otherwise, it returns 1.
William Lallemand74c24fb2016-11-21 17:18:36 +01001645 */
William Dauchy0ef54392021-01-17 18:27:45 +01001646int stats_fill_fe_stats(struct proxy *px, struct field *stats, int len,
1647 enum stat_field *selected_field)
William Lallemand74c24fb2016-11-21 17:18:36 +01001648{
William Dauchy0ef54392021-01-17 18:27:45 +01001649 enum stat_field current_field = (selected_field != NULL ? *selected_field : 0);
William Dauchy0ef54392021-01-17 18:27:45 +01001650
William Lallemand74c24fb2016-11-21 17:18:36 +01001651 if (len < ST_F_TOTAL_FIELDS)
1652 return 0;
1653
William Dauchy0ef54392021-01-17 18:27:45 +01001654 for (; current_field < ST_F_TOTAL_FIELDS; current_field++) {
Christopher Faulet8596bfb2021-01-25 15:16:41 +01001655 struct field metric = { 0 };
1656
William Dauchy0ef54392021-01-17 18:27:45 +01001657 switch (current_field) {
1658 case ST_F_PXNAME:
1659 metric = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
1660 break;
1661 case ST_F_SVNAME:
1662 metric = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, "FRONTEND");
1663 break;
1664 case ST_F_MODE:
1665 metric = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
1666 break;
1667 case ST_F_SCUR:
1668 metric = mkf_u32(0, px->feconn);
1669 break;
1670 case ST_F_SMAX:
1671 metric = mkf_u32(FN_MAX, px->fe_counters.conn_max);
1672 break;
1673 case ST_F_SLIM:
1674 metric = mkf_u32(FO_CONFIG|FN_LIMIT, px->maxconn);
1675 break;
1676 case ST_F_STOT:
1677 metric = mkf_u64(FN_COUNTER, px->fe_counters.cum_sess);
1678 break;
1679 case ST_F_BIN:
1680 metric = mkf_u64(FN_COUNTER, px->fe_counters.bytes_in);
1681 break;
1682 case ST_F_BOUT:
1683 metric = mkf_u64(FN_COUNTER, px->fe_counters.bytes_out);
1684 break;
1685 case ST_F_DREQ:
1686 metric = mkf_u64(FN_COUNTER, px->fe_counters.denied_req);
1687 break;
1688 case ST_F_DRESP:
1689 metric = mkf_u64(FN_COUNTER, px->fe_counters.denied_resp);
1690 break;
1691 case ST_F_EREQ:
1692 metric = mkf_u64(FN_COUNTER, px->fe_counters.failed_req);
1693 break;
1694 case ST_F_DCON:
1695 metric = mkf_u64(FN_COUNTER, px->fe_counters.denied_conn);
1696 break;
1697 case ST_F_DSES:
1698 metric = mkf_u64(FN_COUNTER, px->fe_counters.denied_sess);
1699 break;
1700 case ST_F_STATUS:
Christopher Fauletdfd10ab2021-10-06 14:24:19 +02001701 metric = mkf_str(FO_STATUS, (px->flags & (PR_FL_DISABLED|PR_FL_STOPPED)) ? "STOP" : "OPEN");
William Dauchy0ef54392021-01-17 18:27:45 +01001702 break;
1703 case ST_F_PID:
Willy Tarreaue8422bf2021-06-15 09:08:18 +02001704 metric = mkf_u32(FO_KEY, 1);
William Dauchy0ef54392021-01-17 18:27:45 +01001705 break;
1706 case ST_F_IID:
1707 metric = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
1708 break;
1709 case ST_F_SID:
1710 metric = mkf_u32(FO_KEY|FS_SERVICE, 0);
1711 break;
1712 case ST_F_TYPE:
1713 metric = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_FE);
1714 break;
1715 case ST_F_RATE:
1716 metric = mkf_u32(FN_RATE, read_freq_ctr(&px->fe_sess_per_sec));
1717 break;
1718 case ST_F_RATE_LIM:
1719 metric = mkf_u32(FO_CONFIG|FN_LIMIT, px->fe_sps_lim);
1720 break;
1721 case ST_F_RATE_MAX:
1722 metric = mkf_u32(FN_MAX, px->fe_counters.sps_max);
1723 break;
1724 case ST_F_WREW:
1725 metric = mkf_u64(FN_COUNTER, px->fe_counters.failed_rewrites);
1726 break;
1727 case ST_F_EINT:
1728 metric = mkf_u64(FN_COUNTER, px->fe_counters.internal_errors);
1729 break;
1730 case ST_F_HRSP_1XX:
William Dauchy2107a0f2021-01-22 21:09:48 +01001731 if (px->mode == PR_MODE_HTTP)
1732 metric = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[1]);
William Dauchy0ef54392021-01-17 18:27:45 +01001733 break;
1734 case ST_F_HRSP_2XX:
William Dauchy2107a0f2021-01-22 21:09:48 +01001735 if (px->mode == PR_MODE_HTTP)
1736 metric = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[2]);
William Dauchy0ef54392021-01-17 18:27:45 +01001737 break;
1738 case ST_F_HRSP_3XX:
William Dauchy2107a0f2021-01-22 21:09:48 +01001739 if (px->mode == PR_MODE_HTTP)
1740 metric = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[3]);
William Dauchy0ef54392021-01-17 18:27:45 +01001741 break;
1742 case ST_F_HRSP_4XX:
William Dauchy2107a0f2021-01-22 21:09:48 +01001743 if (px->mode == PR_MODE_HTTP)
1744 metric = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[4]);
William Dauchy0ef54392021-01-17 18:27:45 +01001745 break;
1746 case ST_F_HRSP_5XX:
William Dauchy2107a0f2021-01-22 21:09:48 +01001747 if (px->mode == PR_MODE_HTTP)
1748 metric = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[5]);
William Dauchy0ef54392021-01-17 18:27:45 +01001749 break;
1750 case ST_F_HRSP_OTHER:
William Dauchy2107a0f2021-01-22 21:09:48 +01001751 if (px->mode == PR_MODE_HTTP)
1752 metric = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[0]);
William Dauchy0ef54392021-01-17 18:27:45 +01001753 break;
1754 case ST_F_INTERCEPTED:
William Dauchy2107a0f2021-01-22 21:09:48 +01001755 if (px->mode == PR_MODE_HTTP)
1756 metric = mkf_u64(FN_COUNTER, px->fe_counters.intercepted_req);
William Dauchy0ef54392021-01-17 18:27:45 +01001757 break;
1758 case ST_F_CACHE_LOOKUPS:
William Dauchy2107a0f2021-01-22 21:09:48 +01001759 if (px->mode == PR_MODE_HTTP)
1760 metric = mkf_u64(FN_COUNTER, px->fe_counters.p.http.cache_lookups);
William Dauchy0ef54392021-01-17 18:27:45 +01001761 break;
1762 case ST_F_CACHE_HITS:
William Dauchy2107a0f2021-01-22 21:09:48 +01001763 if (px->mode == PR_MODE_HTTP)
1764 metric = mkf_u64(FN_COUNTER, px->fe_counters.p.http.cache_hits);
William Dauchy0ef54392021-01-17 18:27:45 +01001765 break;
1766 case ST_F_REQ_RATE:
1767 metric = mkf_u32(FN_RATE, read_freq_ctr(&px->fe_req_per_sec));
1768 break;
1769 case ST_F_REQ_RATE_MAX:
1770 metric = mkf_u32(FN_MAX, px->fe_counters.p.http.rps_max);
1771 break;
1772 case ST_F_REQ_TOT:
1773 metric = mkf_u64(FN_COUNTER, px->fe_counters.p.http.cum_req);
1774 break;
1775 case ST_F_COMP_IN:
1776 metric = mkf_u64(FN_COUNTER, px->fe_counters.comp_in);
1777 break;
1778 case ST_F_COMP_OUT:
1779 metric = mkf_u64(FN_COUNTER, px->fe_counters.comp_out);
1780 break;
1781 case ST_F_COMP_BYP:
1782 metric = mkf_u64(FN_COUNTER, px->fe_counters.comp_byp);
1783 break;
1784 case ST_F_COMP_RSP:
1785 metric = mkf_u64(FN_COUNTER, px->fe_counters.p.http.comp_rsp);
1786 break;
1787 case ST_F_CONN_RATE:
1788 metric = mkf_u32(FN_RATE, read_freq_ctr(&px->fe_conn_per_sec));
1789 break;
1790 case ST_F_CONN_RATE_MAX:
1791 metric = mkf_u32(FN_MAX, px->fe_counters.cps_max);
1792 break;
1793 case ST_F_CONN_TOT:
1794 metric = mkf_u64(FN_COUNTER, px->fe_counters.cum_conn);
1795 break;
1796 default:
Christopher Fauletd808f172021-01-22 17:33:22 +01001797 /* not used for frontends. If a specific metric
1798 * is requested, return an error. Otherwise continue.
1799 */
1800 if (selected_field != NULL)
1801 return 0;
1802 continue;
William Dauchy0ef54392021-01-17 18:27:45 +01001803 }
1804 stats[current_field] = metric;
1805 if (selected_field != NULL)
1806 break;
William Lallemand74c24fb2016-11-21 17:18:36 +01001807 }
William Lallemand74c24fb2016-11-21 17:18:36 +01001808 return 1;
1809}
1810
1811/* Dumps a frontend's line to the trash for the current proxy <px> and uses
Christopher Faulet908628c2022-03-25 16:43:49 +01001812 * the state from conn-stream <cs>. The caller is responsible for clearing
William Lallemand74c24fb2016-11-21 17:18:36 +01001813 * the trash if needed. Returns non-zero if it emits anything, zero otherwise.
1814 */
Christopher Faulet908628c2022-03-25 16:43:49 +01001815static int stats_dump_fe_stats(struct conn_stream *cs, struct proxy *px)
William Lallemand74c24fb2016-11-21 17:18:36 +01001816{
Christopher Faulet908628c2022-03-25 16:43:49 +01001817 struct appctx *appctx = __cs_appctx(cs);
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02001818 struct field *stats = stat_l[STATS_DOMAIN_PROXY];
1819 struct stats_module *mod;
1820 size_t stats_count = ST_F_TOTAL_FIELDS;
William Lallemand74c24fb2016-11-21 17:18:36 +01001821
1822 if (!(px->cap & PR_CAP_FE))
1823 return 0;
1824
1825 if ((appctx->ctx.stats.flags & STAT_BOUND) && !(appctx->ctx.stats.type & (1 << STATS_TYPE_FE)))
1826 return 0;
1827
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02001828 memset(stats, 0, sizeof(struct field) * stat_count[STATS_DOMAIN_PROXY]);
1829
William Dauchy0ef54392021-01-17 18:27:45 +01001830 if (!stats_fill_fe_stats(px, stats, ST_F_TOTAL_FIELDS, NULL))
William Lallemand74c24fb2016-11-21 17:18:36 +01001831 return 0;
1832
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02001833 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
1834 void *counters;
1835
1836 if (!(stats_px_get_cap(mod->domain_flags) & STATS_PX_CAP_FE)) {
1837 stats_count += mod->stats_count;
1838 continue;
1839 }
1840
1841 counters = EXTRA_COUNTERS_GET(px->extra_counters_fe, mod);
1842 mod->fill_stats(counters, stats + stats_count);
1843 stats_count += mod->stats_count;
1844 }
1845
1846 return stats_dump_one_line(stats, stats_count, appctx);
William Lallemand74c24fb2016-11-21 17:18:36 +01001847}
1848
William Dauchy655e14e2021-02-14 23:22:54 +01001849/* Fill <stats> with the listener statistics. <stats> is preallocated array of
1850 * length <len>. The length of the array must be at least ST_F_TOTAL_FIELDS. If
1851 * this length is less then this value, the function returns 0, otherwise, it
1852 * returns 1. If selected_field is != NULL, only fill this one. <flags> can
1853 * take the value STAT_SHLGNDS.
William Lallemand74c24fb2016-11-21 17:18:36 +01001854 */
1855int stats_fill_li_stats(struct proxy *px, struct listener *l, int flags,
William Dauchy655e14e2021-02-14 23:22:54 +01001856 struct field *stats, int len, enum stat_field *selected_field)
William Lallemand74c24fb2016-11-21 17:18:36 +01001857{
William Dauchy655e14e2021-02-14 23:22:54 +01001858 enum stat_field current_field = (selected_field != NULL ? *selected_field : 0);
Willy Tarreau83061a82018-07-13 11:56:34 +02001859 struct buffer *out = get_trash_chunk();
William Lallemand74c24fb2016-11-21 17:18:36 +01001860
1861 if (len < ST_F_TOTAL_FIELDS)
1862 return 0;
1863
1864 if (!l->counters)
1865 return 0;
1866
1867 chunk_reset(out);
William Lallemand74c24fb2016-11-21 17:18:36 +01001868
William Dauchy655e14e2021-02-14 23:22:54 +01001869 for (; current_field < ST_F_TOTAL_FIELDS; current_field++) {
1870 struct field metric = { 0 };
William Lallemand74c24fb2016-11-21 17:18:36 +01001871
William Dauchy655e14e2021-02-14 23:22:54 +01001872 switch (current_field) {
1873 case ST_F_PXNAME:
1874 metric = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
1875 break;
1876 case ST_F_SVNAME:
1877 metric = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, l->name);
1878 break;
1879 case ST_F_MODE:
1880 metric = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
1881 break;
1882 case ST_F_SCUR:
1883 metric = mkf_u32(0, l->nbconn);
1884 break;
1885 case ST_F_SMAX:
1886 metric = mkf_u32(FN_MAX, l->counters->conn_max);
1887 break;
1888 case ST_F_SLIM:
1889 metric = mkf_u32(FO_CONFIG|FN_LIMIT, l->maxconn);
1890 break;
1891 case ST_F_STOT:
1892 metric = mkf_u64(FN_COUNTER, l->counters->cum_conn);
1893 break;
1894 case ST_F_BIN:
1895 metric = mkf_u64(FN_COUNTER, l->counters->bytes_in);
1896 break;
1897 case ST_F_BOUT:
1898 metric = mkf_u64(FN_COUNTER, l->counters->bytes_out);
1899 break;
1900 case ST_F_DREQ:
1901 metric = mkf_u64(FN_COUNTER, l->counters->denied_req);
1902 break;
1903 case ST_F_DRESP:
1904 metric = mkf_u64(FN_COUNTER, l->counters->denied_resp);
1905 break;
1906 case ST_F_EREQ:
1907 metric = mkf_u64(FN_COUNTER, l->counters->failed_req);
1908 break;
1909 case ST_F_DCON:
1910 metric = mkf_u64(FN_COUNTER, l->counters->denied_conn);
1911 break;
1912 case ST_F_DSES:
1913 metric = mkf_u64(FN_COUNTER, l->counters->denied_sess);
1914 break;
1915 case ST_F_STATUS:
William Dauchy3679d0c2021-02-14 23:22:55 +01001916 metric = mkf_str(FO_STATUS, li_status_st[get_li_status(l)]);
William Dauchy655e14e2021-02-14 23:22:54 +01001917 break;
1918 case ST_F_PID:
Willy Tarreaue8422bf2021-06-15 09:08:18 +02001919 metric = mkf_u32(FO_KEY, 1);
William Dauchy655e14e2021-02-14 23:22:54 +01001920 break;
1921 case ST_F_IID:
1922 metric = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
1923 break;
1924 case ST_F_SID:
1925 metric = mkf_u32(FO_KEY|FS_SERVICE, l->luid);
1926 break;
1927 case ST_F_TYPE:
1928 metric = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_SO);
1929 break;
1930 case ST_F_WREW:
1931 metric = mkf_u64(FN_COUNTER, l->counters->failed_rewrites);
1932 break;
1933 case ST_F_EINT:
1934 metric = mkf_u64(FN_COUNTER, l->counters->internal_errors);
1935 break;
1936 case ST_F_ADDR:
1937 if (flags & STAT_SHLGNDS) {
1938 char str[INET6_ADDRSTRLEN];
1939 int port;
William Lallemand74c24fb2016-11-21 17:18:36 +01001940
William Dauchy655e14e2021-02-14 23:22:54 +01001941 port = get_host_port(&l->rx.addr);
1942 switch (addr_to_str(&l->rx.addr, str, sizeof(str))) {
1943 case AF_INET:
1944 metric = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
1945 chunk_appendf(out, "%s:%d", str, port);
1946 break;
1947 case AF_INET6:
1948 metric = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
1949 chunk_appendf(out, "[%s]:%d", str, port);
1950 break;
1951 case AF_UNIX:
1952 metric = mkf_str(FO_CONFIG|FS_SERVICE, "unix");
1953 break;
1954 case -1:
1955 metric = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
1956 chunk_strcat(out, strerror(errno));
1957 break;
1958 default: /* address family not supported */
1959 break;
1960 }
1961 }
1962 break;
1963 default:
1964 /* not used for listen. If a specific metric
1965 * is requested, return an error. Otherwise continue.
1966 */
1967 if (selected_field != NULL)
1968 return 0;
1969 continue;
William Lallemand74c24fb2016-11-21 17:18:36 +01001970 }
William Dauchy655e14e2021-02-14 23:22:54 +01001971 stats[current_field] = metric;
1972 if (selected_field != NULL)
1973 break;
William Lallemand74c24fb2016-11-21 17:18:36 +01001974 }
William Lallemand74c24fb2016-11-21 17:18:36 +01001975 return 1;
1976}
1977
1978/* Dumps a line for listener <l> and proxy <px> to the trash and uses the state
Christopher Faulet908628c2022-03-25 16:43:49 +01001979 * from conn-stream <cs>. The caller is responsible for clearing the trash
Willy Tarreau43241ff2019-10-09 11:27:51 +02001980 * if needed. Returns non-zero if it emits anything, zero otherwise.
William Lallemand74c24fb2016-11-21 17:18:36 +01001981 */
Christopher Faulet908628c2022-03-25 16:43:49 +01001982static int stats_dump_li_stats(struct conn_stream *cs, struct proxy *px, struct listener *l)
William Lallemand74c24fb2016-11-21 17:18:36 +01001983{
Christopher Faulet908628c2022-03-25 16:43:49 +01001984 struct appctx *appctx = __cs_appctx(cs);
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02001985 struct field *stats = stat_l[STATS_DOMAIN_PROXY];
1986 struct stats_module *mod;
1987 size_t stats_count = ST_F_TOTAL_FIELDS;
1988
1989 memset(stats, 0, sizeof(struct field) * stat_count[STATS_DOMAIN_PROXY]);
William Lallemand74c24fb2016-11-21 17:18:36 +01001990
William Dauchy655e14e2021-02-14 23:22:54 +01001991 if (!stats_fill_li_stats(px, l, appctx->ctx.stats.flags, stats,
1992 ST_F_TOTAL_FIELDS, NULL))
William Lallemand74c24fb2016-11-21 17:18:36 +01001993 return 0;
1994
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02001995 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
1996 void *counters;
1997
1998 if (!(stats_px_get_cap(mod->domain_flags) & STATS_PX_CAP_LI)) {
1999 stats_count += mod->stats_count;
2000 continue;
2001 }
2002
2003 counters = EXTRA_COUNTERS_GET(l->extra_counters, mod);
2004 mod->fill_stats(counters, stats + stats_count);
2005 stats_count += mod->stats_count;
2006 }
2007
2008 return stats_dump_one_line(stats, stats_count, appctx);
William Lallemand74c24fb2016-11-21 17:18:36 +01002009}
2010
2011enum srv_stats_state {
2012 SRV_STATS_STATE_DOWN = 0,
2013 SRV_STATS_STATE_DOWN_AGENT,
2014 SRV_STATS_STATE_GOING_UP,
2015 SRV_STATS_STATE_UP_GOING_DOWN,
2016 SRV_STATS_STATE_UP,
2017 SRV_STATS_STATE_NOLB_GOING_DOWN,
2018 SRV_STATS_STATE_NOLB,
2019 SRV_STATS_STATE_DRAIN_GOING_DOWN,
2020 SRV_STATS_STATE_DRAIN,
2021 SRV_STATS_STATE_DRAIN_AGENT,
2022 SRV_STATS_STATE_NO_CHECK,
2023
2024 SRV_STATS_STATE_COUNT, /* Must be last */
2025};
2026
2027static const char *srv_hlt_st[SRV_STATS_STATE_COUNT] = {
2028 [SRV_STATS_STATE_DOWN] = "DOWN",
2029 [SRV_STATS_STATE_DOWN_AGENT] = "DOWN (agent)",
2030 [SRV_STATS_STATE_GOING_UP] = "DOWN %d/%d",
2031 [SRV_STATS_STATE_UP_GOING_DOWN] = "UP %d/%d",
2032 [SRV_STATS_STATE_UP] = "UP",
2033 [SRV_STATS_STATE_NOLB_GOING_DOWN] = "NOLB %d/%d",
2034 [SRV_STATS_STATE_NOLB] = "NOLB",
2035 [SRV_STATS_STATE_DRAIN_GOING_DOWN] = "DRAIN %d/%d",
2036 [SRV_STATS_STATE_DRAIN] = "DRAIN",
2037 [SRV_STATS_STATE_DRAIN_AGENT] = "DRAIN (agent)",
2038 [SRV_STATS_STATE_NO_CHECK] = "no check"
2039};
2040
William Dauchyd3a9a492021-01-25 17:29:03 +01002041/* Compute server state helper
William Lallemand74c24fb2016-11-21 17:18:36 +01002042 */
William Dauchyd3a9a492021-01-25 17:29:03 +01002043static void stats_fill_sv_stats_computestate(struct server *sv, struct server *ref,
2044 enum srv_stats_state *state)
William Lallemand74c24fb2016-11-21 17:18:36 +01002045{
Emeric Brun52a91d32017-08-31 14:41:55 +02002046 if (sv->cur_state == SRV_ST_RUNNING || sv->cur_state == SRV_ST_STARTING) {
William Lallemand74c24fb2016-11-21 17:18:36 +01002047 if ((ref->check.state & CHK_ST_ENABLED) &&
2048 (ref->check.health < ref->check.rise + ref->check.fall - 1)) {
William Dauchyd3a9a492021-01-25 17:29:03 +01002049 *state = SRV_STATS_STATE_UP_GOING_DOWN;
William Lallemand74c24fb2016-11-21 17:18:36 +01002050 } else {
William Dauchyd3a9a492021-01-25 17:29:03 +01002051 *state = SRV_STATS_STATE_UP;
William Lallemand74c24fb2016-11-21 17:18:36 +01002052 }
2053
Emeric Brun52a91d32017-08-31 14:41:55 +02002054 if (sv->cur_admin & SRV_ADMF_DRAIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +01002055 if (ref->agent.state & CHK_ST_ENABLED)
William Dauchyd3a9a492021-01-25 17:29:03 +01002056 *state = SRV_STATS_STATE_DRAIN_AGENT;
2057 else if (*state == SRV_STATS_STATE_UP_GOING_DOWN)
2058 *state = SRV_STATS_STATE_DRAIN_GOING_DOWN;
William Lallemand74c24fb2016-11-21 17:18:36 +01002059 else
William Dauchyd3a9a492021-01-25 17:29:03 +01002060 *state = SRV_STATS_STATE_DRAIN;
William Lallemand74c24fb2016-11-21 17:18:36 +01002061 }
2062
William Dauchyd3a9a492021-01-25 17:29:03 +01002063 if (*state == SRV_STATS_STATE_UP && !(ref->check.state & CHK_ST_ENABLED)) {
2064 *state = SRV_STATS_STATE_NO_CHECK;
William Lallemand74c24fb2016-11-21 17:18:36 +01002065 }
2066 }
Emeric Brun52a91d32017-08-31 14:41:55 +02002067 else if (sv->cur_state == SRV_ST_STOPPING) {
William Lallemand74c24fb2016-11-21 17:18:36 +01002068 if ((!(sv->check.state & CHK_ST_ENABLED) && !sv->track) ||
2069 (ref->check.health == ref->check.rise + ref->check.fall - 1)) {
William Dauchyd3a9a492021-01-25 17:29:03 +01002070 *state = SRV_STATS_STATE_NOLB;
William Lallemand74c24fb2016-11-21 17:18:36 +01002071 } else {
William Dauchyd3a9a492021-01-25 17:29:03 +01002072 *state = SRV_STATS_STATE_NOLB_GOING_DOWN;
William Lallemand74c24fb2016-11-21 17:18:36 +01002073 }
2074 }
2075 else { /* stopped */
2076 if ((ref->agent.state & CHK_ST_ENABLED) && !ref->agent.health) {
William Dauchyd3a9a492021-01-25 17:29:03 +01002077 *state = SRV_STATS_STATE_DOWN_AGENT;
William Lallemand74c24fb2016-11-21 17:18:36 +01002078 } else if ((ref->check.state & CHK_ST_ENABLED) && !ref->check.health) {
William Dauchyd3a9a492021-01-25 17:29:03 +01002079 *state = SRV_STATS_STATE_DOWN; /* DOWN */
William Lallemand74c24fb2016-11-21 17:18:36 +01002080 } else if ((ref->agent.state & CHK_ST_ENABLED) || (ref->check.state & CHK_ST_ENABLED)) {
William Dauchyd3a9a492021-01-25 17:29:03 +01002081 *state = SRV_STATS_STATE_GOING_UP;
William Lallemand74c24fb2016-11-21 17:18:36 +01002082 } else {
William Dauchyd3a9a492021-01-25 17:29:03 +01002083 *state = SRV_STATS_STATE_DOWN; /* DOWN, unchecked */
William Lallemand74c24fb2016-11-21 17:18:36 +01002084 }
2085 }
William Dauchyd3a9a492021-01-25 17:29:03 +01002086}
William Lallemand74c24fb2016-11-21 17:18:36 +01002087
William Dauchyd3a9a492021-01-25 17:29:03 +01002088/* Fill <stats> with the backend statistics. <stats> is preallocated array of
2089 * length <len>. If <selected_field> is != NULL, only fill this one. The length
2090 * of the array must be at least ST_F_TOTAL_FIELDS. If this length is less than
2091 * this value, or if the selected field is not implemented for servers, the
2092 * function returns 0, otherwise, it returns 1. <flags> can take the value
2093 * STAT_SHLGNDS.
2094 */
2095int stats_fill_sv_stats(struct proxy *px, struct server *sv, int flags,
2096 struct field *stats, int len,
2097 enum stat_field *selected_field)
2098{
2099 enum stat_field current_field = (selected_field != NULL ? *selected_field : 0);
2100 struct server *via = sv->track ? sv->track : sv;
2101 struct server *ref = via;
2102 enum srv_stats_state state = 0;
2103 char str[INET6_ADDRSTRLEN];
2104 struct buffer *out = get_trash_chunk();
2105 char *fld_status;
2106 long long srv_samples_counter;
2107 unsigned int srv_samples_window = TIME_STATS_SAMPLES;
William Lallemand74c24fb2016-11-21 17:18:36 +01002108
William Dauchyd3a9a492021-01-25 17:29:03 +01002109 if (len < ST_F_TOTAL_FIELDS)
2110 return 0;
William Lallemand74c24fb2016-11-21 17:18:36 +01002111
William Dauchyd3a9a492021-01-25 17:29:03 +01002112 chunk_reset(out);
William Lallemand74c24fb2016-11-21 17:18:36 +01002113
William Dauchyd3a9a492021-01-25 17:29:03 +01002114 /* compute state for later use */
2115 if (selected_field == NULL || *selected_field == ST_F_STATUS ||
2116 *selected_field == ST_F_CHECK_RISE || *selected_field == ST_F_CHECK_FALL ||
2117 *selected_field == ST_F_CHECK_HEALTH || *selected_field == ST_F_HANAFAIL) {
2118 /* we have "via" which is the tracked server as described in the configuration,
2119 * and "ref" which is the checked server and the end of the chain.
2120 */
2121 while (ref->track)
2122 ref = ref->track;
2123 stats_fill_sv_stats_computestate(sv, ref, &state);
William Lallemand74c24fb2016-11-21 17:18:36 +01002124 }
2125
William Dauchyd3a9a492021-01-25 17:29:03 +01002126 /* compue time values for later use */
2127 if (selected_field == NULL || *selected_field == ST_F_QTIME ||
2128 *selected_field == ST_F_CTIME || *selected_field == ST_F_RTIME ||
2129 *selected_field == ST_F_TTIME) {
2130 srv_samples_counter = (px->mode == PR_MODE_HTTP) ? sv->counters.p.http.cum_req : sv->counters.cum_lbconn;
2131 if (srv_samples_counter < TIME_STATS_SAMPLES && srv_samples_counter > 0)
2132 srv_samples_window = srv_samples_counter;
William Lallemand74c24fb2016-11-21 17:18:36 +01002133 }
2134
William Dauchyd3a9a492021-01-25 17:29:03 +01002135 for (; current_field < ST_F_TOTAL_FIELDS; current_field++) {
2136 struct field metric = { 0 };
William Lallemand74c24fb2016-11-21 17:18:36 +01002137
William Dauchyd3a9a492021-01-25 17:29:03 +01002138 switch (current_field) {
2139 case ST_F_PXNAME:
2140 metric = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
2141 break;
2142 case ST_F_SVNAME:
2143 metric = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, sv->id);
2144 break;
2145 case ST_F_MODE:
2146 metric = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
Christopher Faulet3888b8c2021-01-27 13:32:25 +01002147 break;
William Dauchyd3a9a492021-01-25 17:29:03 +01002148 case ST_F_QCUR:
Willy Tarreaua0570452021-06-18 09:30:30 +02002149 metric = mkf_u32(0, sv->queue.length);
William Dauchyd3a9a492021-01-25 17:29:03 +01002150 break;
2151 case ST_F_QMAX:
2152 metric = mkf_u32(FN_MAX, sv->counters.nbpend_max);
2153 break;
2154 case ST_F_SCUR:
2155 metric = mkf_u32(0, sv->cur_sess);
2156 break;
2157 case ST_F_SMAX:
2158 metric = mkf_u32(FN_MAX, sv->counters.cur_sess_max);
2159 break;
2160 case ST_F_SLIM:
2161 if (sv->maxconn)
2162 metric = mkf_u32(FO_CONFIG|FN_LIMIT, sv->maxconn);
2163 break;
2164 case ST_F_SRV_ICUR:
2165 metric = mkf_u32(0, sv->curr_idle_conns);
2166 break;
2167 case ST_F_SRV_ILIM:
2168 if (sv->max_idle_conns != -1)
2169 metric = mkf_u32(FO_CONFIG|FN_LIMIT, sv->max_idle_conns);
2170 break;
2171 case ST_F_STOT:
2172 metric = mkf_u64(FN_COUNTER, sv->counters.cum_sess);
2173 break;
2174 case ST_F_BIN:
2175 metric = mkf_u64(FN_COUNTER, sv->counters.bytes_in);
2176 break;
2177 case ST_F_BOUT:
2178 metric = mkf_u64(FN_COUNTER, sv->counters.bytes_out);
2179 break;
2180 case ST_F_DRESP:
2181 metric = mkf_u64(FN_COUNTER, sv->counters.denied_resp);
2182 break;
2183 case ST_F_ECON:
2184 metric = mkf_u64(FN_COUNTER, sv->counters.failed_conns);
2185 break;
2186 case ST_F_ERESP:
2187 metric = mkf_u64(FN_COUNTER, sv->counters.failed_resp);
2188 break;
2189 case ST_F_WRETR:
2190 metric = mkf_u64(FN_COUNTER, sv->counters.retries);
2191 break;
2192 case ST_F_WREDIS:
2193 metric = mkf_u64(FN_COUNTER, sv->counters.redispatches);
2194 break;
2195 case ST_F_WREW:
2196 metric = mkf_u64(FN_COUNTER, sv->counters.failed_rewrites);
2197 break;
2198 case ST_F_EINT:
2199 metric = mkf_u64(FN_COUNTER, sv->counters.internal_errors);
2200 break;
2201 case ST_F_CONNECT:
2202 metric = mkf_u64(FN_COUNTER, sv->counters.connect);
2203 break;
2204 case ST_F_REUSE:
2205 metric = mkf_u64(FN_COUNTER, sv->counters.reuse);
2206 break;
2207 case ST_F_IDLE_CONN_CUR:
2208 metric = mkf_u32(0, sv->curr_idle_nb);
2209 break;
2210 case ST_F_SAFE_CONN_CUR:
2211 metric = mkf_u32(0, sv->curr_safe_nb);
2212 break;
2213 case ST_F_USED_CONN_CUR:
2214 metric = mkf_u32(0, sv->curr_used_conns);
2215 break;
2216 case ST_F_NEED_CONN_EST:
2217 metric = mkf_u32(0, sv->est_need_conns);
2218 break;
2219 case ST_F_STATUS:
2220 fld_status = chunk_newstr(out);
2221 if (sv->cur_admin & SRV_ADMF_RMAINT)
2222 chunk_appendf(out, "MAINT (resolution)");
2223 else if (sv->cur_admin & SRV_ADMF_IMAINT)
2224 chunk_appendf(out, "MAINT (via %s/%s)", via->proxy->id, via->id);
2225 else if (sv->cur_admin & SRV_ADMF_MAINT)
2226 chunk_appendf(out, "MAINT");
2227 else
2228 chunk_appendf(out,
2229 srv_hlt_st[state],
2230 (ref->cur_state != SRV_ST_STOPPED) ? (ref->check.health - ref->check.rise + 1) : (ref->check.health),
2231 (ref->cur_state != SRV_ST_STOPPED) ? (ref->check.fall) : (ref->check.rise));
William Lallemand74c24fb2016-11-21 17:18:36 +01002232
William Dauchyd3a9a492021-01-25 17:29:03 +01002233 metric = mkf_str(FO_STATUS, fld_status);
2234 break;
2235 case ST_F_LASTCHG:
2236 metric = mkf_u32(FN_AGE, now.tv_sec - sv->last_change);
2237 break;
2238 case ST_F_WEIGHT:
2239 metric = mkf_u32(FN_AVG, (sv->cur_eweight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv);
2240 break;
2241 case ST_F_UWEIGHT:
2242 metric = mkf_u32(FN_AVG, sv->uweight);
2243 break;
2244 case ST_F_ACT:
2245 metric = mkf_u32(FO_STATUS, (sv->flags & SRV_F_BACKUP) ? 0 : 1);
2246 break;
2247 case ST_F_BCK:
2248 metric = mkf_u32(FO_STATUS, (sv->flags & SRV_F_BACKUP) ? 1 : 0);
2249 break;
2250 case ST_F_CHKFAIL:
2251 if (sv->check.state & CHK_ST_ENABLED)
2252 metric = mkf_u64(FN_COUNTER, sv->counters.failed_checks);
2253 break;
2254 case ST_F_CHKDOWN:
2255 if (sv->check.state & CHK_ST_ENABLED)
2256 metric = mkf_u64(FN_COUNTER, sv->counters.down_trans);
2257 break;
2258 case ST_F_DOWNTIME:
2259 if (sv->check.state & CHK_ST_ENABLED)
2260 metric = mkf_u32(FN_COUNTER, srv_downtime(sv));
2261 break;
2262 case ST_F_QLIMIT:
2263 if (sv->maxqueue)
2264 metric = mkf_u32(FO_CONFIG|FS_SERVICE, sv->maxqueue);
2265 break;
2266 case ST_F_PID:
Willy Tarreaue8422bf2021-06-15 09:08:18 +02002267 metric = mkf_u32(FO_KEY, 1);
William Dauchyd3a9a492021-01-25 17:29:03 +01002268 break;
2269 case ST_F_IID:
2270 metric = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
2271 break;
2272 case ST_F_SID:
2273 metric = mkf_u32(FO_KEY|FS_SERVICE, sv->puid);
2274 break;
2275 case ST_F_THROTTLE:
2276 if (sv->cur_state == SRV_ST_STARTING && !server_is_draining(sv))
2277 metric = mkf_u32(FN_AVG, server_throttle_rate(sv));
2278 break;
2279 case ST_F_LBTOT:
2280 metric = mkf_u64(FN_COUNTER, sv->counters.cum_lbconn);
2281 break;
2282 case ST_F_TRACKED:
2283 if (sv->track) {
2284 char *fld_track = chunk_newstr(out);
2285 chunk_appendf(out, "%s/%s", sv->track->proxy->id, sv->track->id);
2286 metric = mkf_str(FO_CONFIG|FN_NAME|FS_SERVICE, fld_track);
2287 }
2288 break;
2289 case ST_F_TYPE:
2290 metric = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_SV);
2291 break;
2292 case ST_F_RATE:
2293 metric = mkf_u32(FN_RATE, read_freq_ctr(&sv->sess_per_sec));
2294 break;
2295 case ST_F_RATE_MAX:
2296 metric = mkf_u32(FN_MAX, sv->counters.sps_max);
2297 break;
2298 case ST_F_CHECK_STATUS:
2299 if ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED) {
2300 const char *fld_chksts;
Marcin Deraneka8dbdf32020-05-15 20:02:40 +02002301
William Dauchyd3a9a492021-01-25 17:29:03 +01002302 fld_chksts = chunk_newstr(out);
2303 chunk_strcat(out, "* "); // for check in progress
2304 chunk_strcat(out, get_check_status_info(sv->check.status));
2305 if (!(sv->check.state & CHK_ST_INPROGRESS))
2306 fld_chksts += 2; // skip "* "
Willy Tarreau973a9372021-05-12 17:29:14 +02002307 metric = mkf_str(FN_OUTPUT, fld_chksts);
William Dauchyd3a9a492021-01-25 17:29:03 +01002308 }
2309 break;
2310 case ST_F_CHECK_CODE:
2311 if ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED &&
2312 sv->check.status >= HCHK_STATUS_L57DATA)
2313 metric = mkf_u32(FN_OUTPUT, sv->check.code);
2314 break;
2315 case ST_F_CHECK_DURATION:
2316 if ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED &&
2317 sv->check.status >= HCHK_STATUS_CHECKED)
2318 metric = mkf_u64(FN_DURATION, sv->check.duration);
2319 break;
2320 case ST_F_CHECK_DESC:
2321 if ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED)
2322 metric = mkf_str(FN_OUTPUT, get_check_status_description(sv->check.status));
2323 break;
2324 case ST_F_LAST_CHK:
2325 if ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED)
2326 metric = mkf_str(FN_OUTPUT, sv->check.desc);
2327 break;
2328 case ST_F_CHECK_RISE:
2329 if ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED)
2330 metric = mkf_u32(FO_CONFIG|FS_SERVICE, ref->check.rise);
2331 break;
2332 case ST_F_CHECK_FALL:
2333 if ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED)
2334 metric = mkf_u32(FO_CONFIG|FS_SERVICE, ref->check.fall);
2335 break;
2336 case ST_F_CHECK_HEALTH:
2337 if ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED)
2338 metric = mkf_u32(FO_CONFIG|FS_SERVICE, ref->check.health);
2339 break;
Christopher Faulet59bab612021-07-22 08:04:38 +02002340 case ST_F_AGENT_STATUS:
2341 if ((sv->agent.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED) {
2342 const char *fld_chksts;
2343
2344 fld_chksts = chunk_newstr(out);
2345 chunk_strcat(out, "* "); // for check in progress
2346 chunk_strcat(out, get_check_status_info(sv->agent.status));
2347 if (!(sv->agent.state & CHK_ST_INPROGRESS))
2348 fld_chksts += 2; // skip "* "
2349 metric = mkf_str(FN_OUTPUT, fld_chksts);
2350 }
2351 break;
2352 case ST_F_AGENT_CODE:
2353 if ((sv->agent.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED &&
2354 (sv->agent.status >= HCHK_STATUS_L57DATA))
2355 metric = mkf_u32(FN_OUTPUT, sv->agent.code);
2356 break;
2357 case ST_F_AGENT_DURATION:
2358 if ((sv->agent.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED)
2359 metric = mkf_u64(FN_DURATION, sv->agent.duration);
2360 break;
2361 case ST_F_AGENT_DESC:
2362 if ((sv->agent.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED)
2363 metric = mkf_str(FN_OUTPUT, get_check_status_description(sv->agent.status));
2364 break;
2365 case ST_F_LAST_AGT:
2366 if ((sv->agent.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED)
2367 metric = mkf_str(FN_OUTPUT, sv->agent.desc);
2368 break;
2369 case ST_F_AGENT_RISE:
2370 if ((sv->agent.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED)
2371 metric = mkf_u32(FO_CONFIG|FS_SERVICE, sv->agent.rise);
2372 break;
2373 case ST_F_AGENT_FALL:
2374 if ((sv->agent.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED)
2375 metric = mkf_u32(FO_CONFIG|FS_SERVICE, sv->agent.fall);
2376 break;
2377 case ST_F_AGENT_HEALTH:
2378 if ((sv->agent.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED)
2379 metric = mkf_u32(FO_CONFIG|FS_SERVICE, sv->agent.health);
2380 break;
William Dauchyd3a9a492021-01-25 17:29:03 +01002381 case ST_F_REQ_TOT:
2382 if (px->mode == PR_MODE_HTTP)
2383 metric = mkf_u64(FN_COUNTER, sv->counters.p.http.cum_req);
2384 break;
2385 case ST_F_HRSP_1XX:
2386 if (px->mode == PR_MODE_HTTP)
2387 metric = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[1]);
2388 break;
2389 case ST_F_HRSP_2XX:
2390 if (px->mode == PR_MODE_HTTP)
2391 metric = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[2]);
2392 break;
2393 case ST_F_HRSP_3XX:
2394 if (px->mode == PR_MODE_HTTP)
2395 metric = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[3]);
2396 break;
2397 case ST_F_HRSP_4XX:
2398 if (px->mode == PR_MODE_HTTP)
2399 metric = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[4]);
2400 break;
2401 case ST_F_HRSP_5XX:
2402 if (px->mode == PR_MODE_HTTP)
2403 metric = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[5]);
2404 break;
2405 case ST_F_HRSP_OTHER:
2406 if (px->mode == PR_MODE_HTTP)
2407 metric = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[0]);
2408 break;
2409 case ST_F_HANAFAIL:
2410 if (ref->observe)
2411 metric = mkf_u64(FN_COUNTER, sv->counters.failed_hana);
2412 break;
2413 case ST_F_CLI_ABRT:
2414 metric = mkf_u64(FN_COUNTER, sv->counters.cli_aborts);
2415 break;
2416 case ST_F_SRV_ABRT:
2417 metric = mkf_u64(FN_COUNTER, sv->counters.srv_aborts);
2418 break;
2419 case ST_F_LASTSESS:
2420 metric = mkf_s32(FN_AGE, srv_lastsession(sv));
2421 break;
2422 case ST_F_QTIME:
2423 metric = mkf_u32(FN_AVG, swrate_avg(sv->counters.q_time, srv_samples_window));
2424 break;
2425 case ST_F_CTIME:
2426 metric = mkf_u32(FN_AVG, swrate_avg(sv->counters.c_time, srv_samples_window));
2427 break;
2428 case ST_F_RTIME:
2429 metric = mkf_u32(FN_AVG, swrate_avg(sv->counters.d_time, srv_samples_window));
2430 break;
2431 case ST_F_TTIME:
2432 metric = mkf_u32(FN_AVG, swrate_avg(sv->counters.t_time, srv_samples_window));
2433 break;
2434 case ST_F_QT_MAX:
2435 metric = mkf_u32(FN_MAX, sv->counters.qtime_max);
2436 break;
2437 case ST_F_CT_MAX:
2438 metric = mkf_u32(FN_MAX, sv->counters.ctime_max);
2439 break;
2440 case ST_F_RT_MAX:
2441 metric = mkf_u32(FN_MAX, sv->counters.dtime_max);
2442 break;
2443 case ST_F_TT_MAX:
2444 metric = mkf_u32(FN_MAX, sv->counters.ttime_max);
2445 break;
2446 case ST_F_ADDR:
2447 if (flags & STAT_SHLGNDS) {
2448 switch (addr_to_str(&sv->addr, str, sizeof(str))) {
2449 case AF_INET:
2450 metric = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
2451 chunk_appendf(out, "%s:%d", str, sv->svc_port);
2452 break;
2453 case AF_INET6:
2454 metric = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
2455 chunk_appendf(out, "[%s]:%d", str, sv->svc_port);
2456 break;
2457 case AF_UNIX:
2458 metric = mkf_str(FO_CONFIG|FS_SERVICE, "unix");
2459 break;
2460 case -1:
2461 metric = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
2462 chunk_strcat(out, strerror(errno));
2463 break;
2464 default: /* address family not supported */
2465 break;
2466 }
2467 }
2468 break;
2469 case ST_F_COOKIE:
2470 if (flags & STAT_SHLGNDS && sv->cookie)
2471 metric = mkf_str(FO_CONFIG|FN_NAME|FS_SERVICE, sv->cookie);
2472 break;
2473 default:
2474 /* not used for servers. If a specific metric
2475 * is requested, return an error. Otherwise continue.
2476 */
2477 if (selected_field != NULL)
2478 return 0;
2479 continue;
William Lallemand74c24fb2016-11-21 17:18:36 +01002480 }
William Dauchyd3a9a492021-01-25 17:29:03 +01002481 stats[current_field] = metric;
2482 if (selected_field != NULL)
2483 break;
William Lallemand74c24fb2016-11-21 17:18:36 +01002484 }
William Lallemand74c24fb2016-11-21 17:18:36 +01002485 return 1;
2486}
2487
2488/* Dumps a line for server <sv> and proxy <px> to the trash and uses the state
Christopher Faulet908628c2022-03-25 16:43:49 +01002489 * from conn-stream <cs>, and server state <state>. The caller is
Willy Tarreau43241ff2019-10-09 11:27:51 +02002490 * responsible for clearing the trash if needed. Returns non-zero if it emits
2491 * anything, zero otherwise.
William Lallemand74c24fb2016-11-21 17:18:36 +01002492 */
Christopher Faulet908628c2022-03-25 16:43:49 +01002493static int stats_dump_sv_stats(struct conn_stream *cs, struct proxy *px, struct server *sv)
William Lallemand74c24fb2016-11-21 17:18:36 +01002494{
Christopher Faulet908628c2022-03-25 16:43:49 +01002495 struct appctx *appctx = __cs_appctx(cs);
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02002496 struct stats_module *mod;
2497 struct field *stats = stat_l[STATS_DOMAIN_PROXY];
2498 size_t stats_count = ST_F_TOTAL_FIELDS;
2499
2500 memset(stats, 0, sizeof(struct field) * stat_count[STATS_DOMAIN_PROXY]);
William Lallemand74c24fb2016-11-21 17:18:36 +01002501
William Dauchyd3a9a492021-01-25 17:29:03 +01002502 if (!stats_fill_sv_stats(px, sv, appctx->ctx.stats.flags, stats,
2503 ST_F_TOTAL_FIELDS, NULL))
William Lallemand74c24fb2016-11-21 17:18:36 +01002504 return 0;
2505
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02002506 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
2507 void *counters;
2508
2509 if (stats_get_domain(mod->domain_flags) != STATS_DOMAIN_PROXY)
2510 continue;
2511
2512 if (!(stats_px_get_cap(mod->domain_flags) & STATS_PX_CAP_SRV)) {
2513 stats_count += mod->stats_count;
2514 continue;
2515 }
2516
2517 counters = EXTRA_COUNTERS_GET(sv->extra_counters, mod);
2518 mod->fill_stats(counters, stats + stats_count);
2519 stats_count += mod->stats_count;
2520 }
2521
2522 return stats_dump_one_line(stats, stats_count, appctx);
William Lallemand74c24fb2016-11-21 17:18:36 +01002523}
2524
William Dauchyda3b4662021-01-25 17:29:01 +01002525/* Helper to compute srv values for a given backend
William Lallemand74c24fb2016-11-21 17:18:36 +01002526 */
William Dauchyda3b4662021-01-25 17:29:01 +01002527static void stats_fill_be_stats_computesrv(struct proxy *px, int *nbup, int *nbsrv, int *totuw)
William Lallemand74c24fb2016-11-21 17:18:36 +01002528{
William Dauchyda3b4662021-01-25 17:29:01 +01002529 int nbup_tmp, nbsrv_tmp, totuw_tmp;
Willy Tarreau2fbe6942020-10-23 18:02:54 +02002530 const struct server *srv;
William Lallemand74c24fb2016-11-21 17:18:36 +01002531
William Dauchyda3b4662021-01-25 17:29:01 +01002532 nbup_tmp = nbsrv_tmp = totuw_tmp = 0;
Willy Tarreaubd715102020-10-23 22:44:30 +02002533 for (srv = px->srv; srv; srv = srv->next) {
2534 if (srv->cur_state != SRV_ST_STOPPED) {
William Dauchyda3b4662021-01-25 17:29:01 +01002535 nbup_tmp++;
Willy Tarreaubd715102020-10-23 22:44:30 +02002536 if (srv_currently_usable(srv) &&
2537 (!px->srv_act ^ !(srv->flags & SRV_F_BACKUP)))
William Dauchyda3b4662021-01-25 17:29:01 +01002538 totuw_tmp += srv->uweight;
Willy Tarreau2fbe6942020-10-23 18:02:54 +02002539 }
William Dauchyda3b4662021-01-25 17:29:01 +01002540 nbsrv_tmp++;
Willy Tarreau2fbe6942020-10-23 18:02:54 +02002541 }
2542
Willy Tarreaubd715102020-10-23 22:44:30 +02002543 HA_RWLOCK_RDLOCK(LBPRM_LOCK, &px->lbprm.lock);
2544 if (!px->srv_act && px->lbprm.fbck)
William Dauchyda3b4662021-01-25 17:29:01 +01002545 totuw_tmp = px->lbprm.fbck->uweight;
Willy Tarreaubd715102020-10-23 22:44:30 +02002546 HA_RWLOCK_RDUNLOCK(LBPRM_LOCK, &px->lbprm.lock);
2547
William Dauchyda3b4662021-01-25 17:29:01 +01002548 /* use tmp variable then assign result to make gcc happy */
2549 *nbup = nbup_tmp;
2550 *nbsrv = nbsrv_tmp;
2551 *totuw = totuw_tmp;
2552}
Willy Tarreau2fbe6942020-10-23 18:02:54 +02002553
William Dauchyda3b4662021-01-25 17:29:01 +01002554/* Fill <stats> with the backend statistics. <stats> is preallocated array of
2555 * length <len>. If <selected_field> is != NULL, only fill this one. The length
2556 * of the array must be at least ST_F_TOTAL_FIELDS. If this length is less than
2557 * this value, or if the selected field is not implemented for backends, the
2558 * function returns 0, otherwise, it returns 1. <flags> can take the value
2559 * STAT_SHLGNDS.
2560 */
2561int stats_fill_be_stats(struct proxy *px, int flags, struct field *stats, int len,
2562 enum stat_field *selected_field)
2563{
2564 enum stat_field current_field = (selected_field != NULL ? *selected_field : 0);
2565 long long be_samples_counter;
2566 unsigned int be_samples_window = TIME_STATS_SAMPLES;
2567 struct buffer *out = get_trash_chunk();
2568 int nbup, nbsrv, totuw;
2569 char *fld;
William Lallemand74c24fb2016-11-21 17:18:36 +01002570
William Dauchyda3b4662021-01-25 17:29:01 +01002571 if (len < ST_F_TOTAL_FIELDS)
2572 return 0;
William Lallemand74c24fb2016-11-21 17:18:36 +01002573
William Dauchyda3b4662021-01-25 17:29:01 +01002574 nbup = nbsrv = totuw = 0;
2575 /* some srv values compute for later if we either select all fields or
2576 * need them for one of the mentioned ones */
2577 if (selected_field == NULL || *selected_field == ST_F_STATUS ||
2578 *selected_field == ST_F_UWEIGHT)
2579 stats_fill_be_stats_computesrv(px, &nbup, &nbsrv, &totuw);
William Lallemand74c24fb2016-11-21 17:18:36 +01002580
William Dauchyda3b4662021-01-25 17:29:01 +01002581 /* same here but specific to time fields */
2582 if (selected_field == NULL || *selected_field == ST_F_QTIME ||
2583 *selected_field == ST_F_CTIME || *selected_field == ST_F_RTIME ||
2584 *selected_field == ST_F_TTIME) {
2585 be_samples_counter = (px->mode == PR_MODE_HTTP) ? px->be_counters.p.http.cum_req : px->be_counters.cum_lbconn;
2586 if (be_samples_counter < TIME_STATS_SAMPLES && be_samples_counter > 0)
2587 be_samples_window = be_samples_counter;
William Lallemand74c24fb2016-11-21 17:18:36 +01002588 }
2589
William Dauchyda3b4662021-01-25 17:29:01 +01002590 for (; current_field < ST_F_TOTAL_FIELDS; current_field++) {
2591 struct field metric = { 0 };
William Lallemand74c24fb2016-11-21 17:18:36 +01002592
William Dauchyda3b4662021-01-25 17:29:01 +01002593 switch (current_field) {
2594 case ST_F_PXNAME:
2595 metric = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
2596 break;
2597 case ST_F_SVNAME:
2598 metric = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, "BACKEND");
2599 break;
2600 case ST_F_MODE:
2601 metric = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
2602 break;
2603 case ST_F_QCUR:
Willy Tarreau7f3c1df2021-06-18 09:22:21 +02002604 metric = mkf_u32(0, px->queue.length);
William Dauchyda3b4662021-01-25 17:29:01 +01002605 break;
2606 case ST_F_QMAX:
2607 metric = mkf_u32(FN_MAX, px->be_counters.nbpend_max);
2608 break;
2609 case ST_F_SCUR:
2610 metric = mkf_u32(0, px->beconn);
2611 break;
2612 case ST_F_SMAX:
2613 metric = mkf_u32(FN_MAX, px->be_counters.conn_max);
2614 break;
2615 case ST_F_SLIM:
2616 metric = mkf_u32(FO_CONFIG|FN_LIMIT, px->fullconn);
2617 break;
2618 case ST_F_STOT:
2619 metric = mkf_u64(FN_COUNTER, px->be_counters.cum_conn);
2620 break;
2621 case ST_F_BIN:
2622 metric = mkf_u64(FN_COUNTER, px->be_counters.bytes_in);
2623 break;
2624 case ST_F_BOUT:
2625 metric = mkf_u64(FN_COUNTER, px->be_counters.bytes_out);
2626 break;
2627 case ST_F_DREQ:
2628 metric = mkf_u64(FN_COUNTER, px->be_counters.denied_req);
2629 break;
2630 case ST_F_DRESP:
2631 metric = mkf_u64(FN_COUNTER, px->be_counters.denied_resp);
2632 break;
2633 case ST_F_ECON:
2634 metric = mkf_u64(FN_COUNTER, px->be_counters.failed_conns);
2635 break;
2636 case ST_F_ERESP:
2637 metric = mkf_u64(FN_COUNTER, px->be_counters.failed_resp);
2638 break;
2639 case ST_F_WRETR:
2640 metric = mkf_u64(FN_COUNTER, px->be_counters.retries);
2641 break;
2642 case ST_F_WREDIS:
2643 metric = mkf_u64(FN_COUNTER, px->be_counters.redispatches);
2644 break;
2645 case ST_F_WREW:
2646 metric = mkf_u64(FN_COUNTER, px->be_counters.failed_rewrites);
2647 break;
2648 case ST_F_EINT:
2649 metric = mkf_u64(FN_COUNTER, px->be_counters.internal_errors);
2650 break;
2651 case ST_F_CONNECT:
2652 metric = mkf_u64(FN_COUNTER, px->be_counters.connect);
2653 break;
2654 case ST_F_REUSE:
2655 metric = mkf_u64(FN_COUNTER, px->be_counters.reuse);
2656 break;
2657 case ST_F_STATUS:
2658 fld = chunk_newstr(out);
2659 chunk_appendf(out, "%s", (px->lbprm.tot_weight > 0 || !px->srv) ? "UP" : "DOWN");
2660 if (flags & (STAT_HIDE_MAINT|STAT_HIDE_DOWN))
2661 chunk_appendf(out, " (%d/%d)", nbup, nbsrv);
2662 metric = mkf_str(FO_STATUS, fld);
2663 break;
William Dauchy42d7c402021-11-07 10:18:47 +01002664 case ST_F_AGG_SRV_CHECK_STATUS:
2665 metric = mkf_u32(FN_GAUGE, 0);
2666 break;
William Dauchyda3b4662021-01-25 17:29:01 +01002667 case ST_F_WEIGHT:
2668 metric = mkf_u32(FN_AVG, (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv);
2669 break;
2670 case ST_F_UWEIGHT:
2671 metric = mkf_u32(FN_AVG, totuw);
2672 break;
2673 case ST_F_ACT:
2674 metric = mkf_u32(0, px->srv_act);
2675 break;
2676 case ST_F_BCK:
2677 metric = mkf_u32(0, px->srv_bck);
2678 break;
2679 case ST_F_CHKDOWN:
2680 metric = mkf_u64(FN_COUNTER, px->down_trans);
2681 break;
2682 case ST_F_LASTCHG:
2683 metric = mkf_u32(FN_AGE, now.tv_sec - px->last_change);
2684 break;
2685 case ST_F_DOWNTIME:
2686 if (px->srv)
2687 metric = mkf_u32(FN_COUNTER, be_downtime(px));
2688 break;
2689 case ST_F_PID:
Willy Tarreaue8422bf2021-06-15 09:08:18 +02002690 metric = mkf_u32(FO_KEY, 1);
William Dauchyda3b4662021-01-25 17:29:01 +01002691 break;
2692 case ST_F_IID:
2693 metric = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
2694 break;
2695 case ST_F_SID:
2696 metric = mkf_u32(FO_KEY|FS_SERVICE, 0);
2697 break;
2698 case ST_F_LBTOT:
2699 metric = mkf_u64(FN_COUNTER, px->be_counters.cum_lbconn);
2700 break;
2701 case ST_F_TYPE:
2702 metric = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_BE);
2703 break;
2704 case ST_F_RATE:
2705 metric = mkf_u32(0, read_freq_ctr(&px->be_sess_per_sec));
2706 break;
2707 case ST_F_RATE_MAX:
2708 metric = mkf_u32(0, px->be_counters.sps_max);
2709 break;
2710 case ST_F_COOKIE:
2711 if (flags & STAT_SHLGNDS && px->cookie_name)
2712 metric = mkf_str(FO_CONFIG|FN_NAME|FS_SERVICE, px->cookie_name);
2713 break;
2714 case ST_F_ALGO:
2715 if (flags & STAT_SHLGNDS)
2716 metric = mkf_str(FO_CONFIG|FS_SERVICE, backend_lb_algo_str(px->lbprm.algo & BE_LB_ALGO));
2717 break;
2718 case ST_F_REQ_TOT:
2719 if (px->mode == PR_MODE_HTTP)
2720 metric = mkf_u64(FN_COUNTER, px->be_counters.p.http.cum_req);
2721 break;
2722 case ST_F_HRSP_1XX:
2723 if (px->mode == PR_MODE_HTTP)
2724 metric = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[1]);
2725 break;
2726 case ST_F_HRSP_2XX:
2727 if (px->mode == PR_MODE_HTTP)
2728 metric = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[2]);
2729 break;
2730 case ST_F_HRSP_3XX:
2731 if (px->mode == PR_MODE_HTTP)
2732 metric = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[3]);
2733 break;
2734 case ST_F_HRSP_4XX:
2735 if (px->mode == PR_MODE_HTTP)
2736 metric = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[4]);
2737 break;
2738 case ST_F_HRSP_5XX:
2739 if (px->mode == PR_MODE_HTTP)
2740 metric = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[5]);
2741 break;
2742 case ST_F_HRSP_OTHER:
2743 if (px->mode == PR_MODE_HTTP)
2744 metric = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[0]);
2745 break;
2746 case ST_F_CACHE_LOOKUPS:
2747 if (px->mode == PR_MODE_HTTP)
2748 metric = mkf_u64(FN_COUNTER, px->be_counters.p.http.cache_lookups);
2749 break;
2750 case ST_F_CACHE_HITS:
2751 if (px->mode == PR_MODE_HTTP)
2752 metric = mkf_u64(FN_COUNTER, px->be_counters.p.http.cache_hits);
2753 break;
2754 case ST_F_CLI_ABRT:
2755 metric = mkf_u64(FN_COUNTER, px->be_counters.cli_aborts);
2756 break;
2757 case ST_F_SRV_ABRT:
2758 metric = mkf_u64(FN_COUNTER, px->be_counters.srv_aborts);
2759 break;
2760 case ST_F_COMP_IN:
2761 metric = mkf_u64(FN_COUNTER, px->be_counters.comp_in);
2762 break;
2763 case ST_F_COMP_OUT:
2764 metric = mkf_u64(FN_COUNTER, px->be_counters.comp_out);
2765 break;
2766 case ST_F_COMP_BYP:
2767 metric = mkf_u64(FN_COUNTER, px->be_counters.comp_byp);
2768 break;
2769 case ST_F_COMP_RSP:
2770 metric = mkf_u64(FN_COUNTER, px->be_counters.p.http.comp_rsp);
2771 break;
2772 case ST_F_LASTSESS:
2773 metric = mkf_s32(FN_AGE, be_lastsession(px));
2774 break;
2775 case ST_F_QTIME:
2776 metric = mkf_u32(FN_AVG, swrate_avg(px->be_counters.q_time, be_samples_window));
2777 break;
2778 case ST_F_CTIME:
2779 metric = mkf_u32(FN_AVG, swrate_avg(px->be_counters.c_time, be_samples_window));
2780 break;
2781 case ST_F_RTIME:
2782 metric = mkf_u32(FN_AVG, swrate_avg(px->be_counters.d_time, be_samples_window));
2783 break;
2784 case ST_F_TTIME:
2785 metric = mkf_u32(FN_AVG, swrate_avg(px->be_counters.t_time, be_samples_window));
2786 break;
2787 case ST_F_QT_MAX:
2788 metric = mkf_u32(FN_MAX, px->be_counters.qtime_max);
2789 break;
2790 case ST_F_CT_MAX:
2791 metric = mkf_u32(FN_MAX, px->be_counters.ctime_max);
2792 break;
2793 case ST_F_RT_MAX:
2794 metric = mkf_u32(FN_MAX, px->be_counters.dtime_max);
2795 break;
2796 case ST_F_TT_MAX:
2797 metric = mkf_u32(FN_MAX, px->be_counters.ttime_max);
2798 break;
2799 default:
2800 /* not used for backends. If a specific metric
2801 * is requested, return an error. Otherwise continue.
2802 */
2803 if (selected_field != NULL)
2804 return 0;
2805 continue;
2806 }
2807 stats[current_field] = metric;
2808 if (selected_field != NULL)
2809 break;
2810 }
William Lallemand74c24fb2016-11-21 17:18:36 +01002811 return 1;
2812}
2813
2814/* Dumps a line for backend <px> to the trash for and uses the state from stream
Willy Tarreau43241ff2019-10-09 11:27:51 +02002815 * interface <si>. The caller is responsible for clearing the trash if needed.
2816 * Returns non-zero if it emits anything, zero otherwise.
William Lallemand74c24fb2016-11-21 17:18:36 +01002817 */
Christopher Faulet908628c2022-03-25 16:43:49 +01002818static int stats_dump_be_stats(struct conn_stream *cs, struct proxy *px)
William Lallemand74c24fb2016-11-21 17:18:36 +01002819{
Christopher Faulet908628c2022-03-25 16:43:49 +01002820 struct appctx *appctx = __cs_appctx(cs);
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02002821 struct field *stats = stat_l[STATS_DOMAIN_PROXY];
2822 struct stats_module *mod;
2823 size_t stats_count = ST_F_TOTAL_FIELDS;
William Lallemand74c24fb2016-11-21 17:18:36 +01002824
2825 if (!(px->cap & PR_CAP_BE))
2826 return 0;
2827
2828 if ((appctx->ctx.stats.flags & STAT_BOUND) && !(appctx->ctx.stats.type & (1 << STATS_TYPE_BE)))
2829 return 0;
2830
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02002831 memset(stats, 0, sizeof(struct field) * stat_count[STATS_DOMAIN_PROXY]);
2832
William Dauchyda3b4662021-01-25 17:29:01 +01002833 if (!stats_fill_be_stats(px, appctx->ctx.stats.flags, stats, ST_F_TOTAL_FIELDS, NULL))
William Lallemand74c24fb2016-11-21 17:18:36 +01002834 return 0;
2835
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02002836 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
2837 struct extra_counters *counters;
2838
2839 if (stats_get_domain(mod->domain_flags) != STATS_DOMAIN_PROXY)
2840 continue;
2841
2842 if (!(stats_px_get_cap(mod->domain_flags) & STATS_PX_CAP_BE)) {
2843 stats_count += mod->stats_count;
2844 continue;
2845 }
2846
2847 counters = EXTRA_COUNTERS_GET(px->extra_counters_be, mod);
2848 mod->fill_stats(counters, stats + stats_count);
2849 stats_count += mod->stats_count;
2850 }
2851
2852 return stats_dump_one_line(stats, stats_count, appctx);
William Lallemand74c24fb2016-11-21 17:18:36 +01002853}
2854
2855/* Dumps the HTML table header for proxy <px> to the trash for and uses the state from
Christopher Faulet908628c2022-03-25 16:43:49 +01002856 * conn-stream <cs> and per-uri parameters <uri>. The caller is responsible
William Lallemand74c24fb2016-11-21 17:18:36 +01002857 * for clearing the trash if needed.
2858 */
Christopher Faulet908628c2022-03-25 16:43:49 +01002859static void stats_dump_html_px_hdr(struct conn_stream *cs, struct proxy *px)
William Lallemand74c24fb2016-11-21 17:18:36 +01002860{
Christopher Faulet908628c2022-03-25 16:43:49 +01002861 struct appctx *appctx = __cs_appctx(cs);
William Lallemand74c24fb2016-11-21 17:18:36 +01002862 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02002863 struct stats_module *mod;
2864 int stats_module_len = 0;
William Lallemand74c24fb2016-11-21 17:18:36 +01002865
2866 if (px->cap & PR_CAP_BE && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
2867 /* A form to enable/disable this proxy servers */
2868
2869 /* scope_txt = search pattern + search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
2870 scope_txt[0] = 0;
2871 if (appctx->ctx.stats.scope_len) {
Christopher Faulet908628c2022-03-25 16:43:49 +01002872 const char *scope_ptr = stats_scope_ptr(appctx, cs);
Christopher Fauleted7a0662019-01-14 11:07:34 +01002873
William Lallemand74c24fb2016-11-21 17:18:36 +01002874 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Christopher Fauleted7a0662019-01-14 11:07:34 +01002875 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), scope_ptr, appctx->ctx.stats.scope_len);
William Lallemand74c24fb2016-11-21 17:18:36 +01002876 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
2877 }
2878
2879 chunk_appendf(&trash,
2880 "<form method=\"post\">");
2881 }
2882
2883 /* print a new table */
2884 chunk_appendf(&trash,
2885 "<table class=\"tbl\" width=\"100%%\">\n"
2886 "<tr class=\"titre\">"
2887 "<th class=\"pxname\" width=\"10%%\">");
2888
2889 chunk_appendf(&trash,
2890 "<a name=\"%s\"></a>%s"
2891 "<a class=px href=\"#%s\">%s</a>",
2892 px->id,
Willy Tarreau676c29e2019-10-09 10:50:01 +02002893 (appctx->ctx.stats.flags & STAT_SHLGNDS) ? "<u>":"",
William Lallemand74c24fb2016-11-21 17:18:36 +01002894 px->id, px->id);
2895
Willy Tarreau676c29e2019-10-09 10:50:01 +02002896 if (appctx->ctx.stats.flags & STAT_SHLGNDS) {
William Lallemand74c24fb2016-11-21 17:18:36 +01002897 /* cap, mode, id */
2898 chunk_appendf(&trash, "<div class=tips>cap: %s, mode: %s, id: %d",
2899 proxy_cap_str(px->cap), proxy_mode_str(px->mode),
2900 px->uuid);
2901 chunk_appendf(&trash, "</div>");
2902 }
2903
2904 chunk_appendf(&trash,
2905 "%s</th>"
2906 "<th class=\"%s\" width=\"90%%\">%s</th>"
2907 "</tr>\n"
2908 "</table>\n"
2909 "<table class=\"tbl\" width=\"100%%\">\n"
2910 "<tr class=\"titre\">",
Willy Tarreau676c29e2019-10-09 10:50:01 +02002911 (appctx->ctx.stats.flags & STAT_SHLGNDS) ? "</u>":"",
William Lallemand74c24fb2016-11-21 17:18:36 +01002912 px->desc ? "desc" : "empty", px->desc ? px->desc : "");
2913
Christopher Fauletbc271ec2019-12-02 11:29:04 +01002914 if (appctx->ctx.stats.flags & STAT_ADMIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +01002915 /* Column heading for Enable or Disable server */
Christopher Fauletbc271ec2019-12-02 11:29:04 +01002916 if ((px->cap & PR_CAP_BE) && px->srv)
2917 chunk_appendf(&trash,
2918 "<th rowspan=2 width=1><input type=\"checkbox\" "
2919 "onclick=\"for(c in document.getElementsByClassName('%s-checkbox')) "
2920 "document.getElementsByClassName('%s-checkbox').item(c).checked = this.checked\"></th>",
2921 px->id,
2922 px->id);
2923 else
2924 chunk_appendf(&trash, "<th rowspan=2></th>");
William Lallemand74c24fb2016-11-21 17:18:36 +01002925 }
2926
2927 chunk_appendf(&trash,
2928 "<th rowspan=2></th>"
2929 "<th colspan=3>Queue</th>"
2930 "<th colspan=3>Session rate</th><th colspan=6>Sessions</th>"
2931 "<th colspan=2>Bytes</th><th colspan=2>Denied</th>"
2932 "<th colspan=3>Errors</th><th colspan=2>Warnings</th>"
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02002933 "<th colspan=9>Server</th>");
2934
2935 if (appctx->ctx.stats.flags & STAT_SHMODULES) {
2936 // calculate the count of module for colspan attribute
2937 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
2938 ++stats_module_len;
2939 }
2940 chunk_appendf(&trash, "<th colspan=%d>Extra modules</th>",
2941 stats_module_len);
2942 }
2943
2944 chunk_appendf(&trash,
William Lallemand74c24fb2016-11-21 17:18:36 +01002945 "</tr>\n"
2946 "<tr class=\"titre\">"
2947 "<th>Cur</th><th>Max</th><th>Limit</th>"
2948 "<th>Cur</th><th>Max</th><th>Limit</th><th>Cur</th><th>Max</th>"
2949 "<th>Limit</th><th>Total</th><th>LbTot</th><th>Last</th><th>In</th><th>Out</th>"
2950 "<th>Req</th><th>Resp</th><th>Req</th><th>Conn</th>"
2951 "<th>Resp</th><th>Retr</th><th>Redis</th>"
2952 "<th>Status</th><th>LastChk</th><th>Wght</th><th>Act</th>"
2953 "<th>Bck</th><th>Chk</th><th>Dwn</th><th>Dwntme</th>"
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02002954 "<th>Thrtle</th>\n");
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02002955
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02002956 if (appctx->ctx.stats.flags & STAT_SHMODULES) {
2957 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
2958 chunk_appendf(&trash, "<th>%s</th>", mod->name);
2959 }
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02002960 }
2961
2962 chunk_appendf(&trash, "</tr>");
William Lallemand74c24fb2016-11-21 17:18:36 +01002963}
2964
2965/* Dumps the HTML table trailer for proxy <px> to the trash for and uses the state from
Christopher Faulet908628c2022-03-25 16:43:49 +01002966 * conn_stream <cs>. The caller is responsible for clearing the trash if needed.
William Lallemand74c24fb2016-11-21 17:18:36 +01002967 */
Christopher Faulet908628c2022-03-25 16:43:49 +01002968static void stats_dump_html_px_end(struct conn_stream *cs, struct proxy *px)
William Lallemand74c24fb2016-11-21 17:18:36 +01002969{
Christopher Faulet908628c2022-03-25 16:43:49 +01002970 struct appctx *appctx = __cs_appctx(cs);
Christopher Faulet693b23b2022-02-28 09:09:05 +01002971
William Lallemand74c24fb2016-11-21 17:18:36 +01002972 chunk_appendf(&trash, "</table>");
2973
2974 if ((px->cap & PR_CAP_BE) && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
2975 /* close the form used to enable/disable this proxy servers */
2976 chunk_appendf(&trash,
2977 "Choose the action to perform on the checked servers : "
2978 "<select name=action>"
2979 "<option value=\"\"></option>"
2980 "<option value=\"ready\">Set state to READY</option>"
2981 "<option value=\"drain\">Set state to DRAIN</option>"
2982 "<option value=\"maint\">Set state to MAINT</option>"
2983 "<option value=\"dhlth\">Health: disable checks</option>"
2984 "<option value=\"ehlth\">Health: enable checks</option>"
2985 "<option value=\"hrunn\">Health: force UP</option>"
2986 "<option value=\"hnolb\">Health: force NOLB</option>"
2987 "<option value=\"hdown\">Health: force DOWN</option>"
2988 "<option value=\"dagent\">Agent: disable checks</option>"
2989 "<option value=\"eagent\">Agent: enable checks</option>"
2990 "<option value=\"arunn\">Agent: force UP</option>"
2991 "<option value=\"adown\">Agent: force DOWN</option>"
2992 "<option value=\"shutdown\">Kill Sessions</option>"
2993 "</select>"
2994 "<input type=\"hidden\" name=\"b\" value=\"#%d\">"
2995 "&nbsp;<input type=\"submit\" value=\"Apply\">"
2996 "</form>",
2997 px->uuid);
2998 }
2999
3000 chunk_appendf(&trash, "<p>\n");
3001}
3002
3003/*
Christopher Faulet908628c2022-03-25 16:43:49 +01003004 * Dumps statistics for a proxy. The output is sent to the conn-stream's
William Lallemand74c24fb2016-11-21 17:18:36 +01003005 * input buffer. Returns 0 if it had to stop dumping data because of lack of
3006 * buffer space, or non-zero if everything completed. This function is used
3007 * both by the CLI and the HTTP entry points, and is able to dump the output
3008 * in HTML or CSV formats. If the later, <uri> must be NULL.
3009 */
Christopher Faulet908628c2022-03-25 16:43:49 +01003010int stats_dump_proxy_to_buffer(struct conn_stream *cs, struct htx *htx,
Christopher Fauletef779222018-10-31 08:47:01 +01003011 struct proxy *px, struct uri_auth *uri)
William Lallemand74c24fb2016-11-21 17:18:36 +01003012{
Christopher Faulet908628c2022-03-25 16:43:49 +01003013 struct appctx *appctx = __cs_appctx(cs);
3014 struct stream *s = __cs_strm(cs);
3015 struct channel *rep = cs_ic(cs);
William Lallemand74c24fb2016-11-21 17:18:36 +01003016 struct server *sv, *svs; /* server and server-state, server-state=server or server->track */
3017 struct listener *l;
William Lallemand74c24fb2016-11-21 17:18:36 +01003018
3019 chunk_reset(&trash);
3020
3021 switch (appctx->ctx.stats.px_st) {
3022 case STAT_PX_ST_INIT:
3023 /* we are on a new proxy */
3024 if (uri && uri->scope) {
3025 /* we have a limited scope, we have to check the proxy name */
3026 struct stat_scope *scope;
3027 int len;
3028
3029 len = strlen(px->id);
3030 scope = uri->scope;
3031
3032 while (scope) {
3033 /* match exact proxy name */
3034 if (scope->px_len == len && !memcmp(px->id, scope->px_id, len))
3035 break;
3036
3037 /* match '.' which means 'self' proxy */
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003038 if (strcmp(scope->px_id, ".") == 0 && px == s->be)
William Lallemand74c24fb2016-11-21 17:18:36 +01003039 break;
3040 scope = scope->next;
3041 }
3042
3043 /* proxy name not found : don't dump anything */
3044 if (scope == NULL)
3045 return 1;
3046 }
3047
3048 /* if the user has requested a limited output and the proxy
3049 * name does not match, skip it.
3050 */
Christopher Fauleted7a0662019-01-14 11:07:34 +01003051 if (appctx->ctx.stats.scope_len) {
Christopher Faulet908628c2022-03-25 16:43:49 +01003052 const char *scope_ptr = stats_scope_ptr(appctx, cs);
Christopher Fauleted7a0662019-01-14 11:07:34 +01003053
3054 if (strnistr(px->id, strlen(px->id), scope_ptr, appctx->ctx.stats.scope_len) == NULL)
3055 return 1;
3056 }
William Lallemand74c24fb2016-11-21 17:18:36 +01003057
3058 if ((appctx->ctx.stats.flags & STAT_BOUND) &&
3059 (appctx->ctx.stats.iid != -1) &&
3060 (px->uuid != appctx->ctx.stats.iid))
3061 return 1;
3062
3063 appctx->ctx.stats.px_st = STAT_PX_ST_TH;
3064 /* fall through */
3065
3066 case STAT_PX_ST_TH:
3067 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Christopher Faulet908628c2022-03-25 16:43:49 +01003068 stats_dump_html_px_hdr(cs, px);
Christopher Fauletef779222018-10-31 08:47:01 +01003069 if (!stats_putchk(rep, htx, &trash))
3070 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01003071 }
3072
3073 appctx->ctx.stats.px_st = STAT_PX_ST_FE;
3074 /* fall through */
3075
3076 case STAT_PX_ST_FE:
3077 /* print the frontend */
Christopher Faulet908628c2022-03-25 16:43:49 +01003078 if (stats_dump_fe_stats(cs, px)) {
Christopher Fauletef779222018-10-31 08:47:01 +01003079 if (!stats_putchk(rep, htx, &trash))
3080 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01003081 }
3082
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +02003083 appctx->ctx.stats.obj2 = px->conf.listeners.n;
William Lallemand74c24fb2016-11-21 17:18:36 +01003084 appctx->ctx.stats.px_st = STAT_PX_ST_LI;
3085 /* fall through */
3086
3087 case STAT_PX_ST_LI:
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +02003088 /* obj2 points to listeners list as initialized above */
3089 for (; appctx->ctx.stats.obj2 != &px->conf.listeners; appctx->ctx.stats.obj2 = l->by_fe.n) {
Christopher Fauletef779222018-10-31 08:47:01 +01003090 if (htx) {
3091 if (htx_almost_full(htx))
3092 goto full;
3093 }
3094 else {
3095 if (buffer_almost_full(&rep->buf))
3096 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01003097 }
3098
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +02003099 l = LIST_ELEM(appctx->ctx.stats.obj2, struct listener *, by_fe);
William Lallemand74c24fb2016-11-21 17:18:36 +01003100 if (!l->counters)
3101 continue;
3102
3103 if (appctx->ctx.stats.flags & STAT_BOUND) {
3104 if (!(appctx->ctx.stats.type & (1 << STATS_TYPE_SO)))
3105 break;
3106
3107 if (appctx->ctx.stats.sid != -1 && l->luid != appctx->ctx.stats.sid)
3108 continue;
3109 }
3110
3111 /* print the frontend */
Christopher Faulet908628c2022-03-25 16:43:49 +01003112 if (stats_dump_li_stats(cs, px, l)) {
Christopher Fauletef779222018-10-31 08:47:01 +01003113 if (!stats_putchk(rep, htx, &trash))
3114 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01003115 }
3116 }
3117
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +02003118 appctx->ctx.stats.obj2 = px->srv; /* may be NULL */
William Lallemand74c24fb2016-11-21 17:18:36 +01003119 appctx->ctx.stats.px_st = STAT_PX_ST_SV;
3120 /* fall through */
3121
3122 case STAT_PX_ST_SV:
Amaury Denoyelle0a8d05d2021-08-25 14:39:53 +02003123 /* obj2 points to servers list as initialized above.
3124 *
Amaury Denoyellebc2ebfa2021-08-25 15:34:53 +02003125 * A server may be removed during the stats dumping.
Amaury Denoyelle0a8d05d2021-08-25 14:39:53 +02003126 * Temporarily increment its refcount to prevent its
3127 * anticipated cleaning. Call free_server to release it.
3128 */
3129 for (; appctx->ctx.stats.obj2 != NULL;
Amaury Denoyellebc2ebfa2021-08-25 15:34:53 +02003130 appctx->ctx.stats.obj2 = srv_drop(sv)) {
Amaury Denoyelle0a8d05d2021-08-25 14:39:53 +02003131
3132 sv = appctx->ctx.stats.obj2;
Amaury Denoyellebc2ebfa2021-08-25 15:34:53 +02003133 srv_take(sv);
Amaury Denoyelle0a8d05d2021-08-25 14:39:53 +02003134
Christopher Fauletef779222018-10-31 08:47:01 +01003135 if (htx) {
3136 if (htx_almost_full(htx))
3137 goto full;
3138 }
3139 else {
3140 if (buffer_almost_full(&rep->buf))
3141 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01003142 }
3143
William Lallemand74c24fb2016-11-21 17:18:36 +01003144 if (appctx->ctx.stats.flags & STAT_BOUND) {
Amaury Denoyelle0a8d05d2021-08-25 14:39:53 +02003145 if (!(appctx->ctx.stats.type & (1 << STATS_TYPE_SV))) {
Amaury Denoyellebc2ebfa2021-08-25 15:34:53 +02003146 srv_drop(sv);
William Lallemand74c24fb2016-11-21 17:18:36 +01003147 break;
Amaury Denoyelle0a8d05d2021-08-25 14:39:53 +02003148 }
William Lallemand74c24fb2016-11-21 17:18:36 +01003149
3150 if (appctx->ctx.stats.sid != -1 && sv->puid != appctx->ctx.stats.sid)
3151 continue;
3152 }
3153
Willy Tarreau3e320362020-10-23 17:28:57 +02003154 /* do not report disabled servers */
3155 if (appctx->ctx.stats.flags & STAT_HIDE_MAINT &&
3156 sv->cur_admin & SRV_ADMF_MAINT) {
3157 continue;
3158 }
3159
William Lallemand74c24fb2016-11-21 17:18:36 +01003160 svs = sv;
3161 while (svs->track)
3162 svs = svs->track;
3163
3164 /* do not report servers which are DOWN and not changing state */
3165 if ((appctx->ctx.stats.flags & STAT_HIDE_DOWN) &&
Emeric Brun52a91d32017-08-31 14:41:55 +02003166 ((sv->cur_admin & SRV_ADMF_MAINT) || /* server is in maintenance */
3167 (sv->cur_state == SRV_ST_STOPPED && /* server is down */
William Lallemand74c24fb2016-11-21 17:18:36 +01003168 (!((svs->agent.state | svs->check.state) & CHK_ST_ENABLED) ||
3169 ((svs->agent.state & CHK_ST_ENABLED) && !svs->agent.health) ||
3170 ((svs->check.state & CHK_ST_ENABLED) && !svs->check.health))))) {
3171 continue;
3172 }
3173
Christopher Faulet908628c2022-03-25 16:43:49 +01003174 if (stats_dump_sv_stats(cs, px, sv)) {
Christopher Fauletef779222018-10-31 08:47:01 +01003175 if (!stats_putchk(rep, htx, &trash))
3176 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01003177 }
3178 } /* for sv */
3179
3180 appctx->ctx.stats.px_st = STAT_PX_ST_BE;
3181 /* fall through */
3182
3183 case STAT_PX_ST_BE:
3184 /* print the backend */
Christopher Faulet908628c2022-03-25 16:43:49 +01003185 if (stats_dump_be_stats(cs, px)) {
Christopher Fauletef779222018-10-31 08:47:01 +01003186 if (!stats_putchk(rep, htx, &trash))
3187 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01003188 }
3189
3190 appctx->ctx.stats.px_st = STAT_PX_ST_END;
3191 /* fall through */
3192
3193 case STAT_PX_ST_END:
3194 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Christopher Faulet908628c2022-03-25 16:43:49 +01003195 stats_dump_html_px_end(cs, px);
Christopher Fauletef779222018-10-31 08:47:01 +01003196 if (!stats_putchk(rep, htx, &trash))
3197 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01003198 }
3199
3200 appctx->ctx.stats.px_st = STAT_PX_ST_FIN;
3201 /* fall through */
3202
3203 case STAT_PX_ST_FIN:
3204 return 1;
3205
3206 default:
3207 /* unknown state, we should put an abort() here ! */
3208 return 1;
3209 }
Christopher Fauletef779222018-10-31 08:47:01 +01003210
3211 full:
Christopher Fauleta0bdec32022-04-04 07:51:21 +02003212 cs_rx_room_blk(cs);
Christopher Fauletef779222018-10-31 08:47:01 +01003213 return 0;
William Lallemand74c24fb2016-11-21 17:18:36 +01003214}
3215
3216/* Dumps the HTTP stats head block to the trash for and uses the per-uri
3217 * parameters <uri>. The caller is responsible for clearing the trash if needed.
3218 */
Willy Tarreau676c29e2019-10-09 10:50:01 +02003219static void stats_dump_html_head(struct appctx *appctx, struct uri_auth *uri)
William Lallemand74c24fb2016-11-21 17:18:36 +01003220{
3221 /* WARNING! This must fit in the first buffer !!! */
3222 chunk_appendf(&trash,
3223 "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"\n"
3224 "\"http://www.w3.org/TR/html4/loose.dtd\">\n"
3225 "<html><head><title>Statistics Report for " PRODUCT_NAME "%s%s</title>\n"
Thierry Fournierb6b1cde2021-09-24 00:03:07 +02003226 "<link rel=\"icon\" href=\"data:,\">\n"
William Lallemand74c24fb2016-11-21 17:18:36 +01003227 "<meta http-equiv=\"content-type\" content=\"text/html; charset=iso-8859-1\">\n"
3228 "<style type=\"text/css\"><!--\n"
3229 "body {"
3230 " font-family: arial, helvetica, sans-serif;"
3231 " font-size: 12px;"
3232 " font-weight: normal;"
3233 " color: black;"
3234 " background: white;"
3235 "}\n"
3236 "th,td {"
3237 " font-size: 10px;"
3238 "}\n"
3239 "h1 {"
3240 " font-size: x-large;"
3241 " margin-bottom: 0.5em;"
3242 "}\n"
3243 "h2 {"
3244 " font-family: helvetica, arial;"
3245 " font-size: x-large;"
3246 " font-weight: bold;"
3247 " font-style: italic;"
3248 " color: #6020a0;"
3249 " margin-top: 0em;"
3250 " margin-bottom: 0em;"
3251 "}\n"
3252 "h3 {"
3253 " font-family: helvetica, arial;"
3254 " font-size: 16px;"
3255 " font-weight: bold;"
3256 " color: #b00040;"
3257 " background: #e8e8d0;"
3258 " margin-top: 0em;"
3259 " margin-bottom: 0em;"
3260 "}\n"
3261 "li {"
3262 " margin-top: 0.25em;"
3263 " margin-right: 2em;"
3264 "}\n"
3265 ".hr {margin-top: 0.25em;"
3266 " border-color: black;"
3267 " border-bottom-style: solid;"
3268 "}\n"
3269 ".titre {background: #20D0D0;color: #000000; font-weight: bold; text-align: center;}\n"
3270 ".total {background: #20D0D0;color: #ffff80;}\n"
3271 ".frontend {background: #e8e8d0;}\n"
3272 ".socket {background: #d0d0d0;}\n"
3273 ".backend {background: #e8e8d0;}\n"
3274 ".active_down {background: #ff9090;}\n"
3275 ".active_going_up {background: #ffd020;}\n"
3276 ".active_going_down {background: #ffffa0;}\n"
3277 ".active_up {background: #c0ffc0;}\n"
3278 ".active_nolb {background: #20a0ff;}\n"
3279 ".active_draining {background: #20a0FF;}\n"
3280 ".active_no_check {background: #e0e0e0;}\n"
3281 ".backup_down {background: #ff9090;}\n"
3282 ".backup_going_up {background: #ff80ff;}\n"
3283 ".backup_going_down {background: #c060ff;}\n"
3284 ".backup_up {background: #b0d0ff;}\n"
3285 ".backup_nolb {background: #90b0e0;}\n"
3286 ".backup_draining {background: #cc9900;}\n"
3287 ".backup_no_check {background: #e0e0e0;}\n"
3288 ".maintain {background: #c07820;}\n"
3289 ".rls {letter-spacing: 0.2em; margin-right: 1px;}\n" /* right letter spacing (used for grouping digits) */
3290 "\n"
3291 "a.px:link {color: #ffff40; text-decoration: none;}"
3292 "a.px:visited {color: #ffff40; text-decoration: none;}"
3293 "a.px:hover {color: #ffffff; text-decoration: none;}"
3294 "a.lfsb:link {color: #000000; text-decoration: none;}"
3295 "a.lfsb:visited {color: #000000; text-decoration: none;}"
3296 "a.lfsb:hover {color: #505050; text-decoration: none;}"
3297 "\n"
3298 "table.tbl { border-collapse: collapse; border-style: none;}\n"
3299 "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"
3300 "table.tbl td.ac { text-align: center;}\n"
3301 "table.tbl th { border-width: 1px; border-style: solid solid solid solid; border-color: gray;}\n"
3302 "table.tbl th.pxname { background: #b00040; color: #ffff40; font-weight: bold; border-style: solid solid none solid; padding: 2px 3px; white-space: nowrap;}\n"
3303 "table.tbl th.empty { border-style: none; empty-cells: hide; background: white;}\n"
3304 "table.tbl th.desc { background: white; border-style: solid solid none solid; text-align: left; padding: 2px 3px;}\n"
3305 "\n"
3306 "table.lgd { border-collapse: collapse; border-width: 1px; border-style: none none none solid; border-color: black;}\n"
3307 "table.lgd td { border-width: 1px; border-style: solid solid solid solid; border-color: gray; padding: 2px;}\n"
3308 "table.lgd td.noborder { border-style: none; padding: 2px; white-space: nowrap;}\n"
3309 "table.det { border-collapse: collapse; border-style: none; }\n"
3310 "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"
3311 "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"
3312 "u {text-decoration:none; border-bottom: 1px dotted black;}\n"
Thierry Fournierb6b1cde2021-09-24 00:03:07 +02003313 "div.tips {\n"
3314 " display:block;\n"
3315 " visibility:hidden;\n"
3316 " z-index:2147483647;\n"
3317 " position:absolute;\n"
3318 " padding:2px 4px 3px;\n"
3319 " background:#f0f060; color:#000000;\n"
3320 " border:1px solid #7040c0;\n"
3321 " white-space:nowrap;\n"
3322 " font-style:normal;font-size:11px;font-weight:normal;\n"
3323 " -moz-border-radius:3px;-webkit-border-radius:3px;border-radius:3px;\n"
3324 " -moz-box-shadow:gray 2px 2px 3px;-webkit-box-shadow:gray 2px 2px 3px;box-shadow:gray 2px 2px 3px;\n"
3325 "}\n"
3326 "u:hover div.tips {visibility:visible;}\n"
Thierry Fourniere9ed63e2021-09-23 23:59:49 +02003327 "@media (prefers-color-scheme: dark) {\n"
3328 " body { font-family: arial, helvetica, sans-serif; font-size: 12px; font-weight: normal; color: #e8e6e3; background: #131516;}\n"
3329 " h1 { color: #a265e0!important; }\n"
3330 " h2 { color: #a265e0; }\n"
3331 " h3 { color: #ff5190; background-color: #3e3e1f; }\n"
3332 " a { color: #3391ff; }\n"
3333 " input { background-color: #2f3437; }\n"
3334 " .hr { border-color: #8c8273; }\n"
3335 " .titre { background-color: #1aa6a6; color: #e8e6e3; }\n"
3336 " .frontend {background: #2f3437;}\n"
Marno Krahmera690b732022-03-08 13:45:09 +01003337 " .socket {background: #2a2d2f;}\n"
Thierry Fourniere9ed63e2021-09-23 23:59:49 +02003338 " .backend {background: #2f3437;}\n"
3339 " .active_down {background: #760000;}\n"
3340 " .active_going_up {background: #b99200;}\n"
3341 " .active_going_down {background: #6c6c00;}\n"
3342 " .active_up {background: #165900;}\n"
3343 " .active_nolb {background: #006ab9;}\n"
3344 " .active_draining {background: #006ab9;}\n"
3345 " .active_no_check {background: #2a2d2f;}\n"
3346 " .backup_down {background: #760000;}\n"
3347 " .backup_going_up {background: #7f007f;}\n"
3348 " .backup_going_down {background: #580092;}\n"
3349 " .backup_up {background: #2e3234;}\n"
3350 " .backup_nolb {background: #1e3c6a;}\n"
3351 " .backup_draining {background: #a37a00;}\n"
3352 " .backup_no_check {background: #2a2d2f;}\n"
3353 " .maintain {background: #9a601a;}\n"
3354 " a.px:link {color: #d8d83b; text-decoration: none;}\n"
3355 " a.px:visited {color: #d8d83b; text-decoration: none;}\n"
3356 " a.px:hover {color: #ffffff; text-decoration: none;}\n"
3357 " a.lfsb:link {color: #e8e6e3; text-decoration: none;}\n"
3358 " a.lfsb:visited {color: #e8e6e3; text-decoration: none;}\n"
3359 " a.lfsb:hover {color: #b5afa6; text-decoration: none;}\n"
3360 " table.tbl th.empty { background-color: #181a1b; }\n"
Willy Tarreau00147f72022-04-11 07:59:27 +02003361 " table.tbl th.desc { background: #181a1b; }\n"
Thierry Fourniere9ed63e2021-09-23 23:59:49 +02003362 " table.tbl th.pxname { background-color: #8d0033; color: #ffff46; }\n"
3363 " table.tbl th { border-color: #808080; }\n"
3364 " table.tbl td { border-color: #808080; }\n"
3365 " u {text-decoration:none; border-bottom: 1px dotted #e8e6e3;}\n"
3366 " div.tips {\n"
3367 " background:#8e8e0d;\n"
3368 " color:#e8e6e3;\n"
3369 " border-color: #4e2c86;\n"
3370 " -moz-box-shadow: #60686c 2px 2px 3px;\n"
3371 " -webkit-box-shadow: #60686c 2px 2px 3px;\n"
3372 " box-shadow: #60686c 2px 2px 3px;\n"
3373 " }\n"
3374 "}\n"
William Lallemand74c24fb2016-11-21 17:18:36 +01003375 "-->\n"
3376 "</style></head>\n",
Willy Tarreau676c29e2019-10-09 10:50:01 +02003377 (appctx->ctx.stats.flags & STAT_SHNODE) ? " on " : "",
Willy Tarreau027d2062020-01-23 11:47:13 +01003378 (appctx->ctx.stats.flags & STAT_SHNODE) ? (uri && uri->node ? uri->node : global.node) : ""
William Lallemand74c24fb2016-11-21 17:18:36 +01003379 );
3380}
3381
3382/* Dumps the HTML stats information block to the trash for and uses the state from
Christopher Faulet908628c2022-03-25 16:43:49 +01003383 * conn-stream <cs> and per-uri parameters <uri>. The caller is responsible
William Lallemand74c24fb2016-11-21 17:18:36 +01003384 * for clearing the trash if needed.
3385 */
Christopher Faulet908628c2022-03-25 16:43:49 +01003386static void stats_dump_html_info(struct conn_stream *cs, struct uri_auth *uri)
William Lallemand74c24fb2016-11-21 17:18:36 +01003387{
Christopher Faulet908628c2022-03-25 16:43:49 +01003388 struct appctx *appctx = __cs_appctx(cs);
William Lallemand74c24fb2016-11-21 17:18:36 +01003389 unsigned int up = (now.tv_sec - start_date.tv_sec);
3390 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
Christopher Faulet908628c2022-03-25 16:43:49 +01003391 const char *scope_ptr = stats_scope_ptr(appctx, cs);
Willy Tarreau1713c032019-05-23 12:23:55 +02003392 unsigned long long bps = (unsigned long long)read_freq_ctr(&global.out_32bps) * 32;
3393
3394 /* Turn the bytes per second to bits per second and take care of the
3395 * usual ethernet overhead in order to help figure how far we are from
3396 * interface saturation since it's the only case which usually matters.
3397 * For this we count the total size of an Ethernet frame on the wire
3398 * including preamble and IFG (1538) for the largest TCP segment it
3399 * transports (1448 with TCP timestamps). This is not valid for smaller
3400 * packets (under-estimated), but it gives a reasonably accurate
3401 * estimation of how far we are from uplink saturation.
3402 */
3403 bps = bps * 8 * 1538 / 1448;
William Lallemand74c24fb2016-11-21 17:18:36 +01003404
3405 /* WARNING! this has to fit the first packet too.
3406 * We are around 3.5 kB, add adding entries will
3407 * become tricky if we want to support 4kB buffers !
3408 */
3409 chunk_appendf(&trash,
3410 "<body><h1><a href=\"" PRODUCT_URL "\" style=\"text-decoration: none;\">"
3411 PRODUCT_NAME "%s</a></h1>\n"
3412 "<h2>Statistics Report for pid %d%s%s%s%s</h2>\n"
3413 "<hr width=\"100%%\" class=\"hr\">\n"
3414 "<h3>&gt; General process information</h3>\n"
3415 "<table border=0><tr><td align=\"left\" nowrap width=\"1%%\">\n"
Yves Lafon95317282018-02-26 11:10:37 +01003416 "<p><b>pid = </b> %d (process #%d, nbproc = %d, nbthread = %d)<br>\n"
William Lallemand74c24fb2016-11-21 17:18:36 +01003417 "<b>uptime = </b> %dd %dh%02dm%02ds<br>\n"
3418 "<b>system limits:</b> memmax = %s%s; ulimit-n = %d<br>\n"
3419 "<b>maxsock = </b> %d; <b>maxconn = </b> %d; <b>maxpipes = </b> %d<br>\n"
Willy Tarreau1713c032019-05-23 12:23:55 +02003420 "current conns = %d; current pipes = %d/%d; conn rate = %d/sec; bit rate = %.3f %cbps<br>\n"
William Lallemand74c24fb2016-11-21 17:18:36 +01003421 "Running tasks: %d/%d; idle = %d %%<br>\n"
3422 "</td><td align=\"center\" nowrap>\n"
3423 "<table class=\"lgd\"><tr>\n"
3424 "<td class=\"active_up\">&nbsp;</td><td class=\"noborder\">active UP </td>"
3425 "<td class=\"backup_up\">&nbsp;</td><td class=\"noborder\">backup UP </td>"
3426 "</tr><tr>\n"
3427 "<td class=\"active_going_down\"></td><td class=\"noborder\">active UP, going down </td>"
3428 "<td class=\"backup_going_down\"></td><td class=\"noborder\">backup UP, going down </td>"
3429 "</tr><tr>\n"
3430 "<td class=\"active_going_up\"></td><td class=\"noborder\">active DOWN, going up </td>"
3431 "<td class=\"backup_going_up\"></td><td class=\"noborder\">backup DOWN, going up </td>"
3432 "</tr><tr>\n"
3433 "<td class=\"active_down\"></td><td class=\"noborder\">active or backup DOWN &nbsp;</td>"
3434 "<td class=\"active_no_check\"></td><td class=\"noborder\">not checked </td>"
3435 "</tr><tr>\n"
3436 "<td class=\"maintain\"></td><td class=\"noborder\" colspan=\"3\">active or backup DOWN for maintenance (MAINT) &nbsp;</td>"
3437 "</tr><tr>\n"
3438 "<td class=\"active_draining\"></td><td class=\"noborder\" colspan=\"3\">active or backup SOFT STOPPED for maintenance &nbsp;</td>"
3439 "</tr></table>\n"
3440 "Note: \"NOLB\"/\"DRAIN\" = UP with load-balancing disabled."
3441 "</td>"
3442 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
3443 "<b>Display option:</b><ul style=\"margin-top: 0.25em;\">"
3444 "",
Willy Tarreau676c29e2019-10-09 10:50:01 +02003445 (appctx->ctx.stats.flags & STAT_HIDEVER) ? "" : (stats_version_string),
3446 pid, (appctx->ctx.stats.flags & STAT_SHNODE) ? " on " : "",
3447 (appctx->ctx.stats.flags & STAT_SHNODE) ? (uri->node ? uri->node : global.node) : "",
3448 (appctx->ctx.stats.flags & STAT_SHDESC) ? ": " : "",
3449 (appctx->ctx.stats.flags & STAT_SHDESC) ? (uri->desc ? uri->desc : global.desc) : "",
Willy Tarreaue8422bf2021-06-15 09:08:18 +02003450 pid, 1, 1, global.nbthread,
William Lallemand74c24fb2016-11-21 17:18:36 +01003451 up / 86400, (up % 86400) / 3600,
3452 (up % 3600) / 60, (up % 60),
3453 global.rlimit_memmax ? ultoa(global.rlimit_memmax) : "unlimited",
3454 global.rlimit_memmax ? " MB" : "",
3455 global.rlimit_nofile,
3456 global.maxsock, global.maxconn, global.maxpipes,
3457 actconn, pipes_used, pipes_used+pipes_free, read_freq_ctr(&global.conn_per_sec),
Willy Tarreau1713c032019-05-23 12:23:55 +02003458 bps >= 1000000000UL ? (bps / 1000000000.0) : bps >= 1000000UL ? (bps / 1000000.0) : (bps / 1000.0),
3459 bps >= 1000000000UL ? 'G' : bps >= 1000000UL ? 'M' : 'k',
Willy Tarreauf9d5e102021-10-08 10:43:59 +02003460 total_run_queues(), total_allocated_tasks(), clock_report_idle()
William Lallemand74c24fb2016-11-21 17:18:36 +01003461 );
3462
3463 /* scope_txt = search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
Christopher Fauleted7a0662019-01-14 11:07:34 +01003464 memcpy(scope_txt, scope_ptr, appctx->ctx.stats.scope_len);
William Lallemand74c24fb2016-11-21 17:18:36 +01003465 scope_txt[appctx->ctx.stats.scope_len] = '\0';
3466
3467 chunk_appendf(&trash,
3468 "<li><form method=\"GET\">Scope : <input value=\"%s\" name=\"" STAT_SCOPE_INPUT_NAME "\" size=\"8\" maxlength=\"%d\" tabindex=\"1\"/></form>\n",
3469 (appctx->ctx.stats.scope_len > 0) ? scope_txt : "",
3470 STAT_SCOPE_TXT_MAXLEN);
3471
3472 /* scope_txt = search pattern + search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
3473 scope_txt[0] = 0;
3474 if (appctx->ctx.stats.scope_len) {
3475 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Christopher Fauleted7a0662019-01-14 11:07:34 +01003476 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), scope_ptr, appctx->ctx.stats.scope_len);
William Lallemand74c24fb2016-11-21 17:18:36 +01003477 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
3478 }
3479
3480 if (appctx->ctx.stats.flags & STAT_HIDE_DOWN)
3481 chunk_appendf(&trash,
3482 "<li><a href=\"%s%s%s%s\">Show all servers</a><br>\n",
3483 uri->uri_prefix,
3484 "",
3485 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
3486 scope_txt);
3487 else
3488 chunk_appendf(&trash,
3489 "<li><a href=\"%s%s%s%s\">Hide 'DOWN' servers</a><br>\n",
3490 uri->uri_prefix,
3491 ";up",
3492 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
3493 scope_txt);
3494
3495 if (uri->refresh > 0) {
3496 if (appctx->ctx.stats.flags & STAT_NO_REFRESH)
3497 chunk_appendf(&trash,
3498 "<li><a href=\"%s%s%s%s\">Enable refresh</a><br>\n",
3499 uri->uri_prefix,
3500 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3501 "",
3502 scope_txt);
3503 else
3504 chunk_appendf(&trash,
3505 "<li><a href=\"%s%s%s%s\">Disable refresh</a><br>\n",
3506 uri->uri_prefix,
3507 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3508 ";norefresh",
3509 scope_txt);
3510 }
3511
3512 chunk_appendf(&trash,
3513 "<li><a href=\"%s%s%s%s\">Refresh now</a><br>\n",
3514 uri->uri_prefix,
3515 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3516 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
3517 scope_txt);
3518
3519 chunk_appendf(&trash,
3520 "<li><a href=\"%s;csv%s%s\">CSV export</a><br>\n",
3521 uri->uri_prefix,
3522 (uri->refresh > 0) ? ";norefresh" : "",
3523 scope_txt);
3524
3525 chunk_appendf(&trash,
Christopher Faulet6338a082019-09-09 15:50:54 +02003526 "<li><a href=\"%s;json%s%s\">JSON export</a> (<a href=\"%s;json-schema\">schema</a>)<br>\n",
3527 uri->uri_prefix,
3528 (uri->refresh > 0) ? ";norefresh" : "",
3529 scope_txt, uri->uri_prefix);
3530
3531 chunk_appendf(&trash,
William Lallemand74c24fb2016-11-21 17:18:36 +01003532 "</ul></td>"
3533 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
3534 "<b>External resources:</b><ul style=\"margin-top: 0.25em;\">\n"
3535 "<li><a href=\"" PRODUCT_URL "\">Primary site</a><br>\n"
3536 "<li><a href=\"" PRODUCT_URL_UPD "\">Updates (v" PRODUCT_BRANCH ")</a><br>\n"
3537 "<li><a href=\"" PRODUCT_URL_DOC "\">Online manual</a><br>\n"
3538 "</ul>"
3539 "</td>"
3540 "</tr></table>\n"
3541 ""
3542 );
3543
3544 if (appctx->ctx.stats.st_code) {
3545 switch (appctx->ctx.stats.st_code) {
3546 case STAT_STATUS_DONE:
3547 chunk_appendf(&trash,
3548 "<p><div class=active_up>"
3549 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
3550 "Action processed successfully."
3551 "</div>\n", uri->uri_prefix,
3552 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3553 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
3554 scope_txt);
3555 break;
3556 case STAT_STATUS_NONE:
3557 chunk_appendf(&trash,
3558 "<p><div class=active_going_down>"
3559 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
3560 "Nothing has changed."
3561 "</div>\n", uri->uri_prefix,
3562 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3563 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
3564 scope_txt);
3565 break;
3566 case STAT_STATUS_PART:
3567 chunk_appendf(&trash,
3568 "<p><div class=active_going_down>"
3569 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
3570 "Action partially processed.<br>"
3571 "Some server names are probably unknown or ambiguous (duplicated names in the backend)."
3572 "</div>\n", uri->uri_prefix,
3573 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3574 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
3575 scope_txt);
3576 break;
3577 case STAT_STATUS_ERRP:
3578 chunk_appendf(&trash,
3579 "<p><div class=active_down>"
3580 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
3581 "Action not processed because of invalid parameters."
3582 "<ul>"
3583 "<li>The action is maybe unknown.</li>"
Christopher Faulet2f9a41d2019-02-27 15:30:57 +01003584 "<li>Invalid key parameter (empty or too long).</li>"
William Lallemand74c24fb2016-11-21 17:18:36 +01003585 "<li>The backend name is probably unknown or ambiguous (duplicated names).</li>"
3586 "<li>Some server names are probably unknown or ambiguous (duplicated names in the backend).</li>"
3587 "</ul>"
3588 "</div>\n", uri->uri_prefix,
3589 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3590 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
3591 scope_txt);
3592 break;
3593 case STAT_STATUS_EXCD:
3594 chunk_appendf(&trash,
3595 "<p><div class=active_down>"
3596 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
3597 "<b>Action not processed : the buffer couldn't store all the data.<br>"
3598 "You should retry with less servers at a time.</b>"
3599 "</div>\n", uri->uri_prefix,
3600 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3601 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
3602 scope_txt);
3603 break;
3604 case STAT_STATUS_DENY:
3605 chunk_appendf(&trash,
3606 "<p><div class=active_down>"
3607 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
3608 "<b>Action denied.</b>"
3609 "</div>\n", uri->uri_prefix,
3610 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3611 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
3612 scope_txt);
3613 break;
Christopher Faulet3c2ecf72019-02-27 16:41:27 +01003614 case STAT_STATUS_IVAL:
3615 chunk_appendf(&trash,
3616 "<p><div class=active_down>"
3617 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
3618 "<b>Invalid requests (unsupported method or chunked encoded request).</b>"
3619 "</div>\n", uri->uri_prefix,
3620 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3621 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
3622 scope_txt);
3623 break;
William Lallemand74c24fb2016-11-21 17:18:36 +01003624 default:
3625 chunk_appendf(&trash,
3626 "<p><div class=active_no_check>"
3627 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
3628 "Unexpected result."
3629 "</div>\n", uri->uri_prefix,
3630 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3631 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
3632 scope_txt);
3633 }
3634 chunk_appendf(&trash, "<p>\n");
3635 }
3636}
3637
3638/* Dumps the HTML stats trailer block to the trash. The caller is responsible
3639 * for clearing the trash if needed.
3640 */
3641static void stats_dump_html_end()
3642{
3643 chunk_appendf(&trash, "</body></html>\n");
3644}
3645
Simon Horman05ee2132017-01-04 09:37:25 +01003646/* Dumps the stats JSON header to the trash buffer which. The caller is responsible
3647 * for clearing it if needed.
3648 */
3649static void stats_dump_json_header()
3650{
3651 chunk_strcat(&trash, "[");
3652}
3653
3654
3655/* Dumps the JSON stats trailer block to the trash. The caller is responsible
3656 * for clearing the trash if needed.
3657 */
3658static void stats_dump_json_end()
3659{
3660 chunk_strcat(&trash, "]");
3661}
3662
Amaury Denoyelle98b81cb2020-10-02 18:32:04 +02003663/* Uses <appctx.ctx.stats.obj1> as a pointer to the current proxy and <obj2> as
3664 * a pointer to the current server/listener.
3665 */
Christopher Faulet908628c2022-03-25 16:43:49 +01003666static int stats_dump_proxies(struct conn_stream *cs,
Amaury Denoyelle98b81cb2020-10-02 18:32:04 +02003667 struct htx *htx,
3668 struct uri_auth *uri)
3669{
Christopher Faulet908628c2022-03-25 16:43:49 +01003670 struct appctx *appctx = __cs_appctx(cs);
3671 struct channel *rep = cs_ic(cs);
Amaury Denoyelle98b81cb2020-10-02 18:32:04 +02003672 struct proxy *px;
3673
3674 /* dump proxies */
3675 while (appctx->ctx.stats.obj1) {
3676 if (htx) {
3677 if (htx_almost_full(htx))
3678 goto full;
3679 }
3680 else {
3681 if (buffer_almost_full(&rep->buf))
3682 goto full;
3683 }
3684
3685 px = appctx->ctx.stats.obj1;
Marno Krahmer07954fb2021-06-24 16:51:08 +02003686 /* Skip the global frontend proxies and non-networked ones.
William Lallemand85a16b22021-08-03 13:18:11 +02003687 * Also skip proxies that were disabled in the configuration
Marno Krahmer07954fb2021-06-24 16:51:08 +02003688 * This change allows retrieving stats from "old" proxies after a reload.
3689 */
Christopher Fauletdfd10ab2021-10-06 14:24:19 +02003690 if (!(px->flags & PR_FL_DISABLED) && px->uuid > 0 &&
William Lallemande7f74622021-07-28 17:45:18 +02003691 (px->cap & (PR_CAP_FE | PR_CAP_BE)) && !(px->cap & PR_CAP_INT)) {
Christopher Faulet908628c2022-03-25 16:43:49 +01003692 if (stats_dump_proxy_to_buffer(cs, htx, px, uri) == 0)
Amaury Denoyelle98b81cb2020-10-02 18:32:04 +02003693 return 0;
3694 }
3695
3696 appctx->ctx.stats.obj1 = px->next;
3697 appctx->ctx.stats.px_st = STAT_PX_ST_INIT;
3698 }
3699
3700 return 1;
3701
3702 full:
Christopher Fauleta0bdec32022-04-04 07:51:21 +02003703 cs_rx_room_blk(cs);
Amaury Denoyelle98b81cb2020-10-02 18:32:04 +02003704 return 0;
3705}
3706
Christopher Faulet908628c2022-03-25 16:43:49 +01003707/* This function dumps statistics onto the conn-stream's read buffer in
William Lallemand74c24fb2016-11-21 17:18:36 +01003708 * either CSV or HTML format. <uri> contains some HTML-specific parameters that
3709 * are ignored for CSV format (hence <uri> may be NULL there). It returns 0 if
3710 * it had to stop writing data and an I/O is needed, 1 if the dump is finished
3711 * and the stream must be closed, or -1 in case of any error. This function is
3712 * used by both the CLI and the HTTP handlers.
3713 */
Christopher Faulet908628c2022-03-25 16:43:49 +01003714static int stats_dump_stat_to_buffer(struct conn_stream *cs, struct htx *htx,
Christopher Fauletef779222018-10-31 08:47:01 +01003715 struct uri_auth *uri)
William Lallemand74c24fb2016-11-21 17:18:36 +01003716{
Christopher Faulet908628c2022-03-25 16:43:49 +01003717 struct appctx *appctx = __cs_appctx(cs);
3718 struct channel *rep = cs_ic(cs);
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02003719 enum stats_domain domain = appctx->ctx.stats.domain;
William Lallemand74c24fb2016-11-21 17:18:36 +01003720
3721 chunk_reset(&trash);
3722
3723 switch (appctx->st2) {
3724 case STAT_ST_INIT:
3725 appctx->st2 = STAT_ST_HEAD; /* let's start producing data */
3726 /* fall through */
3727
3728 case STAT_ST_HEAD:
3729 if (appctx->ctx.stats.flags & STAT_FMT_HTML)
Willy Tarreau676c29e2019-10-09 10:50:01 +02003730 stats_dump_html_head(appctx, uri);
Christopher Faulet6338a082019-09-09 15:50:54 +02003731 else if (appctx->ctx.stats.flags & STAT_JSON_SCHM)
3732 stats_dump_json_schema(&trash);
Simon Horman05ee2132017-01-04 09:37:25 +01003733 else if (appctx->ctx.stats.flags & STAT_FMT_JSON)
Willy Tarreau9d7fb632017-04-11 07:53:04 +02003734 stats_dump_json_header();
William Lallemand74c24fb2016-11-21 17:18:36 +01003735 else if (!(appctx->ctx.stats.flags & STAT_FMT_TYPED))
Amaury Denoyelle50660a82020-10-05 11:49:39 +02003736 stats_dump_csv_header(appctx->ctx.stats.domain);
William Lallemand74c24fb2016-11-21 17:18:36 +01003737
Christopher Fauletef779222018-10-31 08:47:01 +01003738 if (!stats_putchk(rep, htx, &trash))
3739 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01003740
Christopher Faulet6338a082019-09-09 15:50:54 +02003741 if (appctx->ctx.stats.flags & STAT_JSON_SCHM) {
3742 appctx->st2 = STAT_ST_FIN;
3743 return 1;
3744 }
William Lallemand74c24fb2016-11-21 17:18:36 +01003745 appctx->st2 = STAT_ST_INFO;
3746 /* fall through */
3747
3748 case STAT_ST_INFO:
3749 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Christopher Faulet908628c2022-03-25 16:43:49 +01003750 stats_dump_html_info(cs, uri);
Christopher Fauletef779222018-10-31 08:47:01 +01003751 if (!stats_putchk(rep, htx, &trash))
3752 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01003753 }
3754
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02003755 if (domain == STATS_DOMAIN_PROXY)
3756 appctx->ctx.stats.obj1 = proxies_list;
3757
William Lallemand74c24fb2016-11-21 17:18:36 +01003758 appctx->ctx.stats.px_st = STAT_PX_ST_INIT;
3759 appctx->st2 = STAT_ST_LIST;
3760 /* fall through */
3761
3762 case STAT_ST_LIST:
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02003763 switch (domain) {
Emeric Brunf8642ee2021-10-29 17:59:18 +02003764 case STATS_DOMAIN_RESOLVERS:
Christopher Faulet908628c2022-03-25 16:43:49 +01003765 if (!stats_dump_resolvers(cs, stat_l[domain],
Emeric Brund3b44952021-01-06 16:01:02 +01003766 stat_count[domain],
3767 &stats_module_list[domain])) {
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02003768 return 0;
3769 }
3770 break;
3771
3772 case STATS_DOMAIN_PROXY:
3773 default:
3774 /* dump proxies */
Christopher Faulet908628c2022-03-25 16:43:49 +01003775 if (!stats_dump_proxies(cs, htx, uri))
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02003776 return 0;
3777 break;
3778 }
William Lallemand74c24fb2016-11-21 17:18:36 +01003779
3780 appctx->st2 = STAT_ST_END;
3781 /* fall through */
3782
3783 case STAT_ST_END:
Simon Horman05ee2132017-01-04 09:37:25 +01003784 if (appctx->ctx.stats.flags & (STAT_FMT_HTML|STAT_FMT_JSON)) {
3785 if (appctx->ctx.stats.flags & STAT_FMT_HTML)
3786 stats_dump_html_end();
3787 else
3788 stats_dump_json_end();
Christopher Fauletef779222018-10-31 08:47:01 +01003789 if (!stats_putchk(rep, htx, &trash))
3790 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01003791 }
3792
3793 appctx->st2 = STAT_ST_FIN;
3794 /* fall through */
3795
3796 case STAT_ST_FIN:
3797 return 1;
3798
3799 default:
3800 /* unknown state ! */
3801 appctx->st2 = STAT_ST_FIN;
3802 return -1;
3803 }
Christopher Fauletef779222018-10-31 08:47:01 +01003804
3805 full:
Christopher Fauleta0bdec32022-04-04 07:51:21 +02003806 cs_rx_room_blk(cs);
Christopher Fauletef779222018-10-31 08:47:01 +01003807 return 0;
3808
William Lallemand74c24fb2016-11-21 17:18:36 +01003809}
3810
3811/* We reached the stats page through a POST request. The appctx is
3812 * expected to have already been allocated by the caller.
3813 * Parse the posted data and enable/disable servers if necessary.
3814 * Returns 1 if request was parsed or zero if it needs more data.
3815 */
Christopher Faulet908628c2022-03-25 16:43:49 +01003816static int stats_process_http_post(struct conn_stream *cs)
William Lallemand74c24fb2016-11-21 17:18:36 +01003817{
Christopher Faulet908628c2022-03-25 16:43:49 +01003818 struct stream *s = __cs_strm(cs);
3819 struct appctx *appctx = __cs_appctx(cs);
William Lallemand74c24fb2016-11-21 17:18:36 +01003820
3821 struct proxy *px = NULL;
3822 struct server *sv = NULL;
3823
3824 char key[LINESIZE];
3825 int action = ST_ADM_ACTION_NONE;
3826 int reprocess = 0;
3827
3828 int total_servers = 0;
3829 int altered_servers = 0;
3830
3831 char *first_param, *cur_param, *next_param, *end_params;
3832 char *st_cur_param = NULL;
3833 char *st_next_param = NULL;
3834
Christopher Fauleta3618372018-12-13 21:59:56 +01003835 struct buffer *temp = get_trash_chunk();
William Lallemand74c24fb2016-11-21 17:18:36 +01003836
Christopher Fauletb7f88902019-07-15 21:56:43 +02003837 struct htx *htx = htxbuf(&s->req.buf);
3838 struct htx_blk *blk;
Christopher Fauleta3618372018-12-13 21:59:56 +01003839
Christopher Fauletb7f88902019-07-15 21:56:43 +02003840 /* we need more data */
3841 if (s->txn->req.msg_state < HTTP_MSG_DONE) {
3842 /* check if we can receive more */
3843 if (htx_free_data_space(htx) <= global.tune.maxrewrite) {
3844 appctx->ctx.stats.st_code = STAT_STATUS_EXCD;
3845 goto out;
Christopher Fauleta3618372018-12-13 21:59:56 +01003846 }
Christopher Fauletb7f88902019-07-15 21:56:43 +02003847 goto wait;
3848 }
Christopher Fauleta3618372018-12-13 21:59:56 +01003849
Christopher Fauletb7f88902019-07-15 21:56:43 +02003850 /* The request was fully received. Copy data */
3851 blk = htx_get_head_blk(htx);
3852 while (blk) {
3853 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Fauleta3618372018-12-13 21:59:56 +01003854
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01003855 if (type == HTX_BLK_TLR || type == HTX_BLK_EOT)
Christopher Fauletb7f88902019-07-15 21:56:43 +02003856 break;
3857 if (type == HTX_BLK_DATA) {
3858 struct ist v = htx_get_blk_value(htx, blk);
Christopher Fauleta3618372018-12-13 21:59:56 +01003859
Christopher Fauletb7f88902019-07-15 21:56:43 +02003860 if (!chunk_memcat(temp, v.ptr, v.len)) {
Christopher Faulet2f9a41d2019-02-27 15:30:57 +01003861 appctx->ctx.stats.st_code = STAT_STATUS_EXCD;
3862 goto out;
3863 }
Christopher Fauleta3618372018-12-13 21:59:56 +01003864 }
Christopher Fauletb7f88902019-07-15 21:56:43 +02003865 blk = htx_get_next_blk(htx, blk);
William Lallemand74c24fb2016-11-21 17:18:36 +01003866 }
3867
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003868 first_param = temp->area;
Christopher Fauleta3618372018-12-13 21:59:56 +01003869 end_params = temp->area + temp->data;
William Lallemand74c24fb2016-11-21 17:18:36 +01003870 cur_param = next_param = end_params;
3871 *end_params = '\0';
3872
3873 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
3874
3875 /*
3876 * Parse the parameters in reverse order to only store the last value.
3877 * From the html form, the backend and the action are at the end.
3878 */
3879 while (cur_param > first_param) {
3880 char *value;
3881 int poffset, plen;
3882
3883 cur_param--;
3884
3885 if ((*cur_param == '&') || (cur_param == first_param)) {
3886 reprocess_servers:
3887 /* Parse the key */
3888 poffset = (cur_param != first_param ? 1 : 0);
3889 plen = next_param - cur_param + (cur_param == first_param ? 1 : 0);
3890 if ((plen > 0) && (plen <= sizeof(key))) {
3891 strncpy(key, cur_param + poffset, plen);
3892 key[plen - 1] = '\0';
3893 } else {
Christopher Faulet2f9a41d2019-02-27 15:30:57 +01003894 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
William Lallemand74c24fb2016-11-21 17:18:36 +01003895 goto out;
3896 }
3897
3898 /* Parse the value */
3899 value = key;
3900 while (*value != '\0' && *value != '=') {
3901 value++;
3902 }
3903 if (*value == '=') {
3904 /* Ok, a value is found, we can mark the end of the key */
3905 *value++ = '\0';
3906 }
Willy Tarreau62ba9ba2020-04-23 17:54:47 +02003907 if (url_decode(key, 1) < 0 || url_decode(value, 1) < 0)
William Lallemand74c24fb2016-11-21 17:18:36 +01003908 break;
3909
3910 /* Now we can check the key to see what to do */
3911 if (!px && (strcmp(key, "b") == 0)) {
3912 if ((px = proxy_be_by_name(value)) == NULL) {
3913 /* the backend name is unknown or ambiguous (duplicate names) */
3914 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
3915 goto out;
3916 }
3917 }
3918 else if (!action && (strcmp(key, "action") == 0)) {
3919 if (strcmp(value, "ready") == 0) {
3920 action = ST_ADM_ACTION_READY;
3921 }
3922 else if (strcmp(value, "drain") == 0) {
3923 action = ST_ADM_ACTION_DRAIN;
3924 }
3925 else if (strcmp(value, "maint") == 0) {
3926 action = ST_ADM_ACTION_MAINT;
3927 }
3928 else if (strcmp(value, "shutdown") == 0) {
3929 action = ST_ADM_ACTION_SHUTDOWN;
3930 }
3931 else if (strcmp(value, "dhlth") == 0) {
3932 action = ST_ADM_ACTION_DHLTH;
3933 }
3934 else if (strcmp(value, "ehlth") == 0) {
3935 action = ST_ADM_ACTION_EHLTH;
3936 }
3937 else if (strcmp(value, "hrunn") == 0) {
3938 action = ST_ADM_ACTION_HRUNN;
3939 }
3940 else if (strcmp(value, "hnolb") == 0) {
3941 action = ST_ADM_ACTION_HNOLB;
3942 }
3943 else if (strcmp(value, "hdown") == 0) {
3944 action = ST_ADM_ACTION_HDOWN;
3945 }
3946 else if (strcmp(value, "dagent") == 0) {
3947 action = ST_ADM_ACTION_DAGENT;
3948 }
3949 else if (strcmp(value, "eagent") == 0) {
3950 action = ST_ADM_ACTION_EAGENT;
3951 }
3952 else if (strcmp(value, "arunn") == 0) {
3953 action = ST_ADM_ACTION_ARUNN;
3954 }
3955 else if (strcmp(value, "adown") == 0) {
3956 action = ST_ADM_ACTION_ADOWN;
3957 }
3958 /* else these are the old supported methods */
3959 else if (strcmp(value, "disable") == 0) {
3960 action = ST_ADM_ACTION_DISABLE;
3961 }
3962 else if (strcmp(value, "enable") == 0) {
3963 action = ST_ADM_ACTION_ENABLE;
3964 }
3965 else if (strcmp(value, "stop") == 0) {
3966 action = ST_ADM_ACTION_STOP;
3967 }
3968 else if (strcmp(value, "start") == 0) {
3969 action = ST_ADM_ACTION_START;
3970 }
3971 else {
3972 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
3973 goto out;
3974 }
3975 }
3976 else if (strcmp(key, "s") == 0) {
3977 if (!(px && action)) {
3978 /*
3979 * Indicates that we'll need to reprocess the parameters
3980 * as soon as backend and action are known
3981 */
3982 if (!reprocess) {
3983 st_cur_param = cur_param;
3984 st_next_param = next_param;
3985 }
3986 reprocess = 1;
3987 }
3988 else if ((sv = findserver(px, value)) != NULL) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003989 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
William Lallemand74c24fb2016-11-21 17:18:36 +01003990 switch (action) {
3991 case ST_ADM_ACTION_DISABLE:
Emeric Brun52a91d32017-08-31 14:41:55 +02003992 if (!(sv->cur_admin & SRV_ADMF_FMAINT)) {
William Lallemand74c24fb2016-11-21 17:18:36 +01003993 altered_servers++;
3994 total_servers++;
3995 srv_set_admin_flag(sv, SRV_ADMF_FMAINT, "'disable' on stats page");
3996 }
3997 break;
3998 case ST_ADM_ACTION_ENABLE:
Emeric Brun52a91d32017-08-31 14:41:55 +02003999 if (sv->cur_admin & SRV_ADMF_FMAINT) {
William Lallemand74c24fb2016-11-21 17:18:36 +01004000 altered_servers++;
4001 total_servers++;
4002 srv_clr_admin_flag(sv, SRV_ADMF_FMAINT);
4003 }
4004 break;
4005 case ST_ADM_ACTION_STOP:
Emeric Brun52a91d32017-08-31 14:41:55 +02004006 if (!(sv->cur_admin & SRV_ADMF_FDRAIN)) {
William Lallemand74c24fb2016-11-21 17:18:36 +01004007 srv_set_admin_flag(sv, SRV_ADMF_FDRAIN, "'stop' on stats page");
4008 altered_servers++;
4009 total_servers++;
4010 }
4011 break;
4012 case ST_ADM_ACTION_START:
Emeric Brun52a91d32017-08-31 14:41:55 +02004013 if (sv->cur_admin & SRV_ADMF_FDRAIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +01004014 srv_clr_admin_flag(sv, SRV_ADMF_FDRAIN);
4015 altered_servers++;
4016 total_servers++;
4017 }
4018 break;
4019 case ST_ADM_ACTION_DHLTH:
4020 if (sv->check.state & CHK_ST_CONFIGURED) {
4021 sv->check.state &= ~CHK_ST_ENABLED;
4022 altered_servers++;
4023 total_servers++;
4024 }
4025 break;
4026 case ST_ADM_ACTION_EHLTH:
4027 if (sv->check.state & CHK_ST_CONFIGURED) {
4028 sv->check.state |= CHK_ST_ENABLED;
4029 altered_servers++;
4030 total_servers++;
4031 }
4032 break;
4033 case ST_ADM_ACTION_HRUNN:
4034 if (!(sv->track)) {
4035 sv->check.health = sv->check.rise + sv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02004036 srv_set_running(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01004037 altered_servers++;
4038 total_servers++;
4039 }
4040 break;
4041 case ST_ADM_ACTION_HNOLB:
4042 if (!(sv->track)) {
4043 sv->check.health = sv->check.rise + sv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02004044 srv_set_stopping(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01004045 altered_servers++;
4046 total_servers++;
4047 }
4048 break;
4049 case ST_ADM_ACTION_HDOWN:
4050 if (!(sv->track)) {
4051 sv->check.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02004052 srv_set_stopped(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01004053 altered_servers++;
4054 total_servers++;
4055 }
4056 break;
4057 case ST_ADM_ACTION_DAGENT:
4058 if (sv->agent.state & CHK_ST_CONFIGURED) {
4059 sv->agent.state &= ~CHK_ST_ENABLED;
4060 altered_servers++;
4061 total_servers++;
4062 }
4063 break;
4064 case ST_ADM_ACTION_EAGENT:
4065 if (sv->agent.state & CHK_ST_CONFIGURED) {
4066 sv->agent.state |= CHK_ST_ENABLED;
4067 altered_servers++;
4068 total_servers++;
4069 }
4070 break;
4071 case ST_ADM_ACTION_ARUNN:
4072 if (sv->agent.state & CHK_ST_ENABLED) {
4073 sv->agent.health = sv->agent.rise + sv->agent.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02004074 srv_set_running(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01004075 altered_servers++;
4076 total_servers++;
4077 }
4078 break;
4079 case ST_ADM_ACTION_ADOWN:
4080 if (sv->agent.state & CHK_ST_ENABLED) {
4081 sv->agent.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02004082 srv_set_stopped(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01004083 altered_servers++;
4084 total_servers++;
4085 }
4086 break;
4087 case ST_ADM_ACTION_READY:
4088 srv_adm_set_ready(sv);
4089 altered_servers++;
4090 total_servers++;
4091 break;
4092 case ST_ADM_ACTION_DRAIN:
4093 srv_adm_set_drain(sv);
4094 altered_servers++;
4095 total_servers++;
4096 break;
4097 case ST_ADM_ACTION_MAINT:
4098 srv_adm_set_maint(sv);
4099 altered_servers++;
4100 total_servers++;
4101 break;
4102 case ST_ADM_ACTION_SHUTDOWN:
Christopher Fauletdfd10ab2021-10-06 14:24:19 +02004103 if (!(px->flags & (PR_FL_DISABLED|PR_FL_STOPPED))) {
Willy Tarreauaf7ea812019-11-14 16:42:04 +01004104 srv_shutdown_streams(sv, SF_ERR_KILLED);
William Lallemand74c24fb2016-11-21 17:18:36 +01004105 altered_servers++;
4106 total_servers++;
4107 }
4108 break;
4109 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004110 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
William Lallemand74c24fb2016-11-21 17:18:36 +01004111 } else {
4112 /* the server name is unknown or ambiguous (duplicate names) */
4113 total_servers++;
4114 }
4115 }
4116 if (reprocess && px && action) {
4117 /* Now, we know the backend and the action chosen by the user.
4118 * We can safely restart from the first server parameter
4119 * to reprocess them
4120 */
4121 cur_param = st_cur_param;
4122 next_param = st_next_param;
4123 reprocess = 0;
4124 goto reprocess_servers;
4125 }
4126
4127 next_param = cur_param;
4128 }
4129 }
4130
4131 if (total_servers == 0) {
4132 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
4133 }
4134 else if (altered_servers == 0) {
4135 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
4136 }
4137 else if (altered_servers == total_servers) {
4138 appctx->ctx.stats.st_code = STAT_STATUS_DONE;
4139 }
4140 else {
4141 appctx->ctx.stats.st_code = STAT_STATUS_PART;
4142 }
4143 out:
4144 return 1;
Christopher Faulet2f9a41d2019-02-27 15:30:57 +01004145 wait:
4146 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
4147 return 0;
Christopher Fauletef779222018-10-31 08:47:01 +01004148}
4149
4150
Christopher Faulet908628c2022-03-25 16:43:49 +01004151static int stats_send_http_headers(struct conn_stream *cs, struct htx *htx)
Christopher Fauletef779222018-10-31 08:47:01 +01004152{
Christopher Faulet908628c2022-03-25 16:43:49 +01004153 struct stream *s = __cs_strm(cs);
Christopher Fauletef779222018-10-31 08:47:01 +01004154 struct uri_auth *uri = s->be->uri_auth;
Christopher Faulet908628c2022-03-25 16:43:49 +01004155 struct appctx *appctx = __cs_appctx(cs);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004156 struct htx_sl *sl;
4157 unsigned int flags;
Christopher Fauletef779222018-10-31 08:47:01 +01004158
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004159 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);
4160 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), ist("200"), ist("OK"));
4161 if (!sl)
Christopher Fauletef779222018-10-31 08:47:01 +01004162 goto full;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004163 sl->info.res.status = 200;
Christopher Fauletef779222018-10-31 08:47:01 +01004164
Christopher Fauletb829f4c2019-03-29 16:13:55 +01004165 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")))
Christopher Fauletef779222018-10-31 08:47:01 +01004166 goto full;
4167 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
4168 if (!htx_add_header(htx, ist("Content-Type"), ist("text/html")))
4169 goto full;
4170 }
Christopher Faulet6338a082019-09-09 15:50:54 +02004171 else if (appctx->ctx.stats.flags & (STAT_FMT_JSON|STAT_JSON_SCHM)) {
4172 if (!htx_add_header(htx, ist("Content-Type"), ist("application/json")))
4173 goto full;
4174 }
Christopher Fauletef779222018-10-31 08:47:01 +01004175 else {
4176 if (!htx_add_header(htx, ist("Content-Type"), ist("text/plain")))
4177 goto full;
4178 }
4179
4180 if (uri->refresh > 0 && !(appctx->ctx.stats.flags & STAT_NO_REFRESH)) {
4181 const char *refresh = U2A(uri->refresh);
Tim Duesterhusdcf753a2021-03-04 17:31:47 +01004182 if (!htx_add_header(htx, ist("Refresh"), ist(refresh)))
Christopher Fauletef779222018-10-31 08:47:01 +01004183 goto full;
4184 }
4185
4186 if (appctx->ctx.stats.flags & STAT_CHUNKED) {
4187 if (!htx_add_header(htx, ist("Transfer-Encoding"), ist("chunked")))
4188 goto full;
4189 }
4190
4191 if (!htx_add_endof(htx, HTX_BLK_EOH))
4192 goto full;
4193
Christopher Faulet1e2d6362019-02-27 16:28:48 +01004194 channel_add_input(&s->res, htx->data);
Christopher Fauletef779222018-10-31 08:47:01 +01004195 return 1;
4196
4197 full:
4198 htx_reset(htx);
Christopher Fauleta0bdec32022-04-04 07:51:21 +02004199 cs_rx_room_blk(cs);
Christopher Fauletef779222018-10-31 08:47:01 +01004200 return 0;
William Lallemand74c24fb2016-11-21 17:18:36 +01004201}
4202
Christopher Fauletef779222018-10-31 08:47:01 +01004203
Christopher Faulet908628c2022-03-25 16:43:49 +01004204static int stats_send_http_redirect(struct conn_stream *cs, struct htx *htx)
Christopher Fauletef779222018-10-31 08:47:01 +01004205{
4206 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
Christopher Faulet908628c2022-03-25 16:43:49 +01004207 struct stream *s = __cs_strm(cs);
Christopher Fauletef779222018-10-31 08:47:01 +01004208 struct uri_auth *uri = s->be->uri_auth;
Christopher Faulet908628c2022-03-25 16:43:49 +01004209 struct appctx *appctx = __cs_appctx(cs);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004210 struct htx_sl *sl;
4211 unsigned int flags;
Christopher Fauletef779222018-10-31 08:47:01 +01004212
4213 /* scope_txt = search pattern + search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
4214 scope_txt[0] = 0;
4215 if (appctx->ctx.stats.scope_len) {
Christopher Faulet908628c2022-03-25 16:43:49 +01004216 const char *scope_ptr = stats_scope_ptr(appctx, cs);
Christopher Fauleted7a0662019-01-14 11:07:34 +01004217
Christopher Fauletef779222018-10-31 08:47:01 +01004218 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Christopher Fauleted7a0662019-01-14 11:07:34 +01004219 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), scope_ptr, appctx->ctx.stats.scope_len);
Christopher Fauletef779222018-10-31 08:47:01 +01004220 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
4221 }
4222
4223 /* We don't want to land on the posted stats page because a refresh will
4224 * repost the data. We don't want this to happen on accident so we redirect
4225 * the browse to the stats page with a GET.
4226 */
4227 chunk_printf(&trash, "%s;st=%s%s%s%s",
4228 uri->uri_prefix,
4229 ((appctx->ctx.stats.st_code > STAT_STATUS_INIT) &&
4230 (appctx->ctx.stats.st_code < STAT_STATUS_SIZE) &&
4231 stat_status_codes[appctx->ctx.stats.st_code]) ?
4232 stat_status_codes[appctx->ctx.stats.st_code] :
4233 stat_status_codes[STAT_STATUS_UNKN],
4234 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
4235 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
4236 scope_txt);
4237
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004238 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_CHNK);
4239 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), ist("303"), ist("See Other"));
4240 if (!sl)
Christopher Fauletef779222018-10-31 08:47:01 +01004241 goto full;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004242 sl->info.res.status = 303;
Christopher Fauletef779222018-10-31 08:47:01 +01004243
4244 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")) ||
Christopher Fauletef779222018-10-31 08:47:01 +01004245 !htx_add_header(htx, ist("Content-Type"), ist("text/plain")) ||
4246 !htx_add_header(htx, ist("Content-Length"), ist("0")) ||
4247 !htx_add_header(htx, ist("Location"), ist2(trash.area, trash.data)))
4248 goto full;
4249
4250 if (!htx_add_endof(htx, HTX_BLK_EOH))
4251 goto full;
4252
Christopher Faulet1e2d6362019-02-27 16:28:48 +01004253 channel_add_input(&s->res, htx->data);
Christopher Fauletef779222018-10-31 08:47:01 +01004254 return 1;
4255
4256full:
4257 htx_reset(htx);
Christopher Fauleta0bdec32022-04-04 07:51:21 +02004258 cs_rx_room_blk(cs);
Christopher Fauletef779222018-10-31 08:47:01 +01004259 return 0;
4260}
William Lallemand74c24fb2016-11-21 17:18:36 +01004261
Simon Horman05ee2132017-01-04 09:37:25 +01004262
William Lallemand74c24fb2016-11-21 17:18:36 +01004263/* This I/O handler runs as an applet embedded in a stream interface. It is
4264 * used to send HTTP stats over a TCP socket. The mechanism is very simple.
4265 * appctx->st0 contains the operation in progress (dump, done). The handler
4266 * automatically unregisters itself once transfer is complete.
4267 */
Christopher Fauletb7f88902019-07-15 21:56:43 +02004268static void http_stats_io_handler(struct appctx *appctx)
Christopher Fauletef779222018-10-31 08:47:01 +01004269{
Christopher Faulet908628c2022-03-25 16:43:49 +01004270 struct conn_stream *cs = appctx->owner;
4271 struct stream *s = __cs_strm(cs);
4272 struct channel *req = cs_oc(cs);
4273 struct channel *res = cs_ic(cs);
Christopher Fauletef779222018-10-31 08:47:01 +01004274 struct htx *req_htx, *res_htx;
4275
Amaury Denoyelle072f97e2020-10-05 11:49:37 +02004276 /* only proxy stats are available via http */
4277 appctx->ctx.stats.domain = STATS_DOMAIN_PROXY;
4278
Christopher Fauletef779222018-10-31 08:47:01 +01004279 res_htx = htx_from_buf(&res->buf);
4280
Christopher Faulet62e75742022-03-31 09:16:34 +02004281 if (unlikely(cs->state == CS_ST_DIS || cs->state == CS_ST_CLO))
Christopher Fauletef779222018-10-31 08:47:01 +01004282 goto out;
4283
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05004284 /* Check if the input buffer is available. */
Christopher Fauletef779222018-10-31 08:47:01 +01004285 if (!b_size(&res->buf)) {
Christopher Fauleta0bdec32022-04-04 07:51:21 +02004286 cs_rx_room_blk(cs);
Christopher Fauletef779222018-10-31 08:47:01 +01004287 goto out;
4288 }
4289
4290 /* check that the output is not closed */
Christopher Faulet3a78aa62019-02-27 16:19:48 +01004291 if (res->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_SHUTR))
4292 appctx->st0 = STAT_HTTP_END;
Christopher Fauletef779222018-10-31 08:47:01 +01004293
4294 /* all states are processed in sequence */
4295 if (appctx->st0 == STAT_HTTP_HEAD) {
Christopher Faulet908628c2022-03-25 16:43:49 +01004296 if (stats_send_http_headers(cs, res_htx)) {
Christopher Fauletef779222018-10-31 08:47:01 +01004297 if (s->txn->meth == HTTP_METH_HEAD)
4298 appctx->st0 = STAT_HTTP_DONE;
4299 else
4300 appctx->st0 = STAT_HTTP_DUMP;
4301 }
4302 }
4303
4304 if (appctx->st0 == STAT_HTTP_DUMP) {
Christopher Faulet908628c2022-03-25 16:43:49 +01004305 if (stats_dump_stat_to_buffer(cs, res_htx, s->be->uri_auth))
Christopher Fauletef779222018-10-31 08:47:01 +01004306 appctx->st0 = STAT_HTTP_DONE;
4307 }
4308
4309 if (appctx->st0 == STAT_HTTP_POST) {
Christopher Faulet908628c2022-03-25 16:43:49 +01004310 if (stats_process_http_post(cs))
Christopher Fauletef779222018-10-31 08:47:01 +01004311 appctx->st0 = STAT_HTTP_LAST;
Christopher Faulet3a78aa62019-02-27 16:19:48 +01004312 else if (req->flags & CF_SHUTR)
Christopher Fauletef779222018-10-31 08:47:01 +01004313 appctx->st0 = STAT_HTTP_DONE;
4314 }
4315
4316 if (appctx->st0 == STAT_HTTP_LAST) {
Christopher Faulet908628c2022-03-25 16:43:49 +01004317 if (stats_send_http_redirect(cs, res_htx))
Christopher Fauletef779222018-10-31 08:47:01 +01004318 appctx->st0 = STAT_HTTP_DONE;
4319 }
4320
4321 if (appctx->st0 == STAT_HTTP_DONE) {
Christopher Faulet08b45cb2022-04-07 09:25:13 +02004322 /* no more data are expected. If the response buffer is empty,
4323 * be sure to add something (EOT block in this case) to have
4324 * something to send. It is important to be sure the EOM flags
4325 * will be handled by the endpoint.
4326 */
4327 if (htx_is_empty(res_htx)) {
4328 if (!htx_add_endof(res_htx, HTX_BLK_EOT)) {
Christopher Fauleta0bdec32022-04-04 07:51:21 +02004329 cs_rx_room_blk(cs);
Christopher Faulet08b45cb2022-04-07 09:25:13 +02004330 goto out;
4331 }
4332 channel_add_input(res, 1);
4333 }
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01004334 res_htx->flags |= HTX_FL_EOM;
Christopher Faulet908628c2022-03-25 16:43:49 +01004335 cs->endp->flags |= CS_EP_EOI;
Christopher Faulet3fa5d192022-03-07 15:52:42 +01004336 res->flags |= CF_EOI;
Christopher Faulet3a78aa62019-02-27 16:19:48 +01004337 appctx->st0 = STAT_HTTP_END;
Christopher Fauletef779222018-10-31 08:47:01 +01004338 }
4339
Christopher Faulet3a78aa62019-02-27 16:19:48 +01004340 if (appctx->st0 == STAT_HTTP_END) {
4341 if (!(res->flags & CF_SHUTR)) {
Christopher Fauletef779222018-10-31 08:47:01 +01004342 res->flags |= CF_READ_NULL;
Christopher Fauletda098e62022-03-31 17:44:45 +02004343 cs_shutr(cs);
Christopher Faulet3a78aa62019-02-27 16:19:48 +01004344 }
4345
4346 /* eat the whole request */
4347 if (co_data(req)) {
4348 req_htx = htx_from_buf(&req->buf);
4349 co_htx_skip(req, req_htx, co_data(req));
4350 htx_to_buf(req_htx, &req->buf);
Christopher Fauletef779222018-10-31 08:47:01 +01004351 }
4352 }
Christopher Faulet3a78aa62019-02-27 16:19:48 +01004353
Christopher Fauletef779222018-10-31 08:47:01 +01004354 out:
4355 /* we have left the request in the buffer for the case where we
4356 * process a POST, and this automatically re-enables activity on
4357 * read. It's better to indicate that we want to stop reading when
4358 * we're sending, so that we know there's at most one direction
4359 * deciding to wake the applet up. It saves it from looping when
4360 * emitting large blocks into small TCP windows.
4361 */
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01004362 htx_to_buf(res_htx, &res->buf);
4363 if (!channel_is_empty(res))
Christopher Fauleta0bdec32022-04-04 07:51:21 +02004364 cs_stop_get(cs);
Christopher Fauletef779222018-10-31 08:47:01 +01004365}
4366
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004367/* Dump all fields from <info> into <out> using the "show info" format (name: value) */
Willy Tarreau83061a82018-07-13 11:56:34 +02004368static int stats_dump_info_fields(struct buffer *out,
Willy Tarreau43241ff2019-10-09 11:27:51 +02004369 const struct field *info, unsigned int flags)
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004370{
4371 int field;
4372
4373 for (field = 0; field < INF_TOTAL_FIELDS; field++) {
4374 if (!field_format(info, field))
4375 continue;
4376
Willy Tarreaueaa55372019-10-09 07:39:11 +02004377 if (!chunk_appendf(out, "%s: ", info_fields[field].name))
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004378 return 0;
4379 if (!stats_emit_raw_data_field(out, &info[field]))
4380 return 0;
Willy Tarreau6b19b142019-10-09 15:44:21 +02004381 if ((flags & STAT_SHOW_FDESC) && !chunk_appendf(out, ":\"%s\"", info_fields[field].desc))
4382 return 0;
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004383 if (!chunk_strcat(out, "\n"))
4384 return 0;
4385 }
4386 return 1;
4387}
4388
4389/* Dump all fields from <info> into <out> using the "show info typed" format */
Willy Tarreau83061a82018-07-13 11:56:34 +02004390static int stats_dump_typed_info_fields(struct buffer *out,
Willy Tarreau43241ff2019-10-09 11:27:51 +02004391 const struct field *info, unsigned int flags)
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004392{
4393 int field;
4394
4395 for (field = 0; field < INF_TOTAL_FIELDS; field++) {
4396 if (!field_format(info, field))
4397 continue;
4398
Willy Tarreaueaa55372019-10-09 07:39:11 +02004399 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 +01004400 return 0;
4401 if (!stats_emit_field_tags(out, &info[field], ':'))
4402 return 0;
4403 if (!stats_emit_typed_data_field(out, &info[field]))
4404 return 0;
Willy Tarreau6b19b142019-10-09 15:44:21 +02004405 if ((flags & STAT_SHOW_FDESC) && !chunk_appendf(out, ":\"%s\"", info_fields[field].desc))
4406 return 0;
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004407 if (!chunk_strcat(out, "\n"))
4408 return 0;
4409 }
4410 return 1;
4411}
4412
Willy Tarreau0b26b382021-05-08 07:43:53 +02004413/* Fill <info> with HAProxy global info. <info> is preallocated array of length
4414 * <len>. The length of the array must be INF_TOTAL_FIELDS. If this length is
4415 * less then this value, the function returns 0, otherwise, it returns 1. Some
4416 * fields' presence or precision may depend on some of the STAT_* flags present
4417 * in <flags>.
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004418 */
Willy Tarreau0b26b382021-05-08 07:43:53 +02004419int stats_fill_info(struct field *info, int len, uint flags)
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004420{
Willy Tarreaud37e26e2021-05-08 07:40:52 +02004421 struct timeval up;
Willy Tarreau83061a82018-07-13 11:56:34 +02004422 struct buffer *out = get_trash_chunk();
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004423
4424#ifdef USE_OPENSSL
Willy Tarreauc5977722021-05-08 08:14:04 +02004425 double ssl_sess_rate = read_freq_ctr_flt(&global.ssl_per_sec);
4426 double ssl_key_rate = read_freq_ctr_flt(&global.ssl_fe_keys_per_sec);
4427 double ssl_reuse = 0;
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004428
Willy Tarreauc5977722021-05-08 08:14:04 +02004429 if (ssl_key_rate < ssl_sess_rate)
4430 ssl_reuse = 100.0 * (1.0 - ssl_key_rate / ssl_sess_rate);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004431#endif
4432
Willy Tarreaud37e26e2021-05-08 07:40:52 +02004433 tv_remain(&start_date, &now, &up);
4434
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004435 if (len < INF_TOTAL_FIELDS)
4436 return 0;
4437
4438 chunk_reset(out);
4439 memset(info, 0, sizeof(*info) * len);
4440
4441 info[INF_NAME] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, PRODUCT_NAME);
Willy Tarreau909b9d82019-01-04 18:20:32 +01004442 info[INF_VERSION] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, haproxy_version);
Adis Nezirovicb62b78b2021-01-15 13:12:33 +01004443 info[INF_BUILD_INFO] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, haproxy_version);
Willy Tarreau909b9d82019-01-04 18:20:32 +01004444 info[INF_RELEASE_DATE] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, haproxy_date);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004445
Yves Lafon95317282018-02-26 11:10:37 +01004446 info[INF_NBTHREAD] = mkf_u32(FO_CONFIG|FS_SERVICE, global.nbthread);
Willy Tarreau91358592021-06-15 08:08:04 +02004447 info[INF_NBPROC] = mkf_u32(FO_CONFIG|FS_SERVICE, 1);
Willy Tarreaue8422bf2021-06-15 09:08:18 +02004448 info[INF_PROCESS_NUM] = mkf_u32(FO_KEY, 1);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004449 info[INF_PID] = mkf_u32(FO_STATUS, pid);
4450
4451 info[INF_UPTIME] = mkf_str(FN_DURATION, chunk_newstr(out));
Willy Tarreaud37e26e2021-05-08 07:40:52 +02004452 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 +01004453
Willy Tarreaue8abc322021-05-08 07:56:56 +02004454 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);
4455 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 +01004456 info[INF_MEMMAX_MB] = mkf_u32(FO_CONFIG|FN_LIMIT, global.rlimit_memmax);
William Dauchya8766cf2021-01-15 22:41:37 +01004457 info[INF_MEMMAX_BYTES] = mkf_u32(FO_CONFIG|FN_LIMIT, global.rlimit_memmax * 1048576L);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004458 info[INF_POOL_ALLOC_MB] = mkf_u32(0, (unsigned)(pool_total_allocated() / 1048576L));
William Dauchya8766cf2021-01-15 22:41:37 +01004459 info[INF_POOL_ALLOC_BYTES] = mkf_u32(0, pool_total_allocated());
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004460 info[INF_POOL_USED_MB] = mkf_u32(0, (unsigned)(pool_total_used() / 1048576L));
William Dauchya8766cf2021-01-15 22:41:37 +01004461 info[INF_POOL_USED_BYTES] = mkf_u32(0, pool_total_used());
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004462 info[INF_POOL_FAILED] = mkf_u32(FN_COUNTER, pool_total_failures());
4463 info[INF_ULIMIT_N] = mkf_u32(FO_CONFIG|FN_LIMIT, global.rlimit_nofile);
4464 info[INF_MAXSOCK] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxsock);
4465 info[INF_MAXCONN] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxconn);
4466 info[INF_HARD_MAXCONN] = mkf_u32(FO_CONFIG|FN_LIMIT, global.hardmaxconn);
4467 info[INF_CURR_CONN] = mkf_u32(0, actconn);
4468 info[INF_CUM_CONN] = mkf_u32(FN_COUNTER, totalconn);
4469 info[INF_CUM_REQ] = mkf_u32(FN_COUNTER, global.req_count);
4470#ifdef USE_OPENSSL
4471 info[INF_MAX_SSL_CONNS] = mkf_u32(FN_MAX, global.maxsslconn);
Willy Tarreau82531f62021-10-06 12:15:18 +02004472 info[INF_CURR_SSL_CONNS] = mkf_u32(0, global.sslconns);
4473 info[INF_CUM_SSL_CONNS] = mkf_u32(FN_COUNTER, global.totalsslconns);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004474#endif
4475 info[INF_MAXPIPES] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxpipes);
4476 info[INF_PIPES_USED] = mkf_u32(0, pipes_used);
4477 info[INF_PIPES_FREE] = mkf_u32(0, pipes_free);
Willy Tarreauc5977722021-05-08 08:14:04 +02004478 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 +01004479 info[INF_CONN_RATE_LIMIT] = mkf_u32(FO_CONFIG|FN_LIMIT, global.cps_lim);
4480 info[INF_MAX_CONN_RATE] = mkf_u32(FN_MAX, global.cps_max);
Willy Tarreauc5977722021-05-08 08:14:04 +02004481 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 +01004482 info[INF_SESS_RATE_LIMIT] = mkf_u32(FO_CONFIG|FN_LIMIT, global.sps_lim);
4483 info[INF_MAX_SESS_RATE] = mkf_u32(FN_RATE, global.sps_max);
4484
4485#ifdef USE_OPENSSL
Willy Tarreauc5977722021-05-08 08:14:04 +02004486 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 +01004487 info[INF_SSL_RATE_LIMIT] = mkf_u32(FO_CONFIG|FN_LIMIT, global.ssl_lim);
4488 info[INF_MAX_SSL_RATE] = mkf_u32(FN_MAX, global.ssl_max);
Willy Tarreauc5977722021-05-08 08:14:04 +02004489 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 +01004490 info[INF_SSL_FRONTEND_MAX_KEY_RATE] = mkf_u32(FN_MAX, global.ssl_fe_keys_max);
Willy Tarreauc5977722021-05-08 08:14:04 +02004491 info[INF_SSL_FRONTEND_SESSION_REUSE_PCT] = (flags & STAT_USE_FLOAT) ? mkf_flt(FN_RATE, ssl_reuse) : mkf_u32(0, ssl_reuse);
4492 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 +01004493 info[INF_SSL_BACKEND_MAX_KEY_RATE] = mkf_u32(FN_MAX, global.ssl_be_keys_max);
4494 info[INF_SSL_CACHE_LOOKUPS] = mkf_u32(FN_COUNTER, global.shctx_lookups);
4495 info[INF_SSL_CACHE_MISSES] = mkf_u32(FN_COUNTER, global.shctx_misses);
4496#endif
Willy Tarreauc5977722021-05-08 08:14:04 +02004497 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));
4498 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 +01004499 info[INF_COMPRESS_BPS_RATE_LIM] = mkf_u32(FO_CONFIG|FN_LIMIT, global.comp_rate_lim);
4500#ifdef USE_ZLIB
4501 info[INF_ZLIB_MEM_USAGE] = mkf_u32(0, zlib_used_memory);
4502 info[INF_MAX_ZLIB_MEM_USAGE] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxzlibmem);
4503#endif
Willy Tarreau955a11e2021-02-24 17:03:30 +01004504 info[INF_TASKS] = mkf_u32(0, total_allocated_tasks());
Willy Tarreau9c7b8082021-02-24 15:10:07 +01004505 info[INF_RUN_QUEUE] = mkf_u32(0, total_run_queues());
Willy Tarreauf9d5e102021-10-08 10:43:59 +02004506 info[INF_IDLE_PCT] = mkf_u32(FN_AVG, clock_report_idle());
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004507 info[INF_NODE] = mkf_str(FO_CONFIG|FN_OUTPUT|FS_SERVICE, global.node);
4508 if (global.desc)
4509 info[INF_DESCRIPTION] = mkf_str(FO_CONFIG|FN_OUTPUT|FS_SERVICE, global.desc);
Willy Tarreau00098ea2018-11-05 14:38:13 +01004510 info[INF_STOPPING] = mkf_u32(0, stopping);
4511 info[INF_JOBS] = mkf_u32(0, jobs);
William Lallemanda7199262018-11-16 16:57:20 +01004512 info[INF_UNSTOPPABLE_JOBS] = mkf_u32(0, unstoppable_jobs);
Willy Tarreau00098ea2018-11-05 14:38:13 +01004513 info[INF_LISTENERS] = mkf_u32(0, listeners);
Willy Tarreau199ad242018-11-05 16:31:22 +01004514 info[INF_ACTIVE_PEERS] = mkf_u32(0, active_peers);
Willy Tarreau2d372c22018-11-05 17:12:27 +01004515 info[INF_CONNECTED_PEERS] = mkf_u32(0, connected_peers);
Willy Tarreau13ef7732018-11-12 07:25:28 +01004516 info[INF_DROPPED_LOGS] = mkf_u32(0, dropped_logs);
Willy Tarreaubeb859a2018-11-22 18:07:59 +01004517 info[INF_BUSY_POLLING] = mkf_u32(0, !!(global.tune.options & GTUNE_BUSY_POLLING));
Emeric Brund30e9a12020-12-23 18:49:16 +01004518 info[INF_FAILED_RESOLUTIONS] = mkf_u32(0, resolv_failed_resolutions);
Willy Tarreau7cf0e452019-05-23 11:39:14 +02004519 info[INF_TOTAL_BYTES_OUT] = mkf_u64(0, global.out_bytes);
Christopher Fauletaaa70852020-07-10 13:56:30 +02004520 info[INF_TOTAL_SPLICED_BYTES_OUT] = mkf_u64(0, global.spliced_out_bytes);
Willy Tarreau7cf0e452019-05-23 11:39:14 +02004521 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 +02004522 info[INF_DEBUG_COMMANDS_ISSUED] = mkf_u32(0, debug_commands_issued);
Emeric Brun45c457a2020-07-09 23:23:34 +02004523 info[INF_CUM_LOG_MSGS] = mkf_u32(FN_COUNTER, cum_log_messages);
Amaury Denoyelle5dfdf3e2021-05-05 17:09:12 +02004524
4525 info[INF_TAINTED] = mkf_str(FO_STATUS, chunk_newstr(out));
4526 chunk_appendf(out, "%#x", get_tainted());
4527
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004528 return 1;
4529}
4530
Christopher Faulet908628c2022-03-25 16:43:49 +01004531/* This function dumps information onto the conn-stream's read buffer.
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004532 * It returns 0 as long as it does not complete, non-zero upon completion.
4533 * No state is used.
4534 */
Christopher Faulet908628c2022-03-25 16:43:49 +01004535static int stats_dump_info_to_buffer(struct conn_stream *cs)
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004536{
Christopher Faulet908628c2022-03-25 16:43:49 +01004537 struct appctx *appctx = __cs_appctx(cs);
Christopher Faulet2da02ae2022-02-24 13:45:27 +01004538
Willy Tarreau0b26b382021-05-08 07:43:53 +02004539 if (!stats_fill_info(info, INF_TOTAL_FIELDS, appctx->ctx.stats.flags))
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004540 return 0;
4541
4542 chunk_reset(&trash);
4543
4544 if (appctx->ctx.stats.flags & STAT_FMT_TYPED)
Willy Tarreau43241ff2019-10-09 11:27:51 +02004545 stats_dump_typed_info_fields(&trash, info, appctx->ctx.stats.flags);
Simon Horman05ee2132017-01-04 09:37:25 +01004546 else if (appctx->ctx.stats.flags & STAT_FMT_JSON)
Willy Tarreau43241ff2019-10-09 11:27:51 +02004547 stats_dump_json_info_fields(&trash, info, appctx->ctx.stats.flags);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004548 else
Willy Tarreau43241ff2019-10-09 11:27:51 +02004549 stats_dump_info_fields(&trash, info, appctx->ctx.stats.flags);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004550
Christopher Faulet908628c2022-03-25 16:43:49 +01004551 if (ci_putchk(cs_ic(cs), &trash) == -1) {
Christopher Fauleta0bdec32022-04-04 07:51:21 +02004552 cs_rx_room_blk(cs);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004553 return 0;
4554 }
4555
4556 return 1;
4557}
4558
Simon Horman6f6bb382017-01-04 09:37:26 +01004559/* This function dumps the schema onto the stream interface's read buffer.
4560 * It returns 0 as long as it does not complete, non-zero upon completion.
4561 * No state is used.
4562 *
Ilya Shipitsinf38a0182020-12-21 01:16:17 +05004563 * Integer values bounded to the range [-(2**53)+1, (2**53)-1] as
Simon Horman6f6bb382017-01-04 09:37:26 +01004564 * per the recommendation for interoperable integers in section 6 of RFC 7159.
4565 */
Willy Tarreau83061a82018-07-13 11:56:34 +02004566static void stats_dump_json_schema(struct buffer *out)
Simon Horman6f6bb382017-01-04 09:37:26 +01004567{
4568
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004569 int old_len = out->data;
Simon Horman6f6bb382017-01-04 09:37:26 +01004570
4571 chunk_strcat(out,
4572 "{"
4573 "\"$schema\":\"http://json-schema.org/draft-04/schema#\","
4574 "\"oneOf\":["
4575 "{"
4576 "\"title\":\"Info\","
4577 "\"type\":\"array\","
4578 "\"items\":{"
Amaury Denoyellea53ce4c2020-10-02 18:31:59 +02004579 "\"title\":\"InfoItem\","
4580 "\"type\":\"object\","
Simon Horman6f6bb382017-01-04 09:37:26 +01004581 "\"properties\":{"
Simon Horman6f6bb382017-01-04 09:37:26 +01004582 "\"field\":{\"$ref\":\"#/definitions/field\"},"
4583 "\"processNum\":{\"$ref\":\"#/definitions/processNum\"},"
4584 "\"tags\":{\"$ref\":\"#/definitions/tags\"},"
4585 "\"value\":{\"$ref\":\"#/definitions/typedValue\"}"
4586 "},"
4587 "\"required\":[\"field\",\"processNum\",\"tags\","
4588 "\"value\"]"
4589 "}"
4590 "},"
4591 "{"
4592 "\"title\":\"Stat\","
4593 "\"type\":\"array\","
4594 "\"items\":{"
4595 "\"title\":\"InfoItem\","
4596 "\"type\":\"object\","
4597 "\"properties\":{"
4598 "\"objType\":{"
4599 "\"enum\":[\"Frontend\",\"Backend\",\"Listener\","
4600 "\"Server\",\"Unknown\"]"
4601 "},"
4602 "\"proxyId\":{"
4603 "\"type\":\"integer\","
4604 "\"minimum\":0"
4605 "},"
4606 "\"id\":{"
4607 "\"type\":\"integer\","
4608 "\"minimum\":0"
4609 "},"
4610 "\"field\":{\"$ref\":\"#/definitions/field\"},"
4611 "\"processNum\":{\"$ref\":\"#/definitions/processNum\"},"
4612 "\"tags\":{\"$ref\":\"#/definitions/tags\"},"
4613 "\"typedValue\":{\"$ref\":\"#/definitions/typedValue\"}"
4614 "},"
4615 "\"required\":[\"objType\",\"proxyId\",\"id\","
4616 "\"field\",\"processNum\",\"tags\","
4617 "\"value\"]"
4618 "}"
4619 "},"
4620 "{"
4621 "\"title\":\"Error\","
4622 "\"type\":\"object\","
4623 "\"properties\":{"
4624 "\"errorStr\":{"
4625 "\"type\":\"string\""
Amaury Denoyellea53ce4c2020-10-02 18:31:59 +02004626 "}"
4627 "},"
4628 "\"required\":[\"errorStr\"]"
Simon Horman6f6bb382017-01-04 09:37:26 +01004629 "}"
4630 "],"
4631 "\"definitions\":{"
4632 "\"field\":{"
4633 "\"type\":\"object\","
4634 "\"pos\":{"
4635 "\"type\":\"integer\","
4636 "\"minimum\":0"
4637 "},"
4638 "\"name\":{"
4639 "\"type\":\"string\""
4640 "},"
4641 "\"required\":[\"pos\",\"name\"]"
4642 "},"
4643 "\"processNum\":{"
4644 "\"type\":\"integer\","
4645 "\"minimum\":1"
4646 "},"
4647 "\"tags\":{"
4648 "\"type\":\"object\","
4649 "\"origin\":{"
4650 "\"type\":\"string\","
4651 "\"enum\":[\"Metric\",\"Status\",\"Key\","
4652 "\"Config\",\"Product\",\"Unknown\"]"
4653 "},"
4654 "\"nature\":{"
4655 "\"type\":\"string\","
4656 "\"enum\":[\"Gauge\",\"Limit\",\"Min\",\"Max\","
4657 "\"Rate\",\"Counter\",\"Duration\","
4658 "\"Age\",\"Time\",\"Name\",\"Output\","
4659 "\"Avg\", \"Unknown\"]"
4660 "},"
4661 "\"scope\":{"
4662 "\"type\":\"string\","
4663 "\"enum\":[\"Cluster\",\"Process\",\"Service\","
4664 "\"System\",\"Unknown\"]"
4665 "},"
4666 "\"required\":[\"origin\",\"nature\",\"scope\"]"
4667 "},"
4668 "\"typedValue\":{"
4669 "\"type\":\"object\","
4670 "\"oneOf\":["
4671 "{\"$ref\":\"#/definitions/typedValue/definitions/s32Value\"},"
4672 "{\"$ref\":\"#/definitions/typedValue/definitions/s64Value\"},"
4673 "{\"$ref\":\"#/definitions/typedValue/definitions/u32Value\"},"
4674 "{\"$ref\":\"#/definitions/typedValue/definitions/u64Value\"},"
4675 "{\"$ref\":\"#/definitions/typedValue/definitions/strValue\"}"
4676 "],"
4677 "\"definitions\":{"
4678 "\"s32Value\":{"
4679 "\"properties\":{"
4680 "\"type\":{"
4681 "\"type\":\"string\","
4682 "\"enum\":[\"s32\"]"
4683 "},"
4684 "\"value\":{"
4685 "\"type\":\"integer\","
4686 "\"minimum\":-2147483648,"
4687 "\"maximum\":2147483647"
4688 "}"
4689 "},"
4690 "\"required\":[\"type\",\"value\"]"
4691 "},"
4692 "\"s64Value\":{"
4693 "\"properties\":{"
4694 "\"type\":{"
4695 "\"type\":\"string\","
4696 "\"enum\":[\"s64\"]"
4697 "},"
4698 "\"value\":{"
4699 "\"type\":\"integer\","
4700 "\"minimum\":-9007199254740991,"
4701 "\"maximum\":9007199254740991"
4702 "}"
4703 "},"
4704 "\"required\":[\"type\",\"value\"]"
4705 "},"
4706 "\"u32Value\":{"
4707 "\"properties\":{"
4708 "\"type\":{"
4709 "\"type\":\"string\","
4710 "\"enum\":[\"u32\"]"
4711 "},"
4712 "\"value\":{"
4713 "\"type\":\"integer\","
4714 "\"minimum\":0,"
4715 "\"maximum\":4294967295"
4716 "}"
4717 "},"
4718 "\"required\":[\"type\",\"value\"]"
4719 "},"
4720 "\"u64Value\":{"
4721 "\"properties\":{"
4722 "\"type\":{"
4723 "\"type\":\"string\","
4724 "\"enum\":[\"u64\"]"
4725 "},"
4726 "\"value\":{"
4727 "\"type\":\"integer\","
4728 "\"minimum\":0,"
4729 "\"maximum\":9007199254740991"
4730 "}"
4731 "},"
4732 "\"required\":[\"type\",\"value\"]"
4733 "},"
4734 "\"strValue\":{"
4735 "\"properties\":{"
4736 "\"type\":{"
4737 "\"type\":\"string\","
4738 "\"enum\":[\"str\"]"
4739 "},"
4740 "\"value\":{\"type\":\"string\"}"
4741 "},"
4742 "\"required\":[\"type\",\"value\"]"
4743 "},"
4744 "\"unknownValue\":{"
4745 "\"properties\":{"
4746 "\"type\":{"
4747 "\"type\":\"integer\","
4748 "\"minimum\":0"
4749 "},"
4750 "\"value\":{"
4751 "\"type\":\"string\","
4752 "\"enum\":[\"unknown\"]"
4753 "}"
4754 "},"
4755 "\"required\":[\"type\",\"value\"]"
4756 "}"
4757 "}"
4758 "}"
4759 "}"
4760 "}");
4761
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004762 if (old_len == out->data) {
Simon Horman6f6bb382017-01-04 09:37:26 +01004763 chunk_reset(out);
4764 chunk_appendf(out,
4765 "{\"errorStr\":\"output buffer too short\"}");
4766 }
4767}
4768
4769/* This function dumps the schema onto the stream interface's read buffer.
4770 * It returns 0 as long as it does not complete, non-zero upon completion.
4771 * No state is used.
4772 */
Christopher Faulet908628c2022-03-25 16:43:49 +01004773static int stats_dump_json_schema_to_buffer(struct conn_stream *cs)
Simon Horman6f6bb382017-01-04 09:37:26 +01004774{
4775 chunk_reset(&trash);
4776
4777 stats_dump_json_schema(&trash);
4778
Christopher Faulet908628c2022-03-25 16:43:49 +01004779 if (ci_putchk(cs_ic(cs), &trash) == -1) {
Christopher Fauleta0bdec32022-04-04 07:51:21 +02004780 cs_rx_room_blk(cs);
Simon Horman6f6bb382017-01-04 09:37:26 +01004781 return 0;
4782 }
4783
4784 return 1;
4785}
4786
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004787static int cli_parse_clear_counters(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau89d467c2016-11-23 11:02:40 +01004788{
4789 struct proxy *px;
4790 struct server *sv;
4791 struct listener *li;
Amaury Denoyelled3700a72020-10-05 11:49:43 +02004792 struct stats_module *mod;
Willy Tarreau89d467c2016-11-23 11:02:40 +01004793 int clrall = 0;
4794
4795 if (strcmp(args[2], "all") == 0)
4796 clrall = 1;
4797
4798 /* check permissions */
4799 if (!cli_has_level(appctx, ACCESS_LVL_OPER) ||
4800 (clrall && !cli_has_level(appctx, ACCESS_LVL_ADMIN)))
4801 return 1;
4802
Olivier Houchardfbc74e82017-11-24 16:54:05 +01004803 for (px = proxies_list; px; px = px->next) {
Willy Tarreau89d467c2016-11-23 11:02:40 +01004804 if (clrall) {
4805 memset(&px->be_counters, 0, sizeof(px->be_counters));
4806 memset(&px->fe_counters, 0, sizeof(px->fe_counters));
4807 }
4808 else {
4809 px->be_counters.conn_max = 0;
4810 px->be_counters.p.http.rps_max = 0;
4811 px->be_counters.sps_max = 0;
4812 px->be_counters.cps_max = 0;
4813 px->be_counters.nbpend_max = 0;
Christopher Fauletefb41f02019-11-08 14:53:15 +01004814 px->be_counters.qtime_max = 0;
4815 px->be_counters.ctime_max = 0;
4816 px->be_counters.dtime_max = 0;
4817 px->be_counters.ttime_max = 0;
Willy Tarreau89d467c2016-11-23 11:02:40 +01004818
4819 px->fe_counters.conn_max = 0;
4820 px->fe_counters.p.http.rps_max = 0;
4821 px->fe_counters.sps_max = 0;
4822 px->fe_counters.cps_max = 0;
Willy Tarreau89d467c2016-11-23 11:02:40 +01004823 }
4824
4825 for (sv = px->srv; sv; sv = sv->next)
4826 if (clrall)
4827 memset(&sv->counters, 0, sizeof(sv->counters));
4828 else {
4829 sv->counters.cur_sess_max = 0;
4830 sv->counters.nbpend_max = 0;
4831 sv->counters.sps_max = 0;
Christopher Fauletefb41f02019-11-08 14:53:15 +01004832 sv->counters.qtime_max = 0;
4833 sv->counters.ctime_max = 0;
4834 sv->counters.dtime_max = 0;
4835 sv->counters.ttime_max = 0;
Willy Tarreau89d467c2016-11-23 11:02:40 +01004836 }
4837
4838 list_for_each_entry(li, &px->conf.listeners, by_fe)
4839 if (li->counters) {
4840 if (clrall)
4841 memset(li->counters, 0, sizeof(*li->counters));
4842 else
4843 li->counters->conn_max = 0;
4844 }
4845 }
4846
4847 global.cps_max = 0;
4848 global.sps_max = 0;
Olivier Houchard00bc3cb2017-10-17 19:23:25 +02004849 global.ssl_max = 0;
4850 global.ssl_fe_keys_max = 0;
4851 global.ssl_be_keys_max = 0;
Willy Tarreaud80cb4e2018-01-20 19:30:13 +01004852
Amaury Denoyelled3700a72020-10-05 11:49:43 +02004853 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
4854 if (!mod->clearable && !clrall)
4855 continue;
4856
4857 for (px = proxies_list; px; px = px->next) {
4858 enum stats_domain_px_cap mod_cap = stats_px_get_cap(mod->domain_flags);
4859
4860 if (px->cap & PR_CAP_FE && mod_cap & STATS_PX_CAP_FE) {
4861 EXTRA_COUNTERS_INIT(px->extra_counters_fe,
4862 mod,
4863 mod->counters,
4864 mod->counters_size);
4865 }
4866
4867 if (px->cap & PR_CAP_BE && mod_cap & STATS_PX_CAP_BE) {
4868 EXTRA_COUNTERS_INIT(px->extra_counters_be,
4869 mod,
4870 mod->counters,
4871 mod->counters_size);
4872 }
4873
4874 if (mod_cap & STATS_PX_CAP_SRV) {
4875 for (sv = px->srv; sv; sv = sv->next) {
4876 EXTRA_COUNTERS_INIT(sv->extra_counters,
4877 mod,
4878 mod->counters,
4879 mod->counters_size);
4880 }
4881 }
4882
4883 if (mod_cap & STATS_PX_CAP_LI) {
4884 list_for_each_entry(li, &px->conf.listeners, by_fe) {
4885 EXTRA_COUNTERS_INIT(li->extra_counters,
4886 mod,
4887 mod->counters,
4888 mod->counters_size);
4889 }
4890 }
4891 }
4892 }
4893
Emeric Brunf8642ee2021-10-29 17:59:18 +02004894 resolv_stats_clear_counters(clrall, &stats_module_list[STATS_DOMAIN_RESOLVERS]);
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02004895
Willy Tarreaud80cb4e2018-01-20 19:30:13 +01004896 memset(activity, 0, sizeof(activity));
Willy Tarreau89d467c2016-11-23 11:02:40 +01004897 return 1;
4898}
4899
4900
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004901static int cli_parse_show_info(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004902{
Willy Tarreau2f397382019-10-09 11:43:59 +02004903 int arg = 2;
4904
Willy Tarreaud25fc792016-12-16 12:33:47 +01004905 appctx->ctx.stats.scope_str = 0;
4906 appctx->ctx.stats.scope_len = 0;
4907 appctx->ctx.stats.flags = 0;
4908
Willy Tarreau2f397382019-10-09 11:43:59 +02004909 while (*args[arg]) {
4910 if (strcmp(args[arg], "typed") == 0)
4911 appctx->ctx.stats.flags = (appctx->ctx.stats.flags & ~STAT_FMT_MASK) | STAT_FMT_TYPED;
4912 else if (strcmp(args[arg], "json") == 0)
4913 appctx->ctx.stats.flags = (appctx->ctx.stats.flags & ~STAT_FMT_MASK) | STAT_FMT_JSON;
4914 else if (strcmp(args[arg], "desc") == 0)
4915 appctx->ctx.stats.flags |= STAT_SHOW_FDESC;
Willy Tarreau27456202021-05-08 07:54:24 +02004916 else if (strcmp(args[arg], "float") == 0)
4917 appctx->ctx.stats.flags |= STAT_USE_FLOAT;
Willy Tarreau2f397382019-10-09 11:43:59 +02004918 arg++;
4919 }
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004920 return 0;
4921}
4922
4923
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004924static int cli_parse_show_stat(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau2b812e22016-11-22 16:18:05 +01004925{
Willy Tarreau2f397382019-10-09 11:43:59 +02004926 int arg = 2;
4927
Willy Tarreaud25fc792016-12-16 12:33:47 +01004928 appctx->ctx.stats.scope_str = 0;
4929 appctx->ctx.stats.scope_len = 0;
Willy Tarreau578d6e42019-10-09 11:00:22 +02004930 appctx->ctx.stats.flags = STAT_SHNODE | STAT_SHDESC;
4931
Christopher Faulet908628c2022-03-25 16:43:49 +01004932 if ((strm_li(__cs_strm(appctx->owner))->bind_conf->level & ACCESS_LVL_MASK) >= ACCESS_LVL_OPER)
Willy Tarreau578d6e42019-10-09 11:00:22 +02004933 appctx->ctx.stats.flags |= STAT_SHLGNDS;
Willy Tarreaud25fc792016-12-16 12:33:47 +01004934
Amaury Denoyelle072f97e2020-10-05 11:49:37 +02004935 /* proxy is the default domain */
4936 appctx->ctx.stats.domain = STATS_DOMAIN_PROXY;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01004937 if (strcmp(args[arg], "domain") == 0) {
Amaury Denoyelle072f97e2020-10-05 11:49:37 +02004938 ++args;
4939
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01004940 if (strcmp(args[arg], "proxy") == 0) {
Amaury Denoyelle072f97e2020-10-05 11:49:37 +02004941 ++args;
Emeric Brunf8642ee2021-10-29 17:59:18 +02004942 } else if (strcmp(args[arg], "resolvers") == 0) {
4943 appctx->ctx.stats.domain = STATS_DOMAIN_RESOLVERS;
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02004944 ++args;
4945 } else {
Amaury Denoyelle072f97e2020-10-05 11:49:37 +02004946 return cli_err(appctx, "Invalid statistics domain.\n");
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02004947 }
Amaury Denoyelle072f97e2020-10-05 11:49:37 +02004948 }
4949
4950 if (appctx->ctx.stats.domain == STATS_DOMAIN_PROXY
4951 && *args[arg] && *args[arg+1] && *args[arg+2]) {
Willy Tarreaua1b1ed52016-11-25 08:50:58 +01004952 struct proxy *px;
4953
Willy Tarreau2f397382019-10-09 11:43:59 +02004954 px = proxy_find_by_name(args[arg], 0, 0);
Willy Tarreaua1b1ed52016-11-25 08:50:58 +01004955 if (px)
4956 appctx->ctx.stats.iid = px->uuid;
4957 else
Willy Tarreau2f397382019-10-09 11:43:59 +02004958 appctx->ctx.stats.iid = atoi(args[arg]);
Willy Tarreaua1b1ed52016-11-25 08:50:58 +01004959
Willy Tarreau9d008692019-08-09 11:21:01 +02004960 if (!appctx->ctx.stats.iid)
4961 return cli_err(appctx, "No such proxy.\n");
Willy Tarreaua1b1ed52016-11-25 08:50:58 +01004962
Willy Tarreau2b812e22016-11-22 16:18:05 +01004963 appctx->ctx.stats.flags |= STAT_BOUND;
Willy Tarreau2f397382019-10-09 11:43:59 +02004964 appctx->ctx.stats.type = atoi(args[arg+1]);
4965 appctx->ctx.stats.sid = atoi(args[arg+2]);
4966 arg += 3;
4967 }
4968
4969 while (*args[arg]) {
4970 if (strcmp(args[arg], "typed") == 0)
4971 appctx->ctx.stats.flags = (appctx->ctx.stats.flags & ~STAT_FMT_MASK) | STAT_FMT_TYPED;
4972 else if (strcmp(args[arg], "json") == 0)
4973 appctx->ctx.stats.flags = (appctx->ctx.stats.flags & ~STAT_FMT_MASK) | STAT_FMT_JSON;
4974 else if (strcmp(args[arg], "desc") == 0)
4975 appctx->ctx.stats.flags |= STAT_SHOW_FDESC;
Willy Tarreau3e320362020-10-23 17:28:57 +02004976 else if (strcmp(args[arg], "no-maint") == 0)
4977 appctx->ctx.stats.flags |= STAT_HIDE_MAINT;
Willy Tarreau65141ff2020-10-23 17:19:48 +02004978 else if (strcmp(args[arg], "up") == 0)
4979 appctx->ctx.stats.flags |= STAT_HIDE_DOWN;
Willy Tarreau2f397382019-10-09 11:43:59 +02004980 arg++;
Willy Tarreau2b812e22016-11-22 16:18:05 +01004981 }
Willy Tarreau2b812e22016-11-22 16:18:05 +01004982
Willy Tarreau2b812e22016-11-22 16:18:05 +01004983 return 0;
4984}
4985
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004986static int cli_io_handler_dump_info(struct appctx *appctx)
4987{
Christopher Faulet908628c2022-03-25 16:43:49 +01004988 return stats_dump_info_to_buffer(appctx->owner);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004989}
4990
Christopher Faulet908628c2022-03-25 16:43:49 +01004991/* This I/O handler runs as an applet embedded in a conn-stream. It is
Willy Tarreau2b812e22016-11-22 16:18:05 +01004992 * used to send raw stats over a socket.
4993 */
4994static int cli_io_handler_dump_stat(struct appctx *appctx)
4995{
Christopher Faulet908628c2022-03-25 16:43:49 +01004996 return stats_dump_stat_to_buffer(appctx->owner, NULL, NULL);
Willy Tarreau2b812e22016-11-22 16:18:05 +01004997}
4998
Simon Horman6f6bb382017-01-04 09:37:26 +01004999static int cli_io_handler_dump_json_schema(struct appctx *appctx)
5000{
Christopher Faulet908628c2022-03-25 16:43:49 +01005001 return stats_dump_json_schema_to_buffer(appctx->owner);
Simon Horman6f6bb382017-01-04 09:37:26 +01005002}
5003
Amaury Denoyelle216a1ce2021-03-18 15:48:14 +01005004int stats_allocate_proxy_counters_internal(struct extra_counters **counters,
5005 int type, int px_cap)
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02005006{
5007 struct stats_module *mod;
5008
5009 EXTRA_COUNTERS_REGISTER(counters, type, alloc_failed);
5010
5011 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
5012 if (!(stats_px_get_cap(mod->domain_flags) & px_cap))
5013 continue;
5014
5015 EXTRA_COUNTERS_ADD(mod, *counters, mod->counters, mod->counters_size);
5016 }
5017
5018 EXTRA_COUNTERS_ALLOC(*counters, alloc_failed);
5019
5020 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
5021 if (!(stats_px_get_cap(mod->domain_flags) & px_cap))
5022 continue;
5023
5024 EXTRA_COUNTERS_INIT(*counters, mod, mod->counters, mod->counters_size);
5025 }
5026
5027 return 1;
5028
5029 alloc_failed:
5030 return 0;
5031}
5032
5033/* Initialize and allocate all extra counters for a proxy and its attached
5034 * servers/listeners with all already registered stats module
5035 */
5036int stats_allocate_proxy_counters(struct proxy *px)
5037{
5038 struct server *sv;
5039 struct listener *li;
5040
5041 if (px->cap & PR_CAP_FE) {
5042 if (!stats_allocate_proxy_counters_internal(&px->extra_counters_fe,
5043 COUNTERS_FE,
5044 STATS_PX_CAP_FE)) {
5045 return 0;
5046 }
5047 }
5048
5049 if (px->cap & PR_CAP_BE) {
5050 if (!stats_allocate_proxy_counters_internal(&px->extra_counters_be,
5051 COUNTERS_BE,
5052 STATS_PX_CAP_BE)) {
5053 return 0;
5054 }
5055 }
5056
5057 for (sv = px->srv; sv; sv = sv->next) {
5058 if (!stats_allocate_proxy_counters_internal(&sv->extra_counters,
5059 COUNTERS_SV,
5060 STATS_PX_CAP_SRV)) {
5061 return 0;
5062 }
5063 }
5064
5065 list_for_each_entry(li, &px->conf.listeners, by_fe) {
5066 if (!stats_allocate_proxy_counters_internal(&li->extra_counters,
5067 COUNTERS_LI,
5068 STATS_PX_CAP_LI)) {
5069 return 0;
5070 }
5071 }
5072
5073 return 1;
5074}
5075
Amaury Denoyelle58d395e2020-10-05 11:49:40 +02005076void stats_register_module(struct stats_module *m)
5077{
5078 const uint8_t domain = stats_get_domain(m->domain_flags);
5079
Willy Tarreau2b718102021-04-21 07:32:39 +02005080 LIST_APPEND(&stats_module_list[domain], &m->list);
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02005081 stat_count[domain] += m->stats_count;
Amaury Denoyelle58d395e2020-10-05 11:49:40 +02005082}
5083
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02005084static int allocate_stats_px_postcheck(void)
5085{
5086 struct stats_module *mod;
5087 size_t i = ST_F_TOTAL_FIELDS;
5088 int err_code = 0;
Amaury Denoyelle27373f72020-10-05 16:57:33 +02005089 struct proxy *px;
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02005090
5091 stat_count[STATS_DOMAIN_PROXY] += ST_F_TOTAL_FIELDS;
5092
5093 stat_f[STATS_DOMAIN_PROXY] = malloc(stat_count[STATS_DOMAIN_PROXY] * sizeof(struct name_desc));
5094 if (!stat_f[STATS_DOMAIN_PROXY]) {
5095 ha_alert("stats: cannot allocate all fields for proxy statistics\n");
5096 err_code |= ERR_ALERT | ERR_FATAL;
5097 return err_code;
5098 }
5099
5100 memcpy(stat_f[STATS_DOMAIN_PROXY], stat_fields,
5101 ST_F_TOTAL_FIELDS * sizeof(struct name_desc));
5102
5103 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
5104 memcpy(stat_f[STATS_DOMAIN_PROXY] + i,
5105 mod->stats,
5106 mod->stats_count * sizeof(struct name_desc));
5107 i += mod->stats_count;
5108 }
5109
Amaury Denoyelle27373f72020-10-05 16:57:33 +02005110 for (px = proxies_list; px; px = px->next) {
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02005111 if (!stats_allocate_proxy_counters(px)) {
5112 ha_alert("stats: cannot allocate all counters for proxy statistics\n");
5113 err_code |= ERR_ALERT | ERR_FATAL;
5114 return err_code;
5115 }
5116 }
5117
Christopher Fauletde79cd22021-01-06 07:41:56 +01005118 /* wait per-thread alloc to perform corresponding stat_l allocation */
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02005119
5120 return err_code;
5121}
5122
5123REGISTER_CONFIG_POSTPARSER("allocate-stats-px", allocate_stats_px_postcheck);
5124
Emeric Brunf8642ee2021-10-29 17:59:18 +02005125static int allocate_stats_rslv_postcheck(void)
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02005126{
5127 struct stats_module *mod;
5128 size_t i = 0;
5129 int err_code = 0;
5130
Emeric Brunf8642ee2021-10-29 17:59:18 +02005131 stat_f[STATS_DOMAIN_RESOLVERS] = malloc(stat_count[STATS_DOMAIN_RESOLVERS] * sizeof(struct name_desc));
5132 if (!stat_f[STATS_DOMAIN_RESOLVERS]) {
5133 ha_alert("stats: cannot allocate all fields for resolver statistics\n");
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02005134 err_code |= ERR_ALERT | ERR_FATAL;
5135 return err_code;
5136 }
5137
Emeric Brunf8642ee2021-10-29 17:59:18 +02005138 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_RESOLVERS], list) {
5139 memcpy(stat_f[STATS_DOMAIN_RESOLVERS] + i,
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02005140 mod->stats,
5141 mod->stats_count * sizeof(struct name_desc));
5142 i += mod->stats_count;
5143 }
5144
Emeric Brunf8642ee2021-10-29 17:59:18 +02005145 if (!resolv_allocate_counters(&stats_module_list[STATS_DOMAIN_RESOLVERS])) {
5146 ha_alert("stats: cannot allocate all counters for resolver statistics\n");
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02005147 err_code |= ERR_ALERT | ERR_FATAL;
5148 return err_code;
5149 }
5150
Christopher Fauletde79cd22021-01-06 07:41:56 +01005151 /* wait per-thread alloc to perform corresponding stat_l allocation */
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02005152
5153 return err_code;
5154}
5155
Emeric Brunf8642ee2021-10-29 17:59:18 +02005156REGISTER_CONFIG_POSTPARSER("allocate-stats-resolver", allocate_stats_rslv_postcheck);
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02005157
Christopher Fauletde79cd22021-01-06 07:41:56 +01005158static int allocate_stat_lines_per_thread(void)
5159{
Emeric Brunf8642ee2021-10-29 17:59:18 +02005160 int domains[] = { STATS_DOMAIN_PROXY, STATS_DOMAIN_RESOLVERS }, i;
Christopher Fauletde79cd22021-01-06 07:41:56 +01005161
5162 for (i = 0; i < STATS_DOMAIN_COUNT; ++i) {
5163 const int domain = domains[i];
5164
5165 stat_l[domain] = malloc(stat_count[domain] * sizeof(struct field));
5166 if (!stat_l[domain])
5167 return 0;
5168 }
5169 return 1;
5170}
5171
5172REGISTER_PER_THREAD_ALLOC(allocate_stat_lines_per_thread);
5173
Amaury Denoyelle7f8f6cb2020-11-10 14:24:31 +01005174static int allocate_trash_counters(void)
5175{
5176 struct stats_module *mod;
Emeric Brunf8642ee2021-10-29 17:59:18 +02005177 int domains[] = { STATS_DOMAIN_PROXY, STATS_DOMAIN_RESOLVERS }, i;
Amaury Denoyelle7f8f6cb2020-11-10 14:24:31 +01005178 size_t max_counters_size = 0;
5179
5180 /* calculate the greatest counters used by any stats modules */
5181 for (i = 0; i < STATS_DOMAIN_COUNT; ++i) {
5182 list_for_each_entry(mod, &stats_module_list[domains[i]], list) {
5183 max_counters_size = mod->counters_size > max_counters_size ?
5184 mod->counters_size : max_counters_size;
5185 }
5186 }
5187
5188 /* allocate the trash with the size of the greatest counters */
5189 if (max_counters_size) {
5190 trash_counters = malloc(max_counters_size);
5191 if (!trash_counters) {
5192 ha_alert("stats: cannot allocate trash counters for statistics\n");
5193 return 0;
5194 }
5195 }
5196
5197 return 1;
5198}
5199
5200REGISTER_PER_THREAD_ALLOC(allocate_trash_counters);
5201
Christopher Fauletde79cd22021-01-06 07:41:56 +01005202static void deinit_stat_lines_per_thread(void)
Amaury Denoyellea2a68992020-11-10 14:24:30 +01005203{
Emeric Brunf8642ee2021-10-29 17:59:18 +02005204 int domains[] = { STATS_DOMAIN_PROXY, STATS_DOMAIN_RESOLVERS }, i;
Amaury Denoyellea2a68992020-11-10 14:24:30 +01005205
5206 for (i = 0; i < STATS_DOMAIN_COUNT; ++i) {
5207 const int domain = domains[i];
5208
Willy Tarreau61cfdf42021-02-20 10:46:51 +01005209 ha_free(&stat_l[domain]);
Christopher Fauletde79cd22021-01-06 07:41:56 +01005210 }
5211}
5212
5213
5214REGISTER_PER_THREAD_FREE(deinit_stat_lines_per_thread);
5215
5216static void deinit_stats(void)
5217{
Emeric Brunf8642ee2021-10-29 17:59:18 +02005218 int domains[] = { STATS_DOMAIN_PROXY, STATS_DOMAIN_RESOLVERS }, i;
Christopher Fauletde79cd22021-01-06 07:41:56 +01005219
5220 for (i = 0; i < STATS_DOMAIN_COUNT; ++i) {
5221 const int domain = domains[i];
Amaury Denoyellea2a68992020-11-10 14:24:30 +01005222
5223 if (stat_f[domain])
5224 free(stat_f[domain]);
5225 }
5226}
5227
5228REGISTER_POST_DEINIT(deinit_stats);
5229
Amaury Denoyelle7f8f6cb2020-11-10 14:24:31 +01005230static void free_trash_counters(void)
5231{
5232 if (trash_counters)
5233 free(trash_counters);
5234}
5235
5236REGISTER_PER_THREAD_FREE(free_trash_counters);
5237
Willy Tarreau2b812e22016-11-22 16:18:05 +01005238/* register cli keywords */
5239static struct cli_kw_list cli_kws = {{ },{
Willy Tarreaub205bfd2021-05-07 11:38:37 +02005240 { { "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 +02005241 { { "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 +02005242 { { "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 },
5243 { { "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 +01005244 {{},}
5245}};
5246
Willy Tarreau0108d902018-11-25 19:14:37 +01005247INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
5248
William Lallemand74c24fb2016-11-21 17:18:36 +01005249struct applet http_stats_applet = {
5250 .obj_type = OBJ_TYPE_APPLET,
5251 .name = "<STATS>", /* used for logging */
5252 .fct = http_stats_io_handler,
5253 .release = NULL,
5254};
5255
William Lallemand74c24fb2016-11-21 17:18:36 +01005256/*
5257 * Local variables:
5258 * c-indent-level: 8
5259 * c-basic-offset: 8
5260 * End:
5261 */