blob: d918ee68e1ee73e1457a8f0a78f81e286ea725b8 [file] [log] [blame]
William Lallemand74c24fb2016-11-21 17:18:36 +01001/*
2 * Functions dedicated to statistics output and the stats socket
3 *
4 * Copyright 2000-2012 Willy Tarreau <w@1wt.eu>
5 * Copyright 2007-2009 Krzysztof Piotr Oledzki <ole@ans.pl>
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version
10 * 2 of the License, or (at your option) any later version.
11 *
12 */
13
14#include <ctype.h>
15#include <errno.h>
William Lallemand74c24fb2016-11-21 17:18:36 +010016#include <stdio.h>
17#include <stdlib.h>
18#include <string.h>
19#include <pwd.h>
20#include <grp.h>
21
22#include <sys/socket.h>
23#include <sys/stat.h>
24#include <sys/types.h>
25
Willy Tarreaub2551052020-06-09 09:07:15 +020026#include <haproxy/api.h>
Willy Tarreau5d9ddc52021-10-06 19:54:09 +020027#include <haproxy/activity.h>
Willy Tarreau3f0f82e2020-06-04 19:42:41 +020028#include <haproxy/applet-t.h>
Willy Tarreau49801602020-06-04 22:50:02 +020029#include <haproxy/backend.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020030#include <haproxy/base64.h>
Willy Tarreau6be78492020-06-05 00:00:29 +020031#include <haproxy/cfgparse.h>
Willy Tarreauf1d32c42020-06-04 21:07:02 +020032#include <haproxy/channel.h>
Willy Tarreau4aa573d2020-06-04 18:21:56 +020033#include <haproxy/check.h>
Willy Tarreau83487a82020-06-04 20:19:54 +020034#include <haproxy/cli.h>
Willy Tarreau55542642021-10-08 09:33:24 +020035#include <haproxy/clock.h>
Willy Tarreau0a3bd392020-06-04 08:52:38 +020036#include <haproxy/compression.h>
Christopher Faulet908628c2022-03-25 16:43:49 +010037#include <haproxy/conn_stream.h>
38#include <haproxy/cs_utils.h>
Willy Tarreau2a83d602020-05-27 16:58:08 +020039#include <haproxy/debug.h>
Willy Tarreau36979d92020-06-05 17:27:29 +020040#include <haproxy/errors.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020041#include <haproxy/fd.h>
42#include <haproxy/freq_ctr.h>
Willy Tarreau762d7a52020-06-04 11:23:07 +020043#include <haproxy/frontend.h>
Willy Tarreauf268ee82020-06-04 17:05:57 +020044#include <haproxy/global.h>
Willy Tarreaucd72d8c2020-06-02 19:11:26 +020045#include <haproxy/http.h>
Willy Tarreaub7fc4c42021-10-06 18:56:42 +020046#include <haproxy/http_ana.h>
Willy Tarreau87735332020-06-04 09:08:41 +020047#include <haproxy/http_htx.h>
Willy Tarreau16f958c2020-06-03 08:44:35 +020048#include <haproxy/htx.h>
Willy Tarreau853b2972020-05-27 18:01:47 +020049#include <haproxy/list.h>
Willy Tarreau36979d92020-06-05 17:27:29 +020050#include <haproxy/listener.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020051#include <haproxy/log.h>
Willy Tarreau2cd58092020-06-04 15:10:43 +020052#include <haproxy/map-t.h>
Willy Tarreau225a90a2020-06-04 15:06:28 +020053#include <haproxy/pattern-t.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020054#include <haproxy/pipe.h>
55#include <haproxy/pool.h>
Willy Tarreaua264d962020-06-04 22:29:18 +020056#include <haproxy/proxy.h>
Emeric Brunc9437992021-02-12 19:42:55 +010057#include <haproxy/resolvers.h>
Willy Tarreau1e56f922020-06-04 23:20:13 +020058#include <haproxy/server.h>
Willy Tarreau48d25b32020-06-04 18:58:52 +020059#include <haproxy/session.h>
Willy Tarreau2eec9b52020-06-04 19:58:55 +020060#include <haproxy/stats.h>
Willy Tarreaudfd3de82020-06-04 23:46:14 +020061#include <haproxy/stream.h>
Willy Tarreaucea0e1b2020-06-04 17:25:40 +020062#include <haproxy/task.h>
Willy Tarreauc2f7c582020-06-02 18:15:32 +020063#include <haproxy/ticks.h>
Willy Tarreau92b4f132020-06-01 11:05:15 +020064#include <haproxy/time.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020065#include <haproxy/tools.h>
Willy Tarreau8c42b8a2020-06-04 19:27:34 +020066#include <haproxy/uri_auth-t.h>
Willy Tarreaud6788052020-05-27 15:59:00 +020067#include <haproxy/version.h>
William Lallemand74c24fb2016-11-21 17:18:36 +010068
William Lallemand74c24fb2016-11-21 17:18:36 +010069
Christopher Faulet2b9b6782019-02-27 16:42:58 +010070/* status codes available for the stats admin page (strictly 4 chars length) */
71const char *stat_status_codes[STAT_STATUS_SIZE] = {
72 [STAT_STATUS_DENY] = "DENY",
73 [STAT_STATUS_DONE] = "DONE",
74 [STAT_STATUS_ERRP] = "ERRP",
75 [STAT_STATUS_EXCD] = "EXCD",
76 [STAT_STATUS_NONE] = "NONE",
77 [STAT_STATUS_PART] = "PART",
78 [STAT_STATUS_UNKN] = "UNKN",
79 [STAT_STATUS_IVAL] = "IVAL",
80};
81
Willy Tarreau0baac8c2016-11-22 16:36:53 +010082/* These are the field names for each INF_* field position. Please pay attention
83 * to always use the exact same name except that the strings for new names must
84 * be lower case or CamelCase while the enum entries must be upper case.
85 */
Willy Tarreaueaa55372019-10-09 07:39:11 +020086const struct name_desc info_fields[INF_TOTAL_FIELDS] = {
Willy Tarreau6d4897e2019-10-11 16:31:46 +020087 [INF_NAME] = { .name = "Name", .desc = "Product name" },
88 [INF_VERSION] = { .name = "Version", .desc = "Product version" },
89 [INF_RELEASE_DATE] = { .name = "Release_date", .desc = "Date of latest source code update" },
90 [INF_NBTHREAD] = { .name = "Nbthread", .desc = "Number of started threads (global.nbthread)" },
Willy Tarreau91358592021-06-15 08:08:04 +020091 [INF_NBPROC] = { .name = "Nbproc", .desc = "Number of started worker processes (historical, always 1)" },
Willy Tarreaue8422bf2021-06-15 09:08:18 +020092 [INF_PROCESS_NUM] = { .name = "Process_num", .desc = "Relative worker process number (1)" },
Willy Tarreau6d4897e2019-10-11 16:31:46 +020093 [INF_PID] = { .name = "Pid", .desc = "This worker process identifier for the system" },
94 [INF_UPTIME] = { .name = "Uptime", .desc = "How long ago this worker process was started (days+hours+minutes+seconds)" },
95 [INF_UPTIME_SEC] = { .name = "Uptime_sec", .desc = "How long ago this worker process was started (seconds)" },
William Dauchydefd1562021-01-15 22:41:38 +010096 [INF_START_TIME_SEC] = { .name = "Start_time_sec", .desc = "Start time in seconds" },
Willy Tarreau6d4897e2019-10-11 16:31:46 +020097 [INF_MEMMAX_MB] = { .name = "Memmax_MB", .desc = "Worker process's hard limit on memory usage in MB (-m on command line)" },
William Dauchya8766cf2021-01-15 22:41:37 +010098 [INF_MEMMAX_BYTES] = { .name = "Memmax_bytes", .desc = "Worker process's hard limit on memory usage in byes (-m on command line)" },
Willy Tarreau6d4897e2019-10-11 16:31:46 +020099 [INF_POOL_ALLOC_MB] = { .name = "PoolAlloc_MB", .desc = "Amount of memory allocated in pools (in MB)" },
William Dauchya8766cf2021-01-15 22:41:37 +0100100 [INF_POOL_ALLOC_BYTES] = { .name = "PoolAlloc_bytes", .desc = "Amount of memory allocated in pools (in bytes)" },
Willy Tarreau6d4897e2019-10-11 16:31:46 +0200101 [INF_POOL_USED_MB] = { .name = "PoolUsed_MB", .desc = "Amount of pool memory currently used (in MB)" },
William Dauchya8766cf2021-01-15 22:41:37 +0100102 [INF_POOL_USED_BYTES] = { .name = "PoolUsed_bytes", .desc = "Amount of pool memory currently used (in bytes)" },
Willy Tarreau6d4897e2019-10-11 16:31:46 +0200103 [INF_POOL_FAILED] = { .name = "PoolFailed", .desc = "Number of failed pool allocations since this worker was started" },
104 [INF_ULIMIT_N] = { .name = "Ulimit-n", .desc = "Hard limit on the number of per-process file descriptors" },
105 [INF_MAXSOCK] = { .name = "Maxsock", .desc = "Hard limit on the number of per-process sockets" },
106 [INF_MAXCONN] = { .name = "Maxconn", .desc = "Hard limit on the number of per-process connections (configured or imposed by Ulimit-n)" },
107 [INF_HARD_MAXCONN] = { .name = "Hard_maxconn", .desc = "Hard limit on the number of per-process connections (imposed by Memmax_MB or Ulimit-n)" },
108 [INF_CURR_CONN] = { .name = "CurrConns", .desc = "Current number of connections on this worker process" },
109 [INF_CUM_CONN] = { .name = "CumConns", .desc = "Total number of connections on this worker process since started" },
110 [INF_CUM_REQ] = { .name = "CumReq", .desc = "Total number of requests on this worker process since started" },
111 [INF_MAX_SSL_CONNS] = { .name = "MaxSslConns", .desc = "Hard limit on the number of per-process SSL endpoints (front+back), 0=unlimited" },
112 [INF_CURR_SSL_CONNS] = { .name = "CurrSslConns", .desc = "Current number of SSL endpoints on this worker process (front+back)" },
113 [INF_CUM_SSL_CONNS] = { .name = "CumSslConns", .desc = "Total number of SSL endpoints on this worker process since started (front+back)" },
114 [INF_MAXPIPES] = { .name = "Maxpipes", .desc = "Hard limit on the number of pipes for splicing, 0=unlimited" },
115 [INF_PIPES_USED] = { .name = "PipesUsed", .desc = "Current number of pipes in use in this worker process" },
116 [INF_PIPES_FREE] = { .name = "PipesFree", .desc = "Current number of allocated and available pipes in this worker process" },
117 [INF_CONN_RATE] = { .name = "ConnRate", .desc = "Number of front connections created on this worker process over the last second" },
118 [INF_CONN_RATE_LIMIT] = { .name = "ConnRateLimit", .desc = "Hard limit for ConnRate (global.maxconnrate)" },
119 [INF_MAX_CONN_RATE] = { .name = "MaxConnRate", .desc = "Highest ConnRate reached on this worker process since started (in connections per second)" },
120 [INF_SESS_RATE] = { .name = "SessRate", .desc = "Number of sessions created on this worker process over the last second" },
121 [INF_SESS_RATE_LIMIT] = { .name = "SessRateLimit", .desc = "Hard limit for SessRate (global.maxsessrate)" },
122 [INF_MAX_SESS_RATE] = { .name = "MaxSessRate", .desc = "Highest SessRate reached on this worker process since started (in sessions per second)" },
123 [INF_SSL_RATE] = { .name = "SslRate", .desc = "Number of SSL connections created on this worker process over the last second" },
124 [INF_SSL_RATE_LIMIT] = { .name = "SslRateLimit", .desc = "Hard limit for SslRate (global.maxsslrate)" },
125 [INF_MAX_SSL_RATE] = { .name = "MaxSslRate", .desc = "Highest SslRate reached on this worker process since started (in connections per second)" },
126 [INF_SSL_FRONTEND_KEY_RATE] = { .name = "SslFrontendKeyRate", .desc = "Number of SSL keys created on frontends in this worker process over the last second" },
127 [INF_SSL_FRONTEND_MAX_KEY_RATE] = { .name = "SslFrontendMaxKeyRate", .desc = "Highest SslFrontendKeyRate reached on this worker process since started (in SSL keys per second)" },
128 [INF_SSL_FRONTEND_SESSION_REUSE_PCT] = { .name = "SslFrontendSessionReuse_pct", .desc = "Percent of frontend SSL connections which did not require a new key" },
129 [INF_SSL_BACKEND_KEY_RATE] = { .name = "SslBackendKeyRate", .desc = "Number of SSL keys created on backends in this worker process over the last second" },
130 [INF_SSL_BACKEND_MAX_KEY_RATE] = { .name = "SslBackendMaxKeyRate", .desc = "Highest SslBackendKeyRate reached on this worker process since started (in SSL keys per second)" },
131 [INF_SSL_CACHE_LOOKUPS] = { .name = "SslCacheLookups", .desc = "Total number of SSL session ID lookups in the SSL session cache on this worker since started" },
132 [INF_SSL_CACHE_MISSES] = { .name = "SslCacheMisses", .desc = "Total number of SSL session ID lookups that didn't find a session in the SSL session cache on this worker since started" },
133 [INF_COMPRESS_BPS_IN] = { .name = "CompressBpsIn", .desc = "Number of bytes submitted to the HTTP compressor in this worker process over the last second" },
134 [INF_COMPRESS_BPS_OUT] = { .name = "CompressBpsOut", .desc = "Number of bytes emitted by the HTTP compressor in this worker process over the last second" },
135 [INF_COMPRESS_BPS_RATE_LIM] = { .name = "CompressBpsRateLim", .desc = "Limit of CompressBpsOut beyond which HTTP compression is automatically disabled" },
136 [INF_ZLIB_MEM_USAGE] = { .name = "ZlibMemUsage", .desc = "Amount of memory currently used by HTTP compression on the current worker process (in bytes)" },
137 [INF_MAX_ZLIB_MEM_USAGE] = { .name = "MaxZlibMemUsage", .desc = "Limit on the amount of memory used by HTTP compression above which it is automatically disabled (in bytes, see global.maxzlibmem)" },
138 [INF_TASKS] = { .name = "Tasks", .desc = "Total number of tasks in the current worker process (active + sleeping)" },
139 [INF_RUN_QUEUE] = { .name = "Run_queue", .desc = "Total number of active tasks+tasklets in the current worker process" },
140 [INF_IDLE_PCT] = { .name = "Idle_pct", .desc = "Percentage of last second spent waiting in the current worker thread" },
141 [INF_NODE] = { .name = "node", .desc = "Node name (global.node)" },
142 [INF_DESCRIPTION] = { .name = "description", .desc = "Node description (global.description)" },
143 [INF_STOPPING] = { .name = "Stopping", .desc = "1 if the worker process is currently stopping, otherwise zero" },
144 [INF_JOBS] = { .name = "Jobs", .desc = "Current number of active jobs on the current worker process (frontend connections, master connections, listeners)" },
145 [INF_UNSTOPPABLE_JOBS] = { .name = "Unstoppable Jobs", .desc = "Current number of unstoppable jobs on the current worker process (master connections)" },
146 [INF_LISTENERS] = { .name = "Listeners", .desc = "Current number of active listeners on the current worker process" },
147 [INF_ACTIVE_PEERS] = { .name = "ActivePeers", .desc = "Current number of verified active peers connections on the current worker process" },
148 [INF_CONNECTED_PEERS] = { .name = "ConnectedPeers", .desc = "Current number of peers having passed the connection step on the current worker process" },
149 [INF_DROPPED_LOGS] = { .name = "DroppedLogs", .desc = "Total number of dropped logs for current worker process since started" },
150 [INF_BUSY_POLLING] = { .name = "BusyPolling", .desc = "1 if busy-polling is currently in use on the worker process, otherwise zero (config.busy-polling)" },
151 [INF_FAILED_RESOLUTIONS] = { .name = "FailedResolutions", .desc = "Total number of failed DNS resolutions in current worker process since started" },
152 [INF_TOTAL_BYTES_OUT] = { .name = "TotalBytesOut", .desc = "Total number of bytes emitted by current worker process since started" },
Christopher Fauletaaa70852020-07-10 13:56:30 +0200153 [INF_TOTAL_SPLICED_BYTES_OUT] = { .name = "TotalSplicdedBytesOut", .desc = "Total number of bytes emitted by current worker process through a kernel pipe since started" },
Willy Tarreau6d4897e2019-10-11 16:31:46 +0200154 [INF_BYTES_OUT_RATE] = { .name = "BytesOutRate", .desc = "Number of bytes emitted by current worker process over the last second" },
Willy Tarreau9b013702019-10-24 18:18:02 +0200155 [INF_DEBUG_COMMANDS_ISSUED] = { .name = "DebugCommandsIssued", .desc = "Number of debug commands issued on this process (anything > 0 is unsafe)" },
Emeric Brun45c457a2020-07-09 23:23:34 +0200156 [INF_CUM_LOG_MSGS] = { .name = "CumRecvLogs", .desc = "Total number of log messages received by log-forwarding listeners on this worker process since started" },
Adis Nezirovicb62b78b2021-01-15 13:12:33 +0100157 [INF_BUILD_INFO] = { .name = "Build info", .desc = "Build info" },
Amaury Denoyelle5dfdf3e2021-05-05 17:09:12 +0200158 [INF_TAINTED] = { .name = "Tainted", .desc = "Experimental features used" },
Willy Tarreau0baac8c2016-11-22 16:36:53 +0100159};
160
Willy Tarreaueaa55372019-10-09 07:39:11 +0200161const struct name_desc stat_fields[ST_F_TOTAL_FIELDS] = {
Willy Tarreau6d4897e2019-10-11 16:31:46 +0200162 [ST_F_PXNAME] = { .name = "pxname", .desc = "Proxy name" },
163 [ST_F_SVNAME] = { .name = "svname", .desc = "Server name" },
William Dauchyeedb9b12021-02-01 13:11:53 +0100164 [ST_F_QCUR] = { .name = "qcur", .desc = "Number of current queued connections" },
165 [ST_F_QMAX] = { .name = "qmax", .desc = "Highest value of queued connections encountered since process started" },
William Dauchy19f7cfc2021-02-01 13:11:54 +0100166 [ST_F_SCUR] = { .name = "scur", .desc = "Number of current sessions on the frontend, backend or server" },
167 [ST_F_SMAX] = { .name = "smax", .desc = "Highest value of current sessions encountered since process started" },
Willy Tarreau6d4897e2019-10-11 16:31:46 +0200168 [ST_F_SLIM] = { .name = "slim", .desc = "Frontend/listener/server's maxconn, backend's fullconn" },
169 [ST_F_STOT] = { .name = "stot", .desc = "Total number of sessions since process started" },
170 [ST_F_BIN] = { .name = "bin", .desc = "Total number of request bytes since process started" },
171 [ST_F_BOUT] = { .name = "bout", .desc = "Total number of response bytes since process started" },
172 [ST_F_DREQ] = { .name = "dreq", .desc = "Total number of denied requests since process started" },
173 [ST_F_DRESP] = { .name = "dresp", .desc = "Total number of denied responses since process started" },
174 [ST_F_EREQ] = { .name = "ereq", .desc = "Total number of invalid requests since process started" },
175 [ST_F_ECON] = { .name = "econ", .desc = "Total number of failed connections to server since the worker process started" },
176 [ST_F_ERESP] = { .name = "eresp", .desc = "Total number of invalid responses since the worker process started" },
177 [ST_F_WRETR] = { .name = "wretr", .desc = "Total number of server connection retries since the worker process started" },
178 [ST_F_WREDIS] = { .name = "wredis", .desc = "Total number of server redispatches due to connection failures since the worker process started" },
179 [ST_F_STATUS] = { .name = "status", .desc = "Frontend/listen status: OPEN/WAITING/FULL/STOP; backend: UP/DOWN; server: last check status" },
Willy Tarreaubd715102020-10-23 22:44:30 +0200180 [ST_F_WEIGHT] = { .name = "weight", .desc = "Server's effective weight, or sum of active servers' effective weights for a backend" },
Willy Tarreau6d4897e2019-10-11 16:31:46 +0200181 [ST_F_ACT] = { .name = "act", .desc = "Total number of active UP servers with a non-zero weight" },
182 [ST_F_BCK] = { .name = "bck", .desc = "Total number of backup UP servers with a non-zero weight" },
183 [ST_F_CHKFAIL] = { .name = "chkfail", .desc = "Total number of failed individual health checks per server/backend, since the worker process started" },
184 [ST_F_CHKDOWN] = { .name = "chkdown", .desc = "Total number of failed checks causing UP to DOWN server transitions, per server/backend, since the worker process started" },
185 [ST_F_LASTCHG] = { .name = "lastchg", .desc = "How long ago the last server state changed, in seconds" },
186 [ST_F_DOWNTIME] = { .name = "downtime", .desc = "Total time spent in DOWN state, for server or backend" },
187 [ST_F_QLIMIT] = { .name = "qlimit", .desc = "Limit on the number of connections in queue, for servers only (maxqueue argument)" },
Willy Tarreaue8422bf2021-06-15 09:08:18 +0200188 [ST_F_PID] = { .name = "pid", .desc = "Relative worker process number (1)" },
Willy Tarreau6d4897e2019-10-11 16:31:46 +0200189 [ST_F_IID] = { .name = "iid", .desc = "Frontend or Backend numeric identifier ('id' setting)" },
190 [ST_F_SID] = { .name = "sid", .desc = "Server numeric identifier ('id' setting)" },
191 [ST_F_THROTTLE] = { .name = "throttle", .desc = "Throttling ratio applied to a server's maxconn and weight during the slowstart period (0 to 100%)" },
192 [ST_F_LBTOT] = { .name = "lbtot", .desc = "Total number of requests routed by load balancing since the worker process started (ignores queue pop and stickiness)" },
193 [ST_F_TRACKED] = { .name = "tracked", .desc = "Name of the other server this server tracks for its state" },
194 [ST_F_TYPE] = { .name = "type", .desc = "Type of the object (Listener, Frontend, Backend, Server)" },
195 [ST_F_RATE] = { .name = "rate", .desc = "Total number of sessions processed by this object over the last second (sessions for listeners/frontends, requests for backends/servers)" },
196 [ST_F_RATE_LIM] = { .name = "rate_lim", .desc = "Limit on the number of sessions accepted in a second (frontend only, 'rate-limit sessions' setting)" },
William Dauchy19f7cfc2021-02-01 13:11:54 +0100197 [ST_F_RATE_MAX] = { .name = "rate_max", .desc = "Highest value of sessions per second observed since the worker process started" },
Willy Tarreau6d4897e2019-10-11 16:31:46 +0200198 [ST_F_CHECK_STATUS] = { .name = "check_status", .desc = "Status report of the server's latest health check, prefixed with '*' if a check is currently in progress" },
199 [ST_F_CHECK_CODE] = { .name = "check_code", .desc = "HTTP/SMTP/LDAP status code reported by the latest server health check" },
200 [ST_F_CHECK_DURATION] = { .name = "check_duration", .desc = "Total duration of the latest server health check, in milliseconds" },
201 [ST_F_HRSP_1XX] = { .name = "hrsp_1xx", .desc = "Total number of HTTP responses with status 100-199 returned by this object since the worker process started" },
202 [ST_F_HRSP_2XX] = { .name = "hrsp_2xx", .desc = "Total number of HTTP responses with status 200-299 returned by this object since the worker process started" },
203 [ST_F_HRSP_3XX] = { .name = "hrsp_3xx", .desc = "Total number of HTTP responses with status 300-399 returned by this object since the worker process started" },
204 [ST_F_HRSP_4XX] = { .name = "hrsp_4xx", .desc = "Total number of HTTP responses with status 400-499 returned by this object since the worker process started" },
205 [ST_F_HRSP_5XX] = { .name = "hrsp_5xx", .desc = "Total number of HTTP responses with status 500-599 returned by this object since the worker process started" },
206 [ST_F_HRSP_OTHER] = { .name = "hrsp_other", .desc = "Total number of HTTP responses with status <100, >599 returned by this object since the worker process started (error -1 included)" },
207 [ST_F_HANAFAIL] = { .name = "hanafail", .desc = "Total number of failed checks caused by an 'on-error' directive after an 'observe' condition matched" },
208 [ST_F_REQ_RATE] = { .name = "req_rate", .desc = "Number of HTTP requests processed over the last second on this object" },
William Dauchy19f7cfc2021-02-01 13:11:54 +0100209 [ST_F_REQ_RATE_MAX] = { .name = "req_rate_max", .desc = "Highest value of http requests observed since the worker process started" },
Willy Tarreau6d4897e2019-10-11 16:31:46 +0200210 [ST_F_REQ_TOT] = { .name = "req_tot", .desc = "Total number of HTTP requests processed by this object since the worker process started" },
211 [ST_F_CLI_ABRT] = { .name = "cli_abrt", .desc = "Total number of requests or connections aborted by the client since the worker process started" },
212 [ST_F_SRV_ABRT] = { .name = "srv_abrt", .desc = "Total number of requests or connections aborted by the server since the worker process started" },
213 [ST_F_COMP_IN] = { .name = "comp_in", .desc = "Total number of bytes submitted to the HTTP compressor for this object since the worker process started" },
214 [ST_F_COMP_OUT] = { .name = "comp_out", .desc = "Total number of bytes emitted by the HTTP compressor for this object since the worker process started" },
215 [ST_F_COMP_BYP] = { .name = "comp_byp", .desc = "Total number of bytes that bypassed HTTP compression for this object since the worker process started (CPU/memory/bandwidth limitation)" },
216 [ST_F_COMP_RSP] = { .name = "comp_rsp", .desc = "Total number of HTTP responses that were compressed for this object since the worker process started" },
217 [ST_F_LASTSESS] = { .name = "lastsess", .desc = "How long ago some traffic was seen on this object on this worker process, in seconds" },
218 [ST_F_LAST_CHK] = { .name = "last_chk", .desc = "Short description of the latest health check report for this server (see also check_desc)" },
219 [ST_F_LAST_AGT] = { .name = "last_agt", .desc = "Short description of the latest agent check report for this server (see also agent_desc)" },
220 [ST_F_QTIME] = { .name = "qtime", .desc = "Time spent in the queue, in milliseconds, averaged over the 1024 last requests (backend/server)" },
221 [ST_F_CTIME] = { .name = "ctime", .desc = "Time spent waiting for a connection to complete, in milliseconds, averaged over the 1024 last requests (backend/server)" },
222 [ST_F_RTIME] = { .name = "rtime", .desc = "Time spent waiting for a server response, in milliseconds, averaged over the 1024 last requests (backend/server)" },
223 [ST_F_TTIME] = { .name = "ttime", .desc = "Total request+response time (request+queue+connect+response+processing), in milliseconds, averaged over the 1024 last requests (backend/server)" },
224 [ST_F_AGENT_STATUS] = { .name = "agent_status", .desc = "Status report of the server's latest agent check, prefixed with '*' if a check is currently in progress" },
225 [ST_F_AGENT_CODE] = { .name = "agent_code", .desc = "Status code reported by the latest server agent check" },
226 [ST_F_AGENT_DURATION] = { .name = "agent_duration", .desc = "Total duration of the latest server agent check, in milliseconds" },
227 [ST_F_CHECK_DESC] = { .name = "check_desc", .desc = "Textual description of the latest health check report for this server" },
228 [ST_F_AGENT_DESC] = { .name = "agent_desc", .desc = "Textual description of the latest agent check report for this server" },
229 [ST_F_CHECK_RISE] = { .name = "check_rise", .desc = "Number of successful health checks before declaring a server UP (server 'rise' setting)" },
230 [ST_F_CHECK_FALL] = { .name = "check_fall", .desc = "Number of failed health checks before declaring a server DOWN (server 'fall' setting)" },
231 [ST_F_CHECK_HEALTH] = { .name = "check_health", .desc = "Current server health check level (0..fall-1=DOWN, fall..rise-1=UP)" },
232 [ST_F_AGENT_RISE] = { .name = "agent_rise", .desc = "Number of successful agent checks before declaring a server UP (server 'rise' setting)" },
233 [ST_F_AGENT_FALL] = { .name = "agent_fall", .desc = "Number of failed agent checks before declaring a server DOWN (server 'fall' setting)" },
234 [ST_F_AGENT_HEALTH] = { .name = "agent_health", .desc = "Current server agent check level (0..fall-1=DOWN, fall..rise-1=UP)" },
235 [ST_F_ADDR] = { .name = "addr", .desc = "Server's address:port, shown only if show-legends is set, or at levels oper/admin for the CLI" },
236 [ST_F_COOKIE] = { .name = "cookie", .desc = "Backend's cookie name or Server's cookie value, shown only if show-legends is set, or at levels oper/admin for the CLI" },
237 [ST_F_MODE] = { .name = "mode", .desc = "'mode' setting (tcp/http/health/cli)" },
238 [ST_F_ALGO] = { .name = "algo", .desc = "Backend's load balancing algorithm, shown only if show-legends is set, or at levels oper/admin for the CLI" },
239 [ST_F_CONN_RATE] = { .name = "conn_rate", .desc = "Number of new connections accepted over the last second on the frontend for this worker process" },
William Dauchy19f7cfc2021-02-01 13:11:54 +0100240 [ST_F_CONN_RATE_MAX] = { .name = "conn_rate_max", .desc = "Highest value of connections per second observed since the worker process started" },
Willy Tarreau6d4897e2019-10-11 16:31:46 +0200241 [ST_F_CONN_TOT] = { .name = "conn_tot", .desc = "Total number of new connections accepted on this frontend since the worker process started" },
242 [ST_F_INTERCEPTED] = { .name = "intercepted", .desc = "Total number of HTTP requests intercepted on the frontend (redirects/stats/services) since the worker process started" },
243 [ST_F_DCON] = { .name = "dcon", .desc = "Total number of incoming connections blocked on a listener/frontend by a tcp-request connection rule since the worker process started" },
244 [ST_F_DSES] = { .name = "dses", .desc = "Total number of incoming sessions blocked on a listener/frontend by a tcp-request connection rule since the worker process started" },
245 [ST_F_WREW] = { .name = "wrew", .desc = "Total number of failed HTTP header rewrites since the worker process started" },
246 [ST_F_CONNECT] = { .name = "connect", .desc = "Total number of outgoing connection attempts on this backend/server since the worker process started" },
247 [ST_F_REUSE] = { .name = "reuse", .desc = "Total number of reused connection on this backend/server since the worker process started" },
248 [ST_F_CACHE_LOOKUPS] = { .name = "cache_lookups", .desc = "Total number of HTTP requests looked up in the cache on this frontend/backend since the worker process started" },
249 [ST_F_CACHE_HITS] = { .name = "cache_hits", .desc = "Total number of HTTP requests not found in the cache on this frontend/backend since the worker process started" },
250 [ST_F_SRV_ICUR] = { .name = "srv_icur", .desc = "Current number of idle connections available for reuse on this server" },
251 [ST_F_SRV_ILIM] = { .name = "src_ilim", .desc = "Limit on the number of available idle connections on this server (server 'pool_max_conn' directive)" },
Christopher Faulet0d1c2a62019-11-08 14:59:51 +0100252 [ST_F_QT_MAX] = { .name = "qtime_max", .desc = "Maximum observed time spent in the queue, in milliseconds (backend/server)" },
253 [ST_F_CT_MAX] = { .name = "ctime_max", .desc = "Maximum observed time spent waiting for a connection to complete, in milliseconds (backend/server)" },
254 [ST_F_RT_MAX] = { .name = "rtime_max", .desc = "Maximum observed time spent waiting for a server response, in milliseconds (backend/server)" },
255 [ST_F_TT_MAX] = { .name = "ttime_max", .desc = "Maximum observed total request+response time (request+queue+connect+response+processing), in milliseconds (backend/server)" },
Christopher Faulet0159ee42019-12-16 14:40:39 +0100256 [ST_F_EINT] = { .name = "eint", .desc = "Total number of internal errors since process started"},
Willy Tarreau3bb617c2020-06-29 13:51:05 +0200257 [ST_F_IDLE_CONN_CUR] = { .name = "idle_conn_cur", .desc = "Current number of unsafe idle connections"},
258 [ST_F_SAFE_CONN_CUR] = { .name = "safe_conn_cur", .desc = "Current number of safe idle connections"},
259 [ST_F_USED_CONN_CUR] = { .name = "used_conn_cur", .desc = "Current number of connections in use"},
Willy Tarreaua9fcecb2020-06-29 15:38:53 +0200260 [ST_F_NEED_CONN_EST] = { .name = "need_conn_est", .desc = "Estimated needed number of connections"},
Willy Tarreaubd715102020-10-23 22:44:30 +0200261 [ST_F_UWEIGHT] = { .name = "uweight", .desc = "Server's user weight, or sum of active servers' user weights for a backend" },
William Dauchy42d7c402021-11-07 10:18:47 +0100262 [ST_F_AGG_SRV_CHECK_STATUS] = { .name = "agg_server_check_status", .desc = "Backend's aggregated gauge of servers' state check status" },
William Lallemand74c24fb2016-11-21 17:18:36 +0100263};
264
Willy Tarreau0baac8c2016-11-22 16:36:53 +0100265/* one line of info */
William Dauchy5d9b8f32021-01-11 20:07:49 +0100266THREAD_LOCAL struct field info[INF_TOTAL_FIELDS];
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +0200267
268/* description of statistics (static and dynamic) */
269static struct name_desc *stat_f[STATS_DOMAIN_COUNT];
270static size_t stat_count[STATS_DOMAIN_COUNT];
271
272/* one line for stats */
William Dauchyb9577452021-01-17 18:27:46 +0100273THREAD_LOCAL struct field *stat_l[STATS_DOMAIN_COUNT];
William Lallemand74c24fb2016-11-21 17:18:36 +0100274
Amaury Denoyelle58d395e2020-10-05 11:49:40 +0200275/* list of all registered stats module */
276static struct list stats_module_list[STATS_DOMAIN_COUNT] = {
277 LIST_HEAD_INIT(stats_module_list[STATS_DOMAIN_PROXY]),
Emeric Brunf8642ee2021-10-29 17:59:18 +0200278 LIST_HEAD_INIT(stats_module_list[STATS_DOMAIN_RESOLVERS]),
Amaury Denoyelle58d395e2020-10-05 11:49:40 +0200279};
280
Amaury Denoyelle7f8f6cb2020-11-10 14:24:31 +0100281THREAD_LOCAL void *trash_counters;
282
Amaury Denoyelle072f97e2020-10-05 11:49:37 +0200283static inline uint8_t stats_get_domain(uint32_t domain)
284{
285 return domain >> STATS_DOMAIN & STATS_DOMAIN_MASK;
286}
287
Amaury Denoyelle72b16e52020-10-05 11:49:38 +0200288static inline enum stats_domain_px_cap stats_px_get_cap(uint32_t domain)
289{
290 return domain >> STATS_PX_CAP & STATS_PX_CAP_MASK;
291}
292
Christopher Faulet6338a082019-09-09 15:50:54 +0200293static void stats_dump_json_schema(struct buffer *out);
William Lallemand74c24fb2016-11-21 17:18:36 +0100294
Amaury Denoyelle3ca927e2020-10-02 18:32:00 +0200295int stats_putchk(struct channel *chn, struct htx *htx, struct buffer *chk)
Christopher Fauletef779222018-10-31 08:47:01 +0100296{
297 if (htx) {
Christopher Faulet69fc88c2019-01-07 14:27:53 +0100298 if (chk->data >= channel_htx_recv_max(chn, htx))
299 return 0;
Willy Tarreau0a7ef022019-05-28 10:30:11 +0200300 if (!htx_add_data_atonce(htx, ist2(chk->area, chk->data)))
Christopher Fauletef779222018-10-31 08:47:01 +0100301 return 0;
Christopher Faulet5adbeeb2019-01-02 14:34:39 +0100302 channel_add_input(chn, chk->data);
Christopher Fauletef779222018-10-31 08:47:01 +0100303 chk->data = 0;
Christopher Fauletef779222018-10-31 08:47:01 +0100304 }
305 else {
306 if (ci_putchk(chn, chk) == -1)
307 return 0;
308 }
309 return 1;
310}
Willy Tarreau0baac8c2016-11-22 16:36:53 +0100311
Christopher Faulet908628c2022-03-25 16:43:49 +0100312static const char *stats_scope_ptr(struct appctx *appctx, struct conn_stream *cs)
Christopher Fauleted7a0662019-01-14 11:07:34 +0100313{
Christopher Faulet908628c2022-03-25 16:43:49 +0100314 struct channel *req = cs_oc(cs);
Christopher Fauletb7f88902019-07-15 21:56:43 +0200315 struct htx *htx = htxbuf(&req->buf);
316 struct htx_blk *blk;
317 struct ist uri;
Christopher Fauleted7a0662019-01-14 11:07:34 +0100318
Christopher Fauletb7f88902019-07-15 21:56:43 +0200319 blk = htx_get_head_blk(htx);
Christopher Fauletea009732019-11-18 15:50:25 +0100320 BUG_ON(!blk || htx_get_blk_type(blk) != HTX_BLK_REQ_SL);
Christopher Fauletb7f88902019-07-15 21:56:43 +0200321 ALREADY_CHECKED(blk);
322 uri = htx_sl_req_uri(htx_get_blk_ptr(htx, blk));
323 return uri.ptr + appctx->ctx.stats.scope_str;
Christopher Fauleted7a0662019-01-14 11:07:34 +0100324}
325
William Lallemand74c24fb2016-11-21 17:18:36 +0100326/*
327 * http_stats_io_handler()
328 * -> stats_dump_stat_to_buffer() // same as above, but used for CSV or HTML
329 * -> stats_dump_csv_header() // emits the CSV headers (same as above)
Simon Horman05ee2132017-01-04 09:37:25 +0100330 * -> stats_dump_json_header() // emits the JSON headers (same as above)
William Lallemand74c24fb2016-11-21 17:18:36 +0100331 * -> stats_dump_html_head() // emits the HTML headers
332 * -> stats_dump_html_info() // emits the equivalent of "show info" at the top
333 * -> stats_dump_proxy_to_buffer() // same as above, valid for CSV and HTML
334 * -> stats_dump_html_px_hdr()
335 * -> stats_dump_fe_stats()
336 * -> stats_dump_li_stats()
337 * -> stats_dump_sv_stats()
338 * -> stats_dump_be_stats()
339 * -> stats_dump_html_px_end()
340 * -> stats_dump_html_end() // emits HTML trailer
Simon Horman05ee2132017-01-04 09:37:25 +0100341 * -> stats_dump_json_end() // emits JSON trailer
William Lallemand74c24fb2016-11-21 17:18:36 +0100342 */
343
344
William Lallemand74c24fb2016-11-21 17:18:36 +0100345/* Dumps the stats CSV header to the trash buffer which. The caller is responsible
346 * for clearing it if needed.
347 * NOTE: Some tools happen to rely on the field position instead of its name,
348 * so please only append new fields at the end, never in the middle.
349 */
Amaury Denoyelle50660a82020-10-05 11:49:39 +0200350static void stats_dump_csv_header(enum stats_domain domain)
William Lallemand74c24fb2016-11-21 17:18:36 +0100351{
352 int field;
353
354 chunk_appendf(&trash, "# ");
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +0200355 if (stat_f[domain]) {
356 for (field = 0; field < stat_count[domain]; ++field) {
357 chunk_appendf(&trash, "%s,", stat_f[domain][field].name);
William Lallemand74c24fb2016-11-21 17:18:36 +0100358
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +0200359 /* print special delimiter on proxy stats to mark end of
360 static fields */
361 if (domain == STATS_DOMAIN_PROXY && field + 1 == ST_F_TOTAL_FIELDS)
362 chunk_appendf(&trash, "-,");
Amaury Denoyelle50660a82020-10-05 11:49:39 +0200363 }
364 }
365
William Lallemand74c24fb2016-11-21 17:18:36 +0100366 chunk_appendf(&trash, "\n");
367}
368
William Lallemand74c24fb2016-11-21 17:18:36 +0100369/* Emits a stats field without any surrounding element and properly encoded to
370 * resist CSV output. Returns non-zero on success, 0 if the buffer is full.
371 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200372int stats_emit_raw_data_field(struct buffer *out, const struct field *f)
William Lallemand74c24fb2016-11-21 17:18:36 +0100373{
374 switch (field_format(f, 0)) {
375 case FF_EMPTY: return 1;
376 case FF_S32: return chunk_appendf(out, "%d", f->u.s32);
377 case FF_U32: return chunk_appendf(out, "%u", f->u.u32);
378 case FF_S64: return chunk_appendf(out, "%lld", (long long)f->u.s64);
379 case FF_U64: return chunk_appendf(out, "%llu", (unsigned long long)f->u.u64);
Willy Tarreau065ba312021-05-08 10:38:20 +0200380 case FF_FLT: {
381 size_t prev_data = out->data;
382 out->data = flt_trim(out->area, prev_data, chunk_appendf(out, "%f", f->u.flt));
383 return out->data;
384 }
William Lallemand74c24fb2016-11-21 17:18:36 +0100385 case FF_STR: return csv_enc_append(field_str(f, 0), 1, out) != NULL;
386 default: return chunk_appendf(out, "[INCORRECT_FIELD_TYPE_%08x]", f->type);
387 }
388}
389
Amaury Denoyellee3f576c2020-10-05 11:49:44 +0200390const char *field_to_html_str(const struct field *f)
391{
392 switch (field_format(f, 0)) {
393 case FF_S32: return U2H(f->u.s32);
394 case FF_S64: return U2H(f->u.s64);
395 case FF_U64: return U2H(f->u.u64);
396 case FF_U32: return U2H(f->u.u32);
Willy Tarreau6004fb72021-05-08 07:37:38 +0200397 case FF_FLT: return F2H(f->u.flt);
Amaury Denoyellee3f576c2020-10-05 11:49:44 +0200398 case FF_STR: return field_str(f, 0);
399 case FF_EMPTY:
400 default:
401 return "";
402 }
403}
404
William Lallemand74c24fb2016-11-21 17:18:36 +0100405/* Emits a stats field prefixed with its type. No CSV encoding is prepared, the
406 * output is supposed to be used on its own line. Returns non-zero on success, 0
407 * if the buffer is full.
408 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200409int stats_emit_typed_data_field(struct buffer *out, const struct field *f)
William Lallemand74c24fb2016-11-21 17:18:36 +0100410{
411 switch (field_format(f, 0)) {
412 case FF_EMPTY: return 1;
413 case FF_S32: return chunk_appendf(out, "s32:%d", f->u.s32);
414 case FF_U32: return chunk_appendf(out, "u32:%u", f->u.u32);
415 case FF_S64: return chunk_appendf(out, "s64:%lld", (long long)f->u.s64);
416 case FF_U64: return chunk_appendf(out, "u64:%llu", (unsigned long long)f->u.u64);
Willy Tarreau065ba312021-05-08 10:38:20 +0200417 case FF_FLT: {
418 size_t prev_data = out->data;
419 out->data = flt_trim(out->area, prev_data, chunk_appendf(out, "flt:%f", f->u.flt));
420 return out->data;
421 }
William Lallemand74c24fb2016-11-21 17:18:36 +0100422 case FF_STR: return chunk_appendf(out, "str:%s", field_str(f, 0));
423 default: return chunk_appendf(out, "%08x:?", f->type);
424 }
425}
426
Simon Horman05ee2132017-01-04 09:37:25 +0100427/* Limit JSON integer values to the range [-(2**53)+1, (2**53)-1] as per
428 * the recommendation for interoperable integers in section 6 of RFC 7159.
429 */
430#define JSON_INT_MAX ((1ULL << 53) - 1)
431#define JSON_INT_MIN (0 - JSON_INT_MAX)
432
433/* Emits a stats field value and its type in JSON.
434 * Returns non-zero on success, 0 on error.
435 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200436int stats_emit_json_data_field(struct buffer *out, const struct field *f)
Simon Horman05ee2132017-01-04 09:37:25 +0100437{
438 int old_len;
439 char buf[20];
440 const char *type, *value = buf, *quote = "";
441
442 switch (field_format(f, 0)) {
443 case FF_EMPTY: return 1;
444 case FF_S32: type = "\"s32\"";
445 snprintf(buf, sizeof(buf), "%d", f->u.s32);
446 break;
447 case FF_U32: type = "\"u32\"";
448 snprintf(buf, sizeof(buf), "%u", f->u.u32);
449 break;
450 case FF_S64: type = "\"s64\"";
451 if (f->u.s64 < JSON_INT_MIN || f->u.s64 > JSON_INT_MAX)
452 return 0;
453 type = "\"s64\"";
454 snprintf(buf, sizeof(buf), "%lld", (long long)f->u.s64);
455 break;
456 case FF_U64: if (f->u.u64 > JSON_INT_MAX)
457 return 0;
458 type = "\"u64\"";
459 snprintf(buf, sizeof(buf), "%llu",
460 (unsigned long long) f->u.u64);
Christopher Faulet52c91bb2019-09-28 10:37:31 +0200461 break;
Christopher Faulet88a0db22019-09-24 16:35:10 +0200462 case FF_FLT: type = "\"flt\"";
Willy Tarreau065ba312021-05-08 10:38:20 +0200463 flt_trim(buf, 0, snprintf(buf, sizeof(buf), "%f", f->u.flt));
Simon Horman05ee2132017-01-04 09:37:25 +0100464 break;
465 case FF_STR: type = "\"str\"";
466 value = field_str(f, 0);
467 quote = "\"";
468 break;
469 default: snprintf(buf, sizeof(buf), "%u", f->type);
470 type = buf;
471 value = "unknown";
472 quote = "\"";
473 break;
474 }
475
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200476 old_len = out->data;
Simon Horman05ee2132017-01-04 09:37:25 +0100477 chunk_appendf(out, ",\"value\":{\"type\":%s,\"value\":%s%s%s}",
478 type, quote, value, quote);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200479 return !(old_len == out->data);
Simon Horman05ee2132017-01-04 09:37:25 +0100480}
481
William Lallemand74c24fb2016-11-21 17:18:36 +0100482/* Emits an encoding of the field type on 3 characters followed by a delimiter.
483 * Returns non-zero on success, 0 if the buffer is full.
484 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200485int stats_emit_field_tags(struct buffer *out, const struct field *f,
486 char delim)
William Lallemand74c24fb2016-11-21 17:18:36 +0100487{
488 char origin, nature, scope;
489
490 switch (field_origin(f, 0)) {
491 case FO_METRIC: origin = 'M'; break;
492 case FO_STATUS: origin = 'S'; break;
493 case FO_KEY: origin = 'K'; break;
494 case FO_CONFIG: origin = 'C'; break;
495 case FO_PRODUCT: origin = 'P'; break;
496 default: origin = '?'; break;
497 }
498
499 switch (field_nature(f, 0)) {
500 case FN_GAUGE: nature = 'G'; break;
501 case FN_LIMIT: nature = 'L'; break;
502 case FN_MIN: nature = 'm'; break;
503 case FN_MAX: nature = 'M'; break;
504 case FN_RATE: nature = 'R'; break;
505 case FN_COUNTER: nature = 'C'; break;
506 case FN_DURATION: nature = 'D'; break;
507 case FN_AGE: nature = 'A'; break;
508 case FN_TIME: nature = 'T'; break;
509 case FN_NAME: nature = 'N'; break;
510 case FN_OUTPUT: nature = 'O'; break;
511 case FN_AVG: nature = 'a'; break;
512 default: nature = '?'; break;
513 }
514
515 switch (field_scope(f, 0)) {
516 case FS_PROCESS: scope = 'P'; break;
517 case FS_SERVICE: scope = 'S'; break;
518 case FS_SYSTEM: scope = 's'; break;
519 case FS_CLUSTER: scope = 'C'; break;
520 default: scope = '?'; break;
521 }
522
523 return chunk_appendf(out, "%c%c%c%c", origin, nature, scope, delim);
524}
525
Simon Horman05ee2132017-01-04 09:37:25 +0100526/* Emits an encoding of the field type as JSON.
527 * Returns non-zero on success, 0 if the buffer is full.
528 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200529int stats_emit_json_field_tags(struct buffer *out, const struct field *f)
Simon Horman05ee2132017-01-04 09:37:25 +0100530{
531 const char *origin, *nature, *scope;
532 int old_len;
533
534 switch (field_origin(f, 0)) {
535 case FO_METRIC: origin = "Metric"; break;
536 case FO_STATUS: origin = "Status"; break;
537 case FO_KEY: origin = "Key"; break;
538 case FO_CONFIG: origin = "Config"; break;
539 case FO_PRODUCT: origin = "Product"; break;
540 default: origin = "Unknown"; break;
541 }
542
543 switch (field_nature(f, 0)) {
544 case FN_GAUGE: nature = "Gauge"; break;
545 case FN_LIMIT: nature = "Limit"; break;
546 case FN_MIN: nature = "Min"; break;
547 case FN_MAX: nature = "Max"; break;
548 case FN_RATE: nature = "Rate"; break;
549 case FN_COUNTER: nature = "Counter"; break;
550 case FN_DURATION: nature = "Duration"; break;
551 case FN_AGE: nature = "Age"; break;
552 case FN_TIME: nature = "Time"; break;
553 case FN_NAME: nature = "Name"; break;
554 case FN_OUTPUT: nature = "Output"; break;
555 case FN_AVG: nature = "Avg"; break;
556 default: nature = "Unknown"; break;
557 }
558
559 switch (field_scope(f, 0)) {
560 case FS_PROCESS: scope = "Process"; break;
561 case FS_SERVICE: scope = "Service"; break;
562 case FS_SYSTEM: scope = "System"; break;
563 case FS_CLUSTER: scope = "Cluster"; break;
564 default: scope = "Unknown"; break;
565 }
566
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200567 old_len = out->data;
Simon Horman05ee2132017-01-04 09:37:25 +0100568 chunk_appendf(out, "\"tags\":{"
569 "\"origin\":\"%s\","
570 "\"nature\":\"%s\","
571 "\"scope\":\"%s\""
572 "}", origin, nature, scope);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200573 return !(old_len == out->data);
Simon Horman05ee2132017-01-04 09:37:25 +0100574}
William Lallemand74c24fb2016-11-21 17:18:36 +0100575
576/* Dump all fields from <stats> into <out> using CSV format */
Willy Tarreau83061a82018-07-13 11:56:34 +0200577static int stats_dump_fields_csv(struct buffer *out,
Amaury Denoyelle97323c92020-10-02 18:32:01 +0200578 const struct field *stats, size_t stats_count,
Amaury Denoyelle50660a82020-10-05 11:49:39 +0200579 unsigned int flags,
580 enum stats_domain domain)
William Lallemand74c24fb2016-11-21 17:18:36 +0100581{
582 int field;
583
Amaury Denoyelle50660a82020-10-05 11:49:39 +0200584 for (field = 0; field < stats_count; ++field) {
William Lallemand74c24fb2016-11-21 17:18:36 +0100585 if (!stats_emit_raw_data_field(out, &stats[field]))
586 return 0;
587 if (!chunk_strcat(out, ","))
588 return 0;
Amaury Denoyelle50660a82020-10-05 11:49:39 +0200589
590 /* print special delimiter on proxy stats to mark end of
591 static fields */
592 if (domain == STATS_DOMAIN_PROXY && field + 1 == ST_F_TOTAL_FIELDS) {
593 if (!chunk_strcat(out, "-,"))
594 return 0;
595 }
William Lallemand74c24fb2016-11-21 17:18:36 +0100596 }
Amaury Denoyelle50660a82020-10-05 11:49:39 +0200597
William Lallemand74c24fb2016-11-21 17:18:36 +0100598 chunk_strcat(out, "\n");
599 return 1;
600}
601
602/* Dump all fields from <stats> into <out> using a typed "field:desc:type:value" format */
Willy Tarreau83061a82018-07-13 11:56:34 +0200603static int stats_dump_fields_typed(struct buffer *out,
Amaury Denoyelle97323c92020-10-02 18:32:01 +0200604 const struct field *stats,
605 size_t stats_count,
Amaury Denoyelle072f97e2020-10-05 11:49:37 +0200606 unsigned int flags,
607 enum stats_domain domain)
William Lallemand74c24fb2016-11-21 17:18:36 +0100608{
609 int field;
610
Amaury Denoyelle97323c92020-10-02 18:32:01 +0200611 for (field = 0; field < stats_count; ++field) {
William Lallemand74c24fb2016-11-21 17:18:36 +0100612 if (!stats[field].type)
613 continue;
614
Amaury Denoyelle072f97e2020-10-05 11:49:37 +0200615 switch (domain) {
616 case STATS_DOMAIN_PROXY:
617 chunk_appendf(out, "%c.%u.%u.%d.%s.%u:",
618 stats[ST_F_TYPE].u.u32 == STATS_TYPE_FE ? 'F' :
619 stats[ST_F_TYPE].u.u32 == STATS_TYPE_BE ? 'B' :
620 stats[ST_F_TYPE].u.u32 == STATS_TYPE_SO ? 'L' :
621 stats[ST_F_TYPE].u.u32 == STATS_TYPE_SV ? 'S' :
622 '?',
623 stats[ST_F_IID].u.u32, stats[ST_F_SID].u.u32,
624 field,
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +0200625 stat_f[domain][field].name,
Amaury Denoyelle072f97e2020-10-05 11:49:37 +0200626 stats[ST_F_PID].u.u32);
627 break;
628
Emeric Brunf8642ee2021-10-29 17:59:18 +0200629 case STATS_DOMAIN_RESOLVERS:
630 chunk_appendf(out, "N.%d.%s:", field,
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +0200631 stat_f[domain][field].name);
632 break;
633
Amaury Denoyelle072f97e2020-10-05 11:49:37 +0200634 default:
635 break;
636 }
William Lallemand74c24fb2016-11-21 17:18:36 +0100637
638 if (!stats_emit_field_tags(out, &stats[field], ':'))
639 return 0;
640 if (!stats_emit_typed_data_field(out, &stats[field]))
641 return 0;
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +0200642
Willy Tarreaufc8e4382021-06-17 07:22:27 +0200643 if (flags & STAT_SHOW_FDESC &&
644 !chunk_appendf(out, ":\"%s\"", stat_f[domain][field].desc)) {
Willy Tarreau6b19b142019-10-09 15:44:21 +0200645 return 0;
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +0200646 }
647
William Lallemand74c24fb2016-11-21 17:18:36 +0100648 if (!chunk_strcat(out, "\n"))
649 return 0;
650 }
651 return 1;
652}
653
Simon Horman05ee2132017-01-04 09:37:25 +0100654/* Dump all fields from <stats> into <out> using the "show info json" format */
Willy Tarreau83061a82018-07-13 11:56:34 +0200655static int stats_dump_json_info_fields(struct buffer *out,
Willy Tarreau43241ff2019-10-09 11:27:51 +0200656 const struct field *info, unsigned int flags)
Simon Horman05ee2132017-01-04 09:37:25 +0100657{
658 int field;
659 int started = 0;
660
661 if (!chunk_strcat(out, "["))
662 return 0;
663
664 for (field = 0; field < INF_TOTAL_FIELDS; field++) {
665 int old_len;
666
667 if (!field_format(info, field))
668 continue;
669
670 if (started && !chunk_strcat(out, ","))
671 goto err;
672 started = 1;
673
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200674 old_len = out->data;
Simon Horman05ee2132017-01-04 09:37:25 +0100675 chunk_appendf(out,
676 "{\"field\":{\"pos\":%d,\"name\":\"%s\"},"
677 "\"processNum\":%u,",
Willy Tarreaueaa55372019-10-09 07:39:11 +0200678 field, info_fields[field].name,
Simon Horman05ee2132017-01-04 09:37:25 +0100679 info[INF_PROCESS_NUM].u.u32);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200680 if (old_len == out->data)
Simon Horman05ee2132017-01-04 09:37:25 +0100681 goto err;
682
683 if (!stats_emit_json_field_tags(out, &info[field]))
684 goto err;
685
686 if (!stats_emit_json_data_field(out, &info[field]))
687 goto err;
688
689 if (!chunk_strcat(out, "}"))
690 goto err;
691 }
692
693 if (!chunk_strcat(out, "]"))
694 goto err;
695 return 1;
696
697err:
698 chunk_reset(out);
699 chunk_appendf(out, "{\"errorStr\":\"output buffer too short\"}");
700 return 0;
701}
702
Amaury Denoyellef34017b2020-10-02 18:32:03 +0200703static void stats_print_proxy_field_json(struct buffer *out,
704 const struct field *stat,
705 const char *name,
706 int pos,
707 uint32_t field_type,
708 uint32_t iid,
709 uint32_t sid,
710 uint32_t pid)
711{
712 const char *obj_type;
713 switch (field_type) {
714 case STATS_TYPE_FE: obj_type = "Frontend"; break;
715 case STATS_TYPE_BE: obj_type = "Backend"; break;
716 case STATS_TYPE_SO: obj_type = "Listener"; break;
717 case STATS_TYPE_SV: obj_type = "Server"; break;
718 default: obj_type = "Unknown"; break;
719 }
720
721 chunk_appendf(out,
722 "{"
723 "\"objType\":\"%s\","
724 "\"proxyId\":%u,"
725 "\"id\":%u,"
726 "\"field\":{\"pos\":%d,\"name\":\"%s\"},"
727 "\"processNum\":%u,",
728 obj_type, iid, sid, pos, name, pid);
729}
730
Emeric Brunf8642ee2021-10-29 17:59:18 +0200731static void stats_print_rslv_field_json(struct buffer *out,
732 const struct field *stat,
733 const char *name,
734 int pos)
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +0200735{
736 chunk_appendf(out,
737 "{"
738 "\"field\":{\"pos\":%d,\"name\":\"%s\"},",
739 pos, name);
740}
741
742
Simon Horman05ee2132017-01-04 09:37:25 +0100743/* Dump all fields from <stats> into <out> using a typed "field:desc:type:value" format */
Willy Tarreau83061a82018-07-13 11:56:34 +0200744static int stats_dump_fields_json(struct buffer *out,
Amaury Denoyelle97323c92020-10-02 18:32:01 +0200745 const struct field *stats, size_t stats_count,
Amaury Denoyelle072f97e2020-10-05 11:49:37 +0200746 unsigned int flags,
747 enum stats_domain domain)
Simon Horman05ee2132017-01-04 09:37:25 +0100748{
749 int field;
750 int started = 0;
751
Willy Tarreaub0ce3ad2019-10-09 11:19:29 +0200752 if ((flags & STAT_STARTED) && !chunk_strcat(out, ","))
Simon Horman05ee2132017-01-04 09:37:25 +0100753 return 0;
754 if (!chunk_strcat(out, "["))
755 return 0;
756
Amaury Denoyelle97323c92020-10-02 18:32:01 +0200757 for (field = 0; field < stats_count; field++) {
Simon Horman05ee2132017-01-04 09:37:25 +0100758 int old_len;
759
760 if (!stats[field].type)
761 continue;
762
763 if (started && !chunk_strcat(out, ","))
764 goto err;
765 started = 1;
766
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200767 old_len = out->data;
Amaury Denoyelle072f97e2020-10-05 11:49:37 +0200768 if (domain == STATS_DOMAIN_PROXY) {
769 stats_print_proxy_field_json(out, &stats[field],
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +0200770 stat_f[domain][field].name,
771 field,
Amaury Denoyelle072f97e2020-10-05 11:49:37 +0200772 stats[ST_F_TYPE].u.u32,
773 stats[ST_F_IID].u.u32,
774 stats[ST_F_SID].u.u32,
775 stats[ST_F_PID].u.u32);
Emeric Brunf8642ee2021-10-29 17:59:18 +0200776 } else if (domain == STATS_DOMAIN_RESOLVERS) {
777 stats_print_rslv_field_json(out, &stats[field],
778 stat_f[domain][field].name,
779 field);
Amaury Denoyelle072f97e2020-10-05 11:49:37 +0200780 }
Amaury Denoyellef34017b2020-10-02 18:32:03 +0200781
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200782 if (old_len == out->data)
Simon Horman05ee2132017-01-04 09:37:25 +0100783 goto err;
784
785 if (!stats_emit_json_field_tags(out, &stats[field]))
786 goto err;
787
788 if (!stats_emit_json_data_field(out, &stats[field]))
789 goto err;
790
791 if (!chunk_strcat(out, "}"))
792 goto err;
793 }
794
795 if (!chunk_strcat(out, "]"))
796 goto err;
797
798 return 1;
799
800err:
801 chunk_reset(out);
Willy Tarreaub0ce3ad2019-10-09 11:19:29 +0200802 if (flags & STAT_STARTED)
803 chunk_strcat(out, ",");
Simon Horman05ee2132017-01-04 09:37:25 +0100804 chunk_appendf(out, "{\"errorStr\":\"output buffer too short\"}");
805 return 0;
806}
807
William Lallemand74c24fb2016-11-21 17:18:36 +0100808/* Dump all fields from <stats> into <out> using the HTML format. A column is
Willy Tarreauab02b3f2019-10-09 11:11:46 +0200809 * reserved for the checkbox is STAT_ADMIN is set in <flags>. Some extra info
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +0200810 * are provided if STAT_SHLGNDS is present in <flags>. The statistics from
811 * extra modules are displayed at the end of the lines if STAT_SHMODULES is
812 * present in <flags>.
William Lallemand74c24fb2016-11-21 17:18:36 +0100813 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200814static int stats_dump_fields_html(struct buffer *out,
815 const struct field *stats,
816 unsigned int flags)
William Lallemand74c24fb2016-11-21 17:18:36 +0100817{
Willy Tarreau83061a82018-07-13 11:56:34 +0200818 struct buffer src;
Amaury Denoyellee3f576c2020-10-05 11:49:44 +0200819 struct stats_module *mod;
820 int i = 0, j = 0;
William Lallemand74c24fb2016-11-21 17:18:36 +0100821
822 if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_FE) {
823 chunk_appendf(out,
824 /* name, queue */
825 "<tr class=\"frontend\">");
826
Willy Tarreauab02b3f2019-10-09 11:11:46 +0200827 if (flags & STAT_ADMIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +0100828 /* Column sub-heading for Enable or Disable server */
829 chunk_appendf(out, "<td></td>");
830 }
831
832 chunk_appendf(out,
833 "<td class=ac>"
834 "<a name=\"%s/Frontend\"></a>"
835 "<a class=lfsb href=\"#%s/Frontend\">Frontend</a></td>"
836 "<td colspan=3></td>"
837 "",
838 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_PXNAME));
839
840 chunk_appendf(out,
841 /* sessions rate : current */
842 "<td><u>%s<div class=tips><table class=det>"
843 "<tr><th>Current connection rate:</th><td>%s/s</td></tr>"
844 "<tr><th>Current session rate:</th><td>%s/s</td></tr>"
845 "",
846 U2H(stats[ST_F_RATE].u.u32),
847 U2H(stats[ST_F_CONN_RATE].u.u32),
848 U2H(stats[ST_F_RATE].u.u32));
849
850 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
851 chunk_appendf(out,
852 "<tr><th>Current request rate:</th><td>%s/s</td></tr>",
853 U2H(stats[ST_F_REQ_RATE].u.u32));
854
855 chunk_appendf(out,
856 "</table></div></u></td>"
857 /* sessions rate : max */
858 "<td><u>%s<div class=tips><table class=det>"
859 "<tr><th>Max connection rate:</th><td>%s/s</td></tr>"
860 "<tr><th>Max session rate:</th><td>%s/s</td></tr>"
861 "",
862 U2H(stats[ST_F_RATE_MAX].u.u32),
863 U2H(stats[ST_F_CONN_RATE_MAX].u.u32),
864 U2H(stats[ST_F_RATE_MAX].u.u32));
865
866 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
867 chunk_appendf(out,
868 "<tr><th>Max request rate:</th><td>%s/s</td></tr>",
869 U2H(stats[ST_F_REQ_RATE_MAX].u.u32));
870
871 chunk_appendf(out,
872 "</table></div></u></td>"
873 /* sessions rate : limit */
874 "<td>%s</td>",
875 LIM2A(stats[ST_F_RATE_LIM].u.u32, "-"));
876
877 chunk_appendf(out,
878 /* sessions: current, max, limit, total */
879 "<td>%s</td><td>%s</td><td>%s</td>"
880 "<td><u>%s<div class=tips><table class=det>"
881 "<tr><th>Cum. connections:</th><td>%s</td></tr>"
882 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
883 "",
884 U2H(stats[ST_F_SCUR].u.u32), U2H(stats[ST_F_SMAX].u.u32), U2H(stats[ST_F_SLIM].u.u32),
885 U2H(stats[ST_F_STOT].u.u64),
886 U2H(stats[ST_F_CONN_TOT].u.u64),
887 U2H(stats[ST_F_STOT].u.u64));
888
889 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
890 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0) {
891 chunk_appendf(out,
892 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
893 "<tr><th>- HTTP 1xx responses:</th><td>%s</td></tr>"
894 "<tr><th>- HTTP 2xx responses:</th><td>%s</td></tr>"
895 "<tr><th>&nbsp;&nbsp;Compressed 2xx:</th><td>%s</td><td>(%d%%)</td></tr>"
896 "<tr><th>- HTTP 3xx responses:</th><td>%s</td></tr>"
897 "<tr><th>- HTTP 4xx responses:</th><td>%s</td></tr>"
898 "<tr><th>- HTTP 5xx responses:</th><td>%s</td></tr>"
899 "<tr><th>- other responses:</th><td>%s</td></tr>"
900 "<tr><th>Intercepted requests:</th><td>%s</td></tr>"
Willy Tarreaua1214a52018-12-14 14:00:25 +0100901 "<tr><th>Cache lookups:</th><td>%s</td></tr>"
902 "<tr><th>Cache hits:</th><td>%s</td><td>(%d%%)</td></tr>"
Willy Tarreau1b0f85e2018-05-28 15:12:40 +0200903 "<tr><th>Failed hdr rewrites:</th><td>%s</td></tr>"
Christopher Faulet0159ee42019-12-16 14:40:39 +0100904 "<tr><th>Internal errors:</th><td>%s</td></tr>"
William Lallemand74c24fb2016-11-21 17:18:36 +0100905 "",
906 U2H(stats[ST_F_REQ_TOT].u.u64),
907 U2H(stats[ST_F_HRSP_1XX].u.u64),
908 U2H(stats[ST_F_HRSP_2XX].u.u64),
909 U2H(stats[ST_F_COMP_RSP].u.u64),
910 stats[ST_F_HRSP_2XX].u.u64 ?
911 (int)(100 * stats[ST_F_COMP_RSP].u.u64 / stats[ST_F_HRSP_2XX].u.u64) : 0,
912 U2H(stats[ST_F_HRSP_3XX].u.u64),
913 U2H(stats[ST_F_HRSP_4XX].u.u64),
914 U2H(stats[ST_F_HRSP_5XX].u.u64),
915 U2H(stats[ST_F_HRSP_OTHER].u.u64),
Willy Tarreau1b0f85e2018-05-28 15:12:40 +0200916 U2H(stats[ST_F_INTERCEPTED].u.u64),
Willy Tarreaua1214a52018-12-14 14:00:25 +0100917 U2H(stats[ST_F_CACHE_LOOKUPS].u.u64),
918 U2H(stats[ST_F_CACHE_HITS].u.u64),
919 stats[ST_F_CACHE_LOOKUPS].u.u64 ?
920 (int)(100 * stats[ST_F_CACHE_HITS].u.u64 / stats[ST_F_CACHE_LOOKUPS].u.u64) : 0,
Christopher Faulet0159ee42019-12-16 14:40:39 +0100921 U2H(stats[ST_F_WREW].u.u64),
922 U2H(stats[ST_F_EINT].u.u64));
William Lallemand74c24fb2016-11-21 17:18:36 +0100923 }
924
925 chunk_appendf(out,
926 "</table></div></u></td>"
927 /* sessions: lbtot, lastsess */
928 "<td></td><td></td>"
929 /* bytes : in */
930 "<td>%s</td>"
931 "",
932 U2H(stats[ST_F_BIN].u.u64));
933
934 chunk_appendf(out,
935 /* bytes:out + compression stats (via hover): comp_in, comp_out, comp_byp */
936 "<td>%s%s<div class=tips><table class=det>"
937 "<tr><th>Response bytes in:</th><td>%s</td></tr>"
938 "<tr><th>Compression in:</th><td>%s</td></tr>"
939 "<tr><th>Compression out:</th><td>%s</td><td>(%d%%)</td></tr>"
940 "<tr><th>Compression bypass:</th><td>%s</td></tr>"
941 "<tr><th>Total bytes saved:</th><td>%s</td><td>(%d%%)</td></tr>"
942 "</table></div>%s</td>",
943 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "<u>":"",
944 U2H(stats[ST_F_BOUT].u.u64),
945 U2H(stats[ST_F_BOUT].u.u64),
946 U2H(stats[ST_F_COMP_IN].u.u64),
947 U2H(stats[ST_F_COMP_OUT].u.u64),
948 stats[ST_F_COMP_IN].u.u64 ? (int)(stats[ST_F_COMP_OUT].u.u64 * 100 / stats[ST_F_COMP_IN].u.u64) : 0,
949 U2H(stats[ST_F_COMP_BYP].u.u64),
950 U2H(stats[ST_F_COMP_IN].u.u64 - stats[ST_F_COMP_OUT].u.u64),
951 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,
952 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "</u>":"");
953
954 chunk_appendf(out,
955 /* denied: req, resp */
956 "<td>%s</td><td>%s</td>"
957 /* errors : request, connect, response */
958 "<td>%s</td><td></td><td></td>"
959 /* warnings: retries, redispatches */
960 "<td></td><td></td>"
961 /* server status : reflect frontend status */
962 "<td class=ac>%s</td>"
963 /* rest of server: nothing */
Amaury Denoyellee3f576c2020-10-05 11:49:44 +0200964 "<td class=ac colspan=8></td>"
William Lallemand74c24fb2016-11-21 17:18:36 +0100965 "",
966 U2H(stats[ST_F_DREQ].u.u64), U2H(stats[ST_F_DRESP].u.u64),
967 U2H(stats[ST_F_EREQ].u.u64),
968 field_str(stats, ST_F_STATUS));
Amaury Denoyellee3f576c2020-10-05 11:49:44 +0200969
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +0200970 if (flags & STAT_SHMODULES) {
971 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
Amaury Denoyellee6ee8202020-11-03 15:04:46 +0100972 chunk_appendf(out, "<td>");
973
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +0200974 if (stats_px_get_cap(mod->domain_flags) & STATS_PX_CAP_FE) {
Amaury Denoyellee6ee8202020-11-03 15:04:46 +0100975 chunk_appendf(out,
976 "<u>%s<div class=tips><table class=det>",
977 mod->name);
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +0200978 for (j = 0; j < mod->stats_count; ++j) {
979 chunk_appendf(out,
980 "<tr><th>%s</th><td>%s</td></tr>",
981 mod->stats[j].desc, field_to_html_str(&stats[ST_F_TOTAL_FIELDS + i]));
982 ++i;
983 }
Amaury Denoyellee6ee8202020-11-03 15:04:46 +0100984 chunk_appendf(out, "</table></div></u>");
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +0200985 } else {
986 i += mod->stats_count;
Amaury Denoyellee3f576c2020-10-05 11:49:44 +0200987 }
Amaury Denoyellee3f576c2020-10-05 11:49:44 +0200988
Amaury Denoyellee6ee8202020-11-03 15:04:46 +0100989 chunk_appendf(out, "</td>");
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +0200990 }
Amaury Denoyellee3f576c2020-10-05 11:49:44 +0200991 }
992
993 chunk_appendf(out, "</tr>");
William Lallemand74c24fb2016-11-21 17:18:36 +0100994 }
995 else if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_SO) {
996 chunk_appendf(out, "<tr class=socket>");
Willy Tarreauab02b3f2019-10-09 11:11:46 +0200997 if (flags & STAT_ADMIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +0100998 /* Column sub-heading for Enable or Disable server */
999 chunk_appendf(out, "<td></td>");
1000 }
1001
1002 chunk_appendf(out,
1003 /* frontend name, listener name */
1004 "<td class=ac><a name=\"%s/+%s\"></a>%s"
1005 "<a class=lfsb href=\"#%s/+%s\">%s</a>"
1006 "",
1007 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME),
Willy Tarreau708c4162019-10-09 10:19:16 +02001008 (flags & STAT_SHLGNDS)?"<u>":"",
William Lallemand74c24fb2016-11-21 17:18:36 +01001009 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME), field_str(stats, ST_F_SVNAME));
1010
Willy Tarreau708c4162019-10-09 10:19:16 +02001011 if (flags & STAT_SHLGNDS) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001012 chunk_appendf(out, "<div class=tips>");
1013
Willy Tarreau90807112020-02-25 08:16:33 +01001014 if (isdigit((unsigned char)*field_str(stats, ST_F_ADDR)))
William Lallemand74c24fb2016-11-21 17:18:36 +01001015 chunk_appendf(out, "IPv4: %s, ", field_str(stats, ST_F_ADDR));
1016 else if (*field_str(stats, ST_F_ADDR) == '[')
1017 chunk_appendf(out, "IPv6: %s, ", field_str(stats, ST_F_ADDR));
1018 else if (*field_str(stats, ST_F_ADDR))
1019 chunk_appendf(out, "%s, ", field_str(stats, ST_F_ADDR));
1020
1021 /* id */
1022 chunk_appendf(out, "id: %d</div>", stats[ST_F_SID].u.u32);
1023 }
1024
1025 chunk_appendf(out,
1026 /* queue */
1027 "%s</td><td colspan=3></td>"
1028 /* sessions rate: current, max, limit */
1029 "<td colspan=3>&nbsp;</td>"
1030 /* sessions: current, max, limit, total, lbtot, lastsess */
1031 "<td>%s</td><td>%s</td><td>%s</td>"
1032 "<td>%s</td><td>&nbsp;</td><td>&nbsp;</td>"
1033 /* bytes: in, out */
1034 "<td>%s</td><td>%s</td>"
1035 "",
Willy Tarreau708c4162019-10-09 10:19:16 +02001036 (flags & STAT_SHLGNDS)?"</u>":"",
William Lallemand74c24fb2016-11-21 17:18:36 +01001037 U2H(stats[ST_F_SCUR].u.u32), U2H(stats[ST_F_SMAX].u.u32), U2H(stats[ST_F_SLIM].u.u32),
1038 U2H(stats[ST_F_STOT].u.u64), U2H(stats[ST_F_BIN].u.u64), U2H(stats[ST_F_BOUT].u.u64));
1039
1040 chunk_appendf(out,
1041 /* denied: req, resp */
1042 "<td>%s</td><td>%s</td>"
1043 /* errors: request, connect, response */
1044 "<td>%s</td><td></td><td></td>"
1045 /* warnings: retries, redispatches */
1046 "<td></td><td></td>"
1047 /* server status: reflect listener status */
1048 "<td class=ac>%s</td>"
1049 /* rest of server: nothing */
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001050 "<td class=ac colspan=8></td>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001051 "",
1052 U2H(stats[ST_F_DREQ].u.u64), U2H(stats[ST_F_DRESP].u.u64),
1053 U2H(stats[ST_F_EREQ].u.u64),
1054 field_str(stats, ST_F_STATUS));
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001055
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001056 if (flags & STAT_SHMODULES) {
1057 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001058 chunk_appendf(out, "<td>");
1059
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001060 if (stats_px_get_cap(mod->domain_flags) & STATS_PX_CAP_LI) {
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001061 chunk_appendf(out,
1062 "<u>%s<div class=tips><table class=det>",
1063 mod->name);
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001064 for (j = 0; j < mod->stats_count; ++j) {
1065 chunk_appendf(out,
1066 "<tr><th>%s</th><td>%s</td></tr>",
1067 mod->stats[j].desc, field_to_html_str(&stats[ST_F_TOTAL_FIELDS + i]));
1068 ++i;
1069 }
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001070 chunk_appendf(out, "</table></div></u>");
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001071 } else {
1072 i += mod->stats_count;
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001073 }
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001074
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001075 chunk_appendf(out, "</td>");
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001076 }
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001077 }
1078
1079 chunk_appendf(out, "</tr>");
William Lallemand74c24fb2016-11-21 17:18:36 +01001080 }
1081 else if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_SV) {
1082 const char *style;
1083
1084 /* determine the style to use depending on the server's state,
1085 * its health and weight. There isn't a 1-to-1 mapping between
1086 * state and styles for the cases where the server is (still)
1087 * up. The reason is that we don't want to report nolb and
1088 * drain with the same color.
1089 */
1090
1091 if (strcmp(field_str(stats, ST_F_STATUS), "DOWN") == 0 ||
1092 strcmp(field_str(stats, ST_F_STATUS), "DOWN (agent)") == 0) {
1093 style = "down";
1094 }
Florian Apolloner39272c22021-03-30 13:28:35 +02001095 else if (strncmp(field_str(stats, ST_F_STATUS), "DOWN ", strlen("DOWN ")) == 0) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001096 style = "going_up";
1097 }
Daniel Corbettb4285172020-03-28 12:35:50 -04001098 else if (strcmp(field_str(stats, ST_F_STATUS), "DRAIN") == 0) {
1099 style = "draining";
1100 }
Florian Apolloner39272c22021-03-30 13:28:35 +02001101 else if (strncmp(field_str(stats, ST_F_STATUS), "NOLB ", strlen("NOLB ")) == 0) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001102 style = "going_down";
1103 }
1104 else if (strcmp(field_str(stats, ST_F_STATUS), "NOLB") == 0) {
1105 style = "nolb";
1106 }
1107 else if (strcmp(field_str(stats, ST_F_STATUS), "no check") == 0) {
1108 style = "no_check";
1109 }
1110 else if (!stats[ST_F_CHKFAIL].type ||
1111 stats[ST_F_CHECK_HEALTH].u.u32 == stats[ST_F_CHECK_RISE].u.u32 + stats[ST_F_CHECK_FALL].u.u32 - 1) {
1112 /* no check or max health = UP */
1113 if (stats[ST_F_WEIGHT].u.u32)
1114 style = "up";
1115 else
1116 style = "draining";
1117 }
1118 else {
1119 style = "going_down";
1120 }
1121
Willy Tarreau7b524852020-08-11 10:26:36 +02001122 if (strncmp(field_str(stats, ST_F_STATUS), "MAINT", 5) == 0)
William Lallemand74c24fb2016-11-21 17:18:36 +01001123 chunk_appendf(out, "<tr class=\"maintain\">");
1124 else
1125 chunk_appendf(out,
1126 "<tr class=\"%s_%s\">",
1127 (stats[ST_F_BCK].u.u32) ? "backup" : "active", style);
1128
1129
Willy Tarreauab02b3f2019-10-09 11:11:46 +02001130 if (flags & STAT_ADMIN)
William Lallemand74c24fb2016-11-21 17:18:36 +01001131 chunk_appendf(out,
David Harrigand3db35a2016-12-30 12:12:49 +00001132 "<td><input class='%s-checkbox' type=\"checkbox\" name=\"s\" value=\"%s\"></td>",
1133 field_str(stats, ST_F_PXNAME),
William Lallemand74c24fb2016-11-21 17:18:36 +01001134 field_str(stats, ST_F_SVNAME));
1135
1136 chunk_appendf(out,
1137 "<td class=ac><a name=\"%s/%s\"></a>%s"
1138 "<a class=lfsb href=\"#%s/%s\">%s</a>"
1139 "",
1140 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME),
Willy Tarreau708c4162019-10-09 10:19:16 +02001141 (flags & STAT_SHLGNDS) ? "<u>" : "",
William Lallemand74c24fb2016-11-21 17:18:36 +01001142 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME), field_str(stats, ST_F_SVNAME));
1143
Willy Tarreau708c4162019-10-09 10:19:16 +02001144 if (flags & STAT_SHLGNDS) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001145 chunk_appendf(out, "<div class=tips>");
1146
Willy Tarreau90807112020-02-25 08:16:33 +01001147 if (isdigit((unsigned char)*field_str(stats, ST_F_ADDR)))
William Lallemand74c24fb2016-11-21 17:18:36 +01001148 chunk_appendf(out, "IPv4: %s, ", field_str(stats, ST_F_ADDR));
1149 else if (*field_str(stats, ST_F_ADDR) == '[')
1150 chunk_appendf(out, "IPv6: %s, ", field_str(stats, ST_F_ADDR));
1151 else if (*field_str(stats, ST_F_ADDR))
1152 chunk_appendf(out, "%s, ", field_str(stats, ST_F_ADDR));
1153
1154 /* id */
1155 chunk_appendf(out, "id: %d", stats[ST_F_SID].u.u32);
1156
1157 /* cookie */
1158 if (stats[ST_F_COOKIE].type) {
1159 chunk_appendf(out, ", cookie: '");
1160 chunk_initstr(&src, field_str(stats, ST_F_COOKIE));
1161 chunk_htmlencode(out, &src);
1162 chunk_appendf(out, "'");
1163 }
1164
1165 chunk_appendf(out, "</div>");
1166 }
1167
1168 chunk_appendf(out,
1169 /* queue : current, max, limit */
1170 "%s</td><td>%s</td><td>%s</td><td>%s</td>"
1171 /* sessions rate : current, max, limit */
1172 "<td>%s</td><td>%s</td><td></td>"
1173 "",
Willy Tarreau708c4162019-10-09 10:19:16 +02001174 (flags & STAT_SHLGNDS) ? "</u>" : "",
William Lallemand74c24fb2016-11-21 17:18:36 +01001175 U2H(stats[ST_F_QCUR].u.u32), U2H(stats[ST_F_QMAX].u.u32), LIM2A(stats[ST_F_QLIMIT].u.u32, "-"),
1176 U2H(stats[ST_F_RATE].u.u32), U2H(stats[ST_F_RATE_MAX].u.u32));
1177
1178 chunk_appendf(out,
1179 /* sessions: current, max, limit, total */
Willy Tarreauf21d17b2019-09-08 09:24:56 +02001180 "<td><u>%s<div class=tips>"
1181 "<table class=det>"
1182 "<tr><th>Current active connections:</th><td>%s</td></tr>"
Willy Tarreau3bb617c2020-06-29 13:51:05 +02001183 "<tr><th>Current used connections:</th><td>%s</td></tr>"
Willy Tarreauf21d17b2019-09-08 09:24:56 +02001184 "<tr><th>Current idle connections:</th><td>%s</td></tr>"
Willy Tarreau3bb617c2020-06-29 13:51:05 +02001185 "<tr><th>- unsafe:</th><td>%s</td></tr>"
1186 "<tr><th>- safe:</th><td>%s</td></tr>"
Willy Tarreaua9fcecb2020-06-29 15:38:53 +02001187 "<tr><th>Estimated need of connections:</th><td>%s</td></tr>"
Willy Tarreauf21d17b2019-09-08 09:24:56 +02001188 "<tr><th>Active connections limit:</th><td>%s</td></tr>"
1189 "<tr><th>Idle connections limit:</th><td>%s</td></tr>"
1190 "</table></div></u>"
1191 "</td><td>%s</td><td>%s</td>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001192 "<td><u>%s<div class=tips><table class=det>"
1193 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
1194 "",
Willy Tarreauf21d17b2019-09-08 09:24:56 +02001195 U2H(stats[ST_F_SCUR].u.u32),
Willy Tarreau3bb617c2020-06-29 13:51:05 +02001196 U2H(stats[ST_F_SCUR].u.u32),
1197 U2H(stats[ST_F_USED_CONN_CUR].u.u32),
1198 U2H(stats[ST_F_SRV_ICUR].u.u32),
1199 U2H(stats[ST_F_IDLE_CONN_CUR].u.u32),
1200 U2H(stats[ST_F_SAFE_CONN_CUR].u.u32),
Willy Tarreaua9fcecb2020-06-29 15:38:53 +02001201 U2H(stats[ST_F_NEED_CONN_EST].u.u32),
Willy Tarreau3bb617c2020-06-29 13:51:05 +02001202
Willy Tarreauf21d17b2019-09-08 09:24:56 +02001203 LIM2A(stats[ST_F_SLIM].u.u32, "-"),
1204 stats[ST_F_SRV_ILIM].type ? U2H(stats[ST_F_SRV_ILIM].u.u32) : "-",
1205 U2H(stats[ST_F_SMAX].u.u32), LIM2A(stats[ST_F_SLIM].u.u32, "-"),
William Lallemand74c24fb2016-11-21 17:18:36 +01001206 U2H(stats[ST_F_STOT].u.u64),
1207 U2H(stats[ST_F_STOT].u.u64));
1208
1209 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
1210 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001211 chunk_appendf(out,
Willy Tarreauf1573842018-12-14 11:35:36 +01001212 "<tr><th>New connections:</th><td>%s</td></tr>"
1213 "<tr><th>Reused connections:</th><td>%s</td><td>(%d%%)</td></tr>"
Marcin Deranek3c27dda2020-05-15 18:32:51 +02001214 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001215 "<tr><th>- HTTP 1xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
1216 "<tr><th>- HTTP 2xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
1217 "<tr><th>- HTTP 3xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
1218 "<tr><th>- HTTP 4xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
1219 "<tr><th>- HTTP 5xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
1220 "<tr><th>- other responses:</th><td>%s</td><td>(%d%%)</td></tr>"
Willy Tarreau1b0f85e2018-05-28 15:12:40 +02001221 "<tr><th>Failed hdr rewrites:</th><td>%s</td></tr>"
Christopher Faulet0159ee42019-12-16 14:40:39 +01001222 "<tr><th>Internal error:</th><td>%s</td></tr>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001223 "",
Willy Tarreauf1573842018-12-14 11:35:36 +01001224 U2H(stats[ST_F_CONNECT].u.u64),
1225 U2H(stats[ST_F_REUSE].u.u64),
1226 (stats[ST_F_CONNECT].u.u64 + stats[ST_F_REUSE].u.u64) ?
1227 (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 +02001228 U2H(stats[ST_F_REQ_TOT].u.u64),
1229 U2H(stats[ST_F_HRSP_1XX].u.u64), stats[ST_F_REQ_TOT].u.u64 ?
1230 (int)(100 * stats[ST_F_HRSP_1XX].u.u64 / stats[ST_F_REQ_TOT].u.u64) : 0,
1231 U2H(stats[ST_F_HRSP_2XX].u.u64), stats[ST_F_REQ_TOT].u.u64 ?
1232 (int)(100 * stats[ST_F_HRSP_2XX].u.u64 / stats[ST_F_REQ_TOT].u.u64) : 0,
1233 U2H(stats[ST_F_HRSP_3XX].u.u64), stats[ST_F_REQ_TOT].u.u64 ?
1234 (int)(100 * stats[ST_F_HRSP_3XX].u.u64 / stats[ST_F_REQ_TOT].u.u64) : 0,
1235 U2H(stats[ST_F_HRSP_4XX].u.u64), stats[ST_F_REQ_TOT].u.u64 ?
1236 (int)(100 * stats[ST_F_HRSP_4XX].u.u64 / stats[ST_F_REQ_TOT].u.u64) : 0,
1237 U2H(stats[ST_F_HRSP_5XX].u.u64), stats[ST_F_REQ_TOT].u.u64 ?
1238 (int)(100 * stats[ST_F_HRSP_5XX].u.u64 / stats[ST_F_REQ_TOT].u.u64) : 0,
1239 U2H(stats[ST_F_HRSP_OTHER].u.u64), stats[ST_F_REQ_TOT].u.u64 ?
1240 (int)(100 * stats[ST_F_HRSP_OTHER].u.u64 / stats[ST_F_REQ_TOT].u.u64) : 0,
Christopher Faulet0159ee42019-12-16 14:40:39 +01001241 U2H(stats[ST_F_WREW].u.u64),
1242 U2H(stats[ST_F_EINT].u.u64));
William Lallemand74c24fb2016-11-21 17:18:36 +01001243 }
1244
Christopher Faulet0d1c2a62019-11-08 14:59:51 +01001245 chunk_appendf(out, "<tr><th colspan=3>Max / Avg over last 1024 success. conn.</th></tr>");
1246 chunk_appendf(out, "<tr><th>- Queue time:</th><td>%s / %s</td><td>ms</td></tr>",
1247 U2H(stats[ST_F_QT_MAX].u.u32), U2H(stats[ST_F_QTIME].u.u32));
1248 chunk_appendf(out, "<tr><th>- Connect time:</th><td>%s / %s</td><td>ms</td></tr>",
1249 U2H(stats[ST_F_CT_MAX].u.u32), U2H(stats[ST_F_CTIME].u.u32));
William Lallemand74c24fb2016-11-21 17:18:36 +01001250 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
Christopher Faulet0d1c2a62019-11-08 14:59:51 +01001251 chunk_appendf(out, "<tr><th>- Responses time:</th><td>%s / %s</td><td>ms</td></tr>",
1252 U2H(stats[ST_F_RT_MAX].u.u32), U2H(stats[ST_F_RTIME].u.u32));
1253 chunk_appendf(out, "<tr><th>- Total time:</th><td>%s / %s</td><td>ms</td></tr>",
1254 U2H(stats[ST_F_TT_MAX].u.u32), U2H(stats[ST_F_TTIME].u.u32));
William Lallemand74c24fb2016-11-21 17:18:36 +01001255
1256 chunk_appendf(out,
1257 "</table></div></u></td>"
1258 /* sessions: lbtot, last */
1259 "<td>%s</td><td>%s</td>",
1260 U2H(stats[ST_F_LBTOT].u.u64),
1261 human_time(stats[ST_F_LASTSESS].u.s32, 1));
1262
1263 chunk_appendf(out,
1264 /* bytes : in, out */
1265 "<td>%s</td><td>%s</td>"
1266 /* denied: req, resp */
1267 "<td></td><td>%s</td>"
1268 /* errors : request, connect */
1269 "<td></td><td>%s</td>"
1270 /* errors : response */
1271 "<td><u>%s<div class=tips>Connection resets during transfers: %lld client, %lld server</div></u></td>"
1272 /* warnings: retries, redispatches */
1273 "<td>%lld</td><td>%lld</td>"
1274 "",
1275 U2H(stats[ST_F_BIN].u.u64), U2H(stats[ST_F_BOUT].u.u64),
1276 U2H(stats[ST_F_DRESP].u.u64),
1277 U2H(stats[ST_F_ECON].u.u64),
1278 U2H(stats[ST_F_ERESP].u.u64),
1279 (long long)stats[ST_F_CLI_ABRT].u.u64,
1280 (long long)stats[ST_F_SRV_ABRT].u.u64,
1281 (long long)stats[ST_F_WRETR].u.u64,
1282 (long long)stats[ST_F_WREDIS].u.u64);
1283
1284 /* status, last change */
1285 chunk_appendf(out, "<td class=ac>");
1286
1287 /* FIXME!!!!
1288 * LASTCHG should contain the last change for *this* server and must be computed
1289 * properly above, as was done below, ie: this server if maint, otherwise ref server
1290 * if tracking. Note that ref is either local or remote depending on tracking.
1291 */
1292
1293
Willy Tarreau7b524852020-08-11 10:26:36 +02001294 if (strncmp(field_str(stats, ST_F_STATUS), "MAINT", 5) == 0) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001295 chunk_appendf(out, "%s MAINT", human_time(stats[ST_F_LASTCHG].u.u32, 1));
1296 }
Willy Tarreau7b524852020-08-11 10:26:36 +02001297 else if (strcmp(field_str(stats, ST_F_STATUS), "no check") == 0) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001298 chunk_strcat(out, "<i>no check</i>");
1299 }
1300 else {
1301 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 +02001302 if (strncmp(field_str(stats, ST_F_STATUS), "DOWN", 4) == 0) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001303 if (stats[ST_F_CHECK_HEALTH].u.u32)
1304 chunk_strcat(out, " &uarr;");
1305 }
1306 else if (stats[ST_F_CHECK_HEALTH].u.u32 < stats[ST_F_CHECK_RISE].u.u32 + stats[ST_F_CHECK_FALL].u.u32 - 1)
1307 chunk_strcat(out, " &darr;");
1308 }
1309
Willy Tarreau7b524852020-08-11 10:26:36 +02001310 if (strncmp(field_str(stats, ST_F_STATUS), "DOWN", 4) == 0 &&
William Lallemand74c24fb2016-11-21 17:18:36 +01001311 stats[ST_F_AGENT_STATUS].type && !stats[ST_F_AGENT_HEALTH].u.u32) {
1312 chunk_appendf(out,
1313 "</td><td class=ac><u> %s",
1314 field_str(stats, ST_F_AGENT_STATUS));
1315
1316 if (stats[ST_F_AGENT_CODE].type)
1317 chunk_appendf(out, "/%d", stats[ST_F_AGENT_CODE].u.u32);
1318
1319 if (stats[ST_F_AGENT_DURATION].type)
1320 chunk_appendf(out, " in %lums", (long)stats[ST_F_AGENT_DURATION].u.u64);
1321
1322 chunk_appendf(out, "<div class=tips>%s", field_str(stats, ST_F_AGENT_DESC));
1323
1324 if (*field_str(stats, ST_F_LAST_AGT)) {
1325 chunk_appendf(out, ": ");
1326 chunk_initstr(&src, field_str(stats, ST_F_LAST_AGT));
1327 chunk_htmlencode(out, &src);
1328 }
1329 chunk_appendf(out, "</div></u>");
1330 }
1331 else if (stats[ST_F_CHECK_STATUS].type) {
1332 chunk_appendf(out,
1333 "</td><td class=ac><u> %s",
1334 field_str(stats, ST_F_CHECK_STATUS));
1335
1336 if (stats[ST_F_CHECK_CODE].type)
1337 chunk_appendf(out, "/%d", stats[ST_F_CHECK_CODE].u.u32);
1338
1339 if (stats[ST_F_CHECK_DURATION].type)
1340 chunk_appendf(out, " in %lums", (long)stats[ST_F_CHECK_DURATION].u.u64);
1341
1342 chunk_appendf(out, "<div class=tips>%s", field_str(stats, ST_F_CHECK_DESC));
1343
1344 if (*field_str(stats, ST_F_LAST_CHK)) {
1345 chunk_appendf(out, ": ");
1346 chunk_initstr(&src, field_str(stats, ST_F_LAST_CHK));
1347 chunk_htmlencode(out, &src);
1348 }
1349 chunk_appendf(out, "</div></u>");
1350 }
1351 else
1352 chunk_appendf(out, "</td><td>");
1353
1354 chunk_appendf(out,
Willy Tarreaubd715102020-10-23 22:44:30 +02001355 /* weight / uweight */
1356 "</td><td class=ac>%d/%d</td>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001357 /* act, bck */
1358 "<td class=ac>%s</td><td class=ac>%s</td>"
1359 "",
Willy Tarreaubd715102020-10-23 22:44:30 +02001360 stats[ST_F_WEIGHT].u.u32, stats[ST_F_UWEIGHT].u.u32,
William Lallemand74c24fb2016-11-21 17:18:36 +01001361 stats[ST_F_BCK].u.u32 ? "-" : "Y",
1362 stats[ST_F_BCK].u.u32 ? "Y" : "-");
1363
1364 /* check failures: unique, fatal, down time */
1365 if (strcmp(field_str(stats, ST_F_STATUS), "MAINT (resolution)") == 0) {
1366 chunk_appendf(out, "<td class=ac colspan=3>resolution</td>");
1367 }
1368 else if (stats[ST_F_CHKFAIL].type) {
1369 chunk_appendf(out, "<td><u>%lld", (long long)stats[ST_F_CHKFAIL].u.u64);
1370
1371 if (stats[ST_F_HANAFAIL].type)
1372 chunk_appendf(out, "/%lld", (long long)stats[ST_F_HANAFAIL].u.u64);
1373
1374 chunk_appendf(out,
1375 "<div class=tips>Failed Health Checks%s</div></u></td>"
1376 "<td>%lld</td><td>%s</td>"
1377 "",
1378 stats[ST_F_HANAFAIL].type ? "/Health Analyses" : "",
1379 (long long)stats[ST_F_CHKDOWN].u.u64, human_time(stats[ST_F_DOWNTIME].u.u32, 1));
1380 }
1381 else if (strcmp(field_str(stats, ST_F_STATUS), "MAINT") != 0 && field_format(stats, ST_F_TRACKED) == FF_STR) {
1382 /* tracking a server (hence inherited maint would appear as "MAINT (via...)" */
1383 chunk_appendf(out,
1384 "<td class=ac colspan=3><a class=lfsb href=\"#%s\">via %s</a></td>",
1385 field_str(stats, ST_F_TRACKED), field_str(stats, ST_F_TRACKED));
1386 }
1387 else
1388 chunk_appendf(out, "<td colspan=3></td>");
1389
1390 /* throttle */
1391 if (stats[ST_F_THROTTLE].type)
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001392 chunk_appendf(out, "<td class=ac>%d %%</td>\n", stats[ST_F_THROTTLE].u.u32);
William Lallemand74c24fb2016-11-21 17:18:36 +01001393 else
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001394 chunk_appendf(out, "<td class=ac>-</td>");
1395
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001396 if (flags & STAT_SHMODULES) {
1397 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001398 chunk_appendf(out, "<td>");
1399
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001400 if (stats_px_get_cap(mod->domain_flags) & STATS_PX_CAP_SRV) {
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001401 chunk_appendf(out,
1402 "<u>%s<div class=tips><table class=det>",
1403 mod->name);
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001404 for (j = 0; j < mod->stats_count; ++j) {
1405 chunk_appendf(out,
1406 "<tr><th>%s</th><td>%s</td></tr>",
1407 mod->stats[j].desc, field_to_html_str(&stats[ST_F_TOTAL_FIELDS + i]));
1408 ++i;
1409 }
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001410 chunk_appendf(out, "</table></div></u>");
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001411 } else {
1412 i += mod->stats_count;
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001413 }
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001414
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001415 chunk_appendf(out, "</td>");
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001416 }
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001417 }
1418
1419 chunk_appendf(out, "</tr>\n");
William Lallemand74c24fb2016-11-21 17:18:36 +01001420 }
1421 else if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_BE) {
1422 chunk_appendf(out, "<tr class=\"backend\">");
Willy Tarreauab02b3f2019-10-09 11:11:46 +02001423 if (flags & STAT_ADMIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001424 /* Column sub-heading for Enable or Disable server */
1425 chunk_appendf(out, "<td></td>");
1426 }
1427 chunk_appendf(out,
1428 "<td class=ac>"
1429 /* name */
1430 "%s<a name=\"%s/Backend\"></a>"
1431 "<a class=lfsb href=\"#%s/Backend\">Backend</a>"
1432 "",
Willy Tarreau708c4162019-10-09 10:19:16 +02001433 (flags & STAT_SHLGNDS)?"<u>":"",
William Lallemand74c24fb2016-11-21 17:18:36 +01001434 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_PXNAME));
1435
Willy Tarreau708c4162019-10-09 10:19:16 +02001436 if (flags & STAT_SHLGNDS) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001437 /* balancing */
1438 chunk_appendf(out, "<div class=tips>balancing: %s",
1439 field_str(stats, ST_F_ALGO));
1440
1441 /* cookie */
1442 if (stats[ST_F_COOKIE].type) {
1443 chunk_appendf(out, ", cookie: '");
1444 chunk_initstr(&src, field_str(stats, ST_F_COOKIE));
1445 chunk_htmlencode(out, &src);
1446 chunk_appendf(out, "'");
1447 }
1448 chunk_appendf(out, "</div>");
1449 }
1450
1451 chunk_appendf(out,
1452 "%s</td>"
1453 /* queue : current, max */
1454 "<td>%s</td><td>%s</td><td></td>"
1455 /* sessions rate : current, max, limit */
1456 "<td>%s</td><td>%s</td><td></td>"
1457 "",
Willy Tarreau708c4162019-10-09 10:19:16 +02001458 (flags & STAT_SHLGNDS)?"</u>":"",
William Lallemand74c24fb2016-11-21 17:18:36 +01001459 U2H(stats[ST_F_QCUR].u.u32), U2H(stats[ST_F_QMAX].u.u32),
1460 U2H(stats[ST_F_RATE].u.u32), U2H(stats[ST_F_RATE_MAX].u.u32));
1461
1462 chunk_appendf(out,
1463 /* sessions: current, max, limit, total */
1464 "<td>%s</td><td>%s</td><td>%s</td>"
1465 "<td><u>%s<div class=tips><table class=det>"
1466 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
1467 "",
Willy Tarreau8e0f1752016-12-12 15:07:29 +01001468 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 +01001469 U2H(stats[ST_F_STOT].u.u64),
1470 U2H(stats[ST_F_STOT].u.u64));
1471
1472 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
1473 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0) {
1474 chunk_appendf(out,
Willy Tarreauf1573842018-12-14 11:35:36 +01001475 "<tr><th>New connections:</th><td>%s</td></tr>"
1476 "<tr><th>Reused connections:</th><td>%s</td><td>(%d%%)</td></tr>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001477 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
1478 "<tr><th>- HTTP 1xx responses:</th><td>%s</td></tr>"
1479 "<tr><th>- HTTP 2xx responses:</th><td>%s</td></tr>"
1480 "<tr><th>&nbsp;&nbsp;Compressed 2xx:</th><td>%s</td><td>(%d%%)</td></tr>"
1481 "<tr><th>- HTTP 3xx responses:</th><td>%s</td></tr>"
1482 "<tr><th>- HTTP 4xx responses:</th><td>%s</td></tr>"
1483 "<tr><th>- HTTP 5xx responses:</th><td>%s</td></tr>"
1484 "<tr><th>- other responses:</th><td>%s</td></tr>"
Willy Tarreaua1214a52018-12-14 14:00:25 +01001485 "<tr><th>Cache lookups:</th><td>%s</td></tr>"
1486 "<tr><th>Cache hits:</th><td>%s</td><td>(%d%%)</td></tr>"
Willy Tarreau1b0f85e2018-05-28 15:12:40 +02001487 "<tr><th>Failed hdr rewrites:</th><td>%s</td></tr>"
Christopher Faulet0159ee42019-12-16 14:40:39 +01001488 "<tr><th>Internal errors:</th><td>%s</td></tr>"
Christopher Faulet0d1c2a62019-11-08 14:59:51 +01001489 "",
Willy Tarreauf1573842018-12-14 11:35:36 +01001490 U2H(stats[ST_F_CONNECT].u.u64),
1491 U2H(stats[ST_F_REUSE].u.u64),
1492 (stats[ST_F_CONNECT].u.u64 + stats[ST_F_REUSE].u.u64) ?
1493 (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 +01001494 U2H(stats[ST_F_REQ_TOT].u.u64),
1495 U2H(stats[ST_F_HRSP_1XX].u.u64),
1496 U2H(stats[ST_F_HRSP_2XX].u.u64),
1497 U2H(stats[ST_F_COMP_RSP].u.u64),
1498 stats[ST_F_HRSP_2XX].u.u64 ?
1499 (int)(100 * stats[ST_F_COMP_RSP].u.u64 / stats[ST_F_HRSP_2XX].u.u64) : 0,
1500 U2H(stats[ST_F_HRSP_3XX].u.u64),
1501 U2H(stats[ST_F_HRSP_4XX].u.u64),
1502 U2H(stats[ST_F_HRSP_5XX].u.u64),
Willy Tarreaufeead3a2018-12-14 13:48:44 +01001503 U2H(stats[ST_F_HRSP_OTHER].u.u64),
Willy Tarreaua1214a52018-12-14 14:00:25 +01001504 U2H(stats[ST_F_CACHE_LOOKUPS].u.u64),
1505 U2H(stats[ST_F_CACHE_HITS].u.u64),
1506 stats[ST_F_CACHE_LOOKUPS].u.u64 ?
1507 (int)(100 * stats[ST_F_CACHE_HITS].u.u64 / stats[ST_F_CACHE_LOOKUPS].u.u64) : 0,
Christopher Faulet0159ee42019-12-16 14:40:39 +01001508 U2H(stats[ST_F_WREW].u.u64),
1509 U2H(stats[ST_F_EINT].u.u64));
William Lallemand74c24fb2016-11-21 17:18:36 +01001510 }
1511
Christopher Faulet0d1c2a62019-11-08 14:59:51 +01001512 chunk_appendf(out, "<tr><th colspan=3>Max / Avg over last 1024 success. conn.</th></tr>");
1513 chunk_appendf(out, "<tr><th>- Queue time:</th><td>%s / %s</td><td>ms</td></tr>",
1514 U2H(stats[ST_F_QT_MAX].u.u32), U2H(stats[ST_F_QTIME].u.u32));
1515 chunk_appendf(out, "<tr><th>- Connect time:</th><td>%s / %s</td><td>ms</td></tr>",
1516 U2H(stats[ST_F_CT_MAX].u.u32), U2H(stats[ST_F_CTIME].u.u32));
William Lallemand74c24fb2016-11-21 17:18:36 +01001517 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
Christopher Faulet0d1c2a62019-11-08 14:59:51 +01001518 chunk_appendf(out, "<tr><th>- Responses time:</th><td>%s / %s</td><td>ms</td></tr>",
1519 U2H(stats[ST_F_RT_MAX].u.u32), U2H(stats[ST_F_RTIME].u.u32));
1520 chunk_appendf(out, "<tr><th>- Total time:</th><td>%s / %s</td><td>ms</td></tr>",
1521 U2H(stats[ST_F_TT_MAX].u.u32), U2H(stats[ST_F_TTIME].u.u32));
William Lallemand74c24fb2016-11-21 17:18:36 +01001522
1523 chunk_appendf(out,
1524 "</table></div></u></td>"
1525 /* sessions: lbtot, last */
1526 "<td>%s</td><td>%s</td>"
1527 /* bytes: in */
1528 "<td>%s</td>"
1529 "",
1530 U2H(stats[ST_F_LBTOT].u.u64),
1531 human_time(stats[ST_F_LASTSESS].u.s32, 1),
1532 U2H(stats[ST_F_BIN].u.u64));
1533
1534 chunk_appendf(out,
1535 /* bytes:out + compression stats (via hover): comp_in, comp_out, comp_byp */
1536 "<td>%s%s<div class=tips><table class=det>"
1537 "<tr><th>Response bytes in:</th><td>%s</td></tr>"
1538 "<tr><th>Compression in:</th><td>%s</td></tr>"
1539 "<tr><th>Compression out:</th><td>%s</td><td>(%d%%)</td></tr>"
1540 "<tr><th>Compression bypass:</th><td>%s</td></tr>"
1541 "<tr><th>Total bytes saved:</th><td>%s</td><td>(%d%%)</td></tr>"
1542 "</table></div>%s</td>",
1543 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "<u>":"",
1544 U2H(stats[ST_F_BOUT].u.u64),
1545 U2H(stats[ST_F_BOUT].u.u64),
1546 U2H(stats[ST_F_COMP_IN].u.u64),
1547 U2H(stats[ST_F_COMP_OUT].u.u64),
1548 stats[ST_F_COMP_IN].u.u64 ? (int)(stats[ST_F_COMP_OUT].u.u64 * 100 / stats[ST_F_COMP_IN].u.u64) : 0,
1549 U2H(stats[ST_F_COMP_BYP].u.u64),
1550 U2H(stats[ST_F_COMP_IN].u.u64 - stats[ST_F_COMP_OUT].u.u64),
1551 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,
1552 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "</u>":"");
1553
1554 chunk_appendf(out,
1555 /* denied: req, resp */
1556 "<td>%s</td><td>%s</td>"
1557 /* errors : request, connect */
1558 "<td></td><td>%s</td>"
1559 /* errors : response */
1560 "<td><u>%s<div class=tips>Connection resets during transfers: %lld client, %lld server</div></u></td>"
1561 /* warnings: retries, redispatches */
1562 "<td>%lld</td><td>%lld</td>"
1563 /* backend status: reflect backend status (up/down): we display UP
1564 * if the backend has known working servers or if it has no server at
1565 * all (eg: for stats). Then we display the total weight, number of
1566 * active and backups. */
Willy Tarreaubd715102020-10-23 22:44:30 +02001567 "<td class=ac>%s %s</td><td class=ac>&nbsp;</td><td class=ac>%d/%d</td>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001568 "<td class=ac>%d</td><td class=ac>%d</td>"
1569 "",
1570 U2H(stats[ST_F_DREQ].u.u64), U2H(stats[ST_F_DRESP].u.u64),
1571 U2H(stats[ST_F_ECON].u.u64),
1572 U2H(stats[ST_F_ERESP].u.u64),
1573 (long long)stats[ST_F_CLI_ABRT].u.u64,
1574 (long long)stats[ST_F_SRV_ABRT].u.u64,
1575 (long long)stats[ST_F_WRETR].u.u64, (long long)stats[ST_F_WREDIS].u.u64,
1576 human_time(stats[ST_F_LASTCHG].u.u32, 1),
1577 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 +02001578 stats[ST_F_WEIGHT].u.u32, stats[ST_F_UWEIGHT].u.u32,
William Lallemand74c24fb2016-11-21 17:18:36 +01001579 stats[ST_F_ACT].u.u32, stats[ST_F_BCK].u.u32);
1580
1581 chunk_appendf(out,
1582 /* rest of backend: nothing, down transitions, total downtime, throttle */
1583 "<td class=ac>&nbsp;</td><td>%d</td>"
1584 "<td>%s</td>"
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001585 "<td></td>",
William Lallemand74c24fb2016-11-21 17:18:36 +01001586 stats[ST_F_CHKDOWN].u.u32,
1587 stats[ST_F_DOWNTIME].type ? human_time(stats[ST_F_DOWNTIME].u.u32, 1) : "&nbsp;");
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001588
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001589 if (flags & STAT_SHMODULES) {
1590 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001591 chunk_appendf(out, "<td>");
1592
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001593 if (stats_px_get_cap(mod->domain_flags) & STATS_PX_CAP_BE) {
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001594 chunk_appendf(out,
1595 "<u>%s<div class=tips><table class=det>",
1596 mod->name);
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001597 for (j = 0; j < mod->stats_count; ++j) {
1598 chunk_appendf(out,
1599 "<tr><th>%s</th><td>%s</td></tr>",
1600 mod->stats[j].desc, field_to_html_str(&stats[ST_F_TOTAL_FIELDS + i]));
1601 ++i;
1602 }
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001603 chunk_appendf(out, "</table></div></u>");
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001604 } else {
1605 i += mod->stats_count;
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001606 }
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001607
1608 chunk_appendf(out, "</td>");
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001609 }
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001610 }
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001611
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001612 chunk_appendf(out, "</tr>");
William Lallemand74c24fb2016-11-21 17:18:36 +01001613 }
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001614
William Lallemand74c24fb2016-11-21 17:18:36 +01001615 return 1;
1616}
1617
Amaury Denoyelle97323c92020-10-02 18:32:01 +02001618int stats_dump_one_line(const struct field *stats, size_t stats_count,
1619 struct appctx *appctx)
William Lallemand74c24fb2016-11-21 17:18:36 +01001620{
Simon Horman05ee2132017-01-04 09:37:25 +01001621 int ret;
1622
William Lallemand74c24fb2016-11-21 17:18:36 +01001623 if (appctx->ctx.stats.flags & STAT_FMT_HTML)
Willy Tarreau43241ff2019-10-09 11:27:51 +02001624 ret = stats_dump_fields_html(&trash, stats, appctx->ctx.stats.flags);
William Lallemand74c24fb2016-11-21 17:18:36 +01001625 else if (appctx->ctx.stats.flags & STAT_FMT_TYPED)
Amaury Denoyelle072f97e2020-10-05 11:49:37 +02001626 ret = stats_dump_fields_typed(&trash, stats, stats_count, appctx->ctx.stats.flags, appctx->ctx.stats.domain);
Simon Horman05ee2132017-01-04 09:37:25 +01001627 else if (appctx->ctx.stats.flags & STAT_FMT_JSON)
Amaury Denoyelle072f97e2020-10-05 11:49:37 +02001628 ret = stats_dump_fields_json(&trash, stats, stats_count, appctx->ctx.stats.flags, appctx->ctx.stats.domain);
William Lallemand74c24fb2016-11-21 17:18:36 +01001629 else
Amaury Denoyelle50660a82020-10-05 11:49:39 +02001630 ret = stats_dump_fields_csv(&trash, stats, stats_count, appctx->ctx.stats.flags, appctx->ctx.stats.domain);
Simon Horman05ee2132017-01-04 09:37:25 +01001631
1632 if (ret)
1633 appctx->ctx.stats.flags |= STAT_STARTED;
1634
1635 return ret;
William Lallemand74c24fb2016-11-21 17:18:36 +01001636}
1637
William Dauchy0ef54392021-01-17 18:27:45 +01001638/* Fill <stats> with the frontend statistics. <stats> is preallocated array of
Christopher Fauletd808f172021-01-22 17:33:22 +01001639 * length <len>. If <selected_field> is != NULL, only fill this one. The length
1640 * of the array must be at least ST_F_TOTAL_FIELDS. If this length is less than
1641 * this value, or if the selected field is not implemented for frontends, the
1642 * function returns 0, otherwise, it returns 1.
William Lallemand74c24fb2016-11-21 17:18:36 +01001643 */
William Dauchy0ef54392021-01-17 18:27:45 +01001644int stats_fill_fe_stats(struct proxy *px, struct field *stats, int len,
1645 enum stat_field *selected_field)
William Lallemand74c24fb2016-11-21 17:18:36 +01001646{
William Dauchy0ef54392021-01-17 18:27:45 +01001647 enum stat_field current_field = (selected_field != NULL ? *selected_field : 0);
William Dauchy0ef54392021-01-17 18:27:45 +01001648
William Lallemand74c24fb2016-11-21 17:18:36 +01001649 if (len < ST_F_TOTAL_FIELDS)
1650 return 0;
1651
William Dauchy0ef54392021-01-17 18:27:45 +01001652 for (; current_field < ST_F_TOTAL_FIELDS; current_field++) {
Christopher Faulet8596bfb2021-01-25 15:16:41 +01001653 struct field metric = { 0 };
1654
William Dauchy0ef54392021-01-17 18:27:45 +01001655 switch (current_field) {
1656 case ST_F_PXNAME:
1657 metric = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
1658 break;
1659 case ST_F_SVNAME:
1660 metric = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, "FRONTEND");
1661 break;
1662 case ST_F_MODE:
1663 metric = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
1664 break;
1665 case ST_F_SCUR:
1666 metric = mkf_u32(0, px->feconn);
1667 break;
1668 case ST_F_SMAX:
1669 metric = mkf_u32(FN_MAX, px->fe_counters.conn_max);
1670 break;
1671 case ST_F_SLIM:
1672 metric = mkf_u32(FO_CONFIG|FN_LIMIT, px->maxconn);
1673 break;
1674 case ST_F_STOT:
1675 metric = mkf_u64(FN_COUNTER, px->fe_counters.cum_sess);
1676 break;
1677 case ST_F_BIN:
1678 metric = mkf_u64(FN_COUNTER, px->fe_counters.bytes_in);
1679 break;
1680 case ST_F_BOUT:
1681 metric = mkf_u64(FN_COUNTER, px->fe_counters.bytes_out);
1682 break;
1683 case ST_F_DREQ:
1684 metric = mkf_u64(FN_COUNTER, px->fe_counters.denied_req);
1685 break;
1686 case ST_F_DRESP:
1687 metric = mkf_u64(FN_COUNTER, px->fe_counters.denied_resp);
1688 break;
1689 case ST_F_EREQ:
1690 metric = mkf_u64(FN_COUNTER, px->fe_counters.failed_req);
1691 break;
1692 case ST_F_DCON:
1693 metric = mkf_u64(FN_COUNTER, px->fe_counters.denied_conn);
1694 break;
1695 case ST_F_DSES:
1696 metric = mkf_u64(FN_COUNTER, px->fe_counters.denied_sess);
1697 break;
1698 case ST_F_STATUS:
Christopher Fauletdfd10ab2021-10-06 14:24:19 +02001699 metric = mkf_str(FO_STATUS, (px->flags & (PR_FL_DISABLED|PR_FL_STOPPED)) ? "STOP" : "OPEN");
William Dauchy0ef54392021-01-17 18:27:45 +01001700 break;
1701 case ST_F_PID:
Willy Tarreaue8422bf2021-06-15 09:08:18 +02001702 metric = mkf_u32(FO_KEY, 1);
William Dauchy0ef54392021-01-17 18:27:45 +01001703 break;
1704 case ST_F_IID:
1705 metric = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
1706 break;
1707 case ST_F_SID:
1708 metric = mkf_u32(FO_KEY|FS_SERVICE, 0);
1709 break;
1710 case ST_F_TYPE:
1711 metric = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_FE);
1712 break;
1713 case ST_F_RATE:
1714 metric = mkf_u32(FN_RATE, read_freq_ctr(&px->fe_sess_per_sec));
1715 break;
1716 case ST_F_RATE_LIM:
1717 metric = mkf_u32(FO_CONFIG|FN_LIMIT, px->fe_sps_lim);
1718 break;
1719 case ST_F_RATE_MAX:
1720 metric = mkf_u32(FN_MAX, px->fe_counters.sps_max);
1721 break;
1722 case ST_F_WREW:
1723 metric = mkf_u64(FN_COUNTER, px->fe_counters.failed_rewrites);
1724 break;
1725 case ST_F_EINT:
1726 metric = mkf_u64(FN_COUNTER, px->fe_counters.internal_errors);
1727 break;
1728 case ST_F_HRSP_1XX:
William Dauchy2107a0f2021-01-22 21:09:48 +01001729 if (px->mode == PR_MODE_HTTP)
1730 metric = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[1]);
William Dauchy0ef54392021-01-17 18:27:45 +01001731 break;
1732 case ST_F_HRSP_2XX:
William Dauchy2107a0f2021-01-22 21:09:48 +01001733 if (px->mode == PR_MODE_HTTP)
1734 metric = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[2]);
William Dauchy0ef54392021-01-17 18:27:45 +01001735 break;
1736 case ST_F_HRSP_3XX:
William Dauchy2107a0f2021-01-22 21:09:48 +01001737 if (px->mode == PR_MODE_HTTP)
1738 metric = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[3]);
William Dauchy0ef54392021-01-17 18:27:45 +01001739 break;
1740 case ST_F_HRSP_4XX:
William Dauchy2107a0f2021-01-22 21:09:48 +01001741 if (px->mode == PR_MODE_HTTP)
1742 metric = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[4]);
William Dauchy0ef54392021-01-17 18:27:45 +01001743 break;
1744 case ST_F_HRSP_5XX:
William Dauchy2107a0f2021-01-22 21:09:48 +01001745 if (px->mode == PR_MODE_HTTP)
1746 metric = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[5]);
William Dauchy0ef54392021-01-17 18:27:45 +01001747 break;
1748 case ST_F_HRSP_OTHER:
William Dauchy2107a0f2021-01-22 21:09:48 +01001749 if (px->mode == PR_MODE_HTTP)
1750 metric = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[0]);
William Dauchy0ef54392021-01-17 18:27:45 +01001751 break;
1752 case ST_F_INTERCEPTED:
William Dauchy2107a0f2021-01-22 21:09:48 +01001753 if (px->mode == PR_MODE_HTTP)
1754 metric = mkf_u64(FN_COUNTER, px->fe_counters.intercepted_req);
William Dauchy0ef54392021-01-17 18:27:45 +01001755 break;
1756 case ST_F_CACHE_LOOKUPS:
William Dauchy2107a0f2021-01-22 21:09:48 +01001757 if (px->mode == PR_MODE_HTTP)
1758 metric = mkf_u64(FN_COUNTER, px->fe_counters.p.http.cache_lookups);
William Dauchy0ef54392021-01-17 18:27:45 +01001759 break;
1760 case ST_F_CACHE_HITS:
William Dauchy2107a0f2021-01-22 21:09:48 +01001761 if (px->mode == PR_MODE_HTTP)
1762 metric = mkf_u64(FN_COUNTER, px->fe_counters.p.http.cache_hits);
William Dauchy0ef54392021-01-17 18:27:45 +01001763 break;
1764 case ST_F_REQ_RATE:
1765 metric = mkf_u32(FN_RATE, read_freq_ctr(&px->fe_req_per_sec));
1766 break;
1767 case ST_F_REQ_RATE_MAX:
1768 metric = mkf_u32(FN_MAX, px->fe_counters.p.http.rps_max);
1769 break;
1770 case ST_F_REQ_TOT:
1771 metric = mkf_u64(FN_COUNTER, px->fe_counters.p.http.cum_req);
1772 break;
1773 case ST_F_COMP_IN:
1774 metric = mkf_u64(FN_COUNTER, px->fe_counters.comp_in);
1775 break;
1776 case ST_F_COMP_OUT:
1777 metric = mkf_u64(FN_COUNTER, px->fe_counters.comp_out);
1778 break;
1779 case ST_F_COMP_BYP:
1780 metric = mkf_u64(FN_COUNTER, px->fe_counters.comp_byp);
1781 break;
1782 case ST_F_COMP_RSP:
1783 metric = mkf_u64(FN_COUNTER, px->fe_counters.p.http.comp_rsp);
1784 break;
1785 case ST_F_CONN_RATE:
1786 metric = mkf_u32(FN_RATE, read_freq_ctr(&px->fe_conn_per_sec));
1787 break;
1788 case ST_F_CONN_RATE_MAX:
1789 metric = mkf_u32(FN_MAX, px->fe_counters.cps_max);
1790 break;
1791 case ST_F_CONN_TOT:
1792 metric = mkf_u64(FN_COUNTER, px->fe_counters.cum_conn);
1793 break;
1794 default:
Christopher Fauletd808f172021-01-22 17:33:22 +01001795 /* not used for frontends. If a specific metric
1796 * is requested, return an error. Otherwise continue.
1797 */
1798 if (selected_field != NULL)
1799 return 0;
1800 continue;
William Dauchy0ef54392021-01-17 18:27:45 +01001801 }
1802 stats[current_field] = metric;
1803 if (selected_field != NULL)
1804 break;
William Lallemand74c24fb2016-11-21 17:18:36 +01001805 }
William Lallemand74c24fb2016-11-21 17:18:36 +01001806 return 1;
1807}
1808
1809/* Dumps a frontend's line to the trash for the current proxy <px> and uses
Christopher Faulet908628c2022-03-25 16:43:49 +01001810 * the state from conn-stream <cs>. The caller is responsible for clearing
William Lallemand74c24fb2016-11-21 17:18:36 +01001811 * the trash if needed. Returns non-zero if it emits anything, zero otherwise.
1812 */
Christopher Faulet908628c2022-03-25 16:43:49 +01001813static int stats_dump_fe_stats(struct conn_stream *cs, struct proxy *px)
William Lallemand74c24fb2016-11-21 17:18:36 +01001814{
Christopher Faulet908628c2022-03-25 16:43:49 +01001815 struct appctx *appctx = __cs_appctx(cs);
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02001816 struct field *stats = stat_l[STATS_DOMAIN_PROXY];
1817 struct stats_module *mod;
1818 size_t stats_count = ST_F_TOTAL_FIELDS;
William Lallemand74c24fb2016-11-21 17:18:36 +01001819
1820 if (!(px->cap & PR_CAP_FE))
1821 return 0;
1822
1823 if ((appctx->ctx.stats.flags & STAT_BOUND) && !(appctx->ctx.stats.type & (1 << STATS_TYPE_FE)))
1824 return 0;
1825
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02001826 memset(stats, 0, sizeof(struct field) * stat_count[STATS_DOMAIN_PROXY]);
1827
William Dauchy0ef54392021-01-17 18:27:45 +01001828 if (!stats_fill_fe_stats(px, stats, ST_F_TOTAL_FIELDS, NULL))
William Lallemand74c24fb2016-11-21 17:18:36 +01001829 return 0;
1830
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02001831 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
1832 void *counters;
1833
1834 if (!(stats_px_get_cap(mod->domain_flags) & STATS_PX_CAP_FE)) {
1835 stats_count += mod->stats_count;
1836 continue;
1837 }
1838
1839 counters = EXTRA_COUNTERS_GET(px->extra_counters_fe, mod);
1840 mod->fill_stats(counters, stats + stats_count);
1841 stats_count += mod->stats_count;
1842 }
1843
1844 return stats_dump_one_line(stats, stats_count, appctx);
William Lallemand74c24fb2016-11-21 17:18:36 +01001845}
1846
William Dauchy655e14e2021-02-14 23:22:54 +01001847/* Fill <stats> with the listener statistics. <stats> is preallocated array of
1848 * length <len>. The length of the array must be at least ST_F_TOTAL_FIELDS. If
1849 * this length is less then this value, the function returns 0, otherwise, it
1850 * returns 1. If selected_field is != NULL, only fill this one. <flags> can
1851 * take the value STAT_SHLGNDS.
William Lallemand74c24fb2016-11-21 17:18:36 +01001852 */
1853int stats_fill_li_stats(struct proxy *px, struct listener *l, int flags,
William Dauchy655e14e2021-02-14 23:22:54 +01001854 struct field *stats, int len, enum stat_field *selected_field)
William Lallemand74c24fb2016-11-21 17:18:36 +01001855{
William Dauchy655e14e2021-02-14 23:22:54 +01001856 enum stat_field current_field = (selected_field != NULL ? *selected_field : 0);
Willy Tarreau83061a82018-07-13 11:56:34 +02001857 struct buffer *out = get_trash_chunk();
William Lallemand74c24fb2016-11-21 17:18:36 +01001858
1859 if (len < ST_F_TOTAL_FIELDS)
1860 return 0;
1861
1862 if (!l->counters)
1863 return 0;
1864
1865 chunk_reset(out);
William Lallemand74c24fb2016-11-21 17:18:36 +01001866
William Dauchy655e14e2021-02-14 23:22:54 +01001867 for (; current_field < ST_F_TOTAL_FIELDS; current_field++) {
1868 struct field metric = { 0 };
William Lallemand74c24fb2016-11-21 17:18:36 +01001869
William Dauchy655e14e2021-02-14 23:22:54 +01001870 switch (current_field) {
1871 case ST_F_PXNAME:
1872 metric = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
1873 break;
1874 case ST_F_SVNAME:
1875 metric = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, l->name);
1876 break;
1877 case ST_F_MODE:
1878 metric = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
1879 break;
1880 case ST_F_SCUR:
1881 metric = mkf_u32(0, l->nbconn);
1882 break;
1883 case ST_F_SMAX:
1884 metric = mkf_u32(FN_MAX, l->counters->conn_max);
1885 break;
1886 case ST_F_SLIM:
1887 metric = mkf_u32(FO_CONFIG|FN_LIMIT, l->maxconn);
1888 break;
1889 case ST_F_STOT:
1890 metric = mkf_u64(FN_COUNTER, l->counters->cum_conn);
1891 break;
1892 case ST_F_BIN:
1893 metric = mkf_u64(FN_COUNTER, l->counters->bytes_in);
1894 break;
1895 case ST_F_BOUT:
1896 metric = mkf_u64(FN_COUNTER, l->counters->bytes_out);
1897 break;
1898 case ST_F_DREQ:
1899 metric = mkf_u64(FN_COUNTER, l->counters->denied_req);
1900 break;
1901 case ST_F_DRESP:
1902 metric = mkf_u64(FN_COUNTER, l->counters->denied_resp);
1903 break;
1904 case ST_F_EREQ:
1905 metric = mkf_u64(FN_COUNTER, l->counters->failed_req);
1906 break;
1907 case ST_F_DCON:
1908 metric = mkf_u64(FN_COUNTER, l->counters->denied_conn);
1909 break;
1910 case ST_F_DSES:
1911 metric = mkf_u64(FN_COUNTER, l->counters->denied_sess);
1912 break;
1913 case ST_F_STATUS:
William Dauchy3679d0c2021-02-14 23:22:55 +01001914 metric = mkf_str(FO_STATUS, li_status_st[get_li_status(l)]);
William Dauchy655e14e2021-02-14 23:22:54 +01001915 break;
1916 case ST_F_PID:
Willy Tarreaue8422bf2021-06-15 09:08:18 +02001917 metric = mkf_u32(FO_KEY, 1);
William Dauchy655e14e2021-02-14 23:22:54 +01001918 break;
1919 case ST_F_IID:
1920 metric = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
1921 break;
1922 case ST_F_SID:
1923 metric = mkf_u32(FO_KEY|FS_SERVICE, l->luid);
1924 break;
1925 case ST_F_TYPE:
1926 metric = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_SO);
1927 break;
1928 case ST_F_WREW:
1929 metric = mkf_u64(FN_COUNTER, l->counters->failed_rewrites);
1930 break;
1931 case ST_F_EINT:
1932 metric = mkf_u64(FN_COUNTER, l->counters->internal_errors);
1933 break;
1934 case ST_F_ADDR:
1935 if (flags & STAT_SHLGNDS) {
1936 char str[INET6_ADDRSTRLEN];
1937 int port;
William Lallemand74c24fb2016-11-21 17:18:36 +01001938
William Dauchy655e14e2021-02-14 23:22:54 +01001939 port = get_host_port(&l->rx.addr);
1940 switch (addr_to_str(&l->rx.addr, str, sizeof(str))) {
1941 case AF_INET:
1942 metric = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
1943 chunk_appendf(out, "%s:%d", str, port);
1944 break;
1945 case AF_INET6:
1946 metric = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
1947 chunk_appendf(out, "[%s]:%d", str, port);
1948 break;
1949 case AF_UNIX:
1950 metric = mkf_str(FO_CONFIG|FS_SERVICE, "unix");
1951 break;
1952 case -1:
1953 metric = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
1954 chunk_strcat(out, strerror(errno));
1955 break;
1956 default: /* address family not supported */
1957 break;
1958 }
1959 }
1960 break;
1961 default:
1962 /* not used for listen. If a specific metric
1963 * is requested, return an error. Otherwise continue.
1964 */
1965 if (selected_field != NULL)
1966 return 0;
1967 continue;
William Lallemand74c24fb2016-11-21 17:18:36 +01001968 }
William Dauchy655e14e2021-02-14 23:22:54 +01001969 stats[current_field] = metric;
1970 if (selected_field != NULL)
1971 break;
William Lallemand74c24fb2016-11-21 17:18:36 +01001972 }
William Lallemand74c24fb2016-11-21 17:18:36 +01001973 return 1;
1974}
1975
1976/* Dumps a line for listener <l> and proxy <px> to the trash and uses the state
Christopher Faulet908628c2022-03-25 16:43:49 +01001977 * from conn-stream <cs>. The caller is responsible for clearing the trash
Willy Tarreau43241ff2019-10-09 11:27:51 +02001978 * if needed. Returns non-zero if it emits anything, zero otherwise.
William Lallemand74c24fb2016-11-21 17:18:36 +01001979 */
Christopher Faulet908628c2022-03-25 16:43:49 +01001980static int stats_dump_li_stats(struct conn_stream *cs, struct proxy *px, struct listener *l)
William Lallemand74c24fb2016-11-21 17:18:36 +01001981{
Christopher Faulet908628c2022-03-25 16:43:49 +01001982 struct appctx *appctx = __cs_appctx(cs);
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02001983 struct field *stats = stat_l[STATS_DOMAIN_PROXY];
1984 struct stats_module *mod;
1985 size_t stats_count = ST_F_TOTAL_FIELDS;
1986
1987 memset(stats, 0, sizeof(struct field) * stat_count[STATS_DOMAIN_PROXY]);
William Lallemand74c24fb2016-11-21 17:18:36 +01001988
William Dauchy655e14e2021-02-14 23:22:54 +01001989 if (!stats_fill_li_stats(px, l, appctx->ctx.stats.flags, stats,
1990 ST_F_TOTAL_FIELDS, NULL))
William Lallemand74c24fb2016-11-21 17:18:36 +01001991 return 0;
1992
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02001993 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
1994 void *counters;
1995
1996 if (!(stats_px_get_cap(mod->domain_flags) & STATS_PX_CAP_LI)) {
1997 stats_count += mod->stats_count;
1998 continue;
1999 }
2000
2001 counters = EXTRA_COUNTERS_GET(l->extra_counters, mod);
2002 mod->fill_stats(counters, stats + stats_count);
2003 stats_count += mod->stats_count;
2004 }
2005
2006 return stats_dump_one_line(stats, stats_count, appctx);
William Lallemand74c24fb2016-11-21 17:18:36 +01002007}
2008
2009enum srv_stats_state {
2010 SRV_STATS_STATE_DOWN = 0,
2011 SRV_STATS_STATE_DOWN_AGENT,
2012 SRV_STATS_STATE_GOING_UP,
2013 SRV_STATS_STATE_UP_GOING_DOWN,
2014 SRV_STATS_STATE_UP,
2015 SRV_STATS_STATE_NOLB_GOING_DOWN,
2016 SRV_STATS_STATE_NOLB,
2017 SRV_STATS_STATE_DRAIN_GOING_DOWN,
2018 SRV_STATS_STATE_DRAIN,
2019 SRV_STATS_STATE_DRAIN_AGENT,
2020 SRV_STATS_STATE_NO_CHECK,
2021
2022 SRV_STATS_STATE_COUNT, /* Must be last */
2023};
2024
2025static const char *srv_hlt_st[SRV_STATS_STATE_COUNT] = {
2026 [SRV_STATS_STATE_DOWN] = "DOWN",
2027 [SRV_STATS_STATE_DOWN_AGENT] = "DOWN (agent)",
2028 [SRV_STATS_STATE_GOING_UP] = "DOWN %d/%d",
2029 [SRV_STATS_STATE_UP_GOING_DOWN] = "UP %d/%d",
2030 [SRV_STATS_STATE_UP] = "UP",
2031 [SRV_STATS_STATE_NOLB_GOING_DOWN] = "NOLB %d/%d",
2032 [SRV_STATS_STATE_NOLB] = "NOLB",
2033 [SRV_STATS_STATE_DRAIN_GOING_DOWN] = "DRAIN %d/%d",
2034 [SRV_STATS_STATE_DRAIN] = "DRAIN",
2035 [SRV_STATS_STATE_DRAIN_AGENT] = "DRAIN (agent)",
2036 [SRV_STATS_STATE_NO_CHECK] = "no check"
2037};
2038
William Dauchyd3a9a492021-01-25 17:29:03 +01002039/* Compute server state helper
William Lallemand74c24fb2016-11-21 17:18:36 +01002040 */
William Dauchyd3a9a492021-01-25 17:29:03 +01002041static void stats_fill_sv_stats_computestate(struct server *sv, struct server *ref,
2042 enum srv_stats_state *state)
William Lallemand74c24fb2016-11-21 17:18:36 +01002043{
Emeric Brun52a91d32017-08-31 14:41:55 +02002044 if (sv->cur_state == SRV_ST_RUNNING || sv->cur_state == SRV_ST_STARTING) {
William Lallemand74c24fb2016-11-21 17:18:36 +01002045 if ((ref->check.state & CHK_ST_ENABLED) &&
2046 (ref->check.health < ref->check.rise + ref->check.fall - 1)) {
William Dauchyd3a9a492021-01-25 17:29:03 +01002047 *state = SRV_STATS_STATE_UP_GOING_DOWN;
William Lallemand74c24fb2016-11-21 17:18:36 +01002048 } else {
William Dauchyd3a9a492021-01-25 17:29:03 +01002049 *state = SRV_STATS_STATE_UP;
William Lallemand74c24fb2016-11-21 17:18:36 +01002050 }
2051
Emeric Brun52a91d32017-08-31 14:41:55 +02002052 if (sv->cur_admin & SRV_ADMF_DRAIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +01002053 if (ref->agent.state & CHK_ST_ENABLED)
William Dauchyd3a9a492021-01-25 17:29:03 +01002054 *state = SRV_STATS_STATE_DRAIN_AGENT;
2055 else if (*state == SRV_STATS_STATE_UP_GOING_DOWN)
2056 *state = SRV_STATS_STATE_DRAIN_GOING_DOWN;
William Lallemand74c24fb2016-11-21 17:18:36 +01002057 else
William Dauchyd3a9a492021-01-25 17:29:03 +01002058 *state = SRV_STATS_STATE_DRAIN;
William Lallemand74c24fb2016-11-21 17:18:36 +01002059 }
2060
William Dauchyd3a9a492021-01-25 17:29:03 +01002061 if (*state == SRV_STATS_STATE_UP && !(ref->check.state & CHK_ST_ENABLED)) {
2062 *state = SRV_STATS_STATE_NO_CHECK;
William Lallemand74c24fb2016-11-21 17:18:36 +01002063 }
2064 }
Emeric Brun52a91d32017-08-31 14:41:55 +02002065 else if (sv->cur_state == SRV_ST_STOPPING) {
William Lallemand74c24fb2016-11-21 17:18:36 +01002066 if ((!(sv->check.state & CHK_ST_ENABLED) && !sv->track) ||
2067 (ref->check.health == ref->check.rise + ref->check.fall - 1)) {
William Dauchyd3a9a492021-01-25 17:29:03 +01002068 *state = SRV_STATS_STATE_NOLB;
William Lallemand74c24fb2016-11-21 17:18:36 +01002069 } else {
William Dauchyd3a9a492021-01-25 17:29:03 +01002070 *state = SRV_STATS_STATE_NOLB_GOING_DOWN;
William Lallemand74c24fb2016-11-21 17:18:36 +01002071 }
2072 }
2073 else { /* stopped */
2074 if ((ref->agent.state & CHK_ST_ENABLED) && !ref->agent.health) {
William Dauchyd3a9a492021-01-25 17:29:03 +01002075 *state = SRV_STATS_STATE_DOWN_AGENT;
William Lallemand74c24fb2016-11-21 17:18:36 +01002076 } else if ((ref->check.state & CHK_ST_ENABLED) && !ref->check.health) {
William Dauchyd3a9a492021-01-25 17:29:03 +01002077 *state = SRV_STATS_STATE_DOWN; /* DOWN */
William Lallemand74c24fb2016-11-21 17:18:36 +01002078 } else if ((ref->agent.state & CHK_ST_ENABLED) || (ref->check.state & CHK_ST_ENABLED)) {
William Dauchyd3a9a492021-01-25 17:29:03 +01002079 *state = SRV_STATS_STATE_GOING_UP;
William Lallemand74c24fb2016-11-21 17:18:36 +01002080 } else {
William Dauchyd3a9a492021-01-25 17:29:03 +01002081 *state = SRV_STATS_STATE_DOWN; /* DOWN, unchecked */
William Lallemand74c24fb2016-11-21 17:18:36 +01002082 }
2083 }
William Dauchyd3a9a492021-01-25 17:29:03 +01002084}
William Lallemand74c24fb2016-11-21 17:18:36 +01002085
William Dauchyd3a9a492021-01-25 17:29:03 +01002086/* Fill <stats> with the backend statistics. <stats> is preallocated array of
2087 * length <len>. If <selected_field> is != NULL, only fill this one. The length
2088 * of the array must be at least ST_F_TOTAL_FIELDS. If this length is less than
2089 * this value, or if the selected field is not implemented for servers, the
2090 * function returns 0, otherwise, it returns 1. <flags> can take the value
2091 * STAT_SHLGNDS.
2092 */
2093int stats_fill_sv_stats(struct proxy *px, struct server *sv, int flags,
2094 struct field *stats, int len,
2095 enum stat_field *selected_field)
2096{
2097 enum stat_field current_field = (selected_field != NULL ? *selected_field : 0);
2098 struct server *via = sv->track ? sv->track : sv;
2099 struct server *ref = via;
2100 enum srv_stats_state state = 0;
2101 char str[INET6_ADDRSTRLEN];
2102 struct buffer *out = get_trash_chunk();
2103 char *fld_status;
2104 long long srv_samples_counter;
2105 unsigned int srv_samples_window = TIME_STATS_SAMPLES;
William Lallemand74c24fb2016-11-21 17:18:36 +01002106
William Dauchyd3a9a492021-01-25 17:29:03 +01002107 if (len < ST_F_TOTAL_FIELDS)
2108 return 0;
William Lallemand74c24fb2016-11-21 17:18:36 +01002109
William Dauchyd3a9a492021-01-25 17:29:03 +01002110 chunk_reset(out);
William Lallemand74c24fb2016-11-21 17:18:36 +01002111
William Dauchyd3a9a492021-01-25 17:29:03 +01002112 /* compute state for later use */
2113 if (selected_field == NULL || *selected_field == ST_F_STATUS ||
2114 *selected_field == ST_F_CHECK_RISE || *selected_field == ST_F_CHECK_FALL ||
2115 *selected_field == ST_F_CHECK_HEALTH || *selected_field == ST_F_HANAFAIL) {
2116 /* we have "via" which is the tracked server as described in the configuration,
2117 * and "ref" which is the checked server and the end of the chain.
2118 */
2119 while (ref->track)
2120 ref = ref->track;
2121 stats_fill_sv_stats_computestate(sv, ref, &state);
William Lallemand74c24fb2016-11-21 17:18:36 +01002122 }
2123
William Dauchyd3a9a492021-01-25 17:29:03 +01002124 /* compue time values for later use */
2125 if (selected_field == NULL || *selected_field == ST_F_QTIME ||
2126 *selected_field == ST_F_CTIME || *selected_field == ST_F_RTIME ||
2127 *selected_field == ST_F_TTIME) {
2128 srv_samples_counter = (px->mode == PR_MODE_HTTP) ? sv->counters.p.http.cum_req : sv->counters.cum_lbconn;
2129 if (srv_samples_counter < TIME_STATS_SAMPLES && srv_samples_counter > 0)
2130 srv_samples_window = srv_samples_counter;
William Lallemand74c24fb2016-11-21 17:18:36 +01002131 }
2132
William Dauchyd3a9a492021-01-25 17:29:03 +01002133 for (; current_field < ST_F_TOTAL_FIELDS; current_field++) {
2134 struct field metric = { 0 };
William Lallemand74c24fb2016-11-21 17:18:36 +01002135
William Dauchyd3a9a492021-01-25 17:29:03 +01002136 switch (current_field) {
2137 case ST_F_PXNAME:
2138 metric = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
2139 break;
2140 case ST_F_SVNAME:
2141 metric = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, sv->id);
2142 break;
2143 case ST_F_MODE:
2144 metric = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
Christopher Faulet3888b8c2021-01-27 13:32:25 +01002145 break;
William Dauchyd3a9a492021-01-25 17:29:03 +01002146 case ST_F_QCUR:
Willy Tarreaua0570452021-06-18 09:30:30 +02002147 metric = mkf_u32(0, sv->queue.length);
William Dauchyd3a9a492021-01-25 17:29:03 +01002148 break;
2149 case ST_F_QMAX:
2150 metric = mkf_u32(FN_MAX, sv->counters.nbpend_max);
2151 break;
2152 case ST_F_SCUR:
2153 metric = mkf_u32(0, sv->cur_sess);
2154 break;
2155 case ST_F_SMAX:
2156 metric = mkf_u32(FN_MAX, sv->counters.cur_sess_max);
2157 break;
2158 case ST_F_SLIM:
2159 if (sv->maxconn)
2160 metric = mkf_u32(FO_CONFIG|FN_LIMIT, sv->maxconn);
2161 break;
2162 case ST_F_SRV_ICUR:
2163 metric = mkf_u32(0, sv->curr_idle_conns);
2164 break;
2165 case ST_F_SRV_ILIM:
2166 if (sv->max_idle_conns != -1)
2167 metric = mkf_u32(FO_CONFIG|FN_LIMIT, sv->max_idle_conns);
2168 break;
2169 case ST_F_STOT:
2170 metric = mkf_u64(FN_COUNTER, sv->counters.cum_sess);
2171 break;
2172 case ST_F_BIN:
2173 metric = mkf_u64(FN_COUNTER, sv->counters.bytes_in);
2174 break;
2175 case ST_F_BOUT:
2176 metric = mkf_u64(FN_COUNTER, sv->counters.bytes_out);
2177 break;
2178 case ST_F_DRESP:
2179 metric = mkf_u64(FN_COUNTER, sv->counters.denied_resp);
2180 break;
2181 case ST_F_ECON:
2182 metric = mkf_u64(FN_COUNTER, sv->counters.failed_conns);
2183 break;
2184 case ST_F_ERESP:
2185 metric = mkf_u64(FN_COUNTER, sv->counters.failed_resp);
2186 break;
2187 case ST_F_WRETR:
2188 metric = mkf_u64(FN_COUNTER, sv->counters.retries);
2189 break;
2190 case ST_F_WREDIS:
2191 metric = mkf_u64(FN_COUNTER, sv->counters.redispatches);
2192 break;
2193 case ST_F_WREW:
2194 metric = mkf_u64(FN_COUNTER, sv->counters.failed_rewrites);
2195 break;
2196 case ST_F_EINT:
2197 metric = mkf_u64(FN_COUNTER, sv->counters.internal_errors);
2198 break;
2199 case ST_F_CONNECT:
2200 metric = mkf_u64(FN_COUNTER, sv->counters.connect);
2201 break;
2202 case ST_F_REUSE:
2203 metric = mkf_u64(FN_COUNTER, sv->counters.reuse);
2204 break;
2205 case ST_F_IDLE_CONN_CUR:
2206 metric = mkf_u32(0, sv->curr_idle_nb);
2207 break;
2208 case ST_F_SAFE_CONN_CUR:
2209 metric = mkf_u32(0, sv->curr_safe_nb);
2210 break;
2211 case ST_F_USED_CONN_CUR:
2212 metric = mkf_u32(0, sv->curr_used_conns);
2213 break;
2214 case ST_F_NEED_CONN_EST:
2215 metric = mkf_u32(0, sv->est_need_conns);
2216 break;
2217 case ST_F_STATUS:
2218 fld_status = chunk_newstr(out);
2219 if (sv->cur_admin & SRV_ADMF_RMAINT)
2220 chunk_appendf(out, "MAINT (resolution)");
2221 else if (sv->cur_admin & SRV_ADMF_IMAINT)
2222 chunk_appendf(out, "MAINT (via %s/%s)", via->proxy->id, via->id);
2223 else if (sv->cur_admin & SRV_ADMF_MAINT)
2224 chunk_appendf(out, "MAINT");
2225 else
2226 chunk_appendf(out,
2227 srv_hlt_st[state],
2228 (ref->cur_state != SRV_ST_STOPPED) ? (ref->check.health - ref->check.rise + 1) : (ref->check.health),
2229 (ref->cur_state != SRV_ST_STOPPED) ? (ref->check.fall) : (ref->check.rise));
William Lallemand74c24fb2016-11-21 17:18:36 +01002230
William Dauchyd3a9a492021-01-25 17:29:03 +01002231 metric = mkf_str(FO_STATUS, fld_status);
2232 break;
2233 case ST_F_LASTCHG:
2234 metric = mkf_u32(FN_AGE, now.tv_sec - sv->last_change);
2235 break;
2236 case ST_F_WEIGHT:
2237 metric = mkf_u32(FN_AVG, (sv->cur_eweight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv);
2238 break;
2239 case ST_F_UWEIGHT:
2240 metric = mkf_u32(FN_AVG, sv->uweight);
2241 break;
2242 case ST_F_ACT:
2243 metric = mkf_u32(FO_STATUS, (sv->flags & SRV_F_BACKUP) ? 0 : 1);
2244 break;
2245 case ST_F_BCK:
2246 metric = mkf_u32(FO_STATUS, (sv->flags & SRV_F_BACKUP) ? 1 : 0);
2247 break;
2248 case ST_F_CHKFAIL:
2249 if (sv->check.state & CHK_ST_ENABLED)
2250 metric = mkf_u64(FN_COUNTER, sv->counters.failed_checks);
2251 break;
2252 case ST_F_CHKDOWN:
2253 if (sv->check.state & CHK_ST_ENABLED)
2254 metric = mkf_u64(FN_COUNTER, sv->counters.down_trans);
2255 break;
2256 case ST_F_DOWNTIME:
2257 if (sv->check.state & CHK_ST_ENABLED)
2258 metric = mkf_u32(FN_COUNTER, srv_downtime(sv));
2259 break;
2260 case ST_F_QLIMIT:
2261 if (sv->maxqueue)
2262 metric = mkf_u32(FO_CONFIG|FS_SERVICE, sv->maxqueue);
2263 break;
2264 case ST_F_PID:
Willy Tarreaue8422bf2021-06-15 09:08:18 +02002265 metric = mkf_u32(FO_KEY, 1);
William Dauchyd3a9a492021-01-25 17:29:03 +01002266 break;
2267 case ST_F_IID:
2268 metric = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
2269 break;
2270 case ST_F_SID:
2271 metric = mkf_u32(FO_KEY|FS_SERVICE, sv->puid);
2272 break;
2273 case ST_F_THROTTLE:
2274 if (sv->cur_state == SRV_ST_STARTING && !server_is_draining(sv))
2275 metric = mkf_u32(FN_AVG, server_throttle_rate(sv));
2276 break;
2277 case ST_F_LBTOT:
2278 metric = mkf_u64(FN_COUNTER, sv->counters.cum_lbconn);
2279 break;
2280 case ST_F_TRACKED:
2281 if (sv->track) {
2282 char *fld_track = chunk_newstr(out);
2283 chunk_appendf(out, "%s/%s", sv->track->proxy->id, sv->track->id);
2284 metric = mkf_str(FO_CONFIG|FN_NAME|FS_SERVICE, fld_track);
2285 }
2286 break;
2287 case ST_F_TYPE:
2288 metric = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_SV);
2289 break;
2290 case ST_F_RATE:
2291 metric = mkf_u32(FN_RATE, read_freq_ctr(&sv->sess_per_sec));
2292 break;
2293 case ST_F_RATE_MAX:
2294 metric = mkf_u32(FN_MAX, sv->counters.sps_max);
2295 break;
2296 case ST_F_CHECK_STATUS:
2297 if ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED) {
2298 const char *fld_chksts;
Marcin Deraneka8dbdf32020-05-15 20:02:40 +02002299
William Dauchyd3a9a492021-01-25 17:29:03 +01002300 fld_chksts = chunk_newstr(out);
2301 chunk_strcat(out, "* "); // for check in progress
2302 chunk_strcat(out, get_check_status_info(sv->check.status));
2303 if (!(sv->check.state & CHK_ST_INPROGRESS))
2304 fld_chksts += 2; // skip "* "
Willy Tarreau973a9372021-05-12 17:29:14 +02002305 metric = mkf_str(FN_OUTPUT, fld_chksts);
William Dauchyd3a9a492021-01-25 17:29:03 +01002306 }
2307 break;
2308 case ST_F_CHECK_CODE:
2309 if ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED &&
2310 sv->check.status >= HCHK_STATUS_L57DATA)
2311 metric = mkf_u32(FN_OUTPUT, sv->check.code);
2312 break;
2313 case ST_F_CHECK_DURATION:
2314 if ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED &&
2315 sv->check.status >= HCHK_STATUS_CHECKED)
2316 metric = mkf_u64(FN_DURATION, sv->check.duration);
2317 break;
2318 case ST_F_CHECK_DESC:
2319 if ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED)
2320 metric = mkf_str(FN_OUTPUT, get_check_status_description(sv->check.status));
2321 break;
2322 case ST_F_LAST_CHK:
2323 if ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED)
2324 metric = mkf_str(FN_OUTPUT, sv->check.desc);
2325 break;
2326 case ST_F_CHECK_RISE:
2327 if ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED)
2328 metric = mkf_u32(FO_CONFIG|FS_SERVICE, ref->check.rise);
2329 break;
2330 case ST_F_CHECK_FALL:
2331 if ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED)
2332 metric = mkf_u32(FO_CONFIG|FS_SERVICE, ref->check.fall);
2333 break;
2334 case ST_F_CHECK_HEALTH:
2335 if ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED)
2336 metric = mkf_u32(FO_CONFIG|FS_SERVICE, ref->check.health);
2337 break;
Christopher Faulet59bab612021-07-22 08:04:38 +02002338 case ST_F_AGENT_STATUS:
2339 if ((sv->agent.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED) {
2340 const char *fld_chksts;
2341
2342 fld_chksts = chunk_newstr(out);
2343 chunk_strcat(out, "* "); // for check in progress
2344 chunk_strcat(out, get_check_status_info(sv->agent.status));
2345 if (!(sv->agent.state & CHK_ST_INPROGRESS))
2346 fld_chksts += 2; // skip "* "
2347 metric = mkf_str(FN_OUTPUT, fld_chksts);
2348 }
2349 break;
2350 case ST_F_AGENT_CODE:
2351 if ((sv->agent.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED &&
2352 (sv->agent.status >= HCHK_STATUS_L57DATA))
2353 metric = mkf_u32(FN_OUTPUT, sv->agent.code);
2354 break;
2355 case ST_F_AGENT_DURATION:
2356 if ((sv->agent.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED)
2357 metric = mkf_u64(FN_DURATION, sv->agent.duration);
2358 break;
2359 case ST_F_AGENT_DESC:
2360 if ((sv->agent.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED)
2361 metric = mkf_str(FN_OUTPUT, get_check_status_description(sv->agent.status));
2362 break;
2363 case ST_F_LAST_AGT:
2364 if ((sv->agent.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED)
2365 metric = mkf_str(FN_OUTPUT, sv->agent.desc);
2366 break;
2367 case ST_F_AGENT_RISE:
2368 if ((sv->agent.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED)
2369 metric = mkf_u32(FO_CONFIG|FS_SERVICE, sv->agent.rise);
2370 break;
2371 case ST_F_AGENT_FALL:
2372 if ((sv->agent.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED)
2373 metric = mkf_u32(FO_CONFIG|FS_SERVICE, sv->agent.fall);
2374 break;
2375 case ST_F_AGENT_HEALTH:
2376 if ((sv->agent.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED)
2377 metric = mkf_u32(FO_CONFIG|FS_SERVICE, sv->agent.health);
2378 break;
William Dauchyd3a9a492021-01-25 17:29:03 +01002379 case ST_F_REQ_TOT:
2380 if (px->mode == PR_MODE_HTTP)
2381 metric = mkf_u64(FN_COUNTER, sv->counters.p.http.cum_req);
2382 break;
2383 case ST_F_HRSP_1XX:
2384 if (px->mode == PR_MODE_HTTP)
2385 metric = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[1]);
2386 break;
2387 case ST_F_HRSP_2XX:
2388 if (px->mode == PR_MODE_HTTP)
2389 metric = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[2]);
2390 break;
2391 case ST_F_HRSP_3XX:
2392 if (px->mode == PR_MODE_HTTP)
2393 metric = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[3]);
2394 break;
2395 case ST_F_HRSP_4XX:
2396 if (px->mode == PR_MODE_HTTP)
2397 metric = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[4]);
2398 break;
2399 case ST_F_HRSP_5XX:
2400 if (px->mode == PR_MODE_HTTP)
2401 metric = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[5]);
2402 break;
2403 case ST_F_HRSP_OTHER:
2404 if (px->mode == PR_MODE_HTTP)
2405 metric = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[0]);
2406 break;
2407 case ST_F_HANAFAIL:
2408 if (ref->observe)
2409 metric = mkf_u64(FN_COUNTER, sv->counters.failed_hana);
2410 break;
2411 case ST_F_CLI_ABRT:
2412 metric = mkf_u64(FN_COUNTER, sv->counters.cli_aborts);
2413 break;
2414 case ST_F_SRV_ABRT:
2415 metric = mkf_u64(FN_COUNTER, sv->counters.srv_aborts);
2416 break;
2417 case ST_F_LASTSESS:
2418 metric = mkf_s32(FN_AGE, srv_lastsession(sv));
2419 break;
2420 case ST_F_QTIME:
2421 metric = mkf_u32(FN_AVG, swrate_avg(sv->counters.q_time, srv_samples_window));
2422 break;
2423 case ST_F_CTIME:
2424 metric = mkf_u32(FN_AVG, swrate_avg(sv->counters.c_time, srv_samples_window));
2425 break;
2426 case ST_F_RTIME:
2427 metric = mkf_u32(FN_AVG, swrate_avg(sv->counters.d_time, srv_samples_window));
2428 break;
2429 case ST_F_TTIME:
2430 metric = mkf_u32(FN_AVG, swrate_avg(sv->counters.t_time, srv_samples_window));
2431 break;
2432 case ST_F_QT_MAX:
2433 metric = mkf_u32(FN_MAX, sv->counters.qtime_max);
2434 break;
2435 case ST_F_CT_MAX:
2436 metric = mkf_u32(FN_MAX, sv->counters.ctime_max);
2437 break;
2438 case ST_F_RT_MAX:
2439 metric = mkf_u32(FN_MAX, sv->counters.dtime_max);
2440 break;
2441 case ST_F_TT_MAX:
2442 metric = mkf_u32(FN_MAX, sv->counters.ttime_max);
2443 break;
2444 case ST_F_ADDR:
2445 if (flags & STAT_SHLGNDS) {
2446 switch (addr_to_str(&sv->addr, str, sizeof(str))) {
2447 case AF_INET:
2448 metric = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
2449 chunk_appendf(out, "%s:%d", str, sv->svc_port);
2450 break;
2451 case AF_INET6:
2452 metric = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
2453 chunk_appendf(out, "[%s]:%d", str, sv->svc_port);
2454 break;
2455 case AF_UNIX:
2456 metric = mkf_str(FO_CONFIG|FS_SERVICE, "unix");
2457 break;
2458 case -1:
2459 metric = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
2460 chunk_strcat(out, strerror(errno));
2461 break;
2462 default: /* address family not supported */
2463 break;
2464 }
2465 }
2466 break;
2467 case ST_F_COOKIE:
2468 if (flags & STAT_SHLGNDS && sv->cookie)
2469 metric = mkf_str(FO_CONFIG|FN_NAME|FS_SERVICE, sv->cookie);
2470 break;
2471 default:
2472 /* not used for servers. If a specific metric
2473 * is requested, return an error. Otherwise continue.
2474 */
2475 if (selected_field != NULL)
2476 return 0;
2477 continue;
William Lallemand74c24fb2016-11-21 17:18:36 +01002478 }
William Dauchyd3a9a492021-01-25 17:29:03 +01002479 stats[current_field] = metric;
2480 if (selected_field != NULL)
2481 break;
William Lallemand74c24fb2016-11-21 17:18:36 +01002482 }
William Lallemand74c24fb2016-11-21 17:18:36 +01002483 return 1;
2484}
2485
2486/* Dumps a line for server <sv> and proxy <px> to the trash and uses the state
Christopher Faulet908628c2022-03-25 16:43:49 +01002487 * from conn-stream <cs>, and server state <state>. The caller is
Willy Tarreau43241ff2019-10-09 11:27:51 +02002488 * responsible for clearing the trash if needed. Returns non-zero if it emits
2489 * anything, zero otherwise.
William Lallemand74c24fb2016-11-21 17:18:36 +01002490 */
Christopher Faulet908628c2022-03-25 16:43:49 +01002491static int stats_dump_sv_stats(struct conn_stream *cs, struct proxy *px, struct server *sv)
William Lallemand74c24fb2016-11-21 17:18:36 +01002492{
Christopher Faulet908628c2022-03-25 16:43:49 +01002493 struct appctx *appctx = __cs_appctx(cs);
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02002494 struct stats_module *mod;
2495 struct field *stats = stat_l[STATS_DOMAIN_PROXY];
2496 size_t stats_count = ST_F_TOTAL_FIELDS;
2497
2498 memset(stats, 0, sizeof(struct field) * stat_count[STATS_DOMAIN_PROXY]);
William Lallemand74c24fb2016-11-21 17:18:36 +01002499
William Dauchyd3a9a492021-01-25 17:29:03 +01002500 if (!stats_fill_sv_stats(px, sv, appctx->ctx.stats.flags, stats,
2501 ST_F_TOTAL_FIELDS, NULL))
William Lallemand74c24fb2016-11-21 17:18:36 +01002502 return 0;
2503
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02002504 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
2505 void *counters;
2506
2507 if (stats_get_domain(mod->domain_flags) != STATS_DOMAIN_PROXY)
2508 continue;
2509
2510 if (!(stats_px_get_cap(mod->domain_flags) & STATS_PX_CAP_SRV)) {
2511 stats_count += mod->stats_count;
2512 continue;
2513 }
2514
2515 counters = EXTRA_COUNTERS_GET(sv->extra_counters, mod);
2516 mod->fill_stats(counters, stats + stats_count);
2517 stats_count += mod->stats_count;
2518 }
2519
2520 return stats_dump_one_line(stats, stats_count, appctx);
William Lallemand74c24fb2016-11-21 17:18:36 +01002521}
2522
William Dauchyda3b4662021-01-25 17:29:01 +01002523/* Helper to compute srv values for a given backend
William Lallemand74c24fb2016-11-21 17:18:36 +01002524 */
William Dauchyda3b4662021-01-25 17:29:01 +01002525static void stats_fill_be_stats_computesrv(struct proxy *px, int *nbup, int *nbsrv, int *totuw)
William Lallemand74c24fb2016-11-21 17:18:36 +01002526{
William Dauchyda3b4662021-01-25 17:29:01 +01002527 int nbup_tmp, nbsrv_tmp, totuw_tmp;
Willy Tarreau2fbe6942020-10-23 18:02:54 +02002528 const struct server *srv;
William Lallemand74c24fb2016-11-21 17:18:36 +01002529
William Dauchyda3b4662021-01-25 17:29:01 +01002530 nbup_tmp = nbsrv_tmp = totuw_tmp = 0;
Willy Tarreaubd715102020-10-23 22:44:30 +02002531 for (srv = px->srv; srv; srv = srv->next) {
2532 if (srv->cur_state != SRV_ST_STOPPED) {
William Dauchyda3b4662021-01-25 17:29:01 +01002533 nbup_tmp++;
Willy Tarreaubd715102020-10-23 22:44:30 +02002534 if (srv_currently_usable(srv) &&
2535 (!px->srv_act ^ !(srv->flags & SRV_F_BACKUP)))
William Dauchyda3b4662021-01-25 17:29:01 +01002536 totuw_tmp += srv->uweight;
Willy Tarreau2fbe6942020-10-23 18:02:54 +02002537 }
William Dauchyda3b4662021-01-25 17:29:01 +01002538 nbsrv_tmp++;
Willy Tarreau2fbe6942020-10-23 18:02:54 +02002539 }
2540
Willy Tarreaubd715102020-10-23 22:44:30 +02002541 HA_RWLOCK_RDLOCK(LBPRM_LOCK, &px->lbprm.lock);
2542 if (!px->srv_act && px->lbprm.fbck)
William Dauchyda3b4662021-01-25 17:29:01 +01002543 totuw_tmp = px->lbprm.fbck->uweight;
Willy Tarreaubd715102020-10-23 22:44:30 +02002544 HA_RWLOCK_RDUNLOCK(LBPRM_LOCK, &px->lbprm.lock);
2545
William Dauchyda3b4662021-01-25 17:29:01 +01002546 /* use tmp variable then assign result to make gcc happy */
2547 *nbup = nbup_tmp;
2548 *nbsrv = nbsrv_tmp;
2549 *totuw = totuw_tmp;
2550}
Willy Tarreau2fbe6942020-10-23 18:02:54 +02002551
William Dauchyda3b4662021-01-25 17:29:01 +01002552/* Fill <stats> with the backend statistics. <stats> is preallocated array of
2553 * length <len>. If <selected_field> is != NULL, only fill this one. The length
2554 * of the array must be at least ST_F_TOTAL_FIELDS. If this length is less than
2555 * this value, or if the selected field is not implemented for backends, the
2556 * function returns 0, otherwise, it returns 1. <flags> can take the value
2557 * STAT_SHLGNDS.
2558 */
2559int stats_fill_be_stats(struct proxy *px, int flags, struct field *stats, int len,
2560 enum stat_field *selected_field)
2561{
2562 enum stat_field current_field = (selected_field != NULL ? *selected_field : 0);
2563 long long be_samples_counter;
2564 unsigned int be_samples_window = TIME_STATS_SAMPLES;
2565 struct buffer *out = get_trash_chunk();
2566 int nbup, nbsrv, totuw;
2567 char *fld;
William Lallemand74c24fb2016-11-21 17:18:36 +01002568
William Dauchyda3b4662021-01-25 17:29:01 +01002569 if (len < ST_F_TOTAL_FIELDS)
2570 return 0;
William Lallemand74c24fb2016-11-21 17:18:36 +01002571
William Dauchyda3b4662021-01-25 17:29:01 +01002572 nbup = nbsrv = totuw = 0;
2573 /* some srv values compute for later if we either select all fields or
2574 * need them for one of the mentioned ones */
2575 if (selected_field == NULL || *selected_field == ST_F_STATUS ||
2576 *selected_field == ST_F_UWEIGHT)
2577 stats_fill_be_stats_computesrv(px, &nbup, &nbsrv, &totuw);
William Lallemand74c24fb2016-11-21 17:18:36 +01002578
William Dauchyda3b4662021-01-25 17:29:01 +01002579 /* same here but specific to time fields */
2580 if (selected_field == NULL || *selected_field == ST_F_QTIME ||
2581 *selected_field == ST_F_CTIME || *selected_field == ST_F_RTIME ||
2582 *selected_field == ST_F_TTIME) {
2583 be_samples_counter = (px->mode == PR_MODE_HTTP) ? px->be_counters.p.http.cum_req : px->be_counters.cum_lbconn;
2584 if (be_samples_counter < TIME_STATS_SAMPLES && be_samples_counter > 0)
2585 be_samples_window = be_samples_counter;
William Lallemand74c24fb2016-11-21 17:18:36 +01002586 }
2587
William Dauchyda3b4662021-01-25 17:29:01 +01002588 for (; current_field < ST_F_TOTAL_FIELDS; current_field++) {
2589 struct field metric = { 0 };
William Lallemand74c24fb2016-11-21 17:18:36 +01002590
William Dauchyda3b4662021-01-25 17:29:01 +01002591 switch (current_field) {
2592 case ST_F_PXNAME:
2593 metric = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
2594 break;
2595 case ST_F_SVNAME:
2596 metric = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, "BACKEND");
2597 break;
2598 case ST_F_MODE:
2599 metric = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
2600 break;
2601 case ST_F_QCUR:
Willy Tarreau7f3c1df2021-06-18 09:22:21 +02002602 metric = mkf_u32(0, px->queue.length);
William Dauchyda3b4662021-01-25 17:29:01 +01002603 break;
2604 case ST_F_QMAX:
2605 metric = mkf_u32(FN_MAX, px->be_counters.nbpend_max);
2606 break;
2607 case ST_F_SCUR:
2608 metric = mkf_u32(0, px->beconn);
2609 break;
2610 case ST_F_SMAX:
2611 metric = mkf_u32(FN_MAX, px->be_counters.conn_max);
2612 break;
2613 case ST_F_SLIM:
2614 metric = mkf_u32(FO_CONFIG|FN_LIMIT, px->fullconn);
2615 break;
2616 case ST_F_STOT:
2617 metric = mkf_u64(FN_COUNTER, px->be_counters.cum_conn);
2618 break;
2619 case ST_F_BIN:
2620 metric = mkf_u64(FN_COUNTER, px->be_counters.bytes_in);
2621 break;
2622 case ST_F_BOUT:
2623 metric = mkf_u64(FN_COUNTER, px->be_counters.bytes_out);
2624 break;
2625 case ST_F_DREQ:
2626 metric = mkf_u64(FN_COUNTER, px->be_counters.denied_req);
2627 break;
2628 case ST_F_DRESP:
2629 metric = mkf_u64(FN_COUNTER, px->be_counters.denied_resp);
2630 break;
2631 case ST_F_ECON:
2632 metric = mkf_u64(FN_COUNTER, px->be_counters.failed_conns);
2633 break;
2634 case ST_F_ERESP:
2635 metric = mkf_u64(FN_COUNTER, px->be_counters.failed_resp);
2636 break;
2637 case ST_F_WRETR:
2638 metric = mkf_u64(FN_COUNTER, px->be_counters.retries);
2639 break;
2640 case ST_F_WREDIS:
2641 metric = mkf_u64(FN_COUNTER, px->be_counters.redispatches);
2642 break;
2643 case ST_F_WREW:
2644 metric = mkf_u64(FN_COUNTER, px->be_counters.failed_rewrites);
2645 break;
2646 case ST_F_EINT:
2647 metric = mkf_u64(FN_COUNTER, px->be_counters.internal_errors);
2648 break;
2649 case ST_F_CONNECT:
2650 metric = mkf_u64(FN_COUNTER, px->be_counters.connect);
2651 break;
2652 case ST_F_REUSE:
2653 metric = mkf_u64(FN_COUNTER, px->be_counters.reuse);
2654 break;
2655 case ST_F_STATUS:
2656 fld = chunk_newstr(out);
2657 chunk_appendf(out, "%s", (px->lbprm.tot_weight > 0 || !px->srv) ? "UP" : "DOWN");
2658 if (flags & (STAT_HIDE_MAINT|STAT_HIDE_DOWN))
2659 chunk_appendf(out, " (%d/%d)", nbup, nbsrv);
2660 metric = mkf_str(FO_STATUS, fld);
2661 break;
William Dauchy42d7c402021-11-07 10:18:47 +01002662 case ST_F_AGG_SRV_CHECK_STATUS:
2663 metric = mkf_u32(FN_GAUGE, 0);
2664 break;
William Dauchyda3b4662021-01-25 17:29:01 +01002665 case ST_F_WEIGHT:
2666 metric = mkf_u32(FN_AVG, (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv);
2667 break;
2668 case ST_F_UWEIGHT:
2669 metric = mkf_u32(FN_AVG, totuw);
2670 break;
2671 case ST_F_ACT:
2672 metric = mkf_u32(0, px->srv_act);
2673 break;
2674 case ST_F_BCK:
2675 metric = mkf_u32(0, px->srv_bck);
2676 break;
2677 case ST_F_CHKDOWN:
2678 metric = mkf_u64(FN_COUNTER, px->down_trans);
2679 break;
2680 case ST_F_LASTCHG:
2681 metric = mkf_u32(FN_AGE, now.tv_sec - px->last_change);
2682 break;
2683 case ST_F_DOWNTIME:
2684 if (px->srv)
2685 metric = mkf_u32(FN_COUNTER, be_downtime(px));
2686 break;
2687 case ST_F_PID:
Willy Tarreaue8422bf2021-06-15 09:08:18 +02002688 metric = mkf_u32(FO_KEY, 1);
William Dauchyda3b4662021-01-25 17:29:01 +01002689 break;
2690 case ST_F_IID:
2691 metric = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
2692 break;
2693 case ST_F_SID:
2694 metric = mkf_u32(FO_KEY|FS_SERVICE, 0);
2695 break;
2696 case ST_F_LBTOT:
2697 metric = mkf_u64(FN_COUNTER, px->be_counters.cum_lbconn);
2698 break;
2699 case ST_F_TYPE:
2700 metric = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_BE);
2701 break;
2702 case ST_F_RATE:
2703 metric = mkf_u32(0, read_freq_ctr(&px->be_sess_per_sec));
2704 break;
2705 case ST_F_RATE_MAX:
2706 metric = mkf_u32(0, px->be_counters.sps_max);
2707 break;
2708 case ST_F_COOKIE:
2709 if (flags & STAT_SHLGNDS && px->cookie_name)
2710 metric = mkf_str(FO_CONFIG|FN_NAME|FS_SERVICE, px->cookie_name);
2711 break;
2712 case ST_F_ALGO:
2713 if (flags & STAT_SHLGNDS)
2714 metric = mkf_str(FO_CONFIG|FS_SERVICE, backend_lb_algo_str(px->lbprm.algo & BE_LB_ALGO));
2715 break;
2716 case ST_F_REQ_TOT:
2717 if (px->mode == PR_MODE_HTTP)
2718 metric = mkf_u64(FN_COUNTER, px->be_counters.p.http.cum_req);
2719 break;
2720 case ST_F_HRSP_1XX:
2721 if (px->mode == PR_MODE_HTTP)
2722 metric = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[1]);
2723 break;
2724 case ST_F_HRSP_2XX:
2725 if (px->mode == PR_MODE_HTTP)
2726 metric = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[2]);
2727 break;
2728 case ST_F_HRSP_3XX:
2729 if (px->mode == PR_MODE_HTTP)
2730 metric = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[3]);
2731 break;
2732 case ST_F_HRSP_4XX:
2733 if (px->mode == PR_MODE_HTTP)
2734 metric = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[4]);
2735 break;
2736 case ST_F_HRSP_5XX:
2737 if (px->mode == PR_MODE_HTTP)
2738 metric = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[5]);
2739 break;
2740 case ST_F_HRSP_OTHER:
2741 if (px->mode == PR_MODE_HTTP)
2742 metric = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[0]);
2743 break;
2744 case ST_F_CACHE_LOOKUPS:
2745 if (px->mode == PR_MODE_HTTP)
2746 metric = mkf_u64(FN_COUNTER, px->be_counters.p.http.cache_lookups);
2747 break;
2748 case ST_F_CACHE_HITS:
2749 if (px->mode == PR_MODE_HTTP)
2750 metric = mkf_u64(FN_COUNTER, px->be_counters.p.http.cache_hits);
2751 break;
2752 case ST_F_CLI_ABRT:
2753 metric = mkf_u64(FN_COUNTER, px->be_counters.cli_aborts);
2754 break;
2755 case ST_F_SRV_ABRT:
2756 metric = mkf_u64(FN_COUNTER, px->be_counters.srv_aborts);
2757 break;
2758 case ST_F_COMP_IN:
2759 metric = mkf_u64(FN_COUNTER, px->be_counters.comp_in);
2760 break;
2761 case ST_F_COMP_OUT:
2762 metric = mkf_u64(FN_COUNTER, px->be_counters.comp_out);
2763 break;
2764 case ST_F_COMP_BYP:
2765 metric = mkf_u64(FN_COUNTER, px->be_counters.comp_byp);
2766 break;
2767 case ST_F_COMP_RSP:
2768 metric = mkf_u64(FN_COUNTER, px->be_counters.p.http.comp_rsp);
2769 break;
2770 case ST_F_LASTSESS:
2771 metric = mkf_s32(FN_AGE, be_lastsession(px));
2772 break;
2773 case ST_F_QTIME:
2774 metric = mkf_u32(FN_AVG, swrate_avg(px->be_counters.q_time, be_samples_window));
2775 break;
2776 case ST_F_CTIME:
2777 metric = mkf_u32(FN_AVG, swrate_avg(px->be_counters.c_time, be_samples_window));
2778 break;
2779 case ST_F_RTIME:
2780 metric = mkf_u32(FN_AVG, swrate_avg(px->be_counters.d_time, be_samples_window));
2781 break;
2782 case ST_F_TTIME:
2783 metric = mkf_u32(FN_AVG, swrate_avg(px->be_counters.t_time, be_samples_window));
2784 break;
2785 case ST_F_QT_MAX:
2786 metric = mkf_u32(FN_MAX, px->be_counters.qtime_max);
2787 break;
2788 case ST_F_CT_MAX:
2789 metric = mkf_u32(FN_MAX, px->be_counters.ctime_max);
2790 break;
2791 case ST_F_RT_MAX:
2792 metric = mkf_u32(FN_MAX, px->be_counters.dtime_max);
2793 break;
2794 case ST_F_TT_MAX:
2795 metric = mkf_u32(FN_MAX, px->be_counters.ttime_max);
2796 break;
2797 default:
2798 /* not used for backends. If a specific metric
2799 * is requested, return an error. Otherwise continue.
2800 */
2801 if (selected_field != NULL)
2802 return 0;
2803 continue;
2804 }
2805 stats[current_field] = metric;
2806 if (selected_field != NULL)
2807 break;
2808 }
William Lallemand74c24fb2016-11-21 17:18:36 +01002809 return 1;
2810}
2811
2812/* Dumps a line for backend <px> to the trash for and uses the state from stream
Willy Tarreau43241ff2019-10-09 11:27:51 +02002813 * interface <si>. The caller is responsible for clearing the trash if needed.
2814 * Returns non-zero if it emits anything, zero otherwise.
William Lallemand74c24fb2016-11-21 17:18:36 +01002815 */
Christopher Faulet908628c2022-03-25 16:43:49 +01002816static int stats_dump_be_stats(struct conn_stream *cs, struct proxy *px)
William Lallemand74c24fb2016-11-21 17:18:36 +01002817{
Christopher Faulet908628c2022-03-25 16:43:49 +01002818 struct appctx *appctx = __cs_appctx(cs);
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02002819 struct field *stats = stat_l[STATS_DOMAIN_PROXY];
2820 struct stats_module *mod;
2821 size_t stats_count = ST_F_TOTAL_FIELDS;
William Lallemand74c24fb2016-11-21 17:18:36 +01002822
2823 if (!(px->cap & PR_CAP_BE))
2824 return 0;
2825
2826 if ((appctx->ctx.stats.flags & STAT_BOUND) && !(appctx->ctx.stats.type & (1 << STATS_TYPE_BE)))
2827 return 0;
2828
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02002829 memset(stats, 0, sizeof(struct field) * stat_count[STATS_DOMAIN_PROXY]);
2830
William Dauchyda3b4662021-01-25 17:29:01 +01002831 if (!stats_fill_be_stats(px, appctx->ctx.stats.flags, stats, ST_F_TOTAL_FIELDS, NULL))
William Lallemand74c24fb2016-11-21 17:18:36 +01002832 return 0;
2833
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02002834 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
2835 struct extra_counters *counters;
2836
2837 if (stats_get_domain(mod->domain_flags) != STATS_DOMAIN_PROXY)
2838 continue;
2839
2840 if (!(stats_px_get_cap(mod->domain_flags) & STATS_PX_CAP_BE)) {
2841 stats_count += mod->stats_count;
2842 continue;
2843 }
2844
2845 counters = EXTRA_COUNTERS_GET(px->extra_counters_be, mod);
2846 mod->fill_stats(counters, stats + stats_count);
2847 stats_count += mod->stats_count;
2848 }
2849
2850 return stats_dump_one_line(stats, stats_count, appctx);
William Lallemand74c24fb2016-11-21 17:18:36 +01002851}
2852
2853/* Dumps the HTML table header for proxy <px> to the trash for and uses the state from
Christopher Faulet908628c2022-03-25 16:43:49 +01002854 * conn-stream <cs> and per-uri parameters <uri>. The caller is responsible
William Lallemand74c24fb2016-11-21 17:18:36 +01002855 * for clearing the trash if needed.
2856 */
Christopher Faulet908628c2022-03-25 16:43:49 +01002857static void stats_dump_html_px_hdr(struct conn_stream *cs, struct proxy *px)
William Lallemand74c24fb2016-11-21 17:18:36 +01002858{
Christopher Faulet908628c2022-03-25 16:43:49 +01002859 struct appctx *appctx = __cs_appctx(cs);
William Lallemand74c24fb2016-11-21 17:18:36 +01002860 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02002861 struct stats_module *mod;
2862 int stats_module_len = 0;
William Lallemand74c24fb2016-11-21 17:18:36 +01002863
2864 if (px->cap & PR_CAP_BE && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
2865 /* A form to enable/disable this proxy servers */
2866
2867 /* scope_txt = search pattern + search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
2868 scope_txt[0] = 0;
2869 if (appctx->ctx.stats.scope_len) {
Christopher Faulet908628c2022-03-25 16:43:49 +01002870 const char *scope_ptr = stats_scope_ptr(appctx, cs);
Christopher Fauleted7a0662019-01-14 11:07:34 +01002871
William Lallemand74c24fb2016-11-21 17:18:36 +01002872 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Christopher Fauleted7a0662019-01-14 11:07:34 +01002873 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), scope_ptr, appctx->ctx.stats.scope_len);
William Lallemand74c24fb2016-11-21 17:18:36 +01002874 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
2875 }
2876
2877 chunk_appendf(&trash,
2878 "<form method=\"post\">");
2879 }
2880
2881 /* print a new table */
2882 chunk_appendf(&trash,
2883 "<table class=\"tbl\" width=\"100%%\">\n"
2884 "<tr class=\"titre\">"
2885 "<th class=\"pxname\" width=\"10%%\">");
2886
2887 chunk_appendf(&trash,
2888 "<a name=\"%s\"></a>%s"
2889 "<a class=px href=\"#%s\">%s</a>",
2890 px->id,
Willy Tarreau676c29e2019-10-09 10:50:01 +02002891 (appctx->ctx.stats.flags & STAT_SHLGNDS) ? "<u>":"",
William Lallemand74c24fb2016-11-21 17:18:36 +01002892 px->id, px->id);
2893
Willy Tarreau676c29e2019-10-09 10:50:01 +02002894 if (appctx->ctx.stats.flags & STAT_SHLGNDS) {
William Lallemand74c24fb2016-11-21 17:18:36 +01002895 /* cap, mode, id */
2896 chunk_appendf(&trash, "<div class=tips>cap: %s, mode: %s, id: %d",
2897 proxy_cap_str(px->cap), proxy_mode_str(px->mode),
2898 px->uuid);
2899 chunk_appendf(&trash, "</div>");
2900 }
2901
2902 chunk_appendf(&trash,
2903 "%s</th>"
2904 "<th class=\"%s\" width=\"90%%\">%s</th>"
2905 "</tr>\n"
2906 "</table>\n"
2907 "<table class=\"tbl\" width=\"100%%\">\n"
2908 "<tr class=\"titre\">",
Willy Tarreau676c29e2019-10-09 10:50:01 +02002909 (appctx->ctx.stats.flags & STAT_SHLGNDS) ? "</u>":"",
William Lallemand74c24fb2016-11-21 17:18:36 +01002910 px->desc ? "desc" : "empty", px->desc ? px->desc : "");
2911
Christopher Fauletbc271ec2019-12-02 11:29:04 +01002912 if (appctx->ctx.stats.flags & STAT_ADMIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +01002913 /* Column heading for Enable or Disable server */
Christopher Fauletbc271ec2019-12-02 11:29:04 +01002914 if ((px->cap & PR_CAP_BE) && px->srv)
2915 chunk_appendf(&trash,
2916 "<th rowspan=2 width=1><input type=\"checkbox\" "
2917 "onclick=\"for(c in document.getElementsByClassName('%s-checkbox')) "
2918 "document.getElementsByClassName('%s-checkbox').item(c).checked = this.checked\"></th>",
2919 px->id,
2920 px->id);
2921 else
2922 chunk_appendf(&trash, "<th rowspan=2></th>");
William Lallemand74c24fb2016-11-21 17:18:36 +01002923 }
2924
2925 chunk_appendf(&trash,
2926 "<th rowspan=2></th>"
2927 "<th colspan=3>Queue</th>"
2928 "<th colspan=3>Session rate</th><th colspan=6>Sessions</th>"
2929 "<th colspan=2>Bytes</th><th colspan=2>Denied</th>"
2930 "<th colspan=3>Errors</th><th colspan=2>Warnings</th>"
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02002931 "<th colspan=9>Server</th>");
2932
2933 if (appctx->ctx.stats.flags & STAT_SHMODULES) {
2934 // calculate the count of module for colspan attribute
2935 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
2936 ++stats_module_len;
2937 }
2938 chunk_appendf(&trash, "<th colspan=%d>Extra modules</th>",
2939 stats_module_len);
2940 }
2941
2942 chunk_appendf(&trash,
William Lallemand74c24fb2016-11-21 17:18:36 +01002943 "</tr>\n"
2944 "<tr class=\"titre\">"
2945 "<th>Cur</th><th>Max</th><th>Limit</th>"
2946 "<th>Cur</th><th>Max</th><th>Limit</th><th>Cur</th><th>Max</th>"
2947 "<th>Limit</th><th>Total</th><th>LbTot</th><th>Last</th><th>In</th><th>Out</th>"
2948 "<th>Req</th><th>Resp</th><th>Req</th><th>Conn</th>"
2949 "<th>Resp</th><th>Retr</th><th>Redis</th>"
2950 "<th>Status</th><th>LastChk</th><th>Wght</th><th>Act</th>"
2951 "<th>Bck</th><th>Chk</th><th>Dwn</th><th>Dwntme</th>"
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02002952 "<th>Thrtle</th>\n");
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02002953
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02002954 if (appctx->ctx.stats.flags & STAT_SHMODULES) {
2955 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
2956 chunk_appendf(&trash, "<th>%s</th>", mod->name);
2957 }
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02002958 }
2959
2960 chunk_appendf(&trash, "</tr>");
William Lallemand74c24fb2016-11-21 17:18:36 +01002961}
2962
2963/* Dumps the HTML table trailer for proxy <px> to the trash for and uses the state from
Christopher Faulet908628c2022-03-25 16:43:49 +01002964 * conn_stream <cs>. The caller is responsible for clearing the trash if needed.
William Lallemand74c24fb2016-11-21 17:18:36 +01002965 */
Christopher Faulet908628c2022-03-25 16:43:49 +01002966static void stats_dump_html_px_end(struct conn_stream *cs, struct proxy *px)
William Lallemand74c24fb2016-11-21 17:18:36 +01002967{
Christopher Faulet908628c2022-03-25 16:43:49 +01002968 struct appctx *appctx = __cs_appctx(cs);
Christopher Faulet693b23b2022-02-28 09:09:05 +01002969
William Lallemand74c24fb2016-11-21 17:18:36 +01002970 chunk_appendf(&trash, "</table>");
2971
2972 if ((px->cap & PR_CAP_BE) && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
2973 /* close the form used to enable/disable this proxy servers */
2974 chunk_appendf(&trash,
2975 "Choose the action to perform on the checked servers : "
2976 "<select name=action>"
2977 "<option value=\"\"></option>"
2978 "<option value=\"ready\">Set state to READY</option>"
2979 "<option value=\"drain\">Set state to DRAIN</option>"
2980 "<option value=\"maint\">Set state to MAINT</option>"
2981 "<option value=\"dhlth\">Health: disable checks</option>"
2982 "<option value=\"ehlth\">Health: enable checks</option>"
2983 "<option value=\"hrunn\">Health: force UP</option>"
2984 "<option value=\"hnolb\">Health: force NOLB</option>"
2985 "<option value=\"hdown\">Health: force DOWN</option>"
2986 "<option value=\"dagent\">Agent: disable checks</option>"
2987 "<option value=\"eagent\">Agent: enable checks</option>"
2988 "<option value=\"arunn\">Agent: force UP</option>"
2989 "<option value=\"adown\">Agent: force DOWN</option>"
2990 "<option value=\"shutdown\">Kill Sessions</option>"
2991 "</select>"
2992 "<input type=\"hidden\" name=\"b\" value=\"#%d\">"
2993 "&nbsp;<input type=\"submit\" value=\"Apply\">"
2994 "</form>",
2995 px->uuid);
2996 }
2997
2998 chunk_appendf(&trash, "<p>\n");
2999}
3000
3001/*
Christopher Faulet908628c2022-03-25 16:43:49 +01003002 * Dumps statistics for a proxy. The output is sent to the conn-stream's
William Lallemand74c24fb2016-11-21 17:18:36 +01003003 * input buffer. Returns 0 if it had to stop dumping data because of lack of
3004 * buffer space, or non-zero if everything completed. This function is used
3005 * both by the CLI and the HTTP entry points, and is able to dump the output
3006 * in HTML or CSV formats. If the later, <uri> must be NULL.
3007 */
Christopher Faulet908628c2022-03-25 16:43:49 +01003008int stats_dump_proxy_to_buffer(struct conn_stream *cs, struct htx *htx,
Christopher Fauletef779222018-10-31 08:47:01 +01003009 struct proxy *px, struct uri_auth *uri)
William Lallemand74c24fb2016-11-21 17:18:36 +01003010{
Christopher Faulet908628c2022-03-25 16:43:49 +01003011 struct appctx *appctx = __cs_appctx(cs);
3012 struct stream *s = __cs_strm(cs);
3013 struct channel *rep = cs_ic(cs);
William Lallemand74c24fb2016-11-21 17:18:36 +01003014 struct server *sv, *svs; /* server and server-state, server-state=server or server->track */
3015 struct listener *l;
William Lallemand74c24fb2016-11-21 17:18:36 +01003016
3017 chunk_reset(&trash);
3018
3019 switch (appctx->ctx.stats.px_st) {
3020 case STAT_PX_ST_INIT:
3021 /* we are on a new proxy */
3022 if (uri && uri->scope) {
3023 /* we have a limited scope, we have to check the proxy name */
3024 struct stat_scope *scope;
3025 int len;
3026
3027 len = strlen(px->id);
3028 scope = uri->scope;
3029
3030 while (scope) {
3031 /* match exact proxy name */
3032 if (scope->px_len == len && !memcmp(px->id, scope->px_id, len))
3033 break;
3034
3035 /* match '.' which means 'self' proxy */
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003036 if (strcmp(scope->px_id, ".") == 0 && px == s->be)
William Lallemand74c24fb2016-11-21 17:18:36 +01003037 break;
3038 scope = scope->next;
3039 }
3040
3041 /* proxy name not found : don't dump anything */
3042 if (scope == NULL)
3043 return 1;
3044 }
3045
3046 /* if the user has requested a limited output and the proxy
3047 * name does not match, skip it.
3048 */
Christopher Fauleted7a0662019-01-14 11:07:34 +01003049 if (appctx->ctx.stats.scope_len) {
Christopher Faulet908628c2022-03-25 16:43:49 +01003050 const char *scope_ptr = stats_scope_ptr(appctx, cs);
Christopher Fauleted7a0662019-01-14 11:07:34 +01003051
3052 if (strnistr(px->id, strlen(px->id), scope_ptr, appctx->ctx.stats.scope_len) == NULL)
3053 return 1;
3054 }
William Lallemand74c24fb2016-11-21 17:18:36 +01003055
3056 if ((appctx->ctx.stats.flags & STAT_BOUND) &&
3057 (appctx->ctx.stats.iid != -1) &&
3058 (px->uuid != appctx->ctx.stats.iid))
3059 return 1;
3060
3061 appctx->ctx.stats.px_st = STAT_PX_ST_TH;
3062 /* fall through */
3063
3064 case STAT_PX_ST_TH:
3065 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Christopher Faulet908628c2022-03-25 16:43:49 +01003066 stats_dump_html_px_hdr(cs, px);
Christopher Fauletef779222018-10-31 08:47:01 +01003067 if (!stats_putchk(rep, htx, &trash))
3068 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01003069 }
3070
3071 appctx->ctx.stats.px_st = STAT_PX_ST_FE;
3072 /* fall through */
3073
3074 case STAT_PX_ST_FE:
3075 /* print the frontend */
Christopher Faulet908628c2022-03-25 16:43:49 +01003076 if (stats_dump_fe_stats(cs, px)) {
Christopher Fauletef779222018-10-31 08:47:01 +01003077 if (!stats_putchk(rep, htx, &trash))
3078 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01003079 }
3080
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +02003081 appctx->ctx.stats.obj2 = px->conf.listeners.n;
William Lallemand74c24fb2016-11-21 17:18:36 +01003082 appctx->ctx.stats.px_st = STAT_PX_ST_LI;
3083 /* fall through */
3084
3085 case STAT_PX_ST_LI:
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +02003086 /* obj2 points to listeners list as initialized above */
3087 for (; appctx->ctx.stats.obj2 != &px->conf.listeners; appctx->ctx.stats.obj2 = l->by_fe.n) {
Christopher Fauletef779222018-10-31 08:47:01 +01003088 if (htx) {
3089 if (htx_almost_full(htx))
3090 goto full;
3091 }
3092 else {
3093 if (buffer_almost_full(&rep->buf))
3094 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01003095 }
3096
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +02003097 l = LIST_ELEM(appctx->ctx.stats.obj2, struct listener *, by_fe);
William Lallemand74c24fb2016-11-21 17:18:36 +01003098 if (!l->counters)
3099 continue;
3100
3101 if (appctx->ctx.stats.flags & STAT_BOUND) {
3102 if (!(appctx->ctx.stats.type & (1 << STATS_TYPE_SO)))
3103 break;
3104
3105 if (appctx->ctx.stats.sid != -1 && l->luid != appctx->ctx.stats.sid)
3106 continue;
3107 }
3108
3109 /* print the frontend */
Christopher Faulet908628c2022-03-25 16:43:49 +01003110 if (stats_dump_li_stats(cs, px, l)) {
Christopher Fauletef779222018-10-31 08:47:01 +01003111 if (!stats_putchk(rep, htx, &trash))
3112 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01003113 }
3114 }
3115
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +02003116 appctx->ctx.stats.obj2 = px->srv; /* may be NULL */
William Lallemand74c24fb2016-11-21 17:18:36 +01003117 appctx->ctx.stats.px_st = STAT_PX_ST_SV;
3118 /* fall through */
3119
3120 case STAT_PX_ST_SV:
Amaury Denoyelle0a8d05d2021-08-25 14:39:53 +02003121 /* obj2 points to servers list as initialized above.
3122 *
Amaury Denoyellebc2ebfa2021-08-25 15:34:53 +02003123 * A server may be removed during the stats dumping.
Amaury Denoyelle0a8d05d2021-08-25 14:39:53 +02003124 * Temporarily increment its refcount to prevent its
3125 * anticipated cleaning. Call free_server to release it.
3126 */
3127 for (; appctx->ctx.stats.obj2 != NULL;
Amaury Denoyellebc2ebfa2021-08-25 15:34:53 +02003128 appctx->ctx.stats.obj2 = srv_drop(sv)) {
Amaury Denoyelle0a8d05d2021-08-25 14:39:53 +02003129
3130 sv = appctx->ctx.stats.obj2;
Amaury Denoyellebc2ebfa2021-08-25 15:34:53 +02003131 srv_take(sv);
Amaury Denoyelle0a8d05d2021-08-25 14:39:53 +02003132
Christopher Fauletef779222018-10-31 08:47:01 +01003133 if (htx) {
3134 if (htx_almost_full(htx))
3135 goto full;
3136 }
3137 else {
3138 if (buffer_almost_full(&rep->buf))
3139 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01003140 }
3141
William Lallemand74c24fb2016-11-21 17:18:36 +01003142 if (appctx->ctx.stats.flags & STAT_BOUND) {
Amaury Denoyelle0a8d05d2021-08-25 14:39:53 +02003143 if (!(appctx->ctx.stats.type & (1 << STATS_TYPE_SV))) {
Amaury Denoyellebc2ebfa2021-08-25 15:34:53 +02003144 srv_drop(sv);
William Lallemand74c24fb2016-11-21 17:18:36 +01003145 break;
Amaury Denoyelle0a8d05d2021-08-25 14:39:53 +02003146 }
William Lallemand74c24fb2016-11-21 17:18:36 +01003147
3148 if (appctx->ctx.stats.sid != -1 && sv->puid != appctx->ctx.stats.sid)
3149 continue;
3150 }
3151
Willy Tarreau3e320362020-10-23 17:28:57 +02003152 /* do not report disabled servers */
3153 if (appctx->ctx.stats.flags & STAT_HIDE_MAINT &&
3154 sv->cur_admin & SRV_ADMF_MAINT) {
3155 continue;
3156 }
3157
William Lallemand74c24fb2016-11-21 17:18:36 +01003158 svs = sv;
3159 while (svs->track)
3160 svs = svs->track;
3161
3162 /* do not report servers which are DOWN and not changing state */
3163 if ((appctx->ctx.stats.flags & STAT_HIDE_DOWN) &&
Emeric Brun52a91d32017-08-31 14:41:55 +02003164 ((sv->cur_admin & SRV_ADMF_MAINT) || /* server is in maintenance */
3165 (sv->cur_state == SRV_ST_STOPPED && /* server is down */
William Lallemand74c24fb2016-11-21 17:18:36 +01003166 (!((svs->agent.state | svs->check.state) & CHK_ST_ENABLED) ||
3167 ((svs->agent.state & CHK_ST_ENABLED) && !svs->agent.health) ||
3168 ((svs->check.state & CHK_ST_ENABLED) && !svs->check.health))))) {
3169 continue;
3170 }
3171
Christopher Faulet908628c2022-03-25 16:43:49 +01003172 if (stats_dump_sv_stats(cs, px, sv)) {
Christopher Fauletef779222018-10-31 08:47:01 +01003173 if (!stats_putchk(rep, htx, &trash))
3174 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01003175 }
3176 } /* for sv */
3177
3178 appctx->ctx.stats.px_st = STAT_PX_ST_BE;
3179 /* fall through */
3180
3181 case STAT_PX_ST_BE:
3182 /* print the backend */
Christopher Faulet908628c2022-03-25 16:43:49 +01003183 if (stats_dump_be_stats(cs, px)) {
Christopher Fauletef779222018-10-31 08:47:01 +01003184 if (!stats_putchk(rep, htx, &trash))
3185 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01003186 }
3187
3188 appctx->ctx.stats.px_st = STAT_PX_ST_END;
3189 /* fall through */
3190
3191 case STAT_PX_ST_END:
3192 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Christopher Faulet908628c2022-03-25 16:43:49 +01003193 stats_dump_html_px_end(cs, px);
Christopher Fauletef779222018-10-31 08:47:01 +01003194 if (!stats_putchk(rep, htx, &trash))
3195 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01003196 }
3197
3198 appctx->ctx.stats.px_st = STAT_PX_ST_FIN;
3199 /* fall through */
3200
3201 case STAT_PX_ST_FIN:
3202 return 1;
3203
3204 default:
3205 /* unknown state, we should put an abort() here ! */
3206 return 1;
3207 }
Christopher Fauletef779222018-10-31 08:47:01 +01003208
3209 full:
Christopher Fauleta0bdec32022-04-04 07:51:21 +02003210 cs_rx_room_blk(cs);
Christopher Fauletef779222018-10-31 08:47:01 +01003211 return 0;
William Lallemand74c24fb2016-11-21 17:18:36 +01003212}
3213
3214/* Dumps the HTTP stats head block to the trash for and uses the per-uri
3215 * parameters <uri>. The caller is responsible for clearing the trash if needed.
3216 */
Willy Tarreau676c29e2019-10-09 10:50:01 +02003217static void stats_dump_html_head(struct appctx *appctx, struct uri_auth *uri)
William Lallemand74c24fb2016-11-21 17:18:36 +01003218{
3219 /* WARNING! This must fit in the first buffer !!! */
3220 chunk_appendf(&trash,
3221 "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"\n"
3222 "\"http://www.w3.org/TR/html4/loose.dtd\">\n"
3223 "<html><head><title>Statistics Report for " PRODUCT_NAME "%s%s</title>\n"
Thierry Fournierb6b1cde2021-09-24 00:03:07 +02003224 "<link rel=\"icon\" href=\"data:,\">\n"
William Lallemand74c24fb2016-11-21 17:18:36 +01003225 "<meta http-equiv=\"content-type\" content=\"text/html; charset=iso-8859-1\">\n"
3226 "<style type=\"text/css\"><!--\n"
3227 "body {"
3228 " font-family: arial, helvetica, sans-serif;"
3229 " font-size: 12px;"
3230 " font-weight: normal;"
3231 " color: black;"
3232 " background: white;"
3233 "}\n"
3234 "th,td {"
3235 " font-size: 10px;"
3236 "}\n"
3237 "h1 {"
3238 " font-size: x-large;"
3239 " margin-bottom: 0.5em;"
3240 "}\n"
3241 "h2 {"
3242 " font-family: helvetica, arial;"
3243 " font-size: x-large;"
3244 " font-weight: bold;"
3245 " font-style: italic;"
3246 " color: #6020a0;"
3247 " margin-top: 0em;"
3248 " margin-bottom: 0em;"
3249 "}\n"
3250 "h3 {"
3251 " font-family: helvetica, arial;"
3252 " font-size: 16px;"
3253 " font-weight: bold;"
3254 " color: #b00040;"
3255 " background: #e8e8d0;"
3256 " margin-top: 0em;"
3257 " margin-bottom: 0em;"
3258 "}\n"
3259 "li {"
3260 " margin-top: 0.25em;"
3261 " margin-right: 2em;"
3262 "}\n"
3263 ".hr {margin-top: 0.25em;"
3264 " border-color: black;"
3265 " border-bottom-style: solid;"
3266 "}\n"
3267 ".titre {background: #20D0D0;color: #000000; font-weight: bold; text-align: center;}\n"
3268 ".total {background: #20D0D0;color: #ffff80;}\n"
3269 ".frontend {background: #e8e8d0;}\n"
3270 ".socket {background: #d0d0d0;}\n"
3271 ".backend {background: #e8e8d0;}\n"
3272 ".active_down {background: #ff9090;}\n"
3273 ".active_going_up {background: #ffd020;}\n"
3274 ".active_going_down {background: #ffffa0;}\n"
3275 ".active_up {background: #c0ffc0;}\n"
3276 ".active_nolb {background: #20a0ff;}\n"
3277 ".active_draining {background: #20a0FF;}\n"
3278 ".active_no_check {background: #e0e0e0;}\n"
3279 ".backup_down {background: #ff9090;}\n"
3280 ".backup_going_up {background: #ff80ff;}\n"
3281 ".backup_going_down {background: #c060ff;}\n"
3282 ".backup_up {background: #b0d0ff;}\n"
3283 ".backup_nolb {background: #90b0e0;}\n"
3284 ".backup_draining {background: #cc9900;}\n"
3285 ".backup_no_check {background: #e0e0e0;}\n"
3286 ".maintain {background: #c07820;}\n"
3287 ".rls {letter-spacing: 0.2em; margin-right: 1px;}\n" /* right letter spacing (used for grouping digits) */
3288 "\n"
3289 "a.px:link {color: #ffff40; text-decoration: none;}"
3290 "a.px:visited {color: #ffff40; text-decoration: none;}"
3291 "a.px:hover {color: #ffffff; text-decoration: none;}"
3292 "a.lfsb:link {color: #000000; text-decoration: none;}"
3293 "a.lfsb:visited {color: #000000; text-decoration: none;}"
3294 "a.lfsb:hover {color: #505050; text-decoration: none;}"
3295 "\n"
3296 "table.tbl { border-collapse: collapse; border-style: none;}\n"
3297 "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"
3298 "table.tbl td.ac { text-align: center;}\n"
3299 "table.tbl th { border-width: 1px; border-style: solid solid solid solid; border-color: gray;}\n"
3300 "table.tbl th.pxname { background: #b00040; color: #ffff40; font-weight: bold; border-style: solid solid none solid; padding: 2px 3px; white-space: nowrap;}\n"
3301 "table.tbl th.empty { border-style: none; empty-cells: hide; background: white;}\n"
3302 "table.tbl th.desc { background: white; border-style: solid solid none solid; text-align: left; padding: 2px 3px;}\n"
3303 "\n"
3304 "table.lgd { border-collapse: collapse; border-width: 1px; border-style: none none none solid; border-color: black;}\n"
3305 "table.lgd td { border-width: 1px; border-style: solid solid solid solid; border-color: gray; padding: 2px;}\n"
3306 "table.lgd td.noborder { border-style: none; padding: 2px; white-space: nowrap;}\n"
3307 "table.det { border-collapse: collapse; border-style: none; }\n"
3308 "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"
3309 "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"
3310 "u {text-decoration:none; border-bottom: 1px dotted black;}\n"
Thierry Fournierb6b1cde2021-09-24 00:03:07 +02003311 "div.tips {\n"
3312 " display:block;\n"
3313 " visibility:hidden;\n"
3314 " z-index:2147483647;\n"
3315 " position:absolute;\n"
3316 " padding:2px 4px 3px;\n"
3317 " background:#f0f060; color:#000000;\n"
3318 " border:1px solid #7040c0;\n"
3319 " white-space:nowrap;\n"
3320 " font-style:normal;font-size:11px;font-weight:normal;\n"
3321 " -moz-border-radius:3px;-webkit-border-radius:3px;border-radius:3px;\n"
3322 " -moz-box-shadow:gray 2px 2px 3px;-webkit-box-shadow:gray 2px 2px 3px;box-shadow:gray 2px 2px 3px;\n"
3323 "}\n"
3324 "u:hover div.tips {visibility:visible;}\n"
Thierry Fourniere9ed63e2021-09-23 23:59:49 +02003325 "@media (prefers-color-scheme: dark) {\n"
3326 " body { font-family: arial, helvetica, sans-serif; font-size: 12px; font-weight: normal; color: #e8e6e3; background: #131516;}\n"
3327 " h1 { color: #a265e0!important; }\n"
3328 " h2 { color: #a265e0; }\n"
3329 " h3 { color: #ff5190; background-color: #3e3e1f; }\n"
3330 " a { color: #3391ff; }\n"
3331 " input { background-color: #2f3437; }\n"
3332 " .hr { border-color: #8c8273; }\n"
3333 " .titre { background-color: #1aa6a6; color: #e8e6e3; }\n"
3334 " .frontend {background: #2f3437;}\n"
Marno Krahmera690b732022-03-08 13:45:09 +01003335 " .socket {background: #2a2d2f;}\n"
Thierry Fourniere9ed63e2021-09-23 23:59:49 +02003336 " .backend {background: #2f3437;}\n"
3337 " .active_down {background: #760000;}\n"
3338 " .active_going_up {background: #b99200;}\n"
3339 " .active_going_down {background: #6c6c00;}\n"
3340 " .active_up {background: #165900;}\n"
3341 " .active_nolb {background: #006ab9;}\n"
3342 " .active_draining {background: #006ab9;}\n"
3343 " .active_no_check {background: #2a2d2f;}\n"
3344 " .backup_down {background: #760000;}\n"
3345 " .backup_going_up {background: #7f007f;}\n"
3346 " .backup_going_down {background: #580092;}\n"
3347 " .backup_up {background: #2e3234;}\n"
3348 " .backup_nolb {background: #1e3c6a;}\n"
3349 " .backup_draining {background: #a37a00;}\n"
3350 " .backup_no_check {background: #2a2d2f;}\n"
3351 " .maintain {background: #9a601a;}\n"
3352 " a.px:link {color: #d8d83b; text-decoration: none;}\n"
3353 " a.px:visited {color: #d8d83b; text-decoration: none;}\n"
3354 " a.px:hover {color: #ffffff; text-decoration: none;}\n"
3355 " a.lfsb:link {color: #e8e6e3; text-decoration: none;}\n"
3356 " a.lfsb:visited {color: #e8e6e3; text-decoration: none;}\n"
3357 " a.lfsb:hover {color: #b5afa6; text-decoration: none;}\n"
3358 " table.tbl th.empty { background-color: #181a1b; }\n"
Willy Tarreau00147f72022-04-11 07:59:27 +02003359 " table.tbl th.desc { background: #181a1b; }\n"
Thierry Fourniere9ed63e2021-09-23 23:59:49 +02003360 " table.tbl th.pxname { background-color: #8d0033; color: #ffff46; }\n"
3361 " table.tbl th { border-color: #808080; }\n"
3362 " table.tbl td { border-color: #808080; }\n"
3363 " u {text-decoration:none; border-bottom: 1px dotted #e8e6e3;}\n"
3364 " div.tips {\n"
3365 " background:#8e8e0d;\n"
3366 " color:#e8e6e3;\n"
3367 " border-color: #4e2c86;\n"
3368 " -moz-box-shadow: #60686c 2px 2px 3px;\n"
3369 " -webkit-box-shadow: #60686c 2px 2px 3px;\n"
3370 " box-shadow: #60686c 2px 2px 3px;\n"
3371 " }\n"
3372 "}\n"
William Lallemand74c24fb2016-11-21 17:18:36 +01003373 "-->\n"
3374 "</style></head>\n",
Willy Tarreau676c29e2019-10-09 10:50:01 +02003375 (appctx->ctx.stats.flags & STAT_SHNODE) ? " on " : "",
Willy Tarreau027d2062020-01-23 11:47:13 +01003376 (appctx->ctx.stats.flags & STAT_SHNODE) ? (uri && uri->node ? uri->node : global.node) : ""
William Lallemand74c24fb2016-11-21 17:18:36 +01003377 );
3378}
3379
3380/* Dumps the HTML stats information block to the trash for and uses the state from
Christopher Faulet908628c2022-03-25 16:43:49 +01003381 * conn-stream <cs> and per-uri parameters <uri>. The caller is responsible
William Lallemand74c24fb2016-11-21 17:18:36 +01003382 * for clearing the trash if needed.
3383 */
Christopher Faulet908628c2022-03-25 16:43:49 +01003384static void stats_dump_html_info(struct conn_stream *cs, struct uri_auth *uri)
William Lallemand74c24fb2016-11-21 17:18:36 +01003385{
Christopher Faulet908628c2022-03-25 16:43:49 +01003386 struct appctx *appctx = __cs_appctx(cs);
William Lallemand74c24fb2016-11-21 17:18:36 +01003387 unsigned int up = (now.tv_sec - start_date.tv_sec);
3388 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
Christopher Faulet908628c2022-03-25 16:43:49 +01003389 const char *scope_ptr = stats_scope_ptr(appctx, cs);
Willy Tarreau1713c032019-05-23 12:23:55 +02003390 unsigned long long bps = (unsigned long long)read_freq_ctr(&global.out_32bps) * 32;
3391
3392 /* Turn the bytes per second to bits per second and take care of the
3393 * usual ethernet overhead in order to help figure how far we are from
3394 * interface saturation since it's the only case which usually matters.
3395 * For this we count the total size of an Ethernet frame on the wire
3396 * including preamble and IFG (1538) for the largest TCP segment it
3397 * transports (1448 with TCP timestamps). This is not valid for smaller
3398 * packets (under-estimated), but it gives a reasonably accurate
3399 * estimation of how far we are from uplink saturation.
3400 */
3401 bps = bps * 8 * 1538 / 1448;
William Lallemand74c24fb2016-11-21 17:18:36 +01003402
3403 /* WARNING! this has to fit the first packet too.
3404 * We are around 3.5 kB, add adding entries will
3405 * become tricky if we want to support 4kB buffers !
3406 */
3407 chunk_appendf(&trash,
3408 "<body><h1><a href=\"" PRODUCT_URL "\" style=\"text-decoration: none;\">"
3409 PRODUCT_NAME "%s</a></h1>\n"
3410 "<h2>Statistics Report for pid %d%s%s%s%s</h2>\n"
3411 "<hr width=\"100%%\" class=\"hr\">\n"
3412 "<h3>&gt; General process information</h3>\n"
3413 "<table border=0><tr><td align=\"left\" nowrap width=\"1%%\">\n"
Yves Lafon95317282018-02-26 11:10:37 +01003414 "<p><b>pid = </b> %d (process #%d, nbproc = %d, nbthread = %d)<br>\n"
William Lallemand74c24fb2016-11-21 17:18:36 +01003415 "<b>uptime = </b> %dd %dh%02dm%02ds<br>\n"
3416 "<b>system limits:</b> memmax = %s%s; ulimit-n = %d<br>\n"
3417 "<b>maxsock = </b> %d; <b>maxconn = </b> %d; <b>maxpipes = </b> %d<br>\n"
Willy Tarreau1713c032019-05-23 12:23:55 +02003418 "current conns = %d; current pipes = %d/%d; conn rate = %d/sec; bit rate = %.3f %cbps<br>\n"
William Lallemand74c24fb2016-11-21 17:18:36 +01003419 "Running tasks: %d/%d; idle = %d %%<br>\n"
3420 "</td><td align=\"center\" nowrap>\n"
3421 "<table class=\"lgd\"><tr>\n"
3422 "<td class=\"active_up\">&nbsp;</td><td class=\"noborder\">active UP </td>"
3423 "<td class=\"backup_up\">&nbsp;</td><td class=\"noborder\">backup UP </td>"
3424 "</tr><tr>\n"
3425 "<td class=\"active_going_down\"></td><td class=\"noborder\">active UP, going down </td>"
3426 "<td class=\"backup_going_down\"></td><td class=\"noborder\">backup UP, going down </td>"
3427 "</tr><tr>\n"
3428 "<td class=\"active_going_up\"></td><td class=\"noborder\">active DOWN, going up </td>"
3429 "<td class=\"backup_going_up\"></td><td class=\"noborder\">backup DOWN, going up </td>"
3430 "</tr><tr>\n"
3431 "<td class=\"active_down\"></td><td class=\"noborder\">active or backup DOWN &nbsp;</td>"
3432 "<td class=\"active_no_check\"></td><td class=\"noborder\">not checked </td>"
3433 "</tr><tr>\n"
3434 "<td class=\"maintain\"></td><td class=\"noborder\" colspan=\"3\">active or backup DOWN for maintenance (MAINT) &nbsp;</td>"
3435 "</tr><tr>\n"
3436 "<td class=\"active_draining\"></td><td class=\"noborder\" colspan=\"3\">active or backup SOFT STOPPED for maintenance &nbsp;</td>"
3437 "</tr></table>\n"
3438 "Note: \"NOLB\"/\"DRAIN\" = UP with load-balancing disabled."
3439 "</td>"
3440 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
3441 "<b>Display option:</b><ul style=\"margin-top: 0.25em;\">"
3442 "",
Willy Tarreau676c29e2019-10-09 10:50:01 +02003443 (appctx->ctx.stats.flags & STAT_HIDEVER) ? "" : (stats_version_string),
3444 pid, (appctx->ctx.stats.flags & STAT_SHNODE) ? " on " : "",
3445 (appctx->ctx.stats.flags & STAT_SHNODE) ? (uri->node ? uri->node : global.node) : "",
3446 (appctx->ctx.stats.flags & STAT_SHDESC) ? ": " : "",
3447 (appctx->ctx.stats.flags & STAT_SHDESC) ? (uri->desc ? uri->desc : global.desc) : "",
Willy Tarreaue8422bf2021-06-15 09:08:18 +02003448 pid, 1, 1, global.nbthread,
William Lallemand74c24fb2016-11-21 17:18:36 +01003449 up / 86400, (up % 86400) / 3600,
3450 (up % 3600) / 60, (up % 60),
3451 global.rlimit_memmax ? ultoa(global.rlimit_memmax) : "unlimited",
3452 global.rlimit_memmax ? " MB" : "",
3453 global.rlimit_nofile,
3454 global.maxsock, global.maxconn, global.maxpipes,
3455 actconn, pipes_used, pipes_used+pipes_free, read_freq_ctr(&global.conn_per_sec),
Willy Tarreau1713c032019-05-23 12:23:55 +02003456 bps >= 1000000000UL ? (bps / 1000000000.0) : bps >= 1000000UL ? (bps / 1000000.0) : (bps / 1000.0),
3457 bps >= 1000000000UL ? 'G' : bps >= 1000000UL ? 'M' : 'k',
Willy Tarreauf9d5e102021-10-08 10:43:59 +02003458 total_run_queues(), total_allocated_tasks(), clock_report_idle()
William Lallemand74c24fb2016-11-21 17:18:36 +01003459 );
3460
3461 /* scope_txt = search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
Christopher Fauleted7a0662019-01-14 11:07:34 +01003462 memcpy(scope_txt, scope_ptr, appctx->ctx.stats.scope_len);
William Lallemand74c24fb2016-11-21 17:18:36 +01003463 scope_txt[appctx->ctx.stats.scope_len] = '\0';
3464
3465 chunk_appendf(&trash,
3466 "<li><form method=\"GET\">Scope : <input value=\"%s\" name=\"" STAT_SCOPE_INPUT_NAME "\" size=\"8\" maxlength=\"%d\" tabindex=\"1\"/></form>\n",
3467 (appctx->ctx.stats.scope_len > 0) ? scope_txt : "",
3468 STAT_SCOPE_TXT_MAXLEN);
3469
3470 /* scope_txt = search pattern + search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
3471 scope_txt[0] = 0;
3472 if (appctx->ctx.stats.scope_len) {
3473 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Christopher Fauleted7a0662019-01-14 11:07:34 +01003474 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), scope_ptr, appctx->ctx.stats.scope_len);
William Lallemand74c24fb2016-11-21 17:18:36 +01003475 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
3476 }
3477
3478 if (appctx->ctx.stats.flags & STAT_HIDE_DOWN)
3479 chunk_appendf(&trash,
3480 "<li><a href=\"%s%s%s%s\">Show all servers</a><br>\n",
3481 uri->uri_prefix,
3482 "",
3483 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
3484 scope_txt);
3485 else
3486 chunk_appendf(&trash,
3487 "<li><a href=\"%s%s%s%s\">Hide 'DOWN' servers</a><br>\n",
3488 uri->uri_prefix,
3489 ";up",
3490 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
3491 scope_txt);
3492
3493 if (uri->refresh > 0) {
3494 if (appctx->ctx.stats.flags & STAT_NO_REFRESH)
3495 chunk_appendf(&trash,
3496 "<li><a href=\"%s%s%s%s\">Enable refresh</a><br>\n",
3497 uri->uri_prefix,
3498 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3499 "",
3500 scope_txt);
3501 else
3502 chunk_appendf(&trash,
3503 "<li><a href=\"%s%s%s%s\">Disable refresh</a><br>\n",
3504 uri->uri_prefix,
3505 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3506 ";norefresh",
3507 scope_txt);
3508 }
3509
3510 chunk_appendf(&trash,
3511 "<li><a href=\"%s%s%s%s\">Refresh now</a><br>\n",
3512 uri->uri_prefix,
3513 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3514 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
3515 scope_txt);
3516
3517 chunk_appendf(&trash,
3518 "<li><a href=\"%s;csv%s%s\">CSV export</a><br>\n",
3519 uri->uri_prefix,
3520 (uri->refresh > 0) ? ";norefresh" : "",
3521 scope_txt);
3522
3523 chunk_appendf(&trash,
Christopher Faulet6338a082019-09-09 15:50:54 +02003524 "<li><a href=\"%s;json%s%s\">JSON export</a> (<a href=\"%s;json-schema\">schema</a>)<br>\n",
3525 uri->uri_prefix,
3526 (uri->refresh > 0) ? ";norefresh" : "",
3527 scope_txt, uri->uri_prefix);
3528
3529 chunk_appendf(&trash,
William Lallemand74c24fb2016-11-21 17:18:36 +01003530 "</ul></td>"
3531 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
3532 "<b>External resources:</b><ul style=\"margin-top: 0.25em;\">\n"
3533 "<li><a href=\"" PRODUCT_URL "\">Primary site</a><br>\n"
3534 "<li><a href=\"" PRODUCT_URL_UPD "\">Updates (v" PRODUCT_BRANCH ")</a><br>\n"
3535 "<li><a href=\"" PRODUCT_URL_DOC "\">Online manual</a><br>\n"
3536 "</ul>"
3537 "</td>"
3538 "</tr></table>\n"
3539 ""
3540 );
3541
3542 if (appctx->ctx.stats.st_code) {
3543 switch (appctx->ctx.stats.st_code) {
3544 case STAT_STATUS_DONE:
3545 chunk_appendf(&trash,
3546 "<p><div class=active_up>"
3547 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
3548 "Action processed successfully."
3549 "</div>\n", uri->uri_prefix,
3550 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3551 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
3552 scope_txt);
3553 break;
3554 case STAT_STATUS_NONE:
3555 chunk_appendf(&trash,
3556 "<p><div class=active_going_down>"
3557 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
3558 "Nothing has changed."
3559 "</div>\n", uri->uri_prefix,
3560 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3561 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
3562 scope_txt);
3563 break;
3564 case STAT_STATUS_PART:
3565 chunk_appendf(&trash,
3566 "<p><div class=active_going_down>"
3567 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
3568 "Action partially processed.<br>"
3569 "Some server names are probably unknown or ambiguous (duplicated names in the backend)."
3570 "</div>\n", uri->uri_prefix,
3571 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3572 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
3573 scope_txt);
3574 break;
3575 case STAT_STATUS_ERRP:
3576 chunk_appendf(&trash,
3577 "<p><div class=active_down>"
3578 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
3579 "Action not processed because of invalid parameters."
3580 "<ul>"
3581 "<li>The action is maybe unknown.</li>"
Christopher Faulet2f9a41d2019-02-27 15:30:57 +01003582 "<li>Invalid key parameter (empty or too long).</li>"
William Lallemand74c24fb2016-11-21 17:18:36 +01003583 "<li>The backend name is probably unknown or ambiguous (duplicated names).</li>"
3584 "<li>Some server names are probably unknown or ambiguous (duplicated names in the backend).</li>"
3585 "</ul>"
3586 "</div>\n", uri->uri_prefix,
3587 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3588 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
3589 scope_txt);
3590 break;
3591 case STAT_STATUS_EXCD:
3592 chunk_appendf(&trash,
3593 "<p><div class=active_down>"
3594 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
3595 "<b>Action not processed : the buffer couldn't store all the data.<br>"
3596 "You should retry with less servers at a time.</b>"
3597 "</div>\n", uri->uri_prefix,
3598 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3599 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
3600 scope_txt);
3601 break;
3602 case STAT_STATUS_DENY:
3603 chunk_appendf(&trash,
3604 "<p><div class=active_down>"
3605 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
3606 "<b>Action denied.</b>"
3607 "</div>\n", uri->uri_prefix,
3608 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3609 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
3610 scope_txt);
3611 break;
Christopher Faulet3c2ecf72019-02-27 16:41:27 +01003612 case STAT_STATUS_IVAL:
3613 chunk_appendf(&trash,
3614 "<p><div class=active_down>"
3615 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
3616 "<b>Invalid requests (unsupported method or chunked encoded request).</b>"
3617 "</div>\n", uri->uri_prefix,
3618 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3619 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
3620 scope_txt);
3621 break;
William Lallemand74c24fb2016-11-21 17:18:36 +01003622 default:
3623 chunk_appendf(&trash,
3624 "<p><div class=active_no_check>"
3625 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
3626 "Unexpected result."
3627 "</div>\n", uri->uri_prefix,
3628 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3629 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
3630 scope_txt);
3631 }
3632 chunk_appendf(&trash, "<p>\n");
3633 }
3634}
3635
3636/* Dumps the HTML stats trailer block to the trash. The caller is responsible
3637 * for clearing the trash if needed.
3638 */
3639static void stats_dump_html_end()
3640{
3641 chunk_appendf(&trash, "</body></html>\n");
3642}
3643
Simon Horman05ee2132017-01-04 09:37:25 +01003644/* Dumps the stats JSON header to the trash buffer which. The caller is responsible
3645 * for clearing it if needed.
3646 */
3647static void stats_dump_json_header()
3648{
3649 chunk_strcat(&trash, "[");
3650}
3651
3652
3653/* Dumps the JSON stats trailer block to the trash. The caller is responsible
3654 * for clearing the trash if needed.
3655 */
3656static void stats_dump_json_end()
3657{
3658 chunk_strcat(&trash, "]");
3659}
3660
Amaury Denoyelle98b81cb2020-10-02 18:32:04 +02003661/* Uses <appctx.ctx.stats.obj1> as a pointer to the current proxy and <obj2> as
3662 * a pointer to the current server/listener.
3663 */
Christopher Faulet908628c2022-03-25 16:43:49 +01003664static int stats_dump_proxies(struct conn_stream *cs,
Amaury Denoyelle98b81cb2020-10-02 18:32:04 +02003665 struct htx *htx,
3666 struct uri_auth *uri)
3667{
Christopher Faulet908628c2022-03-25 16:43:49 +01003668 struct appctx *appctx = __cs_appctx(cs);
3669 struct channel *rep = cs_ic(cs);
Amaury Denoyelle98b81cb2020-10-02 18:32:04 +02003670 struct proxy *px;
3671
3672 /* dump proxies */
3673 while (appctx->ctx.stats.obj1) {
3674 if (htx) {
3675 if (htx_almost_full(htx))
3676 goto full;
3677 }
3678 else {
3679 if (buffer_almost_full(&rep->buf))
3680 goto full;
3681 }
3682
3683 px = appctx->ctx.stats.obj1;
Marno Krahmer07954fb2021-06-24 16:51:08 +02003684 /* Skip the global frontend proxies and non-networked ones.
William Lallemand85a16b22021-08-03 13:18:11 +02003685 * Also skip proxies that were disabled in the configuration
Marno Krahmer07954fb2021-06-24 16:51:08 +02003686 * This change allows retrieving stats from "old" proxies after a reload.
3687 */
Christopher Fauletdfd10ab2021-10-06 14:24:19 +02003688 if (!(px->flags & PR_FL_DISABLED) && px->uuid > 0 &&
William Lallemande7f74622021-07-28 17:45:18 +02003689 (px->cap & (PR_CAP_FE | PR_CAP_BE)) && !(px->cap & PR_CAP_INT)) {
Christopher Faulet908628c2022-03-25 16:43:49 +01003690 if (stats_dump_proxy_to_buffer(cs, htx, px, uri) == 0)
Amaury Denoyelle98b81cb2020-10-02 18:32:04 +02003691 return 0;
3692 }
3693
3694 appctx->ctx.stats.obj1 = px->next;
3695 appctx->ctx.stats.px_st = STAT_PX_ST_INIT;
3696 }
3697
3698 return 1;
3699
3700 full:
Christopher Fauleta0bdec32022-04-04 07:51:21 +02003701 cs_rx_room_blk(cs);
Amaury Denoyelle98b81cb2020-10-02 18:32:04 +02003702 return 0;
3703}
3704
Christopher Faulet908628c2022-03-25 16:43:49 +01003705/* This function dumps statistics onto the conn-stream's read buffer in
William Lallemand74c24fb2016-11-21 17:18:36 +01003706 * either CSV or HTML format. <uri> contains some HTML-specific parameters that
3707 * are ignored for CSV format (hence <uri> may be NULL there). It returns 0 if
3708 * it had to stop writing data and an I/O is needed, 1 if the dump is finished
3709 * and the stream must be closed, or -1 in case of any error. This function is
3710 * used by both the CLI and the HTTP handlers.
3711 */
Christopher Faulet908628c2022-03-25 16:43:49 +01003712static int stats_dump_stat_to_buffer(struct conn_stream *cs, struct htx *htx,
Christopher Fauletef779222018-10-31 08:47:01 +01003713 struct uri_auth *uri)
William Lallemand74c24fb2016-11-21 17:18:36 +01003714{
Christopher Faulet908628c2022-03-25 16:43:49 +01003715 struct appctx *appctx = __cs_appctx(cs);
3716 struct channel *rep = cs_ic(cs);
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02003717 enum stats_domain domain = appctx->ctx.stats.domain;
William Lallemand74c24fb2016-11-21 17:18:36 +01003718
3719 chunk_reset(&trash);
3720
3721 switch (appctx->st2) {
3722 case STAT_ST_INIT:
3723 appctx->st2 = STAT_ST_HEAD; /* let's start producing data */
3724 /* fall through */
3725
3726 case STAT_ST_HEAD:
3727 if (appctx->ctx.stats.flags & STAT_FMT_HTML)
Willy Tarreau676c29e2019-10-09 10:50:01 +02003728 stats_dump_html_head(appctx, uri);
Christopher Faulet6338a082019-09-09 15:50:54 +02003729 else if (appctx->ctx.stats.flags & STAT_JSON_SCHM)
3730 stats_dump_json_schema(&trash);
Simon Horman05ee2132017-01-04 09:37:25 +01003731 else if (appctx->ctx.stats.flags & STAT_FMT_JSON)
Willy Tarreau9d7fb632017-04-11 07:53:04 +02003732 stats_dump_json_header();
William Lallemand74c24fb2016-11-21 17:18:36 +01003733 else if (!(appctx->ctx.stats.flags & STAT_FMT_TYPED))
Amaury Denoyelle50660a82020-10-05 11:49:39 +02003734 stats_dump_csv_header(appctx->ctx.stats.domain);
William Lallemand74c24fb2016-11-21 17:18:36 +01003735
Christopher Fauletef779222018-10-31 08:47:01 +01003736 if (!stats_putchk(rep, htx, &trash))
3737 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01003738
Christopher Faulet6338a082019-09-09 15:50:54 +02003739 if (appctx->ctx.stats.flags & STAT_JSON_SCHM) {
3740 appctx->st2 = STAT_ST_FIN;
3741 return 1;
3742 }
William Lallemand74c24fb2016-11-21 17:18:36 +01003743 appctx->st2 = STAT_ST_INFO;
3744 /* fall through */
3745
3746 case STAT_ST_INFO:
3747 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Christopher Faulet908628c2022-03-25 16:43:49 +01003748 stats_dump_html_info(cs, uri);
Christopher Fauletef779222018-10-31 08:47:01 +01003749 if (!stats_putchk(rep, htx, &trash))
3750 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01003751 }
3752
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02003753 if (domain == STATS_DOMAIN_PROXY)
3754 appctx->ctx.stats.obj1 = proxies_list;
3755
William Lallemand74c24fb2016-11-21 17:18:36 +01003756 appctx->ctx.stats.px_st = STAT_PX_ST_INIT;
3757 appctx->st2 = STAT_ST_LIST;
3758 /* fall through */
3759
3760 case STAT_ST_LIST:
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02003761 switch (domain) {
Emeric Brunf8642ee2021-10-29 17:59:18 +02003762 case STATS_DOMAIN_RESOLVERS:
Christopher Faulet908628c2022-03-25 16:43:49 +01003763 if (!stats_dump_resolvers(cs, stat_l[domain],
Emeric Brund3b44952021-01-06 16:01:02 +01003764 stat_count[domain],
3765 &stats_module_list[domain])) {
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02003766 return 0;
3767 }
3768 break;
3769
3770 case STATS_DOMAIN_PROXY:
3771 default:
3772 /* dump proxies */
Christopher Faulet908628c2022-03-25 16:43:49 +01003773 if (!stats_dump_proxies(cs, htx, uri))
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02003774 return 0;
3775 break;
3776 }
William Lallemand74c24fb2016-11-21 17:18:36 +01003777
3778 appctx->st2 = STAT_ST_END;
3779 /* fall through */
3780
3781 case STAT_ST_END:
Simon Horman05ee2132017-01-04 09:37:25 +01003782 if (appctx->ctx.stats.flags & (STAT_FMT_HTML|STAT_FMT_JSON)) {
3783 if (appctx->ctx.stats.flags & STAT_FMT_HTML)
3784 stats_dump_html_end();
3785 else
3786 stats_dump_json_end();
Christopher Fauletef779222018-10-31 08:47:01 +01003787 if (!stats_putchk(rep, htx, &trash))
3788 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01003789 }
3790
3791 appctx->st2 = STAT_ST_FIN;
3792 /* fall through */
3793
3794 case STAT_ST_FIN:
3795 return 1;
3796
3797 default:
3798 /* unknown state ! */
3799 appctx->st2 = STAT_ST_FIN;
3800 return -1;
3801 }
Christopher Fauletef779222018-10-31 08:47:01 +01003802
3803 full:
Christopher Fauleta0bdec32022-04-04 07:51:21 +02003804 cs_rx_room_blk(cs);
Christopher Fauletef779222018-10-31 08:47:01 +01003805 return 0;
3806
William Lallemand74c24fb2016-11-21 17:18:36 +01003807}
3808
3809/* We reached the stats page through a POST request. The appctx is
3810 * expected to have already been allocated by the caller.
3811 * Parse the posted data and enable/disable servers if necessary.
3812 * Returns 1 if request was parsed or zero if it needs more data.
3813 */
Christopher Faulet908628c2022-03-25 16:43:49 +01003814static int stats_process_http_post(struct conn_stream *cs)
William Lallemand74c24fb2016-11-21 17:18:36 +01003815{
Christopher Faulet908628c2022-03-25 16:43:49 +01003816 struct stream *s = __cs_strm(cs);
3817 struct appctx *appctx = __cs_appctx(cs);
William Lallemand74c24fb2016-11-21 17:18:36 +01003818
3819 struct proxy *px = NULL;
3820 struct server *sv = NULL;
3821
3822 char key[LINESIZE];
3823 int action = ST_ADM_ACTION_NONE;
3824 int reprocess = 0;
3825
3826 int total_servers = 0;
3827 int altered_servers = 0;
3828
3829 char *first_param, *cur_param, *next_param, *end_params;
3830 char *st_cur_param = NULL;
3831 char *st_next_param = NULL;
3832
Christopher Fauleta3618372018-12-13 21:59:56 +01003833 struct buffer *temp = get_trash_chunk();
William Lallemand74c24fb2016-11-21 17:18:36 +01003834
Christopher Fauletb7f88902019-07-15 21:56:43 +02003835 struct htx *htx = htxbuf(&s->req.buf);
3836 struct htx_blk *blk;
Christopher Fauleta3618372018-12-13 21:59:56 +01003837
Christopher Fauletb7f88902019-07-15 21:56:43 +02003838 /* we need more data */
3839 if (s->txn->req.msg_state < HTTP_MSG_DONE) {
3840 /* check if we can receive more */
3841 if (htx_free_data_space(htx) <= global.tune.maxrewrite) {
3842 appctx->ctx.stats.st_code = STAT_STATUS_EXCD;
3843 goto out;
Christopher Fauleta3618372018-12-13 21:59:56 +01003844 }
Christopher Fauletb7f88902019-07-15 21:56:43 +02003845 goto wait;
3846 }
Christopher Fauleta3618372018-12-13 21:59:56 +01003847
Christopher Fauletb7f88902019-07-15 21:56:43 +02003848 /* The request was fully received. Copy data */
3849 blk = htx_get_head_blk(htx);
3850 while (blk) {
3851 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Fauleta3618372018-12-13 21:59:56 +01003852
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01003853 if (type == HTX_BLK_TLR || type == HTX_BLK_EOT)
Christopher Fauletb7f88902019-07-15 21:56:43 +02003854 break;
3855 if (type == HTX_BLK_DATA) {
3856 struct ist v = htx_get_blk_value(htx, blk);
Christopher Fauleta3618372018-12-13 21:59:56 +01003857
Christopher Fauletb7f88902019-07-15 21:56:43 +02003858 if (!chunk_memcat(temp, v.ptr, v.len)) {
Christopher Faulet2f9a41d2019-02-27 15:30:57 +01003859 appctx->ctx.stats.st_code = STAT_STATUS_EXCD;
3860 goto out;
3861 }
Christopher Fauleta3618372018-12-13 21:59:56 +01003862 }
Christopher Fauletb7f88902019-07-15 21:56:43 +02003863 blk = htx_get_next_blk(htx, blk);
William Lallemand74c24fb2016-11-21 17:18:36 +01003864 }
3865
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003866 first_param = temp->area;
Christopher Fauleta3618372018-12-13 21:59:56 +01003867 end_params = temp->area + temp->data;
William Lallemand74c24fb2016-11-21 17:18:36 +01003868 cur_param = next_param = end_params;
3869 *end_params = '\0';
3870
3871 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
3872
3873 /*
3874 * Parse the parameters in reverse order to only store the last value.
3875 * From the html form, the backend and the action are at the end.
3876 */
3877 while (cur_param > first_param) {
3878 char *value;
3879 int poffset, plen;
3880
3881 cur_param--;
3882
3883 if ((*cur_param == '&') || (cur_param == first_param)) {
3884 reprocess_servers:
3885 /* Parse the key */
3886 poffset = (cur_param != first_param ? 1 : 0);
3887 plen = next_param - cur_param + (cur_param == first_param ? 1 : 0);
3888 if ((plen > 0) && (plen <= sizeof(key))) {
3889 strncpy(key, cur_param + poffset, plen);
3890 key[plen - 1] = '\0';
3891 } else {
Christopher Faulet2f9a41d2019-02-27 15:30:57 +01003892 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
William Lallemand74c24fb2016-11-21 17:18:36 +01003893 goto out;
3894 }
3895
3896 /* Parse the value */
3897 value = key;
3898 while (*value != '\0' && *value != '=') {
3899 value++;
3900 }
3901 if (*value == '=') {
3902 /* Ok, a value is found, we can mark the end of the key */
3903 *value++ = '\0';
3904 }
Willy Tarreau62ba9ba2020-04-23 17:54:47 +02003905 if (url_decode(key, 1) < 0 || url_decode(value, 1) < 0)
William Lallemand74c24fb2016-11-21 17:18:36 +01003906 break;
3907
3908 /* Now we can check the key to see what to do */
3909 if (!px && (strcmp(key, "b") == 0)) {
3910 if ((px = proxy_be_by_name(value)) == NULL) {
3911 /* the backend name is unknown or ambiguous (duplicate names) */
3912 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
3913 goto out;
3914 }
3915 }
3916 else if (!action && (strcmp(key, "action") == 0)) {
3917 if (strcmp(value, "ready") == 0) {
3918 action = ST_ADM_ACTION_READY;
3919 }
3920 else if (strcmp(value, "drain") == 0) {
3921 action = ST_ADM_ACTION_DRAIN;
3922 }
3923 else if (strcmp(value, "maint") == 0) {
3924 action = ST_ADM_ACTION_MAINT;
3925 }
3926 else if (strcmp(value, "shutdown") == 0) {
3927 action = ST_ADM_ACTION_SHUTDOWN;
3928 }
3929 else if (strcmp(value, "dhlth") == 0) {
3930 action = ST_ADM_ACTION_DHLTH;
3931 }
3932 else if (strcmp(value, "ehlth") == 0) {
3933 action = ST_ADM_ACTION_EHLTH;
3934 }
3935 else if (strcmp(value, "hrunn") == 0) {
3936 action = ST_ADM_ACTION_HRUNN;
3937 }
3938 else if (strcmp(value, "hnolb") == 0) {
3939 action = ST_ADM_ACTION_HNOLB;
3940 }
3941 else if (strcmp(value, "hdown") == 0) {
3942 action = ST_ADM_ACTION_HDOWN;
3943 }
3944 else if (strcmp(value, "dagent") == 0) {
3945 action = ST_ADM_ACTION_DAGENT;
3946 }
3947 else if (strcmp(value, "eagent") == 0) {
3948 action = ST_ADM_ACTION_EAGENT;
3949 }
3950 else if (strcmp(value, "arunn") == 0) {
3951 action = ST_ADM_ACTION_ARUNN;
3952 }
3953 else if (strcmp(value, "adown") == 0) {
3954 action = ST_ADM_ACTION_ADOWN;
3955 }
3956 /* else these are the old supported methods */
3957 else if (strcmp(value, "disable") == 0) {
3958 action = ST_ADM_ACTION_DISABLE;
3959 }
3960 else if (strcmp(value, "enable") == 0) {
3961 action = ST_ADM_ACTION_ENABLE;
3962 }
3963 else if (strcmp(value, "stop") == 0) {
3964 action = ST_ADM_ACTION_STOP;
3965 }
3966 else if (strcmp(value, "start") == 0) {
3967 action = ST_ADM_ACTION_START;
3968 }
3969 else {
3970 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
3971 goto out;
3972 }
3973 }
3974 else if (strcmp(key, "s") == 0) {
3975 if (!(px && action)) {
3976 /*
3977 * Indicates that we'll need to reprocess the parameters
3978 * as soon as backend and action are known
3979 */
3980 if (!reprocess) {
3981 st_cur_param = cur_param;
3982 st_next_param = next_param;
3983 }
3984 reprocess = 1;
3985 }
3986 else if ((sv = findserver(px, value)) != NULL) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003987 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
William Lallemand74c24fb2016-11-21 17:18:36 +01003988 switch (action) {
3989 case ST_ADM_ACTION_DISABLE:
Emeric Brun52a91d32017-08-31 14:41:55 +02003990 if (!(sv->cur_admin & SRV_ADMF_FMAINT)) {
William Lallemand74c24fb2016-11-21 17:18:36 +01003991 altered_servers++;
3992 total_servers++;
3993 srv_set_admin_flag(sv, SRV_ADMF_FMAINT, "'disable' on stats page");
3994 }
3995 break;
3996 case ST_ADM_ACTION_ENABLE:
Emeric Brun52a91d32017-08-31 14:41:55 +02003997 if (sv->cur_admin & SRV_ADMF_FMAINT) {
William Lallemand74c24fb2016-11-21 17:18:36 +01003998 altered_servers++;
3999 total_servers++;
4000 srv_clr_admin_flag(sv, SRV_ADMF_FMAINT);
4001 }
4002 break;
4003 case ST_ADM_ACTION_STOP:
Emeric Brun52a91d32017-08-31 14:41:55 +02004004 if (!(sv->cur_admin & SRV_ADMF_FDRAIN)) {
William Lallemand74c24fb2016-11-21 17:18:36 +01004005 srv_set_admin_flag(sv, SRV_ADMF_FDRAIN, "'stop' on stats page");
4006 altered_servers++;
4007 total_servers++;
4008 }
4009 break;
4010 case ST_ADM_ACTION_START:
Emeric Brun52a91d32017-08-31 14:41:55 +02004011 if (sv->cur_admin & SRV_ADMF_FDRAIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +01004012 srv_clr_admin_flag(sv, SRV_ADMF_FDRAIN);
4013 altered_servers++;
4014 total_servers++;
4015 }
4016 break;
4017 case ST_ADM_ACTION_DHLTH:
4018 if (sv->check.state & CHK_ST_CONFIGURED) {
4019 sv->check.state &= ~CHK_ST_ENABLED;
4020 altered_servers++;
4021 total_servers++;
4022 }
4023 break;
4024 case ST_ADM_ACTION_EHLTH:
4025 if (sv->check.state & CHK_ST_CONFIGURED) {
4026 sv->check.state |= CHK_ST_ENABLED;
4027 altered_servers++;
4028 total_servers++;
4029 }
4030 break;
4031 case ST_ADM_ACTION_HRUNN:
4032 if (!(sv->track)) {
4033 sv->check.health = sv->check.rise + sv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02004034 srv_set_running(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01004035 altered_servers++;
4036 total_servers++;
4037 }
4038 break;
4039 case ST_ADM_ACTION_HNOLB:
4040 if (!(sv->track)) {
4041 sv->check.health = sv->check.rise + sv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02004042 srv_set_stopping(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01004043 altered_servers++;
4044 total_servers++;
4045 }
4046 break;
4047 case ST_ADM_ACTION_HDOWN:
4048 if (!(sv->track)) {
4049 sv->check.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02004050 srv_set_stopped(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01004051 altered_servers++;
4052 total_servers++;
4053 }
4054 break;
4055 case ST_ADM_ACTION_DAGENT:
4056 if (sv->agent.state & CHK_ST_CONFIGURED) {
4057 sv->agent.state &= ~CHK_ST_ENABLED;
4058 altered_servers++;
4059 total_servers++;
4060 }
4061 break;
4062 case ST_ADM_ACTION_EAGENT:
4063 if (sv->agent.state & CHK_ST_CONFIGURED) {
4064 sv->agent.state |= CHK_ST_ENABLED;
4065 altered_servers++;
4066 total_servers++;
4067 }
4068 break;
4069 case ST_ADM_ACTION_ARUNN:
4070 if (sv->agent.state & CHK_ST_ENABLED) {
4071 sv->agent.health = sv->agent.rise + sv->agent.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02004072 srv_set_running(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01004073 altered_servers++;
4074 total_servers++;
4075 }
4076 break;
4077 case ST_ADM_ACTION_ADOWN:
4078 if (sv->agent.state & CHK_ST_ENABLED) {
4079 sv->agent.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02004080 srv_set_stopped(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01004081 altered_servers++;
4082 total_servers++;
4083 }
4084 break;
4085 case ST_ADM_ACTION_READY:
4086 srv_adm_set_ready(sv);
4087 altered_servers++;
4088 total_servers++;
4089 break;
4090 case ST_ADM_ACTION_DRAIN:
4091 srv_adm_set_drain(sv);
4092 altered_servers++;
4093 total_servers++;
4094 break;
4095 case ST_ADM_ACTION_MAINT:
4096 srv_adm_set_maint(sv);
4097 altered_servers++;
4098 total_servers++;
4099 break;
4100 case ST_ADM_ACTION_SHUTDOWN:
Christopher Fauletdfd10ab2021-10-06 14:24:19 +02004101 if (!(px->flags & (PR_FL_DISABLED|PR_FL_STOPPED))) {
Willy Tarreauaf7ea812019-11-14 16:42:04 +01004102 srv_shutdown_streams(sv, SF_ERR_KILLED);
William Lallemand74c24fb2016-11-21 17:18:36 +01004103 altered_servers++;
4104 total_servers++;
4105 }
4106 break;
4107 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004108 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
William Lallemand74c24fb2016-11-21 17:18:36 +01004109 } else {
4110 /* the server name is unknown or ambiguous (duplicate names) */
4111 total_servers++;
4112 }
4113 }
4114 if (reprocess && px && action) {
4115 /* Now, we know the backend and the action chosen by the user.
4116 * We can safely restart from the first server parameter
4117 * to reprocess them
4118 */
4119 cur_param = st_cur_param;
4120 next_param = st_next_param;
4121 reprocess = 0;
4122 goto reprocess_servers;
4123 }
4124
4125 next_param = cur_param;
4126 }
4127 }
4128
4129 if (total_servers == 0) {
4130 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
4131 }
4132 else if (altered_servers == 0) {
4133 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
4134 }
4135 else if (altered_servers == total_servers) {
4136 appctx->ctx.stats.st_code = STAT_STATUS_DONE;
4137 }
4138 else {
4139 appctx->ctx.stats.st_code = STAT_STATUS_PART;
4140 }
4141 out:
4142 return 1;
Christopher Faulet2f9a41d2019-02-27 15:30:57 +01004143 wait:
4144 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
4145 return 0;
Christopher Fauletef779222018-10-31 08:47:01 +01004146}
4147
4148
Christopher Faulet908628c2022-03-25 16:43:49 +01004149static int stats_send_http_headers(struct conn_stream *cs, struct htx *htx)
Christopher Fauletef779222018-10-31 08:47:01 +01004150{
Christopher Faulet908628c2022-03-25 16:43:49 +01004151 struct stream *s = __cs_strm(cs);
Christopher Fauletef779222018-10-31 08:47:01 +01004152 struct uri_auth *uri = s->be->uri_auth;
Christopher Faulet908628c2022-03-25 16:43:49 +01004153 struct appctx *appctx = __cs_appctx(cs);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004154 struct htx_sl *sl;
4155 unsigned int flags;
Christopher Fauletef779222018-10-31 08:47:01 +01004156
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004157 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);
4158 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), ist("200"), ist("OK"));
4159 if (!sl)
Christopher Fauletef779222018-10-31 08:47:01 +01004160 goto full;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004161 sl->info.res.status = 200;
Christopher Fauletef779222018-10-31 08:47:01 +01004162
Christopher Fauletb829f4c2019-03-29 16:13:55 +01004163 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")))
Christopher Fauletef779222018-10-31 08:47:01 +01004164 goto full;
4165 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
4166 if (!htx_add_header(htx, ist("Content-Type"), ist("text/html")))
4167 goto full;
4168 }
Christopher Faulet6338a082019-09-09 15:50:54 +02004169 else if (appctx->ctx.stats.flags & (STAT_FMT_JSON|STAT_JSON_SCHM)) {
4170 if (!htx_add_header(htx, ist("Content-Type"), ist("application/json")))
4171 goto full;
4172 }
Christopher Fauletef779222018-10-31 08:47:01 +01004173 else {
4174 if (!htx_add_header(htx, ist("Content-Type"), ist("text/plain")))
4175 goto full;
4176 }
4177
4178 if (uri->refresh > 0 && !(appctx->ctx.stats.flags & STAT_NO_REFRESH)) {
4179 const char *refresh = U2A(uri->refresh);
Tim Duesterhusdcf753a2021-03-04 17:31:47 +01004180 if (!htx_add_header(htx, ist("Refresh"), ist(refresh)))
Christopher Fauletef779222018-10-31 08:47:01 +01004181 goto full;
4182 }
4183
4184 if (appctx->ctx.stats.flags & STAT_CHUNKED) {
4185 if (!htx_add_header(htx, ist("Transfer-Encoding"), ist("chunked")))
4186 goto full;
4187 }
4188
4189 if (!htx_add_endof(htx, HTX_BLK_EOH))
4190 goto full;
4191
Christopher Faulet1e2d6362019-02-27 16:28:48 +01004192 channel_add_input(&s->res, htx->data);
Christopher Fauletef779222018-10-31 08:47:01 +01004193 return 1;
4194
4195 full:
4196 htx_reset(htx);
Christopher Fauleta0bdec32022-04-04 07:51:21 +02004197 cs_rx_room_blk(cs);
Christopher Fauletef779222018-10-31 08:47:01 +01004198 return 0;
William Lallemand74c24fb2016-11-21 17:18:36 +01004199}
4200
Christopher Fauletef779222018-10-31 08:47:01 +01004201
Christopher Faulet908628c2022-03-25 16:43:49 +01004202static int stats_send_http_redirect(struct conn_stream *cs, struct htx *htx)
Christopher Fauletef779222018-10-31 08:47:01 +01004203{
4204 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
Christopher Faulet908628c2022-03-25 16:43:49 +01004205 struct stream *s = __cs_strm(cs);
Christopher Fauletef779222018-10-31 08:47:01 +01004206 struct uri_auth *uri = s->be->uri_auth;
Christopher Faulet908628c2022-03-25 16:43:49 +01004207 struct appctx *appctx = __cs_appctx(cs);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004208 struct htx_sl *sl;
4209 unsigned int flags;
Christopher Fauletef779222018-10-31 08:47:01 +01004210
4211 /* scope_txt = search pattern + search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
4212 scope_txt[0] = 0;
4213 if (appctx->ctx.stats.scope_len) {
Christopher Faulet908628c2022-03-25 16:43:49 +01004214 const char *scope_ptr = stats_scope_ptr(appctx, cs);
Christopher Fauleted7a0662019-01-14 11:07:34 +01004215
Christopher Fauletef779222018-10-31 08:47:01 +01004216 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Christopher Fauleted7a0662019-01-14 11:07:34 +01004217 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), scope_ptr, appctx->ctx.stats.scope_len);
Christopher Fauletef779222018-10-31 08:47:01 +01004218 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
4219 }
4220
4221 /* We don't want to land on the posted stats page because a refresh will
4222 * repost the data. We don't want this to happen on accident so we redirect
4223 * the browse to the stats page with a GET.
4224 */
4225 chunk_printf(&trash, "%s;st=%s%s%s%s",
4226 uri->uri_prefix,
4227 ((appctx->ctx.stats.st_code > STAT_STATUS_INIT) &&
4228 (appctx->ctx.stats.st_code < STAT_STATUS_SIZE) &&
4229 stat_status_codes[appctx->ctx.stats.st_code]) ?
4230 stat_status_codes[appctx->ctx.stats.st_code] :
4231 stat_status_codes[STAT_STATUS_UNKN],
4232 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
4233 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
4234 scope_txt);
4235
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004236 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_CHNK);
4237 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), ist("303"), ist("See Other"));
4238 if (!sl)
Christopher Fauletef779222018-10-31 08:47:01 +01004239 goto full;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004240 sl->info.res.status = 303;
Christopher Fauletef779222018-10-31 08:47:01 +01004241
4242 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")) ||
Christopher Fauletef779222018-10-31 08:47:01 +01004243 !htx_add_header(htx, ist("Content-Type"), ist("text/plain")) ||
4244 !htx_add_header(htx, ist("Content-Length"), ist("0")) ||
4245 !htx_add_header(htx, ist("Location"), ist2(trash.area, trash.data)))
4246 goto full;
4247
4248 if (!htx_add_endof(htx, HTX_BLK_EOH))
4249 goto full;
4250
Christopher Faulet1e2d6362019-02-27 16:28:48 +01004251 channel_add_input(&s->res, htx->data);
Christopher Fauletef779222018-10-31 08:47:01 +01004252 return 1;
4253
4254full:
4255 htx_reset(htx);
Christopher Fauleta0bdec32022-04-04 07:51:21 +02004256 cs_rx_room_blk(cs);
Christopher Fauletef779222018-10-31 08:47:01 +01004257 return 0;
4258}
William Lallemand74c24fb2016-11-21 17:18:36 +01004259
Simon Horman05ee2132017-01-04 09:37:25 +01004260
Christopher Faulet6b0a0fb2022-04-04 11:29:28 +02004261/* This I/O handler runs as an applet embedded in a conn-stream. It is
William Lallemand74c24fb2016-11-21 17:18:36 +01004262 * used to send HTTP stats over a TCP socket. The mechanism is very simple.
4263 * appctx->st0 contains the operation in progress (dump, done). The handler
4264 * automatically unregisters itself once transfer is complete.
4265 */
Christopher Fauletb7f88902019-07-15 21:56:43 +02004266static void http_stats_io_handler(struct appctx *appctx)
Christopher Fauletef779222018-10-31 08:47:01 +01004267{
Christopher Faulet908628c2022-03-25 16:43:49 +01004268 struct conn_stream *cs = appctx->owner;
4269 struct stream *s = __cs_strm(cs);
4270 struct channel *req = cs_oc(cs);
4271 struct channel *res = cs_ic(cs);
Christopher Fauletef779222018-10-31 08:47:01 +01004272 struct htx *req_htx, *res_htx;
4273
Amaury Denoyelle072f97e2020-10-05 11:49:37 +02004274 /* only proxy stats are available via http */
4275 appctx->ctx.stats.domain = STATS_DOMAIN_PROXY;
4276
Christopher Fauletef779222018-10-31 08:47:01 +01004277 res_htx = htx_from_buf(&res->buf);
4278
Christopher Faulet62e75742022-03-31 09:16:34 +02004279 if (unlikely(cs->state == CS_ST_DIS || cs->state == CS_ST_CLO))
Christopher Fauletef779222018-10-31 08:47:01 +01004280 goto out;
4281
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05004282 /* Check if the input buffer is available. */
Christopher Fauletef779222018-10-31 08:47:01 +01004283 if (!b_size(&res->buf)) {
Christopher Fauleta0bdec32022-04-04 07:51:21 +02004284 cs_rx_room_blk(cs);
Christopher Fauletef779222018-10-31 08:47:01 +01004285 goto out;
4286 }
4287
4288 /* check that the output is not closed */
Christopher Faulet3a78aa62019-02-27 16:19:48 +01004289 if (res->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_SHUTR))
4290 appctx->st0 = STAT_HTTP_END;
Christopher Fauletef779222018-10-31 08:47:01 +01004291
4292 /* all states are processed in sequence */
4293 if (appctx->st0 == STAT_HTTP_HEAD) {
Christopher Faulet908628c2022-03-25 16:43:49 +01004294 if (stats_send_http_headers(cs, res_htx)) {
Christopher Fauletef779222018-10-31 08:47:01 +01004295 if (s->txn->meth == HTTP_METH_HEAD)
4296 appctx->st0 = STAT_HTTP_DONE;
4297 else
4298 appctx->st0 = STAT_HTTP_DUMP;
4299 }
4300 }
4301
4302 if (appctx->st0 == STAT_HTTP_DUMP) {
Christopher Faulet908628c2022-03-25 16:43:49 +01004303 if (stats_dump_stat_to_buffer(cs, res_htx, s->be->uri_auth))
Christopher Fauletef779222018-10-31 08:47:01 +01004304 appctx->st0 = STAT_HTTP_DONE;
4305 }
4306
4307 if (appctx->st0 == STAT_HTTP_POST) {
Christopher Faulet908628c2022-03-25 16:43:49 +01004308 if (stats_process_http_post(cs))
Christopher Fauletef779222018-10-31 08:47:01 +01004309 appctx->st0 = STAT_HTTP_LAST;
Christopher Faulet3a78aa62019-02-27 16:19:48 +01004310 else if (req->flags & CF_SHUTR)
Christopher Fauletef779222018-10-31 08:47:01 +01004311 appctx->st0 = STAT_HTTP_DONE;
4312 }
4313
4314 if (appctx->st0 == STAT_HTTP_LAST) {
Christopher Faulet908628c2022-03-25 16:43:49 +01004315 if (stats_send_http_redirect(cs, res_htx))
Christopher Fauletef779222018-10-31 08:47:01 +01004316 appctx->st0 = STAT_HTTP_DONE;
4317 }
4318
4319 if (appctx->st0 == STAT_HTTP_DONE) {
Christopher Faulet08b45cb2022-04-07 09:25:13 +02004320 /* no more data are expected. If the response buffer is empty,
4321 * be sure to add something (EOT block in this case) to have
4322 * something to send. It is important to be sure the EOM flags
4323 * will be handled by the endpoint.
4324 */
4325 if (htx_is_empty(res_htx)) {
4326 if (!htx_add_endof(res_htx, HTX_BLK_EOT)) {
Christopher Fauleta0bdec32022-04-04 07:51:21 +02004327 cs_rx_room_blk(cs);
Christopher Faulet08b45cb2022-04-07 09:25:13 +02004328 goto out;
4329 }
4330 channel_add_input(res, 1);
4331 }
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01004332 res_htx->flags |= HTX_FL_EOM;
Christopher Faulet908628c2022-03-25 16:43:49 +01004333 cs->endp->flags |= CS_EP_EOI;
Christopher Faulet3fa5d192022-03-07 15:52:42 +01004334 res->flags |= CF_EOI;
Christopher Faulet3a78aa62019-02-27 16:19:48 +01004335 appctx->st0 = STAT_HTTP_END;
Christopher Fauletef779222018-10-31 08:47:01 +01004336 }
4337
Christopher Faulet3a78aa62019-02-27 16:19:48 +01004338 if (appctx->st0 == STAT_HTTP_END) {
4339 if (!(res->flags & CF_SHUTR)) {
Christopher Fauletef779222018-10-31 08:47:01 +01004340 res->flags |= CF_READ_NULL;
Christopher Fauletda098e62022-03-31 17:44:45 +02004341 cs_shutr(cs);
Christopher Faulet3a78aa62019-02-27 16:19:48 +01004342 }
4343
4344 /* eat the whole request */
4345 if (co_data(req)) {
4346 req_htx = htx_from_buf(&req->buf);
4347 co_htx_skip(req, req_htx, co_data(req));
4348 htx_to_buf(req_htx, &req->buf);
Christopher Fauletef779222018-10-31 08:47:01 +01004349 }
4350 }
Christopher Faulet3a78aa62019-02-27 16:19:48 +01004351
Christopher Fauletef779222018-10-31 08:47:01 +01004352 out:
4353 /* we have left the request in the buffer for the case where we
4354 * process a POST, and this automatically re-enables activity on
4355 * read. It's better to indicate that we want to stop reading when
4356 * we're sending, so that we know there's at most one direction
4357 * deciding to wake the applet up. It saves it from looping when
4358 * emitting large blocks into small TCP windows.
4359 */
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01004360 htx_to_buf(res_htx, &res->buf);
4361 if (!channel_is_empty(res))
Christopher Fauleta0bdec32022-04-04 07:51:21 +02004362 cs_stop_get(cs);
Christopher Fauletef779222018-10-31 08:47:01 +01004363}
4364
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004365/* Dump all fields from <info> into <out> using the "show info" format (name: value) */
Willy Tarreau83061a82018-07-13 11:56:34 +02004366static int stats_dump_info_fields(struct buffer *out,
Willy Tarreau43241ff2019-10-09 11:27:51 +02004367 const struct field *info, unsigned int flags)
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004368{
4369 int field;
4370
4371 for (field = 0; field < INF_TOTAL_FIELDS; field++) {
4372 if (!field_format(info, field))
4373 continue;
4374
Willy Tarreaueaa55372019-10-09 07:39:11 +02004375 if (!chunk_appendf(out, "%s: ", info_fields[field].name))
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004376 return 0;
4377 if (!stats_emit_raw_data_field(out, &info[field]))
4378 return 0;
Willy Tarreau6b19b142019-10-09 15:44:21 +02004379 if ((flags & STAT_SHOW_FDESC) && !chunk_appendf(out, ":\"%s\"", info_fields[field].desc))
4380 return 0;
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004381 if (!chunk_strcat(out, "\n"))
4382 return 0;
4383 }
4384 return 1;
4385}
4386
4387/* Dump all fields from <info> into <out> using the "show info typed" format */
Willy Tarreau83061a82018-07-13 11:56:34 +02004388static int stats_dump_typed_info_fields(struct buffer *out,
Willy Tarreau43241ff2019-10-09 11:27:51 +02004389 const struct field *info, unsigned int flags)
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004390{
4391 int field;
4392
4393 for (field = 0; field < INF_TOTAL_FIELDS; field++) {
4394 if (!field_format(info, field))
4395 continue;
4396
Willy Tarreaueaa55372019-10-09 07:39:11 +02004397 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 +01004398 return 0;
4399 if (!stats_emit_field_tags(out, &info[field], ':'))
4400 return 0;
4401 if (!stats_emit_typed_data_field(out, &info[field]))
4402 return 0;
Willy Tarreau6b19b142019-10-09 15:44:21 +02004403 if ((flags & STAT_SHOW_FDESC) && !chunk_appendf(out, ":\"%s\"", info_fields[field].desc))
4404 return 0;
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004405 if (!chunk_strcat(out, "\n"))
4406 return 0;
4407 }
4408 return 1;
4409}
4410
Willy Tarreau0b26b382021-05-08 07:43:53 +02004411/* Fill <info> with HAProxy global info. <info> is preallocated array of length
4412 * <len>. The length of the array must be INF_TOTAL_FIELDS. If this length is
4413 * less then this value, the function returns 0, otherwise, it returns 1. Some
4414 * fields' presence or precision may depend on some of the STAT_* flags present
4415 * in <flags>.
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004416 */
Willy Tarreau0b26b382021-05-08 07:43:53 +02004417int stats_fill_info(struct field *info, int len, uint flags)
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004418{
Willy Tarreaud37e26e2021-05-08 07:40:52 +02004419 struct timeval up;
Willy Tarreau83061a82018-07-13 11:56:34 +02004420 struct buffer *out = get_trash_chunk();
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004421
4422#ifdef USE_OPENSSL
Willy Tarreauc5977722021-05-08 08:14:04 +02004423 double ssl_sess_rate = read_freq_ctr_flt(&global.ssl_per_sec);
4424 double ssl_key_rate = read_freq_ctr_flt(&global.ssl_fe_keys_per_sec);
4425 double ssl_reuse = 0;
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004426
Willy Tarreauc5977722021-05-08 08:14:04 +02004427 if (ssl_key_rate < ssl_sess_rate)
4428 ssl_reuse = 100.0 * (1.0 - ssl_key_rate / ssl_sess_rate);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004429#endif
4430
Willy Tarreaud37e26e2021-05-08 07:40:52 +02004431 tv_remain(&start_date, &now, &up);
4432
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004433 if (len < INF_TOTAL_FIELDS)
4434 return 0;
4435
4436 chunk_reset(out);
4437 memset(info, 0, sizeof(*info) * len);
4438
4439 info[INF_NAME] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, PRODUCT_NAME);
Willy Tarreau909b9d82019-01-04 18:20:32 +01004440 info[INF_VERSION] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, haproxy_version);
Adis Nezirovicb62b78b2021-01-15 13:12:33 +01004441 info[INF_BUILD_INFO] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, haproxy_version);
Willy Tarreau909b9d82019-01-04 18:20:32 +01004442 info[INF_RELEASE_DATE] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, haproxy_date);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004443
Yves Lafon95317282018-02-26 11:10:37 +01004444 info[INF_NBTHREAD] = mkf_u32(FO_CONFIG|FS_SERVICE, global.nbthread);
Willy Tarreau91358592021-06-15 08:08:04 +02004445 info[INF_NBPROC] = mkf_u32(FO_CONFIG|FS_SERVICE, 1);
Willy Tarreaue8422bf2021-06-15 09:08:18 +02004446 info[INF_PROCESS_NUM] = mkf_u32(FO_KEY, 1);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004447 info[INF_PID] = mkf_u32(FO_STATUS, pid);
4448
4449 info[INF_UPTIME] = mkf_str(FN_DURATION, chunk_newstr(out));
Willy Tarreaud37e26e2021-05-08 07:40:52 +02004450 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 +01004451
Willy Tarreaue8abc322021-05-08 07:56:56 +02004452 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);
4453 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 +01004454 info[INF_MEMMAX_MB] = mkf_u32(FO_CONFIG|FN_LIMIT, global.rlimit_memmax);
William Dauchya8766cf2021-01-15 22:41:37 +01004455 info[INF_MEMMAX_BYTES] = mkf_u32(FO_CONFIG|FN_LIMIT, global.rlimit_memmax * 1048576L);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004456 info[INF_POOL_ALLOC_MB] = mkf_u32(0, (unsigned)(pool_total_allocated() / 1048576L));
William Dauchya8766cf2021-01-15 22:41:37 +01004457 info[INF_POOL_ALLOC_BYTES] = mkf_u32(0, pool_total_allocated());
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004458 info[INF_POOL_USED_MB] = mkf_u32(0, (unsigned)(pool_total_used() / 1048576L));
William Dauchya8766cf2021-01-15 22:41:37 +01004459 info[INF_POOL_USED_BYTES] = mkf_u32(0, pool_total_used());
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004460 info[INF_POOL_FAILED] = mkf_u32(FN_COUNTER, pool_total_failures());
4461 info[INF_ULIMIT_N] = mkf_u32(FO_CONFIG|FN_LIMIT, global.rlimit_nofile);
4462 info[INF_MAXSOCK] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxsock);
4463 info[INF_MAXCONN] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxconn);
4464 info[INF_HARD_MAXCONN] = mkf_u32(FO_CONFIG|FN_LIMIT, global.hardmaxconn);
4465 info[INF_CURR_CONN] = mkf_u32(0, actconn);
4466 info[INF_CUM_CONN] = mkf_u32(FN_COUNTER, totalconn);
4467 info[INF_CUM_REQ] = mkf_u32(FN_COUNTER, global.req_count);
4468#ifdef USE_OPENSSL
4469 info[INF_MAX_SSL_CONNS] = mkf_u32(FN_MAX, global.maxsslconn);
Willy Tarreau82531f62021-10-06 12:15:18 +02004470 info[INF_CURR_SSL_CONNS] = mkf_u32(0, global.sslconns);
4471 info[INF_CUM_SSL_CONNS] = mkf_u32(FN_COUNTER, global.totalsslconns);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004472#endif
4473 info[INF_MAXPIPES] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxpipes);
4474 info[INF_PIPES_USED] = mkf_u32(0, pipes_used);
4475 info[INF_PIPES_FREE] = mkf_u32(0, pipes_free);
Willy Tarreauc5977722021-05-08 08:14:04 +02004476 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 +01004477 info[INF_CONN_RATE_LIMIT] = mkf_u32(FO_CONFIG|FN_LIMIT, global.cps_lim);
4478 info[INF_MAX_CONN_RATE] = mkf_u32(FN_MAX, global.cps_max);
Willy Tarreauc5977722021-05-08 08:14:04 +02004479 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 +01004480 info[INF_SESS_RATE_LIMIT] = mkf_u32(FO_CONFIG|FN_LIMIT, global.sps_lim);
4481 info[INF_MAX_SESS_RATE] = mkf_u32(FN_RATE, global.sps_max);
4482
4483#ifdef USE_OPENSSL
Willy Tarreauc5977722021-05-08 08:14:04 +02004484 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 +01004485 info[INF_SSL_RATE_LIMIT] = mkf_u32(FO_CONFIG|FN_LIMIT, global.ssl_lim);
4486 info[INF_MAX_SSL_RATE] = mkf_u32(FN_MAX, global.ssl_max);
Willy Tarreauc5977722021-05-08 08:14:04 +02004487 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 +01004488 info[INF_SSL_FRONTEND_MAX_KEY_RATE] = mkf_u32(FN_MAX, global.ssl_fe_keys_max);
Willy Tarreauc5977722021-05-08 08:14:04 +02004489 info[INF_SSL_FRONTEND_SESSION_REUSE_PCT] = (flags & STAT_USE_FLOAT) ? mkf_flt(FN_RATE, ssl_reuse) : mkf_u32(0, ssl_reuse);
4490 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 +01004491 info[INF_SSL_BACKEND_MAX_KEY_RATE] = mkf_u32(FN_MAX, global.ssl_be_keys_max);
4492 info[INF_SSL_CACHE_LOOKUPS] = mkf_u32(FN_COUNTER, global.shctx_lookups);
4493 info[INF_SSL_CACHE_MISSES] = mkf_u32(FN_COUNTER, global.shctx_misses);
4494#endif
Willy Tarreauc5977722021-05-08 08:14:04 +02004495 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));
4496 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 +01004497 info[INF_COMPRESS_BPS_RATE_LIM] = mkf_u32(FO_CONFIG|FN_LIMIT, global.comp_rate_lim);
4498#ifdef USE_ZLIB
4499 info[INF_ZLIB_MEM_USAGE] = mkf_u32(0, zlib_used_memory);
4500 info[INF_MAX_ZLIB_MEM_USAGE] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxzlibmem);
4501#endif
Willy Tarreau955a11e2021-02-24 17:03:30 +01004502 info[INF_TASKS] = mkf_u32(0, total_allocated_tasks());
Willy Tarreau9c7b8082021-02-24 15:10:07 +01004503 info[INF_RUN_QUEUE] = mkf_u32(0, total_run_queues());
Willy Tarreauf9d5e102021-10-08 10:43:59 +02004504 info[INF_IDLE_PCT] = mkf_u32(FN_AVG, clock_report_idle());
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004505 info[INF_NODE] = mkf_str(FO_CONFIG|FN_OUTPUT|FS_SERVICE, global.node);
4506 if (global.desc)
4507 info[INF_DESCRIPTION] = mkf_str(FO_CONFIG|FN_OUTPUT|FS_SERVICE, global.desc);
Willy Tarreau00098ea2018-11-05 14:38:13 +01004508 info[INF_STOPPING] = mkf_u32(0, stopping);
4509 info[INF_JOBS] = mkf_u32(0, jobs);
William Lallemanda7199262018-11-16 16:57:20 +01004510 info[INF_UNSTOPPABLE_JOBS] = mkf_u32(0, unstoppable_jobs);
Willy Tarreau00098ea2018-11-05 14:38:13 +01004511 info[INF_LISTENERS] = mkf_u32(0, listeners);
Willy Tarreau199ad242018-11-05 16:31:22 +01004512 info[INF_ACTIVE_PEERS] = mkf_u32(0, active_peers);
Willy Tarreau2d372c22018-11-05 17:12:27 +01004513 info[INF_CONNECTED_PEERS] = mkf_u32(0, connected_peers);
Willy Tarreau13ef7732018-11-12 07:25:28 +01004514 info[INF_DROPPED_LOGS] = mkf_u32(0, dropped_logs);
Willy Tarreaubeb859a2018-11-22 18:07:59 +01004515 info[INF_BUSY_POLLING] = mkf_u32(0, !!(global.tune.options & GTUNE_BUSY_POLLING));
Emeric Brund30e9a12020-12-23 18:49:16 +01004516 info[INF_FAILED_RESOLUTIONS] = mkf_u32(0, resolv_failed_resolutions);
Willy Tarreau7cf0e452019-05-23 11:39:14 +02004517 info[INF_TOTAL_BYTES_OUT] = mkf_u64(0, global.out_bytes);
Christopher Fauletaaa70852020-07-10 13:56:30 +02004518 info[INF_TOTAL_SPLICED_BYTES_OUT] = mkf_u64(0, global.spliced_out_bytes);
Willy Tarreau7cf0e452019-05-23 11:39:14 +02004519 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 +02004520 info[INF_DEBUG_COMMANDS_ISSUED] = mkf_u32(0, debug_commands_issued);
Emeric Brun45c457a2020-07-09 23:23:34 +02004521 info[INF_CUM_LOG_MSGS] = mkf_u32(FN_COUNTER, cum_log_messages);
Amaury Denoyelle5dfdf3e2021-05-05 17:09:12 +02004522
4523 info[INF_TAINTED] = mkf_str(FO_STATUS, chunk_newstr(out));
4524 chunk_appendf(out, "%#x", get_tainted());
4525
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004526 return 1;
4527}
4528
Christopher Faulet908628c2022-03-25 16:43:49 +01004529/* This function dumps information onto the conn-stream's read buffer.
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004530 * It returns 0 as long as it does not complete, non-zero upon completion.
4531 * No state is used.
4532 */
Christopher Faulet908628c2022-03-25 16:43:49 +01004533static int stats_dump_info_to_buffer(struct conn_stream *cs)
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004534{
Christopher Faulet908628c2022-03-25 16:43:49 +01004535 struct appctx *appctx = __cs_appctx(cs);
Christopher Faulet2da02ae2022-02-24 13:45:27 +01004536
Willy Tarreau0b26b382021-05-08 07:43:53 +02004537 if (!stats_fill_info(info, INF_TOTAL_FIELDS, appctx->ctx.stats.flags))
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004538 return 0;
4539
4540 chunk_reset(&trash);
4541
4542 if (appctx->ctx.stats.flags & STAT_FMT_TYPED)
Willy Tarreau43241ff2019-10-09 11:27:51 +02004543 stats_dump_typed_info_fields(&trash, info, appctx->ctx.stats.flags);
Simon Horman05ee2132017-01-04 09:37:25 +01004544 else if (appctx->ctx.stats.flags & STAT_FMT_JSON)
Willy Tarreau43241ff2019-10-09 11:27:51 +02004545 stats_dump_json_info_fields(&trash, info, appctx->ctx.stats.flags);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004546 else
Willy Tarreau43241ff2019-10-09 11:27:51 +02004547 stats_dump_info_fields(&trash, info, appctx->ctx.stats.flags);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004548
Christopher Faulet908628c2022-03-25 16:43:49 +01004549 if (ci_putchk(cs_ic(cs), &trash) == -1) {
Christopher Fauleta0bdec32022-04-04 07:51:21 +02004550 cs_rx_room_blk(cs);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004551 return 0;
4552 }
4553
4554 return 1;
4555}
4556
Christopher Faulet6b0a0fb2022-04-04 11:29:28 +02004557/* This function dumps the schema onto the conn-stream's read buffer.
Simon Horman6f6bb382017-01-04 09:37:26 +01004558 * It returns 0 as long as it does not complete, non-zero upon completion.
4559 * No state is used.
4560 *
Ilya Shipitsinf38a0182020-12-21 01:16:17 +05004561 * Integer values bounded to the range [-(2**53)+1, (2**53)-1] as
Simon Horman6f6bb382017-01-04 09:37:26 +01004562 * per the recommendation for interoperable integers in section 6 of RFC 7159.
4563 */
Willy Tarreau83061a82018-07-13 11:56:34 +02004564static void stats_dump_json_schema(struct buffer *out)
Simon Horman6f6bb382017-01-04 09:37:26 +01004565{
4566
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004567 int old_len = out->data;
Simon Horman6f6bb382017-01-04 09:37:26 +01004568
4569 chunk_strcat(out,
4570 "{"
4571 "\"$schema\":\"http://json-schema.org/draft-04/schema#\","
4572 "\"oneOf\":["
4573 "{"
4574 "\"title\":\"Info\","
4575 "\"type\":\"array\","
4576 "\"items\":{"
Amaury Denoyellea53ce4c2020-10-02 18:31:59 +02004577 "\"title\":\"InfoItem\","
4578 "\"type\":\"object\","
Simon Horman6f6bb382017-01-04 09:37:26 +01004579 "\"properties\":{"
Simon Horman6f6bb382017-01-04 09:37:26 +01004580 "\"field\":{\"$ref\":\"#/definitions/field\"},"
4581 "\"processNum\":{\"$ref\":\"#/definitions/processNum\"},"
4582 "\"tags\":{\"$ref\":\"#/definitions/tags\"},"
4583 "\"value\":{\"$ref\":\"#/definitions/typedValue\"}"
4584 "},"
4585 "\"required\":[\"field\",\"processNum\",\"tags\","
4586 "\"value\"]"
4587 "}"
4588 "},"
4589 "{"
4590 "\"title\":\"Stat\","
4591 "\"type\":\"array\","
4592 "\"items\":{"
4593 "\"title\":\"InfoItem\","
4594 "\"type\":\"object\","
4595 "\"properties\":{"
4596 "\"objType\":{"
4597 "\"enum\":[\"Frontend\",\"Backend\",\"Listener\","
4598 "\"Server\",\"Unknown\"]"
4599 "},"
4600 "\"proxyId\":{"
4601 "\"type\":\"integer\","
4602 "\"minimum\":0"
4603 "},"
4604 "\"id\":{"
4605 "\"type\":\"integer\","
4606 "\"minimum\":0"
4607 "},"
4608 "\"field\":{\"$ref\":\"#/definitions/field\"},"
4609 "\"processNum\":{\"$ref\":\"#/definitions/processNum\"},"
4610 "\"tags\":{\"$ref\":\"#/definitions/tags\"},"
4611 "\"typedValue\":{\"$ref\":\"#/definitions/typedValue\"}"
4612 "},"
4613 "\"required\":[\"objType\",\"proxyId\",\"id\","
4614 "\"field\",\"processNum\",\"tags\","
4615 "\"value\"]"
4616 "}"
4617 "},"
4618 "{"
4619 "\"title\":\"Error\","
4620 "\"type\":\"object\","
4621 "\"properties\":{"
4622 "\"errorStr\":{"
4623 "\"type\":\"string\""
Amaury Denoyellea53ce4c2020-10-02 18:31:59 +02004624 "}"
4625 "},"
4626 "\"required\":[\"errorStr\"]"
Simon Horman6f6bb382017-01-04 09:37:26 +01004627 "}"
4628 "],"
4629 "\"definitions\":{"
4630 "\"field\":{"
4631 "\"type\":\"object\","
4632 "\"pos\":{"
4633 "\"type\":\"integer\","
4634 "\"minimum\":0"
4635 "},"
4636 "\"name\":{"
4637 "\"type\":\"string\""
4638 "},"
4639 "\"required\":[\"pos\",\"name\"]"
4640 "},"
4641 "\"processNum\":{"
4642 "\"type\":\"integer\","
4643 "\"minimum\":1"
4644 "},"
4645 "\"tags\":{"
4646 "\"type\":\"object\","
4647 "\"origin\":{"
4648 "\"type\":\"string\","
4649 "\"enum\":[\"Metric\",\"Status\",\"Key\","
4650 "\"Config\",\"Product\",\"Unknown\"]"
4651 "},"
4652 "\"nature\":{"
4653 "\"type\":\"string\","
4654 "\"enum\":[\"Gauge\",\"Limit\",\"Min\",\"Max\","
4655 "\"Rate\",\"Counter\",\"Duration\","
4656 "\"Age\",\"Time\",\"Name\",\"Output\","
4657 "\"Avg\", \"Unknown\"]"
4658 "},"
4659 "\"scope\":{"
4660 "\"type\":\"string\","
4661 "\"enum\":[\"Cluster\",\"Process\",\"Service\","
4662 "\"System\",\"Unknown\"]"
4663 "},"
4664 "\"required\":[\"origin\",\"nature\",\"scope\"]"
4665 "},"
4666 "\"typedValue\":{"
4667 "\"type\":\"object\","
4668 "\"oneOf\":["
4669 "{\"$ref\":\"#/definitions/typedValue/definitions/s32Value\"},"
4670 "{\"$ref\":\"#/definitions/typedValue/definitions/s64Value\"},"
4671 "{\"$ref\":\"#/definitions/typedValue/definitions/u32Value\"},"
4672 "{\"$ref\":\"#/definitions/typedValue/definitions/u64Value\"},"
4673 "{\"$ref\":\"#/definitions/typedValue/definitions/strValue\"}"
4674 "],"
4675 "\"definitions\":{"
4676 "\"s32Value\":{"
4677 "\"properties\":{"
4678 "\"type\":{"
4679 "\"type\":\"string\","
4680 "\"enum\":[\"s32\"]"
4681 "},"
4682 "\"value\":{"
4683 "\"type\":\"integer\","
4684 "\"minimum\":-2147483648,"
4685 "\"maximum\":2147483647"
4686 "}"
4687 "},"
4688 "\"required\":[\"type\",\"value\"]"
4689 "},"
4690 "\"s64Value\":{"
4691 "\"properties\":{"
4692 "\"type\":{"
4693 "\"type\":\"string\","
4694 "\"enum\":[\"s64\"]"
4695 "},"
4696 "\"value\":{"
4697 "\"type\":\"integer\","
4698 "\"minimum\":-9007199254740991,"
4699 "\"maximum\":9007199254740991"
4700 "}"
4701 "},"
4702 "\"required\":[\"type\",\"value\"]"
4703 "},"
4704 "\"u32Value\":{"
4705 "\"properties\":{"
4706 "\"type\":{"
4707 "\"type\":\"string\","
4708 "\"enum\":[\"u32\"]"
4709 "},"
4710 "\"value\":{"
4711 "\"type\":\"integer\","
4712 "\"minimum\":0,"
4713 "\"maximum\":4294967295"
4714 "}"
4715 "},"
4716 "\"required\":[\"type\",\"value\"]"
4717 "},"
4718 "\"u64Value\":{"
4719 "\"properties\":{"
4720 "\"type\":{"
4721 "\"type\":\"string\","
4722 "\"enum\":[\"u64\"]"
4723 "},"
4724 "\"value\":{"
4725 "\"type\":\"integer\","
4726 "\"minimum\":0,"
4727 "\"maximum\":9007199254740991"
4728 "}"
4729 "},"
4730 "\"required\":[\"type\",\"value\"]"
4731 "},"
4732 "\"strValue\":{"
4733 "\"properties\":{"
4734 "\"type\":{"
4735 "\"type\":\"string\","
4736 "\"enum\":[\"str\"]"
4737 "},"
4738 "\"value\":{\"type\":\"string\"}"
4739 "},"
4740 "\"required\":[\"type\",\"value\"]"
4741 "},"
4742 "\"unknownValue\":{"
4743 "\"properties\":{"
4744 "\"type\":{"
4745 "\"type\":\"integer\","
4746 "\"minimum\":0"
4747 "},"
4748 "\"value\":{"
4749 "\"type\":\"string\","
4750 "\"enum\":[\"unknown\"]"
4751 "}"
4752 "},"
4753 "\"required\":[\"type\",\"value\"]"
4754 "}"
4755 "}"
4756 "}"
4757 "}"
4758 "}");
4759
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004760 if (old_len == out->data) {
Simon Horman6f6bb382017-01-04 09:37:26 +01004761 chunk_reset(out);
4762 chunk_appendf(out,
4763 "{\"errorStr\":\"output buffer too short\"}");
4764 }
4765}
4766
Christopher Faulet6b0a0fb2022-04-04 11:29:28 +02004767/* This function dumps the schema onto the conn-stream's read buffer.
Simon Horman6f6bb382017-01-04 09:37:26 +01004768 * It returns 0 as long as it does not complete, non-zero upon completion.
4769 * No state is used.
4770 */
Christopher Faulet908628c2022-03-25 16:43:49 +01004771static int stats_dump_json_schema_to_buffer(struct conn_stream *cs)
Simon Horman6f6bb382017-01-04 09:37:26 +01004772{
4773 chunk_reset(&trash);
4774
4775 stats_dump_json_schema(&trash);
4776
Christopher Faulet908628c2022-03-25 16:43:49 +01004777 if (ci_putchk(cs_ic(cs), &trash) == -1) {
Christopher Fauleta0bdec32022-04-04 07:51:21 +02004778 cs_rx_room_blk(cs);
Simon Horman6f6bb382017-01-04 09:37:26 +01004779 return 0;
4780 }
4781
4782 return 1;
4783}
4784
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004785static int cli_parse_clear_counters(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau89d467c2016-11-23 11:02:40 +01004786{
4787 struct proxy *px;
4788 struct server *sv;
4789 struct listener *li;
Amaury Denoyelled3700a72020-10-05 11:49:43 +02004790 struct stats_module *mod;
Willy Tarreau89d467c2016-11-23 11:02:40 +01004791 int clrall = 0;
4792
4793 if (strcmp(args[2], "all") == 0)
4794 clrall = 1;
4795
4796 /* check permissions */
4797 if (!cli_has_level(appctx, ACCESS_LVL_OPER) ||
4798 (clrall && !cli_has_level(appctx, ACCESS_LVL_ADMIN)))
4799 return 1;
4800
Olivier Houchardfbc74e82017-11-24 16:54:05 +01004801 for (px = proxies_list; px; px = px->next) {
Willy Tarreau89d467c2016-11-23 11:02:40 +01004802 if (clrall) {
4803 memset(&px->be_counters, 0, sizeof(px->be_counters));
4804 memset(&px->fe_counters, 0, sizeof(px->fe_counters));
4805 }
4806 else {
4807 px->be_counters.conn_max = 0;
4808 px->be_counters.p.http.rps_max = 0;
4809 px->be_counters.sps_max = 0;
4810 px->be_counters.cps_max = 0;
4811 px->be_counters.nbpend_max = 0;
Christopher Fauletefb41f02019-11-08 14:53:15 +01004812 px->be_counters.qtime_max = 0;
4813 px->be_counters.ctime_max = 0;
4814 px->be_counters.dtime_max = 0;
4815 px->be_counters.ttime_max = 0;
Willy Tarreau89d467c2016-11-23 11:02:40 +01004816
4817 px->fe_counters.conn_max = 0;
4818 px->fe_counters.p.http.rps_max = 0;
4819 px->fe_counters.sps_max = 0;
4820 px->fe_counters.cps_max = 0;
Willy Tarreau89d467c2016-11-23 11:02:40 +01004821 }
4822
4823 for (sv = px->srv; sv; sv = sv->next)
4824 if (clrall)
4825 memset(&sv->counters, 0, sizeof(sv->counters));
4826 else {
4827 sv->counters.cur_sess_max = 0;
4828 sv->counters.nbpend_max = 0;
4829 sv->counters.sps_max = 0;
Christopher Fauletefb41f02019-11-08 14:53:15 +01004830 sv->counters.qtime_max = 0;
4831 sv->counters.ctime_max = 0;
4832 sv->counters.dtime_max = 0;
4833 sv->counters.ttime_max = 0;
Willy Tarreau89d467c2016-11-23 11:02:40 +01004834 }
4835
4836 list_for_each_entry(li, &px->conf.listeners, by_fe)
4837 if (li->counters) {
4838 if (clrall)
4839 memset(li->counters, 0, sizeof(*li->counters));
4840 else
4841 li->counters->conn_max = 0;
4842 }
4843 }
4844
4845 global.cps_max = 0;
4846 global.sps_max = 0;
Olivier Houchard00bc3cb2017-10-17 19:23:25 +02004847 global.ssl_max = 0;
4848 global.ssl_fe_keys_max = 0;
4849 global.ssl_be_keys_max = 0;
Willy Tarreaud80cb4e2018-01-20 19:30:13 +01004850
Amaury Denoyelled3700a72020-10-05 11:49:43 +02004851 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
4852 if (!mod->clearable && !clrall)
4853 continue;
4854
4855 for (px = proxies_list; px; px = px->next) {
4856 enum stats_domain_px_cap mod_cap = stats_px_get_cap(mod->domain_flags);
4857
4858 if (px->cap & PR_CAP_FE && mod_cap & STATS_PX_CAP_FE) {
4859 EXTRA_COUNTERS_INIT(px->extra_counters_fe,
4860 mod,
4861 mod->counters,
4862 mod->counters_size);
4863 }
4864
4865 if (px->cap & PR_CAP_BE && mod_cap & STATS_PX_CAP_BE) {
4866 EXTRA_COUNTERS_INIT(px->extra_counters_be,
4867 mod,
4868 mod->counters,
4869 mod->counters_size);
4870 }
4871
4872 if (mod_cap & STATS_PX_CAP_SRV) {
4873 for (sv = px->srv; sv; sv = sv->next) {
4874 EXTRA_COUNTERS_INIT(sv->extra_counters,
4875 mod,
4876 mod->counters,
4877 mod->counters_size);
4878 }
4879 }
4880
4881 if (mod_cap & STATS_PX_CAP_LI) {
4882 list_for_each_entry(li, &px->conf.listeners, by_fe) {
4883 EXTRA_COUNTERS_INIT(li->extra_counters,
4884 mod,
4885 mod->counters,
4886 mod->counters_size);
4887 }
4888 }
4889 }
4890 }
4891
Emeric Brunf8642ee2021-10-29 17:59:18 +02004892 resolv_stats_clear_counters(clrall, &stats_module_list[STATS_DOMAIN_RESOLVERS]);
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02004893
Willy Tarreaud80cb4e2018-01-20 19:30:13 +01004894 memset(activity, 0, sizeof(activity));
Willy Tarreau89d467c2016-11-23 11:02:40 +01004895 return 1;
4896}
4897
4898
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004899static int cli_parse_show_info(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004900{
Willy Tarreau2f397382019-10-09 11:43:59 +02004901 int arg = 2;
4902
Willy Tarreaud25fc792016-12-16 12:33:47 +01004903 appctx->ctx.stats.scope_str = 0;
4904 appctx->ctx.stats.scope_len = 0;
4905 appctx->ctx.stats.flags = 0;
4906
Willy Tarreau2f397382019-10-09 11:43:59 +02004907 while (*args[arg]) {
4908 if (strcmp(args[arg], "typed") == 0)
4909 appctx->ctx.stats.flags = (appctx->ctx.stats.flags & ~STAT_FMT_MASK) | STAT_FMT_TYPED;
4910 else if (strcmp(args[arg], "json") == 0)
4911 appctx->ctx.stats.flags = (appctx->ctx.stats.flags & ~STAT_FMT_MASK) | STAT_FMT_JSON;
4912 else if (strcmp(args[arg], "desc") == 0)
4913 appctx->ctx.stats.flags |= STAT_SHOW_FDESC;
Willy Tarreau27456202021-05-08 07:54:24 +02004914 else if (strcmp(args[arg], "float") == 0)
4915 appctx->ctx.stats.flags |= STAT_USE_FLOAT;
Willy Tarreau2f397382019-10-09 11:43:59 +02004916 arg++;
4917 }
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004918 return 0;
4919}
4920
4921
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004922static int cli_parse_show_stat(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau2b812e22016-11-22 16:18:05 +01004923{
Willy Tarreau2f397382019-10-09 11:43:59 +02004924 int arg = 2;
4925
Willy Tarreaud25fc792016-12-16 12:33:47 +01004926 appctx->ctx.stats.scope_str = 0;
4927 appctx->ctx.stats.scope_len = 0;
Willy Tarreau578d6e42019-10-09 11:00:22 +02004928 appctx->ctx.stats.flags = STAT_SHNODE | STAT_SHDESC;
4929
Christopher Faulet908628c2022-03-25 16:43:49 +01004930 if ((strm_li(__cs_strm(appctx->owner))->bind_conf->level & ACCESS_LVL_MASK) >= ACCESS_LVL_OPER)
Willy Tarreau578d6e42019-10-09 11:00:22 +02004931 appctx->ctx.stats.flags |= STAT_SHLGNDS;
Willy Tarreaud25fc792016-12-16 12:33:47 +01004932
Amaury Denoyelle072f97e2020-10-05 11:49:37 +02004933 /* proxy is the default domain */
4934 appctx->ctx.stats.domain = STATS_DOMAIN_PROXY;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01004935 if (strcmp(args[arg], "domain") == 0) {
Amaury Denoyelle072f97e2020-10-05 11:49:37 +02004936 ++args;
4937
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01004938 if (strcmp(args[arg], "proxy") == 0) {
Amaury Denoyelle072f97e2020-10-05 11:49:37 +02004939 ++args;
Emeric Brunf8642ee2021-10-29 17:59:18 +02004940 } else if (strcmp(args[arg], "resolvers") == 0) {
4941 appctx->ctx.stats.domain = STATS_DOMAIN_RESOLVERS;
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02004942 ++args;
4943 } else {
Amaury Denoyelle072f97e2020-10-05 11:49:37 +02004944 return cli_err(appctx, "Invalid statistics domain.\n");
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02004945 }
Amaury Denoyelle072f97e2020-10-05 11:49:37 +02004946 }
4947
4948 if (appctx->ctx.stats.domain == STATS_DOMAIN_PROXY
4949 && *args[arg] && *args[arg+1] && *args[arg+2]) {
Willy Tarreaua1b1ed52016-11-25 08:50:58 +01004950 struct proxy *px;
4951
Willy Tarreau2f397382019-10-09 11:43:59 +02004952 px = proxy_find_by_name(args[arg], 0, 0);
Willy Tarreaua1b1ed52016-11-25 08:50:58 +01004953 if (px)
4954 appctx->ctx.stats.iid = px->uuid;
4955 else
Willy Tarreau2f397382019-10-09 11:43:59 +02004956 appctx->ctx.stats.iid = atoi(args[arg]);
Willy Tarreaua1b1ed52016-11-25 08:50:58 +01004957
Willy Tarreau9d008692019-08-09 11:21:01 +02004958 if (!appctx->ctx.stats.iid)
4959 return cli_err(appctx, "No such proxy.\n");
Willy Tarreaua1b1ed52016-11-25 08:50:58 +01004960
Willy Tarreau2b812e22016-11-22 16:18:05 +01004961 appctx->ctx.stats.flags |= STAT_BOUND;
Willy Tarreau2f397382019-10-09 11:43:59 +02004962 appctx->ctx.stats.type = atoi(args[arg+1]);
4963 appctx->ctx.stats.sid = atoi(args[arg+2]);
4964 arg += 3;
4965 }
4966
4967 while (*args[arg]) {
4968 if (strcmp(args[arg], "typed") == 0)
4969 appctx->ctx.stats.flags = (appctx->ctx.stats.flags & ~STAT_FMT_MASK) | STAT_FMT_TYPED;
4970 else if (strcmp(args[arg], "json") == 0)
4971 appctx->ctx.stats.flags = (appctx->ctx.stats.flags & ~STAT_FMT_MASK) | STAT_FMT_JSON;
4972 else if (strcmp(args[arg], "desc") == 0)
4973 appctx->ctx.stats.flags |= STAT_SHOW_FDESC;
Willy Tarreau3e320362020-10-23 17:28:57 +02004974 else if (strcmp(args[arg], "no-maint") == 0)
4975 appctx->ctx.stats.flags |= STAT_HIDE_MAINT;
Willy Tarreau65141ff2020-10-23 17:19:48 +02004976 else if (strcmp(args[arg], "up") == 0)
4977 appctx->ctx.stats.flags |= STAT_HIDE_DOWN;
Willy Tarreau2f397382019-10-09 11:43:59 +02004978 arg++;
Willy Tarreau2b812e22016-11-22 16:18:05 +01004979 }
Willy Tarreau2b812e22016-11-22 16:18:05 +01004980
Willy Tarreau2b812e22016-11-22 16:18:05 +01004981 return 0;
4982}
4983
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004984static int cli_io_handler_dump_info(struct appctx *appctx)
4985{
Christopher Faulet908628c2022-03-25 16:43:49 +01004986 return stats_dump_info_to_buffer(appctx->owner);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004987}
4988
Christopher Faulet908628c2022-03-25 16:43:49 +01004989/* This I/O handler runs as an applet embedded in a conn-stream. It is
Willy Tarreau2b812e22016-11-22 16:18:05 +01004990 * used to send raw stats over a socket.
4991 */
4992static int cli_io_handler_dump_stat(struct appctx *appctx)
4993{
Christopher Faulet908628c2022-03-25 16:43:49 +01004994 return stats_dump_stat_to_buffer(appctx->owner, NULL, NULL);
Willy Tarreau2b812e22016-11-22 16:18:05 +01004995}
4996
Simon Horman6f6bb382017-01-04 09:37:26 +01004997static int cli_io_handler_dump_json_schema(struct appctx *appctx)
4998{
Christopher Faulet908628c2022-03-25 16:43:49 +01004999 return stats_dump_json_schema_to_buffer(appctx->owner);
Simon Horman6f6bb382017-01-04 09:37:26 +01005000}
5001
Amaury Denoyelle216a1ce2021-03-18 15:48:14 +01005002int stats_allocate_proxy_counters_internal(struct extra_counters **counters,
5003 int type, int px_cap)
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02005004{
5005 struct stats_module *mod;
5006
5007 EXTRA_COUNTERS_REGISTER(counters, type, alloc_failed);
5008
5009 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
5010 if (!(stats_px_get_cap(mod->domain_flags) & px_cap))
5011 continue;
5012
5013 EXTRA_COUNTERS_ADD(mod, *counters, mod->counters, mod->counters_size);
5014 }
5015
5016 EXTRA_COUNTERS_ALLOC(*counters, alloc_failed);
5017
5018 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
5019 if (!(stats_px_get_cap(mod->domain_flags) & px_cap))
5020 continue;
5021
5022 EXTRA_COUNTERS_INIT(*counters, mod, mod->counters, mod->counters_size);
5023 }
5024
5025 return 1;
5026
5027 alloc_failed:
5028 return 0;
5029}
5030
5031/* Initialize and allocate all extra counters for a proxy and its attached
5032 * servers/listeners with all already registered stats module
5033 */
5034int stats_allocate_proxy_counters(struct proxy *px)
5035{
5036 struct server *sv;
5037 struct listener *li;
5038
5039 if (px->cap & PR_CAP_FE) {
5040 if (!stats_allocate_proxy_counters_internal(&px->extra_counters_fe,
5041 COUNTERS_FE,
5042 STATS_PX_CAP_FE)) {
5043 return 0;
5044 }
5045 }
5046
5047 if (px->cap & PR_CAP_BE) {
5048 if (!stats_allocate_proxy_counters_internal(&px->extra_counters_be,
5049 COUNTERS_BE,
5050 STATS_PX_CAP_BE)) {
5051 return 0;
5052 }
5053 }
5054
5055 for (sv = px->srv; sv; sv = sv->next) {
5056 if (!stats_allocate_proxy_counters_internal(&sv->extra_counters,
5057 COUNTERS_SV,
5058 STATS_PX_CAP_SRV)) {
5059 return 0;
5060 }
5061 }
5062
5063 list_for_each_entry(li, &px->conf.listeners, by_fe) {
5064 if (!stats_allocate_proxy_counters_internal(&li->extra_counters,
5065 COUNTERS_LI,
5066 STATS_PX_CAP_LI)) {
5067 return 0;
5068 }
5069 }
5070
5071 return 1;
5072}
5073
Amaury Denoyelle58d395e2020-10-05 11:49:40 +02005074void stats_register_module(struct stats_module *m)
5075{
5076 const uint8_t domain = stats_get_domain(m->domain_flags);
5077
Willy Tarreau2b718102021-04-21 07:32:39 +02005078 LIST_APPEND(&stats_module_list[domain], &m->list);
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02005079 stat_count[domain] += m->stats_count;
Amaury Denoyelle58d395e2020-10-05 11:49:40 +02005080}
5081
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02005082static int allocate_stats_px_postcheck(void)
5083{
5084 struct stats_module *mod;
5085 size_t i = ST_F_TOTAL_FIELDS;
5086 int err_code = 0;
Amaury Denoyelle27373f72020-10-05 16:57:33 +02005087 struct proxy *px;
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02005088
5089 stat_count[STATS_DOMAIN_PROXY] += ST_F_TOTAL_FIELDS;
5090
5091 stat_f[STATS_DOMAIN_PROXY] = malloc(stat_count[STATS_DOMAIN_PROXY] * sizeof(struct name_desc));
5092 if (!stat_f[STATS_DOMAIN_PROXY]) {
5093 ha_alert("stats: cannot allocate all fields for proxy statistics\n");
5094 err_code |= ERR_ALERT | ERR_FATAL;
5095 return err_code;
5096 }
5097
5098 memcpy(stat_f[STATS_DOMAIN_PROXY], stat_fields,
5099 ST_F_TOTAL_FIELDS * sizeof(struct name_desc));
5100
5101 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
5102 memcpy(stat_f[STATS_DOMAIN_PROXY] + i,
5103 mod->stats,
5104 mod->stats_count * sizeof(struct name_desc));
5105 i += mod->stats_count;
5106 }
5107
Amaury Denoyelle27373f72020-10-05 16:57:33 +02005108 for (px = proxies_list; px; px = px->next) {
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02005109 if (!stats_allocate_proxy_counters(px)) {
5110 ha_alert("stats: cannot allocate all counters for proxy statistics\n");
5111 err_code |= ERR_ALERT | ERR_FATAL;
5112 return err_code;
5113 }
5114 }
5115
Christopher Fauletde79cd22021-01-06 07:41:56 +01005116 /* wait per-thread alloc to perform corresponding stat_l allocation */
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02005117
5118 return err_code;
5119}
5120
5121REGISTER_CONFIG_POSTPARSER("allocate-stats-px", allocate_stats_px_postcheck);
5122
Emeric Brunf8642ee2021-10-29 17:59:18 +02005123static int allocate_stats_rslv_postcheck(void)
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02005124{
5125 struct stats_module *mod;
5126 size_t i = 0;
5127 int err_code = 0;
5128
Emeric Brunf8642ee2021-10-29 17:59:18 +02005129 stat_f[STATS_DOMAIN_RESOLVERS] = malloc(stat_count[STATS_DOMAIN_RESOLVERS] * sizeof(struct name_desc));
5130 if (!stat_f[STATS_DOMAIN_RESOLVERS]) {
5131 ha_alert("stats: cannot allocate all fields for resolver statistics\n");
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02005132 err_code |= ERR_ALERT | ERR_FATAL;
5133 return err_code;
5134 }
5135
Emeric Brunf8642ee2021-10-29 17:59:18 +02005136 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_RESOLVERS], list) {
5137 memcpy(stat_f[STATS_DOMAIN_RESOLVERS] + i,
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02005138 mod->stats,
5139 mod->stats_count * sizeof(struct name_desc));
5140 i += mod->stats_count;
5141 }
5142
Emeric Brunf8642ee2021-10-29 17:59:18 +02005143 if (!resolv_allocate_counters(&stats_module_list[STATS_DOMAIN_RESOLVERS])) {
5144 ha_alert("stats: cannot allocate all counters for resolver statistics\n");
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02005145 err_code |= ERR_ALERT | ERR_FATAL;
5146 return err_code;
5147 }
5148
Christopher Fauletde79cd22021-01-06 07:41:56 +01005149 /* wait per-thread alloc to perform corresponding stat_l allocation */
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02005150
5151 return err_code;
5152}
5153
Emeric Brunf8642ee2021-10-29 17:59:18 +02005154REGISTER_CONFIG_POSTPARSER("allocate-stats-resolver", allocate_stats_rslv_postcheck);
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02005155
Christopher Fauletde79cd22021-01-06 07:41:56 +01005156static int allocate_stat_lines_per_thread(void)
5157{
Emeric Brunf8642ee2021-10-29 17:59:18 +02005158 int domains[] = { STATS_DOMAIN_PROXY, STATS_DOMAIN_RESOLVERS }, i;
Christopher Fauletde79cd22021-01-06 07:41:56 +01005159
5160 for (i = 0; i < STATS_DOMAIN_COUNT; ++i) {
5161 const int domain = domains[i];
5162
5163 stat_l[domain] = malloc(stat_count[domain] * sizeof(struct field));
5164 if (!stat_l[domain])
5165 return 0;
5166 }
5167 return 1;
5168}
5169
5170REGISTER_PER_THREAD_ALLOC(allocate_stat_lines_per_thread);
5171
Amaury Denoyelle7f8f6cb2020-11-10 14:24:31 +01005172static int allocate_trash_counters(void)
5173{
5174 struct stats_module *mod;
Emeric Brunf8642ee2021-10-29 17:59:18 +02005175 int domains[] = { STATS_DOMAIN_PROXY, STATS_DOMAIN_RESOLVERS }, i;
Amaury Denoyelle7f8f6cb2020-11-10 14:24:31 +01005176 size_t max_counters_size = 0;
5177
5178 /* calculate the greatest counters used by any stats modules */
5179 for (i = 0; i < STATS_DOMAIN_COUNT; ++i) {
5180 list_for_each_entry(mod, &stats_module_list[domains[i]], list) {
5181 max_counters_size = mod->counters_size > max_counters_size ?
5182 mod->counters_size : max_counters_size;
5183 }
5184 }
5185
5186 /* allocate the trash with the size of the greatest counters */
5187 if (max_counters_size) {
5188 trash_counters = malloc(max_counters_size);
5189 if (!trash_counters) {
5190 ha_alert("stats: cannot allocate trash counters for statistics\n");
5191 return 0;
5192 }
5193 }
5194
5195 return 1;
5196}
5197
5198REGISTER_PER_THREAD_ALLOC(allocate_trash_counters);
5199
Christopher Fauletde79cd22021-01-06 07:41:56 +01005200static void deinit_stat_lines_per_thread(void)
Amaury Denoyellea2a68992020-11-10 14:24:30 +01005201{
Emeric Brunf8642ee2021-10-29 17:59:18 +02005202 int domains[] = { STATS_DOMAIN_PROXY, STATS_DOMAIN_RESOLVERS }, i;
Amaury Denoyellea2a68992020-11-10 14:24:30 +01005203
5204 for (i = 0; i < STATS_DOMAIN_COUNT; ++i) {
5205 const int domain = domains[i];
5206
Willy Tarreau61cfdf42021-02-20 10:46:51 +01005207 ha_free(&stat_l[domain]);
Christopher Fauletde79cd22021-01-06 07:41:56 +01005208 }
5209}
5210
5211
5212REGISTER_PER_THREAD_FREE(deinit_stat_lines_per_thread);
5213
5214static void deinit_stats(void)
5215{
Emeric Brunf8642ee2021-10-29 17:59:18 +02005216 int domains[] = { STATS_DOMAIN_PROXY, STATS_DOMAIN_RESOLVERS }, i;
Christopher Fauletde79cd22021-01-06 07:41:56 +01005217
5218 for (i = 0; i < STATS_DOMAIN_COUNT; ++i) {
5219 const int domain = domains[i];
Amaury Denoyellea2a68992020-11-10 14:24:30 +01005220
5221 if (stat_f[domain])
5222 free(stat_f[domain]);
5223 }
5224}
5225
5226REGISTER_POST_DEINIT(deinit_stats);
5227
Amaury Denoyelle7f8f6cb2020-11-10 14:24:31 +01005228static void free_trash_counters(void)
5229{
5230 if (trash_counters)
5231 free(trash_counters);
5232}
5233
5234REGISTER_PER_THREAD_FREE(free_trash_counters);
5235
Willy Tarreau2b812e22016-11-22 16:18:05 +01005236/* register cli keywords */
5237static struct cli_kw_list cli_kws = {{ },{
Willy Tarreaub205bfd2021-05-07 11:38:37 +02005238 { { "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 +02005239 { { "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 +02005240 { { "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 },
5241 { { "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 +01005242 {{},}
5243}};
5244
Willy Tarreau0108d902018-11-25 19:14:37 +01005245INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
5246
William Lallemand74c24fb2016-11-21 17:18:36 +01005247struct applet http_stats_applet = {
5248 .obj_type = OBJ_TYPE_APPLET,
5249 .name = "<STATS>", /* used for logging */
5250 .fct = http_stats_io_handler,
5251 .release = NULL,
5252};
5253
William Lallemand74c24fb2016-11-21 17:18:36 +01005254/*
5255 * Local variables:
5256 * c-indent-level: 8
5257 * c-basic-offset: 8
5258 * End:
5259 */