blob: 2367fb614f4f9f25e058ed81125374275a420623 [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 Tarreaucea0e1b2020-06-04 17:25:40 +020063#include <haproxy/task.h>
Willy Tarreauc2f7c582020-06-02 18:15:32 +020064#include <haproxy/ticks.h>
Willy Tarreau92b4f132020-06-01 11:05:15 +020065#include <haproxy/time.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020066#include <haproxy/tools.h>
Willy Tarreau8c42b8a2020-06-04 19:27:34 +020067#include <haproxy/uri_auth-t.h>
Willy Tarreaud6788052020-05-27 15:59:00 +020068#include <haproxy/version.h>
William Lallemand74c24fb2016-11-21 17:18:36 +010069
William Lallemand74c24fb2016-11-21 17:18:36 +010070
Christopher Faulet2b9b6782019-02-27 16:42:58 +010071/* status codes available for the stats admin page (strictly 4 chars length) */
72const char *stat_status_codes[STAT_STATUS_SIZE] = {
73 [STAT_STATUS_DENY] = "DENY",
74 [STAT_STATUS_DONE] = "DONE",
75 [STAT_STATUS_ERRP] = "ERRP",
76 [STAT_STATUS_EXCD] = "EXCD",
77 [STAT_STATUS_NONE] = "NONE",
78 [STAT_STATUS_PART] = "PART",
79 [STAT_STATUS_UNKN] = "UNKN",
80 [STAT_STATUS_IVAL] = "IVAL",
81};
82
Willy Tarreau0baac8c2016-11-22 16:36:53 +010083/* These are the field names for each INF_* field position. Please pay attention
84 * to always use the exact same name except that the strings for new names must
85 * be lower case or CamelCase while the enum entries must be upper case.
86 */
Willy Tarreaueaa55372019-10-09 07:39:11 +020087const struct name_desc info_fields[INF_TOTAL_FIELDS] = {
Willy Tarreau6d4897e2019-10-11 16:31:46 +020088 [INF_NAME] = { .name = "Name", .desc = "Product name" },
89 [INF_VERSION] = { .name = "Version", .desc = "Product version" },
90 [INF_RELEASE_DATE] = { .name = "Release_date", .desc = "Date of latest source code update" },
91 [INF_NBTHREAD] = { .name = "Nbthread", .desc = "Number of started threads (global.nbthread)" },
Willy Tarreau91358592021-06-15 08:08:04 +020092 [INF_NBPROC] = { .name = "Nbproc", .desc = "Number of started worker processes (historical, always 1)" },
Willy Tarreaue8422bf2021-06-15 09:08:18 +020093 [INF_PROCESS_NUM] = { .name = "Process_num", .desc = "Relative worker process number (1)" },
Willy Tarreau6d4897e2019-10-11 16:31:46 +020094 [INF_PID] = { .name = "Pid", .desc = "This worker process identifier for the system" },
95 [INF_UPTIME] = { .name = "Uptime", .desc = "How long ago this worker process was started (days+hours+minutes+seconds)" },
96 [INF_UPTIME_SEC] = { .name = "Uptime_sec", .desc = "How long ago this worker process was started (seconds)" },
William Dauchydefd1562021-01-15 22:41:38 +010097 [INF_START_TIME_SEC] = { .name = "Start_time_sec", .desc = "Start time in seconds" },
Willy Tarreau6d4897e2019-10-11 16:31:46 +020098 [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 +010099 [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 +0200100 [INF_POOL_ALLOC_MB] = { .name = "PoolAlloc_MB", .desc = "Amount of memory allocated in pools (in MB)" },
William Dauchya8766cf2021-01-15 22:41:37 +0100101 [INF_POOL_ALLOC_BYTES] = { .name = "PoolAlloc_bytes", .desc = "Amount of memory allocated in pools (in bytes)" },
Willy Tarreau6d4897e2019-10-11 16:31:46 +0200102 [INF_POOL_USED_MB] = { .name = "PoolUsed_MB", .desc = "Amount of pool memory currently used (in MB)" },
William Dauchya8766cf2021-01-15 22:41:37 +0100103 [INF_POOL_USED_BYTES] = { .name = "PoolUsed_bytes", .desc = "Amount of pool memory currently used (in bytes)" },
Willy Tarreau6d4897e2019-10-11 16:31:46 +0200104 [INF_POOL_FAILED] = { .name = "PoolFailed", .desc = "Number of failed pool allocations since this worker was started" },
105 [INF_ULIMIT_N] = { .name = "Ulimit-n", .desc = "Hard limit on the number of per-process file descriptors" },
106 [INF_MAXSOCK] = { .name = "Maxsock", .desc = "Hard limit on the number of per-process sockets" },
107 [INF_MAXCONN] = { .name = "Maxconn", .desc = "Hard limit on the number of per-process connections (configured or imposed by Ulimit-n)" },
108 [INF_HARD_MAXCONN] = { .name = "Hard_maxconn", .desc = "Hard limit on the number of per-process connections (imposed by Memmax_MB or Ulimit-n)" },
109 [INF_CURR_CONN] = { .name = "CurrConns", .desc = "Current number of connections on this worker process" },
110 [INF_CUM_CONN] = { .name = "CumConns", .desc = "Total number of connections on this worker process since started" },
111 [INF_CUM_REQ] = { .name = "CumReq", .desc = "Total number of requests on this worker process since started" },
112 [INF_MAX_SSL_CONNS] = { .name = "MaxSslConns", .desc = "Hard limit on the number of per-process SSL endpoints (front+back), 0=unlimited" },
113 [INF_CURR_SSL_CONNS] = { .name = "CurrSslConns", .desc = "Current number of SSL endpoints on this worker process (front+back)" },
114 [INF_CUM_SSL_CONNS] = { .name = "CumSslConns", .desc = "Total number of SSL endpoints on this worker process since started (front+back)" },
115 [INF_MAXPIPES] = { .name = "Maxpipes", .desc = "Hard limit on the number of pipes for splicing, 0=unlimited" },
116 [INF_PIPES_USED] = { .name = "PipesUsed", .desc = "Current number of pipes in use in this worker process" },
117 [INF_PIPES_FREE] = { .name = "PipesFree", .desc = "Current number of allocated and available pipes in this worker process" },
118 [INF_CONN_RATE] = { .name = "ConnRate", .desc = "Number of front connections created on this worker process over the last second" },
119 [INF_CONN_RATE_LIMIT] = { .name = "ConnRateLimit", .desc = "Hard limit for ConnRate (global.maxconnrate)" },
120 [INF_MAX_CONN_RATE] = { .name = "MaxConnRate", .desc = "Highest ConnRate reached on this worker process since started (in connections per second)" },
121 [INF_SESS_RATE] = { .name = "SessRate", .desc = "Number of sessions created on this worker process over the last second" },
122 [INF_SESS_RATE_LIMIT] = { .name = "SessRateLimit", .desc = "Hard limit for SessRate (global.maxsessrate)" },
123 [INF_MAX_SESS_RATE] = { .name = "MaxSessRate", .desc = "Highest SessRate reached on this worker process since started (in sessions per second)" },
124 [INF_SSL_RATE] = { .name = "SslRate", .desc = "Number of SSL connections created on this worker process over the last second" },
125 [INF_SSL_RATE_LIMIT] = { .name = "SslRateLimit", .desc = "Hard limit for SslRate (global.maxsslrate)" },
126 [INF_MAX_SSL_RATE] = { .name = "MaxSslRate", .desc = "Highest SslRate reached on this worker process since started (in connections per second)" },
127 [INF_SSL_FRONTEND_KEY_RATE] = { .name = "SslFrontendKeyRate", .desc = "Number of SSL keys created on frontends in this worker process over the last second" },
128 [INF_SSL_FRONTEND_MAX_KEY_RATE] = { .name = "SslFrontendMaxKeyRate", .desc = "Highest SslFrontendKeyRate reached on this worker process since started (in SSL keys per second)" },
129 [INF_SSL_FRONTEND_SESSION_REUSE_PCT] = { .name = "SslFrontendSessionReuse_pct", .desc = "Percent of frontend SSL connections which did not require a new key" },
130 [INF_SSL_BACKEND_KEY_RATE] = { .name = "SslBackendKeyRate", .desc = "Number of SSL keys created on backends in this worker process over the last second" },
131 [INF_SSL_BACKEND_MAX_KEY_RATE] = { .name = "SslBackendMaxKeyRate", .desc = "Highest SslBackendKeyRate reached on this worker process since started (in SSL keys per second)" },
132 [INF_SSL_CACHE_LOOKUPS] = { .name = "SslCacheLookups", .desc = "Total number of SSL session ID lookups in the SSL session cache on this worker since started" },
133 [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" },
134 [INF_COMPRESS_BPS_IN] = { .name = "CompressBpsIn", .desc = "Number of bytes submitted to the HTTP compressor in this worker process over the last second" },
135 [INF_COMPRESS_BPS_OUT] = { .name = "CompressBpsOut", .desc = "Number of bytes emitted by the HTTP compressor in this worker process over the last second" },
136 [INF_COMPRESS_BPS_RATE_LIM] = { .name = "CompressBpsRateLim", .desc = "Limit of CompressBpsOut beyond which HTTP compression is automatically disabled" },
137 [INF_ZLIB_MEM_USAGE] = { .name = "ZlibMemUsage", .desc = "Amount of memory currently used by HTTP compression on the current worker process (in bytes)" },
138 [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)" },
139 [INF_TASKS] = { .name = "Tasks", .desc = "Total number of tasks in the current worker process (active + sleeping)" },
140 [INF_RUN_QUEUE] = { .name = "Run_queue", .desc = "Total number of active tasks+tasklets in the current worker process" },
141 [INF_IDLE_PCT] = { .name = "Idle_pct", .desc = "Percentage of last second spent waiting in the current worker thread" },
142 [INF_NODE] = { .name = "node", .desc = "Node name (global.node)" },
143 [INF_DESCRIPTION] = { .name = "description", .desc = "Node description (global.description)" },
144 [INF_STOPPING] = { .name = "Stopping", .desc = "1 if the worker process is currently stopping, otherwise zero" },
145 [INF_JOBS] = { .name = "Jobs", .desc = "Current number of active jobs on the current worker process (frontend connections, master connections, listeners)" },
146 [INF_UNSTOPPABLE_JOBS] = { .name = "Unstoppable Jobs", .desc = "Current number of unstoppable jobs on the current worker process (master connections)" },
147 [INF_LISTENERS] = { .name = "Listeners", .desc = "Current number of active listeners on the current worker process" },
148 [INF_ACTIVE_PEERS] = { .name = "ActivePeers", .desc = "Current number of verified active peers connections on the current worker process" },
149 [INF_CONNECTED_PEERS] = { .name = "ConnectedPeers", .desc = "Current number of peers having passed the connection step on the current worker process" },
150 [INF_DROPPED_LOGS] = { .name = "DroppedLogs", .desc = "Total number of dropped logs for current worker process since started" },
151 [INF_BUSY_POLLING] = { .name = "BusyPolling", .desc = "1 if busy-polling is currently in use on the worker process, otherwise zero (config.busy-polling)" },
152 [INF_FAILED_RESOLUTIONS] = { .name = "FailedResolutions", .desc = "Total number of failed DNS resolutions in current worker process since started" },
153 [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 +0200154 [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 +0200155 [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 +0200156 [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 +0200157 [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 +0100158 [INF_BUILD_INFO] = { .name = "Build info", .desc = "Build info" },
Amaury Denoyelle5dfdf3e2021-05-05 17:09:12 +0200159 [INF_TAINTED] = { .name = "Tainted", .desc = "Experimental features used" },
Willy Tarreau0baac8c2016-11-22 16:36:53 +0100160};
161
Willy Tarreaueaa55372019-10-09 07:39:11 +0200162const struct name_desc stat_fields[ST_F_TOTAL_FIELDS] = {
Willy Tarreau6d4897e2019-10-11 16:31:46 +0200163 [ST_F_PXNAME] = { .name = "pxname", .desc = "Proxy name" },
164 [ST_F_SVNAME] = { .name = "svname", .desc = "Server name" },
William Dauchyeedb9b12021-02-01 13:11:53 +0100165 [ST_F_QCUR] = { .name = "qcur", .desc = "Number of current queued connections" },
166 [ST_F_QMAX] = { .name = "qmax", .desc = "Highest value of queued connections encountered since process started" },
William Dauchy19f7cfc2021-02-01 13:11:54 +0100167 [ST_F_SCUR] = { .name = "scur", .desc = "Number of current sessions on the frontend, backend or server" },
168 [ST_F_SMAX] = { .name = "smax", .desc = "Highest value of current sessions encountered since process started" },
Willy Tarreau6d4897e2019-10-11 16:31:46 +0200169 [ST_F_SLIM] = { .name = "slim", .desc = "Frontend/listener/server's maxconn, backend's fullconn" },
170 [ST_F_STOT] = { .name = "stot", .desc = "Total number of sessions since process started" },
171 [ST_F_BIN] = { .name = "bin", .desc = "Total number of request bytes since process started" },
172 [ST_F_BOUT] = { .name = "bout", .desc = "Total number of response bytes since process started" },
173 [ST_F_DREQ] = { .name = "dreq", .desc = "Total number of denied requests since process started" },
174 [ST_F_DRESP] = { .name = "dresp", .desc = "Total number of denied responses since process started" },
175 [ST_F_EREQ] = { .name = "ereq", .desc = "Total number of invalid requests since process started" },
176 [ST_F_ECON] = { .name = "econ", .desc = "Total number of failed connections to server since the worker process started" },
177 [ST_F_ERESP] = { .name = "eresp", .desc = "Total number of invalid responses since the worker process started" },
178 [ST_F_WRETR] = { .name = "wretr", .desc = "Total number of server connection retries since the worker process started" },
179 [ST_F_WREDIS] = { .name = "wredis", .desc = "Total number of server redispatches due to connection failures since the worker process started" },
180 [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 +0200181 [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 +0200182 [ST_F_ACT] = { .name = "act", .desc = "Total number of active UP servers with a non-zero weight" },
183 [ST_F_BCK] = { .name = "bck", .desc = "Total number of backup UP servers with a non-zero weight" },
184 [ST_F_CHKFAIL] = { .name = "chkfail", .desc = "Total number of failed individual health checks per server/backend, since the worker process started" },
185 [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" },
186 [ST_F_LASTCHG] = { .name = "lastchg", .desc = "How long ago the last server state changed, in seconds" },
187 [ST_F_DOWNTIME] = { .name = "downtime", .desc = "Total time spent in DOWN state, for server or backend" },
188 [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 +0200189 [ST_F_PID] = { .name = "pid", .desc = "Relative worker process number (1)" },
Willy Tarreau6d4897e2019-10-11 16:31:46 +0200190 [ST_F_IID] = { .name = "iid", .desc = "Frontend or Backend numeric identifier ('id' setting)" },
191 [ST_F_SID] = { .name = "sid", .desc = "Server numeric identifier ('id' setting)" },
192 [ST_F_THROTTLE] = { .name = "throttle", .desc = "Throttling ratio applied to a server's maxconn and weight during the slowstart period (0 to 100%)" },
193 [ST_F_LBTOT] = { .name = "lbtot", .desc = "Total number of requests routed by load balancing since the worker process started (ignores queue pop and stickiness)" },
194 [ST_F_TRACKED] = { .name = "tracked", .desc = "Name of the other server this server tracks for its state" },
195 [ST_F_TYPE] = { .name = "type", .desc = "Type of the object (Listener, Frontend, Backend, Server)" },
196 [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)" },
197 [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 +0100198 [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 +0200199 [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" },
200 [ST_F_CHECK_CODE] = { .name = "check_code", .desc = "HTTP/SMTP/LDAP status code reported by the latest server health check" },
201 [ST_F_CHECK_DURATION] = { .name = "check_duration", .desc = "Total duration of the latest server health check, in milliseconds" },
202 [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" },
203 [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" },
204 [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" },
205 [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" },
206 [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" },
207 [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)" },
208 [ST_F_HANAFAIL] = { .name = "hanafail", .desc = "Total number of failed checks caused by an 'on-error' directive after an 'observe' condition matched" },
209 [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 +0100210 [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 +0200211 [ST_F_REQ_TOT] = { .name = "req_tot", .desc = "Total number of HTTP requests processed by this object since the worker process started" },
212 [ST_F_CLI_ABRT] = { .name = "cli_abrt", .desc = "Total number of requests or connections aborted by the client since the worker process started" },
213 [ST_F_SRV_ABRT] = { .name = "srv_abrt", .desc = "Total number of requests or connections aborted by the server since the worker process started" },
214 [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" },
215 [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" },
216 [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)" },
217 [ST_F_COMP_RSP] = { .name = "comp_rsp", .desc = "Total number of HTTP responses that were compressed for this object since the worker process started" },
218 [ST_F_LASTSESS] = { .name = "lastsess", .desc = "How long ago some traffic was seen on this object on this worker process, in seconds" },
219 [ST_F_LAST_CHK] = { .name = "last_chk", .desc = "Short description of the latest health check report for this server (see also check_desc)" },
220 [ST_F_LAST_AGT] = { .name = "last_agt", .desc = "Short description of the latest agent check report for this server (see also agent_desc)" },
221 [ST_F_QTIME] = { .name = "qtime", .desc = "Time spent in the queue, in milliseconds, averaged over the 1024 last requests (backend/server)" },
222 [ST_F_CTIME] = { .name = "ctime", .desc = "Time spent waiting for a connection to complete, in milliseconds, averaged over the 1024 last requests (backend/server)" },
223 [ST_F_RTIME] = { .name = "rtime", .desc = "Time spent waiting for a server response, in milliseconds, averaged over the 1024 last requests (backend/server)" },
224 [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)" },
225 [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" },
226 [ST_F_AGENT_CODE] = { .name = "agent_code", .desc = "Status code reported by the latest server agent check" },
227 [ST_F_AGENT_DURATION] = { .name = "agent_duration", .desc = "Total duration of the latest server agent check, in milliseconds" },
228 [ST_F_CHECK_DESC] = { .name = "check_desc", .desc = "Textual description of the latest health check report for this server" },
229 [ST_F_AGENT_DESC] = { .name = "agent_desc", .desc = "Textual description of the latest agent check report for this server" },
230 [ST_F_CHECK_RISE] = { .name = "check_rise", .desc = "Number of successful health checks before declaring a server UP (server 'rise' setting)" },
231 [ST_F_CHECK_FALL] = { .name = "check_fall", .desc = "Number of failed health checks before declaring a server DOWN (server 'fall' setting)" },
232 [ST_F_CHECK_HEALTH] = { .name = "check_health", .desc = "Current server health check level (0..fall-1=DOWN, fall..rise-1=UP)" },
233 [ST_F_AGENT_RISE] = { .name = "agent_rise", .desc = "Number of successful agent checks before declaring a server UP (server 'rise' setting)" },
234 [ST_F_AGENT_FALL] = { .name = "agent_fall", .desc = "Number of failed agent checks before declaring a server DOWN (server 'fall' setting)" },
235 [ST_F_AGENT_HEALTH] = { .name = "agent_health", .desc = "Current server agent check level (0..fall-1=DOWN, fall..rise-1=UP)" },
236 [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" },
237 [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" },
238 [ST_F_MODE] = { .name = "mode", .desc = "'mode' setting (tcp/http/health/cli)" },
239 [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" },
240 [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 +0100241 [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 +0200242 [ST_F_CONN_TOT] = { .name = "conn_tot", .desc = "Total number of new connections accepted on this frontend since the worker process started" },
243 [ST_F_INTERCEPTED] = { .name = "intercepted", .desc = "Total number of HTTP requests intercepted on the frontend (redirects/stats/services) since the worker process started" },
244 [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" },
245 [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" },
246 [ST_F_WREW] = { .name = "wrew", .desc = "Total number of failed HTTP header rewrites since the worker process started" },
247 [ST_F_CONNECT] = { .name = "connect", .desc = "Total number of outgoing connection attempts on this backend/server since the worker process started" },
248 [ST_F_REUSE] = { .name = "reuse", .desc = "Total number of reused connection on this backend/server since the worker process started" },
249 [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" },
250 [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" },
251 [ST_F_SRV_ICUR] = { .name = "srv_icur", .desc = "Current number of idle connections available for reuse on this server" },
252 [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 +0100253 [ST_F_QT_MAX] = { .name = "qtime_max", .desc = "Maximum observed time spent in the queue, in milliseconds (backend/server)" },
254 [ST_F_CT_MAX] = { .name = "ctime_max", .desc = "Maximum observed time spent waiting for a connection to complete, in milliseconds (backend/server)" },
255 [ST_F_RT_MAX] = { .name = "rtime_max", .desc = "Maximum observed time spent waiting for a server response, in milliseconds (backend/server)" },
256 [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 +0100257 [ST_F_EINT] = { .name = "eint", .desc = "Total number of internal errors since process started"},
Willy Tarreau3bb617c2020-06-29 13:51:05 +0200258 [ST_F_IDLE_CONN_CUR] = { .name = "idle_conn_cur", .desc = "Current number of unsafe idle connections"},
259 [ST_F_SAFE_CONN_CUR] = { .name = "safe_conn_cur", .desc = "Current number of safe idle connections"},
260 [ST_F_USED_CONN_CUR] = { .name = "used_conn_cur", .desc = "Current number of connections in use"},
Willy Tarreaua9fcecb2020-06-29 15:38:53 +0200261 [ST_F_NEED_CONN_EST] = { .name = "need_conn_est", .desc = "Estimated needed number of connections"},
Willy Tarreaubd715102020-10-23 22:44:30 +0200262 [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 +0100263 [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 +0100264};
265
Willy Tarreau0baac8c2016-11-22 16:36:53 +0100266/* one line of info */
William Dauchy5d9b8f32021-01-11 20:07:49 +0100267THREAD_LOCAL struct field info[INF_TOTAL_FIELDS];
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +0200268
269/* description of statistics (static and dynamic) */
270static struct name_desc *stat_f[STATS_DOMAIN_COUNT];
271static size_t stat_count[STATS_DOMAIN_COUNT];
272
273/* one line for stats */
William Dauchyb9577452021-01-17 18:27:46 +0100274THREAD_LOCAL struct field *stat_l[STATS_DOMAIN_COUNT];
William Lallemand74c24fb2016-11-21 17:18:36 +0100275
Amaury Denoyelle58d395e2020-10-05 11:49:40 +0200276/* list of all registered stats module */
277static struct list stats_module_list[STATS_DOMAIN_COUNT] = {
278 LIST_HEAD_INIT(stats_module_list[STATS_DOMAIN_PROXY]),
Emeric Brunf8642ee2021-10-29 17:59:18 +0200279 LIST_HEAD_INIT(stats_module_list[STATS_DOMAIN_RESOLVERS]),
Amaury Denoyelle58d395e2020-10-05 11:49:40 +0200280};
281
Amaury Denoyelle7f8f6cb2020-11-10 14:24:31 +0100282THREAD_LOCAL void *trash_counters;
283
Amaury Denoyelle072f97e2020-10-05 11:49:37 +0200284static inline uint8_t stats_get_domain(uint32_t domain)
285{
286 return domain >> STATS_DOMAIN & STATS_DOMAIN_MASK;
287}
288
Amaury Denoyelle72b16e52020-10-05 11:49:38 +0200289static inline enum stats_domain_px_cap stats_px_get_cap(uint32_t domain)
290{
291 return domain >> STATS_PX_CAP & STATS_PX_CAP_MASK;
292}
293
Christopher Faulet6338a082019-09-09 15:50:54 +0200294static void stats_dump_json_schema(struct buffer *out);
William Lallemand74c24fb2016-11-21 17:18:36 +0100295
Amaury Denoyelle3ca927e2020-10-02 18:32:00 +0200296int stats_putchk(struct channel *chn, struct htx *htx, struct buffer *chk)
Christopher Fauletef779222018-10-31 08:47:01 +0100297{
298 if (htx) {
Christopher Faulet69fc88c2019-01-07 14:27:53 +0100299 if (chk->data >= channel_htx_recv_max(chn, htx))
300 return 0;
Willy Tarreau0a7ef022019-05-28 10:30:11 +0200301 if (!htx_add_data_atonce(htx, ist2(chk->area, chk->data)))
Christopher Fauletef779222018-10-31 08:47:01 +0100302 return 0;
Christopher Faulet5adbeeb2019-01-02 14:34:39 +0100303 channel_add_input(chn, chk->data);
Christopher Fauletef779222018-10-31 08:47:01 +0100304 chk->data = 0;
Christopher Fauletef779222018-10-31 08:47:01 +0100305 }
306 else {
307 if (ci_putchk(chn, chk) == -1)
308 return 0;
309 }
310 return 1;
311}
Willy Tarreau0baac8c2016-11-22 16:36:53 +0100312
Christopher Faulet908628c2022-03-25 16:43:49 +0100313static const char *stats_scope_ptr(struct appctx *appctx, struct conn_stream *cs)
Christopher Fauleted7a0662019-01-14 11:07:34 +0100314{
Christopher Faulet908628c2022-03-25 16:43:49 +0100315 struct channel *req = cs_oc(cs);
Christopher Fauletb7f88902019-07-15 21:56:43 +0200316 struct htx *htx = htxbuf(&req->buf);
317 struct htx_blk *blk;
318 struct ist uri;
Christopher Fauleted7a0662019-01-14 11:07:34 +0100319
Christopher Fauletb7f88902019-07-15 21:56:43 +0200320 blk = htx_get_head_blk(htx);
Christopher Fauletea009732019-11-18 15:50:25 +0100321 BUG_ON(!blk || htx_get_blk_type(blk) != HTX_BLK_REQ_SL);
Christopher Fauletb7f88902019-07-15 21:56:43 +0200322 ALREADY_CHECKED(blk);
323 uri = htx_sl_req_uri(htx_get_blk_ptr(htx, blk));
324 return uri.ptr + appctx->ctx.stats.scope_str;
Christopher Fauleted7a0662019-01-14 11:07:34 +0100325}
326
William Lallemand74c24fb2016-11-21 17:18:36 +0100327/*
328 * http_stats_io_handler()
329 * -> stats_dump_stat_to_buffer() // same as above, but used for CSV or HTML
330 * -> stats_dump_csv_header() // emits the CSV headers (same as above)
Simon Horman05ee2132017-01-04 09:37:25 +0100331 * -> stats_dump_json_header() // emits the JSON headers (same as above)
William Lallemand74c24fb2016-11-21 17:18:36 +0100332 * -> stats_dump_html_head() // emits the HTML headers
333 * -> stats_dump_html_info() // emits the equivalent of "show info" at the top
334 * -> stats_dump_proxy_to_buffer() // same as above, valid for CSV and HTML
335 * -> stats_dump_html_px_hdr()
336 * -> stats_dump_fe_stats()
337 * -> stats_dump_li_stats()
338 * -> stats_dump_sv_stats()
339 * -> stats_dump_be_stats()
340 * -> stats_dump_html_px_end()
341 * -> stats_dump_html_end() // emits HTML trailer
Simon Horman05ee2132017-01-04 09:37:25 +0100342 * -> stats_dump_json_end() // emits JSON trailer
William Lallemand74c24fb2016-11-21 17:18:36 +0100343 */
344
345
William Lallemand74c24fb2016-11-21 17:18:36 +0100346/* Dumps the stats CSV header to the trash buffer which. The caller is responsible
347 * for clearing it if needed.
348 * NOTE: Some tools happen to rely on the field position instead of its name,
349 * so please only append new fields at the end, never in the middle.
350 */
Amaury Denoyelle50660a82020-10-05 11:49:39 +0200351static void stats_dump_csv_header(enum stats_domain domain)
William Lallemand74c24fb2016-11-21 17:18:36 +0100352{
353 int field;
354
355 chunk_appendf(&trash, "# ");
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +0200356 if (stat_f[domain]) {
357 for (field = 0; field < stat_count[domain]; ++field) {
358 chunk_appendf(&trash, "%s,", stat_f[domain][field].name);
William Lallemand74c24fb2016-11-21 17:18:36 +0100359
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +0200360 /* print special delimiter on proxy stats to mark end of
361 static fields */
362 if (domain == STATS_DOMAIN_PROXY && field + 1 == ST_F_TOTAL_FIELDS)
363 chunk_appendf(&trash, "-,");
Amaury Denoyelle50660a82020-10-05 11:49:39 +0200364 }
365 }
366
William Lallemand74c24fb2016-11-21 17:18:36 +0100367 chunk_appendf(&trash, "\n");
368}
369
William Lallemand74c24fb2016-11-21 17:18:36 +0100370/* Emits a stats field without any surrounding element and properly encoded to
371 * resist CSV output. Returns non-zero on success, 0 if the buffer is full.
372 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200373int stats_emit_raw_data_field(struct buffer *out, const struct field *f)
William Lallemand74c24fb2016-11-21 17:18:36 +0100374{
375 switch (field_format(f, 0)) {
376 case FF_EMPTY: return 1;
377 case FF_S32: return chunk_appendf(out, "%d", f->u.s32);
378 case FF_U32: return chunk_appendf(out, "%u", f->u.u32);
379 case FF_S64: return chunk_appendf(out, "%lld", (long long)f->u.s64);
380 case FF_U64: return chunk_appendf(out, "%llu", (unsigned long long)f->u.u64);
Willy Tarreau065ba312021-05-08 10:38:20 +0200381 case FF_FLT: {
382 size_t prev_data = out->data;
383 out->data = flt_trim(out->area, prev_data, chunk_appendf(out, "%f", f->u.flt));
384 return out->data;
385 }
William Lallemand74c24fb2016-11-21 17:18:36 +0100386 case FF_STR: return csv_enc_append(field_str(f, 0), 1, out) != NULL;
387 default: return chunk_appendf(out, "[INCORRECT_FIELD_TYPE_%08x]", f->type);
388 }
389}
390
Amaury Denoyellee3f576c2020-10-05 11:49:44 +0200391const char *field_to_html_str(const struct field *f)
392{
393 switch (field_format(f, 0)) {
394 case FF_S32: return U2H(f->u.s32);
395 case FF_S64: return U2H(f->u.s64);
396 case FF_U64: return U2H(f->u.u64);
397 case FF_U32: return U2H(f->u.u32);
Willy Tarreau6004fb72021-05-08 07:37:38 +0200398 case FF_FLT: return F2H(f->u.flt);
Amaury Denoyellee3f576c2020-10-05 11:49:44 +0200399 case FF_STR: return field_str(f, 0);
400 case FF_EMPTY:
401 default:
402 return "";
403 }
404}
405
William Lallemand74c24fb2016-11-21 17:18:36 +0100406/* Emits a stats field prefixed with its type. No CSV encoding is prepared, the
407 * output is supposed to be used on its own line. Returns non-zero on success, 0
408 * if the buffer is full.
409 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200410int stats_emit_typed_data_field(struct buffer *out, const struct field *f)
William Lallemand74c24fb2016-11-21 17:18:36 +0100411{
412 switch (field_format(f, 0)) {
413 case FF_EMPTY: return 1;
414 case FF_S32: return chunk_appendf(out, "s32:%d", f->u.s32);
415 case FF_U32: return chunk_appendf(out, "u32:%u", f->u.u32);
416 case FF_S64: return chunk_appendf(out, "s64:%lld", (long long)f->u.s64);
417 case FF_U64: return chunk_appendf(out, "u64:%llu", (unsigned long long)f->u.u64);
Willy Tarreau065ba312021-05-08 10:38:20 +0200418 case FF_FLT: {
419 size_t prev_data = out->data;
420 out->data = flt_trim(out->area, prev_data, chunk_appendf(out, "flt:%f", f->u.flt));
421 return out->data;
422 }
William Lallemand74c24fb2016-11-21 17:18:36 +0100423 case FF_STR: return chunk_appendf(out, "str:%s", field_str(f, 0));
424 default: return chunk_appendf(out, "%08x:?", f->type);
425 }
426}
427
Simon Horman05ee2132017-01-04 09:37:25 +0100428/* Limit JSON integer values to the range [-(2**53)+1, (2**53)-1] as per
429 * the recommendation for interoperable integers in section 6 of RFC 7159.
430 */
431#define JSON_INT_MAX ((1ULL << 53) - 1)
432#define JSON_INT_MIN (0 - JSON_INT_MAX)
433
434/* Emits a stats field value and its type in JSON.
435 * Returns non-zero on success, 0 on error.
436 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200437int stats_emit_json_data_field(struct buffer *out, const struct field *f)
Simon Horman05ee2132017-01-04 09:37:25 +0100438{
439 int old_len;
440 char buf[20];
441 const char *type, *value = buf, *quote = "";
442
443 switch (field_format(f, 0)) {
444 case FF_EMPTY: return 1;
445 case FF_S32: type = "\"s32\"";
446 snprintf(buf, sizeof(buf), "%d", f->u.s32);
447 break;
448 case FF_U32: type = "\"u32\"";
449 snprintf(buf, sizeof(buf), "%u", f->u.u32);
450 break;
451 case FF_S64: type = "\"s64\"";
452 if (f->u.s64 < JSON_INT_MIN || f->u.s64 > JSON_INT_MAX)
453 return 0;
454 type = "\"s64\"";
455 snprintf(buf, sizeof(buf), "%lld", (long long)f->u.s64);
456 break;
457 case FF_U64: if (f->u.u64 > JSON_INT_MAX)
458 return 0;
459 type = "\"u64\"";
460 snprintf(buf, sizeof(buf), "%llu",
461 (unsigned long long) f->u.u64);
Christopher Faulet52c91bb2019-09-28 10:37:31 +0200462 break;
Christopher Faulet88a0db22019-09-24 16:35:10 +0200463 case FF_FLT: type = "\"flt\"";
Willy Tarreau065ba312021-05-08 10:38:20 +0200464 flt_trim(buf, 0, snprintf(buf, sizeof(buf), "%f", f->u.flt));
Simon Horman05ee2132017-01-04 09:37:25 +0100465 break;
466 case FF_STR: type = "\"str\"";
467 value = field_str(f, 0);
468 quote = "\"";
469 break;
470 default: snprintf(buf, sizeof(buf), "%u", f->type);
471 type = buf;
472 value = "unknown";
473 quote = "\"";
474 break;
475 }
476
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200477 old_len = out->data;
Simon Horman05ee2132017-01-04 09:37:25 +0100478 chunk_appendf(out, ",\"value\":{\"type\":%s,\"value\":%s%s%s}",
479 type, quote, value, quote);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200480 return !(old_len == out->data);
Simon Horman05ee2132017-01-04 09:37:25 +0100481}
482
William Lallemand74c24fb2016-11-21 17:18:36 +0100483/* Emits an encoding of the field type on 3 characters followed by a delimiter.
484 * Returns non-zero on success, 0 if the buffer is full.
485 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200486int stats_emit_field_tags(struct buffer *out, const struct field *f,
487 char delim)
William Lallemand74c24fb2016-11-21 17:18:36 +0100488{
489 char origin, nature, scope;
490
491 switch (field_origin(f, 0)) {
492 case FO_METRIC: origin = 'M'; break;
493 case FO_STATUS: origin = 'S'; break;
494 case FO_KEY: origin = 'K'; break;
495 case FO_CONFIG: origin = 'C'; break;
496 case FO_PRODUCT: origin = 'P'; break;
497 default: origin = '?'; break;
498 }
499
500 switch (field_nature(f, 0)) {
501 case FN_GAUGE: nature = 'G'; break;
502 case FN_LIMIT: nature = 'L'; break;
503 case FN_MIN: nature = 'm'; break;
504 case FN_MAX: nature = 'M'; break;
505 case FN_RATE: nature = 'R'; break;
506 case FN_COUNTER: nature = 'C'; break;
507 case FN_DURATION: nature = 'D'; break;
508 case FN_AGE: nature = 'A'; break;
509 case FN_TIME: nature = 'T'; break;
510 case FN_NAME: nature = 'N'; break;
511 case FN_OUTPUT: nature = 'O'; break;
512 case FN_AVG: nature = 'a'; break;
513 default: nature = '?'; break;
514 }
515
516 switch (field_scope(f, 0)) {
517 case FS_PROCESS: scope = 'P'; break;
518 case FS_SERVICE: scope = 'S'; break;
519 case FS_SYSTEM: scope = 's'; break;
520 case FS_CLUSTER: scope = 'C'; break;
521 default: scope = '?'; break;
522 }
523
524 return chunk_appendf(out, "%c%c%c%c", origin, nature, scope, delim);
525}
526
Simon Horman05ee2132017-01-04 09:37:25 +0100527/* Emits an encoding of the field type as JSON.
528 * Returns non-zero on success, 0 if the buffer is full.
529 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200530int stats_emit_json_field_tags(struct buffer *out, const struct field *f)
Simon Horman05ee2132017-01-04 09:37:25 +0100531{
532 const char *origin, *nature, *scope;
533 int old_len;
534
535 switch (field_origin(f, 0)) {
536 case FO_METRIC: origin = "Metric"; break;
537 case FO_STATUS: origin = "Status"; break;
538 case FO_KEY: origin = "Key"; break;
539 case FO_CONFIG: origin = "Config"; break;
540 case FO_PRODUCT: origin = "Product"; break;
541 default: origin = "Unknown"; break;
542 }
543
544 switch (field_nature(f, 0)) {
545 case FN_GAUGE: nature = "Gauge"; break;
546 case FN_LIMIT: nature = "Limit"; break;
547 case FN_MIN: nature = "Min"; break;
548 case FN_MAX: nature = "Max"; break;
549 case FN_RATE: nature = "Rate"; break;
550 case FN_COUNTER: nature = "Counter"; break;
551 case FN_DURATION: nature = "Duration"; break;
552 case FN_AGE: nature = "Age"; break;
553 case FN_TIME: nature = "Time"; break;
554 case FN_NAME: nature = "Name"; break;
555 case FN_OUTPUT: nature = "Output"; break;
556 case FN_AVG: nature = "Avg"; break;
557 default: nature = "Unknown"; break;
558 }
559
560 switch (field_scope(f, 0)) {
561 case FS_PROCESS: scope = "Process"; break;
562 case FS_SERVICE: scope = "Service"; break;
563 case FS_SYSTEM: scope = "System"; break;
564 case FS_CLUSTER: scope = "Cluster"; break;
565 default: scope = "Unknown"; break;
566 }
567
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200568 old_len = out->data;
Simon Horman05ee2132017-01-04 09:37:25 +0100569 chunk_appendf(out, "\"tags\":{"
570 "\"origin\":\"%s\","
571 "\"nature\":\"%s\","
572 "\"scope\":\"%s\""
573 "}", origin, nature, scope);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200574 return !(old_len == out->data);
Simon Horman05ee2132017-01-04 09:37:25 +0100575}
William Lallemand74c24fb2016-11-21 17:18:36 +0100576
577/* Dump all fields from <stats> into <out> using CSV format */
Willy Tarreau83061a82018-07-13 11:56:34 +0200578static int stats_dump_fields_csv(struct buffer *out,
Amaury Denoyelle97323c92020-10-02 18:32:01 +0200579 const struct field *stats, size_t stats_count,
Amaury Denoyelle50660a82020-10-05 11:49:39 +0200580 unsigned int flags,
581 enum stats_domain domain)
William Lallemand74c24fb2016-11-21 17:18:36 +0100582{
583 int field;
584
Amaury Denoyelle50660a82020-10-05 11:49:39 +0200585 for (field = 0; field < stats_count; ++field) {
William Lallemand74c24fb2016-11-21 17:18:36 +0100586 if (!stats_emit_raw_data_field(out, &stats[field]))
587 return 0;
588 if (!chunk_strcat(out, ","))
589 return 0;
Amaury Denoyelle50660a82020-10-05 11:49:39 +0200590
591 /* print special delimiter on proxy stats to mark end of
592 static fields */
593 if (domain == STATS_DOMAIN_PROXY && field + 1 == ST_F_TOTAL_FIELDS) {
594 if (!chunk_strcat(out, "-,"))
595 return 0;
596 }
William Lallemand74c24fb2016-11-21 17:18:36 +0100597 }
Amaury Denoyelle50660a82020-10-05 11:49:39 +0200598
William Lallemand74c24fb2016-11-21 17:18:36 +0100599 chunk_strcat(out, "\n");
600 return 1;
601}
602
603/* Dump all fields from <stats> into <out> using a typed "field:desc:type:value" format */
Willy Tarreau83061a82018-07-13 11:56:34 +0200604static int stats_dump_fields_typed(struct buffer *out,
Amaury Denoyelle97323c92020-10-02 18:32:01 +0200605 const struct field *stats,
606 size_t stats_count,
Amaury Denoyelle072f97e2020-10-05 11:49:37 +0200607 unsigned int flags,
608 enum stats_domain domain)
William Lallemand74c24fb2016-11-21 17:18:36 +0100609{
610 int field;
611
Amaury Denoyelle97323c92020-10-02 18:32:01 +0200612 for (field = 0; field < stats_count; ++field) {
William Lallemand74c24fb2016-11-21 17:18:36 +0100613 if (!stats[field].type)
614 continue;
615
Amaury Denoyelle072f97e2020-10-05 11:49:37 +0200616 switch (domain) {
617 case STATS_DOMAIN_PROXY:
618 chunk_appendf(out, "%c.%u.%u.%d.%s.%u:",
619 stats[ST_F_TYPE].u.u32 == STATS_TYPE_FE ? 'F' :
620 stats[ST_F_TYPE].u.u32 == STATS_TYPE_BE ? 'B' :
621 stats[ST_F_TYPE].u.u32 == STATS_TYPE_SO ? 'L' :
622 stats[ST_F_TYPE].u.u32 == STATS_TYPE_SV ? 'S' :
623 '?',
624 stats[ST_F_IID].u.u32, stats[ST_F_SID].u.u32,
625 field,
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +0200626 stat_f[domain][field].name,
Amaury Denoyelle072f97e2020-10-05 11:49:37 +0200627 stats[ST_F_PID].u.u32);
628 break;
629
Emeric Brunf8642ee2021-10-29 17:59:18 +0200630 case STATS_DOMAIN_RESOLVERS:
631 chunk_appendf(out, "N.%d.%s:", field,
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +0200632 stat_f[domain][field].name);
633 break;
634
Amaury Denoyelle072f97e2020-10-05 11:49:37 +0200635 default:
636 break;
637 }
William Lallemand74c24fb2016-11-21 17:18:36 +0100638
639 if (!stats_emit_field_tags(out, &stats[field], ':'))
640 return 0;
641 if (!stats_emit_typed_data_field(out, &stats[field]))
642 return 0;
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +0200643
Willy Tarreaufc8e4382021-06-17 07:22:27 +0200644 if (flags & STAT_SHOW_FDESC &&
645 !chunk_appendf(out, ":\"%s\"", stat_f[domain][field].desc)) {
Willy Tarreau6b19b142019-10-09 15:44:21 +0200646 return 0;
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +0200647 }
648
William Lallemand74c24fb2016-11-21 17:18:36 +0100649 if (!chunk_strcat(out, "\n"))
650 return 0;
651 }
652 return 1;
653}
654
Simon Horman05ee2132017-01-04 09:37:25 +0100655/* Dump all fields from <stats> into <out> using the "show info json" format */
Willy Tarreau83061a82018-07-13 11:56:34 +0200656static int stats_dump_json_info_fields(struct buffer *out,
Willy Tarreau43241ff2019-10-09 11:27:51 +0200657 const struct field *info, unsigned int flags)
Simon Horman05ee2132017-01-04 09:37:25 +0100658{
659 int field;
660 int started = 0;
661
662 if (!chunk_strcat(out, "["))
663 return 0;
664
665 for (field = 0; field < INF_TOTAL_FIELDS; field++) {
666 int old_len;
667
668 if (!field_format(info, field))
669 continue;
670
671 if (started && !chunk_strcat(out, ","))
672 goto err;
673 started = 1;
674
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200675 old_len = out->data;
Simon Horman05ee2132017-01-04 09:37:25 +0100676 chunk_appendf(out,
677 "{\"field\":{\"pos\":%d,\"name\":\"%s\"},"
678 "\"processNum\":%u,",
Willy Tarreaueaa55372019-10-09 07:39:11 +0200679 field, info_fields[field].name,
Simon Horman05ee2132017-01-04 09:37:25 +0100680 info[INF_PROCESS_NUM].u.u32);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200681 if (old_len == out->data)
Simon Horman05ee2132017-01-04 09:37:25 +0100682 goto err;
683
684 if (!stats_emit_json_field_tags(out, &info[field]))
685 goto err;
686
687 if (!stats_emit_json_data_field(out, &info[field]))
688 goto err;
689
690 if (!chunk_strcat(out, "}"))
691 goto err;
692 }
693
694 if (!chunk_strcat(out, "]"))
695 goto err;
696 return 1;
697
698err:
699 chunk_reset(out);
700 chunk_appendf(out, "{\"errorStr\":\"output buffer too short\"}");
701 return 0;
702}
703
Amaury Denoyellef34017b2020-10-02 18:32:03 +0200704static void stats_print_proxy_field_json(struct buffer *out,
705 const struct field *stat,
706 const char *name,
707 int pos,
708 uint32_t field_type,
709 uint32_t iid,
710 uint32_t sid,
711 uint32_t pid)
712{
713 const char *obj_type;
714 switch (field_type) {
715 case STATS_TYPE_FE: obj_type = "Frontend"; break;
716 case STATS_TYPE_BE: obj_type = "Backend"; break;
717 case STATS_TYPE_SO: obj_type = "Listener"; break;
718 case STATS_TYPE_SV: obj_type = "Server"; break;
719 default: obj_type = "Unknown"; break;
720 }
721
722 chunk_appendf(out,
723 "{"
724 "\"objType\":\"%s\","
725 "\"proxyId\":%u,"
726 "\"id\":%u,"
727 "\"field\":{\"pos\":%d,\"name\":\"%s\"},"
728 "\"processNum\":%u,",
729 obj_type, iid, sid, pos, name, pid);
730}
731
Emeric Brunf8642ee2021-10-29 17:59:18 +0200732static void stats_print_rslv_field_json(struct buffer *out,
733 const struct field *stat,
734 const char *name,
735 int pos)
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +0200736{
737 chunk_appendf(out,
738 "{"
739 "\"field\":{\"pos\":%d,\"name\":\"%s\"},",
740 pos, name);
741}
742
743
Simon Horman05ee2132017-01-04 09:37:25 +0100744/* Dump all fields from <stats> into <out> using a typed "field:desc:type:value" format */
Willy Tarreau83061a82018-07-13 11:56:34 +0200745static int stats_dump_fields_json(struct buffer *out,
Amaury Denoyelle97323c92020-10-02 18:32:01 +0200746 const struct field *stats, size_t stats_count,
Amaury Denoyelle072f97e2020-10-05 11:49:37 +0200747 unsigned int flags,
748 enum stats_domain domain)
Simon Horman05ee2132017-01-04 09:37:25 +0100749{
750 int field;
751 int started = 0;
752
Willy Tarreaub0ce3ad2019-10-09 11:19:29 +0200753 if ((flags & STAT_STARTED) && !chunk_strcat(out, ","))
Simon Horman05ee2132017-01-04 09:37:25 +0100754 return 0;
755 if (!chunk_strcat(out, "["))
756 return 0;
757
Amaury Denoyelle97323c92020-10-02 18:32:01 +0200758 for (field = 0; field < stats_count; field++) {
Simon Horman05ee2132017-01-04 09:37:25 +0100759 int old_len;
760
761 if (!stats[field].type)
762 continue;
763
764 if (started && !chunk_strcat(out, ","))
765 goto err;
766 started = 1;
767
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200768 old_len = out->data;
Amaury Denoyelle072f97e2020-10-05 11:49:37 +0200769 if (domain == STATS_DOMAIN_PROXY) {
770 stats_print_proxy_field_json(out, &stats[field],
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +0200771 stat_f[domain][field].name,
772 field,
Amaury Denoyelle072f97e2020-10-05 11:49:37 +0200773 stats[ST_F_TYPE].u.u32,
774 stats[ST_F_IID].u.u32,
775 stats[ST_F_SID].u.u32,
776 stats[ST_F_PID].u.u32);
Emeric Brunf8642ee2021-10-29 17:59:18 +0200777 } else if (domain == STATS_DOMAIN_RESOLVERS) {
778 stats_print_rslv_field_json(out, &stats[field],
779 stat_f[domain][field].name,
780 field);
Amaury Denoyelle072f97e2020-10-05 11:49:37 +0200781 }
Amaury Denoyellef34017b2020-10-02 18:32:03 +0200782
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200783 if (old_len == out->data)
Simon Horman05ee2132017-01-04 09:37:25 +0100784 goto err;
785
786 if (!stats_emit_json_field_tags(out, &stats[field]))
787 goto err;
788
789 if (!stats_emit_json_data_field(out, &stats[field]))
790 goto err;
791
792 if (!chunk_strcat(out, "}"))
793 goto err;
794 }
795
796 if (!chunk_strcat(out, "]"))
797 goto err;
798
799 return 1;
800
801err:
802 chunk_reset(out);
Willy Tarreaub0ce3ad2019-10-09 11:19:29 +0200803 if (flags & STAT_STARTED)
804 chunk_strcat(out, ",");
Simon Horman05ee2132017-01-04 09:37:25 +0100805 chunk_appendf(out, "{\"errorStr\":\"output buffer too short\"}");
806 return 0;
807}
808
William Lallemand74c24fb2016-11-21 17:18:36 +0100809/* Dump all fields from <stats> into <out> using the HTML format. A column is
Willy Tarreauab02b3f2019-10-09 11:11:46 +0200810 * reserved for the checkbox is STAT_ADMIN is set in <flags>. Some extra info
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +0200811 * are provided if STAT_SHLGNDS is present in <flags>. The statistics from
812 * extra modules are displayed at the end of the lines if STAT_SHMODULES is
813 * present in <flags>.
William Lallemand74c24fb2016-11-21 17:18:36 +0100814 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200815static int stats_dump_fields_html(struct buffer *out,
816 const struct field *stats,
817 unsigned int flags)
William Lallemand74c24fb2016-11-21 17:18:36 +0100818{
Willy Tarreau83061a82018-07-13 11:56:34 +0200819 struct buffer src;
Amaury Denoyellee3f576c2020-10-05 11:49:44 +0200820 struct stats_module *mod;
821 int i = 0, j = 0;
William Lallemand74c24fb2016-11-21 17:18:36 +0100822
823 if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_FE) {
824 chunk_appendf(out,
825 /* name, queue */
826 "<tr class=\"frontend\">");
827
Willy Tarreauab02b3f2019-10-09 11:11:46 +0200828 if (flags & STAT_ADMIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +0100829 /* Column sub-heading for Enable or Disable server */
830 chunk_appendf(out, "<td></td>");
831 }
832
833 chunk_appendf(out,
834 "<td class=ac>"
835 "<a name=\"%s/Frontend\"></a>"
836 "<a class=lfsb href=\"#%s/Frontend\">Frontend</a></td>"
837 "<td colspan=3></td>"
838 "",
839 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_PXNAME));
840
841 chunk_appendf(out,
842 /* sessions rate : current */
843 "<td><u>%s<div class=tips><table class=det>"
844 "<tr><th>Current connection rate:</th><td>%s/s</td></tr>"
845 "<tr><th>Current session rate:</th><td>%s/s</td></tr>"
846 "",
847 U2H(stats[ST_F_RATE].u.u32),
848 U2H(stats[ST_F_CONN_RATE].u.u32),
849 U2H(stats[ST_F_RATE].u.u32));
850
851 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
852 chunk_appendf(out,
853 "<tr><th>Current request rate:</th><td>%s/s</td></tr>",
854 U2H(stats[ST_F_REQ_RATE].u.u32));
855
856 chunk_appendf(out,
857 "</table></div></u></td>"
858 /* sessions rate : max */
859 "<td><u>%s<div class=tips><table class=det>"
860 "<tr><th>Max connection rate:</th><td>%s/s</td></tr>"
861 "<tr><th>Max session rate:</th><td>%s/s</td></tr>"
862 "",
863 U2H(stats[ST_F_RATE_MAX].u.u32),
864 U2H(stats[ST_F_CONN_RATE_MAX].u.u32),
865 U2H(stats[ST_F_RATE_MAX].u.u32));
866
867 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
868 chunk_appendf(out,
869 "<tr><th>Max request rate:</th><td>%s/s</td></tr>",
870 U2H(stats[ST_F_REQ_RATE_MAX].u.u32));
871
872 chunk_appendf(out,
873 "</table></div></u></td>"
874 /* sessions rate : limit */
875 "<td>%s</td>",
876 LIM2A(stats[ST_F_RATE_LIM].u.u32, "-"));
877
878 chunk_appendf(out,
879 /* sessions: current, max, limit, total */
880 "<td>%s</td><td>%s</td><td>%s</td>"
881 "<td><u>%s<div class=tips><table class=det>"
882 "<tr><th>Cum. connections:</th><td>%s</td></tr>"
883 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
884 "",
885 U2H(stats[ST_F_SCUR].u.u32), U2H(stats[ST_F_SMAX].u.u32), U2H(stats[ST_F_SLIM].u.u32),
886 U2H(stats[ST_F_STOT].u.u64),
887 U2H(stats[ST_F_CONN_TOT].u.u64),
888 U2H(stats[ST_F_STOT].u.u64));
889
890 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
891 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0) {
892 chunk_appendf(out,
893 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
894 "<tr><th>- HTTP 1xx responses:</th><td>%s</td></tr>"
895 "<tr><th>- HTTP 2xx responses:</th><td>%s</td></tr>"
896 "<tr><th>&nbsp;&nbsp;Compressed 2xx:</th><td>%s</td><td>(%d%%)</td></tr>"
897 "<tr><th>- HTTP 3xx responses:</th><td>%s</td></tr>"
898 "<tr><th>- HTTP 4xx responses:</th><td>%s</td></tr>"
899 "<tr><th>- HTTP 5xx responses:</th><td>%s</td></tr>"
900 "<tr><th>- other responses:</th><td>%s</td></tr>"
901 "<tr><th>Intercepted requests:</th><td>%s</td></tr>"
Willy Tarreaua1214a52018-12-14 14:00:25 +0100902 "<tr><th>Cache lookups:</th><td>%s</td></tr>"
903 "<tr><th>Cache hits:</th><td>%s</td><td>(%d%%)</td></tr>"
Willy Tarreau1b0f85e2018-05-28 15:12:40 +0200904 "<tr><th>Failed hdr rewrites:</th><td>%s</td></tr>"
Christopher Faulet0159ee42019-12-16 14:40:39 +0100905 "<tr><th>Internal errors:</th><td>%s</td></tr>"
William Lallemand74c24fb2016-11-21 17:18:36 +0100906 "",
907 U2H(stats[ST_F_REQ_TOT].u.u64),
908 U2H(stats[ST_F_HRSP_1XX].u.u64),
909 U2H(stats[ST_F_HRSP_2XX].u.u64),
910 U2H(stats[ST_F_COMP_RSP].u.u64),
911 stats[ST_F_HRSP_2XX].u.u64 ?
912 (int)(100 * stats[ST_F_COMP_RSP].u.u64 / stats[ST_F_HRSP_2XX].u.u64) : 0,
913 U2H(stats[ST_F_HRSP_3XX].u.u64),
914 U2H(stats[ST_F_HRSP_4XX].u.u64),
915 U2H(stats[ST_F_HRSP_5XX].u.u64),
916 U2H(stats[ST_F_HRSP_OTHER].u.u64),
Willy Tarreau1b0f85e2018-05-28 15:12:40 +0200917 U2H(stats[ST_F_INTERCEPTED].u.u64),
Willy Tarreaua1214a52018-12-14 14:00:25 +0100918 U2H(stats[ST_F_CACHE_LOOKUPS].u.u64),
919 U2H(stats[ST_F_CACHE_HITS].u.u64),
920 stats[ST_F_CACHE_LOOKUPS].u.u64 ?
921 (int)(100 * stats[ST_F_CACHE_HITS].u.u64 / stats[ST_F_CACHE_LOOKUPS].u.u64) : 0,
Christopher Faulet0159ee42019-12-16 14:40:39 +0100922 U2H(stats[ST_F_WREW].u.u64),
923 U2H(stats[ST_F_EINT].u.u64));
William Lallemand74c24fb2016-11-21 17:18:36 +0100924 }
925
926 chunk_appendf(out,
927 "</table></div></u></td>"
928 /* sessions: lbtot, lastsess */
929 "<td></td><td></td>"
930 /* bytes : in */
931 "<td>%s</td>"
932 "",
933 U2H(stats[ST_F_BIN].u.u64));
934
935 chunk_appendf(out,
936 /* bytes:out + compression stats (via hover): comp_in, comp_out, comp_byp */
937 "<td>%s%s<div class=tips><table class=det>"
938 "<tr><th>Response bytes in:</th><td>%s</td></tr>"
939 "<tr><th>Compression in:</th><td>%s</td></tr>"
940 "<tr><th>Compression out:</th><td>%s</td><td>(%d%%)</td></tr>"
941 "<tr><th>Compression bypass:</th><td>%s</td></tr>"
942 "<tr><th>Total bytes saved:</th><td>%s</td><td>(%d%%)</td></tr>"
943 "</table></div>%s</td>",
944 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "<u>":"",
945 U2H(stats[ST_F_BOUT].u.u64),
946 U2H(stats[ST_F_BOUT].u.u64),
947 U2H(stats[ST_F_COMP_IN].u.u64),
948 U2H(stats[ST_F_COMP_OUT].u.u64),
949 stats[ST_F_COMP_IN].u.u64 ? (int)(stats[ST_F_COMP_OUT].u.u64 * 100 / stats[ST_F_COMP_IN].u.u64) : 0,
950 U2H(stats[ST_F_COMP_BYP].u.u64),
951 U2H(stats[ST_F_COMP_IN].u.u64 - stats[ST_F_COMP_OUT].u.u64),
952 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,
953 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "</u>":"");
954
955 chunk_appendf(out,
956 /* denied: req, resp */
957 "<td>%s</td><td>%s</td>"
958 /* errors : request, connect, response */
959 "<td>%s</td><td></td><td></td>"
960 /* warnings: retries, redispatches */
961 "<td></td><td></td>"
962 /* server status : reflect frontend status */
963 "<td class=ac>%s</td>"
964 /* rest of server: nothing */
Amaury Denoyellee3f576c2020-10-05 11:49:44 +0200965 "<td class=ac colspan=8></td>"
William Lallemand74c24fb2016-11-21 17:18:36 +0100966 "",
967 U2H(stats[ST_F_DREQ].u.u64), U2H(stats[ST_F_DRESP].u.u64),
968 U2H(stats[ST_F_EREQ].u.u64),
969 field_str(stats, ST_F_STATUS));
Amaury Denoyellee3f576c2020-10-05 11:49:44 +0200970
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +0200971 if (flags & STAT_SHMODULES) {
972 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
Amaury Denoyellee6ee8202020-11-03 15:04:46 +0100973 chunk_appendf(out, "<td>");
974
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +0200975 if (stats_px_get_cap(mod->domain_flags) & STATS_PX_CAP_FE) {
Amaury Denoyellee6ee8202020-11-03 15:04:46 +0100976 chunk_appendf(out,
977 "<u>%s<div class=tips><table class=det>",
978 mod->name);
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +0200979 for (j = 0; j < mod->stats_count; ++j) {
980 chunk_appendf(out,
981 "<tr><th>%s</th><td>%s</td></tr>",
982 mod->stats[j].desc, field_to_html_str(&stats[ST_F_TOTAL_FIELDS + i]));
983 ++i;
984 }
Amaury Denoyellee6ee8202020-11-03 15:04:46 +0100985 chunk_appendf(out, "</table></div></u>");
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +0200986 } else {
987 i += mod->stats_count;
Amaury Denoyellee3f576c2020-10-05 11:49:44 +0200988 }
Amaury Denoyellee3f576c2020-10-05 11:49:44 +0200989
Amaury Denoyellee6ee8202020-11-03 15:04:46 +0100990 chunk_appendf(out, "</td>");
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +0200991 }
Amaury Denoyellee3f576c2020-10-05 11:49:44 +0200992 }
993
994 chunk_appendf(out, "</tr>");
William Lallemand74c24fb2016-11-21 17:18:36 +0100995 }
996 else if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_SO) {
997 chunk_appendf(out, "<tr class=socket>");
Willy Tarreauab02b3f2019-10-09 11:11:46 +0200998 if (flags & STAT_ADMIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +0100999 /* Column sub-heading for Enable or Disable server */
1000 chunk_appendf(out, "<td></td>");
1001 }
1002
1003 chunk_appendf(out,
1004 /* frontend name, listener name */
1005 "<td class=ac><a name=\"%s/+%s\"></a>%s"
1006 "<a class=lfsb href=\"#%s/+%s\">%s</a>"
1007 "",
1008 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME),
Willy Tarreau708c4162019-10-09 10:19:16 +02001009 (flags & STAT_SHLGNDS)?"<u>":"",
William Lallemand74c24fb2016-11-21 17:18:36 +01001010 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME), field_str(stats, ST_F_SVNAME));
1011
Willy Tarreau708c4162019-10-09 10:19:16 +02001012 if (flags & STAT_SHLGNDS) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001013 chunk_appendf(out, "<div class=tips>");
1014
Willy Tarreau90807112020-02-25 08:16:33 +01001015 if (isdigit((unsigned char)*field_str(stats, ST_F_ADDR)))
William Lallemand74c24fb2016-11-21 17:18:36 +01001016 chunk_appendf(out, "IPv4: %s, ", field_str(stats, ST_F_ADDR));
1017 else if (*field_str(stats, ST_F_ADDR) == '[')
1018 chunk_appendf(out, "IPv6: %s, ", field_str(stats, ST_F_ADDR));
1019 else if (*field_str(stats, ST_F_ADDR))
1020 chunk_appendf(out, "%s, ", field_str(stats, ST_F_ADDR));
1021
1022 /* id */
1023 chunk_appendf(out, "id: %d</div>", stats[ST_F_SID].u.u32);
1024 }
1025
1026 chunk_appendf(out,
1027 /* queue */
1028 "%s</td><td colspan=3></td>"
1029 /* sessions rate: current, max, limit */
1030 "<td colspan=3>&nbsp;</td>"
1031 /* sessions: current, max, limit, total, lbtot, lastsess */
1032 "<td>%s</td><td>%s</td><td>%s</td>"
1033 "<td>%s</td><td>&nbsp;</td><td>&nbsp;</td>"
1034 /* bytes: in, out */
1035 "<td>%s</td><td>%s</td>"
1036 "",
Willy Tarreau708c4162019-10-09 10:19:16 +02001037 (flags & STAT_SHLGNDS)?"</u>":"",
William Lallemand74c24fb2016-11-21 17:18:36 +01001038 U2H(stats[ST_F_SCUR].u.u32), U2H(stats[ST_F_SMAX].u.u32), U2H(stats[ST_F_SLIM].u.u32),
1039 U2H(stats[ST_F_STOT].u.u64), U2H(stats[ST_F_BIN].u.u64), U2H(stats[ST_F_BOUT].u.u64));
1040
1041 chunk_appendf(out,
1042 /* denied: req, resp */
1043 "<td>%s</td><td>%s</td>"
1044 /* errors: request, connect, response */
1045 "<td>%s</td><td></td><td></td>"
1046 /* warnings: retries, redispatches */
1047 "<td></td><td></td>"
1048 /* server status: reflect listener status */
1049 "<td class=ac>%s</td>"
1050 /* rest of server: nothing */
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001051 "<td class=ac colspan=8></td>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001052 "",
1053 U2H(stats[ST_F_DREQ].u.u64), U2H(stats[ST_F_DRESP].u.u64),
1054 U2H(stats[ST_F_EREQ].u.u64),
1055 field_str(stats, ST_F_STATUS));
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001056
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001057 if (flags & STAT_SHMODULES) {
1058 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001059 chunk_appendf(out, "<td>");
1060
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001061 if (stats_px_get_cap(mod->domain_flags) & STATS_PX_CAP_LI) {
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001062 chunk_appendf(out,
1063 "<u>%s<div class=tips><table class=det>",
1064 mod->name);
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001065 for (j = 0; j < mod->stats_count; ++j) {
1066 chunk_appendf(out,
1067 "<tr><th>%s</th><td>%s</td></tr>",
1068 mod->stats[j].desc, field_to_html_str(&stats[ST_F_TOTAL_FIELDS + i]));
1069 ++i;
1070 }
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001071 chunk_appendf(out, "</table></div></u>");
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001072 } else {
1073 i += mod->stats_count;
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001074 }
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001075
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001076 chunk_appendf(out, "</td>");
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001077 }
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001078 }
1079
1080 chunk_appendf(out, "</tr>");
William Lallemand74c24fb2016-11-21 17:18:36 +01001081 }
1082 else if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_SV) {
1083 const char *style;
1084
1085 /* determine the style to use depending on the server's state,
1086 * its health and weight. There isn't a 1-to-1 mapping between
1087 * state and styles for the cases where the server is (still)
1088 * up. The reason is that we don't want to report nolb and
1089 * drain with the same color.
1090 */
1091
1092 if (strcmp(field_str(stats, ST_F_STATUS), "DOWN") == 0 ||
1093 strcmp(field_str(stats, ST_F_STATUS), "DOWN (agent)") == 0) {
1094 style = "down";
1095 }
Florian Apolloner39272c22021-03-30 13:28:35 +02001096 else if (strncmp(field_str(stats, ST_F_STATUS), "DOWN ", strlen("DOWN ")) == 0) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001097 style = "going_up";
1098 }
Daniel Corbettb4285172020-03-28 12:35:50 -04001099 else if (strcmp(field_str(stats, ST_F_STATUS), "DRAIN") == 0) {
1100 style = "draining";
1101 }
Florian Apolloner39272c22021-03-30 13:28:35 +02001102 else if (strncmp(field_str(stats, ST_F_STATUS), "NOLB ", strlen("NOLB ")) == 0) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001103 style = "going_down";
1104 }
1105 else if (strcmp(field_str(stats, ST_F_STATUS), "NOLB") == 0) {
1106 style = "nolb";
1107 }
1108 else if (strcmp(field_str(stats, ST_F_STATUS), "no check") == 0) {
1109 style = "no_check";
1110 }
1111 else if (!stats[ST_F_CHKFAIL].type ||
1112 stats[ST_F_CHECK_HEALTH].u.u32 == stats[ST_F_CHECK_RISE].u.u32 + stats[ST_F_CHECK_FALL].u.u32 - 1) {
1113 /* no check or max health = UP */
1114 if (stats[ST_F_WEIGHT].u.u32)
1115 style = "up";
1116 else
1117 style = "draining";
1118 }
1119 else {
1120 style = "going_down";
1121 }
1122
Willy Tarreau7b524852020-08-11 10:26:36 +02001123 if (strncmp(field_str(stats, ST_F_STATUS), "MAINT", 5) == 0)
William Lallemand74c24fb2016-11-21 17:18:36 +01001124 chunk_appendf(out, "<tr class=\"maintain\">");
1125 else
1126 chunk_appendf(out,
1127 "<tr class=\"%s_%s\">",
1128 (stats[ST_F_BCK].u.u32) ? "backup" : "active", style);
1129
1130
Willy Tarreauab02b3f2019-10-09 11:11:46 +02001131 if (flags & STAT_ADMIN)
William Lallemand74c24fb2016-11-21 17:18:36 +01001132 chunk_appendf(out,
David Harrigand3db35a2016-12-30 12:12:49 +00001133 "<td><input class='%s-checkbox' type=\"checkbox\" name=\"s\" value=\"%s\"></td>",
1134 field_str(stats, ST_F_PXNAME),
William Lallemand74c24fb2016-11-21 17:18:36 +01001135 field_str(stats, ST_F_SVNAME));
1136
1137 chunk_appendf(out,
1138 "<td class=ac><a name=\"%s/%s\"></a>%s"
1139 "<a class=lfsb href=\"#%s/%s\">%s</a>"
1140 "",
1141 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME),
Willy Tarreau708c4162019-10-09 10:19:16 +02001142 (flags & STAT_SHLGNDS) ? "<u>" : "",
William Lallemand74c24fb2016-11-21 17:18:36 +01001143 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME), field_str(stats, ST_F_SVNAME));
1144
Willy Tarreau708c4162019-10-09 10:19:16 +02001145 if (flags & STAT_SHLGNDS) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001146 chunk_appendf(out, "<div class=tips>");
1147
Willy Tarreau90807112020-02-25 08:16:33 +01001148 if (isdigit((unsigned char)*field_str(stats, ST_F_ADDR)))
William Lallemand74c24fb2016-11-21 17:18:36 +01001149 chunk_appendf(out, "IPv4: %s, ", field_str(stats, ST_F_ADDR));
1150 else if (*field_str(stats, ST_F_ADDR) == '[')
1151 chunk_appendf(out, "IPv6: %s, ", field_str(stats, ST_F_ADDR));
1152 else if (*field_str(stats, ST_F_ADDR))
1153 chunk_appendf(out, "%s, ", field_str(stats, ST_F_ADDR));
1154
1155 /* id */
1156 chunk_appendf(out, "id: %d", stats[ST_F_SID].u.u32);
1157
1158 /* cookie */
1159 if (stats[ST_F_COOKIE].type) {
1160 chunk_appendf(out, ", cookie: '");
1161 chunk_initstr(&src, field_str(stats, ST_F_COOKIE));
1162 chunk_htmlencode(out, &src);
1163 chunk_appendf(out, "'");
1164 }
1165
1166 chunk_appendf(out, "</div>");
1167 }
1168
1169 chunk_appendf(out,
1170 /* queue : current, max, limit */
1171 "%s</td><td>%s</td><td>%s</td><td>%s</td>"
1172 /* sessions rate : current, max, limit */
1173 "<td>%s</td><td>%s</td><td></td>"
1174 "",
Willy Tarreau708c4162019-10-09 10:19:16 +02001175 (flags & STAT_SHLGNDS) ? "</u>" : "",
William Lallemand74c24fb2016-11-21 17:18:36 +01001176 U2H(stats[ST_F_QCUR].u.u32), U2H(stats[ST_F_QMAX].u.u32), LIM2A(stats[ST_F_QLIMIT].u.u32, "-"),
1177 U2H(stats[ST_F_RATE].u.u32), U2H(stats[ST_F_RATE_MAX].u.u32));
1178
1179 chunk_appendf(out,
1180 /* sessions: current, max, limit, total */
Willy Tarreauf21d17b2019-09-08 09:24:56 +02001181 "<td><u>%s<div class=tips>"
1182 "<table class=det>"
1183 "<tr><th>Current active connections:</th><td>%s</td></tr>"
Willy Tarreau3bb617c2020-06-29 13:51:05 +02001184 "<tr><th>Current used connections:</th><td>%s</td></tr>"
Willy Tarreauf21d17b2019-09-08 09:24:56 +02001185 "<tr><th>Current idle connections:</th><td>%s</td></tr>"
Willy Tarreau3bb617c2020-06-29 13:51:05 +02001186 "<tr><th>- unsafe:</th><td>%s</td></tr>"
1187 "<tr><th>- safe:</th><td>%s</td></tr>"
Willy Tarreaua9fcecb2020-06-29 15:38:53 +02001188 "<tr><th>Estimated need of connections:</th><td>%s</td></tr>"
Willy Tarreauf21d17b2019-09-08 09:24:56 +02001189 "<tr><th>Active connections limit:</th><td>%s</td></tr>"
1190 "<tr><th>Idle connections limit:</th><td>%s</td></tr>"
1191 "</table></div></u>"
1192 "</td><td>%s</td><td>%s</td>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001193 "<td><u>%s<div class=tips><table class=det>"
1194 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
1195 "",
Willy Tarreauf21d17b2019-09-08 09:24:56 +02001196 U2H(stats[ST_F_SCUR].u.u32),
Willy Tarreau3bb617c2020-06-29 13:51:05 +02001197 U2H(stats[ST_F_SCUR].u.u32),
1198 U2H(stats[ST_F_USED_CONN_CUR].u.u32),
1199 U2H(stats[ST_F_SRV_ICUR].u.u32),
1200 U2H(stats[ST_F_IDLE_CONN_CUR].u.u32),
1201 U2H(stats[ST_F_SAFE_CONN_CUR].u.u32),
Willy Tarreaua9fcecb2020-06-29 15:38:53 +02001202 U2H(stats[ST_F_NEED_CONN_EST].u.u32),
Willy Tarreau3bb617c2020-06-29 13:51:05 +02001203
Willy Tarreauf21d17b2019-09-08 09:24:56 +02001204 LIM2A(stats[ST_F_SLIM].u.u32, "-"),
1205 stats[ST_F_SRV_ILIM].type ? U2H(stats[ST_F_SRV_ILIM].u.u32) : "-",
1206 U2H(stats[ST_F_SMAX].u.u32), LIM2A(stats[ST_F_SLIM].u.u32, "-"),
William Lallemand74c24fb2016-11-21 17:18:36 +01001207 U2H(stats[ST_F_STOT].u.u64),
1208 U2H(stats[ST_F_STOT].u.u64));
1209
1210 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
1211 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001212 chunk_appendf(out,
Willy Tarreauf1573842018-12-14 11:35:36 +01001213 "<tr><th>New connections:</th><td>%s</td></tr>"
1214 "<tr><th>Reused connections:</th><td>%s</td><td>(%d%%)</td></tr>"
Marcin Deranek3c27dda2020-05-15 18:32:51 +02001215 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001216 "<tr><th>- HTTP 1xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
1217 "<tr><th>- HTTP 2xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
1218 "<tr><th>- HTTP 3xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
1219 "<tr><th>- HTTP 4xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
1220 "<tr><th>- HTTP 5xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
1221 "<tr><th>- other responses:</th><td>%s</td><td>(%d%%)</td></tr>"
Willy Tarreau1b0f85e2018-05-28 15:12:40 +02001222 "<tr><th>Failed hdr rewrites:</th><td>%s</td></tr>"
Christopher Faulet0159ee42019-12-16 14:40:39 +01001223 "<tr><th>Internal error:</th><td>%s</td></tr>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001224 "",
Willy Tarreauf1573842018-12-14 11:35:36 +01001225 U2H(stats[ST_F_CONNECT].u.u64),
1226 U2H(stats[ST_F_REUSE].u.u64),
1227 (stats[ST_F_CONNECT].u.u64 + stats[ST_F_REUSE].u.u64) ?
1228 (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 +02001229 U2H(stats[ST_F_REQ_TOT].u.u64),
1230 U2H(stats[ST_F_HRSP_1XX].u.u64), stats[ST_F_REQ_TOT].u.u64 ?
1231 (int)(100 * stats[ST_F_HRSP_1XX].u.u64 / stats[ST_F_REQ_TOT].u.u64) : 0,
1232 U2H(stats[ST_F_HRSP_2XX].u.u64), stats[ST_F_REQ_TOT].u.u64 ?
1233 (int)(100 * stats[ST_F_HRSP_2XX].u.u64 / stats[ST_F_REQ_TOT].u.u64) : 0,
1234 U2H(stats[ST_F_HRSP_3XX].u.u64), stats[ST_F_REQ_TOT].u.u64 ?
1235 (int)(100 * stats[ST_F_HRSP_3XX].u.u64 / stats[ST_F_REQ_TOT].u.u64) : 0,
1236 U2H(stats[ST_F_HRSP_4XX].u.u64), stats[ST_F_REQ_TOT].u.u64 ?
1237 (int)(100 * stats[ST_F_HRSP_4XX].u.u64 / stats[ST_F_REQ_TOT].u.u64) : 0,
1238 U2H(stats[ST_F_HRSP_5XX].u.u64), stats[ST_F_REQ_TOT].u.u64 ?
1239 (int)(100 * stats[ST_F_HRSP_5XX].u.u64 / stats[ST_F_REQ_TOT].u.u64) : 0,
1240 U2H(stats[ST_F_HRSP_OTHER].u.u64), stats[ST_F_REQ_TOT].u.u64 ?
1241 (int)(100 * stats[ST_F_HRSP_OTHER].u.u64 / stats[ST_F_REQ_TOT].u.u64) : 0,
Christopher Faulet0159ee42019-12-16 14:40:39 +01001242 U2H(stats[ST_F_WREW].u.u64),
1243 U2H(stats[ST_F_EINT].u.u64));
William Lallemand74c24fb2016-11-21 17:18:36 +01001244 }
1245
Christopher Faulet0d1c2a62019-11-08 14:59:51 +01001246 chunk_appendf(out, "<tr><th colspan=3>Max / Avg over last 1024 success. conn.</th></tr>");
1247 chunk_appendf(out, "<tr><th>- Queue time:</th><td>%s / %s</td><td>ms</td></tr>",
1248 U2H(stats[ST_F_QT_MAX].u.u32), U2H(stats[ST_F_QTIME].u.u32));
1249 chunk_appendf(out, "<tr><th>- Connect time:</th><td>%s / %s</td><td>ms</td></tr>",
1250 U2H(stats[ST_F_CT_MAX].u.u32), U2H(stats[ST_F_CTIME].u.u32));
William Lallemand74c24fb2016-11-21 17:18:36 +01001251 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
Christopher Faulet0d1c2a62019-11-08 14:59:51 +01001252 chunk_appendf(out, "<tr><th>- Responses time:</th><td>%s / %s</td><td>ms</td></tr>",
1253 U2H(stats[ST_F_RT_MAX].u.u32), U2H(stats[ST_F_RTIME].u.u32));
1254 chunk_appendf(out, "<tr><th>- Total time:</th><td>%s / %s</td><td>ms</td></tr>",
1255 U2H(stats[ST_F_TT_MAX].u.u32), U2H(stats[ST_F_TTIME].u.u32));
William Lallemand74c24fb2016-11-21 17:18:36 +01001256
1257 chunk_appendf(out,
1258 "</table></div></u></td>"
1259 /* sessions: lbtot, last */
1260 "<td>%s</td><td>%s</td>",
1261 U2H(stats[ST_F_LBTOT].u.u64),
1262 human_time(stats[ST_F_LASTSESS].u.s32, 1));
1263
1264 chunk_appendf(out,
1265 /* bytes : in, out */
1266 "<td>%s</td><td>%s</td>"
1267 /* denied: req, resp */
1268 "<td></td><td>%s</td>"
1269 /* errors : request, connect */
1270 "<td></td><td>%s</td>"
1271 /* errors : response */
1272 "<td><u>%s<div class=tips>Connection resets during transfers: %lld client, %lld server</div></u></td>"
1273 /* warnings: retries, redispatches */
1274 "<td>%lld</td><td>%lld</td>"
1275 "",
1276 U2H(stats[ST_F_BIN].u.u64), U2H(stats[ST_F_BOUT].u.u64),
1277 U2H(stats[ST_F_DRESP].u.u64),
1278 U2H(stats[ST_F_ECON].u.u64),
1279 U2H(stats[ST_F_ERESP].u.u64),
1280 (long long)stats[ST_F_CLI_ABRT].u.u64,
1281 (long long)stats[ST_F_SRV_ABRT].u.u64,
1282 (long long)stats[ST_F_WRETR].u.u64,
1283 (long long)stats[ST_F_WREDIS].u.u64);
1284
1285 /* status, last change */
1286 chunk_appendf(out, "<td class=ac>");
1287
1288 /* FIXME!!!!
1289 * LASTCHG should contain the last change for *this* server and must be computed
1290 * properly above, as was done below, ie: this server if maint, otherwise ref server
1291 * if tracking. Note that ref is either local or remote depending on tracking.
1292 */
1293
1294
Willy Tarreau7b524852020-08-11 10:26:36 +02001295 if (strncmp(field_str(stats, ST_F_STATUS), "MAINT", 5) == 0) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001296 chunk_appendf(out, "%s MAINT", human_time(stats[ST_F_LASTCHG].u.u32, 1));
1297 }
Willy Tarreau7b524852020-08-11 10:26:36 +02001298 else if (strcmp(field_str(stats, ST_F_STATUS), "no check") == 0) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001299 chunk_strcat(out, "<i>no check</i>");
1300 }
1301 else {
1302 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 +02001303 if (strncmp(field_str(stats, ST_F_STATUS), "DOWN", 4) == 0) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001304 if (stats[ST_F_CHECK_HEALTH].u.u32)
1305 chunk_strcat(out, " &uarr;");
1306 }
1307 else if (stats[ST_F_CHECK_HEALTH].u.u32 < stats[ST_F_CHECK_RISE].u.u32 + stats[ST_F_CHECK_FALL].u.u32 - 1)
1308 chunk_strcat(out, " &darr;");
1309 }
1310
Willy Tarreau7b524852020-08-11 10:26:36 +02001311 if (strncmp(field_str(stats, ST_F_STATUS), "DOWN", 4) == 0 &&
William Lallemand74c24fb2016-11-21 17:18:36 +01001312 stats[ST_F_AGENT_STATUS].type && !stats[ST_F_AGENT_HEALTH].u.u32) {
1313 chunk_appendf(out,
1314 "</td><td class=ac><u> %s",
1315 field_str(stats, ST_F_AGENT_STATUS));
1316
1317 if (stats[ST_F_AGENT_CODE].type)
1318 chunk_appendf(out, "/%d", stats[ST_F_AGENT_CODE].u.u32);
1319
1320 if (stats[ST_F_AGENT_DURATION].type)
1321 chunk_appendf(out, " in %lums", (long)stats[ST_F_AGENT_DURATION].u.u64);
1322
1323 chunk_appendf(out, "<div class=tips>%s", field_str(stats, ST_F_AGENT_DESC));
1324
1325 if (*field_str(stats, ST_F_LAST_AGT)) {
1326 chunk_appendf(out, ": ");
1327 chunk_initstr(&src, field_str(stats, ST_F_LAST_AGT));
1328 chunk_htmlencode(out, &src);
1329 }
1330 chunk_appendf(out, "</div></u>");
1331 }
1332 else if (stats[ST_F_CHECK_STATUS].type) {
1333 chunk_appendf(out,
1334 "</td><td class=ac><u> %s",
1335 field_str(stats, ST_F_CHECK_STATUS));
1336
1337 if (stats[ST_F_CHECK_CODE].type)
1338 chunk_appendf(out, "/%d", stats[ST_F_CHECK_CODE].u.u32);
1339
1340 if (stats[ST_F_CHECK_DURATION].type)
1341 chunk_appendf(out, " in %lums", (long)stats[ST_F_CHECK_DURATION].u.u64);
1342
1343 chunk_appendf(out, "<div class=tips>%s", field_str(stats, ST_F_CHECK_DESC));
1344
1345 if (*field_str(stats, ST_F_LAST_CHK)) {
1346 chunk_appendf(out, ": ");
1347 chunk_initstr(&src, field_str(stats, ST_F_LAST_CHK));
1348 chunk_htmlencode(out, &src);
1349 }
1350 chunk_appendf(out, "</div></u>");
1351 }
1352 else
1353 chunk_appendf(out, "</td><td>");
1354
1355 chunk_appendf(out,
Willy Tarreaubd715102020-10-23 22:44:30 +02001356 /* weight / uweight */
1357 "</td><td class=ac>%d/%d</td>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001358 /* act, bck */
1359 "<td class=ac>%s</td><td class=ac>%s</td>"
1360 "",
Willy Tarreaubd715102020-10-23 22:44:30 +02001361 stats[ST_F_WEIGHT].u.u32, stats[ST_F_UWEIGHT].u.u32,
William Lallemand74c24fb2016-11-21 17:18:36 +01001362 stats[ST_F_BCK].u.u32 ? "-" : "Y",
1363 stats[ST_F_BCK].u.u32 ? "Y" : "-");
1364
1365 /* check failures: unique, fatal, down time */
1366 if (strcmp(field_str(stats, ST_F_STATUS), "MAINT (resolution)") == 0) {
1367 chunk_appendf(out, "<td class=ac colspan=3>resolution</td>");
1368 }
1369 else if (stats[ST_F_CHKFAIL].type) {
1370 chunk_appendf(out, "<td><u>%lld", (long long)stats[ST_F_CHKFAIL].u.u64);
1371
1372 if (stats[ST_F_HANAFAIL].type)
1373 chunk_appendf(out, "/%lld", (long long)stats[ST_F_HANAFAIL].u.u64);
1374
1375 chunk_appendf(out,
1376 "<div class=tips>Failed Health Checks%s</div></u></td>"
1377 "<td>%lld</td><td>%s</td>"
1378 "",
1379 stats[ST_F_HANAFAIL].type ? "/Health Analyses" : "",
1380 (long long)stats[ST_F_CHKDOWN].u.u64, human_time(stats[ST_F_DOWNTIME].u.u32, 1));
1381 }
1382 else if (strcmp(field_str(stats, ST_F_STATUS), "MAINT") != 0 && field_format(stats, ST_F_TRACKED) == FF_STR) {
1383 /* tracking a server (hence inherited maint would appear as "MAINT (via...)" */
1384 chunk_appendf(out,
1385 "<td class=ac colspan=3><a class=lfsb href=\"#%s\">via %s</a></td>",
1386 field_str(stats, ST_F_TRACKED), field_str(stats, ST_F_TRACKED));
1387 }
1388 else
1389 chunk_appendf(out, "<td colspan=3></td>");
1390
1391 /* throttle */
1392 if (stats[ST_F_THROTTLE].type)
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001393 chunk_appendf(out, "<td class=ac>%d %%</td>\n", stats[ST_F_THROTTLE].u.u32);
William Lallemand74c24fb2016-11-21 17:18:36 +01001394 else
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001395 chunk_appendf(out, "<td class=ac>-</td>");
1396
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001397 if (flags & STAT_SHMODULES) {
1398 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001399 chunk_appendf(out, "<td>");
1400
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001401 if (stats_px_get_cap(mod->domain_flags) & STATS_PX_CAP_SRV) {
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001402 chunk_appendf(out,
1403 "<u>%s<div class=tips><table class=det>",
1404 mod->name);
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001405 for (j = 0; j < mod->stats_count; ++j) {
1406 chunk_appendf(out,
1407 "<tr><th>%s</th><td>%s</td></tr>",
1408 mod->stats[j].desc, field_to_html_str(&stats[ST_F_TOTAL_FIELDS + i]));
1409 ++i;
1410 }
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001411 chunk_appendf(out, "</table></div></u>");
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001412 } else {
1413 i += mod->stats_count;
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001414 }
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001415
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001416 chunk_appendf(out, "</td>");
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001417 }
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001418 }
1419
1420 chunk_appendf(out, "</tr>\n");
William Lallemand74c24fb2016-11-21 17:18:36 +01001421 }
1422 else if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_BE) {
1423 chunk_appendf(out, "<tr class=\"backend\">");
Willy Tarreauab02b3f2019-10-09 11:11:46 +02001424 if (flags & STAT_ADMIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001425 /* Column sub-heading for Enable or Disable server */
1426 chunk_appendf(out, "<td></td>");
1427 }
1428 chunk_appendf(out,
1429 "<td class=ac>"
1430 /* name */
1431 "%s<a name=\"%s/Backend\"></a>"
1432 "<a class=lfsb href=\"#%s/Backend\">Backend</a>"
1433 "",
Willy Tarreau708c4162019-10-09 10:19:16 +02001434 (flags & STAT_SHLGNDS)?"<u>":"",
William Lallemand74c24fb2016-11-21 17:18:36 +01001435 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_PXNAME));
1436
Willy Tarreau708c4162019-10-09 10:19:16 +02001437 if (flags & STAT_SHLGNDS) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001438 /* balancing */
1439 chunk_appendf(out, "<div class=tips>balancing: %s",
1440 field_str(stats, ST_F_ALGO));
1441
1442 /* cookie */
1443 if (stats[ST_F_COOKIE].type) {
1444 chunk_appendf(out, ", cookie: '");
1445 chunk_initstr(&src, field_str(stats, ST_F_COOKIE));
1446 chunk_htmlencode(out, &src);
1447 chunk_appendf(out, "'");
1448 }
1449 chunk_appendf(out, "</div>");
1450 }
1451
1452 chunk_appendf(out,
1453 "%s</td>"
1454 /* queue : current, max */
1455 "<td>%s</td><td>%s</td><td></td>"
1456 /* sessions rate : current, max, limit */
1457 "<td>%s</td><td>%s</td><td></td>"
1458 "",
Willy Tarreau708c4162019-10-09 10:19:16 +02001459 (flags & STAT_SHLGNDS)?"</u>":"",
William Lallemand74c24fb2016-11-21 17:18:36 +01001460 U2H(stats[ST_F_QCUR].u.u32), U2H(stats[ST_F_QMAX].u.u32),
1461 U2H(stats[ST_F_RATE].u.u32), U2H(stats[ST_F_RATE_MAX].u.u32));
1462
1463 chunk_appendf(out,
1464 /* sessions: current, max, limit, total */
1465 "<td>%s</td><td>%s</td><td>%s</td>"
1466 "<td><u>%s<div class=tips><table class=det>"
1467 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
1468 "",
Willy Tarreau8e0f1752016-12-12 15:07:29 +01001469 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 +01001470 U2H(stats[ST_F_STOT].u.u64),
1471 U2H(stats[ST_F_STOT].u.u64));
1472
1473 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
1474 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0) {
1475 chunk_appendf(out,
Willy Tarreauf1573842018-12-14 11:35:36 +01001476 "<tr><th>New connections:</th><td>%s</td></tr>"
1477 "<tr><th>Reused connections:</th><td>%s</td><td>(%d%%)</td></tr>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001478 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
1479 "<tr><th>- HTTP 1xx responses:</th><td>%s</td></tr>"
1480 "<tr><th>- HTTP 2xx responses:</th><td>%s</td></tr>"
1481 "<tr><th>&nbsp;&nbsp;Compressed 2xx:</th><td>%s</td><td>(%d%%)</td></tr>"
1482 "<tr><th>- HTTP 3xx responses:</th><td>%s</td></tr>"
1483 "<tr><th>- HTTP 4xx responses:</th><td>%s</td></tr>"
1484 "<tr><th>- HTTP 5xx responses:</th><td>%s</td></tr>"
1485 "<tr><th>- other responses:</th><td>%s</td></tr>"
Willy Tarreaua1214a52018-12-14 14:00:25 +01001486 "<tr><th>Cache lookups:</th><td>%s</td></tr>"
1487 "<tr><th>Cache hits:</th><td>%s</td><td>(%d%%)</td></tr>"
Willy Tarreau1b0f85e2018-05-28 15:12:40 +02001488 "<tr><th>Failed hdr rewrites:</th><td>%s</td></tr>"
Christopher Faulet0159ee42019-12-16 14:40:39 +01001489 "<tr><th>Internal errors:</th><td>%s</td></tr>"
Christopher Faulet0d1c2a62019-11-08 14:59:51 +01001490 "",
Willy Tarreauf1573842018-12-14 11:35:36 +01001491 U2H(stats[ST_F_CONNECT].u.u64),
1492 U2H(stats[ST_F_REUSE].u.u64),
1493 (stats[ST_F_CONNECT].u.u64 + stats[ST_F_REUSE].u.u64) ?
1494 (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 +01001495 U2H(stats[ST_F_REQ_TOT].u.u64),
1496 U2H(stats[ST_F_HRSP_1XX].u.u64),
1497 U2H(stats[ST_F_HRSP_2XX].u.u64),
1498 U2H(stats[ST_F_COMP_RSP].u.u64),
1499 stats[ST_F_HRSP_2XX].u.u64 ?
1500 (int)(100 * stats[ST_F_COMP_RSP].u.u64 / stats[ST_F_HRSP_2XX].u.u64) : 0,
1501 U2H(stats[ST_F_HRSP_3XX].u.u64),
1502 U2H(stats[ST_F_HRSP_4XX].u.u64),
1503 U2H(stats[ST_F_HRSP_5XX].u.u64),
Willy Tarreaufeead3a2018-12-14 13:48:44 +01001504 U2H(stats[ST_F_HRSP_OTHER].u.u64),
Willy Tarreaua1214a52018-12-14 14:00:25 +01001505 U2H(stats[ST_F_CACHE_LOOKUPS].u.u64),
1506 U2H(stats[ST_F_CACHE_HITS].u.u64),
1507 stats[ST_F_CACHE_LOOKUPS].u.u64 ?
1508 (int)(100 * stats[ST_F_CACHE_HITS].u.u64 / stats[ST_F_CACHE_LOOKUPS].u.u64) : 0,
Christopher Faulet0159ee42019-12-16 14:40:39 +01001509 U2H(stats[ST_F_WREW].u.u64),
1510 U2H(stats[ST_F_EINT].u.u64));
William Lallemand74c24fb2016-11-21 17:18:36 +01001511 }
1512
Christopher Faulet0d1c2a62019-11-08 14:59:51 +01001513 chunk_appendf(out, "<tr><th colspan=3>Max / Avg over last 1024 success. conn.</th></tr>");
1514 chunk_appendf(out, "<tr><th>- Queue time:</th><td>%s / %s</td><td>ms</td></tr>",
1515 U2H(stats[ST_F_QT_MAX].u.u32), U2H(stats[ST_F_QTIME].u.u32));
1516 chunk_appendf(out, "<tr><th>- Connect time:</th><td>%s / %s</td><td>ms</td></tr>",
1517 U2H(stats[ST_F_CT_MAX].u.u32), U2H(stats[ST_F_CTIME].u.u32));
William Lallemand74c24fb2016-11-21 17:18:36 +01001518 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
Christopher Faulet0d1c2a62019-11-08 14:59:51 +01001519 chunk_appendf(out, "<tr><th>- Responses time:</th><td>%s / %s</td><td>ms</td></tr>",
1520 U2H(stats[ST_F_RT_MAX].u.u32), U2H(stats[ST_F_RTIME].u.u32));
1521 chunk_appendf(out, "<tr><th>- Total time:</th><td>%s / %s</td><td>ms</td></tr>",
1522 U2H(stats[ST_F_TT_MAX].u.u32), U2H(stats[ST_F_TTIME].u.u32));
William Lallemand74c24fb2016-11-21 17:18:36 +01001523
1524 chunk_appendf(out,
1525 "</table></div></u></td>"
1526 /* sessions: lbtot, last */
1527 "<td>%s</td><td>%s</td>"
1528 /* bytes: in */
1529 "<td>%s</td>"
1530 "",
1531 U2H(stats[ST_F_LBTOT].u.u64),
1532 human_time(stats[ST_F_LASTSESS].u.s32, 1),
1533 U2H(stats[ST_F_BIN].u.u64));
1534
1535 chunk_appendf(out,
1536 /* bytes:out + compression stats (via hover): comp_in, comp_out, comp_byp */
1537 "<td>%s%s<div class=tips><table class=det>"
1538 "<tr><th>Response bytes in:</th><td>%s</td></tr>"
1539 "<tr><th>Compression in:</th><td>%s</td></tr>"
1540 "<tr><th>Compression out:</th><td>%s</td><td>(%d%%)</td></tr>"
1541 "<tr><th>Compression bypass:</th><td>%s</td></tr>"
1542 "<tr><th>Total bytes saved:</th><td>%s</td><td>(%d%%)</td></tr>"
1543 "</table></div>%s</td>",
1544 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "<u>":"",
1545 U2H(stats[ST_F_BOUT].u.u64),
1546 U2H(stats[ST_F_BOUT].u.u64),
1547 U2H(stats[ST_F_COMP_IN].u.u64),
1548 U2H(stats[ST_F_COMP_OUT].u.u64),
1549 stats[ST_F_COMP_IN].u.u64 ? (int)(stats[ST_F_COMP_OUT].u.u64 * 100 / stats[ST_F_COMP_IN].u.u64) : 0,
1550 U2H(stats[ST_F_COMP_BYP].u.u64),
1551 U2H(stats[ST_F_COMP_IN].u.u64 - stats[ST_F_COMP_OUT].u.u64),
1552 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,
1553 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "</u>":"");
1554
1555 chunk_appendf(out,
1556 /* denied: req, resp */
1557 "<td>%s</td><td>%s</td>"
1558 /* errors : request, connect */
1559 "<td></td><td>%s</td>"
1560 /* errors : response */
1561 "<td><u>%s<div class=tips>Connection resets during transfers: %lld client, %lld server</div></u></td>"
1562 /* warnings: retries, redispatches */
1563 "<td>%lld</td><td>%lld</td>"
1564 /* backend status: reflect backend status (up/down): we display UP
1565 * if the backend has known working servers or if it has no server at
1566 * all (eg: for stats). Then we display the total weight, number of
1567 * active and backups. */
Willy Tarreaubd715102020-10-23 22:44:30 +02001568 "<td class=ac>%s %s</td><td class=ac>&nbsp;</td><td class=ac>%d/%d</td>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001569 "<td class=ac>%d</td><td class=ac>%d</td>"
1570 "",
1571 U2H(stats[ST_F_DREQ].u.u64), U2H(stats[ST_F_DRESP].u.u64),
1572 U2H(stats[ST_F_ECON].u.u64),
1573 U2H(stats[ST_F_ERESP].u.u64),
1574 (long long)stats[ST_F_CLI_ABRT].u.u64,
1575 (long long)stats[ST_F_SRV_ABRT].u.u64,
1576 (long long)stats[ST_F_WRETR].u.u64, (long long)stats[ST_F_WREDIS].u.u64,
1577 human_time(stats[ST_F_LASTCHG].u.u32, 1),
1578 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 +02001579 stats[ST_F_WEIGHT].u.u32, stats[ST_F_UWEIGHT].u.u32,
William Lallemand74c24fb2016-11-21 17:18:36 +01001580 stats[ST_F_ACT].u.u32, stats[ST_F_BCK].u.u32);
1581
1582 chunk_appendf(out,
1583 /* rest of backend: nothing, down transitions, total downtime, throttle */
1584 "<td class=ac>&nbsp;</td><td>%d</td>"
1585 "<td>%s</td>"
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001586 "<td></td>",
William Lallemand74c24fb2016-11-21 17:18:36 +01001587 stats[ST_F_CHKDOWN].u.u32,
1588 stats[ST_F_DOWNTIME].type ? human_time(stats[ST_F_DOWNTIME].u.u32, 1) : "&nbsp;");
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001589
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001590 if (flags & STAT_SHMODULES) {
1591 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001592 chunk_appendf(out, "<td>");
1593
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001594 if (stats_px_get_cap(mod->domain_flags) & STATS_PX_CAP_BE) {
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001595 chunk_appendf(out,
1596 "<u>%s<div class=tips><table class=det>",
1597 mod->name);
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001598 for (j = 0; j < mod->stats_count; ++j) {
1599 chunk_appendf(out,
1600 "<tr><th>%s</th><td>%s</td></tr>",
1601 mod->stats[j].desc, field_to_html_str(&stats[ST_F_TOTAL_FIELDS + i]));
1602 ++i;
1603 }
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001604 chunk_appendf(out, "</table></div></u>");
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001605 } else {
1606 i += mod->stats_count;
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001607 }
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001608
1609 chunk_appendf(out, "</td>");
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001610 }
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001611 }
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001612
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001613 chunk_appendf(out, "</tr>");
William Lallemand74c24fb2016-11-21 17:18:36 +01001614 }
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001615
William Lallemand74c24fb2016-11-21 17:18:36 +01001616 return 1;
1617}
1618
Amaury Denoyelle97323c92020-10-02 18:32:01 +02001619int stats_dump_one_line(const struct field *stats, size_t stats_count,
1620 struct appctx *appctx)
William Lallemand74c24fb2016-11-21 17:18:36 +01001621{
Simon Horman05ee2132017-01-04 09:37:25 +01001622 int ret;
1623
William Lallemand74c24fb2016-11-21 17:18:36 +01001624 if (appctx->ctx.stats.flags & STAT_FMT_HTML)
Willy Tarreau43241ff2019-10-09 11:27:51 +02001625 ret = stats_dump_fields_html(&trash, stats, appctx->ctx.stats.flags);
William Lallemand74c24fb2016-11-21 17:18:36 +01001626 else if (appctx->ctx.stats.flags & STAT_FMT_TYPED)
Amaury Denoyelle072f97e2020-10-05 11:49:37 +02001627 ret = stats_dump_fields_typed(&trash, stats, stats_count, appctx->ctx.stats.flags, appctx->ctx.stats.domain);
Simon Horman05ee2132017-01-04 09:37:25 +01001628 else if (appctx->ctx.stats.flags & STAT_FMT_JSON)
Amaury Denoyelle072f97e2020-10-05 11:49:37 +02001629 ret = stats_dump_fields_json(&trash, stats, stats_count, appctx->ctx.stats.flags, appctx->ctx.stats.domain);
William Lallemand74c24fb2016-11-21 17:18:36 +01001630 else
Amaury Denoyelle50660a82020-10-05 11:49:39 +02001631 ret = stats_dump_fields_csv(&trash, stats, stats_count, appctx->ctx.stats.flags, appctx->ctx.stats.domain);
Simon Horman05ee2132017-01-04 09:37:25 +01001632
1633 if (ret)
1634 appctx->ctx.stats.flags |= STAT_STARTED;
1635
1636 return ret;
William Lallemand74c24fb2016-11-21 17:18:36 +01001637}
1638
William Dauchy0ef54392021-01-17 18:27:45 +01001639/* Fill <stats> with the frontend statistics. <stats> is preallocated array of
Christopher Fauletd808f172021-01-22 17:33:22 +01001640 * length <len>. If <selected_field> is != NULL, only fill this one. The length
1641 * of the array must be at least ST_F_TOTAL_FIELDS. If this length is less than
1642 * this value, or if the selected field is not implemented for frontends, the
1643 * function returns 0, otherwise, it returns 1.
William Lallemand74c24fb2016-11-21 17:18:36 +01001644 */
William Dauchy0ef54392021-01-17 18:27:45 +01001645int stats_fill_fe_stats(struct proxy *px, struct field *stats, int len,
1646 enum stat_field *selected_field)
William Lallemand74c24fb2016-11-21 17:18:36 +01001647{
William Dauchy0ef54392021-01-17 18:27:45 +01001648 enum stat_field current_field = (selected_field != NULL ? *selected_field : 0);
William Dauchy0ef54392021-01-17 18:27:45 +01001649
William Lallemand74c24fb2016-11-21 17:18:36 +01001650 if (len < ST_F_TOTAL_FIELDS)
1651 return 0;
1652
William Dauchy0ef54392021-01-17 18:27:45 +01001653 for (; current_field < ST_F_TOTAL_FIELDS; current_field++) {
Christopher Faulet8596bfb2021-01-25 15:16:41 +01001654 struct field metric = { 0 };
1655
William Dauchy0ef54392021-01-17 18:27:45 +01001656 switch (current_field) {
1657 case ST_F_PXNAME:
1658 metric = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
1659 break;
1660 case ST_F_SVNAME:
1661 metric = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, "FRONTEND");
1662 break;
1663 case ST_F_MODE:
1664 metric = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
1665 break;
1666 case ST_F_SCUR:
1667 metric = mkf_u32(0, px->feconn);
1668 break;
1669 case ST_F_SMAX:
1670 metric = mkf_u32(FN_MAX, px->fe_counters.conn_max);
1671 break;
1672 case ST_F_SLIM:
1673 metric = mkf_u32(FO_CONFIG|FN_LIMIT, px->maxconn);
1674 break;
1675 case ST_F_STOT:
1676 metric = mkf_u64(FN_COUNTER, px->fe_counters.cum_sess);
1677 break;
1678 case ST_F_BIN:
1679 metric = mkf_u64(FN_COUNTER, px->fe_counters.bytes_in);
1680 break;
1681 case ST_F_BOUT:
1682 metric = mkf_u64(FN_COUNTER, px->fe_counters.bytes_out);
1683 break;
1684 case ST_F_DREQ:
1685 metric = mkf_u64(FN_COUNTER, px->fe_counters.denied_req);
1686 break;
1687 case ST_F_DRESP:
1688 metric = mkf_u64(FN_COUNTER, px->fe_counters.denied_resp);
1689 break;
1690 case ST_F_EREQ:
1691 metric = mkf_u64(FN_COUNTER, px->fe_counters.failed_req);
1692 break;
1693 case ST_F_DCON:
1694 metric = mkf_u64(FN_COUNTER, px->fe_counters.denied_conn);
1695 break;
1696 case ST_F_DSES:
1697 metric = mkf_u64(FN_COUNTER, px->fe_counters.denied_sess);
1698 break;
1699 case ST_F_STATUS:
Christopher Fauletdfd10ab2021-10-06 14:24:19 +02001700 metric = mkf_str(FO_STATUS, (px->flags & (PR_FL_DISABLED|PR_FL_STOPPED)) ? "STOP" : "OPEN");
William Dauchy0ef54392021-01-17 18:27:45 +01001701 break;
1702 case ST_F_PID:
Willy Tarreaue8422bf2021-06-15 09:08:18 +02001703 metric = mkf_u32(FO_KEY, 1);
William Dauchy0ef54392021-01-17 18:27:45 +01001704 break;
1705 case ST_F_IID:
1706 metric = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
1707 break;
1708 case ST_F_SID:
1709 metric = mkf_u32(FO_KEY|FS_SERVICE, 0);
1710 break;
1711 case ST_F_TYPE:
1712 metric = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_FE);
1713 break;
1714 case ST_F_RATE:
1715 metric = mkf_u32(FN_RATE, read_freq_ctr(&px->fe_sess_per_sec));
1716 break;
1717 case ST_F_RATE_LIM:
1718 metric = mkf_u32(FO_CONFIG|FN_LIMIT, px->fe_sps_lim);
1719 break;
1720 case ST_F_RATE_MAX:
1721 metric = mkf_u32(FN_MAX, px->fe_counters.sps_max);
1722 break;
1723 case ST_F_WREW:
1724 metric = mkf_u64(FN_COUNTER, px->fe_counters.failed_rewrites);
1725 break;
1726 case ST_F_EINT:
1727 metric = mkf_u64(FN_COUNTER, px->fe_counters.internal_errors);
1728 break;
1729 case ST_F_HRSP_1XX:
William Dauchy2107a0f2021-01-22 21:09:48 +01001730 if (px->mode == PR_MODE_HTTP)
1731 metric = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[1]);
William Dauchy0ef54392021-01-17 18:27:45 +01001732 break;
1733 case ST_F_HRSP_2XX:
William Dauchy2107a0f2021-01-22 21:09:48 +01001734 if (px->mode == PR_MODE_HTTP)
1735 metric = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[2]);
William Dauchy0ef54392021-01-17 18:27:45 +01001736 break;
1737 case ST_F_HRSP_3XX:
William Dauchy2107a0f2021-01-22 21:09:48 +01001738 if (px->mode == PR_MODE_HTTP)
1739 metric = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[3]);
William Dauchy0ef54392021-01-17 18:27:45 +01001740 break;
1741 case ST_F_HRSP_4XX:
William Dauchy2107a0f2021-01-22 21:09:48 +01001742 if (px->mode == PR_MODE_HTTP)
1743 metric = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[4]);
William Dauchy0ef54392021-01-17 18:27:45 +01001744 break;
1745 case ST_F_HRSP_5XX:
William Dauchy2107a0f2021-01-22 21:09:48 +01001746 if (px->mode == PR_MODE_HTTP)
1747 metric = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[5]);
William Dauchy0ef54392021-01-17 18:27:45 +01001748 break;
1749 case ST_F_HRSP_OTHER:
William Dauchy2107a0f2021-01-22 21:09:48 +01001750 if (px->mode == PR_MODE_HTTP)
1751 metric = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[0]);
William Dauchy0ef54392021-01-17 18:27:45 +01001752 break;
1753 case ST_F_INTERCEPTED:
William Dauchy2107a0f2021-01-22 21:09:48 +01001754 if (px->mode == PR_MODE_HTTP)
1755 metric = mkf_u64(FN_COUNTER, px->fe_counters.intercepted_req);
William Dauchy0ef54392021-01-17 18:27:45 +01001756 break;
1757 case ST_F_CACHE_LOOKUPS:
William Dauchy2107a0f2021-01-22 21:09:48 +01001758 if (px->mode == PR_MODE_HTTP)
1759 metric = mkf_u64(FN_COUNTER, px->fe_counters.p.http.cache_lookups);
William Dauchy0ef54392021-01-17 18:27:45 +01001760 break;
1761 case ST_F_CACHE_HITS:
William Dauchy2107a0f2021-01-22 21:09:48 +01001762 if (px->mode == PR_MODE_HTTP)
1763 metric = mkf_u64(FN_COUNTER, px->fe_counters.p.http.cache_hits);
William Dauchy0ef54392021-01-17 18:27:45 +01001764 break;
1765 case ST_F_REQ_RATE:
1766 metric = mkf_u32(FN_RATE, read_freq_ctr(&px->fe_req_per_sec));
1767 break;
1768 case ST_F_REQ_RATE_MAX:
1769 metric = mkf_u32(FN_MAX, px->fe_counters.p.http.rps_max);
1770 break;
1771 case ST_F_REQ_TOT:
1772 metric = mkf_u64(FN_COUNTER, px->fe_counters.p.http.cum_req);
1773 break;
1774 case ST_F_COMP_IN:
1775 metric = mkf_u64(FN_COUNTER, px->fe_counters.comp_in);
1776 break;
1777 case ST_F_COMP_OUT:
1778 metric = mkf_u64(FN_COUNTER, px->fe_counters.comp_out);
1779 break;
1780 case ST_F_COMP_BYP:
1781 metric = mkf_u64(FN_COUNTER, px->fe_counters.comp_byp);
1782 break;
1783 case ST_F_COMP_RSP:
1784 metric = mkf_u64(FN_COUNTER, px->fe_counters.p.http.comp_rsp);
1785 break;
1786 case ST_F_CONN_RATE:
1787 metric = mkf_u32(FN_RATE, read_freq_ctr(&px->fe_conn_per_sec));
1788 break;
1789 case ST_F_CONN_RATE_MAX:
1790 metric = mkf_u32(FN_MAX, px->fe_counters.cps_max);
1791 break;
1792 case ST_F_CONN_TOT:
1793 metric = mkf_u64(FN_COUNTER, px->fe_counters.cum_conn);
1794 break;
1795 default:
Christopher Fauletd808f172021-01-22 17:33:22 +01001796 /* not used for frontends. If a specific metric
1797 * is requested, return an error. Otherwise continue.
1798 */
1799 if (selected_field != NULL)
1800 return 0;
1801 continue;
William Dauchy0ef54392021-01-17 18:27:45 +01001802 }
1803 stats[current_field] = metric;
1804 if (selected_field != NULL)
1805 break;
William Lallemand74c24fb2016-11-21 17:18:36 +01001806 }
William Lallemand74c24fb2016-11-21 17:18:36 +01001807 return 1;
1808}
1809
1810/* Dumps a frontend's line to the trash for the current proxy <px> and uses
Christopher Faulet908628c2022-03-25 16:43:49 +01001811 * the state from conn-stream <cs>. The caller is responsible for clearing
William Lallemand74c24fb2016-11-21 17:18:36 +01001812 * the trash if needed. Returns non-zero if it emits anything, zero otherwise.
1813 */
Christopher Faulet908628c2022-03-25 16:43:49 +01001814static int stats_dump_fe_stats(struct conn_stream *cs, struct proxy *px)
William Lallemand74c24fb2016-11-21 17:18:36 +01001815{
Christopher Faulet908628c2022-03-25 16:43:49 +01001816 struct appctx *appctx = __cs_appctx(cs);
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02001817 struct field *stats = stat_l[STATS_DOMAIN_PROXY];
1818 struct stats_module *mod;
1819 size_t stats_count = ST_F_TOTAL_FIELDS;
William Lallemand74c24fb2016-11-21 17:18:36 +01001820
1821 if (!(px->cap & PR_CAP_FE))
1822 return 0;
1823
1824 if ((appctx->ctx.stats.flags & STAT_BOUND) && !(appctx->ctx.stats.type & (1 << STATS_TYPE_FE)))
1825 return 0;
1826
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02001827 memset(stats, 0, sizeof(struct field) * stat_count[STATS_DOMAIN_PROXY]);
1828
William Dauchy0ef54392021-01-17 18:27:45 +01001829 if (!stats_fill_fe_stats(px, stats, ST_F_TOTAL_FIELDS, NULL))
William Lallemand74c24fb2016-11-21 17:18:36 +01001830 return 0;
1831
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02001832 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
1833 void *counters;
1834
1835 if (!(stats_px_get_cap(mod->domain_flags) & STATS_PX_CAP_FE)) {
1836 stats_count += mod->stats_count;
1837 continue;
1838 }
1839
1840 counters = EXTRA_COUNTERS_GET(px->extra_counters_fe, mod);
1841 mod->fill_stats(counters, stats + stats_count);
1842 stats_count += mod->stats_count;
1843 }
1844
1845 return stats_dump_one_line(stats, stats_count, appctx);
William Lallemand74c24fb2016-11-21 17:18:36 +01001846}
1847
William Dauchy655e14e2021-02-14 23:22:54 +01001848/* Fill <stats> with the listener statistics. <stats> is preallocated array of
1849 * length <len>. The length of the array must be at least ST_F_TOTAL_FIELDS. If
1850 * this length is less then this value, the function returns 0, otherwise, it
1851 * returns 1. If selected_field is != NULL, only fill this one. <flags> can
1852 * take the value STAT_SHLGNDS.
William Lallemand74c24fb2016-11-21 17:18:36 +01001853 */
1854int stats_fill_li_stats(struct proxy *px, struct listener *l, int flags,
William Dauchy655e14e2021-02-14 23:22:54 +01001855 struct field *stats, int len, enum stat_field *selected_field)
William Lallemand74c24fb2016-11-21 17:18:36 +01001856{
William Dauchy655e14e2021-02-14 23:22:54 +01001857 enum stat_field current_field = (selected_field != NULL ? *selected_field : 0);
Willy Tarreau83061a82018-07-13 11:56:34 +02001858 struct buffer *out = get_trash_chunk();
William Lallemand74c24fb2016-11-21 17:18:36 +01001859
1860 if (len < ST_F_TOTAL_FIELDS)
1861 return 0;
1862
1863 if (!l->counters)
1864 return 0;
1865
1866 chunk_reset(out);
William Lallemand74c24fb2016-11-21 17:18:36 +01001867
William Dauchy655e14e2021-02-14 23:22:54 +01001868 for (; current_field < ST_F_TOTAL_FIELDS; current_field++) {
1869 struct field metric = { 0 };
William Lallemand74c24fb2016-11-21 17:18:36 +01001870
William Dauchy655e14e2021-02-14 23:22:54 +01001871 switch (current_field) {
1872 case ST_F_PXNAME:
1873 metric = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
1874 break;
1875 case ST_F_SVNAME:
1876 metric = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, l->name);
1877 break;
1878 case ST_F_MODE:
1879 metric = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
1880 break;
1881 case ST_F_SCUR:
1882 metric = mkf_u32(0, l->nbconn);
1883 break;
1884 case ST_F_SMAX:
1885 metric = mkf_u32(FN_MAX, l->counters->conn_max);
1886 break;
1887 case ST_F_SLIM:
1888 metric = mkf_u32(FO_CONFIG|FN_LIMIT, l->maxconn);
1889 break;
1890 case ST_F_STOT:
1891 metric = mkf_u64(FN_COUNTER, l->counters->cum_conn);
1892 break;
1893 case ST_F_BIN:
1894 metric = mkf_u64(FN_COUNTER, l->counters->bytes_in);
1895 break;
1896 case ST_F_BOUT:
1897 metric = mkf_u64(FN_COUNTER, l->counters->bytes_out);
1898 break;
1899 case ST_F_DREQ:
1900 metric = mkf_u64(FN_COUNTER, l->counters->denied_req);
1901 break;
1902 case ST_F_DRESP:
1903 metric = mkf_u64(FN_COUNTER, l->counters->denied_resp);
1904 break;
1905 case ST_F_EREQ:
1906 metric = mkf_u64(FN_COUNTER, l->counters->failed_req);
1907 break;
1908 case ST_F_DCON:
1909 metric = mkf_u64(FN_COUNTER, l->counters->denied_conn);
1910 break;
1911 case ST_F_DSES:
1912 metric = mkf_u64(FN_COUNTER, l->counters->denied_sess);
1913 break;
1914 case ST_F_STATUS:
William Dauchy3679d0c2021-02-14 23:22:55 +01001915 metric = mkf_str(FO_STATUS, li_status_st[get_li_status(l)]);
William Dauchy655e14e2021-02-14 23:22:54 +01001916 break;
1917 case ST_F_PID:
Willy Tarreaue8422bf2021-06-15 09:08:18 +02001918 metric = mkf_u32(FO_KEY, 1);
William Dauchy655e14e2021-02-14 23:22:54 +01001919 break;
1920 case ST_F_IID:
1921 metric = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
1922 break;
1923 case ST_F_SID:
1924 metric = mkf_u32(FO_KEY|FS_SERVICE, l->luid);
1925 break;
1926 case ST_F_TYPE:
1927 metric = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_SO);
1928 break;
1929 case ST_F_WREW:
1930 metric = mkf_u64(FN_COUNTER, l->counters->failed_rewrites);
1931 break;
1932 case ST_F_EINT:
1933 metric = mkf_u64(FN_COUNTER, l->counters->internal_errors);
1934 break;
1935 case ST_F_ADDR:
1936 if (flags & STAT_SHLGNDS) {
1937 char str[INET6_ADDRSTRLEN];
1938 int port;
William Lallemand74c24fb2016-11-21 17:18:36 +01001939
William Dauchy655e14e2021-02-14 23:22:54 +01001940 port = get_host_port(&l->rx.addr);
1941 switch (addr_to_str(&l->rx.addr, str, sizeof(str))) {
1942 case AF_INET:
1943 metric = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
1944 chunk_appendf(out, "%s:%d", str, port);
1945 break;
1946 case AF_INET6:
1947 metric = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
1948 chunk_appendf(out, "[%s]:%d", str, port);
1949 break;
1950 case AF_UNIX:
1951 metric = mkf_str(FO_CONFIG|FS_SERVICE, "unix");
1952 break;
1953 case -1:
1954 metric = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
1955 chunk_strcat(out, strerror(errno));
1956 break;
1957 default: /* address family not supported */
1958 break;
1959 }
1960 }
1961 break;
1962 default:
1963 /* not used for listen. If a specific metric
1964 * is requested, return an error. Otherwise continue.
1965 */
1966 if (selected_field != NULL)
1967 return 0;
1968 continue;
William Lallemand74c24fb2016-11-21 17:18:36 +01001969 }
William Dauchy655e14e2021-02-14 23:22:54 +01001970 stats[current_field] = metric;
1971 if (selected_field != NULL)
1972 break;
William Lallemand74c24fb2016-11-21 17:18:36 +01001973 }
William Lallemand74c24fb2016-11-21 17:18:36 +01001974 return 1;
1975}
1976
1977/* Dumps a line for listener <l> and proxy <px> to the trash and uses the state
Christopher Faulet908628c2022-03-25 16:43:49 +01001978 * from conn-stream <cs>. The caller is responsible for clearing the trash
Willy Tarreau43241ff2019-10-09 11:27:51 +02001979 * if needed. Returns non-zero if it emits anything, zero otherwise.
William Lallemand74c24fb2016-11-21 17:18:36 +01001980 */
Christopher Faulet908628c2022-03-25 16:43:49 +01001981static int stats_dump_li_stats(struct conn_stream *cs, struct proxy *px, struct listener *l)
William Lallemand74c24fb2016-11-21 17:18:36 +01001982{
Christopher Faulet908628c2022-03-25 16:43:49 +01001983 struct appctx *appctx = __cs_appctx(cs);
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02001984 struct field *stats = stat_l[STATS_DOMAIN_PROXY];
1985 struct stats_module *mod;
1986 size_t stats_count = ST_F_TOTAL_FIELDS;
1987
1988 memset(stats, 0, sizeof(struct field) * stat_count[STATS_DOMAIN_PROXY]);
William Lallemand74c24fb2016-11-21 17:18:36 +01001989
William Dauchy655e14e2021-02-14 23:22:54 +01001990 if (!stats_fill_li_stats(px, l, appctx->ctx.stats.flags, stats,
1991 ST_F_TOTAL_FIELDS, NULL))
William Lallemand74c24fb2016-11-21 17:18:36 +01001992 return 0;
1993
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02001994 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
1995 void *counters;
1996
1997 if (!(stats_px_get_cap(mod->domain_flags) & STATS_PX_CAP_LI)) {
1998 stats_count += mod->stats_count;
1999 continue;
2000 }
2001
2002 counters = EXTRA_COUNTERS_GET(l->extra_counters, mod);
2003 mod->fill_stats(counters, stats + stats_count);
2004 stats_count += mod->stats_count;
2005 }
2006
2007 return stats_dump_one_line(stats, stats_count, appctx);
William Lallemand74c24fb2016-11-21 17:18:36 +01002008}
2009
2010enum srv_stats_state {
2011 SRV_STATS_STATE_DOWN = 0,
2012 SRV_STATS_STATE_DOWN_AGENT,
2013 SRV_STATS_STATE_GOING_UP,
2014 SRV_STATS_STATE_UP_GOING_DOWN,
2015 SRV_STATS_STATE_UP,
2016 SRV_STATS_STATE_NOLB_GOING_DOWN,
2017 SRV_STATS_STATE_NOLB,
2018 SRV_STATS_STATE_DRAIN_GOING_DOWN,
2019 SRV_STATS_STATE_DRAIN,
2020 SRV_STATS_STATE_DRAIN_AGENT,
2021 SRV_STATS_STATE_NO_CHECK,
2022
2023 SRV_STATS_STATE_COUNT, /* Must be last */
2024};
2025
2026static const char *srv_hlt_st[SRV_STATS_STATE_COUNT] = {
2027 [SRV_STATS_STATE_DOWN] = "DOWN",
2028 [SRV_STATS_STATE_DOWN_AGENT] = "DOWN (agent)",
2029 [SRV_STATS_STATE_GOING_UP] = "DOWN %d/%d",
2030 [SRV_STATS_STATE_UP_GOING_DOWN] = "UP %d/%d",
2031 [SRV_STATS_STATE_UP] = "UP",
2032 [SRV_STATS_STATE_NOLB_GOING_DOWN] = "NOLB %d/%d",
2033 [SRV_STATS_STATE_NOLB] = "NOLB",
2034 [SRV_STATS_STATE_DRAIN_GOING_DOWN] = "DRAIN %d/%d",
2035 [SRV_STATS_STATE_DRAIN] = "DRAIN",
2036 [SRV_STATS_STATE_DRAIN_AGENT] = "DRAIN (agent)",
2037 [SRV_STATS_STATE_NO_CHECK] = "no check"
2038};
2039
William Dauchyd3a9a492021-01-25 17:29:03 +01002040/* Compute server state helper
William Lallemand74c24fb2016-11-21 17:18:36 +01002041 */
William Dauchyd3a9a492021-01-25 17:29:03 +01002042static void stats_fill_sv_stats_computestate(struct server *sv, struct server *ref,
2043 enum srv_stats_state *state)
William Lallemand74c24fb2016-11-21 17:18:36 +01002044{
Emeric Brun52a91d32017-08-31 14:41:55 +02002045 if (sv->cur_state == SRV_ST_RUNNING || sv->cur_state == SRV_ST_STARTING) {
William Lallemand74c24fb2016-11-21 17:18:36 +01002046 if ((ref->check.state & CHK_ST_ENABLED) &&
2047 (ref->check.health < ref->check.rise + ref->check.fall - 1)) {
William Dauchyd3a9a492021-01-25 17:29:03 +01002048 *state = SRV_STATS_STATE_UP_GOING_DOWN;
William Lallemand74c24fb2016-11-21 17:18:36 +01002049 } else {
William Dauchyd3a9a492021-01-25 17:29:03 +01002050 *state = SRV_STATS_STATE_UP;
William Lallemand74c24fb2016-11-21 17:18:36 +01002051 }
2052
Emeric Brun52a91d32017-08-31 14:41:55 +02002053 if (sv->cur_admin & SRV_ADMF_DRAIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +01002054 if (ref->agent.state & CHK_ST_ENABLED)
William Dauchyd3a9a492021-01-25 17:29:03 +01002055 *state = SRV_STATS_STATE_DRAIN_AGENT;
2056 else if (*state == SRV_STATS_STATE_UP_GOING_DOWN)
2057 *state = SRV_STATS_STATE_DRAIN_GOING_DOWN;
William Lallemand74c24fb2016-11-21 17:18:36 +01002058 else
William Dauchyd3a9a492021-01-25 17:29:03 +01002059 *state = SRV_STATS_STATE_DRAIN;
William Lallemand74c24fb2016-11-21 17:18:36 +01002060 }
2061
William Dauchyd3a9a492021-01-25 17:29:03 +01002062 if (*state == SRV_STATS_STATE_UP && !(ref->check.state & CHK_ST_ENABLED)) {
2063 *state = SRV_STATS_STATE_NO_CHECK;
William Lallemand74c24fb2016-11-21 17:18:36 +01002064 }
2065 }
Emeric Brun52a91d32017-08-31 14:41:55 +02002066 else if (sv->cur_state == SRV_ST_STOPPING) {
William Lallemand74c24fb2016-11-21 17:18:36 +01002067 if ((!(sv->check.state & CHK_ST_ENABLED) && !sv->track) ||
2068 (ref->check.health == ref->check.rise + ref->check.fall - 1)) {
William Dauchyd3a9a492021-01-25 17:29:03 +01002069 *state = SRV_STATS_STATE_NOLB;
William Lallemand74c24fb2016-11-21 17:18:36 +01002070 } else {
William Dauchyd3a9a492021-01-25 17:29:03 +01002071 *state = SRV_STATS_STATE_NOLB_GOING_DOWN;
William Lallemand74c24fb2016-11-21 17:18:36 +01002072 }
2073 }
2074 else { /* stopped */
2075 if ((ref->agent.state & CHK_ST_ENABLED) && !ref->agent.health) {
William Dauchyd3a9a492021-01-25 17:29:03 +01002076 *state = SRV_STATS_STATE_DOWN_AGENT;
William Lallemand74c24fb2016-11-21 17:18:36 +01002077 } else if ((ref->check.state & CHK_ST_ENABLED) && !ref->check.health) {
William Dauchyd3a9a492021-01-25 17:29:03 +01002078 *state = SRV_STATS_STATE_DOWN; /* DOWN */
William Lallemand74c24fb2016-11-21 17:18:36 +01002079 } else if ((ref->agent.state & CHK_ST_ENABLED) || (ref->check.state & CHK_ST_ENABLED)) {
William Dauchyd3a9a492021-01-25 17:29:03 +01002080 *state = SRV_STATS_STATE_GOING_UP;
William Lallemand74c24fb2016-11-21 17:18:36 +01002081 } else {
William Dauchyd3a9a492021-01-25 17:29:03 +01002082 *state = SRV_STATS_STATE_DOWN; /* DOWN, unchecked */
William Lallemand74c24fb2016-11-21 17:18:36 +01002083 }
2084 }
William Dauchyd3a9a492021-01-25 17:29:03 +01002085}
William Lallemand74c24fb2016-11-21 17:18:36 +01002086
William Dauchyd3a9a492021-01-25 17:29:03 +01002087/* Fill <stats> with the backend statistics. <stats> is preallocated array of
2088 * length <len>. If <selected_field> is != NULL, only fill this one. The length
2089 * of the array must be at least ST_F_TOTAL_FIELDS. If this length is less than
2090 * this value, or if the selected field is not implemented for servers, the
2091 * function returns 0, otherwise, it returns 1. <flags> can take the value
2092 * STAT_SHLGNDS.
2093 */
2094int stats_fill_sv_stats(struct proxy *px, struct server *sv, int flags,
2095 struct field *stats, int len,
2096 enum stat_field *selected_field)
2097{
2098 enum stat_field current_field = (selected_field != NULL ? *selected_field : 0);
2099 struct server *via = sv->track ? sv->track : sv;
2100 struct server *ref = via;
2101 enum srv_stats_state state = 0;
2102 char str[INET6_ADDRSTRLEN];
2103 struct buffer *out = get_trash_chunk();
2104 char *fld_status;
2105 long long srv_samples_counter;
2106 unsigned int srv_samples_window = TIME_STATS_SAMPLES;
William Lallemand74c24fb2016-11-21 17:18:36 +01002107
William Dauchyd3a9a492021-01-25 17:29:03 +01002108 if (len < ST_F_TOTAL_FIELDS)
2109 return 0;
William Lallemand74c24fb2016-11-21 17:18:36 +01002110
William Dauchyd3a9a492021-01-25 17:29:03 +01002111 chunk_reset(out);
William Lallemand74c24fb2016-11-21 17:18:36 +01002112
William Dauchyd3a9a492021-01-25 17:29:03 +01002113 /* compute state for later use */
2114 if (selected_field == NULL || *selected_field == ST_F_STATUS ||
2115 *selected_field == ST_F_CHECK_RISE || *selected_field == ST_F_CHECK_FALL ||
2116 *selected_field == ST_F_CHECK_HEALTH || *selected_field == ST_F_HANAFAIL) {
2117 /* we have "via" which is the tracked server as described in the configuration,
2118 * and "ref" which is the checked server and the end of the chain.
2119 */
2120 while (ref->track)
2121 ref = ref->track;
2122 stats_fill_sv_stats_computestate(sv, ref, &state);
William Lallemand74c24fb2016-11-21 17:18:36 +01002123 }
2124
William Dauchyd3a9a492021-01-25 17:29:03 +01002125 /* compue time values for later use */
2126 if (selected_field == NULL || *selected_field == ST_F_QTIME ||
2127 *selected_field == ST_F_CTIME || *selected_field == ST_F_RTIME ||
2128 *selected_field == ST_F_TTIME) {
2129 srv_samples_counter = (px->mode == PR_MODE_HTTP) ? sv->counters.p.http.cum_req : sv->counters.cum_lbconn;
2130 if (srv_samples_counter < TIME_STATS_SAMPLES && srv_samples_counter > 0)
2131 srv_samples_window = srv_samples_counter;
William Lallemand74c24fb2016-11-21 17:18:36 +01002132 }
2133
William Dauchyd3a9a492021-01-25 17:29:03 +01002134 for (; current_field < ST_F_TOTAL_FIELDS; current_field++) {
2135 struct field metric = { 0 };
William Lallemand74c24fb2016-11-21 17:18:36 +01002136
William Dauchyd3a9a492021-01-25 17:29:03 +01002137 switch (current_field) {
2138 case ST_F_PXNAME:
2139 metric = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
2140 break;
2141 case ST_F_SVNAME:
2142 metric = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, sv->id);
2143 break;
2144 case ST_F_MODE:
2145 metric = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
Christopher Faulet3888b8c2021-01-27 13:32:25 +01002146 break;
William Dauchyd3a9a492021-01-25 17:29:03 +01002147 case ST_F_QCUR:
Willy Tarreaua0570452021-06-18 09:30:30 +02002148 metric = mkf_u32(0, sv->queue.length);
William Dauchyd3a9a492021-01-25 17:29:03 +01002149 break;
2150 case ST_F_QMAX:
2151 metric = mkf_u32(FN_MAX, sv->counters.nbpend_max);
2152 break;
2153 case ST_F_SCUR:
2154 metric = mkf_u32(0, sv->cur_sess);
2155 break;
2156 case ST_F_SMAX:
2157 metric = mkf_u32(FN_MAX, sv->counters.cur_sess_max);
2158 break;
2159 case ST_F_SLIM:
2160 if (sv->maxconn)
2161 metric = mkf_u32(FO_CONFIG|FN_LIMIT, sv->maxconn);
2162 break;
2163 case ST_F_SRV_ICUR:
2164 metric = mkf_u32(0, sv->curr_idle_conns);
2165 break;
2166 case ST_F_SRV_ILIM:
2167 if (sv->max_idle_conns != -1)
2168 metric = mkf_u32(FO_CONFIG|FN_LIMIT, sv->max_idle_conns);
2169 break;
2170 case ST_F_STOT:
2171 metric = mkf_u64(FN_COUNTER, sv->counters.cum_sess);
2172 break;
2173 case ST_F_BIN:
2174 metric = mkf_u64(FN_COUNTER, sv->counters.bytes_in);
2175 break;
2176 case ST_F_BOUT:
2177 metric = mkf_u64(FN_COUNTER, sv->counters.bytes_out);
2178 break;
2179 case ST_F_DRESP:
2180 metric = mkf_u64(FN_COUNTER, sv->counters.denied_resp);
2181 break;
2182 case ST_F_ECON:
2183 metric = mkf_u64(FN_COUNTER, sv->counters.failed_conns);
2184 break;
2185 case ST_F_ERESP:
2186 metric = mkf_u64(FN_COUNTER, sv->counters.failed_resp);
2187 break;
2188 case ST_F_WRETR:
2189 metric = mkf_u64(FN_COUNTER, sv->counters.retries);
2190 break;
2191 case ST_F_WREDIS:
2192 metric = mkf_u64(FN_COUNTER, sv->counters.redispatches);
2193 break;
2194 case ST_F_WREW:
2195 metric = mkf_u64(FN_COUNTER, sv->counters.failed_rewrites);
2196 break;
2197 case ST_F_EINT:
2198 metric = mkf_u64(FN_COUNTER, sv->counters.internal_errors);
2199 break;
2200 case ST_F_CONNECT:
2201 metric = mkf_u64(FN_COUNTER, sv->counters.connect);
2202 break;
2203 case ST_F_REUSE:
2204 metric = mkf_u64(FN_COUNTER, sv->counters.reuse);
2205 break;
2206 case ST_F_IDLE_CONN_CUR:
2207 metric = mkf_u32(0, sv->curr_idle_nb);
2208 break;
2209 case ST_F_SAFE_CONN_CUR:
2210 metric = mkf_u32(0, sv->curr_safe_nb);
2211 break;
2212 case ST_F_USED_CONN_CUR:
2213 metric = mkf_u32(0, sv->curr_used_conns);
2214 break;
2215 case ST_F_NEED_CONN_EST:
2216 metric = mkf_u32(0, sv->est_need_conns);
2217 break;
2218 case ST_F_STATUS:
2219 fld_status = chunk_newstr(out);
2220 if (sv->cur_admin & SRV_ADMF_RMAINT)
2221 chunk_appendf(out, "MAINT (resolution)");
2222 else if (sv->cur_admin & SRV_ADMF_IMAINT)
2223 chunk_appendf(out, "MAINT (via %s/%s)", via->proxy->id, via->id);
2224 else if (sv->cur_admin & SRV_ADMF_MAINT)
2225 chunk_appendf(out, "MAINT");
2226 else
2227 chunk_appendf(out,
2228 srv_hlt_st[state],
2229 (ref->cur_state != SRV_ST_STOPPED) ? (ref->check.health - ref->check.rise + 1) : (ref->check.health),
2230 (ref->cur_state != SRV_ST_STOPPED) ? (ref->check.fall) : (ref->check.rise));
William Lallemand74c24fb2016-11-21 17:18:36 +01002231
William Dauchyd3a9a492021-01-25 17:29:03 +01002232 metric = mkf_str(FO_STATUS, fld_status);
2233 break;
2234 case ST_F_LASTCHG:
2235 metric = mkf_u32(FN_AGE, now.tv_sec - sv->last_change);
2236 break;
2237 case ST_F_WEIGHT:
2238 metric = mkf_u32(FN_AVG, (sv->cur_eweight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv);
2239 break;
2240 case ST_F_UWEIGHT:
2241 metric = mkf_u32(FN_AVG, sv->uweight);
2242 break;
2243 case ST_F_ACT:
2244 metric = mkf_u32(FO_STATUS, (sv->flags & SRV_F_BACKUP) ? 0 : 1);
2245 break;
2246 case ST_F_BCK:
2247 metric = mkf_u32(FO_STATUS, (sv->flags & SRV_F_BACKUP) ? 1 : 0);
2248 break;
2249 case ST_F_CHKFAIL:
2250 if (sv->check.state & CHK_ST_ENABLED)
2251 metric = mkf_u64(FN_COUNTER, sv->counters.failed_checks);
2252 break;
2253 case ST_F_CHKDOWN:
2254 if (sv->check.state & CHK_ST_ENABLED)
2255 metric = mkf_u64(FN_COUNTER, sv->counters.down_trans);
2256 break;
2257 case ST_F_DOWNTIME:
2258 if (sv->check.state & CHK_ST_ENABLED)
2259 metric = mkf_u32(FN_COUNTER, srv_downtime(sv));
2260 break;
2261 case ST_F_QLIMIT:
2262 if (sv->maxqueue)
2263 metric = mkf_u32(FO_CONFIG|FS_SERVICE, sv->maxqueue);
2264 break;
2265 case ST_F_PID:
Willy Tarreaue8422bf2021-06-15 09:08:18 +02002266 metric = mkf_u32(FO_KEY, 1);
William Dauchyd3a9a492021-01-25 17:29:03 +01002267 break;
2268 case ST_F_IID:
2269 metric = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
2270 break;
2271 case ST_F_SID:
2272 metric = mkf_u32(FO_KEY|FS_SERVICE, sv->puid);
2273 break;
2274 case ST_F_THROTTLE:
2275 if (sv->cur_state == SRV_ST_STARTING && !server_is_draining(sv))
2276 metric = mkf_u32(FN_AVG, server_throttle_rate(sv));
2277 break;
2278 case ST_F_LBTOT:
2279 metric = mkf_u64(FN_COUNTER, sv->counters.cum_lbconn);
2280 break;
2281 case ST_F_TRACKED:
2282 if (sv->track) {
2283 char *fld_track = chunk_newstr(out);
2284 chunk_appendf(out, "%s/%s", sv->track->proxy->id, sv->track->id);
2285 metric = mkf_str(FO_CONFIG|FN_NAME|FS_SERVICE, fld_track);
2286 }
2287 break;
2288 case ST_F_TYPE:
2289 metric = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_SV);
2290 break;
2291 case ST_F_RATE:
2292 metric = mkf_u32(FN_RATE, read_freq_ctr(&sv->sess_per_sec));
2293 break;
2294 case ST_F_RATE_MAX:
2295 metric = mkf_u32(FN_MAX, sv->counters.sps_max);
2296 break;
2297 case ST_F_CHECK_STATUS:
2298 if ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED) {
2299 const char *fld_chksts;
Marcin Deraneka8dbdf32020-05-15 20:02:40 +02002300
William Dauchyd3a9a492021-01-25 17:29:03 +01002301 fld_chksts = chunk_newstr(out);
2302 chunk_strcat(out, "* "); // for check in progress
2303 chunk_strcat(out, get_check_status_info(sv->check.status));
2304 if (!(sv->check.state & CHK_ST_INPROGRESS))
2305 fld_chksts += 2; // skip "* "
Willy Tarreau973a9372021-05-12 17:29:14 +02002306 metric = mkf_str(FN_OUTPUT, fld_chksts);
William Dauchyd3a9a492021-01-25 17:29:03 +01002307 }
2308 break;
2309 case ST_F_CHECK_CODE:
2310 if ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED &&
2311 sv->check.status >= HCHK_STATUS_L57DATA)
2312 metric = mkf_u32(FN_OUTPUT, sv->check.code);
2313 break;
2314 case ST_F_CHECK_DURATION:
2315 if ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED &&
2316 sv->check.status >= HCHK_STATUS_CHECKED)
2317 metric = mkf_u64(FN_DURATION, sv->check.duration);
2318 break;
2319 case ST_F_CHECK_DESC:
2320 if ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED)
2321 metric = mkf_str(FN_OUTPUT, get_check_status_description(sv->check.status));
2322 break;
2323 case ST_F_LAST_CHK:
2324 if ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED)
2325 metric = mkf_str(FN_OUTPUT, sv->check.desc);
2326 break;
2327 case ST_F_CHECK_RISE:
2328 if ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED)
2329 metric = mkf_u32(FO_CONFIG|FS_SERVICE, ref->check.rise);
2330 break;
2331 case ST_F_CHECK_FALL:
2332 if ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED)
2333 metric = mkf_u32(FO_CONFIG|FS_SERVICE, ref->check.fall);
2334 break;
2335 case ST_F_CHECK_HEALTH:
2336 if ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED)
2337 metric = mkf_u32(FO_CONFIG|FS_SERVICE, ref->check.health);
2338 break;
Christopher Faulet59bab612021-07-22 08:04:38 +02002339 case ST_F_AGENT_STATUS:
2340 if ((sv->agent.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED) {
2341 const char *fld_chksts;
2342
2343 fld_chksts = chunk_newstr(out);
2344 chunk_strcat(out, "* "); // for check in progress
2345 chunk_strcat(out, get_check_status_info(sv->agent.status));
2346 if (!(sv->agent.state & CHK_ST_INPROGRESS))
2347 fld_chksts += 2; // skip "* "
2348 metric = mkf_str(FN_OUTPUT, fld_chksts);
2349 }
2350 break;
2351 case ST_F_AGENT_CODE:
2352 if ((sv->agent.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED &&
2353 (sv->agent.status >= HCHK_STATUS_L57DATA))
2354 metric = mkf_u32(FN_OUTPUT, sv->agent.code);
2355 break;
2356 case ST_F_AGENT_DURATION:
2357 if ((sv->agent.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED)
2358 metric = mkf_u64(FN_DURATION, sv->agent.duration);
2359 break;
2360 case ST_F_AGENT_DESC:
2361 if ((sv->agent.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED)
2362 metric = mkf_str(FN_OUTPUT, get_check_status_description(sv->agent.status));
2363 break;
2364 case ST_F_LAST_AGT:
2365 if ((sv->agent.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED)
2366 metric = mkf_str(FN_OUTPUT, sv->agent.desc);
2367 break;
2368 case ST_F_AGENT_RISE:
2369 if ((sv->agent.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED)
2370 metric = mkf_u32(FO_CONFIG|FS_SERVICE, sv->agent.rise);
2371 break;
2372 case ST_F_AGENT_FALL:
2373 if ((sv->agent.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED)
2374 metric = mkf_u32(FO_CONFIG|FS_SERVICE, sv->agent.fall);
2375 break;
2376 case ST_F_AGENT_HEALTH:
2377 if ((sv->agent.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED)
2378 metric = mkf_u32(FO_CONFIG|FS_SERVICE, sv->agent.health);
2379 break;
William Dauchyd3a9a492021-01-25 17:29:03 +01002380 case ST_F_REQ_TOT:
2381 if (px->mode == PR_MODE_HTTP)
2382 metric = mkf_u64(FN_COUNTER, sv->counters.p.http.cum_req);
2383 break;
2384 case ST_F_HRSP_1XX:
2385 if (px->mode == PR_MODE_HTTP)
2386 metric = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[1]);
2387 break;
2388 case ST_F_HRSP_2XX:
2389 if (px->mode == PR_MODE_HTTP)
2390 metric = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[2]);
2391 break;
2392 case ST_F_HRSP_3XX:
2393 if (px->mode == PR_MODE_HTTP)
2394 metric = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[3]);
2395 break;
2396 case ST_F_HRSP_4XX:
2397 if (px->mode == PR_MODE_HTTP)
2398 metric = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[4]);
2399 break;
2400 case ST_F_HRSP_5XX:
2401 if (px->mode == PR_MODE_HTTP)
2402 metric = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[5]);
2403 break;
2404 case ST_F_HRSP_OTHER:
2405 if (px->mode == PR_MODE_HTTP)
2406 metric = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[0]);
2407 break;
2408 case ST_F_HANAFAIL:
2409 if (ref->observe)
2410 metric = mkf_u64(FN_COUNTER, sv->counters.failed_hana);
2411 break;
2412 case ST_F_CLI_ABRT:
2413 metric = mkf_u64(FN_COUNTER, sv->counters.cli_aborts);
2414 break;
2415 case ST_F_SRV_ABRT:
2416 metric = mkf_u64(FN_COUNTER, sv->counters.srv_aborts);
2417 break;
2418 case ST_F_LASTSESS:
2419 metric = mkf_s32(FN_AGE, srv_lastsession(sv));
2420 break;
2421 case ST_F_QTIME:
2422 metric = mkf_u32(FN_AVG, swrate_avg(sv->counters.q_time, srv_samples_window));
2423 break;
2424 case ST_F_CTIME:
2425 metric = mkf_u32(FN_AVG, swrate_avg(sv->counters.c_time, srv_samples_window));
2426 break;
2427 case ST_F_RTIME:
2428 metric = mkf_u32(FN_AVG, swrate_avg(sv->counters.d_time, srv_samples_window));
2429 break;
2430 case ST_F_TTIME:
2431 metric = mkf_u32(FN_AVG, swrate_avg(sv->counters.t_time, srv_samples_window));
2432 break;
2433 case ST_F_QT_MAX:
2434 metric = mkf_u32(FN_MAX, sv->counters.qtime_max);
2435 break;
2436 case ST_F_CT_MAX:
2437 metric = mkf_u32(FN_MAX, sv->counters.ctime_max);
2438 break;
2439 case ST_F_RT_MAX:
2440 metric = mkf_u32(FN_MAX, sv->counters.dtime_max);
2441 break;
2442 case ST_F_TT_MAX:
2443 metric = mkf_u32(FN_MAX, sv->counters.ttime_max);
2444 break;
2445 case ST_F_ADDR:
2446 if (flags & STAT_SHLGNDS) {
2447 switch (addr_to_str(&sv->addr, str, sizeof(str))) {
2448 case AF_INET:
2449 metric = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
2450 chunk_appendf(out, "%s:%d", str, sv->svc_port);
2451 break;
2452 case AF_INET6:
2453 metric = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
2454 chunk_appendf(out, "[%s]:%d", str, sv->svc_port);
2455 break;
2456 case AF_UNIX:
2457 metric = mkf_str(FO_CONFIG|FS_SERVICE, "unix");
2458 break;
2459 case -1:
2460 metric = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
2461 chunk_strcat(out, strerror(errno));
2462 break;
2463 default: /* address family not supported */
2464 break;
2465 }
2466 }
2467 break;
2468 case ST_F_COOKIE:
2469 if (flags & STAT_SHLGNDS && sv->cookie)
2470 metric = mkf_str(FO_CONFIG|FN_NAME|FS_SERVICE, sv->cookie);
2471 break;
2472 default:
2473 /* not used for servers. If a specific metric
2474 * is requested, return an error. Otherwise continue.
2475 */
2476 if (selected_field != NULL)
2477 return 0;
2478 continue;
William Lallemand74c24fb2016-11-21 17:18:36 +01002479 }
William Dauchyd3a9a492021-01-25 17:29:03 +01002480 stats[current_field] = metric;
2481 if (selected_field != NULL)
2482 break;
William Lallemand74c24fb2016-11-21 17:18:36 +01002483 }
William Lallemand74c24fb2016-11-21 17:18:36 +01002484 return 1;
2485}
2486
2487/* Dumps a line for server <sv> and proxy <px> to the trash and uses the state
Christopher Faulet908628c2022-03-25 16:43:49 +01002488 * from conn-stream <cs>, and server state <state>. The caller is
Willy Tarreau43241ff2019-10-09 11:27:51 +02002489 * responsible for clearing the trash if needed. Returns non-zero if it emits
2490 * anything, zero otherwise.
William Lallemand74c24fb2016-11-21 17:18:36 +01002491 */
Christopher Faulet908628c2022-03-25 16:43:49 +01002492static int stats_dump_sv_stats(struct conn_stream *cs, struct proxy *px, struct server *sv)
William Lallemand74c24fb2016-11-21 17:18:36 +01002493{
Christopher Faulet908628c2022-03-25 16:43:49 +01002494 struct appctx *appctx = __cs_appctx(cs);
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02002495 struct stats_module *mod;
2496 struct field *stats = stat_l[STATS_DOMAIN_PROXY];
2497 size_t stats_count = ST_F_TOTAL_FIELDS;
2498
2499 memset(stats, 0, sizeof(struct field) * stat_count[STATS_DOMAIN_PROXY]);
William Lallemand74c24fb2016-11-21 17:18:36 +01002500
William Dauchyd3a9a492021-01-25 17:29:03 +01002501 if (!stats_fill_sv_stats(px, sv, appctx->ctx.stats.flags, stats,
2502 ST_F_TOTAL_FIELDS, NULL))
William Lallemand74c24fb2016-11-21 17:18:36 +01002503 return 0;
2504
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02002505 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
2506 void *counters;
2507
2508 if (stats_get_domain(mod->domain_flags) != STATS_DOMAIN_PROXY)
2509 continue;
2510
2511 if (!(stats_px_get_cap(mod->domain_flags) & STATS_PX_CAP_SRV)) {
2512 stats_count += mod->stats_count;
2513 continue;
2514 }
2515
2516 counters = EXTRA_COUNTERS_GET(sv->extra_counters, mod);
2517 mod->fill_stats(counters, stats + stats_count);
2518 stats_count += mod->stats_count;
2519 }
2520
2521 return stats_dump_one_line(stats, stats_count, appctx);
William Lallemand74c24fb2016-11-21 17:18:36 +01002522}
2523
William Dauchyda3b4662021-01-25 17:29:01 +01002524/* Helper to compute srv values for a given backend
William Lallemand74c24fb2016-11-21 17:18:36 +01002525 */
William Dauchyda3b4662021-01-25 17:29:01 +01002526static void stats_fill_be_stats_computesrv(struct proxy *px, int *nbup, int *nbsrv, int *totuw)
William Lallemand74c24fb2016-11-21 17:18:36 +01002527{
William Dauchyda3b4662021-01-25 17:29:01 +01002528 int nbup_tmp, nbsrv_tmp, totuw_tmp;
Willy Tarreau2fbe6942020-10-23 18:02:54 +02002529 const struct server *srv;
William Lallemand74c24fb2016-11-21 17:18:36 +01002530
William Dauchyda3b4662021-01-25 17:29:01 +01002531 nbup_tmp = nbsrv_tmp = totuw_tmp = 0;
Willy Tarreaubd715102020-10-23 22:44:30 +02002532 for (srv = px->srv; srv; srv = srv->next) {
2533 if (srv->cur_state != SRV_ST_STOPPED) {
William Dauchyda3b4662021-01-25 17:29:01 +01002534 nbup_tmp++;
Willy Tarreaubd715102020-10-23 22:44:30 +02002535 if (srv_currently_usable(srv) &&
2536 (!px->srv_act ^ !(srv->flags & SRV_F_BACKUP)))
William Dauchyda3b4662021-01-25 17:29:01 +01002537 totuw_tmp += srv->uweight;
Willy Tarreau2fbe6942020-10-23 18:02:54 +02002538 }
William Dauchyda3b4662021-01-25 17:29:01 +01002539 nbsrv_tmp++;
Willy Tarreau2fbe6942020-10-23 18:02:54 +02002540 }
2541
Willy Tarreaubd715102020-10-23 22:44:30 +02002542 HA_RWLOCK_RDLOCK(LBPRM_LOCK, &px->lbprm.lock);
2543 if (!px->srv_act && px->lbprm.fbck)
William Dauchyda3b4662021-01-25 17:29:01 +01002544 totuw_tmp = px->lbprm.fbck->uweight;
Willy Tarreaubd715102020-10-23 22:44:30 +02002545 HA_RWLOCK_RDUNLOCK(LBPRM_LOCK, &px->lbprm.lock);
2546
William Dauchyda3b4662021-01-25 17:29:01 +01002547 /* use tmp variable then assign result to make gcc happy */
2548 *nbup = nbup_tmp;
2549 *nbsrv = nbsrv_tmp;
2550 *totuw = totuw_tmp;
2551}
Willy Tarreau2fbe6942020-10-23 18:02:54 +02002552
William Dauchyda3b4662021-01-25 17:29:01 +01002553/* Fill <stats> with the backend statistics. <stats> is preallocated array of
2554 * length <len>. If <selected_field> is != NULL, only fill this one. The length
2555 * of the array must be at least ST_F_TOTAL_FIELDS. If this length is less than
2556 * this value, or if the selected field is not implemented for backends, the
2557 * function returns 0, otherwise, it returns 1. <flags> can take the value
2558 * STAT_SHLGNDS.
2559 */
2560int stats_fill_be_stats(struct proxy *px, int flags, struct field *stats, int len,
2561 enum stat_field *selected_field)
2562{
2563 enum stat_field current_field = (selected_field != NULL ? *selected_field : 0);
2564 long long be_samples_counter;
2565 unsigned int be_samples_window = TIME_STATS_SAMPLES;
2566 struct buffer *out = get_trash_chunk();
2567 int nbup, nbsrv, totuw;
2568 char *fld;
William Lallemand74c24fb2016-11-21 17:18:36 +01002569
William Dauchyda3b4662021-01-25 17:29:01 +01002570 if (len < ST_F_TOTAL_FIELDS)
2571 return 0;
William Lallemand74c24fb2016-11-21 17:18:36 +01002572
William Dauchyda3b4662021-01-25 17:29:01 +01002573 nbup = nbsrv = totuw = 0;
2574 /* some srv values compute for later if we either select all fields or
2575 * need them for one of the mentioned ones */
2576 if (selected_field == NULL || *selected_field == ST_F_STATUS ||
2577 *selected_field == ST_F_UWEIGHT)
2578 stats_fill_be_stats_computesrv(px, &nbup, &nbsrv, &totuw);
William Lallemand74c24fb2016-11-21 17:18:36 +01002579
William Dauchyda3b4662021-01-25 17:29:01 +01002580 /* same here but specific to time fields */
2581 if (selected_field == NULL || *selected_field == ST_F_QTIME ||
2582 *selected_field == ST_F_CTIME || *selected_field == ST_F_RTIME ||
2583 *selected_field == ST_F_TTIME) {
2584 be_samples_counter = (px->mode == PR_MODE_HTTP) ? px->be_counters.p.http.cum_req : px->be_counters.cum_lbconn;
2585 if (be_samples_counter < TIME_STATS_SAMPLES && be_samples_counter > 0)
2586 be_samples_window = be_samples_counter;
William Lallemand74c24fb2016-11-21 17:18:36 +01002587 }
2588
William Dauchyda3b4662021-01-25 17:29:01 +01002589 for (; current_field < ST_F_TOTAL_FIELDS; current_field++) {
2590 struct field metric = { 0 };
William Lallemand74c24fb2016-11-21 17:18:36 +01002591
William Dauchyda3b4662021-01-25 17:29:01 +01002592 switch (current_field) {
2593 case ST_F_PXNAME:
2594 metric = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
2595 break;
2596 case ST_F_SVNAME:
2597 metric = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, "BACKEND");
2598 break;
2599 case ST_F_MODE:
2600 metric = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
2601 break;
2602 case ST_F_QCUR:
Willy Tarreau7f3c1df2021-06-18 09:22:21 +02002603 metric = mkf_u32(0, px->queue.length);
William Dauchyda3b4662021-01-25 17:29:01 +01002604 break;
2605 case ST_F_QMAX:
2606 metric = mkf_u32(FN_MAX, px->be_counters.nbpend_max);
2607 break;
2608 case ST_F_SCUR:
2609 metric = mkf_u32(0, px->beconn);
2610 break;
2611 case ST_F_SMAX:
2612 metric = mkf_u32(FN_MAX, px->be_counters.conn_max);
2613 break;
2614 case ST_F_SLIM:
2615 metric = mkf_u32(FO_CONFIG|FN_LIMIT, px->fullconn);
2616 break;
2617 case ST_F_STOT:
2618 metric = mkf_u64(FN_COUNTER, px->be_counters.cum_conn);
2619 break;
2620 case ST_F_BIN:
2621 metric = mkf_u64(FN_COUNTER, px->be_counters.bytes_in);
2622 break;
2623 case ST_F_BOUT:
2624 metric = mkf_u64(FN_COUNTER, px->be_counters.bytes_out);
2625 break;
2626 case ST_F_DREQ:
2627 metric = mkf_u64(FN_COUNTER, px->be_counters.denied_req);
2628 break;
2629 case ST_F_DRESP:
2630 metric = mkf_u64(FN_COUNTER, px->be_counters.denied_resp);
2631 break;
2632 case ST_F_ECON:
2633 metric = mkf_u64(FN_COUNTER, px->be_counters.failed_conns);
2634 break;
2635 case ST_F_ERESP:
2636 metric = mkf_u64(FN_COUNTER, px->be_counters.failed_resp);
2637 break;
2638 case ST_F_WRETR:
2639 metric = mkf_u64(FN_COUNTER, px->be_counters.retries);
2640 break;
2641 case ST_F_WREDIS:
2642 metric = mkf_u64(FN_COUNTER, px->be_counters.redispatches);
2643 break;
2644 case ST_F_WREW:
2645 metric = mkf_u64(FN_COUNTER, px->be_counters.failed_rewrites);
2646 break;
2647 case ST_F_EINT:
2648 metric = mkf_u64(FN_COUNTER, px->be_counters.internal_errors);
2649 break;
2650 case ST_F_CONNECT:
2651 metric = mkf_u64(FN_COUNTER, px->be_counters.connect);
2652 break;
2653 case ST_F_REUSE:
2654 metric = mkf_u64(FN_COUNTER, px->be_counters.reuse);
2655 break;
2656 case ST_F_STATUS:
2657 fld = chunk_newstr(out);
2658 chunk_appendf(out, "%s", (px->lbprm.tot_weight > 0 || !px->srv) ? "UP" : "DOWN");
2659 if (flags & (STAT_HIDE_MAINT|STAT_HIDE_DOWN))
2660 chunk_appendf(out, " (%d/%d)", nbup, nbsrv);
2661 metric = mkf_str(FO_STATUS, fld);
2662 break;
William Dauchy42d7c402021-11-07 10:18:47 +01002663 case ST_F_AGG_SRV_CHECK_STATUS:
2664 metric = mkf_u32(FN_GAUGE, 0);
2665 break;
William Dauchyda3b4662021-01-25 17:29:01 +01002666 case ST_F_WEIGHT:
2667 metric = mkf_u32(FN_AVG, (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv);
2668 break;
2669 case ST_F_UWEIGHT:
2670 metric = mkf_u32(FN_AVG, totuw);
2671 break;
2672 case ST_F_ACT:
2673 metric = mkf_u32(0, px->srv_act);
2674 break;
2675 case ST_F_BCK:
2676 metric = mkf_u32(0, px->srv_bck);
2677 break;
2678 case ST_F_CHKDOWN:
2679 metric = mkf_u64(FN_COUNTER, px->down_trans);
2680 break;
2681 case ST_F_LASTCHG:
2682 metric = mkf_u32(FN_AGE, now.tv_sec - px->last_change);
2683 break;
2684 case ST_F_DOWNTIME:
2685 if (px->srv)
2686 metric = mkf_u32(FN_COUNTER, be_downtime(px));
2687 break;
2688 case ST_F_PID:
Willy Tarreaue8422bf2021-06-15 09:08:18 +02002689 metric = mkf_u32(FO_KEY, 1);
William Dauchyda3b4662021-01-25 17:29:01 +01002690 break;
2691 case ST_F_IID:
2692 metric = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
2693 break;
2694 case ST_F_SID:
2695 metric = mkf_u32(FO_KEY|FS_SERVICE, 0);
2696 break;
2697 case ST_F_LBTOT:
2698 metric = mkf_u64(FN_COUNTER, px->be_counters.cum_lbconn);
2699 break;
2700 case ST_F_TYPE:
2701 metric = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_BE);
2702 break;
2703 case ST_F_RATE:
2704 metric = mkf_u32(0, read_freq_ctr(&px->be_sess_per_sec));
2705 break;
2706 case ST_F_RATE_MAX:
2707 metric = mkf_u32(0, px->be_counters.sps_max);
2708 break;
2709 case ST_F_COOKIE:
2710 if (flags & STAT_SHLGNDS && px->cookie_name)
2711 metric = mkf_str(FO_CONFIG|FN_NAME|FS_SERVICE, px->cookie_name);
2712 break;
2713 case ST_F_ALGO:
2714 if (flags & STAT_SHLGNDS)
2715 metric = mkf_str(FO_CONFIG|FS_SERVICE, backend_lb_algo_str(px->lbprm.algo & BE_LB_ALGO));
2716 break;
2717 case ST_F_REQ_TOT:
2718 if (px->mode == PR_MODE_HTTP)
2719 metric = mkf_u64(FN_COUNTER, px->be_counters.p.http.cum_req);
2720 break;
2721 case ST_F_HRSP_1XX:
2722 if (px->mode == PR_MODE_HTTP)
2723 metric = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[1]);
2724 break;
2725 case ST_F_HRSP_2XX:
2726 if (px->mode == PR_MODE_HTTP)
2727 metric = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[2]);
2728 break;
2729 case ST_F_HRSP_3XX:
2730 if (px->mode == PR_MODE_HTTP)
2731 metric = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[3]);
2732 break;
2733 case ST_F_HRSP_4XX:
2734 if (px->mode == PR_MODE_HTTP)
2735 metric = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[4]);
2736 break;
2737 case ST_F_HRSP_5XX:
2738 if (px->mode == PR_MODE_HTTP)
2739 metric = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[5]);
2740 break;
2741 case ST_F_HRSP_OTHER:
2742 if (px->mode == PR_MODE_HTTP)
2743 metric = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[0]);
2744 break;
2745 case ST_F_CACHE_LOOKUPS:
2746 if (px->mode == PR_MODE_HTTP)
2747 metric = mkf_u64(FN_COUNTER, px->be_counters.p.http.cache_lookups);
2748 break;
2749 case ST_F_CACHE_HITS:
2750 if (px->mode == PR_MODE_HTTP)
2751 metric = mkf_u64(FN_COUNTER, px->be_counters.p.http.cache_hits);
2752 break;
2753 case ST_F_CLI_ABRT:
2754 metric = mkf_u64(FN_COUNTER, px->be_counters.cli_aborts);
2755 break;
2756 case ST_F_SRV_ABRT:
2757 metric = mkf_u64(FN_COUNTER, px->be_counters.srv_aborts);
2758 break;
2759 case ST_F_COMP_IN:
2760 metric = mkf_u64(FN_COUNTER, px->be_counters.comp_in);
2761 break;
2762 case ST_F_COMP_OUT:
2763 metric = mkf_u64(FN_COUNTER, px->be_counters.comp_out);
2764 break;
2765 case ST_F_COMP_BYP:
2766 metric = mkf_u64(FN_COUNTER, px->be_counters.comp_byp);
2767 break;
2768 case ST_F_COMP_RSP:
2769 metric = mkf_u64(FN_COUNTER, px->be_counters.p.http.comp_rsp);
2770 break;
2771 case ST_F_LASTSESS:
2772 metric = mkf_s32(FN_AGE, be_lastsession(px));
2773 break;
2774 case ST_F_QTIME:
2775 metric = mkf_u32(FN_AVG, swrate_avg(px->be_counters.q_time, be_samples_window));
2776 break;
2777 case ST_F_CTIME:
2778 metric = mkf_u32(FN_AVG, swrate_avg(px->be_counters.c_time, be_samples_window));
2779 break;
2780 case ST_F_RTIME:
2781 metric = mkf_u32(FN_AVG, swrate_avg(px->be_counters.d_time, be_samples_window));
2782 break;
2783 case ST_F_TTIME:
2784 metric = mkf_u32(FN_AVG, swrate_avg(px->be_counters.t_time, be_samples_window));
2785 break;
2786 case ST_F_QT_MAX:
2787 metric = mkf_u32(FN_MAX, px->be_counters.qtime_max);
2788 break;
2789 case ST_F_CT_MAX:
2790 metric = mkf_u32(FN_MAX, px->be_counters.ctime_max);
2791 break;
2792 case ST_F_RT_MAX:
2793 metric = mkf_u32(FN_MAX, px->be_counters.dtime_max);
2794 break;
2795 case ST_F_TT_MAX:
2796 metric = mkf_u32(FN_MAX, px->be_counters.ttime_max);
2797 break;
2798 default:
2799 /* not used for backends. If a specific metric
2800 * is requested, return an error. Otherwise continue.
2801 */
2802 if (selected_field != NULL)
2803 return 0;
2804 continue;
2805 }
2806 stats[current_field] = metric;
2807 if (selected_field != NULL)
2808 break;
2809 }
William Lallemand74c24fb2016-11-21 17:18:36 +01002810 return 1;
2811}
2812
2813/* Dumps a line for backend <px> to the trash for and uses the state from stream
Willy Tarreau43241ff2019-10-09 11:27:51 +02002814 * interface <si>. The caller is responsible for clearing the trash if needed.
2815 * Returns non-zero if it emits anything, zero otherwise.
William Lallemand74c24fb2016-11-21 17:18:36 +01002816 */
Christopher Faulet908628c2022-03-25 16:43:49 +01002817static int stats_dump_be_stats(struct conn_stream *cs, struct proxy *px)
William Lallemand74c24fb2016-11-21 17:18:36 +01002818{
Christopher Faulet908628c2022-03-25 16:43:49 +01002819 struct appctx *appctx = __cs_appctx(cs);
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02002820 struct field *stats = stat_l[STATS_DOMAIN_PROXY];
2821 struct stats_module *mod;
2822 size_t stats_count = ST_F_TOTAL_FIELDS;
William Lallemand74c24fb2016-11-21 17:18:36 +01002823
2824 if (!(px->cap & PR_CAP_BE))
2825 return 0;
2826
2827 if ((appctx->ctx.stats.flags & STAT_BOUND) && !(appctx->ctx.stats.type & (1 << STATS_TYPE_BE)))
2828 return 0;
2829
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02002830 memset(stats, 0, sizeof(struct field) * stat_count[STATS_DOMAIN_PROXY]);
2831
William Dauchyda3b4662021-01-25 17:29:01 +01002832 if (!stats_fill_be_stats(px, appctx->ctx.stats.flags, stats, ST_F_TOTAL_FIELDS, NULL))
William Lallemand74c24fb2016-11-21 17:18:36 +01002833 return 0;
2834
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02002835 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
2836 struct extra_counters *counters;
2837
2838 if (stats_get_domain(mod->domain_flags) != STATS_DOMAIN_PROXY)
2839 continue;
2840
2841 if (!(stats_px_get_cap(mod->domain_flags) & STATS_PX_CAP_BE)) {
2842 stats_count += mod->stats_count;
2843 continue;
2844 }
2845
2846 counters = EXTRA_COUNTERS_GET(px->extra_counters_be, mod);
2847 mod->fill_stats(counters, stats + stats_count);
2848 stats_count += mod->stats_count;
2849 }
2850
2851 return stats_dump_one_line(stats, stats_count, appctx);
William Lallemand74c24fb2016-11-21 17:18:36 +01002852}
2853
2854/* Dumps the HTML table header for proxy <px> to the trash for and uses the state from
Christopher Faulet908628c2022-03-25 16:43:49 +01002855 * conn-stream <cs> and per-uri parameters <uri>. The caller is responsible
William Lallemand74c24fb2016-11-21 17:18:36 +01002856 * for clearing the trash if needed.
2857 */
Christopher Faulet908628c2022-03-25 16:43:49 +01002858static void stats_dump_html_px_hdr(struct conn_stream *cs, struct proxy *px)
William Lallemand74c24fb2016-11-21 17:18:36 +01002859{
Christopher Faulet908628c2022-03-25 16:43:49 +01002860 struct appctx *appctx = __cs_appctx(cs);
William Lallemand74c24fb2016-11-21 17:18:36 +01002861 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02002862 struct stats_module *mod;
2863 int stats_module_len = 0;
William Lallemand74c24fb2016-11-21 17:18:36 +01002864
2865 if (px->cap & PR_CAP_BE && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
2866 /* A form to enable/disable this proxy servers */
2867
2868 /* scope_txt = search pattern + search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
2869 scope_txt[0] = 0;
2870 if (appctx->ctx.stats.scope_len) {
Christopher Faulet908628c2022-03-25 16:43:49 +01002871 const char *scope_ptr = stats_scope_ptr(appctx, cs);
Christopher Fauleted7a0662019-01-14 11:07:34 +01002872
William Lallemand74c24fb2016-11-21 17:18:36 +01002873 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Christopher Fauleted7a0662019-01-14 11:07:34 +01002874 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), scope_ptr, appctx->ctx.stats.scope_len);
William Lallemand74c24fb2016-11-21 17:18:36 +01002875 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
2876 }
2877
2878 chunk_appendf(&trash,
2879 "<form method=\"post\">");
2880 }
2881
2882 /* print a new table */
2883 chunk_appendf(&trash,
2884 "<table class=\"tbl\" width=\"100%%\">\n"
2885 "<tr class=\"titre\">"
2886 "<th class=\"pxname\" width=\"10%%\">");
2887
2888 chunk_appendf(&trash,
2889 "<a name=\"%s\"></a>%s"
2890 "<a class=px href=\"#%s\">%s</a>",
2891 px->id,
Willy Tarreau676c29e2019-10-09 10:50:01 +02002892 (appctx->ctx.stats.flags & STAT_SHLGNDS) ? "<u>":"",
William Lallemand74c24fb2016-11-21 17:18:36 +01002893 px->id, px->id);
2894
Willy Tarreau676c29e2019-10-09 10:50:01 +02002895 if (appctx->ctx.stats.flags & STAT_SHLGNDS) {
William Lallemand74c24fb2016-11-21 17:18:36 +01002896 /* cap, mode, id */
2897 chunk_appendf(&trash, "<div class=tips>cap: %s, mode: %s, id: %d",
2898 proxy_cap_str(px->cap), proxy_mode_str(px->mode),
2899 px->uuid);
2900 chunk_appendf(&trash, "</div>");
2901 }
2902
2903 chunk_appendf(&trash,
2904 "%s</th>"
2905 "<th class=\"%s\" width=\"90%%\">%s</th>"
2906 "</tr>\n"
2907 "</table>\n"
2908 "<table class=\"tbl\" width=\"100%%\">\n"
2909 "<tr class=\"titre\">",
Willy Tarreau676c29e2019-10-09 10:50:01 +02002910 (appctx->ctx.stats.flags & STAT_SHLGNDS) ? "</u>":"",
William Lallemand74c24fb2016-11-21 17:18:36 +01002911 px->desc ? "desc" : "empty", px->desc ? px->desc : "");
2912
Christopher Fauletbc271ec2019-12-02 11:29:04 +01002913 if (appctx->ctx.stats.flags & STAT_ADMIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +01002914 /* Column heading for Enable or Disable server */
Christopher Fauletbc271ec2019-12-02 11:29:04 +01002915 if ((px->cap & PR_CAP_BE) && px->srv)
2916 chunk_appendf(&trash,
2917 "<th rowspan=2 width=1><input type=\"checkbox\" "
2918 "onclick=\"for(c in document.getElementsByClassName('%s-checkbox')) "
2919 "document.getElementsByClassName('%s-checkbox').item(c).checked = this.checked\"></th>",
2920 px->id,
2921 px->id);
2922 else
2923 chunk_appendf(&trash, "<th rowspan=2></th>");
William Lallemand74c24fb2016-11-21 17:18:36 +01002924 }
2925
2926 chunk_appendf(&trash,
2927 "<th rowspan=2></th>"
2928 "<th colspan=3>Queue</th>"
2929 "<th colspan=3>Session rate</th><th colspan=6>Sessions</th>"
2930 "<th colspan=2>Bytes</th><th colspan=2>Denied</th>"
2931 "<th colspan=3>Errors</th><th colspan=2>Warnings</th>"
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02002932 "<th colspan=9>Server</th>");
2933
2934 if (appctx->ctx.stats.flags & STAT_SHMODULES) {
2935 // calculate the count of module for colspan attribute
2936 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
2937 ++stats_module_len;
2938 }
2939 chunk_appendf(&trash, "<th colspan=%d>Extra modules</th>",
2940 stats_module_len);
2941 }
2942
2943 chunk_appendf(&trash,
William Lallemand74c24fb2016-11-21 17:18:36 +01002944 "</tr>\n"
2945 "<tr class=\"titre\">"
2946 "<th>Cur</th><th>Max</th><th>Limit</th>"
2947 "<th>Cur</th><th>Max</th><th>Limit</th><th>Cur</th><th>Max</th>"
2948 "<th>Limit</th><th>Total</th><th>LbTot</th><th>Last</th><th>In</th><th>Out</th>"
2949 "<th>Req</th><th>Resp</th><th>Req</th><th>Conn</th>"
2950 "<th>Resp</th><th>Retr</th><th>Redis</th>"
2951 "<th>Status</th><th>LastChk</th><th>Wght</th><th>Act</th>"
2952 "<th>Bck</th><th>Chk</th><th>Dwn</th><th>Dwntme</th>"
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02002953 "<th>Thrtle</th>\n");
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02002954
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02002955 if (appctx->ctx.stats.flags & STAT_SHMODULES) {
2956 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
2957 chunk_appendf(&trash, "<th>%s</th>", mod->name);
2958 }
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02002959 }
2960
2961 chunk_appendf(&trash, "</tr>");
William Lallemand74c24fb2016-11-21 17:18:36 +01002962}
2963
2964/* Dumps the HTML table trailer for proxy <px> to the trash for and uses the state from
Christopher Faulet908628c2022-03-25 16:43:49 +01002965 * conn_stream <cs>. The caller is responsible for clearing the trash if needed.
William Lallemand74c24fb2016-11-21 17:18:36 +01002966 */
Christopher Faulet908628c2022-03-25 16:43:49 +01002967static void stats_dump_html_px_end(struct conn_stream *cs, struct proxy *px)
William Lallemand74c24fb2016-11-21 17:18:36 +01002968{
Christopher Faulet908628c2022-03-25 16:43:49 +01002969 struct appctx *appctx = __cs_appctx(cs);
Christopher Faulet693b23b2022-02-28 09:09:05 +01002970
William Lallemand74c24fb2016-11-21 17:18:36 +01002971 chunk_appendf(&trash, "</table>");
2972
2973 if ((px->cap & PR_CAP_BE) && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
2974 /* close the form used to enable/disable this proxy servers */
2975 chunk_appendf(&trash,
2976 "Choose the action to perform on the checked servers : "
2977 "<select name=action>"
2978 "<option value=\"\"></option>"
2979 "<option value=\"ready\">Set state to READY</option>"
2980 "<option value=\"drain\">Set state to DRAIN</option>"
2981 "<option value=\"maint\">Set state to MAINT</option>"
2982 "<option value=\"dhlth\">Health: disable checks</option>"
2983 "<option value=\"ehlth\">Health: enable checks</option>"
2984 "<option value=\"hrunn\">Health: force UP</option>"
2985 "<option value=\"hnolb\">Health: force NOLB</option>"
2986 "<option value=\"hdown\">Health: force DOWN</option>"
2987 "<option value=\"dagent\">Agent: disable checks</option>"
2988 "<option value=\"eagent\">Agent: enable checks</option>"
2989 "<option value=\"arunn\">Agent: force UP</option>"
2990 "<option value=\"adown\">Agent: force DOWN</option>"
2991 "<option value=\"shutdown\">Kill Sessions</option>"
2992 "</select>"
2993 "<input type=\"hidden\" name=\"b\" value=\"#%d\">"
2994 "&nbsp;<input type=\"submit\" value=\"Apply\">"
2995 "</form>",
2996 px->uuid);
2997 }
2998
2999 chunk_appendf(&trash, "<p>\n");
3000}
3001
3002/*
Christopher Faulet908628c2022-03-25 16:43:49 +01003003 * Dumps statistics for a proxy. The output is sent to the conn-stream's
William Lallemand74c24fb2016-11-21 17:18:36 +01003004 * input buffer. Returns 0 if it had to stop dumping data because of lack of
3005 * buffer space, or non-zero if everything completed. This function is used
3006 * both by the CLI and the HTTP entry points, and is able to dump the output
3007 * in HTML or CSV formats. If the later, <uri> must be NULL.
3008 */
Christopher Faulet908628c2022-03-25 16:43:49 +01003009int stats_dump_proxy_to_buffer(struct conn_stream *cs, struct htx *htx,
Christopher Fauletef779222018-10-31 08:47:01 +01003010 struct proxy *px, struct uri_auth *uri)
William Lallemand74c24fb2016-11-21 17:18:36 +01003011{
Christopher Faulet908628c2022-03-25 16:43:49 +01003012 struct appctx *appctx = __cs_appctx(cs);
3013 struct stream *s = __cs_strm(cs);
3014 struct channel *rep = cs_ic(cs);
William Lallemand74c24fb2016-11-21 17:18:36 +01003015 struct server *sv, *svs; /* server and server-state, server-state=server or server->track */
3016 struct listener *l;
William Lallemand74c24fb2016-11-21 17:18:36 +01003017
3018 chunk_reset(&trash);
3019
3020 switch (appctx->ctx.stats.px_st) {
3021 case STAT_PX_ST_INIT:
3022 /* we are on a new proxy */
3023 if (uri && uri->scope) {
3024 /* we have a limited scope, we have to check the proxy name */
3025 struct stat_scope *scope;
3026 int len;
3027
3028 len = strlen(px->id);
3029 scope = uri->scope;
3030
3031 while (scope) {
3032 /* match exact proxy name */
3033 if (scope->px_len == len && !memcmp(px->id, scope->px_id, len))
3034 break;
3035
3036 /* match '.' which means 'self' proxy */
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003037 if (strcmp(scope->px_id, ".") == 0 && px == s->be)
William Lallemand74c24fb2016-11-21 17:18:36 +01003038 break;
3039 scope = scope->next;
3040 }
3041
3042 /* proxy name not found : don't dump anything */
3043 if (scope == NULL)
3044 return 1;
3045 }
3046
3047 /* if the user has requested a limited output and the proxy
3048 * name does not match, skip it.
3049 */
Christopher Fauleted7a0662019-01-14 11:07:34 +01003050 if (appctx->ctx.stats.scope_len) {
Christopher Faulet908628c2022-03-25 16:43:49 +01003051 const char *scope_ptr = stats_scope_ptr(appctx, cs);
Christopher Fauleted7a0662019-01-14 11:07:34 +01003052
3053 if (strnistr(px->id, strlen(px->id), scope_ptr, appctx->ctx.stats.scope_len) == NULL)
3054 return 1;
3055 }
William Lallemand74c24fb2016-11-21 17:18:36 +01003056
3057 if ((appctx->ctx.stats.flags & STAT_BOUND) &&
3058 (appctx->ctx.stats.iid != -1) &&
3059 (px->uuid != appctx->ctx.stats.iid))
3060 return 1;
3061
3062 appctx->ctx.stats.px_st = STAT_PX_ST_TH;
3063 /* fall through */
3064
3065 case STAT_PX_ST_TH:
3066 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Christopher Faulet908628c2022-03-25 16:43:49 +01003067 stats_dump_html_px_hdr(cs, px);
Christopher Fauletef779222018-10-31 08:47:01 +01003068 if (!stats_putchk(rep, htx, &trash))
3069 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01003070 }
3071
3072 appctx->ctx.stats.px_st = STAT_PX_ST_FE;
3073 /* fall through */
3074
3075 case STAT_PX_ST_FE:
3076 /* print the frontend */
Christopher Faulet908628c2022-03-25 16:43:49 +01003077 if (stats_dump_fe_stats(cs, px)) {
Christopher Fauletef779222018-10-31 08:47:01 +01003078 if (!stats_putchk(rep, htx, &trash))
3079 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01003080 }
3081
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +02003082 appctx->ctx.stats.obj2 = px->conf.listeners.n;
William Lallemand74c24fb2016-11-21 17:18:36 +01003083 appctx->ctx.stats.px_st = STAT_PX_ST_LI;
3084 /* fall through */
3085
3086 case STAT_PX_ST_LI:
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +02003087 /* obj2 points to listeners list as initialized above */
3088 for (; appctx->ctx.stats.obj2 != &px->conf.listeners; appctx->ctx.stats.obj2 = l->by_fe.n) {
Christopher Fauletef779222018-10-31 08:47:01 +01003089 if (htx) {
3090 if (htx_almost_full(htx))
3091 goto full;
3092 }
3093 else {
3094 if (buffer_almost_full(&rep->buf))
3095 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01003096 }
3097
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +02003098 l = LIST_ELEM(appctx->ctx.stats.obj2, struct listener *, by_fe);
William Lallemand74c24fb2016-11-21 17:18:36 +01003099 if (!l->counters)
3100 continue;
3101
3102 if (appctx->ctx.stats.flags & STAT_BOUND) {
3103 if (!(appctx->ctx.stats.type & (1 << STATS_TYPE_SO)))
3104 break;
3105
3106 if (appctx->ctx.stats.sid != -1 && l->luid != appctx->ctx.stats.sid)
3107 continue;
3108 }
3109
3110 /* print the frontend */
Christopher Faulet908628c2022-03-25 16:43:49 +01003111 if (stats_dump_li_stats(cs, px, l)) {
Christopher Fauletef779222018-10-31 08:47:01 +01003112 if (!stats_putchk(rep, htx, &trash))
3113 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01003114 }
3115 }
3116
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +02003117 appctx->ctx.stats.obj2 = px->srv; /* may be NULL */
William Lallemand74c24fb2016-11-21 17:18:36 +01003118 appctx->ctx.stats.px_st = STAT_PX_ST_SV;
3119 /* fall through */
3120
3121 case STAT_PX_ST_SV:
Amaury Denoyelle0a8d05d2021-08-25 14:39:53 +02003122 /* obj2 points to servers list as initialized above.
3123 *
Amaury Denoyellebc2ebfa2021-08-25 15:34:53 +02003124 * A server may be removed during the stats dumping.
Amaury Denoyelle0a8d05d2021-08-25 14:39:53 +02003125 * Temporarily increment its refcount to prevent its
3126 * anticipated cleaning. Call free_server to release it.
3127 */
3128 for (; appctx->ctx.stats.obj2 != NULL;
Amaury Denoyellebc2ebfa2021-08-25 15:34:53 +02003129 appctx->ctx.stats.obj2 = srv_drop(sv)) {
Amaury Denoyelle0a8d05d2021-08-25 14:39:53 +02003130
3131 sv = appctx->ctx.stats.obj2;
Amaury Denoyellebc2ebfa2021-08-25 15:34:53 +02003132 srv_take(sv);
Amaury Denoyelle0a8d05d2021-08-25 14:39:53 +02003133
Christopher Fauletef779222018-10-31 08:47:01 +01003134 if (htx) {
3135 if (htx_almost_full(htx))
3136 goto full;
3137 }
3138 else {
3139 if (buffer_almost_full(&rep->buf))
3140 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01003141 }
3142
William Lallemand74c24fb2016-11-21 17:18:36 +01003143 if (appctx->ctx.stats.flags & STAT_BOUND) {
Amaury Denoyelle0a8d05d2021-08-25 14:39:53 +02003144 if (!(appctx->ctx.stats.type & (1 << STATS_TYPE_SV))) {
Amaury Denoyellebc2ebfa2021-08-25 15:34:53 +02003145 srv_drop(sv);
William Lallemand74c24fb2016-11-21 17:18:36 +01003146 break;
Amaury Denoyelle0a8d05d2021-08-25 14:39:53 +02003147 }
William Lallemand74c24fb2016-11-21 17:18:36 +01003148
3149 if (appctx->ctx.stats.sid != -1 && sv->puid != appctx->ctx.stats.sid)
3150 continue;
3151 }
3152
Willy Tarreau3e320362020-10-23 17:28:57 +02003153 /* do not report disabled servers */
3154 if (appctx->ctx.stats.flags & STAT_HIDE_MAINT &&
3155 sv->cur_admin & SRV_ADMF_MAINT) {
3156 continue;
3157 }
3158
William Lallemand74c24fb2016-11-21 17:18:36 +01003159 svs = sv;
3160 while (svs->track)
3161 svs = svs->track;
3162
3163 /* do not report servers which are DOWN and not changing state */
3164 if ((appctx->ctx.stats.flags & STAT_HIDE_DOWN) &&
Emeric Brun52a91d32017-08-31 14:41:55 +02003165 ((sv->cur_admin & SRV_ADMF_MAINT) || /* server is in maintenance */
3166 (sv->cur_state == SRV_ST_STOPPED && /* server is down */
William Lallemand74c24fb2016-11-21 17:18:36 +01003167 (!((svs->agent.state | svs->check.state) & CHK_ST_ENABLED) ||
3168 ((svs->agent.state & CHK_ST_ENABLED) && !svs->agent.health) ||
3169 ((svs->check.state & CHK_ST_ENABLED) && !svs->check.health))))) {
3170 continue;
3171 }
3172
Christopher Faulet908628c2022-03-25 16:43:49 +01003173 if (stats_dump_sv_stats(cs, px, sv)) {
Christopher Fauletef779222018-10-31 08:47:01 +01003174 if (!stats_putchk(rep, htx, &trash))
3175 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01003176 }
3177 } /* for sv */
3178
3179 appctx->ctx.stats.px_st = STAT_PX_ST_BE;
3180 /* fall through */
3181
3182 case STAT_PX_ST_BE:
3183 /* print the backend */
Christopher Faulet908628c2022-03-25 16:43:49 +01003184 if (stats_dump_be_stats(cs, px)) {
Christopher Fauletef779222018-10-31 08:47:01 +01003185 if (!stats_putchk(rep, htx, &trash))
3186 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01003187 }
3188
3189 appctx->ctx.stats.px_st = STAT_PX_ST_END;
3190 /* fall through */
3191
3192 case STAT_PX_ST_END:
3193 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Christopher Faulet908628c2022-03-25 16:43:49 +01003194 stats_dump_html_px_end(cs, px);
Christopher Fauletef779222018-10-31 08:47:01 +01003195 if (!stats_putchk(rep, htx, &trash))
3196 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01003197 }
3198
3199 appctx->ctx.stats.px_st = STAT_PX_ST_FIN;
3200 /* fall through */
3201
3202 case STAT_PX_ST_FIN:
3203 return 1;
3204
3205 default:
3206 /* unknown state, we should put an abort() here ! */
3207 return 1;
3208 }
Christopher Fauletef779222018-10-31 08:47:01 +01003209
3210 full:
Christopher Fauleta0bdec32022-04-04 07:51:21 +02003211 cs_rx_room_blk(cs);
Christopher Fauletef779222018-10-31 08:47:01 +01003212 return 0;
William Lallemand74c24fb2016-11-21 17:18:36 +01003213}
3214
3215/* Dumps the HTTP stats head block to the trash for and uses the per-uri
3216 * parameters <uri>. The caller is responsible for clearing the trash if needed.
3217 */
Willy Tarreau676c29e2019-10-09 10:50:01 +02003218static void stats_dump_html_head(struct appctx *appctx, struct uri_auth *uri)
William Lallemand74c24fb2016-11-21 17:18:36 +01003219{
3220 /* WARNING! This must fit in the first buffer !!! */
3221 chunk_appendf(&trash,
3222 "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"\n"
3223 "\"http://www.w3.org/TR/html4/loose.dtd\">\n"
3224 "<html><head><title>Statistics Report for " PRODUCT_NAME "%s%s</title>\n"
Thierry Fournierb6b1cde2021-09-24 00:03:07 +02003225 "<link rel=\"icon\" href=\"data:,\">\n"
William Lallemand74c24fb2016-11-21 17:18:36 +01003226 "<meta http-equiv=\"content-type\" content=\"text/html; charset=iso-8859-1\">\n"
3227 "<style type=\"text/css\"><!--\n"
3228 "body {"
3229 " font-family: arial, helvetica, sans-serif;"
3230 " font-size: 12px;"
3231 " font-weight: normal;"
3232 " color: black;"
3233 " background: white;"
3234 "}\n"
3235 "th,td {"
3236 " font-size: 10px;"
3237 "}\n"
3238 "h1 {"
3239 " font-size: x-large;"
3240 " margin-bottom: 0.5em;"
3241 "}\n"
3242 "h2 {"
3243 " font-family: helvetica, arial;"
3244 " font-size: x-large;"
3245 " font-weight: bold;"
3246 " font-style: italic;"
3247 " color: #6020a0;"
3248 " margin-top: 0em;"
3249 " margin-bottom: 0em;"
3250 "}\n"
3251 "h3 {"
3252 " font-family: helvetica, arial;"
3253 " font-size: 16px;"
3254 " font-weight: bold;"
3255 " color: #b00040;"
3256 " background: #e8e8d0;"
3257 " margin-top: 0em;"
3258 " margin-bottom: 0em;"
3259 "}\n"
3260 "li {"
3261 " margin-top: 0.25em;"
3262 " margin-right: 2em;"
3263 "}\n"
3264 ".hr {margin-top: 0.25em;"
3265 " border-color: black;"
3266 " border-bottom-style: solid;"
3267 "}\n"
3268 ".titre {background: #20D0D0;color: #000000; font-weight: bold; text-align: center;}\n"
3269 ".total {background: #20D0D0;color: #ffff80;}\n"
3270 ".frontend {background: #e8e8d0;}\n"
3271 ".socket {background: #d0d0d0;}\n"
3272 ".backend {background: #e8e8d0;}\n"
3273 ".active_down {background: #ff9090;}\n"
3274 ".active_going_up {background: #ffd020;}\n"
3275 ".active_going_down {background: #ffffa0;}\n"
3276 ".active_up {background: #c0ffc0;}\n"
3277 ".active_nolb {background: #20a0ff;}\n"
3278 ".active_draining {background: #20a0FF;}\n"
3279 ".active_no_check {background: #e0e0e0;}\n"
3280 ".backup_down {background: #ff9090;}\n"
3281 ".backup_going_up {background: #ff80ff;}\n"
3282 ".backup_going_down {background: #c060ff;}\n"
3283 ".backup_up {background: #b0d0ff;}\n"
3284 ".backup_nolb {background: #90b0e0;}\n"
3285 ".backup_draining {background: #cc9900;}\n"
3286 ".backup_no_check {background: #e0e0e0;}\n"
3287 ".maintain {background: #c07820;}\n"
3288 ".rls {letter-spacing: 0.2em; margin-right: 1px;}\n" /* right letter spacing (used for grouping digits) */
3289 "\n"
3290 "a.px:link {color: #ffff40; text-decoration: none;}"
3291 "a.px:visited {color: #ffff40; text-decoration: none;}"
3292 "a.px:hover {color: #ffffff; text-decoration: none;}"
3293 "a.lfsb:link {color: #000000; text-decoration: none;}"
3294 "a.lfsb:visited {color: #000000; text-decoration: none;}"
3295 "a.lfsb:hover {color: #505050; text-decoration: none;}"
3296 "\n"
3297 "table.tbl { border-collapse: collapse; border-style: none;}\n"
3298 "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"
3299 "table.tbl td.ac { text-align: center;}\n"
3300 "table.tbl th { border-width: 1px; border-style: solid solid solid solid; border-color: gray;}\n"
3301 "table.tbl th.pxname { background: #b00040; color: #ffff40; font-weight: bold; border-style: solid solid none solid; padding: 2px 3px; white-space: nowrap;}\n"
3302 "table.tbl th.empty { border-style: none; empty-cells: hide; background: white;}\n"
3303 "table.tbl th.desc { background: white; border-style: solid solid none solid; text-align: left; padding: 2px 3px;}\n"
3304 "\n"
3305 "table.lgd { border-collapse: collapse; border-width: 1px; border-style: none none none solid; border-color: black;}\n"
3306 "table.lgd td { border-width: 1px; border-style: solid solid solid solid; border-color: gray; padding: 2px;}\n"
3307 "table.lgd td.noborder { border-style: none; padding: 2px; white-space: nowrap;}\n"
3308 "table.det { border-collapse: collapse; border-style: none; }\n"
3309 "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"
3310 "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"
3311 "u {text-decoration:none; border-bottom: 1px dotted black;}\n"
Thierry Fournierb6b1cde2021-09-24 00:03:07 +02003312 "div.tips {\n"
3313 " display:block;\n"
3314 " visibility:hidden;\n"
3315 " z-index:2147483647;\n"
3316 " position:absolute;\n"
3317 " padding:2px 4px 3px;\n"
3318 " background:#f0f060; color:#000000;\n"
3319 " border:1px solid #7040c0;\n"
3320 " white-space:nowrap;\n"
3321 " font-style:normal;font-size:11px;font-weight:normal;\n"
3322 " -moz-border-radius:3px;-webkit-border-radius:3px;border-radius:3px;\n"
3323 " -moz-box-shadow:gray 2px 2px 3px;-webkit-box-shadow:gray 2px 2px 3px;box-shadow:gray 2px 2px 3px;\n"
3324 "}\n"
3325 "u:hover div.tips {visibility:visible;}\n"
Thierry Fourniere9ed63e2021-09-23 23:59:49 +02003326 "@media (prefers-color-scheme: dark) {\n"
3327 " body { font-family: arial, helvetica, sans-serif; font-size: 12px; font-weight: normal; color: #e8e6e3; background: #131516;}\n"
3328 " h1 { color: #a265e0!important; }\n"
3329 " h2 { color: #a265e0; }\n"
3330 " h3 { color: #ff5190; background-color: #3e3e1f; }\n"
3331 " a { color: #3391ff; }\n"
3332 " input { background-color: #2f3437; }\n"
3333 " .hr { border-color: #8c8273; }\n"
3334 " .titre { background-color: #1aa6a6; color: #e8e6e3; }\n"
3335 " .frontend {background: #2f3437;}\n"
Marno Krahmera690b732022-03-08 13:45:09 +01003336 " .socket {background: #2a2d2f;}\n"
Thierry Fourniere9ed63e2021-09-23 23:59:49 +02003337 " .backend {background: #2f3437;}\n"
3338 " .active_down {background: #760000;}\n"
3339 " .active_going_up {background: #b99200;}\n"
3340 " .active_going_down {background: #6c6c00;}\n"
3341 " .active_up {background: #165900;}\n"
3342 " .active_nolb {background: #006ab9;}\n"
3343 " .active_draining {background: #006ab9;}\n"
3344 " .active_no_check {background: #2a2d2f;}\n"
3345 " .backup_down {background: #760000;}\n"
3346 " .backup_going_up {background: #7f007f;}\n"
3347 " .backup_going_down {background: #580092;}\n"
3348 " .backup_up {background: #2e3234;}\n"
3349 " .backup_nolb {background: #1e3c6a;}\n"
3350 " .backup_draining {background: #a37a00;}\n"
3351 " .backup_no_check {background: #2a2d2f;}\n"
3352 " .maintain {background: #9a601a;}\n"
3353 " a.px:link {color: #d8d83b; text-decoration: none;}\n"
3354 " a.px:visited {color: #d8d83b; text-decoration: none;}\n"
3355 " a.px:hover {color: #ffffff; text-decoration: none;}\n"
3356 " a.lfsb:link {color: #e8e6e3; text-decoration: none;}\n"
3357 " a.lfsb:visited {color: #e8e6e3; text-decoration: none;}\n"
3358 " a.lfsb:hover {color: #b5afa6; text-decoration: none;}\n"
3359 " table.tbl th.empty { background-color: #181a1b; }\n"
Willy Tarreau00147f72022-04-11 07:59:27 +02003360 " table.tbl th.desc { background: #181a1b; }\n"
Thierry Fourniere9ed63e2021-09-23 23:59:49 +02003361 " table.tbl th.pxname { background-color: #8d0033; color: #ffff46; }\n"
3362 " table.tbl th { border-color: #808080; }\n"
3363 " table.tbl td { border-color: #808080; }\n"
3364 " u {text-decoration:none; border-bottom: 1px dotted #e8e6e3;}\n"
3365 " div.tips {\n"
3366 " background:#8e8e0d;\n"
3367 " color:#e8e6e3;\n"
3368 " border-color: #4e2c86;\n"
3369 " -moz-box-shadow: #60686c 2px 2px 3px;\n"
3370 " -webkit-box-shadow: #60686c 2px 2px 3px;\n"
3371 " box-shadow: #60686c 2px 2px 3px;\n"
3372 " }\n"
3373 "}\n"
William Lallemand74c24fb2016-11-21 17:18:36 +01003374 "-->\n"
3375 "</style></head>\n",
Willy Tarreau676c29e2019-10-09 10:50:01 +02003376 (appctx->ctx.stats.flags & STAT_SHNODE) ? " on " : "",
Willy Tarreau027d2062020-01-23 11:47:13 +01003377 (appctx->ctx.stats.flags & STAT_SHNODE) ? (uri && uri->node ? uri->node : global.node) : ""
William Lallemand74c24fb2016-11-21 17:18:36 +01003378 );
3379}
3380
3381/* Dumps the HTML stats information block to the trash for and uses the state from
Christopher Faulet908628c2022-03-25 16:43:49 +01003382 * conn-stream <cs> and per-uri parameters <uri>. The caller is responsible
William Lallemand74c24fb2016-11-21 17:18:36 +01003383 * for clearing the trash if needed.
3384 */
Christopher Faulet908628c2022-03-25 16:43:49 +01003385static void stats_dump_html_info(struct conn_stream *cs, struct uri_auth *uri)
William Lallemand74c24fb2016-11-21 17:18:36 +01003386{
Christopher Faulet908628c2022-03-25 16:43:49 +01003387 struct appctx *appctx = __cs_appctx(cs);
William Lallemand74c24fb2016-11-21 17:18:36 +01003388 unsigned int up = (now.tv_sec - start_date.tv_sec);
3389 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
Christopher Faulet908628c2022-03-25 16:43:49 +01003390 const char *scope_ptr = stats_scope_ptr(appctx, cs);
Willy Tarreau1713c032019-05-23 12:23:55 +02003391 unsigned long long bps = (unsigned long long)read_freq_ctr(&global.out_32bps) * 32;
3392
3393 /* Turn the bytes per second to bits per second and take care of the
3394 * usual ethernet overhead in order to help figure how far we are from
3395 * interface saturation since it's the only case which usually matters.
3396 * For this we count the total size of an Ethernet frame on the wire
3397 * including preamble and IFG (1538) for the largest TCP segment it
3398 * transports (1448 with TCP timestamps). This is not valid for smaller
3399 * packets (under-estimated), but it gives a reasonably accurate
3400 * estimation of how far we are from uplink saturation.
3401 */
3402 bps = bps * 8 * 1538 / 1448;
William Lallemand74c24fb2016-11-21 17:18:36 +01003403
3404 /* WARNING! this has to fit the first packet too.
3405 * We are around 3.5 kB, add adding entries will
3406 * become tricky if we want to support 4kB buffers !
3407 */
3408 chunk_appendf(&trash,
3409 "<body><h1><a href=\"" PRODUCT_URL "\" style=\"text-decoration: none;\">"
3410 PRODUCT_NAME "%s</a></h1>\n"
3411 "<h2>Statistics Report for pid %d%s%s%s%s</h2>\n"
3412 "<hr width=\"100%%\" class=\"hr\">\n"
3413 "<h3>&gt; General process information</h3>\n"
3414 "<table border=0><tr><td align=\"left\" nowrap width=\"1%%\">\n"
Yves Lafon95317282018-02-26 11:10:37 +01003415 "<p><b>pid = </b> %d (process #%d, nbproc = %d, nbthread = %d)<br>\n"
William Lallemand74c24fb2016-11-21 17:18:36 +01003416 "<b>uptime = </b> %dd %dh%02dm%02ds<br>\n"
3417 "<b>system limits:</b> memmax = %s%s; ulimit-n = %d<br>\n"
3418 "<b>maxsock = </b> %d; <b>maxconn = </b> %d; <b>maxpipes = </b> %d<br>\n"
Willy Tarreau1713c032019-05-23 12:23:55 +02003419 "current conns = %d; current pipes = %d/%d; conn rate = %d/sec; bit rate = %.3f %cbps<br>\n"
William Lallemand74c24fb2016-11-21 17:18:36 +01003420 "Running tasks: %d/%d; idle = %d %%<br>\n"
3421 "</td><td align=\"center\" nowrap>\n"
3422 "<table class=\"lgd\"><tr>\n"
3423 "<td class=\"active_up\">&nbsp;</td><td class=\"noborder\">active UP </td>"
3424 "<td class=\"backup_up\">&nbsp;</td><td class=\"noborder\">backup UP </td>"
3425 "</tr><tr>\n"
3426 "<td class=\"active_going_down\"></td><td class=\"noborder\">active UP, going down </td>"
3427 "<td class=\"backup_going_down\"></td><td class=\"noborder\">backup UP, going down </td>"
3428 "</tr><tr>\n"
3429 "<td class=\"active_going_up\"></td><td class=\"noborder\">active DOWN, going up </td>"
3430 "<td class=\"backup_going_up\"></td><td class=\"noborder\">backup DOWN, going up </td>"
3431 "</tr><tr>\n"
3432 "<td class=\"active_down\"></td><td class=\"noborder\">active or backup DOWN &nbsp;</td>"
3433 "<td class=\"active_no_check\"></td><td class=\"noborder\">not checked </td>"
3434 "</tr><tr>\n"
3435 "<td class=\"maintain\"></td><td class=\"noborder\" colspan=\"3\">active or backup DOWN for maintenance (MAINT) &nbsp;</td>"
3436 "</tr><tr>\n"
3437 "<td class=\"active_draining\"></td><td class=\"noborder\" colspan=\"3\">active or backup SOFT STOPPED for maintenance &nbsp;</td>"
3438 "</tr></table>\n"
3439 "Note: \"NOLB\"/\"DRAIN\" = UP with load-balancing disabled."
3440 "</td>"
3441 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
3442 "<b>Display option:</b><ul style=\"margin-top: 0.25em;\">"
3443 "",
Willy Tarreau676c29e2019-10-09 10:50:01 +02003444 (appctx->ctx.stats.flags & STAT_HIDEVER) ? "" : (stats_version_string),
3445 pid, (appctx->ctx.stats.flags & STAT_SHNODE) ? " on " : "",
3446 (appctx->ctx.stats.flags & STAT_SHNODE) ? (uri->node ? uri->node : global.node) : "",
3447 (appctx->ctx.stats.flags & STAT_SHDESC) ? ": " : "",
3448 (appctx->ctx.stats.flags & STAT_SHDESC) ? (uri->desc ? uri->desc : global.desc) : "",
Willy Tarreaue8422bf2021-06-15 09:08:18 +02003449 pid, 1, 1, global.nbthread,
William Lallemand74c24fb2016-11-21 17:18:36 +01003450 up / 86400, (up % 86400) / 3600,
3451 (up % 3600) / 60, (up % 60),
3452 global.rlimit_memmax ? ultoa(global.rlimit_memmax) : "unlimited",
3453 global.rlimit_memmax ? " MB" : "",
3454 global.rlimit_nofile,
3455 global.maxsock, global.maxconn, global.maxpipes,
3456 actconn, pipes_used, pipes_used+pipes_free, read_freq_ctr(&global.conn_per_sec),
Willy Tarreau1713c032019-05-23 12:23:55 +02003457 bps >= 1000000000UL ? (bps / 1000000000.0) : bps >= 1000000UL ? (bps / 1000000.0) : (bps / 1000.0),
3458 bps >= 1000000000UL ? 'G' : bps >= 1000000UL ? 'M' : 'k',
Willy Tarreauf9d5e102021-10-08 10:43:59 +02003459 total_run_queues(), total_allocated_tasks(), clock_report_idle()
William Lallemand74c24fb2016-11-21 17:18:36 +01003460 );
3461
3462 /* scope_txt = search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
Christopher Fauleted7a0662019-01-14 11:07:34 +01003463 memcpy(scope_txt, scope_ptr, appctx->ctx.stats.scope_len);
William Lallemand74c24fb2016-11-21 17:18:36 +01003464 scope_txt[appctx->ctx.stats.scope_len] = '\0';
3465
3466 chunk_appendf(&trash,
3467 "<li><form method=\"GET\">Scope : <input value=\"%s\" name=\"" STAT_SCOPE_INPUT_NAME "\" size=\"8\" maxlength=\"%d\" tabindex=\"1\"/></form>\n",
3468 (appctx->ctx.stats.scope_len > 0) ? scope_txt : "",
3469 STAT_SCOPE_TXT_MAXLEN);
3470
3471 /* scope_txt = search pattern + search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
3472 scope_txt[0] = 0;
3473 if (appctx->ctx.stats.scope_len) {
3474 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Christopher Fauleted7a0662019-01-14 11:07:34 +01003475 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), scope_ptr, appctx->ctx.stats.scope_len);
William Lallemand74c24fb2016-11-21 17:18:36 +01003476 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
3477 }
3478
3479 if (appctx->ctx.stats.flags & STAT_HIDE_DOWN)
3480 chunk_appendf(&trash,
3481 "<li><a href=\"%s%s%s%s\">Show all servers</a><br>\n",
3482 uri->uri_prefix,
3483 "",
3484 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
3485 scope_txt);
3486 else
3487 chunk_appendf(&trash,
3488 "<li><a href=\"%s%s%s%s\">Hide 'DOWN' servers</a><br>\n",
3489 uri->uri_prefix,
3490 ";up",
3491 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
3492 scope_txt);
3493
3494 if (uri->refresh > 0) {
3495 if (appctx->ctx.stats.flags & STAT_NO_REFRESH)
3496 chunk_appendf(&trash,
3497 "<li><a href=\"%s%s%s%s\">Enable refresh</a><br>\n",
3498 uri->uri_prefix,
3499 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3500 "",
3501 scope_txt);
3502 else
3503 chunk_appendf(&trash,
3504 "<li><a href=\"%s%s%s%s\">Disable refresh</a><br>\n",
3505 uri->uri_prefix,
3506 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3507 ";norefresh",
3508 scope_txt);
3509 }
3510
3511 chunk_appendf(&trash,
3512 "<li><a href=\"%s%s%s%s\">Refresh now</a><br>\n",
3513 uri->uri_prefix,
3514 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3515 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
3516 scope_txt);
3517
3518 chunk_appendf(&trash,
3519 "<li><a href=\"%s;csv%s%s\">CSV export</a><br>\n",
3520 uri->uri_prefix,
3521 (uri->refresh > 0) ? ";norefresh" : "",
3522 scope_txt);
3523
3524 chunk_appendf(&trash,
Christopher Faulet6338a082019-09-09 15:50:54 +02003525 "<li><a href=\"%s;json%s%s\">JSON export</a> (<a href=\"%s;json-schema\">schema</a>)<br>\n",
3526 uri->uri_prefix,
3527 (uri->refresh > 0) ? ";norefresh" : "",
3528 scope_txt, uri->uri_prefix);
3529
3530 chunk_appendf(&trash,
William Lallemand74c24fb2016-11-21 17:18:36 +01003531 "</ul></td>"
3532 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
3533 "<b>External resources:</b><ul style=\"margin-top: 0.25em;\">\n"
3534 "<li><a href=\"" PRODUCT_URL "\">Primary site</a><br>\n"
3535 "<li><a href=\"" PRODUCT_URL_UPD "\">Updates (v" PRODUCT_BRANCH ")</a><br>\n"
3536 "<li><a href=\"" PRODUCT_URL_DOC "\">Online manual</a><br>\n"
3537 "</ul>"
3538 "</td>"
3539 "</tr></table>\n"
3540 ""
3541 );
3542
3543 if (appctx->ctx.stats.st_code) {
3544 switch (appctx->ctx.stats.st_code) {
3545 case STAT_STATUS_DONE:
3546 chunk_appendf(&trash,
3547 "<p><div class=active_up>"
3548 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
3549 "Action processed successfully."
3550 "</div>\n", uri->uri_prefix,
3551 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3552 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
3553 scope_txt);
3554 break;
3555 case STAT_STATUS_NONE:
3556 chunk_appendf(&trash,
3557 "<p><div class=active_going_down>"
3558 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
3559 "Nothing has changed."
3560 "</div>\n", uri->uri_prefix,
3561 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3562 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
3563 scope_txt);
3564 break;
3565 case STAT_STATUS_PART:
3566 chunk_appendf(&trash,
3567 "<p><div class=active_going_down>"
3568 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
3569 "Action partially processed.<br>"
3570 "Some server names are probably unknown or ambiguous (duplicated names in the backend)."
3571 "</div>\n", uri->uri_prefix,
3572 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3573 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
3574 scope_txt);
3575 break;
3576 case STAT_STATUS_ERRP:
3577 chunk_appendf(&trash,
3578 "<p><div class=active_down>"
3579 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
3580 "Action not processed because of invalid parameters."
3581 "<ul>"
3582 "<li>The action is maybe unknown.</li>"
Christopher Faulet2f9a41d2019-02-27 15:30:57 +01003583 "<li>Invalid key parameter (empty or too long).</li>"
William Lallemand74c24fb2016-11-21 17:18:36 +01003584 "<li>The backend name is probably unknown or ambiguous (duplicated names).</li>"
3585 "<li>Some server names are probably unknown or ambiguous (duplicated names in the backend).</li>"
3586 "</ul>"
3587 "</div>\n", uri->uri_prefix,
3588 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3589 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
3590 scope_txt);
3591 break;
3592 case STAT_STATUS_EXCD:
3593 chunk_appendf(&trash,
3594 "<p><div class=active_down>"
3595 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
3596 "<b>Action not processed : the buffer couldn't store all the data.<br>"
3597 "You should retry with less servers at a time.</b>"
3598 "</div>\n", uri->uri_prefix,
3599 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3600 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
3601 scope_txt);
3602 break;
3603 case STAT_STATUS_DENY:
3604 chunk_appendf(&trash,
3605 "<p><div class=active_down>"
3606 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
3607 "<b>Action denied.</b>"
3608 "</div>\n", uri->uri_prefix,
3609 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3610 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
3611 scope_txt);
3612 break;
Christopher Faulet3c2ecf72019-02-27 16:41:27 +01003613 case STAT_STATUS_IVAL:
3614 chunk_appendf(&trash,
3615 "<p><div class=active_down>"
3616 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
3617 "<b>Invalid requests (unsupported method or chunked encoded request).</b>"
3618 "</div>\n", uri->uri_prefix,
3619 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3620 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
3621 scope_txt);
3622 break;
William Lallemand74c24fb2016-11-21 17:18:36 +01003623 default:
3624 chunk_appendf(&trash,
3625 "<p><div class=active_no_check>"
3626 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
3627 "Unexpected result."
3628 "</div>\n", uri->uri_prefix,
3629 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3630 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
3631 scope_txt);
3632 }
3633 chunk_appendf(&trash, "<p>\n");
3634 }
3635}
3636
3637/* Dumps the HTML stats trailer block to the trash. The caller is responsible
3638 * for clearing the trash if needed.
3639 */
3640static void stats_dump_html_end()
3641{
3642 chunk_appendf(&trash, "</body></html>\n");
3643}
3644
Simon Horman05ee2132017-01-04 09:37:25 +01003645/* Dumps the stats JSON header to the trash buffer which. The caller is responsible
3646 * for clearing it if needed.
3647 */
3648static void stats_dump_json_header()
3649{
3650 chunk_strcat(&trash, "[");
3651}
3652
3653
3654/* Dumps the JSON stats trailer block to the trash. The caller is responsible
3655 * for clearing the trash if needed.
3656 */
3657static void stats_dump_json_end()
3658{
3659 chunk_strcat(&trash, "]");
3660}
3661
Amaury Denoyelle98b81cb2020-10-02 18:32:04 +02003662/* Uses <appctx.ctx.stats.obj1> as a pointer to the current proxy and <obj2> as
3663 * a pointer to the current server/listener.
3664 */
Christopher Faulet908628c2022-03-25 16:43:49 +01003665static int stats_dump_proxies(struct conn_stream *cs,
Amaury Denoyelle98b81cb2020-10-02 18:32:04 +02003666 struct htx *htx,
3667 struct uri_auth *uri)
3668{
Christopher Faulet908628c2022-03-25 16:43:49 +01003669 struct appctx *appctx = __cs_appctx(cs);
3670 struct channel *rep = cs_ic(cs);
Amaury Denoyelle98b81cb2020-10-02 18:32:04 +02003671 struct proxy *px;
3672
3673 /* dump proxies */
3674 while (appctx->ctx.stats.obj1) {
3675 if (htx) {
3676 if (htx_almost_full(htx))
3677 goto full;
3678 }
3679 else {
3680 if (buffer_almost_full(&rep->buf))
3681 goto full;
3682 }
3683
3684 px = appctx->ctx.stats.obj1;
Marno Krahmer07954fb2021-06-24 16:51:08 +02003685 /* Skip the global frontend proxies and non-networked ones.
William Lallemand85a16b22021-08-03 13:18:11 +02003686 * Also skip proxies that were disabled in the configuration
Marno Krahmer07954fb2021-06-24 16:51:08 +02003687 * This change allows retrieving stats from "old" proxies after a reload.
3688 */
Christopher Fauletdfd10ab2021-10-06 14:24:19 +02003689 if (!(px->flags & PR_FL_DISABLED) && px->uuid > 0 &&
William Lallemande7f74622021-07-28 17:45:18 +02003690 (px->cap & (PR_CAP_FE | PR_CAP_BE)) && !(px->cap & PR_CAP_INT)) {
Christopher Faulet908628c2022-03-25 16:43:49 +01003691 if (stats_dump_proxy_to_buffer(cs, htx, px, uri) == 0)
Amaury Denoyelle98b81cb2020-10-02 18:32:04 +02003692 return 0;
3693 }
3694
3695 appctx->ctx.stats.obj1 = px->next;
3696 appctx->ctx.stats.px_st = STAT_PX_ST_INIT;
3697 }
3698
3699 return 1;
3700
3701 full:
Christopher Fauleta0bdec32022-04-04 07:51:21 +02003702 cs_rx_room_blk(cs);
Amaury Denoyelle98b81cb2020-10-02 18:32:04 +02003703 return 0;
3704}
3705
Christopher Faulet908628c2022-03-25 16:43:49 +01003706/* This function dumps statistics onto the conn-stream's read buffer in
William Lallemand74c24fb2016-11-21 17:18:36 +01003707 * either CSV or HTML format. <uri> contains some HTML-specific parameters that
3708 * are ignored for CSV format (hence <uri> may be NULL there). It returns 0 if
3709 * it had to stop writing data and an I/O is needed, 1 if the dump is finished
3710 * and the stream must be closed, or -1 in case of any error. This function is
3711 * used by both the CLI and the HTTP handlers.
3712 */
Christopher Faulet908628c2022-03-25 16:43:49 +01003713static int stats_dump_stat_to_buffer(struct conn_stream *cs, struct htx *htx,
Christopher Fauletef779222018-10-31 08:47:01 +01003714 struct uri_auth *uri)
William Lallemand74c24fb2016-11-21 17:18:36 +01003715{
Christopher Faulet908628c2022-03-25 16:43:49 +01003716 struct appctx *appctx = __cs_appctx(cs);
3717 struct channel *rep = cs_ic(cs);
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02003718 enum stats_domain domain = appctx->ctx.stats.domain;
William Lallemand74c24fb2016-11-21 17:18:36 +01003719
3720 chunk_reset(&trash);
3721
3722 switch (appctx->st2) {
3723 case STAT_ST_INIT:
3724 appctx->st2 = STAT_ST_HEAD; /* let's start producing data */
3725 /* fall through */
3726
3727 case STAT_ST_HEAD:
3728 if (appctx->ctx.stats.flags & STAT_FMT_HTML)
Willy Tarreau676c29e2019-10-09 10:50:01 +02003729 stats_dump_html_head(appctx, uri);
Christopher Faulet6338a082019-09-09 15:50:54 +02003730 else if (appctx->ctx.stats.flags & STAT_JSON_SCHM)
3731 stats_dump_json_schema(&trash);
Simon Horman05ee2132017-01-04 09:37:25 +01003732 else if (appctx->ctx.stats.flags & STAT_FMT_JSON)
Willy Tarreau9d7fb632017-04-11 07:53:04 +02003733 stats_dump_json_header();
William Lallemand74c24fb2016-11-21 17:18:36 +01003734 else if (!(appctx->ctx.stats.flags & STAT_FMT_TYPED))
Amaury Denoyelle50660a82020-10-05 11:49:39 +02003735 stats_dump_csv_header(appctx->ctx.stats.domain);
William Lallemand74c24fb2016-11-21 17:18:36 +01003736
Christopher Fauletef779222018-10-31 08:47:01 +01003737 if (!stats_putchk(rep, htx, &trash))
3738 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01003739
Christopher Faulet6338a082019-09-09 15:50:54 +02003740 if (appctx->ctx.stats.flags & STAT_JSON_SCHM) {
3741 appctx->st2 = STAT_ST_FIN;
3742 return 1;
3743 }
William Lallemand74c24fb2016-11-21 17:18:36 +01003744 appctx->st2 = STAT_ST_INFO;
3745 /* fall through */
3746
3747 case STAT_ST_INFO:
3748 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Christopher Faulet908628c2022-03-25 16:43:49 +01003749 stats_dump_html_info(cs, uri);
Christopher Fauletef779222018-10-31 08:47:01 +01003750 if (!stats_putchk(rep, htx, &trash))
3751 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01003752 }
3753
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02003754 if (domain == STATS_DOMAIN_PROXY)
3755 appctx->ctx.stats.obj1 = proxies_list;
3756
William Lallemand74c24fb2016-11-21 17:18:36 +01003757 appctx->ctx.stats.px_st = STAT_PX_ST_INIT;
3758 appctx->st2 = STAT_ST_LIST;
3759 /* fall through */
3760
3761 case STAT_ST_LIST:
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02003762 switch (domain) {
Emeric Brunf8642ee2021-10-29 17:59:18 +02003763 case STATS_DOMAIN_RESOLVERS:
Christopher Faulet908628c2022-03-25 16:43:49 +01003764 if (!stats_dump_resolvers(cs, stat_l[domain],
Emeric Brund3b44952021-01-06 16:01:02 +01003765 stat_count[domain],
3766 &stats_module_list[domain])) {
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02003767 return 0;
3768 }
3769 break;
3770
3771 case STATS_DOMAIN_PROXY:
3772 default:
3773 /* dump proxies */
Christopher Faulet908628c2022-03-25 16:43:49 +01003774 if (!stats_dump_proxies(cs, htx, uri))
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02003775 return 0;
3776 break;
3777 }
William Lallemand74c24fb2016-11-21 17:18:36 +01003778
3779 appctx->st2 = STAT_ST_END;
3780 /* fall through */
3781
3782 case STAT_ST_END:
Simon Horman05ee2132017-01-04 09:37:25 +01003783 if (appctx->ctx.stats.flags & (STAT_FMT_HTML|STAT_FMT_JSON)) {
3784 if (appctx->ctx.stats.flags & STAT_FMT_HTML)
3785 stats_dump_html_end();
3786 else
3787 stats_dump_json_end();
Christopher Fauletef779222018-10-31 08:47:01 +01003788 if (!stats_putchk(rep, htx, &trash))
3789 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01003790 }
3791
3792 appctx->st2 = STAT_ST_FIN;
3793 /* fall through */
3794
3795 case STAT_ST_FIN:
3796 return 1;
3797
3798 default:
3799 /* unknown state ! */
3800 appctx->st2 = STAT_ST_FIN;
3801 return -1;
3802 }
Christopher Fauletef779222018-10-31 08:47:01 +01003803
3804 full:
Christopher Fauleta0bdec32022-04-04 07:51:21 +02003805 cs_rx_room_blk(cs);
Christopher Fauletef779222018-10-31 08:47:01 +01003806 return 0;
3807
William Lallemand74c24fb2016-11-21 17:18:36 +01003808}
3809
3810/* We reached the stats page through a POST request. The appctx is
3811 * expected to have already been allocated by the caller.
3812 * Parse the posted data and enable/disable servers if necessary.
3813 * Returns 1 if request was parsed or zero if it needs more data.
3814 */
Christopher Faulet908628c2022-03-25 16:43:49 +01003815static int stats_process_http_post(struct conn_stream *cs)
William Lallemand74c24fb2016-11-21 17:18:36 +01003816{
Christopher Faulet908628c2022-03-25 16:43:49 +01003817 struct stream *s = __cs_strm(cs);
3818 struct appctx *appctx = __cs_appctx(cs);
William Lallemand74c24fb2016-11-21 17:18:36 +01003819
3820 struct proxy *px = NULL;
3821 struct server *sv = NULL;
3822
3823 char key[LINESIZE];
3824 int action = ST_ADM_ACTION_NONE;
3825 int reprocess = 0;
3826
3827 int total_servers = 0;
3828 int altered_servers = 0;
3829
3830 char *first_param, *cur_param, *next_param, *end_params;
3831 char *st_cur_param = NULL;
3832 char *st_next_param = NULL;
3833
Christopher Fauleta3618372018-12-13 21:59:56 +01003834 struct buffer *temp = get_trash_chunk();
William Lallemand74c24fb2016-11-21 17:18:36 +01003835
Christopher Fauletb7f88902019-07-15 21:56:43 +02003836 struct htx *htx = htxbuf(&s->req.buf);
3837 struct htx_blk *blk;
Christopher Fauleta3618372018-12-13 21:59:56 +01003838
Christopher Fauletb7f88902019-07-15 21:56:43 +02003839 /* we need more data */
3840 if (s->txn->req.msg_state < HTTP_MSG_DONE) {
3841 /* check if we can receive more */
3842 if (htx_free_data_space(htx) <= global.tune.maxrewrite) {
3843 appctx->ctx.stats.st_code = STAT_STATUS_EXCD;
3844 goto out;
Christopher Fauleta3618372018-12-13 21:59:56 +01003845 }
Christopher Fauletb7f88902019-07-15 21:56:43 +02003846 goto wait;
3847 }
Christopher Fauleta3618372018-12-13 21:59:56 +01003848
Christopher Fauletb7f88902019-07-15 21:56:43 +02003849 /* The request was fully received. Copy data */
3850 blk = htx_get_head_blk(htx);
3851 while (blk) {
3852 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Fauleta3618372018-12-13 21:59:56 +01003853
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01003854 if (type == HTX_BLK_TLR || type == HTX_BLK_EOT)
Christopher Fauletb7f88902019-07-15 21:56:43 +02003855 break;
3856 if (type == HTX_BLK_DATA) {
3857 struct ist v = htx_get_blk_value(htx, blk);
Christopher Fauleta3618372018-12-13 21:59:56 +01003858
Christopher Fauletb7f88902019-07-15 21:56:43 +02003859 if (!chunk_memcat(temp, v.ptr, v.len)) {
Christopher Faulet2f9a41d2019-02-27 15:30:57 +01003860 appctx->ctx.stats.st_code = STAT_STATUS_EXCD;
3861 goto out;
3862 }
Christopher Fauleta3618372018-12-13 21:59:56 +01003863 }
Christopher Fauletb7f88902019-07-15 21:56:43 +02003864 blk = htx_get_next_blk(htx, blk);
William Lallemand74c24fb2016-11-21 17:18:36 +01003865 }
3866
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003867 first_param = temp->area;
Christopher Fauleta3618372018-12-13 21:59:56 +01003868 end_params = temp->area + temp->data;
William Lallemand74c24fb2016-11-21 17:18:36 +01003869 cur_param = next_param = end_params;
3870 *end_params = '\0';
3871
3872 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
3873
3874 /*
3875 * Parse the parameters in reverse order to only store the last value.
3876 * From the html form, the backend and the action are at the end.
3877 */
3878 while (cur_param > first_param) {
3879 char *value;
3880 int poffset, plen;
3881
3882 cur_param--;
3883
3884 if ((*cur_param == '&') || (cur_param == first_param)) {
3885 reprocess_servers:
3886 /* Parse the key */
3887 poffset = (cur_param != first_param ? 1 : 0);
3888 plen = next_param - cur_param + (cur_param == first_param ? 1 : 0);
3889 if ((plen > 0) && (plen <= sizeof(key))) {
3890 strncpy(key, cur_param + poffset, plen);
3891 key[plen - 1] = '\0';
3892 } else {
Christopher Faulet2f9a41d2019-02-27 15:30:57 +01003893 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
William Lallemand74c24fb2016-11-21 17:18:36 +01003894 goto out;
3895 }
3896
3897 /* Parse the value */
3898 value = key;
3899 while (*value != '\0' && *value != '=') {
3900 value++;
3901 }
3902 if (*value == '=') {
3903 /* Ok, a value is found, we can mark the end of the key */
3904 *value++ = '\0';
3905 }
Willy Tarreau62ba9ba2020-04-23 17:54:47 +02003906 if (url_decode(key, 1) < 0 || url_decode(value, 1) < 0)
William Lallemand74c24fb2016-11-21 17:18:36 +01003907 break;
3908
3909 /* Now we can check the key to see what to do */
3910 if (!px && (strcmp(key, "b") == 0)) {
3911 if ((px = proxy_be_by_name(value)) == NULL) {
3912 /* the backend name is unknown or ambiguous (duplicate names) */
3913 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
3914 goto out;
3915 }
3916 }
3917 else if (!action && (strcmp(key, "action") == 0)) {
3918 if (strcmp(value, "ready") == 0) {
3919 action = ST_ADM_ACTION_READY;
3920 }
3921 else if (strcmp(value, "drain") == 0) {
3922 action = ST_ADM_ACTION_DRAIN;
3923 }
3924 else if (strcmp(value, "maint") == 0) {
3925 action = ST_ADM_ACTION_MAINT;
3926 }
3927 else if (strcmp(value, "shutdown") == 0) {
3928 action = ST_ADM_ACTION_SHUTDOWN;
3929 }
3930 else if (strcmp(value, "dhlth") == 0) {
3931 action = ST_ADM_ACTION_DHLTH;
3932 }
3933 else if (strcmp(value, "ehlth") == 0) {
3934 action = ST_ADM_ACTION_EHLTH;
3935 }
3936 else if (strcmp(value, "hrunn") == 0) {
3937 action = ST_ADM_ACTION_HRUNN;
3938 }
3939 else if (strcmp(value, "hnolb") == 0) {
3940 action = ST_ADM_ACTION_HNOLB;
3941 }
3942 else if (strcmp(value, "hdown") == 0) {
3943 action = ST_ADM_ACTION_HDOWN;
3944 }
3945 else if (strcmp(value, "dagent") == 0) {
3946 action = ST_ADM_ACTION_DAGENT;
3947 }
3948 else if (strcmp(value, "eagent") == 0) {
3949 action = ST_ADM_ACTION_EAGENT;
3950 }
3951 else if (strcmp(value, "arunn") == 0) {
3952 action = ST_ADM_ACTION_ARUNN;
3953 }
3954 else if (strcmp(value, "adown") == 0) {
3955 action = ST_ADM_ACTION_ADOWN;
3956 }
3957 /* else these are the old supported methods */
3958 else if (strcmp(value, "disable") == 0) {
3959 action = ST_ADM_ACTION_DISABLE;
3960 }
3961 else if (strcmp(value, "enable") == 0) {
3962 action = ST_ADM_ACTION_ENABLE;
3963 }
3964 else if (strcmp(value, "stop") == 0) {
3965 action = ST_ADM_ACTION_STOP;
3966 }
3967 else if (strcmp(value, "start") == 0) {
3968 action = ST_ADM_ACTION_START;
3969 }
3970 else {
3971 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
3972 goto out;
3973 }
3974 }
3975 else if (strcmp(key, "s") == 0) {
3976 if (!(px && action)) {
3977 /*
3978 * Indicates that we'll need to reprocess the parameters
3979 * as soon as backend and action are known
3980 */
3981 if (!reprocess) {
3982 st_cur_param = cur_param;
3983 st_next_param = next_param;
3984 }
3985 reprocess = 1;
3986 }
3987 else if ((sv = findserver(px, value)) != NULL) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003988 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
William Lallemand74c24fb2016-11-21 17:18:36 +01003989 switch (action) {
3990 case ST_ADM_ACTION_DISABLE:
Emeric Brun52a91d32017-08-31 14:41:55 +02003991 if (!(sv->cur_admin & SRV_ADMF_FMAINT)) {
William Lallemand74c24fb2016-11-21 17:18:36 +01003992 altered_servers++;
3993 total_servers++;
3994 srv_set_admin_flag(sv, SRV_ADMF_FMAINT, "'disable' on stats page");
3995 }
3996 break;
3997 case ST_ADM_ACTION_ENABLE:
Emeric Brun52a91d32017-08-31 14:41:55 +02003998 if (sv->cur_admin & SRV_ADMF_FMAINT) {
William Lallemand74c24fb2016-11-21 17:18:36 +01003999 altered_servers++;
4000 total_servers++;
4001 srv_clr_admin_flag(sv, SRV_ADMF_FMAINT);
4002 }
4003 break;
4004 case ST_ADM_ACTION_STOP:
Emeric Brun52a91d32017-08-31 14:41:55 +02004005 if (!(sv->cur_admin & SRV_ADMF_FDRAIN)) {
William Lallemand74c24fb2016-11-21 17:18:36 +01004006 srv_set_admin_flag(sv, SRV_ADMF_FDRAIN, "'stop' on stats page");
4007 altered_servers++;
4008 total_servers++;
4009 }
4010 break;
4011 case ST_ADM_ACTION_START:
Emeric Brun52a91d32017-08-31 14:41:55 +02004012 if (sv->cur_admin & SRV_ADMF_FDRAIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +01004013 srv_clr_admin_flag(sv, SRV_ADMF_FDRAIN);
4014 altered_servers++;
4015 total_servers++;
4016 }
4017 break;
4018 case ST_ADM_ACTION_DHLTH:
4019 if (sv->check.state & CHK_ST_CONFIGURED) {
4020 sv->check.state &= ~CHK_ST_ENABLED;
4021 altered_servers++;
4022 total_servers++;
4023 }
4024 break;
4025 case ST_ADM_ACTION_EHLTH:
4026 if (sv->check.state & CHK_ST_CONFIGURED) {
4027 sv->check.state |= CHK_ST_ENABLED;
4028 altered_servers++;
4029 total_servers++;
4030 }
4031 break;
4032 case ST_ADM_ACTION_HRUNN:
4033 if (!(sv->track)) {
4034 sv->check.health = sv->check.rise + sv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02004035 srv_set_running(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01004036 altered_servers++;
4037 total_servers++;
4038 }
4039 break;
4040 case ST_ADM_ACTION_HNOLB:
4041 if (!(sv->track)) {
4042 sv->check.health = sv->check.rise + sv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02004043 srv_set_stopping(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01004044 altered_servers++;
4045 total_servers++;
4046 }
4047 break;
4048 case ST_ADM_ACTION_HDOWN:
4049 if (!(sv->track)) {
4050 sv->check.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02004051 srv_set_stopped(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01004052 altered_servers++;
4053 total_servers++;
4054 }
4055 break;
4056 case ST_ADM_ACTION_DAGENT:
4057 if (sv->agent.state & CHK_ST_CONFIGURED) {
4058 sv->agent.state &= ~CHK_ST_ENABLED;
4059 altered_servers++;
4060 total_servers++;
4061 }
4062 break;
4063 case ST_ADM_ACTION_EAGENT:
4064 if (sv->agent.state & CHK_ST_CONFIGURED) {
4065 sv->agent.state |= CHK_ST_ENABLED;
4066 altered_servers++;
4067 total_servers++;
4068 }
4069 break;
4070 case ST_ADM_ACTION_ARUNN:
4071 if (sv->agent.state & CHK_ST_ENABLED) {
4072 sv->agent.health = sv->agent.rise + sv->agent.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02004073 srv_set_running(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01004074 altered_servers++;
4075 total_servers++;
4076 }
4077 break;
4078 case ST_ADM_ACTION_ADOWN:
4079 if (sv->agent.state & CHK_ST_ENABLED) {
4080 sv->agent.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02004081 srv_set_stopped(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01004082 altered_servers++;
4083 total_servers++;
4084 }
4085 break;
4086 case ST_ADM_ACTION_READY:
4087 srv_adm_set_ready(sv);
4088 altered_servers++;
4089 total_servers++;
4090 break;
4091 case ST_ADM_ACTION_DRAIN:
4092 srv_adm_set_drain(sv);
4093 altered_servers++;
4094 total_servers++;
4095 break;
4096 case ST_ADM_ACTION_MAINT:
4097 srv_adm_set_maint(sv);
4098 altered_servers++;
4099 total_servers++;
4100 break;
4101 case ST_ADM_ACTION_SHUTDOWN:
Christopher Fauletdfd10ab2021-10-06 14:24:19 +02004102 if (!(px->flags & (PR_FL_DISABLED|PR_FL_STOPPED))) {
Willy Tarreauaf7ea812019-11-14 16:42:04 +01004103 srv_shutdown_streams(sv, SF_ERR_KILLED);
William Lallemand74c24fb2016-11-21 17:18:36 +01004104 altered_servers++;
4105 total_servers++;
4106 }
4107 break;
4108 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004109 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
William Lallemand74c24fb2016-11-21 17:18:36 +01004110 } else {
4111 /* the server name is unknown or ambiguous (duplicate names) */
4112 total_servers++;
4113 }
4114 }
4115 if (reprocess && px && action) {
4116 /* Now, we know the backend and the action chosen by the user.
4117 * We can safely restart from the first server parameter
4118 * to reprocess them
4119 */
4120 cur_param = st_cur_param;
4121 next_param = st_next_param;
4122 reprocess = 0;
4123 goto reprocess_servers;
4124 }
4125
4126 next_param = cur_param;
4127 }
4128 }
4129
4130 if (total_servers == 0) {
4131 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
4132 }
4133 else if (altered_servers == 0) {
4134 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
4135 }
4136 else if (altered_servers == total_servers) {
4137 appctx->ctx.stats.st_code = STAT_STATUS_DONE;
4138 }
4139 else {
4140 appctx->ctx.stats.st_code = STAT_STATUS_PART;
4141 }
4142 out:
4143 return 1;
Christopher Faulet2f9a41d2019-02-27 15:30:57 +01004144 wait:
4145 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
4146 return 0;
Christopher Fauletef779222018-10-31 08:47:01 +01004147}
4148
4149
Christopher Faulet908628c2022-03-25 16:43:49 +01004150static int stats_send_http_headers(struct conn_stream *cs, struct htx *htx)
Christopher Fauletef779222018-10-31 08:47:01 +01004151{
Christopher Faulet908628c2022-03-25 16:43:49 +01004152 struct stream *s = __cs_strm(cs);
Christopher Fauletef779222018-10-31 08:47:01 +01004153 struct uri_auth *uri = s->be->uri_auth;
Christopher Faulet908628c2022-03-25 16:43:49 +01004154 struct appctx *appctx = __cs_appctx(cs);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004155 struct htx_sl *sl;
4156 unsigned int flags;
Christopher Fauletef779222018-10-31 08:47:01 +01004157
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004158 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);
4159 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), ist("200"), ist("OK"));
4160 if (!sl)
Christopher Fauletef779222018-10-31 08:47:01 +01004161 goto full;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004162 sl->info.res.status = 200;
Christopher Fauletef779222018-10-31 08:47:01 +01004163
Christopher Fauletb829f4c2019-03-29 16:13:55 +01004164 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")))
Christopher Fauletef779222018-10-31 08:47:01 +01004165 goto full;
4166 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
4167 if (!htx_add_header(htx, ist("Content-Type"), ist("text/html")))
4168 goto full;
4169 }
Christopher Faulet6338a082019-09-09 15:50:54 +02004170 else if (appctx->ctx.stats.flags & (STAT_FMT_JSON|STAT_JSON_SCHM)) {
4171 if (!htx_add_header(htx, ist("Content-Type"), ist("application/json")))
4172 goto full;
4173 }
Christopher Fauletef779222018-10-31 08:47:01 +01004174 else {
4175 if (!htx_add_header(htx, ist("Content-Type"), ist("text/plain")))
4176 goto full;
4177 }
4178
4179 if (uri->refresh > 0 && !(appctx->ctx.stats.flags & STAT_NO_REFRESH)) {
4180 const char *refresh = U2A(uri->refresh);
Tim Duesterhusdcf753a2021-03-04 17:31:47 +01004181 if (!htx_add_header(htx, ist("Refresh"), ist(refresh)))
Christopher Fauletef779222018-10-31 08:47:01 +01004182 goto full;
4183 }
4184
4185 if (appctx->ctx.stats.flags & STAT_CHUNKED) {
4186 if (!htx_add_header(htx, ist("Transfer-Encoding"), ist("chunked")))
4187 goto full;
4188 }
4189
4190 if (!htx_add_endof(htx, HTX_BLK_EOH))
4191 goto full;
4192
Christopher Faulet1e2d6362019-02-27 16:28:48 +01004193 channel_add_input(&s->res, htx->data);
Christopher Fauletef779222018-10-31 08:47:01 +01004194 return 1;
4195
4196 full:
4197 htx_reset(htx);
Christopher Fauleta0bdec32022-04-04 07:51:21 +02004198 cs_rx_room_blk(cs);
Christopher Fauletef779222018-10-31 08:47:01 +01004199 return 0;
William Lallemand74c24fb2016-11-21 17:18:36 +01004200}
4201
Christopher Fauletef779222018-10-31 08:47:01 +01004202
Christopher Faulet908628c2022-03-25 16:43:49 +01004203static int stats_send_http_redirect(struct conn_stream *cs, struct htx *htx)
Christopher Fauletef779222018-10-31 08:47:01 +01004204{
4205 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
Christopher Faulet908628c2022-03-25 16:43:49 +01004206 struct stream *s = __cs_strm(cs);
Christopher Fauletef779222018-10-31 08:47:01 +01004207 struct uri_auth *uri = s->be->uri_auth;
Christopher Faulet908628c2022-03-25 16:43:49 +01004208 struct appctx *appctx = __cs_appctx(cs);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004209 struct htx_sl *sl;
4210 unsigned int flags;
Christopher Fauletef779222018-10-31 08:47:01 +01004211
4212 /* scope_txt = search pattern + search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
4213 scope_txt[0] = 0;
4214 if (appctx->ctx.stats.scope_len) {
Christopher Faulet908628c2022-03-25 16:43:49 +01004215 const char *scope_ptr = stats_scope_ptr(appctx, cs);
Christopher Fauleted7a0662019-01-14 11:07:34 +01004216
Christopher Fauletef779222018-10-31 08:47:01 +01004217 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Christopher Fauleted7a0662019-01-14 11:07:34 +01004218 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), scope_ptr, appctx->ctx.stats.scope_len);
Christopher Fauletef779222018-10-31 08:47:01 +01004219 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
4220 }
4221
4222 /* We don't want to land on the posted stats page because a refresh will
4223 * repost the data. We don't want this to happen on accident so we redirect
4224 * the browse to the stats page with a GET.
4225 */
4226 chunk_printf(&trash, "%s;st=%s%s%s%s",
4227 uri->uri_prefix,
4228 ((appctx->ctx.stats.st_code > STAT_STATUS_INIT) &&
4229 (appctx->ctx.stats.st_code < STAT_STATUS_SIZE) &&
4230 stat_status_codes[appctx->ctx.stats.st_code]) ?
4231 stat_status_codes[appctx->ctx.stats.st_code] :
4232 stat_status_codes[STAT_STATUS_UNKN],
4233 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
4234 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
4235 scope_txt);
4236
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004237 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_CHNK);
4238 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), ist("303"), ist("See Other"));
4239 if (!sl)
Christopher Fauletef779222018-10-31 08:47:01 +01004240 goto full;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004241 sl->info.res.status = 303;
Christopher Fauletef779222018-10-31 08:47:01 +01004242
4243 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")) ||
Christopher Fauletef779222018-10-31 08:47:01 +01004244 !htx_add_header(htx, ist("Content-Type"), ist("text/plain")) ||
4245 !htx_add_header(htx, ist("Content-Length"), ist("0")) ||
4246 !htx_add_header(htx, ist("Location"), ist2(trash.area, trash.data)))
4247 goto full;
4248
4249 if (!htx_add_endof(htx, HTX_BLK_EOH))
4250 goto full;
4251
Christopher Faulet1e2d6362019-02-27 16:28:48 +01004252 channel_add_input(&s->res, htx->data);
Christopher Fauletef779222018-10-31 08:47:01 +01004253 return 1;
4254
4255full:
4256 htx_reset(htx);
Christopher Fauleta0bdec32022-04-04 07:51:21 +02004257 cs_rx_room_blk(cs);
Christopher Fauletef779222018-10-31 08:47:01 +01004258 return 0;
4259}
William Lallemand74c24fb2016-11-21 17:18:36 +01004260
Simon Horman05ee2132017-01-04 09:37:25 +01004261
Christopher Faulet6b0a0fb2022-04-04 11:29:28 +02004262/* This I/O handler runs as an applet embedded in a conn-stream. It is
William Lallemand74c24fb2016-11-21 17:18:36 +01004263 * used to send HTTP stats over a TCP socket. The mechanism is very simple.
4264 * appctx->st0 contains the operation in progress (dump, done). The handler
4265 * automatically unregisters itself once transfer is complete.
4266 */
Christopher Fauletb7f88902019-07-15 21:56:43 +02004267static void http_stats_io_handler(struct appctx *appctx)
Christopher Fauletef779222018-10-31 08:47:01 +01004268{
Christopher Faulet908628c2022-03-25 16:43:49 +01004269 struct conn_stream *cs = appctx->owner;
4270 struct stream *s = __cs_strm(cs);
4271 struct channel *req = cs_oc(cs);
4272 struct channel *res = cs_ic(cs);
Christopher Fauletef779222018-10-31 08:47:01 +01004273 struct htx *req_htx, *res_htx;
4274
Amaury Denoyelle072f97e2020-10-05 11:49:37 +02004275 /* only proxy stats are available via http */
4276 appctx->ctx.stats.domain = STATS_DOMAIN_PROXY;
4277
Christopher Fauletef779222018-10-31 08:47:01 +01004278 res_htx = htx_from_buf(&res->buf);
4279
Christopher Faulet62e75742022-03-31 09:16:34 +02004280 if (unlikely(cs->state == CS_ST_DIS || cs->state == CS_ST_CLO))
Christopher Fauletef779222018-10-31 08:47:01 +01004281 goto out;
4282
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05004283 /* Check if the input buffer is available. */
Christopher Fauletef779222018-10-31 08:47:01 +01004284 if (!b_size(&res->buf)) {
Christopher Fauleta0bdec32022-04-04 07:51:21 +02004285 cs_rx_room_blk(cs);
Christopher Fauletef779222018-10-31 08:47:01 +01004286 goto out;
4287 }
4288
4289 /* check that the output is not closed */
Christopher Faulet3a78aa62019-02-27 16:19:48 +01004290 if (res->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_SHUTR))
4291 appctx->st0 = STAT_HTTP_END;
Christopher Fauletef779222018-10-31 08:47:01 +01004292
4293 /* all states are processed in sequence */
4294 if (appctx->st0 == STAT_HTTP_HEAD) {
Christopher Faulet908628c2022-03-25 16:43:49 +01004295 if (stats_send_http_headers(cs, res_htx)) {
Christopher Fauletef779222018-10-31 08:47:01 +01004296 if (s->txn->meth == HTTP_METH_HEAD)
4297 appctx->st0 = STAT_HTTP_DONE;
4298 else
4299 appctx->st0 = STAT_HTTP_DUMP;
4300 }
4301 }
4302
4303 if (appctx->st0 == STAT_HTTP_DUMP) {
Christopher Faulet908628c2022-03-25 16:43:49 +01004304 if (stats_dump_stat_to_buffer(cs, res_htx, s->be->uri_auth))
Christopher Fauletef779222018-10-31 08:47:01 +01004305 appctx->st0 = STAT_HTTP_DONE;
4306 }
4307
4308 if (appctx->st0 == STAT_HTTP_POST) {
Christopher Faulet908628c2022-03-25 16:43:49 +01004309 if (stats_process_http_post(cs))
Christopher Fauletef779222018-10-31 08:47:01 +01004310 appctx->st0 = STAT_HTTP_LAST;
Christopher Faulet3a78aa62019-02-27 16:19:48 +01004311 else if (req->flags & CF_SHUTR)
Christopher Fauletef779222018-10-31 08:47:01 +01004312 appctx->st0 = STAT_HTTP_DONE;
4313 }
4314
4315 if (appctx->st0 == STAT_HTTP_LAST) {
Christopher Faulet908628c2022-03-25 16:43:49 +01004316 if (stats_send_http_redirect(cs, res_htx))
Christopher Fauletef779222018-10-31 08:47:01 +01004317 appctx->st0 = STAT_HTTP_DONE;
4318 }
4319
4320 if (appctx->st0 == STAT_HTTP_DONE) {
Christopher Faulet08b45cb2022-04-07 09:25:13 +02004321 /* no more data are expected. If the response buffer is empty,
4322 * be sure to add something (EOT block in this case) to have
4323 * something to send. It is important to be sure the EOM flags
4324 * will be handled by the endpoint.
4325 */
4326 if (htx_is_empty(res_htx)) {
4327 if (!htx_add_endof(res_htx, HTX_BLK_EOT)) {
Christopher Fauleta0bdec32022-04-04 07:51:21 +02004328 cs_rx_room_blk(cs);
Christopher Faulet08b45cb2022-04-07 09:25:13 +02004329 goto out;
4330 }
4331 channel_add_input(res, 1);
4332 }
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01004333 res_htx->flags |= HTX_FL_EOM;
Christopher Faulet908628c2022-03-25 16:43:49 +01004334 cs->endp->flags |= CS_EP_EOI;
Christopher Faulet3fa5d192022-03-07 15:52:42 +01004335 res->flags |= CF_EOI;
Christopher Faulet3a78aa62019-02-27 16:19:48 +01004336 appctx->st0 = STAT_HTTP_END;
Christopher Fauletef779222018-10-31 08:47:01 +01004337 }
4338
Christopher Faulet3a78aa62019-02-27 16:19:48 +01004339 if (appctx->st0 == STAT_HTTP_END) {
4340 if (!(res->flags & CF_SHUTR)) {
Christopher Fauletef779222018-10-31 08:47:01 +01004341 res->flags |= CF_READ_NULL;
Christopher Fauletda098e62022-03-31 17:44:45 +02004342 cs_shutr(cs);
Christopher Faulet3a78aa62019-02-27 16:19:48 +01004343 }
4344
4345 /* eat the whole request */
4346 if (co_data(req)) {
4347 req_htx = htx_from_buf(&req->buf);
4348 co_htx_skip(req, req_htx, co_data(req));
4349 htx_to_buf(req_htx, &req->buf);
Christopher Fauletef779222018-10-31 08:47:01 +01004350 }
4351 }
Christopher Faulet3a78aa62019-02-27 16:19:48 +01004352
Christopher Fauletef779222018-10-31 08:47:01 +01004353 out:
4354 /* we have left the request in the buffer for the case where we
4355 * process a POST, and this automatically re-enables activity on
4356 * read. It's better to indicate that we want to stop reading when
4357 * we're sending, so that we know there's at most one direction
4358 * deciding to wake the applet up. It saves it from looping when
4359 * emitting large blocks into small TCP windows.
4360 */
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01004361 htx_to_buf(res_htx, &res->buf);
4362 if (!channel_is_empty(res))
Christopher Fauleta0bdec32022-04-04 07:51:21 +02004363 cs_stop_get(cs);
Christopher Fauletef779222018-10-31 08:47:01 +01004364}
4365
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004366/* Dump all fields from <info> into <out> using the "show info" format (name: value) */
Willy Tarreau83061a82018-07-13 11:56:34 +02004367static int stats_dump_info_fields(struct buffer *out,
Willy Tarreau43241ff2019-10-09 11:27:51 +02004368 const struct field *info, unsigned int flags)
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004369{
4370 int field;
4371
4372 for (field = 0; field < INF_TOTAL_FIELDS; field++) {
4373 if (!field_format(info, field))
4374 continue;
4375
Willy Tarreaueaa55372019-10-09 07:39:11 +02004376 if (!chunk_appendf(out, "%s: ", info_fields[field].name))
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004377 return 0;
4378 if (!stats_emit_raw_data_field(out, &info[field]))
4379 return 0;
Willy Tarreau6b19b142019-10-09 15:44:21 +02004380 if ((flags & STAT_SHOW_FDESC) && !chunk_appendf(out, ":\"%s\"", info_fields[field].desc))
4381 return 0;
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004382 if (!chunk_strcat(out, "\n"))
4383 return 0;
4384 }
4385 return 1;
4386}
4387
4388/* Dump all fields from <info> into <out> using the "show info typed" format */
Willy Tarreau83061a82018-07-13 11:56:34 +02004389static int stats_dump_typed_info_fields(struct buffer *out,
Willy Tarreau43241ff2019-10-09 11:27:51 +02004390 const struct field *info, unsigned int flags)
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004391{
4392 int field;
4393
4394 for (field = 0; field < INF_TOTAL_FIELDS; field++) {
4395 if (!field_format(info, field))
4396 continue;
4397
Willy Tarreaueaa55372019-10-09 07:39:11 +02004398 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 +01004399 return 0;
4400 if (!stats_emit_field_tags(out, &info[field], ':'))
4401 return 0;
4402 if (!stats_emit_typed_data_field(out, &info[field]))
4403 return 0;
Willy Tarreau6b19b142019-10-09 15:44:21 +02004404 if ((flags & STAT_SHOW_FDESC) && !chunk_appendf(out, ":\"%s\"", info_fields[field].desc))
4405 return 0;
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004406 if (!chunk_strcat(out, "\n"))
4407 return 0;
4408 }
4409 return 1;
4410}
4411
Willy Tarreau0b26b382021-05-08 07:43:53 +02004412/* Fill <info> with HAProxy global info. <info> is preallocated array of length
4413 * <len>. The length of the array must be INF_TOTAL_FIELDS. If this length is
4414 * less then this value, the function returns 0, otherwise, it returns 1. Some
4415 * fields' presence or precision may depend on some of the STAT_* flags present
4416 * in <flags>.
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004417 */
Willy Tarreau0b26b382021-05-08 07:43:53 +02004418int stats_fill_info(struct field *info, int len, uint flags)
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004419{
Willy Tarreaud37e26e2021-05-08 07:40:52 +02004420 struct timeval up;
Willy Tarreau83061a82018-07-13 11:56:34 +02004421 struct buffer *out = get_trash_chunk();
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004422
4423#ifdef USE_OPENSSL
Willy Tarreauc5977722021-05-08 08:14:04 +02004424 double ssl_sess_rate = read_freq_ctr_flt(&global.ssl_per_sec);
4425 double ssl_key_rate = read_freq_ctr_flt(&global.ssl_fe_keys_per_sec);
4426 double ssl_reuse = 0;
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004427
Willy Tarreauc5977722021-05-08 08:14:04 +02004428 if (ssl_key_rate < ssl_sess_rate)
4429 ssl_reuse = 100.0 * (1.0 - ssl_key_rate / ssl_sess_rate);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004430#endif
4431
Willy Tarreaud37e26e2021-05-08 07:40:52 +02004432 tv_remain(&start_date, &now, &up);
4433
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004434 if (len < INF_TOTAL_FIELDS)
4435 return 0;
4436
4437 chunk_reset(out);
4438 memset(info, 0, sizeof(*info) * len);
4439
4440 info[INF_NAME] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, PRODUCT_NAME);
Willy Tarreau909b9d82019-01-04 18:20:32 +01004441 info[INF_VERSION] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, haproxy_version);
Adis Nezirovicb62b78b2021-01-15 13:12:33 +01004442 info[INF_BUILD_INFO] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, haproxy_version);
Willy Tarreau909b9d82019-01-04 18:20:32 +01004443 info[INF_RELEASE_DATE] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, haproxy_date);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004444
Yves Lafon95317282018-02-26 11:10:37 +01004445 info[INF_NBTHREAD] = mkf_u32(FO_CONFIG|FS_SERVICE, global.nbthread);
Willy Tarreau91358592021-06-15 08:08:04 +02004446 info[INF_NBPROC] = mkf_u32(FO_CONFIG|FS_SERVICE, 1);
Willy Tarreaue8422bf2021-06-15 09:08:18 +02004447 info[INF_PROCESS_NUM] = mkf_u32(FO_KEY, 1);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004448 info[INF_PID] = mkf_u32(FO_STATUS, pid);
4449
4450 info[INF_UPTIME] = mkf_str(FN_DURATION, chunk_newstr(out));
Willy Tarreaud37e26e2021-05-08 07:40:52 +02004451 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 +01004452
Willy Tarreaue8abc322021-05-08 07:56:56 +02004453 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);
4454 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 +01004455 info[INF_MEMMAX_MB] = mkf_u32(FO_CONFIG|FN_LIMIT, global.rlimit_memmax);
William Dauchya8766cf2021-01-15 22:41:37 +01004456 info[INF_MEMMAX_BYTES] = mkf_u32(FO_CONFIG|FN_LIMIT, global.rlimit_memmax * 1048576L);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004457 info[INF_POOL_ALLOC_MB] = mkf_u32(0, (unsigned)(pool_total_allocated() / 1048576L));
William Dauchya8766cf2021-01-15 22:41:37 +01004458 info[INF_POOL_ALLOC_BYTES] = mkf_u32(0, pool_total_allocated());
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004459 info[INF_POOL_USED_MB] = mkf_u32(0, (unsigned)(pool_total_used() / 1048576L));
William Dauchya8766cf2021-01-15 22:41:37 +01004460 info[INF_POOL_USED_BYTES] = mkf_u32(0, pool_total_used());
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004461 info[INF_POOL_FAILED] = mkf_u32(FN_COUNTER, pool_total_failures());
4462 info[INF_ULIMIT_N] = mkf_u32(FO_CONFIG|FN_LIMIT, global.rlimit_nofile);
4463 info[INF_MAXSOCK] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxsock);
4464 info[INF_MAXCONN] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxconn);
4465 info[INF_HARD_MAXCONN] = mkf_u32(FO_CONFIG|FN_LIMIT, global.hardmaxconn);
4466 info[INF_CURR_CONN] = mkf_u32(0, actconn);
4467 info[INF_CUM_CONN] = mkf_u32(FN_COUNTER, totalconn);
4468 info[INF_CUM_REQ] = mkf_u32(FN_COUNTER, global.req_count);
4469#ifdef USE_OPENSSL
4470 info[INF_MAX_SSL_CONNS] = mkf_u32(FN_MAX, global.maxsslconn);
Willy Tarreau82531f62021-10-06 12:15:18 +02004471 info[INF_CURR_SSL_CONNS] = mkf_u32(0, global.sslconns);
4472 info[INF_CUM_SSL_CONNS] = mkf_u32(FN_COUNTER, global.totalsslconns);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004473#endif
4474 info[INF_MAXPIPES] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxpipes);
4475 info[INF_PIPES_USED] = mkf_u32(0, pipes_used);
4476 info[INF_PIPES_FREE] = mkf_u32(0, pipes_free);
Willy Tarreauc5977722021-05-08 08:14:04 +02004477 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 +01004478 info[INF_CONN_RATE_LIMIT] = mkf_u32(FO_CONFIG|FN_LIMIT, global.cps_lim);
4479 info[INF_MAX_CONN_RATE] = mkf_u32(FN_MAX, global.cps_max);
Willy Tarreauc5977722021-05-08 08:14:04 +02004480 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 +01004481 info[INF_SESS_RATE_LIMIT] = mkf_u32(FO_CONFIG|FN_LIMIT, global.sps_lim);
4482 info[INF_MAX_SESS_RATE] = mkf_u32(FN_RATE, global.sps_max);
4483
4484#ifdef USE_OPENSSL
Willy Tarreauc5977722021-05-08 08:14:04 +02004485 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 +01004486 info[INF_SSL_RATE_LIMIT] = mkf_u32(FO_CONFIG|FN_LIMIT, global.ssl_lim);
4487 info[INF_MAX_SSL_RATE] = mkf_u32(FN_MAX, global.ssl_max);
Willy Tarreauc5977722021-05-08 08:14:04 +02004488 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 +01004489 info[INF_SSL_FRONTEND_MAX_KEY_RATE] = mkf_u32(FN_MAX, global.ssl_fe_keys_max);
Willy Tarreauc5977722021-05-08 08:14:04 +02004490 info[INF_SSL_FRONTEND_SESSION_REUSE_PCT] = (flags & STAT_USE_FLOAT) ? mkf_flt(FN_RATE, ssl_reuse) : mkf_u32(0, ssl_reuse);
4491 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 +01004492 info[INF_SSL_BACKEND_MAX_KEY_RATE] = mkf_u32(FN_MAX, global.ssl_be_keys_max);
4493 info[INF_SSL_CACHE_LOOKUPS] = mkf_u32(FN_COUNTER, global.shctx_lookups);
4494 info[INF_SSL_CACHE_MISSES] = mkf_u32(FN_COUNTER, global.shctx_misses);
4495#endif
Willy Tarreauc5977722021-05-08 08:14:04 +02004496 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));
4497 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 +01004498 info[INF_COMPRESS_BPS_RATE_LIM] = mkf_u32(FO_CONFIG|FN_LIMIT, global.comp_rate_lim);
4499#ifdef USE_ZLIB
4500 info[INF_ZLIB_MEM_USAGE] = mkf_u32(0, zlib_used_memory);
4501 info[INF_MAX_ZLIB_MEM_USAGE] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxzlibmem);
4502#endif
Willy Tarreau955a11e2021-02-24 17:03:30 +01004503 info[INF_TASKS] = mkf_u32(0, total_allocated_tasks());
Willy Tarreau9c7b8082021-02-24 15:10:07 +01004504 info[INF_RUN_QUEUE] = mkf_u32(0, total_run_queues());
Willy Tarreauf9d5e102021-10-08 10:43:59 +02004505 info[INF_IDLE_PCT] = mkf_u32(FN_AVG, clock_report_idle());
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004506 info[INF_NODE] = mkf_str(FO_CONFIG|FN_OUTPUT|FS_SERVICE, global.node);
4507 if (global.desc)
4508 info[INF_DESCRIPTION] = mkf_str(FO_CONFIG|FN_OUTPUT|FS_SERVICE, global.desc);
Willy Tarreau00098ea2018-11-05 14:38:13 +01004509 info[INF_STOPPING] = mkf_u32(0, stopping);
4510 info[INF_JOBS] = mkf_u32(0, jobs);
William Lallemanda7199262018-11-16 16:57:20 +01004511 info[INF_UNSTOPPABLE_JOBS] = mkf_u32(0, unstoppable_jobs);
Willy Tarreau00098ea2018-11-05 14:38:13 +01004512 info[INF_LISTENERS] = mkf_u32(0, listeners);
Willy Tarreau199ad242018-11-05 16:31:22 +01004513 info[INF_ACTIVE_PEERS] = mkf_u32(0, active_peers);
Willy Tarreau2d372c22018-11-05 17:12:27 +01004514 info[INF_CONNECTED_PEERS] = mkf_u32(0, connected_peers);
Willy Tarreau13ef7732018-11-12 07:25:28 +01004515 info[INF_DROPPED_LOGS] = mkf_u32(0, dropped_logs);
Willy Tarreaubeb859a2018-11-22 18:07:59 +01004516 info[INF_BUSY_POLLING] = mkf_u32(0, !!(global.tune.options & GTUNE_BUSY_POLLING));
Emeric Brund30e9a12020-12-23 18:49:16 +01004517 info[INF_FAILED_RESOLUTIONS] = mkf_u32(0, resolv_failed_resolutions);
Willy Tarreau7cf0e452019-05-23 11:39:14 +02004518 info[INF_TOTAL_BYTES_OUT] = mkf_u64(0, global.out_bytes);
Christopher Fauletaaa70852020-07-10 13:56:30 +02004519 info[INF_TOTAL_SPLICED_BYTES_OUT] = mkf_u64(0, global.spliced_out_bytes);
Willy Tarreau7cf0e452019-05-23 11:39:14 +02004520 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 +02004521 info[INF_DEBUG_COMMANDS_ISSUED] = mkf_u32(0, debug_commands_issued);
Emeric Brun45c457a2020-07-09 23:23:34 +02004522 info[INF_CUM_LOG_MSGS] = mkf_u32(FN_COUNTER, cum_log_messages);
Amaury Denoyelle5dfdf3e2021-05-05 17:09:12 +02004523
4524 info[INF_TAINTED] = mkf_str(FO_STATUS, chunk_newstr(out));
4525 chunk_appendf(out, "%#x", get_tainted());
4526
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004527 return 1;
4528}
4529
Christopher Faulet908628c2022-03-25 16:43:49 +01004530/* This function dumps information onto the conn-stream's read buffer.
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004531 * It returns 0 as long as it does not complete, non-zero upon completion.
4532 * No state is used.
4533 */
Christopher Faulet908628c2022-03-25 16:43:49 +01004534static int stats_dump_info_to_buffer(struct conn_stream *cs)
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004535{
Christopher Faulet908628c2022-03-25 16:43:49 +01004536 struct appctx *appctx = __cs_appctx(cs);
Christopher Faulet2da02ae2022-02-24 13:45:27 +01004537
Willy Tarreau0b26b382021-05-08 07:43:53 +02004538 if (!stats_fill_info(info, INF_TOTAL_FIELDS, appctx->ctx.stats.flags))
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004539 return 0;
4540
4541 chunk_reset(&trash);
4542
4543 if (appctx->ctx.stats.flags & STAT_FMT_TYPED)
Willy Tarreau43241ff2019-10-09 11:27:51 +02004544 stats_dump_typed_info_fields(&trash, info, appctx->ctx.stats.flags);
Simon Horman05ee2132017-01-04 09:37:25 +01004545 else if (appctx->ctx.stats.flags & STAT_FMT_JSON)
Willy Tarreau43241ff2019-10-09 11:27:51 +02004546 stats_dump_json_info_fields(&trash, info, appctx->ctx.stats.flags);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004547 else
Willy Tarreau43241ff2019-10-09 11:27:51 +02004548 stats_dump_info_fields(&trash, info, appctx->ctx.stats.flags);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004549
Christopher Faulet908628c2022-03-25 16:43:49 +01004550 if (ci_putchk(cs_ic(cs), &trash) == -1) {
Christopher Fauleta0bdec32022-04-04 07:51:21 +02004551 cs_rx_room_blk(cs);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004552 return 0;
4553 }
4554
4555 return 1;
4556}
4557
Christopher Faulet6b0a0fb2022-04-04 11:29:28 +02004558/* This function dumps the schema onto the conn-stream's read buffer.
Simon Horman6f6bb382017-01-04 09:37:26 +01004559 * It returns 0 as long as it does not complete, non-zero upon completion.
4560 * No state is used.
4561 *
Ilya Shipitsinf38a0182020-12-21 01:16:17 +05004562 * Integer values bounded to the range [-(2**53)+1, (2**53)-1] as
Simon Horman6f6bb382017-01-04 09:37:26 +01004563 * per the recommendation for interoperable integers in section 6 of RFC 7159.
4564 */
Willy Tarreau83061a82018-07-13 11:56:34 +02004565static void stats_dump_json_schema(struct buffer *out)
Simon Horman6f6bb382017-01-04 09:37:26 +01004566{
4567
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004568 int old_len = out->data;
Simon Horman6f6bb382017-01-04 09:37:26 +01004569
4570 chunk_strcat(out,
4571 "{"
4572 "\"$schema\":\"http://json-schema.org/draft-04/schema#\","
4573 "\"oneOf\":["
4574 "{"
4575 "\"title\":\"Info\","
4576 "\"type\":\"array\","
4577 "\"items\":{"
Amaury Denoyellea53ce4c2020-10-02 18:31:59 +02004578 "\"title\":\"InfoItem\","
4579 "\"type\":\"object\","
Simon Horman6f6bb382017-01-04 09:37:26 +01004580 "\"properties\":{"
Simon Horman6f6bb382017-01-04 09:37:26 +01004581 "\"field\":{\"$ref\":\"#/definitions/field\"},"
4582 "\"processNum\":{\"$ref\":\"#/definitions/processNum\"},"
4583 "\"tags\":{\"$ref\":\"#/definitions/tags\"},"
4584 "\"value\":{\"$ref\":\"#/definitions/typedValue\"}"
4585 "},"
4586 "\"required\":[\"field\",\"processNum\",\"tags\","
4587 "\"value\"]"
4588 "}"
4589 "},"
4590 "{"
4591 "\"title\":\"Stat\","
4592 "\"type\":\"array\","
4593 "\"items\":{"
4594 "\"title\":\"InfoItem\","
4595 "\"type\":\"object\","
4596 "\"properties\":{"
4597 "\"objType\":{"
4598 "\"enum\":[\"Frontend\",\"Backend\",\"Listener\","
4599 "\"Server\",\"Unknown\"]"
4600 "},"
4601 "\"proxyId\":{"
4602 "\"type\":\"integer\","
4603 "\"minimum\":0"
4604 "},"
4605 "\"id\":{"
4606 "\"type\":\"integer\","
4607 "\"minimum\":0"
4608 "},"
4609 "\"field\":{\"$ref\":\"#/definitions/field\"},"
4610 "\"processNum\":{\"$ref\":\"#/definitions/processNum\"},"
4611 "\"tags\":{\"$ref\":\"#/definitions/tags\"},"
4612 "\"typedValue\":{\"$ref\":\"#/definitions/typedValue\"}"
4613 "},"
4614 "\"required\":[\"objType\",\"proxyId\",\"id\","
4615 "\"field\",\"processNum\",\"tags\","
4616 "\"value\"]"
4617 "}"
4618 "},"
4619 "{"
4620 "\"title\":\"Error\","
4621 "\"type\":\"object\","
4622 "\"properties\":{"
4623 "\"errorStr\":{"
4624 "\"type\":\"string\""
Amaury Denoyellea53ce4c2020-10-02 18:31:59 +02004625 "}"
4626 "},"
4627 "\"required\":[\"errorStr\"]"
Simon Horman6f6bb382017-01-04 09:37:26 +01004628 "}"
4629 "],"
4630 "\"definitions\":{"
4631 "\"field\":{"
4632 "\"type\":\"object\","
4633 "\"pos\":{"
4634 "\"type\":\"integer\","
4635 "\"minimum\":0"
4636 "},"
4637 "\"name\":{"
4638 "\"type\":\"string\""
4639 "},"
4640 "\"required\":[\"pos\",\"name\"]"
4641 "},"
4642 "\"processNum\":{"
4643 "\"type\":\"integer\","
4644 "\"minimum\":1"
4645 "},"
4646 "\"tags\":{"
4647 "\"type\":\"object\","
4648 "\"origin\":{"
4649 "\"type\":\"string\","
4650 "\"enum\":[\"Metric\",\"Status\",\"Key\","
4651 "\"Config\",\"Product\",\"Unknown\"]"
4652 "},"
4653 "\"nature\":{"
4654 "\"type\":\"string\","
4655 "\"enum\":[\"Gauge\",\"Limit\",\"Min\",\"Max\","
4656 "\"Rate\",\"Counter\",\"Duration\","
4657 "\"Age\",\"Time\",\"Name\",\"Output\","
4658 "\"Avg\", \"Unknown\"]"
4659 "},"
4660 "\"scope\":{"
4661 "\"type\":\"string\","
4662 "\"enum\":[\"Cluster\",\"Process\",\"Service\","
4663 "\"System\",\"Unknown\"]"
4664 "},"
4665 "\"required\":[\"origin\",\"nature\",\"scope\"]"
4666 "},"
4667 "\"typedValue\":{"
4668 "\"type\":\"object\","
4669 "\"oneOf\":["
4670 "{\"$ref\":\"#/definitions/typedValue/definitions/s32Value\"},"
4671 "{\"$ref\":\"#/definitions/typedValue/definitions/s64Value\"},"
4672 "{\"$ref\":\"#/definitions/typedValue/definitions/u32Value\"},"
4673 "{\"$ref\":\"#/definitions/typedValue/definitions/u64Value\"},"
4674 "{\"$ref\":\"#/definitions/typedValue/definitions/strValue\"}"
4675 "],"
4676 "\"definitions\":{"
4677 "\"s32Value\":{"
4678 "\"properties\":{"
4679 "\"type\":{"
4680 "\"type\":\"string\","
4681 "\"enum\":[\"s32\"]"
4682 "},"
4683 "\"value\":{"
4684 "\"type\":\"integer\","
4685 "\"minimum\":-2147483648,"
4686 "\"maximum\":2147483647"
4687 "}"
4688 "},"
4689 "\"required\":[\"type\",\"value\"]"
4690 "},"
4691 "\"s64Value\":{"
4692 "\"properties\":{"
4693 "\"type\":{"
4694 "\"type\":\"string\","
4695 "\"enum\":[\"s64\"]"
4696 "},"
4697 "\"value\":{"
4698 "\"type\":\"integer\","
4699 "\"minimum\":-9007199254740991,"
4700 "\"maximum\":9007199254740991"
4701 "}"
4702 "},"
4703 "\"required\":[\"type\",\"value\"]"
4704 "},"
4705 "\"u32Value\":{"
4706 "\"properties\":{"
4707 "\"type\":{"
4708 "\"type\":\"string\","
4709 "\"enum\":[\"u32\"]"
4710 "},"
4711 "\"value\":{"
4712 "\"type\":\"integer\","
4713 "\"minimum\":0,"
4714 "\"maximum\":4294967295"
4715 "}"
4716 "},"
4717 "\"required\":[\"type\",\"value\"]"
4718 "},"
4719 "\"u64Value\":{"
4720 "\"properties\":{"
4721 "\"type\":{"
4722 "\"type\":\"string\","
4723 "\"enum\":[\"u64\"]"
4724 "},"
4725 "\"value\":{"
4726 "\"type\":\"integer\","
4727 "\"minimum\":0,"
4728 "\"maximum\":9007199254740991"
4729 "}"
4730 "},"
4731 "\"required\":[\"type\",\"value\"]"
4732 "},"
4733 "\"strValue\":{"
4734 "\"properties\":{"
4735 "\"type\":{"
4736 "\"type\":\"string\","
4737 "\"enum\":[\"str\"]"
4738 "},"
4739 "\"value\":{\"type\":\"string\"}"
4740 "},"
4741 "\"required\":[\"type\",\"value\"]"
4742 "},"
4743 "\"unknownValue\":{"
4744 "\"properties\":{"
4745 "\"type\":{"
4746 "\"type\":\"integer\","
4747 "\"minimum\":0"
4748 "},"
4749 "\"value\":{"
4750 "\"type\":\"string\","
4751 "\"enum\":[\"unknown\"]"
4752 "}"
4753 "},"
4754 "\"required\":[\"type\",\"value\"]"
4755 "}"
4756 "}"
4757 "}"
4758 "}"
4759 "}");
4760
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004761 if (old_len == out->data) {
Simon Horman6f6bb382017-01-04 09:37:26 +01004762 chunk_reset(out);
4763 chunk_appendf(out,
4764 "{\"errorStr\":\"output buffer too short\"}");
4765 }
4766}
4767
Christopher Faulet6b0a0fb2022-04-04 11:29:28 +02004768/* This function dumps the schema onto the conn-stream's read buffer.
Simon Horman6f6bb382017-01-04 09:37:26 +01004769 * It returns 0 as long as it does not complete, non-zero upon completion.
4770 * No state is used.
4771 */
Christopher Faulet908628c2022-03-25 16:43:49 +01004772static int stats_dump_json_schema_to_buffer(struct conn_stream *cs)
Simon Horman6f6bb382017-01-04 09:37:26 +01004773{
4774 chunk_reset(&trash);
4775
4776 stats_dump_json_schema(&trash);
4777
Christopher Faulet908628c2022-03-25 16:43:49 +01004778 if (ci_putchk(cs_ic(cs), &trash) == -1) {
Christopher Fauleta0bdec32022-04-04 07:51:21 +02004779 cs_rx_room_blk(cs);
Simon Horman6f6bb382017-01-04 09:37:26 +01004780 return 0;
4781 }
4782
4783 return 1;
4784}
4785
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004786static int cli_parse_clear_counters(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau89d467c2016-11-23 11:02:40 +01004787{
4788 struct proxy *px;
4789 struct server *sv;
4790 struct listener *li;
Amaury Denoyelled3700a72020-10-05 11:49:43 +02004791 struct stats_module *mod;
Willy Tarreau89d467c2016-11-23 11:02:40 +01004792 int clrall = 0;
4793
4794 if (strcmp(args[2], "all") == 0)
4795 clrall = 1;
4796
4797 /* check permissions */
4798 if (!cli_has_level(appctx, ACCESS_LVL_OPER) ||
4799 (clrall && !cli_has_level(appctx, ACCESS_LVL_ADMIN)))
4800 return 1;
4801
Olivier Houchardfbc74e82017-11-24 16:54:05 +01004802 for (px = proxies_list; px; px = px->next) {
Willy Tarreau89d467c2016-11-23 11:02:40 +01004803 if (clrall) {
4804 memset(&px->be_counters, 0, sizeof(px->be_counters));
4805 memset(&px->fe_counters, 0, sizeof(px->fe_counters));
4806 }
4807 else {
4808 px->be_counters.conn_max = 0;
4809 px->be_counters.p.http.rps_max = 0;
4810 px->be_counters.sps_max = 0;
4811 px->be_counters.cps_max = 0;
4812 px->be_counters.nbpend_max = 0;
Christopher Fauletefb41f02019-11-08 14:53:15 +01004813 px->be_counters.qtime_max = 0;
4814 px->be_counters.ctime_max = 0;
4815 px->be_counters.dtime_max = 0;
4816 px->be_counters.ttime_max = 0;
Willy Tarreau89d467c2016-11-23 11:02:40 +01004817
4818 px->fe_counters.conn_max = 0;
4819 px->fe_counters.p.http.rps_max = 0;
4820 px->fe_counters.sps_max = 0;
4821 px->fe_counters.cps_max = 0;
Willy Tarreau89d467c2016-11-23 11:02:40 +01004822 }
4823
4824 for (sv = px->srv; sv; sv = sv->next)
4825 if (clrall)
4826 memset(&sv->counters, 0, sizeof(sv->counters));
4827 else {
4828 sv->counters.cur_sess_max = 0;
4829 sv->counters.nbpend_max = 0;
4830 sv->counters.sps_max = 0;
Christopher Fauletefb41f02019-11-08 14:53:15 +01004831 sv->counters.qtime_max = 0;
4832 sv->counters.ctime_max = 0;
4833 sv->counters.dtime_max = 0;
4834 sv->counters.ttime_max = 0;
Willy Tarreau89d467c2016-11-23 11:02:40 +01004835 }
4836
4837 list_for_each_entry(li, &px->conf.listeners, by_fe)
4838 if (li->counters) {
4839 if (clrall)
4840 memset(li->counters, 0, sizeof(*li->counters));
4841 else
4842 li->counters->conn_max = 0;
4843 }
4844 }
4845
4846 global.cps_max = 0;
4847 global.sps_max = 0;
Olivier Houchard00bc3cb2017-10-17 19:23:25 +02004848 global.ssl_max = 0;
4849 global.ssl_fe_keys_max = 0;
4850 global.ssl_be_keys_max = 0;
Willy Tarreaud80cb4e2018-01-20 19:30:13 +01004851
Amaury Denoyelled3700a72020-10-05 11:49:43 +02004852 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
4853 if (!mod->clearable && !clrall)
4854 continue;
4855
4856 for (px = proxies_list; px; px = px->next) {
4857 enum stats_domain_px_cap mod_cap = stats_px_get_cap(mod->domain_flags);
4858
4859 if (px->cap & PR_CAP_FE && mod_cap & STATS_PX_CAP_FE) {
4860 EXTRA_COUNTERS_INIT(px->extra_counters_fe,
4861 mod,
4862 mod->counters,
4863 mod->counters_size);
4864 }
4865
4866 if (px->cap & PR_CAP_BE && mod_cap & STATS_PX_CAP_BE) {
4867 EXTRA_COUNTERS_INIT(px->extra_counters_be,
4868 mod,
4869 mod->counters,
4870 mod->counters_size);
4871 }
4872
4873 if (mod_cap & STATS_PX_CAP_SRV) {
4874 for (sv = px->srv; sv; sv = sv->next) {
4875 EXTRA_COUNTERS_INIT(sv->extra_counters,
4876 mod,
4877 mod->counters,
4878 mod->counters_size);
4879 }
4880 }
4881
4882 if (mod_cap & STATS_PX_CAP_LI) {
4883 list_for_each_entry(li, &px->conf.listeners, by_fe) {
4884 EXTRA_COUNTERS_INIT(li->extra_counters,
4885 mod,
4886 mod->counters,
4887 mod->counters_size);
4888 }
4889 }
4890 }
4891 }
4892
Emeric Brunf8642ee2021-10-29 17:59:18 +02004893 resolv_stats_clear_counters(clrall, &stats_module_list[STATS_DOMAIN_RESOLVERS]);
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02004894
Willy Tarreaud80cb4e2018-01-20 19:30:13 +01004895 memset(activity, 0, sizeof(activity));
Willy Tarreau89d467c2016-11-23 11:02:40 +01004896 return 1;
4897}
4898
4899
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004900static int cli_parse_show_info(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004901{
Willy Tarreau2f397382019-10-09 11:43:59 +02004902 int arg = 2;
4903
Willy Tarreaud25fc792016-12-16 12:33:47 +01004904 appctx->ctx.stats.scope_str = 0;
4905 appctx->ctx.stats.scope_len = 0;
4906 appctx->ctx.stats.flags = 0;
4907
Willy Tarreau2f397382019-10-09 11:43:59 +02004908 while (*args[arg]) {
4909 if (strcmp(args[arg], "typed") == 0)
4910 appctx->ctx.stats.flags = (appctx->ctx.stats.flags & ~STAT_FMT_MASK) | STAT_FMT_TYPED;
4911 else if (strcmp(args[arg], "json") == 0)
4912 appctx->ctx.stats.flags = (appctx->ctx.stats.flags & ~STAT_FMT_MASK) | STAT_FMT_JSON;
4913 else if (strcmp(args[arg], "desc") == 0)
4914 appctx->ctx.stats.flags |= STAT_SHOW_FDESC;
Willy Tarreau27456202021-05-08 07:54:24 +02004915 else if (strcmp(args[arg], "float") == 0)
4916 appctx->ctx.stats.flags |= STAT_USE_FLOAT;
Willy Tarreau2f397382019-10-09 11:43:59 +02004917 arg++;
4918 }
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004919 return 0;
4920}
4921
4922
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004923static int cli_parse_show_stat(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau2b812e22016-11-22 16:18:05 +01004924{
Willy Tarreau2f397382019-10-09 11:43:59 +02004925 int arg = 2;
4926
Willy Tarreaud25fc792016-12-16 12:33:47 +01004927 appctx->ctx.stats.scope_str = 0;
4928 appctx->ctx.stats.scope_len = 0;
Willy Tarreau578d6e42019-10-09 11:00:22 +02004929 appctx->ctx.stats.flags = STAT_SHNODE | STAT_SHDESC;
4930
Christopher Faulet908628c2022-03-25 16:43:49 +01004931 if ((strm_li(__cs_strm(appctx->owner))->bind_conf->level & ACCESS_LVL_MASK) >= ACCESS_LVL_OPER)
Willy Tarreau578d6e42019-10-09 11:00:22 +02004932 appctx->ctx.stats.flags |= STAT_SHLGNDS;
Willy Tarreaud25fc792016-12-16 12:33:47 +01004933
Amaury Denoyelle072f97e2020-10-05 11:49:37 +02004934 /* proxy is the default domain */
4935 appctx->ctx.stats.domain = STATS_DOMAIN_PROXY;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01004936 if (strcmp(args[arg], "domain") == 0) {
Amaury Denoyelle072f97e2020-10-05 11:49:37 +02004937 ++args;
4938
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01004939 if (strcmp(args[arg], "proxy") == 0) {
Amaury Denoyelle072f97e2020-10-05 11:49:37 +02004940 ++args;
Emeric Brunf8642ee2021-10-29 17:59:18 +02004941 } else if (strcmp(args[arg], "resolvers") == 0) {
4942 appctx->ctx.stats.domain = STATS_DOMAIN_RESOLVERS;
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02004943 ++args;
4944 } else {
Amaury Denoyelle072f97e2020-10-05 11:49:37 +02004945 return cli_err(appctx, "Invalid statistics domain.\n");
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02004946 }
Amaury Denoyelle072f97e2020-10-05 11:49:37 +02004947 }
4948
4949 if (appctx->ctx.stats.domain == STATS_DOMAIN_PROXY
4950 && *args[arg] && *args[arg+1] && *args[arg+2]) {
Willy Tarreaua1b1ed52016-11-25 08:50:58 +01004951 struct proxy *px;
4952
Willy Tarreau2f397382019-10-09 11:43:59 +02004953 px = proxy_find_by_name(args[arg], 0, 0);
Willy Tarreaua1b1ed52016-11-25 08:50:58 +01004954 if (px)
4955 appctx->ctx.stats.iid = px->uuid;
4956 else
Willy Tarreau2f397382019-10-09 11:43:59 +02004957 appctx->ctx.stats.iid = atoi(args[arg]);
Willy Tarreaua1b1ed52016-11-25 08:50:58 +01004958
Willy Tarreau9d008692019-08-09 11:21:01 +02004959 if (!appctx->ctx.stats.iid)
4960 return cli_err(appctx, "No such proxy.\n");
Willy Tarreaua1b1ed52016-11-25 08:50:58 +01004961
Willy Tarreau2b812e22016-11-22 16:18:05 +01004962 appctx->ctx.stats.flags |= STAT_BOUND;
Willy Tarreau2f397382019-10-09 11:43:59 +02004963 appctx->ctx.stats.type = atoi(args[arg+1]);
4964 appctx->ctx.stats.sid = atoi(args[arg+2]);
4965 arg += 3;
4966 }
4967
4968 while (*args[arg]) {
4969 if (strcmp(args[arg], "typed") == 0)
4970 appctx->ctx.stats.flags = (appctx->ctx.stats.flags & ~STAT_FMT_MASK) | STAT_FMT_TYPED;
4971 else if (strcmp(args[arg], "json") == 0)
4972 appctx->ctx.stats.flags = (appctx->ctx.stats.flags & ~STAT_FMT_MASK) | STAT_FMT_JSON;
4973 else if (strcmp(args[arg], "desc") == 0)
4974 appctx->ctx.stats.flags |= STAT_SHOW_FDESC;
Willy Tarreau3e320362020-10-23 17:28:57 +02004975 else if (strcmp(args[arg], "no-maint") == 0)
4976 appctx->ctx.stats.flags |= STAT_HIDE_MAINT;
Willy Tarreau65141ff2020-10-23 17:19:48 +02004977 else if (strcmp(args[arg], "up") == 0)
4978 appctx->ctx.stats.flags |= STAT_HIDE_DOWN;
Willy Tarreau2f397382019-10-09 11:43:59 +02004979 arg++;
Willy Tarreau2b812e22016-11-22 16:18:05 +01004980 }
Willy Tarreau2b812e22016-11-22 16:18:05 +01004981
Willy Tarreau2b812e22016-11-22 16:18:05 +01004982 return 0;
4983}
4984
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004985static int cli_io_handler_dump_info(struct appctx *appctx)
4986{
Christopher Faulet908628c2022-03-25 16:43:49 +01004987 return stats_dump_info_to_buffer(appctx->owner);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004988}
4989
Christopher Faulet908628c2022-03-25 16:43:49 +01004990/* This I/O handler runs as an applet embedded in a conn-stream. It is
Willy Tarreau2b812e22016-11-22 16:18:05 +01004991 * used to send raw stats over a socket.
4992 */
4993static int cli_io_handler_dump_stat(struct appctx *appctx)
4994{
Christopher Faulet908628c2022-03-25 16:43:49 +01004995 return stats_dump_stat_to_buffer(appctx->owner, NULL, NULL);
Willy Tarreau2b812e22016-11-22 16:18:05 +01004996}
4997
Simon Horman6f6bb382017-01-04 09:37:26 +01004998static int cli_io_handler_dump_json_schema(struct appctx *appctx)
4999{
Christopher Faulet908628c2022-03-25 16:43:49 +01005000 return stats_dump_json_schema_to_buffer(appctx->owner);
Simon Horman6f6bb382017-01-04 09:37:26 +01005001}
5002
Amaury Denoyelle216a1ce2021-03-18 15:48:14 +01005003int stats_allocate_proxy_counters_internal(struct extra_counters **counters,
5004 int type, int px_cap)
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02005005{
5006 struct stats_module *mod;
5007
5008 EXTRA_COUNTERS_REGISTER(counters, type, alloc_failed);
5009
5010 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
5011 if (!(stats_px_get_cap(mod->domain_flags) & px_cap))
5012 continue;
5013
5014 EXTRA_COUNTERS_ADD(mod, *counters, mod->counters, mod->counters_size);
5015 }
5016
5017 EXTRA_COUNTERS_ALLOC(*counters, alloc_failed);
5018
5019 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
5020 if (!(stats_px_get_cap(mod->domain_flags) & px_cap))
5021 continue;
5022
5023 EXTRA_COUNTERS_INIT(*counters, mod, mod->counters, mod->counters_size);
5024 }
5025
5026 return 1;
5027
5028 alloc_failed:
5029 return 0;
5030}
5031
5032/* Initialize and allocate all extra counters for a proxy and its attached
5033 * servers/listeners with all already registered stats module
5034 */
5035int stats_allocate_proxy_counters(struct proxy *px)
5036{
5037 struct server *sv;
5038 struct listener *li;
5039
5040 if (px->cap & PR_CAP_FE) {
5041 if (!stats_allocate_proxy_counters_internal(&px->extra_counters_fe,
5042 COUNTERS_FE,
5043 STATS_PX_CAP_FE)) {
5044 return 0;
5045 }
5046 }
5047
5048 if (px->cap & PR_CAP_BE) {
5049 if (!stats_allocate_proxy_counters_internal(&px->extra_counters_be,
5050 COUNTERS_BE,
5051 STATS_PX_CAP_BE)) {
5052 return 0;
5053 }
5054 }
5055
5056 for (sv = px->srv; sv; sv = sv->next) {
5057 if (!stats_allocate_proxy_counters_internal(&sv->extra_counters,
5058 COUNTERS_SV,
5059 STATS_PX_CAP_SRV)) {
5060 return 0;
5061 }
5062 }
5063
5064 list_for_each_entry(li, &px->conf.listeners, by_fe) {
5065 if (!stats_allocate_proxy_counters_internal(&li->extra_counters,
5066 COUNTERS_LI,
5067 STATS_PX_CAP_LI)) {
5068 return 0;
5069 }
5070 }
5071
5072 return 1;
5073}
5074
Amaury Denoyelle58d395e2020-10-05 11:49:40 +02005075void stats_register_module(struct stats_module *m)
5076{
5077 const uint8_t domain = stats_get_domain(m->domain_flags);
5078
Willy Tarreau2b718102021-04-21 07:32:39 +02005079 LIST_APPEND(&stats_module_list[domain], &m->list);
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02005080 stat_count[domain] += m->stats_count;
Amaury Denoyelle58d395e2020-10-05 11:49:40 +02005081}
5082
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02005083static int allocate_stats_px_postcheck(void)
5084{
5085 struct stats_module *mod;
5086 size_t i = ST_F_TOTAL_FIELDS;
5087 int err_code = 0;
Amaury Denoyelle27373f72020-10-05 16:57:33 +02005088 struct proxy *px;
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02005089
5090 stat_count[STATS_DOMAIN_PROXY] += ST_F_TOTAL_FIELDS;
5091
5092 stat_f[STATS_DOMAIN_PROXY] = malloc(stat_count[STATS_DOMAIN_PROXY] * sizeof(struct name_desc));
5093 if (!stat_f[STATS_DOMAIN_PROXY]) {
5094 ha_alert("stats: cannot allocate all fields for proxy statistics\n");
5095 err_code |= ERR_ALERT | ERR_FATAL;
5096 return err_code;
5097 }
5098
5099 memcpy(stat_f[STATS_DOMAIN_PROXY], stat_fields,
5100 ST_F_TOTAL_FIELDS * sizeof(struct name_desc));
5101
5102 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
5103 memcpy(stat_f[STATS_DOMAIN_PROXY] + i,
5104 mod->stats,
5105 mod->stats_count * sizeof(struct name_desc));
5106 i += mod->stats_count;
5107 }
5108
Amaury Denoyelle27373f72020-10-05 16:57:33 +02005109 for (px = proxies_list; px; px = px->next) {
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02005110 if (!stats_allocate_proxy_counters(px)) {
5111 ha_alert("stats: cannot allocate all counters for proxy statistics\n");
5112 err_code |= ERR_ALERT | ERR_FATAL;
5113 return err_code;
5114 }
5115 }
5116
Christopher Fauletde79cd22021-01-06 07:41:56 +01005117 /* wait per-thread alloc to perform corresponding stat_l allocation */
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02005118
5119 return err_code;
5120}
5121
5122REGISTER_CONFIG_POSTPARSER("allocate-stats-px", allocate_stats_px_postcheck);
5123
Emeric Brunf8642ee2021-10-29 17:59:18 +02005124static int allocate_stats_rslv_postcheck(void)
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02005125{
5126 struct stats_module *mod;
5127 size_t i = 0;
5128 int err_code = 0;
5129
Emeric Brunf8642ee2021-10-29 17:59:18 +02005130 stat_f[STATS_DOMAIN_RESOLVERS] = malloc(stat_count[STATS_DOMAIN_RESOLVERS] * sizeof(struct name_desc));
5131 if (!stat_f[STATS_DOMAIN_RESOLVERS]) {
5132 ha_alert("stats: cannot allocate all fields for resolver statistics\n");
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02005133 err_code |= ERR_ALERT | ERR_FATAL;
5134 return err_code;
5135 }
5136
Emeric Brunf8642ee2021-10-29 17:59:18 +02005137 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_RESOLVERS], list) {
5138 memcpy(stat_f[STATS_DOMAIN_RESOLVERS] + i,
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02005139 mod->stats,
5140 mod->stats_count * sizeof(struct name_desc));
5141 i += mod->stats_count;
5142 }
5143
Emeric Brunf8642ee2021-10-29 17:59:18 +02005144 if (!resolv_allocate_counters(&stats_module_list[STATS_DOMAIN_RESOLVERS])) {
5145 ha_alert("stats: cannot allocate all counters for resolver statistics\n");
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02005146 err_code |= ERR_ALERT | ERR_FATAL;
5147 return err_code;
5148 }
5149
Christopher Fauletde79cd22021-01-06 07:41:56 +01005150 /* wait per-thread alloc to perform corresponding stat_l allocation */
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02005151
5152 return err_code;
5153}
5154
Emeric Brunf8642ee2021-10-29 17:59:18 +02005155REGISTER_CONFIG_POSTPARSER("allocate-stats-resolver", allocate_stats_rslv_postcheck);
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02005156
Christopher Fauletde79cd22021-01-06 07:41:56 +01005157static int allocate_stat_lines_per_thread(void)
5158{
Emeric Brunf8642ee2021-10-29 17:59:18 +02005159 int domains[] = { STATS_DOMAIN_PROXY, STATS_DOMAIN_RESOLVERS }, i;
Christopher Fauletde79cd22021-01-06 07:41:56 +01005160
5161 for (i = 0; i < STATS_DOMAIN_COUNT; ++i) {
5162 const int domain = domains[i];
5163
5164 stat_l[domain] = malloc(stat_count[domain] * sizeof(struct field));
5165 if (!stat_l[domain])
5166 return 0;
5167 }
5168 return 1;
5169}
5170
5171REGISTER_PER_THREAD_ALLOC(allocate_stat_lines_per_thread);
5172
Amaury Denoyelle7f8f6cb2020-11-10 14:24:31 +01005173static int allocate_trash_counters(void)
5174{
5175 struct stats_module *mod;
Emeric Brunf8642ee2021-10-29 17:59:18 +02005176 int domains[] = { STATS_DOMAIN_PROXY, STATS_DOMAIN_RESOLVERS }, i;
Amaury Denoyelle7f8f6cb2020-11-10 14:24:31 +01005177 size_t max_counters_size = 0;
5178
5179 /* calculate the greatest counters used by any stats modules */
5180 for (i = 0; i < STATS_DOMAIN_COUNT; ++i) {
5181 list_for_each_entry(mod, &stats_module_list[domains[i]], list) {
5182 max_counters_size = mod->counters_size > max_counters_size ?
5183 mod->counters_size : max_counters_size;
5184 }
5185 }
5186
5187 /* allocate the trash with the size of the greatest counters */
5188 if (max_counters_size) {
5189 trash_counters = malloc(max_counters_size);
5190 if (!trash_counters) {
5191 ha_alert("stats: cannot allocate trash counters for statistics\n");
5192 return 0;
5193 }
5194 }
5195
5196 return 1;
5197}
5198
5199REGISTER_PER_THREAD_ALLOC(allocate_trash_counters);
5200
Christopher Fauletde79cd22021-01-06 07:41:56 +01005201static void deinit_stat_lines_per_thread(void)
Amaury Denoyellea2a68992020-11-10 14:24:30 +01005202{
Emeric Brunf8642ee2021-10-29 17:59:18 +02005203 int domains[] = { STATS_DOMAIN_PROXY, STATS_DOMAIN_RESOLVERS }, i;
Amaury Denoyellea2a68992020-11-10 14:24:30 +01005204
5205 for (i = 0; i < STATS_DOMAIN_COUNT; ++i) {
5206 const int domain = domains[i];
5207
Willy Tarreau61cfdf42021-02-20 10:46:51 +01005208 ha_free(&stat_l[domain]);
Christopher Fauletde79cd22021-01-06 07:41:56 +01005209 }
5210}
5211
5212
5213REGISTER_PER_THREAD_FREE(deinit_stat_lines_per_thread);
5214
5215static void deinit_stats(void)
5216{
Emeric Brunf8642ee2021-10-29 17:59:18 +02005217 int domains[] = { STATS_DOMAIN_PROXY, STATS_DOMAIN_RESOLVERS }, i;
Christopher Fauletde79cd22021-01-06 07:41:56 +01005218
5219 for (i = 0; i < STATS_DOMAIN_COUNT; ++i) {
5220 const int domain = domains[i];
Amaury Denoyellea2a68992020-11-10 14:24:30 +01005221
5222 if (stat_f[domain])
5223 free(stat_f[domain]);
5224 }
5225}
5226
5227REGISTER_POST_DEINIT(deinit_stats);
5228
Amaury Denoyelle7f8f6cb2020-11-10 14:24:31 +01005229static void free_trash_counters(void)
5230{
5231 if (trash_counters)
5232 free(trash_counters);
5233}
5234
5235REGISTER_PER_THREAD_FREE(free_trash_counters);
5236
Willy Tarreau2b812e22016-11-22 16:18:05 +01005237/* register cli keywords */
5238static struct cli_kw_list cli_kws = {{ },{
Willy Tarreaub205bfd2021-05-07 11:38:37 +02005239 { { "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 +02005240 { { "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 +02005241 { { "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 },
5242 { { "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 +01005243 {{},}
5244}};
5245
Willy Tarreau0108d902018-11-25 19:14:37 +01005246INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
5247
William Lallemand74c24fb2016-11-21 17:18:36 +01005248struct applet http_stats_applet = {
5249 .obj_type = OBJ_TYPE_APPLET,
5250 .name = "<STATS>", /* used for logging */
5251 .fct = http_stats_io_handler,
5252 .release = NULL,
5253};
5254
William Lallemand74c24fb2016-11-21 17:18:36 +01005255/*
5256 * Local variables:
5257 * c-indent-level: 8
5258 * c-basic-offset: 8
5259 * End:
5260 */